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 30 2e 20 20 ersion 3.6.10.
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 36 37 33 36 20 6c 69 6e 65 rst.** 6736 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 31 2d 31 35 20 31 36 3a 30 2009-01-15 16:0
04c0: 30 3a 33 39 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 0:39 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 32 34 20 32 30 30 39 2f 30 31 2f 31 34 20 .824 2009/01/14
0790: 32 33 3a 30 33 3a 34 31 20 64 72 68 20 45 78 70 23:03:41 drh Exp
07a0: 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 $.*/.#ifndef _S
07b0: 51 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 QLITEINT_H_.#def
07c0: 69 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 ine _SQLITEINT_H
07d0: 5f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 _../*.** Include
07e0: 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 the configurati
07f0: 6f 6e 20 68 65 61 64 65 72 20 6f 75 74 70 75 74 on header output
0800: 20 62 79 20 27 63 6f 6e 66 69 67 75 72 65 27 20 by 'configure'
0810: 69 66 20 77 65 27 72 65 20 75 73 69 6e 67 20 74 if we're using t
0820: 68 65 0a 2a 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 he.** autoconf-b
0830: 61 73 65 64 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 ased build.*/.#i
0840: 66 64 65 66 20 5f 48 41 56 45 5f 53 51 4c 49 54 fdef _HAVE_SQLIT
0850: 45 5f 43 4f 4e 46 49 47 5f 48 0a 23 69 6e 63 6c E_CONFIG_H.#incl
0860: 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23 ude "config.h".#
0870: 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a endif../********
0880: 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 ****** Include s
0890: 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 69 6e 20 qliteLimit.h in
08a0: 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 the middle of sq
08b0: 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
08c0: 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
08d0: 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
08e0: 65 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 e sqliteLimit.h
08f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0900: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0910: 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
0920: 37 20 4d 61 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 7 May 7.**.** Th
0930: 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
0940: 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
0950: 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
0960: 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
0970: 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
0980: 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
0990: 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
09a0: 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
09b0: 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
09c0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
09d0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
09e0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
09f0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
0a00: 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
0a10: 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
0a20: 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
0a30: 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
0a40: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a80: 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 ******.** .** Th
0a90: 69 73 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 is file defines
0aa0: 76 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 6f various limits o
0ab0: 66 20 77 68 61 74 20 53 51 4c 69 74 65 20 63 61 f what SQLite ca
0ac0: 6e 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a n process..**.**
0ad0: 20 40 28 23 29 20 24 49 64 3a 20 73 71 6c 69 74 @(#) $Id: sqlit
0ae0: 65 4c 69 6d 69 74 2e 68 2c 76 20 31 2e 31 30 20 eLimit.h,v 1.10
0af0: 32 30 30 39 2f 30 31 2f 31 30 20 31 36 3a 31 35 2009/01/10 16:15
0b00: 3a 30 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :09 danielk1977
0b10: 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
0b20: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 The maximum leng
0b30: 74 68 20 6f 66 20 61 20 54 45 58 54 20 6f 72 20 th of a TEXT or
0b40: 42 4c 4f 42 20 69 6e 20 62 79 74 65 73 2e 20 20 BLOB in bytes.
0b50: 20 54 68 69 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69 This also.** li
0b60: 6d 69 74 73 20 74 68 65 20 73 69 7a 65 20 6f 66 mits the size of
0b70: 20 61 20 72 6f 77 20 69 6e 20 61 20 74 61 62 6c a row in a tabl
0b80: 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a e or index..**.*
0b90: 2a 20 54 68 65 20 68 61 72 64 20 6c 69 6d 69 74 * The hard limit
0ba0: 20 69 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 is the ability
0bb0: 6f 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e of a 32-bit sign
0bc0: 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 6f ed integer.** to
0bd0: 20 63 6f 75 6e 74 20 74 68 65 20 73 69 7a 65 3a count the size:
0be0: 20 32 5e 33 31 2d 31 20 6f 72 20 32 31 34 37 34 2^31-1 or 21474
0bf0: 38 33 36 34 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 83647..*/.#ifnde
0c00: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e f SQLITE_MAX_LEN
0c10: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c GTH.# define SQL
0c20: 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 31 ITE_MAX_LENGTH 1
0c30: 30 30 30 30 30 30 30 30 30 0a 23 65 6e 64 69 66 000000000.#endif
0c40: 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ../*.** This is
0c50: 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 the maximum numb
0c60: 65 72 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a er of.**.** *
0c70: 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 61 Columns in a ta
0c80: 62 6c 65 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 ble.** * Colu
0c90: 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a mns in an index.
0ca0: 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 ** * Columns
0cb0: 69 6e 20 61 20 76 69 65 77 0a 2a 2a 20 20 20 20 in a view.**
0cc0: 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 53 * Terms in the S
0cd0: 45 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e 20 ET clause of an
0ce0: 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 UPDATE statement
0cf0: 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 .** * Terms i
0d00: 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
0d10: 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 of a SELECT sta
0d20: 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 tement.** * T
0d30: 65 72 6d 73 20 69 6e 20 74 68 65 20 47 52 4f 55 erms in the GROU
0d40: 50 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59 P BY or ORDER BY
0d50: 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 53 45 clauses of a SE
0d60: 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a LECT statement..
0d70: 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e ** * Terms in
0d80: 20 74 68 65 20 56 41 4c 55 45 53 20 63 6c 61 75 the VALUES clau
0d90: 73 65 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 se of an INSERT
0da0: 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 statement.**.**
0db0: 54 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c The hard upper l
0dc0: 69 6d 69 74 20 68 65 72 65 20 69 73 20 33 32 36 imit here is 326
0dd0: 37 36 2e 20 20 4d 6f 73 74 20 64 61 74 61 62 61 76. Most databa
0de0: 73 65 20 70 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a se people will.*
0df0: 2a 20 74 65 6c 6c 20 79 6f 75 20 74 68 61 74 20 * tell you that
0e00: 69 6e 20 61 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c in a well-normal
0e10: 69 7a 65 64 20 64 61 74 61 62 61 73 65 2c 20 79 ized database, y
0e20: 6f 75 20 75 73 75 61 6c 6c 79 20 73 68 6f 75 6c ou usually shoul
0e30: 64 0a 2a 2a 20 6e 6f 74 20 68 61 76 65 20 6d 6f d.** not have mo
0e40: 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e 20 re than a dozen
0e50: 6f 72 20 73 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e or so columns in
0e60: 20 61 6e 79 20 74 61 62 6c 65 2e 20 20 41 6e 64 any table. And
0e70: 20 69 66 0a 2a 2a 20 74 68 61 74 20 69 73 20 74 if.** that is t
0e80: 68 65 20 63 61 73 65 2c 20 74 68 65 72 65 20 69 he case, there i
0e90: 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 68 61 s no point in ha
0ea0: 76 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 61 ving more than a
0eb0: 20 66 65 77 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 few.** dozen va
0ec0: 6c 75 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 lues in any of t
0ed0: 68 65 20 6f 74 68 65 72 20 73 69 74 75 61 74 69 he other situati
0ee0: 6f 6e 73 20 64 65 73 63 72 69 62 65 64 20 61 62 ons described ab
0ef0: 6f 76 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ove..*/.#ifndef
0f00: 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d SQLITE_MAX_COLUM
0f10: 4e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 N.# define SQLIT
0f20: 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 E_MAX_COLUMN 200
0f30: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
0f40: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 The maximum leng
0f50: 74 68 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 53 th of a single S
0f60: 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 QL statement in
0f70: 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 bytes..**.** It
0f80: 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 63 used to be the c
0f90: 61 73 65 20 74 68 61 74 20 73 65 74 74 69 6e 67 ase that setting
0fa0: 20 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 7a this value to z
0fb0: 65 72 6f 20 77 6f 75 6c 64 0a 2a 2a 20 74 75 72 ero would.** tur
0fc0: 6e 20 74 68 65 20 6c 69 6d 69 74 20 6f 66 66 2e n the limit off.
0fd0: 20 20 54 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e That is no lon
0fe0: 67 65 72 20 74 72 75 65 2e 20 20 49 74 20 69 73 ger true. It is
0ff0: 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a 2a not possible.**
1000: 20 74 6f 20 74 75 72 6e 20 74 68 69 73 20 6c 69 to turn this li
1010: 6d 69 74 20 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e mit off..*/.#ifn
1020: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 def SQLITE_MAX_S
1030: 51 4c 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 QL_LENGTH.# defi
1040: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 ne SQLITE_MAX_SQ
1050: 4c 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30 L_LENGTH 1000000
1060: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 000.#endif../*.*
1070: 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 * The maximum de
1080: 70 74 68 20 6f 66 20 61 6e 20 65 78 70 72 65 73 pth of an expres
1090: 73 69 6f 6e 20 74 72 65 65 2e 20 54 68 69 73 20 sion tree. This
10a0: 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 0a 2a is limited to .*
10b0: 2a 20 73 6f 6d 65 20 65 78 74 65 6e 74 20 62 79 * some extent by
10c0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f SQLITE_MAX_SQL_
10d0: 4c 45 4e 47 54 48 2e 20 42 75 74 20 73 6f 6d 65 LENGTH. But some
10e0: 74 69 6d 65 20 79 6f 75 20 6d 69 67 68 74 20 0a time you might .
10f0: 2a 2a 20 77 61 6e 74 20 74 6f 20 70 6c 61 63 65 ** want to place
1100: 20 6d 6f 72 65 20 73 65 76 65 72 65 20 6c 69 6d more severe lim
1110: 69 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 6c its on the compl
1120: 65 78 69 74 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 exity of an .**
1130: 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a expression..**.*
1140: 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 75 * A value of 0 u
1150: 73 65 64 20 74 6f 20 6d 65 61 6e 20 74 68 61 74 sed to mean that
1160: 20 74 68 65 20 6c 69 6d 69 74 20 77 61 73 20 6e the limit was n
1170: 6f 74 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 ot enforced..**
1180: 42 75 74 20 74 68 61 74 20 69 73 20 6e 6f 20 6c But that is no l
1190: 6f 6e 67 65 72 20 74 72 75 65 2e 20 20 54 68 65 onger true. The
11a0: 20 6c 69 6d 69 74 20 69 73 20 6e 6f 77 20 73 74 limit is now st
11b0: 72 69 63 74 6c 79 20 65 6e 66 6f 72 63 65 64 0a rictly enforced.
11c0: 2a 2a 20 61 74 20 61 6c 6c 20 74 69 6d 65 73 2e ** at all times.
11d0: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
11e0: 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 TE_MAX_EXPR_DEPT
11f0: 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 H.# define SQLIT
1200: 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 E_MAX_EXPR_DEPTH
1210: 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 1000.#endif../*
1220: 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 .** The maximum
1230: 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 number of terms
1240: 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 in a compound SE
1250: 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a LECT statement..
1260: 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 ** The code gene
1270: 72 61 74 6f 72 20 66 6f 72 20 63 6f 6d 70 6f 75 rator for compou
1280: 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d nd SELECT statem
1290: 65 6e 74 73 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a ents does one.**
12a0: 20 6c 65 76 65 6c 20 6f 66 20 72 65 63 75 72 73 level of recurs
12b0: 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 65 72 ion for each ter
12c0: 6d 2e 20 20 41 20 73 74 61 63 6b 20 6f 76 65 72 m. A stack over
12d0: 66 6c 6f 77 20 63 61 6e 20 72 65 73 75 6c 74 0a flow can result.
12e0: 2a 2a 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 ** if the number
12f0: 20 6f 66 20 74 65 72 6d 73 20 69 73 20 74 6f 6f of terms is too
1300: 20 6c 61 72 67 65 2e 20 20 49 6e 20 70 72 61 63 large. In prac
1310: 74 69 63 65 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a tice, most SQL.*
1320: 2a 20 6e 65 76 65 72 20 68 61 73 20 6d 6f 72 65 * never has more
1330: 20 74 68 61 6e 20 33 20 6f 72 20 34 20 74 65 72 than 3 or 4 ter
1340: 6d 73 2e 20 20 55 73 65 20 61 20 76 61 6c 75 65 ms. Use a value
1350: 20 6f 66 20 30 20 74 6f 20 64 69 73 61 62 6c 65 of 0 to disable
1360: 0a 2a 2a 20 61 6e 79 20 6c 69 6d 69 74 20 6f 6e .** any limit on
1370: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 the number of t
1380: 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 erms in a compou
1390: 6e 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 nt SELECT..*/.#i
13a0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
13b0: 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 _COMPOUND_SELECT
13c0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
13d0: 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 _MAX_COMPOUND_SE
13e0: 4c 45 43 54 20 35 30 30 0a 23 65 6e 64 69 66 0a LECT 500.#endif.
13f0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d ./*.** The maxim
1400: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 63 um number of opc
1410: 6f 64 65 73 20 69 6e 20 61 20 56 44 42 45 20 70 odes in a VDBE p
1420: 72 6f 67 72 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 rogram..** Not c
1430: 75 72 72 65 6e 74 6c 79 20 65 6e 66 6f 72 63 65 urrently enforce
1440: 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 d..*/.#ifndef SQ
1450: 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 LITE_MAX_VDBE_OP
1460: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
1470: 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20 32 35 30 _MAX_VDBE_OP 250
1480: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 00.#endif../*.**
1490: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d The maximum num
14a0: 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 ber of arguments
14b0: 20 74 6f 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 to an SQL funct
14c0: 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ion..*/.#ifndef
14d0: 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 SQLITE_MAX_FUNCT
14e0: 49 4f 4e 5f 41 52 47 0a 23 20 64 65 66 69 6e 65 ION_ARG.# define
14f0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 SQLITE_MAX_FUNC
1500: 54 49 4f 4e 5f 41 52 47 20 31 32 37 0a 23 65 6e TION_ARG 127.#en
1510: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d dif../*.** The m
1520: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
1530: 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 in-memory pages
1540: 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 to use for the
1550: 6d 61 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a main database.**
1560: 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 74 table and for t
1570: 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e emporary tables.
1580: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46 The SQLITE_DEF
1590: 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a AULT_CACHE_SIZE.
15a0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
15b0: 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f E_DEFAULT_CACHE_
15c0: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SIZE.# define SQ
15d0: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 LITE_DEFAULT_CAC
15e0: 48 45 5f 53 49 5a 45 20 20 32 30 30 30 0a 23 65 HE_SIZE 2000.#e
15f0: 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c ndif.#ifndef SQL
1600: 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 ITE_DEFAULT_TEMP
1610: 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 65 _CACHE_SIZE.# de
1620: 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 fine SQLITE_DEFA
1630: 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 ULT_TEMP_CACHE_S
1640: 49 5a 45 20 20 35 30 30 0a 23 65 6e 64 69 66 0a IZE 500.#endif.
1650: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d ./*.** The maxim
1660: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 74 74 um number of att
1670: 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e ached databases.
1680: 20 20 54 68 69 73 20 6d 75 73 74 20 62 65 20 62 This must be b
1690: 65 74 77 65 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 etween 0.** and
16a0: 33 30 2e 20 20 54 68 65 20 75 70 70 65 72 20 62 30. The upper b
16b0: 6f 75 6e 64 20 6f 6e 20 33 30 20 69 73 20 62 65 ound on 30 is be
16c0: 63 61 75 73 65 20 61 20 33 32 2d 62 69 74 20 69 cause a 32-bit i
16d0: 6e 74 65 67 65 72 20 62 69 74 6d 61 70 0a 2a 2a nteger bitmap.**
16e0: 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 is used interna
16f0: 6c 6c 79 20 74 6f 20 74 72 61 63 6b 20 61 74 74 lly to track att
1700: 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e ached databases.
1710: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1720: 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 0a TE_MAX_ATTACHED.
1730: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1740: 4d 41 58 5f 41 54 54 41 43 48 45 44 20 31 30 0a MAX_ATTACHED 10.
1750: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 #endif.../*.** T
1760: 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 he maximum value
1770: 20 6f 66 20 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 of a ?nnn wildc
1780: 61 72 64 20 74 68 61 74 20 74 68 65 20 70 61 72 ard that the par
1790: 73 65 72 20 77 69 6c 6c 20 61 63 63 65 70 74 2e ser will accept.
17a0: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
17b0: 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f TE_MAX_VARIABLE_
17c0: 4e 55 4d 42 45 52 0a 23 20 64 65 66 69 6e 65 20 NUMBER.# define
17d0: 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 SQLITE_MAX_VARIA
17e0: 42 4c 45 5f 4e 55 4d 42 45 52 20 39 39 39 0a 23 BLE_NUMBER 999.#
17f0: 65 6e 64 69 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75 endif../* Maximu
1800: 6d 20 70 61 67 65 20 73 69 7a 65 2e 20 20 54 68 m page size. Th
1810: 65 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e e upper bound on
1820: 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 33 this value is 3
1830: 32 37 36 38 2e 20 20 54 68 69 73 20 61 20 6c 69 2768. This a li
1840: 6d 69 74 0a 2a 2a 20 69 6d 70 6f 73 65 64 20 62 mit.** imposed b
1850: 79 20 74 68 65 20 6e 65 63 65 73 73 69 74 79 20 y the necessity
1860: 6f 66 20 73 74 6f 72 69 6e 67 20 74 68 65 20 76 of storing the v
1870: 61 6c 75 65 20 69 6e 20 61 20 32 2d 62 79 74 65 alue in a 2-byte
1880: 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 unsigned intege
1890: 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 66 61 63 r.** and the fac
18a0: 74 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 t that the page
18b0: 73 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 size must be a p
18c0: 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a ower of 2..**.**
18d0: 20 49 66 20 74 68 69 73 20 6c 69 6d 69 74 20 69 If this limit i
18e0: 73 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 s changed, then
18f0: 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 6c 69 62 the compiled lib
1900: 72 61 72 79 20 69 73 20 74 65 63 68 6e 69 63 61 rary is technica
1910: 6c 6c 79 0a 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 lly.** incompati
1920: 62 6c 65 20 77 69 74 68 20 61 6e 20 53 51 4c 69 ble with an SQLi
1930: 74 65 20 6c 69 62 72 61 72 79 20 63 6f 6d 70 69 te library compi
1940: 6c 65 64 20 77 69 74 68 20 61 20 64 69 66 66 65 led with a diffe
1950: 72 65 6e 74 20 6c 69 6d 69 74 2e 20 49 66 0a 2a rent limit. If.*
1960: 2a 20 61 20 70 72 6f 63 65 73 73 20 6f 70 65 72 * a process oper
1970: 61 74 69 6e 67 20 6f 6e 20 61 20 64 61 74 61 62 ating on a datab
1980: 61 73 65 20 77 69 74 68 20 61 20 70 61 67 65 2d ase with a page-
1990: 73 69 7a 65 20 6f 66 20 36 35 35 33 36 20 62 79 size of 65536 by
19a0: 74 65 73 20 0a 2a 2a 20 63 72 61 73 68 65 73 2c tes .** crashes,
19b0: 20 74 68 65 6e 20 61 6e 20 69 6e 73 74 61 6e 63 then an instanc
19c0: 65 20 6f 66 20 53 51 4c 69 74 65 20 63 6f 6d 70 e of SQLite comp
19d0: 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 64 65 iled with the de
19e0: 66 61 75 6c 74 20 70 61 67 65 2d 73 69 7a 65 20 fault page-size
19f0: 0a 2a 2a 20 6c 69 6d 69 74 20 77 69 6c 6c 20 6e .** limit will n
1a00: 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f ot be able to ro
1a10: 6c 6c 62 61 63 6b 20 74 68 65 20 61 62 6f 72 74 llback the abort
1a20: 65 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ed transaction.
1a30: 54 68 69 73 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65 This could.** le
1a40: 61 64 20 74 6f 20 64 61 74 61 62 61 73 65 20 63 ad to database c
1a50: 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 orruption..*/.#i
1a60: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
1a70: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 _PAGE_SIZE.# def
1a80: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 ine SQLITE_MAX_P
1a90: 41 47 45 5f 53 49 5a 45 20 33 32 37 36 38 0a 23 AGE_SIZE 32768.#
1aa0: 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 endif.../*.** Th
1ab0: 65 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f e default size o
1ac0: 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 f a database pag
1ad0: 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 e..*/.#ifndef SQ
1ae0: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 LITE_DEFAULT_PAG
1af0: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 E_SIZE.# define
1b00: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 SQLITE_DEFAULT_P
1b10: 41 47 45 5f 53 49 5a 45 20 31 30 32 34 0a 23 65 AGE_SIZE 1024.#e
1b20: 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f ndif.#if SQLITE_
1b30: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
1b40: 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 E>SQLITE_MAX_PAG
1b50: 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 E_SIZE.# undef S
1b60: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 QLITE_DEFAULT_PA
1b70: 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 GE_SIZE.# define
1b80: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
1b90: 50 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54 45 PAGE_SIZE SQLITE
1ba0: 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 _MAX_PAGE_SIZE.#
1bb0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 endif../*.** Ord
1bc0: 69 6e 61 72 69 6c 79 2c 20 69 66 20 6e 6f 20 76 inarily, if no v
1bd0: 61 6c 75 65 20 69 73 20 65 78 70 6c 69 63 69 74 alue is explicit
1be0: 6c 79 20 70 72 6f 76 69 64 65 64 2c 20 53 51 4c ly provided, SQL
1bf0: 69 74 65 20 63 72 65 61 74 65 73 20 64 61 74 61 ite creates data
1c00: 62 61 73 65 73 0a 2a 2a 20 77 69 74 68 20 70 61 bases.** with pa
1c10: 67 65 20 73 69 7a 65 20 53 51 4c 49 54 45 5f 44 ge size SQLITE_D
1c20: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
1c30: 2e 20 48 6f 77 65 76 65 72 2c 20 62 61 73 65 64 . However, based
1c40: 20 6f 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 64 on certain.** d
1c50: 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 evice characteri
1c60: 73 74 69 63 73 20 28 73 65 63 74 6f 72 2d 73 69 stics (sector-si
1c70: 7a 65 20 61 6e 64 20 61 74 6f 6d 69 63 20 77 72 ze and atomic wr
1c80: 69 74 65 28 29 20 73 75 70 70 6f 72 74 29 2c 0a ite() support),.
1c90: 2a 2a 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68 ** SQLite may ch
1ca0: 6f 6f 73 65 20 61 20 6c 61 72 67 65 72 20 76 61 oose a larger va
1cb0: 6c 75 65 2e 20 54 68 69 73 20 63 6f 6e 73 74 61 lue. This consta
1cc0: 6e 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 nt is the maximu
1cd0: 6d 20 76 61 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 m value.** SQLit
1ce0: 65 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e e will choose on
1cf0: 20 69 74 73 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 its own..*/.#if
1d00: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f ndef SQLITE_MAX_
1d10: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
1d20: 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
1d30: 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 E_MAX_DEFAULT_PA
1d40: 47 45 5f 53 49 5a 45 20 38 31 39 32 0a 23 65 6e GE_SIZE 8192.#en
1d50: 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d dif.#if SQLITE_M
1d60: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
1d70: 53 49 5a 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f SIZE>SQLITE_MAX_
1d80: 50 41 47 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65 PAGE_SIZE.# unde
1d90: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 f SQLITE_MAX_DEF
1da0: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 AULT_PAGE_SIZE.#
1db0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
1dc0: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
1dd0: 53 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f SIZE SQLITE_MAX_
1de0: 50 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 PAGE_SIZE.#endif
1df0: 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d .../*.** Maximum
1e00: 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1e10: 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 in one database
1e20: 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 file..**.** Thi
1e30: 73 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 s is really just
1e40: 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c the default val
1e50: 75 65 20 66 6f 72 20 74 68 65 20 6d 61 78 5f 70 ue for the max_p
1e60: 61 67 65 5f 63 6f 75 6e 74 20 70 72 61 67 6d 61 age_count pragma
1e70: 2e 0a 2a 2a 20 54 68 69 73 20 76 61 6c 75 65 20 ..** This value
1e80: 63 61 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 28 can be lowered (
1e90: 6f 72 20 72 61 69 73 65 64 29 20 61 74 20 72 75 or raised) at ru
1ea0: 6e 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68 61 n-time using tha
1eb0: 74 20 74 68 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 t the.** max_pag
1ec0: 65 5f 63 6f 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a e_count macro..*
1ed0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1ee0: 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 0a _MAX_PAGE_COUNT.
1ef0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1f00: 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 20 31 MAX_PAGE_COUNT 1
1f10: 30 37 33 37 34 31 38 32 33 0a 23 65 6e 64 69 66 073741823.#endif
1f20: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 ../*.** Maximum
1f30: 6c 65 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73 length (in bytes
1f40: 29 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e ) of the pattern
1f50: 20 69 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c in a LIKE or GL
1f60: 4f 42 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a OB.** operator..
1f70: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1f80: 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 E_MAX_LIKE_PATTE
1f90: 52 4e 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 RN_LENGTH.# defi
1fa0: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 ne SQLITE_MAX_LI
1fb0: 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 KE_PATTERN_LENGT
1fc0: 48 20 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a H 50000.#endif..
1fd0: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1fe0: 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 4c 69 6d End of sqliteLim
1ff0: 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a it.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 2f 0a **************/.
2020: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
2030: 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
2040: 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
2050: 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
2060: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
2070: 0a 2f 2a 20 44 69 73 61 62 6c 65 20 6e 75 69 73 ./* Disable nuis
2080: 61 6e 63 65 20 77 61 72 6e 69 6e 67 73 20 6f 6e ance warnings on
2090: 20 42 6f 72 6c 61 6e 64 20 63 6f 6d 70 69 6c 65 Borland compile
20a0: 72 73 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 rs */.#if define
20b0: 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a d(__BORLANDC__).
20c0: 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 72 63 #pragma warn -rc
20d0: 68 20 2f 2a 20 75 6e 72 65 61 63 68 61 62 6c 65 h /* unreachable
20e0: 20 63 6f 64 65 20 2a 2f 0a 23 70 72 61 67 6d 61 code */.#pragma
20f0: 20 77 61 72 6e 20 2d 63 63 63 20 2f 2a 20 43 6f warn -ccc /* Co
2100: 6e 64 69 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 ndition is alway
2110: 73 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 20 s true or false
2120: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 */.#pragma warn
2130: 2d 61 75 73 20 2f 2a 20 41 73 73 69 67 6e 65 64 -aus /* Assigned
2140: 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20 value is never
2150: 75 73 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 used */.#pragma
2160: 77 61 72 6e 20 2d 63 73 75 20 2f 2a 20 43 6f 6d warn -csu /* Com
2170: 70 61 72 69 6e 67 20 73 69 67 6e 65 64 20 61 6e paring signed an
2180: 64 20 75 6e 73 69 67 6e 65 64 20 2a 2f 0a 23 70 d unsigned */.#p
2190: 72 61 67 6d 61 20 77 61 72 6e 20 2d 73 70 61 20 ragma warn -spa
21a0: 2f 2a 20 53 75 73 70 69 63 69 6f 75 73 20 70 6f /* Suspicious po
21b0: 69 6e 74 65 72 20 61 72 69 74 68 6d 65 74 69 63 inter arithmetic
21c0: 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4e */.#endif../* N
21d0: 65 65 64 65 64 20 66 6f 72 20 76 61 72 69 6f 75 eeded for variou
21e0: 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e 2e s definitions...
21f0: 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 47 4e 55 */.#ifndef _GNU
2200: 5f 53 4f 55 52 43 45 0a 23 20 64 65 66 69 6e 65 _SOURCE.# define
2210: 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 65 6e _GNU_SOURCE.#en
2220: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 dif../*.** Inclu
2230: 64 65 20 73 74 61 6e 64 61 72 64 20 68 65 61 64 de standard head
2240: 65 72 20 66 69 6c 65 73 20 61 73 20 6e 65 63 65 er files as nece
2250: 73 73 61 72 79 0a 2a 2f 0a 23 69 66 64 65 66 20 ssary.*/.#ifdef
2260: 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23 69 HAVE_STDINT_H.#i
2270: 6e 63 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e 68 nclude <stdint.h
2280: 3e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 >.#endif.#ifdef
2290: 48 41 56 45 5f 49 4e 54 54 59 50 45 53 5f 48 0a HAVE_INTTYPES_H.
22a0: 23 69 6e 63 6c 75 64 65 20 3c 69 6e 74 74 79 70 #include <inttyp
22b0: 65 73 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a es.h>.#endif../*
22c0: 0a 20 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 . * This macro i
22d0: 73 20 75 73 65 64 20 74 6f 20 22 68 69 64 65 22 s used to "hide"
22e0: 20 73 6f 6d 65 20 75 67 6c 69 6e 65 73 73 20 69 some ugliness i
22f0: 6e 20 63 61 73 74 69 6e 67 20 61 6e 20 69 6e 74 n casting an int
2300: 0a 20 2a 20 76 61 6c 75 65 20 74 6f 20 61 20 70 . * value to a p
2310: 74 72 20 76 61 6c 75 65 20 75 6e 64 65 72 20 74 tr value under t
2320: 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20 63 he MSVC 64-bit c
2330: 6f 6d 70 69 6c 65 72 2e 20 20 20 43 61 73 74 69 ompiler. Casti
2340: 6e 67 0a 20 2a 20 6e 6f 6e 20 36 34 2d 62 69 74 ng. * non 64-bit
2350: 20 76 61 6c 75 65 73 20 74 6f 20 70 74 72 20 74 values to ptr t
2360: 79 70 65 73 20 72 65 73 75 6c 74 73 20 69 6e 20 ypes results in
2370: 61 20 22 68 61 72 64 22 20 65 72 72 6f 72 20 77 a "hard" error w
2380: 69 74 68 20 0a 20 2a 20 74 68 65 20 4d 53 56 43 ith . * the MSVC
2390: 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 72 64-bit compiler
23a0: 20 77 68 69 63 68 20 74 68 69 73 20 61 74 74 65 which this atte
23b0: 6d 70 74 73 20 74 6f 20 61 76 6f 69 64 2e 20 20 mpts to avoid.
23c0: 0a 20 2a 0a 20 2a 20 41 20 73 69 6d 70 6c 65 20 . *. * A simple
23d0: 63 6f 6d 70 69 6c 65 72 20 70 72 61 67 6d 61 20 compiler pragma
23e0: 6f 72 20 63 61 73 74 69 6e 67 20 73 65 71 75 65 or casting seque
23f0: 6e 63 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 nce could not be
2400: 20 66 6f 75 6e 64 0a 20 2a 20 74 6f 20 63 6f 72 found. * to cor
2410: 72 65 63 74 20 74 68 69 73 20 69 6e 20 61 6c 6c rect this in all
2420: 20 73 69 74 75 61 74 69 6f 6e 73 2c 20 73 6f 20 situations, so
2430: 74 68 69 73 20 6d 61 63 72 6f 20 77 61 73 20 69 this macro was i
2440: 6e 74 72 6f 64 75 63 65 64 2e 0a 20 2a 0a 20 2a ntroduced.. *. *
2450: 20 49 74 20 63 6f 75 6c 64 20 62 65 20 61 72 67 It could be arg
2460: 75 65 64 20 74 68 61 74 20 74 68 65 20 69 6e 74 ued that the int
2470: 70 74 72 5f 74 20 74 79 70 65 20 63 6f 75 6c 64 ptr_t type could
2480: 20 62 65 20 75 73 65 64 20 69 6e 20 74 68 69 73 be used in this
2490: 0a 20 2a 20 63 61 73 65 2c 20 62 75 74 20 74 68 . * case, but th
24a0: 61 74 20 74 79 70 65 20 69 73 20 6e 6f 74 20 61 at type is not a
24b0: 76 61 69 6c 61 62 6c 65 20 6f 6e 20 61 6c 6c 20 vailable on all
24c0: 63 6f 6d 70 69 6c 65 72 73 2c 20 6f 72 20 0a 20 compilers, or .
24d0: 2a 20 72 65 71 75 69 72 65 73 20 74 68 65 20 23 * requires the #
24e0: 69 6e 63 6c 75 64 65 20 6f 66 20 73 70 65 63 69 include of speci
24f0: 66 69 63 20 68 65 61 64 65 72 73 20 77 68 69 63 fic headers whic
2500: 68 20 64 69 66 66 65 72 73 20 62 65 74 77 65 65 h differs betwee
2510: 6e 0a 20 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 0a n. * platforms..
2520: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
2530: 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29 TE_INT_TO_PTR(X)
2540: 20 20 20 28 28 76 6f 69 64 2a 29 26 28 28 63 68 ((void*)&((ch
2550: 61 72 2a 29 30 29 5b 58 5d 29 0a 23 64 65 66 69 ar*)0)[X]).#defi
2560: 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f ne SQLITE_PTR_TO
2570: 5f 49 4e 54 28 58 29 20 20 20 28 28 69 6e 74 29 _INT(X) ((int)
2580: 28 28 28 63 68 61 72 2a 29 58 29 2d 28 63 68 61 (((char*)X)-(cha
2590: 72 2a 29 30 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 r*)0))../*.** Th
25a0: 65 73 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f ese #defines sho
25b0: 75 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 uld enable >2GB
25c0: 66 69 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 file support on
25d0: 50 4f 53 49 58 20 69 66 20 74 68 65 0a 2a 2a 20 POSIX if the.**
25e0: 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 underlying opera
25f0: 74 69 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 ting system supp
2600: 6f 72 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 orts it. If the
2610: 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 OS lacks.** lar
2620: 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c ge file support,
2630: 20 6f 72 20 69 66 20 74 68 65 20 4f 53 20 69 73 or if the OS is
2640: 20 77 69 6e 64 6f 77 73 2c 20 74 68 65 73 65 20 windows, these
2650: 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 73 should be no-ops
2660: 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 ..**.** Ticket #
2670: 32 37 33 39 3a 20 20 54 68 65 20 5f 4c 41 52 47 2739: The _LARG
2680: 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 6d 61 63 EFILE_SOURCE mac
2690: 72 6f 20 6d 75 73 74 20 61 70 70 65 61 72 20 62 ro must appear b
26a0: 65 66 6f 72 65 20 61 6e 79 0a 2a 2a 20 73 79 73 efore any.** sys
26b0: 74 65 6d 20 23 69 6e 63 6c 75 64 65 73 2e 20 20 tem #includes.
26c0: 48 65 6e 63 65 2c 20 74 68 69 73 20 62 6c 6f 63 Hence, this bloc
26d0: 6b 20 6f 66 20 63 6f 64 65 20 6d 75 73 74 20 62 k of code must b
26e0: 65 20 74 68 65 20 76 65 72 79 20 66 69 72 73 74 e the very first
26f0: 0a 2a 2a 20 63 6f 64 65 20 69 6e 20 61 6c 6c 20 .** code in all
2700: 73 6f 75 72 63 65 20 66 69 6c 65 73 2e 0a 2a 2a source files..**
2710: 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65 20 73 .** Large file s
2720: 75 70 70 6f 72 74 20 63 61 6e 20 62 65 20 64 69 upport can be di
2730: 73 61 62 6c 65 64 20 75 73 69 6e 67 20 74 68 65 sabled using the
2740: 20 2d 44 53 51 4c 49 54 45 5f 44 49 53 41 42 4c -DSQLITE_DISABL
2750: 45 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a 2a 20 E_LFS switch.**
2760: 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 on the compiler
2770: 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20 20 54 command line. T
2780: 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 his is necessary
2790: 20 69 66 20 79 6f 75 20 61 72 65 20 63 6f 6d 70 if you are comp
27a0: 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20 72 65 iling.** on a re
27b0: 63 65 6e 74 20 6d 61 63 68 69 6e 65 20 28 65 78 cent machine (ex
27c0: 3a 20 52 65 64 20 48 61 74 20 37 2e 32 29 20 62 : Red Hat 7.2) b
27d0: 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f 75 72 ut you want your
27e0: 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a code to work.**
27f0: 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61 63 on an older mac
2800: 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 48 61 hine (ex: Red Ha
2810: 74 20 36 2e 30 29 2e 20 20 49 66 20 79 6f 75 20 t 6.0). If you
2820: 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 20 48 compile on Red H
2830: 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f 75 at 7.2.** withou
2840: 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c t this option, L
2850: 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 42 FS is enable. B
2860: 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20 ut LFS does not
2870: 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b 65 72 exist in the ker
2880: 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 20 48 61 nel.** in Red Ha
2890: 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f t 6.0, so the co
28a0: 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 de won't work.
28b0: 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d Hence, for maxim
28c0: 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72 um binary.** por
28d0: 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f tability you sho
28e0: 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a uld omit LFS..**
28f0: 0a 2a 2a 20 53 69 6d 69 6c 61 72 20 69 73 20 74 .** Similar is t
2900: 72 75 65 20 66 6f 72 20 4d 61 63 20 4f 53 20 58 rue for Mac OS X
2910: 2e 20 20 4c 46 53 20 69 73 20 6f 6e 6c 79 20 73 . LFS is only s
2920: 75 70 70 6f 72 74 65 64 20 6f 6e 20 4d 61 63 20 upported on Mac
2930: 4f 53 20 58 20 39 20 61 6e 64 20 6c 61 74 65 72 OS X 9 and later
2940: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
2950: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a ITE_DISABLE_LFS.
2960: 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f # define _LARGE_
2970: 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 FILE 1.# i
2980: 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 fndef _FILE_OFFS
2990: 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 ET_BITS.# defi
29a0: 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f ne _FILE_OFFSET_
29b0: 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a BITS 64.# endif.
29c0: 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 # define _LARGEF
29d0: 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e ILE_SOURCE 1.#en
29e0: 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 dif.../*.** The
29f0: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
2a00: 45 20 6d 61 63 72 6f 20 6d 75 73 74 20 62 65 20 E macro must be
2a10: 64 65 66 69 6e 65 64 20 61 73 20 65 69 74 68 65 defined as eithe
2a20: 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 4f 6c 64 r 0 or 1..** Old
2a30: 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 er versions of S
2a40: 51 4c 69 74 65 20 75 73 65 64 20 61 6e 20 6f 70 QLite used an op
2a50: 74 69 6f 6e 61 6c 20 54 48 52 45 41 44 53 41 46 tional THREADSAF
2a60: 45 20 6d 61 63 72 6f 2e 0a 2a 2a 20 57 65 20 73 E macro..** We s
2a70: 75 70 70 6f 72 74 20 74 68 61 74 20 66 6f 72 20 upport that for
2a80: 6c 65 67 61 63 79 0a 2a 2f 0a 23 69 66 20 21 64 legacy.*/.#if !d
2a90: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 efined(SQLITE_TH
2aa0: 52 45 41 44 53 41 46 45 29 0a 23 69 66 20 64 65 READSAFE).#if de
2ab0: 66 69 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 fined(THREADSAFE
2ac0: 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 ).# define SQLIT
2ad0: 45 5f 54 48 52 45 41 44 53 41 46 45 20 54 48 52 E_THREADSAFE THR
2ae0: 45 41 44 53 41 46 45 0a 23 65 6c 73 65 0a 23 20 EADSAFE.#else.#
2af0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 define SQLITE_TH
2b00: 52 45 41 44 53 41 46 45 20 31 0a 23 65 6e 64 69 READSAFE 1.#endi
2b10: 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 f.#endif../*.**
2b20: 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 The SQLITE_DEFAU
2b30: 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20 6d 61 63 LT_MEMSTATUS mac
2b40: 72 6f 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e ro must be defin
2b50: 65 64 20 61 73 20 65 69 74 68 65 72 20 30 20 6f ed as either 0 o
2b60: 72 20 31 2e 0a 2a 2a 20 49 74 20 64 65 74 65 72 r 1..** It deter
2b70: 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 mines whether or
2b80: 20 6e 6f 74 20 74 68 65 20 66 65 61 74 75 72 65 not the feature
2b90: 73 20 72 65 6c 61 74 65 64 20 74 6f 20 0a 2a 2a s related to .**
2ba0: 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d SQLITE_CONFIG_M
2bb0: 45 4d 53 54 41 54 55 53 20 61 72 65 20 61 76 61 EMSTATUS are ava
2bc0: 69 6c 61 62 6c 65 20 62 79 20 64 65 66 61 75 6c ilable by defaul
2bd0: 74 20 6f 72 20 6e 6f 74 2e 20 54 68 69 73 20 76 t or not. This v
2be0: 61 6c 75 65 20 63 61 6e 0a 2a 2a 20 62 65 20 6f alue can.** be o
2bf0: 76 65 72 72 69 64 64 65 6e 20 61 74 20 72 75 6e verridden at run
2c00: 74 69 6d 65 20 75 73 69 6e 67 20 74 68 65 20 73 time using the s
2c10: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 qlite3_config()
2c20: 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 API..*/.#if !def
2c30: 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 46 41 ined(SQLITE_DEFA
2c40: 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 29 0a 23 ULT_MEMSTATUS).#
2c50: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 define SQLITE_D
2c60: 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 EFAULT_MEMSTATUS
2c70: 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 1.#endif../*.**
2c80: 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 Exactly one of
2c90: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 the following ma
2ca0: 63 72 6f 73 20 6d 75 73 74 20 62 65 20 64 65 66 cros must be def
2cb0: 69 6e 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f ined in order to
2cc0: 0a 2a 2a 20 73 70 65 63 69 66 79 20 77 68 69 63 .** specify whic
2cd0: 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 h memory allocat
2ce0: 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 74 6f ion subsystem to
2cf0: 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 use..**.**
2d00: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
2d10: 4c 4c 4f 43 20 20 20 20 20 20 20 20 20 20 2f 2f LLOC //
2d20: 20 55 73 65 20 6e 6f 72 6d 61 6c 20 73 79 73 74 Use normal syst
2d30: 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 em malloc().**
2d40: 20 20 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 SQLITE_MEMDEB
2d50: 55 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 UG
2d60: 20 2f 2f 20 44 65 62 75 67 67 69 6e 67 20 76 65 // Debugging ve
2d70: 72 73 69 6f 6e 20 6f 66 20 73 79 73 74 65 6d 20 rsion of system
2d80: 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 malloc().**
2d90: 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 SQLITE_MEMORY_SI
2da0: 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f ZE //
2db0: 20 69 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 internal alloca
2dc0: 74 6f 72 20 23 31 0a 2a 2a 20 20 20 20 20 53 51 tor #1.** SQ
2dd0: 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 LITE_MMAP_HEAP_S
2de0: 49 5a 45 20 20 20 20 20 20 20 20 20 2f 2f 20 69 IZE // i
2df0: 6e 74 65 72 6e 61 6c 20 6d 6d 61 70 28 29 20 61 nternal mmap() a
2e00: 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 20 20 20 20 20 llocator.**
2e10: 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f SQLITE_POW2_MEMO
2e20: 52 59 5f 53 49 5a 45 20 20 20 20 20 20 20 2f 2f RY_SIZE //
2e30: 20 69 6e 74 65 72 6e 61 6c 20 70 6f 77 65 72 2d internal power-
2e40: 6f 66 2d 74 77 6f 20 61 6c 6c 6f 63 61 74 6f 72 of-two allocator
2e50: 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f .**.** If none o
2e60: 66 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 f the above are
2e70: 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 73 65 defined, then se
2e80: 74 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f t SQLITE_SYSTEM_
2e90: 4d 41 4c 4c 4f 43 20 61 73 0a 2a 2a 20 74 68 65 MALLOC as.** the
2ea0: 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 23 69 66 default..*/.#if
2eb0: 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
2ec0: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 SYSTEM_MALLOC)+d
2ed0: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 efined(SQLITE_ME
2ee0: 4d 44 45 42 55 47 29 2b 5c 0a 20 20 20 20 64 65 MDEBUG)+\. de
2ef0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d fined(SQLITE_MEM
2f00: 4f 52 59 5f 53 49 5a 45 29 2b 64 65 66 69 6e 65 ORY_SIZE)+define
2f10: 64 28 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 d(SQLITE_MMAP_HE
2f20: 41 50 5f 53 49 5a 45 29 2b 5c 0a 20 20 20 20 64 AP_SIZE)+\. d
2f30: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f efined(SQLITE_PO
2f40: 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 3e W2_MEMORY_SIZE)>
2f50: 31 0a 23 20 65 72 72 6f 72 20 22 41 74 20 6d 6f 1.# error "At mo
2f60: 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f st one of the fo
2f70: 6c 6c 6f 77 69 6e 67 20 63 6f 6d 70 69 6c 65 2d llowing compile-
2f80: 74 69 6d 65 20 63 6f 6e 66 69 67 75 72 61 74 69 time configurati
2f90: 6f 6e 20 6f 70 74 69 6f 6e 73 5c 0a 20 69 73 20 on options\. is
2fa0: 61 6c 6c 6f 77 73 3a 20 53 51 4c 49 54 45 5f 53 allows: SQLITE_S
2fb0: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 2c 20 53 51 YSTEM_MALLOC, SQ
2fc0: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 2c 20 53 LITE_MEMDEBUG, S
2fd0: 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a QLITE_MEMORY_SIZ
2fe0: 45 2c 5c 0a 20 53 51 4c 49 54 45 5f 4d 4d 41 50 E,\. SQLITE_MMAP
2ff0: 5f 48 45 41 50 5f 53 49 5a 45 2c 20 53 51 4c 49 _HEAP_SIZE, SQLI
3000: 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 TE_POW2_MEMORY_S
3010: 49 5a 45 22 0a 23 65 6e 64 69 66 0a 23 69 66 20 IZE".#endif.#if
3020: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 defined(SQLITE_S
3030: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 YSTEM_MALLOC)+de
3040: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d fined(SQLITE_MEM
3050: 44 45 42 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 DEBUG)+\. def
3060: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f ined(SQLITE_MEMO
3070: 52 59 5f 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 RY_SIZE)+defined
3080: 28 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 (SQLITE_MMAP_HEA
3090: 50 5f 53 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 P_SIZE)+\. de
30a0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 fined(SQLITE_POW
30b0: 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 3d 3d 2_MEMORY_SIZE)==
30c0: 30 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 0.# define SQLIT
30d0: 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 E_SYSTEM_MALLOC
30e0: 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 1.#endif../*.**
30f0: 49 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 If SQLITE_MALLOC
3100: 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 69 73 20 64 _SOFT_LIMIT is d
3110: 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 72 79 efined, then try
3120: 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 to keep the.**
3130: 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 sizes of memory
3140: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f allocations belo
3150: 77 20 74 68 69 73 20 76 61 6c 75 65 20 77 68 65 w this value whe
3160: 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a re possible..*/.
3170: 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
3180: 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 TE_POW2_MEMORY_S
3190: 49 5a 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 IZE) && !defined
31a0: 28 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 (SQLITE_MALLOC_S
31b0: 4f 46 54 5f 4c 49 4d 49 54 29 0a 23 20 64 65 66 OFT_LIMIT).# def
31c0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f ine SQLITE_MALLO
31d0: 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 31 30 32 C_SOFT_LIMIT 102
31e0: 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4.#endif../*.**
31f0: 57 65 20 6e 65 65 64 20 74 6f 20 64 65 66 69 6e We need to defin
3200: 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 e _XOPEN_SOURCE
3210: 61 73 20 66 6f 6c 6c 6f 77 73 20 69 6e 20 6f 72 as follows in or
3220: 64 65 72 20 74 6f 20 65 6e 61 62 6c 65 0a 2a 2a der to enable.**
3230: 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
3240: 65 73 20 6f 6e 20 6d 6f 73 74 20 55 6e 69 78 20 es on most Unix
3250: 73 79 73 74 65 6d 73 2e 20 20 42 75 74 20 4d 61 systems. But Ma
3260: 63 20 4f 53 20 58 20 69 73 20 64 69 66 66 65 72 c OS X is differ
3270: 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 5f 58 4f 50 ent..** The _XOP
3280: 45 4e 5f 53 4f 55 52 43 45 20 64 65 66 69 6e 65 EN_SOURCE define
3290: 20 63 61 75 73 65 73 20 70 72 6f 62 6c 65 6d 73 causes problems
32a0: 20 66 6f 72 20 4d 61 63 20 4f 53 20 58 20 77 65 for Mac OS X we
32b0: 20 61 72 65 20 74 6f 6c 64 2c 0a 2a 2a 20 73 6f are told,.** so
32c0: 20 69 74 20 69 73 20 6f 6d 69 74 74 65 64 20 74 it is omitted t
32d0: 68 65 72 65 2e 20 20 53 65 65 20 74 69 63 6b 65 here. See ticke
32e0: 74 20 23 32 36 37 33 2e 0a 2a 2a 0a 2a 2a 20 4c t #2673..**.** L
32f0: 61 74 65 72 20 77 65 20 6c 65 61 72 6e 20 74 68 ater we learn th
3300: 61 74 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 at _XOPEN_SOURCE
3310: 20 69 73 20 70 6f 6f 72 6c 79 20 6f 72 20 69 6e is poorly or in
3320: 63 6f 72 72 65 63 74 6c 79 0a 2a 2a 20 69 6d 70 correctly.** imp
3330: 6c 65 6d 65 6e 74 65 64 20 6f 6e 20 73 6f 6d 65 lemented on some
3340: 20 73 79 73 74 65 6d 73 2e 20 20 53 6f 20 77 65 systems. So we
3350: 20 61 76 6f 69 64 20 64 65 66 69 6e 69 6e 67 20 avoid defining
3360: 69 74 20 61 74 20 61 6c 6c 0a 2a 2a 20 69 66 20 it at all.** if
3370: 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 64 65 it is already de
3380: 66 69 6e 65 64 20 6f 72 20 69 66 20 69 74 20 69 fined or if it i
3390: 73 20 75 6e 6e 65 65 64 65 64 20 62 65 63 61 75 s unneeded becau
33a0: 73 65 20 77 65 20 61 72 65 0a 2a 2a 20 6e 6f 74 se we are.** not
33b0: 20 64 6f 69 6e 67 20 61 20 74 68 72 65 61 64 73 doing a threads
33c0: 61 66 65 20 62 75 69 6c 64 2e 20 20 54 69 63 6b afe build. Tick
33d0: 65 74 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 et #2681..**.**
33e0: 53 65 65 20 61 6c 73 6f 20 74 69 63 6b 65 74 20 See also ticket
33f0: 23 32 37 34 31 2e 0a 2a 2f 0a 23 69 66 20 21 64 #2741..*/.#if !d
3400: 65 66 69 6e 65 64 28 5f 58 4f 50 45 4e 5f 53 4f efined(_XOPEN_SO
3410: 55 52 43 45 29 20 26 26 20 21 64 65 66 69 6e 65 URCE) && !define
3420: 64 28 5f 5f 44 41 52 57 49 4e 5f 5f 29 20 26 26 d(__DARWIN__) &&
3430: 20 21 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c !defined(__APPL
3440: 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 54 E__) && SQLITE_T
3450: 48 52 45 41 44 53 41 46 45 0a 23 20 20 64 65 66 HREADSAFE.# def
3460: 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 ine _XOPEN_SOURC
3470: 45 20 35 30 30 20 20 2f 2a 20 4e 65 65 64 65 64 E 500 /* Needed
3480: 20 74 6f 20 65 6e 61 62 6c 65 20 70 74 68 72 65 to enable pthre
3490: 61 64 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 ad recursive mut
34a0: 65 78 65 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a exes */.#endif..
34b0: 2f 2a 0a 2a 2a 20 54 68 65 20 54 43 4c 20 68 65 /*.** The TCL he
34c0: 61 64 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 6e aders are only n
34d0: 65 65 64 65 64 20 77 68 65 6e 20 63 6f 6d 70 69 eeded when compi
34e0: 6c 69 6e 67 20 74 68 65 20 54 43 4c 20 62 69 6e ling the TCL bin
34f0: 64 69 6e 67 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 dings..*/.#if de
3500: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 43 4c fined(SQLITE_TCL
3510: 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 54 43 4c ) || defined(TCL
3520: 53 48 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c 74 SH).# include <t
3530: 63 6c 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a cl.h>.#endif../*
3540: 0a 2a 2a 20 4d 61 6e 79 20 70 65 6f 70 6c 65 20 .** Many people
3550: 61 72 65 20 66 61 69 6c 69 6e 67 20 74 6f 20 73 are failing to s
3560: 65 74 20 2d 44 4e 44 45 42 55 47 3d 31 20 77 68 et -DNDEBUG=1 wh
3570: 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 53 51 4c en compiling SQL
3580: 69 74 65 2e 0a 2a 2a 20 53 65 74 74 69 6e 67 20 ite..** Setting
3590: 4e 44 45 42 55 47 20 6d 61 6b 65 73 20 74 68 65 NDEBUG makes the
35a0: 20 63 6f 64 65 20 73 6d 61 6c 6c 65 72 20 61 6e code smaller an
35b0: 64 20 72 75 6e 20 66 61 73 74 65 72 2e 20 20 53 d run faster. S
35c0: 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a o the following.
35d0: 2a 2a 20 6c 69 6e 65 73 20 61 72 65 20 61 64 64 ** lines are add
35e0: 65 64 20 74 6f 20 61 75 74 6f 6d 61 74 69 63 61 ed to automatica
35f0: 6c 6c 79 20 73 65 74 20 4e 44 45 42 55 47 20 75 lly set NDEBUG u
3600: 6e 6c 65 73 73 20 74 68 65 20 2d 44 53 51 4c 49 nless the -DSQLI
3610: 54 45 5f 44 45 42 55 47 3d 31 0a 2a 2a 20 6f 70 TE_DEBUG=1.** op
3620: 74 69 6f 6e 20 69 73 20 73 65 74 2e 20 20 54 68 tion is set. Th
3630: 75 73 20 4e 44 45 42 55 47 20 62 65 63 6f 6d 65 us NDEBUG become
3640: 73 20 61 6e 20 6f 70 74 2d 69 6e 20 72 61 74 68 s an opt-in rath
3650: 65 72 20 74 68 61 6e 20 61 6e 20 6f 70 74 2d 6f er than an opt-o
3660: 75 74 0a 2a 2a 20 66 65 61 74 75 72 65 2e 0a 2a ut.** feature..*
3670: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e /.#if !defined(N
3680: 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e DEBUG) && !defin
3690: 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
36a0: 20 0a 23 20 64 65 66 69 6e 65 20 4e 44 45 42 55 .# define NDEBU
36b0: 47 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a G 1.#endif../*.*
36c0: 2a 20 54 68 65 20 74 65 73 74 63 61 73 65 28 29 * The testcase()
36d0: 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74 macro is used t
36e0: 6f 20 61 69 64 20 69 6e 20 63 6f 76 65 72 61 67 o aid in coverag
36f0: 65 20 74 65 73 74 69 6e 67 2e 20 20 57 68 65 6e e testing. When
3700: 20 0a 2a 2a 20 64 6f 69 6e 67 20 63 6f 76 65 72 .** doing cover
3710: 61 67 65 20 74 65 73 74 69 6e 67 2c 20 74 68 65 age testing, the
3720: 20 63 6f 6e 64 69 74 69 6f 6e 20 69 6e 73 69 64 condition insid
3730: 65 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 e the argument t
3740: 6f 0a 2a 2a 20 74 65 73 74 63 61 73 65 28 29 20 o.** testcase()
3750: 6d 75 73 74 20 62 65 20 65 76 61 6c 75 61 74 65 must be evaluate
3760: 64 20 62 6f 74 68 20 74 72 75 65 20 61 6e 64 20 d both true and
3770: 66 61 6c 73 65 20 69 6e 20 6f 72 64 65 72 20 74 false in order t
3780: 6f 0a 2a 2a 20 67 65 74 20 66 75 6c 6c 20 62 72 o.** get full br
3790: 61 6e 63 68 20 63 6f 76 65 72 61 67 65 2e 20 20 anch coverage.
37a0: 54 68 65 20 74 65 73 74 63 61 73 65 28 29 20 6d The testcase() m
37b0: 61 63 72 6f 20 69 73 20 69 6e 73 65 72 74 65 64 acro is inserted
37c0: 0a 2a 2a 20 74 6f 20 68 65 6c 70 20 65 6e 73 75 .** to help ensu
37d0: 72 65 20 61 64 65 71 75 61 74 65 20 74 65 73 74 re adequate test
37e0: 20 63 6f 76 65 72 61 67 65 20 69 6e 20 70 6c 61 coverage in pla
37f0: 63 65 73 20 77 68 65 72 65 20 73 69 6d 70 6c 65 ces where simple
3800: 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 2f 64 65 .** condition/de
3810: 63 69 73 69 6f 6e 20 63 6f 76 65 72 61 67 65 20 cision coverage
3820: 69 73 20 69 6e 61 64 65 71 75 61 74 65 2e 20 20 is inadequate.
3830: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 65 73 For example, tes
3840: 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e 20 62 tcase().** can b
3850: 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 e used to make s
3860: 75 72 65 20 62 6f 75 6e 64 61 72 79 20 76 61 6c ure boundary val
3870: 75 65 73 20 61 72 65 20 74 65 73 74 65 64 2e 20 ues are tested.
3880: 20 46 6f 72 0a 2a 2a 20 62 69 74 6d 61 73 6b 20 For.** bitmask
3890: 74 65 73 74 73 2c 20 74 65 73 74 63 61 73 65 28 tests, testcase(
38a0: 29 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f ) can be used to
38b0: 20 6d 61 6b 65 20 73 75 72 65 20 65 61 63 68 20 make sure each
38c0: 62 69 74 0a 2a 2a 20 69 73 20 73 69 67 6e 69 66 bit.** is signif
38d0: 69 63 61 6e 74 20 61 6e 64 20 75 73 65 64 20 61 icant and used a
38e0: 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20 4f t least once. O
38f0: 6e 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 n switch stateme
3900: 6e 74 73 0a 2a 2a 20 77 68 65 72 65 20 6d 75 6c nts.** where mul
3910: 74 69 70 6c 65 20 63 61 73 65 73 20 67 6f 20 74 tiple cases go t
3920: 6f 20 74 68 65 20 73 61 6d 65 20 62 6c 6f 63 6b o the same block
3930: 20 6f 66 20 63 6f 64 65 2c 20 74 65 73 74 63 61 of code, testca
3940: 73 65 28 29 0a 2a 2a 20 63 61 6e 20 69 6e 73 75 se().** can insu
3950: 72 65 20 74 68 61 74 20 61 6c 6c 20 63 61 73 65 re that all case
3960: 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 2e s are evaluated.
3970: 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 .**.*/.#ifdef SQ
3980: 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 LITE_COVERAGE_TE
3990: 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ST.SQLITE_PRIVAT
39a0: 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
39b0: 43 6f 76 65 72 61 67 65 28 69 6e 74 29 3b 0a 23 Coverage(int);.#
39c0: 20 64 65 66 69 6e 65 20 74 65 73 74 63 61 73 65 define testcase
39d0: 28 58 29 20 20 69 66 28 20 58 20 29 7b 20 73 71 (X) if( X ){ sq
39e0: 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 5f 5f lite3Coverage(__
39f0: 4c 49 4e 45 5f 5f 29 3b 20 7d 0a 23 65 6c 73 65 LINE__); }.#else
3a00: 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74 63 61 .# define testca
3a10: 73 65 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a se(X).#endif../*
3a20: 0a 2a 2a 20 54 68 65 20 54 45 53 54 4f 4e 4c 59 .** The TESTONLY
3a30: 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74 macro is used t
3a40: 6f 20 65 6e 63 6c 6f 73 65 20 76 61 72 69 61 62 o enclose variab
3a50: 6c 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 le declarations
3a60: 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 62 69 74 73 or.** other bits
3a70: 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 61 72 of code that ar
3a80: 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70 e needed to supp
3a90: 6f 72 74 20 74 68 65 20 61 72 67 75 6d 65 6e 74 ort the argument
3aa0: 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 65 73 74 s.** within test
3ab0: 63 61 73 65 28 29 20 61 6e 64 20 61 73 73 65 72 case() and asser
3ac0: 74 28 29 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 t() macros..*/.#
3ad0: 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 if !defined(NDEB
3ae0: 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 UG) || defined(S
3af0: 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 QLITE_COVERAGE_T
3b00: 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 54 45 EST).# define TE
3b10: 53 54 4f 4e 4c 59 28 58 29 20 20 58 0a 23 65 6c STONLY(X) X.#el
3b20: 73 65 0a 23 20 64 65 66 69 6e 65 20 54 45 53 54 se.# define TEST
3b30: 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66 0a 0a ONLY(X).#endif..
3b40: 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 41 59 53 /*.** The ALWAYS
3b50: 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 63 72 6f and NEVER macro
3b60: 73 20 73 75 72 72 6f 75 6e 64 20 62 6f 6f 6c 65 s surround boole
3b70: 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77 an expressions w
3b80: 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 69 6e 74 hich .** are int
3b90: 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 79 73 20 ended to always
3ba0: 62 65 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 be true or false
3bb0: 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 , respectively.
3bc0: 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 65 73 73 Such.** express
3bd0: 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 20 6f 6d ions could be om
3be0: 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 63 itted from the c
3bf0: 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c 79 2e 20 ode completely.
3c00: 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 61 72 65 But they.** are
3c10: 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 61 20 66 included in a f
3c20: 65 77 20 63 61 73 65 73 20 69 6e 20 6f 72 64 65 ew cases in orde
3c30: 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 74 68 65 r to enhance the
3c40: 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a 2a 20 6f resilience.** o
3c50: 66 20 53 51 4c 69 74 65 20 74 6f 20 75 6e 65 78 f SQLite to unex
3c60: 70 65 63 74 65 64 20 62 65 68 61 76 69 6f 72 20 pected behavior
3c70: 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 63 6f - to make the co
3c80: 64 65 20 22 73 65 6c 66 2d 68 65 61 6c 69 6e 67 de "self-healing
3c90: 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 69 6c 65 ".** or "ductile
3ca0: 22 20 72 61 74 68 65 72 20 74 68 61 6e 20 62 65 " rather than be
3cb0: 69 6e 67 20 22 62 72 69 74 74 6c 65 22 20 61 6e ing "brittle" an
3cc0: 64 20 63 72 61 73 68 69 6e 67 20 61 74 20 74 68 d crashing at th
3cd0: 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 6e 74 20 e first.** hint
3ce0: 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 62 65 68 of unplanned beh
3cf0: 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 avior..**.** In
3d00: 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 41 4c 57 other words, ALW
3d10: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 61 72 AYS and NEVER ar
3d20: 65 20 61 64 64 65 64 20 66 6f 72 20 64 65 66 65 e added for defe
3d30: 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a nsive code..**.*
3d40: 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 63 6f 76 * When doing cov
3d50: 65 72 61 67 65 20 74 65 73 74 69 6e 67 20 41 4c erage testing AL
3d60: 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 61 WAYS and NEVER a
3d70: 72 65 20 68 61 72 64 2d 63 6f 64 65 64 20 74 6f re hard-coded to
3d80: 0a 2a 2a 20 62 65 20 74 72 75 65 20 61 6e 64 20 .** be true and
3d90: 66 61 6c 73 65 20 73 6f 20 74 68 61 74 20 74 68 false so that th
3da0: 65 20 75 6e 72 65 61 63 68 61 62 6c 65 20 63 6f e unreachable co
3db0: 64 65 20 74 68 65 6e 20 73 70 65 63 69 66 79 20 de then specify
3dc0: 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 65 20 63 will.** not be c
3dd0: 6f 75 6e 74 65 64 20 61 73 20 75 6e 74 65 73 74 ounted as untest
3de0: 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 20 ed code..*/.#if
3df0: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 defined(SQLITE_C
3e00: 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a 23 20 OVERAGE_TEST).#
3e10: 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29 define ALWAYS(X)
3e20: 20 20 20 20 20 20 28 31 29 0a 23 20 64 65 66 69 (1).# defi
3e30: 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20 ne NEVER(X)
3e40: 20 20 28 30 29 0a 23 65 6c 69 66 20 21 64 65 66 (0).#elif !def
3e50: 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 53 51 4c ined(NDEBUG).SQL
3e60: 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
3e70: 74 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 28 t sqlite3Assert(
3e80: 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 void);.# define
3e90: 41 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28 ALWAYS(X) (
3ea0: 28 58 29 3f 31 3a 73 71 6c 69 74 65 33 41 73 73 (X)?1:sqlite3Ass
3eb0: 65 72 74 28 29 29 0a 23 20 64 65 66 69 6e 65 20 ert()).# define
3ec0: 4e 45 56 45 52 28 58 29 20 20 20 20 20 20 20 28 NEVER(X) (
3ed0: 28 58 29 3f 73 71 6c 69 74 65 33 41 73 73 65 72 (X)?sqlite3Asser
3ee0: 74 28 29 3a 30 29 0a 23 65 6c 73 65 0a 23 20 64 t():0).#else.# d
3ef0: 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29 20 efine ALWAYS(X)
3f00: 20 20 20 20 20 28 58 29 0a 23 20 64 65 66 69 6e (X).# defin
3f10: 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20 20 e NEVER(X)
3f20: 20 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a (X).#endif../*.
3f30: 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 75 6e 6c ** The macro unl
3f40: 69 6b 65 6c 79 28 29 20 69 73 20 61 20 68 69 6e ikely() is a hin
3f50: 74 20 74 68 61 74 20 73 75 72 72 6f 75 6e 64 73 t that surrounds
3f60: 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 65 78 a boolean.** ex
3f70: 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 pression that is
3f80: 20 75 73 75 61 6c 6c 79 20 66 61 6c 73 65 2e 20 usually false.
3f90: 20 4d 61 63 72 6f 20 6c 69 6b 65 6c 79 28 29 20 Macro likely()
3fa0: 73 75 72 72 6f 75 6e 64 73 0a 2a 2a 20 61 20 62 surrounds.** a b
3fb0: 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f oolean expressio
3fc0: 6e 20 74 68 61 74 20 69 73 20 75 73 75 61 6c 6c n that is usuall
3fd0: 79 20 74 72 75 65 2e 20 20 47 43 43 20 69 73 20 y true. GCC is
3fe0: 61 62 6c 65 20 74 6f 0a 2a 2a 20 75 73 65 20 74 able to.** use t
3ff0: 68 65 73 65 20 68 69 6e 74 73 20 74 6f 20 67 65 hese hints to ge
4000: 6e 65 72 61 74 65 20 62 65 74 74 65 72 20 63 6f nerate better co
4010: 64 65 2c 20 73 6f 6d 65 74 69 6d 65 73 2e 0a 2a de, sometimes..*
4020: 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f /.#if defined(__
4030: 47 4e 55 43 5f 5f 29 20 26 26 20 30 0a 23 20 64 GNUC__) && 0.# d
4040: 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28 58 29 20 efine likely(X)
4050: 20 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78 70 __builtin_exp
4060: 65 63 74 28 28 58 29 2c 31 29 0a 23 20 64 65 66 ect((X),1).# def
4070: 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58 29 20 ine unlikely(X)
4080: 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78 70 65 63 __builtin_expec
4090: 74 28 28 58 29 2c 30 29 0a 23 65 6c 73 65 0a 23 t((X),0).#else.#
40a0: 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28 58 define likely(X
40b0: 29 20 20 20 20 21 21 28 58 29 0a 23 20 64 65 66 ) !!(X).# def
40c0: 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58 29 20 ine unlikely(X)
40d0: 20 21 21 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f !!(X).#endif../
40e0: 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
40f0: 65 20 6e 65 65 64 20 61 20 73 6d 61 6c 6c 20 61 e need a small a
4100: 6d 6f 75 6e 74 20 6f 66 20 63 6f 64 65 20 73 75 mount of code su
4110: 63 68 20 61 73 20 61 20 76 61 72 69 61 62 6c 65 ch as a variable
4120: 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a initialization.
4130: 2a 2a 20 74 6f 20 73 65 74 75 70 20 66 6f 72 20 ** to setup for
4140: 61 20 6c 61 74 65 72 20 61 73 73 65 72 74 28 29 a later assert()
4150: 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 57 65 20 statement. We
4160: 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 69 73 do not want this
4170: 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 61 70 70 65 code to.** appe
4180: 61 72 20 77 68 65 6e 20 61 73 73 65 72 74 28 29 ar when assert()
4190: 20 69 73 20 64 69 73 61 62 6c 65 64 2e 20 20 54 is disabled. T
41a0: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 he following mac
41b0: 72 6f 20 69 73 20 74 68 65 72 65 66 6f 72 65 0a ro is therefore.
41c0: 2a 2a 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 61 ** used to conta
41d0: 69 6e 20 74 68 61 74 20 73 65 74 75 70 20 63 6f in that setup co
41e0: 64 65 2e 20 20 54 68 65 20 22 56 56 41 22 20 61 de. The "VVA" a
41f0: 63 72 6f 6e 79 6d 20 73 74 61 6e 64 73 20 66 6f cronym stands fo
4200: 72 0a 2a 2a 20 22 56 65 72 69 66 69 63 61 74 69 r.** "Verificati
4210: 6f 6e 2c 20 56 61 6c 69 64 61 74 69 6f 6e 2c 20 on, Validation,
4220: 61 6e 64 20 41 63 63 72 65 64 69 74 61 74 69 6f and Accreditatio
4230: 6e 22 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f n". In other wo
4240: 72 64 73 2c 20 74 68 65 0a 2a 2a 20 63 6f 64 65 rds, the.** code
4250: 20 77 69 74 68 69 6e 20 56 56 41 5f 4f 4e 4c 59 within VVA_ONLY
4260: 28 29 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 75 6e () will only run
4270: 20 64 75 72 69 6e 67 20 76 65 72 69 66 69 63 61 during verifica
4280: 74 69 6f 6e 20 70 72 6f 63 65 73 73 65 73 2e 0a tion processes..
4290: 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 */.#ifndef NDEBU
42a0: 47 0a 23 20 64 65 66 69 6e 65 20 56 56 41 5f 4f G.# define VVA_O
42b0: 4e 4c 59 28 58 29 20 20 58 0a 23 65 6c 73 65 0a NLY(X) X.#else.
42c0: 23 20 64 65 66 69 6e 65 20 56 56 41 5f 4f 4e 4c # define VVA_ONL
42d0: 59 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a Y(X).#endif../**
42e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
42f0: 6c 75 64 65 20 73 71 6c 69 74 65 33 2e 68 20 69 lude sqlite3.h i
4300: 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
4310: 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
4320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
4330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
4340: 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33 2e in file sqlite3.
4350: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4360: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4370: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
4380: 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
4390: 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
43a0: 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
43b0: 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
43c0: 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
43d0: 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
43e0: 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
43f0: 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
4400: 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
4410: 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
4420: 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
4430: 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
4440: 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
4450: 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
4460: 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
4470: 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
4480: 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
4490: 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
44a0: 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
44b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44f0: 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 ***.** This head
4500: 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 er file defines
4510: 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 the interface th
4520: 61 74 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 at the SQLite li
4530: 62 72 61 72 79 0a 2a 2a 20 70 72 65 73 65 6e 74 brary.** present
4540: 73 20 74 6f 20 63 6c 69 65 6e 74 20 70 72 6f 67 s to client prog
4550: 72 61 6d 73 2e 20 20 49 66 20 61 20 43 2d 66 75 rams. If a C-fu
4560: 6e 63 74 69 6f 6e 2c 20 73 74 72 75 63 74 75 72 nction, structur
4570: 65 2c 20 64 61 74 61 74 79 70 65 2c 0a 2a 2a 20 e, datatype,.**
4580: 6f 72 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 or constant defi
4590: 6e 69 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 nition does not
45a0: 61 70 70 65 61 72 20 69 6e 20 74 68 69 73 20 66 appear in this f
45b0: 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a ile, then it is.
45c0: 2a 2a 20 6e 6f 74 20 61 20 70 75 62 6c 69 73 68 ** not a publish
45d0: 65 64 20 41 50 49 20 6f 66 20 53 51 4c 69 74 65 ed API of SQLite
45e0: 2c 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20 , is subject to
45f0: 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 0a 2a change without.*
4600: 2a 20 6e 6f 74 69 63 65 2c 20 61 6e 64 20 73 68 * notice, and sh
4610: 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 66 65 ould not be refe
4620: 72 65 6e 63 65 64 20 62 79 20 70 72 6f 67 72 61 renced by progra
4630: 6d 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 ms that use SQLi
4640: 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f te..**.** Some o
4650: 66 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e f the definition
4660: 73 20 74 68 61 74 20 61 72 65 20 69 6e 20 74 68 s that are in th
4670: 69 73 20 66 69 6c 65 20 61 72 65 20 6d 61 72 6b is file are mark
4680: 65 64 20 61 73 0a 2a 2a 20 22 65 78 70 65 72 69 ed as.** "experi
4690: 6d 65 6e 74 61 6c 22 2e 20 20 45 78 70 65 72 69 mental". Experi
46a0: 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65 mental interface
46b0: 73 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 6e s are normally n
46c0: 65 77 0a 2a 2a 20 66 65 61 74 75 72 65 73 20 72 ew.** features r
46d0: 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 74 6f ecently added to
46e0: 20 53 51 4c 69 74 65 2e 20 20 57 65 20 64 6f 20 SQLite. We do
46f0: 6e 6f 74 20 61 6e 74 69 63 69 70 61 74 65 20 63 not anticipate c
4700: 68 61 6e 67 65 73 0a 2a 2a 20 74 6f 20 65 78 70 hanges.** to exp
4710: 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 erimental interf
4720: 61 63 65 73 20 62 75 74 20 72 65 73 65 72 76 65 aces but reserve
4730: 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 63 to make minor c
4740: 68 61 6e 67 65 73 20 69 66 0a 2a 2a 20 65 78 70 hanges if.** exp
4750: 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 75 73 65 erience from use
4760: 20 22 69 6e 20 74 68 65 20 77 69 6c 64 22 20 73 "in the wild" s
4770: 75 67 67 65 73 74 20 73 75 63 68 20 63 68 61 6e uggest such chan
4780: 67 65 73 20 61 72 65 20 70 72 75 64 65 6e 74 2e ges are prudent.
4790: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69 63 .**.** The offic
47a0: 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20 41 ial C-language A
47b0: 50 49 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e PI documentation
47c0: 20 66 6f 72 20 53 51 4c 69 74 65 20 69 73 20 64 for SQLite is d
47d0: 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 63 erived.** from c
47e0: 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 69 73 20 omments in this
47f0: 66 69 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65 file. This file
4800: 20 69 73 20 74 68 65 20 61 75 74 68 6f 72 69 74 is the authorit
4810: 61 74 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a 20 ative source.**
4820: 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65 20 69 6e on how SQLite in
4830: 74 65 72 66 61 63 65 73 20 61 72 65 20 73 75 70 terfaces are sup
4840: 70 6f 73 65 20 74 6f 20 6f 70 65 72 61 74 65 2e pose to operate.
4850: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 .**.** The name
4860: 6f 66 20 74 68 69 73 20 66 69 6c 65 20 75 6e 64 of this file und
4870: 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e er configuration
4880: 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69 73 20 22 management is "
4890: 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a sqlite.h.in"..**
48a0: 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d 61 The makefile ma
48b0: 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20 63 kes some minor c
48c0: 68 61 6e 67 65 73 20 74 6f 20 74 68 69 73 20 66 hanges to this f
48d0: 69 6c 65 20 28 73 75 63 68 20 61 73 20 69 6e 73 ile (such as ins
48e0: 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 65 erting.** the ve
48f0: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61 6e rsion number) an
4900: 64 20 63 68 61 6e 67 65 73 20 69 74 73 20 6e 61 d changes its na
4910: 6d 65 20 74 6f 20 22 73 71 6c 69 74 65 33 2e 68 me to "sqlite3.h
4920: 22 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 " as.** part of
4930: 74 68 65 20 62 75 69 6c 64 20 70 72 6f 63 65 73 the build proces
4940: 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 s..**.** @(#) $I
4950: 64 3a 20 73 71 6c 69 74 65 2e 68 2e 69 6e 2c 76 d: sqlite.h.in,v
4960: 20 31 2e 34 32 31 20 32 30 30 38 2f 31 32 2f 33 1.421 2008/12/3
4970: 30 20 30 36 3a 32 34 3a 35 38 20 64 61 6e 69 65 0 06:24:58 danie
4980: 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
4990: 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 #ifndef _SQLITE3
49a0: 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c _H_.#define _SQL
49b0: 49 54 45 33 5f 48 5f 0a 23 69 6e 63 6c 75 64 65 ITE3_H_.#include
49c0: 20 3c 73 74 64 61 72 67 2e 68 3e 20 20 20 20 20 <stdarg.h>
49d0: 2f 2a 20 4e 65 65 64 65 64 20 66 6f 72 20 74 68 /* Needed for th
49e0: 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 e definition of
49f0: 76 61 5f 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a va_list */../*.*
4a00: 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 63 * Make sure we c
4a10: 61 6e 20 63 61 6c 6c 20 74 68 69 73 20 73 74 75 an call this stu
4a20: 66 66 20 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a ff from C++..*/.
4a30: 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 #if 0.extern "C"
4a40: 20 7b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a {.#endif.../*.*
4a50: 2a 20 41 64 64 20 74 68 65 20 61 62 69 6c 69 74 * Add the abilit
4a60: 79 20 74 6f 20 6f 76 65 72 72 69 64 65 20 27 65 y to override 'e
4a70: 78 74 65 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64 65 xtern'.*/.#ifnde
4a80: 66 20 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 0a f SQLITE_EXTERN.
4a90: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
4aa0: 45 58 54 45 52 4e 20 65 78 74 65 72 6e 0a 23 65 EXTERN extern.#e
4ab0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 ndif../*.** Thes
4ac0: 65 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 61 e no-op macros a
4ad0: 72 65 20 75 73 65 64 20 69 6e 20 66 72 6f 6e 74 re used in front
4ae0: 20 6f 66 20 69 6e 74 65 72 66 61 63 65 73 20 74 of interfaces t
4af0: 6f 20 6d 61 72 6b 20 74 68 6f 73 65 0a 2a 2a 20 o mark those.**
4b00: 69 6e 74 65 72 66 61 63 65 73 20 61 73 20 65 69 interfaces as ei
4b10: 74 68 65 72 20 64 65 70 72 65 63 61 74 65 64 20 ther deprecated
4b20: 6f 72 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e or experimental.
4b30: 20 20 4e 65 77 20 61 70 70 6c 69 63 61 74 69 6f New applicatio
4b40: 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 ns.** should not
4b50: 20 75 73 65 20 64 65 70 72 65 63 61 74 65 64 20 use deprecated
4b60: 69 6e 74 72 66 61 63 65 73 20 2d 20 74 68 65 79 intrfaces - they
4b70: 20 61 72 65 20 73 75 70 70 6f 72 74 20 66 6f 72 are support for
4b80: 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f backwards.** co
4b90: 6d 70 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 mpatibility only
4ba0: 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 77 . Application w
4bb0: 72 69 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 riters should be
4bc0: 20 61 77 61 72 65 20 74 68 61 74 0a 2a 2a 20 65 aware that.** e
4bd0: 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 xperimental inte
4be0: 72 66 61 63 65 73 20 61 72 65 20 73 75 62 6a 65 rfaces are subje
4bf0: 63 74 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 ct to change in
4c00: 70 6f 69 6e 74 20 72 65 6c 65 61 73 65 73 2e 0a point releases..
4c10: 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 **.** These macr
4c20: 6f 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c os used to resol
4c30: 76 65 20 74 6f 20 76 61 72 69 6f 75 73 20 6b 69 ve to various ki
4c40: 6e 64 73 20 6f 66 20 63 6f 6d 70 69 6c 65 72 20 nds of compiler
4c50: 6d 61 67 69 63 20 74 68 61 74 0a 2a 2a 20 77 6f magic that.** wo
4c60: 75 6c 64 20 67 65 6e 65 72 61 74 65 20 77 61 72 uld generate war
4c70: 6e 69 6e 67 20 6d 65 73 73 61 67 65 73 20 77 68 ning messages wh
4c80: 65 6e 20 74 68 65 79 20 77 65 72 65 20 75 73 65 en they were use
4c90: 64 2e 20 20 42 75 74 20 74 68 61 74 0a 2a 2a 20 d. But that.**
4ca0: 63 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 65 compiler magic e
4cb0: 6e 64 65 64 20 75 70 20 67 65 6e 65 72 61 74 69 nded up generati
4cc0: 6e 67 20 73 75 63 68 20 61 20 66 6c 75 72 72 79 ng such a flurry
4cd0: 20 6f 66 20 62 75 67 20 72 65 70 6f 72 74 73 0a of bug reports.
4ce0: 2a 2a 20 74 68 61 74 20 77 65 20 68 61 76 65 20 ** that we have
4cf0: 74 61 6b 65 6e 20 69 74 20 61 6c 6c 20 6f 75 74 taken it all out
4d00: 20 61 6e 64 20 67 6f 6e 65 20 62 61 63 6b 20 74 and gone back t
4d10: 6f 20 75 73 69 6e 67 20 73 69 6d 70 6c 65 0a 2a o using simple.*
4d20: 2a 20 6e 6f 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a * noop macros..*
4d30: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
4d40: 5f 44 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 _DEPRECATED.#def
4d50: 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 50 45 52 ine SQLITE_EXPER
4d60: 49 4d 45 4e 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 IMENTAL../*.** E
4d70: 6e 73 75 72 65 20 74 68 65 73 65 20 73 79 6d 62 nsure these symb
4d80: 6f 6c 73 20 77 65 72 65 20 6e 6f 74 20 64 65 66 ols were not def
4d90: 69 6e 65 64 20 62 79 20 73 6f 6d 65 20 70 72 65 ined by some pre
4da0: 76 69 6f 75 73 20 68 65 61 64 65 72 20 66 69 6c vious header fil
4db0: 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
4dc0: 49 54 45 5f 56 45 52 53 49 4f 4e 0a 23 20 75 6e ITE_VERSION.# un
4dd0: 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 def SQLITE_VERSI
4de0: 4f 4e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 ON.#endif.#ifdef
4df0: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f SQLITE_VERSION_
4e00: 4e 55 4d 42 45 52 0a 23 20 75 6e 64 65 66 20 53 NUMBER.# undef S
4e10: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 QLITE_VERSION_NU
4e20: 4d 42 45 52 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a MBER.#endif../*.
4e30: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d ** CAPI3REF: Com
4e40: 70 69 6c 65 2d 54 69 6d 65 20 4c 69 62 72 61 72 pile-Time Librar
4e50: 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 y Version Number
4e60: 73 20 7b 48 31 30 30 31 30 7d 20 3c 53 36 30 31 s {H10010} <S601
4e70: 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 00>.**.** The SQ
4e80: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 20 61 6e 64 LITE_VERSION and
4e90: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f SQLITE_VERSION_
4ea0: 4e 55 4d 42 45 52 20 23 64 65 66 69 6e 65 73 20 NUMBER #defines
4eb0: 69 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 in.** the sqlite
4ec0: 33 2e 68 20 66 69 6c 65 20 73 70 65 63 69 66 79 3.h file specify
4ed0: 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 the version of
4ee0: 53 51 4c 69 74 65 20 77 69 74 68 20 77 68 69 63 SQLite with whic
4ef0: 68 0a 2a 2a 20 74 68 61 74 20 68 65 61 64 65 72 h.** that header
4f00: 20 66 69 6c 65 20 69 73 20 61 73 73 6f 63 69 61 file is associa
4f10: 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 ted..**.** The "
4f20: 76 65 72 73 69 6f 6e 22 20 6f 66 20 53 51 4c 69 version" of SQLi
4f30: 74 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f te is a string o
4f40: 66 20 74 68 65 20 66 6f 72 6d 20 22 58 2e 59 2e f the form "X.Y.
4f50: 5a 22 2e 0a 2a 2a 20 54 68 65 20 70 68 72 61 73 Z"..** The phras
4f60: 65 20 22 61 6c 70 68 61 22 20 6f 72 20 22 62 65 e "alpha" or "be
4f70: 74 61 22 20 6d 69 67 68 74 20 62 65 20 61 70 70 ta" might be app
4f80: 65 6e 64 65 64 20 61 66 74 65 72 20 74 68 65 20 ended after the
4f90: 5a 2e 0a 2a 2a 20 54 68 65 20 58 20 76 61 6c 75 Z..** The X valu
4fa0: 65 20 69 73 20 6d 61 6a 6f 72 20 76 65 72 73 69 e is major versi
4fb0: 6f 6e 20 6e 75 6d 62 65 72 20 61 6c 77 61 79 73 on number always
4fc0: 20 33 20 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 3 in SQLite3..*
4fd0: 2a 20 54 68 65 20 58 20 76 61 6c 75 65 20 6f 6e * The X value on
4fe0: 6c 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e 20 ly changes when
4ff0: 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 backwards compat
5000: 69 62 69 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72 ibility is.** br
5010: 6f 6b 65 6e 20 61 6e 64 20 77 65 20 69 6e 74 65 oken and we inte
5020: 6e 64 20 74 6f 20 6e 65 76 65 72 20 62 72 65 61 nd to never brea
5030: 6b 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 k backwards comp
5040: 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 atibility..** Th
5050: 65 20 59 20 76 61 6c 75 65 20 69 73 20 74 68 65 e Y value is the
5060: 20 6d 69 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e minor version n
5070: 75 6d 62 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63 umber and only c
5080: 68 61 6e 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74 hanges when.** t
5090: 68 65 72 65 20 61 72 65 20 6d 61 6a 6f 72 20 66 here are major f
50a0: 65 61 74 75 72 65 20 65 6e 68 61 6e 63 65 6d 65 eature enhanceme
50b0: 6e 74 73 20 74 68 61 74 20 61 72 65 20 66 6f 72 nts that are for
50c0: 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 wards compatible
50d0: 0a 2a 2a 20 62 75 74 20 6e 6f 74 20 62 61 63 6b .** but not back
50e0: 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 wards compatible
50f0: 2e 0a 2a 2a 20 54 68 65 20 5a 20 76 61 6c 75 65 ..** The Z value
5100: 20 69 73 20 74 68 65 20 72 65 6c 65 61 73 65 20 is the release
5110: 6e 75 6d 62 65 72 20 61 6e 64 20 69 73 20 69 6e number and is in
5120: 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a cremented with.*
5130: 2a 20 65 61 63 68 20 72 65 6c 65 61 73 65 20 62 * each release b
5140: 75 74 20 72 65 73 65 74 73 20 62 61 63 6b 20 74 ut resets back t
5150: 6f 20 30 20 77 68 65 6e 65 76 65 72 20 59 20 69 o 0 whenever Y i
5160: 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a s incremented..*
5170: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
5180: 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 sqlite3_libversi
5190: 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 on()] and [sqlit
51a0: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 e3_libversion_nu
51b0: 6d 62 65 72 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 mber()]..**.** I
51c0: 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
51d0: 20 7b 48 31 30 30 31 31 7d 20 54 68 65 20 53 51 {H10011} The SQ
51e0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 20 23 64 65 LITE_VERSION #de
51f0: 66 69 6e 65 20 69 6e 20 74 68 65 20 73 71 6c 69 fine in the sqli
5200: 74 65 33 2e 68 20 68 65 61 64 65 72 20 66 69 6c te3.h header fil
5210: 65 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 e shall.**
5220: 20 20 20 20 65 76 61 6c 75 61 74 65 20 74 6f 20 evaluate to
5230: 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c a string literal
5240: 20 74 68 61 74 20 69 73 20 74 68 65 20 53 51 4c that is the SQL
5250: 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 ite version.**
5260: 20 20 20 20 20 20 20 20 77 69 74 68 20 77 68 69 with whi
5270: 63 68 20 74 68 65 20 68 65 61 64 65 72 20 66 69 ch the header fi
5280: 6c 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 le is associated
5290: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 30 31 34 7d ..**.** {H10014}
52a0: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 The SQLITE_VERS
52b0: 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 64 65 66 69 ION_NUMBER #defi
52c0: 6e 65 20 73 68 61 6c 6c 20 72 65 73 6f 6c 76 65 ne shall resolve
52d0: 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a to an integer.*
52e0: 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68 20 * with
52f0: 74 68 65 20 76 61 6c 75 65 20 28 58 2a 31 30 30 the value (X*100
5300: 30 30 30 30 20 2b 20 59 2a 31 30 30 30 20 2b 20 0000 + Y*1000 +
5310: 5a 29 20 77 68 65 72 65 20 58 2c 20 59 2c 20 61 Z) where X, Y, a
5320: 6e 64 20 5a 0a 2a 2a 20 20 20 20 20 20 20 20 20 nd Z.**
5330: 20 61 72 65 20 74 68 65 20 6d 61 6a 6f 72 20 76 are the major v
5340: 65 72 73 69 6f 6e 2c 20 6d 69 6e 6f 72 20 76 65 ersion, minor ve
5350: 72 73 69 6f 6e 2c 20 61 6e 64 20 72 65 6c 65 61 rsion, and relea
5360: 73 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 23 64 se number..*/.#d
5370: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 45 52 efine SQLITE_VER
5380: 53 49 4f 4e 20 20 20 20 20 20 20 20 20 22 33 2e SION "3.
5390: 36 2e 31 30 22 0a 23 64 65 66 69 6e 65 20 53 51 6.10".#define SQ
53a0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d LITE_VERSION_NUM
53b0: 42 45 52 20 20 33 30 30 36 30 31 30 0a 0a 2f 2a BER 3006010../*
53c0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 .** CAPI3REF: Ru
53d0: 6e 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56 n-Time Library V
53e0: 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b ersion Numbers {
53f0: 48 31 30 30 32 30 7d 20 3c 53 36 30 31 30 30 3e H10020} <S60100>
5400: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 .** KEYWORDS: sq
5410: 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a lite3_version.**
5420: 0a 2a 2a 20 54 68 65 73 65 20 66 65 61 74 75 72 .** These featur
5430: 65 73 20 70 72 6f 76 69 64 65 20 74 68 65 20 73 es provide the s
5440: 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ame information
5450: 61 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 56 as the [SQLITE_V
5460: 45 52 53 49 4f 4e 5d 0a 2a 2a 20 61 6e 64 20 5b ERSION].** and [
5470: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e SQLITE_VERSION_N
5480: 55 4d 42 45 52 5d 20 23 64 65 66 69 6e 65 73 20 UMBER] #defines
5490: 69 6e 20 74 68 65 20 68 65 61 64 65 72 2c 20 62 in the header, b
54a0: 75 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 ut are associate
54b0: 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6c 69 d.** with the li
54c0: 62 72 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66 brary instead of
54d0: 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 the header file
54e0: 2e 20 20 43 61 75 74 69 6f 75 73 20 70 72 6f 67 . Cautious prog
54f0: 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 0a 2a 2a rammers might.**
5500: 20 69 6e 63 6c 75 64 65 20 61 20 63 68 65 63 6b include a check
5510: 20 69 6e 20 74 68 65 69 72 20 61 70 70 6c 69 63 in their applic
5520: 61 74 69 6f 6e 20 74 6f 20 76 65 72 69 66 79 20 ation to verify
5530: 74 68 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 5f that.** sqlite3_
5540: 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 libversion_numbe
5550: 72 28 29 20 61 6c 77 61 79 73 20 72 65 74 75 72 r() always retur
5560: 6e 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 ns the value.**
5570: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f [SQLITE_VERSION_
5580: 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 54 NUMBER]..**.** T
5590: 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 he sqlite3_libve
55a0: 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e rsion() function
55b0: 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d returns the sam
55c0: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 e information as
55d0: 20 69 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 is.** in the sq
55e0: 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 lite3_version[]
55f0: 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 2e string constant.
5600: 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 The function i
5610: 73 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20 66 6f s provided.** fo
5620: 72 20 75 73 65 20 69 6e 20 44 4c 4c 73 20 73 69 r use in DLLs si
5630: 6e 63 65 20 44 4c 4c 20 75 73 65 72 73 20 75 73 nce DLL users us
5640: 75 61 6c 6c 79 20 64 6f 20 6e 6f 74 20 68 61 76 ually do not hav
5650: 65 20 64 69 72 65 63 74 20 61 63 63 65 73 73 20 e direct access
5660: 74 6f 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e to string.** con
5670: 73 74 61 6e 74 73 20 77 69 74 68 69 6e 20 74 68 stants within th
5680: 65 20 44 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 e DLL..**.** INV
5690: 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
56a0: 48 31 30 30 32 31 7d 20 54 68 65 20 5b 73 71 6c H10021} The [sql
56b0: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f ite3_libversion_
56c0: 6e 75 6d 62 65 72 28 29 5d 20 69 6e 74 65 72 66 number()] interf
56d0: 61 63 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e ace shall return
56e0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 20 .** an
56f0: 69 6e 74 65 67 65 72 20 65 71 75 61 6c 20 74 6f integer equal to
5700: 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e [SQLITE_VERSION
5710: 5f 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 _NUMBER]..**.**
5720: 7b 48 31 30 30 32 32 7d 20 54 68 65 20 5b 73 71 {H10022} The [sq
5730: 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5d 20 73 lite3_version] s
5740: 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 73 tring constant s
5750: 68 61 6c 6c 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 hall contain.**
5760: 20 20 20 20 20 20 20 20 20 74 68 65 20 74 65 78 the tex
5770: 74 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 t of the [SQLITE
5780: 5f 56 45 52 53 49 4f 4e 5d 20 73 74 72 69 6e 67 _VERSION] string
5790: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 30 32 33 7d ..**.** {H10023}
57a0: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 The [sqlite3_li
57b0: 62 76 65 72 73 69 6f 6e 28 29 5d 20 66 75 6e 63 bversion()] func
57c0: 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 72 tion shall retur
57d0: 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 n.** a
57e0: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b pointer to the [
57f0: 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5d sqlite3_version]
5800: 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 string constant
5810: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
5820: 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 const char sqlit
5830: 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 e3_version[] = S
5840: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53 QLITE_VERSION;.S
5850: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
5860: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 char *sqlite3_li
5870: 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a bversion(void);.
5880: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
5890: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f qlite3_libversio
58a0: 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a n_number(void);.
58b0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
58c0: 20 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 Test To See If
58d0: 54 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 The Library Is T
58e0: 68 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30 hreadsafe {H1010
58f0: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S60100>.**.*
5900: 2a 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20 * SQLite can be
5910: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72 compiled with or
5920: 20 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73 without mutexes
5930: 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b . When.** the [
5940: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
5950: 45 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f E] C preprocesso
5960: 72 20 6d 61 63 72 6f 20 31 20 6f 72 20 32 2c 20 r macro 1 or 2,
5970: 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 mutexes.** are e
5980: 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c 69 74 nabled and SQLit
5990: 65 20 69 73 20 74 68 72 65 61 64 73 61 66 65 2e e is threadsafe.
59a0: 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 53 When the.** [S
59b0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
59c0: 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20 0a 2a ] macro is 0, .*
59d0: 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 72 * the mutexes ar
59e0: 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69 74 68 e omitted. With
59f0: 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65 73 2c out the mutexes,
5a00: 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0a it is not safe.
5a10: 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69 74 65 ** to use SQLite
5a20: 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20 66 72 concurrently fr
5a30: 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 om more than one
5a40: 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 45 thread..**.** E
5a50: 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65 73 20 nabling mutexes
5a60: 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75 72 61 incurs a measura
5a70: 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 ble performance
5a80: 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f 20 69 penalty..** So i
5a90: 66 20 73 70 65 65 64 20 69 73 20 6f 66 20 75 74 f speed is of ut
5aa0: 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63 65 2c most importance,
5ab0: 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73 65 20 it makes sense
5ac0: 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68 to disable.** th
5ad0: 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75 74 20 e mutexes. But
5ae0: 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61 66 65 for maximum safe
5af0: 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68 6f 75 ty, mutexes shou
5b00: 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a ld be enabled..*
5b10: 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 65 * The default be
5b20: 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20 6d 75 havior is for mu
5b30: 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e 61 62 texes to be enab
5b40: 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 led..**.** This
5b50: 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 interface can be
5b60: 20 75 73 65 64 20 62 79 20 61 20 70 72 6f 67 72 used by a progr
5b70: 61 6d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 am to make sure
5b80: 74 68 61 74 20 74 68 65 0a 2a 2a 20 76 65 72 73 that the.** vers
5b90: 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 74 68 ion of SQLite th
5ba0: 61 74 20 69 74 20 69 73 20 6c 69 6e 6b 69 6e 67 at it is linking
5bb0: 20 61 67 61 69 6e 73 74 20 77 61 73 20 63 6f 6d against was com
5bc0: 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 piled with.** th
5bd0: 65 20 64 65 73 69 72 65 64 20 73 65 74 74 69 6e e desired settin
5be0: 67 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 g of the [SQLITE
5bf0: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63 _THREADSAFE] mac
5c00: 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ro..**.** This i
5c10: 6e 74 65 72 66 61 63 65 20 6f 6e 6c 79 20 72 65 nterface only re
5c20: 70 6f 72 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d ports on the com
5c30: 70 69 6c 65 2d 74 69 6d 65 20 6d 75 74 65 78 20 pile-time mutex
5c40: 73 65 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 setting.** of th
5c50: 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 e [SQLITE_THREAD
5c60: 53 41 46 45 5d 20 66 6c 61 67 2e 20 20 49 66 20 SAFE] flag. If
5c70: 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c SQLite is compil
5c80: 65 64 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 ed with.** SQLIT
5c90: 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31 20 74 E_THREADSAFE=1 t
5ca0: 68 65 6e 20 6d 75 74 65 78 65 73 20 61 72 65 20 hen mutexes are
5cb0: 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 enabled by defau
5cc0: 6c 74 20 62 75 74 0a 2a 2a 20 63 61 6e 20 62 65 lt but.** can be
5cd0: 20 66 75 6c 6c 79 20 6f 72 20 70 61 72 74 69 61 fully or partia
5ce0: 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 75 73 69 lly disabled usi
5cf0: 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 ng a call to [sq
5d00: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 0a lite3_config()].
5d10: 2a 2a 20 77 69 74 68 20 74 68 65 20 76 65 72 62 ** with the verb
5d20: 73 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 s [SQLITE_CONFIG
5d30: 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 2c 20 _SINGLETHREAD],
5d40: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
5d50: 55 4c 54 49 54 48 52 45 41 44 5d 2c 0a 2a 2a 20 ULTITHREAD],.**
5d60: 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 or [SQLITE_CONFI
5d70: 47 5f 4d 55 54 45 58 5d 2e 20 20 54 68 65 20 72 G_MUTEX]. The r
5d80: 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 eturn value of t
5d90: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f his function sho
5da0: 77 73 0a 2a 2a 20 6f 6e 6c 79 20 74 68 65 20 64 ws.** only the d
5db0: 65 66 61 75 6c 74 20 63 6f 6d 70 69 6c 65 2d 74 efault compile-t
5dc0: 69 6d 65 20 73 65 74 74 69 6e 67 2c 20 6e 6f 74 ime setting, not
5dd0: 20 61 6e 79 20 72 75 6e 2d 74 69 6d 65 20 63 68 any run-time ch
5de0: 61 6e 67 65 73 0a 2a 2a 20 74 6f 20 74 68 61 74 anges.** to that
5df0: 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 setting..**.**
5e00: 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 See the [threadi
5e10: 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e ng mode] documen
5e20: 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 tation for addit
5e30: 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
5e40: 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 n..**.** INVARIA
5e50: 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 31 NTS:.**.** {H101
5e60: 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 01} The [sqlite3
5e70: 5f 74 68 72 65 61 64 73 61 66 65 28 29 5d 20 66 _threadsafe()] f
5e80: 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 unction shall re
5e90: 74 75 72 6e 20 7a 65 72 6f 20 69 66 0a 2a 2a 20 turn zero if.**
5ea0: 20 20 20 20 20 20 20 20 20 61 6e 64 20 6f 6e 6c and onl
5eb0: 79 20 69 66 20 53 51 4c 69 74 65 20 77 61 73 20 y if SQLite was
5ec0: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6d 75 compiled with mu
5ed0: 74 65 78 69 6e 67 20 63 6f 64 65 20 6f 6d 69 74 texing code omit
5ee0: 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 31 ted..**.** {H101
5ef0: 30 32 7d 20 54 68 65 20 76 61 6c 75 65 20 72 65 02} The value re
5f00: 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 5b 73 turned by the [s
5f10: 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 qlite3_threadsaf
5f20: 65 28 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a e()] function.**
5f30: 20 20 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 shall
5f40: 72 65 6d 61 69 6e 20 74 68 65 20 73 61 6d 65 20 remain the same
5f50: 61 63 72 6f 73 73 20 63 61 6c 6c 73 20 74 6f 20 across calls to
5f60: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 [sqlite3_config(
5f70: 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
5f80: 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 I int sqlite3_th
5f90: 72 65 61 64 73 61 66 65 28 76 6f 69 64 29 3b 0a readsafe(void);.
5fa0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
5fb0: 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 Database Connec
5fc0: 74 69 6f 6e 20 48 61 6e 64 6c 65 20 7b 48 31 32 tion Handle {H12
5fd0: 30 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 000} <S40200>.**
5fe0: 20 4b 45 59 57 4f 52 44 53 3a 20 7b 64 61 74 61 KEYWORDS: {data
5ff0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d base connection}
6000: 20 7b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 {database conne
6010: 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 ctions}.**.** Ea
6020: 63 68 20 6f 70 65 6e 20 53 51 4c 69 74 65 20 64 ch open SQLite d
6030: 61 74 61 62 61 73 65 20 69 73 20 72 65 70 72 65 atabase is repre
6040: 73 65 6e 74 65 64 20 62 79 20 61 20 70 6f 69 6e sented by a poin
6050: 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e ter to an instan
6060: 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 61 ce of.** the opa
6070: 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 que structure na
6080: 6d 65 64 20 22 73 71 6c 69 74 65 33 22 2e 20 20 med "sqlite3".
6090: 49 74 20 69 73 20 75 73 65 66 75 6c 20 74 6f 20 It is useful to
60a0: 74 68 69 6e 6b 20 6f 66 20 61 6e 20 73 71 6c 69 think of an sqli
60b0: 74 65 33 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 te3.** pointer a
60c0: 73 20 61 6e 20 6f 62 6a 65 63 74 2e 20 20 54 68 s an object. Th
60d0: 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 e [sqlite3_open(
60e0: 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 )], [sqlite3_ope
60f0: 6e 31 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b n16()], and.** [
6100: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
6110: 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 )] interfaces ar
6120: 65 20 69 74 73 20 63 6f 6e 73 74 72 75 63 74 6f e its constructo
6130: 72 73 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 rs, and [sqlite3
6140: 5f 63 6c 6f 73 65 28 29 5d 0a 2a 2a 20 69 73 20 _close()].** is
6150: 69 74 73 20 64 65 73 74 72 75 63 74 6f 72 2e 20 its destructor.
6160: 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 There are many
6170: 6f 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 other interfaces
6180: 20 28 73 75 63 68 20 61 73 0a 2a 2a 20 5b 73 71 (such as.** [sq
6190: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
61a0: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 ()], [sqlite3_cr
61b0: 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d eate_function()]
61c0: 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 , and.** [sqlite
61d0: 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 3_busy_timeout()
61e0: 5d 20 74 6f 20 6e 61 6d 65 20 62 75 74 20 74 68 ] to name but th
61f0: 72 65 65 29 20 74 68 61 74 20 61 72 65 20 6d 65 ree) that are me
6200: 74 68 6f 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 thods on an.** s
6210: 71 6c 69 74 65 33 20 6f 62 6a 65 63 74 2e 0a 2a qlite3 object..*
6220: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
6230: 20 73 71 6c 69 74 65 33 20 73 71 6c 69 74 65 33 sqlite3 sqlite3
6240: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
6250: 46 3a 20 36 34 2d 42 69 74 20 49 6e 74 65 67 65 F: 64-Bit Intege
6260: 72 20 54 79 70 65 73 20 7b 48 31 30 32 30 30 7d r Types {H10200}
6270: 20 3c 53 31 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 <S10110>.** KEY
6280: 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 5f 69 6e WORDS: sqlite_in
6290: 74 36 34 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 t64 sqlite_uint6
62a0: 34 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 4.**.** Because
62b0: 74 68 65 72 65 20 69 73 20 6e 6f 20 63 72 6f 73 there is no cros
62c0: 73 2d 70 6c 61 74 66 6f 72 6d 20 77 61 79 20 74 s-platform way t
62d0: 6f 20 73 70 65 63 69 66 79 20 36 34 2d 62 69 74 o specify 64-bit
62e0: 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 0a 2a integer types.*
62f0: 2a 20 53 51 4c 69 74 65 20 69 6e 63 6c 75 64 65 * SQLite include
6300: 73 20 74 79 70 65 64 65 66 73 20 66 6f 72 20 36 s typedefs for 6
6310: 34 2d 62 69 74 20 73 69 67 6e 65 64 20 61 6e 64 4-bit signed and
6320: 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 unsigned intege
6330: 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 rs..**.** The sq
6340: 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 6e 64 20 lite3_int64 and
6350: 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 61 sqlite3_uint64 a
6360: 72 65 20 74 68 65 20 70 72 65 66 65 72 72 65 64 re the preferred
6370: 20 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f 6e type definition
6380: 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 s..** The sqlite
6390: 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74 _int64 and sqlit
63a0: 65 5f 75 69 6e 74 36 34 20 74 79 70 65 73 20 61 e_uint64 types a
63b0: 72 65 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 re supported for
63c0: 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f backwards.** co
63d0: 6d 70 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 mpatibility only
63e0: 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
63f0: 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 30 TS:.**.** {H1020
6400: 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 5f 69 1} The [sqlite_i
6410: 6e 74 36 34 5d 20 61 6e 64 20 5b 73 71 6c 69 74 nt64] and [sqlit
6420: 65 33 5f 69 6e 74 36 34 5d 20 74 79 70 65 20 73 e3_int64] type s
6430: 68 61 6c 6c 20 73 70 65 63 69 66 79 0a 2a 2a 20 hall specify.**
6440: 20 20 20 20 20 20 20 20 20 61 20 36 34 2d 62 69 a 64-bi
6450: 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
6460: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 30 32 7d ..**.** {H10202}
6470: 20 54 68 65 20 5b 73 71 6c 69 74 65 5f 75 69 6e The [sqlite_uin
6480: 74 36 34 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 t64] and [sqlite
6490: 33 5f 75 69 6e 74 36 34 5d 20 74 79 70 65 20 73 3_uint64] type s
64a0: 68 61 6c 6c 20 73 70 65 63 69 66 79 0a 2a 2a 20 hall specify.**
64b0: 20 20 20 20 20 20 20 20 20 61 20 36 34 2d 62 69 a 64-bi
64c0: 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 t unsigned integ
64d0: 65 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 er..*/.#ifdef SQ
64e0: 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a LITE_INT64_TYPE.
64f0: 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 typedef SQLITE
6500: 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 _INT64_TYPE sqli
6510: 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 te_int64;. type
6520: 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c def unsigned SQL
6530: 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 ITE_INT64_TYPE s
6540: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 qlite_uint64;.#e
6550: 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 lif defined(_MSC
6560: 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 _VER) || defined
6570: 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 (__BORLANDC__).
6580: 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 typedef __int64
6590: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 sqlite_int64;.
65a0: 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 typedef unsigne
65b0: 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 d __int64 sqlite
65c0: 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 _uint64;.#else.
65d0: 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f typedef long lo
65e0: 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e ng int sqlite_in
65f0: 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 t64;. typedef u
6600: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
6610: 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e g int sqlite_uin
6620: 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 t64;.#endif.type
6630: 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 def sqlite_int64
6640: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a sqlite3_int64;.
6650: 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 typedef sqlite_u
6660: 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 int64 sqlite3_ui
6670: 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 nt64;../*.** If
6680: 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 compiling for a
6690: 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c processor that l
66a0: 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f acks floating po
66b0: 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 int support,.**
66c0: 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 substitute integ
66d0: 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d er for floating-
66e0: 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 point..*/.#ifdef
66f0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
6700: 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 ATING_POINT.# de
6710: 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 fine double sqli
6720: 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 te3_int64.#endif
6730: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
6740: 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 : Closing A Data
6750: 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 base Connection
6760: 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30 {H12010} <S30100
6770: 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 ><S40200>.**.**
6780: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
6790: 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 the destructor f
67a0: 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d or the [sqlite3]
67b0: 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 object..**.** A
67c0: 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 pplications shou
67d0: 6c 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 ld [sqlite3_fina
67e0: 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d lize | finalize]
67f0: 20 61 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 all [prepared s
6800: 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e tatements].** an
6810: 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f d [sqlite3_blob_
6820: 63 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 close | close] a
6830: 6c 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 ll [BLOB handles
6840: 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 ] associated wit
6850: 68 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 h.** the [sqlite
6860: 33 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 3] object prior
6870: 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f to attempting to
6880: 20 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 close the objec
6890: 74 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 t..** The [sqlit
68a0: 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 29 5d 20 e3_next_stmt()]
68b0: 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 interface can be
68c0: 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 used to locate
68d0: 61 6c 6c 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 all.** [prepared
68e0: 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 61 73 73 statements] ass
68f0: 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 5b ociated with a [
6900: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
6910: 69 6f 6e 5d 20 69 66 20 64 65 73 69 72 65 64 2e ion] if desired.
6920: 0a 2a 2a 20 54 79 70 69 63 61 6c 20 63 6f 64 65 .** Typical code
6930: 20 6d 69 67 68 74 20 6c 6f 6f 6b 20 6c 69 6b 65 might look like
6940: 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c this:.**.** <bl
6950: 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a ockquote><pre>.*
6960: 2a 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a * sqlite3_stmt *
6970: 70 53 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c 65 28 pStmt;.** while(
6980: 20 28 70 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 (pStmt = sqlite
6990: 33 5f 6e 65 78 74 5f 73 74 6d 74 28 64 62 2c 20 3_next_stmt(db,
69a0: 30 29 29 21 3d 30 20 29 7b 0a 2a 2a 20 26 6e 62 0))!=0 ){.** &nb
69b0: 73 70 3b 20 20 20 73 71 6c 69 74 65 33 5f 66 69 sp; sqlite3_fi
69c0: 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 2a nalize(pStmt);.*
69d0: 2a 20 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 * }.** </pre></b
69e0: 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
69f0: 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f If [sqlite3_clo
6a00: 73 65 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 se()] is invoked
6a10: 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73 61 63 while a transac
6a20: 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a 2a 2a tion is open,.**
6a30: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
6a40: 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c is automaticall
6a50: 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a y rolled back..*
6a60: 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
6a70: 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 30 31 31 7d 20 .**.** {H12011}
6a80: 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c A successful cal
6a90: 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c l to [sqlite3_cl
6aa0: 6f 73 65 28 43 29 5d 20 73 68 61 6c 6c 20 64 65 ose(C)] shall de
6ab0: 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20 20 stroy the.**
6ac0: 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 [database
6ad0: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 6a 65 connection] obje
6ae0: 63 74 20 43 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 ct C..**.** {H12
6af0: 30 31 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 012} A successfu
6b00: 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 l call to [sqlit
6b10: 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 73 68 61 e3_close(C)] sha
6b20: 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ll return SQLITE
6b30: 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 30 _OK..**.** {H120
6b40: 31 33 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 13} A successful
6b50: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
6b60: 33 5f 63 6c 6f 73 65 28 43 29 5d 20 73 68 61 6c 3_close(C)] shal
6b70: 6c 20 72 65 6c 65 61 73 65 20 61 6c 6c 0a 2a 2a l release all.**
6b80: 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 memory
6b90: 20 61 6e 64 20 73 79 73 74 65 6d 20 72 65 73 6f and system reso
6ba0: 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 urces associated
6bb0: 20 77 69 74 68 20 5b 64 61 74 61 62 61 73 65 20 with [database
6bc0: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 20 connection].**
6bd0: 20 20 20 20 20 20 20 20 43 2e 0a 2a 2a 0a 2a 2a C..**.**
6be0: 20 7b 48 31 32 30 31 34 7d 20 41 20 63 61 6c 6c {H12014} A call
6bf0: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f to [sqlite3_clo
6c00: 73 65 28 43 29 5d 20 6f 6e 20 61 20 5b 64 61 74 se(C)] on a [dat
6c10: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
6c20: 5d 20 43 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 ] C that.**
6c30: 20 20 20 20 20 68 61 73 20 6f 6e 65 20 6f 72 20 has one or
6c40: 6d 6f 72 65 20 6f 70 65 6e 20 5b 70 72 65 70 61 more open [prepa
6c50: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 red statements]
6c60: 73 68 61 6c 6c 20 66 61 69 6c 20 77 69 74 68 0a shall fail with.
6c70: 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 20 5b ** an [
6c80: 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72 SQLITE_BUSY] err
6c90: 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b or code..**.** {
6ca0: 48 31 32 30 31 35 7d 20 41 20 63 61 6c 6c 20 74 H12015} A call t
6cb0: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 o [sqlite3_close
6cc0: 28 43 29 5d 20 77 68 65 72 65 20 43 20 69 73 20 (C)] where C is
6cd0: 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 73 a NULL pointer s
6ce0: 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 hall.**
6cf0: 20 62 65 20 61 20 68 61 72 6d 6c 65 73 73 20 6e be a harmless n
6d00: 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 53 o-op returning S
6d10: 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 QLITE_OK..**.**
6d20: 7b 48 31 32 30 31 39 7d 20 57 68 65 6e 20 5b 73 {H12019} When [s
6d30: 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d qlite3_close(C)]
6d40: 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 is invoked on a
6d50: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
6d60: 63 74 69 6f 6e 5d 20 43 0a 2a 2a 20 20 20 20 20 ction] C.**
6d70: 20 20 20 20 20 74 68 61 74 20 68 61 73 20 61 20 that has a
6d80: 70 65 6e 64 69 6e 67 20 74 72 61 6e 73 61 63 74 pending transact
6d90: 69 6f 6e 2c 20 74 68 65 20 74 72 61 6e 73 61 63 ion, the transac
6da0: 74 69 6f 6e 20 73 68 61 6c 6c 20 62 65 0a 2a 2a tion shall be.**
6db0: 20 20 20 20 20 20 20 20 20 20 72 6f 6c 6c 65 64 rolled
6dc0: 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 back..**.** ASS
6dd0: 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 UMPTIONS:.**.**
6de0: 7b 41 31 32 30 31 36 7d 20 54 68 65 20 43 20 70 {A12016} The C p
6df0: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
6e00: 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 6d ite3_close(C)] m
6e10: 75 73 74 20 62 65 20 65 69 74 68 65 72 20 61 20 ust be either a
6e20: 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 NULL.**
6e30: 20 70 6f 69 6e 74 65 72 20 6f 72 20 61 6e 20 5b pointer or an [
6e40: 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 20 sqlite3] object
6e50: 70 6f 69 6e 74 65 72 20 6f 62 74 61 69 6e 65 64 pointer obtained
6e60: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f .** fro
6e70: 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 m [sqlite3_open(
6e80: 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 )], [sqlite3_ope
6e90: 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 n16()], or.**
6ea0: 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
6eb0: 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 open_v2()], and
6ec0: 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 not previously c
6ed0: 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 losed..*/.SQLITE
6ee0: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
6ef0: 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a _close(sqlite3 *
6f00: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 );../*.** The ty
6f10: 70 65 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 pe for a callbac
6f20: 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 k function..** T
6f30: 68 69 73 20 69 73 20 6c 65 67 61 63 79 20 61 6e his is legacy an
6f40: 64 20 64 65 70 72 65 63 61 74 65 64 2e 20 20 49 d deprecated. I
6f50: 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 66 6f t is included fo
6f60: 72 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 r historical.**
6f70: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 61 6e compatibility an
6f80: 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 6d 65 6e d is not documen
6f90: 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ted..*/.typedef
6fa0: 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 5f 63 61 int (*sqlite3_ca
6fb0: 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e llback)(void*,in
6fc0: 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a t,char**, char**
6fd0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
6fe0: 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 51 75 65 EF: One-Step Que
6ff0: 72 79 20 45 78 65 63 75 74 69 6f 6e 20 49 6e 74 ry Execution Int
7000: 65 72 66 61 63 65 20 7b 48 31 32 31 30 30 7d 20 erface {H12100}
7010: 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10000>.**.** T
7020: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 he sqlite3_exec(
7030: 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 61 ) interface is a
7040: 20 63 6f 6e 76 65 6e 69 65 6e 74 20 77 61 79 20 convenient way
7050: 6f 66 20 72 75 6e 6e 69 6e 67 20 6f 6e 65 20 6f of running one o
7060: 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20 73 74 r more.** SQL st
7070: 61 74 65 6d 65 6e 74 73 20 77 69 74 68 6f 75 74 atements without
7080: 20 68 61 76 69 6e 67 20 74 6f 20 77 72 69 74 65 having to write
7090: 20 61 20 6c 6f 74 20 6f 66 20 43 20 63 6f 64 65 a lot of C code
70a0: 2e 20 20 54 68 65 20 55 54 46 2d 38 20 65 6e 63 . The UTF-8 enc
70b0: 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 73 74 61 74 oded.** SQL stat
70c0: 65 6d 65 6e 74 73 20 61 72 65 20 70 61 73 73 65 ements are passe
70d0: 64 20 69 6e 20 61 73 20 74 68 65 20 73 65 63 6f d in as the seco
70e0: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
70f0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a sqlite3_exec()..
7100: 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 ** The statement
7110: 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 s are evaluated
7120: 6f 6e 65 20 62 79 20 6f 6e 65 20 75 6e 74 69 6c one by one until
7130: 20 65 69 74 68 65 72 20 61 6e 20 65 72 72 6f 72 either an error
7140: 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e 74 65 72 72 or.** an interr
7150: 75 70 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 upt is encounter
7160: 65 64 2c 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 ed, or until the
7170: 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e 65 2e 20 y are all done.
7180: 20 54 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 The 3rd paramet
7190: 65 72 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 74 69 er.** is an opti
71a0: 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 74 68 onal callback th
71b0: 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e at is invoked on
71c0: 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 ce for each row
71d0: 6f 66 20 61 6e 79 20 71 75 65 72 79 0a 2a 2a 20 of any query.**
71e0: 72 65 73 75 6c 74 73 20 70 72 6f 64 75 63 65 64 results produced
71f0: 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 by the SQL stat
7200: 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 35 74 68 ements. The 5th
7210: 20 70 61 72 61 6d 65 74 65 72 20 74 65 6c 6c 73 parameter tells
7220: 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20 77 72 69 where.** to wri
7230: 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 te any error mes
7240: 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 sages..**.** The
7250: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70 error message p
7260: 61 73 73 65 64 20 62 61 63 6b 20 74 68 72 6f 75 assed back throu
7270: 67 68 20 74 68 65 20 35 74 68 20 70 61 72 61 6d gh the 5th param
7280: 65 74 65 72 20 69 73 20 68 65 6c 64 0a 2a 2a 20 eter is held.**
7290: 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e in memory obtain
72a0: 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
72b0: 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 6f 20 _malloc()]. To
72c0: 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c avoid a memory l
72d0: 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c eak,.** the call
72e0: 69 6e 67 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 ing application
72f0: 73 68 6f 75 6c 64 20 63 61 6c 6c 20 5b 73 71 6c should call [sql
7300: 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 6e 20 ite3_free()] on
7310: 61 6e 79 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 any error.** mes
7320: 73 61 67 65 20 72 65 74 75 72 6e 65 64 20 74 68 sage returned th
7330: 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61 rough the 5th pa
7340: 72 61 6d 65 74 65 72 20 77 68 65 6e 20 69 74 20 rameter when it
7350: 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 has finished usi
7360: 6e 67 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 ng.** the error
7370: 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 message..**.** I
7380: 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d f the SQL statem
7390: 65 6e 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70 ent in the 2nd p
73a0: 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 4c 4c arameter is NULL
73b0: 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72 or an empty str
73c0: 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 73 74 72 69 ing.** or a stri
73d0: 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f 6e ng containing on
73e0: 6c 79 20 77 68 69 74 65 73 70 61 63 65 20 61 6e ly whitespace an
73f0: 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 65 6e d comments, then
7400: 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 no SQL.** state
7410: 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 ments are evalua
7420: 74 65 64 20 61 6e 64 20 74 68 65 20 64 61 74 61 ted and the data
7430: 62 61 73 65 20 69 73 20 6e 6f 74 20 63 68 61 6e base is not chan
7440: 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ged..**.** The s
7450: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e qlite3_exec() in
7460: 74 65 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65 terface is imple
7470: 6d 65 6e 74 65 64 20 69 6e 20 74 65 72 6d 73 20 mented in terms
7480: 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 of.** [sqlite3_p
7490: 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 repare_v2()], [s
74a0: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 qlite3_step()],
74b0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e and [sqlite3_fin
74c0: 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 alize()]..** The
74d0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 sqlite3_exec()
74e0: 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 routine does not
74f0: 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 hing to the data
7500: 62 61 73 65 20 74 68 61 74 20 63 61 6e 6e 6f 74 base that cannot
7510: 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 5b be done.** by [
7520: 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
7530: 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f v2()], [sqlite3_
7540: 73 74 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71 step()], and [sq
7550: 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
7560: 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 ]..**.** INVARIA
7570: 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 NTS:.**.** {H121
7580: 30 31 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 01} A successful
7590: 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b invocation of [
75a0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 sqlite3_exec(D,S
75b0: 2c 43 2c 41 2c 45 29 5d 0a 2a 2a 20 20 20 20 20 ,C,A,E)].**
75c0: 20 20 20 20 20 73 68 61 6c 6c 20 73 65 71 75 65 shall seque
75d0: 6e 74 69 61 6c 6c 79 20 65 76 61 6c 75 61 74 65 ntially evaluate
75e0: 20 61 6c 6c 20 6f 66 20 74 68 65 20 55 54 46 2d all of the UTF-
75f0: 38 20 65 6e 63 6f 64 65 64 2c 0a 2a 2a 20 20 20 8 encoded,.**
7600: 20 20 20 20 20 20 20 73 65 6d 69 63 6f 6c 6f 6e semicolon
7610: 2d 73 65 70 61 72 61 74 65 64 20 53 51 4c 20 73 -separated SQL s
7620: 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 tatements in the
7630: 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
7640: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 72 .** str
7650: 69 6e 67 20 53 20 77 69 74 68 69 6e 20 74 68 65 ing S within the
7660: 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 context of the
7670: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
7680: 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b tion] D..**.** {
7690: 48 31 32 31 30 32 7d 20 49 66 20 74 68 65 20 53 H12102} If the S
76a0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
76b0: 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c qlite3_exec(D,S,
76c0: 43 2c 41 2c 45 29 5d 20 69 73 20 4e 55 4c 4c 20 C,A,E)] is NULL
76d0: 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 then.**
76e0: 20 74 68 65 20 61 63 74 69 6f 6e 73 20 6f 66 20 the actions of
76f0: 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 73 68 the interface sh
7700: 61 6c 6c 20 62 65 20 74 68 65 20 73 61 6d 65 20 all be the same
7710: 61 73 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 20 as if the.**
7720: 20 20 20 20 20 20 53 20 70 61 72 61 6d 65 74 65 S paramete
7730: 72 20 77 65 72 65 20 61 6e 20 65 6d 70 74 79 20 r were an empty
7740: 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 string..**.** {H
7750: 31 32 31 30 34 7d 20 54 68 65 20 72 65 74 75 72 12104} The retur
7760: 6e 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 n value of [sqli
7770: 74 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 6c te3_exec()] shal
7780: 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d l be [SQLITE_OK]
7790: 20 69 66 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 if all.**
77a0: 20 20 20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e SQL statemen
77b0: 74 73 20 72 75 6e 20 73 75 63 63 65 73 73 66 75 ts run successfu
77c0: 6c 6c 79 20 61 6e 64 20 74 6f 20 63 6f 6d 70 6c lly and to compl
77d0: 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 etion..**.** {H1
77e0: 32 31 30 35 7d 20 54 68 65 20 72 65 74 75 72 6e 2105} The return
77f0: 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74 value of [sqlit
7800: 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 6c 6c e3_exec()] shall
7810: 20 62 65 20 61 6e 20 61 70 70 72 6f 70 72 69 61 be an appropria
7820: 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e te.** n
7830: 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 on-zero [error c
7840: 6f 64 65 5d 20 69 66 20 61 6e 79 20 53 51 4c 20 ode] if any SQL
7850: 73 74 61 74 65 6d 65 6e 74 20 66 61 69 6c 73 2e statement fails.
7860: 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 30 37 7d 20 .**.** {H12107}
7870: 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f If one or more o
7880: 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d f the SQL statem
7890: 65 6e 74 73 20 68 61 6e 64 65 64 20 74 6f 20 5b ents handed to [
78a0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 0a sqlite3_exec()].
78b0: 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ** retu
78c0: 72 6e 20 72 65 73 75 6c 74 73 20 61 6e 64 20 74 rn results and t
78d0: 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 he 3rd parameter
78e0: 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 is not NULL, th
78f0: 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 en.** t
7900: 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 he callback func
7910: 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 62 tion specified b
7920: 79 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 y the 3rd parame
7930: 74 65 72 20 73 68 61 6c 6c 20 62 65 0a 2a 2a 20 ter shall be.**
7940: 20 20 20 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 invoked
7950: 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 once for each r
7960: 6f 77 20 6f 66 20 72 65 73 75 6c 74 2e 0a 2a 2a ow of result..**
7970: 0a 2a 2a 20 7b 48 31 32 31 31 30 7d 20 49 66 20 .** {H12110} If
7980: 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 the callback ret
7990: 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 urns a non-zero
79a0: 76 61 6c 75 65 20 74 68 65 6e 20 5b 73 71 6c 69 value then [sqli
79b0: 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a 20 20 te3_exec()].**
79c0: 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 61 62 shall ab
79d0: 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 ort the SQL stat
79e0: 65 6d 65 6e 74 20 69 74 20 69 73 20 63 75 72 72 ement it is curr
79f0: 65 6e 74 6c 79 20 65 76 61 6c 75 61 74 69 6e 67 ently evaluating
7a00: 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 6b ,.** sk
7a10: 69 70 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e ip all subsequen
7a20: 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 t SQL statements
7a30: 2c 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 , and return [SQ
7a40: 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a LITE_ABORT]..**.
7a50: 2a 2a 20 7b 48 31 32 31 31 33 7d 20 54 68 65 20 ** {H12113} The
7a60: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d [sqlite3_exec()]
7a70: 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 70 routine shall p
7a80: 61 73 73 20 69 74 73 20 34 74 68 20 70 61 72 61 ass its 4th para
7a90: 6d 65 74 65 72 20 74 68 72 6f 75 67 68 0a 2a 2a meter through.**
7aa0: 20 20 20 20 20 20 20 20 20 20 61 73 20 74 68 65 as the
7ab0: 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 20 6f 1st parameter o
7ac0: 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a f the callback..
7ad0: 2a 2a 0a 2a 2a 20 7b 48 31 32 31 31 36 7d 20 54 **.** {H12116} T
7ae0: 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 he [sqlite3_exec
7af0: 28 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 61 6c ()] routine shal
7b00: 6c 20 73 65 74 20 74 68 65 20 32 6e 64 20 70 61 l set the 2nd pa
7b10: 72 61 6d 65 74 65 72 20 6f 66 20 69 74 73 0a 2a rameter of its.*
7b20: 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62 * callb
7b30: 61 63 6b 20 74 6f 20 62 65 20 74 68 65 20 6e 75 ack to be the nu
7b40: 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
7b50: 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 in the current r
7b60: 6f 77 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 ow of.**
7b70: 20 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 result..**.**
7b80: 7b 48 31 32 31 31 39 7d 20 54 68 65 20 5b 73 71 {H12119} The [sq
7b90: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 72 6f lite3_exec()] ro
7ba0: 75 74 69 6e 65 20 73 68 61 6c 6c 20 73 65 74 20 utine shall set
7bb0: 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 the 3rd paramete
7bc0: 72 20 6f 66 20 69 74 73 0a 2a 2a 20 20 20 20 20 r of its.**
7bd0: 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 74 6f callback to
7be0: 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 be an array of
7bf0: 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 69 pointers to stri
7c00: 6e 67 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a ngs holding the.
7c10: 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 ** valu
7c20: 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 es for each colu
7c30: 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e mn in the curren
7c40: 74 20 72 65 73 75 6c 74 20 73 65 74 20 72 6f 77 t result set row
7c50: 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 as.**
7c60: 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 obtained from [s
7c70: 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
7c80: 78 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 xt()]..**.** {H1
7c90: 32 31 32 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 2122} The [sqlit
7ca0: 65 33 5f 65 78 65 63 28 29 5d 20 72 6f 75 74 69 e3_exec()] routi
7cb0: 6e 65 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 ne shall set the
7cc0: 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 6f 4th parameter o
7cd0: 66 20 69 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 f its.**
7ce0: 20 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 callback to be
7cf0: 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 an array of poi
7d00: 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 nters to strings
7d10: 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20 holding the.**
7d20: 20 20 20 20 20 20 20 20 20 6e 61 6d 65 73 20 6f names o
7d30: 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 f result columns
7d40: 20 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f as obtained fro
7d50: 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d m [sqlite3_colum
7d60: 6e 5f 6e 61 6d 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a n_name()]..**.**
7d70: 20 7b 48 31 32 31 32 35 7d 20 49 66 20 74 68 65 {H12125} If the
7d80: 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 3rd parameter t
7d90: 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 o [sqlite3_exec(
7da0: 29 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a )] is NULL then.
7db0: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
7dc0: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 ite3_exec()] sha
7dd0: 6c 6c 20 73 69 6c 65 6e 74 6c 79 20 64 69 73 63 ll silently disc
7de0: 61 72 64 20 71 75 65 72 79 20 72 65 73 75 6c 74 ard query result
7df0: 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 33 31 s..**.** {H12131
7e00: 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 } If an error oc
7e10: 63 75 72 73 20 77 68 69 6c 65 20 70 61 72 73 69 curs while parsi
7e20: 6e 67 20 6f 72 20 65 76 61 6c 75 61 74 69 6e 67 ng or evaluating
7e30: 20 61 6e 79 20 6f 66 20 74 68 65 20 53 51 4c 0a any of the SQL.
7e40: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 ** stat
7e50: 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 53 20 ements in the S
7e60: 70 61 72 61 6d 65 74 65 72 20 6f 66 20 5b 73 71 parameter of [sq
7e70: 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 lite3_exec(D,S,C
7e80: 2c 41 2c 45 29 5d 20 61 6e 64 20 69 66 0a 2a 2a ,A,E)] and if.**
7e90: 20 20 20 20 20 20 20 20 20 20 74 68 65 20 45 20 the E
7ea0: 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 parameter is not
7eb0: 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 5b 73 71 6c NULL, then [sql
7ec0: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 ite3_exec()] sha
7ed0: 6c 6c 20 73 74 6f 72 65 0a 2a 2a 20 20 20 20 20 ll store.**
7ee0: 20 20 20 20 20 69 6e 20 2a 45 20 61 6e 20 61 70 in *E an ap
7ef0: 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 propriate error
7f00: 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 message written
7f10: 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
7f20: 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 ined.**
7f30: 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
7f40: 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 alloc()]..**.**
7f50: 7b 48 31 32 31 33 34 7d 20 54 68 65 20 5b 73 71 {H12134} The [sq
7f60: 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 lite3_exec(D,S,C
7f70: 2c 41 2c 45 29 5d 20 72 6f 75 74 69 6e 65 20 73 ,A,E)] routine s
7f80: 68 61 6c 6c 20 73 65 74 20 74 68 65 20 76 61 6c hall set the val
7f90: 75 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 ue of.**
7fa0: 20 20 2a 45 20 74 6f 20 4e 55 4c 4c 20 69 66 20 *E to NULL if
7fb0: 45 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e E is not NULL an
7fc0: 64 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 65 d there are no e
7fd0: 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 rrors..**.** {H1
7fe0: 32 31 33 37 7d 20 54 68 65 20 5b 73 71 6c 69 74 2137} The [sqlit
7ff0: 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c 41 2c e3_exec(D,S,C,A,
8000: 45 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 E)] function sha
8010: 6c 6c 20 73 65 74 20 74 68 65 20 5b 65 72 72 6f ll set the [erro
8020: 72 20 63 6f 64 65 5d 0a 2a 2a 20 20 20 20 20 20 r code].**
8030: 20 20 20 20 61 6e 64 20 6d 65 73 73 61 67 65 20 and message
8040: 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b accessible via [
8050: 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 sqlite3_errcode(
8060: 29 5d 2c 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 )], .**
8070: 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 [sqlite3_extend
8080: 65 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a ed_errcode()],.*
8090: 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
80a0: 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 61 te3_errmsg()], a
80b0: 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d nd [sqlite3_errm
80c0: 73 67 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b sg16()]..**.** {
80d0: 48 31 32 31 33 38 7d 20 49 66 20 74 68 65 20 53 H12138} If the S
80e0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
80f0: 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c qlite3_exec(D,S,
8100: 43 2c 41 2c 45 29 5d 20 69 73 20 4e 55 4c 4c 20 C,A,E)] is NULL
8110: 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 or an.**
8120: 20 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 6f empty string o
8130: 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 74 68 69 r contains nothi
8140: 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 77 68 ng other than wh
8150: 69 74 65 73 70 61 63 65 2c 20 63 6f 6d 6d 65 6e itespace, commen
8160: 74 73 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ts,.**
8170: 61 6e 64 2f 6f 72 20 73 65 6d 69 63 6f 6c 6f 6e and/or semicolon
8180: 73 2c 20 74 68 65 6e 20 72 65 73 75 6c 74 73 20 s, then results
8190: 6f 66 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 of [sqlite3_errc
81a0: 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 ode()],.**
81b0: 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 [sqlite3_ext
81c0: 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 5d ended_errcode()]
81d0: 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 ,.** [s
81e0: 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d qlite3_errmsg()]
81f0: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 , and [sqlite3_e
8200: 72 72 6d 73 67 31 36 28 29 5d 0a 2a 2a 20 20 20 rrmsg16()].**
8210: 20 20 20 20 20 20 20 73 68 61 6c 6c 20 72 65 73 shall res
8220: 65 74 20 74 6f 20 69 6e 64 69 63 61 74 65 20 6e et to indicate n
8230: 6f 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 o errors..**.**
8240: 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a ASSUMPTIONS:.**.
8250: 2a 2a 20 7b 41 31 32 31 34 31 7d 20 54 68 65 20 ** {A12141} The
8260: 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
8270: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 to [sqlite3_exec
8280: 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e 20 76 ()] must be an v
8290: 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a 2a 2a alid and open.**
82a0: 20 20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 [datab
82b0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e ase connection].
82c0: 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 31 34 32 7d 20 .**.** {A12142}
82d0: 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
82e0: 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 nection must not
82f0: 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 be closed while
8300: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
8310: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73 lite3_exec()] is
8320: 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 running..**.**
8330: 7b 41 31 32 31 34 33 7d 20 54 68 65 20 63 61 6c {A12143} The cal
8340: 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 ling function sh
8350: 6f 75 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65 ould use [sqlite
8360: 33 5f 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65 3_free()] to fre
8370: 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 e.** th
8380: 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 2a 65 e memory that *e
8390: 72 72 6d 73 67 20 69 73 20 6c 65 66 74 20 70 6f rrmsg is left po
83a0: 69 6e 74 69 6e 67 20 61 74 20 6f 6e 63 65 20 74 inting at once t
83b0: 68 65 20 65 72 72 6f 72 0a 2a 2a 20 20 20 20 20 he error.**
83c0: 20 20 20 20 20 6d 65 73 73 61 67 65 20 69 73 20 message is
83d0: 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 64 65 64 no longer needed
83e0: 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 31 34 35 7d ..**.** {A12145}
83f0: 20 54 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 The SQL stateme
8400: 6e 74 20 74 65 78 74 20 69 6e 20 74 68 65 20 32 nt text in the 2
8410: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
8420: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d [sqlite3_exec()]
8430: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 75 73 .** mus
8440: 74 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 t remain unchang
8450: 65 64 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 ed while [sqlite
8460: 33 5f 65 78 65 63 28 29 5d 20 69 73 20 72 75 6e 3_exec()] is run
8470: 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ning..*/.SQLITE_
8480: 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
8490: 65 78 65 63 28 0a 20 20 73 71 6c 69 74 65 33 2a exec(. sqlite3*
84a0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
84b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
84c0: 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 /* An open da
84d0: 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 tabase */. cons
84e0: 74 20 63 68 61 72 20 2a 73 71 6c 2c 20 20 20 20 t char *sql,
84f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8500: 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f /* SQL to
8510: 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f be evaluated */
8520: 0a 20 20 69 6e 74 20 28 2a 63 61 6c 6c 62 61 63 . int (*callbac
8530: 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 k)(void*,int,cha
8540: 72 2a 2a 2c 63 68 61 72 2a 2a 29 2c 20 20 2f 2a r**,char**), /*
8550: 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 Callback functi
8560: 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2c 20 on */. void *,
8570: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8580: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8590: 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75 6d 65 /* 1st argume
85a0: 6e 74 20 74 6f 20 63 61 6c 6c 62 61 63 6b 20 2a nt to callback *
85b0: 2f 0a 20 20 63 68 61 72 20 2a 2a 65 72 72 6d 73 /. char **errms
85c0: 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g
85d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
85e0: 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74 * Error msg writ
85f0: 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 0a ten here */.);..
8600: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
8610: 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31 Result Codes {H1
8620: 30 32 31 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 0210} <S10700>.*
8630: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 4c 49 * KEYWORDS: SQLI
8640: 54 45 5f 4f 4b 20 7b 65 72 72 6f 72 20 63 6f 64 TE_OK {error cod
8650: 65 7d 20 7b 65 72 72 6f 72 20 63 6f 64 65 73 7d e} {error codes}
8660: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 72 .** KEYWORDS: {r
8670: 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b 72 65 73 esult code} {res
8680: 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a ult codes}.**.**
8690: 20 4d 61 6e 79 20 53 51 4c 69 74 65 20 66 75 6e Many SQLite fun
86a0: 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 61 6e ctions return an
86b0: 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74 20 integer result
86c0: 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 73 65 code from the se
86d0: 74 20 73 68 6f 77 6e 0a 2a 2a 20 68 65 72 65 20 t shown.** here
86e0: 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 64 69 in order to indi
86f0: 63 61 74 65 73 20 73 75 63 63 65 73 73 20 6f 72 cates success or
8700: 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 failure..**.**
8710: 4e 65 77 20 65 72 72 6f 72 20 63 6f 64 65 73 20 New error codes
8720: 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 may be added in
8730: 66 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 future versions
8740: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a of SQLite..**.**
8750: 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 See also: [SQLI
8760: 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 7c 20 TE_IOERR_READ |
8770: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
8780: 63 6f 64 65 73 5d 0a 2a 2f 0a 23 64 65 66 69 6e codes].*/.#defin
8790: 65 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 20 e SQLITE_OK
87a0: 20 20 20 20 20 20 30 20 20 20 2f 2a 20 53 75 63 0 /* Suc
87b0: 63 65 73 73 66 75 6c 20 72 65 73 75 6c 74 20 2a cessful result *
87c0: 2f 0a 2f 2a 20 62 65 67 69 6e 6e 69 6e 67 2d 6f /./* beginning-o
87d0: 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f f-error-codes */
87e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
87f0: 45 52 52 4f 52 20 20 20 20 20 20 20 20 31 20 20 ERROR 1
8800: 20 2f 2a 20 53 51 4c 20 65 72 72 6f 72 20 6f 72 /* SQL error or
8810: 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62 61 73 missing databas
8820: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
8830: 49 54 45 5f 49 4e 54 45 52 4e 41 4c 20 20 20 20 ITE_INTERNAL
8840: 20 32 20 20 20 2f 2a 20 49 6e 74 65 72 6e 61 6c 2 /* Internal
8850: 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 logic error in
8860: 53 51 4c 69 74 65 20 2a 2f 0a 23 64 65 66 69 6e SQLite */.#defin
8870: 65 20 53 51 4c 49 54 45 5f 50 45 52 4d 20 20 20 e SQLITE_PERM
8880: 20 20 20 20 20 20 33 20 20 20 2f 2a 20 41 63 63 3 /* Acc
8890: 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 64 ess permission d
88a0: 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 enied */.#define
88b0: 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 20 20 SQLITE_ABORT
88c0: 20 20 20 20 20 34 20 20 20 2f 2a 20 43 61 6c 6c 4 /* Call
88d0: 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 71 back routine req
88e0: 75 65 73 74 65 64 20 61 6e 20 61 62 6f 72 74 20 uested an abort
88f0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8900: 45 5f 42 55 53 59 20 20 20 20 20 20 20 20 20 35 E_BUSY 5
8910: 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
8920: 73 65 20 66 69 6c 65 20 69 73 20 6c 6f 63 6b 65 se file is locke
8930: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
8940: 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20 20 20 20 ITE_LOCKED
8950: 20 36 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 6 /* A table
8960: 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
8970: 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 is locked */.#de
8980: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 fine SQLITE_NOME
8990: 4d 20 20 20 20 20 20 20 20 37 20 20 20 2f 2a 20 M 7 /*
89a0: 41 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 A malloc() faile
89b0: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
89c0: 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 ITE_READONLY
89d0: 20 38 20 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 8 /* Attempt
89e0: 74 6f 20 77 72 69 74 65 20 61 20 72 65 61 64 6f to write a reado
89f0: 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a nly database */.
8a00: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8a10: 4e 54 45 52 52 55 50 54 20 20 20 20 39 20 20 20 NTERRUPT 9
8a20: 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 74 65 72 /* Operation ter
8a30: 6d 69 6e 61 74 65 64 20 62 79 20 73 71 6c 69 74 minated by sqlit
8a40: 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 2a 2f e3_interrupt()*/
8a50: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8a60: 49 4f 45 52 52 20 20 20 20 20 20 20 31 30 20 20 IOERR 10
8a70: 20 2f 2a 20 53 6f 6d 65 20 6b 69 6e 64 20 6f 66 /* Some kind of
8a80: 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 disk I/O error
8a90: 6f 63 63 75 72 72 65 64 20 2a 2f 0a 23 64 65 66 occurred */.#def
8aa0: 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 ine SQLITE_CORRU
8ab0: 50 54 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54 PT 11 /* T
8ac0: 68 65 20 64 61 74 61 62 61 73 65 20 64 69 73 6b he database disk
8ad0: 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 image is malfor
8ae0: 6d 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 med */.#define S
8af0: 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 QLITE_NOTFOUND
8b00: 20 20 31 32 20 20 20 2f 2a 20 4e 4f 54 20 55 53 12 /* NOT US
8b10: 45 44 2e 20 54 61 62 6c 65 20 6f 72 20 72 65 63 ED. Table or rec
8b20: 6f 72 64 20 6e 6f 74 20 66 6f 75 6e 64 20 2a 2f ord not found */
8b30: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8b40: 46 55 4c 4c 20 20 20 20 20 20 20 20 31 33 20 20 FULL 13
8b50: 20 2f 2a 20 49 6e 73 65 72 74 69 6f 6e 20 66 61 /* Insertion fa
8b60: 69 6c 65 64 20 62 65 63 61 75 73 65 20 64 61 74 iled because dat
8b70: 61 62 61 73 65 20 69 73 20 66 75 6c 6c 20 2a 2f abase is full */
8b80: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8b90: 43 41 4e 54 4f 50 45 4e 20 20 20 20 31 34 20 20 CANTOPEN 14
8ba0: 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 70 /* Unable to op
8bb0: 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
8bc0: 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 file */.#define
8bd0: 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 SQLITE_PROTOCOL
8be0: 20 20 20 31 35 20 20 20 2f 2a 20 4e 4f 54 20 55 15 /* NOT U
8bf0: 53 45 44 2e 20 44 61 74 61 62 61 73 65 20 6c 6f SED. Database lo
8c00: 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f ck protocol erro
8c10: 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c r */.#define SQL
8c20: 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20 20 20 ITE_EMPTY
8c30: 31 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 16 /* Database
8c40: 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65 is empty */.#de
8c50: 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 43 48 45 fine SQLITE_SCHE
8c60: 4d 41 20 20 20 20 20 20 31 37 20 20 20 2f 2a 20 MA 17 /*
8c70: 54 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 The database sch
8c80: 65 6d 61 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 ema changed */.#
8c90: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 4f define SQLITE_TO
8ca0: 4f 42 49 47 20 20 20 20 20 20 31 38 20 20 20 2f OBIG 18 /
8cb0: 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 * String or BLOB
8cc0: 20 65 78 63 65 65 64 73 20 73 69 7a 65 20 6c 69 exceeds size li
8cd0: 6d 69 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 mit */.#define S
8ce0: 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
8cf0: 20 20 31 39 20 20 20 2f 2a 20 41 62 6f 72 74 20 19 /* Abort
8d00: 64 75 65 20 74 6f 20 63 6f 6e 73 74 72 61 69 6e due to constrain
8d10: 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f 0a 23 t violation */.#
8d20: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 define SQLITE_MI
8d30: 53 4d 41 54 43 48 20 20 20 20 32 30 20 20 20 2f SMATCH 20 /
8d40: 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69 73 6d * Data type mism
8d50: 61 74 63 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 atch */.#define
8d60: 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 20 20 SQLITE_MISUSE
8d70: 20 20 20 32 31 20 20 20 2f 2a 20 4c 69 62 72 61 21 /* Libra
8d80: 72 79 20 75 73 65 64 20 69 6e 63 6f 72 72 65 63 ry used incorrec
8d90: 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 tly */.#define S
8da0: 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20 20 20 QLITE_NOLFS
8db0: 20 20 32 32 20 20 20 2f 2a 20 55 73 65 73 20 4f 22 /* Uses O
8dc0: 53 20 66 65 61 74 75 72 65 73 20 6e 6f 74 20 73 S features not s
8dd0: 75 70 70 6f 72 74 65 64 20 6f 6e 20 68 6f 73 74 upported on host
8de0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8df0: 54 45 5f 41 55 54 48 20 20 20 20 20 20 20 20 32 TE_AUTH 2
8e00: 33 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 3 /* Authoriza
8e10: 74 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23 tion denied */.#
8e20: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 4f define SQLITE_FO
8e30: 52 4d 41 54 20 20 20 20 20 20 32 34 20 20 20 2f RMAT 24 /
8e40: 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61 74 61 * Auxiliary data
8e50: 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72 72 6f base format erro
8e60: 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c r */.#define SQL
8e70: 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20 20 20 ITE_RANGE
8e80: 32 35 20 20 20 2f 2a 20 32 6e 64 20 70 61 72 61 25 /* 2nd para
8e90: 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
8ea0: 5f 62 69 6e 64 20 6f 75 74 20 6f 66 20 72 61 6e _bind out of ran
8eb0: 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ge */.#define SQ
8ec0: 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20 20 20 LITE_NOTADB
8ed0: 20 32 36 20 20 20 2f 2a 20 46 69 6c 65 20 6f 70 26 /* File op
8ee0: 65 6e 65 64 20 74 68 61 74 20 69 73 20 6e 6f 74 ened that is not
8ef0: 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
8f00: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8f10: 54 45 5f 52 4f 57 20 20 20 20 20 20 20 20 20 31 TE_ROW 1
8f20: 30 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 00 /* sqlite3_s
8f30: 74 65 70 28 29 20 68 61 73 20 61 6e 6f 74 68 65 tep() has anothe
8f40: 72 20 72 6f 77 20 72 65 61 64 79 20 2a 2f 0a 23 r row ready */.#
8f50: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 4f define SQLITE_DO
8f60: 4e 45 20 20 20 20 20 20 20 20 31 30 31 20 20 2f NE 101 /
8f70: 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 * sqlite3_step()
8f80: 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 65 78 has finished ex
8f90: 65 63 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20 65 6e ecuting */./* en
8fa0: 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 d-of-error-codes
8fb0: 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 */../*.** CAPI3
8fc0: 52 45 46 3a 20 45 78 74 65 6e 64 65 64 20 52 65 REF: Extended Re
8fd0: 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31 30 32 sult Codes {H102
8fe0: 32 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 20} <S10700>.**
8ff0: 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e KEYWORDS: {exten
9000: 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 7d 20 ded error code}
9010: 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 {extended error
9020: 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 codes}.** KEYWOR
9030: 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 72 65 DS: {extended re
9040: 73 75 6c 74 20 63 6f 64 65 7d 20 7b 65 78 74 65 sult code} {exte
9050: 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 nded result code
9060: 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74 73 20 s}.**.** In its
9070: 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 default configur
9080: 61 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 41 50 ation, SQLite AP
9090: 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 I routines retur
90a0: 6e 20 6f 6e 65 20 6f 66 20 32 36 20 69 6e 74 65 n one of 26 inte
90b0: 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f ger.** [SQLITE_O
90c0: 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 K | result codes
90d0: 5d 2e 20 20 48 6f 77 65 76 65 72 2c 20 65 78 70 ]. However, exp
90e0: 65 72 69 65 6e 63 65 20 68 61 73 20 73 68 6f 77 erience has show
90f0: 6e 20 74 68 61 74 20 6d 61 6e 79 20 6f 66 0a 2a n that many of.*
9100: 2a 20 74 68 65 73 65 20 72 65 73 75 6c 74 20 63 * these result c
9110: 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63 6f 61 odes are too coa
9120: 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20 54 68 rse-grained. Th
9130: 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64 ey do not provid
9140: 65 20 61 73 0a 2a 2a 20 6d 75 63 68 20 69 6e 66 e as.** much inf
9150: 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 70 ormation about p
9160: 72 6f 62 6c 65 6d 73 20 61 73 20 70 72 6f 67 72 roblems as progr
9170: 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 6c 69 6b ammers might lik
9180: 65 2e 20 20 49 6e 20 61 6e 20 65 66 66 6f 72 74 e. In an effort
9190: 20 74 6f 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 to.** address t
91a0: 68 69 73 2c 20 6e 65 77 65 72 20 76 65 72 73 69 his, newer versi
91b0: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 28 76 ons of SQLite (v
91c0: 65 72 73 69 6f 6e 20 33 2e 33 2e 38 20 61 6e 64 ersion 3.3.8 and
91d0: 20 6c 61 74 65 72 29 20 69 6e 63 6c 75 64 65 0a later) include.
91e0: 2a 2a 20 73 75 70 70 6f 72 74 20 66 6f 72 20 61 ** support for a
91f0: 64 64 69 74 69 6f 6e 61 6c 20 72 65 73 75 6c 74 dditional result
9200: 20 63 6f 64 65 73 20 74 68 61 74 20 70 72 6f 76 codes that prov
9210: 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 ide more detaile
9220: 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a d information.**
9230: 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 20 54 about errors. T
9240: 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 he extended resu
9250: 6c 74 20 63 6f 64 65 73 20 61 72 65 20 65 6e 61 lt codes are ena
9260: 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 bled or disabled
9270: 0a 2a 2a 20 6f 6e 20 61 20 70 65 72 20 64 61 74 .** on a per dat
9280: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
9290: 20 62 61 73 69 73 20 75 73 69 6e 67 20 74 68 65 basis using the
92a0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 .** [sqlite3_ext
92b0: 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 ended_result_cod
92c0: 65 73 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a 2a 2a es()] API..**.**
92d0: 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 76 61 Some of the ava
92e0: 69 6c 61 62 6c 65 20 65 78 74 65 6e 64 65 64 20 ilable extended
92f0: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 result codes are
9300: 20 6c 69 73 74 65 64 20 68 65 72 65 2e 0a 2a 2a listed here..**
9310: 20 4f 6e 65 20 6d 61 79 20 65 78 70 65 63 74 20 One may expect
9320: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 78 the number of ex
9330: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
9340: 64 65 73 20 77 69 6c 6c 20 62 65 20 65 78 70 61 des will be expa
9350: 6e 64 0a 2a 2a 20 6f 76 65 72 20 74 69 6d 65 2e nd.** over time.
9360: 20 20 53 6f 66 74 77 61 72 65 20 74 68 61 74 20 Software that
9370: 75 73 65 73 20 65 78 74 65 6e 64 65 64 20 72 65 uses extended re
9380: 73 75 6c 74 20 63 6f 64 65 73 20 73 68 6f 75 6c sult codes shoul
9390: 64 20 65 78 70 65 63 74 0a 2a 2a 20 74 6f 20 73 d expect.** to s
93a0: 65 65 20 6e 65 77 20 72 65 73 75 6c 74 20 63 6f ee new result co
93b0: 64 65 73 20 69 6e 20 66 75 74 75 72 65 20 72 65 des in future re
93c0: 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 leases of SQLite
93d0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ..**.** The SQLI
93e0: 54 45 5f 4f 4b 20 72 65 73 75 6c 74 20 63 6f 64 TE_OK result cod
93f0: 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 e will never be
9400: 65 78 74 65 6e 64 65 64 2e 20 20 49 74 20 77 69 extended. It wi
9410: 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 ll always.** be
9420: 65 78 61 63 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a exactly zero..**
9430: 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
9440: 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 33 7d 20 54 **.** {H10223} T
9450: 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 he symbolic name
9460: 20 66 6f 72 20 61 6e 20 65 78 74 65 6e 64 65 64 for an extended
9470: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 73 68 61 result code sha
9480: 6c 6c 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20 ll contains.**
9490: 20 20 20 20 20 20 20 20 61 20 72 65 6c 61 74 65 a relate
94a0: 64 20 70 72 69 6d 61 72 79 20 72 65 73 75 6c 74 d primary result
94b0: 20 63 6f 64 65 20 61 73 20 61 20 70 72 65 66 69 code as a prefi
94c0: 78 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 34 x..**.** {H10224
94d0: 7d 20 50 72 69 6d 61 72 79 20 72 65 73 75 6c 74 } Primary result
94e0: 20 63 6f 64 65 20 6e 61 6d 65 73 20 73 68 61 6c code names shal
94f0: 6c 20 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 l contain a sing
9500: 6c 65 20 22 5f 22 20 63 68 61 72 61 63 74 65 72 le "_" character
9510: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 35 7d ..**.** {H10225}
9520: 20 45 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 Extended result
9530: 20 63 6f 64 65 20 6e 61 6d 65 73 20 73 68 61 6c code names shal
9540: 6c 20 63 6f 6e 74 61 69 6e 20 74 77 6f 20 6f 72 l contain two or
9550: 20 6d 6f 72 65 20 22 5f 22 20 63 68 61 72 61 63 more "_" charac
9560: 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 ters..**.** {H10
9570: 32 32 36 7d 20 54 68 65 20 6e 75 6d 65 72 69 63 226} The numeric
9580: 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 65 78 74 value of an ext
9590: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 ended result cod
95a0: 65 20 73 68 61 6c 6c 20 63 6f 6e 74 61 69 6e 20 e shall contain
95b0: 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
95c0: 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 6f 66 numeric value of
95d0: 20 69 74 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 its correspondi
95e0: 6e 67 20 70 72 69 6d 61 72 79 20 72 65 73 75 6c ng primary resul
95f0: 74 20 63 6f 64 65 20 69 6e 0a 2a 2a 20 20 20 20 t code in.**
9600: 20 20 20 20 20 20 69 74 73 20 6c 65 61 73 74 20 its least
9610: 73 69 67 6e 69 66 69 63 61 6e 74 20 38 20 62 69 significant 8 bi
9620: 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ts..*/.#define S
9630: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 QLITE_IOERR_READ
9640: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 (S
9650: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 QLITE_IOERR | (1
9660: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 <<8)).#define SQ
9670: 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
9680: 5f 52 45 41 44 20 20 20 20 20 20 20 20 28 53 51 _READ (SQ
9690: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 32 3c LITE_IOERR | (2<
96a0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
96b0: 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 ITE_IOERR_WRITE
96c0: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
96d0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c 3c ITE_IOERR | (3<<
96e0: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8)).#define SQLI
96f0: 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20 20 TE_IOERR_FSYNC
9700: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 (SQLI
9710: 54 45 5f 49 4f 45 52 52 20 7c 20 28 34 3c 3c 38 TE_IOERR | (4<<8
9720: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
9730: 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e E_IOERR_DIR_FSYN
9740: 43 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 C (SQLIT
9750: 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c 3c 38 29 E_IOERR | (5<<8)
9760: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
9770: 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 _IOERR_TRUNCATE
9780: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
9790: 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c 38 29 29 _IOERR | (6<<8))
97a0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
97b0: 49 4f 45 52 52 5f 46 53 54 41 54 20 20 20 20 20 IOERR_FSTAT
97c0: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
97d0: 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29 29 0a IOERR | (7<<8)).
97e0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
97f0: 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 OERR_UNLOCK
9800: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
9810: 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29 29 0a 23 OERR | (8<<8)).#
9820: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f define SQLITE_IO
9830: 45 52 52 5f 52 44 4c 4f 43 4b 20 20 20 20 20 20 ERR_RDLOCK
9840: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f (SQLITE_IO
9850: 45 52 52 20 7c 20 28 39 3c 3c 38 29 29 0a 23 64 ERR | (9<<8)).#d
9860: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
9870: 52 52 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 RR_DELETE
9880: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
9890: 52 52 20 7c 20 28 31 30 3c 3c 38 29 29 0a 23 64 RR | (10<<8)).#d
98a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
98b0: 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20 20 20 20 RR_BLOCKED
98c0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
98d0: 52 52 20 7c 20 28 31 31 3c 3c 38 29 29 0a 23 64 RR | (11<<8)).#d
98e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
98f0: 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 RR_NOMEM
9900: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
9910: 52 52 20 7c 20 28 31 32 3c 3c 38 29 29 0a 23 64 RR | (12<<8)).#d
9920: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
9930: 52 52 5f 41 43 43 45 53 53 20 20 20 20 20 20 20 RR_ACCESS
9940: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
9950: 52 52 20 7c 20 28 31 33 3c 3c 38 29 29 0a 23 64 RR | (13<<8)).#d
9960: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
9970: 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 RR_CHECKRESERVED
9980: 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f 49 4f 45 LOCK (SQLITE_IOE
9990: 52 52 20 7c 20 28 31 34 3c 3c 38 29 29 0a 23 64 RR | (14<<8)).#d
99a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
99b0: 52 52 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 RR_LOCK
99c0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
99d0: 52 52 20 7c 20 28 31 35 3c 3c 38 29 29 0a 23 64 RR | (15<<8)).#d
99e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
99f0: 52 52 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20 RR_CLOSE
9a00: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
9a10: 52 52 20 7c 20 28 31 36 3c 3c 38 29 29 0a 23 64 RR | (16<<8)).#d
9a20: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
9a30: 52 52 5f 44 49 52 5f 43 4c 4f 53 45 20 20 20 20 RR_DIR_CLOSE
9a40: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
9a50: 52 52 20 7c 20 28 31 37 3c 3c 38 29 29 0a 0a 2f RR | (17<<8))../
9a60: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 *.** CAPI3REF: F
9a70: 6c 61 67 73 20 46 6f 72 20 46 69 6c 65 20 4f 70 lags For File Op
9a80: 65 6e 20 4f 70 65 72 61 74 69 6f 6e 73 20 7b 48 en Operations {H
9a90: 31 30 32 33 30 7d 20 3c 48 31 31 31 32 30 3e 20 10230} <H11120>
9aa0: 3c 48 31 32 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <H12700>.**.** T
9ab0: 68 65 73 65 20 62 69 74 20 76 61 6c 75 65 73 20 hese bit values
9ac0: 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 are intended for
9ad0: 20 75 73 65 20 69 6e 20 74 68 65 0a 2a 2a 20 33 use in the.** 3
9ae0: 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 rd parameter to
9af0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 the [sqlite3_ope
9b00: 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 n_v2()] interfac
9b10: 65 20 61 6e 64 0a 2a 2a 20 69 6e 20 74 68 65 20 e and.** in the
9b20: 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 4th parameter to
9b30: 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f the xOpen metho
9b40: 64 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c d of the.** [sql
9b50: 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
9b60: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
9b70: 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
9b80: 59 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 Y 0x0000
9b90: 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 0001.#define SQL
9ba0: 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
9bb0: 54 45 20 20 20 20 20 20 20 20 30 78 30 30 30 30 TE 0x0000
9bc0: 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 0002.#define SQL
9bd0: 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 ITE_OPEN_CREATE
9be0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 0x0000
9bf0: 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 0004.#define SQL
9c00: 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
9c10: 4e 43 4c 4f 53 45 20 20 20 20 30 78 30 30 30 30 NCLOSE 0x0000
9c20: 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 0008.#define SQL
9c30: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
9c40: 56 45 20 20 20 20 20 20 20 20 30 78 30 30 30 30 VE 0x0000
9c50: 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0010.#define SQL
9c60: 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
9c70: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 0x0000
9c80: 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0100.#define SQL
9c90: 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 ITE_OPEN_TEMP_DB
9ca0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 0x0000
9cb0: 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0200.#define SQL
9cc0: 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 ITE_OPEN_TRANSIE
9cd0: 4e 54 5f 44 42 20 20 20 20 20 30 78 30 30 30 30 NT_DB 0x0000
9ce0: 30 34 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0400.#define SQL
9cf0: 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
9d00: 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30 URNAL 0x0000
9d10: 30 38 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0800.#define SQL
9d20: 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f ITE_OPEN_TEMP_JO
9d30: 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30 URNAL 0x0000
9d40: 31 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 1000.#define SQL
9d50: 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 ITE_OPEN_SUBJOUR
9d60: 4e 41 4c 20 20 20 20 20 20 20 30 78 30 30 30 30 NAL 0x0000
9d70: 32 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 2000.#define SQL
9d80: 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f ITE_OPEN_MASTER_
9d90: 4a 4f 55 52 4e 41 4c 20 20 20 30 78 30 30 30 30 JOURNAL 0x0000
9da0: 34 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 4000.#define SQL
9db0: 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 ITE_OPEN_NOMUTEX
9dc0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 0x0000
9dd0: 38 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 8000.#define SQL
9de0: 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 ITE_OPEN_FULLMUT
9df0: 45 58 20 20 20 20 20 20 20 20 30 78 30 30 30 31 EX 0x0001
9e00: 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 0000../*.** CAPI
9e10: 33 52 45 46 3a 20 44 65 76 69 63 65 20 43 68 61 3REF: Device Cha
9e20: 72 61 63 74 65 72 69 73 74 69 63 73 20 7b 48 31 racteristics {H1
9e30: 30 32 34 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a 0240} <H11120>.*
9e40: 2a 0a 2a 2a 20 54 68 65 20 78 44 65 76 69 63 65 *.** The xDevice
9e50: 43 61 70 61 62 69 6c 69 74 69 65 73 20 6d 65 74 Capabilities met
9e60: 68 6f 64 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 hod of the [sqli
9e70: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a te3_io_methods].
9e80: 2a 2a 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e ** object return
9e90: 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 s an integer whi
9ea0: 63 68 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f ch is a vector o
9eb0: 66 20 74 68 65 20 74 68 65 73 65 0a 2a 2a 20 62 f the these.** b
9ec0: 69 74 20 76 61 6c 75 65 73 20 65 78 70 72 65 73 it values expres
9ed0: 73 69 6e 67 20 49 2f 4f 20 63 68 61 72 61 63 74 sing I/O charact
9ee0: 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 eristics of the
9ef0: 6d 61 73 73 20 73 74 6f 72 61 67 65 0a 2a 2a 20 mass storage.**
9f00: 64 65 76 69 63 65 20 74 68 61 74 20 68 6f 6c 64 device that hold
9f10: 73 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 s the file that
9f20: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f the [sqlite3_io_
9f30: 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 72 65 66 65 methods].** refe
9f40: 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rs to..**.** The
9f50: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
9f60: 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d 65 OMIC property me
9f70: 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72 69 ans that all wri
9f80: 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 tes of.** any si
9f90: 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 ze are atomic.
9fa0: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 The SQLITE_IOCAP
9fb0: 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 _ATOMICnnn value
9fc0: 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 s.** mean that w
9fd0: 72 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 rites of blocks
9fe0: 74 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 that are nnn byt
9ff0: 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a es in size and.*
a000: 2a 20 61 72 65 20 61 6c 69 67 6e 65 64 20 74 6f * are aligned to
a010: 20 61 6e 20 61 64 64 72 65 73 73 20 77 68 69 63 an address whic
a020: 68 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 h is an integer
a030: 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e multiple of.** n
a040: 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 nn are atomic.
a050: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 The SQLITE_IOCAP
a060: 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 76 61 6c _SAFE_APPEND val
a070: 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 ue means.** that
a080: 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 61 70 when data is ap
a090: 70 65 6e 64 65 64 20 74 6f 20 61 20 66 69 6c 65 pended to a file
a0a0: 2c 20 74 68 65 20 64 61 74 61 20 69 73 20 61 70 , the data is ap
a0b0: 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 pended.** first
a0c0: 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 then the size of
a0d0: 20 74 68 65 20 66 69 6c 65 20 69 73 20 65 78 74 the file is ext
a0e0: 65 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 68 65 ended, never the
a0f0: 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 other.** way ar
a100: 6f 75 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 54 ound. The SQLIT
a110: 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 E_IOCAP_SEQUENTI
a120: 41 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e AL property mean
a130: 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d s that.** inform
a140: 61 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e ation is written
a150: 20 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 20 to disk in the
a160: 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20 63 61 same order as ca
a170: 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 lls.** to xWrite
a180: 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ()..*/.#define S
a190: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a1a0: 49 43 20 20 20 20 20 20 20 20 20 20 30 78 30 30 IC 0x00
a1b0: 30 30 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53 000001.#define S
a1c0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a1d0: 49 43 35 31 32 20 20 20 20 20 20 20 30 78 30 30 IC512 0x00
a1e0: 30 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 000002.#define S
a1f0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a200: 49 43 31 4b 20 20 20 20 20 20 20 20 30 78 30 30 IC1K 0x00
a210: 30 30 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53 000004.#define S
a220: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a230: 49 43 32 4b 20 20 20 20 20 20 20 20 30 78 30 30 IC2K 0x00
a240: 30 30 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53 000008.#define S
a250: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a260: 49 43 34 4b 20 20 20 20 20 20 20 20 30 78 30 30 IC4K 0x00
a270: 30 30 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53 000010.#define S
a280: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a290: 49 43 38 4b 20 20 20 20 20 20 20 20 30 78 30 30 IC8K 0x00
a2a0: 30 30 30 30 32 30 0a 23 64 65 66 69 6e 65 20 53 000020.#define S
a2b0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a2c0: 49 43 31 36 4b 20 20 20 20 20 20 20 30 78 30 30 IC16K 0x00
a2d0: 30 30 30 30 34 30 0a 23 64 65 66 69 6e 65 20 53 000040.#define S
a2e0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a2f0: 49 43 33 32 4b 20 20 20 20 20 20 20 30 78 30 30 IC32K 0x00
a300: 30 30 30 30 38 30 0a 23 64 65 66 69 6e 65 20 53 000080.#define S
a310: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a320: 49 43 36 34 4b 20 20 20 20 20 20 20 30 78 30 30 IC64K 0x00
a330: 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 000100.#define S
a340: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 QLITE_IOCAP_SAFE
a350: 5f 41 50 50 45 4e 44 20 20 20 20 20 30 78 30 30 _APPEND 0x00
a360: 30 30 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53 000200.#define S
a370: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 QLITE_IOCAP_SEQU
a380: 45 4e 54 49 41 4c 20 20 20 20 20 20 30 78 30 30 ENTIAL 0x00
a390: 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 000400../*.** CA
a3a0: 50 49 33 52 45 46 3a 20 46 69 6c 65 20 4c 6f 63 PI3REF: File Loc
a3b0: 6b 69 6e 67 20 4c 65 76 65 6c 73 20 7b 48 31 30 king Levels {H10
a3c0: 32 35 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48 250} <H11120> <H
a3d0: 31 31 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 11310>.**.** SQL
a3e0: 69 74 65 20 75 73 65 73 20 6f 6e 65 20 6f 66 20 ite uses one of
a3f0: 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 these integer va
a400: 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63 6f lues as the seco
a410: 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 nd.** argument t
a420: 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65 73 o calls it makes
a430: 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29 20 to the xLock()
a440: 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d 65 and xUnlock() me
a450: 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 5b thods.** of an [
a460: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
a470: 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 ds] object..*/.#
a480: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f define SQLITE_LO
a490: 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20 20 CK_NONE
a4a0: 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 0.#define SQLIT
a4b0: 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20 20 E_LOCK_SHARED
a4c0: 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 1.#define S
a4d0: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 QLITE_LOCK_RESER
a4e0: 56 45 44 20 20 20 20 20 20 32 0a 23 64 65 66 69 VED 2.#defi
a4f0: 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 ne SQLITE_LOCK_P
a500: 45 4e 44 49 4e 47 20 20 20 20 20 20 20 33 0a 23 ENDING 3.#
a510: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f define SQLITE_LO
a520: 43 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 CK_EXCLUSIVE
a530: 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 4../*.** CAPI3R
a540: 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61 74 EF: Synchronizat
a550: 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 73 20 7b ion Type Flags {
a560: 48 31 30 32 36 30 7d 20 3c 48 31 31 31 32 30 3e H10260} <H11120>
a570: 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 .**.** When SQLi
a580: 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 te invokes the x
a590: 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 Sync() method of
a5a0: 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f an.** [sqlite3_
a5b0: 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 io_methods] obje
a5c0: 63 74 20 69 74 20 75 73 65 73 20 61 20 63 6f 6d ct it uses a com
a5d0: 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 bination of.** t
a5e0: 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c hese integer val
a5f0: 75 65 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e ues as the secon
a600: 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a d argument..**.*
a610: 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 * When the SQLIT
a620: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 E_SYNC_DATAONLY
a630: 66 6c 61 67 20 69 73 20 75 73 65 64 2c 20 69 74 flag is used, it
a640: 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a means that the.
a650: 2a 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f ** sync operatio
a660: 6e 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 n only needs to
a670: 66 6c 75 73 68 20 64 61 74 61 20 74 6f 20 6d 61 flush data to ma
a680: 73 73 20 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f ss storage. Ino
a690: 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f de.** informatio
a6a0: 6e 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 66 6c n need not be fl
a6b0: 75 73 68 65 64 2e 20 54 68 65 20 53 51 4c 49 54 ushed. The SQLIT
a6c0: 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 66 6c E_SYNC_NORMAL fl
a6d0: 61 67 20 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 ag means.** to u
a6e0: 73 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 se normal fsync(
a6f0: 29 20 73 65 6d 61 6e 74 69 63 73 2e 20 54 68 65 ) semantics. The
a700: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c SQLITE_SYNC_FUL
a710: 4c 20 66 6c 61 67 20 6d 65 61 6e 73 0a 2a 2a 20 L flag means.**
a720: 74 6f 20 75 73 65 20 4d 61 63 20 4f 53 20 58 20 to use Mac OS X
a730: 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 20 69 style fullsync i
a740: 6e 73 74 65 61 64 20 6f 66 20 66 73 79 6e 63 28 nstead of fsync(
a750: 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 )..*/.#define SQ
a760: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
a770: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 32 0a 0x00002.
a780: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
a790: 59 4e 43 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 YNC_FULL
a7a0: 20 20 30 78 30 30 30 30 33 0a 23 64 65 66 69 6e 0x00003.#defin
a7b0: 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 e SQLITE_SYNC_DA
a7c0: 54 41 4f 4e 4c 59 20 20 20 20 20 20 30 78 30 30 TAONLY 0x00
a7d0: 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 010../*.** CAPI3
a7e0: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 REF: OS Interfac
a7f0: 65 20 4f 70 65 6e 20 46 69 6c 65 20 48 61 6e 64 e Open File Hand
a800: 6c 65 20 7b 48 31 31 31 31 30 7d 20 3c 53 32 30 le {H11110} <S20
a810: 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 5b 73 110>.**.** An [s
a820: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a qlite3_file] obj
a830: 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 ect represents a
a840: 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69 6e 20 74 n open file in t
a850: 68 65 20 4f 53 0a 2a 2a 20 69 6e 74 65 72 66 61 he OS.** interfa
a860: 63 65 20 6c 61 79 65 72 2e 20 20 49 6e 64 69 76 ce layer. Indiv
a870: 69 64 75 61 6c 20 4f 53 20 69 6e 74 65 72 66 61 idual OS interfa
a880: 63 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ce implementatio
a890: 6e 73 20 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 ns will.** want
a8a0: 74 6f 20 73 75 62 63 6c 61 73 73 20 74 68 69 73 to subclass this
a8b0: 20 6f 62 6a 65 63 74 20 62 79 20 61 70 70 65 6e object by appen
a8c0: 64 69 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 ding additional
a8d0: 66 69 65 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68 fields.** for th
a8e0: 65 69 72 20 6f 77 6e 20 75 73 65 2e 20 20 54 68 eir own use. Th
a8f0: 65 20 70 4d 65 74 68 6f 64 73 20 65 6e 74 72 79 e pMethods entry
a900: 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
a910: 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f an.** [sqlite3_
a920: 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 io_methods] obje
a930: 63 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 ct that defines
a940: 6d 65 74 68 6f 64 73 20 66 6f 72 20 70 65 72 66 methods for perf
a950: 6f 72 6d 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 orming.** I/O op
a960: 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 erations on the
a970: 6f 70 65 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 open file..*/.ty
a980: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
a990: 69 74 65 33 5f 66 69 6c 65 20 73 71 6c 69 74 65 ite3_file sqlite
a9a0: 33 5f 66 69 6c 65 3b 0a 73 74 72 75 63 74 20 73 3_file;.struct s
a9b0: 71 6c 69 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20 qlite3_file {.
a9c0: 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c const struct sql
a9d0: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
a9e0: 2a 70 4d 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d *pMethods; /* M
a9f0: 65 74 68 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70 ethods for an op
aa00: 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f en file */.};../
aa10: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f *.** CAPI3REF: O
aa20: 53 20 49 6e 74 65 72 66 61 63 65 20 46 69 6c 65 S Interface File
aa30: 20 56 69 72 74 75 61 6c 20 4d 65 74 68 6f 64 73 Virtual Methods
aa40: 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 32 30 7d Object {H11120}
aa50: 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 <S20110>.**.**
aa60: 45 76 65 72 79 20 66 69 6c 65 20 6f 70 65 6e 65 Every file opene
aa70: 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 d by the [sqlite
aa80: 33 5f 76 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74 3_vfs] xOpen met
aa90: 68 6f 64 20 70 6f 70 75 6c 61 74 65 73 20 61 6e hod populates an
aaa0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c .** [sqlite3_fil
aab0: 65 5d 20 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d e] object (or, m
aac0: 6f 72 65 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 ore commonly, a
aad0: 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 0a subclass of the.
aae0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 ** [sqlite3_file
aaf0: 5d 20 6f 62 6a 65 63 74 29 20 77 69 74 68 20 61 ] object) with a
ab00: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 pointer to an i
ab10: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
ab20: 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20 object..** This
ab30: 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 object defines t
ab40: 68 65 20 6d 65 74 68 6f 64 73 20 75 73 65 64 20 he methods used
ab50: 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f to perform vario
ab60: 75 73 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a us operations.**
ab70: 20 61 67 61 69 6e 73 74 20 74 68 65 20 6f 70 65 against the ope
ab80: 6e 20 66 69 6c 65 20 72 65 70 72 65 73 65 6e 74 n file represent
ab90: 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 ed by the [sqlit
aba0: 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e e3_file] object.
abb0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 .**.** The flags
abc0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 79 argument to xSy
abd0: 6e 63 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 nc may be one of
abe0: 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f [SQLITE_SYNC_NO
abf0: 52 4d 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c RMAL] or.** [SQL
ac00: 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 ITE_SYNC_FULL].
ac10: 20 54 68 65 20 66 69 72 73 74 20 63 68 6f 69 63 The first choic
ac20: 65 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 e is the normal
ac30: 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 54 68 65 20 fsync()..** The
ac40: 73 65 63 6f 6e 64 20 63 68 6f 69 63 65 20 69 73 second choice is
ac50: 20 61 20 4d 61 63 20 4f 53 20 58 20 73 74 79 6c a Mac OS X styl
ac60: 65 20 66 75 6c 6c 73 79 6e 63 2e 20 20 54 68 65 e fullsync. The
ac70: 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 [SQLITE_SYNC_DA
ac80: 54 41 4f 4e 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 TAONLY].** flag
ac90: 6d 61 79 20 62 65 20 4f 52 65 64 20 69 6e 20 74 may be ORed in t
aca0: 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 o indicate that
acb0: 6f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 6f 66 only the data of
acc0: 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 the file.** and
acd0: 20 6e 6f 74 20 69 74 73 20 69 6e 6f 64 65 20 6e not its inode n
ace0: 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 65 eeds to be synce
acf0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 d..**.** The int
ad00: 65 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20 78 eger values to x
ad10: 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f Lock() and xUnlo
ad20: 63 6b 28 29 20 61 72 65 20 6f 6e 65 20 6f 66 0a ck() are one of.
ad30: 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
ad40: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e [SQLITE_LOCK_NON
ad50: 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c E],.** <li> [SQL
ad60: 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d ITE_LOCK_SHARED]
ad70: 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ,.** <li> [SQLIT
ad80: 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d E_LOCK_RESERVED]
ad90: 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ,.** <li> [SQLIT
ada0: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c E_LOCK_PENDING],
adb0: 20 6f 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c or.** <li> [SQL
adc0: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 ITE_LOCK_EXCLUSI
add0: 56 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a VE]..** </ul>.**
ade0: 20 78 4c 6f 63 6b 28 29 20 69 6e 63 72 65 61 73 xLock() increas
adf0: 65 73 20 74 68 65 20 6c 6f 63 6b 2e 20 78 55 6e es the lock. xUn
ae00: 6c 6f 63 6b 28 29 20 64 65 63 72 65 61 73 65 73 lock() decreases
ae10: 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 the lock..** Th
ae20: 65 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 e xCheckReserved
ae30: 4c 6f 63 6b 28 29 20 6d 65 74 68 6f 64 20 63 68 Lock() method ch
ae40: 65 63 6b 73 20 77 68 65 74 68 65 72 20 61 6e 79 ecks whether any
ae50: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
ae60: 74 69 6f 6e 2c 0a 2a 2a 20 65 69 74 68 65 72 20 tion,.** either
ae70: 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 in this process
ae80: 6f 72 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 or in some other
ae90: 20 70 72 6f 63 65 73 73 2c 20 69 73 20 68 6f 6c process, is hol
aea0: 64 69 6e 67 20 61 20 52 45 53 45 52 56 45 44 2c ding a RESERVED,
aeb0: 0a 2a 2a 20 50 45 4e 44 49 4e 47 2c 20 6f 72 20 .** PENDING, or
aec0: 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f EXCLUSIVE lock o
aed0: 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 49 74 20 n the file. It
aee0: 72 65 74 75 72 6e 73 20 74 72 75 65 0a 2a 2a 20 returns true.**
aef0: 69 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 65 if such a lock e
af00: 78 69 73 74 73 20 61 6e 64 20 66 61 6c 73 65 20 xists and false
af10: 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a otherwise..**.**
af20: 20 54 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f The xFileContro
af30: 6c 28 29 20 6d 65 74 68 6f 64 20 69 73 20 61 20 l() method is a
af40: 67 65 6e 65 72 69 63 20 69 6e 74 65 72 66 61 63 generic interfac
af50: 65 20 74 68 61 74 20 61 6c 6c 6f 77 73 20 63 75 e that allows cu
af60: 73 74 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d 70 6c stom.** VFS impl
af70: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f 20 64 ementations to d
af80: 69 72 65 63 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 irectly control
af90: 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 75 73 69 an open file usi
afa0: 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 ng the.** [sqlit
afb0: 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 e3_file_control(
afc0: 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54 )] interface. T
afd0: 68 65 20 73 65 63 6f 6e 64 20 22 6f 70 22 20 61 he second "op" a
afe0: 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 0a 2a 2a rgument is an.**
aff0: 20 69 6e 74 65 67 65 72 20 6f 70 63 6f 64 65 2e integer opcode.
b000: 20 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 The third argu
b010: 6d 65 6e 74 20 69 73 20 61 20 67 65 6e 65 72 69 ment is a generi
b020: 63 20 70 6f 69 6e 74 65 72 20 69 6e 74 65 6e 64 c pointer intend
b030: 65 64 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 ed to.** point t
b040: 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 o a structure th
b050: 61 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 at may contain a
b060: 72 67 75 6d 65 6e 74 73 20 6f 72 20 73 70 61 63 rguments or spac
b070: 65 20 69 6e 20 77 68 69 63 68 20 74 6f 0a 2a 2a e in which to.**
b080: 20 77 72 69 74 65 20 72 65 74 75 72 6e 20 76 61 write return va
b090: 6c 75 65 73 2e 20 20 50 6f 74 65 6e 74 69 61 6c lues. Potential
b0a0: 20 75 73 65 73 20 66 6f 72 20 78 46 69 6c 65 43 uses for xFileC
b0b0: 6f 6e 74 72 6f 6c 28 29 20 6d 69 67 68 74 20 62 ontrol() might b
b0c0: 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 e.** functions t
b0d0: 6f 20 65 6e 61 62 6c 65 20 62 6c 6f 63 6b 69 6e o enable blockin
b0e0: 67 20 6c 6f 63 6b 73 20 77 69 74 68 20 74 69 6d g locks with tim
b0f0: 65 6f 75 74 73 2c 20 74 6f 20 63 68 61 6e 67 65 eouts, to change
b100: 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 the.** locking
b110: 73 74 72 61 74 65 67 79 20 28 66 6f 72 20 65 78 strategy (for ex
b120: 61 6d 70 6c 65 20 74 6f 20 75 73 65 20 64 6f 74 ample to use dot
b130: 2d 66 69 6c 65 20 6c 6f 63 6b 73 29 2c 20 74 6f -file locks), to
b140: 20 69 6e 71 75 69 72 65 0a 2a 2a 20 61 62 6f 75 inquire.** abou
b150: 74 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 t the status of
b160: 61 20 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20 62 72 a lock, or to br
b170: 65 61 6b 20 73 74 61 6c 65 20 6c 6f 63 6b 73 2e eak stale locks.
b180: 20 20 54 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20 The SQLite.**
b190: 63 6f 72 65 20 72 65 73 65 72 76 65 73 20 61 6c core reserves al
b1a0: 6c 20 6f 70 63 6f 64 65 73 20 6c 65 73 73 20 74 l opcodes less t
b1b0: 68 61 6e 20 31 30 30 20 66 6f 72 20 69 74 73 20 han 100 for its
b1c0: 6f 77 6e 20 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 own use..** A [S
b1d0: 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b QLITE_FCNTL_LOCK
b1e0: 53 54 41 54 45 20 7c 20 6c 69 73 74 20 6f 66 20 STATE | list of
b1f0: 6f 70 63 6f 64 65 73 5d 20 6c 65 73 73 20 74 68 opcodes] less th
b200: 61 6e 20 31 30 30 20 69 73 20 61 76 61 69 6c 61 an 100 is availa
b210: 62 6c 65 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 ble..** Applicat
b220: 69 6f 6e 73 20 74 68 61 74 20 64 65 66 69 6e 65 ions that define
b230: 20 61 20 63 75 73 74 6f 6d 20 78 46 69 6c 65 43 a custom xFileC
b240: 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 73 68 ontrol method sh
b250: 6f 75 6c 64 20 75 73 65 20 6f 70 63 6f 64 65 73 ould use opcodes
b260: 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e .** greater than
b270: 20 31 30 30 20 74 6f 20 61 76 6f 69 64 20 63 6f 100 to avoid co
b280: 6e 66 6c 69 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 nflicts..**.** T
b290: 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 he xSectorSize()
b2a0: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 method returns
b2b0: 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 the sector size
b2c0: 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 69 63 65 of the.** device
b2d0: 20 74 68 61 74 20 75 6e 64 65 72 6c 69 65 73 20 that underlies
b2e0: 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 73 the file. The s
b2f0: 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 74 68 ector size is th
b300: 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77 72 69 e.** minimum wri
b310: 74 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 te that can be p
b320: 65 72 66 6f 72 6d 65 64 20 77 69 74 68 6f 75 74 erformed without
b330: 20 64 69 73 74 75 72 62 69 6e 67 0a 2a 2a 20 6f disturbing.** o
b340: 74 68 65 72 20 62 79 74 65 73 20 69 6e 20 74 68 ther bytes in th
b350: 65 20 66 69 6c 65 2e 20 20 54 68 65 20 78 44 65 e file. The xDe
b360: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
b370: 69 63 73 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 ics().** method
b380: 72 65 74 75 72 6e 73 20 61 20 62 69 74 20 76 65 returns a bit ve
b390: 63 74 6f 72 20 64 65 73 63 72 69 62 69 6e 67 20 ctor describing
b3a0: 62 65 68 61 76 69 6f 72 73 20 6f 66 20 74 68 65 behaviors of the
b3b0: 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 .** underlying d
b3c0: 65 76 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c evice:.**.** <ul
b3d0: 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 >.** <li> [SQLIT
b3e0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 5d 0a E_IOCAP_ATOMIC].
b3f0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
b400: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 5d IOCAP_ATOMIC512]
b410: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
b420: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 5d _IOCAP_ATOMIC1K]
b430: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
b440: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 5d _IOCAP_ATOMIC2K]
b450: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
b460: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 5d _IOCAP_ATOMIC4K]
b470: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
b480: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 5d _IOCAP_ATOMIC8K]
b490: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
b4a0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b _IOCAP_ATOMIC16K
b4b0: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ].** <li> [SQLIT
b4c0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32 E_IOCAP_ATOMIC32
b4d0: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 K].** <li> [SQLI
b4e0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 TE_IOCAP_ATOMIC6
b4f0: 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 4K].** <li> [SQL
b500: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 ITE_IOCAP_SAFE_A
b510: 50 50 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b PPEND].** <li> [
b520: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 SQLITE_IOCAP_SEQ
b530: 55 45 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c UENTIAL].** </ul
b540: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 >.**.** The SQLI
b550: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 TE_IOCAP_ATOMIC
b560: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 property means t
b570: 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f hat all writes o
b580: 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72 f.** any size ar
b590: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 e atomic. The S
b5a0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
b5b0: 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 ICnnn values.**
b5c0: 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74 65 73 mean that writes
b5d0: 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 of blocks that
b5e0: 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e are nnn bytes in
b5f0: 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65 size and.** are
b600: 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61 aligned to an a
b610: 64 64 72 65 73 73 20 77 68 69 63 68 20 69 73 20 ddress which is
b620: 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 an integer multi
b630: 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 ple of.** nnn ar
b640: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 e atomic. The S
b650: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 QLITE_IOCAP_SAFE
b660: 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65 _APPEND value me
b670: 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e ans.** that when
b680: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 data is appende
b690: 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65 d to a file, the
b6a0: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 data is appende
b6b0: 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20 d.** first then
b6c0: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
b6d0: 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64 file is extended
b6e0: 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65 , never the othe
b6f0: 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e r.** way around.
b700: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 The SQLITE_IOC
b710: 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72 AP_SEQUENTIAL pr
b720: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 operty means tha
b730: 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e t.** information
b740: 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 is written to d
b750: 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 isk in the same
b760: 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a order as calls.*
b770: 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a * to xWrite()..*
b780: 2a 0a 2a 2a 20 49 66 20 78 52 65 61 64 28 29 20 *.** If xRead()
b790: 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 returns SQLITE_I
b7a0: 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 OERR_SHORT_READ
b7b0: 69 74 20 6d 75 73 74 20 61 6c 73 6f 20 66 69 6c it must also fil
b7c0: 6c 0a 2a 2a 20 69 6e 20 74 68 65 20 75 6e 72 65 l.** in the unre
b7d0: 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74 ad portions of t
b7e0: 68 65 20 62 75 66 66 65 72 20 77 69 74 68 20 7a he buffer with z
b7f0: 65 72 6f 73 2e 20 20 41 20 56 46 53 20 74 68 61 eros. A VFS tha
b800: 74 0a 2a 2a 20 66 61 69 6c 73 20 74 6f 20 7a 65 t.** fails to ze
b810: 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 ro-fill short re
b820: 61 64 73 20 6d 69 67 68 74 20 73 65 65 6d 20 74 ads might seem t
b830: 6f 20 77 6f 72 6b 2e 20 20 48 6f 77 65 76 65 72 o work. However
b840: 2c 0a 2a 2a 20 66 61 69 6c 75 72 65 20 74 6f 20 ,.** failure to
b850: 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 zero-fill short
b860: 72 65 61 64 73 20 77 69 6c 6c 20 65 76 65 6e 74 reads will event
b870: 75 61 6c 6c 79 20 6c 65 61 64 20 74 6f 0a 2a 2a ually lead to.**
b880: 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 database corrup
b890: 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 tion..*/.typedef
b8a0: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
b8b0: 69 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 io_methods sqlit
b8c0: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 e3_io_methods;.s
b8d0: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f truct sqlite3_io
b8e0: 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74 _methods {. int
b8f0: 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 iVersion;. int
b900: 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 (*xClose)(sqlit
b910: 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 e3_file*);. int
b920: 20 28 2a 78 52 65 61 64 29 28 73 71 6c 69 74 65 (*xRead)(sqlite
b930: 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 3_file*, void*,
b940: 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65 int iAmt, sqlite
b950: 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a 3_int64 iOfst);.
b960: 20 20 69 6e 74 20 28 2a 78 57 72 69 74 65 29 28 int (*xWrite)(
b970: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 sqlite3_file*, c
b980: 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 onst void*, int
b990: 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e iAmt, sqlite3_in
b9a0: 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e t64 iOfst);. in
b9b0: 74 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28 73 t (*xTruncate)(s
b9c0: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 qlite3_file*, sq
b9d0: 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 69 7a 65 lite3_int64 size
b9e0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63 );. int (*xSync
b9f0: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c )(sqlite3_file*,
ba00: 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 20 20 69 int flags);. i
ba10: 6e 74 20 28 2a 78 46 69 6c 65 53 69 7a 65 29 28 nt (*xFileSize)(
ba20: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 sqlite3_file*, s
ba30: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 qlite3_int64 *pS
ba40: 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4c ize);. int (*xL
ba50: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ock)(sqlite3_fil
ba60: 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 e*, int);. int
ba70: 28 2a 78 55 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 (*xUnlock)(sqlit
ba80: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a e3_file*, int);.
ba90: 20 20 69 6e 74 20 28 2a 78 43 68 65 63 6b 52 65 int (*xCheckRe
baa0: 73 65 72 76 65 64 4c 6f 63 6b 29 28 73 71 6c 69 servedLock)(sqli
bab0: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 2a te3_file*, int *
bac0: 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 pResOut);. int
bad0: 28 2a 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 (*xFileControl)(
bae0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
baf0: 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 nt op, void *pAr
bb00: 67 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 g);. int (*xSec
bb10: 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 torSize)(sqlite3
bb20: 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 _file*);. int (
bb30: 2a 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 *xDeviceCharacte
bb40: 72 69 73 74 69 63 73 29 28 73 71 6c 69 74 65 33 ristics)(sqlite3
bb50: 5f 66 69 6c 65 2a 29 3b 0a 20 20 2f 2a 20 41 64 _file*);. /* Ad
bb60: 64 69 74 69 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 ditional methods
bb70: 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e may be added in
bb80: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
bb90: 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 */.};../*.** CA
bba0: 50 49 33 52 45 46 3a 20 53 74 61 6e 64 61 72 64 PI3REF: Standard
bbb0: 20 46 69 6c 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 File Control Op
bbc0: 63 6f 64 65 73 20 7b 48 31 31 33 31 30 7d 20 3c codes {H11310} <
bbd0: 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30800>.**.** Th
bbe0: 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 ese integer cons
bbf0: 74 61 6e 74 73 20 61 72 65 20 6f 70 63 6f 64 65 tants are opcode
bc00: 73 20 66 6f 72 20 74 68 65 20 78 46 69 6c 65 43 s for the xFileC
bc10: 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a ontrol method.**
bc20: 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
bc30: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a _io_methods] obj
bc40: 65 63 74 20 61 6e 64 20 66 6f 72 20 74 68 65 20 ect and for the
bc50: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f [sqlite3_file_co
bc60: 6e 74 72 6f 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 ntrol()].** inte
bc70: 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rface..**.** The
bc80: 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c [SQLITE_FCNTL_L
bc90: 4f 43 4b 53 54 41 54 45 5d 20 6f 70 63 6f 64 65 OCKSTATE] opcode
bca0: 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62 is used for deb
bcb0: 75 67 67 69 6e 67 2e 20 20 54 68 69 73 0a 2a 2a ugging. This.**
bcc0: 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20 74 opcode causes t
bcd0: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 he xFileControl
bce0: 6d 65 74 68 6f 64 20 74 6f 20 77 72 69 74 65 20 method to write
bcf0: 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 the current stat
bd00: 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b e of.** the lock
bd10: 20 28 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 (one of [SQLITE
bd20: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 _LOCK_NONE], [SQ
bd30: 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 LITE_LOCK_SHARED
bd40: 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f ],.** [SQLITE_LO
bd50: 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 20 5b 53 CK_RESERVED], [S
bd60: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 QLITE_LOCK_PENDI
bd70: 4e 47 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f NG], or [SQLITE_
bd80: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 29 LOCK_EXCLUSIVE])
bd90: 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 .** into an inte
bda0: 67 65 72 20 74 68 61 74 20 74 68 65 20 70 41 72 ger that the pAr
bdb0: 67 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 g argument point
bdc0: 73 20 74 6f 2e 20 54 68 69 73 20 63 61 70 61 62 s to. This capab
bdd0: 69 6c 69 74 79 0a 2a 2a 20 69 73 20 75 73 65 64 ility.** is used
bde0: 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 20 during testing
bdf0: 61 6e 64 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 and only needs t
be00: 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 20 77 o be supported w
be10: 68 65 6e 20 53 51 4c 49 54 45 5f 54 45 53 54 0a hen SQLITE_TEST.
be20: 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a ** is defined..*
be30: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
be40: 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 _FCNTL_LOCKSTATE
be50: 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
be60: 65 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 e SQLITE_GET_LOC
be70: 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 20 20 KPROXYFILE
be80: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
be90: 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _SET_LOCKPROXYFI
bea0: 4c 45 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e LE 3.#defin
beb0: 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 e SQLITE_LAST_ER
bec0: 52 4e 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 RNO
bed0: 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 4../*.** CAPI3RE
bee0: 46 3a 20 4d 75 74 65 78 20 48 61 6e 64 6c 65 20 F: Mutex Handle
bef0: 7b 48 31 37 31 31 30 7d 20 3c 53 32 30 31 33 30 {H17110} <S20130
bf00: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 >.**.** The mute
bf10: 78 20 6d 6f 64 75 6c 65 20 77 69 74 68 69 6e 20 x module within
bf20: 53 51 4c 69 74 65 20 64 65 66 69 6e 65 73 20 5b SQLite defines [
bf30: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 74 sqlite3_mutex] t
bf40: 6f 20 62 65 20 61 6e 0a 2a 2a 20 61 62 73 74 72 o be an.** abstr
bf50: 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20 6d act type for a m
bf60: 75 74 65 78 20 6f 62 6a 65 63 74 2e 20 20 54 68 utex object. Th
bf70: 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6e 65 e SQLite core ne
bf80: 76 65 72 20 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 20 ver looks.** at
bf90: 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 the internal rep
bfa0: 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 resentation of a
bfb0: 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 n [sqlite3_mutex
bfc0: 5d 2e 20 20 49 74 20 6f 6e 6c 79 0a 2a 2a 20 64 ]. It only.** d
bfd0: 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65 eals with pointe
bfe0: 72 73 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 rs to the [sqlit
bff0: 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 e3_mutex] object
c000: 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 65 78 65 73 20 ..**.** Mutexes
c010: 61 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e are created usin
c020: 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 g [sqlite3_mutex
c030: 5f 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 79 _alloc()]..*/.ty
c040: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
c050: 69 74 65 33 5f 6d 75 74 65 78 20 73 71 6c 69 74 ite3_mutex sqlit
c060: 65 33 5f 6d 75 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a e3_mutex;../*.**
c070: 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e CAPI3REF: OS In
c080: 74 65 72 66 61 63 65 20 4f 62 6a 65 63 74 20 7b terface Object {
c090: 48 31 31 31 34 30 7d 20 3c 53 32 30 31 30 30 3e H11140} <S20100>
c0a0: 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e .**.** An instan
c0b0: 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 ce of the sqlite
c0c0: 33 5f 76 66 73 20 6f 62 6a 65 63 74 20 64 65 66 3_vfs object def
c0d0: 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 ines the interfa
c0e0: 63 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 ce between.** th
c0f0: 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 61 6e e SQLite core an
c100: 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 d the underlying
c110: 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
c120: 6d 2e 20 20 54 68 65 20 22 76 66 73 22 0a 2a 2a m. The "vfs".**
c130: 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 in the name of
c140: 74 68 65 20 6f 62 6a 65 63 74 20 73 74 61 6e 64 the object stand
c150: 73 20 66 6f 72 20 22 76 69 72 74 75 61 6c 20 66 s for "virtual f
c160: 69 6c 65 20 73 79 73 74 65 6d 22 2e 0a 2a 2a 0a ile system"..**.
c170: 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 ** The value of
c180: 74 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 the iVersion fie
c190: 6c 64 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 ld is initially
c1a0: 31 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 1 but may be lar
c1b0: 67 65 72 20 69 6e 0a 2a 2a 20 66 75 74 75 72 65 ger in.** future
c1c0: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
c1d0: 69 74 65 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c ite. Additional
c1e0: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 fields may be a
c1f0: 70 70 65 6e 64 65 64 20 74 6f 20 74 68 69 73 0a ppended to this.
c200: 2a 2a 20 6f 62 6a 65 63 74 20 77 68 65 6e 20 74 ** object when t
c210: 68 65 20 69 56 65 72 73 69 6f 6e 20 76 61 6c 75 he iVersion valu
c220: 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 2e 20 e is increased.
c230: 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73 Note that the s
c240: 74 72 75 63 74 75 72 65 0a 2a 2a 20 6f 66 20 74 tructure.** of t
c250: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f he sqlite3_vfs o
c260: 62 6a 65 63 74 20 63 68 61 6e 67 65 73 20 69 6e bject changes in
c270: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
c280: 20 62 65 74 77 65 65 6e 0a 2a 2a 20 53 51 4c 69 between.** SQLi
c290: 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 39 te version 3.5.9
c2a0: 20 61 6e 64 20 33 2e 36 2e 30 20 61 6e 64 20 79 and 3.6.0 and y
c2b0: 65 74 20 74 68 65 20 69 56 65 72 73 69 6f 6e 20 et the iVersion
c2c0: 66 69 65 6c 64 20 77 61 73 20 6e 6f 74 0a 2a 2a field was not.**
c2d0: 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a modified..**.**
c2e0: 20 54 68 65 20 73 7a 4f 73 46 69 6c 65 20 66 69 The szOsFile fi
c2f0: 65 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 eld is the size
c300: 6f 66 20 74 68 65 20 73 75 62 63 6c 61 73 73 65 of the subclasse
c310: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d d [sqlite3_file]
c320: 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 75 73 .** structure us
c330: 65 64 20 62 79 20 74 68 69 73 20 56 46 53 2e 20 ed by this VFS.
c340: 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 74 mxPathname is t
c350: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 he maximum lengt
c360: 68 20 6f 66 0a 2a 2a 20 61 20 70 61 74 68 6e 61 h of.** a pathna
c370: 6d 65 20 69 6e 20 74 68 69 73 20 56 46 53 2e 0a me in this VFS..
c380: 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 65 64 **.** Registered
c390: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a sqlite3_vfs obj
c3a0: 65 63 74 73 20 61 72 65 20 6b 65 70 74 20 6f 6e ects are kept on
c3b0: 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 66 a linked list f
c3c0: 6f 72 6d 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 ormed by.** the
c3d0: 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e 20 20 pNext pointer.
c3e0: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 The [sqlite3_vfs
c3f0: 5f 72 65 67 69 73 74 65 72 28 29 5d 0a 2a 2a 20 _register()].**
c400: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 and [sqlite3_vfs
c410: 5f 75 6e 72 65 67 69 73 74 65 72 28 29 5d 20 69 _unregister()] i
c420: 6e 74 65 72 66 61 63 65 73 20 6d 61 6e 61 67 65 nterfaces manage
c430: 20 74 68 69 73 20 6c 69 73 74 0a 2a 2a 20 69 6e this list.** in
c440: 20 61 20 74 68 72 65 61 64 2d 73 61 66 65 20 77 a thread-safe w
c450: 61 79 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 ay. The [sqlite
c460: 33 5f 76 66 73 5f 66 69 6e 64 28 29 5d 20 69 6e 3_vfs_find()] in
c470: 74 65 72 66 61 63 65 0a 2a 2a 20 73 65 61 72 63 terface.** searc
c480: 68 65 73 20 74 68 65 20 6c 69 73 74 2e 20 20 4e hes the list. N
c490: 65 69 74 68 65 72 20 74 68 65 20 61 70 70 6c 69 either the appli
c4a0: 63 61 74 69 6f 6e 20 63 6f 64 65 20 6e 6f 72 20 cation code nor
c4b0: 74 68 65 20 56 46 53 0a 2a 2a 20 69 6d 70 6c 65 the VFS.** imple
c4c0: 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 mentation should
c4d0: 20 75 73 65 20 74 68 65 20 70 4e 65 78 74 20 70 use the pNext p
c4e0: 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ointer..**.** Th
c4f0: 65 20 70 4e 65 78 74 20 66 69 65 6c 64 20 69 73 e pNext field is
c500: 20 74 68 65 20 6f 6e 6c 79 20 66 69 65 6c 64 20 the only field
c510: 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 in the sqlite3_v
c520: 66 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 fs.** structure
c530: 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c that SQLite will
c540: 20 65 76 65 72 20 6d 6f 64 69 66 79 2e 20 20 53 ever modify. S
c550: 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 QLite will only
c560: 61 63 63 65 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64 access.** or mod
c570: 69 66 79 20 74 68 69 73 20 66 69 65 6c 64 20 77 ify this field w
c580: 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 61 20 70 hile holding a p
c590: 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 69 63 articular static
c5a0: 20 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 61 mutex..** The a
c5b0: 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c pplication shoul
c5c0: 64 20 6e 65 76 65 72 20 6d 6f 64 69 66 79 20 61 d never modify a
c5d0: 6e 79 74 68 69 6e 67 20 77 69 74 68 69 6e 20 74 nything within t
c5e0: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a he sqlite3_vfs.*
c5f0: 2a 20 6f 62 6a 65 63 74 20 6f 6e 63 65 20 74 68 * object once th
c600: 65 20 6f 62 6a 65 63 74 20 68 61 73 20 62 65 65 e object has bee
c610: 6e 20 72 65 67 69 73 74 65 72 65 64 2e 0a 2a 2a n registered..**
c620: 0a 2a 2a 20 54 68 65 20 7a 4e 61 6d 65 20 66 69 .** The zName fi
c630: 65 6c 64 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 eld holds the na
c640: 6d 65 20 6f 66 20 74 68 65 20 56 46 53 20 6d 6f me of the VFS mo
c650: 64 75 6c 65 2e 20 20 54 68 65 20 6e 61 6d 65 20 dule. The name
c660: 6d 75 73 74 0a 2a 2a 20 62 65 20 75 6e 69 71 75 must.** be uniqu
c670: 65 20 61 63 72 6f 73 73 20 61 6c 6c 20 56 46 53 e across all VFS
c680: 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 modules..**.**
c690: 53 51 4c 69 74 65 20 77 69 6c 6c 20 67 75 61 72 SQLite will guar
c6a0: 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 7a antee that the z
c6b0: 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 Filename paramet
c6c0: 65 72 20 74 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69 er to xOpen.** i
c6d0: 73 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 20 s either a NULL
c6e0: 70 6f 69 6e 74 65 72 20 6f 72 20 73 74 72 69 6e pointer or strin
c6f0: 67 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 g obtained.** fr
c700: 6f 6d 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 om xFullPathname
c710: 28 29 2e 20 20 53 51 4c 69 74 65 20 66 75 72 74 (). SQLite furt
c720: 68 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 her guarantees t
c730: 68 61 74 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e hat.** the strin
c740: 67 20 77 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 g will be valid
c750: 61 6e 64 20 75 6e 63 68 61 6e 67 65 64 20 75 6e and unchanged un
c760: 74 69 6c 20 78 43 6c 6f 73 65 28 29 20 69 73 0a til xClose() is.
c770: 2a 2a 20 63 61 6c 6c 65 64 2e 20 42 65 63 61 75 ** called. Becau
c780: 73 65 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f se of the previo
c790: 75 73 20 73 65 6e 74 65 6e 73 65 2c 0a 2a 2a 20 us sentense,.**
c7a0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c the [sqlite3_fil
c7b0: 65 5d 20 63 61 6e 20 73 61 66 65 6c 79 20 73 74 e] can safely st
c7c0: 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ore a pointer to
c7d0: 20 74 68 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 the.** filename
c7e0: 20 69 66 20 69 74 20 6e 65 65 64 73 20 74 6f 20 if it needs to
c7f0: 72 65 6d 65 6d 62 65 72 20 74 68 65 20 66 69 6c remember the fil
c800: 65 6e 61 6d 65 20 66 6f 72 20 73 6f 6d 65 20 72 ename for some r
c810: 65 61 73 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 eason..** If the
c820: 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d zFilename param
c830: 65 74 65 72 20 69 73 20 78 4f 70 65 6e 20 69 73 eter is xOpen is
c840: 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
c850: 74 68 65 6e 20 78 4f 70 65 6e 0a 2a 2a 20 6d 75 then xOpen.** mu
c860: 73 74 20 69 6e 76 69 74 65 20 69 74 73 20 6f 77 st invite its ow
c870: 6e 20 74 65 6d 70 6f 72 61 72 79 20 6e 61 6d 65 n temporary name
c880: 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 20 for the file.
c890: 57 68 65 6e 65 76 65 72 20 74 68 65 20 0a 2a 2a Whenever the .**
c8a0: 20 78 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d xFilename param
c8b0: 65 74 65 72 20 69 73 20 4e 55 4c 4c 20 69 74 20 eter is NULL it
c8c0: 77 69 6c 6c 20 61 6c 73 6f 20 62 65 20 74 68 65 will also be the
c8d0: 20 63 61 73 65 20 74 68 61 74 20 74 68 65 0a 2a case that the.*
c8e0: 2a 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 * flags paramete
c8f0: 72 20 77 69 6c 6c 20 69 6e 63 6c 75 64 65 20 5b r will include [
c900: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
c910: 54 45 4f 4e 43 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a TEONCLOSE]..**.*
c920: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 * The flags argu
c930: 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28 29 20 ment to xOpen()
c940: 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20 62 69 74 includes all bit
c950: 73 20 73 65 74 20 69 6e 0a 2a 2a 20 74 68 65 20 s set in.** the
c960: 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 flags argument t
c970: 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f o [sqlite3_open_
c980: 76 32 28 29 5d 2e 20 20 4f 72 20 69 66 20 5b 73 v2()]. Or if [s
c990: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a qlite3_open()].*
c9a0: 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 * or [sqlite3_op
c9b0: 65 6e 31 36 28 29 5d 20 69 73 20 75 73 65 64 2c en16()] is used,
c9c0: 20 74 68 65 6e 20 66 6c 61 67 73 20 69 6e 63 6c then flags incl
c9d0: 75 64 65 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a udes at least.**
c9e0: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 [SQLITE_OPEN_RE
c9f0: 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49 ADWRITE] | [SQLI
ca00: 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 2e TE_OPEN_CREATE].
ca10: 20 0a 2a 2a 20 49 66 20 78 4f 70 65 6e 28 29 20 .** If xOpen()
ca20: 6f 70 65 6e 73 20 61 20 66 69 6c 65 20 72 65 61 opens a file rea
ca30: 64 2d 6f 6e 6c 79 20 74 68 65 6e 20 69 74 20 73 d-only then it s
ca40: 65 74 73 20 2a 70 4f 75 74 46 6c 61 67 73 20 74 ets *pOutFlags t
ca50: 6f 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 5b 53 51 o.** include [SQ
ca60: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
ca70: 4c 59 5d 2e 20 20 4f 74 68 65 72 20 62 69 74 73 LY]. Other bits
ca80: 20 69 6e 20 2a 70 4f 75 74 46 6c 61 67 73 20 6d in *pOutFlags m
ca90: 61 79 20 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a ay be set..**.**
caa0: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 73 SQLite will als
cab0: 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 o add one of the
cac0: 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 following flags
cad0: 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 28 29 0a to the xOpen().
cae0: 2a 2a 20 63 61 6c 6c 2c 20 64 65 70 65 6e 64 69 ** call, dependi
caf0: 6e 67 20 6f 6e 20 74 68 65 20 6f 62 6a 65 63 74 ng on the object
cb00: 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a being opened:.*
cb10: 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
cb20: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f > [SQLITE_OPEN_
cb30: 4d 41 49 4e 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e MAIN_DB].** <li>
cb40: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d [SQLITE_OPEN_M
cb50: 41 49 4e 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 AIN_JOURNAL].**
cb60: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 <li> [SQLITE_OP
cb70: 45 4e 5f 54 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c EN_TEMP_DB].** <
cb80: 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 li> [SQLITE_OPE
cb90: 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a N_TEMP_JOURNAL].
cba0: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 ** <li> [SQLITE
cbb0: 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f _OPEN_TRANSIENT_
cbc0: 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 DB].** <li> [SQ
cbd0: 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 LITE_OPEN_SUBJOU
cbe0: 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b RNAL].** <li> [
cbf0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 SQLITE_OPEN_MAST
cc00: 45 52 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c ER_JOURNAL].** <
cc10: 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 /ul>.**.** The f
cc20: 69 6c 65 20 49 2f 4f 20 69 6d 70 6c 65 6d 65 6e ile I/O implemen
cc30: 74 61 74 69 6f 6e 20 63 61 6e 20 75 73 65 20 74 tation can use t
cc40: 68 65 20 6f 62 6a 65 63 74 20 74 79 70 65 20 66 he object type f
cc50: 6c 61 67 73 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 lags to.** chang
cc60: 65 20 74 68 65 20 77 61 79 20 69 74 20 64 65 61 e the way it dea
cc70: 6c 73 20 77 69 74 68 20 66 69 6c 65 73 2e 20 20 ls with files.
cc80: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 For example, an
cc90: 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 74 application.** t
cca0: 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 63 61 72 hat does not car
ccb0: 65 20 61 62 6f 75 74 20 63 72 61 73 68 20 72 65 e about crash re
ccc0: 63 6f 76 65 72 79 20 6f 72 20 72 6f 6c 6c 62 61 covery or rollba
ccd0: 63 6b 20 6d 69 67 68 74 20 6d 61 6b 65 0a 2a 2a ck might make.**
cce0: 20 74 68 65 20 6f 70 65 6e 20 6f 66 20 61 20 6a the open of a j
ccf0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 20 6e 6f ournal file a no
cd00: 2d 6f 70 2e 20 20 57 72 69 74 65 73 20 74 6f 20 -op. Writes to
cd10: 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 this journal wou
cd20: 6c 64 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 6e 6f ld.** also be no
cd30: 2d 6f 70 73 2c 20 61 6e 64 20 61 6e 79 20 61 74 -ops, and any at
cd40: 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68 tempt to read th
cd50: 65 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 20 e journal would
cd60: 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 return.** SQLITE
cd70: 5f 49 4f 45 52 52 2e 20 20 4f 72 20 74 68 65 20 _IOERR. Or the
cd80: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d implementation m
cd90: 69 67 68 74 20 72 65 63 6f 67 6e 69 7a 65 20 74 ight recognize t
cda0: 68 61 74 20 61 20 64 61 74 61 62 61 73 65 0a 2a hat a database.*
cdb0: 2a 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 * file will be d
cdc0: 6f 69 6e 67 20 70 61 67 65 2d 61 6c 69 67 6e 65 oing page-aligne
cdd0: 64 20 73 65 63 74 6f 72 20 72 65 61 64 73 20 61 d sector reads a
cde0: 6e 64 20 77 72 69 74 65 73 20 69 6e 20 61 20 72 nd writes in a r
cdf0: 61 6e 64 6f 6d 0a 2a 2a 20 6f 72 64 65 72 20 61 andom.** order a
ce00: 6e 64 20 73 65 74 20 75 70 20 69 74 73 20 49 2f nd set up its I/
ce10: 4f 20 73 75 62 73 79 73 74 65 6d 20 61 63 63 6f O subsystem acco
ce20: 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 rdingly..**.** S
ce30: 51 4c 69 74 65 20 6d 69 67 68 74 20 61 6c 73 6f QLite might also
ce40: 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 add one of the
ce50: 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 following flags
ce60: 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 to the xOpen met
ce70: 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a hod:.**.** <ul>.
ce80: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
ce90: 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
cea0: 53 45 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c SE].** <li> [SQL
ceb0: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
cec0: 56 45 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a VE].** </ul>.**.
ced0: 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f ** The [SQLITE_O
cee0: 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
cef0: 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 E] flag means th
cf00: 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 e file should be
cf10: 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e .** deleted when
cf20: 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 it is closed.
cf30: 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e The [SQLITE_OPEN
cf40: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a _DELETEONCLOSE].
cf50: 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 66 ** will be set f
cf60: 6f 72 20 54 45 4d 50 20 20 64 61 74 61 62 61 73 or TEMP databas
cf70: 65 73 2c 20 6a 6f 75 72 6e 61 6c 73 20 61 6e 64 es, journals and
cf80: 20 66 6f 72 20 73 75 62 6a 6f 75 72 6e 61 6c 73 for subjournals
cf90: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c ..**.** The [SQL
cfa0: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
cfb0: 56 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 VE] flag means t
cfc0: 68 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 he file should b
cfd0: 65 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 20 e opened.** for
cfe0: 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 exclusive access
cff0: 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 . This flag is
d000: 73 65 74 20 66 6f 72 20 61 6c 6c 20 66 69 6c 65 set for all file
d010: 73 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20 s except.** for
d020: 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
d030: 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 e file..**.** At
d040: 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65 20 least szOsFile
d050: 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 bytes of memory
d060: 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 are allocated by
d070: 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 68 6f SQLite.** to ho
d080: 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65 33 ld the [sqlite3
d090: 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72 65 _file] structure
d0a0: 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74 passed as the t
d0b0: 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 hird.** argument
d0c0: 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 65 20 to xOpen. The
d0d0: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f 65 xOpen method doe
d0e0: 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a 2a s not have to.**
d0f0: 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 74 allocate the st
d100: 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f 75 ructure; it shou
d110: 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74 20 ld just fill it
d120: 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c in..**.** The fl
d130: 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ags argument to
d140: 78 41 63 63 65 73 73 28 29 20 6d 61 79 20 62 65 xAccess() may be
d150: 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f [SQLITE_ACCESS_
d160: 45 58 49 53 54 53 5d 0a 2a 2a 20 74 6f 20 74 65 EXISTS].** to te
d170: 73 74 20 66 6f 72 20 74 68 65 20 65 78 69 73 74 st for the exist
d180: 65 6e 63 65 20 6f 66 20 61 20 66 69 6c 65 2c 20 ence of a file,
d190: 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53 or [SQLITE_ACCES
d1a0: 53 5f 52 45 41 44 57 52 49 54 45 5d 20 74 6f 0a S_READWRITE] to.
d1b0: 2a 2a 20 74 65 73 74 20 77 68 65 74 68 65 72 20 ** test whether
d1c0: 61 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 a file is readab
d1d0: 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2c le and writable,
d1e0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 or [SQLITE_ACCE
d1f0: 53 53 5f 52 45 41 44 5d 0a 2a 2a 20 74 6f 20 74 SS_READ].** to t
d200: 65 73 74 20 77 68 65 74 68 65 72 20 61 20 66 69 est whether a fi
d210: 6c 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 72 le is at least r
d220: 65 61 64 61 62 6c 65 2e 20 20 20 54 68 65 20 66 eadable. The f
d230: 69 6c 65 20 63 61 6e 20 62 65 20 61 0a 2a 2a 20 ile can be a.**
d240: 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a directory..**.**
d250: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 77 SQLite will alw
d260: 61 79 73 20 61 6c 6c 6f 63 61 74 65 20 61 74 20 ays allocate at
d270: 6c 65 61 73 74 20 6d 78 50 61 74 68 6e 61 6d 65 least mxPathname
d280: 2b 31 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 +1 bytes for the
d290: 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65 .** output buffe
d2a0: 72 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2e r xFullPathname.
d2b0: 20 20 54 68 65 20 65 78 61 63 74 20 73 69 7a 65 The exact size
d2c0: 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 of the output b
d2d0: 75 66 66 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f uffer.** is also
d2e0: 20 70 61 73 73 65 64 20 61 73 20 61 20 70 61 72 passed as a par
d2f0: 61 6d 65 74 65 72 20 74 6f 20 62 6f 74 68 20 20 ameter to both
d300: 6d 65 74 68 6f 64 73 2e 20 49 66 20 74 68 65 20 methods. If the
d310: 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a output buffer.**
d320: 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 20 65 6e is not large en
d330: 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45 5f 43 41 ough, [SQLITE_CA
d340: 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c 64 20 62 NTOPEN] should b
d350: 65 20 72 65 74 75 72 6e 65 64 2e 20 53 69 6e 63 e returned. Sinc
d360: 65 20 74 68 69 73 20 69 73 0a 2a 2a 20 68 61 6e e this is.** han
d370: 64 6c 65 64 20 61 73 20 61 20 66 61 74 61 6c 20 dled as a fatal
d380: 65 72 72 6f 72 20 62 79 20 53 51 4c 69 74 65 2c error by SQLite,
d390: 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 vfs implementat
d3a0: 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65 6e 64 65 ions should ende
d3b0: 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 72 65 76 65 avor.** to preve
d3c0: 6e 74 20 74 68 69 73 20 62 79 20 73 65 74 74 69 nt this by setti
d3d0: 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65 20 74 6f ng mxPathname to
d3e0: 20 61 20 73 75 66 66 69 63 69 65 6e 74 6c 79 20 a sufficiently
d3f0: 6c 61 72 67 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a large value..**.
d400: 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65 ** The xRandomne
d410: 73 73 28 29 2c 20 78 53 6c 65 65 70 28 29 2c 20 ss(), xSleep(),
d420: 61 6e 64 20 78 43 75 72 72 65 6e 74 54 69 6d 65 and xCurrentTime
d430: 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a () interfaces.**
d440: 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63 74 6c are not strictl
d450: 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 20 y a part of the
d460: 66 69 6c 65 73 79 73 74 65 6d 2c 20 62 75 74 20 filesystem, but
d470: 74 68 65 79 20 61 72 65 0a 2a 2a 20 69 6e 63 6c they are.** incl
d480: 75 64 65 64 20 69 6e 20 74 68 65 20 56 46 53 20 uded in the VFS
d490: 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 63 6f structure for co
d4a0: 6d 70 6c 65 74 65 6e 65 73 73 2e 0a 2a 2a 20 54 mpleteness..** T
d4b0: 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 28 29 he xRandomness()
d4c0: 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 function attemp
d4d0: 74 73 20 74 6f 20 72 65 74 75 72 6e 20 6e 42 79 ts to return nBy
d4e0: 74 65 73 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 tes bytes.** of
d4f0: 67 6f 6f 64 2d 71 75 61 6c 69 74 79 20 72 61 6e good-quality ran
d500: 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 4f 75 domness into zOu
d510: 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 t. The return v
d520: 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 61 alue is.** the a
d530: 63 74 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 ctual number of
d540: 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e bytes of randomn
d550: 65 73 73 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2a ess obtained..**
d560: 20 54 68 65 20 78 53 6c 65 65 70 28 29 20 6d 65 The xSleep() me
d570: 74 68 6f 64 20 63 61 75 73 65 73 20 74 68 65 20 thod causes the
d580: 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 74 calling thread t
d590: 6f 20 73 6c 65 65 70 20 66 6f 72 20 61 74 0a 2a o sleep for at.*
d5a0: 2a 20 6c 65 61 73 74 20 74 68 65 20 6e 75 6d 62 * least the numb
d5b0: 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e er of microsecon
d5c0: 64 73 20 67 69 76 65 6e 2e 20 20 54 68 65 20 78 ds given. The x
d5d0: 43 75 72 72 65 6e 74 54 69 6d 65 28 29 0a 2a 2a CurrentTime().**
d5e0: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 method returns
d5f0: 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d a Julian Day Num
d600: 62 65 72 20 66 6f 72 20 74 68 65 20 63 75 72 72 ber for the curr
d610: 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 6d ent date and tim
d620: 65 2e 0a 2a 2a 0a 2a 2f 0a 74 79 70 65 64 65 66 e..**.*/.typedef
d630: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
d640: 76 66 73 20 73 71 6c 69 74 65 33 5f 76 66 73 3b vfs sqlite3_vfs;
d650: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
d660: 76 66 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 vfs {. int iVer
d670: 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 sion;
d680: 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 76 65 /* Structure ve
d690: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a rsion number */.
d6a0: 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 3b 20 int szOsFile;
d6b0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
d6c0: 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 65 64 ze of subclassed
d6d0: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2f sqlite3_file */
d6e0: 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e 61 6d . int mxPathnam
d6f0: 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d e; /* M
d700: 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 74 68 aximum file path
d710: 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 name length */.
d720: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e sqlite3_vfs *pN
d730: 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 ext; /* Nex
d740: 74 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53 t registered VFS
d750: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
d760: 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f *zName; /
d770: 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 76 * Name of this v
d780: 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74 irtual file syst
d790: 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 em */. void *pA
d7a0: 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 ppData;
d7b0: 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 /* Pointer to a
d7c0: 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 63 69 pplication-speci
d7d0: 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e fic data */. in
d7e0: 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74 t (*xOpen)(sqlit
d7f0: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 e3_vfs*, const c
d800: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 6c 69 har *zName, sqli
d810: 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 20 20 te3_file*,.
d820: 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c int fl
d830: 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 46 6c ags, int *pOutFl
d840: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 ags);. int (*xD
d850: 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 5f 76 elete)(sqlite3_v
d860: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 fs*, const char
d870: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 6e 63 *zName, int sync
d880: 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 41 Dir);. int (*xA
d890: 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 5f 76 ccess)(sqlite3_v
d8a0: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 fs*, const char
d8b0: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67 *zName, int flag
d8c0: 73 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 s, int *pResOut)
d8d0: 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6c 6c 50 ;. int (*xFullP
d8e0: 61 74 68 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 athname)(sqlite3
d8f0: 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 _vfs*, const cha
d900: 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4f r *zName, int nO
d910: 75 74 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b ut, char *zOut);
d920: 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 4f 70 . void *(*xDlOp
d930: 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a en)(sqlite3_vfs*
d940: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
d950: 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 76 6f 69 64 ilename);. void
d960: 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73 71 6c (*xDlError)(sql
d970: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e ite3_vfs*, int n
d980: 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 72 72 Byte, char *zErr
d990: 4d 73 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a 28 Msg);. void (*(
d9a0: 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69 74 65 33 *xDlSym)(sqlite3
d9b0: 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63 6f 6e _vfs*,void*, con
d9c0: 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c st char *zSymbol
d9d0: 29 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 ))(void);. void
d9e0: 20 28 2a 78 44 6c 43 6c 6f 73 65 29 28 73 71 6c (*xDlClose)(sql
d9f0: 69 74 65 33 5f 76 66 73 2a 2c 20 76 6f 69 64 2a ite3_vfs*, void*
da00: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 61 6e 64 );. int (*xRand
da10: 6f 6d 6e 65 73 73 29 28 73 71 6c 69 74 65 33 5f omness)(sqlite3_
da20: 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c vfs*, int nByte,
da30: 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a 20 20 char *zOut);.
da40: 69 6e 74 20 28 2a 78 53 6c 65 65 70 29 28 73 71 int (*xSleep)(sq
da50: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 lite3_vfs*, int
da60: 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b 0a 20 microseconds);.
da70: 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e 74 54 int (*xCurrentT
da80: 69 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73 ime)(sqlite3_vfs
da90: 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 20 20 69 *, double*);. i
daa0: 6e 74 20 28 2a 78 47 65 74 4c 61 73 74 45 72 72 nt (*xGetLastErr
dab0: 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a or)(sqlite3_vfs*
dac0: 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a , int, char *);.
dad0: 20 20 2f 2a 20 4e 65 77 20 66 69 65 6c 64 73 20 /* New fields
dae0: 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64 20 may be appended
daf0: 69 6e 20 66 69 67 75 72 65 20 76 65 72 73 69 6f in figure versio
db00: 6e 73 2e 20 20 54 68 65 20 69 56 65 72 73 69 6f ns. The iVersio
db10: 6e 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c n. ** value wil
db20: 6c 20 69 6e 63 72 65 6d 65 6e 74 20 77 68 65 6e l increment when
db30: 65 76 65 72 20 74 68 69 73 20 68 61 70 70 65 6e ever this happen
db40: 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 s. */.};../*.**
db50: 43 41 50 49 33 52 45 46 3a 20 46 6c 61 67 73 20 CAPI3REF: Flags
db60: 66 6f 72 20 74 68 65 20 78 41 63 63 65 73 73 20 for the xAccess
db70: 56 46 53 20 6d 65 74 68 6f 64 20 7b 48 31 31 31 VFS method {H111
db80: 39 30 7d 20 3c 48 31 31 31 34 30 3e 0a 2a 2a 0a 90} <H11140>.**.
db90: 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 ** These integer
dba0: 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 6e 20 62 constants can b
dbb0: 65 20 75 73 65 64 20 61 73 20 74 68 65 20 74 68 e used as the th
dbc0: 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f ird parameter to
dbd0: 0a 2a 2a 20 74 68 65 20 78 41 63 63 65 73 73 20 .** the xAccess
dbe0: 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 5b 73 71 method of an [sq
dbf0: 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 lite3_vfs] objec
dc00: 74 2e 20 7b 45 4e 44 7d 20 20 54 68 65 79 20 64 t. {END} They d
dc10: 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 61 74 etermine.** what
dc20: 20 6b 69 6e 64 20 6f 66 20 70 65 72 6d 69 73 73 kind of permiss
dc30: 69 6f 6e 73 20 74 68 65 20 78 41 63 63 65 73 73 ions the xAccess
dc40: 20 6d 65 74 68 6f 64 20 69 73 20 6c 6f 6f 6b 69 method is looki
dc50: 6e 67 20 66 6f 72 2e 0a 2a 2a 20 57 69 74 68 20 ng for..** With
dc60: 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
dc70: 49 53 54 53 2c 20 74 68 65 20 78 41 63 63 65 73 ISTS, the xAcces
dc80: 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 73 69 6d 70 s method.** simp
dc90: 6c 79 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 ly checks whethe
dca0: 72 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 r the file exist
dcb0: 73 2e 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 s..** With SQLIT
dcc0: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 E_ACCESS_READWRI
dcd0: 54 45 2c 20 74 68 65 20 78 41 63 63 65 73 73 20 TE, the xAccess
dce0: 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73 method.** checks
dcf0: 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c whether the fil
dd00: 65 20 69 73 20 62 6f 74 68 20 72 65 61 64 61 62 e is both readab
dd10: 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2e le and writable.
dd20: 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f .** With SQLITE_
dd30: 41 43 43 45 53 53 5f 52 45 41 44 2c 20 74 68 65 ACCESS_READ, the
dd40: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a xAccess method.
dd50: 2a 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 ** checks whethe
dd60: 72 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 r the file is re
dd70: 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 adable..*/.#defi
dd80: 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 ne SQLITE_ACCESS
dd90: 5f 45 58 49 53 54 53 20 20 20 20 30 0a 23 64 65 _EXISTS 0.#de
dda0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 fine SQLITE_ACCE
ddb0: 53 53 5f 52 45 41 44 57 52 49 54 45 20 31 0a 23 SS_READWRITE 1.#
ddc0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 define SQLITE_AC
ddd0: 43 45 53 53 5f 52 45 41 44 20 20 20 20 20 20 32 CESS_READ 2
dde0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
ddf0: 3a 20 49 6e 69 74 69 61 6c 69 7a 65 20 54 68 65 : Initialize The
de00: 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 20 SQLite Library
de10: 7b 48 31 30 31 33 30 7d 20 3c 53 32 30 30 30 30 {H10130} <S20000
de20: 3e 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 ><S30100>.**.**
de30: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 The sqlite3_init
de40: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 ialize() routine
de50: 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 initializes the
de60: 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69 62 72 61 .** SQLite libra
de70: 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 ry. The sqlite3
de80: 5f 73 68 75 74 64 6f 77 6e 28 29 20 72 6f 75 74 _shutdown() rout
de90: 69 6e 65 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74 ine.** deallocat
dea0: 65 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 es any resources
deb0: 20 74 68 61 74 20 77 65 72 65 20 61 6c 6c 6f 63 that were alloc
dec0: 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f ated by sqlite3_
ded0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a initialize()..**
dee0: 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 .** A call to sq
def0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
df00: 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74 () is an "effect
df10: 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20 ive" call if it
df20: 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 is.** the first
df30: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 time sqlite3_ini
df40: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 tialize() is inv
df50: 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 oked during the
df60: 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 lifetime of.** t
df70: 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 he process, or i
df80: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73 f it is the firs
df90: 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 t time sqlite3_i
dfa0: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 nitialize() is i
dfb0: 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 nvoked.** follow
dfc0: 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ing a call to sq
dfd0: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
dfe0: 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63 . Only an effec
dff0: 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20 tive call.** of
e000: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
e010: 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e ze() does any in
e020: 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41 itialization. A
e030: 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a ll other calls.*
e040: 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e * are harmless n
e050: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f o-ops..**.** Amo
e060: 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67 73 2c ng other things,
e070: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
e080: 69 7a 65 28 29 20 73 68 61 6c 6c 20 69 6e 76 6f ize() shall invo
e090: 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 ke.** sqlite3_os
e0a0: 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d 69 6c 61 _init(). Simila
e0b0: 72 6c 79 2c 20 73 71 6c 69 74 65 33 5f 73 68 75 rly, sqlite3_shu
e0c0: 74 64 6f 77 6e 28 29 0a 2a 2a 20 73 68 61 6c 6c tdown().** shall
e0d0: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f invoke sqlite3_
e0e0: 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 os_end()..**.**
e0f0: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 The sqlite3_init
e100: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 ialize() routine
e110: 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
e120: 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 2e _OK] on success.
e130: 0a 2a 2a 20 49 66 20 66 6f 72 20 73 6f 6d 65 20 .** If for some
e140: 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65 33 5f reason, sqlite3_
e150: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 initialize() is
e160: 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74 69 61 unable to initia
e170: 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 lize.** the libr
e180: 61 72 79 20 28 70 65 72 68 61 70 73 20 69 74 20 ary (perhaps it
e190: 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c is unable to all
e1a0: 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64 20 72 ocate a needed r
e1b0: 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a 2a 20 esource such.**
e1c0: 61 73 20 61 20 6d 75 74 65 78 29 20 69 74 20 72 as a mutex) it r
e1d0: 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72 6f 72 eturns an [error
e1e0: 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74 68 61 code] other tha
e1f0: 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a n [SQLITE_OK]..*
e200: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
e210: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f _initialize() ro
e220: 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
e230: 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 6d 61 internally by ma
e240: 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69 ny other.** SQLi
e250: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 73 6f te interfaces so
e260: 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61 that an applica
e270: 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64 6f 65 tion usually doe
e280: 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 2a 2a s not need to.**
e290: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f invoke sqlite3_
e2a0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72 initialize() dir
e2b0: 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78 61 6d ectly. For exam
e2c0: 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 ple, [sqlite3_op
e2d0: 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73 20 73 en()].** calls s
e2e0: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
e2f0: 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c 69 74 e() so the SQLit
e300: 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 62 e library will b
e310: 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a e automatically.
e320: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 ** initialized w
e330: 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 hen [sqlite3_ope
e340: 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 69 n()] is called i
e350: 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65 20 f it has not be
e360: 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 61 initialized.** a
e370: 6c 72 65 61 64 79 2e 20 20 48 6f 77 65 76 65 72 lready. However
e380: 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 , if SQLite is c
e390: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
e3a0: 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 [SQLITE_OMIT_AU
e3b0: 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d 70 69 TOINIT].** compi
e3c0: 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2c 20 le-time option,
e3d0: 74 68 65 6e 20 74 68 65 20 61 75 74 6f 6d 61 74 then the automat
e3e0: 69 63 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 ic calls to sqli
e3f0: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
e400: 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 74 65 64 20 .** are omitted
e410: 61 6e 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 and the applicat
e420: 69 6f 6e 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 ion must call sq
e430: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
e440: 28 29 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 70 () directly.** p
e450: 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e rior to using an
e460: 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69 y other SQLite i
e470: 6e 74 65 72 66 61 63 65 2e 20 20 46 6f 72 20 6d nterface. For m
e480: 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69 6c 69 aximum portabili
e490: 74 79 2c 0a 2a 2a 20 69 74 20 69 73 20 72 65 63 ty,.** it is rec
e4a0: 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70 ommended that ap
e4b0: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6c 77 61 79 plications alway
e4c0: 73 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 s invoke sqlite3
e4d0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a _initialize().**
e4e0: 20 64 69 72 65 63 74 6c 79 20 70 72 69 6f 72 20 directly prior
e4f0: 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68 to using any oth
e500: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 er SQLite interf
e510: 61 63 65 2e 20 20 46 75 74 75 72 65 20 72 65 6c ace. Future rel
e520: 65 61 73 65 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 eases.** of SQLi
e530: 74 65 20 6d 61 79 20 72 65 71 75 69 72 65 20 74 te may require t
e540: 68 69 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 his. In other w
e550: 6f 72 64 73 2c 20 74 68 65 20 62 65 68 61 76 69 ords, the behavi
e560: 6f 72 20 65 78 68 69 62 69 74 65 64 0a 2a 2a 20 or exhibited.**
e570: 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 when SQLite is c
e580: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 53 51 ompiled with [SQ
e590: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
e5a0: 49 54 5d 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 IT] might become
e5b0: 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c 74 20 the.** default
e5c0: 62 65 68 61 76 69 6f 72 20 69 6e 20 73 6f 6d 65 behavior in some
e5d0: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 20 future release
e5e0: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a of SQLite..**.**
e5f0: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f The sqlite3_os_
e600: 69 6e 69 74 28 29 20 72 6f 75 74 69 6e 65 20 64 init() routine d
e610: 6f 65 73 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 oes operating-sy
e620: 73 74 65 6d 20 73 70 65 63 69 66 69 63 0a 2a 2a stem specific.**
e630: 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
e640: 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 of the SQLite li
e650: 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69 brary. The sqli
e660: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 te3_os_end().**
e670: 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73 20 74 routine undoes t
e680: 68 65 20 65 66 66 65 63 74 20 6f 66 20 73 71 6c he effect of sql
e690: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 ite3_os_init().
e6a0: 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73 0a 2a Typical tasks.*
e6b0: 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 * performed by t
e6c0: 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6e hese routines in
e6d0: 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e clude allocation
e6e0: 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6f 6e or deallocation
e6f0: 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20 72 65 .** of static re
e700: 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69 61 6c sources, initial
e710: 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f 62 61 ization of globa
e720: 6c 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a 2a 20 l variables,.**
e730: 73 65 74 74 69 6e 67 20 75 70 20 61 20 64 65 66 setting up a def
e740: 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 ault [sqlite3_vf
e750: 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 73 65 s] module, or se
e760: 74 74 69 6e 67 20 75 70 0a 2a 2a 20 61 20 64 65 tting up.** a de
e770: 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 fault configurat
e780: 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ion using [sqlit
e790: 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a e3_config()]..**
e7a0: 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 .** The applicat
e7b0: 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 ion should never
e7c0: 20 69 6e 76 6f 6b 65 20 65 69 74 68 65 72 20 73 invoke either s
e7d0: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
e7e0: 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f .** or sqlite3_o
e7f0: 73 5f 65 6e 64 28 29 20 64 69 72 65 63 74 6c 79 s_end() directly
e800: 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 . The applicati
e810: 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 69 on should only i
e820: 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 nvoke.** sqlite3
e830: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e _initialize() an
e840: 64 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f d sqlite3_shutdo
e850: 77 6e 28 29 2e 20 20 54 68 65 20 73 71 6c 69 74 wn(). The sqlit
e860: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 e3_os_init().**
e870: 69 6e 74 65 72 66 61 63 65 20 69 73 20 63 61 6c interface is cal
e880: 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c led automaticall
e890: 79 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 y by sqlite3_ini
e8a0: 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 0a 2a 2a tialize() and.**
e8b0: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
e8c0: 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 73 ) is called by s
e8d0: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
e8e0: 29 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65 0a ). Appropriate.
e8f0: 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
e900: 6e 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f ns for sqlite3_o
e910: 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c s_init() and sql
e920: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a ite3_os_end().**
e930: 20 61 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 are built into
e940: 53 51 4c 69 74 65 20 77 68 65 6e 20 69 74 20 69 SQLite when it i
e950: 73 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72 20 75 s compiled for u
e960: 6e 69 78 2c 20 77 69 6e 64 6f 77 73 2c 20 6f 72 nix, windows, or
e970: 20 6f 73 2f 32 2e 0a 2a 2a 20 57 68 65 6e 20 62 os/2..** When b
e980: 75 69 6c 74 20 66 6f 72 20 6f 74 68 65 72 20 70 uilt for other p
e990: 6c 61 74 66 6f 72 6d 73 20 28 75 73 69 6e 67 20 latforms (using
e9a0: 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 53 5f 4f the [SQLITE_OS_O
e9b0: 54 48 45 52 3d 31 5d 20 63 6f 6d 70 69 6c 65 2d THER=1] compile-
e9c0: 74 69 6d 65 0a 2a 2a 20 6f 70 74 69 6f 6e 29 20 time.** option)
e9d0: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
e9e0: 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 73 75 must supply a su
e9f0: 69 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 itable implement
ea00: 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c ation for.** sql
ea10: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 ite3_os_init() a
ea20: 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e nd sqlite3_os_en
ea30: 64 28 29 2e 20 20 41 6e 20 61 70 70 6c 69 63 61 d(). An applica
ea40: 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 0a 2a 2a tion-supplied.**
ea50: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
ea60: 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e of sqlite3_os_in
ea70: 69 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f it() or sqlite3_
ea80: 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 6d 75 73 74 os_end().** must
ea90: 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
eaa0: 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 61 OK] on success a
eab0: 6e 64 20 73 6f 6d 65 20 6f 74 68 65 72 20 5b 65 nd some other [e
eac0: 72 72 6f 72 20 63 6f 64 65 5d 20 75 70 6f 6e 0a rror code] upon.
ead0: 2a 2a 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 53 ** failure..*/.S
eae0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
eaf0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
eb00: 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 (void);.SQLITE_A
eb10: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
eb20: 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 53 hutdown(void);.S
eb30: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
eb40: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f lite3_os_init(vo
eb50: 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
eb60: 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 int sqlite3_os_e
eb70: 6e 64 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a nd(void);../*.**
eb80: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 CAPI3REF: Confi
eb90: 67 75 72 69 6e 67 20 54 68 65 20 53 51 4c 69 74 guring The SQLit
eba0: 65 20 4c 69 62 72 61 72 79 20 7b 48 31 34 31 30 e Library {H1410
ebb0: 30 7d 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 32 0} <S20000><S302
ebc0: 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
ebd0: 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 TAL.**.** The sq
ebe0: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 lite3_config() i
ebf0: 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 nterface is used
ec00: 20 74 6f 20 6d 61 6b 65 20 67 6c 6f 62 61 6c 20 to make global
ec10: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a configuration.**
ec20: 20 63 68 61 6e 67 65 73 20 74 6f 20 53 51 4c 69 changes to SQLi
ec30: 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 te in order to t
ec40: 75 6e 65 20 53 51 4c 69 74 65 20 74 6f 20 74 68 une SQLite to th
ec50: 65 20 73 70 65 63 69 66 69 63 20 6e 65 65 64 73 e specific needs
ec60: 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 of.** the appli
ec70: 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 cation. The def
ec80: 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 ault configurati
ec90: 6f 6e 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 on is recommende
eca0: 64 20 66 6f 72 20 6d 6f 73 74 0a 2a 2a 20 61 70 d for most.** ap
ecb0: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 73 plications and s
ecc0: 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 o this routine i
ecd0: 73 20 75 73 75 61 6c 6c 79 20 6e 6f 74 20 6e 65 s usually not ne
ece0: 63 65 73 73 61 72 79 2e 20 20 49 74 20 69 73 0a cessary. It is.
ecf0: 2a 2a 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73 ** provided to s
ed00: 75 70 70 6f 72 74 20 72 61 72 65 20 61 70 70 6c upport rare appl
ed10: 69 63 61 74 69 6f 6e 73 20 77 69 74 68 20 75 6e ications with un
ed20: 75 73 75 61 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a usual needs..**.
ed30: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
ed40: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 onfig() interfac
ed50: 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 e is not threads
ed60: 61 66 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63 afe. The applic
ed70: 61 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e ation.** must in
ed80: 73 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68 sure that no oth
ed90: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 er SQLite interf
eda0: 61 63 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 aces are invoked
edb0: 20 62 79 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72 by other.** thr
edc0: 65 61 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74 eads while sqlit
edd0: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72 e3_config() is r
ede0: 75 6e 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72 unning. Further
edf0: 6d 6f 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f more, sqlite3_co
ee00: 6e 66 69 67 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e nfig().** may on
ee10: 6c 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 ly be invoked pr
ee20: 69 6f 72 20 74 6f 20 6c 69 62 72 61 72 79 20 69 ior to library i
ee30: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 73 nitialization us
ee40: 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ing.** [sqlite3_
ee50: 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 6f 72 initialize()] or
ee60: 20 61 66 74 65 72 20 73 68 75 74 64 6f 77 6e 20 after shutdown
ee70: 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 by [sqlite3_shut
ee80: 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 20 4e 6f 74 65 down()]..** Note
ee90: 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 61 74 20 , however, that
eea0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
eeb0: 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 can be called a
eec0: 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a s part of the.**
eed0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
eee0: 6f 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f of an applicatio
eef0: 6e 2d 64 65 66 69 6e 65 64 20 5b 73 71 6c 69 74 n-defined [sqlit
ef00: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 5d 2e 0a 2a e3_os_init()]..*
ef10: 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 *.** The first a
ef20: 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
ef30: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 61 e3_config() is a
ef40: 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51 n integer.** [SQ
ef50: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 LITE_CONFIG_SING
ef60: 4c 45 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 LETHREAD | confi
ef70: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d guration option]
ef80: 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 that determines
ef90: 0a 2a 2a 20 77 68 61 74 20 70 72 6f 70 65 72 74 .** what propert
efa0: 79 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 74 y of SQLite is t
efb0: 6f 20 62 65 20 63 6f 6e 66 69 67 75 72 65 64 2e o be configured.
efc0: 20 20 53 75 62 73 65 71 75 65 6e 74 20 61 72 67 Subsequent arg
efd0: 75 6d 65 6e 74 73 0a 2a 2a 20 76 61 72 79 20 64 uments.** vary d
efe0: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 epending on the
eff0: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 [SQLITE_CONFIG_S
f000: 49 4e 47 4c 45 54 48 52 45 41 44 20 7c 20 63 6f INGLETHREAD | co
f010: 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
f020: 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 on].** in the fi
f030: 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a rst argument..**
f040: 0a 2a 2a 20 57 68 65 6e 20 61 20 63 6f 6e 66 69 .** When a confi
f050: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 guration option
f060: 69 73 20 73 65 74 2c 20 73 71 6c 69 74 65 33 5f is set, sqlite3_
f070: 63 6f 6e 66 69 67 28 29 20 72 65 74 75 72 6e 73 config() returns
f080: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a [SQLITE_OK]..**
f090: 20 49 66 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 If the option i
f0a0: 73 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c s unknown or SQL
f0b0: 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f ite is unable to
f0c0: 20 73 65 74 20 74 68 65 20 6f 70 74 69 6f 6e 0a set the option.
f0d0: 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 ** then this rou
f0e0: 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6e tine returns a n
f0f0: 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 on-zero [error c
f100: 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 ode]..**.** INVA
f110: 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
f120: 31 34 31 30 33 7d 20 41 20 73 75 63 63 65 73 73 14103} A success
f130: 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f ful invocation o
f140: 66 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 f [sqlite3_confi
f150: 67 28 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 g()] shall retur
f160: 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 n.** [S
f170: 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a QLITE_OK]..**.**
f180: 20 7b 48 31 34 31 30 36 7d 20 54 68 65 20 5b 73 {H14106} The [s
f190: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
f1a0: 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c interface shall
f1b0: 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
f1c0: 4d 49 53 55 53 45 5d 0a 2a 2a 20 20 20 20 20 20 MISUSE].**
f1d0: 20 20 20 20 69 66 20 69 74 20 69 73 20 69 6e 76 if it is inv
f1e0: 6f 6b 65 64 20 69 6e 20 62 65 74 77 65 65 6e 20 oked in between
f1f0: 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 calls to [sqlite
f200: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 3_initialize()]
f210: 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 and.**
f220: 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 [sqlite3_shutdow
f230: 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 n()]..**.** {H14
f240: 31 32 30 7d 20 41 20 73 75 63 63 65 73 73 66 75 120} A successfu
f250: 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 l call to [sqlit
f260: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 e3_config]([SQLI
f270: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 TE_CONFIG_SINGLE
f280: 54 48 52 45 41 44 5d 29 0a 2a 2a 20 20 20 20 20 THREAD]).**
f290: 20 20 20 20 20 73 68 61 6c 6c 20 73 65 74 20 74 shall set t
f2a0: 68 65 20 64 65 66 61 75 6c 74 20 5b 74 68 72 65 he default [thre
f2b0: 61 64 69 6e 67 20 6d 6f 64 65 5d 20 74 6f 20 53 ading mode] to S
f2c0: 69 6e 67 6c 65 2d 74 68 72 65 61 64 2e 0a 2a 2a ingle-thread..**
f2d0: 0a 2a 2a 20 7b 48 31 34 31 32 33 7d 20 41 20 73 .** {H14123} A s
f2e0: 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
f2f0: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 o [sqlite3_confi
f300: 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 g]([SQLITE_CONFI
f310: 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d 29 0a G_MULTITHREAD]).
f320: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 6c ** shal
f330: 6c 20 73 65 74 20 74 68 65 20 64 65 66 61 75 6c l set the defaul
f340: 74 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 t [threading mod
f350: 65 5d 20 74 6f 20 4d 75 6c 74 69 2d 74 68 72 65 e] to Multi-thre
f360: 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 32 ad..**.** {H1412
f370: 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 6} A successful
f380: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
f390: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 _config]([SQLITE
f3a0: 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a _CONFIG_SERIALIZ
f3b0: 45 44 5d 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 ED]).**
f3c0: 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 20 64 shall set the d
f3d0: 65 66 61 75 6c 74 20 5b 74 68 72 65 61 64 69 6e efault [threadin
f3e0: 67 20 6d 6f 64 65 5d 20 74 6f 20 53 65 72 69 61 g mode] to Seria
f3f0: 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 lized..**.** {H1
f400: 34 31 32 39 7d 20 41 20 73 75 63 63 65 73 73 66 4129} A successf
f410: 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ul call to [sqli
f420: 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c te3_config]([SQL
f430: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 ITE_CONFIG_MUTEX
f440: 5d 2c 58 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 ],X).**
f450: 20 77 68 65 72 65 20 58 20 69 73 20 61 20 70 6f where X is a po
f460: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 69 74 inter to an init
f470: 69 61 6c 69 7a 65 64 20 5b 73 71 6c 69 74 65 33 ialized [sqlite3
f480: 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a _mutex_methods].
f490: 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 6a 65 ** obje
f4a0: 63 74 20 73 68 61 6c 6c 20 63 61 75 73 65 20 61 ct shall cause a
f4b0: 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 6d 75 ll subsequent mu
f4c0: 74 65 78 20 6f 70 65 72 61 74 69 6f 6e 73 20 70 tex operations p
f4d0: 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 20 20 20 20 erformed.**
f4e0: 20 20 20 20 20 62 79 20 53 51 4c 69 74 65 20 74 by SQLite t
f4f0: 6f 20 75 73 65 20 74 68 65 20 6d 75 74 65 78 20 o use the mutex
f500: 6d 65 74 68 6f 64 73 20 74 68 61 74 20 77 65 72 methods that wer
f510: 65 20 70 72 65 73 65 6e 74 20 69 6e 20 58 0a 2a e present in X.*
f520: 2a 20 20 20 20 20 20 20 20 20 20 64 75 72 69 6e * durin
f530: 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 g the call to [s
f540: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
f550: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 32 7d ..**.** {H14132}
f560: 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 A successful ca
f570: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ll to [sqlite3_c
f580: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 onfig]([SQLITE_C
f590: 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 5d 2c ONFIG_GETMUTEX],
f5a0: 58 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 X).** w
f5b0: 68 65 72 65 20 58 20 69 73 20 61 20 70 6f 69 6e here X is a poin
f5c0: 74 65 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74 ter to an [sqlit
f5d0: 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
f5e0: 5d 20 6f 62 6a 65 63 74 20 0a 2a 2a 20 20 20 20 ] object .**
f5f0: 20 20 20 20 20 20 73 68 61 6c 6c 20 6f 76 65 72 shall over
f600: 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e write the conten
f610: 74 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 75 t of [sqlite3_mu
f620: 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a tex_methods] obj
f630: 65 63 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ect.**
f640: 77 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 6d with the mutex m
f650: 65 74 68 6f 64 73 20 63 75 72 72 65 6e 74 6c 79 ethods currently
f660: 20 69 6e 20 75 73 65 20 62 79 20 53 51 4c 69 74 in use by SQLit
f670: 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 35 e..**.** {H14135
f680: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 } A successful c
f690: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
f6a0: 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f config]([SQLITE_
f6b0: 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2c 4d CONFIG_MALLOC],M
f6c0: 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 ).** wh
f6d0: 65 72 65 20 4d 20 69 73 20 61 20 70 6f 69 6e 74 ere M is a point
f6e0: 65 72 20 74 6f 20 61 6e 20 69 6e 69 74 69 61 6c er to an initial
f6f0: 69 7a 65 64 20 5b 73 71 6c 69 74 65 33 5f 6d 65 ized [sqlite3_me
f700: 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 20 20 m_methods].**
f710: 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 73 68 object sh
f720: 61 6c 6c 20 63 61 75 73 65 20 61 6c 6c 20 73 75 all cause all su
f730: 62 73 65 71 75 65 6e 74 20 6d 65 6d 6f 72 79 20 bsequent memory
f740: 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 70 65 72 61 allocation opera
f750: 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 tions.**
f760: 20 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 53 performed by S
f770: 51 4c 69 74 65 20 74 6f 20 75 73 65 20 74 68 65 QLite to use the
f780: 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 77 65 methods that we
f790: 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 0a 2a re present in .*
f7a0: 2a 20 20 20 20 20 20 20 20 20 20 4d 20 64 75 72 * M dur
f7b0: 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 ing the call to
f7c0: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 [sqlite3_config(
f7d0: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 )]..**.** {H1413
f7e0: 38 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 8} A successful
f7f0: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
f800: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 _config]([SQLITE
f810: 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f _CONFIG_GETMALLO
f820: 43 5d 2c 4d 29 0a 2a 2a 20 20 20 20 20 20 20 20 C],M).**
f830: 20 20 77 68 65 72 65 20 4d 20 69 73 20 61 20 70 where M is a p
f840: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 5b 73 71 ointer to an [sq
f850: 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
f860: 73 5d 20 6f 62 6a 65 63 74 20 73 68 61 6c 6c 0a s] object shall.
f870: 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 ** over
f880: 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e write the conten
f890: 74 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 65 t of [sqlite3_me
f8a0: 6d 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 m_methods] objec
f8b0: 74 20 77 69 74 68 20 0a 2a 2a 20 20 20 20 20 20 t with .**
f8c0: 20 20 20 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 the memory a
f8d0: 6c 6c 6f 63 61 74 69 6f 6e 20 6d 65 74 68 6f 64 llocation method
f8e0: 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 s currently in u
f8f0: 73 65 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20 se by.**
f900: 20 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 SQLite..**.**
f910: 7b 48 31 34 31 34 31 7d 20 41 20 73 75 63 63 65 {H14141} A succe
f920: 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 ssful call to [s
f930: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b qlite3_config]([
f940: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 SQLITE_CONFIG_ME
f950: 4d 53 54 41 54 55 53 5d 2c 31 29 0a 2a 2a 20 20 MSTATUS],1).**
f960: 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 65 6e shall en
f970: 61 62 6c 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 able the memory
f980: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 74 61 74 75 allocation statu
f990: 73 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6c 6f 67 s collection log
f9a0: 69 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 34 ic..**.** {H1414
f9b0: 34 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 4} A successful
f9c0: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
f9d0: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 _config]([SQLITE
f9e0: 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 _CONFIG_MEMSTATU
f9f0: 53 5d 2c 30 29 0a 2a 2a 20 20 20 20 20 20 20 20 S],0).**
fa00: 20 20 73 68 61 6c 6c 20 64 69 73 61 62 6c 65 20 shall disable
fa10: 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
fa20: 61 74 69 6f 6e 20 73 74 61 74 75 73 20 63 6f 6c ation status col
fa30: 6c 65 63 74 69 6f 6e 20 6c 6f 67 69 63 2e 0a 2a lection logic..*
fa40: 2a 0a 2a 2a 20 7b 48 31 34 31 34 37 7d 20 54 68 *.** {H14147} Th
fa50: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
fa60: 69 6f 6e 20 73 74 61 74 75 73 20 63 6f 6c 6c 65 ion status colle
fa70: 63 74 69 6f 6e 20 6c 6f 67 69 63 20 73 68 61 6c ction logic shal
fa80: 6c 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 l be.**
fa90: 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 enabled by defa
faa0: 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 ult..**.** {H141
fab0: 35 30 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 50} A successful
fac0: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
fad0: 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 3_config]([SQLIT
fae0: 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
faf0: 5d 2c 53 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 ],S,Z,N).**
fb00: 20 20 20 20 20 77 68 65 72 65 20 5a 20 61 6e 64 where Z and
fb10: 20 4e 20 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 N are non-negat
fb20: 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64 ive integers and
fb30: 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 20 .** S
fb40: 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
fb50: 61 6e 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 an aligned memor
fb60: 79 20 62 75 66 66 65 72 20 6e 6f 74 20 6c 65 73 y buffer not les
fb70: 73 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 s than.**
fb80: 20 20 20 5a 2a 4e 20 62 79 74 65 73 20 69 6e 20 Z*N bytes in
fb90: 73 69 7a 65 20 73 68 61 6c 6c 20 63 61 75 73 65 size shall cause
fba0: 20 53 20 74 6f 20 62 65 20 75 73 65 64 20 62 79 S to be used by
fbb0: 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
fbc0: 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 [scratch memory
fbd0: 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 66 6f 72 20 allocator] for
fbe0: 61 73 20 6d 61 6e 79 20 61 73 20 4e 20 73 69 6d as many as N sim
fbf0: 75 6c 61 74 61 6e 65 6f 75 73 0a 2a 2a 20 20 20 ulataneous.**
fc00: 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f allocatio
fc10: 6e 73 20 65 61 63 68 20 6f 66 20 73 69 7a 65 20 ns each of size
fc20: 28 5a 20 26 20 7e 37 29 2e 0a 2a 2a 0a 2a 2a 20 (Z & ~7)..**.**
fc30: 7b 48 31 34 31 35 33 7d 20 41 20 73 75 63 63 65 {H14153} A succe
fc40: 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 ssful call to [s
fc50: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b qlite3_config]([
fc60: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 SQLITE_CONFIG_SC
fc70: 52 41 54 43 48 5d 2c 53 2c 5a 2c 4e 29 0a 2a 2a RATCH],S,Z,N).**
fc80: 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65 20 where
fc90: 53 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e S is a NULL poin
fca0: 74 65 72 20 73 68 61 6c 6c 20 64 69 73 61 62 6c ter shall disabl
fcb0: 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 e the.**
fcc0: 20 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 [scratch memor
fcd0: 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 0a 2a 2a y allocator]..**
fce0: 0a 2a 2a 20 7b 48 31 34 31 35 36 7d 20 41 20 73 .** {H14156} A s
fcf0: 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
fd00: 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 o.** [s
fd10: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b qlite3_config]([
fd20: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
fd30: 47 45 43 41 43 48 45 5d 2c 53 2c 5a 2c 4e 29 0a GECACHE],S,Z,N).
fd40: 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 65 72 ** wher
fd50: 65 20 5a 20 61 6e 64 20 4e 20 61 72 65 20 6e 6f e Z and N are no
fd60: 6e 2d 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67 n-negative integ
fd70: 65 72 73 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 ers and .**
fd80: 20 20 20 20 20 53 20 69 73 20 61 20 70 6f 69 6e S is a poin
fd90: 74 65 72 20 74 6f 20 61 6e 20 61 6c 69 67 6e 65 ter to an aligne
fda0: 64 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 d memory buffer
fdb0: 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a not less than.**
fdc0: 20 20 20 20 20 20 20 20 20 20 5a 2a 4e 20 62 79 Z*N by
fdd0: 74 65 73 20 69 6e 20 73 69 7a 65 20 73 68 61 6c tes in size shal
fde0: 6c 20 63 61 75 73 65 20 53 20 74 6f 20 62 65 20 l cause S to be
fdf0: 75 73 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 used by the.**
fe00: 20 20 20 20 20 20 20 20 5b 70 61 67 65 63 61 63 [pagecac
fe10: 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
fe20: 74 6f 72 5d 20 66 6f 72 20 61 73 20 6d 61 6e 79 tor] for as many
fe30: 20 61 73 20 4e 20 73 69 6d 75 6c 61 74 61 6e 65 as N simulatane
fe40: 6f 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ous.**
fe50: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 65 61 63 68 allocations each
fe60: 20 6f 66 20 73 69 7a 65 20 28 5a 20 26 20 7e 37 of size (Z & ~7
fe70: 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 35 39 )..**.** {H14159
fe80: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 } A successful c
fe90: 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 all to.**
fea0: 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 [sqlite3_conf
feb0: 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 ig]([SQLITE_CONF
fec0: 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2c 53 2c IG_PAGECACHE],S,
fed0: 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 Z,N).**
fee0: 20 77 68 65 72 65 20 53 20 69 73 20 61 20 4e 55 where S is a NU
fef0: 4c 4c 20 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c LL pointer shall
ff00: 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 disable the.**
ff10: 20 20 20 20 20 20 20 20 20 5b 70 61 67 65 63 61 [pageca
ff20: 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 che memory alloc
ff30: 61 74 6f 72 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ator]..**.** {H1
ff40: 34 31 36 32 7d 20 41 20 73 75 63 63 65 73 73 66 4162} A successf
ff50: 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ul call to [sqli
ff60: 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c te3_config]([SQL
ff70: 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 5d ITE_CONFIG_HEAP]
ff80: 2c 48 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 ,H,Z,N).**
ff90: 20 20 20 20 77 68 65 72 65 20 5a 20 61 6e 64 20 where Z and
ffa0: 4e 20 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 N are non-negati
ffb0: 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 ve integers and
ffc0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 48 20 69 .** H i
ffd0: 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
ffe0: 6e 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 79 n aligned memory
fff0: 20 62 75 66 66 65 72 20 6e 6f 74 20 6c 65 73 73 buffer not less
10000 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 than.**
10010 20 20 5a 20 62 79 74 65 73 20 69 6e 20 73 69 7a Z bytes in siz
10020 65 20 73 68 61 6c 6c 20 65 6e 61 62 6c 65 20 74 e shall enable t
10030 68 65 20 5b 6d 65 6d 73 79 73 35 5d 20 6d 65 6d he [memsys5] mem
10040 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a ory allocator.**
10050 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 63 61 and ca
10060 75 73 65 20 69 74 20 74 6f 20 75 73 65 20 62 75 use it to use bu
10070 66 66 65 72 20 53 20 61 73 20 69 74 73 20 6d 65 ffer S as its me
10080 6d 6f 72 79 20 73 6f 75 72 63 65 20 61 6e 64 20 mory source and
10090 74 6f 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20 to use.**
100a0 20 20 20 61 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c a minimum all
100b0 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 6f 66 20 ocation size of
100c0 4e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 36 35 N..**.** {H14165
100d0 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 } A successful c
100e0 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
100f0 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f config]([SQLITE_
10100 43 4f 4e 46 49 47 5f 48 45 41 50 5d 2c 48 2c 5a CONFIG_HEAP],H,Z
10110 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ,N).**
10120 77 68 65 72 65 20 48 20 69 73 20 61 20 4e 55 4c where H is a NUL
10130 4c 20 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c 20 L pointer shall
10140 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 20 disable the.**
10150 20 20 20 20 20 20 20 20 5b 6d 65 6d 73 79 73 35 [memsys5
10160 5d 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 ] memory allocat
10170 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 36 or..**.** {H1416
10180 38 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 8} A successful
10190 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
101a0 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 _config]([SQLITE
101b0 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 _CONFIG_LOOKASID
101c0 45 5d 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 E],Z,N).**
101d0 20 20 20 20 73 68 61 6c 6c 20 63 61 75 73 65 20 shall cause
101e0 74 68 65 20 64 65 66 61 75 6c 74 20 5b 6c 6f 6f the default [loo
101f0 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c kaside memory al
10200 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e 66 69 67 75 locator] configu
10210 72 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 ration.**
10220 20 20 20 66 6f 72 20 6e 65 77 20 5b 64 61 74 61 for new [data
10230 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
10240 5d 20 74 6f 20 62 65 20 4e 20 73 6c 6f 74 73 20 ] to be N slots
10250 6f 66 20 5a 20 62 79 74 65 73 20 65 61 63 68 2e of Z bytes each.
10260 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 .*/.SQLITE_API S
10270 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
10280 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 AL int sqlite3_c
10290 6f 6e 66 69 67 28 69 6e 74 2c 20 2e 2e 2e 29 3b onfig(int, ...);
102a0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
102b0 3a 20 43 6f 6e 66 69 67 75 72 65 20 64 61 74 61 : Configure data
102c0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
102d0 20 20 7b 48 31 34 32 30 30 7d 20 3c 53 32 30 30 {H14200} <S200
102e0 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
102f0 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 TAL.**.** The sq
10300 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 lite3_db_config(
10310 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 ) interface is u
10320 73 65 64 20 74 6f 20 6d 61 6b 65 20 63 6f 6e 66 sed to make conf
10330 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 iguration.** cha
10340 6e 67 65 73 20 74 6f 20 61 20 5b 64 61 74 61 62 nges to a [datab
10350 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e ase connection].
10360 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 The interface
10370 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 0a 2a 2a is similar to.**
10380 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 [sqlite3_config
10390 28 29 5d 20 65 78 63 65 70 74 20 74 68 61 74 20 ()] except that
103a0 74 68 65 20 63 68 61 6e 67 65 73 20 61 70 70 6c the changes appl
103b0 79 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a y to a single.**
103c0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
103d0 63 74 69 6f 6e 5d 20 28 73 70 65 63 69 66 69 65 ction] (specifie
103e0 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 d in the first a
103f0 72 67 75 6d 65 6e 74 29 2e 20 20 54 68 65 0a 2a rgument). The.*
10400 2a 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e * sqlite3_db_con
10410 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 fig() interface
10420 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 can only be used
10430 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 immediately aft
10440 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 er.** the databa
10450 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 se connection is
10460 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b created using [
10470 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c sqlite3_open()],
10480 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 .** [sqlite3_ope
10490 6e 31 36 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69 n16()], or [sqli
104a0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20 te3_open_v2()].
104b0 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f .**.** The seco
104c0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 nd argument to s
104d0 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 qlite3_db_config
104e0 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 73 20 74 68 (D,V,...) is th
104f0 65 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 61 74 69 e.** configurati
10500 6f 6e 20 76 65 72 62 20 2d 20 61 6e 20 69 6e 74 on verb - an int
10510 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 20 69 eger code that i
10520 6e 64 69 63 61 74 65 73 20 77 68 61 74 0a 2a 2a ndicates what.**
10530 20 61 73 70 65 63 74 20 6f 66 20 74 68 65 20 5b aspect of the [
10540 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
10550 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 63 6f ion] is being co
10560 6e 66 69 67 75 72 65 64 2e 0a 2a 2a 20 54 68 65 nfigured..** The
10570 20 6f 6e 6c 79 20 63 68 6f 69 63 65 20 66 6f 72 only choice for
10580 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 5b this value is [
10590 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f SQLITE_DBCONFIG_
105a0 4c 4f 4f 4b 41 53 49 44 45 5d 2e 0a 2a 2a 20 4e LOOKASIDE]..** N
105b0 65 77 20 76 65 72 62 73 20 61 72 65 20 6c 69 6b ew verbs are lik
105c0 65 6c 79 20 74 6f 20 62 65 20 61 64 64 65 64 20 ely to be added
105d0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
105e0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a es of SQLite..**
105f0 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 72 67 75 Additional argu
10600 6d 65 6e 74 73 20 64 65 70 65 6e 64 20 6f 6e 20 ments depend on
10610 74 68 65 20 76 65 72 62 2e 0a 2a 2a 0a 2a 2a 20 the verb..**.**
10620 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
10630 2a 20 7b 48 31 34 32 30 33 7d 20 41 20 63 61 6c * {H14203} A cal
10640 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 64 62 l to [sqlite3_db
10650 5f 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 _config(D,V,...)
10660 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 5b ] shall return [
10670 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 20 20 SQLITE_OK].**
10680 20 20 20 20 20 20 20 69 66 20 61 6e 64 20 6f 6e if and on
10690 6c 79 20 69 66 20 74 68 65 20 63 61 6c 6c 20 69 ly if the call i
106a0 73 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a s successful..**
106b0 0a 2a 2a 20 7b 48 31 34 32 30 36 7d 20 49 66 20 .** {H14206} If
106c0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 6c 6f 74 one or more slot
106d0 73 20 6f 66 20 74 68 65 20 5b 6c 6f 6f 6b 61 73 s of the [lookas
106e0 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ide memory alloc
106f0 61 74 6f 72 5d 20 66 6f 72 0a 2a 2a 20 20 20 20 ator] for.**
10700 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 [database
10710 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 61 72 connection] D ar
10720 65 20 69 6e 20 75 73 65 2c 20 74 68 65 6e 20 61 e in use, then a
10730 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 call to.**
10740 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 64 62 [sqlite3_db
10750 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c 49 _config](D,[SQLI
10760 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b TE_DBCONFIG_LOOK
10770 41 53 49 44 45 5d 2c 2e 2e 2e 29 20 73 68 61 6c ASIDE],...) shal
10780 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 61 l.** fa
10790 69 6c 20 77 69 74 68 20 61 6e 20 5b 53 51 4c 49 il with an [SQLI
107a0 54 45 5f 42 55 53 59 5d 20 72 65 74 75 72 6e 20 TE_BUSY] return
107b0 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 code..**.** {H14
107c0 32 30 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 209} A successfu
107d0 6c 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 20 l call to .**
107e0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
107f0 64 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 db_config](D,[SQ
10800 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f LITE_DBCONFIG_LO
10810 4f 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 20 OKASIDE],B,Z,N)
10820 77 68 65 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 where.**
10830 20 20 44 20 69 73 20 61 6e 20 6f 70 65 6e 20 5b D is an open [
10840 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
10850 69 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 20 4e ion] and Z and N
10860 20 61 72 65 20 70 6f 73 69 74 69 76 65 0a 2a 2a are positive.**
10870 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 67 65 intege
10880 72 73 20 61 6e 64 20 42 20 69 73 20 61 6e 20 61 rs and B is an a
10890 6c 69 67 6e 65 64 20 62 75 66 66 65 72 20 61 74 ligned buffer at
108a0 20 6c 65 61 73 74 20 5a 2a 4e 20 62 79 74 65 73 least Z*N bytes
108b0 20 69 6e 20 73 69 7a 65 0a 2a 2a 20 20 20 20 20 in size.**
108c0 20 20 20 20 20 73 68 61 6c 6c 20 63 61 75 73 65 shall cause
108d0 20 74 68 65 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 the [lookaside
108e0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
108f0 5d 20 66 6f 72 20 44 20 74 6f 20 75 73 65 20 62 ] for D to use b
10900 75 66 66 65 72 20 42 20 0a 2a 2a 20 20 20 20 20 uffer B .**
10910 20 20 20 20 20 77 69 74 68 20 4e 20 73 6c 6f 74 with N slot
10920 73 20 6f 66 20 5a 20 62 79 74 65 73 20 65 61 63 s of Z bytes eac
10930 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 32 31 32 h..**.** {H14212
10940 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 } A successful c
10950 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 20 20 20 20 all to .**
10960 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f [sqlite3_db_
10970 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c 49 54 config](D,[SQLIT
10980 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 E_DBCONFIG_LOOKA
10990 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 20 77 68 65 SIDE],B,Z,N) whe
109a0 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 44 re.** D
109b0 20 69 73 20 61 6e 20 6f 70 65 6e 20 5b 64 61 74 is an open [dat
109c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
109d0 5d 20 61 6e 64 20 5a 20 61 6e 64 20 4e 20 61 72 ] and Z and N ar
109e0 65 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 20 20 e positive.**
109f0 20 20 20 20 20 20 20 69 6e 74 65 67 65 72 73 20 integers
10a00 61 6e 64 20 42 20 69 73 20 4e 55 4c 4c 20 70 6f and B is NULL po
10a10 69 6e 74 65 72 20 73 68 61 6c 6c 20 63 61 75 73 inter shall caus
10a20 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 e the.**
10a30 20 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d [lookaside mem
10a40 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 66 ory allocator] f
10a50 6f 72 20 44 20 74 6f 20 61 20 6f 62 74 61 69 6e or D to a obtain
10a60 20 5a 2a 4e 20 62 79 74 65 20 62 75 66 66 65 72 Z*N byte buffer
10a70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f .** fro
10a80 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20 6d 65 m the primary me
10a90 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 61 mory allocator a
10aa0 6e 64 20 75 73 65 20 74 68 61 74 20 62 75 66 66 nd use that buff
10ab0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 er.** w
10ac0 69 74 68 20 4e 20 6c 6f 6f 6b 61 73 69 64 65 20 ith N lookaside
10ad0 73 6c 6f 74 73 20 6f 66 20 5a 20 62 79 74 65 73 slots of Z bytes
10ae0 20 65 61 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 each..**.** {H1
10af0 34 32 31 35 7d 20 41 20 73 75 63 63 65 73 73 66 4215} A successf
10b00 75 6c 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 ul call to .**
10b10 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
10b20 5f 64 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 _db_config](D,[S
10b30 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c QLITE_DBCONFIG_L
10b40 4f 4f 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 OOKASIDE],B,Z,N)
10b50 20 77 68 65 72 65 0a 2a 2a 20 20 20 20 20 20 20 where.**
10b60 20 20 20 44 20 69 73 20 61 6e 20 6f 70 65 6e 20 D is an open
10b70 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
10b80 74 69 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 20 tion] and Z and
10b90 4e 20 61 72 65 20 7a 65 72 6f 20 73 68 61 6c 6c N are zero shall
10ba0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 69 73 .** dis
10bb0 61 62 6c 65 20 74 68 65 20 5b 6c 6f 6f 6b 61 73 able the [lookas
10bc0 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ide memory alloc
10bd0 61 74 6f 72 5d 20 66 6f 72 20 44 2e 0a 2a 2a 0a ator] for D..**.
10be0 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 **.*/.SQLITE_API
10bf0 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 SQLITE_EXPERIME
10c00 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 NTAL int sqlite3
10c10 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 _db_config(sqlit
10c20 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e e3*, int op, ...
10c30 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
10c40 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 EF: Memory Alloc
10c50 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b ation Routines {
10c60 48 31 30 31 35 35 7d 20 3c 53 32 30 31 32 30 3e H10155} <S20120>
10c70 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
10c80 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e .**.** An instan
10c90 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 ce of this objec
10ca0 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e t defines the in
10cb0 74 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 20 terface between
10cc0 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f SQLite.** and lo
10cd0 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
10ce0 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e llocation routin
10cf0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f es..**.** This o
10d00 62 6a 65 63 74 20 69 73 20 75 73 65 64 20 69 6e bject is used in
10d10 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 only one place
10d20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e in the SQLite in
10d30 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f terface..** A po
10d40 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 inter to an inst
10d50 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a ance of this obj
10d60 65 63 74 20 69 73 20 74 68 65 20 61 72 67 75 6d ect is the argum
10d70 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 ent to.** [sqlit
10d80 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 e3_config()] whe
10d90 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 n the configurat
10da0 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a ion option is.**
10db0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
10dc0 4d 41 4c 4c 4f 43 5d 2e 20 20 42 79 20 63 72 65 MALLOC]. By cre
10dd0 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63 ating an instanc
10de0 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
10df0 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20 .** and passing
10e00 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 it to [sqlite3_c
10e10 6f 6e 66 69 67 28 29 5d 20 64 75 72 69 6e 67 20 onfig()] during
10e20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 61 configuration, a
10e30 6e 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e n.** application
10e40 20 63 61 6e 20 73 70 65 63 69 66 79 20 61 6e 20 can specify an
10e50 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 65 6d 6f alternative memo
10e60 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
10e70 62 73 79 73 74 65 6d 0a 2a 2a 20 66 6f 72 20 53 bsystem.** for S
10e80 51 4c 69 74 65 20 74 6f 20 75 73 65 20 66 6f 72 QLite to use for
10e90 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 all of its dyna
10ea0 6d 69 63 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 mic memory needs
10eb0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
10ec0 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73 20 77 t SQLite comes w
10ed0 69 74 68 20 61 20 62 75 69 6c 74 2d 69 6e 20 6d ith a built-in m
10ee0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
10ef0 74 68 61 74 20 69 73 0a 2a 2a 20 70 65 72 66 65 that is.** perfe
10f00 63 74 6c 79 20 61 64 65 71 75 61 74 65 20 66 6f ctly adequate fo
10f10 72 20 74 68 65 20 6f 76 65 72 77 68 65 6c 6d 69 r the overwhelmi
10f20 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 61 ng majority of a
10f30 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 pplications.** a
10f40 6e 64 20 74 68 61 74 20 74 68 69 73 20 6f 62 6a nd that this obj
10f50 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 73 65 66 ect is only usef
10f60 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 6d 69 6e ul to a tiny min
10f70 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69 63 61 ority of applica
10f80 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 20 73 70 tions.** with sp
10f90 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d 6f 72 79 ecialized memory
10fa0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 allocation requ
10fb0 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 69 73 20 irements. This
10fc0 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 61 6c 73 object is.** als
10fd0 6f 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 o used during te
10fe0 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 74 65 20 sting of SQLite
10ff0 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 65 63 in order to spec
11000 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 ify an alternati
11010 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c ve.** memory all
11020 6f 63 61 74 6f 72 20 74 68 61 74 20 73 69 6d 75 ocator that simu
11030 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f 75 74 lates memory out
11040 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 -of-memory condi
11050 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72 64 65 tions in.** orde
11060 72 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 r to verify that
11070 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65 72 73 SQLite recovers
11080 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72 6f 6d gracefully from
11090 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 69 74 69 such.** conditi
110a0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ons..**.** The x
110b0 4d 61 6c 6c 6f 63 2c 20 78 46 72 65 65 2c 20 61 Malloc, xFree, a
110c0 6e 64 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74 68 nd xRealloc meth
110d0 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b 20 6c 69 ods must work li
110e0 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 ke the.** malloc
110f0 28 29 2c 20 66 72 65 65 28 29 2c 20 61 6e 64 20 (), free(), and
11100 72 65 61 6c 6c 6f 63 28 29 20 66 75 6e 63 74 69 realloc() functi
11110 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 ons from the sta
11120 6e 64 61 72 64 20 6c 69 62 72 61 72 79 2e 0a 2a ndard library..*
11130 2a 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f 75 6c *.** xSize shoul
11140 64 20 72 65 74 75 72 6e 20 74 68 65 20 61 6c 6c d return the all
11150 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 ocated size of a
11160 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
11170 6f 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 on.** previously
11180 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 78 obtained from x
11190 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61 6c 6c Malloc or xReall
111a0 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 oc. The allocat
111b0 65 64 20 73 69 7a 65 0a 2a 2a 20 69 73 20 61 6c ed size.** is al
111c0 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 61 73 ways at least as
111d0 20 62 69 67 20 61 73 20 74 68 65 20 72 65 71 75 big as the requ
111e0 65 73 74 65 64 20 73 69 7a 65 20 62 75 74 20 6d ested size but m
111f0 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0a 2a 2a ay be larger..**
11200 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e 64 75 70 .** The xRoundup
11210 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 method returns
11220 77 68 61 74 20 77 6f 75 6c 64 20 62 65 20 74 68 what would be th
11230 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 e allocated size
11240 20 6f 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72 79 20 of.** a memory
11250 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 69 76 65 6e allocation given
11260 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 a particular re
11270 71 75 65 73 74 65 64 20 73 69 7a 65 2e 20 20 4d quested size. M
11280 6f 73 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c ost memory.** al
11290 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 locators round u
112a0 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 p memory allocat
112b0 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20 74 6f ions at least to
112c0 20 74 68 65 20 6e 65 78 74 20 6d 75 6c 74 69 70 the next multip
112d0 6c 65 0a 2a 2a 20 6f 66 20 38 2e 20 20 53 6f 6d le.** of 8. Som
112e0 65 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75 e allocators rou
112f0 6e 64 20 75 70 20 74 6f 20 61 20 6c 61 72 67 65 nd up to a large
11300 72 20 6d 75 6c 74 69 70 6c 65 20 6f 72 20 74 6f r multiple or to
11310 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a a power of 2..*
11320 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 20 6d *.** The xInit m
11330 65 74 68 6f 64 20 69 6e 69 74 69 61 6c 69 7a 65 ethod initialize
11340 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c s the memory all
11350 6f 63 61 74 6f 72 2e 20 20 28 46 6f 72 20 65 78 ocator. (For ex
11360 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d 69 67 ample,.** it mig
11370 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 ht allocate any
11380 72 65 71 75 69 72 65 20 6d 75 74 65 78 65 73 20 require mutexes
11390 6f 72 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e or initialize in
113a0 74 65 72 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 73 ternal data.** s
113b0 74 72 75 63 74 75 72 65 73 2e 20 20 54 68 65 20 tructures. The
113c0 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 xShutdown method
113d0 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 6e 64 is invoked (ind
113e0 69 72 65 63 74 6c 79 29 20 62 79 0a 2a 2a 20 5b irectly) by.** [
113f0 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
11400 28 29 5d 20 61 6e 64 20 73 68 6f 75 6c 64 20 64 ()] and should d
11410 65 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 72 65 eallocate any re
11420 73 6f 75 72 63 65 73 20 61 63 71 75 69 72 65 64 sources acquired
11430 0a 2a 2a 20 62 79 20 78 49 6e 69 74 2e 20 20 54 .** by xInit. T
11440 68 65 20 70 41 70 70 44 61 74 61 20 70 6f 69 6e he pAppData poin
11450 74 65 72 20 69 73 20 75 73 65 64 20 61 73 20 74 ter is used as t
11460 68 65 20 6f 6e 6c 79 20 70 61 72 61 6d 65 74 65 he only paramete
11470 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 74 20 61 6e r to.** xInit an
11480 64 20 78 53 68 75 74 64 6f 77 6e 2e 0a 2a 2f 0a d xShutdown..*/.
11490 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
114a0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
114b0 64 73 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d ds sqlite3_mem_m
114c0 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 ethods;.struct s
114d0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
114e0 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 ds {. void *(*x
114f0 4d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 20 20 Malloc)(int);
11500 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 /* Memory
11510 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 allocation funct
11520 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a ion */. void (*
11530 78 46 72 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 xFree)(void*);
11540 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 /* Free
11550 61 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 a prior allocati
11560 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 28 2a on */. void *(*
11570 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c xRealloc)(void*,
11580 69 6e 74 29 3b 20 20 2f 2a 20 52 65 73 69 7a 65 int); /* Resize
11590 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a an allocation *
115a0 2f 0a 20 20 69 6e 74 20 28 2a 78 53 69 7a 65 29 /. int (*xSize)
115b0 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 (void*);
115c0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 /* Return the
115d0 20 73 69 7a 65 20 6f 66 20 61 6e 20 61 6c 6c 6f size of an allo
115e0 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 cation */. int
115f0 28 2a 78 52 6f 75 6e 64 75 70 29 28 69 6e 74 29 (*xRoundup)(int)
11600 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f ; /* Ro
11610 75 6e 64 20 75 70 20 72 65 71 75 65 73 74 20 73 und up request s
11620 69 7a 65 20 74 6f 20 61 6c 6c 6f 63 61 74 69 6f ize to allocatio
11630 6e 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 n size */. int
11640 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b (*xInit)(void*);
11650 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
11660 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d itialize the mem
11670 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f ory allocator */
11680 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 . void (*xShutd
11690 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 own)(void*);
116a0 20 20 2f 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a /* Deinitializ
116b0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c e the memory all
116c0 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 ocator */. void
116d0 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 20 20 *pAppData;
116e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
116f0 67 75 6d 65 6e 74 20 74 6f 20 78 49 6e 69 74 28 gument to xInit(
11700 29 20 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 28 ) and xShutdown(
11710 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ) */.};../*.** C
11720 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 API3REF: Configu
11730 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b ration Options {
11740 48 31 30 31 36 30 7d 20 3c 53 32 30 30 30 30 3e H10160} <S20000>
11750 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
11760 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e .**.** These con
11770 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 stants are the a
11780 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 vailable integer
11790 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
117a0 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 ptions that.** c
117b0 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 73 20 an be passed as
117c0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
117d0 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 nt to the [sqlit
117e0 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 e3_config()] int
117f0 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 erface..**.** Ne
11800 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 w configuration
11810 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 options may be a
11820 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 dded in future r
11830 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 eleases of SQLit
11840 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 e..** Existing c
11850 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 onfiguration opt
11860 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 ions might be di
11870 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 scontinued. App
11880 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f lications.** sho
11890 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 uld check the re
118a0 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b turn code from [
118b0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
118c0 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 ] to make sure t
118d0 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 hat.** the call
118e0 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 worked. The [sq
118f0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 lite3_config()]
11900 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 interface will r
11910 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a eturn a.** non-z
11920 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d ero [error code]
11930 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 if a discontinu
11940 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 ed or unsupporte
11950 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 d configuration
11960 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 option.** is inv
11970 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e oked..**.** <dl>
11980 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
11990 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 ONFIG_SINGLETHRE
119a0 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 AD</dt>.** <dd>T
119b0 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 here are no argu
119c0 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 ments to this op
119d0 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 tion. This opti
119e0 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 61 on disables.** a
119f0 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 6e 64 20 ll mutexing and
11a00 70 75 74 73 20 53 51 4c 69 74 65 20 69 6e 74 6f puts SQLite into
11a10 20 61 20 6d 6f 64 65 20 77 68 65 72 65 20 69 74 a mode where it
11a20 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 can only be use
11a30 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e 67 6c 65 d.** by a single
11a40 20 74 68 72 65 61 64 2e 3c 2f 64 64 3e 0a 2a 2a thread.</dd>.**
11a50 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
11a60 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 ONFIG_MULTITHREA
11a70 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
11a80 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d ere are no argum
11a90 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 ents to this opt
11aa0 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f ion. This optio
11ab0 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 6d 75 n disables.** mu
11ac0 74 65 78 69 6e 67 20 6f 6e 20 5b 64 61 74 61 62 texing on [datab
11ad0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
11ae0 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 and [prepared st
11af0 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73 atement] objects
11b00 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 ..** The applica
11b10 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 tion is responsi
11b20 62 6c 65 20 66 6f 72 20 73 65 72 69 61 6c 69 7a ble for serializ
11b30 69 6e 67 20 61 63 63 65 73 73 20 74 6f 0a 2a 2a ing access to.**
11b40 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
11b50 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 ctions] and [pre
11b60 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
11b70 5d 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d 75 ]. But other mu
11b80 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61 texes.** are ena
11b90 62 6c 65 64 20 73 6f 20 74 68 61 74 20 53 51 4c bled so that SQL
11ba0 69 74 65 20 77 69 6c 6c 20 62 65 20 73 61 66 65 ite will be safe
11bb0 20 74 6f 20 75 73 65 20 69 6e 20 61 20 6d 75 6c to use in a mul
11bc0 74 69 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 65 ti-threaded.** e
11bd0 6e 76 69 72 6f 6e 6d 65 6e 74 20 61 73 20 6c 6f nvironment as lo
11be0 6e 67 20 61 73 20 6e 6f 20 74 77 6f 20 74 68 72 ng as no two thr
11bf0 65 61 64 73 20 61 74 74 65 6d 70 74 20 74 6f 20 eads attempt to
11c00 75 73 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 use the same.**
11c10 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
11c20 74 69 6f 6e 5d 20 61 74 20 74 68 65 20 73 61 6d tion] at the sam
11c30 65 20 74 69 6d 65 2e 20 20 53 65 65 20 74 68 65 e time. See the
11c40 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
11c50 5d 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 ].** documentati
11c60 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 on for additiona
11c70 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f l information.</
11c80 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
11c90 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 LITE_CONFIG_SERI
11ca0 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c ALIZED</dt>.** <
11cb0 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 dd>There are no
11cc0 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 arguments to thi
11cd0 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 s option. This
11ce0 6f 70 74 69 6f 6e 20 65 6e 61 62 6c 65 73 0a 2a option enables.*
11cf0 2a 20 61 6c 6c 20 6d 75 74 65 78 65 73 20 69 6e * all mutexes in
11d00 63 6c 75 64 69 6e 67 20 74 68 65 20 72 65 63 75 cluding the recu
11d10 72 73 69 76 65 0a 2a 2a 20 6d 75 74 65 78 65 73 rsive.** mutexes
11d20 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f on [database co
11d30 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 nnection] and [p
11d40 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
11d50 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 49 t] objects..** I
11d60 6e 20 74 68 69 73 20 6d 6f 64 65 20 28 77 68 69 n this mode (whi
11d70 63 68 20 69 73 20 74 68 65 20 64 65 66 61 75 6c ch is the defaul
11d80 74 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 t when SQLite is
11d90 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a compiled with.*
11da0 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 * [SQLITE_THREAD
11db0 53 41 46 45 3d 31 5d 29 20 74 68 65 20 53 51 4c SAFE=1]) the SQL
11dc0 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c ite library will
11dd0 20 69 74 73 65 6c 66 20 73 65 72 69 61 6c 69 7a itself serializ
11de0 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 5b e access.** to [
11df0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
11e00 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 ions] and [prepa
11e10 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 red statements]
11e20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 so that the.** a
11e30 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20 66 72 pplication is fr
11e40 65 65 20 74 6f 20 75 73 65 20 74 68 65 20 73 61 ee to use the sa
11e50 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e me [database con
11e60 6e 65 63 74 69 6f 6e 5d 20 6f 72 20 74 68 65 0a nection] or the.
11e70 2a 2a 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 ** same [prepare
11e80 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 d statement] in
11e90 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 different thread
11ea0 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 s at the same ti
11eb0 6d 65 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b me..** See the [
11ec0 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 threading mode]
11ed0 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f documentation fo
11ee0 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
11ef0 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a ormation.</dd>.*
11f00 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
11f10 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64 CONFIG_MALLOC</d
11f20 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
11f30 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 ption takes a si
11f40 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 ngle argument wh
11f50 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ich is a pointer
11f60 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e to an.** instan
11f70 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 ce of the [sqlit
11f80 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 e3_mem_methods]
11f90 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 structure. The
11fa0 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 argument specifi
11fb0 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 es.** alternativ
11fc0 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f e low-level memo
11fd0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f ry allocation ro
11fe0 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65 utines to be use
11ff0 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a d in place of.**
12000 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
12010 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 cation routines
12020 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 built into SQLit
12030 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 e.</dd>.**.** <d
12040 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t>SQLITE_CONFIG_
12050 47 45 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a GETMALLOC</dt>.*
12060 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f * <dd>This optio
12070 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 n takes a single
12080 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 argument which
12090 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
120a0 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f an.** instance o
120b0 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d f the [sqlite3_m
120c0 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 em_methods] stru
120d0 63 74 75 72 65 2e 20 20 54 68 65 20 5b 73 71 6c cture. The [sql
120e0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
120f0 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 ].** structure i
12100 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 s filled with th
12110 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 e currently defi
12120 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ned memory alloc
12130 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a ation routines..
12140 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 ** This option c
12150 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 an be used to ov
12160 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 erload the defau
12170 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 lt memory alloca
12180 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 tion.** routines
12190 20 77 69 74 68 20 61 20 77 72 61 70 70 65 72 20 with a wrapper
121a0 74 68 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e 73 that simulations
121b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
121c0 6f 6e 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a 2a on failure or.**
121d0 20 74 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20 75 tracks memory u
121e0 73 61 67 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c sage, for exampl
121f0 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 e.</dd>.**.** <d
12200 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t>SQLITE_CONFIG_
12210 4d 45 4d 53 54 41 54 55 53 3c 2f 64 74 3e 0a 2a MEMSTATUS</dt>.*
12220 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f * <dd>This optio
12230 6e 20 74 61 6b 65 73 20 73 69 6e 67 6c 65 20 61 n takes single a
12240 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 rgument of type
12250 69 6e 74 2c 20 69 6e 74 65 72 70 72 65 74 65 64 int, interpreted
12260 20 61 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61 as a .** boolea
12270 6e 2c 20 77 68 69 63 68 20 65 6e 61 62 6c 65 73 n, which enables
12280 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 or disables the
12290 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 6d collection of m
122a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
122b0 20 0a 2a 2a 20 73 74 61 74 69 73 74 69 63 73 2e .** statistics.
122c0 20 57 68 65 6e 20 64 69 73 61 62 6c 65 64 2c 20 When disabled,
122d0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 the following SQ
122e0 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 Lite interfaces
122f0 62 65 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d 6f become .** non-o
12300 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 20 perational:.**
12310 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 <ul>.** <li>
12320 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f [sqlite3_memory_
12330 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 used()].** <li
12340 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 > [sqlite3_memor
12350 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d 0a 2a y_highwater()].*
12360 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 * <li> [sqlite
12370 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 3_soft_heap_limi
12380 74 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b t()].** <li> [
12390 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 sqlite3_status()
123a0 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a 2a 20 ].** </ul>.**
123b0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
123c0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 SQLITE_CONFIG_SC
123d0 52 41 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 RATCH</dt>.** <d
123e0 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 d>This option sp
123f0 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69 63 ecifies a static
12400 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 memory buffer t
12410 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75 hat SQLite can u
12420 73 65 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74 63 se for.** scratc
12430 68 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72 65 h memory. There
12440 20 61 72 65 20 74 68 72 65 65 20 61 72 67 75 6d are three argum
12450 65 6e 74 73 3a 20 20 41 20 70 6f 69 6e 74 65 72 ents: A pointer
12460 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c 20 to the memory,
12470 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 the.** size of e
12480 61 63 68 20 73 63 72 61 74 63 68 20 62 75 66 66 ach scratch buff
12490 65 72 20 28 73 7a 29 2c 20 61 6e 64 20 74 68 65 er (sz), and the
124a0 20 6e 75 6d 62 65 72 20 6f 66 20 62 75 66 66 65 number of buffe
124b0 72 73 20 28 4e 29 2e 20 20 54 68 65 20 73 7a 0a rs (N). The sz.
124c0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 ** argument must
124d0 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f be a multiple o
124e0 66 20 31 36 2e 20 54 68 65 20 73 7a 20 70 61 72 f 16. The sz par
124f0 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 ameter should be
12500 20 61 20 66 65 77 20 62 79 74 65 73 0a 2a 2a 20 a few bytes.**
12510 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
12520 61 63 74 75 61 6c 20 73 63 72 61 74 63 68 20 73 actual scratch s
12530 70 61 63 65 20 72 65 71 75 69 72 65 64 20 64 75 pace required du
12540 65 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72 68 e internal overh
12550 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 ead..** The firs
12560 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 73 68 t.** argument sh
12570 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20 61 6e ould point to an
12580 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 allocation of a
12590 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79 74 t least sz*N byt
125a0 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a es of memory..**
125b0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 SQLite will use
125c0 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e no more than on
125d0 65 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 e scratch buffer
125e0 20 61 74 20 6f 6e 63 65 20 70 65 72 20 74 68 72 at once per thr
125f0 65 61 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 68 6f ead, so.** N sho
12600 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 74 68 uld be set to th
12610 65 20 65 78 70 65 63 74 65 64 20 6d 61 78 69 6d e expected maxim
12620 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 um number of thr
12630 65 61 64 73 2e 20 20 54 68 65 20 73 7a 0a 2a 2a eads. The sz.**
12640 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c parameter shoul
12650 64 20 62 65 20 36 20 74 69 6d 65 73 20 74 68 65 d be 6 times the
12660 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 size of the lar
12670 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70 61 gest database pa
12680 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 72 61 ge size..** Scra
12690 74 63 68 20 62 75 66 66 65 72 73 20 61 72 65 20 tch buffers are
126a0 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 used as part of
126b0 74 68 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 the btree balanc
126c0 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 e operation. If
126d0 0a 2a 2a 20 54 68 65 20 62 74 72 65 65 20 62 61 .** The btree ba
126e0 6c 61 6e 63 65 72 20 6e 65 65 64 73 20 61 64 64 lancer needs add
126f0 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 62 itional memory b
12700 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 70 72 eyond what is pr
12710 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20 73 63 72 ovided by.** scr
12720 61 74 63 68 20 62 75 66 66 65 72 73 20 6f 72 20 atch buffers or
12730 69 66 20 6e 6f 20 73 63 72 61 74 63 68 20 62 75 if no scratch bu
12740 66 66 65 72 20 73 70 61 63 65 20 69 73 20 73 70 ffer space is sp
12750 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 53 51 ecified, then SQ
12760 4c 69 74 65 0a 2a 2a 20 67 6f 65 73 20 74 6f 20 Lite.** goes to
12770 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
12780 29 5d 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 )] to obtain the
12790 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 memory it needs
127a0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
127b0 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 >SQLITE_CONFIG_P
127c0 41 47 45 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a AGECACHE</dt>.**
127d0 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e <dd>This option
127e0 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 specifies a sta
127f0 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 tic memory buffe
12800 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 r that SQLite ca
12810 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 n use for.** the
12820 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63 database page c
12830 61 63 68 65 20 77 69 74 68 20 74 68 65 20 64 65 ache with the de
12840 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 fault page cache
12850 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 2e 20 implemenation.
12860 20 0a 2a 2a 20 54 68 69 73 20 63 6f 6e 66 69 67 .** This config
12870 75 72 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e uration should n
12880 6f 74 20 62 65 20 75 73 65 64 20 69 66 20 61 6e ot be used if an
12890 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
128a0 69 6e 65 20 70 61 67 65 0a 2a 2a 20 63 61 63 68 ine page.** cach
128b0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
128c0 20 69 73 20 6c 6f 61 64 65 64 20 75 73 69 6e 67 is loaded using
128d0 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 the SQLITE_CONF
128e0 49 47 5f 50 43 41 43 48 45 20 6f 70 74 69 6f 6e IG_PCACHE option
128f0 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 ..** There are t
12900 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 hree arguments t
12910 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 3a 20 41 o this option: A
12920 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a pointer to the.
12930 2a 2a 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 73 ** memory, the s
12940 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 ize of each page
12950 20 62 75 66 66 65 72 20 28 73 7a 29 2c 20 61 6e buffer (sz), an
12960 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 d the number of
12970 70 61 67 65 73 20 28 4e 29 2e 0a 2a 2a 20 54 68 pages (N)..** Th
12980 65 20 73 7a 20 61 72 67 75 6d 65 6e 74 20 6d 75 e sz argument mu
12990 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 st be a power of
129a0 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 two between 512
129b0 20 61 6e 64 20 33 32 37 36 38 2e 20 20 54 68 65 and 32768. The
129c0 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 first.** argume
129d0 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 nt should point
129e0 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e to an allocation
129f0 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a of at least sz*
12a00 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 N bytes of memor
12a10 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c y..** SQLite wil
12a20 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 l use the memory
12a30 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 provided by the
12a40 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
12a50 74 6f 20 73 61 74 69 73 66 79 20 69 74 73 0a 2a to satisfy its.*
12a60 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 66 * memory needs f
12a70 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 70 or the first N p
12a80 61 67 65 73 20 74 68 61 74 20 69 74 20 61 64 64 ages that it add
12a90 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49 66 20 s to cache. If
12aa0 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61 additional.** pa
12ab0 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 ge cache memory
12ac0 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f 6e 64 is needed beyond
12ad0 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 what is provide
12ae0 64 20 62 79 20 74 68 69 73 20 6f 70 74 69 6f 6e d by this option
12af0 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 , then.** SQLite
12b00 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 goes to [sqlite
12b10 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 3_malloc()] for
12b20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 the additional s
12b30 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2a torage space..**
12b40 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 The implementat
12b50 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20 6f 6e ion might use on
12b60 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 e or more of the
12b70 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20 68 6f N buffers to ho
12b80 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 63 ld .** memory ac
12b90 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d 61 counting informa
12ba0 74 69 6f 6e 2e 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a tion. </dd>.**.*
12bb0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
12bc0 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a 2a FIG_HEAP</dt>.**
12bd0 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e <dd>This option
12be0 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 specifies a sta
12bf0 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 tic memory buffe
12c00 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 r that SQLite wi
12c10 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61 6c ll use.** for al
12c20 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 l of its dynamic
12c30 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
12c40 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e 64 20 on needs beyond
12c50 74 68 6f 73 65 20 70 72 6f 76 69 64 65 64 0a 2a those provided.*
12c60 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 54 45 * for by [SQLITE
12c70 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d _CONFIG_SCRATCH]
12c80 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f 4e and [SQLITE_CON
12c90 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 0a FIG_PAGECACHE]..
12ca0 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 ** There are thr
12cb0 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 41 20 ee arguments: A
12cc0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d pointer to the m
12cd0 65 6d 6f 72 79 2c 20 74 68 65 20 6e 75 6d 62 65 emory, the numbe
12ce0 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e r of.** bytes in
12cf0 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 66 66 the memory buff
12d00 65 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e 69 er, and the mini
12d10 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 mum allocation s
12d20 69 7a 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 ize. If.** the
12d30 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74 first pointer (t
12d40 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 he memory pointe
12d50 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e r) is NULL, then
12d60 20 53 51 4c 69 74 65 20 72 65 76 65 72 74 73 0a SQLite reverts.
12d70 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74 73 20 ** to using its
12d80 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 default memory a
12d90 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20 73 79 llocator (the sy
12da0 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d stem malloc() im
12db0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a plementation),.*
12dc0 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72 * undoing any pr
12dd0 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f ior invocation o
12de0 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 f [SQLITE_CONFIG
12df0 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 68 _MALLOC]. If th
12e00 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e e.** memory poin
12e10 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 ter is not NULL
12e20 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49 and either [SQLI
12e30 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
12e40 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 3] or.** [SQLITE
12e50 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 5d _ENABLE_MEMSYS5]
12e60 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 are defined, th
12e70 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 en the alternati
12e80 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c ve memory.** all
12e90 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61 67 65 ocator is engage
12ea0 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c 20 d to handle all
12eb0 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f 72 of SQLites memor
12ec0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 y allocation nee
12ed0 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ds.</dd>.**.** <
12ee0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
12ef0 5f 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c _MUTEX</dt>.** <
12f00 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 dd>This option t
12f10 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 akes a single ar
12f20 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 gument which is
12f30 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a a pointer to an.
12f40 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ** instance of t
12f50 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 he [sqlite3_mute
12f60 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 x_methods] struc
12f70 74 75 72 65 2e 20 20 54 68 65 20 61 72 67 75 6d ture. The argum
12f80 65 6e 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a ent specifies.**
12f90 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 alternative low
12fa0 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 72 6f 75 -level mutex rou
12fb0 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 tines to be used
12fc0 20 69 6e 20 70 6c 61 63 65 0a 2a 2a 20 74 68 65 in place.** the
12fd0 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 mutex routines
12fe0 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 built into SQLit
12ff0 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 e.</dd>.**.** <d
13000 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t>SQLITE_CONFIG_
13010 47 45 54 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a GETMUTEX</dt>.**
13020 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e <dd>This option
13030 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 takes a single
13040 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 argument which i
13050 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
13060 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 n.** instance of
13070 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 the [sqlite3_mu
13080 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 tex_methods] str
13090 75 63 74 75 72 65 2e 20 20 54 68 65 0a 2a 2a 20 ucture. The.**
130a0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d [sqlite3_mutex_m
130b0 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 63 ethods].** struc
130c0 74 75 72 65 20 69 73 20 66 69 6c 6c 65 64 20 77 ture is filled w
130d0 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 6c ith the currentl
130e0 79 20 64 65 66 69 6e 65 64 20 6d 75 74 65 78 20 y defined mutex
130f0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 routines..** Thi
13100 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20 s option can be
13110 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64 used to overload
13120 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 75 74 the default mut
13130 65 78 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a ex allocation.**
13140 20 72 6f 75 74 69 6e 65 73 20 77 69 74 68 20 61 routines with a
13150 20 77 72 61 70 70 65 72 20 75 73 65 64 20 74 6f wrapper used to
13160 20 74 72 61 63 6b 20 6d 75 74 65 78 20 75 73 61 track mutex usa
13170 67 65 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e ge for performan
13180 63 65 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 20 ce.** profiling
13190 6f 72 20 74 65 73 74 69 6e 67 2c 20 66 6f 72 20 or testing, for
131a0 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a example.</dd>.**
131b0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
131c0 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c ONFIG_LOOKASIDE<
131d0 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
131e0 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 77 option takes tw
131f0 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 o arguments that
13200 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 determine the d
13210 65 66 61 75 6c 74 0a 2a 2a 20 6d 65 6d 6f 72 79 efault.** memory
13220 20 61 6c 6c 63 61 74 69 6f 6e 20 6c 6f 6f 6b 61 allcation looka
13230 73 69 64 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f side optimizatio
13240 6e 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 n. The first ar
13250 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a gument is the.**
13260 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f size of each lo
13270 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73 okaside buffer s
13280 6c 6f 74 20 61 6e 64 20 74 68 65 20 73 65 63 6f lot and the seco
13290 6e 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 nd is the number
132a0 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 20 61 6c 6c of.** slots all
132b0 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68 20 64 ocated to each d
132c0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
132d0 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c on.</dd>.**.** <
132e0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
132f0 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 _PCACHE</dt>.**
13300 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
13310 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 takes a single a
13320 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 rgument which is
13330 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a a pointer to.**
13340 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 an [sqlite3_pca
13350 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a che_methods] obj
13360 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 63 ect. This objec
13370 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 t specifies the
13380 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 interface.** to
13390 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 a custom page ca
133a0 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 che implementati
133b0 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b 65 on. SQLite make
133c0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a s a copy of the.
133d0 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73 ** object and us
133e0 65 73 20 69 74 20 66 6f 72 20 70 61 67 65 20 63 es it for page c
133f0 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ache memory allo
13400 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a cations.</dd>.**
13410 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
13420 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 3c ONFIG_GETPCACHE<
13430 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
13440 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 option takes a
13450 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
13460 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 which is a point
13470 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c er to an.** [sql
13480 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
13490 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 51 ods] object. SQ
134a0 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66 20 74 Lite copies of t
134b0 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 61 he current.** pa
134c0 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 ge cache impleme
134d0 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 61 ntation into tha
134e0 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a 2a t object.</dd>.*
134f0 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 *.** </dl>.*/.#d
13500 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
13510 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 FIG_SINGLETHREAD
13520 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 1 /* nil */.#
13530 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
13540 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 NFIG_MULTITHREAD
13550 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 2 /* nil */.
13560 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
13570 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 ONFIG_SERIALIZED
13580 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 3 /* nil */
13590 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
135a0 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 CONFIG_MALLOC
135b0 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 4 /* sqlit
135c0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 e3_mem_methods*
135d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
135e0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c E_CONFIG_GETMALL
135f0 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c OC 5 /* sql
13600 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
13610 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c * */.#define SQL
13620 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
13630 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 CH 6 /* v
13640 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e oid*, int sz, in
13650 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 t N */.#define S
13660 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
13670 45 43 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a ECACHE 7 /*
13680 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 void*, int sz,
13690 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 int N */.#define
136a0 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 SQLITE_CONFIG_H
136b0 45 41 50 20 20 20 20 20 20 20 20 20 20 38 20 20 EAP 8
136c0 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 /* void*, int nB
136d0 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a yte, int min */.
136e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
136f0 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 ONFIG_MEMSTATUS
13700 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 9 /* boolea
13710 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c n */.#define SQL
13720 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 ITE_CONFIG_MUTEX
13730 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 10 /* s
13740 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
13750 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 hods* */.#define
13760 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 SQLITE_CONFIG_G
13770 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 ETMUTEX 11
13780 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 /* sqlite3_mutex
13790 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20 _methods* */./*
137a0 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54 previously SQLIT
137b0 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c E_CONFIG_CHUNKAL
137c0 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 73 20 LOC 12 which is
137d0 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a now unused. */ .
137e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
137f0 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 ONFIG_LOOKASIDE
13800 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e 13 /* int in
13810 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c t */.#define SQL
13820 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 ITE_CONFIG_PCACH
13830 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a 20 73 E 14 /* s
13840 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
13850 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e thods* */.#defin
13860 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f e SQLITE_CONFIG_
13870 47 45 54 50 43 41 43 48 45 20 20 20 20 31 35 20 GETPCACHE 15
13880 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 /* sqlite3_pcac
13890 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 0a he_methods* */..
138a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
138b0 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 Configuration Op
138c0 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c tions {H10170} <
138d0 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S20000>.** EXPER
138e0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
138f0 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 ese constants ar
13900 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 e the available
13910 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72 integer configur
13920 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 ation options th
13930 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 at.** can be pas
13940 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
13950 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 d argument to th
13960 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f e [sqlite3_db_co
13970 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 nfig()] interfac
13980 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e e..**.** New con
13990 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
139a0 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 ns may be added
139b0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
139c0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a es of SQLite..**
139d0 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 Existing config
139e0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 uration options
139f0 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 might be discont
13a00 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 inued. Applicat
13a10 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 ions.** should c
13a20 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 heck the return
13a30 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 code from [sqlit
13a40 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 e3_db_config()]
13a50 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
13a60 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f t.** the call wo
13a70 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 rked. The [sqli
13a80 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d te3_db_config()]
13a90 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 interface will
13aa0 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d return a.** non-
13ab0 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 zero [error code
13ac0 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e ] if a discontin
13ad0 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 ued or unsupport
13ae0 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e ed configuration
13af0 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e option.** is in
13b00 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c voked..**.** <dl
13b10 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f >.** <dt>SQLITE_
13b20 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 DBCONFIG_LOOKASI
13b30 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 DE</dt>.** <dd>T
13b40 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 his option takes
13b50 20 74 68 72 65 65 20 61 64 64 69 74 69 6f 6e 61 three additiona
13b60 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 l arguments that
13b70 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 0a determine the .
13b80 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 ** [lookaside me
13b90 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 mory allocator]
13ba0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f configuration fo
13bb0 72 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 r the [database
13bc0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20 connection]..**
13bd0 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
13be0 6e 74 20 28 74 68 65 20 74 68 69 72 64 20 70 61 nt (the third pa
13bf0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 rameter to [sqli
13c00 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d te3_db_config()]
13c10 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 is a.** pointer
13c20 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 62 75 66 to a memory buf
13c30 66 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20 6c fer to use for l
13c40 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e ookaside memory.
13c50 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 The first.** a
13c60 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e rgument may be N
13c70 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73 ULL in which cas
13c80 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c e SQLite will al
13c90 6c 6f 63 61 74 65 20 74 68 65 20 6c 6f 6f 6b 61 locate the looka
13ca0 73 69 64 65 0a 2a 2a 20 62 75 66 66 65 72 20 69 side.** buffer i
13cb0 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c tself using [sql
13cc0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 ite3_malloc()].
13cd0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
13ce0 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73 ment is the.** s
13cf0 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b ize of each look
13d00 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f aside buffer slo
13d10 74 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 t and the third
13d20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
13d30 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f number of.** slo
13d40 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 ts. The size of
13d50 20 74 68 65 20 62 75 66 66 65 72 20 69 6e 20 74 the buffer in t
13d60 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
13d70 74 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 t must be greate
13d80 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75 r than.** or equ
13d90 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64 75 63 al to the produc
13da0 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 t of the second
13db0 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 6d 65 and third argume
13dc0 6e 74 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 nts.</dd>.**.**
13dd0 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 </dl>.*/.#define
13de0 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 SQLITE_DBCONFIG
13df0 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20 31 30 _LOOKASIDE 10
13e00 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69 6e 74 01 /* void* int
13e10 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 int */.../*.**
13e20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 CAPI3REF: Enable
13e30 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 65 Or Disable Exte
13e40 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64 65 nded Result Code
13e50 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31 30 37 s {H12200} <S107
13e60 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
13e70 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 lite3_extended_r
13e80 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20 72 6f esult_codes() ro
13e90 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 utine enables or
13ea0 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a 2a 2a disables the.**
13eb0 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c [extended resul
13ec0 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75 72 65 t codes] feature
13ed0 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68 65 20 of SQLite. The
13ee0 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 0a extended result.
13ef0 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64 69 73 ** codes are dis
13f00 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 abled by default
13f10 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 for historical
13f20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 63 6f compatibility co
13f30 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a nsiderations..**
13f40 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
13f50 2a 2a 0a 2a 2a 20 7b 48 31 32 32 30 31 7d 20 45 **.** {H12201} E
13f60 61 63 68 20 6e 65 77 20 5b 64 61 74 61 62 61 73 ach new [databas
13f70 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 68 e connection] sh
13f80 61 6c 6c 20 68 61 76 65 20 74 68 65 0a 2a 2a 20 all have the.**
13f90 20 20 20 20 20 20 20 20 20 5b 65 78 74 65 6e 64 [extend
13fa0 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d ed result codes]
13fb0 20 66 65 61 74 75 72 65 20 64 69 73 61 62 6c 65 feature disable
13fc0 64 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a d by default..**
13fd0 0a 2a 2a 20 7b 48 31 32 32 30 32 7d 20 54 68 65 .** {H12202} The
13fe0 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 [sqlite3_extend
13ff0 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 ed_result_codes(
14000 44 2c 46 29 5d 20 69 6e 74 65 72 66 61 63 65 20 D,F)] interface
14010 73 68 61 6c 6c 20 65 6e 61 62 6c 65 0a 2a 2a 20 shall enable.**
14020 20 20 20 20 20 20 20 20 20 5b 65 78 74 65 6e 64 [extend
14030 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d ed result codes]
14040 20 66 6f 72 20 74 68 65 20 20 5b 64 61 74 61 62 for the [datab
14050 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
14060 44 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66 D.** if
14070 20 74 68 65 20 46 20 70 61 72 61 6d 65 74 65 72 the F parameter
14080 20 69 73 20 74 72 75 65 2c 20 6f 72 20 64 69 73 is true, or dis
14090 61 62 6c 65 20 74 68 65 6d 20 69 66 20 46 20 69 able them if F i
140a0 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 s false..*/.SQLI
140b0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
140c0 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 e3_extended_resu
140d0 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 lt_codes(sqlite3
140e0 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a *, int onoff);..
140f0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
14100 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69 Last Insert Rowi
14110 64 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37 d {H12220} <S107
14120 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 00>.**.** Each e
14130 6e 74 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 ntry in an SQLit
14140 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e e table has a un
14150 69 71 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e ique 64-bit sign
14160 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 ed.** integer ke
14170 79 20 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f y called the [RO
14180 57 49 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20 WID | "rowid"].
14190 54 68 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77 The rowid is alw
141a0 61 79 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a ays available.**
141b0 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65 as an undeclare
141c0 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52 d column named R
141d0 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52 OWID, OID, or _R
141e0 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73 OWID_ as long as
141f0 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20 those.** names
14200 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65 are not also use
14210 64 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20 d by explicitly
14220 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73 declared columns
14230 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c . If.** the tabl
14240 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f e has a column o
14250 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20 f type [INTEGER
14260 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65 PRIMARY KEY] the
14270 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a n that column.**
14280 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61 is another alia
14290 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e s for the rowid.
142a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
142b0 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ine returns the
142c0 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d [rowid] of the m
142d0 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 ost recent.** su
142e0 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 ccessful [INSERT
142f0 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 ] into the datab
14300 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 ase from the [da
14310 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14320 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 n].** in the fir
14330 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 st argument. If
14340 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b no successful [
14350 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 INSERT]s.** have
14360 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f ever occurred o
14370 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 n that database
14380 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f connection, zero
14390 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
143a0 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 .** If an [INSER
143b0 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e T] occurs within
143c0 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e a trigger, then
143d0 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 the [rowid] of
143e0 74 68 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 the inserted.**
143f0 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20 row is returned
14400 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 by this routine
14410 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 as long as the t
14420 72 69 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e rigger is runnin
14430 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 g..** But once t
14440 68 65 20 74 72 69 67 67 65 72 20 74 65 72 6d 69 he trigger termi
14450 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65 nates, the value
14460 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 returned by thi
14470 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 s routine.** rev
14480 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 erts to the last
14490 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20 value inserted
144a0 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 before the trigg
144b0 65 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 er fired..**.**
144c0 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 An [INSERT] that
144d0 20 66 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20 fails due to a
144e0 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
144f0 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a tion is not a.**
14500 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 successful [INS
14510 45 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f ERT] and does no
14520 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c t change the val
14530 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ue returned by t
14540 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 his.** routine.
14550 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20 Thus INSERT OR
14560 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 FAIL, INSERT OR
14570 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f IGNORE, INSERT O
14580 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 R ROLLBACK,.** a
14590 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f nd INSERT OR ABO
145a0 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 RT make no chang
145b0 65 73 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e es to the return
145c0 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a value of this.*
145d0 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 * routine when t
145e0 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 heir insertion f
145f0 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 ails. When INSE
14600 52 54 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a RT OR REPLACE.**
14610 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f encounters a co
14620 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 nstraint violati
14630 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 on, it does not
14640 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e fail. The.** IN
14650 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 SERT continues t
14660 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 o completion aft
14670 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 er deleting rows
14680 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 that caused.**
14690 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 the constraint p
146a0 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 roblem so INSERT
146b0 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c OR REPLACE will
146c0 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a always change.*
146d0 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c * the return val
146e0 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 ue of this inter
146f0 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 face..**.** For
14700 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
14710 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e this routine, an
14720 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e [INSERT] is con
14730 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 sidered to.** be
14740 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e successful even
14750 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65 71 if it is subseq
14760 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 uently rolled ba
14770 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 ck..**.** INVARI
14780 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 ANTS:.**.** {H12
14790 32 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 221} The [sqlite
147a0 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 3_last_insert_ro
147b0 77 69 64 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 wid()] function
147c0 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 shall return.**
147d0 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 72 6f the [ro
147e0 77 69 64 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 wid].**
147f0 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 of the most rec
14800 65 6e 74 20 73 75 63 63 65 73 73 66 75 6c 20 5b ent successful [
14810 49 4e 53 45 52 54 5d 20 70 65 72 66 6f 72 6d 65 INSERT] performe
14820 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a d on the same.**
14830 20 20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 [datab
14840 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
14850 61 6e 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 and within the s
14860 61 6d 65 20 6f 72 20 68 69 67 68 65 72 20 6c 65 ame or higher le
14870 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 vel.**
14880 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c trigger context,
14890 20 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 72 or zero if ther
148a0 65 20 68 61 76 65 20 62 65 65 6e 20 6e 6f 20 71 e have been no q
148b0 75 61 6c 69 66 79 69 6e 67 0a 2a 2a 20 20 20 20 ualifying.**
148c0 20 20 20 20 20 20 5b 49 4e 53 45 52 54 5d 20 73 [INSERT] s
148d0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a tatements..**.**
148e0 20 7b 48 31 32 32 32 33 7d 20 54 68 65 20 5b 73 {H12223} The [s
148f0 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 qlite3_last_inse
14900 72 74 5f 72 6f 77 69 64 28 29 5d 20 66 75 6e 63 rt_rowid()] func
14910 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 72 tion shall retur
14920 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 n the.**
14930 20 20 73 61 6d 65 20 76 61 6c 75 65 20 77 68 65 same value whe
14940 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 n called from th
14950 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 20 63 e same trigger c
14960 6f 6e 74 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 ontext.**
14970 20 20 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 immediately b
14980 65 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 20 efore and after
14990 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a a [ROLLBACK]..**
149a0 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a .** ASSUMPTIONS:
149b0 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 32 33 32 7d 20 .**.** {A12232}
149c0 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68 If a separate th
149d0 72 65 61 64 20 70 65 72 66 6f 72 6d 73 20 61 20 read performs a
149e0 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 6f 6e 20 new [INSERT] on
149f0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 the same.**
14a00 20 20 20 20 20 64 61 74 61 62 61 73 65 20 63 6f database co
14a10 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 nnection while t
14a20 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 he [sqlite3_last
14a30 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d _insert_rowid()]
14a40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e .** fun
14a50 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 ction is running
14a60 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65 and thus change
14a70 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 s the last inser
14a80 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 20 20 t [rowid],.**
14a90 20 20 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 then the
14aa0 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
14ab0 79 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f y [sqlite3_last_
14ac0 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 20 insert_rowid()]
14ad0 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 is.** u
14ae0 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 npredictable and
14af0 20 6d 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c might not equal
14b00 20 65 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 either the old
14b10 6f 72 20 74 68 65 20 6e 65 77 0a 2a 2a 20 20 20 or the new.**
14b20 20 20 20 20 20 20 20 6c 61 73 74 20 69 6e 73 65 last inse
14b30 72 74 20 5b 72 6f 77 69 64 5d 2e 0a 2a 2f 0a 53 rt [rowid]..*/.S
14b40 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
14b50 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
14b60 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 last_insert_rowi
14b70 64 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a d(sqlite3*);../*
14b80 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f .** CAPI3REF: Co
14b90 75 6e 74 20 54 68 65 20 4e 75 6d 62 65 72 20 4f unt The Number O
14ba0 66 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64 20 f Rows Modified
14bb0 7b 48 31 32 32 34 30 7d 20 3c 53 31 30 36 30 30 {H12240} <S10600
14bc0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e >.**.** This fun
14bd0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
14be0 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 e number of data
14bf0 62 61 73 65 20 72 6f 77 73 20 74 68 61 74 20 77 base rows that w
14c00 65 72 65 20 63 68 61 6e 67 65 64 0a 2a 2a 20 6f ere changed.** o
14c10 72 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 r inserted or de
14c20 6c 65 74 65 64 20 62 79 20 74 68 65 20 6d 6f 73 leted by the mos
14c30 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c t recently compl
14c40 65 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 eted SQL stateme
14c50 6e 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 5b 64 61 nt.** on the [da
14c60 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14c70 6e 5d 20 73 70 65 63 69 66 69 65 64 20 62 79 20 n] specified by
14c80 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 the first parame
14c90 74 65 72 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 68 61 ter..** Only cha
14ca0 6e 67 65 73 20 74 68 61 74 20 61 72 65 20 64 69 nges that are di
14cb0 72 65 63 74 6c 79 20 73 70 65 63 69 66 69 65 64 rectly specified
14cc0 20 62 79 20 74 68 65 20 5b 49 4e 53 45 52 54 5d by the [INSERT]
14cd0 2c 20 5b 55 50 44 41 54 45 5d 2c 0a 2a 2a 20 6f , [UPDATE],.** o
14ce0 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65 r [DELETE] state
14cf0 6d 65 6e 74 20 61 72 65 20 63 6f 75 6e 74 65 64 ment are counted
14d00 2e 20 20 41 75 78 69 6c 69 61 72 79 20 63 68 61 . Auxiliary cha
14d10 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 0a 2a nges caused by.*
14d20 2a 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6e * triggers are n
14d30 6f 74 20 63 6f 75 6e 74 65 64 2e 20 55 73 65 20 ot counted. Use
14d40 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 the [sqlite3_tot
14d50 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 66 75 al_changes()] fu
14d60 6e 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 66 69 6e nction.** to fin
14d70 64 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 d the total numb
14d80 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e er of changes in
14d90 63 6c 75 64 69 6e 67 20 63 68 61 6e 67 65 73 20 cluding changes
14da0 63 61 75 73 65 64 20 62 79 20 74 72 69 67 67 65 caused by trigge
14db0 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 72 6f 77 rs..**.** A "row
14dc0 20 63 68 61 6e 67 65 22 20 69 73 20 61 20 63 68 change" is a ch
14dd0 61 6e 67 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 ange to a single
14de0 20 72 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c 65 row of a single
14df0 20 74 61 62 6c 65 0a 2a 2a 20 63 61 75 73 65 64 table.** caused
14e00 20 62 79 20 61 6e 20 49 4e 53 45 52 54 2c 20 44 by an INSERT, D
14e10 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45 ELETE, or UPDATE
14e20 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 6f 77 statement. Row
14e30 73 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 68 s that.** are ch
14e40 61 6e 67 65 64 20 61 73 20 73 69 64 65 20 65 66 anged as side ef
14e50 66 65 63 74 73 20 6f 66 20 52 45 50 4c 41 43 45 fects of REPLACE
14e60 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 73 6f constraint reso
14e70 6c 75 74 69 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 lution,.** rollb
14e80 61 63 6b 2c 20 41 42 4f 52 54 20 70 72 6f 63 65 ack, ABORT proce
14e90 73 73 69 6e 67 2c 20 44 52 4f 50 20 54 41 42 4c ssing, DROP TABL
14ea0 45 2c 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68 E, or by any oth
14eb0 65 72 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73 er.** mechanisms
14ec0 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 do not count as
14ed0 20 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e direct row chan
14ee0 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72 ges..**.** A "tr
14ef0 69 67 67 65 72 20 63 6f 6e 74 65 78 74 22 20 69 igger context" i
14f00 73 20 61 20 73 63 6f 70 65 20 6f 66 20 65 78 65 s a scope of exe
14f10 63 75 74 69 6f 6e 20 74 68 61 74 20 62 65 67 69 cution that begi
14f20 6e 73 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20 77 ns and.** ends w
14f30 69 74 68 20 74 68 65 20 73 63 72 69 70 74 20 6f ith the script o
14f40 66 20 61 20 74 72 69 67 67 65 72 2e 20 20 4d 6f f a trigger. Mo
14f50 73 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 st SQL statement
14f60 73 20 61 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74 s are.** evaluat
14f70 65 64 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e ed outside of an
14f80 79 20 74 72 69 67 67 65 72 2e 20 20 54 68 69 73 y trigger. This
14f90 20 69 73 20 74 68 65 20 22 74 6f 70 20 6c 65 76 is the "top lev
14fa0 65 6c 22 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 el".** trigger c
14fb0 6f 6e 74 65 78 74 2e 20 20 49 66 20 61 20 74 72 ontext. If a tr
14fc0 69 67 67 65 72 20 66 69 72 65 73 20 66 72 6f 6d igger fires from
14fd0 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 the top level,
14fe0 61 0a 2a 2a 20 6e 65 77 20 74 72 69 67 67 65 72 a.** new trigger
14ff0 20 63 6f 6e 74 65 78 74 20 69 73 20 65 6e 74 65 context is ente
15000 72 65 64 20 66 6f 72 20 74 68 65 20 64 75 72 61 red for the dura
15010 74 69 6f 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65 tion of that one
15020 0a 2a 2a 20 74 72 69 67 67 65 72 2e 20 20 53 75 .** trigger. Su
15030 62 74 72 69 67 67 65 72 73 20 63 72 65 61 74 65 btriggers create
15040 20 73 75 62 63 6f 6e 74 65 78 74 73 20 66 6f 72 subcontexts for
15050 20 74 68 65 69 72 20 64 75 72 61 74 69 6f 6e 2e their duration.
15060 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b .**.** Calling [
15070 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 sqlite3_exec()]
15080 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 or [sqlite3_step
15090 28 29 5d 20 72 65 63 75 72 73 69 76 65 6c 79 20 ()] recursively
150a0 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61 does.** not crea
150b0 74 65 20 61 20 6e 65 77 20 74 72 69 67 67 65 72 te a new trigger
150c0 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 context..**.**
150d0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
150e0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
150f0 20 6f 66 20 64 69 72 65 63 74 20 72 6f 77 20 63 of direct row c
15100 68 61 6e 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a hanges in the.**
15110 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53 most recent INS
15120 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 ERT, UPDATE, or
15130 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 DELETE statement
15140 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 within the same
15150 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 .** trigger cont
15160 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c ext..**.** Thus,
15170 20 77 68 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f when called fro
15180 6d 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c m the top level,
15190 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
151a0 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6e 75 eturns the.** nu
151b0 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 mber of changes
151c0 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 in the most rece
151d0 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 nt INSERT, UPDAT
151e0 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 E, or DELETE.**
151f0 74 68 61 74 20 61 6c 73 6f 20 6f 63 63 75 72 72 that also occurr
15200 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6c 65 ed at the top le
15210 76 65 6c 2e 20 20 57 69 74 68 69 6e 20 74 68 65 vel. Within the
15220 20 62 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67 body of a trigg
15230 65 72 2c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 er,.** the sqlit
15240 65 33 5f 63 68 61 6e 67 65 73 28 29 20 69 6e 74 e3_changes() int
15250 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 63 61 erface can be ca
15260 6c 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 lled to find the
15270 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 number of.** ch
15280 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 anges in the mos
15290 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c t recently compl
152a0 65 74 65 64 20 49 4e 53 45 52 54 2c 20 55 50 44 eted INSERT, UPD
152b0 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a ATE, or DELETE.*
152c0 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 * statement with
152d0 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 in the body of t
152e0 68 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 2e he same trigger.
152f0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 .** However, the
15300 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64 number returned
15310 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 does not includ
15320 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75 e changes.** cau
15330 73 65 64 20 62 79 20 73 75 62 74 72 69 67 67 65 sed by subtrigge
15340 72 73 20 73 69 6e 63 65 20 74 68 6f 73 65 20 68 rs since those h
15350 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 63 6f ave their own co
15360 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c ntext..**.** SQL
15370 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 ite implements t
15380 68 65 20 63 6f 6d 6d 61 6e 64 20 22 44 45 4c 45 he command "DELE
15390 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 22 20 77 TE FROM table" w
153a0 69 74 68 6f 75 74 20 61 20 57 48 45 52 45 20 63 ithout a WHERE c
153b0 6c 61 75 73 65 0a 2a 2a 20 62 79 20 64 72 6f 70 lause.** by drop
153c0 70 69 6e 67 20 61 6e 64 20 72 65 63 72 65 61 74 ping and recreat
153d0 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20 ing the table.
153e0 44 6f 69 6e 67 20 73 6f 20 69 73 20 6d 75 63 68 Doing so is much
153f0 20 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f 69 faster than goi
15400 6e 67 0a 2a 2a 20 74 68 72 6f 75 67 68 20 61 6e ng.** through an
15410 64 20 64 65 6c 65 74 69 6e 67 20 69 6e 64 69 76 d deleting indiv
15420 69 64 75 61 6c 20 65 6c 65 6d 65 6e 74 73 20 66 idual elements f
15430 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 2e 20 20 rom the table.
15440 42 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 0a Because of this.
15450 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c ** optimization,
15460 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 73 20 69 the deletions i
15470 6e 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 n "DELETE FROM t
15480 61 62 6c 65 22 20 61 72 65 20 6e 6f 74 20 72 6f able" are not ro
15490 77 20 63 68 61 6e 67 65 73 20 61 6e 64 0a 2a 2a w changes and.**
154a0 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 75 will not be cou
154b0 6e 74 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 nted by the sqli
154c0 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 72 te3_changes() or
154d0 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f [sqlite3_total_
154e0 63 68 61 6e 67 65 73 28 29 5d 0a 2a 2a 20 66 75 changes()].** fu
154f0 6e 63 74 69 6f 6e 73 2c 20 72 65 67 61 72 64 6c nctions, regardl
15500 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ess of the numbe
15510 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 74 68 r of elements th
15520 61 74 20 77 65 72 65 20 6f 72 69 67 69 6e 61 6c at were original
15530 6c 79 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 ly.** in the tab
15540 6c 65 2e 20 20 54 6f 20 67 65 74 20 61 6e 20 61 le. To get an a
15550 63 63 75 72 61 74 65 20 63 6f 75 6e 74 20 6f 66 ccurate count of
15560 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 the number of r
15570 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 75 73 65 ows deleted, use
15580 0a 2a 2a 20 22 44 45 4c 45 54 45 20 46 52 4f 4d .** "DELETE FROM
15590 20 74 61 62 6c 65 20 57 48 45 52 45 20 31 22 20 table WHERE 1"
155a0 69 6e 73 74 65 61 64 2e 20 20 4f 72 20 72 65 63 instead. Or rec
155b0 6f 6d 70 69 6c 65 20 75 73 69 6e 67 20 74 68 65 ompile using the
155c0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 .** [SQLITE_OMIT
155d0 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d 49 _TRUNCATE_OPTIMI
155e0 5a 41 54 49 4f 4e 5d 20 63 6f 6d 70 69 6c 65 2d ZATION] compile-
155f0 74 69 6d 65 20 6f 70 74 69 6f 6e 20 74 6f 20 64 time option to d
15600 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 6f 70 isable the.** op
15610 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 6c timization on al
15620 6c 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a l queries..**.**
15630 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
15640 2a 2a 20 7b 48 31 32 32 34 31 7d 20 54 68 65 20 ** {H12241} The
15650 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 [sqlite3_changes
15660 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 ()] function sha
15670 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 ll return the nu
15680 6d 62 65 72 20 6f 66 0a 2a 2a 20 20 20 20 20 20 mber of.**
15690 20 20 20 20 72 6f 77 20 63 68 61 6e 67 65 73 20 row changes
156a0 63 61 75 73 65 64 20 62 79 20 74 68 65 20 6d 6f caused by the mo
156b0 73 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 st recent INSERT
156c0 2c 20 55 50 44 41 54 45 2c 0a 2a 2a 20 20 20 20 , UPDATE,.**
156d0 20 20 20 20 20 20 6f 72 20 44 45 4c 45 54 45 20 or DELETE
156e0 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 74 68 65 statement on the
156f0 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 same database c
15700 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a onnection and.**
15710 20 20 20 20 20 20 20 20 20 20 77 69 74 68 69 6e within
15720 20 74 68 65 20 73 61 6d 65 20 6f 72 20 68 69 67 the same or hig
15730 68 65 72 20 74 72 69 67 67 65 72 20 63 6f 6e 74 her trigger cont
15740 65 78 74 2c 20 6f 72 20 7a 65 72 6f 20 69 66 20 ext, or zero if
15750 74 68 65 72 65 20 68 61 76 65 0a 2a 2a 20 20 20 there have.**
15760 20 20 20 20 20 20 20 6e 6f 74 20 62 65 65 6e 20 not been
15770 61 6e 79 20 71 75 61 6c 69 66 79 69 6e 67 20 72 any qualifying r
15780 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a ow changes..**.*
15790 2a 20 7b 48 31 32 32 34 33 7d 20 53 74 61 74 65 * {H12243} State
157a0 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 ments of the for
157b0 6d 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 m "DELETE FROM t
157c0 61 62 6c 65 6e 61 6d 65 22 20 77 69 74 68 20 6e ablename" with n
157d0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 57 48 o.** WH
157e0 45 52 45 20 63 6c 61 75 73 65 20 73 68 61 6c 6c ERE clause shall
157f0 20 63 61 75 73 65 20 73 75 62 73 65 71 75 65 6e cause subsequen
15800 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20 t calls to.**
15810 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
15820 63 68 61 6e 67 65 73 28 29 5d 20 74 6f 20 72 65 changes()] to re
15830 74 75 72 6e 20 7a 65 72 6f 2c 20 72 65 67 61 72 turn zero, regar
15840 64 6c 65 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 dless of the.**
15850 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 number
15860 6f 66 20 72 6f 77 73 20 6f 72 69 67 69 6e 61 6c of rows original
15870 6c 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e ly in the table.
15880 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f .**.** ASSUMPTIO
15890 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 32 35 NS:.**.** {A1225
158a0 32 7d 20 49 66 20 61 20 73 65 70 61 72 61 74 65 2} If a separate
158b0 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68 thread makes ch
158c0 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d anges on the sam
158d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
158e0 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ction.**
158f0 20 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 while [sqlite3
15900 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 73 20 72 _changes()] is r
15910 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 68 65 20 unning then the
15920 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 0a 2a value returned.*
15930 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 75 6e * is un
15940 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 predictable and
15950 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a not meaningful..
15960 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
15970 74 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 t sqlite3_change
15980 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a s(sqlite3*);../*
15990 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 6f .** CAPI3REF: To
159a0 74 61 6c 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f tal Number Of Ro
159b0 77 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 ws Modified {H12
159c0 32 36 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 260} <S10600>.**
159d0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
159e0 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 n returns the nu
159f0 6d 62 65 72 20 6f 66 20 72 6f 77 20 63 68 61 6e mber of row chan
15a00 67 65 73 20 63 61 75 73 65 64 20 62 79 20 49 4e ges caused by IN
15a10 53 45 52 54 2c 0a 2a 2a 20 55 50 44 41 54 45 20 SERT,.** UPDATE
15a20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d or DELETE statem
15a30 65 6e 74 73 20 73 69 6e 63 65 20 74 68 65 20 5b ents since the [
15a40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
15a50 69 6f 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64 2e ion] was opened.
15a60 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 20 69 6e .** The count in
15a70 63 6c 75 64 65 73 20 61 6c 6c 20 63 68 61 6e 67 cludes all chang
15a80 65 73 20 66 72 6f 6d 20 61 6c 6c 20 74 72 69 67 es from all trig
15a90 67 65 72 20 63 6f 6e 74 65 78 74 73 2e 20 20 48 ger contexts. H
15aa0 6f 77 65 76 65 72 2c 0a 2a 2a 20 74 68 65 20 63 owever,.** the c
15ab0 6f 75 6e 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e ount does not in
15ac0 63 6c 75 64 65 20 63 68 61 6e 67 65 73 20 75 73 clude changes us
15ad0 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
15ae0 52 45 50 4c 41 43 45 20 63 6f 6e 73 74 72 61 69 REPLACE constrai
15af0 6e 74 73 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 nts,.** do rollb
15b00 61 63 6b 73 20 6f 72 20 41 42 4f 52 54 20 70 72 acks or ABORT pr
15b10 6f 63 65 73 73 69 6e 67 2c 20 6f 72 20 44 52 4f ocessing, or DRO
15b20 50 20 74 61 62 6c 65 20 70 72 6f 63 65 73 73 69 P table processi
15b30 6e 67 2e 0a 2a 2a 20 54 68 65 20 63 68 61 6e 67 ng..** The chang
15b40 65 73 20 61 72 65 20 63 6f 75 6e 74 65 64 20 61 es are counted a
15b50 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 73 74 s soon as the st
15b60 61 74 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 6b atement that mak
15b70 65 73 20 74 68 65 6d 20 69 73 0a 2a 2a 20 63 6f es them is.** co
15b80 6d 70 6c 65 74 65 64 20 28 77 68 65 6e 20 74 68 mpleted (when th
15b90 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 e statement hand
15ba0 6c 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 le is passed to
15bb0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
15bc0 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ] or.** [sqlite3
15bd0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a _finalize()])..*
15be0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c *.** SQLite impl
15bf0 65 6d 65 6e 74 73 20 74 68 65 20 63 6f 6d 6d 61 ements the comma
15c00 6e 64 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 nd "DELETE FROM
15c10 74 61 62 6c 65 22 20 77 69 74 68 6f 75 74 20 61 table" without a
15c20 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a 2a 2a WHERE clause.**
15c30 20 62 79 20 64 72 6f 70 70 69 6e 67 20 61 6e 64 by dropping and
15c40 20 72 65 63 72 65 61 74 69 6e 67 20 74 68 65 20 recreating the
15c50 74 61 62 6c 65 2e 20 20 28 54 68 69 73 20 69 73 table. (This is
15c60 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 61 much faster tha
15c70 6e 20 67 6f 69 6e 67 0a 2a 2a 20 74 68 72 6f 75 n going.** throu
15c80 67 68 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 20 gh and deleting
15c90 69 6e 64 69 76 69 64 75 61 6c 20 65 6c 65 6d 65 individual eleme
15ca0 6e 74 73 20 66 72 6f 6d 20 74 68 65 20 74 61 62 nts from the tab
15cb0 6c 65 2e 29 20 20 42 65 63 61 75 73 65 20 6f 66 le.) Because of
15cc0 20 74 68 69 73 0a 2a 2a 20 6f 70 74 69 6d 69 7a this.** optimiz
15cd0 61 74 69 6f 6e 2c 20 74 68 65 20 64 65 6c 65 74 ation, the delet
15ce0 69 6f 6e 73 20 69 6e 20 22 44 45 4c 45 54 45 20 ions in "DELETE
15cf0 46 52 4f 4d 20 74 61 62 6c 65 22 20 61 72 65 20 FROM table" are
15d00 6e 6f 74 20 72 6f 77 20 63 68 61 6e 67 65 73 20 not row changes
15d10 61 6e 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 and.** will not
15d20 62 65 20 63 6f 75 6e 74 65 64 20 62 79 20 74 68 be counted by th
15d30 65 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 e sqlite3_change
15d40 73 28 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f s() or [sqlite3_
15d50 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d total_changes()]
15d60 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2c 20 72 .** functions, r
15d70 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 egardless of the
15d80 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 number of eleme
15d90 6e 74 73 20 74 68 61 74 20 77 65 72 65 20 6f 72 nts that were or
15da0 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 69 6e 20 74 iginally.** in t
15db0 68 65 20 74 61 62 6c 65 2e 20 20 54 6f 20 67 65 he table. To ge
15dc0 74 20 61 6e 20 61 63 63 75 72 61 74 65 20 63 6f t an accurate co
15dd0 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 unt of the numbe
15de0 72 20 6f 66 20 72 6f 77 73 20 64 65 6c 65 74 65 r of rows delete
15df0 64 2c 20 75 73 65 0a 2a 2a 20 22 44 45 4c 45 54 d, use.** "DELET
15e00 45 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 E FROM table WHE
15e10 52 45 20 31 22 20 69 6e 73 74 65 61 64 2e 20 20 RE 1" instead.
15e20 20 4f 72 20 72 65 63 6f 6d 70 69 6c 65 20 75 73 Or recompile us
15e30 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 ing the.** [SQLI
15e40 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 TE_OMIT_TRUNCATE
15e50 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 5d 20 63 _OPTIMIZATION] c
15e60 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 ompile-time opti
15e70 6f 6e 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 on to disable th
15e80 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f e.** optimizatio
15e90 6e 20 6f 6e 20 61 6c 6c 20 71 75 65 72 69 65 73 n on all queries
15ea0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
15eb0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 the [sqlite3_ch
15ec0 61 6e 67 65 73 28 29 5d 20 69 6e 74 65 72 66 61 anges()] interfa
15ed0 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 ce..**.** INVARI
15ee0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 ANTS:.**.** {H12
15ef0 32 36 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 261} The [sqlite
15f00 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 3_total_changes(
15f10 29 5d 20 72 65 74 75 72 6e 73 20 74 68 65 20 74 )] returns the t
15f20 6f 74 61 6c 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 otal number.**
15f30 20 20 20 20 20 20 20 20 6f 66 20 72 6f 77 20 63 of row c
15f40 68 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 hanges caused by
15f50 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
15f60 20 61 6e 64 2f 6f 72 20 44 45 4c 45 54 45 0a 2a and/or DELETE.*
15f70 2a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 * state
15f80 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 73 61 6d ments on the sam
15f90 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
15fa0 65 63 74 69 6f 6e 5d 2c 20 69 6e 20 61 6e 79 0a ection], in any.
15fb0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 72 69 67 ** trig
15fc0 67 65 72 20 63 6f 6e 74 65 78 74 2c 20 73 69 6e ger context, sin
15fd0 63 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ce the database
15fe0 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 61 73 20 63 connection was c
15ff0 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 reated..**.** {H
16000 31 32 32 36 33 7d 20 53 74 61 74 65 6d 65 6e 74 12263} Statement
16010 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 44 s of the form "D
16020 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 ELETE FROM table
16030 6e 61 6d 65 22 20 77 69 74 68 20 6e 6f 0a 2a 2a name" with no.**
16040 20 20 20 20 20 20 20 20 20 20 57 48 45 52 45 20 WHERE
16050 63 6c 61 75 73 65 20 73 68 61 6c 6c 20 6e 6f 74 clause shall not
16060 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 change the valu
16070 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 20 20 e returned.**
16080 20 20 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74 by [sqlit
16090 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
160a0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d ()]..**.** ASSUM
160b0 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 PTIONS:.**.** {A
160c0 31 32 32 36 34 7d 20 49 66 20 61 20 73 65 70 61 12264} If a sepa
160d0 72 61 74 65 20 74 68 72 65 61 64 20 6d 61 6b 65 rate thread make
160e0 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 s changes on the
160f0 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 same database c
16100 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 onnection.**
16110 20 20 20 20 20 20 77 68 69 6c 65 20 5b 73 71 6c while [sql
16120 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 ite3_total_chang
16130 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 es()] is running
16140 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 0a then the value.
16150 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ** retu
16160 72 6e 65 64 20 69 73 20 75 6e 70 72 65 64 69 63 rned is unpredic
16170 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 table and not me
16180 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c aningful..*/.SQL
16190 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
161a0 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 te3_total_change
161b0 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a s(sqlite3*);../*
161c0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e .** CAPI3REF: In
161d0 74 65 72 72 75 70 74 20 41 20 4c 6f 6e 67 2d 52 terrupt A Long-R
161e0 75 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b 48 31 unning Query {H1
161f0 32 32 37 30 7d 20 3c 53 33 30 35 30 30 3e 0a 2a 2270} <S30500>.*
16200 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
16210 6f 6e 20 63 61 75 73 65 73 20 61 6e 79 20 70 65 on causes any pe
16220 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65 20 6f nding database o
16230 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 62 6f 72 peration to abor
16240 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 t and.** return
16250 61 74 20 69 74 73 20 65 61 72 6c 69 65 73 74 20 at its earliest
16260 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 54 68 69 opportunity. Thi
16270 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 79 70 s routine is typ
16280 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64 ically.** called
16290 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 in response to
162a0 61 20 75 73 65 72 20 61 63 74 69 6f 6e 20 73 75 a user action su
162b0 63 68 20 61 73 20 70 72 65 73 73 69 6e 67 20 22 ch as pressing "
162c0 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 20 43 74 Cancel".** or Ct
162d0 72 6c 2d 43 20 77 68 65 72 65 20 74 68 65 20 75 rl-C where the u
162e0 73 65 72 20 77 61 6e 74 73 20 61 20 6c 6f 6e 67 ser wants a long
162f0 20 71 75 65 72 79 20 6f 70 65 72 61 74 69 6f 6e query operation
16300 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65 to halt.** imme
16310 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 diately..**.** I
16320 74 20 69 73 20 73 61 66 65 20 74 6f 20 63 61 6c t is safe to cal
16330 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 l this routine f
16340 72 6f 6d 20 61 20 74 68 72 65 61 64 20 64 69 66 rom a thread dif
16350 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a ferent from the.
16360 2a 2a 20 74 68 72 65 61 64 20 74 68 61 74 20 69 ** thread that i
16370 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e s currently runn
16380 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 ing the database
16390 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 operation. But
163a0 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 61 it.** is not sa
163b0 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 fe to call this
163c0 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61 20 5b routine with a [
163d0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
163e0 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 69 73 20 ion] that.** is
163f0 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68 74 20 closed or might
16400 63 6c 6f 73 65 20 62 65 66 6f 72 65 20 73 71 6c close before sql
16410 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
16420 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 returns..**.**
16430 49 66 20 61 6e 20 53 51 4c 20 6f 70 65 72 61 74 If an SQL operat
16440 69 6f 6e 20 69 73 20 76 65 72 79 20 6e 65 61 72 ion is very near
16450 6c 79 20 66 69 6e 69 73 68 65 64 20 61 74 20 74 ly finished at t
16460 68 65 20 74 69 6d 65 20 77 68 65 6e 0a 2a 2a 20 he time when.**
16470 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
16480 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 t() is called, t
16490 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 hen it might not
164a0 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 have an opportu
164b0 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 65 20 69 6e nity.** to be in
164c0 74 65 72 72 75 70 74 65 64 20 61 6e 64 20 6d 69 terrupted and mi
164d0 67 68 74 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 ght continue to
164e0 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a completion..**.*
164f0 2a 20 41 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 * An SQL operati
16500 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 74 65 72 on that is inter
16510 72 75 70 74 65 64 20 77 69 6c 6c 20 72 65 74 75 rupted will retu
16520 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 rn [SQLITE_INTER
16530 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 RUPT]..** If the
16540 20 69 6e 74 65 72 72 75 70 74 65 64 20 53 51 4c interrupted SQL
16550 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e operation is an
16560 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
16570 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 or DELETE.** th
16580 61 74 20 69 73 20 69 6e 73 69 64 65 20 61 6e 20 at is inside an
16590 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 explicit transac
165a0 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 65 tion, then the e
165b0 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f ntire transactio
165c0 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 6f 6c n.** will be rol
165d0 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 led back automat
165e0 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 ically..**.** A
165f0 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
16600 69 6e 74 65 72 72 75 70 74 28 29 20 68 61 73 20 interrupt() has
16610 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 53 51 4c no effect on SQL
16620 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 statements.** t
16630 68 61 74 20 61 72 65 20 73 74 61 72 74 65 64 20 hat are started
16640 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 69 6e after sqlite3_in
16650 74 65 72 72 75 70 74 28 29 20 72 65 74 75 72 6e terrupt() return
16660 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 s..**.** INVARIA
16670 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 NTS:.**.** {H122
16680 37 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 71} The [sqlite3
16690 5f 69 6e 74 65 72 72 75 70 74 28 29 5d 20 69 6e _interrupt()] in
166a0 74 65 72 66 61 63 65 20 77 69 6c 6c 20 66 6f 72 terface will for
166b0 63 65 20 61 6c 6c 20 72 75 6e 6e 69 6e 67 0a 2a ce all running.*
166c0 2a 20 20 20 20 20 20 20 20 20 20 53 51 4c 20 73 * SQL s
166d0 74 61 74 65 6d 65 6e 74 73 20 61 73 73 6f 63 69 tatements associ
166e0 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 61 ated with the sa
166f0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
16700 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 ection.**
16710 20 20 20 74 6f 20 68 61 6c 74 20 61 66 74 65 72 to halt after
16720 20 70 72 6f 63 65 73 73 69 6e 67 20 61 74 20 6d processing at m
16730 6f 73 74 20 6f 6e 65 20 61 64 64 69 74 69 6f 6e ost one addition
16740 61 6c 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 0a al row of data..
16750 2a 2a 0a 2a 2a 20 7b 48 31 32 32 37 32 7d 20 41 **.** {H12272} A
16760 6e 79 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ny SQL statement
16770 20 74 68 61 74 20 69 73 20 69 6e 74 65 72 72 75 that is interru
16780 70 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 pted by [sqlite3
16790 5f 69 6e 74 65 72 72 75 70 74 28 29 5d 0a 2a 2a _interrupt()].**
167a0 20 20 20 20 20 20 20 20 20 20 77 69 6c 6c 20 72 will r
167b0 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e eturn [SQLITE_IN
167c0 54 45 52 52 55 50 54 5d 2e 0a 2a 2a 0a 2a 2a 20 TERRUPT]..**.**
167d0 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a ASSUMPTIONS:.**.
167e0 2a 2a 20 7b 41 31 32 32 37 39 7d 20 49 66 20 74 ** {A12279} If t
167f0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
16800 65 63 74 69 6f 6e 20 63 6c 6f 73 65 73 20 77 68 ection closes wh
16810 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 ile [sqlite3_int
16820 65 72 72 75 70 74 28 29 5d 0a 2a 2a 20 20 20 20 errupt()].**
16830 20 20 20 20 20 20 69 73 20 72 75 6e 6e 69 6e 67 is running
16840 20 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67 73 then bad things
16850 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 70 will likely hap
16860 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 pen..*/.SQLITE_A
16870 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
16880 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65 interrupt(sqlite
16890 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 3*);../*.** CAPI
168a0 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 3REF: Determine
168b0 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d If An SQL Statem
168c0 65 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 20 ent Is Complete
168d0 7b 48 31 30 35 31 30 7d 20 3c 53 37 30 32 30 30 {H10510} <S70200
168e0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f >.**.** These ro
168f0 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 66 75 utines are usefu
16900 6c 20 66 6f 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 l for command-li
16910 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65 74 65 ne input to dete
16920 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a 2a 20 rmine if the.**
16930 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 currently entere
16940 64 20 74 65 78 74 20 73 65 65 6d 73 20 74 6f 20 d text seems to
16950 66 6f 72 6d 20 63 6f 6d 70 6c 65 74 65 20 61 20 form complete a
16960 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 SQL statement or
16970 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f 6e 61 .** if additiona
16980 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65 64 65 l input is neede
16990 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69 6e 67 d before sending
169a0 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f 0a 2a the text into.*
169b0 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 61 72 * SQLite for par
169c0 73 69 6e 67 2e 20 20 54 68 65 73 65 20 72 6f 75 sing. These rou
169d0 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 72 75 tines return tru
169e0 65 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73 e if the input s
169f0 74 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73 tring.** appears
16a00 20 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74 to be a complet
16a10 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e e SQL statement.
16a20 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 69 73 A statement is
16a30 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a 2a judged to be.**
16a40 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 20 complete if it
16a50 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d 69 ends with a semi
16a60 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 20 colon token and
16a70 69 73 20 6e 6f 74 20 61 20 66 72 61 67 6d 65 6e is not a fragmen
16a80 74 20 6f 66 20 61 0a 2a 2a 20 43 52 45 41 54 45 t of a.** CREATE
16a90 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 TRIGGER stateme
16aa0 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e 73 20 nt. Semicolons
16ab0 74 68 61 74 20 61 72 65 20 65 6d 62 65 64 64 65 that are embedde
16ac0 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74 72 69 d within.** stri
16ad0 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72 20 71 ng literals or q
16ae0 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65 72 uoted identifier
16af0 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d 65 6e names or commen
16b00 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 69 6e ts are not.** in
16b10 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65 6e 73 dependent tokens
16b20 20 28 74 68 65 79 20 61 72 65 20 70 61 72 74 20 (they are part
16b30 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e 20 of the token in
16b40 77 68 69 63 68 20 74 68 65 79 20 61 72 65 0a 2a which they are.*
16b50 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e 64 20 * embedded) and
16b60 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f 75 6e thus do not coun
16b70 74 20 61 73 20 61 20 73 74 61 74 65 6d 65 6e 74 t as a statement
16b80 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a terminator..**.
16b90 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
16ba0 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 20 74 s do not parse t
16bb0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
16bc0 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c 20 6e s thus.** will n
16bd0 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74 61 63 ot detect syntac
16be0 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 65 63 tically incorrec
16bf0 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 t SQL..**.** INV
16c00 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
16c10 48 31 30 35 31 31 7d 20 41 20 73 75 63 63 65 73 H10511} A succes
16c20 73 66 75 6c 20 65 76 61 6c 75 61 74 69 6f 6e 20 sful evaluation
16c30 6f 66 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 of [sqlite3_comp
16c40 6c 65 74 65 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 lete()] or.**
16c50 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
16c60 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 66 75 complete16()] fu
16c70 6e 63 74 69 6f 6e 73 20 73 68 61 6c 6c 0a 2a 2a nctions shall.**
16c80 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
16c90 20 61 20 6e 75 6d 65 72 69 63 20 31 20 69 66 20 a numeric 1 if
16ca0 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 and only if the
16cb0 6c 61 73 74 20 6e 6f 6e 2d 77 68 69 74 65 73 70 last non-whitesp
16cc0 61 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ace.**
16cd0 74 6f 6b 65 6e 20 69 6e 20 74 68 65 69 72 20 69 token in their i
16ce0 6e 70 75 74 20 69 73 20 61 20 73 65 6d 69 63 6f nput is a semico
16cf0 6c 6f 6e 20 74 68 61 74 20 69 73 20 6e 6f 74 20 lon that is not
16d00 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 20 20 in between.**
16d10 20 20 20 20 20 20 20 74 68 65 20 42 45 47 49 4e the BEGIN
16d20 20 61 6e 64 20 45 4e 44 20 6f 66 20 61 20 43 52 and END of a CR
16d30 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61 EATE TRIGGER sta
16d40 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 tement..**.** {H
16d50 31 30 35 31 32 7d 20 49 66 20 61 20 6d 65 6d 6f 10512} If a memo
16d60 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ry allocation er
16d70 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
16d80 67 20 61 6e 20 69 6e 76 6f 63 61 74 69 6f 6e 0a g an invocation.
16d90 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 5b ** of [
16da0 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 sqlite3_complete
16db0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
16dc0 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 74 68 complete16()] th
16dd0 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 en the.**
16de0 20 20 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c routine shall
16df0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
16e00 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 NOMEM]..**.** AS
16e10 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a SUMPTIONS:.**.**
16e20 20 7b 41 31 30 35 31 32 7d 20 54 68 65 20 69 6e {A10512} The in
16e30 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f put to [sqlite3_
16e40 63 6f 6d 70 6c 65 74 65 28 29 5d 20 6d 75 73 74 complete()] must
16e50 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 be a zero-termi
16e60 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 nated.**
16e70 20 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a UTF-8 string..
16e80 2a 2a 0a 2a 2a 20 7b 41 31 30 35 31 33 7d 20 54 **.** {A10513} T
16e90 68 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c he input to [sql
16ea0 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 ite3_complete16(
16eb0 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72 )] must be a zer
16ec0 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 o-terminated.**
16ed0 20 20 20 20 20 20 20 20 20 55 54 46 2d 31 36 20 UTF-16
16ee0 73 74 72 69 6e 67 20 69 6e 20 6e 61 74 69 76 65 string in native
16ef0 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a byte order..*/.
16f00 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
16f10 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 qlite3_complete(
16f20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 29 const char *sql)
16f30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
16f40 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 sqlite3_complet
16f50 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a e16(const void *
16f60 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 sql);../*.** CAP
16f70 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 I3REF: Register
16f80 41 20 43 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61 A Callback To Ha
16f90 6e 64 6c 65 20 53 51 4c 49 54 45 5f 42 55 53 59 ndle SQLITE_BUSY
16fa0 20 45 72 72 6f 72 73 20 7b 48 31 32 33 31 30 7d Errors {H12310}
16fb0 20 3c 53 34 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S40400>.**.**
16fc0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 This routine set
16fd0 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e s a callback fun
16fe0 63 74 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 74 ction that might
16ff0 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e be invoked when
17000 65 76 65 72 0a 2a 2a 20 61 6e 20 61 74 74 65 6d ever.** an attem
17010 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 pt is made to op
17020 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 61 en a database ta
17030 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 ble that another
17040 20 74 68 72 65 61 64 0a 2a 2a 20 6f 72 20 70 72 thread.** or pr
17050 6f 63 65 73 73 20 68 61 73 20 6c 6f 63 6b 65 64 ocess has locked
17060 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 ..**.** If the b
17070 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 usy callback is
17080 4e 55 4c 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49 NULL, then [SQLI
17090 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c TE_BUSY] or [SQL
170a0 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
170b0 44 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 D].** is returne
170c0 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 d immediately up
170d0 6f 6e 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 on encountering
170e0 74 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 the lock. If the
170f0 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a busy callback.*
17100 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 * is not NULL, t
17110 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b hen the callback
17120 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 will be invoked
17130 20 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 with two argume
17140 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 nts..**.** The f
17150 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
17160 20 74 68 65 20 68 61 6e 64 6c 65 72 20 69 73 20 the handler is
17170 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f a copy of the vo
17180 69 64 2a 20 70 6f 69 6e 74 65 72 20 77 68 69 63 id* pointer whic
17190 68 0a 2a 2a 20 69 73 20 74 68 65 20 74 68 69 72 h.** is the thir
171a0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 d argument to sq
171b0 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c lite3_busy_handl
171c0 65 72 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e er(). The secon
171d0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a d argument to.**
171e0 20 74 68 65 20 68 61 6e 64 6c 65 72 20 63 61 6c the handler cal
171f0 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 75 6d lback is the num
17200 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 ber of times tha
17210 74 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c t the busy handl
17220 65 72 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69 er has.** been i
17230 6e 76 6f 6b 65 64 20 66 6f 72 20 74 68 69 73 20 nvoked for this
17240 6c 6f 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20 locking event.
17250 49 66 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 If the.** busy c
17260 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 allback returns
17270 30 2c 20 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 0, then no addit
17280 69 6f 6e 61 6c 20 61 74 74 65 6d 70 74 73 20 61 ional attempts a
17290 72 65 20 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63 re made to.** ac
172a0 63 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 cess the databas
172b0 65 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 e and [SQLITE_BU
172c0 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 SY] or [SQLITE_I
172d0 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 OERR_BLOCKED] is
172e0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 returned..** If
172f0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 the callback re
17300 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 turns non-zero,
17310 74 68 65 6e 20 61 6e 6f 74 68 65 72 20 61 74 74 then another att
17320 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 empt.** is made
17330 74 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 to open the data
17340 62 61 73 65 20 66 6f 72 20 72 65 61 64 69 6e 67 base for reading
17350 20 61 6e 64 20 74 68 65 20 63 79 63 6c 65 20 72 and the cycle r
17360 65 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 epeats..**.** Th
17370 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20 e presence of a
17380 62 75 73 79 20 68 61 6e 64 6c 65 72 20 64 6f 65 busy handler doe
17390 73 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20 s not guarantee
173a0 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 that it will be
173b0 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 invoked.** when
173c0 74 68 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f there is lock co
173d0 6e 74 65 6e 74 69 6f 6e 2e 20 49 66 20 53 51 4c ntention. If SQL
173e0 69 74 65 20 64 65 74 65 72 6d 69 6e 65 73 20 74 ite determines t
173f0 68 61 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 hat invoking the
17400 20 62 75 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 busy.** handler
17410 20 63 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e could result in
17420 20 61 20 64 65 61 64 6c 6f 63 6b 2c 20 69 74 20 a deadlock, it
17430 77 69 6c 6c 20 67 6f 20 61 68 65 61 64 20 61 6e will go ahead an
17440 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 d return [SQLITE
17450 5f 42 55 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 _BUSY].** or [SQ
17460 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b LITE_IOERR_BLOCK
17470 45 44 5d 20 69 6e 73 74 65 61 64 20 6f 66 20 69 ED] instead of i
17480 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 nvoking the busy
17490 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e handler..** Con
174a0 73 69 64 65 72 20 61 20 73 63 65 6e 61 72 69 6f sider a scenario
174b0 20 77 68 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 where one proce
174c0 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 ss is holding a
174d0 72 65 61 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a read lock that.*
174e0 2a 20 69 74 20 69 73 20 74 72 79 69 6e 67 20 74 * it is trying t
174f0 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 o promote to a r
17500 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 eserved lock and
17510 0a 2a 2a 20 61 20 73 65 63 6f 6e 64 20 70 72 6f .** a second pro
17520 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 cess is holding
17530 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 a reserved lock
17540 74 68 61 74 20 69 74 20 69 73 20 74 72 79 69 6e that it is tryin
17550 67 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 g.** to promote
17560 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 to an exclusive
17570 6c 6f 63 6b 2e 20 20 54 68 65 20 66 69 72 73 74 lock. The first
17580 20 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 process cannot
17590 70 72 6f 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 proceed.** becau
175a0 73 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 se it is blocked
175b0 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 by the second a
175c0 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 nd the second pr
175d0 6f 63 65 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 ocess cannot.**
175e0 70 72 6f 63 65 65 64 20 62 65 63 61 75 73 65 20 proceed because
175f0 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 it is blocked by
17600 20 74 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 the first. If
17610 62 6f 74 68 20 70 72 6f 63 65 73 73 65 73 0a 2a both processes.*
17620 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 * invoke the bus
17630 79 20 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 y handlers, neit
17640 68 65 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e her will make an
17650 79 20 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65 y progress. The
17660 72 65 66 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 refore,.** SQLit
17670 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 e returns [SQLIT
17680 45 5f 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20 E_BUSY] for the
17690 66 69 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68 first process, h
176a0 6f 70 69 6e 67 20 74 68 61 74 20 74 68 69 73 0a oping that this.
176b0 2a 2a 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 ** will induce t
176c0 68 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 he first process
176d0 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20 to release its
176e0 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c read lock and al
176f0 6c 6f 77 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e low.** the secon
17700 64 20 70 72 6f 63 65 73 73 20 74 6f 20 70 72 6f d process to pro
17710 63 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ceed..**.** The
17720 64 65 66 61 75 6c 74 20 62 75 73 79 20 63 61 6c default busy cal
17730 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a lback is NULL..*
17740 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 *.** The [SQLITE
17750 5f 42 55 53 59 5d 20 65 72 72 6f 72 20 69 73 20 _BUSY] error is
17760 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 5b 53 51 converted to [SQ
17770 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b LITE_IOERR_BLOCK
17780 45 44 5d 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 ED].** when SQLi
17790 74 65 20 69 73 20 69 6e 20 74 68 65 20 6d 69 64 te is in the mid
177a0 64 6c 65 20 6f 66 20 61 20 6c 61 72 67 65 20 74 dle of a large t
177b0 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 65 72 65 ransaction where
177c0 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e all the.** chan
177d0 67 65 73 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 ges will not fit
177e0 20 69 6e 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d into the in-mem
177f0 6f 72 79 20 63 61 63 68 65 2e 20 20 53 51 4c 69 ory cache. SQLi
17800 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 te will.** alrea
17810 64 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 dy hold a RESERV
17820 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 ED lock on the d
17830 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 atabase file, bu
17840 74 20 69 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f t it needs.** to
17850 20 70 72 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f promote this lo
17860 63 6b 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 ck to EXCLUSIVE
17870 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 73 so that it can s
17880 70 69 6c 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 pill cache.** pa
17890 67 65 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 ges into the dat
178a0 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f abase file witho
178b0 75 74 20 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 ut harm to concu
178c0 72 72 65 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 rrent.** readers
178d0 2e 20 20 49 66 20 69 74 20 69 73 20 75 6e 61 62 . If it is unab
178e0 6c 65 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 le to promote th
178f0 65 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 e lock, then the
17900 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 in-memory.** ca
17910 63 68 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 che will be left
17920 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 in an inconsist
17930 65 6e 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f ent state and so
17940 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f the error.** co
17950 64 65 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 de is promoted f
17960 72 6f 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65 rom the relative
17970 6c 79 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 ly benign [SQLIT
17980 45 5f 42 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 E_BUSY] to.** th
17990 65 20 6d 6f 72 65 20 73 65 76 65 72 65 20 5b 53 e more severe [S
179a0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 QLITE_IOERR_BLOC
179b0 4b 45 44 5d 2e 20 20 54 68 69 73 20 65 72 72 6f KED]. This erro
179c0 72 20 63 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e r code promotion
179d0 0a 2a 2a 20 66 6f 72 63 65 73 20 61 6e 20 61 75 .** forces an au
179e0 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b tomatic rollback
179f0 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 2e of the changes.
17a00 20 20 53 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 See the.** <a
17a10 68 72 65 66 3d 22 2f 63 76 73 74 72 61 63 2f 77 href="/cvstrac/w
17a20 69 6b 69 3f 70 3d 43 6f 72 72 75 70 74 69 6f 6e iki?p=Corruption
17a30 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 FollowingBusyErr
17a40 6f 72 22 3e 0a 2a 2a 20 43 6f 72 72 75 70 74 69 or">.** Corrupti
17a50 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 onFollowingBusyE
17a60 72 72 6f 72 3c 2f 61 3e 20 77 69 6b 69 20 70 61 rror</a> wiki pa
17a70 67 65 20 66 6f 72 20 61 20 64 69 73 63 75 73 73 ge for a discuss
17a80 69 6f 6e 20 6f 66 20 77 68 79 0a 2a 2a 20 74 68 ion of why.** th
17a90 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e is is important.
17aa0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e .**.** There can
17ab0 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c only be a singl
17ac0 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 64 e busy handler d
17ad0 65 66 69 6e 65 64 20 66 6f 72 20 65 61 63 68 0a efined for each.
17ae0 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
17af0 6e 65 63 74 69 6f 6e 5d 2e 20 20 53 65 74 74 69 nection]. Setti
17b00 6e 67 20 61 20 6e 65 77 20 62 75 73 79 20 68 61 ng a new busy ha
17b10 6e 64 6c 65 72 20 63 6c 65 61 72 73 20 61 6e 79 ndler clears any
17b20 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 .** previously s
17b30 65 74 20 68 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 et handler. Not
17b40 65 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b e that calling [
17b50 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d sqlite3_busy_tim
17b60 65 6f 75 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 eout()].** will
17b70 61 6c 73 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 also set or clea
17b80 72 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c r the busy handl
17b90 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 er..**.** The bu
17ba0 73 79 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 sy callback shou
17bb0 6c 64 20 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 ld not take any
17bc0 61 63 74 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f actions which mo
17bd0 64 69 66 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 dify the.** data
17be0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
17bf0 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 that invoked the
17c00 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 busy handler.
17c10 41 6e 79 20 73 75 63 68 20 61 63 74 69 6f 6e 73 Any such actions
17c20 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20 75 6e .** result in un
17c30 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 defined behavior
17c40 2e 0a 2a 2a 20 0a 2a 2a 20 49 4e 56 41 52 49 41 ..** .** INVARIA
17c50 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 NTS:.**.** {H123
17c60 31 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 11} The [sqlite3
17c70 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 44 2c _busy_handler(D,
17c80 43 2c 41 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73 C,A)] function s
17c90 68 61 6c 6c 20 72 65 70 6c 61 63 65 0a 2a 2a 20 hall replace.**
17ca0 20 20 20 20 20 20 20 20 20 62 75 73 79 20 63 61 busy ca
17cb0 6c 6c 62 61 63 6b 20 69 6e 20 74 68 65 20 5b 64 llback in the [d
17cc0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
17cd0 6f 6e 5d 20 44 20 77 69 74 68 20 61 20 6e 65 77 on] D with a new
17ce0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 6e .** a n
17cf0 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 ew busy handler
17d00 43 20 61 6e 64 20 61 70 70 6c 69 63 61 74 69 6f C and applicatio
17d10 6e 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 41 n data pointer A
17d20 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 32 7d ..**.** {H12312}
17d30 20 4e 65 77 6c 79 20 63 72 65 61 74 65 64 20 5b Newly created [
17d40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
17d50 69 6f 6e 73 5d 20 73 68 61 6c 6c 20 68 61 76 65 ions] shall have
17d60 20 61 20 62 75 73 79 0a 2a 2a 20 20 20 20 20 20 a busy.**
17d70 20 20 20 20 68 61 6e 64 6c 65 72 20 6f 66 20 4e handler of N
17d80 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 ULL..**.** {H123
17d90 31 34 7d 20 57 68 65 6e 20 74 77 6f 20 6f 72 20 14} When two or
17da0 6d 6f 72 65 20 5b 64 61 74 61 62 61 73 65 20 63 more [database c
17db0 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 73 68 61 72 onnections] shar
17dc0 65 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 e a.**
17dd0 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f [sqlite3_enable_
17de0 73 68 61 72 65 64 5f 63 61 63 68 65 20 7c 20 63 shared_cache | c
17df0 6f 6d 6d 6f 6e 20 63 61 63 68 65 5d 2c 0a 2a 2a ommon cache],.**
17e00 20 20 20 20 20 20 20 20 20 20 74 68 65 20 62 75 the bu
17e10 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 74 sy handler for t
17e20 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
17e30 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 ection currently
17e40 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 using.**
17e50 20 20 20 74 68 65 20 63 61 63 68 65 20 73 68 61 the cache sha
17e60 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 ll be invoked wh
17e70 65 6e 20 74 68 65 20 63 61 63 68 65 20 65 6e 63 en the cache enc
17e80 6f 75 6e 74 65 72 73 20 61 20 6c 6f 63 6b 2e 0a ounters a lock..
17e90 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 36 7d 20 49 **.** {H12316} I
17ea0 66 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 f a busy handler
17eb0 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
17ec0 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 s zero, then the
17ed0 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 SQLite interfac
17ee0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 e.** th
17ef0 61 74 20 70 72 6f 76 6f 6b 65 64 20 74 68 65 20 at provoked the
17f00 6c 6f 63 6b 69 6e 67 20 65 76 65 6e 74 20 73 68 locking event sh
17f10 61 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 all return [SQLI
17f20 54 45 5f 42 55 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20 TE_BUSY]..**.**
17f30 7b 48 31 32 33 31 38 7d 20 53 51 4c 69 74 65 20 {H12318} SQLite
17f40 73 68 61 6c 6c 20 69 6e 76 6f 6b 65 73 20 74 68 shall invokes th
17f50 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 77 e busy handler w
17f60 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 ith two argument
17f70 73 20 77 68 69 63 68 0a 2a 2a 20 20 20 20 20 20 s which.**
17f80 20 20 20 20 61 72 65 20 61 20 63 6f 70 79 20 6f are a copy o
17f90 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 73 75 f the pointer su
17fa0 70 70 6c 69 65 64 20 62 79 20 74 68 65 20 33 72 pplied by the 3r
17fb0 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a d parameter to.*
17fc0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
17fd0 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 te3_busy_handler
17fe0 28 29 5d 20 61 6e 64 20 61 20 63 6f 75 6e 74 20 ()] and a count
17ff0 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 of the number of
18000 20 70 72 69 6f 72 0a 2a 2a 20 20 20 20 20 20 20 prior.**
18010 20 20 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f invocations o
18020 66 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c f the busy handl
18030 65 72 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 er for the same
18040 6c 6f 63 6b 69 6e 67 20 65 76 65 6e 74 2e 0a 2a locking event..*
18050 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 *.** ASSUMPTIONS
18060 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 33 31 39 7d :.**.** {A12319}
18070 20 41 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 A busy handler
18080 6d 75 73 74 20 6e 6f 74 20 63 6c 6f 73 65 20 74 must not close t
18090 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
180a0 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 ection.**
180b0 20 20 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20 or [prepared
180c0 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 statement] that
180d0 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79 invoked the busy
180e0 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c handler..*/.SQL
180f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
18100 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 te3_busy_handler
18110 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a (sqlite3*, int(*
18120 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 6f )(void*,int), vo
18130 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 id*);../*.** CAP
18140 49 33 52 45 46 3a 20 53 65 74 20 41 20 42 75 73 I3REF: Set A Bus
18150 79 20 54 69 6d 65 6f 75 74 20 7b 48 31 32 33 34 y Timeout {H1234
18160 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 0} <S40410>.**.*
18170 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 * This routine s
18180 65 74 73 20 61 20 5b 73 71 6c 69 74 65 33 5f 62 ets a [sqlite3_b
18190 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 usy_handler | bu
181a0 73 79 20 68 61 6e 64 6c 65 72 5d 20 74 68 61 74 sy handler] that
181b0 20 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 20 61 sleeps.** for a
181c0 20 73 70 65 63 69 66 69 65 64 20 61 6d 6f 75 6e specified amoun
181d0 74 20 6f 66 20 74 69 6d 65 20 77 68 65 6e 20 61 t of time when a
181e0 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 table is locked
181f0 2e 20 20 54 68 65 20 68 61 6e 64 6c 65 72 0a 2a . The handler.*
18200 2a 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75 6c * will sleep mul
18210 74 69 70 6c 65 20 74 69 6d 65 73 20 75 6e 74 69 tiple times unti
18220 6c 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20 l at least "ms"
18230 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 milliseconds of
18240 73 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61 76 65 sleeping.** have
18250 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 7b 48 accumulated. {H
18260 31 32 33 34 33 7d 20 41 66 74 65 72 20 22 6d 73 12343} After "ms
18270 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f " milliseconds o
18280 66 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 f sleeping,.** t
18290 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74 75 72 he handler retur
182a0 6e 73 20 30 20 77 68 69 63 68 20 63 61 75 73 65 ns 0 which cause
182b0 73 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 s [sqlite3_step(
182c0 29 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 )] to return.**
182d0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 [SQLITE_BUSY] or
182e0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 [SQLITE_IOERR_B
182f0 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 LOCKED]..**.** C
18300 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 alling this rout
18310 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67 75 ine with an argu
18320 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 6f ment less than o
18330 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 0a r equal to zero.
18340 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c 6c ** turns off all
18350 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e 0a busy handlers..
18360 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 **.** There can
18370 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 only be a single
18380 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f busy handler fo
18390 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a r a particular.*
183a0 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e * [database conn
183b0 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 ection] any any
183c0 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 given moment. I
183d0 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79 20 68 f another busy h
183e0 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64 65 andler.** was de
183f0 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20 5b 73 fined (using [s
18400 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 qlite3_busy_hand
18410 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20 74 6f ler()]) prior to
18420 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 calling.** this
18430 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 6f routine, that o
18440 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 ther busy handle
18450 72 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 2a 2a r is cleared..**
18460 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
18470 2a 2a 0a 2a 2a 20 7b 48 31 32 33 34 31 7d 20 54 **.** {H12341} T
18480 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 he [sqlite3_busy
18490 5f 74 69 6d 65 6f 75 74 28 29 5d 20 66 75 6e 63 _timeout()] func
184a0 74 69 6f 6e 20 73 68 61 6c 6c 20 6f 76 65 72 72 tion shall overr
184b0 69 64 65 20 61 6e 79 20 70 72 69 6f 72 0a 2a 2a ide any prior.**
184c0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
184d0 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 e3_busy_timeout(
184e0 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 62 )] or [sqlite3_b
184f0 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 20 73 usy_handler()] s
18500 65 74 74 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 etting.**
18510 20 20 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b on the same [
18520 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
18530 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 ion]..**.** {H12
18540 33 34 33 7d 20 49 66 20 74 68 65 20 32 6e 64 20 343} If the 2nd
18550 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 parameter to [sq
18560 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f lite3_busy_timeo
18570 75 74 28 29 5d 20 69 73 20 6c 65 73 73 20 74 68 ut()] is less th
18580 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f an.** o
18590 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2c r equal to zero,
185a0 20 74 68 65 6e 20 74 68 65 20 62 75 73 79 20 68 then the busy h
185b0 61 6e 64 6c 65 72 20 73 68 61 6c 6c 20 62 65 20 andler shall be
185c0 63 6c 65 61 72 65 64 20 73 6f 20 74 68 61 74 0a cleared so that.
185d0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6c 6c 20 ** all
185e0 73 75 62 73 65 71 75 65 6e 74 20 6c 6f 63 6b 69 subsequent locki
185f0 6e 67 20 65 76 65 6e 74 73 20 69 6d 6d 65 64 69 ng events immedi
18600 61 74 65 6c 79 20 72 65 74 75 72 6e 20 5b 53 51 ately return [SQ
18610 4c 49 54 45 5f 42 55 53 59 5d 2e 0a 2a 2a 0a 2a LITE_BUSY]..**.*
18620 2a 20 7b 48 31 32 33 34 34 7d 20 49 66 20 74 68 * {H12344} If th
18630 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 e 2nd parameter
18640 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 to [sqlite3_busy
18650 5f 74 69 6d 65 6f 75 74 28 29 5d 20 69 73 20 61 _timeout()] is a
18660 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 20 20 20 positive.**
18670 20 20 20 20 20 20 6e 75 6d 62 65 72 20 4e 2c 20 number N,
18680 74 68 65 6e 20 61 20 62 75 73 79 20 68 61 6e 64 then a busy hand
18690 6c 65 72 20 73 68 61 6c 6c 20 62 65 20 73 65 74 ler shall be set
186a0 20 74 68 61 74 20 72 65 70 65 61 74 65 64 6c 79 that repeatedly
186b0 20 63 61 6c 6c 73 0a 2a 2a 20 20 20 20 20 20 20 calls.**
186c0 20 20 20 74 68 65 20 78 53 6c 65 65 70 28 29 20 the xSleep()
186d0 6d 65 74 68 6f 64 20 69 6e 20 74 68 65 20 5b 73 method in the [s
186e0 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 56 46 53 qlite3_vfs | VFS
186f0 20 69 6e 74 65 72 66 61 63 65 5d 20 75 6e 74 69 interface] unti
18700 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65 69 l.** ei
18710 74 68 65 72 20 74 68 65 20 6c 6f 63 6b 20 63 6c ther the lock cl
18720 65 61 72 73 20 6f 72 20 75 6e 74 69 6c 20 74 68 ears or until th
18730 65 20 63 75 6d 75 6c 61 74 69 76 65 20 73 6c 65 e cumulative sle
18740 65 70 20 74 69 6d 65 0a 2a 2a 20 20 20 20 20 20 ep time.**
18750 20 20 20 20 72 65 70 6f 72 74 65 64 20 62 61 63 reported bac
18760 6b 20 62 79 20 78 53 6c 65 65 70 28 29 20 65 78 k by xSleep() ex
18770 63 65 65 64 73 20 4e 20 6d 69 6c 6c 69 73 65 63 ceeds N millisec
18780 6f 6e 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f onds..*/.SQLITE_
18790 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
187a0 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 73 71 6c busy_timeout(sql
187b0 69 74 65 33 2a 2c 20 69 6e 74 20 6d 73 29 3b 0a ite3*, int ms);.
187c0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
187d0 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 52 6f 75 Convenience Rou
187e0 74 69 6e 65 73 20 46 6f 72 20 52 75 6e 6e 69 6e tines For Runnin
187f0 67 20 51 75 65 72 69 65 73 20 7b 48 31 32 33 37 g Queries {H1237
18800 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 0} <S10000>.**.*
18810 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a 20 41 20 * Definition: A
18820 3c 62 3e 72 65 73 75 6c 74 20 74 61 62 6c 65 3c <b>result table<
18830 2f 62 3e 20 69 73 20 6d 65 6d 6f 72 79 20 64 61 /b> is memory da
18840 74 61 20 73 74 72 75 63 74 75 72 65 20 63 72 65 ta structure cre
18850 61 74 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 5b ated by the.** [
18860 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c sqlite3_get_tabl
18870 65 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 20 e()] interface.
18880 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 A result table
18890 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20 63 records the.** c
188a0 6f 6d 70 6c 65 74 65 20 71 75 65 72 79 20 72 65 omplete query re
188b0 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e 65 20 6f sults from one o
188c0 72 20 6d 6f 72 65 20 71 75 65 72 69 65 73 2e 0a r more queries..
188d0 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 **.** The table
188e0 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 68 61 73 conceptually has
188f0 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 a number of row
18900 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73 2e 20 20 s and columns.
18910 42 75 74 0a 2a 2a 20 74 68 65 73 65 20 6e 75 6d But.** these num
18920 62 65 72 73 20 61 72 65 20 6e 6f 74 20 70 61 72 bers are not par
18930 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 t of the result
18940 74 61 62 6c 65 20 69 74 73 65 6c 66 2e 20 20 54 table itself. T
18950 68 65 73 65 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 hese.** numbers
18960 61 72 65 20 6f 62 74 61 69 6e 65 64 20 73 65 70 are obtained sep
18970 61 72 61 74 65 6c 79 2e 20 20 4c 65 74 20 4e 20 arately. Let N
18980 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 be the number of
18990 20 72 6f 77 73 0a 2a 2a 20 61 6e 64 20 4d 20 62 rows.** and M b
189a0 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 e the number of
189b0 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 columns..**.** A
189c0 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 69 73 result table is
189d0 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 an array of poi
189e0 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f 2d 74 65 nters to zero-te
189f0 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 rminated UTF-8 s
18a00 74 72 69 6e 67 73 2e 0a 2a 2a 20 54 68 65 72 65 trings..** There
18a10 20 61 72 65 20 28 4e 2b 31 29 2a 4d 20 65 6c 65 are (N+1)*M ele
18a20 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 72 72 ments in the arr
18a30 61 79 2e 20 20 54 68 65 20 66 69 72 73 74 20 4d ay. The first M
18a40 20 70 6f 69 6e 74 65 72 73 20 70 6f 69 6e 74 0a pointers point.
18a50 2a 2a 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 ** to zero-termi
18a60 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74 68 nated strings th
18a70 61 74 20 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 at contain the
18a80 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63 6f 6c names of the col
18a90 75 6d 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 65 6d umns..** The rem
18aa0 61 69 6e 69 6e 67 20 65 6e 74 72 69 65 73 20 61 aining entries a
18ab0 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 71 75 65 72 ll point to quer
18ac0 79 20 72 65 73 75 6c 74 73 2e 20 20 4e 55 4c 4c y results. NULL
18ad0 20 76 61 6c 75 65 73 20 72 65 73 75 6c 74 0a 2a values result.*
18ae0 2a 20 69 6e 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 * in NULL pointe
18af0 72 73 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 76 rs. All other v
18b00 61 6c 75 65 73 20 61 72 65 20 69 6e 20 74 68 65 alues are in the
18b10 69 72 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 65 ir UTF-8 zero-te
18b20 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72 69 rminated.** stri
18b30 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ng representatio
18b40 6e 20 61 73 20 72 65 74 75 72 6e 65 64 20 62 79 n as returned by
18b50 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
18b60 5f 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 _text()]..**.**
18b70 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 6d A result table m
18b80 69 67 68 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 ight consist of
18b90 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6d 65 6d 6f one or more memo
18ba0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a ry allocations..
18bb0 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 ** It is not saf
18bc0 65 20 74 6f 20 70 61 73 73 20 61 20 72 65 73 75 e to pass a resu
18bd0 6c 74 20 74 61 62 6c 65 20 64 69 72 65 63 74 6c lt table directl
18be0 79 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 y to [sqlite3_fr
18bf0 65 65 28 29 5d 2e 0a 2a 2a 20 41 20 72 65 73 75 ee()]..** A resu
18c00 6c 74 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 lt table should
18c10 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 75 be deallocated u
18c20 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 sing [sqlite3_fr
18c30 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a 2a 2a 0a ee_table()]..**.
18c40 2a 2a 20 41 73 20 61 6e 20 65 78 61 6d 70 6c 65 ** As an example
18c50 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 74 of the result t
18c60 61 62 6c 65 20 66 6f 72 6d 61 74 2c 20 73 75 70 able format, sup
18c70 70 6f 73 65 20 61 20 71 75 65 72 79 20 72 65 73 pose a query res
18c80 75 6c 74 0a 2a 2a 20 69 73 20 61 73 20 66 6f 6c ult.** is as fol
18c90 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f lows:.**.** <blo
18ca0 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a ckquote><pre>.**
18cb0 20 20 20 20 20 20 20 20 4e 61 6d 65 20 20 20 20 Name
18cc0 20 20 20 20 7c 20 41 67 65 0a 2a 2a 20 20 20 20 | Age.**
18cd0 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ------------
18ce0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 -----------.**
18cf0 20 20 20 20 20 20 41 6c 69 63 65 20 20 20 20 20 Alice
18d00 20 20 7c 20 34 33 0a 2a 2a 20 20 20 20 20 20 20 | 43.**
18d10 20 42 6f 62 20 20 20 20 20 20 20 20 20 7c 20 32 Bob | 2
18d20 38 0a 2a 2a 20 20 20 20 20 20 20 20 43 69 6e 64 8.** Cind
18d30 79 20 20 20 20 20 20 20 7c 20 32 31 0a 2a 2a 20 y | 21.**
18d40 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f </pre></blockquo
18d50 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 te>.**.** There
18d60 61 72 65 20 74 77 6f 20 63 6f 6c 75 6d 6e 20 28 are two column (
18d70 4d 3d 3d 32 29 20 61 6e 64 20 74 68 72 65 65 20 M==2) and three
18d80 72 6f 77 73 20 28 4e 3d 3d 33 29 2e 20 20 54 68 rows (N==3). Th
18d90 75 73 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 us the.** result
18da0 20 74 61 62 6c 65 20 68 61 73 20 38 20 65 6e 74 table has 8 ent
18db0 72 69 65 73 2e 20 20 53 75 70 70 6f 73 65 20 74 ries. Suppose t
18dc0 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 he result table
18dd0 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 is stored.** in
18de0 61 6e 20 61 72 72 61 79 20 6e 61 6d 65 73 20 61 an array names a
18df0 7a 52 65 73 75 6c 74 2e 20 20 54 68 65 6e 20 61 zResult. Then a
18e00 7a 52 65 73 75 6c 74 20 68 6f 6c 64 73 20 74 68 zResult holds th
18e10 69 73 20 63 6f 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a is content:.**.*
18e20 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 * <blockquote><p
18e30 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a re>.** az
18e40 52 65 73 75 6c 74 26 23 39 31 3b 30 5d 20 3d 20 Result[0] =
18e50 22 4e 61 6d 65 22 3b 0a 2a 2a 20 20 20 20 20 20 "Name";.**
18e60 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 31 azResult[1
18e70 5d 20 3d 20 22 41 67 65 22 3b 0a 2a 2a 20 20 20 ] = "Age";.**
18e80 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 azResult	
18e90 31 3b 32 5d 20 3d 20 22 41 6c 69 63 65 22 3b 0a 1;2] = "Alice";.
18ea0 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 ** azResu
18eb0 6c 74 26 23 39 31 3b 33 5d 20 3d 20 22 34 33 22 lt[3] = "43"
18ec0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 ;.** azRe
18ed0 73 75 6c 74 26 23 39 31 3b 34 5d 20 3d 20 22 42 sult[4] = "B
18ee0 6f 62 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 ob";.** a
18ef0 7a 52 65 73 75 6c 74 26 23 39 31 3b 35 5d 20 3d zResult[5] =
18f00 20 22 32 38 22 3b 0a 2a 2a 20 20 20 20 20 20 20 "28";.**
18f10 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 36 5d azResult[6]
18f20 20 3d 20 22 43 69 6e 64 79 22 3b 0a 2a 2a 20 20 = "Cindy";.**
18f30 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 azResult&#
18f40 39 31 3b 37 5d 20 3d 20 22 32 31 22 3b 0a 2a 2a 91;7] = "21";.**
18f50 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 </pre></blockqu
18f60 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ote>.**.** The s
18f70 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 qlite3_get_table
18f80 28 29 20 66 75 6e 63 74 69 6f 6e 20 65 76 61 6c () function eval
18f90 75 61 74 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 uates one or mor
18fa0 65 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73 e.** semicolon-s
18fb0 65 70 61 72 61 74 65 64 20 53 51 4c 20 73 74 61 eparated SQL sta
18fc0 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 7a tements in the z
18fd0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 ero-terminated U
18fe0 54 46 2d 38 0a 2a 2a 20 73 74 72 69 6e 67 20 6f TF-8.** string o
18ff0 66 20 69 74 73 20 32 6e 64 20 70 61 72 61 6d 65 f its 2nd parame
19000 74 65 72 2e 20 20 49 74 20 72 65 74 75 72 6e 73 ter. It returns
19010 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 a result table
19020 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 to the.** pointe
19030 72 20 67 69 76 65 6e 20 69 6e 20 69 74 73 20 33 r given in its 3
19040 72 64 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a rd parameter..**
19050 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 63 61 .** After the ca
19060 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 68 lling function h
19070 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e as finished usin
19080 67 20 74 68 65 20 72 65 73 75 6c 74 2c 20 69 74 g the result, it
19090 20 73 68 6f 75 6c 64 0a 2a 2a 20 70 61 73 73 20 should.** pass
190a0 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 the pointer to t
190b0 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 he result table
190c0 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f to sqlite3_free_
190d0 74 61 62 6c 65 28 29 20 69 6e 20 6f 72 64 65 72 table() in order
190e0 20 74 6f 0a 2a 2a 20 72 65 6c 65 61 73 65 20 74 to.** release t
190f0 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 77 he memory that w
19100 61 73 20 6d 61 6c 6c 6f 63 65 64 2e 20 20 42 65 as malloced. Be
19110 63 61 75 73 65 20 6f 66 20 74 68 65 20 77 61 79 cause of the way
19120 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
19130 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61 70 70 65 _malloc()] happe
19140 6e 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 ns within sqlite
19150 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 2c 20 74 3_get_table(), t
19160 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 he calling.** fu
19170 6e 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 nction must not
19180 74 72 79 20 74 6f 20 63 61 6c 6c 20 5b 73 71 6c try to call [sql
19190 69 74 65 33 5f 66 72 65 65 28 29 5d 20 64 69 72 ite3_free()] dir
191a0 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 ectly. Only.**
191b0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 [sqlite3_free_ta
191c0 62 6c 65 28 29 5d 20 69 73 20 61 62 6c 65 20 74 ble()] is able t
191d0 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65 o release the me
191e0 6d 6f 72 79 20 70 72 6f 70 65 72 6c 79 20 61 6e mory properly an
191f0 64 20 73 61 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 d safely..**.**
19200 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f The sqlite3_get_
19210 74 61 62 6c 65 28 29 20 69 6e 74 65 72 66 61 63 table() interfac
19220 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 e is implemented
19230 20 61 73 20 61 20 77 72 61 70 70 65 72 20 61 72 as a wrapper ar
19240 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ound.** [sqlite3
19250 5f 65 78 65 63 28 29 5d 2e 20 20 54 68 65 20 73 _exec()]. The s
19260 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 qlite3_get_table
19270 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 () routine does
19280 6e 6f 74 20 68 61 76 65 20 61 63 63 65 73 73 0a not have access.
19290 2a 2a 20 74 6f 20 61 6e 79 20 69 6e 74 65 72 6e ** to any intern
192a0 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72 al data structur
192b0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 49 es of SQLite. I
192c0 74 20 75 73 65 73 20 6f 6e 6c 79 20 74 68 65 20 t uses only the
192d0 70 75 62 6c 69 63 0a 2a 2a 20 69 6e 74 65 72 66 public.** interf
192e0 61 63 65 20 64 65 66 69 6e 65 64 20 68 65 72 65 ace defined here
192f0 2e 20 20 41 73 20 61 20 63 6f 6e 73 65 71 75 65 . As a conseque
19300 6e 63 65 2c 20 65 72 72 6f 72 73 20 74 68 61 74 nce, errors that
19310 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a 2a 2a occur in the.**
19320 20 77 72 61 70 70 65 72 20 6c 61 79 65 72 20 6f wrapper layer o
19330 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 69 6e utside of the in
19340 74 65 72 6e 61 6c 20 5b 73 71 6c 69 74 65 33 5f ternal [sqlite3_
19350 65 78 65 63 28 29 5d 20 63 61 6c 6c 20 61 72 65 exec()] call are
19360 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c 65 63 74 65 not.** reflecte
19370 64 20 69 6e 20 73 75 62 73 65 71 75 65 6e 74 20 d in subsequent
19380 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 calls to [sqlite
19390 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 6f 72 20 3_errcode()] or
193a0 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 [sqlite3_errmsg(
193b0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 )]..**.** INVARI
193c0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 ANTS:.**.** {H12
193d0 33 37 31 7d 20 49 66 20 61 20 5b 73 71 6c 69 74 371} If a [sqlit
193e0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 e3_get_table()]
193f0 66 61 69 6c 73 20 61 20 6d 65 6d 6f 72 79 20 61 fails a memory a
19400 6c 6c 6f 63 61 74 69 6f 6e 2c 20 74 68 65 6e 0a llocation, then.
19410 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 73 ** it s
19420 68 61 6c 6c 20 66 72 65 65 20 74 68 65 20 72 65 hall free the re
19430 73 75 6c 74 20 74 61 62 6c 65 20 75 6e 64 65 72 sult table under
19440 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2c 20 61 construction, a
19450 62 6f 72 74 20 74 68 65 0a 2a 2a 20 20 20 20 20 bort the.**
19460 20 20 20 20 20 71 75 65 72 79 20 69 6e 20 70 72 query in pr
19470 6f 63 65 73 73 2c 20 73 6b 69 70 20 61 6e 79 20 ocess, skip any
19480 73 75 62 73 65 71 75 65 6e 74 20 71 75 65 72 69 subsequent queri
19490 65 73 2c 20 73 65 74 20 74 68 65 0a 2a 2a 20 20 es, set the.**
194a0 20 20 20 20 20 20 20 20 2a 70 61 7a 52 65 73 75 *pazResu
194b0 6c 74 20 6f 75 74 70 75 74 20 70 6f 69 6e 74 65 lt output pointe
194c0 72 20 74 6f 20 4e 55 4c 4c 20 61 6e 64 20 72 65 r to NULL and re
194d0 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d turn [SQLITE_NOM
194e0 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 EM]..**.** {H123
194f0 37 33 7d 20 49 66 20 74 68 65 20 70 6e 43 6f 6c 73} If the pnCol
19500 75 6d 6e 20 70 61 72 61 6d 65 74 65 72 20 74 6f umn parameter to
19510 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 [sqlite3_get_ta
19520 62 6c 65 28 29 5d 20 69 73 20 6e 6f 74 20 4e 55 ble()] is not NU
19530 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 LL.** t
19540 68 65 6e 20 61 20 73 75 63 63 65 73 73 66 75 6c hen a successful
19550 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b invocation of [
19560 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c sqlite3_get_tabl
19570 65 28 29 5d 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 e()] shall.**
19580 20 20 20 20 20 20 20 77 72 69 74 65 20 74 68 65 write the
19590 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
195a0 6e 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 ns in the.**
195b0 20 20 20 20 20 20 72 65 73 75 6c 74 20 73 65 74 result set
195c0 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 69 6e of the query in
195d0 74 6f 20 2a 70 6e 43 6f 6c 75 6d 6e 2e 0a 2a 2a to *pnColumn..**
195e0 0a 2a 2a 20 7b 48 31 32 33 37 34 7d 20 49 66 20 .** {H12374} If
195f0 74 68 65 20 70 6e 52 6f 77 20 70 61 72 61 6d 65 the pnRow parame
19600 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ter to [sqlite3_
19610 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 69 73 20 get_table()] is
19620 6e 6f 74 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 not NULL.**
19630 20 20 20 20 20 74 68 65 6e 20 61 20 73 75 63 63 then a succ
19640 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f essful invocatio
19650 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 67 65 n of [sqlite3_ge
19660 74 5f 74 61 62 6c 65 28 29 5d 20 73 68 61 6c 6c t_table()] shall
19670 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 72 69 .** wri
19680 74 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f tes the number o
19690 66 20 72 6f 77 73 20 69 6e 20 74 68 65 0a 2a 2a f rows in the.**
196a0 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 result
196b0 20 73 65 74 20 6f 66 20 74 68 65 20 71 75 65 72 set of the quer
196c0 79 20 69 6e 74 6f 20 2a 70 6e 52 6f 77 2e 0a 2a y into *pnRow..*
196d0 2a 0a 2a 2a 20 7b 48 31 32 33 37 36 7d 20 41 20 *.** {H12376} A
196e0 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 successful invoc
196f0 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 ation of [sqlite
19700 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 74 3_get_table()] t
19710 68 61 74 20 63 6f 6d 70 75 74 65 73 0a 2a 2a 20 hat computes.**
19720 20 20 20 20 20 20 20 20 20 4e 20 72 6f 77 73 20 N rows
19730 6f 66 20 72 65 73 75 6c 74 20 77 69 74 68 20 43 of result with C
19740 20 63 6f 6c 75 6d 6e 73 20 70 65 72 20 72 6f 77 columns per row
19750 20 73 68 61 6c 6c 20 6d 61 6b 65 20 2a 70 61 7a shall make *paz
19760 52 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 Result.**
19770 20 20 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61 point to an a
19780 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 rray of pointers
19790 20 74 6f 20 28 4e 2b 31 29 2a 43 20 73 74 72 69 to (N+1)*C stri
197a0 6e 67 73 20 77 68 65 72 65 20 74 68 65 20 66 69 ngs where the fi
197b0 72 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 rst.**
197c0 43 20 73 74 72 69 6e 67 73 20 61 72 65 20 63 6f C strings are co
197d0 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 73 20 6f 62 lumn names as ob
197e0 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 20 tained from.**
197f0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
19800 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20 _column_name()]
19810 61 6e 64 20 74 68 65 20 72 65 73 74 20 61 72 65 and the rest are
19820 20 63 6f 6c 75 6d 6e 20 72 65 73 75 6c 74 20 76 column result v
19830 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 alues.**
19840 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 obtained from
19850 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
19860 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b text()]..**.** {
19870 48 31 32 33 37 39 7d 20 54 68 65 20 76 61 6c 75 H12379} The valu
19880 65 73 20 69 6e 20 74 68 65 20 70 61 7a 52 65 73 es in the pazRes
19890 75 6c 74 20 61 72 72 61 79 20 72 65 74 75 72 6e ult array return
198a0 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 67 ed by [sqlite3_g
198b0 65 74 5f 74 61 62 6c 65 28 29 5d 0a 2a 2a 20 20 et_table()].**
198c0 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 72 65 shall re
198d0 6d 61 69 6e 20 76 61 6c 69 64 20 75 6e 74 69 6c main valid until
198e0 20 63 6c 65 61 72 65 64 20 62 79 20 5b 73 71 6c cleared by [sql
198f0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 ite3_free_table(
19900 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 38 )]..**.** {H1238
19910 32 7d 20 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 2} When an error
19920 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 65 occurs during e
19930 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b 73 71 valuation of [sq
19940 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 lite3_get_table(
19950 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 )].** t
19960 68 65 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c he function shal
19970 6c 20 73 65 74 20 2a 70 61 7a 52 65 73 75 6c 74 l set *pazResult
19980 20 74 6f 20 4e 55 4c 4c 2c 20 77 72 69 74 65 20 to NULL, write
19990 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
199a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 74 .** int
199b0 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 o memory obtaine
199c0 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f d from [sqlite3_
199d0 6d 61 6c 6c 6f 63 28 29 5d 2c 20 6d 61 6b 65 0a malloc()], make.
199e0 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 2a 70 7a ** **pz
199f0 45 72 72 6d 73 67 20 70 6f 69 6e 74 20 74 6f 20 Errmsg point to
19a00 74 68 61 74 20 65 72 72 6f 72 20 6d 65 73 73 61 that error messa
19a10 67 65 2c 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ge, and return a
19a20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 70 70 .** app
19a30 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20 ropriate [error
19a40 63 6f 64 65 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 code]..*/.SQLITE
19a50 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
19a60 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 20 73 71 _get_table(. sq
19a70 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
19a80 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 /* An open d
19a90 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e atabase */. con
19aa0 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 st char *zSql,
19ab0 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 /* SQL to be
19ac0 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 63 evaluated */. c
19ad0 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 har ***pazResult
19ae0 2c 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 , /* Results
19af0 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a of the query */.
19b00 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20 int *pnRow,
19b10 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
19b20 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f 77 73 r of result rows
19b30 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f written here */
19b40 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e . int *pnColumn
19b50 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 , /* Numb
19b60 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c er of result col
19b70 75 6d 6e 73 20 77 72 69 74 74 65 6e 20 68 65 72 umns written her
19b80 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a e */. char **pz
19b90 45 72 72 6d 73 67 20 20 20 20 20 20 20 2f 2a 20 Errmsg /*
19ba0 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74 74 65 Error msg writte
19bb0 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c n here */.);.SQL
19bc0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
19bd0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 ite3_free_table(
19be0 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 29 3b 0a char **result);.
19bf0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
19c00 20 46 6f 72 6d 61 74 74 65 64 20 53 74 72 69 6e Formatted Strin
19c10 67 20 50 72 69 6e 74 69 6e 67 20 46 75 6e 63 74 g Printing Funct
19c20 69 6f 6e 73 20 7b 48 31 37 34 30 30 7d 20 3c 53 ions {H17400} <S
19c30 37 30 30 30 30 3e 3c 53 32 30 30 30 30 3e 0a 2a 70000><S20000>.*
19c40 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
19c50 6e 65 73 20 61 72 65 20 77 6f 72 6b 61 6c 69 6b nes are workalik
19c60 65 73 20 6f 66 20 74 68 65 20 22 70 72 69 6e 74 es of the "print
19c70 66 28 29 22 20 66 61 6d 69 6c 79 20 6f 66 20 66 f()" family of f
19c80 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d unctions.** from
19c90 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 the standard C
19ca0 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 library..**.** T
19cb0 68 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e he sqlite3_mprin
19cc0 74 66 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 tf() and sqlite3
19cd0 5f 76 6d 70 72 69 6e 74 66 28 29 20 72 6f 75 74 _vmprintf() rout
19ce0 69 6e 65 73 20 77 72 69 74 65 20 74 68 65 69 72 ines write their
19cf0 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 6f .** results into
19d00 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
19d10 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
19d20 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 alloc()]..** The
19d30 20 73 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 strings returne
19d40 64 20 62 79 20 74 68 65 73 65 20 74 77 6f 20 72 d by these two r
19d50 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 62 outines should b
19d60 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 62 79 e.** released by
19d70 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
19d80 5d 2e 20 20 42 6f 74 68 20 72 6f 75 74 69 6e 65 ]. Both routine
19d90 73 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 s return a.** NU
19da0 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66 20 5b 73 LL pointer if [s
19db0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
19dc0 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c is unable to al
19dd0 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68 0a 2a 2a locate enough.**
19de0 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 memory to hold
19df0 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74 the resulting st
19e00 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 ring..**.** In s
19e10 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
19e20 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d ) routine is sim
19e30 69 6c 61 72 20 74 6f 20 22 73 6e 70 72 69 6e 74 ilar to "snprint
19e40 66 28 29 22 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 f()" from.** the
19e50 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 standard C libr
19e60 61 72 79 2e 20 20 54 68 65 20 72 65 73 75 6c 74 ary. The result
19e70 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f is written into
19e80 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 73 the.** buffer s
19e90 75 70 70 6c 69 65 64 20 61 73 20 74 68 65 20 73 upplied as the s
19ea0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
19eb0 77 68 6f 73 65 20 73 69 7a 65 20 69 73 20 67 69 whose size is gi
19ec0 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 69 ven by.** the fi
19ed0 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 4e rst parameter. N
19ee0 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64 ote that the ord
19ef0 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 er of the.** fir
19f00 73 74 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72 st two parameter
19f10 73 20 69 73 20 72 65 76 65 72 73 65 64 20 66 72 s is reversed fr
19f20 6f 6d 20 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 om snprintf().
19f30 54 68 69 73 20 69 73 20 61 6e 0a 2a 2a 20 68 69 This is an.** hi
19f40 73 74 6f 72 69 63 61 6c 20 61 63 63 69 64 65 6e storical acciden
19f50 74 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65 t that cannot be
19f60 20 66 69 78 65 64 20 77 69 74 68 6f 75 74 20 62 fixed without b
19f70 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 reaking.** backw
19f80 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 ards compatibili
19f90 74 79 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74 ty. Note also t
19fa0 68 61 74 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 hat sqlite3_snpr
19fb0 69 6e 74 66 28 29 0a 2a 2a 20 72 65 74 75 72 6e intf().** return
19fc0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 s a pointer to i
19fd0 74 73 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 ts buffer instea
19fe0 64 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 d of the number
19ff0 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 of.** characters
1a000 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 actually writte
1a010 6e 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 n into the buffe
1a020 72 2e 20 20 57 65 20 61 64 6d 69 74 20 74 68 61 r. We admit tha
1a030 74 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 t.** the number
1a040 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 77 72 of characters wr
1a050 69 74 74 65 6e 20 77 6f 75 6c 64 20 62 65 20 61 itten would be a
1a060 20 6d 6f 72 65 20 75 73 65 66 75 6c 20 72 65 74 more useful ret
1a070 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 62 75 74 urn.** value but
1a080 20 77 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 we cannot chang
1a090 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 e the implementa
1a0a0 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f tion of sqlite3_
1a0b0 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f snprintf().** no
1a0c0 77 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b 69 w without breaki
1a0d0 6e 67 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 ng compatibility
1a0e0 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 ..**.** As long
1a0f0 61 73 20 74 68 65 20 62 75 66 66 65 72 20 73 69 as the buffer si
1a100 7a 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 ze is greater th
1a110 61 6e 20 7a 65 72 6f 2c 20 73 71 6c 69 74 65 33 an zero, sqlite3
1a120 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 67 _snprintf().** g
1a130 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 uarantees that t
1a140 68 65 20 62 75 66 66 65 72 20 69 73 20 61 6c 77 he buffer is alw
1a150 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 ays zero-termina
1a160 74 65 64 2e 20 20 54 68 65 20 66 69 72 73 74 0a ted. The first.
1a170 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 22 6e 22 ** parameter "n"
1a180 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 73 69 is the total si
1a190 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 ze of the buffer
1a1a0 2c 20 69 6e 63 6c 75 64 69 6e 67 20 73 70 61 63 , including spac
1a1b0 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 7a 65 72 e for.** the zer
1a1c0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 53 o terminator. S
1a1d0 6f 20 74 68 65 20 6c 6f 6e 67 65 73 74 20 73 74 o the longest st
1a1e0 72 69 6e 67 20 74 68 61 74 20 63 61 6e 20 62 65 ring that can be
1a1f0 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 completely.** w
1a200 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 6e ritten will be n
1a210 2d 31 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a -1 characters..*
1a220 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
1a230 6e 65 73 20 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e nes all implemen
1a240 74 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 t some additiona
1a250 6c 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 l formatting.**
1a260 6f 70 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 options that are
1a270 20 75 73 65 66 75 6c 20 66 6f 72 20 63 6f 6e 73 useful for cons
1a280 74 72 75 63 74 69 6e 67 20 53 51 4c 20 73 74 61 tructing SQL sta
1a290 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 tements..** All
1a2a0 6f 66 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 of the usual pri
1a2b0 6e 74 66 28 29 20 66 6f 72 6d 61 74 74 69 6e 67 ntf() formatting
1a2c0 20 6f 70 74 69 6f 6e 73 20 61 70 70 6c 79 2e 20 options apply.
1a2d0 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20 74 68 In addition, th
1a2e0 65 72 65 0a 2a 2a 20 69 73 20 61 72 65 20 22 25 ere.** is are "%
1a2f0 71 22 2c 20 22 25 51 22 2c 20 61 6e 64 20 22 25 q", "%Q", and "%
1a300 7a 22 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a z" options..**.*
1a310 2a 20 54 68 65 20 25 71 20 6f 70 74 69 6f 6e 20 * The %q option
1a320 77 6f 72 6b 73 20 6c 69 6b 65 20 25 73 20 69 6e works like %s in
1a330 20 74 68 61 74 20 69 74 20 73 75 62 73 74 69 74 that it substit
1a340 75 74 65 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d utes a null-term
1a350 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 inated.** string
1a360 20 66 72 6f 6d 20 74 68 65 20 61 72 67 75 6d 65 from the argume
1a370 6e 74 20 6c 69 73 74 2e 20 20 42 75 74 20 25 71 nt list. But %q
1a380 20 61 6c 73 6f 20 64 6f 75 62 6c 65 73 20 65 76 also doubles ev
1a390 65 72 79 20 27 5c 27 27 20 63 68 61 72 61 63 74 ery '\'' charact
1a3a0 65 72 2e 0a 2a 2a 20 25 71 20 69 73 20 64 65 73 er..** %q is des
1a3b0 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 69 6e igned for use in
1a3c0 73 69 64 65 20 61 20 73 74 72 69 6e 67 20 6c 69 side a string li
1a3d0 74 65 72 61 6c 2e 20 20 42 79 20 64 6f 75 62 6c teral. By doubl
1a3e0 69 6e 67 20 65 61 63 68 20 27 5c 27 27 0a 2a 2a ing each '\''.**
1a3f0 20 63 68 61 72 61 63 74 65 72 20 69 74 20 65 73 character it es
1a400 63 61 70 65 73 20 74 68 61 74 20 63 68 61 72 61 capes that chara
1a410 63 74 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73 20 cter and allows
1a420 69 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 it to be inserte
1a430 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 74 d into.** the st
1a440 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 ring..**.** For
1a450 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d 65 20 example, assume
1a460 74 68 65 20 73 74 72 69 6e 67 20 76 61 72 69 61 the string varia
1a470 62 6c 65 20 7a 54 65 78 74 20 63 6f 6e 74 61 69 ble zText contai
1a480 6e 73 20 74 65 78 74 20 61 73 20 66 6f 6c 6c 6f ns text as follo
1a490 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b ws:.**.** <block
1a4a0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 quote><pre>.**
1a4b0 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 22 49 char *zText = "I
1a4c0 74 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21 t's a happy day!
1a4d0 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c ";.** </pre></bl
1a4e0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 ockquote>.**.**
1a4f0 4f 6e 65 20 63 61 6e 20 75 73 65 20 74 68 69 73 One can use this
1a500 20 74 65 78 74 20 69 6e 20 61 6e 20 53 51 4c 20 text in an SQL
1a510 73 74 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c statement as fol
1a520 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f lows:.**.** <blo
1a530 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a ckquote><pre>.**
1a540 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 char *zSQL = s
1a550 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 qlite3_mprintf("
1a560 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c INSERT INTO tabl
1a570 65 20 56 41 4c 55 45 53 28 27 25 71 27 29 22 2c e VALUES('%q')",
1a580 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c zText);.** sql
1a590 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 ite3_exec(db, zS
1a5a0 51 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a QL, 0, 0, 0);.**
1a5b0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
1a5c0 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c SQL);.** </pre><
1a5d0 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
1a5e0 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 25 ** Because the %
1a5f0 71 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 q format string
1a600 69 73 20 75 73 65 64 2c 20 74 68 65 20 27 5c 27 is used, the '\'
1a610 27 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a ' character in z
1a620 54 65 78 74 0a 2a 2a 20 69 73 20 65 73 63 61 70 Text.** is escap
1a630 65 64 20 61 6e 64 20 74 68 65 20 53 51 4c 20 67 ed and the SQL g
1a640 65 6e 65 72 61 74 65 64 20 69 73 20 61 73 20 66 enerated is as f
1a650 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 ollows:.**.** <b
1a660 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a lockquote><pre>.
1a670 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 ** INSERT INTO
1a680 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 table1 VALUES('I
1a690 74 27 27 73 20 61 20 68 61 70 70 79 20 64 61 79 t''s a happy day
1a6a0 21 27 29 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 !').** </pre></b
1a6b0 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
1a6c0 20 54 68 69 73 20 69 73 20 63 6f 72 72 65 63 74 This is correct
1a6d0 2e 20 20 48 61 64 20 77 65 20 75 73 65 64 20 25 . Had we used %
1a6e0 73 20 69 6e 73 74 65 61 64 20 6f 66 20 25 71 2c s instead of %q,
1a6f0 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 53 the generated S
1a700 51 4c 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 QL.** would have
1a710 20 6c 6f 6f 6b 65 64 20 6c 69 6b 65 20 74 68 69 looked like thi
1a720 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 s:.**.** <blockq
1a730 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 uote><pre>.** I
1a740 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 NSERT INTO table
1a750 31 20 56 41 4c 55 45 53 28 27 49 74 27 73 20 61 1 VALUES('It's a
1a760 20 68 61 70 70 79 20 64 61 79 21 27 29 3b 0a 2a happy day!');.*
1a770 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 * </pre></blockq
1a780 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 uote>.**.** This
1a790 20 73 65 63 6f 6e 64 20 65 78 61 6d 70 6c 65 20 second example
1a7a0 69 73 20 61 6e 20 53 51 4c 20 73 79 6e 74 61 78 is an SQL syntax
1a7b0 20 65 72 72 6f 72 2e 20 20 41 73 20 61 20 67 65 error. As a ge
1a7c0 6e 65 72 61 6c 20 72 75 6c 65 20 79 6f 75 20 73 neral rule you s
1a7d0 68 6f 75 6c 64 0a 2a 2a 20 61 6c 77 61 79 73 20 hould.** always
1a7e0 75 73 65 20 25 71 20 69 6e 73 74 65 61 64 20 6f use %q instead o
1a7f0 66 20 25 73 20 77 68 65 6e 20 69 6e 73 65 72 74 f %s when insert
1a800 69 6e 67 20 74 65 78 74 20 69 6e 74 6f 20 61 20 ing text into a
1a810 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 0a string literal..
1a820 2a 2a 0a 2a 2a 20 54 68 65 20 25 51 20 6f 70 74 **.** The %Q opt
1a830 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 ion works like %
1a840 71 20 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f q except it also
1a850 20 61 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f adds single quo
1a860 74 65 73 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 tes around.** th
1a870 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 e outside of the
1a880 20 74 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20 20 total string.
1a890 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 Additionally, if
1a8a0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 the parameter i
1a8b0 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e n the.** argumen
1a8c0 74 20 6c 69 73 74 20 69 73 20 61 20 4e 55 4c 4c t list is a NULL
1a8d0 20 70 6f 69 6e 74 65 72 2c 20 25 51 20 73 75 62 pointer, %Q sub
1a8e0 73 74 69 74 75 74 65 73 20 74 68 65 20 74 65 78 stitutes the tex
1a8f0 74 20 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f 75 t "NULL" (withou
1a900 74 0a 2a 2a 20 73 69 6e 67 6c 65 20 71 75 6f 74 t.** single quot
1a910 65 73 29 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 es) in place of
1a920 74 68 65 20 25 51 20 6f 70 74 69 6f 6e 2e 20 20 the %Q option.
1a930 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c So, for example,
1a940 20 6f 6e 65 20 63 6f 75 6c 64 20 73 61 79 3a 0a one could say:.
1a950 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
1a960 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 e><pre>.** char
1a970 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 *zSQL = sqlite3
1a980 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 52 54 _mprintf("INSERT
1a990 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 INTO table VALU
1a9a0 45 53 28 25 51 29 22 2c 20 7a 54 65 78 74 29 3b ES(%Q)", zText);
1a9b0 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 .** sqlite3_exe
1a9c0 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 c(db, zSQL, 0, 0
1a9d0 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 , 0);.** sqlite
1a9e0 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 3_free(zSQL);.**
1a9f0 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 </pre></blockqu
1aa00 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ote>.**.** The c
1aa10 6f 64 65 20 61 62 6f 76 65 20 77 69 6c 6c 20 72 ode above will r
1aa20 65 6e 64 65 72 20 61 20 63 6f 72 72 65 63 74 20 ender a correct
1aa30 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e SQL statement in
1aa40 20 74 68 65 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 the zSQL.** var
1aa50 69 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 iable even if th
1aa60 65 20 7a 54 65 78 74 20 76 61 72 69 61 62 6c 65 e zText variable
1aa70 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 is a NULL point
1aa80 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 er..**.** The "%
1aa90 7a 22 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 z" formatting op
1aaa0 74 69 6f 6e 20 77 6f 72 6b 73 20 65 78 61 63 74 tion works exact
1aab0 6c 79 20 6c 69 6b 65 20 22 25 73 22 20 77 69 74 ly like "%s" wit
1aac0 68 20 74 68 65 0a 2a 2a 20 61 64 64 69 74 69 6f h the.** additio
1aad0 6e 20 74 68 61 74 20 61 66 74 65 72 20 74 68 65 n that after the
1aae0 20 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e string has been
1aaf0 20 72 65 61 64 20 61 6e 64 20 63 6f 70 69 65 64 read and copied
1ab00 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 72 65 73 into.** the res
1ab10 75 6c 74 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 ult, [sqlite3_fr
1ab20 65 65 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 ee()] is called
1ab30 6f 6e 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 on the input str
1ab40 69 6e 67 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a ing. {END}.**.**
1ab50 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
1ab60 2a 2a 20 7b 48 31 37 34 30 33 7d 20 20 54 68 65 ** {H17403} The
1ab70 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 [sqlite3_mprint
1ab80 66 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 f()] and [sqlite
1ab90 33 5f 76 6d 70 72 69 6e 74 66 28 29 5d 20 69 6e 3_vmprintf()] in
1aba0 74 65 72 66 61 63 65 73 0a 2a 2a 20 20 20 20 20 terfaces.**
1abb0 20 20 20 20 20 20 72 65 74 75 72 6e 20 65 69 74 return eit
1abc0 68 65 72 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 her pointers to
1abd0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 zero-terminated
1abe0 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 68 65 UTF-8 strings he
1abf0 6c 64 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ld in.**
1ac00 20 20 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e memory obtain
1ac10 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
1ac20 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 4e 55 _malloc()] or NU
1ac30 4c 4c 20 70 6f 69 6e 74 65 72 73 20 69 66 0a 2a LL pointers if.*
1ac40 2a 20 20 20 20 20 20 20 20 20 20 20 61 20 63 61 * a ca
1ac50 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d ll to [sqlite3_m
1ac60 61 6c 6c 6f 63 28 29 5d 20 66 61 69 6c 73 2e 0a alloc()] fails..
1ac70 2a 2a 0a 2a 2a 20 7b 48 31 37 34 30 36 7d 20 20 **.** {H17406}
1ac80 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 The [sqlite3_snp
1ac90 72 69 6e 74 66 28 29 5d 20 69 6e 74 65 72 66 61 rintf()] interfa
1aca0 63 65 20 77 72 69 74 65 73 20 61 20 7a 65 72 6f ce writes a zero
1acb0 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 -terminated.**
1acc0 20 20 20 20 20 20 20 20 20 55 54 46 2d 38 20 73 UTF-8 s
1acd0 74 72 69 6e 67 20 69 6e 74 6f 20 74 68 65 20 62 tring into the b
1ace0 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f uffer pointed to
1acf0 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 70 by the second p
1ad00 61 72 61 6d 65 74 65 72 0a 2a 2a 20 20 20 20 20 arameter.**
1ad10 20 20 20 20 20 20 70 72 6f 76 69 64 65 64 20 74 provided t
1ad20 68 61 74 20 74 68 65 20 66 69 72 73 74 20 70 61 hat the first pa
1ad30 72 61 6d 65 74 65 72 20 69 73 20 67 72 65 61 74 rameter is great
1ad40 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e 0a 2a 2a er than zero..**
1ad50 0a 2a 2a 20 7b 48 31 37 34 30 37 7d 20 20 54 68 .** {H17407} Th
1ad60 65 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 e [sqlite3_snpri
1ad70 6e 74 66 28 29 5d 20 69 6e 74 65 72 66 61 63 65 ntf()] interface
1ad80 20 64 6f 65 73 20 6e 6f 74 20 77 72 69 74 65 20 does not write
1ad90 73 6c 6f 74 73 20 6f 66 0a 2a 2a 20 20 20 20 20 slots of.**
1ada0 20 20 20 20 20 20 69 74 73 20 6f 75 74 70 75 74 its output
1adb0 20 62 75 66 66 65 72 20 28 74 68 65 20 73 65 63 buffer (the sec
1adc0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 29 20 6f ond parameter) o
1add0 75 74 73 69 64 65 20 74 68 65 20 72 61 6e 67 65 utside the range
1ade0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 66 .** of
1adf0 20 30 20 74 68 72 6f 75 67 68 20 4e 2d 31 20 28 0 through N-1 (
1ae00 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 66 where N is the f
1ae10 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a irst parameter).
1ae20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 72 65 67 ** reg
1ae30 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6c ardless of the l
1ae40 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 ength of the str
1ae50 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ing.**
1ae60 20 72 65 71 75 65 73 74 65 64 20 62 79 20 74 68 requested by th
1ae70 65 20 66 6f 72 6d 61 74 20 73 70 65 63 69 66 69 e format specifi
1ae80 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 cation..*/.SQLIT
1ae90 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 E_API char *sqli
1aea0 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 te3_mprintf(cons
1aeb0 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 t char*,...);.SQ
1aec0 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 LITE_API char *s
1aed0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 qlite3_vmprintf(
1aee0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f const char*, va_
1aef0 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 list);.SQLITE_AP
1af00 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f I char *sqlite3_
1af10 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c 63 68 61 snprintf(int,cha
1af20 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 r*,const char*,
1af30 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ...);../*.** CAP
1af40 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c I3REF: Memory Al
1af50 6c 6f 63 61 74 69 6f 6e 20 53 75 62 73 79 73 74 location Subsyst
1af60 65 6d 20 7b 48 31 37 33 30 30 7d 20 3c 53 32 30 em {H17300} <S20
1af70 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 000>.**.** The S
1af80 51 4c 69 74 65 20 63 6f 72 65 20 20 75 73 65 73 QLite core uses
1af90 20 74 68 65 73 65 20 74 68 72 65 65 20 72 6f 75 these three rou
1afa0 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6f 66 tines for all of
1afb0 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 69 6e 74 65 its own.** inte
1afc0 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f rnal memory allo
1afd0 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 22 43 cation needs. "C
1afe0 6f 72 65 22 20 69 6e 20 74 68 65 20 70 72 65 76 ore" in the prev
1aff0 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 0a 2a 2a ious sentence.**
1b000 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 does not includ
1b010 65 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74 e operating-syst
1b020 65 6d 20 73 70 65 63 69 66 69 63 20 56 46 53 20 em specific VFS
1b030 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 implementation.
1b040 20 54 68 65 0a 2a 2a 20 57 69 6e 64 6f 77 73 20 The.** Windows
1b050 56 46 53 20 75 73 65 73 20 6e 61 74 69 76 65 20 VFS uses native
1b060 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 malloc() and fre
1b070 65 28 29 20 66 6f 72 20 73 6f 6d 65 20 6f 70 65 e() for some ope
1b080 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 rations..**.** T
1b090 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f he sqlite3_mallo
1b0a0 63 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 c() routine retu
1b0b0 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
1b0c0 20 61 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 6d a block.** of m
1b0d0 65 6d 6f 72 79 20 61 74 20 6c 65 61 73 74 20 4e emory at least N
1b0e0 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 bytes in length
1b0f0 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 , where N is the
1b100 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 49 parameter..** I
1b110 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 f sqlite3_malloc
1b120 28 29 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 () is unable to
1b130 6f 62 74 61 69 6e 20 73 75 66 66 69 63 69 65 6e obtain sufficien
1b140 74 20 66 72 65 65 0a 2a 2a 20 6d 65 6d 6f 72 79 t free.** memory
1b150 2c 20 69 74 20 72 65 74 75 72 6e 73 20 61 20 4e , it returns a N
1b160 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 49 66 ULL pointer. If
1b170 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 4e the parameter N
1b180 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d to.** sqlite3_m
1b190 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f 20 alloc() is zero
1b1a0 6f 72 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e or negative then
1b1b0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
1b1c0 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 20 4e ) returns.** a N
1b1d0 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a ULL pointer..**.
1b1e0 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74 ** Calling sqlit
1b1f0 65 33 5f 66 72 65 65 28 29 20 77 69 74 68 20 61 e3_free() with a
1b200 20 70 6f 69 6e 74 65 72 20 70 72 65 76 69 6f 75 pointer previou
1b210 73 6c 79 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 sly returned.**
1b220 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f by sqlite3_mallo
1b230 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 c() or sqlite3_r
1b240 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65 61 73 65 ealloc() release
1b250 73 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 73 6f s that memory so
1b260 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d 69 67 68 .** that it migh
1b270 74 20 62 65 20 72 65 75 73 65 64 2e 20 20 54 68 t be reused. Th
1b280 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 e sqlite3_free()
1b290 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 61 routine is.** a
1b2a0 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 63 61 6c no-op if is cal
1b2b0 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 led with a NULL
1b2c0 70 6f 69 6e 74 65 72 2e 20 20 50 61 73 73 69 6e pointer. Passin
1b2d0 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 g a NULL pointer
1b2e0 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 .** to sqlite3_f
1b2f0 72 65 65 28 29 20 69 73 20 68 61 72 6d 6c 65 73 ree() is harmles
1b300 73 2e 20 20 41 66 74 65 72 20 62 65 69 6e 67 20 s. After being
1b310 66 72 65 65 64 2c 20 6d 65 6d 6f 72 79 0a 2a 2a freed, memory.**
1b320 20 73 68 6f 75 6c 64 20 6e 65 69 74 68 65 72 20 should neither
1b330 62 65 20 72 65 61 64 20 6e 6f 72 20 77 72 69 74 be read nor writ
1b340 74 65 6e 2e 20 20 45 76 65 6e 20 72 65 61 64 69 ten. Even readi
1b350 6e 67 20 70 72 65 76 69 6f 75 73 6c 79 20 66 72 ng previously fr
1b360 65 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6d 69 eed.** memory mi
1b370 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 61 20 ght result in a
1b380 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 segmentation fau
1b390 6c 74 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 lt or other seve
1b3a0 72 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 4d 65 6d re error..** Mem
1b3b0 6f 72 79 20 63 6f 72 72 75 70 74 69 6f 6e 2c 20 ory corruption,
1b3c0 61 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66 a segmentation f
1b3d0 61 75 6c 74 2c 20 6f 72 20 6f 74 68 65 72 20 73 ault, or other s
1b3e0 65 76 65 72 65 20 65 72 72 6f 72 0a 2a 2a 20 6d evere error.** m
1b3f0 69 67 68 74 20 72 65 73 75 6c 74 20 69 66 20 73 ight result if s
1b400 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 73 qlite3_free() is
1b410 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 6e called with a n
1b420 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 on-NULL pointer
1b430 74 68 61 74 0a 2a 2a 20 77 61 73 20 6e 6f 74 20 that.** was not
1b440 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
1b450 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f lite3_malloc() o
1b460 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f r sqlite3_reallo
1b470 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 c()..**.** The s
1b480 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
1b490 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d interface attem
1b4a0 70 74 73 20 74 6f 20 72 65 73 69 7a 65 20 61 0a pts to resize a.
1b4b0 2a 2a 20 70 72 69 6f 72 20 6d 65 6d 6f 72 79 20 ** prior memory
1b4c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 allocation to be
1b4d0 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 at least N byte
1b4e0 73 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 s, where N is th
1b4f0 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 e.** second para
1b500 6d 65 74 65 72 2e 20 20 54 68 65 20 6d 65 6d 6f meter. The memo
1b510 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f ry allocation to
1b520 20 62 65 20 72 65 73 69 7a 65 64 20 69 73 20 74 be resized is t
1b530 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 he first.** para
1b540 6d 65 74 65 72 2e 20 20 49 66 20 74 68 65 20 66 meter. If the f
1b550 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 irst parameter t
1b560 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f o sqlite3_reallo
1b570 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e 55 4c 4c c().** is a NULL
1b580 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 69 74 pointer then it
1b590 73 20 62 65 68 61 76 69 6f 72 20 69 73 20 69 64 s behavior is id
1b5a0 65 6e 74 69 63 61 6c 20 74 6f 20 63 61 6c 6c 69 entical to calli
1b5b0 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 ng.** sqlite3_ma
1b5c0 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65 20 4e 20 lloc(N) where N
1b5d0 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 is the second pa
1b5e0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
1b5f0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a e3_realloc()..**
1b600 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 If the second p
1b610 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
1b620 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 73 te3_realloc() is
1b630 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61 zero or.** nega
1b640 74 69 76 65 20 74 68 65 6e 20 74 68 65 20 62 65 tive then the be
1b650 68 61 76 69 6f 72 20 69 73 20 65 78 61 63 74 6c havior is exactl
1b660 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 63 61 y the same as ca
1b670 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 lling.** sqlite3
1b680 5f 66 72 65 65 28 50 29 20 77 68 65 72 65 20 50 _free(P) where P
1b690 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70 61 is the first pa
1b6a0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
1b6b0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a e3_realloc()..**
1b6c0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 sqlite3_realloc
1b6d0 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 () returns a poi
1b6e0 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f 72 79 nter to a memory
1b6f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f allocation.** o
1b700 66 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 f at least N byt
1b710 65 73 20 69 6e 20 73 69 7a 65 20 6f 72 20 4e 55 es in size or NU
1b720 4c 4c 20 69 66 20 73 75 66 66 69 63 69 65 6e 74 LL if sufficient
1b730 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e 61 76 61 memory is unava
1b740 69 6c 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 4d 20 ilable..** If M
1b750 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 is the size of t
1b760 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 he prior allocat
1b770 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e 28 4e 2c ion, then min(N,
1b780 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 74 M) bytes.** of t
1b790 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 he prior allocat
1b7a0 69 6f 6e 20 61 72 65 20 63 6f 70 69 65 64 20 69 ion are copied i
1b7b0 6e 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e nto the beginnin
1b7c0 67 20 6f 66 20 62 75 66 66 65 72 20 72 65 74 75 g of buffer retu
1b7d0 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 rned.** by sqlit
1b7e0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 61 6e 64 e3_realloc() and
1b7f0 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 the prior alloc
1b800 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a ation is freed..
1b810 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 72 65 ** If sqlite3_re
1b820 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 20 alloc() returns
1b830 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 70 NULL, then the p
1b840 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a rior allocation.
1b850 2a 2a 20 69 73 20 6e 6f 74 20 66 72 65 65 64 2e ** is not freed.
1b860 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 .**.** The memor
1b870 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 y returned by sq
1b880 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 lite3_malloc() a
1b890 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c nd sqlite3_reall
1b8a0 6f 63 28 29 0a 2a 2a 20 69 73 20 61 6c 77 61 79 oc().** is alway
1b8b0 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 74 20 s aligned to at
1b8c0 6c 65 61 73 74 20 61 6e 20 38 20 62 79 74 65 20 least an 8 byte
1b8d0 62 6f 75 6e 64 61 72 79 2e 20 7b 45 4e 44 7d 0a boundary. {END}.
1b8e0 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c **.** The defaul
1b8f0 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e t implementation
1b900 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
1b910 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
1b920 74 65 6d 20 75 73 65 73 0a 2a 2a 20 74 68 65 20 tem uses.** the
1b930 6d 61 6c 6c 6f 63 28 29 2c 20 72 65 61 6c 6c 6f malloc(), reallo
1b940 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 70 c() and free() p
1b950 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 73 rovided by the s
1b960 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 tandard C librar
1b970 79 2e 0a 2a 2a 20 7b 48 31 37 33 38 32 7d 20 48 y..** {H17382} H
1b980 6f 77 65 76 65 72 2c 20 69 66 20 53 51 4c 69 74 owever, if SQLit
1b990 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 e is compiled wi
1b9a0 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 th the.** SQLITE
1b9b0 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3d 3c 69 3e _MEMORY_SIZE=<i>
1b9c0 4e 4e 4e 3c 2f 69 3e 20 43 20 70 72 65 70 72 6f NNN</i> C prepro
1b9d0 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 28 77 68 cessor macro (wh
1b9e0 65 72 65 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a ere <i>NNN</i>.*
1b9f0 2a 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 29 * is an integer)
1ba00 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 63 72 , then SQLite cr
1ba10 65 61 74 65 20 61 20 73 74 61 74 69 63 20 61 72 eate a static ar
1ba20 72 61 79 20 6f 66 20 61 74 20 6c 65 61 73 74 0a ray of at least.
1ba30 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 ** <i>NNN</i> by
1ba40 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 20 tes in size and
1ba50 75 73 65 73 20 74 68 61 74 20 61 72 72 61 79 20 uses that array
1ba60 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 for all of its d
1ba70 79 6e 61 6d 69 63 0a 2a 2a 20 6d 65 6d 6f 72 79 ynamic.** memory
1ba80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 allocation need
1ba90 73 2e 20 7b 45 4e 44 7d 20 20 41 64 64 69 74 69 s. {END} Additi
1baa0 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f onal memory allo
1bab0 63 61 74 6f 72 20 6f 70 74 69 6f 6e 73 0a 2a 2a cator options.**
1bac0 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e may be added in
1bad0 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
1bae0 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 53 51 4c 69 74 ..**.** In SQLit
1baf0 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 20 e version 3.5.0
1bb00 61 6e 64 20 33 2e 35 2e 31 2c 20 69 74 20 77 61 and 3.5.1, it wa
1bb10 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 s possible to de
1bb20 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c 49 fine.** the SQLI
1bb30 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f 41 TE_OMIT_MEMORY_A
1bb40 4c 4c 4f 43 41 54 49 4f 4e 20 77 68 69 63 68 20 LLOCATION which
1bb50 77 6f 75 6c 64 20 63 61 75 73 65 20 74 68 65 20 would cause the
1bb60 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 69 6d 70 6c built-in.** impl
1bb70 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
1bb80 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 ese routines to
1bb90 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 61 be omitted. Tha
1bba0 74 20 63 61 70 61 62 69 6c 69 74 79 0a 2a 2a 20 t capability.**
1bbb0 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72 6f is no longer pro
1bbc0 76 69 64 65 64 2e 20 20 4f 6e 6c 79 20 62 75 69 vided. Only bui
1bbd0 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c lt-in memory all
1bbe0 6f 63 61 74 6f 72 73 20 63 61 6e 20 62 65 20 75 ocators can be u
1bbf0 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 57 sed..**.** The W
1bc00 69 6e 64 6f 77 73 20 4f 53 20 69 6e 74 65 72 66 indows OS interf
1bc10 61 63 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 0a ace layer calls.
1bc20 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20 6d 61 ** the system ma
1bc30 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 lloc() and free(
1bc40 29 20 64 69 72 65 63 74 6c 79 20 77 68 65 6e 20 ) directly when
1bc50 63 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a 20 66 69 converting.** fi
1bc60 6c 65 6e 61 6d 65 73 20 62 65 74 77 65 65 6e 20 lenames between
1bc70 74 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 the UTF-8 encodi
1bc80 6e 67 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 ng used by SQLit
1bc90 65 0a 2a 2a 20 61 6e 64 20 77 68 61 74 65 76 65 e.** and whateve
1bca0 72 20 66 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 r filename encod
1bcb0 69 6e 67 20 69 73 20 75 73 65 64 20 62 79 20 74 ing is used by t
1bcc0 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 57 69 he particular Wi
1bcd0 6e 64 6f 77 73 0a 2a 2a 20 69 6e 73 74 61 6c 6c ndows.** install
1bce0 61 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72 79 20 61 ation. Memory a
1bcf0 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 73 llocation errors
1bd00 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20 62 are detected, b
1bd10 75 74 0a 2a 2a 20 74 68 65 79 20 61 72 65 20 72 ut.** they are r
1bd20 65 70 6f 72 74 65 64 20 62 61 63 6b 20 61 73 20 eported back as
1bd30 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e [SQLITE_CANTOPEN
1bd40 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f ] or.** [SQLITE_
1bd50 49 4f 45 52 52 5d 20 72 61 74 68 65 72 20 74 68 IOERR] rather th
1bd60 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d an [SQLITE_NOMEM
1bd70 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 ]..**.** INVARIA
1bd80 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 NTS:.**.** {H173
1bd90 30 33 7d 20 20 54 68 65 20 5b 73 71 6c 69 74 65 03} The [sqlite
1bda0 33 5f 6d 61 6c 6c 6f 63 28 4e 29 5d 20 69 6e 74 3_malloc(N)] int
1bdb0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 erface returns e
1bdc0 69 74 68 65 72 20 61 20 70 6f 69 6e 74 65 72 20 ither a pointer
1bdd0 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 to.**
1bde0 61 20 6e 65 77 6c 79 20 63 68 65 63 6b 65 64 2d a newly checked-
1bdf0 6f 75 74 20 62 6c 6f 63 6b 20 6f 66 20 61 74 20 out block of at
1be00 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 6f 66 least N bytes of
1be10 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 memory.**
1be20 20 20 20 20 20 74 68 61 74 20 69 73 20 38 2d 62 that is 8-b
1be30 79 74 65 20 61 6c 69 67 6e 65 64 2c 20 6f 72 20 yte aligned, or
1be40 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 it returns NULL
1be50 69 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 0a if it is unable.
1be60 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 6f 20 ** to
1be70 66 75 6c 66 69 6c 6c 20 74 68 65 20 72 65 71 75 fulfill the requ
1be80 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 est..**.** {H173
1be90 30 34 7d 20 20 54 68 65 20 5b 73 71 6c 69 74 65 04} The [sqlite
1bea0 33 5f 6d 61 6c 6c 6f 63 28 4e 29 5d 20 69 6e 74 3_malloc(N)] int
1beb0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
1bec0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66 NULL pointer if
1bed0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 4e 20 .** N
1bee0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 is less than or
1bef0 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 0a 2a equal to zero..*
1bf00 2a 0a 2a 2a 20 7b 48 31 37 33 30 35 7d 20 20 54 *.** {H17305} T
1bf10 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 he [sqlite3_free
1bf20 28 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 (P)] interface r
1bf30 65 6c 65 61 73 65 73 20 6d 65 6d 6f 72 79 20 70 eleases memory p
1bf40 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 20 20 20 reviously.**
1bf50 20 20 20 20 20 20 20 72 65 74 75 72 6e 65 64 20 returned
1bf60 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 from [sqlite3_ma
1bf70 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 lloc()] or [sqli
1bf80 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 2c 0a te3_realloc()],.
1bf90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6d 61 6b ** mak
1bfa0 69 6e 67 20 69 74 20 61 76 61 69 6c 61 62 6c 65 ing it available
1bfb0 20 66 6f 72 20 72 65 75 73 65 2e 0a 2a 2a 0a 2a for reuse..**.*
1bfc0 2a 20 7b 48 31 37 33 30 36 7d 20 20 41 20 63 61 * {H17306} A ca
1bfd0 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 ll to [sqlite3_f
1bfe0 72 65 65 28 4e 55 4c 4c 29 5d 20 69 73 20 61 20 ree(NULL)] is a
1bff0 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a harmless no-op..
1c000 2a 2a 0a 2a 2a 20 7b 48 31 37 33 31 30 7d 20 20 **.** {H17310}
1c010 41 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 A call to [sqlit
1c020 65 33 5f 72 65 61 6c 6c 6f 63 28 30 2c 4e 29 5d e3_realloc(0,N)]
1c030 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 is equivalent t
1c040 6f 20 61 20 63 61 6c 6c 0a 2a 2a 20 20 20 20 20 o a call.**
1c050 20 20 20 20 20 20 74 6f 20 5b 73 71 6c 69 74 65 to [sqlite
1c060 33 5f 6d 61 6c 6c 6f 63 28 4e 29 5d 2e 0a 2a 2a 3_malloc(N)]..**
1c070 0a 2a 2a 20 7b 48 31 37 33 31 32 7d 20 20 41 20 .** {H17312} A
1c080 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
1c090 5f 72 65 61 6c 6c 6f 63 28 50 2c 30 29 5d 20 69 _realloc(P,0)] i
1c0a0 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 s equivalent to
1c0b0 61 20 63 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 a call.**
1c0c0 20 20 20 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f to [sqlite3_
1c0d0 66 72 65 65 28 50 29 5d 2e 0a 2a 2a 0a 2a 2a 20 free(P)]..**.**
1c0e0 7b 48 31 37 33 31 35 7d 20 20 54 68 65 20 53 51 {H17315} The SQ
1c0f0 4c 69 74 65 20 63 6f 72 65 20 75 73 65 73 20 5b Lite core uses [
1c100 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
1c110 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c ], [sqlite3_real
1c120 6c 6f 63 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 loc()],.**
1c130 20 20 20 20 20 61 6e 64 20 5b 73 71 6c 69 74 65 and [sqlite
1c140 33 5f 66 72 65 65 28 29 5d 20 66 6f 72 20 61 6c 3_free()] for al
1c150 6c 20 6f 66 20 69 74 73 20 6d 65 6d 6f 72 79 20 l of its memory
1c160 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 0a 2a allocation and.*
1c170 2a 20 20 20 20 20 20 20 20 20 20 20 64 65 61 6c * deal
1c180 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 0a location needs..
1c190 2a 2a 0a 2a 2a 20 7b 48 31 37 33 31 38 7d 20 20 **.** {H17318}
1c1a0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 The [sqlite3_rea
1c1b0 6c 6c 6f 63 28 50 2c 4e 29 5d 20 69 6e 74 65 72 lloc(P,N)] inter
1c1c0 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 face returns eit
1c1d0 68 65 72 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a her a pointer.**
1c1e0 20 20 20 20 20 20 20 20 20 20 20 74 6f 20 61 20 to a
1c1f0 62 6c 6f 63 6b 20 6f 66 20 63 68 65 63 6b 65 64 block of checked
1c200 2d 6f 75 74 20 6d 65 6d 6f 72 79 20 6f 66 20 61 -out memory of a
1c210 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 t least N bytes
1c220 69 6e 20 73 69 7a 65 0a 2a 2a 20 20 20 20 20 20 in size.**
1c230 20 20 20 20 20 74 68 61 74 20 69 73 20 38 2d 62 that is 8-b
1c240 79 74 65 20 61 6c 69 67 6e 65 64 2c 20 6f 72 20 yte aligned, or
1c250 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
1c260 2a 2a 0a 2a 2a 20 7b 48 31 37 33 32 31 7d 20 20 **.** {H17321}
1c270 57 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65 When [sqlite3_re
1c280 61 6c 6c 6f 63 28 50 2c 4e 29 5d 20 72 65 74 75 alloc(P,N)] retu
1c290 72 6e 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 rns a non-NULL p
1c2a0 6f 69 6e 74 65 72 2c 20 69 74 20 66 69 72 73 74 ointer, it first
1c2b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63 6f .** co
1c2c0 70 69 65 73 20 74 68 65 20 66 69 72 73 74 20 4b pies the first K
1c2d0 20 62 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e bytes of conten
1c2e0 74 20 66 72 6f 6d 20 50 20 69 6e 74 6f 20 74 68 t from P into th
1c2f0 65 20 6e 65 77 6c 79 0a 2a 2a 20 20 20 20 20 20 e newly.**
1c300 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 64 20 62 allocated b
1c310 6c 6f 63 6b 2c 20 77 68 65 72 65 20 4b 20 69 73 lock, where K is
1c320 20 74 68 65 20 6c 65 73 73 65 72 20 6f 66 20 4e the lesser of N
1c330 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 and the size of
1c340 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 .** th
1c350 65 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a e buffer P..**.*
1c360 2a 20 7b 48 31 37 33 32 32 7d 20 20 57 68 65 6e * {H17322} When
1c370 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f [sqlite3_reallo
1c380 63 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73 20 c(P,N)] returns
1c390 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 a non-NULL point
1c3a0 65 72 2c 20 69 74 20 66 69 72 73 74 0a 2a 2a 20 er, it first.**
1c3b0 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 releas
1c3c0 65 73 20 74 68 65 20 62 75 66 66 65 72 20 50 2e es the buffer P.
1c3d0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 32 33 7d 20 .**.** {H17323}
1c3e0 20 57 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 72 When [sqlite3_r
1c3f0 65 61 6c 6c 6f 63 28 50 2c 4e 29 5d 20 72 65 74 ealloc(P,N)] ret
1c400 75 72 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 20 62 urns NULL, the b
1c410 75 66 66 65 72 20 50 20 69 73 0a 2a 2a 20 20 20 uffer P is.**
1c420 20 20 20 20 20 20 20 20 6e 6f 74 20 6d 6f 64 69 not modi
1c430 66 69 65 64 20 6f 72 20 72 65 6c 65 61 73 65 64 fied or released
1c440 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 ..**.** ASSUMPTI
1c450 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 37 33 ONS:.**.** {A173
1c460 35 30 7d 20 20 54 68 65 20 70 6f 69 6e 74 65 72 50} The pointer
1c470 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 5b 73 arguments to [s
1c480 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 61 qlite3_free()] a
1c490 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c nd [sqlite3_real
1c4a0 6c 6f 63 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 loc()].**
1c4b0 20 20 20 20 6d 75 73 74 20 62 65 20 65 69 74 68 must be eith
1c4c0 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73 65 20 er NULL or else
1c4d0 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 69 6e 65 pointers obtaine
1c4e0 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a 2a d from a prior.*
1c4f0 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 76 6f * invo
1c500 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 cation of [sqlit
1c510 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 e3_malloc()] or
1c520 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 [sqlite3_realloc
1c530 28 29 5d 20 74 68 61 74 20 68 61 76 65 0a 2a 2a ()] that have.**
1c540 20 20 20 20 20 20 20 20 20 20 20 6e 6f 74 20 79 not y
1c550 65 74 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 et been released
1c560 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 37 33 35 31 7d ..**.** {A17351}
1c570 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f The applicatio
1c580 6e 20 6d 75 73 74 20 6e 6f 74 20 72 65 61 64 20 n must not read
1c590 6f 72 20 77 72 69 74 65 20 61 6e 79 20 70 61 72 or write any par
1c5a0 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 t of.**
1c5b0 20 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d a block of mem
1c5c0 6f 72 79 20 61 66 74 65 72 20 69 74 20 68 61 73 ory after it has
1c5d0 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75 been released u
1c5e0 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 sing.**
1c5f0 20 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 [sqlite3_free(
1c600 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 )] or [sqlite3_r
1c610 65 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 ealloc()]..*/.SQ
1c620 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
1c630 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e qlite3_malloc(in
1c640 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 t);.SQLITE_API v
1c650 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 oid *sqlite3_rea
1c660 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 lloc(void*, int)
1c670 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
1c680 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 d sqlite3_free(v
1c690 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 oid*);../*.** CA
1c6a0 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 PI3REF: Memory A
1c6b0 6c 6c 6f 63 61 74 6f 72 20 53 74 61 74 69 73 74 llocator Statist
1c6c0 69 63 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 33 ics {H17370} <S3
1c6d0 30 32 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 0210>.**.** SQLi
1c6e0 74 65 20 70 72 6f 76 69 64 65 73 20 74 68 65 73 te provides thes
1c6f0 65 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73 e two interfaces
1c700 20 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f for reporting o
1c710 6e 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20 n the status.**
1c720 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
1c730 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 malloc()], [sqli
1c740 74 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 te3_free()], and
1c750 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f [sqlite3_reallo
1c760 63 28 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 c()].** routines
1c770 2c 20 77 68 69 63 68 20 66 6f 72 6d 20 74 68 65 , which form the
1c780 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 built-in memory
1c790 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
1c7a0 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 ystem..**.** INV
1c7b0 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
1c7c0 48 31 37 33 37 31 7d 20 54 68 65 20 5b 73 71 6c H17371} The [sql
1c7d0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 ite3_memory_used
1c7e0 28 29 5d 20 72 6f 75 74 69 6e 65 20 72 65 74 75 ()] routine retu
1c7f0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
1c800 66 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20 20 f bytes.**
1c810 20 20 20 20 6f 66 20 6d 65 6d 6f 72 79 20 63 75 of memory cu
1c820 72 72 65 6e 74 6c 79 20 6f 75 74 73 74 61 6e 64 rrently outstand
1c830 69 6e 67 20 28 6d 61 6c 6c 6f 63 65 64 20 62 75 ing (malloced bu
1c840 74 20 6e 6f 74 20 66 72 65 65 64 29 2e 0a 2a 2a t not freed)..**
1c850 0a 2a 2a 20 7b 48 31 37 33 37 33 7d 20 54 68 65 .** {H17373} The
1c860 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 [sqlite3_memory
1c870 5f 68 69 67 68 77 61 74 65 72 28 29 5d 20 72 6f _highwater()] ro
1c880 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 utine returns th
1c890 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 20 20 20 e maximum.**
1c8a0 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20 5b value of [
1c8b0 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 sqlite3_memory_u
1c8c0 73 65 64 28 29 5d 20 73 69 6e 63 65 20 74 68 65 sed()] since the
1c8d0 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b high-water mark
1c8e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 61 73 .** was
1c8f0 20 6c 61 73 74 20 72 65 73 65 74 2e 0a 2a 2a 0a last reset..**.
1c900 2a 2a 20 7b 48 31 37 33 37 34 7d 20 54 68 65 20 ** {H17374} The
1c910 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 values returned
1c920 62 79 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f by [sqlite3_memo
1c930 72 79 5f 75 73 65 64 28 29 5d 20 61 6e 64 0a 2a ry_used()] and.*
1c940 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
1c950 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 te3_memory_highw
1c960 61 74 65 72 28 29 5d 20 69 6e 63 6c 75 64 65 20 ater()] include
1c970 61 6e 79 20 6f 76 65 72 68 65 61 64 0a 2a 2a 20 any overhead.**
1c980 20 20 20 20 20 20 20 20 20 61 64 64 65 64 20 62 added b
1c990 79 20 53 51 4c 69 74 65 20 69 6e 20 69 74 73 20 y SQLite in its
1c9a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1c9b0 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f f [sqlite3_mallo
1c9c0 63 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 c()],.**
1c9d0 20 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 68 65 but not overhe
1c9e0 61 64 20 61 64 64 65 64 20 62 79 20 74 68 65 20 ad added by the
1c9f0 61 6e 79 20 75 6e 64 65 72 6c 79 69 6e 67 20 73 any underlying s
1ca00 79 73 74 65 6d 20 6c 69 62 72 61 72 79 0a 2a 2a ystem library.**
1ca10 20 20 20 20 20 20 20 20 20 20 72 6f 75 74 69 6e routin
1ca20 65 73 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 es that [sqlite3
1ca30 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6d 61 79 20 63 _malloc()] may c
1ca40 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 all..**.** {H173
1ca50 37 35 7d 20 54 68 65 20 6d 65 6d 6f 72 79 20 68 75} The memory h
1ca60 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 igh-water mark i
1ca70 73 20 72 65 73 65 74 20 74 6f 20 74 68 65 20 63 s reset to the c
1ca80 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 0a urrent value of.
1ca90 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
1caa0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 ite3_memory_used
1cab0 28 29 5d 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 ()] if and only
1cac0 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 if the parameter
1cad0 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 to.**
1cae0 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f [sqlite3_memory_
1caf0 68 69 67 68 77 61 74 65 72 28 29 5d 20 69 73 20 highwater()] is
1cb00 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65 true. The value
1cb10 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 20 20 20 returned.**
1cb20 20 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74 65 by [sqlite
1cb30 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 3_memory_highwat
1cb40 65 72 28 31 29 5d 20 69 73 20 74 68 65 20 68 69 er(1)] is the hi
1cb50 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 0a 2a 2a gh-water mark.**
1cb60 20 20 20 20 20 20 20 20 20 20 70 72 69 6f 72 20 prior
1cb70 74 6f 20 74 68 65 20 72 65 73 65 74 2e 0a 2a 2f to the reset..*/
1cb80 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
1cb90 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 te3_int64 sqlite
1cba0 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 3_memory_used(vo
1cbb0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
1cbc0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 sqlite3_int64 sq
1cbd0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 lite3_memory_hig
1cbe0 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 hwater(int reset
1cbf0 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 Flag);../*.** CA
1cc00 50 49 33 52 45 46 3a 20 50 73 65 75 64 6f 2d 52 PI3REF: Pseudo-R
1cc10 61 6e 64 6f 6d 20 4e 75 6d 62 65 72 20 47 65 6e andom Number Gen
1cc20 65 72 61 74 6f 72 20 7b 48 31 37 33 39 30 7d 20 erator {H17390}
1cc30 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 <S20000>.**.** S
1cc40 51 4c 69 74 65 20 63 6f 6e 74 61 69 6e 73 20 61 QLite contains a
1cc50 20 68 69 67 68 2d 71 75 61 6c 69 74 79 20 70 73 high-quality ps
1cc60 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 eudo-random numb
1cc70 65 72 20 67 65 6e 65 72 61 74 6f 72 20 28 50 52 er generator (PR
1cc80 4e 47 29 20 75 73 65 64 20 74 6f 0a 2a 2a 20 73 NG) used to.** s
1cc90 65 6c 65 63 74 20 72 61 6e 64 6f 6d 20 5b 52 4f elect random [RO
1cca0 57 49 44 20 7c 20 52 4f 57 49 44 73 5d 20 77 68 WID | ROWIDs] wh
1ccb0 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 en inserting new
1ccc0 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 records into a
1ccd0 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 61 6c table that.** al
1cce0 72 65 61 64 79 20 75 73 65 73 20 74 68 65 20 6c ready uses the l
1ccf0 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 argest possible
1cd00 5b 52 4f 57 49 44 5d 2e 20 20 54 68 65 20 50 52 [ROWID]. The PR
1cd10 4e 47 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 NG is also used
1cd20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 for.** the build
1cd30 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20 61 6e 64 -in random() and
1cd40 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 53 51 randomblob() SQ
1cd50 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 L functions. Th
1cd60 69 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c is interface all
1cd70 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 ows.** applicati
1cd80 6f 6e 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 ons to access th
1cd90 65 20 73 61 6d 65 20 50 52 4e 47 20 66 6f 72 20 e same PRNG for
1cda0 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a other purposes..
1cdb0 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 **.** A call to
1cdc0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f this routine sto
1cdd0 72 65 73 20 4e 20 62 79 74 65 73 20 6f 66 20 72 res N bytes of r
1cde0 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 62 andomness into b
1cdf0 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54 uffer P..**.** T
1ce00 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 he first time th
1ce10 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e is routine is in
1ce20 76 6f 6b 65 64 20 28 65 69 74 68 65 72 20 69 6e voked (either in
1ce30 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0a 2a ternally or by.*
1ce40 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f * the applicatio
1ce50 6e 29 20 74 68 65 20 50 52 4e 47 20 69 73 20 73 n) the PRNG is s
1ce60 65 65 64 65 64 20 75 73 69 6e 67 20 72 61 6e 64 eeded using rand
1ce70 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 0a omness obtained.
1ce80 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 52 61 6e ** from the xRan
1ce90 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f domness method o
1cea0 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 f the default [s
1ceb0 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
1cec0 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75 ct..** On all su
1ced0 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 bsequent invocat
1cee0 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 75 64 6f ions, the pseudo
1cef0 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73 20 67 -randomness is g
1cf00 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e 74 65 enerated.** inte
1cf10 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74 68 6f rnally and witho
1cf20 75 74 20 72 65 63 6f 75 72 73 65 20 74 6f 20 74 ut recourse to t
1cf30 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d he [sqlite3_vfs]
1cf40 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 xRandomness.**
1cf50 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e method..**.** IN
1cf60 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 VARIANTS:.**.**
1cf70 7b 48 31 37 33 39 32 7d 20 54 68 65 20 5b 73 71 {H17392} The [sq
1cf80 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
1cf90 28 4e 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65 (N,P)] interface
1cfa0 20 77 72 69 74 65 73 20 4e 20 62 79 74 65 73 20 writes N bytes
1cfb0 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 68 of.** h
1cfc0 69 67 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75 igh-quality pseu
1cfd0 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e do-randomness in
1cfe0 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2f 0a to buffer P..*/.
1cff0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1d000 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
1d010 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a ss(int N, void *
1d020 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 P);../*.** CAPI3
1d030 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d REF: Compile-Tim
1d040 65 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 e Authorization
1d050 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 Callbacks {H1250
1d060 30 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S70100>.**.*
1d070 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
1d080 65 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f egisters a autho
1d090 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 rizer callback w
1d0a0 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 ith a particular
1d0b0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f .** [database co
1d0c0 6e 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c nnection], suppl
1d0d0 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 ied in the first
1d0e0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 argument..** Th
1d0f0 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c e authorizer cal
1d100 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
1d110 20 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e as SQL statemen
1d120 74 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d ts are being com
1d130 70 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c piled.** by [sql
1d140 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 ite3_prepare()]
1d150 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 or its variants
1d160 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
1d170 5f 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 _v2()],.** [sqli
1d180 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d te3_prepare16()]
1d190 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 and [sqlite3_pr
1d1a0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 epare16_v2()].
1d1b0 41 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f At various.** po
1d1c0 69 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 ints during the
1d1d0 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 compilation proc
1d1e0 65 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 ess, as logic is
1d1f0 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a being created.*
1d200 2a 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 * to perform var
1d210 69 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 ious actions, th
1d220 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c e authorizer cal
1d230 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
1d240 20 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 to.** see if th
1d250 6f 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 ose actions are
1d260 61 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 allowed. The au
1d270 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1d280 6b 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 k should.** retu
1d290 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 rn [SQLITE_OK] t
1d2a0 6f 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 o allow the acti
1d2b0 6f 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f on, [SQLITE_IGNO
1d2c0 52 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 RE] to disallow
1d2d0 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 the.** specific
1d2e0 61 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 action but allow
1d2f0 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
1d300 6e 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 nt to continue t
1d310 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 o be.** compiled
1d320 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e , or [SQLITE_DEN
1d330 59 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 Y] to cause the
1d340 65 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 entire SQL state
1d350 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 ment to be.** re
1d360 6a 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 jected with an e
1d370 72 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 rror. If the au
1d380 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1d390 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 k returns.** any
1d3a0 20 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 value other tha
1d3b0 6e 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 n [SQLITE_IGNORE
1d3c0 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 ], [SQLITE_OK],
1d3d0 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d or [SQLITE_DENY]
1d3e0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 .** then the [sq
1d3f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1d400 28 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e ()] or equivalen
1d410 74 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 t call that trig
1d420 67 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 gered.** the aut
1d430 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 horizer will fai
1d440 6c 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 l with an error
1d450 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 message..**.** W
1d460 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b hen the callback
1d470 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
1d480 5f 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 _OK], that means
1d490 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a the operation.*
1d4a0 2a 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f * requested is o
1d4b0 6b 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c k. When the cal
1d4c0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 lback returns [S
1d4d0 51 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 QLITE_DENY], the
1d4e0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 .** [sqlite3_pre
1d4f0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 pare_v2()] or eq
1d500 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 uivalent call th
1d510 61 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 at triggered the
1d520 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 .** authorizer w
1d530 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e ill fail with an
1d540 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 error message e
1d550 78 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a xplaining that.*
1d560 2a 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 * access is deni
1d570 65 64 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 ed. If the auth
1d580 6f 72 69 7a 65 72 20 63 6f 64 65 20 69 73 20 5b orizer code is [
1d590 53 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20 SQLITE_READ].**
1d5a0 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b and the callback
1d5b0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
1d5c0 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 _IGNORE] then th
1d5d0 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 e.** [prepared s
1d5e0 74 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d tatement] statem
1d5f0 65 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 ent is construct
1d600 65 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65 ed to substitute
1d610 0a 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 .** a NULL value
1d620 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 in place of the
1d630 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 table column th
1d640 61 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a at would have.**
1d650 20 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 been read if [S
1d660 51 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 QLITE_OK] had be
1d670 65 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 en returned. Th
1d680 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 e [SQLITE_IGNORE
1d690 5d 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20 ].** return can
1d6a0 62 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20 be used to deny
1d6b0 61 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65 an untrusted use
1d6c0 72 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69 r access to indi
1d6d0 76 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e vidual.** column
1d6e0 73 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a s of a table..**
1d6f0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 .** The first pa
1d700 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 rameter to the a
1d710 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
1d720 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 ck is a copy of
1d730 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 the third.** par
1d740 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 ameter to the sq
1d750 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 lite3_set_author
1d760 69 7a 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 izer() interface
1d770 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 . The second par
1d780 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ameter.** to the
1d790 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 callback is an
1d7a0 69 6e 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f integer [SQLITE_
1d7b0 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f COPY | action co
1d7c0 64 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 de] that specifi
1d7d0 65 73 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63 es.** the partic
1d7e0 75 6c 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 ular action to b
1d7f0 65 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68 e authorized. Th
1d800 65 20 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 e third through
1d810 73 69 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 sixth parameters
1d820 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 .** to the callb
1d830 61 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 ack are zero-ter
1d840 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 minated strings
1d850 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 that contain add
1d860 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 itional.** detai
1d870 6c 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 ls about the act
1d880 69 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 ion to be author
1d890 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 ized..**.** An a
1d8a0 75 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 uthorizer is use
1d8b0 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f d when [sqlite3_
1d8c0 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 prepare | prepar
1d8d0 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 ing].** SQL stat
1d8e0 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 ements from an u
1d8f0 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c ntrusted source,
1d900 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 to ensure that
1d910 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
1d920 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 ts.** do not try
1d930 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 to access data
1d940 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c they are not all
1d950 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 owed to see, or
1d960 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 that they do not
1d970 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 .** try to execu
1d980 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 te malicious sta
1d990 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d tements that dam
1d9a0 61 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 age the database
1d9b0 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c . For.** exampl
1d9c0 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f e, an applicatio
1d9d0 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 n may allow a us
1d9e0 65 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 er to enter arbi
1d9f0 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 trary.** SQL que
1da00 72 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 ries for evaluat
1da10 69 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 ion by a databas
1da20 65 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c e. But the appl
1da30 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 ication does.**
1da40 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 not want the use
1da50 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 r to be able to
1da60 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 make arbitrary c
1da70 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a hanges to the.**
1da80 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 database. An a
1da90 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 uthorizer could
1daa0 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 then be put in p
1dab0 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a lace while the.*
1dac0 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 * user-entered S
1dad0 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c QL is being [sql
1dae0 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 ite3_prepare | p
1daf0 72 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a repared] that.**
1db00 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 disallows every
1db10 74 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 thing except [SE
1db20 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 LECT] statements
1db30 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 ..**.** Applicat
1db40 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 ions that need t
1db50 6f 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 o process SQL fr
1db60 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 om untrusted sou
1db70 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c rces.** might al
1db80 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 so consider lowe
1db90 72 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 ring resource li
1dba0 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 mits using [sqli
1dbb0 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 te3_limit()].**
1dbc0 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 and limiting dat
1dbd0 61 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 abase size using
1dbe0 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 the [max_page_c
1dbf0 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a ount] [PRAGMA].*
1dc00 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f * in addition to
1dc10 20 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 using an author
1dc20 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 izer..**.** Only
1dc30 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 a single author
1dc40 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 izer can be in p
1dc50 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 lace on a databa
1dc60 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a se connection.**
1dc70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 at a time. Eac
1dc80 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 h call to sqlite
1dc90 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 3_set_authorizer
1dca0 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a overrides the.*
1dcb0 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e * previous call.
1dcc0 20 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 Disable the au
1dcd0 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 thorizer by inst
1dce0 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 alling a NULL ca
1dcf0 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 llback..** The a
1dd00 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 uthorizer is dis
1dd10 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 abled by default
1dd20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 ..**.** The auth
1dd30 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 orizer callback
1dd40 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 must not do anyt
1dd50 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d hing that will m
1dd60 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 odify.** the dat
1dd70 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1dd80 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 that invoked th
1dd90 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c e authorizer cal
1dda0 6c 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 lback..** Note t
1ddb0 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 hat [sqlite3_pre
1ddc0 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b pare_v2()] and [
1ddd0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
1dde0 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 both modify thei
1ddf0 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f r.** database co
1de00 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 nnections for th
1de10 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f e meaning of "mo
1de20 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 dify" in this pa
1de30 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 ragraph..**.** W
1de40 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 hen [sqlite3_pre
1de50 70 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 pare_v2()] is us
1de60 65 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 ed to prepare a
1de70 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a statement, the.*
1de80 2a 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 * statement migh
1de90 74 20 62 65 20 72 65 70 72 65 70 61 72 65 64 20 t be reprepared
1dea0 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f during [sqlite3_
1deb0 73 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61 step()] due to a
1dec0 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e .** schema chan
1ded0 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 ge. Hence, the
1dee0 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 application shou
1def0 6c 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 ld ensure that t
1df00 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75 he.** correct au
1df10 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1df20 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61 k remains in pla
1df30 63 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73 ce during the [s
1df40 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a qlite3_step()]..
1df50 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
1df60 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 the authorizer c
1df70 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
1df80 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a ed only during.*
1df90 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
1dfa0 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 re()] or its var
1dfb0 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a iants. Authoriz
1dfc0 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 ation is not.**
1dfd0 70 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67 performed during
1dfe0 20 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75 statement evalu
1dff0 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65 ation in [sqlite
1e000 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 3_step()]..**.**
1e010 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
1e020 2a 2a 20 7b 48 31 32 35 30 31 7d 20 54 68 65 20 ** {H12501} The
1e030 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 [sqlite3_set_aut
1e040 68 6f 72 69 7a 65 72 28 44 2c 2e 2e 2e 29 5d 20 horizer(D,...)]
1e050 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 interface regist
1e060 65 72 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 ers a.**
1e070 20 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c authorizer cal
1e080 6c 62 61 63 6b 20 77 69 74 68 20 64 61 74 61 62 lback with datab
1e090 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 ase connection D
1e0a0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 32 7d ..**.** {H12502}
1e0b0 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 The authorizer
1e0c0 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f callback is invo
1e0d0 6b 65 64 20 61 73 20 53 51 4c 20 73 74 61 74 65 ked as SQL state
1e0e0 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 20 20 20 ments are.**
1e0f0 20 20 20 20 20 20 62 65 69 6e 67 20 70 61 72 73 being pars
1e100 65 65 64 20 61 6e 64 20 63 6f 6d 70 69 6c 65 64 eed and compiled
1e110 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 33 7d ..**.** {H12503}
1e120 20 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 7a If the authoriz
1e130 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 er callback retu
1e140 72 6e 73 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 rns any value ot
1e150 68 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 her than.**
1e160 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47 4e [SQLITE_IGN
1e170 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b ORE], [SQLITE_OK
1e180 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 ], or [SQLITE_DE
1e190 4e 59 5d 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 NY], then.**
1e1a0 20 20 20 20 20 20 74 68 65 20 61 70 70 6c 69 63 the applic
1e1b0 61 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 20 ation interface
1e1c0 63 61 6c 6c 20 74 68 61 74 20 63 61 75 73 65 64 call that caused
1e1d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
1e1e0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
1e1f0 62 61 63 6b 20 74 6f 20 72 75 6e 20 73 68 61 6c back to run shal
1e200 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a l fail with an.*
1e210 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 * [SQLI
1e220 54 45 5f 45 52 52 4f 52 5d 20 65 72 72 6f 72 20 TE_ERROR] error
1e230 63 6f 64 65 20 61 6e 64 20 61 6e 20 61 70 70 72 code and an appr
1e240 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 opriate error me
1e250 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ssage..**.** {H1
1e260 32 35 30 34 7d 20 57 68 65 6e 20 74 68 65 20 61 2504} When the a
1e270 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
1e280 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 ck returns [SQLI
1e290 54 45 5f 4f 4b 5d 2c 20 74 68 65 20 6f 70 65 72 TE_OK], the oper
1e2a0 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ation.**
1e2b0 20 20 64 65 73 63 72 69 62 65 64 20 69 73 20 70 described is p
1e2c0 72 6f 63 65 73 73 65 64 20 6e 6f 72 6d 61 6c 6c rocessed normall
1e2d0 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 35 y..**.** {H12505
1e2e0 7d 20 57 68 65 6e 20 74 68 65 20 61 75 74 68 6f } When the autho
1e2f0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 rizer callback r
1e300 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 eturns [SQLITE_D
1e310 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20 20 20 20 ENY], the.**
1e320 20 20 20 20 20 20 61 70 70 6c 69 63 61 74 69 6f applicatio
1e330 6e 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c n interface call
1e340 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 that caused the
1e350 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 .** aut
1e360 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1e370 20 74 6f 20 72 75 6e 20 73 68 61 6c 6c 20 66 61 to run shall fa
1e380 69 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 il.** w
1e390 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f 45 ith an [SQLITE_E
1e3a0 52 52 4f 52 5d 20 65 72 72 6f 72 20 63 6f 64 65 RROR] error code
1e3b0 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 and an error me
1e3c0 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 ssage.**
1e3d0 20 20 65 78 70 6c 61 69 6e 69 6e 67 20 74 68 61 explaining tha
1e3e0 74 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 t access is deni
1e3f0 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 ed..**.** {H1250
1e400 36 7d 20 49 66 20 74 68 65 20 61 75 74 68 6f 72 6} If the author
1e410 69 7a 65 72 20 63 6f 64 65 20 28 74 68 65 20 32 izer code (the 2
1e420 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
1e430 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 0a 2a the authorizer.*
1e440 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62 * callb
1e450 61 63 6b 29 20 69 73 20 5b 53 51 4c 49 54 45 5f ack) is [SQLITE_
1e460 52 45 41 44 5d 20 61 6e 64 20 74 68 65 20 61 75 READ] and the au
1e470 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1e480 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 k returns.**
1e490 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47 [SQLITE_IG
1e4a0 4e 4f 52 45 5d 2c 20 74 68 65 6e 20 74 68 65 20 NORE], then the
1e4b0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1e4c0 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 nt is constructe
1e4d0 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 d to.**
1e4e0 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 76 insert a NULL v
1e4f0 61 6c 75 65 20 69 6e 20 70 6c 61 63 65 20 6f 66 alue in place of
1e500 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d the table colum
1e510 6e 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 n that would hav
1e520 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 65 e.** be
1e530 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51 4c 49 en read if [SQLI
1e540 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20 TE_OK] had been
1e550 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
1e560 7b 48 31 32 35 30 37 7d 20 49 66 20 74 68 65 20 {H12507} If the
1e570 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 64 65 20 authorizer code
1e580 28 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 (the 2nd paramet
1e590 65 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 er to the author
1e5a0 69 7a 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 izer.**
1e5b0 20 63 61 6c 6c 62 61 63 6b 29 20 69 73 20 61 6e callback) is an
1e5c0 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 ything other tha
1e5d0 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 2c n [SQLITE_READ],
1e5e0 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 then.**
1e5f0 20 20 61 20 72 65 74 75 72 6e 20 6f 66 20 5b 53 a return of [S
1e600 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 68 61 QLITE_IGNORE] ha
1e610 73 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63 s the same effec
1e620 74 20 61 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e t as [SQLITE_DEN
1e630 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 31 Y]..**.** {H1251
1e640 30 7d 20 54 68 65 20 66 69 72 73 74 20 70 61 72 0} The first par
1e650 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 ameter to the au
1e660 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1e670 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a k is a copy of.*
1e680 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74 * the t
1e690 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 hird parameter t
1e6a0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 o the [sqlite3_s
1e6b0 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d et_authorizer()]
1e6c0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a interface..**.*
1e6d0 2a 20 7b 48 31 32 35 31 31 7d 20 54 68 65 20 73 * {H12511} The s
1e6e0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
1e6f0 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 to the callback
1e700 69 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a is an integer.**
1e710 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 [SQLIT
1e720 45 5f 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 E_COPY | action
1e730 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 63 69 code] that speci
1e740 66 69 65 73 20 74 68 65 20 70 61 72 74 69 63 75 fies the particu
1e750 6c 61 72 20 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 lar action.**
1e760 20 20 20 20 20 20 20 74 6f 20 62 65 20 61 75 74 to be aut
1e770 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b horized..**.** {
1e780 48 31 32 35 31 32 7d 20 54 68 65 20 74 68 69 72 H12512} The thir
1e790 64 20 74 68 72 6f 75 67 68 20 73 69 78 74 68 20 d through sixth
1e7a0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 parameters to th
1e7b0 65 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 0a 2a e callback are.*
1e7c0 2a 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 2d * zero-
1e7d0 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e terminated strin
1e7e0 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 0a gs that contain.
1e7f0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 64 64 69 ** addi
1e800 74 69 6f 6e 61 6c 20 64 65 74 61 69 6c 73 20 61 tional details a
1e810 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20 bout the action
1e820 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64 to be authorized
1e830 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 30 7d ..**.** {H12520}
1e840 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 Each call to [s
1e850 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f qlite3_set_autho
1e860 72 69 7a 65 72 28 29 5d 20 6f 76 65 72 72 69 64 rizer()] overrid
1e870 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 es.** a
1e880 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e ny previously in
1e890 73 74 61 6c 6c 65 64 20 61 75 74 68 6f 72 69 7a stalled authoriz
1e8a0 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 er..**.** {H1252
1e8b0 31 7d 20 41 20 4e 55 4c 4c 20 61 75 74 68 6f 72 1} A NULL author
1e8c0 69 7a 65 72 20 6d 65 61 6e 73 20 74 68 61 74 20 izer means that
1e8d0 6e 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e no authorization
1e8e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c .** cal
1e8f0 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
1e900 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 32 7d ..**.** {H12522}
1e910 20 54 68 65 20 64 65 66 61 75 6c 74 20 61 75 74 The default aut
1e920 68 6f 72 69 7a 65 72 20 69 73 20 4e 55 4c 4c 2e horizer is NULL.
1e930 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1e940 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 nt sqlite3_set_a
1e950 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c uthorizer(. sql
1e960 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a 78 ite3*,. int (*x
1e970 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c Auth)(void*,int,
1e980 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 const char*,cons
1e990 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 t char*,const ch
1e9a0 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 ar*,const char*)
1e9b0 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 ,. void *pUserD
1e9c0 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ata.);../*.** CA
1e9d0 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a PI3REF: Authoriz
1e9e0 65 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73 20 er Return Codes
1e9f0 7b 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30 30 {H12590} <H12500
1ea00 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c >.**.** The [sql
1ea10 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 ite3_set_authori
1ea20 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 zer | authorizer
1ea30 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
1ea40 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75 on] must.** retu
1ea50 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 rn either [SQLIT
1ea60 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 E_OK] or one of
1ea70 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61 these two consta
1ea80 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 nts in order.**
1ea90 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65 to signal SQLite
1eaa0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1eab0 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 the action is pe
1eac0 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 68 rmitted. See th
1ead0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65 e.** [sqlite3_se
1eae0 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 t_authorizer | a
1eaf0 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65 uthorizer docume
1eb00 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64 ntation] for add
1eb10 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 itional.** infor
1eb20 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 mation..*/.#defi
1eb30 6e 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20 ne SQLITE_DENY
1eb40 20 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68 1 /* Abort th
1eb50 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
1eb60 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f with an error */
1eb70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1eb80 49 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f IGNORE 2 /* Do
1eb90 6e 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 n't allow access
1eba0 2c 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65 , but don't gene
1ebb0 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f rate an error */
1ebc0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
1ebd0 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74 : Authorizer Act
1ebe0 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 35 ion Codes {H1255
1ebf0 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 0} <H12500>.**.*
1ec00 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 * The [sqlite3_s
1ec10 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d et_authorizer()]
1ec20 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 interface regis
1ec30 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 ters a callback
1ec40 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 function.** that
1ec50 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61 is invoked to a
1ec60 75 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69 6e uthorize certain
1ec70 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 SQL statement a
1ec80 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20 ctions. The.**
1ec90 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
1eca0 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b to the callback
1ecb0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 is an integer c
1ecc0 6f 64 65 20 74 68 61 74 20 73 70 65 63 69 66 69 ode that specifi
1ecd0 65 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f es.** what actio
1ece0 6e 20 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f n is being autho
1ecf0 72 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72 rized. These ar
1ed00 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 61 63 e the integer ac
1ed10 74 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a tion codes that.
1ed20 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 ** the authorize
1ed30 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62 r callback may b
1ed40 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 e passed..**.**
1ed50 54 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64 These action cod
1ed60 65 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79 e values signify
1ed70 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70 what kind of op
1ed80 65 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 eration is to be
1ed90 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 .** authorized.
1eda0 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 The 3rd and 4th
1edb0 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 parameters to t
1edc0 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e he authorization
1edd0 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e .** callback fun
1ede0 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61 ction will be pa
1edf0 72 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c rameters or NULL
1ee00 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 depending on wh
1ee10 69 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 ich of these.**
1ee20 63 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 73 codes is used as
1ee30 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
1ee40 6d 65 74 65 72 2e 20 20 54 68 65 20 35 74 68 20 meter. The 5th
1ee50 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
1ee60 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63 .** authorizer c
1ee70 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e allback is the n
1ee80 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
1ee90 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 ase ("main", "te
1eea0 6d 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 66 mp",.** etc.) if
1eeb0 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68 applicable. Th
1eec0 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 6th parameter
1eed0 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 to the authorize
1eee0 72 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 r callback.** is
1eef0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1ef00 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67 inner-most trig
1ef10 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 74 ger or view that
1ef20 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
1ef30 66 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73 for.** the acces
1ef40 73 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c s attempt or NUL
1ef50 4c 20 69 66 20 74 68 69 73 20 61 63 63 65 73 73 L if this access
1ef60 20 61 74 74 65 6d 70 74 20 69 73 20 64 69 72 65 attempt is dire
1ef70 63 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f 70 ctly from.** top
1ef80 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e -level SQL code.
1ef90 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 .**.** INVARIANT
1efa0 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35 31 S:.**.** {H12551
1efb0 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 } The second par
1efc0 61 6d 65 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 ameter to an.**
1efd0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
1efe0 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 3_set_authorizer
1eff0 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 | authorizer ca
1f000 6c 6c 62 61 63 6b 5d 20 73 68 61 6c 6c 20 62 65 llback] shall be
1f010 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 an integer.**
1f020 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f [SQLITE_
1f030 43 4f 50 59 20 7c 20 61 75 74 68 6f 72 69 7a 65 COPY | authorize
1f040 72 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 r code] that spe
1f050 63 69 66 69 65 73 20 77 68 61 74 20 61 63 74 69 cifies what acti
1f060 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 on.** i
1f070 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72 69 7a s being authoriz
1f080 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35 ed..**.** {H1255
1f090 32 7d 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 2} The 3rd and 4
1f0a0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f th parameters to
1f0b0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
1f0c0 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 [sqlite3_set_au
1f0d0 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f thorizer | autho
1f0e0 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 rization callbac
1f0f0 6b 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 k].** s
1f100 68 61 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 hall be paramete
1f110 72 73 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e rs or NULL depen
1f120 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 0a 2a 2a ding on which.**
1f130 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 [SQLIT
1f140 45 5f 43 4f 50 59 20 7c 20 61 75 74 68 6f 72 69 E_COPY | authori
1f150 7a 65 72 20 63 6f 64 65 5d 20 69 73 20 75 73 65 zer code] is use
1f160 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
1f170 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a parameter..**.**
1f180 20 7b 48 31 32 35 35 33 7d 20 54 68 65 20 35 74 {H12553} The 5t
1f190 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
1f1a0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
1f1b0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
1f1c0 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 orizer | authori
1f1d0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68 zer callback] sh
1f1e0 61 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a all be the name.
1f1f0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 ** of t
1f200 68 65 20 64 61 74 61 62 61 73 65 20 28 65 78 61 he database (exa
1f210 6d 70 6c 65 3a 20 22 6d 61 69 6e 22 2c 20 22 74 mple: "main", "t
1f220 65 6d 70 22 2c 20 65 74 63 2e 29 20 69 66 20 61 emp", etc.) if a
1f230 70 70 6c 69 63 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a pplicable..**.**
1f240 20 7b 48 31 32 35 35 34 7d 20 54 68 65 20 36 74 {H12554} The 6t
1f250 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
1f260 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
1f270 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
1f280 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 orizer | authori
1f290 7a 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68 zer callback] sh
1f2a0 61 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a all be the name.
1f2b0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 ** of t
1f2c0 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 he inner-most tr
1f2d0 69 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 igger or view th
1f2e0 61 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c at is responsibl
1f2f0 65 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 e for.**
1f300 20 20 74 68 65 20 61 63 63 65 73 73 20 61 74 74 the access att
1f310 65 6d 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 empt or NULL if
1f320 74 68 69 73 20 61 63 63 65 73 73 20 61 74 74 65 this access atte
1f330 6d 70 74 20 69 73 20 64 69 72 65 63 74 6c 79 20 mpt is directly
1f340 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 from.**
1f350 20 74 6f 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63 top-level SQL c
1f360 6f 64 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ode..*/./*******
1f370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f390 2a 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a **** 3rd *******
1f3a0 2a 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a ***** 4th ******
1f3b0 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 *****/.#define S
1f3c0 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 QLITE_CREATE_IND
1f3d0 45 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20 EX 1
1f3e0 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 /* Index Name
1f3f0 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
1f400 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1f410 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c LITE_CREATE_TABL
1f420 45 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f E 2 /
1f430 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 * Table Name
1f440 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1f450 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1f460 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f ITE_CREATE_TEMP_
1f470 49 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a INDEX 3 /*
1f480 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 Index Name
1f490 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1f4a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1f4b0 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 TE_CREATE_TEMP_T
1f4c0 41 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 ABLE 4 /*
1f4d0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1f4e0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
1f4f0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1f500 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 E_CREATE_TEMP_TR
1f510 49 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 IGGER 5 /* T
1f520 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 rigger Name T
1f530 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a able Name *
1f540 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1f550 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 _CREATE_TEMP_VIE
1f560 57 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 W 6 /* Vi
1f570 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 ew Name NU
1f580 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1f590 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1f5a0 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 CREATE_TRIGGER
1f5b0 20 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 7 /* Tri
1f5c0 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 gger Name Tab
1f5d0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a le Name */.
1f5e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
1f5f0 52 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 REATE_VIEW
1f600 20 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 8 /* View
1f610 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c Name NULL
1f620 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1f630 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
1f640 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 LETE
1f650 20 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 9 /* Table
1f660 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
1f670 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1f680 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f efine SQLITE_DRO
1f690 50 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 P_INDEX
1f6a0 20 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 10 /* Index
1f6b0 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 Name Table
1f6c0 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
1f6d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 fine SQLITE_DROP
1f6e0 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 _TABLE
1f6f0 20 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 11 /* Table N
1f700 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 ame NULL
1f710 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1f720 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f ine SQLITE_DROP_
1f730 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 TEMP_INDEX
1f740 31 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 12 /* Index Na
1f750 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 me Table Na
1f760 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 me */.#defi
1f770 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 ne SQLITE_DROP_T
1f780 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 EMP_TABLE 1
1f790 33 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 3 /* Table Nam
1f7a0 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 e NULL
1f7b0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
1f7c0 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 e SQLITE_DROP_TE
1f7d0 4d 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 MP_TRIGGER 14
1f7e0 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 /* Trigger Na
1f7f0 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 me Table Name
1f800 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1f810 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d SQLITE_DROP_TEM
1f820 50 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20 P_VIEW 15
1f830 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 /* View Name
1f840 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 NULL
1f850 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1f860 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 SQLITE_DROP_TRIG
1f870 47 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20 GER 16
1f880 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 /* Trigger Name
1f890 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 Table Name
1f8a0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1f8b0 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 QLITE_DROP_VIEW
1f8c0 20 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20 17
1f8d0 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 /* View Name
1f8e0 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
1f8f0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1f900 4c 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 LITE_INSERT
1f910 20 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 18 /
1f920 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 * Table Name
1f930 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1f940 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1f950 49 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 ITE_PRAGMA
1f960 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 19 /*
1f970 20 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 Pragma Name
1f980 20 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 1st arg or NULL
1f990 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1f9a0 54 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 TE_READ
1f9b0 20 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 20 /*
1f9c0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1f9d0 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 Column Name
1f9e0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1f9f0 45 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 E_SELECT
1fa00 20 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 21 /* N
1fa10 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e ULL N
1fa20 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1fa30 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1fa40 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 _TRANSACTION
1fa50 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 22 /* Op
1fa60 65 72 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 eration NU
1fa70 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1fa80 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1fa90 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 UPDATE
1faa0 20 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 23 /* Tab
1fab0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c le Name Col
1fac0 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a umn Name */.
1fad0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
1fae0 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 TTACH
1faf0 20 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 24 /* File
1fb00 6e 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c name NULL
1fb10 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1fb20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
1fb30 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 TACH
1fb40 20 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 25 /* Datab
1fb50 61 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 ase Name NULL
1fb60 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1fb70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 efine SQLITE_ALT
1fb80 45 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 ER_TABLE
1fb90 20 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 26 /* Databa
1fba0 73 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 se Name Table
1fbb0 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
1fbc0 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e fine SQLITE_REIN
1fbd0 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 DEX
1fbe0 20 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 27 /* Index N
1fbf0 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 ame NULL
1fc00 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1fc10 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 ine SQLITE_ANALY
1fc20 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ZE
1fc30 32 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 28 /* Table Na
1fc40 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1fc50 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1fc60 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 ne SQLITE_CREATE
1fc70 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 _VTABLE 2
1fc80 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 9 /* Table Nam
1fc90 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 e Module Na
1fca0 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e me */.#defin
1fcb0 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 e SQLITE_DROP_VT
1fcc0 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 ABLE 30
1fcd0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1fce0 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d Module Nam
1fcf0 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 e */.#define
1fd00 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e SQLITE_FUNCTION
1fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20 31
1fd20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 /* NULL
1fd30 20 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 Function Na
1fd40 6d 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 me */.#define
1fd50 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 SQLITE_SAVEPOINT
1fd60 20 20 20 20 20 20 20 20 20 20 20 20 33 32 20 20 32
1fd70 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 /* Operation
1fd80 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 4e 61 Savepoint Na
1fd90 6d 65 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 me */.#define S
1fda0 51 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20 QLITE_COPY
1fdb0 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 0
1fdc0 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 /* No longer use
1fdd0 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 d */../*.** CAPI
1fde0 33 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 3REF: Tracing An
1fdf0 64 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 d Profiling Func
1fe00 74 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c tions {H12280} <
1fe10 53 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S60400>.** EXPER
1fe20 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
1fe30 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67 ese routines reg
1fe40 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 ister callback f
1fe50 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 unctions that ca
1fe60 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a n be used for.**
1fe70 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f tracing and pro
1fe80 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75 filing the execu
1fe90 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 tion of SQL stat
1fea0 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ements..**.** Th
1feb0 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 e callback funct
1fec0 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 ion registered b
1fed0 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 y sqlite3_trace(
1fee0 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a ) is invoked at.
1fef0 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73 ** various times
1ff00 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 when an SQL sta
1ff10 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 tement is being
1ff20 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f run by [sqlite3_
1ff30 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 step()]..** The
1ff40 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 callback returns
1ff50 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69 a UTF-8 renderi
1ff60 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 ng of the SQL st
1ff70 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 atement text.**
1ff80 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 as the statement
1ff90 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 78 first begins ex
1ffa0 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69 ecuting. Additi
1ffb0 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f onal callbacks o
1ffc0 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 ccur.** as each
1ffd0 74 72 69 67 67 65 72 65 64 20 73 75 62 70 72 6f triggered subpro
1ffe0 67 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e gram is entered.
1fff0 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 The callbacks
20000 66 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 for triggers.**
20010 63 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20 contain a UTF-8
20020 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 SQL comment that
20030 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 identifies the
20040 74 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 trigger..**.** T
20050 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 he callback func
20060 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 tion registered
20070 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 by sqlite3_profi
20080 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a le() is invoked.
20090 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73 ** as each SQL s
200a0 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 tatement finishe
200b0 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20 s. The profile
200c0 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e callback contain
200d0 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 s.** the origina
200e0 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 l statement text
200f0 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65 and an estimate
20100 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 of wall-clock t
20110 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f ime.** of how lo
20120 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e ng that statemen
20130 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a t took to run..*
20140 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
20150 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 38 31 7d 20 .**.** {H12281}
20160 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e The callback fun
20170 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 ction registered
20180 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74 72 61 by [sqlite3_tra
20190 63 65 28 29 5d 20 0a 2a 2a 20 20 20 20 20 20 20 ce()] .**
201a0 20 20 20 73 68 61 6c 6c 20 62 65 20 69 6e 76 6f shall be invo
201b0 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ked.**
201c0 77 68 65 6e 65 76 65 72 20 61 6e 20 53 51 4c 20 whenever an SQL
201d0 73 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74 20 statement first
201e0 62 65 67 69 6e 73 20 74 6f 20 65 78 65 63 75 74 begins to execut
201f0 65 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 e and.**
20200 20 20 77 68 65 6e 65 76 65 72 20 61 20 74 72 69 whenever a tri
20210 67 67 65 72 20 73 75 62 70 72 6f 67 72 61 6d 20 gger subprogram
20220 66 69 72 73 74 20 62 65 67 69 6e 73 20 74 6f 20 first begins to
20230 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 run..**.** {H122
20240 38 32 7d 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 82} Each call to
20250 20 5b 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 [sqlite3_trace(
20260 29 5d 20 73 68 61 6c 6c 20 6f 76 65 72 72 69 64 )] shall overrid
20270 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 e the previously
20280 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 67 .** reg
20290 69 73 74 65 72 65 64 20 74 72 61 63 65 20 63 61 istered trace ca
202a0 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 7b 48 llback..**.** {H
202b0 31 32 32 38 33 7d 20 41 20 4e 55 4c 4c 20 74 72 12283} A NULL tr
202c0 61 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 61 ace callback sha
202d0 6c 6c 20 64 69 73 61 62 6c 65 20 74 72 61 63 69 ll disable traci
202e0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 38 ng..**.** {H1228
202f0 34 7d 20 54 68 65 20 66 69 72 73 74 20 61 72 67 4} The first arg
20300 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61 ument to the tra
20310 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 61 6c ce callback shal
20320 6c 20 62 65 20 61 20 63 6f 70 79 20 6f 66 0a 2a l be a copy of.*
20330 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 70 * the p
20340 6f 69 6e 74 65 72 20 77 68 69 63 68 20 77 61 73 ointer which was
20350 20 74 68 65 20 33 72 64 20 61 72 67 75 6d 65 6e the 3rd argumen
20360 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74 72 t to [sqlite3_tr
20370 61 63 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ace()]..**.** {H
20380 31 32 32 38 35 7d 20 54 68 65 20 73 65 63 6f 6e 12285} The secon
20390 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 d argument to th
203a0 65 20 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b e trace callback
203b0 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 is a.**
203c0 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 zero-terminate
203d0 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63 d UTF-8 string c
203e0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 72 ontaining the or
203f0 69 67 69 6e 61 6c 20 74 65 78 74 0a 2a 2a 20 20 iginal text.**
20400 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 53 of the S
20410 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73 20 QL statement as
20420 69 74 20 77 61 73 20 70 61 73 73 65 64 20 69 6e it was passed in
20430 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 to [sqlite3_prep
20440 61 72 65 5f 76 32 28 29 5d 0a 2a 2a 20 20 20 20 are_v2()].**
20450 20 20 20 20 20 20 6f 72 20 74 68 65 20 65 71 75 or the equ
20460 69 76 61 6c 65 6e 74 2c 20 6f 72 20 61 6e 20 53 ivalent, or an S
20470 51 4c 20 63 6f 6d 6d 65 6e 74 20 69 6e 64 69 63 QL comment indic
20480 61 74 69 6e 67 20 74 68 65 20 62 65 67 69 6e 6e ating the beginn
20490 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ing.**
204a0 6f 66 20 61 20 74 72 69 67 67 65 72 20 73 75 62 of a trigger sub
204b0 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b program..**.** {
204c0 48 31 32 32 38 37 7d 20 54 68 65 20 63 61 6c 6c H12287} The call
204d0 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 back function re
204e0 67 69 73 74 65 72 65 64 20 62 79 20 5b 73 71 6c gistered by [sql
204f0 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 5d 20 ite3_profile()]
20500 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 20 20 is invoked.**
20510 20 20 20 20 20 20 20 61 73 20 65 61 63 68 20 53 as each S
20520 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 69 6e QL statement fin
20530 69 73 68 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ishes..**.** {H1
20540 32 32 38 38 7d 20 54 68 65 20 66 69 72 73 74 20 2288} The first
20550 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
20560 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 profile callbac
20570 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a k is a copy of.*
20580 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 33 * the 3
20590 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 rd parameter to
205a0 5b 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 [sqlite3_profile
205b0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 ()]..**.** {H122
205c0 38 39 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 70 89} The second p
205d0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
205e0 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b profile callback
205f0 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 is a.**
20600 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 zero-terminate
20610 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 d UTF-8 string t
20620 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
20630 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f complete text o
20640 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 f.** th
20650 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
20660 61 73 20 69 74 20 77 61 73 20 70 72 6f 63 65 73 as it was proces
20670 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f sed by [sqlite3_
20680 70 72 65 70 61 72 65 5f 76 32 28 29 5d 0a 2a 2a prepare_v2()].**
20690 20 20 20 20 20 20 20 20 20 20 6f 72 20 74 68 65 or the
206a0 20 65 71 75 69 76 61 6c 65 6e 74 2e 0a 2a 2a 0a equivalent..**.
206b0 2a 2a 20 7b 48 31 32 32 39 30 7d 20 54 68 65 20 ** {H12290} The
206c0 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 third parameter
206d0 74 6f 20 74 68 65 20 70 72 6f 66 69 6c 65 20 63 to the profile c
206e0 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 65 73 allback is an es
206f0 74 69 6d 61 74 65 0a 2a 2a 20 20 20 20 20 20 20 timate.**
20700 20 20 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 of the number
20710 20 6f 66 20 6e 61 6e 6f 73 65 63 6f 6e 64 73 20 of nanoseconds
20720 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69 of wall-clock ti
20730 6d 65 20 72 65 71 75 69 72 65 64 20 74 6f 0a 2a me required to.*
20740 2a 20 20 20 20 20 20 20 20 20 20 72 75 6e 20 74 * run t
20750 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
20760 20 66 72 6f 6d 20 73 74 61 72 74 20 74 6f 20 66 from start to f
20770 69 6e 69 73 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 inish..*/.SQLITE
20780 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 _API SQLITE_EXPE
20790 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a 73 RIMENTAL void *s
207a0 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c qlite3_trace(sql
207b0 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 72 ite3*, void(*xTr
207c0 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 ace)(void*,const
207d0 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 3b char*), void*);
207e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
207f0 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
20800 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72 void *sqlite3_pr
20810 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c 0a ofile(sqlite3*,.
20820 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 6c void(*xProfil
20830 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 e)(void*,const c
20840 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 6e har*,sqlite3_uin
20850 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f t64), void*);../
20860 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 51 *.** CAPI3REF: Q
20870 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 43 61 uery Progress Ca
20880 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 30 7d llbacks {H12910}
20890 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S60400>.**.**
208a0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e This routine con
208b0 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c 62 61 figures a callba
208c0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 74 68 ck function - th
208d0 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 63 61 e.** progress ca
208e0 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 69 73 llback - that is
208f0 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f 64 69 invoked periodi
20900 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c 6f 6e cally during lon
20910 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 61 6c g.** running cal
20920 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 ls to [sqlite3_e
20930 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 xec()], [sqlite3
20940 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a 2a 20 _step()] and.**
20950 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 [sqlite3_get_tab
20960 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d 70 le()]. An examp
20970 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 73 0a le use for this.
20980 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 ** interface is
20990 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75 70 to keep a GUI up
209a0 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20 6c dated during a l
209b0 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a arge query..**.*
209c0 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 73 * If the progres
209d0 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 s callback retur
209e0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 ns non-zero, the
209f0 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a operation is.**
20a00 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20 54 interrupted. T
20a10 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e 20 his feature can
20a20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 be used to imple
20a30 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63 65 ment a.** "Cance
20a40 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20 47 l" button on a G
20a50 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61 6c UI progress dial
20a60 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 og box..**.** Th
20a70 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c e progress handl
20a80 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 er must not do a
20a90 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c nything that wil
20aa0 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 l modify.** the
20ab0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
20ac0 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 ion that invoked
20ad0 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 the progress ha
20ae0 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 20 74 ndler..** Note t
20af0 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 hat [sqlite3_pre
20b00 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b pare_v2()] and [
20b10 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
20b20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 both modify thei
20b30 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f r.** database co
20b40 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 nnections for th
20b50 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f e meaning of "mo
20b60 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 dify" in this pa
20b70 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 49 ragraph..**.** I
20b80 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
20b90 20 7b 48 31 32 39 31 31 7d 20 54 68 65 20 63 61 {H12911} The ca
20ba0 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 llback function
20bb0 72 65 67 69 73 74 65 72 65 64 20 62 79 20 73 71 registered by sq
20bc0 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 lite3_progress_h
20bd0 61 6e 64 6c 65 72 28 29 0a 2a 2a 20 20 20 20 20 andler().**
20be0 20 20 20 20 20 69 73 20 69 6e 76 6f 6b 65 64 20 is invoked
20bf0 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64 75 72 periodically dur
20c00 69 6e 67 20 6c 6f 6e 67 20 72 75 6e 6e 69 6e 67 ing long running
20c10 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20 20 calls to.**
20c20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 [sqlite3_s
20c30 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 tep()]..**.** {H
20c40 31 32 39 31 32 7d 20 54 68 65 20 70 72 6f 67 72 12912} The progr
20c50 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 ess callback is
20c60 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 invoked once for
20c70 20 65 76 65 72 79 20 4e 20 76 69 72 74 75 61 6c every N virtual
20c80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 61 63 .** mac
20c90 68 69 6e 65 20 6f 70 63 6f 64 65 73 2c 20 77 68 hine opcodes, wh
20ca0 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63 ere N is the sec
20cb0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a ond argument to.
20cc0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
20cd0 5b 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 [sqlite3_progres
20ce0 73 5f 68 61 6e 64 6c 65 72 28 29 5d 20 63 61 6c s_handler()] cal
20cf0 6c 20 74 68 61 74 20 72 65 67 69 73 74 65 72 65 l that registere
20d00 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 d.** th
20d10 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 49 66 20 e callback. If
20d20 4e 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 31 N is less than 1
20d30 2c 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 , sqlite3_progre
20d40 73 73 5f 68 61 6e 64 6c 65 72 28 29 0a 2a 2a 20 ss_handler().**
20d50 20 20 20 20 20 20 20 20 20 61 63 74 73 20 61 73 acts as
20d60 20 69 66 20 61 20 4e 55 4c 4c 20 70 72 6f 67 72 if a NULL progr
20d70 65 73 73 20 68 61 6e 64 6c 65 72 20 68 61 64 20 ess handler had
20d80 62 65 65 6e 20 73 70 65 63 69 66 69 65 64 2e 0a been specified..
20d90 2a 2a 0a 2a 2a 20 7b 48 31 32 39 31 33 7d 20 54 **.** {H12913} T
20da0 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c he progress call
20db0 62 61 63 6b 20 69 74 73 65 6c 66 20 69 73 20 69 back itself is i
20dc0 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 dentified by the
20dd0 20 74 68 69 72 64 0a 2a 2a 20 20 20 20 20 20 20 third.**
20de0 20 20 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 argument to s
20df0 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f qlite3_progress_
20e00 68 61 6e 64 6c 65 72 28 29 2e 0a 2a 2a 0a 2a 2a handler()..**.**
20e10 20 7b 48 31 32 39 31 34 7d 20 54 68 65 20 66 6f {H12914} The fo
20e20 75 72 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f urth argument to
20e30 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 sqlite3_progres
20e40 73 5f 68 61 6e 64 6c 65 72 28 29 20 69 73 20 61 s_handler() is a
20e50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 6f 69 .** voi
20e60 64 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64 d pointer passed
20e70 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 to the progress
20e80 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 callback.**
20e90 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 65 function e
20ea0 61 63 68 20 74 69 6d 65 20 69 74 20 69 73 20 69 ach time it is i
20eb0 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 nvoked..**.** {H
20ec0 31 32 39 31 35 7d 20 49 66 20 61 20 63 61 6c 6c 12915} If a call
20ed0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 to [sqlite3_ste
20ee0 70 28 29 5d 20 72 65 73 75 6c 74 73 20 69 6e 20 p()] results in
20ef0 66 65 77 65 72 20 74 68 61 6e 20 4e 20 6f 70 63 fewer than N opc
20f00 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 odes.**
20f10 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 2c being executed,
20f20 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 65 then the progre
20f30 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e ss callback is n
20f40 65 76 65 72 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a ever invoked..**
20f50 0a 2a 2a 20 7b 48 31 32 39 31 36 7d 20 45 76 65 .** {H12916} Eve
20f60 72 79 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ry call to [sqli
20f70 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e te3_progress_han
20f80 64 6c 65 72 28 29 5d 0a 2a 2a 20 20 20 20 20 20 dler()].**
20f90 20 20 20 20 6f 76 65 72 77 72 69 74 65 73 20 61 overwrites a
20fa0 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 ny previously re
20fb0 67 69 73 74 65 72 65 64 20 70 72 6f 67 72 65 73 gistered progres
20fc0 73 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a s handler..**.**
20fd0 20 7b 48 31 32 39 31 37 7d 20 49 66 20 74 68 65 {H12917} If the
20fe0 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 progress handle
20ff0 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 r callback is NU
21000 4c 4c 20 74 68 65 6e 20 6e 6f 20 70 72 6f 67 72 LL then no progr
21010 65 73 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ess.**
21020 68 61 6e 64 6c 65 72 20 69 73 20 69 6e 76 6f 6b handler is invok
21030 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 31 ed..**.** {H1291
21040 38 7d 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 8} If the progre
21050 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 ss callback retu
21060 72 6e 73 20 61 20 72 65 73 75 6c 74 20 6f 74 68 rns a result oth
21070 65 72 20 74 68 61 6e 20 30 2c 20 74 68 65 6e 0a er than 0, then.
21080 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
21090 62 65 68 61 76 69 6f 72 20 69 73 20 61 20 69 66 behavior is a if
210a0 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 [sqlite3_interr
210b0 75 70 74 28 29 5d 20 68 61 64 20 62 65 65 6e 20 upt()] had been
210c0 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 20 20 20 called..**
210d0 20 20 20 20 3c 53 33 30 35 30 30 3e 0a 2a 2f 0a <S30500>.*/.
210e0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
210f0 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 sqlite3_progress
21100 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 _handler(sqlite3
21110 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76 *, int, int(*)(v
21120 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a oid*), void*);..
21130 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
21140 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61 Opening A New Da
21150 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f tabase Connectio
21160 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32 n {H12700} <S402
21170 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 00>.**.** These
21180 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e routines open an
21190 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 SQLite database
211a0 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65 file whose name
211b0 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 is given by the
211c0 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 .** filename arg
211d0 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e ument. The filen
211e0 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ame argument is
211f0 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 55 interpreted as U
21200 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 TF-8 for.** sqli
21210 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 te3_open() and s
21220 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
21230 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69 and as UTF-16 i
21240 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 n the native byt
21250 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73 e.** order for s
21260 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e qlite3_open16().
21270 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e A [database con
21280 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 nection] handle
21290 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65 is usually.** re
212a0 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c turned in *ppDb,
212b0 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f even if an erro
212c0 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f r occurs. The o
212d0 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73 nly exception is
212e0 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69 that.** if SQLi
212f0 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 te is unable to
21300 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 allocate memory
21310 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c to hold the [sql
21320 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a ite3] object,.**
21330 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20 a NULL will be
21340 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70 written into *pp
21350 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 Db instead of a
21360 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b pointer to the [
21370 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65 sqlite3].** obje
21380 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 ct. If the datab
21390 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61 ase is opened (a
213a0 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73 nd/or created) s
213b0 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65 uccessfully, the
213c0 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d n.** [SQLITE_OK]
213d0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f is returned. O
213e0 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 therwise an [err
213f0 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
21400 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73 rned. The.** [s
21410 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d qlite3_errmsg()]
21420 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 or [sqlite3_err
21430 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 msg16()] routine
21440 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f s can be used to
21450 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e obtain.** an En
21460 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64 glish language d
21470 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 escription of th
21480 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 e error..**.** T
21490 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64 he default encod
214a0 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 ing for the data
214b0 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46 base will be UTF
214c0 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 -8 if.** sqlite3
214d0 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 _open() or sqlit
214e0 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 e3_open_v2() is
214f0 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54 called and.** UT
21500 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69 F-16 in the nati
21510 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66 ve byte order if
21520 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 sqlite3_open16(
21530 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a ) is used..**.**
21540 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 Whether or not
21550 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
21560 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 when it is opene
21570 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20 d, resources.**
21580 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
21590 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
215a0 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 nnection] handle
215b0 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61 should be relea
215c0 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e sed by.** passin
215d0 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 g it to [sqlite3
215e0 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69 _close()] when i
215f0 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 t is no longer r
21600 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 equired..**.** T
21610 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f he sqlite3_open_
21620 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 77 v2() interface w
21630 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 orks like sqlite
21640 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65 3_open().** exce
21650 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65 70 pt that it accep
21660 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 ts two additiona
21670 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 l parameters for
21680 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74 additional cont
21690 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20 rol.** over the
216a0 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e new database con
216b0 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c nection. The fl
216c0 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 61 ags parameter ca
216d0 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a n take one of.**
216e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
216f0 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74 hree values, opt
21700 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64 ionally combined
21710 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53 with the .** [S
21720 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 QLITE_OPEN_NOMUT
21730 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4f EX] or [SQLITE_O
21740 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 PEN_FULLMUTEX] f
21750 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e lags:.**.** <dl>
21760 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f .** <dt>[SQLITE_
21770 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f OPEN_READONLY]</
21780 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 dt>.** <dd>The d
21790 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 atabase is opene
217a0 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d d in read-only m
217b0 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74 ode. If the dat
217c0 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a abase does not.*
217d0 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c * already exist,
217e0 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 an error is ret
217f0 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a urned.</dd>.**.*
21800 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 * <dt>[SQLITE_OP
21810 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 EN_READWRITE]</d
21820 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 t>.** <dd>The da
21830 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 tabase is opened
21840 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 for reading and
21850 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73 writing if poss
21860 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 ible, or reading
21870 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 .** only if the
21880 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72 file is write pr
21890 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f otected by the o
218a0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e perating system.
218b0 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 In either.** c
218c0 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ase the database
218d0 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 must already ex
218e0 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61 ist, otherwise a
218f0 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
21900 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ned.</dd>.**.**
21910 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e <dt>[SQLITE_OPEN
21920 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 _READWRITE] | [S
21930 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
21940 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 E]</dt>.** <dd>T
21950 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f he database is o
21960 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e pened for readin
21970 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61 g and writing, a
21980 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69 74 nd is creates it
21990 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e if.** it does n
219a0 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ot already exist
219b0 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62 65 . This is the be
219c0 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 havior that is a
219d0 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a lways used for.*
219e0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 * sqlite3_open()
219f0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 and sqlite3_ope
21a00 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c n16().</dd>.** <
21a10 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 /dl>.**.** If th
21a20 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 e 3rd parameter
21a30 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f to sqlite3_open_
21a40 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 v2() is not one
21a50 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e of the.** combin
21a60 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f ations shown abo
21a70 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 ve or one of the
21a80 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 combinations sh
21a90 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e own above combin
21aa0 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b ed.** with the [
21ab0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 SQLITE_OPEN_NOMU
21ac0 54 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f TEX] or [SQLITE_
21ad0 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 OPEN_FULLMUTEX]
21ae0 66 6c 61 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 flags,.** then t
21af0 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 he behavior is u
21b00 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
21b10 49 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f If the [SQLITE_O
21b20 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c 61 PEN_NOMUTEX] fla
21b30 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 g is set, then t
21b40 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
21b50 65 63 74 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20 ection.** opens
21b60 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 in the multi-thr
21b70 65 61 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d ead [threading m
21b80 6f 64 65 5d 20 61 73 20 6c 6f 6e 67 20 61 73 20 ode] as long as
21b90 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 the single-threa
21ba0 64 0a 2a 2a 20 6d 6f 64 65 20 68 61 73 20 6e 6f d.** mode has no
21bb0 74 20 62 65 65 6e 20 73 65 74 20 61 74 20 63 6f t been set at co
21bc0 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74 mpile-time or st
21bd0 61 72 74 2d 74 69 6d 65 2e 20 20 49 66 20 74 68 art-time. If th
21be0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 e.** [SQLITE_OPE
21bf0 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 N_FULLMUTEX] fla
21c00 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 74 68 g is set then th
21c10 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
21c20 63 74 69 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 ction opens.** i
21c30 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 n the serialized
21c40 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
21c50 5d 20 75 6e 6c 65 73 73 20 73 69 6e 67 6c 65 2d ] unless single-
21c60 74 68 72 65 61 64 20 77 61 73 0a 2a 2a 20 70 72 thread was.** pr
21c70 65 76 69 6f 75 73 6c 79 20 73 65 6c 65 63 74 65 eviously selecte
21c80 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d d at compile-tim
21c90 65 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e e or start-time.
21ca0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
21cb0 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f lename is ":memo
21cc0 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 72 69 ry:", then a pri
21cd0 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 20 vate, temporary
21ce0 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
21cf0 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 65 64 se.** is created
21d00 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74 for the connect
21d10 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d 6d 65 ion. This in-me
21d20 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69 mory database wi
21d30 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e 0a 2a ll vanish when.*
21d40 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 * the database c
21d50 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f onnection is clo
21d60 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 65 72 sed. Future ver
21d70 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
21d80 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 75 73 might.** make us
21d90 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 e of additional
21da0 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d 65 special filename
21db0 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74 s that begin wit
21dc0 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61 63 h the ":" charac
21dd0 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 72 65 ter..** It is re
21de0 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 77 commended that w
21df0 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 hen a database f
21e00 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c 79 ilename actually
21e10 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74 68 does begin with
21e20 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 61 63 .** a ":" charac
21e30 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 20 70 ter you should p
21e40 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 6e 61 refix the filena
21e50 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 6e 61 me with a pathna
21e60 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 22 2e me such as.** ".
21e70 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d 62 69 /" to avoid ambi
21e80 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 guity..**.** If
21e90 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 the filename is
21ea0 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c an empty string,
21eb0 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c then a private,
21ec0 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 6f 6e temporary.** on
21ed0 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 20 77 -disk database w
21ee0 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e 20 ill be created.
21ef0 20 54 68 69 73 20 70 72 69 76 61 74 65 20 64 61 This private da
21f00 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 0a 2a tabase will be.*
21f10 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * automatically
21f20 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f 6e 20 deleted as soon
21f30 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 as the database
21f40 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c connection is cl
21f50 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 osed..**.** The
21f60 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 fourth parameter
21f70 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e to sqlite3_open
21f80 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e 61 6d _v2() is the nam
21f90 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c e of the.** [sql
21fa0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
21fb0 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 that defines th
21fc0 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
21fd0 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 em interface tha
21fe0 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 t.** the new dat
21ff0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
22000 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 49 66 should use. If
22010 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 the fourth para
22020 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 4e 55 meter is.** a NU
22030 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 LL pointer then
22040 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c the default [sql
22050 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
22060 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 is used..**.**
22070 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e 64 6f <b>Note to Windo
22080 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54 ws users:</b> T
22090 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 he encoding used
220a0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61 6d for the filenam
220b0 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f 66 e argument.** of
220c0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 sqlite3_open()
220d0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e and sqlite3_open
220e0 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 55 54 _v2() must be UT
220f0 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76 65 F-8, not whateve
22100 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69 73 r.** codepage is
22110 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e currently defin
22120 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20 63 ed. Filenames c
22130 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72 6e ontaining intern
22140 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72 61 ational.** chara
22150 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 63 6f cters must be co
22160 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d 38 nverted to UTF-8
22170 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69 6e prior to passin
22180 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20 73 g them into.** s
22190 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72 qlite3_open() or
221a0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
221b0 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 ()..**.** INVARI
221c0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 ANTS:.**.** {H12
221d0 37 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 701} The [sqlite
221e0 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 3_open()], [sqli
221f0 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 61 te3_open16()], a
22200 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b nd.** [
22210 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
22220 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 63 72 )] interfaces cr
22230 65 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 20 20 eate a new.**
22240 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 [database
22250 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 73 73 connection] ass
22260 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 ociated with.**
22270 20 20 20 20 20 20 20 20 20 74 68 65 20 64 61 74 the dat
22280 61 62 61 73 65 20 66 69 6c 65 20 67 69 76 65 6e abase file given
22290 20 69 6e 20 74 68 65 69 72 20 66 69 72 73 74 20 in their first
222a0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a parameter..**.**
222b0 20 7b 48 31 32 37 30 32 7d 20 54 68 65 20 66 69 {H12702} The fi
222c0 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 lename argument
222d0 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 is interpreted a
222e0 73 20 55 54 46 2d 38 0a 2a 2a 20 20 20 20 20 20 s UTF-8.**
222f0 20 20 20 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 for [sqlite3
22300 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 5b 73 71 _open()] and [sq
22310 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
22320 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 0a 2a and as UTF-16.*
22330 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 68 * in th
22340 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 e native byte or
22350 64 65 72 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 der for [sqlite3
22360 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 0a 2a _open16()]..**.*
22370 2a 20 7b 48 31 32 37 30 33 7d 20 41 20 73 75 63 * {H12703} A suc
22380 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 cessful invocati
22390 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6f on of [sqlite3_o
223a0 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 pen()], [sqlite3
223b0 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 _open16()],.**
223c0 20 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c 69 or [sqli
223d0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 77 te3_open_v2()] w
223e0 72 69 74 65 73 20 61 20 70 6f 69 6e 74 65 72 20 rites a pointer
223f0 74 6f 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20 20 to a new.**
22400 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 [database c
22410 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 6e 74 6f 20 onnection] into
22420 2a 70 70 44 62 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 *ppDb..**.** {H1
22430 32 37 30 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 2704} The [sqlit
22440 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c e3_open()], [sql
22450 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 ite3_open16()],
22460 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 and.**
22470 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 [sqlite3_open_v2
22480 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 72 ()] interfaces r
22490 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b eturn [SQLITE_OK
224a0 5d 20 75 70 6f 6e 20 73 75 63 63 65 73 73 2c 0a ] upon success,.
224b0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 61 ** or a
224c0 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 n appropriate [e
224d0 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66 61 rror code] on fa
224e0 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ilure..**.** {H1
224f0 32 37 30 36 7d 20 54 68 65 20 64 65 66 61 75 6c 2706} The defaul
22500 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 t text encoding
22510 66 6f 72 20 61 20 6e 65 77 20 64 61 74 61 62 61 for a new databa
22520 73 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 se created using
22530 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
22540 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 6f 72 lite3_open()] or
22550 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
22560 32 28 29 5d 20 77 69 6c 6c 20 62 65 20 55 54 46 2()] will be UTF
22570 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 30 -8..**.** {H1270
22580 37 7d 20 54 68 65 20 64 65 66 61 75 6c 74 20 74 7} The default t
22590 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 ext encoding for
225a0 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 a new database
225b0 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a created using.**
225c0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
225d0 65 33 5f 6f 70 65 6e 31 36 28 29 5d 20 77 69 6c e3_open16()] wil
225e0 6c 20 62 65 20 55 54 46 2d 31 36 2e 0a 2a 2a 0a l be UTF-16..**.
225f0 2a 2a 20 7b 48 31 32 37 30 39 7d 20 54 68 65 20 ** {H12709} The
22600 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 46 2c [sqlite3_open(F,
22610 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 69 73 D)] interface is
22620 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 2a equivalent to.*
22630 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
22640 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 2c te3_open_v2(F,D,
22650 47 2c 30 29 5d 20 77 68 65 72 65 20 74 68 65 20 G,0)] where the
22660 47 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a G parameter is.*
22670 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 * [SQLI
22680 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
22690 45 5d 7c 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f E]|[SQLITE_OPEN_
226a0 43 52 45 41 54 45 5d 2e 0a 2a 2a 0a 2a 2a 20 7b CREATE]..**.** {
226b0 48 31 32 37 31 31 7d 20 49 66 20 74 68 65 20 47 H12711} If the G
226c0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
226d0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 qlite3_open_v2(F
226e0 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e 74 61 69 6e ,D,G,V)] contain
226f0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
22700 20 20 62 69 74 20 76 61 6c 75 65 20 5b 53 51 4c bit value [SQL
22710 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
22720 59 5d 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 Y] then the data
22730 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 0a 2a base is opened.*
22740 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 72 * for r
22750 65 61 64 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2a 0a eading only..**.
22760 2a 2a 20 7b 48 31 32 37 31 32 7d 20 49 66 20 74 ** {H12712} If t
22770 68 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 74 he G parameter t
22780 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f o [sqlite3_open_
22790 76 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e v2(F,D,G,V)] con
227a0 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 tains the.**
227b0 20 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20 bit value
227c0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 [SQLITE_OPEN_REA
227d0 44 57 52 49 54 45 5d 20 74 68 65 6e 20 74 68 65 DWRITE] then the
227e0 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 database is ope
227f0 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ned.**
22800 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 reading and writ
22810 69 6e 67 20 69 66 20 70 6f 73 73 69 62 6c 65 2c ing if possible,
22820 20 6f 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 or for reading
22830 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 20 only if the.**
22840 20 20 20 20 20 20 20 20 66 69 6c 65 20 69 73 20 file is
22850 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 64 20 write protected
22860 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 by the operating
22870 20 73 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 7b system..**.** {
22880 48 31 32 37 31 33 7d 20 49 66 20 74 68 65 20 47 H12713} If the G
22890 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
228a0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 qlite3_open_v2(F
228b0 2c 44 2c 47 2c 56 29 5d 20 6f 6d 69 74 73 20 74 ,D,G,V)] omits t
228c0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 he.** b
228d0 69 74 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 it value [SQLITE
228e0 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 20 61 6e _OPEN_CREATE] an
228f0 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64 d the database d
22900 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 oes not.**
22910 20 20 20 20 70 72 65 76 69 6f 75 73 6c 79 20 65 previously e
22920 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72 20 69 xist, an error i
22930 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
22940 2a 20 7b 48 31 32 37 31 34 7d 20 49 66 20 74 68 * {H12714} If th
22950 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 74 6f e G parameter to
22960 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
22970 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e 74 2(F,D,G,V)] cont
22980 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 ains the.**
22990 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20 5b bit value [
229a0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
229b0 54 45 5d 20 61 6e 64 20 74 68 65 20 64 61 74 61 TE] and the data
229c0 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a base does not.**
229d0 20 20 20 20 20 20 20 20 20 20 70 72 65 76 69 6f previo
229e0 75 73 6c 79 20 65 78 69 73 74 2c 20 74 68 65 6e usly exist, then
229f0 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d an attempt is m
22a00 61 64 65 20 74 6f 20 63 72 65 61 74 65 20 61 6e ade to create an
22a10 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e d.** in
22a20 69 74 69 61 6c 69 7a 65 20 74 68 65 20 64 61 74 itialize the dat
22a30 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 abase..**.** {H1
22a40 32 37 31 37 7d 20 49 66 20 74 68 65 20 66 69 6c 2717} If the fil
22a50 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 74 ename argument t
22a60 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 o [sqlite3_open(
22a70 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 )], [sqlite3_ope
22a80 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 n16()],.**
22a90 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f or [sqlite3_
22aa0 6f 70 65 6e 5f 76 32 28 29 5d 20 69 73 20 22 3a open_v2()] is ":
22ab0 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61 memory:", then a
22ac0 6e 20 70 72 69 76 61 74 65 2c 0a 2a 2a 20 20 20 n private,.**
22ad0 20 20 20 20 20 20 20 65 70 68 65 6d 65 72 61 6c ephemeral
22ae0 2c 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 , in-memory data
22af0 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 20 base is created
22b00 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 for the connecti
22b10 6f 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 on..**
22b20 3c 74 6f 64 6f 3e 49 73 20 53 51 4c 49 54 45 5f <todo>Is SQLITE_
22b30 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49 OPEN_CREATE|SQLI
22b40 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
22b50 45 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 20 20 E required.**
22b60 20 20 20 20 20 20 20 69 6e 20 73 71 6c 69 74 65 in sqlite
22b70 33 5f 6f 70 65 6e 5f 76 32 28 29 3f 3c 2f 74 6f 3_open_v2()?</to
22b80 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 31 do>.**.** {H1271
22b90 39 7d 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61 9} If the filena
22ba0 6d 65 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e me is NULL or an
22bb0 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 20 74 empty string, t
22bc0 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 0a 2a hen a private,.*
22bd0 2a 20 20 20 20 20 20 20 20 20 20 65 70 68 65 6d * ephem
22be0 65 72 61 6c 20 6f 6e 2d 64 69 73 6b 20 64 61 74 eral on-disk dat
22bf0 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 72 abase will be cr
22c00 65 61 74 65 64 2e 0a 2a 2a 20 20 20 20 20 20 20 eated..**
22c10 20 20 20 3c 74 6f 64 6f 3e 49 73 20 53 51 4c 49 <todo>Is SQLI
22c20 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 TE_OPEN_CREATE|S
22c30 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
22c40 52 49 54 45 20 72 65 71 75 69 72 65 64 0a 2a 2a RITE required.**
22c50 20 20 20 20 20 20 20 20 20 20 69 6e 20 73 71 6c in sql
22c60 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 3f 3c ite3_open_v2()?<
22c70 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 /todo>.**.** {H1
22c80 32 37 32 31 7d 20 54 68 65 20 5b 64 61 74 61 62 2721} The [datab
22c90 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
22ca0 63 72 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69 created by [sqli
22cb0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 2c te3_open_v2(F,D,
22cc0 47 2c 56 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 G,V)].**
22cd0 20 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 5b will use the [
22ce0 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a sqlite3_vfs] obj
22cf0 65 63 74 20 69 64 65 6e 74 69 66 69 65 64 20 62 ect identified b
22d00 79 20 74 68 65 20 56 20 70 61 72 61 6d 65 74 65 y the V paramete
22d10 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f r,.** o
22d20 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 r the default [s
22d30 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
22d40 63 74 20 69 66 20 56 20 69 73 20 61 20 4e 55 4c ct if V is a NUL
22d50 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a L pointer..**.**
22d60 20 7b 48 31 32 37 32 33 7d 20 54 77 6f 20 5b 64 {H12723} Two [d
22d70 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
22d80 6f 6e 73 5d 20 77 69 6c 6c 20 73 68 61 72 65 20 ons] will share
22d90 61 20 63 6f 6d 6d 6f 6e 20 63 61 63 68 65 20 69 a common cache i
22da0 66 20 62 6f 74 68 20 77 65 72 65 0a 2a 2a 20 20 f both were.**
22db0 20 20 20 20 20 20 20 20 6f 70 65 6e 65 64 20 77 opened w
22dc0 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 46 53 ith the same VFS
22dd0 20 77 68 69 6c 65 20 5b 73 68 61 72 65 64 20 63 while [shared c
22de0 61 63 68 65 20 6d 6f 64 65 5d 20 77 61 73 20 65 ache mode] was e
22df0 6e 61 62 6c 65 64 20 61 6e 64 0a 2a 2a 20 20 20 nabled and.**
22e00 20 20 20 20 20 20 20 69 66 20 62 6f 74 68 20 66 if both f
22e10 69 6c 65 6e 61 6d 65 73 20 63 6f 6d 70 61 72 65 ilenames compare
22e20 20 65 71 75 61 6c 20 75 73 69 6e 67 20 6d 65 6d equal using mem
22e30 63 6d 70 28 29 20 61 66 74 65 72 20 68 61 76 69 cmp() after havi
22e40 6e 67 20 62 65 65 6e 0a 2a 2a 20 20 20 20 20 20 ng been.**
22e50 20 20 20 20 70 72 6f 63 65 73 73 65 64 20 62 79 processed by
22e60 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 the [sqlite3_vf
22e70 73 20 7c 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d s | xFullPathnam
22e80 65 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 e] method of the
22e90 20 56 46 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f VFS..*/.SQLITE_
22ea0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
22eb0 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 open(. const ch
22ec0 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 ar *filename,
22ed0 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 /* Database file
22ee0 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a name (UTF-8) */.
22ef0 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 sqlite3 **ppDb
22f00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
22f10 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 : SQLite db hand
22f20 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f le */.);.SQLITE_
22f30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
22f40 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20 open16(. const
22f50 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 void *filename,
22f60 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 /* Database fi
22f70 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29 20 lename (UTF-16)
22f80 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 */. sqlite3 **p
22f90 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 pDb /*
22fa0 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 OUT: SQLite db h
22fb0 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 andle */.);.SQLI
22fc0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
22fd0 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f e3_open_v2(. co
22fe0 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 nst char *filena
22ff0 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 me, /* Databas
23000 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d e filename (UTF-
23010 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 8) */. sqlite3
23020 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 20 **ppDb,
23030 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 /* OUT: SQLite d
23040 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e b handle */. in
23050 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
23060 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a /* Flags *
23070 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
23080 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 4e zVfs /* N
23090 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c ame of VFS modul
230a0 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a 0a e to use */.);..
230b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
230c0 45 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 20 Error Codes And
230d0 4d 65 73 73 61 67 65 73 20 7b 48 31 32 38 30 30 Messages {H12800
230e0 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a 2a } <S60200>.**.**
230f0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 The sqlite3_err
23100 63 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 65 code() interface
23110 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
23120 65 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f 64 eric [result cod
23130 65 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 e] or.** [extend
23140 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 ed result code]
23150 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 for the most rec
23160 65 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 74 ent failed sqlit
23170 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a 2a e3_* API call.**
23180 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
23190 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e a [database con
231a0 6e 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20 70 nection]. If a p
231b0 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 61 rior API call fa
231c0 69 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20 iled.** but the
231d0 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49 20 most recent API
231e0 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c 20 call succeeded,
231f0 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
23200 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 from.** sqlite3
23210 5f 65 72 72 63 6f 64 65 28 29 20 69 73 20 75 6e _errcode() is un
23220 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73 71 defined. The sq
23230 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 lite3_extended_e
23240 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 65 rrcode().** inte
23250 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61 6d rface is the sam
23260 65 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 e except that it
23270 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 always returns
23280 74 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 the .** [extende
23290 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 65 d result code] e
232a0 76 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64 65 ven when extende
232b0 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 d result codes a
232c0 72 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0a re.** disabled..
232d0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
232e0 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 3_errmsg() and s
232f0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 qlite3_errmsg16(
23300 29 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 ) return English
23310 2d 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 78 -language.** tex
23320 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 t that describes
23330 20 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20 65 the error, as e
23340 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 ither UTF-8 or U
23350 54 46 2d 31 36 20 72 65 73 70 65 63 74 69 76 65 TF-16 respective
23360 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f ly..** Memory to
23370 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 hold the error
23380 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 message string i
23390 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e s managed intern
233a0 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 70 ally..** The app
233b0 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f lication does no
233c0 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 t need to worry
233d0 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 68 about freeing th
233e0 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f 77 e result..** How
233f0 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20 ever, the error
23400 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 20 string might be
23410 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 64 overwritten or d
23420 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a eallocated by.**
23430 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
23440 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 s to other SQLit
23450 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63 e interface func
23460 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 tions..**.** Whe
23470 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 n the serialized
23480 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
23490 5d 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74 20 ] is in use, it
234a0 6d 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a 20 might be the.**
234b0 63 61 73 65 20 74 68 61 74 20 61 20 73 65 63 6f case that a seco
234c0 6e 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 nd error occurs
234d0 6f 6e 20 61 20 73 65 70 61 72 61 74 65 20 74 68 on a separate th
234e0 72 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e 0a read in between.
234f0 2a 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20 74 ** the time of t
23500 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 61 he first error a
23510 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 nd the call to t
23520 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 2e hese interfaces.
23530 0a 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68 61 .** When that ha
23540 70 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f 6e ppens, the secon
23550 64 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 d error will be
23560 72 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20 74 reported since t
23570 68 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 hese.** interfac
23580 65 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 74 es always report
23590 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
235a0 20 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 6f result. To avo
235b0 69 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 68 id.** this, each
235c0 20 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 61 thread can obta
235d0 69 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 65 in exclusive use
235e0 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 of the [databas
235f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a e connection] D.
23600 2a 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 5b ** by invoking [
23610 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
23620 74 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 ter]([sqlite3_db
23630 5f 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66 6f _mutex](D)) befo
23640 72 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 re beginning.**
23650 74 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e 76 to use D and inv
23660 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d oking [sqlite3_m
23670 75 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71 6c utex_leave]([sql
23680 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 ite3_db_mutex](D
23690 29 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c 20 )) after.** all
236a0 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e 74 calls to the int
236b0 65 72 66 61 63 65 73 20 6c 69 73 74 65 64 20 68 erfaces listed h
236c0 65 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74 65 ere are complete
236d0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 d..**.** If an i
236e0 6e 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 77 nterface fails w
236f0 69 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 ith SQLITE_MISUS
23700 45 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 E, that means th
23710 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 77 e interface.** w
23720 61 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f 72 as invoked incor
23730 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 61 70 rectly by the ap
23740 70 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20 74 plication. In t
23750 68 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a 2a hat case, the.**
23760 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 error code and
23770 6d 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20 6d message may or m
23780 61 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a ay not be set..*
23790 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
237a0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 31 7d 20 .**.** {H12801}
237b0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 The [sqlite3_err
237c0 63 6f 64 65 28 44 29 5d 20 69 6e 74 65 72 66 61 code(D)] interfa
237d0 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e ce returns the n
237e0 75 6d 65 72 69 63 0a 2a 2a 20 20 20 20 20 20 20 umeric.**
237f0 20 20 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 5d [result code]
23800 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 72 65 or [extended re
23810 73 75 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 74 sult code] for t
23820 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
23830 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 61 69 .** fai
23840 6c 65 64 20 69 6e 74 65 72 66 61 63 65 20 63 61 led interface ca
23850 6c 6c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ll associated wi
23860 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 th the [database
23870 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a connection] D..
23880 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 32 7d 20 54 **.** {H12802} T
23890 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 he [sqlite3_exte
238a0 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 44 29 5d nded_errcode(D)]
238b0 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
238c0 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63 0a 2a ns the numeric.*
238d0 2a 20 20 20 20 20 20 20 20 20 20 5b 65 78 74 65 * [exte
238e0 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 nded result code
238f0 5d 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 ] for the most r
23900 65 63 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20 ecently.**
23910 20 20 20 20 66 61 69 6c 65 64 20 69 6e 74 65 72 failed inter
23920 66 61 63 65 20 63 61 6c 6c 20 61 73 73 6f 63 69 face call associ
23930 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 ated with the [d
23940 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
23950 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 on] D..**.** {H1
23960 32 38 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 2803} The [sqlit
23970 65 33 5f 65 72 72 6d 73 67 28 44 29 5d 20 61 6e e3_errmsg(D)] an
23980 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 d [sqlite3_errms
23990 67 31 36 28 44 29 5d 0a 2a 2a 20 20 20 20 20 20 g16(D)].**
239a0 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 72 interfaces r
239b0 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 eturn English-la
239c0 6e 67 75 61 67 65 20 74 65 78 74 20 74 68 61 74 nguage text that
239d0 20 64 65 73 63 72 69 62 65 73 0a 2a 2a 20 20 20 describes.**
239e0 20 20 20 20 20 20 20 74 68 65 20 65 72 72 6f 72 the error
239f0 20 69 6e 20 74 68 65 20 6d 6f 73 74 6c 79 20 72 in the mostly r
23a00 65 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 69 ecently failed i
23a10 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 2c 0a 2a nterface call,.*
23a20 2a 20 20 20 20 20 20 20 20 20 20 65 6e 63 6f 64 * encod
23a30 65 64 20 61 73 20 65 69 74 68 65 72 20 55 54 46 ed as either UTF
23a40 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65 73 -8 or UTF-16 res
23a50 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a pectively..**.**
23a60 20 7b 48 31 32 38 30 37 7d 20 54 68 65 20 73 74 {H12807} The st
23a70 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 rings returned b
23a80 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 y [sqlite3_errms
23a90 67 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 g()] and [sqlite
23aa0 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 0a 2a 2a 3_errmsg16()].**
23ab0 20 20 20 20 20 20 20 20 20 20 61 72 65 20 76 61 are va
23ac0 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 lid until the ne
23ad0 78 74 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 xt SQLite interf
23ae0 61 63 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 ace call..**.**
23af0 7b 48 31 32 38 30 38 7d 20 43 61 6c 6c 73 20 74 {H12808} Calls t
23b00 6f 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 74 o API routines t
23b10 68 61 74 20 64 6f 20 6e 6f 74 20 72 65 74 75 72 hat do not retur
23b20 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a n an error code.
23b30 2a 2a 20 20 20 20 20 20 20 20 20 20 28 65 78 61 ** (exa
23b40 6d 70 6c 65 3a 20 5b 73 71 6c 69 74 65 33 5f 64 mple: [sqlite3_d
23b50 61 74 61 5f 63 6f 75 6e 74 28 29 5d 29 20 64 6f ata_count()]) do
23b60 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 not.**
23b70 20 63 68 61 6e 67 65 20 74 68 65 20 65 72 72 6f change the erro
23b80 72 20 63 6f 64 65 20 6f 72 20 6d 65 73 73 61 67 r code or messag
23b90 65 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a e returned by.**
23ba0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
23bb0 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b e3_errcode()], [
23bc0 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
23bd0 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20 _errcode()],.**
23be0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
23bf0 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 6f 72 20 3_errmsg()], or
23c00 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 [sqlite3_errmsg1
23c10 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 6()]..**.** {H12
23c20 38 30 39 7d 20 49 6e 74 65 72 66 61 63 65 73 20 809} Interfaces
23c30 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 73 73 that are not ass
23c40 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73 ociated with a s
23c50 70 65 63 69 66 69 63 0a 2a 2a 20 20 20 20 20 20 pecific.**
23c60 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f [database co
23c70 6e 6e 65 63 74 69 6f 6e 5d 20 28 65 78 61 6d 70 nnection] (examp
23c80 6c 65 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 les:.**
23c90 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 [sqlite3_mprint
23ca0 66 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 f()] or [sqlite3
23cb0 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 _enable_shared_c
23cc0 61 63 68 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20 ache()].**
23cd0 20 20 20 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 do not chang
23ce0 65 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 e the values ret
23cf0 75 72 6e 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 urned by.**
23d00 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 [sqlite3_er
23d10 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69 74 rcode()], [sqlit
23d20 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 e3_extended_errc
23d30 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 ode()],.**
23d40 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 [sqlite3_err
23d50 6d 73 67 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69 msg()], or [sqli
23d60 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 2e te3_errmsg16()].
23d70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
23d80 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f nt sqlite3_errco
23d90 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b de(sqlite3 *db);
23da0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
23db0 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
23dc0 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 _errcode(sqlite3
23dd0 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 *db);.SQLITE_AP
23de0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
23df0 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c lite3_errmsg(sql
23e00 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 ite3*);.SQLITE_A
23e10 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
23e20 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 qlite3_errmsg16(
23e30 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a sqlite3*);../*.*
23e40 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 * CAPI3REF: SQL
23e50 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 Statement Object
23e60 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 30 31 {H13000} <H1301
23e70 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
23e80 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d {prepared statem
23e90 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 20 73 ent} {prepared s
23ea0 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a 2a tatements}.**.**
23eb0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
23ec0 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 this object repr
23ed0 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 esents a single
23ee0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a SQL statement..*
23ef0 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 * This object is
23f00 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77 6e variously known
23f10 20 61 73 20 61 20 22 70 72 65 70 61 72 65 64 20 as a "prepared
23f20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61 0a statement" or a.
23f30 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51 4c ** "compiled SQL
23f40 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 73 statement" or s
23f50 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61 74 imply as a "stat
23f60 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 ement"..**.** Th
23f70 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 61 74 e life of a stat
23f80 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 6f 65 ement object goe
23f90 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65 s something like
23fa0 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c this:.**.** <ol
23fb0 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 74 65 >.** <li> Create
23fc0 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 6e the object usin
23fd0 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 g [sqlite3_prepa
23fe0 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 72 65 re_v2()] or a re
23ff0 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 66 75 lated.** fu
24000 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e 20 nction..** <li>
24010 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 5b Bind values to [
24020 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 5d host parameters]
24030 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 using the sqlit
24040 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20 20 e3_bind_*().**
24050 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 2e 0a interfaces..
24060 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 65 20 ** <li> Run the
24070 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b SQL by calling [
24080 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
24090 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 one or more time
240a0 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 65 74 s..** <li> Reset
240b0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 75 the statement u
240c0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 sing [sqlite3_re
240d0 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f 20 62 set()] then go b
240e0 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f 20 73 ack.** to s
240f0 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 73 20 tep 2. Do this
24100 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 69 6d zero or more tim
24110 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 73 74 es..** <li> Dest
24120 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 20 75 roy the object u
24130 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 sing [sqlite3_fi
24140 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 3c 2f nalize()]..** </
24150 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 20 ol>.**.** Refer
24160 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e to documentation
24170 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 6d on individual m
24180 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f 72 ethods above for
24190 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 additional.** i
241a0 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 nformation..*/.t
241b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
241c0 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c 69 74 lite3_stmt sqlit
241d0 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a 20 e3_stmt;../*.**
241e0 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 74 69 CAPI3REF: Run-ti
241f0 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 37 36 me Limits {H1276
24200 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 0a 2a 0} <S20600>.**.*
24210 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
24220 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a 65 allows the size
24230 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f 6e 73 of various cons
24240 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c 69 6d tructs to be lim
24250 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f 6e ited.** on a con
24260 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e 65 nection by conne
24270 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 54 68 ction basis. Th
24280 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
24290 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61 74 r is the.** [dat
242a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
242b0 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69 73 ] whose limit is
242c0 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 71 75 to be set or qu
242d0 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73 eried. The.** s
242e0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
242f0 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b 6c is one of the [l
24300 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 5d imit categories]
24310 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 0a 2a that define a.*
24320 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73 74 * class of const
24330 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 7a 65 ructs to be size
24340 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 20 74 limited. The t
24350 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 hird parameter i
24360 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d s the.** new lim
24370 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 73 it for that cons
24380 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 6e 63 truct. The func
24390 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
243a0 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a old limit..**.*
243b0 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c 69 6d * If the new lim
243c0 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76 65 it is a negative
243d0 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69 6d number, the lim
243e0 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e it is unchanged.
243f0 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d 69 .** For the limi
24400 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 53 51 t category of SQ
24410 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20 74 LITE_LIMIT_XYZ t
24420 68 65 72 65 20 69 73 20 61 20 68 61 72 64 20 75 here is a hard u
24430 70 70 65 72 0a 2a 2a 20 62 6f 75 6e 64 20 73 65 pper.** bound se
24440 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d 74 t by a compile-t
24450 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73 73 ime C preprocess
24460 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20 53 or macro named S
24470 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 2e 0a 2a QLITE_MAX_XYZ..*
24480 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22 * (The "_LIMIT_"
24490 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20 in the name is
244a0 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 58 changed to "_MAX
244b0 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 73 _".).** Attempts
244c0 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20 6c to increase a l
244d0 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20 68 imit above its h
244e0 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20 ard upper bound
244f0 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20 are.** silently
24500 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65 truncated to the
24510 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 hard upper limi
24520 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d t..**.** Run tim
24530 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e 74 e limits are int
24540 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e ended for use in
24550 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 applications th
24560 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f 74 at manage.** bot
24570 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 65 h their own inte
24580 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61 6e rnal database an
24590 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65 73 d also databases
245a0 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72 6f that are contro
245b0 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 75 lled.** by untru
245c0 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f sted external so
245d0 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70 urces. An examp
245e0 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d le application m
245f0 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 62 ight be a.** web
24600 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 73 browser that has
24610 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 73 its own databas
24620 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 68 es for storing h
24630 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73 65 istory and.** se
24640 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65 73 parate databases
24650 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 4a controlled by J
24660 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69 63 avaScript applic
24670 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64 65 ations downloade
24680 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e 74 d.** off the Int
24690 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74 65 ernet. The inte
246a0 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20 63 rnal databases c
246b0 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65 0a an be given the.
246c0 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75 6c ** large, defaul
246d0 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61 62 t limits. Datab
246e0 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79 20 ases managed by
246f0 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 external sources
24700 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65 6e can.** be given
24710 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c 69 much smaller li
24720 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 74 6f mits designed to
24730 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69 61 prevent a denia
24740 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a 20 l of service.**
24750 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f 70 attack. Develop
24760 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 77 ers might also w
24770 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 5b ant to use the [
24780 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
24790 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e 74 orizer()].** int
247a0 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 68 65 erface to furthe
247b0 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75 73 r control untrus
247c0 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73 69 ted SQL. The si
247d0 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
247e0 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 se.** created by
247f0 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73 63 an untrusted sc
24800 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e 74 ript can be cont
24810 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 0a ained using the.
24820 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75 ** [max_page_cou
24830 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a 2a nt] [PRAGMA]..**
24840 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d 65 .** New run-time
24850 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 limit categorie
24860 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 s may be added i
24870 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
24880 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 s..**.** INVARIA
24890 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 NTS:.**.** {H127
248a0 36 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 62} A successful
248b0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
248c0 33 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20 3_limit(D,C,V)]
248d0 77 68 65 72 65 20 56 20 69 73 0a 2a 2a 20 20 20 where V is.**
248e0 20 20 20 20 20 20 20 70 6f 73 69 74 69 76 65 20 positive
248f0 63 68 61 6e 67 65 73 20 74 68 65 20 6c 69 6d 69 changes the limi
24900 74 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 t on the size of
24910 20 63 6f 6e 73 74 72 75 63 74 20 43 20 69 6e 20 construct C in
24920 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
24930 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
24940 74 69 6f 6e 5d 20 44 20 74 6f 20 74 68 65 20 6c tion] D to the l
24950 65 73 73 65 72 20 6f 66 20 56 20 61 6e 64 20 74 esser of V and t
24960 68 65 20 68 61 72 64 20 75 70 70 65 72 0a 2a 2a he hard upper.**
24970 20 20 20 20 20 20 20 20 20 20 62 6f 75 6e 64 20 bound
24980 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 43 on the size of C
24990 20 74 68 61 74 20 69 73 20 73 65 74 20 61 74 20 that is set at
249a0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2a compile-time..**
249b0 0a 2a 2a 20 7b 48 31 32 37 36 36 7d 20 41 20 73 .** {H12766} A s
249c0 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
249d0 6f 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 o [sqlite3_limit
249e0 28 44 2c 43 2c 56 29 5d 20 77 68 65 72 65 20 56 (D,C,V)] where V
249f0 20 69 73 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 is negative.**
24a00 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73 20 leaves
24a10 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 the state of the
24a20 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
24a30 63 74 69 6f 6e 5d 20 44 20 75 6e 63 68 61 6e 67 ction] D unchang
24a40 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 36 ed..**.** {H1276
24a50 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 9} A successful
24a60 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
24a70 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20 72 _limit(D,C,V)] r
24a80 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 20 20 eturns the.**
24a90 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20 value of
24aa0 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 the limit on the
24ab0 20 73 69 7a 65 20 6f 66 20 63 6f 6e 73 74 72 75 size of constru
24ac0 63 74 20 43 20 69 6e 20 74 68 65 0a 2a 2a 20 20 ct C in the.**
24ad0 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 [databas
24ae0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 e connection] D
24af0 61 73 20 69 74 20 77 61 73 20 70 72 69 6f 72 20 as it was prior
24b00 74 6f 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2f 0a to the call..*/.
24b10 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
24b20 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71 6c qlite3_limit(sql
24b30 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20 69 ite3*, int id, i
24b40 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a 0a nt newVal);../*.
24b50 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e ** CAPI3REF: Run
24b60 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74 65 -Time Limit Cate
24b70 67 6f 72 69 65 73 20 7b 48 31 32 37 39 30 7d 20 gories {H12790}
24b80 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 45 59 57 <H12760>.** KEYW
24b90 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 63 61 74 ORDS: {limit cat
24ba0 65 67 6f 72 79 7d 20 7b 6c 69 6d 69 74 20 63 61 egory} {limit ca
24bb0 74 65 67 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a 20 tegories}.**.**
24bc0 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 These constants
24bd0 64 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 61 define various a
24be0 73 70 65 63 74 73 20 6f 66 20 61 20 5b 64 61 74 spects of a [dat
24bf0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
24c00 5d 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 ].** that can be
24c10 20 6c 69 6d 69 74 65 64 20 69 6e 20 73 69 7a 65 limited in size
24c20 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 by calls to [sq
24c30 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 2e 0a lite3_limit()]..
24c40 2a 2a 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 ** The meanings
24c50 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c of the various l
24c60 69 6d 69 74 73 20 61 72 65 20 61 73 20 66 6f 6c imits are as fol
24c70 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e lows:.**.** <dl>
24c80 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
24c90 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e IMIT_LENGTH</dt>
24ca0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
24cb0 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 mum size of any
24cc0 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f string or BLOB o
24cd0 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e r table row.<dd>
24ce0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
24cf0 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 E_LIMIT_SQL_LENG
24d00 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 TH</dt>.** <dd>T
24d10 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 he maximum lengt
24d20 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 h of an SQL stat
24d30 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a ement.</dd>.**.*
24d40 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d * <dt>SQLITE_LIM
24d50 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a IT_COLUMN</dt>.*
24d60 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
24d70 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 m number of colu
24d80 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64 mns in a table d
24d90 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20 efinition or in
24da0 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 the.** result se
24db0 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 6f 72 t of a SELECT or
24dc0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d the maximum num
24dd0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
24de0 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 72 n an index.** or
24df0 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 in an ORDER BY
24e00 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 or GROUP BY clau
24e10 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c se.</dd>.**.** <
24e20 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f dt>SQLITE_LIMIT_
24e30 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a EXPR_DEPTH</dt>.
24e40 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
24e50 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 um depth of the
24e60 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61 6e parse tree on an
24e70 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f 64 y expression.</d
24e80 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
24e90 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 ITE_LIMIT_COMPOU
24ea0 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a ND_SELECT</dt>.*
24eb0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
24ec0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d m number of term
24ed0 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 s in a compound
24ee0 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
24ef0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
24f00 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 >SQLITE_LIMIT_VD
24f10 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 BE_OP</dt>.** <d
24f20 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 d>The maximum nu
24f30 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74 mber of instruct
24f40 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75 61 ions in a virtua
24f50 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 61 l machine progra
24f60 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d 70 m.** used to imp
24f70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 74 lement an SQL st
24f80 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a atement.</dd>.**
24f90 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
24fa0 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 IMIT_FUNCTION_AR
24fb0 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 G</dt>.** <dd>Th
24fc0 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
24fd0 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e of arguments on
24fe0 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64 a function.</dd
24ff0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
25000 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 TE_LIMIT_ATTACHE
25010 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
25020 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
25030 20 6f 66 20 61 74 74 61 63 68 65 64 20 64 61 74 of attached dat
25040 61 62 61 73 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a abases.</dd>.**.
25050 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 ** <dt>SQLITE_LI
25060 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e MIT_LIKE_PATTERN
25070 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 _LENGTH</dt>.**
25080 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 <dd>The maximum
25090 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70 61 length of the pa
250a0 74 74 65 72 6e 20 61 72 67 75 6d 65 6e 74 20 74 ttern argument t
250b0 6f 20 74 68 65 20 4c 49 4b 45 20 6f 72 0a 2a 2a o the LIKE or.**
250c0 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e GLOB operators.
250d0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
250e0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 SQLITE_LIMIT_VAR
250f0 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74 IABLE_NUMBER</dt
25100 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 >.** <dd>The max
25110 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76 imum number of v
25120 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 53 ariables in an S
25130 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 QL statement tha
25140 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e t can.** be boun
25150 64 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e d.</dd>.** </dl>
25160 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
25170 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 20 TE_LIMIT_LENGTH
25180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
25190 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0.#define SQL
251a0 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 ITE_LIMIT_SQL_LE
251b0 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 20 20 NGTH
251c0 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 1.#define SQ
251d0 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d LITE_LIMIT_COLUM
251e0 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
251f0 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 2.#define S
25200 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 QLITE_LIMIT_EXPR
25210 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 20 20 _DEPTH
25220 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 3.#define
25230 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d SQLITE_LIMIT_COM
25240 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20 20 20 POUND_SELECT
25250 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 4.#define
25260 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 SQLITE_LIMIT_VD
25270 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20 20 20 BE_OP
25280 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 5.#defin
25290 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 e SQLITE_LIMIT_F
252a0 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 20 20 UNCTION_ARG
252b0 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6.#defi
252c0 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
252d0 41 54 54 41 43 48 45 44 20 20 20 20 20 20 20 20 ATTACHED
252e0 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 7.#def
252f0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
25300 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 _LIKE_PATTERN_LE
25310 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23 64 65 NGTH 8.#de
25320 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
25330 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 T_VARIABLE_NUMBE
25340 52 20 20 20 20 20 20 20 20 20 20 20 39 0a 0a 2f R 9../
25350 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 *.** CAPI3REF: C
25360 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20 ompiling An SQL
25370 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 30 31 Statement {H1301
25380 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 4b 0} <S10000>.** K
25390 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73 74 EYWORDS: {SQL st
253a0 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72 atement compiler
253b0 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 75 }.**.** To execu
253c0 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2c te an SQL query,
253d0 20 69 74 20 6d 75 73 74 20 66 69 72 73 74 20 62 it must first b
253e0 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20 e compiled into
253f0 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 70 a byte-code.** p
25400 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e 65 rogram using one
25410 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e of these routin
25420 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 es..**.** The fi
25430 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 22 64 rst argument, "d
25440 62 22 2c 20 69 73 20 61 20 5b 64 61 74 61 62 61 b", is a [databa
25450 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f se connection] o
25460 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a 2a btained from a.*
25470 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 * prior call to
25480 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
25490 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f , [sqlite3_open_
254a0 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 v2()] or [sqlite
254b0 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 0a 3_open16()]..**.
254c0 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 ** The second ar
254d0 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20 gument, "zSql",
254e0 69 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 is the statement
254f0 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c to be compiled,
25500 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65 encoded.** as e
25510 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 ither UTF-8 or U
25520 54 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c 69 TF-16. The sqli
25530 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 61 6e te3_prepare() an
25540 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 d sqlite3_prepar
25550 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66 e_v2().** interf
25560 61 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c 20 aces use UTF-8,
25570 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 and sqlite3_prep
25580 61 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c 69 are16() and sqli
25590 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 te3_prepare16_v2
255a0 28 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31 36 ().** use UTF-16
255b0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e ..**.** If the n
255c0 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 73 Byte argument is
255d0 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c less than zero,
255e0 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65 then zSql is re
255f0 61 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20 ad up to the.**
25600 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 first zero termi
25610 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65 20 nator. If nByte
25620 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c is non-negative,
25630 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 then it is the
25640 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65 maximum.** numbe
25650 72 20 6f 66 20 20 62 79 74 65 73 20 72 65 61 64 r of bytes read
25660 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65 from zSql. Whe
25670 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e n nByte is non-n
25680 65 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20 egative, the.**
25690 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64 73 zSql string ends
256a0 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20 66 at either the f
256b0 69 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20 27 irst '\000' or '
256c0 5c 75 30 30 30 30 27 20 63 68 61 72 61 63 74 65 \u0000' characte
256d0 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74 r or.** the nByt
256e0 65 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63 68 e-th byte, which
256f0 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 ever comes first
25700 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 . If the caller
25710 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 68 knows.** that th
25720 65 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 6e e supplied strin
25730 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 g is nul-termina
25740 74 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 ted, then there
25750 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65 is a small.** pe
25760 72 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e 74 rformance advant
25770 61 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65 64 age to be gained
25780 20 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20 6e by passing an n
25790 42 79 74 65 20 70 61 72 61 6d 65 74 65 72 20 74 Byte parameter t
257a0 68 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 hat.** is equal
257b0 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
257c0 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 69 6e bytes in the in
257d0 70 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e put string <i>in
257e0 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74 cluding</i>.** t
257f0 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f he nul-terminato
25800 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 2a r bytes..**.** *
25810 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 pzTail is made t
25820 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 o point to the f
25830 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 irst byte past t
25840 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a he end of the.**
25850 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 65 first SQL state
25860 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54 ment in zSql. T
25870 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e hese routines on
25880 6c 79 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 66 ly compile the f
25890 69 72 73 74 0a 2a 2a 20 73 74 61 74 65 6d 65 6e irst.** statemen
258a0 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a 70 t in zSql, so *p
258b0 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70 6f zTail is left po
258c0 69 6e 74 69 6e 67 20 74 6f 20 77 68 61 74 20 72 inting to what r
258d0 65 6d 61 69 6e 73 0a 2a 2a 20 75 6e 63 6f 6d 70 emains.** uncomp
258e0 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53 iled..**.** *ppS
258f0 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e tmt is left poin
25900 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 6c ting to a compil
25910 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 ed [prepared sta
25920 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 6e tement] that can
25930 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 be.** executed
25940 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 using [sqlite3_s
25950 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65 72 tep()]. If ther
25960 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a e is an error, *
25970 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a 2a ppStmt is set.**
25980 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68 to NULL. If th
25990 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e e input text con
259a0 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66 tains no SQL (if
259b0 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e the input is an
259c0 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67 empty.** string
259d0 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74 or a comment) t
259e0 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73 hen *ppStmt is s
259f0 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 7b et to NULL..** {
25a00 41 31 33 30 31 38 7d 20 54 68 65 20 63 61 6c 6c A13018} The call
25a10 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69 73 ing procedure is
25a20 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 responsible for
25a30 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f deleting the co
25a40 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 73 74 mpiled.** SQL st
25a50 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b 73 atement using [s
25a60 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
25a70 29 5d 20 61 66 74 65 72 20 69 74 20 68 61 73 20 )] after it has
25a80 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 finished with it
25a90 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 ..**.** On succe
25aa0 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 ss, [SQLITE_OK]
25ab0 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 74 68 is returned, oth
25ac0 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f 72 erwise an [error
25ad0 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e code] is return
25ae0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ed..**.** The sq
25af0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
25b00 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 () and sqlite3_p
25b10 72 65 70 61 72 65 31 36 5f 76 32 28 29 20 69 6e repare16_v2() in
25b20 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a 2a 20 terfaces are.**
25b30 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 recommended for
25b40 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61 6d 73 all new programs
25b50 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65 72 20 . The two older
25b60 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 72 interfaces are r
25b70 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 62 etained.** for b
25b80 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
25b90 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68 65 69 bility, but thei
25ba0 72 20 75 73 65 20 69 73 20 64 69 73 63 6f 75 72 r use is discour
25bb0 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 aged..** In the
25bc0 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 73 2c "v2" interfaces,
25bd0 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 the prepared st
25be0 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 20 atement.** that
25bf0 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 68 65 is returned (the
25c00 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 [sqlite3_stmt]
25c10 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 6e 73 object) contains
25c20 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a a copy of the.*
25c30 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 * original SQL t
25c40 65 78 74 2e 20 54 68 69 73 20 63 61 75 73 65 73 ext. This causes
25c50 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 the [sqlite3_st
25c60 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 ep()] interface
25c70 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 20 64 to.** behave a d
25c80 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74 77 ifferently in tw
25c90 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f o ways:.**.** <o
25ca0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66 l>.** <li>.** If
25cb0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
25cc0 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 69 6e hema changes, in
25cd0 73 74 65 61 64 20 6f 66 20 72 65 74 75 72 6e 69 stead of returni
25ce0 6e 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d ng [SQLITE_SCHEM
25cf0 41 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61 A] as it.** alwa
25d00 79 73 20 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b ys used to do, [
25d10 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
25d20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c will automatical
25d30 6c 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 68 65 ly recompile the
25d40 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e SQL.** statemen
25d50 74 20 61 6e 64 20 74 72 79 20 74 6f 20 72 75 6e t and try to run
25d60 20 69 74 20 61 67 61 69 6e 2e 20 20 49 66 20 74 it again. If t
25d70 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 he schema has ch
25d80 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20 77 61 anged in.** a wa
25d90 79 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 y that makes the
25da0 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f statement no lo
25db0 6e 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c nger valid, [sql
25dc0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c ite3_step()] wil
25dd0 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 l still.** retur
25de0 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 n [SQLITE_SCHEMA
25df0 5d 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 20 74 ]. But unlike t
25e00 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 69 he legacy behavi
25e10 6f 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 or, [SQLITE_SCHE
25e20 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 20 MA] is.** now a
25e30 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 43 61 fatal error. Ca
25e40 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 lling [sqlite3_p
25e50 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 67 61 repare_v2()] aga
25e60 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65 in will not make
25e70 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 67 6f the.** error go
25e80 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20 75 73 away. Note: us
25e90 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 e [sqlite3_errms
25ea0 67 28 29 5d 20 74 6f 20 66 69 6e 64 20 74 68 65 g()] to find the
25eb0 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74 68 65 20 text.** of the
25ec0 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 74 68 parsing error th
25ed0 61 74 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e at results in an
25ee0 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d [SQLITE_SCHEMA]
25ef0 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69 return..** </li
25f00 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 >.**.** <li>.**
25f10 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 When an error oc
25f20 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33 5f 73 curs, [sqlite3_s
25f30 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 tep()] will retu
25f40 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 rn one of the de
25f50 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f 72 tailed.** [error
25f60 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78 74 65 codes] or [exte
25f70 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73 nded error codes
25f80 5d 2e 20 20 54 68 65 20 6c 65 67 61 63 79 20 62 ]. The legacy b
25f90 65 68 61 76 69 6f 72 20 77 61 73 20 74 68 61 74 ehavior was that
25fa0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 .** [sqlite3_ste
25fb0 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 p()] would only
25fc0 72 65 74 75 72 6e 20 61 20 67 65 6e 65 72 69 63 return a generic
25fd0 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
25fe0 72 65 73 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61 result code.** a
25ff0 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20 68 61 76 nd you would hav
26000 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65 63 6f e to make a seco
26010 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 nd call to [sqli
26020 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69 6e 20 te3_reset()] in
26030 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 66 69 6e 64 order.** to find
26040 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
26050 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 72 6f cause of the pro
26060 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65 20 22 blem. With the "
26070 76 32 22 20 70 72 65 70 61 72 65 0a 2a 2a 20 69 v2" prepare.** i
26080 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 20 75 nterfaces, the u
26090 6e 64 65 72 6c 79 69 6e 67 20 72 65 61 73 6f 6e nderlying reason
260a0 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20 69 for the error i
260b0 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 s returned immed
260c0 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e iately..** </li>
260d0 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 .** </ol>.**.**
260e0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
260f0 2a 20 7b 48 31 33 30 31 31 7d 20 54 68 65 20 5b * {H13011} The [
26100 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
26110 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 61 6e db,zSql,...)] an
26120 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 d.** [s
26130 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
26140 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 2(db,zSql,...)]
26150 69 6e 74 65 72 66 61 63 65 73 20 69 6e 74 65 72 interfaces inter
26160 70 72 65 74 20 74 68 65 0a 2a 2a 20 20 20 20 20 pret the.**
26170 20 20 20 20 20 74 65 78 74 20 69 6e 20 74 68 65 text in the
26180 69 72 20 7a 53 71 6c 20 70 61 72 61 6d 65 74 65 ir zSql paramete
26190 72 20 61 73 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a r as UTF-8..**.*
261a0 2a 20 7b 48 31 33 30 31 32 7d 20 54 68 65 20 5b * {H13012} The [
261b0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
261c0 36 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 6(db,zSql,...)]
261d0 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 and.**
261e0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
261f0 31 36 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e 16_v2(db,zSql,..
26200 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 69 .)] interfaces i
26210 6e 74 65 72 70 72 65 74 20 74 68 65 0a 2a 2a 20 nterpret the.**
26220 20 20 20 20 20 20 20 20 20 74 65 78 74 20 69 6e text in
26230 20 74 68 65 69 72 20 7a 53 71 6c 20 70 61 72 61 their zSql para
26240 6d 65 74 65 72 20 61 73 20 55 54 46 2d 31 36 20 meter as UTF-16
26250 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 in the native by
26260 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 te order..**.**
26270 7b 48 31 33 30 31 33 7d 20 49 66 20 74 68 65 20 {H13013} If the
26280 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 74 nByte argument t
26290 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 o [sqlite3_prepa
262a0 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 6e 42 re_v2(db,zSql,nB
262b0 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 yte,...)].**
262c0 20 20 20 20 20 20 61 6e 64 20 69 74 73 20 76 61 and its va
262d0 72 69 61 6e 74 73 20 69 73 20 6c 65 73 73 20 74 riants is less t
262e0 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 20 53 51 han zero, the SQ
262f0 4c 20 74 65 78 74 20 69 73 0a 2a 2a 20 20 20 20 L text is.**
26300 20 20 20 20 20 20 72 65 61 64 20 66 72 6f 6d 20 read from
26310 7a 53 71 6c 20 69 73 20 72 65 61 64 20 75 70 20 zSql is read up
26320 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 to the first zer
26330 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a o terminator..**
26340 0a 2a 2a 20 7b 48 31 33 30 31 34 7d 20 49 66 20 .** {H13014} If
26350 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65 the nByte argume
26360 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 nt to [sqlite3_p
26370 72 65 70 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 repare_v2(db,zSq
26380 6c 2c 6e 42 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a l,nByte,...)].**
26390 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 69 74 and it
263a0 73 20 76 61 72 69 61 6e 74 73 20 69 73 20 6e 6f s variants is no
263b0 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e n-negative, then
263c0 20 61 74 20 6d 6f 73 74 20 6e 42 79 74 65 73 20 at most nBytes
263d0 62 79 74 65 73 20 6f 66 0a 2a 2a 20 20 20 20 20 bytes of.**
263e0 20 20 20 20 20 53 51 4c 20 74 65 78 74 20 69 73 SQL text is
263f0 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e read from zSql.
26400 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 30 31 35 7d 20 .**.** {H13015}
26410 49 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 In [sqlite3_prep
26420 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 4e are_v2(db,zSql,N
26430 2c 50 2c 70 7a 54 61 69 6c 29 5d 20 61 6e 64 20 ,P,pzTail)] and
26440 69 74 73 20 76 61 72 69 61 6e 74 73 0a 2a 2a 20 its variants.**
26450 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65 20 if the
26460 7a 53 71 6c 20 69 6e 70 75 74 20 74 65 78 74 20 zSql input text
26470 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 contains more th
26480 61 6e 20 6f 6e 65 20 53 51 4c 20 73 74 61 74 65 an one SQL state
26490 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 ment.**
264a0 20 61 6e 64 20 70 7a 54 61 69 6c 20 69 73 20 6e and pzTail is n
264b0 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a 70 ot NULL, then *p
264c0 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 6f zTail is made to
264d0 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a point to the.**
264e0 20 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20 first
264f0 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e byte past the en
26500 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 53 d of the first S
26510 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 QL statement in
26520 7a 53 71 6c 2e 0a 2a 2a 20 20 20 20 20 20 20 20 zSql..**
26530 20 20 3c 74 6f 64 6f 3e 57 68 61 74 20 64 6f 65 <todo>What doe
26540 73 20 2a 70 7a 54 61 69 6c 20 70 6f 69 6e 74 20 s *pzTail point
26550 74 6f 20 69 66 20 74 68 65 72 65 20 69 73 20 6f to if there is o
26560 6e 65 20 73 74 61 74 65 6d 65 6e 74 3f 3c 2f 74 ne statement?</t
26570 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 30 odo>.**.** {H130
26580 31 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 16} A successful
26590 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
265a0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 3_prepare_v2(db,
265b0 7a 53 71 6c 2c 4e 2c 70 70 53 74 6d 74 2c 2e 2e zSql,N,ppStmt,..
265c0 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .)].**
265d0 6f 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 or one of its va
265e0 72 69 61 6e 74 73 20 77 72 69 74 65 73 20 69 6e riants writes in
265f0 74 6f 20 2a 70 70 53 74 6d 74 20 61 20 70 6f 69 to *ppStmt a poi
26600 6e 74 65 72 20 74 6f 20 61 20 6e 65 77 0a 2a 2a nter to a new.**
26610 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 [prepa
26620 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f red statement] o
26630 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 4e r a pointer to N
26640 55 4c 4c 20 69 66 20 7a 53 71 6c 20 63 6f 6e 74 ULL if zSql cont
26650 61 69 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ains.**
26660 20 6e 6f 74 68 69 6e 67 20 6f 74 68 65 72 20 74 nothing other t
26670 68 61 6e 20 77 68 69 74 65 73 70 61 63 65 20 6f han whitespace o
26680 72 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a r comments..**.*
26690 2a 20 7b 48 31 33 30 31 39 7d 20 54 68 65 20 5b * {H13019} The [
266a0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
266b0 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 v2()] interface
266c0 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 and its variants
266d0 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 return.**
266e0 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 [SQLITE_OK]
266f0 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 or an appropriat
26700 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 75 e [error code] u
26710 70 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a pon failure..**.
26720 2a 2a 20 7b 48 31 33 30 32 31 7d 20 42 65 66 6f ** {H13021} Befo
26730 72 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 re [sqlite3_prep
26740 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 are(db,zSql,nByt
26750 65 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 e,ppStmt,pzTail)
26760 5d 20 6f 72 20 69 74 73 0a 2a 2a 20 20 20 20 20 ] or its.**
26770 20 20 20 20 20 76 61 72 69 61 6e 74 73 20 72 65 variants re
26780 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 28 turns an error (
26790 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20 any value other
267a0 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d than [SQLITE_OK]
267b0 29 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 ),.** t
267c0 68 65 79 20 66 69 72 73 74 20 73 65 74 20 2a 70 hey first set *p
267d0 70 53 74 6d 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a pStmt to NULL..*
267e0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
267f0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
26800 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
26810 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
26820 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a atabase handle *
26830 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
26840 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 zSql, /* S
26850 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 QL statement, UT
26860 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 F-8 encoded */.
26870 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 int nByte,
26880 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
26890 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 mum length of zS
268a0 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a ql in bytes. */.
268b0 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
268c0 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 *ppStmt, /* OUT
268d0 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 : Statement hand
268e0 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 le */. const ch
268f0 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 ar **pzTail
26900 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 /* OUT: Pointer
26910 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f to unused portio
26920 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a n of zSql */.);.
26930 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
26940 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
26950 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2(. sqlite3 *db
26960 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
26970 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 Database handle
26980 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
26990 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 *zSql, /*
269a0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 SQL statement, U
269b0 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a TF-8 encoded */.
269c0 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 int nByte,
269d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
269e0 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a imum length of z
269f0 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f Sql in bytes. */
26a00 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
26a10 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 **ppStmt, /* OU
26a20 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e T: Statement han
26a30 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 dle */. const c
26a40 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 har **pzTail
26a50 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 /* OUT: Pointer
26a60 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 to unused porti
26a70 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b on of zSql */.);
26a80 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
26a90 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
26aa0 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 6(. sqlite3 *db
26ab0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
26ac0 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 Database handle
26ad0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
26ae0 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 *zSql, /*
26af0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 SQL statement, U
26b00 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f TF-16 encoded */
26b10 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 . int nByte,
26b20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
26b30 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 ximum length of
26b40 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a zSql in bytes. *
26b50 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 /. sqlite3_stmt
26b60 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f **ppStmt, /* O
26b70 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 UT: Statement ha
26b80 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
26b90 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 void **pzTail
26ba0 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 /* OUT: Pointe
26bb0 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 r to unused port
26bc0 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 ion of zSql */.)
26bd0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
26be0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
26bf0 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 16_v2(. sqlite3
26c00 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
26c10 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e /* Database han
26c20 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 dle */. const v
26c30 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 oid *zSql,
26c40 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e /* SQL statemen
26c50 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 t, UTF-16 encode
26c60 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 d */. int nByte
26c70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
26c80 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 * Maximum length
26c90 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 of zSql in byte
26ca0 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f s. */. sqlite3_
26cb0 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 stmt **ppStmt,
26cc0 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e /* OUT: Statemen
26cd0 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f t handle */. co
26ce0 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 nst void **pzTai
26cf0 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f l /* OUT: Po
26d00 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 inter to unused
26d10 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 portion of zSql
26d20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.);../*.** CAP
26d30 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 69 6e I3REF: Retrievin
26d40 67 20 53 74 61 74 65 6d 65 6e 74 20 53 51 4c 20 g Statement SQL
26d50 7b 48 31 33 31 30 30 7d 20 3c 48 31 33 30 30 30 {H13100} <H13000
26d60 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 >.**.** This int
26d70 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 73 erface can be us
26d80 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 ed to retrieve a
26d90 20 73 61 76 65 64 20 63 6f 70 79 20 6f 66 20 74 saved copy of t
26da0 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53 he original.** S
26db0 51 4c 20 74 65 78 74 20 75 73 65 64 20 74 6f 20 QL text used to
26dc0 63 72 65 61 74 65 20 61 20 5b 70 72 65 70 61 72 create a [prepar
26dd0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 66 ed statement] if
26de0 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 that statement
26df0 77 61 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 was.** compiled
26e00 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71 using either [sq
26e10 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
26e20 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
26e30 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e prepare16_v2()].
26e40 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 .**.** INVARIANT
26e50 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31 30 31 S:.**.** {H13101
26e60 7d 20 49 66 20 74 68 65 20 5b 70 72 65 70 61 72 } If the [prepar
26e70 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 70 61 ed statement] pa
26e80 73 73 65 64 20 61 73 20 74 68 65 20 61 72 67 75 ssed as the argu
26e90 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 ment to.**
26ea0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 71 6c [sqlite3_sql
26eb0 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 ()] was compiled
26ec0 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 using either [s
26ed0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
26ee0 32 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 2()] or.**
26ef0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 [sqlite3_pre
26f00 70 61 72 65 31 36 5f 76 32 28 29 5d 2c 20 74 68 pare16_v2()], th
26f10 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 71 6c 28 en [sqlite3_sql(
26f20 29 5d 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 )] returns.**
26f30 20 20 20 20 20 20 20 61 20 70 6f 69 6e 74 65 72 a pointer
26f40 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 to a zero-termi
26f50 6e 61 74 65 64 20 73 74 72 69 6e 67 20 63 6f 6e nated string con
26f60 74 61 69 6e 69 6e 67 20 61 20 55 54 46 2d 38 20 taining a UTF-8
26f70 72 65 6e 64 65 72 69 6e 67 0a 2a 2a 20 20 20 20 rendering.**
26f80 20 20 20 20 20 20 6f 66 20 74 68 65 20 6f 72 69 of the ori
26f90 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d ginal SQL statem
26fa0 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31 ent..**.** {H131
26fb0 30 32 7d 20 49 66 20 74 68 65 20 5b 70 72 65 70 02} If the [prep
26fc0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
26fd0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 61 72 passed as the ar
26fe0 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 gument to.**
26ff0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 [sqlite3_s
27000 71 6c 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 6c ql()] was compil
27010 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 ed using either
27020 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
27030 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 ()] or.**
27040 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 [sqlite3_prep
27050 61 72 65 31 36 28 29 5d 2c 20 74 68 65 6e 20 5b are16()], then [
27060 73 71 6c 69 74 65 33 5f 73 71 6c 28 29 5d 20 72 sqlite3_sql()] r
27070 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f eturns a NULL po
27080 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 inter..**.** {H1
27090 33 31 30 33 7d 20 54 68 65 20 73 74 72 69 6e 67 3103} The string
270a0 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 returned by [sq
270b0 6c 69 74 65 33 5f 73 71 6c 28 53 29 5d 20 69 73 lite3_sql(S)] is
270c0 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 valid until the
270d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 .** [pr
270e0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
270f0 5d 20 53 20 69 73 20 64 65 6c 65 74 65 64 20 75 ] S is deleted u
27100 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 sing [sqlite3_fi
27110 6e 61 6c 69 7a 65 28 53 29 5d 2e 0a 2a 2f 0a 53 nalize(S)]..*/.S
27120 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
27130 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 char *sqlite3_sq
27140 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a l(sqlite3_stmt *
27150 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 pStmt);../*.** C
27160 41 50 49 33 52 45 46 3a 20 44 79 6e 61 6d 69 63 API3REF: Dynamic
27170 61 6c 6c 79 20 54 79 70 65 64 20 56 61 6c 75 65 ally Typed Value
27180 20 4f 62 6a 65 63 74 20 7b 48 31 35 30 30 30 7d Object {H15000}
27190 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 <S20200>.** KEY
271a0 57 4f 52 44 53 3a 20 7b 70 72 6f 74 65 63 74 65 WORDS: {protecte
271b0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d d sqlite3_value}
271c0 20 7b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 {unprotected sq
271d0 6c 69 74 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a lite3_value}.**.
271e0 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 ** SQLite uses t
271f0 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 he sqlite3_value
27200 20 6f 62 6a 65 63 74 20 74 6f 20 72 65 70 72 65 object to repre
27210 73 65 6e 74 20 61 6c 6c 20 76 61 6c 75 65 73 0a sent all values.
27220 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 73 ** that can be s
27230 74 6f 72 65 64 20 69 6e 20 61 20 64 61 74 61 62 tored in a datab
27240 61 73 65 20 74 61 62 6c 65 2e 20 53 51 4c 69 74 ase table. SQLit
27250 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63 20 74 e uses dynamic t
27260 79 70 69 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 65 yping.** for the
27270 20 76 61 6c 75 65 73 20 69 74 20 73 74 6f 72 65 values it store
27280 73 2e 20 56 61 6c 75 65 73 20 73 74 6f 72 65 64 s. Values stored
27290 20 69 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 in sqlite3_valu
272a0 65 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 63 61 6e e objects.** can
272b0 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20 66 6c be integers, fl
272c0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
272d0 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c ues, strings, BL
272e0 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a OBs, or NULL..**
272f0 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f 76 .** An sqlite3_v
27300 61 6c 75 65 20 6f 62 6a 65 63 74 20 6d 61 79 20 alue object may
27310 62 65 20 65 69 74 68 65 72 20 22 70 72 6f 74 65 be either "prote
27320 63 74 65 64 22 20 6f 72 20 22 75 6e 70 72 6f 74 cted" or "unprot
27330 65 63 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20 ected"..** Some
27340 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69 interfaces requi
27350 72 65 20 61 20 70 72 6f 74 65 63 74 65 64 20 73 re a protected s
27360 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f qlite3_value. O
27370 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 0a ther interfaces.
27380 2a 2a 20 77 69 6c 6c 20 61 63 63 65 70 74 20 65 ** will accept e
27390 69 74 68 65 72 20 61 20 70 72 6f 74 65 63 74 65 ither a protecte
273a0 64 20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 d or an unprotec
273b0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
273c0 65 2e 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74 65 e..** Every inte
273d0 72 66 61 63 65 20 74 68 61 74 20 61 63 63 65 70 rface that accep
273e0 74 73 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ts sqlite3_value
273f0 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69 arguments speci
27400 66 69 65 73 0a 2a 2a 20 77 68 65 74 68 65 72 20 fies.** whether
27410 6f 72 20 6e 6f 74 20 69 74 20 72 65 71 75 69 72 or not it requir
27420 65 73 20 61 20 70 72 6f 74 65 63 74 65 64 20 73 es a protected s
27430 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a qlite3_value..**
27440 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 22 70 .** The terms "p
27450 72 6f 74 65 63 74 65 64 22 20 61 6e 64 20 22 75 rotected" and "u
27460 6e 70 72 6f 74 65 63 74 65 64 22 20 72 65 66 65 nprotected" refe
27470 72 20 74 6f 20 77 68 65 74 68 65 72 20 6f 72 20 r to whether or
27480 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20 69 not.** a mutex i
27490 73 20 68 65 6c 64 2e 20 20 41 20 69 6e 74 65 72 s held. A inter
274a0 6e 61 6c 20 6d 75 74 65 78 20 69 73 20 68 65 6c nal mutex is hel
274b0 64 20 66 6f 72 20 61 20 70 72 6f 74 65 63 74 65 d for a protecte
274c0 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c d.** sqlite3_val
274d0 75 65 20 6f 62 6a 65 63 74 20 62 75 74 20 6e 6f ue object but no
274e0 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 mutex is held f
274f0 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 or an unprotecte
27500 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c d.** sqlite3_val
27510 75 65 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 53 ue object. If S
27520 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
27530 64 20 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74 d to be single-t
27540 68 72 65 61 64 65 64 0a 2a 2a 20 28 77 69 74 68 hreaded.** (with
27550 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 [SQLITE_THREADS
27560 41 46 45 3d 30 5d 20 61 6e 64 20 77 69 74 68 20 AFE=0] and with
27570 5b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 [sqlite3_threads
27580 61 66 65 28 29 5d 20 72 65 74 75 72 6e 69 6e 67 afe()] returning
27590 20 30 29 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c 0).** or if SQL
275a0 69 74 65 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e ite is run in on
275b0 65 20 6f 66 20 72 65 64 75 63 65 64 20 6d 75 74 e of reduced mut
275c0 65 78 20 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 51 ex modes .** [SQ
275d0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 LITE_CONFIG_SING
275e0 4c 45 54 48 52 45 41 44 5d 20 6f 72 20 5b 53 51 LETHREAD] or [SQ
275f0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 LITE_CONFIG_MULT
27600 49 54 48 52 45 41 44 5d 0a 2a 2a 20 74 68 65 6e ITHREAD].** then
27610 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69 73 there is no dis
27620 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e tinction between
27630 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75 protected and u
27640 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 nprotected.** sq
27650 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
27660 63 74 73 20 61 6e 64 20 74 68 65 79 20 63 61 6e cts and they can
27670 20 62 65 20 75 73 65 64 20 69 6e 74 65 72 63 68 be used interch
27680 61 6e 67 65 61 62 6c 79 2e 20 20 48 6f 77 65 76 angeably. Howev
27690 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 6d er,.** for maxim
276a0 75 6d 20 63 6f 64 65 20 70 6f 72 74 61 62 69 6c um code portabil
276b0 69 74 79 20 69 74 20 69 73 20 72 65 63 6f 6d 6d ity it is recomm
276c0 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c 69 ended that appli
276d0 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c cations.** still
276e0 20 6d 61 6b 65 20 74 68 65 20 64 69 73 74 69 6e make the distin
276f0 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 62 65 ction between be
27700 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 tween protected
27710 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a and unprotected.
27720 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ** sqlite3_value
27730 20 6f 62 6a 65 63 74 73 20 65 76 65 6e 20 77 68 objects even wh
27740 65 6e 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 en not strictly
27750 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 required..**.**
27760 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 The sqlite3_valu
27770 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 e objects that a
27780 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61 72 re passed as par
27790 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68 65 ameters into the
277a0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
277b0 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74 69 on of [applicati
277c0 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 on-defined SQL f
277d0 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70 72 unctions] are pr
277e0 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 54 68 65 20 otected..** The
277f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
27800 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 ject returned by
27810 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c .** [sqlite3_col
27820 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 umn_value()] is
27830 75 6e 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 unprotected..**
27840 55 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 Unprotected sqli
27850 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
27860 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 s may only be us
27870 65 64 20 77 69 74 68 0a 2a 2a 20 5b 73 71 6c 69 ed with.** [sqli
27880 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
27890 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
278a0 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 0a _bind_value()]..
278b0 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f ** The [sqlite3_
278c0 76 61 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 71 6c value_blob | sql
278d0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
278e0 29 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 )] family of.**
278f0 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69 interfaces requi
27900 72 65 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c re protected sql
27910 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
27920 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ts..*/.typedef s
27930 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69 74 65 truct Mem sqlite
27940 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 3_value;../*.**
27950 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 46 75 CAPI3REF: SQL Fu
27960 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f nction Context O
27970 62 6a 65 63 74 20 7b 48 31 36 30 30 31 7d 20 3c bject {H16001} <
27980 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20200>.**.** Th
27990 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 e context in whi
279a0 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 ch an SQL functi
279b0 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73 20 73 on executes is s
279c0 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 tored in an.** s
279d0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f qlite3_context o
279e0 62 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e 74 65 bject. A pointe
279f0 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f r to an sqlite3_
27a00 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a 2a context object.*
27a10 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72 73 * is always firs
27a20 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b t parameter to [
27a30 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
27a40 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e ned SQL function
27a50 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 s]..** The appli
27a60 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 cation-defined S
27a70 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c QL function impl
27a80 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 ementation will
27a90 70 61 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f 69 pass this.** poi
27aa0 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69 6e 74 nter through int
27ab0 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 o calls to [sqli
27ac0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 7c te3_result_int |
27ad0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 28 sqlite3_result(
27ae0 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f )],.** [sqlite3_
27af0 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
27b00 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 75 t()], [sqlite3_u
27b10 73 65 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 ser_data()],.**
27b20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 [sqlite3_context
27b30 5f 64 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 5b _db_handle()], [
27b40 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
27b50 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f ata()],.** and/o
27b60 72 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 r [sqlite3_set_a
27b70 75 78 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79 uxdata()]..*/.ty
27b80 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
27b90 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71 6c ite3_context sql
27ba0 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f ite3_context;../
27bb0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 42 *.** CAPI3REF: B
27bc0 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20 54 6f inding Values To
27bd0 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d Prepared Statem
27be0 65 6e 74 73 20 7b 48 31 33 35 30 30 7d 20 3c 53 ents {H13500} <S
27bf0 37 30 33 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 70300>.** KEYWOR
27c00 44 53 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 DS: {host parame
27c10 74 65 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d ter} {host param
27c20 65 74 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 72 eters} {host par
27c30 61 6d 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 ameter name}.**
27c40 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70 KEYWORDS: {SQL p
27c50 61 72 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70 arameter} {SQL p
27c60 61 72 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 61 arameters} {para
27c70 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a meter binding}.*
27c80 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 53 51 4c 20 *.** In the SQL
27c90 73 74 72 69 6e 67 73 20 69 6e 70 75 74 20 74 6f strings input to
27ca0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
27cb0 65 5f 76 32 28 29 5d 20 61 6e 64 20 69 74 73 20 e_v2()] and its
27cc0 76 61 72 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74 variants,.** lit
27cd0 65 72 61 6c 73 20 6d 61 79 20 62 65 20 72 65 70 erals may be rep
27ce0 6c 61 63 65 64 20 62 79 20 61 20 70 61 72 61 6d laced by a param
27cf0 65 74 65 72 20 69 6e 20 6f 6e 65 20 6f 66 20 74 eter in one of t
27d00 68 65 73 65 20 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a hese forms:.**.*
27d10 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 * <ul>.** <li>
27d20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a ?.** <li> ?NNN.
27d30 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a ** <li> :VVV.**
27d40 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c <li> @VVV.** <
27d50 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75 li> $VVV.** </u
27d60 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 l>.**.** In the
27d70 70 61 72 61 6d 65 74 65 72 20 66 6f 72 6d 73 20 parameter forms
27d80 73 68 6f 77 6e 20 61 62 6f 76 65 20 4e 4e 4e 20 shown above NNN
27d90 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c 69 is an integer li
27da0 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 teral,.** and VV
27db0 56 20 69 73 20 61 6e 20 61 6c 70 68 61 2d 6e 75 V is an alpha-nu
27dc0 6d 65 72 69 63 20 70 61 72 61 6d 65 74 65 72 20 meric parameter
27dd0 6e 61 6d 65 2e 20 54 68 65 20 76 61 6c 75 65 73 name. The values
27de0 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70 61 72 of these.** par
27df0 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 63 61 ameters (also ca
27e00 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72 61 6d lled "host param
27e10 65 74 65 72 20 6e 61 6d 65 73 22 20 6f 72 20 22 eter names" or "
27e20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 22 29 SQL parameters")
27e30 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 75 .** can be set u
27e40 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 sing the sqlite3
27e50 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e _bind_*() routin
27e60 65 73 20 64 65 66 69 6e 65 64 20 68 65 72 65 2e es defined here.
27e70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
27e80 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
27e90 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 sqlite3_bind_*(
27ea0 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20 61 6c ) routines is al
27eb0 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 ways.** a pointe
27ec0 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 r to the [sqlite
27ed0 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 20 72 3_stmt] object r
27ee0 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 eturned from.**
27ef0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
27f00 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20 76 61 _v2()] or its va
27f10 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 riants..**.** Th
27f20 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
27f30 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f t is the index o
27f40 66 20 74 68 65 20 53 51 4c 20 70 61 72 61 6d 65 f the SQL parame
27f50 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e 0a 2a ter to be set..*
27f60 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 53 * The leftmost S
27f70 51 4c 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 QL parameter has
27f80 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 20 an index of 1.
27f90 20 57 68 65 6e 20 74 68 65 20 73 61 6d 65 20 6e When the same n
27fa0 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61 72 61 amed.** SQL para
27fb0 6d 65 74 65 72 20 69 73 20 75 73 65 64 20 6d 6f meter is used mo
27fc0 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 73 65 re than once, se
27fd0 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 cond and subsequ
27fe0 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65 6e 63 ent.** occurrenc
27ff0 65 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 es have the same
28000 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 66 69 index as the fi
28010 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65 2e 0a rst occurrence..
28020 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f 72 ** The index for
28030 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 named parameter
28040 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20 s can be looked
28050 75 70 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 up using the.**
28060 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 [sqlite3_bind_pa
28070 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d rameter_index()]
28080 20 41 50 49 20 69 66 20 64 65 73 69 72 65 64 2e API if desired.
28090 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a 20 66 The index.** f
280a0 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 6d 65 or "?NNN" parame
280b0 74 65 72 73 20 69 73 20 74 68 65 20 76 61 6c 75 ters is the valu
280c0 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 e of NNN..** The
280d0 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73 74 20 NNN value must
280e0 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 be between 1 and
280f0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 the [sqlite3_li
28100 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61 6d 65 mit()].** parame
28110 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 ter [SQLITE_LIMI
28120 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 T_VARIABLE_NUMBE
28130 52 5d 20 28 64 65 66 61 75 6c 74 20 76 61 6c 75 R] (default valu
28140 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 e: 999)..**.** T
28150 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
28160 74 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 74 t is the value t
28170 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 20 70 61 o bind to the pa
28180 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 rameter..**.** I
28190 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 n those routines
281a0 20 74 68 61 74 20 68 61 76 65 20 61 20 66 6f 75 that have a fou
281b0 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 74 rth argument, it
281c0 73 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a s value is the.*
281d0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * number of byte
281e0 73 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 s in the paramet
281f0 65 72 2e 20 20 54 6f 20 62 65 20 63 6c 65 61 72 er. To be clear
28200 3a 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 : the value is t
28210 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
28220 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 <u>bytes</u> in
28230 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 the value, not t
28240 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 he number of cha
28250 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20 74 racters..** If t
28260 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 he fourth parame
28270 74 65 72 20 69 73 20 6e 65 67 61 74 69 76 65 2c ter is negative,
28280 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 the length of t
28290 68 65 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 he string is.**
282a0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
282b0 74 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 tes up to the fi
282c0 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 rst zero termina
282d0 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 tor..**.** The f
282e0 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f ifth argument to
282f0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c sqlite3_bind_bl
28300 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 ob(), sqlite3_bi
28310 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a nd_text(), and.*
28320 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 * sqlite3_bind_t
28330 65 78 74 31 36 28 29 20 69 73 20 61 20 64 65 73 ext16() is a des
28340 74 72 75 63 74 6f 72 20 75 73 65 64 20 74 6f 20 tructor used to
28350 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 42 dispose of the B
28360 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67 LOB or.** string
28370 20 61 66 74 65 72 20 53 51 4c 69 74 65 20 68 61 after SQLite ha
28380 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 s finished with
28390 69 74 2e 20 49 66 20 74 68 65 20 66 69 66 74 68 it. If the fifth
283a0 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 argument is.**
283b0 74 68 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 the special valu
283c0 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43 e [SQLITE_STATIC
283d0 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 61 ], then SQLite a
283e0 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 0a ssumes that the.
283f0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 ** information i
28400 73 20 69 6e 20 73 74 61 74 69 63 2c 20 75 6e 6d s in static, unm
28410 61 6e 61 67 65 64 20 73 70 61 63 65 20 61 6e 64 anaged space and
28420 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
28430 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a 20 49 o be freed..** I
28440 66 20 74 68 65 20 66 69 66 74 68 20 61 72 67 75 f the fifth argu
28450 6d 65 6e 74 20 68 61 73 20 74 68 65 20 76 61 6c ment has the val
28460 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 ue [SQLITE_TRANS
28470 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 53 IENT], then.** S
28480 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74 73 20 QLite makes its
28490 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 own private copy
284a0 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 6d 6d of the data imm
284b0 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 ediately, before
284c0 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f .** the sqlite3_
284d0 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 bind_*() routine
284e0 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 returns..**.**
284f0 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 The sqlite3_bind
28500 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f 75 74 _zeroblob() rout
28510 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c 4f 42 ine binds a BLOB
28520 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74 68 61 of length N tha
28530 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20 77 t.** is filled w
28540 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 41 20 7a ith zeroes. A z
28550 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 61 20 66 eroblob uses a f
28560 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 6d ixed amount of m
28570 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74 20 61 emory.** (just a
28580 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 68 6f 6c n integer to hol
28590 64 20 69 74 73 20 73 69 7a 65 29 20 77 68 69 6c d its size) whil
285a0 65 20 69 74 20 69 73 20 62 65 69 6e 67 20 70 72 e it is being pr
285b0 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 6f ocessed..** Zero
285c0 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 65 6e 64 blobs are intend
285d0 65 64 20 74 6f 20 73 65 72 76 65 20 61 73 20 70 ed to serve as p
285e0 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 laceholders for
285f0 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a 20 63 BLOBs whose.** c
28600 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 65 72 20 ontent is later
28610 77 72 69 74 74 65 6e 20 75 73 69 6e 67 0a 2a 2a written using.**
28620 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
28630 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 pen | incrementa
28640 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 74 l BLOB I/O] rout
28650 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 74 ines..** A negat
28660 69 76 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 ive value for th
28670 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73 75 6c e zeroblob resul
28680 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 6e ts in a zero-len
28690 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 gth BLOB..**.**
286a0 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 The sqlite3_bind
286b0 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 6d 75 _*() routines mu
286c0 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 st be called aft
286d0 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 er.** [sqlite3_p
286e0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28 61 6e repare_v2()] (an
286f0 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 d its variants)
28700 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 or [sqlite3_rese
28710 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 6f t()] and.** befo
28720 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 re [sqlite3_step
28730 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 73 ()]..** Bindings
28740 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72 65 64 are not cleared
28750 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 by the [sqlite3
28760 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74 69 6e _reset()] routin
28770 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 61 e..** Unbound pa
28780 72 61 6d 65 74 65 72 73 20 61 72 65 20 69 6e 74 rameters are int
28790 65 72 70 72 65 74 65 64 20 61 73 20 4e 55 4c 4c erpreted as NULL
287a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
287b0 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 5b 53 utines return [S
287c0 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 QLITE_OK] on suc
287d0 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 cess or an error
287e0 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 code if.** anyt
287f0 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e hing goes wrong.
28800 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d [SQLITE_RANGE]
28810 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
28820 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a the parameter.**
28830 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66 index is out of
28840 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49 54 45 range. [SQLITE
28850 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 75 72 _NOMEM] is retur
28860 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 ned if malloc()
28870 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54 fails..** [SQLIT
28880 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68 74 20 E_MISUSE] might
28890 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 be returned if t
288a0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
288b0 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a 2a e called on a.**
288c0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
288d0 20 74 68 61 74 20 69 73 20 74 68 65 20 77 72 6f that is the wro
288e0 6e 67 20 73 74 61 74 65 20 6f 72 20 77 68 69 63 ng state or whic
288f0 68 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 h has already be
28900 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a en finalized..**
28910 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 20 6d 69 Detection of mi
28920 73 75 73 65 20 69 73 20 75 6e 72 65 6c 69 61 62 suse is unreliab
28930 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e le. Application
28940 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64 65 70 s should not dep
28950 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 45 end.** on SQLITE
28960 5f 4d 49 53 55 53 45 20 72 65 74 75 72 6e 73 2e _MISUSE returns.
28970 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 SQLITE_MISUSE
28980 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 69 is intended to i
28990 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 61 20 6c ndicate a.** a l
289a0 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 74 68 ogic error in th
289b0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 e application.
289c0 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 Future versions
289d0 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a of SQLite might.
289e0 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 65 72 20 ** panic rather
289f0 74 68 61 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 than return SQLI
28a00 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a 2a TE_MISUSE..**.**
28a10 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 See also: [sqli
28a20 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
28a30 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20 er_count()],.**
28a40 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 [sqlite3_bind_pa
28a50 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c rameter_name()],
28a60 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 and [sqlite3_bi
28a70 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 nd_parameter_ind
28a80 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 ex()]..**.** INV
28a90 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
28aa0 48 31 33 35 30 36 7d 20 54 68 65 20 5b 53 51 4c H13506} The [SQL
28ab0 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 statement compi
28ac0 6c 65 72 5d 20 72 65 63 6f 67 6e 69 7a 65 73 20 ler] recognizes
28ad0 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 20 66 6f tokens of the fo
28ae0 72 6d 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 rms.**
28af0 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 24 56 "?", "?NNN", "$V
28b00 56 56 22 2c 20 22 3a 56 56 56 22 2c 20 61 6e 64 VV", ":VVV", and
28b10 20 22 40 56 56 56 22 20 61 73 20 53 51 4c 20 70 "@VVV" as SQL p
28b20 61 72 61 6d 65 74 65 72 73 2c 0a 2a 2a 20 20 20 arameters,.**
28b30 20 20 20 20 20 20 20 77 68 65 72 65 20 4e 4e 4e where NNN
28b40 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65 is any sequence
28b50 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 of one or more
28b60 64 69 67 69 74 73 0a 2a 2a 20 20 20 20 20 20 20 digits.**
28b70 20 20 20 61 6e 64 20 77 68 65 72 65 20 56 56 56 and where VVV
28b80 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65 is any sequence
28b90 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 of one or more
28ba0 61 6c 70 68 61 6e 75 6d 65 72 69 63 0a 2a 2a 20 alphanumeric.**
28bb0 20 20 20 20 20 20 20 20 20 63 68 61 72 61 63 74 charact
28bc0 65 72 73 20 6f 72 20 22 3a 3a 22 20 6f 70 74 69 ers or "::" opti
28bd0 6f 6e 61 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 onally followed
28be0 62 79 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 by a string cont
28bf0 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 aining.**
28c00 20 20 20 6e 6f 20 73 70 61 63 65 73 20 61 6e 64 no spaces and
28c10 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 contained withi
28c20 6e 20 70 61 72 65 6e 74 68 65 73 65 73 2e 0a 2a n parentheses..*
28c30 2a 0a 2a 2a 20 7b 48 31 33 35 30 39 7d 20 54 68 *.** {H13509} Th
28c40 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 e initial value
28c50 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 of an SQL parame
28c60 74 65 72 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a ter is NULL..**.
28c70 2a 2a 20 7b 48 31 33 35 31 32 7d 20 54 68 65 20 ** {H13512} The
28c80 69 6e 64 65 78 20 6f 66 20 61 6e 20 22 3f 22 20 index of an "?"
28c90 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 SQL parameter is
28ca0 20 6f 6e 65 20 6c 61 72 67 65 72 20 74 68 61 6e one larger than
28cb0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
28cc0 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78 20 6f largest index o
28cd0 66 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 f SQL parameter
28ce0 74 6f 20 74 68 65 20 6c 65 66 74 2c 20 6f 72 20 to the left, or
28cf0 31 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 1 if.**
28d00 20 74 68 65 20 22 3f 22 20 69 73 20 74 68 65 20 the "?" is the
28d10 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 70 61 72 leftmost SQL par
28d20 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ameter..**.** {H
28d30 31 33 35 31 35 7d 20 54 68 65 20 69 6e 64 65 78 13515} The index
28d40 20 6f 66 20 61 6e 20 22 3f 4e 4e 4e 22 20 53 51 of an "?NNN" SQ
28d50 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 L parameter is t
28d60 68 65 20 69 6e 74 65 67 65 72 20 4e 4e 4e 2e 0a he integer NNN..
28d70 2a 2a 0a 2a 2a 20 7b 48 31 33 35 31 38 7d 20 54 **.** {H13518} T
28d80 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 22 he index of an "
28d90 3a 56 56 56 22 2c 20 22 24 56 56 56 22 2c 20 6f :VVV", "$VVV", o
28da0 72 20 22 40 56 56 56 22 20 53 51 4c 20 70 61 72 r "@VVV" SQL par
28db0 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 20 20 20 ameter is.**
28dc0 20 20 20 20 20 20 74 68 65 20 73 61 6d 65 20 61 the same a
28dd0 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 6c s the index of l
28de0 65 66 74 6d 6f 73 74 20 6f 63 63 75 72 72 65 6e eftmost occurren
28df0 63 65 73 20 6f 66 20 74 68 65 20 73 61 6d 65 0a ces of the same.
28e00 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 61 ** para
28e10 6d 65 74 65 72 2c 20 6f 72 20 6f 6e 65 20 6d 6f meter, or one mo
28e20 72 65 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 re than the larg
28e30 65 73 74 20 69 6e 64 65 78 20 6f 76 65 72 20 61 est index over a
28e40 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 ll.** p
28e50 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65 arameters to the
28e60 20 6c 65 66 74 20 69 66 20 74 68 69 73 20 69 73 left if this is
28e70 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75 72 the first occur
28e80 72 65 6e 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 rence.**
28e90 20 20 6f 66 20 74 68 69 73 20 70 61 72 61 6d 65 of this parame
28ea0 74 65 72 2c 20 6f 72 20 31 20 69 66 20 74 68 69 ter, or 1 if thi
28eb0 73 20 69 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 s is the leftmos
28ec0 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a t parameter..**.
28ed0 2a 2a 20 7b 48 31 33 35 32 31 7d 20 54 68 65 20 ** {H13521} The
28ee0 5b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 [SQL statement c
28ef0 6f 6d 70 69 6c 65 72 5d 20 66 61 69 6c 73 20 77 ompiler] fails w
28f00 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f 52 ith an [SQLITE_R
28f10 41 4e 47 45 5d 0a 2a 2a 20 20 20 20 20 20 20 20 ANGE].**
28f20 20 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 69 error if the i
28f30 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 4c 20 70 ndex of an SQL p
28f40 61 72 61 6d 65 74 65 72 20 69 73 20 6c 65 73 73 arameter is less
28f50 20 74 68 61 6e 20 31 0a 2a 2a 20 20 20 20 20 20 than 1.**
28f60 20 20 20 20 6f 72 20 67 72 65 61 74 65 72 20 74 or greater t
28f70 68 61 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d han the compile-
28f80 74 69 6d 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f time SQLITE_MAX_
28f90 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 0a VARIABLE_NUMBER.
28fa0 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 61 ** para
28fb0 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 meter..**.** {H1
28fc0 33 35 32 34 7d 20 43 61 6c 6c 73 20 74 6f 20 5b 3524} Calls to [
28fd0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
28fe0 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 t | sqlite3_bind
28ff0 28 53 2c 4e 2c 56 2c 2e 2e 2e 29 5d 0a 2a 2a 20 (S,N,V,...)].**
29000 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 61 associa
29010 74 65 20 74 68 65 20 76 61 6c 75 65 20 56 20 77 te the value V w
29020 69 74 68 20 61 6c 6c 20 53 51 4c 20 70 61 72 61 ith all SQL para
29030 6d 65 74 65 72 73 20 68 61 76 69 6e 67 20 61 6e meters having an
29040 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 64 .** ind
29050 65 78 20 6f 66 20 4e 20 69 6e 20 74 68 65 20 5b ex of N in the [
29060 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
29070 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 nt] S..**.** {H1
29080 33 35 32 37 7d 20 43 61 6c 6c 73 20 74 6f 20 5b 3527} Calls to [
29090 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
290a0 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 t | sqlite3_bind
290b0 28 53 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 (S,N,...)].**
290c0 20 20 20 20 20 20 20 6f 76 65 72 72 69 64 65 20 override
290d0 70 72 69 6f 72 20 63 61 6c 6c 73 20 77 69 74 68 prior calls with
290e0 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 73 the same values
290f0 20 6f 66 20 53 20 61 6e 64 20 4e 2e 0a 2a 2a 0a of S and N..**.
29100 2a 2a 20 7b 48 31 33 35 33 30 7d 20 42 69 6e 64 ** {H13530} Bind
29110 69 6e 67 73 20 65 73 74 61 62 6c 69 73 68 65 64 ings established
29120 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e by [sqlite3_bin
29130 64 5f 74 65 78 74 20 7c 20 73 71 6c 69 74 65 33 d_text | sqlite3
29140 5f 62 69 6e 64 28 53 2c 2e 2e 2e 29 5d 0a 2a 2a _bind(S,...)].**
29150 20 20 20 20 20 20 20 20 20 20 70 65 72 73 69 73 persis
29160 74 20 61 63 72 6f 73 73 20 63 61 6c 6c 73 20 74 t across calls t
29170 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 o [sqlite3_reset
29180 28 53 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 (S)]..**.** {H13
29190 35 33 33 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 533} In calls to
291a0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 [sqlite3_bind_b
291b0 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c lob(S,N,V,L,D)],
291c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
291d0 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 lite3_bind_text(
291e0 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 0a S,N,V,L,D)], or.
291f0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
29200 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 ite3_bind_text16
29210 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51 4c (S,N,V,L,D)] SQL
29220 69 74 65 20 62 69 6e 64 73 20 74 68 65 20 66 69 ite binds the fi
29230 72 73 74 20 4c 0a 2a 2a 20 20 20 20 20 20 20 20 rst L.**
29240 20 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42 bytes of the B
29250 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 20 70 6f LOB or string po
29260 69 6e 74 65 64 20 74 6f 20 62 79 20 56 2c 20 77 inted to by V, w
29270 68 65 6e 20 4c 0a 2a 2a 20 20 20 20 20 20 20 20 hen L.**
29280 20 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 is non-negativ
29290 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 33 36 e..**.** {H13536
292a0 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73 } In calls to [s
292b0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
292c0 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 6f 72 0a (S,N,V,L,D)] or.
292d0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
292e0 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 ite3_bind_text16
292f0 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51 4c (S,N,V,L,D)] SQL
29300 69 74 65 20 62 69 6e 64 73 20 63 68 61 72 61 63 ite binds charac
29310 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ters.**
29320 20 66 72 6f 6d 20 56 20 74 68 72 6f 75 67 68 20 from V through
29330 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 63 the first zero c
29340 68 61 72 61 63 74 65 72 20 77 68 65 6e 20 4c 20 haracter when L
29350 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a is negative..**.
29360 2a 2a 20 7b 48 31 33 35 33 39 7d 20 49 6e 20 63 ** {H13539} In c
29370 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
29380 5f 62 69 6e 64 5f 62 6c 6f 62 28 53 2c 4e 2c 56 _bind_blob(S,N,V
29390 2c 4c 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 ,L,D)],.**
293a0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e [sqlite3_bin
293b0 64 5f 74 65 78 74 28 53 2c 4e 2c 56 2c 4c 2c 44 d_text(S,N,V,L,D
293c0 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 )], or.**
293d0 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 [sqlite3_bind
293e0 5f 74 65 78 74 31 36 28 53 2c 4e 2c 56 2c 4c 2c _text16(S,N,V,L,
293f0 44 29 5d 20 77 68 65 6e 20 44 20 69 73 20 74 68 D)] when D is th
29400 65 20 73 70 65 63 69 61 6c 0a 2a 2a 20 20 20 20 e special.**
29410 20 20 20 20 20 20 63 6f 6e 73 74 61 6e 74 20 5b constant [
29420 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20 SQLITE_STATIC],
29430 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74 SQLite assumes t
29440 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 56 0a hat the value V.
29450 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 68 ** is h
29460 65 6c 64 20 69 6e 20 73 74 61 74 69 63 20 75 6e eld in static un
29470 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 74 68 managed space th
29480 61 74 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e at will not chan
29490 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 ge.** d
294a0 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 uring the lifeti
294b0 6d 65 20 6f 66 20 74 68 65 20 62 69 6e 64 69 6e me of the bindin
294c0 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 34 32 g..**.** {H13542
294d0 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73 } In calls to [s
294e0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
294f0 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 0a 2a 2a (S,N,V,L,D)],.**
29500 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
29510 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 53 2c 4e e3_bind_text(S,N
29520 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20 ,V,L,D)], or.**
29530 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
29540 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 53 2c 3_bind_text16(S,
29550 4e 2c 56 2c 4c 2c 44 29 5d 20 77 68 65 6e 20 44 N,V,L,D)] when D
29560 20 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 0a is the special.
29570 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 ** cons
29580 74 61 6e 74 20 5b 53 51 4c 49 54 45 5f 54 52 41 tant [SQLITE_TRA
29590 4e 53 49 45 4e 54 5d 2c 20 74 68 65 20 72 6f 75 NSIENT], the rou
295a0 74 69 6e 65 20 6d 61 6b 65 73 20 61 0a 2a 2a 20 tine makes a.**
295b0 20 20 20 20 20 20 20 20 20 70 72 69 76 61 74 65 private
295c0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c copy of the val
295d0 75 65 20 56 20 62 65 66 6f 72 65 20 69 74 20 72 ue V before it r
295e0 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 eturns..**.** {H
295f0 31 33 35 34 35 7d 20 49 6e 20 63 61 6c 6c 73 20 13545} In calls
29600 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 to [sqlite3_bind
29610 5f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 _blob(S,N,V,L,D)
29620 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b ],.** [
29630 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
29640 74 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f t(S,N,V,L,D)], o
29650 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 r.** [s
29660 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
29670 31 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 77 16(S,N,V,L,D)] w
29680 68 65 6e 20 44 20 69 73 20 61 20 70 6f 69 6e 74 hen D is a point
29690 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 er to.**
296a0 20 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 a function, SQ
296b0 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 61 Lite invokes tha
296c0 74 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65 t function to de
296d0 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20 20 stroy the.**
296e0 20 20 20 20 20 20 76 61 6c 75 65 20 56 20 61 66 value V af
296f0 74 65 72 20 69 74 20 68 61 73 20 66 69 6e 69 73 ter it has finis
29700 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 76 61 hed using the va
29710 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 lue V..**.** {H1
29720 33 35 34 38 7d 20 49 6e 20 63 61 6c 6c 73 20 74 3548} In calls t
29730 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f o [sqlite3_bind_
29740 7a 65 72 6f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c zeroblob(S,N,V,L
29750 29 5d 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75 )] the value bou
29760 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 nd.** i
29770 73 20 61 20 42 4c 4f 42 20 6f 66 20 4c 20 62 79 s a BLOB of L by
29780 74 65 73 2c 20 6f 72 20 61 20 7a 65 72 6f 2d 6c tes, or a zero-l
29790 65 6e 67 74 68 20 42 4c 4f 42 20 69 66 20 4c 20 ength BLOB if L
297a0 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a is negative..**.
297b0 2a 2a 20 7b 48 31 33 35 35 31 7d 20 49 6e 20 63 ** {H13551} In c
297c0 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
297d0 5f 62 69 6e 64 5f 76 61 6c 75 65 28 53 2c 4e 2c _bind_value(S,N,
297e0 56 29 5d 20 74 68 65 20 56 20 61 72 67 75 6d 65 V)] the V argume
297f0 6e 74 20 6d 61 79 0a 2a 2a 20 20 20 20 20 20 20 nt may.**
29800 20 20 20 62 65 20 65 69 74 68 65 72 20 61 20 5b be either a [
29810 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
29820 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 3_value] object
29830 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 or an.**
29840 20 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 [unprotected s
29850 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 qlite3_value] ob
29860 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ject..*/.SQLITE_
29870 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
29880 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65 bind_blob(sqlite
29890 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
298a0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e nst void*, int n
298b0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 , void(*)(void*)
298c0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
298d0 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 t sqlite3_bind_d
298e0 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 ouble(sqlite3_st
298f0 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65 mt*, int, double
29900 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
29910 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 t sqlite3_bind_i
29920 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a nt(sqlite3_stmt*
29930 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
29940 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
29950 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 te3_bind_int64(s
29960 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
29970 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 t, sqlite3_int64
29980 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
29990 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e t sqlite3_bind_n
299a0 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ull(sqlite3_stmt
299b0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
299c0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
299d0 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 65 bind_text(sqlite
299e0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
299f0 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e nst char*, int n
29a00 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 , void(*)(void*)
29a10 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
29a20 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 t sqlite3_bind_t
29a30 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ext16(sqlite3_st
29a40 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 mt*, int, const
29a50 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 void*, int, void
29a60 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
29a70 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
29a80 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 te3_bind_value(s
29a90 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
29aa0 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 t, const sqlite3
29ab0 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
29ac0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
29ad0 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 _bind_zeroblob(s
29ae0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
29af0 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a t, int n);../*.*
29b00 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 * CAPI3REF: Numb
29b10 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d 65 er Of SQL Parame
29b20 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c 53 ters {H13600} <S
29b30 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 70300>.**.** Thi
29b40 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 s routine can be
29b50 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68 used to find th
29b60 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c e number of [SQL
29b70 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a 20 parameters].**
29b80 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 in a [prepared s
29b90 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c 20 tatement]. SQL
29ba0 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 74 parameters are t
29bb0 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a 20 okens of the.**
29bc0 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22 form "?", "?NNN"
29bd0 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41 22 , ":AAA", "$AAA"
29be0 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61 74 , or "@AAA" that
29bf0 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c 61 serve as.** pla
29c00 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76 61 ceholders for va
29c10 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b 73 lues that are [s
29c20 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
29c30 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20 | bound].** to
29c40 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 the parameters a
29c50 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a t a later time..
29c60 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
29c70 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74 75 ne actually retu
29c80 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 rns the index of
29c90 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72 69 the largest (ri
29ca0 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61 ghtmost).** para
29cb0 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66 meter. For all f
29cc0 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e orms except ?NNN
29cd0 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 72 , this will corr
29ce0 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a espond to the.**
29cf0 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 75 number of uniqu
29d00 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 49 e parameters. I
29d10 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 f parameters of
29d20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73 65 the ?NNN are use
29d30 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 20 d,.** there may
29d40 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 6c be gaps in the l
29d50 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ist..**.** See a
29d60 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 lso: [sqlite3_bi
29d70 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f nd_blob|sqlite3_
29d80 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c bind()],.** [sql
29d90 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
29da0 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64 ter_name()], and
29db0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
29dc0 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 d_parameter_inde
29dd0 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 x()]..**.** INVA
29de0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
29df0 31 33 36 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 13601} The [sqli
29e00 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
29e10 65 72 5f 63 6f 75 6e 74 28 53 29 5d 20 69 6e 74 er_count(S)] int
29e20 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a 2a erface returns.*
29e30 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6c * the l
29e40 61 72 67 65 73 74 20 69 6e 64 65 78 20 6f 66 20 argest index of
29e50 61 6c 6c 20 53 51 4c 20 70 61 72 61 6d 65 74 65 all SQL paramete
29e60 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 rs in the.**
29e70 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 [prepared
29e80 73 74 61 74 65 6d 65 6e 74 5d 20 53 2c 20 6f 72 statement] S, or
29e90 20 30 20 69 66 20 53 20 63 6f 6e 74 61 69 6e 73 0 if S contains
29ea0 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 74 65 no SQL paramete
29eb0 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 rs..*/.SQLITE_AP
29ec0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
29ed0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 nd_parameter_cou
29ee0 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a nt(sqlite3_stmt*
29ef0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
29f00 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f EF: Name Of A Ho
29f10 73 74 20 50 61 72 61 6d 65 74 65 72 20 7b 48 31 st Parameter {H1
29f20 33 36 32 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 3620} <S70300>.*
29f30 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
29f40 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e e returns a poin
29f50 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 ter to the name
29f60 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 5b of the n-th.** [
29f70 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 69 SQL parameter] i
29f80 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 n a [prepared st
29f90 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 4c atement]..** SQL
29fa0 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 parameters of t
29fb0 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 6f he form "?NNN" o
29fc0 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40 41 41 r ":AAA" or "@AA
29fd0 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 A" or "$AAA".**
29fe0 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68 69 63 have a name whic
29ff0 68 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 20 h is the string
2a000 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 "?NNN" or ":AAA"
2a010 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20 22 24 or "@AAA" or "$
2a020 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 74 69 AAA".** respecti
2a030 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 vely..** In othe
2a040 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 r words, the ini
2a050 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 tial ":" or "$"
2a060 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a 2a 2a or "@" or "?".**
2a070 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73 20 is included as
2a080 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 part of the name
2a090 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 20 ..** Parameters
2a0a0 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 20 of the form "?"
2a0b0 77 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c 6f 77 without a follow
2a0c0 69 6e 67 20 69 6e 74 65 67 65 72 20 68 61 76 65 ing integer have
2a0d0 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 no name.** and
2a0e0 61 72 65 20 61 6c 73 6f 20 72 65 66 65 72 72 65 are also referre
2a0f0 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79 6d 6f d to as "anonymo
2a100 75 73 20 70 61 72 61 6d 65 74 65 72 73 22 2e 0a us parameters"..
2a110 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
2a120 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 68 host parameter h
2a130 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 as an index of 1
2a140 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 , not 0..**.** I
2a150 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20 69 73 f the value n is
2a160 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 out of range or
2a170 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70 61 72 if the n-th par
2a180 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d ameter is.** nam
2a190 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c eless, then NULL
2a1a0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 is returned. T
2a1b0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 he returned stri
2a1c0 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 ng is.** always
2a1d0 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e in UTF-8 encodin
2a1e0 67 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e 61 g even if the na
2a1f0 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20 77 61 med parameter wa
2a200 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 s.** originally
2a210 73 70 65 63 69 66 69 65 64 20 61 73 20 55 54 46 specified as UTF
2a220 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f -16 in [sqlite3_
2a230 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f 72 0a prepare16()] or.
2a240 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
2a250 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a are16_v2()]..**.
2a260 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
2a270 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c lite3_bind_blob|
2a280 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c sqlite3_bind()],
2a290 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
2a2a0 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e d_parameter_coun
2a2b0 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 t()], and.** [sq
2a2c0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
2a2d0 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a eter_index()]..*
2a2e0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
2a2f0 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 36 32 31 7d 20 .**.** {H13621}
2a300 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e The [sqlite3_bin
2a310 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 d_parameter_name
2a320 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 (S,N)] interface
2a330 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 returns.**
2a340 20 20 20 20 20 61 20 55 54 46 2d 38 20 72 65 6e a UTF-8 ren
2a350 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 6e 61 dering of the na
2a360 6d 65 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61 me of the SQL pa
2a370 72 61 6d 65 74 65 72 20 69 6e 0a 2a 2a 20 20 20 rameter in.**
2a380 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 65 70 the [prep
2a390 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
2a3a0 53 20 68 61 76 69 6e 67 20 69 6e 64 65 78 20 4e S having index N
2a3b0 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 , or.**
2a3c0 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 NULL if there i
2a3d0 73 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 74 s no SQL paramet
2a3e0 65 72 20 77 69 74 68 20 69 6e 64 65 78 20 4e 20 er with index N
2a3f0 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 20 or if the.**
2a400 20 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 20 parameter
2a410 77 69 74 68 20 69 6e 64 65 78 20 4e 20 69 73 20 with index N is
2a420 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 70 61 72 an anonymous par
2a430 61 6d 65 74 65 72 20 22 3f 22 2e 0a 2a 2f 0a 53 ameter "?"..*/.S
2a440 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
2a450 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69 char *sqlite3_bi
2a460 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d nd_parameter_nam
2a470 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
2a480 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 int);../*.** CA
2a490 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66 PI3REF: Index Of
2a4a0 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 69 74 A Parameter Wit
2a4b0 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b h A Given Name {
2a4c0 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e H13640} <S70300>
2a4d0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 .**.** Return th
2a4e0 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 e index of an SQ
2a4f0 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 76 65 L parameter give
2a500 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65 n its name. The
2a510 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20 .** index value
2a520 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 69 74 returned is suit
2a530 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20 able for use as
2a540 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61 the second.** pa
2a550 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 rameter to [sqli
2a560 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 te3_bind_blob|sq
2a570 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20 lite3_bind()].
2a580 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74 A zero.** is ret
2a590 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63 urned if no matc
2a5a0 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69 hing parameter i
2a5b0 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61 s found. The pa
2a5c0 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20 rameter.** name
2a5d0 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e must be given in
2a5e0 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74 UTF-8 even if t
2a5f0 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 he original stat
2a600 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65 ement.** was pre
2a610 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31 pared from UTF-1
2a620 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71 6 text using [sq
2a630 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
2a640 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 v2()]..**.** See
2a650 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f also: [sqlite3_
2a660 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 bind_blob|sqlite
2a670 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 3_bind()],.** [s
2a680 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
2a690 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20 meter_count()],
2a6a0 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f and.** [sqlite3_
2a6b0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 bind_parameter_i
2a6c0 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 ndex()]..**.** I
2a6d0 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
2a6e0 20 7b 48 31 33 36 34 31 7d 20 54 68 65 20 5b 73 {H13641} The [s
2a6f0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
2a700 6d 65 74 65 72 5f 69 6e 64 65 78 28 53 2c 4e 29 meter_index(S,N)
2a710 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ] interface retu
2a720 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 rns.**
2a730 74 68 65 20 69 6e 64 65 78 20 6f 66 20 53 51 4c the index of SQL
2a740 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68 parameter in th
2a750 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
2a760 65 6d 65 6e 74 5d 0a 2a 2a 20 20 20 20 20 20 20 ement].**
2a770 20 20 20 53 20 77 68 6f 73 65 20 6e 61 6d 65 20 S whose name
2a780 6d 61 74 63 68 65 73 20 74 68 65 20 55 54 46 2d matches the UTF-
2a790 38 20 73 74 72 69 6e 67 20 4e 2c 20 6f 72 20 30 8 string N, or 0
2a7a0 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 if there is.**
2a7b0 20 20 20 20 20 20 20 20 20 6e 6f 20 6d 61 74 63 no matc
2a7c0 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 h..*/.SQLITE_API
2a7d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
2a7e0 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 d_parameter_inde
2a7f0 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c x(sqlite3_stmt*,
2a800 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
2a810 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 me);../*.** CAPI
2a820 33 52 45 46 3a 20 52 65 73 65 74 20 41 6c 6c 20 3REF: Reset All
2a830 42 69 6e 64 69 6e 67 73 20 4f 6e 20 41 20 50 72 Bindings On A Pr
2a840 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 epared Statement
2a850 20 7b 48 31 33 36 36 30 7d 20 3c 53 37 30 33 30 {H13660} <S7030
2a860 30 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 0>.**.** Contrar
2a870 79 20 74 6f 20 74 68 65 20 69 6e 74 75 69 74 69 y to the intuiti
2a880 6f 6e 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c on of many, [sql
2a890 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 64 6f ite3_reset()] do
2a8a0 65 73 20 6e 6f 74 20 72 65 73 65 74 0a 2a 2a 20 es not reset.**
2a8b0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e the [sqlite3_bin
2a8c0 64 5f 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67 d_blob | binding
2a8d0 73 5d 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 s] on a [prepare
2a8e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a d statement]..**
2a8f0 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e Use this routin
2a900 65 20 74 6f 20 72 65 73 65 74 20 61 6c 6c 20 68 e to reset all h
2a910 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 ost parameters t
2a920 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e o NULL..**.** IN
2a930 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 VARIANTS:.**.**
2a940 7b 48 31 33 36 36 31 7d 20 54 68 65 20 5b 73 71 {H13661} The [sq
2a950 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 lite3_clear_bind
2a960 69 6e 67 73 28 53 29 5d 20 69 6e 74 65 72 66 61 ings(S)] interfa
2a970 63 65 20 72 65 73 65 74 73 20 61 6c 6c 20 53 51 ce resets all SQ
2a980 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 L.** pa
2a990 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 73 rameter bindings
2a9a0 20 69 6e 20 74 68 65 20 5b 70 72 65 70 61 72 65 in the [prepare
2a9b0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 62 d statement] S b
2a9c0 61 63 6b 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a ack to NULL..*/.
2a9d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2a9e0 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e qlite3_clear_bin
2a9f0 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 dings(sqlite3_st
2aa00 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 mt*);../*.** CAP
2aa10 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66 I3REF: Number Of
2aa20 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 65 Columns In A Re
2aa30 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31 30 sult Set {H13710
2aa40 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a } <S10700>.**.**
2aa50 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
2aa60 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
2aa70 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
2aa80 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a returned by the.
2aa90 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 ** [prepared sta
2aaa0 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72 6f tement]. This ro
2aab0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 20 utine returns 0
2aac0 69 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20 53 if pStmt is an S
2aad0 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 QL.** statement
2aae0 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 that does not re
2aaf0 74 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20 65 turn data (for e
2ab00 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41 54 xample an [UPDAT
2ab10 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 E])..**.** INVAR
2ab20 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 IANTS:.**.** {H1
2ab30 33 37 31 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 3711} The [sqlit
2ab40 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 e3_column_count(
2ab50 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 S)] interface re
2ab60 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
2ab70 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of.**
2ab80 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 columns in the r
2ab90 65 73 75 6c 74 20 73 65 74 20 67 65 6e 65 72 61 esult set genera
2aba0 74 65 64 20 62 79 20 74 68 65 20 5b 70 72 65 70 ted by the [prep
2abb0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
2abc0 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f S,.** o
2abd0 72 20 30 20 69 66 20 53 20 64 6f 65 73 20 6e 6f r 0 if S does no
2abe0 74 20 67 65 6e 65 72 61 74 65 20 61 20 72 65 73 t generate a res
2abf0 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 ult set..*/.SQLI
2ac00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
2ac10 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 e3_column_count(
2ac20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
2ac30 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 tmt);../*.** CAP
2ac40 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61 I3REF: Column Na
2ac50 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20 mes In A Result
2ac60 53 65 74 20 7b 48 31 33 37 32 30 7d 20 3c 53 31 Set {H13720} <S1
2ac70 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 0700>.**.** Thes
2ac80 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 e routines retur
2ac90 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67 n the name assig
2aca0 6e 65 64 20 74 6f 20 61 20 70 61 72 74 69 63 75 ned to a particu
2acb0 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e lar column.** in
2acc0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
2acd0 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 of a [SELECT] st
2ace0 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 73 71 atement. The sq
2acf0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
2ad00 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 e().** interface
2ad10 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
2ad20 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 er to a zero-ter
2ad30 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 minated UTF-8 st
2ad40 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 ring.** and sqli
2ad50 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 te3_column_name1
2ad60 36 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f 6() returns a po
2ad70 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d inter to a zero-
2ad80 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 terminated.** UT
2ad90 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 F-16 string. Th
2ada0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
2adb0 72 20 69 73 20 74 68 65 20 5b 70 72 65 70 61 72 r is the [prepar
2adc0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a ed statement].**
2add0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
2ade0 20 74 68 65 20 5b 53 45 4c 45 43 54 5d 20 73 74 the [SELECT] st
2adf0 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 atement. The sec
2ae00 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 ond parameter is
2ae10 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e the.** column n
2ae20 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65 66 74 umber. The left
2ae30 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6e most column is n
2ae40 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 umber 0..**.** T
2ae50 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 he returned stri
2ae60 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 ng pointer is va
2ae70 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72 lid until either
2ae80 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 the [prepared s
2ae90 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20 tatement].** is
2aea0 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71 destroyed by [sq
2aeb0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
2aec0 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 6e ] or until the n
2aed0 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 ext call to.** s
2aee0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 qlite3_column_na
2aef0 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f me() or sqlite3_
2af00 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 column_name16()
2af10 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 on the same colu
2af20 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c mn..**.** If sql
2af30 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 ite3_malloc() fa
2af40 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65 20 70 ils during the p
2af50 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 69 74 rocessing of eit
2af60 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28 her routine.** (
2af70 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 72 69 for example duri
2af80 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 ng a conversion
2af90 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 from UTF-8 to UT
2afa0 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20 F-16) then a.**
2afb0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 NULL pointer is
2afc0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
2afd0 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 72 65 The name of a re
2afe0 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74 sult column is t
2aff0 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
2b000 22 41 53 22 20 63 6c 61 75 73 65 20 66 6f 72 0a "AS" clause for.
2b010 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20 ** that column,
2b020 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 41 if there is an A
2b030 53 20 63 6c 61 75 73 65 2e 20 20 49 66 20 74 68 S clause. If th
2b040 65 72 65 20 69 73 20 6e 6f 20 41 53 20 63 6c 61 ere is no AS cla
2b050 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 use.** then the
2b060 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 name of the colu
2b070 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 66 69 65 mn is unspecifie
2b080 64 20 61 6e 64 20 6d 61 79 20 63 68 61 6e 67 65 d and may change
2b090 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 6c from.** one rel
2b0a0 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 20 74 ease of SQLite t
2b0b0 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a 2a o the next..**.*
2b0c0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
2b0d0 0a 2a 2a 20 7b 48 31 33 37 32 31 7d 20 41 20 73 .** {H13721} A s
2b0e0 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 uccessful invoca
2b0f0 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c tion of the [sql
2b100 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 ite3_column_name
2b110 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 (S,N)].**
2b120 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 interface ret
2b130 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 urns the name of
2b140 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
2b150 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a 20 20 (where 0 is.**
2b160 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 66 74 the left
2b170 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 6f 72 most column) for
2b180 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
2b190 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 of the.**
2b1a0 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 [prepared sta
2b1b0 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 20 7a tement] S as a z
2b1c0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 ero-terminated U
2b1d0 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a TF-8 string..**.
2b1e0 2a 2a 20 7b 48 31 33 37 32 33 7d 20 41 20 73 75 ** {H13723} A su
2b1f0 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 ccessful invocat
2b200 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 ion of the [sqli
2b210 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 te3_column_name1
2b220 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20 6(S,N)].**
2b230 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65 interface re
2b240 74 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f turns the name o
2b250 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e f the Nth column
2b260 20 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a 20 (where 0 is.**
2b270 20 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 66 the lef
2b280 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 6f tmost column) fo
2b290 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 r the result set
2b2a0 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 of the.**
2b2b0 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 [prepared st
2b2c0 61 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 20 atement] S as a
2b2d0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 zero-terminated
2b2e0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 0a 2a 2a UTF-16 string.**
2b2f0 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 68 65 in the
2b300 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
2b310 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 32 er..**.** {H1372
2b320 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 4} The [sqlite3_
2b330 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20 61 column_name()] a
2b340 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 nd [sqlite3_colu
2b350 6d 6e 5f 6e 61 6d 65 31 36 28 29 5d 0a 2a 2a 20 mn_name16()].**
2b360 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61 interfa
2b370 63 65 73 20 72 65 74 75 72 6e 20 61 20 4e 55 4c ces return a NUL
2b380 4c 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68 65 L pointer if the
2b390 79 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 0a y are unable to.
2b3a0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6c 6c 6f ** allo
2b3b0 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20 68 cate memory to h
2b3c0 6f 6c 64 20 74 68 65 69 72 20 6e 6f 72 6d 61 6c old their normal
2b3d0 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 73 2e return strings.
2b3e0 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 32 35 7d 20 .**.** {H13725}
2b3f0 49 66 20 74 68 65 20 4e 20 70 61 72 61 6d 65 74 If the N paramet
2b400 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 er to [sqlite3_c
2b410 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d olumn_name(S,N)]
2b420 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 or.**
2b430 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
2b440 6e 61 6d 65 31 36 28 53 2c 4e 29 5d 20 69 73 20 name16(S,N)] is
2b450 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 out of range, th
2b460 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 en the.**
2b470 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 72 65 interfaces re
2b480 74 75 72 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e turn a NULL poin
2b490 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 ter..**.** {H137
2b4a0 32 36 7d 20 54 68 65 20 73 74 72 69 6e 67 73 20 26} The strings
2b4b0 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c returned by [sql
2b4c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 ite3_column_name
2b4d0 28 53 2c 4e 29 5d 20 61 6e 64 0a 2a 2a 20 20 20 (S,N)] and.**
2b4e0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
2b4f0 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 53 2c column_name16(S,
2b500 4e 29 5d 20 61 72 65 20 76 61 6c 69 64 20 75 6e N)] are valid un
2b510 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 til the next.**
2b520 20 20 20 20 20 20 20 20 20 63 61 6c 6c 20 74 6f call to
2b530 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65 20 either routine
2b540 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 53 20 with the same S
2b550 61 6e 64 20 4e 20 70 61 72 61 6d 65 74 65 72 73 and N parameters
2b560 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 .** or
2b570 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f 66 until [sqlite3_f
2b580 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20 63 inalize(S)] is c
2b590 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 alled..**.** {H1
2b5a0 33 37 32 37 7d 20 57 68 65 6e 20 61 20 72 65 73 3727} When a res
2b5b0 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 ult column of a
2b5c0 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 [SELECT] stateme
2b5d0 6e 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20 nt contains.**
2b5e0 20 20 20 20 20 20 20 20 61 6e 20 41 53 20 63 6c an AS cl
2b5f0 61 75 73 65 2c 20 74 68 65 20 6e 61 6d 65 20 6f ause, the name o
2b600 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 f that column is
2b610 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a the identifier.
2b620 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 74 ** to t
2b630 68 65 20 72 69 67 68 74 20 6f 66 20 74 68 65 20 he right of the
2b640 41 53 20 6b 65 79 77 6f 72 64 2e 0a 2a 2f 0a 53 AS keyword..*/.S
2b650 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
2b660 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
2b670 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 lumn_name(sqlite
2b680 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 3_stmt*, int N);
2b690 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
2b6a0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
2b6b0 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 column_name16(sq
2b6c0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
2b6d0 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 N);../*.** CAPI
2b6e0 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 20 3REF: Source Of
2b6f0 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 20 Data In A Query
2b700 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d 20 Result {H13740}
2b710 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10700>.**.** T
2b720 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72 hese routines pr
2b730 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f ovide a means to
2b740 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 determine what
2b750 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a column of what.*
2b760 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 * table in which
2b770 20 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75 database a resu
2b780 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d lt of a [SELECT]
2b790 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 statement comes
2b7a0 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 from..** The na
2b7b0 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
2b7c0 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 63 se or table or c
2b7d0 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 74 olumn can be ret
2b7e0 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 68 urned as.** eith
2b7f0 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 54 er a UTF-8 or UT
2b800 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 F-16 string. Th
2b810 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f 75 e _database_ rou
2b820 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a 20 tines return.**
2b830 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
2b840 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 72 e, the _table_ r
2b850 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 outines return t
2b860 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 61 he table name, a
2b870 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e nd.** the origin
2b880 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 _ routines retur
2b890 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d n the column nam
2b8a0 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e e..** The return
2b8b0 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 6c ed string is val
2b8c0 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 72 id until the [pr
2b8d0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2b8e0 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a 2a ] is destroyed.*
2b8f0 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 * using [sqlite3
2b900 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 _finalize()] or
2b910 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69 until the same i
2b920 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 nformation is re
2b930 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e quested.** again
2b940 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 in a different
2b950 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 encoding..**.**
2b960 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 6e The names return
2b970 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 69 ed are the origi
2b980 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e nal un-aliased n
2b990 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 ames of the.** d
2b9a0 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c 20 atabase, table,
2b9b0 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a and column..**.*
2b9c0 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 * The first argu
2b9d0 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c ment to the foll
2b9e0 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 61 owing calls is a
2b9f0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
2ba00 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 ment]..** These
2ba10 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e functions return
2ba20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
2ba30 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d ut the Nth colum
2ba40 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a n returned by.**
2ba50 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 the statement,
2ba60 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 where N is the s
2ba70 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 econd function a
2ba80 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 rgument..**.** I
2ba90 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e f the Nth column
2baa0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
2bab0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e statement is an
2bac0 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a 2a expression or.*
2bad0 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69 * subquery and i
2bae0 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76 s not a column v
2baf0 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f alue, then all o
2bb00 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e f these function
2bb10 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c s return.** NULL
2bb20 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
2bb30 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75 might also retu
2bb40 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d rn NULL if a mem
2bb50 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
2bb60 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 rror.** occurs.
2bb70 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79 Otherwise, they
2bb80 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 return the name
2bb90 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64 of the attached
2bba0 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 database, table
2bbb0 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 .** and column t
2bbc0 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74 hat query result
2bbd0 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 72 column was extr
2bbe0 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a acted from..**.*
2bbf0 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f 74 * As with all ot
2bc00 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 2c her SQLite APIs,
2bc10 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 64 those postfixed
2bc20 20 77 69 74 68 20 22 31 36 22 20 72 65 74 75 72 with "16" retur
2bc30 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f n.** UTF-16 enco
2bc40 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 65 ded strings, the
2bc50 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 other functions
2bc60 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 7b return UTF-8. {
2bc70 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 END}.**.** These
2bc80 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61 APIs are only a
2bc90 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 vailable if the
2bca0 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 library was comp
2bcb0 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a iled with the.**
2bcc0 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f [SQLITE_ENABLE_
2bcd0 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d COLUMN_METADATA]
2bce0 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 C-preprocessor
2bcf0 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a symbol defined..
2bd00 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a 2a **.** {A13751}.*
2bd10 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 * If two or more
2bd20 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e threads call on
2bd30 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 e or more of the
2bd40 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69 se routines agai
2bd50 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 nst the same.**
2bd60 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2bd70 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 nt and column at
2bd80 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 the same time t
2bd90 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 hen the results
2bda0 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 are.** undefined
2bdb0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
2bdc0 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 34 TS:.**.** {H1374
2bdd0 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 1} The [sqlite3_
2bde0 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f column_database_
2bdf0 6e 61 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 name(S,N)] inter
2be00 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 face returns eit
2be10 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 her.**
2be20 74 68 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 the UTF-8 zero-t
2be30 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f erminated name o
2be40 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
2be50 72 6f 6d 20 77 68 69 63 68 20 74 68 65 0a 2a 2a rom which the.**
2be60 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 72 65 Nth re
2be70 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 sult column of t
2be80 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
2be90 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 78 74 tement] S is ext
2bea0 72 61 63 74 65 64 2c 0a 2a 2a 20 20 20 20 20 20 racted,.**
2beb0 20 20 20 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 or NULL if t
2bec0 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 he Nth column of
2bed0 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c 20 S is a general
2bee0 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20 20 expression.**
2bef0 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e 61 or if una
2bf00 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ble to allocate
2bf10 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65 20 memory to store
2bf20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 the name..**.**
2bf30 7b 48 31 33 37 34 32 7d 20 54 68 65 20 5b 73 71 {H13742} The [sq
2bf40 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 lite3_column_dat
2bf50 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 53 2c 4e abase_name16(S,N
2bf60 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 )] interface ret
2bf70 75 72 6e 73 20 65 69 74 68 65 72 0a 2a 2a 20 20 urns either.**
2bf80 20 20 20 20 20 20 20 20 74 68 65 20 55 54 46 2d the UTF-
2bf90 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 16 native byte o
2bfa0 72 64 65 72 20 7a 65 72 6f 2d 74 65 72 6d 69 6e rder zero-termin
2bfb0 61 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 ated name of the
2bfc0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 database.**
2bfd0 20 20 20 20 20 20 66 72 6f 6d 20 77 68 69 63 68 from which
2bfe0 20 74 68 65 20 4e 74 68 20 72 65 73 75 6c 74 20 the Nth result
2bff0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70 column of the [p
2c000 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2c010 74 5d 20 53 20 69 73 0a 2a 2a 20 20 20 20 20 20 t] S is.**
2c020 20 20 20 20 65 78 74 72 61 63 74 65 64 2c 20 6f extracted, o
2c030 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e 74 r NULL if the Nt
2c040 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 h column of S is
2c050 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65 a general expre
2c060 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ssion.**
2c070 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74 or if unable t
2c080 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 o allocate memor
2c090 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6e y to store the n
2c0a0 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 ame..**.** {H137
2c0b0 34 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 43} The [sqlite3
2c0c0 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 _column_table_na
2c0d0 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 me(S,N)] interfa
2c0e0 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 ce returns eithe
2c0f0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 r.** th
2c100 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72 e UTF-8 zero-ter
2c110 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66 20 minated name of
2c120 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 the table from w
2c130 68 69 63 68 20 74 68 65 0a 2a 2a 20 20 20 20 20 hich the.**
2c140 20 20 20 20 20 4e 74 68 20 72 65 73 75 6c 74 20 Nth result
2c150 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70 column of the [p
2c160 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2c170 74 5d 20 53 20 69 73 20 65 78 74 72 61 63 74 65 t] S is extracte
2c180 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f d,.** o
2c190 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e 74 r NULL if the Nt
2c1a0 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 h column of S is
2c1b0 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65 a general expre
2c1c0 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ssion.**
2c1d0 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74 or if unable t
2c1e0 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 o allocate memor
2c1f0 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6e y to store the n
2c200 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 ame..**.** {H137
2c210 34 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 44} The [sqlite3
2c220 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 _column_table_na
2c230 6d 65 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 me16(S,N)] inter
2c240 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 face returns eit
2c250 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 her.**
2c260 74 68 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76 the UTF-16 nativ
2c270 65 20 62 79 74 65 20 6f 72 64 65 72 20 7a 65 72 e byte order zer
2c280 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d o-terminated nam
2c290 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a e of the table.*
2c2a0 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20 * from
2c2b0 77 68 69 63 68 20 74 68 65 20 4e 74 68 20 72 65 which the Nth re
2c2c0 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 sult column of t
2c2d0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
2c2e0 74 65 6d 65 6e 74 5d 20 53 20 69 73 0a 2a 2a 20 tement] S is.**
2c2f0 20 20 20 20 20 20 20 20 20 65 78 74 72 61 63 74 extract
2c300 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 ed, or NULL if t
2c310 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 he Nth column of
2c320 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c 20 S is a general
2c330 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20 20 expression.**
2c340 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e 61 or if una
2c350 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ble to allocate
2c360 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65 20 memory to store
2c370 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 the name..**.**
2c380 7b 48 31 33 37 34 35 7d 20 54 68 65 20 5b 73 71 {H13745} The [sq
2c390 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 lite3_column_ori
2c3a0 67 69 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d 20 69 gin_name(S,N)] i
2c3b0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
2c3c0 20 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 either.**
2c3d0 20 20 20 20 74 68 65 20 55 54 46 2d 38 20 7a 65 the UTF-8 ze
2c3e0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 ro-terminated na
2c3f0 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
2c400 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63 column from whic
2c410 68 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 h the.**
2c420 20 20 4e 74 68 20 72 65 73 75 6c 74 20 63 6f 6c Nth result col
2c430 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70 72 65 70 umn of the [prep
2c440 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
2c450 53 20 69 73 20 65 78 74 72 61 63 74 65 64 2c 0a S is extracted,.
2c460 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 4e ** or N
2c470 55 4c 4c 20 69 66 20 74 68 65 20 4e 74 68 20 63 ULL if the Nth c
2c480 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 20 61 20 olumn of S is a
2c490 67 65 6e 65 72 61 6c 20 65 78 70 72 65 73 73 69 general expressi
2c4a0 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f on.** o
2c4b0 72 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 61 r if unable to a
2c4c0 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 llocate memory t
2c4d0 6f 20 73 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 o store the name
2c4e0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 34 36 7d ..**.** {H13746}
2c4f0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f The [sqlite3_co
2c500 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 lumn_origin_name
2c510 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 16(S,N)] interfa
2c520 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 ce returns eithe
2c530 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 r.** th
2c540 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 e UTF-16 native
2c550 62 79 74 65 20 6f 72 64 65 72 20 7a 65 72 6f 2d byte order zero-
2c560 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 terminated name
2c570 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 of the table.**
2c580 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20 column
2c590 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 20 4e from which the N
2c5a0 74 68 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e th result column
2c5b0 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 of the.**
2c5c0 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 [prepared st
2c5d0 61 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 78 atement] S is ex
2c5e0 74 72 61 63 74 65 64 2c 20 6f 72 20 4e 55 4c 4c tracted, or NULL
2c5f0 20 69 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 if the Nth colu
2c600 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f mn.** o
2c610 66 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c f S is a general
2c620 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 69 expression or i
2c630 66 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f f unable to allo
2c640 63 61 74 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 cate memory.**
2c650 20 20 20 20 20 20 20 20 74 6f 20 73 74 6f 72 65 to store
2c660 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a the name..**.**
2c670 20 7b 48 31 33 37 34 38 7d 20 54 68 65 20 72 65 {H13748} The re
2c680 74 75 72 6e 20 76 61 6c 75 65 73 20 66 72 6f 6d turn values from
2c690 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
2c6a0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 lite3_column_dat
2c6b0 61 62 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c abase_name | col
2c6c0 75 6d 6e 20 6d 65 74 61 64 61 74 61 20 69 6e 74 umn metadata int
2c6d0 65 72 66 61 63 65 73 5d 0a 2a 2a 20 20 20 20 20 erfaces].**
2c6e0 20 20 20 20 20 61 72 65 20 76 61 6c 69 64 20 66 are valid f
2c6f0 6f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 or the lifetime
2c700 6f 66 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 of the [prepared
2c710 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 20 statement].**
2c720 20 20 20 20 20 20 20 20 6f 72 20 75 6e 74 69 6c or until
2c730 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 the encoding is
2c740 20 63 68 61 6e 67 65 64 20 62 79 20 61 6e 6f 74 changed by anot
2c750 68 65 72 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 her metadata.**
2c760 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61 interfa
2c770 63 65 20 63 61 6c 6c 20 66 6f 72 20 74 68 65 20 ce call for the
2c780 73 61 6d 65 20 70 72 65 70 61 72 65 64 20 73 74 same prepared st
2c790 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c 75 atement and colu
2c7a0 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 mn..**.** ASSUMP
2c7b0 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 TIONS:.**.** {A1
2c7c0 33 37 35 31 7d 20 49 66 20 74 77 6f 20 6f 72 20 3751} If two or
2c7d0 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 6c more threads cal
2c7e0 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a l one or more.**
2c7f0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
2c800 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 e3_column_databa
2c810 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e se_name | column
2c820 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 metadata interf
2c830 61 63 65 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20 aces].**
2c840 20 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b for the same [
2c850 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2c860 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63 nt] and result c
2c870 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 olumn.**
2c880 20 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 at the same ti
2c890 6d 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 me then the resu
2c8a0 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 lts are undefine
2c8b0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
2c8c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
2c8d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 ite3_column_data
2c8e0 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 base_name(sqlite
2c8f0 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 3_stmt*,int);.SQ
2c900 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
2c910 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
2c920 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d umn_database_nam
2c930 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 e16(sqlite3_stmt
2c940 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 *,int);.SQLITE_A
2c950 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
2c960 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 qlite3_column_ta
2c970 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 ble_name(sqlite3
2c980 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c _stmt*,int);.SQL
2c990 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
2c9a0 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
2c9b0 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 mn_table_name16(
2c9c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e sqlite3_stmt*,in
2c9d0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 t);.SQLITE_API c
2c9e0 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
2c9f0 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e e3_column_origin
2ca00 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
2ca10 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 mt*,int);.SQLITE
2ca20 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
2ca30 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
2ca40 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 origin_name16(sq
2ca50 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 lite3_stmt*,int)
2ca60 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2ca70 46 3a 20 44 65 63 6c 61 72 65 64 20 44 61 74 61 F: Declared Data
2ca80 74 79 70 65 20 4f 66 20 41 20 51 75 65 72 79 20 type Of A Query
2ca90 52 65 73 75 6c 74 20 7b 48 31 33 37 36 30 7d 20 Result {H13760}
2caa0 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10700>.**.** T
2cab0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
2cac0 65 72 20 69 73 20 61 20 5b 70 72 65 70 61 72 65 er is a [prepare
2cad0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a d statement]..**
2cae0 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 If this stateme
2caf0 6e 74 20 69 73 20 61 20 5b 53 45 4c 45 43 54 5d nt is a [SELECT]
2cb00 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 statement and t
2cb10 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 he Nth column of
2cb20 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 the.** returned
2cb30 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 result set of t
2cb40 68 61 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 20 hat [SELECT] is
2cb50 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 a table column (
2cb60 6e 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 not an.** expres
2cb70 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 sion or subquery
2cb80 29 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61 ) then the decla
2cb90 72 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 red type of the
2cba0 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 table.** column
2cbb0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 is returned. If
2cbc0 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
2cbd0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
2cbe0 74 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 t is an.** expre
2cbf0 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 ssion or subquer
2cc00 79 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 y, then a NULL p
2cc10 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e ointer is return
2cc20 65 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 ed..** The retur
2cc30 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c ned string is al
2cc40 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 ways UTF-8 encod
2cc50 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 ed. {END}.**.**
2cc60 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 For example, giv
2cc70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
2cc80 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 schema:.**.** CR
2cc90 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 31 EATE TABLE t1(c1
2cca0 20 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a VARIANT);.**.**
2ccb0 20 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69 and the followi
2ccc0 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 ng statement to
2ccd0 62 65 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a be compiled:.**.
2cce0 2a 2a 20 53 45 4c 45 43 54 20 63 31 20 2b 20 31 ** SELECT c1 + 1
2ccf0 2c 20 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a , c1 FROM t1;.**
2cd00 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
2cd10 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 would return th
2cd20 65 20 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e e string "VARIAN
2cd30 54 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e T" for the secon
2cd40 64 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 d result.** colu
2cd50 6d 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61 mn (i==1), and a
2cd60 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f NULL pointer fo
2cd70 72 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75 r the first resu
2cd80 6c 74 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 lt column (i==0)
2cd90 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 ..**.** SQLite u
2cda0 73 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d ses dynamic run-
2cdb0 74 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f time typing. So
2cdc0 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 20 just because a
2cdd0 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 column.** is dec
2cde0 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e lared to contain
2cdf0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 79 a particular ty
2ce00 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e pe does not mean
2ce10 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 that the.** dat
2ce20 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74 a stored in that
2ce30 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 column is of th
2ce40 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e e declared type.
2ce50 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 SQLite is.** s
2ce60 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 trongly typed, b
2ce70 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 73 ut the typing is
2ce80 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 dynamic not sta
2ce90 74 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73 tic. Type.** is
2cea0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
2ceb0 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75 individual valu
2cec0 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 es, not with the
2ced0 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75 containers.** u
2cee0 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73 sed to hold thos
2cef0 65 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 e values..**.**
2cf00 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
2cf10 2a 20 7b 48 31 33 37 36 31 7d 20 20 41 20 73 75 * {H13761} A su
2cf20 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f ccessful call to
2cf30 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
2cf40 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d 20 _decltype(S,N)]
2cf50 72 65 74 75 72 6e 73 20 61 0a 2a 2a 20 20 20 20 returns a.**
2cf60 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72 6d zero-term
2cf70 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 inated UTF-8 str
2cf80 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ing containing t
2cf90 68 65 20 64 65 63 6c 61 72 65 64 20 64 61 74 61 he declared data
2cfa0 74 79 70 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 type.**
2cfb0 20 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 of the table c
2cfc0 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 olumn that appea
2cfd0 72 73 20 61 73 20 74 68 65 20 4e 74 68 20 63 6f rs as the Nth co
2cfe0 6c 75 6d 6e 20 28 6e 75 6d 62 65 72 65 64 0a 2a lumn (numbered.*
2cff0 2a 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d * from
2d000 20 30 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c 0) of the resul
2d010 74 20 73 65 74 20 74 6f 20 74 68 65 20 5b 70 72 t set to the [pr
2d020 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2d030 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 ] S..**.** {H137
2d040 36 32 7d 20 20 41 20 73 75 63 63 65 73 73 66 75 62} A successfu
2d050 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 l call to [sqlit
2d060 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 e3_column_declty
2d070 70 65 31 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 pe16(S,N)].**
2d080 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 returns
2d090 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 a zero-terminate
2d0a0 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 d UTF-16 native
2d0b0 62 79 74 65 20 6f 72 64 65 72 20 73 74 72 69 6e byte order strin
2d0c0 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63 g.** c
2d0d0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 64 65 ontaining the de
2d0e0 63 6c 61 72 65 64 20 64 61 74 61 74 79 70 65 20 clared datatype
2d0f0 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c of the table col
2d100 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 72 73 umn that appears
2d110 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 73 .** as
2d120 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
2d130 28 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 (numbered from 0
2d140 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 ) of the result
2d150 73 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 set to the.**
2d160 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 [prepare
2d170 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 2e 0a d statement] S..
2d180 2a 2a 0a 2a 2a 20 7b 48 31 33 37 36 33 7d 20 20 **.** {H13763}
2d190 49 66 20 4e 20 69 73 20 6c 65 73 73 20 74 68 61 If N is less tha
2d1a0 6e 20 30 20 6f 72 20 4e 20 69 73 20 67 72 65 61 n 0 or N is grea
2d1b0 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ter than or equa
2d1c0 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 l to.**
2d1d0 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 the number of
2d1e0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 5b columns in the [
2d1f0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2d200 6e 74 5d 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 nt] S,.**
2d210 20 20 20 20 6f 72 20 69 66 20 74 68 65 20 4e 74 or if the Nt
2d220 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 h column of S is
2d230 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f an expression o
2d240 72 20 73 75 62 71 75 65 72 79 20 72 61 74 68 65 r subquery rathe
2d250 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 r.** t
2d260 68 61 6e 20 61 20 74 61 62 6c 65 20 63 6f 6c 75 han a table colu
2d270 6d 6e 2c 20 6f 72 20 69 66 20 61 20 6d 65 6d 6f mn, or if a memo
2d280 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ry allocation fa
2d290 69 6c 75 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 ilure.**
2d2a0 20 20 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 occurs during
2d2b0 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 encoding conver
2d2c0 73 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a 2a 20 20 sions, then.**
2d2d0 20 20 20 20 20 20 20 20 20 63 61 6c 6c 73 20 74 calls t
2d2e0 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d o [sqlite3_colum
2d2f0 6e 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d n_decltype(S,N)]
2d300 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 or.**
2d310 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
2d320 5f 64 65 63 6c 74 79 70 65 31 36 28 53 2c 4e 29 _decltype16(S,N)
2d330 5d 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a ] return NULL..*
2d340 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
2d350 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
2d360 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 _column_decltype
2d370 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 (sqlite3_stmt*,i
2d380 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 nt);.SQLITE_API
2d390 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
2d3a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 te3_column_declt
2d3b0 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ype16(sqlite3_st
2d3c0 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a mt*,int);../*.**
2d3d0 20 43 41 50 49 33 52 45 46 3a 20 45 76 61 6c 75 CAPI3REF: Evalu
2d3e0 61 74 65 20 41 6e 20 53 51 4c 20 53 74 61 74 65 ate An SQL State
2d3f0 6d 65 6e 74 20 7b 48 31 33 32 30 30 7d 20 3c 53 ment {H13200} <S
2d400 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 66 74 10000>.**.** Aft
2d410 65 72 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 er a [prepared s
2d420 74 61 74 65 6d 65 6e 74 5d 20 68 61 73 20 62 65 tatement] has be
2d430 65 6e 20 70 72 65 70 61 72 65 64 20 75 73 69 6e en prepared usin
2d440 67 20 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c g either.** [sql
2d450 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
2d460 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 )] or [sqlite3_p
2d470 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f repare16_v2()] o
2d480 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 65 67 r one of the leg
2d490 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 acy.** interface
2d4a0 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 s [sqlite3_prepa
2d4b0 72 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 re()] or [sqlite
2d4c0 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2c 20 3_prepare16()],
2d4d0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a this function.**
2d4e0 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 must be called
2d4f0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 one or more time
2d500 73 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 s to evaluate th
2d510 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a e statement..**.
2d520 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f ** The details o
2d530 66 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f f the behavior o
2d540 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 f the sqlite3_st
2d550 65 70 28 29 20 69 6e 74 65 72 66 61 63 65 20 64 ep() interface d
2d560 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 77 68 65 74 epend.** on whet
2d570 68 65 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e her the statemen
2d580 74 20 77 61 73 20 70 72 65 70 61 72 65 64 20 75 t was prepared u
2d590 73 69 6e 67 20 74 68 65 20 6e 65 77 65 72 20 22 sing the newer "
2d5a0 76 32 22 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a v2" interface.**
2d5b0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
2d5c0 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c e_v2()] and [sql
2d5d0 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 ite3_prepare16_v
2d5e0 32 28 29 5d 20 6f 72 20 74 68 65 20 6f 6c 64 65 2()] or the olde
2d5f0 72 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 r legacy.** inte
2d600 72 66 61 63 65 20 5b 73 71 6c 69 74 65 33 5f 70 rface [sqlite3_p
2d610 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 repare()] and [s
2d620 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
2d630 28 29 5d 2e 20 20 54 68 65 20 75 73 65 20 6f 66 ()]. The use of
2d640 20 74 68 65 0a 2a 2a 20 6e 65 77 20 22 76 32 22 the.** new "v2"
2d650 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 interface is re
2d660 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6e 65 commended for ne
2d670 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 62 w applications b
2d680 75 74 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a ut the legacy.**
2d690 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 interface will
2d6a0 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 20 73 continue to be s
2d6b0 75 70 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 upported..**.**
2d6c0 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e In the legacy in
2d6d0 74 65 72 66 61 63 65 2c 20 74 68 65 20 72 65 74 terface, the ret
2d6e0 75 72 6e 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 urn value will b
2d6f0 65 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 e either [SQLITE
2d700 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 _BUSY],.** [SQLI
2d710 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 TE_DONE], [SQLIT
2d720 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c 49 54 45 5f E_ROW], [SQLITE_
2d730 45 52 52 4f 52 5d 2c 20 6f 72 20 5b 53 51 4c 49 ERROR], or [SQLI
2d740 54 45 5f 4d 49 53 55 53 45 5d 2e 0a 2a 2a 20 57 TE_MISUSE]..** W
2d750 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e 74 ith the "v2" int
2d760 65 72 66 61 63 65 2c 20 61 6e 79 20 6f 66 20 74 erface, any of t
2d770 68 65 20 6f 74 68 65 72 20 5b 72 65 73 75 6c 74 he other [result
2d780 20 63 6f 64 65 73 5d 20 6f 72 0a 2a 2a 20 5b 65 codes] or.** [e
2d790 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
2d7a0 6f 64 65 73 5d 20 6d 69 67 68 74 20 62 65 20 72 odes] might be r
2d7b0 65 74 75 72 6e 65 64 20 61 73 20 77 65 6c 6c 2e eturned as well.
2d7c0 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 .**.** [SQLITE_B
2d7d0 55 53 59 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 USY] means that
2d7e0 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67 the database eng
2d7f0 69 6e 65 20 77 61 73 20 75 6e 61 62 6c 65 20 74 ine was unable t
2d800 6f 20 61 63 71 75 69 72 65 20 74 68 65 0a 2a 2a o acquire the.**
2d810 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 20 database locks
2d820 69 74 20 6e 65 65 64 73 20 74 6f 20 64 6f 20 69 it needs to do i
2d830 74 73 20 6a 6f 62 2e 20 20 49 66 20 74 68 65 20 ts job. If the
2d840 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b statement is a [
2d850 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20 6f 63 COMMIT].** or oc
2d860 63 75 72 73 20 6f 75 74 73 69 64 65 20 6f 66 20 curs outside of
2d870 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e an explicit tran
2d880 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 79 6f saction, then yo
2d890 75 20 63 61 6e 20 72 65 74 72 79 20 74 68 65 0a u can retry the.
2d8a0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 ** statement. I
2d8b0 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 f the statement
2d8c0 69 73 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d 49 54 is not a [COMMIT
2d8d0 5d 20 61 6e 64 20 6f 63 63 75 72 73 20 77 69 74 ] and occurs wit
2d8e0 68 69 6e 20 61 0a 2a 2a 20 65 78 70 6c 69 63 69 hin a.** explici
2d8f0 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 68 t transaction th
2d900 65 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20 72 6f en you should ro
2d910 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 llback the trans
2d920 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a 2a 2a action before.**
2d930 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a 0a continuing..**.
2d940 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d ** [SQLITE_DONE]
2d950 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
2d960 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66 69 statement has fi
2d970 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67 nished executing
2d980 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 .** successfully
2d990 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 . sqlite3_step(
2d9a0 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ) should not be
2d9b0 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 20 called again on
2d9c0 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a 2a 20 this virtual.**
2d9d0 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75 74 20 machine without
2d9e0 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20 5b 73 first calling [s
2d9f0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 qlite3_reset()]
2da00 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72 to reset the vir
2da10 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 tual.** machine
2da20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 back to its init
2da30 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a ial state..**.**
2da40 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 If the SQL stat
2da50 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78 65 63 ement being exec
2da60 75 74 65 64 20 72 65 74 75 72 6e 73 20 61 6e 79 uted returns any
2da70 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53 51 4c data, then [SQL
2da80 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73 20 72 ITE_ROW].** is r
2da90 65 74 75 72 6e 65 64 20 65 61 63 68 20 74 69 6d eturned each tim
2daa0 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 20 64 e a new row of d
2dab0 61 74 61 20 69 73 20 72 65 61 64 79 20 66 6f 72 ata is ready for
2dac0 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79 20 74 processing by t
2dad0 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54 68 he.** caller. Th
2dae0 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65 20 e values may be
2daf0 61 63 63 65 73 73 65 64 20 75 73 69 6e 67 20 74 accessed using t
2db00 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 he [column acces
2db10 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a s functions]..**
2db20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
2db30 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 is called again
2db40 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20 to retrieve the
2db50 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61 74 61 next row of data
2db60 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f ..**.** [SQLITE_
2db70 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74 68 61 ERROR] means tha
2db80 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 t a run-time err
2db90 6f 72 20 28 73 75 63 68 20 61 73 20 61 20 63 6f or (such as a co
2dba0 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69 6f 6c nstraint.** viol
2dbb0 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63 75 72 ation) has occur
2dbc0 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 red. sqlite3_st
2dbd0 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 ep() should not
2dbe0 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 be called again
2dbf0 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20 4d 6f on.** the VM. Mo
2dc00 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d re information m
2dc10 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79 20 63 ay be found by c
2dc20 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f alling [sqlite3_
2dc30 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20 57 69 errmsg()]..** Wi
2dc40 74 68 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e th the legacy in
2dc50 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72 65 20 terface, a more
2dc60 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 63 specific error c
2dc70 6f 64 65 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 ode (for example
2dc80 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 ,.** [SQLITE_INT
2dc90 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49 54 45 ERRUPT], [SQLITE
2dca0 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c 49 54 _SCHEMA], [SQLIT
2dcb0 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e 64 20 E_CORRUPT], and
2dcc0 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 63 61 6e so forth).** can
2dcd0 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 be obtained by
2dce0 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 calling [sqlite3
2dcf0 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 74 68 65 _reset()] on the
2dd00 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 .** [prepared st
2dd10 61 74 65 6d 65 6e 74 5d 2e 20 20 49 6e 20 74 68 atement]. In th
2dd20 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 e "v2" interface
2dd30 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 70 ,.** the more sp
2dd40 65 63 69 66 69 63 20 65 72 72 6f 72 20 63 6f 64 ecific error cod
2dd50 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 64 69 e is returned di
2dd60 72 65 63 74 6c 79 20 62 79 20 73 71 6c 69 74 65 rectly by sqlite
2dd70 33 5f 73 74 65 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 3_step()..**.**
2dd80 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 [SQLITE_MISUSE]
2dd90 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 means that the t
2dda0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 his routine was
2ddb0 63 61 6c 6c 65 64 20 69 6e 61 70 70 72 6f 70 72 called inappropr
2ddc0 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 65 72 68 61 iately..** Perha
2ddd0 70 73 20 69 74 20 77 61 73 20 63 61 6c 6c 65 64 ps it was called
2dde0 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 on a [prepared
2ddf0 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 statement] that
2de00 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 has.** already b
2de10 65 65 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e een [sqlite3_fin
2de20 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 alize | finalize
2de30 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 20 74 68 61 d] or on one tha
2de40 74 20 68 61 64 0a 2a 2a 20 70 72 65 76 69 6f 75 t had.** previou
2de50 73 6c 79 20 72 65 74 75 72 6e 65 64 20 5b 53 51 sly returned [SQ
2de60 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6f 72 20 5b LITE_ERROR] or [
2de70 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 4f SQLITE_DONE]. O
2de80 72 20 69 74 20 63 6f 75 6c 64 0a 2a 2a 20 62 65 r it could.** be
2de90 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 74 the case that t
2dea0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
2deb0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 62 connection is b
2dec0 65 69 6e 67 20 75 73 65 64 20 62 79 20 74 77 6f eing used by two
2ded0 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 68 72 65 or.** more thre
2dee0 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 ads at the same
2def0 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 6d 65 2e 0a moment in time..
2df00 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79 20 49 **.** <b>Goofy I
2df10 6e 74 65 72 66 61 63 65 20 41 6c 65 72 74 3a 3c nterface Alert:<
2df20 2f 62 3e 20 49 6e 20 74 68 65 20 6c 65 67 61 63 /b> In the legac
2df30 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 y interface, the
2df40 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a sqlite3_step().
2df50 2a 2a 20 41 50 49 20 61 6c 77 61 79 73 20 72 65 ** API always re
2df60 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69 63 20 turns a generic
2df70 65 72 72 6f 72 20 63 6f 64 65 2c 20 5b 53 51 4c error code, [SQL
2df80 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66 6f 6c 6c ITE_ERROR], foll
2df90 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a 20 65 72 72 owing any.** err
2dfa0 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 or other than [S
2dfb0 51 4c 49 54 45 5f 42 55 53 59 5d 20 61 6e 64 20 QLITE_BUSY] and
2dfc0 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e [SQLITE_MISUSE].
2dfd0 20 20 59 6f 75 20 6d 75 73 74 20 63 61 6c 6c 0a You must call.
2dfe0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 ** [sqlite3_rese
2dff0 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 t()] or [sqlite3
2e000 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 6e 20 _finalize()] in
2e010 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20 6f 6e order to find on
2e020 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 70 65 63 e of the.** spec
2e030 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 ific [error code
2e040 73 5d 20 74 68 61 74 20 62 65 74 74 65 72 20 64 s] that better d
2e050 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72 72 escribes the err
2e060 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 6d 69 74 20 or..** We admit
2e070 74 68 61 74 20 74 68 69 73 20 69 73 20 61 20 67 that this is a g
2e080 6f 6f 66 79 20 64 65 73 69 67 6e 2e 20 20 54 68 oofy design. Th
2e090 65 20 70 72 6f 62 6c 65 6d 20 68 61 73 20 62 65 e problem has be
2e0a0 65 6e 20 66 69 78 65 64 0a 2a 2a 20 77 69 74 68 en fixed.** with
2e0b0 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 the "v2" interf
2e0c0 61 63 65 2e 20 20 49 66 20 79 6f 75 20 70 72 65 ace. If you pre
2e0d0 70 61 72 65 20 61 6c 6c 20 6f 66 20 79 6f 75 72 pare all of your
2e0e0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a SQL statements.
2e0f0 2a 2a 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 ** using either
2e100 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
2e110 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 _v2()] or [sqlit
2e120 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 e3_prepare16_v2(
2e130 29 5d 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 )] instead.** of
2e140 20 74 68 65 20 6c 65 67 61 63 79 20 5b 73 71 6c the legacy [sql
2e150 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 ite3_prepare()]
2e160 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 and [sqlite3_pre
2e170 70 61 72 65 31 36 28 29 5d 20 69 6e 74 65 72 66 pare16()] interf
2e180 61 63 65 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 aces,.** then th
2e190 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 20 e more specific
2e1a0 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 61 72 [error codes] ar
2e1b0 65 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63 e returned direc
2e1c0 74 6c 79 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 tly.** by sqlite
2e1d0 33 5f 73 74 65 70 28 29 2e 20 20 54 68 65 20 75 3_step(). The u
2e1e0 73 65 20 6f 66 20 74 68 65 20 22 76 32 22 20 69 se of the "v2" i
2e1f0 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63 6f nterface is reco
2e200 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 mmended..**.** I
2e210 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
2e220 20 7b 48 31 33 32 30 32 7d 20 20 49 66 20 74 68 {H13202} If th
2e230 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
2e240 65 6d 65 6e 74 5d 20 53 20 69 73 20 72 65 61 64 ement] S is read
2e250 79 20 74 6f 20 62 65 20 72 75 6e 2c 20 74 68 65 y to be run, the
2e260 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b n.** [
2e270 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d sqlite3_step(S)]
2e280 20 61 64 76 61 6e 63 65 73 20 74 68 61 74 20 70 advances that p
2e290 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2e2a0 74 20 75 6e 74 69 6c 0a 2a 2a 20 20 20 20 20 20 t until.**
2e2b0 20 20 20 20 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 completion
2e2c0 6f 72 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72 or until it is r
2e2d0 65 61 64 79 20 74 6f 20 72 65 74 75 72 6e 20 61 eady to return a
2e2e0 6e 6f 74 68 65 72 20 72 6f 77 20 6f 66 20 74 68 nother row of th
2e2f0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 72 e.** r
2e300 65 73 75 6c 74 20 73 65 74 2c 20 6f 72 20 75 6e esult set, or un
2e310 74 69 6c 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f til an [sqlite3_
2e320 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74 65 interrupt | inte
2e330 72 72 75 70 74 5d 0a 2a 2a 20 20 20 20 20 20 20 rrupt].**
2e340 20 20 20 20 6f 72 20 61 20 72 75 6e 2d 74 69 6d or a run-tim
2e350 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a e error occurs..
2e360 2a 2a 0a 2a 2a 20 7b 48 31 35 33 30 34 7d 20 20 **.** {H15304}
2e370 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b When a call to [
2e380 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d sqlite3_step(S)]
2e390 20 63 61 75 73 65 73 20 74 68 65 20 5b 70 72 65 causes the [pre
2e3a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
2e3b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 53 20 .** S
2e3c0 74 6f 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 to run to comple
2e3d0 74 69 6f 6e 2c 20 74 68 65 20 66 75 6e 63 74 69 tion, the functi
2e3e0 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 on returns [SQLI
2e3f0 54 45 5f 44 4f 4e 45 5d 2e 0a 2a 2a 0a 2a 2a 20 TE_DONE]..**.**
2e400 7b 48 31 35 33 30 36 7d 20 20 57 68 65 6e 20 61 {H15306} When a
2e410 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
2e420 33 5f 73 74 65 70 28 53 29 5d 20 73 74 6f 70 73 3_step(S)] stops
2e430 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 72 because it is r
2e440 65 61 64 79 20 74 6f 0a 2a 2a 20 20 20 20 20 20 eady to.**
2e450 20 20 20 20 20 72 65 74 75 72 6e 20 61 6e 6f 74 return anot
2e460 68 65 72 20 72 6f 77 20 6f 66 20 74 68 65 20 72 her row of the r
2e470 65 73 75 6c 74 20 73 65 74 2c 20 69 74 20 72 65 esult set, it re
2e480 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 4f turns [SQLITE_RO
2e490 57 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 30 W]..**.** {H1530
2e4a0 38 7d 20 20 49 66 20 61 20 63 61 6c 6c 20 74 6f 8} If a call to
2e4b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 [sqlite3_step(S
2e4c0 29 5d 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 6e )] encounters an
2e4d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 .** [s
2e4e0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
2e4f0 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 20 6f 72 | interrupt] or
2e500 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f a run-time erro
2e510 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 r,.**
2e520 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20 61 70 it returns an ap
2e530 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 propriate error
2e540 63 6f 64 65 20 74 68 61 74 20 69 73 20 6e 6f 74 code that is not
2e550 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 one of.**
2e560 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d [SQLITE_OK]
2e570 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 , [SQLITE_ROW],
2e580 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d or [SQLITE_DONE]
2e590 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 31 30 7d ..**.** {H15310}
2e5a0 20 20 49 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 If an [sqlite3
2e5b0 5f 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74 _interrupt | int
2e5c0 65 72 72 75 70 74 5d 20 6f 72 20 61 20 72 75 6e errupt] or a run
2e5d0 2d 74 69 6d 65 20 65 72 72 6f 72 0a 2a 2a 20 20 -time error.**
2e5e0 20 20 20 20 20 20 20 20 20 6f 63 63 75 72 73 20 occurs
2e5f0 64 75 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f during a call to
2e600 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 [sqlite3_step(S
2e610 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 )].**
2e620 66 6f 72 20 61 20 5b 70 72 65 70 61 72 65 64 20 for a [prepared
2e630 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 63 72 65 statement] S cre
2e640 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 ated using.**
2e650 20 20 20 20 20 20 20 20 6c 65 67 61 63 79 20 69 legacy i
2e660 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69 74 nterfaces [sqlit
2e670 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 e3_prepare()] or
2e680 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 .** [s
2e690 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
2e6a0 28 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 66 75 ()], then the fu
2e6b0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 65 nction returns e
2e6c0 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 ither.**
2e6d0 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 [SQLITE_ERROR
2e6e0 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d ], [SQLITE_BUSY]
2e6f0 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53 , or [SQLITE_MIS
2e700 55 53 45 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f USE]..*/.SQLITE_
2e710 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
2e720 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d step(sqlite3_stm
2e730 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 t*);../*.** CAPI
2e740 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20 3REF: Number of
2e750 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65 73 columns in a res
2e760 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30 7d ult set {H13770}
2e770 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
2e780 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 Returns the numb
2e790 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e 20 er of values in
2e7a0 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 the current row
2e7b0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
2e7c0 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 t..**.** INVARIA
2e7d0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 NTS:.**.** {H137
2e7e0 37 31 7d 20 20 41 66 74 65 72 20 61 20 63 61 6c 71} After a cal
2e7f0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 l to [sqlite3_st
2e800 65 70 28 53 29 5d 20 74 68 61 74 20 72 65 74 75 ep(S)] that retu
2e810 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d rns [SQLITE_ROW]
2e820 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 ,.** t
2e830 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 61 74 61 he [sqlite3_data
2e840 5f 63 6f 75 6e 74 28 53 29 5d 20 72 6f 75 74 69 _count(S)] routi
2e850 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 74 ne will return t
2e860 68 65 20 73 61 6d 65 20 76 61 6c 75 65 0a 2a 2a he same value.**
2e870 20 20 20 20 20 20 20 20 20 20 20 61 73 20 74 68 as th
2e880 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d e [sqlite3_colum
2e890 6e 5f 63 6f 75 6e 74 28 53 29 5d 20 66 75 6e 63 n_count(S)] func
2e8a0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 tion..**.** {H13
2e8b0 37 37 32 7d 20 20 41 66 74 65 72 20 5b 73 71 6c 772} After [sql
2e8c0 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61 ite3_step(S)] ha
2e8d0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 79 20 76 s returned any v
2e8e0 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e 0a alue other than.
2e8f0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 53 51 ** [SQ
2e900 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 62 65 66 LITE_ROW] or bef
2e910 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 ore [sqlite3_ste
2e920 70 28 53 29 5d 20 68 61 73 20 62 65 65 6e 20 63 p(S)] has been c
2e930 61 6c 6c 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 alled on the.**
2e940 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 [prepa
2e950 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 66 red statement] f
2e960 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 6d or the first tim
2e970 65 20 73 69 6e 63 65 20 69 74 20 77 61 73 0a 2a e since it was.*
2e980 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c * [sql
2e990 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 ite3_prepare | p
2e9a0 72 65 70 61 72 65 64 5d 20 6f 72 20 5b 73 71 6c repared] or [sql
2e9b0 69 74 65 33 5f 72 65 73 65 74 20 7c 20 72 65 73 ite3_reset | res
2e9c0 65 74 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 et],.**
2e9d0 20 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 the [sqlite3_d
2e9e0 61 74 61 5f 63 6f 75 6e 74 28 53 29 5d 20 72 6f ata_count(S)] ro
2e9f0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 7a 65 utine returns ze
2ea00 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ro..*/.SQLITE_AP
2ea10 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 61 I int sqlite3_da
2ea20 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 ta_count(sqlite3
2ea30 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a _stmt *pStmt);..
2ea40 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2ea50 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74 61 Fundamental Data
2ea60 74 79 70 65 73 20 7b 48 31 30 32 36 35 7d 20 3c types {H10265} <
2ea70 53 31 30 31 31 30 3e 3c 53 31 30 31 32 30 3e 0a S10110><S10120>.
2ea80 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 4c ** KEYWORDS: SQL
2ea90 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a 2a 20 7b ITE_TEXT.**.** {
2eaa0 48 31 30 32 36 36 7d 20 45 76 65 72 79 20 76 61 H10266} Every va
2eab0 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20 68 61 lue in SQLite ha
2eac0 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20 66 75 s one of five fu
2ead0 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61 74 79 ndamental dataty
2eae0 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a pes:.**.** <ul>.
2eaf0 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 73 ** <li> 64-bit s
2eb00 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
2eb10 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49 45 45 <li> 64-bit IEE
2eb20 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 E floating point
2eb30 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e 20 number.** <li>
2eb40 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20 42 string.** <li> B
2eb50 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c 4c LOB.** <li> NULL
2eb60 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d 0a .** </ul> {END}.
2eb70 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 **.** These cons
2eb80 74 61 6e 74 73 20 61 72 65 20 63 6f 64 65 73 20 tants are codes
2eb90 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 6f 73 for each of thos
2eba0 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e e types..**.** N
2ebb0 6f 74 65 20 74 68 61 74 20 74 68 65 20 53 51 4c ote that the SQL
2ebc0 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 74 61 6e ITE_TEXT constan
2ebd0 74 20 77 61 73 20 61 6c 73 6f 20 75 73 65 64 20 t was also used
2ebe0 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f in SQLite versio
2ebf0 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d n 2.** for a com
2ec00 70 6c 65 74 65 6c 79 20 64 69 66 66 65 72 65 6e pletely differen
2ec10 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66 74 t meaning. Soft
2ec20 77 61 72 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 ware that links
2ec30 61 67 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a 20 against both.**
2ec40 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32 SQLite version 2
2ec50 20 61 6e 64 20 53 51 4c 69 74 65 20 76 65 72 73 and SQLite vers
2ec60 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 75 73 65 ion 3 should use
2ec70 20 53 51 4c 49 54 45 33 5f 54 45 58 54 2c 20 6e SQLITE3_TEXT, n
2ec80 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 58 ot.** SQLITE_TEX
2ec90 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 T..*/.#define SQ
2eca0 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 20 31 0a LITE_INTEGER 1.
2ecb0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 #define SQLITE_F
2ecc0 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 66 69 6e LOAT 2.#defin
2ecd0 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20 20 e SQLITE_BLOB
2ece0 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 4.#define SQLI
2ecf0 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23 69 TE_NULL 5.#i
2ed00 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54 fdef SQLITE_TEXT
2ed10 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f .# undef SQLITE_
2ed20 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 64 65 66 TEXT.#else.# def
2ed30 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 58 54 20 ine SQLITE_TEXT
2ed40 20 20 20 20 33 0a 23 65 6e 64 69 66 0a 23 64 65 3.#endif.#de
2ed50 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 54 45 58 fine SQLITE3_TEX
2ed60 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 43 T 3../*.** C
2ed70 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20 API3REF: Result
2ed80 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 20 51 75 Values From A Qu
2ed90 65 72 79 20 7b 48 31 33 38 30 30 7d 20 3c 53 31 ery {H13800} <S1
2eda0 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0700>.** KEYWORD
2edb0 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 S: {column acces
2edc0 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a s functions}.**.
2edd0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
2ede0 73 20 66 6f 72 6d 20 74 68 65 20 22 72 65 73 75 s form the "resu
2edf0 6c 74 20 73 65 74 20 71 75 65 72 79 22 20 69 6e lt set query" in
2ee00 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 terface..**.** T
2ee10 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 hese routines re
2ee20 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e turn information
2ee30 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 20 about a single
2ee40 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 75 column of the cu
2ee50 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 6c 74 20 rrent.** result
2ee60 72 6f 77 20 6f 66 20 61 20 71 75 65 72 79 2e 20 row of a query.
2ee70 20 49 6e 20 65 76 65 72 79 20 63 61 73 65 20 74 In every case t
2ee80 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
2ee90 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a t is a pointer.*
2eea0 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 70 61 72 * to the [prepar
2eeb0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 ed statement] th
2eec0 61 74 20 69 73 20 62 65 69 6e 67 20 65 76 61 6c at is being eval
2eed0 75 61 74 65 64 20 28 74 68 65 20 5b 73 71 6c 69 uated (the [sqli
2eee0 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a 20 74 68 te3_stmt*].** th
2eef0 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 at was returned
2ef00 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 70 72 from [sqlite3_pr
2ef10 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 6f epare_v2()] or o
2ef20 6e 65 20 6f 66 20 69 74 73 20 76 61 72 69 61 6e ne of its varian
2ef30 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 ts).** and the s
2ef40 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
2ef50 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 s the index of t
2ef60 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 68 he column for wh
2ef70 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a ich information.
2ef80 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 ** should be ret
2ef90 75 72 6e 65 64 2e 20 20 54 68 65 20 6c 65 66 74 urned. The left
2efa0 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 most column of t
2efb0 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 68 61 he result set ha
2efc0 73 20 74 68 65 20 69 6e 64 65 78 20 30 2e 0a 2a s the index 0..*
2efd0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 *.** If the SQL
2efe0 73 74 61 74 65 6d 65 6e 74 20 64 6f 65 73 20 6e statement does n
2eff0 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 ot currently poi
2f000 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f nt to a valid ro
2f010 77 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 w, or if the.**
2f020 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 73 20 column index is
2f030 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 out of range, th
2f040 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e 64 65 e result is unde
2f050 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 fined..** These
2f060 72 6f 75 74 69 6e 65 73 20 6d 61 79 20 6f 6e 6c routines may onl
2f070 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e y be called when
2f080 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
2f090 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c call to.** [sql
2f0a0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 68 61 73 ite3_step()] has
2f0b0 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 returned [SQLIT
2f0c0 45 5f 52 4f 57 5d 20 61 6e 64 20 6e 65 69 74 68 E_ROW] and neith
2f0d0 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 er.** [sqlite3_r
2f0e0 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b 73 71 6c eset()] nor [sql
2f0f0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d ite3_finalize()]
2f100 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 have been calle
2f110 64 20 73 75 62 73 65 71 75 65 6e 74 6c 79 2e 0a d subsequently..
2f120 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 ** If any of the
2f130 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 se routines are
2f140 63 61 6c 6c 65 64 20 61 66 74 65 72 20 5b 73 71 called after [sq
2f150 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f lite3_reset()] o
2f160 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 r.** [sqlite3_fi
2f170 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 nalize()] or aft
2f180 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 er [sqlite3_step
2f190 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 ()] has returned
2f1a0 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 .** something ot
2f1b0 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 her than [SQLITE
2f1c0 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65 73 75 6c _ROW], the resul
2f1d0 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 ts are undefined
2f1e0 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 33 ..** If [sqlite3
2f1f0 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c _step()] or [sql
2f200 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 ite3_reset()] or
2f210 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
2f220 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20 63 61 6c ze()].** are cal
2f230 6c 65 64 20 66 72 6f 6d 20 61 20 64 69 66 66 65 led from a diffe
2f240 72 65 6e 74 20 74 68 72 65 61 64 20 77 68 69 6c rent thread whil
2f250 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72 e any of these r
2f260 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 70 outines.** are p
2f270 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 ending, then the
2f280 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 results are und
2f290 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 efined..**.** Th
2f2a0 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e e sqlite3_column
2f2b0 5f 74 79 70 65 28 29 20 72 6f 75 74 69 6e 65 20 _type() routine
2f2c0 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 5b returns the.** [
2f2d0 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c SQLITE_INTEGER |
2f2e0 20 64 61 74 61 74 79 70 65 20 63 6f 64 65 5d 20 datatype code]
2f2f0 66 6f 72 20 74 68 65 20 69 6e 69 74 69 61 6c 20 for the initial
2f300 64 61 74 61 20 74 79 70 65 0a 2a 2a 20 6f 66 20 data type.** of
2f310 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d the result colum
2f320 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 n. The returned
2f330 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f 66 value is one of
2f340 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 [SQLITE_INTEGER
2f350 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 4c ],.** [SQLITE_FL
2f360 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 OAT], [SQLITE_TE
2f370 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 4c 4f XT], [SQLITE_BLO
2f380 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4e B], or [SQLITE_N
2f390 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 ULL]. The value
2f3a0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79 20 .** returned by
2f3b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
2f3c0 79 70 65 28 29 20 69 73 20 6f 6e 6c 79 20 6d 65 ype() is only me
2f3d0 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e 6f 20 74 aningful if no t
2f3e0 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f ype.** conversio
2f3f0 6e 73 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 ns have occurred
2f400 20 61 73 20 64 65 73 63 72 69 62 65 64 20 62 65 as described be
2f410 6c 6f 77 2e 20 20 41 66 74 65 72 20 61 20 74 79 low. After a ty
2f420 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 0a 2a pe conversion,.*
2f430 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 * the value retu
2f440 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
2f450 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 column_type() is
2f460 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 46 75 74 undefined. Fut
2f470 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 ure.** versions
2f480 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68 of SQLite may ch
2f490 61 6e 67 65 20 74 68 65 20 62 65 68 61 76 69 6f ange the behavio
2f4a0 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c r of sqlite3_col
2f4b0 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a 20 66 6f umn_type().** fo
2f4c0 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70 65 20 63 llowing a type c
2f4d0 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a onversion..**.**
2f4e0 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 If the result i
2f4f0 73 20 61 20 42 4c 4f 42 20 6f 72 20 55 54 46 2d s a BLOB or UTF-
2f500 38 20 73 74 72 69 6e 67 20 74 68 65 6e 20 74 68 8 string then th
2f510 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e e sqlite3_column
2f520 5f 62 79 74 65 73 28 29 0a 2a 2a 20 72 6f 75 74 _bytes().** rout
2f530 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ine returns the
2f540 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
2f550 69 6e 20 74 68 61 74 20 42 4c 4f 42 20 6f 72 20 in that BLOB or
2f560 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66 20 74 68 string..** If th
2f570 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 55 54 e result is a UT
2f580 46 2d 31 36 20 73 74 72 69 6e 67 2c 20 74 68 65 F-16 string, the
2f590 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e n sqlite3_column
2f5a0 5f 62 79 74 65 73 28 29 20 63 6f 6e 76 65 72 74 _bytes() convert
2f5b0 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 s.** the string
2f5c0 74 6f 20 55 54 46 2d 38 20 61 6e 64 20 74 68 65 to UTF-8 and the
2f5d0 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 n returns the nu
2f5e0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 2e 0a 2a mber of bytes..*
2f5f0 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 * If the result
2f600 69 73 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c is a numeric val
2f610 75 65 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f ue then sqlite3_
2f620 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 75 column_bytes() u
2f630 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ses.** [sqlite3_
2f640 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 63 snprintf()] to c
2f650 6f 6e 76 65 72 74 20 74 68 61 74 20 76 61 6c 75 onvert that valu
2f660 65 20 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72 e to a UTF-8 str
2f670 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 0a ing and returns.
2f680 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
2f690 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 73 bytes in that s
2f6a0 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 76 61 tring..** The va
2f6b0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 64 6f 65 lue returned doe
2f6c0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 s not include th
2f6d0 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f e zero terminato
2f6e0 72 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 r at the end.**
2f6f0 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 of the string.
2f700 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 74 68 65 For clarity: the
2f710 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
2f720 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
2f730 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 65 .** bytes in the
2f740 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 74 68 65 string, not the
2f750 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 number of chara
2f760 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 72 cters..**.** Str
2f770 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 ings returned by
2f780 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
2f790 74 65 78 74 28 29 20 61 6e 64 20 73 71 6c 69 74 text() and sqlit
2f7a0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
2f7b0 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65 6d 70 74 (),.** even empt
2f7c0 79 20 73 74 72 69 6e 67 73 2c 20 61 72 65 20 61 y strings, are a
2f7d0 6c 77 61 79 73 20 7a 65 72 6f 20 74 65 72 6d 69 lways zero termi
2f7e0 6e 61 74 65 64 2e 20 20 54 68 65 20 72 65 74 75 nated. The retu
2f7f0 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66 72 6f 6d rn.** value from
2f800 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
2f810 62 6c 6f 62 28 29 20 66 6f 72 20 61 20 7a 65 72 blob() for a zer
2f820 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 20 69 73 o-length BLOB is
2f830 20 61 6e 20 61 72 62 69 74 72 61 72 79 0a 2a 2a an arbitrary.**
2f840 20 70 6f 69 6e 74 65 72 2c 20 70 6f 73 73 69 62 pointer, possib
2f850 6c 79 20 65 76 65 6e 20 61 20 4e 55 4c 4c 20 70 ly even a NULL p
2f860 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ointer..**.** Th
2f870 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e e sqlite3_column
2f880 5f 62 79 74 65 73 31 36 28 29 20 72 6f 75 74 69 _bytes16() routi
2f890 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f ne is similar to
2f8a0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
2f8b0 62 79 74 65 73 28 29 0a 2a 2a 20 62 75 74 20 6c bytes().** but l
2f8c0 65 61 76 65 73 20 74 68 65 20 72 65 73 75 6c 74 eaves the result
2f8d0 20 69 6e 20 55 54 46 2d 31 36 20 69 6e 20 6e 61 in UTF-16 in na
2f8e0 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 tive byte order
2f8f0 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46 2d 38 instead of UTF-8
2f900 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 20 74 65 ..** The zero te
2f910 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e 6f 74 20 rminator is not
2f920 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 69 73 included in this
2f930 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 count..**.** Th
2f940 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 e object returne
2f950 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f d by [sqlite3_co
2f960 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 lumn_value()] is
2f970 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 an.** [unprotec
2f980 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
2f990 65 5d 20 6f 62 6a 65 63 74 2e 20 20 41 6e 20 75 e] object. An u
2f9a0 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 nprotected sqlit
2f9b0 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a e3_value object.
2f9c0 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 ** may only be u
2f9d0 73 65 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65 sed with [sqlite
2f9e0 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 20 3_bind_value()]
2f9f0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 and [sqlite3_res
2fa00 75 6c 74 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a ult_value()]..**
2fa10 20 49 66 20 74 68 65 20 5b 75 6e 70 72 6f 74 65 If the [unprote
2fa20 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
2fa30 75 65 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 72 ue] object retur
2fa40 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 ned by.** [sqlit
2fa50 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 e3_column_value(
2fa60 29 5d 20 69 73 20 75 73 65 64 20 69 6e 20 61 6e )] is used in an
2fa70 79 20 6f 74 68 65 72 20 77 61 79 2c 20 69 6e 63 y other way, inc
2fa80 6c 75 64 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20 luding calls.**
2fa90 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 to routines like
2faa0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
2fab0 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 int()], [sqlite3
2fac0 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a _value_text()],.
2fad0 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 ** or [sqlite3_v
2fae0 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 74 alue_bytes()], t
2faf0 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 hen the behavior
2fb00 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
2fb10 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
2fb20 6e 65 73 20 61 74 74 65 6d 70 74 20 74 6f 20 63 nes attempt to c
2fb30 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c 75 65 onvert the value
2fb40 20 77 68 65 72 65 20 61 70 70 72 6f 70 72 69 61 where appropria
2fb50 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d te. For.** exam
2fb60 70 6c 65 2c 20 69 66 20 74 68 65 20 69 6e 74 65 ple, if the inte
2fb70 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 rnal representat
2fb80 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20 61 6e 64 ion is FLOAT and
2fb90 20 61 20 74 65 78 74 20 72 65 73 75 6c 74 0a 2a a text result.*
2fba0 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 * is requested,
2fbb0 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 [sqlite3_snprint
2fbc0 66 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e 74 f()] is used int
2fbd0 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65 72 66 6f ernally to perfo
2fbe0 72 6d 20 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 72 rm the.** conver
2fbf0 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c sion automatical
2fc00 6c 79 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ly. The followi
2fc10 6e 67 20 74 61 62 6c 65 20 64 65 74 61 69 6c 73 ng table details
2fc20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 the conversions
2fc30 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 61 70 70 .** that are app
2fc40 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f lied:.**.** <blo
2fc50 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 ckquote>.** <tab
2fc60 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a le border="1">.*
2fc70 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e 74 65 72 * <tr><th> Inter
2fc80 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e nal<br>Type <th>
2fc90 20 52 65 71 75 65 73 74 65 64 3c 62 72 3e 54 79 Requested<br>Ty
2fca0 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76 65 72 73 pe <th> Convers
2fcb0 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 ion.**.** <tr><t
2fcc0 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e d> NULL <td>
2fcd0 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 INTEGER <td>
2fce0 52 65 73 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c Result is 0.** <
2fcf0 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 tr><td> NULL
2fd00 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 <td> FLOAT
2fd10 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 <td> Result is 0
2fd20 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 .0.** <tr><td>
2fd30 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 54 NULL <td> T
2fd40 45 58 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75 EXT <td> Resu
2fd50 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 lt is NULL point
2fd60 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 er.** <tr><td>
2fd70 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 42 NULL <td> B
2fd80 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 65 73 75 LOB <td> Resu
2fd90 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 lt is NULL point
2fda0 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 er.** <tr><td> I
2fdb0 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 46 4c NTEGER <td> FL
2fdc0 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 OAT <td> Conv
2fdd0 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 67 65 72 ert from integer
2fde0 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 to float.** <tr
2fdf0 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c ><td> INTEGER <
2fe00 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 td> TEXT <t
2fe10 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 d> ASCII renderi
2fe20 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 ng of the intege
2fe30 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e r.** <tr><td> IN
2fe40 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 42 4c TEGER <td> BL
2fe50 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 OB <td> Same
2fe60 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 45 58 54 as INTEGER->TEXT
2fe70 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c .** <tr><td> FL
2fe80 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 OAT <td> INTEG
2fe90 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 ER <td> Conver
2fea0 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 74 6f 20 t from float to
2feb0 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c integer.** <tr><
2fec0 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 td> FLOAT <td
2fed0 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e > TEXT <td>
2fee0 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67 ASCII rendering
2fef0 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 0a 2a 2a of the float.**
2ff00 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 <tr><td> FLOAT
2ff10 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 <td> BLOB
2ff20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 46 <td> Same as F
2ff30 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 LOAT->TEXT.** <t
2ff40 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20 r><td> TEXT
2ff50 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c <td> INTEGER <
2ff60 74 64 3e 20 55 73 65 20 61 74 6f 69 28 29 0a 2a td> Use atoi().*
2ff70 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 * <tr><td> TEXT
2ff80 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <td> FLOAT
2ff90 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 66 <td> Use atof
2ffa0 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 ().** <tr><td>
2ffb0 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 20 42 TEXT <td> B
2ffc0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e 6f 20 63 LOB <td> No c
2ffd0 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 hange.** <tr><td
2ffe0 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 > BLOB <td>
2fff0 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 INTEGER <td> C
30000 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20 74 onvert to TEXT t
30010 68 65 6e 20 75 73 65 20 61 74 6f 69 28 29 0a 2a hen use atoi().*
30020 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 * <tr><td> BLOB
30030 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <td> FLOAT
30040 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 <td> Convert
30050 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73 65 to TEXT then use
30060 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c atof().** <tr><
30070 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 td> BLOB <td
30080 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e > TEXT <td>
30090 20 41 64 64 20 61 20 7a 65 72 6f 20 74 65 72 6d Add a zero term
300a0 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 64 65 64 inator if needed
300b0 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 .** </table>.**
300c0 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a </blockquote>.**
300d0 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61 62 .** The table ab
300e0 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 65 72 65 ove makes refere
300f0 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 72 64 20 nce to standard
30100 43 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 C library functi
30110 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a 20 61 6e ons atoi().** an
30120 64 20 61 74 6f 66 28 29 2e 20 20 53 51 4c 69 74 d atof(). SQLit
30130 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c e does not reall
30140 79 20 75 73 65 20 74 68 65 73 65 20 66 75 6e 63 y use these func
30150 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 73 20 69 tions. It has i
30160 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75 69 76 61 ts.** own equiva
30170 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c 20 72 6f lent internal ro
30180 75 74 69 6e 65 73 2e 20 20 54 68 65 20 61 74 6f utines. The ato
30190 69 28 29 20 61 6e 64 20 61 74 6f 66 28 29 20 6e i() and atof() n
301a0 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 ames are.** used
301b0 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 66 6f in the table fo
301c0 72 20 62 72 65 76 69 74 79 20 61 6e 64 20 62 65 r brevity and be
301d0 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20 66 cause they are f
301e0 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f 73 74 0a amiliar to most.
301f0 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d 65 72 73 ** C programmers
30200 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
30210 74 20 77 68 65 6e 20 74 79 70 65 20 63 6f 6e 76 t when type conv
30220 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 2c 20 70 ersions occur, p
30230 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 ointers returned
30240 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c by prior.** cal
30250 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f ls to sqlite3_co
30260 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 73 71 6c lumn_blob(), sql
30270 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
30280 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71 (), and/or.** sq
30290 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
302a0 74 31 36 28 29 20 6d 61 79 20 62 65 20 69 6e 76 t16() may be inv
302b0 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20 54 79 70 alidated..** Typ
302c0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 6e e conversions an
302d0 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 61 6c 69 d pointer invali
302e0 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 6f 63 dations might oc
302f0 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f cur.** in the fo
30300 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 3a 0a 2a llowing cases:.*
30310 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
30320 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f > The initial co
30330 6e 74 65 6e 74 20 69 73 20 61 20 42 4c 4f 42 20 ntent is a BLOB
30340 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 and sqlite3_colu
30350 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 2a 2a 20 mn_text() or.**
30360 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c sqlite3_col
30370 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 umn_text16() is
30380 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65 72 6f 2d called. A zero-
30390 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69 67 68 74 terminator might
303a0 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64 20 74 6f .** need to
303b0 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 be added to the
303c0 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a string.</li>.**
303d0 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 <li> The initia
303e0 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 l content is UTF
303f0 2d 38 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69 -8 text and sqli
30400 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
30410 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 16() or.**
30420 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
30430 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c 65 ext16() is calle
30440 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 d. The content
30450 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 must be converte
30460 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46 d.** to UTF
30470 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 -16.</li>.** <li
30480 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f > The initial co
30490 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 31 36 20 ntent is UTF-16
304a0 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33 text and sqlite3
304b0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 _column_bytes()
304c0 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 or.** sqlit
304d0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 e3_column_text()
304e0 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 is called. The
304f0 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 content must be
30500 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20 converted.**
30510 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c 2f 6c 69 to UTF-8.</li
30520 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a >.** </ul>.**.**
30530 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 62 65 74 Conversions bet
30540 77 65 65 6e 20 55 54 46 2d 31 36 62 65 20 61 6e ween UTF-16be an
30550 64 20 55 54 46 2d 31 36 6c 65 20 61 72 65 20 61 d UTF-16le are a
30560 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e 20 70 6c lways done in pl
30570 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a 20 6e 6f ace and do.** no
30580 74 20 69 6e 76 61 6c 69 64 61 74 65 20 61 20 70 t invalidate a p
30590 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c 20 74 68 rior pointer, th
305a0 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74 ough of course t
305b0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 he content of th
305c0 65 20 62 75 66 66 65 72 0a 2a 2a 20 74 68 61 74 e buffer.** that
305d0 20 74 68 65 20 70 72 69 6f 72 20 70 6f 69 6e 74 the prior point
305e0 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 77 69 6c er points to wil
305f0 6c 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 l have been modi
30600 66 69 65 64 2e 20 20 4f 74 68 65 72 20 6b 69 6e fied. Other kin
30610 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e 76 65 72 73 ds.** of convers
30620 69 6f 6e 20 61 72 65 20 64 6f 6e 65 20 69 6e 20 ion are done in
30630 70 6c 61 63 65 20 77 68 65 6e 20 69 74 20 69 73 place when it is
30640 20 70 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 73 possible, but s
30650 6f 6d 65 74 69 6d 65 73 20 74 68 65 79 0a 2a 2a ometimes they.**
30660 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c are not possibl
30670 65 20 61 6e 64 20 69 6e 20 74 68 6f 73 65 20 63 e and in those c
30680 61 73 65 73 20 70 72 69 6f 72 20 70 6f 69 6e 74 ases prior point
30690 65 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 ers are invalida
306a0 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ted..**.** The s
306b0 61 66 65 73 74 20 61 6e 64 20 65 61 73 69 65 73 afest and easies
306c0 74 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 70 6f t to remember po
306d0 6c 69 63 79 20 69 73 20 74 6f 20 69 6e 76 6f 6b licy is to invok
306e0 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 e these routines
306f0 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 .** in one of th
30700 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 61 79 73 e following ways
30710 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
30720 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c <li>sqlite3_col
30730 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f 6c 6c 6f umn_text() follo
30740 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 wed by sqlite3_c
30750 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c olumn_bytes()</l
30760 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 i>.** <li>sqlit
30770 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 e3_column_blob()
30780 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c followed by sql
30790 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
307a0 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 s()</li>.** <li
307b0 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f >sqlite3_column_
307c0 74 65 78 74 31 36 28 29 20 66 6f 6c 6c 6f 77 65 text16() followe
307d0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c d by sqlite3_col
307e0 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 3c 2f 6c umn_bytes16()</l
307f0 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a i>.** </ul>.**.*
30800 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
30810 2c 20 79 6f 75 20 73 68 6f 75 6c 64 20 63 61 6c , you should cal
30820 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e l sqlite3_column
30830 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 73 71 6c 69 _text(),.** sqli
30840 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
30850 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f ), or sqlite3_co
30860 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 69 lumn_text16() fi
30870 72 73 74 20 74 6f 20 66 6f 72 63 65 20 74 68 65 rst to force the
30880 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 74 6f 20 result.** into
30890 74 68 65 20 64 65 73 69 72 65 64 20 66 6f 72 6d the desired form
308a0 61 74 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 at, then invoke
308b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
308c0 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 73 71 6c ytes() or.** sql
308d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
308e0 73 31 36 28 29 20 74 6f 20 66 69 6e 64 20 74 68 s16() to find th
308f0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 72 65 e size of the re
30900 73 75 6c 74 2e 20 20 44 6f 20 6e 6f 74 20 6d 69 sult. Do not mi
30910 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 x calls.** to sq
30920 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
30930 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 t() or sqlite3_c
30940 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 77 69 74 olumn_blob() wit
30950 68 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 h calls to.** sq
30960 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
30970 65 73 31 36 28 29 2c 20 61 6e 64 20 64 6f 20 6e es16(), and do n
30980 6f 74 20 6d 69 78 20 63 61 6c 6c 73 20 74 6f 20 ot mix calls to
30990 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
309a0 65 78 74 31 36 28 29 0a 2a 2a 20 77 69 74 68 20 ext16().** with
309b0 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
309c0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 2e _column_bytes().
309d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 .**.** The point
309e0 65 72 73 20 72 65 74 75 72 6e 65 64 20 61 72 65 ers returned are
309f0 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 61 20 74 valid until a t
30a00 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f ype conversion o
30a10 63 63 75 72 73 20 61 73 0a 2a 2a 20 64 65 73 63 ccurs as.** desc
30a20 72 69 62 65 64 20 61 62 6f 76 65 2c 20 6f 72 20 ribed above, or
30a30 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f 73 until [sqlite3_s
30a40 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 tep()] or [sqlit
30a50 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a e3_reset()] or.*
30a60 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c * [sqlite3_final
30a70 69 7a 65 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 ize()] is called
30a80 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 73 70 . The memory sp
30a90 61 63 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 ace used to hold
30aa0 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61 6e 64 20 strings.** and
30ab0 42 4c 4f 42 73 20 69 73 20 66 72 65 65 64 20 61 BLOBs is freed a
30ac0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 44 utomatically. D
30ad0 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20 70 61 73 o <b>not</b> pas
30ae0 73 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20 72 s the pointers r
30af0 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73 71 6c 69 eturned.** [sqli
30b00 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
30b10 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c )], [sqlite3_col
30b20 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 65 74 63 umn_text()], etc
30b30 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 . into.** [sqlit
30b40 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a e3_free()]..**.*
30b50 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c * If a memory al
30b60 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f location error o
30b70 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 65 ccurs during the
30b80 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 61 evaluation of a
30b90 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 72 ny.** of these r
30ba0 6f 75 74 69 6e 65 73 2c 20 61 20 64 65 66 61 75 outines, a defau
30bb0 6c 74 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 lt value is retu
30bc0 72 6e 65 64 2e 20 20 54 68 65 20 64 65 66 61 75 rned. The defau
30bd0 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 65 lt value.** is e
30be0 69 74 68 65 72 20 74 68 65 20 69 6e 74 65 67 65 ither the intege
30bf0 72 20 30 2c 20 74 68 65 20 66 6c 6f 61 74 69 6e r 0, the floatin
30c00 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 20 30 g point number 0
30c10 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c 0a 2a 2a .0, or a NULL.**
30c20 20 70 6f 69 6e 74 65 72 2e 20 20 53 75 62 73 65 pointer. Subse
30c30 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b quent calls to [
30c40 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 sqlite3_errcode(
30c50 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a )] will return.*
30c60 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d * [SQLITE_NOMEM]
30c70 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
30c80 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 30 TS:.**.** {H1380
30c90 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 3} The [sqlite3_
30ca0 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 4e 29 column_blob(S,N)
30cb0 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 ] interface conv
30cc0 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 erts the.**
30cd0 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 Nth column
30ce0 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 in the current r
30cf0 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ow of the result
30d00 20 73 65 74 20 66 6f 72 0a 2a 2a 20 20 20 20 20 set for.**
30d10 20 20 20 20 20 74 68 65 20 5b 70 72 65 70 61 72 the [prepar
30d20 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 ed statement] S
30d30 69 6e 74 6f 20 61 20 42 4c 4f 42 20 61 6e 64 20 into a BLOB and
30d40 74 68 65 6e 20 72 65 74 75 72 6e 73 20 61 0a 2a then returns a.*
30d50 2a 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74 * point
30d60 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 er to the conver
30d70 74 65 64 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a ted value..**.**
30d80 20 7b 48 31 33 38 30 36 7d 20 54 68 65 20 5b 73 {H13806} The [s
30d90 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
30da0 74 65 73 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 tes(S,N)] interf
30db0 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a ace returns the.
30dc0 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 ** numb
30dd0 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 er of bytes in t
30de0 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e he BLOB or strin
30df0 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20 g (exclusive of
30e00 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
30e10 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 zero terminator
30e20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29 20 74 on the string) t
30e30 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 hat was returned
30e40 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 by the.**
30e50 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 most recent
30e60 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
30e70 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 4e _column_blob(S,N
30e80 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 )] or.**
30e90 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d [sqlite3_colum
30ea0 6e 5f 74 65 78 74 28 53 2c 4e 29 5d 2e 0a 2a 2a n_text(S,N)]..**
30eb0 0a 2a 2a 20 7b 48 31 33 38 30 39 7d 20 54 68 65 .** {H13809} The
30ec0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
30ed0 5f 62 79 74 65 73 31 36 28 53 2c 4e 29 5d 20 69 _bytes16(S,N)] i
30ee0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
30ef0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
30f00 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
30f10 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 20 28 in the string (
30f20 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 exclusive of the
30f30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65 72 .** zer
30f40 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 6f 6e 20 o terminator on
30f50 74 68 65 20 73 74 72 69 6e 67 29 20 74 68 61 74 the string) that
30f60 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 62 79 was returned by
30f70 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
30f80 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c most recent cal
30f90 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f l to [sqlite3_co
30fa0 6c 75 6d 6e 5f 74 65 78 74 31 36 28 53 2c 4e 29 lumn_text16(S,N)
30fb0 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 31 32 ]..**.** {H13812
30fc0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 } The [sqlite3_c
30fd0 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 53 2c 4e olumn_double(S,N
30fe0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e )] interface con
30ff0 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 verts the.**
31000 20 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d 6e Nth column
31010 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 in the current
31020 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c row of the resul
31030 74 20 73 65 74 20 66 6f 72 20 74 68 65 0a 2a 2a t set for the.**
31040 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 [prepa
31050 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 red statement] S
31060 20 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67 into a floating
31070 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 61 6e 64 point value and
31080 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 .** ret
31090 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 urns a copy of t
310a0 68 61 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a hat value..**.**
310b0 20 7b 48 31 33 38 31 35 7d 20 54 68 65 20 5b 73 {H13815} The [s
310c0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e qlite3_column_in
310d0 74 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 t(S,N)] interfac
310e0 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a e converts the.*
310f0 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 * Nth c
31100 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 olumn in the cur
31110 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 rent row of the
31120 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 74 result set for t
31130 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
31140 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
31150 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 36 34 2d nt] S into a 64-
31160 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
31170 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 er and.**
31180 20 20 20 72 65 74 75 72 6e 73 20 74 68 65 20 6c returns the l
31190 6f 77 65 72 20 33 32 20 62 69 74 73 20 6f 66 20 ower 32 bits of
311a0 74 68 61 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a that integer..**
311b0 0a 2a 2a 20 7b 48 31 33 38 31 38 7d 20 54 68 65 .** {H13818} The
311c0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
311d0 5f 69 6e 74 36 34 28 53 2c 4e 29 5d 20 69 6e 74 _int64(S,N)] int
311e0 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20 erface converts
311f0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
31200 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 Nth column in th
31210 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 e current row of
31220 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
31230 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 for the.**
31240 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 [prepared st
31250 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 74 6f 20 atement] S into
31260 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 a 64-bit signed
31270 69 6e 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20 integer and.**
31280 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 returns
31290 61 20 63 6f 70 79 20 6f 66 20 74 68 61 74 20 69 a copy of that i
312a0 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 nteger..**.** {H
312b0 31 33 38 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 13821} The [sqli
312c0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 te3_column_text(
312d0 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 S,N)] interface
312e0 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 converts the.**
312f0 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 6f 6c Nth col
31300 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 umn in the curre
31310 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65 nt row of the re
31320 73 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a 2a 20 sult set for.**
31330 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 the [pr
31340 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
31350 5d 20 53 20 69 6e 74 6f 20 61 20 7a 65 72 6f 2d ] S into a zero-
31360 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
31370 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 72 .** str
31380 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ing and returns
31390 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 a pointer to tha
313a0 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 t string..**.**
313b0 7b 48 31 33 38 32 34 7d 20 54 68 65 20 5b 73 71 {H13824} The [sq
313c0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
313d0 74 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 t16(S,N)] interf
313e0 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 ace converts the
313f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 .** Nth
31400 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 column in the c
31410 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 urrent row of th
31420 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 e result set for
31430 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
31440 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
31450 6d 65 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 7a ment] S into a z
31460 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 32 ero-terminated 2
31470 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 -byte.**
31480 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31 36 aligned UTF-16
31490 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
314a0 65 72 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 er string and re
314b0 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 turns.**
314c0 20 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 a pointer to t
314d0 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a hat string..**.*
314e0 2a 20 7b 48 31 33 38 32 37 7d 20 54 68 65 20 5b * {H13827} The [
314f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
31500 79 70 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 ype(S,N)] interf
31510 61 63 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 ace returns.**
31520 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 5b one of [
31530 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53 SQLITE_NULL], [S
31540 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 20 QLITE_INTEGER],
31550 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a [SQLITE_FLOAT],.
31560 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c ** [SQL
31570 49 54 45 5f 54 45 58 54 5d 2c 20 6f 72 20 5b 53 ITE_TEXT], or [S
31580 51 4c 49 54 45 5f 42 4c 4f 42 5d 20 61 73 20 61 QLITE_BLOB] as a
31590 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a ppropriate for.*
315a0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 4e * the N
315b0 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 th column in the
315c0 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 current row of
315d0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 the result set f
315e0 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 or.** t
315f0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
31600 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a tement] S..**.**
31610 20 7b 48 31 33 38 33 30 7d 20 54 68 65 20 5b 73 {H13830} The [s
31620 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 qlite3_column_va
31630 6c 75 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 lue(S,N)] interf
31640 61 63 65 20 72 65 74 75 72 6e 73 20 61 0a 2a 2a ace returns a.**
31650 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74 65 pointe
31660 72 20 74 6f 20 61 6e 20 5b 75 6e 70 72 6f 74 65 r to an [unprote
31670 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
31680 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 ue] object for t
31690 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e he.** N
316a0 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 th column in the
316b0 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 current row of
316c0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 the result set f
316d0 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 or.** t
316e0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
316f0 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 tement] S..*/.SQ
31700 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
31710 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
31720 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 umn_blob(sqlite3
31730 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
31740 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
31750 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
31760 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 _bytes(sqlite3_s
31770 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
31780 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
31790 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
317a0 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 ytes16(sqlite3_s
317b0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
317c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 .SQLITE_API doub
317d0 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d le sqlite3_colum
317e0 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 n_double(sqlite3
317f0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
31800 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
31810 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
31820 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d _int(sqlite3_stm
31830 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
31840 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
31850 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
31860 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c column_int64(sql
31870 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
31880 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 iCol);.SQLITE_AP
31890 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 I const unsigned
318a0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
318b0 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 olumn_text(sqlit
318c0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 e3_stmt*, int iC
318d0 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ol);.SQLITE_API
318e0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
318f0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
31900 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 6(sqlite3_stmt*,
31910 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 int iCol);.SQLI
31920 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
31930 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 e3_column_type(s
31940 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
31950 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f t iCol);.SQLITE_
31960 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 API sqlite3_valu
31970 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d e *sqlite3_colum
31980 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f n_value(sqlite3_
31990 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
319a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
319b0 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 F: Destroy A Pre
319c0 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 pared Statement
319d0 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20 Object {H13300}
319e0 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e <S70300><S30100>
319f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
31a00 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 e3_finalize() fu
31a10 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
31a20 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72 to delete a [pr
31a30 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
31a40 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 ]..** If the sta
31a50 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75 tement was execu
31a60 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 ted successfully
31a70 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64 or not executed
31a80 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a at all, then.**
31a90 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
31aa0 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75 turned. If execu
31ab0 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 tion of the stat
31ac0 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65 ement failed the
31ad0 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 n an.** [error c
31ae0 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 ode] or [extende
31af0 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 d error code] is
31b00 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
31b10 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
31b20 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 n be called at a
31b30 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 ny point during
31b40 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 the execution of
31b50 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 the.** [prepare
31b60 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 d statement]. I
31b70 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 f the virtual ma
31b80 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a chine has not.**
31b90 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 completed execu
31ba0 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72 tion when this r
31bb0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
31bc0 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a , that is like.*
31bd0 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61 * encountering a
31be0 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73 n error or an [s
31bf0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
31c00 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a | interrupt]..*
31c10 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64 * Incomplete upd
31c20 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c ates may be roll
31c30 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e ed back and tran
31c40 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65 sactions cancele
31c50 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 d,.** depending
31c60 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 on the circumsta
31c70 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a nces, and the.**
31c80 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 [error code] re
31c90 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b turned will be [
31ca0 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a SQLITE_ABORT]..*
31cb0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
31cc0 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 32 7d 20 .**.** {H11302}
31cd0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e The [sqlite3_fin
31ce0 61 6c 69 7a 65 28 53 29 5d 20 69 6e 74 65 72 66 alize(S)] interf
31cf0 61 63 65 20 64 65 73 74 72 6f 79 73 20 74 68 65 ace destroys the
31d00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 .** [pr
31d10 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
31d20 5d 20 53 20 61 6e 64 20 72 65 6c 65 61 73 65 73 ] S and releases
31d30 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 all.**
31d40 20 6d 65 6d 6f 72 79 20 61 6e 64 20 66 69 6c 65 memory and file
31d50 20 72 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20 resources held
31d60 62 79 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 0a by that object..
31d70 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 34 7d 20 49 **.** {H11304} I
31d80 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e f the most recen
31d90 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 t call to [sqlit
31da0 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 e3_step(S)] for
31db0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
31dc0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
31dd0 65 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20 ent] S returned
31de0 61 6e 20 65 72 72 6f 72 2c 0a 2a 2a 20 20 20 20 an error,.**
31df0 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c 69 then [sqli
31e00 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 53 29 5d te3_finalize(S)]
31e10 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 73 61 returns that sa
31e20 6d 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c me error..*/.SQL
31e30 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
31e40 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c te3_finalize(sql
31e50 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
31e60 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
31e70 45 46 3a 20 52 65 73 65 74 20 41 20 50 72 65 70 EF: Reset A Prep
31e80 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f ared Statement O
31e90 62 6a 65 63 74 20 7b 48 31 33 33 33 30 7d 20 3c bject {H13330} <
31ea0 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S70300>.**.** Th
31eb0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 e sqlite3_reset(
31ec0 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 ) function is ca
31ed0 6c 6c 65 64 20 74 6f 20 72 65 73 65 74 20 61 20 lled to reset a
31ee0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
31ef0 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 ent].** object b
31f00 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69 ack to its initi
31f10 61 6c 20 73 74 61 74 65 2c 20 72 65 61 64 79 20 al state, ready
31f20 74 6f 20 62 65 20 72 65 2d 65 78 65 63 75 74 65 to be re-execute
31f30 64 2e 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74 d..** Any SQL st
31f40 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 atement variable
31f50 73 20 74 68 61 74 20 68 61 64 20 76 61 6c 75 65 s that had value
31f60 73 20 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20 s bound to them
31f70 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71 using.** the [sq
31f80 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 lite3_bind_blob
31f90 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a | sqlite3_bind_*
31fa0 28 29 20 41 50 49 5d 20 72 65 74 61 69 6e 20 74 () API] retain t
31fb0 68 65 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 heir values..**
31fc0 55 73 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65 Use [sqlite3_cle
31fd0 61 72 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 74 ar_bindings()] t
31fe0 6f 20 72 65 73 65 74 20 74 68 65 20 62 69 6e 64 o reset the bind
31ff0 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 ings..**.** {H11
32000 33 33 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 332} The [sqlite
32010 33 5f 72 65 73 65 74 28 53 29 5d 20 69 6e 74 65 3_reset(S)] inte
32020 72 66 61 63 65 20 72 65 73 65 74 73 20 74 68 65 rface resets the
32030 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
32040 6d 65 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20 ment] S.**
32050 20 20 20 20 62 61 63 6b 20 74 6f 20 74 68 65 20 back to the
32060 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 beginning of its
32070 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 program..**.**
32080 7b 48 31 31 33 33 34 7d 20 49 66 20 74 68 65 20 {H11334} If the
32090 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c most recent call
320a0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 to [sqlite3_ste
320b0 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a p(S)] for the.**
320c0 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 [prepa
320d0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 red statement] S
320e0 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 returned [SQLIT
320f0 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 E_ROW] or [SQLIT
32100 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 E_DONE],.**
32110 20 20 20 20 20 6f 72 20 69 66 20 5b 73 71 6c 69 or if [sqli
32120 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73 te3_step(S)] has
32130 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 never before be
32140 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a en called on S,.
32150 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e ** then
32160 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
32170 53 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c S)] returns [SQL
32180 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b ITE_OK]..**.** {
32190 48 31 31 33 33 36 7d 20 49 66 20 74 68 65 20 6d H11336} If the m
321a0 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 ost recent call
321b0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 to [sqlite3_step
321c0 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 (S)] for the.**
321d0 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 [prepar
321e0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 ed statement] S
321f0 69 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72 72 indicated an err
32200 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 or, then.**
32210 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 [sqlite3_re
32220 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20 set(S)] returns
32230 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b an appropriate [
32240 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a error code]..**.
32250 2a 2a 20 7b 48 31 31 33 33 38 7d 20 54 68 65 20 ** {H11338} The
32260 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 [sqlite3_reset(S
32270 29 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 )] interface doe
32280 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 s not change the
32290 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 values.**
322a0 20 20 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69 of any [sqli
322b0 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 te3_bind_blob|bi
322c0 6e 64 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b ndings] on the [
322d0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
322e0 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nt] S..*/.SQLITE
322f0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
32300 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 _reset(sqlite3_s
32310 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a tmt *pStmt);../*
32320 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72 .** CAPI3REF: Cr
32330 65 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65 eate Or Redefine
32340 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b SQL Functions {
32350 48 31 36 31 30 30 7d 20 3c 53 32 30 32 30 30 3e H16100} <S20200>
32360 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 .** KEYWORDS: {f
32370 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e unction creation
32380 20 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 routines}.** KE
32390 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 YWORDS: {applica
323a0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c tion-defined SQL
323b0 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 function}.** KE
323c0 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 YWORDS: {applica
323d0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c tion-defined SQL
323e0 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a functions}.**.*
323f0 2a 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63 * These two func
32400 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 tions (collectiv
32410 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 ely known as "fu
32420 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 nction creation
32430 72 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 routines").** ar
32440 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53 51 e used to add SQ
32450 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 L functions or a
32460 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20 ggregates or to
32470 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 68 redefine the beh
32480 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 avior.** of exis
32490 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f ting SQL functio
324a0 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 ns or aggregates
324b0 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 . The only diff
324c0 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 erence between t
324d0 68 65 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 61 he.** two is tha
324e0 74 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 t the second par
324f0 61 6d 65 74 65 72 2c 20 74 68 65 20 6e 61 6d 65 ameter, the name
32500 20 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72 29 of the (scalar)
32510 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 function or.**
32520 61 67 67 72 65 67 61 74 65 2c 20 69 73 20 65 6e aggregate, is en
32530 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20 66 coded in UTF-8 f
32540 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 or sqlite3_creat
32550 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 e_function() and
32560 20 55 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 UTF-16.** for s
32570 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
32580 6e 63 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a nction16()..**.*
32590 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 * The first para
325a0 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 64 61 meter is the [da
325b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
325c0 6e 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 n] to which the
325d0 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 SQL.** function
325e0 69 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e 20 is to be added.
325f0 20 49 66 20 61 20 73 69 6e 67 6c 65 20 70 72 6f If a single pro
32600 67 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 gram uses more t
32610 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 han one database
32620 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 .** connection i
32630 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 nternally, then
32640 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 SQL functions mu
32650 73 74 20 62 65 20 61 64 64 65 64 20 69 6e 64 69 st be added indi
32660 76 69 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 vidually to.** e
32670 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e ach database con
32680 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 nection..**.** T
32690 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
326a0 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ter is the name
326b0 6f 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 of the SQL funct
326c0 69 6f 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65 ion to be create
326d0 64 20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 d or.** redefine
326e0 64 2e 20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f d. The length o
326f0 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 f the name is li
32700 6d 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 mited to 255 byt
32710 65 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 es, exclusive of
32720 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 .** the zero-ter
32730 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 minator. Note t
32740 68 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e hat the name len
32750 67 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 gth limit is in
32760 62 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 bytes, not.** ch
32770 61 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61 aracters. Any a
32780 74 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65 ttempt to create
32790 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 a function with
327a0 20 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a a longer name.*
327b0 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e * will result in
327c0 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
327d0 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a being returned..
327e0 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 **.** The third
327f0 70 61 72 61 6d 65 74 65 72 20 28 6e 41 72 67 29 parameter (nArg)
32800 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 .** is the numbe
32810 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 r of arguments t
32820 68 61 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 hat the SQL func
32830 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 tion or.** aggre
32840 67 61 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74 gate takes. If t
32850 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 his parameter is
32860 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 negative, then
32870 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
32880 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 or.** aggregate
32890 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e 75 may take any nu
328a0 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
328b0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 s..**.** The fou
328c0 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20 65 rth parameter, e
328d0 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66 69 TextRep, specifi
328e0 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c 49 es what.** [SQLI
328f0 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20 65 TE_UTF8 | text e
32900 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53 51 ncoding] this SQ
32910 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 L function prefe
32920 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70 61 rs for.** its pa
32930 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20 53 rameters. Any S
32940 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c QL function impl
32950 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c ementation shoul
32960 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f 72 d be able to wor
32970 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 55 k.** work with U
32980 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c 20 TF-8, UTF-16le,
32990 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 75 or UTF-16be. Bu
329a0 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 t some implement
329b0 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a 2a ations may be.**
329c0 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 more efficient
329d0 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 6e with one encodin
329e0 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e 20 g than another.
329f0 20 49 74 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 It is allowed t
32a00 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69 o.** invoke sqli
32a10 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
32a20 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 ion() or sqlite3
32a30 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
32a40 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 16() multiple.**
32a50 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 20 times with the
32a60 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62 75 same function bu
32a70 74 20 77 69 74 68 20 64 69 66 66 65 72 65 6e 74 t with different
32a80 20 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78 74 values of eText
32a90 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c Rep..** When mul
32aa0 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 tiple implementa
32ab0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d tions of the sam
32ac0 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 61 e function are a
32ad0 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74 65 vailable, SQLite
32ae0 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74 68 .** will pick th
32af0 65 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f 6c e one that invol
32b00 76 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 6d ves the least am
32b10 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f 6e ount of data con
32b20 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 version..** If t
32b30 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 here is only a s
32b40 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 ingle implementa
32b50 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20 tion which does
32b60 6e 6f 74 20 63 61 72 65 20 77 68 61 74 20 74 65 not care what te
32b70 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 xt.** encoding i
32b80 73 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 s used, then the
32b90 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 fourth argument
32ba0 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 51 4c 49 should be [SQLI
32bb0 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 TE_ANY]..**.** T
32bc0 68 65 20 66 69 66 74 68 20 70 61 72 61 6d 65 74 he fifth paramet
32bd0 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 er is an arbitra
32be0 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 ry pointer. The
32bf0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
32c00 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e 63 74 69 of the.** functi
32c10 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63 65 on can gain acce
32c20 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 ss to this point
32c30 65 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 er using [sqlite
32c40 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e 0a 3_user_data()]..
32c50 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65 6e 74 **.** The sevent
32c60 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20 6e 69 h, eighth and ni
32c70 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73 2c 20 nth parameters,
32c80 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61 6e 64 xFunc, xStep and
32c90 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a 2a 20 xFinal, are.**
32ca0 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d 6c 61 pointers to C-la
32cb0 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f 6e 73 nguage functions
32cc0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
32cd0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
32ce0 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 or.** aggregate
32cf0 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c 20 66 . A scalar SQL f
32d00 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 73 unction requires
32d10 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 an implementati
32d20 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 6e 63 0a on of the xFunc.
32d30 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 6c 79 ** callback only
32d40 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20 , NULL pointers
32d50 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 should be passed
32d60 20 61 73 20 74 68 65 20 78 53 74 65 70 20 61 6e as the xStep an
32d70 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 61 72 61 d xFinal.** para
32d80 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67 72 65 meters. An aggre
32d90 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f gate SQL functio
32da0 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d n requires an im
32db0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
32dc0 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46 69 xStep.** and xFi
32dd0 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73 68 6f nal and NULL sho
32de0 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 66 6f uld be passed fo
32df0 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65 6c 65 r xFunc. To dele
32e00 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a te an existing.*
32e10 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f * SQL function o
32e20 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61 73 r aggregate, pas
32e30 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c 20 74 s NULL for all t
32e40 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 hree function ca
32e50 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 llbacks..**.** I
32e60 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 t is permitted t
32e70 6f 20 72 65 67 69 73 74 65 72 20 6d 75 6c 74 69 o register multi
32e80 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ple implementati
32e90 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 0a ons of the same.
32ea0 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 74 ** functions wit
32eb0 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 h the same name
32ec0 62 75 74 20 77 69 74 68 20 65 69 74 68 65 72 20 but with either
32ed0 64 69 66 66 65 72 69 6e 67 20 6e 75 6d 62 65 72 differing number
32ee0 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 s of.** argument
32ef0 73 20 6f 72 20 64 69 66 66 65 72 69 6e 67 20 70 s or differing p
32f00 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 6e referred text en
32f10 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c 69 74 65 codings. SQLite
32f20 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 74 68 65 will use.** the
32f30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
32f40 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74 most closely mat
32f50 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20 ches the way in
32f60 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c which the.** SQL
32f70 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
32f80 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69 d. A function i
32f90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 mplementation wi
32fa0 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 th a non-negativ
32fb0 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65 e.** nArg parame
32fc0 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 20 ter is a better
32fd0 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e match than a fun
32fe0 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 ction implementa
32ff0 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e tion with.** a n
33000 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41 egative nArg. A
33010 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 function where
33020 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 65 the preferred te
33030 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d xt encoding.** m
33040 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 62 atches the datab
33050 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 ase encoding is
33060 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63 a better.** matc
33070 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f h than a functio
33080 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f n where the enco
33090 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e ding is differen
330a0 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69 t. .** A functi
330b0 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 on where the enc
330c0 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 oding difference
330d0 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 31 is between UTF1
330e0 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a 6le and UTF16be.
330f0 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d ** is a closer m
33100 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 atch than a func
33110 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 tion where the e
33120 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e ncoding differen
33130 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e ce is.** between
33140 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e UTF8 and UTF16.
33150 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20 .**.** Built-in
33160 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 functions may be
33170 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e overloaded by n
33180 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 ew application-d
33190 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 efined functions
331a0 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 ..** The first a
331b0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
331c0 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 ed function with
331d0 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76 a given name ov
331e0 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62 errides all.** b
331f0 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e uilt-in function
33200 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 s in the same [d
33210 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
33220 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d on] with the sam
33230 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65 e name..** Subse
33240 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f quent applicatio
33250 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
33260 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 ons of the same
33270 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69 name only overri
33280 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70 de .** prior app
33290 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
332a0 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
332b0 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74 are an exact mat
332c0 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75 ch for the.** nu
332d0 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 mber of paramete
332e0 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 64 rs and preferred
332f0 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a encoding..**.**
33300 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d An application-
33310 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
33320 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f is permitted to
33330 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 call other.** S
33340 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 QLite interfaces
33350 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68 . However, such
33360 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a calls must not.
33370 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 ** close the dat
33380 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
33390 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72 nor finalize or
333a0 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61 reset the prepa
333b0 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 red.** statement
333c0 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 75 in which the fu
333d0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
333e0 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 g..**.** INVARIA
333f0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 NTS:.**.** {H161
33400 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 03} The [sqlite3
33410 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
33420 31 36 28 44 2c 58 2c 2e 2e 2e 29 5d 20 69 6e 74 16(D,X,...)] int
33430 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 68 erface shall beh
33440 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ave.**
33450 61 73 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 as [sqlite3_crea
33460 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c te_function(D,X,
33470 2e 2e 2e 29 5d 20 69 6e 20 65 76 65 72 79 20 77 ...)] in every w
33480 61 79 20 65 78 63 65 70 74 20 74 68 61 74 20 69 ay except that i
33490 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e t.** in
334a0 74 65 72 70 72 65 74 73 20 74 68 65 20 58 20 61 terprets the X a
334b0 72 67 75 6d 65 6e 74 20 61 73 20 7a 65 72 6f 2d rgument as zero-
334c0 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 31 terminated UTF-1
334d0 36 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 61 6.** na
334e0 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 tive byte order
334f0 69 6e 73 74 65 61 64 20 6f 66 20 61 73 20 7a 65 instead of as ze
33500 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 ro-terminated UT
33510 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 F-8..**.** {H161
33520 30 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 06} A successful
33530 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 invocation of t
33540 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
33550 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
33560 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c unction(D,X,N,E,
33570 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 ...)] interface
33580 73 68 61 6c 6c 20 72 65 67 69 73 74 65 72 0a 2a shall register.*
33590 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 72 65 * or re
335a0 70 6c 61 63 65 73 20 63 61 6c 6c 62 61 63 6b 20 places callback
335b0 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 functions in the
335c0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
335d0 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 20 ction] D.**
335e0 20 20 20 20 20 75 73 65 64 20 74 6f 20 69 6d 70 used to imp
335f0 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66 lement the SQL f
33600 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 58 20 unction named X
33610 77 69 74 68 20 4e 20 70 61 72 61 6d 65 74 65 72 with N parameter
33620 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e s.** an
33630 64 20 68 61 76 69 6e 67 20 61 20 70 72 65 66 65 d having a prefe
33640 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 rred text encodi
33650 6e 67 20 6f 66 20 45 2e 0a 2a 2a 0a 2a 2a 20 7b ng of E..**.** {
33660 48 31 36 31 30 39 7d 20 41 20 73 75 63 63 65 73 H16109} A succes
33670 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 sful call to [sq
33680 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
33690 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c ction(D,X,N,E,P,
336a0 46 2c 53 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 F,S,L)].**
336b0 20 20 20 20 73 68 61 6c 6c 20 72 65 70 6c 61 63 shall replac
336c0 65 20 74 68 65 20 50 2c 20 46 2c 20 53 2c 20 61 e the P, F, S, a
336d0 6e 64 20 4c 20 76 61 6c 75 65 73 20 66 72 6f 6d nd L values from
336e0 20 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c 73 any prior calls
336f0 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 20 20 20 with.**
33700 20 20 74 68 65 20 73 61 6d 65 20 44 2c 20 58 2c the same D, X,
33710 20 4e 2c 20 61 6e 64 20 45 20 76 61 6c 75 65 73 N, and E values
33720 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 31 32 7d ..**.** {H16112}
33730 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 The [sqlite3_cr
33740 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c eate_function(D,
33750 58 2c 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 X,...)] interfac
33760 65 20 73 68 61 6c 6c 20 66 61 69 6c 0a 2a 2a 20 e shall fail.**
33770 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65 20 if the
33780 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d SQL function nam
33790 65 20 58 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 e X is.**
337a0 20 20 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 32 longer than 2
337b0 35 35 20 62 79 74 65 73 20 65 78 63 6c 75 73 69 55 bytes exclusi
337c0 76 65 20 6f 66 20 74 68 65 20 7a 65 72 6f 20 74 ve of the zero t
337d0 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a erminator..**.**
337e0 20 7b 48 31 36 31 31 38 7d 20 54 68 65 20 5b 73 {H16118} The [s
337f0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
33800 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 nction(D,X,N,E,P
33810 2c 46 2c 53 2c 4c 29 5d 20 69 6e 74 65 72 66 61 ,F,S,L)] interfa
33820 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 ce.** s
33830 68 61 6c 6c 20 66 61 69 6c 20 75 6e 6c 65 73 73 hall fail unless
33840 20 65 69 74 68 65 72 20 46 20 69 73 20 4e 55 4c either F is NUL
33850 4c 20 61 6e 64 20 53 20 61 6e 64 20 4c 20 61 72 L and S and L ar
33860 65 20 6e 6f 6e 2d 4e 55 4c 4c 20 6f 72 0a 2a 2a e non-NULL or.**
33870 2a 20 20 20 20 20 20 20 20 20 46 20 69 73 20 6e * F is n
33880 6f 6e 2d 4e 55 4c 4c 20 61 6e 64 20 53 20 61 6e on-NULL and S an
33890 64 20 4c 20 61 72 65 20 4e 55 4c 4c 2e 0a 2a 2a d L are NULL..**
338a0 0a 2a 2a 20 7b 48 31 36 31 32 31 7d 20 54 68 65 .** {H16121} The
338b0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
338c0 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 2e 2e 2e 29 _function(D,...)
338d0 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c ] interface shal
338e0 6c 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a l fails with an.
338f0 2a 2a 20 20 20 20 20 20 20 20 20 20 65 72 72 6f ** erro
33900 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 r code of [SQLIT
33910 45 5f 42 55 53 59 5d 20 69 66 20 74 68 65 72 65 E_BUSY] if there
33920 20 65 78 69 73 74 20 5b 70 72 65 70 61 72 65 64 exist [prepared
33930 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 statements].**
33940 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 61 associa
33950 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 ted with the [da
33960 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
33970 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 n] D..**.** {H16
33980 31 32 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 124} The [sqlite
33990 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
339a0 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 20 69 6e n(D,X,N,...)] in
339b0 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 66 61 terface shall fa
339c0 69 6c 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 20 il with.**
339d0 20 20 20 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 an error cod
339e0 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 45 52 52 e of [SQLITE_ERR
339f0 4f 52 5d 20 69 66 20 70 61 72 61 6d 65 74 65 72 OR] if parameter
33a00 20 4e 20 69 73 20 6c 65 73 73 0a 2a 2a 20 20 20 N is less.**
33a10 20 20 20 20 20 20 20 74 68 61 6e 20 2d 31 20 6f than -1 o
33a20 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 r greater than 1
33a30 32 37 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 32 27..**.** {H1612
33a40 37 7d 20 57 68 65 6e 20 4e 20 69 73 20 6e 6f 6e 7} When N is non
33a50 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 5b -negative, the [
33a60 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
33a70 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 2e 2e unction(D,X,N,..
33a80 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .)].**
33a90 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 interface shall
33aa0 72 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63 register callbac
33ab0 6b 73 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 ks to be invoked
33ac0 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 for the.**
33ad0 20 20 20 20 20 53 51 4c 20 66 75 6e 63 74 69 6f SQL functio
33ae0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 61 n.** na
33af0 6d 65 64 20 58 20 77 68 65 6e 20 74 68 65 20 6e med X when the n
33b00 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
33b10 74 73 20 74 6f 20 74 68 65 20 53 51 4c 20 66 75 ts to the SQL fu
33b20 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 nction is.**
33b30 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 4e 2e exactly N.
33b40 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 30 7d 20 .**.** {H16130}
33b50 57 68 65 6e 20 4e 20 69 73 20 2d 31 2c 20 74 68 When N is -1, th
33b60 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 e [sqlite3_creat
33b70 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e e_function(D,X,N
33b80 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ,...)].**
33b90 20 20 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 interface sha
33ba0 6c 6c 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c ll register call
33bb0 62 61 63 6b 73 20 74 6f 20 62 65 20 69 6e 76 6f backs to be invo
33bc0 6b 65 64 20 66 6f 72 20 74 68 65 20 53 51 4c 0a ked for the SQL.
33bd0 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 ** func
33be0 74 69 6f 6e 20 6e 61 6d 65 64 20 58 20 77 69 74 tion named X wit
33bf0 68 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 h any number of
33c00 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a arguments..**.**
33c10 20 7b 48 31 36 31 33 33 7d 20 57 68 65 6e 20 63 {H16133} When c
33c20 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
33c30 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
33c40 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20 (D,X,N,...)].**
33c50 20 20 20 20 20 20 20 20 20 73 70 65 63 69 66 79 specify
33c60 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d multiple implem
33c70 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
33c80 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 58 same function X
33c90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 .** and
33ca0 20 77 68 65 6e 20 6f 6e 65 20 69 6d 70 6c 65 6d when one implem
33cb0 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 4e 3e 3d entation has N>=
33cc0 30 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20 0 and the other
33cd0 68 61 73 20 4e 3d 28 2d 31 29 0a 2a 2a 20 20 20 has N=(-1).**
33ce0 20 20 20 20 20 20 20 74 68 65 20 69 6d 70 6c 65 the imple
33cf0 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 20 61 mentation with a
33d00 20 6e 6f 6e 2d 7a 65 72 6f 20 4e 20 73 68 61 6c non-zero N shal
33d10 6c 20 62 65 20 70 72 65 66 65 72 72 65 64 2e 0a l be preferred..
33d20 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 36 7d 20 57 **.** {H16136} W
33d30 68 65 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 hen calls to [sq
33d40 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
33d50 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 2e 2e ction(D,X,N,E,..
33d60 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .)].**
33d70 73 70 65 63 69 66 79 20 6d 75 6c 74 69 70 6c 65 specify multiple
33d80 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
33d90 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e of the same fun
33da0 63 74 69 6f 6e 20 58 20 77 69 74 68 0a 2a 2a 20 ction X with.**
33db0 20 20 20 20 20 20 20 20 20 74 68 65 20 73 61 6d the sam
33dc0 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 e number of argu
33dd0 6d 65 6e 74 73 20 4e 20 62 75 74 20 77 69 74 68 ments N but with
33de0 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 20 20 different.**
33df0 20 20 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 encodings
33e00 20 45 2c 20 74 68 65 6e 20 74 68 65 20 69 6d 70 E, then the imp
33e10 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65 72 lementation wher
33e20 65 20 45 20 6d 61 74 63 68 65 73 20 74 68 65 0a e E matches the.
33e30 2a 2a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 ** data
33e40 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 73 68 base encoding sh
33e50 61 6c 6c 20 70 72 65 66 65 72 72 65 64 2e 0a 2a all preferred..*
33e60 2a 0a 2a 2a 20 7b 48 31 36 31 33 39 7d 20 46 6f *.** {H16139} Fo
33e70 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 53 r an aggregate S
33e80 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 QL function crea
33e90 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 ted using.**
33ea0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 [sqlite3_c
33eb0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 reate_function(D
33ec0 2c 58 2c 4e 2c 45 2c 50 2c 30 2c 53 2c 4c 29 5d ,X,N,E,P,0,S,L)]
33ed0 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 0a 2a the finalizer.*
33ee0 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 74 * funct
33ef0 69 6f 6e 20 4c 20 73 68 61 6c 6c 20 61 6c 77 61 ion L shall alwa
33f00 79 73 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 78 ys be invoked ex
33f10 61 63 74 6c 79 20 6f 6e 63 65 20 69 66 20 74 68 actly once if th
33f20 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 e.** st
33f30 65 70 20 66 75 6e 63 74 69 6f 6e 20 53 20 69 73 ep function S is
33f40 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d called one or m
33f50 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 0a 2a 2a ore times..**.**
33f60 20 7b 48 31 36 31 34 32 7d 20 57 68 65 6e 20 53 {H16142} When S
33f70 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 65 69 QLite invokes ei
33f80 74 68 65 72 20 74 68 65 20 78 46 75 6e 63 20 6f ther the xFunc o
33f90 72 20 78 53 74 65 70 20 66 75 6e 63 74 69 6f 6e r xStep function
33fa0 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of.**
33fb0 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 an application-d
33fc0 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 efined SQL funct
33fd0 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65 ion or aggregate
33fe0 20 63 72 65 61 74 65 64 0a 2a 2a 20 20 20 20 20 created.**
33ff0 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74 65 33 by [sqlite3
34000 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
34010 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
34020 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 create_function1
34030 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 6()],.**
34040 20 20 74 68 65 6e 20 74 68 65 20 61 72 72 61 79 then the array
34050 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c of [sqlite3_val
34060 75 65 5d 20 6f 62 6a 65 63 74 73 20 70 61 73 73 ue] objects pass
34070 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 20 ed as the.**
34080 20 20 20 20 20 20 74 68 69 72 64 20 70 61 72 61 third para
34090 6d 65 74 65 72 20 73 68 61 6c 6c 20 62 65 20 5b meter shall be [
340a0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
340b0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 3_value] objects
340c0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
340d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 int sqlite3_crea
340e0 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 te_function(. s
340f0 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f qlite3 *db,. co
34100 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 nst char *zFunct
34110 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e ionName,. int n
34120 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 Arg,. int eText
34130 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 Rep,. void *pAp
34140 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e p,. void (*xFun
34150 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 c)(sqlite3_conte
34160 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f xt*,int,sqlite3_
34170 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 value**),. void
34180 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 (*xStep)(sqlite
34190 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
341a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c qlite3_value**),
341b0 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c . void (*xFinal
341c0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
341d0 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 t*).);.SQLITE_AP
341e0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 I int sqlite3_cr
341f0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 eate_function16(
34200 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a . sqlite3 *db,.
34210 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 const void *zF
34220 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 unctionName,. i
34230 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 nt nArg,. int e
34240 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 TextRep,. void
34250 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a *pApp,. void (*
34260 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 xFunc)(sqlite3_c
34270 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
34280 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 te3_value**),.
34290 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 void (*xStep)(sq
342a0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 lite3_context*,i
342b0 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt,sqlite3_value
342c0 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 **),. void (*xF
342d0 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f inal)(sqlite3_co
342e0 6e 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a ntext*).);../*.*
342f0 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 78 74 * CAPI3REF: Text
34300 20 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30 32 Encodings {H102
34310 36 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48 31 67} <S50200> <H1
34320 36 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 6100>.**.** Thes
34330 65 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e e constant defin
34340 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20 e integer codes
34350 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 74 that represent t
34360 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 65 he various.** te
34370 78 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 70 xt encodings sup
34380 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 ported by SQLite
34390 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
343a0 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 ITE_UTF8
343b0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 1.#define SQL
343c0 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20 ITE_UTF16LE
343d0 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 2.#define SQL
343e0 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20 ITE_UTF16BE
343f0 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 3.#define SQL
34400 49 54 45 5f 55 54 46 31 36 20 20 20 20 20 20 20 ITE_UTF16
34410 20 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20 6e 4 /* Use n
34420 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
34430 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
34440 54 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20 TE_ANY
34450 20 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 5 /* sqlite
34460 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
34470 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e n only */.#defin
34480 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 e SQLITE_UTF16_A
34490 4c 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a 20 LIGNED 8 /*
344a0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
344b0 6f 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f ollation only */
344c0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
344d0 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75 6e : Deprecated Fun
344e0 63 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 43 ctions.** DEPREC
344f0 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ATED.**.** These
34500 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 5b functions are [
34510 64 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49 6e deprecated]. In
34520 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 order to mainta
34530 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 in.** backwards
34540 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 compatibility wi
34550 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 74 th older code, t
34560 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 hese functions c
34570 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 62 ontinue .** to b
34580 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48 6f e supported. Ho
34590 77 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c 69 wever, new appli
345a0 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 61 cations should a
345b0 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65 20 void.** the use
345c0 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f of these functio
345d0 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e 63 ns. To help enc
345e0 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74 6f ourage people to
345f0 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 20 avoid.** using
34600 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2c these functions,
34610 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69 6e we are not goin
34620 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77 68 g to tell you wh
34630 61 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23 at they do..*/.#
34640 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
34650 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53 51 IT_DEPRECATED.SQ
34660 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f LITE_API SQLITE_
34670 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 DEPRECATED int s
34680 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 qlite3_aggregate
34690 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 63 _count(sqlite3_c
346a0 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 ontext*);.SQLITE
346b0 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 _API SQLITE_DEPR
346c0 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 ECATED int sqlit
346d0 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74 e3_expired(sqlit
346e0 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 e3_stmt*);.SQLIT
346f0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
34700 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 RECATED int sqli
34710 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e te3_transfer_bin
34720 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 dings(sqlite3_st
34730 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d mt*, sqlite3_stm
34740 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 t*);.SQLITE_API
34750 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 SQLITE_DEPRECATE
34760 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c D int sqlite3_gl
34770 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 obal_recover(voi
34780 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 d);.SQLITE_API S
34790 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 QLITE_DEPRECATED
347a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68 void sqlite3_th
347b0 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 read_cleanup(voi
347c0 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 d);.SQLITE_API S
347d0 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 QLITE_DEPRECATED
347e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d int sqlite3_mem
347f0 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 2a ory_alarm(void(*
34800 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f )(void*,sqlite3_
34810 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 2a int64,int),void*
34820 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b ,sqlite3_int64);
34830 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
34840 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e 69 API3REF: Obtaini
34850 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 ng SQL Function
34860 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 73 Parameter Values
34870 20 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32 30 {H15100} <S2020
34880 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 0>.**.** The C-l
34890 61 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e anguage implemen
348a0 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75 tation of SQL fu
348b0 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 nctions and aggr
348c0 65 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 74 egates uses.** t
348d0 68 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 72 his set of inter
348e0 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 6f face routines to
348f0 20 61 63 63 65 73 73 20 74 68 65 20 70 61 72 61 access the para
34900 6d 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a meter values on.
34910 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 ** the function
34920 6f 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a or aggregate..**
34930 0a 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 66 .** The xFunc (f
34940 6f 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 or scalar functi
34950 6f 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 66 ons) or xStep (f
34960 6f 72 20 61 67 67 72 65 67 61 74 65 73 29 20 70 or aggregates) p
34970 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 arameters.** to
34980 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
34990 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 function()] and
349a0 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
349b0 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a function16()].**
349c0 20 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b define callback
349d0 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
349e0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
349f0 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 ns and aggregate
34a00 73 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70 61 s..** The 4th pa
34a10 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 65 rameter to these
34a20 20 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e callbacks is an
34a30 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 array of pointe
34a40 72 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 rs to.** [protec
34a50 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
34a60 65 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 e] objects. The
34a70 72 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 re is one [sqlit
34a80 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
34a90 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 for.** each par
34aa0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 51 ameter to the SQ
34ab0 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 L function. The
34ac0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 se routines are
34ad0 75 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 used to.** extra
34ae0 63 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 ct values from t
34af0 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 he [sqlite3_valu
34b00 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a e] objects..**.*
34b10 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
34b20 20 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20 work only with
34b30 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 [protected sqlit
34b40 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
34b50 73 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 s..** Any attemp
34b60 74 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 72 t to use these r
34b70 6f 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 outines on an [u
34b80 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 nprotected sqlit
34b90 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a e3_value].** obj
34ba0 65 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 75 ect results in u
34bb0 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f ndefined behavio
34bc0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 r..**.** These r
34bd0 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73 outines work jus
34be0 74 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 65 t like the corre
34bf0 73 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e sponding [column
34c00 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e access function
34c10 73 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 s].** except tha
34c20 74 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 t these routine
34c30 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 20 s take a single
34c40 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 [protected sqlit
34c50 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
34c60 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 .** pointer inst
34c70 65 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 65 ead of a [sqlite
34c80 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72 3_stmt*] pointer
34c90 20 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 20 and an integer
34ca0 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a column number..*
34cb0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
34cc0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 20 _value_text16()
34cd0 69 6e 74 65 72 66 61 63 65 20 65 78 74 72 61 63 interface extrac
34ce0 74 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 ts a UTF-16 stri
34cf0 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 74 ng.** in the nat
34d00 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 6f ive byte-order o
34d10 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68 69 f the host machi
34d20 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 ne. The.** sqli
34d30 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
34d40 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 be() and sqlite3
34d50 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 _value_text16le(
34d60 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 ) interfaces.**
34d70 65 78 74 72 61 63 74 20 55 54 46 2d 31 36 20 73 extract UTF-16 s
34d80 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65 6e trings as big-en
34d90 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65 2d dian and little-
34da0 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69 76 endian respectiv
34db0 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ely..**.** The s
34dc0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d qlite3_value_num
34dd0 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74 65 eric_type() inte
34de0 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 rface attempts t
34df0 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72 o apply.** numer
34e00 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20 74 ic affinity to t
34e10 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 he value. This
34e20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61 74 means that an at
34e30 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64 65 tempt is.** made
34e40 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 to convert the
34e50 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74 65 value to an inte
34e60 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20 ger or floating
34e70 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 75 point. If.** su
34e80 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 ch a conversion
34e90 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68 is possible with
34ea0 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f out loss of info
34eb0 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68 65 rmation (in othe
34ec0 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20 74 r.** words, if t
34ed0 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 he value is a st
34ee0 72 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73 20 ring that looks
34ef0 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a 2a like a number).*
34f00 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76 65 * then the conve
34f10 72 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d rsion is perform
34f20 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6e ed. Otherwise n
34f30 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 o conversion occ
34f40 75 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c urs..** The [SQL
34f50 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 ITE_INTEGER | da
34f60 74 61 74 79 70 65 5d 20 61 66 74 65 72 20 63 6f tatype] after co
34f70 6e 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74 75 nversion is retu
34f80 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 rned..**.** Plea
34f90 73 65 20 70 61 79 20 70 61 72 74 69 63 75 6c 61 se pay particula
34fa0 72 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20 74 r attention to t
34fb0 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 he fact that the
34fc0 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 pointer returne
34fd0 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 d.** from [sqlit
34fe0 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29 5d e3_value_blob()]
34ff0 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 , [sqlite3_value
35000 5f 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20 _text()], or.**
35010 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 [sqlite3_value_t
35020 65 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65 20 ext16()] can be
35030 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 61 invalidated by a
35040 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
35050 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f to.** [sqlite3_
35060 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 value_bytes()],
35070 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 [sqlite3_value_b
35080 79 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c 69 ytes16()], [sqli
35090 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 te3_value_text()
350a0 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 ],.** or [sqlite
350b0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 3_value_text16()
350c0 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 ]..**.** These r
350d0 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20 outines must be
350e0 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 called from the
350f0 73 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a 2a same thread as.*
35100 2a 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 * the SQL functi
35110 6f 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65 64 on that supplied
35120 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 the [sqlite3_va
35130 6c 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72 73 lue*] parameters
35140 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
35150 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 30 TS:.**.** {H1510
35160 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 3} The [sqlite3_
35170 76 61 6c 75 65 5f 62 6c 6f 62 28 56 29 5d 20 69 value_blob(V)] i
35180 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 nterface convert
35190 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
351a0 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c [protected sql
351b0 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
351c0 63 74 20 56 20 69 6e 74 6f 20 61 20 42 4c 4f 42 ct V into a BLOB
351d0 20 61 6e 64 20 74 68 65 6e 0a 2a 2a 20 20 20 20 and then.**
351e0 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61 20 returns a
351f0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 pointer to the c
35200 6f 6e 76 65 72 74 65 64 20 76 61 6c 75 65 2e 0a onverted value..
35210 2a 2a 0a 2a 2a 20 7b 48 31 35 31 30 36 7d 20 54 **.** {H15106} T
35220 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 he [sqlite3_valu
35230 65 5f 62 79 74 65 73 28 56 29 5d 20 69 6e 74 65 e_bytes(V)] inte
35240 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 rface returns th
35250 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75 e.** nu
35260 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
35270 20 74 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 the BLOB or str
35280 69 6e 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f ing (exclusive o
35290 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 f the.**
352a0 20 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f zero terminato
352b0 72 20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29 r on the string)
352c0 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e that was return
352d0 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20 ed by the.**
352e0 20 20 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e most recen
352f0 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 t call to [sqlit
35300 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 56 29 e3_value_blob(V)
35310 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ] or.**
35320 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
35330 74 65 78 74 28 56 29 5d 2e 0a 2a 2a 0a 2a 2a 20 text(V)]..**.**
35340 7b 48 31 35 31 30 39 7d 20 54 68 65 20 5b 73 71 {H15109} The [sq
35350 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
35360 73 31 36 28 56 29 5d 20 69 6e 74 65 72 66 61 63 s16(V)] interfac
35370 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a e returns the.**
35380 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72 number
35390 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
353a0 20 73 74 72 69 6e 67 20 28 65 78 63 6c 75 73 69 string (exclusi
353b0 76 65 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 ve of the.**
353c0 20 20 20 20 20 20 7a 65 72 6f 20 74 65 72 6d 69 zero termi
353d0 6e 61 74 6f 72 20 6f 6e 20 74 68 65 20 73 74 72 nator on the str
353e0 69 6e 67 29 20 74 68 61 74 20 77 61 73 20 72 65 ing) that was re
353f0 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a turned by the.**
35400 20 20 20 20 20 20 20 20 20 20 6d 6f 73 74 20 72 most r
35410 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73 ecent call to [s
35420 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
35430 74 31 36 28 56 29 5d 2c 0a 2a 2a 20 20 20 20 20 t16(V)],.**
35440 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76 61 [sqlite3_va
35450 6c 75 65 5f 74 65 78 74 31 36 62 65 28 56 29 5d lue_text16be(V)]
35460 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 , or [sqlite3_va
35470 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 56 29 5d lue_text16le(V)]
35480 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 31 32 7d ..**.** {H15112}
35490 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 The [sqlite3_va
354a0 6c 75 65 5f 64 6f 75 62 6c 65 28 56 29 5d 20 69 lue_double(V)] i
354b0 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 nterface convert
354c0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
354d0 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c [protected sql
354e0 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
354f0 63 74 20 56 20 69 6e 74 6f 20 61 20 66 6c 6f 61 ct V into a floa
35500 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
35510 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 and.**
35520 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 returns a copy
35530 6f 66 20 74 68 61 74 20 76 61 6c 75 65 2e 0a 2a of that value..*
35540 2a 0a 2a 2a 20 7b 48 31 35 31 31 35 7d 20 54 68 *.** {H15115} Th
35550 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 e [sqlite3_value
35560 5f 69 6e 74 28 56 29 5d 20 69 6e 74 65 72 66 61 _int(V)] interfa
35570 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a ce converts the.
35580 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 6f ** [pro
35590 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
355a0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 69 alue] object V i
355b0 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69 67 nto a 64-bit sig
355c0 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 0a ned integer and.
355d0 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ** retu
355e0 72 6e 73 20 74 68 65 20 6c 6f 77 65 72 20 33 32 rns the lower 32
355f0 20 62 69 74 73 20 6f 66 20 74 68 61 74 20 69 6e bits of that in
35600 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 teger..**.** {H1
35610 35 31 31 38 7d 20 54 68 65 20 5b 73 71 6c 69 74 5118} The [sqlit
35620 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 56 e3_value_int64(V
35630 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e )] interface con
35640 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 verts the.**
35650 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 [protected
35660 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 sqlite3_value]
35670 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 object V into a
35680 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
35690 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 teger and.**
356a0 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61 20 returns a
356b0 63 6f 70 79 20 6f 66 20 74 68 61 74 20 69 6e 74 copy of that int
356c0 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 eger..**.** {H15
356d0 31 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 121} The [sqlite
356e0 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 56 29 5d 3_value_text(V)]
356f0 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 interface conve
35700 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 rts the.**
35710 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 [protected s
35720 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 qlite3_value] ob
35730 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 7a 65 ject V into a ze
35740 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 ro-terminated UT
35750 46 2d 38 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 F-8.**
35760 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 string and retur
35770 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
35780 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a that string..**.
35790 2a 2a 20 7b 48 31 35 31 32 34 7d 20 54 68 65 20 ** {H15124} The
357a0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 [sqlite3_value_t
357b0 65 78 74 31 36 28 56 29 5d 20 69 6e 74 65 72 66 ext16(V)] interf
357c0 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 ace converts the
357d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 .** [pr
357e0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
357f0 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 value] object V
35800 69 6e 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d into a zero-term
35810 69 6e 61 74 65 64 20 32 2d 62 79 74 65 0a 2a 2a inated 2-byte.**
35820 20 20 20 20 20 20 20 20 20 20 61 6c 69 67 6e 65 aligne
35830 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 d UTF-16 native
35840 62 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 byte order.**
35850 20 20 20 20 20 20 20 73 74 72 69 6e 67 20 61 6e string an
35860 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e d returns a poin
35870 74 65 72 20 74 6f 20 74 68 61 74 20 73 74 72 69 ter to that stri
35880 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 32 ng..**.** {H1512
35890 37 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 7} The [sqlite3_
358a0 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 56 value_text16be(V
358b0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e )] interface con
358c0 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 verts the.**
358d0 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 [protected
358e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 sqlite3_value]
358f0 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 object V into a
35900 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 zero-terminated
35910 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20 2-byte.**
35920 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31 aligned UTF-1
35930 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a 2a 20 6 big-endian.**
35940 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 20 string
35950 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
35960 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 73 74 inter to that st
35970 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 ring..**.** {H15
35980 31 33 30 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 130} The [sqlite
35990 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 3_value_text16le
359a0 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 (V)] interface c
359b0 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 onverts the.**
359c0 20 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 [protect
359d0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
359e0 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 ] object V into
359f0 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 a zero-terminate
35a00 64 20 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 d 2-byte.**
35a10 20 20 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46 aligned UTF
35a20 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 -16 little-endia
35a30 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 n.** st
35a40 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 ring and returns
35a50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
35a60 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a at string..**.**
35a70 20 7b 48 31 35 31 33 33 7d 20 54 68 65 20 5b 73 {H15133} The [s
35a80 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
35a90 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 e(V)] interface
35aa0 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 returns.**
35ab0 20 20 20 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 one of [SQLI
35ac0 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 TE_NULL], [SQLIT
35ad0 45 5f 49 4e 54 45 47 45 52 5d 2c 20 5b 53 51 4c E_INTEGER], [SQL
35ae0 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a 2a 2a 20 20 ITE_FLOAT],.**
35af0 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f [SQLITE_
35b00 54 45 58 54 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 TEXT], or [SQLIT
35b10 45 5f 42 4c 4f 42 5d 20 61 73 20 61 70 70 72 6f E_BLOB] as appro
35b20 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a 20 20 20 priate for.**
35b30 20 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c 69 the [sqli
35b40 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
35b50 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 t V..**.** {H151
35b60 33 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 36} The [sqlite3
35b70 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 _value_numeric_t
35b80 79 70 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63 ype(V)] interfac
35b90 65 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 20 20 e converts.**
35ba0 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 6f 74 the [prot
35bb0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
35bc0 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e lue] object V in
35bd0 74 6f 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 to either an int
35be0 65 67 65 72 20 6f 72 0a 2a 2a 20 20 20 20 20 20 eger or.**
35bf0 20 20 20 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 a floating p
35c00 6f 69 6e 74 20 76 61 6c 75 65 20 69 66 20 69 74 oint value if it
35c10 20 63 61 6e 20 64 6f 20 73 6f 20 77 69 74 68 6f can do so witho
35c20 75 74 20 6c 6f 73 73 20 6f 66 0a 2a 2a 20 20 20 ut loss of.**
35c30 20 20 20 20 20 20 20 69 6e 66 6f 72 6d 61 74 69 informati
35c40 6f 6e 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 on, and returns
35c50 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4e one of [SQLITE_N
35c60 55 4c 4c 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 ULL],.**
35c70 20 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 [SQLITE_INTEGE
35c80 52 5d 2c 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 R], [SQLITE_FLOA
35c90 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 T], [SQLITE_TEXT
35ca0 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 ], or.**
35cb0 20 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 20 [SQLITE_BLOB]
35cc0 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 as appropriate f
35cd0 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 or the.**
35ce0 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 [protected sq
35cf0 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
35d00 65 63 74 20 56 20 61 66 74 65 72 20 74 68 65 20 ect V after the
35d10 63 6f 6e 76 65 72 73 69 6f 6e 20 61 74 74 65 6d conversion attem
35d20 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 pt..*/.SQLITE_AP
35d30 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
35d40 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
35d50 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
35d60 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
35d70 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
35d80 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c ytes(sqlite3_val
35d90 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
35da0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
35db0 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 ue_bytes16(sqlit
35dc0 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 e3_value*);.SQLI
35dd0 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 TE_API double sq
35de0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 lite3_value_doub
35df0 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le(sqlite3_value
35e00 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
35e10 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt sqlite3_value
35e20 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c _int(sqlite3_val
35e30 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
35e40 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
35e50 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
35e60 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 64(sqlite3_value
35e70 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 *);.SQLITE_API c
35e80 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
35e90 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 ar *sqlite3_valu
35ea0 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76 e_text(sqlite3_v
35eb0 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 alue*);.SQLITE_A
35ec0 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
35ed0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
35ee0 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 t16(sqlite3_valu
35ef0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
35f00 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
35f10 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
35f20 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le(sqlite3_value
35f30 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 *);.SQLITE_API c
35f40 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
35f50 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 e3_value_text16b
35f60 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a e(sqlite3_value*
35f70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
35f80 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f t sqlite3_value_
35f90 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c type(sqlite3_val
35fa0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
35fb0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
35fc0 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 ue_numeric_type(
35fd0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
35fe0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
35ff0 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67 61 : Obtain Aggrega
36000 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 te Function Cont
36010 65 78 74 20 7b 48 31 36 32 31 30 7d 20 3c 53 32 ext {H16210} <S2
36020 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0200>.**.** The
36030 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
36040 66 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 f aggregate SQL
36050 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 68 functions use th
36060 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 61 6c is routine to al
36070 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 74 72 75 locate.** a stru
36080 63 74 75 72 65 20 66 6f 72 20 73 74 6f 72 69 6e cture for storin
36090 67 20 74 68 65 69 72 20 73 74 61 74 65 2e 0a 2a g their state..*
360a0 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 *.** The first t
360b0 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f ime the sqlite3_
360c0 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
360d0 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63 t() routine is c
360e0 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a 2a 20 70 alled for a.** p
360f0 61 72 74 69 63 75 6c 61 72 20 61 67 67 72 65 67 articular aggreg
36100 61 74 65 2c 20 53 51 4c 69 74 65 20 61 6c 6c 6f ate, SQLite allo
36110 63 61 74 65 73 20 6e 42 79 74 65 73 20 6f 66 20 cates nBytes of
36120 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 73 20 6f memory, zeroes o
36130 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 6d 6f 72 ut that.** memor
36140 79 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 y, and returns a
36150 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 pointer to it.
36160 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 On second and su
36170 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 bsequent calls t
36180 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 61 67 67 o.** sqlite3_agg
36190 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 regate_context()
361a0 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 61 67 for the same ag
361b0 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
361c0 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 20 73 index,.** the s
361d0 61 6d 65 20 62 75 66 66 65 72 20 69 73 20 72 65 ame buffer is re
361e0 74 75 72 6e 65 64 2e 20 54 68 65 20 69 6d 70 6c turned. The impl
361f0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
36200 65 20 61 67 67 72 65 67 61 74 65 20 63 61 6e 20 e aggregate can
36210 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 use.** the retur
36220 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20 61 63 ned buffer to ac
36230 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e 0a 2a cumulate data..*
36240 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75 74 6f *.** SQLite auto
36250 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 73 20 matically frees
36260 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 the allocated bu
36270 66 66 65 72 20 77 68 65 6e 20 74 68 65 20 61 67 ffer when the ag
36280 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65 72 79 gregate.** query
36290 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a 0a 2a concludes..**.*
362a0 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 * The first para
362b0 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 meter should be
362c0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a a copy of the.**
362d0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 [sqlite3_contex
362e0 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e t | SQL function
362f0 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20 69 context] that i
36300 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61 s the first para
36310 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 meter.** to the
36320 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 callback routine
36330 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
36340 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 66 the aggregate f
36350 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 unction..**.** T
36360 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 his routine must
36370 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 be called from
36380 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
36390 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 in which.** the
363a0 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75 aggregate SQL fu
363b0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
363c0 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 g..**.** INVARIA
363d0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 NTS:.**.** {H162
363e0 31 31 7d 20 54 68 65 20 66 69 72 73 74 20 69 6e 11} The first in
363f0 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c vocation of [sql
36400 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
36410 6f 6e 74 65 78 74 28 43 2c 4e 29 5d 20 66 6f 72 ontext(C,N)] for
36420 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 70 .** a p
36430 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e articular instan
36440 63 65 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61 ce of an aggrega
36450 74 65 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 te function (for
36460 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
36470 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78 contex
36480 74 20 43 29 20 63 61 75 73 65 73 20 53 51 4c 69 t C) causes SQLi
36490 74 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 4e te to allocate N
364a0 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
364b0 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65 ,.** ze
364c0 72 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 2c 20 ro that memory,
364d0 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 and return a poi
364e0 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f nter to the allo
364f0 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2a cated memory..**
36500 0a 2a 2a 20 7b 48 31 36 32 31 33 7d 20 49 66 20 .** {H16213} If
36510 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
36520 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ion error occurs
36530 20 64 75 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 during.**
36540 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 [sqlite3_agg
36550 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 regate_context(C
36560 2c 4e 29 5d 20 74 68 65 6e 20 74 68 65 20 66 75 ,N)] then the fu
36570 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 30 nction returns 0
36580 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 31 35 7d ..**.** {H16215}
36590 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 Second and subs
365a0 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f equent invocatio
365b0 6e 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 ns of.**
365c0 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 65 [sqlite3_aggre
365d0 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 2c 4e gate_context(C,N
365e0 29 5d 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 )] for the same
365f0 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 context pointer
36600 43 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 67 C.** ig
36610 6e 6f 72 65 20 74 68 65 20 4e 20 70 61 72 61 6d nore the N param
36620 65 74 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 eter and return
36630 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
36640 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 same.**
36650 20 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 block of memor
36660 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 y returned by th
36670 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74 69 e first invocati
36680 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 31 on..**.** {H1621
36690 37 7d 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 7} The memory al
366a0 6c 6f 63 61 74 65 64 20 62 79 20 5b 73 71 6c 69 located by [sqli
366b0 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
366c0 6e 74 65 78 74 28 43 2c 4e 29 5d 20 69 73 0a 2a ntext(C,N)] is.*
366d0 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d * autom
366e0 61 74 69 63 61 6c 6c 79 20 66 72 65 65 64 20 6f atically freed o
366f0 6e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 n the next call
36700 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 to [sqlite3_rese
36710 74 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 t()].**
36720 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e or [sqlite3_fin
36730 61 6c 69 7a 65 28 29 5d 20 66 6f 72 20 74 68 65 alize()] for the
36740 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
36750 6d 65 6e 74 5d 20 63 6f 6e 74 61 69 6e 69 6e 67 ment] containing
36760 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
36770 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
36780 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 ion associated w
36790 69 74 68 20 63 6f 6e 74 65 78 74 20 43 2e 0a 2a ith context C..*
367a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
367b0 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65 d *sqlite3_aggre
367c0 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c gate_context(sql
367d0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 ite3_context*, i
367e0 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a 0a nt nBytes);../*.
367f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 65 ** CAPI3REF: Use
36800 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 74 r Data For Funct
36810 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 3c 53 ions {H16240} <S
36820 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20200>.**.** The
36830 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 sqlite3_user_da
36840 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ta() interface r
36850 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 eturns a copy of
36860 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 .** the pointer
36870 74 68 61 74 20 77 61 73 20 74 68 65 20 70 55 73 that was the pUs
36880 65 72 44 61 74 61 20 70 61 72 61 6d 65 74 65 72 erData parameter
36890 20 28 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 (the 5th parame
368a0 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 5b ter).** of the [
368b0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
368c0 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e unction()].** an
368d0 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 d [sqlite3_creat
368e0 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20 e_function16()]
368f0 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f 72 routines that or
36900 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 iginally.** regi
36910 73 74 65 72 65 64 20 74 68 65 20 61 70 70 6c 69 stered the appli
36920 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 66 cation defined f
36930 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d 0a 2a unction. {END}.*
36940 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
36950 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 e must be called
36960 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 from the same t
36970 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a hread in which.*
36980 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f * the applicatio
36990 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
369a0 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a on is running..*
369b0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
369c0 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 34 33 7d 20 .**.** {H16243}
369d0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 The [sqlite3_use
369e0 72 5f 64 61 74 61 28 43 29 5d 20 69 6e 74 65 72 r_data(C)] inter
369f0 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 63 face returns a c
36a00 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 opy of the.**
36a10 20 20 20 20 20 20 20 50 20 70 6f 69 6e 74 65 72 P pointer
36a20 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 from the [sqlit
36a30 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
36a40 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 on(D,X,N,E,P,F,S
36a50 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 ,L)].**
36a60 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 or [sqlite3_cre
36a70 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44 ate_function16(D
36a80 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d ,X,N,E,P,F,S,L)]
36a90 20 63 61 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20 call that.**
36aa0 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 registere
36ab0 64 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 d the SQL functi
36ac0 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 on associated wi
36ad0 74 68 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 th [sqlite3_cont
36ae0 65 78 74 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 ext] C..*/.SQLIT
36af0 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
36b00 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 te3_user_data(sq
36b10 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b lite3_context*);
36b20 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
36b30 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 : Database Conne
36b40 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69 ction For Functi
36b50 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 36 ons {H16250} <S6
36b60 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 2a 0600><S20200>.**
36b70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
36b80 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
36b90 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 e() interface re
36ba0 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a turns a copy of.
36bb0 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 ** the pointer t
36bc0 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 o the [database
36bd0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65 connection] (the
36be0 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a 1st parameter).
36bf0 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 ** of the [sqlit
36c00 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
36c10 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 on()].** and [sq
36c20 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
36c30 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 ction16()] routi
36c40 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 nes that origina
36c50 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 lly.** registere
36c60 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f d the applicatio
36c70 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n defined functi
36c80 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 on..**.** INVARI
36c90 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 ANTS:.**.** {H16
36ca0 32 35 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 253} The [sqlite
36cb0 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
36cc0 64 6c 65 28 43 29 5d 20 69 6e 74 65 72 66 61 63 dle(C)] interfac
36cd0 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 e returns a copy
36ce0 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 of the.**
36cf0 20 20 20 20 44 20 70 6f 69 6e 74 65 72 20 66 72 D pointer fr
36d00 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f om the [sqlite3_
36d10 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
36d20 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 D,X,N,E,P,F,S,L)
36d30 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 ].** or
36d40 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
36d50 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44 2c 58 2c _function16(D,X,
36d60 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d 20 63 61 N,E,P,F,S,L)] ca
36d70 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 ll that.**
36d80 20 20 20 20 72 65 67 69 73 74 65 72 65 64 20 74 registered t
36d90 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 he SQL function
36da0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
36db0 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 [sqlite3_context
36dc0 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ] C..*/.SQLITE_A
36dd0 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 PI sqlite3 *sqli
36de0 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
36df0 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f andle(sqlite3_co
36e00 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 ntext*);../*.**
36e10 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 CAPI3REF: Functi
36e20 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 on Auxiliary Dat
36e30 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32 a {H16270} <S202
36e40 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 00>.**.** The fo
36e50 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 llowing two func
36e60 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 tions may be use
36e70 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 d by scalar SQL
36e80 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 functions to.**
36e90 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 associate metada
36ea0 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 ta with argument
36eb0 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 values. If the
36ec0 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 same value is pa
36ed0 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 ssed to.** multi
36ee0 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 ple invocations
36ef0 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 of the same SQL
36f00 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 function during
36f10 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c query execution,
36f20 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 under.** some c
36f30 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 ircumstances the
36f40 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 associated meta
36f50 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 data may be pres
36f60 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a erved. This may.
36f70 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 ** be used, for
36f80 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 example, to add
36f90 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 a regular-expres
36fa0 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 sion matching sc
36fb0 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e alar.** function
36fc0 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 . The compiled v
36fd0 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 ersion of the re
36fe0 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e gular expression
36ff0 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a is stored as.**
37000 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 metadata associ
37010 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 51 ated with the SQ
37020 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 L value passed a
37030 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 s the regular ex
37040 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 pression.** patt
37050 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c ern. The compil
37060 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 ed regular expre
37070 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 ssion can be reu
37080 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a sed on multiple.
37090 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f ** invocations o
370a0 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 f the same funct
370b0 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 ion so that the
370c0 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e original pattern
370d0 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 string.** does
370e0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 not need to be r
370f0 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 ecompiled on eac
37100 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a h invocation..**
37110 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
37120 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e get_auxdata() in
37130 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
37140 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
37150 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 metadata.** ass
37160 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73 ociated by the s
37170 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
37180 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 ta() function wi
37190 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d th the Nth argum
371a0 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 ent.** value to
371b0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
371c0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
371d0 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 . If no metadata
371e0 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a has been ever.*
371f0 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 * been set for t
37200 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 he Nth argument
37210 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c of the function,
37220 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 or if the corre
37230 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 sponding.** func
37240 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 tion parameter h
37250 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 as changed since
37260 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 the meta-data w
37270 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 as set,.** then
37280 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
37290 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 ata() returns a
372a0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a NULL pointer..**
372b0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
372c0 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e set_auxdata() in
372d0 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 68 terface saves th
372e0 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f e metadata.** po
372f0 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 inted to by its
37300 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 3rd parameter as
37310 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f the metadata fo
37320 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72 r the N-th.** ar
37330 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70 gument of the ap
37340 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
37350 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62 d function. Sub
37360 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 sequent.** calls
37370 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f to sqlite3_get_
37380 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20 auxdata() might
37390 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61 return this data
373a0 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e , if it has.** n
373b0 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65 ot been destroye
373c0 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e d..** If it is n
373d0 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20 ot NULL, SQLite
373e0 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 will invoke the
373f0 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75 destructor.** fu
37400 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20 nction given by
37410 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 the 4th paramete
37420 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 r to sqlite3_set
37430 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a _auxdata() on.**
37440 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 68 the metadata wh
37450 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e en the correspon
37460 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 ding function pa
37470 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a rameter changes.
37480 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53 ** or when the S
37490 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d QL statement com
374a0 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 65 pletes, whicheve
374b0 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a r comes first..*
374c0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 *.** SQLite is f
374d0 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 ree to call the
374e0 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 destructor and d
374f0 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 rop metadata on
37500 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 any.** parameter
37510 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e of any function
37520 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54 at any time. T
37530 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 he only guarante
37540 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 e is that.** the
37550 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c destructor will
37560 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 be called befor
37570 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 69 e the metadata i
37580 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a s dropped..**.**
37590 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65 In practice, me
375a0 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65 72 tadata is preser
375b0 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63 ved between func
375c0 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a tion calls for.*
375d0 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 * expressions th
375e0 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 at are constant
375f0 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e at compile time.
37600 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c This includes l
37610 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 iteral.** values
37620 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c and SQL variabl
37630 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 es..**.** These
37640 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 routines must be
37650 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 called from the
37660 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 same thread in
37670 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c which.** the SQL
37680 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e function is run
37690 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 ning..**.** INVA
376a0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
376b0 31 36 32 37 32 7d 20 54 68 65 20 5b 73 71 6c 69 16272} The [sqli
376c0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
376d0 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 C,N)] interface
376e0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
376f0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f r.** to
37700 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 metadata associ
37710 61 74 65 64 20 77 69 74 68 20 74 68 65 20 4e 74 ated with the Nt
37720 68 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 h parameter of t
37730 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 0a he SQL function.
37740 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 6f 73 ** whos
37750 65 20 63 6f 6e 74 65 78 74 20 69 73 20 43 2c 20 e context is C,
37760 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 or NULL if there
37770 20 69 73 20 6e 6f 20 6d 65 74 61 64 61 74 61 20 is no metadata
37780 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 20 20 associated.**
37790 20 20 20 20 20 20 20 77 69 74 68 20 74 68 61 74 with that
377a0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a parameter..**.*
377b0 2a 20 7b 48 31 36 32 37 34 7d 20 54 68 65 20 5b * {H16274} The [
377c0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 sqlite3_set_auxd
377d0 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 69 6e ata(C,N,P,D)] in
377e0 74 65 72 66 61 63 65 20 61 73 73 69 67 6e 73 20 terface assigns
377f0 61 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 20 20 a metadata.**
37800 20 20 20 20 20 20 20 70 6f 69 6e 74 65 72 20 50 pointer P
37810 20 74 6f 20 74 68 65 20 4e 74 68 20 70 61 72 61 to the Nth para
37820 6d 65 74 65 72 20 6f 66 20 74 68 65 20 53 51 4c meter of the SQL
37830 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 63 function with c
37840 6f 6e 74 65 78 74 20 43 2e 0a 2a 2a 0a 2a 2a 20 ontext C..**.**
37850 7b 48 31 36 32 37 36 7d 20 53 51 4c 69 74 65 20 {H16276} SQLite
37860 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 will invoke the
37870 64 65 73 74 72 75 63 74 6f 72 20 44 20 77 69 74 destructor D wit
37880 68 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d h a single argum
37890 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ent.**
378a0 77 68 69 63 68 20 69 73 20 74 68 65 20 6d 65 74 which is the met
378b0 61 64 61 74 61 20 70 6f 69 6e 74 65 72 20 50 20 adata pointer P
378c0 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c following a call
378d0 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 to.**
378e0 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 [sqlite3_set_aux
378f0 64 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 77 data(C,N,P,D)] w
37900 68 65 6e 20 53 51 4c 69 74 65 20 63 65 61 73 65 hen SQLite cease
37910 73 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 20 20 20 s to hold.**
37920 20 20 20 20 20 20 74 68 65 20 6d 65 74 61 64 61 the metada
37930 74 61 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 37 ta..**.** {H1627
37940 37 7d 20 53 51 4c 69 74 65 20 63 65 61 73 65 73 7} SQLite ceases
37950 20 74 6f 20 68 6f 6c 64 20 6d 65 74 61 64 61 74 to hold metadat
37960 61 20 66 6f 72 20 61 6e 20 53 51 4c 20 66 75 6e a for an SQL fun
37970 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 0a ction parameter.
37980 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 65 6e ** when
37990 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
379a0 61 74 20 70 61 72 61 6d 65 74 65 72 20 63 68 61 at parameter cha
379b0 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 nges..**.** {H16
379c0 32 37 38 7d 20 57 68 65 6e 20 5b 73 71 6c 69 74 278} When [sqlit
379d0 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 43 e3_set_auxdata(C
379e0 2c 4e 2c 50 2c 44 29 5d 20 69 73 20 69 6e 76 6f ,N,P,D)] is invo
379f0 6b 65 64 2c 20 74 68 65 20 64 65 73 74 72 75 63 ked, the destruc
37a00 74 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 tor.**
37a10 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 6e is called for an
37a20 79 20 70 72 69 6f 72 20 6d 65 74 61 64 61 74 61 y prior metadata
37a30 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
37a40 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 the same functi
37a50 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 on.** c
37a60 6f 6e 74 65 78 74 20 43 20 61 6e 64 20 70 61 72 ontext C and par
37a70 61 6d 65 74 65 72 20 4e 2e 0a 2a 2a 0a 2a 2a 20 ameter N..**.**
37a80 7b 48 31 36 32 37 39 7d 20 53 51 4c 69 74 65 20 {H16279} SQLite
37a90 77 69 6c 6c 20 63 61 6c 6c 20 64 65 73 74 72 75 will call destru
37aa0 63 74 6f 72 73 20 66 6f 72 20 61 6e 79 20 6d 65 ctors for any me
37ab0 74 61 64 61 74 61 20 69 74 20 69 73 20 68 6f 6c tadata it is hol
37ac0 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 ding.**
37ad0 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 in a particular
37ae0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
37af0 6d 65 6e 74 5d 20 53 20 77 68 65 6e 20 65 69 74 ment] S when eit
37b00 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 her.**
37b10 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 [sqlite3_reset(S
37b20 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 )] or [sqlite3_f
37b30 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20 63 inalize(S)] is c
37b40 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 alled..*/.SQLITE
37b50 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
37b60 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 73 e3_get_auxdata(s
37b70 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
37b80 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f int N);.SQLITE_
37b90 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
37ba0 5f 73 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c _set_auxdata(sql
37bb0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 ite3_context*, i
37bc0 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f 69 nt N, void*, voi
37bd0 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 0a d (*)(void*));..
37be0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
37bf0 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66 69 6e Constants Defin
37c00 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65 73 74 ing Special Dest
37c10 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f 72 20 ructor Behavior
37c20 7b 48 31 30 32 38 30 7d 20 3c 53 33 30 31 30 30 {H10280} <S30100
37c30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72 >.**.** These ar
37c40 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 73 e special values
37c50 20 66 6f 72 20 74 68 65 20 64 65 73 74 72 75 63 for the destruc
37c60 74 6f 72 20 74 68 61 74 20 69 73 20 70 61 73 73 tor that is pass
37c70 65 64 20 69 6e 20 61 73 20 74 68 65 0a 2a 2a 20 ed in as the.**
37c80 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 74 final argument t
37c90 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 o routines like
37ca0 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f [sqlite3_result_
37cb0 62 6c 6f 62 28 29 5d 2e 20 20 49 66 20 74 68 65 blob()]. If the
37cc0 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 61 destructor.** a
37cd0 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 rgument is SQLIT
37ce0 45 5f 53 54 41 54 49 43 2c 20 69 74 20 6d 65 61 E_STATIC, it mea
37cf0 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 ns that the cont
37d00 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 20 63 ent pointer is c
37d10 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 77 onstant.** and w
37d20 69 6c 6c 20 6e 65 76 65 72 20 63 68 61 6e 67 65 ill never change
37d30 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 6e . It does not n
37d40 65 65 64 20 74 6f 20 62 65 20 64 65 73 74 72 6f eed to be destro
37d50 79 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53 51 4c yed. The.** SQL
37d60 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 76 61 ITE_TRANSIENT va
37d70 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 74 lue means that t
37d80 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 he content will
37d90 6c 69 6b 65 6c 79 20 63 68 61 6e 67 65 20 69 6e likely change in
37da0 0a 2a 2a 20 74 68 65 20 6e 65 61 72 20 66 75 74 .** the near fut
37db0 75 72 65 20 61 6e 64 20 74 68 61 74 20 53 51 4c ure and that SQL
37dc0 69 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 ite should make
37dd0 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 its own private
37de0 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63 copy of.** the c
37df0 6f 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 72 65 ontent before re
37e00 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 turning..**.** T
37e10 68 65 20 74 79 70 65 64 65 66 20 69 73 20 6e 65 he typedef is ne
37e20 63 65 73 73 61 72 79 20 74 6f 20 77 6f 72 6b 20 cessary to work
37e30 61 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d 73 20 around problems
37e40 69 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 43 2b in certain.** C+
37e50 2b 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20 53 65 + compilers. Se
37e60 65 20 74 69 63 6b 65 74 20 23 32 31 39 31 2e 0a e ticket #2191..
37e70 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64 20 */.typedef void
37e80 28 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 (*sqlite3_destru
37e90 63 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 64 2a ctor_type)(void*
37ea0 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 );.#define SQLIT
37eb0 45 5f 53 54 41 54 49 43 20 20 20 20 20 20 28 28 E_STATIC ((
37ec0 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 sqlite3_destruct
37ed0 6f 72 5f 74 79 70 65 29 30 29 0a 23 64 65 66 69 or_type)0).#defi
37ee0 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 ne SQLITE_TRANSI
37ef0 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65 33 5f ENT ((sqlite3_
37f00 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 destructor_type)
37f10 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 -1)../*.** CAPI3
37f20 52 45 46 3a 20 53 65 74 74 69 6e 67 20 54 68 65 REF: Setting The
37f30 20 52 65 73 75 6c 74 20 4f 66 20 41 6e 20 53 51 Result Of An SQ
37f40 4c 20 46 75 6e 63 74 69 6f 6e 20 7b 48 31 36 34 L Function {H164
37f50 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 00} <S20200>.**.
37f60 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
37f70 73 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 s are used by th
37f80 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69 6e 61 e xFunc or xFina
37f90 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 l callbacks that
37fa0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 53 51 .** implement SQ
37fb0 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 L functions and
37fc0 61 67 67 72 65 67 61 74 65 73 2e 20 20 53 65 65 aggregates. See
37fd0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 .** [sqlite3_cre
37fe0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20 ate_function()]
37ff0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 and [sqlite3_cre
38000 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 ate_function16()
38010 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 6f ].** for additio
38020 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
38030 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e .**.** These fun
38040 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65 72 79 ctions work very
38050 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20 5b much like the [
38060 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e parameter bindin
38070 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 g] family of.**
38080 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 functions used t
38090 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20 74 6f o bind values to
380a0 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 host parameters
380b0 20 69 6e 20 70 72 65 70 61 72 65 64 20 73 74 61 in prepared sta
380c0 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65 66 65 tements..** Refe
380d0 72 20 74 6f 20 74 68 65 20 5b 53 51 4c 20 70 61 r to the [SQL pa
380e0 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 6d 65 6e rameter] documen
380f0 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 tation for addit
38100 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
38110 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c n..**.** The sql
38120 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
38130 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 () interface set
38140 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f s the result fro
38150 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 74 m.** an applicat
38160 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
38170 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 42 tion to be the B
38180 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74 65 6e LOB whose conten
38190 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 t is pointed.**
381a0 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 to by the second
381b0 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 77 parameter and w
381c0 68 69 63 68 20 69 73 20 4e 20 62 79 74 65 73 20 hich is N bytes
381d0 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69 73 20 long where N is
381e0 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70 61 72 the.** third par
381f0 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ameter..**.** Th
38200 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
38210 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e 74 65 _zeroblob() inte
38220 72 66 61 63 65 73 20 73 65 74 20 74 68 65 20 72 rfaces set the r
38230 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 esult of.** the
38240 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
38250 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 ned function to
38260 62 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74 61 69 be a BLOB contai
38270 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a ning all zero.**
38280 20 62 79 74 65 73 20 61 6e 64 20 4e 20 62 79 74 bytes and N byt
38290 65 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65 72 es in size, wher
382a0 65 20 4e 20 69 73 20 74 68 65 20 76 61 6c 75 65 e N is the value
382b0 20 6f 66 20 74 68 65 20 32 6e 64 20 70 61 72 61 of the 2nd para
382c0 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 meter..**.** The
382d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
382e0 64 6f 75 62 6c 65 28 29 20 69 6e 74 65 72 66 61 double() interfa
382f0 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75 ce sets the resu
38300 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 lt from.** an ap
38310 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
38320 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 d function to be
38330 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e a floating poin
38340 74 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 t value specifie
38350 64 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e 64 20 d.** by its 2nd
38360 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
38370 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
38380 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73 lt_error() and s
38390 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
383a0 72 6f 72 31 36 28 29 20 66 75 6e 63 74 69 6f 6e ror16() function
383b0 73 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 20 69 s.** cause the i
383c0 6d 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20 66 mplemented SQL f
383d0 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f 77 unction to throw
383e0 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a an exception..*
383f0 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 68 * SQLite uses th
38400 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 e string pointed
38410 20 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20 32 6e to by the.** 2n
38420 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 d parameter of s
38430 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
38440 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74 65 33 ror() or sqlite3
38450 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
38460 29 0a 2a 2a 20 61 73 20 74 68 65 20 74 65 78 74 ).** as the text
38470 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 of an error mes
38480 73 61 67 65 2e 20 20 53 51 4c 69 74 65 20 69 6e sage. SQLite in
38490 74 65 72 70 72 65 74 73 20 74 68 65 20 65 72 72 terprets the err
384a0 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 73 74 or.** message st
384b0 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65 ring from sqlite
384c0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 3_result_error()
384d0 20 61 73 20 55 54 46 2d 38 2e 20 53 51 4c 69 74 as UTF-8. SQLit
384e0 65 0a 2a 2a 20 69 6e 74 65 72 70 72 65 74 73 20 e.** interprets
384f0 74 68 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 the string from
38500 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
38510 72 72 6f 72 31 36 28 29 20 61 73 20 55 54 46 2d rror16() as UTF-
38520 31 36 20 69 6e 20 6e 61 74 69 76 65 0a 2a 2a 20 16 in native.**
38530 62 79 74 65 20 6f 72 64 65 72 2e 20 20 49 66 20 byte order. If
38540 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 the third parame
38550 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 ter to sqlite3_r
38560 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a esult_error().**
38570 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 or sqlite3_resu
38580 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73 20 lt_error16() is
38590 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 53 51 negative then SQ
385a0 4c 69 74 65 20 74 61 6b 65 73 20 61 73 20 74 68 Lite takes as th
385b0 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 e error.** messa
385c0 67 65 20 61 6c 6c 20 74 65 78 74 20 75 70 20 74 ge all text up t
385d0 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 73 74 hrough the first
385e0 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e zero character.
385f0 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69 72 64 .** If the third
38600 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
38610 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
38620 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 or() or.** sqlit
38630 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 e3_result_error1
38640 36 28 29 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 6() is non-negat
38650 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20 ive then SQLite
38660 74 61 6b 65 73 20 74 68 61 74 20 6d 61 6e 79 0a takes that many.
38670 2a 2a 20 62 79 74 65 73 20 28 6e 6f 74 20 63 68 ** bytes (not ch
38680 61 72 61 63 74 65 72 73 29 20 66 72 6f 6d 20 74 aracters) from t
38690 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 he 2nd parameter
386a0 20 61 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65 as the error me
386b0 73 73 61 67 65 2e 0a 2a 2a 20 54 68 65 20 73 71 ssage..** The sq
386c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
386d0 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 or() and sqlite3
386e0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
386f0 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 6d 61 ).** routines ma
38700 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 6f 70 ke a private cop
38710 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 6d y of the error m
38720 65 73 73 61 67 65 20 74 65 78 74 20 62 65 66 6f essage text befo
38730 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74 75 72 re.** they retur
38740 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 63 n. Hence, the c
38750 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 alling function
38760 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65 20 6f can deallocate o
38770 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 65 20 r.** modify the
38780 74 65 78 74 20 61 66 74 65 72 20 74 68 65 79 20 text after they
38790 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 68 return without h
387a0 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 arm..** The sqli
387b0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
387c0 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69 6f 6e _code() function
387d0 20 63 68 61 6e 67 65 73 20 74 68 65 20 65 72 72 changes the err
387e0 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74 75 72 or code.** retur
387f0 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 73 ned by SQLite as
38800 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 a result of an
38810 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e 63 74 error in a funct
38820 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75 6c 74 ion. By default
38830 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63 ,.** the error c
38840 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f 45 52 ode is SQLITE_ER
38850 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71 75 65 ROR. A subseque
38860 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 nt call to sqlit
38870 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
38880 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f ).** or sqlite3_
38890 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 result_error16()
388a0 20 72 65 73 65 74 73 20 74 68 65 20 65 72 72 6f resets the erro
388b0 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 45 r code to SQLITE
388c0 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 68 _ERROR..**.** Th
388d0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
388e0 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65 72 66 _toobig() interf
388f0 61 63 65 20 63 61 75 73 65 73 20 53 51 4c 69 74 ace causes SQLit
38900 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 72 e to throw an er
38910 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e ror.** indicatin
38920 67 20 74 68 61 74 20 61 20 73 74 72 69 6e 67 20 g that a string
38930 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20 6c 6f or BLOB is to lo
38940 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e 74 2e ng to represent.
38950 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
38960 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28 e3_result_nomem(
38970 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 ) interface caus
38980 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68 72 es SQLite to thr
38990 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 ow an error.** i
389a0 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 ndicating that a
389b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
389c0 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a on failed..**.**
389d0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
389e0 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65 72 66 ult_int() interf
389f0 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74 ace sets the ret
38a00 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 urn value.** of
38a10 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
38a20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
38a30 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d 62 69 to be the 32-bi
38a40 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
38a50 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e 20 .** value given
38a60 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75 6d in the 2nd argum
38a70 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ent..** The sqli
38a80 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 te3_result_int64
38a90 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 () interface set
38aa0 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c s the return val
38ab0 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 ue.** of the app
38ac0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
38ad0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 function to be
38ae0 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 the 64-bit signe
38af0 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61 6c d integer.** val
38b00 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 ue given in the
38b10 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 2nd argument..**
38b20 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
38b30 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e result_null() in
38b40 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 terface sets the
38b50 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
38b60 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 of the applicat
38b70 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
38b80 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c 4c 2e tion to be NULL.
38b90 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
38ba0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 29 e3_result_text()
38bb0 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 , sqlite3_result
38bc0 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 73 71 _text16(),.** sq
38bd0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
38be0 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 73 71 6c t16le(), and sql
38bf0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
38c00 31 36 62 65 28 29 20 69 6e 74 65 72 66 61 63 65 16be() interface
38c10 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 72 65 74 s.** set the ret
38c20 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 urn value of the
38c30 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
38c40 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f ined function to
38c50 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 20 73 74 be.** a text st
38c60 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 72 65 ring which is re
38c70 70 72 65 73 65 6e 74 65 64 20 61 73 20 55 54 46 presented as UTF
38c80 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 74 69 76 -8, UTF-16 nativ
38c90 65 20 62 79 74 65 20 6f 72 64 65 72 2c 0a 2a 2a e byte order,.**
38ca0 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 65 UTF-16 little e
38cb0 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36 ndian, or UTF-16
38cc0 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 72 65 73 big endian, res
38cd0 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 53 51 pectively..** SQ
38ce0 4c 69 74 65 20 74 61 6b 65 73 20 74 68 65 20 74 Lite takes the t
38cf0 65 78 74 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 ext result from
38d00 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
38d10 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 32 6e 64 20 from.** the 2nd
38d20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65 parameter of the
38d30 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
38d40 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 text* interfaces
38d50 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20 ..** If the 3rd
38d60 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
38d70 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
38d80 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 text* interfaces
38d90 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 76 65 2c .** is negative,
38da0 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b then SQLite tak
38db0 65 73 20 72 65 73 75 6c 74 20 74 65 78 74 20 66 es result text f
38dc0 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72 61 rom the 2nd para
38dd0 6d 65 74 65 72 0a 2a 2a 20 74 68 72 6f 75 67 68 meter.** through
38de0 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 the first zero
38df0 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 character..** If
38e00 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 the 3rd paramet
38e10 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 er to the sqlite
38e20 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 3_result_text* i
38e30 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73 20 nterfaces.** is
38e40 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 non-negative, th
38e50 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74 65 73 en as many bytes
38e60 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 72 73 (not characters
38e70 29 20 6f 66 20 74 68 65 20 74 65 78 74 0a 2a 2a ) of the text.**
38e80 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 pointed to by t
38e90 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 he 2nd parameter
38ea0 20 61 72 65 20 74 61 6b 65 6e 20 61 73 20 74 68 are taken as th
38eb0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
38ec0 66 69 6e 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f fined.** functio
38ed0 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 n result..** If
38ee0 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 the 4th paramete
38ef0 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 r to the sqlite3
38f00 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e _result_text* in
38f10 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 terfaces.** or s
38f20 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c qlite3_result_bl
38f30 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c ob is a non-NULL
38f40 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 pointer, then S
38f50 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 61 74 QLite calls that
38f60 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 .** function as
38f70 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 6f the destructor o
38f80 6e 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c n the text or BL
38f90 4f 42 20 72 65 73 75 6c 74 20 77 68 65 6e 20 69 OB result when i
38fa0 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68 65 t has.** finishe
38fb0 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 65 73 d using that res
38fc0 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 ult..** If the 4
38fd0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 th parameter to
38fe0 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 the sqlite3_resu
38ff0 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 lt_text* interfa
39000 63 65 73 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 ces or.** sqlite
39010 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 3_result_blob is
39020 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e the special con
39030 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 stant SQLITE_STA
39040 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 TIC, then SQLite
39050 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 .** assumes that
39060 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f the text or BLO
39070 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63 B result is in c
39080 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e onstant space an
39090 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f d does not.** co
390a0 70 79 20 74 68 65 20 69 74 20 6f 72 20 63 61 6c py the it or cal
390b0 6c 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 77 l a destructor w
390c0 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 hen it has finis
390d0 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 hed using that r
390e0 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 esult..** If the
390f0 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 4th parameter t
39100 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 o the sqlite3_re
39110 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 sult_text* inter
39120 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 faces.** or sqli
39130 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 te3_result_blob
39140 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 is the special c
39150 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 54 onstant SQLITE_T
39160 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 6e RANSIENT.** then
39170 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 SQLite makes a
39180 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 copy of the resu
39190 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 lt into space ob
391a0 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 66 tained from.** f
391b0 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c rom [sqlite3_mal
391c0 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69 74 loc()] before it
391d0 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 returns..**.**
391e0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
391f0 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 lt_value() inter
39200 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 face sets the re
39210 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 sult of.** the a
39220 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
39230 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 ed function to b
39240 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 e a copy the.**
39250 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c [unprotected sql
39260 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
39270 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ct specified by
39280 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
39290 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 r. The.** sqlit
392a0 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 e3_result_value(
392b0 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 ) interface make
392c0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
392d0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a [sqlite3_value].
392e0 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b ** so that the [
392f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73 sqlite3_value] s
39300 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 pecified in the
39310 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68 parameter may ch
39320 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65 ange or.** be de
39330 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20 allocated after
39340 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 sqlite3_result_v
39350 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20 77 alue() returns w
39360 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 ithout harm..**
39370 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c A [protected sql
39380 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
39390 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65 ct may always be
393a0 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a used where an.*
393b0 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 * [unprotected s
393c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 qlite3_value] ob
393d0 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65 64 ject is required
393e0 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b , so either.** k
393f0 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f ind of [sqlite3_
39400 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61 value] object ca
39410 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 n be used with t
39420 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a his interface..*
39430 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f *.** If these ro
39440 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 utines are calle
39450 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 d from within th
39460 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 e different thre
39470 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f ad.** than the o
39480 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ne containing th
39490 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
394a0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
394b0 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20 hat received.**
394c0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e the [sqlite3_con
394d0 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74 text] pointer, t
394e0 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 he results are u
394f0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
39500 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
39510 2a 20 7b 48 31 36 34 30 33 7d 20 54 68 65 20 64 * {H16403} The d
39520 65 66 61 75 6c 74 20 72 65 74 75 72 6e 20 76 61 efault return va
39530 6c 75 65 20 66 72 6f 6d 20 61 6e 79 20 53 51 4c lue from any SQL
39540 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c function is NUL
39550 4c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 30 36 L..**.** {H16406
39560 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 } The [sqlite3_r
39570 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e esult_blob(C,V,N
39580 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 ,D)] interface c
39590 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20 hanges the.**
395a0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 return va
395b0 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 lue of function
395c0 43 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 74 C to be a BLOB t
395d0 68 61 74 20 69 73 20 4e 20 62 79 74 65 73 0a 2a hat is N bytes.*
395e0 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 6c 65 * in le
395f0 6e 67 74 68 20 61 6e 64 20 77 69 74 68 20 63 6f ngth and with co
39600 6e 74 65 6e 74 20 70 6f 69 6e 74 65 64 20 74 6f ntent pointed to
39610 20 62 79 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 by V..**.** {H1
39620 36 34 30 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 6409} The [sqlit
39630 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 e3_result_double
39640 28 43 2c 56 29 5d 20 69 6e 74 65 72 66 61 63 65 (C,V)] interface
39650 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 changes the.**
39660 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
39670 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f value of functio
39680 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 66 6c n C to be the fl
39690 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
396a0 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 ue V..**.** {H16
396b0 34 31 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 412} The [sqlite
396c0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 43 3_result_error(C
396d0 2c 56 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 ,V,N)] interface
396e0 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65 74 changes the ret
396f0 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 urn.**
39700 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f value of functio
39710 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63 n C to be an exc
39720 65 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f eption with erro
39730 72 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20 r code.**
39740 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 [SQLITE_ERROR
39750 5d 20 61 6e 64 20 61 20 55 54 46 2d 38 20 65 72 ] and a UTF-8 er
39760 72 6f 72 20 6d 65 73 73 61 67 65 20 63 6f 70 69 ror message copi
39770 65 64 20 66 72 6f 6d 20 56 20 75 70 20 74 6f 20 ed from V up to
39780 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
39790 66 69 72 73 74 20 7a 65 72 6f 20 62 79 74 65 20 first zero byte
397a0 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 65 73 or until N bytes
397b0 20 61 72 65 20 72 65 61 64 20 69 66 20 4e 20 69 are read if N i
397c0 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a s positive..**.*
397d0 2a 20 7b 48 31 36 34 31 35 7d 20 54 68 65 20 5b * {H16415} The [
397e0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
397f0 72 72 6f 72 31 36 28 43 2c 56 2c 4e 29 5d 20 69 rror16(C,V,N)] i
39800 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 nterface changes
39810 20 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20 the return.**
39820 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 value of
39830 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 function C to b
39840 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 77 e an exception w
39850 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a ith error code.*
39860 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 * [SQLI
39870 54 45 5f 45 52 52 4f 52 5d 20 61 6e 64 20 61 20 TE_ERROR] and a
39880 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 UTF-16 native by
39890 74 65 20 6f 72 64 65 72 20 65 72 72 6f 72 20 6d te order error m
398a0 65 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 20 essage.**
398b0 20 20 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 56 copied from V
398c0 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 up to the first
398d0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
398e0 20 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 65 or until N byte
398f0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72 s.** ar
39900 65 20 72 65 61 64 20 69 66 20 4e 20 69 73 20 70 e read if N is p
39910 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b ositive..**.** {
39920 48 31 36 34 31 38 7d 20 54 68 65 20 5b 73 71 6c H16418} The [sql
39930 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
39940 72 5f 74 6f 6f 62 69 67 28 43 29 5d 20 69 6e 74 r_toobig(C)] int
39950 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 erface changes t
39960 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 he return.**
39970 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20 74 value of t
39980 68 65 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f he function C to
39990 20 62 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e be an exception
399a0 20 77 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65 with error code
399b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 .** [SQ
399c0 4c 49 54 45 5f 54 4f 4f 42 49 47 5d 20 61 6e 64 LITE_TOOBIG] and
399d0 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
399e0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a error message..*
399f0 2a 0a 2a 2a 20 7b 48 31 36 34 32 31 7d 20 54 68 *.** {H16421} Th
39a00 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c e [sqlite3_resul
39a10 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 43 29 t_error_nomem(C)
39a20 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e ] interface chan
39a30 67 65 73 20 74 68 65 20 72 65 74 75 72 6e 0a 2a ges the return.*
39a40 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 * value
39a50 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
39a60 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63 65 C to be an exce
39a70 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f 72 ption with error
39a80 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20 20 code.**
39a90 20 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d [SQLITE_NOMEM]
39aa0 20 61 6e 64 20 61 6e 20 61 70 70 72 6f 70 72 69 and an appropri
39ab0 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ate error messag
39ac0 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 32 34 e..**.** {H16424
39ad0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 } The [sqlite3_r
39ae0 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 esult_error_code
39af0 28 43 2c 45 29 5d 20 69 6e 74 65 72 66 61 63 65 (C,E)] interface
39b00 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65 74 changes the ret
39b10 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 urn.**
39b20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 75 6e value of the fun
39b30 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e ction C to be an
39b40 20 65 78 63 65 70 74 69 6f 6e 20 77 69 74 68 20 exception with
39b50 65 72 72 6f 72 20 63 6f 64 65 20 45 2e 0a 2a 2a error code E..**
39b60 20 20 20 20 20 20 20 20 20 20 54 68 65 20 65 72 The er
39b70 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 ror message text
39b80 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a is unchanged..*
39b90 2a 0a 2a 2a 20 7b 48 31 36 34 32 37 7d 20 54 68 *.** {H16427} Th
39ba0 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c e [sqlite3_resul
39bb0 74 5f 69 6e 74 28 43 2c 56 29 5d 20 69 6e 74 65 t_int(C,V)] inte
39bc0 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 rface changes th
39bd0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 e.** re
39be0 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 turn value of fu
39bf0 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 nction C to be t
39c00 68 65 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 he 32-bit intege
39c10 72 20 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a r value V..**.**
39c20 20 7b 48 31 36 34 33 30 7d 20 54 68 65 20 5b 73 {H16430} The [s
39c30 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
39c40 74 36 34 28 43 2c 56 29 5d 20 69 6e 74 65 72 66 t64(C,V)] interf
39c50 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a ace changes the.
39c60 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ** retu
39c70 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 rn value of func
39c80 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65 tion C to be the
39c90 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 64-bit integer
39ca0 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b value V..**.** {
39cb0 48 31 36 34 33 33 7d 20 54 68 65 20 5b 73 71 6c H16433} The [sql
39cc0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c ite3_result_null
39cd0 28 43 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 (C)] interface c
39ce0 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20 hanges the.**
39cf0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 return va
39d00 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 lue of function
39d10 43 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a C to be NULL..**
39d20 0a 2a 2a 20 7b 48 31 36 34 33 36 7d 20 54 68 65 .** {H16436} The
39d30 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
39d40 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 20 _text(C,V,N,D)]
39d50 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 interface change
39d60 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
39d70 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f return value o
39d80 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 f function C to
39d90 62 65 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 be the UTF-8 str
39da0 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ing.**
39db0 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 V up to the firs
39dc0 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 20 6e t zero if N is n
39dd0 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 20 20 egative.**
39de0 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 73 74 or the first
39df0 20 4e 20 62 79 74 65 73 20 6f 66 20 56 20 69 66 N bytes of V if
39e00 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 N is non-negati
39e10 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 33 ve..**.** {H1643
39e20 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 9} The [sqlite3_
39e30 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43 2c result_text16(C,
39e40 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 V,N,D)] interfac
39e50 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a e changes the.**
39e60 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
39e70 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 value of functi
39e80 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 55 on C to be the U
39e90 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 TF-16 native byt
39ea0 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20 20 20 e order.**
39eb0 20 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 20 string V up
39ec0 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 to the first zer
39ed0 6f 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69 o if N is negati
39ee0 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f ve.** o
39ef0 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 r the first N by
39f00 74 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73 tes of V if N is
39f10 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a non-negative..*
39f20 2a 0a 2a 2a 20 7b 48 31 36 34 34 32 7d 20 54 68 *.** {H16442} Th
39f30 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c e [sqlite3_resul
39f40 74 5f 74 65 78 74 31 36 62 65 28 43 2c 56 2c 4e t_text16be(C,V,N
39f50 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 ,D)] interface c
39f60 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20 hanges the.**
39f70 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 return va
39f80 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 lue of function
39f90 43 20 74 6f 20 62 65 20 74 68 65 20 55 54 46 2d C to be the UTF-
39fa0 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a 2a 16 big-endian.**
39fb0 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 string
39fc0 20 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 V up to the fir
39fd0 73 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 20 st zero if N is
39fe0 6e 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 20 negative.**
39ff0 20 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 73 or the firs
3a000 74 20 4e 20 62 79 74 65 73 20 6f 72 20 56 20 69 t N bytes or V i
3a010 66 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 f N is non-negat
3a020 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 ive..**.** {H164
3a030 34 35 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 45} The [sqlite3
3a040 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 _result_text16le
3a050 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72 (C,V,N,D)] inter
3a060 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 face changes the
3a070 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 .** ret
3a080 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e urn value of fun
3a090 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 ction C to be th
3a0a0 65 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 2d e UTF-16 little-
3a0b0 65 6e 64 69 61 6e 0a 2a 2a 20 20 20 20 20 20 20 endian.**
3a0c0 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 20 74 string V up t
3a0d0 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f o the first zero
3a0e0 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69 76 if N is negativ
3a0f0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 e.** or
3a100 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 74 the first N byt
3a110 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73 20 es of V if N is
3a120 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2a non-negative..**
3a130 0a 2a 2a 20 7b 48 31 36 34 34 38 7d 20 54 68 65 .** {H16448} The
3a140 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
3a150 5f 76 61 6c 75 65 28 43 2c 56 29 5d 20 69 6e 74 _value(C,V)] int
3a160 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 erface changes t
3a170 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 he.** r
3a180 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 eturn value of f
3a190 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 unction C to be
3a1a0 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 the [unprotected
3a1b0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a sqlite3_value].
3a1c0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 6a 65 ** obje
3a1d0 63 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 ct V..**.** {H16
3a1e0 34 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 451} The [sqlite
3a1f0 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 3_result_zeroblo
3a200 62 28 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 b(C,N)] interfac
3a210 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a e changes the.**
3a220 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
3a230 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 value of functi
3a240 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 4e 2d on C to be an N-
3a250 62 79 74 65 20 42 4c 4f 42 20 6f 66 20 61 6c 6c byte BLOB of all
3a260 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 zeros..**.** {H
3a270 31 36 34 35 34 7d 20 54 68 65 20 5b 73 71 6c 69 16454} The [sqli
3a280 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
3a290 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
3a2a0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
3a2b0 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 )].** i
3a2c0 6e 74 65 72 66 61 63 65 73 20 6d 61 6b 65 20 61 nterfaces make a
3a2d0 20 63 6f 70 79 20 6f 66 20 74 68 65 69 72 20 65 copy of their e
3a2e0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 rror message str
3a2f0 69 6e 67 73 20 62 65 66 6f 72 65 0a 2a 2a 20 20 ings before.**
3a300 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 69 6e returnin
3a310 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 35 37 g..**.** {H16457
3a320 7d 20 49 66 20 74 68 65 20 44 20 64 65 73 74 72 } If the D destr
3a330 75 63 74 6f 72 20 70 61 72 61 6d 65 74 65 72 20 uctor parameter
3a340 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 to [sqlite3_resu
3a350 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e 2c 44 29 lt_blob(C,V,N,D)
3a360 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b ],.** [
3a370 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
3a380 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 2c 20 5b ext(C,V,N,D)], [
3a390 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
3a3a0 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44 29 5d 2c ext16(C,V,N,D)],
3a3b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
3a3c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
3a3d0 74 31 36 62 65 28 43 2c 56 2c 4e 2c 44 29 5d 2c t16be(C,V,N,D)],
3a3e0 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 or.**
3a3f0 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f [sqlite3_result_
3a400 74 65 78 74 31 36 6c 65 28 43 2c 56 2c 4e 2c 44 text16le(C,V,N,D
3a410 29 5d 20 69 73 20 74 68 65 20 63 6f 6e 73 74 61 )] is the consta
3a420 6e 74 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 nt [SQLITE_STATI
3a430 43 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 C].** t
3a440 68 65 6e 20 6e 6f 20 64 65 73 74 72 75 63 74 6f hen no destructo
3a450 72 20 69 73 20 65 76 65 72 20 63 61 6c 6c 65 64 r is ever called
3a460 20 6f 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 on the pointer
3a470 56 20 61 6e 64 20 53 51 4c 69 74 65 0a 2a 2a 20 V and SQLite.**
3a480 20 20 20 20 20 20 20 20 20 61 73 73 75 6d 65 73 assumes
3a490 20 74 68 61 74 20 56 20 69 73 20 69 6d 6d 75 74 that V is immut
3a4a0 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 able..**.** {H16
3a4b0 34 36 30 7d 20 49 66 20 74 68 65 20 44 20 64 65 460} If the D de
3a4c0 73 74 72 75 63 74 6f 72 20 70 61 72 61 6d 65 74 structor paramet
3a4d0 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 er to [sqlite3_r
3a4e0 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e esult_blob(C,V,N
3a4f0 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 ,D)],.**
3a500 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c [sqlite3_resul
3a510 74 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d t_text(C,V,N,D)]
3a520 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c , [sqlite3_resul
3a530 74 5f 74 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44 t_text16(C,V,N,D
3a540 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 )],.**
3a550 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f [sqlite3_result_
3a560 74 65 78 74 31 36 62 65 28 43 2c 56 2c 4e 2c 44 text16be(C,V,N,D
3a570 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 )], or.**
3a580 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 [sqlite3_resu
3a590 6c 74 5f 74 65 78 74 31 36 6c 65 28 43 2c 56 2c lt_text16le(C,V,
3a5a0 4e 2c 44 29 5d 20 69 73 20 74 68 65 20 63 6f 6e N,D)] is the con
3a5b0 73 74 61 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 stant.**
3a5c0 20 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 [SQLITE_TRANSI
3a5d0 45 4e 54 5d 20 74 68 65 6e 20 74 68 65 20 69 6e ENT] then the in
3a5e0 74 65 72 66 61 63 65 73 20 6d 61 6b 65 73 20 61 terfaces makes a
3a5f0 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 copy of the.**
3a600 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 content
3a610 20 6f 66 20 56 20 61 6e 64 20 72 65 74 61 69 6e of V and retain
3a620 73 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 0a 2a s the copy..**.*
3a630 2a 20 7b 48 31 36 34 36 33 7d 20 49 66 20 74 68 * {H16463} If th
3a640 65 20 44 20 64 65 73 74 72 75 63 74 6f 72 20 70 e D destructor p
3a650 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
3a660 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
3a670 28 43 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 (C,V,N,D)],.**
3a680 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
3a690 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 43 2c 56 _result_text(C,V
3a6a0 2c 4e 2c 44 29 5d 2c 20 5b 73 71 6c 69 74 65 33 ,N,D)], [sqlite3
3a6b0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43 _result_text16(C
3a6c0 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 ,V,N,D)],.**
3a6d0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 [sqlite3_r
3a6e0 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 43 esult_text16be(C
3a6f0 2c 56 2c 4e 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20 ,V,N,D)], or.**
3a700 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
3a710 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 3_result_text16l
3a720 65 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 73 20 73 e(C,V,N,D)] is s
3a730 6f 6d 65 20 76 61 6c 75 65 20 6f 74 68 65 72 20 ome value other
3a740 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 than.**
3a750 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b the constants [
3a760 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 20 61 SQLITE_STATIC] a
3a770 6e 64 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 nd [SQLITE_TRANS
3a780 49 45 4e 54 5d 20 74 68 65 6e 0a 2a 2a 20 20 20 IENT] then.**
3a790 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 77 69 SQLite wi
3a7a0 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 65 ll invoke the de
3a7b0 73 74 72 75 63 74 6f 72 20 44 20 77 69 74 68 20 structor D with
3a7c0 56 20 61 73 20 69 74 73 20 6f 6e 6c 79 20 61 72 V as its only ar
3a7d0 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 gument.**
3a7e0 20 20 20 77 68 65 6e 20 69 74 20 68 61 73 20 66 when it has f
3a7f0 69 6e 69 73 68 65 64 20 77 69 74 68 20 74 68 65 inished with the
3a800 20 56 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c V value..*/.SQL
3a810 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
3a820 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
3a830 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
3a840 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
3a850 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 int, void(*)(voi
3a860 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 d*));.SQLITE_API
3a870 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
3a880 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 sult_double(sqli
3a890 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f te3_context*, do
3a8a0 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 uble);.SQLITE_AP
3a8b0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
3a8c0 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 esult_error(sqli
3a8d0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f te3_context*, co
3a8e0 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b nst char*, int);
3a8f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
3a900 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
3a910 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f error16(sqlite3_
3a920 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 context*, const
3a930 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c void*, int);.SQL
3a940 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
3a950 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
3a960 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 r_toobig(sqlite3
3a970 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 _context*);.SQLI
3a980 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
3a990 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
3a9a0 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 _nomem(sqlite3_c
3a9b0 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 ontext*);.SQLITE
3a9c0 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
3a9d0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 3_result_error_c
3a9e0 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ode(sqlite3_cont
3a9f0 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ext*, int);.SQLI
3aa00 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
3aa10 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73 te3_result_int(s
3aa20 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
3aa30 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 int);.SQLITE_AP
3aa40 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
3aa50 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 esult_int64(sqli
3aa60 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71 te3_context*, sq
3aa70 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 lite3_int64);.SQ
3aa80 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
3aa90 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c lite3_result_nul
3aaa0 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 l(sqlite3_contex
3aab0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 t*);.SQLITE_API
3aac0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
3aad0 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 33 ult_text(sqlite3
3aae0 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 _context*, const
3aaf0 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f 69 char*, int, voi
3ab00 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
3ab10 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
3ab20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
3ab30 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 t16(sqlite3_cont
3ab40 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ext*, const void
3ab50 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 *, int, void(*)(
3ab60 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f void*));.SQLITE_
3ab70 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
3ab80 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 _result_text16le
3ab90 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
3aba0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
3abb0 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 int,void(*)(void
3abc0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 *));.SQLITE_API
3abd0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
3abe0 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 6c ult_text16be(sql
3abf0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 ite3_context*, c
3ac00 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c onst void*, int,
3ac10 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
3ac20 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
3ac30 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
3ac40 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f value(sqlite3_co
3ac50 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f ntext*, sqlite3_
3ac60 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
3ac70 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
3ac80 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 _result_zeroblob
3ac90 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
3aca0 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a *, int n);../*.*
3acb0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 69 * CAPI3REF: Defi
3acc0 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e 67 ne New Collating
3acd0 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36 36 Sequences {H166
3ace0 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a 00} <S20300>.**.
3acf0 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f ** These functio
3ad00 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 ns are used to a
3ad10 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e dd new collation
3ad20 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 68 sequences to th
3ad30 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 e.** [database c
3ad40 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 onnection] speci
3ad50 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72 73 fied as the firs
3ad60 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a t argument..**.*
3ad70 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
3ad80 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 e new collation
3ad90 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65 63 sequence is spec
3ada0 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d 38 ified as a UTF-8
3adb0 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73 string.** for s
3adc0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
3add0 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 71 llation() and sq
3ade0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
3adf0 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61 lation_v2().** a
3ae00 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 nd a UTF-16 stri
3ae10 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 ng for sqlite3_c
3ae20 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 reate_collation1
3ae30 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73 65 6(). In all case
3ae40 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 73 s.** the name is
3ae50 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
3ae60 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 econd function a
3ae70 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 rgument..**.** T
3ae80 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
3ae90 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 t may be one of
3aea0 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b 53 the constants [S
3aeb0 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20 QLITE_UTF8],.**
3aec0 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d [SQLITE_UTF16LE]
3aed0 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 or [SQLITE_UTF1
3aee0 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e 67 6BE], indicating
3aef0 20 74 68 61 74 20 74 68 65 20 75 73 65 72 2d 73 that the user-s
3af00 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74 69 upplied.** routi
3af10 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 62 65 ne expects to be
3af20 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72 73 passed pointers
3af30 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63 6f to strings enco
3af40 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38 2c ded using UTF-8,
3af50 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c .** UTF-16 littl
3af60 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 e-endian, or UTF
3af70 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 2c 20 -16 big-endian,
3af80 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68 respectively. Th
3af90 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75 6d e.** third argum
3afa0 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 ent might also b
3afb0 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 5f e [SQLITE_UTF16_
3afc0 41 4c 49 47 4e 45 44 5d 20 74 6f 20 69 6e 64 69 ALIGNED] to indi
3afd0 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 cate that.** the
3afe0 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73 routine expects
3aff0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36 2d pointers to 16-
3b000 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65 64 bit word aligned
3b010 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20 55 strings.** of U
3b020 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 TF-16 in the nat
3b030 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 6f ive byte order o
3b040 66 20 74 68 65 20 68 6f 73 74 20 63 6f 6d 70 75 f the host compu
3b050 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 ter..**.** A poi
3b060 6e 74 65 72 20 74 6f 20 74 68 65 20 75 73 65 72 nter to the user
3b070 20 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e supplied routin
3b080 65 20 6d 75 73 74 20 62 65 20 70 61 73 73 65 64 e must be passed
3b090 20 61 73 20 74 68 65 20 66 69 66 74 68 0a 2a 2a as the fifth.**
3b0a0 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 69 argument. If i
3b0b0 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 t is NULL, this
3b0c0 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 64 is the same as d
3b0d0 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c eleting the coll
3b0e0 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 ation.** sequenc
3b0f0 65 20 28 73 6f 20 74 68 61 74 20 53 51 4c 69 74 e (so that SQLit
3b100 65 20 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 e cannot call it
3b110 20 61 6e 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 anymore)..** Ea
3b120 63 68 20 74 69 6d 65 20 74 68 65 20 61 70 70 6c ch time the appl
3b130 69 63 61 74 69 6f 6e 20 73 75 70 70 6c 69 65 64 ication supplied
3b140 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 function is inv
3b150 6f 6b 65 64 2c 20 69 74 20 69 73 20 70 61 73 73 oked, it is pass
3b160 65 64 0a 2a 2a 20 61 73 20 69 74 73 20 66 69 72 ed.** as its fir
3b170 73 74 20 70 61 72 61 6d 65 74 65 72 20 61 20 63 st parameter a c
3b180 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a opy of the void*
3b190 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 passed as the f
3b1a0 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a ourth argument.*
3b1b0 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 * to sqlite3_cre
3b1c0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 ate_collation()
3b1d0 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 or sqlite3_creat
3b1e0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e e_collation16().
3b1f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 .**.** The remai
3b200 6e 69 6e 67 20 61 72 67 75 6d 65 6e 74 73 20 74 ning arguments t
3b210 6f 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f o the applicatio
3b220 6e 2d 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 n-supplied routi
3b230 6e 65 20 61 72 65 20 74 77 6f 20 73 74 72 69 6e ne are two strin
3b240 67 73 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70 72 gs,.** each repr
3b250 65 73 65 6e 74 65 64 20 62 79 20 61 20 28 6c 65 esented by a (le
3b260 6e 67 74 68 2c 20 64 61 74 61 29 20 70 61 69 72 ngth, data) pair
3b270 20 61 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e 20 and encoded in
3b280 74 68 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 the encoding.**
3b290 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20 that was passed
3b2a0 61 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67 as the third arg
3b2b0 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 63 ument when the c
3b2c0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
3b2d0 65 20 77 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 e was.** registe
3b2e0 72 65 64 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 red. {END} The
3b2f0 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 application defi
3b300 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f ned collation ro
3b310 75 74 69 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 utine should.**
3b320 72 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c return negative,
3b330 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 zero or positiv
3b340 65 20 69 66 20 74 68 65 20 66 69 72 73 74 20 73 e if the first s
3b350 74 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68 tring is less th
3b360 61 6e 2c 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c an,.** equal to,
3b370 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e or greater than
3b380 20 74 68 65 20 73 65 63 6f 6e 64 20 73 74 72 69 the second stri
3b390 6e 67 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 ng. i.e. (STRING
3b3a0 31 20 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 1 - STRING2)..**
3b3b0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
3b3c0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
3b3d0 5f 76 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 _v2() works like
3b3e0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
3b3f0 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 collation().** e
3b400 78 63 65 70 74 20 74 68 61 74 20 69 74 20 74 61 xcept that it ta
3b410 6b 65 73 20 61 6e 20 65 78 74 72 61 20 61 72 67 kes an extra arg
3b420 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 ument which is a
3b430 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 0a destructor for.
3b440 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e ** the collation
3b450 2e 20 20 54 68 65 20 64 65 73 74 72 75 63 74 6f . The destructo
3b460 72 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e r is called when
3b470 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 the collation i
3b480 73 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64 20 61 s.** destroyed a
3b490 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 20 63 nd is passed a c
3b4a0 6f 70 79 20 6f 66 20 74 68 65 20 66 6f 75 72 74 opy of the fourt
3b4b0 68 20 70 61 72 61 6d 65 74 65 72 20 76 6f 69 64 h parameter void
3b4c0 2a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 * pointer.** of
3b4d0 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 the sqlite3_crea
3b4e0 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 te_collation_v2(
3b4f0 29 2e 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 )..** Collations
3b500 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 77 are destroyed w
3b510 68 65 6e 20 74 68 65 79 20 61 72 65 20 6f 76 65 hen they are ove
3b520 72 72 69 64 64 65 6e 20 62 79 20 6c 61 74 65 72 rridden by later
3b530 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a calls to the.**
3b540 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 collation creat
3b550 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 ion functions or
3b560 20 77 68 65 6e 20 74 68 65 20 5b 64 61 74 61 62 when the [datab
3b570 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
3b580 69 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 is closed.** usi
3b590 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 ng [sqlite3_clos
3b5a0 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 e()]..**.** INVA
3b5b0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
3b5c0 31 36 36 30 33 7d 20 41 20 73 75 63 63 65 73 73 16603} A success
3b5d0 66 75 6c 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a ful call to the.
3b5e0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
3b5f0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
3b600 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c 50 ation_v2(B,X,E,P
3b610 2c 46 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 ,F,D)] interface
3b620 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 67 .** reg
3b630 69 73 74 65 72 73 20 66 75 6e 63 74 69 6f 6e 20 isters function
3b640 46 20 61 73 20 74 68 65 20 63 6f 6d 70 61 72 69 F as the compari
3b650 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 son function use
3b660 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 d to.**
3b670 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f 6c 6c 61 implement colla
3b680 74 69 6f 6e 20 58 20 6f 6e 20 74 68 65 20 5b 64 tion X on the [d
3b690 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3b6a0 6f 6e 5d 20 42 20 66 6f 72 0a 2a 2a 20 20 20 20 on] B for.**
3b6b0 20 20 20 20 20 20 64 61 74 61 62 61 73 65 73 20 databases
3b6c0 68 61 76 69 6e 67 20 65 6e 63 6f 64 69 6e 67 20 having encoding
3b6d0 45 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 30 34 E..**.** {H16604
3b6e0 7d 20 53 51 4c 69 74 65 20 75 6e 64 65 72 73 74 } SQLite underst
3b6f0 61 6e 64 73 20 74 68 65 20 58 20 70 61 72 61 6d ands the X param
3b700 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 eter to.**
3b710 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 [sqlite3_cre
3b720 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 ate_collation_v2
3b730 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29 5d 20 61 (B,X,E,P,F,D)] a
3b740 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 s a zero-termina
3b750 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ted.**
3b760 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 6e 20 UTF-8 string in
3b770 77 68 69 63 68 20 63 61 73 65 20 69 73 20 69 67 which case is ig
3b780 6e 6f 72 65 64 20 66 6f 72 20 41 53 43 49 49 20 nored for ASCII
3b790 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a characters and.*
3b7a0 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 73 69 * is si
3b7b0 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 20 6e 6f gnificant for no
3b7c0 6e 2d 41 53 43 49 49 20 63 68 61 72 61 63 74 65 n-ASCII characte
3b7d0 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 30 rs..**.** {H1660
3b7e0 36 7d 20 53 75 63 63 65 73 73 69 76 65 20 63 61 6} Successive ca
3b7f0 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
3b800 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
3b810 5f 76 32 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29 _v2(B,X,E,P,F,D)
3b820 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 ].** wi
3b830 74 68 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 th the same valu
3b840 65 73 20 66 6f 72 20 42 2c 20 58 2c 20 61 6e 64 es for B, X, and
3b850 20 45 2c 20 6f 76 65 72 72 69 64 65 20 70 72 69 E, override pri
3b860 6f 72 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 or values.**
3b870 20 20 20 20 20 20 6f 66 20 50 2c 20 46 2c 20 61 of P, F, a
3b880 6e 64 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 nd D..**.** {H16
3b890 36 30 39 7d 20 49 66 20 74 68 65 20 64 65 73 74 609} If the dest
3b8a0 72 75 63 74 6f 72 20 44 20 69 6e 20 5b 73 71 6c ructor D in [sql
3b8b0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
3b8c0 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c 50 ation_v2(B,X,E,P
3b8d0 2c 46 2c 44 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ,F,D)].**
3b8e0 20 20 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 is not NULL t
3b8f0 68 65 6e 20 69 74 20 69 73 20 63 61 6c 6c 65 64 hen it is called
3b900 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 50 with argument P
3b910 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 when the.**
3b920 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67 20 collating
3b930 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f 70 function is drop
3b940 70 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a ped by SQLite..*
3b950 2a 0a 2a 2a 20 7b 48 31 36 36 31 32 7d 20 41 20 *.** {H16612} A
3b960 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 collating functi
3b970 6f 6e 20 69 73 20 64 72 6f 70 70 65 64 20 77 68 on is dropped wh
3b980 65 6e 20 69 74 20 69 73 20 6f 76 65 72 6c 6f 61 en it is overloa
3b990 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 ded..**.** {H166
3b9a0 31 35 7d 20 41 20 63 6f 6c 6c 61 74 69 6e 67 20 15} A collating
3b9b0 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f 70 function is drop
3b9c0 70 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 ped when the dat
3b9d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
3b9e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 .** is
3b9f0 63 6c 6f 73 65 64 20 75 73 69 6e 67 20 5b 73 71 closed using [sq
3ba00 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a lite3_close()]..
3ba10 2a 2a 0a 2a 2a 20 7b 48 31 36 36 31 38 7d 20 54 **.** {H16618} T
3ba20 68 65 20 70 6f 69 6e 74 65 72 20 50 20 69 6e 20 he pointer P in
3ba30 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
3ba40 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c 58 collation_v2(B,X
3ba50 2c 45 2c 50 2c 46 2c 44 29 5d 0a 2a 2a 20 20 20 ,E,P,F,D)].**
3ba60 20 20 20 20 20 20 20 69 73 20 70 61 73 73 65 64 is passed
3ba70 20 74 68 72 6f 75 67 68 20 61 73 20 74 68 65 20 through as the
3ba80 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
3ba90 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f to the compariso
3baa0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 n.** fu
3bab0 6e 63 74 69 6f 6e 20 46 20 66 6f 72 20 61 6c 6c nction F for all
3bac0 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f subsequent invo
3bad0 63 61 74 69 6f 6e 73 20 6f 66 20 46 2e 0a 2a 2a cations of F..**
3bae0 0a 2a 2a 20 7b 48 31 36 36 32 31 7d 20 41 20 63 .** {H16621} A c
3baf0 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
3bb00 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
3bb10 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 69 73 20 (B,X,E,P,F)] is
3bb20 65 78 61 63 74 6c 79 0a 2a 2a 20 20 20 20 20 20 exactly.**
3bb30 20 20 20 20 74 68 65 20 73 61 6d 65 20 61 73 20 the same as
3bb40 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 a call to [sqlit
3bb50 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
3bb60 69 6f 6e 5f 76 32 28 29 5d 20 77 69 74 68 0a 2a ion_v2()] with.*
3bb70 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 * the s
3bb80 61 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 ame parameters a
3bb90 6e 64 20 61 20 4e 55 4c 4c 20 64 65 73 74 72 75 nd a NULL destru
3bba0 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 ctor..**.** {H16
3bbb0 36 32 34 7d 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 624} Following a
3bbc0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
3bbd0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c _collation_v2(B,
3bbe0 58 2c 45 2c 50 2c 46 2c 44 29 5d 2c 0a 2a 2a 20 X,E,P,F,D)],.**
3bbf0 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 SQLite
3bc00 75 73 65 73 20 74 68 65 20 63 6f 6d 70 61 72 69 uses the compari
3bc10 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 46 20 66 son function F f
3bc20 6f 72 20 61 6c 6c 20 74 65 78 74 20 63 6f 6d 70 or all text comp
3bc30 61 72 69 73 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 arison.**
3bc40 20 20 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e operations on
3bc50 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
3bc60 6f 6e 6e 65 63 74 69 6f 6e 5d 20 42 20 6f 6e 20 onnection] B on
3bc70 74 65 78 74 20 76 61 6c 75 65 73 20 74 68 61 74 text values that
3bc80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 73 65 .** use
3bc90 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 the collating s
3bca0 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 58 2e equence named X.
3bcb0 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 32 37 7d 20 .**.** {H16627}
3bcc0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 The [sqlite3_cre
3bcd0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 ate_collation16(
3bce0 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 77 6f 72 6b B,X,E,P,F)] work
3bcf0 73 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 s the same.**
3bd00 20 20 20 20 20 20 20 61 73 20 5b 73 71 6c 69 74 as [sqlit
3bd10 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
3bd20 69 6f 6e 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 ion(B,X,E,P,F)]
3bd30 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 0a except that the.
3bd40 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6c 6c ** coll
3bd50 61 74 69 6f 6e 20 6e 61 6d 65 20 58 20 69 73 20 ation name X is
3bd60 75 6e 64 65 72 73 74 6f 6f 64 20 61 73 20 55 54 understood as UT
3bd70 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 20 62 F-16 in native b
3bd80 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20 yte order.**
3bd90 20 20 20 20 20 20 69 6e 73 74 65 61 64 20 6f 66 instead of
3bda0 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 UTF-8..**.** {H
3bdb0 31 36 36 33 30 7d 20 57 68 65 6e 20 6d 75 6c 74 16630} When mult
3bdc0 69 70 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 iple comparison
3bdd0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 61 76 functions are av
3bde0 61 69 6c 61 62 6c 65 20 66 6f 72 20 74 68 65 20 ailable for the
3bdf0 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 same.**
3be00 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
3be10 6e 63 65 2c 20 53 51 4c 69 74 65 20 63 68 6f 6f nce, SQLite choo
3be20 73 65 73 20 74 68 65 20 6f 6e 65 20 77 68 6f 73 ses the one whos
3be30 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a e text encoding.
3be40 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 71 75 ** requ
3be50 69 72 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 ires the least a
3be60 6d 6f 75 6e 74 20 6f 66 20 63 6f 6e 76 65 72 73 mount of convers
3be70 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 64 65 66 ion from the def
3be80 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 ault.**
3be90 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 6f text encoding o
3bea0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a f the database..
3beb0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
3bec0 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 t sqlite3_create
3bed0 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 _collation(. sq
3bee0 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 lite3*, . const
3bef0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 char *zName, .
3bf00 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a int eTextRep, .
3bf10 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a void*,. int(*
3bf20 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c xCompare)(void*,
3bf30 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
3bf40 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 int,const void*)
3bf50 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
3bf60 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
3bf70 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a e_collation_v2(.
3bf80 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 sqlite3*, . c
3bf90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
3bfa0 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 , . int eTextRe
3bfb0 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 p, . void*,. i
3bfc0 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f nt(*xCompare)(vo
3bfd0 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
3bfe0 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
3bff0 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 id*),. void(*xD
3c000 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a 29 estroy)(void*).)
3c010 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
3c020 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
3c030 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 collation16(. s
3c040 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 qlite3*, . cons
3c050 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20 t void *zName,.
3c060 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a int eTextRep, .
3c070 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a void*,. int(*
3c080 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c xCompare)(void*,
3c090 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
3c0a0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 int,const void*)
3c0b0 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
3c0c0 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e REF: Collation N
3c0d0 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 20 eeded Callbacks
3c0e0 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30 30 {H16700} <S20300
3c0f0 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 >.**.** To avoid
3c100 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73 having to regis
3c110 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f ter all collatio
3c120 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f n sequences befo
3c130 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a re a database.**
3c140 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20 can be used, a
3c150 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20 single callback
3c160 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 function may be
3c170 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 registered with
3c180 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 the.** [database
3c190 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 connection] to
3c1a0 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76 be called whenev
3c1b0 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20 er an undefined
3c1c0 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 collation.** seq
3c1d0 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72 65 uence is require
3c1e0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
3c1f0 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69 function is regi
3c200 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 stered using the
3c210 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 sqlite3_collati
3c220 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c on_needed() API,
3c230 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70 .** then it is p
3c240 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20 assed the names
3c250 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c of undefined col
3c260 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 lation sequences
3c270 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65 as strings.** e
3c280 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e ncoded in UTF-8.
3c290 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71 6c {H16703} If sql
3c2a0 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e ite3_collation_n
3c2b0 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65 eeded16() is use
3c2c0 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 d,.** the names
3c2d0 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55 54 are passed as UT
3c2e0 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20 F-16 in machine
3c2f0 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 native byte orde
3c300 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 r..** A call to
3c310 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 either function
3c320 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69 replaces any exi
3c330 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a sting callback..
3c340 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 **.** When the c
3c350 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
3c360 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61 72 ed, the first ar
3c370 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73 gument passed is
3c380 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68 a copy.** of th
3c390 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
3c3a0 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c t to sqlite3_col
3c3b0 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 lation_needed()
3c3c0 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f or.** sqlite3_co
3c3d0 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 llation_needed16
3c3e0 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 (). The second
3c3f0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
3c400 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e database.** conn
3c410 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 69 ection. The thi
3c420 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f rd argument is o
3c430 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54 ne of [SQLITE_UT
3c440 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46 F8], [SQLITE_UTF
3c450 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 16BE],.** or [SQ
3c460 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69 LITE_UTF16LE], i
3c470 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f ndicating the mo
3c480 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72 st desirable for
3c490 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 m of the collati
3c4a0 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66 on.** sequence f
3c4b0 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64 unction required
3c4c0 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 . The fourth pa
3c4d0 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e rameter is the n
3c4e0 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 ame of the.** re
3c4f0 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e quired collation
3c500 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a sequence..**.**
3c510 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 The callback fu
3c520 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 nction should re
3c530 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69 72 gister the desir
3c540 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69 ed collation usi
3c550 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 ng.** [sqlite3_c
3c560 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 reate_collation(
3c570 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 )], [sqlite3_cre
3c580 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 ate_collation16(
3c590 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 )], or.** [sqlit
3c5a0 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
3c5b0 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a ion_v2()]..**.**
3c5c0 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
3c5d0 2a 2a 20 7b 48 31 36 37 30 32 7d 20 41 20 73 75 ** {H16702} A su
3c5e0 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f ccessful call to
3c5f0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 [sqlite3_collat
3c600 69 6f 6e 5f 6e 65 65 64 65 64 28 44 2c 50 2c 46 ion_needed(D,P,F
3c610 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f )].** o
3c620 72 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 r [sqlite3_colla
3c630 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 44 2c tion_needed16(D,
3c640 50 2c 46 29 5d 20 63 61 75 73 65 73 0a 2a 2a 20 P,F)] causes.**
3c650 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 64 61 the [da
3c660 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3c670 6e 5d 20 44 20 74 6f 20 69 6e 76 6f 6b 65 20 63 n] D to invoke c
3c680 61 6c 6c 62 61 63 6b 20 46 20 77 69 74 68 20 66 allback F with f
3c690 69 72 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 irst.**
3c6a0 20 70 61 72 61 6d 65 74 65 72 20 50 20 77 68 65 parameter P whe
3c6b0 6e 65 76 65 72 20 69 74 20 6e 65 65 64 73 20 61 never it needs a
3c6c0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 comparison func
3c6d0 74 69 6f 6e 20 66 6f 72 20 61 0a 2a 2a 20 20 20 tion for a.**
3c6e0 20 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67 collating
3c6f0 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20 69 sequence that i
3c700 74 20 64 6f 65 73 20 6e 6f 74 20 6b 6e 6f 77 20 t does not know
3c710 61 62 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 about..**.** {H1
3c720 36 37 30 34 7d 20 45 61 63 68 20 73 75 63 63 65 6704} Each succe
3c730 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 ssful call to [s
3c740 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
3c750 5f 6e 65 65 64 65 64 28 29 5d 20 6f 72 0a 2a 2a _needed()] or.**
3c760 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
3c770 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 e3_collation_nee
3c780 64 65 64 31 36 28 29 5d 20 6f 76 65 72 72 69 64 ded16()] overrid
3c790 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 es the callback
3c7a0 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 registered.**
3c7b0 20 20 20 20 20 20 20 6f 6e 20 74 68 65 20 73 61 on the sa
3c7c0 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e me [database con
3c7d0 6e 65 63 74 69 6f 6e 5d 20 62 79 20 70 72 69 6f nection] by prio
3c7e0 72 20 63 61 6c 6c 73 20 74 6f 20 65 69 74 68 65 r calls to eithe
3c7f0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e r.** in
3c800 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 7b terface..**.** {
3c810 48 31 36 37 30 36 7d 20 54 68 65 20 6e 61 6d 65 H16706} The name
3c820 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 of the requeste
3c830 64 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 d collating func
3c840 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 20 74 tion passed in t
3c850 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 34 he.** 4
3c860 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 th parameter to
3c870 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 the callback is
3c880 69 6e 20 55 54 46 2d 38 20 69 66 20 74 68 65 20 in UTF-8 if the
3c890 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 callback.**
3c8a0 20 20 20 20 20 77 61 73 20 72 65 67 69 73 74 65 was registe
3c8b0 72 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 red using [sqlit
3c8c0 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 e3_collation_nee
3c8d0 64 65 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 20 20 ded()] and.**
3c8e0 20 20 20 20 20 20 20 69 73 20 69 6e 20 55 54 46 is in UTF
3c8f0 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 -16 native byte
3c900 6f 72 64 65 72 20 69 66 20 74 68 65 20 63 61 6c order if the cal
3c910 6c 62 61 63 6b 20 77 61 73 0a 2a 2a 20 20 20 20 lback was.**
3c920 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64 registered
3c930 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
3c940 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
3c950 31 36 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 16()]..*/.SQLITE
3c960 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
3c970 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 _collation_neede
3c980 64 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a d(. sqlite3*, .
3c990 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64 void*, . void
3c9a0 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 (*)(void*,sqlite
3c9b0 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 3*,int eTextRep,
3c9c0 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a const char*).);.
3c9d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
3c9e0 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
3c9f0 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c _needed16(. sql
3ca00 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c ite3*, . void*,
3ca10 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a . void(*)(void*
3ca20 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 ,sqlite3*,int eT
3ca30 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 extRep,const voi
3ca40 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 70 d*).);../*.** Sp
3ca50 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66 6f ecify the key fo
3ca60 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 r an encrypted d
3ca70 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72 atabase. This r
3ca80 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 outine should be
3ca90 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 74 .** called right
3caa0 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 6f after sqlite3_o
3cab0 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 pen()..**.** The
3cac0 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 code to impleme
3cad0 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e nt this API is n
3cae0 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 ot available in
3caf0 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 the public relea
3cb00 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e se.** of SQLite.
3cb10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
3cb20 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 0a nt sqlite3_key(.
3cb30 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
3cb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3cb50 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 /* Database to
3cb60 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 be rekeyed */.
3cb70 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 const void *pKey
3cb80 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f , int nKey /
3cb90 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b 0a * The key */.);.
3cba0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
3cbb0 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 6e e key on an open
3cbc0 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 74 database. If t
3cbd0 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62 he current datab
3cbe0 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 6e ase is not.** en
3cbf0 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72 6f crypted, this ro
3cc00 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 79 utine will encry
3cc10 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77 3d pt it. If pNew=
3cc20 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 74 =0 or nNew==0, t
3cc30 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 he.** database i
3cc40 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a 0a s decrypted..**.
3cc50 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 ** The code to i
3cc60 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 mplement this AP
3cc70 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 I is not availab
3cc80 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 le in the public
3cc90 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 release.** of S
3cca0 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 QLite..*/.SQLITE
3ccb0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
3ccc0 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 _rekey(. sqlite
3ccd0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
3cce0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
3ccf0 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 base to be rekey
3cd00 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f ed */. const vo
3cd10 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b id *pKey, int nK
3cd20 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 ey /* The ne
3cd30 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a w key */.);../*.
3cd40 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 75 73 ** CAPI3REF: Sus
3cd50 70 65 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 46 pend Execution F
3cd60 6f 72 20 41 20 53 68 6f 72 74 20 54 69 6d 65 20 or A Short Time
3cd70 7b 48 31 30 35 33 30 7d 20 3c 53 34 30 34 31 30 {H10530} <S40410
3cd80 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
3cd90 74 65 33 5f 73 6c 65 65 70 28 29 20 66 75 6e 63 te3_sleep() func
3cda0 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 tion causes the
3cdb0 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 74 current thread t
3cdc0 6f 20 73 75 73 70 65 6e 64 20 65 78 65 63 75 74 o suspend execut
3cdd0 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c 65 ion.** for at le
3cde0 61 73 74 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 ast a number of
3cdf0 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70 65 milliseconds spe
3ce00 63 69 66 69 65 64 20 69 6e 20 69 74 73 20 70 61 cified in its pa
3ce10 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 rameter..**.** I
3ce20 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 f the operating
3ce30 73 79 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20 system does not
3ce40 73 75 70 70 6f 72 74 20 73 6c 65 65 70 20 72 65 support sleep re
3ce50 71 75 65 73 74 73 20 77 69 74 68 0a 2a 2a 20 6d quests with.** m
3ce60 69 6c 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65 20 illisecond time
3ce70 72 65 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65 6e resolution, then
3ce80 20 74 68 65 20 74 69 6d 65 20 77 69 6c 6c 20 62 the time will b
3ce90 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 0a e rounded up to.
3cea0 2a 2a 20 74 68 65 20 6e 65 61 72 65 73 74 20 73 ** the nearest s
3ceb0 65 63 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62 65 econd. The numbe
3cec0 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 r of millisecond
3ced0 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61 s of sleep actua
3cee0 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 lly.** requested
3cef0 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 72 61 74 from the operat
3cf00 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 72 65 ing system is re
3cf10 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 turned..**.** SQ
3cf20 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 Lite implements
3cf30 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 62 this interface b
3cf40 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 53 y calling the xS
3cf50 6c 65 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 leep().** method
3cf60 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 of the default
3cf70 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 [sqlite3_vfs] ob
3cf80 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 ject..**.** INVA
3cf90 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
3cfa0 31 30 35 33 33 7d 20 54 68 65 20 5b 73 71 6c 69 10533} The [sqli
3cfb0 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69 6e te3_sleep(M)] in
3cfc0 74 65 72 66 61 63 65 20 69 6e 76 6f 6b 65 73 20 terface invokes
3cfd0 74 68 65 20 78 53 6c 65 65 70 0a 2a 2a 20 20 20 the xSleep.**
3cfe0 20 20 20 20 20 20 20 6d 65 74 68 6f 64 20 6f 66 method of
3cff0 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 the default [sq
3d000 6c 69 74 65 33 5f 76 66 73 7c 56 46 53 5d 20 69 lite3_vfs|VFS] i
3d010 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 20 20 n order to.**
3d020 20 20 20 20 20 20 20 73 75 73 70 65 6e 64 20 65 suspend e
3d030 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 xecution of the
3d040 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 66 current thread f
3d050 6f 72 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 20 or at least.**
3d060 20 20 20 20 20 20 20 20 4d 20 6d 69 6c 6c 69 73 M millis
3d070 65 63 6f 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 econds..**.** {H
3d080 31 30 35 33 36 7d 20 54 68 65 20 5b 73 71 6c 69 10536} The [sqli
3d090 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69 6e te3_sleep(M)] in
3d0a0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
3d0b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
3d0c0 20 20 20 20 20 20 20 20 20 20 6d 69 6c 6c 69 73 millis
3d0d0 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 econds of sleep
3d0e0 61 63 74 75 61 6c 6c 79 20 72 65 71 75 65 73 74 actually request
3d0f0 65 64 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 ed of the operat
3d100 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ing.**
3d110 73 79 73 74 65 6d 2c 20 77 68 69 63 68 20 6d 69 system, which mi
3d120 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 ght be larger th
3d130 61 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 an the parameter
3d140 20 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 M..*/.SQLITE_AP
3d150 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c I int sqlite3_sl
3d160 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a eep(int);../*.**
3d170 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 CAPI3REF: Name
3d180 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f Of The Folder Ho
3d190 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20 lding Temporary
3d1a0 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c Files {H10310} <
3d1b0 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 S20000>.**.** If
3d1c0 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 this global var
3d1d0 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f iable is made to
3d1e0 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 point to a stri
3d1f0 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 ng which is.** t
3d200 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c he name of a fol
3d210 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 der (a.k.a. dire
3d220 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c ctory), then all
3d230 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
3d240 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 53 .** created by S
3d250 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c QLite will be pl
3d260 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 72 aced in that dir
3d270 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73 ectory. If this
3d280 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 variable.** is
3d290 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 a NULL pointer,
3d2a0 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 then SQLite perf
3d2b0 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f orms a search fo
3d2c0 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 r an appropriate
3d2d0 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 .** temporary fi
3d2e0 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a le directory..**
3d2f0 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 .** It is not sa
3d300 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 69 fe to modify thi
3d310 73 20 76 61 72 69 61 62 6c 65 20 6f 6e 63 65 20 s variable once
3d320 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a [database conn
3d330 65 63 74 69 6f 6e 5d 0a 2a 2a 20 68 61 73 20 62 ection].** has b
3d340 65 65 6e 20 6f 70 65 6e 65 64 2e 20 20 49 74 20 een opened. It
3d350 69 73 20 69 6e 74 65 6e 64 65 64 20 74 68 61 74 is intended that
3d360 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 62 this variable b
3d370 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a 20 61 73 e set once.** as
3d380 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73 73 part of process
3d390 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
3d3a0 61 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20 53 and before any S
3d3b0 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 0a QLite interface.
3d3c0 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 ** routines have
3d3d0 20 62 65 65 6e 20 63 61 6c 6c 20 61 6e 64 20 72 been call and r
3d3e0 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 emain unchanged
3d3f0 74 68 65 72 65 61 66 74 65 72 2e 0a 2a 2f 0a 53 thereafter..*/.S
3d400 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
3d410 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 sqlite3_temp_dir
3d420 65 63 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ectory;../*.** C
3d430 41 50 49 33 52 45 46 3a 20 54 65 73 74 20 46 6f API3REF: Test Fo
3d440 72 20 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f r Auto-Commit Mo
3d450 64 65 20 7b 48 31 32 39 33 30 7d 20 3c 53 36 30 de {H12930} <S60
3d460 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 200>.** KEYWORDS
3d470 3a 20 7b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f : {autocommit mo
3d480 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 de}.**.** The sq
3d490 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f lite3_get_autoco
3d4a0 6d 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 mmit() interface
3d4b0 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
3d4c0 6f 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 o or.** zero if
3d4d0 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61 the given databa
3d4e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 se connection is
3d4f0 20 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 or is not in au
3d500 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a tocommit mode,.*
3d510 2a 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 * respectively.
3d520 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 Autocommit mode
3d530 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c is on by defaul
3d540 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 t..** Autocommit
3d550 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c 65 mode is disable
3d560 64 20 62 79 20 61 20 5b 42 45 47 49 4e 5d 20 73 d by a [BEGIN] s
3d570 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74 tatement..** Aut
3d580 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 ocommit mode is
3d590 72 65 2d 65 6e 61 62 6c 65 64 20 62 79 20 61 20 re-enabled by a
3d5a0 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c [COMMIT] or [ROL
3d5b0 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 LBACK]..**.** If
3d5c0 20 63 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f certain kinds o
3d5d0 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 20 6f f errors occur o
3d5e0 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 n a statement wi
3d5f0 74 68 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61 thin a multi-sta
3d600 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 tement.** transa
3d610 63 74 69 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e ction (errors in
3d620 63 6c 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f cluding [SQLITE_
3d630 46 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 FULL], [SQLITE_I
3d640 4f 45 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 OERR],.** [SQLIT
3d650 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 E_NOMEM], [SQLIT
3d660 45 5f 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51 E_BUSY], and [SQ
3d670 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29 LITE_INTERRUPT])
3d680 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61 then the.** tra
3d690 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62 nsaction might b
3d6a0 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 e rolled back au
3d6b0 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 tomatically. Th
3d6c0 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a e only way to.**
3d6d0 20 66 69 6e 64 20 6f 75 74 20 77 68 65 74 68 65 find out whethe
3d6e0 72 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 r SQLite automat
3d6f0 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 ically rolled ba
3d700 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ck the transacti
3d710 6f 6e 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65 on after.** an e
3d720 72 72 6f 72 20 69 73 20 74 6f 20 75 73 65 20 74 rror is to use t
3d730 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a his function..**
3d740 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
3d750 2a 2a 0a 2a 2a 20 7b 48 31 32 39 33 31 7d 20 54 **.** {H12931} T
3d760 68 65 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f he [sqlite3_get_
3d770 61 75 74 6f 63 6f 6d 6d 69 74 28 44 29 5d 20 69 autocommit(D)] i
3d780 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
3d790 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 non-zero or.**
3d7a0 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20 69 66 zero if
3d7b0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
3d7c0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 69 73 20 onnection] D is
3d7d0 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 74 or is not in aut
3d7e0 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 20 20 20 20 20 ocommit.**
3d7f0 20 20 20 20 6d 6f 64 65 2c 20 72 65 73 70 65 63 mode, respec
3d800 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 tively..**.** {H
3d810 31 32 39 33 32 7d 20 41 75 74 6f 63 6f 6d 6d 69 12932} Autocommi
3d820 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20 t mode is on by
3d830 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b default..**.** {
3d840 48 31 32 39 33 33 7d 20 41 75 74 6f 63 6f 6d 6d H12933} Autocomm
3d850 69 74 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62 it mode is disab
3d860 6c 65 64 20 62 79 20 61 20 73 75 63 63 65 73 73 led by a success
3d870 66 75 6c 20 5b 42 45 47 49 4e 5d 20 73 74 61 74 ful [BEGIN] stat
3d880 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ement..**.** {H1
3d890 32 39 33 34 7d 20 41 75 74 6f 63 6f 6d 6d 69 74 2934} Autocommit
3d8a0 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c 65 64 mode is enabled
3d8b0 20 62 79 20 61 20 73 75 63 63 65 73 73 66 75 6c by a successful
3d8c0 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f [COMMIT] or [RO
3d8d0 4c 4c 42 41 43 4b 5d 0a 2a 2a 20 20 20 20 20 20 LLBACK].**
3d8e0 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a statement..*
3d8f0 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 *.** ASSUMPTIONS
3d900 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 39 33 36 7d :.**.** {A12936}
3d910 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 If another thre
3d920 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 61 ad changes the a
3d930 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 73 utocommit status
3d940 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
3d950 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e .** con
3d960 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 nection while th
3d970 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 is routine is ru
3d980 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 nning, then the
3d990 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
3d9a0 20 20 20 20 20 20 20 20 20 69 73 20 75 6e 64 65 is unde
3d9b0 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fined..*/.SQLITE
3d9c0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
3d9d0 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 _get_autocommit(
3d9e0 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a sqlite3*);../*.*
3d9f0 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 * CAPI3REF: Find
3da00 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61 The Database Ha
3da10 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 ndle Of A Prepar
3da20 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 ed Statement {H1
3da30 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 3120} <S60600>.*
3da40 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
3da50 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 _db_handle inter
3da60 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 face returns the
3da70 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
3da80 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a ction] handle.**
3da90 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 to which a [pre
3daa0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
3dab0 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 64 belongs. The d
3dac0 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 72 atabase handle r
3dad0 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 eturned by.** sq
3dae0 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20 lite3_db_handle
3daf0 69 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 is the same data
3db00 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74 base handle that
3db10 20 77 61 73 20 74 68 65 20 66 69 72 73 74 20 61 was the first a
3db20 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 rgument.** to th
3db30 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 e [sqlite3_prepa
3db40 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f re_v2()] call (o
3db50 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 r its variants)
3db60 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f that was used to
3db70 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65 20 73 .** create the s
3db80 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 tatement in the
3db90 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a first place..**.
3dba0 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a ** INVARIANTS:.*
3dbb0 2a 0a 2a 2a 20 7b 48 31 33 31 32 33 7d 20 54 68 *.** {H13123} Th
3dbc0 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 e [sqlite3_db_ha
3dbd0 6e 64 6c 65 28 53 29 5d 20 69 6e 74 65 72 66 61 ndle(S)] interfa
3dbe0 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ce returns a poi
3dbf0 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 nter.**
3dc00 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 to the [databas
3dc10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 73 e connection] as
3dc20 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
3dc30 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 e.** [p
3dc40 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
3dc50 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f t] S..*/.SQLITE_
3dc60 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c API sqlite3 *sql
3dc70 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 ite3_db_handle(s
3dc80 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a qlite3_stmt*);..
3dc90 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3dca0 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72 Find the next pr
3dcb0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
3dcc0 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30 {H13140} <S6060
3dcd0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 0>.**.** This in
3dce0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
3dcf0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
3dd00 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 20 next [prepared
3dd10 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72 statement] after
3dd20 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69 .** pStmt associ
3dd30 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 ated with the [d
3dd40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3dd50 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74 on] pDb. If pSt
3dd60 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 mt is NULL.** th
3dd70 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 en this interfac
3dd80 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e e returns a poin
3dd90 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 ter to the first
3dda0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
3ddb0 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 ent.** associate
3ddc0 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 d with the datab
3ddd0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 ase connection p
3dde0 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61 Db. If no prepa
3ddf0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a red statement.**
3de00 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 63 satisfies the c
3de10 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69 onditions of thi
3de20 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65 s routine, it re
3de30 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a turns NULL..**.*
3de40 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
3de50 0a 2a 2a 20 7b 48 31 33 31 34 33 7d 20 49 66 20 .** {H13143} If
3de60 44 20 69 73 20 61 20 5b 64 61 74 61 62 61 73 65 D is a [database
3de70 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61 connection] tha
3de80 74 20 68 6f 6c 64 73 20 6f 6e 65 20 6f 72 20 6d t holds one or m
3de90 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ore.**
3dea0 75 6e 66 69 6e 61 6c 69 7a 65 64 20 5b 70 72 65 unfinalized [pre
3deb0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
3dec0 5d 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 4c ] and S is a NUL
3ded0 4c 20 70 6f 69 6e 74 65 72 2c 0a 2a 2a 20 20 20 L pointer,.**
3dee0 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c then [sql
3def0 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 ite3_next_stmt(D
3df00 2c 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 , S)] routine sh
3df10 61 6c 6c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 all return a poi
3df20 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 nter.**
3df30 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 70 to one of the p
3df40 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
3df50 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ts associated wi
3df60 74 68 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 th D..**.** {H13
3df70 31 34 36 7d 20 49 66 20 44 20 69 73 20 61 20 5b 146} If D is a [
3df80 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3df90 69 6f 6e 5d 20 74 68 61 74 20 68 6f 6c 64 73 20 ion] that holds
3dfa0 6e 6f 20 75 6e 66 69 6e 61 6c 69 7a 65 64 0a 2a no unfinalized.*
3dfb0 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 * [prep
3dfc0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d ared statements]
3dfd0 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 4c 4c and S is a NULL
3dfe0 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 0a 2a pointer, then.*
3dff0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
3e000 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c te3_next_stmt(D,
3e010 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 61 S)] routine sha
3e020 6c 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c ll return a NULL
3e030 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
3e040 7b 48 31 33 31 34 39 7d 20 49 66 20 53 20 69 73 {H13149} If S is
3e050 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 a [prepared sta
3e060 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 65 20 5b tement] in the [
3e070 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3e080 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 20 20 20 ion] D.**
3e090 20 20 20 61 6e 64 20 53 20 69 73 20 6e 6f 74 20 and S is not
3e0a0 74 68 65 20 6c 61 73 74 20 70 72 65 70 61 72 65 the last prepare
3e0b0 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 44 d statement in D
3e0c0 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 , then.**
3e0d0 20 20 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74 [sqlite3_next
3e0e0 5f 73 74 6d 74 28 44 2c 20 53 29 5d 20 72 6f 75 _stmt(D, S)] rou
3e0f0 74 69 6e 65 20 73 68 61 6c 6c 20 72 65 74 75 72 tine shall retur
3e100 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 n a pointer.**
3e110 20 20 20 20 20 20 20 20 74 6f 20 74 68 65 20 6e to the n
3e120 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74 61 ext prepared sta
3e130 74 65 6d 65 6e 74 20 69 6e 20 44 20 61 66 74 65 tement in D afte
3e140 72 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31 r S..**.** {H131
3e150 35 32 7d 20 49 66 20 53 20 69 73 20 74 68 65 20 52} If S is the
3e160 6c 61 73 74 20 5b 70 72 65 70 61 72 65 64 20 73 last [prepared s
3e170 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 65 tatement] in the
3e180 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 61 .** [da
3e190 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3e1a0 6e 5d 20 44 20 74 68 65 6e 20 74 68 65 20 5b 73 n] D then the [s
3e1b0 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 qlite3_next_stmt
3e1c0 28 44 2c 20 53 29 5d 0a 2a 2a 20 20 20 20 20 20 (D, S)].**
3e1d0 20 20 20 20 72 6f 75 74 69 6e 65 20 73 68 61 6c routine shal
3e1e0 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c 20 l return a NULL
3e1f0 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 pointer..**.** A
3e200 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a SSUMPTIONS:.**.*
3e210 2a 20 7b 41 31 33 31 35 34 7d 20 54 68 65 20 5b * {A13154} The [
3e220 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3e230 69 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20 69 ion] pointer D i
3e240 6e 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 n a call to.**
3e250 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
3e260 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d _next_stmt(D,S)]
3e270 20 6d 75 73 74 20 72 65 66 65 72 20 74 6f 20 61 must refer to a
3e280 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a n open database.
3e290 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 6e ** conn
3e2a0 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e 20 70 61 ection and in pa
3e2b0 72 74 69 63 75 6c 61 72 20 6d 75 73 74 20 6e 6f rticular must no
3e2c0 74 20 62 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e t be a NULL poin
3e2d0 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ter..*/.SQLITE_A
3e2e0 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 PI sqlite3_stmt
3e2f0 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 *sqlite3_next_st
3e300 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c mt(sqlite3 *pDb,
3e310 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 sqlite3_stmt *p
3e320 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 Stmt);../*.** CA
3e330 50 49 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 PI3REF: Commit A
3e340 6e 64 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 nd Rollback Noti
3e350 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 fication Callbac
3e360 6b 73 20 7b 48 31 32 39 35 30 7d 20 3c 53 36 30 ks {H12950} <S60
3e370 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 400>.**.** The s
3e380 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f qlite3_commit_ho
3e390 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ok() interface r
3e3a0 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 egisters a callb
3e3b0 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ack.** function
3e3c0 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 to be invoked wh
3e3d0 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 enever a transac
3e3e0 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 tion is committe
3e3f0 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 d..** Any callba
3e400 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 ck set by a prev
3e410 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c ious call to sql
3e420 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b ite3_commit_hook
3e430 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 ().** for the sa
3e440 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
3e450 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 ection is overri
3e460 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c dden..** The sql
3e470 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f ite3_rollback_ho
3e480 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ok() interface r
3e490 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 egisters a callb
3e4a0 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ack.** function
3e4b0 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 to be invoked wh
3e4c0 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 enever a transac
3e4d0 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 tion is committe
3e4e0 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 d..** Any callba
3e4f0 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 ck set by a prev
3e500 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c ious call to sql
3e510 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b ite3_commit_hook
3e520 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 ().** for the sa
3e530 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
3e540 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 ection is overri
3e550 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72 dden..** The pAr
3e560 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61 g argument is pa
3e570 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 ssed through to
3e580 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a the callback..**
3e590 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b If the callback
3e5a0 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f on a commit hoo
3e5b0 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 k function retur
3e5c0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 ns non-zero,.**
3e5d0 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 then the commit
3e5e0 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 is converted int
3e5f0 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a o a rollback..**
3e600 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 .** If another f
3e610 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 unction was prev
3e620 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 iously registere
3e630 64 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20 76 d, its.** pArg v
3e640 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 alue is returned
3e650 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c . Otherwise NUL
3e660 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a L is returned..*
3e670 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 *.** The callbac
3e680 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
3e690 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 must not do any
3e6a0 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 thing that will
3e6b0 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 modify.** the da
3e6c0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3e6d0 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 n that invoked t
3e6e0 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e he callback. An
3e6f0 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 y actions.** to
3e700 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 modify the datab
3e710 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d ase connection m
3e720 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 ust be deferred
3e730 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a until after the.
3e740 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 ** completion of
3e750 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 the [sqlite3_st
3e760 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 ep()] call that
3e770 74 72 69 67 67 65 72 65 64 20 74 68 65 20 63 6f triggered the co
3e780 6d 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 mmit.** or rollb
3e790 61 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 ack hook in the
3e7a0 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 first place..**
3e7b0 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 Note that [sqlit
3e7c0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
3e7d0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 and [sqlite3_st
3e7e0 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 ep()] both modif
3e7f0 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 y their.** datab
3e800 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ase connections
3e810 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 for the meaning
3e820 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 of "modify" in t
3e830 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a his paragraph..*
3e840 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e 67 *.** Registering
3e850 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e a NULL function
3e860 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61 disables the ca
3e870 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f llback..**.** Fo
3e880 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f r the purposes o
3e890 66 20 74 68 69 73 20 41 50 49 2c 20 61 20 74 72 f this API, a tr
3e8a0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61 69 ansaction is sai
3e8b0 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a 2a d to have been.*
3e8c0 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 * rolled back if
3e8d0 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52 4f an explicit "RO
3e8e0 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65 6e LLBACK" statemen
3e8f0 74 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 6f t is executed, o
3e900 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 72 r.** an error or
3e910 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75 73 constraint caus
3e920 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 72 es an implicit r
3e930 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75 72 ollback to occur
3e940 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 ..** The rollbac
3e950 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f k callback is no
3e960 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 t invoked if a t
3e970 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a ransaction is.**
3e980 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 automatically r
3e990 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 olled back becau
3e9a0 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 se the database
3e9b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c connection is cl
3e9c0 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c osed..** The rol
3e9d0 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 lback callback i
3e9e0 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 s not invoked if
3e9f0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
3ea00 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b s.** rolled back
3ea10 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d 69 because a commi
3ea20 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 t callback retur
3ea30 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a ned non-zero..**
3ea40 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f 6e <todo> Check on
3ea50 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a 2a this </todo>.**
3ea60 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
3ea70 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 31 7d 20 54 **.** {H12951} T
3ea80 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d he [sqlite3_comm
3ea90 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 it_hook(D,F,P)]
3eaa0 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 interface regist
3eab0 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 ers the.**
3eac0 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e callback fun
3ead0 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 69 6e ction F to be in
3eae0 76 6f 6b 65 64 20 77 69 74 68 20 61 72 67 75 6d voked with argum
3eaf0 65 6e 74 20 50 20 77 68 65 6e 65 76 65 72 0a 2a ent P whenever.*
3eb00 2a 20 20 20 20 20 20 20 20 20 20 61 20 74 72 61 * a tra
3eb10 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 nsaction commits
3eb20 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 on the [databas
3eb30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e e connection] D.
3eb40 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 32 7d 20 .**.** {H12952}
3eb50 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d The [sqlite3_com
3eb60 6d 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d mit_hook(D,F,P)]
3eb70 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
3eb80 6e 73 20 74 68 65 20 50 20 61 72 67 75 6d 65 6e ns the P argumen
3eb90 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 72 t.** fr
3eba0 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 om the previous
3ebb0 63 61 6c 6c 20 77 69 74 68 20 74 68 65 20 73 61 call with the sa
3ebc0 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e me [database con
3ebd0 6e 65 63 74 69 6f 6e 5d 20 44 2c 0a 2a 2a 20 20 nection] D,.**
3ebe0 20 20 20 20 20 20 20 20 6f 72 20 4e 55 4c 4c 20 or NULL
3ebf0 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c on the first cal
3ec00 6c 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c l for a particul
3ec10 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ar database conn
3ec20 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a 0a 2a 2a 20 ection D..**.**
3ec30 7b 48 31 32 39 35 33 7d 20 45 61 63 68 20 63 61 {H12953} Each ca
3ec40 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ll to [sqlite3_c
3ec50 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20 6f 76 ommit_hook()] ov
3ec60 65 72 77 72 69 74 65 73 20 74 68 65 20 63 61 6c erwrites the cal
3ec70 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 lback.**
3ec80 20 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20 registered by
3ec90 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2a 0a prior calls..**.
3eca0 2a 2a 20 7b 48 31 32 39 35 34 7d 20 49 66 20 74 ** {H12954} If t
3ecb0 68 65 20 46 20 61 72 67 75 6d 65 6e 74 20 74 6f he F argument to
3ecc0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 [sqlite3_commit
3ecd0 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 69 73 _hook(D,F,P)] is
3ece0 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 NULL.**
3ecf0 20 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 then the commi
3ed00 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20 t hook callback
3ed10 69 73 20 63 61 6e 63 65 6c 65 64 20 61 6e 64 20 is canceled and
3ed20 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 no callback.**
3ed30 20 20 20 20 20 20 20 20 69 73 20 69 6e 76 6f 6b is invok
3ed40 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 ed when a transa
3ed50 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 2e 0a 2a ction commits..*
3ed60 2a 0a 2a 2a 20 7b 48 31 32 39 35 35 7d 20 49 66 *.** {H12955} If
3ed70 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 61 6c 6c the commit call
3ed80 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e back returns non
3ed90 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 63 -zero then the c
3eda0 6f 6d 6d 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 ommit is.**
3edb0 20 20 20 20 20 63 6f 6e 76 65 72 74 65 64 20 69 converted i
3edc0 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a nto a rollback..
3edd0 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 31 7d 20 54 **.** {H12961} T
3ede0 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c 6c he [sqlite3_roll
3edf0 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 back_hook(D,F,P)
3ee00 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 ] interface regi
3ee10 73 74 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 20 sters the.**
3ee20 20 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 66 callback f
3ee30 75 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 unction F to be
3ee40 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 72 67 invoked with arg
3ee50 75 6d 65 6e 74 20 50 20 77 68 65 6e 65 76 65 72 ument P whenever
3ee60 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 74 .** a t
3ee70 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 73 ransaction rolls
3ee80 20 62 61 63 6b 20 6f 6e 20 74 68 65 20 5b 64 61 back on the [da
3ee90 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3eea0 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 n] D..**.** {H12
3eeb0 39 36 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 962} The [sqlite
3eec0 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 3_rollback_hook(
3eed0 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 D,F,P)] interfac
3eee0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 50 0a e returns the P.
3eef0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72 67 75 ** argu
3ef00 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 72 ment from the pr
3ef10 65 76 69 6f 75 73 20 63 61 6c 6c 20 77 69 74 68 evious call with
3ef20 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 the same.**
3ef30 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 [database
3ef40 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2c 20 6f connection] D, o
3ef50 72 20 4e 55 4c 4c 20 6f 6e 20 74 68 65 20 66 69 r NULL on the fi
3ef60 72 73 74 20 63 61 6c 6c 0a 2a 2a 20 20 20 20 20 rst call.**
3ef70 20 20 20 20 20 66 6f 72 20 61 20 70 61 72 74 69 for a parti
3ef80 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 cular database c
3ef90 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a 0a onnection D..**.
3efa0 2a 2a 20 7b 48 31 32 39 36 33 7d 20 45 61 63 68 ** {H12963} Each
3efb0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
3efc0 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 3_rollback_hook(
3efd0 29 5d 20 6f 76 65 72 77 72 69 74 65 73 20 74 68 )] overwrites th
3efe0 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 e callback.**
3eff0 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 registere
3f000 64 20 62 79 20 70 72 69 6f 72 20 63 61 6c 6c 73 d by prior calls
3f010 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 34 7d ..**.** {H12964}
3f020 20 49 66 20 74 68 65 20 46 20 61 72 67 75 6d 65 If the F argume
3f030 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 nt to [sqlite3_r
3f040 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 46 ollback_hook(D,F
3f050 2c 50 29 5d 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 ,P)] is NULL.**
3f060 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 74 68 then th
3f070 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 e rollback hook
3f080 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61 6e 63 callback is canc
3f090 65 6c 65 64 20 61 6e 64 20 6e 6f 20 63 61 6c 6c eled and no call
3f0a0 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20 back.**
3f0b0 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e is invoked when
3f0c0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 a transaction r
3f0d0 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2f 0a 53 51 olls back..*/.SQ
3f0e0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
3f0f0 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f qlite3_commit_ho
3f100 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 ok(sqlite3*, int
3f110 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 (*)(void*), void
3f120 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 *);.SQLITE_API v
3f130 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c oid *sqlite3_rol
3f140 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 lback_hook(sqlit
3f150 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 e3*, void(*)(voi
3f160 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f d *), void*);../
3f170 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
3f180 61 74 61 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 ata Change Notif
3f190 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b ication Callback
3f1a0 73 20 7b 48 31 32 39 37 30 7d 20 3c 53 36 30 34 s {H12970} <S604
3f1b0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
3f1c0 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f lite3_update_hoo
3f1d0 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 k() interface re
3f1e0 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 gisters a callba
3f1f0 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 ck function.** w
3f200 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 ith the [databas
3f210 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 e connection] id
3f220 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 entified by the
3f230 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a first argument.*
3f240 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 * to be invoked
3f250 77 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 69 whenever a row i
3f260 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72 s updated, inser
3f270 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a ted or deleted..
3f280 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 ** Any callback
3f290 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f 75 set by a previou
3f2a0 73 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 s call to this f
3f2b0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 unction.** for t
3f2c0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
3f2d0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f connection is o
3f2e0 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a verridden..**.**
3f2f0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
3f300 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 ment is a pointe
3f310 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f r to the functio
3f320 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e n to invoke when
3f330 20 61 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 a.** row is upd
3f340 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f ated, inserted o
3f350 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 r deleted..** Th
3f360 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
3f370 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b to the callback
3f380 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 is a copy of th
3f390 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
3f3a0 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 .** to sqlite3_u
3f3b0 70 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a pdate_hook()..**
3f3c0 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c The second call
3f3d0 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 back argument is
3f3e0 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f one of [SQLITE_
3f3f0 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 INSERT], [SQLITE
3f400 5f 44 45 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 _DELETE],.** or
3f410 5b 53 51 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c [SQLITE_UPDATE],
3f420 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 depending on th
3f430 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 e operation that
3f440 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c caused the call
3f450 62 61 63 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e back.** to be in
3f460 76 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 voked..** The th
3f470 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 ird and fourth a
3f480 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
3f490 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e callback contain
3f4a0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 pointers to the
3f4b0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64 .** database and
3f4c0 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 table name cont
3f4d0 61 69 6e 69 6e 67 20 74 68 65 20 61 66 66 65 63 aining the affec
3f4e0 74 65 64 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 ted row..** The
3f4f0 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 final callback p
3f500 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 arameter is the
3f510 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 72 [rowid] of the r
3f520 6f 77 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61 ow..** In the ca
3f530 73 65 20 6f 66 20 61 6e 20 75 70 64 61 74 65 2c se of an update,
3f540 20 74 68 69 73 20 69 73 20 74 68 65 20 5b 72 6f this is the [ro
3f550 77 69 64 5d 20 61 66 74 65 72 20 74 68 65 20 75 wid] after the u
3f560 70 64 61 74 65 20 74 61 6b 65 73 20 70 6c 61 63 pdate takes plac
3f570 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 e..**.** The upd
3f580 61 74 65 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 ate hook is not
3f590 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 invoked when int
3f5a0 65 72 6e 61 6c 20 73 79 73 74 65 6d 20 74 61 62 ernal system tab
3f5b0 6c 65 73 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 les are.** modif
3f5c0 69 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 ied (i.e. sqlite
3f5d0 5f 6d 61 73 74 65 72 20 61 6e 64 20 73 71 6c 69 _master and sqli
3f5e0 74 65 5f 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a te_sequence)..**
3f5f0 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 68 .** The update h
3f600 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ook implementati
3f610 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 on must not do a
3f620 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c nything that wil
3f630 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 l modify.** the
3f640 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3f650 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 ion that invoked
3f660 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b the update hook
3f670 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a . Any actions.*
3f680 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 * to modify the
3f690 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3f6a0 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66 65 ion must be defe
3f6b0 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65 72 rred until after
3f6c0 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 the.** completi
3f6d0 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 on of the [sqlit
3f6e0 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 e3_step()] call
3f6f0 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20 74 that triggered t
3f700 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a he update hook..
3f710 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 ** Note that [sq
3f720 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
3f730 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
3f740 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f _step()] both mo
3f750 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 dify their.** da
3f760 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3f770 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 ns for the meani
3f780 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 ng of "modify" i
3f790 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 n this paragraph
3f7a0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 ..**.** If anoth
3f7b0 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 er function was
3f7c0 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 previously regis
3f7d0 74 65 72 65 64 2c 20 69 74 73 20 70 41 72 67 20 tered, its pArg
3f7e0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 72 65 74 75 value.** is retu
3f7f0 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 rned. Otherwise
3f800 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 NULL is returne
3f810 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 d..**.** INVARIA
3f820 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 NTS:.**.** {H129
3f830 37 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 71} The [sqlite3
3f840 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 44 2c 46 _update_hook(D,F
3f850 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 ,P)] interface c
3f860 61 75 73 65 73 20 74 68 65 20 63 61 6c 6c 62 61 auses the callba
3f870 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 ck.** f
3f880 75 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 unction F to be
3f890 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 66 69 72 invoked with fir
3f8a0 73 74 20 70 61 72 61 6d 65 74 65 72 20 50 20 77 st parameter P w
3f8b0 68 65 6e 65 76 65 72 0a 2a 2a 20 20 20 20 20 20 henever.**
3f8c0 20 20 20 20 61 20 74 61 62 6c 65 20 72 6f 77 20 a table row
3f8d0 69 73 20 6d 6f 64 69 66 69 65 64 2c 20 69 6e 73 is modified, ins
3f8e0 65 72 74 65 64 2c 20 6f 72 20 64 65 6c 65 74 65 erted, or delete
3f8f0 64 20 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 d on.**
3f900 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
3f910 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a onnection] D..**
3f920 0a 2a 2a 20 7b 48 31 32 39 37 33 7d 20 54 68 65 .** {H12973} The
3f930 20 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 [sqlite3_update
3f940 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 69 6e _hook(D,F,P)] in
3f950 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
3f960 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 20 20 20 the value.**
3f970 20 20 20 20 20 20 6f 66 20 50 20 66 6f 72 20 74 of P for t
3f980 68 65 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c he previous call
3f990 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61 on the same [da
3f9a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3f9b0 6e 5d 20 44 2c 0a 2a 2a 20 20 20 20 20 20 20 20 n] D,.**
3f9c0 20 20 6f 72 20 4e 55 4c 4c 20 66 6f 72 20 74 68 or NULL for th
3f9d0 65 20 66 69 72 73 74 20 63 61 6c 6c 2e 0a 2a 2a e first call..**
3f9e0 0a 2a 2a 20 7b 48 31 32 39 37 35 7d 20 49 66 20 .** {H12975} If
3f9f0 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 the update hook
3fa00 63 61 6c 6c 62 61 63 6b 20 46 20 69 6e 20 5b 73 callback F in [s
3fa10 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f qlite3_update_ho
3fa20 6f 6b 28 44 2c 46 2c 50 29 5d 0a 2a 2a 20 20 20 ok(D,F,P)].**
3fa30 20 20 20 20 20 20 20 69 73 20 4e 55 4c 4c 20 74 is NULL t
3fa40 68 65 6e 20 74 68 65 20 6e 6f 20 75 70 64 61 74 hen the no updat
3fa50 65 20 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 e callbacks are
3fa60 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 made..**.** {H12
3fa70 39 37 37 7d 20 45 61 63 68 20 63 61 6c 6c 20 74 977} Each call t
3fa80 6f 20 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74 o [sqlite3_updat
3fa90 65 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 6f e_hook(D,F,P)] o
3faa0 76 65 72 72 69 64 65 73 20 70 72 69 6f 72 20 63 verrides prior c
3fab0 61 6c 6c 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 alls.**
3fac0 20 74 6f 20 74 68 65 20 73 61 6d 65 20 69 6e 74 to the same int
3fad0 65 72 66 61 63 65 20 6f 6e 20 74 68 65 20 73 61 erface on the sa
3fae0 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e me [database con
3faf0 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a nection] D..**.*
3fb00 2a 20 7b 48 31 32 39 37 39 7d 20 54 68 65 20 75 * {H12979} The u
3fb10 70 64 61 74 65 20 68 6f 6f 6b 20 63 61 6c 6c 62 pdate hook callb
3fb20 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b ack is not invok
3fb30 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e 61 6c ed when internal
3fb40 20 73 79 73 74 65 6d 0a 2a 2a 20 20 20 20 20 20 system.**
3fb50 20 20 20 20 74 61 62 6c 65 73 20 73 75 63 68 20 tables such
3fb60 61 73 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 as sqlite_master
3fb70 20 61 6e 64 20 73 71 6c 69 74 65 5f 73 65 71 75 and sqlite_sequ
3fb80 65 6e 63 65 20 61 72 65 20 6d 6f 64 69 66 69 65 ence are modifie
3fb90 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 38 31 d..**.** {H12981
3fba0 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 } The second par
3fbb0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 75 70 ameter to the up
3fbc0 64 61 74 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a date callback.**
3fbd0 20 20 20 20 20 20 20 20 20 20 69 73 20 6f 6e 65 is one
3fbe0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 of [SQLITE_INSE
3fbf0 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c RT], [SQLITE_DEL
3fc00 45 54 45 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f ETE] or [SQLITE_
3fc10 55 50 44 41 54 45 5d 2c 0a 2a 2a 20 20 20 20 20 UPDATE],.**
3fc20 20 20 20 20 20 64 65 70 65 6e 64 69 6e 67 20 6f depending o
3fc30 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 n the operation
3fc40 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20 that caused the
3fc50 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 callback to be i
3fc60 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 nvoked..**.** {H
3fc70 31 32 39 38 33 7d 20 54 68 65 20 74 68 69 72 64 12983} The third
3fc80 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 and fourth argu
3fc90 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c ments to the cal
3fca0 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f lback contain po
3fcb0 69 6e 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 inters.**
3fcc0 20 20 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 to zero-termi
3fcd0 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 nated UTF-8 stri
3fce0 6e 67 73 20 77 68 69 63 68 20 61 72 65 20 74 68 ngs which are th
3fcf0 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a e names of the.*
3fd00 2a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 * datab
3fd10 61 73 65 20 61 6e 64 20 74 61 62 6c 65 20 74 68 ase and table th
3fd20 61 74 20 69 73 20 62 65 69 6e 67 20 75 70 64 61 at is being upda
3fd30 74 65 64 2e 0a 0a 2a 2a 20 7b 48 31 32 39 38 35 ted...** {H12985
3fd40 7d 20 54 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c } The final call
3fd50 62 61 63 6b 20 70 61 72 61 6d 65 74 65 72 20 69 back parameter i
3fd60 73 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 s the [rowid] of
3fd70 20 74 68 65 20 72 6f 77 20 61 66 74 65 72 0a 2a the row after.*
3fd80 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 63 * the c
3fd90 68 61 6e 67 65 20 6f 63 63 75 72 73 2e 0a 2a 2f hange occurs..*/
3fda0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
3fdb0 20 2a 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 *sqlite3_update
3fdc0 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 _hook(. sqlite3
3fdd0 2a 2c 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f *, . void(*)(vo
3fde0 69 64 20 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63 id *,int ,char c
3fdf0 6f 6e 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 onst *,char cons
3fe00 74 20 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 t *,sqlite3_int6
3fe10 34 29 2c 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 4),. void*.);..
3fe20 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3fe30 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c Enable Or Disabl
3fe40 65 20 53 68 61 72 65 64 20 50 61 67 65 72 20 43 e Shared Pager C
3fe50 61 63 68 65 20 7b 48 31 30 33 33 30 7d 20 3c 53 ache {H10330} <S
3fe60 33 30 39 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 30900>.** KEYWOR
3fe70 44 53 3a 20 7b 73 68 61 72 65 64 20 63 61 63 68 DS: {shared cach
3fe80 65 7d 20 7b 73 68 61 72 65 64 20 63 61 63 68 65 e} {shared cache
3fe90 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 mode}.**.** Thi
3fea0 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 s routine enable
3feb0 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 s or disables th
3fec0 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74 68 65 e sharing of the
3fed0 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 0a database cache.
3fee0 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20 64 61 ** and schema da
3fef0 74 61 20 73 74 72 75 63 74 75 72 65 73 20 62 65 ta structures be
3ff00 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73 65 20 tween [database
3ff10 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63 6f 6e connection | con
3ff20 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74 6f 20 nections].** to
3ff30 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
3ff40 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20 65 6e e. Sharing is en
3ff50 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 67 abled if the arg
3ff60 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a 2a 2a ument is true.**
3ff70 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 69 66 and disabled if
3ff80 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 the argument is
3ff90 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 43 61 false..**.** Ca
3ffa0 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20 65 che sharing is e
3ffb0 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73 61 62 nabled and disab
3ffc0 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 led for an entir
3ffd0 65 20 70 72 6f 63 65 73 73 2e 20 7b 45 4e 44 7d e process. {END}
3ffe0 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 63 68 .** This is a ch
3fff0 61 6e 67 65 20 61 73 20 6f 66 20 53 51 4c 69 74 ange as of SQLit
40000 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2e e version 3.5.0.
40010 20 49 6e 20 70 72 69 6f 72 20 76 65 72 73 69 6f In prior versio
40020 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 2a 2a ns of SQLite,.**
40030 20 73 68 61 72 69 6e 67 20 77 61 73 20 65 6e 61 sharing was ena
40040 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 bled or disabled
40050 20 66 6f 72 20 65 61 63 68 20 74 68 72 65 61 64 for each thread
40060 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a separately..**.
40070 2a 2a 20 54 68 65 20 63 61 63 68 65 20 73 68 61 ** The cache sha
40080 72 69 6e 67 20 6d 6f 64 65 20 73 65 74 20 62 79 ring mode set by
40090 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 this interface
400a0 65 66 66 65 63 74 73 20 61 6c 6c 20 73 75 62 73 effects all subs
400b0 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 equent.** calls
400c0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e to [sqlite3_open
400d0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 ()], [sqlite3_op
400e0 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 5b 73 en_v2()], and [s
400f0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d qlite3_open16()]
40100 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 64 61 ..** Existing da
40110 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
40120 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 73 65 20 ns continue use
40130 74 68 65 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 the sharing mode
40140 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 69 6e 20 .** that was in
40150 65 66 66 65 63 74 20 61 74 20 74 68 65 20 74 69 effect at the ti
40160 6d 65 20 74 68 65 79 20 77 65 72 65 20 6f 70 65 me they were ope
40170 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 ned..**.** Virtu
40180 61 6c 20 74 61 62 6c 65 73 20 63 61 6e 6e 6f 74 al tables cannot
40190 20 62 65 20 75 73 65 64 20 77 69 74 68 20 61 20 be used with a
401a0 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 57 shared cache. W
401b0 68 65 6e 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 hen shared.** ca
401c0 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 che is enabled,
401d0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 the [sqlite3_cre
401e0 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 41 50 ate_module()] AP
401f0 49 20 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 I used to regist
40200 65 72 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 er.** virtual ta
40210 62 6c 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 bles will always
40220 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 return an error
40230 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
40240 74 69 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 tine returns [SQ
40250 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 LITE_OK] if shar
40260 65 64 20 63 61 63 68 65 20 77 61 73 20 65 6e 61 ed cache was ena
40270 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 bled or disabled
40280 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 .** successfully
40290 2e 20 20 41 6e 20 5b 65 72 72 6f 72 20 63 6f 64 . An [error cod
402a0 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f e] is returned o
402b0 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 therwise..**.**
402c0 53 68 61 72 65 64 20 63 61 63 68 65 20 69 73 20 Shared cache is
402d0 64 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 disabled by defa
402e0 75 6c 74 2e 20 42 75 74 20 74 68 69 73 20 6d 69 ult. But this mi
402f0 67 68 74 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a ght change in.**
40300 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
40310 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41 70 70 of SQLite. App
40320 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 63 lications that c
40330 61 72 65 20 61 62 6f 75 74 20 73 68 61 72 65 64 are about shared
40340 0a 2a 2a 20 63 61 63 68 65 20 73 65 74 74 69 6e .** cache settin
40350 67 20 73 68 6f 75 6c 64 20 73 65 74 20 69 74 20 g should set it
40360 65 78 70 6c 69 63 69 74 6c 79 2e 0a 2a 2a 0a 2a explicitly..**.*
40370 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
40380 0a 2a 2a 20 7b 48 31 30 33 33 31 7d 20 41 20 73 .** {H10331} A s
40390 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 uccessful invoca
403a0 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 tion of [sqlite3
403b0 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 _enable_shared_c
403c0 61 63 68 65 28 42 29 5d 0a 2a 2a 20 20 20 20 20 ache(B)].**
403d0 20 20 20 20 20 77 69 6c 6c 20 65 6e 61 62 6c 65 will enable
403e0 20 6f 72 20 64 69 73 61 62 6c 65 20 73 68 61 72 or disable shar
403f0 65 64 20 63 61 63 68 65 20 6d 6f 64 65 20 66 6f ed cache mode fo
40400 72 20 61 6e 79 20 73 75 62 73 65 71 75 65 6e 74 r any subsequent
40410 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 ly.** c
40420 72 65 61 74 65 64 20 5b 64 61 74 61 62 61 73 65 reated [database
40430 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 6e 20 connection] in
40440 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 the same process
40450 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 33 33 36 7d ..**.** {H10336}
40460 20 57 68 65 6e 20 73 68 61 72 65 64 20 63 61 63 When shared cac
40470 68 65 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 he is enabled, t
40480 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 he [sqlite3_crea
40490 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 0a 2a 2a 20 te_module()].**
404a0 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61 interfa
404b0 63 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 ce will always r
404c0 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a eturn an error..
404d0 2a 2a 0a 2a 2a 20 7b 48 31 30 33 33 37 7d 20 54 **.** {H10337} T
404e0 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 he [sqlite3_enab
404f0 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 le_shared_cache(
40500 42 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 B)] interface re
40510 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 turns.**
40520 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 [SQLITE_OK] if
40530 20 73 68 61 72 65 64 20 63 61 63 68 65 20 77 61 shared cache wa
40540 73 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 s enabled or dis
40550 61 62 6c 65 64 20 73 75 63 63 65 73 73 66 75 6c abled successful
40560 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 33 33 ly..**.** {H1033
40570 39 7d 20 53 68 61 72 65 64 20 63 61 63 68 65 20 9} Shared cache
40580 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 is disabled by d
40590 65 66 61 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 efault..*/.SQLIT
405a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
405b0 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 3_enable_shared_
405c0 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a cache(int);../*.
405d0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 74 74 ** CAPI3REF: Att
405e0 65 6d 70 74 20 54 6f 20 46 72 65 65 20 48 65 61 empt To Free Hea
405f0 70 20 4d 65 6d 6f 72 79 20 7b 48 31 37 33 34 30 p Memory {H17340
40600 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a } <S30220>.**.**
40610 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c The sqlite3_rel
40620 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e ease_memory() in
40630 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 terface attempts
40640 20 74 6f 20 66 72 65 65 20 4e 20 62 79 74 65 73 to free N bytes
40650 0a 2a 2a 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f .** of heap memo
40660 72 79 20 62 79 20 64 65 61 6c 6c 6f 63 61 74 69 ry by deallocati
40670 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c ng non-essential
40680 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
40690 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20 62 79 20 74 ons.** held by t
406a0 68 65 20 64 61 74 61 62 61 73 65 20 6c 69 62 72 he database libr
406b0 61 72 79 2e 20 7b 45 4e 44 7d 20 20 4d 65 6d 6f ary. {END} Memo
406c0 72 79 20 75 73 65 64 20 74 6f 20 63 61 63 68 65 ry used to cache
406d0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67 database.** pag
406e0 65 73 20 74 6f 20 69 6d 70 72 6f 76 65 20 70 65 es to improve pe
406f0 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6e 20 rformance is an
40700 65 78 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 example of non-e
40710 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 2e ssential memory.
40720 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 .** sqlite3_rele
40730 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 72 65 74 ase_memory() ret
40740 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
40750 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c of bytes actuall
40760 79 20 66 72 65 65 64 2c 0a 2a 2a 20 77 68 69 63 y freed,.** whic
40770 68 20 6d 69 67 68 74 20 62 65 20 6d 6f 72 65 20 h might be more
40780 6f 72 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 or less than the
40790 20 61 6d 6f 75 6e 74 20 72 65 71 75 65 73 74 65 amount requeste
407a0 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 d..**.** INVARIA
407b0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 NTS:.**.** {H173
407c0 34 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 41} The [sqlite3
407d0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
407e0 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 74 N)] interface at
407f0 74 65 6d 70 74 73 20 74 6f 0a 2a 2a 20 20 20 20 tempts to.**
40800 20 20 20 20 20 20 66 72 65 65 20 4e 20 62 79 74 free N byt
40810 65 73 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 es of heap memor
40820 79 20 62 79 20 64 65 61 6c 6c 6f 63 61 74 69 6e y by deallocatin
40830 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 0a g non-essential.
40840 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f ** memo
40850 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 68 ry allocations h
40860 65 6c 64 20 62 79 20 74 68 65 20 64 61 74 61 62 eld by the datab
40870 61 73 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a ase library..**.
40880 2a 2a 20 7b 48 31 36 33 34 32 7d 20 54 68 65 20 ** {H16342} The
40890 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 [sqlite3_release
408a0 5f 6d 65 6d 6f 72 79 28 4e 29 5d 20 72 65 74 75 _memory(N)] retu
408b0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a rns the number.*
408c0 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 62 79 * of by
408d0 74 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65 tes actually fre
408e0 65 64 2c 20 77 68 69 63 68 20 6d 69 67 68 74 20 ed, which might
408f0 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65 73 73 0a be more or less.
40900 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 61 6e ** than
40910 20 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75 the amount requ
40920 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ested..*/.SQLITE
40930 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
40940 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
40950 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 int);../*.** CAP
40960 49 33 52 45 46 3a 20 49 6d 70 6f 73 65 20 41 20 I3REF: Impose A
40970 4c 69 6d 69 74 20 4f 6e 20 48 65 61 70 20 53 69 Limit On Heap Si
40980 7a 65 20 7b 48 31 37 33 35 30 7d 20 3c 53 33 30 ze {H17350} <S30
40990 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 220>.**.** The s
409a0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 qlite3_soft_heap
409b0 5f 6c 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 _limit() interfa
409c0 63 65 20 70 6c 61 63 65 73 20 61 20 22 73 6f 66 ce places a "sof
409d0 74 22 20 6c 69 6d 69 74 0a 2a 2a 20 6f 6e 20 74 t" limit.** on t
409e0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61 he amount of hea
409f0 70 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 61 p memory that ma
40a00 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 y be allocated b
40a10 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 49 66 20 y SQLite..** If
40a20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f an internal allo
40a30 63 61 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73 cation is reques
40a40 74 65 64 20 74 68 61 74 20 77 6f 75 6c 64 20 65 ted that would e
40a50 78 63 65 65 64 20 74 68 65 0a 2a 2a 20 73 6f 66 xceed the.** sof
40a60 74 20 68 65 61 70 20 6c 69 6d 69 74 2c 20 5b 73 t heap limit, [s
40a70 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
40a80 65 6d 6f 72 79 28 29 5d 20 69 73 20 69 6e 76 6f emory()] is invo
40a90 6b 65 64 20 6f 6e 65 20 6f 72 0a 2a 2a 20 6d 6f ked one or.** mo
40aa0 72 65 20 74 69 6d 65 73 20 74 6f 20 66 72 65 65 re times to free
40ab0 20 75 70 20 73 6f 6d 65 20 73 70 61 63 65 20 62 up some space b
40ac0 65 66 6f 72 65 20 74 68 65 20 61 6c 6c 6f 63 61 efore the alloca
40ad0 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 tion is performe
40ae0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 6d d..**.** The lim
40af0 69 74 20 69 73 20 63 61 6c 6c 65 64 20 22 73 6f it is called "so
40b00 66 74 22 2c 20 62 65 63 61 75 73 65 20 69 66 20 ft", because if
40b10 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 [sqlite3_release
40b20 5f 6d 65 6d 6f 72 79 28 29 5d 0a 2a 2a 20 63 61 _memory()].** ca
40b30 6e 6e 6f 74 20 66 72 65 65 20 73 75 66 66 69 63 nnot free suffic
40b40 69 65 6e 74 20 6d 65 6d 6f 72 79 20 74 6f 20 70 ient memory to p
40b50 72 65 76 65 6e 74 20 74 68 65 20 6c 69 6d 69 74 revent the limit
40b60 20 66 72 6f 6d 20 62 65 69 6e 67 20 65 78 63 65 from being exce
40b70 65 64 65 64 2c 0a 2a 2a 20 74 68 65 20 6d 65 6d eded,.** the mem
40b80 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ory is allocated
40b90 20 61 6e 79 77 61 79 20 61 6e 64 20 74 68 65 20 anyway and the
40ba0 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f current operatio
40bb0 6e 20 70 72 6f 63 65 65 64 73 2e 0a 2a 2a 0a 2a n proceeds..**.*
40bc0 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6f 72 20 * A negative or
40bd0 7a 65 72 6f 20 76 61 6c 75 65 20 66 6f 72 20 4e zero value for N
40be0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 means that ther
40bf0 65 20 69 73 20 6e 6f 20 73 6f 66 74 20 68 65 61 e is no soft hea
40c00 70 20 6c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20 5b p limit and.** [
40c10 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
40c20 6d 65 6d 6f 72 79 28 29 5d 20 77 69 6c 6c 20 6f memory()] will o
40c30 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 nly be called wh
40c40 65 6e 20 6d 65 6d 6f 72 79 20 69 73 20 65 78 68 en memory is exh
40c50 61 75 73 74 65 64 2e 0a 2a 2a 20 54 68 65 20 64 austed..** The d
40c60 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 efault value for
40c70 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c the soft heap l
40c80 69 6d 69 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a imit is zero..**
40c90 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 .** SQLite makes
40ca0 20 61 20 62 65 73 74 20 65 66 66 6f 72 74 20 74 a best effort t
40cb0 6f 20 68 6f 6e 6f 72 20 74 68 65 20 73 6f 66 74 o honor the soft
40cc0 20 68 65 61 70 20 6c 69 6d 69 74 2e 0a 2a 2a 20 heap limit..**
40cd0 42 75 74 20 69 66 20 74 68 65 20 73 6f 66 74 20 But if the soft
40ce0 68 65 61 70 20 6c 69 6d 69 74 20 63 61 6e 6e 6f heap limit canno
40cf0 74 20 62 65 20 68 6f 6e 6f 72 65 64 2c 20 65 78 t be honored, ex
40d00 65 63 75 74 69 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 ecution will.**
40d10 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 6f 75 74 continue without
40d20 20 65 72 72 6f 72 20 6f 72 20 6e 6f 74 69 66 69 error or notifi
40d30 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 cation. This is
40d40 20 77 68 79 20 74 68 65 20 6c 69 6d 69 74 20 69 why the limit i
40d50 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 20 22 73 s.** called a "s
40d60 6f 66 74 22 20 6c 69 6d 69 74 2e 20 20 49 74 20 oft" limit. It
40d70 69 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 is advisory only
40d80 2e 0a 2a 2a 0a 2a 2a 20 50 72 69 6f 72 20 74 6f ..**.** Prior to
40d90 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 SQLite version
40da0 33 2e 35 2e 30 2c 20 74 68 69 73 20 72 6f 75 74 3.5.0, this rout
40db0 69 6e 65 20 6f 6e 6c 79 20 63 6f 6e 73 74 72 61 ine only constra
40dc0 69 6e 65 64 20 74 68 65 20 6d 65 6d 6f 72 79 0a ined the memory.
40dd0 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 ** allocated by
40de0 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 a single thread
40df0 2d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 - the same threa
40e00 64 20 69 6e 20 77 68 69 63 68 20 74 68 69 73 20 d in which this
40e10 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 75 6e 73 2e routine.** runs.
40e20 20 20 42 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 Beginning with
40e30 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 SQLite version
40e40 33 2e 35 2e 30 2c 20 74 68 65 20 73 6f 66 74 20 3.5.0, the soft
40e50 68 65 61 70 20 6c 69 6d 69 74 20 69 73 0a 2a 2a heap limit is.**
40e60 20 61 70 70 6c 69 65 64 20 74 6f 20 61 6c 6c 20 applied to all
40e70 74 68 72 65 61 64 73 2e 20 54 68 65 20 76 61 6c threads. The val
40e80 75 65 20 73 70 65 63 69 66 69 65 64 20 66 6f 72 ue specified for
40e90 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c the soft heap l
40ea0 69 6d 69 74 0a 2a 2a 20 69 73 20 61 6e 20 75 70 imit.** is an up
40eb0 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 per bound on the
40ec0 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c total memory al
40ed0 6c 6f 63 61 74 69 6f 6e 20 66 6f 72 20 61 6c 6c location for all
40ee0 20 74 68 72 65 61 64 73 2e 20 49 6e 0a 2a 2a 20 threads. In.**
40ef0 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 20 74 68 version 3.5.0 th
40f00 65 72 65 20 69 73 20 6e 6f 20 6d 65 63 68 61 6e ere is no mechan
40f10 69 73 6d 20 66 6f 72 20 6c 69 6d 69 74 69 6e 67 ism for limiting
40f20 20 74 68 65 20 68 65 61 70 20 75 73 61 67 65 20 the heap usage
40f30 66 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 for.** individua
40f40 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a l threads..**.**
40f50 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
40f60 2a 2a 20 7b 48 31 36 33 35 31 7d 20 54 68 65 20 ** {H16351} The
40f70 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 [sqlite3_soft_he
40f80 61 70 5f 6c 69 6d 69 74 28 4e 29 5d 20 69 6e 74 ap_limit(N)] int
40f90 65 72 66 61 63 65 20 70 6c 61 63 65 73 20 61 20 erface places a
40fa0 73 6f 66 74 20 6c 69 6d 69 74 0a 2a 2a 20 20 20 soft limit.**
40fb0 20 20 20 20 20 20 20 6f 66 20 4e 20 62 79 74 65 of N byte
40fc0 73 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 s on the amount
40fd0 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74 of heap memory t
40fe0 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 hat may be alloc
40ff0 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 ated.**
41000 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
41010 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 malloc()] or [sq
41020 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d lite3_realloc()]
41030 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 0a 2a 2a at any point.**
41040 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 69 6d in tim
41050 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 33 35 32 e..**.** {H16352
41060 7d 20 49 66 20 61 20 63 61 6c 6c 20 74 6f 20 5b } If a call to [
41070 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
41080 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 ] or [sqlite3_re
41090 61 6c 6c 6f 63 28 29 5d 20 77 6f 75 6c 64 0a 2a alloc()] would.*
410a0 2a 20 20 20 20 20 20 20 20 20 20 63 61 75 73 65 * cause
410b0 20 74 68 65 20 74 6f 74 61 6c 20 61 6d 6f 75 6e the total amoun
410c0 74 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d t of allocated m
410d0 65 6d 6f 72 79 20 74 6f 20 65 78 63 65 65 64 20 emory to exceed
410e0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
410f0 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 2c soft heap limit,
41100 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 72 then [sqlite3_r
41110 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d elease_memory()]
41120 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 20 is invoked.**
41130 20 20 20 20 20 20 20 20 69 6e 20 61 6e 20 61 74 in an at
41140 74 65 6d 70 74 20 74 6f 20 72 65 64 75 63 65 20 tempt to reduce
41150 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 the memory usage
41160 20 70 72 69 6f 72 20 74 6f 20 70 72 6f 63 65 65 prior to procee
41170 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 ding.**
41180 20 77 69 74 68 20 74 68 65 20 6d 65 6d 6f 72 79 with the memory
41190 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 allocation atte
411a0 6d 70 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 33 mpt..**.** {H163
411b0 35 33 7d 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 53} Calls to [sq
411c0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 lite3_malloc()]
411d0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c or [sqlite3_real
411e0 6c 6f 63 28 29 5d 20 74 68 61 74 20 74 72 69 67 loc()] that trig
411f0 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ger.**
41200 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 64 75 attempts to redu
41210 63 65 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 ce memory usage
41220 74 68 72 6f 75 67 68 20 74 68 65 20 73 6f 66 74 through the soft
41230 20 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 20 heap limit.**
41240 20 20 20 20 20 20 20 20 6d 65 63 68 61 6e 69 73 mechanis
41250 6d 20 63 6f 6e 74 69 6e 75 65 20 65 76 65 6e 20 m continue even
41260 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 if the attempt t
41270 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 0a o reduce memory.
41280 2a 2a 20 20 20 20 20 20 20 20 20 20 75 73 61 67 ** usag
41290 65 20 69 73 20 75 6e 73 75 63 63 65 73 73 66 75 e is unsuccessfu
412a0 6c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 33 35 34 l..**.** {H16354
412b0 7d 20 41 20 6e 65 67 61 74 69 76 65 20 6f 72 20 } A negative or
412c0 7a 65 72 6f 20 76 61 6c 75 65 20 66 6f 72 20 4e zero value for N
412d0 20 69 6e 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a in a call to.**
412e0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
412f0 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d e3_soft_heap_lim
41300 69 74 28 4e 29 5d 20 6d 65 61 6e 73 20 74 68 61 it(N)] means tha
41310 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f t there is no so
41320 66 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 68 ft.** h
41330 65 61 70 20 6c 69 6d 69 74 20 61 6e 64 20 5b 73 eap limit and [s
41340 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
41350 65 6d 6f 72 79 28 29 5d 20 77 69 6c 6c 20 6f 6e emory()] will on
41360 6c 79 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 ly be.**
41370 20 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 6d 65 called when me
41380 6d 6f 72 79 20 69 73 20 63 6f 6d 70 6c 65 74 65 mory is complete
41390 6c 79 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a ly exhausted..**
413a0 0a 2a 2a 20 7b 48 31 36 33 35 35 7d 20 54 68 65 .** {H16355} The
413b0 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 default value f
413c0 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 or the soft heap
413d0 20 6c 69 6d 69 74 20 69 73 20 7a 65 72 6f 2e 0a limit is zero..
413e0 2a 2a 0a 2a 2a 20 7b 48 31 36 33 35 38 7d 20 45 **.** {H16358} E
413f0 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c ach call to [sql
41400 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c ite3_soft_heap_l
41410 69 6d 69 74 28 4e 29 5d 20 6f 76 65 72 72 69 64 imit(N)] overrid
41420 65 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 es the.**
41430 20 20 20 76 61 6c 75 65 73 20 73 65 74 20 62 79 values set by
41440 20 61 6c 6c 20 70 72 69 6f 72 20 63 61 6c 6c 73 all prior calls
41450 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
41460 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66 void sqlite3_sof
41470 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74 t_heap_limit(int
41480 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
41490 45 46 3a 20 45 78 74 72 61 63 74 20 4d 65 74 61 EF: Extract Meta
414a0 64 61 74 61 20 41 62 6f 75 74 20 41 20 43 6f 6c data About A Col
414b0 75 6d 6e 20 4f 66 20 41 20 54 61 62 6c 65 20 7b umn Of A Table {
414c0 48 31 32 38 35 30 7d 20 3c 53 36 30 33 30 30 3e H12850} <S60300>
414d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
414e0 69 6e 65 20 72 65 74 75 72 6e 73 20 6d 65 74 61 ine returns meta
414f0 64 61 74 61 20 61 62 6f 75 74 20 61 20 73 70 65 data about a spe
41500 63 69 66 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 cific column of
41510 61 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 64 61 a specific.** da
41520 74 61 62 61 73 65 20 74 61 62 6c 65 20 61 63 63 tabase table acc
41530 65 73 73 69 62 6c 65 20 75 73 69 6e 67 20 74 68 essible using th
41540 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
41550 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a ection] handle.*
41560 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 * passed as the
41570 66 69 72 73 74 20 66 75 6e 63 74 69 6f 6e 20 61 first function a
41580 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 rgument..**.** T
41590 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 64 65 he column is ide
415a0 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73 ntified by the s
415b0 65 63 6f 6e 64 2c 20 74 68 69 72 64 20 61 6e 64 econd, third and
415c0 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 fourth paramete
415d0 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 66 75 rs to.** this fu
415e0 6e 63 74 69 6f 6e 2e 20 54 68 65 20 73 65 63 6f nction. The seco
415f0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 nd parameter is
41600 65 69 74 68 65 72 20 74 68 65 20 6e 61 6d 65 20 either the name
41610 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a of the database.
41620 2a 2a 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c ** (i.e. "main",
41630 20 22 74 65 6d 70 22 20 6f 72 20 61 6e 20 61 74 "temp" or an at
41640 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 29 tached database)
41650 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
41660 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 74 61 62 specified.** tab
41670 6c 65 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 69 le or NULL. If i
41680 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 t is NULL, then
41690 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 all attached dat
416a0 61 62 61 73 65 73 20 61 72 65 20 73 65 61 72 63 abases are searc
416b0 68 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 hed.** for the t
416c0 61 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 73 able using the s
416d0 61 6d 65 20 61 6c 67 6f 72 69 74 68 6d 20 75 73 ame algorithm us
416e0 65 64 20 62 79 20 74 68 65 20 64 61 74 61 62 61 ed by the databa
416f0 73 65 20 65 6e 67 69 6e 65 20 74 6f 0a 2a 2a 20 se engine to.**
41700 72 65 73 6f 6c 76 65 20 75 6e 71 75 61 6c 69 66 resolve unqualif
41710 69 65 64 20 74 61 62 6c 65 20 72 65 66 65 72 65 ied table refere
41720 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 nces..**.** The
41730 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 third and fourth
41740 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 parameters to t
41750 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 his function are
41760 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 63 the table and c
41770 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 20 6f 66 olumn.** name of
41780 20 74 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c the desired col
41790 75 6d 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c umn, respectivel
417a0 79 2e 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 y. Neither of th
417b0 65 73 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a ese parameters.*
417c0 2a 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a * may be NULL..*
417d0 2a 0a 2a 2a 20 4d 65 74 61 64 61 74 61 20 69 73 *.** Metadata is
417e0 20 72 65 74 75 72 6e 65 64 20 62 79 20 77 72 69 returned by wri
417f0 74 69 6e 67 20 74 6f 20 74 68 65 20 6d 65 6d 6f ting to the memo
41800 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 70 61 73 ry locations pas
41810 73 65 64 20 61 73 20 74 68 65 20 35 74 68 0a 2a sed as the 5th.*
41820 2a 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 * and subsequent
41830 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 parameters to t
41840 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 41 6e his function. An
41850 79 20 6f 66 20 74 68 65 73 65 20 61 72 67 75 6d y of these argum
41860 65 6e 74 73 20 6d 61 79 20 62 65 0a 2a 2a 20 4e ents may be.** N
41870 55 4c 4c 2c 20 69 6e 20 77 68 69 63 68 20 63 61 ULL, in which ca
41880 73 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e se the correspon
41890 64 69 6e 67 20 65 6c 65 6d 65 6e 74 20 6f 66 20 ding element of
418a0 6d 65 74 61 64 61 74 61 20 69 73 20 6f 6d 69 74 metadata is omit
418b0 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 ted..**.** <bloc
418c0 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c kquote>.** <tabl
418d0 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a e border="1">.**
418e0 20 3c 74 72 3e 3c 74 68 3e 20 50 61 72 61 6d 65 <tr><th> Parame
418f0 74 65 72 20 3c 74 68 3e 20 4f 75 74 70 75 74 3c ter <th> Output<
41900 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 44 65 br>Type <th> De
41910 73 63 72 69 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 scription.**.**
41920 3c 74 72 3e 3c 74 64 3e 20 35 74 68 20 3c 74 64 <tr><td> 5th <td
41930 3e 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 > const char* <t
41940 64 3e 20 44 61 74 61 20 74 79 70 65 0a 2a 2a 20 d> Data type.**
41950 3c 74 72 3e 3c 74 64 3e 20 36 74 68 20 3c 74 64 <tr><td> 6th <td
41960 3e 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 > const char* <t
41970 64 3e 20 4e 61 6d 65 20 6f 66 20 64 65 66 61 75 d> Name of defau
41980 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 lt collation seq
41990 75 65 6e 63 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 uence.** <tr><td
419a0 3e 20 37 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 > 7th <td> int
419b0 20 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 <td> True
419c0 20 69 66 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 if column has a
419d0 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 NOT NULL constr
419e0 61 69 6e 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e aint.** <tr><td>
419f0 20 38 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 8th <td> int
41a00 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 <td> True
41a10 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 if column is par
41a20 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 t of the PRIMARY
41a30 20 4b 45 59 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e KEY.** <tr><td>
41a40 20 39 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 9th <td> int
41a50 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 <td> True
41a60 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 if column is [AU
41a70 54 4f 49 4e 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 TOINCREMENT].**
41a80 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c </table>.** </bl
41a90 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 ockquote>.**.**
41aa0 54 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 The memory point
41ab0 65 64 20 74 6f 20 62 79 20 74 68 65 20 63 68 61 ed to by the cha
41ac0 72 61 63 74 65 72 20 70 6f 69 6e 74 65 72 73 20 racter pointers
41ad0 72 65 74 75 72 6e 65 64 20 66 6f 72 20 74 68 65 returned for the
41ae0 0a 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 .** declaration
41af0 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69 type and collati
41b00 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 76 on sequence is v
41b10 61 6c 69 64 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 alid only until
41b20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 63 61 6c 6c the next.** call
41b30 20 74 6f 20 61 6e 79 20 53 51 4c 69 74 65 20 41 to any SQLite A
41b40 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a PI function..**.
41b50 2a 2a 20 49 66 20 74 68 65 20 73 70 65 63 69 66 ** If the specif
41b60 69 65 64 20 74 61 62 6c 65 20 69 73 20 61 63 74 ied table is act
41b70 75 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 61 6e ually a view, an
41b80 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 [error code] is
41b90 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
41ba0 20 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65 If the specifie
41bb0 64 20 63 6f 6c 75 6d 6e 20 69 73 20 22 72 6f 77 d column is "row
41bc0 69 64 22 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f id", "oid" or "_
41bd0 72 6f 77 69 64 5f 22 20 61 6e 64 20 61 6e 0a 2a rowid_" and an.*
41be0 2a 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 * [INTEGER PRIMA
41bf0 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 20 68 RY KEY] column h
41c00 61 73 20 62 65 65 6e 20 65 78 70 6c 69 63 69 74 as been explicit
41c10 6c 79 20 64 65 63 6c 61 72 65 64 2c 20 74 68 65 ly declared, the
41c20 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 n the output.**
41c30 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 73 parameters are s
41c40 65 74 20 66 6f 72 20 74 68 65 20 65 78 70 6c 69 et for the expli
41c50 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 citly declared c
41c60 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 72 65 20 olumn. If there
41c70 69 73 20 6e 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 is no.** explici
41c80 74 6c 79 20 64 65 63 6c 61 72 65 64 20 5b 49 4e tly declared [IN
41c90 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
41ca0 59 5d 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 Y] column, then
41cb0 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 the output.** pa
41cc0 72 61 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 rameters are set
41cd0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
41ce0 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 ** <pre>.**
41cf0 64 61 74 61 20 74 79 70 65 3a 20 22 49 4e 54 45 data type: "INTE
41d00 47 45 52 22 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c GER".** coll
41d10 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 ation sequence:
41d20 22 42 49 4e 41 52 59 22 0a 2a 2a 20 20 20 20 20 "BINARY".**
41d30 6e 6f 74 20 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20 not null: 0.**
41d40 20 20 20 70 72 69 6d 61 72 79 20 6b 65 79 3a 20 primary key:
41d50 31 0a 2a 2a 20 20 20 20 20 61 75 74 6f 20 69 6e 1.** auto in
41d60 63 72 65 6d 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f crement: 0.** </
41d70 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 pre>.**.** This
41d80 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6c 6f 61 function may loa
41d90 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 63 d one or more sc
41da0 68 65 6d 61 73 20 66 72 6f 6d 20 64 61 74 61 62 hemas from datab
41db0 61 73 65 20 66 69 6c 65 73 2e 20 49 66 20 61 6e ase files. If an
41dc0 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 .** error occurs
41dd0 20 64 75 72 69 6e 67 20 74 68 69 73 20 70 72 6f during this pro
41de0 63 65 73 73 2c 20 6f 72 20 69 66 20 74 68 65 20 cess, or if the
41df0 72 65 71 75 65 73 74 65 64 20 74 61 62 6c 65 20 requested table
41e00 6f 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e or column.** can
41e10 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 61 6e not be found, an
41e20 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 [error code] is
41e30 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e returned and an
41e40 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6c error message l
41e50 65 66 74 0a 2a 2a 20 69 6e 20 74 68 65 20 5b 64 eft.** in the [d
41e60 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
41e70 6f 6e 5d 20 28 74 6f 20 62 65 20 72 65 74 72 69 on] (to be retri
41e80 65 76 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 eved using sqlit
41e90 65 33 5f 65 72 72 6d 73 67 28 29 29 2e 0a 2a 2a e3_errmsg())..**
41ea0 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 .** This API is
41eb0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
41ec0 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 f the library wa
41ed0 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 s compiled with
41ee0 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 the.** [SQLITE_E
41ef0 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 NABLE_COLUMN_MET
41f00 41 44 41 54 41 5d 20 43 2d 70 72 65 70 72 6f 63 ADATA] C-preproc
41f10 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 essor symbol def
41f20 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ined..*/.SQLITE_
41f30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
41f40 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 table_column_met
41f50 61 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65 33 adata(. sqlite3
41f60 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
41f70 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 /* Connecti
41f80 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 on handle */. c
41f90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 onst char *zDbNa
41fa0 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 61 me, /* Da
41fb0 74 61 62 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e tabase name or N
41fc0 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ULL */. const c
41fd0 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c har *zTableName,
41fe0 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 /* Table na
41ff0 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 me */. const ch
42000 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c ar *zColumnName,
42010 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 /* Column na
42020 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e me */. char con
42030 73 74 20 2a 2a 70 7a 44 61 74 61 54 79 70 65 2c st **pzDataType,
42040 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 44 /* OUTPUT: D
42050 65 63 6c 61 72 65 64 20 64 61 74 61 20 74 79 70 eclared data typ
42060 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 e */. char cons
42070 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 t **pzCollSeq,
42080 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 43 6f /* OUTPUT: Co
42090 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
420a0 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 2a name */. int *
420b0 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 20 20 20 20 pNotNull,
420c0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 /* OUTPUT
420d0 3a 20 54 72 75 65 20 69 66 20 4e 4f 54 20 4e 55 : True if NOT NU
420e0 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 LL constraint ex
420f0 69 73 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 ists */. int *p
42100 50 72 69 6d 61 72 79 4b 65 79 2c 20 20 20 20 20 PrimaryKey,
42110 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a /* OUTPUT:
42120 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 True if column
42130 70 61 72 74 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 part of PK */.
42140 69 6e 74 20 2a 70 41 75 74 6f 69 6e 63 20 20 20 int *pAutoinc
42150 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
42160 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 UTPUT: True if c
42170 6f 6c 75 6d 6e 20 69 73 20 61 75 74 6f 2d 69 6e olumn is auto-in
42180 63 72 65 6d 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f crement */.);../
42190 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c *.** CAPI3REF: L
421a0 6f 61 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e oad An Extension
421b0 20 7b 48 31 32 36 30 30 7d 20 3c 53 32 30 35 30 {H12600} <S2050
421c0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 0>.**.** This in
421d0 74 65 72 66 61 63 65 20 6c 6f 61 64 73 20 61 6e terface loads an
421e0 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f SQLite extensio
421f0 6e 20 6c 69 62 72 61 72 79 20 66 72 6f 6d 20 74 n library from t
42200 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a he named file..*
42210 2a 0a 2a 2a 20 7b 48 31 32 36 30 31 7d 20 54 68 *.** {H12601} Th
42220 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 e sqlite3_load_e
42230 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 xtension() inter
42240 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f face attempts to
42250 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20 20 20 20 20 load an.**
42260 20 20 20 20 20 53 51 4c 69 74 65 20 65 78 74 65 SQLite exte
42270 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 63 6f nsion library co
42280 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 ntained in the f
42290 69 6c 65 20 7a 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a ile zFile..**.**
422a0 20 7b 48 31 32 36 30 32 7d 20 54 68 65 20 65 6e {H12602} The en
422b0 74 72 79 20 70 6f 69 6e 74 20 69 73 20 7a 50 72 try point is zPr
422c0 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 oc..**.** {H1260
422d0 33 7d 20 7a 50 72 6f 63 20 6d 61 79 20 62 65 20 3} zProc may be
422e0 30 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 0, in which case
422f0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
42300 20 65 6e 74 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 entry point.**
42310 20 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 default
42320 73 20 74 6f 20 22 73 71 6c 69 74 65 33 5f 65 78 s to "sqlite3_ex
42330 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a tension_init"..*
42340 2a 0a 2a 2a 20 7b 48 31 32 36 30 34 7d 20 54 68 *.** {H12604} Th
42350 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 e sqlite3_load_e
42360 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 xtension() inter
42370 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74 75 72 face shall retur
42380 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 n.** [S
42390 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 QLITE_OK] on suc
423a0 63 65 73 73 20 61 6e 64 20 5b 53 51 4c 49 54 45 cess and [SQLITE
423b0 5f 45 52 52 4f 52 5d 20 69 66 20 73 6f 6d 65 74 _ERROR] if somet
423c0 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e hing goes wrong.
423d0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 35 7d 20 .**.** {H12605}
423e0 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
423f0 72 73 20 61 6e 64 20 70 7a 45 72 72 4d 73 67 20 rs and pzErrMsg
42400 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74 is not 0, then t
42410 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
42420 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 sqlite3_load_ext
42430 65 6e 73 69 6f 6e 28 29 5d 20 69 6e 74 65 72 66 ension()] interf
42440 61 63 65 20 73 68 61 6c 6c 20 61 74 74 65 6d 70 ace shall attemp
42450 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 t to.**
42460 20 66 69 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20 fill *pzErrMsg
42470 77 69 74 68 20 65 72 72 6f 72 20 6d 65 73 73 61 with error messa
42480 67 65 20 74 65 78 74 20 73 74 6f 72 65 64 20 69 ge text stored i
42490 6e 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 n memory.**
424a0 20 20 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72 obtained fr
424b0 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c om [sqlite3_mall
424c0 6f 63 28 29 5d 2e 20 7b 45 4e 44 7d 20 20 54 68 oc()]. {END} Th
424d0 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
424e0 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 on.** s
424f0 68 6f 75 6c 64 20 66 72 65 65 20 74 68 69 73 20 hould free this
42500 6d 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e memory by callin
42510 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 g [sqlite3_free(
42520 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 )]..**.** {H1260
42530 36 7d 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 6} Extension loa
42540 64 69 6e 67 20 6d 75 73 74 20 62 65 20 65 6e 61 ding must be ena
42550 62 6c 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 bled using.**
42560 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
42570 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 enable_load_exte
42580 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 nsion()] prior t
42590 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 41 o calling this A
425a0 50 49 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 PI,.**
425b0 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 otherwise an err
425c0 6f 72 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 or will be retur
425d0 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ned..*/.SQLITE_A
425e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c PI int sqlite3_l
425f0 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 0a 20 oad_extension(.
42600 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
42610 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 /* Load t
42620 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74 he extension int
42630 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 o this database
42640 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
42650 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
42660 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 e, /* Name of
42670 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
42680 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65 ary containing e
42690 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f xtension */. co
426a0 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c nst char *zProc,
426b0 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69 /* Entry poi
426c0 6e 74 2e 20 20 44 65 72 69 76 65 64 20 66 72 6f nt. Derived fro
426d0 6d 20 7a 46 69 6c 65 20 69 66 20 30 20 2a 2f 0a m zFile if 0 */.
426e0 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 char **pzErrMs
426f0 67 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 65 g /* Put e
42700 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 rror message her
42710 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 3b e if not 0 */.);
42720 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
42730 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 : Enable Or Disa
42740 62 6c 65 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f ble Extension Lo
42750 61 64 69 6e 67 20 7b 48 31 32 36 32 30 7d 20 3c ading {H12620} <
42760 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f S20500>.**.** So
42770 20 61 73 20 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 as not to open
42780 73 65 63 75 72 69 74 79 20 68 6f 6c 65 73 20 69 security holes i
42790 6e 20 6f 6c 64 65 72 20 61 70 70 6c 69 63 61 74 n older applicat
427a0 69 6f 6e 73 20 74 68 61 74 20 61 72 65 0a 2a 2a ions that are.**
427b0 20 75 6e 70 72 65 70 61 72 65 64 20 74 6f 20 64 unprepared to d
427c0 65 61 6c 20 77 69 74 68 20 65 78 74 65 6e 73 69 eal with extensi
427d0 6f 6e 20 6c 6f 61 64 69 6e 67 2c 20 61 6e 64 20 on loading, and
427e0 61 73 20 61 20 6d 65 61 6e 73 20 6f 66 20 64 69 as a means of di
427f0 73 61 62 6c 69 6e 67 0a 2a 2a 20 65 78 74 65 6e sabling.** exten
42800 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 77 68 69 sion loading whi
42810 6c 65 20 65 76 61 6c 75 61 74 69 6e 67 20 75 73 le evaluating us
42820 65 72 2d 65 6e 74 65 72 65 64 20 53 51 4c 2c 20 er-entered SQL,
42830 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 the following AP
42840 49 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 64 I.** is provided
42850 20 74 6f 20 74 75 72 6e 20 74 68 65 20 5b 73 71 to turn the [sq
42860 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e lite3_load_exten
42870 73 69 6f 6e 28 29 5d 20 6d 65 63 68 61 6e 69 73 sion()] mechanis
42880 6d 20 6f 6e 20 61 6e 64 20 6f 66 66 2e 0a 2a 2a m on and off..**
42890 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f .** Extension lo
428a0 61 64 69 6e 67 20 69 73 20 6f 66 66 20 62 79 20 ading is off by
428b0 64 65 66 61 75 6c 74 2e 20 53 65 65 20 74 69 63 default. See tic
428c0 6b 65 74 20 23 31 38 36 33 2e 0a 2a 2a 0a 2a 2a ket #1863..**.**
428d0 20 7b 48 31 32 36 32 31 7d 20 43 61 6c 6c 20 74 {H12621} Call t
428e0 68 65 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c he sqlite3_enabl
428f0 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e e_load_extension
42900 28 29 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 () routine with
42910 6f 6e 6f 66 66 3d 3d 31 0a 2a 2a 20 20 20 20 20 onoff==1.**
42920 20 20 20 20 20 74 6f 20 74 75 72 6e 20 65 78 74 to turn ext
42930 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f ension loading o
42940 6e 20 61 6e 64 20 63 61 6c 6c 20 69 74 20 77 69 n and call it wi
42950 74 68 20 6f 6e 6f 66 66 3d 3d 30 20 74 6f 20 74 th onoff==0 to t
42960 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 urn.**
42970 69 74 20 62 61 63 6b 20 6f 66 66 20 61 67 61 69 it back off agai
42980 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 32 n..**.** {H12622
42990 7d 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 } Extension load
429a0 69 6e 67 20 69 73 20 6f 66 66 20 62 79 20 64 65 ing is off by de
429b0 66 61 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fault..*/.SQLITE
429c0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
429d0 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 _enable_load_ext
429e0 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a ension(sqlite3 *
429f0 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a db, int onoff);.
42a00 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
42a10 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 4c Automatically L
42a20 6f 61 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e oad An Extension
42a30 73 20 7b 48 31 32 36 34 30 7d 20 3c 53 32 30 35 s {H12640} <S205
42a40 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 00>.**.** This A
42a50 50 49 20 63 61 6e 20 62 65 20 69 6e 76 6f 6b 65 PI can be invoke
42a60 64 20 61 74 20 70 72 6f 67 72 61 6d 20 73 74 61 d at program sta
42a70 72 74 75 70 20 69 6e 20 6f 72 64 65 72 20 74 6f rtup in order to
42a80 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6f 6e 65 register.** one
42a90 20 6f 72 20 6d 6f 72 65 20 73 74 61 74 69 63 61 or more statica
42aa0 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 78 74 65 6e lly linked exten
42ab0 73 69 6f 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 sions that will
42ac0 62 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 be available.**
42ad0 74 6f 20 61 6c 6c 20 6e 65 77 20 5b 64 61 74 61 to all new [data
42ae0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
42af0 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 ]. {END}.**.** T
42b00 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 his routine stor
42b10 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 es a pointer to
42b20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e the extension in
42b30 20 61 6e 20 61 72 72 61 79 20 74 68 61 74 20 69 an array that i
42b40 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 s.** obtained fr
42b50 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c om [sqlite3_mall
42b60 6f 63 28 29 5d 2e 20 20 49 66 20 79 6f 75 20 72 oc()]. If you r
42b70 75 6e 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b un a memory leak
42b80 20 63 68 65 63 6b 65 72 0a 2a 2a 20 6f 6e 20 79 checker.** on y
42b90 6f 75 72 20 70 72 6f 67 72 61 6d 20 61 6e 64 20 our program and
42ba0 69 74 20 72 65 70 6f 72 74 73 20 61 20 6c 65 61 it reports a lea
42bb0 6b 20 62 65 63 61 75 73 65 20 6f 66 20 74 68 69 k because of thi
42bc0 73 20 61 72 72 61 79 2c 20 69 6e 76 6f 6b 65 0a s array, invoke.
42bd0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 ** [sqlite3_rese
42be0 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e t_auto_extension
42bf0 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 73 68 75 ()] prior to shu
42c00 74 64 6f 77 6e 20 74 6f 20 66 72 65 65 20 74 68 tdown to free th
42c10 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 e memory..**.**
42c20 7b 48 31 32 36 34 31 7d 20 54 68 69 73 20 66 75 {H12641} This fu
42c30 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 73 nction registers
42c40 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 20 65 6e an extension en
42c50 74 72 79 20 70 6f 69 6e 74 20 74 68 61 74 20 69 try point that i
42c60 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 s.** au
42c70 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 76 6f tomatically invo
42c80 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 6e ked whenever a n
42c90 65 77 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ew [database con
42ca0 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20 nection].**
42cb0 20 20 20 20 20 69 73 20 6f 70 65 6e 65 64 20 75 is opened u
42cc0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 sing [sqlite3_op
42cd0 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f en()], [sqlite3_
42ce0 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 open16()],.**
42cf0 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 or [sqlit
42d00 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a e3_open_v2()]..*
42d10 2a 0a 2a 2a 20 7b 48 31 32 36 34 32 7d 20 44 75 *.** {H12642} Du
42d20 70 6c 69 63 61 74 65 20 65 78 74 65 6e 73 69 6f plicate extensio
42d30 6e 73 20 61 72 65 20 64 65 74 65 63 74 65 64 20 ns are detected
42d40 73 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 so calling this
42d50 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 20 20 20 20 routine.**
42d60 20 20 20 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d multiple tim
42d70 65 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 es with the same
42d80 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 68 61 extension is ha
42d90 72 6d 6c 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 rmless..**.** {H
42da0 31 32 36 34 33 7d 20 54 68 69 73 20 72 6f 75 74 12643} This rout
42db0 69 6e 65 20 73 74 6f 72 65 73 20 61 20 70 6f 69 ine stores a poi
42dc0 6e 74 65 72 20 74 6f 20 74 68 65 20 65 78 74 65 nter to the exte
42dd0 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 nsion in an arra
42de0 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 y.** th
42df0 61 74 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 at is obtained f
42e00 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c rom [sqlite3_mal
42e10 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 loc()]..**.** {H
42e20 31 32 36 34 34 7d 20 41 75 74 6f 6d 61 74 69 63 12644} Automatic
42e30 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 70 70 6c extensions appl
42e40 79 20 61 63 72 6f 73 73 20 61 6c 6c 20 74 68 72 y across all thr
42e50 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eads..*/.SQLITE_
42e60 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
42e70 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 auto_extension(v
42e80 6f 69 64 20 28 2a 78 45 6e 74 72 79 50 6f 69 6e oid (*xEntryPoin
42e90 74 29 28 76 6f 69 64 29 29 3b 0a 0a 2f 2a 0a 2a t)(void));../*.*
42ea0 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 * CAPI3REF: Rese
42eb0 74 20 41 75 74 6f 6d 61 74 69 63 20 45 78 74 65 t Automatic Exte
42ec0 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 nsion Loading {H
42ed0 31 32 36 36 30 7d 20 3c 53 32 30 35 30 30 3e 0a 12660} <S20500>.
42ee0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
42ef0 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 6c 6c ion disables all
42f00 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 previously regi
42f10 73 74 65 72 65 64 20 61 75 74 6f 6d 61 74 69 63 stered automatic
42f20 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 .** extensions.
42f30 7b 45 4e 44 7d 20 20 49 74 20 75 6e 64 6f 65 73 {END} It undoes
42f40 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 61 the effect of a
42f50 6c 6c 20 70 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c ll prior.** [sql
42f60 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 ite3_auto_extens
42f70 69 6f 6e 28 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a ion()] calls..**
42f80 0a 2a 2a 20 7b 48 31 32 36 36 31 7d 20 54 68 69 .** {H12661} Thi
42f90 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 s function disab
42fa0 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 les all previous
42fb0 6c 79 20 72 65 67 69 73 74 65 72 65 64 0a 2a 2a ly registered.**
42fc0 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 automa
42fd0 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a tic extensions..
42fe0 2a 2a 0a 2a 2a 20 7b 48 31 32 36 36 32 7d 20 54 **.** {H12662} T
42ff0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 his function dis
43000 61 62 6c 65 73 20 61 75 74 6f 6d 61 74 69 63 20 ables automatic
43010 65 78 74 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c extensions in al
43020 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 l threads..*/.SQ
43030 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
43040 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f lite3_reset_auto
43050 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 _extension(void)
43060 3b 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 ;../*.****** EXP
43070 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73 75 62 6a ERIMENTAL - subj
43080 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 ect to change wi
43090 74 68 6f 75 74 20 6e 6f 74 69 63 65 20 2a 2a 2a thout notice ***
430a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
430b0 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 * The interface
430c0 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 to the virtual-t
430d0 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 69 able mechanism i
430e0 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 s currently cons
430f0 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 idered.** to be
43100 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54 experimental. T
43110 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 he interface mig
43120 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69 6e 63 ht change in inc
43130 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73 2e 0a ompatible ways..
43140 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 ** If this is a
43150 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c problem for you,
43160 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20 do not use the
43170 69 6e 74 65 72 66 61 63 65 20 61 74 20 74 68 69 interface at thi
43180 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 s time..**.** Wh
43190 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 en the virtual-t
431a0 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 73 able mechanism s
431b0 74 61 62 69 6c 69 7a 65 73 2c 20 77 65 20 77 69 tabilizes, we wi
431c0 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 65 0a 2a ll declare the.*
431d0 2a 20 69 6e 74 65 72 66 61 63 65 20 66 69 78 65 * interface fixe
431e0 64 2c 20 73 75 70 70 6f 72 74 20 69 74 20 69 6e d, support it in
431f0 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 definitely, and
43200 72 65 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d remove this comm
43210 65 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 ent..*/../*.** S
43220 74 72 75 63 74 75 72 65 73 20 75 73 65 64 20 62 tructures used b
43230 79 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 y the virtual ta
43240 62 6c 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2f ble interface.*/
43250 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
43260 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 71 6c sqlite3_vtab sql
43270 69 74 65 33 5f 76 74 61 62 3b 0a 74 79 70 65 64 ite3_vtab;.typed
43280 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
43290 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c 3_index_info sql
432a0 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b ite3_index_info;
432b0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
432c0 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
432d0 73 6f 72 20 73 71 6c 69 74 65 33 5f 76 74 61 62 sor sqlite3_vtab
432e0 5f 63 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 _cursor;.typedef
432f0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
43300 6d 6f 64 75 6c 65 20 73 71 6c 69 74 65 33 5f 6d module sqlite3_m
43310 6f 64 75 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 odule;../*.** CA
43320 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 PI3REF: Virtual
43330 54 61 62 6c 65 20 4f 62 6a 65 63 74 20 7b 48 31 Table Object {H1
43340 38 30 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 8000} <S20400>.*
43350 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 * KEYWORDS: sqli
43360 74 65 33 5f 6d 6f 64 75 6c 65 0a 2a 2a 20 45 58 te3_module.** EX
43370 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
43380 20 41 20 6d 6f 64 75 6c 65 20 69 73 20 61 20 63 A module is a c
43390 6c 61 73 73 20 6f 66 20 76 69 72 74 75 61 6c 20 lass of virtual
433a0 74 61 62 6c 65 73 2e 20 20 45 61 63 68 20 6d 6f tables. Each mo
433b0 64 75 6c 65 20 69 73 20 64 65 66 69 6e 65 64 0a dule is defined.
433c0 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 ** by an instanc
433d0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
433e0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 ng structure. T
433f0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f his structure co
43400 6e 73 69 73 74 73 0a 2a 2a 20 6d 6f 73 74 6c 79 nsists.** mostly
43410 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 of methods for
43420 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a the module..**.*
43430 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
43440 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c is experimental
43450 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 and is subject
43460 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 to change or.**
43470 72 65 6d 6f 76 61 6c 20 69 6e 20 66 75 74 75 72 removal in futur
43480 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 e releases of SQ
43490 4c 69 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 Lite..*/.struct
434a0 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b sqlite3_module {
434b0 0a 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b . int iVersion;
434c0 0a 20 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 . int (*xCreate
434d0 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 )(sqlite3*, void
434e0 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 *pAux,.
434f0 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c int argc,
43500 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e const char *con
43510 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 st*argv,.
43520 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
43530 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 vtab **ppVTab, c
43540 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a har**);. int (*
43550 78 43 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 xConnect)(sqlite
43560 33 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 3*, void *pAux,.
43570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
43580 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 nt argc, const c
43590 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c har *const*argv,
435a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
435b0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 sqlite3_vtab **p
435c0 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a pVTab, char**);.
435d0 20 20 69 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 int (*xBestInd
435e0 65 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 ex)(sqlite3_vtab
435f0 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 *pVTab, sqlite3
43600 5f 69 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 _index_info*);.
43610 20 69 6e 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 int (*xDisconne
43620 63 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 ct)(sqlite3_vtab
43630 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 *pVTab);. int
43640 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 (*xDestroy)(sqli
43650 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 te3_vtab *pVTab)
43660 3b 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 ;. int (*xOpen)
43670 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
43680 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 VTab, sqlite3_vt
43690 61 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 ab_cursor **ppCu
436a0 72 73 6f 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 rsor);. int (*x
436b0 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 Close)(sqlite3_v
436c0 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 tab_cursor*);.
436d0 69 6e 74 20 28 2a 78 46 69 6c 74 65 72 29 28 73 int (*xFilter)(s
436e0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
436f0 6f 72 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c or*, int idxNum,
43700 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 const char *idx
43710 53 74 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Str,.
43720 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73 int argc, s
43730 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
43740 72 67 76 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e rgv);. int (*xN
43750 65 78 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 ext)(sqlite3_vta
43760 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e b_cursor*);. in
43770 74 20 28 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 t (*xEof)(sqlite
43780 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 3_vtab_cursor*);
43790 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e . int (*xColumn
437a0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 )(sqlite3_vtab_c
437b0 75 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f ursor*, sqlite3_
437c0 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a context*, int);.
437d0 20 20 69 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 int (*xRowid)(
437e0 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
437f0 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e sor*, sqlite3_in
43800 74 36 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 t64 *pRowid);.
43810 69 6e 74 20 28 2a 78 55 70 64 61 74 65 29 28 73 int (*xUpdate)(s
43820 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 qlite3_vtab *, i
43830 6e 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 nt, sqlite3_valu
43840 65 20 2a 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e e **, sqlite3_in
43850 74 36 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a t64 *);. int (*
43860 78 42 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f xBegin)(sqlite3_
43870 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 vtab *pVTab);.
43880 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c int (*xSync)(sql
43890 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 ite3_vtab *pVTab
438a0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d );. int (*xComm
438b0 69 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 it)(sqlite3_vtab
438c0 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 *pVTab);. int
438d0 28 2a 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c (*xRollback)(sql
438e0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 ite3_vtab *pVTab
438f0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 );. int (*xFind
43900 46 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 Function)(sqlite
43910 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 3_vtab *pVtab, i
43920 6e 74 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 nt nArg, const c
43930 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 har *zName,.
43940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43950 20 20 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e void (**pxFun
43960 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 c)(sqlite3_conte
43970 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f xt*,int,sqlite3_
43980 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 value**),.
43990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
439a0 20 76 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a void **ppArg);.
439b0 20 20 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 int (*xRename)
439c0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
439d0 56 74 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 Vtab, const char
439e0 20 2a 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a *zNew);.};../*.
439f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 ** CAPI3REF: Vir
43a00 74 75 61 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 tual Table Index
43a10 69 6e 67 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 ing Information
43a20 7b 48 31 38 31 30 30 7d 20 3c 53 32 30 34 30 30 {H18100} <S20400
43a30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 >.** KEYWORDS: s
43a40 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 qlite3_index_inf
43a50 6f 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 o.** EXPERIMENTA
43a60 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 L.**.** The sqli
43a70 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 te3_index_info s
43a80 74 72 75 63 74 75 72 65 20 61 6e 64 20 69 74 73 tructure and its
43a90 20 73 75 62 73 74 72 75 63 74 75 72 65 73 20 69 substructures i
43aa0 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 s used to.** pas
43ab0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e s information in
43ac0 74 6f 20 61 6e 64 20 72 65 63 65 69 76 65 20 74 to and receive t
43ad0 68 65 20 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 he reply from th
43ae0 65 20 78 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 e xBestIndex.**
43af0 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 73 71 6c method of an sql
43b00 69 74 65 33 5f 6d 6f 64 75 6c 65 2e 20 20 54 68 ite3_module. Th
43b10 65 20 66 69 65 6c 64 73 20 75 6e 64 65 72 20 2a e fields under *
43b20 2a 49 6e 70 75 74 73 2a 2a 20 61 72 65 20 74 68 *Inputs** are th
43b30 65 0a 2a 2a 20 69 6e 70 75 74 73 20 74 6f 20 78 e.** inputs to x
43b40 42 65 73 74 49 6e 64 65 78 20 61 6e 64 20 61 72 BestIndex and ar
43b50 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 e read-only. xB
43b60 65 73 74 49 6e 64 65 78 20 69 6e 73 65 72 74 73 estIndex inserts
43b70 20 69 74 73 0a 2a 2a 20 72 65 73 75 6c 74 73 20 its.** results
43b80 69 6e 74 6f 20 74 68 65 20 2a 2a 4f 75 74 70 75 into the **Outpu
43b90 74 73 2a 2a 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a ts** fields..**.
43ba0 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 ** The aConstrai
43bb0 6e 74 5b 5d 20 61 72 72 61 79 20 72 65 63 6f 72 nt[] array recor
43bc0 64 73 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 ds WHERE clause
43bd0 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66 20 74 constraints of t
43be0 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 3c he form:.**.** <
43bf0 70 72 65 3e 63 6f 6c 75 6d 6e 20 4f 50 20 65 78 pre>column OP ex
43c00 70 72 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 77 pr</pre>.**.** w
43c10 68 65 72 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c here OP is =, &l
43c20 74 3b 2c 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c t;, <=, >,
43c30 20 6f 72 20 26 67 74 3b 3d 2e 20 20 54 68 65 20 or >=. The
43c40 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65 72 61 particular opera
43c50 74 6f 72 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 tor is.** stored
43c60 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b in aConstraint[
43c70 5d 2e 6f 70 2e 20 20 54 68 65 20 69 6e 64 65 78 ].op. The index
43c80 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 of the column i
43c90 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 s stored in.** a
43ca0 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f Constraint[].iCo
43cb0 6c 75 6d 6e 2e 20 20 61 43 6f 6e 73 74 72 61 69 lumn. aConstrai
43cc0 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69 73 20 54 nt[].usable is T
43cd0 52 55 45 20 69 66 20 74 68 65 0a 2a 2a 20 65 78 RUE if the.** ex
43ce0 70 72 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2d pr on the right-
43cf0 68 61 6e 64 20 73 69 64 65 20 63 61 6e 20 62 65 hand side can be
43d00 20 65 76 61 6c 75 61 74 65 64 20 28 61 6e 64 20 evaluated (and
43d10 74 68 75 73 20 74 68 65 20 63 6f 6e 73 74 72 61 thus the constra
43d20 69 6e 74 0a 2a 2a 20 69 73 20 75 73 61 62 6c 65 int.** is usable
43d30 29 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 69 ) and false if i
43d40 74 20 63 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 t cannot..**.**
43d50 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 61 75 The optimizer au
43d60 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 76 65 tomatically inve
43d70 72 74 73 20 74 65 72 6d 73 20 6f 66 20 74 68 65 rts terms of the
43d80 20 66 6f 72 6d 20 22 65 78 70 72 20 4f 50 20 63 form "expr OP c
43d90 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61 olumn".** and ma
43da0 6b 65 73 20 6f 74 68 65 72 20 73 69 6d 70 6c 69 kes other simpli
43db0 66 69 63 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 fications to the
43dc0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e WHERE clause in
43dd0 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a an attempt to.*
43de0 2a 20 67 65 74 20 61 73 20 6d 61 6e 79 20 57 48 * get as many WH
43df0 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 ERE clause terms
43e00 20 69 6e 74 6f 20 74 68 65 20 66 6f 72 6d 20 73 into the form s
43e10 68 6f 77 6e 20 61 62 6f 76 65 20 61 73 20 70 6f hown above as po
43e20 73 73 69 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 61 ssible..** The a
43e30 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 Constraint[] arr
43e40 61 79 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 ay only reports
43e50 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 WHERE clause ter
43e60 6d 73 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 ms in the correc
43e70 74 0a 2a 2a 20 66 6f 72 6d 20 74 68 61 74 20 72 t.** form that r
43e80 65 66 65 72 20 74 6f 20 74 68 65 20 70 61 72 74 efer to the part
43e90 69 63 75 6c 61 72 20 76 69 72 74 75 61 6c 20 74 icular virtual t
43ea0 61 62 6c 65 20 62 65 69 6e 67 20 71 75 65 72 69 able being queri
43eb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d ed..**.** Inform
43ec0 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
43ed0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
43ee0 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 4f 72 is stored in aOr
43ef0 64 65 72 42 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68 derBy[]..** Each
43f00 20 74 65 72 6d 20 6f 66 20 61 4f 72 64 65 72 42 term of aOrderB
43f10 79 20 72 65 63 6f 72 64 73 20 61 20 63 6f 6c 75 y records a colu
43f20 6d 6e 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 mn of the ORDER
43f30 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a BY clause..**.**
43f40 20 54 68 65 20 78 42 65 73 74 49 6e 64 65 78 20 The xBestIndex
43f50 6d 65 74 68 6f 64 20 6d 75 73 74 20 66 69 6c 6c method must fill
43f60 20 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 aConstraintUsag
43f70 65 5b 5d 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 e[] with informa
43f80 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 77 68 tion.** about wh
43f90 61 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f at parameters to
43fa0 20 70 61 73 73 20 74 6f 20 78 46 69 6c 74 65 72 pass to xFilter
43fb0 2e 20 20 49 66 20 61 72 67 76 49 6e 64 65 78 3e . If argvIndex>
43fc0 30 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69 0 then.** the ri
43fd0 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 ght-hand side of
43fe0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
43ff0 6e 67 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d ng aConstraint[]
44000 20 69 73 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a is evaluated.**
44010 20 61 6e 64 20 62 65 63 6f 6d 65 73 20 74 68 65 and becomes the
44020 20 61 72 67 76 49 6e 64 65 78 2d 74 68 20 65 6e argvIndex-th en
44030 74 72 79 20 69 6e 20 61 72 67 76 2e 20 20 49 66 try in argv. If
44040 20 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 aConstraintUsag
44050 65 5b 5d 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 e[].omit.** is t
44060 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f rue, then the co
44070 6e 73 74 72 61 69 6e 74 20 69 73 20 61 73 73 75 nstraint is assu
44080 6d 65 64 20 74 6f 20 62 65 20 66 75 6c 6c 79 20 med to be fully
44090 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a handled by the.*
440a0 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 * virtual table
440b0 61 6e 64 20 69 73 20 6e 6f 74 20 63 68 65 63 6b and is not check
440c0 65 64 20 61 67 61 69 6e 20 62 79 20 53 51 4c 69 ed again by SQLi
440d0 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 te..**.** The id
440e0 78 4e 75 6d 20 61 6e 64 20 69 64 78 50 74 72 20 xNum and idxPtr
440f0 76 61 6c 75 65 73 20 61 72 65 20 72 65 63 6f 72 values are recor
44100 64 65 64 20 61 6e 64 20 70 61 73 73 65 64 20 69 ded and passed i
44110 6e 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 20 nto xFilter..**
44120 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 sqlite3_free() i
44130 73 20 75 73 65 64 20 74 6f 20 66 72 65 65 20 69 s used to free i
44140 64 78 50 74 72 20 69 66 20 6e 65 65 64 54 6f 46 dxPtr if needToF
44150 72 65 65 49 64 78 50 74 72 20 69 73 20 74 72 75 reeIdxPtr is tru
44160 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 64 e..**.** The ord
44170 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 6d 65 61 erByConsumed mea
44180 6e 73 20 74 68 61 74 20 6f 75 74 70 75 74 20 66 ns that output f
44190 72 6f 6d 20 78 46 69 6c 74 65 72 20 77 69 6c 6c rom xFilter will
441a0 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65 occur in.** the
441b0 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20 74 correct order t
441c0 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52 o satisfy the OR
441d0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73 6f DER BY clause so
441e0 20 74 68 61 74 20 6e 6f 20 73 65 70 61 72 61 74 that no separat
441f0 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74 65 e.** sorting ste
44200 70 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a p is required..*
44210 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74 *.** The estimat
44220 65 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73 20 edCost value is
44230 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 an estimate of t
44240 68 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67 he cost of doing
44250 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c the.** particul
44260 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75 ar lookup. A fu
44270 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61 62 ll scan of a tab
44280 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69 65 le with N entrie
44290 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a s should have.**
442a0 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41 a cost of N. A
442b0 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20 6f binary search o
442c0 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20 65 f a table of N e
442d0 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 ntries should ha
442e0 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 ve a.** cost of
442f0 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f approximately lo
44300 67 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 g(N)..**.** This
44310 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 interface is ex
44320 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 perimental and i
44330 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 s subject to cha
44340 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 nge or.** remova
44350 6c 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 l in future rele
44360 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a ases of SQLite..
44370 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
44380 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 7b 0a 20 3_index_info {.
44390 20 2f 2a 20 49 6e 70 75 74 73 20 2a 2f 0a 20 20 /* Inputs */.
443a0 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b int nConstraint;
443b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
443c0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
443d0 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a in aConstraint *
443e0 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 /. struct sqlit
443f0 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 e3_index_constra
44400 69 6e 74 20 7b 0a 20 20 20 20 20 69 6e 74 20 69 int {. int i
44410 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 Column;
44420 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f /* Column o
44430 6e 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 n left-hand side
44440 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a of constraint *
44450 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 /. unsigned
44460 63 68 61 72 20 6f 70 3b 20 20 20 20 20 20 20 20 char op;
44470 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 6f /* Constraint o
44480 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 perator */.
44490 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 73 unsigned char us
444a0 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 able; /* Tru
444b0 65 20 69 66 20 74 68 69 73 20 63 6f 6e 73 74 72 e if this constr
444c0 61 69 6e 74 20 69 73 20 75 73 61 62 6c 65 20 2a aint is usable *
444d0 2f 0a 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d /. int iTerm
444e0 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
444f0 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e 61 /* Used interna
44500 6c 6c 79 20 2d 20 78 42 65 73 74 49 6e 64 65 78 lly - xBestIndex
44510 20 73 68 6f 75 6c 64 20 69 67 6e 6f 72 65 20 2a should ignore *
44520 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 /. } *aConstrai
44530 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f nt; /
44540 2a 20 54 61 62 6c 65 20 6f 66 20 57 48 45 52 45 * Table of WHERE
44550 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 clause constrai
44560 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 nts */. int nOr
44570 64 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 20 derBy;
44580 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
44590 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 terms in the OR
445a0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f DER BY clause */
445b0 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 . struct sqlite
445c0 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 3_index_orderby
445d0 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 {. int iColu
445e0 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 mn;
445f0 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 /* Column numbe
44600 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e r */. unsign
44610 65 64 20 63 68 61 72 20 64 65 73 63 3b 20 20 20 ed char desc;
44620 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 /* True for
44630 44 45 53 43 2e 20 20 46 61 6c 73 65 20 66 6f 72 DESC. False for
44640 20 41 53 43 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f ASC. */. } *aO
44650 72 64 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 rderBy;
44660 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 /* The ORD
44670 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a ER BY clause */.
44680 20 20 2f 2a 20 4f 75 74 70 75 74 73 20 2a 2f 0a /* Outputs */.
44690 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 struct sqlite3
446a0 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e _index_constrain
446b0 74 5f 75 73 61 67 65 20 7b 0a 20 20 20 20 69 6e t_usage {. in
446c0 74 20 61 72 67 76 49 6e 64 65 78 3b 20 20 20 20 t argvIndex;
446d0 20 20 20 20 20 20 20 2f 2a 20 69 66 20 3e 30 2c /* if >0,
446e0 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 70 constraint is p
446f0 61 72 74 20 6f 66 20 61 72 67 76 20 74 6f 20 78 art of argv to x
44700 46 69 6c 74 65 72 20 2a 2f 0a 20 20 20 20 75 6e Filter */. un
44710 73 69 67 6e 65 64 20 63 68 61 72 20 6f 6d 69 74 signed char omit
44720 3b 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 ; /* Do not
44730 20 63 6f 64 65 20 61 20 74 65 73 74 20 66 6f 72 code a test for
44740 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 this constraint
44750 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72 */. } *aConstr
44760 61 69 6e 74 55 73 61 67 65 3b 0a 20 20 69 6e 74 aintUsage;. int
44770 20 69 64 78 4e 75 6d 3b 20 20 20 20 20 20 20 20 idxNum;
44780 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
44790 72 20 75 73 65 64 20 74 6f 20 69 64 65 6e 74 69 r used to identi
447a0 66 79 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a fy the index */.
447b0 20 20 63 68 61 72 20 2a 69 64 78 53 74 72 3b 20 char *idxStr;
447c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
447d0 53 74 72 69 6e 67 2c 20 70 6f 73 73 69 62 6c 79 String, possibly
447e0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
447f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f qlite3_malloc */
44800 0a 20 20 69 6e 74 20 6e 65 65 64 54 6f 46 72 65 . int needToFre
44810 65 49 64 78 53 74 72 3b 20 20 20 20 20 20 2f 2a eIdxStr; /*
44820 20 46 72 65 65 20 69 64 78 53 74 72 20 75 73 69 Free idxStr usi
44830 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 ng sqlite3_free(
44840 29 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 69 ) if true */. i
44850 6e 74 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d nt orderByConsum
44860 65 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ed; /* Tru
44870 65 20 69 66 20 6f 75 74 70 75 74 20 69 73 20 61 e if output is a
44880 6c 72 65 61 64 79 20 6f 72 64 65 72 65 64 20 2a lready ordered *
44890 2f 0a 20 20 64 6f 75 62 6c 65 20 65 73 74 69 6d /. double estim
448a0 61 74 65 64 43 6f 73 74 3b 20 20 20 20 20 20 2f atedCost; /
448b0 2a 20 45 73 74 69 6d 61 74 65 64 20 63 6f 73 74 * Estimated cost
448c0 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20 69 of using this i
448d0 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 ndex */.};.#defi
448e0 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f ne SQLITE_INDEX_
448f0 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 20 20 CONSTRAINT_EQ
44900 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 2.#define SQLIT
44910 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 E_INDEX_CONSTRAI
44920 4e 54 5f 47 54 20 20 20 20 34 0a 23 64 65 66 69 NT_GT 4.#defi
44930 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f ne SQLITE_INDEX_
44940 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20 20 20 CONSTRAINT_LE
44950 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 8.#define SQLIT
44960 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 E_INDEX_CONSTRAI
44970 4e 54 5f 4c 54 20 20 20 20 31 36 0a 23 64 65 66 NT_LT 16.#def
44980 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 ine SQLITE_INDEX
44990 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20 20 _CONSTRAINT_GE
449a0 20 20 33 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 32.#define SQL
449b0 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 ITE_INDEX_CONSTR
449c0 41 49 4e 54 5f 4d 41 54 43 48 20 36 34 0a 0a 2f AINT_MATCH 64../
449d0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 *.** CAPI3REF: R
449e0 65 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61 egister A Virtua
449f0 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e l Table Implemen
44a00 74 61 74 69 6f 6e 20 7b 48 31 38 32 30 30 7d 20 tation {H18200}
44a10 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 <S20400>.** EXPE
44a20 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
44a30 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
44a40 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 sed to register
44a50 61 20 6e 65 77 20 6d 6f 64 75 6c 65 20 6e 61 6d a new module nam
44a60 65 20 77 69 74 68 20 61 0a 2a 2a 20 5b 64 61 74 e with a.** [dat
44a70 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
44a80 5d 2e 20 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 ]. Module names
44a90 20 6d 75 73 74 20 62 65 20 72 65 67 69 73 74 65 must be registe
44aa0 72 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 red before.** cr
44ab0 65 61 74 69 6e 67 20 6e 65 77 20 76 69 72 74 75 eating new virtu
44ac0 61 6c 20 74 61 62 6c 65 73 20 6f 6e 20 74 68 65 al tables on the
44ad0 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f module, or befo
44ae0 72 65 20 75 73 69 6e 67 0a 2a 2a 20 70 72 65 65 re using.** pree
44af0 78 69 73 74 69 6e 67 20 76 69 72 74 75 61 6c 20 xisting virtual
44b00 74 61 62 6c 65 73 20 6f 66 20 74 68 65 20 6d 6f tables of the mo
44b10 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 dule..**.** This
44b20 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 interface is ex
44b30 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 perimental and i
44b40 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 s subject to cha
44b50 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 nge or.** remova
44b60 6c 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 l in future rele
44b70 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a ases of SQLite..
44b80 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
44b90 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
44ba0 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 L int sqlite3_cr
44bb0 65 61 74 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 eate_module(. s
44bc0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
44bd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c /* SQL
44be0 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ite connection t
44bf0 6f 20 72 65 67 69 73 74 65 72 20 6d 6f 64 75 6c o register modul
44c00 65 20 77 69 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 e with */. cons
44c10 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 t char *zName,
44c20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
44c30 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a f the module */.
44c40 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
44c50 6d 6f 64 75 6c 65 20 2a 2c 20 20 20 20 2f 2a 20 module *, /*
44c60 4d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 Methods for the
44c70 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 module */. void
44c80 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
44c90 20 20 20 20 20 20 20 2f 2a 20 43 6c 69 65 6e 74 /* Client
44ca0 20 64 61 74 61 20 66 6f 72 20 78 43 72 65 61 74 data for xCreat
44cb0 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b e/xConnect */.);
44cc0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
44cd0 3a 20 52 65 67 69 73 74 65 72 20 41 20 56 69 72 : Register A Vir
44ce0 74 75 61 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65 tual Table Imple
44cf0 6d 65 6e 74 61 74 69 6f 6e 20 7b 48 31 38 32 31 mentation {H1821
44d00 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 0} <S20400>.** E
44d10 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
44d20 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
44d30 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 74 s identical to t
44d40 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 he [sqlite3_crea
44d50 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6d 65 74 te_module()] met
44d60 68 6f 64 20 61 62 6f 76 65 2c 0a 2a 2a 20 65 78 hod above,.** ex
44d70 63 65 70 74 20 74 68 61 74 20 69 74 20 61 6c 6c cept that it all
44d80 6f 77 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 ows a destructor
44d90 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 function to be
44da0 73 70 65 63 69 66 69 65 64 2e 20 49 74 20 69 73 specified. It is
44db0 0a 2a 2a 20 65 76 65 6e 20 6d 6f 72 65 20 65 78 .** even more ex
44dc0 70 65 72 69 6d 65 6e 74 61 6c 20 74 68 61 6e 20 perimental than
44dd0 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 the rest of the
44de0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 41 virtual tables A
44df0 50 49 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 PI..*/.SQLITE_AP
44e00 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d I SQLITE_EXPERIM
44e10 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 ENTAL int sqlite
44e20 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 3_create_module_
44e30 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 v2(. sqlite3 *d
44e40 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b,
44e50 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 /* SQLite conne
44e60 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 ction to registe
44e70 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f r module with */
44e80 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
44e90 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a Name, /*
44ea0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 Name of the mod
44eb0 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 ule */. const s
44ec0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 2c qlite3_module *,
44ed0 20 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 /* Methods f
44ee0 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f or the module */
44ef0 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 . void *,
44f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
44f10 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72 Client data for
44f20 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 xCreate/xConnec
44f30 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65 t */. void(*xDe
44f40 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 20 20 20 stroy)(void*)
44f50 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 /* Module dest
44f60 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 ructor function
44f70 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.);../*.** CAP
44f80 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 I3REF: Virtual T
44f90 61 62 6c 65 20 49 6e 73 74 61 6e 63 65 20 4f 62 able Instance Ob
44fa0 6a 65 63 74 20 7b 48 31 38 30 31 30 7d 20 3c 53 ject {H18010} <S
44fb0 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 20400>.** KEYWOR
44fc0 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 DS: sqlite3_vtab
44fd0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
44fe0 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 6d 6f 64 .**.** Every mod
44ff0 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ule implementati
45000 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c 61 on uses a subcla
45010 73 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ss of the follow
45020 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a ing structure.**
45030 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 to describe a p
45040 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e articular instan
45050 63 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 ce of the module
45060 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 . Each subclass
45070 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c will.** be tail
45080 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 ored to the spec
45090 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 ific needs of th
450a0 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 e module impleme
450b0 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 ntation..** The
450c0 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 purpose of this
450d0 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f superclass is to
450e0 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 define certain
450f0 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a fields that are.
45100 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c ** common to all
45110 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
45120 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 tations..**.** V
45130 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 irtual tables me
45140 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e thods can set an
45150 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 error message b
45160 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a y assigning a.**
45170 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 string obtained
45180 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
45190 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 printf()] to zEr
451a0 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f rMsg. The metho
451b0 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 d should.** take
451c0 20 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 care that any p
451d0 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 rior string is f
451e0 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 reed by a call t
451f0 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 o [sqlite3_free(
45200 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 )].** prior to a
45210 73 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 ssigning a new s
45220 74 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 tring to zErrMsg
45230 2e 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72 . After the err
45240 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 or message.** is
45250 20 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f delivered up to
45260 20 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c the client appl
45270 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 ication, the str
45280 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f ing will be auto
45290 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 matically.** fre
452a0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 ed by sqlite3_fr
452b0 65 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 ee() and the zEr
452c0 72 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 rMsg field will
452d0 62 65 20 7a 65 72 6f 65 64 2e 20 20 4e 6f 74 65 be zeroed. Note
452e0 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 .** that sqlite3
452f0 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 _mprintf() and s
45300 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 61 72 qlite3_free() ar
45310 65 20 75 73 65 64 20 6f 6e 20 74 68 65 20 7a 45 e used on the zE
45320 72 72 4d 73 67 20 66 69 65 6c 64 0a 2a 2a 20 73 rrMsg field.** s
45330 69 6e 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 ince virtual tab
45340 6c 65 73 20 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79 les are commonly
45350 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 implemented in
45360 6c 6f 61 64 61 62 6c 65 20 65 78 74 65 6e 73 69 loadable extensi
45370 6f 6e 73 20 77 68 69 63 68 0a 2a 2a 20 64 6f 20 ons which.** do
45380 6e 6f 74 20 68 61 76 65 20 61 63 63 65 73 73 20 not have access
45390 74 6f 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 to sqlite3MPrint
453a0 66 28 29 20 6f 72 20 73 71 6c 69 74 65 33 46 72 f() or sqlite3Fr
453b0 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ee()..**.** This
453c0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 interface is ex
453d0 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 perimental and i
453e0 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 s subject to cha
453f0 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 nge or.** remova
45400 6c 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 l in future rele
45410 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a ases of SQLite..
45420 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
45430 33 5f 76 74 61 62 20 7b 0a 20 20 63 6f 6e 73 74 3_vtab {. const
45440 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
45450 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 *pModule; /* Th
45460 65 20 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 69 e module for thi
45470 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 s virtual table
45480 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 */. int nRef;
45490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
454a0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 69 6e 74 /* Used int
454b0 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 63 68 61 ernally */. cha
454c0 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 r *zErrMsg;
454d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
454e0 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 Error message fr
454f0 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e om sqlite3_mprin
45500 74 66 28 29 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 tf() */. /* Vir
45510 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 tual table imple
45520 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 mentations will
45530 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 typically add ad
45540 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 ditional fields
45550 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.};../*.** CAP
45560 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 I3REF: Virtual T
45570 61 62 6c 65 20 43 75 72 73 6f 72 20 4f 62 6a 65 able Cursor Obje
45580 63 74 20 20 7b 48 31 38 30 32 30 7d 20 3c 53 32 ct {H18020} <S2
45590 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0400>.** KEYWORD
455a0 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f S: sqlite3_vtab_
455b0 63 75 72 73 6f 72 0a 2a 2a 20 45 58 50 45 52 49 cursor.** EXPERI
455c0 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 MENTAL.**.** Eve
455d0 72 79 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d ry module implem
455e0 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 entation uses a
455f0 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 20 subclass of the
45600 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
45610 75 72 65 0a 2a 2a 20 74 6f 20 64 65 73 63 72 69 ure.** to descri
45620 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 be cursors that
45630 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 20 76 point into the v
45640 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 irtual table and
45650 20 61 72 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 are used.** to
45660 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 loop through the
45670 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 virtual table.
45680 20 43 75 72 73 6f 72 73 20 61 72 65 20 63 72 65 Cursors are cre
45690 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a ated using the.*
456a0 2a 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6f * xOpen method o
456b0 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 f the module. E
456c0 61 63 68 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 ach module imple
456d0 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 mentation will d
456e0 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e efine.** the con
456f0 74 65 6e 74 20 6f 66 20 61 20 63 75 72 73 6f 72 tent of a cursor
45700 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 73 75 structure to su
45710 69 74 20 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 it its own needs
45720 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 ..**.** This sup
45730 65 72 63 6c 61 73 73 20 65 78 69 73 74 73 20 69 erclass exists i
45740 6e 20 6f 72 64 65 72 20 74 6f 20 64 65 66 69 6e n order to defin
45750 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 e fields of the
45760 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 cursor that.** a
45770 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c re common to all
45780 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
45790 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 ..**.** This int
457a0 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 69 erface is experi
457b0 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75 mental and is su
457c0 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 bject to change
457d0 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e or.** removal in
457e0 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
457f0 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 of SQLite..*/.s
45800 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 truct sqlite3_vt
45810 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 ab_cursor {. sq
45820 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
45830 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 b; /* Virtu
45840 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 al table of this
45850 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 cursor */. /*
45860 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d Virtual table im
45870 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 plementations wi
45880 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 ll typically add
45890 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c additional fiel
458a0 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ds */.};../*.**
458b0 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 CAPI3REF: Declar
458c0 65 20 54 68 65 20 53 63 68 65 6d 61 20 4f 66 20 e The Schema Of
458d0 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 A Virtual Table
458e0 7b 48 31 38 32 38 30 7d 20 3c 53 32 30 34 30 30 {H18280} <S20400
458f0 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
45900 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 L.**.** The xCre
45910 61 74 65 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 ate and xConnect
45920 20 6d 65 74 68 6f 64 73 20 6f 66 20 61 20 6d 6f methods of a mo
45930 64 75 6c 65 20 75 73 65 20 74 68 65 20 66 6f 6c dule use the fol
45940 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 74 6f lowing API.** to
45950 20 64 65 63 6c 61 72 65 20 74 68 65 20 66 6f 72 declare the for
45960 6d 61 74 20 28 74 68 65 20 6e 61 6d 65 73 20 61 mat (the names a
45970 6e 64 20 64 61 74 61 74 79 70 65 73 20 6f 66 20 nd datatypes of
45980 74 68 65 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a the columns) of.
45990 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 ** the virtual t
459a0 61 62 6c 65 73 20 74 68 65 79 20 69 6d 70 6c 65 ables they imple
459b0 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ment..**.** This
459c0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 interface is ex
459d0 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 perimental and i
459e0 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 s subject to cha
459f0 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 nge or.** remova
45a00 6c 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 l in future rele
45a10 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a ases of SQLite..
45a20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
45a30 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
45a40 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 L int sqlite3_de
45a50 63 6c 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 clare_vtab(sqlit
45a60 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 e3*, const char
45a70 2a 7a 43 72 65 61 74 65 54 61 62 6c 65 29 3b 0a *zCreateTable);.
45a80 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
45a90 20 4f 76 65 72 6c 6f 61 64 20 41 20 46 75 6e 63 Overload A Func
45aa0 74 69 6f 6e 20 46 6f 72 20 41 20 56 69 72 74 75 tion For A Virtu
45ab0 61 6c 20 54 61 62 6c 65 20 7b 48 31 38 33 30 30 al Table {H18300
45ac0 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 } <S20400>.** EX
45ad0 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
45ae0 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 Virtual tables
45af0 63 61 6e 20 70 72 6f 76 69 64 65 20 61 6c 74 65 can provide alte
45b00 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e rnative implemen
45b10 74 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e 63 74 tations of funct
45b20 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e 67 20 74 68 ions.** using th
45b30 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 e xFindFunction
45b40 6d 65 74 68 6f 64 2e 20 20 42 75 74 20 67 6c 6f method. But glo
45b50 62 61 6c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 bal versions of
45b60 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 0a those functions.
45b70 2a 2a 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e ** must exist in
45b80 20 6f 72 64 65 72 20 74 6f 20 62 65 20 6f 76 65 order to be ove
45b90 72 6c 6f 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 rloaded..**.** T
45ba0 68 69 73 20 41 50 49 20 6d 61 6b 65 73 20 73 75 his API makes su
45bb0 72 65 20 61 20 67 6c 6f 62 61 6c 20 76 65 72 73 re a global vers
45bc0 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f ion of a functio
45bd0 6e 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 n with a particu
45be0 6c 61 72 0a 2a 2a 20 6e 61 6d 65 20 61 6e 64 20 lar.** name and
45bf0 6e 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 number of parame
45c00 74 65 72 73 20 65 78 69 73 74 73 2e 20 20 49 66 ters exists. If
45c10 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f no such functio
45c20 6e 20 65 78 69 73 74 73 0a 2a 2a 20 62 65 66 6f n exists.** befo
45c30 72 65 20 74 68 69 73 20 41 50 49 20 69 73 20 63 re this API is c
45c40 61 6c 6c 65 64 2c 20 61 20 6e 65 77 20 66 75 6e alled, a new fun
45c50 63 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 ction is created
45c60 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 . The implement
45c70 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 ation.** of the
45c80 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 new function alw
45c90 61 79 73 20 63 61 75 73 65 73 20 61 6e 20 65 78 ays causes an ex
45ca0 63 65 70 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 ception to be th
45cb0 72 6f 77 6e 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 rown. So.** the
45cc0 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 new function is
45cd0 20 6e 6f 74 20 67 6f 6f 64 20 66 6f 72 20 61 6e not good for an
45ce0 79 74 68 69 6e 67 20 62 79 20 69 74 73 65 6c 66 ything by itself
45cf0 2e 20 20 49 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 . Its only.** p
45d00 75 72 70 6f 73 65 20 69 73 20 74 6f 20 62 65 20 urpose is to be
45d10 61 20 70 6c 61 63 65 68 6f 6c 64 65 72 20 66 75 a placeholder fu
45d20 6e 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 nction that can
45d30 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 0a 2a 2a be overloaded.**
45d40 20 62 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c by virtual tabl
45d50 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 es..**.** This A
45d60 50 49 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e PI should be con
45d70 73 69 64 65 72 65 64 20 70 61 72 74 20 6f 66 20 sidered part of
45d80 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c the virtual tabl
45d90 65 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 e interface,.**
45da0 77 68 69 63 68 20 69 73 20 65 78 70 65 72 69 6d which is experim
45db0 65 6e 74 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 ental and subjec
45dc0 74 20 74 6f 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a t to change..*/.
45dd0 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
45de0 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 E_EXPERIMENTAL i
45df0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c nt sqlite3_overl
45e00 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c oad_function(sql
45e10 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 ite3*, const cha
45e20 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e r *zFuncName, in
45e30 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t nArg);../*.**
45e40 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f The interface to
45e50 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 the virtual-tab
45e60 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64 65 66 le mechanism def
45e70 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61 63 6b ined above (back
45e80 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d up.** to a comm
45e90 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79 20 73 ent remarkably s
45ea0 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f imilar to this o
45eb0 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ne) is currently
45ec0 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74 considered.** t
45ed0 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e 74 61 o be experimenta
45ee0 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 l. The interfac
45ef0 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 e might change i
45f00 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 n incompatible w
45f10 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 ays..** If this
45f20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72 is a problem for
45f30 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65 you, do not use
45f40 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 61 the interface a
45f50 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a t this time..**.
45f60 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69 72 74 ** When the virt
45f70 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e ual-table mechan
45f80 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73 2c 20 ism stabilizes,
45f90 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72 65 20 we will declare
45fa0 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 the.** interface
45fb0 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72 74 20 fixed, support
45fc0 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c it indefinitely,
45fd0 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 69 73 and remove this
45fe0 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a comment..**.***
45ff0 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c *** EXPERIMENTAL
46000 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 - subject to ch
46010 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 ange without not
46020 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ice ************
46030 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 **.*/../*.** CAP
46040 49 33 52 45 46 3a 20 41 20 48 61 6e 64 6c 65 20 I3REF: A Handle
46050 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 To An Open BLOB
46060 7b 48 31 37 38 30 30 7d 20 3c 53 33 30 32 33 30 {H17800} <S30230
46070 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
46080 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c BLOB handle} {BL
46090 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a OB handles}.**.*
460a0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
460b0 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 this object rep
460c0 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 resents an open
460d0 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a BLOB on which.**
460e0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
460f0 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 pen | incrementa
46100 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20 l BLOB I/O] can
46110 62 65 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a be performed..**
46120 20 4f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73 Objects of this
46130 20 74 79 70 65 20 61 72 65 20 63 72 65 61 74 65 type are create
46140 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c d by [sqlite3_bl
46150 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e ob_open()].** an
46160 64 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b d destroyed by [
46170 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f sqlite3_blob_clo
46180 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 se()]..** The [s
46190 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 qlite3_blob_read
461a0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
461b0 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 69 _blob_write()] i
461c0 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e nterfaces.** can
461d0 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 64 be used to read
461e0 20 6f 72 20 77 72 69 74 65 20 73 6d 61 6c 6c 20 or write small
461f0 73 75 62 73 65 63 74 69 6f 6e 73 20 6f 66 20 74 subsections of t
46200 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20 he BLOB..** The
46210 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 [sqlite3_blob_by
46220 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 tes()] interface
46230 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a returns the siz
46240 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 69 6e e of the BLOB in
46250 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 bytes..*/.typed
46260 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
46270 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62 3_blob sqlite3_b
46280 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 lob;../*.** CAPI
46290 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f 3REF: Open A BLO
462a0 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61 B For Incrementa
462b0 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c l I/O {H17810} <
462c0 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30230>.**.** Th
462d0 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 6f 70 is interfaces op
462e0 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 ens a [BLOB hand
462f0 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20 le | handle] to
46300 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64 the BLOB located
46310 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c .** in row iRow,
46320 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c column zColumn,
46330 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e table zTable in
46340 20 64 61 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a database zDb;.*
46350 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * in other words
46360 2c 20 74 68 65 20 73 61 6d 65 20 42 4c 4f 42 20 , the same BLOB
46370 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 73 65 that would be se
46380 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a lected by:.**.**
46390 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45 <pre>.** SE
463a0 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f LECT zColumn FRO
463b0 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 57 48 45 M zDb.zTable WHE
463c0 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20 69 52 6f RE [rowid] = iRo
463d0 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b 45 4e w;.** </pre> {EN
463e0 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 D}.**.** If the
463f0 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
46400 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
46410 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65 the BLOB is ope
46420 6e 65 64 20 66 6f 72 20 72 65 61 64 0a 2a 2a 20 ned for read.**
46430 61 6e 64 20 77 72 69 74 65 20 61 63 63 65 73 73 and write access
46440 2e 20 49 66 20 69 74 20 69 73 20 7a 65 72 6f 2c . If it is zero,
46450 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65 the BLOB is ope
46460 6e 65 64 20 66 6f 72 20 72 65 61 64 20 61 63 63 ned for read acc
46470 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ess..**.** Note
46480 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 that the databas
46490 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 74 68 e name is not th
464a0 65 20 66 69 6c 65 6e 61 6d 65 20 74 68 61 74 20 e filename that
464b0 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 contains.** the
464c0 64 61 74 61 62 61 73 65 20 62 75 74 20 72 61 74 database but rat
464d0 68 65 72 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 her the symbolic
464e0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
464f0 61 62 61 73 65 20 74 68 61 74 0a 2a 2a 20 69 73 abase that.** is
46500 20 61 73 73 69 67 6e 65 64 20 77 68 65 6e 20 74 assigned when t
46510 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 he database is c
46520 6f 6e 6e 65 63 74 65 64 20 75 73 69 6e 67 20 5b onnected using [
46530 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f 72 20 ATTACH]..** For
46540 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
46550 65 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 e file, the data
46560 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 6d 61 base name is "ma
46570 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 45 4d 50 in"..** For TEMP
46580 20 74 61 62 6c 65 73 2c 20 74 68 65 20 64 61 74 tables, the dat
46590 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 74 abase name is "t
465a0 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 emp"..**.** On s
465b0 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f uccess, [SQLITE_
465c0 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 OK] is returned
465d0 61 6e 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f and the new [BLO
465e0 42 20 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69 B handle] is wri
465f0 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c tten.** to *ppBl
46600 6f 62 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e ob. Otherwise an
46610 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 [error code] is
46620 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e returned and an
46630 79 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 0a y value written.
46640 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 20 73 68 ** to *ppBlob sh
46650 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 ould not be used
46660 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a by the caller..
46670 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
46680 20 73 65 74 73 20 74 68 65 20 5b 64 61 74 61 62 sets the [datab
46690 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
466a0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d error code and m
466b0 65 73 73 61 67 65 0a 2a 2a 20 61 63 63 65 73 73 essage.** access
466c0 69 62 6c 65 20 76 69 61 20 5b 73 71 6c 69 74 65 ible via [sqlite
466d0 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 61 6e 64 3_errcode()] and
466e0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 [sqlite3_errmsg
466f0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ()]..**.** If th
46700 65 20 72 6f 77 20 74 68 61 74 20 61 20 42 4c 4f e row that a BLO
46710 42 20 68 61 6e 64 6c 65 20 70 6f 69 6e 74 73 20 B handle points
46720 74 6f 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 to is modified b
46730 79 20 61 6e 0a 2a 2a 20 5b 55 50 44 41 54 45 5d y an.** [UPDATE]
46740 2c 20 5b 44 45 4c 45 54 45 5d 2c 20 6f 72 20 62 , [DELETE], or b
46750 79 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 y [ON CONFLICT]
46760 73 69 64 65 2d 65 66 66 65 63 74 73 0a 2a 2a 20 side-effects.**
46770 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 68 61 then the BLOB ha
46780 6e 64 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 61 ndle is marked a
46790 73 20 22 65 78 70 69 72 65 64 22 2e 0a 2a 2a 20 s "expired"..**
467a0 54 68 69 73 20 69 73 20 74 72 75 65 20 69 66 20 This is true if
467b0 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 any column of th
467c0 65 20 72 6f 77 20 69 73 20 63 68 61 6e 67 65 64 e row is changed
467d0 2c 20 65 76 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a , even a column.
467e0 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 ** other than th
467f0 65 20 6f 6e 65 20 74 68 65 20 42 4c 4f 42 20 68 e one the BLOB h
46800 61 6e 64 6c 65 20 69 73 20 6f 70 65 6e 20 6f 6e andle is open on
46810 2e 0a 2a 2a 20 43 61 6c 6c 73 20 74 6f 20 5b 73 ..** Calls to [s
46820 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 qlite3_blob_read
46830 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
46840 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 66 _blob_write()] f
46850 6f 72 0a 2a 2a 20 61 20 65 78 70 69 72 65 64 20 or.** a expired
46860 42 4c 4f 42 20 68 61 6e 64 6c 65 20 66 61 69 6c BLOB handle fail
46870 20 77 69 74 68 20 61 6e 20 72 65 74 75 72 6e 20 with an return
46880 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f code of [SQLITE_
46890 41 42 4f 52 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 ABORT]..** Chang
468a0 65 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 es written into
468b0 61 20 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20 a BLOB prior to
468c0 74 68 65 20 42 4c 4f 42 20 65 78 70 69 72 69 6e the BLOB expirin
468d0 67 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c g are not.** rol
468e0 6c 62 61 63 6b 20 62 79 20 74 68 65 20 65 78 70 lback by the exp
468f0 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 42 iration of the B
46900 4c 4f 42 2e 20 20 53 75 63 68 20 63 68 61 6e 67 LOB. Such chang
46910 65 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c es will eventual
46920 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 69 66 20 ly.** commit if
46930 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
46940 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d continues to com
46950 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 pletion..**.** I
46960 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
46970 20 7b 48 31 37 38 31 33 7d 20 41 20 73 75 63 63 {H17813} A succ
46980 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f essful invocatio
46990 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 n of the [sqlite
469a0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 44 2c 42 2c 3_blob_open(D,B,
469b0 54 2c 43 2c 52 2c 46 2c 50 29 5d 0a 2a 2a 20 20 T,C,R,F,P)].**
469c0 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 interfac
469d0 65 20 73 68 61 6c 6c 20 6f 70 65 6e 20 61 6e 20 e shall open an
469e0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f [sqlite3_blob] o
469f0 62 6a 65 63 74 20 50 20 6f 6e 20 74 68 65 20 42 bject P on the B
46a00 4c 4f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 LOB.**
46a10 69 6e 20 63 6f 6c 75 6d 6e 20 43 20 6f 66 20 74 in column C of t
46a20 68 65 20 74 61 62 6c 65 20 54 20 69 6e 20 74 68 he table T in th
46a30 65 20 64 61 74 61 62 61 73 65 20 42 20 6f 6e 0a e database B on.
46a40 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
46a50 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
46a60 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b tion] D..**.** {
46a70 48 31 37 38 31 34 7d 20 41 20 73 75 63 63 65 73 H17814} A succes
46a80 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 sful invocation
46a90 6f 66 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 of [sqlite3_blob
46aa0 5f 6f 70 65 6e 28 44 2c 2e 2e 2e 29 5d 20 73 68 _open(D,...)] sh
46ab0 61 6c 6c 20 73 74 61 72 74 0a 2a 2a 20 20 20 20 all start.**
46ac0 20 20 20 20 20 20 61 20 6e 65 77 20 74 72 61 6e a new tran
46ad0 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 5b saction on the [
46ae0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
46af0 69 6f 6e 5d 20 44 20 69 66 20 74 68 61 74 0a 2a ion] D if that.*
46b00 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 6e 65 * conne
46b10 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6c 72 ction is not alr
46b20 65 61 64 79 20 69 6e 20 61 20 74 72 61 6e 73 61 eady in a transa
46b30 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ction..**.** {H1
46b40 37 38 31 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 7816} The [sqlit
46b50 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 44 2c 42 e3_blob_open(D,B
46b60 2c 54 2c 43 2c 52 2c 46 2c 50 29 5d 20 69 6e 74 ,T,C,R,F,P)] int
46b70 65 72 66 61 63 65 20 73 68 61 6c 6c 20 6f 70 65 erface shall ope
46b80 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 n.** th
46b90 65 20 42 4c 4f 42 20 66 6f 72 20 72 65 61 64 20 e BLOB for read
46ba0 61 6e 64 20 77 72 69 74 65 20 61 63 63 65 73 73 and write access
46bb0 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 if and only if
46bc0 74 68 65 20 46 0a 2a 2a 20 20 20 20 20 20 20 20 the F.**
46bd0 20 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e parameter is n
46be0 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b on-zero..**.** {
46bf0 48 31 37 38 31 39 7d 20 54 68 65 20 5b 73 71 6c H17819} The [sql
46c00 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 ite3_blob_open()
46c10 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c ] interface shal
46c20 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 l return [SQLITE
46c30 5f 4f 4b 5d 20 6f 6e 0a 2a 2a 20 20 20 20 20 20 _OK] on.**
46c40 20 20 20 20 73 75 63 63 65 73 73 20 61 6e 64 20 success and
46c50 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b an appropriate [
46c60 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66 error code] on f
46c70 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ailure..**.** {H
46c80 31 37 38 32 31 7d 20 49 66 20 61 6e 20 65 72 72 17821} If an err
46c90 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
46ca0 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b evaluation of [
46cb0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 sqlite3_blob_ope
46cc0 6e 28 44 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 n(D,...)].**
46cd0 20 20 20 20 20 20 74 68 65 6e 20 73 75 62 73 65 then subse
46ce0 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b quent calls to [
46cf0 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 sqlite3_errcode(
46d00 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 D)],.**
46d10 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 [sqlite3_extend
46d20 65 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 0a ed_errcode()], .
46d30 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
46d40 69 74 65 33 5f 65 72 72 6d 73 67 28 44 29 5d 2c ite3_errmsg(D)],
46d50 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 and [sqlite3_er
46d60 72 6d 73 67 31 36 28 44 29 5d 20 73 68 61 6c 6c rmsg16(D)] shall
46d70 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 return.**
46d80 20 20 20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 information
46d90 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 appropriate for
46da0 74 68 61 74 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a that error..**.*
46db0 2a 20 7b 48 31 37 38 32 34 7d 20 49 66 20 61 6e * {H17824} If an
46dc0 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 y column in the
46dd0 72 6f 77 20 74 68 61 74 20 61 20 5b 73 71 6c 69 row that a [sqli
46de0 74 65 33 5f 62 6c 6f 62 5d 20 68 61 73 20 6f 70 te3_blob] has op
46df0 65 6e 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 en is.**
46e00 20 20 63 68 61 6e 67 65 64 20 62 79 20 61 20 73 changed by a s
46e10 65 70 61 72 61 74 65 20 5b 55 50 44 41 54 45 5d eparate [UPDATE]
46e20 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 61 or [DELETE] sta
46e30 74 65 6d 65 6e 74 20 6f 72 20 62 79 0a 2a 2a 20 tement or by.**
46e40 20 20 20 20 20 20 20 20 20 61 6e 20 5b 4f 4e 20 an [ON
46e50 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 20 65 CONFLICT] side e
46e60 66 66 65 63 74 2c 20 74 68 65 6e 20 74 68 65 20 ffect, then the
46e70 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 73 [sqlite3_blob] s
46e80 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 hall.**
46e90 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 69 6e be marked as in
46ea0 76 61 6c 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 valid..*/.SQLITE
46eb0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
46ec0 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 _blob_open(. sq
46ed0 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 lite3*,. const
46ee0 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e char *zDb,. con
46ef0 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c st char *zTable,
46f00 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
46f10 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 Column,. sqlite
46f20 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 3_int64 iRow,.
46f30 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c int flags,. sql
46f40 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c ite3_blob **ppBl
46f50 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ob.);../*.** CAP
46f60 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 20 42 I3REF: Close A B
46f70 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 37 38 LOB Handle {H178
46f80 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 30} <S30230>.**.
46f90 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65 ** Closes an ope
46fa0 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e n [BLOB handle].
46fb0 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61 .**.** Closing a
46fc0 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 75 73 BLOB shall caus
46fd0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 e the current tr
46fe0 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d ansaction to com
46ff0 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 mit.** if there
47000 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f are no other BLO
47010 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70 Bs, no pending p
47020 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
47030 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 ts, and the.** d
47040 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
47050 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f on is in [autoco
47060 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 mmit mode]..** I
47070 66 20 61 6e 79 20 77 72 69 74 65 73 20 77 65 72 f any writes wer
47080 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42 4c e made to the BL
47090 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20 62 OB, they might b
470a0 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 0a e held in cache.
470b0 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f ** until the clo
470c0 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66 20 se operation if
470d0 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 20 7b they will fit. {
470e0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 END}.**.** Closi
470f0 6e 67 20 74 68 65 20 42 4c 4f 42 20 6f 66 74 65 ng the BLOB ofte
47100 6e 20 66 6f 72 63 65 73 20 74 68 65 20 63 68 61 n forces the cha
47110 6e 67 65 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 nges.** out to d
47120 69 73 6b 20 61 6e 64 20 73 6f 20 69 66 20 61 6e isk and so if an
47130 79 20 49 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63 y I/O errors occ
47140 75 72 2c 20 74 68 65 79 20 77 69 6c 6c 20 6c 69 ur, they will li
47150 6b 65 6c 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74 kely occur.** at
47160 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 20 74 the time when t
47170 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 he BLOB is close
47180 64 2e 20 20 7b 48 31 37 38 33 33 7d 20 41 6e 79 d. {H17833} Any
47190 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 errors that occ
471a0 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20 63 6c 6f ur during.** clo
471b0 73 69 6e 67 20 61 72 65 20 72 65 70 6f 72 74 65 sing are reporte
471c0 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 d as a non-zero
471d0 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a return value..**
471e0 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20 69 73 20 .** The BLOB is
471f0 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 69 74 69 closed unconditi
47200 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e 20 69 66 onally. Even if
47210 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
47220 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 6f turns.** an erro
47230 72 20 63 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42 r code, the BLOB
47240 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64 is still closed
47250 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
47260 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 33 TS:.**.** {H1783
47270 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 3} The [sqlite3_
47280 62 6c 6f 62 5f 63 6c 6f 73 65 28 50 29 5d 20 69 blob_close(P)] i
47290 6e 74 65 72 66 61 63 65 20 63 6c 6f 73 65 73 20 nterface closes
472a0 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 an [sqlite3_blob
472b0 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 ].** ob
472c0 6a 65 63 74 20 50 20 70 72 65 76 69 6f 75 73 6c ject P previousl
472d0 79 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 5b y opened using [
472e0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 sqlite3_blob_ope
472f0 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 n()]..**.** {H17
47300 38 33 36 7d 20 43 6c 6f 73 69 6e 67 20 61 6e 20 836} Closing an
47310 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f [sqlite3_blob] o
47320 62 6a 65 63 74 20 75 73 69 6e 67 0a 2a 2a 20 20 bject using.**
47330 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
47340 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 73 _blob_close()] s
47350 68 61 6c 6c 20 63 61 75 73 65 20 74 68 65 20 63 hall cause the c
47360 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
47370 6f 6e 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 on to.**
47380 20 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 72 commit if ther
47390 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 6f e are no other o
473a0 70 65 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f pen [sqlite3_blo
473b0 62 5d 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 20 20 b] objects.**
473c0 20 20 20 20 20 20 20 6f 72 20 5b 70 72 65 70 61 or [prepa
473d0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 red statements]
473e0 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 on the same [dat
473f0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
47400 5d 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 ] and.**
47410 20 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 the database c
47420 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 onnection is in
47430 5b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 [autocommit mode
47440 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 33 39 ]..**.** {H17839
47450 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 } The [sqlite3_b
47460 6c 6f 62 5f 63 6c 6f 73 65 28 50 29 5d 20 69 6e lob_close(P)] in
47470 74 65 72 66 61 63 65 73 20 73 68 61 6c 6c 20 63 terfaces shall c
47480 6c 6f 73 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 lose the.**
47490 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 6c [sqlite3_bl
474a0 6f 62 5d 20 6f 62 6a 65 63 74 20 50 20 75 6e 63 ob] object P unc
474b0 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 65 76 onditionally, ev
474c0 65 6e 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 en if.**
474d0 20 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f [sqlite3_blob_
474e0 63 6c 6f 73 65 28 50 29 5d 20 72 65 74 75 72 6e close(P)] return
474f0 73 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 s something othe
47500 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f r than [SQLITE_O
47510 4b 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 K]..*/.SQLITE_AP
47520 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
47530 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 ob_close(sqlite3
47540 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _blob *);../*.**
47550 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 75 72 CAPI3REF: Retur
47560 6e 20 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e n The Size Of An
47570 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 Open BLOB {H178
47580 34 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 40} <S30230>.**.
47590 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 73 ** Returns the s
475a0 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 ize in bytes of
475b0 74 68 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69 the BLOB accessi
475c0 62 6c 65 20 76 69 61 20 74 68 65 20 6f 70 65 6e ble via the open
475d0 0a 2a 2a 20 5b 5d 42 4c 4f 42 20 68 61 6e 64 6c .** []BLOB handl
475e0 65 5d 20 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 e] in its only a
475f0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 rgument..**.** I
47600 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
47610 20 7b 48 31 37 38 34 33 7d 20 54 68 65 20 5b 73 {H17843} The [s
47620 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 qlite3_blob_byte
47630 73 28 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 s(P)] interface
47640 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 returns the size
47650 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 .** in
47660 62 79 74 65 73 20 6f 66 20 74 68 65 20 42 4c 4f bytes of the BLO
47670 42 20 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69 B that the [sqli
47680 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 te3_blob] object
47690 20 50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 P.** r
476a0 65 66 65 72 73 20 74 6f 2e 0a 2a 2f 0a 53 51 4c efers to..*/.SQL
476b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
476c0 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73 te3_blob_bytes(s
476d0 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a qlite3_blob *);.
476e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
476f0 20 52 65 61 64 20 44 61 74 61 20 46 72 6f 6d 20 Read Data From
47700 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 A BLOB Increment
47710 61 6c 6c 79 20 7b 48 31 37 38 35 30 7d 20 3c 53 ally {H17850} <S
47720 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 30230>.**.** Thi
47730 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
47740 65 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20 ed to read data
47750 66 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b 42 4c from an open [BL
47760 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 74 6f 20 OB handle] into
47770 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 a.** caller-supp
47780 6c 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 lied buffer. N b
47790 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 ytes of data are
477a0 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 75 66 copied into buf
477b0 66 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68 fer Z.** from th
477c0 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 e open BLOB, sta
477d0 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 rting at offset
477e0 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 iOffset..**.** I
477f0 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 f offset iOffset
47800 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 is less than N
47810 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 bytes from the e
47820 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a nd of the BLOB,.
47830 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 ** [SQLITE_ERROR
47840 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e ] is returned an
47850 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 65 61 d no data is rea
47860 64 2e 20 20 49 66 20 4e 20 6f 72 20 69 4f 66 66 d. If N or iOff
47870 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74 set is.** less t
47880 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51 4c 49 54 han zero, [SQLIT
47890 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 E_ERROR] is retu
478a0 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 rned and no data
478b0 20 69 73 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 is read..**.**
478c0 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 An attempt to re
478d0 61 64 20 66 72 6f 6d 20 61 6e 20 65 78 70 69 72 ad from an expir
478e0 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d ed [BLOB handle]
478f0 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a fails with an.*
47900 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 * error code of
47910 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a [SQLITE_ABORT]..
47920 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 **.** On success
47930 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 , SQLITE_OK is r
47940 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 eturned..** Othe
47950 72 77 69 73 65 2c 20 61 6e 20 5b 65 72 72 6f 72 rwise, an [error
47960 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 code] or an [ex
47970 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 tended error cod
47980 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a e] is returned..
47990 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
479a0 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 35 33 7d :.**.** {H17853}
479b0 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e A successful in
479c0 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c vocation of [sql
479d0 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 ite3_blob_read(P
479e0 2c 5a 2c 4e 2c 58 29 5d 20 0a 2a 2a 20 20 20 20 ,Z,N,X)] .**
479f0 20 20 20 20 20 20 73 68 61 6c 6c 20 72 65 61 64 shall read
47a00 73 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 s N bytes of dat
47a10 61 20 6f 75 74 20 6f 66 20 74 68 65 20 42 4c 4f a out of the BLO
47a20 42 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 0a B referenced by.
47a30 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 42 4c 4f ** [BLO
47a40 42 20 68 61 6e 64 6c 65 5d 20 50 20 62 65 67 69 B handle] P begi
47a50 6e 6e 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 nning at offset
47a60 58 20 61 6e 64 20 73 74 6f 72 65 20 74 68 6f 73 X and store thos
47a70 65 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20 20 e bytes.**
47a80 20 20 20 20 69 6e 74 6f 20 62 75 66 66 65 72 20 into buffer
47a90 5a 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 35 36 Z..**.** {H17856
47aa0 7d 20 49 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c } In [sqlite3_bl
47ab0 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58 29 ob_read(P,Z,N,X)
47ac0 5d 20 69 66 20 74 68 65 20 73 69 7a 65 20 6f 66 ] if the size of
47ad0 20 74 68 65 20 42 4c 4f 42 0a 2a 2a 20 20 20 20 the BLOB.**
47ae0 20 20 20 20 20 20 69 73 20 6c 65 73 73 20 74 68 is less th
47af0 61 6e 20 4e 2b 58 20 62 79 74 65 73 2c 20 74 68 an N+X bytes, th
47b00 65 6e 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 en the function
47b10 73 68 61 6c 6c 20 6c 65 61 76 65 20 74 68 65 0a shall leave the.
47b20 2a 2a 20 20 20 20 20 20 20 20 20 20 5a 20 62 75 ** Z bu
47b30 66 66 65 72 20 75 6e 63 68 61 6e 67 65 64 20 61 ffer unchanged a
47b40 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 nd return [SQLIT
47b50 45 5f 45 52 52 4f 52 5d 2e 0a 2a 2a 0a 2a 2a 20 E_ERROR]..**.**
47b60 7b 48 31 37 38 35 39 7d 20 49 6e 20 5b 73 71 6c {H17859} In [sql
47b70 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 ite3_blob_read(P
47b80 2c 5a 2c 4e 2c 58 29 5d 20 69 66 20 58 20 6f 72 ,Z,N,X)] if X or
47b90 20 4e 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 N is less than
47ba0 7a 65 72 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 zero.**
47bb0 20 74 68 65 6e 20 74 68 65 20 66 75 6e 63 74 69 then the functi
47bc0 6f 6e 20 73 68 61 6c 6c 20 6c 65 61 76 65 20 74 on shall leave t
47bd0 68 65 20 5a 20 62 75 66 66 65 72 20 75 6e 63 68 he Z buffer unch
47be0 61 6e 67 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 anged.**
47bf0 20 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 and return [SQ
47c00 4c 49 54 45 5f 45 52 52 4f 52 5d 2e 0a 2a 2a 0a LITE_ERROR]..**.
47c10 2a 2a 20 7b 48 31 37 38 36 32 7d 20 54 68 65 20 ** {H17862} The
47c20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 [sqlite3_blob_re
47c30 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20 69 6e 74 ad(P,Z,N,X)] int
47c40 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74 erface shall ret
47c50 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a urn [SQLITE_OK].
47c60 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 4e ** if N
47c70 20 62 79 74 65 73 20 61 72 65 20 73 75 63 63 65 bytes are succe
47c80 73 73 66 75 6c 6c 79 20 72 65 61 64 20 69 6e 74 ssfully read int
47c90 6f 20 62 75 66 66 65 72 20 5a 2e 0a 2a 2a 0a 2a o buffer Z..**.*
47ca0 2a 20 7b 48 31 37 38 36 33 7d 20 49 66 20 74 68 * {H17863} If th
47cb0 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 e [BLOB handle]
47cc0 50 20 69 73 20 65 78 70 69 72 65 64 20 61 6e 64 P is expired and
47cd0 20 58 20 61 6e 64 20 4e 20 61 72 65 20 77 69 74 X and N are wit
47ce0 68 69 6e 20 62 6f 75 6e 64 73 0a 2a 2a 20 20 20 hin bounds.**
47cf0 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c then [sql
47d00 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 ite3_blob_read(P
47d10 2c 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 20 6c ,Z,N,X)] shall l
47d20 65 61 76 65 20 74 68 65 20 5a 20 62 75 66 66 65 eave the Z buffe
47d30 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 6e r.** un
47d40 63 68 61 6e 67 65 64 20 61 6e 64 20 72 65 74 75 changed and retu
47d50 72 6e 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 rn [SQLITE_ABORT
47d60 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 36 35 ]..**.** {H17865
47d70 7d 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 } If the request
47d80 65 64 20 72 65 61 64 20 63 6f 75 6c 64 20 6e 6f ed read could no
47d90 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 64 2c 0a t be completed,.
47da0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
47db0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 [sqlite3_blob_re
47dc0 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20 69 6e 74 ad(P,Z,N,X)] int
47dd0 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74 erface shall ret
47de0 75 72 6e 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 urn an.**
47df0 20 20 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b appropriate [
47e00 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 5b error code] or [
47e10 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
47e20 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 ode]..**.** {H17
47e30 38 36 38 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 868} If an error
47e40 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 65 occurs during e
47e50 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b 73 71 valuation of [sq
47e60 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 lite3_blob_read(
47e70 50 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 P,...)].**
47e80 20 20 20 20 74 68 65 6e 20 73 75 62 73 65 71 75 then subsequ
47e90 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 ent calls to [sq
47ea0 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 44 29 lite3_errcode(D)
47eb0 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b ],.** [
47ec0 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
47ed0 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20 _errcode()],.**
47ee0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
47ef0 33 5f 65 72 72 6d 73 67 28 44 29 5d 2c 20 61 6e 3_errmsg(D)], an
47f00 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 d [sqlite3_errms
47f10 67 31 36 28 44 29 5d 20 73 68 61 6c 6c 20 72 65 g16(D)] shall re
47f20 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 turn.**
47f30 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 information app
47f40 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 61 ropriate for tha
47f50 74 20 65 72 72 6f 72 2c 20 77 68 65 72 65 20 44 t error, where D
47f60 20 69 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 is the.**
47f70 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f [database co
47f80 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61 74 20 77 nnection] that w
47f90 61 73 20 75 73 65 64 20 74 6f 20 6f 70 65 6e 20 as used to open
47fa0 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 the [BLOB handle
47fb0 5d 20 50 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ] P..*/.SQLITE_A
47fc0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
47fd0 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33 lob_read(sqlite3
47fe0 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69 64 20 2a 5a _blob *, void *Z
47ff0 2c 20 69 6e 74 20 4e 2c 20 69 6e 74 20 69 4f 66 , int N, int iOf
48000 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 fset);../*.** CA
48010 50 49 33 52 45 46 3a 20 57 72 69 74 65 20 44 61 PI3REF: Write Da
48020 74 61 20 49 6e 74 6f 20 41 20 42 4c 4f 42 20 49 ta Into A BLOB I
48030 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 ncrementally {H1
48040 37 38 37 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 7870} <S30230>.*
48050 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
48060 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 77 72 on is used to wr
48070 69 74 65 20 64 61 74 61 20 69 6e 74 6f 20 61 6e ite data into an
48080 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 open [BLOB hand
48090 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a 20 63 61 le] from a.** ca
480a0 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62 75 ller-supplied bu
480b0 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f 66 ffer. N bytes of
480c0 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 data are copied
480d0 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 from the buffer
480e0 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6f Z.** into the o
480f0 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 pen BLOB, starti
48100 6e 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 ng at offset iOf
48110 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 fset..**.** If t
48120 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d he [BLOB handle]
48130 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 passed as the f
48140 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 77 61 irst argument wa
48150 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 66 6f 72 s not opened for
48160 0a 2a 2a 20 77 72 69 74 69 6e 67 20 28 74 68 65 .** writing (the
48170 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 flags parameter
48180 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f to [sqlite3_blo
48190 62 5f 6f 70 65 6e 28 29 5d 20 77 61 73 20 7a 65 b_open()] was ze
481a0 72 6f 29 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e ro),.** this fun
481b0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 ction returns [S
481c0 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2e QLITE_READONLY].
481d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
481e0 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 6d 6f tion may only mo
481f0 64 69 66 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 dify the content
48200 73 20 6f 66 20 74 68 65 20 42 4c 4f 42 3b 20 69 s of the BLOB; i
48210 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73 t is.** not poss
48220 69 62 6c 65 20 74 6f 20 69 6e 63 72 65 61 73 65 ible to increase
48230 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 the size of a B
48240 4c 4f 42 20 75 73 69 6e 67 20 74 68 69 73 20 41 LOB using this A
48250 50 49 2e 0a 2a 2a 20 49 66 20 6f 66 66 73 65 74 PI..** If offset
48260 20 69 4f 66 66 73 65 74 20 69 73 20 6c 65 73 73 iOffset is less
48270 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 66 72 than N bytes fr
48280 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 om the end of th
48290 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 e BLOB,.** [SQLI
482a0 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 TE_ERROR] is ret
482b0 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 urned and no dat
482c0 61 20 69 73 20 77 72 69 74 74 65 6e 2e 20 20 49 a is written. I
482d0 66 20 4e 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74 f N is.** less t
482e0 68 61 6e 20 7a 65 72 6f 20 5b 53 51 4c 49 54 45 han zero [SQLITE
482f0 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 _ERROR] is retur
48300 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 ned and no data
48310 69 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a is written..**.*
48320 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 * An attempt to
48330 77 72 69 74 65 20 74 6f 20 61 6e 20 65 78 70 69 write to an expi
48340 72 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 red [BLOB handle
48350 5d 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a ] fails with an.
48360 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 ** error code of
48370 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e [SQLITE_ABORT].
48380 20 20 57 72 69 74 65 73 20 74 6f 20 74 68 65 20 Writes to the
48390 42 4c 4f 42 20 74 68 61 74 20 6f 63 63 75 72 72 BLOB that occurr
483a0 65 64 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 ed.** before the
483b0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 [BLOB handle] e
483c0 78 70 69 72 65 64 20 61 72 65 20 6e 6f 74 20 72 xpired are not r
483d0 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 olled back by th
483e0 65 0a 2a 2a 20 65 78 70 69 72 61 74 69 6f 6e 20 e.** expiration
483f0 6f 66 20 74 68 65 20 68 61 6e 64 6c 65 2c 20 74 of the handle, t
48400 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 hough of course
48410 74 68 6f 73 65 20 63 68 61 6e 67 65 73 20 6d 69 those changes mi
48420 67 68 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e ght.** have been
48430 20 6f 76 65 72 77 72 69 74 74 65 6e 20 62 79 20 overwritten by
48440 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 the statement th
48450 61 74 20 65 78 70 69 72 65 64 20 74 68 65 20 42 at expired the B
48460 4c 4f 42 20 68 61 6e 64 6c 65 0a 2a 2a 20 6f 72 LOB handle.** or
48470 20 62 79 20 6f 74 68 65 72 20 69 6e 64 65 70 65 by other indepe
48480 6e 64 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 73 ndent statements
48490 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 ..**.** On succe
484a0 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 ss, SQLITE_OK is
484b0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 returned..** Ot
484c0 68 65 72 77 69 73 65 2c 20 61 6e 20 20 5b 65 72 herwise, an [er
484d0 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 ror code] or an
484e0 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 [extended error
484f0 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 code] is returne
48500 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 d..**.** INVARIA
48510 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 NTS:.**.** {H178
48520 37 33 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 73} A successful
48530 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b invocation of [
48540 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 sqlite3_blob_wri
48550 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 0a 2a 2a 20 te(P,Z,N,X)].**
48560 20 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 77 shall w
48570 72 69 74 65 20 4e 20 62 79 74 65 73 20 6f 66 20 rite N bytes of
48580 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66 65 72 data from buffer
48590 20 5a 20 69 6e 74 6f 20 74 68 65 20 42 4c 4f 42 Z into the BLOB
485a0 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 .** re
485b0 66 65 72 65 6e 63 65 64 20 62 79 20 5b 42 4c 4f ferenced by [BLO
485c0 42 20 68 61 6e 64 6c 65 5d 20 50 20 62 65 67 69 B handle] P begi
485d0 6e 6e 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 nning at offset
485e0 58 20 69 6e 74 6f 0a 2a 2a 20 20 20 20 20 20 20 X into.**
485f0 20 20 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 0a the BLOB..**.
48600 2a 2a 20 7b 48 31 37 38 37 34 7d 20 49 6e 20 74 ** {H17874} In t
48610 68 65 20 61 62 73 65 6e 63 65 20 6f 66 20 6f 74 he absence of ot
48620 68 65 72 20 6f 76 65 72 72 69 64 64 69 6e 67 20 her overridding
48630 63 68 61 6e 67 65 73 2c 20 74 68 65 20 63 68 61 changes, the cha
48640 6e 67 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 nges.**
48650 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20 42 4c written to a BL
48660 4f 42 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 OB by [sqlite3_b
48670 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 73 68 61 lob_write()] sha
48680 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 ll.** r
48690 65 6d 61 69 6e 20 69 6e 20 65 66 66 65 63 74 20 emain in effect
486a0 61 66 74 65 72 20 74 68 65 20 61 73 73 6f 63 69 after the associ
486b0 61 74 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c ated [BLOB handl
486c0 65 5d 20 65 78 70 69 72 65 73 2e 0a 2a 2a 0a 2a e] expires..**.*
486d0 2a 20 7b 48 31 37 38 37 35 7d 20 49 66 20 74 68 * {H17875} If th
486e0 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 e [BLOB handle]
486f0 50 20 77 61 73 20 6f 70 65 6e 65 64 20 66 6f 72 P was opened for
48700 20 72 65 61 64 69 6e 67 20 6f 6e 6c 79 20 74 68 reading only th
48710 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 en.** a
48720 6e 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 n invocation of
48730 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 [sqlite3_blob_wr
48740 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 20 73 68 ite(P,Z,N,X)] sh
48750 61 6c 6c 20 6c 65 61 76 65 0a 2a 2a 20 20 20 20 all leave.**
48760 20 20 20 20 20 20 74 68 65 20 72 65 66 65 72 65 the refere
48770 6e 63 65 64 20 42 4c 4f 42 20 75 6e 63 68 61 6e nced BLOB unchan
48780 67 65 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b ged and return [
48790 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d SQLITE_READONLY]
487a0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 37 36 7d ..**.** {H17876}
487b0 20 49 66 20 74 68 65 20 73 69 7a 65 20 6f 66 20 If the size of
487c0 74 68 65 20 42 4c 4f 42 20 72 65 66 65 72 65 6e the BLOB referen
487d0 63 65 64 20 62 79 20 5b 42 4c 4f 42 20 68 61 6e ced by [BLOB han
487e0 64 6c 65 5d 20 50 20 69 73 0a 2a 2a 20 20 20 20 dle] P is.**
487f0 20 20 20 20 20 20 6c 65 73 73 20 74 68 61 6e 20 less than
48800 4e 2b 58 20 62 79 74 65 73 20 74 68 65 6e 20 5b N+X bytes then [
48810 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 sqlite3_blob_wri
48820 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 20 73 68 61 te(P,Z,N,X)] sha
48830 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6c ll.** l
48840 65 61 76 65 20 74 68 65 20 42 4c 4f 42 20 75 6e eave the BLOB un
48850 63 68 61 6e 67 65 64 20 61 6e 64 20 72 65 74 75 changed and retu
48860 72 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 rn [SQLITE_ERROR
48870 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 37 37 ]..**.** {H17877
48880 7d 20 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 } If the [BLOB h
48890 61 6e 64 6c 65 5d 20 50 20 69 73 20 65 78 70 69 andle] P is expi
488a0 72 65 64 20 61 6e 64 20 58 20 61 6e 64 20 4e 20 red and X and N
488b0 61 72 65 20 77 69 74 68 69 6e 20 62 6f 75 6e 64 are within bound
488c0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 s.** th
488d0 65 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 en [sqlite3_blob
488e0 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20 _read(P,Z,N,X)]
488f0 73 68 61 6c 6c 20 6c 65 61 76 65 20 74 68 65 20 shall leave the
48900 42 4c 4f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 BLOB.**
48910 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 72 unchanged and r
48920 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 41 42 eturn [SQLITE_AB
48930 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 ORT]..**.** {H17
48940 38 37 39 7d 20 49 66 20 58 20 6f 72 20 4e 20 61 879} If X or N a
48950 72 65 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 re less than zer
48960 6f 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f o then [sqlite3_
48970 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a 2c 4e blob_write(P,Z,N
48980 2c 58 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 ,X)].**
48990 20 73 68 61 6c 6c 20 6c 65 61 76 65 20 74 68 65 shall leave the
489a0 20 42 4c 4f 42 20 72 65 66 65 72 65 6e 63 65 64 BLOB referenced
489b0 20 62 79 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 by [BLOB handle
489c0 5d 20 50 20 75 6e 63 68 61 6e 67 65 64 0a 2a 2a ] P unchanged.**
489d0 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 72 65 and re
489e0 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 turn [SQLITE_ERR
489f0 4f 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 OR]..**.** {H178
48a00 38 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 82} The [sqlite3
48a10 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a 2c _blob_write(P,Z,
48a20 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 65 20 N,X)] interface
48a30 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 shall return.**
48a40 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 [SQLITE
48a50 5f 4f 4b 5d 20 69 66 20 4e 20 62 79 74 65 73 20 _OK] if N bytes
48a60 77 68 65 72 65 20 73 75 63 63 65 73 73 66 75 6c where successful
48a70 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 ly written into
48a80 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 the BLOB..**.**
48a90 7b 48 31 37 38 38 35 7d 20 49 66 20 74 68 65 20 {H17885} If the
48aa0 72 65 71 75 65 73 74 65 64 20 77 72 69 74 65 20 requested write
48ab0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6d could not be com
48ac0 70 6c 65 74 65 64 2c 0a 2a 2a 20 20 20 20 20 20 pleted,.**
48ad0 20 20 20 20 74 68 65 20 5b 73 71 6c 69 74 65 33 the [sqlite3
48ae0 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a 2c _blob_write(P,Z,
48af0 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 65 20 N,X)] interface
48b00 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 61 6e 0a shall return an.
48b10 2a 2a 20 20 20 20 20 20 20 20 20 20 61 70 70 72 ** appr
48b20 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20 63 opriate [error c
48b30 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 ode] or [extende
48b40 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a d error code]..*
48b50 2a 0a 2a 2a 20 7b 48 31 37 38 38 38 7d 20 49 66 *.** {H17888} If
48b60 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
48b70 20 64 75 72 69 6e 67 20 65 76 61 6c 75 61 74 69 during evaluati
48b80 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 62 on of [sqlite3_b
48b90 6c 6f 62 5f 77 72 69 74 65 28 44 2c 2e 2e 2e 29 lob_write(D,...)
48ba0 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 ].** th
48bb0 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 en subsequent ca
48bc0 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
48bd0 65 72 72 63 6f 64 65 28 44 29 5d 2c 0a 2a 2a 20 errcode(D)],.**
48be0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
48bf0 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 3_extended_errco
48c00 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 de()],.**
48c10 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d [sqlite3_errm
48c20 73 67 28 44 29 5d 2c 20 61 6e 64 20 5b 73 71 6c sg(D)], and [sql
48c30 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 44 29 ite3_errmsg16(D)
48c40 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a ] shall return.*
48c50 2a 20 20 20 20 20 20 20 20 20 20 69 6e 66 6f 72 * infor
48c60 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 69 61 mation appropria
48c70 74 65 20 66 6f 72 20 74 68 61 74 20 65 72 72 6f te for that erro
48c80 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 r..*/.SQLITE_API
48c90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f int sqlite3_blo
48ca0 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f b_write(sqlite3_
48cb0 62 6c 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f blob *, const vo
48cc0 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e id *z, int n, in
48cd0 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a t iOffset);../*.
48ce0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 ** CAPI3REF: Vir
48cf0 74 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d tual File System
48d00 20 4f 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30 Objects {H11200
48d10 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S20100>.**.**
48d20 20 41 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73 A virtual files
48d30 79 73 74 65 6d 20 28 56 46 53 29 20 69 73 20 61 ystem (VFS) is a
48d40 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 n [sqlite3_vfs]
48d50 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53 object.** that S
48d60 51 4c 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e QLite uses to in
48d70 74 65 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74 teract.** with t
48d80 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 he underlying op
48d90 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 erating system.
48da0 20 4d 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69 Most SQLite bui
48db0 6c 64 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a lds come with a.
48dc0 2a 2a 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c ** single defaul
48dd0 74 20 56 46 53 20 74 68 61 74 20 69 73 20 61 70 t VFS that is ap
48de0 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 propriate for th
48df0 65 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e e host computer.
48e00 0a 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61 .** New VFSes ca
48e10 6e 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 n be registered
48e20 61 6e 64 20 65 78 69 73 74 69 6e 67 20 56 46 53 and existing VFS
48e30 65 73 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69 es can be unregi
48e40 73 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 stered..** The f
48e50 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 ollowing interfa
48e60 63 65 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 ces are provided
48e70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
48e80 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 te3_vfs_find() i
48e90 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
48ea0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
48eb0 56 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61 VFS given its na
48ec0 6d 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 me..** Names are
48ed0 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e case sensitive.
48ee0 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65 .** Names are ze
48ef0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 ro-terminated UT
48f00 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 F-8 strings..**
48f10 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d If there is no m
48f20 61 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 atch, a NULL poi
48f30 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 nter is returned
48f40 2e 0a 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65 ..** If zVfsName
48f50 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 is NULL then th
48f60 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 e default VFS is
48f70 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
48f80 20 4e 65 77 20 56 46 53 65 73 20 61 72 65 20 72 New VFSes are r
48f90 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 73 egistered with s
48fa0 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
48fb0 74 65 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e ter()..** Each n
48fc0 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 ew VFS becomes t
48fd0 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 he default VFS i
48fe0 66 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 f the makeDflt f
48ff0 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54 lag is set..** T
49000 68 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20 he same VFS can
49010 62 65 20 72 65 67 69 73 74 65 72 65 64 20 6d 75 be registered mu
49020 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 ltiple times wit
49030 68 6f 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20 hout injury..**
49040 54 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74 To make an exist
49050 69 6e 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65 ing VFS into the
49060 20 64 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65 default VFS, re
49070 67 69 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a gister it again.
49080 2a 2a 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65 ** with the make
49090 44 66 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20 Dflt flag set.
490a0 49 66 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 If two different
490b0 20 56 46 53 65 73 20 77 69 74 68 20 74 68 65 0a VFSes with the.
490c0 2a 2a 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65 ** same name are
490d0 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 registered, the
490e0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
490f0 65 66 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a efined. If a.**
49100 20 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72 VFS is register
49110 65 64 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 ed with a name t
49120 68 61 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 hat is NULL or a
49130 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a n empty string,.
49140 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 ** then the beha
49150 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 vior is undefine
49160 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 d..**.** Unregis
49170 74 65 72 20 61 20 56 46 53 20 77 69 74 68 20 74 ter a VFS with t
49180 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 he sqlite3_vfs_u
49190 6e 72 65 67 69 73 74 65 72 28 29 20 69 6e 74 65 nregister() inte
491a0 72 66 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65 rface..** If the
491b0 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 default VFS is
491c0 75 6e 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e unregistered, an
491d0 6f 74 68 65 72 20 56 46 53 20 69 73 20 63 68 6f other VFS is cho
491e0 73 65 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 sen as.** the de
491f0 66 61 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 fault. The choi
49200 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56 ce for the new V
49210 46 53 20 69 73 20 61 72 62 69 74 72 61 72 79 2e FS is arbitrary.
49220 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 .**.** INVARIANT
49230 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32 30 33 S:.**.** {H11203
49240 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 } The [sqlite3_v
49250 66 73 5f 66 69 6e 64 28 4e 29 5d 20 69 6e 74 65 fs_find(N)] inte
49260 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 rface returns a
49270 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a pointer to the.*
49280 2a 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73 * regis
49290 74 65 72 65 64 20 5b 73 71 6c 69 74 65 33 5f 76 tered [sqlite3_v
492a0 66 73 5d 20 6f 62 6a 65 63 74 20 77 68 6f 73 65 fs] object whose
492b0 20 6e 61 6d 65 20 65 78 61 63 74 6c 79 20 6d 61 name exactly ma
492c0 74 63 68 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 tches.**
492d0 20 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 the zero-termi
492e0 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 nated UTF-8 stri
492f0 6e 67 20 4e 2c 20 6f 72 20 69 74 20 72 65 74 75 ng N, or it retu
49300 72 6e 73 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20 20 rns NULL if.**
49310 20 20 20 20 20 20 20 20 74 68 65 72 65 20 69 73 there is
49320 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a 2a no match..**.**
49330 20 7b 48 31 31 32 30 36 7d 20 49 66 20 74 68 65 {H11206} If the
49340 20 4e 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 N parameter to
49350 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e [sqlite3_vfs_fin
49360 64 28 4e 29 5d 20 69 73 20 4e 55 4c 4c 20 74 68 d(N)] is NULL th
49370 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 en.** t
49380 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 he function retu
49390 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
493a0 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 the default [sq
493b0 6c 69 74 65 33 5f 76 66 73 5d 0a 2a 2a 20 20 20 lite3_vfs].**
493c0 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 69 66 object if
493d0 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2c 20 6f there is one, o
493e0 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 r NULL if there
493f0 69 73 20 6e 6f 20 64 65 66 61 75 6c 74 0a 2a 2a is no default.**
49400 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
49410 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a e3_vfs] object..
49420 2a 2a 0a 2a 2a 20 7b 48 31 31 32 30 39 7d 20 54 **.** {H11209} T
49430 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f he [sqlite3_vfs_
49440 72 65 67 69 73 74 65 72 28 50 2c 46 29 5d 20 69 register(P,F)] i
49450 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65 nterface registe
49460 72 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 rs the.**
49470 20 20 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 5b well-formed [
49480 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a sqlite3_vfs] obj
49490 65 63 74 20 50 20 75 73 69 6e 67 20 74 68 65 20 ect P using the
494a0 6e 61 6d 65 20 67 69 76 65 6e 0a 2a 2a 20 20 20 name given.**
494b0 20 20 20 20 20 20 20 62 79 20 74 68 65 20 7a 4e by the zN
494c0 61 6d 65 20 66 69 65 6c 64 20 6f 66 20 74 68 65 ame field of the
494d0 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 7b object..**.** {
494e0 48 31 31 32 31 32 7d 20 55 73 69 6e 67 20 74 68 H11212} Using th
494f0 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 e [sqlite3_vfs_r
49500 65 67 69 73 74 65 72 28 50 2c 46 29 5d 20 69 6e egister(P,F)] in
49510 74 65 72 66 61 63 65 20 74 6f 20 72 65 67 69 73 terface to regis
49520 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ter.**
49530 74 68 65 20 73 61 6d 65 20 5b 73 71 6c 69 74 65 the same [sqlite
49540 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20 6d 75 3_vfs] object mu
49550 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 69 73 20 ltiple times is
49560 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 a harmless no-op
49570 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32 31 35 7d ..**.** {H11215}
49580 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 The [sqlite3_vf
49590 73 5f 72 65 67 69 73 74 65 72 28 50 2c 46 29 5d s_register(P,F)]
495a0 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 interface makes
495b0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 the [sqlite3_vf
495c0 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f s].** o
495d0 62 6a 65 63 74 20 50 20 74 68 65 20 64 65 66 61 bject P the defa
495e0 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 ult [sqlite3_vfs
495f0 5d 20 6f 62 6a 65 63 74 20 69 66 20 46 20 69 73 ] object if F is
49600 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a non-zero..**.**
49610 20 7b 48 31 31 32 31 38 7d 20 54 68 65 20 5b 73 {H11218} The [s
49620 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 qlite3_vfs_unreg
49630 69 73 74 65 72 28 50 29 5d 20 69 6e 74 65 72 66 ister(P)] interf
49640 61 63 65 20 75 6e 72 65 67 69 73 74 65 72 73 20 ace unregisters
49650 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
49660 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 [sqlite3_vfs] ob
49670 6a 65 63 74 20 50 20 73 6f 20 74 68 61 74 20 69 ject P so that i
49680 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 t is no longer r
49690 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 20 20 eturned by.**
496a0 20 20 20 20 20 20 20 73 75 62 73 65 71 75 65 6e subsequen
496b0 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 t calls to [sqli
496c0 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 5d 2e te3_vfs_find()].
496d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 .*/.SQLITE_API s
496e0 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 qlite3_vfs *sqli
496f0 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e te3_vfs_find(con
49700 73 74 20 63 68 61 72 20 2a 7a 56 66 73 4e 61 6d st char *zVfsNam
49710 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 e);.SQLITE_API i
49720 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 nt sqlite3_vfs_r
49730 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f egister(sqlite3_
49740 76 66 73 2a 2c 20 69 6e 74 20 6d 61 6b 65 44 66 vfs*, int makeDf
49750 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 lt);.SQLITE_API
49760 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f int sqlite3_vfs_
49770 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 unregister(sqlit
49780 65 33 5f 76 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a e3_vfs*);../*.**
49790 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 CAPI3REF: Mutex
497a0 65 73 20 7b 48 31 37 30 30 30 7d 20 3c 53 32 30 es {H17000} <S20
497b0 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 000>.**.** The S
497c0 51 4c 69 74 65 20 63 6f 72 65 20 75 73 65 73 20 QLite core uses
497d0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66 these routines f
497e0 6f 72 20 74 68 72 65 61 64 0a 2a 2a 20 73 79 6e or thread.** syn
497f0 63 68 72 6f 6e 69 7a 61 74 69 6f 6e 2e 20 54 68 chronization. Th
49800 6f 75 67 68 20 74 68 65 79 20 61 72 65 20 69 6e ough they are in
49810 74 65 6e 64 65 64 20 66 6f 72 20 69 6e 74 65 72 tended for inter
49820 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 nal.** use by SQ
49830 4c 69 74 65 2c 20 63 6f 64 65 20 74 68 61 74 20 Lite, code that
49840 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20 53 51 links against SQ
49850 4c 69 74 65 20 69 73 0a 2a 2a 20 70 65 72 6d 69 Lite is.** permi
49860 74 74 65 64 20 74 6f 20 75 73 65 20 61 6e 79 20 tted to use any
49870 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 of these routine
49880 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c s..**.** The SQL
49890 69 74 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 ite source code
498a0 63 6f 6e 74 61 69 6e 73 20 6d 75 6c 74 69 70 6c contains multipl
498b0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
498c0 73 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 6d 75 s.** of these mu
498d0 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 20 20 41 tex routines. A
498e0 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6d n appropriate im
498f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 plementation.**
49900 69 73 20 73 65 6c 65 63 74 65 64 20 61 75 74 6f is selected auto
49910 6d 61 74 69 63 61 6c 6c 79 20 61 74 20 63 6f 6d matically at com
49920 70 69 6c 65 2d 74 69 6d 65 2e 20 20 54 68 65 20 pile-time. The
49930 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 69 6d 70 following.** imp
49940 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 72 65 lementations are
49950 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 available in th
49960 65 20 53 51 4c 69 74 65 20 63 6f 72 65 3a 0a 2a e SQLite core:.*
49970 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
49980 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 > SQLITE_MUTEX
49990 5f 4f 53 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 _OS2.** <li> S
499a0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 QLITE_MUTEX_PTHR
499b0 45 41 44 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 EAD.** <li> SQ
499c0 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 2a LITE_MUTEX_W32.*
499d0 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f * <li> SQLITE_
499e0 4d 55 54 45 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f MUTEX_NOOP.** </
499f0 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 ul>.**.** The SQ
49a00 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 LITE_MUTEX_NOOP
49a10 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 implementation i
49a20 73 20 61 20 73 65 74 20 6f 66 20 72 6f 75 74 69 s a set of routi
49a30 6e 65 73 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 nes.** that does
49a40 20 6e 6f 20 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 no real locking
49a50 20 61 6e 64 20 69 73 20 61 70 70 72 6f 70 72 69 and is appropri
49a60 61 74 65 20 66 6f 72 20 75 73 65 20 69 6e 0a 2a ate for use in.*
49a70 2a 20 61 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 * a single-threa
49a80 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e ded application.
49a90 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 The SQLITE_MUT
49aa0 45 58 5f 4f 53 32 2c 0a 2a 2a 20 53 51 4c 49 54 EX_OS2,.** SQLIT
49ab0 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 2c E_MUTEX_PTHREAD,
49ac0 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 and SQLITE_MUTE
49ad0 58 5f 57 33 32 20 69 6d 70 6c 65 6d 65 6e 74 61 X_W32 implementa
49ae0 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 70 70 tions.** are app
49af0 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 ropriate for use
49b00 20 6f 6e 20 4f 53 2f 32 2c 20 55 6e 69 78 2c 20 on OS/2, Unix,
49b10 61 6e 64 20 57 69 6e 64 6f 77 73 2e 0a 2a 2a 0a and Windows..**.
49b20 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 ** If SQLite is
49b30 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 compiled with th
49b40 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 e SQLITE_MUTEX_A
49b50 50 50 44 45 46 20 70 72 65 70 72 6f 63 65 73 73 PPDEF preprocess
49b60 6f 72 0a 2a 2a 20 6d 61 63 72 6f 20 64 65 66 69 or.** macro defi
49b70 6e 65 64 20 28 77 69 74 68 20 22 2d 44 53 51 4c ned (with "-DSQL
49b80 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 ITE_MUTEX_APPDEF
49b90 3d 31 22 29 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 =1"), then no mu
49ba0 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 tex.** implement
49bb0 61 74 69 6f 6e 20 69 73 20 69 6e 63 6c 75 64 65 ation is include
49bc0 64 20 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 d with the libra
49bd0 72 79 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ry. In this case
49be0 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 the.** applicat
49bf0 69 6f 6e 20 6d 75 73 74 20 73 75 70 70 6c 79 20 ion must supply
49c00 61 20 63 75 73 74 6f 6d 20 6d 75 74 65 78 20 69 a custom mutex i
49c10 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 mplementation us
49c20 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 ing the.** [SQLI
49c30 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d TE_CONFIG_MUTEX]
49c40 20 6f 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 option of the s
49c50 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 qlite3_config()
49c60 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f function.** befo
49c70 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 re calling sqlit
49c80 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
49c90 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 75 62 or any other pub
49ca0 6c 69 63 20 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 lic sqlite3_.**
49cb0 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 63 61 function that ca
49cc0 6c 6c 73 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 lls sqlite3_init
49cd0 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 ialize()..**.**
49ce0 7b 48 31 37 30 31 31 7d 20 54 68 65 20 73 71 6c {H17011} The sql
49cf0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
49d00 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 () routine alloc
49d10 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 ates a new.** mu
49d20 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 tex and returns
49d30 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
49d40 20 7b 48 31 37 30 31 32 7d 20 49 66 20 69 74 20 {H17012} If it
49d50 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 returns NULL.**
49d60 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 that means that
49d70 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f a mutex could no
49d80 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 t be allocated.
49d90 7b 48 31 37 30 31 33 7d 20 53 51 4c 69 74 65 0a {H17013} SQLite.
49da0 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 ** will unwind i
49db0 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 ts stack and ret
49dc0 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 7b 48 urn an error. {H
49dd0 31 37 30 31 34 7d 20 54 68 65 20 61 72 67 75 6d 17014} The argum
49de0 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 ent.** to sqlite
49df0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 3_mutex_alloc()
49e00 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 is one of these
49e10 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 integer constant
49e20 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a s:.**.** <ul>.**
49e30 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
49e40 54 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e TEX_FAST.** <li>
49e50 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 SQLITE_MUTEX_R
49e60 45 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e ECURSIVE.** <li>
49e70 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
49e80 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 TATIC_MASTER.**
49e90 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
49ea0 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a EX_STATIC_MEM.**
49eb0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
49ec0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a TEX_STATIC_MEM2.
49ed0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
49ee0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e MUTEX_STATIC_PRN
49ef0 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 G.** <li> SQLIT
49f00 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c E_MUTEX_STATIC_L
49f10 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 RU.** <li> SQLI
49f20 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
49f30 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a LRU2.** </ul>.**
49f40 0a 2a 2a 20 7b 48 31 37 30 31 35 7d 20 54 68 65 .** {H17015} The
49f50 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 first two const
49f60 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 ants cause sqlit
49f70 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
49f80 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 to create.** a
49f90 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 new mutex. The
49fa0 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 new mutex is rec
49fb0 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 ursive when SQLI
49fc0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
49fd0 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 VE.** is used bu
49fe0 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c t not necessaril
49ff0 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 y so when SQLITE
4a000 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 _MUTEX_FAST is u
4a010 73 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68 sed. {END}.** Th
4a020 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e e mutex implemen
4a030 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 tation does not
4a040 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 need to make a d
4a050 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 istinction.** be
4a060 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 tween SQLITE_MUT
4a070 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 EX_RECURSIVE and
4a080 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
4a090 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a ST if it does.**
4a0a0 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 7b not want to. {
4a0b0 48 31 37 30 31 36 7d 20 42 75 74 20 53 51 4c 69 H17016} But SQLi
4a0c0 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 te will only req
4a0d0 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65 uest a recursive
4a0e0 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 mutex in.** cas
4a0f0 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c es where it real
4a100 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 7b ly needs one. {
4a110 45 4e 44 7d 20 49 66 20 61 20 66 61 73 74 65 72 END} If a faster
4a120 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d non-recursive m
4a130 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e utex.** implemen
4a140 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 tation is availa
4a150 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 ble on the host
4a160 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 platform, the mu
4a170 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a tex subsystem.**
4a180 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 75 might return su
4a190 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65 ch a mutex in re
4a1a0 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 sponse to SQLITE
4a1b0 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a _MUTEX_FAST..**.
4a1c0 2a 2a 20 7b 48 31 37 30 31 37 7d 20 54 68 65 20 ** {H17017} The
4a1d0 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 other allowed pa
4a1e0 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 rameters to sqli
4a1f0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
4a200 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a ) each return.**
4a210 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
4a220 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69 static preexisti
4a230 6e 67 20 6d 75 74 65 78 2e 20 7b 45 4e 44 7d 20 ng mutex. {END}
4a240 20 46 6f 75 72 20 73 74 61 74 69 63 20 6d 75 74 Four static mut
4a250 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 exes are.** used
4a260 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 by the current
4a270 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 version of SQLit
4a280 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 e. Future versi
4a290 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a ons of SQLite.**
4a2a0 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f may add additio
4a2b0 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 nal static mutex
4a2c0 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 es. Static mute
4a2d0 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 xes are for inte
4a2e0 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 rnal.** use by S
4a2f0 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 QLite only. App
4a300 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 lications that u
4a310 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 se SQLite mutexe
4a320 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 s should.** use
4a330 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 only the dynamic
4a340 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 mutexes returne
4a350 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 d by SQLITE_MUTE
4a360 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c X_FAST or.** SQL
4a370 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
4a380 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 IVE..**.** {H170
4a390 31 38 7d 20 4e 6f 74 65 20 74 68 61 74 20 69 66 18} Note that if
4a3a0 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 one of the dyna
4a3b0 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 mic mutex parame
4a3c0 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 ters (SQLITE_MUT
4a3d0 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 EX_FAST.** or SQ
4a3e0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
4a3f0 53 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68 SIVE) is used th
4a400 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 en sqlite3_mutex
4a410 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 _alloc().** retu
4a420 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 rns a different
4a430 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 mutex on every c
4a440 61 6c 6c 2e 20 20 7b 48 31 37 30 33 34 7d 20 42 all. {H17034} B
4a450 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69 ut for the stati
4a460 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 c.** mutex types
4a470 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 , the same mutex
4a480 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
4a490 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 every call that
4a4a0 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 has.** the same
4a4b0 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a type number..**.
4a4c0 2a 2a 20 7b 48 31 37 30 31 39 7d 20 54 68 65 20 ** {H17019} The
4a4d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 sqlite3_mutex_fr
4a4e0 65 65 28 29 20 72 6f 75 74 69 6e 65 20 64 65 61 ee() routine dea
4a4f0 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 llocates a previ
4a500 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 ously.** allocat
4a510 65 64 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 ed dynamic mutex
4a520 2e 20 7b 48 31 37 30 32 30 7d 20 53 51 4c 69 74 . {H17020} SQLit
4a530 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 e is careful to
4a540 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 deallocate every
4a550 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 .** dynamic mute
4a560 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 x that it alloca
4a570 74 65 73 2e 20 7b 41 31 37 30 32 31 7d 20 54 68 tes. {A17021} Th
4a580 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 e dynamic mutexe
4a590 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 69 6e s must not be in
4a5a0 0a 2a 2a 20 75 73 65 20 77 68 65 6e 20 74 68 65 .** use when the
4a5b0 79 20 61 72 65 20 64 65 61 6c 6c 6f 63 61 74 65 y are deallocate
4a5c0 64 2e 20 7b 41 31 37 30 32 32 7d 20 41 74 74 65 d. {A17022} Atte
4a5d0 6d 70 74 69 6e 67 20 74 6f 20 64 65 61 6c 6c 6f mpting to deallo
4a5e0 63 61 74 65 20 61 20 73 74 61 74 69 63 0a 2a 2a cate a static.**
4a5f0 20 6d 75 74 65 78 20 72 65 73 75 6c 74 73 20 69 mutex results i
4a600 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61 n undefined beha
4a610 76 69 6f 72 2e 20 7b 48 31 37 30 32 33 7d 20 53 vior. {H17023} S
4a620 51 4c 69 74 65 20 6e 65 76 65 72 20 64 65 61 6c QLite never deal
4a630 6c 6f 63 61 74 65 73 0a 2a 2a 20 61 20 73 74 61 locates.** a sta
4a640 74 69 63 20 6d 75 74 65 78 2e 20 7b 45 4e 44 7d tic mutex. {END}
4a650 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
4a660 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
4a670 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
4a680 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 ex_try() routine
4a690 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 s attempt.** to
4a6a0 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 7b enter a mutex. {
4a6b0 48 31 37 30 32 34 7d 20 49 66 20 61 6e 6f 74 68 H17024} If anoth
4a6c0 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 er thread is alr
4a6d0 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 eady within the
4a6e0 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 mutex,.** sqlite
4a6f0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 3_mutex_enter()
4a700 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 will block and s
4a710 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
4a720 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a () will return.*
4a730 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 7b * SQLITE_BUSY. {
4a740 48 31 37 30 32 35 7d 20 20 54 68 65 20 73 71 6c H17025} The sql
4a750 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
4a760 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
4a770 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a ns [SQLITE_OK].*
4a780 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 * upon successfu
4a790 6c 20 65 6e 74 72 79 2e 20 20 7b 48 31 37 30 32 l entry. {H1702
4a7a0 36 7d 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 6} Mutexes creat
4a7b0 65 64 20 75 73 69 6e 67 0a 2a 2a 20 53 51 4c 49 ed using.** SQLI
4a7c0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
4a7d0 56 45 20 63 61 6e 20 62 65 20 65 6e 74 65 72 65 VE can be entere
4a7e0 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 d multiple times
4a7f0 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 by the same thr
4a800 65 61 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 37 7d ead..** {H17027}
4a810 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 In such cases t
4a820 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 he,.** mutex mus
4a830 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 t be exited an e
4a840 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 qual number of t
4a850 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 imes before anot
4a860 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 her thread.** ca
4a870 6e 20 65 6e 74 65 72 2e 20 20 7b 41 31 37 30 32 n enter. {A1702
4a880 38 7d 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 8} If the same t
4a890 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 hread tries to e
4a8a0 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 0a 2a nter any other.*
4a8b0 2a 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 20 * kind of mutex
4a8c0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 more than once,
4a8d0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 the behavior is
4a8e0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 7b 48 undefined..** {H
4a8f0 31 37 30 32 39 7d 20 53 51 4c 69 74 65 20 77 69 17029} SQLite wi
4a900 6c 6c 20 6e 65 76 65 72 20 65 78 68 69 62 69 74 ll never exhibit
4a910 0a 2a 2a 20 73 75 63 68 20 62 65 68 61 76 69 6f .** such behavio
4a920 72 20 69 6e 20 69 74 73 20 6f 77 6e 20 75 73 65 r in its own use
4a930 20 6f 66 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a of mutexes..**.
4a940 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 ** Some systems
4a950 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 57 69 (for example, Wi
4a960 6e 64 6f 77 73 20 39 35 29 20 64 6f 20 6e 6f 74 ndows 95) do not
4a970 20 73 75 70 70 6f 72 74 20 74 68 65 20 6f 70 65 support the ope
4a980 72 61 74 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d ration.** implem
4a990 65 6e 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 ented by sqlite3
4a9a0 5f 6d 75 74 65 78 5f 74 72 79 28 29 2e 20 20 4f _mutex_try(). O
4a9b0 6e 20 74 68 6f 73 65 20 73 79 73 74 65 6d 73 2c n those systems,
4a9c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
4a9d0 72 79 28 29 0a 2a 2a 20 77 69 6c 6c 20 61 6c 77 ry().** will alw
4a9e0 61 79 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ays return SQLIT
4a9f0 45 5f 42 55 53 59 2e 20 20 7b 48 31 37 30 33 30 E_BUSY. {H17030
4aa00 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 } The SQLite cor
4aa10 65 20 6f 6e 6c 79 20 65 76 65 72 20 75 73 65 73 e only ever uses
4aa20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
4aa30 78 5f 74 72 79 28 29 20 61 73 20 61 6e 20 6f 70 x_try() as an op
4aa40 74 69 6d 69 7a 61 74 69 6f 6e 20 73 6f 20 74 68 timization so th
4aa50 69 73 20 69 73 20 61 63 63 65 70 74 61 62 6c 65 is is acceptable
4aa60 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a behavior..**.**
4aa70 20 7b 48 31 37 30 33 31 7d 20 54 68 65 20 73 71 {H17031} The sq
4aa80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
4aa90 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 e() routine exit
4aaa0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 s a mutex that w
4aab0 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 as.** previously
4aac0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 entered by the
4aad0 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 7b 41 same thread. {A
4aae0 31 37 30 33 32 7d 20 54 68 65 20 62 65 68 61 76 17032} The behav
4aaf0 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 ior.** is undefi
4ab00 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 ned if the mutex
4ab10 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
4ab20 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 y entered by the
4ab30 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 .** calling thre
4ab40 61 64 20 6f 72 20 69 73 20 6e 6f 74 20 63 75 72 ad or is not cur
4ab50 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 rently allocated
4ab60 2e 20 20 7b 48 31 37 30 33 33 7d 20 53 51 4c 69 . {H17033} SQLi
4ab70 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 te will.** never
4ab80 20 64 6f 20 65 69 74 68 65 72 2e 20 7b 45 4e 44 do either. {END
4ab90 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 }.**.** If the a
4aba0 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
4abb0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
4abc0 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f , sqlite3_mutex_
4abd0 74 72 79 28 29 2c 20 6f 72 0a 2a 2a 20 73 71 6c try(), or.** sql
4abe0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
4abf0 28 29 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 () is a NULL poi
4ac00 6e 74 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20 74 nter, then all t
4ac10 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a hree routines.**
4ac20 20 62 65 68 61 76 65 20 61 73 20 6e 6f 2d 6f 70 behave as no-op
4ac30 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 s..**.** See als
4ac40 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 o: [sqlite3_mute
4ac50 78 5f 68 65 6c 64 28 29 5d 20 61 6e 64 20 5b 73 x_held()] and [s
4ac60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
4ac70 68 65 6c 64 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 held()]..*/.SQLI
4ac80 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d TE_API sqlite3_m
4ac90 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75 utex *sqlite3_mu
4aca0 74 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a tex_alloc(int);.
4acb0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
4acc0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 sqlite3_mutex_fr
4acd0 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ee(sqlite3_mutex
4ace0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 *);.SQLITE_API v
4acf0 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 oid sqlite3_mute
4ad00 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f x_enter(sqlite3_
4ad10 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f mutex*);.SQLITE_
4ad20 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
4ad30 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65 mutex_try(sqlite
4ad40 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 3_mutex*);.SQLIT
4ad50 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
4ad60 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 e3_mutex_leave(s
4ad70 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a qlite3_mutex*);.
4ad80 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
4ad90 20 4d 75 74 65 78 20 4d 65 74 68 6f 64 73 20 4f Mutex Methods O
4ada0 62 6a 65 63 74 20 7b 48 31 37 31 32 30 7d 20 3c bject {H17120} <
4adb0 53 32 30 31 33 30 3e 0a 2a 2a 20 45 58 50 45 52 S20130>.** EXPER
4adc0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e IMENTAL.**.** An
4add0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
4ade0 73 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69 s structure defi
4adf0 6e 65 73 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 nes the low-leve
4ae00 6c 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 75 73 l routines.** us
4ae10 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 ed to allocate a
4ae20 6e 64 20 75 73 65 20 6d 75 74 65 78 65 73 2e 0a nd use mutexes..
4ae30 2a 2a 0a 2a 2a 20 55 73 75 61 6c 6c 79 2c 20 74 **.** Usually, t
4ae40 68 65 20 64 65 66 61 75 6c 74 20 6d 75 74 65 78 he default mutex
4ae50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
4ae60 20 70 72 6f 76 69 64 65 64 20 62 79 20 53 51 4c provided by SQL
4ae70 69 74 65 20 61 72 65 0a 2a 2a 20 73 75 66 66 69 ite are.** suffi
4ae80 63 69 65 6e 74 2c 20 68 6f 77 65 76 65 72 20 74 cient, however t
4ae90 68 65 20 75 73 65 72 20 68 61 73 20 74 68 65 20 he user has the
4aea0 6f 70 74 69 6f 6e 20 6f 66 20 73 75 62 73 74 69 option of substi
4aeb0 74 75 74 69 6e 67 20 61 20 63 75 73 74 6f 6d 0a tuting a custom.
4aec0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
4aed0 6e 20 66 6f 72 20 73 70 65 63 69 61 6c 69 7a 65 n for specialize
4aee0 64 20 64 65 70 6c 6f 79 6d 65 6e 74 73 20 6f 72 d deployments or
4aef0 20 73 79 73 74 65 6d 73 20 66 6f 72 20 77 68 69 systems for whi
4af00 63 68 20 53 51 4c 69 74 65 0a 2a 2a 20 64 6f 65 ch SQLite.** doe
4af10 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 20 s not provide a
4af20 73 75 69 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 suitable impleme
4af30 6e 74 61 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 ntation. In this
4af40 20 63 61 73 65 2c 20 74 68 65 20 75 73 65 72 0a case, the user.
4af50 2a 2a 20 63 72 65 61 74 65 73 20 61 6e 64 20 70 ** creates and p
4af60 6f 70 75 6c 61 74 65 73 20 61 6e 20 69 6e 73 74 opulates an inst
4af70 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
4af80 75 63 74 75 72 65 20 74 6f 20 70 61 73 73 0a 2a ucture to pass.*
4af90 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e * to sqlite3_con
4afa0 66 69 67 28 29 20 61 6c 6f 6e 67 20 77 69 74 68 fig() along with
4afb0 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e the [SQLITE_CON
4afc0 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f FIG_MUTEX] optio
4afd0 6e 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c n..** Additional
4afe0 6c 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 ly, an instance
4aff0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
4b000 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 e can be used as
4b010 20 61 6e 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 an.** output va
4b020 72 69 61 62 6c 65 20 77 68 65 6e 20 71 75 65 72 riable when quer
4b030 79 69 6e 67 20 74 68 65 20 73 79 73 74 65 6d 20 ying the system
4b040 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 for the current
4b050 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 mutex.** impleme
4b060 6e 74 61 74 69 6f 6e 2c 20 75 73 69 6e 67 20 74 ntation, using t
4b070 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 he [SQLITE_CONFI
4b080 47 5f 47 45 54 4d 55 54 45 58 5d 20 6f 70 74 69 G_GETMUTEX] opti
4b090 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d on..**.** The xM
4b0a0 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 utexInit method
4b0b0 64 65 66 69 6e 65 64 20 62 79 20 74 68 69 73 20 defined by this
4b0c0 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 76 structure is inv
4b0d0 6f 6b 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 oked as.** part
4b0e0 6f 66 20 73 79 73 74 65 6d 20 69 6e 69 74 69 61 of system initia
4b0f0 6c 69 7a 61 74 69 6f 6e 20 62 79 20 74 68 65 20 lization by the
4b100 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
4b110 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a ze() function..*
4b120 2a 20 7b 48 31 37 30 30 31 7d 20 54 68 65 20 78 * {H17001} The x
4b130 4d 75 74 65 78 49 6e 69 74 20 72 6f 75 74 69 6e MutexInit routin
4b140 65 20 73 68 61 6c 6c 20 62 65 20 63 61 6c 6c 65 e shall be calle
4b150 64 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 63 65 d by SQLite once
4b160 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 65 66 66 for each.** eff
4b170 65 63 74 69 76 65 20 63 61 6c 6c 20 74 6f 20 5b ective call to [
4b180 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
4b190 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ze()]..**.** The
4b1a0 20 78 4d 75 74 65 78 45 6e 64 20 6d 65 74 68 6f xMutexEnd metho
4b1b0 64 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 69 d defined by thi
4b1c0 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69 s structure is i
4b1d0 6e 76 6f 6b 65 64 20 61 73 0a 2a 2a 20 70 61 72 nvoked as.** par
4b1e0 74 20 6f 66 20 73 79 73 74 65 6d 20 73 68 75 74 t of system shut
4b1f0 64 6f 77 6e 20 62 79 20 74 68 65 20 73 71 6c 69 down by the sqli
4b200 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 66 te3_shutdown() f
4b210 75 6e 63 74 69 6f 6e 2e 20 54 68 65 0a 2a 2a 20 unction. The.**
4b220 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
4b230 66 20 74 68 69 73 20 6d 65 74 68 6f 64 20 69 73 f this method is
4b240 20 65 78 70 65 63 74 65 64 20 74 6f 20 72 65 6c expected to rel
4b250 65 61 73 65 20 61 6c 6c 20 6f 75 74 73 74 61 6e ease all outstan
4b260 64 69 6e 67 0a 2a 2a 20 72 65 73 6f 75 72 63 65 ding.** resource
4b270 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 s obtained by th
4b280 65 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 20 e mutex methods
4b290 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
4b2a0 65 73 70 65 63 69 61 6c 6c 79 0a 2a 2a 20 74 68 especially.** th
4b2b0 6f 73 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 ose obtained by
4b2c0 74 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20 6d the xMutexInit m
4b2d0 65 74 68 6f 64 2e 20 7b 48 31 37 30 30 33 7d 20 ethod. {H17003}
4b2e0 54 68 65 20 78 4d 75 74 65 78 45 6e 64 28 29 0a The xMutexEnd().
4b2f0 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 ** interface sha
4b300 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 6f 6e ll be invoked on
4b310 63 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c ce for each call
4b320 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 to [sqlite3_shu
4b330 74 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 tdown()]..**.**
4b340 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 73 65 The remaining se
4b350 76 65 6e 20 6d 65 74 68 6f 64 73 20 64 65 66 69 ven methods defi
4b360 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 ned by this stru
4b370 63 74 75 72 65 20 28 78 4d 75 74 65 78 41 6c 6c cture (xMutexAll
4b380 6f 63 2c 0a 2a 2a 20 78 4d 75 74 65 78 46 72 65 oc,.** xMutexFre
4b390 65 2c 20 78 4d 75 74 65 78 45 6e 74 65 72 2c 20 e, xMutexEnter,
4b3a0 78 4d 75 74 65 78 54 72 79 2c 20 78 4d 75 74 65 xMutexTry, xMute
4b3b0 78 4c 65 61 76 65 2c 20 78 4d 75 74 65 78 48 65 xLeave, xMutexHe
4b3c0 6c 64 20 61 6e 64 0a 2a 2a 20 78 4d 75 74 65 78 ld and.** xMutex
4b3d0 4e 6f 74 68 65 6c 64 29 20 69 6d 70 6c 65 6d 65 Notheld) impleme
4b3e0 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 nt the following
4b3f0 20 69 6e 74 65 72 66 61 63 65 73 20 28 72 65 73 interfaces (res
4b400 70 65 63 74 69 76 65 6c 79 29 3a 0a 2a 2a 0a 2a pectively):.**.*
4b410 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e * <ul>.** <li>
4b420 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 [sqlite3_mutex
4b430 5f 61 6c 6c 6f 63 28 29 5d 20 3c 2f 6c 69 3e 0a _alloc()] </li>.
4b440 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 ** <li> [sqli
4b450 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 te3_mutex_free()
4b460 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 ] </li>.** <li
4b470 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 > [sqlite3_mute
4b480 78 5f 65 6e 74 65 72 28 29 5d 20 3c 2f 6c 69 3e x_enter()] </li>
4b490 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c .** <li> [sql
4b4a0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
4b4b0 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 ] </li>.** <li
4b4c0 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 > [sqlite3_mute
4b4d0 78 5f 6c 65 61 76 65 28 29 5d 20 3c 2f 6c 69 3e x_leave()] </li>
4b4e0 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c .** <li> [sql
4b4f0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
4b500 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c )] </li>.** <l
4b510 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 i> [sqlite3_mut
4b520 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 20 3c 2f ex_notheld()] </
4b530 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a li>.** </ul>.**.
4b540 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 ** The only diff
4b550 65 72 65 6e 63 65 20 69 73 20 74 68 61 74 20 74 erence is that t
4b560 68 65 20 70 75 62 6c 69 63 20 73 71 6c 69 74 65 he public sqlite
4b570 33 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 73 20 3_XXX functions
4b580 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a 20 61 62 enumerated.** ab
4b590 6f 76 65 20 73 69 6c 65 6e 74 6c 79 20 69 67 6e ove silently ign
4b5a0 6f 72 65 20 61 6e 79 20 69 6e 76 6f 63 61 74 69 ore any invocati
4b5b0 6f 6e 73 20 74 68 61 74 20 70 61 73 73 20 61 20 ons that pass a
4b5c0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 NULL pointer ins
4b5d0 74 65 61 64 0a 2a 2a 20 6f 66 20 61 20 76 61 6c tead.** of a val
4b5e0 69 64 20 6d 75 74 65 78 20 68 61 6e 64 6c 65 2e id mutex handle.
4b5f0 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 The implementat
4b600 69 6f 6e 73 20 6f 66 20 74 68 65 20 6d 65 74 68 ions of the meth
4b610 6f 64 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 62 ods defined.** b
4b620 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 y this structure
4b630 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69 72 65 are not require
4b640 64 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 69 73 d to handle this
4b650 20 63 61 73 65 2c 20 74 68 65 20 72 65 73 75 6c case, the resul
4b660 74 73 0a 2a 2a 20 6f 66 20 70 61 73 73 69 6e 67 ts.** of passing
4b670 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
4b680 69 6e 73 74 65 61 64 20 6f 66 20 61 20 76 61 6c instead of a val
4b690 69 64 20 6d 75 74 65 78 20 68 61 6e 64 6c 65 20 id mutex handle
4b6a0 61 72 65 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a are undefined.**
4b6b0 20 28 69 2e 65 2e 20 69 74 20 69 73 20 61 63 63 (i.e. it is acc
4b6c0 65 70 74 61 62 6c 65 20 74 6f 20 70 72 6f 76 69 eptable to provi
4b6d0 64 65 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 de an implementa
4b6e0 74 69 6f 6e 20 74 68 61 74 20 73 65 67 66 61 75 tion that segfau
4b6f0 6c 74 73 20 69 66 0a 2a 2a 20 69 74 20 69 73 20 lts if.** it is
4b700 70 61 73 73 65 64 20 61 20 4e 55 4c 4c 20 70 6f passed a NULL po
4b710 69 6e 74 65 72 29 2e 0a 2a 2f 0a 74 79 70 65 64 inter)..*/.typed
4b720 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
4b730 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
4b740 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
4b750 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 thods;.struct sq
4b760 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
4b770 6f 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 4d ods {. int (*xM
4b780 75 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 3b utexInit)(void);
4b790 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 45 . int (*xMutexE
4b7a0 6e 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c nd)(void);. sql
4b7b0 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 4d ite3_mutex *(*xM
4b7c0 75 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 3b utexAlloc)(int);
4b7d0 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 . void (*xMutex
4b7e0 46 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 Free)(sqlite3_mu
4b7f0 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 tex *);. void (
4b800 2a 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71 *xMutexEnter)(sq
4b810 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a lite3_mutex *);.
4b820 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72 int (*xMutexTr
4b830 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 y)(sqlite3_mutex
4b840 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d *);. void (*xM
4b850 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74 utexLeave)(sqlit
4b860 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 e3_mutex *);. i
4b870 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 29 nt (*xMutexHeld)
4b880 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
4b890 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 );. int (*xMute
4b8a0 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 65 xNotheld)(sqlite
4b8b0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 3_mutex *);.};..
4b8c0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
4b8d0 4d 75 74 65 78 20 56 65 72 69 66 69 63 61 74 69 Mutex Verificati
4b8e0 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 37 on Routines {H17
4b8f0 30 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 3c 53 080} <S20130> <S
4b900 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 30800>.**.** The
4b910 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
4b920 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 eld() and sqlite
4b930 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
4b940 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 ) routines.** ar
4b950 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 e intended for u
4b960 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 se inside assert
4b970 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 7b () statements. {
4b980 48 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c 69 H17081} The SQLi
4b990 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72 te core.** never
4b9a0 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74 uses these rout
4b9b0 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73 69 ines except insi
4b9c0 64 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 61 de an assert() a
4b9d0 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a nd applications.
4b9e0 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64 20 74 ** are advised t
4b9f0 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61 o follow the lea
4ba00 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20 20 d of the core.
4ba10 7b 48 31 37 30 38 32 7d 20 54 68 65 20 63 6f 72 {H17082} The cor
4ba20 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 e only.** provid
4ba30 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f es implementatio
4ba40 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f 75 ns for these rou
4ba50 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69 73 tines when it is
4ba60 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74 compiled.** wit
4ba70 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 42 h the SQLITE_DEB
4ba80 55 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 30 38 UG flag. {A1708
4ba90 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74 65 7} External mute
4baa0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
4bab0 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72 65 s.** are only re
4bac0 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64 quired to provid
4bad0 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 e these routines
4bae0 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 if SQLITE_DEBUG
4baf0 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 is.** defined a
4bb00 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 nd if NDEBUG is
4bb10 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a not defined..**.
4bb20 2a 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 65 73 ** {H17083} Thes
4bb30 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c e routines shoul
4bb40 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 d return true if
4bb50 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74 68 the mutex in th
4bb60 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 eir argument.**
4bb70 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 68 is held or not h
4bb80 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c eld, respectivel
4bb90 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e y, by the callin
4bba0 67 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 g thread..**.**
4bbb0 7b 58 31 37 30 38 34 7d 20 54 68 65 20 69 6d 70 {X17084} The imp
4bbc0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e lementation is n
4bbd0 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 ot required to p
4bbe0 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f 6e 73 rovided versions
4bbf0 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75 of these.** rou
4bc00 74 69 6e 65 73 20 74 68 61 74 20 61 63 74 75 61 tines that actua
4bc10 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 68 65 lly work. If the
4bc20 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
4bc30 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 does not provide
4bc40 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 72 73 working.** vers
4bc50 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 72 6f ions of these ro
4bc60 75 74 69 6e 65 73 2c 20 69 74 20 73 68 6f 75 6c utines, it shoul
4bc70 64 20 61 74 20 6c 65 61 73 74 20 70 72 6f 76 69 d at least provi
4bc80 64 65 20 73 74 75 62 73 20 74 68 61 74 20 61 6c de stubs that al
4bc90 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 20 74 ways.** return t
4bca0 72 75 65 20 73 6f 20 74 68 61 74 20 6f 6e 65 20 rue so that one
4bcb0 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 70 75 does not get spu
4bcc0 72 69 6f 75 73 20 61 73 73 65 72 74 69 6f 6e 20 rious assertion
4bcd0 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 failures..**.**
4bce0 7b 48 31 37 30 38 35 7d 20 49 66 20 74 68 65 20 {H17085} If the
4bcf0 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 argument to sqli
4bd00 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
4bd10 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 is a NULL point
4bd20 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 er then.** the r
4bd30 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 outine should re
4bd40 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d 20 54 turn 1. {END} T
4bd50 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74 65 his seems counte
4bd60 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e 63 r-intuitive sinc
4bd70 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 68 65 e.** clearly the
4bd80 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 62 65 mutex cannot be
4bd90 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f 65 73 held if it does
4bda0 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 75 74 not exist. But
4bdb0 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 61 73 the.** the reas
4bdc0 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f 65 on the mutex doe
4bdd0 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20 62 s not exist is b
4bde0 65 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 64 ecause the build
4bdf0 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67 is not.** using
4be00 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20 77 mutexes. And w
4be10 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 e do not want th
4be20 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74 61 e assert() conta
4be30 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 61 6c ining the.** cal
4be40 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 l to sqlite3_mut
4be50 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61 69 ex_held() to fai
4be60 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f l, so a non-zero
4be70 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 74 68 return is.** th
4be80 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 68 e appropriate th
4be90 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 31 37 ing to do. {H17
4bea0 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74 65 33 086} The sqlite3
4beb0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
4bec0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68 .** interface sh
4bed0 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72 6e ould also return
4bee0 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61 20 1 when given a
4bef0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f NULL pointer..*/
4bf00 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
4bf10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
4bf20 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
4bf30 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
4bf40 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nt sqlite3_mutex
4bf50 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 _notheld(sqlite3
4bf60 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _mutex*);../*.**
4bf70 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 CAPI3REF: Mutex
4bf80 20 54 79 70 65 73 20 7b 48 31 37 30 30 31 7d 20 Types {H17001}
4bf90 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <H17000>.**.** T
4bfa0 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 he [sqlite3_mute
4bfb0 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72 x_alloc()] inter
4bfc0 66 61 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e face takes a sin
4bfd0 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 gle argument.**
4bfe0 77 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20 which is one of
4bff0 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f these integer co
4c000 6e 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 nstants..**.** T
4c010 68 65 20 73 65 74 20 6f 66 20 73 74 61 74 69 63 he set of static
4c020 20 6d 75 74 65 78 65 73 20 6d 61 79 20 63 68 61 mutexes may cha
4c030 6e 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 4c nge from one SQL
4c040 69 74 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 ite release to t
4c050 68 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70 he.** next. App
4c060 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f lications that o
4c070 76 65 72 72 69 64 65 20 74 68 65 20 62 75 69 6c verride the buil
4c080 74 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63 t-in mutex logic
4c090 20 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 70 must be.** prep
4c0a0 61 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 ared to accommod
4c0b0 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 ate additional s
4c0c0 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 0a 2a tatic mutexes..*
4c0d0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
4c0e0 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 _MUTEX_FAST
4c0f0 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 0.#defin
4c100 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 e SQLITE_MUTEX_R
4c110 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 ECURSIVE
4c120 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
4c130 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
4c140 53 54 45 52 20 20 20 20 32 0a 23 64 65 66 69 6e STER 2.#defin
4c150 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
4c160 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 TATIC_MEM
4c170 33 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 61 3 /* sqlite3_ma
4c180 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e lloc() */.#defin
4c190 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
4c1a0 54 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 20 TATIC_MEM2
4c1b0 34 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72 65 4 /* sqlite3_re
4c1c0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 2a lease_memory() *
4c1d0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
4c1e0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 _MUTEX_STATIC_PR
4c1f0 4e 47 20 20 20 20 20 20 35 20 20 2f 2a 20 73 71 NG 5 /* sq
4c200 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 29 20 2a lite3_random() *
4c210 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
4c220 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
4c230 55 20 20 20 20 20 20 20 36 20 20 2f 2a 20 6c 72 U 6 /* lr
4c240 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 23 u page list */.#
4c250 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 define SQLITE_MU
4c260 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 TEX_STATIC_LRU2
4c270 20 20 20 20 20 37 20 20 2f 2a 20 6c 72 75 20 70 7 /* lru p
4c280 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a age list */../*.
4c290 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 ** CAPI3REF: Ret
4c2a0 72 69 65 76 65 20 74 68 65 20 6d 75 74 65 78 20 rieve the mutex
4c2b0 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 63 for a database c
4c2c0 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 37 30 30 onnection {H1700
4c2d0 32 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2} <H17000>.**.*
4c2e0 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
4c2f0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
4c300 65 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f er the [sqlite3_
4c310 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 20 74 68 mutex] object th
4c320 61 74 20 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 65 at .** serialize
4c330 73 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 s access to the
4c340 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
4c350 74 69 6f 6e 5d 20 67 69 76 65 6e 20 69 6e 20 74 tion] given in t
4c360 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 he argument.** w
4c370 68 65 6e 20 74 68 65 20 5b 74 68 72 65 61 64 69 hen the [threadi
4c380 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 65 72 69 ng mode] is Seri
4c390 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 74 68 alized..** If th
4c3a0 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 e [threading mod
4c3b0 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d 74 68 72 e] is Single-thr
4c3c0 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d 74 68 72 ead or Multi-thr
4c3d0 65 61 64 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a ead then this.**
4c3e0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
4c3f0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e a NULL pointer.
4c400 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 .*/.SQLITE_API s
4c410 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 qlite3_mutex *sq
4c420 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73 lite3_db_mutex(s
4c430 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a qlite3*);../*.**
4c440 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 77 2d 4c CAPI3REF: Low-L
4c450 65 76 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 20 evel Control Of
4c460 44 61 74 61 62 61 73 65 20 46 69 6c 65 73 20 7b Database Files {
4c470 48 31 31 33 30 30 7d 20 3c 53 33 30 38 30 30 3e H11300} <S30800>
4c480 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 31 7d 20 .**.** {H11301}
4c490 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c The [sqlite3_fil
4c4a0 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 e_control()] int
4c4b0 65 72 66 61 63 65 20 6d 61 6b 65 73 20 61 20 64 erface makes a d
4c4c0 69 72 65 63 74 20 63 61 6c 6c 20 74 6f 20 74 68 irect call to th
4c4d0 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f e.** xFileContro
4c4e0 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 65 l method for the
4c4f0 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 [sqlite3_io_met
4c500 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 73 73 hods] object ass
4c510 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 ociated.** with
4c520 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 a particular dat
4c530 61 62 61 73 65 20 69 64 65 6e 74 69 66 69 65 64 abase identified
4c540 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 by the second a
4c550 72 67 75 6d 65 6e 74 2e 20 7b 48 31 31 33 30 32 rgument. {H11302
4c560 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 } The.** name of
4c570 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
4c580 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e the name assign
4c590 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ed to the databa
4c5a0 73 65 20 62 79 20 74 68 65 0a 2a 2a 20 3c 61 20 se by the.** <a
4c5b0 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74 74 61 63 href="lang_attac
4c5c0 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43 48 3c 2f h.html">ATTACH</
4c5d0 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 a> SQL command t
4c5e0 68 61 74 20 6f 70 65 6e 65 64 20 74 68 65 0a 2a hat opened the.*
4c5f0 2a 20 64 61 74 61 62 61 73 65 2e 20 7b 48 31 31 * database. {H11
4c600 33 30 33 7d 20 54 6f 20 63 6f 6e 74 72 6f 6c 20 303} To control
4c610 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
4c620 65 20 66 69 6c 65 2c 20 75 73 65 20 74 68 65 20 e file, use the
4c630 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a 20 6f name "main".** o
4c640 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 r a NULL pointer
4c650 2e 20 7b 48 31 31 33 30 34 7d 20 54 68 65 20 74 . {H11304} The t
4c660 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 hird and fourth
4c670 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 parameters to th
4c680 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 72 is routine.** ar
4c690 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74 6c e passed directl
4c6a0 79 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 y through to the
4c6b0 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 second and thir
4c6c0 64 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 0a d parameters of.
4c6d0 2a 2a 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 ** the xFileCont
4c6e0 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20 7b 48 31 rol method. {H1
4c6f0 31 33 30 35 7d 20 54 68 65 20 72 65 74 75 72 6e 1305} The return
4c700 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 78 46 value of the xF
4c710 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 6d 65 ileControl.** me
4c720 74 68 6f 64 20 62 65 63 6f 6d 65 73 20 74 68 65 thod becomes the
4c730 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 return value of
4c740 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
4c750 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 7d 20 49 66 *.** {H11306} If
4c760 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
4c770 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d 65 29 20 meter (zDbName)
4c780 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 does not match t
4c790 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 79 0a 2a he name of any.*
4c7a0 2a 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 * open database
4c7b0 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 file, then SQLIT
4c7c0 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 E_ERROR is retur
4c7d0 6e 65 64 2e 20 7b 48 31 31 33 30 37 7d 20 54 68 ned. {H11307} Th
4c7e0 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 is error.** code
4c7f0 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d 62 65 72 is not remember
4c800 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 ed and will not
4c810 62 65 20 72 65 63 61 6c 6c 65 64 20 62 79 20 5b be recalled by [
4c820 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 sqlite3_errcode(
4c830 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 )].** or [sqlite
4c840 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20 7b 41 31 3_errmsg()]. {A1
4c850 31 33 30 38 7d 20 54 68 65 20 75 6e 64 65 72 6c 1308} The underl
4c860 79 69 6e 67 20 78 46 69 6c 65 43 6f 6e 74 72 6f ying xFileContro
4c870 6c 20 6d 65 74 68 6f 64 20 6d 69 67 68 74 0a 2a l method might.*
4c880 2a 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 53 51 * also return SQ
4c890 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 7b 41 31 LITE_ERROR. {A1
4c8a0 31 33 30 39 7d 20 54 68 65 72 65 20 69 73 20 6e 1309} There is n
4c8b0 6f 20 77 61 79 20 74 6f 20 64 69 73 74 69 6e 67 o way to disting
4c8c0 75 69 73 68 20 62 65 74 77 65 65 6e 0a 2a 2a 20 uish between.**
4c8d0 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 7a 44 62 an incorrect zDb
4c8e0 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53 51 4c 49 Name and an SQLI
4c8f0 54 45 5f 45 52 52 4f 52 20 72 65 74 75 72 6e 20 TE_ERROR return
4c900 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 from the underly
4c910 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 ing.** xFileCont
4c920 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 7b 45 4e 44 rol method. {END
4c930 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f }.**.** See also
4c940 3a 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f : [SQLITE_FCNTL_
4c950 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a 53 51 LOCKSTATE].*/.SQ
4c960 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
4c970 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f ite3_file_contro
4c980 6c 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 l(sqlite3*, cons
4c990 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c t char *zDbName,
4c9a0 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 2a 29 3b int op, void*);
4c9b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
4c9c0 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65 72 66 : Testing Interf
4c9d0 61 63 65 20 7b 48 31 31 34 30 30 7d 20 3c 53 33 ace {H11400} <S3
4c9e0 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0800>.**.** The
4c9f0 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e sqlite3_test_con
4ca00 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 65 trol() interface
4ca10 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 is used to read
4ca20 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a out internal.**
4ca30 20 73 74 61 74 65 20 6f 66 20 53 51 4c 69 74 65 state of SQLite
4ca40 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63 74 20 66 and to inject f
4ca50 61 75 6c 74 73 20 69 6e 74 6f 20 53 51 4c 69 74 aults into SQLit
4ca60 65 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a e for testing.**
4ca70 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68 65 20 purposes. The
4ca80 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
4ca90 69 73 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 is an operation
4caa0 63 6f 64 65 20 74 68 61 74 20 64 65 74 65 72 6d code that determ
4cab0 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 ines.** the numb
4cac0 65 72 2c 20 6d 65 61 6e 69 6e 67 2c 20 61 6e 64 er, meaning, and
4cad0 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 61 6c operation of al
4cae0 6c 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 l subsequent par
4caf0 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 ameters..**.** T
4cb00 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 his interface is
4cb10 20 6e 6f 74 20 66 6f 72 20 75 73 65 20 62 79 20 not for use by
4cb20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 49 applications. I
4cb30 74 20 65 78 69 73 74 73 20 73 6f 6c 65 6c 79 0a t exists solely.
4cb40 2a 2a 20 66 6f 72 20 76 65 72 69 66 79 69 6e 67 ** for verifying
4cb50 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 the correct ope
4cb60 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 ration of the SQ
4cb70 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 44 Lite library. D
4cb80 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 68 epending.** on h
4cb90 6f 77 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 ow the SQLite li
4cba0 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 brary is compile
4cbb0 64 2c 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 d, this interfac
4cbc0 65 20 6d 69 67 68 74 20 6e 6f 74 20 65 78 69 73 e might not exis
4cbd0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 t..**.** The det
4cbe0 61 69 6c 73 20 6f 66 20 74 68 65 20 6f 70 65 72 ails of the oper
4cbf0 61 74 69 6f 6e 20 63 6f 64 65 73 2c 20 74 68 65 ation codes, the
4cc00 69 72 20 6d 65 61 6e 69 6e 67 73 2c 20 74 68 65 ir meanings, the
4cc10 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 parameters.** t
4cc20 68 65 79 20 74 61 6b 65 2c 20 61 6e 64 20 77 68 hey take, and wh
4cc30 61 74 20 74 68 65 79 20 64 6f 20 61 72 65 20 61 at they do are a
4cc40 6c 6c 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 ll subject to ch
4cc50 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 ange without not
4cc60 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d ice..** Unlike m
4cc70 6f 73 74 20 6f 66 20 74 68 65 20 53 51 4c 69 74 ost of the SQLit
4cc80 65 20 41 50 49 2c 20 74 68 69 73 20 66 75 6e 63 e API, this func
4cc90 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 75 61 72 tion is not guar
4cca0 61 6e 74 65 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 anteed to.** ope
4ccb0 72 61 74 65 20 63 6f 6e 73 69 73 74 65 6e 74 6c rate consistentl
4ccc0 79 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 61 y from one relea
4ccd0 73 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a se to the next..
4cce0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
4ccf0 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 t sqlite3_test_c
4cd00 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e ontrol(int op, .
4cd10 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ..);../*.** CAPI
4cd20 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 3REF: Testing In
4cd30 74 65 72 66 61 63 65 20 4f 70 65 72 61 74 69 6f terface Operatio
4cd40 6e 20 43 6f 64 65 73 20 7b 48 31 31 34 31 30 7d n Codes {H11410}
4cd50 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 <H11400>.**.**
4cd60 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 These constants
4cd70 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 6f 70 are the valid op
4cd80 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 70 61 72 eration code par
4cd90 61 6d 65 74 65 72 73 20 75 73 65 64 0a 2a 2a 20 ameters used.**
4cda0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 as the first arg
4cdb0 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 ument to [sqlite
4cdc0 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 3_test_control()
4cdd0 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 ]..**.** These p
4cde0 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20 74 68 arameters and th
4cdf0 65 69 72 20 6d 65 61 6e 69 6e 67 73 20 61 72 65 eir meanings are
4ce00 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e subject to chan
4ce10 67 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20 6e 6f ge.** without no
4ce20 74 69 63 65 2e 20 20 54 68 65 73 65 20 76 61 6c tice. These val
4ce30 75 65 73 20 61 72 65 20 66 6f 72 20 74 65 73 74 ues are for test
4ce40 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c ing purposes onl
4ce50 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f y..** Applicatio
4ce60 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73 ns should not us
4ce70 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 70 e any of these p
4ce80 61 72 61 6d 65 74 65 72 73 20 6f 72 20 74 68 65 arameters or the
4ce90 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 .** [sqlite3_tes
4cea0 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 t_control()] int
4ceb0 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 69 erface..*/.#defi
4cec0 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 ne SQLITE_TESTCT
4ced0 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 20 20 20 RL_PRNG_SAVE
4cee0 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 5.#d
4cef0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 efine SQLITE_TES
4cf00 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f TCTRL_PRNG_RESTO
4cf10 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20 36 RE 6
4cf20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
4cf30 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 TESTCTRL_PRNG_RE
4cf40 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20 20 SET
4cf50 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 7.#define SQLI
4cf60 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49 54 56 TE_TESTCTRL_BITV
4cf70 45 43 5f 54 45 53 54 20 20 20 20 20 20 20 20 20 EC_TEST
4cf80 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 53 8.#define S
4cf90 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 46 QLITE_TESTCTRL_F
4cfa0 41 55 4c 54 5f 49 4e 53 54 41 4c 4c 20 20 20 20 AULT_INSTALL
4cfb0 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 9.#defin
4cfc0 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 e SQLITE_TESTCTR
4cfd0 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f L_BENIGN_MALLOC_
4cfe0 48 4f 4f 4b 53 20 20 20 20 20 31 30 0a 0a 2f 2a HOOKS 10../*
4cff0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 .** CAPI3REF: SQ
4d000 4c 69 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61 Lite Runtime Sta
4d010 74 75 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 tus {H17200} <S6
4d020 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0200>.** EXPERIM
4d030 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ENTAL.**.** This
4d040 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 interface is us
4d050 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 ed to retrieve r
4d060 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e untime status in
4d070 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f formation.** abo
4d080 75 74 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e ut the preforman
4d090 63 65 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e ce of SQLite, an
4d0a0 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 d optionally to
4d0b0 72 65 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a reset various.**
4d0c0 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73 highwater marks
4d0d0 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 . The first arg
4d0e0 75 6d 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 ument is an inte
4d0f0 67 65 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 ger code for.**
4d100 74 68 65 20 73 70 65 63 69 66 69 63 20 70 61 72 the specific par
4d110 61 6d 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72 ameter to measur
4d120 65 2e 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 e. Recognized i
4d130 6e 74 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 nteger codes.**
4d140 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 are of the form
4d150 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d [SQLITE_STATUS_M
4d160 45 4d 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c EMORY_USED | SQL
4d170 49 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e ITE_STATUS_...].
4d180 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 .** The current
4d190 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72 value of the par
4d1a0 61 6d 65 74 65 72 20 69 73 20 72 65 74 75 72 6e ameter is return
4d1b0 65 64 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e ed into *pCurren
4d1c0 74 2e 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73 t..** The highes
4d1d0 74 20 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65 t recorded value
4d1e0 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 is returned in
4d1f0 2a 70 48 69 67 68 77 61 74 65 72 2e 20 20 49 66 *pHighwater. If
4d200 20 74 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 the.** resetFla
4d210 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 g is true, then
4d220 74 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f the highest reco
4d230 72 64 20 76 61 6c 75 65 20 69 73 20 72 65 73 65 rd value is rese
4d240 74 20 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 t after.** *pHig
4d250 68 77 61 74 65 72 20 69 73 20 77 72 69 74 74 65 hwater is writte
4d260 6e 2e 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 n. Some paramete
4d270 72 73 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 rs do not record
4d280 20 74 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 the highest.**
4d290 76 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 value. For thos
4d2a0 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 e parameters.**
4d2b0 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 nothing is writt
4d2c0 65 6e 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 en into *pHighwa
4d2d0 74 65 72 20 61 6e 64 20 74 68 65 20 72 65 73 65 ter and the rese
4d2e0 74 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 tFlag is ignored
4d2f0 2e 0a 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d ..** Other param
4d300 65 74 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c eters record onl
4d310 79 20 74 68 65 20 68 69 67 68 77 61 74 65 72 20 y the highwater
4d320 6d 61 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 mark and not the
4d330 20 63 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 current.** valu
4d340 65 2e 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61 e. For these la
4d350 74 74 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 tter parameters
4d360 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 nothing is writt
4d370 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e en into *pCurren
4d380 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f t..**.** This ro
4d390 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 utine returns SQ
4d3a0 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
4d3b0 73 73 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 ss and a non-zer
4d3c0 6f 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 o.** [error code
4d3d0 5d 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a ] on failure..**
4d3e0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
4d3f0 20 69 73 20 74 68 72 65 61 64 73 61 66 65 20 62 is threadsafe b
4d400 75 74 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 ut is not atomic
4d410 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
4d420 63 61 6e 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 68 can.** called wh
4d430 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61 64 ile other thread
4d440 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74 68 s are running th
4d450 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65 72 e same or differ
4d460 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e ent SQLite.** in
4d470 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76 terfaces. Howev
4d480 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 er the values re
4d490 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72 72 turned in *pCurr
4d4a0 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 67 ent and.** *pHig
4d4b0 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20 74 hwater reflect t
4d4c0 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51 4c he status of SQL
4d4d0 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e 74 ite at different
4d4e0 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65 0a points in time.
4d4f0 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f 73 ** and it is pos
4d500 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 sible that anoth
4d510 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74 20 er thread might
4d520 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61 6d change the param
4d530 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77 65 eter.** in betwe
4d540 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68 65 en the times whe
4d550 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 20 n *pCurrent and
4d560 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65 20 *pHighwater are
4d570 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 written..**.** S
4d580 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 ee also: [sqlite
4d590 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a 2a 3_db_status()].*
4d5a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c /.SQLITE_API SQL
4d5b0 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
4d5c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 int sqlite3_sta
4d5d0 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 tus(int op, int
4d5e0 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a *pCurrent, int *
4d5f0 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 pHighwater, int
4d600 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a resetFlag);.../*
4d610 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 .** CAPI3REF: St
4d620 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 20 atus Parameters
4d630 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 30 30 {H17250} <H17200
4d640 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
4d650 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e L.**.** These in
4d660 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 teger constants
4d670 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 6f 75 designate variou
4d680 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 74 75 s run-time statu
4d690 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 s parameters.**
4d6a0 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 74 75 that can be retu
4d6b0 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 rned by [sqlite3
4d6c0 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a _status()]..**.*
4d6d0 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 * <dl>.** <dt>SQ
4d6e0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f LITE_STATUS_MEMO
4d6f0 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 RY_USED</dt>.**
4d700 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
4d710 65 72 20 69 73 20 74 68 65 20 63 75 72 72 65 6e er is the curren
4d720 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f t amount of memo
4d730 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0a 2a ry checked out.*
4d740 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 * using [sqlite3
4d750 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 68 _malloc()], eith
4d760 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69 er directly or i
4d770 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 0a ndirectly. The.
4d780 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75 64 ** figure includ
4d790 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74 6f es calls made to
4d7a0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
4d7b0 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c 69 ()] by the appli
4d7c0 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e cation.** and in
4d7d0 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 73 ternal memory us
4d7e0 61 67 65 20 62 79 20 74 68 65 20 53 51 4c 69 74 age by the SQLit
4d7f0 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 72 61 e library. Scra
4d800 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f tch memory.** co
4d810 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c ntrolled by [SQL
4d820 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
4d830 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 61 72 CH] and auxiliar
4d840 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a 2a 20 y page-cache.**
4d850 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 memory controlle
4d860 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e d by [SQLITE_CON
4d870 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 20 69 FIG_PAGECACHE] i
4d880 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 s not included i
4d890 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 6d 65 n.** this parame
4d8a0 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 6e 74 ter. The amount
4d8b0 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 returned is the
4d8c0 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c 6c 6f sum of the allo
4d8d0 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20 cation.** sizes
4d8e0 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 20 74 as reported by t
4d8f0 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f 64 20 he xSize method
4d900 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f in [sqlite3_mem_
4d910 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a methods].</dd>.*
4d920 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
4d930 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 STATUS_MALLOC_SI
4d940 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ZE</dt>.** <dd>T
4d950 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
4d960 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 cords the larges
4d970 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 t memory allocat
4d980 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 ion request.** h
4d990 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 anded to [sqlite
4d9a0 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 3_malloc()] or [
4d9b0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
4d9c0 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20 )] (or their.**
4d9d0 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 61 6c internal equival
4d9e0 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65 ents). Only the
4d9f0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
4da00 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 in the.** *pHigh
4da10 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 water parameter
4da20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 to [sqlite3_stat
4da30 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 us()] is of inte
4da40 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 rest. .** The v
4da50 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 alue written int
4da60 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 o the *pCurrent
4da70 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 parameter is und
4da80 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a efined.</dd>.**.
4da90 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
4daa0 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 ATUS_PAGECACHE_U
4dab0 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e SED</dt>.** <dd>
4dac0 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
4dad0 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
4dae0 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 64 20 r of pages used
4daf0 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 out of the.** [p
4db00 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 agecache memory
4db10 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74 20 allocator] that
4db20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 75 was configured u
4db30 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 sing .** [SQLITE
4db40 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
4db50 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c 75 E]. The.** valu
4db60 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e e returned is in
4db70 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e 20 62 pages, not in b
4db80 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ytes.</dd>.**.**
4db90 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 <dt>SQLITE_STAT
4dba0 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45 US_PAGECACHE_OVE
4dbb0 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 RFLOW</dt>.** <d
4dbc0 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 d>This parameter
4dbd0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
4dbe0 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
4dbf0 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 61 6c page cache.** al
4dc00 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63 location which c
4dc10 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74 ould not be stat
4dc20 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b 53 isfied by the [S
4dc30 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
4dc40 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 66 65 ECACHE].** buffe
4dc50 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63 r and where forc
4dc60 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 ed to overflow t
4dc70 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f o [sqlite3_mallo
4dc80 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 65 c()]. The.** re
4dc90 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e 63 turned value inc
4dca0 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 6f 6e ludes allocation
4dcb0 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 s that overflowe
4dcc0 64 20 62 65 63 61 75 73 65 20 74 68 65 79 0a 2a d because they.*
4dcd0 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 72 67 * where too larg
4dce0 65 20 28 74 68 65 79 20 77 65 72 65 20 6c 61 72 e (they were lar
4dcf0 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 73 7a ger than the "sz
4dd00 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a " parameter to.*
4dd10 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 * [SQLITE_CONFIG
4dd20 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 6e 64 _PAGECACHE]) and
4dd30 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 allocations tha
4dd40 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63 t overflowed bec
4dd50 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 65 ause.** no space
4dd60 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68 65 was left in the
4dd70 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f 64 64 page cache.</dd
4dd80 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
4dd90 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 TE_STATUS_PAGECA
4dda0 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a CHE_SIZE</dt>.**
4ddb0 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 <dd>This parame
4ddc0 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 ter records the
4ddd0 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 largest memory a
4dde0 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 llocation reques
4ddf0 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b t.** handed to [
4de00 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 pagecache memory
4de10 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e allocator]. On
4de20 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 ly the value ret
4de30 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 urned in the.**
4de40 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 61 *pHighwater para
4de50 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
4de60 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f 3_status()] is o
4de70 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a f interest. .**
4de80 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 74 The value writt
4de90 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 en into the *pCu
4dea0 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 rrent parameter
4deb0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 is undefined.</d
4dec0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
4ded0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
4dee0 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 CH_USED</dt>.**
4def0 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
4df00 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e er returns the n
4df10 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 umber of allocat
4df20 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 6f 66 ions used out of
4df30 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 63 68 the.** [scratch
4df40 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
4df50 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 r] configured us
4df60 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 ing.** [SQLITE_C
4df70 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2e 20 ONFIG_SCRATCH].
4df80 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 The value retur
4df90 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f 63 61 ned is in alloca
4dfa0 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e tions, not.** in
4dfb0 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 20 61 bytes. Since a
4dfc0 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 6d single thread m
4dfd0 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f 6e 65 ay only have one
4dfe0 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 scratch allocat
4dff0 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 ion.** outstandi
4e000 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 69 73 ng at time, this
4e010 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 6f 20 parameter also
4e020 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 6d 62 reports the numb
4e030 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a 2a 2a er of threads.**
4e040 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 20 6d using scratch m
4e050 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 61 6d emory at the sam
4e060 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a e time.</dd>.**.
4e070 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
4e080 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 ATUS_SCRATCH_OVE
4e090 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 RFLOW</dt>.** <d
4e0a0 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 d>This parameter
4e0b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
4e0c0 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
4e0d0 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a scratch memory.*
4e0e0 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 * allocation whi
4e0f0 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ch could not be
4e100 73 74 61 74 69 73 66 69 65 64 20 62 79 20 74 68 statisfied by th
4e110 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 e [SQLITE_CONFIG
4e120 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75 66 _SCRATCH].** buf
4e130 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f fer and where fo
4e140 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 rced to overflow
4e150 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c to [sqlite3_mal
4e160 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61 6c loc()]. The val
4e170 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 ues.** returned
4e180 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f 77 include overflow
4e190 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72 65 s because the re
4e1a0 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 quested allocati
4e1b0 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61 on was too.** la
4e1c0 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20 62 rger (that is, b
4e1d0 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 65 ecause the reque
4e1e0 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 sted allocation
4e1f0 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 was larger than
4e200 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72 61 the.** "sz" para
4e210 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54 45 meter to [SQLITE
4e220 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d _CONFIG_SCRATCH]
4e230 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e 6f ) and because no
4e240 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 0a scratch buffer.
4e250 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61 76 ** slots were av
4e260 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64 ailable..** </dd
4e270 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
4e280 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
4e290 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c H_SIZE</dt>.** <
4e2a0 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 dd>This paramete
4e2b0 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 r records the la
4e2c0 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c rgest memory all
4e2d0 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a ocation request.
4e2e0 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 63 ** handed to [sc
4e2f0 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c ratch memory all
4e300 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 ocator]. Only t
4e310 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
4e320 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 d in the.** *pHi
4e330 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 ghwater paramete
4e340 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 r to [sqlite3_st
4e350 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e atus()] is of in
4e360 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 terest. .** The
4e370 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 value written i
4e380 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e nto the *pCurren
4e390 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 t parameter is u
4e3a0 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a ndefined.</dd>.*
4e3b0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
4e3c0 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54 STATUS_PARSER_ST
4e3d0 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e ACK</dt>.** <dd>
4e3e0 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
4e3f0 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 70 65 ecords the deepe
4e400 73 74 20 70 61 72 73 65 72 20 73 74 61 63 6b 2e st parser stack.
4e410 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 It is only.**
4e420 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 53 51 meaningful if SQ
4e430 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
4e440 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b 4d 41 with [YYTRACKMA
4e450 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f 64 XSTACKDEPTH].</d
4e460 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a d>.** </dl>.**.*
4e470 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 61 72 * New status par
4e480 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 20 61 ameters may be a
4e490 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 20 74 dded from time t
4e4a0 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69 o time..*/.#defi
4e4b0 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 ne SQLITE_STATUS
4e4c0 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 20 20 _MEMORY_USED
4e4d0 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
4e4e0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
4e4f0 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 20 20 GECACHE_USED
4e500 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 1.#define SQL
4e510 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
4e520 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 20 20 ACHE_OVERFLOW
4e530 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
4e540 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
4e550 55 53 45 44 20 20 20 20 20 20 20 20 20 33 0a 23 USED 3.#
4e560 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
4e570 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 ATUS_SCRATCH_OVE
4e580 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 65 66 RFLOW 4.#def
4e590 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
4e5a0 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 20 S_MALLOC_SIZE
4e5b0 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 5.#define
4e5c0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 SQLITE_STATUS_P
4e5d0 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 20 20 ARSER_STACK
4e5e0 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51 6.#define SQ
4e5f0 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
4e600 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 20 20 CACHE_SIZE
4e610 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 7.#define SQLIT
4e620 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
4e630 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 38 0a _SIZE 8.
4e640 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
4e650 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 Database Connec
4e660 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 31 37 tion Status {H17
4e670 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 500} <S60200>.**
4e680 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
4e690 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
4e6a0 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 ce is used to re
4e6b0 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20 73 trieve runtime s
4e6c0 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f tatus informatio
4e6d0 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73 69 n .** about a si
4e6e0 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20 63 ngle [database c
4e6f0 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 onnection]. The
4e700 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
4e710 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 is the.** databa
4e720 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 se connection ob
4e730 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 65 72 ject to be inter
4e740 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 73 65 rogated. The se
4e750 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a cond argument.**
4e760 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 65 is the paramete
4e770 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 74 65 r to interrogate
4e780 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 . Currently, th
4e790 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 76 e only allowed v
4e7a0 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 alue.** for the
4e7b0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
4e7c0 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 53 54 is [SQLITE_DBST
4e7d0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 ATUS_LOOKASIDE_U
4e7e0 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 6f SED]..** Additio
4e7f0 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 6c 6c nal options will
4e800 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 20 69 likely appear i
4e810 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
4e820 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a s of SQLite..**.
4e830 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 ** The current v
4e840 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71 75 alue of the requ
4e850 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 72 20 ested parameter
4e860 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
4e870 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 68 65 *pCur.** and the
4e880 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 6e 74 highest instant
4e890 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 73 20 aneous value is
4e8a0 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48 written into *pH
4e8b0 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 68 65 iwtr. If.** the
4e8c0 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72 75 resetFlg is tru
4e8d0 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67 68 e, then the high
4e8e0 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 est instantaneou
4e8f0 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 72 65 s value is.** re
4e900 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 74 6f set back down to
4e910 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c the current val
4e920 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c ue..**.** See al
4e930 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 so: [sqlite3_sta
4e940 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 tus()] and [sqli
4e950 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 te3_stmt_status(
4e960 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
4e970 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d I SQLITE_EXPERIM
4e980 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 ENTAL int sqlite
4e990 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c 69 3_db_status(sqli
4e9a0 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e te3*, int op, in
4e9b0 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 t *pCur, int *pH
4e9c0 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74 46 iwtr, int resetF
4e9d0 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 lg);../*.** CAPI
4e9e0 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 3REF: Status Par
4e9f0 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 74 61 ameters for data
4ea00 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
4ea10 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 35 30 {H17520} <H1750
4ea20 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
4ea30 41 4c 0a 2a 2a 0a 2a 2a 20 53 74 61 74 75 73 20 AL.**.** Status
4ea40 76 65 72 62 73 20 66 6f 72 20 5b 73 71 6c 69 74 verbs for [sqlit
4ea50 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e e3_db_status()].
4ea60 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c .**.** <dl>.** <
4ea70 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 dt>SQLITE_DBSTAT
4ea80 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 US_LOOKASIDE_USE
4ea90 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
4eaa0 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 is parameter ret
4eab0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
4eac0 6f 66 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d of lookaside mem
4ead0 6f 72 79 20 73 6c 6f 74 73 20 63 75 72 72 65 6e ory slots curren
4eae0 74 6c 79 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f tly.** checked o
4eaf0 75 74 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c ut.</dd>.** </dl
4eb00 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c >.*/.#define SQL
4eb10 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f ITE_DBSTATUS_LOO
4eb20 4b 41 53 49 44 45 5f 55 53 45 44 20 20 20 20 20 KASIDE_USED
4eb30 30 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 0.../*.** CAPI3R
4eb40 45 46 3a 20 50 72 65 70 61 72 65 64 20 53 74 61 EF: Prepared Sta
4eb50 74 65 6d 65 6e 74 20 53 74 61 74 75 73 20 7b 48 tement Status {H
4eb60 31 37 35 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a 17550} <S60200>.
4eb70 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
4eb80 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 72 65 70 61 **.** Each prepa
4eb90 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 red statement ma
4eba0 69 6e 74 61 69 6e 73 20 76 61 72 69 6f 75 73 0a intains various.
4ebb0 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 ** [SQLITE_STMTS
4ebc0 54 41 54 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 TATUS_SORT | cou
4ebd0 6e 74 65 72 73 5d 20 74 68 61 74 20 6d 65 61 73 nters] that meas
4ebe0 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a ure the number.*
4ebf0 2a 20 6f 66 20 74 69 6d 65 73 20 69 74 20 68 61 * of times it ha
4ec00 73 20 70 65 72 66 6f 72 6d 65 64 20 73 70 65 63 s performed spec
4ec10 69 66 69 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e ific operations.
4ec20 20 20 54 68 65 73 65 20 63 6f 75 6e 74 65 72 73 These counters
4ec30 20 63 61 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20 can.** be used
4ec40 74 6f 20 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 to monitor the p
4ec50 65 72 66 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 erformance chara
4ec60 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 cteristics of th
4ec70 65 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 e prepared.** st
4ec80 61 74 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 atements. For e
4ec90 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 6e xample, if the n
4eca0 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73 umber of table s
4ecb0 74 65 70 73 20 67 72 65 61 74 6c 79 20 65 78 63 teps greatly exc
4ecc0 65 65 64 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 eeds.** the numb
4ecd0 65 72 20 6f 66 20 74 61 62 6c 65 20 73 65 61 72 er of table sear
4ece0 63 68 65 73 20 6f 72 20 72 65 73 75 6c 74 20 72 ches or result r
4ecf0 6f 77 73 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 ows, that would
4ed00 74 65 6e 64 20 74 6f 20 69 6e 64 69 63 61 74 65 tend to indicate
4ed10 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 72 65 .** that the pre
4ed20 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 pared statement
4ed30 69 73 20 75 73 69 6e 67 20 61 20 66 75 6c 6c 20 is using a full
4ed40 74 61 62 6c 65 20 73 63 61 6e 20 72 61 74 68 65 table scan rathe
4ed50 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 r than.** an ind
4ed60 65 78 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ex. .**.** This
4ed70 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 interface is us
4ed80 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 ed to retrieve a
4ed90 6e 64 20 72 65 73 65 74 20 63 6f 75 6e 74 65 72 nd reset counter
4eda0 20 76 61 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 values from.**
4edb0 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 a [prepared stat
4edc0 65 6d 65 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 ement]. The fir
4edd0 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 st argument is t
4ede0 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 he prepared stat
4edf0 65 6d 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 ement.** object
4ee00 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 to be interrogat
4ee10 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 ed. The second
4ee20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 argument.** is a
4ee30 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 n integer code f
4ee40 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 5b 53 or a specific [S
4ee50 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
4ee60 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d _SORT | counter]
4ee70 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 .** to be interr
4ee80 6f 67 61 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20 ogated. .** The
4ee90 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 current value of
4eea0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63 the requested c
4eeb0 6f 75 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e ounter is return
4eec0 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 ed..** If the re
4eed0 73 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 setFlg is true,
4eee0 74 68 65 6e 20 74 68 65 20 63 6f 75 6e 74 65 72 then the counter
4eef0 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 is reset to zer
4ef00 6f 20 61 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 o after this.**
4ef10 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 20 72 interface call r
4ef20 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 eturns..**.** Se
4ef30 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 e also: [sqlite3
4ef40 5f 73 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b _status()] and [
4ef50 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 sqlite3_db_statu
4ef60 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f s()]..*/.SQLITE_
4ef70 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 API SQLITE_EXPER
4ef80 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 IMENTAL int sqli
4ef90 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 te3_stmt_status(
4efa0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
4efb0 6e 74 20 6f 70 2c 69 6e 74 20 72 65 73 65 74 46 nt op,int resetF
4efc0 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 lg);../*.** CAPI
4efd0 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 3REF: Status Par
4efe0 61 6d 65 74 65 72 73 20 66 6f 72 20 70 72 65 70 ameters for prep
4eff0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 ared statements
4f000 7b 48 31 37 35 37 30 7d 20 3c 48 31 37 35 35 30 {H17570} <H17550
4f010 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
4f020 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72 L.**.** These pr
4f030 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f eprocessor macro
4f040 73 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 s define integer
4f050 20 63 6f 64 65 73 20 74 68 61 74 20 6e 61 6d 65 codes that name
4f060 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 counter.** valu
4f070 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 es associated wi
4f080 74 68 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f th the [sqlite3_
4f090 73 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 20 69 stmt_status()] i
4f0a0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 nterface..** The
4f0b0 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 meanings of the
4f0c0 20 76 61 72 69 6f 75 73 20 63 6f 75 6e 74 65 72 various counter
4f0d0 73 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 s are as follows
4f0e0 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 :.**.** <dl>.**
4f0f0 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 <dt>SQLITE_STMTS
4f100 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 TATUS_FULLSCAN_S
4f110 54 45 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TEP</dt>.** <dd>
4f120 54 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62 This is the numb
4f130 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 er of times that
4f140 20 53 51 4c 69 74 65 20 68 61 73 20 73 74 65 70 SQLite has step
4f150 70 65 64 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a ped forward in.*
4f160 2a 20 61 20 74 61 62 6c 65 20 61 73 20 70 61 72 * a table as par
4f170 74 20 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c t of a full tabl
4f180 65 20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e e scan. Large n
4f190 75 6d 62 65 72 73 20 66 6f 72 20 74 68 69 73 20 umbers for this
4f1a0 63 6f 75 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 counter.** may i
4f1b0 6e 64 69 63 61 74 65 20 6f 70 70 6f 72 74 75 6e ndicate opportun
4f1c0 69 74 69 65 73 20 66 6f 72 20 70 65 72 66 6f 72 ities for perfor
4f1d0 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e mance improvemen
4f1e0 74 20 74 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 t through .** ca
4f1f0 72 65 66 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 reful use of ind
4f200 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ices.</dd>.**.**
4f210 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 <dt>SQLITE_STMT
4f220 53 54 41 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e STATUS_SORT</dt>
4f230 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 73 20 .** <dd>This is
4f240 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f the number of so
4f250 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 rt operations th
4f260 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 at have occurred
4f270 2e 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 ..** A non-zero
4f280 76 61 6c 75 65 20 69 6e 20 74 68 69 73 20 63 6f value in this co
4f290 75 6e 74 65 72 20 6d 61 79 20 69 6e 64 69 63 61 unter may indica
4f2a0 74 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 te an opportunit
4f2b0 79 20 74 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d y to.** improvem
4f2c0 65 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 ent performance
4f2d0 74 68 72 6f 75 67 68 20 63 61 72 65 66 75 6c 20 through careful
4f2e0 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c use of indices.<
4f2f0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e /dd>.**.** </dl>
4f300 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
4f310 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55 TE_STMTSTATUS_FU
4f320 4c 4c 53 43 41 4e 5f 53 54 45 50 20 20 20 20 20 LLSCAN_STEP
4f330 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
4f340 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 _STMTSTATUS_SORT
4f350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 2.
4f360 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
4f370 20 43 75 73 74 6f 6d 20 50 61 67 65 20 43 61 63 Custom Page Cac
4f380 68 65 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 he Object.** EXP
4f390 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
4f3a0 54 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 The sqlite3_pcac
4f3b0 68 65 20 74 79 70 65 20 69 73 20 6f 70 61 71 75 he type is opaqu
4f3c0 65 2e 20 20 49 74 20 69 73 20 69 6d 70 6c 65 6d e. It is implem
4f3d0 65 6e 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 ented by.** the
4f3e0 70 6c 75 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65 pluggable module
4f3f0 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f . The SQLite co
4f400 72 65 20 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 re has no knowle
4f410 64 67 65 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69 dge of.** its si
4f420 7a 65 20 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73 ze or internal s
4f430 74 72 75 63 74 75 72 65 20 61 6e 64 20 6e 65 76 tructure and nev
4f440 65 72 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 er deals with th
4f450 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 e.** sqlite3_pca
4f460 63 68 65 20 6f 62 6a 65 63 74 20 65 78 63 65 70 che object excep
4f470 74 20 62 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 t by holding and
4f480 20 70 61 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 passing pointer
4f490 73 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 s.** to the obje
4f4a0 63 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 ct..**.** See [s
4f4b0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
4f4c0 74 68 6f 64 73 5d 20 66 6f 72 20 61 64 64 69 74 thods] for addit
4f4d0 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
4f4e0 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 n..*/.typedef st
4f4f0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61 ruct sqlite3_pca
4f500 63 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 che sqlite3_pcac
4f510 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 he;../*.** CAPI3
4f520 52 45 46 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e REF: Application
4f530 20 44 65 66 69 6e 65 64 20 50 61 67 65 20 43 61 Defined Page Ca
4f540 63 68 65 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 che..** EXPERIME
4f550 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b NTAL.**.** The [
4f560 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 sqlite3_config](
4f570 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 [SQLITE_CONFIG_P
4f580 43 41 43 48 45 5d 2c 20 2e 2e 2e 29 20 69 6e 74 CACHE], ...) int
4f590 65 72 66 61 63 65 20 63 61 6e 0a 2a 2a 20 72 65 erface can.** re
4f5a0 67 69 73 74 65 72 20 61 6e 20 61 6c 74 65 72 6e gister an altern
4f5b0 61 74 69 76 65 20 70 61 67 65 20 63 61 63 68 65 ative page cache
4f5c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
4f5d0 62 79 20 70 61 73 73 69 6e 67 20 69 6e 20 61 6e by passing in an
4f5e0 20 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 .** instance of
4f5f0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
4f600 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 che_methods stru
4f610 63 74 75 72 65 2e 20 54 68 65 20 6d 61 6a 6f 72 cture. The major
4f620 69 74 79 20 6f 66 20 74 68 65 20 0a 2a 2a 20 68 ity of the .** h
4f630 65 61 70 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 eap memory used
4f640 62 79 20 73 71 6c 69 74 65 20 69 73 20 75 73 65 by sqlite is use
4f650 64 20 62 79 20 74 68 65 20 70 61 67 65 20 63 61 d by the page ca
4f660 63 68 65 20 74 6f 20 63 61 63 68 65 20 64 61 74 che to cache dat
4f670 61 20 72 65 61 64 20 0a 2a 2a 20 66 72 6f 6d 2c a read .** from,
4f680 20 6f 72 20 72 65 61 64 79 20 74 6f 20 62 65 20 or ready to be
4f690 77 72 69 74 74 65 6e 20 74 6f 2c 20 74 68 65 20 written to, the
4f6a0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 42 database file. B
4f6b0 79 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 61 y implementing a
4f6c0 20 0a 2a 2a 20 63 75 73 74 6f 6d 20 70 61 67 65 .** custom page
4f6d0 20 63 61 63 68 65 20 75 73 69 6e 67 20 74 68 69 cache using thi
4f6e0 73 20 41 50 49 2c 20 61 6e 20 61 70 70 6c 69 63 s API, an applic
4f6f0 61 74 69 6f 6e 20 63 61 6e 20 63 6f 6e 74 72 6f ation can contro
4f700 6c 20 6d 6f 72 65 20 0a 2a 2a 20 70 72 65 63 69 l more .** preci
4f710 73 65 6c 79 20 74 68 65 20 61 6d 6f 75 6e 74 20 sely the amount
4f720 6f 66 20 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d of memory consum
4f730 65 64 20 62 79 20 73 71 6c 69 74 65 2c 20 74 68 ed by sqlite, th
4f740 65 20 77 61 79 20 69 6e 20 77 68 69 63 68 20 0a e way in which .
4f750 2a 2a 20 73 61 69 64 20 6d 65 6d 6f 72 79 20 69 ** said memory i
4f760 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 s allocated and
4f770 72 65 6c 65 61 73 65 64 2c 20 61 6e 64 20 74 68 released, and th
4f780 65 20 70 6f 6c 69 63 69 65 73 20 75 73 65 64 20 e policies used
4f790 74 6f 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 to .** determine
4f7a0 20 65 78 61 63 74 6c 79 20 77 68 69 63 68 20 70 exactly which p
4f7b0 61 72 74 73 20 6f 66 20 61 20 64 61 74 61 62 61 arts of a databa
4f7c0 73 65 20 66 69 6c 65 20 61 72 65 20 63 61 63 68 se file are cach
4f7d0 65 64 20 61 6e 64 20 66 6f 72 20 0a 2a 2a 20 68 ed and for .** h
4f7e0 6f 77 20 6c 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 ow long..**.** T
4f7f0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
4f800 68 65 20 73 74 72 75 63 74 75 72 65 20 61 72 65 he structure are
4f810 20 63 6f 70 69 65 64 20 74 6f 20 61 6e 20 69 6e copied to an in
4f820 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62 79 ternal buffer by
4f830 20 73 71 6c 69 74 65 0a 2a 2a 20 77 69 74 68 69 sqlite.** withi
4f840 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 n the call to [s
4f850 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 0a qlite3_config]..
4f860 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 28 **.** The xInit(
4f870 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c 6c ) method is call
4f880 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ed once for each
4f890 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
4f8a0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 0a 3_initialize()].
4f8b0 2a 2a 20 28 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 ** (usually only
4f8c0 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 74 68 65 once during the
4f8d0 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 lifetime of the
4f8e0 20 70 72 6f 63 65 73 73 29 2e 20 49 74 20 69 73 process). It is
4f8f0 20 70 61 73 73 65 64 0a 2a 2a 20 61 20 63 6f 70 passed.** a cop
4f900 79 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 y of the sqlite3
4f910 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2e _pcache_methods.
4f920 70 41 72 67 20 76 61 6c 75 65 2e 20 49 74 20 63 pArg value. It c
4f930 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 an be used to se
4f940 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 6c 20 73 t.** up global s
4f950 74 72 75 63 74 75 72 65 73 20 61 6e 64 20 6d 75 tructures and mu
4f960 74 65 78 65 73 20 72 65 71 75 69 72 65 64 20 62 texes required b
4f970 79 20 74 68 65 20 63 75 73 74 6f 6d 20 70 61 67 y the custom pag
4f980 65 20 63 61 63 68 65 20 0a 2a 2a 20 69 6d 70 6c e cache .** impl
4f990 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68 65 20 ementation. The
4f9a0 78 53 68 75 74 64 6f 77 6e 28 29 20 6d 65 74 68 xShutdown() meth
4f9b0 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f od is called fro
4f9c0 6d 20 77 69 74 68 69 6e 20 0a 2a 2a 20 5b 73 71 m within .** [sq
4f9d0 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
4f9e0 5d 2c 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 ], if the applic
4f9f0 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 74 68 ation invokes th
4fa00 69 73 20 41 50 49 2e 20 49 74 20 63 61 6e 20 62 is API. It can b
4fa10 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 63 6c 65 e used.** to cle
4fa20 61 6e 20 75 70 20 61 6e 79 20 6f 75 74 73 74 61 an up any outsta
4fa30 6e 64 69 6e 67 20 72 65 73 6f 75 72 63 65 73 20 nding resources
4fa40 62 65 66 6f 72 65 20 70 72 6f 63 65 73 73 20 73 before process s
4fa50 68 75 74 64 6f 77 6e 2c 20 69 66 20 72 65 71 75 hutdown, if requ
4fa60 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ired..**.** The
4fa70 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64 xCreate() method
4fa80 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 is used to cons
4fa90 74 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 68 truct a new cach
4faa0 65 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 0a e instance. The.
4fab0 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 ** first paramet
4fac0 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73 20 74 er, szPage, is t
4fad0 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 he size in bytes
4fae0 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 74 68 of the pages th
4faf0 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 6c at must.** be al
4fb00 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 63 located by the c
4fb10 61 63 68 65 2e 20 73 7a 50 61 67 65 20 77 69 6c ache. szPage wil
4fb20 6c 20 6e 6f 74 20 62 65 20 61 20 70 6f 77 65 72 l not be a power
4fb30 20 6f 66 20 74 77 6f 2e 20 54 68 65 0a 2a 2a 20 of two. The.**
4fb40 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c second argument,
4fb50 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20 bPurgeable, is
4fb60 74 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68 true if the cach
4fb70 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 e being created
4fb80 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 20 will.** be used
4fb90 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73 to cache databas
4fba0 65 20 70 61 67 65 73 20 72 65 61 64 20 66 72 6f e pages read fro
4fbb0 6d 20 61 20 66 69 6c 65 20 73 74 6f 72 65 64 20 m a file stored
4fbc0 6f 6e 20 64 69 73 6b 2c 20 6f 72 0a 2a 2a 20 66 on disk, or.** f
4fbd0 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 75 73 alse if it is us
4fbe0 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d ed for an in-mem
4fbf0 6f 72 79 20 64 61 74 61 62 61 73 65 2e 20 54 68 ory database. Th
4fc00 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e e cache implemen
4fc10 74 61 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e tation.** does n
4fc20 6f 74 20 68 61 76 65 20 74 6f 20 64 6f 20 61 6e ot have to do an
4fc30 79 74 68 69 6e 67 20 73 70 65 63 69 61 6c 20 62 ything special b
4fc40 61 73 65 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 ased on the valu
4fc50 65 20 6f 66 20 62 50 75 72 67 65 61 62 6c 65 2c e of bPurgeable,
4fc60 0a 2a 2a 20 69 74 20 69 73 20 70 75 72 65 6c 79 .** it is purely
4fc70 20 61 64 76 69 73 6f 72 79 2e 20 0a 2a 2a 0a 2a advisory. .**.*
4fc80 2a 20 54 68 65 20 78 43 61 63 68 65 73 69 7a 65 * The xCachesize
4fc90 28 29 20 6d 65 74 68 6f 64 20 6d 61 79 20 62 65 () method may be
4fca0 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79 20 74 called at any t
4fcb0 69 6d 65 20 62 79 20 53 51 4c 69 74 65 20 74 6f ime by SQLite to
4fcc0 20 73 65 74 20 74 68 65 0a 2a 2a 20 73 75 67 67 set the.** sugg
4fcd0 65 73 74 65 64 20 6d 61 78 69 6d 75 6d 20 63 61 ested maximum ca
4fce0 63 68 65 2d 73 69 7a 65 20 28 6e 75 6d 62 65 72 che-size (number
4fcf0 20 6f 66 20 70 61 67 65 73 20 73 74 6f 72 65 64 of pages stored
4fd00 20 62 79 29 20 74 68 65 20 63 61 63 68 65 0a 2a by) the cache.*
4fd10 2a 20 69 6e 73 74 61 6e 63 65 20 70 61 73 73 65 * instance passe
4fd20 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 d as the first a
4fd30 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 rgument. This is
4fd40 20 74 68 65 20 76 61 6c 75 65 20 63 6f 6e 66 69 the value confi
4fd50 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 gured using.** t
4fd60 68 65 20 53 51 4c 69 74 65 20 22 5b 50 52 41 47 he SQLite "[PRAG
4fd70 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 5d 22 20 MA cache_size]"
4fd80 63 6f 6d 6d 61 6e 64 2e 20 41 73 20 77 69 74 68 command. As with
4fd90 20 74 68 65 20 62 50 75 72 67 65 61 62 6c 65 20 the bPurgeable
4fda0 70 61 72 61 6d 65 74 65 72 2c 0a 2a 2a 20 74 68 parameter,.** th
4fdb0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
4fdc0 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 is not required
4fdd0 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 to do anything
4fde0 73 70 65 63 69 61 6c 20 77 69 74 68 20 74 68 69 special with thi
4fdf0 73 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 s.** value, it i
4fe00 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e s advisory only.
4fe10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 .**.** The xPage
4fe20 63 6f 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 count() method s
4fe30 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 hould return the
4fe40 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
4fe50 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 currently.** st
4fe60 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 ored in the cach
4fe70 65 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e e supplied as an
4fe80 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a argument..** .*
4fe90 2a 20 54 68 65 20 78 46 65 74 63 68 28 29 20 6d * The xFetch() m
4fea0 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f ethod is used to
4feb0 20 66 65 74 63 68 20 61 20 70 61 67 65 20 61 6e fetch a page an
4fec0 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 d return a point
4fed0 65 72 20 74 6f 20 69 74 2e 20 0a 2a 2a 20 41 20 er to it. .** A
4fee0 27 70 61 67 65 27 2c 20 69 6e 20 74 68 69 73 20 'page', in this
4fef0 63 6f 6e 74 65 78 74 2c 20 69 73 20 61 20 62 75 context, is a bu
4ff00 66 66 65 72 20 6f 66 20 73 7a 50 61 67 65 20 62 ffer of szPage b
4ff10 79 74 65 73 20 61 6c 69 67 6e 65 64 20 61 74 20 ytes aligned at
4ff20 61 6e 0a 2a 2a 20 38 2d 62 79 74 65 20 62 6f 75 an.** 8-byte bou
4ff30 6e 64 61 72 79 2e 20 54 68 65 20 70 61 67 65 20 ndary. The page
4ff40 74 6f 20 62 65 20 66 65 74 63 68 65 64 20 69 73 to be fetched is
4ff50 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 determined by t
4ff60 68 65 20 6b 65 79 2e 20 54 68 65 0a 2a 2a 20 6d he key. The.** m
4ff70 69 6d 69 6d 75 6d 20 6b 65 79 20 76 61 6c 75 65 imimum key value
4ff80 20 69 73 20 31 2e 20 41 66 74 65 72 20 69 74 20 is 1. After it
4ff90 68 61 73 20 62 65 65 6e 20 72 65 74 72 69 65 76 has been retriev
4ffa0 65 64 20 75 73 69 6e 67 20 78 46 65 74 63 68 2c ed using xFetch,
4ffb0 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 69 73 the page .** is
4ffc0 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 considered to b
4ffd0 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 e pinned..**.**
4ffe0 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 If the requested
4fff0 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 page is already
50000 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 in the page cac
50010 68 65 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e 74 he, then a point
50020 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 63 61 63 er to.** the cac
50030 68 65 64 20 62 75 66 66 65 72 20 73 68 6f 75 6c hed buffer shoul
50040 64 20 62 65 20 72 65 74 75 72 6e 65 64 20 77 69 d be returned wi
50050 74 68 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 th its contents
50060 69 6e 74 61 63 74 2e 20 49 66 20 74 68 65 0a 2a intact. If the.*
50070 2a 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c * page is not al
50080 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 ready in the cac
50090 68 65 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70 he, then the exp
500a0 65 63 74 65 64 20 62 65 68 61 76 69 6f 75 72 20 ected behaviour
500b0 6f 66 20 74 68 65 0a 2a 2a 20 63 61 63 68 65 20 of the.** cache
500c0 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 is determined by
500d0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
500e0 65 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 e createFlag par
500f0 61 6d 65 74 65 72 20 70 61 73 73 65 64 0a 2a 2a ameter passed.**
50100 20 74 6f 20 78 46 65 74 63 68 2c 20 61 63 63 6f to xFetch, acco
50110 72 64 69 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c rding to the fol
50120 6c 6f 77 69 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a lowing table:.**
50130 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 .** <table borde
50140 72 3d 31 20 77 69 64 74 68 3d 38 35 25 20 61 6c r=1 width=85% al
50150 69 67 6e 3d 63 65 6e 74 65 72 3e 0a 2a 2a 20 20 ign=center>.**
50160 20 3c 74 72 3e 3c 74 68 3e 63 72 65 61 74 65 46 <tr><th>createF
50170 6c 61 67 3c 74 68 3e 45 78 70 65 63 74 65 64 20 lag<th>Expected
50180 42 65 68 61 76 69 6f 75 72 0a 2a 2a 20 20 20 3c Behaviour.** <
50190 74 72 3e 3c 74 64 3e 30 3c 74 64 3e 4e 55 4c 4c tr><td>0<td>NULL
501a0 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 should be retur
501b0 6e 65 64 2e 20 4e 6f 20 6e 65 77 20 63 61 63 68 ned. No new cach
501c0 65 20 65 6e 74 72 79 20 69 73 20 63 72 65 61 74 e entry is creat
501d0 65 64 2e 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 64 ed..** <tr><td
501e0 3e 31 3c 74 64 3e 49 66 20 63 72 65 61 74 65 46 >1<td>If createF
501f0 6c 61 67 20 69 73 20 73 65 74 20 74 6f 20 31 2c lag is set to 1,
50200 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 this indicates
50210 74 68 61 74 20 0a 2a 2a 20 20 20 20 20 20 20 20 that .**
50220 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 69 SQLite i
50230 73 20 68 6f 6c 64 69 6e 67 20 70 69 6e 6e 65 64 s holding pinned
50240 20 70 61 67 65 73 20 74 68 61 74 20 63 61 6e 20 pages that can
50250 62 65 20 75 6e 70 69 6e 6e 65 64 0a 2a 2a 20 20 be unpinned.**
50260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 79 by
50270 20 77 72 69 74 69 6e 67 20 74 68 65 69 72 20 63 writing their c
50280 6f 6e 74 65 6e 74 73 20 74 6f 20 74 68 65 20 64 ontents to the d
50290 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61 0a atabase file (a.
502a0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
502b0 20 20 72 65 6c 61 74 69 76 65 6c 79 20 65 78 70 relatively exp
502c0 65 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e ensive operation
502d0 29 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75 61 ). In this situa
502e0 74 69 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 tion the.**
502f0 20 20 20 20 20 20 20 20 20 20 20 63 61 63 68 65 cache
50300 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
50310 68 61 73 20 74 77 6f 20 63 68 6f 69 63 65 73 3a has two choices:
50320 20 69 74 20 63 61 6e 20 72 65 74 75 72 6e 20 4e it can return N
50330 55 4c 4c 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 ULL,.**
50340 20 20 20 20 20 20 20 69 6e 20 77 68 69 63 68 20 in which
50350 63 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c case SQLite will
50360 20 61 74 74 65 6d 70 74 20 74 6f 20 75 6e 70 69 attempt to unpi
50370 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 0a 2a n one or more .*
50380 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
50390 20 70 61 67 65 73 20 62 65 66 6f 72 65 20 72 65 pages before re
503a0 2d 72 65 71 75 65 73 74 69 6e 67 20 74 68 65 20 -requesting the
503b0 73 61 6d 65 20 70 61 67 65 2c 20 6f 72 20 69 74 same page, or it
503c0 20 63 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 can.**
503d0 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 20 allocate
503e0 61 20 6e 65 77 20 70 61 67 65 20 61 6e 64 20 72 a new page and r
503f0 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
50400 74 6f 20 69 74 2e 20 49 66 20 61 20 6e 65 77 0a to it. If a new.
50410 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
50420 20 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 page is alloca
50430 74 65 64 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 ted, then it mus
50440 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 t be completely
50450 7a 65 72 6f 65 64 20 62 65 66 6f 72 65 20 0a 2a zeroed before .*
50460 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
50470 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e it is returned.
50480 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 64 3e 32 3c .** <tr><td>2<
50490 74 64 3e 49 66 20 63 72 65 61 74 65 46 6c 61 67 td>If createFlag
504a0 20 69 73 20 73 65 74 20 74 6f 20 32 2c 20 74 68 is set to 2, th
504b0 65 6e 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 en SQLite is not
504c0 20 68 6f 6c 64 69 6e 67 20 61 6e 79 0a 2a 2a 20 holding any.**
504d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
504e0 69 6e 6e 65 64 20 70 61 67 65 73 20 61 73 73 6f inned pages asso
504f0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
50500 73 70 65 63 69 66 69 63 20 63 61 63 68 65 20 70 specific cache p
50510 61 73 73 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 assed.**
50520 20 20 20 20 20 20 20 20 61 73 20 74 68 65 20 66 as the f
50530 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
50540 20 78 46 65 74 63 68 28 29 20 74 68 61 74 20 63 xFetch() that c
50550 61 6e 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20 an be unpinned.
50560 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 The.**
50570 20 20 20 20 20 20 63 61 63 68 65 20 69 6d 70 6c cache impl
50580 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c ementation shoul
50590 64 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c d attempt to all
505a0 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 20 ocate a new.**
505b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 61 ca
505c0 63 68 65 20 65 6e 74 72 79 20 61 6e 64 20 72 65 che entry and re
505d0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
505e0 6f 20 69 74 2e 20 41 67 61 69 6e 2c 20 74 68 65 o it. Again, the
505f0 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 new.**
50600 20 20 20 20 20 20 20 70 61 67 65 20 73 68 6f 75 page shou
50610 6c 64 20 62 65 20 7a 65 72 6f 65 64 20 62 65 66 ld be zeroed bef
50620 6f 72 65 20 69 74 20 69 73 20 72 65 74 75 72 6e ore it is return
50630 65 64 2e 20 49 66 20 74 68 65 20 78 46 65 74 63 ed. If the xFetc
50640 68 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 h().**
50650 20 20 20 20 20 20 6d 65 74 68 6f 64 20 72 65 74 method ret
50660 75 72 6e 73 20 4e 55 4c 4c 20 77 68 65 6e 20 63 urns NULL when c
50670 72 65 61 74 65 46 6c 61 67 3d 3d 32 2c 20 53 51 reateFlag==2, SQ
50680 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74 68 61 Lite assumes tha
50690 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
506a0 20 20 20 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c a memory all
506b0 6f 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 20 61 ocation failed a
506c0 6e 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 nd returns SQLIT
506d0 45 5f 4e 4f 4d 45 4d 20 74 6f 20 74 68 65 0a 2a E_NOMEM to the.*
506e0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
506f0 20 75 73 65 72 2e 0a 2a 2a 20 3c 2f 74 61 62 6c user..** </tabl
50700 65 3e 0a 2a 2a 0a 2a 2a 20 78 55 6e 70 69 6e 28 e>.**.** xUnpin(
50710 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 53 ) is called by S
50720 51 4c 69 74 65 20 77 69 74 68 20 61 20 70 6f 69 QLite with a poi
50730 6e 74 65 72 20 74 6f 20 61 20 63 75 72 72 65 6e nter to a curren
50740 74 6c 79 20 70 69 6e 6e 65 64 20 70 61 67 65 0a tly pinned page.
50750 2a 2a 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 ** as its second
50760 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74 68 argument. If th
50770 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 e third paramete
50780 72 2c 20 64 69 73 63 61 72 64 2c 20 69 73 20 6e r, discard, is n
50790 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e on-zero,.** then
507a0 20 74 68 65 20 70 61 67 65 20 73 68 6f 75 6c 64 the page should
507b0 20 62 65 20 65 76 69 63 74 65 64 20 66 72 6f 6d be evicted from
507c0 20 74 68 65 20 63 61 63 68 65 2e 20 49 6e 20 74 the cache. In t
507d0 68 69 73 20 63 61 73 65 20 53 51 4c 69 74 65 20 his case SQLite
507e0 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 .** assumes that
507f0 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74 the next time t
50800 68 65 20 70 61 67 65 20 69 73 20 72 65 74 72 69 he page is retri
50810 65 76 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 eved from the ca
50820 63 68 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 che using.** the
50830 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f 64 xFetch() method
50840 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 7a 65 72 , it will be zer
50850 6f 65 64 2e 20 49 66 20 74 68 65 20 64 69 73 63 oed. If the disc
50860 61 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 ard parameter is
50870 0a 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 .** zero, then t
50880 68 65 20 70 61 67 65 20 69 73 20 63 6f 6e 73 69 he page is consi
50890 64 65 72 65 64 20 74 6f 20 62 65 20 75 6e 70 69 dered to be unpi
508a0 6e 6e 65 64 2e 20 54 68 65 20 63 61 63 68 65 20 nned. The cache
508b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
508c0 2a 20 6d 61 79 20 63 68 6f 6f 73 65 20 74 6f 20 * may choose to
508d0 72 65 63 6c 61 69 6d 20 28 66 72 65 65 20 6f 72 reclaim (free or
508e0 20 72 65 63 79 63 6c 65 29 20 75 6e 70 69 6e 6e recycle) unpinn
508f0 65 64 20 70 61 67 65 73 20 61 74 20 61 6e 79 20 ed pages at any
50900 74 69 6d 65 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 time..** SQLite
50910 61 73 73 75 6d 65 73 20 74 68 61 74 20 6e 65 78 assumes that nex
50920 74 20 74 69 6d 65 20 74 68 65 20 70 61 67 65 20 t time the page
50930 69 73 20 72 65 74 72 69 65 76 65 64 20 66 72 6f is retrieved fro
50940 6d 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 69 m the cache.** i
50950 74 20 77 69 6c 6c 20 65 69 74 68 65 72 20 62 65 t will either be
50960 20 7a 65 72 6f 65 64 2c 20 6f 72 20 63 6f 6e 74 zeroed, or cont
50970 61 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 ain the same dat
50980 61 20 74 68 61 74 20 69 74 20 64 69 64 20 77 68 a that it did wh
50990 65 6e 20 69 74 0a 2a 2a 20 77 61 73 20 75 6e 70 en it.** was unp
509a0 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 inned..**.** The
509b0 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 72 65 cache is not re
509c0 71 75 69 72 65 64 20 74 6f 20 70 65 72 66 6f 72 quired to perfor
509d0 6d 20 61 6e 79 20 72 65 66 65 72 65 6e 63 65 20 m any reference
509e0 63 6f 75 6e 74 69 6e 67 2e 20 41 20 73 69 6e 67 counting. A sing
509f0 6c 65 20 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 le .** call to x
50a00 55 6e 70 69 6e 28 29 20 75 6e 70 69 6e 73 20 74 Unpin() unpins t
50a10 68 65 20 70 61 67 65 20 72 65 67 61 72 64 6c 65 he page regardle
50a20 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 ss of the number
50a30 20 6f 66 20 70 72 69 6f 72 20 63 61 6c 6c 73 20 of prior calls
50a40 0a 2a 2a 20 74 6f 20 78 46 65 74 63 68 28 29 2e .** to xFetch().
50a50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 65 6b 65 .**.** The xReke
50a60 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 73 y() method is us
50a70 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 ed to change the
50a80 20 6b 65 79 20 76 61 6c 75 65 20 61 73 73 6f 63 key value assoc
50a90 69 61 74 65 64 20 77 69 74 68 20 74 68 65 0a 2a iated with the.*
50aa0 2a 20 70 61 67 65 20 70 61 73 73 65 64 20 61 73 * page passed as
50ab0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
50ac0 6d 65 6e 74 20 66 72 6f 6d 20 6f 6c 64 4b 65 79 ment from oldKey
50ad0 20 74 6f 20 6e 65 77 4b 65 79 2e 20 49 66 20 74 to newKey. If t
50ae0 68 65 20 63 61 63 68 65 0a 2a 2a 20 70 72 65 76 he cache.** prev
50af0 69 6f 75 73 6c 79 20 63 6f 6e 74 61 69 6e 73 20 iously contains
50b00 61 6e 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61 an entry associa
50b10 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 2c ted with newKey,
50b20 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a it should be.**
50b30 20 64 69 73 63 61 72 64 65 64 2e 20 41 6e 79 20 discarded. Any
50b40 70 72 69 6f 72 20 63 61 63 68 65 20 65 6e 74 72 prior cache entr
50b50 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 y associated wit
50b60 68 20 6e 65 77 4b 65 79 20 69 73 20 67 75 61 72 h newKey is guar
50b70 61 6e 74 65 65 64 20 6e 6f 74 0a 2a 2a 20 74 6f anteed not.** to
50b80 20 62 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a be pinned..**.*
50b90 2a 20 57 68 65 6e 20 53 51 4c 69 74 65 20 63 61 * When SQLite ca
50ba0 6c 6c 73 20 74 68 65 20 78 54 72 75 6e 63 61 74 lls the xTruncat
50bb0 65 28 29 20 6d 65 74 68 6f 64 2c 20 74 68 65 20 e() method, the
50bc0 63 61 63 68 65 20 6d 75 73 74 20 64 69 73 63 61 cache must disca
50bd0 72 64 20 61 6c 6c 0a 2a 2a 20 65 78 69 73 74 69 rd all.** existi
50be0 6e 67 20 63 61 63 68 65 20 65 6e 74 72 69 65 73 ng cache entries
50bf0 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65 with page numbe
50c00 72 73 20 28 6b 65 79 73 29 20 67 72 65 61 74 65 rs (keys) greate
50c10 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a r than or equal.
50c20 2a 2a 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 ** to the value
50c30 6f 66 20 74 68 65 20 69 4c 69 6d 69 74 20 70 61 of the iLimit pa
50c40 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 rameter passed t
50c50 6f 20 78 54 72 75 6e 63 61 74 65 28 29 2e 20 49 o xTruncate(). I
50c60 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 f any.** of thes
50c70 65 20 70 61 67 65 73 20 61 72 65 20 70 69 6e 6e e pages are pinn
50c80 65 64 2c 20 74 68 65 79 20 61 72 65 20 69 6d 70 ed, they are imp
50c90 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 licitly unpinned
50ca0 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 0a 2a , meaning that.*
50cb0 2a 20 74 68 65 79 20 63 61 6e 20 62 65 20 73 61 * they can be sa
50cc0 66 65 6c 79 20 64 69 73 63 61 72 64 65 64 2e 0a fely discarded..
50cd0 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 73 74 72 **.** The xDestr
50ce0 6f 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 oy() method is u
50cf0 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20 61 20 sed to delete a
50d00 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 cache allocated
50d10 62 79 20 78 43 72 65 61 74 65 28 29 2e 0a 2a 2a by xCreate()..**
50d20 20 41 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61 All resources a
50d30 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
50d40 68 65 20 73 70 65 63 69 66 69 65 64 20 63 61 63 he specified cac
50d50 68 65 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 he should be fre
50d60 65 64 2e 20 41 66 74 65 72 0a 2a 2a 20 63 61 6c ed. After.** cal
50d70 6c 69 6e 67 20 74 68 65 20 78 44 65 73 74 72 6f ling the xDestro
50d80 79 28 29 20 6d 65 74 68 6f 64 2c 20 53 51 4c 69 y() method, SQLi
50d90 74 65 20 63 6f 6e 73 69 64 65 72 73 20 74 68 65 te considers the
50da0 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 [sqlite3_pcache
50db0 2a 5d 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 6e 76 *].** handle inv
50dc0 61 6c 69 64 2c 20 61 6e 64 20 77 69 6c 6c 20 6e alid, and will n
50dd0 6f 74 20 75 73 65 20 69 74 20 77 69 74 68 20 61 ot use it with a
50de0 6e 79 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33 ny other sqlite3
50df0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 0a _pcache_methods.
50e00 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f ** functions..*/
50e10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
50e20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d sqlite3_pcache_m
50e30 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 70 ethods sqlite3_p
50e40 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3b 0a 73 cache_methods;.s
50e50 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 truct sqlite3_pc
50e60 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 ache_methods {.
50e70 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 69 void *pArg;. i
50e80 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 nt (*xInit)(void
50e90 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 *);. void (*xSh
50ea0 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 0a utdown)(void*);.
50eb0 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 sqlite3_pcache
50ec0 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69 6e 74 *(*xCreate)(int
50ed0 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 50 75 szPage, int bPu
50ee0 72 67 65 61 62 6c 65 29 3b 0a 20 20 76 6f 69 64 rgeable);. void
50ef0 20 28 2a 78 43 61 63 68 65 73 69 7a 65 29 28 73 (*xCachesize)(s
50f00 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 qlite3_pcache*,
50f10 69 6e 74 20 6e 43 61 63 68 65 73 69 7a 65 29 3b int nCachesize);
50f20 0a 20 20 69 6e 74 20 28 2a 78 50 61 67 65 63 6f . int (*xPageco
50f30 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 70 63 61 unt)(sqlite3_pca
50f40 63 68 65 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28 che*);. void *(
50f50 2a 78 46 65 74 63 68 29 28 73 71 6c 69 74 65 33 *xFetch)(sqlite3
50f60 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e _pcache*, unsign
50f70 65 64 20 6b 65 79 2c 20 69 6e 74 20 63 72 65 61 ed key, int crea
50f80 74 65 46 6c 61 67 29 3b 0a 20 20 76 6f 69 64 20 teFlag);. void
50f90 28 2a 78 55 6e 70 69 6e 29 28 73 71 6c 69 74 65 (*xUnpin)(sqlite
50fa0 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a 3_pcache*, void*
50fb0 2c 20 69 6e 74 20 64 69 73 63 61 72 64 29 3b 0a , int discard);.
50fc0 20 20 76 6f 69 64 20 28 2a 78 52 65 6b 65 79 29 void (*xRekey)
50fd0 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
50fe0 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65 , void*, unsigne
50ff0 64 20 6f 6c 64 4b 65 79 2c 20 75 6e 73 69 67 6e d oldKey, unsign
51000 65 64 20 6e 65 77 4b 65 79 29 3b 0a 20 20 76 6f ed newKey);. vo
51010 69 64 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28 id (*xTruncate)(
51020 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c sqlite3_pcache*,
51030 20 75 6e 73 69 67 6e 65 64 20 69 4c 69 6d 69 74 unsigned iLimit
51040 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 );. void (*xDes
51050 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 70 63 troy)(sqlite3_pc
51060 61 63 68 65 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a ache*);.};../*.*
51070 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 * Undo the hack
51080 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c that converts fl
51090 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 oating point typ
510a0 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f es to integer fo
510b0 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70 r.** builds on p
510c0 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75 rocessors withou
510d0 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 t floating point
510e0 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66 support..*/.#if
510f0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
51100 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 FLOATING_POINT.#
51110 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 65 undef double.#e
51120 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f ndif..#if 0.} /
51130 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65 78 * End of the 'ex
51140 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20 tern "C"' block
51150 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 */.#endif.#endif
51160 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
51170 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 * End of sqlite3
51180 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
51190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
511a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
511b0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
511c0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
511d0 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
511e0 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a n sqliteInt.h **
511f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51200 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
51210 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 68 2e 68 * Include hash.h
51220 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
51230 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a f sqliteInt.h **
51240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51250 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
51260 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 * Begin file has
51270 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h.h ************
51280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
512a0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
512b0 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 tember 22.**.**
512c0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
512d0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
512e0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
512f0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
51300 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
51310 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
51320 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
51330 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
51340 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
51350 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
51360 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
51370 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
51380 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
51390 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
513a0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
513b0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
513c0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
513d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
513e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
513f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51410 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
51420 20 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66 is the header f
51430 69 6c 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 ile for the gene
51440 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 ric hash-table i
51450 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 mplemenation.**
51460 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a used in SQLite..
51470 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e **.** $Id: hash.
51480 68 2c 76 20 31 2e 31 32 20 32 30 30 38 2f 31 30 h,v 1.12 2008/10
51490 2f 31 30 20 31 37 3a 34 31 3a 32 39 20 64 72 68 /10 17:41:29 drh
514a0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 Exp $.*/.#ifnde
514b0 66 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 f _SQLITE_HASH_H
514c0 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 _.#define _SQLIT
514d0 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f E_HASH_H_../* Fo
514e0 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f rward declaratio
514f0 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 73 ns of structures
51500 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 . */.typedef str
51510 75 63 74 20 48 61 73 68 20 48 61 73 68 3b 0a 74 uct Hash Hash;.t
51520 79 70 65 64 65 66 20 73 74 72 75 63 74 20 48 61 ypedef struct Ha
51530 73 68 45 6c 65 6d 20 48 61 73 68 45 6c 65 6d 3b shElem HashElem;
51540 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 ../* A complete
51550 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e hash table is an
51560 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
51570 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
51580 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 74 ture..** The int
51590 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 ernals of this s
515a0 74 72 75 63 74 75 72 65 20 61 72 65 20 69 6e 74 tructure are int
515b0 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 ended to be opaq
515c0 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20 ue -- client.**
515d0 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 code should not
515e0 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63 65 73 attempt to acces
515f0 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 20 s or modify the
51600 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73 fields of this s
51610 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 65 tructure.** dire
51620 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 68 ctly. Change th
51630 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c is structure onl
51640 79 20 62 79 20 75 73 69 6e 67 20 74 68 65 20 72 y by using the r
51650 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a outines below..*
51660 2a 20 48 6f 77 65 76 65 72 2c 20 6d 61 6e 79 20 * However, many
51670 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64 75 72 of the "procedur
51680 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74 69 6f es" and "functio
51690 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 6e ns" for modifyin
516a0 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 69 g and.** accessi
516b0 6e 67 20 74 68 69 73 20 73 74 72 75 63 74 75 72 ng this structur
516c0 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 63 e are really mac
516d0 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 74 ros, so we can't
516e0 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20 really make.**
516f0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f this structure o
51700 70 61 71 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 paque..*/.struct
51710 20 48 61 73 68 20 7b 0a 20 20 75 6e 73 69 67 6e Hash {. unsign
51720 65 64 20 69 6e 74 20 63 6f 70 79 4b 65 79 3a 20 ed int copyKey:
51730 31 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 1; /* True if c
51740 6f 70 79 20 6f 66 20 6b 65 79 20 6d 61 64 65 20 opy of key made
51750 6f 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 75 on insert */. u
51760 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 74 73 69 nsigned int htsi
51770 7a 65 20 3a 20 33 31 3b 20 2f 2a 20 4e 75 6d 62 ze : 31; /* Numb
51780 65 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 6e er of buckets in
51790 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
517a0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e */. unsigned in
517b0 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f t count; /
517c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
517d0 69 65 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c ies in this tabl
517e0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 e */. HashElem
517f0 2a 66 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 *first;
51800 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 65 6c /* The first el
51810 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72 ement of the arr
51820 61 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f ay */. struct _
51830 68 74 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 ht {
51840 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 74 61 /* the hash ta
51850 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 ble */. int c
51860 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 ount;
51870 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
51880 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 of entries with
51890 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 this hash */.
518a0 20 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e HashElem *chain
518b0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 ; /* P
518c0 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 ointer to first
518d0 65 6e 74 72 79 20 77 69 74 68 20 74 68 69 73 20 entry with this
518e0 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b hash */. } *ht;
518f0 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 .};../* Each ele
51900 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73 68 ment in the hash
51910 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 table is an ins
51920 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
51930 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 lowing .** struc
51940 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 ture. All eleme
51950 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f nts are stored o
51960 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c n a single doubl
51970 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a y-linked list..*
51980 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 *.** Again, this
51990 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e structure is in
519a0 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 tended to be opa
519b0 71 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27 que, but it can'
519c0 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f t really.** be o
519d0 70 61 71 75 65 20 62 65 63 61 75 73 65 20 69 74 paque because it
519e0 20 69 73 20 75 73 65 64 20 62 79 20 6d 61 63 72 is used by macr
519f0 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 os..*/.struct Ha
51a00 73 68 45 6c 65 6d 20 7b 0a 20 20 48 61 73 68 45 shElem {. HashE
51a10 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 lem *next, *prev
51a20 3b 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64 20 ; /* Next and
51a30 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 previous element
51a40 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 2a s in the table *
51a50 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 20 /. void *data;
51a60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
51a70 44 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 Data associated
51a80 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e with this elemen
51a90 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65 t */. void *pKe
51aa0 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20 y; int nKey;
51ab0 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 /* Key associate
51ac0 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d d with this elem
51ad0 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ent */.};../*.**
51ae0 20 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 Access routines
51af0 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e . To delete, in
51b00 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e sert a NULL poin
51b10 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ter..*/.SQLITE_P
51b20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
51b30 74 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 te3HashInit(Hash
51b40 2a 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 3b *, int copyKey);
51b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
51b60 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 void *sqlite3Has
51b70 68 49 6e 73 65 72 74 28 48 61 73 68 2a 2c 20 63 hInsert(Hash*, c
51b80 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c onst void *pKey,
51b90 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 int nKey, void
51ba0 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f *pData);.SQLITE_
51bb0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
51bc0 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f lite3HashFind(co
51bd0 6e 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73 74 nst Hash*, const
51be0 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 void *pKey, int
51bf0 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 nKey);.SQLITE_P
51c00 52 49 56 41 54 45 20 48 61 73 68 45 6c 65 6d 20 RIVATE HashElem
51c10 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 *sqlite3HashFind
51c20 45 6c 65 6d 28 63 6f 6e 73 74 20 48 61 73 68 2a Elem(const Hash*
51c30 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b , const void *pK
51c40 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 ey, int nKey);.S
51c50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
51c60 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c id sqlite3HashCl
51c70 65 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a ear(Hash*);../*.
51c80 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f ** Macros for lo
51c90 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 oping over all e
51ca0 6c 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 lements of a has
51cb0 68 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 h table. The id
51cc0 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 iom is.** like t
51cd0 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73 his:.**.** Has
51ce0 68 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c h h;.** HashEl
51cf0 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a em *p;.** ....
51d00 2a 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 ** for(p=sqlit
51d10 65 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 eHashFirst(&h);
51d20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e p; p=sqliteHashN
51d30 65 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 ext(p)){.**
51d40 53 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 SomeStructure *p
51d50 44 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73 Data = sqliteHas
51d60 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20 hData(p);.**
51d70 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 // do something
51d80 20 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20 with pData.**
51d90 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 }.*/.#define sq
51da0 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 48 29 liteHashFirst(H)
51db0 20 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 ((H)->first).#
51dc0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 define sqliteHas
51dd0 68 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d hNext(E) ((E)-
51de0 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 73 >next).#define s
51df0 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 29 qliteHashData(E)
51e00 20 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 23 ((E)->data).#
51e10 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 define sqliteHas
51e20 68 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d hKey(E) ((E)-
51e30 3e 70 4b 65 79 29 0a 23 64 65 66 69 6e 65 20 73 >pKey).#define s
51e40 71 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65 qliteHashKeysize
51e50 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a (E) ((E)->nKey).
51e60 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 ./*.** Number of
51e70 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61 entries in a ha
51e80 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66 sh table.*/.#def
51e90 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 43 6f ine sqliteHashCo
51ea0 75 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f unt(H) ((H)->co
51eb0 75 6e 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 unt)..#endif /*
51ec0 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 20 _SQLITE_HASH_H_
51ed0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
51ee0 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e *** End of hash.
51ef0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
51f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51f20 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
51f30 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
51f40 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
51f50 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
51f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51f70 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
51f80 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 72 73 *** Include pars
51f90 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c e.h in the middl
51fa0 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 e of sqliteInt.h
51fb0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
51fc0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
51fd0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 *** Begin file p
51fe0 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a arse.h *********
51ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52010 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 **/.#define TK_S
52020 45 4d 49 20 20 20 20 20 20 20 20 20 20 20 20 20 EMI
52030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
52040 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 50 4c .#define TK_EXPL
52050 41 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 AIN
52060 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 2.#d
52070 65 66 69 6e 65 20 54 4b 5f 51 55 45 52 59 20 20 efine TK_QUERY
52080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52090 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 3.#defi
520a0 6e 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20 20 ne TK_PLAN
520b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
520c0 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4.#define
520d0 54 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 20 20 TK_BEGIN
520e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
520f0 20 20 20 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 5.#define TK_
52100 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 TRANSACTION
52110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52120 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 6.#define TK_DEF
52130 45 52 52 45 44 20 20 20 20 20 20 20 20 20 20 20 ERRED
52140 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 7.#
52150 64 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44 49 define TK_IMMEDI
52160 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ATE
52170 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 8.#def
52180 69 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 ine TK_EXCLUSIVE
52190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
521a0 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 9.#define
521b0 20 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20 20 TK_COMMIT
521c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
521d0 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 10.#define TK
521e0 5f 45 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 _END
521f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52200 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 11.#define TK_RO
52210 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20 20 LLBACK
52220 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 12.
52230 23 64 65 66 69 6e 65 20 54 4b 5f 53 41 56 45 50 #define TK_SAVEP
52240 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 OINT
52250 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 13.#de
52260 66 69 6e 65 20 54 4b 5f 52 45 4c 45 41 53 45 20 fine TK_RELEASE
52270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52280 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 14.#defin
52290 65 20 54 4b 5f 54 4f 20 20 20 20 20 20 20 20 20 e TK_TO
522a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
522b0 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 54 15.#define T
522c0 4b 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 20 K_CREATE
522d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
522e0 20 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 16.#define TK_T
522f0 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 ABLE
52300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 17
52310 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 46 20 20 .#define TK_IF
52320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52330 20 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 18.#d
52340 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 20 20 20 20 efine TK_NOT
52350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52360 20 20 20 20 20 20 20 20 31 39 0a 23 64 65 66 69 19.#defi
52370 6e 65 20 54 4b 5f 45 58 49 53 54 53 20 20 20 20 ne TK_EXISTS
52380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52390 20 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 20.#define
523a0 54 4b 5f 54 45 4d 50 20 20 20 20 20 20 20 20 20 TK_TEMP
523b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
523c0 20 20 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 21.#define TK_
523d0 4c 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LP
523e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
523f0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 50 20 2.#define TK_RP
52400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52410 20 20 20 20 20 20 20 20 20 20 20 20 32 33 0a 23 23.#
52420 64 65 66 69 6e 65 20 54 4b 5f 41 53 20 20 20 20 define TK_AS
52430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52440 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 65 66 24.#def
52450 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41 20 20 20 20 ine TK_COMMA
52460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52470 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65 25.#define
52480 20 54 4b 5f 49 44 20 20 20 20 20 20 20 20 20 20 TK_ID
52490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
524a0 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 26.#define TK
524b0 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20 20 20 _ABORT
524c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
524d0 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 46 27.#define TK_AF
524e0 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 TER
524f0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 28.
52500 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 41 4c 59 #define TK_ANALY
52510 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ZE
52520 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 29.#de
52530 66 69 6e 65 20 54 4b 5f 41 53 43 20 20 20 20 20 fine TK_ASC
52540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52550 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e 30.#defin
52560 65 20 54 4b 5f 41 54 54 41 43 48 20 20 20 20 20 e TK_ATTACH
52570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52580 20 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20 54 31.#define T
52590 4b 5f 42 45 46 4f 52 45 20 20 20 20 20 20 20 20 K_BEFORE
525a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
525b0 20 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 32.#define TK_C
525c0 41 53 43 41 44 45 20 20 20 20 20 20 20 20 20 20 ASCADE
525d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33 33
525e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 54 .#define TK_CAST
525f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52600 20 20 20 20 20 20 20 20 20 20 20 33 34 0a 23 64 34.#d
52610 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 4c 49 43 efine TK_CONFLIC
52620 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
52630 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 35.#defi
52640 6e 65 20 54 4b 5f 44 41 54 41 42 41 53 45 20 20 ne TK_DATABASE
52650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52660 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 36.#define
52670 54 4b 5f 44 45 53 43 20 20 20 20 20 20 20 20 20 TK_DESC
52680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52690 20 20 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 37.#define TK_
526a0 44 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 DETACH
526b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
526c0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 41 43 8.#define TK_EAC
526d0 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H
526e0 20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 39.#
526f0 64 65 66 69 6e 65 20 54 4b 5f 46 41 49 4c 20 20 define TK_FAIL
52700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52710 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 65 66 40.#def
52720 69 6e 65 20 54 4b 5f 46 4f 52 20 20 20 20 20 20 ine TK_FOR
52730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52740 20 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 41.#define
52750 20 54 4b 5f 49 47 4e 4f 52 45 20 20 20 20 20 20 TK_IGNORE
52760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52770 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 42.#define TK
52780 5f 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 _INITIALLY
52790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
527a0 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 43.#define TK_IN
527b0 53 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20 STEAD
527c0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 44.
527d0 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f #define TK_LIKE_
527e0 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 KW
527f0 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 65 45.#de
52800 66 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20 20 fine TK_MATCH
52810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52820 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 46.#defin
52830 65 20 54 4b 5f 4b 45 59 20 20 20 20 20 20 20 20 e TK_KEY
52840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52850 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20 54 47.#define T
52860 4b 5f 4f 46 20 20 20 20 20 20 20 20 20 20 20 20 K_OF
52870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52880 20 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 48.#define TK_O
52890 46 46 53 45 54 20 20 20 20 20 20 20 20 20 20 20 FFSET
528a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39 49
528b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 41 47 .#define TK_PRAG
528c0 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MA
528d0 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 64 50.#d
528e0 65 66 69 6e 65 20 54 4b 5f 52 41 49 53 45 20 20 efine TK_RAISE
528f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52900 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 66 69 51.#defi
52910 6e 65 20 54 4b 5f 52 45 50 4c 41 43 45 20 20 20 ne TK_REPLACE
52920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52930 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 52.#define
52940 54 4b 5f 52 45 53 54 52 49 43 54 20 20 20 20 20 TK_RESTRICT
52950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52960 20 20 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53.#define TK_
52970 52 4f 57 20 20 20 20 20 20 20 20 20 20 20 20 20 ROW
52980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
52990 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 49 4.#define TK_TRI
529a0 47 47 45 52 20 20 20 20 20 20 20 20 20 20 20 20 GGER
529b0 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a 23 55.#
529c0 64 65 66 69 6e 65 20 54 4b 5f 56 41 43 55 55 4d define TK_VACUUM
529d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
529e0 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 56.#def
529f0 69 6e 65 20 54 4b 5f 56 49 45 57 20 20 20 20 20 ine TK_VIEW
52a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52a10 20 20 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65 57.#define
52a20 20 54 4b 5f 56 49 52 54 55 41 4c 20 20 20 20 20 TK_VIRTUAL
52a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52a40 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 54 4b 58.#define TK
52a50 5f 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 _REINDEX
52a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52a70 35 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 59.#define TK_RE
52a80 4e 41 4d 45 20 20 20 20 20 20 20 20 20 20 20 20 NAME
52a90 20 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 60.
52aa0 23 64 65 66 69 6e 65 20 54 4b 5f 43 54 49 4d 45 #define TK_CTIME
52ab0 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 _KW
52ac0 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65 61.#de
52ad0 66 69 6e 65 20 54 4b 5f 41 4e 59 20 20 20 20 20 fine TK_ANY
52ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52af0 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e 62.#defin
52b00 65 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 20 20 e TK_OR
52b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52b20 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 54 63.#define T
52b30 4b 5f 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 K_AND
52b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52b50 20 36 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 64.#define TK_I
52b60 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 S
52b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35 65
52b80 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 54 57 .#define TK_BETW
52b90 45 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 EEN
52ba0 20 20 20 20 20 20 20 20 20 20 20 36 36 0a 23 64 66.#d
52bb0 65 66 69 6e 65 20 54 4b 5f 49 4e 20 20 20 20 20 efine TK_IN
52bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52bd0 20 20 20 20 20 20 20 20 36 37 0a 23 64 65 66 69 67.#defi
52be0 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 ne TK_ISNULL
52bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52c00 20 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65 20 68.#define
52c10 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 TK_NOTNULL
52c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52c30 20 20 36 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 69.#define TK_
52c40 4e 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NE
52c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
52c60 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 51 20 0.#define TK_EQ
52c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52c80 20 20 20 20 20 20 20 20 20 20 20 20 37 31 0a 23 71.#
52c90 64 65 66 69 6e 65 20 54 4b 5f 47 54 20 20 20 20 define TK_GT
52ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52cb0 20 20 20 20 20 20 20 20 20 37 32 0a 23 64 65 66 72.#def
52cc0 69 6e 65 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 ine TK_LE
52cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52ce0 20 20 20 20 20 20 37 33 0a 23 64 65 66 69 6e 65 73.#define
52cf0 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20 20 TK_LT
52d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52d10 20 20 20 37 34 0a 23 64 65 66 69 6e 65 20 54 4b 74.#define TK
52d20 5f 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20 _GE
52d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52d40 37 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 53 75.#define TK_ES
52d50 43 41 50 45 20 20 20 20 20 20 20 20 20 20 20 20 CAPE
52d60 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 0a 76.
52d70 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 41 4e #define TK_BITAN
52d80 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 D
52d90 20 20 20 20 20 20 20 20 20 20 37 37 0a 23 64 65 77.#de
52da0 66 69 6e 65 20 54 4b 5f 42 49 54 4f 52 20 20 20 fine TK_BITOR
52db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52dc0 20 20 20 20 20 20 20 37 38 0a 23 64 65 66 69 6e 78.#defin
52dd0 65 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 20 20 e TK_LSHIFT
52de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52df0 20 20 20 20 37 39 0a 23 64 65 66 69 6e 65 20 54 79.#define T
52e00 4b 5f 52 53 48 49 46 54 20 20 20 20 20 20 20 20 K_RSHIFT
52e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52e20 20 38 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 80.#define TK_P
52e30 4c 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 LUS
52e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31 81
52e50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 49 4e 55 .#define TK_MINU
52e60 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 S
52e70 20 20 20 20 20 20 20 20 20 20 20 38 32 0a 23 64 82.#d
52e80 65 66 69 6e 65 20 54 4b 5f 53 54 41 52 20 20 20 efine TK_STAR
52e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52ea0 20 20 20 20 20 20 20 20 38 33 0a 23 64 65 66 69 83.#defi
52eb0 6e 65 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 20 ne TK_SLASH
52ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52ed0 20 20 20 20 20 38 34 0a 23 64 65 66 69 6e 65 20 84.#define
52ee0 54 4b 5f 52 45 4d 20 20 20 20 20 20 20 20 20 20 TK_REM
52ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52f00 20 20 38 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 85.#define TK_
52f10 43 4f 4e 43 41 54 20 20 20 20 20 20 20 20 20 20 CONCAT
52f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
52f30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 6.#define TK_COL
52f40 4c 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 LATE
52f50 20 20 20 20 20 20 20 20 20 20 20 20 38 37 0a 23 87.#
52f60 64 65 66 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 define TK_UMINUS
52f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52f80 20 20 20 20 20 20 20 20 20 38 38 0a 23 64 65 66 88.#def
52f90 69 6e 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20 ine TK_UPLUS
52fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52fb0 20 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65 89.#define
52fc0 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 20 20 20 TK_BITNOT
52fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52fe0 20 20 20 39 30 0a 23 64 65 66 69 6e 65 20 54 4b 90.#define TK
52ff0 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20 _STRING
53000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53010 39 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 91.#define TK_JO
53020 49 4e 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20 IN_KW
53030 20 20 20 20 20 20 20 20 20 20 20 20 20 39 32 0a 92.
53040 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 #define TK_CONST
53050 52 41 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 RAINT
53060 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 64 65 93.#de
53070 66 69 6e 65 20 54 4b 5f 44 45 46 41 55 4c 54 20 fine TK_DEFAULT
53080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53090 20 20 20 20 20 20 20 39 34 0a 23 64 65 66 69 6e 94.#defin
530a0 65 20 54 4b 5f 4e 55 4c 4c 20 20 20 20 20 20 20 e TK_NULL
530b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
530c0 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 54 95.#define T
530d0 4b 5f 50 52 49 4d 41 52 59 20 20 20 20 20 20 20 K_PRIMARY
530e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
530f0 20 39 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 96.#define TK_U
53100 4e 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20 NIQUE
53110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 37 97
53120 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 48 45 43 .#define TK_CHEC
53130 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K
53140 20 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64 98.#d
53150 65 66 69 6e 65 20 54 4b 5f 52 45 46 45 52 45 4e efine TK_REFEREN
53160 43 45 53 20 20 20 20 20 20 20 20 20 20 20 20 20 CES
53170 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69 99.#defi
53180 6e 65 20 54 4b 5f 41 55 54 4f 49 4e 43 52 20 20 ne TK_AUTOINCR
53190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
531a0 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 100.#define
531b0 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 20 20 20 TK_ON
531c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
531d0 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20 54 101.#define T
531e0 4b 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 K_DELETE
531f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53200 20 31 30 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 102.#define TK_
53210 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 UPDATE
53220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
53230 30 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 03.#define TK_IN
53240 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20 20 SERT
53250 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34 104
53260 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 54 20 .#define TK_SET
53270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53280 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 105.#
53290 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 define TK_DEFERR
532a0 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 ABLE
532b0 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 106.#de
532c0 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20 fine TK_FOREIGN
532d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
532e0 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66 69 107.#defi
532f0 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20 20 20 ne TK_DROP
53300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53310 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e 65 108.#define
53320 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20 TK_UNION
53330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53340 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 54 109.#define T
53350 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 K_ALL
53360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53370 20 31 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 110.#define TK_
53380 45 58 43 45 50 54 20 20 20 20 20 20 20 20 20 20 EXCEPT
53390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
533a0 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 11.#define TK_IN
533b0 54 45 52 53 45 43 54 20 20 20 20 20 20 20 20 20 TERSECT
533c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 32 112
533d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45 .#define TK_SELE
533e0 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CT
533f0 20 20 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 113.#
53400 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e define TK_DISTIN
53410 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CT
53420 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 114.#de
53430 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20 20 20 fine TK_DOT
53440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53450 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 115.#defi
53460 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20 ne TK_FROM
53470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53480 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e 65 116.#define
53490 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20 TK_JOIN
534a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
534b0 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 54 117.#define T
534c0 4b 5f 49 4e 44 45 58 45 44 20 20 20 20 20 20 20 K_INDEXED
534d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
534e0 20 31 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 118.#define TK_
534f0 42 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 BY
53500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
53510 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 53 19.#define TK_US
53520 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 ING
53530 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 30 120
53540 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 44 45 .#define TK_ORDE
53550 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 R
53560 20 20 20 20 20 20 20 20 20 20 20 31 32 31 0a 23 121.#
53570 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55 50 20 define TK_GROUP
53580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53590 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 122.#de
535a0 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47 20 20 fine TK_HAVING
535b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
535c0 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 123.#defi
535d0 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20 20 20 ne TK_LIMIT
535e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
535f0 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65 124.#define
53600 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20 20 20 TK_WHERE
53610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53620 20 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20 54 125.#define T
53630 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20 20 20 K_INTO
53640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53650 20 31 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 126.#define TK_
53660 56 41 4c 55 45 53 20 20 20 20 20 20 20 20 20 20 VALUES
53670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
53680 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 27.#define TK_IN
53690 54 45 47 45 52 20 20 20 20 20 20 20 20 20 20 20 TEGER
536a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38 128
536b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c 4f 41 .#define TK_FLOA
536c0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
536d0 20 20 20 20 20 20 20 20 20 20 20 31 32 39 0a 23 129.#
536e0 64 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42 20 20 define TK_BLOB
536f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53700 20 20 20 20 20 20 20 20 20 31 33 30 0a 23 64 65 130.#de
53710 66 69 6e 65 20 54 4b 5f 52 45 47 49 53 54 45 52 fine TK_REGISTER
53720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53730 20 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 131.#defi
53740 6e 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 20 20 ne TK_VARIABLE
53750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53760 20 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 132.#define
53770 20 54 4b 5f 43 41 53 45 20 20 20 20 20 20 20 20 TK_CASE
53780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53790 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 54 133.#define T
537a0 4b 5f 57 48 45 4e 20 20 20 20 20 20 20 20 20 20 K_WHEN
537b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
537c0 20 31 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 134.#define TK_
537d0 54 48 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 THEN
537e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
537f0 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4c 35.#define TK_EL
53800 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 SE
53810 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 136
53820 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45 .#define TK_INDE
53830 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 X
53840 20 20 20 20 20 20 20 20 20 20 20 31 33 37 0a 23 137.#
53850 64 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45 52 20 define TK_ALTER
53860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53870 20 20 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 138.#de
53880 66 69 6e 65 20 54 4b 5f 41 44 44 20 20 20 20 20 fine TK_ADD
53890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
538a0 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69 139.#defi
538b0 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 20 ne TK_COLUMNKW
538c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
538d0 20 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e 65 140.#define
538e0 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20 20 20 20 TK_TO_TEXT
538f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53900 20 20 20 31 34 31 0a 23 64 65 66 69 6e 65 20 54 141.#define T
53910 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20 20 20 20 20 K_TO_BLOB
53920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53930 20 31 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 142.#define TK_
53940 54 4f 5f 4e 55 4d 45 52 49 43 20 20 20 20 20 20 TO_NUMERIC
53950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
53960 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 43.#define TK_TO
53970 5f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 _INT
53980 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 34 144
53990 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 52 .#define TK_TO_R
539a0 45 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 EAL
539b0 20 20 20 20 20 20 20 20 20 20 20 31 34 35 0a 23 145.#
539c0 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 define TK_END_OF
539d0 5f 46 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 _FILE
539e0 20 20 20 20 20 20 20 20 20 31 34 36 0a 23 64 65 146.#de
539f0 66 69 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 fine TK_ILLEGAL
53a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53a10 20 20 20 20 20 20 20 31 34 37 0a 23 64 65 66 69 147.#defi
53a20 6e 65 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20 ne TK_SPACE
53a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53a40 20 20 20 20 20 31 34 38 0a 23 64 65 66 69 6e 65 148.#define
53a50 20 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52 TK_UNCLOSED_STR
53a60 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 ING
53a70 20 20 20 31 34 39 0a 23 64 65 66 69 6e 65 20 54 149.#define T
53a80 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 K_FUNCTION
53a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53aa0 20 31 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 150.#define TK_
53ab0 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 COLUMN
53ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
53ad0 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 51.#define TK_AG
53ae0 47 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 G_FUNCTION
53af0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 32 152
53b00 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f .#define TK_AGG_
53b10 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 COLUMN
53b20 20 20 20 20 20 20 20 20 20 20 20 31 35 33 0a 23 153.#
53b30 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f define TK_CONST_
53b40 46 55 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 FUNC
53b50 20 20 20 20 20 20 20 20 20 31 35 34 0a 0a 2f 2a 154../*
53b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
53b70 64 20 6f 66 20 70 61 72 73 65 2e 68 20 2a 2a 2a d of parse.h ***
53b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
53bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
53bc0 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
53bd0 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 e left off in sq
53be0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
53bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 ************/.#i
53c00 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e nclude <stdio.h>
53c10 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 .#include <stdli
53c20 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 b.h>.#include <s
53c30 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 tring.h>.#includ
53c40 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e e <assert.h>.#in
53c50 63 6c 75 64 65 20 3c 73 74 64 64 65 66 2e 68 3e clude <stddef.h>
53c60 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 ../*.** If compi
53c70 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65 ling for a proce
53c80 73 73 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20 ssor that lacks
53c90 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 floating point s
53ca0 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74 upport,.** subst
53cb0 69 74 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f itute integer fo
53cc0 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 r floating-point
53cd0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
53ce0 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
53cf0 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 POINT.# define d
53d00 6f 75 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e 74 ouble sqlite_int
53d10 36 34 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 64.# define LONG
53d20 44 4f 55 42 4c 45 5f 54 59 50 45 20 73 71 6c 69 DOUBLE_TYPE sqli
53d30 74 65 5f 69 6e 74 36 34 0a 23 20 69 66 6e 64 65 te_int64.# ifnde
53d40 66 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c f SQLITE_BIG_DBL
53d50 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 .# define SQLI
53d60 54 45 5f 42 49 47 5f 44 42 4c 20 28 30 78 37 66 TE_BIG_DBL (0x7f
53d70 66 66 66 66 66 66 66 66 66 66 66 66 66 66 29 0a ffffffffffffff).
53d80 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 # endif.# define
53d90 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 SQLITE_OMIT_DAT
53da0 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20 ETIME_FUNCS 1.#
53db0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d define SQLITE_OM
53dc0 49 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64 IT_TRACE 1.# und
53dd0 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f ef SQLITE_MIXED_
53de0 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f ENDIAN_64BIT_FLO
53df0 41 54 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 AT.#endif.#ifnde
53e00 66 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c f SQLITE_BIG_DBL
53e10 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
53e20 5f 42 49 47 5f 44 42 4c 20 28 31 65 39 39 29 0a _BIG_DBL (1e99).
53e30 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 4d #endif../*.** OM
53e40 49 54 5f 54 45 4d 50 44 42 20 69 73 20 73 65 74 IT_TEMPDB is set
53e50 20 74 6f 20 31 20 69 66 20 53 51 4c 49 54 45 5f to 1 if SQLITE_
53e60 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 73 20 64 OMIT_TEMPDB is d
53e70 65 66 69 6e 65 64 2c 20 6f 72 20 30 0a 2a 2a 20 efined, or 0.**
53e80 61 66 74 65 72 77 61 72 64 2e 20 48 61 76 69 6e afterward. Havin
53e90 67 20 74 68 69 73 20 6d 61 63 72 6f 20 61 6c 6c g this macro all
53ea0 6f 77 73 20 75 73 20 74 6f 20 63 61 75 73 65 20 ows us to cause
53eb0 74 68 65 20 43 20 63 6f 6d 70 69 6c 65 72 20 0a the C compiler .
53ec0 2a 2a 20 74 6f 20 6f 6d 69 74 20 63 6f 64 65 20 ** to omit code
53ed0 75 73 65 64 20 62 79 20 54 45 4d 50 20 74 61 62 used by TEMP tab
53ee0 6c 65 73 20 77 69 74 68 6f 75 74 20 6d 65 73 73 les without mess
53ef0 79 20 23 69 66 6e 64 65 66 20 73 74 61 74 65 6d y #ifndef statem
53f00 65 6e 74 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ents..*/.#ifdef
53f10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 SQLITE_OMIT_TEMP
53f20 44 42 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f DB.#define OMIT_
53f30 54 45 4d 50 44 42 20 31 0a 23 65 6c 73 65 0a 23 TEMPDB 1.#else.#
53f40 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 define OMIT_TEMP
53f50 44 42 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a DB 0.#endif../*.
53f60 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** If the follow
53f70 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 ing macro is set
53f80 20 74 6f 20 31 2c 20 74 68 65 6e 20 4e 55 4c 4c to 1, then NULL
53f90 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 values are cons
53fa0 69 64 65 72 65 64 0a 2a 2a 20 64 69 73 74 69 6e idered.** distin
53fb0 63 74 20 77 68 65 6e 20 64 65 74 65 72 6d 69 6e ct when determin
53fc0 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e ing whether or n
53fd0 6f 74 20 74 77 6f 20 65 6e 74 72 69 65 73 20 61 ot two entries a
53fe0 72 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 69 re the same.** i
53ff0 6e 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 n a UNIQUE index
54000 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 77 . This is the w
54010 61 79 20 50 6f 73 74 67 72 65 53 51 4c 2c 20 4f ay PostgreSQL, O
54020 72 61 63 6c 65 2c 20 44 42 32 2c 20 4d 79 53 51 racle, DB2, MySQ
54030 4c 2c 0a 2a 2a 20 4f 43 45 4c 4f 54 2c 20 61 6e L,.** OCELOT, an
54040 64 20 46 69 72 65 62 69 72 64 20 61 6c 6c 20 77 d Firebird all w
54050 6f 72 6b 2e 20 20 54 68 65 20 53 51 4c 39 32 20 ork. The SQL92
54060 73 70 65 63 20 65 78 70 6c 69 63 69 74 6c 79 20 spec explicitly
54070 73 61 79 73 20 74 68 69 73 0a 2a 2a 20 69 73 20 says this.** is
54080 74 68 65 20 77 61 79 20 74 68 69 6e 67 73 20 61 the way things a
54090 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 77 6f re suppose to wo
540a0 72 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 rk..**.** If the
540b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f following macro
540c0 20 69 73 20 73 65 74 20 74 6f 20 30 2c 20 74 68 is set to 0, th
540d0 65 20 4e 55 4c 4c 73 20 61 72 65 20 69 6e 64 69 e NULLs are indi
540e0 73 74 69 6e 63 74 20 66 6f 72 0a 2a 2a 20 61 20 stinct for.** a
540f0 55 4e 49 51 55 45 20 69 6e 64 65 78 2e 20 20 49 UNIQUE index. I
54100 6e 20 74 68 69 73 20 6d 6f 64 65 2c 20 79 6f 75 n this mode, you
54110 20 63 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 61 can only have a
54120 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 20 65 6e 74 single NULL ent
54130 72 79 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6c 75 ry.** for a colu
54140 6d 6e 20 64 65 63 6c 61 72 65 64 20 55 4e 49 51 mn declared UNIQ
54150 55 45 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 UE. This is the
54160 20 77 61 79 20 49 6e 66 6f 72 6d 69 78 20 61 6e way Informix an
54170 64 20 53 51 4c 20 53 65 72 76 65 72 0a 2a 2a 20 d SQL Server.**
54180 77 6f 72 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 work..*/.#define
54190 20 4e 55 4c 4c 5f 44 49 53 54 49 4e 43 54 5f 46 NULL_DISTINCT_F
541a0 4f 52 5f 55 4e 49 51 55 45 20 31 0a 0a 2f 2a 0a OR_UNIQUE 1../*.
541b0 2a 2a 20 54 68 65 20 22 66 69 6c 65 20 66 6f 72 ** The "file for
541c0 6d 61 74 22 20 6e 75 6d 62 65 72 20 69 73 20 61 mat" number is a
541d0 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 n integer that i
541e0 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 s incremented wh
541f0 65 6e 65 76 65 72 0a 2a 2a 20 74 68 65 20 56 44 enever.** the VD
54200 42 45 2d 6c 65 76 65 6c 20 66 69 6c 65 20 66 6f BE-level file fo
54210 72 6d 61 74 20 63 68 61 6e 67 65 73 2e 20 20 54 rmat changes. T
54220 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 he following mac
54230 72 6f 73 20 64 65 66 69 6e 65 20 74 68 65 0a 2a ros define the.*
54240 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 66 69 * the default fi
54250 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 6e 65 le format for ne
54260 77 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 20 w databases and
54270 74 68 65 20 6d 61 78 69 6d 75 6d 20 66 69 6c 65 the maximum file
54280 20 66 6f 72 6d 61 74 0a 2a 2a 20 74 68 61 74 20 format.** that
54290 74 68 65 20 6c 69 62 72 61 72 79 20 63 61 6e 20 the library can
542a0 72 65 61 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 read..*/.#define
542b0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 SQLITE_MAX_FILE
542c0 5f 46 4f 52 4d 41 54 20 34 0a 23 69 66 6e 64 65 _FORMAT 4.#ifnde
542d0 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 f SQLITE_DEFAULT
542e0 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 0a 23 20 64 _FILE_FORMAT.# d
542f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 efine SQLITE_DEF
54300 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 AULT_FILE_FORMAT
54310 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 1.#endif../*.**
54320 20 50 72 6f 76 69 64 65 20 61 20 64 65 66 61 75 Provide a defau
54330 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 53 51 4c lt value for SQL
54340 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 69 ITE_TEMP_STORE i
54350 6e 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f 74 n case it is not
54360 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 6f 6e specified.** on
54370 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e the command-lin
54380 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c e.*/.#ifndef SQL
54390 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 23 ITE_TEMP_STORE.#
543a0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 define SQLITE_T
543b0 45 4d 50 5f 53 54 4f 52 45 20 31 0a 23 65 6e 64 EMP_STORE 1.#end
543c0 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43 20 64 6f if../*.** GCC do
543d0 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 74 68 es not define th
543e0 65 20 6f 66 66 73 65 74 6f 66 28 29 20 6d 61 63 e offsetof() mac
543f0 72 6f 20 73 6f 20 77 65 27 6c 6c 20 68 61 76 65 ro so we'll have
54400 20 74 6f 20 64 6f 20 69 74 0a 2a 2a 20 6f 75 72 to do it.** our
54410 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 selves..*/.#ifnd
54420 65 66 20 6f 66 66 73 65 74 6f 66 0a 23 64 65 66 ef offsetof.#def
54430 69 6e 65 20 6f 66 66 73 65 74 6f 66 28 53 54 52 ine offsetof(STR
54440 55 43 54 55 52 45 2c 46 49 45 4c 44 29 20 28 28 UCTURE,FIELD) ((
54450 69 6e 74 29 28 28 63 68 61 72 2a 29 26 28 28 53 int)((char*)&((S
54460 54 52 55 43 54 55 52 45 2a 29 30 29 2d 3e 46 49 TRUCTURE*)0)->FI
54470 45 4c 44 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a ELD)).#endif../*
54480 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 .** Check to see
54490 20 69 66 20 74 68 69 73 20 6d 61 63 68 69 6e 65 if this machine
544a0 20 75 73 65 73 20 45 42 43 44 49 43 2e 20 20 28 uses EBCDIC. (
544b0 59 65 73 2c 20 62 65 6c 69 65 76 65 20 69 74 20 Yes, believe it
544c0 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68 65 72 65 or.** not, there
544d0 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 63 68 69 are still machi
544e0 6e 65 73 20 6f 75 74 20 74 68 65 72 65 20 74 68 nes out there th
544f0 61 74 20 75 73 65 20 45 42 43 44 49 43 2e 29 0a at use EBCDIC.).
54500 2a 2f 0a 23 69 66 20 27 41 27 20 3d 3d 20 27 5c */.#if 'A' == '\
54510 33 30 31 27 0a 23 20 64 65 66 69 6e 65 20 53 51 301'.# define SQ
54520 4c 49 54 45 5f 45 42 43 44 49 43 20 31 0a 23 65 LITE_EBCDIC 1.#e
54530 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c lse.# define SQL
54540 49 54 45 5f 41 53 43 49 49 20 31 0a 23 65 6e 64 ITE_ASCII 1.#end
54550 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 67 65 if../*.** Intege
54560 72 73 20 6f 66 20 6b 6e 6f 77 6e 20 73 69 7a 65 rs of known size
54570 73 2e 20 20 54 68 65 73 65 20 74 79 70 65 64 65 s. These typede
54580 66 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 fs might change
54590 66 6f 72 20 61 72 63 68 69 74 65 63 74 75 72 65 for architecture
545a0 73 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 73 s.** where the s
545b0 69 7a 65 73 20 76 65 72 79 2e 20 20 50 72 65 70 izes very. Prep
545c0 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 rocessor macros
545d0 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 73 6f are available so
545e0 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 79 70 that the.** typ
545f0 65 73 20 63 61 6e 20 62 65 20 63 6f 6e 76 65 6e es can be conven
54600 69 65 6e 74 6c 79 20 72 65 64 65 66 69 6e 65 64 iently redefined
54610 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 79 70 65 at compile-type
54620 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a . Like this:.**
54630 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 63 20 27 .** cc '
54640 2d 44 55 49 4e 54 50 54 52 5f 54 59 50 45 3d 6c -DUINTPTR_TYPE=l
54650 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 27 20 2e 2e ong long int' ..
54660 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 55 49 4e ..*/.#ifndef UIN
54670 54 33 32 5f 54 59 50 45 0a 23 20 69 66 64 65 66 T32_TYPE.# ifdef
54680 20 48 41 56 45 5f 55 49 4e 54 33 32 5f 54 0a 23 HAVE_UINT32_T.#
54690 20 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f define UINT32_
546a0 54 59 50 45 20 75 69 6e 74 33 32 5f 74 0a 23 20 TYPE uint32_t.#
546b0 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 55 else.# define U
546c0 49 4e 54 33 32 5f 54 59 50 45 20 75 6e 73 69 67 INT32_TYPE unsig
546d0 6e 65 64 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a ned int.# endif.
546e0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 55 #endif.#ifndef U
546f0 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 INT16_TYPE.# ifd
54700 65 66 20 48 41 56 45 5f 55 49 4e 54 31 36 5f 54 ef HAVE_UINT16_T
54710 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 .# define UINT1
54720 36 5f 54 59 50 45 20 75 69 6e 74 31 36 5f 74 0a 6_TYPE uint16_t.
54730 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 # else.# define
54740 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75 6e 73 UINT16_TYPE uns
54750 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 0a igned short int.
54760 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 # endif.#endif.#
54770 69 66 6e 64 65 66 20 49 4e 54 31 36 5f 54 59 50 ifndef INT16_TYP
54780 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 E.# ifdef HAVE_I
54790 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 NT16_T.# define
547a0 20 49 4e 54 31 36 5f 54 59 50 45 20 69 6e 74 31 INT16_TYPE int1
547b0 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 6_t.# else.# de
547c0 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 fine INT16_TYPE
547d0 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 short int.# endi
547e0 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 f.#endif.#ifndef
547f0 20 55 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 UINT8_TYPE.# if
54800 64 65 66 20 48 41 56 45 5f 55 49 4e 54 38 5f 54 def HAVE_UINT8_T
54810 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 38 .# define UINT8
54820 5f 54 59 50 45 20 75 69 6e 74 38 5f 74 0a 23 20 _TYPE uint8_t.#
54830 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 55 else.# define U
54840 49 4e 54 38 5f 54 59 50 45 20 75 6e 73 69 67 6e INT8_TYPE unsign
54850 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a ed char.# endif.
54860 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49 #endif.#ifndef I
54870 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 64 65 66 NT8_TYPE.# ifdef
54880 20 48 41 56 45 5f 49 4e 54 38 5f 54 0a 23 20 20 HAVE_INT8_T.#
54890 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 define INT8_TYPE
548a0 20 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 int8_t.# else.#
548b0 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 define INT8_TY
548c0 50 45 20 73 69 67 6e 65 64 20 63 68 61 72 0a 23 PE signed char.#
548d0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 endif.#endif.#i
548e0 66 6e 64 65 66 20 4c 4f 4e 47 44 4f 55 42 4c 45 fndef LONGDOUBLE
548f0 5f 54 59 50 45 0a 23 20 64 65 66 69 6e 65 20 4c _TYPE.# define L
54900 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 6c ONGDOUBLE_TYPE l
54910 6f 6e 67 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 ong double.#endi
54920 66 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 f.typedef sqlite
54930 5f 69 6e 74 36 34 20 69 36 34 3b 20 20 20 20 20 _int64 i64;
54940 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 /* 8-byte s
54950 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f igned integer */
54960 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f .typedef sqlite_
54970 75 69 6e 74 36 34 20 75 36 34 3b 20 20 20 20 20 uint64 u64;
54980 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 75 6e /* 8-byte un
54990 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
549a0 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 33 32 /.typedef UINT32
549b0 5f 54 59 50 45 20 75 33 32 3b 20 20 20 20 20 20 _TYPE u32;
549c0 20 20 20 20 20 2f 2a 20 34 2d 62 79 74 65 20 75 /* 4-byte u
549d0 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 nsigned integer
549e0 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 31 */.typedef UINT1
549f0 36 5f 54 59 50 45 20 75 31 36 3b 20 20 20 20 20 6_TYPE u16;
54a00 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 74 65 20 /* 2-byte
54a10 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
54a20 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e 54 31 */.typedef INT1
54a30 36 5f 54 59 50 45 20 69 31 36 3b 20 20 20 20 20 6_TYPE i16;
54a40 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 74 65 /* 2-byte
54a50 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
54a60 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 38 */.typedef UINT8
54a70 5f 54 59 50 45 20 75 38 3b 20 20 20 20 20 20 20 _TYPE u8;
54a80 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 74 65 20 /* 1-byte
54a90 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
54aa0 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e 54 38 */.typedef INT8
54ab0 5f 54 59 50 45 20 69 38 3b 20 20 20 20 20 20 20 _TYPE i8;
54ac0 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 74 65 /* 1-byte
54ad0 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
54ae0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 */../*.** Macros
54af0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 to determine wh
54b00 65 74 68 65 72 20 74 68 65 20 6d 61 63 68 69 6e ether the machin
54b10 65 20 69 73 20 62 69 67 20 6f 72 20 6c 69 74 74 e is big or litt
54b20 6c 65 20 65 6e 64 69 61 6e 2c 0a 2a 2a 20 65 76 le endian,.** ev
54b30 61 6c 75 61 74 65 64 20 61 74 20 72 75 6e 74 69 aluated at runti
54b40 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 me..*/.#ifdef SQ
54b50 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f LITE_AMALGAMATIO
54b60 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 N.SQLITE_PRIVATE
54b70 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 const int sqlit
54b80 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6c 73 65 e3one = 1;.#else
54b90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54ba0 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 const int sqlite
54bb0 33 6f 6e 65 3b 0a 23 65 6e 64 69 66 0a 23 69 66 3one;.#endif.#if
54bc0 20 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c defined(i386) |
54bd0 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 | defined(__i386
54be0 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f __) || defined(_
54bf0 4d 5f 49 58 38 36 29 5c 0a 20 20 20 20 20 20 20 M_IX86)\.
54c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54c10 20 20 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64 || defined
54c20 28 5f 5f 78 38 36 5f 36 34 29 20 7c 7c 20 64 65 (__x86_64) || de
54c30 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f fined(__x86_64__
54c40 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 ).# define SQLIT
54c50 45 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 30 E_BIGENDIAN 0
54c60 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
54c70 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 31 0a _LITTLEENDIAN 1.
54c80 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
54c90 55 54 46 31 36 4e 41 54 49 56 45 20 20 53 51 4c UTF16NATIVE SQL
54ca0 49 54 45 5f 55 54 46 31 36 4c 45 0a 23 65 6c 73 ITE_UTF16LE.#els
54cb0 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 e.# define SQLIT
54cc0 45 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 28 E_BIGENDIAN (
54cd0 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 *(char *)(&sqlit
54ce0 65 33 6f 6e 65 29 3d 3d 30 29 0a 23 20 64 65 66 e3one)==0).# def
54cf0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c ine SQLITE_LITTL
54d00 45 45 4e 44 49 41 4e 20 28 2a 28 63 68 61 72 20 EENDIAN (*(char
54d10 2a 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d *)(&sqlite3one)=
54d20 3d 31 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c =1).# define SQL
54d30 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 ITE_UTF16NATIVE
54d40 28 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 (SQLITE_BIGENDIA
54d50 4e 3f 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 N?SQLITE_UTF16BE
54d60 3a 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 :SQLITE_UTF16LE)
54d70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
54d80 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 onstants for the
54d90 20 6c 61 72 67 65 73 74 20 61 6e 64 20 73 6d 61 largest and sma
54da0 6c 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 36 llest possible 6
54db0 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 4-bit signed int
54dc0 65 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 egers..** These
54dd0 6d 61 63 72 6f 73 20 61 72 65 20 64 65 73 69 67 macros are desig
54de0 6e 65 64 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 ned to work corr
54df0 65 63 74 6c 79 20 6f 6e 20 62 6f 74 68 20 33 32 ectly on both 32
54e00 2d 62 69 74 20 61 6e 64 20 36 34 2d 62 69 74 0a -bit and 64-bit.
54e10 2a 2a 20 63 6f 6d 70 69 6c 65 72 73 2e 0a 2a 2f ** compilers..*/
54e20 0a 23 64 65 66 69 6e 65 20 4c 41 52 47 45 53 54 .#define LARGEST
54e30 5f 49 4e 54 36 34 20 20 28 30 78 66 66 66 66 66 _INT64 (0xfffff
54e40 66 66 66 7c 28 28 28 69 36 34 29 30 78 37 66 66 fff|(((i64)0x7ff
54e50 66 66 66 66 66 29 3c 3c 33 32 29 29 0a 23 64 65 fffff)<<32)).#de
54e60 66 69 6e 65 20 53 4d 41 4c 4c 45 53 54 5f 49 4e fine SMALLEST_IN
54e70 54 36 34 20 28 28 28 69 36 34 29 2d 31 29 20 2d T64 (((i64)-1) -
54e80 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 0a LARGEST_INT64).
54e90 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
54ea0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
54eb0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 ing structure is
54ec0 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 used to store t
54ed0 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a he busy-handler.
54ee0 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 ** callback for
54ef0 61 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68 a given sqlite h
54f00 61 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 andle. .**.** Th
54f10 65 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e e sqlite.busyHan
54f20 64 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 dler member of t
54f30 68 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74 he sqlite struct
54f40 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75 contains the bu
54f50 73 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 sy.** callback f
54f60 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 or the database
54f70 68 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67 handle. Each pag
54f80 65 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 er opened via th
54f90 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 e sqlite.** hand
54fa0 6c 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70 le is passed a p
54fb0 6f 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ointer to sqlite
54fc0 2e 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 .busyHandler. Th
54fd0 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a e busy-handler.*
54fe0 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 * callback is cu
54ff0 72 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 rrently invoked
55000 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e only from within
55010 20 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 pager.c..*/.typ
55020 65 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79 edef struct Busy
55030 48 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64 Handler BusyHand
55040 6c 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79 ler;.struct Busy
55050 48 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 Handler {. int
55060 28 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c (*xFunc)(void *,
55070 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 int); /* The bu
55080 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 sy callback */.
55090 20 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20 void *pArg;
550a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
550b0 69 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79 irst arg to busy
550c0 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 callback */. i
550d0 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 nt nBusy;
550e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 /* Inc
550f0 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 remented with ea
55100 63 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a ch busy call */.
55110 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f };../*.** Name o
55120 66 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 f the master dat
55130 61 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68 abase table. Th
55140 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 e master databas
55150 65 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 e table.** is a
55160 73 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68 special table th
55170 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d at holds the nam
55180 65 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65 es and attribute
55190 73 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72 s of all.** user
551a0 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 tables and indi
551b0 63 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ces..*/.#define
551c0 4d 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20 MASTER_NAME
551d0 20 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 "sqlite_master
551e0 22 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d ".#define TEMP_M
551f0 41 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c ASTER_NAME "sql
55200 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 ite_temp_master"
55210 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 ../*.** The root
55220 2d 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73 -page of the mas
55230 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 ter database tab
55240 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d le..*/.#define M
55250 41 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20 ASTER_ROOT
55260 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 1../*.** The na
55270 6d 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 me of the schema
55280 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 table..*/.#defi
55290 6e 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 ne SCHEMA_TABLE(
552a0 78 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50 x) ((!OMIT_TEMP
552b0 44 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50 DB)&&(x==1)?TEMP
552c0 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53 _MASTER_NAME:MAS
552d0 54 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a TER_NAME)../*.**
552e0 20 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d A convenience m
552f0 61 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e acro that return
55300 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
55310 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61 elements in.** a
55320 6e 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66 n array..*/.#def
55330 69 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29 ine ArraySize(X)
55340 20 20 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f ((int)(sizeo
55350 66 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d f(X)/sizeof(X[0]
55360 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 )))../*.** The f
55370 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61 ollowing value a
55380 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d s a destructor m
55390 65 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69 eans to use sqli
553a0 74 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2a 20 te3DbFree()..**
553b0 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 This is an inter
553c0 6e 61 6c 20 65 78 74 65 6e 73 69 6f 6e 20 74 6f nal extension to
553d0 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 61 SQLITE_STATIC a
553e0 6e 64 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 nd SQLITE_TRANSI
553f0 45 4e 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ENT..*/.#define
55400 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 20 SQLITE_DYNAMIC
55410 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 ((sqlite3_destr
55420 75 63 74 6f 72 5f 74 79 70 65 29 73 71 6c 69 74 uctor_type)sqlit
55430 65 33 44 62 46 72 65 65 29 0a 0a 2f 2a 0a 2a 2a e3DbFree)../*.**
55440 20 57 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4d 49 When SQLITE_OMI
55450 54 5f 57 53 44 20 69 73 20 64 65 66 69 6e 65 64 T_WSD is defined
55460 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
55470 74 68 65 20 74 61 72 67 65 74 20 70 6c 61 74 66 the target platf
55480 6f 72 6d 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 orm does.** not
55490 73 75 70 70 6f 72 74 20 57 72 69 74 61 62 6c 65 support Writable
554a0 20 53 74 61 74 69 63 20 44 61 74 61 20 28 57 53 Static Data (WS
554b0 44 29 20 73 75 63 68 20 61 73 20 67 6c 6f 62 61 D) such as globa
554c0 6c 20 61 6e 64 20 73 74 61 74 69 63 20 76 61 72 l and static var
554d0 69 61 62 6c 65 73 2e 0a 2a 2a 20 41 6c 6c 20 76 iables..** All v
554e0 61 72 69 61 62 6c 65 73 20 6d 75 73 74 20 65 69 ariables must ei
554f0 74 68 65 72 20 62 65 20 6f 6e 20 74 68 65 20 73 ther be on the s
55500 74 61 63 6b 20 6f 72 20 64 79 6e 61 6d 69 63 61 tack or dynamica
55510 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 lly allocated fr
55520 6f 6d 0a 2a 2a 20 74 68 65 20 68 65 61 70 2e 20 om.** the heap.
55530 20 57 68 65 6e 20 57 53 44 20 69 73 20 75 6e 73 When WSD is uns
55540 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 76 61 upported, the va
55550 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 riable declarati
55560 6f 6e 73 20 73 63 61 74 74 65 72 65 64 0a 2a 2a ons scattered.**
55570 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20 throughout the
55580 53 51 4c 69 74 65 20 63 6f 64 65 20 6d 75 73 74 SQLite code must
55590 20 62 65 63 6f 6d 65 20 63 6f 6e 73 74 61 6e 74 become constant
555a0 73 20 69 6e 73 74 65 61 64 2e 20 20 54 68 65 20 s instead. The
555b0 53 51 4c 49 54 45 5f 57 53 44 0a 2a 2a 20 6d 61 SQLITE_WSD.** ma
555c0 63 72 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20 cro is used for
555d0 74 68 69 73 20 70 75 72 70 6f 73 65 2e 20 20 41 this purpose. A
555e0 6e 64 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 nd instead of re
555f0 66 65 72 65 6e 63 69 6e 67 20 74 68 65 20 76 61 ferencing the va
55600 72 69 61 62 6c 65 0a 2a 2a 20 64 69 72 65 63 74 riable.** direct
55610 6c 79 2c 20 77 65 20 75 73 65 20 69 74 73 20 63 ly, we use its c
55620 6f 6e 73 74 61 6e 74 20 61 73 20 61 20 6b 65 79 onstant as a key
55630 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 72 to lookup the r
55640 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 un-time allocate
55650 64 0a 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74 d.** buffer that
55660 20 68 6f 6c 64 73 20 72 65 61 6c 20 76 61 72 69 holds real vari
55670 61 62 6c 65 2e 20 20 54 68 65 20 63 6f 6e 73 74 able. The const
55680 61 6e 74 20 69 73 20 61 6c 73 6f 20 74 68 65 20 ant is also the
55690 69 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a 20 66 initializer.** f
556a0 6f 72 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 or the run-time
556b0 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 allocated buffer
556c0 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 75 ..**.** In the u
556d0 73 75 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 sual case where
556e0 57 53 44 20 69 73 20 73 75 70 70 6f 72 74 65 64 WSD is supported
556f0 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 57 53 44 , the SQLITE_WSD
55700 20 61 6e 64 20 47 4c 4f 42 41 4c 0a 2a 2a 20 6d and GLOBAL.** m
55710 61 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e 6f 2d acros become no-
55720 6f 70 73 20 61 6e 64 20 68 61 76 65 20 7a 65 72 ops and have zer
55730 6f 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d o performance im
55740 70 61 63 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 pact..*/.#ifdef
55750 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a SQLITE_OMIT_WSD.
55760 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 #define SQLITE
55770 5f 57 53 44 20 63 6f 6e 73 74 0a 20 20 23 64 65 _WSD const. #de
55780 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 fine GLOBAL(t,v)
55790 20 28 2a 28 74 2a 29 73 71 6c 69 74 65 33 5f 77 (*(t*)sqlite3_w
557a0 73 64 5f 66 69 6e 64 28 28 76 6f 69 64 2a 29 26 sd_find((void*)&
557b0 28 76 29 2c 20 73 69 7a 65 6f 66 28 76 29 29 29 (v), sizeof(v)))
557c0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
557d0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 47 e3GlobalConfig G
557e0 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 53 71 6c LOBAL(struct Sql
557f0 69 74 65 33 43 6f 6e 66 69 67 2c 20 73 71 6c 69 ite3Config, sqli
55800 74 65 33 43 6f 6e 66 69 67 29 0a 53 51 4c 49 54 te3Config).SQLIT
55810 45 5f 41 50 49 20 20 20 69 6e 74 20 73 71 6c 69 E_API int sqli
55820 74 65 33 5f 77 73 64 5f 69 6e 69 74 28 69 6e 74 te3_wsd_init(int
55830 20 4e 2c 20 69 6e 74 20 4a 29 3b 0a 53 51 4c 49 N, int J);.SQLI
55840 54 45 5f 41 50 49 20 20 20 76 6f 69 64 20 2a 73 TE_API void *s
55850 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 qlite3_wsd_find(
55860 76 6f 69 64 20 2a 4b 2c 20 69 6e 74 20 4c 29 3b void *K, int L);
55870 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
55880 20 53 51 4c 49 54 45 5f 57 53 44 20 0a 20 20 23 SQLITE_WSD . #
55890 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c define GLOBAL(t,
558a0 76 29 20 76 0a 20 20 23 64 65 66 69 6e 65 20 73 v) v. #define s
558b0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
558c0 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 ig sqlite3Config
558d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
558e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 he following mac
558f0 72 6f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 ros are used to
55900 73 75 70 70 72 65 73 73 20 63 6f 6d 70 69 6c 65 suppress compile
55910 72 20 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 74 r warnings and t
55920 6f 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63 6c 65 o.** make it cle
55930 61 72 20 74 6f 20 68 75 6d 61 6e 20 72 65 61 64 ar to human read
55940 65 72 73 20 77 68 65 6e 20 61 20 66 75 6e 63 74 ers when a funct
55950 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 ion parameter is
55960 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 0a 2a deliberately .*
55970 2a 20 6c 65 66 74 20 75 6e 75 73 65 64 20 77 69 * left unused wi
55980 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 thin the body of
55990 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69 a function. Thi
559a0 73 20 75 73 75 61 6c 6c 79 20 68 61 70 70 65 6e s usually happen
559b0 73 20 77 68 65 6e 0a 2a 2a 20 61 20 66 75 6e 63 s when.** a func
559c0 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 76 tion is called v
559d0 69 61 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f ia a function po
559e0 69 6e 74 65 72 2e 20 46 6f 72 20 65 78 61 6d 70 inter. For examp
559f0 6c 65 20 74 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 le the .** imple
55a00 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 mentation of an
55a10 53 51 4c 20 61 67 67 72 65 67 61 74 65 20 73 74 SQL aggregate st
55a20 65 70 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 ep callback may
55a30 6e 6f 74 20 75 73 65 20 74 68 65 0a 2a 2a 20 70 not use the.** p
55a40 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 arameter indicat
55a50 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ing the number o
55a60 66 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 f arguments pass
55a70 65 64 20 74 6f 20 74 68 65 20 61 67 67 72 65 67 ed to the aggreg
55a80 61 74 65 2c 0a 2a 2a 20 69 66 20 69 74 20 6b 6e ate,.** if it kn
55a90 6f 77 73 20 74 68 61 74 20 74 68 69 73 20 69 73 ows that this is
55aa0 20 65 6e 66 6f 72 63 65 64 20 65 6c 73 65 77 68 enforced elsewh
55ab0 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ere..**.** When
55ac0 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d a function param
55ad0 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 eter is not used
55ae0 20 61 74 20 61 6c 6c 20 77 69 74 68 69 6e 20 74 at all within t
55af0 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e he body of a fun
55b00 63 74 69 6f 6e 2c 0a 2a 2a 20 69 74 20 69 73 20 ction,.** it is
55b10 67 65 6e 65 72 61 6c 6c 79 20 6e 61 6d 65 64 20 generally named
55b20 22 4e 6f 74 55 73 65 64 22 20 6f 72 20 22 4e 6f "NotUsed" or "No
55b30 74 55 73 65 64 32 22 20 74 6f 20 6d 61 6b 65 20 tUsed2" to make
55b40 74 68 69 6e 67 73 20 65 76 65 6e 20 63 6c 65 61 things even clea
55b50 72 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c rer..** However,
55b60 20 74 68 65 73 65 20 6d 61 63 72 6f 73 20 6d 61 these macros ma
55b70 79 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74 y also be used t
55b80 6f 20 73 75 70 70 72 65 73 73 20 77 61 72 6e 69 o suppress warni
55b90 6e 67 73 20 72 65 6c 61 74 65 64 20 74 6f 0a 2a ngs related to.*
55ba0 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 * parameters tha
55bb0 74 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 t may or may not
55bc0 20 62 65 20 75 73 65 64 20 64 65 70 65 6e 64 69 be used dependi
55bd0 6e 67 20 6f 6e 20 63 6f 6d 70 69 6c 61 74 69 6f ng on compilatio
55be0 6e 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 20 46 6f n options..** Fo
55bf0 72 20 65 78 61 6d 70 6c 65 20 74 68 6f 73 65 20 r example those
55c00 70 61 72 61 6d 65 74 65 72 73 20 6f 6e 6c 79 20 parameters only
55c10 75 73 65 64 20 69 6e 20 61 73 73 65 72 74 28 29 used in assert()
55c20 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 6e 20 statements. In
55c30 74 68 65 73 65 0a 2a 2a 20 63 61 73 65 73 20 74 these.** cases t
55c40 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 he parameters ar
55c50 65 20 6e 61 6d 65 64 20 61 73 20 70 65 72 20 74 e named as per t
55c60 68 65 20 75 73 75 61 6c 20 63 6f 6e 76 65 6e 74 he usual convent
55c70 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ions..*/.#define
55c80 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
55c90 52 28 78 29 20 28 76 6f 69 64 29 28 78 29 0a 23 R(x) (void)(x).#
55ca0 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 define UNUSED_PA
55cb0 52 41 4d 45 54 45 52 32 28 78 2c 79 29 20 55 4e RAMETER2(x,y) UN
55cc0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 USED_PARAMETER(x
55cd0 29 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 ),UNUSED_PARAMET
55ce0 45 52 28 79 29 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 ER(y)../*.** For
55cf0 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 ward references
55d00 74 6f 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2f to structures.*/
55d10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
55d20 41 67 67 49 6e 66 6f 20 41 67 67 49 6e 66 6f 3b AggInfo AggInfo;
55d30 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
55d40 41 75 74 68 43 6f 6e 74 65 78 74 20 41 75 74 68 AuthContext Auth
55d50 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 Context;.typedef
55d60 20 73 74 72 75 63 74 20 42 69 74 76 65 63 20 42 struct Bitvec B
55d70 69 74 76 65 63 3b 0a 74 79 70 65 64 65 66 20 73 itvec;.typedef s
55d80 74 72 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77 truct RowSet Row
55d90 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 Set;.typedef str
55da0 75 63 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c uct CollSeq Coll
55db0 53 65 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72 Seq;.typedef str
55dc0 75 63 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d uct Column Colum
55dd0 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 n;.typedef struc
55de0 74 20 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66 t Db Db;.typedef
55df0 20 73 74 72 75 63 74 20 53 63 68 65 6d 61 20 53 struct Schema S
55e00 63 68 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 chema;.typedef s
55e10 74 72 75 63 74 20 45 78 70 72 20 45 78 70 72 3b truct Expr Expr;
55e20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
55e30 45 78 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73 ExprList ExprLis
55e40 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
55e50 74 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 t FKey FKey;.typ
55e60 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 edef struct Func
55e70 44 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 Def FuncDef;.typ
55e80 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 edef struct Func
55e90 44 65 66 48 61 73 68 20 46 75 6e 63 44 65 66 48 DefHash FuncDefH
55ea0 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ash;.typedef str
55eb0 75 63 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73 uct IdList IdLis
55ec0 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
55ed0 74 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 t Index Index;.t
55ee0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 ypedef struct Ke
55ef0 79 43 6c 61 73 73 20 4b 65 79 43 6c 61 73 73 3b yClass KeyClass;
55f00 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
55f10 4b 65 79 49 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b KeyInfo KeyInfo;
55f20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
55f30 4c 6f 6f 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 73 Lookaside Lookas
55f40 69 64 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ide;.typedef str
55f50 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f uct LookasideSlo
55f60 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 3b t LookasideSlot;
55f70 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
55f80 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 Module Module;.t
55f90 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4e 61 ypedef struct Na
55fa0 6d 65 43 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f meContext NameCo
55fb0 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 20 73 ntext;.typedef s
55fc0 74 72 75 63 74 20 50 61 72 73 65 20 50 61 72 73 truct Parse Pars
55fd0 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 e;.typedef struc
55fe0 74 20 53 61 76 65 70 6f 69 6e 74 20 53 61 76 65 t Savepoint Save
55ff0 70 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 point;.typedef s
56000 74 72 75 63 74 20 53 65 6c 65 63 74 20 53 65 6c truct Select Sel
56010 65 63 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ect;.typedef str
56020 75 63 74 20 53 72 63 4c 69 73 74 20 53 72 63 4c uct SrcList SrcL
56030 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ist;.typedef str
56040 75 63 74 20 53 74 72 41 63 63 75 6d 20 53 74 72 uct StrAccum Str
56050 41 63 63 75 6d 3b 0a 74 79 70 65 64 65 66 20 73 Accum;.typedef s
56060 74 72 75 63 74 20 54 61 62 6c 65 20 54 61 62 6c truct Table Tabl
56070 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 e;.typedef struc
56080 74 20 54 61 62 6c 65 4c 6f 63 6b 20 54 61 62 6c t TableLock Tabl
56090 65 4c 6f 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 eLock;.typedef s
560a0 74 72 75 63 74 20 54 6f 6b 65 6e 20 54 6f 6b 65 truct Token Toke
560b0 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 n;.typedef struc
560c0 74 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 54 t TriggerStack T
560d0 72 69 67 67 65 72 53 74 61 63 6b 3b 0a 74 79 70 riggerStack;.typ
560e0 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67 edef struct Trig
560f0 67 65 72 53 74 65 70 20 54 72 69 67 67 65 72 53 gerStep TriggerS
56100 74 65 70 3b 0a 74 79 70 65 64 65 66 20 73 74 72 tep;.typedef str
56110 75 63 74 20 54 72 69 67 67 65 72 20 54 72 69 67 uct Trigger Trig
56120 67 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ger;.typedef str
56130 75 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f uct UnpackedReco
56140 72 64 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 rd UnpackedRecor
56150 64 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 d;.typedef struc
56160 74 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65 72 3b t Walker Walker;
56170 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
56180 57 68 65 72 65 50 6c 61 6e 20 57 68 65 72 65 50 WherePlan WhereP
56190 6c 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 lan;.typedef str
561a0 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20 57 68 uct WhereInfo Wh
561b0 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 ereInfo;.typedef
561c0 20 73 74 72 75 63 74 20 57 68 65 72 65 4c 65 76 struct WhereLev
561d0 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b 0a 0a el WhereLevel;..
561e0 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f 75 72 /*.** Defer sour
561f0 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e 64 20 cing vdbe.h and
56200 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 61 66 btree.h until af
56210 74 65 72 20 74 68 65 20 22 75 38 22 20 61 6e 64 ter the "u8" and
56220 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 6c 65 .** "BusyHandle
56230 72 22 20 74 79 70 65 64 65 66 73 2e 20 76 64 62 r" typedefs. vdb
56240 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69 72 65 e.h also require
56250 73 20 61 20 66 65 77 20 6f 66 20 74 68 65 20 6f s a few of the o
56260 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 paque.** pointer
56270 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46 75 6e types (i.e. Fun
56280 63 44 65 66 29 20 64 65 66 69 6e 65 64 20 61 62 cDef) defined ab
56290 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ove..*/./*******
562a0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
562b0 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20 6d btree.h in the m
562c0 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 iddle of sqliteI
562d0 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
562e0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
562f0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
56300 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a le btree.h *****
56310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56330 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
56340 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 01 September 15.
56350 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
56360 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
56370 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
56380 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
56390 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
563a0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
563b0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
563c0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
563d0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
563e0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
563f0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
56400 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
56410 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
56420 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
56430 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
56440 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
56450 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
56460 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
56470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
564a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
564b0 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 * This header fi
564c0 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 le defines the i
564d0 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 nterface that th
564e0 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65 65 20 e sqlite B-Tree
564f0 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 file.** subsyste
56500 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 m. See comments
56510 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 63 in the source c
56520 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61 69 6c ode for a detail
56530 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e 0a 2a ed description.*
56540 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68 20 69 * of what each i
56550 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 nterface routine
56560 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 does..**.** @(#
56570 29 20 24 49 64 3a 20 62 74 72 65 65 2e 68 2c 76 ) $Id: btree.h,v
56580 20 31 2e 31 30 36 20 32 30 30 38 2f 31 32 2f 31 1.106 2008/12/1
56590 37 20 31 37 3a 33 30 3a 32 36 20 64 61 6e 69 65 7 17:30:26 danie
565a0 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
565b0 23 69 66 6e 64 65 66 20 5f 42 54 52 45 45 5f 48 #ifndef _BTREE_H
565c0 5f 0a 23 64 65 66 69 6e 65 20 5f 42 54 52 45 45 _.#define _BTREE
565d0 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68 _H_../* TODO: Th
565e0 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 73 is definition is
565f0 20 6a 75 73 74 20 69 6e 63 6c 75 64 65 64 20 73 just included s
56600 6f 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20 o other modules
56610 63 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a 2a 20 6e compile. It.** n
56620 65 65 64 73 20 74 6f 20 62 65 20 72 65 76 69 73 eeds to be revis
56630 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ited..*/.#define
56640 20 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f SQLITE_N_BTREE_
56650 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49 META 10../*.** I
56660 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f 6e f defined as non
56670 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 75 -zero, auto-vacu
56680 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 79 um is enabled by
56690 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 77 default. Otherw
566a0 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 62 ise.** it must b
566b0 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 20 e turned on for
566c0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75 73 each database us
566d0 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 6f ing "PRAGMA auto
566e0 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f _vacuum = 1"..*/
566f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
56700 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 DEFAULT_AUTOVACU
56710 55 4d 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c UM. #define SQL
56720 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f ITE_DEFAULT_AUTO
56730 56 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a VACUUM 0.#endif.
56740 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 .#define BTREE_A
56750 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 20 30 UTOVACUUM_NONE 0
56760 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f /* Do no
56770 74 20 64 6f 20 61 75 74 6f 2d 76 61 63 75 75 6d t do auto-vacuum
56780 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
56790 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c E_AUTOVACUUM_FUL
567a0 4c 20 31 20 20 20 20 20 20 20 20 2f 2a 20 44 6f L 1 /* Do
567b0 20 66 75 6c 6c 20 61 75 74 6f 2d 76 61 63 75 75 full auto-vacuu
567c0 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 m */.#define BTR
567d0 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e EE_AUTOVACUUM_IN
567e0 43 52 20 32 20 20 20 20 20 20 20 20 2f 2a 20 49 CR 2 /* I
567f0 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 ncremental vacuu
56800 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 m */../*.** Forw
56810 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 ard declarations
56820 20 6f 66 20 73 74 72 75 63 74 75 72 65 0a 2a 2f of structure.*/
56830 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
56840 42 74 72 65 65 20 42 74 72 65 65 3b 0a 74 79 70 Btree Btree;.typ
56850 65 64 65 66 20 73 74 72 75 63 74 20 42 74 43 75 edef struct BtCu
56860 72 73 6f 72 20 42 74 43 75 72 73 6f 72 3b 0a 74 rsor BtCursor;.t
56870 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74 ypedef struct Bt
56880 53 68 61 72 65 64 20 42 74 53 68 61 72 65 64 3b Shared BtShared;
56890 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
568a0 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 BtreeMutexArray
568b0 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3b BtreeMutexArray;
568c0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 ../*.** This str
568d0 75 63 74 75 72 65 20 72 65 63 6f 72 64 73 20 61 ucture records a
568e0 6c 6c 20 6f 66 20 74 68 65 20 42 74 72 65 65 73 ll of the Btrees
568f0 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 68 6f that need to ho
56900 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 78 20 62 65 ld.** a mutex be
56910 66 6f 72 65 20 77 65 20 65 6e 74 65 72 20 73 71 fore we enter sq
56920 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e lite3VdbeExec().
56930 20 20 54 68 65 20 42 74 72 65 65 73 20 61 72 65 The Btrees are
56940 0a 2a 2a 20 61 72 65 20 70 6c 61 63 65 64 20 69 .** are placed i
56950 6e 20 61 42 74 72 65 65 5b 5d 20 69 6e 20 6f 72 n aBtree[] in or
56960 64 65 72 20 6f 66 20 61 42 74 72 65 65 5b 5d 2d der of aBtree[]-
56970 3e 70 42 74 2e 20 20 54 68 61 74 20 77 61 79 2c >pBt. That way,
56980 0a 2a 2a 20 77 65 20 63 61 6e 20 61 6c 77 61 79 .** we can alway
56990 73 20 6c 6f 63 6b 20 61 6e 64 20 75 6e 6c 6f 63 s lock and unloc
569a0 6b 20 74 68 65 6d 20 61 6c 6c 20 71 75 69 63 6b k them all quick
569b0 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 ly..*/.struct Bt
569c0 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 7b 0a reeMutexArray {.
569d0 20 20 69 6e 74 20 6e 4d 75 74 65 78 3b 0a 20 20 int nMutex;.
569e0 42 74 72 65 65 20 2a 61 42 74 72 65 65 5b 53 51 Btree *aBtree[SQ
569f0 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 LITE_MAX_ATTACHE
56a00 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51 4c 49 54 D+1];.};...SQLIT
56a10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
56a20 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a lite3BtreeOpen(.
56a30 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
56a40 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 ilename, /* Na
56a50 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 me of database f
56a60 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 ile to open */.
56a70 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
56a80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 /* Ass
56a90 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 ociated database
56aa0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
56ab0 20 42 74 72 65 65 20 2a 2a 2c 20 20 20 20 20 20 Btree **,
56ac0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
56ad0 75 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a 20 urn open Btree*
56ae0 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c here */. int fl
56af0 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ags,
56b00 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 /* Flags */.
56b10 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 int vfsFlags
56b20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
56b30 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 gs passed throug
56b40 68 20 74 6f 20 56 46 53 20 6f 70 65 6e 20 2a 2f h to VFS open */
56b50 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 .);../* The flag
56b60 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 s parameter to s
56b70 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 20 qlite3BtreeOpen
56b80 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 can be the bitwi
56b90 73 65 20 6f 72 20 6f 66 20 74 68 65 0a 2a 2a 20 se or of the.**
56ba0 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 following values
56bb0 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 54 ..**.** NOTE: T
56bc0 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 74 hese values must
56bd0 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 65 match the corre
56be0 73 70 6f 6e 64 69 6e 67 20 50 41 47 45 52 5f 20 sponding PAGER_
56bf0 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 70 61 67 values in.** pag
56c00 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 er.h..*/.#define
56c10 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 BTREE_OMIT_JOUR
56c20 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f 20 6e 6f NAL 1 /* Do no
56c30 74 20 75 73 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 t use journal.
56c40 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 No argument */.#
56c50 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4e 4f 5f define BTREE_NO_
56c60 52 45 41 44 4c 4f 43 4b 20 20 20 32 20 20 2f 2a READLOCK 2 /*
56c70 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73 20 Omit readlocks
56c80 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65 on readonly file
56c90 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 s */.#define BTR
56ca0 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 20 EE_MEMORY
56cb0 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 4 /* In-memory
56cc0 20 44 42 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e DB. No argumen
56cd0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 t */.#define BTR
56ce0 45 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 EE_READONLY
56cf0 20 38 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 8 /* Open the
56d00 64 61 74 61 62 61 73 65 20 69 6e 20 72 65 61 64 database in read
56d10 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f 0a 23 64 -only mode */.#d
56d20 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 44 efine BTREE_READ
56d30 57 52 49 54 45 20 20 20 20 31 36 20 20 2f 2a 20 WRITE 16 /*
56d40 4f 70 65 6e 20 66 6f 72 20 62 6f 74 68 20 72 65 Open for both re
56d50 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e ading and writin
56d60 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 g */.#define BTR
56d70 45 45 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 EE_CREATE
56d80 33 32 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 32 /* Create th
56d90 65 20 64 61 74 61 62 61 73 65 20 69 66 20 69 74 e database if it
56da0 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 does not exist
56db0 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */..SQLITE_PRIVA
56dc0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
56dd0 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a 29 reeClose(Btree*)
56de0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
56df0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
56e00 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74 eSetCacheSize(Bt
56e10 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ree*,int);.SQLIT
56e20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
56e30 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 lite3BtreeSetSaf
56e40 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a 2c etyLevel(Btree*,
56e50 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
56e60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
56e70 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 ite3BtreeSyncDis
56e80 61 62 6c 65 64 28 42 74 72 65 65 2a 29 3b 0a 53 abled(Btree*);.S
56e90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
56ea0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
56eb0 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 2a tPageSize(Btree*
56ec0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ,int,int);.SQLIT
56ed0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
56ee0 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
56ef0 65 53 69 7a 65 28 42 74 72 65 65 2a 29 3b 0a 53 eSize(Btree*);.S
56f00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
56f10 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 t sqlite3BtreeMa
56f20 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 xPageCount(Btree
56f30 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
56f40 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
56f50 65 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76 e3BtreeGetReserv
56f60 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 e(Btree*);.SQLIT
56f70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
56f80 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74 lite3BtreeSetAut
56f90 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 2c oVacuum(Btree *,
56fa0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
56fb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
56fc0 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63 3BtreeGetAutoVac
56fd0 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 uum(Btree *);.SQ
56fe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
56ff0 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 sqlite3BtreeBeg
57000 69 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 2c 69 inTrans(Btree*,i
57010 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
57020 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
57030 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
57040 6e 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e 73 74 ne(Btree*, const
57050 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 3b char *zMaster);
57060 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
57070 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
57080 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42 CommitPhaseTwo(B
57090 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tree*);.SQLITE_P
570a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
570b0 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 e3BtreeCommit(Bt
570c0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
570d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
570e0 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 3BtreeRollback(B
570f0 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tree*);.SQLITE_P
57100 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
57110 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 e3BtreeBeginStmt
57120 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
57130 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
57140 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53 ite3BtreeCommitS
57150 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c tmt(Btree*);.SQL
57160 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
57170 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c sqlite3BtreeRoll
57180 62 61 63 6b 53 74 6d 74 28 42 74 72 65 65 2a 29 backStmt(Btree*)
57190 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
571a0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
571b0 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 eCreateTable(Btr
571c0 65 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66 ee*, int*, int f
571d0 6c 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 lags);.SQLITE_PR
571e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
571f0 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 3BtreeIsInTrans(
57200 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Btree*);.SQLITE_
57210 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
57220 74 65 33 42 74 72 65 65 49 73 49 6e 53 74 6d 74 te3BtreeIsInStmt
57230 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
57240 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
57250 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 ite3BtreeIsInRea
57260 64 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a dTrans(Btree*);.
57270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
57280 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 oid *sqlite3Btre
57290 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 2c eSchema(Btree *,
572a0 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f int, void(*)(vo
572b0 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 id *));.SQLITE_P
572c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
572d0 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 e3BtreeSchemaLoc
572e0 6b 65 64 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 ked(Btree *);.SQ
572f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
57300 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 sqlite3BtreeLoc
57310 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 2c 20 kTable(Btree *,
57320 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 int, u8);.SQLITE
57330 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
57340 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
57350 6e 74 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c nt(Btree *, int,
57360 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 int);..SQLITE_P
57370 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
57380 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 r *sqlite3BtreeG
57390 65 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 etFilename(Btree
573a0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
573b0 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
573c0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 44 sqlite3BtreeGetD
573d0 69 72 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b irname(Btree *);
573e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
573f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
57400 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e te3BtreeGetJourn
57410 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b alname(Btree *);
57420 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
57430 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
57440 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a CopyFile(Btree *
57450 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a 53 51 4c , Btree *);..SQL
57460 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
57470 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 sqlite3BtreeIncr
57480 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b Vacuum(Btree *);
57490 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70 ../* The flags p
574a0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
574b0 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 te3BtreeCreateTa
574c0 62 6c 65 20 63 61 6e 20 62 65 20 74 68 65 20 62 ble can be the b
574d0 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 6f 66 20 itwise OR.** of
574e0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c the following fl
574f0 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ags:.*/.#define
57500 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 20 20 20 BTREE_INTKEY
57510 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 1 /* Table h
57520 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20 73 as only 64-bit s
57530 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b 65 igned integer ke
57540 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 ys */.#define BT
57550 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 20 20 32 REE_ZERODATA 2
57560 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 /* Table has
57570 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 keys only - no
57580 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65 20 data */.#define
57590 42 54 52 45 45 5f 4c 45 41 46 44 41 54 41 20 20 BTREE_LEAFDATA
575a0 20 34 20 20 20 20 2f 2a 20 44 61 74 61 20 73 74 4 /* Data st
575b0 6f 72 65 64 20 69 6e 20 6c 65 61 76 65 73 20 6f ored in leaves o
575c0 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73 20 49 4e nly. Implies IN
575d0 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f TKEY */..SQLITE_
575e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
575f0 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c te3BtreeDropTabl
57600 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 e(Btree*, int, i
57610 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
57620 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
57630 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 BtreeClearTable(
57640 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Btree*, int, int
57650 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
57660 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
57670 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 reeGetMeta(Btree
57680 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 *, int idx, u32
57690 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 *pValue);.SQLITE
576a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
576b0 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d ite3BtreeUpdateM
576c0 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 eta(Btree*, int
576d0 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29 3b idx, u32 value);
576e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
576f0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
57700 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 eTripAllCursors(
57710 42 74 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 Btree*, int);..S
57720 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
57730 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
57740 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c 20 rsor(. Btree*,
57750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57760 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
57770 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e 67 BTree containing
57780 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a table to open *
57790 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 /. int iTable,
577a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
577b0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
577c0 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 20 2a x of root page *
577d0 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 /. int wrFlag,
577e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
577f0 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f /* 1 fo
57800 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20 66 6f r writing. 0 fo
57810 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 r read-only */.
57820 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 2a struct KeyInfo*
57830 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
57840 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 /* First a
57850 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 61 rgument to compa
57860 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 re function */.
57870 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 BtCursor *pCurs
57880 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 or
57890 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 /* Space t
578a0 6f 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 73 o write cursor s
578b0 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a 53 tructure */.);.S
578c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
578d0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
578e0 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b 0a rsorSize(void);.
578f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
57900 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
57910 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75 CloseCursor(BtCu
57920 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rsor*);.SQLITE_P
57930 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
57940 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20 e3BtreeMoveto(.
57950 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 63 6f BtCursor*,. co
57960 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 0a nst void *pKey,.
57970 20 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 69 6e i64 nKey,. in
57980 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70 t bias,. int *p
57990 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Res.);.SQLITE_PR
579a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
579b0 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 3BtreeMovetoUnpa
579c0 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f 72 cked(. BtCursor
579d0 2a 2c 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 *,. UnpackedRec
579e0 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a 20 20 69 ord *pUnKey,. i
579f0 36 34 20 69 6e 74 4b 65 79 2c 0a 20 20 69 6e 74 64 intKey,. int
57a00 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70 52 bias,. int *pR
57a10 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 es.);.SQLITE_PRI
57a20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
57a30 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f BtreeCursorHasMo
57a40 76 65 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 ved(BtCursor*, i
57a50 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
57a60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
57a70 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43 75 BtreeDelete(BtCu
57a80 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rsor*);.SQLITE_P
57a90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
57aa0 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 42 74 e3BtreeInsert(Bt
57ab0 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73 74 20 76 Cursor*, const v
57ac0 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e oid *pKey, i64 n
57ad0 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Key,.
57ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57af0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 const voi
57b00 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 d *pData, int nD
57b10 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ata,.
57b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57b30 20 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f int nZero
57b40 2c 20 69 6e 74 20 62 69 61 73 29 3b 0a 53 51 4c , int bias);.SQL
57b50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
57b60 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 sqlite3BtreeFirs
57b70 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 t(BtCursor*, int
57b80 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f *pRes);.SQLITE_
57b90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
57ba0 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 te3BtreeLast(BtC
57bb0 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 ursor*, int *pRe
57bc0 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 s);.SQLITE_PRIVA
57bd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
57be0 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72 reeNext(BtCursor
57bf0 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 *, int *pRes);.S
57c00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
57c10 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6f t sqlite3BtreeEo
57c20 66 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 f(BtCursor*);.SQ
57c30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
57c40 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61 sqlite3BtreeFla
57c50 67 73 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 gs(BtCursor*);.S
57c60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
57c70 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 t sqlite3BtreePr
57c80 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72 2a evious(BtCursor*
57c90 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 , int *pRes);.SQ
57ca0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
57cb0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
57cc0 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 Size(BtCursor*,
57cd0 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c i64 *pSize);.SQL
57ce0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
57cf0 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 sqlite3BtreeKey(
57d00 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f BtCursor*, u32 o
57d10 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 ffset, u32 amt,
57d20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 void*);.SQLITE_P
57d30 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a RIVATE sqlite3 *
57d40 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
57d50 6f 72 44 62 28 63 6f 6e 73 74 20 42 74 43 75 72 orDb(const BtCur
57d60 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sor*);.SQLITE_PR
57d70 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 IVATE const void
57d80 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 *sqlite3BtreeKe
57d90 79 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a yFetch(BtCursor*
57da0 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51 , int *pAmt);.SQ
57db0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
57dc0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
57dd0 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28 42 BtreeDataFetch(B
57de0 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 tCursor*, int *p
57df0 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Amt);.SQLITE_PRI
57e00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
57e10 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 42 74 BtreeDataSize(Bt
57e20 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 2a 70 53 Cursor*, u32 *pS
57e30 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ize);.SQLITE_PRI
57e40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
57e50 42 74 72 65 65 44 61 74 61 28 42 74 43 75 72 73 BtreeData(BtCurs
57e60 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c or*, u32 offset,
57e70 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 u32 amt, void*)
57e80 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
57e90 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 E char *sqlite3B
57ea0 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 treeIntegrityChe
57eb0 63 6b 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 2a ck(Btree*, int *
57ec0 61 52 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 aRoot, int nRoot
57ed0 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 , int, int*);.SQ
57ee0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 72 LITE_PRIVATE str
57ef0 75 63 74 20 50 61 67 65 72 20 2a 73 71 6c 69 74 uct Pager *sqlit
57f00 65 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72 e3BtreePager(Btr
57f10 65 65 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 ee*);..SQLITE_PR
57f20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
57f30 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74 3BtreePutData(Bt
57f40 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 Cursor*, u32 off
57f50 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f set, u32 amt, vo
57f60 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 id*);.SQLITE_PRI
57f70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
57f80 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 3BtreeCacheOverf
57f90 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29 3b low(BtCursor *);
57fa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
57fb0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
57fc0 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 eClearCursor(BtC
57fd0 75 72 73 6f 72 20 2a 29 3b 0a 0a 23 69 66 64 65 ursor *);..#ifde
57fe0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
57ff0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
58000 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
58010 73 6f 72 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 sorInfo(BtCursor
58020 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 *, int*, int);.S
58030 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
58040 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 id sqlite3BtreeC
58050 75 72 73 6f 72 4c 69 73 74 28 42 74 72 65 65 2a ursorList(Btree*
58060 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a );.#endif../*.**
58070 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 75 If we are not u
58080 73 69 6e 67 20 73 68 61 72 65 64 20 63 61 63 68 sing shared cach
58090 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 e, then there is
580a0 20 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 75 no need to.** u
580b0 73 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61 63 se mutexes to ac
580c0 63 65 73 73 20 74 68 65 20 42 74 53 68 61 72 65 cess the BtShare
580d0 64 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 53 d structures. S
580e0 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45 6e o make the.** En
580f0 74 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70 72 ter and Leave pr
58100 6f 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73 2e ocedures no-ops.
58110 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .*/.#if !defined
58120 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 (SQLITE_OMIT_SHA
58130 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 53 51 RED_CACHE) && SQ
58140 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a LITE_THREADSAFE.
58150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
58160 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
58170 65 65 45 6e 74 65 72 28 42 74 72 65 65 2a 29 3b eeEnter(Btree*);
58180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
58190 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 void sqlite3Bt
581a0 72 65 65 4c 65 61 76 65 28 42 74 72 65 65 2a 29 reeLeave(Btree*)
581b0 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ;.#ifndef NDEBUG
581c0 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 . /* This routi
581d0 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 ne is used insid
581e0 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 e assert() state
581f0 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 53 ments only. */.S
58200 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
58210 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
58220 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 HoldsMutex(Btree
58230 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 *);.#endif.SQLIT
58240 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
58250 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
58260 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f erCursor(BtCurso
58270 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
58280 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
58290 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 e3BtreeLeaveCurs
582a0 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 or(BtCursor*);.S
582b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
582c0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
582d0 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 eEnterAll(sqlite
582e0 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 3*);.SQLITE_PRIV
582f0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
58300 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 e3BtreeLeaveAll(
58310 73 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 sqlite3*);.#ifnd
58320 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 ef NDEBUG. /* T
58330 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
58340 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 sed inside asser
58350 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f t() statements o
58360 6e 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 nly. */.SQLITE_P
58370 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
58380 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c ite3BtreeHoldsAl
58390 6c 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65 33 lMutexes(sqlite3
583a0 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 *);.#endif.SQLIT
583b0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
583c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
583d0 65 78 41 72 72 61 79 45 6e 74 65 72 28 42 74 72 exArrayEnter(Btr
583e0 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a eeMutexArray*);.
583f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
58400 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
58410 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 eeMutexArrayLeav
58420 65 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e(BtreeMutexArra
58430 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 y*);.SQLITE_PRIV
58440 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
58450 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
58460 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 yInsert(BtreeMut
58470 65 78 41 72 72 61 79 2a 2c 20 42 74 72 65 65 2a exArray*, Btree*
58480 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
58490 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e e sqlite3BtreeEn
584a0 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 ter(X).# define
584b0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
584c0 65 28 58 29 0a 23 69 66 6e 64 65 66 20 4e 44 45 e(X).#ifndef NDE
584d0 42 55 47 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f BUG. /* This ro
584e0 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e utine is used in
584f0 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 side assert() st
58500 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a atements only. *
58510 2f 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 /.# define sqlit
58520 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
58530 78 28 58 29 20 31 0a 23 65 6e 64 69 66 0a 23 20 x(X) 1.#endif.#
58540 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
58550 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58 reeEnterCursor(X
58560 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
58570 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 e3BtreeLeaveCurs
58580 6f 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 or(X).# define s
58590 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
585a0 41 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 All(X).# define
585b0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
585c0 65 41 6c 6c 28 58 29 0a 23 69 66 6e 64 65 66 20 eAll(X).#ifndef
585d0 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 69 73 NDEBUG. /* This
585e0 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
585f0 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 inside assert()
58600 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 statements only
58610 2e 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 71 . */.# define sq
58620 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 lite3BtreeHoldsA
58630 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31 0a 23 llMutexes(X) 1.#
58640 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 73 endif.# define s
58650 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
58660 41 72 72 61 79 45 6e 74 65 72 28 58 29 0a 23 20 ArrayEnter(X).#
58670 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
58680 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 reeMutexArrayLea
58690 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 ve(X).# define s
586a0 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
586b0 41 72 72 61 79 49 6e 73 65 72 74 28 58 2c 59 29 ArrayInsert(X,Y)
586c0 0a 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66 .#endif...#endif
586d0 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f /* _BTREE_H_ */
586e0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
586f0 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68 * End of btree.h
58700 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
58710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58730 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
58740 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
58750 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
58760 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a n sqliteInt.h **
58770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58780 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
58790 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68 * Include vdbe.h
587a0 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
587b0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a f sqliteInt.h **
587c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
587d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
587e0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 * Begin file vdb
587f0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
58800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58820 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
58830 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 tember 15.**.**
58840 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
58850 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
58860 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
58870 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
58880 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
58890 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
588a0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
588b0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
588c0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
588d0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
588e0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
588f0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
58900 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
58910 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
58920 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
58930 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
58940 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
58950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58990 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64 ********.** Head
589a0 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 er file for the
589b0 56 69 72 74 75 61 6c 20 44 61 74 61 42 61 73 65 Virtual DataBase
589c0 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a Engine (VDBE).*
589d0 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 *.** This header
589e0 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 defines the int
589f0 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 69 erface to the vi
58a00 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 rtual database e
58a10 6e 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45 ngine.** or VDBE
58a20 2e 20 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c . The VDBE impl
58a30 65 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61 ements an abstra
58a40 63 74 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 ct machine that
58a50 72 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65 runs a.** simple
58a60 20 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65 program to acce
58a70 73 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 ss and modify th
58a80 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 e underlying dat
58a90 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 abase..**.** $Id
58aa0 3a 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 33 39 : vdbe.h,v 1.139
58ab0 20 32 30 30 38 2f 31 30 2f 33 31 20 31 30 3a 35 2008/10/31 10:5
58ac0 33 3a 32 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 3:23 danielk1977
58ad0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 Exp $.*/.#ifnde
58ae0 66 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 f _SQLITE_VDBE_H
58af0 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 _.#define _SQLIT
58b00 45 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a E_VDBE_H_../*.**
58b10 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45 20 69 A single VDBE i
58b20 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 s an opaque stru
58b30 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56 64 62 cture named "Vdb
58b40 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e e". Only routin
58b50 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 es.** in the sou
58b60 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74 65 56 rce file sqliteV
58b70 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 dbe.c are allowe
58b80 64 20 74 6f 20 73 65 65 20 74 68 65 20 69 6e 73 d to see the ins
58b90 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 ides.** of this
58ba0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 structure..*/.ty
58bb0 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
58bc0 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 e Vdbe;../*.** T
58bd0 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 he names of the
58be0 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 following types
58bf0 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65 declared in vdbe
58c00 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75 69 72 Int.h are requir
58c10 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 ed.** for the Vd
58c20 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e beOp definition.
58c30 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
58c40 63 74 20 56 64 62 65 46 75 6e 63 20 56 64 62 65 ct VdbeFunc Vdbe
58c50 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20 73 74 Func;.typedef st
58c60 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 0a 2f ruct Mem Mem;../
58c70 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e *.** A single in
58c80 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 struction of the
58c90 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
58ca0 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a has an opcode.*
58cb0 2a 20 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 * and as many as
58cc0 20 74 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e three operands.
58cd0 20 20 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f The instructio
58ce0 6e 20 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a n is recorded.**
58cf0 20 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 as an instance
58d00 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
58d10 20 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73 structure:.*/.s
58d20 74 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20 truct VdbeOp {.
58d30 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 u8 opcode;
58d40 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 /* What ope
58d50 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 ration to perfor
58d60 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 m */. signed ch
58d70 61 72 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e ar p4type; /* On
58d80 65 20 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20 e of the P4_xxx
58d90 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34 constants for p4
58da0 20 2a 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73 */. u8 opflags
58db0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 ; /* Not
58dc0 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 currently used
58dd0 2a 2f 0a 20 20 75 38 20 70 35 3b 20 20 20 20 20 */. u8 p5;
58de0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 66 74 /* Fift
58df0 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 h parameter is a
58e00 6e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 n unsigned chara
58e10 63 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31 cter */. int p1
58e20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
58e30 20 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a First operand *
58e40 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 /. int p2;
58e50 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e /* Secon
58e60 64 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 d parameter (oft
58e70 65 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 en the jump dest
58e80 69 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e ination) */. in
58e90 74 20 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 t p3;
58ea0 20 20 2f 2a 20 54 68 65 20 74 68 69 72 64 20 70 /* The third p
58eb0 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e arameter */. un
58ec0 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 ion {
58ed0 20 20 2f 2a 20 66 6f 72 74 68 20 70 61 72 61 6d /* forth param
58ee0 65 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 eter */. int
58ef0 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
58f00 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 /* Integer va
58f10 6c 75 65 20 69 66 20 70 34 74 79 70 65 3d 3d 50 lue if p4type==P
58f20 34 5f 49 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76 4_INT32 */. v
58f30 6f 69 64 20 2a 70 3b 20 20 20 20 20 20 20 20 20 oid *p;
58f40 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 69 63 /* Generic
58f50 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 pointer */.
58f60 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 char *z;
58f70 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
58f80 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 73 74 r to data for st
58f90 72 69 6e 67 20 28 63 68 61 72 20 61 72 72 61 79 ring (char array
58fa0 29 20 74 79 70 65 73 20 2a 2f 0a 20 20 20 20 69 ) types */. i
58fb0 36 34 20 2a 70 49 36 34 3b 20 20 20 20 20 20 20 64 *pI64;
58fc0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 /* Used wh
58fd0 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f en p4type is P4_
58fe0 49 4e 54 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75 INT64 */. dou
58ff0 62 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 ble *pReal;
59000 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
59010 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 52 45 p4type is P4_RE
59020 41 4c 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 AL */. FuncDe
59030 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 f *pFunc;
59040 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 /* Used when p4
59050 74 79 70 65 20 69 73 20 50 34 5f 46 55 4e 43 44 type is P4_FUNCD
59060 45 46 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 75 EF */. VdbeFu
59070 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 nc *pVdbeFunc;
59080 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 /* Used when p4
59090 74 79 70 65 20 69 73 20 50 34 5f 56 44 42 45 46 type is P4_VDBEF
590a0 55 4e 43 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 UNC */. CollS
590b0 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 eq *pColl;
590c0 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 /* Used when p
590d0 34 74 79 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c 4type is P4_COLL
590e0 53 45 51 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a SEQ */. Mem *
590f0 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 pMem;
59100 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 /* Used when p
59110 34 74 79 70 65 20 69 73 20 50 34 5f 4d 45 4d 20 4type is P4_MEM
59120 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 */. sqlite3_v
59130 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 2f 2a tab *pVtab; /*
59140 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 Used when p4typ
59150 65 20 69 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a e is P4_VTAB */.
59160 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 KeyInfo *pKe
59170 79 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 yInfo; /* Us
59180 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
59190 73 20 50 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a s P4_KEYINFO */.
591a0 20 20 20 20 69 6e 74 20 2a 61 69 3b 20 20 20 20 int *ai;
591b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
591c0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
591d0 73 20 50 34 5f 49 4e 54 41 52 52 41 59 20 2a 2f s P4_INTARRAY */
591e0 0a 20 20 7d 20 70 34 3b 0a 23 69 66 64 65 66 20 . } p4;.#ifdef
591f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 63 SQLITE_DEBUG. c
59200 68 61 72 20 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 har *zComment;
59210 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 /* Comme
59220 6e 74 20 74 6f 20 69 6d 70 72 6f 76 65 20 72 65 nt to improve re
59230 61 64 61 62 69 6c 69 74 79 20 2a 2f 0a 23 65 6e adability */.#en
59240 64 69 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f dif.#ifdef VDBE_
59250 50 52 4f 46 49 4c 45 0a 20 20 69 6e 74 20 63 6e PROFILE. int cn
59260 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
59270 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
59280 74 69 6d 65 73 20 74 68 69 73 20 69 6e 73 74 72 times this instr
59290 75 63 74 69 6f 6e 20 77 61 73 20 65 78 65 63 75 uction was execu
592a0 74 65 64 20 2a 2f 0a 20 20 75 36 34 20 63 79 63 ted */. u64 cyc
592b0 6c 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 les;
592c0 20 20 2f 2a 20 54 6f 74 61 6c 20 74 69 6d 65 20 /* Total time
592d0 73 70 65 6e 74 20 65 78 65 63 75 74 69 6e 67 20 spent executing
592e0 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
592f0 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 */.#endif.};.ty
59300 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
59310 65 4f 70 20 56 64 62 65 4f 70 3b 0a 0a 2f 2a 0a eOp VdbeOp;../*.
59320 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 ** A smaller ver
59330 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 sion of VdbeOp u
59340 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65 sed for the Vdbe
59350 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 AddOpList() func
59360 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 tion because.**
59370 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73 it takes up less
59380 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 space..*/.struc
59390 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20 t VdbeOpList {.
593a0 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 u8 opcode;
593b0 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 /* What ope
593c0 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 ration to perfor
593d0 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 m */. signed ch
593e0 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69 ar p1; /* Fi
593f0 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 rst operand */.
59400 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b signed char p2;
59410 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 /* Second p
59420 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 arameter (often
59430 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 the jump destina
59440 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65 tion) */. signe
59450 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f d char p3; /
59460 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65 * Third paramete
59470 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 r */.};.typedef
59480 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73 struct VdbeOpLis
59490 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f t VdbeOpList;../
594a0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c *.** Allowed val
594b0 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 33 ues of VdbeOp.p3
594c0 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 type.*/.#define
594d0 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20 P4_NOTUSED 0
594e0 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61 /* The P4 para
594f0 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 meter is not use
59500 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f d */.#define P4_
59510 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f DYNAMIC (-1) /
59520 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 * Pointer to a s
59530 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 tring obtained f
59540 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 rom sqliteMalloc
59550 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 () */.#define P4
59560 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20 _STATIC (-2)
59570 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 /* Pointer to a
59580 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f static string */
59590 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c .#define P4_COLL
595a0 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34 SEQ (-4) /* P4
595b0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
595c0 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 a CollSeq struc
595d0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
595e0 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29 P4_FUNCDEF (-5)
595f0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 /* P4 is a poi
59600 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65 nter to a FuncDe
59610 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 f structure */.#
59620 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 define P4_KEYINF
59630 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69 O (-6) /* P4 i
59640 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
59650 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
59660 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 re */.#define P4
59670 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20 _VDBEFUNC (-7)
59680 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 /* P4 is a point
59690 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63 er to a VdbeFunc
596a0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 structure */.#d
596b0 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20 efine P4_MEM
596c0 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73 (-8) /* P4 is
596d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
596e0 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72 Mem* structur
596f0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f e */.#define P4_
59700 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f TRANSIENT (-9) /
59710 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
59720 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 r to a transient
59730 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 string */.#defi
59740 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28 ne P4_VTAB (
59750 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20 -10) /* P4 is a
59760 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 pointer to an sq
59770 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 lite3_vtab struc
59780 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
59790 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31 P4_MPRINTF (-11
597a0 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72 ) /* P4 is a str
597b0 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ing obtained fro
597c0 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 m sqlite3_mprint
597d0 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 f() */.#define P
597e0 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29 4_REAL (-12)
597f0 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62 /* P4 is a 64-b
59800 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e it floating poin
59810 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 t value */.#defi
59820 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28 ne P4_INT64 (
59830 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20 -13) /* P4 is a
59840 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
59850 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 teger */.#define
59860 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31 P4_INT32 (-1
59870 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32 4) /* P4 is a 32
59880 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
59890 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ger */.#define P
598a0 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 29 4_INTARRAY (-15)
598b0 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 74 /* P4 is a vect
598c0 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e 74 or of 32-bit int
598d0 65 67 65 72 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 egers */../* Whe
598e0 6e 20 61 64 64 69 6e 67 20 61 20 50 34 20 61 72 n adding a P4 ar
598f0 67 75 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f gument using P4_
59900 4b 45 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 KEYINFO, a copy
59910 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 of the KeyInfo s
59920 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d tructure.** is m
59930 61 64 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20 ade. That copy
59940 69 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 is freed when th
59950 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 e Vdbe is finali
59960 7a 65 64 2e 20 20 42 75 74 20 69 66 20 74 68 65 zed. But if the
59970 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 .** argument is
59980 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f P4_KEYINFO_HANDO
59990 46 46 2c 20 74 68 65 20 70 61 73 73 65 64 20 69 FF, the passed i
599a0 6e 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 n pointer is use
599b0 64 2e 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 d. It still.**
599c0 67 65 74 73 20 66 72 65 65 64 20 77 68 65 6e 20 gets freed when
599d0 74 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 the Vdbe is fina
599e0 6c 69 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c lized so it stil
599f0 6c 20 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 l should be obta
59a00 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 ined.** from a s
59a10 69 6e 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c ingle sqliteMall
59a20 6f 63 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f oc(). But no co
59a30 70 79 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 py is made and t
59a40 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 he calling.** fu
59a50 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e nction should *n
59a60 6f 74 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20 ot* try to free
59a70 74 68 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a the KeyInfo..*/.
59a80 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e #define P4_KEYIN
59a90 46 4f 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 FO_HANDOFF (-16)
59aa0 0a 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 .#define P4_KEYI
59ab0 4e 46 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37 NFO_STATIC (-17
59ac0 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 )../*.** The Vdb
59ad0 65 2e 61 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 e.aColName array
59ae0 20 63 6f 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d contains 5n Mem
59af0 20 73 74 72 75 63 74 75 72 65 73 2c 20 77 68 65 structures, whe
59b00 72 65 20 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 re n is the .**
59b10 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
59b20 73 20 6f 66 20 64 61 74 61 20 72 65 74 75 72 6e s of data return
59b30 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d ed by the statem
59b40 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ent..*/.#define
59b50 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 COLNAME_NAME
59b60 20 30 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 0.#define COLNA
59b70 4d 45 5f 44 45 43 4c 54 59 50 45 20 31 0a 23 64 ME_DECLTYPE 1.#d
59b80 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 efine COLNAME_DA
59b90 54 41 42 41 53 45 20 32 0a 23 64 65 66 69 6e 65 TABASE 2.#define
59ba0 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 COLNAME_TABLE
59bb0 20 20 33 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 3.#define COLN
59bc0 41 4d 45 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 AME_COLUMN 4.#
59bd0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
59be0 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 BLE_COLUMN_METAD
59bf0 41 54 41 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c ATA.# define COL
59c00 4e 41 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20 NAME_N 5
59c10 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
59c20 66 20 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 f COLNAME_xxx sy
59c30 6d 62 6f 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 mbols */.#else.#
59c40 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifdef SQLITE_OM
59c50 49 54 5f 44 45 43 4c 54 59 50 45 0a 23 20 20 20 IT_DECLTYPE.#
59c60 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e define COLNAME_N
59c70 20 20 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20 1 /*
59c80 53 74 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e Store only the n
59c90 61 6d 65 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 ame */.# else.#
59ca0 20 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 define COLNAME
59cb0 5f 4e 20 20 20 20 20 20 32 20 20 20 20 20 20 2f _N 2 /
59cc0 2a 20 53 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 * Store the name
59cd0 20 61 6e 64 20 64 65 63 6c 74 79 70 65 20 2a 2f and decltype */
59ce0 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a .# endif.#endif.
59cf0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
59d00 77 69 6e 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 wing macro conve
59d10 72 74 73 20 61 20 72 65 6c 61 74 69 76 65 20 61 rts a relative a
59d20 64 64 72 65 73 73 20 69 6e 20 74 68 65 20 70 32 ddress in the p2
59d30 20 66 69 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 field.** of a V
59d40 64 62 65 4f 70 20 73 74 72 75 63 74 75 72 65 20 dbeOp structure
59d50 69 6e 74 6f 20 61 20 6e 65 67 61 74 69 76 65 20 into a negative
59d60 6e 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 0a number so that .
59d70 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 ** sqlite3VdbeAd
59d80 64 4f 70 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 dOpList() knows
59d90 74 68 61 74 20 74 68 65 20 61 64 64 72 65 73 73 that the address
59da0 20 69 73 20 72 65 6c 61 74 69 76 65 2e 20 20 43 is relative. C
59db0 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 alling.** the ma
59dc0 63 72 6f 20 61 67 61 69 6e 20 72 65 73 74 6f 72 cro again restor
59dd0 65 73 20 74 68 65 20 61 64 64 72 65 73 73 2e 0a es the address..
59de0 2a 2f 0a 23 64 65 66 69 6e 65 20 41 44 44 52 28 */.#define ADDR(
59df0 58 29 20 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a X) (-1-(X))../*
59e00 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 .** The makefile
59e10 20 73 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e scans the vdbe.
59e20 63 20 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e c source file an
59e30 64 20 63 72 65 61 74 65 73 20 74 68 65 20 22 6f d creates the "o
59e40 70 63 6f 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 pcodes.h".** hea
59e50 64 65 72 20 66 69 6c 65 20 74 68 61 74 20 64 65 der file that de
59e60 66 69 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66 fines a number f
59e70 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75 or each opcode u
59e80 73 65 64 20 62 79 20 74 68 65 20 56 44 42 45 2e sed by the VDBE.
59e90 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
59ea0 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f *** Include opco
59eb0 64 65 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 des.h in the mid
59ec0 64 6c 65 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a dle of vdbe.h **
59ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59ee0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
59ef0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f *** Begin file o
59f00 70 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a pcodes.h *******
59f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59f30 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 **/./* Automatic
59f40 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 ally generated.
59f50 20 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a Do not edit */.
59f60 2f 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 /* See the mkopc
59f70 6f 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 odeh.awk script
59f80 66 6f 72 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 for details */.#
59f90 64 65 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 define OP_VNext
59fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
59fc0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 .#define OP_Affi
59fd0 6e 69 74 79 20 20 20 20 20 20 20 20 20 20 20 20 nity
59fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59ff0 20 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 2.#define OP_Co
5a000 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 lumn
5a010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a020 20 20 20 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 3.#define OP_
5a030 53 65 74 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 SetCookie
5a040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a050 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 4.#define O
5a060 50 5f 53 65 65 6b 20 20 20 20 20 20 20 20 20 20 P_Seek
5a070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a080 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 5.#define
5a090 20 4f 50 5f 52 65 61 6c 20 20 20 20 20 20 20 20 OP_Real
5a0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a0b0 20 20 20 20 20 20 20 31 32 39 20 20 20 2f 2a 20 129 /*
5a0c0 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 same as TK_FLOAT
5a0d0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
5a0e0 50 5f 53 65 71 75 65 6e 63 65 20 20 20 20 20 20 P_Sequence
5a0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a100 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 6.#define
5a110 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 OP_Savepoint
5a120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a130 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 7.#defi
5a140 6e 65 20 4f 50 5f 47 65 20 20 20 20 20 20 20 20 ne OP_Ge
5a150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a160 20 20 20 20 20 20 20 20 20 20 37 35 20 20 20 2f 75 /
5a170 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 * same as TK_GE
5a180 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
5a190 20 4f 50 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 OP_RowKey
5a1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a1b0 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 8.#defi
5a1c0 6e 65 20 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 ne OP_SCopy
5a1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a1e0 20 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 9.#de
5a1f0 66 69 6e 65 20 4f 50 5f 45 71 20 20 20 20 20 20 fine OP_Eq
5a200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a210 20 20 20 20 20 20 20 20 20 20 20 20 37 31 20 20 71
5a220 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 /* same as TK_E
5a230 51 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 Q */.#defi
5a240 6e 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 ne OP_OpenWrite
5a250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a260 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 10.#de
5a270 66 69 6e 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 fine OP_NotNull
5a280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a290 20 20 20 20 20 20 20 20 20 20 20 20 36 39 20 20 69
5a2a0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e /* same as TK_N
5a2b0 4f 54 4e 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 OTNULL */.#defi
5a2c0 6e 65 20 4f 50 5f 49 66 20 20 20 20 20 20 20 20 ne OP_If
5a2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a2e0 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 11.#de
5a2f0 66 69 6e 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 fine OP_ToInt
5a300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a310 20 20 20 20 20 20 20 20 20 20 20 31 34 34 20 20 144
5a320 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
5a330 4f 5f 49 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 O_INT */.#defi
5a340 6e 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 ne OP_String8
5a350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a360 20 20 20 20 20 20 20 20 20 20 39 31 20 20 20 2f 91 /
5a370 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 * same as TK_STR
5a380 49 4e 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 ING */.#define
5a390 20 4f 50 5f 56 52 6f 77 69 64 20 20 20 20 20 20 OP_VRowid
5a3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a3b0 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 12.#defi
5a3c0 6e 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 20 20 20 ne OP_CollSeq
5a3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a3e0 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 13.#de
5a3f0 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 fine OP_OpenRead
5a400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a410 20 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 23 14.#
5a420 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 69 72 65 define OP_Expire
5a430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 15
5a450 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 75 74 6f .#define OP_Auto
5a460 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20 Commit
5a470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a480 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 74 16.#define OP_Gt
5a490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a4b0 20 20 37 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 72 /* same a
5a4c0 73 20 54 4b 5f 47 54 20 20 20 20 20 20 20 2a 2f s TK_GT */
5a4d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 67 65 .#define OP_Page
5a4e0 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 count
5a4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a500 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 17.#define OP_In
5a510 74 65 67 72 69 74 79 43 6b 20 20 20 20 20 20 20 tegrityCk
5a520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a530 20 20 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 18.#define OP_
5a540 53 6f 72 74 20 20 20 20 20 20 20 20 20 20 20 20 Sort
5a550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a560 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 20.#define O
5a570 50 5f 43 6f 70 79 20 20 20 20 20 20 20 20 20 20 P_Copy
5a580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a590 20 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 21.#define
5a5a0 20 4f 50 5f 54 72 61 63 65 20 20 20 20 20 20 20 OP_Trace
5a5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a5c0 20 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 22.#defi
5a5d0 6e 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 ne OP_Function
5a5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a5f0 20 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 23.#de
5a600 66 69 6e 65 20 4f 50 5f 49 66 4e 65 67 20 20 20 fine OP_IfNeg
5a610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a620 20 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 24.#
5a630 64 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20 20 20 define OP_And
5a640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 34 64
5a660 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
5a670 5f 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23 64 65 _AND */.#de
5a680 66 69 6e 65 20 4f 50 5f 53 75 62 74 72 61 63 74 fine OP_Subtract
5a690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a6a0 20 20 20 20 20 20 20 20 20 20 20 20 38 32 20 20 82
5a6b0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d /* same as TK_M
5a6c0 49 4e 55 53 20 20 20 20 2a 2f 0a 23 64 65 66 69 INUS */.#defi
5a6d0 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 20 20 20 ne OP_Noop
5a6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a6f0 20 20 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 25.#de
5a700 66 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 fine OP_Return
5a710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a720 20 20 20 20 20 20 20 20 20 20 20 20 32 36 0a 23 26.#
5a730 64 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e define OP_Remain
5a740 64 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 der
5a750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35 85
5a760 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
5a770 5f 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 _REM */.#de
5a780 66 69 6e 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 fine OP_NewRowid
5a790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a7a0 20 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 27.#
5a7b0 64 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 define OP_Multip
5a7c0 6c 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ly
5a7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33 83
5a7e0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
5a7f0 5f 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 _STAR */.#de
5a800 66 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 fine OP_Variable
5a810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a820 20 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23 28.#
5a830 64 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 define OP_String
5a840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 39 29
5a860 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c .#define OP_Real
5a870 41 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 Affinity
5a880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a890 33 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 30.#define OP_VR
5a8a0 65 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 ename
5a8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a8c0 20 20 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 31.#define OP_
5a8d0 50 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 ParseSchema
5a8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a8f0 20 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 32.#define O
5a900 50 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20 P_VOpen
5a910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a920 20 20 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 33.#define
5a930 20 4f 50 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 OP_Close
5a940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a950 20 20 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 34.#defi
5a960 6e 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 ne OP_CreateInde
5a970 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x
5a980 20 20 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 35.#de
5a990 66 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 fine OP_IsUnique
5a9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a9b0 20 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 23 36.#
5a9c0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 define OP_NotFou
5a9d0 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nd
5a9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 37
5a9f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36 .#define OP_Int6
5aa00 34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4
5aa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aa20 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 38.#define OP_Mu
5aa30 73 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20 stBeInt
5aa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aa50 20 20 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 39.#define OP_
5aa60 48 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 Halt
5aa70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aa80 20 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 40.#define O
5aa90 50 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 P_Rowid
5aaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aab0 20 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 41.#define
5aac0 20 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 OP_IdxLT
5aad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aae0 20 20 20 20 20 20 20 20 34 32 0a 23 64 65 66 69 42.#defi
5aaf0 6e 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 ne OP_AddImm
5ab00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ab10 20 20 20 20 20 20 20 20 20 20 34 33 0a 23 64 65 43.#de
5ab20 66 69 6e 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e fine OP_Statemen
5ab30 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
5ab40 20 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23 44.#
5ab50 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 define OP_RowDat
5ab60 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a
5ab70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 35 45
5ab80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d .#define OP_MemM
5ab90 61 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ax
5aba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5abb0 34 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 72 46.#define OP_Or
5abc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5abd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5abe0 20 20 36 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 63 /* same a
5abf0 73 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 2a 2f s TK_OR */
5ac00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45 .#define OP_NotE
5ac10 78 69 73 74 73 20 20 20 20 20 20 20 20 20 20 20 xists
5ac20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ac30 34 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 47.#define OP_Go
5ac40 73 75 62 20 20 20 20 20 20 20 20 20 20 20 20 20 sub
5ac50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ac60 20 20 34 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48.#define OP_
5ac70 44 69 76 69 64 65 20 20 20 20 20 20 20 20 20 20 Divide
5ac80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ac90 20 20 20 20 38 34 20 20 20 2f 2a 20 73 61 6d 65 84 /* same
5aca0 20 61 73 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 as TK_SLASH
5acb0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e */.#define OP_In
5acc0 74 65 67 65 72 20 20 20 20 20 20 20 20 20 20 20 teger
5acd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ace0 20 20 34 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49.#define OP_
5acf0 54 6f 4e 75 6d 65 72 69 63 20 20 20 20 20 20 20 ToNumeric
5ad00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ad10 20 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 143 /* same
5ad20 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 as TK_TO_NUMERI
5ad30 43 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 C*/.#define OP_P
5ad40 72 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20 rev
5ad50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ad60 20 20 20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 50.#define OP
5ad70 5f 52 6f 77 53 65 74 52 65 61 64 20 20 20 20 20 _RowSetRead
5ad80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ad90 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 51.#define
5ada0 4f 50 5f 43 6f 6e 63 61 74 20 20 20 20 20 20 20 OP_Concat
5adb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5adc0 20 20 20 20 20 20 20 38 36 20 20 20 2f 2a 20 73 86 /* s
5add0 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 ame as TK_CONCAT
5ade0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
5adf0 5f 52 6f 77 53 65 74 41 64 64 20 20 20 20 20 20 _RowSetAdd
5ae00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ae10 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 52.#define
5ae20 4f 50 5f 42 69 74 41 6e 64 20 20 20 20 20 20 20 OP_BitAnd
5ae30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ae40 20 20 20 20 20 20 20 37 37 20 20 20 2f 2a 20 73 77 /* s
5ae50 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 41 4e 44 ame as TK_BITAND
5ae60 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
5ae70 5f 56 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 20 _VColumn
5ae80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ae90 20 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 53.#define
5aea0 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 20 OP_CreateTable
5aeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aec0 20 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 54.#defin
5aed0 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 20 20 20 e OP_Last
5aee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aef0 20 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 55.#def
5af00 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 ine OP_SeekLe
5af10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af20 20 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 56.#d
5af30 65 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 efine OP_IsNull
5af40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af50 20 20 20 20 20 20 20 20 20 20 20 20 20 36 38 20 68
5af60 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
5af70 49 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 ISNULL */.#def
5af80 69 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 ine OP_IncrVacuu
5af90 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m
5afa0 20 20 20 20 20 20 20 20 20 20 20 35 37 0a 23 64 57.#d
5afb0 65 66 69 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 efine OP_IdxRowi
5afc0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
5afd0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 38 0a 58.
5afe0 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 #define OP_Shift
5aff0 52 69 67 68 74 20 20 20 20 20 20 20 20 20 20 20 Right
5b000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
5b010 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 0 /* same as T
5b020 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 K_RSHIFT */.#d
5b030 65 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f efine OP_ResetCo
5b040 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 unt
5b050 20 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 59.
5b060 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 74 65 #define OP_Conte
5b070 78 74 50 75 73 68 20 20 20 20 20 20 20 20 20 20 xtPush
5b080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
5b090 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59 69 65 0.#define OP_Yie
5b0a0 6c 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ld
5b0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b0c0 20 36 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 61.#define OP_D
5b0d0 72 6f 70 54 72 69 67 67 65 72 20 20 20 20 20 20 ropTrigger
5b0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b0f0 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 62.#define OP
5b100 5f 44 72 6f 70 49 6e 64 65 78 20 20 20 20 20 20 _DropIndex
5b110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b120 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 20 65.#define
5b130 4f 50 5f 49 64 78 47 45 20 20 20 20 20 20 20 20 OP_IdxGE
5b140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b150 20 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 6e 66.#defin
5b160 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 20 20 e OP_IdxDelete
5b170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b180 20 20 20 20 20 20 20 20 20 36 37 0a 23 64 65 66 67.#def
5b190 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20 20 20 ine OP_Vacuum
5b1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b1b0 20 20 20 20 20 20 20 20 20 20 20 37 36 0a 23 64 76.#d
5b1c0 65 66 69 6e 65 20 4f 50 5f 49 66 4e 6f 74 20 20 efine OP_IfNot
5b1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 37 0a 87.
5b1f0 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 #define OP_DropT
5b200 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 able
5b210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
5b220 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 8.#define OP_See
5b230 6b 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 kLt
5b240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b250 20 38 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 89.#define OP_M
5b260 61 6b 65 52 65 63 6f 72 64 20 20 20 20 20 20 20 akeRecord
5b270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b280 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f 50 92.#define OP
5b290 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 _ToBlob
5b2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b2b0 20 20 20 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 142 /* sam
5b2c0 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 e as TK_TO_BLOB
5b2d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 */.#define OP_R
5b2e0 65 73 75 6c 74 52 6f 77 20 20 20 20 20 20 20 20 esultRow
5b2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b300 20 20 20 39 33 0a 23 64 65 66 69 6e 65 20 4f 50 93.#define OP
5b310 5f 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 _Delete
5b320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b330 20 20 20 20 20 39 34 0a 23 64 65 66 69 6e 65 20 94.#define
5b340 4f 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20 20 OP_AggFinal
5b350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b360 20 20 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e 95.#defin
5b370 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 e OP_Compare
5b380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b390 20 20 20 20 20 20 20 20 20 39 36 0a 23 64 65 66 96.#def
5b3a0 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 ine OP_ShiftLeft
5b3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b3c0 20 20 20 20 20 20 20 20 20 20 20 37 39 20 20 20 79
5b3d0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 /* same as TK_LS
5b3e0 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e HIFT */.#defin
5b3f0 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 e OP_Goto
5b400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b410 20 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 66 97.#def
5b420 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b ine OP_TableLock
5b430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b440 20 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64 98.#d
5b450 65 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 efine OP_Clear
5b460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b470 20 20 20 20 20 20 20 20 20 20 20 20 20 39 39 0a 99.
5b480 23 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20 #define OP_Le
5b490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
5b4b0 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 3 /* same as T
5b4c0 4b 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 K_LE */.#d
5b4d0 65 66 69 6e 65 20 4f 50 5f 56 65 72 69 66 79 43 efine OP_VerifyC
5b4e0 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 ookie
5b4f0 20 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 100.
5b500 23 64 65 66 69 6e 65 20 4f 50 5f 41 67 67 53 74 #define OP_AggSt
5b510 65 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ep
5b520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
5b530 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 1.#define OP_ToT
5b540 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 ext
5b550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b560 31 34 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 141 /* same as
5b570 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a TK_TO_TEXT */.
5b580 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 #define OP_Not
5b590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
5b5b0 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 9 /* same as T
5b5c0 4b 5f 4e 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 K_NOT */.#d
5b5d0 65 66 69 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20 efine OP_ToReal
5b5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b5f0 20 20 20 20 20 20 20 20 20 20 20 20 31 34 35 20 145
5b600 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
5b610 54 4f 5f 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66 TO_REAL */.#def
5b620 69 6e 65 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c ine OP_SetNumCol
5b630 75 6d 6e 73 20 20 20 20 20 20 20 20 20 20 20 20 umns
5b640 20 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 102.#d
5b650 65 66 69 6e 65 20 4f 50 5f 54 72 61 6e 73 61 63 efine OP_Transac
5b660 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 tion
5b670 20 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 103.
5b680 23 64 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74 #define OP_VFilt
5b690 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 er
5b6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
5b6b0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20 4.#define OP_Ne
5b6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b6e0 20 37 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 70 /* same as
5b6f0 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a TK_NE */.
5b700 23 64 65 66 69 6e 65 20 4f 50 5f 56 44 65 73 74 #define OP_VDest
5b710 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20 roy
5b720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
5b730 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 5.#define OP_Con
5b740 74 65 78 74 50 6f 70 20 20 20 20 20 20 20 20 20 textPop
5b750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b760 31 30 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 106.#define OP_B
5b770 69 74 4f 72 20 20 20 20 20 20 20 20 20 20 20 20 itOr
5b780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b790 20 20 20 37 38 20 20 20 2f 2a 20 73 61 6d 65 20 78 /* same
5b7a0 61 73 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 2a as TK_BITOR *
5b7b0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 78 /.#define OP_Nex
5b7c0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
5b7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b7e0 31 30 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 107.#define OP_I
5b7f0 64 78 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 dxInsert
5b800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b810 20 20 31 30 38 0a 23 64 65 66 69 6e 65 20 4f 50 108.#define OP
5b820 5f 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 _Lt
5b830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b840 20 20 20 20 20 37 34 20 20 20 2f 2a 20 73 61 6d 74 /* sam
5b850 65 20 61 73 20 54 4b 5f 4c 54 20 20 20 20 20 20 e as TK_LT
5b860 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 */.#define OP_S
5b870 65 65 6b 47 65 20 20 20 20 20 20 20 20 20 20 20 eekGe
5b880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b890 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 109.#define OP
5b8a0 5f 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 _Insert
5b8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b8c0 20 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20 110.#define
5b8d0 4f 50 5f 44 65 73 74 72 6f 79 20 20 20 20 20 20 OP_Destroy
5b8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b8f0 20 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 111.#defin
5b900 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 e OP_ReadCookie
5b910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b920 20 20 20 20 20 20 20 20 31 31 32 0a 23 64 65 66 112.#def
5b930 69 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 ine OP_LoadAnaly
5b940 73 69 73 20 20 20 20 20 20 20 20 20 20 20 20 20 sis
5b950 20 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64 113.#d
5b960 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e efine OP_Explain
5b970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b980 20 20 20 20 20 20 20 20 20 20 20 20 31 31 34 0a 114.
5b990 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 #define OP_OpenP
5b9a0 73 65 75 64 6f 20 20 20 20 20 20 20 20 20 20 20 seudo
5b9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
5b9c0 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 5.#define OP_Ope
5b9d0 6e 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 nEphemeral
5b9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b9f0 31 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 116.#define OP_N
5ba00 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 ull
5ba10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ba20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 117.#define OP
5ba30 5f 4d 6f 76 65 20 20 20 20 20 20 20 20 20 20 20 _Move
5ba40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ba50 20 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 118.#define
5ba60 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 OP_Blob
5ba70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ba80 20 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 6e 119.#defin
5ba90 65 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20 20 e OP_Add
5baa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bab0 20 20 20 20 20 20 20 20 20 38 31 20 20 20 2f 2a 81 /*
5bac0 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 same as TK_PLUS
5bad0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
5bae0 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20 20 OP_Rewind
5baf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bb00 20 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 120.#defin
5bb10 65 20 4f 50 5f 53 65 65 6b 47 74 20 20 20 20 20 e OP_SeekGt
5bb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bb30 20 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 121.#def
5bb40 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20 20 20 ine OP_VBegin
5bb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bb60 20 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 122.#d
5bb70 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 efine OP_VUpdate
5bb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bb90 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33 0a 123.
5bba0 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 #define OP_IfZer
5bbb0 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o
5bbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
5bbd0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4.#define OP_Bit
5bbe0 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 Not
5bbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bc00 20 39 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 90 /* same as
5bc10 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a TK_BITNOT */.
5bc20 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65 61 #define OP_VCrea
5bc30 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 te
5bc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
5bc50 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75 5.#define OP_Fou
5bc60 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nd
5bc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bc80 31 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 126.#define OP_I
5bc90 66 50 6f 73 20 20 20 20 20 20 20 20 20 20 20 20 fPos
5bca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bcb0 20 20 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 127.#define OP
5bcc0 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20 20 20 _NullRow
5bcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bce0 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 128.#define
5bcf0 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 20 OP_Jump
5bd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bd10 20 20 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e 130.#defin
5bd20 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e e OP_Permutation
5bd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bd40 20 20 20 20 20 20 20 20 31 33 31 0a 0a 2f 2a 20 131../*
5bd50 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 The following op
5bd60 63 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65 20 code values are
5bd70 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 never used */.#d
5bd80 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 efine OP_NotUsed
5bd90 5f 31 33 32 20 20 20 20 20 20 20 20 20 20 20 20 _132
5bda0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 32 0a 132.
5bdb0 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 #define OP_NotUs
5bdc0 65 64 5f 31 33 33 20 20 20 20 20 20 20 20 20 20 ed_133
5bdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
5bde0 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 3.#define OP_Not
5bdf0 55 73 65 64 5f 31 33 34 20 20 20 20 20 20 20 20 Used_134
5be00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5be10 31 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 134.#define OP_N
5be20 6f 74 55 73 65 64 5f 31 33 35 20 20 20 20 20 20 otUsed_135
5be30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5be40 20 20 31 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 135.#define OP
5be50 5f 4e 6f 74 55 73 65 64 5f 31 33 36 20 20 20 20 _NotUsed_136
5be60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5be70 20 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 136.#define
5be80 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 OP_NotUsed_137
5be90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bea0 20 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 137.#defin
5beb0 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 e OP_NotUsed_138
5bec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bed0 20 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 138.#def
5bee0 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 ine OP_NotUsed_1
5bef0 33 39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39
5bf00 20 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 139.#d
5bf10 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 efine OP_NotUsed
5bf20 5f 31 34 30 20 20 20 20 20 20 20 20 20 20 20 20 _140
5bf30 20 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 140.
5bf40 0a 0a 2f 2a 20 50 72 6f 70 65 72 74 69 65 73 20 ../* Properties
5bf50 73 75 63 68 20 61 73 20 22 6f 75 74 32 22 20 6f such as "out2" o
5bf60 72 20 22 6a 75 6d 70 22 20 74 68 61 74 20 61 72 r "jump" that ar
5bf70 65 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a e specified in.*
5bf80 2a 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f * comments follo
5bf90 77 69 6e 67 20 74 68 65 20 22 63 61 73 65 22 20 wing the "case"
5bfa0 66 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 for each opcode
5bfb0 69 6e 20 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a in the vdbe.c.**
5bfc0 20 61 72 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 are encoded int
5bfd0 6f 20 62 69 74 76 65 63 74 6f 72 73 20 61 73 20 o bitvectors as
5bfe0 66 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 follows:.*/.#def
5bff0 69 6e 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 ine OPFLG_JUMP
5c000 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 31 0x0001
5c010 20 20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 /* jump: P2 h
5c020 6f 6c 64 73 20 6a 6d 70 20 74 61 72 67 65 74 20 olds jmp target
5c030 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 */.#define OPFLG
5c040 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 _OUT2_PRERELEASE
5c050 20 30 78 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 0x0002 /* out2
5c060 2d 70 72 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a -prerelease: */.
5c070 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e #define OPFLG_IN
5c080 31 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 1 0x
5c090 30 30 30 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 0004 /* in1:
5c0a0 50 31 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a P1 is an input *
5c0b0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f /.#define OPFLG_
5c0c0 49 4e 32 20 20 20 20 20 20 20 20 20 20 20 20 20 IN2
5c0d0 30 78 30 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 0x0008 /* in2:
5c0e0 20 20 50 32 20 69 73 20 61 6e 20 69 6e 70 75 74 P2 is an input
5c0f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c */.#define OPFL
5c100 47 5f 49 4e 33 20 20 20 20 20 20 20 20 20 20 20 G_IN3
5c110 20 20 30 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 0x0010 /* in3
5c120 3a 20 20 20 50 33 20 69 73 20 61 6e 20 69 6e 70 : P3 is an inp
5c130 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 ut */.#define OP
5c140 46 4c 47 5f 4f 55 54 33 20 20 20 20 20 20 20 20 FLG_OUT3
5c150 20 20 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 0x0020 /* o
5c160 75 74 33 3a 20 20 50 33 20 69 73 20 61 6e 20 6f ut3: P3 is an o
5c170 75 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 utput */.#define
5c180 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a OPFLG_INITIALIZ
5c190 45 52 20 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 ER {\./* 0 */
5c1a0 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 0x00, 0x01, 0x00
5c1b0 2c 20 30 78 30 30 2c 20 30 78 31 30 2c 20 30 78 , 0x00, 0x10, 0x
5c1c0 30 38 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 08, 0x02, 0x00,\
5c1d0 0a 2f 2a 20 20 20 38 20 2a 2f 20 30 78 30 30 2c ./* 8 */ 0x00,
5c1e0 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30 0x04, 0x00, 0x0
5c1f0 35 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 5, 0x02, 0x00, 0
5c200 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 x00, 0x00,\./*
5c210 31 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32 16 */ 0x00, 0x02
5c220 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 , 0x00, 0x04, 0x
5c230 30 31 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 01, 0x04, 0x00,
5c240 30 78 30 30 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 0x00,\./* 24 */
5c250 20 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 0x05, 0x00, 0x0
5c260 34 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 4, 0x02, 0x02, 0
5c270 78 30 32 2c 20 30 78 30 34 2c 20 30 78 30 30 2c x02, 0x04, 0x00,
5c280 5c 0a 2f 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 \./* 32 */ 0x00
5c290 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
5c2a0 30 32 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 02, 0x11, 0x11,
5c2b0 30 78 30 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 0x02, 0x05,\./*
5c2c0 20 34 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 40 */ 0x00, 0x0
5c2d0 32 2c 20 30 78 31 31 2c 20 30 78 30 34 2c 20 30 2, 0x11, 0x04, 0
5c2e0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 63 2c x00, 0x00, 0x0c,
5c2f0 20 30 78 31 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 0x11,\./* 48 *
5c300 2f 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 / 0x01, 0x02, 0x
5c310 30 31 2c 20 30 78 32 31 2c 20 30 78 30 38 2c 20 01, 0x21, 0x08,
5c320 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 31 0x00, 0x02, 0x01
5c330 2c 5c 0a 2f 2a 20 20 35 36 20 2a 2f 20 30 78 31 ,\./* 56 */ 0x1
5c340 31 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 1, 0x01, 0x02, 0
5c350 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 34 2c x00, 0x00, 0x04,
5c360 20 30 78 30 30 2c 20 30 78 32 63 2c 5c 0a 2f 2a 0x00, 0x2c,\./*
5c370 20 20 36 34 20 2a 2f 20 30 78 32 63 2c 20 30 78 64 */ 0x2c, 0x
5c380 30 30 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 20 00, 0x11, 0x00,
5c390 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 31 35 0x05, 0x05, 0x15
5c3a0 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20 20 37 32 20 , 0x15,\./* 72
5c3b0 2a 2f 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 */ 0x15, 0x15, 0
5c3c0 78 31 35 2c 20 30 78 31 35 2c 20 30 78 30 30 2c x15, 0x15, 0x00,
5c3d0 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 0x2c, 0x2c, 0x2
5c3e0 63 2c 5c 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 c,\./* 80 */ 0x
5c3f0 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 2c, 0x2c, 0x2c,
5c400 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 0x2c, 0x2c, 0x2c
5c410 2c 20 30 78 32 63 2c 20 30 78 30 35 2c 5c 0a 2f , 0x2c, 0x05,\./
5c420 2a 20 20 38 38 20 2a 2f 20 30 78 30 30 2c 20 30 * 88 */ 0x00, 0
5c430 78 31 31 2c 20 30 78 30 34 2c 20 30 78 30 32 2c x11, 0x04, 0x02,
5c440 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
5c450 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 0, 0x00,\./* 96
5c460 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 */ 0x00, 0x01,
5c470 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
5c480 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
5c490 30 30 2c 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 00,\./* 104 */ 0
5c4a0 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x01, 0x00, 0x00,
5c4b0 20 30 78 30 31 2c 20 30 78 30 38 2c 20 30 78 31 0x01, 0x08, 0x1
5c4c0 31 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 1, 0x00, 0x02,\.
5c4d0 2f 2a 20 31 31 32 20 2a 2f 20 30 78 30 32 2c 20 /* 112 */ 0x02,
5c4e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
5c4f0 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 , 0x00, 0x02, 0x
5c500 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 00, 0x02,\./* 12
5c510 30 20 2a 2f 20 30 78 30 31 2c 20 30 78 31 31 2c 0 */ 0x01, 0x11,
5c520 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
5c530 35 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 5, 0x00, 0x11, 0
5c540 78 30 35 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 x05,\./* 128 */
5c550 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 31 0x00, 0x02, 0x01
5c560 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
5c570 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 00, 0x00, 0x00,\
5c580 0a 2f 2a 20 31 33 36 20 2a 2f 20 30 78 30 30 2c ./* 136 */ 0x00,
5c590 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
5c5a0 30 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 0, 0x00, 0x04, 0
5c5b0 78 30 34 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 x04, 0x04,\./* 1
5c5c0 34 34 20 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 44 */ 0x04, 0x04
5c5d0 2c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ,}../***********
5c5e0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 *** End of opcod
5c5f0 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a es.h ***********
5c600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c620 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
5c630 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
5c640 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
5c650 20 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a in vdbe.h *****
5c660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c670 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f **/../*.** Proto
5c680 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 44 types for the VD
5c690 42 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53 BE interface. S
5c6a0 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 ee comments on t
5c6b0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
5c6c0 6e 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72 n.** for a descr
5c6d0 69 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 iption of what e
5c6e0 61 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75 ach of these rou
5c6f0 74 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 tines does..*/.S
5c700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
5c710 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 be *sqlite3VdbeC
5c720 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b reate(sqlite3*);
5c730 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5c740 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
5c750 64 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 ddOp0(Vdbe*,int)
5c760 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5c770 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
5c780 41 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 AddOp1(Vdbe*,int
5c790 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
5c7a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5c7b0 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 3VdbeAddOp2(Vdbe
5c7c0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a *,int,int,int);.
5c7d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5c7e0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
5c7f0 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 dOp3(Vdbe*,int,i
5c800 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c nt,int,int);.SQL
5c810 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
5c820 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
5c830 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 4(Vdbe*,int,int,
5c840 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 int,int,const ch
5c850 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 ar *zP4,int);.SQ
5c860 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5c870 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
5c880 70 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 pList(Vdbe*, int
5c890 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 nOp, VdbeOpList
5c8a0 20 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 const *aOp);.SQ
5c8b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
5c8c0 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 d sqlite3VdbeCha
5c8d0 6e 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 ngeP1(Vdbe*, int
5c8e0 20 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a addr, int P1);.
5c8f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5c900 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
5c910 68 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69 hangeP2(Vdbe*, i
5c920 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29 nt addr, int P2)
5c930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5c940 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
5c950 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c eChangeP3(Vdbe*,
5c960 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 int addr, int P
5c970 33 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 3);.SQLITE_PRIVA
5c980 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
5c990 64 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 dbeChangeP5(Vdbe
5c9a0 2a 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 *, u8 P5);.SQLIT
5c9b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
5c9c0 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
5c9d0 72 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 re(Vdbe*, int ad
5c9e0 64 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 dr);.SQLITE_PRIV
5c9f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5ca00 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 VdbeChangeToNoop
5ca10 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 (Vdbe*, int addr
5ca20 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 , int N);.SQLITE
5ca30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
5ca40 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
5ca50 34 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 4(Vdbe*, int add
5ca60 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a r, const char *z
5ca70 50 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 P4, int N);.SQLI
5ca80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
5ca90 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 sqlite3VdbeUsesB
5caa0 74 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 tree(Vdbe*, int)
5cab0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5cac0 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 VdbeOp *sqlite3
5cad0 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c VdbeGetOp(Vdbe*,
5cae0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
5caf0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5cb00 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 3VdbeMakeLabel(V
5cb10 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
5cb20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
5cb30 65 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 e3VdbeDelete(Vdb
5cb40 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
5cb50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5cb60 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64 VdbeMakeReady(Vd
5cb70 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c be*,int,int,int,
5cb80 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
5cb90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5cba0 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 VdbeFinalize(Vdb
5cbb0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
5cbc0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5cbd0 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c VdbeResolveLabel
5cbe0 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 (Vdbe*, int);.SQ
5cbf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5cc00 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 sqlite3VdbeCurr
5cc10 65 6e 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a entAddr(Vdbe*);.
5cc20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
5cc30 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
5cc40 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
5cc50 33 56 64 62 65 54 72 61 63 65 28 56 64 62 65 2a 3VdbeTrace(Vdbe*
5cc60 2c 46 49 4c 45 2a 29 3b 0a 23 65 6e 64 69 66 0a ,FILE*);.#endif.
5cc70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5cc80 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 oid sqlite3VdbeR
5cc90 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56 esetStepResult(V
5cca0 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
5ccb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5ccc0 33 56 64 62 65 52 65 73 65 74 28 56 64 62 65 2a 3VdbeReset(Vdbe*
5ccd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5cce0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
5ccf0 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 beSetNumCols(Vdb
5cd00 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f e*,int);.SQLITE_
5cd10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
5cd20 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d te3VdbeSetColNam
5cd30 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e e(Vdbe*, int, in
5cd40 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c t, const char *,
5cd50 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 void(*)(void*))
5cd60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5cd70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
5cd80 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64 eCountChanges(Vd
5cd90 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
5cda0 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 VATE sqlite3 *sq
5cdb0 6c 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65 lite3VdbeDb(Vdbe
5cdc0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
5cdd0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
5cde0 64 62 65 53 65 74 53 71 6c 28 56 64 62 65 2a 2c dbeSetSql(Vdbe*,
5cdf0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
5ce00 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 int n);.SQLITE_P
5ce10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
5ce20 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 te3VdbeSwap(Vdbe
5ce30 2a 2c 56 64 62 65 2a 29 3b 0a 0a 23 69 66 64 65 *,Vdbe*);..#ifde
5ce40 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
5ce50 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e MEMORY_MANAGEMEN
5ce60 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
5ce70 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
5ce80 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e ReleaseMemory(in
5ce90 74 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 t);.#endif.SQLIT
5cea0 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b E_PRIVATE Unpack
5ceb0 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 edRecord *sqlite
5cec0 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 3VdbeRecordUnpac
5ced0 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69 6e 74 2c 63 k(KeyInfo*,int,c
5cee0 6f 6e 73 74 20 76 6f 69 64 2a 2c 0a 20 20 20 20 onst void*,.
5cef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cf10 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f UnpackedReco
5cf20 72 64 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 rd*,int);.SQLITE
5cf30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
5cf40 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 lite3VdbeDeleteU
5cf50 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e npackedRecord(Un
5cf60 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a packedRecord*);.
5cf70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5cf80 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 nt sqlite3VdbeRe
5cf90 63 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74 2c cordCompare(int,
5cfa0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70 61 const void*,Unpa
5cfb0 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a 0a ckedRecord*);...
5cfc0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 #ifndef NDEBUG.S
5cfd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
5cfe0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
5cff0 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63 Comment(Vdbe*, c
5d000 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
5d010 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43 ;.# define VdbeC
5d020 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 omment(X) sqlit
5d030 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 0a e3VdbeComment X.
5d040 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
5d050 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
5d060 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62 eNoopComment(Vdb
5d070 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c e*, const char*,
5d080 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 ...);.# define
5d090 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 VdbeNoopComment(
5d0a0 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 4e X) sqlite3VdbeN
5d0b0 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 6c oopComment X.#el
5d0c0 73 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 se.# define Vdbe
5d0d0 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65 66 Comment(X).# def
5d0e0 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d ine VdbeNoopComm
5d0f0 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 ent(X).#endif..#
5d100 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a endif../********
5d110 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 ****** End of vd
5d120 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a be.h ***********
5d130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d150 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
5d160 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
5d170 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
5d180 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 off in sqliteInt
5d190 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
5d1a0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
5d1b0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 ****** Include p
5d1c0 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 ager.h in the mi
5d1d0 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e ddle of sqliteIn
5d1e0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
5d1f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
5d200 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
5d210 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a e pager.h ******
5d220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d240 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
5d250 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
5d260 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
5d270 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
5d280 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
5d290 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
5d2a0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
5d2b0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
5d2c0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
5d2d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
5d2e0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
5d2f0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
5d300 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
5d310 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
5d320 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
5d330 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
5d340 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
5d350 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
5d360 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
5d370 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
5d380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5d3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
5d3c0 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c This header fil
5d3d0 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e e defines the in
5d3e0 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65 terface that the
5d3f0 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 sqlite page cac
5d400 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e he.** subsystem.
5d410 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 The page cache
5d420 20 73 75 62 73 79 73 74 65 6d 20 72 65 61 64 73 subsystem reads
5d430 20 61 6e 64 20 77 72 69 74 65 73 20 61 20 66 69 and writes a fi
5d440 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74 20 le a page.** at
5d450 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76 69 a time and provi
5d460 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66 6f des a journal fo
5d470 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a r rollback..**.*
5d480 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65 * @(#) $Id: page
5d490 72 2e 68 2c 76 20 31 2e 39 33 20 32 30 30 39 2f r.h,v 1.93 2009/
5d4a0 30 31 2f 30 37 20 31 35 3a 31 38 3a 32 31 20 64 01/07 15:18:21 d
5d4b0 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
5d4c0 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 .*/..#ifndef _PA
5d4d0 47 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f GER_H_.#define _
5d4e0 50 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 PAGER_H_../*.**
5d4f0 49 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f If defined as no
5d500 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 n-zero, auto-vac
5d510 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 uum is enabled b
5d520 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 y default. Other
5d530 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 wise.** it must
5d540 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 be turned on for
5d550 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75 each database u
5d560 73 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 sing "PRAGMA aut
5d570 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a o_vacuum = 1"..*
5d580 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
5d590 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c _DEFAULT_JOURNAL
5d5a0 5f 53 49 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 _SIZE_LIMIT. #d
5d5b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 efine SQLITE_DEF
5d5c0 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a AULT_JOURNAL_SIZ
5d5d0 45 5f 4c 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 E_LIMIT -1.#endi
5d5e0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 f../*.** The typ
5d5f0 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 e used to repres
5d600 65 6e 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65 ent a page numbe
5d610 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 r. The first pa
5d620 67 65 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 ge in a file.**
5d630 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 is called page 1
5d640 2e 20 20 30 20 69 73 20 75 73 65 64 20 74 6f 20 . 0 is used to
5d650 72 65 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 represent "not a
5d660 20 70 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 page"..*/.typed
5d670 65 66 20 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a ef u32 Pgno;../*
5d680 0a 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69 .** Each open fi
5d690 6c 65 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79 le is managed by
5d6a0 20 61 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 a separate inst
5d6b0 61 6e 63 65 20 6f 66 20 74 68 65 20 22 50 61 67 ance of the "Pag
5d6c0 65 72 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a er" structure..*
5d6d0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
5d6e0 20 50 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f Pager Pager;../
5d6f0 2a 0a 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 *.** Handle type
5d700 20 66 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 for pages..*/.t
5d710 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 ypedef struct Pg
5d720 48 64 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a Hdr DbPage;../*.
5d730 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 ** Allowed value
5d740 73 20 66 6f 72 20 74 68 65 20 66 6c 61 67 73 20 s for the flags
5d750 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
5d760 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 29 2e ite3PagerOpen().
5d770 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 54 68 69 .**.** NOTE: Thi
5d780 73 20 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 s values must ma
5d790 74 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f tch the correspo
5d7a0 6e 64 69 6e 67 20 42 54 52 45 45 5f 20 76 61 6c nding BTREE_ val
5d7b0 75 65 73 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a ues in btree.h..
5d7c0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
5d7d0 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 30 _OMIT_JOURNAL 0
5d7e0 78 30 30 30 31 20 20 20 20 2f 2a 20 44 6f 20 6e x0001 /* Do n
5d7f0 6f 74 20 75 73 65 20 61 20 72 6f 6c 6c 62 61 63 ot use a rollbac
5d800 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 k journal */.#de
5d810 66 69 6e 65 20 50 41 47 45 52 5f 4e 4f 5f 52 45 fine PAGER_NO_RE
5d820 41 44 4c 4f 43 4b 20 20 20 30 78 30 30 30 32 20 ADLOCK 0x0002
5d830 20 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c /* Omit readl
5d840 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 ocks on readonly
5d850 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a files */../*.**
5d860 20 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f Valid values fo
5d870 72 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 r the second arg
5d880 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
5d890 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 PagerLockingMode
5d8a0 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ()..*/.#define P
5d8b0 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
5d8c0 5f 51 55 45 52 59 20 20 20 20 20 20 2d 31 0a 23 _QUERY -1.#
5d8d0 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 define PAGER_LOC
5d8e0 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 KINGMODE_NORMAL
5d8f0 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 0.#define P
5d900 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
5d910 5f 45 58 43 4c 55 53 49 56 45 20 20 20 31 0a 0a _EXCLUSIVE 1..
5d920 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 /*.** Valid valu
5d930 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e es for the secon
5d940 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 d argument to sq
5d950 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 lite3PagerJourna
5d960 6c 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 lMode()..*/.#def
5d970 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ine PAGER_JOURNA
5d980 4c 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 LMODE_QUERY
5d990 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 -1.#define PAGE
5d9a0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 R_JOURNALMODE_DE
5d9b0 4c 45 54 45 20 20 20 20 20 20 30 20 20 20 2f 2a LETE 0 /*
5d9c0 20 43 6f 6d 6d 69 74 20 62 79 20 64 65 6c 65 74 Commit by delet
5d9d0 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ing journal file
5d9e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 */.#define PAGE
5d9f0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 R_JOURNALMODE_PE
5da00 52 53 49 53 54 20 20 20 20 20 31 20 20 20 2f 2a RSIST 1 /*
5da10 20 43 6f 6d 6d 69 74 20 62 79 20 7a 65 72 6f 69 Commit by zeroi
5da20 6e 67 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 ng journal heade
5da30 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 r */.#define PAG
5da40 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
5da50 46 46 20 20 20 20 20 20 20 20 20 32 20 20 20 2f FF 2 /
5da60 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d 69 74 74 65 * Journal omitte
5da70 64 2e 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 d. */.#define P
5da80 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
5da90 5f 54 52 55 4e 43 41 54 45 20 20 20 20 33 20 20 _TRUNCATE 3
5daa0 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 74 72 /* Commit by tr
5dab0 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c uncating journal
5dac0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 */.#define PAGE
5dad0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
5dae0 4d 4f 52 59 20 20 20 20 20 20 34 20 20 20 2f 2a MORY 4 /*
5daf0 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e In-memory journ
5db00 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a al file */../*.*
5db10 2a 20 53 65 65 20 73 6f 75 72 63 65 20 63 6f 64 * See source cod
5db20 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 61 e comments for a
5db30 20 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 detailed descri
5db40 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 6c ption of the fol
5db50 6c 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e lowing.** routin
5db60 65 73 3a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es:.*/.SQLITE_PR
5db70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5db80 33 50 61 67 65 72 4f 70 65 6e 28 73 71 6c 69 74 3PagerOpen(sqlit
5db90 65 33 5f 76 66 73 20 2a 2c 20 50 61 67 65 72 20 e3_vfs *, Pager
5dba0 2a 2a 70 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 **ppPager, const
5dbb0 20 63 68 61 72 2a 2c 20 69 6e 74 2c 69 6e 74 2c char*, int,int,
5dbc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
5dbd0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
5dbe0 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 3PagerSetBusyhan
5dbf0 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 dler(Pager*, int
5dc00 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 (*)(void *), voi
5dc10 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 d *);.SQLITE_PRI
5dc20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
5dc30 33 50 61 67 65 72 53 65 74 52 65 69 6e 69 74 65 3PagerSetReinite
5dc40 72 28 50 61 67 65 72 2a 2c 20 76 6f 69 64 28 2a r(Pager*, void(*
5dc50 29 28 44 62 50 61 67 65 2a 29 29 3b 0a 53 51 4c )(DbPage*));.SQL
5dc60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
5dc70 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 sqlite3PagerSetP
5dc80 61 67 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20 agesize(Pager*,
5dc90 75 31 36 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 u16*);.SQLITE_PR
5dca0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5dcb0 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 3PagerMaxPageCou
5dcc0 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b nt(Pager*, int);
5dcd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5dce0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
5dcf0 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50 ReadFileheader(P
5dd00 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e 73 69 ager*, int, unsi
5dd10 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 53 51 4c gned char*);.SQL
5dd20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
5dd30 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
5dd40 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a Cachesize(Pager*
5dd50 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
5dd60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5dd70 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 67 e3PagerClose(Pag
5dd80 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c er *pPager);.SQL
5dd90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
5dda0 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 sqlite3PagerAcqu
5ddb0 69 72 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ire(Pager *pPage
5ddc0 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 r, Pgno pgno, Db
5ddd0 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 Page **ppPage, i
5dde0 6e 74 20 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 nt clrFlag);.#de
5ddf0 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 67 65 fine sqlite3Page
5de00 72 47 65 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 rGet(A,B,C) sqli
5de10 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 te3PagerAcquire(
5de20 41 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f A,B,C,0).SQLITE_
5de30 50 52 49 56 41 54 45 20 44 62 50 61 67 65 20 2a PRIVATE DbPage *
5de40 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b sqlite3PagerLook
5de50 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 up(Pager *pPager
5de60 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 , Pgno pgno);.SQ
5de70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5de80 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
5de90 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 eRefcount(DbPage
5dea0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
5deb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
5dec0 67 65 72 52 65 66 28 44 62 50 61 67 65 2a 29 3b gerRef(DbPage*);
5ded0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5dee0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
5def0 55 6e 72 65 66 28 44 62 50 61 67 65 2a 29 3b 0a Unref(DbPage*);.
5df00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5df10 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 nt sqlite3PagerW
5df20 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 rite(DbPage*);.S
5df30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5df40 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 t sqlite3PagerPa
5df50 67 65 63 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 gecount(Pager*,
5df60 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
5df70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5df80 33 50 61 67 65 72 42 65 67 69 6e 28 44 62 50 61 3PagerBegin(DbPa
5df90 67 65 2a 2c 20 69 6e 74 20 65 78 46 6c 61 67 29 ge*, int exFlag)
5dfa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5dfb0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
5dfc0 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 rCommitPhaseOne(
5dfd0 50 61 67 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 Pager*,const cha
5dfe0 72 20 2a 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 r *zMaster, int)
5dff0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5e000 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
5e010 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 rCommitPhaseTwo(
5e020 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Pager*);.SQLITE_
5e030 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
5e040 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
5e050 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
5e060 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 _PRIVATE u8 sqli
5e070 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e te3PagerIsreadon
5e080 6c 79 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 ly(Pager*);.SQLI
5e090 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
5e0a0 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 sqlite3PagerDont
5e0b0 52 6f 6c 6c 62 61 63 6b 28 44 62 50 61 67 65 2a Rollback(DbPage*
5e0c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5e0d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
5e0e0 65 72 44 6f 6e 74 57 72 69 74 65 28 44 62 50 61 erDontWrite(DbPa
5e0f0 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ge*);.SQLITE_PRI
5e100 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5e110 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 PagerRefcount(Pa
5e120 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
5e130 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
5e140 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 e3PagerSetSafety
5e150 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e 74 Level(Pager*,int
5e160 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
5e170 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
5e180 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 *sqlite3PagerFi
5e190 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a lename(Pager*);.
5e1a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
5e1b0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 onst sqlite3_vfs
5e1c0 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 *sqlite3PagerVf
5e1d0 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 s(Pager*);.SQLIT
5e1e0 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 E_PRIVATE sqlite
5e1f0 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 33 50 3_file *sqlite3P
5e200 61 67 65 72 46 69 6c 65 28 50 61 67 65 72 2a 29 agerFile(Pager*)
5e210 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5e220 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
5e230 69 74 65 33 50 61 67 65 72 44 69 72 6e 61 6d 65 ite3PagerDirname
5e240 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
5e250 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
5e260 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 har *sqlite3Page
5e270 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67 rJournalname(Pag
5e280 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
5e290 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5e2a0 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 PagerNosync(Page
5e2b0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
5e2c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
5e2d0 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50 61 67 agerMovepage(Pag
5e2e0 65 72 2a 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f er*,DbPage*,Pgno
5e2f0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
5e300 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
5e310 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
5e320 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49 DbPage *); .SQLI
5e330 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
5e340 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 *sqlite3PagerGet
5e350 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 3b Extra(DbPage *);
5e360 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 .SQLITE_PRIVATE
5e370 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
5e380 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 rLockingMode(Pag
5e390 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 er *, int);.SQLI
5e3a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5e3b0 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
5e3c0 61 6c 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 alMode(Pager *,
5e3d0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
5e3e0 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 VATE i64 sqlite3
5e3f0 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 PagerJournalSize
5e400 4c 69 6d 69 74 28 50 61 67 65 72 20 2a 2c 20 69 Limit(Pager *, i
5e410 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 64);.SQLITE_PRIV
5e420 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
5e430 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 3PagerTempSpace(
5e440 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Pager*);.SQLITE_
5e450 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
5e460 74 65 33 50 61 67 65 72 53 79 6e 63 28 50 61 67 te3PagerSync(Pag
5e470 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 0a 53 51 er *pPager);..SQ
5e480 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5e490 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 sqlite3PagerOpe
5e4a0 6e 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 nSavepoint(Pager
5e4b0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 *pPager, int n)
5e4c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5e4d0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
5e4e0 72 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 rSavepoint(Pager
5e4f0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 *pPager, int op
5e500 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 , int iSavepoint
5e510 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
5e520 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
5e530 55 4d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UM.SQLITE_PRIVAT
5e540 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
5e550 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 PagerTruncateIma
5e560 67 65 28 50 61 67 65 72 2a 2c 50 67 6e 6f 29 3b ge(Pager*,Pgno);
5e570 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5e580 20 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 Pgno sqlite3Pa
5e590 67 65 72 49 6d 61 67 65 53 69 7a 65 28 50 61 67 gerImageSize(Pag
5e5a0 65 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 er *);.#endif..#
5e5b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 ifdef SQLITE_HAS
5e5c0 5f 43 4f 44 45 43 0a 53 51 4c 49 54 45 5f 50 52 _CODEC.SQLITE_PR
5e5d0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
5e5e0 69 74 65 33 50 61 67 65 72 53 65 74 43 6f 64 65 ite3PagerSetCode
5e5f0 63 28 50 61 67 65 72 2a 2c 76 6f 69 64 2a 28 2a c(Pager*,void*(*
5e600 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 )(void*,void*,Pg
5e610 6e 6f 2c 69 6e 74 29 2c 76 6f 69 64 2a 29 3b 0a no,int),void*);.
5e620 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 #endif..#if !def
5e630 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 ined(NDEBUG) ||
5e640 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 defined(SQLITE_T
5e650 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 EST).SQLITE_PRIV
5e660 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 74 ATE Pgno sqlit
5e670 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 e3PagerPagenumbe
5e680 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 r(DbPage*);.SQLI
5e690 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
5e6a0 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
5e6b0 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 2a riteable(DbPage*
5e6c0 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 );.#endif..#ifde
5e6d0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
5e6e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
5e6f0 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 nt *sqlite3Pager
5e700 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 Stats(Pager*);.S
5e710 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
5e720 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
5e730 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29 rRefdump(Pager*)
5e740 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5e750 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 int sqlite3Pa
5e760 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 gerIsMemdb(Pager
5e770 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 *);.#endif..#ifd
5e780 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 76 ef SQLITE_TEST.v
5e790 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 oid disable_simu
5e7a0 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
5e7b0 76 6f 69 64 29 3b 0a 76 6f 69 64 20 65 6e 61 62 void);.void enab
5e7c0 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f le_simulated_io_
5e7d0 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a 23 65 errors(void);.#e
5e7e0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73 lse.# define dis
5e7f0 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
5e800 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66 o_errors().# def
5e810 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c ine enable_simul
5e820 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 ated_io_errors()
5e830 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
5e840 2f 2a 20 5f 50 41 47 45 52 5f 48 5f 20 2a 2f 0a /* _PAGER_H_ */.
5e850 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
5e860 20 45 6e 64 20 6f 66 20 70 61 67 65 72 2e 68 20 End of pager.h
5e870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
5e8a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
5e8b0 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
5e8c0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
5e8d0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
5e8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
5e8f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
5e900 20 49 6e 63 6c 75 64 65 20 70 63 61 63 68 65 2e Include pcache.
5e910 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
5e920 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a of sqliteInt.h *
5e930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
5e940 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
5e950 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63 61 63 Begin file pcac
5e960 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a he.h ***********
5e970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
5e990 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 ./*.** 2008 Augu
5e9a0 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 05.**.** The
5e9b0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
5e9c0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
5e9d0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
5e9e0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
5e9f0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
5ea00 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
5ea10 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
5ea20 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
5ea30 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
5ea40 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
5ea50 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
5ea60 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
5ea70 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
5ea80 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
5ea90 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
5eaa0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
5eab0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
5eac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5ead0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5eae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5eaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5eb00 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 ****.** This hea
5eb10 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 der file defines
5eb20 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 the interface t
5eb30 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 70 hat the sqlite p
5eb40 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 age cache.** sub
5eb50 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 40 system. .**.** @
5eb60 28 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 2e (#) $Id: pcache.
5eb70 68 2c 76 20 31 2e 31 36 20 32 30 30 38 2f 31 31 h,v 1.16 2008/11
5eb80 2f 31 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e /19 16:52:44 dan
5eb90 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a ielk1977 Exp $.*
5eba0 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 43 41 43 /..#ifndef _PCAC
5ebb0 48 45 5f 48 5f 0a 0a 74 79 70 65 64 65 66 20 73 HE_H_..typedef s
5ebc0 74 72 75 63 74 20 50 67 48 64 72 20 50 67 48 64 truct PgHdr PgHd
5ebd0 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 r;.typedef struc
5ebe0 74 20 50 43 61 63 68 65 20 50 43 61 63 68 65 3b t PCache PCache;
5ebf0 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 72 79 20 70 61 ../*.** Every pa
5ec00 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 ge in the cache
5ec10 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 is controlled by
5ec20 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
5ec30 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a the following.**
5ec40 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
5ec50 74 72 75 63 74 20 50 67 48 64 72 20 7b 0a 20 20 truct PgHdr {.
5ec60 76 6f 69 64 20 2a 70 44 61 74 61 3b 20 20 20 20 void *pData;
5ec70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5ec80 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 * Content of thi
5ec90 73 20 70 61 67 65 20 2a 2f 0a 20 20 76 6f 69 64 s page */. void
5eca0 20 2a 70 45 78 74 72 61 3b 20 20 20 20 20 20 20 *pExtra;
5ecb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
5ecc0 74 72 61 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 tra content */.
5ecd0 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 3b 20 PgHdr *pDirty;
5ece0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ecf0 2f 2a 20 54 72 61 6e 73 69 65 6e 74 20 6c 69 73 /* Transient lis
5ed00 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 t of dirty pages
5ed10 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b */. Pgno pgno;
5ed20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ed30 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d /* Page num
5ed40 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61 67 ber for this pag
5ed50 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 50 e */. Pager *pP
5ed60 61 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 ager;
5ed70 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
5ed80 65 72 20 74 68 69 73 20 70 61 67 65 20 69 73 20 er this page is
5ed90 70 61 72 74 20 6f 66 20 2a 2f 0a 23 69 66 64 65 part of */.#ifde
5eda0 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
5edb0 41 47 45 53 0a 20 20 75 33 32 20 70 61 67 65 48 AGES. u32 pageH
5edc0 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 ash;
5edd0 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 6f 66 /* Hash of
5ede0 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 2a 2f page content */
5edf0 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 20 66 6c .#endif. u16 fl
5ee00 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
5ee10 20 20 20 20 20 20 20 20 20 2f 2a 20 50 47 48 44 /* PGHD
5ee20 52 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20 R flags defined
5ee30 62 65 6c 6f 77 20 2a 2f 0a 0a 20 20 2f 2a 2a 2a below */.. /***
5ee40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5ee50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5ee60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5ee70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5ee80 2a 2a 2a 0a 20 20 2a 2a 20 45 6c 65 6d 65 6e 74 ***. ** Element
5ee90 73 20 61 62 6f 76 65 20 61 72 65 20 70 75 62 6c s above are publ
5eea0 69 63 2e 20 20 41 6c 6c 20 74 68 61 74 20 66 6f ic. All that fo
5eeb0 6c 6c 6f 77 73 20 69 73 20 70 72 69 76 61 74 65 llows is private
5eec0 20 74 6f 20 70 63 61 63 68 65 2e 63 0a 20 20 2a to pcache.c. *
5eed0 2a 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 * and should not
5eee0 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20 be accessed by
5eef0 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 20 other modules..
5ef00 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 66 3b 20 */. i16 nRef;
5ef10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ef20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
5ef30 66 20 75 73 65 72 73 20 6f 66 20 74 68 69 73 20 f users of this
5ef40 70 61 67 65 20 2a 2f 0a 20 20 50 43 61 63 68 65 page */. PCache
5ef50 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 *pCache;
5ef60 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 /* Cach
5ef70 65 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73 e that owns this
5ef80 20 70 61 67 65 20 2a 2f 0a 0a 20 20 50 67 48 64 page */.. PgHd
5ef90 72 20 2a 70 44 69 72 74 79 4e 65 78 74 3b 20 20 r *pDirtyNext;
5efa0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
5efb0 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 xt element in li
5efc0 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 st of dirty page
5efd0 73 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 s */. PgHdr *pD
5efe0 69 72 74 79 50 72 65 76 3b 20 20 20 20 20 20 20 irtyPrev;
5eff0 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 /* Previou
5f000 73 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 73 s element in lis
5f010 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 t of dirty pages
5f020 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 42 69 74 20 76 */.};../* Bit v
5f030 61 6c 75 65 73 20 66 6f 72 20 50 67 48 64 72 2e alues for PgHdr.
5f040 66 6c 61 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 flags */.#define
5f050 20 50 47 48 44 52 5f 44 49 52 54 59 20 20 20 20 PGHDR_DIRTY
5f060 20 20 20 20 20 20 20 20 20 30 78 30 30 32 20 20 0x002
5f070 2f 2a 20 50 61 67 65 20 68 61 73 20 63 68 61 6e /* Page has chan
5f080 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ged */.#define P
5f090 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 20 GHDR_NEED_SYNC
5f0a0 20 20 20 20 20 20 20 30 78 30 30 34 20 20 2f 2a 0x004 /*
5f0b0 20 46 73 79 6e 63 20 74 68 65 20 72 6f 6c 6c 62 Fsync the rollb
5f0c0 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f ack journal befo
5f0d0 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 re.
5f0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5f0f0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 72 69 ** wri
5f100 74 69 6e 67 20 74 68 69 73 20 70 61 67 65 20 74 ting this page t
5f110 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a o the database *
5f120 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f /.#define PGHDR_
5f130 4e 45 45 44 5f 52 45 41 44 20 20 20 20 20 20 20 NEED_READ
5f140 20 20 30 78 30 30 38 20 20 2f 2a 20 43 6f 6e 74 0x008 /* Cont
5f150 65 6e 74 20 69 73 20 75 6e 72 65 61 64 20 2a 2f ent is unread */
5f160 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 52 .#define PGHDR_R
5f170 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 20 20 EUSE_UNLIKELY
5f180 20 30 78 30 31 30 20 20 2f 2a 20 41 20 68 69 6e 0x010 /* A hin
5f190 74 20 74 68 61 74 20 72 65 75 73 65 20 69 73 20 t that reuse is
5f1a0 75 6e 6c 69 6b 65 6c 79 20 2a 2f 0a 23 64 65 66 unlikely */.#def
5f1b0 69 6e 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 ine PGHDR_DONT_W
5f1c0 52 49 54 45 20 20 20 20 20 20 20 20 30 78 30 32 RITE 0x02
5f1d0 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69 0 /* Do not wri
5f1e0 74 65 20 63 6f 6e 74 65 6e 74 20 74 6f 20 64 69 te content to di
5f1f0 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 6e 69 74 69 61 sk */../* Initia
5f200 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f 77 lize and shutdow
5f210 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 n the page cache
5f220 20 73 75 62 73 79 73 74 65 6d 20 2a 2f 0a 53 51 subsystem */.SQ
5f230 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5f240 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 6e sqlite3PcacheIn
5f250 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b 0a itialize(void);.
5f260 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5f270 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
5f280 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 3b eShutdown(void);
5f290 0a 0a 2f 2a 20 50 61 67 65 20 63 61 63 68 65 20 ../* Page cache
5f2a0 62 75 66 66 65 72 20 6d 61 6e 61 67 65 6d 65 6e buffer managemen
5f2b0 74 3a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 t:.** These rout
5f2c0 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 53 ines implement S
5f2d0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
5f2e0 45 43 41 43 48 45 2e 0a 2a 2f 0a 53 51 4c 49 54 ECACHE..*/.SQLIT
5f2f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
5f300 71 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66 qlite3PCacheBuff
5f310 65 72 53 65 74 75 70 28 76 6f 69 64 20 2a 2c 20 erSetup(void *,
5f320 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 3b 0a int sz, int n);.
5f330 0a 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 ./* Create a new
5f340 20 70 61 67 65 72 20 63 61 63 68 65 2e 0a 2a 2a pager cache..**
5f350 20 55 6e 64 65 72 20 6d 65 6d 6f 72 79 20 73 74 Under memory st
5f360 72 65 73 73 2c 20 69 6e 76 6f 6b 65 20 78 53 74 ress, invoke xSt
5f370 72 65 73 73 20 74 6f 20 74 72 79 20 74 6f 20 6d ress to try to m
5f380 61 6b 65 20 70 61 67 65 73 20 63 6c 65 61 6e 2e ake pages clean.
5f390 0a 2a 2a 20 4f 6e 6c 79 20 63 6c 65 61 6e 20 61 .** Only clean a
5f3a0 6e 64 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 nd unpinned page
5f3b0 73 20 63 61 6e 20 62 65 20 72 65 63 6c 61 69 6d s can be reclaim
5f3c0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
5f3d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
5f3e0 65 33 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 e3PcacheOpen(.
5f3f0 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 int szPage,
5f400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5f410 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20 * Size of every
5f420 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a page */. int sz
5f430 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 Extra,
5f440 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 /* Extr
5f450 61 20 73 70 61 63 65 20 61 73 73 6f 63 69 61 74 a space associat
5f460 65 64 20 77 69 74 68 20 65 61 63 68 20 70 61 67 ed with each pag
5f470 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 e */. int bPurg
5f480 65 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 eable,
5f490 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
5f4a0 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 pages are on ba
5f4b0 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 cking store */.
5f4c0 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 int (*xStress)(
5f4d0 76 6f 69 64 2a 2c 20 50 67 48 64 72 2a 29 2c 20 void*, PgHdr*),
5f4e0 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 74 /* Call to try t
5f4f0 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65 o make pages cle
5f500 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 an */. void *pS
5f510 74 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 tress,
5f520 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 /* Argume
5f530 6e 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f nt to xStress */
5f540 0a 20 20 50 43 61 63 68 65 20 2a 70 54 6f 49 6e . PCache *pToIn
5f550 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 it
5f560 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 /* Preallocate
5f570 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 d space for the
5f580 50 43 61 63 68 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a PCache */.);../*
5f590 20 4d 6f 64 69 66 79 20 74 68 65 20 70 61 67 65 Modify the page
5f5a0 2d 73 69 7a 65 20 61 66 74 65 72 20 74 68 65 20 -size after the
5f5b0 63 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 63 cache has been c
5f5c0 72 65 61 74 65 64 2e 20 2a 2f 0a 53 51 4c 49 54 reated. */.SQLIT
5f5d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
5f5e0 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 50 qlite3PcacheSetP
5f5f0 61 67 65 53 69 7a 65 28 50 43 61 63 68 65 20 2a ageSize(PCache *
5f600 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20 52 65 74 75 , int);../* Retu
5f610 72 6e 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 rn the size in b
5f620 79 74 65 73 20 6f 66 20 61 20 50 43 61 63 68 65 ytes of a PCache
5f630 20 6f 62 6a 65 63 74 2e 20 20 55 73 65 64 20 74 object. Used t
5f640 6f 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a o preallocate.**
5f650 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a storage space..
5f660 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
5f670 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
5f680 63 68 65 53 69 7a 65 28 76 6f 69 64 29 3b 0a 0a cheSize(void);..
5f690 2f 2a 20 4f 6e 65 20 72 65 6c 65 61 73 65 20 70 /* One release p
5f6a0 65 72 20 73 75 63 63 65 73 73 66 75 6c 20 66 65 er successful fe
5f6b0 74 63 68 2e 20 20 50 61 67 65 20 69 73 20 70 69 tch. Page is pi
5f6c0 6e 6e 65 64 20 75 6e 74 69 6c 20 72 65 6c 65 61 nned until relea
5f6d0 73 65 64 2e 0a 2a 2a 20 52 65 66 65 72 65 6e 63 sed..** Referenc
5f6e0 65 20 63 6f 75 6e 74 65 64 2e 20 0a 2a 2f 0a 53 e counted. .*/.S
5f6f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5f700 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 t sqlite3PcacheF
5f710 65 74 63 68 28 50 43 61 63 68 65 2a 2c 20 50 67 etch(PCache*, Pg
5f720 6e 6f 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c no, int createFl
5f730 61 67 2c 20 50 67 48 64 72 2a 2a 29 3b 0a 53 51 ag, PgHdr**);.SQ
5f740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
5f750 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 d sqlite3PcacheR
5f760 65 6c 65 61 73 65 28 50 67 48 64 72 2a 29 3b 0a elease(PgHdr*);.
5f770 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5f780 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
5f790 68 65 44 72 6f 70 28 50 67 48 64 72 2a 29 3b 20 heDrop(PgHdr*);
5f7a0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 /* Remov
5f7b0 65 20 70 61 67 65 20 66 72 6f 6d 20 63 61 63 68 e page from cach
5f7c0 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 e */.SQLITE_PRIV
5f7d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5f7e0 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 PcacheMakeDirty(
5f7f0 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 4d PgHdr*); /* M
5f800 61 6b 65 20 73 75 72 65 20 70 61 67 65 20 69 73 ake sure page is
5f810 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20 2a 2f marked dirty */
5f820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5f830 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
5f840 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 heMakeClean(PgHd
5f850 72 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 r*); /* Mark
5f860 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 61 73 a single page as
5f870 20 63 6c 65 61 6e 20 2a 2f 0a 53 51 4c 49 54 45 clean */.SQLITE
5f880 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
5f890 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e lite3PcacheClean
5f8a0 41 6c 6c 28 50 43 61 63 68 65 2a 29 3b 20 20 20 All(PCache*);
5f8b0 20 2f 2a 20 4d 61 72 6b 20 61 6c 6c 20 64 69 72 /* Mark all dir
5f8c0 74 79 20 6c 69 73 74 20 70 61 67 65 73 20 61 73 ty list pages as
5f8d0 20 63 6c 65 61 6e 20 2a 2f 0a 0a 2f 2a 20 43 68 clean */../* Ch
5f8e0 61 6e 67 65 20 61 20 70 61 67 65 20 6e 75 6d 62 ange a page numb
5f8f0 65 72 2e 20 20 55 73 65 64 20 62 79 20 69 6e 63 er. Used by inc
5f900 72 2d 76 61 63 75 75 6d 2e 20 2a 2f 0a 53 51 4c r-vacuum. */.SQL
5f910 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
5f920 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f sqlite3PcacheMo
5f930 76 65 28 50 67 48 64 72 2a 2c 20 50 67 6e 6f 29 ve(PgHdr*, Pgno)
5f940 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c ;../* Remove all
5f950 20 70 61 67 65 73 20 77 69 74 68 20 70 67 6e 6f pages with pgno
5f960 3e 78 2e 20 20 52 65 73 65 74 20 74 68 65 20 63 >x. Reset the c
5f970 61 63 68 65 20 69 66 20 78 3d 3d 30 20 2a 2f 0a ache if x==0 */.
5f980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5f990 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
5f9a0 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 eTruncate(PCache
5f9b0 2a 2c 20 50 67 6e 6f 20 78 29 3b 0a 0a 2f 2a 20 *, Pgno x);../*
5f9c0 47 65 74 20 61 20 6c 69 73 74 20 6f 66 20 61 6c Get a list of al
5f9d0 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e l dirty pages in
5f9e0 20 74 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74 the cache, sort
5f9f0 65 64 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65 ed by page numbe
5fa00 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 r */.SQLITE_PRIV
5fa10 41 54 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 ATE PgHdr *sqlit
5fa20 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 e3PcacheDirtyLis
5fa30 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 t(PCache*);../*
5fa40 52 65 73 65 74 20 61 6e 64 20 63 6c 6f 73 65 20 Reset and close
5fa50 74 68 65 20 63 61 63 68 65 20 6f 62 6a 65 63 74 the cache object
5fa60 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
5fa70 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
5fa80 63 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68 cacheClose(PCach
5fa90 65 2a 29 3b 0a 0a 2f 2a 20 43 6c 65 61 72 20 66 e*);../* Clear f
5faa0 6c 61 67 73 20 66 72 6f 6d 20 70 61 67 65 73 20 lags from pages
5fab0 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 of the page cach
5fac0 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 e */.SQLITE_PRIV
5fad0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5fae0 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46 PcacheClearSyncF
5faf0 6c 61 67 73 28 50 43 61 63 68 65 20 2a 29 3b 0a lags(PCache *);.
5fb00 0a 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 ./* Discard the
5fb10 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
5fb20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f cache */.SQLITE_
5fb30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
5fb40 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 50 te3PcacheClear(P
5fb50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 74 Cache*);../* Ret
5fb60 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 urn the total nu
5fb70 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 mber of outstand
5fb80 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 6e ing page referen
5fb90 63 65 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ces */.SQLITE_PR
5fba0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5fbb0 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 3PcacheRefCount(
5fbc0 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 49 6e PCache*);../* In
5fbd0 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 crement the refe
5fbe0 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 rence count of a
5fbf0 6e 20 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 n existing page
5fc00 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
5fc10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
5fc20 61 63 68 65 52 65 66 28 50 67 48 64 72 2a 29 3b acheRef(PgHdr*);
5fc30 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
5fc40 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 int sqlite3Pcac
5fc50 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 50 hePageRefcount(P
5fc60 67 48 64 72 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 gHdr*);../* Retu
5fc70 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d rn the total num
5fc80 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 6f ber of pages sto
5fc90 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 red in the cache
5fca0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
5fcb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
5fcc0 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 50 43 achePagecount(PC
5fcd0 61 63 68 65 2a 29 3b 0a 0a 23 69 66 64 65 66 20 ache*);..#ifdef
5fce0 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
5fcf0 45 53 0a 2f 2a 20 49 74 65 72 61 74 65 20 74 68 ES./* Iterate th
5fd00 72 6f 75 67 68 20 61 6c 6c 20 64 69 72 74 79 20 rough all dirty
5fd10 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 pages currently
5fd20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 stored in the ca
5fd30 63 68 65 2e 20 54 68 69 73 0a 2a 2a 20 69 6e 74 che. This.** int
5fd40 65 72 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 61 erface is only a
5fd50 76 61 69 6c 61 62 6c 65 20 69 66 20 53 51 4c 49 vailable if SQLI
5fd60 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 69 TE_CHECK_PAGES i
5fd70 73 20 64 65 66 69 6e 65 64 20 77 68 65 6e 20 74 s defined when t
5fd80 68 65 20 0a 2a 2a 20 6c 69 62 72 61 72 79 20 69 he .** library i
5fd90 73 20 62 75 69 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 s built..*/.SQLI
5fda0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
5fdb0 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 sqlite3PcacheIte
5fdc0 72 61 74 65 44 69 72 74 79 28 50 43 61 63 68 65 rateDirty(PCache
5fdd0 20 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 *pCache, void (
5fde0 2a 78 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 *xIter)(PgHdr *)
5fdf0 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 53 65 );.#endif../* Se
5fe00 74 20 61 6e 64 20 67 65 74 20 74 68 65 20 73 75 t and get the su
5fe10 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 ggested cache-si
5fe20 7a 65 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 ze for the speci
5fe30 66 69 65 64 20 70 61 67 65 72 2d 63 61 63 68 65 fied pager-cache
5fe40 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 67 6c ..**.** If no gl
5fe50 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 69 73 20 obal maximum is
5fe60 63 6f 6e 66 69 67 75 72 65 64 2c 20 74 68 65 6e configured, then
5fe70 20 74 68 65 20 73 79 73 74 65 6d 20 61 74 74 65 the system atte
5fe80 6d 70 74 73 20 74 6f 20 6c 69 6d 69 74 0a 2a 2a mpts to limit.**
5fe90 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 the total numbe
5fea0 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68 65 r of pages cache
5feb0 64 20 62 79 20 70 75 72 67 65 61 62 6c 65 20 70 d by purgeable p
5fec0 61 67 65 72 2d 63 61 63 68 65 73 20 74 6f 20 74 ager-caches to t
5fed0 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 he sum.** of the
5fee0 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68 65 suggested cache
5fef0 2d 73 69 7a 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 -sizes..*/.SQLIT
5ff00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
5ff10 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43 qlite3PcacheSetC
5ff20 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 achesize(PCache
5ff30 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 64 65 66 20 *, int);.#ifdef
5ff40 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
5ff50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5ff60 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 qlite3PcacheGetC
5ff70 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 achesize(PCache
5ff80 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 *);.#endif..#ifd
5ff90 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
5ffa0 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
5ffb0 4e 54 0a 2f 2a 20 54 72 79 20 74 6f 20 72 65 74 NT./* Try to ret
5ffc0 75 72 6e 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 urn memory used
5ffd0 62 79 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f by the pcache mo
5ffe0 64 75 6c 65 20 74 6f 20 74 68 65 20 6d 61 69 6e dule to the main
5fff0 20 6d 65 6d 6f 72 79 20 68 65 61 70 20 2a 2f 0a memory heap */.
60000 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
60010 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
60020 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e ReleaseMemory(in
60030 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 t);.#endif..#ifd
60040 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
60050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
60060 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
60070 53 74 61 74 73 28 69 6e 74 2a 2c 69 6e 74 2a 2c Stats(int*,int*,
60080 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 23 65 6e 64 int*,int*);.#end
60090 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 if..SQLITE_PRIVA
600a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
600b0 43 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 CacheSetDefault(
600c0 76 6f 69 64 29 3b 0a 0a 23 65 6e 64 69 66 20 2f void);..#endif /
600d0 2a 20 5f 50 43 41 43 48 45 5f 48 5f 20 2a 2f 0a * _PCACHE_H_ */.
600e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
600f0 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 2e 68 End of pcache.h
60100 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
60110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
60130 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
60140 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
60150 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
60160 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
60170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
60180 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
60190 2a 20 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20 69 * Include os.h i
601a0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
601b0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
601c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
601d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
601e0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e * Begin file os.
601f0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
60200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60220 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
60230 74 65 6d 62 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 tember 16.**.**
60240 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
60250 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
60260 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
60270 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
60280 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
60290 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
602a0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
602b0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
602c0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
602d0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
602e0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
602f0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
60300 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
60310 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
60320 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
60330 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
60340 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
60350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
603a0 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 .** This header
603b0 66 69 6c 65 20 28 74 6f 67 65 74 68 65 72 20 77 file (together w
603c0 69 74 68 20 69 73 20 63 6f 6d 70 61 6e 69 6f 6e ith is companion
603d0 20 43 20 73 6f 75 72 63 65 2d 63 6f 64 65 20 66 C source-code f
603e0 69 6c 65 0a 2a 2a 20 22 6f 73 2e 63 22 29 20 61 ile.** "os.c") a
603f0 74 74 65 6d 70 74 20 74 6f 20 61 62 73 74 72 61 ttempt to abstra
60400 63 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e ct the underlyin
60410 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 g operating syst
60420 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 74 68 em so that.** th
60430 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 e SQLite library
60440 20 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f will work on bo
60450 74 68 20 50 4f 53 49 58 20 61 6e 64 20 77 69 6e th POSIX and win
60460 64 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2a dows systems..**
60470 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 .** This header
60480 66 69 6c 65 20 69 73 20 23 69 6e 63 6c 75 64 65 file is #include
60490 2d 65 64 20 62 79 20 73 71 6c 69 74 65 49 6e 74 -ed by sqliteInt
604a0 2e 68 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73 .h and thus ends
604b0 20 75 70 0a 2a 2a 20 62 65 69 6e 67 20 69 6e 63 up.** being inc
604c0 6c 75 64 65 64 20 62 79 20 65 76 65 72 79 20 73 luded by every s
604d0 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a ource file..**.*
604e0 2a 20 24 49 64 3a 20 6f 73 2e 68 2c 76 20 31 2e * $Id: os.h,v 1.
604f0 31 30 37 20 32 30 30 39 2f 30 31 2f 31 34 20 32 107 2009/01/14 2
60500 33 3a 30 33 3a 34 31 20 64 72 68 20 45 78 70 20 3:03:41 drh Exp
60510 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 $.*/.#ifndef _SQ
60520 4c 49 54 45 5f 4f 53 5f 48 5f 0a 23 64 65 66 69 LITE_OS_H_.#defi
60530 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f ne _SQLITE_OS_H_
60540 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f ../*.** Figure o
60550 75 74 20 69 66 20 77 65 20 61 72 65 20 64 65 61 ut if we are dea
60560 6c 69 6e 67 20 77 69 74 68 20 55 6e 69 78 2c 20 ling with Unix,
60570 57 69 6e 64 6f 77 73 2c 20 6f 72 20 73 6f 6d 65 Windows, or some
60580 20 6f 74 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74 other.** operat
60590 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 41 66 74 ing system. Aft
605a0 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 er the following
605b0 20 62 6c 6f 63 6b 20 6f 66 20 70 72 65 70 72 6f block of prepro
605c0 63 65 73 73 20 6d 61 63 72 6f 73 2c 0a 2a 2a 20 cess macros,.**
605d0 61 6c 6c 20 6f 66 20 53 51 4c 49 54 45 5f 4f 53 all of SQLITE_OS
605e0 5f 55 4e 49 58 2c 20 53 51 4c 49 54 45 5f 4f 53 _UNIX, SQLITE_OS
605f0 5f 57 49 4e 2c 20 53 51 4c 49 54 45 5f 4f 53 5f _WIN, SQLITE_OS_
60600 4f 53 32 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f OS2, and SQLITE_
60610 4f 53 5f 4f 54 48 45 52 20 0a 2a 2a 20 77 69 6c OS_OTHER .** wil
60620 6c 20 64 65 66 69 6e 65 64 20 74 6f 20 65 69 74 l defined to eit
60630 68 65 72 20 31 20 6f 72 20 30 2e 20 20 4f 6e 65 her 1 or 0. One
60640 20 6f 66 20 74 68 65 20 66 6f 75 72 20 77 69 6c of the four wil
60650 6c 20 62 65 20 31 2e 20 20 54 68 65 20 6f 74 68 l be 1. The oth
60660 65 72 20 0a 2a 2a 20 74 68 72 65 65 20 77 69 6c er .** three wil
60670 6c 20 62 65 20 30 2e 0a 2a 2f 0a 23 69 66 20 64 l be 0..*/.#if d
60680 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 efined(SQLITE_OS
60690 5f 4f 54 48 45 52 29 0a 23 20 69 66 20 53 51 4c _OTHER).# if SQL
606a0 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 3d 31 0a ITE_OS_OTHER==1.
606b0 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 # undef SQLITE
606c0 5f 4f 53 5f 55 4e 49 58 0a 23 20 20 20 64 65 66 _OS_UNIX.# def
606d0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e ine SQLITE_OS_UN
606e0 49 58 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53 IX 0.# undef S
606f0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 QLITE_OS_WIN.#
60700 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
60710 53 5f 57 49 4e 20 30 0a 23 20 20 20 75 6e 64 65 S_WIN 0.# unde
60720 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a f SQLITE_OS_OS2.
60730 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 # define SQLIT
60740 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6c 73 E_OS_OS2 0.# els
60750 65 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 e.# undef SQLI
60760 54 45 5f 4f 53 5f 4f 54 48 45 52 0a 23 20 65 6e TE_OS_OTHER.# en
60770 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 20 21 dif.#endif.#if !
60780 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
60790 53 5f 55 4e 49 58 29 20 26 26 20 21 64 65 66 69 S_UNIX) && !defi
607a0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 ned(SQLITE_OS_OT
607b0 48 45 52 29 0a 23 20 64 65 66 69 6e 65 20 53 51 HER).# define SQ
607c0 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 30 0a LITE_OS_OTHER 0.
607d0 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f # ifndef SQLITE_
607e0 4f 53 5f 57 49 4e 0a 23 20 20 20 69 66 20 64 65 OS_WIN.# if de
607f0 66 69 6e 65 64 28 5f 57 49 4e 33 32 29 20 7c 7c fined(_WIN32) ||
60800 20 64 65 66 69 6e 65 64 28 57 49 4e 33 32 29 20 defined(WIN32)
60810 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 || defined(__CYG
60820 57 49 4e 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 WIN__) || define
60830 64 28 5f 5f 4d 49 4e 47 57 33 32 5f 5f 29 20 7c d(__MINGW32__) |
60840 7c 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c | defined(__BORL
60850 41 4e 44 43 5f 5f 29 0a 23 20 20 20 20 20 64 65 ANDC__).# de
60860 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 fine SQLITE_OS_W
60870 49 4e 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e IN 1.# defin
60880 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 e SQLITE_OS_UNIX
60890 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 0.# define
608a0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a SQLITE_OS_OS2 0.
608b0 23 20 20 20 65 6c 69 66 20 64 65 66 69 6e 65 64 # elif defined
608c0 28 5f 5f 45 4d 58 5f 5f 29 20 7c 7c 20 64 65 66 (__EMX__) || def
608d0 69 6e 65 64 28 5f 4f 53 32 29 20 7c 7c 20 64 65 ined(_OS2) || de
608e0 66 69 6e 65 64 28 4f 53 32 29 20 7c 7c 20 64 65 fined(OS2) || de
608f0 66 69 6e 65 64 28 5f 4f 53 32 5f 29 20 7c 7c 20 fined(_OS2_) ||
60900 64 65 66 69 6e 65 64 28 5f 5f 4f 53 32 5f 5f 29 defined(__OS2__)
60910 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 .# define SQ
60920 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 LITE_OS_WIN 0.#
60930 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
60940 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 E_OS_UNIX 0.#
60950 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
60960 4f 53 5f 4f 53 32 20 31 0a 23 20 20 20 65 6c 73 OS_OS2 1.# els
60970 65 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 e.# define S
60980 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 QLITE_OS_WIN 0.#
60990 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
609a0 54 45 5f 4f 53 5f 55 4e 49 58 20 31 0a 23 20 20 TE_OS_UNIX 1.#
609b0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
609c0 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20 65 6e 64 _OS_OS2 0.# end
609d0 69 66 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 if.# else.# def
609e0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e ine SQLITE_OS_UN
609f0 49 58 20 30 0a 23 20 20 64 65 66 69 6e 65 20 53 IX 0.# define S
60a00 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 QLITE_OS_OS2 0.#
60a10 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 23 20 69 endif.#else.# i
60a20 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f fndef SQLITE_OS_
60a30 57 49 4e 0a 23 20 20 64 65 66 69 6e 65 20 53 51 WIN.# define SQ
60a40 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 LITE_OS_WIN 0.#
60a50 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a endif.#endif../*
60a60 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66 .** Determine if
60a70 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 we are dealing
60a80 77 69 74 68 20 57 69 6e 64 6f 77 73 43 45 20 2d with WindowsCE -
60a90 20 77 68 69 63 68 20 68 61 73 20 61 20 6d 75 63 which has a muc
60aa0 68 0a 2a 2a 20 72 65 64 75 63 65 64 20 41 50 49 h.** reduced API
60ab0 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
60ac0 28 5f 57 49 4e 33 32 5f 57 43 45 29 0a 23 20 64 (_WIN32_WCE).# d
60ad0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
60ae0 57 49 4e 43 45 20 31 0a 23 65 6c 73 65 0a 23 20 WINCE 1.#else.#
60af0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
60b00 5f 57 49 4e 43 45 20 30 0a 23 65 6e 64 69 66 0a _WINCE 0.#endif.
60b10 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 ../*.** Define t
60b20 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 he maximum size
60b30 6f 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 of a temporary f
60b40 69 6c 65 6e 61 6d 65 0a 2a 2f 0a 23 69 66 20 53 ilename.*/.#if S
60b50 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 69 QLITE_OS_WIN.# i
60b60 6e 63 6c 75 64 65 20 3c 77 69 6e 64 6f 77 73 2e nclude <windows.
60b70 68 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 h>.# define SQLI
60b80 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 TE_TEMPNAME_SIZE
60b90 20 28 4d 41 58 5f 50 41 54 48 2b 35 30 29 0a 23 (MAX_PATH+50).#
60ba0 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f elif SQLITE_OS_O
60bb0 53 32 0a 23 20 69 66 20 28 5f 5f 47 4e 55 43 5f S2.# if (__GNUC_
60bc0 5f 20 3e 20 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f _ > 3 || __GNUC_
60bd0 5f 20 3d 3d 20 33 20 26 26 20 5f 5f 47 4e 55 43 _ == 3 && __GNUC
60be0 5f 4d 49 4e 4f 52 5f 5f 20 3e 3d 20 33 29 20 26 _MINOR__ >= 3) &
60bf0 26 20 64 65 66 69 6e 65 64 28 4f 53 32 5f 48 49 & defined(OS2_HI
60c00 47 48 5f 4d 45 4d 4f 52 59 29 0a 23 20 20 69 6e GH_MEMORY).# in
60c10 63 6c 75 64 65 20 3c 6f 73 32 73 61 66 65 2e 68 clude <os2safe.h
60c20 3e 20 2f 2a 20 68 61 73 20 74 6f 20 62 65 20 69 > /* has to be i
60c30 6e 63 6c 75 64 65 64 20 62 65 66 6f 72 65 20 6f ncluded before o
60c40 73 32 2e 68 20 66 6f 72 20 6c 69 6e 6b 69 6e 67 s2.h for linking
60c50 20 74 6f 20 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e to work */.# en
60c60 64 69 66 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 dif.# define INC
60c70 4c 5f 44 4f 53 44 41 54 45 54 49 4d 45 0a 23 20 L_DOSDATETIME.#
60c80 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 46 define INCL_DOSF
60c90 49 4c 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 ILEMGR.# define
60ca0 49 4e 43 4c 5f 44 4f 53 45 52 52 4f 52 53 0a 23 INCL_DOSERRORS.#
60cb0 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 define INCL_DOS
60cc0 4d 49 53 43 0a 23 20 64 65 66 69 6e 65 20 49 4e MISC.# define IN
60cd0 43 4c 5f 44 4f 53 50 52 4f 43 45 53 53 0a 23 20 CL_DOSPROCESS.#
60ce0 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d define INCL_DOSM
60cf0 4f 44 55 4c 45 4d 47 52 0a 23 20 64 65 66 69 6e ODULEMGR.# defin
60d00 65 20 49 4e 43 4c 5f 44 4f 53 53 45 4d 41 50 48 e INCL_DOSSEMAPH
60d10 4f 52 45 53 0a 23 20 69 6e 63 6c 75 64 65 20 3c ORES.# include <
60d20 6f 73 32 2e 68 3e 0a 23 20 69 6e 63 6c 75 64 65 os2.h>.# include
60d30 20 3c 75 63 6f 6e 76 2e 68 3e 0a 23 20 64 65 66 <uconv.h>.# def
60d40 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e ine SQLITE_TEMPN
60d50 41 4d 45 5f 53 49 5a 45 20 28 43 43 48 4d 41 58 AME_SIZE (CCHMAX
60d60 50 41 54 48 43 4f 4d 50 29 0a 23 65 6c 73 65 0a PATHCOMP).#else.
60d70 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
60d80 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 32 30 TEMPNAME_SIZE 20
60d90 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 66 20 0.#endif../* If
60da0 74 68 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 the SET_FULLSYNC
60db0 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20 64 65 macro is not de
60dc0 66 69 6e 65 64 20 61 62 6f 76 65 2c 20 74 68 65 fined above, the
60dd0 6e 20 6d 61 6b 65 20 69 74 0a 2a 2a 20 61 20 6e n make it.** a n
60de0 6f 2d 6f 70 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 o-op.*/.#ifndef
60df0 53 45 54 5f 46 55 4c 4c 53 59 4e 43 0a 23 20 64 SET_FULLSYNC.# d
60e00 65 66 69 6e 65 20 53 45 54 5f 46 55 4c 4c 53 59 efine SET_FULLSY
60e10 4e 43 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a NC(x,y).#endif..
60e20 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c /*.** The defaul
60e30 74 20 73 69 7a 65 20 6f 66 20 61 20 64 69 73 6b t size of a disk
60e40 20 73 65 63 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 sector.*/.#ifnd
60e50 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ef SQLITE_DEFAUL
60e60 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a 23 20 T_SECTOR_SIZE.#
60e70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
60e80 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a FAULT_SECTOR_SIZ
60e90 45 20 35 31 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a E 512.#endif../*
60ea0 0a 2a 2a 20 54 65 6d 70 6f 72 61 72 79 20 66 69 .** Temporary fi
60eb0 6c 65 73 20 61 72 65 20 6e 61 6d 65 64 20 73 74 les are named st
60ec0 61 72 74 69 6e 67 20 77 69 74 68 20 74 68 69 73 arting with this
60ed0 20 70 72 65 66 69 78 20 66 6f 6c 6c 6f 77 65 64 prefix followed
60ee0 20 62 79 20 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a by 16 random.**
60ef0 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 63 68 alphanumeric ch
60f00 61 72 61 63 74 65 72 73 2c 20 61 6e 64 20 6e 6f aracters, and no
60f10 20 66 69 6c 65 20 65 78 74 65 6e 73 69 6f 6e 2e file extension.
60f20 20 54 68 65 79 20 61 72 65 20 73 74 6f 72 65 64 They are stored
60f30 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 53 27 73 20 in the.** OS's
60f40 73 74 61 6e 64 61 72 64 20 74 65 6d 70 6f 72 61 standard tempora
60f50 72 79 20 66 69 6c 65 20 64 69 72 65 63 74 6f 72 ry file director
60f60 79 2c 20 61 6e 64 20 61 72 65 20 64 65 6c 65 74 y, and are delet
60f70 65 64 20 70 72 69 6f 72 20 74 6f 20 65 78 69 74 ed prior to exit
60f80 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 20 69 ..** If sqlite i
60f90 73 20 62 65 69 6e 67 20 65 6d 62 65 64 64 65 64 s being embedded
60fa0 20 69 6e 20 61 6e 6f 74 68 65 72 20 70 72 6f 67 in another prog
60fb0 72 61 6d 2c 20 79 6f 75 20 6d 61 79 20 77 69 73 ram, you may wis
60fc0 68 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a h to change the.
60fd0 2a 2a 20 70 72 65 66 69 78 20 74 6f 20 72 65 66 ** prefix to ref
60fe0 6c 65 63 74 20 79 6f 75 72 20 70 72 6f 67 72 61 lect your progra
60ff0 6d 27 73 20 6e 61 6d 65 2c 20 73 6f 20 74 68 61 m's name, so tha
61000 74 20 69 66 20 79 6f 75 72 20 70 72 6f 67 72 61 t if your progra
61010 6d 20 65 78 69 74 73 0a 2a 2a 20 70 72 65 6d 61 m exits.** prema
61020 74 75 72 65 6c 79 2c 20 6f 6c 64 20 74 65 6d 70 turely, old temp
61030 6f 72 61 72 79 20 66 69 6c 65 73 20 63 61 6e 20 orary files can
61040 62 65 20 65 61 73 69 6c 79 20 69 64 65 6e 74 69 be easily identi
61050 66 69 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62 fied. This can b
61060 65 20 64 6f 6e 65 0a 2a 2a 20 75 73 69 6e 67 20 e done.** using
61070 2d 44 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 -DSQLITE_TEMP_FI
61080 4c 45 5f 50 52 45 46 49 58 3d 6d 79 70 72 65 66 LE_PREFIX=mypref
61090 69 78 5f 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 ix_ on the compi
610a0 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 ler command line
610b0 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 36 2d 31 30 2d ..**.** 2006-10-
610c0 33 31 3a 20 20 54 68 65 20 64 65 66 61 75 6c 74 31: The default
610d0 20 70 72 65 66 69 78 20 75 73 65 64 20 74 6f 20 prefix used to
610e0 62 65 20 22 73 71 6c 69 74 65 5f 22 2e 20 20 42 be "sqlite_". B
610f0 75 74 20 74 68 65 6e 0a 2a 2a 20 4d 63 61 66 65 ut then.** Mcafe
61100 65 20 73 74 61 72 74 65 64 20 75 73 69 6e 67 20 e started using
61110 53 51 4c 69 74 65 20 69 6e 20 74 68 65 69 72 20 SQLite in their
61120 61 6e 74 69 2d 76 69 72 75 73 20 70 72 6f 64 75 anti-virus produ
61130 63 74 20 61 6e 64 20 69 74 0a 2a 2a 20 73 74 61 ct and it.** sta
61140 72 74 65 64 20 70 75 74 74 69 6e 67 20 66 69 6c rted putting fil
61150 65 73 20 77 69 74 68 20 74 68 65 20 22 73 71 6c es with the "sql
61160 69 74 65 22 20 6e 61 6d 65 20 69 6e 20 74 68 65 ite" name in the
61170 20 63 3a 2f 74 65 6d 70 20 66 6f 6c 64 65 72 2e c:/temp folder.
61180 0a 2a 2a 20 54 68 69 73 20 61 6e 6e 6f 79 65 64 .** This annoyed
61190 20 6d 61 6e 79 20 77 69 6e 64 6f 77 73 20 75 73 many windows us
611a0 65 72 73 2e 20 20 54 68 6f 73 65 20 75 73 65 72 ers. Those user
611b0 73 20 77 6f 75 6c 64 20 74 68 65 6e 20 64 6f 20 s would then do
611c0 61 20 0a 2a 2a 20 47 6f 6f 67 6c 65 20 73 65 61 a .** Google sea
611d0 72 63 68 20 66 6f 72 20 22 73 71 6c 69 74 65 22 rch for "sqlite"
611e0 2c 20 66 69 6e 64 20 74 68 65 20 74 65 6c 65 70 , find the telep
611f0 68 6f 6e 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 hone numbers of
61200 74 68 65 0a 2a 2a 20 64 65 76 65 6c 6f 70 65 72 the.** developer
61210 73 20 61 6e 64 20 63 61 6c 6c 20 74 6f 20 77 61 s and call to wa
61220 6b 65 20 74 68 65 6d 20 75 70 20 61 74 20 6e 69 ke them up at ni
61230 67 68 74 20 61 6e 64 20 63 6f 6d 70 6c 61 69 6e ght and complain
61240 2e 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 65 ..** For this re
61250 61 73 6f 6e 2c 20 74 68 65 20 64 65 66 61 75 6c ason, the defaul
61260 74 20 6e 61 6d 65 20 70 72 65 66 69 78 20 69 73 t name prefix is
61270 20 63 68 61 6e 67 65 64 20 74 6f 20 62 65 20 22 changed to be "
61280 73 71 6c 69 74 65 22 20 0a 2a 2a 20 73 70 65 6c sqlite" .** spel
61290 6c 65 64 20 62 61 63 6b 77 61 72 64 73 2e 20 20 led backwards.
612a0 53 6f 20 74 68 65 20 74 65 6d 70 20 66 69 6c 65 So the temp file
612b0 73 20 61 72 65 20 73 74 69 6c 6c 20 69 64 65 6e s are still iden
612c0 74 69 66 69 65 64 2c 20 62 75 74 0a 2a 2a 20 61 tified, but.** a
612d0 6e 79 62 6f 64 79 20 73 6d 61 72 74 20 65 6e 6f nybody smart eno
612e0 75 67 68 20 74 6f 20 66 69 67 75 72 65 20 6f 75 ugh to figure ou
612f0 74 20 74 68 65 20 63 6f 64 65 20 69 73 20 61 6c t the code is al
61300 73 6f 20 6c 69 6b 65 6c 79 20 73 6d 61 72 74 0a so likely smart.
61310 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f ** enough to kno
61320 77 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 74 w that calling t
61330 68 65 20 64 65 76 65 6c 6f 70 65 72 20 77 69 6c he developer wil
61340 6c 20 6e 6f 74 20 68 65 6c 70 20 67 65 74 20 72 l not help get r
61350 69 64 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c id.** of the fil
61360 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 e..*/.#ifndef SQ
61370 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 LITE_TEMP_FILE_P
61380 52 45 46 49 58 0a 23 20 64 65 66 69 6e 65 20 53 REFIX.# define S
61390 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f QLITE_TEMP_FILE_
613a0 50 52 45 46 49 58 20 22 65 74 69 6c 71 73 5f 22 PREFIX "etilqs_"
613b0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
613c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c he following val
613d0 75 65 73 20 6d 61 79 20 62 65 20 70 61 73 73 65 ues may be passe
613e0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
613f0 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73 argument to.** s
61400 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 qlite3OsLock().
61410 54 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b The various lock
61420 73 20 65 78 68 69 62 69 74 20 74 68 65 20 66 6f s exhibit the fo
61430 6c 6c 6f 77 69 6e 67 20 73 65 6d 61 6e 74 69 63 llowing semantic
61440 73 3a 0a 2a 2a 0a 2a 2a 20 53 48 41 52 45 44 3a s:.**.** SHARED:
61450 20 20 20 20 41 6e 79 20 6e 75 6d 62 65 72 20 6f Any number o
61460 66 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 f processes may
61470 68 6f 6c 64 20 61 20 53 48 41 52 45 44 20 6c 6f hold a SHARED lo
61480 63 6b 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c ck simultaneousl
61490 79 2e 0a 2a 2a 20 52 45 53 45 52 56 45 44 3a 20 y..** RESERVED:
614a0 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 A single proces
614b0 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 52 45 53 s may hold a RES
614c0 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 61 20 ERVED lock on a
614d0 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 file at.**
614e0 20 20 20 20 20 20 61 6e 79 20 74 69 6d 65 2e 20 any time.
614f0 4f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 Other processes
61500 6d 61 79 20 68 6f 6c 64 20 61 6e 64 20 6f 62 74 may hold and obt
61510 61 69 6e 20 6e 65 77 20 53 48 41 52 45 44 20 6c ain new SHARED l
61520 6f 63 6b 73 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 ocks..** PENDING
61530 3a 20 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f : A single pro
61540 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 cess may hold a
61550 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20 PENDING lock on
61560 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20 a file at.**
61570 20 20 20 20 20 20 20 20 61 6e 79 20 6f 6e 65 20 any one
61580 74 69 6d 65 2e 20 45 78 69 73 74 69 6e 67 20 53 time. Existing S
61590 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 HARED locks may
615a0 70 65 72 73 69 73 74 2c 20 62 75 74 20 6e 6f 20 persist, but no
615b0 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 new.**
615c0 20 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d SHARED locks m
615d0 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 ay be obtained b
615e0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 y other processe
615f0 73 2e 0a 2a 2a 20 45 58 43 4c 55 53 49 56 45 3a s..** EXCLUSIVE:
61600 20 41 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f An EXCLUSIVE lo
61610 63 6b 20 70 72 65 63 6c 75 64 65 73 20 61 6c 6c ck precludes all
61620 20 6f 74 68 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a other locks..**
61630 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b .** PENDING_LOCK
61640 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 61 73 73 may not be pass
61650 65 64 20 64 69 72 65 63 74 6c 79 20 74 6f 20 73 ed directly to s
61660 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 qlite3OsLock().
61670 49 6e 73 74 65 61 64 2c 20 61 0a 2a 2a 20 70 72 Instead, a.** pr
61680 6f 63 65 73 73 20 74 68 61 74 20 72 65 71 75 65 ocess that reque
61690 73 74 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 sts an EXCLUSIVE
616a0 20 6c 6f 63 6b 20 6d 61 79 20 61 63 74 75 61 6c lock may actual
616b0 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44 ly obtain a PEND
616c0 49 4e 47 0a 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69 ING.** lock. Thi
616d0 73 20 63 61 6e 20 62 65 20 75 70 67 72 61 64 65 s can be upgrade
616e0 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 d to an EXCLUSIV
616f0 45 20 6c 6f 63 6b 20 62 79 20 61 20 73 75 62 73 E lock by a subs
61700 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a equent call to.*
61710 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 * sqlite3OsLock(
61720 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4f )..*/.#define NO
61730 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 30 0a _LOCK 0.
61740 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f 4c #define SHARED_L
61750 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66 69 6e OCK 1.#defin
61760 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 e RESERVED_LOCK
61770 20 20 32 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 2.#define PEND
61780 49 4e 47 5f 4c 4f 43 4b 20 20 20 20 33 0a 23 64 ING_LOCK 3.#d
61790 65 66 69 6e 65 20 45 58 43 4c 55 53 49 56 45 5f efine EXCLUSIVE_
617a0 4c 4f 43 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 LOCK 4../*.** F
617b0 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65 ile Locking Note
617c0 73 3a 20 20 28 4d 6f 73 74 6c 79 20 61 62 6f 75 s: (Mostly abou
617d0 74 20 77 69 6e 64 6f 77 73 20 62 75 74 20 61 6c t windows but al
617e0 73 6f 20 73 6f 6d 65 20 69 6e 66 6f 20 66 6f 72 so some info for
617f0 20 55 6e 69 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20 Unix).**.** We
61800 63 61 6e 6e 6f 74 20 75 73 65 20 4c 6f 63 6b 46 cannot use LockF
61810 69 6c 65 45 78 28 29 20 6f 72 20 55 6e 6c 6f 63 ileEx() or Unloc
61820 6b 46 69 6c 65 45 78 28 29 20 6f 6e 20 57 69 6e kFileEx() on Win
61830 39 35 2f 39 38 2f 4d 45 20 62 65 63 61 75 73 65 95/98/ME because
61840 0a 2a 2a 20 74 68 6f 73 65 20 66 75 6e 63 74 69 .** those functi
61850 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 ons are not avai
61860 6c 61 62 6c 65 2e 20 20 53 6f 20 77 65 20 75 73 lable. So we us
61870 65 20 6f 6e 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 e only LockFile(
61880 29 20 61 6e 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 ) and.** UnlockF
61890 69 6c 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 ile()..**.** Loc
618a0 6b 46 69 6c 65 28 29 20 70 72 65 76 65 6e 74 73 kFile() prevents
618b0 20 6e 6f 74 20 6a 75 73 74 20 77 72 69 74 69 6e not just writin
618c0 67 20 62 75 74 20 61 6c 73 6f 20 72 65 61 64 69 g but also readi
618d0 6e 67 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63 ng by other proc
618e0 65 73 73 65 73 2e 0a 2a 2a 20 41 20 53 48 41 52 esses..** A SHAR
618f0 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 ED_LOCK is obtai
61900 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 ned by locking a
61910 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 single randomly
61920 2d 63 68 6f 73 65 6e 20 0a 2a 2a 20 62 79 74 65 -chosen .** byte
61930 20 6f 75 74 20 6f 66 20 61 20 73 70 65 63 69 66 out of a specif
61940 69 63 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 ic range of byte
61950 73 2e 20 54 68 65 20 6c 6f 63 6b 20 62 79 74 65 s. The lock byte
61960 20 69 73 20 6f 62 74 61 69 6e 65 64 20 61 74 20 is obtained at
61970 0a 2a 2a 20 72 61 6e 64 6f 6d 20 73 6f 20 74 77 .** random so tw
61980 6f 20 73 65 70 61 72 61 74 65 20 72 65 61 64 65 o separate reade
61990 72 73 20 63 61 6e 20 70 72 6f 62 61 62 6c 79 20 rs can probably
619a0 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 20 access the file
619b0 61 74 20 74 68 65 20 0a 2a 2a 20 73 61 6d 65 20 at the .** same
619c0 74 69 6d 65 2c 20 75 6e 6c 65 73 73 20 74 68 65 time, unless the
619d0 79 20 61 72 65 20 75 6e 6c 75 63 6b 79 20 61 6e y are unlucky an
619e0 64 20 63 68 6f 6f 73 65 20 74 68 65 20 73 61 6d d choose the sam
619f0 65 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 e lock byte..**
61a00 41 6e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 An EXCLUSIVE_LOC
61a10 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 K is obtained by
61a20 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 62 79 74 locking all byt
61a30 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2e es in the range.
61a40 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e .** There can on
61a50 6c 79 20 62 65 20 6f 6e 65 20 77 72 69 74 65 72 ly be one writer
61a60 2e 20 20 41 20 52 45 53 45 52 56 45 44 5f 4c 4f . A RESERVED_LO
61a70 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 CK is obtained b
61a80 79 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 61 20 73 y locking.** a s
61a90 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 20 74 68 ingle byte of th
61aa0 65 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 64 e file that is d
61ab0 65 73 69 67 6e 61 74 65 64 20 61 73 20 74 68 65 esignated as the
61ac0 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 62 reserved lock b
61ad0 79 74 65 2e 0a 2a 2a 20 41 20 50 45 4e 44 49 4e yte..** A PENDIN
61ae0 47 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e G_LOCK is obtain
61af0 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 ed by locking a
61b00 64 65 73 69 67 6e 61 74 65 64 20 62 79 74 65 20 designated byte
61b10 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a different from.*
61b20 2a 20 74 68 65 20 52 45 53 45 52 56 45 44 5f 4c * the RESERVED_L
61b30 4f 43 4b 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 OCK byte..**.**
61b40 4f 6e 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 On WinNT/2K/XP s
61b50 79 73 74 65 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 ystems, LockFile
61b60 45 78 28 29 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 Ex() and UnlockF
61b70 69 6c 65 45 78 28 29 20 61 72 65 20 61 76 61 69 ileEx() are avai
61b80 6c 61 62 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 lable,.** which
61b90 6d 65 61 6e 73 20 77 65 20 63 61 6e 20 75 73 65 means we can use
61ba0 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 20 6c reader/writer l
61bb0 6f 63 6b 73 2e 20 20 57 68 65 6e 20 72 65 61 64 ocks. When read
61bc0 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73 0a er/writer locks.
61bd0 2a 2a 20 61 72 65 20 75 73 65 64 2c 20 74 68 65 ** are used, the
61be0 20 6c 6f 63 6b 20 69 73 20 70 6c 61 63 65 64 20 lock is placed
61bf0 6f 6e 20 74 68 65 20 73 61 6d 65 20 72 61 6e 67 on the same rang
61c00 65 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 e of bytes that
61c10 69 73 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 70 is used.** for p
61c20 72 6f 62 61 62 69 6c 69 73 74 69 63 20 6c 6f 63 robabilistic loc
61c30 6b 69 6e 67 20 69 6e 20 57 69 6e 39 35 2f 39 38 king in Win95/98
61c40 2f 4d 45 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 /ME. Hence, the
61c50 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 0a locking scheme.
61c60 2a 2a 20 77 69 6c 6c 20 73 75 70 70 6f 72 74 20 ** will support
61c70 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 39 two or more Win9
61c80 35 20 72 65 61 64 65 72 73 20 6f 72 20 74 77 6f 5 readers or two
61c90 20 6f 72 20 6d 6f 72 65 20 57 69 6e 4e 54 20 72 or more WinNT r
61ca0 65 61 64 65 72 73 2e 0a 2a 2a 20 42 75 74 20 61 eaders..** But a
61cb0 20 73 69 6e 67 6c 65 20 57 69 6e 39 35 20 72 65 single Win95 re
61cc0 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f ader will lock o
61cd0 75 74 20 61 6c 6c 20 57 69 6e 4e 54 20 72 65 61 ut all WinNT rea
61ce0 64 65 72 73 20 61 6e 64 20 61 20 73 69 6e 67 6c ders and a singl
61cf0 65 0a 2a 2a 20 57 69 6e 4e 54 20 72 65 61 64 65 e.** WinNT reade
61d00 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 r will lock out
61d10 61 6c 6c 20 6f 74 68 65 72 20 57 69 6e 39 35 20 all other Win95
61d20 72 65 61 64 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 readers..**.** T
61d30 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 23 64 65 he following #de
61d40 66 69 6e 65 73 20 73 70 65 63 69 66 79 20 74 68 fines specify th
61d50 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 e range of bytes
61d60 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e used for lockin
61d70 67 2e 0a 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a g..** SHARED_SIZ
61d80 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 E is the number
61d90 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c 61 62 of bytes availab
61da0 6c 65 20 69 6e 20 74 68 65 20 70 6f 6f 6c 20 66 le in the pool f
61db0 72 6f 6d 20 77 68 69 63 68 0a 2a 2a 20 61 20 72 rom which.** a r
61dc0 61 6e 64 6f 6d 20 62 79 74 65 20 69 73 20 73 65 andom byte is se
61dd0 6c 65 63 74 65 64 20 66 6f 72 20 61 20 73 68 61 lected for a sha
61de0 72 65 64 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 red lock. The p
61df0 6f 6f 6c 20 6f 66 20 62 79 74 65 73 20 66 6f 72 ool of bytes for
61e00 0a 2a 2a 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 .** shared locks
61e10 20 62 65 67 69 6e 73 20 61 74 20 53 48 41 52 45 begins at SHARE
61e20 44 5f 46 49 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 D_FIRST. .**.**
61e30 54 68 65 73 65 20 23 64 65 66 69 6e 65 73 20 61 These #defines a
61e40 72 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 re available in
61e50 73 71 6c 69 74 65 5f 61 75 78 2e 68 20 73 6f 20 sqlite_aux.h so
61e60 74 68 61 74 20 61 64 61 70 74 6f 72 73 20 66 6f that adaptors fo
61e70 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6e 67 20 r.** connecting
61e80 53 51 4c 69 74 65 20 74 6f 20 6f 74 68 65 72 20 SQLite to other
61e90 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
61ea0 73 20 63 61 6e 20 75 73 65 20 74 68 65 20 73 61 s can use the sa
61eb0 6d 65 20 62 79 74 65 0a 2a 2a 20 72 61 6e 67 65 me byte.** range
61ec0 73 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 s for locking.
61ed0 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c 20 74 In particular, t
61ee0 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 he same locking
61ef0 73 74 72 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20 strategy and.**
61f00 62 79 74 65 20 72 61 6e 67 65 73 20 61 72 65 20 byte ranges are
61f10 75 73 65 64 20 66 6f 72 20 55 6e 69 78 2e 20 20 used for Unix.
61f20 54 68 69 73 20 6c 65 61 76 65 73 20 6f 70 65 6e This leaves open
61f30 20 74 68 65 20 70 6f 73 73 69 62 6c 69 74 79 20 the possiblity
61f40 6f 66 20 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 of having.** cli
61f50 65 6e 74 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77 ents on win95, w
61f60 69 6e 4e 54 2c 20 61 6e 64 20 75 6e 69 78 20 61 inNT, and unix a
61f70 6c 6c 20 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68 ll talking to th
61f80 65 20 73 61 6d 65 20 73 68 61 72 65 64 20 66 69 e same shared fi
61f90 6c 65 0a 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f le.** and all lo
61fa0 63 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e cking correctly.
61fb0 20 20 54 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 To do so would
61fc0 20 72 65 71 75 69 72 65 20 74 68 61 74 20 73 61 require that sa
61fd0 6d 62 61 20 28 6f 72 20 77 68 61 74 65 76 65 72 mba (or whatever
61fe0 0a 2a 2a 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e .** tool is bein
61ff0 67 20 75 73 65 64 20 66 6f 72 20 66 69 6c 65 20 g used for file
62000 73 68 61 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 sharing) impleme
62010 6e 74 73 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 nts locks correc
62020 74 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77 tly between.** w
62030 69 6e 64 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e indows and unix.
62040 20 20 49 27 6d 20 67 75 65 73 73 69 6e 67 20 74 I'm guessing t
62050 68 61 74 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 hat isn't likely
62060 20 74 6f 20 68 61 70 70 65 6e 2c 20 62 75 74 20 to happen, but
62070 62 79 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 by.** using the
62080 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e same locking ran
62090 67 65 20 77 65 20 61 72 65 20 61 74 20 6c 65 61 ge we are at lea
620a0 73 74 20 6f 70 65 6e 20 74 6f 20 74 68 65 20 70 st open to the p
620b0 6f 73 73 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a ossibility..**.*
620c0 2a 20 4c 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e * Locking in win
620d0 64 6f 77 73 20 69 73 20 6d 61 6e 64 69 74 6f 72 dows is manditor
620e0 79 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 y. For this rea
620f0 73 6f 6e 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73 son, we cannot s
62100 74 6f 72 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64 tore.** actual d
62110 61 74 61 20 69 6e 20 74 68 65 20 62 79 74 65 73 ata in the bytes
62120 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e used for lockin
62130 67 2e 20 20 54 68 65 20 70 61 67 65 72 20 6e 65 g. The pager ne
62140 76 65 72 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a ver allocates.**
62150 20 74 68 65 20 70 61 67 65 73 20 69 6e 76 6f 6c the pages invol
62160 76 65 64 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 ved in locking t
62170 68 65 72 65 66 6f 72 65 2e 20 20 53 48 41 52 45 herefore. SHARE
62180 44 5f 53 49 5a 45 20 69 73 20 73 65 6c 65 63 74 D_SIZE is select
62190 65 64 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c ed so.** that al
621a0 6c 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74 l locks will fit
621b0 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 on a single pag
621c0 65 20 65 76 65 6e 20 61 74 20 74 68 65 20 6d 69 e even at the mi
621d0 6e 69 6d 75 6d 20 70 61 67 65 20 73 69 7a 65 2e nimum page size.
621e0 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 .** PENDING_BYTE
621f0 20 64 65 66 69 6e 65 73 20 74 68 65 20 62 65 67 defines the beg
62200 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f inning of the lo
62210 63 6b 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74 cks. By default
62220 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a PENDING_BYTE.**
62230 20 69 73 20 73 65 74 20 68 69 67 68 20 73 6f 20 is set high so
62240 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 68 61 that we don't ha
62250 76 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 ve to allocate a
62260 6e 20 75 6e 75 73 65 64 20 70 61 67 65 20 65 78 n unused page ex
62270 63 65 70 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79 cept.** for very
62280 20 6c 61 72 67 65 20 64 61 74 61 62 61 73 65 73 large databases
62290 2e 20 20 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c . But one shoul
622a0 64 20 74 65 73 74 20 74 68 65 20 70 61 67 65 20 d test the page
622b0 73 6b 69 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a skipping logic .
622c0 2a 2a 20 62 79 20 73 65 74 74 69 6e 67 20 50 45 ** by setting PE
622d0 4e 44 49 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61 NDING_BYTE low a
622e0 6e 64 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 65 nd running the e
622f0 6e 74 69 72 65 20 72 65 67 72 65 73 73 69 6f 6e ntire regression
62300 20 73 75 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 suite..**.** Ch
62310 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65 anging the value
62320 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 of PENDING_BYTE
62330 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 73 75 results in a su
62340 62 74 6c 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c btly incompatibl
62350 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74 e.** file format
62360 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 . Depending on
62370 68 6f 77 20 69 74 20 69 73 20 63 68 61 6e 67 65 how it is change
62380 64 2c 20 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74 d, you might not
62390 20 6e 6f 74 69 63 65 0a 2a 2a 20 74 68 65 20 69 notice.** the i
623a0 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 72 ncompatibility r
623b0 69 67 68 74 20 61 77 61 79 2c 20 65 76 65 6e 20 ight away, even
623c0 72 75 6e 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 running a full r
623d0 65 67 72 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a egression test..
623e0 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c ** The default l
623f0 6f 63 61 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 ocation of PENDI
62400 4e 47 5f 42 59 54 45 20 69 73 20 74 68 65 20 66 NG_BYTE is the f
62410 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 irst byte past t
62420 68 65 0a 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 he.** 1GB bounda
62430 72 79 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65 ry..**.*/.#ifnde
62440 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 23 64 f SQLITE_TEST.#d
62450 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 efine PENDING_BY
62460 54 45 20 20 20 20 20 20 30 78 34 30 30 30 30 30 TE 0x400000
62470 30 30 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 00 /* First byt
62480 65 20 70 61 73 74 20 74 68 65 20 31 47 42 20 62 e past the 1GB b
62490 6f 75 6e 64 61 72 79 20 2a 2f 0a 23 65 6c 73 65 oundary */.#else
624a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 .SQLITE_API exte
624b0 72 6e 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 rn unsigned int
624c0 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f sqlite3_pending_
624d0 62 79 74 65 3b 0a 23 64 65 66 69 6e 65 20 50 45 byte;.#define PE
624e0 4e 44 49 4e 47 5f 42 59 54 45 20 73 71 6c 69 74 NDING_BYTE sqlit
624f0 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74 65 0a e3_pending_byte.
62500 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 #endif..#define
62510 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 20 20 RESERVED_BYTE
62520 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b (PENDING_BYTE+
62530 31 29 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 1).#define SHARE
62540 44 5f 46 49 52 53 54 20 20 20 20 20 20 28 50 45 D_FIRST (PE
62550 4e 44 49 4e 47 5f 42 59 54 45 2b 32 29 0a 23 64 NDING_BYTE+2).#d
62560 65 66 69 6e 65 20 53 48 41 52 45 44 5f 53 49 5a efine SHARED_SIZ
62570 45 20 20 20 20 20 20 20 35 31 30 0a 0a 2f 2a 20 E 510../*
62580 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f .** Functions fo
62590 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69 r accessing sqli
625a0 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 te3_file methods
625b0 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
625c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
625d0 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 sClose(sqlite3_f
625e0 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ile*);.SQLITE_PR
625f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
62600 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f 3OsRead(sqlite3_
62610 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e file*, void*, in
62620 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 t amt, i64 offse
62630 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
62640 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
62650 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 Write(sqlite3_fi
62660 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a le*, const void*
62670 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f , int amt, i64 o
62680 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 ffset);.SQLITE_P
62690 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
626a0 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c e3OsTruncate(sql
626b0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 ite3_file*, i64
626c0 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 size);.SQLITE_PR
626d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
626e0 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 3OsSync(sqlite3_
626f0 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c file*, int);.SQL
62700 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
62710 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
62720 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c e(sqlite3_file*,
62730 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 i64 *pSize);.SQ
62740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
62750 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 sqlite3OsLock(s
62760 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
62770 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
62780 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
62790 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
627a0 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ile*, int);.SQLI
627b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
627c0 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 qlite3OsCheckRes
627d0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
627e0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
627f0 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 *pResOut);.SQLIT
62800 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
62810 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 lite3OsFileContr
62820 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a ol(sqlite3_file*
62830 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 ,int,void*);.#de
62840 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 fine SQLITE_FCNT
62850 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30 L_DB_UNCHANGED 0
62860 78 63 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45 xca093fa0.SQLITE
62870 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
62880 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 ite3OsSectorSize
62890 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
628a0 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 d);.SQLITE_PRIVA
628b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
628c0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
628d0 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 stics(sqlite3_fi
628e0 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a le *id);../* .**
628f0 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 Functions for a
62900 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 ccessing sqlite3
62910 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f _vfs methods .*/
62920 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
62930 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 int sqlite3OsOpe
62940 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c n(sqlite3_vfs *,
62950 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 const char *, s
62960 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
62970 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 t, int *);.SQLIT
62980 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
62990 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 lite3OsDelete(sq
629a0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
629b0 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b st char *, int);
629c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
629d0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 int sqlite3OsAcc
629e0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ess(sqlite3_vfs
629f0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c *, const char *,
62a00 20 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f int, int *pResO
62a10 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ut);.SQLITE_PRIV
62a20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
62a30 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 sFullPathname(sq
62a40 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
62a50 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 st char *, int,
62a60 63 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66 char *);.#ifndef
62a70 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
62a80 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 D_EXTENSION.SQLI
62a90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
62aa0 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e *sqlite3OsDlOpen
62ab0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 (sqlite3_vfs *,
62ac0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 const char *);.S
62ad0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
62ae0 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 id sqlite3OsDlEr
62af0 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ror(sqlite3_vfs
62b00 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b *, int, char *);
62b10 0a 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f .void (*sqlite3O
62b20 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 sDlSym(sqlite3_v
62b30 66 73 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f fs *, void *, co
62b40 6e 73 74 20 63 68 61 72 20 2a 29 29 28 76 6f 69 nst char *))(voi
62b50 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 d);.SQLITE_PRIVA
62b60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f TE void sqlite3O
62b70 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 sDlClose(sqlite3
62b80 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b _vfs *, void *);
62b90 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
62ba0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
62bb0 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f NSION */.SQLITE_
62bc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
62bd0 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 te3OsRandomness(
62be0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 sqlite3_vfs *, i
62bf0 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nt, char *);.SQL
62c00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
62c10 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 sqlite3OsSleep(s
62c20 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e qlite3_vfs *, in
62c30 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
62c40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
62c50 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 CurrentTime(sqli
62c60 74 65 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c te3_vfs *, doubl
62c70 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 e*);../*.** Conv
62c80 65 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e enience function
62c90 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e s for opening an
62ca0 64 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 d closing files
62cb0 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 using .** sqlite
62cc0 33 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 3_malloc() to ob
62cd0 74 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74 tain space for t
62ce0 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 he file-handle s
62cf0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c tructure..*/.SQL
62d00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
62d10 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c sqlite3OsOpenMal
62d20 6c 6f 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20 loc(sqlite3_vfs
62d30 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c *, const char *,
62d40 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a sqlite3_file **
62d50 2c 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c , int,int*);.SQL
62d60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
62d70 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 sqlite3OsCloseFr
62d80 65 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ee(sqlite3_file
62d90 2a 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f *);..#endif /* _
62da0 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a SQLITE_OS_H_ */.
62db0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
62dc0 20 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a End of os.h ***
62dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
62de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
62df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
62e00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
62e10 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
62e20 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
62e30 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
62e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
62e50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
62e60 20 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 Include mutex.h
62e70 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
62e80 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a f sqliteInt.h **
62e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
62ea0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
62eb0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 Begin file mute
62ec0 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.h ************
62ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
62ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
62ef0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
62f00 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 28.**.** The
62f10 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
62f20 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
62f30 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
62f40 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
62f50 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
62f60 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
62f70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
62f80 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
62f90 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
62fa0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
62fb0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
62fc0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
62fd0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
62fe0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
62ff0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
63000 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
63010 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
63020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
63030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
63040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
63050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
63060 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
63070 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 file contains th
63080 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 e common header
63090 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d for all mutex im
630a0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a plementations..*
630b0 2a 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e * The sqliteInt.
630c0 68 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64 h header #includ
630d0 65 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20 es this file so
630e0 74 68 61 74 20 69 74 20 69 73 20 61 76 61 69 6c that it is avail
630f0 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 able.** to all s
63100 6f 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65 ource files. We
63110 20 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e break it out in
63120 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 an effort to ke
63130 65 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 ep the code.** b
63140 65 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e etter organized.
63150 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f .**.** NOTE: so
63160 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c urce files shoul
63170 64 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 d *not* #include
63180 20 74 68 69 73 20 68 65 61 64 65 72 20 66 69 6c this header fil
63190 65 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 e directly..** S
631a0 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 ource files shou
631b0 6c 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20 ld #include the
631c0 73 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 sqliteInt.h file
631d0 20 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69 and let that fi
631e0 6c 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 le.** include th
631f0 69 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c is one indirectl
63200 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 y..**.** $Id: mu
63210 74 65 78 2e 68 2c 76 20 31 2e 39 20 32 30 30 38 tex.h,v 1.9 2008
63220 2f 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 /10/07 15:25:48
63230 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f drh Exp $.*/.../
63240 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 *.** Figure out
63250 77 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 what version of
63260 74 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e the code to use.
63270 20 20 54 68 65 20 63 68 6f 69 63 65 73 20 61 72 The choices ar
63280 65 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 e.**.** SQLITE
63290 5f 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 _MUTEX_OMIT
632a0 20 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 No mutex log
632b0 69 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 ic. Not even st
632c0 75 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 ubs. The.**
632d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
632e0 20 20 20 20 20 20 20 20 20 6d 75 74 65 78 65 73 mutexes
632f0 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 implemention ca
63300 6e 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64 nnot be overridd
63310 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 en.**
63320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63330 20 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e at start-time.
63340 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f .**.** SQLITE_
63350 4d 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 MUTEX_NOOP
63360 20 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 For single-th
63370 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 readed applicati
63380 6f 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 ons. No.**
63390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
633a0 20 20 20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 mutual e
633b0 78 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 xclusion is prov
633c0 69 64 65 64 2e 20 20 42 75 74 20 74 68 69 73 0a ided. But this.
633d0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
633e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
633f0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 mplementation ca
63400 6e 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 n be overridden
63410 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 at.**
63420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63430 20 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a start-time..**
63440 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 .** SQLITE_MUT
63450 45 58 5f 50 54 48 52 45 41 44 53 20 20 20 20 20 EX_PTHREADS
63460 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 For multi-thread
63470 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 ed applications
63480 6f 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 on Unix..**.**
63490 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
634a0 32 20 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 2 For m
634b0 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 ulti-threaded ap
634c0 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 plications on Wi
634d0 6e 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c n32..**.** SQL
634e0 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 ITE_MUTEX_OS2
634f0 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 For multi
63500 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 -threaded applic
63510 61 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a ations on OS/2..
63520 2a 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 54 */.#if !SQLITE_T
63530 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 HREADSAFE.# defi
63540 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ne SQLITE_MUTEX_
63550 4f 4d 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 OMIT.#endif.#if
63560 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
63570 45 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 E && !defined(SQ
63580 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 LITE_MUTEX_NOOP)
63590 0a 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 .# if SQLITE_OS
635a0 5f 55 4e 49 58 0a 23 20 20 20 20 64 65 66 69 6e _UNIX.# defin
635b0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 e SQLITE_MUTEX_P
635c0 54 48 52 45 41 44 53 0a 23 20 20 65 6c 69 66 20 THREADS.# elif
635d0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 SQLITE_OS_WIN.#
635e0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
635f0 5f 4d 55 54 45 58 5f 57 33 32 0a 23 20 20 65 6c _MUTEX_W32.# el
63600 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 if SQLITE_OS_OS2
63610 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c .# define SQL
63620 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 ITE_MUTEX_OS2.#
63630 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e else.# defin
63640 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e e SQLITE_MUTEX_N
63650 4f 4f 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e OOP.# endif.#en
63660 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
63670 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a TE_MUTEX_OMIT./*
63680 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 .** If this is a
63690 20 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 no-op implement
636a0 61 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 ation, implement
636b0 20 65 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d everything as m
636c0 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e acros..*/.#defin
636d0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
636e0 61 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71 alloc(X) ((sq
636f0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a lite3_mutex*)8).
63700 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f #define sqlite3_
63710 6d 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64 mutex_free(X).#d
63720 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 efine sqlite3_mu
63730 74 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 tex_enter(X).#de
63740 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 fine sqlite3_mut
63750 65 78 5f 74 72 79 28 58 29 20 20 20 20 20 20 53 ex_try(X) S
63760 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 QLITE_OK.#define
63770 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
63780 65 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20 eave(X).#define
63790 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
637a0 6c 64 28 58 29 20 20 20 20 20 31 0a 23 64 65 66 ld(X) 1.#def
637b0 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ine sqlite3_mute
637c0 78 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a x_notheld(X) 1.
637d0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d #define sqlite3M
637e0 75 74 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 utexAlloc(X)
637f0 20 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ((sqlite3_mute
63800 78 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 x*)8).#define sq
63810 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 lite3MutexInit()
63820 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f SQLITE_O
63830 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 K.#define sqlite
63840 33 4d 75 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 3MutexEnd().#end
63850 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 if /* defined(SQ
63860 4c 49 54 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 29 LITE_OMIT_MUTEX)
63870 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
63880 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 **** End of mute
63890 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.h ************
638a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
638b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
638c0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
638d0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
638e0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
638f0 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 f in sqliteInt.h
63900 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
63910 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 ***/.../*.** Eac
63920 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 h database file
63930 74 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 62 to be accessed b
63940 79 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 y the system is
63950 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f an instance.** o
63960 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
63970 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 structure. Ther
63980 65 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 e are normally t
63990 77 6f 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 wo of these stru
639a0 63 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 ctures.** in the
639b0 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 sqlite.aDb[] ar
639c0 72 61 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 ray. aDb[0] is
639d0 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
639e0 65 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 e file and.** aD
639f0 62 5b 31 5d 20 69 73 20 74 68 65 20 64 61 74 61 b[1] is the data
63a00 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 base file used t
63a10 6f 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 o hold temporary
63a20 20 74 61 62 6c 65 73 2e 20 20 41 64 64 69 74 69 tables. Additi
63a30 6f 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 onal.** database
63a40 73 20 6d 61 79 20 62 65 20 61 74 74 61 63 68 65 s may be attache
63a50 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 d..*/.struct Db
63a60 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b {. char *zName;
63a70 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
63a80 20 6f 66 20 74 68 69 73 20 64 61 74 61 62 61 73 of this databas
63a90 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 e */. Btree *pB
63aa0 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 t; /* T
63ab0 68 65 20 42 2a 54 72 65 65 20 73 74 72 75 63 74 he B*Tree struct
63ac0 75 72 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 ure for this dat
63ad0 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
63ae0 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 u8 inTrans;
63af0 20 20 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 /* 0: not w
63b00 72 69 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 ritable. 1: Tra
63b10 6e 73 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 nsaction. 2: Ch
63b20 65 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 eckpoint */. u8
63b30 20 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 safety_level;
63b40 20 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 /* How aggres
63b50 73 69 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20 sive at syncing
63b60 64 61 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a data to disk */.
63b70 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 void *pAux;
63b80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
63b90 75 78 69 6c 69 61 72 79 20 64 61 74 61 2e 20 20 uxiliary data.
63ba0 55 73 75 61 6c 6c 79 20 4e 55 4c 4c 20 2a 2f 0a Usually NULL */.
63bb0 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 41 75 void (*xFreeAu
63bc0 78 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 52 x)(void*); /* R
63bd0 6f 75 74 69 6e 65 20 74 6f 20 66 72 65 65 20 70 outine to free p
63be0 41 75 78 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 Aux */. Schema
63bf0 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a *pSchema; /*
63c00 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 Pointer to data
63c10 62 61 73 65 20 73 63 68 65 6d 61 20 28 70 6f 73 base schema (pos
63c20 73 69 62 6c 79 20 73 68 61 72 65 64 29 20 2a 2f sibly shared) */
63c30 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e .};../*.** An in
63c40 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
63c50 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
63c60 65 20 73 74 6f 72 65 73 20 61 20 64 61 74 61 62 e stores a datab
63c70 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a ase schema..**.*
63c80 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e * If there are n
63c90 6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 o virtual tables
63ca0 20 63 6f 6e 66 69 67 75 72 65 64 20 69 6e 20 74 configured in t
63cb0 68 69 73 20 73 63 68 65 6d 61 2c 20 74 68 65 0a his schema, the.
63cc0 2a 2a 20 53 63 68 65 6d 61 2e 64 62 20 76 61 72 ** Schema.db var
63cd0 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 iable is set to
63ce0 4e 55 4c 4c 2e 20 41 66 74 65 72 20 74 68 65 20 NULL. After the
63cf0 66 69 72 73 74 20 76 69 72 74 75 61 6c 20 74 61 first virtual ta
63d00 62 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 ble.** has been
63d10 61 64 64 65 64 2c 20 69 74 20 69 73 20 73 65 74 added, it is set
63d20 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 to point to the
63d30 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
63d40 74 69 6f 6e 20 0a 2a 2a 20 75 73 65 64 20 74 6f tion .** used to
63d50 20 63 72 65 61 74 65 20 74 68 65 20 63 6f 6e 6e create the conn
63d60 65 63 74 69 6f 6e 2e 20 4f 6e 63 65 20 61 20 76 ection. Once a v
63d70 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 73 irtual table has
63d80 20 62 65 65 6e 0a 2a 2a 20 61 64 64 65 64 20 74 been.** added t
63d90 6f 20 74 68 65 20 53 63 68 65 6d 61 20 73 74 72 o the Schema str
63da0 75 63 74 75 72 65 20 61 6e 64 20 74 68 65 20 53 ucture and the S
63db0 63 68 65 6d 61 2e 64 62 20 76 61 72 69 61 62 6c chema.db variabl
63dc0 65 20 70 6f 70 75 6c 61 74 65 64 2c 20 0a 2a 2a e populated, .**
63dd0 20 6f 6e 6c 79 20 74 68 61 74 20 64 61 74 61 62 only that datab
63de0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d ase connection m
63df0 61 79 20 75 73 65 20 74 68 65 20 53 63 68 65 6d ay use the Schem
63e00 61 20 74 6f 20 70 72 65 70 61 72 65 20 0a 2a 2a a to prepare .**
63e10 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a statements..*/.
63e20 73 74 72 75 63 74 20 53 63 68 65 6d 61 20 7b 0a struct Schema {.
63e30 20 20 69 6e 74 20 73 63 68 65 6d 61 5f 63 6f 6f int schema_coo
63e40 6b 69 65 3b 20 20 20 2f 2a 20 44 61 74 61 62 61 kie; /* Databa
63e50 73 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f se schema versio
63e60 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 n number for thi
63e70 73 20 66 69 6c 65 20 2a 2f 0a 20 20 48 61 73 68 s file */. Hash
63e80 20 74 62 6c 48 61 73 68 3b 20 20 20 20 20 20 20 tblHash;
63e90 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 20 69 /* All tables i
63ea0 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20 2a ndexed by name *
63eb0 2f 0a 20 20 48 61 73 68 20 69 64 78 48 61 73 68 /. Hash idxHash
63ec0 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 ; /* All
63ed0 28 6e 61 6d 65 64 29 20 69 6e 64 69 63 65 73 20 (named) indices
63ee0 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20 indexed by name
63ef0 2a 2f 0a 20 20 48 61 73 68 20 74 72 69 67 48 61 */. Hash trigHa
63f00 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c sh; /* All
63f10 20 74 72 69 67 67 65 72 73 20 69 6e 64 65 78 65 triggers indexe
63f20 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 d by name */. H
63f30 61 73 68 20 61 46 4b 65 79 3b 20 20 20 20 20 20 ash aFKey;
63f40 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b /* Foreign k
63f50 65 79 73 20 69 6e 64 65 78 65 64 20 62 79 20 74 eys indexed by t
63f60 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 o-table */. Tab
63f70 6c 65 20 2a 70 53 65 71 54 61 62 3b 20 20 20 20 le *pSeqTab;
63f80 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f /* The sqlite_
63f90 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75 sequence table u
63fa0 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 52 45 sed by AUTOINCRE
63fb0 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c MENT */. u8 fil
63fc0 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f e_format; /
63fd0 2a 20 53 63 68 65 6d 61 20 66 6f 72 6d 61 74 20 * Schema format
63fe0 76 65 72 73 69 6f 6e 20 66 6f 72 20 74 68 69 73 version for this
63ff0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e file */. u8 en
64000 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
64010 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 /* Text encoding
64020 20 75 73 65 64 20 62 79 20 74 68 69 73 20 64 61 used by this da
64030 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 31 36 20 tabase */. u16
64040 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
64050 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 69 /* Flags associ
64060 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 73 ated with this s
64070 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63 chema */. int c
64080 61 63 68 65 5f 73 69 7a 65 3b 20 20 20 20 20 20 ache_size;
64090 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
640a0 65 73 20 74 6f 20 75 73 65 20 69 6e 20 74 68 65 es to use in the
640b0 20 63 61 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65 cache */.#ifnde
640c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
640d0 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c RTUALTABLE. sql
640e0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
640f0 20 20 2f 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e /* "Owner" con
64100 6e 65 63 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d nection. See com
64110 6d 65 6e 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65 ment above */.#e
64120 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 ndif.};../*.** T
64130 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 hese macros can
64140 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c be used to test,
64150 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 set, or clear b
64160 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44 its in the .** D
64170 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a b.flags field..*
64180 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73 50 /.#define DbHasP
64190 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 roperty(D,I,P)
641a0 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d (((D)->aDb[I]
641b0 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 .pSchema->flags&
641c0 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 (P))==(P)).#defi
641d0 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f 70 65 ne DbHasAnyPrope
641e0 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28 28 44 rty(D,I,P) (((D
641f0 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d )->aDb[I].pSchem
64200 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 a->flags&(P))!=0
64210 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65 74 50 ).#define DbSetP
64220 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 roperty(D,I,P)
64230 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 (D)->aDb[I].p
64240 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28 Schema->flags|=(
64250 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43 6c 65 P).#define DbCle
64260 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 arProperty(D,I,P
64270 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e ) (D)->aDb[I].
64280 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d pSchema->flags&=
64290 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f ~(P)../*.** Allo
642a0 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 wed values for t
642b0 68 65 20 44 42 2e 66 6c 61 67 73 20 66 69 65 6c he DB.flags fiel
642c0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f d..**.** The DB_
642d0 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61 SchemaLoaded fla
642e0 67 20 69 73 20 73 65 74 20 61 66 74 65 72 20 74 g is set after t
642f0 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 he database sche
64300 6d 61 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72 ma has been.** r
64310 65 61 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 ead into interna
64320 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a l hash tables..*
64330 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65 73 65 74 56 *.** DB_UnresetV
64340 69 65 77 73 20 6d 65 61 6e 73 20 74 68 61 74 20 iews means that
64350 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 one or more view
64360 73 20 68 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 s have column na
64370 6d 65 73 20 74 68 61 74 0a 2a 2a 20 68 61 76 65 mes that.** have
64380 20 62 65 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74 been filled out
64390 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 . If the schema
643a0 20 63 68 61 6e 67 65 73 2c 20 74 68 65 73 65 20 changes, these
643b0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67 column names mig
643c0 68 74 0a 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e ht.** changes an
643d0 64 20 73 6f 20 74 68 65 20 76 69 65 77 20 77 69 d so the view wi
643e0 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 ll need to be re
643f0 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 set..*/.#define
64400 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 DB_SchemaLoaded
64410 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 54 68 0x0001 /* Th
64420 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65 65 e schema has bee
64430 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66 n loaded */.#def
64440 69 6e 65 20 44 42 5f 55 6e 72 65 73 65 74 56 69 ine DB_UnresetVi
64450 65 77 73 20 20 20 20 30 78 30 30 30 32 20 20 2f ews 0x0002 /
64460 2a 20 53 6f 6d 65 20 76 69 65 77 73 20 68 61 76 * Some views hav
64470 65 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e e defined column
64480 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e names */.#defin
64490 65 20 44 42 5f 45 6d 70 74 79 20 20 20 20 20 20 e DB_Empty
644a0 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 0x0004 /*
644b0 54 68 65 20 66 69 6c 65 20 69 73 20 65 6d 70 74 The file is empt
644c0 79 20 28 6c 65 6e 67 74 68 20 30 20 62 79 74 65 y (length 0 byte
644d0 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 s) */../*.** The
644e0 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 number of diffe
644f0 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74 68 rent kinds of th
64500 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20 62 65 ings that can be
64510 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 75 73 69 6e limited.** usin
64520 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 g the sqlite3_li
64530 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e mit() interface.
64540 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
64550 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 4c 49 TE_N_LIMIT (SQLI
64560 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c TE_LIMIT_VARIABL
64570 45 5f 4e 55 4d 42 45 52 2b 31 29 0a 0a 2f 2a 0a E_NUMBER+1)../*.
64580 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c ** Lookaside mal
64590 6c 6f 63 20 69 73 20 61 20 73 65 74 20 6f 66 20 loc is a set of
645a0 66 69 78 65 64 2d 73 69 7a 65 20 62 75 66 66 65 fixed-size buffe
645b0 72 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 rs that can be u
645c0 73 65 64 0a 2a 2a 20 74 6f 20 73 61 74 69 73 66 sed.** to satisf
645d0 79 20 73 6d 61 6c 6c 20 74 72 61 6e 73 69 65 6e y small transien
645e0 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 t memory allocat
645f0 69 6f 6e 20 72 65 71 75 65 73 74 73 20 66 6f 72 ion requests for
64600 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 61 73 73 6f objects.** asso
64610 63 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61 ciated with a pa
64620 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 rticular databas
64630 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 e connection. T
64640 68 65 20 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f he use of.** loo
64650 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 70 72 kaside malloc pr
64660 6f 76 69 64 65 73 20 61 20 73 69 67 6e 69 66 69 ovides a signifi
64670 63 61 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 cant performance
64680 20 65 6e 68 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 enhancement.**
64690 28 61 70 70 72 6f 78 20 31 30 25 29 20 62 79 20 (approx 10%) by
646a0 61 76 6f 69 64 69 6e 67 20 6e 75 6d 65 72 6f 75 avoiding numerou
646b0 73 20 6d 61 6c 6c 6f 63 2f 66 72 65 65 20 72 65 s malloc/free re
646c0 71 75 65 73 74 73 20 77 68 69 6c 65 20 70 61 72 quests while par
646d0 73 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 sing.** SQL stat
646e0 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ements..**.** Th
646f0 65 20 4c 6f 6f 6b 61 73 69 64 65 20 73 74 72 75 e Lookaside stru
64700 63 74 75 72 65 20 68 6f 6c 64 73 20 63 6f 6e 66 cture holds conf
64710 69 67 75 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d iguration inform
64720 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a ation about the.
64730 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c ** lookaside mal
64740 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 2e 20 20 loc subsystem.
64750 45 61 63 68 20 61 76 61 69 6c 61 62 6c 65 20 6d Each available m
64760 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
64770 20 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 in.** the looka
64780 73 69 64 65 20 73 75 62 73 79 73 74 65 6d 20 69 side subsystem i
64790 73 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69 s stored on a li
647a0 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f nked list of Loo
647b0 6b 61 73 69 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 kasideSlot.** ob
647c0 6a 65 63 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 jects..*/.struct
647d0 20 4c 6f 6f 6b 61 73 69 64 65 20 7b 0a 20 20 75 Lookaside {. u
647e0 31 36 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20 16 sz;
647f0 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
64800 66 20 65 61 63 68 20 62 75 66 66 65 72 20 69 6e f each buffer in
64810 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 38 20 62 bytes */. u8 b
64820 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 Enabled;
64830 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75 /* True if u
64840 73 65 20 6c 6f 6f 6b 61 73 69 64 65 2e 20 20 46 se lookaside. F
64850 61 6c 73 65 20 74 6f 20 69 67 6e 6f 72 65 20 69 alse to ignore i
64860 74 20 2a 2f 0a 20 20 75 38 20 62 4d 61 6c 6c 6f t */. u8 bMallo
64870 63 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f ced; /
64880 2a 20 54 72 75 65 20 69 66 20 70 53 74 61 72 74 * True if pStart
64890 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
648a0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
648b0 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 3b 20 20 */. int nOut;
648c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
648d0 4e 75 6d 62 65 72 20 6f 66 20 62 75 66 66 65 72 Number of buffer
648e0 73 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 s currently chec
648f0 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 ked out */. int
64900 20 6d 78 4f 75 74 3b 20 20 20 20 20 20 20 20 20 mxOut;
64910 20 20 20 20 20 2f 2a 20 48 69 67 68 77 61 74 65 /* Highwate
64920 72 20 6d 61 72 6b 20 66 6f 72 20 6e 4f 75 74 20 r mark for nOut
64930 2a 2f 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c */. LookasideSl
64940 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 2f 2a 20 ot *pFree; /*
64950 4c 69 73 74 20 6f 66 20 61 76 61 69 6c 61 62 6c List of availabl
64960 65 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 20 76 e buffers */. v
64970 6f 69 64 20 2a 70 53 74 61 72 74 3b 20 20 20 20 oid *pStart;
64980 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
64990 62 79 74 65 20 6f 66 20 61 76 61 69 6c 61 62 6c byte of availabl
649a0 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 2a e memory space *
649b0 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e 64 3b 20 /. void *pEnd;
649c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
649d0 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 65 irst byte past e
649e0 6e 64 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 nd of available
649f0 73 70 61 63 65 20 2a 2f 0a 7d 3b 0a 73 74 72 75 space */.};.stru
64a00 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 ct LookasideSlot
64a10 20 7b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c {. LookasideSl
64a20 6f 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a ot *pNext; /*
64a30 20 4e 65 78 74 20 62 75 66 66 65 72 20 69 6e 20 Next buffer in
64a40 74 68 65 20 6c 69 73 74 20 6f 66 20 66 72 65 65 the list of free
64a50 20 62 75 66 66 65 72 73 20 2a 2f 0a 7d 3b 0a 0a buffers */.};..
64a60 2f 2a 0a 2a 2a 20 41 20 68 61 73 68 20 74 61 62 /*.** A hash tab
64a70 6c 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 le for function
64a80 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 0a definitions..**.
64a90 2a 2a 20 48 61 73 68 20 65 61 63 68 20 46 75 6e ** Hash each Fun
64aa0 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69 cDef structure i
64ab0 6e 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 46 nto one of the F
64ac0 75 6e 63 44 65 66 48 61 73 68 2e 61 5b 5d 20 73 uncDefHash.a[] s
64ad0 6c 6f 74 73 2e 0a 2a 2a 20 43 6f 6c 6c 69 73 69 lots..** Collisi
64ae0 6f 6e 73 20 61 72 65 20 6f 6e 20 74 68 65 20 46 ons are on the F
64af0 75 6e 63 44 65 66 2e 70 48 61 73 68 20 63 68 61 uncDef.pHash cha
64b00 69 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75 in..*/.struct Fu
64b10 6e 63 44 65 66 48 61 73 68 20 7b 0a 20 20 46 75 ncDefHash {. Fu
64b20 6e 63 44 65 66 20 2a 61 5b 32 33 5d 3b 20 20 20 ncDef *a[23];
64b30 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c /* Hash tabl
64b40 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 e for functions
64b50 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 */.};../*.** Eac
64b60 68 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6e h database is an
64b70 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
64b80 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
64b90 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ture..**.** The
64ba0 73 71 6c 69 74 65 2e 6c 61 73 74 52 6f 77 69 64 sqlite.lastRowid
64bb0 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 73 records the las
64bc0 74 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20 67 t insert rowid g
64bd0 65 6e 65 72 61 74 65 64 20 62 79 20 61 6e 0a 2a enerated by an.*
64be0 2a 20 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65 * insert stateme
64bf0 6e 74 2e 20 20 49 6e 73 65 72 74 73 20 6f 6e 20 nt. Inserts on
64c00 76 69 65 77 73 20 64 6f 20 6e 6f 74 20 61 66 66 views do not aff
64c10 65 63 74 20 69 74 73 20 76 61 6c 75 65 2e 20 20 ect its value.
64c20 45 61 63 68 0a 2a 2a 20 74 72 69 67 67 65 72 20 Each.** trigger
64c30 68 61 73 20 69 74 73 20 6f 77 6e 20 63 6f 6e 74 has its own cont
64c40 65 78 74 2c 20 73 6f 20 74 68 61 74 20 6c 61 73 ext, so that las
64c50 74 52 6f 77 69 64 20 63 61 6e 20 62 65 20 75 70 tRowid can be up
64c60 64 61 74 65 64 20 69 6e 73 69 64 65 0a 2a 2a 20 dated inside.**
64c70 74 72 69 67 67 65 72 73 20 61 73 20 75 73 75 61 triggers as usua
64c80 6c 2e 20 20 54 68 65 20 70 72 65 76 69 6f 75 73 l. The previous
64c90 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 72 value will be r
64ca0 65 73 74 6f 72 65 64 20 6f 6e 63 65 20 74 68 65 estored once the
64cb0 20 74 72 69 67 67 65 72 0a 2a 2a 20 65 78 69 74 trigger.** exit
64cc0 73 2e 20 20 55 70 6f 6e 20 65 6e 74 65 72 69 6e s. Upon enterin
64cd0 67 20 61 20 62 65 66 6f 72 65 20 6f 72 20 69 6e g a before or in
64ce0 73 74 65 61 64 20 6f 66 20 74 72 69 67 67 65 72 stead of trigger
64cf0 2c 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20 6e , lastRowid is n
64d00 6f 0a 2a 2a 20 6c 6f 6e 67 65 72 20 28 73 69 6e o.** longer (sin
64d10 63 65 20 61 66 74 65 72 20 76 65 72 73 69 6f 6e ce after version
64d20 20 32 2e 38 2e 31 32 29 20 72 65 73 65 74 20 74 2.8.12) reset t
64d30 6f 20 2d 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 o -1..**.** The
64d40 73 71 6c 69 74 65 2e 6e 43 68 61 6e 67 65 20 64 sqlite.nChange d
64d50 6f 65 73 20 6e 6f 74 20 63 6f 75 6e 74 20 63 68 oes not count ch
64d60 61 6e 67 65 73 20 77 69 74 68 69 6e 20 74 72 69 anges within tri
64d70 67 67 65 72 73 20 61 6e 64 20 6b 65 65 70 73 20 ggers and keeps
64d80 6e 6f 0a 2a 2a 20 63 6f 6e 74 65 78 74 2e 20 20 no.** context.
64d90 49 74 20 69 73 20 72 65 73 65 74 20 61 74 20 73 It is reset at s
64da0 74 61 72 74 20 6f 66 20 73 71 6c 69 74 65 33 5f tart of sqlite3_
64db0 65 78 65 63 2e 0a 2a 2a 20 54 68 65 20 73 71 6c exec..** The sql
64dc0 69 74 65 2e 6c 73 43 68 61 6e 67 65 20 72 65 70 ite.lsChange rep
64dd0 72 65 73 65 6e 74 73 20 74 68 65 20 6e 75 6d 62 resents the numb
64de0 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 6d 61 er of changes ma
64df0 64 65 20 62 79 20 74 68 65 20 6c 61 73 74 0a 2a de by the last.*
64e00 2a 20 69 6e 73 65 72 74 2c 20 75 70 64 61 74 65 * insert, update
64e10 2c 20 6f 72 20 64 65 6c 65 74 65 20 73 74 61 74 , or delete stat
64e20 65 6d 65 6e 74 2e 20 20 49 74 20 72 65 6d 61 69 ement. It remai
64e30 6e 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 72 6f ns constant thro
64e40 75 67 68 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 65 ughout the.** le
64e50 6e 67 74 68 20 6f 66 20 61 20 73 74 61 74 65 6d ngth of a statem
64e60 65 6e 74 20 61 6e 64 20 69 73 20 74 68 65 6e 20 ent and is then
64e70 75 70 64 61 74 65 64 20 62 79 20 4f 50 5f 53 65 updated by OP_Se
64e80 74 43 6f 75 6e 74 73 2e 20 20 49 74 20 6b 65 65 tCounts. It kee
64e90 70 73 20 61 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 ps a.** context
64ea0 73 74 61 63 6b 20 6a 75 73 74 20 6c 69 6b 65 20 stack just like
64eb0 6c 61 73 74 52 6f 77 69 64 20 73 6f 20 74 68 61 lastRowid so tha
64ec0 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 63 t the count of c
64ed0 68 61 6e 67 65 73 0a 2a 2a 20 77 69 74 68 69 6e hanges.** within
64ee0 20 61 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f a trigger is no
64ef0 74 20 73 65 65 6e 20 6f 75 74 73 69 64 65 20 74 t seen outside t
64f00 68 65 20 74 72 69 67 67 65 72 2e 20 20 43 68 61 he trigger. Cha
64f10 6e 67 65 73 20 74 6f 20 76 69 65 77 73 20 64 6f nges to views do
64f20 20 6e 6f 74 0a 2a 2a 20 61 66 66 65 63 74 20 74 not.** affect t
64f30 68 65 20 76 61 6c 75 65 20 6f 66 20 6c 73 43 68 he value of lsCh
64f40 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c ange..** The sql
64f50 69 74 65 2e 63 73 43 68 61 6e 67 65 20 6b 65 65 ite.csChange kee
64f60 70 73 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 ps track of the
64f70 6e 75 6d 62 65 72 20 6f 66 20 63 75 72 72 65 6e number of curren
64f80 74 20 63 68 61 6e 67 65 73 20 28 73 69 6e 63 65 t changes (since
64f90 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 73 74 61 .** the last sta
64fa0 74 65 6d 65 6e 74 29 20 61 6e 64 20 69 73 20 75 tement) and is u
64fb0 73 65 64 20 74 6f 20 75 70 64 61 74 65 20 73 71 sed to update sq
64fc0 6c 69 74 65 5f 6c 73 43 68 61 6e 67 65 2e 0a 2a lite_lsChange..*
64fd0 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 62 65 72 20 *.** The member
64fe0 76 61 72 69 61 62 6c 65 73 20 73 71 6c 69 74 65 variables sqlite
64ff0 2e 65 72 72 43 6f 64 65 2c 20 73 71 6c 69 74 65 .errCode, sqlite
65000 2e 7a 45 72 72 4d 73 67 20 61 6e 64 20 73 71 6c .zErrMsg and sql
65010 69 74 65 2e 7a 45 72 72 4d 73 67 31 36 0a 2a 2a ite.zErrMsg16.**
65020 20 73 74 6f 72 65 20 74 68 65 20 6d 6f 73 74 20 store the most
65030 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 recent error cod
65040 65 20 61 6e 64 2c 20 69 66 20 61 70 70 6c 69 63 e and, if applic
65050 61 62 6c 65 2c 20 73 74 72 69 6e 67 2e 20 54 68 able, string. Th
65060 65 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 66 75 e.** internal fu
65070 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 45 72 nction sqlite3Er
65080 72 6f 72 28 29 20 69 73 20 75 73 65 64 20 74 6f ror() is used to
65090 20 73 65 74 20 74 68 65 73 65 20 76 61 72 69 61 set these varia
650a0 62 6c 65 73 0a 2a 2a 20 63 6f 6e 73 69 73 74 65 bles.** consiste
650b0 6e 74 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ntly..*/.struct
650c0 73 71 6c 69 74 65 33 20 7b 0a 20 20 73 71 6c 69 sqlite3 {. sqli
650d0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 te3_vfs *pVfs;
650e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 53 20 /* OS
650f0 49 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 69 Interface */. i
65100 6e 74 20 6e 44 62 3b 20 20 20 20 20 20 20 20 20 nt nDb;
65110 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
65120 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 65 6e Number of backen
65130 64 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 ds currently in
65140 75 73 65 20 2a 2f 0a 20 20 44 62 20 2a 61 44 62 use */. Db *aDb
65150 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
65160 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 62 61 /* All ba
65170 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 ckends */. int
65180 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
65190 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 73 /* Mis
651a0 63 65 6c 6c 61 6e 65 6f 75 73 20 66 6c 61 67 73 cellaneous flags
651b0 2e 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 . See below */.
651c0 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 3b 20 int openFlags;
651d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
651e0 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 * Flags passed t
651f0 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f o sqlite3_vfs.xO
65200 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65 pen() */. int e
65210 72 72 43 6f 64 65 3b 20 20 20 20 20 20 20 20 20 rrCode;
65220 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 /* Most
65230 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f recent error co
65240 64 65 20 28 53 51 4c 49 54 45 5f 2a 29 20 2a 2f de (SQLITE_*) */
65250 0a 20 20 69 6e 74 20 65 72 72 4d 61 73 6b 3b 20 . int errMask;
65260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65270 20 2f 2a 20 26 20 72 65 73 75 6c 74 20 63 6f 64 /* & result cod
65280 65 73 20 77 69 74 68 20 74 68 69 73 20 62 65 66 es with this bef
65290 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 2a 2f ore returning */
652a0 0a 20 20 75 38 20 61 75 74 6f 43 6f 6d 6d 69 74 . u8 autoCommit
652b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
652c0 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d /* The auto-com
652d0 6d 69 74 20 66 6c 61 67 2e 20 2a 2f 0a 20 20 75 mit flag. */. u
652e0 38 20 74 65 6d 70 5f 73 74 6f 72 65 3b 20 20 20 8 temp_store;
652f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
65300 31 3a 20 66 69 6c 65 20 32 3a 20 6d 65 6d 6f 72 1: file 2: memor
65310 79 20 30 3a 20 64 65 66 61 75 6c 74 20 2a 2f 0a y 0: default */.
65320 20 20 75 38 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 u8 mallocFaile
65330 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
65340 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 68 61 /* True if we ha
65350 76 65 20 73 65 65 6e 20 61 20 6d 61 6c 6c 6f 63 ve seen a malloc
65360 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 75 38 failure */. u8
65370 20 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 3b 20 20 dfltLockMode;
65380 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
65390 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 2d 6d efault locking-m
653a0 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68 65 64 ode for attached
653b0 20 64 62 73 20 2a 2f 0a 20 20 75 38 20 64 66 6c dbs */. u8 dfl
653c0 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 tJournalMode;
653d0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 /* Defau
653e0 6c 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 lt journal mode
653f0 66 6f 72 20 61 74 74 61 63 68 65 64 20 64 62 73 for attached dbs
65400 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 */. signed cha
65410 72 20 6e 65 78 74 41 75 74 6f 76 61 63 3b 20 20 r nextAutovac;
65420 20 20 20 20 2f 2a 20 41 75 74 6f 76 61 63 20 73 /* Autovac s
65430 65 74 74 69 6e 67 20 61 66 74 65 72 20 56 41 43 etting after VAC
65440 55 55 4d 20 69 66 20 3e 3d 30 20 2a 2f 0a 20 20 UUM if >=0 */.
65450 69 6e 74 20 6e 65 78 74 50 61 67 65 73 69 7a 65 int nextPagesize
65460 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
65470 20 50 61 67 65 73 69 7a 65 20 61 66 74 65 72 20 Pagesize after
65480 56 41 43 55 55 4d 20 69 66 20 3e 30 20 2a 2f 0a VACUUM if >0 */.
65490 20 20 69 6e 74 20 6e 54 61 62 6c 65 3b 20 20 20 int nTable;
654a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
654b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 /* Number of tab
654c0 6c 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 les in the datab
654d0 61 73 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 ase */. CollSeq
654e0 20 2a 70 44 66 6c 74 43 6f 6c 6c 3b 20 20 20 20 *pDfltColl;
654f0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 /* The de
65500 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20 fault collating
65510 73 65 71 75 65 6e 63 65 20 28 42 49 4e 41 52 59 sequence (BINARY
65520 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 ) */. i64 lastR
65530 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 owid;
65540 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20 6f 66 /* ROWID of
65550 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 73 most recent ins
65560 65 72 74 20 28 73 65 65 20 61 62 6f 76 65 29 20 ert (see above)
65570 2a 2f 0a 20 20 69 36 34 20 70 72 69 6f 72 4e 65 */. i64 priorNe
65580 77 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 wRowid;
65590 20 20 20 2f 2a 20 4c 61 73 74 20 72 61 6e 64 6f /* Last rando
655a0 6d 6c 79 20 67 65 6e 65 72 61 74 65 64 20 52 4f mly generated RO
655b0 57 49 44 20 2a 2f 0a 20 20 75 33 32 20 6d 61 67 WID */. u32 mag
655c0 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ic;
655d0 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 /* Magic
655e0 6e 75 6d 62 65 72 20 66 6f 72 20 64 65 74 65 63 number for detec
655f0 74 20 6c 69 62 72 61 72 79 20 6d 69 73 75 73 65 t library misuse
65600 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 */. int nChang
65610 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
65620 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 /* Value ret
65630 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
65640 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 20 _changes() */.
65650 69 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e 67 65 int nTotalChange
65660 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
65670 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 Value returned
65680 62 79 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c by sqlite3_total
65690 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 20 _changes() */.
656a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
656b0 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a utex; /*
656c0 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 65 Connection mute
656d0 78 20 2a 2f 0a 20 20 69 6e 74 20 61 4c 69 6d 69 x */. int aLimi
656e0 74 5b 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 t[SQLITE_N_LIMIT
656f0 5d 3b 20 20 20 2f 2a 20 4c 69 6d 69 74 73 20 2a ]; /* Limits *
65700 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 /. struct sqlit
65710 65 33 49 6e 69 74 49 6e 66 6f 20 7b 20 20 20 20 e3InitInfo {
65720 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e /* Information
65730 20 75 73 65 64 20 64 75 72 69 6e 67 20 69 6e 69 used during ini
65740 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 tialization */.
65750 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 int iDb;
65760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
65770 2a 20 57 68 65 6e 20 62 61 63 6b 20 69 73 20 62 * When back is b
65780 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 eing initialized
65790 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 77 54 */. int newT
657a0 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 num;
657b0 20 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67 65 20 /* Rootpage
657c0 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69 of table being i
657d0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 nitialized */.
657e0 20 20 75 38 20 62 75 73 79 3b 20 20 20 20 20 20 u8 busy;
657f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
65800 20 54 52 55 45 20 69 66 20 63 75 72 72 65 6e 74 TRUE if current
65810 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 ly initializing
65820 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20 20 69 */. } init;. i
65830 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 20 nt nExtension;
65840 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
65850 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64 65 64 Number of loaded
65860 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 extensions */.
65870 20 76 6f 69 64 20 2a 2a 61 45 78 74 65 6e 73 69 void **aExtensi
65880 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f on; /
65890 2a 20 41 72 72 61 79 20 6f 66 20 73 68 61 72 65 * Array of share
658a0 64 20 6c 69 62 72 61 72 79 20 68 61 6e 64 6c 65 d library handle
658b0 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 56 64 s */. struct Vd
658c0 62 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 be *pVdbe;
658d0 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
658e0 61 63 74 69 76 65 20 76 69 72 74 75 61 6c 20 6d active virtual m
658f0 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 achines */. int
65900 20 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 20 activeVdbeCnt;
65910 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
65920 6d 62 65 72 20 6f 66 20 56 44 42 45 73 20 63 75 mber of VDBEs cu
65930 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e rrently executin
65940 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72 69 74 65 g */. int write
65950 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 20 VdbeCnt;
65960 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
65970 66 20 61 63 74 69 76 65 20 56 44 42 45 73 20 74 f active VDBEs t
65980 68 61 74 20 61 72 65 20 77 72 69 74 69 6e 67 20 hat are writing
65990 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 61 */. void (*xTra
659a0 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 ce)(void*,const
659b0 63 68 61 72 2a 29 3b 20 20 20 20 20 20 20 20 2f char*); /
659c0 2a 20 54 72 61 63 65 20 66 75 6e 63 74 69 6f 6e * Trace function
659d0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 54 72 61 */. void *pTra
659e0 63 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 ceArg;
659f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65a00 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 /* Argument to t
65a10 68 65 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f he trace functio
65a20 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 50 n */. void (*xP
65a30 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f rofile)(void*,co
65a40 6e 73 74 20 63 68 61 72 2a 2c 75 36 34 29 3b 20 nst char*,u64);
65a50 20 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 20 66 75 /* Profiling fu
65a60 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
65a70 20 2a 70 50 72 6f 66 69 6c 65 41 72 67 3b 20 20 *pProfileArg;
65a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65a90 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
65aa0 74 20 74 6f 20 70 72 6f 66 69 6c 65 20 66 75 6e t to profile fun
65ab0 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 ction */. void
65ac0 2a 70 43 6f 6d 6d 69 74 41 72 67 3b 20 20 20 20 *pCommitArg;
65ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
65ae0 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 6d Argument to xCom
65af0 6d 69 74 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f mitCallback() */
65b00 20 20 20 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d . int (*xCom
65b10 6d 69 74 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 mitCallback)(voi
65b20 64 2a 29 3b 20 20 20 20 2f 2a 20 49 6e 76 6f 6b d*); /* Invok
65b30 65 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d ed at every comm
65b40 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 it. */. void *p
65b50 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 20 20 20 RollbackArg;
65b60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
65b70 67 75 6d 65 6e 74 20 74 6f 20 78 52 6f 6c 6c 62 gument to xRollb
65b80 61 63 6b 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f ackCallback() */
65b90 20 20 20 0a 20 20 76 6f 69 64 20 28 2a 78 52 6f . void (*xRo
65ba0 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 29 28 llbackCallback)(
65bb0 76 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e 76 6f 6b void*); /* Invok
65bc0 65 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d ed at every comm
65bd0 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 it. */. void *p
65be0 55 70 64 61 74 65 41 72 67 3b 0a 20 20 76 6f 69 UpdateArg;. voi
65bf0 64 20 28 2a 78 55 70 64 61 74 65 43 61 6c 6c 62 d (*xUpdateCallb
65c00 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 ack)(void*,int,
65c10 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 const char*,cons
65c20 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 69 t char*,sqlite_i
65c30 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64 28 2a 78 nt64);. void(*x
65c40 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 CollNeeded)(void
65c50 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 *,sqlite3*,int e
65c60 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 TextRep,const ch
65c70 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 ar*);. void(*xC
65c80 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69 ollNeeded16)(voi
65c90 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 d*,sqlite3*,int
65ca0 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 eTextRep,const v
65cb0 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 70 oid*);. void *p
65cc0 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 CollNeededArg;.
65cd0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
65ce0 70 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 2f pErr; /
65cf0 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 * Most recent er
65d00 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 ror message */.
65d10 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 char *zErrMsg;
65d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
65d30 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 * Most recent er
65d40 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46 ror message (UTF
65d50 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 -8 encoded) */.
65d60 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 31 36 char *zErrMsg16
65d70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
65d80 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 * Most recent er
65d90 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46 ror message (UTF
65da0 2d 31 36 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a -16 encoded) */.
65db0 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 76 6f union {. vo
65dc0 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 49 6e 74 latile int isInt
65dd0 65 72 72 75 70 74 65 64 3b 20 2f 2a 20 54 72 75 errupted; /* Tru
65de0 65 20 69 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 e if sqlite3_int
65df0 65 72 72 75 70 74 20 68 61 73 20 62 65 65 6e 20 errupt has been
65e00 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 64 6f called */. do
65e10 75 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b 20 20 uble notUsed1;
65e20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 /* Spa
65e30 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b 0a 20 cer */. } u1;.
65e40 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f 6f 6b 61 Lookaside looka
65e50 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f side; /
65e60 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c * Lookaside mall
65e70 6f 63 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e oc configuration
65e80 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
65e90 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a TE_OMIT_AUTHORIZ
65ea0 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28 2a 78 41 ATION. int (*xA
65eb0 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 uth)(void*,int,c
65ec0 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 onst char*,const
65ed0 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 char*,const cha
65ee0 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b r*,const char*);
65ef0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
65f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65f10 20 2f 2a 20 41 63 63 65 73 73 20 61 75 74 68 6f /* Access autho
65f20 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f rization functio
65f30 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 n */. void *pAu
65f40 74 68 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 thArg;
65f50 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75 /* 1st argu
65f60 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 63 63 65 ment to the acce
65f70 73 73 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e ss auth function
65f80 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 */.#endif.#ifnd
65f90 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 ef SQLITE_OMIT_P
65fa0 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b ROGRESS_CALLBACK
65fb0 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 . int (*xProgre
65fc0 73 73 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 ss)(void *);
65fd0 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 65 73 73 /* The progress
65fe0 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 callback */. v
65ff0 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 73 41 72 oid *pProgressAr
66000 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 g; /*
66010 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 Argument to the
66020 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 progress callbac
66030 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 6f 67 k */. int nProg
66040 72 65 73 73 4f 70 73 3b 20 20 20 20 20 20 20 20 ressOps;
66050 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
66060 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 70 72 f opcodes for pr
66070 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 ogress callback
66080 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 */.#endif.#ifnde
66090 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
660a0 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 48 61 73 RTUALTABLE. Has
660b0 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 h aModule;
660c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 6f /* po
660d0 70 75 6c 61 74 65 64 20 62 79 20 73 71 6c 69 74 pulated by sqlit
660e0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 e3_create_module
660f0 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 () */. Table *p
66100 56 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 VTab;
66110 20 20 20 20 20 20 2f 2a 20 76 74 61 62 20 77 69 /* vtab wi
66120 74 68 20 61 63 74 69 76 65 20 43 6f 6e 6e 65 63 th active Connec
66130 74 2f 43 72 65 61 74 65 20 6d 65 74 68 6f 64 20 t/Create method
66140 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 */. sqlite3_vta
66150 62 20 2a 2a 61 56 54 72 61 6e 73 3b 20 20 20 20 b **aVTrans;
66160 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 /* Virtual ta
66170 62 6c 65 73 20 77 69 74 68 20 6f 70 65 6e 20 74 bles with open t
66180 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a 2f 0a 20 ransactions */.
66190 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b 20 20 20 int nVTrans;
661a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
661b0 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 * Allocated size
661c0 20 6f 66 20 61 56 54 72 61 6e 73 20 2a 2f 0a 23 of aVTrans */.#
661d0 65 6e 64 69 66 0a 20 20 46 75 6e 63 44 65 66 48 endif. FuncDefH
661e0 61 73 68 20 61 46 75 6e 63 3b 20 20 20 20 20 20 ash aFunc;
661f0 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 /* Hash ta
66200 62 6c 65 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f ble of connectio
66210 6e 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 n functions */.
66220 20 48 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20 Hash aCollSeq;
66230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
66240 2a 20 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20 * All collating
66250 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42 sequences */. B
66260 75 73 79 48 61 6e 64 6c 65 72 20 62 75 73 79 48 usyHandler busyH
66270 61 6e 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 andler; /*
66280 42 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f Busy callback */
66290 0a 20 20 69 6e 74 20 62 75 73 79 54 69 6d 65 6f . int busyTimeo
662a0 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ut;
662b0 20 2f 2a 20 42 75 73 79 20 68 61 6e 64 6c 65 72 /* Busy handler
662c0 20 74 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 65 timeout, in mse
662d0 63 20 2a 2f 0a 20 20 44 62 20 61 44 62 53 74 61 c */. Db aDbSta
662e0 74 69 63 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 tic[2];
662f0 20 20 20 20 20 2f 2a 20 53 74 61 74 69 63 20 73 /* Static s
66300 70 61 63 65 20 66 6f 72 20 74 68 65 20 32 20 64 pace for the 2 d
66310 65 66 61 75 6c 74 20 62 61 63 6b 65 6e 64 73 20 efault backends
66320 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
66330 5f 53 53 45 0a 20 20 73 71 6c 69 74 65 33 5f 73 _SSE. sqlite3_s
66340 74 6d 74 20 2a 70 46 65 74 63 68 3b 20 20 20 20 tmt *pFetch;
66350 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 /* Used by
66360 53 53 45 20 74 6f 20 66 65 74 63 68 20 73 74 6f SSE to fetch sto
66370 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 2a red statements *
66380 2f 0a 23 65 6e 64 69 66 0a 20 20 53 61 76 65 70 /.#endif. Savep
66390 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 oint *pSavepoint
663a0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 ; /* List
663b0 20 6f 66 20 61 63 74 69 76 65 20 73 61 76 65 70 of active savep
663c0 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e oints */. int n
663d0 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 Savepoint;
663e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
663f0 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 er of non-transa
66400 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 73 ction savepoints
66410 20 2a 2f 0a 20 20 75 38 20 69 73 54 72 61 6e 73 */. u8 isTrans
66420 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b actionSavepoint;
66430 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
66440 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 he outermost sav
66450 65 70 6f 69 6e 74 20 69 73 20 61 20 54 53 20 2a epoint is a TS *
66460 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 /.};../*.** A ma
66470 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20 cro to discover
66480 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 the encoding of
66490 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23 a database..*/.#
664a0 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28 define ENC(db) (
664b0 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 (db)->aDb[0].pSc
664c0 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a hema->enc)../*.*
664d0 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 * Possible value
664e0 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 s for the sqlite
664f0 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 62 .flags and or Db
66500 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a 2a .flags fields..*
66510 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e 66 *.** On sqlite.f
66520 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49 54 45 lags, the SQLITE
66530 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 6d _InTrans value m
66540 65 61 6e 73 20 74 68 61 74 20 77 65 20 68 61 76 eans that we hav
66550 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 61 20 e.** executed a
66560 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c BEGIN. On Db.fl
66570 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 72 ags, SQLITE_InTr
66580 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74 61 74 ans means a stat
66590 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 ement.** transac
665a0 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 6f tion is active o
665b0 6e 20 74 68 61 74 20 70 61 72 74 69 63 75 6c 61 n that particula
665c0 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e r database file.
665d0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
665e0 54 45 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 TE_VdbeTrace
665f0 20 20 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 0x00000001 /*
66600 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 20 56 True to trace V
66610 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f DBE execution */
66620 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
66630 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20 20 30 InTrans 0
66640 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 54 72 x00000008 /* Tr
66650 75 65 20 69 66 20 69 6e 20 61 20 74 72 61 6e 73 ue if in a trans
66660 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e action */.#defin
66670 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 e SQLITE_InternC
66680 68 61 6e 67 65 73 20 20 30 78 30 30 30 30 30 30 hanges 0x000000
66690 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 10 /* Uncommitt
666a0 65 64 20 48 61 73 68 20 74 61 62 6c 65 20 63 68 ed Hash table ch
666b0 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 anges */.#define
666c0 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e SQLITE_FullColN
666d0 61 6d 65 73 20 20 20 30 78 30 30 30 30 30 30 32 ames 0x0000002
666e0 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 0 /* Show full
666f0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 column names on
66700 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e SELECT */.#defin
66710 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f e SQLITE_ShortCo
66720 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30 30 lNames 0x000000
66730 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72 40 /* Show shor
66740 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 t columns names
66750 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
66760 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 E_CountRows
66770 20 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20 0x00000080 /*
66780 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67 Count rows chang
66790 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f ed by INSERT, */
667a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
667b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
667c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 /*
667d0 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 DELETE, or UPDAT
667e0 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a E and return */.
667f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66810 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74 /* t
66820 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61 he count using a
66830 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 callback. */.#d
66840 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c efine SQLITE_Nul
66850 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30 lCallback 0x00
66860 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 6b 000100 /* Invok
66870 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f e the callback o
66880 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20 nce if the */.
66890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
668a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
668b0 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73 /* res
668c0 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 ult set is empty
668d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
668e0 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 TE_SqlTrace
668f0 20 20 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 0x00000200 /*
66900 20 44 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c Debug print SQL
66910 20 61 73 20 69 74 20 65 78 65 63 75 74 65 73 20 as it executes
66920 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
66930 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 E_VdbeListing
66940 20 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 0x00000400 /*
66950 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f Debug listings o
66960 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 f VDBE programs
66970 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
66980 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20 20 E_WriteSchema
66990 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 0x00000800 /*
669a0 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c OK to update SQL
669b0 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 64 ITE_MASTER */.#d
669c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f 52 efine SQLITE_NoR
669d0 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 30 eadlock 0x00
669e0 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61 64 6c 001000 /* Readl
669f0 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64 ocks are omitted
66a00 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20 20 20 when .
66a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66a30 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 72 65 ** accessing re
66a40 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 ad-only database
66a50 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
66a60 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 ITE_IgnoreChecks
66a70 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 0x00002000 /
66a80 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65 * Do not enforce
66a90 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e check constrain
66aa0 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ts */.#define SQ
66ab0 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 LITE_ReadUncommi
66ac0 74 74 65 64 20 30 78 30 30 30 30 34 30 30 30 20 tted 0x00004000
66ad0 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d 63 61 /* For shared-ca
66ae0 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 che mode */.#def
66af0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 ine SQLITE_Legac
66b00 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30 30 30 yFileFmt 0x0000
66b10 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74 65 20 8000 /* Create
66b20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 69 6e new databases in
66b30 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 65 format 1 */.#de
66b40 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c fine SQLITE_Full
66b50 46 53 79 6e 63 20 20 20 20 20 20 30 78 30 30 30 FSync 0x000
66b60 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20 66 75 10000 /* Use fu
66b70 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 20 ll fsync on the
66b80 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 69 backend */.#defi
66b90 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 ne SQLITE_LoadEx
66ba0 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 30 tension 0x00020
66bb0 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6c 000 /* Enable l
66bc0 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a 2f oad_extension */
66bd0 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ..#define SQLITE
66be0 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 20 _RecoveryMode
66bf0 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 49 0x00040000 /* I
66c00 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65 72 72 gnore schema err
66c10 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ors */.#define S
66c20 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68 QLITE_SharedCach
66c30 65 20 20 20 20 30 78 30 30 30 38 30 30 30 30 20 e 0x00080000
66c40 20 2f 2a 20 43 61 63 68 65 20 73 68 61 72 69 6e /* Cache sharin
66c50 67 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a g is enabled */.
66c60 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 #define SQLITE_V
66c70 74 61 62 20 20 20 20 20 20 20 20 20 20 20 30 78 tab 0x
66c80 30 30 31 30 30 30 30 30 20 20 2f 2a 20 54 68 65 00100000 /* The
66c90 72 65 20 65 78 69 73 74 73 20 61 20 76 69 72 74 re exists a virt
66ca0 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 ual table */.#de
66cb0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f 6d 6d fine SQLITE_Comm
66cc0 69 74 42 75 73 79 20 20 20 20 20 30 78 30 30 32 itBusy 0x002
66cd0 30 30 30 30 30 20 20 2f 2a 20 49 6e 20 74 68 65 00000 /* In the
66ce0 20 70 72 6f 63 65 73 73 20 6f 66 20 63 6f 6d 6d process of comm
66cf0 69 74 74 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a itting */../*.**
66d00 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 Possible values
66d10 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e for the sqlite.
66d20 6d 61 67 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 magic field..**
66d30 54 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 The numbers are
66d40 6f 62 74 61 69 6e 65 64 20 61 74 20 72 61 6e 64 obtained at rand
66d50 6f 6d 20 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 om and have no s
66d60 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 pecial meaning,
66d70 6f 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 other.** than be
66d80 69 6e 67 20 64 69 73 74 69 6e 63 74 20 66 72 6f ing distinct fro
66d90 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a m one another..*
66da0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
66db0 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 _MAGIC_OPEN
66dc0 30 78 61 30 32 39 61 36 39 37 20 20 2f 2a 20 44 0xa029a697 /* D
66dd0 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20 atabase is open
66de0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
66df0 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 E_MAGIC_CLOSED
66e00 20 30 78 39 66 33 63 32 64 33 33 20 20 2f 2a 20 0x9f3c2d33 /*
66e10 44 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 Database is clos
66e20 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ed */.#define SQ
66e30 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 LITE_MAGIC_SICK
66e40 20 20 20 20 30 78 34 62 37 37 31 32 39 30 20 20 0x4b771290
66e50 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20 61 77 61 /* Error and awa
66e60 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23 iting close */.#
66e70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
66e80 47 49 43 5f 42 55 53 59 20 20 20 20 20 30 78 66 GIC_BUSY 0xf
66e90 30 33 62 37 39 30 36 20 20 2f 2a 20 44 61 74 61 03b7906 /* Data
66ea0 62 61 73 65 20 63 75 72 72 65 6e 74 6c 79 20 69 base currently i
66eb0 6e 20 75 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 n use */.#define
66ec0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 SQLITE_MAGIC_ER
66ed0 52 4f 52 20 20 20 20 30 78 62 35 33 35 37 39 33 ROR 0xb535793
66ee0 30 20 20 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 0 /* An SQLITE_
66ef0 4d 49 53 55 53 45 20 65 72 72 6f 72 20 6f 63 63 MISUSE error occ
66f00 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 urred */../*.**
66f10 45 61 63 68 20 53 51 4c 20 66 75 6e 63 74 69 6f Each SQL functio
66f20 6e 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 n is defined by
66f30 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
66f40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 he following.**
66f50 73 74 72 75 63 74 75 72 65 2e 20 20 41 20 70 6f structure. A po
66f60 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 inter to this st
66f70 72 75 63 74 75 72 65 20 69 73 20 73 74 6f 72 65 ructure is store
66f80 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e d in the sqlite.
66f90 61 46 75 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61 aFunc.** hash ta
66fa0 62 6c 65 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69 ble. When multi
66fb0 70 6c 65 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 ple functions ha
66fc0 76 65 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 ve the same name
66fd0 2c 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 , the hash table
66fe0 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 .** points to a
66ff0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 linked list of t
67000 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 2e hese structures.
67010 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 .*/.struct FuncD
67020 65 66 20 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b ef {. i16 nArg;
67030 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
67040 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
67050 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e ts. -1 means un
67060 6c 69 6d 69 74 65 64 20 2a 2f 0a 20 20 75 38 20 limited */. u8
67070 69 50 72 65 66 45 6e 63 3b 20 20 20 20 20 20 20 iPrefEnc;
67080 20 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 /* Preferred t
67090 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 ext encoding (SQ
670a0 4c 49 54 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c LITE_UTF8, 16LE,
670b0 20 31 36 42 45 29 20 2a 2f 0a 20 20 75 38 20 66 16BE) */. u8 f
670c0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
670d0 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 /* Some combina
670e0 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46 tion of SQLITE_F
670f0 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20 UNC_* */. void
67100 2a 70 55 73 65 72 44 61 74 61 3b 20 20 20 20 20 *pUserData;
67110 2f 2a 20 55 73 65 72 20 64 61 74 61 20 70 61 72 /* User data par
67120 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63 ameter */. Func
67130 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 Def *pNext;
67140 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74 69 6f /* Next functio
67150 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61 6d 65 n with same name
67160 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 */. void (*xFu
67170 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 nc)(sqlite3_cont
67180 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
67190 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65 _value**); /* Re
671a0 67 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a gular function *
671b0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 /. void (*xStep
671c0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
671d0 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 t*,int,sqlite3_v
671e0 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72 alue**); /* Aggr
671f0 65 67 61 74 65 20 73 74 65 70 20 2a 2f 0a 20 20 egate step */.
67200 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65 void (*xFinalize
67210 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
67220 74 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 t*);
67230 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 /* Aggregate
67240 20 66 69 6e 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 finalizer */.
67250 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
67260 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65 /* SQL name
67270 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
67280 2e 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a . */. FuncDef *
67290 70 48 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e pHash; /* N
672a0 65 78 74 20 77 69 74 68 20 61 20 64 69 66 66 65 ext with a diffe
672b0 72 65 6e 74 20 6e 61 6d 65 20 62 75 74 20 74 68 rent name but th
672c0 65 20 73 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d e same hash */.}
672d0 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c ;../*.** Possibl
672e0 65 20 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e e values for Fun
672f0 63 44 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 cDef.flags.*/.#d
67300 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
67310 43 5f 4c 49 4b 45 20 20 20 20 20 30 78 30 31 20 C_LIKE 0x01
67320 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 66 6f 72 /* Candidate for
67330 20 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 the LIKE optimi
67340 7a 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e zation */.#defin
67350 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 e SQLITE_FUNC_CA
67360 53 45 20 20 20 20 20 30 78 30 32 20 2f 2a 20 43 SE 0x02 /* C
67370 61 73 65 2d 73 65 6e 73 69 74 69 76 65 20 4c 49 ase-sensitive LI
67380 4b 45 2d 74 79 70 65 20 66 75 6e 63 74 69 6f 6e KE-type function
67390 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
673a0 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 20 20 TE_FUNC_EPHEM
673b0 20 30 78 30 34 20 2f 2a 20 45 70 68 65 6d 65 72 0x04 /* Ephemer
673c0 61 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 74 68 al. Delete with
673d0 20 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65 VDBE */.#define
673e0 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 SQLITE_FUNC_NEE
673f0 44 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71 DCOLL 0x08 /* sq
67400 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c lite3GetFuncColl
67410 53 65 71 28 29 20 6d 69 67 68 74 20 62 65 20 63 Seq() might be c
67420 61 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 alled */.#define
67430 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 50 52 49 SQLITE_FUNC_PRI
67440 56 41 54 45 20 20 30 78 31 30 20 2f 2a 20 41 6c VATE 0x10 /* Al
67450 6c 6f 77 65 64 20 66 6f 72 20 69 6e 74 65 72 6e lowed for intern
67460 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 0a al use only */..
67470 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
67480 69 6e 67 20 74 68 72 65 65 20 6d 61 63 72 6f 73 ing three macros
67490 2c 20 46 55 4e 43 54 49 4f 4e 28 29 2c 20 4c 49 , FUNCTION(), LI
674a0 4b 45 46 55 4e 43 28 29 20 61 6e 64 20 41 47 47 KEFUNC() and AGG
674b0 52 45 47 41 54 45 28 29 20 61 72 65 0a 2a 2a 20 REGATE() are.**
674c0 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 used to create t
674d0 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 73 20 he initializers
674e0 66 6f 72 20 74 68 65 20 46 75 6e 63 44 65 66 20 for the FuncDef
674f0 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a structures..**.*
67500 2a 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 * FUNCTION(zNa
67510 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 me, nArg, iArg,
67520 62 4e 43 2c 20 78 46 75 6e 63 29 0a 2a 2a 20 20 bNC, xFunc).**
67530 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 Used to creat
67540 65 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 e a scalar funct
67550 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f ion definition o
67560 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 f a function zNa
67570 6d 65 20 0a 2a 2a 20 20 20 20 20 69 6d 70 6c 65 me .** imple
67580 6d 65 6e 74 65 64 20 62 79 20 43 20 66 75 6e 63 mented by C func
67590 74 69 6f 6e 20 78 46 75 6e 63 20 74 68 61 74 20 tion xFunc that
675a0 61 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 accepts nArg arg
675b0 75 6d 65 6e 74 73 2e 20 54 68 65 0a 2a 2a 20 20 uments. The.**
675c0 20 20 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 value passed
675d0 61 73 20 69 41 72 67 20 69 73 20 63 61 73 74 20 as iArg is cast
675e0 74 6f 20 61 20 28 76 6f 69 64 2a 29 20 61 6e 64 to a (void*) and
675f0 20 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 0a made available.
67600 2a 2a 20 20 20 20 20 61 73 20 74 68 65 20 75 73 ** as the us
67610 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 er-data (sqlite3
67620 5f 75 73 65 72 5f 64 61 74 61 28 29 29 20 66 6f _user_data()) fo
67630 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 r the function.
67640 49 66 20 0a 2a 2a 20 20 20 20 20 61 72 67 75 6d If .** argum
67650 65 6e 74 20 62 4e 43 20 69 73 20 74 72 75 65 2c ent bNC is true,
67660 20 74 68 65 6e 20 74 68 65 20 46 75 6e 63 44 65 then the FuncDe
67670 66 2e 6e 65 65 64 43 6f 6c 6c 61 74 65 20 66 6c f.needCollate fl
67680 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a ag is set..**.**
67690 20 20 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 AGGREGATE(zNa
676a0 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 me, nArg, iArg,
676b0 62 4e 43 2c 20 78 53 74 65 70 2c 20 78 46 69 6e bNC, xStep, xFin
676c0 61 6c 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 al).** Used
676d0 74 6f 20 63 72 65 61 74 65 20 61 6e 20 61 67 67 to create an agg
676e0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 regate function
676f0 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70 6c 65 definition imple
67700 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 20 20 20 mented by.**
67710 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 the C functions
67720 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 xStep and xFina
67730 6c 2e 20 54 68 65 20 66 69 72 73 74 20 66 6f 75 l. The first fou
67740 72 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 r parameters.**
67750 20 20 20 20 61 72 65 20 69 6e 74 65 72 70 72 65 are interpre
67760 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ted in the same
67770 77 61 79 20 61 73 20 74 68 65 20 66 69 72 73 74 way as the first
67780 20 34 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 4 parameters to
67790 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54 49 4f 4e .** FUNCTION
677a0 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c 49 4b 45 ()..**.** LIKE
677b0 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 FUNC(zName, nArg
677c0 2c 20 70 41 72 67 2c 20 66 6c 61 67 73 29 0a 2a , pArg, flags).*
677d0 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 * Used to cr
677e0 65 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75 eate a scalar fu
677f0 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f nction definitio
67800 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 n of a function
67810 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 74 68 zName .** th
67820 61 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20 at accepts nArg
67830 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 69 73 arguments and is
67840 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 implemented by
67850 61 20 63 61 6c 6c 20 74 6f 20 43 20 0a 2a 2a 20 a call to C .**
67860 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b function lik
67870 65 46 75 6e 63 2e 20 41 72 67 75 6d 65 6e 74 20 eFunc. Argument
67880 70 41 72 67 20 69 73 20 63 61 73 74 20 74 6f 20 pArg is cast to
67890 61 20 28 76 6f 69 64 20 2a 29 20 61 6e 64 20 6d a (void *) and m
678a0 61 64 65 0a 2a 2a 20 20 20 20 20 61 76 61 69 6c ade.** avail
678b0 61 62 6c 65 20 61 73 20 74 68 65 20 66 75 6e 63 able as the func
678c0 74 69 6f 6e 20 75 73 65 72 2d 64 61 74 61 20 28 tion user-data (
678d0 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
678e0 61 28 29 29 2e 20 54 68 65 0a 2a 2a 20 20 20 20 a()). The.**
678f0 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 20 76 FuncDef.flags v
67900 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 ariable is set t
67910 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 o the value pass
67920 65 64 20 61 73 20 74 68 65 20 66 6c 61 67 73 0a ed as the flags.
67930 2a 2a 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 ** parameter
67940 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 55 4e ..*/.#define FUN
67950 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 CTION(zName, nAr
67960 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 g, iArg, bNC, xF
67970 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 unc) \. {nArg,
67980 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 SQLITE_UTF8, bNC
67990 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 *8, SQLITE_INT_T
679a0 4f 5f 50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 O_PTR(iArg), 0,
679b0 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e xFunc, 0, 0, #zN
679c0 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 ame, 0}.#define
679d0 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 STR_FUNCTION(zNa
679e0 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 me, nArg, pArg,
679f0 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 bNC, xFunc) \.
67a00 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 {nArg, SQLITE_UT
67a10 46 38 2c 20 62 4e 43 2a 38 2c 20 70 41 72 67 2c F8, bNC*8, pArg,
67a20 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 0, xFunc, 0, 0,
67a30 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 #zName, 0}.#def
67a40 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 ine LIKEFUNC(zNa
67a50 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 66 me, nArg, arg, f
67a60 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41 72 67 2c lags) \. {nArg,
67a70 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 66 6c SQLITE_UTF8, fl
67a80 61 67 73 2c 20 28 76 6f 69 64 20 2a 29 61 72 67 ags, (void *)arg
67a90 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 , 0, likeFunc, 0
67aa0 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a , 0, #zName, 0}.
67ab0 23 64 65 66 69 6e 65 20 41 47 47 52 45 47 41 54 #define AGGREGAT
67ac0 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 E(zName, nArg, a
67ad0 72 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78 rg, nc, xStep, x
67ae0 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 Final) \. {nArg
67af0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e , SQLITE_UTF8, n
67b00 63 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f c*8, SQLITE_INT_
67b10 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20 TO_PTR(arg), 0,
67b20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c 0, xStep,xFinal,
67b30 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a 2a 2a #zName,0}../*.**
67b40 20 41 6c 6c 20 63 75 72 72 65 6e 74 20 73 61 76 All current sav
67b50 65 70 6f 69 6e 74 73 20 61 72 65 20 73 74 6f 72 epoints are stor
67b60 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c ed in a linked l
67b70 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 0a ist starting at.
67b80 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 ** sqlite3.pSave
67b90 70 6f 69 6e 74 2e 20 54 68 65 20 66 69 72 73 74 point. The first
67ba0 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 element in the
67bb0 6c 69 73 74 20 69 73 20 74 68 65 20 6d 6f 73 74 list is the most
67bc0 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f 70 65 recently.** ope
67bd0 6e 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 53 ned savepoint. S
67be0 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 61 64 avepoints are ad
67bf0 64 65 64 20 74 6f 20 74 68 65 20 6c 69 73 74 20 ded to the list
67c00 62 79 20 74 68 65 20 76 64 62 65 0a 2a 2a 20 4f by the vdbe.** O
67c10 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 P_Savepoint inst
67c20 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 ruction..*/.stru
67c30 63 74 20 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 ct Savepoint {.
67c40 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
67c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67c60 20 20 20 20 20 2f 2a 20 53 61 76 65 70 6f 69 6e /* Savepoin
67c70 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 72 6d t name (nul-term
67c80 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20 53 61 76 inated) */. Sav
67c90 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 20 epoint *pNext;
67ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67cb0 20 2f 2a 20 50 61 72 65 6e 74 20 73 61 76 65 70 /* Parent savep
67cc0 6f 69 6e 74 20 28 69 66 20 61 6e 79 29 20 2a 2f oint (if any) */
67cd0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 .};../*.** The f
67ce0 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 65 ollowing are use
67cf0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
67d00 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
67d10 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 29 2c ite3Savepoint(),
67d20 0a 2a 2a 20 61 6e 64 20 61 73 20 74 68 65 20 50 .** and as the P
67d30 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 1 argument to th
67d40 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 e OP_Savepoint i
67d50 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 nstruction..*/.#
67d60 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 define SAVEPOINT
67d70 5f 42 45 47 49 4e 20 20 20 20 20 20 30 0a 23 64 _BEGIN 0.#d
67d80 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f efine SAVEPOINT_
67d90 52 45 4c 45 41 53 45 20 20 20 20 31 0a 23 64 65 RELEASE 1.#de
67da0 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 fine SAVEPOINT_R
67db0 4f 4c 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a OLLBACK 2.../*
67dc0 0a 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20 .** Each SQLite
67dd0 6d 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20 module (virtual
67de0 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e table definition
67df0 29 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 ) is defined by
67e00 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f an.** instance o
67e10 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
67e20 73 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65 structure, store
67e30 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 d in the sqlite3
67e40 2e 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 .aModule.** hash
67e50 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 table..*/.struc
67e60 74 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e t Module {. con
67e70 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
67e80 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 e *pModule;
67e90 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f /* Callback po
67ea0 69 6e 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 inters */. cons
67eb0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 t char *zName;
67ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67ed0 20 2f 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 20 /* Name passed
67ee0 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 to create_module
67ef0 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 () */. void *pA
67f00 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ux;
67f10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
67f20 70 41 75 78 20 70 61 73 73 65 64 20 74 6f 20 63 pAux passed to c
67f30 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a reate_module() *
67f40 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 /. void (*xDest
67f50 72 6f 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 roy)(void *);
67f60 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 /* Modu
67f70 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 le destructor fu
67f80 6e 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a nction */.};../*
67f90 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
67fa0 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d about each colum
67fb0 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c n of an SQL tabl
67fc0 65 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 e is held in an
67fd0 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 instance.** of t
67fe0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a his structure..*
67ff0 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 /.struct Column
68000 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b {. char *zName;
68010 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
68020 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 this column */.
68030 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 Expr *pDflt;
68040 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c /* Default val
68050 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d ue of this colum
68060 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79 n */. char *zTy
68070 70 65 3b 20 20 20 20 20 2f 2a 20 44 61 74 61 20 pe; /* Data
68080 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 63 6f type for this co
68090 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a lumn */. char *
680a0 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f zColl; /* Co
680b0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
680c0 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20 . If NULL, use
680d0 74 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 the default */.
680e0 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 u8 notNull;
680f0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
68100 72 65 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c re is a NOT NULL
68110 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 constraint */.
68120 20 75 38 20 69 73 50 72 69 6d 4b 65 79 3b 20 20 u8 isPrimKey;
68130 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 /* True if thi
68140 73 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 s column is part
68150 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 of the PRIMARY
68160 4b 45 59 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 KEY */. char af
68170 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 finity; /* One
68180 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41 of the SQLITE_A
68190 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f FF_... values */
681a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
681b0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
681c0 45 0a 20 20 75 38 20 69 73 48 69 64 64 65 6e 3b E. u8 isHidden;
681d0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
681e0 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27 this column is '
681f0 68 69 64 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 hidden' */.#endi
68200 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 f.};../*.** A "C
68210 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 6e 63 ollating Sequenc
68220 65 22 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 e" is defined by
68230 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
68240 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a the following.**
68250 20 73 74 72 75 63 74 75 72 65 2e 20 43 6f 6e 63 structure. Conc
68260 65 70 74 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c eptually, a coll
68270 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63 ating sequence c
68280 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 6e 61 6d onsists of a nam
68290 65 20 61 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 e and.** a compa
682a0 72 69 73 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68 rison routine th
682b0 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6f at defines the o
682c0 72 64 65 72 20 6f 66 20 74 68 61 74 20 73 65 71 rder of that seq
682d0 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 uence..**.** The
682e0 72 65 20 6d 61 79 20 74 77 6f 20 73 65 70 61 72 re may two separ
682f0 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ate implementati
68300 6f 6e 73 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 ons of the colla
68310 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f tion function, o
68320 6e 65 0a 2a 2a 20 74 68 61 74 20 70 72 6f 63 65 ne.** that proce
68330 73 73 65 73 20 74 65 78 74 20 69 6e 20 55 54 46 sses text in UTF
68340 2d 38 20 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c -8 encoding (Col
68350 6c 53 65 71 2e 78 43 6d 70 29 20 61 6e 64 20 61 lSeq.xCmp) and a
68360 6e 6f 74 68 65 72 20 74 68 61 74 0a 2a 2a 20 70 nother that.** p
68370 72 6f 63 65 73 73 65 73 20 74 65 78 74 20 65 6e rocesses text en
68380 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 31 36 20 coded in UTF-16
68390 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29 (CollSeq.xCmp16)
683a0 2c 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63 68 , using the mach
683b0 69 6e 65 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79 ine.** native by
683c0 74 65 20 6f 72 64 65 72 2e 20 57 68 65 6e 20 61 te order. When a
683d0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
683e0 6e 63 65 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 nce is invoked,
683f0 53 51 4c 69 74 65 20 73 65 6c 65 63 74 73 0a 2a SQLite selects.*
68400 2a 20 74 68 65 20 76 65 72 73 69 6f 6e 20 74 68 * the version th
68410 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20 at will require
68420 74 68 65 20 6c 65 61 73 74 20 65 78 70 65 6e 73 the least expens
68430 69 76 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 ive encoding.**
68440 74 72 61 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66 translations, if
68450 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 any..**.** The
68460 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 20 6d 65 CollSeq.pUser me
68470 6d 62 65 72 20 76 61 72 69 61 62 6c 65 20 69 73 mber variable is
68480 20 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d 65 an extra parame
68490 74 65 72 20 74 68 61 74 20 70 61 73 73 65 64 20 ter that passed
684a0 69 6e 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 in.** as the fir
684b0 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 st argument to t
684c0 68 65 20 55 54 46 2d 38 20 63 6f 6d 70 61 72 69 he UTF-8 compari
684d0 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43 son function, xC
684e0 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 mp..** CollSeq.p
684f0 55 73 65 72 31 36 20 69 73 20 74 68 65 20 65 71 User16 is the eq
68500 75 69 76 61 6c 65 6e 74 20 66 6f 72 20 74 68 65 uivalent for the
68510 20 55 54 46 2d 31 36 20 63 6f 6d 70 61 72 69 73 UTF-16 comparis
68520 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 on function,.**
68530 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 xCmp16..**.** If
68540 20 62 6f 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43 both CollSeq.xC
68550 6d 70 20 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 mp and CollSeq.x
68560 43 6d 70 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20 Cmp16 are NULL,
68570 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 it means that th
68580 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 e.** collating s
68590 65 71 75 65 6e 63 65 20 69 73 20 75 6e 64 65 66 equence is undef
685a0 69 6e 65 64 2e 20 20 49 6e 64 69 63 65 73 20 62 ined. Indices b
685b0 75 69 6c 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66 uilt on an undef
685c0 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e ined.** collatin
685d0 67 20 73 65 71 75 65 6e 63 65 20 6d 61 79 20 6e g sequence may n
685e0 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 ot be read or wr
685f0 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 itten..*/.struct
68600 20 43 6f 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61 CollSeq {. cha
68610 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 r *zName;
68620 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
68630 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 e collating sequ
68640 65 6e 63 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f ence, UTF-8 enco
68650 64 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b ded */. u8 enc;
68660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
68670 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 * Text encoding
68680 68 61 6e 64 6c 65 64 20 62 79 20 78 43 6d 70 28 handled by xCmp(
68690 29 20 2a 2f 0a 20 20 75 38 20 74 79 70 65 3b 20 ) */. u8 type;
686a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
686b0 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 One of the SQLIT
686c0 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 E_COLL_... value
686d0 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 s below */. voi
686e0 64 20 2a 70 55 73 65 72 3b 20 20 20 20 20 20 20 d *pUser;
686f0 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 /* First argu
68700 6d 65 6e 74 20 74 6f 20 78 43 6d 70 28 29 20 2a ment to xCmp() *
68710 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 /. int (*xCmp)(
68720 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 void*,int, const
68730 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e void*, int, con
68740 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 st void*);. voi
68750 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 d (*xDel)(void*)
68760 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 ; /* Destructor
68770 20 66 6f 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b for pUser */.};
68780 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 ../*.** Allowed
68790 76 61 6c 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65 values of CollSe
687a0 71 2e 74 79 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 q.type:.*/.#defi
687b0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 ne SQLITE_COLL_B
687c0 49 4e 41 52 59 20 20 31 20 20 2f 2a 20 54 68 65 INARY 1 /* The
687d0 20 64 65 66 61 75 6c 74 20 6d 65 6d 63 6d 70 28 default memcmp(
687e0 29 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 ) collating sequ
687f0 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ence */.#define
68800 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 SQLITE_COLL_NOCA
68810 53 45 20 20 32 20 20 2f 2a 20 54 68 65 20 62 75 SE 2 /* The bu
68820 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f ilt-in NOCASE co
68830 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
68840 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
68850 54 45 5f 43 4f 4c 4c 5f 52 45 56 45 52 53 45 20 TE_COLL_REVERSE
68860 33 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 3 /* The built-
68870 69 6e 20 52 45 56 45 52 53 45 20 63 6f 6c 6c 61 in REVERSE colla
68880 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f ting sequence */
68890 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
688a0 43 4f 4c 4c 5f 55 53 45 52 20 20 20 20 30 20 20 COLL_USER 0
688b0 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20 75 73 65 /* Any other use
688c0 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 r-defined collat
688d0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a ing sequence */.
688e0 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74 20 6f 72 ./*.** A sort or
688f0 64 65 72 20 63 61 6e 20 62 65 20 65 69 74 68 65 der can be eithe
68900 72 20 41 53 43 20 6f 72 20 44 45 53 43 2e 0a 2a r ASC or DESC..*
68910 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
68920 5f 53 4f 5f 41 53 43 20 20 20 20 20 20 20 30 20 _SO_ASC 0
68930 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65 /* Sort in asce
68940 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 23 nding order */.#
68950 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f define SQLITE_SO
68960 5f 44 45 53 43 20 20 20 20 20 20 31 20 20 2f 2a _DESC 1 /*
68970 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69 Sort in ascendi
68980 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a ng order */../*.
68990 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 ** Column affini
689a0 74 79 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 ty types..**.**
689b0 54 68 65 73 65 20 75 73 65 64 20 74 6f 20 68 61 These used to ha
689c0 76 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 ve mnemonic name
689d0 20 6c 69 6b 65 20 27 69 27 20 66 6f 72 20 53 51 like 'i' for SQ
689e0 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
689f0 20 61 6e 64 0a 2a 2a 20 27 74 27 20 66 6f 72 20 and.** 't' for
68a00 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2e SQLITE_AFF_TEXT.
68a10 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 61 76 But we can sav
68a20 65 20 61 20 6c 69 74 74 6c 65 20 73 70 61 63 65 e a little space
68a30 20 61 6e 64 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 and improve.**
68a40 74 68 65 20 73 70 65 65 64 20 61 20 6c 69 74 74 the speed a litt
68a50 6c 65 20 62 79 20 6e 75 6d 62 65 72 69 6e 67 20 le by numbering
68a60 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 the values conse
68a70 63 75 74 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a cutively. .**.*
68a80 2a 20 42 75 74 20 72 61 74 68 65 72 20 74 68 61 * But rather tha
68a90 6e 20 73 74 61 72 74 20 77 69 74 68 20 30 20 6f n start with 0 o
68aa0 72 20 31 2c 20 77 65 20 62 65 67 69 6e 20 77 69 r 1, we begin wi
68ab0 74 68 20 27 61 27 2e 20 20 54 68 61 74 20 77 61 th 'a'. That wa
68ac0 79 2c 0a 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 y,.** when multi
68ad0 70 6c 65 20 61 66 66 69 6e 69 74 79 20 74 79 70 ple affinity typ
68ae0 65 73 20 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 es are concatena
68af0 74 65 64 20 69 6e 74 6f 20 61 20 73 74 72 69 6e ted into a strin
68b00 67 20 61 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 g and.** used as
68b10 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 2c the P4 operand,
68b20 20 74 68 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f they will be mo
68b30 72 65 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a re readable..**.
68b40 2a 2a 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 ** Note also tha
68b50 74 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 t the numeric ty
68b60 70 65 73 20 61 72 65 20 67 72 6f 75 70 65 64 20 pes are grouped
68b70 74 6f 67 65 74 68 65 72 20 73 6f 20 74 68 61 74 together so that
68b80 20 74 65 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 testing.** for
68b90 61 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 69 a numeric type i
68ba0 73 20 61 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 s a single compa
68bb0 72 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e rison..*/.#defin
68bc0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 e SQLITE_AFF_TEX
68bd0 54 20 20 20 20 20 27 61 27 0a 23 64 65 66 69 6e T 'a'.#defin
68be0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e e SQLITE_AFF_NON
68bf0 45 20 20 20 20 20 27 62 27 0a 23 64 65 66 69 6e E 'b'.#defin
68c00 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d e SQLITE_AFF_NUM
68c10 45 52 49 43 20 20 27 63 27 0a 23 64 65 66 69 6e ERIC 'c'.#defin
68c20 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 e SQLITE_AFF_INT
68c30 45 47 45 52 20 20 27 64 27 0a 23 64 65 66 69 6e EGER 'd'.#defin
68c40 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 e SQLITE_AFF_REA
68c50 4c 20 20 20 20 20 27 65 27 0a 0a 23 64 65 66 69 L 'e'..#defi
68c60 6e 65 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 ne sqlite3IsNume
68c70 72 69 63 41 66 66 69 6e 69 74 79 28 58 29 20 20 ricAffinity(X)
68c80 28 28 58 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 ((X)>=SQLITE_AFF
68c90 5f 4e 55 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a _NUMERIC)../*.**
68ca0 20 54 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f The SQLITE_AFF_
68cb0 4d 41 53 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b MASK values mask
68cc0 73 20 6f 66 66 20 74 68 65 20 73 69 67 6e 69 66 s off the signif
68cd0 69 63 61 6e 74 20 62 69 74 73 20 6f 66 20 61 6e icant bits of an
68ce0 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 76 61 6c .** affinity val
68cf0 75 65 2e 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ue. .*/.#define
68d00 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 SQLITE_AFF_MASK
68d10 20 20 20 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 0x67../*.**
68d20 41 64 64 69 74 69 6f 6e 61 6c 20 62 69 74 20 76 Additional bit v
68d30 61 6c 75 65 73 20 74 68 61 74 20 63 61 6e 20 62 alues that can b
68d40 65 20 4f 52 65 64 20 77 69 74 68 20 61 6e 20 61 e ORed with an a
68d50 66 66 69 6e 69 74 79 20 77 69 74 68 6f 75 74 0a ffinity without.
68d60 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 ** changing the
68d70 61 66 66 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 affinity..*/.#de
68d80 66 69 6e 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 fine SQLITE_JUMP
68d90 49 46 4e 55 4c 4c 20 20 20 30 78 30 38 20 20 2f IFNULL 0x08 /
68da0 2a 20 6a 75 6d 70 73 20 69 66 20 65 69 74 68 65 * jumps if eithe
68db0 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c r operand is NUL
68dc0 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c L */.#define SQL
68dd0 49 54 45 5f 53 54 4f 52 45 50 32 20 20 20 20 20 ITE_STOREP2
68de0 20 30 78 31 30 20 20 2f 2a 20 53 74 6f 72 65 20 0x10 /* Store
68df0 72 65 73 75 6c 74 20 69 6e 20 72 65 67 5b 50 32 result in reg[P2
68e00 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 6a 75 ] rather than ju
68e10 6d 70 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 mp */../*.** Eac
68e20 68 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20 72 h SQL table is r
68e30 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 epresented in me
68e40 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e 73 74 61 mory by an insta
68e50 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f nce of the.** fo
68e60 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
68e70 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 7a e..**.** Table.z
68e80 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 Name is the name
68e90 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 of the table.
68ea0 54 68 65 20 63 61 73 65 20 6f 66 20 74 68 65 20 The case of the
68eb0 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43 52 45 41 original.** CREA
68ec0 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 TE TABLE stateme
68ed0 6e 74 20 69 73 20 73 74 6f 72 65 64 2c 20 62 75 nt is stored, bu
68ee0 74 20 63 61 73 65 20 69 73 20 6e 6f 74 20 73 69 t case is not si
68ef0 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 0a 2a 2a gnificant for.**
68f00 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a comparisons..**
68f10 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f 6c 20 69 .** Table.nCol i
68f20 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
68f30 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 columns in this
68f40 74 61 62 6c 65 2e 20 20 54 61 62 6c 65 2e 61 43 table. Table.aC
68f50 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 ol is a.** point
68f60 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f er to an array o
68f70 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75 63 74 75 f Column structu
68f80 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20 65 61 63 res, one for eac
68f90 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 h column..**.**
68fa0 49 66 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 If the table has
68fb0 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d an INTEGER PRIM
68fc0 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 54 61 ARY KEY, then Ta
68fd0 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 74 68 65 ble.iPKey is the
68fe0 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 index of.** the
68ff0 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 column that is
69000 74 68 61 74 20 6b 65 79 2e 20 20 20 4f 74 68 65 that key. Othe
69010 72 77 69 73 65 20 54 61 62 6c 65 2e 69 50 4b 65 rwise Table.iPKe
69020 79 20 69 73 20 6e 65 67 61 74 69 76 65 2e 20 20 y is negative.
69030 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 Note.** that the
69040 20 64 61 74 61 74 79 70 65 20 6f 66 20 74 68 65 datatype of the
69050 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 PRIMARY KEY mus
69060 74 20 62 65 20 49 4e 54 45 47 45 52 20 66 6f 72 t be INTEGER for
69070 20 74 68 69 73 20 66 69 65 6c 64 20 74 6f 0a 2a this field to.*
69080 2a 20 62 65 20 73 65 74 2e 20 20 41 6e 20 49 4e * be set. An IN
69090 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
690a0 59 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 Y is used as the
690b0 20 72 6f 77 69 64 20 66 6f 72 20 65 61 63 68 20 rowid for each
690c0 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 row of.** the ta
690d0 62 6c 65 2e 20 20 49 66 20 61 20 74 61 62 6c 65 ble. If a table
690e0 20 68 61 73 20 6e 6f 20 49 4e 54 45 47 45 52 20 has no INTEGER
690f0 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 PRIMARY KEY, the
69100 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 n a random rowid
69110 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61 74 65 64 .** is generated
69120 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 for each row of
69130 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 46 5f the table. TF_
69140 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 69 73 HasPrimaryKey is
69150 20 73 65 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 set if.** the t
69160 61 62 6c 65 20 68 61 73 20 61 6e 79 20 50 52 49 able has any PRI
69170 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54 45 47 45 MARY KEY, INTEGE
69180 52 20 6f 72 20 6f 74 68 65 72 77 69 73 65 2e 0a R or otherwise..
69190 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74 6e 75 6d **.** Table.tnum
691a0 20 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d is the page num
691b0 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 ber for the root
691c0 20 42 54 72 65 65 20 70 61 67 65 20 6f 66 20 74 BTree page of t
691d0 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 0a he table in the.
691e0 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
691f0 2e 20 20 49 66 20 54 61 62 6c 65 2e 69 44 62 20 . If Table.iDb
69200 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 is the index of
69210 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 the database tab
69220 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e le backend.** in
69230 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d 2e 20 20 sqlite.aDb[].
69240 30 20 69 73 20 66 6f 72 20 74 68 65 20 6d 61 69 0 is for the mai
69250 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 31 n database and 1
69260 20 69 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 is for the file
69270 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64 73 20 74 that.** holds t
69280 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 emporary tables
69290 61 6e 64 20 69 6e 64 69 63 65 73 2e 20 20 49 66 and indices. If
692a0 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 69 73 TF_Ephemeral is
692b0 20 73 65 74 0a 2a 2a 20 74 68 65 6e 20 74 68 65 set.** then the
692c0 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 table is stored
692d0 20 69 6e 20 61 20 66 69 6c 65 20 74 68 61 74 20 in a file that
692e0 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 is automatically
692f0 20 64 65 6c 65 74 65 64 0a 2a 2a 20 77 68 65 6e deleted.** when
69300 20 74 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 the VDBE cursor
69310 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 69 73 to the table is
69320 20 63 6c 6f 73 65 64 2e 20 20 49 6e 20 74 68 69 closed. In thi
69330 73 20 63 61 73 65 20 54 61 62 6c 65 2e 74 6e 75 s case Table.tnu
69340 6d 20 0a 2a 2a 20 72 65 66 65 72 73 20 56 44 42 m .** refers VDB
69350 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 E cursor number
69360 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 74 that holds the t
69370 61 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 able open, not t
69380 6f 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 o the root.** pa
69390 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 72 61 6e ge number. Tran
693a0 73 69 65 6e 74 20 74 61 62 6c 65 73 20 61 72 65 sient tables are
693b0 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 used to hold th
693c0 65 20 72 65 73 75 6c 74 73 20 6f 66 20 61 0a 2a e results of a.*
693d0 2a 20 73 75 62 2d 71 75 65 72 79 20 74 68 61 74 * sub-query that
693e0 20 61 70 70 65 61 72 73 20 69 6e 73 74 65 61 64 appears instead
693f0 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 of a real table
69400 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 46 52 4f name in the FRO
69410 4d 20 63 6c 61 75 73 65 20 0a 2a 2a 20 6f 66 20 M clause .** of
69420 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 a SELECT stateme
69430 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 nt..*/.struct Ta
69440 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 ble {. sqlite3
69450 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 *db; /*
69460 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 Associated datab
69470 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 ase connection.
69480 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 2e 20 Might be NULL.
69490 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 */. char *zName
694a0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d ; /* Nam
694b0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f e of the table o
694c0 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 r view */. int
694d0 69 50 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 iPKey;
694e0 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 65 67 61 74 /* If not negat
694f0 69 76 65 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50 ive, use aCol[iP
69500 4b 65 79 5d 20 61 73 20 74 68 65 20 70 72 69 6d Key] as the prim
69510 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 ary key */. int
69520 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 nCol;
69530 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
69540 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 olumns in this t
69550 61 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e able */. Column
69560 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f *aCol; /
69570 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 * Information ab
69580 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 out each column
69590 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 */. Index *pInd
695a0 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 ex; /* Lis
695b0 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65 73 t of SQL indexes
695c0 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 on this table.
695d0 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 */. int tnum;
695e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f /* Roo
695f0 74 20 42 54 72 65 65 20 6e 6f 64 65 20 66 6f 72 t BTree node for
69600 20 74 68 69 73 20 74 61 62 6c 65 20 28 73 65 65 this table (see
69610 20 6e 6f 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a note above) */.
69620 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 Select *pSelec
69630 74 3b 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 66 t; /* NULL f
69640 6f 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69 6e or tables. Poin
69650 74 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f 6e ts to definition
69660 20 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 if a view. */.
69670 20 75 31 36 20 6e 52 65 66 3b 20 20 20 20 20 20 u16 nRef;
69680 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
69690 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 of pointers to t
696a0 68 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20 75 his Table */. u
696b0 38 20 74 61 62 46 6c 61 67 73 3b 20 20 20 20 20 8 tabFlags;
696c0 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 /* Mask of T
696d0 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 F_* values */.
696e0 75 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20 u8 keyConf;
696f0 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 /* What to
69700 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 75 6e do in case of un
69710 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 iqueness conflic
69720 74 20 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 t on iPKey */.
69730 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 Trigger *pTrigge
69740 72 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 r; /* List of
69750 53 51 4c 20 74 72 69 67 67 65 72 73 20 6f 6e 20 SQL triggers on
69760 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 this table */.
69770 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 FKey *pFKey;
69780 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c /* Linked l
69790 69 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69 ist of all forei
697a0 67 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73 20 gn keys in this
697b0 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 table */. char
697c0 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20 20 *zColAff;
697d0 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69 /* String defini
697e0 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 ng the affinity
697f0 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a of each column *
69800 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
69810 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45 78 _OMIT_CHECK. Ex
69820 70 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20 20 pr *pCheck;
69830 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 /* The AND of
69840 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 all CHECK const
69850 72 61 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 raints */.#endif
69860 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
69870 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a OMIT_ALTERTABLE.
69880 20 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 int addColOffs
69890 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 et; /* Offset
698a0 20 69 6e 20 43 52 45 41 54 45 20 54 41 42 4c 45 in CREATE TABLE
698b0 20 73 74 6d 74 20 74 6f 20 61 64 64 20 61 20 6e stmt to add a n
698c0 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e ew column */.#en
698d0 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 dif.#ifndef SQLI
698e0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
698f0 41 42 4c 45 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 ABLE. Module *p
69900 4d 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 Mod; /* P
69910 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69 6d ointer to the im
69920 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
69930 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 the module */.
69940 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
69950 74 61 62 3b 20 2f 2a 20 50 6f 69 6e 74 65 72 20 tab; /* Pointer
69960 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 6e to the module in
69970 73 74 61 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20 stance */. int
69980 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 20 20 20 20 nModuleArg;
69990 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 /* Number of ar
699a0 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 6d guments to the m
699b0 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 odule */. char
699c0 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 **azModuleArg;
699d0 2f 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d /* Text of all m
699e0 6f 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 odule args. [0]
699f0 69 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a is module name *
69a00 2f 0a 23 65 6e 64 69 66 0a 20 20 53 63 68 65 6d /.#endif. Schem
69a10 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 a *pSchema;
69a20 2f 2a 20 53 63 68 65 6d 61 20 74 68 61 74 20 63 /* Schema that c
69a30 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 74 61 62 ontains this tab
69a40 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 le */. Table *p
69a50 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20 NextZombie; /*
69a60 4e 65 78 74 20 6f 6e 20 74 68 65 20 50 61 72 73 Next on the Pars
69a70 65 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73 e.pZombieTab lis
69a80 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 t */.};../*.** A
69a90 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f llowed values fo
69aa0 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67 73 2e r Tabe.tabFlags.
69ab0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 52 .*/.#define TF_R
69ac0 65 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 20 30 eadonly 0
69ad0 78 30 31 20 20 20 20 2f 2a 20 52 65 61 64 2d 6f x01 /* Read-o
69ae0 6e 6c 79 20 73 79 73 74 65 6d 20 74 61 62 6c 65 nly system table
69af0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 45 */.#define TF_E
69b00 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 30 phemeral 0
69b10 78 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 70 68 x02 /* An eph
69b20 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a 2f 0a emeral table */.
69b30 23 64 65 66 69 6e 65 20 54 46 5f 48 61 73 50 72 #define TF_HasPr
69b40 69 6d 61 72 79 4b 65 79 20 20 20 30 78 30 34 20 imaryKey 0x04
69b50 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 /* Table has
69b60 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f a primary key */
69b70 0a 23 64 65 66 69 6e 65 20 54 46 5f 41 75 74 6f .#define TF_Auto
69b80 69 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 30 38 increment 0x08
69b90 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 70 /* Integer p
69ba0 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61 75 rimary key is au
69bb0 74 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23 toincrement */.#
69bc0 64 65 66 69 6e 65 20 54 46 5f 56 69 72 74 75 61 define TF_Virtua
69bd0 6c 20 20 20 20 20 20 20 20 20 30 78 31 30 20 20 l 0x10
69be0 20 20 2f 2a 20 49 73 20 61 20 76 69 72 74 75 61 /* Is a virtua
69bf0 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 l table */.#defi
69c00 6e 65 20 54 46 5f 4e 65 65 64 4d 65 74 61 64 61 ne TF_NeedMetada
69c10 74 61 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a ta 0x20 /*
69c20 20 61 43 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 6e aCol[].zType an
69c30 64 20 61 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 6d d aCol[].pColl m
69c40 69 73 73 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a 0a issing */..../*.
69c50 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20 77 ** Test to see w
69c60 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 hether or not a
69c70 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 75 table is a virtu
69c80 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 al table. This
69c90 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20 is.** done as a
69ca0 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 74 macro so that it
69cb0 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a will be optimiz
69cc0 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72 74 ed out when virt
69cd0 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70 ual.** table sup
69ce0 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64 20 port is omitted
69cf0 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a from the build..
69d00 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
69d10 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
69d20 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 49 73 BLE.# define Is
69d30 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 20 Virtual(X)
69d40 28 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 73 20 (((X)->tabFlags
69d50 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 & TF_Virtual)!=0
69d60 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 69 ).# define IsHi
69d70 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 28 ddenColumn(X) ((
69d80 58 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23 65 X)->isHidden).#e
69d90 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 73 lse.# define Is
69da0 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 20 Virtual(X)
69db0 30 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 69 0.# define IsHi
69dc0 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 0a ddenColumn(X) 0.
69dd0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 #endif../*.** Ea
69de0 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 ch foreign key c
69df0 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e 20 onstraint is an
69e00 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
69e10 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
69e20 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f 72 ure..**.** A for
69e30 65 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73 6f eign key is asso
69e40 63 69 61 74 65 64 20 77 69 74 68 20 74 77 6f 20 ciated with two
69e50 74 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66 72 tables. The "fr
69e60 6f 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 om" table is.**
69e70 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 the table that c
69e80 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46 45 ontains the REFE
69e90 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74 68 RENCES clause th
69ea0 61 74 20 63 72 65 61 74 65 73 20 74 68 65 20 66 at creates the f
69eb0 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 20 oreign.** key.
69ec0 54 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20 69 The "to" table i
69ed0 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 s the table that
69ee0 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 is named in the
69ef0 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 REFERENCES clau
69f00 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 se..** Consider
69f10 74 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a this example:.**
69f20 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 .** CREATE T
69f30 41 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20 20 ABLE ex1(.**
69f40 20 20 20 61 20 49 4e 54 45 47 45 52 20 50 52 49 a INTEGER PRI
69f50 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 20 MARY KEY,.**
69f60 20 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f 4e b INTEGER CON
69f70 53 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46 45 STRAINT fk1 REFE
69f80 52 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a 2a RENCES ex2(x).**
69f90 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 6f );.**.** Fo
69fa0 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22 66 r foreign key "f
69fb0 6b 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74 61 k1", the from-ta
69fc0 62 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e 64 ble is "ex1" and
69fd0 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 the to-table is
69fe0 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 61 "ex2"..**.** Ea
69ff0 63 68 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c ch REFERENCES cl
6a000 61 75 73 65 20 67 65 6e 65 72 61 74 65 73 20 61 ause generates a
6a010 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
6a020 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
6a030 63 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20 69 cture.** which i
6a040 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 s attached to th
6a050 65 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 54 e from-table. T
6a060 68 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65 64 he to-table need
6a070 20 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e 0a not exist when.
6a080 2a 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c ** the from-tabl
6a090 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 e is created. T
6a0a0 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 he existence of
6a0b0 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 the to-table is
6a0c0 6e 6f 74 20 63 68 65 63 6b 65 64 0a 2a 2a 20 75 not checked.** u
6a0d0 6e 74 69 6c 20 61 6e 20 61 74 74 65 6d 70 74 20 ntil an attempt
6a0e0 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 is made to inser
6a0f0 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 t data into the
6a100 66 72 6f 6d 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a from-table..**.*
6a110 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 61 46 4b * The sqlite.aFK
6a120 65 79 20 68 61 73 68 20 74 61 62 6c 65 20 73 74 ey hash table st
6a130 6f 72 65 73 20 70 6f 69 6e 74 65 72 73 20 74 6f ores pointers to
6a140 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a this structure.
6a150 2a 2a 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d ** given the nam
6a160 65 20 6f 66 20 61 20 74 6f 2d 74 61 62 6c 65 2e e of a to-table.
6a170 20 20 46 6f 72 20 65 61 63 68 20 74 6f 2d 74 61 For each to-ta
6a180 62 6c 65 2c 20 61 6c 6c 20 66 6f 72 65 69 67 6e ble, all foreign
6a190 20 6b 65 79 73 0a 2a 2a 20 61 73 73 6f 63 69 61 keys.** associa
6a1a0 74 65 64 20 77 69 74 68 20 74 68 61 74 20 74 61 ted with that ta
6a1b0 62 6c 65 20 61 72 65 20 6f 6e 20 61 20 6c 69 6e ble are on a lin
6a1c0 6b 65 64 20 6c 69 73 74 20 75 73 69 6e 67 20 74 ked list using t
6a1d0 68 65 20 46 4b 65 79 2e 70 4e 65 78 74 54 6f 0a he FKey.pNextTo.
6a1e0 2a 2a 20 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74 72 ** field..*/.str
6a1f0 75 63 74 20 46 4b 65 79 20 7b 0a 20 20 54 61 62 uct FKey {. Tab
6a200 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 20 20 20 2f le *pFrom; /
6a210 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 * The table that
6a220 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 contains the RE
6a230 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 FERENCES clause
6a240 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 */. FKey *pNext
6a250 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 From; /* Next f
6a260 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 oreign key in pF
6a270 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a rom */. char *z
6a280 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 To; /* Na
6a290 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74 me of table that
6a2a0 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 the key points
6a2b0 74 6f 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e to */. FKey *pN
6a2c0 65 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 extTo; /* Nex
6a2d0 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 68 t foreign key th
6a2e0 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 7a 54 6f at points to zTo
6a2f0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 */. int nCol;
6a300 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6a310 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
6a320 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 73 74 this key */. st
6a330 72 75 63 74 20 73 43 6f 6c 4d 61 70 20 7b 20 20 ruct sColMap {
6a340 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20 63 6f /* Mapping of co
6a350 6c 75 6d 6e 73 20 69 6e 20 70 46 72 6f 6d 20 74 lumns in pFrom t
6a360 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a 54 6f o columns in zTo
6a370 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 46 72 6f */. int iFro
6a380 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e m; /* In
6a390 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e dex of column in
6a3a0 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 68 pFrom */. ch
6a3b0 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 ar *zCol;
6a3c0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 /* Name of colu
6a3d0 6d 6e 20 69 6e 20 7a 54 6f 2e 20 20 49 66 20 30 mn in zTo. If 0
6a3e0 20 75 73 65 20 50 52 49 4d 41 52 59 20 4b 45 59 use PRIMARY KEY
6a3f0 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 20 20 */. } *aCol;
6a400 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 /* One e
6a410 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f 66 ntry for each of
6a420 20 6e 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73 20 2a nCol column s *
6a430 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 72 65 /. u8 isDeferre
6a440 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 d; /* True if
6a450 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 constraint chec
6a460 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 king is deferred
6a470 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a till COMMIT */.
6a480 20 20 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b u8 updateConf;
6a490 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 /* How to re
6a4a0 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 solve conflicts
6a4b0 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50 that occur on UP
6a4c0 44 41 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c DATE */. u8 del
6a4d0 65 74 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 eteConf; /* H
6a4e0 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f ow to resolve co
6a4f0 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63 nflicts that occ
6a500 75 72 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a ur on DELETE */.
6a510 20 20 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b u8 insertConf;
6a520 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 /* How to re
6a530 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 solve conflicts
6a540 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e that occur on IN
6a550 53 45 52 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a SERT */.};../*.*
6a560 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74 * SQLite support
6a570 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74 s many different
6a580 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 ways to resolve
6a590 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a a constraint.**
6a5a0 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 error. ROLLBAC
6a5b0 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 K processing mea
6a5c0 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72 ns that a constr
6a5d0 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a aint violation.*
6a5e0 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f 70 65 * causes the ope
6a5f0 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 ration in proces
6a600 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f s to fail and fo
6a610 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 r the current tr
6a620 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 ansaction.** to
6a630 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 be rolled back.
6a640 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e ABORT processin
6a650 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 g means the oper
6a660 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 ation in process
6a670 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e .** fails and an
6a680 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 y prior changes
6a690 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 from that one op
6a6a0 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b eration are back
6a6b0 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 ed out,.** but t
6a6c0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
6a6d0 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 s not rolled bac
6a6e0 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73 k. FAIL process
6a6f0 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a ing means that.*
6a700 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 * the operation
6a710 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70 in progress stop
6a720 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e s and returns an
6a730 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 error code. Bu
6a740 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 t prior.** chang
6a750 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 73 61 es due to the sa
6a760 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 me operation are
6a770 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20 not backed out
6a780 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a and no rollback.
6a790 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f ** occurs. IGNO
6a7a0 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 RE means that th
6a7b0 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77 e particular row
6a7c0 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 that caused the
6a7d0 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 constraint.** e
6a7e0 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 rror is not inse
6a7f0 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e rted or updated.
6a800 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e Processing con
6a810 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 tinues and no er
6a820 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e ror.** is return
6a830 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61 ed. REPLACE mea
6a840 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73 74 ns that preexist
6a850 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f 77 ing database row
6a860 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a s that caused.**
6a870 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 a UNIQUE constr
6a880 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 aint violation a
6a890 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 re removed so th
6a8a0 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72 at the new inser
6a8b0 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 t or.** update c
6a8c0 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f an proceed. Pro
6a8d0 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 cessing continue
6a8e0 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 s and no error i
6a8f0 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a s reported..**.*
6a900 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e * RESTRICT, SETN
6a910 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45 ULL, and CASCADE
6a920 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f actions apply o
6a930 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b nly to foreign k
6a940 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 eys..** RESTRICT
6a950 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
6a960 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 ABORT for IMMEDI
6a970 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 ATE foreign keys
6a980 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 and the.** same
6a990 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 as ROLLBACK for
6a9a0 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20 DEFERRED keys.
6a9b0 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 SETNULL means t
6a9c0 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a hat the foreign.
6a9d0 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f ** key is set to
6a9e0 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 NULL. CASCADE
6a9f0 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c means that a DEL
6aa00 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66 ETE or UPDATE of
6aa10 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 the.** referenc
6aa20 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 ed table row is
6aa30 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20 propagated into
6aa40 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c the row that hol
6aa50 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 ds the.** foreig
6aa60 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 n key..** .** Th
6aa70 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 e following symb
6aa80 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65 20 olic values are
6aa90 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 used to record w
6aaa0 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 hich type.** of
6aab0 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a action to take..
6aac0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f */.#define OE_No
6aad0 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68 ne 0 /* Th
6aae0 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 ere is no constr
6aaf0 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f aint to check */
6ab00 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c .#define OE_Roll
6ab10 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c back 1 /* Fail
6ab20 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 the operation a
6ab30 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 nd rollback the
6ab40 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 transaction */.#
6ab50 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 define OE_Abort
6ab60 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 2 /* Back o
6ab70 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20 64 ut changes but d
6ab80 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 o no rollback tr
6ab90 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 ansaction */.#de
6aba0 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 fine OE_Fail
6abb0 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 3 /* Stop the
6abc0 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c operation but l
6abd0 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 eave all prior c
6abe0 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e hanges */.#defin
6abf0 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 e OE_Ignore 4
6ac00 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 /* Ignore the
6ac10 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f error. Do not do
6ac20 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 the INSERT or U
6ac30 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 PDATE */.#define
6ac40 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20 OE_Replace 5
6ac50 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74 /* Delete exist
6ac60 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e ing record, then
6ac70 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 do INSERT or UP
6ac80 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 DATE */..#define
6ac90 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20 20 OE_Restrict 6
6aca0 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 /* OE_Abort for
6acb0 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 IMMEDIATE, OE_R
6acc0 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45 ollback for DEFE
6acd0 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 RRED */.#define
6ace0 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 OE_SetNull 7
6acf0 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 /* Set the forei
6ad00 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 gn key value to
6ad10 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NULL */.#define
6ad20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20 OE_SetDflt 8
6ad30 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 /* Set the forei
6ad40 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 gn key value to
6ad50 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 its default */.#
6ad60 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 define OE_Cascad
6ad70 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64 e 9 /* Cascad
6ad80 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f e the changes */
6ad90 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 ..#define OE_Def
6ada0 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 ault 99 /* Do
6adb0 77 68 61 74 65 76 65 72 20 74 68 65 20 64 65 66 whatever the def
6adc0 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a ault action is *
6add0 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 /.../*.** An ins
6ade0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
6adf0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
6ae00 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 is passed as th
6ae10 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d e first.** argum
6ae20 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64 ent to sqlite3Vd
6ae30 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 beKeyCompare and
6ae40 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 is used to cont
6ae50 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 rol the .** comp
6ae60 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 arison of the tw
6ae70 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f o index keys..*/
6ae80 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 .struct KeyInfo
6ae90 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
6aea0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
6aeb0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
6aec0 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 on */. u8 enc;
6aed0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
6aee0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f ext encoding - o
6aef0 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55 ne of the TEXT_U
6af00 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 tf* values */.
6af10 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 u16 nField;
6af20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6af30 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c entries in aCol
6af40 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f l[] */. u8 *aSo
6af50 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a 20 rtOrder; /*
6af60 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20 61 53 If defined an aS
6af70 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 74 ortOrder[i] is t
6af80 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43 20 2a rue, sort DESC *
6af90 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f /. CollSeq *aCo
6afa0 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 ll[1]; /* Colla
6afb0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f ting sequence fo
6afc0 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 r each term of t
6afd0 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a he key */.};../*
6afe0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
6aff0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
6b000 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 structure holds
6b010 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
6b020 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 ut a.** single i
6b030 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68 61 74 ndex record that
6b040 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
6b050 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69 6e 74 n parsed out int
6b060 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 o individual.**
6b070 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 values..**.** A
6b080 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a record is an obj
6b090 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ect that contain
6b0a0 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 s one or more fi
6b0b0 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a elds of data..**
6b0c0 20 52 65 63 6f 72 64 73 20 61 72 65 20 75 73 65 Records are use
6b0d0 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 d to store the c
6b0e0 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 62 6c ontent of a tabl
6b0f0 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f e row and to sto
6b100 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 re.** the key of
6b110 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c an index. A bl
6b120 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 ob encoding of a
6b130 20 72 65 63 6f 72 64 20 69 73 20 63 72 65 61 74 record is creat
6b140 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f ed by.** the OP_
6b150 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 MakeRecord opcod
6b160 65 20 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e e of the VDBE an
6b170 64 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c 65 d is disassemble
6b180 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 d by the.** OP_C
6b190 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a olumn opcode..**
6b1a0 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 .** This structu
6b1b0 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f 72 re holds a recor
6b1c0 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 d that has alrea
6b1d0 64 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 6d dy been disassem
6b1e0 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 bled.** into its
6b1f0 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 69 65 constituent fie
6b200 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55 lds..*/.struct U
6b210 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a npackedRecord {.
6b220 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
6b230 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 nfo; /* Collati
6b240 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65 on and sort-orde
6b250 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f r information */
6b260 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 . u16 nField;
6b270 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6b280 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
6b290 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20 pMem[] */. u16
6b2a0 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
6b2b0 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69 /* Boolean setti
6b2c0 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e ngs. UNPACKED_.
6b2d0 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 4d 65 .. below */. Me
6b2e0 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *aMem;
6b2f0 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d /* Values */.}
6b300 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 ;../*.** Allowed
6b310 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 63 values of Unpac
6b320 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a kedRecord.flags.
6b330 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 */.#define UNPAC
6b340 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 20 KED_NEED_FREE
6b350 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d 0x0001 /* Mem
6b360 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c 69 ory is from sqli
6b370 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 te3Malloc() */.#
6b380 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f define UNPACKED_
6b390 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 78 NEED_DESTROY 0x
6b3a0 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 0002 /* apMem[]
6b3b0 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 s should all be
6b3c0 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65 destroyed */.#de
6b3d0 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 fine UNPACKED_IG
6b3e0 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 30 NORE_ROWID 0x00
6b3f0 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72 04 /* Ignore tr
6b400 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20 ailing rowid on
6b410 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 key1 */.#define
6b420 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 UNPACKED_INCRKEY
6b430 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 0x0008 /
6b440 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 20 * Make this key
6b450 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65 an epsilon large
6b460 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 r */.#define UNP
6b470 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 ACKED_PREFIX_MAT
6b480 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 CH 0x0010 /* A
6b490 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 prefix match is
6b4a0 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a considered OK *
6b4b0 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 /../*.** Each SQ
6b4c0 4c 20 69 6e 64 65 78 20 69 73 20 72 65 70 72 65 L index is repre
6b4d0 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 sented in memory
6b4e0 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e by an.** instan
6b4f0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
6b500 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
6b510 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 *.** The columns
6b520 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 of the table th
6b530 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64 at are to be ind
6b540 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69 62 exed are describ
6b550 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43 ed.** by the aiC
6b560 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66 olumn[] field of
6b570 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e this structure.
6b580 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 For example, s
6b590 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76 uppose.** we hav
6b5a0 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 e the following
6b5b0 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a table and index:
6b5c0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 .**.** CREAT
6b5d0 45 20 54 41 42 4c 45 20 45 78 31 28 63 31 20 69 E TABLE Ex1(c1 i
6b5e0 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74 nt, c2 int, c3 t
6b5f0 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45 ext);.** CRE
6b600 41 54 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e ATE INDEX Ex2 ON
6b610 20 45 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a Ex1(c3,c1);.**.
6b620 2a 2a 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20 ** In the Table
6b630 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 structure descri
6b640 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d bing Ex1, nCol==
6b650 33 20 62 65 63 61 75 73 65 20 74 68 65 72 65 20 3 because there
6b660 61 72 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c are.** three col
6b670 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c umns in the tabl
6b680 65 2e 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78 e. In the Index
6b690 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 structure descr
6b6a0 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43 ibing.** Ex2, nC
6b6b0 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32 olumn==2 since 2
6b6c0 20 6f 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e of the 3 column
6b6d0 73 20 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64 s of Ex1 are ind
6b6e0 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c exed..** The val
6b6f0 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69 ue of aiColumn i
6b700 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c s {2, 0}. aiCol
6b710 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73 umn[0]==2 becaus
6b720 65 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 e the .** first
6b730 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 column to be ind
6b740 65 78 65 64 20 28 63 33 29 20 68 61 73 20 61 6e exed (c3) has an
6b750 20 69 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45 index of 2 in E
6b760 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68 x1.aCol[]..** Th
6b770 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20 e second column
6b780 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 to be indexed (c
6b790 31 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 1) has an index
6b7a0 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61 of 0 in.** Ex1.a
6b7b0 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32 Col[], hence Ex2
6b7c0 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e .aiColumn[1]==0.
6b7d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78 .**.** The Index
6b7e0 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64 .onError field d
6b7f0 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 etermines whethe
6b800 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 r or not the ind
6b810 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 exed columns.**
6b820 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 61 must be unique a
6b830 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66 nd what to do if
6b840 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 they are not.
6b850 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72 When Index.onErr
6b860 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69 or=OE_None,.** i
6b870 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 t means this is
6b880 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64 not a unique ind
6b890 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 ex. Otherwise i
6b8a0 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e t is a unique in
6b8b0 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76 dex.** and the v
6b8c0 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e alue of Index.on
6b8d0 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74 Error indicate t
6b8e0 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63 he which conflic
6b8f0 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a t resolution .**
6b900 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d algorithm to em
6b910 70 6c 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e ploy whenever an
6b920 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
6b930 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e to insert a non
6b940 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65 -unique.** eleme
6b950 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e nt..*/.struct In
6b960 64 65 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e dex {. char *zN
6b970 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ame; /* Name
6b980 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a of this index *
6b990 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b /. int nColumn;
6b9a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6b9b0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
6b9c0 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 table used by t
6b9d0 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 his index */. i
6b9e0 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20 nt *aiColumn;
6b9f0 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73 /* Which columns
6ba00 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 69 are used by thi
6ba10 73 20 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73 s index. 1st is
6ba20 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 0 */. unsigned
6ba30 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52 *aiRowEst; /* R
6ba40 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45 esult of ANALYZE
6ba50 3a 20 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65 : Est. rows sele
6ba60 63 74 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c cted by each col
6ba70 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a umn */. Table *
6ba80 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65 pTable; /* The
6ba90 20 53 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67 SQL table being
6baa0 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e indexed */. in
6bab0 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f t tnum; /
6bac0 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e * Page containin
6bad0 67 20 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69 g root of this i
6bae0 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 ndex in database
6baf0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e file */. u8 on
6bb00 45 72 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f Error; /* O
6bb10 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f E_Abort, OE_Igno
6bb20 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 re, OE_Replace,
6bb30 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 or OE_None */.
6bb40 75 38 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20 u8 autoIndex;
6bb50 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 /* True if is a
6bb60 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 utomatically cre
6bb70 61 74 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49 ated (ex: by UNI
6bb80 51 55 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a QUE) */. char *
6bb90 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74 zColAff; /* St
6bba0 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 ring defining th
6bbb0 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 e affinity of ea
6bbc0 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 ch column */. I
6bbd0 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20 ndex *pNext;
6bbe0 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65 /* The next inde
6bbf0 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 x associated wit
6bc00 68 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 h the same table
6bc10 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 */. Schema *pS
6bc20 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61 chema; /* Schema
6bc30 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 containing this
6bc40 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a index */. u8 *
6bc50 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20 aSortOrder; /*
6bc60 41 72 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e Array of size In
6bc70 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 dex.nColumn. Tru
6bc80 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d e==DESC, False==
6bc90 41 53 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a ASC */. char **
6bca0 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 azColl; /* Arr
6bcb0 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 ay of collation
6bcc0 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66 sequence names f
6bcd0 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 0a or index */.};..
6bce0 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e /*.** Each token
6bcf0 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74 coming out of t
6bd00 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20 69 he lexer is an i
6bd10 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 nstance of.** th
6bd20 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 is structure. T
6bd30 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 75 okens are also u
6bd40 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 sed as part of a
6bd50 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a n expression..**
6bd60 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65 .** Note if Toke
6bd70 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65 n.z==0 then Toke
6bd80 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e n.dyn and Token.
6bd90 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 n are undefined
6bda0 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 and.** may conta
6bdb0 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73 in random values
6bdc0 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61 . Do not make a
6bdd0 6e 79 20 61 73 73 75 6d 70 74 69 6f 6e 73 20 61 ny assumptions a
6bde0 62 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a bout Token.dyn.*
6bdf0 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 * and Token.n wh
6be00 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a en Token.z==0..*
6be10 2f 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b /.struct Token {
6be20 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 . const unsigne
6be30 64 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 65 d char *z; /* Te
6be40 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e xt of the token.
6be50 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 Not NULL-termi
6be60 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 nated! */. unsi
6be70 67 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b 20 20 gned dyn : 1;
6be80 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 /* True for
6be90 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2c malloced memory,
6bea0 20 66 61 6c 73 65 20 66 6f 72 20 73 74 61 74 69 false for stati
6beb0 63 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 c */. unsigned
6bec0 6e 20 20 20 20 3a 20 33 31 3b 20 20 20 20 20 2f n : 31; /
6bed0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 * Number of char
6bee0 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74 acters in this t
6bef0 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a oken */.};../*.*
6bf00 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
6bf10 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
6bf20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 contains informa
6bf30 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 tion needed to g
6bf40 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 enerate.** code
6bf50 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61 for a SELECT tha
6bf60 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 t contains aggre
6bf70 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a gate functions..
6bf80 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 **.** If Expr.op
6bf90 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 ==TK_AGG_COLUMN
6bfa0 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 or TK_AGG_FUNCTI
6bfb0 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67 ON then Expr.pAg
6bfc0 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f gInfo is a.** po
6bfd0 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 inter to this st
6bfe0 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78 ructure. The Ex
6bff0 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 pr.iColumn field
6c000 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e is the index in
6c010 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c .** AggInfo.aCol
6c020 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 [] or AggInfo.aF
6c030 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 unc[] of informa
6c040 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 tion needed to g
6c050 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 enerate.** code
6c060 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a for that node..*
6c070 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 *.** AggInfo.pGr
6c080 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66 oupBy and AggInf
6c090 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f o.aFunc.pExpr po
6c0a0 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69 int to fields wi
6c0b0 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 thin the.** orig
6c0c0 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75 inal Select stru
6c0d0 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 cture that descr
6c0e0 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20 ibes the SELECT
6c0f0 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 statement. Thes
6c100 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e e.** fields do n
6c110 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 ot need to be fr
6c120 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 eed when dealloc
6c130 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66 ating the AggInf
6c140 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a o structure..*/.
6c150 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b struct AggInfo {
6c160 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65 . u8 directMode
6c170 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 ; /* Di
6c180 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d rect rendering m
6c190 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 ode means take d
6c1a0 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20 ata directly.
6c1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c1c0 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 ** from s
6c1d0 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 ource tables rat
6c1e0 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 her than from ac
6c1f0 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 cumulators */.
6c200 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 u8 useSortingIdx
6c210 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 ; /* In di
6c220 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 rect mode, refer
6c230 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 ence the sorting
6c240 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20 index rather.
6c250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c260 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 ** than
6c270 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 the source table
6c280 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e */. int sortin
6c290 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a gIdx; /*
6c2a0 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f Cursor number o
6c2b0 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e f the sorting in
6c2c0 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 dex */. ExprLis
6c2d0 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 t *pGroupBy;
6c2e0 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 /* The group by
6c2f0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 clause */. int
6c300 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b nSortingColumn;
6c310 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6c320 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
6c330 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a sorting index *
6c340 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e /. struct AggIn
6c350 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 fo_col { /* F
6c360 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 or each column u
6c370 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61 sed in source ta
6c380 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c bles */. Tabl
6c390 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 e *pTab;
6c3a0 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 /* Source t
6c3b0 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 able */. int
6c3c0 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 iTable;
6c3d0 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e /* Cursor n
6c3e0 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75 umber of the sou
6c3f0 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 rce table */.
6c400 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 int iColumn;
6c410 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c /* Col
6c420 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 umn number withi
6c430 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 n the source tab
6c440 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 le */. int iS
6c450 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 orterColumn;
6c460 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d /* Column num
6c470 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69 ber in the sorti
6c480 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 ng index */.
6c490 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 int iMem;
6c4a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f /* Memo
6c4b0 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 ry location that
6c4c0 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c acts as accumul
6c4d0 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 ator */. Expr
6c4e0 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 *pExpr;
6c4f0 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 /* The orig
6c500 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 inal expression
6c510 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 */. } *aCol;.
6c520 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 int nColumn;
6c530 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6c540 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65 r of used entrie
6c550 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 s in aCol[] */.
6c560 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f int nColumnAllo
6c570 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 c; /* Numb
6c580 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f er of slots allo
6c590 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d cated for aCol[]
6c5a0 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d */. int nAccum
6c5b0 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a ulator; /*
6c5c0 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d Number of colum
6c5d0 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72 ns that show thr
6c5e0 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70 ough to the outp
6c5f0 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 ut..
6c600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
6c610 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 Additional colu
6c620 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c mns are used onl
6c630 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 y as parameters
6c640 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 to.
6c650 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
6c660 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
6c670 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ons */. struct
6c680 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 AggInfo_func {
6c690 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67 /* For each agg
6c6a0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 regate function
6c6b0 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 */. Expr *pEx
6c6c0 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pr;
6c6d0 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e /* Expression en
6c6e0 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74 coding the funct
6c6f0 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 ion */. FuncD
6c700 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 ef *pFunc;
6c710 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65 /* The aggre
6c720 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d gate function im
6c730 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a plementation */.
6c740 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 int iMem;
6c750 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6c760 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 Memory location
6c770 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 that acts as acc
6c780 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 umulator */.
6c790 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20 int iDistinct;
6c7a0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65 /* Ephe
6c7b0 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64 meral table used
6c7c0 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54 to enforce DIST
6c7d0 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 INCT */. } *aFu
6c7e0 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b nc;. int nFunc;
6c7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6c800 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
6c810 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f es in aFunc[] */
6c820 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f . int nFuncAllo
6c830 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 c; /* Nu
6c840 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c mber of slots al
6c850 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e located for aFun
6c860 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a c[] */.};../*.**
6c870 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e Each node of an
6c880 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 expression in t
6c890 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 73 he parse tree is
6c8a0 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 an instance.**
6c8b0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
6c8c0 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 e..**.** Expr.op
6c8d0 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 is the opcode.
6c8e0 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72 The integer par
6c8f0 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20 ser token codes
6c900 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73 are reused.** as
6c910 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 20 opcodes here.
6c920 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 For example, the
6c930 20 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20 parser defines
6c940 54 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69 TK_GE to be an i
6c950 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72 nteger.** code r
6c960 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 epresenting the
6c970 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 20 ">=" operator.
6c980 54 68 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65 This same intege
6c990 72 20 63 6f 64 65 20 69 73 20 72 65 75 73 65 64 r code is reused
6c9a0 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74 .** to represent
6c9b0 20 74 68 65 20 67 72 65 61 74 65 72 2d 74 68 61 the greater-tha
6c9c0 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70 n-or-equal-to op
6c9d0 65 72 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78 erator in the ex
6c9e0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 pression.** tree
6c9f0 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69 ..**.** Expr.pRi
6ca00 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c 65 ght and Expr.pLe
6ca10 66 74 20 61 72 65 20 73 75 62 65 78 70 72 65 73 ft are subexpres
6ca20 73 69 6f 6e 73 2e 20 20 45 78 70 72 2e 70 4c 69 sions. Expr.pLi
6ca30 73 74 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20 st is a list.**
6ca40 6f 66 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74 of argument if t
6ca50 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
6ca60 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a a function..**.
6ca70 2a 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 20 69 73 ** Expr.token is
6ca80 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f the operator to
6ca90 6b 65 6e 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 ken for this nod
6caa0 65 2e 20 20 46 6f 72 20 73 6f 6d 65 20 65 78 70 e. For some exp
6cab0 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 ressions.** that
6cac0 20 68 61 76 65 20 73 75 62 65 78 70 72 65 73 73 have subexpress
6cad0 69 6f 6e 73 2c 20 45 78 70 72 2e 74 6f 6b 65 6e ions, Expr.token
6cae0 20 63 61 6e 20 62 65 20 74 68 65 20 63 6f 6d 70 can be the comp
6caf0 6c 65 74 65 20 74 65 78 74 20 74 68 61 74 20 67 lete text that g
6cb00 61 76 65 0a 2a 2a 20 72 69 73 65 20 74 6f 20 74 ave.** rise to t
6cb10 68 65 20 45 78 70 72 2e 20 20 49 6e 20 74 68 65 he Expr. In the
6cb20 20 6c 61 74 74 65 72 20 63 61 73 65 2c 20 74 68 latter case, th
6cb30 65 20 74 6f 6b 65 6e 20 69 73 20 6d 61 72 6b 65 e token is marke
6cb40 64 20 61 73 20 62 65 69 6e 67 0a 2a 2a 20 61 20 d as being.** a
6cb50 63 6f 6d 70 6f 75 6e 64 20 74 6f 6b 65 6e 2e 0a compound token..
6cb60 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 73 **.** An express
6cb70 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 ion of the form
6cb80 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 65 ID or ID.ID refe
6cb90 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 rs to a column i
6cba0 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f n a table..** Fo
6cbb0 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 6f r such expressio
6cbc0 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 73 ns, Expr.op is s
6cbd0 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 et to TK_COLUMN
6cbe0 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20 and Expr.iTable
6cbf0 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 is.** the intege
6cc00 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 r cursor number
6cc10 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f 72 of a VDBE cursor
6cc20 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 61 pointing to tha
6cc30 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45 t table and.** E
6cc40 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74 xpr.iColumn is t
6cc50 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 he column number
6cc60 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 for the specifi
6cc70 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 c column. If th
6cc80 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 e.** expression
6cc90 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 73 is used as a res
6cca0 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 67 ult in an aggreg
6ccb0 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 6e ate SELECT, then
6ccc0 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73 the.** value is
6ccd0 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 also stored in
6cce0 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 6f the Expr.iAgg co
6ccf0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 72 lumn in the aggr
6cd00 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a egate so that.**
6cd10 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 73 it can be acces
6cd20 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 67 sed after all ag
6cd30 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f 6d gregates are com
6cd40 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 puted..**.** If
6cd50 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
6cd60 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 s a function, th
6cd70 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73 e Expr.iTable is
6cd80 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 an integer code
6cd90 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 69 6e 67 .** representing
6cda0 20 77 68 69 63 68 20 66 75 6e 63 74 69 6f 6e 2e which function.
6cdb0 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 If the express
6cdc0 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e ion is an unboun
6cdd0 64 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 d variable.** ma
6cde0 72 6b 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e rker (a question
6cdf0 20 6d 61 72 6b 20 63 68 61 72 61 63 74 65 72 20 mark character
6ce00 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69 '?' in the origi
6ce10 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68 nal SQL) then th
6ce20 65 0a 2a 2a 20 45 78 70 72 2e 69 54 61 62 6c 65 e.** Expr.iTable
6ce30 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78 holds the index
6ce40 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 number for that
6ce50 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a variable..**.**
6ce60 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 If the expressi
6ce70 6f 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79 on is a subquery
6ce80 20 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 then Expr.iColu
6ce90 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 mn holds an inte
6cea0 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 ger.** register
6ceb0 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e number containin
6cec0 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 g the result of
6ced0 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 the subquery. I
6cee0 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 f the.** subquer
6cef0 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61 y gives a consta
6cf00 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 nt result, then
6cf10 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 iTable is -1. I
6cf20 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a f the subquery.*
6cf30 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72 * gives a differ
6cf40 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69 ent answer at di
6cf50 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75 fferent times du
6cf60 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 ring statement p
6cf70 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 rocessing.** the
6cf80 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20 n iTable is the
6cf90 61 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62 address of a sub
6cfa0 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d routine that com
6cfb0 70 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65 putes the subque
6cfc0 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78 ry..**.** The Ex
6cfd0 70 72 2e 70 53 65 6c 65 63 74 20 66 69 65 6c 64 pr.pSelect field
6cfe0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 53 45 4c points to a SEL
6cff0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ECT statement.
6d000 54 68 65 20 53 45 4c 45 43 54 20 6d 69 67 68 74 The SELECT might
6d010 0a 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 74 .** be the right
6d020 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 49 operand of an I
6d030 4e 20 6f 70 65 72 61 74 6f 72 2e 20 20 4f 72 2c N operator. Or,
6d040 20 69 66 20 61 20 73 63 61 6c 61 72 20 53 45 4c if a scalar SEL
6d050 45 43 54 20 61 70 70 65 61 72 73 0a 2a 2a 20 69 ECT appears.** i
6d060 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 n an expression
6d070 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b the opcode is TK
6d080 5f 53 45 4c 45 43 54 20 61 6e 64 20 45 78 70 72 _SELECT and Expr
6d090 2e 70 53 65 6c 65 63 74 20 69 73 20 74 68 65 20 .pSelect is the
6d0a0 6f 6e 6c 79 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e only.** operand.
6d0b0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 .**.** If the Ex
6d0c0 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f 50 pr is of type OP
6d0d0 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65 _Column, and the
6d0e0 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 6c table it is sel
6d0f0 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69 ecting from.** i
6d100 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 6f s a disk table o
6d110 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73 r the "old.*" ps
6d120 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e eudo-table, then
6d130 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f 20 pTab points to
6d140 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e the.** correspon
6d150 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 6e ding table defin
6d160 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 ition..*/.struct
6d170 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b Expr {. u8 op;
6d180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d190 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65 /* Operation pe
6d1a0 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20 rformed by this
6d1b0 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61 node */. char a
6d1c0 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 ffinity;
6d1d0 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 /* The affinity
6d1e0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f of the column o
6d1f0 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c r 0 if not a col
6d200 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 umn */. u16 fla
6d210 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
6d220 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73 /* Various flags
6d230 2e 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a . See below */.
6d240 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
6d250 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
6d260 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f collation type o
6d270 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 f the column or
6d280 30 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 0 */. Expr *pLe
6d290 66 74 2c 20 2a 70 52 69 67 68 74 3b 20 20 2f 2a ft, *pRight; /*
6d2a0 20 4c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 Left and right
6d2b0 73 75 62 6e 6f 64 65 73 20 2a 2f 0a 20 20 45 78 subnodes */. Ex
6d2c0 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 prList *pList;
6d2d0 20 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f /* A list o
6d2e0 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 75 73 f expressions us
6d2f0 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 20 61 ed as function a
6d300 72 67 75 6d 65 6e 74 73 0a 20 20 20 20 20 20 20 rguments.
6d310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d320 20 20 2a 2a 20 6f 72 20 69 6e 20 22 3c 65 78 70 ** or in "<exp
6d330 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73 r> IN (<expr-lis
6d340 74 29 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 t)" */. Token t
6d350 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 oken;
6d360 2f 2a 20 41 6e 20 6f 70 65 72 61 6e 64 20 74 6f /* An operand to
6d370 6b 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 ken */. Token s
6d380 70 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 pan;
6d390 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 /* Complete text
6d3a0 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
6d3b0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 on */. int iTab
6d3c0 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f le, iColumn; /
6d3d0 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 4f * When op==TK_CO
6d3e0 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68 69 73 20 LUMN, then this
6d3f0 65 78 70 72 20 6e 6f 64 65 20 6d 65 61 6e 73 20 expr node means
6d400 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 the.
6d410 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
6d420 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69 65 6c 64 iColumn-th field
6d430 20 6f 66 20 74 68 65 20 69 54 61 62 6c 65 2d 74 of the iTable-t
6d440 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 67 h table. */. Ag
6d450 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b gInfo *pAggInfo;
6d460 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 /* Used by
6d470 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e TK_AGG_COLUMN an
6d480 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f d TK_AGG_FUNCTIO
6d490 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 67 3b N */. int iAgg;
6d4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6d4b0 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20 Which entry in
6d4c0 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d pAggInfo->aCol[]
6d4d0 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f or ->aFunc[] */
6d4e0 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69 . int iRightJoi
6d4f0 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20 nTable; /* If
6d500 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65 EP_FromJoin, the
6d510 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 right table of
6d520 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 53 65 the join */. Se
6d530 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 lect *pSelect;
6d540 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 /* When the
6d550 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
6d560 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 20 41 6c sub-select. Al
6d570 73 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 so the.
6d580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d590 2a 2a 20 72 69 67 68 74 20 73 69 64 65 20 6f 66 ** right side of
6d5a0 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65 "<expr> IN (<se
6d5b0 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 54 61 62 lect>)" */. Tab
6d5c0 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 le *pTab;
6d5d0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72 /* Table for
6d5e0 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 TK_COLUMN expre
6d5f0 73 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 20 53 ssions. */.#if S
6d600 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
6d610 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65 EPTH>0. int nHe
6d620 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 ight;
6d630 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65 /* Height of the
6d640 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20 tree headed by
6d650 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e this node */.#en
6d660 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 dif.};../*.** Th
6d670 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
6d680 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 the meanings of
6d690 62 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 bits in the Expr
6d6a0 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f .flags field..*/
6d6b0 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d .#define EP_From
6d6c0 4a 6f 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f Join 0x0001 /
6d6d0 2a 20 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 * Originated in
6d6e0 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 ON or USING clau
6d6f0 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a se of a join */.
6d700 23 64 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20 #define EP_Agg
6d710 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 0x0002 /*
6d720 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 Contains one or
6d730 20 6d 6f 72 65 20 61 67 67 72 65 67 61 74 65 20 more aggregate
6d740 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 functions */.#de
6d750 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64 fine EP_Resolved
6d760 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44 0x0004 /* ID
6d770 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f s have been reso
6d780 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 lved to COLUMNs
6d790 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72 */.#define EP_Er
6d7a0 72 6f 72 20 20 20 20 20 20 30 78 30 30 30 38 20 ror 0x0008
6d7b0 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63 /* Expression c
6d7c0 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d ontains one or m
6d7d0 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 ore errors */.#d
6d7e0 65 66 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63 efine EP_Distinc
6d7f0 74 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 t 0x0010 /* A
6d800 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
6d810 6e 20 77 69 74 68 20 44 49 53 54 49 4e 43 54 20 n with DISTINCT
6d820 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 keyword */.#defi
6d830 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20 ne EP_VarSelect
6d840 20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c 0x0020 /* pSel
6d850 65 63 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65 ect is correlate
6d860 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20 d, not constant
6d870 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 65 */.#define EP_De
6d880 71 75 6f 74 65 64 20 20 20 30 78 30 30 34 30 20 quoted 0x0040
6d890 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
6d8a0 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20 string has been
6d8b0 64 65 71 75 6f 74 65 64 20 2a 2f 0a 23 64 65 66 dequoted */.#def
6d8c0 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 ine EP_InfixFunc
6d8d0 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72 75 0x0080 /* Tru
6d8e0 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 66 e for an infix f
6d8f0 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47 unction: LIKE, G
6d900 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66 LOB, etc */.#def
6d910 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 ine EP_ExpCollat
6d920 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c e 0x0100 /* Col
6d930 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
6d940 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 63 specified explic
6d950 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 itly */.#define
6d960 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30 78 EP_AnyAff 0x
6d970 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b 0200 /* Can tak
6d980 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d e a cached colum
6d990 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 74 n of any affinit
6d9a0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f y */.#define EP_
6d9b0 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34 30 FixedDest 0x040
6d9c0 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65 0 /* Result nee
6d9d0 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66 69 ded in a specifi
6d9e0 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 64 c register */.#d
6d9f0 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c 75 efine EP_IntValu
6da00 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a 20 49 e 0x0800 /* I
6da10 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 6f 6e nteger value con
6da20 74 61 69 6e 65 64 20 69 6e 20 69 54 61 62 6c 65 tained in iTable
6da30 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 */./*.** These
6da40 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 macros can be us
6da50 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c ed to test, set,
6da60 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 or clear bits i
6da70 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66 n the .** Expr.f
6da80 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 lags field..*/.#
6da90 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72 define ExprHasPr
6daa0 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 operty(E,P)
6dab0 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 (((E)->flags&(P)
6dac0 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 )==(P)).#define
6dad0 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
6dae0 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e ty(E,P) (((E)->
6daf0 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 flags&(P))!=0).#
6db00 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 72 define ExprSetPr
6db10 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 operty(E,P)
6db20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a (E)->flags|=(P).
6db30 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61 #define ExprClea
6db40 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 rProperty(E,P)
6db50 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 (E)->flags&=~(P
6db60 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 )../*.** A list
6db70 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 of expressions.
6db80 20 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e Each expression
6db90 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 may optionally
6dba0 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 have a.** name.
6dbb0 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f An expr/name co
6dbc0 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 mbination can be
6dbd0 20 75 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c used in several
6dbe0 20 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 ways, such.** a
6dbf0 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65 s the list of "e
6dc00 78 70 72 20 41 53 20 49 44 22 20 66 69 65 6c 64 xpr AS ID" field
6dc10 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 s following a "S
6dc20 45 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65 ELECT" or in the
6dc30 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20 .** list of "ID
6dc40 3d 20 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e = expr" items in
6dc50 20 61 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c an UPDATE. A l
6dc60 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f ist of expressio
6dc70 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 ns can.** also b
6dc80 65 20 75 73 65 64 20 61 73 20 74 68 65 20 61 72 e used as the ar
6dc90 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 gument to a func
6dca0 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 tion, in which c
6dcb0 61 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a ase the a.zName.
6dcc0 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 ** field is not
6dcd0 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 used..*/.struct
6dce0 45 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 ExprList {. int
6dcf0 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 nExpr;
6dd00 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6dd10 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 expressions on
6dd20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e the list */. in
6dd30 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 t nAlloc;
6dd40 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6dd50 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 f entries alloca
6dd60 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 ted below */. i
6dd70 6e 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20 nt iECursor;
6dd80 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 /* VDBE Cu
6dd90 72 73 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20 rsor associated
6dda0 77 69 74 68 20 74 68 69 73 20 45 78 70 72 4c 69 with this ExprLi
6ddb0 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 st */. struct E
6ddc0 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 xprList_item {.
6ddd0 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 Expr *pExpr;
6dde0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
6ddf0 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 list of express
6de00 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 ions */. char
6de10 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 *zName;
6de20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f /* Token asso
6de30 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 ciated with this
6de40 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
6de50 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b u8 sortOrder;
6de60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 /* 1 f
6de70 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72 or DESC or 0 for
6de80 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 ASC */. u8 d
6de90 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 one;
6dea0 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 /* A flag to
6deb0 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 indicate when pr
6dec0 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 ocessing is fini
6ded0 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 shed */. u16
6dee0 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 iCol;
6def0 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 /* For ORDER
6df00 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 BY, column numbe
6df10 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 r in result set
6df20 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61 */. u16 iAlia
6df30 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a s; /*
6df40 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73 Index into Pars
6df50 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a e.aAlias[] for z
6df60 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 Name */. } *a;
6df70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6df80 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f /* One entry fo
6df90 72 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f r each expressio
6dfa0 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 n */.};../*.** A
6dfb0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
6dfc0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e is structure can
6dfd0 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c hold a simple l
6dfe0 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 ist of identifie
6dff0 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 rs,.** such as t
6e000 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 he list "a,b,c"
6e010 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 in the following
6e020 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a statements:.**.
6e030 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 ** INSERT I
6e040 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c NTO t(a,b,c) VAL
6e050 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 UES ...;.**
6e060 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 CREATE INDEX id
6e070 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a x ON t(a,b,c);.*
6e080 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 * CREATE TR
6e090 49 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 IGGER trig BEFOR
6e0a0 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c E UPDATE ON t(a,
6e0b0 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 b,c) ...;.**.**
6e0c0 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 The IdList.a.idx
6e0d0 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 field is used w
6e0e0 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 hen the IdList r
6e0f0 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 epresents the li
6e100 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 st of.** column
6e110 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 names after a ta
6e120 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 ble name in an I
6e130 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e NSERT statement.
6e140 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 In the stateme
6e150 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 nt.**.** INS
6e160 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 ERT INTO t(a,b,c
6e170 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 ) ....**.** If "
6e180 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 a" is the k-th c
6e190 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 olumn of table "
6e1a0 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e t", then IdList.
6e1b0 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a a[0].idx==k..*/.
6e1c0 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a struct IdList {.
6e1d0 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f struct IdList_
6e1e0 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 item {. char
6e1f0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 *zName; /*
6e200 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e Name of the iden
6e210 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e tifier */. in
6e220 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 t idx;
6e230 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 /* Index in some
6e240 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 Table.aCol[] of
6e250 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 a column named
6e260 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b zName */. } *a;
6e270 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 . int nId;
6e280 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6e290 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 identifiers on
6e2a0 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e the list */. in
6e2b0 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f t nAlloc; /
6e2c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
6e2d0 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ies allocated fo
6e2e0 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d r a[] below */.}
6e2f0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 ;../*.** The bit
6e300 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 mask datatype de
6e310 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 fined below is u
6e320 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 sed for various
6e330 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a optimizations..*
6e340 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 *.** Changing th
6e350 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 is from a 64-bit
6e360 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 to a 32-bit typ
6e370 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d e limits the num
6e380 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 ber of.** tables
6e390 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 in a join to 32
6e3a0 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 instead of 64.
6e3b0 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 But it also red
6e3c0 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a uces the size.**
6e3d0 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 of the library
6e3e0 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 by 738 bytes on
6e3f0 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ix86..*/.typedef
6e400 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f u64 Bitmask;../
6e410 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 *.** The number
6e420 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 of bits in a Bit
6e430 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61 mask. "BMS" mea
6e440 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65 ns "BitMask Size
6e450 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d "..*/.#define BM
6e460 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 S ((int)(sizeof
6e470 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f (Bitmask)*8))../
6e480 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
6e490 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 ng structure des
6e4a0 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 cribes the FROM
6e4b0 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 clause of a SELE
6e4c0 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a CT statement..**
6e4d0 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 Each table or s
6e4e0 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 ubquery in the F
6e4f0 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 ROM clause is a
6e500 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 separate element
6e510 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 of.** the SrcLi
6e520 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a st.a[] array..**
6e530 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 .** With the add
6e540 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c ition of multipl
6e550 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
6e560 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e rt, the followin
6e570 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 g structure.** c
6e580 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 an also be used
6e590 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 to describe a pa
6e5a0 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 rticular table s
6e5b0 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 uch as the table
6e5c0 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 that.** is modi
6e5d0 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 fied by an INSER
6e5e0 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 T, DELETE, or UP
6e5f0 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 DATE statement.
6e600 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c In standard SQL
6e610 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c ,.** such a tabl
6e620 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 e must be a simp
6e630 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 le name: ID. Bu
6e640 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 t in SQLite, the
6e650 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f table can.** no
6e660 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 w be identified
6e670 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 by a database na
6e680 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 me, a dot, then
6e690 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 the table name:
6e6a0 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ID.ID..**.** The
6e6b0 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 jointype starts
6e6c0 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 out showing the
6e6d0 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 join type betwe
6e6e0 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 en the current t
6e6f0 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 able.** and the
6e700 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 next table on th
6e710 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 e list. The par
6e720 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c ser builds the l
6e730 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a ist this way..**
6e740 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c But sqlite3SrcL
6e750 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 istShiftJoinType
6e760 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 () later shifts
6e770 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f the jointypes so
6e780 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f that each.** jo
6e790 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 intype expresses
6e7a0 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 the join betwee
6e7b0 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 n the table and
6e7c0 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 the previous tab
6e7d0 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 le..*/.struct Sr
6e7e0 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 cList {. i16 nS
6e7f0 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 rc; /* Nu
6e800 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f mber of tables o
6e810 72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 r subqueries in
6e820 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 the FROM clause
6e830 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b */. i16 nAlloc;
6e840 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6e850 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 of entries alloc
6e860 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f ated in a[] belo
6e870 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 w */. struct Sr
6e880 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 cList_item {.
6e890 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 char *zDatabase
6e8a0 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 ; /* Name of da
6e8b0 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 tabase holding t
6e8c0 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 his table */.
6e8d0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
6e8e0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
6e8f0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 e table */. c
6e900 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 har *zAlias;
6e910 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 /* The "B" part
6e920 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 of a "A AS B" p
6e930 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 hrase. zName is
6e940 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 the "A" */.
6e950 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 Table *pTab;
6e960 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c /* An SQL tabl
6e970 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
6e980 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 to zName */.
6e990 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b Select *pSelect;
6e9a0 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 /* A SELECT st
6e9b0 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 atement used in
6e9c0 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 place of a table
6e9d0 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 name */. u8
6e9e0 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f isPopulated; /
6e9f0 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c * Temporary tabl
6ea00 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
6ea10 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 h SELECT is popu
6ea20 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 lated */. u8
6ea30 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f jointype; /
6ea40 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 * Type of join b
6ea50 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 etween this able
6ea60 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 and the previou
6ea70 73 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 49 s */. u8 notI
6ea80 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 72 ndexed; /* Tr
6ea90 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ue if there is a
6eaa0 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 NOT INDEXED cla
6eab0 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 use */. int i
6eac0 43 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 Cursor; /*
6ead0 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 The VDBE cursor
6eae0 6e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 number used to a
6eaf0 63 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 ccess this table
6eb00 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f */. Expr *pO
6eb10 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 n; /* The
6eb20 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 ON clause of a
6eb30 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 join */. IdLi
6eb40 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a st *pUsing; /*
6eb50 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 The USING claus
6eb60 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 e of a join */.
6eb70 20 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 Bitmask colUs
6eb80 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 ed; /* Bit N (1
6eb90 3c 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 <<N) set if colu
6eba0 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 20 mn N of pTab is
6ebb0 75 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 72 used */. char
6ebc0 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a *zIndex; /*
6ebd0 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d Identifier from
6ebe0 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49 "INDEXED BY <zI
6ebf0 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f ndex>" clause */
6ec00 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 . Index *pInd
6ec10 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 ex; /* Index
6ec20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73 structure corres
6ec30 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65 ponding to zInde
6ec40 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d x, if any */. }
6ec50 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 a[1];
6ec60 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 /* One entry
6ec70 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66 for each identif
6ec80 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 ier on the list
6ec90 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 */.};../*.** Per
6eca0 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66 mitted values of
6ecb0 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a the SrcList.a.j
6ecc0 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f ointype field.*/
6ecd0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 .#define JT_INNE
6ece0 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20 R 0x0001
6ecf0 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 /* Any kind of i
6ed00 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f nner or cross jo
6ed10 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 in */.#define JT
6ed20 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30 _CROSS 0x000
6ed30 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 2 /* Explicit
6ed40 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53 use of the CROS
6ed50 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 S keyword */.#de
6ed60 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 fine JT_NATURAL
6ed70 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 0x0004 /* T
6ed80 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 rue for a "natur
6ed90 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 al" join */.#def
6eda0 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 ine JT_LEFT
6edb0 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 0x0008 /* Le
6edc0 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f ft outer join */
6edd0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 .#define JT_RIGH
6ede0 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20 T 0x0010
6edf0 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a /* Right outer j
6ee00 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a oin */.#define J
6ee10 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30 T_OUTER 0x00
6ee20 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 20 /* The "OU
6ee30 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20 TER" keyword is
6ee40 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 present */.#defi
6ee50 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 ne JT_ERROR
6ee60 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 0x0040 /* unk
6ee70 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 nown or unsuppor
6ee80 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f ted join type */
6ee90 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 .../*.** A Where
6eea0 50 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c 64 Plan object hold
6eeb0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 s information th
6eec0 61 74 20 64 65 73 63 72 69 62 65 73 20 61 20 6c at describes a l
6eed0 6f 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 67 ookup.** strateg
6eee0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 y..**.** This ob
6eef0 6a 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 64 ject is intended
6ef00 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f 75 to be opaque ou
6ef10 74 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 65 tside of the whe
6ef20 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 re.c module..**
6ef30 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 68 It is included h
6ef40 65 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 74 ere only so that
6ef50 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 77 that compiler w
6ef60 69 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67 ill know how big
6ef70 20 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65 it.** is. None
6ef80 20 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 69 of the fields i
6ef90 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 68 n this object sh
6efa0 6f 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 74 ould be used out
6efb0 73 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 77 side of.** the w
6efc0 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a here.c module..*
6efd0 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20 *.** Within the
6efe0 75 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f union, pIdx is o
6eff0 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 nly used when ws
6f000 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45 Flags&WHERE_INDE
6f010 58 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 XED is true..**
6f020 70 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73 pTerm is only us
6f030 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 ed when wsFlags&
6f040 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 WHERE_MULTI_OR i
6f050 73 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74 s true. And pVt
6f060 61 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79 abIdx.** is only
6f070 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 used when wsFla
6f080 67 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c gs&WHERE_VIRTUAL
6f090 54 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20 TABLE is true.
6f0a0 49 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0a It is never the.
6f0b0 2a 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f 72 ** case that mor
6f0c0 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 e than one of th
6f0d0 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 ese conditions i
6f0e0 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 s true..*/.struc
6f0f0 74 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 20 t WherePlan {.
6f100 75 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 20 u32 wsFlags;
6f110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6f120 2a 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 * WHERE_* flags
6f130 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 that describe th
6f140 65 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 e strategy */.
6f150 75 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 20 u32 nEq;
6f160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6f170 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63 * Number of == c
6f180 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 onstraints */.
6f190 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 65 union {. Inde
6f1a0 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 x *pIdx;
6f1b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
6f1c0 64 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f 49 dex when WHERE_I
6f1d0 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 20 2a NDEXED is true *
6f1e0 2f 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 65 /. struct Whe
6f1f0 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 reTerm *pTerm;
6f200 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c /* WHERE cl
6f210 61 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f 52 ause term for OR
6f220 2d 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 73 -search */. s
6f230 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 qlite3_index_inf
6f240 6f 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f 2a o *pVtabIdx; /*
6f250 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 Virtual table i
6f260 6e 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20 ndex to use */.
6f270 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 } u;.};../*.**
6f280 46 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20 For each nested
6f290 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20 loop in a WHERE
6f2a0 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74 clause implement
6f2b0 61 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65 ation, the Where
6f2c0 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 Info.** structur
6f2d0 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e e contains a sin
6f2e0 67 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 gle instance of
6f2f0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 this structure.
6f300 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a This structure.
6f310 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 ** is intended t
6f320 6f 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65 o be private the
6f330 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 the where.c mod
6f340 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e ule and should n
6f350 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20 ot be.** access
6f360 6f 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f or modified by o
6f370 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a ther modules..**
6f380 0a 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f .** The pIdxInfo
6f390 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 field is used t
6f3a0 6f 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20 o help pick the
6f3b0 62 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0a best index on a.
6f3c0 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 ** virtual table
6f3d0 2e 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 . The pIdxInfo
6f3e0 70 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73 pointer contains
6f3f0 20 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 indexing.** inf
6f400 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 ormation for the
6f410 20 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74 i-th table in t
6f420 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62 he FROM clause b
6f430 65 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67 efore reordering
6f440 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64 ..** All the pId
6f450 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61 xInfo pointers a
6f460 72 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72 re freed by wher
6f470 65 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77 eInfoFree() in w
6f480 68 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f here.c..** All o
6f490 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ther information
6f4a0 20 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 65 in the i-th Whe
6f4b0 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 66 reLevel object f
6f4c0 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c or the i-th tabl
6f4d0 65 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d 20 e.** after FROM
6f4e0 63 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e clause ordering.
6f4f0 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 .*/.struct Where
6f500 4c 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 50 Level {. WhereP
6f510 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 20 lan plan;
6f520 2f 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f /* query plan fo
6f530 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 6f r this element o
6f540 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 f the FROM claus
6f550 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74 e */. int iLeft
6f560 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 Join; /*
6f570 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 Memory cell used
6f580 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c 45 to implement LE
6f590 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a 2f FT OUTER JOIN */
6f5a0 0a 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20 . int iTabCur;
6f5b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
6f5c0 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 VDBE cursor used
6f5d0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 74 to access the t
6f5e0 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49 able */. int iI
6f5f0 64 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 dxCur;
6f600 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 /* The VDBE curs
6f610 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 or used to acces
6f620 73 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 s pIdx */. int
6f630 61 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20 addrBrk;
6f640 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
6f650 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 o break out of t
6f660 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 he loop */. int
6f670 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20 addrNxt;
6f680 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 /* Jump here
6f690 74 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 78 to start the nex
6f6a0 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e t IN combination
6f6b0 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f */. int addrCo
6f6c0 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4a nt; /* J
6f6d0 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 ump here to cont
6f6e0 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 inue with the ne
6f6f0 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f xt loop cycle */
6f700 0a 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74 . int addrFirst
6f710 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 ; /* Firs
6f720 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 t instruction of
6f730 20 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65 interior of the
6f740 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 46 loop */. u8 iF
6f750 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 rom;
6f760 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 /* Which entry
6f770 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 in the FROM clau
6f780 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 70 se */. u8 op, p
6f790 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 5; /*
6f7a0 20 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f Opcode and P5 o
6f7b0 66 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 f the opcode tha
6f7c0 74 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 t ends the loop
6f7d0 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b */. int p1, p2;
6f7e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
6f7f0 65 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70 erands of the op
6f800 63 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64 code used to end
6f810 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 s the loop */.
6f820 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 union {
6f830 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 /* Informa
6f840 74 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e 64 tion that depend
6f850 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 s on plan.wsFlag
6f860 73 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 s */. struct
6f870 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b {. int nIn;
6f880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6f890 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
6f8a0 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 es in aInLoop[]
6f8b0 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 */. struct
6f8c0 49 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20 InLoop {.
6f8d0 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 int iCur;
6f8e0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
6f8f0 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 DBE cursor used
6f900 62 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61 by this IN opera
6f910 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 tor */. i
6f920 6e 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20 nt addrInTop;
6f930 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 /* Top of
6f940 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 the IN loop */.
6f950 20 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b } *aInLoop;
6f960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
6f970 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
6f980 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f each nested IN o
6f990 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 7d perator */. }
6f9a0 20 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 in;
6f9b0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 /* Used whe
6f9c0 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 n plan.wsFlags&W
6f9d0 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a HERE_IN_ABLE */.
6f9e0 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20 20 struct {.
6f9f0 20 20 20 57 68 65 72 65 50 6c 61 6e 20 2a 61 50 WherePlan *aP
6fa00 6c 61 6e 3b 20 20 20 20 20 2f 2a 20 50 6c 61 6e lan; /* Plan
6fa10 73 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20 s for each term
6fa20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 of the WHERE cla
6fa30 75 73 65 20 2a 2f 0a 20 20 20 20 7d 20 6f 72 3b use */. } or;
6fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6fa50 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 6c /* Used when pl
6fa60 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 an.wsFlags&WHERE
6fa70 5f 4d 55 4c 54 49 5f 4f 52 20 2a 2f 0a 20 20 7d _MULTI_OR */. }
6fa80 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f u;.. /* The fo
6fa90 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 69 73 llowing field is
6faa0 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72 74 really not part
6fab0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
6fac0 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 2a 2a level. But. **
6fad0 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 63 65 we need a place
6fae0 20 74 6f 20 63 61 63 68 65 20 76 69 72 74 75 61 to cache virtua
6faf0 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20 69 6e l table index in
6fb00 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 65 61 formation for ea
6fb10 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 ch. ** virtual
6fb20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f table in the FRO
6fb30 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65 M clause and the
6fb40 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74 72 75 WhereLevel stru
6fb50 63 74 75 72 65 20 69 73 0a 20 20 2a 2a 20 61 20 cture is. ** a
6fb60 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61 63 65 convenient place
6fb70 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 20 since there is
6fb80 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c 20 66 one WhereLevel f
6fb90 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63 6c 61 or each FROM cla
6fba0 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 use. ** element
6fbb0 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
6fbc0 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64 _index_info *pId
6fbd0 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64 65 78 xInfo; /* Index
6fbe0 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68 20 73 info for n-th s
6fbf0 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 7d ource table */.}
6fc00 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73 20 61 ;../*.** Flags a
6fc10 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 ppropriate for t
6fc20 68 65 20 77 63 74 72 6c 46 6c 61 67 73 20 70 61 he wctrlFlags pa
6fc30 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 rameter of sqlit
6fc40 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 2e 0a e3WhereBegin()..
6fc50 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 */.#define WHERE
6fc60 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 20 _ORDERBY_NORMAL
6fc70 20 20 30 78 30 30 30 30 20 2f 2a 20 4e 6f 2d 6f 0x0000 /* No-o
6fc80 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 p */.#define WHE
6fc90 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20 20 RE_ORDERBY_MIN
6fca0 20 20 20 20 30 78 30 30 30 31 20 2f 2a 20 4f 52 0x0001 /* OR
6fcb0 44 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e DER BY processin
6fcc0 67 20 66 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63 g for min() func
6fcd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 */.#define WHER
6fce0 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 20 20 E_ORDERBY_MAX
6fcf0 20 20 20 30 78 30 30 30 32 20 2f 2a 20 4f 52 44 0x0002 /* ORD
6fd00 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 ER BY processing
6fd10 20 66 6f 72 20 6d 61 78 28 29 20 66 75 6e 63 20 for max() func
6fd20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 */.#define WHERE
6fd30 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 _ONEPASS_DESIRED
6fd40 20 20 30 78 30 30 30 34 20 2f 2a 20 57 61 6e 74 0x0004 /* Want
6fd50 20 74 6f 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20 to do one-pass
6fd60 55 50 44 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f UPDATE/DELETE */
6fd70 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 46 .#define WHERE_F
6fd80 49 4c 4c 5f 52 4f 57 53 45 54 20 20 20 20 20 20 ILL_ROWSET
6fd90 30 78 30 30 30 38 20 20 2f 2a 20 53 61 76 65 20 0x0008 /* Save
6fda0 72 65 73 75 6c 74 73 20 69 6e 20 61 20 52 6f 77 results in a Row
6fdb0 53 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 Set object */.#d
6fdc0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4d 49 54 efine WHERE_OMIT
6fdd0 5f 4f 50 45 4e 20 20 20 20 20 20 20 20 30 78 30 _OPEN 0x0
6fde0 30 31 30 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 010 /* Table cu
6fdf0 72 73 6f 72 20 61 72 65 20 61 6c 72 65 61 64 79 rsor are already
6fe00 20 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 open */.#define
6fe10 20 57 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 WHERE_OMIT_CLOS
6fe20 45 20 20 20 20 20 20 20 30 78 30 30 32 30 20 20 E 0x0020
6fe30 2f 2a 20 4f 6d 69 74 20 63 6c 6f 73 65 20 6f 66 /* Omit close of
6fe40 20 74 61 62 6c 65 20 26 20 69 6e 64 65 78 20 63 table & index c
6fe50 75 72 73 6f 72 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a ursors */../*.**
6fe60 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 The WHERE claus
6fe70 65 20 70 72 6f 63 65 73 73 69 6e 67 20 72 6f 75 e processing rou
6fe80 74 69 6e 65 20 68 61 73 20 74 77 6f 20 68 61 6c tine has two hal
6fe90 76 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 72 ves. The.** fir
6fea0 73 74 20 70 61 72 74 20 64 6f 65 73 20 74 68 65 st part does the
6feb0 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 57 48 start of the WH
6fec0 45 52 45 20 6c 6f 6f 70 20 61 6e 64 20 74 68 65 ERE loop and the
6fed0 20 73 65 63 6f 6e 64 0a 2a 2a 20 68 61 6c 66 20 second.** half
6fee0 64 6f 65 73 20 74 68 65 20 74 61 69 6c 20 6f 66 does the tail of
6fef0 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e the WHERE loop.
6ff00 20 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 An instance of
6ff10 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 .** this structu
6ff20 72 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 re is returned b
6ff30 79 20 74 68 65 20 66 69 72 73 74 20 68 61 6c 66 y the first half
6ff40 20 61 6e 64 20 70 61 73 73 65 64 0a 2a 2a 20 69 and passed.** i
6ff50 6e 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 68 nto the second h
6ff60 61 6c 66 20 74 6f 20 67 69 76 65 20 73 6f 6d 65 alf to give some
6ff70 20 63 6f 6e 74 69 6e 75 69 74 79 2e 0a 2a 2f 0a continuity..*/.
6ff80 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f struct WhereInfo
6ff90 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 {. Parse *pPar
6ffa0 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 se; /* Par
6ffb0 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 sing and code ge
6ffc0 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 nerating context
6ffd0 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c 46 */. u16 wctrlF
6ffe0 6c 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 46 6c lags; /* Fl
6fff0 61 67 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 70 ags originally p
70000 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 assed to sqlite3
70010 57 68 65 72 65 42 65 67 69 6e 28 29 20 2a 2f 0a WhereBegin() */.
70020 20 20 75 38 20 6f 6b 4f 6e 65 50 61 73 73 3b 20 u8 okOnePass;
70030 20 20 20 20 20 20 20 2f 2a 20 4f 6b 20 74 6f 20 /* Ok to
70040 75 73 65 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 use one-pass alg
70050 6f 72 69 74 68 6d 20 66 6f 72 20 55 50 44 41 54 orithm for UPDAT
70060 45 20 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a 20 E or DELETE */.
70070 20 69 6e 74 20 72 65 67 52 6f 77 53 65 74 3b 20 int regRowSet;
70080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70090 2f 2a 20 53 74 6f 72 65 20 72 6f 77 69 64 73 20 /* Store rowids
700a0 69 6e 20 74 68 69 73 20 72 6f 77 73 65 74 20 69 in this rowset i
700b0 66 20 3e 3d 30 20 2a 2f 0a 20 20 53 72 63 4c 69 f >=0 */. SrcLi
700c0 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 st *pTabList;
700d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 /* Lis
700e0 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 t of tables in t
700f0 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74 he join */. int
70100 20 69 54 6f 70 3b 20 20 20 20 20 20 20 20 20 20 iTop;
70110 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
70120 68 65 20 76 65 72 79 20 62 65 67 69 6e 6e 69 6e he very beginnin
70130 67 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c g of the WHERE l
70140 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f oop */. int iCo
70150 6e 74 69 6e 75 65 3b 20 20 20 20 20 20 20 20 20 ntinue;
70160 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 /* Jump
70170 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 here to continue
70180 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f 72 with next recor
70190 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 d */. int iBrea
701a0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
701b0 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 /* Jump he
701c0 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 re to break out
701d0 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 of the loop */.
701e0 20 69 6e 74 20 6e 4c 65 76 65 6c 3b 20 20 20 20 int nLevel;
701f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70200 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 /* Number of nes
70210 74 65 64 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 74 ted loop */. st
70220 72 75 63 74 20 57 68 65 72 65 43 6c 61 75 73 65 ruct WhereClause
70230 20 2a 70 57 43 3b 20 20 20 20 20 20 20 2f 2a 20 *pWC; /*
70240 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 Decomposition of
70250 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
70260 65 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 e */. WhereLeve
70270 6c 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 l a[1];
70280 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 /* Informa
70290 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 tion about each
702a0 6e 65 73 74 20 6c 6f 6f 70 20 69 6e 20 57 48 45 nest loop in WHE
702b0 52 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 RE */.};../*.**
702c0 41 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 A NameContext de
702d0 66 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20 fines a context
702e0 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f in which to reso
702f0 6c 76 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f lve table and co
70300 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 lumn.** names.
70310 54 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 The context cons
70320 69 73 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f ists of a list o
70330 66 20 74 61 62 6c 65 73 20 28 74 68 65 20 70 53 f tables (the pS
70340 72 63 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e rcList) field an
70350 64 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e d.** a list of n
70360 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 amed expression
70370 28 70 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e (pEList). The n
70380 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 amed expression
70390 6c 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e list may.** be N
703a0 55 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63 ULL. The pSrc c
703b0 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 orresponds to th
703c0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 e FROM clause of
703d0 20 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 a SELECT or.**
703e0 74 6f 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 to the table bei
703f0 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 ng operated on b
70400 79 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 y INSERT, UPDATE
70410 2c 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68 , or DELETE. Th
70420 65 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 e.** pEList corr
70430 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 esponds to the r
70440 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 esult set of a S
70450 45 4c 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c ELECT and is NUL
70460 4c 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 L for.** other s
70470 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a tatements..**.**
70480 20 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 NameContexts ca
70490 6e 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68 n be nested. Wh
704a0 65 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d en resolving nam
704b0 65 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f es, the inner-mo
704c0 73 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 st .** context i
704d0 73 20 73 65 61 72 63 68 65 64 20 66 69 72 73 74 s searched first
704e0 2e 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 . If no match i
704f0 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 s found, the nex
70500 74 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 t outer.** conte
70510 78 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 xt is checked.
70520 49 66 20 74 68 65 72 65 20 69 73 20 73 74 69 6c If there is stil
70530 6c 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 l no match, the
70540 6e 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 next context.**
70550 69 73 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69 is checked. Thi
70560 73 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e s process contin
70570 75 65 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72 ues until either
70580 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e a match is foun
70590 64 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 d.** or all cont
705a0 65 78 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20 exts are check.
705b0 20 57 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73 When a match is
705c0 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 found, the nRef
705d0 20 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 member of.** th
705e0 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 e context contai
705f0 6e 69 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69 ning the match i
70600 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a s incremented. .
70610 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75 **.** Each subqu
70620 65 72 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e ery gets a new N
70630 61 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 ameContext. The
70640 20 70 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69 pNext field poi
70650 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 nts to the.** Na
70660 6d 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 meContext in the
70670 20 70 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20 parent query.
70680 54 68 75 73 20 74 68 65 20 70 72 6f 63 65 73 73 Thus the process
70690 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 of scanning the
706a0 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 .** NameContext
706b0 6c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 list corresponds
706c0 20 74 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68 to searching th
706d0 72 6f 75 67 68 20 73 75 63 63 65 73 73 69 76 65 rough successive
706e0 6c 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 ly outer.** subq
706f0 75 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 ueries looking f
70700 6f 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 or a match..*/.s
70710 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 truct NameContex
70720 74 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 t {. Parse *pPa
70730 72 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 rse; /* Th
70740 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 e parser */. Sr
70750 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b cList *pSrcList;
70760 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 /* One or mor
70770 65 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f e tables used to
70780 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a resolve names *
70790 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
707a0 4c 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 List; /* Opti
707b0 6f 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d onal list of nam
707c0 65 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a ed expressions *
707d0 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 /. int nRef;
707e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
707f0 65 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f er of names reso
70800 6c 76 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e lved by this con
70810 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 text */. int nE
70820 72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f rr; /
70830 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f * Number of erro
70840 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 rs encountered w
70850 68 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e hile resolving n
70860 61 6d 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c ames */. u8 all
70870 6f 77 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f owAgg; /
70880 2a 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 * Aggregate func
70890 74 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 tions allowed he
708a0 72 65 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 re */. u8 hasAg
708b0 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 g; /*
708c0 54 72 75 65 20 69 66 20 61 67 67 72 65 67 61 74 True if aggregat
708d0 65 73 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 es are seen */.
708e0 20 75 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20 u8 isCheck;
708f0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
70900 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 resolving names
70910 20 69 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 in a CHECK cons
70920 74 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 traint */. int
70930 6e 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 nDepth;
70940 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62 /* Depth of sub
70950 71 75 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e query recursion.
70960 20 31 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 1 for no recurs
70970 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f ion */. AggInfo
70980 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a *pAggInfo; /*
70990 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f Information abo
709a0 75 74 20 61 67 67 72 65 67 61 74 65 73 20 61 74 ut aggregates at
709b0 20 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 this level */.
709c0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e NameContext *pN
709d0 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 ext; /* Next ou
709e0 74 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 ter name context
709f0 2e 20 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 . NULL for oute
70a00 72 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a rmost */.};../*.
70a10 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
70a20 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
70a30 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
70a40 6e 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 ns all informati
70a50 6f 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 on.** needed to
70a60 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f generate code fo
70a70 72 20 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 r a single SELEC
70a80 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a T statement..**.
70a90 2a 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 ** nLimit is set
70aa0 20 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 to -1 if there
70ab0 69 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 is no LIMIT clau
70ac0 73 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20 se. nOffset is
70ad0 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 set to 0..** If
70ae0 74 68 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54 there is a LIMIT
70af0 20 63 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72 clause, the par
70b00 73 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 ser sets nLimit
70b10 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 to the value of
70b20 74 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 the.** limit and
70b30 20 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 nOffset to the
70b40 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66 value of the off
70b50 73 65 74 20 28 6f 72 20 30 20 69 66 20 74 68 65 set (or 0 if the
70b60 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 re is not.** off
70b70 73 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72 set). But later
70b80 20 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 on, nLimit and
70b90 6e 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74 nOffset become t
70ba0 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 he memory locati
70bb0 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 ons.** in the VD
70bc0 42 45 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 BE that record t
70bd0 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 he limit and off
70be0 73 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a set counters..**
70bf0 0a 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d .** addrOpenEphm
70c00 5b 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 [] entries conta
70c10 69 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f in the address o
70c20 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 f OP_OpenEphemer
70c30 61 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 al opcodes..** T
70c40 68 65 73 65 20 61 64 64 72 65 73 73 65 73 20 6d hese addresses m
70c50 75 73 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f ust be stored so
70c60 20 74 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20 that we can go
70c70 62 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e back and fill in
70c80 0a 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e .** the P4_KEYIN
70c90 46 4f 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65 FO and P2 parame
70ca0 74 65 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 ters later. Nei
70cb0 74 68 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f ther the KeyInfo
70cc0 20 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 nor.** the numb
70cd0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
70ce0 20 50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 P2 can be compu
70cf0 74 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 ted at the same
70d00 74 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f time.** as the O
70d10 50 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 P_OpenEphm instr
70d20 75 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 uction is coded
70d30 62 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 because not.** e
70d40 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f nough informatio
70d50 6e 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 n about the comp
70d60 6f 75 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e ound query is kn
70d70 6f 77 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e own at that poin
70d80 74 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 t..** The KeyInf
70d90 6f 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 o for addrOpenTr
70da0 61 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f an[0] and [1] co
70db0 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 ntains collating
70dc0 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f sequences.** fo
70dd0 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 r the result set
70de0 2e 20 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 . The KeyInfo f
70df0 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b or addrOpenTran[
70e00 32 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 2] contains coll
70e10 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 ating.** sequenc
70e20 65 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 es for the ORDER
70e30 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 BY clause..*/.s
70e40 74 72 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 truct Select {.
70e50 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
70e60 74 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 t; /* The f
70e70 69 65 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73 ields of the res
70e80 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 ult */. u8 op;
70e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70ea0 2f 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e /* One of: TK_UN
70eb0 49 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e ION TK_ALL TK_IN
70ec0 54 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50 TERSECT TK_EXCEP
70ed0 54 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 T */. char affi
70ee0 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a nity; /*
70ef0 20 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 MakeRecord with
70f00 20 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 66 this affinity f
70f10 6f 72 20 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 or SRT_Set */.
70f20 75 31 36 20 73 65 6c 46 6c 61 67 73 3b 20 20 20 u16 selFlags;
70f30 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 /* Variou
70f40 73 20 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f s SF_* values */
70f50 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 . SrcList *pSrc
70f60 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
70f70 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a FROM clause */.
70f80 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 Expr *pWhere;
70f90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
70fa0 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a WHERE clause */.
70fb0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f ExprList *pGro
70fc0 75 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 upBy; /* The
70fd0 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
70fe0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 */. Expr *pHavi
70ff0 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ng; /* T
71000 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 he HAVING clause
71010 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
71020 70 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 pOrderBy; /*
71030 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 The ORDER BY cla
71040 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 use */. Select
71050 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 20 *pPrior;
71060 2f 2a 20 50 72 69 6f 72 20 73 65 6c 65 63 74 20 /* Prior select
71070 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 in a compound se
71080 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a lect statement *
71090 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 78 /. Select *pNex
710a0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 t; /* Ne
710b0 78 74 20 73 65 6c 65 63 74 20 74 6f 20 74 68 65 xt select to the
710c0 20 6c 65 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f left in a compo
710d0 75 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 und */. Select
710e0 2a 70 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 *pRightmost;
710f0 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 /* Right-most se
71100 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 lect in a compou
71110 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d nd select statem
71120 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ent */. Expr *p
71130 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 Limit;
71140 2f 2a 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 /* LIMIT express
71150 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 ion. NULL means
71160 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 not used. */. E
71170 78 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 xpr *pOffset;
71180 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 /* OFFSET
71190 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c expression. NULL
711a0 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e means not used.
711b0 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 */. int iLimit
711c0 2c 20 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 , iOffset; /*
711d0 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 Memory registers
711e0 20 68 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 holding LIMIT &
711f0 20 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73 OFFSET counters
71200 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 */. int addrOp
71210 65 6e 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 enEphm[3]; /*
71220 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 OP_OpenEphem opc
71230 6f 64 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 odes related to
71240 74 68 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d this select */.}
71250 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 ;../*.** Allowed
71260 20 76 61 6c 75 65 73 20 66 6f 72 20 53 65 6c 65 values for Sele
71270 63 74 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54 68 ct.selFlags. Th
71280 65 20 22 53 46 22 20 70 72 65 66 69 78 20 73 74 e "SF" prefix st
71290 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c ands for.** "Sel
712a0 65 63 74 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 ect Flag"..*/.#d
712b0 65 66 69 6e 65 20 53 46 5f 44 69 73 74 69 6e 63 efine SF_Distinc
712c0 74 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 t 0x0001
712d0 20 2f 2a 20 4f 75 74 70 75 74 20 73 68 6f 75 6c /* Output shoul
712e0 64 20 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f d be DISTINCT */
712f0 0a 23 64 65 66 69 6e 65 20 53 46 5f 52 65 73 6f .#define SF_Reso
71300 6c 76 65 64 20 20 20 20 20 20 20 20 30 78 30 30 lved 0x00
71310 30 32 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 02 /* Identifie
71320 72 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 rs have been res
71330 6f 6c 76 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 olved */.#define
71340 20 53 46 5f 41 67 67 72 65 67 61 74 65 20 20 20 SF_Aggregate
71350 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 43 0x0004 /* C
71360 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 ontains aggregat
71370 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 e functions */.#
71380 64 65 66 69 6e 65 20 53 46 5f 55 73 65 73 45 70 define SF_UsesEp
71390 68 65 6d 65 72 61 6c 20 20 20 30 78 30 30 30 38 hemeral 0x0008
713a0 20 20 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70 /* Uses the Op
713b0 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f enEphemeral opco
713c0 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 de */.#define SF
713d0 5f 45 78 70 61 6e 64 65 64 20 20 20 20 20 20 20 _Expanded
713e0 20 30 78 30 30 31 30 20 20 2f 2a 20 73 71 6c 69 0x0010 /* sqli
713f0 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 te3SelectExpand(
71400 29 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 ) called on this
71410 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 */.#define SF_H
71420 61 73 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30 asTypeInfo 0
71430 78 30 30 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 x0020 /* FROM s
71440 75 62 71 75 65 72 69 65 73 20 68 61 76 65 20 54 ubqueries have T
71450 61 62 6c 65 20 6d 65 74 61 64 61 74 61 20 2a 2f able metadata */
71460 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 .../*.** The res
71470 75 6c 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74 ults of a select
71480 20 63 61 6e 20 62 65 20 64 69 73 74 72 69 62 75 can be distribu
71490 74 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 ted in several w
714a0 61 79 73 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52 ays. The.** "SR
714b0 54 22 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20 T" prefix means
714c0 22 53 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54 "SELECT Result T
714d0 79 70 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ype"..*/.#define
714e0 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 SRT_Union
714f0 20 20 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 1 /* Store re
71500 73 75 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20 sult as keys in
71510 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 an index */.#def
71520 69 6e 65 20 53 52 54 5f 45 78 63 65 70 74 20 20 ine SRT_Except
71530 20 20 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 2 /* Remov
71540 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 e result from a
71550 55 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 UNION index */.#
71560 64 65 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74 define SRT_Exist
71570 73 20 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74 s 3 /* St
71580 6f 72 65 20 31 20 69 66 20 74 68 65 20 72 65 73 ore 1 if the res
71590 75 6c 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 ult is not empty
715a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f */.#define SRT_
715b0 44 69 73 63 61 72 64 20 20 20 20 20 20 34 20 20 Discard 4
715c0 2f 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 /* Do not save t
715d0 68 65 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68 he results anywh
715e0 65 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f ere */../* The O
715f0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 RDER BY clause i
71600 73 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c s ignored for al
71610 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a l of the above *
71620 2f 0a 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 /.#define Ignora
71630 62 6c 65 4f 72 64 65 72 62 79 28 58 29 20 28 28 bleOrderby(X) ((
71640 58 2d 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44 X->eDest)<=SRT_D
71650 69 73 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 iscard)..#define
71660 20 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20 SRT_Output
71670 20 20 35 20 20 2f 2a 20 4f 75 74 70 75 74 20 65 5 /* Output e
71680 61 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c ach row of resul
71690 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 t */.#define SRT
716a0 5f 4d 65 6d 20 20 20 20 20 20 20 20 20 20 36 20 _Mem 6
716b0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 /* Store result
716c0 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c in a memory cel
716d0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 l */.#define SRT
716e0 5f 53 65 74 20 20 20 20 20 20 20 20 20 20 37 20 _Set 7
716f0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 /* Store result
71700 73 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 s as keys in an
71710 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 index */.#define
71720 20 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20 SRT_Table
71730 20 20 38 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 8 /* Store re
71740 73 75 6c 74 20 61 73 20 64 61 74 61 20 77 69 74 sult as data wit
71750 68 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 h an automatic r
71760 6f 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 owid */.#define
71770 53 52 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 SRT_EphemTab
71780 20 39 20 20 2f 2a 20 43 72 65 61 74 65 20 74 72 9 /* Create tr
71790 61 6e 73 69 65 6e 74 20 74 61 62 20 61 6e 64 20 ansient tab and
717a0 73 74 6f 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 store like SRT_T
717b0 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 able */.#define
717c0 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 SRT_Coroutine
717d0 31 30 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 10 /* Generate
717e0 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 a single row of
717f0 72 65 73 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a result */../*.**
71800 20 41 20 73 74 72 75 63 74 75 72 65 20 75 73 65 A structure use
71810 64 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 d to customize t
71820 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 73 he behavior of s
71830 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20 qlite3Select().
71840 53 65 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 See.** comments
71850 61 62 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c above sqlite3Sel
71860 65 63 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c ect() for detail
71870 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 s..*/.typedef st
71880 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 ruct SelectDest
71890 53 65 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75 SelectDest;.stru
718a0 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a ct SelectDest {.
718b0 20 20 75 38 20 65 44 65 73 74 3b 20 20 20 20 20 u8 eDest;
718c0 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 /* How to di
718d0 73 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73 spose of the res
718e0 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66 ults */. u8 aff
718f0 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 inity; /* A
71900 66 66 69 6e 69 74 79 20 75 73 65 64 20 77 68 65 ffinity used whe
71910 6e 20 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 n eDest==SRT_Set
71920 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b */. int iParm;
71930 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 /* A par
71940 61 6d 65 74 65 72 20 75 73 65 64 20 62 79 20 74 ameter used by t
71950 68 65 20 65 44 65 73 74 20 64 69 73 70 6f 73 61 he eDest disposa
71960 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e l method */. in
71970 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 t iMem;
71980 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72 /* Base register
71990 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 where results a
719a0 72 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 re written */.
719b0 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 int nMem;
719c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
719d0 65 67 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 egisters allocat
719e0 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ed */.};../*.**
719f0 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f An SQL parser co
71a00 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f ntext. A copy o
71a10 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
71a20 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 is passed throu
71a30 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72 gh.** the parser
71a40 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61 and down into a
71a50 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63 ll the parser ac
71a60 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20 tion routine in
71a70 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72 order to.** carr
71a80 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 y around informa
71a90 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f tion that is glo
71aa0 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72 bal to the entir
71ab0 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 e parse..**.** T
71ac0 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 he structure is
71ad0 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f divided into two
71ae0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68 parts. When th
71af0 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 e parser and cod
71b00 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61 e.** generate ca
71b10 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65 ll themselves re
71b20 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66 cursively, the f
71b30 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 irst part of the
71b40 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 structure.** is
71b50 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68 constant but th
71b60 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73 e second part is
71b70 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65 reset at the be
71b80 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 ginning and end
71b90 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72 of.** each recur
71ba0 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sion..**.** The
71bb0 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 nTableLock and a
71bc0 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62 TableLock variab
71bd0 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65 les are only use
71be0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d d if the shared-
71bf0 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72 cache .** featur
71c00 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66 e is enabled (if
71c10 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75 sqlite3Tsd()->u
71c20 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20 seSharedData is
71c30 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a true). They are.
71c40 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 ** used to store
71c50 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c the set of tabl
71c60 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64 e-locks required
71c70 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e by the statemen
71c80 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 t being.** compi
71c90 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71 led. Function sq
71ca0 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 lite3TableLock()
71cb0 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 is used to add
71cc0 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a entries to the.*
71cd0 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 * list..*/.struc
71ce0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69 t Parse {. sqli
71cf0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
71d00 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74 /* The main dat
71d10 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20 abase structure
71d20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
71d30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
71d40 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78 urn code from ex
71d50 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 ecution */. cha
71d60 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 r *zErrMsg;
71d70 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 /* An error me
71d80 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20 ssage */. Vdbe
71d90 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20 *pVdbe;
71da0 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72 /* An engine for
71db0 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62 executing datab
71dc0 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a ase bytecode */.
71dd0 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74 u8 colNamesSet
71de0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61 ; /* TRUE a
71df0 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 fter OP_ColumnNa
71e00 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75 me has been issu
71e10 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20 ed to pVdbe */.
71e20 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20 u8 nameClash;
71e30 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61 /* A perma
71e40 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20 nent table name
71e50 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d clashes with tem
71e60 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a p table name */.
71e70 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61 u8 checkSchema
71e80 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73 ; /* Causes
71e90 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 schema cookie c
71ea0 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 heck after an er
71eb0 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74 ror */. u8 nest
71ec0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ed; /*
71ed0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
71ee0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70 d calls to the p
71ef0 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72 arser/code gener
71f00 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72 ator */. u8 par
71f10 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f seError; /
71f20 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70 * True after a p
71f30 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54 arsing error. T
71f40 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20 icket #1794 */.
71f50 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20 u8 nTempReg;
71f60 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
71f70 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 of temporary reg
71f80 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52 isters in aTempR
71f90 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65 eg[] */. u8 nTe
71fa0 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f mpInUse; /
71fb0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d * Number of aTem
71fc0 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79 pReg[] currently
71fd0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
71fe0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38 int aTempReg[8
71ff0 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e ]; /* Holdin
72000 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f g area for tempo
72010 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a rary registers *
72020 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65 /. int nRangeRe
72030 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 g; /* Size
72040 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 of the temporar
72050 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b y register block
72060 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65 */. int iRange
72070 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 Reg; /* Fi
72080 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 rst register in
72090 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 temporary regist
720a0 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e er block */. in
720b0 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 t nErr;
720c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
720d0 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20 errors seen */.
720e0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20 int nTab;
720f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
72100 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c of previously al
72110 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72 located VDBE cur
72120 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d sors */. int nM
72130 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f em; /
72140 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f * Number of memo
72150 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f ry cells used so
72160 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 far */. int nS
72170 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f et; /
72180 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 * Number of sets
72190 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a used so far */.
721a0 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20 int ckBase;
721b0 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 /* Base r
721c0 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20 egister of data
721d0 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e during check con
721e0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e straints */. in
721f0 74 20 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 t disableColCach
72200 65 3b 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69 e; /* True to di
72210 73 61 62 6c 65 20 61 64 64 69 6e 67 20 74 6f 20 sable adding to
72220 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a column cache */.
72230 20 20 69 6e 74 20 6e 43 6f 6c 43 61 63 68 65 3b int nColCache;
72240 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
72250 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 of entries in t
72260 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 he column cache
72270 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 43 61 63 */. int iColCac
72280 68 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 he; /* Nex
72290 74 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 t entry of the c
722a0 61 63 68 65 20 74 6f 20 72 65 70 6c 61 63 65 20 ache to replace
722b0 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c */. struct yCol
722c0 43 61 63 68 65 20 7b 0a 20 20 20 20 69 6e 74 20 Cache {. int
722d0 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 iTable;
722e0 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f /* Table curso
722f0 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 r number */.
72300 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 int iColumn;
72310 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 /* Table c
72320 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a olumn number */.
72330 20 20 20 20 63 68 61 72 20 61 66 66 43 68 61 6e char affChan
72340 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ge; /* Tru
72350 65 20 69 66 20 74 68 69 73 20 72 65 67 69 73 74 e if this regist
72360 65 72 20 68 61 73 20 68 61 64 20 61 6e 20 61 66 er has had an af
72370 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 2a 2f finity change */
72380 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 . int iReg;
72390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
723a0 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 76 gister holding v
723b0 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c alue of this col
723c0 75 6d 6e 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 umn */. } aColC
723d0 61 63 68 65 5b 31 30 5d 3b 20 20 20 20 20 2f 2a ache[10]; /*
723e0 20 4f 6e 65 20 66 6f 72 20 65 61 63 68 20 76 61 One for each va
723f0 6c 69 64 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 lid column cache
72400 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32 20 entry */. u32
72410 77 72 69 74 65 4d 61 73 6b 3b 20 20 20 20 20 20 writeMask;
72420 20 2f 2a 20 53 74 61 72 74 20 61 20 77 72 69 74 /* Start a writ
72430 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e e transaction on
72440 20 74 68 65 73 65 20 64 61 74 61 62 61 73 65 73 these databases
72450 20 2a 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65 */. u32 cookie
72460 4d 61 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69 Mask; /* Bi
72470 74 6d 61 73 6b 20 6f 66 20 73 63 68 65 6d 61 20 tmask of schema
72480 76 65 72 69 66 69 65 64 20 64 61 74 61 62 61 73 verified databas
72490 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b es */. int cook
724a0 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20 ieGoto; /*
724b0 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f Address of OP_Go
724c0 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72 to to cookie ver
724d0 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65 ifier subroutine
724e0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 */. int cookie
724f0 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58 Value[SQLITE_MAX
72500 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f _ATTACHED+2]; /
72510 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b * Values of cook
72520 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f ies to verify */
72530 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
72540 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
72550 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f E. int nTableLo
72560 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ck; /* Nu
72570 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e mber of locks in
72580 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20 aTableLock */.
72590 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62 TableLock *aTab
725a0 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69 leLock; /* Requi
725b0 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 red table locks
725c0 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 for shared-cache
725d0 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a mode */.#endif.
725e0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 int regRowid;
725f0 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
72600 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 er holding rowid
72610 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c 45 of CREATE TABLE
72620 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 entry */. int
72630 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 regRoot;
72640 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c /* Register hol
72650 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e ding root page n
72660 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62 umber for new ob
72670 6a 65 63 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41 jects */.. /* A
72680 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e 74 bove is constant
72690 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73 69 between recursi
726a0 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 72 ons. Below is r
726b0 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64 20 eset before and
726c0 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 20 after. ** each
726d0 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 20 recursion */..
726e0 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20 int nVar;
726f0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
72700 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73 20 f '?' variables
72710 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c 20 seen in the SQL
72720 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 so far */. int
72730 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20 20 nVarExpr;
72740 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 /* Number of us
72750 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 ed slots in apVa
72760 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e 74 rExpr[] */. int
72770 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b 20 nVarExprAlloc;
72780 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
72790 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 69 llocated slots i
727a0 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f n apVarExpr[] */
727b0 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72 45 . Expr **apVarE
727c0 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 xpr; /* Point
727d0 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 20 ers to :aaa and
727e0 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20 65 $aaaa wildcard e
727f0 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 xpressions */.
72800 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20 20 20 int nAlias;
72810 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
72820 66 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 f aliased result
72830 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a set columns */.
72840 20 20 69 6e 74 20 6e 41 6c 69 61 73 41 6c 6c 6f int nAliasAllo
72850 63 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 c; /* Number
72860 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c of allocated sl
72870 6f 74 73 20 66 6f 72 20 61 41 6c 69 61 73 5b 5d ots for aAlias[]
72880 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 41 6c 69 61 */. int *aAlia
72890 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 s; /* Re
728a0 67 69 73 74 65 72 20 75 73 65 64 20 74 6f 20 68 gister used to h
728b0 6f 6c 64 20 61 6c 69 61 73 65 64 20 72 65 73 75 old aliased resu
728c0 6c 74 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 lt */. u8 expla
728d0 69 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 in; /*
728e0 54 72 75 65 20 69 66 20 74 68 65 20 45 58 50 4c True if the EXPL
728f0 41 49 4e 20 66 6c 61 67 20 69 73 20 66 6f 75 6e AIN flag is foun
72900 64 20 6f 6e 20 74 68 65 20 71 75 65 72 79 20 2a d on the query *
72910 2f 0a 20 20 54 6f 6b 65 6e 20 73 45 72 72 54 6f /. Token sErrTo
72920 6b 65 6e 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 ken; /* The
72930 74 6f 6b 65 6e 20 61 74 20 77 68 69 63 68 20 74 token at which t
72940 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 he error occurre
72950 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4e 61 d */. Token sNa
72960 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 meToken; /* T
72970 6f 6b 65 6e 20 77 69 74 68 20 75 6e 71 75 61 6c oken with unqual
72980 69 66 69 65 64 20 73 63 68 65 6d 61 20 6f 62 6a ified schema obj
72990 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f ect name */. To
729a0 6b 65 6e 20 73 4c 61 73 74 54 6f 6b 65 6e 3b 20 ken sLastToken;
729b0 20 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 74 /* The last t
729c0 6f 6b 65 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 oken parsed */.
729d0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 const char *zSq
729e0 6c 3b 20 20 20 20 2f 2a 20 41 6c 6c 20 53 51 4c l; /* All SQL
729f0 20 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 text */. const
72a00 20 63 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20 char *zTail;
72a10 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 /* All SQL text
72a20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 65 past the last se
72a30 6d 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a micolon parsed *
72a40 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54 /. Table *pNewT
72a50 61 62 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 61 able; /* A ta
72a60 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 ble being constr
72a70 75 63 74 65 64 20 62 79 20 43 52 45 41 54 45 20 ucted by CREATE
72a80 54 41 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67 TABLE */. Trigg
72a90 65 72 20 2a 70 4e 65 77 54 72 69 67 67 65 72 3b er *pNewTrigger;
72aa0 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 /* Trigger
72ab0 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 20 under construct
72ac0 62 79 20 61 20 43 52 45 41 54 45 20 54 52 49 47 by a CREATE TRIG
72ad0 47 45 52 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 GER */. Trigger
72ae0 53 74 61 63 6b 20 2a 74 72 69 67 53 74 61 63 6b Stack *trigStack
72af0 3b 20 20 2f 2a 20 54 72 69 67 67 65 72 20 61 63 ; /* Trigger ac
72b00 74 69 6f 6e 73 20 62 65 69 6e 67 20 63 6f 64 65 tions being code
72b10 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 d */. const cha
72b20 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b r *zAuthContext;
72b30 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61 72 61 /* The 6th para
72b40 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 41 75 meter to db->xAu
72b50 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a th callbacks */.
72b60 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
72b70 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
72b80 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b 20 20 . Token sArg;
72b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
72ba0 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f Complete text o
72bb0 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d f a module argum
72bc0 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 63 6c ent */. u8 decl
72bd0 61 72 65 56 74 61 62 3b 20 20 20 20 20 20 20 20 areVtab;
72be0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 /* True if i
72bf0 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f 64 65 nside sqlite3_de
72c00 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a 2f 0a clare_vtab() */.
72c10 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 6b 3b int nVtabLock;
72c20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
72c30 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74 75 61 Number of virtua
72c40 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f 63 6b l tables to lock
72c50 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70 */. Table **ap
72c60 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20 VtabLock;
72c70 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 /* Pointer to v
72c80 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6e 65 irtual tables ne
72c90 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f eding locking */
72ca0 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e 48 .#endif. int nH
72cb0 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 eight;
72cc0 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 /* Expression
72cd0 74 72 65 65 20 68 65 69 67 68 74 20 6f 66 20 63 tree height of c
72ce0 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c 65 63 urrent sub-selec
72cf0 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 5a t */. Table *pZ
72d00 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20 20 2f ombieTab; /
72d10 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c 65 20 * List of Table
72d20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c 65 74 objects to delet
72d30 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 65 6e e after code gen
72d40 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53 */.};..#ifdef S
72d50 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
72d60 41 4c 54 41 42 4c 45 0a 20 20 23 64 65 66 69 6e ALTABLE. #defin
72d70 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 e IN_DECLARE_VTA
72d80 42 20 30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 B 0.#else. #def
72d90 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 ine IN_DECLARE_V
72da0 54 41 42 20 28 70 50 61 72 73 65 2d 3e 64 65 63 TAB (pParse->dec
72db0 6c 61 72 65 56 74 61 62 29 0a 23 65 6e 64 69 66 lareVtab).#endif
72dc0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
72dd0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
72de0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 wing structure c
72df0 61 6e 20 62 65 20 64 65 63 6c 61 72 65 64 20 6f an be declared o
72e00 6e 20 61 20 73 74 61 63 6b 20 61 6e 64 20 75 73 n a stack and us
72e10 65 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 68 ed.** to save th
72e20 65 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e e Parse.zAuthCon
72e30 74 65 78 74 20 76 61 6c 75 65 20 73 6f 20 74 68 text value so th
72e40 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 73 at it can be res
72e50 74 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a tored later..*/.
72e60 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 struct AuthConte
72e70 78 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 xt {. const cha
72e80 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b r *zAuthContext;
72e90 20 20 20 2f 2a 20 50 75 74 20 73 61 76 65 64 20 /* Put saved
72ea0 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 Parse.zAuthConte
72eb0 78 74 20 68 65 72 65 20 2a 2f 0a 20 20 50 61 72 xt here */. Par
72ec0 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
72ed0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
72ee0 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 Parse structure
72ef0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 */.};../*.** Bit
72f00 66 69 65 6c 64 20 66 6c 61 67 73 20 66 6f 72 20 field flags for
72f10 50 32 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f 49 P2 value in OP_I
72f20 6e 73 65 72 74 20 61 6e 64 20 4f 50 5f 44 65 6c nsert and OP_Del
72f30 65 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f ete.*/.#define O
72f40 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20 20 PFLAG_NCHANGE
72f50 31 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 1 /* Set to u
72f60 70 64 61 74 65 20 64 62 2d 3e 6e 43 68 61 6e 67 pdate db->nChang
72f70 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 e */.#define OPF
72f80 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 32 20 LAG_LASTROWID 2
72f90 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 /* Set to upd
72fa0 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 ate db->lastRowi
72fb0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 d */.#define OPF
72fc0 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20 34 20 LAG_ISUPDATE 4
72fd0 20 20 20 2f 2a 20 54 68 69 73 20 4f 50 5f 49 6e /* This OP_In
72fe0 73 65 72 74 20 69 73 20 61 6e 20 73 71 6c 20 55 sert is an sql U
72ff0 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 PDATE */.#define
73000 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 20 20 OPFLAG_APPEND
73010 20 20 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69 8 /* This i
73020 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 s likely to be a
73030 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 0a 2f 2a 0a n append */../*.
73040 20 2a 20 45 61 63 68 20 74 72 69 67 67 65 72 20 * Each trigger
73050 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64 present in the d
73060 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 atabase schema i
73070 73 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20 69 s stored as an i
73080 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73 74 nstance of. * st
73090 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 0a 20 ruct Trigger. .
730a0 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f *. * Pointers to
730b0 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 instances of st
730c0 72 75 63 74 20 54 72 69 67 67 65 72 20 61 72 65 ruct Trigger are
730d0 20 73 74 6f 72 65 64 20 69 6e 20 74 77 6f 20 77 stored in two w
730e0 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74 68 ays.. * 1. In th
730f0 65 20 22 74 72 69 67 48 61 73 68 22 20 68 61 73 e "trigHash" has
73100 68 20 74 61 62 6c 65 20 28 70 61 72 74 20 6f 66 h table (part of
73110 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 74 68 the sqlite3* th
73120 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 at represents th
73130 65 20 0a 20 2a 20 20 20 20 64 61 74 61 62 61 73 e . * databas
73140 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 e). This allows
73150 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75 72 Trigger structur
73160 65 73 20 74 6f 20 62 65 20 72 65 74 72 69 65 76 es to be retriev
73170 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 ed by name.. * 2
73180 2e 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 61 . All triggers a
73190 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
731a0 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 66 6f single table fo
731b0 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 rm a linked list
731c0 2c 20 75 73 69 6e 67 20 74 68 65 0a 20 2a 20 20 , using the. *
731d0 20 20 70 4e 65 78 74 20 6d 65 6d 62 65 72 20 6f pNext member o
731e0 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 f struct Trigger
731f0 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 . A pointer to t
73200 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 he first element
73210 20 6f 66 20 74 68 65 0a 20 2a 20 20 20 20 6c 69 of the. * li
73220 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 73 74 6f nked list is sto
73230 72 65 64 20 61 73 20 74 68 65 20 22 70 54 72 69 red as the "pTri
73240 67 67 65 72 22 20 6d 65 6d 62 65 72 20 6f 66 20 gger" member of
73250 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20 the associated.
73260 2a 20 20 20 20 73 74 72 75 63 74 20 54 61 62 6c * struct Tabl
73270 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73 74 e.. *. * The "st
73280 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 ep_list" member
73290 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 points to the fi
732a0 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 rst element of a
732b0 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a 20 linked list. *
732c0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 53 containing the S
732d0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 73 70 QL statements sp
732e0 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20 74 ecified as the t
732f0 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a rigger program..
73300 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 */.struct Trigg
73310 65 72 20 7b 0a 20 20 63 68 61 72 20 2a 6e 61 6d er {. char *nam
73320 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
73330 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
73340 65 20 74 72 69 67 67 65 72 20 20 20 20 20 20 20 e trigger
73350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73360 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61 62 6c */. char *tabl
73370 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
73380 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 The table or vi
73390 65 77 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 ew to which the
733a0 74 72 69 67 67 65 72 20 61 70 70 6c 69 65 73 20 trigger applies
733b0 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 */. u8 op;
733c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
733d0 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45 One of TK_DELETE
733e0 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f , TK_UPDATE, TK_
733f0 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 2a INSERT *
73400 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20 20 /. u8 tr_tm;
73410 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
73420 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 ne of TRIGGER_BE
73430 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41 46 FORE, TRIGGER_AF
73440 54 45 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 TER */. Expr *p
73450 57 68 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 When;
73460 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 63 6c 61 /* The WHEN cla
73470 75 73 65 20 6f 66 20 74 68 65 20 65 78 70 72 65 use of the expre
73480 73 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e 55 ssion (may be NU
73490 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 LL) */. IdList
734a0 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20 20 *pColumns;
734b0 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 /* If this is a
734c0 6e 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f 6c n UPDATE OF <col
734d0 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67 65 umn-list> trigge
734e0 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 r,.
734f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73500 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 the <column-list
73510 3e 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 > is stored here
73520 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 6e 61 6d 65 */. Token name
73530 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 2f 2a Token; /*
73540 20 54 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 69 6e Token containin
73550 67 20 7a 4e 61 6d 65 2e 20 55 73 65 20 64 75 72 g zName. Use dur
73560 69 6e 67 20 70 61 72 73 69 6e 67 20 6f 6e 6c 79 ing parsing only
73570 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 */. Schema *pS
73580 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a chema; /*
73590 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 Schema containi
735a0 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20 2a ng the trigger *
735b0 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61 62 /. Schema *pTab
735c0 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 53 Schema; /* S
735d0 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 chema containing
735e0 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
735f0 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 74 65 TriggerStep *ste
73600 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b 20 p_list; /* Link
73610 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 20 list of trigger
73620 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20 20 program steps
73630 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 54 */. T
73640 72 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20 20 rigger *pNext;
73650 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 74 /* Next t
73660 72 69 67 67 65 72 20 61 73 73 6f 63 69 61 74 65 rigger associate
73670 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 d with the table
73680 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 */.};../*.** A
73690 74 72 69 67 67 65 72 20 69 73 20 65 69 74 68 65 trigger is eithe
736a0 72 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61 6e r a BEFORE or an
736b0 20 41 46 54 45 52 20 74 72 69 67 67 65 72 2e 20 AFTER trigger.
736c0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
736d0 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74 65 onstants.** dete
736e0 72 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a 2a rmine which. .**
736f0 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 .** If there are
73700 20 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67 65 multiple trigge
73710 72 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f 66 rs, you might of
73720 20 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e 64 some BEFORE and
73730 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a 20 some AFTER..**
73740 49 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20 74 In that cases, t
73750 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c he constants bel
73760 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20 74 ow can be ORed t
73770 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 ogether..*/.#def
73780 69 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46 4f ine TRIGGER_BEFO
73790 52 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52 RE 1.#define TR
737a0 49 47 47 45 52 5f 41 46 54 45 52 20 20 20 32 0a IGGER_AFTER 2.
737b0 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e ./*. * An instan
737c0 63 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 ce of struct Tri
737d0 67 67 65 72 53 74 65 70 20 69 73 20 75 73 65 64 ggerStep is used
737e0 20 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e 67 to store a sing
737f0 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 le SQL statement
73800 0a 20 2a 20 74 68 61 74 20 69 73 20 61 20 70 61 . * that is a pa
73810 72 74 20 6f 66 20 61 20 74 72 69 67 67 65 72 2d rt of a trigger-
73820 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a 20 program. . *. *
73830 49 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72 Instances of str
73840 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 uct TriggerStep
73850 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 are stored in a
73860 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c 69 singly linked li
73870 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75 73 st (linked. * us
73880 69 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22 20 ing the "pNext"
73890 6d 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e 63 member) referenc
738a0 65 64 20 62 79 20 74 68 65 20 22 73 74 65 70 5f ed by the "step_
738b0 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66 20 list" member of
738c0 74 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61 74 the . * associat
738d0 65 64 20 73 74 72 75 63 74 20 54 72 69 67 67 65 ed struct Trigge
738e0 72 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 20 r instance. The
738f0 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 first element of
73900 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 the linked list
73910 20 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73 74 is. * the first
73920 20 73 74 65 70 20 6f 66 20 74 68 65 20 74 72 69 step of the tri
73930 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20 2a gger-program.. *
73940 20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d 65 . * The "op" me
73950 6d 62 65 72 20 69 6e 64 69 63 61 74 65 73 20 77 mber indicates w
73960 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 hether this is a
73970 20 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53 45 "DELETE", "INSE
73980 52 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f 72 RT", "UPDATE" or
73990 0a 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74 61 . * "SELECT" sta
739a0 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61 6e tement. The mean
739b0 69 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68 65 ings of the othe
739c0 72 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65 74 r members is det
739d0 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 0a ermined by the .
739e0 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70 22 * value of "op"
739f0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a as follows:. *.
73a00 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53 * (op == TK_INS
73a10 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20 20 ERT). * orconf
73a20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65 20 -> stores the
73a30 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f ON CONFLICT algo
73a40 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63 74 rithm. * pSelect
73a50 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 -> If this is
73a60 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 an INSERT INTO
73a70 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 73 ... SELECT ... s
73a80 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 tatement, then.
73a90 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 * t
73aa0 68 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f 69 his stores a poi
73ab0 6e 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c 45 nter to the SELE
73ac0 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f 74 CT statement. Ot
73ad0 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a herwise NULL.. *
73ae0 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 target -> A
73af0 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 token holding th
73b00 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 e name of the ta
73b10 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 69 6e ble to insert in
73b20 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74 to.. * pExprList
73b30 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 -> If this is a
73b40 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e n INSERT INTO ..
73b50 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20 73 74 61 . VALUES ... sta
73b60 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 tement, then. *
73b70 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 69 thi
73b80 73 20 73 74 6f 72 65 73 20 76 61 6c 75 65 73 20 s stores values
73b90 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20 to be inserted.
73ba0 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a Otherwise NULL..
73bb0 20 2a 20 70 49 64 4c 69 73 74 20 20 20 2d 3e 20 * pIdList ->
73bc0 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e If this is an IN
73bd0 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 28 3c SERT INTO ... (<
73be0 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e 29 20 56 column-names>) V
73bf0 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a 20 20 20 ALUES ... . *
73c00 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 state
73c10 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 ment, then this
73c20 73 74 6f 72 65 73 20 74 68 65 20 63 6f 6c 75 6d stores the colum
73c30 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65 0a 20 2a n-names to be. *
73c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
73c50 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a 20 2a 0a serted into.. *.
73c60 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 44 45 4c * (op == TK_DEL
73c70 45 54 45 29 0a 20 2a 20 74 61 72 67 65 74 20 20 ETE). * target
73c80 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c -> A token hol
73c90 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f 66 ding the name of
73ca0 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 64 65 the table to de
73cb0 6c 65 74 65 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 lete from.. * pW
73cc0 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57 here -> The W
73cd0 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 HERE clause of t
73ce0 68 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d he DELETE statem
73cf0 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70 ent if one is sp
73d00 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 ecified.. *
73d10 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 Otherwi
73d20 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 se NULL.. * . *
73d30 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45 (op == TK_UPDATE
73d40 29 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d ). * target -
73d50 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e > A token holdin
73d60 67 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 g the name of th
73d70 65 20 74 61 62 6c 65 20 74 6f 20 75 70 64 61 74 e table to updat
73d80 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a 20 70 57 e rows of.. * pW
73d90 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57 here -> The W
73da0 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 HERE clause of t
73db0 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d he UPDATE statem
73dc0 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70 ent if one is sp
73dd0 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 ecified.. *
73de0 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 Otherwi
73df0 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 45 78 70 se NULL.. * pExp
73e00 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 73 74 20 rList -> A list
73e10 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 74 of the columns t
73e20 6f 20 75 70 64 61 74 65 20 61 6e 64 20 74 68 65 o update and the
73e30 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20 expressions to
73e40 75 70 64 61 74 65 0a 20 2a 20 20 20 20 20 20 20 update. *
73e50 20 20 20 20 20 20 20 74 68 65 6d 20 74 6f 2e 20 them to.
73e60 53 65 65 20 73 71 6c 69 74 65 33 55 70 64 61 74 See sqlite3Updat
73e70 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f e() documentatio
73e80 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 73 22 0a n of "pChanges".
73e90 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
73ea0 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 0a 20 2a argument.. * . *
73eb0 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72 /.struct Trigger
73ec0 53 74 65 70 20 7b 0a 20 20 69 6e 74 20 6f 70 3b Step {. int op;
73ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
73ee0 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 One of TK_DELET
73ef0 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b E, TK_UPDATE, TK
73f00 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 45 4c 45 _INSERT, TK_SELE
73f10 43 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f CT */. int orco
73f20 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 nf; /*
73f30 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 74 63 2e OE_Rollback etc.
73f40 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 */. Trigger *p
73f50 54 72 69 67 3b 20 20 20 20 20 20 2f 2a 20 54 68 Trig; /* Th
73f60 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20 74 e trigger that t
73f70 68 69 73 20 73 74 65 70 20 69 73 20 61 20 70 61 his step is a pa
73f80 72 74 20 6f 66 20 2a 2f 0a 0a 20 20 53 65 6c 65 rt of */.. Sele
73f90 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 ct *pSelect;
73fa0 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 53 45 /* Valid for SE
73fb0 4c 45 43 54 20 61 6e 64 20 73 6f 6d 65 74 69 6d LECT and sometim
73fc0 65 73 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 es .
73fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49 4e IN
73fe0 53 45 52 54 20 73 74 65 70 73 20 28 77 68 65 6e SERT steps (when
73ff0 20 70 45 78 70 72 4c 69 73 74 20 3d 3d 20 30 29 pExprList == 0)
74000 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 61 72 67 */. Token targ
74010 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 et; /* Va
74020 6c 69 64 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 lid for DELETE,
74030 55 50 44 41 54 45 2c 20 49 4e 53 45 52 54 20 73 UPDATE, INSERT s
74040 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a teps */. Expr *
74050 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 2f pWhere; /
74060 2a 20 56 61 6c 69 64 20 66 6f 72 20 44 45 4c 45 * Valid for DELE
74070 54 45 2c 20 55 50 44 41 54 45 20 73 74 65 70 73 TE, UPDATE steps
74080 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
74090 70 45 78 70 72 4c 69 73 74 3b 20 2f 2a 20 56 61 pExprList; /* Va
740a0 6c 69 64 20 66 6f 72 20 55 50 44 41 54 45 20 73 lid for UPDATE s
740b0 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 73 6f tatements and so
740c0 6d 65 74 69 6d 65 73 20 0a 20 20 20 20 20 20 20 metimes .
740d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
740e0 20 20 20 20 49 4e 53 45 52 54 20 73 74 65 70 73 INSERT steps
740f0 20 28 77 68 65 6e 20 70 53 65 6c 65 63 74 20 3d (when pSelect =
74100 3d 20 30 29 20 20 20 20 20 20 20 20 20 2a 2f 0a = 0) */.
74110 20 20 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73 IdList *pIdLis
74120 74 3b 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 t; /* Valid
74130 66 6f 72 20 49 4e 53 45 52 54 20 73 74 61 74 65 for INSERT state
74140 6d 65 6e 74 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 ments only */.
74150 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4e 65 TriggerStep *pNe
74160 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 xt; /* Next in
74170 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f the link-list */
74180 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a . TriggerStep *
74190 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20 pLast; /* Last
741a0 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d element in link-
741b0 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 6f 72 20 list. Valid for
741c0 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 1st elem only */
741d0 0a 7d 3b 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e .};../*. * An in
741e0 73 74 61 6e 63 65 20 6f 66 20 73 74 72 75 63 74 stance of struct
741f0 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 73 74 TriggerStack st
74200 6f 72 65 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ores information
74210 20 72 65 71 75 69 72 65 64 20 64 75 72 69 6e 67 required during
74220 20 63 6f 64 65 0a 20 2a 20 67 65 6e 65 72 61 74 code. * generat
74230 69 6f 6e 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 ion of a single
74240 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e trigger program.
74250 20 57 68 69 6c 65 20 74 68 65 20 74 72 69 67 67 While the trigg
74260 65 72 20 70 72 6f 67 72 61 6d 20 69 73 20 62 65 er program is be
74270 69 6e 67 0a 20 2a 20 63 6f 64 65 64 2c 20 69 74 ing. * coded, it
74280 73 20 61 73 73 6f 63 69 61 74 65 64 20 54 72 69 s associated Tri
74290 67 67 65 72 53 74 61 63 6b 20 69 6e 73 74 61 6e ggerStack instan
742a0 63 65 20 69 73 20 70 6f 69 6e 74 65 64 20 74 6f ce is pointed to
742b0 20 62 79 20 74 68 65 0a 20 2a 20 22 70 54 72 69 by the. * "pTri
742c0 67 67 65 72 53 74 61 63 6b 22 20 6d 65 6d 62 65 ggerStack" membe
742d0 72 20 6f 66 20 74 68 65 20 50 61 72 73 65 20 73 r of the Parse s
742e0 74 72 75 63 74 75 72 65 2e 0a 20 2a 0a 20 2a 20 tructure.. *. *
742f0 54 68 65 20 70 54 61 62 20 6d 65 6d 62 65 72 20 The pTab member
74300 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 74 61 points to the ta
74310 62 6c 65 20 74 68 61 74 20 74 72 69 67 67 65 72 ble that trigger
74320 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 64 65 s are being code
74330 64 20 6f 6e 2e 20 54 68 65 20 0a 20 2a 20 6e 65 d on. The . * ne
74340 77 49 64 78 20 6d 65 6d 62 65 72 20 63 6f 6e 74 wIdx member cont
74350 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f ains the index o
74360 66 20 74 68 65 20 76 64 62 65 20 63 75 72 73 6f f the vdbe curso
74370 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20 61 74 r that points at
74380 20 74 68 65 20 74 65 6d 70 0a 20 2a 20 74 61 62 the temp. * tab
74390 6c 65 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 le that stores t
743a0 68 65 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e he new.* referen
743b0 63 65 73 2e 20 49 66 20 6e 65 77 2e 2a 20 72 65 ces. If new.* re
743c0 66 65 72 65 6e 63 65 73 20 61 72 65 20 6e 6f 74 ferences are not
743d0 20 76 61 6c 69 64 0a 20 2a 20 66 6f 72 20 74 68 valid. * for th
743e0 65 20 74 72 69 67 67 65 72 20 62 65 69 6e 67 20 e trigger being
743f0 63 6f 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 coded (for examp
74400 6c 65 20 61 6e 20 4f 4e 20 44 45 4c 45 54 45 20 le an ON DELETE
74410 74 72 69 67 67 65 72 29 2c 20 74 68 65 6e 20 6e trigger), then n
74420 65 77 49 64 78 0a 20 2a 20 69 73 20 73 65 74 20 ewIdx. * is set
74430 74 6f 20 2d 31 2e 20 54 68 65 20 6f 6c 64 49 64 to -1. The oldId
74440 78 20 6d 65 6d 62 65 72 20 69 73 20 61 6e 61 6c x member is anal
74450 6f 67 6f 75 73 20 74 6f 20 6e 65 77 49 64 78 2c ogous to newIdx,
74460 20 66 6f 72 20 6f 6c 64 2e 2a 20 72 65 66 65 72 for old.* refer
74470 65 6e 63 65 73 2e 0a 20 2a 0a 20 2a 20 54 68 65 ences.. *. * The
74480 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c ON CONFLICT pol
74490 69 63 79 20 74 6f 20 62 65 20 75 73 65 64 20 66 icy to be used f
744a0 6f 72 20 74 68 65 20 74 72 69 67 67 65 72 20 70 or the trigger p
744b0 72 6f 67 72 61 6d 20 73 74 65 70 73 20 69 73 20 rogram steps is
744c0 73 74 6f 72 65 64 20 0a 20 2a 20 61 73 20 74 68 stored . * as th
744d0 65 20 6f 72 63 6f 6e 66 20 6d 65 6d 62 65 72 2e e orconf member.
744e0 20 49 66 20 74 68 69 73 20 69 73 20 4f 45 5f 44 If this is OE_D
744f0 65 66 61 75 6c 74 2c 20 74 68 65 6e 20 74 68 65 efault, then the
74500 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 ON CONFLICT cla
74510 75 73 65 20 0a 20 2a 20 73 70 65 63 69 66 69 65 use . * specifie
74520 64 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61 6c d for individual
74530 20 74 72 69 67 67 65 72 73 20 73 74 65 70 73 20 triggers steps
74540 69 73 20 75 73 65 64 2e 0a 20 2a 0a 20 2a 20 73 is used.. *. * s
74550 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 61 truct TriggerSta
74560 63 6b 20 68 61 73 20 61 20 22 70 4e 65 78 74 22 ck has a "pNext"
74570 20 6d 65 6d 62 65 72 2c 20 74 6f 20 61 6c 6c 6f member, to allo
74580 77 20 6c 69 6e 6b 65 64 20 6c 69 73 74 73 20 74 w linked lists t
74590 6f 20 62 65 0a 20 2a 20 63 6f 6e 73 74 72 75 63 o be. * construc
745a0 74 65 64 2e 20 57 68 65 6e 20 63 6f 64 69 6e 67 ted. When coding
745b0 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 73 nested triggers
745c0 20 28 74 72 69 67 67 65 72 73 20 66 69 72 65 64 (triggers fired
745d0 20 62 79 20 6f 74 68 65 72 20 74 72 69 67 67 65 by other trigge
745e0 72 73 29 0a 20 2a 20 65 61 63 68 20 6e 65 73 74 rs). * each nest
745f0 65 64 20 74 72 69 67 67 65 72 20 73 74 6f 72 65 ed trigger store
74600 73 20 69 74 73 20 70 61 72 65 6e 74 20 74 72 69 s its parent tri
74610 67 67 65 72 27 73 20 54 72 69 67 67 65 72 53 74 gger's TriggerSt
74620 61 63 6b 20 61 73 20 74 68 65 20 22 70 4e 65 78 ack as the "pNex
74630 74 22 20 0a 20 2a 20 70 6f 69 6e 74 65 72 2e 20 t" . * pointer.
74640 4f 6e 63 65 20 74 68 65 20 6e 65 73 74 65 64 20 Once the nested
74650 74 72 69 67 67 65 72 20 68 61 73 20 62 65 65 6e trigger has been
74660 20 63 6f 64 65 64 2c 20 74 68 65 20 70 4e 65 78 coded, the pNex
74670 74 20 76 61 6c 75 65 20 69 73 20 72 65 73 74 6f t value is resto
74680 72 65 64 0a 20 2a 20 74 6f 20 74 68 65 20 70 54 red. * to the pT
74690 72 69 67 67 65 72 53 74 61 63 6b 20 6d 65 6d 62 riggerStack memb
746a0 65 72 20 6f 66 20 74 68 65 20 50 61 72 73 65 20 er of the Parse
746b0 73 74 75 63 74 75 72 65 20 61 6e 64 20 63 6f 64 stucture and cod
746c0 69 6e 67 20 6f 66 20 74 68 65 20 70 61 72 65 6e ing of the paren
746d0 74 0a 20 2a 20 74 72 69 67 67 65 72 20 63 6f 6e t. * trigger con
746e0 74 69 6e 75 65 73 2e 0a 20 2a 0a 20 2a 20 42 65 tinues.. *. * Be
746f0 66 6f 72 65 20 61 20 6e 65 73 74 65 64 20 74 72 fore a nested tr
74700 69 67 67 65 72 20 69 73 20 63 6f 64 65 64 2c 20 igger is coded,
74710 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 the linked list
74720 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 pointed to by th
74730 65 20 0a 20 2a 20 70 54 72 69 67 67 65 72 53 74 e . * pTriggerSt
74740 61 63 6b 20 69 73 20 73 63 61 6e 6e 65 64 20 74 ack is scanned t
74750 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 o ensure that th
74760 65 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f 74 e trigger is not
74770 20 61 62 6f 75 74 20 74 6f 20 62 65 20 63 6f 64 about to be cod
74780 65 64 0a 20 2a 20 72 65 63 75 72 73 69 76 65 6c ed. * recursivel
74790 79 2e 20 49 66 20 74 68 69 73 20 63 6f 6e 64 69 y. If this condi
747a0 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 tion is detected
747b0 2c 20 74 68 65 20 6e 65 73 74 65 64 20 74 72 69 , the nested tri
747c0 67 67 65 72 20 69 73 20 6e 6f 74 20 63 6f 64 65 gger is not code
747d0 64 2e 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 d.. */.struct Tr
747e0 69 67 67 65 72 53 74 61 63 6b 20 7b 0a 20 20 54 iggerStack {. T
747f0 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 able *pTab;
74800 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 68 61 /* Table tha
74810 74 20 74 72 69 67 67 65 72 73 20 61 72 65 20 63 t triggers are c
74820 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 urrently being c
74830 6f 64 65 64 20 6f 6e 20 2a 2f 0a 20 20 69 6e 74 oded on */. int
74840 20 6e 65 77 49 64 78 3b 20 20 20 20 20 20 20 20 newIdx;
74850 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 76 64 /* Index of vd
74860 62 65 20 63 75 72 73 6f 72 20 74 6f 20 22 6e 65 be cursor to "ne
74870 77 22 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f w" temp table */
74880 0a 20 20 69 6e 74 20 6f 6c 64 49 64 78 3b 20 20 . int oldIdx;
74890 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
748a0 20 6f 66 20 76 64 62 65 20 63 75 72 73 6f 72 20 of vdbe cursor
748b0 74 6f 20 22 6f 6c 64 22 20 74 65 6d 70 20 74 61 to "old" temp ta
748c0 62 6c 65 20 2a 2f 0a 20 20 75 33 32 20 6e 65 77 ble */. u32 new
748d0 43 6f 6c 4d 61 73 6b 3b 0a 20 20 75 33 32 20 6f ColMask;. u32 o
748e0 6c 64 43 6f 6c 4d 61 73 6b 3b 0a 20 20 69 6e 74 ldColMask;. int
748f0 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 orconf;
74900 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 72 63 /* Current orc
74910 6f 6e 66 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 onf policy */.
74920 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70 3b 20 int ignoreJump;
74930 20 20 20 20 20 2f 2a 20 77 68 65 72 65 20 74 6f /* where to
74940 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 61 20 52 jump to for a R
74950 41 49 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f 0a AISE(IGNORE) */.
74960 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 Trigger *pTrig
74970 67 65 72 3b 20 20 20 2f 2a 20 54 68 65 20 74 72 ger; /* The tr
74980 69 67 67 65 72 20 63 75 72 72 65 6e 74 6c 79 20 igger currently
74990 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 being coded */.
749a0 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70 TriggerStack *p
749b0 4e 65 78 74 3b 20 2f 2a 20 4e 65 78 74 20 74 72 Next; /* Next tr
749c0 69 67 67 65 72 20 64 6f 77 6e 20 6f 6e 20 74 68 igger down on th
749d0 65 20 74 72 69 67 67 65 72 20 73 74 61 63 6b 20 e trigger stack
749e0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 */.};../*.** The
749f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
74a00 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e ture contains in
74a10 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 formation used b
74a20 79 20 74 68 65 20 73 71 6c 69 74 65 46 69 78 2e y the sqliteFix.
74a30 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 ...** routines a
74a40 73 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65 20 s they walk the
74a50 70 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d 61 parse tree to ma
74a60 6b 65 20 64 61 74 61 62 61 73 65 20 72 65 66 65 ke database refe
74a70 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69 63 rences.** explic
74a80 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65 66 it. .*/.typedef
74a90 20 73 74 72 75 63 74 20 44 62 46 69 78 65 72 20 struct DbFixer
74aa0 44 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74 20 DbFixer;.struct
74ab0 44 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72 73 DbFixer {. Pars
74ac0 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 e *pParse;
74ad0 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 /* The parsing c
74ae0 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20 6d ontext. Error m
74af0 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 essages written
74b00 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 here */. const
74b10 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f 2a char *zDb; /*
74b20 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 6f Make sure all o
74b30 62 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74 61 bjects are conta
74b40 69 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61 74 ined in this dat
74b50 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 abase */. const
74b60 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 2f char *zType; /
74b70 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 63 6f * Type of the co
74b80 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20 66 ntainer - used f
74b90 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 or error message
74ba0 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b s */. const Tok
74bb0 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e 61 en *pName; /* Na
74bc0 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 me of the contai
74bd0 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 ner - used for e
74be0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f rror messages */
74bf0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 .};../*.** An ob
74c00 6a 65 63 74 65 64 20 75 73 65 64 20 74 6f 20 61 jected used to a
74c10 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74 65 ccumulate the te
74c20 78 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20 77 xt of a string w
74c30 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e 6f here we.** do no
74c40 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b 6e t necessarily kn
74c50 6f 77 20 68 6f 77 20 62 69 67 20 74 68 65 20 73 ow how big the s
74c60 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69 6e tring will be in
74c70 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 72 the end..*/.str
74c80 75 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a 20 uct StrAccum {.
74c90 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
74ca0 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 /* Optiona
74cb0 6c 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 6c l database for l
74cc0 6f 6f 6b 61 73 69 64 65 2e 20 20 43 61 6e 20 62 ookaside. Can b
74cd0 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 e NULL */. char
74ce0 20 2a 7a 42 61 73 65 3b 20 20 20 20 20 20 20 20 *zBase;
74cf0 20 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f 63 /* A base alloc
74d00 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f 6d ation. Not from
74d10 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63 68 malloc. */. ch
74d20 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 20 ar *zText;
74d30 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 /* The string
74d40 20 63 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66 61 collected so fa
74d50 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68 61 r */. int nCha
74d60 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c r; /* L
74d70 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 ength of the str
74d80 69 6e 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 ing so far */.
74d90 69 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 int nAlloc;
74da0 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f /* Amount o
74db0 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 f space allocate
74dc0 64 20 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20 20 d in zText */.
74dd0 69 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20 20 int mxAlloc;
74de0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
74df0 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6c allowed string l
74e00 65 6e 67 74 68 20 2a 2f 0a 20 20 75 38 20 20 20 ength */. u8
74e10 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 mallocFailed;
74e20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20 /* Becomes true
74e30 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61 6c if any memory al
74e40 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 2a location fails *
74e50 2f 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c 6c /. u8 useMall
74e60 6f 63 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 oc; /* True
74e70 20 69 66 20 7a 54 65 78 74 20 69 73 20 65 6e 6c if zText is enl
74e80 61 72 67 65 61 62 6c 65 20 75 73 69 6e 67 20 72 argeable using r
74e90 65 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 20 ealloc */. u8
74ea0 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 20 tooBig;
74eb0 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 /* Becomes true
74ec0 20 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65 20 if string size
74ed0 65 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20 2a exceeds limits *
74ee0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 6f /.};../*.** A po
74ef0 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 inter to this st
74f00 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 ructure is used
74f10 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 69 to communicate i
74f20 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72 nformation.** fr
74f30 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20 61 om sqlite3Init a
74f40 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d nd OP_ParseSchem
74f50 61 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 a into the sqlit
74f60 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e 0a e3InitCallback..
74f70 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
74f80 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 t {. sqlite3 *d
74f90 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 b; /* The
74fa0 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 database being
74fb0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
74fc0 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
74fd0 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 61 /* 0 for ma
74fe0 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31 20 in database. 1
74ff0 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66 6f for TEMP, 2.. fo
75000 72 20 41 54 54 41 43 48 65 64 20 2a 2f 0a 20 20 r ATTACHed */.
75010 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 3b char **pzErrMsg;
75020 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 /* Error mes
75030 73 61 67 65 20 73 74 6f 72 65 64 20 68 65 72 65 sage stored here
75040 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
75050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
75060 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64 20 ult code stored
75070 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 61 here */.} InitDa
75080 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 ta;../*.** Struc
75090 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ture containing
750a0 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 global configura
750b0 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 68 tion data for th
750c0 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 e SQLite library
750d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 ..**.** This str
750e0 75 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e 74 ucture also cont
750f0 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 20 ains some state
75100 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
75110 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f struct Sqlite3Co
75120 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d 65 nfig {. int bMe
75130 6d 73 74 61 74 3b 20 20 20 20 20 20 20 20 20 20 mstat;
75140 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
75150 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 6d ue to enable mem
75160 6f 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 20 ory status */.
75170 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b 20 int bCoreMutex;
75180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75190 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 /* True to ena
751a0 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69 6e ble core mutexin
751b0 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c 6c g */. int bFull
751c0 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 Mutex;
751d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
751e0 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c 20 to enable full
751f0 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 6e mutexing */. in
75200 74 20 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20 20 t mxStrlen;
75210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75220 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 74 72 69 6e /* Maximum strin
75230 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 6e g length */. in
75240 74 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 t szLookaside;
75250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75260 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 /* Default looka
75270 73 69 64 65 20 62 75 66 66 65 72 20 73 69 7a 65 side buffer size
75280 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61 */. int nLooka
75290 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 side;
752a0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 /* Defau
752b0 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 lt lookaside buf
752c0 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 73 fer count */. s
752d0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
752e0 64 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20 20 ds m;
752f0 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 65 /* Low-level me
75300 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
75310 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 interface */. s
75320 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
75330 68 6f 64 73 20 6d 75 74 65 78 3b 20 20 20 20 20 hods mutex;
75340 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 75 /* Low-level mu
75350 74 65 78 20 69 6e 74 65 72 66 61 63 65 20 2a 2f tex interface */
75360 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 . sqlite3_pcach
75370 65 5f 6d 65 74 68 6f 64 73 20 70 63 61 63 68 65 e_methods pcache
75380 3b 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 ; /* Low-leve
75390 6c 20 70 61 67 65 2d 63 61 63 68 65 20 69 6e 74 l page-cache int
753a0 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 erface */. void
753b0 20 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20 20 *pHeap;
753c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
753d0 20 48 65 61 70 20 73 74 6f 72 61 67 65 20 73 70 Heap storage sp
753e0 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 ace */. int nHe
753f0 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ap;
75400 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
75410 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a 2f ze of pHeap[] */
75420 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d 78 . int mnReq, mx
75430 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 Req;
75440 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64 20 /* Min and
75450 6d 61 78 20 68 65 61 70 20 72 65 71 75 65 73 74 max heap request
75460 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f 69 s sizes */. voi
75470 64 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20 20 d *pScratch;
75480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
75490 2a 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 * Scratch memory
754a0 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 */. int szScra
754b0 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 tch;
754c0 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
754d0 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68 20 of each scratch
754e0 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 buffer */. int
754f0 6e 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 nScratch;
75500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
75510 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74 Number of scrat
75520 63 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 20 ch buffers */.
75530 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20 20 void *pPage;
75540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75550 20 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65 20 /* Page cache
75560 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 memory */. int
75570 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 szPage;
75580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
75590 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61 Size of each pa
755a0 67 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f ge in pPage[] */
755b0 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 . int nPage;
755c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
755d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
755e0 66 20 70 61 67 65 73 20 69 6e 20 70 50 61 67 65 f pages in pPage
755f0 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 [] */. int mxPa
75600 72 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20 20 rserStack;
75610 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 78 /* max
75620 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 imum depth of th
75630 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 2a e parser stack *
75640 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43 61 /. int sharedCa
75650 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 cheEnabled;
75660 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66 /* true if
75670 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f shared-cache mo
75680 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 de enabled */.
75690 2f 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69 67 /* The above mig
756a0 68 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 ht be initialize
756b0 64 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 d to non-zero.
756c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 The following ne
756d0 65 64 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a ed to always. *
756e0 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20 7a * initially be z
756f0 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a 2f ero, however. */
75700 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 20 . int isInit;
75710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75720 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 /* True aft
75730 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f er initializatio
75740 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 2a n has finished *
75750 2f 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 65 /. int inProgre
75760 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ss;
75770 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 /* True wh
75780 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ile initializati
75790 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 2a on in progress *
757a0 2f 0a 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 /. int isMalloc
757b0 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 Init;
757c0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 /* True af
757d0 74 65 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e ter malloc is in
757e0 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 73 itialized */. s
757f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 49 qlite3_mutex *pI
75800 6e 69 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 nitMutex;
75810 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 62 /* Mutex used b
75820 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 y sqlite3_initia
75830 6c 69 7a 65 28 29 20 2a 2f 0a 20 20 69 6e 74 20 lize() */. int
75840 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 3b 20 20 nRefInitMutex;
75850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
75860 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 Number of users
75870 20 6f 66 20 70 49 6e 69 74 4d 75 74 65 78 20 2a of pInitMutex *
75880 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 /.};../*.** Cont
75890 65 78 74 20 70 6f 69 6e 74 65 72 20 70 61 73 73 ext pointer pass
758a0 65 64 20 64 6f 77 6e 20 74 68 72 6f 75 67 68 20 ed down through
758b0 74 68 65 20 74 72 65 65 2d 77 61 6c 6b 2e 0a 2a the tree-walk..*
758c0 2f 0a 73 74 72 75 63 74 20 57 61 6c 6b 65 72 20 /.struct Walker
758d0 7b 0a 20 20 69 6e 74 20 28 2a 78 45 78 70 72 43 {. int (*xExprC
758e0 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a allback)(Walker*
758f0 2c 20 45 78 70 72 2a 29 3b 20 20 20 20 20 2f 2a , Expr*); /*
75900 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65 78 Callback for ex
75910 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 pressions */. i
75920 6e 74 20 28 2a 78 53 65 6c 65 63 74 43 61 6c 6c nt (*xSelectCall
75930 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a 2c 53 65 back)(Walker*,Se
75940 6c 65 63 74 2a 29 3b 20 20 2f 2a 20 43 61 6c 6c lect*); /* Call
75950 62 61 63 6b 20 66 6f 72 20 53 45 4c 45 43 54 73 back for SELECTs
75960 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 */. Parse *pPa
75970 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 rse;
75980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75990 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 /* Parser contex
759a0 74 2e 20 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b t. */. union {
759b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
759c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
759d0 20 20 20 2f 2a 20 45 78 74 72 61 20 64 61 74 61 /* Extra data
759e0 20 66 6f 72 20 63 61 6c 6c 62 61 63 6b 20 2a 2f for callback */
759f0 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 . NameContext
75a00 20 2a 70 4e 43 3b 20 20 20 20 20 20 20 20 20 20 *pNC;
75a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75a20 2f 2a 20 4e 61 6d 69 6e 67 20 63 6f 6e 74 65 78 /* Naming contex
75a30 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 20 t */. int i;
75a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75a60 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 /* Integer v
75a70 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d alue */. } u;.}
75a80 3b 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 ;../* Forward de
75a90 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 53 51 clarations */.SQ
75aa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
75ab0 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
75ac0 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a 29 (Walker*, Expr*)
75ad0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75ae0 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b int sqlite3Walk
75af0 45 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 2a ExprList(Walker*
75b00 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 , ExprList*);.SQ
75b10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
75b20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
75b30 63 74 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 ct(Walker*, Sele
75b40 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ct*);.SQLITE_PRI
75b50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
75b60 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 57 WalkSelectExpr(W
75b70 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 alker*, Select*)
75b80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75b90 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b int sqlite3Walk
75ba0 53 65 6c 65 63 74 46 72 6f 6d 28 57 61 6c 6b 65 SelectFrom(Walke
75bb0 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 0a 2f r*, Select*);../
75bc0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 *.** Return code
75bd0 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73 65 2d from the parse-
75be0 74 72 65 65 20 77 61 6c 6b 69 6e 67 20 70 72 69 tree walking pri
75bf0 6d 69 74 69 76 65 73 20 61 6e 64 20 74 68 65 69 mitives and thei
75c00 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 2e 0a r.** callbacks..
75c10 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 43 */.#define WRC_C
75c20 6f 6e 74 69 6e 75 65 20 20 20 20 30 20 20 20 2f ontinue 0 /
75c30 2a 20 43 6f 6e 74 69 6e 75 65 20 64 6f 77 6e 20 * Continue down
75c40 69 6e 74 6f 20 63 68 69 6c 64 72 65 6e 20 2a 2f into children */
75c50 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 50 72 75 .#define WRC_Pru
75c60 6e 65 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 ne 1 /*
75c70 4f 6d 69 74 20 63 68 69 6c 64 72 65 6e 20 62 75 Omit children bu
75c80 74 20 63 6f 6e 74 69 6e 75 65 20 77 61 6c 6b 69 t continue walki
75c90 6e 67 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0a 23 ng siblings */.#
75ca0 64 65 66 69 6e 65 20 57 52 43 5f 41 62 6f 72 74 define WRC_Abort
75cb0 20 20 20 20 20 20 20 32 20 20 20 2f 2a 20 41 62 2 /* Ab
75cc0 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77 andon the tree w
75cd0 61 6c 6b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 73 alk */../*.** As
75ce0 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f 69 6e 74 suming zIn point
75cf0 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 s to the first b
75d00 79 74 65 20 6f 66 20 61 20 55 54 46 2d 38 20 63 yte of a UTF-8 c
75d10 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20 61 64 76 haracter,.** adv
75d20 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70 6f 69 6e ance zIn to poin
75d30 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 t to the first b
75d40 79 74 65 20 6f 66 20 74 68 65 20 6e 65 78 74 20 yte of the next
75d50 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e UTF-8 character.
75d60 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
75d70 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 49 6e TE_SKIP_UTF8(zIn
75d80 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ) {
75d90 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 \. i
75da0 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29 3e 3d 30 f( (*(zIn++))>=0
75db0 78 63 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 xc0 ){
75dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75dd0 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c 65 28 \. while(
75de0 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d (*zIn & 0xc0)==
75df0 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b 3b 20 7d 0x80 ){ zIn++; }
75e00 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
75e10 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
75e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75e40 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a \.}../*.**
75e50 20 54 68 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 The SQLITE_CORR
75e60 55 50 54 5f 42 4b 50 54 20 6d 61 63 72 6f 20 63 UPT_BKPT macro c
75e70 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 63 an be either a c
75e80 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20 70 72 6f onstant (for pro
75e90 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75 69 6c 64 duction.** build
75ea0 73 29 20 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e s) or a function
75eb0 20 63 61 6c 6c 20 28 66 6f 72 20 64 65 62 75 67 call (for debug
75ec0 67 69 6e 67 29 2e 20 20 49 66 20 69 74 20 69 73 ging). If it is
75ed0 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c a function call
75ee0 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77 73 20 74 ,.** it allows t
75ef0 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 73 he operator to s
75f00 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e 74 20 et a breakpoint
75f10 61 74 20 74 68 65 20 73 70 6f 74 20 77 68 65 72 at the spot wher
75f20 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f e database.** co
75f30 72 72 75 70 74 69 6f 6e 20 69 73 20 66 69 72 73 rruption is firs
75f40 74 20 64 65 74 65 63 74 65 64 2e 0a 2a 2f 0a 23 t detected..*/.#
75f50 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
75f60 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
75f70 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 43 E int sqlite3C
75f80 6f 72 72 75 70 74 28 76 6f 69 64 29 3b 0a 23 20 orrupt(void);.#
75f90 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
75fa0 52 52 55 50 54 5f 42 4b 50 54 20 73 71 6c 69 74 RRUPT_BKPT sqlit
75fb0 65 33 43 6f 72 72 75 70 74 28 29 0a 23 65 6c 73 e3Corrupt().#els
75fc0 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 e.# define SQLIT
75fd0 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 53 E_CORRUPT_BKPT S
75fe0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 0a 23 65 QLITE_CORRUPT.#e
75ff0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 ndif../*.** Inte
76000 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 70 72 rnal function pr
76010 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49 ototypes.*/.SQLI
76020 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
76030 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63 6f qlite3StrICmp(co
76040 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73 nst char *, cons
76050 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 t char *);.SQLIT
76060 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
76070 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 63 6f lite3StrNICmp(co
76080 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73 nst char *, cons
76090 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a t char *, int);.
760a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
760b0 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 nt sqlite3IsNumb
760c0 65 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 er(const char*,
760d0 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 int*, u8);.SQLIT
760e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
760f0 6c 69 74 65 33 53 74 72 6c 65 6e 28 73 71 6c 69 lite3Strlen(sqli
76100 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 te3*, const char
76110 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
76120 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 TE int sqlite3St
76130 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 63 68 61 rlen30(const cha
76140 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 r*);..SQLITE_PRI
76150 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
76160 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 MallocInit(void)
76170 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76180 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c void sqlite3Mal
76190 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 locEnd(void);.SQ
761a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
761b0 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 d *sqlite3Malloc
761c0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
761d0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
761e0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e te3MallocZero(in
761f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
76200 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
76210 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c DbMallocZero(sql
76220 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ite3*, int);.SQL
76230 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
76240 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *sqlite3DbMallo
76250 63 52 61 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 cRaw(sqlite3*, i
76260 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
76270 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
76280 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65 3DbStrDup(sqlite
76290 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 3*,const char*);
762a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
762b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 char *sqlite3DbS
762c0 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a 2c trNDup(sqlite3*,
762d0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
762e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
762f0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 E void *sqlite3R
76300 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e ealloc(void*, in
76310 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
76320 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
76330 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 DbReallocOrFree(
76340 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 sqlite3 *, void
76350 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
76360 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
76370 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 lite3DbRealloc(s
76380 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a qlite3 *, void *
76390 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
763a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
763b0 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 65 te3DbFree(sqlite
763c0 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 3*, void*);.SQLI
763d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
763e0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 qlite3MallocSize
763f0 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f (void*);.SQLITE_
76400 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
76410 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
76420 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 sqlite3*, void*)
76430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76440 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63 void *sqlite3Sc
76450 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 ratchMalloc(int)
76460 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76470 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 72 void sqlite3Scr
76480 61 74 63 68 46 72 65 65 28 76 6f 69 64 2a 29 3b atchFree(void*);
76490 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
764a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
764b0 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 eMalloc(int);.SQ
764c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
764d0 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 d sqlite3PageFre
764e0 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 e(void*);.SQLITE
764f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
76500 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 lite3MemSetDefau
76510 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 lt(void);.SQLITE
76520 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
76530 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f lite3BenignMallo
76540 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 cHooks(void (*)(
76550 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 void), void (*)(
76560 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 void));.SQLITE_P
76570 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
76580 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f e3MemoryAlarm(vo
76590 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 id (*)(void*, sq
765a0 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 lite3_int64, int
765b0 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 ), void*, sqlite
765c0 33 5f 69 6e 74 36 34 29 3b 0a 0a 23 69 66 64 65 3_int64);..#ifde
765d0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
765e0 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 MEMSYS3.SQLITE_P
765f0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c RIVATE const sql
76600 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
76610 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d *sqlite3MemGetM
76620 65 6d 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 emsys3(void);.#e
76630 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
76640 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
76650 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 5.SQLITE_PRIVATE
76660 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
76670 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 em_methods *sqli
76680 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 te3MemGetMemsys5
76690 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a (void);.#endif..
766a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
766b0 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 MUTEX_OMIT.SQLIT
766c0 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
766d0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
766e0 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c s *sqlite3Defaul
766f0 74 4d 75 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 tMutex(void);.SQ
76700 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 LITE_PRIVATE s
76710 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 qlite3_mutex *sq
76720 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
76730 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
76740 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
76750 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 e3MutexInit(void
76760 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
76770 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d E int sqlite3M
76780 75 74 65 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 utexEnd(void);.#
76790 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
767a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
767b0 33 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74 3StatusValue(int
767c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
767d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
767e0 61 74 75 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 atusAdd(int, int
767f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
76800 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
76810 61 74 75 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 atusSet(int, int
76820 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
76830 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 TE int sqlite3Is
76840 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 NaN(double);..SQ
76850 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
76860 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 d sqlite3VXPrint
76870 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 f(StrAccum*, int
76880 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 , const char*, v
76890 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f a_list);.SQLITE_
768a0 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
768b0 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c lite3MPrintf(sql
768c0 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 ite3*,const char
768d0 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f *, ...);.SQLITE_
768e0 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
768f0 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71 lite3VMPrintf(sq
76900 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 lite3*,const cha
76910 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 r*, va_list);.SQ
76920 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
76930 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e r *sqlite3MAppen
76940 64 66 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 df(sqlite3*,char
76950 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e *,const char*,..
76960 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .);.#if defined(
76970 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 SQLITE_TEST) ||
76980 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
76990 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 EBUG).SQLITE_PRI
769a0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
769b0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 te3DebugPrintf(c
769c0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
769d0 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 ;.#endif.#if def
769e0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
769f0 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
76a00 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 void *sqlite3
76a10 54 65 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f TestTextToPtr(co
76a20 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 nst char*);.#end
76a30 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
76a40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 E void sqlite3Se
76a50 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c tString(char **,
76a60 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 sqlite3*, const
76a70 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 char*, ...);.SQ
76a80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
76a90 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 d sqlite3ErrorMs
76aa0 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 g(Parse*, const
76ab0 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c char*, ...);.SQL
76ac0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
76ad0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 sqlite3ErrorCle
76ae0 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 ar(Parse*);.SQLI
76af0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
76b00 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 sqlite3Dequote(c
76b10 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
76b20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
76b30 65 33 44 65 71 75 6f 74 65 45 78 70 72 28 73 71 e3DequoteExpr(sq
76b40 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a lite3*, Expr*);.
76b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
76b60 6e 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 nt sqlite3Keywor
76b70 64 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 dCode(const unsi
76b80 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 29 gned char*, int)
76b90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76ba0 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50 int sqlite3RunP
76bb0 61 72 73 65 72 28 50 61 72 73 65 2a 2c 20 63 6f arser(Parse*, co
76bc0 6e 73 74 20 63 68 61 72 2a 2c 20 63 68 61 72 20 nst char*, char
76bd0 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
76be0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
76bf0 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72 FinishCoding(Par
76c00 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 se*);.SQLITE_PRI
76c10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
76c20 47 65 74 54 65 6d 70 52 65 67 28 50 61 72 73 65 GetTempReg(Parse
76c30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
76c40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
76c50 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 eleaseTempReg(Pa
76c60 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 rse*,int);.SQLIT
76c70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
76c80 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 lite3GetTempRang
76c90 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 e(Parse*,int);.S
76ca0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
76cb0 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 id sqlite3Releas
76cc0 65 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 eTempRange(Parse
76cd0 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 *,int,int);.SQLI
76ce0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
76cf0 2a 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c *sqlite3Expr(sql
76d00 69 74 65 33 2a 2c 20 69 6e 74 2c 20 45 78 70 72 ite3*, int, Expr
76d10 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 *, Expr*, const
76d20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
76d30 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
76d40 6c 69 74 65 33 50 45 78 70 72 28 50 61 72 73 65 lite3PExpr(Parse
76d50 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 *, int, Expr*, E
76d60 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 xpr*, const Toke
76d70 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
76d80 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
76d90 33 52 65 67 69 73 74 65 72 45 78 70 72 28 50 61 3RegisterExpr(Pa
76da0 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 rse*,Token*);.SQ
76db0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
76dc0 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e r *sqlite3ExprAn
76dd0 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a d(sqlite3*,Expr*
76de0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
76df0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
76e00 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 45 78 lite3ExprSpan(Ex
76e10 70 72 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e pr*,Token*,Token
76e20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
76e30 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
76e40 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 ExprFunction(Par
76e50 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 54 se*,ExprList*, T
76e60 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
76e70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
76e80 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 72 te3ExprAssignVar
76e90 4e 75 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 45 Number(Parse*, E
76ea0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
76eb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
76ec0 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c 69 e3ExprClear(sqli
76ed0 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 te3*, Expr*);.SQ
76ee0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
76ef0 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c d sqlite3ExprDel
76f00 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 ete(sqlite3*, Ex
76f10 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
76f20 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 VATE ExprList *s
76f30 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 qlite3ExprListAp
76f40 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 72 pend(Parse*,Expr
76f50 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 54 6f 6b 65 List*,Expr*,Toke
76f60 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
76f70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
76f80 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 ExprListDelete(s
76f90 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 qlite3*, ExprLis
76fa0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
76fb0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
76fc0 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 nit(sqlite3*, ch
76fd0 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ar**);.SQLITE_PR
76fe0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
76ff0 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 3InitCallback(vo
77000 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a id*, int, char**
77010 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 , char**);.SQLIT
77020 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
77030 71 6c 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 qlite3Pragma(Par
77040 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
77050 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 *,Token*,int);.S
77060 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
77070 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 id sqlite3ResetI
77080 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 nternalSchema(sq
77090 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 lite3*, int);.SQ
770a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
770b0 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 d sqlite3BeginPa
770c0 72 73 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b rse(Parse*,int);
770d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
770e0 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d void sqlite3Comm
770f0 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 itInternalChange
77100 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c s(sqlite3*);.SQL
77110 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c ITE_PRIVATE Tabl
77120 65 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 e *sqlite3Result
77130 53 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 SetOfSelect(Pars
77140 65 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c e*,Select*);.SQL
77150 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
77160 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 sqlite3OpenMast
77170 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c erTable(Parse *,
77180 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
77190 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
771a0 65 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 e3StartTable(Par
771b0 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
771c0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e *,int,int,int,in
771d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
771e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
771f0 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c ddColumn(Parse*,
77200 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
77210 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
77220 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 ite3AddNotNull(P
77230 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c arse*, int);.SQL
77240 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
77250 20 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 sqlite3AddPrima
77260 72 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 ryKey(Parse*, Ex
77270 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e prList*, int, in
77280 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
77290 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
772a0 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 ite3AddCheckCons
772b0 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 traint(Parse*, E
772c0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
772d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
772e0 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 e3AddColumnType(
772f0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a Parse*,Token*);.
77300 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
77310 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 oid sqlite3AddDe
77320 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 faultValue(Parse
77330 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 *,Expr*);.SQLITE
77340 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
77350 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 lite3AddCollateT
77360 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 ype(Parse*, Toke
77370 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
77380 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
77390 45 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c EndTable(Parse*,
773a0 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 Token*,Token*,Se
773b0 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f lect*);..SQLITE_
773c0 50 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a PRIVATE Bitvec *
773d0 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 sqlite3BitvecCre
773e0 61 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 ate(u32);.SQLITE
773f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
77400 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 ite3BitvecTest(B
77410 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 itvec*, u32);.SQ
77420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
77430 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 sqlite3BitvecSe
77440 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b t(Bitvec*, u32);
77450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
77460 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 void sqlite3Bitv
77470 65 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c ecClear(Bitvec*,
77480 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 u32);.SQLITE_PR
77490 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
774a0 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
774b0 42 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 Bitvec*);.SQLITE
774c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
774d0 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 ite3BitvecBuilti
774e0 6e 54 65 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b nTest(int,int*);
774f0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
77500 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 RowSet *sqlite3
77510 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 RowSetInit(sqlit
77520 65 33 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 e3*, void*, unsi
77530 67 6e 65 64 20 69 6e 74 29 3b 0a 53 51 4c 49 54 gned int);.SQLIT
77540 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
77550 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 qlite3RowSetClea
77560 72 28 52 6f 77 53 65 74 2a 29 3b 0a 53 51 4c 49 r(RowSet*);.SQLI
77570 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
77580 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 sqlite3RowSetIns
77590 65 72 74 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 ert(RowSet*, i64
775a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
775b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 E int sqlite3Row
775c0 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74 2a 2c SetNext(RowSet*,
775d0 20 69 36 34 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f i64*);..SQLITE_
775e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
775f0 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28 50 ite3CreateView(P
77600 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b arse*,Token*,Tok
77610 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 en*,Token*,Selec
77620 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 69 t*,int,int);..#i
77630 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
77640 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 E_OMIT_VIEW) ||
77650 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
77660 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
77670 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 E).SQLITE_PRIVAT
77680 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 E int sqlite3V
77690 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 iewGetColumnName
776a0 73 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 s(Parse*,Table*)
776b0 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
776c0 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 sqlite3ViewGetC
776d0 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 20 olumnNames(A,B)
776e0 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 0.#endif..SQLITE
776f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
77700 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50 lite3DropTable(P
77710 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c arse*, SrcList*,
77720 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
77730 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
77740 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 sqlite3DeleteTab
77750 6c 65 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 le(Table*);.SQLI
77760 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
77770 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 50 61 sqlite3Insert(Pa
77780 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 rse*, SrcList*,
77790 45 78 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 ExprList*, Selec
777a0 74 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 t*, IdList*, int
777b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
777c0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 E void *sqlite3A
777d0 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c rrayAllocate(sql
777e0 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c ite3*,void*,int,
777f0 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e int,int*,int*,in
77800 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
77810 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 ATE IdList *sqli
77820 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 te3IdListAppend(
77830 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 sqlite3*, IdList
77840 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
77850 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
77860 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 qlite3IdListInde
77870 78 28 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 x(IdList*,const
77880 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
77890 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a RIVATE SrcList *
778a0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e sqlite3SrcListEn
778b0 6c 61 72 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 large(sqlite3*,
778c0 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 SrcList*, int, i
778d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
778e0 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
778f0 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e ite3SrcListAppen
77900 64 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c d(sqlite3*, SrcL
77910 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f ist*, Token*, To
77920 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
77930 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 IVATE SrcList *s
77940 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 qlite3SrcListApp
77950 65 6e 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 endFromTerm(Pars
77960 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f e*, SrcList*, To
77970 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 ken*, Token*,.
77980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
779a0 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 Token*, Sele
779b0 63 74 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 ct*, Expr*, IdLi
779c0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
779d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
779e0 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 3SrcListIndexedB
779f0 79 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 y(Parse *, SrcLi
77a00 73 74 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a st *, Token *);.
77a10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
77a20 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 nt sqlite3Indexe
77a30 64 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 dByLookup(Parse
77a40 2a 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 *, struct SrcLis
77a50 74 5f 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 t_item *);.SQLIT
77a60 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
77a70 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 qlite3SrcListShi
77a80 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 ftJoinType(SrcLi
77a90 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
77aa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
77ab0 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 3SrcListAssignCu
77ac0 72 73 6f 72 73 28 50 61 72 73 65 2a 2c 20 53 72 rsors(Parse*, Sr
77ad0 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f cList*);.SQLITE_
77ae0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
77af0 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 ite3IdListDelete
77b00 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 (sqlite3*, IdLis
77b10 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
77b20 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
77b30 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71 SrcListDelete(sq
77b40 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a lite3*, SrcList*
77b50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
77b60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 E void sqlite3Cr
77b70 65 61 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a eateIndex(Parse*
77b80 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 ,Token*,Token*,S
77b90 72 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 rcList*,ExprList
77ba0 2a 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 *,int,Token*,.
77bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77bc0 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e Token*, in
77bd0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
77be0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
77bf0 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 ite3DropIndex(Pa
77c00 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 rse*, SrcList*,
77c10 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
77c20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
77c30 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53 Select(Parse*, S
77c40 65 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 elect*, SelectDe
77c50 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
77c60 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c VATE Select *sql
77c70 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 50 61 ite3SelectNew(Pa
77c80 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 rse*,ExprList*,S
77c90 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 rcList*,Expr*,Ex
77ca0 70 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 prList*,.
77cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77cc0 20 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 Expr*,ExprList
77cd0 2a 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 *,int,Expr*,Expr
77ce0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
77cf0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
77d00 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 electDelete(sqli
77d10 74 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a te3*, Select*);.
77d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 SQLITE_PRIVATE T
77d30 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 able *sqlite3Src
77d40 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 ListLookup(Parse
77d50 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 *, SrcList*);.SQ
77d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
77d70 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e sqlite3IsReadOn
77d80 6c 79 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 ly(Parse*, Table
77d90 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
77da0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
77db0 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 ite3OpenTable(Pa
77dc0 72 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 rse*, int iCur,
77dd0 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c int iDb, Table*,
77de0 20 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 6e int);.#if defin
77df0 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ed(SQLITE_ENABLE
77e00 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c _UPDATE_DELETE_L
77e10 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 IMIT) && !define
77e20 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 d(SQLITE_OMIT_SU
77e30 42 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 BQUERY).SQLITE_P
77e40 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
77e50 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 50 ite3LimitWhere(P
77e60 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 arse *, SrcList
77e70 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 4c *, Expr *, ExprL
77e80 69 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 ist *, Expr *, E
77e90 78 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a xpr *, char *);.
77ea0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 #endif.SQLITE_PR
77eb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
77ec0 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 e3DeleteFrom(Par
77ed0 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 se*, SrcList*, E
77ee0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
77ef0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
77f00 65 33 55 70 64 61 74 65 28 50 61 72 73 65 2a 2c e3Update(Parse*,
77f10 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c SrcList*, ExprL
77f20 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 ist*, Expr*, int
77f30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
77f40 45 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c E WhereInfo *sql
77f50 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 50 ite3WhereBegin(P
77f60 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c arse*, SrcList*,
77f70 20 45 78 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 Expr*, ExprList
77f80 2a 2a 2c 20 75 38 2c 20 69 6e 74 29 3b 0a 53 51 **, u8, int);.SQ
77f90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
77fa0 64 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e d sqlite3WhereEn
77fb0 64 28 57 68 65 72 65 49 6e 66 6f 2a 29 3b 0a 53 d(WhereInfo*);.S
77fc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
77fd0 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
77fe0 65 47 65 74 43 6f 6c 75 6d 6e 28 50 61 72 73 65 eGetColumn(Parse
77ff0 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 *, Table*, int,
78000 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a int, int, int);.
78010 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
78020 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
78030 6f 64 65 4d 6f 76 65 28 50 61 72 73 65 2a 2c 20 odeMove(Parse*,
78040 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a int, int, int);.
78050 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
78060 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
78070 6f 64 65 43 6f 70 79 28 50 61 72 73 65 2a 2c 20 odeCopy(Parse*,
78080 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a int, int, int);.
78090 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
780a0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
780b0 6c 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 learColumnCache(
780c0 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 Parse*, int);.SQ
780d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
780e0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
780f0 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 heAffinityChange
78100 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e (Parse*, int, in
78110 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
78120 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
78130 78 70 72 57 72 69 74 61 62 6c 65 52 65 67 69 73 xprWritableRegis
78140 74 65 72 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b ter(Parse*,int);
78150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78160 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
78170 48 61 72 64 43 6f 70 79 28 50 61 72 73 65 2a 2c HardCopy(Parse*,
78180 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
78190 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
781a0 69 74 65 33 45 78 70 72 43 6f 64 65 28 50 61 72 ite3ExprCode(Par
781b0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 se*, Expr*, int)
781c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
781d0 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
781e0 43 6f 64 65 54 65 6d 70 28 50 61 72 73 65 2a 2c CodeTemp(Parse*,
781f0 20 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 Expr*, int*);.S
78200 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
78210 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
78220 65 54 61 72 67 65 74 28 50 61 72 73 65 2a 2c 20 eTarget(Parse*,
78230 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Expr*, int);.SQL
78240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
78250 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 sqlite3ExprCodeA
78260 6e 64 43 61 63 68 65 28 50 61 72 73 65 2a 2c 20 ndCache(Parse*,
78270 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Expr*, int);.SQL
78280 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
78290 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
782a0 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73 65 2a Constants(Parse*
782b0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
782c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
782d0 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 ite3ExprCodeExpr
782e0 4c 69 73 74 28 50 61 72 73 65 2a 2c 20 45 78 70 List(Parse*, Exp
782f0 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 rList*, int, int
78300 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
78310 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
78320 70 72 49 66 54 72 75 65 28 50 61 72 73 65 2a 2c prIfTrue(Parse*,
78330 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 Expr*, int, int
78340 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
78350 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
78360 70 72 49 66 46 61 6c 73 65 28 50 61 72 73 65 2a prIfFalse(Parse*
78370 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e , Expr*, int, in
78380 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
78390 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 TE Table *sqlite
783a0 33 46 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 3FindTable(sqlit
783b0 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c e3*,const char*,
783c0 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 const char*);.S
783d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 QLITE_PRIVATE Ta
783e0 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 ble *sqlite3Loca
783f0 74 65 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 69 teTable(Parse*,i
78400 6e 74 20 69 73 56 69 65 77 2c 63 6f 6e 73 74 20 nt isView,const
78410 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 char*, const cha
78420 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
78430 41 54 45 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 ATE Index *sqlit
78440 65 33 46 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 e3FindIndex(sqli
78450 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a te3*,const char*
78460 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a , const char*);.
78470 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
78480 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e oid sqlite3Unlin
78490 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 kAndDeleteTable(
784a0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e sqlite3*,int,con
784b0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
784c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
784d0 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 qlite3UnlinkAndD
784e0 65 6c 65 74 65 49 6e 64 65 78 28 73 71 6c 69 74 eleteIndex(sqlit
784f0 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 e3*,int,const ch
78500 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
78510 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
78520 33 56 61 63 75 75 6d 28 50 61 72 73 65 2a 29 3b 3Vacuum(Parse*);
78530 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78540 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 56 61 int sqlite3RunVa
78550 63 75 75 6d 28 63 68 61 72 2a 2a 2c 20 73 71 6c cuum(char**, sql
78560 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ite3*);.SQLITE_P
78570 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
78580 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
78590 6e 28 73 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 n(sqlite3*, Toke
785a0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
785b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
785c0 78 70 72 43 6f 6d 70 61 72 65 28 45 78 70 72 2a xprCompare(Expr*
785d0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
785e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
785f0 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 lite3ExprAnalyze
78600 41 67 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43 Aggregates(NameC
78610 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b ontext*, Expr*);
78620 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78630 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
78640 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e AnalyzeAggList(N
78650 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 45 78 70 72 ameContext*,Expr
78660 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 List*);.SQLITE_P
78670 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c RIVATE Vdbe *sql
78680 69 74 65 33 47 65 74 56 64 62 65 28 50 61 72 73 ite3GetVdbe(Pars
78690 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
786a0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
786b0 33 43 72 65 61 74 65 49 64 45 78 70 72 28 50 61 3CreateIdExpr(Pa
786c0 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 rse *, const cha
786d0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
786e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
786f0 50 72 6e 67 53 61 76 65 53 74 61 74 65 28 76 6f PrngSaveState(vo
78700 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
78710 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
78720 50 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 PrngRestoreState
78730 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 (void);.SQLITE_P
78740 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
78750 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61 74 te3PrngResetStat
78760 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f e(void);.SQLITE_
78770 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
78780 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 ite3RollbackAll(
78790 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 sqlite3*);.SQLIT
787a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
787b0 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 qlite3CodeVerify
787c0 53 63 68 65 6d 61 28 50 61 72 73 65 2a 2c 20 69 Schema(Parse*, i
787d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
787e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
787f0 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e BeginTransaction
78800 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 (Parse*, int);.S
78810 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
78820 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 id sqlite3Commit
78830 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 Transaction(Pars
78840 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
78850 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
78860 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 RollbackTransact
78870 69 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c ion(Parse*);.SQL
78880 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
78890 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e sqlite3Savepoin
788a0 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 54 t(Parse*, int, T
788b0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
788c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
788d0 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e te3CloseSavepoin
788e0 74 73 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 ts(sqlite3 *);.S
788f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
78900 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 t sqlite3ExprIsC
78910 6f 6e 73 74 61 6e 74 28 45 78 70 72 2a 29 3b 0a onstant(Expr*);.
78920 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
78930 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 nt sqlite3ExprIs
78940 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 ConstantNotJoin(
78950 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
78960 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
78970 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
78980 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 2a OrFunction(Expr*
78990 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
789a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
789b0 72 49 73 49 6e 74 65 67 65 72 28 45 78 70 72 2a rIsInteger(Expr*
789c0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f , int*);.SQLITE_
789d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
789e0 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 te3IsRowid(const
789f0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
78a00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
78a10 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 ite3GenerateRowD
78a20 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 elete(Parse*, Ta
78a30 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 ble*, int, int,
78a40 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
78a50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
78a60 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 3GenerateRowInde
78a70 78 44 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 xDelete(Parse*,
78a80 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Table*, int, int
78a90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
78aa0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 TE int sqlite3Ge
78ab0 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 50 nerateIndexKey(P
78ac0 61 72 73 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69 arse*, Index*, i
78ad0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 nt, int, int);.S
78ae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
78af0 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 id sqlite3Genera
78b00 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 teConstraintChec
78b10 6b 73 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a ks(Parse*,Table*
78b20 2c 69 6e 74 2c 69 6e 74 2c 0a 20 20 20 20 20 20 ,int,int,.
78b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
78b50 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c nt*,int,int,int,
78b60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
78b70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
78b80 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 3CompleteInserti
78b90 6f 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 on(Parse*, Table
78ba0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
78bb0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
78bc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
78bd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 E int sqlite3Ope
78be0 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 nTableAndIndices
78bf0 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
78c00 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
78c10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
78c20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 sqlite3BeginWrit
78c30 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 eOperation(Parse
78c40 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
78c50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
78c60 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 r *sqlite3ExprDu
78c70 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a p(sqlite3*,Expr*
78c80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
78c90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 6f E void sqlite3To
78ca0 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74 65 33 2a kenCopy(sqlite3*
78cb0 2c 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 ,Token*, Token*)
78cc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
78cd0 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 ExprList *sqlit
78ce0 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 73 71 e3ExprListDup(sq
78cf0 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69 73 74 2a lite3*,ExprList*
78d00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
78d10 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 E SrcList *sqlit
78d20 65 33 53 72 63 4c 69 73 74 44 75 70 28 73 71 6c e3SrcListDup(sql
78d30 69 74 65 33 2a 2c 53 72 63 4c 69 73 74 2a 29 3b ite3*,SrcList*);
78d40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78d50 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 IdList *sqlite3I
78d60 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 dListDup(sqlite3
78d70 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 *,IdList*);.SQLI
78d80 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 TE_PRIVATE Selec
78d90 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 t *sqlite3Select
78da0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c Dup(sqlite3*,Sel
78db0 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ect*);.SQLITE_PR
78dc0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
78dd0 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 e3FuncDefInsert(
78de0 46 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 46 75 FuncDefHash*, Fu
78df0 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f ncDef*);.SQLITE_
78e00 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 PRIVATE FuncDef
78e10 2a 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 *sqlite3FindFunc
78e20 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 63 6f tion(sqlite3*,co
78e30 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e nst char*,int,in
78e40 74 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 t,u8,int);.SQLIT
78e50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
78e60 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75 qlite3RegisterBu
78e70 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 iltinFunctions(s
78e80 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 qlite3*);.SQLITE
78e90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
78ea0 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 lite3RegisterDat
78eb0 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 eTimeFunctions(v
78ec0 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
78ed0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
78ee0 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 3RegisterGlobalF
78ef0 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a unctions(void);.
78f00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
78f10 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
78f20 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
78f30 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 SafetyOn(sqlite3
78f40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
78f50 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
78f60 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 SafetyOff(sqlite
78f70 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 3*);.#else.# def
78f80 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 ine sqlite3Safet
78f90 79 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 6e yOn(A) 0.# defin
78fa0 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f e sqlite3SafetyO
78fb0 66 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a 53 ff(A) 0.#endif.S
78fc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
78fd0 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 t sqlite3SafetyC
78fe0 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 heckOk(sqlite3*)
78ff0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
79000 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 int sqlite3Safe
79010 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 tyCheckSickOrOk(
79020 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 sqlite3*);.SQLIT
79030 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
79040 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b qlite3ChangeCook
79050 69 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b ie(Parse*, int);
79060 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
79070 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 QLITE_OMIT_VIEW)
79080 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
79090 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
790a0 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
790b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 void sqlite3Mat
790c0 65 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61 72 erializeView(Par
790d0 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 se*, Table*, Exp
790e0 72 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69 66 r*, int);.#endif
790f0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
79100 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 _OMIT_TRIGGER.SQ
79110 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
79120 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e oid sqlite3Begin
79130 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 Trigger(Parse*,
79140 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e Token*,Token*,in
79150 74 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 72 t,int,IdList*,Sr
79160 63 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 cList*,.
79170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
79180 20 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69 6e Expr*,int, in
79190 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
791a0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
791b0 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 50 3FinishTrigger(P
791c0 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53 74 arse*, TriggerSt
791d0 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 ep*, Token*);.SQ
791e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
791f0 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 oid sqlite3DropT
79200 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 53 rigger(Parse*, S
79210 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 rcList*, int);.S
79220 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
79230 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 void sqlite3Drop
79240 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65 TriggerPtr(Parse
79250 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 *, Trigger*);.SQ
79260 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
79270 6e 74 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 nt sqlite3Trigge
79280 72 73 45 78 69 73 74 28 54 61 62 6c 65 2a 2c 20 rsExist(Table*,
79290 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b int, ExprList*);
792a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
792b0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 int sqlite3Cod
792c0 65 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73 eRowTrigger(Pars
792d0 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 e*, int, ExprLis
792e0 74 2a 2c 20 69 6e 74 2c 20 54 61 62 6c 65 20 2a t*, int, Table *
792f0 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 0a 20 20 20 , int, int, .
79300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
79310 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74 int, int
79320 2c 20 75 33 32 2a 2c 20 75 33 32 2a 29 3b 0a 20 , u32*, u32*);.
79330 20 76 6f 69 64 20 73 71 6c 69 74 65 56 69 65 77 void sqliteView
79340 54 72 69 67 67 65 72 73 28 50 61 72 73 65 2a 2c Triggers(Parse*,
79350 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20 Table*, Expr*,
79360 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b int, ExprList*);
79370 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
79380 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 void sqlite3De
79390 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28 leteTriggerStep(
793a0 73 71 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65 sqlite3*, Trigge
793b0 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f rStep*);.SQLITE_
793c0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 PRIVATE Trigge
793d0 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 rStep *sqlite3Tr
793e0 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70 28 iggerSelectStep(
793f0 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a sqlite3*,Select*
79400 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
79410 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 E TriggerStep
79420 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 *sqlite3TriggerI
79430 6e 73 65 72 74 53 74 65 70 28 73 71 6c 69 74 65 nsertStep(sqlite
79440 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 49 64 4c 69 73 3*,Token*, IdLis
79450 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 t*,.
79460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
79470 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 Expr
79480 4c 69 73 74 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e List*,Select*,in
79490 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
794a0 54 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 TE TriggerStep
794b0 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 *sqlite3Trigger
794c0 55 70 64 61 74 65 53 74 65 70 28 73 71 6c 69 74 UpdateStep(sqlit
794d0 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 45 78 70 72 4c e3*,Token*,ExprL
794e0 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 ist*, Expr*, int
794f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
79500 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 E TriggerStep
79510 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 44 *sqlite3TriggerD
79520 65 6c 65 74 65 53 74 65 70 28 73 71 6c 69 74 65 eleteStep(sqlite
79530 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 2a 3*,Token*, Expr*
79540 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
79550 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
79560 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 DeleteTrigger(sq
79570 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65 72 2a lite3*, Trigger*
79580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
79590 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
795a0 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 UnlinkAndDeleteT
795b0 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c rigger(sqlite3*,
795c0 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 int,const char*)
795d0 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
795e0 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 sqlite3Triggers
795f0 45 78 69 73 74 28 42 2c 43 2c 44 2c 45 2c 46 29 Exist(B,C,D,E,F)
79600 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 0.# define sqli
79610 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 te3DeleteTrigger
79620 28 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73 (A,B).# define s
79630 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 qlite3DropTrigge
79640 72 50 74 72 28 41 2c 42 29 0a 23 20 64 65 66 69 rPtr(A,B).# defi
79650 6e 65 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b ne sqlite3Unlink
79660 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 AndDeleteTrigger
79670 28 41 2c 42 2c 43 29 0a 23 20 64 65 66 69 6e 65 (A,B,C).# define
79680 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 sqlite3CodeRowT
79690 72 69 67 67 65 72 28 41 2c 42 2c 43 2c 44 2c 45 rigger(A,B,C,D,E
796a0 2c 46 2c 47 2c 48 2c 49 2c 4a 2c 4b 29 20 30 0a ,F,G,H,I,J,K) 0.
796b0 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
796c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
796d0 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65 e3JoinType(Parse
796e0 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
796f0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
79700 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
79710 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 sqlite3CreateFor
79720 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 eignKey(Parse*,
79730 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e ExprList*, Token
79740 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e *, ExprList*, in
79750 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
79760 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
79770 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 eferForeignKey(P
79780 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 arse*, int);.#if
79790 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
797a0 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 53 _AUTHORIZATION.S
797b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
797c0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 void sqlite3Auth
797d0 52 65 61 64 28 50 61 72 73 65 2a 2c 45 78 70 72 Read(Parse*,Expr
797e0 2a 2c 53 63 68 65 6d 61 2a 2c 53 72 63 4c 69 73 *,Schema*,SrcLis
797f0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
79800 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
79810 33 41 75 74 68 43 68 65 63 6b 28 50 61 72 73 65 3AuthCheck(Parse
79820 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 *,int, const cha
79830 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c r*, const char*,
79840 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 const char*);.S
79850 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
79860 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 void sqlite3Auth
79870 43 6f 6e 74 65 78 74 50 75 73 68 28 50 61 72 73 ContextPush(Pars
79880 65 2a 2c 20 41 75 74 68 43 6f 6e 74 65 78 74 2a e*, AuthContext*
79890 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a , const char*);.
798a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
798b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 void sqlite3Aut
798c0 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75 74 68 hContextPop(Auth
798d0 43 6f 6e 74 65 78 74 2a 29 3b 0a 23 65 6c 73 65 Context*);.#else
798e0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
798f0 33 41 75 74 68 52 65 61 64 28 61 2c 62 2c 63 2c 3AuthRead(a,b,c,
79900 64 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 d).# define sqli
79910 74 65 33 41 75 74 68 43 68 65 63 6b 28 61 2c 62 te3AuthCheck(a,b
79920 2c 63 2c 64 2c 65 29 20 20 20 20 53 51 4c 49 54 ,c,d,e) SQLIT
79930 45 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 E_OK.# define sq
79940 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 lite3AuthContext
79950 50 75 73 68 28 61 2c 62 2c 63 29 0a 23 20 64 65 Push(a,b,c).# de
79960 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 fine sqlite3Auth
79970 43 6f 6e 74 65 78 74 50 6f 70 28 61 29 20 20 28 ContextPop(a) (
79980 28 76 6f 69 64 29 28 61 29 29 0a 23 65 6e 64 69 (void)(a)).#endi
79990 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 f.SQLITE_PRIVATE
799a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74 void sqlite3Att
799b0 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 ach(Parse*, Expr
799c0 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 *, Expr*, Expr*)
799d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
799e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 void sqlite3Det
799f0 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 ach(Parse*, Expr
79a00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
79a10 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
79a20 72 65 65 46 61 63 74 6f 72 79 28 63 6f 6e 73 74 reeFactory(const
79a30 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f sqlite3 *db, co
79a40 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
79a50 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ame,.
79a60 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
79a70 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74 omitJournal, int
79a80 20 6e 43 61 63 68 65 2c 20 69 6e 74 20 66 6c 61 nCache, int fla
79a90 67 73 2c 20 42 74 72 65 65 20 2a 2a 70 70 42 74 gs, Btree **ppBt
79aa0 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ree);.SQLITE_PRI
79ab0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
79ac0 46 69 78 49 6e 69 74 28 44 62 46 69 78 65 72 2a FixInit(DbFixer*
79ad0 2c 20 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 , Parse*, int, c
79ae0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 onst char*, cons
79af0 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 t Token*);.SQLIT
79b00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
79b10 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 lite3FixSrcList(
79b20 44 62 46 69 78 65 72 2a 2c 20 53 72 63 4c 69 73 DbFixer*, SrcLis
79b30 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
79b40 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 ATE int sqlite3F
79b50 69 78 53 65 6c 65 63 74 28 44 62 46 69 78 65 72 ixSelect(DbFixer
79b60 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c *, Select*);.SQL
79b70 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
79b80 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 44 sqlite3FixExpr(D
79b90 62 46 69 78 65 72 2a 2c 20 45 78 70 72 2a 29 3b bFixer*, Expr*);
79ba0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
79bb0 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 int sqlite3FixEx
79bc0 70 72 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c prList(DbFixer*,
79bd0 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c ExprList*);.SQL
79be0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
79bf0 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65 sqlite3FixTrigge
79c00 72 53 74 65 70 28 44 62 46 69 78 65 72 2a 2c 20 rStep(DbFixer*,
79c10 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 TriggerStep*);.S
79c20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
79c30 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 63 6f t sqlite3AtoF(co
79c40 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75 nst char *z, dou
79c50 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ble*);.SQLITE_PR
79c60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
79c70 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 3GetInt32(const
79c80 63 68 61 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53 char *, int*);.S
79c90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
79ca0 74 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 t sqlite3FitsIn6
79cb0 34 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 4Bits(const char
79cc0 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 *, int);.SQLITE
79cd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
79ce0 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e ite3Utf16ByteLen
79cf0 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 (const void *pDa
79d00 74 61 2c 20 69 6e 74 20 6e 43 68 61 72 29 3b 0a ta, int nChar);.
79d10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
79d20 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 nt sqlite3Utf8Ch
79d30 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 arLen(const char
79d40 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 42 79 *pData, int nBy
79d50 74 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 te);.SQLITE_PRIV
79d60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 ATE int sqlite3U
79d70 74 66 38 52 65 61 64 28 63 6f 6e 73 74 20 75 38 tf8Read(const u8
79d80 2a 2c 20 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f *, const u8*, co
79d90 6e 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a 2a nst u8**);../*.*
79da0 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72 65 * Routines to re
79db0 61 64 20 61 6e 64 20 77 72 69 74 65 20 76 61 72 ad and write var
79dc0 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 iable-length int
79dd0 65 67 65 72 73 2e 20 20 54 68 65 73 65 20 75 73 egers. These us
79de0 65 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66 69 ed to.** be defi
79df0 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75 74 ned locally, but
79e00 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65 20 now we use the
79e10 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73 20 varint routines
79e20 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a 2a in the util.c.**
79e30 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68 6f file. Code sho
79e40 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 52 uld use the MACR
79e50 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20 61 O forms below, a
79e60 73 20 74 68 65 20 56 61 72 69 6e 74 33 32 20 76 s the Varint32 v
79e70 65 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 63 ersions.** are c
79e80 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65 20 74 oded to assume t
79e90 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63 he single byte c
79ea0 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 ase is already h
79eb0 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20 0a 2a andled (which .*
79ec0 2a 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d * the MACRO form
79ed0 20 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54 does)..*/.SQLIT
79ee0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
79ef0 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 lite3PutVarint(u
79f00 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 nsigned char*, u
79f10 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 64);.SQLITE_PRIV
79f20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
79f30 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 67 utVarint32(unsig
79f40 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 29 3b ned char*, u32);
79f50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
79f60 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 u8 sqlite3GetVar
79f70 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e int(const unsign
79f80 65 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20 2a ed char *, u64 *
79f90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
79fa0 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 E u8 sqlite3GetV
79fb0 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e arint32(const un
79fc0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 signed char *, u
79fd0 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 32 *);.SQLITE_PR
79fe0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
79ff0 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76 3VarintLen(u64 v
7a000 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 );../*.** The he
7a010 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72 64 ader of a record
7a020 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 consists of a s
7a030 65 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c 65 equence variable
7a040 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 -length integers
7a050 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 ..** These integ
7a060 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20 61 ers are almost a
7a070 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64 20 lways small and
7a080 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 are encoded as a
7a090 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a single byte..**
7a0a0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d The following m
7a0b0 61 63 72 6f 73 20 74 61 6b 65 20 61 64 76 61 6e acros take advan
7a0c0 74 61 67 65 20 74 68 69 73 20 66 61 63 74 20 74 tage this fact t
7a0d0 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61 73 74 o provide a fast
7a0e0 20 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 64 encode.** and d
7a0f0 65 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e 74 ecode of the int
7a100 65 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f 72 egers in a recor
7a110 64 20 68 65 61 64 65 72 2e 20 20 49 74 20 69 73 d header. It is
7a120 20 66 61 73 74 65 72 20 66 6f 72 20 74 68 65 20 faster for the
7a130 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 common.** case w
7a140 68 65 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 here the integer
7a150 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 is a single byt
7a160 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74 74 e. It is a litt
7a170 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20 74 le slower when t
7a180 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 73 he.** integer is
7a190 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79 74 two or more byt
7a1a0 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c 6c es. But overall
7a1b0 20 69 74 20 69 73 20 66 61 73 74 65 72 2e 0a 2a it is faster..*
7a1c0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
7a1d0 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 ng expressions a
7a1e0 72 65 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a re equivalent:.*
7a1f0 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c *.** x = sql
7a200 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 ite3GetVarint32(
7a210 20 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20 20 A, &B );.**
7a220 20 78 20 3d 20 73 71 6c 69 74 65 33 50 75 74 56 x = sqlite3PutV
7a230 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b arint32( A, B );
7a240 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 67 .**.** x = g
7a250 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 etVarint32( A, B
7a260 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 70 );.** x = p
7a270 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 utVarint32( A, B
7a280 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e );.**.*/.#defin
7a290 65 20 67 65 74 56 61 72 69 6e 74 33 32 28 41 2c e getVarint32(A,
7a2a0 42 29 20 20 28 75 38 29 28 28 2a 28 41 29 3c 28 B) (u8)((*(A)<(
7a2b0 75 38 29 30 78 38 30 29 20 3f 20 28 28 42 29 20 u8)0x80) ? ((B)
7a2c0 3d 20 28 75 33 32 29 2a 28 41 29 29 2c 31 20 3a = (u32)*(A)),1 :
7a2d0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
7a2e0 74 33 32 28 28 41 29 2c 20 28 75 33 32 20 2a 29 t32((A), (u32 *)
7a2f0 26 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20 70 &(B))).#define p
7a300 75 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 utVarint32(A,B)
7a310 20 28 75 38 29 28 28 28 75 33 32 29 28 42 29 3c (u8)(((u32)(B)<
7a320 28 75 33 32 29 30 78 38 30 29 20 3f 20 28 2a 28 (u32)0x80) ? (*(
7a330 41 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 A) = (unsigned c
7a340 68 61 72 29 28 42 29 29 2c 31 20 3a 20 73 71 6c har)(B)),1 : sql
7a350 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 ite3PutVarint32(
7a360 28 41 29 2c 20 28 42 29 29 29 0a 23 64 65 66 69 (A), (B))).#defi
7a370 6e 65 20 67 65 74 56 61 72 69 6e 74 20 20 20 20 ne getVarint
7a380 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 sqlite3GetVarint
7a390 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 .#define putVari
7a3a0 6e 74 20 20 20 20 73 71 6c 69 74 65 33 50 75 74 nt sqlite3Put
7a3b0 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45 5f Varint...SQLITE_
7a3c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
7a3d0 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 ite3IndexAffinit
7a3e0 79 53 74 72 28 56 64 62 65 20 2a 2c 20 49 6e 64 yStr(Vdbe *, Ind
7a3f0 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ex *);.SQLITE_PR
7a400 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
7a410 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 e3TableAffinityS
7a420 74 72 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 tr(Vdbe *, Table
7a430 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
7a440 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 ATE char sqlite3
7a450 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 CompareAffinity(
7a460 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 Expr *pExpr, cha
7a470 72 20 61 66 66 32 29 3b 0a 53 51 4c 49 54 45 5f r aff2);.SQLITE_
7a480 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
7a490 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 te3IndexAffinity
7a4a0 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 Ok(Expr *pExpr,
7a4b0 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 char idx_affinit
7a4c0 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 y);.SQLITE_PRIVA
7a4d0 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 TE char sqlite3E
7a4e0 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 xprAffinity(Expr
7a4f0 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 *pExpr);.SQLITE
7a500 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
7a510 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 ite3Atoi64(const
7a520 20 63 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a 53 char*, i64*);.S
7a530 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
7a540 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 id sqlite3Error(
7a550 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 sqlite3*, int, c
7a560 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b onst char*,...);
7a570 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7a580 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 void *sqlite3Hex
7a590 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a 2c ToBlob(sqlite3*,
7a5a0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
7a5b0 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 int n);.SQLITE_P
7a5c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
7a5d0 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 50 61 e3TwoPartName(Pa
7a5e0 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 rse *, Token *,
7a5f0 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a Token *, Token *
7a600 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
7a610 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
7a620 71 6c 69 74 65 33 45 72 72 53 74 72 28 69 6e 74 qlite3ErrStr(int
7a630 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7a640 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 61 E int sqlite3Rea
7a650 64 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 dSchema(Parse *p
7a660 50 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 Parse);.SQLITE_P
7a670 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a RIVATE CollSeq *
7a680 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 sqlite3FindCollS
7a690 65 71 28 73 71 6c 69 74 65 33 2a 2c 75 38 20 65 eq(sqlite3*,u8 e
7a6a0 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a nc, const char *
7a6b0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ,int,int);.SQLIT
7a6c0 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 E_PRIVATE CollSe
7a6d0 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 q *sqlite3Locate
7a6e0 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 CollSeq(Parse *p
7a6f0 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 Parse, const cha
7a700 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e r *zName, int nN
7a710 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ame);.SQLITE_PRI
7a720 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 VATE CollSeq *sq
7a730 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 lite3ExprCollSeq
7a740 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
7a750 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 Expr *pExpr);.SQ
7a760 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
7a770 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 r *sqlite3ExprSe
7a780 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 tColl(Parse *pPa
7a790 72 73 65 2c 20 45 78 70 72 20 2a 2c 20 54 6f 6b rse, Expr *, Tok
7a7a0 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 en *);.SQLITE_PR
7a7b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7a7c0 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61 3CheckCollSeq(Pa
7a7d0 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 65 71 20 2a rse *, CollSeq *
7a7e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7a7f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 E int sqlite3Che
7a800 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 ckObjectName(Par
7a810 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 se *, const char
7a820 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
7a830 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
7a840 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 VdbeSetChanges(s
7a850 71 6c 69 74 65 33 20 2a 2c 20 69 6e 74 29 3b 0a qlite3 *, int);.
7a860 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7a870 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
7a880 74 65 33 56 61 6c 75 65 54 65 78 74 28 73 71 6c te3ValueText(sql
7a890 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 ite3_value*, u8)
7a8a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
7a8b0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 int sqlite3Valu
7a8c0 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 eBytes(sqlite3_v
7a8d0 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 alue*, u8);.SQLI
7a8e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
7a8f0 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 sqlite3ValueSetS
7a900 74 72 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 tr(sqlite3_value
7a910 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f *, int, const vo
7a920 69 64 20 2a 2c 75 38 2c 20 0a 20 20 20 20 20 20 id *,u8, .
7a930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7a940 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 void(*)(void*)
7a950 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7a960 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 E void sqlite3Va
7a970 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f lueFree(sqlite3_
7a980 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
7a990 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
7a9a0 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61 value *sqlite3Va
7a9b0 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a lueNew(sqlite3 *
7a9c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7a9d0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 E char *sqlite3U
7a9e0 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 tf16to8(sqlite3
7a9f0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
7aa00 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
7aa10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
7aa20 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73 71 ValueFromExpr(sq
7aa30 6c 69 74 65 33 20 2a 2c 20 45 78 70 72 20 2a 2c lite3 *, Expr *,
7aa40 20 75 38 2c 20 75 38 2c 20 73 71 6c 69 74 65 33 u8, u8, sqlite3
7aa50 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 51 4c 49 _value **);.SQLI
7aa60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
7aa70 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c sqlite3ValueAppl
7aa80 79 41 66 66 69 6e 69 74 79 28 73 71 6c 69 74 65 yAffinity(sqlite
7aa90 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38 2c 20 75 3_value *, u8, u
7aaa0 38 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 8);.#ifndef SQLI
7aab0 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a TE_AMALGAMATION.
7aac0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
7aad0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
7aae0 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 ar sqlite3UpperT
7aaf0 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51 4c 49 54 45 oLower[];.SQLITE
7ab00 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f _PRIVATE SQLITE_
7ab10 57 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 WSD struct Sqlit
7ab20 65 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 e3Config sqlite3
7ab30 43 6f 6e 66 69 67 3b 0a 53 51 4c 49 54 45 5f 50 Config;.SQLITE_P
7ab40 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 RIVATE SQLITE_WS
7ab50 44 20 46 75 6e 63 44 65 66 48 61 73 68 20 73 71 D FuncDefHash sq
7ab60 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 lite3GlobalFunct
7ab70 69 6f 6e 73 3b 0a 23 65 6e 64 69 66 0a 53 51 4c ions;.#endif.SQL
7ab80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
7ab90 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 sqlite3RootPage
7aba0 4d 6f 76 65 64 28 44 62 2a 2c 20 69 6e 74 2c 20 Moved(Db*, int,
7abb0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
7abc0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
7abd0 33 52 65 69 6e 64 65 78 28 50 61 72 73 65 2a 2c 3Reindex(Parse*,
7abe0 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 Token*, Token*)
7abf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
7ac00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 void sqlite3Alt
7ac10 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 erFunctions(sqli
7ac20 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
7ac30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
7ac40 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 e3AlterRenameTab
7ac50 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 le(Parse*, SrcLi
7ac60 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 st*, Token*);.SQ
7ac70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
7ac80 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e sqlite3GetToken
7ac90 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 (const unsigned
7aca0 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a char *, int *);.
7acb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
7acc0 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65 oid sqlite3Neste
7acd0 64 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 63 dParse(Parse*, c
7ace0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
7acf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
7ad00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
7ad10 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 irePreparedState
7ad20 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 2a 29 3b ments(sqlite3*);
7ad30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7ad40 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 void sqlite3Code
7ad50 53 75 62 73 65 6c 65 63 74 28 50 61 72 73 65 20 Subselect(Parse
7ad60 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2c 20 *, Expr *, int,
7ad70 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
7ad80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
7ad90 33 53 65 6c 65 63 74 50 72 65 70 28 50 61 72 73 3SelectPrep(Pars
7ada0 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d e*, Select*, Nam
7adb0 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 eContext*);.SQLI
7adc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
7add0 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
7ade0 72 4e 61 6d 65 73 28 4e 61 6d 65 43 6f 6e 74 65 rNames(NameConte
7adf0 78 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c xt*, Expr*);.SQL
7ae00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
7ae10 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 sqlite3ResolveS
7ae20 65 6c 65 63 74 4e 61 6d 65 73 28 50 61 72 73 65 electNames(Parse
7ae30 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 *, Select*, Name
7ae40 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 Context*);.SQLIT
7ae50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
7ae60 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 lite3ResolveOrde
7ae70 72 47 72 6f 75 70 42 79 28 50 61 72 73 65 2a 2c rGroupBy(Parse*,
7ae80 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 4c 69 Select*, ExprLi
7ae90 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a st*, const char*
7aea0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7aeb0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
7aec0 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 65 lumnDefault(Vdbe
7aed0 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74 *, Table *, int
7aee0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7aef0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c E void sqlite3Al
7af00 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 terFinishAddColu
7af10 6d 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 mn(Parse *, Toke
7af20 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 n *);.SQLITE_PRI
7af30 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
7af40 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 3AlterBeginAddCo
7af50 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 lumn(Parse *, Sr
7af60 63 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 cList *);.SQLITE
7af70 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 _PRIVATE CollSeq
7af80 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c *sqlite3GetColl
7af90 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 20 43 6f Seq(sqlite3*, Co
7afa0 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20 63 llSeq *, const c
7afb0 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c har *, int);.SQL
7afc0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
7afd0 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 sqlite3Affinity
7afe0 54 79 70 65 28 63 6f 6e 73 74 20 54 6f 6b 65 6e Type(const Token
7aff0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
7b000 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
7b010 6e 61 6c 79 7a 65 28 50 61 72 73 65 2a 2c 20 54 nalyze(Parse*, T
7b020 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a oken*, Token*);.
7b030 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
7b040 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 nt sqlite3Invoke
7b050 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79 BusyHandler(Busy
7b060 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c 49 54 Handler*);.SQLIT
7b070 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
7b080 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69 lite3FindDb(sqli
7b090 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 te3*, Token*);.S
7b0a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
7b0b0 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 t sqlite3Analysi
7b0c0 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a 2c 69 sLoad(sqlite3*,i
7b0d0 6e 74 20 69 44 42 29 3b 0a 53 51 4c 49 54 45 5f nt iDB);.SQLITE_
7b0e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
7b0f0 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 ite3DefaultRowEs
7b100 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 t(Index*);.SQLIT
7b110 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
7b120 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 qlite3RegisterLi
7b130 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 keFunctions(sqli
7b140 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 te3*, int);.SQLI
7b150 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
7b160 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 qlite3IsLikeFunc
7b170 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 45 78 tion(sqlite3*,Ex
7b180 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 29 3b pr*,int*,char*);
7b190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7b1a0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69 6e 69 void sqlite3Mini
7b1b0 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 50 61 mumFileFormat(Pa
7b1c0 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b rse*, int, int);
7b1d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7b1e0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65 void sqlite3Sche
7b1f0 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29 3b 0a maFree(void *);.
7b200 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
7b210 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 53 63 chema *sqlite3Sc
7b220 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65 33 20 hemaGet(sqlite3
7b230 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c *, Btree *);.SQL
7b240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
7b250 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
7b260 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 ndex(sqlite3 *db
7b270 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53 51 4c , Schema *);.SQL
7b280 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65 79 49 ITE_PRIVATE KeyI
7b290 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 nfo *sqlite3Inde
7b2a0 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 20 2a xKeyinfo(Parse *
7b2b0 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 , Index *);.SQLI
7b2c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
7b2d0 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 qlite3CreateFunc
7b2e0 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 (sqlite3 *, cons
7b2f0 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 t char *, int, i
7b300 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20 20 76 nt, void *, . v
7b310 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f oid (*)(sqlite3_
7b320 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
7b330 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a ite3_value **),.
7b340 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 void (*)(sqlit
7b350 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c e3_context*,int,
7b360 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
7b370 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 ), void (*)(sqli
7b380 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a te3_context*));.
7b390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
7b3a0 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 nt sqlite3ApiExi
7b3b0 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 t(sqlite3 *db, i
7b3c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
7b3d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
7b3e0 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 penTempDatabase(
7b3f0 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 Parse *);..SQLIT
7b400 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
7b410 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e qlite3StrAccumIn
7b420 69 74 28 53 74 72 41 63 63 75 6d 2a 2c 20 63 68 it(StrAccum*, ch
7b430 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a ar*, int, int);.
7b440 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
7b450 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 oid sqlite3StrAc
7b460 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41 63 63 cumAppend(StrAcc
7b470 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c um*,const char*,
7b480 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
7b490 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
7b4a0 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 e3StrAccumFinish
7b4b0 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c (StrAccum*);.SQL
7b4c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
7b4d0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
7b4e0 52 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a 29 Reset(StrAccum*)
7b4f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
7b500 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c void sqlite3Sel
7b510 65 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65 ectDestInit(Sele
7b520 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 ctDest*,int,int)
7b530 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 ;../*.** The int
7b540 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c 45 erface to the LE
7b550 4d 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70 61 MON-generated pa
7b560 72 73 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 rser.*/.SQLITE_P
7b570 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
7b580 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 ite3ParserAlloc(
7b590 76 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 void*(*)(size_t)
7b5a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7b5b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
7b5c0 72 73 65 72 46 72 65 65 28 76 6f 69 64 2a 2c 20 rserFree(void*,
7b5d0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
7b5e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7b5f0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 void sqlite3Pars
7b600 65 72 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 54 er(void*, int, T
7b610 6f 6b 65 6e 2c 20 50 61 72 73 65 2a 29 3b 0a 23 oken, Parse*);.#
7b620 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 ifdef YYTRACKMAX
7b630 53 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 STACKDEPTH.SQLIT
7b640 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
7b650 73 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 sqlite3ParserSta
7b660 63 6b 50 65 61 6b 28 76 6f 69 64 2a 29 3b 0a 23 ckPeak(void*);.#
7b670 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
7b680 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7b690 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 3AutoLoadExtensi
7b6a0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 ons(sqlite3*);.#
7b6b0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
7b6c0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
7b6d0 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 N.SQLITE_PRIVATE
7b6e0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 void sqlite3C
7b6f0 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 loseExtensions(s
7b700 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a qlite3*);.#else.
7b710 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
7b720 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 CloseExtensions(
7b730 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 X).#endif..#ifnd
7b740 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
7b750 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 HARED_CACHE.SQLI
7b760 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
7b770 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f d sqlite3TableLo
7b780 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c ck(Parse *, int,
7b790 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20 int, u8, const
7b7a0 63 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 char *);.#else.
7b7b0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
7b7c0 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c TableLock(v,w,x,
7b7d0 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 y,z).#endif..#if
7b7e0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
7b7f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
7b800 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 int sqlite3Utf8
7b810 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 To8(unsigned cha
7b820 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 r*);.#endif..#if
7b830 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
7b840 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 VIRTUALTABLE.#
7b850 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 define sqlite3Vt
7b860 61 62 43 6c 65 61 72 28 58 29 0a 23 20 20 64 65 abClear(X).# de
7b870 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 fine sqlite3Vtab
7b880 53 79 6e 63 28 58 2c 59 29 20 53 51 4c 49 54 45 Sync(X,Y) SQLITE
7b890 5f 4f 4b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 _OK.# define sq
7b8a0 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 lite3VtabRollbac
7b8b0 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 k(X).# define s
7b8c0 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 qlite3VtabCommit
7b8d0 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 (X).# define sq
7b8e0 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 lite3VtabInSync(
7b8f0 64 62 29 20 30 0a 23 65 6c 73 65 0a 53 51 4c 49 db) 0.#else.SQLI
7b900 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f TE_PRIVATE vo
7b910 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c id sqlite3VtabCl
7b920 65 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c ear(Table*);.SQL
7b930 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 ITE_PRIVATE i
7b940 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 nt sqlite3VtabSy
7b950 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 nc(sqlite3 *db,
7b960 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 char **);.SQLITE
7b970 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 _PRIVATE int
7b980 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 sqlite3VtabRollb
7b990 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ack(sqlite3 *db)
7b9a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
7b9b0 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 int sqlite3V
7b9c0 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 tabCommit(sqlite
7b9d0 33 20 2a 64 62 29 3b 0a 23 20 20 64 65 66 69 6e 3 *db);.# defin
7b9e0 65 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 e sqlite3VtabInS
7b9f0 79 6e 63 28 64 62 29 20 28 28 64 62 29 2d 3e 6e ync(db) ((db)->n
7ba00 56 54 72 61 6e 73 3e 30 20 26 26 20 28 64 62 29 VTrans>0 && (db)
7ba10 2d 3e 61 56 54 72 61 6e 73 3d 3d 30 29 0a 23 65 ->aVTrans==0).#e
7ba20 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
7ba30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
7ba40 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 VtabMakeWritable
7ba50 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b (Parse*,Table*);
7ba60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7ba70 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
7ba80 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 76 74 61 Lock(sqlite3_vta
7ba90 62 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 b*);.SQLITE_PRIV
7baa0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
7bab0 56 74 61 62 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 VtabUnlock(sqlit
7bac0 65 33 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 e3*, sqlite3_vta
7bad0 62 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 b*);.SQLITE_PRIV
7bae0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
7baf0 56 74 61 62 42 65 67 69 6e 50 61 72 73 65 28 50 VtabBeginParse(P
7bb00 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 arse*, Token*, T
7bb10 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a oken*, Token*);.
7bb20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
7bb30 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 46 oid sqlite3VtabF
7bb40 69 6e 69 73 68 50 61 72 73 65 28 50 61 72 73 65 inishParse(Parse
7bb50 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
7bb60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
7bb70 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e sqlite3VtabArgIn
7bb80 69 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 it(Parse*);.SQLI
7bb90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
7bba0 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78 sqlite3VtabArgEx
7bbb0 74 65 6e 64 28 50 61 72 73 65 2a 2c 20 54 6f 6b tend(Parse*, Tok
7bbc0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
7bbd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
7bbe0 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 73 VtabCallCreate(s
7bbf0 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f qlite3*, int, co
7bc00 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 68 61 72 nst char *, char
7bc10 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 **);.SQLITE_PRI
7bc20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
7bc30 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 VtabCallConnect(
7bc40 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b Parse*, Table*);
7bc50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7bc60 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 int sqlite3VtabC
7bc70 61 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c 69 74 allDestroy(sqlit
7bc80 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 e3*, int, const
7bc90 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f char *);.SQLITE_
7bca0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
7bcb0 74 65 33 56 74 61 62 42 65 67 69 6e 28 73 71 6c te3VtabBegin(sql
7bcc0 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 5f ite3 *, sqlite3_
7bcd0 76 74 61 62 20 2a 29 3b 0a 53 51 4c 49 54 45 5f vtab *);.SQLITE_
7bce0 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 PRIVATE FuncDef
7bcf0 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 *sqlite3VtabOver
7bd00 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c loadFunction(sql
7bd10 69 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 2a 2c ite3 *,FuncDef*,
7bd20 20 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 72 2a int nArg, Expr*
7bd30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7bd40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e E void sqlite3In
7bd50 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 73 71 validFunction(sq
7bd60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 lite3_context*,i
7bd70 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt,sqlite3_value
7bd80 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
7bd90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 ATE int sqlite3T
7bda0 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 ransferBindings(
7bdb0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20 sqlite3_stmt *,
7bdc0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 3b sqlite3_stmt *);
7bdd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7bde0 69 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 int sqlite3Repre
7bdf0 70 61 72 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c pare(Vdbe*);.SQL
7be00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
7be10 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
7be20 43 68 65 63 6b 4c 65 6e 67 74 68 28 50 61 72 73 CheckLength(Pars
7be30 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 e*, ExprList*, c
7be40 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
7be50 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c ITE_PRIVATE Coll
7be60 53 65 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 Seq *sqlite3Bina
7be70 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 ryCompareCollSeq
7be80 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a (Parse *, Expr *
7be90 2c 20 45 78 70 72 20 2a 29 3b 0a 0a 0a 2f 2a 0a , Expr *);.../*.
7bea0 2a 2a 20 41 76 61 69 6c 61 62 6c 65 20 66 61 75 ** Available fau
7beb0 6c 74 20 69 6e 6a 65 63 74 6f 72 73 2e 20 20 53 lt injectors. S
7bec0 68 6f 75 6c 64 20 62 65 20 6e 75 6d 62 65 72 65 hould be numbere
7bed0 64 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 d beginning with
7bee0 20 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 0..*/.#define S
7bef0 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 QLITE_FAULTINJEC
7bf00 54 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 30 TOR_MALLOC 0
7bf10 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7bf20 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f FAULTINJECTOR_CO
7bf30 55 4e 54 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a UNT 1../*.*
7bf40 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 * The interface
7bf50 74 6f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 66 to the code in f
7bf60 61 75 6c 74 2e 63 20 75 73 65 64 20 66 6f 72 20 ault.c used for
7bf70 69 64 65 6e 74 69 66 79 69 6e 67 20 22 62 65 6e identifying "ben
7bf80 69 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 ign".** malloc f
7bf90 61 69 6c 75 72 65 73 2e 20 54 68 69 73 20 69 73 ailures. This is
7bfa0 20 6f 6e 6c 79 20 70 72 65 73 65 6e 74 20 69 66 only present if
7bfb0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 SQLITE_OMIT_BUI
7bfc0 4c 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 69 73 20 LTIN_TEST.** is
7bfd0 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a not defined..*/.
7bfe0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
7bff0 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 MIT_BUILTIN_TEST
7c000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7c010 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 void sqlite3Be
7c020 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
7c030 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
7c040 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
7c050 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
7c060 6c 6f 63 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 loc(void);.#else
7c070 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
7c080 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
7c090 6c 6f 63 28 29 0a 20 20 23 64 65 66 69 6e 65 20 loc(). #define
7c0a0 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
7c0b0 4d 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 69 66 0a Malloc().#endif.
7c0c0 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 .#define IN_INDE
7c0d0 58 5f 52 4f 57 49 44 20 20 20 20 20 20 20 20 20 X_ROWID
7c0e0 20 20 31 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 1.#define IN_I
7c0f0 4e 44 45 58 5f 45 50 48 20 20 20 20 20 20 20 20 NDEX_EPH
7c100 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 49 2.#define I
7c110 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 20 20 N_INDEX_INDEX
7c120 20 20 20 20 20 20 20 20 33 0a 53 51 4c 49 54 45 3.SQLITE
7c130 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
7c140 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 ite3FindInIndex(
7c150 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c Parse *, Expr *,
7c160 20 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 65 66 20 int*);..#ifdef
7c170 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 SQLITE_ENABLE_AT
7c180 4f 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49 54 OMIC_WRITE.SQLIT
7c190 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
7c1a0 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 sqlite3JournalOp
7c1b0 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a en(sqlite3_vfs *
7c1c0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
7c1d0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c 20 sqlite3_file *,
7c1e0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
7c1f0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
7c200 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 sqlite3JournalSi
7c210 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ze(sqlite3_vfs *
7c220 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7c230 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a E int sqlite3J
7c240 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c ournalCreate(sql
7c250 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23 65 ite3_file *);.#e
7c260 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
7c270 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 lite3JournalSize
7c280 28 70 56 66 73 29 20 28 28 70 56 66 73 29 2d 3e (pVfs) ((pVfs)->
7c290 73 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69 66 szOsFile).#endif
7c2a0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
7c2b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
7c2c0 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 JournalOpen(sqli
7c2d0 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 53 51 4c te3_file *);.SQL
7c2e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
7c2f0 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
7c300 6c 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c lSize(void);.SQL
7c310 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
7c320 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 sqlite3IsMemJour
7c330 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 nal(sqlite3_file
7c340 20 2a 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 *);..#if SQLITE
7c350 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e _MAX_EXPR_DEPTH>
7c360 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 0.SQLITE_PRIVATE
7c370 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 void sqlite3E
7c380 78 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72 xprSetHeight(Par
7c390 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
7c3a0 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 *p);.SQLITE_PRI
7c3b0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
7c3c0 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 e3SelectExprHeig
7c3d0 68 74 28 53 65 6c 65 63 74 20 2a 29 3b 0a 53 51 ht(Select *);.SQ
7c3e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
7c3f0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 nt sqlite3ExprCh
7c400 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 2a eckHeight(Parse*
7c410 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 20 20 , int);.#else.
7c420 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 #define sqlite3E
7c430 78 70 72 53 65 74 48 65 69 67 68 74 28 78 2c 79 xprSetHeight(x,y
7c440 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 ). #define sqli
7c450 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 te3SelectExprHei
7c460 67 68 74 28 78 29 20 30 0a 20 20 23 64 65 66 69 ght(x) 0. #defi
7c470 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 ne sqlite3ExprCh
7c480 65 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0a 23 eckHeight(x,y).#
7c490 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
7c4a0 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 IVATE u32 sqlite
7c4b0 33 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 3Get4byte(const
7c4c0 75 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u8*);.SQLITE_PRI
7c4d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
7c4e0 33 50 75 74 34 62 79 74 65 28 75 38 2a 2c 20 75 3Put4byte(u8*, u
7c4f0 33 32 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 32);..#ifdef SQL
7c500 49 54 45 5f 53 53 45 0a 23 69 6e 63 6c 75 64 65 ITE_SSE.#include
7c510 20 22 73 73 65 49 6e 74 2e 68 22 0a 23 65 6e 64 "sseInt.h".#end
7c520 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
7c530 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 E_DEBUG.SQLITE_P
7c540 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
7c550 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65 lite3ParserTrace
7c560 28 46 49 4c 45 2a 2c 20 63 68 61 72 20 2a 29 3b (FILE*, char *);
7c570 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
7c580 66 20 74 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 f the SQLITE_ENA
7c590 42 4c 45 20 49 4f 54 52 41 43 45 20 65 78 69 73 BLE IOTRACE exis
7c5a0 74 73 20 74 68 65 6e 20 74 68 65 20 67 6c 6f 62 ts then the glob
7c5b0 61 6c 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 73 al variable.** s
7c5c0 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 69 73 qlite3IoTrace is
7c5d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
7c5e0 70 72 69 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74 printf-like rout
7c5f0 69 6e 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 ine used to.** p
7c600 72 69 6e 74 20 49 2f 4f 20 74 72 61 63 69 6e 67 rint I/O tracing
7c610 20 6d 65 73 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 messages. .*/.#
7c620 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
7c630 42 4c 45 5f 49 4f 54 52 41 43 45 0a 23 20 64 65 BLE_IOTRACE.# de
7c640 66 69 6e 65 20 49 4f 54 52 41 43 45 28 41 29 20 fine IOTRACE(A)
7c650 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 if( sqlite3IoTr
7c660 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 49 6f ace ){ sqlite3Io
7c670 54 72 61 63 65 20 41 3b 20 7d 0a 53 51 4c 49 54 Trace A; }.SQLIT
7c680 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
7c690 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 sqlite3VdbeIOTr
7c6a0 61 63 65 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 aceSql(Vdbe*);.S
7c6b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
7c6c0 69 64 20 28 2a 73 71 6c 69 74 65 33 49 6f 54 72 id (*sqlite3IoTr
7c6d0 61 63 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a ace)(const char*
7c6e0 2c 2e 2e 2e 29 3b 0a 23 65 6c 73 65 0a 23 20 64 ,...);.#else.# d
7c6f0 65 66 69 6e 65 20 49 4f 54 52 41 43 45 28 41 29 efine IOTRACE(A)
7c700 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
7c710 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 3VdbeIOTraceSql(
7c720 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 X).#endif..#endi
7c730 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
7c740 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 ** End of sqlite
7c750 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
7c760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c780 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
7c790 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 67 6c ** Begin file gl
7c7a0 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a obal.c *********
7c7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c7d0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 */./*.** 2008 Ju
7c7e0 6e 65 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ne 13.**.** The
7c7f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
7c800 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
7c810 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
7c820 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
7c830 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
7c840 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
7c850 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
7c860 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
7c870 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
7c880 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
7c890 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
7c8a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
7c8b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
7c8c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
7c8d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
7c8e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
7c8f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
7c900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c940 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
7c950 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 64 65 file contains de
7c960 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 67 6c 6f finitions of glo
7c970 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 61 6e bal variables an
7c980 64 20 63 6f 6e 74 61 6e 74 73 2e 0a 2a 2a 0a 2a d contants..**.*
7c990 2a 20 24 49 64 3a 20 67 6c 6f 62 61 6c 2e 63 2c * $Id: global.c,
7c9a0 76 20 31 2e 39 20 32 30 30 38 2f 31 32 2f 30 38 v 1.9 2008/12/08
7c9b0 20 31 38 3a 31 39 3a 31 38 20 64 72 68 20 45 78 18:19:18 drh Ex
7c9c0 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6e 20 61 p $.*/.../* An a
7c9d0 72 72 61 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20 rray to map all
7c9e0 75 70 70 65 72 2d 63 61 73 65 20 63 68 61 72 61 upper-case chara
7c9f0 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72 cters into their
7ca00 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a corresponding.*
7ca10 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20 63 68 61 * lower-case cha
7ca20 72 61 63 74 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 53 racter. .**.** S
7ca30 51 4c 69 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69 QLite only consi
7ca40 64 65 72 73 20 55 53 2d 41 53 43 49 49 20 28 6f ders US-ASCII (o
7ca50 72 20 45 42 43 44 49 43 29 20 63 68 61 72 61 63 r EBCDIC) charac
7ca60 74 65 72 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 ters. We do not
7ca70 0a 2a 2a 20 68 61 6e 64 6c 65 20 63 61 73 65 20 .** handle case
7ca80 63 6f 6e 76 65 72 73 69 6f 6e 73 20 66 6f 72 20 conversions for
7ca90 74 68 65 20 55 54 46 20 63 68 61 72 61 63 74 65 the UTF characte
7caa0 72 20 73 65 74 20 73 69 6e 63 65 20 74 68 65 20 r set since the
7cab0 74 61 62 6c 65 73 0a 2a 2a 20 69 6e 76 6f 6c 76 tables.** involv
7cac0 65 64 20 61 72 65 20 6e 65 61 72 6c 79 20 61 73 ed are nearly as
7cad0 20 62 69 67 20 6f 72 20 62 69 67 67 65 72 20 74 big or bigger t
7cae0 68 61 6e 20 53 51 4c 69 74 65 20 69 74 73 65 6c han SQLite itsel
7caf0 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 f..*/.SQLITE_PRI
7cb00 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 VATE const unsig
7cb10 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 ned char sqlite3
7cb20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d UpperToLower[] =
7cb30 20 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 {.#ifdef SQLITE
7cb40 5f 41 53 43 49 49 0a 20 20 20 20 20 20 30 2c 20 _ASCII. 0,
7cb50 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 1, 2, 3, 4,
7cb60 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 5, 6, 7, 8,
7cb70 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 9, 10, 11, 12,
7cb80 31 33 2c 20 31 34 2c 20 31 35 2c 20 31 36 2c 20 13, 14, 15, 16,
7cb90 31 37 2c 0a 20 20 20 20 20 31 38 2c 20 31 39 2c 17,. 18, 19,
7cba0 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20, 21, 22, 23,
7cbb0 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 24, 25, 26, 27,
7cbc0 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 28, 29, 30, 31,
7cbd0 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 32, 33, 34, 35,
7cbe0 0a 20 20 20 20 20 33 36 2c 20 33 37 2c 20 33 38 . 36, 37, 38
7cbf0 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 , 39, 40, 41, 42
7cc00 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 , 43, 44, 45, 46
7cc10 2c 20 34 37 2c 20 34 38 2c 20 34 39 2c 20 35 30 , 47, 48, 49, 50
7cc20 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20 , 51, 52, 53,.
7cc30 20 20 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 54, 55, 56, 5
7cc40 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 7, 58, 59, 60, 6
7cc50 31 2c 20 36 32 2c 20 36 33 2c 20 36 34 2c 20 39 1, 62, 63, 64, 9
7cc60 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 7, 98, 99,100,10
7cc70 31 2c 31 30 32 2c 31 30 33 2c 0a 20 20 20 20 31 1,102,103,. 1
7cc80 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 31 04,105,106,107,1
7cc90 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 08,109,110,111,1
7cca0 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 12,113,114,115,1
7ccb0 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 16,117,118,119,1
7ccc0 32 30 2c 31 32 31 2c 0a 20 20 20 20 31 32 32 2c 20,121,. 122,
7ccd0 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 91, 92, 93, 94,
7cce0 20 39 35 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c 95, 96, 97, 98,
7ccf0 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 99,100,101,102,
7cd00 31 30 33 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c 103,104,105,106,
7cd10 31 30 37 2c 0a 20 20 20 20 31 30 38 2c 31 30 39 107,. 108,109
7cd20 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 ,110,111,112,113
7cd30 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 ,114,115,116,117
7cd40 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 ,118,119,120,121
7cd50 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 ,122,123,124,125
7cd60 2c 0a 20 20 20 20 31 32 36 2c 31 32 37 2c 31 32 ,. 126,127,12
7cd70 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 8,129,130,131,13
7cd80 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 2,133,134,135,13
7cd90 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 6,137,138,139,14
7cda0 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20 0,141,142,143,.
7cdb0 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 144,145,146,1
7cdc0 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 47,148,149,150,1
7cdd0 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 51,152,153,154,1
7cde0 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 38 2c 31 55,156,157,158,1
7cdf0 35 39 2c 31 36 30 2c 31 36 31 2c 0a 20 20 20 20 59,160,161,.
7ce00 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 162,163,164,165,
7ce10 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 166,167,168,169,
7ce20 31 37 30 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c 170,171,172,173,
7ce30 31 37 34 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c 174,175,176,177,
7ce40 31 37 38 2c 31 37 39 2c 0a 20 20 20 20 31 38 30 178,179,. 180
7ce50 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 ,181,182,183,184
7ce60 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 ,185,186,187,188
7ce70 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 31 39 32 ,189,190,191,192
7ce80 2c 31 39 33 2c 31 39 34 2c 31 39 35 2c 31 39 36 ,193,194,195,196
7ce90 2c 31 39 37 2c 0a 20 20 20 20 31 39 38 2c 31 39 ,197,. 198,19
7cea0 39 2c 32 30 30 2c 32 30 31 2c 32 30 32 2c 32 30 9,200,201,202,20
7ceb0 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 3,204,205,206,20
7cec0 37 2c 32 30 38 2c 32 30 39 2c 32 31 30 2c 32 31 7,208,209,210,21
7ced0 31 2c 32 31 32 2c 32 31 33 2c 32 31 34 2c 32 31 1,212,213,214,21
7cee0 35 2c 0a 20 20 20 20 32 31 36 2c 32 31 37 2c 32 5,. 216,217,2
7cef0 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 18,219,220,221,2
7cf00 32 32 2c 32 32 33 2c 32 32 34 2c 32 32 35 2c 32 22,223,224,225,2
7cf10 32 36 2c 32 32 37 2c 32 32 38 2c 32 32 39 2c 32 26,227,228,229,2
7cf20 33 30 2c 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a 30,231,232,233,.
7cf30 20 20 20 20 32 33 34 2c 32 33 35 2c 32 33 36 2c 234,235,236,
7cf40 32 33 37 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c 237,238,239,240,
7cf50 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 241,242,243,244,
7cf60 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 245,246,247,248,
7cf70 32 34 39 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20 249,250,251,.
7cf80 20 32 35 32 2c 32 35 33 2c 32 35 34 2c 32 35 35 252,253,254,255
7cf90 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
7cfa0 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 20 QLITE_EBCDIC.
7cfb0 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 0, 1, 2, 3
7cfc0 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 , 4, 5, 6, 7
7cfd0 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 , 8, 9, 10, 11
7cfe0 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 , 12, 13, 14, 15
7cff0 2c 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20 , /* 0x */.
7d000 31 36 2c 20 31 37 2c 20 31 38 2c 20 31 39 2c 20 16, 17, 18, 19,
7d010 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 20, 21, 22, 23,
7d020 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 24, 25, 26, 27,
7d030 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 28, 29, 30, 31,
7d040 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 20 20 33 32 /* 1x */. 32
7d050 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 20 33 36 , 33, 34, 35, 36
7d060 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 , 37, 38, 39, 40
7d070 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 , 41, 42, 43, 44
7d080 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a , 45, 46, 47, /*
7d090 20 32 78 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20 2x */. 48,
7d0a0 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 49, 50, 51, 52,
7d0b0 35 33 2c 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 53, 54, 55, 56,
7d0c0 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 57, 58, 59, 60,
7d0d0 36 31 2c 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33 61, 62, 63, /* 3
7d0e0 78 20 2a 2f 0a 20 20 20 20 20 36 34 2c 20 36 35 x */. 64, 65
7d0f0 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 , 66, 67, 68, 69
7d100 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 , 70, 71, 72, 73
7d110 2c 20 37 34 2c 20 37 35 2c 20 37 36 2c 20 37 37 , 74, 75, 76, 77
7d120 2c 20 37 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20 , 78, 79, /* 4x
7d130 2a 2f 0a 20 20 20 20 20 38 30 2c 20 38 31 2c 20 */. 80, 81,
7d140 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 82, 83, 84, 85,
7d150 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 20 86, 87, 88, 89,
7d160 39 30 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 90, 91, 92, 93,
7d170 39 34 2c 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f 94, 95, /* 5x */
7d180 0a 20 20 20 20 20 39 36 2c 20 39 37 2c 20 36 36 . 96, 97, 66
7d190 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 , 67, 68, 69, 70
7d1a0 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 31 30 36 , 71, 72, 73,106
7d1b0 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 ,107,108,109,110
7d1c0 2c 31 31 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20 ,111, /* 6x */.
7d1d0 20 20 20 31 31 32 2c 20 38 31 2c 20 38 32 2c 20 112, 81, 82,
7d1e0 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 83, 84, 85, 86,
7d1f0 38 37 2c 20 38 38 2c 20 38 39 2c 31 32 32 2c 31 87, 88, 89,122,1
7d200 32 33 2c 31 32 34 2c 31 32 35 2c 31 32 36 2c 31 23,124,125,126,1
7d210 32 37 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 27, /* 7x */.
7d220 20 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 128,129,130,131
7d230 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 ,132,133,134,135
7d240 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 ,136,137,138,139
7d250 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 ,140,141,142,143
7d260 2c 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31 , /* 8x */. 1
7d270 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 44,145,146,147,1
7d280 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 48,149,150,151,1
7d290 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 52,153,154,155,1
7d2a0 35 36 2c 31 35 37 2c 31 35 36 2c 31 35 39 2c 20 56,157,156,159,
7d2b0 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 36 30 /* 9x */. 160
7d2c0 2c 31 36 31 2c 31 36 32 2c 31 36 33 2c 31 36 34 ,161,162,163,164
7d2d0 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 ,165,166,167,168
7d2e0 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 34 30 ,169,170,171,140
7d2f0 2c 31 34 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a ,141,142,175, /*
7d300 20 41 78 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31 Ax */. 176,1
7d310 37 37 2c 31 37 38 2c 31 37 39 2c 31 38 30 2c 31 77,178,179,180,1
7d320 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 81,182,183,184,1
7d330 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 85,186,187,188,1
7d340 38 39 2c 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42 89,190,191, /* B
7d350 78 20 2a 2f 0a 20 20 20 20 31 39 32 2c 31 32 39 x */. 192,129
7d360 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 ,130,131,132,133
7d370 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 ,134,135,136,137
7d380 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 ,202,203,204,205
7d390 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20 ,206,207, /* Cx
7d3a0 2a 2f 0a 20 20 20 20 32 30 38 2c 31 34 35 2c 31 */. 208,145,1
7d3b0 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 46,147,148,149,1
7d3c0 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 32 50,151,152,153,2
7d3d0 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 18,219,220,221,2
7d3e0 32 32 2c 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f 22,223, /* Dx */
7d3f0 0a 20 20 20 20 32 32 34 2c 32 32 35 2c 31 36 32 . 224,225,162
7d400 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 ,163,164,165,166
7d410 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 32 33 32 ,167,168,169,232
7d420 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 ,203,204,205,206
7d430 2c 32 30 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20 ,207, /* Ex */.
7d440 20 20 20 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 239,240,241,2
7d450 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 42,243,244,245,2
7d460 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 46,247,248,249,2
7d470 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 19,220,221,222,2
7d480 35 35 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e 55, /* Fx */.#en
7d490 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 dif.};../*.** Th
7d4a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67 e following sing
7d4b0 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 leton contains t
7d4c0 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 he global config
7d4d0 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 74 uration for.** t
7d4e0 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 he SQLite librar
7d4f0 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
7d500 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 VATE SQLITE_WSD
7d510 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f struct Sqlite3Co
7d520 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 nfig sqlite3Conf
7d530 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54 45 ig = {. SQLITE
7d540 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 _DEFAULT_MEMSTAT
7d550 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61 74 US, /* bMemstat
7d560 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 20 20 */. 1,
7d570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d580 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78 20 /* bCoreMutex
7d590 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 48 52 */. SQLITE_THR
7d5a0 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 20 20 EADSAFE==1,
7d5b0 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20 2a /* bFullMutex *
7d5c0 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66 65 2c /. 0x7ffffffe,
7d5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d5e0 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20 /* mxStrlen */.
7d5f0 20 20 31 30 30 2c 20 20 20 20 20 20 20 20 20 20 100,
7d600 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
7d610 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 szLookaside */.
7d620 20 20 35 30 30 2c 20 20 20 20 20 20 20 20 20 20 500,
7d630 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
7d640 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 nLookaside */.
7d650 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c {0,0,0,0,0,0,0,
7d660 30 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 0}, /* m
7d670 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c */. {0,0,0,0,
7d680 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20 0,0,0,0,0},
7d690 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a 20 20 /* mutex */.
7d6a0 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c {0,0,0,0,0,0,0,
7d6b0 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a 20 70 0,0,0,0}, /* p
7d6c0 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 76 6f 69 cache */. (voi
7d6d0 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 d*)0,
7d6e0 20 20 20 20 20 20 20 2f 2a 20 70 48 65 61 70 20 /* pHeap
7d6f0 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
7d700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d710 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 20 20 /* nHeap */.
7d720 30 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0, 0,
7d730 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 6e /* mn
7d740 48 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f 0a Heap, mxHeap */.
7d750 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 (void*)0,
7d760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7d770 20 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 pScratch */.
7d780 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
7d790 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a /* sz
7d7a0 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c Scratch */. 0,
7d7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d7c0 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 63 72 /* nScr
7d7d0 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f 69 64 atch */. (void
7d7e0 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 *)0,
7d7f0 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 2a /* pPage *
7d800 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 /. 0,
7d810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d820 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 20 20 /* szPage */.
7d830 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
7d840 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 50 /* nP
7d850 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 age */. 0,
7d860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d870 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 65 72 /* mxParser
7d880 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c 20 20 Stack */. 0,
7d890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d8a0 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 64 /* shared
7d8b0 43 61 63 68 65 45 6e 61 62 6c 65 64 20 2a 2f 0a CacheEnabled */.
7d8c0 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 /* All the re
7d8d0 73 74 20 6e 65 65 64 20 74 6f 20 61 6c 77 61 79 st need to alway
7d8e0 73 20 62 65 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 s be zero */.
7d8f0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
7d900 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 /* is
7d910 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Init */. 0,
7d920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d930 20 20 20 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 /* inProgr
7d940 65 73 73 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 ess */. 0,
7d950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d960 20 20 20 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 /* isMalloc
7d970 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Init */. 0,
7d980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d990 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75 /* pInitMu
7d9a0 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 tex */. 0,
7d9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d9c0 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74 /* nRefInit
7d9d0 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a Mutex */.};.../*
7d9e0 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 .** Hash table f
7d9f0 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 or global functi
7da00 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20 ons - functions
7da10 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a common to all.**
7da20 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
7da30 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e tions. After in
7da40 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 itialization, th
7da50 69 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72 is table is.** r
7da60 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c ead-only..*/.SQL
7da70 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 ITE_PRIVATE SQLI
7da80 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 TE_WSD FuncDefHa
7da90 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sh sqlite3Global
7daa0 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 2a 2a Functions;../***
7dab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
7dac0 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a of global.c ****
7dad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7dae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7daf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
7db00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
7db10 6e 20 66 69 6c 65 20 73 74 61 74 75 73 2e 63 20 n file status.c
7db20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7db30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7db40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
7db50 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 38 0a 2a * 2008 June 18.*
7db60 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
7db70 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
7db80 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
7db90 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
7dba0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
7dbb0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
7dbc0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
7dbd0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
7dbe0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
7dbf0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
7dc00 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
7dc10 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
7dc20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
7dc30 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
7dc40 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
7dc50 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
7dc60 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
7dc70 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
7dc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7dc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7dca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7dcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
7dcc0 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 .** This module
7dcd0 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 implements the s
7dce0 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 20 qlite3_status()
7dcf0 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 72 65 interface and re
7dd00 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f lated.** functio
7dd10 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 nality..**.** $I
7dd20 64 3a 20 73 74 61 74 75 73 2e 63 2c 76 20 31 2e d: status.c,v 1.
7dd30 39 20 32 30 30 38 2f 30 39 2f 30 32 20 30 30 3a 9 2008/09/02 00:
7dd40 35 32 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a 52:52 drh Exp $.
7dd50 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 */../*.** Variab
7dd60 6c 65 73 20 69 6e 20 77 68 69 63 68 20 74 6f 20 les in which to
7dd70 72 65 63 6f 72 64 20 73 74 61 74 75 73 20 69 6e record status in
7dd80 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 formation..*/.ty
7dd90 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
7dda0 69 74 65 33 53 74 61 74 54 79 70 65 20 73 71 6c ite3StatType sql
7ddb0 69 74 65 33 53 74 61 74 54 79 70 65 3b 0a 73 74 ite3StatType;.st
7ddc0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
7ddd0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 53 74 struct sqlite3St
7dde0 61 74 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e atType {. int n
7ddf0 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 owValue[9];
7de00 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 /* Current v
7de10 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 alue */. int mx
7de20 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 Value[9];
7de30 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 /* Maximum va
7de40 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 lue */.} sqlite3
7de50 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b Stat = { {0,}, {
7de60 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 0,} };.../* The
7de70 22 77 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20 "wsdStat" macro
7de80 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 will resolve to
7de90 74 68 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 the status infor
7dea0 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 mation.** state
7deb0 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 vector. If writ
7dec0 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 able static data
7ded0 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 is unsupported
7dee0 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a on the target,.*
7def0 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 * we have to loc
7df00 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65 ate the state ve
7df10 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 ctor at run-time
7df20 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 . In the more c
7df30 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 ommon.** case wh
7df40 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 ere writable sta
7df50 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 tic data is supp
7df60 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 20 63 orted, wsdStat c
7df70 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c an refer directl
7df80 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c y.** to the "sql
7df90 69 74 65 33 53 74 61 74 22 20 73 74 61 74 65 20 ite3Stat" state
7dfa0 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 vector declared
7dfb0 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 above..*/.#ifdef
7dfc0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 SQLITE_OMIT_WSD
7dfd0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 .# define wsdSta
7dfe0 74 49 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74 tInit sqlite3St
7dff0 61 74 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f atType *x = &GLO
7e000 42 41 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54 BAL(sqlite3StatT
7e010 79 70 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29 ype,sqlite3Stat)
7e020 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 .# define wsdSta
7e030 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 t x[0].#else.# d
7e040 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 efine wsdStatIni
7e050 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 t.# define wsdSt
7e060 61 74 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23 at sqlite3Stat.#
7e070 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 endif../*.** Ret
7e080 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 urn the current
7e090 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 value of a statu
7e0a0 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a s parameter..*/.
7e0b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
7e0c0 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 nt sqlite3Status
7e0d0 56 61 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 Value(int op){.
7e0e0 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 wsdStatInit;.
7e0f0 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 assert( op>=0 &&
7e100 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 op<ArraySize(ws
7e110 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 dStat.nowValue)
7e120 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53 );. return wsdS
7e130 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d tat.nowValue[op]
7e140 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e ;.}../*.** Add N
7e150 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 to the value of
7e160 20 61 20 73 74 61 74 75 73 20 72 65 63 6f 72 64 a status record
7e170 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 . It is assumed
7e180 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c that the.** cal
7e190 6c 65 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 ler holds approp
7e1a0 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a riate locks..*/.
7e1b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
7e1c0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 oid sqlite3Statu
7e1d0 73 41 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 sAdd(int op, int
7e1e0 20 4e 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e N){. wsdStatIn
7e1f0 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 it;. assert( op
7e200 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 >=0 && op<ArrayS
7e210 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 ize(wsdStat.nowV
7e220 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 alue) );. wsdSt
7e230 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 at.nowValue[op]
7e240 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 += N;. if( wsdS
7e250 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d tat.nowValue[op]
7e260 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 >wsdStat.mxValue
7e270 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 [op] ){. wsdS
7e280 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 tat.mxValue[op]
7e290 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c = wsdStat.nowVal
7e2a0 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f ue[op];. }.}../
7e2b0 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c *.** Set the val
7e2c0 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 74 ue of a status t
7e2d0 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 o X..*/.SQLITE_P
7e2e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
7e2f0 74 65 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 te3StatusSet(int
7e300 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 op, int X){. w
7e310 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 sdStatInit;. as
7e320 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f sert( op>=0 && o
7e330 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 p<ArraySize(wsdS
7e340 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b tat.nowValue) );
7e350 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 . wsdStat.nowVa
7e360 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 lue[op] = X;. i
7e370 66 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 f( wsdStat.nowVa
7e380 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e lue[op]>wsdStat.
7e390 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 mxValue[op] ){.
7e3a0 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c wsdStat.mxVal
7e3b0 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 ue[op] = wsdStat
7e3c0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 .nowValue[op];.
7e3d0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 }.}../*.** Quer
7e3e0 79 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 y status informa
7e3f0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tion..**.** This
7e400 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
7e410 61 73 73 75 6d 65 73 20 74 68 61 74 20 72 65 61 assumes that rea
7e420 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 ding or writing
7e430 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 an aligned.** 32
7e440 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 -bit integer is
7e450 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 an atomic operat
7e460 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73 ion. If that as
7e470 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 sumption is not
7e480 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 true,.** then th
7e490 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f is routine is no
7e4a0 74 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f t threadsafe..*/
7e4b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
7e4c0 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 sqlite3_status(i
7e4d0 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 nt op, int *pCur
7e4e0 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 rent, int *pHigh
7e4f0 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 water, int reset
7e500 46 6c 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74 Flag){. wsdStat
7e510 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 Init;. if( op<0
7e520 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a || op>=ArraySiz
7e530 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c e(wsdStat.nowVal
7e540 75 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ue) ){. retur
7e550 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
7e560 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74 . }. *pCurrent
7e570 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 = wsdStat.nowVa
7e580 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67 lue[op];. *pHig
7e590 68 77 61 74 65 72 20 3d 20 77 73 64 53 74 61 74 hwater = wsdStat
7e5a0 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 .mxValue[op];.
7e5b0 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b if( resetFlag ){
7e5c0 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 . wsdStat.mxV
7e5d0 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 alue[op] = wsdSt
7e5e0 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b at.nowValue[op];
7e5f0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
7e600 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
7e610 2a 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69 * Query status i
7e620 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 nformation for a
7e630 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 single database
7e640 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 connection.*/.S
7e650 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
7e660 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 lite3_db_status(
7e670 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
7e680 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
7e690 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
7e6a0 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 74 75 73 ion whose status
7e6b0 20 69 73 20 64 65 73 69 72 65 64 20 2a 2f 0a 20 is desired */.
7e6c0 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 int op,
7e6d0 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 /* Status
7e6e0 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a verb */. int *
7e6f0 70 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 20 pCurrent,
7e700 20 2f 2a 20 57 72 69 74 65 20 63 75 72 72 65 6e /* Write curren
7e710 74 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a t value here */.
7e720 20 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65 int *pHighwate
7e730 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 r, /* Write
7e740 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b high-water mark
7e750 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 here */. int r
7e760 65 73 65 74 46 6c 61 67 20 20 20 20 20 20 20 20 esetFlag
7e770 20 2f 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77 /* Reset high-w
7e780 61 74 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75 ater mark if tru
7e790 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 e */.){. switch
7e7a0 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 ( op ){. case
7e7b0 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 SQLITE_DBSTATUS
7e7c0 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a _LOOKASIDE_USED:
7e7d0 20 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65 {. *pCurre
7e7e0 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 nt = db->lookasi
7e7f0 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a de.nOut;. *
7e800 70 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d pHighwater = db-
7e810 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 >lookaside.mxOut
7e820 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 65 ;. if( rese
7e830 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 tFlag ){.
7e840 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d db->lookaside.m
7e850 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 xOut = db->looka
7e860 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 side.nOut;.
7e870 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
7e880 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
7e890 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 t: {. retur
7e8a0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
7e8b0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
7e8c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
7e8d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
7e8e0 20 45 6e 64 20 6f 66 20 73 74 61 74 75 73 2e 63 End of status.c
7e8f0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
7e900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
7e920 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
7e930 20 42 65 67 69 6e 20 66 69 6c 65 20 64 61 74 65 Begin file date
7e940 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
7e950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
7e970 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f ./*.** 2003 Octo
7e980 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 31.**.** The
7e990 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
7e9a0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
7e9b0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
7e9c0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
7e9d0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
7e9e0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
7e9f0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
7ea00 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
7ea10 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
7ea20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
7ea30 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
7ea40 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
7ea50 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
7ea60 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
7ea70 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
7ea80 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
7ea90 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
7eaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7eab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7eac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7ead0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7eae0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
7eaf0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
7eb00 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
7eb10 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65 20 implement date
7eb20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63 and time.** func
7eb30 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 tions for SQLite
7eb40 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 . .**.** There
7eb50 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f is only one expo
7eb60 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 rted symbol in t
7eb70 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66 his file - the f
7eb80 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 unction.** sqlit
7eb90 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69 e3RegisterDateTi
7eba0 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f meFunctions() fo
7ebb0 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f und at the botto
7ebc0 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a m of the file..*
7ebd0 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 * All other code
7ebe0 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e has file scope.
7ebf0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 61 74 65 .**.** $Id: date
7ec00 2e 63 2c 76 20 31 2e 39 39 20 32 30 30 38 2f 31 .c,v 1.99 2008/1
7ec10 32 2f 32 30 20 31 33 3a 31 38 3a 35 30 20 64 72 2/20 13:18:50 dr
7ec20 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 53 51 h Exp $.**.** SQ
7ec30 4c 69 74 65 20 70 72 6f 63 65 73 73 65 73 20 61 Lite processes a
7ec40 6c 6c 20 74 69 6d 65 73 20 61 6e 64 20 64 61 74 ll times and dat
7ec50 65 73 20 61 73 20 4a 75 6c 69 61 6e 20 44 61 79 es as Julian Day
7ec60 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 0a 2a numbers. The.*
7ec70 2a 20 64 61 74 65 73 20 61 6e 64 20 74 69 6d 65 * dates and time
7ec80 73 20 61 72 65 20 73 74 6f 72 65 64 20 61 73 20 s are stored as
7ec90 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 the number of da
7eca0 79 73 20 73 69 6e 63 65 20 6e 6f 6f 6e 0a 2a 2a ys since noon.**
7ecb0 20 69 6e 20 47 72 65 65 6e 77 69 63 68 20 6f 6e in Greenwich on
7ecc0 20 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20 34 37 November 24, 47
7ecd0 31 34 20 42 2e 43 2e 20 61 63 63 6f 72 64 69 6e 14 B.C. accordin
7ece0 67 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 g to the Gregori
7ecf0 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64 61 72 20 73 an.** calendar s
7ed00 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 31 39 ystem. .**.** 19
7ed10 37 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 70-01-01 00:00:0
7ed20 30 20 69 73 20 4a 44 20 32 34 34 30 35 38 37 2e 0 is JD 2440587.
7ed30 35 0a 2a 2a 20 32 30 30 30 2d 30 31 2d 30 31 20 5.** 2000-01-01
7ed40 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32 00:00:00 is JD 2
7ed50 34 35 31 35 34 34 2e 35 0a 2a 2a 0a 2a 2a 20 54 451544.5.**.** T
7ed60 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e his implemention
7ed70 20 72 65 71 75 69 72 65 73 20 79 65 61 72 73 20 requires years
7ed80 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 20 to be expressed
7ed90 61 73 20 61 20 34 2d 64 69 67 69 74 20 6e 75 6d as a 4-digit num
7eda0 62 65 72 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 ber.** which mea
7edb0 6e 73 20 74 68 61 74 20 6f 6e 6c 79 20 64 61 74 ns that only dat
7edc0 65 73 20 62 65 74 77 65 65 6e 20 30 30 30 30 2d es between 0000-
7edd0 30 31 2d 30 31 20 61 6e 64 20 39 39 39 39 2d 31 01-01 and 9999-1
7ede0 32 2d 33 31 20 63 61 6e 0a 2a 2a 20 62 65 20 72 2-31 can.** be r
7edf0 65 70 72 65 73 65 6e 74 65 64 2c 20 65 76 65 6e epresented, even
7ee00 20 74 68 6f 75 67 68 20 6a 75 6c 69 61 6e 20 64 though julian d
7ee10 61 79 20 6e 75 6d 62 65 72 73 20 61 6c 6c 6f 77 ay numbers allow
7ee20 20 61 20 6d 75 63 68 20 77 69 64 65 72 0a 2a 2a a much wider.**
7ee30 20 72 61 6e 67 65 20 6f 66 20 64 61 74 65 73 2e range of dates.
7ee40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47 72 65 67 6f .**.** The Grego
7ee50 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 73 79 rian calendar sy
7ee60 73 74 65 6d 20 69 73 20 75 73 65 64 20 66 6f 72 stem is used for
7ee70 20 61 6c 6c 20 64 61 74 65 73 20 61 6e 64 20 74 all dates and t
7ee80 69 6d 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 imes,.** even th
7ee90 6f 73 65 20 74 68 61 74 20 70 72 65 64 61 74 65 ose that predate
7eea0 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 the Gregorian c
7eeb0 61 6c 65 6e 64 61 72 2e 20 20 48 69 73 74 6f 72 alendar. Histor
7eec0 69 61 6e 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 ians usually.**
7eed0 75 73 65 20 74 68 65 20 4a 75 6c 69 61 6e 20 63 use the Julian c
7eee0 61 6c 65 6e 64 61 72 20 66 6f 72 20 64 61 74 65 alendar for date
7eef0 73 20 70 72 69 6f 72 20 74 6f 20 31 35 38 32 2d s prior to 1582-
7ef00 31 30 2d 31 35 20 61 6e 64 20 66 6f 72 20 73 6f 10-15 and for so
7ef10 6d 65 0a 2a 2a 20 64 61 74 65 73 20 61 66 74 65 me.** dates afte
7ef20 72 77 61 72 64 73 2c 20 64 65 70 65 6e 64 69 6e rwards, dependin
7ef30 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20 20 42 65 g on locale. Be
7ef40 77 61 72 65 20 6f 66 20 74 68 69 73 20 64 69 66 ware of this dif
7ef50 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 ference..**.** T
7ef60 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 6c he conversion al
7ef70 67 6f 72 69 74 68 6d 73 20 61 72 65 20 69 6d 70 gorithms are imp
7ef80 6c 65 6d 65 6e 74 65 64 20 62 61 73 65 64 20 6f lemented based o
7ef90 6e 20 64 65 73 63 72 69 70 74 69 6f 6e 73 0a 2a n descriptions.*
7efa0 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * in the followi
7efb0 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a 2a 2a 20 20 ng text:.**.**
7efc0 20 20 20 20 4a 65 61 6e 20 4d 65 65 75 73 0a 2a Jean Meeus.*
7efd0 2a 20 20 20 20 20 20 41 73 74 72 6f 6e 6f 6d 69 * Astronomi
7efe0 63 61 6c 20 41 6c 67 6f 72 69 74 68 6d 73 2c 20 cal Algorithms,
7eff0 32 6e 64 20 45 64 69 74 69 6f 6e 2c 20 31 39 39 2nd Edition, 199
7f000 38 0a 2a 2a 20 20 20 20 20 20 49 53 42 4d 20 30 8.** ISBM 0
7f010 2d 39 34 33 33 39 36 2d 36 31 2d 31 0a 2a 2a 20 -943396-61-1.**
7f020 20 20 20 20 20 57 69 6c 6c 6d 61 6e 6e 2d 42 65 Willmann-Be
7f030 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20 20 20 20 20 ll, Inc.**
7f040 52 69 63 68 6d 6f 6e 64 2c 20 56 69 72 67 69 6e Richmond, Virgin
7f050 69 61 20 28 55 53 41 29 0a 2a 2f 0a 23 69 6e 63 ia (USA).*/.#inc
7f060 6c 75 64 65 20 3c 63 74 79 70 65 2e 68 3e 0a 23 lude <ctype.h>.#
7f070 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e include <time.h>
7f080 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
7f090 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 _OMIT_DATETIME_F
7f0a0 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 72 UNCS../*.** On r
7f0b0 65 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c ecent Windows pl
7f0c0 61 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63 atforms, the loc
7f0d0 61 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74 altime_s() funct
7f0e0 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 ion is available
7f0f0 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74 .** as part of t
7f100 68 65 20 22 53 65 63 75 72 65 20 43 52 54 22 2e he "Secure CRT".
7f110 20 49 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c It is essential
7f120 6c 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f ly equivalent to
7f130 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 .** localtime_r
7f140 28 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e 64 () available und
7f150 65 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 6c er most POSIX pl
7f160 61 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 20 atforms, except
7f170 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 64 that the .** ord
7f180 65 72 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 er of the parame
7f190 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65 64 ters is reversed
7f1a0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 70 ..**.** See http
7f1b0 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f 66 ://msdn.microsof
7f1c0 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 72 t.com/en-us/libr
7f1d0 61 72 79 2f 61 34 34 32 78 33 79 65 28 56 53 2e ary/a442x3ye(VS.
7f1e0 38 30 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a 20 80).aspx..**.**
7f1f0 49 66 20 74 68 65 20 75 73 65 72 20 68 61 73 20 If the user has
7f200 6e 6f 74 20 69 6e 64 69 63 61 74 65 64 20 74 6f not indicated to
7f210 20 75 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 use localtime_r
7f220 28 29 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 5f () or localtime_
7f230 73 28 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c 20 s().** already,
7f240 63 68 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 56 check for an MSV
7f250 43 20 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e 6d C build environm
7f260 65 6e 74 20 74 68 61 74 20 70 72 6f 76 69 64 65 ent that provide
7f270 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f s .** localtime_
7f280 73 28 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 s()..*/.#if !def
7f290 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 ined(HAVE_LOCALT
7f2a0 49 4d 45 5f 52 29 20 26 26 20 21 64 65 66 69 6e IME_R) && !defin
7f2b0 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d ed(HAVE_LOCALTIM
7f2c0 45 5f 53 29 20 26 26 20 5c 0a 20 20 20 20 20 64 E_S) && \. d
7f2d0 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 efined(_MSC_VER)
7f2e0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 43 52 54 && defined(_CRT
7f2f0 5f 49 4e 53 45 43 55 52 45 5f 44 45 50 52 45 43 _INSECURE_DEPREC
7f300 41 54 45 29 0a 23 64 65 66 69 6e 65 20 48 41 56 ATE).#define HAV
7f310 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 0a E_LOCALTIME_S 1.
7f320 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 #endif../*.** A
7f330 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 68 6f structure for ho
7f340 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 64 lding a single d
7f350 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f ate and time..*/
7f360 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
7f370 44 61 74 65 54 69 6d 65 20 44 61 74 65 54 69 6d DateTime DateTim
7f380 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54 69 e;.struct DateTi
7f390 6d 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 me {. sqlite3_i
7f3a0 6e 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68 65 nt64 iJD; /* The
7f3b0 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 julian day numb
7f3c0 65 72 20 74 69 6d 65 73 20 38 36 34 30 30 30 30 er times 8640000
7f3d0 30 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 0 */. int Y, M,
7f3e0 20 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 65 61 D; /* Yea
7f3f0 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 61 r, month, and da
7f400 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d 3b y */. int h, m;
7f410 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 75 /* Hou
7f420 72 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a 2f r and minutes */
7f430 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 20 . int tz;
7f440 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f 6e /* Timezon
7f450 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e 75 e offset in minu
7f460 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 tes */. double
7f470 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 s; /* S
7f480 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 72 econds */. char
7f490 20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 2f validYMD; /
7f4a0 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 59 2c * True (1) if Y,
7f4b0 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a 2f M,D are valid */
7f4c0 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d 53 . char validHMS
7f4d0 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 ; /* True (1
7f4e0 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65 20 76 ) if h,m,s are v
7f4f0 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 alid */. char v
7f500 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a 20 alidJD; /*
7f510 54 72 75 65 20 28 31 29 20 69 66 20 69 4a 44 20 True (1) if iJD
7f520 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 is valid */. ch
7f530 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20 20 20 ar validTZ;
7f540 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 /* True (1) if
7f550 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 7d tz is valid */.}
7f560 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 ;.../*.** Conver
7f570 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e 65 t zDate into one
7f580 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 72 or more integer
7f590 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 s. Additional a
7f5a0 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d 65 rguments.** come
7f5b0 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 20 in groups of 5
7f5c0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
7f5d0 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20 20 20 * N
7f5e0 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73 number of digits
7f5f0 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72 0a in the integer.
7f600 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20 20 ** min
7f610 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 minimum allowed
7f620 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e value of the in
7f630 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d teger.** m
7f640 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 61 ax maximum a
7f650 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 llowed value of
7f660 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 the integer.**
7f670 20 20 20 20 20 6e 65 78 74 43 20 20 20 66 69 72 nextC fir
7f680 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74 st character aft
7f690 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a er the integer.*
7f6a0 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20 20 20 * pVal
7f6b0 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 where to write t
7f6c0 68 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c 75 he integers valu
7f6d0 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 e..**.** Convers
7f6e0 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 6e ions continue un
7f6f0 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 78 til one with nex
7f700 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e 74 tC==0 is encount
7f710 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 6e ered..** The fun
7f720 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
7f730 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 63 e number of succ
7f740 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 6f essful conversio
7f750 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ns..*/.static in
7f760 74 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e 73 t getDigits(cons
7f770 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 2e t char *zDate, .
7f780 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
7f790 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 20 p;. int val;.
7f7a0 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 6e int N;. int min
7f7b0 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 69 ;. int max;. i
7f7c0 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 20 nt nextC;. int
7f7d0 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e 74 *pVal;. int cnt
7f7e0 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 = 0;. va_start
7f7f0 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 64 (ap, zDate);. d
7f800 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 72 o{. N = va_ar
7f810 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 g(ap, int);.
7f820 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c min = va_arg(ap,
7f830 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 3d int);. max =
7f840 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 va_arg(ap, int)
7f850 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76 61 ;. nextC = va
7f860 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 _arg(ap, int);.
7f870 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 67 pVal = va_arg
7f880 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 (ap, int*);.
7f890 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 val = 0;. whi
7f8a0 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 le( N-- ){.
7f8b0 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a 28 if( !isdigit(*(
7f8c0 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 0a 20 20 u8*)zDate) ){.
7f8d0 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 goto end_g
7f8e0 65 74 44 69 67 69 74 73 3b 0a 20 20 20 20 20 20 etDigits;.
7f8f0 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d 20 76 61 }. val = va
7f900 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20 l*10 + *zDate -
7f910 27 30 27 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 '0';. zDate
7f920 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ++;. }. if
7f930 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 76 61 6c ( val<min || val
7f940 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74 43 21 3d >max || (nextC!=
7f950 30 20 26 26 20 6e 65 78 74 43 21 3d 2a 7a 44 61 0 && nextC!=*zDa
7f960 74 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 te) ){. got
7f970 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b o end_getDigits;
7f980 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 56 61 6c . }. *pVal
7f990 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a 44 61 74 = val;. zDat
7f9a0 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b 2b 3b 0a e++;. cnt++;.
7f9b0 20 20 7d 77 68 69 6c 65 28 20 6e 65 78 74 43 20 }while( nextC
7f9c0 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67 69 74 73 );.end_getDigits
7f9d0 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a :. va_end(ap);.
7f9e0 20 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a 7d 0a return cnt;.}.
7f9f0 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 65 78 74 ./*.** Read text
7fa00 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 20 63 6f from z[] and co
7fa10 6e 76 65 72 74 20 69 6e 74 6f 20 61 20 66 6c 6f nvert into a flo
7fa20 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 ating point numb
7fa30 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 er. Return.** t
7fa40 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 he number of dig
7fa50 69 74 73 20 63 6f 6e 76 65 72 74 65 64 2e 0a 2a its converted..*
7fa60 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 6c /.#define getVal
7fa70 75 65 20 73 71 6c 69 74 65 33 41 74 6f 46 0a 0a ue sqlite3AtoF..
7fa80 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 74 69 /*.** Parse a ti
7fa90 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73 69 6f 6e mezone extension
7faa0 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 61 on the end of a
7fab0 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a 2a 20 54 date-time..** T
7fac0 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 he extension is
7fad0 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a of the form:.**.
7fae0 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f 2d 29 48 ** (+/-)H
7faf0 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 20 74 68 H:MM.**.** Or th
7fb00 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61 74 69 6f e "zulu" notatio
7fb10 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 n:.**.**
7fb20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 Z.**.** If the p
7fb30 61 72 73 65 20 69 73 20 73 75 63 63 65 73 73 66 arse is successf
7fb40 75 6c 2c 20 77 72 69 74 65 20 74 68 65 20 6e 75 ul, write the nu
7fb50 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74 65 73 0a mber of minutes.
7fb60 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20 69 6e 20 ** of change in
7fb70 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74 75 72 6e p->tz and return
7fb80 20 30 2e 20 20 49 66 20 61 20 70 61 72 73 65 72 0. If a parser
7fb90 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a error occurs,.*
7fba0 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 * return non-zer
7fbb0 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 73 73 69 o..**.** A missi
7fbc0 6e 67 20 73 70 65 63 69 66 69 65 72 20 69 73 20 ng specifier is
7fbd0 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 20 61 not considered a
7fbe0 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 n error..*/.stat
7fbf0 69 63 20 69 6e 74 20 70 61 72 73 65 54 69 6d 65 ic int parseTime
7fc00 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68 61 72 20 zone(const char
7fc10 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 *zDate, DateTime
7fc20 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 67 6e 20 *p){. int sgn
7fc30 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 72 2c 20 = 0;. int nHr,
7fc40 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 nMn;. int c;.
7fc50 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a while( isspace(*
7fc60 28 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 20 7a (u8*)zDate) ){ z
7fc70 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 70 2d 3e 74 Date++; }. p->t
7fc80 7a 20 3d 20 30 3b 0a 20 20 63 20 3d 20 2a 7a 44 z = 0;. c = *zD
7fc90 61 74 65 3b 0a 20 20 69 66 28 20 63 3d 3d 27 2d ate;. if( c=='-
7fca0 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2d ' ){. sgn = -
7fcb0 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63 1;. }else if( c
7fcc0 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 73 67 6e =='+' ){. sgn
7fcd0 20 3d 20 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 69 = +1;. }else i
7fce0 66 28 20 63 3d 3d 27 5a 27 20 7c 7c 20 63 3d 3d f( c=='Z' || c==
7fcf0 27 7a 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 'z' ){. zDate
7fd00 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 7a 75 6c ++;. goto zul
7fd10 75 5f 74 69 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b u_time;. }else{
7fd20 0a 20 20 20 20 72 65 74 75 72 6e 20 63 21 3d 30 . return c!=0
7fd30 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 2b 2b 3b ;. }. zDate++;
7fd40 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 74 73 . if( getDigits
7fd50 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 31 34 (zDate, 2, 0, 14
7fd60 2c 20 27 3a 27 2c 20 26 6e 48 72 2c 20 32 2c 20 , ':', &nHr, 2,
7fd70 30 2c 20 35 39 2c 20 30 2c 20 26 6e 4d 6e 29 21 0, 59, 0, &nMn)!
7fd80 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =2 ){. return
7fd90 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 1;. }. zDate
7fda0 2b 3d 20 35 3b 0a 20 20 70 2d 3e 74 7a 20 3d 20 += 5;. p->tz =
7fdb0 73 67 6e 2a 28 6e 4d 6e 20 2b 20 6e 48 72 2a 36 sgn*(nMn + nHr*6
7fdc0 30 29 3b 0a 7a 75 6c 75 5f 74 69 6d 65 3a 0a 20 0);.zulu_time:.
7fdd0 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 while( isspace(
7fde0 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 20 *(u8*)zDate) ){
7fdf0 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 72 65 74 zDate++; }. ret
7fe00 75 72 6e 20 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d urn *zDate!=0;.}
7fe10 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 69 ../*.** Parse ti
7fe20 6d 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 mes of the form
7fe30 48 48 3a 4d 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53 HH:MM or HH:MM:S
7fe40 53 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 S or HH:MM:SS.FF
7fe50 46 46 2e 0a 2a 2a 20 54 68 65 20 48 48 2c 20 4d FF..** The HH, M
7fe60 4d 2c 20 61 6e 64 20 53 53 20 6d 75 73 74 20 65 M, and SS must e
7fe70 61 63 68 20 62 65 20 65 78 61 63 74 6c 79 20 32 ach be exactly 2
7fe80 20 64 69 67 69 74 73 2e 20 20 54 68 65 0a 2a 2a digits. The.**
7fe90 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f fractional seco
7fea0 6e 64 73 20 46 46 46 46 20 63 61 6e 20 62 65 20 nds FFFF can be
7feb0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 one or more digi
7fec0 74 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ts..**.** Return
7fed0 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61 1 if there is a
7fee0 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 61 parsing error a
7fef0 6e 64 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e nd 0 on success.
7ff00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
7ff10 61 72 73 65 48 68 4d 6d 53 73 28 63 6f 6e 73 74 arseHhMmSs(const
7ff20 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 char *zDate, Da
7ff30 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e teTime *p){. in
7ff40 74 20 68 2c 20 6d 2c 20 73 3b 0a 20 20 64 6f 75 t h, m, s;. dou
7ff50 62 6c 65 20 6d 73 20 3d 20 30 2e 30 3b 0a 20 20 ble ms = 0.0;.
7ff60 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 if( getDigits(zD
7ff70 61 74 65 2c 20 32 2c 20 30 2c 20 32 34 2c 20 27 ate, 2, 0, 24, '
7ff80 3a 27 2c 20 26 68 2c 20 32 2c 20 30 2c 20 35 39 :', &h, 2, 0, 59
7ff90 2c 20 30 2c 20 26 6d 29 21 3d 32 20 29 7b 0a 20 , 0, &m)!=2 ){.
7ffa0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
7ffb0 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 . zDate += 5;.
7ffc0 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 3a 27 if( *zDate==':'
7ffd0 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b ){. zDate++;
7ffe0 0a 20 20 20 20 69 66 28 20 67 65 74 44 69 67 69 . if( getDigi
7fff0 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 ts(zDate, 2, 0,
80000 35 39 2c 20 30 2c 20 26 73 29 21 3d 31 20 29 7b 59, 0, &s)!=1 ){
80010 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
80020 0a 20 20 20 20 7d 0a 20 20 20 20 7a 44 61 74 65 . }. zDate
80030 20 2b 3d 20 32 3b 0a 20 20 20 20 69 66 28 20 2a += 2;. if( *
80040 7a 44 61 74 65 3d 3d 27 2e 27 20 26 26 20 69 73 zDate=='.' && is
80050 64 69 67 69 74 28 28 75 38 29 7a 44 61 74 65 5b digit((u8)zDate[
80060 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 1]) ){. dou
80070 62 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 ble rScale = 1.0
80080 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b ;. zDate++;
80090 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 73 . while( is
800a0 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 44 61 74 digit(*(u8*)zDat
800b0 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73 e) ){. ms
800c0 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 = ms*10.0 + *zD
800d0 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 ate - '0';.
800e0 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e rScale *= 10.
800f0 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65 0;. zDate
80100 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ++;. }.
80110 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a ms /= rScale;.
80120 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
80130 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 s = 0;. }.
80140 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a p->validJD = 0;.
80150 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 p->validHMS =
80160 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 1;. p->h = h;.
80170 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e p->m = m;. p->
80180 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66 s = s + ms;. if
80190 28 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 ( parseTimezone(
801a0 7a 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75 zDate, p) ) retu
801b0 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 rn 1;. p->valid
801c0 54 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 3f TZ = (p->tz!=0)?
801d0 31 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 1:0;. return 0;
801e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
801f0 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 t from YYYY-MM-D
80200 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 D HH:MM:SS to ju
80210 6c 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c lian day. We al
80220 77 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74 ways assume.** t
80230 68 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d hat the YYYY-MM-
80240 44 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20 DD is according
80250 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e to the Gregorian
80260 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a calendar..**.**
80270 20 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 Reference: Mee
80280 75 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74 us page 61.*/.st
80290 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 atic void comput
802a0 65 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 eJD(DateTime *p)
802b0 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c {. int Y, M, D,
802c0 20 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a A, B, X1, X2;..
802d0 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44 if( p->validJD
802e0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 ) return;. if(
802f0 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a p->validYMD ){.
80300 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 Y = p->Y;.
80310 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 M = p->M;.
80320 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 D = p->D;. }els
80330 65 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b e{. Y = 2000;
80340 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73 /* If no YMD s
80350 70 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65 pecified, assume
80360 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 2000-Jan-01 */.
80370 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44 M = 1;. D
80380 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 1;. }. if(
80390 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b M<=2 ){. Y--;
803a0 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 . M += 12;.
803b0 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20 }. A = Y/100;.
803c0 20 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f B = 2 - A + (A/
803d0 34 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 35 4);. X1 = 36525
803e0 2a 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 20 *(Y+4716)/100;.
803f0 20 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d 2b X2 = 306001*(M+
80400 31 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e 69 1)/10000;. p->i
80410 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e JD = (sqlite3_in
80420 74 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b 20 t64)((X1 + X2 +
80430 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 29 D + B - 1524.5 )
80440 20 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 * 86400000);.
80450 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a p->validJD = 1;.
80460 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d if( p->validHM
80470 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 S ){. p->iJD
80480 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 20 += p->h*3600000
80490 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 28 + p->m*60000 + (
804a0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 70 sqlite3_int64)(p
804b0 2d 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 69 ->s*1000);. i
804c0 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b f( p->validTZ ){
804d0 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d . p->iJD -=
804e0 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 20 p->tz*60000;.
804f0 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 p->validYMD
80500 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 = 0;. p->va
80510 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 20 lidHMS = 0;.
80520 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 p->validTZ = 0
80530 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
80540 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65 73 *.** Parse dates
80550 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a of the form.**.
80560 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 ** YYYY-MM-D
80570 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a D HH:MM:SS.FFF.*
80580 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 * YYYY-MM-DD
80590 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 HH:MM:SS.**
805a0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d YYYY-MM-DD HH:M
805b0 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d M.** YYYY-MM
805c0 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 -DD.**.** Write
805d0 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 the result into
805e0 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 the DateTime str
805f0 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 ucture and retur
80600 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 n 0.** on succes
80610 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 20 69 s and 1 if the i
80620 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73 20 6e nput string is n
80630 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 ot a well-formed
80640 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 .** date..*/.sta
80650 74 69 63 20 69 6e 74 20 70 61 72 73 65 59 79 79 tic int parseYyy
80660 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72 yMmDd(const char
80670 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d *zDate, DateTim
80680 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 e *p){. int Y,
80690 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 M, D, neg;.. if
806a0 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 ( zDate[0]=='-'
806b0 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a ){. zDate++;.
806c0 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 7d neg = 1;. }
806d0 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 else{. neg =
806e0 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74 0;. }. if( get
806f0 44 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c 30 Digits(zDate,4,0
80700 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 ,9999,'-',&Y,2,1
80710 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 ,12,'-',&M,2,1,3
80720 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 20 1,0,&D)!=3 ){.
80730 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
80740 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a 20 zDate += 10;.
80750 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 while( isspace(
80760 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 7c 7c 20 *(u8*)zDate) ||
80770 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65 'T'==*(u8*)zDate
80780 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 ){ zDate++; }.
80790 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 if( parseHhMmSs
807a0 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b (zDate, p)==0 ){
807b0 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 74 . /* We got t
807c0 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c he time */. }el
807d0 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30 se if( *zDate==0
807e0 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 ){. p->valid
807f0 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 HMS = 0;. }else
80800 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a {. return 1;.
80810 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 }. p->validJD
80820 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 = 0;. p->valid
80830 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 YMD = 1;. p->Y
80840 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b 0a = neg ? -Y : Y;.
80850 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d p->M = M;. p-
80860 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20 70 2d >D = D;. if( p-
80870 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 >validTZ ){.
80880 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
80890 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
808a0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 ./*.** Set the t
808b0 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 72 65 ime to the curre
808c0 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 65 64 nt time reported
808d0 20 62 79 20 74 68 65 20 56 46 53 0a 2a 2f 0a 73 by the VFS.*/.s
808e0 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 44 61 tatic void setDa
808f0 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 teTimeToCurrent(
80900 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
80910 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 65 54 69 *context, DateTi
80920 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 62 6c 65 me *p){. double
80930 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 r;. sqlite3 *d
80940 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 b = sqlite3_cont
80950 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f ext_db_handle(co
80960 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 ntext);. sqlite
80970 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 3OsCurrentTime(d
80980 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a 20 20 b->pVfs, &r);.
80990 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 p->iJD = (sqlite
809a0 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30 3_int64)(r*86400
809b0 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 000.0 + 0.5);.
809c0 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a p->validJD = 1;.
809d0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 }../*.** Attempt
809e0 20 74 6f 20 70 61 72 73 65 20 74 68 65 20 67 69 to parse the gi
809f0 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 ven string into
80a00 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d a Julian Day Num
80a10 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 ber. Return.**
80a20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 the number of er
80a30 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rors..**.** The
80a40 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 63 following are ac
80a50 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73 20 66 ceptable forms f
80a60 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 or the input str
80a70 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ing:.**.**
80a80 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d YYYY-MM-DD HH:MM
80a90 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a 4d :SS.FFF +/-HH:M
80aa0 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44 2e 44 M.** DDDD.D
80ab0 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a 2a D .** now.*
80ac0 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69 72 73 *.** In the firs
80ad0 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 t form, the +/-H
80ae0 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73 20 6f H:MM is always o
80af0 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66 72 ptional. The fr
80b00 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f actional.** seco
80b10 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20 28 74 nds extension (t
80b20 68 65 20 22 2e 46 46 46 22 29 20 69 73 20 6f 70 he ".FFF") is op
80b30 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65 63 tional. The sec
80b40 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 onds portion.**
80b50 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73 20 6f (":SS.FFF") is o
80b60 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65 61 72 ption. The year
80b70 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20 62 65 and date can be
80b80 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 omitted as long
80b90 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73 20 .** as there is
80ba0 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e 20 20 a time string.
80bb0 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e 67 20 The time string
80bc0 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 61 can be omitted a
80bd0 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 s long.** as the
80be0 72 65 20 69 73 20 61 20 79 65 61 72 20 61 6e 64 re is a year and
80bf0 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 date..*/.static
80c00 20 69 6e 74 20 70 61 72 73 65 44 61 74 65 4f 72 int parseDateOr
80c10 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f Time(. sqlite3_
80c20 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
80c30 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
80c40 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 74 65 54 *zDate, . DateT
80c50 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 66 28 20 ime *p.){. if(
80c60 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 7a 44 parseYyyyMmDd(zD
80c70 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 20 20 ate,p)==0 ){.
80c80 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c return 0;. }el
80c90 73 65 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d se if( parseHhMm
80ca0 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 Ss(zDate, p)==0
80cb0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
80cc0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c . }else if( sql
80cd0 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74 ite3StrICmp(zDat
80ce0 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a 20 20 e,"now")==0){.
80cf0 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43 setDateTimeToC
80d00 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c 20 urrent(context,
80d10 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 p);. return 0
80d20 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 ;. }else if( sq
80d30 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 7a 44 lite3IsNumber(zD
80d40 61 74 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 ate, 0, SQLITE_U
80d50 54 46 38 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 TF8) ){. doub
80d60 6c 65 20 72 3b 0a 20 20 20 20 67 65 74 56 61 6c le r;. getVal
80d70 75 65 28 7a 44 61 74 65 2c 20 26 72 29 3b 0a 20 ue(zDate, &r);.
80d80 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c p->iJD = (sql
80d90 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 ite3_int64)(r*86
80da0 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 400000.0 + 0.5);
80db0 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 . p->validJD
80dc0 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
80dd0 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
80de0 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 1;.}../*.** Comp
80df0 75 74 65 20 74 68 65 20 59 65 61 72 2c 20 4d 6f ute the Year, Mo
80e00 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 66 72 6f nth, and Day fro
80e10 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 m the julian day
80e20 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 number..*/.stat
80e30 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 ic void computeY
80e40 4d 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b MD(DateTime *p){
80e50 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 . int Z, A, B,
80e60 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 C, D, E, X1;. i
80e70 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 f( p->validYMD )
80e80 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 return;. if( !
80e90 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 p->validJD ){.
80ea0 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 p->Y = 2000;.
80eb0 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 p->M = 1;.
80ec0 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c p->D = 1;. }el
80ed0 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 se{. Z = (int
80ee0 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 )((p->iJD + 4320
80ef0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0000)/86400000);
80f00 0a 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28 28 . A = (int)((
80f10 5a 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f Z - 1867216.25)/
80f20 33 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 20 41 36524.25);. A
80f30 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 = Z + 1 + A - (
80f40 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 A/4);. B = A
80f50 2b 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 + 1524;. C =
80f60 28 69 6e 74 29 28 28 42 20 2d 20 31 32 32 2e 31 (int)((B - 122.1
80f70 29 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 20 44 )/365.25);. D
80f80 20 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 30 30 = (36525*C)/100
80f90 3b 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 29 28 ;. E = (int)(
80fa0 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a (B-D)/30.6001);.
80fb0 20 20 20 20 58 31 20 3d 20 28 69 6e 74 29 28 33 X1 = (int)(3
80fc0 30 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 20 70 0.6001*E);. p
80fd0 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 ->D = B - D - X1
80fe0 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 ;. p->M = E<1
80ff0 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 4 ? E-1 : E-13;.
81000 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e p->Y = p->M>
81010 32 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20 43 2 ? C - 4716 : C
81020 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 - 4715;. }. p
81030 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a ->validYMD = 1;.
81040 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 }../*.** Compute
81050 20 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 the Hour, Minut
81060 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 e, and Seconds f
81070 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 rom the julian d
81080 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 ay number..*/.st
81090 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 atic void comput
810a0 65 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 eHMS(DateTime *p
810b0 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 66 ){. int s;. if
810c0 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20 ( p->validHMS )
810d0 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74 return;. comput
810e0 65 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 28 69 eJD(p);. s = (i
810f0 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 nt)((p->iJD + 43
81100 32 30 30 30 30 30 29 20 25 20 38 36 34 30 30 30 200000) % 864000
81110 30 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 00);. p->s = s/
81120 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69 1000.0;. s = (i
81130 6e 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 nt)p->s;. p->s
81140 2d 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 -= s;. p->h = s
81150 2f 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d /3600;. s -= p-
81160 3e 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 >h*3600;. p->m
81170 3d 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b = s/60;. p->s +
81180 3d 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 = s - p->m*60;.
81190 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 p->validHMS = 1
811a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 ;.}../*.** Compu
811b0 74 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 te both YMD and
811c0 48 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f HMS.*/.static vo
811d0 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d id computeYMD_HM
811e0 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a S(DateTime *p){.
811f0 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b computeYMD(p);
81200 0a 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 . computeHMS(p)
81210 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 ;.}../*.** Clear
81220 20 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 the YMD and HMS
81230 20 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 and the TZ.*/.s
81240 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 tatic void clear
81250 59 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 YMD_HMS_TZ(DateT
81260 69 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 ime *p){. p->va
81270 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d lidYMD = 0;. p-
81280 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 >validHMS = 0;.
81290 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b p->validTZ = 0;
812a0 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
812b0 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d TE_OMIT_LOCALTIM
812c0 45 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 E./*.** Compute
812d0 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 28 the difference (
812e0 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 in milliseconds)
812f0 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 .** between loca
81300 6c 74 69 6d 65 20 61 6e 64 20 55 54 43 20 28 61 ltime and UTC (a
81310 2e 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f .k.a. GMT).** fo
81320 72 20 74 68 65 20 74 69 6d 65 20 76 61 6c 75 65 r the time value
81330 20 70 20 77 68 65 72 65 20 70 20 69 73 20 69 6e p where p is in
81340 20 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 UTC..*/.static
81350 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f sqlite3_int64 lo
81360 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 44 61 caltimeOffset(Da
81370 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 teTime *p){. Da
81380 74 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 teTime x, y;. t
81390 69 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a ime_t t;. x = *
813a0 70 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f p;. computeYMD_
813b0 48 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20 78 HMS(&x);. if( x
813c0 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d .Y<1971 || x.Y>=
813d0 32 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 2038 ){. x.Y
813e0 3d 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 = 2000;. x.M
813f0 3d 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 = 1;. x.D = 1
81400 3b 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 ;. x.h = 0;.
81410 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 x.m = 0;.
81420 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 x.s = 0.0;. } e
81430 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 lse {. int s
81440 3d 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 30 2e = (int)(x.s + 0.
81450 35 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 5);. x.s = s;
81460 0a 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b . }. x.tz = 0;
81470 0a 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 . x.validJD = 0
81480 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 ;. computeJD(&x
81490 29 3b 0a 20 20 74 20 3d 20 78 2e 69 4a 44 2f 31 );. t = x.iJD/1
814a0 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a 28 000 - 21086676*(
814b0 69 36 34 29 31 30 30 30 30 3b 0a 23 69 66 64 65 i64)10000;.#ifde
814c0 66 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 f HAVE_LOCALTIME
814d0 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 _R. {. struc
814e0 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 t tm sLocal;.
814f0 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 26 74 2c localtime_r(&t,
81500 20 26 73 4c 6f 63 61 6c 29 3b 0a 20 20 20 20 79 &sLocal);. y
81510 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 .Y = sLocal.tm_y
81520 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 ear + 1900;.
81530 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.M = sLocal.tm_
81540 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 mon + 1;. y.D
81550 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 = sLocal.tm_mda
81560 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f y;. y.h = sLo
81570 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 cal.tm_hour;.
81580 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.m = sLocal.tm
81590 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 _min;. y.s =
815a0 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 sLocal.tm_sec;.
815b0 20 7d 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 }.#elif defined
815c0 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f (HAVE_LOCALTIME_
815d0 53 29 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 S). {. struc
815e0 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 t tm sLocal;.
815f0 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 26 73 4c localtime_s(&sL
81600 6f 63 61 6c 2c 20 26 74 29 3b 0a 20 20 20 20 79 ocal, &t);. y
81610 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 .Y = sLocal.tm_y
81620 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 ear + 1900;.
81630 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.M = sLocal.tm_
81640 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 mon + 1;. y.D
81650 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 = sLocal.tm_mda
81660 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f y;. y.h = sLo
81670 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 cal.tm_hour;.
81680 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.m = sLocal.tm
81690 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 _min;. y.s =
816a0 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 sLocal.tm_sec;.
816b0 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 }.#else. {.
816c0 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b struct tm *pTm;
816d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
816e0 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 ex_enter(sqlite3
816f0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
81700 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
81710 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54 6d ASTER));. pTm
81720 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26 74 29 = localtime(&t)
81730 3b 0a 20 20 20 20 79 2e 59 20 3d 20 70 54 6d 2d ;. y.Y = pTm-
81740 3e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b >tm_year + 1900;
81750 0a 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d 2d 3e . y.M = pTm->
81760 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 tm_mon + 1;.
81770 79 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 64 y.D = pTm->tm_md
81780 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 70 54 ay;. y.h = pT
81790 6d 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 m->tm_hour;.
817a0 79 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 69 y.m = pTm->tm_mi
817b0 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70 54 6d n;. y.s = pTm
817c0 2d 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20 73 71 ->tm_sec;. sq
817d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
817e0 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c e(sqlite3MutexAl
817f0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
81800 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 _STATIC_MASTER))
81810 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 ;. }.#endif. y
81820 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 .validYMD = 1;.
81830 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b y.validHMS = 1;
81840 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 . y.validJD = 0
81850 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20 3d 20 ;. y.validTZ =
81860 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 0;. computeJD(&
81870 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e 69 y);. return y.i
81880 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 65 JD - x.iJD;.}.#e
81890 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
818a0 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f MIT_LOCALTIME */
818b0 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 ../*.** Process
818c0 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 61 20 a modifier to a
818d0 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e date-time stamp.
818e0 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 73 20 The modifiers
818f0 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 are.** as follow
81900 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e s:.**.** NNN
81910 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e days.** NNN
81920 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e hours.** NN
81930 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20 N minutes.**
81940 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64 NNN.NNNN second
81950 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e s.** NNN mon
81960 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79 ths.** NNN y
81970 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 61 72 ears.** star
81980 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 t of month.**
81990 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72 0a start of year.
819a0 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 ** start of
819b0 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72 week.** star
819c0 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20 20 t of day.**
819d0 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20 weekday N.**
819e0 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20 unixepoch.**
819f0 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20 localtime.**
81a00 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74 utc.**.** Ret
81a10 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 urn 0 on success
81a20 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 20 and 1 if there
81a30 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 is any kind of e
81a40 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rror..*/.static
81a50 69 6e 74 20 70 61 72 73 65 4d 6f 64 69 66 69 65 int parseModifie
81a60 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d r(const char *zM
81a70 6f 64 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 od, DateTime *p)
81a80 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a {. int rc = 1;.
81a90 20 20 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c int n;. doubl
81aa0 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 e r;. char *z,
81ab0 7a 42 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20 zBuf[30];. z =
81ac0 7a 42 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b zBuf;. for(n=0;
81ad0 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a 42 75 n<ArraySize(zBu
81ae0 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d 3b f)-1 && zMod[n];
81af0 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d 20 n++){. z[n]
81b00 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 55 = (char)sqlite3U
81b10 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 pperToLower[(u8)
81b20 7a 4d 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 zMod[n]];. }.
81b30 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69 74 z[n] = 0;. swit
81b40 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e ch( z[0] ){.#ifn
81b50 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
81b60 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 63 61 LOCALTIME. ca
81b70 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20 se 'l': {.
81b80 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a /* localtime.
81b90 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
81ba0 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 63 * Assuming the c
81bb0 75 72 72 65 6e 74 20 74 69 6d 65 20 76 61 6c 75 urrent time valu
81bc0 65 20 69 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e e is UTC (a.k.a.
81bd0 20 47 4d 54 29 2c 20 73 68 69 66 74 20 69 74 20 GMT), shift it
81be0 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f 77 to. ** show
81bf0 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20 local time..
81c00 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
81c10 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c strcmp(z, "local
81c20 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 time")==0 ){.
81c30 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 computeJD(p
81c40 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a );. p->iJ
81c50 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 D += localtimeOf
81c60 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 fset(p);.
81c70 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a clearYMD_HMS_TZ
81c80 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 (p);. rc
81c90 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
81ca0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
81cb0 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 #endif. case
81cc0 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 'u': {. /*.
81cd0 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e 69 78 ** unix
81ce0 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a 0a 20 epoch. **.
81cf0 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20 74 68 ** Treat th
81d00 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
81d10 6f 66 20 70 2d 3e 69 4a 44 20 61 73 20 74 68 65 of p->iJD as the
81d20 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 number of.
81d30 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 ** seconds sinc
81d40 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 72 74 e 1970. Convert
81d50 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c 69 61 to a real julia
81d60 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 20 20 n day number..
81d70 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
81d80 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e 69 78 strcmp(z, "unix
81d90 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20 70 2d epoch")==0 && p-
81da0 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 >validJD ){.
81db0 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 70 2d 3e p->iJD = p->
81dc0 69 4a 44 2f 38 36 34 30 30 20 2b 20 32 31 30 38 iJD/86400 + 2108
81dd0 36 36 37 36 2a 28 69 36 34 29 31 30 30 30 30 30 6676*(i64)100000
81de0 30 30 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 00;. clea
81df0 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a rYMD_HMS_TZ(p);.
81e00 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a rc = 0;.
81e10 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 }.#ifndef
81e20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 SQLITE_OMIT_LOCA
81e30 4c 54 49 4d 45 0a 20 20 20 20 20 20 65 6c 73 65 LTIME. else
81e40 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 if( strcmp(z, "
81e50 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 utc")==0 ){.
81e60 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 sqlite3_int6
81e70 34 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 4 c1;. co
81e80 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 mputeJD(p);.
81e90 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c 74 69 c1 = localti
81ea0 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 meOffset(p);.
81eb0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 63 p->iJD -= c
81ec0 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 1;. clear
81ed0 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 YMD_HMS_TZ(p);.
81ee0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d p->iJD +=
81ef0 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d 65 4f c1 - localtimeO
81f00 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 ffset(p);.
81f10 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 rc = 0;.
81f20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 62 }.#endif. b
81f30 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
81f40 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20 20 20 case 'w': {.
81f50 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 /*. **
81f60 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20 20 20 weekday N.
81f70 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76 **. ** Mov
81f80 65 20 74 68 65 20 64 61 74 65 20 74 6f 20 74 68 e the date to th
81f90 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e 20 74 e same time on t
81fa0 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72 65 6e he next occurren
81fb0 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 77 ce of. ** w
81fc0 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65 20 30 eekday N where 0
81fd0 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d 6f 6e ==Sunday, 1==Mon
81fe0 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 day, and so fort
81ff0 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20 20 20 h. If the.
82000 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c 72 65 ** date is alre
82010 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70 72 6f ady on the appro
82020 70 72 69 61 74 65 20 77 65 65 6b 64 61 79 2c 20 priate weekday,
82030 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e this is a no-op.
82040 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
82050 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 22 if( strncmp(z, "
82060 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d 3d 30 weekday ", 8)==0
82070 20 26 26 20 67 65 74 56 61 6c 75 65 28 26 7a 5b && getValue(&z[
82080 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20 20 20 8],&r)>0.
82090 20 20 20 20 20 20 20 20 20 20 26 26 20 28 6e 3d && (n=
820a0 28 69 6e 74 29 72 29 3d 3d 72 20 26 26 20 6e 3e (int)r)==r && n>
820b0 3d 30 20 26 26 20 72 3c 37 20 29 7b 0a 20 20 20 =0 && r<7 ){.
820c0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 sqlite3_int
820d0 36 34 20 5a 3b 0a 20 20 20 20 20 20 20 20 63 6f 64 Z;. co
820e0 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b mputeYMD_HMS(p);
820f0 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 . p->vali
82100 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 dTZ = 0;.
82110 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b p->validJD = 0;
82120 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
82130 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 5a JD(p);. Z
82140 20 3d 20 28 28 70 2d 3e 69 4a 44 20 2b 20 31 32 = ((p->iJD + 12
82150 39 36 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 9600000)/8640000
82160 30 29 20 25 20 37 3b 0a 20 20 20 20 20 20 20 20 0) % 7;.
82170 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d 3d 20 37 if( Z>n ) Z -= 7
82180 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 ;. p->iJD
82190 20 2b 3d 20 28 6e 20 2d 20 5a 29 2a 38 36 34 30 += (n - Z)*8640
821a0 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63 6c 0000;. cl
821b0 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 earYMD_HMS_TZ(p)
821c0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 ;. rc = 0
821d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
821e0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
821f0 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 20 20 case 's': {.
82200 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 20 /*. **
82210 20 20 73 74 61 72 74 20 6f 66 20 54 54 54 54 54 start of TTTTT
82220 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
82230 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74 65 ** Move the date
82240 20 62 61 63 6b 77 61 72 64 73 20 74 6f 20 74 68 backwards to th
82250 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
82260 68 65 20 63 75 72 72 65 6e 74 20 64 61 79 2c 0a he current day,.
82270 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f 6e 74 ** or mont
82280 68 20 6f 72 20 79 65 61 72 2e 0a 20 20 20 20 20 h or year..
82290 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 */. if( st
822a0 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61 72 74 20 rncmp(z, "start
822b0 6f 66 20 22 2c 20 39 29 21 3d 30 20 29 20 62 72 of ", 9)!=0 ) br
822c0 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 eak;. z +=
822d0 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65 9;. compute
822e0 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 70 2d YMD(p);. p-
822f0 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 >validHMS = 1;.
82300 20 20 20 20 20 70 2d 3e 68 20 3d 20 70 2d 3e 6d p->h = p->m
82310 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 73 = 0;. p->s
82320 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20 70 2d = 0.0;. p-
82330 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 >validTZ = 0;.
82340 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d p->validJD =
82350 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 73 74 0;. if( st
82360 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d rcmp(z,"month")=
82370 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d =0 ){. p-
82380 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 >D = 1;.
82390 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 rc = 0;. }e
823a0 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a lse if( strcmp(z
823b0 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 ,"year")==0 ){.
823c0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d computeYM
823d0 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d D(p);. p-
823e0 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 >M = 1;.
823f0 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 p->D = 1;.
82400 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 rc = 0;.
82410 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 }else if( strcmp
82420 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a (z,"day")==0 ){.
82430 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a rc = 0;.
82440 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
82450 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
82460 61 73 65 20 27 2b 27 3a 0a 20 20 20 20 63 61 73 ase '+':. cas
82470 65 20 27 2d 27 3a 0a 20 20 20 20 63 61 73 65 20 e '-':. case
82480 27 30 27 3a 0a 20 20 20 20 63 61 73 65 20 27 31 '0':. case '1
82490 27 3a 0a 20 20 20 20 63 61 73 65 20 27 32 27 3a ':. case '2':
824a0 0a 20 20 20 20 63 61 73 65 20 27 33 27 3a 0a 20 . case '3':.
824b0 20 20 20 63 61 73 65 20 27 34 27 3a 0a 20 20 20 case '4':.
824c0 20 63 61 73 65 20 27 35 27 3a 0a 20 20 20 20 63 case '5':. c
824d0 61 73 65 20 27 36 27 3a 0a 20 20 20 20 63 61 73 ase '6':. cas
824e0 65 20 27 37 27 3a 0a 20 20 20 20 63 61 73 65 20 e '7':. case
824f0 27 38 27 3a 0a 20 20 20 20 63 61 73 65 20 27 39 '8':. case '9
82500 27 3a 20 7b 0a 20 20 20 20 20 20 6e 20 3d 20 67 ': {. n = g
82510 65 74 56 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a etValue(z, &r);.
82520 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e assert( n>
82530 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 =1 );. if(
82540 7a 5b 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 z[n]==':' ){.
82550 20 20 20 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 /* A modifi
82560 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 er of the form (
82570 2b 7c 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 +|-)HH:MM:SS.FFF
82580 20 61 64 64 73 20 28 6f 72 20 73 75 62 74 72 61 adds (or subtra
82590 63 74 73 29 20 74 68 65 0a 20 20 20 20 20 20 20 cts) the.
825a0 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75 ** specified nu
825b0 6d 62 65 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d mber of hours, m
825c0 69 6e 75 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c inutes, seconds,
825d0 20 61 6e 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 and fractional
825e0 73 65 63 6f 6e 64 73 0a 20 20 20 20 20 20 20 20 seconds.
825f0 2a 2a 20 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 ** to the time.
82600 20 54 68 65 20 22 2e 46 46 46 22 20 6d 61 79 20 The ".FFF" may
82610 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 be omitted. The
82620 20 22 3a 53 53 2e 46 46 46 22 20 6d 61 79 20 62 ":SS.FFF" may b
82630 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69 e. ** omi
82640 74 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f tted.. */
82650 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 . const c
82660 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 har *z2 = z;.
82670 20 20 20 20 20 44 61 74 65 54 69 6d 65 20 74 78 DateTime tx
82680 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
82690 33 5f 69 6e 74 36 34 20 64 61 79 3b 0a 20 20 20 3_int64 day;.
826a0 20 20 20 20 20 69 66 28 20 21 69 73 64 69 67 69 if( !isdigi
826b0 74 28 2a 28 75 38 2a 29 7a 32 29 20 29 20 7a 32 t(*(u8*)z2) ) z2
826c0 2b 2b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 ++;. mems
826d0 65 74 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f et(&tx, 0, sizeo
826e0 66 28 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20 f(tx));.
826f0 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 if( parseHhMmSs(
82700 7a 32 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b z2, &tx) ) break
82710 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 ;. comput
82720 65 4a 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20 eJD(&tx);.
82730 20 20 74 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30 tx.iJD -= 4320
82740 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 64 61 0000;. da
82750 79 20 3d 20 74 78 2e 69 4a 44 2f 38 36 34 30 30 y = tx.iJD/86400
82760 30 30 30 3b 0a 20 20 20 20 20 20 20 20 74 78 2e 000;. tx.
82770 69 4a 44 20 2d 3d 20 64 61 79 2a 38 36 34 30 30 iJD -= day*86400
82780 30 30 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 000;. if(
82790 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e z[0]=='-' ) tx.
827a0 69 4a 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 iJD = -tx.iJD;.
827b0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 computeJD
827c0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 (p);. cle
827d0 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b arYMD_HMS_TZ(p);
827e0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
827f0 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 += tx.iJD;.
82800 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
82810 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
82820 7d 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a }. z += n;.
82830 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 73 73 while( iss
82840 70 61 63 65 28 2a 28 75 38 2a 29 7a 29 20 29 20 pace(*(u8*)z) )
82850 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 z++;. n = s
82860 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
82870 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 31 );. if( n>1
82880 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 0 || n<3 ) break
82890 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d ;. if( z[n-
828a0 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 1]=='s' ){ z[n-1
828b0 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 ] = 0; n--; }.
828c0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 computeJD(p)
828d0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a ;. rc = 0;.
828e0 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33 20 26 if( n==3 &
828f0 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 & strcmp(z,"day"
82900 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
82910 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 p->iJD += (sqlit
82920 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 e3_int64)(r*8640
82930 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 0000.0 + 0.5);.
82940 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e }else if( n
82950 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c ==4 && strcmp(z,
82960 22 68 6f 75 72 22 29 3d 3d 30 20 29 7b 0a 20 20 "hour")==0 ){.
82970 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 p->iJD +=
82980 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
82990 72 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 32 34 r*(86400000.0/24
829a0 2e 30 29 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 .0) + 0.5);.
829b0 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 }else if( n==6
829c0 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69 && strcmp(z,"mi
829d0 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 nute")==0 ){.
829e0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 p->iJD += (
829f0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 sqlite3_int64)(r
82a00 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 *(86400000.0/(24
82a10 2e 30 2a 36 30 2e 30 29 29 20 2b 20 30 2e 35 29 .0*60.0)) + 0.5)
82a20 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
82a30 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 ( n==6 && strcmp
82a40 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 20 (z,"second")==0
82a50 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a ){. p->iJ
82a60 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e D += (sqlite3_in
82a70 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 30 t64)(r*(86400000
82a80 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 30 .0/(24.0*60.0*60
82a90 2e 30 29 29 20 2b 20 30 2e 35 29 3b 0a 20 20 20 .0)) + 0.5);.
82aa0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d }else if( n==
82ab0 35 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 5 && strcmp(z,"m
82ac0 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 onth")==0 ){.
82ad0 20 20 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20 int x, y;.
82ae0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d computeYM
82af0 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 D_HMS(p);.
82b00 20 20 70 2d 3e 4d 20 2b 3d 20 28 69 6e 74 29 72 p->M += (int)r
82b10 3b 0a 20 20 20 20 20 20 20 20 78 20 3d 20 70 2d ;. x = p-
82b20 3e 4d 3e 30 20 3f 20 28 70 2d 3e 4d 2d 31 29 2f >M>0 ? (p->M-1)/
82b30 31 32 20 3a 20 28 70 2d 3e 4d 2d 31 32 29 2f 31 12 : (p->M-12)/1
82b40 32 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2;. p->Y
82b50 2b 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 70 2d += x;. p-
82b60 3e 4d 20 2d 3d 20 78 2a 31 32 3b 0a 20 20 20 20 >M -= x*12;.
82b70 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d p->validJD =
82b80 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 0;. comp
82b90 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 uteJD(p);.
82ba0 20 20 79 20 3d 20 28 69 6e 74 29 72 3b 0a 20 20 y = (int)r;.
82bb0 20 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 if( y!=r )
82bc0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 {. p->i
82bd0 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 JD += (sqlite3_i
82be0 6e 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 30 nt64)((r - y)*30
82bf0 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 .0*86400000.0 +
82c00 30 2e 35 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0.5);. }.
82c10 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
82c20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a n==4 && strcmp(z
82c30 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 ,"year")==0 ){.
82c40 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d computeYM
82c50 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 D_HMS(p);.
82c60 20 20 70 2d 3e 59 20 2b 3d 20 28 69 6e 74 29 72 p->Y += (int)r
82c70 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c ;. p->val
82c80 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 idJD = 0;.
82c90 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
82ca0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
82cb0 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 rc = 1;.
82cc0 20 20 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72 }. clear
82cd0 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 YMD_HMS_TZ(p);.
82ce0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
82cf0 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b }. default: {
82d00 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
82d10 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
82d20 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 rc;.}../*.** Pr
82d30 6f 63 65 73 73 20 74 69 6d 65 20 66 75 6e 63 74 ocess time funct
82d40 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 ion arguments.
82d50 61 72 67 76 5b 30 5d 20 69 73 20 61 20 64 61 74 argv[0] is a dat
82d60 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a e-time stamp..**
82d70 20 61 72 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c argv[1] and fol
82d80 6c 6f 77 69 6e 67 20 61 72 65 20 6d 6f 64 69 66 lowing are modif
82d90 69 65 72 73 2e 20 20 50 61 72 73 65 20 74 68 65 iers. Parse the
82da0 6d 20 61 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a m all and write.
82db0 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 ** the resulting
82dc0 20 74 69 6d 65 20 69 6e 74 6f 20 74 68 65 20 44 time into the D
82dd0 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 ateTime structur
82de0 65 20 70 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a e p. Return 0.*
82df0 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 * on success and
82e00 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65 20 1 if there are
82e10 61 6e 79 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a any errors..**.*
82e20 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 7a * If there are z
82e30 65 72 6f 20 70 61 72 61 6d 65 74 65 72 73 20 28 ero parameters (
82e40 69 66 20 65 76 65 6e 20 61 72 67 76 5b 30 5d 20 if even argv[0]
82e50 69 73 20 75 6e 64 65 66 69 6e 65 64 29 0a 2a 2a is undefined).**
82e60 20 74 68 65 6e 20 61 73 73 75 6d 65 20 61 20 64 then assume a d
82e70 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 efault value of
82e80 22 6e 6f 77 22 20 66 6f 72 20 61 72 67 76 5b 30 "now" for argv[0
82e90 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 ]..*/.static int
82ea0 20 69 73 44 61 74 65 28 0a 20 20 73 71 6c 69 74 isDate(. sqlit
82eb0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
82ec0 65 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 ext, . int argc
82ed0 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c , . sqlite3_val
82ee0 75 65 20 2a 2a 61 72 67 76 2c 20 0a 20 20 44 61 ue **argv, . Da
82ef0 74 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 teTime *p.){. i
82f00 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 75 6e nt i;. const un
82f10 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a signed char *z;.
82f20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 6d int eType;. m
82f30 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 emset(p, 0, size
82f40 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 61 of(*p));. if( a
82f50 72 67 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65 rgc==0 ){. se
82f60 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 tDateTimeToCurre
82f70 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a nt(context, p);.
82f80 20 20 7d 65 6c 73 65 20 69 66 28 20 28 65 54 79 }else if( (eTy
82f90 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c pe = sqlite3_val
82fa0 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 ue_type(argv[0])
82fb0 29 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 0a )==SQLITE_FLOAT.
82fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82fd0 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c || eType==SQL
82fe0 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 ITE_INTEGER ){.
82ff0 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c p->iJD = (sql
83000 69 74 65 33 5f 69 6e 74 36 34 29 28 73 71 6c 69 ite3_int64)(sqli
83010 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 te3_value_double
83020 28 61 72 67 76 5b 30 5d 29 2a 38 36 34 30 30 30 (argv[0])*864000
83030 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 00.0 + 0.5);.
83040 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b p->validJD = 1;
83050 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 . }else{. z
83060 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
83070 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 text(argv[0]);.
83080 20 20 20 69 66 28 20 21 7a 20 7c 7c 20 70 61 72 if( !z || par
83090 73 65 44 61 74 65 4f 72 54 69 6d 65 28 63 6f 6e seDateOrTime(con
830a0 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 text, (char*)z,
830b0 70 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 p) ){. retu
830c0 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 1;. }. }.
830d0 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 for(i=1; i<arg
830e0 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 c; i++){. if(
830f0 20 28 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 (z = sqlite3_va
83100 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d lue_text(argv[i]
83110 29 29 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f ))==0 || parseMo
83120 64 69 66 69 65 72 28 28 63 68 61 72 2a 29 7a 2c difier((char*)z,
83130 20 70 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 p) ){. ret
83140 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 1;. }. }
83150 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
83160 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
83170 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d wing routines im
83180 70 6c 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69 plement the vari
83190 6f 75 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d ous date and tim
831a0 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f e functions.** o
831b0 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a f SQLite..*/../*
831c0 0a 2a 2a 20 20 20 20 6a 75 6c 69 61 6e 64 61 79 .** julianday
831d0 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f ( TIMESTRING, MO
831e0 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a D, MOD, ...).**.
831f0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6a 75 ** Return the ju
83200 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 lian day number
83210 6f 66 20 74 68 65 20 64 61 74 65 20 73 70 65 63 of the date spec
83220 69 66 69 65 64 20 69 6e 20 74 68 65 20 61 72 67 ified in the arg
83230 75 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 uments.*/.static
83240 20 76 6f 69 64 20 6a 75 6c 69 61 6e 64 61 79 46 void juliandayF
83250 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
83260 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
83270 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
83280 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
83290 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d rgv.){. DateTim
832a0 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 e x;. if( isDat
832b0 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c e(context, argc,
832c0 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b argv, &x)==0 ){
832d0 0a 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 . computeJD(&
832e0 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f x);. sqlite3_
832f0 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f result_double(co
83300 6e 74 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34 ntext, x.iJD/864
83310 30 30 30 30 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 00000.0);. }.}.
83320 0a 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69 ./*.** dateti
83330 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 me( TIMESTRING,
83340 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a MOD, MOD, ...).*
83350 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59 *.** Return YYYY
83360 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a -MM-DD HH:MM:SS.
83370 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 */.static void d
83380 61 74 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 atetimeFunc(. s
83390 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
833a0 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
833b0 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
833c0 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
833d0 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 DateTime x;. i
833e0 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 f( isDate(contex
833f0 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 t, argc, argv, &
83400 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 x)==0 ){. cha
83410 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 r zBuf[100];.
83420 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 computeYMD_HMS(
83430 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 &x);. sqlite3
83440 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 _snprintf(sizeof
83450 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 (zBuf), zBuf, "%
83460 30 34 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30 04d-%02d-%02d %0
83470 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20 2d:%02d:%02d",.
83480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
83490 20 20 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e x.Y, x.M, x.
834a0 44 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e D, x.h, x.m, (in
834b0 74 29 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71 t)(x.s));. sq
834c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
834d0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c t(context, zBuf,
834e0 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e -1, SQLITE_TRAN
834f0 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f SIENT);. }.}../
83500 2a 0a 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49 *.** time( TI
83510 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d MESTRING, MOD, M
83520 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 OD, ...).**.** R
83530 65 74 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a eturn HH:MM:SS.*
83540 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 69 /.static void ti
83550 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 meFunc(. sqlite
83560 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
83570 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a xt,. int argc,.
83580 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
83590 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 **argv.){. Date
835a0 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 Time x;. if( is
835b0 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 Date(context, ar
835c0 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 gc, argv, &x)==0
835d0 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 ){. char zBu
835e0 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 f[100];. comp
835f0 75 74 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 uteHMS(&x);.
83600 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
83610 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a (sizeof(zBuf), z
83620 42 75 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a Buf, "%02d:%02d:
83630 25 30 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c %02d", x.h, x.m,
83640 20 28 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 (int)x.s);.
83650 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
83660 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 ext(context, zBu
83670 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 f, -1, SQLITE_TR
83680 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a ANSIENT);. }.}.
83690 0a 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 ./*.** date(
836a0 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c TIMESTRING, MOD,
836b0 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a MOD, ...).**.**
836c0 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d Return YYYY-MM-
836d0 44 44 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 DD.*/.static voi
836e0 64 20 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 d dateFunc(. sq
836f0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
83700 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
83710 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
83720 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
83730 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 DateTime x;. if
83740 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 ( isDate(context
83750 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 , argc, argv, &x
83760 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 )==0 ){. char
83770 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 zBuf[100];.
83780 63 6f 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a computeYMD(&x);.
83790 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
837a0 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 intf(sizeof(zBuf
837b0 29 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 ), zBuf, "%04d-%
837c0 30 32 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20 02d-%02d", x.Y,
837d0 78 2e 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73 x.M, x.D);. s
837e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
837f0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 xt(context, zBuf
83800 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 , -1, SQLITE_TRA
83810 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a NSIENT);. }.}..
83820 2f 2a 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d /*.** strftim
83830 65 28 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53 e( FORMAT, TIMES
83840 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c TRING, MOD, MOD,
83850 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 ...).**.** Retu
83860 72 6e 20 61 20 73 74 72 69 6e 67 20 64 65 73 63 rn a string desc
83870 72 69 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e ribed by FORMAT.
83880 20 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 Conversions as
83890 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
838a0 20 20 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e %d day of mon
838b0 74 68 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66 th.** %f ** f
838c0 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 ractional second
838d0 73 20 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25 s SS.SSS.** %
838e0 48 20 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a H hour 00-24.**
838f0 20 20 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65 %j day of ye
83900 61 72 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20 ar 000-366.**
83910 25 4a 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 %J ** Julian da
83920 79 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d y number.** %m
83930 20 20 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a month 01-12.**
83940 20 20 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30 %M minute 00
83950 2d 35 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63 -59.** %s sec
83960 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d onds since 1970-
83970 30 31 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73 01-01.** %S s
83980 65 63 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20 econds 00-59.**
83990 20 20 25 77 20 20 64 61 79 20 6f 66 20 77 65 65 %w day of wee
839a0 6b 20 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 k 0-6 sunday==0
839b0 0a 2a 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f .** %W week o
839c0 66 20 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20 f year 00-53.**
839d0 20 20 25 59 20 20 79 65 61 72 20 30 30 30 30 2d %Y year 0000-
839e0 39 39 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a 9999.** %% %.
839f0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
83a00 74 72 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 trftimeFunc(. s
83a10 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
83a20 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
83a30 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
83a40 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
83a50 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75 DateTime x;. u
83a60 36 34 20 6e 3b 0a 20 20 73 69 7a 65 5f 74 20 69 64 n;. size_t i
83a70 2c 6a 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 ,j;. char *z;.
83a80 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 sqlite3 *db;.
83a90 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 const char *zFmt
83aa0 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 = (const char*)
83ab0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
83ac0 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 xt(argv[0]);. c
83ad0 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 har zBuf[100];.
83ae0 20 69 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 if( zFmt==0 ||
83af0 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 isDate(context,
83b00 61 72 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 argc-1, argv+1,
83b10 26 78 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 &x) ) return;.
83b20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
83b30 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
83b40 6f 6e 74 65 78 74 29 3b 0a 20 20 66 6f 72 28 69 ontext);. for(i
83b50 3d 30 2c 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d =0, n=1; zFmt[i]
83b60 3b 20 69 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 ; i++, n++){.
83b70 20 69 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 if( zFmt[i]=='%
83b80 27 20 29 7b 0a 20 20 20 20 20 20 73 77 69 74 63 ' ){. switc
83b90 68 28 20 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a h( zFmt[i+1] ){.
83ba0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 64 27 case 'd'
83bb0 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 :. case '
83bc0 48 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 H':. case
83bd0 20 27 6d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 'm':. ca
83be0 73 65 20 27 4d 27 3a 0a 20 20 20 20 20 20 20 20 se 'M':.
83bf0 63 61 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20 case 'S':.
83c00 20 20 63 61 73 65 20 27 57 27 3a 0a 20 20 20 20 case 'W':.
83c10 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 n++;.
83c20 20 20 20 20 20 2f 2a 20 66 61 6c 6c 20 74 68 72 /* fall thr
83c30 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 u */. cas
83c40 65 20 27 77 27 3a 0a 20 20 20 20 20 20 20 20 63 e 'w':. c
83c50 61 73 65 20 27 25 27 3a 0a 20 20 20 20 20 20 20 ase '%':.
83c60 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
83c70 20 20 63 61 73 65 20 27 66 27 3a 0a 20 20 20 20 case 'f':.
83c80 20 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 n += 8;.
83c90 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
83ca0 20 20 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a case 'j':
83cb0 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 . n +=
83cc0 33 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 3;. bre
83cd0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
83ce0 20 27 59 27 3a 0a 20 20 20 20 20 20 20 20 20 20 'Y':.
83cf0 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 n += 8;.
83d00 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
83d10 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 case 's':.
83d20 20 20 20 63 61 73 65 20 27 4a 27 3a 0a 20 20 20 case 'J':.
83d30 20 20 20 20 20 20 20 6e 20 2b 3d 20 35 30 3b 0a n += 50;.
83d40 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
83d50 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 . default
83d60 3a 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 :. retu
83d70 72 6e 3b 20 20 2f 2a 20 45 52 52 4f 52 2e 20 20 rn; /* ERROR.
83d80 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f return a NULL */
83d90 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
83da0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ++;. }. }.
83db0 69 66 28 20 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 if( n<sizeof(zBu
83dc0 66 29 20 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 f) ){. z = zB
83dd0 75 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 uf;. }else if(
83de0 6e 3e 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 n>(u64)db->aLimi
83df0 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
83e00 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 ENGTH] ){. sq
83e10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
83e20 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 or_toobig(contex
83e30 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a t);. return;.
83e40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d }else{. z =
83e50 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
83e60 52 61 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b Raw(db, (int)n);
83e70 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b . if( z==0 ){
83e80 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
83e90 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 esult_error_nome
83ea0 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 m(context);.
83eb0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
83ec0 20 20 7d 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 }. computeJD(
83ed0 26 78 29 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d &x);. computeYM
83ee0 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 66 6f 72 D_HMS(&x);. for
83ef0 28 69 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b (i=j=0; zFmt[i];
83f00 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a i++){. if( z
83f10 46 6d 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0a 20 Fmt[i]!='%' ){.
83f20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46 z[j++] = zF
83f30 6d 74 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 mt[i];. }else
83f40 7b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 {. i++;.
83f50 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b switch( zFmt[
83f60 69 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 i] ){. ca
83f70 73 65 20 27 64 27 3a 20 20 73 71 6c 69 74 65 33 se 'd': sqlite3
83f80 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b _snprintf(3, &z[
83f90 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 44 29 3b 20 j],"%02d",x.D);
83fa0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 j+=2; break;.
83fb0 20 20 20 20 20 63 61 73 65 20 27 66 27 3a 20 7b case 'f': {
83fc0 0a 20 20 20 20 20 20 20 20 20 20 64 6f 75 62 6c . doubl
83fd0 65 20 73 20 3d 20 78 2e 73 3b 0a 20 20 20 20 20 e s = x.s;.
83fe0 20 20 20 20 20 69 66 28 20 73 3e 35 39 2e 39 39 if( s>59.99
83ff0 39 20 29 20 73 20 3d 20 35 39 2e 39 39 39 3b 0a 9 ) s = 59.999;.
84000 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
84010 33 5f 73 6e 70 72 69 6e 74 66 28 37 2c 20 26 7a 3_snprintf(7, &z
84020 5b 6a 5d 2c 22 25 30 36 2e 33 66 22 2c 20 73 29 [j],"%06.3f", s)
84030 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d ;. j +=
84040 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
84050 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 (&z[j]);.
84060 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
84070 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 }. case
84080 20 27 48 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 'H': sqlite3_s
84090 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d nprintf(3, &z[j]
840a0 2c 22 25 30 32 64 22 2c 78 2e 68 29 3b 20 6a 2b ,"%02d",x.h); j+
840b0 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 =2; break;.
840c0 20 20 20 63 61 73 65 20 27 57 27 3a 20 2f 2a 20 case 'W': /*
840d0 46 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 Fall thru */.
840e0 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 20 7b case 'j': {
840f0 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e . int n
84100 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 Day;
84110 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 /* Number of da
84120 79 73 20 73 69 6e 63 65 20 31 73 74 20 64 61 79 ys since 1st day
84130 20 6f 66 20 79 65 61 72 20 2a 2f 0a 20 20 20 20 of year */.
84140 20 20 20 20 20 20 44 61 74 65 54 69 6d 65 20 79 DateTime y
84150 20 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 = x;.
84160 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 y.validJD = 0;.
84170 20 20 20 20 20 20 20 20 20 79 2e 4d 20 3d 20 31 y.M = 1
84180 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e 44 20 ;. y.D
84190 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63 = 1;. c
841a0 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 omputeJD(&y);.
841b0 20 20 20 20 20 20 20 20 6e 44 61 79 20 3d 20 28 nDay = (
841c0 69 6e 74 29 28 28 78 2e 69 4a 44 2d 79 2e 69 4a int)((x.iJD-y.iJ
841d0 44 2b 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 D+43200000)/8640
841e0 30 30 30 30 29 3b 0a 20 20 20 20 20 20 20 20 20 0000);.
841f0 20 69 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57 if( zFmt[i]=='W
84200 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ' ){.
84210 20 69 6e 74 20 77 64 3b 20 20 20 2f 2a 20 30 3d int wd; /* 0=
84220 4d 6f 6e 64 61 79 2c 20 31 3d 54 75 65 73 64 61 Monday, 1=Tuesda
84230 79 2c 20 2e 2e 2e 20 36 3d 53 75 6e 64 61 79 20 y, ... 6=Sunday
84240 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 77 */. w
84250 64 20 3d 20 28 69 6e 74 29 28 28 28 78 2e 69 4a d = (int)(((x.iJ
84260 44 2b 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 D+43200000)/8640
84270 30 30 30 30 29 25 37 29 3b 0a 20 20 20 20 20 20 0000)%7);.
84280 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
84290 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c printf(3, &z[j],
842a0 22 25 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 "%02d",(nDay+7-w
842b0 64 29 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20 d)/7);.
842c0 20 20 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 j += 2;.
842d0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
842e0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
842f0 73 6e 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a snprintf(4, &z[j
84300 5d 2c 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 ],"%03d",nDay+1)
84310 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 ;. j
84320 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 += 3;.
84330 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 }. brea
84340 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
84350 20 20 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b case 'J': {
84360 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
84370 65 33 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 e3_snprintf(20,
84380 26 7a 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e &z[j],"%.16g",x.
84390 69 4a 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b iJD/86400000.0);
843a0 0a 20 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 71 . j+=sq
843b0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a lite3Strlen30(&z
843c0 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 [j]);.
843d0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
843e0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 6d . case 'm
843f0 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 ': sqlite3_snpr
84400 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 intf(3, &z[j],"%
84410 30 32 64 22 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 02d",x.M); j+=2;
84420 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
84430 63 61 73 65 20 27 4d 27 3a 20 20 73 71 6c 69 74 case 'M': sqlit
84440 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 e3_snprintf(3, &
84450 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 6d 29 z[j],"%02d",x.m)
84460 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 ; j+=2; break;.
84470 20 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a case 's':
84480 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c {. sql
84490 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 30 ite3_snprintf(30
844a0 2c 26 7a 5b 6a 5d 2c 22 25 64 22 2c 0a 20 20 20 ,&z[j],"%d",.
844b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
844c0 20 20 20 20 20 20 20 20 28 69 6e 74 29 28 78 2e (int)(x.
844d0 69 4a 44 2f 31 30 30 30 2e 30 20 2d 20 32 31 30 iJD/1000.0 - 210
844e0 38 36 36 37 36 30 30 30 30 2e 30 29 29 3b 0a 20 866760000.0));.
844f0 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 j += sq
84500 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a lite3Strlen30(&z
84510 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 [j]);.
84520 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
84530 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53 . case 'S
84540 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 ': sqlite3_snpr
84550 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 intf(3,&z[j],"%0
84560 32 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2d",(int)x.s); j
84570 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 +=2; break;.
84580 20 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a case 'w': {.
84590 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d z[j++]
845a0 20 3d 20 28 63 68 61 72 29 28 28 28 78 2e 69 4a = (char)(((x.iJ
845b0 44 2b 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 D+129600000)/864
845c0 30 30 30 30 30 29 20 25 20 37 29 20 2b 20 27 30 00000) % 7) + '0
845d0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 ';. bre
845e0 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
845f0 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 20 case 'Y':
84600 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
84610 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 te3_snprintf(5,&
84620 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 z[j],"%04d",x.Y)
84630 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c ; j+=sqlite3Strl
84640 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 en30(&z[j]);.
84650 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
84660 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
84670 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b default: z[j++
84680 5d 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a ] = '%'; break;.
84690 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
846a0 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 }. z[j] = 0;.
846b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
846c0 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 ext(context, z,
846d0 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 -1,.
846e0 20 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 z==zBu
846f0 66 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 f ? SQLITE_TRANS
84700 49 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 IENT : SQLITE_DY
84710 4e 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a NAMIC);.}../*.**
84720 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a current_time().
84730 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
84740 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 ion returns the
84750 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69 same value as ti
84760 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 me('now')..*/.st
84770 61 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 46 atic void ctimeF
84780 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
84790 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
847a0 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a . int NotUsed,.
847b0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
847c0 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 **NotUsed2.){.
847d0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
847e0 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
847f0 65 64 32 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 ed2);. timeFunc
84800 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b (context, 0, 0);
84810 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e .}../*.** curren
84820 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 t_date().**.** T
84830 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
84840 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 urns the same va
84850 6c 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77 lue as date('now
84860 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ')..*/.static vo
84870 69 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20 id cdateFunc(.
84880 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
84890 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
848a0 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 NotUsed,. sqlit
848b0 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 e3_value **NotUs
848c0 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f ed2.){. UNUSED_
848d0 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 PARAMETER2(NotUs
848e0 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 ed, NotUsed2);.
848f0 20 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 dateFunc(contex
84900 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a t, 0, 0);.}../*.
84910 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 ** current_times
84920 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 tamp().**.** Thi
84930 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
84940 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 ns the same valu
84950 65 20 61 73 20 64 61 74 65 74 69 6d 65 28 27 6e e as datetime('n
84960 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ow')..*/.static
84970 76 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70 46 void ctimestampF
84980 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
84990 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
849a0 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a . int NotUsed,.
849b0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
849c0 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 **NotUsed2.){.
849d0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
849e0 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
849f0 65 64 32 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 ed2);. datetime
84a00 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c Func(context, 0,
84a10 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 0);.}.#endif /*
84a20 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
84a30 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 _OMIT_DATETIME_F
84a40 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 UNCS) */..#ifdef
84a50 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 SQLITE_OMIT_DAT
84a60 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a ETIME_FUNCS./*.*
84a70 2a 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79 * If the library
84a80 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 is compiled to
84a90 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 omit the full-sc
84aa0 61 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d ale date and tim
84ab0 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 e.** handling (t
84ac0 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 o get a smaller
84ad0 62 69 6e 61 72 79 29 2c 20 74 68 65 20 66 6f 6c binary), the fol
84ae0 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 lowing minimal v
84af0 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 ersion.** of the
84b00 20 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 functions curre
84b10 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 72 65 nt_time(), curre
84b20 6e 74 5f 64 61 74 65 28 29 20 61 6e 64 20 63 75 nt_date() and cu
84b30 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 rrent_timestamp(
84b40 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 ).** are include
84b50 64 20 69 6e 73 74 65 61 64 2e 20 54 68 69 73 20 d instead. This
84b60 69 73 20 74 6f 20 73 75 70 70 6f 72 74 20 63 6f is to support co
84b70 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e lumn declaration
84b80 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 s that.** includ
84b90 65 20 22 44 45 46 41 55 4c 54 20 43 55 52 52 45 e "DEFAULT CURRE
84ba0 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a NT_TIME" etc..**
84bb0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
84bc0 6e 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69 62 n uses the C-lib
84bd0 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 rary functions t
84be0 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a ime(), gmtime().
84bf0 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65 28 ** and strftime(
84c00 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73 74 ). The format st
84c10 72 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f 20 ring to pass to
84c20 73 74 72 66 74 69 6d 65 28 29 20 69 73 20 73 75 strftime() is su
84c30 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 pplied.** as the
84c40 20 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20 74 user-data for t
84c50 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a he function..*/.
84c60 73 74 61 74 69 63 20 76 6f 69 64 20 63 75 72 72 static void curr
84c70 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 entTimeFunc(. s
84c80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
84c90 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
84ca0 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
84cb0 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
84cc0 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 time_t t;. cha
84cd0 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 r *zFormat = (ch
84ce0 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 ar *)sqlite3_use
84cf0 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b r_data(context);
84d00 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
84d10 20 20 64 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 double rT;. c
84d20 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 har zBuf[20];..
84d30 20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f db = sqlite3_co
84d40 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
84d50 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 context);. sqli
84d60 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
84d70 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 54 29 3b (db->pVfs, &rT);
84d80 0a 20 20 74 20 3d 20 38 36 34 30 30 2e 30 2a 28 . t = 86400.0*(
84d90 72 54 20 2d 20 32 34 34 30 35 38 37 2e 35 29 20 rT - 2440587.5)
84da0 2b 20 30 2e 35 3b 0a 23 69 66 64 65 66 20 48 41 + 0.5;.#ifdef HA
84db0 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a VE_GMTIME_R. {.
84dc0 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4e struct tm sN
84dd0 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d 65 5f 72 ow;. gmtime_r
84de0 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 20 (&t, &sNow);.
84df0 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 strftime(zBuf,
84e00 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 20, zFormat, &sN
84e10 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 ow);. }.#else.
84e20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d {. struct tm
84e30 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 *pTm;. sqlit
84e40 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 e3_mutex_enter(s
84e50 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
84e60 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
84e70 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 ATIC_MASTER));.
84e80 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 pTm = gmtime(
84e90 26 74 29 3b 0a 20 20 20 20 73 74 72 66 74 69 6d &t);. strftim
84ea0 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 e(zBuf, 20, zFor
84eb0 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73 mat, pTm);. s
84ec0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
84ed0 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ve(sqlite3MutexA
84ee0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
84ef0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
84f00 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 );. }.#endif..
84f10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
84f20 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 text(context, zB
84f30 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 uf, -1, SQLITE_T
84f40 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e RANSIENT);.}.#en
84f50 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 dif../*.** This
84f60 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 function registe
84f70 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 red all of the a
84f80 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 bove C functions
84f90 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 as SQL.** funct
84fa0 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f 75 ions. This shou
84fb0 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 ld be the only r
84fc0 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 outine in this f
84fd0 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 ile with.** exte
84fe0 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f rnal linkage..*/
84ff0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
85000 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 void sqlite3Regi
85010 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 sterDateTimeFunc
85020 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73 tions(void){. s
85030 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
85040 20 46 75 6e 63 44 65 66 20 61 44 61 74 65 54 69 FuncDef aDateTi
85050 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 meFuncs[] = {.#i
85060 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
85070 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 T_DATETIME_FUNCS
85080 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6a 75 . FUNCTION(ju
85090 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 20 20 20 lianday,
850a0 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c 69 61 6e -1, 0, 0, julian
850b0 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 20 20 46 dayFunc ),. F
850c0 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c 20 20 20 UNCTION(date,
850d0 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c -1, 0,
850e0 20 30 2c 20 64 61 74 65 46 75 6e 63 20 20 20 20 0, dateFunc
850f0 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
85100 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 N(time,
85110 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 74 69 -1, 0, 0, ti
85120 6d 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 meFunc ),.
85130 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 FUNCTION(date
85140 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 2d 31 time, -1
85150 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 69 6d 65 , 0, 0, datetime
85160 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 55 4e Func ),. FUN
85170 43 54 49 4f 4e 28 73 74 72 66 74 69 6d 65 2c 20 CTION(strftime,
85180 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 -1, 0, 0
85190 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63 20 20 , strftimeFunc
851a0 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
851b0 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 current_time,
851c0 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 0, 0, 0, ctim
851d0 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 eFunc ),.
851e0 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e FUNCTION(curren
851f0 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 t_timestamp, 0,
85200 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 61 6d 70 0, 0, ctimestamp
85210 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e 43 54 Func),. FUNCT
85220 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 65 ION(current_date
85230 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 , 0, 0, 0,
85240 63 64 61 74 65 46 75 6e 63 20 20 20 20 20 29 2c cdateFunc ),
85250 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 52 5f 46 .#else. STR_F
85260 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f UNCTION(current_
85270 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 22 25 time, 0, "%
85280 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20 20 20 H:%M:%S",
85290 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 0, currentTim
852a0 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f eFunc),. STR_
852b0 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 FUNCTION(current
852c0 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 _timestamp, 0, "
852d0 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20 20 20 %Y-%m-%d",
852e0 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69 0, currentTi
852f0 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 meFunc),. STR
85300 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e _FUNCTION(curren
85310 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 2c 20 t_date, 0,
85320 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a "%Y-%m-%d %H:%M:
85330 25 53 22 2c 20 30 2c 20 63 75 72 72 65 6e 74 54 %S", 0, currentT
85340 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66 imeFunc),.#endif
85350 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 . };. int i;.
85360 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 FuncDefHash *pH
85370 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 ash = &GLOBAL(Fu
85380 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74 ncDefHash, sqlit
85390 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e e3GlobalFunction
853a0 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 s);. FuncDef *a
853b0 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66 2a Func = (FuncDef*
853c0 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 )&GLOBAL(FuncDef
853d0 2c 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 73 , aDateTimeFuncs
853e0 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 );.. for(i=0; i
853f0 3c 41 72 72 61 79 53 69 7a 65 28 61 44 61 74 65 <ArraySize(aDate
85400 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29 TimeFuncs); i++)
85410 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e {. sqlite3Fun
85420 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 68 cDefInsert(pHash
85430 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 , &aFunc[i]);.
85440 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../**********
85450 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 **** End of date
85460 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
85470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85490 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
854a0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
854b0 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os.c ***********
854c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
854d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
854e0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 ***/./*.** 2005
854f0 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a November 29.**.*
85500 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
85510 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
85520 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
85530 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
85540 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
85550 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
85560 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
85570 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
85580 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
85590 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
855a0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
855b0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
855c0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
855d0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
855e0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
855f0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
85600 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
85610 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
85620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
85660 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
85670 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 contains OS inte
85680 72 66 61 63 65 20 63 6f 64 65 20 74 68 61 74 20 rface code that
85690 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c is common to all
856a0 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72 65 .** architecture
856b0 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 s..**.** $Id: os
856c0 2e 63 2c 76 20 31 2e 31 32 35 20 32 30 30 38 2f .c,v 1.125 2008/
856d0 31 32 2f 30 38 20 31 38 3a 31 39 3a 31 38 20 64 12/08 18:19:18 d
856e0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 64 65 66 rh Exp $.*/.#def
856f0 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 ine _SQLITE_OS_C
85700 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 4c 49 _ 1.#undef _SQLI
85710 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20 TE_OS_C_../*.**
85720 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 4c 69 The default SQLi
85730 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 69 te sqlite3_vfs i
85740 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 mplementations d
85750 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0a 2a o not allocate.*
85760 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61 6c * memory (actual
85770 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c ly, os_unix.c al
85780 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c 20 locates a small
85790 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
857a0 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 .** from within
857b0 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20 73 OsOpen()), but s
857c0 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79 20 ome third-party
857d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
857e0 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 65 may..** So we te
857f0 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20 6f st the effects o
85800 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
85810 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71 6c ling and the sql
85820 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a 20 66 ite3OsXXX().** f
85830 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 69 6e unction returnin
85840 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e g SQLITE_IOERR_N
85850 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 20 44 OMEM using the D
85860 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
85870 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 macro..**.** Th
85880 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 e following func
85890 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 72 75 tions are instru
858a0 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c 6c 6f mented for mallo
858b0 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 c() failure .**
858c0 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 testing:.**.**
858d0 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e sqlite3OsOpen
858e0 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
858f0 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20 20 3OsRead().**
85900 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
85910 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
85920 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20 OsSync().**
85930 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a sqlite3OsLock().
85940 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 **.*/.#if define
85950 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 d(SQLITE_TEST) &
85960 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e & (SQLITE_OS_WIN
85970 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 20 44 ==0). #define D
85980 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
85990 20 69 66 20 28 31 29 20 7b 20 20 20 20 20 20 20 if (1) {
859a0 20 20 20 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 \. void
859b0 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c *pTstAlloc = sql
859c0 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 ite3Malloc(10);
859d0 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 20 28 \. if (
859e0 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 !pTstAlloc) retu
859f0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
85a00 4e 4f 4d 45 4d 3b 20 20 5c 0a 20 20 20 20 73 71 NOMEM; \. sq
85a10 6c 69 74 65 33 5f 66 72 65 65 28 70 54 73 74 41 lite3_free(pTstA
85a20 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20 20 20 20 lloc);
85a30 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a \. }.
85a40 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
85a50 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
85a60 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 T.#endif../*.**
85a70 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f The following ro
85a80 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 utines are conve
85a90 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 nience wrappers
85aa0 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a around methods.*
85ab0 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 * of the sqlite3
85ac0 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54 _file object. T
85ad0 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75 his is mostly ju
85ae0 73 74 20 73 79 6e 74 61 63 74 69 63 20 73 75 67 st syntactic sug
85af0 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 ar. All.** of th
85b00 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70 is would be comp
85b10 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74 69 63 letely automatic
85b20 20 69 66 20 53 51 4c 69 74 65 20 77 65 72 65 20 if SQLite were
85b30 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43 coded using.** C
85b40 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20 70 6c ++ instead of pl
85b50 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 ain old C..*/.SQ
85b60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
85b70 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
85b80 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 sqlite3_file *pI
85b90 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 d){. int rc = S
85ba0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
85bb0 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b pId->pMethods ){
85bc0 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d 3e 70 . rc = pId->p
85bd0 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 Methods->xClose(
85be0 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70 pId);. pId->p
85bf0 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d Methods = 0;. }
85c00 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
85c10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
85c20 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 nt sqlite3OsRead
85c30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
85c40 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 d, void *pBuf, i
85c50 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 nt amt, i64 offs
85c60 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c et){. DO_OS_MAL
85c70 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 LOC_TEST;. retu
85c80 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
85c90 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75 66 2c >xRead(id, pBuf,
85ca0 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d amt, offset);.}
85cb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
85cc0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 int sqlite3OsWri
85cd0 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 te(sqlite3_file
85ce0 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 *id, const void
85cf0 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 *pBuf, int amt,
85d00 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 i64 offset){. D
85d10 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
85d20 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 ;. return id->p
85d30 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69 74 65 28 Methods->xWrite(
85d40 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f id, pBuf, amt, o
85d50 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 ffset);.}.SQLITE
85d60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
85d70 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 ite3OsTruncate(s
85d80 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
85d90 20 69 36 34 20 73 69 7a 65 29 7b 0a 20 20 72 65 i64 size){. re
85da0 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
85db0 73 2d 3e 78 54 72 75 6e 63 61 74 65 28 69 64 2c s->xTruncate(id,
85dc0 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 size);.}.SQLITE
85dd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
85de0 69 74 65 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 ite3OsSync(sqlit
85df0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
85e00 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f 5f 4f 53 flags){. DO_OS
85e10 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 _MALLOC_TEST;.
85e20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
85e30 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66 ods->xSync(id, f
85e40 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f lags);.}.SQLITE_
85e50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
85e60 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 te3OsFileSize(sq
85e70 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
85e80 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 i64 *pSize){. D
85e90 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
85ea0 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 ;. return id->p
85eb0 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 Methods->xFileSi
85ec0 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d ze(id, pSize);.}
85ed0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
85ee0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 int sqlite3OsLoc
85ef0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
85f00 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 id, int lockType
85f10 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f ){. DO_OS_MALLO
85f20 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72 6e C_TEST;. return
85f30 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
85f40 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 Lock(id, lockTyp
85f50 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 e);.}.SQLITE_PRI
85f60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
85f70 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 OsUnlock(sqlite3
85f80 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
85f90 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74 75 ockType){. retu
85fa0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
85fb0 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 >xUnlock(id, loc
85fc0 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 kType);.}.SQLITE
85fd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
85fe0 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 ite3OsCheckReser
85ff0 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
86000 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
86010 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53 ResOut){. DO_OS
86020 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 _MALLOC_TEST;.
86030 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
86040 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65 73 65 72 ods->xCheckReser
86050 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70 52 65 73 vedLock(id, pRes
86060 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 Out);.}.SQLITE_P
86070 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
86080 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 e3OsFileControl(
86090 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
860a0 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a , int op, void *
860b0 70 41 72 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 pArg){. return
860c0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 id->pMethods->xF
860d0 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f ileControl(id, o
860e0 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 p, pArg);.}.SQLI
860f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
86100 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 qlite3OsSectorSi
86110 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ze(sqlite3_file
86120 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53 *id){. int (*xS
86130 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 ectorSize)(sqlit
86140 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69 64 2d 3e e3_file*) = id->
86150 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65 63 74 6f pMethods->xSecto
86160 72 53 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 rSize;. return
86170 28 78 53 65 63 74 6f 72 53 69 7a 65 20 3f 20 78 (xSectorSize ? x
86180 53 65 63 74 6f 72 53 69 7a 65 28 69 64 29 20 3a SectorSize(id) :
86190 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
861a0 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b 0a 7d 0a SECTOR_SIZE);.}.
861b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
861c0 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 nt sqlite3OsDevi
861d0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
861e0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a s(sqlite3_file *
861f0 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 id){. return id
86200 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 44 65 76 ->pMethods->xDev
86210 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
86220 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a cs(id);.}../*.**
86230 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 The next group
86240 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 of routines are
86250 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 convenience wrap
86260 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68 65 0a pers around the.
86270 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a ** VFS methods..
86280 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
86290 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f E int sqlite3OsO
862a0 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 pen(. sqlite3_v
862b0 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e fs *pVfs, . con
862c0 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
862d0 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
862e0 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e 74 20 66 *pFile, . int f
862f0 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a 70 46 lags, . int *pF
86300 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f lagsOut.){. DO_
86310 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a OS_MALLOC_TEST;.
86320 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 return pVfs->x
86330 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 Open(pVfs, zPath
86340 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 , pFile, flags,
86350 70 46 6c 61 67 73 4f 75 74 29 3b 0a 7d 0a 53 51 pFlagsOut);.}.SQ
86360 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
86370 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
86380 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
86390 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a fs, const char *
863a0 7a 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53 79 zPath, int dirSy
863b0 6e 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 nc){. return pV
863c0 66 73 2d 3e 78 44 65 6c 65 74 65 28 70 56 66 73 fs->xDelete(pVfs
863d0 2c 20 7a 50 61 74 68 2c 20 64 69 72 53 79 6e 63 , zPath, dirSync
863e0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
863f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
86400 73 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 sAccess(. sqlit
86410 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 e3_vfs *pVfs, .
86420 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
86430 74 68 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 th, . int flags
86440 2c 20 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 , . int *pResOu
86450 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c t.){. DO_OS_MAL
86460 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 LOC_TEST;. retu
86470 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 65 73 73 rn pVfs->xAccess
86480 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c (pVfs, zPath, fl
86490 61 67 73 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d ags, pResOut);.}
864a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
864b0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c int sqlite3OsFul
864c0 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c lPathname(. sql
864d0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
864e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
864f0 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 Path, . int nPa
86500 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a thOut, . char *
86510 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20 72 65 zPathOut.){. re
86520 74 75 72 6e 20 70 56 66 73 2d 3e 78 46 75 6c 6c turn pVfs->xFull
86530 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a Pathname(pVfs, z
86540 50 61 74 68 2c 20 6e 50 61 74 68 4f 75 74 2c 20 Path, nPathOut,
86550 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a 23 69 66 zPathOut);.}.#if
86560 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
86570 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a _LOAD_EXTENSION.
86580 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
86590 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c oid *sqlite3OsDl
865a0 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 Open(sqlite3_vfs
865b0 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 *pVfs, const ch
865c0 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20 20 72 65 ar *zPath){. re
865d0 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c 4f 70 turn pVfs->xDlOp
865e0 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 29 3b en(pVfs, zPath);
865f0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
86600 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 E void sqlite3Os
86610 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f DlError(sqlite3_
86620 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
86630 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 Byte, char *zBuf
86640 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 Out){. pVfs->xD
86650 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 6e 42 79 lError(pVfs, nBy
86660 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a te, zBufOut);.}.
86670 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 void (*sqlite3Os
86680 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 DlSym(sqlite3_vf
86690 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 s *pVfs, void *p
866a0 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 Hdle, const char
866b0 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 7b 0a *zSym))(void){.
866c0 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 return pVfs->x
866d0 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48 64 6c DlSym(pVfs, pHdl
866e0 65 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 53 51 4c 49 e, zSym);.}.SQLI
866f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
86700 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 sqlite3OsDlClose
86710 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
86720 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c fs, void *pHandl
86730 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 43 e){. pVfs->xDlC
86740 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 61 6e 64 lose(pVfs, pHand
86750 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a le);.}.#endif /*
86760 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
86770 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53 D_EXTENSION */.S
86780 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
86790 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f t sqlite3OsRando
867a0 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 mness(sqlite3_vf
867b0 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 s *pVfs, int nBy
867c0 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 te, char *zBufOu
867d0 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 t){. return pVf
867e0 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 73 28 70 s->xRandomness(p
867f0 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66 Vfs, nByte, zBuf
86800 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 Out);.}.SQLITE_P
86810 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
86820 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65 e3OsSleep(sqlite
86830 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
86840 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 65 74 75 nMicro){. retu
86850 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65 70 28 rn pVfs->xSleep(
86860 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d pVfs, nMicro);.}
86870 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
86880 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 int sqlite3OsCur
86890 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 rentTime(sqlite3
868a0 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 _vfs *pVfs, doub
868b0 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b 0a 20 le *pTimeOut){.
868c0 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 43 return pVfs->xC
868d0 75 72 72 65 6e 74 54 69 6d 65 28 70 56 66 73 2c urrentTime(pVfs,
868e0 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a 53 pTimeOut);.}..S
868f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
86900 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d t sqlite3OsOpenM
86910 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 alloc(. sqlite3
86920 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 _vfs *pVfs, . c
86930 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
86940 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c , . sqlite3_fil
86950 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 20 69 e **ppFile, . i
86960 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e 74 20 nt flags,. int
86970 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a 20 20 *pOutFlags.){.
86980 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
86990 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 NOMEM;. sqlite3
869a0 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 _file *pFile;.
869b0 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 pFile = (sqlite3
869c0 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d _file *)sqlite3M
869d0 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 alloc(pVfs->szOs
869e0 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69 File);. if( pFi
869f0 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 le ){. rc = s
86a00 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
86a10 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c 65 2c s, zFile, pFile,
86a20 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61 67 flags, pOutFlag
86a30 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d s);. if( rc!=
86a40 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
86a50 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
86a60 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65 6c 73 pFile);. }els
86a70 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 69 6c 65 e{. *ppFile
86a80 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20 7d 0a = pFile;. }.
86a90 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
86aa0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
86ab0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
86ac0 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65 33 loseFree(sqlite3
86ad0 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 _file *pFile){.
86ae0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
86af0 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 _OK;. assert( p
86b00 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d 20 73 File );. rc = s
86b10 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 46 qlite3OsClose(pF
86b20 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f ile);. sqlite3_
86b30 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 72 free(pFile);. r
86b40 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
86b50 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20 61 ** The list of a
86b60 6c 6c 20 72 65 67 69 73 74 65 72 65 64 20 56 46 ll registered VF
86b70 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e S implementation
86b80 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c s..*/.static sql
86b90 69 74 65 33 5f 76 66 73 20 2a 20 53 51 4c 49 54 ite3_vfs * SQLIT
86ba0 45 5f 57 53 44 20 76 66 73 4c 69 73 74 20 3d 20 E_WSD vfsList =
86bb0 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 73 4c 69 0;.#define vfsLi
86bc0 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 st GLOBAL(sqlite
86bd0 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c 69 73 74 3_vfs *, vfsList
86be0 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 )../*.** Locate
86bf0 61 20 56 46 53 20 62 79 20 6e 61 6d 65 2e 20 20 a VFS by name.
86c00 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67 69 If no name is gi
86c10 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74 75 ven, simply retu
86c20 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 rn the.** first
86c30 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e VFS on the list.
86c40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 .*/.SQLITE_API s
86c50 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 qlite3_vfs *sqli
86c60 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e te3_vfs_find(con
86c70 73 74 20 63 68 61 72 20 2a 7a 56 66 73 29 7b 0a st char *zVfs){.
86c80 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
86c90 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c Vfs = 0;.#if SQL
86ca0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 ITE_THREADSAFE.
86cb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
86cc0 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 23 69 mutex;.#endif.#i
86cd0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
86ce0 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74 T_AUTOINIT. int
86cf0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e rc = sqlite3_in
86d00 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 itialize();. if
86d10 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 30 3b ( rc ) return 0;
86d20 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 .#endif.#if SQLI
86d30 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 TE_THREADSAFE.
86d40 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d mutex = sqlite3M
86d50 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
86d60 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
86d70 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 STER);.#endif.
86d80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
86d90 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f ter(mutex);. fo
86da0 72 28 70 56 66 73 20 3d 20 76 66 73 4c 69 73 74 r(pVfs = vfsList
86db0 3b 20 70 56 66 73 3b 20 70 56 66 73 3d 70 56 66 ; pVfs; pVfs=pVf
86dc0 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 s->pNext){. i
86dd0 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 62 72 65 f( zVfs==0 ) bre
86de0 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 74 72 63 ak;. if( strc
86df0 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 2d 3e 7a mp(zVfs, pVfs->z
86e00 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b Name)==0 ) break
86e10 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
86e20 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
86e30 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 x);. return pVf
86e40 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 s;.}../*.** Unli
86e50 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d 20 74 68 nk a VFS from th
86e60 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 2a 2f e linked list.*/
86e70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 66 73 .static void vfs
86e80 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 33 5f 76 Unlink(sqlite3_v
86e90 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 61 73 73 fs *pVfs){. ass
86ea0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
86eb0 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d ex_held(sqlite3M
86ec0 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
86ed0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
86ee0 53 54 45 52 29 29 20 29 3b 0a 20 20 69 66 28 20 STER)) );. if(
86ef0 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 20 20 2f pVfs==0 ){. /
86f00 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d 65 6c * No-op */. }el
86f10 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 3d 3d se if( vfsList==
86f20 70 56 66 73 20 29 7b 0a 20 20 20 20 76 66 73 4c pVfs ){. vfsL
86f30 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 ist = pVfs->pNex
86f40 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 t;. }else if( v
86f50 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 71 fsList ){. sq
86f60 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 3d 20 76 lite3_vfs *p = v
86f70 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c fsList;. whil
86f80 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 26 20 70 e( p->pNext && p
86f90 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 20 29 7b ->pNext!=pVfs ){
86fa0 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e . p = p->pN
86fb0 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ext;. }. i
86fc0 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 56 66 f( p->pNext==pVf
86fd0 73 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4e s ){. p->pN
86fe0 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 ext = pVfs->pNex
86ff0 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a t;. }. }.}..
87000 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 /*.** Register a
87010 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73 79 VFS with the sy
87020 73 74 65 6d 2e 20 20 49 74 20 69 73 20 68 61 72 stem. It is har
87030 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 73 74 65 mless to registe
87040 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 56 46 r the same.** VF
87050 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 S multiple times
87060 2e 20 20 54 68 65 20 6e 65 77 20 56 46 53 20 62 . The new VFS b
87070 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 61 75 ecomes the defau
87080 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c 74 20 69 lt if makeDflt i
87090 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 s.** true..*/.SQ
870a0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
870b0 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 ite3_vfs_registe
870c0 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 r(sqlite3_vfs *p
870d0 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c Vfs, int makeDfl
870e0 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 t){. sqlite3_mu
870f0 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 30 3b 0a tex *mutex = 0;.
87100 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
87110 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 MIT_AUTOINIT. i
87120 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f nt rc = sqlite3_
87130 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 initialize();.
87140 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
87150 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 6d 75 74 rc;.#endif. mut
87160 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
87170 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
87180 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
87190 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 R);. sqlite3_mu
871a0 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 tex_enter(mutex)
871b0 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 ;. vfsUnlink(pV
871c0 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65 44 fs);. if( makeD
871d0 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d 3d flt || vfsList==
871e0 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 0 ){. pVfs->p
871f0 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 3b 0a Next = vfsList;.
87200 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 vfsList = pV
87210 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 fs;. }else{.
87220 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 pVfs->pNext = v
87230 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 fsList->pNext;.
87240 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 vfsList->pNex
87250 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a 20 20 t = pVfs;. }.
87260 61 73 73 65 72 74 28 76 66 73 4c 69 73 74 29 3b assert(vfsList);
87270 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
87280 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 _leave(mutex);.
87290 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
872a0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72 65 K;.}../*.** Unre
872b0 67 69 73 74 65 72 20 61 20 56 46 53 20 73 6f 20 gister a VFS so
872c0 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20 6c 6f that it is no lo
872d0 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c 65 2e nger accessible.
872e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
872f0 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 nt sqlite3_vfs_u
87300 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 nregister(sqlite
87310 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 23 69 3_vfs *pVfs){.#i
87320 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
87330 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 AFE. sqlite3_mu
87340 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c tex *mutex = sql
87350 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
87360 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
87370 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 IC_MASTER);.#end
87380 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 if. sqlite3_mut
87390 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b ex_enter(mutex);
873a0 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 . vfsUnlink(pVf
873b0 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 s);. sqlite3_mu
873c0 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 tex_leave(mutex)
873d0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
873e0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a E_OK;.}../******
873f0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
87400 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os.c ***********
87410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87430 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
87440 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
87450 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a ile fault.c ****
87460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87480 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
87490 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a 2a 008 Jan 22.**.**
874a0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
874b0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
874c0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
874d0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
874e0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
874f0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
87500 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
87510 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
87520 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
87530 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
87540 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
87550 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
87560 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
87570 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
87580 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
87590 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
875a0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
875b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
875c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
875d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
875e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
875f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
87600 24 49 64 3a 20 66 61 75 6c 74 2e 63 2c 76 20 31 $Id: fault.c,v 1
87610 2e 31 31 20 32 30 30 38 2f 30 39 2f 30 32 20 30 .11 2008/09/02 0
87620 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78 70 20 0:52:52 drh Exp
87630 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 $.*/../*.** This
87640 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
87650 6f 64 65 20 74 6f 20 73 75 70 70 6f 72 74 20 74 ode to support t
87660 68 65 20 63 6f 6e 63 65 70 74 20 6f 66 20 22 62 he concept of "b
87670 65 6e 69 67 6e 22 20 0a 2a 2a 20 6d 61 6c 6c 6f enign" .** mallo
87680 63 20 66 61 69 6c 75 72 65 73 20 28 77 68 65 6e c failures (when
87690 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 6f the xMalloc() o
876a0 72 20 78 52 65 61 6c 6c 6f 63 28 29 20 6d 65 74 r xRealloc() met
876b0 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 73 71 hod of the.** sq
876c0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
876d0 73 20 73 74 72 75 63 74 75 72 65 20 66 61 69 6c s structure fail
876e0 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 s to allocate a
876f0 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a block of memory.
87700 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30 ** and returns 0
87710 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6d ). .**.** Most m
87720 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 alloc failures a
87730 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 20 41 re non-benign. A
87740 66 74 65 72 20 74 68 65 79 20 6f 63 63 75 72 2c fter they occur,
87750 20 53 51 4c 69 74 65 0a 2a 2a 20 61 62 61 6e 64 SQLite.** aband
87760 6f 6e 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 ons the current
87770 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 72 65 operation and re
87780 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 turns an error c
87790 6f 64 65 20 28 75 73 75 61 6c 6c 79 0a 2a 2a 20 ode (usually.**
877a0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20 74 6f SQLITE_NOMEM) to
877b0 20 74 68 65 20 75 73 65 72 2e 20 48 6f 77 65 76 the user. Howev
877c0 65 72 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 20 er, sometimes a
877d0 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 6e 65 63 fault is not nec
877e0 65 73 73 61 72 69 6c 79 0a 2a 2a 20 66 61 74 61 essarily.** fata
877f0 6c 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 l. For example,
87800 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c if a malloc fail
87810 73 20 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 s while resizing
87820 20 61 20 68 61 73 68 20 74 61 62 6c 65 2c 20 74 a hash table, t
87830 68 69 73 20 0a 2a 2a 20 69 73 20 63 6f 6d 70 6c his .** is compl
87840 65 74 65 6c 79 20 72 65 63 6f 76 65 72 61 62 6c etely recoverabl
87850 65 20 73 69 6d 70 6c 79 20 62 79 20 6e 6f 74 20 e simply by not
87860 63 61 72 72 79 69 6e 67 20 6f 75 74 20 74 68 65 carrying out the
87870 20 72 65 73 69 7a 65 2e 20 54 68 65 20 0a 2a 2a resize. The .**
87880 20 68 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c hash table will
87890 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 66 75 6e continue to fun
878a0 63 74 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79 2e 20 ction normally.
878b0 20 53 6f 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 So a malloc fai
878c0 6c 75 72 65 20 0a 2a 2a 20 64 75 72 69 6e 67 20 lure .** during
878d0 61 20 68 61 73 68 20 74 61 62 6c 65 20 72 65 73 a hash table res
878e0 69 7a 65 20 69 73 20 61 20 62 65 6e 69 67 6e 20 ize is a benign
878f0 66 61 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 69 66 6e fault..*/...#ifn
87900 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
87910 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 0a 2f 2a BUILTIN_TEST../*
87920 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 .** Global varia
87930 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 bles..*/.typedef
87940 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 struct BenignMa
87950 6c 6c 6f 63 48 6f 6f 6b 73 20 42 65 6e 69 67 6e llocHooks Benign
87960 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a 73 74 61 MallocHooks;.sta
87970 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 tic SQLITE_WSD s
87980 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c truct BenignMall
87990 6f 63 48 6f 6f 6b 73 20 7b 0a 20 20 76 6f 69 64 ocHooks {. void
879a0 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 (*xBenignBegin)
879b0 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 28 (void);. void (
879c0 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 *xBenignEnd)(voi
879d0 64 29 3b 0a 7d 20 73 71 6c 69 74 65 33 48 6f 6f d);.} sqlite3Hoo
879e0 6b 73 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a ks = { 0, 0 };..
879f0 2f 2a 20 54 68 65 20 22 77 73 64 48 6f 6f 6b 73 /* The "wsdHooks
87a00 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 " macro will res
87a10 6f 6c 76 65 20 74 6f 20 74 68 65 20 61 70 70 72 olve to the appr
87a20 6f 70 72 69 61 74 65 20 42 65 6e 69 67 6e 4d 61 opriate BenignMa
87a30 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 73 74 72 llocHooks.** str
87a40 75 63 74 75 72 65 2e 20 20 49 66 20 77 72 69 74 ucture. If writ
87a50 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 able static data
87a60 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 is unsupported
87a70 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a on the target,.*
87a80 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 * we have to loc
87a90 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65 ate the state ve
87aa0 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 ctor at run-time
87ab0 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 . In the more c
87ac0 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 ommon.** case wh
87ad0 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 ere writable sta
87ae0 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 tic data is supp
87af0 6f 72 74 65 64 2c 20 77 73 64 48 6f 6f 6b 73 20 orted, wsdHooks
87b00 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 can refer direct
87b10 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 ly.** to the "sq
87b20 6c 69 74 65 33 48 6f 6f 6b 73 22 20 73 74 61 74 lite3Hooks" stat
87b30 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 e vector declare
87b40 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 d above..*/.#ifd
87b50 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 ef SQLITE_OMIT_W
87b60 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 SD.# define wsdH
87b70 6f 6f 6b 73 49 6e 69 74 20 5c 0a 20 20 42 65 6e ooksInit \. Ben
87b80 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 2a ignMallocHooks *
87b90 78 20 3d 20 26 47 4c 4f 42 41 4c 28 42 65 6e 69 x = &GLOBAL(Beni
87ba0 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 2c 73 71 gnMallocHooks,sq
87bb0 6c 69 74 65 33 48 6f 6f 6b 73 29 0a 23 20 64 65 lite3Hooks).# de
87bc0 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 20 78 5b fine wsdHooks x[
87bd0 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 0].#else.# defin
87be0 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 0a 23 e wsdHooksInit.#
87bf0 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 define wsdHooks
87c00 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 0a 23 65 sqlite3Hooks.#e
87c10 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 ndif.../*.** Reg
87c20 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20 63 ister hooks to c
87c30 61 6c 6c 20 77 68 65 6e 20 73 71 6c 69 74 65 33 all when sqlite3
87c40 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f BeginBenignMallo
87c50 63 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 c() and.** sqlit
87c60 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
87c70 63 28 29 20 61 72 65 20 63 61 6c 6c 65 64 2c 20 c() are called,
87c80 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f respectively..*/
87c90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
87ca0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 6e 69 void sqlite3Beni
87cb0 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 0a 20 gnMallocHooks(.
87cc0 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 void (*xBenignB
87cd0 65 67 69 6e 29 28 76 6f 69 64 29 2c 0a 20 20 76 egin)(void),. v
87ce0 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 oid (*xBenignEnd
87cf0 29 28 76 6f 69 64 29 0a 29 7b 0a 20 20 77 73 64 )(void).){. wsd
87d00 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 77 73 64 HooksInit;. wsd
87d10 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 Hooks.xBenignBeg
87d20 69 6e 20 3d 20 78 42 65 6e 69 67 6e 42 65 67 69 in = xBenignBegi
87d30 6e 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 n;. wsdHooks.xB
87d40 65 6e 69 67 6e 45 6e 64 20 3d 20 78 42 65 6e 69 enignEnd = xBeni
87d50 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 gnEnd;.}../*.**
87d60 54 68 69 73 20 28 73 71 6c 69 74 65 33 45 6e 64 This (sqlite3End
87d70 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 29 20 BenignMalloc())
87d80 69 73 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c is called by SQL
87d90 69 74 65 20 63 6f 64 65 20 74 6f 20 69 6e 64 69 ite code to indi
87da0 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 73 75 62 cate that.** sub
87db0 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 sequent malloc f
87dc0 61 69 6c 75 72 65 73 20 61 72 65 20 62 65 6e 69 ailures are beni
87dd0 67 6e 2e 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 gn. A call to sq
87de0 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
87df0 6c 6c 6f 63 28 29 0a 2a 2a 20 69 6e 64 69 63 61 lloc().** indica
87e00 74 65 73 20 74 68 61 74 20 73 75 62 73 65 71 75 tes that subsequ
87e10 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 ent malloc failu
87e20 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 res are non-beni
87e30 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 gn..*/.SQLITE_PR
87e40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
87e50 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
87e60 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 loc(void){. wsd
87e70 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 HooksInit;. if(
87e80 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 wsdHooks.xBenig
87e90 6e 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 77 73 nBegin ){. ws
87ea0 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 dHooks.xBenignBe
87eb0 67 69 6e 28 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c gin();. }.}.SQL
87ec0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
87ed0 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
87ee0 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 nMalloc(void){.
87ef0 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 wsdHooksInit;.
87f00 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 if( wsdHooks.xB
87f10 65 6e 69 67 6e 45 6e 64 20 29 7b 0a 20 20 20 20 enignEnd ){.
87f20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e wsdHooks.xBenign
87f30 45 6e 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 End();. }.}..#e
87f40 6e 64 69 66 20 20 20 2f 2a 20 23 69 66 6e 64 65 ndif /* #ifnde
87f50 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
87f60 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f ILTIN_TEST */../
87f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
87f80 6e 64 20 6f 66 20 66 61 75 6c 74 2e 63 20 2a 2a nd of fault.c **
87f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
87fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
87fd0 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 30 2e 63 egin file mem0.c
87fe0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
87ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
88010 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 *.** 2008 Octobe
88020 72 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 28.**.** The a
88030 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
88040 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
88050 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
88060 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
88070 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
88080 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
88090 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
880a0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
880b0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
880c0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
880d0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
880e0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
880f0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
88100 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
88110 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
88120 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
88130 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
88140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88180 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
88190 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 6e ile contains a n
881a0 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f o-op memory allo
881b0 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 cation drivers f
881c0 6f 72 20 75 73 65 20 77 68 65 6e 0a 2a 2a 20 53 or use when.** S
881d0 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f QLITE_ZERO_MALLO
881e0 43 20 69 73 20 64 65 66 69 6e 65 64 2e 20 20 54 C is defined. T
881f0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 he allocation dr
88200 69 76 65 72 73 20 69 6d 70 6c 65 6d 65 6e 74 65 ivers implemente
88210 64 0a 2a 2a 20 68 65 72 65 20 61 6c 77 61 79 73 d.** here always
88220 20 66 61 69 6c 2e 20 20 53 51 4c 69 74 65 20 77 fail. SQLite w
88230 69 6c 6c 20 6e 6f 74 20 6f 70 65 72 61 74 65 20 ill not operate
88240 77 69 74 68 20 74 68 65 73 65 20 64 72 69 76 65 with these drive
88250 72 73 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 rs. These.** ar
88260 65 20 6d 65 72 65 6c 79 20 70 6c 61 63 65 68 6f e merely placeho
88270 6c 64 65 72 73 2e 20 20 52 65 61 6c 20 64 72 69 lders. Real dri
88280 76 65 72 73 20 6d 75 73 74 20 62 65 20 73 75 62 vers must be sub
88290 73 74 69 74 75 74 65 64 20 75 73 69 6e 67 0a 2a stituted using.*
882a0 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 * sqlite3_config
882b0 28 29 20 62 65 66 6f 72 65 20 53 51 4c 69 74 65 () before SQLite
882c0 20 77 69 6c 6c 20 6f 70 65 72 61 74 65 2e 0a 2a will operate..*
882d0 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 30 2e 63 *.** $Id: mem0.c
882e0 2c 76 20 31 2e 31 20 32 30 30 38 2f 31 30 2f 32 ,v 1.1 2008/10/2
882f0 38 20 31 38 3a 35 38 3a 32 30 20 64 72 68 20 45 8 18:58:20 drh E
88300 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 xp $.*/../*.** T
88310 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 his version of t
88320 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
88330 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 61 75 tor is the defau
88340 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 lt. It is.** us
88350 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 ed when no other
88360 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
88370 72 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75 r is specified u
88380 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d sing compile-tim
88390 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a e.** macros..*/.
883a0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45 #ifdef SQLITE_ZE
883b0 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a RO_MALLOC../*.**
883c0 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 No-op versions
883d0 6f 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c of all memory al
883e0 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 location routine
883f0 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 s.*/.static void
88400 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c *sqlite3MemMall
88410 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 oc(int nByte){ r
88420 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 eturn 0; }.stati
88430 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 c void sqlite3Me
88440 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 mFree(void *pPri
88450 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 or){ return; }.s
88460 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 tatic void *sqli
88470 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f te3MemRealloc(vo
88480 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 id *pPrior, int
88490 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 nByte){ return 0
884a0 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 ; }.static int s
884b0 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f qlite3MemSize(vo
884c0 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 id *pPrior){ ret
884d0 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 urn 0; }.static
884e0 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f int sqlite3MemRo
884f0 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 65 undup(int n){ re
88500 74 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 63 turn n; }.static
88510 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 int sqlite3MemI
88520 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 nit(void *NotUse
88530 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 d){ return SQLIT
88540 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 E_OK; }.static v
88550 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 oid sqlite3MemSh
88560 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 utdown(void *Not
88570 55 73 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 7d Used){ return; }
88580 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
88590 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 tine is the only
885a0 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 routine in this
885b0 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 file with exter
885c0 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a nal linkage..**.
885d0 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 ** Populate the
885e0 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
885f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 allocation func
88600 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e tion pointers in
88610 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 .** sqlite3Globa
88620 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 lConfig.m with p
88630 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 ointers to the r
88640 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 outines in this
88650 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f file..*/.SQLITE_
88660 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
88670 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c ite3MemSetDefaul
88680 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 t(void){. stati
88690 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
886a0 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 mem_methods defa
886b0 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 ultMethods = {.
886c0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 sqlite3MemMa
886d0 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 lloc,. sqlit
886e0 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 e3MemFree,.
886f0 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f sqlite3MemReallo
88700 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d c,. sqlite3M
88710 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c emSize,. sql
88720 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a ite3MemRoundup,.
88730 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 sqlite3MemI
88740 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 nit,. sqlite
88750 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 3MemShutdown,.
88760 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 0. };. sqli
88770 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 te3_config(SQLIT
88780 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c E_CONFIG_MALLOC,
88790 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 &defaultMethods
887a0 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 );.}..#endif /*
887b0 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c SQLITE_ZERO_MALL
887c0 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a OC */../********
887d0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 ****** End of me
887e0 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m0.c ***********
887f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88810 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
88820 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
88830 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a e mem1.c *******
88840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88860 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
88870 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 7 August 14.**.*
88880 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
88890 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
888a0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
888b0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
888c0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
888d0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
888e0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
888f0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
88900 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
88910 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
88920 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
88930 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
88940 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
88950 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
88960 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
88970 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
88980 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
88990 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
889a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
889b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
889c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
889d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
889e0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
889f0 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 ins low-level me
88a00 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
88a10 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e drivers for when
88a20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 .** SQLite will
88a30 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 use the standard
88a40 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f C-library mallo
88a50 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 c/realloc/free i
88a60 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f nterface.** to o
88a70 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 btain the memory
88a80 20 69 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a it needs..**.**
88a90 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
88aa0 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ins implementati
88ab0 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c ons of the low-l
88ac0 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f evel memory allo
88ad0 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e cation.** routin
88ae0 65 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 es specified in
88af0 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f the sqlite3_mem_
88b00 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a methods object..
88b10 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 31 2e **.** $Id: mem1.
88b20 63 2c 76 20 31 2e 32 39 20 32 30 30 38 2f 31 32 c,v 1.29 2008/12
88b30 2f 31 30 20 32 31 3a 31 39 3a 35 37 20 64 72 68 /10 21:19:57 drh
88b40 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
88b50 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
88b60 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
88b70 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 cator is the def
88b80 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 ault. It is.**
88b90 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 used when no oth
88ba0 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 er memory alloca
88bb0 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64 tor is specified
88bc0 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 using compile-t
88bd0 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a ime.** macros..*
88be0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
88bf0 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a 2f SYSTEM_MALLOC../
88c00 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f 63 *.** Like malloc
88c10 28 29 2c 20 62 75 74 20 72 65 6d 65 6d 62 65 72 (), but remember
88c20 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
88c30 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 allocation.** s
88c40 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 66 69 o that we can fi
88c50 6e 64 20 69 74 20 6c 61 74 65 72 20 75 73 69 6e nd it later usin
88c60 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 g sqlite3MemSize
88c70 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 ()..**.** For th
88c80 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 is low-level rou
88c90 74 69 6e 65 2c 20 77 65 20 61 72 65 20 67 75 61 tine, we are gua
88ca0 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 42 79 ranteed that nBy
88cb0 74 65 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a 20 te>0 because.**
88cc0 63 61 73 65 73 20 6f 66 20 6e 42 79 74 65 3c 3d cases of nByte<=
88cd0 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 63 0 will be interc
88ce0 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20 epted and dealt
88cf0 77 69 74 68 20 62 79 20 68 69 67 68 65 72 20 6c with by higher l
88d00 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 evel.** routines
88d10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
88d20 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c *sqlite3MemMall
88d30 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 oc(int nByte){.
88d40 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a sqlite3_int64 *
88d50 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 p;. assert( nBy
88d60 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 te>0 );. nByte
88d70 3d 20 28 6e 42 79 74 65 2b 37 29 26 7e 37 3b 0a = (nByte+7)&~7;.
88d80 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 p = malloc( nB
88d90 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 yte+8 );. if( p
88da0 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e ){. p[0] = n
88db0 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 Byte;. p++;.
88dc0 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 }. return (voi
88dd0 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 d *)p;.}../*.**
88de0 4c 69 6b 65 20 66 72 65 65 28 29 20 62 75 74 20 Like free() but
88df0 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f 63 61 works for alloca
88e00 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64 20 66 tions obtained f
88e10 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 rom sqlite3MemMa
88e20 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73 71 6c lloc().** or sql
88e30 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 29 ite3MemRealloc()
88e40 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 ..**.** For this
88e50 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 low-level routi
88e60 6e 65 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b ne, we already k
88e70 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72 21 now that pPrior!
88e80 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61 73 65 =0 since.** case
88e90 73 20 77 68 65 72 65 20 70 50 72 69 6f 72 3d 3d s where pPrior==
88ea0 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 0 will have been
88eb0 20 69 6e 74 65 63 65 70 74 65 64 20 61 6e 64 20 intecepted and
88ec0 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20 62 79 dealt with.** by
88ed0 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f higher-level ro
88ee0 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 utines..*/.stati
88ef0 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 c void sqlite3Me
88f00 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 mFree(void *pPri
88f10 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 or){. sqlite3_i
88f20 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 nt64 *p = (sqlit
88f30 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 e3_int64*)pPrior
88f40 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 ;. assert( pPri
88f50 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d 3b 0a or!=0 );. p--;.
88f60 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a free(p);.}../*
88f70 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c 6f 63 .** Like realloc
88f80 28 29 2e 20 20 52 65 73 69 7a 65 20 61 6e 20 61 (). Resize an a
88f90 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f llocation previo
88fa0 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 usly obtained fr
88fb0 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d om.** sqlite3Mem
88fc0 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 Malloc()..**.**
88fd0 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 For this low-lev
88fe0 65 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 77 65 el interface, we
88ff0 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f know that pPrio
89000 72 21 3d 30 2e 20 20 43 61 73 65 73 20 77 68 65 r!=0. Cases whe
89010 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d 30 20 re.** pPrior==0
89020 77 68 69 6c 65 20 68 61 76 65 20 62 65 65 6e 20 while have been
89030 69 6e 74 65 72 63 65 70 74 65 64 20 62 79 20 68 intercepted by h
89040 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 igher-level rout
89050 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 69 72 ine and.** redir
89060 65 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f 63 ected to xMalloc
89070 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77 65 . Similarly, we
89080 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 74 65 know that nByte
89090 3e 30 20 62 65 63 61 75 73 65 73 0a 2a 2a 20 63 >0 becauses.** c
890a0 61 73 65 73 20 77 68 65 72 65 20 6e 42 79 74 65 ases where nByte
890b0 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 <=0 will have be
890c0 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 en intercepted b
890d0 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0a 2a y higher-level.*
890e0 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 72 * routines and r
890f0 65 64 69 72 65 63 74 65 64 20 74 6f 20 78 46 72 edirected to xFr
89100 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ee..*/.static vo
89110 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 id *sqlite3MemRe
89120 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 alloc(void *pPri
89130 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a or, int nByte){.
89140 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
89150 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e *p = (sqlite3_in
89160 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 t64*)pPrior;. a
89170 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 ssert( pPrior!=0
89180 20 26 26 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 && nByte>0 );.
89190 20 6e 42 79 74 65 20 3d 20 28 6e 42 79 74 65 2b nByte = (nByte+
891a0 37 29 26 7e 37 3b 0a 20 20 70 20 3d 20 28 73 71 7)&~7;. p = (sq
891b0 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 lite3_int64*)pPr
891c0 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 ior;. p--;. p
891d0 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 42 79 = realloc(p, nBy
891e0 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 20 te+8 );. if( p
891f0 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42 ){. p[0] = nB
89200 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 yte;. p++;.
89210 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 }. return (void
89220 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 *)p;.}../*.** Re
89230 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 port the allocat
89240 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70 72 69 ed size of a pri
89250 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 78 or return from x
89260 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 78 Malloc().** or x
89270 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 Realloc()..*/.st
89280 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
89290 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 MemSize(void *pP
892a0 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 rior){. sqlite3
892b0 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 69 66 28 _int64 *p;. if(
892c0 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 72 65 74 pPrior==0 ) ret
892d0 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 28 73 71 urn 0;. p = (sq
892e0 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 lite3_int64*)pPr
892f0 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 72 65 ior;. p--;. re
89300 74 75 72 6e 20 28 69 6e 74 29 70 5b 30 5d 3b 0a turn (int)p[0];.
89310 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 }../*.** Round u
89320 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 p a request size
89330 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c to the next val
89340 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 id allocation si
89350 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ze..*/.static in
89360 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e t sqlite3MemRoun
89370 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65 dup(int n){. re
89380 74 75 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37 3b turn (n+7) & ~7;
89390 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
893a0 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 lize this module
893b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
893c0 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 sqlite3MemInit(v
893d0 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 oid *NotUsed){.
893e0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
893f0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 R(NotUsed);. re
89400 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
89410 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 }../*.** Deiniti
89420 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c alize this modul
89430 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
89440 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 d sqlite3MemShut
89450 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 down(void *NotUs
89460 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ed){. UNUSED_PA
89470 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
89480 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f ;. return;.}../
89490 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
894a0 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f e is the only ro
894b0 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 utine in this fi
894c0 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c le with external
894d0 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 linkage..**.**
894e0 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 Populate the low
894f0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
89500 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f location functio
89510 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a n pointers in.**
89520 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
89530 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e nfig.m with poin
89540 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 ters to the rout
89550 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c ines in this fil
89560 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
89570 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
89580 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76 3MemSetDefault(v
89590 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 oid){. static c
895a0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d onst sqlite3_mem
895b0 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c 74 _methods default
895c0 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 Methods = {.
895d0 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f sqlite3MemMallo
895e0 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d c,. sqlite3M
895f0 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 6c emFree,. sql
89600 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a ite3MemRealloc,.
89610 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 sqlite3MemS
89620 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 ize,. sqlite
89630 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 20 3MemRoundup,.
89640 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 sqlite3MemInit
89650 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
89660 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 mShutdown,.
89670 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 0. };. sqlite3
89680 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 _config(SQLITE_C
89690 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 ONFIG_MALLOC, &d
896a0 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a efaultMethods);.
896b0 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
896c0 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f ITE_SYSTEM_MALLO
896d0 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a C */../*********
896e0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d ***** End of mem
896f0 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 1.c ************
89700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89720 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
89730 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
89740 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mem2.c ********
89750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89770 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
89780 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a 2a August 15.**.**
89790 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
897a0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
897b0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
897c0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
897d0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
897e0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
897f0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
89800 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
89810 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
89820 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
89830 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
89840 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
89850 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
89860 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
89870 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
89880 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
89890 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
898a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
898b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
898c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
898d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
898e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
898f0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
89900 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d ns low-level mem
89910 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 ory allocation d
89920 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a rivers for when.
89930 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 ** SQLite will u
89940 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 se the standard
89950 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 C-library malloc
89960 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e /realloc/free in
89970 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 terface.** to ob
89980 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 tain the memory
89990 69 74 20 6e 65 65 64 73 20 77 68 69 6c 65 20 61 it needs while a
899a0 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 61 64 dding lots of ad
899b0 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 67 69 ditional debuggi
899c0 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f ng.** informatio
899d0 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f 63 61 n to each alloca
899e0 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74 6f tion in order to
899f0 20 68 65 6c 70 20 64 65 74 65 63 74 20 61 6e 64 help detect and
89a00 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a 20 6c fix memory.** l
89a10 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 79 20 eaks and memory
89a20 75 73 61 67 65 20 65 72 72 6f 72 73 2e 0a 2a 2a usage errors..**
89a30 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
89a40 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 ntains implement
89a50 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f ations of the lo
89a60 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
89a70 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 llocation.** rou
89a80 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 tines specified
89a90 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d in the sqlite3_m
89aa0 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 em_methods objec
89ab0 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 t..**.** $Id: me
89ac0 6d 32 2e 63 2c 76 20 31 2e 34 32 20 32 30 30 38 m2.c,v 1.42 2008
89ad0 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 20 /12/10 19:26:24
89ae0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
89af0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
89b00 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
89b10 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 llocator is used
89b20 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 only if the.**
89b30 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 SQLITE_MEMDEBUG
89b40 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 macro is defined
89b50 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
89b60 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a 0a 2a E_MEMDEBUG../*.*
89b70 2a 20 54 68 65 20 62 61 63 6b 74 72 61 63 65 20 * The backtrace
89b80 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 functionality is
89b90 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
89ba0 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a 23 69 with GLIBC.*/.#i
89bb0 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f 0a 20 fdef __GLIBC__.
89bc0 20 65 78 74 65 72 6e 20 69 6e 74 20 62 61 63 6b extern int back
89bd0 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69 6e 74 trace(void**,int
89be0 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f 69 64 );. extern void
89bf0 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f backtrace_symbo
89c00 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e 73 74 ls_fd(void*const
89c10 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65 6c 73 *,int,int);.#els
89c20 65 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b 74 e.# define backt
89c30 72 61 63 65 28 41 2c 42 29 20 31 0a 23 20 64 65 race(A,B) 1.# de
89c40 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 5f 73 fine backtrace_s
89c50 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c 43 29 ymbols_fd(A,B,C)
89c60 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 .#endif../*.** E
89c70 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ach memory alloc
89c80 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c 69 6b 65 ation looks like
89c90 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 2d 2d this:.**.** --
89ca0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
89cb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
89cc0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
89cd0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
89ce0 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 54 69 74 ------.** | Tit
89cf0 6c 65 20 7c 20 20 62 61 63 6b 74 72 61 63 65 20 le | backtrace
89d00 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d 65 6d 42 pointers | MemB
89d10 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c 6c 6f 63 lockHdr | alloc
89d20 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47 75 61 72 ation | EndGuar
89d30 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d d |.** --------
89d40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
89d50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
89d60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
89d70 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
89d80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 .**.** The appli
89d90 63 61 74 69 6f 6e 20 63 6f 64 65 20 73 65 65 73 cation code sees
89da0 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 only a pointer
89db0 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f to the allocatio
89dc0 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a 2a 20 74 n. We have.** t
89dd0 6f 20 62 61 63 6b 20 75 70 20 66 72 6f 6d 20 74 o back up from t
89de0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 6f he allocation po
89df0 69 6e 74 65 72 20 74 6f 20 66 69 6e 64 20 74 68 inter to find th
89e00 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 20 20 e MemBlockHdr.
89e10 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 The.** MemBlockH
89e20 64 72 20 74 65 6c 6c 73 20 75 73 20 74 68 65 20 dr tells us the
89e30 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f size of the allo
89e40 63 61 74 69 6f 6e 20 61 6e 64 20 74 68 65 20 6e cation and the n
89e50 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 61 63 6b umber of.** back
89e60 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73 2e 20 trace pointers.
89e70 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61 There is also a
89e80 20 67 75 61 72 64 20 77 6f 72 64 20 61 74 20 74 guard word at t
89e90 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a he end of the.**
89ea0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 0a 2a 2f MemBlockHdr..*/
89eb0 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b .struct MemBlock
89ec0 48 64 72 20 7b 0a 20 20 69 36 34 20 69 53 69 7a Hdr {. i64 iSiz
89ed0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
89ee0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
89ef0 69 7a 65 20 6f 66 20 74 68 69 73 20 61 6c 6c 6f ize of this allo
89f00 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 cation */. stru
89f10 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
89f20 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 20 pNext, *pPrev;
89f30 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f /* Linked list o
89f40 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 f all unfreed me
89f50 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61 72 20 6e mory */. char n
89f60 42 61 63 6b 74 72 61 63 65 3b 20 20 20 20 20 20 Backtrace;
89f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
89f80 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 74 Number of backt
89f90 72 61 63 65 73 20 6f 6e 20 74 68 69 73 20 61 6c races on this al
89fa0 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 6e 42 loc */. char nB
89fb0 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 20 20 acktraceSlots;
89fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
89fd0 41 76 61 69 6c 61 62 6c 65 20 62 61 63 6b 74 72 Available backtr
89fe0 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a 20 20 73 ace slots */. s
89ff0 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20 20 20 20 hort nTitle;
8a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8a010 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 74 /* Bytes of t
8a020 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65 73 20 27 itle; includes '
8a030 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20 69 46 6f \0' */. int iFo
8a040 72 65 47 75 61 72 64 3b 20 20 20 20 20 20 20 20 reGuard;
8a050 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
8a060 47 75 61 72 64 20 77 6f 72 64 20 66 6f 72 20 73 Guard word for s
8a070 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a anity */.};../*.
8a080 2a 2a 20 47 75 61 72 64 20 77 6f 72 64 73 0a 2a ** Guard words.*
8a090 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52 45 47 55 /.#define FOREGU
8a0a0 41 52 44 20 30 78 38 30 46 35 45 31 35 33 0a 23 ARD 0x80F5E153.#
8a0b0 64 65 66 69 6e 65 20 52 45 41 52 47 55 41 52 44 define REARGUARD
8a0c0 20 30 78 45 34 36 37 36 42 35 33 0a 0a 2f 2a 0a 0xE4676B53../*.
8a0d0 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 6c ** Number of mal
8a0e0 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72 65 6d 65 loc size increme
8a0f0 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e 0a 2a 2f nts to track..*/
8a100 0a 23 64 65 66 69 6e 65 20 4e 43 53 49 5a 45 20 .#define NCSIZE
8a110 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 1000../*.** All
8a120 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 76 of the static v
8a130 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 ariables used by
8a140 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 this module are
8a150 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e collected.** in
8a160 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 to a single stru
8a170 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d cture named "mem
8a180 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b ". This is to k
8a190 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 eep the.** stati
8a1a0 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61 c variables orga
8a1b0 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64 nized and to red
8a1c0 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f uce namespace po
8a1d0 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 llution.** when
8a1e0 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 this module is c
8a1f0 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 ombined with oth
8a200 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 er in the amalga
8a210 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 mation..*/.stati
8a220 63 20 73 74 72 75 63 74 20 7b 0a 20 20 0a 20 20 c struct {. .
8a230 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f /*. ** Mutex to
8a240 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 control access
8a250 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c to the memory al
8a260 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
8a270 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 em.. */. sqlit
8a280 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b e3_mutex *mutex;
8a290 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48 65 61 64 .. /*. ** Head
8a2a0 20 61 6e 64 20 74 61 69 6c 20 6f 66 20 61 20 6c and tail of a l
8a2b0 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c inked list of al
8a2c0 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c l outstanding al
8a2d0 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2f 0a 20 locations. */.
8a2e0 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b struct MemBlock
8a2f0 48 64 72 20 2a 70 46 69 72 73 74 3b 0a 20 20 73 Hdr *pFirst;. s
8a300 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 truct MemBlockHd
8a310 72 20 2a 70 4c 61 73 74 3b 0a 20 20 0a 20 20 2f r *pLast;. . /
8a320 2a 0a 20 20 2a 2a 20 54 68 65 20 6e 75 6d 62 65 *. ** The numbe
8a330 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20 62 r of levels of b
8a340 61 63 6b 74 72 61 63 65 20 74 6f 20 73 61 76 65 acktrace to save
8a350 20 69 6e 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 in new allocati
8a360 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 ons.. */. int
8a370 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 76 6f nBacktrace;. vo
8a380 69 64 20 28 2a 78 42 61 63 6b 74 72 61 63 65 29 id (*xBacktrace)
8a390 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 (int, int, void
8a3a0 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 **);.. /*. **
8a3b0 54 69 74 6c 65 20 74 65 78 74 20 74 6f 20 69 6e Title text to in
8a3c0 73 65 72 74 20 69 6e 20 66 72 6f 6e 74 20 6f 66 sert in front of
8a3d0 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20 20 2a 2f each block. */
8a3e0 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65 3b 20 20 . int nTitle;
8a3f0 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f /* Bytes o
8a400 66 20 7a 54 69 74 6c 65 20 74 6f 20 73 61 76 65 f zTitle to save
8a410 2e 20 20 49 6e 63 6c 75 64 65 73 20 27 5c 30 27 . Includes '\0'
8a420 20 61 6e 64 20 70 61 64 64 69 6e 67 20 2a 2f 0a and padding */.
8a430 20 20 63 68 61 72 20 7a 54 69 74 6c 65 5b 31 30 char zTitle[10
8a440 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74 69 74 6c 0]; /* The titl
8a450 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20 2f 2a 20 e text */.. /*
8a460 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c . ** sqlite3Mal
8a470 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 20 69 6e locDisallow() in
8a480 63 72 65 6d 65 6e 74 73 20 74 68 65 20 66 6f 6c crements the fol
8a490 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 65 72 2e 0a lowing counter..
8a4a0 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c ** sqlite3Mall
8a4b0 6f 63 41 6c 6c 6f 77 28 29 20 64 65 63 72 65 6d ocAllow() decrem
8a4c0 65 6e 74 73 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 ents it.. */.
8a4d0 69 6e 74 20 64 69 73 61 6c 6c 6f 77 3b 20 2f 2a int disallow; /*
8a4e0 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 6d 65 Do not allow me
8a4f0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
8a500 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 47 61 */.. /*. ** Ga
8a510 74 68 65 72 20 73 74 61 74 69 73 74 69 63 73 20 ther statistics
8a520 6f 6e 20 74 68 65 20 73 69 7a 65 73 20 6f 66 20 on the sizes of
8a530 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
8a540 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c 6f 63 5b ns.. ** nAlloc[
8a550 69 5d 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 i] is the number
8a560 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 of allocation a
8a570 74 74 65 6d 70 74 73 20 6f 66 20 69 2a 38 0a 20 ttempts of i*8.
8a580 20 2a 2a 20 62 79 74 65 73 2e 20 20 69 3d 3d 4e ** bytes. i==N
8a590 43 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d CSIZE is the num
8a5a0 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f ber of allocatio
8a5b0 6e 20 61 74 74 65 6d 70 74 73 20 66 6f 72 0a 20 n attempts for.
8a5c0 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 65 20 74 ** sizes more t
8a5d0 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20 62 79 74 han NCSIZE*8 byt
8a5e0 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e es.. */. int n
8a5f0 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d 3b 20 20 Alloc[NCSIZE];
8a600 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
8a610 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f ber of allocatio
8a620 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 ns */. int nCur
8a630 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20 20 rent[NCSIZE];
8a640 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 /* Current numb
8a650 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e er of allocation
8a660 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 75 72 s */. int mxCur
8a670 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20 20 rent[NCSIZE];
8a680 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 /* Highwater mar
8a690 6b 20 66 6f 72 20 6e 43 75 72 72 65 6e 74 20 2a k for nCurrent *
8a6a0 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f 2a 0a 2a /..} mem;.../*.*
8a6b0 2a 20 41 64 6a 75 73 74 20 6d 65 6d 6f 72 79 20 * Adjust memory
8a6c0 75 73 61 67 65 20 73 74 61 74 69 73 74 69 63 73 usage statistics
8a6d0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8a6e0 61 64 6a 75 73 74 53 74 61 74 73 28 69 6e 74 20 adjustStats(int
8a6f0 69 53 69 7a 65 2c 20 69 6e 74 20 69 6e 63 72 65 iSize, int incre
8a700 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 69 20 3d ment){. int i =
8a710 20 28 28 69 53 69 7a 65 2b 37 29 26 7e 37 29 2f ((iSize+7)&~7)/
8a720 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 53 49 5a 8;. if( i>NCSIZ
8a730 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 3d 20 4e E-1 ){. i = N
8a740 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 7d 0a 20 CSIZE - 1;. }.
8a750 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74 3e 30 if( increment>0
8a760 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41 6c 6c ){. mem.nAll
8a770 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d 65 6d oc[i]++;. mem
8a780 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b 2b 3b 0a .nCurrent[i]++;.
8a790 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 43 75 72 if( mem.nCur
8a7a0 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78 43 75 rent[i]>mem.mxCu
8a7b0 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 20 20 20 rrent[i] ){.
8a7c0 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b mem.mxCurrent[
8a7d0 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 72 65 6e i] = mem.nCurren
8a7e0 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 t[i];. }. }e
8a7f0 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 lse{. mem.nCu
8a800 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20 20 20 rrent[i]--;.
8a810 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e 43 75 72 assert( mem.nCur
8a820 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a 20 20 rent[i]>=0 );.
8a830 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e }.}../*.** Given
8a840 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 an allocation,
8a850 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63 find the MemBloc
8a860 6b 48 64 72 20 66 6f 72 20 74 68 61 74 20 61 6c kHdr for that al
8a870 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 location..**.**
8a880 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
8a890 63 6b 73 20 74 68 65 20 67 75 61 72 64 73 20 61 cks the guards a
8a8a0 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f 66 20 t either end of
8a8b0 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 the allocation a
8a8c0 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 nd.** if they ar
8a8d0 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74 20 61 e incorrect it a
8a8e0 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 sserts..*/.stati
8a8f0 63 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 c struct MemBloc
8a900 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 4d 65 6d kHdr *sqlite3Mem
8a910 73 79 73 47 65 74 48 65 61 64 65 72 28 76 6f 69 sysGetHeader(voi
8a920 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e 29 7b d *pAllocation){
8a930 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
8a940 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 6e 74 20 ckHdr *p;. int
8a950 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a 70 55 38 *pInt;. u8 *pU8
8a960 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65 ;. int nReserve
8a970 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 75 63 74 ;.. p = (struct
8a980 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 70 41 MemBlockHdr*)pA
8a990 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 2d 2d llocation;. p--
8a9a0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
8a9b0 46 6f 72 65 47 75 61 72 64 3d 3d 28 69 6e 74 29 ForeGuard==(int)
8a9c0 46 4f 52 45 47 55 41 52 44 20 29 3b 0a 20 20 6e FOREGUARD );. n
8a9d0 52 65 73 65 72 76 65 20 3d 20 28 70 2d 3e 69 53 Reserve = (p->iS
8a9e0 69 7a 65 2b 37 29 26 7e 37 3b 0a 20 20 70 49 6e ize+7)&~7;. pIn
8a9f0 74 20 3d 20 28 69 6e 74 2a 29 70 41 6c 6c 6f 63 t = (int*)pAlloc
8aa00 61 74 69 6f 6e 3b 0a 20 20 70 55 38 20 3d 20 28 ation;. pU8 = (
8aa10 75 38 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b u8*)pAllocation;
8aa20 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 74 5b . assert( pInt[
8aa30 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 6f 66 28 nReserve/sizeof(
8aa40 69 6e 74 29 5d 3d 3d 28 69 6e 74 29 52 45 41 52 int)]==(int)REAR
8aa50 47 55 41 52 44 20 29 3b 0a 20 20 61 73 73 65 72 GUARD );. asser
8aa60 74 28 20 28 6e 52 65 73 65 72 76 65 2d 30 29 3c t( (nReserve-0)<
8aa70 3d 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 70 55 38 =p->iSize || pU8
8aa80 5b 6e 52 65 73 65 72 76 65 2d 31 5d 3d 3d 30 78 [nReserve-1]==0x
8aa90 36 35 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 );. assert(
8aaa0 28 6e 52 65 73 65 72 76 65 2d 31 29 3c 3d 70 2d (nReserve-1)<=p-
8aab0 3e 69 53 69 7a 65 20 7c 7c 20 70 55 38 5b 6e 52 >iSize || pU8[nR
8aac0 65 73 65 72 76 65 2d 32 5d 3d 3d 30 78 36 35 20 eserve-2]==0x65
8aad0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 52 );. assert( (nR
8aae0 65 73 65 72 76 65 2d 32 29 3c 3d 70 2d 3e 69 53 eserve-2)<=p->iS
8aaf0 69 7a 65 20 7c 7c 20 70 55 38 5b 6e 52 65 73 65 ize || pU8[nRese
8ab00 72 76 65 2d 33 5d 3d 3d 30 78 36 35 20 29 3b 0a rve-3]==0x65 );.
8ab10 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
8ab20 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
8ab30 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
8ab40 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 currently alloca
8ab50 74 65 64 20 61 74 20 61 64 64 72 65 73 73 20 70 ted at address p
8ab60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8ab70 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 sqlite3MemSize(v
8ab80 6f 69 64 20 2a 70 29 7b 0a 20 20 73 74 72 75 63 oid *p){. struc
8ab90 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
8aba0 48 64 72 3b 0a 20 20 69 66 28 20 21 70 20 29 7b Hdr;. if( !p ){
8abb0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
8abc0 20 7d 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69 }. pHdr = sqli
8abd0 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 te3MemsysGetHead
8abe0 65 72 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 er(p);. return
8abf0 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a pHdr->iSize;.}..
8ac00 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 /*.** Initialize
8ac10 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
8ac20 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
8ac30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8ac40 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 sqlite3MemInit(v
8ac50 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 oid *NotUsed){.
8ac60 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
8ac70 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 R(NotUsed);. if
8ac80 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ( !sqlite3Global
8ac90 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 Config.bMemstat
8aca0 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6d 65 6d ){. /* If mem
8acb0 6f 72 79 20 73 74 61 74 75 73 20 69 73 20 65 6e ory status is en
8acc0 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 abled, then the
8acd0 6d 61 6c 6c 6f 63 2e 63 20 77 72 61 70 70 65 72 malloc.c wrapper
8ace0 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 0a 20 20 will already.
8acf0 20 20 2a 2a 20 68 6f 6c 64 20 74 68 65 20 53 54 ** hold the ST
8ad00 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 77 ATIC_MEM mutex w
8ad10 68 65 6e 20 74 68 65 20 72 6f 75 74 69 6e 65 73 hen the routines
8ad20 20 68 65 72 65 20 61 72 65 20 69 6e 76 6f 6b 65 here are invoke
8ad30 64 2e 20 2a 2f 0a 20 20 20 20 6d 65 6d 2e 6d 75 d. */. mem.mu
8ad40 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
8ad50 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
8ad60 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 UTEX_STATIC_MEM)
8ad70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
8ad80 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
8ad90 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 ** Deinitialize
8ada0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
8adb0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e ation subsystem.
8adc0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8add0 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f sqlite3MemShutdo
8ade0 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 wn(void *NotUsed
8adf0 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
8ae00 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
8ae10 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 30 3b mem.mutex = 0;
8ae20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 .}../*.** Round
8ae30 75 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a up a request siz
8ae40 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 e to the next va
8ae50 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 lid allocation s
8ae60 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ize..*/.static i
8ae70 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 nt sqlite3MemRou
8ae80 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 ndup(int n){. r
8ae90 65 74 75 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37 eturn (n+7) & ~7
8aea0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 ;.}../*.** Alloc
8aeb0 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 ate nByte bytes
8aec0 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 of memory..*/.st
8aed0 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 atic void *sqlit
8aee0 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 e3MemMalloc(int
8aef0 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 63 74 nByte){. struct
8af00 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 MemBlockHdr *pH
8af10 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 dr;. void **pBt
8af20 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 ;. char *z;. i
8af30 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f 69 64 nt *pInt;. void
8af40 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74 *p = 0;. int t
8af50 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 6e 74 20 otalSize;. int
8af60 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71 6c 69 nReserve;. sqli
8af70 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
8af80 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 61 73 mem.mutex);. as
8af90 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c sert( mem.disall
8afa0 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 65 73 65 ow==0 );. nRese
8afb0 72 76 65 20 3d 20 28 6e 42 79 74 65 2b 37 29 26 rve = (nByte+7)&
8afc0 7e 37 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 ~7;. totalSize
8afd0 3d 20 6e 52 65 73 65 72 76 65 20 2b 20 73 69 7a = nReserve + siz
8afe0 65 6f 66 28 2a 70 48 64 72 29 20 2b 20 73 69 7a eof(*pHdr) + siz
8aff0 65 6f 66 28 69 6e 74 29 20 2b 0a 20 20 20 20 20 eof(int) +.
8b000 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e 6e 42 mem.nB
8b010 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 acktrace*sizeof(
8b020 76 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e 6e 54 69 void*) + mem.nTi
8b030 74 6c 65 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f tle;. p = mallo
8b040 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 c(totalSize);.
8b050 69 66 28 20 70 20 29 7b 0a 20 20 20 20 7a 20 3d if( p ){. z =
8b060 20 70 3b 0a 20 20 20 20 70 42 74 20 3d 20 28 76 p;. pBt = (v
8b070 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e 54 69 oid**)&z[mem.nTi
8b080 74 6c 65 5d 3b 0a 20 20 20 20 70 48 64 72 20 3d tle];. pHdr =
8b090 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 (struct MemBloc
8b0a0 6b 48 64 72 2a 29 26 70 42 74 5b 6d 65 6d 2e 6e kHdr*)&pBt[mem.n
8b0b0 42 61 63 6b 74 72 61 63 65 5d 3b 0a 20 20 20 20 Backtrace];.
8b0c0 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b pHdr->pNext = 0;
8b0d0 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 . pHdr->pPrev
8b0e0 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b 0a 20 20 = mem.pLast;.
8b0f0 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73 74 20 if( mem.pLast
8b100 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70 4c 61 ){. mem.pLa
8b110 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 st->pNext = pHdr
8b120 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
8b130 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20 mem.pFirst =
8b140 70 48 64 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 pHdr;. }.
8b150 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72 mem.pLast = pHdr
8b160 3b 0a 20 20 20 20 70 48 64 72 2d 3e 69 46 6f 72 ;. pHdr->iFor
8b170 65 47 75 61 72 64 20 3d 20 46 4f 52 45 47 55 41 eGuard = FOREGUA
8b180 52 44 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 42 RD;. pHdr->nB
8b190 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 3d 20 acktraceSlots =
8b1a0 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 3b 0a mem.nBacktrace;.
8b1b0 20 20 20 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 pHdr->nTitle
8b1c0 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 = mem.nTitle;.
8b1d0 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42 61 63 6b if( mem.nBack
8b1e0 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 76 trace ){. v
8b1f0 6f 69 64 20 2a 61 41 64 64 72 5b 34 30 5d 3b 0a oid *aAddr[40];.
8b200 20 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 pHdr->nBac
8b210 6b 74 72 61 63 65 20 3d 20 62 61 63 6b 74 72 61 ktrace = backtra
8b220 63 65 28 61 41 64 64 72 2c 20 6d 65 6d 2e 6e 42 ce(aAddr, mem.nB
8b230 61 63 6b 74 72 61 63 65 2b 31 29 2d 31 3b 0a 20 acktrace+1)-1;.
8b240 20 20 20 20 20 6d 65 6d 63 70 79 28 70 42 74 2c memcpy(pBt,
8b250 20 26 61 41 64 64 72 5b 31 5d 2c 20 70 48 64 72 &aAddr[1], pHdr
8b260 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a ->nBacktrace*siz
8b270 65 6f 66 28 76 6f 69 64 2a 29 29 3b 0a 20 20 20 eof(void*));.
8b280 20 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b if( mem.xBack
8b290 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 trace ){.
8b2a0 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 mem.xBacktrace(
8b2b0 6e 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 nByte, pHdr->nBa
8b2c0 63 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64 cktrace-1, &aAdd
8b2d0 72 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 r[1]);. }.
8b2e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
8b2f0 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
8b300 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
8b310 69 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 if( mem.nTitle )
8b320 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a {. memcpy(z
8b330 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 , mem.zTitle, me
8b340 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d m.nTitle);. }
8b350 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 . pHdr->iSize
8b360 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 = nByte;. ad
8b370 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c justStats(nByte,
8b380 20 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d +1);. pInt =
8b390 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b (int*)&pHdr[1];
8b3a0 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 . pInt[nReser
8b3b0 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 ve/sizeof(int)]
8b3c0 3d 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20 = REARGUARD;.
8b3d0 20 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 memset(pInt, 0x
8b3e0 36 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 65, nReserve);.
8b3f0 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 p = (void*)pI
8b400 6e 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 nt;. }. sqlite
8b410 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
8b420 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 m.mutex);. retu
8b430 72 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn p; .}../*.**
8b440 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a Free memory..*/.
8b450 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
8b460 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 te3MemFree(void
8b470 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 *pPrior){. stru
8b480 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
8b490 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 pHdr;. void **p
8b4a0 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 Bt;. char *z;.
8b4b0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
8b4c0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
8b4d0 6d 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 mstat || mem.mut
8b4e0 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 ex!=0 );. pHdr
8b4f0 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 = sqlite3MemsysG
8b500 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 etHeader(pPrior)
8b510 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a ;. pBt = (void*
8b520 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d *)pHdr;. pBt -=
8b530 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
8b540 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 eSlots;. sqlite
8b550 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
8b560 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 m.mutex);. if(
8b570 70 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 pHdr->pPrev ){.
8b580 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d assert( pHdr-
8b590 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 >pPrev->pNext==p
8b5a0 48 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d Hdr );. pHdr-
8b5b0 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
8b5c0 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d pHdr->pNext;. }
8b5d0 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
8b5e0 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 ( mem.pFirst==pH
8b5f0 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 dr );. mem.pF
8b600 69 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 irst = pHdr->pNe
8b610 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 xt;. }. if( pH
8b620 64 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 dr->pNext ){.
8b630 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 assert( pHdr->p
8b640 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 Next->pPrev==pHd
8b650 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 r );. pHdr->p
8b660 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 Next->pPrev = pH
8b670 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c dr->pPrev;. }el
8b680 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
8b690 6d 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 mem.pLast==pHdr
8b6a0 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 );. mem.pLast
8b6b0 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a = pHdr->pPrev;.
8b6c0 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a }. z = (char*
8b6d0 29 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 )pBt;. z -= pHd
8b6e0 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a r->nTitle;. adj
8b6f0 75 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69 ustStats(pHdr->i
8b700 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d Size, -1);. mem
8b710 73 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a set(z, 0x2b, siz
8b720 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d eof(void*)*pHdr-
8b730 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
8b740 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 + sizeof(*pHdr)
8b750 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 +.
8b760 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 pHdr->iSize
8b770 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b + sizeof(int) +
8b780 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a pHdr->nTitle);.
8b790 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c free(z);. sql
8b7a0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
8b7b0 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d (mem.mutex); .}
8b7c0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
8b7d0 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 he size of an ex
8b7e0 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c isting memory al
8b7f0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 location..**.**
8b800 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69 For this debuggi
8b810 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ng implementatio
8b820 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d n, we *always* m
8b830 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ake a copy of th
8b840 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 e.** allocation
8b850 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 into a new place
8b860 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 in memory. In
8b870 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 this way, if the
8b880 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65 .** higher leve
8b890 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20 l code is using
8b8a0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f pointer to the o
8b8b0 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 ld allocation, i
8b8c0 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f t is .** much mo
8b8d0 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 re likely to bre
8b8e0 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75 ak and we are mu
8b8f0 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 ch more liking t
8b900 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 o find.** the er
8b910 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ror..*/.static v
8b920 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 oid *sqlite3MemR
8b930 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 ealloc(void *pPr
8b940 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b ior, int nByte){
8b950 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
8b960 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a ckHdr *pOldHdr;.
8b970 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 void *pNew;.
8b980 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 assert( mem.disa
8b990 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c llow==0 );. pOl
8b9a0 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 dHdr = sqlite3Me
8b9b0 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50 msysGetHeader(pP
8b9c0 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 rior);. pNew =
8b9d0 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
8b9e0 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70 (nByte);. if( p
8b9f0 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 New ){. memcp
8ba00 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 y(pNew, pPrior,
8ba10 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 nByte<pOldHdr->i
8ba20 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 Size ? nByte : p
8ba30 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a OldHdr->iSize);.
8ba40 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f if( nByte>pO
8ba50 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a ldHdr->iSize ){.
8ba60 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 memset(&((
8ba70 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 char*)pNew)[pOld
8ba80 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 Hdr->iSize], 0x2
8ba90 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 b, nByte - pOldH
8baa0 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 dr->iSize);.
8bab0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d }. sqlite3Mem
8bac0 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 Free(pPrior);.
8bad0 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b }. return pNew;
8bae0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 .}../*.** Popula
8baf0 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c te the low-level
8bb00 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
8bb10 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e on function poin
8bb20 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 ters in.** sqlit
8bb30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
8bb40 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 with pointers t
8bb50 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 o the routines i
8bb60 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a n this file..*/.
8bb70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
8bb80 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 oid sqlite3MemSe
8bb90 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a tDefault(void){.
8bba0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
8bbb0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
8bbc0 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 ds defaultMethod
8bbd0 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 s = {. sqlit
8bbe0 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 e3MemMalloc,.
8bbf0 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 sqlite3MemFree
8bc00 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
8bc10 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 mRealloc,. s
8bc20 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 qlite3MemSize,.
8bc30 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f sqlite3MemRo
8bc40 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 undup,. sqli
8bc50 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 te3MemInit,.
8bc60 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 sqlite3MemShutd
8bc70 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b own,. 0. };
8bc80 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 . sqlite3_confi
8bc90 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f g(SQLITE_CONFIG_
8bca0 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 MALLOC, &default
8bcb0 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a Methods);.}../*.
8bcc0 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 ** Set the numbe
8bcd0 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c r of backtrace l
8bce0 65 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 evels kept for e
8bcf0 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a ach allocation..
8bd00 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 ** A value of ze
8bd10 72 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63 ro turns off bac
8bd20 6b 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e ktracing. The n
8bd30 75 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 umber is always
8bd40 72 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f rounded.** up to
8bd50 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 a multiple of 2
8bd60 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
8bd70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
8bd80 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 MemdebugBacktrac
8bd90 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 e(int depth){.
8bda0 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64 if( depth<0 ){ d
8bdb0 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 epth = 0; }. if
8bdc0 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65 ( depth>20 ){ de
8bdd0 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 pth = 20; }. de
8bde0 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26 pth = (depth+1)&
8bdf0 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 0xfe;. mem.nBac
8be00 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a ktrace = depth;.
8be10 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }..SQLITE_PRIVAT
8be20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 E void sqlite3Me
8be30 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 mdebugBacktraceC
8be40 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 allback(void (*x
8be50 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 Backtrace)(int,
8be60 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a int, void **)){.
8be70 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 mem.xBacktrace
8be80 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d = xBacktrace;.}
8be90 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
8bea0 74 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 title string for
8beb0 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f subsequent allo
8bec0 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 cations..*/.SQLI
8bed0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
8bee0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 sqlite3MemdebugS
8bef0 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 ettitle(const ch
8bf00 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 ar *zTitle){. u
8bf10 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 nsigned int n =
8bf20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
8bf30 7a 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 zTitle) + 1;. s
8bf40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
8bf50 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 er(mem.mutex);.
8bf60 20 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d if( n>=sizeof(m
8bf70 65 6d 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d em.zTitle) ) n =
8bf80 20 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 sizeof(mem.zTit
8bf90 6c 65 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 le)-1;. memcpy(
8bfa0 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 mem.zTitle, zTit
8bfb0 6c 65 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 le, n);. mem.zT
8bfc0 69 74 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d itle[n] = 0;. m
8bfd0 65 6d 2e 6e 54 69 74 6c 65 20 3d 20 28 6e 2b 37 em.nTitle = (n+7
8bfe0 29 26 7e 37 3b 0a 20 20 73 71 6c 69 74 65 33 5f )&~7;. sqlite3_
8bff0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e mutex_leave(mem.
8c000 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 mutex);.}..SQLIT
8c010 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
8c020 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79 qlite3MemdebugSy
8c030 6e 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d nc(){. struct M
8c040 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 emBlockHdr *pHdr
8c050 3b 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d ;. for(pHdr=mem
8c060 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 .pFirst; pHdr; p
8c070 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 Hdr=pHdr->pNext)
8c080 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74 {. void **pBt
8c090 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b = (void**)pHdr;
8c0a0 0a 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 . pBt -= pHdr
8c0b0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 ->nBacktraceSlot
8c0c0 73 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b s;. mem.xBack
8c0d0 74 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a trace(pHdr->iSiz
8c0e0 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 e, pHdr->nBacktr
8c0f0 61 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b ace-1, &pBt[1]);
8c100 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 . }.}../*.** Op
8c110 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 en the file indi
8c120 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 cated and write
8c130 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 a log of all unf
8c140 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 reed memory .**
8c150 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f allocations into
8c160 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 that log..*/.SQ
8c170 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
8c180 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 d sqlite3Memdebu
8c190 67 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 gDump(const char
8c1a0 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 *zFilename){.
8c1b0 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72 FILE *out;. str
8c1c0 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 uct MemBlockHdr
8c1d0 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a *pHdr;. void **
8c1e0 70 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 pBt;. int i;.
8c1f0 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c out = fopen(zFil
8c200 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69 ename, "w");. i
8c210 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 f( out==0 ){.
8c220 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
8c230 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f "** Unable to o
8c240 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 utput memory deb
8c250 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 ug output log: %
8c260 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 s **\n",.
8c270 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 zFi
8c280 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 lename);. ret
8c290 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 urn;. }. for(p
8c2a0 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 Hdr=mem.pFirst;
8c2b0 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d pHdr; pHdr=pHdr-
8c2c0 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61 >pNext){. cha
8c2d0 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48 r *z = (char*)pH
8c2e0 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64 dr;. z -= pHd
8c2f0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f r->nBacktraceSlo
8c300 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 ts*sizeof(void*)
8c310 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b + pHdr->nTitle;
8c320 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 . fprintf(out
8c330 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 , "**** %lld byt
8c340 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73 es at %p from %s
8c350 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20 ****\n", .
8c360 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 pHdr->iSi
8c370 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 ze, &pHdr[1], pH
8c380 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a dr->nTitle ? z :
8c390 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28 "???");. if(
8c3a0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
8c3b0 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73 e ){. fflus
8c3c0 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42 h(out);. pB
8c3d0 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 t = (void**)pHdr
8c3e0 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70 ;. pBt -= p
8c3f0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 Hdr->nBacktraceS
8c400 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b lots;. back
8c410 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 trace_symbols_fd
8c420 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63 (pBt, pHdr->nBac
8c430 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f ktrace, fileno(o
8c440 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69 ut));. fpri
8c450 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a ntf(out, "\n");.
8c460 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69 }. }. fpri
8c470 6e 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53 ntf(out, "COUNTS
8c480 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 :\n");. for(i=0
8c490 3b 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b ; i<NCSIZE-1; i+
8c4a0 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e +){. if( mem.
8c4b0 6e 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20 nAlloc[i] ){.
8c4c0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
8c4d0 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 " %5d: %10d %1
8c4e0 30 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20 0d %10d\n", .
8c4f0 20 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65 i*8, me
8c500 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d m.nAlloc[i], mem
8c510 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65 .nCurrent[i], me
8c520 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b m.mxCurrent[i]);
8c530 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
8c540 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 mem.nAlloc[NCSI
8c550 5a 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 ZE-1] ){. fpr
8c560 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 intf(out, " %5
8c570 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 d: %10d %10d %10
8c580 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
8c590 20 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d NCSIZE*8-8, m
8c5a0 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 em.nAlloc[NCSIZE
8c5b0 2d 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 -1],.
8c5c0 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e mem.nCurrent[N
8c5d0 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 CSIZE-1], mem.mx
8c5e0 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 Current[NCSIZE-1
8c5f0 5d 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65 ]);. }. fclose
8c600 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 (out);.}../*.**
8c610 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
8c620 72 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74 r of times sqlit
8c630 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61 e3MemMalloc() ha
8c640 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a s been called..*
8c650 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
8c660 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 int sqlite3Memd
8c670 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28 ebugMallocCount(
8c680 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e ){. int i;. in
8c690 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 t nTotal = 0;.
8c6a0 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a for(i=0; i<NCSIZ
8c6b0 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f E; i++){. nTo
8c6c0 74 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f tal += mem.nAllo
8c6d0 63 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 c[i];. }. retu
8c6e0 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 rn nTotal;.}...#
8c6f0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
8c700 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a MEMDEBUG */../**
8c710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
8c720 20 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a of mem2.c *****
8c730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
8c760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
8c770 69 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a in file mem3.c *
8c780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
8c7b0 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 ** 2007 October
8c7c0 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 14.**.** The aut
8c7d0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
8c7e0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
8c7f0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
8c800 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
8c810 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
8c820 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
8c830 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
8c840 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
8c850 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
8c860 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
8c870 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
8c880 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
8c890 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
8c8a0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
8c8b0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
8c8c0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
8c8d0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
8c8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c920 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
8c930 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 ontains the C fu
8c940 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 nctions that imp
8c950 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a lement a memory.
8c960 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ** allocation su
8c970 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 bsystem for use
8c980 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a by SQLite. .**.*
8c990 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
8c9a0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
8c9b0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
8c9c0 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 m omits all.** u
8c9d0 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 se of malloc().
8c9e0 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 The SQLite user
8c9f0 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b supplies a block
8ca00 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 of memory.** be
8ca10 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c fore calling sql
8ca20 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
8ca30 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c ) from which all
8ca40 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 ocations.** are
8ca50 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 made and returne
8ca60 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 d by the xMalloc
8ca70 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 () and xRealloc(
8ca80 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ) .** implementa
8ca90 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 tions. Once sqli
8caa0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
8cab0 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 has been called
8cac0 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 ,.** the amount
8cad0 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 of memory availa
8cae0 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 ble to SQLite is
8caf0 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f fixed and canno
8cb00 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e t.** be changed.
8cb10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 .**.** This vers
8cb20 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
8cb30 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
8cb40 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 system is includ
8cb50 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 ed.** in the bui
8cb60 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 ld only if SQLIT
8cb70 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 E_ENABLE_MEMSYS3
8cb80 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a is defined..**.
8cb90 2a 2a 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c 76 ** $Id: mem3.c,v
8cba0 20 31 2e 32 35 20 32 30 30 38 2f 31 31 2f 31 39 1.25 2008/11/19
8cbb0 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c 16:52:44 daniel
8cbc0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
8cbd0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 /*.** This versi
8cbe0 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
8cbf0 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e allocator is on
8cc00 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 ly built into th
8cc10 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c e library.** SQL
8cc20 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
8cc30 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44 S3 is defined. D
8cc40 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d efining this sym
8cc50 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 bol does not.**
8cc60 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69 mean that the li
8cc70 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61 brary will use a
8cc80 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20 memory-pool by
8cc90 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68 default, just th
8cca0 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69 at.** it is avai
8ccb0 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f lable. The mempo
8ccc0 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ol allocator is
8ccd0 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61 6c activated by cal
8cce0 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ling.** sqlite3_
8ccf0 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66 config()..*/.#if
8cd00 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
8cd10 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a E_MEMSYS3../*.**
8cd20 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69 Maximum size (i
8cd30 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66 n Mem3Blocks) of
8cd40 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b a "small" chunk
8cd50 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f ..*/.#define MX_
8cd60 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a SMALL 10.../*.**
8cd70 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c Number of freel
8cd80 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a ist hash slots.*
8cd90 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48 /.#define N_HASH
8cda0 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 61../*.** A me
8cdb0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
8cdc0 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22 (also called a "
8cdd0 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73 chunk") consists
8cde0 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d of two or .** m
8cdf0 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65 ore blocks where
8ce00 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38 each block is 8
8ce10 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69 72 bytes. The fir
8ce20 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20 0a st 8 bytes are .
8ce30 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61 74 ** a header that
8ce40 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64 is not returned
8ce50 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a to the user..**
8ce60 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74 .** A chunk is t
8ce70 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b wo or more block
8ce80 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65 72 s that is either
8ce90 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a checked out or.
8cea0 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66 69 ** free. The fi
8ceb0 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f rst block has fo
8cec0 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68 rmat u.hdr. u.h
8ced0 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74 dr.size4x is 4 t
8cee0 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 imes the.** size
8cef0 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 of the allocati
8cf00 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 on in blocks if
8cf10 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 the allocation i
8cf20 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75 s free..** The u
8cf30 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69 .hdr.size4x&1 bi
8cf40 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65 t is true if the
8cf50 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 chunk is checke
8cf60 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c d out and.** fal
8cf70 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 se if the chunk
8cf80 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 is on the freeli
8cf90 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73 st. The u.hdr.s
8cfa0 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69 ize4x&2 bit.** i
8cfb0 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72 s true if the pr
8cfc0 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 evious chunk is
8cfd0 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 checked out and
8cfe0 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20 false if the.**
8cff0 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 previous chunk i
8d000 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68 s free. The u.h
8d010 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c dr.prevSize fiel
8d020 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 d is the size of
8d030 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 .** the previous
8d040 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73 chunk in blocks
8d050 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 if the previous
8d060 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65 chunk is on the
8d070 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66 .** freelist. If
8d080 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 the previous ch
8d090 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f unk is checked o
8d0a0 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64 ut, then.** u.hd
8d0b0 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62 r.prevSize can b
8d0c0 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 e part of the da
8d0d0 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e ta for that chun
8d0e0 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 k and should.**
8d0f0 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 not be read or w
8d100 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 ritten..**.** We
8d110 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20 often identify
8d120 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69 a chunk by its i
8d130 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f ndex in mem3.aPo
8d140 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 ol[]. When.** t
8d150 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65 his is done, the
8d160 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66 chunk index ref
8d170 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e ers to the secon
8d180 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68 d block of.** th
8d190 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69 e chunk. In thi
8d1a0 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73 74 s way, the first
8d1b0 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e chunk has an in
8d1c0 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63 dex of 1..** A c
8d1d0 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20 hunk index of 0
8d1e0 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63 means "no such c
8d1f0 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65 hunk" and is the
8d200 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f equivalent.** o
8d210 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 f a NULL pointer
8d220 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ..**.** The seco
8d230 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65 nd block of free
8d240 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68 chunks is of th
8d250 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20 e form u.list.
8d260 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64 The.** two field
8d270 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d s form a double-
8d280 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63 linked list of c
8d290 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64 hunks of related
8d2a0 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74 sizes..** Point
8d2b0 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 ers to the head
8d2c0 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65 20 of the list are
8d2d0 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61 stored in mem3.a
8d2e0 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 iSmall[] .** for
8d2f0 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20 smaller chunks
8d300 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b and mem3.aiHash[
8d310 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75 ] for larger chu
8d320 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 nks..**.** The s
8d330 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61 econd block of a
8d340 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64 chunk is user d
8d350 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b ata if the chunk
8d360 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20 is checked .**
8d370 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b out. If a chunk
8d380 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c is checked out,
8d390 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 6d the user data m
8d3a0 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a ay extend into.*
8d3b0 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76 * the u.hdr.prev
8d3c0 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68 Size value of th
8d3d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e e following chun
8d3e0 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 k..*/.typedef st
8d3f0 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d ruct Mem3Block M
8d400 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74 em3Block;.struct
8d410 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75 Mem3Block {. u
8d420 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63 nion {. struc
8d430 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72 t {. u32 pr
8d440 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a evSize; /* Siz
8d450 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68 e of previous ch
8d460 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b unk in Mem3Block
8d470 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 elements */.
8d480 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20 u32 size4x;
8d490 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a /* 4x the siz
8d4a0 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75 e of current chu
8d4b0 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 nk in Mem3Block
8d4c0 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 elements */.
8d4d0 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63 } hdr;. struc
8d4e0 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65 t {. u32 ne
8d4f0 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 xt; /* Ind
8d500 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c ex in mem3.aPool
8d510 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 [] of next free
8d520 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75 chunk */. u
8d530 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 32 prev; /
8d540 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e * Index in mem3.
8d550 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69 aPool[] of previ
8d560 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a ous free chunk *
8d570 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20 /. } list;.
8d580 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 } u;.};../*.** A
8d590 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 ll of the static
8d5a0 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 variables used
8d5b0 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 by this module a
8d5c0 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 re collected.**
8d5d0 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 into a single st
8d5e0 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d ructure named "m
8d5f0 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20 74 em3". This is t
8d600 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 o keep the.** st
8d610 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f atic variables o
8d620 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 rganized and to
8d630 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 reduce namespace
8d640 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 pollution.** wh
8d650 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 en this module i
8d660 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 s combined with
8d670 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 other in the ama
8d680 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 lgamation..*/.st
8d690 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
8d6a0 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 struct Mem3Globa
8d6b0 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 l {. /*. ** Me
8d6c0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 mory available f
8d6d0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e or allocation. n
8d6e0 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65 Pool is the size
8d6f0 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20 20 of the array.
8d700 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b ** (in Mem3Block
8d710 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 s) pointed to by
8d720 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20 aPool less 2..
8d730 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b */. u32 nPool;
8d740 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 . Mem3Block *aP
8d750 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 ool;.. /*. **
8d760 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 65 True if we are e
8d770 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74 valuating an out
8d780 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62 -of-memory callb
8d790 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 ack.. */. int
8d7a0 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 alarmBusy;. .
8d7b0 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f /*. ** Mutex to
8d7c0 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 control access
8d7d0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c to the memory al
8d7e0 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
8d7f0 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 em.. */. sqlit
8d800 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b e3_mutex *mutex;
8d810 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 . . /*. ** Th
8d820 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 e minimum amount
8d830 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20 74 of free space t
8d840 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65 6e hat we have seen
8d850 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d .. */. u32 mnM
8d860 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a aster;.. /*. *
8d870 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68 65 * iMaster is the
8d880 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 index of the ma
8d890 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73 ster chunk. Mos
8d8a0 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e t new allocation
8d8b0 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66 s. ** occur off
8d8c0 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20 of this chunk.
8d8d0 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 szMaster is the
8d8e0 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c size (in Mem3Bl
8d8f0 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68 ocks). ** of th
8d900 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72 e current master
8d910 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30 20 . iMaster is 0
8d920 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 if there is not
8d930 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20 master chunk..
8d940 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63 68 ** The master ch
8d950 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69 unk is not in ei
8d960 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68 5b ther the aiHash[
8d970 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a ] or aiSmall[]..
8d980 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74 */. u32 iMast
8d990 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74 er;. u32 szMast
8d9a0 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 er;.. /*. ** A
8d9b0 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66 rray of lists of
8d9c0 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63 free blocks acc
8d9d0 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c ording to the bl
8d9e0 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66 ock size . ** f
8d9f0 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b or smaller chunk
8da00 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20 s, or a hash on
8da10 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66 the block size f
8da20 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63 or larger. ** c
8da30 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33 hunks.. */. u3
8da40 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 2 aiSmall[MX_SMA
8da50 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 LL-1]; /* For
8da60 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20 sizes 2 through
8da70 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 MX_SMALL, inclus
8da80 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48 ive */. u32 aiH
8da90 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20 ash[N_HASH];
8daa0 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 /* For sizes
8dab0 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 MX_SMALL+1 and
8dac0 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33 larger */.} mem3
8dad0 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b = { 97535575 };
8dae0 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47 ..#define mem3 G
8daf0 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d LOBAL(struct Mem
8db00 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a 3Global, mem3)..
8db10 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 /*.** Unlink the
8db20 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 chunk at mem3.a
8db30 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 Pool[i] from lis
8db40 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c t it is currentl
8db50 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74 y.** on. *pRoot
8db60 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68 61 is the list tha
8db70 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20 t i is a member
8db80 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f of..*/.static vo
8db90 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b id memsys3Unlink
8dba0 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20 FromList(u32 i,
8dbb0 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 u32 *pRoot){. u
8dbc0 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 32 next = mem3.a
8dbd0 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[i].u.list.n
8dbe0 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20 ext;. u32 prev
8dbf0 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e = mem3.aPool[i].
8dc00 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61 u.list.prev;. a
8dc10 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
8dc20 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
8dc30 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
8dc40 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 rev==0 ){. *p
8dc50 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d Root = next;. }
8dc60 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 else{. mem3.a
8dc70 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 Pool[prev].u.lis
8dc80 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 t.next = next;.
8dc90 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b }. if( next ){
8dca0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
8dcb0 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 next].u.list.pre
8dcc0 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20 v = prev;. }.
8dcd0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
8dce0 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20 list.next = 0;.
8dcf0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 mem3.aPool[i].u
8dd00 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a .list.prev = 0;.
8dd10 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 }../*.** Unlink
8dd20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 the chunk at ind
8dd30 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68 ex i from .** wh
8dd40 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 63 atever list is c
8dd50 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65 urrently a membe
8dd60 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 r of..*/.static
8dd70 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 void memsys3Unli
8dd80 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 nk(u32 i){. u32
8dd90 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 size, hash;. a
8dda0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
8ddb0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
8ddc0 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
8ddd0 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t( (mem3.aPool[i
8dde0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8ddf0 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73 & 1)==0 );. as
8de00 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 sert( i>=1 );.
8de10 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f size = mem3.aPoo
8de20 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
8de30 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 e4x/4;. assert(
8de40 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f size==mem3.aPoo
8de50 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 l[i+size-1].u.hd
8de60 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 r.prevSize );.
8de70 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 assert( size>=2
8de80 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d );. if( size <=
8de90 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 MX_SMALL ){.
8dea0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 memsys3UnlinkFr
8deb0 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e omList(i, &mem3.
8dec0 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 aiSmall[size-2])
8ded0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 ;. }else{. h
8dee0 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 ash = size % N_H
8def0 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 ASH;. memsys3
8df00 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 UnlinkFromList(i
8df10 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 , &mem3.aiHash[h
8df20 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ash]);. }.}../*
8df30 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 .** Link the chu
8df40 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c nk at mem3.aPool
8df50 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f [i] so that is o
8df60 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65 n the list roote
8df70 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a d.** at *pRoot..
8df80 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
8df90 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 emsys3LinkIntoLi
8dfa0 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 st(u32 i, u32 *p
8dfb0 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28 Root){. assert(
8dfc0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
8dfd0 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
8dfe0 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b );. mem3.aPool[
8dff0 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d i].u.list.next =
8e000 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e *pRoot;. mem3.
8e010 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
8e020 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20 prev = 0;. if(
8e030 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65 *pRoot ){. me
8e040 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d m3.aPool[*pRoot]
8e050 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69 .u.list.prev = i
8e060 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d ;. }. *pRoot =
8e070 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e i;.}../*.** Lin
8e080 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 k the chunk at i
8e090 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68 ndex i into eith
8e0a0 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 er the appropria
8e0b0 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e te.** small chun
8e0c0 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20 k list, or into
8e0d0 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 the large chunk
8e0e0 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 hash table..*/.s
8e0f0 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
8e100 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 s3Link(u32 i){.
8e110 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b u32 size, hash;
8e120 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
8e130 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
8e140 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 m3.mutex) );. a
8e150 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 ssert( i>=1 );.
8e160 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 assert( (mem3.a
8e170 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
8e180 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29 size4x & 1)==0 )
8e190 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e ;. size = mem3.
8e1a0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
8e1b0 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 .size4x/4;. ass
8e1c0 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e ert( size==mem3.
8e1d0 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e aPool[i+size-1].
8e1e0 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 u.hdr.prevSize )
8e1f0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 ;. assert( size
8e200 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a >=2 );. if( siz
8e210 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b e <= MX_SMALL ){
8e220 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b . memsys3Link
8e230 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d IntoList(i, &mem
8e240 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 3.aiSmall[size-2
8e250 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ]);. }else{.
8e260 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e hash = size % N
8e270 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 _HASH;. memsy
8e280 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 s3LinkIntoList(i
8e290 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 , &mem3.aiHash[h
8e2a0 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ash]);. }.}../*
8e2b0 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 .** If the STATI
8e2c0 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e C_MEM mutex is n
8e2d0 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c ot already held,
8e2e0 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 obtain it now.
8e2f0 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c The mutex.** wil
8e300 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c l already be hel
8e310 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 d (obtained by c
8e320 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 ode in malloc.c)
8e330 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c if.** sqlite3Gl
8e340 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 obalConfig.bMemS
8e350 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a tat is true..*/.
8e360 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
8e370 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a ys3Enter(void){.
8e380 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
8e390 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
8e3a0 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75 at==0 && mem3.mu
8e3b0 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 tex==0 ){. me
8e3c0 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 m3.mutex = sqlit
8e3d0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
8e3e0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
8e3f0 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c _MEM);. }. sql
8e400 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
8e410 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a (mem3.mutex);.}.
8e420 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
8e430 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a ys3Leave(void){.
8e440 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
8e450 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 leave(mem3.mutex
8e460 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c );.}../*.** Call
8e470 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 75 ed when we are u
8e480 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79 nable to satisfy
8e490 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f an allocation o
8e4a0 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 f nBytes..*/.sta
8e4b0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
8e4c0 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20 OutOfMemory(int
8e4d0 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d nByte){. if( !m
8e4e0 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b em3.alarmBusy ){
8e4f0 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 . mem3.alarmB
8e500 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 usy = 1;. ass
8e510 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
8e520 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
8e530 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ex) );. sqlit
8e540 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
8e550 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em3.mutex);.
8e560 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
8e570 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20 memory(nByte);.
8e580 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
8e590 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65 _enter(mem3.mute
8e5a0 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 x);. mem3.ala
8e5b0 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a rmBusy = 0;. }.
8e5c0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20 }.../*.** Chunk
8e5d0 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75 6e i is a free chun
8e5e0 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 k that has been
8e5f0 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73 unlinked. Adjus
8e600 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70 t its .** size p
8e610 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68 arameters for ch
8e620 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75 eck-out and retu
8e630 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
8e640 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72 the .** user por
8e650 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e tion of the chun
8e660 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 k..*/.static voi
8e670 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f d *memsys3Checko
8e680 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e 42 ut(u32 i, u32 nB
8e690 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a lock){. u32 x;.
8e6a0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
8e6b0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
8e6c0 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 3.mutex) );. as
8e6d0 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 sert( i>=1 );.
8e6e0 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f assert( mem3.aPo
8e6f0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
8e700 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 ze4x/4==nBlock )
8e710 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 ;. assert( mem3
8e720 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d .aPool[i+nBlock-
8e730 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
8e740 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78 e==nBlock );. x
8e750 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d = mem3.aPool[i-
8e760 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 1].u.hdr.size4x;
8e770 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d . mem3.aPool[i-
8e780 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8e790 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c = nBlock*4 | 1 |
8e7a0 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61 (x&2);. mem3.a
8e7b0 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d Pool[i+nBlock-1]
8e7c0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
8e7d0 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33 = nBlock;. mem3
8e7e0 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d .aPool[i+nBlock-
8e7f0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8e800 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26 |= 2;. return &
8e810 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d mem3.aPool[i];.}
8e820 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20 ../*.** Carve a
8e830 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65 piece off of the
8e840 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33 end of the mem3
8e850 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 68 .iMaster free ch
8e860 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 unk..** Return a
8e870 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
8e880 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 new allocation.
8e890 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74 Or, if the mast
8e8a0 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e er chunk.** is n
8e8b0 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c ot large enough,
8e8c0 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 return 0..*/.st
8e8d0 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 atic void *memsy
8e8e0 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 32 s3FromMaster(u32
8e8f0 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65 nBlock){. asse
8e900 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
8e910 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
8e920 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
8e930 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e mem3.szMaster>=n
8e940 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e Block );. if( n
8e950 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 Block>=mem3.szMa
8e960 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a ster-1 ){. /*
8e970 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 Use the entire
8e980 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f master */. vo
8e990 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43 id *p = memsys3C
8e9a0 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61 heckout(mem3.iMa
8e9b0 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73 ster, mem3.szMas
8e9c0 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69 ter);. mem3.i
8e9d0 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
8e9e0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 mem3.szMaster =
8e9f0 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 0;. mem3.mnMa
8ea00 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65 ster = 0;. re
8ea10 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b turn p;. }else{
8ea20 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 . /* Split th
8ea30 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 e master block.
8ea40 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c Return the tail
8ea50 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77 . */. u32 new
8ea60 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d i, x;. newi =
8ea70 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20 mem3.iMaster +
8ea80 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20 mem3.szMaster -
8ea90 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 nBlock;. asse
8eaa0 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e rt( newi > mem3.
8eab0 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20 iMaster+1 );.
8eac0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
8ead0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
8eae0 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
8eaf0 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 prevSize = nBloc
8eb00 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f k;. mem3.aPoo
8eb10 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d l[mem3.iMaster+m
8eb20 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e em3.szMaster-1].
8eb30 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 u.hdr.size4x |=
8eb40 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 2;. mem3.aPoo
8eb50 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e l[newi-1].u.hdr.
8eb60 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a size4x = nBlock*
8eb70 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e 4 + 1;. mem3.
8eb80 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f szMaster -= nBlo
8eb90 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f ck;. mem3.aPo
8eba0 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 ol[newi-1].u.hdr
8ebb0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 .prevSize = mem3
8ebc0 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78 .szMaster;. x
8ebd0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 = mem3.aPool[me
8ebe0 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e m3.iMaster-1].u.
8ebf0 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a hdr.size4x & 2;.
8ec00 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d mem3.aPool[m
8ec10 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
8ec20 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 .hdr.size4x = me
8ec30 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 m3.szMaster*4 |
8ec40 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e x;. if( mem3.
8ec50 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e szMaster < mem3.
8ec60 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 mnMaster ){.
8ec70 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 mem3.mnMaster
8ec80 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b = mem3.szMaster;
8ec90 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
8eca0 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 n (void*)&mem3.a
8ecb0 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a Pool[newi];. }.
8ecc0 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 }../*.** *pRoot
8ecd0 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 61 is the head of a
8ece0 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63 68 list of free ch
8ecf0 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65 unks of the same
8ed00 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 size.** or same
8ed10 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20 size hash. In
8ed20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52 other words, *pR
8ed30 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20 oot is an entry
8ed40 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d in either.** mem
8ed50 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 3.aiSmall[] or m
8ed60 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a em3.aiHash[]. .
8ed70 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
8ed80 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20 ne examines all
8ed90 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67 entries on the g
8eda0 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72 iven list and tr
8edb0 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73 ies.** to coales
8edc0 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73 20 ce each entries
8edd0 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72 with adjacent fr
8ede0 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a ee chunks. .**.
8edf0 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61 20 ** If it sees a
8ee00 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61 chunk that is la
8ee10 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69 rger than mem3.i
8ee20 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61 Master, it repla
8ee30 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72 ces .** the curr
8ee40 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ent mem3.iMaster
8ee50 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61 with the new la
8ee60 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20 rger chunk. In
8ee70 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69 order for.** thi
8ee80 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72 s mem3.iMaster r
8ee90 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f eplacement to wo
8eea0 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63 rk, the master c
8eeb0 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20 hunk must be.**
8eec0 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 linked into the
8eed0 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68 hash tables. Th
8eee0 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f at is not the no
8eef0 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a rmal state of.**
8ef00 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75 affairs, of cou
8ef10 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e rse. The callin
8ef20 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c g routine must l
8ef30 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a ink the master.*
8ef40 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69 * chunk before i
8ef50 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 nvoking this rou
8ef60 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20 tine, then must
8ef70 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73 unlink the (poss
8ef80 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29 ibly.** changed)
8ef90 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e master chunk on
8efa0 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ce this routine
8efb0 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f has finished..*/
8efc0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
8efd0 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70 sys3Merge(u32 *p
8efe0 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65 Root){. u32 iNe
8eff0 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20 xt, prev, size,
8f000 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28 i, x;.. assert(
8f010 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
8f020 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
8f030 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f );. for(i=*pRoo
8f040 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29 t; i>0; i=iNext)
8f050 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 {. iNext = me
8f060 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 m3.aPool[i].u.li
8f070 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a st.next;. siz
8f080 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 e = mem3.aPool[i
8f090 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8f0a0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 73 ;. assert( (s
8f0b0 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20 ize&1)==0 );.
8f0c0 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30 if( (size&2)==0
8f0d0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 ){. memsys
8f0e0 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 3UnlinkFromList(
8f0f0 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 i, pRoot);.
8f100 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d assert( i > mem
8f110 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
8f120 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 dr.prevSize );.
8f130 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20 prev = i -
8f140 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e mem3.aPool[i-1].
8f150 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a u.hdr.prevSize;.
8f160 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d if( prev==
8f170 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 iNext ){.
8f180 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 iNext = mem3.aP
8f190 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 ool[prev].u.list
8f1a0 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 .next;. }.
8f1b0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
8f1c0 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 nk(prev);.
8f1d0 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f size = i + size/
8f1e0 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20 4 - prev;.
8f1f0 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 x = mem3.aPool[p
8f200 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a rev-1].u.hdr.siz
8f210 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d e4x & 2;. m
8f220 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 em3.aPool[prev-1
8f230 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d ].u.hdr.size4x =
8f240 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20 size*4 | x;.
8f250 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 mem3.aPool[pr
8f260 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 ev+size-1].u.hdr
8f270 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 .prevSize = size
8f280 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c ;. memsys3L
8f290 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 ink(prev);.
8f2a0 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d i = prev;. }
8f2b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65 else{. size
8f2c0 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 /= 4;. }.
8f2d0 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73 if( size>mem3.s
8f2e0 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 zMaster ){.
8f2f0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 mem3.iMaster =
8f300 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a i;. mem3.sz
8f310 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20 Master = size;.
8f320 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
8f330 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b * Return a block
8f340 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 of memory of at
8f350 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e least nBytes in
8f360 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e size..** Return
8f370 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e NULL if unable.
8f380 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
8f390 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
8f3a0 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 t the necessary
8f3b0 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c mutexes, if any,
8f3c0 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 are.** already
8f3d0 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c held by the call
8f3e0 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 er. Hence "Unsaf
8f3f0 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f e"..*/.static vo
8f400 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f id *memsys3Mallo
8f410 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 cUnsafe(int nByt
8f420 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 75 e){. u32 i;. u
8f430 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 32 32 nBlock;. u32
8f440 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65 toFree;.. asse
8f450 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
8f460 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
8f470 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
8f480 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b sizeof(Mem3Block
8f490 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42 )==8 );. if( nB
8f4a0 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e yte<=12 ){. n
8f4b0 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c Block = 2;. }el
8f4c0 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d se{. nBlock =
8f4d0 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b (nByte + 11)/8;
8f4e0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e . }. assert( n
8f4f0 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f Block>=2 );.. /
8f500 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c * STEP 1:. ** L
8f510 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 ook for an entry
8f520 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 of the correct
8f530 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74 size in either t
8f540 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68 he small. ** ch
8f550 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20 unk table or in
8f560 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 the large chunk
8f570 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 hash table. Thi
8f580 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73 s is. ** succes
8f590 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65 sful most of the
8f5a0 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74 time (about 9 t
8f5b0 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e imes out of 10).
8f5c0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f . */. if( nBlo
8f5d0 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 ck <= MX_SMALL )
8f5e0 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61 {. i = mem3.a
8f5f0 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d iSmall[nBlock-2]
8f600 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b ;. if( i>0 ){
8f610 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e . memsys3Un
8f620 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 linkFromList(i,
8f630 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 &mem3.aiSmall[nB
8f640 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20 lock-2]);.
8f650 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 return memsys3Ch
8f660 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b eckout(i, nBlock
8f670 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 );. }. }else
8f680 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d {. int hash =
8f690 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48 nBlock % N_HASH
8f6a0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33 ;. for(i=mem3
8f6b0 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69 .aiHash[hash]; i
8f6c0 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c >0; i=mem3.aPool
8f6d0 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 [i].u.list.next)
8f6e0 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 {. if( mem3
8f6f0 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 .aPool[i-1].u.hd
8f700 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f r.size4x/4==nBlo
8f710 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 ck ){. me
8f720 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c msys3UnlinkFromL
8f730 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 ist(i, &mem3.aiH
8f740 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20 ash[hash]);.
8f750 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 return memsy
8f760 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 s3Checkout(i, nB
8f770 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 lock);. }.
8f780 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 }. }.. /* S
8f790 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20 TEP 2:. ** Try
8f7a0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61 to satisfy the a
8f7b0 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 llocation by car
8f7c0 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 ving a piece off
8f7d0 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a of the end. **
8f7e0 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 of the master c
8f7f0 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 hunk. This step
8f800 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69 usually works i
8f810 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a f step 1 fails..
8f820 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e */. if( mem3.
8f830 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b szMaster>=nBlock
8f840 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d ){. return m
8f850 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 emsys3FromMaster
8f860 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a (nBlock);. }...
8f870 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20 /* STEP 3: .
8f880 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 ** Loop through
8f890 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f the entire memo
8f8a0 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 ry pool. Coales
8f8b0 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 ce adjacent free
8f8c0 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52 . ** chunks. R
8f8d0 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73 ecompute the mas
8f8e0 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65 ter chunk as the
8f8f0 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63 68 largest free ch
8f900 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74 unk.. ** Then t
8f910 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69 ry again to sati
8f920 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 sfy the allocati
8f930 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 20 on by carving a
8f940 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f piece off. ** o
8f950 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 f the end of the
8f960 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 master chunk.
8f970 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65 6e This step happen
8f980 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 s very. ** rare
8f990 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20 ly (we hope!).
8f9a0 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d */. for(toFree=
8f9b0 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65 nBlock*16; toFre
8f9c0 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 e<(mem3.nPool*16
8f9d0 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b ); toFree *= 2){
8f9e0 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f . memsys3OutO
8f9f0 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b fMemory(toFree);
8fa00 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d . if( mem3.iM
8fa10 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d aster ){. m
8fa20 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e emsys3Link(mem3.
8fa30 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 iMaster);.
8fa40 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30 mem3.iMaster = 0
8fa50 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d ;. mem3.szM
8fa60 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d aster = 0;. }
8fa70 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
8fa80 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 N_HASH; i++){.
8fa90 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 memsys3Merge
8faa0 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d (&mem3.aiHash[i]
8fab0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
8fac0 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c (i=0; i<MX_SMALL
8fad0 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 -1; i++){.
8fae0 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 memsys3Merge(&me
8faf0 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a m3.aiSmall[i]);.
8fb00 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 }. if( me
8fb10 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 m3.szMaster ){.
8fb20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
8fb30 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 nk(mem3.iMaster)
8fb40 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 ;. if( mem3
8fb50 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 .szMaster>=nBloc
8fb60 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 k ){. ret
8fb70 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d urn memsys3FromM
8fb80 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 aster(nBlock);.
8fb90 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
8fba0 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f .. /* If none o
8fbb0 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b f the above work
8fbc0 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c ed, then we fail
8fbd0 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b . */. return 0;
8fbe0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
8fbf0 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 n outstanding me
8fc00 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e mory allocation.
8fc10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
8fc20 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
8fc30 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 t the necessary
8fc40 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c mutexes, if any,
8fc50 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 are.** already
8fc60 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c held by the call
8fc70 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 er. Hence "Unsaf
8fc80 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 e"..*/.void mems
8fc90 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f ys3FreeUnsafe(vo
8fca0 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d id *pOld){. Mem
8fcb0 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d 3Block *p = (Mem
8fcc0 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20 3Block*)pOld;.
8fcd0 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a int i;. u32 siz
8fce0 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 e, x;. assert(
8fcf0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
8fd00 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 ld(mem3.mutex) )
8fd10 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65 ;. assert( p>me
8fd20 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d m3.aPool && p<&m
8fd30 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e em3.aPool[mem3.n
8fd40 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 70 Pool] );. i = p
8fd50 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 - mem3.aPool;.
8fd60 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 assert( (mem3.a
8fd70 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
8fd80 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b 0a size4x&1)==1 );.
8fd90 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 size = mem3.aP
8fda0 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
8fdb0 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 ize4x/4;. asser
8fdc0 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e t( i+size<=mem3.
8fdd0 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d nPool+1 );. mem
8fde0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
8fdf0 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b dr.size4x &= ~1;
8fe00 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b . mem3.aPool[i+
8fe10 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 size-1].u.hdr.pr
8fe20 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 evSize = size;.
8fe30 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 mem3.aPool[i+si
8fe40 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 ze-1].u.hdr.size
8fe50 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73 4x &= ~2;. mems
8fe60 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f ys3Link(i);.. /
8fe70 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20 * Try to expand
8fe80 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67 the master using
8fe90 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64 the newly freed
8fea0 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 20 chunk */. if(
8feb0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a mem3.iMaster ){.
8fec0 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 while( (mem3
8fed0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
8fee0 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ter-1].u.hdr.siz
8fef0 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20 e4x&2)==0 ){.
8ff00 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 size = mem3.a
8ff10 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
8ff20 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 r-1].u.hdr.prevS
8ff30 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e ize;. mem3.
8ff40 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 3b iMaster -= size;
8ff50 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 . mem3.szMa
8ff60 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 20 ster += size;.
8ff70 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e memsys3Unlin
8ff80 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b k(mem3.iMaster);
8ff90 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e . x = mem3.
8ffa0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
8ffb0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 er-1].u.hdr.size
8ffc0 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 4x & 2;. me
8ffd0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
8ffe0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 aster-1].u.hdr.s
8fff0 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d ize4x = mem3.szM
90000 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 aster*4 | x;.
90010 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 mem3.aPool[me
90020 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e m3.iMaster+mem3.
90030 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 szMaster-1].u.hd
90040 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d r.prevSize = mem
90050 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 3.szMaster;.
90060 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 }. x = mem3.a
90070 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
90080 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 r-1].u.hdr.size4
90090 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 65 x & 2;. while
900a0 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 ( (mem3.aPool[me
900b0 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e m3.iMaster+mem3.
900c0 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 szMaster-1].u.hd
900d0 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 29 r.size4x&1)==0 )
900e0 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 {. memsys3U
900f0 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 nlink(mem3.iMast
90100 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 er+mem3.szMaster
90110 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a );. mem3.sz
90120 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61 Master += mem3.a
90130 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
90140 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d r+mem3.szMaster-
90150 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
90160 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 4;. mem3.aP
90170 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
90180 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
90190 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 = mem3.szMaster
901a0 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 *4 | x;. me
901b0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
901c0 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 aster+mem3.szMas
901d0 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 ter-1].u.hdr.pre
901e0 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d vSize = mem3.szM
901f0 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d aster;. }. }
90200 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
90210 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 the size of an
90220 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f outstanding allo
90230 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73 cation, in bytes
90240 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72 . The.** size r
90250 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68 eturned omits th
90260 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 e 8-byte header
90270 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73 20 overhead. This
90280 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f only.** works fo
90290 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72 r chunks that ar
902a0 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 e currently chec
902b0 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 ked out..*/.stat
902c0 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 69 ic int memsys3Si
902d0 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 4d ze(void *p){. M
902e0 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b em3Block *pBlock
902f0 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
90300 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f 63 eturn 0;. pBloc
90310 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 k = (Mem3Block*)
90320 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 p;. assert( (pB
90330 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 lock[-1].u.hdr.s
90340 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b 0a 20 ize4x&1)!=0 );.
90350 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b return (pBlock[
90360 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
90370 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f &~3)*2 - 4;.}../
90380 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 *.** Round up a
90390 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 request size to
903a0 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 the next valid a
903b0 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a llocation size..
903c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
903d0 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 6e 74 msys3Roundup(int
903e0 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 32 n){. if( n<=12
903f0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 ){. return 1
90400 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2;. }else{.
90410 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 7e return ((n+11)&~
90420 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 7) - 4;. }.}../
90430 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 *.** Allocate nB
90440 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a ytes of memory..
90450 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
90460 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e memsys3Malloc(in
90470 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c t nBytes){. sql
90480 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 ite3_int64 *p;.
90490 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e assert( nBytes>
904a0 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 0 ); /*
904b0 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 72 malloc.c filter
904c0 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65 71 s out 0 byte req
904d0 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 79 uests */. memsy
904e0 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d s3Enter();. p =
904f0 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e memsys3MallocUn
90500 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 safe(nBytes);.
90510 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a memsys3Leave();.
90520 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
90530 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p; .}../*.** Fre
90540 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 e memory..*/.voi
90550 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f d memsys3Free(vo
90560 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 id *pPrior){. a
90570 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b ssert( pPrior );
90580 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 . memsys3Enter(
90590 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65 );. memsys3Free
905a0 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a Unsafe(pPrior);.
905b0 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 memsys3Leave()
905c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
905d0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e e the size of an
905e0 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 existing memory
905f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76 allocation.*/.v
90600 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 6c oid *memsys3Real
90610 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 loc(void *pPrior
90620 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 , int nBytes){.
90630 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 int nOld;. voi
90640 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69 d *p;. if( pPri
90650 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 or==0 ){. ret
90660 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c urn sqlite3_mall
90670 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a oc(nBytes);. }.
90680 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 if( nBytes<=0
90690 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
906a0 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20 ree(pPrior);.
906b0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
906c0 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53 nOld = memsys3S
906d0 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 ize(pPrior);. i
906e0 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 f( nBytes<=nOld
906f0 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d && nBytes>=nOld-
90700 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72 128 ){. retur
90710 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 n pPrior;. }.
90720 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a memsys3Enter();.
90730 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c p = memsys3Mal
90740 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 locUnsafe(nBytes
90750 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 );. if( p ){.
90760 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 65 if( nOld<nByte
90770 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 s ){. memcp
90780 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c y(p, pPrior, nOl
90790 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 d);. }else{.
907a0 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 memcpy(p, p
907b0 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b 0a Prior, nBytes);.
907c0 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 73 }. memsys
907d0 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 3FreeUnsafe(pPri
907e0 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 or);. }. memsy
907f0 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 s3Leave();. ret
90800 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
90810 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 Initialize this
90820 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 module..*/.stati
90830 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e 69 c int memsys3Ini
90840 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
90850 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
90860 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
90870 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f if( !sqlite3Glo
90880 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 20 balConfig.pHeap
90890 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
908a0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
908b0 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 6f . /* Store a po
908c0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d inter to the mem
908d0 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c 6f ory block in glo
908e0 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d 65 bal structure me
908f0 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 m3. */. assert(
90900 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 sizeof(Mem3Bloc
90910 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 2e k)==8 );. mem3.
90920 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c 6f aPool = (Mem3Blo
90930 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 ck *)sqlite3Glob
90940 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a alConfig.pHeap;.
90950 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 28 mem3.nPool = (
90960 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
90970 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a 65 fig.nHeap / size
90980 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 2d of(Mem3Block)) -
90990 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 2;.. /* Initia
909a0 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72 20 lize the master
909b0 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d 33 block. */. mem3
909c0 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 .szMaster = mem3
909d0 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 6d .nPool;. mem3.m
909e0 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73 nMaster = mem3.s
909f0 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 2e zMaster;. mem3.
90a00 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 6d iMaster = 1;. m
90a10 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e 68 em3.aPool[0].u.h
90a20 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 6d dr.size4x = (mem
90a30 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 2b 3.szMaster<<2) +
90a40 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 2;. mem3.aPool
90a50 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 [mem3.nPool].u.h
90a60 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 dr.prevSize = me
90a70 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 m3.nPool;. mem3
90a80 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f .aPool[mem3.nPoo
90a90 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 l].u.hdr.size4x
90aa0 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 = 1;.. return S
90ab0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
90ac0 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 ** Deinitialize
90ad0 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a this module..*/.
90ae0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
90af0 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 ys3Shutdown(void
90b00 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
90b10 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
90b20 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
90b30 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 n;.}..../*.** Op
90b40 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 en the file indi
90b50 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 cated and write
90b60 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 a log of all unf
90b70 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 reed memory .**
90b80 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f allocations into
90b90 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 that log..*/.SQ
90ba0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
90bb0 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 33 d sqlite3Memsys3
90bc0 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 Dump(const char
90bd0 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 23 69 66 *zFilename){.#if
90be0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
90bf0 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 . FILE *out;.
90c00 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75 33 32 20 u32 i, j;. u32
90c10 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a 46 69 6c size;. if( zFil
90c20 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c ename==0 || zFil
90c30 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 ename[0]==0 ){.
90c40 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b out = stdout;
90c50 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 . }else{. ou
90c60 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e t = fopen(zFilen
90c70 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 ame, "w");. i
90c80 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 f( out==0 ){.
90c90 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 fprintf(stder
90ca0 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f r, "** Unable to
90cb0 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 output memory d
90cc0 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a ebug output log:
90cd0 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 %s **\n",.
90ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
90cf0 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 zFilename);.
90d00 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
90d10 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e . }. memsys3En
90d20 74 65 72 28 29 3b 0a 20 20 66 70 72 69 6e 74 66 ter();. fprintf
90d30 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53 3a 5c 6e (out, "CHUNKS:\n
90d40 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 ");. for(i=1; i
90d50 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20 69 2b <=mem3.nPool; i+
90d60 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20 20 73 69 =size/4){. si
90d70 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
90d80 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
90d90 78 3b 0a 20 20 20 20 69 66 28 20 73 69 7a 65 2f x;. if( size/
90da0 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 70 4<=1 ){. fp
90db0 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 73 rintf(out, "%p s
90dc0 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c 20 26 6d ize error\n", &m
90dd0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 em3.aPool[i]);.
90de0 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 assert( 0 )
90df0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
90e00 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 73 69 }. if( (si
90e10 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d 65 6d 33 ze&1)==0 && mem3
90e20 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d .aPool[i+size/4-
90e30 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
90e40 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a 20 20 20 e!=size/4 ){.
90e50 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
90e60 22 25 70 20 74 61 69 6c 20 73 69 7a 65 20 64 6f "%p tail size do
90e70 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c 6e 22 2c es not match\n",
90e80 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 &mem3.aPool[i])
90e90 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
90ea0 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
90eb0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
90ec0 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 ((mem3.aPool[i+s
90ed0 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 73 ize/4-1].u.hdr.s
90ee0 69 7a 65 34 78 26 32 29 3e 3e 31 29 21 3d 28 73 ize4x&2)>>1)!=(s
90ef0 69 7a 65 26 31 29 20 29 7b 0a 20 20 20 20 20 20 ize&1) ){.
90f00 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 fprintf(out, "%p
90f10 20 74 61 69 6c 20 63 68 65 63 6b 6f 75 74 20 62 tail checkout b
90f20 69 74 20 69 73 20 69 6e 63 6f 72 72 65 63 74 5c it is incorrect\
90f30 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b n", &mem3.aPool[
90f40 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 i]);. asser
90f50 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 t( 0 );. br
90f60 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
90f70 66 28 20 73 69 7a 65 26 31 20 29 7b 0a 20 20 20 f( size&1 ){.
90f80 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
90f90 22 25 70 20 25 36 64 20 62 79 74 65 73 20 63 68 "%p %6d bytes ch
90fa0 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c 20 26 6d ecked out\n", &m
90fb0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 em3.aPool[i], (s
90fc0 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 ize/4)*8-8);.
90fd0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 70 }else{. fp
90fe0 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 rintf(out, "%p %
90ff0 36 64 20 62 79 74 65 73 20 66 72 65 65 25 73 5c 6d bytes free%s\
91000 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b n", &mem3.aPool[
91010 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 i], (size/4)*8-8
91020 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
91030 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73 i==mem3.iMas
91040 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73 74 65 72 ter ? " **master
91050 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20 20 20 7d **" : "");. }
91060 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
91070 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b i<MX_SMALL-1; i+
91080 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 +){. if( mem3
91090 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30 20 29 .aiSmall[i]==0 )
910a0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 continue;. f
910b0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 73 6d 61 printf(out, "sma
910c0 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 ll(%2d):", i);.
910d0 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e for(j = mem3.
910e0 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b aiSmall[i]; j>0;
910f0 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d j=mem3.aPool[j]
91100 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 .u.list.next){.
91110 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
91120 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26 6d 65 , " %p(%d)", &me
91130 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 m3.aPool[j],.
91140 20 20 20 20 20 20 20 20 20 20 20 28 6d 65 6d 33 (mem3
91150 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 .aPool[j-1].u.hd
91160 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29 r.size4x/4)*8-8)
91170 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69 ;. }. fpri
91180 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 ntf(out, "\n");
91190 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
911a0 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a i<N_HASH; i++){.
911b0 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 48 if( mem3.aiH
911c0 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 ash[i]==0 ) cont
911d0 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 inue;. fprint
911e0 66 28 6f 75 74 2c 20 22 68 61 73 68 28 25 32 64 f(out, "hash(%2d
911f0 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 ):", i);. for
91200 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48 61 73 68 (j = mem3.aiHash
91210 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 [i]; j>0; j=mem3
91220 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 .aPool[j].u.list
91230 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 .next){. fp
91240 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 rintf(out, " %p(
91250 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f %d)", &mem3.aPoo
91260 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 l[j],.
91270 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b (mem3.aPool[
91280 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 j-1].u.hdr.size4
91290 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d x/4)*8-8);. }
912a0 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 . fprintf(out
912b0 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 , "\n"); . }.
912c0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 61 fprintf(out, "ma
912d0 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 ster=%d\n", mem3
912e0 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 66 70 72 .iMaster);. fpr
912f0 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f 77 55 73 intf(out, "nowUs
91300 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e ed=%d\n", mem3.n
91310 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 73 7a Pool*8 - mem3.sz
91320 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 66 70 72 Master*8);. fpr
91330 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 55 73 65 intf(out, "mxUse
91340 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 d=%d\n", mem3.nP
91350 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d 6e 4d ool*8 - mem3.mnM
91360 61 73 74 65 72 2a 38 29 3b 0a 20 20 73 71 6c 69 aster*8);. sqli
91370 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
91380 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 69 mem3.mutex);. i
91390 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29 f( out==stdout )
913a0 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 74 64 {. fflush(std
913b0 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 out);. }else{.
913c0 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a fclose(out);.
913d0 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 }.#else. UNUS
913e0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 46 69 ED_PARAMETER(zFi
913f0 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a lename);.#endif.
91400 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
91410 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c utine is the onl
91420 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 y routine in thi
91430 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 s file with exte
91440 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 rnal .** linkage
91450 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 ..**.** Populate
91460 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d the low-level m
91470 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
91480 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 function pointe
91490 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 rs in.** sqlite3
914a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 GlobalConfig.m w
914b0 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 ith pointers to
914c0 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 the routines in
914d0 74 68 69 73 20 66 69 6c 65 2e 20 54 68 65 0a 2a this file. The.*
914e0 2a 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 * arguments spec
914f0 69 66 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 ify the block of
91500 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67 memory to manag
91510 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
91520 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 utine is only ca
91530 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f lled by sqlite3_
91540 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20 74 68 config(), and th
91550 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f erefore.** is no
91560 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 t required to be
91570 20 74 68 72 65 61 64 73 61 66 65 20 28 69 74 20 threadsafe (it
91580 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49 is not)..*/.SQLI
91590 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
915a0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
915b0 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d hods *sqlite3Mem
915c0 47 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 GetMemsys3(void)
915d0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 {. static const
915e0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
915f0 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 hods mempoolMeth
91600 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d ods = {. mem
91610 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 sys3Malloc,.
91620 20 6d 65 6d 73 79 73 33 46 72 65 65 2c 0a 20 20 memsys3Free,.
91630 20 20 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f memsys3Reallo
91640 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53 c,. memsys3S
91650 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 ize,. memsys
91660 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 3Roundup,. m
91670 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20 20 20 20 emsys3Init,.
91680 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e memsys3Shutdown
91690 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 ,. 0. };.
916a0 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d return &mempoolM
916b0 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 ethods;.}..#endi
916c0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 f /* SQLITE_ENAB
916d0 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f LE_MEMSYS3 */../
916e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
916f0 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a nd of mem3.c ***
91700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
91730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
91740 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63 egin file mem5.c
91750 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
91760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
91780 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 *.** 2007 Octobe
91790 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 14.**.** The a
917a0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
917b0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
917c0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
917d0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
917e0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
917f0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
91800 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
91810 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
91820 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
91830 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
91840 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
91850 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
91860 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
91870 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
91880 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
91890 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
918a0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
918b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
918c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
918d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
918e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
918f0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
91900 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
91910 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
91920 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 mplement a memor
91930 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 y.** allocation
91940 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 subsystem for us
91950 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a e by SQLite. .**
91960 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
91970 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
91980 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
91990 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a tem omits all.**
919a0 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 use of malloc()
919b0 2e 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 . The SQLite use
919c0 72 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f r supplies a blo
919d0 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ck of memory.**
919e0 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 before calling s
919f0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
91a00 65 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 e() from which a
91a10 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 llocations.** ar
91a20 65 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 e made and retur
91a30 6e 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c ned by the xMall
91a40 6f 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f oc() and xReallo
91a50 63 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e c() .** implemen
91a60 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 tations. Once sq
91a70 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
91a80 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c () has been call
91a90 65 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e ed,.** the amoun
91aa0 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 t of memory avai
91ab0 6c 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 lable to SQLite
91ac0 69 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e is fixed and can
91ad0 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 not.** be change
91ae0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 d..**.** This ve
91af0 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d rsion of the mem
91b00 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
91b10 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c ubsystem is incl
91b20 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 uded.** in the b
91b30 75 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c uild only if SQL
91b40 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
91b50 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a S5 is defined..*
91b60 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 35 2e 63 *.** $Id: mem5.c
91b70 2c 76 20 31 2e 31 39 20 32 30 30 38 2f 31 31 2f ,v 1.19 2008/11/
91b80 31 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 19 16:52:44 dani
91b90 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
91ba0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ../*.** This ver
91bb0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
91bc0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ry allocator is
91bd0 75 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 0a used only when .
91be0 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ** SQLITE_ENABLE
91bf0 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 _MEMSYS5 is defi
91c00 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ned..*/.#ifdef S
91c10 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
91c20 53 59 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 SYS5../*.** A mi
91c30 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e nimum allocation
91c40 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
91c50 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
91c60 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 4c structure..** L
91c70 61 72 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e arger allocation
91c80 73 20 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f s are an array o
91c90 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 f these structur
91ca0 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 es where the.**
91cb0 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 size of the arra
91cc0 79 20 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20 y is a power of
91cd0 32 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 2..*/.typedef st
91ce0 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65 ruct Mem5Link Me
91cf0 6d 35 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d m5Link;.struct M
91d00 65 6d 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20 em5Link {. int
91d10 6e 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 next; /* I
91d20 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 66 72 65 ndex of next fre
91d30 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74 e chunk */. int
91d40 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 prev; /*
91d50 49 6e 64 65 78 20 6f 66 20 70 72 65 76 69 6f 75 Index of previou
91d60 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a s free chunk */.
91d70 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 };../*.** Maximu
91d80 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c m size of any al
91d90 6c 6f 63 61 74 69 6f 6e 20 69 73 20 28 28 31 3c location is ((1<
91da0 3c 4c 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 6e 41 <LOGMAX)*mem5.nA
91db0 74 6f 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 6d tom). Since.** m
91dc0 65 6d 35 2e 6e 41 74 6f 6d 20 69 73 20 61 6c 77 em5.nAtom is alw
91dd0 61 79 73 20 61 74 20 6c 65 61 73 74 20 38 2c 20 ays at least 8,
91de0 74 68 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c this is not real
91df0 6c 79 20 61 20 70 72 61 63 74 69 63 61 6c 0a 2a ly a practical.*
91e00 2a 20 6c 69 6d 69 74 61 74 69 6f 6e 2e 0a 2a 2f * limitation..*/
91e10 0a 23 64 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 .#define LOGMAX
91e20 33 30 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 30../*.** Masks
91e30 75 73 65 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 used for mem5.aC
91e40 74 72 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a trl[] elements..
91e50 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f */.#define CTRL_
91e60 4c 4f 47 53 49 5a 45 20 20 30 78 31 66 20 20 20 LOGSIZE 0x1f
91e70 20 2f 2a 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 /* Log2 Size of
91e80 20 74 68 69 73 20 62 6c 6f 63 6b 20 72 65 6c 61 this block rela
91e90 74 69 76 65 20 74 6f 20 50 4f 57 32 5f 4d 49 4e tive to POW2_MIN
91ea0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c */.#define CTRL
91eb0 5f 46 52 45 45 20 20 20 20 20 30 78 32 30 20 20 _FREE 0x20
91ec0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 74 /* True if not
91ed0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
91ee0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 ./*.** All of th
91ef0 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c e static variabl
91f00 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 es used by this
91f10 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 module are colle
91f20 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 cted.** into a s
91f30 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 ingle structure
91f40 6e 61 6d 65 64 20 22 6d 65 6d 35 22 2e 20 20 54 named "mem5". T
91f50 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 his is to keep t
91f60 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 he.** static var
91f70 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 iables organized
91f80 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e and to reduce n
91f90 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 amespace polluti
91fa0 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 on.** when this
91fb0 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e module is combin
91fc0 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e ed with other in
91fd0 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f the amalgamatio
91fe0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c n..*/.static SQL
91ff0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d ITE_WSD struct M
92000 65 6d 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a em5Global {. /*
92010 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 . ** Memory ava
92020 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 ilable for alloc
92030 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74 ation. */. int
92040 20 6e 41 74 6f 6d 3b 20 20 20 20 20 20 20 2f 2a nAtom; /*
92050 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 Smallest possib
92060 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e le allocation in
92070 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 bytes */. int
92080 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 nBlock; /*
92090 4e 75 6d 62 65 72 20 6f 66 20 6e 41 74 6f 6d 20 Number of nAtom
920a0 73 69 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e 20 sized blocks in
920b0 7a 50 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a 7a zPool */. u8 *z
920c0 50 6f 6f 6c 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 Pool;. . /*.
920d0 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 ** Mutex to cont
920e0 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 rol access to th
920f0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
92100 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 ion subsystem..
92110 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
92120 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f tex *mutex;.. /
92130 2a 0a 20 20 2a 2a 20 50 65 72 66 6f 72 6d 61 6e *. ** Performan
92140 63 65 20 73 74 61 74 69 73 74 69 63 73 0a 20 20 ce statistics.
92150 2a 2f 0a 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b */. u64 nAlloc;
92160 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
92170 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c l number of call
92180 73 20 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 s to malloc */.
92190 20 75 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b u64 totalAlloc;
921a0 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66 /* Total of
921b0 20 61 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c all malloc call
921c0 73 20 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74 s - includes int
921d0 65 72 6e 61 6c 20 66 72 61 67 20 2a 2f 0a 20 20 ernal frag */.
921e0 75 36 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b u64 totalExcess;
921f0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74 /* Total int
92200 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 ernal fragmentat
92210 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 ion */. u32 cur
92220 72 65 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20 rentOut; /*
92230 43 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 Current checkout
92240 2c 20 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 , including inte
92250 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 rnal fragmentati
92260 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 on */. u32 curr
92270 65 6e 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20 43 entCount; /* C
92280 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 urrent number of
92290 20 64 69 73 74 69 6e 63 74 20 63 68 65 63 6b 6f distinct checko
922a0 75 74 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 uts */. u32 max
922b0 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Out; /*
922c0 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 Maximum instanta
922d0 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 4f 75 74 neous currentOut
922e0 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 43 6f 75 */. u32 maxCou
922f0 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 nt; /* Max
92300 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f imum instantaneo
92310 75 73 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 us currentCount
92320 2a 2f 0a 20 20 75 33 32 20 6d 61 78 52 65 71 75 */. u32 maxRequ
92330 65 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 72 67 est; /* Larg
92340 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 est allocation (
92350 65 78 63 6c 75 73 69 76 65 20 6f 66 20 69 6e 74 exclusive of int
92360 65 72 6e 61 6c 20 66 72 61 67 29 20 2a 2f 0a 20 ernal frag) */.
92370 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74 . /*. ** List
92380 73 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 s of free blocks
92390 20 6f 66 20 76 61 72 69 6f 75 73 20 73 69 7a 65 of various size
923a0 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69 s.. */. int ai
923b0 46 72 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b Freelist[LOGMAX+
923c0 31 5d 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 1];.. /*. ** S
923d0 70 61 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e pace for trackin
923e0 67 20 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61 g which blocks a
923f0 72 65 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 re checked out a
92400 6e 64 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a nd the size. **
92410 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20 of each block.
92420 20 4f 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c One byte per bl
92430 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a ock.. */. u8 *
92440 61 43 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 20 3d aCtrl;..} mem5 =
92450 20 7b 20 31 39 38 30 34 31 36 37 20 7d 3b 0a 0a { 19804167 };..
92460 23 64 65 66 69 6e 65 20 6d 65 6d 35 20 47 4c 4f #define mem5 GLO
92470 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 35 47 BAL(struct Mem5G
92480 6c 6f 62 61 6c 2c 20 6d 65 6d 35 29 0a 0a 23 64 lobal, mem5)..#d
92490 65 66 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b 28 69 efine MEM5LINK(i
924a0 64 78 29 20 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a dx) ((Mem5Link *
924b0 29 28 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 )(&mem5.zPool[(i
924c0 64 78 29 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 29 dx)*mem5.nAtom])
924d0 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 )../*.** Unlink
924e0 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d the chunk at mem
924f0 35 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 5.aPool[i] from
92500 6c 69 73 74 20 69 74 20 69 73 20 63 75 72 72 65 list it is curre
92510 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20 ntly.** on. It
92520 73 68 6f 75 6c 64 20 62 65 20 66 6f 75 6e 64 20 should be found
92530 6f 6e 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 on mem5.aiFreeli
92540 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f st[iLogsize]..*/
92550 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
92560 73 79 73 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69 sys5Unlink(int i
92570 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b , int iLogsize){
92580 0a 20 20 69 6e 74 20 6e 65 78 74 2c 20 70 72 65 . int next, pre
92590 76 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d v;. assert( i>=
925a0 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 0 && i<mem5.nBlo
925b0 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ck );. assert(
925c0 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 iLogsize>=0 && i
925d0 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 Logsize<=LOGMAX
925e0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 );. assert( (me
925f0 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 m5.aCtrl[i] & CT
92600 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f RL_LOGSIZE)==iLo
92610 67 73 69 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 74 gsize );.. next
92620 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e = MEM5LINK(i)->
92630 6e 65 78 74 3b 0a 20 20 70 72 65 76 20 3d 20 4d next;. prev = M
92640 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 EM5LINK(i)->prev
92650 3b 0a 20 20 69 66 28 20 70 72 65 76 3c 30 20 29 ;. if( prev<0 )
92660 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 {. mem5.aiFre
92670 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 elist[iLogsize]
92680 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b = next;. }else{
92690 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 72 . MEM5LINK(pr
926a0 65 76 29 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74 ev)->next = next
926b0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 ;. }. if( next
926c0 3e 3d 30 20 29 7b 0a 20 20 20 20 4d 45 4d 35 4c >=0 ){. MEM5L
926d0 49 4e 4b 28 6e 65 78 74 29 2d 3e 70 72 65 76 20 INK(next)->prev
926e0 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f = prev;. }.}../
926f0 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 *.** Link the ch
92700 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f unk at mem5.aPoo
92710 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 l[i] so that is
92720 6f 6e 20 74 68 65 20 69 4c 6f 67 73 69 7a 65 0a on the iLogsize.
92730 2a 2a 20 66 72 65 65 20 6c 69 73 74 2e 0a 2a 2f ** free list..*/
92740 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
92750 73 79 73 35 4c 69 6e 6b 28 69 6e 74 20 69 2c 20 sys5Link(int i,
92760 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 int iLogsize){.
92770 20 69 6e 74 20 78 3b 0a 20 20 61 73 73 65 72 74 int x;. assert
92780 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
92790 68 65 6c 64 28 6d 65 6d 35 2e 6d 75 74 65 78 29 held(mem5.mutex)
927a0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e );. assert( i>
927b0 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c =0 && i<mem5.nBl
927c0 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ock );. assert(
927d0 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 iLogsize>=0 &&
927e0 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 iLogsize<=LOGMAX
927f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d );. assert( (m
92800 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 em5.aCtrl[i] & C
92810 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c TRL_LOGSIZE)==iL
92820 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 78 20 3d ogsize );.. x =
92830 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 MEM5LINK(i)->ne
92840 78 74 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 xt = mem5.aiFree
92850 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a list[iLogsize];.
92860 20 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 MEM5LINK(i)->p
92870 72 65 76 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 rev = -1;. if(
92880 78 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 x>=0 ){. asse
92890 72 74 28 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 rt( x<mem5.nBloc
928a0 6b 20 29 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e k );. MEM5LIN
928b0 4b 28 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0a K(x)->prev = i;.
928c0 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65 }. mem5.aiFre
928d0 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 elist[iLogsize]
928e0 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 = i;.}../*.** If
928f0 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 the STATIC_MEM
92900 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 mutex is not alr
92910 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 eady held, obtai
92920 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 n it now. The mu
92930 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 tex.** will alre
92940 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74 ady be held (obt
92950 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e ained by code in
92960 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a malloc.c) if.**
92970 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
92980 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 nfig.bMemStat is
92990 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 true..*/.static
929a0 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e 74 void memsys5Ent
929b0 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 er(void){. if(
929c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
929d0 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 fig.bMemstat==0
929e0 26 26 20 6d 65 6d 35 2e 6d 75 74 65 78 3d 3d 30 && mem5.mutex==0
929f0 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 74 ){. mem5.mut
92a00 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
92a10 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
92a20 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b TEX_STATIC_MEM);
92a30 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
92a40 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e utex_enter(mem5.
92a50 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 mutex);.}.static
92a60 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 65 61 void memsys5Lea
92a70 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 ve(void){. sqli
92a80 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
92a90 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a mem5.mutex);.}..
92aa0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
92ab0 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 size of an outs
92ac0 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 tanding allocati
92ad0 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 on, in bytes. T
92ae0 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 he.** size retur
92af0 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d ned omits the 8-
92b00 62 79 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 byte header over
92b10 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 head. This only
92b20 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 .** works for ch
92b30 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 unks that are cu
92b40 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 rrently checked
92b50 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 out..*/.static i
92b60 6e 74 20 6d 65 6d 73 79 73 35 53 69 7a 65 28 76 nt memsys5Size(v
92b70 6f 69 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 oid *p){. int i
92b80 53 69 7a 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 Size = 0;. if(
92b90 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d p ){. int i =
92ba0 20 28 28 75 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a ((u8 *)p-mem5.z
92bb0 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 6f 6d Pool)/mem5.nAtom
92bc0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e ;. assert( i>
92bd0 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c =0 && i<mem5.nBl
92be0 6f 63 6b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 ock );. iSize
92bf0 20 3d 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 2a 20 = mem5.nAtom *
92c00 28 31 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 (1 << (mem5.aCtr
92c10 6c 5b 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a l[i]&CTRL_LOGSIZ
92c20 45 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 E));. }. retur
92c30 6e 20 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a n iSize;.}../*.*
92c40 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 * Find the first
92c50 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 entry on the fr
92c60 65 65 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e eelist iLogsize.
92c70 20 20 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a Unlink that.**
92c80 20 65 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 entry and retur
92c90 6e 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f n its index. .*/
92ca0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 .static int mems
92cb0 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 ys5UnlinkFirst(i
92cc0 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 nt iLogsize){.
92cd0 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 int i;. int iFi
92ce0 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rst;.. assert(
92cf0 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 iLogsize>=0 && i
92d00 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 Logsize<=LOGMAX
92d10 29 3b 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 );. i = iFirst
92d20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 = mem5.aiFreelis
92d30 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 t[iLogsize];. a
92d40 73 73 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 ssert( iFirst>=0
92d50 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 );. while( i>0
92d60 20 29 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 ){. if( i<iF
92d70 69 72 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 irst ) iFirst =
92d80 69 3b 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c i;. i = MEM5L
92d90 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 INK(i)->next;.
92da0 7d 0a 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e }. memsys5Unlin
92db0 6b 28 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 k(iFirst, iLogsi
92dc0 7a 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 ze);. return iF
92dd0 69 72 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 irst;.}../*.** R
92de0 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 eturn a block of
92df0 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 memory of at le
92e00 61 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 ast nBytes in si
92e10 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 ze..** Return NU
92e20 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2f LL if unable..*/
92e30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
92e40 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 msys5MallocUnsaf
92e50 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 e(int nByte){.
92e60 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
92e70 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 6d /* Index of a m
92e80 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 em5.aPool[] slot
92e90 20 2a 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b 20 */. int iBin;
92ea0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
92eb0 69 6e 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 65 into mem5.aiFree
92ec0 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 list[] */. int
92ed0 69 46 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a 20 iFullSz; /*
92ee0 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69 Size of allocati
92ef0 6f 6e 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f on rounded up to
92f00 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a 20 power of 2 */.
92f10 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 20 int iLogsize;
92f20 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 75 /* Log2 of iFu
92f30 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f llSz/POW2_MIN */
92f40 0a 0a 20 20 2f 2a 20 4b 65 65 70 20 74 72 61 63 .. /* Keep trac
92f50 6b 20 6f 66 20 74 68 65 20 6d 61 78 69 6d 75 6d k of the maximum
92f60 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 allocation requ
92f70 65 73 74 2e 20 20 45 76 65 6e 20 75 6e 66 75 6c est. Even unful
92f80 66 69 6c 6c 65 64 0a 20 20 2a 2a 20 72 65 71 75 filled. ** requ
92f90 65 73 74 73 20 61 72 65 20 63 6f 75 6e 74 65 64 ests are counted
92fa0 20 2a 2f 0a 20 20 69 66 28 20 28 75 33 32 29 6e */. if( (u32)n
92fb0 42 79 74 65 3e 6d 65 6d 35 2e 6d 61 78 52 65 71 Byte>mem5.maxReq
92fc0 75 65 73 74 20 29 7b 0a 20 20 20 20 6d 65 6d 35 uest ){. mem5
92fd0 2e 6d 61 78 52 65 71 75 65 73 74 20 3d 20 6e 42 .maxRequest = nB
92fe0 79 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 yte;. }.. /* R
92ff0 6f 75 6e 64 20 6e 42 79 74 65 20 75 70 20 74 6f ound nByte up to
93000 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 the next valid
93010 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 2a 2f 0a power of two */.
93020 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 for(iFullSz=me
93030 6d 35 2e 6e 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 m5.nAtom, iLogsi
93040 7a 65 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 ze=0; iFullSz<nB
93050 79 74 65 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 yte; iFullSz *=
93060 32 2c 20 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 2, iLogsize++){}
93070 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
93080 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
93090 5b 69 4c 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 [iLogsize] conta
930a0 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 ins at least one
930b0 20 66 72 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b free. ** block
930c0 2e 20 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 . If not, then
930d0 73 70 6c 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 split a block of
930e0 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 the next larger
930f0 20 70 6f 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 power of. ** t
93100 77 6f 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 wo in order to c
93110 72 65 61 74 65 20 61 20 6e 65 77 20 66 72 65 65 reate a new free
93120 20 62 6c 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 block of size i
93130 4c 6f 67 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 Logsize.. */.
93140 66 6f 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a for(iBin=iLogsiz
93150 65 3b 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 e; mem5.aiFreeli
93160 73 74 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 st[iBin]<0 && iB
93170 69 6e 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e in<=LOGMAX; iBin
93180 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e ++){}. if( iBin
93190 3e 4c 4f 47 4d 41 58 20 29 20 72 65 74 75 72 6e >LOGMAX ) return
931a0 20 30 3b 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 0;. i = memsys
931b0 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 5UnlinkFirst(iBi
931c0 6e 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 42 69 n);. while( iBi
931d0 6e 3e 69 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 n>iLogsize ){.
931e0 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a int newSize;..
931f0 20 20 20 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 iBin--;.
93200 6e 65 77 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 newSize = 1 << i
93210 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 Bin;. mem5.aC
93220 74 72 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d trl[i+newSize] =
93230 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 CTRL_FREE | iBi
93240 6e 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 n;. memsys5Li
93250 6e 6b 28 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 nk(i+newSize, iB
93260 69 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e in);. }. mem5.
93270 61 43 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 aCtrl[i] = iLogs
93280 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 ize;.. /* Updat
93290 65 20 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 e allocator perf
932a0 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69 ormance statisti
932b0 63 73 2e 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 cs. */. mem5.nA
932c0 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 lloc++;. mem5.t
932d0 6f 74 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 otalAlloc += iFu
932e0 6c 6c 53 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 llSz;. mem5.tot
932f0 61 6c 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c alExcess += iFul
93300 6c 53 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d lSz - nByte;. m
93310 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
93320 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 ++;. mem5.curre
93330 6e 74 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a ntOut += iFullSz
93340 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 ;. if( mem5.max
93350 43 6f 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 Count<mem5.curre
93360 6e 74 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d ntCount ) mem5.m
93370 61 78 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 axCount = mem5.c
93380 75 72 72 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 urrentCount;. i
93390 66 28 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d f( mem5.maxOut<m
933a0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 em5.currentOut )
933b0 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d mem5.maxOut = m
933c0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a em5.currentOut;.
933d0 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 . /* Return a p
933e0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c ointer to the al
933f0 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 located memory.
93400 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 */. return (voi
93410 64 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 d*)&mem5.zPool[i
93420 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 7d 0a *mem5.nAtom];.}.
93430 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f ./*.** Free an o
93440 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 utstanding memor
93450 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f y allocation..*/
93460 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
93470 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 sys5FreeUnsafe(v
93480 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 oid *pOld){. u3
93490 32 20 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 2 size, iLogsize
934a0 3b 0a 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 20 ;. int iBlock;
934b0 20 20 20 20 20 20 20 20 20 20 20 20 0a 0a 20 20 ..
934c0 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20 74 6f /* Set iBlock to
934d0 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 the index of th
934e0 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 64 20 e block pointed
934f0 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20 0a 20 to by pOld in .
93500 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20 6f 66 ** the array of
93510 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 62 79 74 65 mem5.nAtom byte
93520 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 65 64 20 blocks pointed
93530 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 6f 6f 6c to by mem5.zPool
93540 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f 63 6b 20 .. */. iBlock
93550 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 = ((u8 *)pOld-me
93560 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e m5.zPool)/mem5.n
93570 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 Atom;.. /* Chec
93580 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 k that the point
93590 65 72 20 70 4f 6c 64 20 70 6f 69 6e 74 73 20 74 er pOld points t
935a0 6f 20 61 20 76 61 6c 69 64 2c 20 6e 6f 6e 2d 66 o a valid, non-f
935b0 72 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 ree block. */.
935c0 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 3e 3d assert( iBlock>=
935d0 30 20 26 26 20 69 42 6c 6f 63 6b 3c 6d 65 6d 35 0 && iBlock<mem5
935e0 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 .nBlock );. ass
935f0 65 72 74 28 20 28 28 75 38 20 2a 29 70 4f 6c 64 ert( ((u8 *)pOld
93600 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25 6d 65 6d -mem5.zPool)%mem
93610 35 2e 6e 41 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 5.nAtom==0 );.
93620 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 assert( (mem5.aC
93630 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 trl[iBlock] & CT
93640 52 4c 5f 46 52 45 45 29 3d 3d 30 20 29 3b 0a 0a RL_FREE)==0 );..
93650 20 20 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d iLogsize = mem
93660 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
93670 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a & CTRL_LOGSIZE;.
93680 20 20 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 size = 1<<iLog
93690 73 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 size;. assert(
936a0 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 iBlock+size-1<(u
936b0 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 32)mem5.nBlock )
936c0 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b ;.. mem5.aCtrl[
936d0 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f iBlock] |= CTRL_
936e0 46 52 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 FREE;. mem5.aCt
936f0 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 rl[iBlock+size-1
93700 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a ] |= CTRL_FREE;.
93710 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 assert( mem5.c
93720 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b urrentCount>0 );
93730 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e . assert( mem5.
93740 63 75 72 72 65 6e 74 4f 75 74 3e 3d 28 73 69 7a currentOut>=(siz
93750 65 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 29 20 29 3b e*mem5.nAtom) );
93760 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 . mem5.currentC
93770 6f 75 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 ount--;. mem5.c
93780 75 72 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a urrentOut -= siz
93790 65 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20 e*mem5.nAtom;.
937a0 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 assert( mem5.cur
937b0 72 65 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d rentOut>0 || mem
937c0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 5.currentCount==
937d0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 0 );. assert( m
937e0 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
937f0 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 >0 || mem5.curre
93800 6e 74 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d ntOut==0 );.. m
93810 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b em5.aCtrl[iBlock
93820 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 ] = CTRL_FREE |
93830 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c iLogsize;. whil
93840 65 28 20 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d e( iLogsize<LOGM
93850 41 58 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 AX ){. int iB
93860 75 64 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 uddy;. if( (i
93870 42 6c 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 Block>>iLogsize)
93880 20 26 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 & 1 ){. iB
93890 75 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 uddy = iBlock -
938a0 73 69 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b size;. }else{
938b0 0a 20 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 . iBuddy =
938c0 69 42 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 iBlock + size;.
938d0 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
938e0 20 69 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 iBuddy>=0 );.
938f0 20 20 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 if( (iBuddy+(1
93900 3c 3c 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d <<iLogsize))>mem
93910 35 2e 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 5.nBlock ) break
93920 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 ;. if( mem5.a
93930 43 74 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 Ctrl[iBuddy]!=(C
93940 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 TRL_FREE | iLogs
93950 69 7a 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 ize) ) break;.
93960 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 memsys5Unlink(
93970 69 42 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 iBuddy, iLogsize
93980 29 3b 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b );. iLogsize+
93990 2b 3b 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 +;. if( iBudd
939a0 79 3c 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 y<iBlock ){.
939b0 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 mem5.aCtrl[iBu
939c0 64 64 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 ddy] = CTRL_FREE
939d0 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 | iLogsize;.
939e0 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 mem5.aCtrl[iB
939f0 6c 6f 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 lock] = 0;.
93a00 20 69 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 iBlock = iBuddy
93a10 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
93a20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 mem5.aCtrl[iB
93a30 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 lock] = CTRL_FRE
93a40 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 E | iLogsize;.
93a50 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 mem5.aCtrl[i
93a60 42 75 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 Buddy] = 0;.
93a70 7d 0a 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b }. size *= 2;
93a80 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 . }. memsys5Li
93a90 6e 6b 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 nk(iBlock, iLogs
93aa0 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ize);.}../*.** A
93ab0 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f llocate nBytes o
93ac0 66 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 f memory.*/.stat
93ad0 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 ic void *memsys5
93ae0 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 Malloc(int nByte
93af0 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e s){. sqlite3_in
93b00 74 36 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 t64 *p = 0;. if
93b10 28 20 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 ( nBytes>0 ){.
93b20 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 memsys5Enter()
93b30 3b 0a 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 ;. p = memsys
93b40 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 5MallocUnsafe(nB
93b50 79 74 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 ytes);. memsy
93b60 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 s5Leave();. }.
93b70 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 return (void*)p
93b80 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 ; .}../*.** Free
93b90 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 memory..*/.stat
93ba0 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 ic void memsys5F
93bb0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ree(void *pPrior
93bc0 29 7b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d ){. if( pPrior=
93bd0 3d 30 20 29 7b 0a 61 73 73 65 72 74 28 30 29 3b =0 ){.assert(0);
93be0 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
93bf0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 . memsys5Enter(
93c00 29 3b 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 65 );. memsys5Free
93c10 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a Unsafe(pPrior);.
93c20 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 memsys5Leave()
93c30 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 ; .}../*.** Cha
93c40 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 nge the size of
93c50 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f an existing memo
93c60 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f ry allocation.*/
93c70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
93c80 6d 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 msys5Realloc(voi
93c90 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e d *pPrior, int n
93ca0 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f Bytes){. int nO
93cb0 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 ld;. void *p;.
93cc0 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 if( pPrior==0 )
93cd0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d {. return mem
93ce0 73 79 73 35 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 sys5Malloc(nByte
93cf0 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 s);. }. if( nB
93d00 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 6d ytes<=0 ){. m
93d10 65 6d 73 79 73 35 46 72 65 65 28 70 50 72 69 6f emsys5Free(pPrio
93d20 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 r);. return 0
93d30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d ;. }. nOld = m
93d40 65 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f emsys5Size(pPrio
93d50 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 r);. if( nBytes
93d60 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 <=nOld ){. re
93d70 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d turn pPrior;. }
93d80 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 . memsys5Enter(
93d90 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 );. p = memsys5
93da0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 MallocUnsafe(nBy
93db0 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b tes);. if( p ){
93dc0 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 . memcpy(p, p
93dd0 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 Prior, nOld);.
93de0 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 memsys5FreeUns
93df0 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d afe(pPrior);. }
93e00 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 . memsys5Leave(
93e10 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d );. return p;.}
93e20 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 ../*.** Round up
93e30 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 a request size
93e40 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 to the next vali
93e50 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a d allocation siz
93e60 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
93e70 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 memsys5Roundup(
93e80 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 int n){. int iF
93e90 75 6c 6c 53 7a 3b 0a 20 20 66 6f 72 28 69 46 75 ullSz;. for(iFu
93ea0 6c 6c 53 7a 3d 6d 65 6d 35 2e 6e 41 74 6f 6d 3b llSz=mem5.nAtom;
93eb0 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c iFullSz<n; iFul
93ec0 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74 lSz *= 2);. ret
93ed0 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a urn iFullSz;.}..
93ee0 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 static int memsy
93ef0 73 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 s5Log(int iValue
93f00 29 7b 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 ){. int iLog;.
93f10 20 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c for(iLog=0; (1<
93f20 3c 69 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 <iLog)<iValue; i
93f30 4c 6f 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e Log++);. return
93f40 20 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 iLog;.}../*.**
93f50 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 Initialize this
93f60 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 module..*/.stati
93f70 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69 c int memsys5Ini
93f80 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
93f90 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e {. int ii;. in
93fa0 74 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 t nByte = sqlite
93fb0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 3GlobalConfig.nH
93fc0 65 61 70 3b 0a 20 20 75 38 20 2a 7a 42 79 74 65 eap;. u8 *zByte
93fd0 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 = (u8 *)sqlite3
93fe0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 GlobalConfig.pHe
93ff0 61 70 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f ap;. int nMinLo
94000 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
94010 20 20 20 2f 2a 20 4c 6f 67 20 6f 66 20 6d 69 6e /* Log of min
94020 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 imum allocation
94030 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2a 2f 0a size in bytes*/.
94040 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 0a int iOffset;..
94050 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
94060 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 ER(NotUsed);..
94070 69 66 28 20 21 7a 42 79 74 65 20 29 7b 0a 20 20 if( !zByte ){.
94080 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
94090 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 6e 4d ERROR;. }.. nM
940a0 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c inLog = memsys5L
940b0 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c og(sqlite3Global
940c0 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20 Config.mnReq);.
940d0 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3d 20 28 31 mem5.nAtom = (1
940e0 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68 <<nMinLog);. wh
940f0 69 6c 65 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 ile( (int)sizeof
94100 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d 35 2e (Mem5Link)>mem5.
94110 6e 41 74 6f 6d 20 29 7b 0a 20 20 20 20 6d 65 6d nAtom ){. mem
94120 35 2e 6e 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 6e 5.nAtom = mem5.n
94130 41 74 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a Atom << 1;. }..
94140 20 20 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 mem5.nBlock =
94150 28 6e 42 79 74 65 20 2f 20 28 6d 65 6d 35 2e 6e (nByte / (mem5.n
94160 41 74 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 Atom+sizeof(u8))
94170 29 3b 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 );. mem5.zPool
94180 3d 20 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e = zByte;. mem5.
94190 61 43 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d aCtrl = (u8 *)&m
941a0 65 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e em5.zPool[mem5.n
941b0 42 6c 6f 63 6b 2a 6d 65 6d 35 2e 6e 41 74 6f 6d Block*mem5.nAtom
941c0 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 ];.. for(ii=0;
941d0 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b ii<=LOGMAX; ii++
941e0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 ){. mem5.aiFr
941f0 65 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b eelist[ii] = -1;
94200 0a 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20 . }.. iOffset
94210 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f = 0;. for(ii=LO
94220 47 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d GMAX; ii>=0; ii-
94230 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c -){. int nAll
94240 6f 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20 oc = (1<<ii);.
94250 20 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e if( (iOffset+n
94260 41 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c Alloc)<=mem5.nBl
94270 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ock ){. mem
94280 35 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d 5.aCtrl[iOffset]
94290 20 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45 = ii | CTRL_FRE
942a0 45 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35 E;. memsys5
942b0 4c 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69 Link(iOffset, ii
942c0 29 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74 );. iOffset
942d0 20 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 += nAlloc;.
942e0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f }. assert((iO
942f0 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 ffset+nAlloc)>me
94300 6d 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a m5.nBlock);. }.
94310 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
94320 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 _OK;.}../*.** De
94330 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 initialize this
94340 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 module..*/.stati
94350 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 53 68 c void memsys5Sh
94360 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 utdown(void *Not
94370 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
94380 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
94390 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a d);. return;.}.
943a0 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 ./*.** Open the
943b0 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 file indicated a
943c0 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f nd write a log o
943d0 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 f all unfreed me
943e0 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 mory .** allocat
943f0 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c ions into that l
94400 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 og..*/.SQLITE_PR
94410 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
94420 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28 63 6f e3Memsys5Dump(co
94430 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
94440 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c ame){.#ifdef SQL
94450 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 ITE_DEBUG. FILE
94460 20 2a 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 *out;. int i,
94470 6a 2c 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e j, n;. int nMin
94480 4c 6f 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c Log;.. if( zFil
94490 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c ename==0 || zFil
944a0 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 ename[0]==0 ){.
944b0 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b out = stdout;
944c0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 . }else{. ou
944d0 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e t = fopen(zFilen
944e0 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 ame, "w");. i
944f0 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 f( out==0 ){.
94500 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 fprintf(stder
94510 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f r, "** Unable to
94520 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 output memory d
94530 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a ebug output log:
94540 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 %s **\n",.
94550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
94560 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 zFilename);.
94570 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
94580 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e . }. memsys5En
94590 74 65 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 ter();. nMinLog
945a0 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 = memsys5Log(me
945b0 6d 35 2e 6e 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 m5.nAtom);. for
945c0 28 69 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 (i=0; i<=LOGMAX
945d0 26 26 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b && i+nMinLog<32;
945e0 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e i++){. for(n
945f0 3d 30 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 =0, j=mem5.aiFre
94600 65 6c 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 elist[i]; j>=0;
94610 6a 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d j = MEM5LINK(j)-
94620 3e 6e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 >next, n++){}.
94630 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
94640 66 72 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f freelist items o
94650 66 20 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 f size %d: %d\n"
94660 2c 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3c 3c 20 , mem5.nAtom <<
94670 69 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 i, n);. }. fpr
94680 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e intf(out, "mem5.
94690 6e 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 nAlloc = %
946a0 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c llu\n", mem5.nAl
946b0 6c 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 loc);. fprintf(
946c0 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c out, "mem5.total
946d0 41 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e Alloc = %llu\n
946e0 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c ", mem5.totalAll
946f0 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f oc);. fprintf(o
94700 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 ut, "mem5.totalE
94710 78 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 xcess = %llu\n"
94720 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 , mem5.totalExce
94730 73 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f ss);. fprintf(o
94740 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e ut, "mem5.curren
94750 74 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 tOut = %u\n",
94760 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 mem5.currentOut)
94770 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c ;. fprintf(out,
94780 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f "mem5.currentCo
94790 75 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d unt = %u\n", mem
947a0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 5.currentCount);
947b0 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
947c0 22 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 "mem5.maxOut
947d0 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 = %u\n", mem5
947e0 2e 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 .maxOut);. fpri
947f0 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d ntf(out, "mem5.m
94800 61 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 axCount = %u
94810 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 \n", mem5.maxCou
94820 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f nt);. fprintf(o
94830 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 ut, "mem5.maxReq
94840 75 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 uest = %u\n",
94850 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 mem5.maxRequest)
94860 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 ;. memsys5Leave
94870 28 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 ();. if( out==s
94880 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c tdout ){. ffl
94890 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d ush(stdout);. }
948a0 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 else{. fclose
948b0 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 (out);. }.#else
948c0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
948d0 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a TER(zFilename);.
948e0 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
948f0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
94900 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 the only routine
94910 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 in this file wi
94920 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 th external .**
94930 6c 69 6e 6b 61 67 65 2e 20 49 74 20 72 65 74 75 linkage. It retu
94940 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
94950 20 61 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 a static sqlite
94960 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a 2a 2a 3_mem_methods.**
94970 20 73 74 72 75 63 74 20 70 6f 70 75 6c 61 74 65 struct populate
94980 64 20 77 69 74 68 20 74 68 65 20 6d 65 6d 73 79 d with the memsy
94990 73 35 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 s5 methods..*/.S
949a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
949b0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
949c0 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
949d0 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f MemGetMemsys5(vo
949e0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f id){. static co
949f0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
94a00 6d 65 74 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d methods memsys5M
94a10 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 ethods = {.
94a20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0a 20 memsys5Malloc,.
94a30 20 20 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c memsys5Free,
94a40 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61 . memsys5Rea
94a50 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 lloc,. memsy
94a60 73 35 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d s5Size,. mem
94a70 73 79 73 35 52 6f 75 6e 64 75 70 2c 0a 20 20 20 sys5Roundup,.
94a80 20 20 6d 65 6d 73 79 73 35 49 6e 69 74 2c 0a 20 memsys5Init,.
94a90 20 20 20 20 6d 65 6d 73 79 73 35 53 68 75 74 64 memsys5Shutd
94aa0 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b own,. 0. };
94ab0 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 73 79 . return &memsy
94ac0 73 35 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 s5Methods;.}..#e
94ad0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 ndif /* SQLITE_E
94ae0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 2a 2f NABLE_MEMSYS5 */
94af0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
94b00 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 35 2e 63 20 * End of mem5.c
94b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94b40 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
94b50 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 * Begin file mut
94b60 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ex.c ***********
94b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94b90 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 /./*.** 2007 Aug
94ba0 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 ust 14.**.** The
94bb0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
94bc0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
94bd0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
94be0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
94bf0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
94c00 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
94c10 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
94c20 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
94c30 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
94c40 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
94c50 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
94c60 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
94c70 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
94c80 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
94c90 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
94ca0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
94cb0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
94cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94d00 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
94d10 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
94d20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
94d30 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 implement mutex
94d40 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 es..**.** This f
94d50 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
94d60 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e e that is common
94d70 20 61 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65 across all mute
94d80 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
94d90 73 2e 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d s...**.** $Id: m
94da0 75 74 65 78 2e 63 2c 76 20 31 2e 32 39 20 32 30 utex.c,v 1.29 20
94db0 30 38 2f 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 08/10/07 15:25:4
94dc0 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 8 drh Exp $.*/..
94dd0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
94de0 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 UTEX_OMIT./*.**
94df0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d Initialize the m
94e00 75 74 65 78 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a utex system..*/.
94e10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
94e20 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 nt sqlite3MutexI
94e30 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 69 6e nit(void){ . in
94e40 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
94e50 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 ;. if( sqlite3G
94e60 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 lobalConfig.bCor
94e70 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 69 66 eMutex ){. if
94e80 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ( !sqlite3Global
94e90 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
94ea0 74 65 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 texAlloc ){.
94eb0 20 20 2f 2a 20 49 66 20 74 68 65 20 78 4d 75 74 /* If the xMut
94ec0 65 78 41 6c 6c 6f 63 20 6d 65 74 68 6f 64 20 68 exAlloc method h
94ed0 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 2c as not been set,
94ee0 20 74 68 65 6e 20 74 68 65 20 75 73 65 72 20 64 then the user d
94ef0 69 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 id not. **
94f00 69 6e 73 74 61 6c 6c 20 61 20 6d 75 74 65 78 20 install a mutex
94f10 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 76 implementation v
94f20 69 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 ia sqlite3_confi
94f30 67 28 29 20 70 72 69 6f 72 20 74 6f 20 0a 20 20 g() prior to .
94f40 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69 ** sqlite3_i
94f50 6e 69 74 69 61 6c 69 7a 65 28 29 20 62 65 69 6e nitialize() bein
94f60 67 20 63 61 6c 6c 65 64 2e 20 54 68 69 73 20 62 g called. This b
94f70 6c 6f 63 6b 20 63 6f 70 69 65 73 20 70 6f 69 6e lock copies poin
94f80 74 65 72 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a ters to. **
94f90 20 74 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 the default imp
94fa0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f lementation into
94fb0 20 74 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 the sqlite3Glob
94fc0 61 6c 43 6f 6e 66 69 67 20 73 74 72 75 63 74 75 alConfig structu
94fd0 72 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 re.. **.
94fe0 20 20 20 2a 2a 20 54 68 65 20 64 61 6e 67 65 72 ** The danger
94ff0 20 69 73 20 74 68 61 74 20 61 6c 74 68 6f 75 67 is that althoug
95000 68 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 h sqlite3_config
95010 28 29 20 69 73 20 6e 6f 74 20 61 20 74 68 72 65 () is not a thre
95020 61 64 73 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 adsafe. **
95030 41 50 49 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 API, sqlite3_ini
95040 74 69 61 6c 69 7a 65 28 29 20 69 73 2c 20 61 6e tialize() is, an
95050 64 20 73 6f 20 6d 75 6c 74 69 70 6c 65 20 74 68 d so multiple th
95060 72 65 61 64 73 20 6d 61 79 20 62 65 0a 20 20 20 reads may be.
95070 20 20 20 2a 2a 20 61 74 74 65 6d 70 74 69 6e 67 ** attempting
95080 20 74 6f 20 72 75 6e 20 74 68 69 73 20 66 75 6e to run this fun
95090 63 74 69 6f 6e 20 73 69 6d 75 6c 74 61 6e 65 6f ction simultaneo
950a0 75 73 6c 79 2e 20 54 6f 20 67 75 61 72 64 20 77 usly. To guard w
950b0 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 61 63 rite. ** ac
950c0 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69 cess to the sqli
950d0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 te3GlobalConfig
950e0 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 20 27 structure, the '
950f0 4d 41 53 54 45 52 27 20 73 74 61 74 69 63 20 6d MASTER' static m
95100 75 74 65 78 0a 20 20 20 20 20 20 2a 2a 20 69 73 utex. ** is
95110 20 6f 62 74 61 69 6e 65 64 20 62 65 66 6f 72 65 obtained before
95120 20 6d 6f 64 69 66 79 69 6e 67 20 69 74 2e 0a 20 modifying it..
95130 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 */. sq
95140 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
95150 6f 64 73 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 ods *p = sqlite3
95160 44 65 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a DefaultMutex();.
95170 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
95180 74 65 78 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 tex *pMaster = 0
95190 3b 0a 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20 ;. . rc =
951a0 70 2d 3e 78 4d 75 74 65 78 49 6e 69 74 28 29 3b p->xMutexInit();
951b0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
951c0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
951d0 20 20 20 20 70 4d 61 73 74 65 72 20 3d 20 70 2d pMaster = p-
951e0 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c >xMutexAlloc(SQL
951f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
95200 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20 _MASTER);.
95210 20 20 61 73 73 65 72 74 28 70 4d 61 73 74 65 72 assert(pMaster
95220 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 78 4d );. p->xM
95230 75 74 65 78 45 6e 74 65 72 28 70 4d 61 73 74 65 utexEnter(pMaste
95240 72 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 r);. asse
95250 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 rt( sqlite3Globa
95260 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d lConfig.mutex.xM
95270 75 74 65 78 41 6c 6c 6f 63 3d 3d 30 20 0a 20 20 utexAlloc==0 .
95280 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 || sq
95290 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
952a0 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c g.mutex.xMutexAl
952b0 6c 6f 63 3d 3d 70 2d 3e 78 4d 75 74 65 78 41 6c loc==p->xMutexAl
952c0 6c 6f 63 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 loc. );.
952d0 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 if( !sqli
952e0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
952f0 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f mutex.xMutexAllo
95300 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 c ){. s
95310 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
95320 69 67 2e 6d 75 74 65 78 20 3d 20 2a 70 3b 0a 20 ig.mutex = *p;.
95330 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
95340 20 70 2d 3e 78 4d 75 74 65 78 4c 65 61 76 65 28 p->xMutexLeave(
95350 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 pMaster);.
95360 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
95370 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 rc = sqlite3G
95380 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
95390 78 2e 78 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a x.xMutexInit();.
953a0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 }. }.. ret
953b0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
953c0 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 Shutdown the mu
953d0 74 65 78 20 73 79 73 74 65 6d 2e 20 54 68 69 73 tex system. This
953e0 20 63 61 6c 6c 20 66 72 65 65 73 20 72 65 73 6f call frees reso
953f0 75 72 63 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 urces allocated
95400 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 by.** sqlite3Mut
95410 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c exInit()..*/.SQL
95420 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
95430 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 sqlite3MutexEnd(
95440 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 void){. int rc
95450 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 = SQLITE_OK;. r
95460 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 c = sqlite3Globa
95470 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d lConfig.mutex.xM
95480 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 72 65 74 utexEnd();. ret
95490 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
954a0 20 52 65 74 72 69 65 76 65 20 61 20 70 6f 69 6e Retrieve a poin
954b0 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 ter to a static
954c0 6d 75 74 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 mutex or allocat
954d0 65 20 61 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 e a new dynamic
954e0 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 one..*/.SQLITE_A
954f0 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 PI sqlite3_mutex
95500 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f *sqlite3_mutex_
95510 61 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23 alloc(int id){.#
95520 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
95530 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 IT_AUTOINIT. if
95540 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 ( sqlite3_initia
95550 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 lize() ) return
95560 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 0;.#endif. retu
95570 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c rn sqlite3Global
95580 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
95590 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a texAlloc(id);.}.
955a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
955b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 sqlite3_mutex *s
955c0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
955d0 28 69 6e 74 20 69 64 29 7b 0a 20 20 69 66 28 20 (int id){. if(
955e0 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f !sqlite3GlobalCo
955f0 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 nfig.bCoreMutex
95600 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
95610 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 . }. return sq
95620 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
95630 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c g.mutex.xMutexAl
95640 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a loc(id);.}../*.*
95650 2a 20 46 72 65 65 20 61 20 64 79 6e 61 6d 69 63 * Free a dynamic
95660 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 mutex..*/.SQLIT
95670 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
95680 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 e3_mutex_free(sq
95690 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
956a0 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
956b0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
956c0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
956d0 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a Free(p);. }.}..
956e0 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 /*.** Obtain the
956f0 20 6d 75 74 65 78 20 70 2e 20 49 66 20 73 6f 6d mutex p. If som
95700 65 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 61 e other thread a
95710 6c 72 65 61 64 79 20 68 61 73 20 74 68 65 20 6d lready has the m
95720 75 74 65 78 2c 20 62 6c 6f 63 6b 0a 2a 2a 20 75 utex, block.** u
95730 6e 74 69 6c 20 69 74 20 63 61 6e 20 62 65 20 6f ntil it can be o
95740 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 btained..*/.SQLI
95750 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
95760 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
95770 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
95780 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 ){. if( p ){.
95790 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
957a0 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 onfig.mutex.xMut
957b0 65 78 45 6e 74 65 72 28 70 29 3b 0a 20 20 7d 0a exEnter(p);. }.
957c0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 }../*.** Obtain
957d0 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 66 20 the mutex p. If
957e0 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 successful, retu
957f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 rn SQLITE_OK. Ot
95800 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 6f 74 herwise, if anot
95810 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 20 68 6f her.** thread ho
95820 6c 64 73 20 74 68 65 20 6d 75 74 65 78 20 61 6e lds the mutex an
95830 64 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 6f d it cannot be o
95840 62 74 61 69 6e 65 64 2c 20 72 65 74 75 72 6e 20 btained, return
95850 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a 2f 0a SQLITE_BUSY..*/.
95860 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
95870 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
95880 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
95890 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 p){. int rc = S
958a0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
958b0 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 p ){. return
958c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
958d0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
958e0 54 72 79 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 Try(p);. }. re
958f0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
95900 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
95910 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 tex_leave() rout
95920 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 ine exits a mute
95930 78 20 74 68 61 74 20 77 61 73 20 70 72 65 76 69 x that was previ
95940 6f 75 73 6c 79 0a 2a 2a 20 65 6e 74 65 72 65 64 ously.** entered
95950 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 by the same thr
95960 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 ead. The behavi
95970 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 or is undefined
95980 69 66 20 74 68 65 20 6d 75 74 65 78 20 0a 2a 2a if the mutex .**
95990 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
959a0 79 20 65 6e 74 65 72 65 64 2e 20 49 66 20 61 20 y entered. If a
959b0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 NULL pointer is
959c0 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 passed as an arg
959d0 75 6d 65 6e 74 0a 2a 2a 20 74 68 69 73 20 66 75 ument.** this fu
959e0 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
959f0 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 p..*/.SQLITE_API
95a00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 void sqlite3_mu
95a10 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 tex_leave(sqlite
95a20 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
95a30 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 f( p ){. sqli
95a40 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
95a50 6d 75 74 65 78 2e 78 4d 75 74 65 78 4c 65 61 76 mutex.xMutexLeav
95a60 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 e(p);. }.}..#if
95a70 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a ndef NDEBUG./*.*
95a80 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
95a90 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 tex_held() and s
95aa0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
95ab0 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 held() routine a
95ac0 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 re.** intended f
95ad0 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 or use inside as
95ae0 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
95af0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
95b00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 int sqlite3_mut
95b10 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 5f ex_held(sqlite3_
95b20 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 mutex *p){. ret
95b30 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 urn p==0 || sqli
95b40 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
95b50 6d 75 74 65 78 2e 78 4d 75 74 65 78 48 65 6c 64 mutex.xMutexHeld
95b60 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 (p);.}.SQLITE_AP
95b70 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 I int sqlite3_mu
95b80 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 tex_notheld(sqli
95b90 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
95ba0 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 return p==0 ||
95bb0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
95bc0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
95bd0 4e 6f 74 68 65 6c 64 28 70 29 3b 0a 7d 0a 23 65 Notheld(p);.}.#e
95be0 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 ndif..#endif /*
95bf0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 55 54 45 SQLITE_OMIT_MUTE
95c00 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a X */../*********
95c10 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 ***** End of mut
95c20 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ex.c ***********
95c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95c50 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
95c60 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
95c70 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a 2a mutex_noop.c **
95c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95ca0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
95cb0 20 4f 63 74 6f 62 65 72 20 30 37 0a 2a 2a 0a 2a October 07.**.*
95cc0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
95cd0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
95ce0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
95cf0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
95d00 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
95d10 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
95d20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
95d30 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
95d40 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
95d50 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
95d60 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
95d70 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
95d80 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
95d90 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
95da0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
95db0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
95dc0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
95dd0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
95de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
95e20 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
95e30 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 the C functions
95e40 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
95e50 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 mutexes..**.** T
95e60 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
95e70 6f 6e 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 on in this file
95e80 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 does not provide
95e90 20 61 6e 79 20 6d 75 74 75 61 6c 0a 2a 2a 20 65 any mutual.** e
95ea0 78 63 6c 75 73 69 6f 6e 20 61 6e 64 20 69 73 20 xclusion and is
95eb0 74 68 75 73 20 73 75 69 74 61 62 6c 65 20 66 6f thus suitable fo
95ec0 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 20 61 70 r use only in ap
95ed0 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 plications.** th
95ee0 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 69 6e at use SQLite in
95ef0 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 a single thread
95f00 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 . The routines
95f10 64 65 66 69 6e 65 64 0a 2a 2a 20 68 65 72 65 20 defined.** here
95f20 61 72 65 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 are place-holder
95f30 73 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 s. Applications
95f40 20 63 61 6e 20 73 75 62 73 74 69 74 75 74 65 20 can substitute
95f50 77 6f 72 6b 69 6e 67 0a 2a 2a 20 6d 75 74 65 78 working.** mutex
95f60 20 72 6f 75 74 69 6e 65 73 20 61 74 20 73 74 61 routines at sta
95f70 72 74 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68 rt-time using th
95f80 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 e.**.** sqli
95f90 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 te3_config(SQLIT
95fa0 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 2c 2e E_CONFIG_MUTEX,.
95fb0 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 6e 74 65 72 66 ..).**.** interf
95fc0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 6f ace..**.** If co
95fd0 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 mpiled with SQLI
95fe0 54 45 5f 44 45 42 55 47 2c 20 74 68 65 6e 20 61 TE_DEBUG, then a
95ff0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67 69 63 20 dditional logic
96000 69 73 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 74 is inserted.** t
96010 68 61 74 20 64 6f 65 73 20 65 72 72 6f 72 20 63 hat does error c
96020 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75 74 65 78 hecking on mutex
96030 65 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 es to make sure
96040 74 68 65 79 20 61 72 65 20 62 65 69 6e 67 0a 2a they are being.*
96050 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72 65 63 74 * called correct
96060 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d ly..**.** $Id: m
96070 75 74 65 78 5f 6e 6f 6f 70 2e 63 2c 76 20 31 2e utex_noop.c,v 1.
96080 33 20 32 30 30 38 2f 31 32 2f 30 35 20 31 37 3a 3 2008/12/05 17:
96090 31 37 3a 30 38 20 64 72 68 20 45 78 70 20 24 0a 17:08 drh Exp $.
960a0 2a 2f 0a 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 */...#if defined
960b0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f (SQLITE_MUTEX_NO
960c0 4f 50 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 OP) && !defined(
960d0 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a SQLITE_DEBUG)./*
960e0 0a 2a 2a 20 53 74 75 62 20 72 6f 75 74 69 6e 65 .** Stub routine
960f0 73 20 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 s for all mutex
96100 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 methods..**.** T
96110 68 69 73 20 72 6f 75 74 69 6e 65 73 20 70 72 6f his routines pro
96120 76 69 64 65 20 6e 6f 20 6d 75 74 75 61 6c 20 65 vide no mutual e
96130 78 63 6c 75 73 69 6f 6e 20 6f 72 20 65 72 72 6f xclusion or erro
96140 72 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 r checking..*/.s
96150 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 tatic int noopMu
96160 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f texHeld(sqlite3_
96170 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 mutex *p){ retur
96180 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e n 1; }.static in
96190 74 20 6e 6f 6f 70 4d 75 74 65 78 4e 6f 74 68 65 t noopMutexNothe
961a0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
961b0 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 *p){ return 1;
961c0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f }.static int noo
961d0 70 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 pMutexInit(void)
961e0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
961f0 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 OK; }.static int
96200 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 28 76 6f noopMutexEnd(vo
96210 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 id){ return SQLI
96220 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 TE_OK; }.static
96230 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6e sqlite3_mutex *n
96240 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e oopMutexAlloc(in
96250 74 20 69 64 29 7b 20 72 65 74 75 72 6e 20 28 73 t id){ return (s
96260 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 3b qlite3_mutex*)8;
96270 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e }.static void n
96280 6f 6f 70 4d 75 74 65 78 46 72 65 65 28 73 71 6c oopMutexFree(sql
96290 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 ite3_mutex *p){
962a0 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 return; }.static
962b0 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 45 void noopMutexE
962c0 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 nter(sqlite3_mut
962d0 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 ex *p){ return;
962e0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f }.static int noo
962f0 70 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 pMutexTry(sqlite
96300 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 3_mutex *p){ ret
96310 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
96320 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f .static void noo
96330 70 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 pMutexLeave(sqli
96340 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 te3_mutex *p){ r
96350 65 74 75 72 6e 3b 20 7d 0a 0a 53 51 4c 49 54 45 eturn; }..SQLITE
96360 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
96370 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a _mutex_methods *
96380 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 sqlite3DefaultMu
96390 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 tex(void){. sta
963a0 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 tic sqlite3_mute
963b0 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 x_methods sMutex
963c0 20 3d 20 7b 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 = {. noopMut
963d0 65 78 49 6e 69 74 2c 0a 20 20 20 20 6e 6f 6f 70 exInit,. noop
963e0 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 6e 6f MutexEnd,. no
963f0 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 opMutexAlloc,.
96400 20 20 6e 6f 6f 70 4d 75 74 65 78 46 72 65 65 2c noopMutexFree,
96410 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e . noopMutexEn
96420 74 65 72 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 ter,. noopMut
96430 65 78 54 72 79 2c 0a 20 20 20 20 6e 6f 6f 70 4d exTry,. noopM
96440 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 utexLeave,..
96450 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 2c 0a 20 noopMutexHeld,.
96460 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4e 6f 74 68 noopMutexNoth
96470 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 eld. };.. retu
96480 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 rn &sMutex;.}.#e
96490 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 ndif /* defined(
964a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f SQLITE_MUTEX_NOO
964b0 50 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 P) && !defined(S
964c0 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a QLITE_DEBUG) */.
964d0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
964e0 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 ITE_MUTEX_NOOP)
964f0 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 && defined(SQLIT
96500 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 49 E_DEBUG)./*.** I
96510 6e 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 n this implement
96520 61 74 69 6f 6e 2c 20 65 72 72 6f 72 20 63 68 65 ation, error che
96530 63 6b 69 6e 67 20 69 73 20 70 72 6f 76 69 64 65 cking is provide
96540 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a d for testing.**
96550 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 70 and debugging p
96560 75 72 70 6f 73 65 73 2e 20 20 54 68 65 20 6d 75 urposes. The mu
96570 74 65 78 65 73 20 73 74 69 6c 6c 20 64 6f 20 6e texes still do n
96580 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79 0a 2a ot provide any.*
96590 2a 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 * mutual exclusi
965a0 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 on..*/../*.** Th
965b0 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a e mutex object.*
965c0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
965d0 5f 6d 75 74 65 78 20 7b 0a 20 20 69 6e 74 20 69 _mutex {. int i
965e0 64 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 75 d; /* The mu
965f0 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e tex type */. in
96600 74 20 63 6e 74 3b 20 20 20 20 2f 2a 20 4e 75 6d t cnt; /* Num
96610 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 ber of entries w
96620 69 74 68 6f 75 74 20 61 20 6d 61 74 63 68 69 6e ithout a matchin
96630 67 20 6c 65 61 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f g leave */.};../
96640 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
96650 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e _mutex_held() an
96660 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
96670 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e notheld() routin
96680 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 e are.** intende
96690 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
966a0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
966b0 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ents..*/.static
966c0 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 48 65 int debugMutexHe
966d0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
966e0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 *p){. return p
966f0 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 3e 30 3b ==0 || p->cnt>0;
96700 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 .}.static int de
96710 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 bugMutexNotheld(
96720 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
96730 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 ){. return p==0
96740 20 7c 7c 20 70 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d || p->cnt==0;.}
96750 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
96760 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c ze and deinitial
96770 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75 ize the mutex su
96780 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 bsystem..*/.stat
96790 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 ic int debugMute
967a0 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 xInit(void){ ret
967b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
967c0 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 .static int debu
967d0 67 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b gMutexEnd(void){
967e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
967f0 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K; }../*.** The
96800 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
96810 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c loc() routine al
96820 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a locates a new.**
96830 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 mutex and retur
96840 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
96850 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 it. If it retur
96860 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 ns NULL.** that
96870 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 means that a mut
96880 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ex could not be
96890 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a 2f 0a 73 allocated. .*/.s
968a0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 tatic sqlite3_mu
968b0 74 65 78 20 2a 64 65 62 75 67 4d 75 74 65 78 41 tex *debugMutexA
968c0 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20 20 lloc(int id){.
968d0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
968e0 75 74 65 78 20 61 53 74 61 74 69 63 5b 36 5d 3b utex aStatic[6];
968f0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
96900 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 73 77 *pNew = 0;. sw
96910 69 74 63 68 28 20 69 64 20 29 7b 0a 20 20 20 20 itch( id ){.
96920 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 case SQLITE_MUTE
96930 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65 X_FAST:. case
96940 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
96950 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 CURSIVE: {.
96960 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d pNew = sqlite3M
96970 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 4e alloc(sizeof(*pN
96980 65 77 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ew));. if(
96990 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 pNew ){.
969a0 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 3b 0a 20 pNew->id = id;.
969b0 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 63 6e 74 pNew->cnt
969c0 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
969d0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
969e0 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
969f0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 64 assert( id
96a00 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 -2 >= 0 );.
96a10 20 61 73 73 65 72 74 28 20 69 64 2d 32 20 3c 20 assert( id-2 <
96a20 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 61 53 74 (int)(sizeof(aSt
96a30 61 74 69 63 29 2f 73 69 7a 65 6f 66 28 61 53 74 atic)/sizeof(aSt
96a40 61 74 69 63 5b 30 5d 29 29 20 29 3b 0a 20 20 20 atic[0])) );.
96a50 20 20 20 70 4e 65 77 20 3d 20 26 61 53 74 61 74 pNew = &aStat
96a60 69 63 5b 69 64 2d 32 5d 3b 0a 20 20 20 20 20 20 ic[id-2];.
96a70 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 3b 0a 20 pNew->id = id;.
96a80 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
96a90 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 }. }. return p
96aa0 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 New;.}../*.** Th
96ab0 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c is routine deall
96ac0 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 ocates a previou
96ad0 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 sly allocated mu
96ae0 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tex..*/.static v
96af0 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78 46 72 oid debugMutexFr
96b00 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ee(sqlite3_mutex
96b10 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
96b20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 p->cnt==0 );. a
96b30 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
96b40 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
96b50 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 || p->id==SQLITE
96b60 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
96b70 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 );. sqlite3_fr
96b80 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ee(p);.}../*.**
96b90 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
96ba0 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 x_enter() and sq
96bb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
96bc0 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d ) routines attem
96bd0 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 pt.** to enter a
96be0 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 mutex. If anot
96bf0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c her thread is al
96c00 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 ready within the
96c10 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 mutex,.** sqlit
96c20 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
96c30 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 will block and
96c40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
96c50 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a y() will return.
96c60 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 ** SQLITE_BUSY.
96c70 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
96c80 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 ex_try() interfa
96c90 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ce returns SQLIT
96ca0 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 E_OK.** upon suc
96cb0 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 cessful entry.
96cc0 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 Mutexes created
96cd0 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 using SQLITE_MUT
96ce0 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e EX_RECURSIVE can
96cf0 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d .** be entered m
96d00 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 ultiple times by
96d10 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
96d20 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 . In such cases
96d30 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d the,.** mutex m
96d40 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e ust be exited an
96d50 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 equal number of
96d60 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e times before an
96d70 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 other thread.**
96d80 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 can enter. If t
96d90 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 he same thread t
96da0 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e ries to enter an
96db0 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 y other kind of
96dc0 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 mutex.** more th
96dd0 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 an once, the beh
96de0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
96df0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
96e00 69 64 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 id debugMutexEnt
96e10 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
96e20 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
96e30 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
96e40 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
96e50 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 debugMutexNothe
96e60 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e ld(p) );. p->cn
96e70 74 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e t++;.}.static in
96e80 74 20 64 65 62 75 67 4d 75 74 65 78 54 72 79 28 t debugMutexTry(
96e90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
96ea0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e ){. assert( p->
96eb0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
96ec0 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 64 65 _RECURSIVE || de
96ed0 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 bugMutexNotheld(
96ee0 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b p) );. p->cnt++
96ef0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
96f00 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
96f10 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
96f20 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 _leave() routine
96f30 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 exits a mutex t
96f40 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 hat was.** previ
96f50 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 ously entered by
96f60 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
96f70 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a . The behavior.
96f80 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 ** is undefined
96f90 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 if the mutex is
96fa0 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e not currently en
96fb0 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e tered or.** is n
96fc0 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c ot currently all
96fd0 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 ocated. SQLite
96fe0 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 will never do ei
96ff0 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ther..*/.static
97000 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78 4c void debugMutexL
97010 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
97020 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ex *p){. assert
97030 28 20 64 65 62 75 67 4d 75 74 65 78 48 65 6c 64 ( debugMutexHeld
97040 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2d (p) );. p->cnt-
97050 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e -;. assert( p->
97060 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
97070 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 64 65 _RECURSIVE || de
97080 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 bugMutexNotheld(
97090 70 29 20 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f p) );.}..SQLITE_
970a0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
970b0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 mutex_methods *s
970c0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 qlite3DefaultMut
970d0 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ex(void){. stat
970e0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
970f0 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 _methods sMutex
97100 3d 20 7b 0a 20 20 20 20 64 65 62 75 67 4d 75 74 = {. debugMut
97110 65 78 49 6e 69 74 2c 0a 20 20 20 20 64 65 62 75 exInit,. debu
97120 67 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 64 gMutexEnd,. d
97130 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a ebugMutexAlloc,.
97140 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 46 72 debugMutexFr
97150 65 65 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 ee,. debugMut
97160 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 64 65 62 exEnter,. deb
97170 75 67 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 ugMutexTry,.
97180 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76 65 2c debugMutexLeave,
97190 0a 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 .. debugMutex
971a0 48 65 6c 64 2c 0a 20 20 20 20 64 65 62 75 67 4d Held,. debugM
971b0 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b utexNotheld. };
971c0 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 .. return &sMut
971d0 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ex;.}.#endif /*
971e0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
971f0 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 UTEX_NOOP) && de
97200 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
97210 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a UG) */../*******
97220 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d ******* End of m
97230 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a utex_noop.c ****
97240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97260 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
97270 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
97280 6c 65 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a le mutex_os2.c *
97290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
972a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
972b0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
972c0 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 07 August 28.**.
972d0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
972e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
972f0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
97300 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
97310 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
97320 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
97330 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
97340 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
97350 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
97360 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
97370 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
97380 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
97390 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
973a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
973b0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
973c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
973d0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
973e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
973f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
97430 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
97440 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e s the C function
97450 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
97460 20 6d 75 74 65 78 65 73 20 66 6f 72 20 4f 53 2f mutexes for OS/
97470 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 2.**.** $Id: mut
97480 65 78 5f 6f 73 32 2e 63 2c 76 20 31 2e 31 31 20 ex_os2.c,v 1.11
97490 32 30 30 38 2f 31 31 2f 32 32 20 31 39 3a 35 30 2008/11/22 19:50
974a0 3a 35 34 20 70 77 65 69 6c 62 61 63 68 65 72 20 :54 pweilbacher
974b0 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
974c0 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 The code in this
974d0 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 file is only us
974e0 65 64 20 69 66 20 53 51 4c 49 54 45 5f 4d 55 54 ed if SQLITE_MUT
974f0 45 58 5f 4f 53 32 20 69 73 20 64 65 66 69 6e 65 EX_OS2 is define
97500 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 6d 75 d..** See the mu
97510 74 65 78 2e 68 20 66 69 6c 65 20 66 6f 72 20 64 tex.h file for d
97520 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 66 64 65 etails..*/.#ifde
97530 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f f SQLITE_MUTEX_O
97540 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a S2../***********
97550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f 53 2f 32 *********** OS/2
97560 20 4d 75 74 65 78 20 49 6d 70 6c 65 6d 65 6e 74 Mutex Implement
97570 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ation **********
97580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
97590 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e ** This implemen
975a0 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74 65 78 65 tation of mutexe
975b0 73 20 69 73 20 62 75 69 6c 74 20 75 73 69 6e 67 s is built using
975c0 20 74 68 65 20 4f 53 2f 32 20 41 50 49 2e 0a 2a the OS/2 API..*
975d0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 /../*.** The mut
975e0 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20 45 61 63 ex object.** Eac
975f0 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 h recursive mute
97600 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 x is an instance
97610 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
97620 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a g structure..*/.
97630 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d struct sqlite3_m
97640 75 74 65 78 20 7b 0a 20 20 48 4d 54 58 20 6d 75 utex {. HMTX mu
97650 74 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4d 75 tex; /* Mu
97660 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 tex controlling
97670 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e the lock */. in
97680 74 20 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 t id;
97690 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f /* Mutex type */
976a0 0a 20 20 69 6e 74 20 20 6e 52 65 66 3b 20 20 20 . int nRef;
976b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
976c0 66 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a f references */.
976d0 20 20 54 49 44 20 20 6f 77 6e 65 72 3b 20 20 20 TID owner;
976e0 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 68 6f /* Thread ho
976f0 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 65 78 lding this mutex
97700 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 */.};..#define
97710 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 OS2_MUTEX_INITIA
97720 4c 49 5a 45 52 20 20 20 30 2c 30 2c 30 2c 30 0a LIZER 0,0,0,0.
97730 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
97740 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 e and deinitiali
97750 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 ze the mutex sub
97760 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 system..*/.stati
97770 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 49 6e c int os2MutexIn
97780 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e it(void){ return
97790 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 SQLITE_OK; }.st
977a0 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 atic int os2Mute
977b0 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 xEnd(void){ retu
977c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
977d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
977e0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
977f0 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 routine allocat
97800 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 es a new.** mute
97810 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 x and returns a
97820 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 pointer to it.
97830 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 If it returns NU
97840 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 LL.** that means
97850 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f that a mutex co
97860 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 uld not be alloc
97870 61 74 65 64 2e 20 0a 2a 2a 20 53 51 4c 69 74 65 ated. .** SQLite
97880 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 will unwind its
97890 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 stack and retur
978a0 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 n an error. The
978b0 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 argument.** to
978c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
978d0 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 loc() is one of
978e0 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f these integer co
978f0 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c nstants:.**.** <
97900 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c ul>.** <li> SQL
97910 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 ITE_MUTEX_FAST
97920 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 2a 0.*
97930 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
97940 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 20 UTEX_RECURSIVE
97950 20 20 20 20 20 20 20 20 31 0a 2a 2a 20 3c 6c 69 1.** <li
97960 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
97970 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 20 20 STATIC_MASTER
97980 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 2.** <li> SQ
97990 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
979a0 43 5f 4d 45 4d 20 20 20 20 20 20 20 20 20 33 0a C_MEM 3.
979b0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
979c0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e MUTEX_STATIC_PRN
979d0 47 20 20 20 20 20 20 20 20 34 0a 2a 2a 20 3c 2f G 4.** </
979e0 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ul>.**.** The fi
979f0 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 rst two constant
97a00 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f s cause sqlite3_
97a10 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f mutex_alloc() to
97a20 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 create.** a new
97a30 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 mutex. The new
97a40 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 mutex is recurs
97a50 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f ive when SQLITE_
97a60 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a MUTEX_RECURSIVE.
97a70 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e ** is used but n
97a80 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 ot necessarily s
97a90 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 o when SQLITE_MU
97aa0 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 TEX_FAST is used
97ab0 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 ..** The mutex i
97ac0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f mplementation do
97ad0 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d es not need to m
97ae0 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f ake a distinctio
97af0 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c n.** between SQL
97b00 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
97b10 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d IVE and SQLITE_M
97b20 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 UTEX_FAST if it
97b30 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 does.** not want
97b40 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 to. But SQLite
97b50 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 will only reque
97b60 73 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d st a recursive m
97b70 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 utex in.** cases
97b80 20 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 where it really
97b90 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 needs one. If
97ba0 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 a faster non-rec
97bb0 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 ursive mutex.**
97bc0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 implementation i
97bd0 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 s available on t
97be0 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d he host platform
97bf0 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 , the mutex subs
97c00 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 ystem.** might r
97c10 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 eturn such a mut
97c20 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 ex in response t
97c30 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 o SQLITE_MUTEX_F
97c40 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f AST..**.** The o
97c50 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 ther allowed par
97c60 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 ameters to sqlit
97c70 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
97c80 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 each return.**
97c90 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 a pointer to a s
97ca0 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e tatic preexistin
97cb0 67 20 6d 75 74 65 78 2e 20 20 54 68 72 65 65 20 g mutex. Three
97cc0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 static mutexes a
97cd0 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 re.** used by th
97ce0 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f e current versio
97cf0 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 n of SQLite. Fu
97d00 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 ture versions of
97d10 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 SQLite.** may a
97d20 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 dd additional st
97d30 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 atic mutexes. S
97d40 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 tatic mutexes ar
97d50 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a e for internal.*
97d60 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 * use by SQLite
97d70 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 only. Applicati
97d80 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 4c ons that use SQL
97d90 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 ite mutexes shou
97da0 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 ld.** use only t
97db0 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 he dynamic mutex
97dc0 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 es returned by S
97dd0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
97de0 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 or.** SQLITE_MU
97df0 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a TEX_RECURSIVE..*
97e00 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 *.** Note that i
97e10 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e f one of the dyn
97e20 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d amic mutex param
97e30 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 eters (SQLITE_MU
97e40 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 TEX_FAST.** or S
97e50 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
97e60 52 53 49 56 45 29 20 69 73 20 75 73 65 64 20 74 RSIVE) is used t
97e70 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 hen sqlite3_mute
97e80 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 x_alloc().** ret
97e90 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 urns a different
97ea0 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 mutex on every
97eb0 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 call. But for t
97ec0 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 he static.** mut
97ed0 65 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 ex types, the sa
97ee0 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 me mutex is retu
97ef0 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 rned on every ca
97f00 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 ll that has.** t
97f10 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d he same type num
97f20 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ber..*/.static s
97f30 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6f 73 qlite3_mutex *os
97f40 32 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 2MutexAlloc(int
97f50 69 54 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 iType){. sqlite
97f60 33 5f 6d 75 74 65 78 20 2a 70 20 3d 20 4e 55 4c 3_mutex *p = NUL
97f70 4c 3b 0a 20 20 73 77 69 74 63 68 28 20 69 54 79 L;. switch( iTy
97f80 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 pe ){. case S
97f90 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
97fa0 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 :. case SQLIT
97fb0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
97fc0 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 E: {. p = s
97fd0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
97fe0 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a ( sizeof(*p) );.
97ff0 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 if( p ){.
98000 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 p->id = i
98010 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 69 66 Type;. if
98020 28 20 44 6f 73 43 72 65 61 74 65 4d 75 74 65 78 ( DosCreateMutex
98030 53 65 6d 28 20 30 2c 20 26 70 2d 3e 6d 75 74 65 Sem( 0, &p->mute
98040 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 20 21 3d x, 0, FALSE ) !=
98050 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
98060 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
98070 72 65 65 28 20 70 20 29 3b 0a 20 20 20 20 20 20 ree( p );.
98080 20 20 20 20 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 p = NULL;.
98090 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
980a0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
980b0 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
980c0 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 76 {. static v
980d0 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 49 6e olatile int isIn
980e0 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 74 it = 0;. st
980f0 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 atic sqlite3_mut
98100 65 78 20 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex staticMutexes
98110 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b [] = {. {
98120 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 OS2_MUTEX_INITI
98130 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 ALIZER, },.
98140 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 { OS2_MUTEX_I
98150 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 NITIALIZER, },.
98160 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 { OS2_MUT
98170 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 EX_INITIALIZER,
98180 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 },. { OS2
98190 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
981a0 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b ER, },. {
981b0 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 OS2_MUTEX_INITI
981c0 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 ALIZER, },.
981d0 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 { OS2_MUTEX_I
981e0 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 NITIALIZER, },.
981f0 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 69 66 };. if
98200 20 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 ( !isInit ){.
98210 20 20 20 20 20 20 41 50 49 52 45 54 20 72 63 3b APIRET rc;
98220 0a 20 20 20 20 20 20 20 20 50 54 49 42 20 70 74 . PTIB pt
98230 69 62 3b 0a 20 20 20 20 20 20 20 20 50 50 49 42 ib;. PPIB
98240 20 70 70 69 62 3b 0a 20 20 20 20 20 20 20 20 48 ppib;. H
98250 4d 54 58 20 6d 75 74 65 78 3b 0a 20 20 20 20 20 MTX mutex;.
98260 20 20 20 63 68 61 72 20 6e 61 6d 65 5b 33 32 5d char name[32]
98270 3b 0a 20 20 20 20 20 20 20 20 44 6f 73 47 65 74 ;. DosGet
98280 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26 70 74 69 InfoBlocks( &pti
98290 62 2c 20 26 70 70 69 62 20 29 3b 0a 20 20 20 20 b, &ppib );.
982a0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
982b0 69 6e 74 66 28 20 73 69 7a 65 6f 66 28 6e 61 6d intf( sizeof(nam
982c0 65 29 2c 20 6e 61 6d 65 2c 20 22 5c 5c 53 45 4d e), name, "\\SEM
982d0 33 32 5c 5c 53 51 4c 49 54 45 25 30 34 78 22 2c 32\\SQLITE%04x",
982e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
982f0 20 20 20 20 20 20 20 20 20 20 20 70 70 69 62 2d ppib-
98300 3e 70 69 62 5f 75 6c 70 69 64 20 29 3b 0a 20 20 >pib_ulpid );.
98310 20 20 20 20 20 20 77 68 69 6c 65 28 20 21 69 73 while( !is
98320 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Init ){.
98330 20 20 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 mutex = 0;.
98340 20 20 20 20 20 20 20 72 63 20 3d 20 44 6f 73 43 rc = DosC
98350 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 6e reateMutexSem( n
98360 61 6d 65 2c 20 26 6d 75 74 65 78 2c 20 30 2c 20 ame, &mutex, 0,
98370 46 41 4c 53 45 29 3b 0a 20 20 20 20 20 20 20 20 FALSE);.
98380 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 if( rc == NO_E
98390 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 RROR ){.
983a0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
983b0 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 i;.
983c0 69 66 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 if( !isInit ){.
983d0 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 for
983e0 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 73 69 7a ( i = 0; i < siz
983f0 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 eof(staticMutexe
98400 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 s)/sizeof(static
98410 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20 69 2b 2b Mutexes[0]); i++
98420 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
98430 20 20 20 20 44 6f 73 43 72 65 61 74 65 4d 75 74 DosCreateMut
98440 65 78 53 65 6d 28 20 30 2c 20 26 73 74 61 74 69 exSem( 0, &stati
98450 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 cMutexes[i].mute
98460 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 3b 0a 20 x, 0, FALSE );.
98470 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }.
98480 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 49 isI
98490 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 nit = 1;.
984a0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
984b0 20 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65 78 DosCloseMutex
984c0 53 65 6d 28 20 6d 75 74 65 78 20 29 3b 0a 20 20 Sem( mutex );.
984d0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
984e0 28 20 72 63 20 3d 3d 20 45 52 52 4f 52 5f 44 55 ( rc == ERROR_DU
984f0 50 4c 49 43 41 54 45 5f 4e 41 4d 45 20 29 7b 0a PLICATE_NAME ){.
98500 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 53 DosS
98510 6c 65 65 70 28 20 31 20 29 3b 0a 20 20 20 20 20 leep( 1 );.
98520 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
98530 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 return p
98540 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
98550 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
98560 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54 assert( iT
98570 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 ype-2 >= 0 );.
98580 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 assert( iTyp
98590 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 e-2 < sizeof(sta
985a0 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 ticMutexes)/size
985b0 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 of(staticMutexes
985c0 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 20 70 20 [0]) );. p
985d0 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 = &staticMutexes
985e0 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 [iType-2];.
985f0 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a p->id = iType;.
98600 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
98610 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
98620 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 p;.}.../*.** Thi
98630 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f s routine deallo
98640 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 cates a previous
98650 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 ly allocated mut
98660 65 78 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 ex..** SQLite is
98670 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c careful to deal
98680 6c 6f 63 61 74 65 20 65 76 65 72 79 20 6d 75 74 locate every mut
98690 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 ex that it alloc
986a0 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ates..*/.static
986b0 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 46 72 65 void os2MutexFre
986c0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
986d0 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 *p){. if( p==0
986e0 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
986f0 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 rt( p->nRef==0 )
98700 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
98710 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
98720 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 FAST || p->id==S
98730 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
98740 52 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 43 6c RSIVE );. DosCl
98750 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 70 2d 3e oseMutexSem( p->
98760 6d 75 74 65 78 20 29 3b 0a 20 20 73 71 6c 69 74 mutex );. sqlit
98770 65 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 7d 0a e3_free( p );.}.
98780 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
98790 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
987a0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
987b0 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 ex_try() routine
987c0 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 s attempt.** to
987d0 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20 enter a mutex.
987e0 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 If another threa
987f0 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 d is already wit
98800 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a hin the mutex,.*
98810 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f * sqlite3_mutex_
98820 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f enter() will blo
98830 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d ck and sqlite3_m
98840 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 utex_try() will
98850 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 return.** SQLITE
98860 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69 _BUSY. The sqli
98870 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
98880 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
98890 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 s SQLITE_OK.** u
988a0 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 pon successful e
988b0 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63 ntry. Mutexes c
988c0 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c reated using SQL
988d0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
988e0 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e IVE can.** be en
988f0 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 tered multiple t
98900 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 imes by the same
98910 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 thread. In suc
98920 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 h cases the,.**
98930 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 mutex must be ex
98940 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 ited an equal nu
98950 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 mber of times be
98960 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 fore another thr
98970 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 ead.** can enter
98980 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 . If the same t
98990 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 hread tries to e
989a0 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b nter any other k
989b0 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 ind of mutex.**
989c0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 more than once,
989d0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 the behavior is
989e0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 undefined..*/.st
989f0 61 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 atic void os2Mut
98a00 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f exEnter(sqlite3_
98a10 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 mutex *p){. TID
98a20 20 74 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 tid;. PID hold
98a30 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c er1;. ULONG hol
98a40 64 65 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 der2;. if( p==0
98a50 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 ) return;. ass
98a60 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
98a70 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
98a80 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78 4e 6f VE || os2MutexNo
98a90 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 44 6f theld(p) );. Do
98aa0 73 52 65 71 75 65 73 74 4d 75 74 65 78 53 65 6d sRequestMutexSem
98ab0 28 70 2d 3e 6d 75 74 65 78 2c 20 53 45 4d 5f 49 (p->mutex, SEM_I
98ac0 4e 44 45 46 49 4e 49 54 45 5f 57 41 49 54 29 3b NDEFINITE_WAIT);
98ad0 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 . DosQueryMutex
98ae0 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 Sem(p->mutex, &h
98af0 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 older1, &tid, &h
98b00 6f 6c 64 65 72 32 29 3b 0a 20 20 70 2d 3e 6f 77 older2);. p->ow
98b10 6e 65 72 20 3d 20 74 69 64 3b 0a 20 20 70 2d 3e ner = tid;. p->
98b20 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 nRef++;.}.static
98b30 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 54 72 79 int os2MutexTry
98b40 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
98b50 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 p){. int rc;.
98b60 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 TID tid;. PID h
98b70 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 older1;. ULONG
98b80 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 holder2;. if( p
98b90 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c ==0 ) return SQL
98ba0 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 ITE_OK;. assert
98bb0 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f ( p->id==SQLITE_
98bc0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
98bd0 7c 7c 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 || os2MutexNothe
98be0 6c 64 28 70 29 20 29 3b 0a 20 20 69 66 28 20 44 ld(p) );. if( D
98bf0 6f 73 52 65 71 75 65 73 74 4d 75 74 65 78 53 65 osRequestMutexSe
98c00 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 53 45 4d 5f m(p->mutex, SEM_
98c10 49 4d 4d 45 44 49 41 54 45 5f 52 45 54 55 52 4e IMMEDIATE_RETURN
98c20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 7b ) == NO_ERROR) {
98c30 0a 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 . DosQueryMut
98c40 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 exSem(p->mutex,
98c50 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 &holder1, &tid,
98c60 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 20 20 70 &holder2);. p
98c70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a 20 ->owner = tid;.
98c80 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 p->nRef++;.
98c90 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
98ca0 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 ;. } else {.
98cb0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
98cc0 59 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e Y;. }.. return
98cd0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
98ce0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
98cf0 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 leave() routine
98d00 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 exits a mutex th
98d10 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f at was.** previo
98d20 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 usly entered by
98d30 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e the same thread.
98d40 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a The behavior.*
98d50 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 * is undefined i
98d60 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e f the mutex is n
98d70 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 ot currently ent
98d80 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f ered or.** is no
98d90 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f t currently allo
98da0 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 cated. SQLite w
98db0 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 ill never do eit
98dc0 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 her..*/.static v
98dd0 6f 69 64 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 oid os2MutexLeav
98de0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
98df0 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a *p){. TID tid;.
98e00 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 PID holder1;.
98e10 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a ULONG holder2;.
98e20 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
98e30 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 urn;. assert( p
98e40 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 44 6f ->nRef>0 );. Do
98e50 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 sQueryMutexSem(p
98e60 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 ->mutex, &holder
98e70 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 1, &tid, &holder
98e80 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 2);. assert( p-
98e90 3e 6f 77 6e 65 72 3d 3d 74 69 64 20 29 3b 0a 20 >owner==tid );.
98ea0 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 p->nRef--;. as
98eb0 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 sert( p->nRef==0
98ec0 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 || p->id==SQLIT
98ed0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
98ee0 45 20 29 3b 0a 20 20 44 6f 73 52 65 6c 65 61 73 E );. DosReleas
98ef0 65 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 eMutexSem(p->mut
98f00 65 78 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 ex);.}..#ifdef S
98f10 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a QLITE_DEBUG./*.*
98f20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
98f30 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 tex_held() and s
98f40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
98f50 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 held() routine a
98f60 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 re.** intended f
98f70 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 or use inside as
98f80 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
98f90 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
98fa0 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 28 73 71 os2MutexHeld(sq
98fb0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
98fc0 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 . TID tid;. PI
98fd0 44 20 70 69 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 D pid;. ULONG u
98fe0 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 49 42 20 70 lCount;. PTIB p
98ff0 74 69 62 3b 0a 20 20 69 66 28 20 70 21 3d 30 20 tib;. if( p!=0
99000 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 ) {. DosQuery
99010 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 MutexSem(p->mute
99020 78 2c 20 26 70 69 64 2c 20 26 74 69 64 2c 20 26 x, &pid, &tid, &
99030 75 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d 20 65 6c ulCount);. } el
99040 73 65 20 7b 0a 20 20 20 20 44 6f 73 47 65 74 49 se {. DosGetI
99050 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c nfoBlocks(&ptib,
99060 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 74 69 64 20 NULL);. tid
99070 3d 20 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 = ptib->tib_ptib
99080 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64 3b 0a 20 2->tib2_ultid;.
99090 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 }. return p==0
990a0 20 7c 7c 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 || (p->nRef!=0
990b0 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64 && p->owner==tid
990c0 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 );.}.static int
990d0 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 os2MutexNotheld(
990e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
990f0 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 ){. TID tid;.
99100 50 49 44 20 70 69 64 3b 0a 20 20 55 4c 4f 4e 47 PID pid;. ULONG
99110 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 49 42 ulCount;. PTIB
99120 20 70 74 69 62 3b 0a 20 20 69 66 28 20 70 21 3d ptib;. if( p!=
99130 20 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 0 ) {. DosQu
99140 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d eryMutexSem(p->m
99150 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74 69 64 utex, &pid, &tid
99160 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d , &ulCount);. }
99170 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f 73 47 else {. DosG
99180 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 etInfoBlocks(&pt
99190 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 74 ib, NULL);. t
991a0 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 5f 70 id = ptib->tib_p
991b0 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64 tib2->tib2_ultid
991c0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
991d0 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 66 3d 3d ==0 || p->nRef==
991e0 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d 74 0 || p->owner!=t
991f0 69 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 53 51 id;.}.#endif..SQ
99200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
99210 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
99220 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 ds *sqlite3Defau
99230 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 ltMutex(void){.
99240 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
99250 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d mutex_methods sM
99260 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 6f 73 32 utex = {. os2
99270 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 6f MutexInit,. o
99280 73 32 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 s2MutexEnd,.
99290 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 os2MutexAlloc,.
992a0 20 20 20 6f 73 32 4d 75 74 65 78 46 72 65 65 2c os2MutexFree,
992b0 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 45 6e 74 . os2MutexEnt
992c0 65 72 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 er,. os2Mutex
992d0 54 72 79 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 Try,. os2Mute
992e0 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 53 xLeave,.#ifdef S
992f0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
99300 6f 73 32 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 os2MutexHeld,.
99310 20 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c os2MutexNothel
99320 64 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 d.#endif. };..
99330 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b return &sMutex;
99340 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
99350 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 2a 2f ITE_MUTEX_OS2 */
99360 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
99370 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6f * End of mutex_o
99380 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s2.c ***********
99390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
993a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
993b0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
993c0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 * Begin file mut
993d0 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a ex_unix.c ******
993e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
993f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99400 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 /./*.** 2007 Aug
99410 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 ust 28.**.** The
99420 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
99430 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
99440 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
99450 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
99460 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
99470 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
99480 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
99490 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
994a0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
994b0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
994c0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
994d0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
994e0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
994f0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
99500 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
99510 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
99520 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
99530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99570 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
99580 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
99590 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
995a0 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 implement mutex
995b0 65 73 20 66 6f 72 20 70 74 68 72 65 61 64 73 0a es for pthreads.
995c0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 **.** $Id: mutex
995d0 5f 75 6e 69 78 2e 63 2c 76 20 31 2e 31 36 20 32 _unix.c,v 1.16 2
995e0 30 30 38 2f 31 32 2f 30 38 20 31 38 3a 31 39 3a 008/12/08 18:19:
995f0 31 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 18 drh Exp $.*/.
99600 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 ./*.** The code
99610 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 in this file is
99620 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 77 65 20 only used if we
99630 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 are compiling th
99640 72 65 61 64 73 61 66 65 0a 2a 2a 20 75 6e 64 65 readsafe.** unde
99650 72 20 75 6e 69 78 20 77 69 74 68 20 70 74 68 72 r unix with pthr
99660 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 eads..**.** Note
99670 20 74 68 61 74 20 74 68 69 73 20 69 6d 70 6c 65 that this imple
99680 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 75 69 72 mentation requir
99690 65 73 20 61 20 76 65 72 73 69 6f 6e 20 6f 66 20 es a version of
996a0 70 74 68 72 65 61 64 73 20 74 68 61 74 0a 2a 2a pthreads that.**
996b0 20 73 75 70 70 6f 72 74 73 20 72 65 63 75 72 73 supports recurs
996c0 69 76 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a ive mutexes..*/.
996d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 #ifdef SQLITE_MU
996e0 54 45 58 5f 50 54 48 52 45 41 44 53 0a 0a 23 69 TEX_PTHREADS..#i
996f0 6e 63 6c 75 64 65 20 3c 70 74 68 72 65 61 64 2e nclude <pthread.
99700 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 h>.../*.** Each
99710 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 recursive mutex
99720 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
99730 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
99740 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 structure..*/.st
99750 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 ruct sqlite3_mut
99760 65 78 20 7b 0a 20 20 70 74 68 72 65 61 64 5f 6d ex {. pthread_m
99770 75 74 65 78 5f 74 20 6d 75 74 65 78 3b 20 20 20 utex_t mutex;
99780 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 /* Mutex contr
99790 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 olling the lock
997a0 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 */. int id;
997b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
997c0 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f /* Mutex type */
997d0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
997e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
997f0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 61 Number of entra
99800 6e 63 65 73 20 2a 2f 0a 20 20 70 74 68 72 65 61 nces */. pthrea
99810 64 5f 74 20 6f 77 6e 65 72 3b 20 20 20 20 20 20 d_t owner;
99820 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 74 /* Thread t
99830 68 61 74 20 69 73 20 77 69 74 68 69 6e 20 74 68 hat is within th
99840 69 73 20 6d 75 74 65 78 20 2a 2f 0a 23 69 66 64 is mutex */.#ifd
99850 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
99860 20 20 69 6e 74 20 74 72 61 63 65 3b 20 20 20 20 int trace;
99870 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
99880 54 72 75 65 20 74 6f 20 74 72 61 63 65 20 63 68 True to trace ch
99890 61 6e 67 65 73 20 2a 2f 0a 23 65 6e 64 69 66 0a anges */.#endif.
998a0 7d 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 };.#ifdef SQLITE
998b0 5f 44 45 42 55 47 0a 23 64 65 66 69 6e 65 20 53 _DEBUG.#define S
998c0 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 QLITE3_MUTEX_INI
998d0 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48 52 45 TIALIZER { PTHRE
998e0 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c AD_MUTEX_INITIAL
998f0 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70 74 68 IZER, 0, 0, (pth
99900 72 65 61 64 5f 74 29 30 2c 20 30 20 7d 0a 23 65 read_t)0, 0 }.#e
99910 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 lse.#define SQLI
99920 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 TE3_MUTEX_INITIA
99930 4c 49 5a 45 52 20 7b 20 50 54 48 52 45 41 44 5f LIZER { PTHREAD_
99940 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
99950 52 2c 20 30 2c 20 30 2c 20 28 70 74 68 72 65 61 R, 0, 0, (pthrea
99960 64 5f 74 29 30 20 7d 0a 23 65 6e 64 69 66 0a 0a d_t)0 }.#endif..
99970 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 /*.** The sqlite
99980 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 3_mutex_held() a
99990 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nd sqlite3_mutex
999a0 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 _notheld() routi
999b0 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 ne are.** intend
999c0 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 ed for use only
999d0 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
999e0 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 4f 6e 20 statements. On
999f0 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d 73 2c 0a some platforms,.
99a00 2a 2a 20 74 68 65 72 65 20 6d 69 67 68 74 20 62 ** there might b
99a10 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e e race condition
99a20 73 20 74 68 61 74 20 63 61 6e 20 63 61 75 73 65 s that can cause
99a30 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
99a40 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72 20 69 6e to.** deliver in
99a50 63 6f 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e correct results.
99a60 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c In particular,
99a70 20 69 66 20 70 74 68 72 65 61 64 5f 65 71 75 61 if pthread_equa
99a80 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 6e l() is.** not an
99a90 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 6f atomic operatio
99aa0 6e 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f n, then these ro
99ab0 75 74 69 6e 65 73 20 6d 69 67 68 74 20 64 65 6c utines might del
99ac0 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f 72 72 65 ivery.** incorre
99ad0 63 74 20 72 65 73 75 6c 74 73 2e 20 20 4f 6e 20 ct results. On
99ae0 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d 73 2c 20 most platforms,
99af0 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 pthread_equal()
99b00 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70 61 72 69 is a .** compari
99b10 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e 74 65 67 son of two integ
99b20 65 72 73 20 61 6e 64 20 69 73 20 74 68 65 72 65 ers and is there
99b30 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20 20 42 75 fore atomic. Bu
99b40 74 20 77 65 20 61 72 65 0a 2a 2a 20 74 6f 6c 64 t we are.** told
99b50 20 74 68 61 74 20 48 50 55 58 20 69 73 20 6e 6f that HPUX is no
99b60 74 20 73 75 63 68 20 61 20 70 6c 61 74 66 6f 72 t such a platfor
99b70 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68 65 6e 20 m. If so, then
99b80 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a these routines.*
99b90 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c 77 61 79 * will not alway
99ba0 73 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 s work correctly
99bb0 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a 2a 2a 20 on HPUX..**.**
99bc0 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74 66 6f 72 On those platfor
99bd0 6d 73 20 77 68 65 72 65 20 70 74 68 72 65 61 64 ms where pthread
99be0 5f 65 71 75 61 6c 28 29 20 69 73 20 6e 6f 74 20 _equal() is not
99bf0 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74 65 0a 2a atomic, SQLite.*
99c00 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 * should be comp
99c10 69 6c 65 64 20 77 69 74 68 6f 75 74 20 2d 44 53 iled without -DS
99c20 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 QLITE_DEBUG and
99c30 77 69 74 68 20 2d 44 4e 44 45 42 55 47 20 74 6f with -DNDEBUG to
99c40 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65 20 6e 6f .** make sure no
99c50 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
99c60 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 ents are evaluat
99c70 65 64 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65 ed and hence the
99c80 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 se.** routines a
99c90 72 65 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e re never called.
99ca0 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .*/.#if !defined
99cb0 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 (NDEBUG) || defi
99cc0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
99cd0 29 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 ).static int pth
99ce0 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28 73 71 readMutexHeld(sq
99cf0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
99d00 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 6e 52 . return (p->nR
99d10 65 66 21 3d 30 20 26 26 20 70 74 68 72 65 61 64 ef!=0 && pthread
99d20 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c _equal(p->owner,
99d30 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 pthread_self())
99d40 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 );.}.static int
99d50 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 pthreadMutexNoth
99d60 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 eld(sqlite3_mute
99d70 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 x *p){. return
99d80 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 74 p->nRef==0 || pt
99d90 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f hread_equal(p->o
99da0 77 6e 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65 wner, pthread_se
99db0 6c 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23 65 6e 64 lf())==0;.}.#end
99dc0 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 if../*.** Initia
99dd0 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 lize and deiniti
99de0 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 alize the mutex
99df0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
99e00 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 atic int pthread
99e10 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b MutexInit(void){
99e20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
99e30 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 K; }.static int
99e40 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 64 28 pthreadMutexEnd(
99e50 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 void){ return SQ
99e60 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a LITE_OK; }../*.*
99e70 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
99e80 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 tex_alloc() rout
99e90 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 ine allocates a
99ea0 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 new.** mutex and
99eb0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
99ec0 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 er to it. If it
99ed0 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a returns NULL.**
99ee0 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 that means that
99ef0 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e a mutex could n
99f00 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e ot be allocated.
99f10 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c SQLite.** will
99f20 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 unwind its stac
99f30 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 k and return an
99f40 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 error. The argu
99f50 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 ment.** to sqlit
99f60 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
99f70 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 is one of these
99f80 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
99f90 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a ts:.**.** <ul>.*
99fa0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
99fb0 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 UTEX_FAST.** <li
99fc0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
99fd0 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 RECURSIVE.** <li
99fe0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
99ff0 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a STATIC_MASTER.**
9a000 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
9a010 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a TEX_STATIC_MEM.*
9a020 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
9a030 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 UTEX_STATIC_MEM2
9a040 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
9a050 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 _MUTEX_STATIC_PR
9a060 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 NG.** <li> SQLI
9a070 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
9a080 4c 52 55 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a LRU.** </ul>.**.
9a090 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f ** The first two
9a0a0 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 constants cause
9a0b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
9a0c0 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 lloc() to create
9a0d0 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e .** a new mutex.
9a0e0 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 The new mutex
9a0f0 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 is recursive whe
9a100 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
9a110 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 ECURSIVE.** is u
9a120 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 sed but not nece
9a130 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 ssarily so when
9a140 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
9a150 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 T is used..** Th
9a160 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e e mutex implemen
9a170 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 tation does not
9a180 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 need to make a d
9a190 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 istinction.** be
9a1a0 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 tween SQLITE_MUT
9a1b0 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 EX_RECURSIVE and
9a1c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
9a1d0 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a ST if it does.**
9a1e0 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 not want to. B
9a1f0 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f ut SQLite will o
9a200 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 nly request a re
9a210 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e cursive mutex in
9a220 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 .** cases where
9a230 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 it really needs
9a240 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 one. If a faste
9a250 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 r non-recursive
9a260 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 mutex.** impleme
9a270 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c ntation is avail
9a280 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 able on the host
9a290 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d platform, the m
9a2a0 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a utex subsystem.*
9a2b0 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 * might return s
9a2c0 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 uch a mutex in r
9a2d0 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 esponse to SQLIT
9a2e0 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a E_MUTEX_FAST..**
9a2f0 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c .** The other al
9a300 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 lowed parameters
9a310 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
9a320 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 x_alloc() each r
9a330 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
9a340 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 er to a static p
9a350 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 reexisting mutex
9a360 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20 . Three static
9a370 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 mutexes are.** u
9a380 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 sed by the curre
9a390 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 nt version of SQ
9a3a0 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 Lite. Future ve
9a3b0 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 rsions of SQLite
9a3c0 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 .** may add addi
9a3d0 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 tional static mu
9a3e0 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d texes. Static m
9a3f0 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 utexes are for i
9a400 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 nternal.** use b
9a410 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 y SQLite only.
9a420 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 Applications tha
9a430 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 t use SQLite mut
9a440 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 exes should.** u
9a450 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 se only the dyna
9a460 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 mic mutexes retu
9a470 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d rned by SQLITE_M
9a480 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 UTEX_FAST or.**
9a490 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
9a4a0 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f URSIVE..**.** No
9a4b0 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f te that if one o
9a4c0 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 f the dynamic mu
9a4d0 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 tex parameters (
9a4e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
9a4f0 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d T.** or SQLITE_M
9a500 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 UTEX_RECURSIVE)
9a510 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c is used then sql
9a520 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
9a530 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 ().** returns a
9a540 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 different mutex
9a550 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 on every call.
9a560 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 But for the stat
9a570 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 ic .** mutex typ
9a580 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 es, the same mut
9a590 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f ex is returned o
9a5a0 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 n every call tha
9a5b0 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d t has.** the sam
9a5c0 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a e type number..*
9a5d0 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 /.static sqlite3
9a5e0 5f 6d 75 74 65 78 20 2a 70 74 68 72 65 61 64 4d _mutex *pthreadM
9a5f0 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 utexAlloc(int iT
9a600 79 70 65 29 7b 0a 20 20 73 74 61 74 69 63 20 73 ype){. static s
9a610 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 qlite3_mutex sta
9a620 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b ticMutexes[] = {
9a630 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 . SQLITE3_MUT
9a640 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a EX_INITIALIZER,.
9a650 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 SQLITE3_MUTE
9a660 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 X_INITIALIZER,.
9a670 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 SQLITE3_MUTEX
9a680 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 _INITIALIZER,.
9a690 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f SQLITE3_MUTEX_
9a6a0 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 INITIALIZER,.
9a6b0 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 SQLITE3_MUTEX_I
9a6c0 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 NITIALIZER,.
9a6d0 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e SQLITE3_MUTEX_IN
9a6e0 49 54 49 41 4c 49 5a 45 52 0a 20 20 7d 3b 0a 20 ITIALIZER. };.
9a6f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
9a700 70 3b 0a 20 20 73 77 69 74 63 68 28 20 69 54 79 p;. switch( iTy
9a710 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 pe ){. case S
9a720 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
9a730 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 RSIVE: {. p
9a740 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
9a750 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 Zero( sizeof(*p)
9a760 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 );. if( p
9a770 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ){.#ifdef SQLITE
9a780 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 _HOMEGROWN_RECUR
9a790 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 20 20 20 SIVE_MUTEX.
9a7a0 20 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 /* If recursi
9a7b0 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e ve mutexes are n
9a7c0 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 77 65 ot available, we
9a7d0 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 0a 20 20 will have to.
9a7e0 20 20 20 20 20 20 2a 2a 20 62 75 69 6c 64 20 6f ** build o
9a7f0 75 72 20 6f 77 6e 2e 20 20 53 65 65 20 62 65 6c ur own. See bel
9a800 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 ow. */. p
9a810 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 thread_mutex_ini
9a820 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 30 29 3b t(&p->mutex, 0);
9a830 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f .#else. /
9a840 2a 20 55 73 65 20 61 20 72 65 63 75 72 73 69 76 * Use a recursiv
9a850 65 20 6d 75 74 65 78 20 69 66 20 69 74 20 69 73 e mutex if it is
9a860 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 available */.
9a870 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 pthread_mu
9a880 74 65 78 61 74 74 72 5f 74 20 72 65 63 75 72 73 texattr_t recurs
9a890 69 76 65 41 74 74 72 3b 0a 20 20 20 20 20 20 20 iveAttr;.
9a8a0 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 pthread_mutexat
9a8b0 74 72 5f 69 6e 69 74 28 26 72 65 63 75 72 73 69 tr_init(&recursi
9a8c0 76 65 41 74 74 72 29 3b 0a 20 20 20 20 20 20 20 veAttr);.
9a8d0 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 pthread_mutexat
9a8e0 74 72 5f 73 65 74 74 79 70 65 28 26 72 65 63 75 tr_settype(&recu
9a8f0 72 73 69 76 65 41 74 74 72 2c 20 50 54 48 52 45 rsiveAttr, PTHRE
9a900 41 44 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 AD_MUTEX_RECURSI
9a910 56 45 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 VE);. pth
9a920 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 read_mutex_init(
9a930 26 70 2d 3e 6d 75 74 65 78 2c 20 26 72 65 63 75 &p->mutex, &recu
9a940 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20 rsiveAttr);.
9a950 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
9a960 78 61 74 74 72 5f 64 65 73 74 72 6f 79 28 26 72 xattr_destroy(&r
9a970 65 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 23 ecursiveAttr);.#
9a980 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 70 2d endif. p-
9a990 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 >id = iType;.
9a9a0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
9a9b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
9a9c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
9a9d0 53 54 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 ST: {. p =
9a9e0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
9a9f0 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b o( sizeof(*p) );
9aa00 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a . if( p ){.
9aa10 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 p->id =
9aa20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 70 iType;. p
9aa30 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 thread_mutex_ini
9aa40 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 30 29 3b t(&p->mutex, 0);
9aa50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
9aa60 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
9aa70 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
9aa80 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 assert( iType-2
9aa90 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 >= 0 );. a
9aaa0 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c ssert( iType-2 <
9aab0 20 41 72 72 61 79 53 69 7a 65 28 73 74 61 74 69 ArraySize(stati
9aac0 63 4d 75 74 65 78 65 73 29 20 29 3b 0a 20 20 20 cMutexes) );.
9aad0 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 p = &staticMu
9aae0 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a texes[iType-2];.
9aaf0 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 p->id = iT
9ab00 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ype;. break
9ab10 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
9ab20 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn p;.}.../*.*
9ab30 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
9ab40 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 eallocates a pre
9ab50 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 viously.** alloc
9ab60 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53 51 4c ated mutex. SQL
9ab70 69 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 ite is careful t
9ab80 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 o deallocate eve
9ab90 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68 61 74 ry.** mutex that
9aba0 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a it allocates..*
9abb0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 /.static void pt
9abc0 68 72 65 61 64 4d 75 74 65 78 46 72 65 65 28 73 hreadMutexFree(s
9abd0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
9abe0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e {. assert( p->n
9abf0 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 Ref==0 );. asse
9ac00 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
9ac10 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 E_MUTEX_FAST ||
9ac20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
9ac30 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b TEX_RECURSIVE );
9ac40 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 . pthread_mutex
9ac50 5f 64 65 73 74 72 6f 79 28 26 70 2d 3e 6d 75 74 _destroy(&p->mut
9ac60 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 ex);. sqlite3_f
9ac70 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ree(p);.}../*.**
9ac80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
9ac90 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 ex_enter() and s
9aca0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
9acb0 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 () routines atte
9acc0 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 mpt.** to enter
9acd0 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f a mutex. If ano
9ace0 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 ther thread is a
9acf0 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 lready within th
9ad00 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 e mutex,.** sqli
9ad10 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
9ad20 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 ) will block and
9ad30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
9ad40 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e ry() will return
9ad50 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e .** SQLITE_BUSY.
9ad60 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 The sqlite3_mu
9ad70 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 tex_try() interf
9ad80 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ace returns SQLI
9ad90 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 TE_OK.** upon su
9ada0 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 ccessful entry.
9adb0 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 Mutexes created
9adc0 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 using SQLITE_MU
9add0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 TEX_RECURSIVE ca
9ade0 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 n.** be entered
9adf0 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 multiple times b
9ae00 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 y the same threa
9ae10 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 d. In such case
9ae20 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 s the,.** mutex
9ae30 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 must be exited a
9ae40 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f n equal number o
9ae50 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 f times before a
9ae60 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a nother thread.**
9ae70 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 can enter. If
9ae80 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
9ae90 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 tries to enter a
9aea0 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 ny other kind of
9aeb0 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 mutex.** more t
9aec0 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 han once, the be
9aed0 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 havior is undefi
9aee0 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ned..*/.static v
9aef0 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78 oid pthreadMutex
9af00 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 Enter(sqlite3_mu
9af10 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 tex *p){. asser
9af20 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 t( p->id==SQLITE
9af30 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
9af40 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78 || pthreadMutex
9af50 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23 Notheld(p) );..#
9af60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d ifdef SQLITE_HOM
9af70 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 EGROWN_RECURSIVE
9af80 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72 _MUTEX. /* If r
9af90 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 ecursive mutexes
9afa0 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 are not availab
9afb0 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65 le, then we have
9afc0 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75 to grow. ** ou
9afd0 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70 r own. This imp
9afe0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 lementation assu
9aff0 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 64 mes that pthread
9b000 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73 _equal(). ** is
9b010 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69 atomic - that i
9b020 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65 t cannot be dece
9b030 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69 ived into thinki
9b040 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64 ng self. ** and
9b050 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71 p->owner are eq
9b060 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20 ual if p->owner
9b070 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20 changes between
9b080 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 two values. **
9b090 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75 that are not equ
9b0a0 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65 al to self while
9b0b0 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
9b0c0 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e is taking place.
9b0d0 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 . ** This imple
9b0e0 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61 mentation also a
9b0f0 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e ssumes a coheren
9b100 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a t cache - that .
9b110 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72 ** separate pr
9b120 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72 ocesses cannot r
9b130 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 61 ead different va
9b140 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 lues from the sa
9b150 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20 me. ** address
9b160 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
9b170 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 . If either of
9b180 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74 these two condit
9b190 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f ions. ** are no
9b1a0 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20 t met, then the
9b1b0 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69 mutexes will fai
9b1c0 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77 l and problems w
9b1d0 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f ill result.. */
9b1e0 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 . {. pthread
9b1f0 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61 _t self = pthrea
9b200 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66 d_self();. if
9b210 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70 ( p->nRef>0 && p
9b220 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e thread_equal(p->
9b230 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a owner, self) ){.
9b240 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b p->nRef++;
9b250 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
9b260 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
9b270 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b lock(&p->mutex);
9b280 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
9b290 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 ->nRef==0 );.
9b2a0 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 p->owner = se
9b2b0 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 lf;. p->nRe
9b2c0 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d f = 1;. }. }
9b2d0 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 20 .#else. /* Use
9b2e0 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63 the built-in rec
9b2f0 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69 ursive mutexes i
9b300 66 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c f they are avail
9b310 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 74 68 able.. */. pth
9b320 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 read_mutex_lock(
9b330 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d &p->mutex);. p-
9b340 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61 64 >owner = pthread
9b350 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d 3e 6e 52 _self();. p->nR
9b360 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 ef++;.#endif..#i
9b370 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
9b380 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 G. if( p->trace
9b390 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 ){. printf("
9b3a0 65 6e 74 65 72 20 6d 75 74 65 78 20 25 70 20 28 enter mutex %p (
9b3b0 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 %d) with nRef=%d
9b3c0 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 \n", p, p->trace
9b3d0 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a , p->nRef);. }.
9b3e0 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69 63 20 #endif.}.static
9b3f0 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 int pthreadMutex
9b400 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 Try(sqlite3_mute
9b410 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b x *p){. int rc;
9b420 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
9b430 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
9b440 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 72 ECURSIVE || pthr
9b450 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 eadMutexNotheld(
9b460 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 p) );..#ifdef SQ
9b470 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 LITE_HOMEGROWN_R
9b480 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 ECURSIVE_MUTEX.
9b490 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 65 /* If recursive
9b4a0 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 mutexes are not
9b4b0 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e available, then
9b4c0 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f 77 we have to grow
9b4d0 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 . ** our own.
9b4e0 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 This implementat
9b4f0 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 ion assumes that
9b500 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 pthread_equal()
9b510 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20 . ** is atomic
9b520 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 - that it cannot
9b530 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e 74 be deceived int
9b540 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a o thinking self.
9b550 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 ** and p->owne
9b560 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 70 r are equal if p
9b570 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20 ->owner changes
9b580 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75 between two valu
9b590 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65 es. ** that are
9b5a0 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65 not equal to se
9b5b0 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d lf while the com
9b5c0 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e parison is takin
9b5d0 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68 g place.. ** Th
9b5e0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
9b5f0 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61 n also assumes a
9b600 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20 coherent cache
9b610 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 70 - that . ** sep
9b620 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 20 arate processes
9b630 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 66 cannot read diff
9b640 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 6f erent values fro
9b650 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 m the same. **
9b660 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 73 address at the s
9b670 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 69 ame time. If ei
9b680 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77 ther of these tw
9b690 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a o conditions. *
9b6a0 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74 * are not met, t
9b6b0 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 hen the mutexes
9b6c0 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 72 will fail and pr
9b6d0 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 75 oblems will resu
9b6e0 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 lt.. */. {.
9b6f0 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20 pthread_t self
9b700 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 = pthread_self()
9b710 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 ;. if( p->nRe
9b720 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f 65 f>0 && pthread_e
9b730 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 qual(p->owner, s
9b740 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 2d elf) ){. p-
9b750 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 72 >nRef++;. r
9b760 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
9b770 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 74 68 }else if( pth
9b780 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 79 6c 6f read_mutex_trylo
9b790 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 ck(&p->mutex)==0
9b7a0 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
9b7b0 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a ( p->nRef==0 );.
9b7c0 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d p->owner =
9b7d0 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e self;. p->
9b7e0 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 nRef = 1;.
9b7f0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
9b800 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
9b810 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
9b820 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c Y;. }. }.#el
9b830 73 65 0a 20 20 2f 2a 20 55 73 65 20 74 68 65 20 se. /* Use the
9b840 62 75 69 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 built-in recursi
9b850 76 65 20 6d 75 74 65 78 65 73 20 69 66 20 74 68 ve mutexes if th
9b860 65 79 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 ey are available
9b870 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 74 68 .. */. if( pth
9b880 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 79 6c 6f read_mutex_trylo
9b890 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 ck(&p->mutex)==0
9b8a0 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 ){. p->owner
9b8b0 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 = pthread_self(
9b8c0 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b );. p->nRef++
9b8d0 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
9b8e0 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
9b8f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
9b900 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a USY;. }.#endif.
9b910 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
9b920 45 42 55 47 0a 20 20 69 66 28 20 72 63 3d 3d 53 EBUG. if( rc==S
9b930 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 74 QLITE_OK && p->t
9b940 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e race ){. prin
9b950 74 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20 tf("enter mutex
9b960 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 %p (%d) with nRe
9b970 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 f=%d\n", p, p->t
9b980 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a race, p->nRef);.
9b990 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 }.#endif. ret
9b9a0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
9b9b0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
9b9c0 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 ex_leave() routi
9b9d0 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 ne exits a mutex
9b9e0 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 that was.** pre
9b9f0 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 viously entered
9ba00 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
9ba10 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f ad. The behavio
9ba20 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 r.** is undefine
9ba30 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 d if the mutex i
9ba40 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
9ba50 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 entered or.** is
9ba60 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 not currently a
9ba70 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 llocated. SQLit
9ba80 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 e will never do
9ba90 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 either..*/.stati
9baa0 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 c void pthreadMu
9bab0 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 texLeave(sqlite3
9bac0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 _mutex *p){. as
9bad0 73 65 72 74 28 20 70 74 68 72 65 61 64 4d 75 74 sert( pthreadMut
9bae0 65 78 48 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 exHeld(p) );. p
9baf0 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 ->nRef--;. asse
9bb00 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c rt( p->nRef==0 |
9bb10 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f | p->id==SQLITE_
9bb20 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
9bb30 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
9bb40 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 E_HOMEGROWN_RECU
9bb50 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 69 66 RSIVE_MUTEX. if
9bb60 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a ( p->nRef==0 ){.
9bb70 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
9bb80 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 x_unlock(&p->mut
9bb90 65 78 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 ex);. }.#else.
9bba0 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 pthread_mutex_u
9bbb0 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 nlock(&p->mutex)
9bbc0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
9bbd0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
9bbe0 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a if( p->trace ){.
9bbf0 20 20 20 20 70 72 69 6e 74 66 28 22 6c 65 61 76 printf("leav
9bc00 65 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 e mutex %p (%d)
9bc10 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c with nRef=%d\n",
9bc20 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d p, p->trace, p-
9bc30 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 >nRef);. }.#end
9bc40 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 if.}..SQLITE_PRI
9bc50 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 VATE sqlite3_mut
9bc60 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 ex_methods *sqli
9bc70 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 te3DefaultMutex(
9bc80 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
9bc90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
9bca0 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b thods sMutex = {
9bcb0 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 . pthreadMute
9bcc0 78 49 6e 69 74 2c 0a 20 20 20 20 70 74 68 72 65 xInit,. pthre
9bcd0 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 adMutexEnd,.
9bce0 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f pthreadMutexAllo
9bcf0 63 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 c,. pthreadMu
9bd00 74 65 78 46 72 65 65 2c 0a 20 20 20 20 70 74 68 texFree,. pth
9bd10 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 2c 0a readMutexEnter,.
9bd20 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 pthreadMutex
9bd30 54 72 79 2c 0a 20 20 20 20 70 74 68 72 65 61 64 Try,. pthread
9bd40 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 MutexLeave,.#ifd
9bd50 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
9bd60 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 pthreadMutex
9bd70 48 65 6c 64 2c 0a 20 20 20 20 70 74 68 72 65 61 Held,. pthrea
9bd80 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 dMutexNotheld.#e
9bd90 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20 20 20 30 lse. 0,. 0
9bda0 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 .#endif. };..
9bdb0 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a return &sMutex;.
9bdc0 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
9bdd0 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 ITE_MUTEX_PTHREA
9bde0 44 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a D */../*********
9bdf0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 ***** End of mut
9be00 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a ex_unix.c ******
9be10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9be20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9be30 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
9be40 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
9be50 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a 2a mutex_w32.c ***
9be60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9be70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9be80 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
9be90 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a August 14.**.**
9bea0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
9beb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
9bec0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
9bed0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
9bee0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
9bef0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
9bf00 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
9bf10 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
9bf20 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
9bf30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
9bf40 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
9bf50 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
9bf60 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
9bf70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
9bf80 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
9bf90 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
9bfa0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
9bfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9bfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9bfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9bfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9bff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
9c000 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
9c010 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 the C functions
9c020 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d that implement m
9c030 75 74 65 78 65 73 20 66 6f 72 20 77 69 6e 33 32 utexes for win32
9c040 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 .**.** $Id: mute
9c050 78 5f 77 33 32 2e 63 2c 76 20 31 2e 31 33 20 32 x_w32.c,v 1.13 2
9c060 30 30 38 2f 31 32 2f 30 38 20 31 38 3a 31 39 3a 008/12/08 18:19:
9c070 31 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 18 drh Exp $.*/.
9c080 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 ./*.** The code
9c090 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 in this file is
9c0a0 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 77 65 20 only used if we
9c0b0 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 20 6d 75 are compiling mu
9c0c0 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 6f ltithreaded.** o
9c0d0 6e 20 61 20 77 69 6e 33 32 20 73 79 73 74 65 6d n a win32 system
9c0e0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
9c0f0 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 0a 2f 2a TE_MUTEX_W32../*
9c100 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 .** Each recursi
9c110 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 ve mutex is an i
9c120 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
9c130 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
9c140 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 re..*/.struct sq
9c150 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 lite3_mutex {.
9c160 43 52 49 54 49 43 41 4c 5f 53 45 43 54 49 4f 4e CRITICAL_SECTION
9c170 20 6d 75 74 65 78 3b 20 20 20 20 2f 2a 20 4d 75 mutex; /* Mu
9c180 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 tex controlling
9c190 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e the lock */. in
9c1a0 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 t id;
9c1b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 /* Mute
9c1c0 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 x type */. int
9c1d0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
9c1e0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
9c1f0 20 6f 66 20 65 6e 74 65 72 61 6e 63 65 73 20 2a of enterances *
9c200 2f 0a 20 20 44 57 4f 52 44 20 6f 77 6e 65 72 3b /. DWORD owner;
9c210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
9c220 2a 20 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 * Thread holding
9c230 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d this mutex */.}
9c240 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 ;../*.** Return
9c250 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 true (non-zero)
9c260 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e if we are runnin
9c270 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57 g under WinNT, W
9c280 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 in2K, WinXP,.**
9c290 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74 75 72 or WinCE. Retur
9c2a0 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 20 66 n false (zero) f
9c2b0 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c or Win95, Win98,
9c2c0 20 6f 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a or WinME..**.**
9c2d0 20 48 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 Here is an inte
9c2e0 72 65 73 74 69 6e 67 20 6f 62 73 65 72 76 61 74 resting observat
9c2f0 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e ion: Win95, Win
9c300 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 98, and WinME la
9c310 63 6b 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 ck.** the LockFi
9c320 6c 65 45 78 28 29 20 41 50 49 2e 20 20 42 75 74 leEx() API. But
9c330 20 77 65 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 we can still st
9c340 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 atically link ag
9c350 61 69 6e 73 74 20 74 68 61 74 0a 2a 2a 20 41 50 ainst that.** AP
9c360 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 I as long as we
9c370 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20 77 69 don't call it wi
9c380 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f n running Win95/
9c390 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 98/ME. A call t
9c3a0 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e o.** this routin
9c3b0 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 e is used to det
9c3c0 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 68 6f ermine if the ho
9c3d0 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d st is Win95/98/M
9c3e0 45 20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b E or.** WinNT/2K
9c3f0 2f 58 50 20 73 6f 20 74 68 61 74 20 77 65 20 77 /XP so that we w
9c400 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 ill know whether
9c410 20 6f 72 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 or not we can s
9c420 61 66 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 afely call.** th
9c430 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 e LockFileEx() A
9c440 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d 75 74 65 78 49 PI..**.** mutexI
9c450 73 4e 54 28 29 20 69 73 20 6f 6e 6c 79 20 75 73 sNT() is only us
9c460 65 64 20 66 6f 72 20 74 68 65 20 54 72 79 45 6e ed for the TryEn
9c470 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
9c480 6f 6e 28 29 20 41 50 49 20 63 61 6c 6c 2c 0a 2a on() API call,.*
9c490 2a 20 77 68 69 63 68 20 69 73 20 6f 6e 6c 79 20 * which is only
9c4a0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 79 6f 75 available if you
9c4b0 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 77 61 r application wa
9c4c0 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 s compiled with
9c4d0 0a 2a 2a 20 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 .** _WIN32_WINNT
9c4e0 20 64 65 66 69 6e 65 64 20 74 6f 20 61 20 76 61 defined to a va
9c4f0 6c 75 65 20 3e 3d 20 30 78 30 34 30 30 2e 20 20 lue >= 0x0400.
9c500 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f Currently, the o
9c510 6e 6c 79 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 54 nly.** call to T
9c520 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 ryEnterCriticalS
9c530 65 63 74 69 6f 6e 28 29 20 69 73 20 23 69 66 64 ection() is #ifd
9c540 65 66 27 65 64 20 6f 75 74 2c 20 73 6f 20 23 69 ef'ed out, so #i
9c550 66 64 65 66 20 0a 2a 2a 20 74 68 69 73 20 6f 75 fdef .** this ou
9c560 74 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 t as well..*/.#i
9c570 66 20 30 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f f 0.#if SQLITE_O
9c580 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 S_WINCE.# define
9c590 20 6d 75 74 65 78 49 73 4e 54 28 29 20 20 28 31 mutexIsNT() (1
9c5a0 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 69 63 ).#else. static
9c5b0 20 69 6e 74 20 6d 75 74 65 78 49 73 4e 54 28 76 int mutexIsNT(v
9c5c0 6f 69 64 29 7b 0a 20 20 20 20 73 74 61 74 69 63 oid){. static
9c5d0 20 69 6e 74 20 6f 73 54 79 70 65 20 3d 20 30 3b int osType = 0;
9c5e0 0a 20 20 20 20 69 66 28 20 6f 73 54 79 70 65 3d . if( osType=
9c5f0 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 =0 ){. OSVE
9c600 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b RSIONINFO sInfo;
9c610 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f . sInfo.dwO
9c620 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 SVersionInfoSize
9c630 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 = sizeof(sInfo)
9c640 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69 ;. GetVersi
9c650 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 onEx(&sInfo);.
9c660 20 20 20 20 6f 73 54 79 70 65 20 3d 20 73 49 6e osType = sIn
9c670 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d fo.dwPlatformId=
9c680 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 =VER_PLATFORM_WI
9c690 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a N32_NT ? 2 : 1;.
9c6a0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
9c6b0 20 6f 73 54 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a osType==2;. }.
9c6c0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
9c6d0 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 23 65 6e _OS_WINCE */.#en
9c6e0 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
9c6f0 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 TE_DEBUG./*.** T
9c700 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
9c710 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 _held() and sqli
9c720 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
9c730 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a d() routine are.
9c740 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 ** intended for
9c750 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 use only inside
9c760 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
9c770 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 nts..*/.static i
9c780 6e 74 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 28 nt winMutexHeld(
9c790 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
9c7a0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e ){. return p->n
9c7b0 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f 77 6e Ref!=0 && p->own
9c7c0 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 er==GetCurrentTh
9c7d0 72 65 61 64 49 64 28 29 3b 0a 7d 0a 73 74 61 74 readId();.}.stat
9c7e0 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 4e ic int winMutexN
9c7f0 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d otheld(sqlite3_m
9c800 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
9c810 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c rn p->nRef==0 ||
9c820 20 70 2d 3e 6f 77 6e 65 72 21 3d 47 65 74 43 75 p->owner!=GetCu
9c830 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b rrentThreadId();
9c840 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a .}.#endif.../*.*
9c850 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 * Initialize and
9c860 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 deinitialize th
9c870 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 e mutex subsyste
9c880 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 m..*/.static int
9c890 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 76 6f winMutexInit(vo
9c8a0 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 id){ return SQLI
9c8b0 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 TE_OK; }.static
9c8c0 69 6e 74 20 77 69 6e 4d 75 74 65 78 45 6e 64 28 int winMutexEnd(
9c8d0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 void){ return SQ
9c8e0 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a LITE_OK; }../*.*
9c8f0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
9c900 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 tex_alloc() rout
9c910 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 ine allocates a
9c920 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 new.** mutex and
9c930 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
9c940 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 er to it. If it
9c950 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a returns NULL.**
9c960 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 that means that
9c970 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e a mutex could n
9c980 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e ot be allocated.
9c990 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c SQLite.** will
9c9a0 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 unwind its stac
9c9b0 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 k and return an
9c9c0 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 error. The argu
9c9d0 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 ment.** to sqlit
9c9e0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
9c9f0 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 is one of these
9ca00 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
9ca10 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a ts:.**.** <ul>.*
9ca20 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
9ca30 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 UTEX_FAST
9ca40 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 0.** <li
9ca50 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
9ca60 52 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 RECURSIVE
9ca70 20 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 1.** <li> SQ
9ca80 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
9ca90 43 5f 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a C_MASTER 2.
9caa0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
9cab0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d MUTEX_STATIC_MEM
9cac0 20 20 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 3.** <l
9cad0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
9cae0 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 _STATIC_PRNG
9caf0 20 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 4.** </ul>.*
9cb00 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 *.** The first t
9cb10 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 wo constants cau
9cb20 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 se sqlite3_mutex
9cb30 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 _alloc() to crea
9cb40 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 te.** a new mute
9cb50 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 x. The new mute
9cb60 78 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77 x is recursive w
9cb70 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 hen SQLITE_MUTEX
9cb80 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 _RECURSIVE.** is
9cb90 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 used but not ne
9cba0 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 cessarily so whe
9cbb0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 n SQLITE_MUTEX_F
9cbc0 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 AST is used..**
9cbd0 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d The mutex implem
9cbe0 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f entation does no
9cbf0 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 t need to make a
9cc00 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 distinction.**
9cc10 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d between SQLITE_M
9cc20 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 UTEX_RECURSIVE a
9cc30 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f nd SQLITE_MUTEX_
9cc40 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a FAST if it does.
9cc50 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 ** not want to.
9cc60 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c But SQLite will
9cc70 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 only request a
9cc80 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 recursive mutex
9cc90 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 in.** cases wher
9cca0 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 e it really need
9ccb0 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 s one. If a fas
9ccc0 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 ter non-recursiv
9ccd0 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 e mutex.** imple
9cce0 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 mentation is ava
9ccf0 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f ilable on the ho
9cd00 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 st platform, the
9cd10 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d mutex subsystem
9cd20 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e .** might return
9cd30 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e such a mutex in
9cd40 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c response to SQL
9cd50 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a ITE_MUTEX_FAST..
9cd60 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 **.** The other
9cd70 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 allowed paramete
9cd80 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 rs to sqlite3_mu
9cd90 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 tex_alloc() each
9cda0 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 return.** a poi
9cdb0 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 nter to a static
9cdc0 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 preexisting mut
9cdd0 65 78 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 ex. Three stati
9cde0 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a c mutexes are.**
9cdf0 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72 used by the cur
9ce00 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 rent version of
9ce10 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 SQLite. Future
9ce20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 versions of SQLi
9ce30 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 te.** may add ad
9ce40 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 ditional static
9ce50 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 mutexes. Static
9ce60 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 mutexes are for
9ce70 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 internal.** use
9ce80 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e by SQLite only.
9ce90 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 Applications t
9cea0 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d hat use SQLite m
9ceb0 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a utexes should.**
9cec0 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 use only the dy
9ced0 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 namic mutexes re
9cee0 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 turned by SQLITE
9cef0 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a _MUTEX_FAST or.*
9cf00 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 * SQLITE_MUTEX_R
9cf10 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 ECURSIVE..**.**
9cf20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 Note that if one
9cf30 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 of the dynamic
9cf40 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 mutex parameters
9cf50 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 (SQLITE_MUTEX_F
9cf60 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 AST.** or SQLITE
9cf70 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
9cf80 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 ) is used then s
9cf90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
9cfa0 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 oc().** returns
9cfb0 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 a different mute
9cfc0 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e x on every call.
9cfd0 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 But for the st
9cfe0 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74 atic .** mutex t
9cff0 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d ypes, the same m
9d000 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 utex is returned
9d010 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 on every call t
9d020 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 hat has.** the s
9d030 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e ame type number.
9d040 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 .*/.static sqlit
9d050 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d 75 74 e3_mutex *winMut
9d060 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 exAlloc(int iTyp
9d070 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 e){. sqlite3_mu
9d080 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69 74 63 tex *p;.. switc
9d090 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 h( iType ){.
9d0a0 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 case SQLITE_MUTE
9d0b0 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65 X_FAST:. case
9d0c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
9d0d0 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 CURSIVE: {.
9d0e0 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c p = sqlite3Mall
9d0f0 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a ocZero( sizeof(*
9d100 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 p) );. if(
9d110 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e p ){. p->
9d120 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 id = iType;.
9d130 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 InitializeCr
9d140 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 iticalSection(&p
9d150 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ->mutex);.
9d160 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
9d170 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 }. default
9d180 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 : {. static
9d190 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 sqlite3_mutex s
9d1a0 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 3b taticMutexes[6];
9d1b0 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 69 6e . static in
9d1c0 74 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 t isInit = 0;.
9d1d0 20 20 20 20 77 68 69 6c 65 28 20 21 69 73 49 6e while( !isIn
9d1e0 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 it ){. st
9d1f0 61 74 69 63 20 6c 6f 6e 67 20 6c 6f 63 6b 20 3d atic long lock =
9d200 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0;. if(
9d210 49 6e 74 65 72 6c 6f 63 6b 65 64 49 6e 63 72 65 InterlockedIncre
9d220 6d 65 6e 74 28 26 6c 6f 63 6b 29 3d 3d 31 20 29 ment(&lock)==1 )
9d230 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 {. int
9d240 69 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 i;. for
9d250 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 73 (i=0; i<sizeof(s
9d260 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 taticMutexes)/si
9d270 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 zeof(staticMutex
9d280 65 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 es[0]); i++){.
9d290 20 20 20 20 20 20 20 20 20 20 49 6e 69 74 69 61 Initia
9d2a0 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 lizeCriticalSect
9d2b0 69 6f 6e 28 26 73 74 61 74 69 63 4d 75 74 65 78 ion(&staticMutex
9d2c0 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 es[i].mutex);.
9d2d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9d2e0 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31 3b 0a isInit = 1;.
9d2f0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
9d300 20 20 20 20 20 20 20 20 20 53 6c 65 65 70 28 31 Sleep(1
9d310 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
9d320 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 }. asser
9d330 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 t( iType-2 >= 0
9d340 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
9d350 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f iType-2 < sizeo
9d360 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 f(staticMutexes)
9d370 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 /sizeof(staticMu
9d380 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 texes[0]) );.
9d390 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 p = &staticMu
9d3a0 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a texes[iType-2];.
9d3b0 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 p->id = iT
9d3c0 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ype;. break
9d3d0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
9d3e0 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn p;.}.../*.*
9d3f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
9d400 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 eallocates a pre
9d410 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 viously.** alloc
9d420 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53 51 4c ated mutex. SQL
9d430 69 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 ite is careful t
9d440 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 o deallocate eve
9d450 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68 61 74 ry.** mutex that
9d460 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a it allocates..*
9d470 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 /.static void wi
9d480 6e 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 nMutexFree(sqlit
9d490 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
9d4a0 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61 assert( p );. a
9d4b0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
9d4c0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
9d4d0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
9d4e0 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 EX_FAST || p->id
9d4f0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
9d500 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44 65 ECURSIVE );. De
9d510 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65 63 74 leteCriticalSect
9d520 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a ion(&p->mutex);.
9d530 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
9d540 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 );.}../*.** The
9d550 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
9d560 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ter() and sqlite
9d570 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 3_mutex_try() ro
9d580 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a utines attempt.*
9d590 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 * to enter a mut
9d5a0 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 ex. If another
9d5b0 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 thread is alread
9d5c0 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 y within the mut
9d5d0 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ex,.** sqlite3_m
9d5e0 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c utex_enter() wil
9d5f0 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 l block and sqli
9d600 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
9d610 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 will return.** S
9d620 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 QLITE_BUSY. The
9d630 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
9d640 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ry() interface r
9d650 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
9d660 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 .** upon success
9d670 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 ful entry. Mute
9d680 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e xes created usin
9d690 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 g SQLITE_MUTEX_R
9d6a0 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 ECURSIVE can.**
9d6b0 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 be entered multi
9d6c0 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 ple times by the
9d6d0 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 same thread. I
9d6e0 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 n such cases the
9d6f0 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 ,.** mutex must
9d700 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 be exited an equ
9d710 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d al number of tim
9d720 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 es before anothe
9d730 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 r thread.** can
9d740 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 enter. If the s
9d750 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 ame thread tries
9d760 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 to enter any ot
9d770 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 her kind of mute
9d780 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f x.** more than o
9d790 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f nce, the behavio
9d7a0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a r is undefined..
9d7b0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 */.static void w
9d7c0 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c inMutexEnter(sql
9d7d0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
9d7e0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
9d7f0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
9d800 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 CURSIVE || winMu
9d810 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
9d820 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63 61 6c . EnterCritical
9d830 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 Section(&p->mute
9d840 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d x);. p->owner =
9d850 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 GetCurrentThrea
9d860 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e 52 65 dId(); . p->nRe
9d870 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e f++;.}.static in
9d880 74 20 77 69 6e 4d 75 74 65 78 54 72 79 28 73 71 t winMutexTry(sq
9d890 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
9d8a0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
9d8b0 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 73 65 72 TE_BUSY;. asser
9d8c0 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 t( p->id==SQLITE
9d8d0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
9d8e0 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 || winMutexNoth
9d8f0 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a 0a 20 eld(p) );. /*.
9d900 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f ** The sqlite3_
9d910 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 mutex_try() rout
9d920 69 6e 65 20 69 73 20 76 65 72 79 20 72 61 72 65 ine is very rare
9d930 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77 68 65 ly used, and whe
9d940 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75 73 65 n it. ** is use
9d950 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79 20 61 d it is merely a
9d960 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 n optimization.
9d970 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 So it is OK for
9d980 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 it to always.
9d990 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a 2a 0a ** fail. . **.
9d9a0 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e 74 65 ** The TryEnte
9d9b0 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e rCriticalSection
9d9c0 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 () interface is
9d9d0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f only available o
9d9e0 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20 41 6e n WinNT.. ** An
9d9f0 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73 20 63 d some windows c
9da00 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 ompilers complai
9da10 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74 6f 20 n if you try to
9da20 75 73 65 20 69 74 20 77 69 74 68 6f 75 74 0a 20 use it without.
9da30 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e 67 20 ** first doing
9da40 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20 74 68 some #defines th
9da50 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c 69 74 at prevent SQLit
9da60 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e 67 20 e from building
9da70 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a 20 46 on Win98.. ** F
9da80 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 or that reason,
9da90 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74 68 69 we will omit thi
9daa0 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66 s optimization f
9dab0 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20 20 2a or now. See. *
9dac0 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35 2e 0a * ticket #2685..
9dad0 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69 66 28 */.#if 0. if(
9dae0 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26 26 20 mutexIsNT() &&
9daf0 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c TryEnterCritical
9db00 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 Section(&p->mute
9db10 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e x) ){. p->own
9db20 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 er = GetCurrentT
9db30 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20 20 70 hreadId();. p
9db40 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 ->nRef++;. rc
9db50 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
9db60 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 }.#endif. retur
9db70 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
9db80 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
9db90 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 _leave() routine
9dba0 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 exits a mutex t
9dbb0 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 hat was.** previ
9dbc0 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 ously entered by
9dbd0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
9dbe0 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a . The behavior.
9dbf0 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 ** is undefined
9dc00 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 if the mutex is
9dc10 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e not currently en
9dc20 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e tered or.** is n
9dc30 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c ot currently all
9dc40 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 ocated. SQLite
9dc50 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 will never do ei
9dc60 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ther..*/.static
9dc70 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 61 void winMutexLea
9dc80 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ve(sqlite3_mutex
9dc90 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
9dca0 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61 p->nRef>0 );. a
9dcb0 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d ssert( p->owner=
9dcc0 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 =GetCurrentThrea
9dcd0 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 dId() );. p->nR
9dce0 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 ef--;. assert(
9dcf0 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d p->nRef==0 || p-
9dd00 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
9dd10 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 X_RECURSIVE );.
9dd20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53 65 LeaveCriticalSe
9dd30 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 ction(&p->mutex)
9dd40 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.}..SQLITE_PRIV
9dd50 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ATE sqlite3_mute
9dd60 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 x_methods *sqlit
9dd70 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 e3DefaultMutex(v
9dd80 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 oid){. static s
9dd90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
9dda0 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a hods sMutex = {.
9ddb0 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 winMutexInit
9ddc0 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e ,. winMutexEn
9ddd0 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 41 d,. winMutexA
9dde0 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75 74 lloc,. winMut
9ddf0 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e 4d exFree,. winM
9de00 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 77 utexEnter,. w
9de10 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 inMutexTry,.
9de20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 winMutexLeave,.#
9de30 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
9de40 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 48 UG. winMutexH
9de50 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 eld,. winMute
9de60 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 xNotheld.#else.
9de70 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 0,. 0.#end
9de80 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 if. };.. retur
9de90 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e n &sMutex;.}.#en
9dea0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 dif /* SQLITE_MU
9deb0 54 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a TEX_W32 */../***
9dec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
9ded0 6f 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a of mutex_w32.c *
9dee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9def0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9df00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
9df10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
9df20 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 n file malloc.c
9df30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9df40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9df50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
9df60 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
9df70 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
9df80 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
9df90 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
9dfa0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
9dfb0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
9dfc0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
9dfd0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
9dfe0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
9dff0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
9e000 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
9e010 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
9e020 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
9e030 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
9e040 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
9e050 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
9e060 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
9e070 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
9e080 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
9e090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e0d0 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 **.**.** Memory
9e0e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 allocation funct
9e0f0 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 ions used throug
9e100 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a hout sqlite..**.
9e110 2a 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63 ** $Id: malloc.c
9e120 2c 76 20 31 2e 35 33 20 32 30 30 38 2f 31 32 2f ,v 1.53 2008/12/
9e130 31 36 20 31 37 3a 32 30 3a 33 38 20 73 68 61 6e 16 17:20:38 shan
9e140 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a e Exp $.*/../*.*
9e150 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
9e160 75 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 65 6d uns when the mem
9e170 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 73 65 ory allocator se
9e180 65 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 es that the.** t
9e190 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f otal memory allo
9e1a0 63 61 74 69 6f 6e 20 69 73 20 61 62 6f 75 74 20 cation is about
9e1b0 74 6f 20 65 78 63 65 65 64 20 74 68 65 20 73 6f to exceed the so
9e1c0 66 74 20 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 74 ft heap.** limit
9e1d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
9e1e0 20 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e softHeapLimitEn
9e1f0 66 6f 72 63 65 72 28 0a 20 20 76 6f 69 64 20 2a forcer(. void *
9e200 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 NotUsed, . sqli
9e210 74 65 33 5f 69 6e 74 36 34 20 4e 6f 74 55 73 65 te3_int64 NotUse
9e220 64 32 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f 63 53 d2,. int allocS
9e230 69 7a 65 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f ize.){. UNUSED_
9e240 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 PARAMETER2(NotUs
9e250 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 ed, NotUsed2);.
9e260 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
9e270 5f 6d 65 6d 6f 72 79 28 61 6c 6c 6f 63 53 69 7a _memory(allocSiz
9e280 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 e);.}../*.** Set
9e290 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 2d 73 the soft heap-s
9e2a0 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 ize limit for th
9e2b0 65 20 6c 69 62 72 61 72 79 2e 20 50 61 73 73 69 e library. Passi
9e2c0 6e 67 20 61 20 7a 65 72 6f 20 6f 72 20 0a 2a 2a ng a zero or .**
9e2d0 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 negative value
9e2e0 69 6e 64 69 63 61 74 65 73 20 6e 6f 20 6c 69 6d indicates no lim
9e2f0 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 it..*/.SQLITE_AP
9e300 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 I void sqlite3_s
9e310 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 oft_heap_limit(i
9e320 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 nt n){. sqlite3
9e330 5f 75 69 6e 74 36 34 20 69 4c 69 6d 69 74 3b 0a _uint64 iLimit;.
9e340 20 20 69 6e 74 20 6f 76 65 72 61 67 65 3b 0a 20 int overage;.
9e350 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 if( n<0 ){.
9e360 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 65 iLimit = 0;. }e
9e370 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 lse{. iLimit
9e380 3d 20 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 = n;. }. sqlit
9e390 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b e3_initialize();
9e3a0 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 30 20 . if( iLimit>0
9e3b0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ){. sqlite3Me
9e3c0 6d 6f 72 79 41 6c 61 72 6d 28 73 6f 66 74 48 65 moryAlarm(softHe
9e3d0 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 2c apLimitEnforcer,
9e3e0 20 30 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 7d 0, iLimit);. }
9e3f0 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
9e400 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30 2c 20 3MemoryAlarm(0,
9e410 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 6f 76 65 0, 0);. }. ove
9e420 72 61 67 65 20 3d 20 28 69 6e 74 29 28 73 71 6c rage = (int)(sql
9e430 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 ite3_memory_used
9e440 28 29 20 2d 20 28 69 36 34 29 6e 29 3b 0a 20 20 () - (i64)n);.
9e450 69 66 28 20 6f 76 65 72 61 67 65 3e 30 20 29 7b if( overage>0 ){
9e460 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6c . sqlite3_rel
9e470 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f 76 65 72 ease_memory(over
9e480 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a age);. }.}../*.
9e490 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 ** Attempt to re
9e4a0 6c 65 61 73 65 20 75 70 20 74 6f 20 6e 20 62 79 lease up to n by
9e4b0 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e tes of non-essen
9e4c0 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63 75 72 72 tial memory curr
9e4d0 65 6e 74 6c 79 0a 2a 2a 20 68 65 6c 64 20 62 79 ently.** held by
9e4e0 20 53 51 4c 69 74 65 2e 20 41 6e 20 65 78 61 6d SQLite. An exam
9e4f0 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e ple of non-essen
9e500 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69 73 20 6d tial memory is m
9e510 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 0a 2a 2a emory used to.**
9e520 20 63 61 63 68 65 20 64 61 74 61 62 61 73 65 20 cache database
9e530 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20 6e pages that are n
9e540 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 ot currently in
9e550 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 use..*/.SQLITE_A
9e560 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 PI int sqlite3_r
9e570 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e elease_memory(in
9e580 74 20 6e 29 7b 0a 23 69 66 64 65 66 20 53 51 4c t n){.#ifdef SQL
9e590 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 ITE_ENABLE_MEMOR
9e5a0 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 69 Y_MANAGEMENT. i
9e5b0 6e 74 20 6e 52 65 74 20 3d 20 30 3b 0a 23 69 66 nt nRet = 0;.#if
9e5c0 20 30 0a 20 20 6e 52 65 74 20 2b 3d 20 73 71 6c 0. nRet += sql
9e5d0 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 4d ite3VdbeReleaseM
9e5e0 65 6d 6f 72 79 28 6e 29 3b 0a 23 65 6e 64 69 66 emory(n);.#endif
9e5f0 0a 20 20 6e 52 65 74 20 2b 3d 20 73 71 6c 69 74 . nRet += sqlit
9e600 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d e3PcacheReleaseM
9e610 65 6d 6f 72 79 28 6e 2d 6e 52 65 74 29 3b 0a 20 emory(n-nRet);.
9e620 20 72 65 74 75 72 6e 20 6e 52 65 74 3b 0a 23 65 return nRet;.#e
9e630 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 lse. UNUSED_PAR
9e640 41 4d 45 54 45 52 28 6e 29 3b 0a 20 20 72 65 74 AMETER(n);. ret
9e650 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 urn SQLITE_OK;.#
9e660 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 endif.}../*.** S
9e670 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tate information
9e680 20 6c 6f 63 61 6c 20 74 6f 20 74 68 65 20 6d 65 local to the me
9e690 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
9e6a0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
9e6b0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
9e6c0 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61 struct Mem0Globa
9e6d0 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 l {. /* Number
9e6e0 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 66 6f of free pages fo
9e6f0 72 20 73 63 72 61 74 63 68 20 61 6e 64 20 70 61 r scratch and pa
9e700 67 65 2d 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 ge-cache memory
9e710 2a 2f 0a 20 20 75 33 32 20 6e 53 63 72 61 74 63 */. u32 nScratc
9e720 68 46 72 65 65 3b 0a 20 20 75 33 32 20 6e 50 61 hFree;. u32 nPa
9e730 67 65 46 72 65 65 3b 0a 0a 20 20 73 71 6c 69 74 geFree;.. sqlit
9e740 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b e3_mutex *mutex;
9e750 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 /* Mute
9e760 78 20 74 6f 20 73 65 72 69 61 6c 69 7a 65 20 61 x to serialize a
9e770 63 63 65 73 73 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 ccess */.. /*.
9e780 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d 20 63 61 ** The alarm ca
9e790 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 73 20 61 llback and its a
9e7a0 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20 6d rguments. The m
9e7b0 65 6d 30 2e 6d 75 74 65 78 20 6c 6f 63 6b 20 77 em0.mutex lock w
9e7c0 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65 6c 64 ill. ** be held
9e7d0 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c 6c 62 while the callb
9e7e0 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 ack is running.
9e7f0 20 52 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 Recursive calls
9e800 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6d into. ** the m
9e810 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 emory subsystem
9e820 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62 75 74 are allowed, but
9e830 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b no new callback
9e840 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a 20 69 s will be. ** i
9e850 73 73 75 65 64 2e 20 20 54 68 65 20 61 6c 61 72 ssued. The alar
9e860 6d 42 75 73 79 20 76 61 72 69 61 62 6c 65 20 69 mBusy variable i
9e870 73 20 73 65 74 20 74 6f 20 70 72 65 76 65 6e 74 s set to prevent
9e880 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a 2a 20 recursive. **
9e890 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a callbacks.. */.
9e8a0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
9e8b0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a alarmThreshold;.
9e8c0 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61 void (*alarmCa
9e8d0 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73 llback)(void*, s
9e8e0 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 qlite3_int64,int
9e8f0 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d );. void *alarm
9e900 41 72 67 3b 0a 20 20 69 6e 74 20 61 6c 61 72 6d Arg;. int alarm
9e910 42 75 73 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a Busy;.. /*. **
9e920 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 Pointers to the
9e930 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 47 end of sqlite3G
9e940 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 lobalConfig.pScr
9e950 61 74 63 68 20 61 6e 64 0a 20 20 2a 2a 20 73 71 atch and. ** sq
9e960 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9e970 67 2e 70 50 61 67 65 20 74 6f 20 61 20 62 6c 6f g.pPage to a blo
9e980 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61 ck of memory tha
9e990 74 20 72 65 63 6f 72 64 73 0a 20 20 2a 2a 20 77 t records. ** w
9e9a0 68 69 63 68 20 70 61 67 65 73 20 61 72 65 20 61 hich pages are a
9e9b0 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 vailable.. */.
9e9c0 20 75 33 32 20 2a 61 53 63 72 61 74 63 68 46 72 u32 *aScratchFr
9e9d0 65 65 3b 0a 20 20 75 33 32 20 2a 61 50 61 67 65 ee;. u32 *aPage
9e9e0 46 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20 7b Free;.} mem0 = {
9e9f0 20 36 32 35 36 30 39 35 35 2c 20 30 2c 20 30 2c 62560955, 0, 0,
9ea00 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
9ea10 30 20 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 0 };..#define me
9ea20 6d 30 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 m0 GLOBAL(struct
9ea30 20 4d 65 6d 30 47 6c 6f 62 61 6c 2c 20 6d 65 6d Mem0Global, mem
9ea40 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 0)../*.** Initia
9ea50 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 lize the memory
9ea60 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
9ea70 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f stem..*/.SQLITE_
9ea80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
9ea90 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f te3MallocInit(vo
9eaa0 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 id){. if( sqlit
9eab0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9eac0 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 7b 0a 20 .xMalloc==0 ){.
9ead0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 sqlite3MemSet
9eae0 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 Default();. }.
9eaf0 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 memset(&mem0, 0
9eb00 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b , sizeof(mem0));
9eb10 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
9eb20 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 obalConfig.bCore
9eb30 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 6d 65 6d Mutex ){. mem
9eb40 30 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 0.mutex = sqlite
9eb50 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
9eb60 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
9eb70 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 MEM);. }. if(
9eb80 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9eb90 66 69 67 2e 70 53 63 72 61 74 63 68 20 26 26 20 fig.pScratch &&
9eba0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9ebb0 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3e 3d 31 fig.szScratch>=1
9ebc0 30 30 0a 20 20 20 20 20 20 26 26 20 73 71 6c 69 00. && sqli
9ebd0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9ebe0 6e 53 63 72 61 74 63 68 3e 3d 30 20 29 7b 0a 20 nScratch>=0 ){.
9ebf0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 int i;. sq
9ec00 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9ec10 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 28 73 g.szScratch = (s
9ec20 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ec30 69 67 2e 73 7a 53 63 72 61 74 63 68 20 2d 20 34 ig.szScratch - 4
9ec40 29 20 26 20 7e 37 3b 0a 20 20 20 20 6d 65 6d 30 ) & ~7;. mem0
9ec50 2e 61 53 63 72 61 74 63 68 46 72 65 65 20 3d 20 .aScratchFree =
9ec60 28 75 33 32 2a 29 26 28 28 63 68 61 72 2a 29 73 (u32*)&((char*)s
9ec70 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ec80 69 67 2e 70 53 63 72 61 74 63 68 29 0a 20 20 20 ig.pScratch).
9ec90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5b [
9eca0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9ecb0 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2a 73 71 fig.szScratch*sq
9ecc0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9ecd0 67 2e 6e 53 63 72 61 74 63 68 5d 3b 0a 20 20 20 g.nScratch];.
9ece0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c 69 for(i=0; i<sqli
9ecf0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9ed00 6e 53 63 72 61 74 63 68 3b 20 69 2b 2b 29 7b 20 nScratch; i++){
9ed10 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 mem0.aScratchFre
9ed20 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 e[i] = i; }.
9ed30 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 mem0.nScratchFre
9ed40 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 e = sqlite3Globa
9ed50 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 lConfig.nScratch
9ed60 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
9ed70 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ed80 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20 30 3b ig.pScratch = 0;
9ed90 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
9eda0 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 alConfig.szScrat
9edb0 63 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 ch = 0;. }. if
9edc0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9edd0 6f 6e 66 69 67 2e 70 50 61 67 65 20 26 26 20 73 onfig.pPage && s
9ede0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9edf0 69 67 2e 73 7a 50 61 67 65 3e 3d 35 31 32 0a 20 ig.szPage>=512.
9ee00 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 47 && sqlite3G
9ee10 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 lobalConfig.nPag
9ee20 65 3e 3d 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 e>=1 ){. int
9ee30 69 3b 0a 20 20 20 20 69 6e 74 20 6f 76 65 72 68 i;. int overh
9ee40 65 61 64 3b 0a 20 20 20 20 69 6e 74 20 73 7a 20 ead;. int sz
9ee50 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
9ee60 6f 6e 66 69 67 2e 73 7a 50 61 67 65 20 26 20 7e onfig.szPage & ~
9ee70 37 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 7;. int n = s
9ee80 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ee90 69 67 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 ig.nPage;. ov
9eea0 65 72 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 erhead = (4*n +
9eeb0 73 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 sz - 1)/sz;.
9eec0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9eed0 66 69 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 fig.nPage -= ove
9eee0 72 68 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e rhead;. mem0.
9eef0 61 50 61 67 65 46 72 65 65 20 3d 20 28 75 33 32 aPageFree = (u32
9ef00 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 *)&((char*)sqlit
9ef10 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
9ef20 50 61 67 65 29 0a 20 20 20 20 20 20 20 20 20 20 Page).
9ef30 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
9ef40 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 GlobalConfig.szP
9ef50 61 67 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 age*sqlite3Globa
9ef60 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a lConfig.nPage];.
9ef70 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 for(i=0; i<s
9ef80 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ef90 69 67 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 ig.nPage; i++){
9efa0 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 mem0.aPageFree[i
9efb0 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d ] = i; }. mem
9efc0 30 2e 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71 0.nPageFree = sq
9efd0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9efe0 67 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 g.nPage;. }else
9eff0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f {. sqlite3Glo
9f000 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 balConfig.pPage
9f010 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = 0;. sqlite3
9f020 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 GlobalConfig.szP
9f030 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 age = 0;. }. r
9f040 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
9f050 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 balConfig.m.xIni
9f060 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 t(sqlite3GlobalC
9f070 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 onfig.m.pAppData
9f080 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e );.}../*.** Dein
9f090 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d itialize the mem
9f0a0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
9f0b0 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c ubsystem..*/.SQL
9f0c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9f0d0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e sqlite3MallocEn
9f0e0 64 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 d(void){. sqlit
9f0f0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9f100 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 .xShutdown(sqlit
9f110 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9f120 2e 70 41 70 70 44 61 74 61 29 3b 0a 20 20 6d 65 .pAppData);. me
9f130 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 mset(&mem0, 0, s
9f140 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a izeof(mem0));.}.
9f150 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
9f160 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f e amount of memo
9f170 72 79 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 ry currently che
9f180 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c cked out..*/.SQL
9f190 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
9f1a0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 int64 sqlite3_me
9f1b0 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b mory_used(void){
9f1c0 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 . int n, mx;.
9f1d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 sqlite3_int64 re
9f1e0 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 s;. sqlite3_sta
9f1f0 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 tus(SQLITE_STATU
9f200 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 S_MEMORY_USED, &
9f210 6e 2c 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 n, &mx, 0);. re
9f220 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 s = (sqlite3_int
9f230 36 34 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 64)n; /* Work a
9f240 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 round bug in Bor
9f250 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 land C. Ticket #
9f260 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3216 */. return
9f270 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 res;.}../*.** R
9f280 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 eturn the maximu
9f290 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f m amount of memo
9f2a0 72 79 20 74 68 61 74 20 68 61 73 20 65 76 65 72 ry that has ever
9f2b0 20 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 been.** checked
9f2c0 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68 65 out since eithe
9f2d0 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 r the beginning
9f2e0 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a of this process.
9f2f0 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20 ** or since the
9f300 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 65 most recent rese
9f310 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
9f320 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
9f330 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 qlite3_memory_hi
9f340 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 ghwater(int rese
9f350 74 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c tFlag){. int n,
9f360 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 mx;. sqlite3_i
9f370 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 nt64 res;. sqli
9f380 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 te3_status(SQLIT
9f390 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
9f3a0 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 USED, &n, &mx, r
9f3b0 65 73 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 esetFlag);. res
9f3c0 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 = (sqlite3_int6
9f3d0 34 29 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 4)mx; /* Work a
9f3e0 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 round bug in Bor
9f3f0 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 land C. Ticket #
9f400 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3216 */. return
9f410 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 res;.}../*.** C
9f420 68 61 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 hange the alarm
9f430 63 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 callback.*/.SQLI
9f440 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
9f450 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 qlite3MemoryAlar
9f460 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c m(. void(*xCall
9f470 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 back)(void *pArg
9f480 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
9f490 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 used,int N),. v
9f4a0 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c oid *pArg,. sql
9f4b0 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 ite3_int64 iThre
9f4c0 73 68 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 shold.){. sqlit
9f4d0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
9f4e0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 em0.mutex);. me
9f4f0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b m0.alarmCallback
9f500 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 = xCallback;.
9f510 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 mem0.alarmArg =
9f520 70 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 pArg;. mem0.ala
9f530 72 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 rmThreshold = iT
9f540 68 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 hreshold;. sqli
9f550 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
9f560 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 mem0.mutex);. r
9f570 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
9f580 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
9f590 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 TE_OMIT_DEPRECAT
9f5a0 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 ED./*.** Depreca
9f5b0 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 ted external int
9f5c0 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 erface. Interna
9f5d0 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f l/core SQLite co
9f5e0 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c de.** should cal
9f5f0 6c 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 l sqlite3MemoryA
9f600 6c 61 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f larm..*/.SQLITE_
9f610 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
9f620 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 memory_alarm(.
9f630 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 void(*xCallback)
9f640 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c (void *pArg, sql
9f650 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c ite3_int64 used,
9f660 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a int N),. void *
9f670 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f pArg,. sqlite3_
9f680 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 int64 iThreshold
9f690 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c .){. return sql
9f6a0 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 ite3MemoryAlarm(
9f6b0 78 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c xCallback, pArg,
9f6c0 20 69 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a iThreshold);.}.
9f6d0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 #endif../*.** Tr
9f6e0 69 67 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20 igger the alarm
9f6f0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
9f700 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 sqlite3MallocAla
9f710 72 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 rm(int nByte){.
9f720 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 void (*xCallbac
9f730 6b 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 k)(void*,sqlite3
9f740 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 _int64,int);. s
9f750 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 qlite3_int64 now
9f760 55 73 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 Used;. void *pA
9f770 72 67 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 rg;. if( mem0.a
9f780 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 larmCallback==0
9f790 7c 7c 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 || mem0.alarmBus
9f7a0 79 20 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d y ) return;. m
9f7b0 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 em0.alarmBusy =
9f7c0 31 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 1;. xCallback =
9f7d0 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 mem0.alarmCallb
9f7e0 61 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d ack;. nowUsed =
9f7f0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 sqlite3StatusVa
9f800 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 lue(SQLITE_STATU
9f810 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a S_MEMORY_USED);.
9f820 20 20 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c pArg = mem0.al
9f830 61 72 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 armArg;. sqlite
9f840 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9f850 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 m0.mutex);. xCa
9f860 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 llback(pArg, now
9f870 55 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 Used, nByte);.
9f880 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
9f890 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ter(mem0.mutex);
9f8a0 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 . mem0.alarmBus
9f8b0 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 y = 0;.}../*.**
9f8c0 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f Do a memory allo
9f8d0 63 61 74 69 6f 6e 20 77 69 74 68 20 73 74 61 74 cation with stat
9f8e0 69 73 74 69 63 73 20 61 6e 64 20 61 6c 61 72 6d istics and alarm
9f8f0 73 2e 20 20 41 73 73 75 6d 65 20 74 68 65 0a 2a s. Assume the.*
9f900 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 * lock is alread
9f910 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 y held..*/.stati
9f920 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 68 c int mallocWith
9f930 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f 69 Alarm(int n, voi
9f940 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 6e d **pp){. int n
9f950 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 3b Full;. void *p;
9f960 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
9f970 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
9f980 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e m0.mutex) );. n
9f990 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 6c Full = sqlite3Gl
9f9a0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f obalConfig.m.xRo
9f9b0 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c 69 undup(n);. sqli
9f9c0 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c te3StatusSet(SQL
9f9d0 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f ITE_STATUS_MALLO
9f9e0 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 66 C_SIZE, n);. if
9f9f0 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c ( mem0.alarmCall
9fa00 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 69 back!=0 ){. i
9fa10 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 nt nUsed = sqlit
9fa20 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 e3StatusValue(SQ
9fa30 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f LITE_STATUS_MEMO
9fa40 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 66 RY_USED);. if
9fa50 28 20 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e 3d ( nUsed+nFull >=
9fa60 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 mem0.alarmThres
9fa70 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 71 hold ){. sq
9fa80 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d lite3MallocAlarm
9fa90 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 (nFull);. }.
9faa0 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 }. p = sqlite3
9fab0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9fac0 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 Malloc(nFull);.
9fad0 20 69 66 28 20 70 3d 3d 30 20 26 26 20 6d 65 6d if( p==0 && mem
9fae0 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 0.alarmCallback
9faf0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 ){. sqlite3Ma
9fb00 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 llocAlarm(nFull)
9fb10 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 ;. p = sqlite
9fb20 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9fb30 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a xMalloc(nFull);.
9fb40 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 }. if( p ){.
9fb50 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 nFull = sqlit
9fb60 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b e3MallocSize(p);
9fb70 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 . sqlite3Stat
9fb80 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
9fb90 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c TUS_MEMORY_USED,
9fba0 20 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 2a nFull);. }. *
9fbb0 70 70 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e pp = p;. return
9fbc0 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a nFull;.}../*.**
9fbd0 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 Allocate memory
9fbe0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
9fbf0 69 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f is like sqlite3_
9fc00 6d 61 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 20 malloc() except
9fc10 74 68 61 74 20 69 74 0a 2a 2a 20 61 73 73 75 6d that it.** assum
9fc20 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 es the memory su
9fc30 62 73 79 73 74 65 6d 20 68 61 73 20 61 6c 72 65 bsystem has alre
9fc40 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c ady been initial
9fc50 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ized..*/.SQLITE_
9fc60 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
9fc70 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 lite3Malloc(int
9fc80 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 n){. void *p;.
9fc90 20 69 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20 if( n<=0 ){.
9fca0 20 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 p = 0;. }else
9fcb0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9fcc0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
9fcd0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
9fce0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
9fcf0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c .mutex);. mal
9fd00 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 locWithAlarm(n,
9fd10 26 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 &p);. sqlite3
9fd20 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
9fd30 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 0.mutex);. }els
9fd40 65 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 e{. p = sqlit
9fd50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9fd60 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d .xMalloc(n);. }
9fd70 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
9fd80 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 /*.** This versi
9fd90 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
9fda0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 allocation is f
9fdb0 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 61 70 or use by the ap
9fdc0 70 6c 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 plication..** Fi
9fdd0 72 73 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 rst make sure th
9fde0 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 e memory subsyst
9fdf0 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 em is initialize
9fe00 64 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a d, then do the.*
9fe10 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f * allocation..*/
9fe20 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
9fe30 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 *sqlite3_malloc
9fe40 28 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 (int n){.#ifndef
9fe50 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
9fe60 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 OINIT. if( sqli
9fe70 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
9fe80 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e ) return 0;.#en
9fe90 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c dif. return sql
9fea0 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d ite3Malloc(n);.}
9feb0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72 ../*.** Each thr
9fec0 65 61 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 ead may only hav
9fed0 65 20 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74 e a single outst
9fee0 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f anding allocatio
9fef0 6e 20 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 n from.** xScrat
9ff00 63 68 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20 chMalloc(). We
9ff10 76 65 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73 verify this cons
9ff20 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69 traint in the si
9ff30 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a ngle-threaded.**
9ff40 20 63 61 73 65 20 62 79 20 73 65 74 74 69 6e 67 case by setting
9ff50 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 scratchAllocOut
9ff60 20 74 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c to 1 when an al
9ff70 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f location.** is o
9ff80 75 74 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72 utstanding clear
9ff90 69 6e 67 20 69 74 20 77 68 65 6e 20 74 68 65 20 ing it when the
9ffa0 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 allocation is fr
9ffb0 65 65 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 eed..*/.#if SQLI
9ffc0 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 TE_THREADSAFE==0
9ffd0 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 && !defined(NDE
9ffe0 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 BUG).static int
9fff0 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 scratchAllocOut
a0000 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a = 0;.#endif.../*
a0010 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d .** Allocate mem
a0020 6f 72 79 20 74 68 61 74 20 69 73 20 74 6f 20 62 ory that is to b
a0030 65 20 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61 e used and relea
a0040 73 65 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a sed right away..
a0050 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
a0060 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c is similar to al
a0070 6c 6f 63 61 28 29 20 69 6e 20 74 68 61 74 20 69 loca() in that i
a0080 74 20 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65 t is not intende
a0090 64 0a 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69 d.** for situati
a00a0 6f 6e 73 20 77 68 65 72 65 20 74 68 65 20 6d 65 ons where the me
a00b0 6d 6f 72 79 20 6d 69 67 68 74 20 62 65 20 68 65 mory might be he
a00c0 6c 64 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 ld long-term. T
a00d0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 his.** routine i
a00e0 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65 s intended to ge
a00f0 74 20 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 t memory to old
a0100 6c 61 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20 large transient
a0110 64 61 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 data.** structur
a0120 65 73 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f es that would no
a0130 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f t normally fit o
a0140 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61 n the stack of a
a0150 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72 n.** embedded pr
a0160 6f 63 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 ocessor..*/.SQLI
a0170 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a0180 2a 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d *sqlite3ScratchM
a0190 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 alloc(int n){.
a01a0 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 void *p;. asser
a01b0 74 28 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53 t( n>0 );..#if S
a01c0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
a01d0 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 ==0 && !defined(
a01e0 4e 44 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 NDEBUG). /* Ver
a01f0 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 ify that no more
a0200 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 than one scratc
a0210 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 h allocation per
a0220 20 74 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20 thread. ** is
a0230 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f outstanding at o
a0240 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 ne time. (This
a0250 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 is only checked
a0260 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67 in the. ** sing
a0270 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 le-threaded case
a0280 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 since checking
a0290 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 in the multi-thr
a02a0 65 61 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20 eaded case. **
a02b0 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f would be much mo
a02c0 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 re complicated.)
a02d0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63 */. assert( sc
a02e0 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 ratchAllocOut==0
a02f0 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 );.#endif.. if
a0300 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
a0310 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c onfig.szScratch<
a0320 6e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63 n ){. goto sc
a0330 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a ratch_overflow;.
a0340 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 }else{ . s
a0350 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
a0360 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a er(mem0.mutex);.
a0370 20 20 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63 if( mem0.nSc
a0380 72 61 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a ratchFree==0 ){.
a0390 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
a03a0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d tex_leave(mem0.m
a03b0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 utex);. got
a03c0 6f 20 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c o scratch_overfl
a03d0 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ow;. }else{.
a03e0 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
a03f0 20 20 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61 i = mem0.aScra
a0400 74 63 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e tchFree[--mem0.n
a0410 53 63 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20 ScratchFree];.
a0420 20 20 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 i *= sqlite3
a0430 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 GlobalConfig.szS
a0440 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71 cratch;. sq
a0450 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
a0460 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 QLITE_STATUS_SCR
a0470 41 54 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20 ATCH_USED, 1);.
a0480 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
a0490 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 usSet(SQLITE_STA
a04a0 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 TUS_SCRATCH_SIZE
a04b0 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , n);. sqli
a04c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
a04d0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem0.mutex);.
a04e0 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28 p = (void*)&(
a04f0 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c (char*)sqlite3Gl
a0500 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 obalConfig.pScra
a0510 74 63 68 29 5b 69 5d 3b 0a 20 20 20 20 20 20 61 tch)[i];. a
a0520 73 73 65 72 74 28 20 20 28 28 28 75 38 2a 29 70 ssert( (((u8*)p
a0530 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d - (u8*)0) & 7)=
a0540 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a =0 );. }. }.
a0550 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
a0560 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 DSAFE==0 && !def
a0570 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73 ined(NDEBUG). s
a0580 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d cratchAllocOut =
a0590 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 p!=0;.#endif..
a05a0 20 72 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72 61 return p;..scra
a05b0 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 tch_overflow:.
a05c0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
a05d0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
a05e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
a05f0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
a0600 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c .mutex);. sql
a0610 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 ite3StatusSet(SQ
a0620 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
a0630 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 TCH_SIZE, n);.
a0640 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74 68 n = mallocWith
a0650 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 Alarm(n, &p);.
a0660 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 74 65 if( p ) sqlite
a0670 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
a0680 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
a0690 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 _OVERFLOW, n);.
a06a0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
a06b0 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 _leave(mem0.mute
a06c0 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 x);. }else{.
a06d0 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 p = sqlite3Glob
a06e0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c alConfig.m.xMall
a06f0 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20 53 oc(n);. }.#if S
a0700 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
a0710 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 ==0 && !defined(
a0720 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 63 NDEBUG). scratc
a0730 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 hAllocOut = p!=0
a0740 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 ;.#endif. retur
a0750 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 54 n p; .}.SQLIT
a0760 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a0770 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65 qlite3ScratchFre
a0780 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 e(void *p){. if
a0790 28 20 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c 49 ( p ){..#if SQLI
a07a0 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 TE_THREADSAFE==0
a07b0 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 && !defined(NDE
a07c0 42 55 47 29 0a 20 20 20 20 2f 2a 20 56 65 72 69 BUG). /* Veri
a07d0 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 fy that no more
a07e0 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 than one scratch
a07f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 allocation per
a0800 74 68 72 65 61 64 0a 20 20 20 20 2a 2a 20 69 73 thread. ** is
a0810 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 outstanding at
a0820 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 one time. (This
a0830 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 is only checked
a0840 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 in the. ** s
a0850 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63 ingle-threaded c
a0860 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69 ase since checki
a0870 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d ng in the multi-
a0880 74 68 72 65 61 64 65 64 20 63 61 73 65 0a 20 20 threaded case.
a0890 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 ** would be mu
a08a0 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 ch more complica
a08b0 74 65 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73 73 ted.) */. ass
a08c0 65 72 74 28 20 73 63 72 61 74 63 68 41 6c 6c 6f ert( scratchAllo
a08d0 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20 73 cOut==1 );. s
a08e0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d cratchAllocOut =
a08f0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 0;.#endif..
a0900 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
a0910 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 lConfig.pScratch
a0920 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c ==0. |
a0930 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 | p<sqlite3Globa
a0940 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 lConfig.pScratch
a0950 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 . || p
a0960 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 53 >=(void*)mem0.aS
a0970 63 72 61 74 63 68 46 72 65 65 20 29 7b 0a 20 20 cratchFree ){.
a0980 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 if( sqlite3G
a0990 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
a09a0 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20 20 20 stat ){.
a09b0 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 int iSize = sqli
a09c0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 te3MallocSize(p)
a09d0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
a09e0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
a09f0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
a0a00 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
a0a10 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
a0a20 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c S_SCRATCH_OVERFL
a0a30 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 OW, -iSize);.
a0a40 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
a0a50 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
a0a60 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c TUS_MEMORY_USED,
a0a70 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 -iSize);.
a0a80 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
a0a90 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 onfig.m.xFree(p)
a0aa0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
a0ab0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
a0ac0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
a0ad0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a0ae0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
a0af0 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a fig.m.xFree(p);.
a0b00 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
a0b10 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a e{. int i;.
a0b20 20 20 20 20 20 20 69 20 3d 20 28 69 6e 74 29 28 i = (int)(
a0b30 28 75 38 2a 29 70 20 2d 20 28 75 38 2a 29 73 71 (u8*)p - (u8*)sq
a0b40 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
a0b50 67 2e 70 53 63 72 61 74 63 68 29 3b 0a 20 20 20 g.pScratch);.
a0b60 20 20 20 69 20 2f 3d 20 73 71 6c 69 74 65 33 47 i /= sqlite3G
a0b70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 lobalConfig.szSc
a0b80 72 61 74 63 68 3b 0a 20 20 20 20 20 20 61 73 73 ratch;. ass
a0b90 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 73 ert( i>=0 && i<s
a0ba0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
a0bb0 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20 ig.nScratch );.
a0bc0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
a0bd0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 ex_enter(mem0.mu
a0be0 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 tex);. asse
a0bf0 72 74 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 rt( mem0.nScratc
a0c00 68 46 72 65 65 3c 28 75 33 32 29 73 71 6c 69 74 hFree<(u32)sqlit
a0c10 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
a0c20 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 Scratch );.
a0c30 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 mem0.aScratchFr
a0c40 65 65 5b 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 ee[mem0.nScratch
a0c50 46 72 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 Free++] = i;.
a0c60 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
a0c70 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
a0c80 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 S_SCRATCH_USED,
a0c90 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 -1);. sqlit
a0ca0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
a0cb0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
a0cc0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 }. }.}../*.** A
a0cd0 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 llocate memory t
a0ce0 6f 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65 o be used by the
a0cf0 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 4d 61 page cache. Ma
a0d00 6b 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a ke use of the.**
a0d10 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 70 memory buffer p
a0d20 72 6f 76 69 64 65 64 20 62 79 20 53 51 4c 49 54 rovided by SQLIT
a0d30 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
a0d40 48 45 20 69 66 20 74 68 65 72 65 20 69 73 20 6f HE if there is o
a0d50 6e 65 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 6d ne.** and that m
a0d60 65 6d 6f 72 79 20 69 73 20 6f 66 20 74 68 65 20 emory is of the
a0d70 72 69 67 68 74 20 73 69 7a 65 20 61 6e 64 20 69 right size and i
a0d80 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 s not completely
a0d90 0a 2a 2a 20 63 6f 6e 73 75 6d 65 64 2e 20 20 4f .** consumed. O
a0da0 74 68 65 72 77 69 73 65 2c 20 66 61 69 6c 6f 76 therwise, failov
a0db0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c er to sqlite3Mal
a0dc0 6c 6f 63 28 29 2e 0a 2a 2f 0a 23 69 66 20 30 0a loc()..*/.#if 0.
a0dd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
a0de0 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 oid *sqlite3Page
a0df0 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 Malloc(int n){.
a0e00 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 void *p;. asse
a0e10 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 rt( n>0 );. ass
a0e20 65 72 74 28 20 28 6e 20 26 20 28 6e 2d 31 29 29 ert( (n & (n-1))
a0e30 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
a0e40 20 6e 3e 3d 35 31 32 20 26 26 20 6e 3c 3d 33 32 n>=512 && n<=32
a0e50 37 36 38 20 29 3b 0a 0a 20 20 69 66 28 20 73 71 768 );.. if( sq
a0e60 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
a0e70 67 2e 73 7a 50 61 67 65 3c 6e 20 29 7b 0a 20 20 g.szPage<n ){.
a0e80 20 20 67 6f 74 6f 20 70 61 67 65 5f 6f 76 65 72 goto page_over
a0e90 66 6c 6f 77 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 flow;. }else{
a0ea0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
a0eb0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 ex_enter(mem0.mu
a0ec0 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 6d 65 tex);. if( me
a0ed0 6d 30 2e 6e 50 61 67 65 46 72 65 65 3d 3d 30 20 m0.nPageFree==0
a0ee0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
a0ef0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
a0f00 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 0.mutex);.
a0f10 67 6f 74 6f 20 70 61 67 65 5f 6f 76 65 72 66 6c goto page_overfl
a0f20 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ow;. }else{.
a0f30 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
a0f40 20 20 69 20 3d 20 6d 65 6d 30 2e 61 50 61 67 65 i = mem0.aPage
a0f50 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 50 61 67 Free[--mem0.nPag
a0f60 65 46 72 65 65 5d 3b 0a 20 20 20 20 20 20 73 71 eFree];. sq
a0f70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
a0f80 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 e(mem0.mutex);.
a0f90 20 20 20 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 i *= sqlite
a0fa0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 3GlobalConfig.sz
a0fb0 50 61 67 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 Page;. sqli
a0fc0 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c te3StatusSet(SQL
a0fd0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
a0fe0 41 43 48 45 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 ACHE_SIZE, n);.
a0ff0 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
a1000 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
a1010 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 TUS_PAGECACHE_US
a1020 45 44 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 20 ED, 1);. p
a1030 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 = (void*)&((char
a1040 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 *)sqlite3GlobalC
a1050 6f 6e 66 69 67 2e 70 50 61 67 65 29 5b 69 5d 3b onfig.pPage)[i];
a1060 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
a1070 75 72 6e 20 70 3b 0a 0a 70 61 67 65 5f 6f 76 65 urn p;..page_ove
a1080 72 66 6c 6f 77 3a 0a 20 20 69 66 28 20 73 71 6c rflow:. if( sql
a1090 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
a10a0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 .bMemstat ){.
a10b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
a10c0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
a10d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
a10e0 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 tusSet(SQLITE_ST
a10f0 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53 ATUS_PAGECACHE_S
a1100 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 6e 20 3d IZE, n);. n =
a1110 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d mallocWithAlarm
a1120 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 69 66 28 (n, &p);. if(
a1130 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 74 p ) sqlite3Stat
a1140 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
a1150 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 TUS_PAGECACHE_OV
a1160 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 ERFLOW, n);.
a1170 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
a1180 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ave(mem0.mutex);
a1190 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 . }else{. p
a11a0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
a11b0 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 onfig.m.xMalloc(
a11c0 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e n);. }. return
a11d0 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 54 45 p; .}.SQLITE
a11e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
a11f0 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76 6f lite3PageFree(vo
a1200 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 id *p){. if( p
a1210 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
a1220 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
a1230 50 61 67 65 3d 3d 30 0a 20 20 20 20 20 20 20 20 Page==0.
a1240 20 20 20 7c 7c 20 70 3c 73 71 6c 69 74 65 33 47 || p<sqlite3G
a1250 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 lobalConfig.pPag
a1260 65 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 e. ||
a1270 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 p>=(void*)mem0.a
a1280 50 61 67 65 46 72 65 65 20 29 7b 0a 20 20 20 20 PageFree ){.
a1290 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 /* In this cas
a12a0 65 2c 20 74 68 65 20 70 61 67 65 20 61 6c 6c 6f e, the page allo
a12b0 63 61 74 69 6f 6e 20 77 61 73 20 6f 62 74 61 69 cation was obtai
a12c0 6e 65 64 20 66 72 6f 6d 20 61 20 72 65 67 75 6c ned from a regul
a12d0 61 72 20 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c ar . ** cal
a12e0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 65 6d l to sqlite3_mem
a12f0 5f 6d 65 74 68 6f 64 73 2e 78 4d 61 6c 6c 6f 63 _methods.xMalloc
a1300 28 29 20 28 61 20 70 61 67 65 2d 63 61 63 68 65 () (a page-cache
a1310 2d 6d 65 6d 6f 72 79 20 0a 20 20 20 20 20 20 2a -memory . *
a1320 2a 20 22 6f 76 65 72 66 6c 6f 77 22 29 2e 20 46 * "overflow"). F
a1330 72 65 65 20 74 68 65 20 62 6c 6f 63 6b 20 77 69 ree the block wi
a1340 74 68 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d th sqlite3_mem_m
a1350 65 74 68 6f 64 73 2e 78 46 72 65 65 28 29 2e 0a ethods.xFree()..
a1360 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
a1370 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
a1380 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 Config.bMemstat
a1390 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 ){. int i
a13a0 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 Size = sqlite3Ma
a13b0 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 llocSize(p);.
a13c0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
a13d0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 ex_enter(mem0.mu
a13e0 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 73 71 tex);. sq
a13f0 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
a1400 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 QLITE_STATUS_PAG
a1410 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c ECACHE_OVERFLOW,
a1420 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 -iSize);.
a1430 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 sqlite3StatusA
a1440 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dd(SQLITE_STATUS
a1450 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69 _MEMORY_USED, -i
a1460 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 Size);. s
a1470 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
a1480 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 ig.m.xFree(p);.
a1490 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d sqlite3_m
a14a0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
a14b0 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 65 mutex);. }e
a14c0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
a14d0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
a14e0 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 .m.xFree(p);.
a14f0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
a1500 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
a1510 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 e allocation was
a1520 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 6f 6d 20 allocated from
a1530 74 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 the sqlite3Globa
a1540 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 0a 20 20 lConfig.pPage.
a1550 20 20 20 20 2a 2a 20 62 75 66 66 65 72 2e 20 49 ** buffer. I
a1560 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20 n this case all
a1570 74 68 61 74 20 69 73 20 61 64 64 20 74 68 65 20 that is add the
a1580 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 67 index of the pag
a1590 65 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 e in. ** th
a15a0 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 e sqlite3GlobalC
a15b0 6f 6e 66 69 67 2e 70 50 61 67 65 20 61 72 72 61 onfig.pPage arra
a15c0 79 20 74 6f 20 74 68 65 20 73 65 74 20 6f 66 20 y to the set of
a15d0 66 72 65 65 20 69 6e 64 65 78 65 73 20 73 74 6f free indexes sto
a15e0 72 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 red. ** in
a15f0 74 68 65 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 the mem0.aPageFr
a1600 65 65 5b 5d 20 61 72 72 61 79 2e 0a 20 20 20 20 ee[] array..
a1610 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 */. int i
a1620 3b 0a 20 20 20 20 20 20 69 20 3d 20 28 75 38 20 ;. i = (u8
a1630 2a 29 70 20 2d 20 28 75 38 20 2a 29 73 71 6c 69 *)p - (u8 *)sqli
a1640 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
a1650 70 50 61 67 65 3b 0a 20 20 20 20 20 20 69 20 2f pPage;. i /
a1660 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
a1670 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3b 0a 20 20 onfig.szPage;.
a1680 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 assert( i>=0
a1690 20 26 26 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f && i<sqlite3Glo
a16a0 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 balConfig.nPage
a16b0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
a16c0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
a16d0 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 0.mutex);.
a16e0 61 73 73 65 72 74 28 20 6d 65 6d 30 2e 6e 50 61 assert( mem0.nPa
a16f0 67 65 46 72 65 65 3c 73 71 6c 69 74 65 33 47 6c geFree<sqlite3Gl
a1700 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 obalConfig.nPage
a1710 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e 61 );. mem0.a
a1720 50 61 67 65 46 72 65 65 5b 6d 65 6d 30 2e 6e 50 PageFree[mem0.nP
a1730 61 67 65 46 72 65 65 2b 2b 5d 20 3d 20 69 3b 0a ageFree++] = i;.
a1740 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 sqlite3Sta
a1750 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
a1760 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 ATUS_PAGECACHE_U
a1770 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 SED, -1);.
a1780 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
a1790 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ave(mem0.mutex);
a17a0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
a17b0 45 42 55 47 29 20 26 26 20 30 0a 20 20 20 20 20 EBUG) && 0.
a17c0 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 /* Assert that
a17d0 61 20 64 75 70 6c 69 63 61 74 65 20 77 61 73 20 a duplicate was
a17e0 6e 6f 74 20 6a 75 73 74 20 69 6e 73 65 72 74 65 not just inserte
a17f0 64 20 69 6e 74 6f 20 61 50 61 67 65 46 72 65 65 d into aPageFree
a1800 5b 5d 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 []. */. for
a1810 28 69 3d 30 3b 20 69 3c 6d 65 6d 30 2e 6e 50 61 (i=0; i<mem0.nPa
a1820 67 65 46 72 65 65 2d 31 3b 20 69 2b 2b 29 7b 0a geFree-1; i++){.
a1830 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
a1840 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 mem0.aPageFree[i
a1850 5d 21 3d 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 ]!=mem0.aPageFre
a1860 65 5b 6d 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 e[mem0.nPageFree
a1870 2d 31 5d 20 29 3b 0a 20 20 20 20 20 20 7d 0a 23 -1] );. }.#
a1880 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 0a endif. }. }.
a1890 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
a18a0 54 52 55 45 20 69 66 20 70 20 69 73 20 61 20 6c TRUE if p is a l
a18b0 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 ookaside memory
a18c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 20 allocation from
a18d0 64 62 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 db.*/.#ifndef SQ
a18e0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 LITE_OMIT_LOOKAS
a18f0 49 44 45 0a 73 74 61 74 69 63 20 69 6e 74 20 69 IDE.static int i
a1900 73 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c 69 74 sLookaside(sqlit
a1910 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 e3 *db, void *p)
a1920 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 20 26 26 {. return db &&
a1930 20 70 20 26 26 20 70 3e 3d 64 62 2d 3e 6c 6f 6f p && p>=db->loo
a1940 6b 61 73 69 64 65 2e 70 53 74 61 72 74 20 26 26 kaside.pStart &&
a1950 20 70 3c 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 p<db->lookaside
a1960 2e 70 45 6e 64 3b 0a 7d 0a 23 65 6c 73 65 0a 23 .pEnd;.}.#else.#
a1970 64 65 66 69 6e 65 20 69 73 4c 6f 6f 6b 61 73 69 define isLookasi
a1980 64 65 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 de(A,B) 0.#endif
a1990 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
a19a0 68 65 20 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d he size of a mem
a19b0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 ory allocation p
a19c0 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e reviously obtain
a19d0 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 ed from.** sqlit
a19e0 65 33 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 e3Malloc() or sq
a19f0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a lite3_malloc()..
a1a00 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
a1a10 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c E int sqlite3Mal
a1a20 6c 6f 63 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 locSize(void *p)
a1a30 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
a1a40 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
a1a50 2e 78 53 69 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c .xSize(p);.}.SQL
a1a60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
a1a70 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 sqlite3DbMallocS
a1a80 69 7a 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ize(sqlite3 *db,
a1a90 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 void *p){. if(
a1aa0 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 p==0 ){. ret
a1ab0 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 urn 0;. }else i
a1ac0 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 f( isLookaside(d
a1ad0 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 74 b, p) ){. ret
a1ae0 75 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 urn db->lookasid
a1af0 65 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 e.sz;. }else{.
a1b00 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 return sqlite
a1b10 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
a1b20 78 53 69 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a xSize(p);. }.}.
a1b30 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f ./*.** Free memo
a1b40 72 79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 ry previously ob
a1b50 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
a1b60 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a te3Malloc()..*/.
a1b70 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
a1b80 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 sqlite3_free(voi
a1b90 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d d *p){. if( p==
a1ba0 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 0 ) return;. if
a1bb0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
a1bc0 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 onfig.bMemstat )
a1bd0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
a1be0 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
a1bf0 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 utex);. sqlit
a1c00 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 e3StatusAdd(SQLI
a1c10 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 TE_STATUS_MEMORY
a1c20 5f 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33 4d _USED, -sqlite3M
a1c30 61 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0a 20 allocSize(p));.
a1c40 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
a1c50 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 Config.m.xFree(p
a1c60 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
a1c70 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
a1c80 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b mutex);. }else{
a1c90 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
a1ca0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 alConfig.m.xFree
a1cb0 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
a1cc0 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74 68 * Free memory th
a1cd0 61 74 20 6d 69 67 68 74 20 62 65 20 61 73 73 6f at might be asso
a1ce0 63 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61 ciated with a pa
a1cf0 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 rticular databas
a1d00 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e e.** connection.
a1d10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a1d20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
a1d30 62 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 bFree(sqlite3 *d
a1d40 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 b, void *p){. i
a1d50 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 f( isLookaside(d
a1d60 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 4c 6f 6f b, p) ){. Loo
a1d70 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 kasideSlot *pBuf
a1d80 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f = (LookasideSlo
a1d90 74 2a 29 70 3b 0a 20 20 20 20 70 42 75 66 2d 3e t*)p;. pBuf->
a1da0 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b pNext = db->look
a1db0 61 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20 aside.pFree;.
a1dc0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 db->lookaside.p
a1dd0 46 72 65 65 20 3d 20 70 42 75 66 3b 0a 20 20 20 Free = pBuf;.
a1de0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e db->lookaside.n
a1df0 4f 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 65 7b 0a Out--;. }else{.
a1e00 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
a1e10 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
a1e20 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a * Change the siz
a1e30 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 e of an existing
a1e40 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
a1e50 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 on.*/.SQLITE_PRI
a1e60 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
a1e70 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a e3Realloc(void *
a1e80 70 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65 73 pOld, int nBytes
a1e90 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20 6e ){. int nOld, n
a1ea0 4e 65 77 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 New;. void *pNe
a1eb0 77 3b 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d 30 w;. if( pOld==0
a1ec0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 ){. return s
a1ed0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 qlite3Malloc(nBy
a1ee0 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 tes);. }. if(
a1ef0 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 nBytes<=0 ){.
a1f00 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f sqlite3_free(pO
a1f10 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ld);. return
a1f20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 0;. }. nOld =
a1f30 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a sqlite3MallocSiz
a1f40 65 28 70 4f 6c 64 29 3b 0a 20 20 69 66 28 20 73 e(pOld);. if( s
a1f50 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
a1f60 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 ig.bMemstat ){.
a1f70 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
a1f80 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 _enter(mem0.mute
a1f90 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 x);. sqlite3S
a1fa0 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f tatusSet(SQLITE_
a1fb0 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 STATUS_MALLOC_SI
a1fc0 5a 45 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 ZE, nBytes);.
a1fd0 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 nNew = sqlite3G
a1fe0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 lobalConfig.m.xR
a1ff0 6f 75 6e 64 75 70 28 6e 42 79 74 65 73 29 3b 0a oundup(nBytes);.
a2000 20 20 20 20 69 66 28 20 6e 4f 6c 64 3d 3d 6e 4e if( nOld==nN
a2010 65 77 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77 ew ){. pNew
a2020 20 3d 20 70 4f 6c 64 3b 0a 20 20 20 20 7d 65 6c = pOld;. }el
a2030 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 se{. if( sq
a2040 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 lite3StatusValue
a2050 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
a2060 45 4d 4f 52 59 5f 55 53 45 44 29 2b 6e 4e 65 77 EMORY_USED)+nNew
a2070 2d 6e 4f 6c 64 20 3e 3d 20 0a 20 20 20 20 20 20 -nOld >= .
a2080 20 20 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d mem0.alarm
a2090 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 Threshold ){.
a20a0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c sqlite3Mall
a20b0 6f 63 41 6c 61 72 6d 28 6e 4e 65 77 2d 6e 4f 6c ocAlarm(nNew-nOl
a20c0 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 d);. }.
a20d0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
a20e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
a20f0 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e Realloc(pOld, nN
a2100 65 77 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ew);. if( p
a2110 4e 65 77 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 New==0 && mem0.a
a2120 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a larmCallback ){.
a2130 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d sqlite3M
a2140 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 42 79 74 65 allocAlarm(nByte
a2150 73 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 s);. pNew
a2160 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
a2170 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f Config.m.xReallo
a2180 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 c(pOld, nNew);.
a2190 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
a21a0 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 pNew ){.
a21b0 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d nNew = sqlite3M
a21c0 61 6c 6c 6f 63 53 69 7a 65 28 70 4e 65 77 29 3b allocSize(pNew);
a21d0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
a21e0 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 StatusAdd(SQLITE
a21f0 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 _STATUS_MEMORY_U
a2200 53 45 44 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29 3b SED, nNew-nOld);
a2210 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
a2220 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
a2230 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 _leave(mem0.mute
a2240 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 x);. }else{.
a2250 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 pNew = sqlite3G
a2260 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 lobalConfig.m.xR
a2270 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 42 79 ealloc(pOld, nBy
a2280 74 65 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tes);. }. retu
a2290 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pNew;.}../*.*
a22a0 2a 20 54 68 65 20 70 75 62 6c 69 63 20 69 6e 74 * The public int
a22b0 65 72 66 61 63 65 20 74 6f 20 73 71 6c 69 74 65 erface to sqlite
a22c0 33 52 65 61 6c 6c 6f 63 2e 20 20 4d 61 6b 65 20 3Realloc. Make
a22d0 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 65 sure that the me
a22e0 6d 6f 72 79 0a 2a 2a 20 73 75 62 73 79 73 74 65 mory.** subsyste
a22f0 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 m is initialized
a2300 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 prior to invoki
a2310 6e 67 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63 ng sqliteRealloc
a2320 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
a2330 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 void *sqlite3_re
a2340 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64 alloc(void *pOld
a2350 2c 20 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 , int n){.#ifnde
a2360 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
a2370 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c TOINIT. if( sql
a2380 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
a2390 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 ) ) return 0;.#e
a23a0 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 ndif. return sq
a23b0 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 70 4f 6c lite3Realloc(pOl
a23c0 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a d, n);.}.../*.**
a23d0 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 Allocate and ze
a23e0 72 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 20 0a 53 ro memory..*/ .S
a23f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
a2400 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f id *sqlite3Mallo
a2410 63 5a 65 72 6f 28 69 6e 74 20 6e 29 7b 0a 20 20 cZero(int n){.
a2420 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 void *p = sqlite
a2430 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 3Malloc(n);. if
a2440 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 ( p ){. memse
a2450 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a t(p, 0, n);. }.
a2460 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
a2470 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e *.** Allocate an
a2480 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 d zero memory.
a2490 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f If the allocatio
a24a0 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a n fails, make.**
a24b0 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 the mallocFaile
a24c0 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f d flag in the co
a24d0 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 nnection pointer
a24e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
a24f0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
a2500 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 3DbMallocZero(sq
a2510 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e lite3 *db, int n
a2520 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 ){. void *p = s
a2530 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
a2540 77 28 64 62 2c 20 6e 29 3b 0a 20 20 69 66 28 20 w(db, n);. if(
a2550 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 p ){. memset(
a2560 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 p, 0, n);. }.
a2570 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
a2580 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 ** Allocate and
a2590 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 66 zero memory. If
a25a0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
a25b0 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 fails, make.** t
a25c0 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 he mallocFailed
a25d0 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e flag in the conn
a25e0 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a ection pointer..
a25f0 2a 2a 0a 2a 2a 20 49 66 20 64 62 21 3d 30 20 61 **.** If db!=0 a
a2600 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 nd db->mallocFai
a2610 6c 65 64 20 69 73 20 74 72 75 65 20 28 69 6e 64 led is true (ind
a2620 69 63 61 74 69 6e 67 20 61 20 70 72 69 6f 72 20 icating a prior
a2630 6d 61 6c 6c 6f 63 0a 2a 2a 20 66 61 69 6c 75 72 malloc.** failur
a2640 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 e on the same da
a2650 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
a2660 6e 29 20 74 68 65 6e 20 61 6c 77 61 79 73 20 72 n) then always r
a2670 65 74 75 72 6e 20 30 2e 0a 2a 2a 20 48 65 6e 63 eturn 0..** Henc
a2680 65 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c e for a particul
a2690 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ar database conn
a26a0 65 63 74 69 6f 6e 2c 20 6f 6e 63 65 20 6d 61 6c ection, once mal
a26b0 6c 6f 63 20 73 74 61 72 74 73 0a 2a 2a 20 66 61 loc starts.** fa
a26c0 69 6c 69 6e 67 2c 20 69 74 20 66 61 69 6c 73 20 iling, it fails
a26d0 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 75 6e 74 consistently unt
a26e0 69 6c 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 il mallocFailed
a26f0 69 73 20 72 65 73 65 74 2e 0a 2a 2a 20 54 68 69 is reset..** Thi
a2700 73 20 69 73 20 61 6e 20 69 6d 70 6f 72 74 61 6e s is an importan
a2710 74 20 61 73 73 75 6d 70 74 69 6f 6e 2e 20 20 54 t assumption. T
a2720 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 70 6c here are many pl
a2730 61 63 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 63 aces in the.** c
a2740 6f 64 65 20 74 68 61 74 20 64 6f 20 74 68 69 6e ode that do thin
a2750 67 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a gs like this:.**
a2760 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20 .** int
a2770 2a 61 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 *a = (int*)sqlit
a2780 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
a2790 2c 20 31 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 , 100);.**
a27a0 20 20 20 69 6e 74 20 2a 62 20 3d 20 28 69 6e 74 int *b = (int
a27b0 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *)sqlite3DbMallo
a27c0 63 52 61 77 28 64 62 2c 20 32 30 30 29 3b 0a 2a cRaw(db, 200);.*
a27d0 2a 20 20 20 20 20 20 20 20 20 69 66 28 20 62 20 * if( b
a27e0 29 20 61 5b 31 30 5d 20 3d 20 39 3b 0a 2a 2a 0a ) a[10] = 9;.**.
a27f0 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 ** In other word
a2800 73 2c 20 69 66 20 61 20 73 75 62 73 65 71 75 65 s, if a subseque
a2810 6e 74 20 6d 61 6c 6c 6f 63 20 28 65 78 3a 20 22 nt malloc (ex: "
a2820 62 22 29 20 77 6f 72 6b 65 64 2c 20 69 74 20 69 b") worked, it i
a2830 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68 61 s assumed.** tha
a2840 74 20 61 6c 6c 20 70 72 69 6f 72 20 6d 61 6c 6c t all prior mall
a2850 6f 63 73 20 28 65 78 3a 20 22 61 22 29 20 77 6f ocs (ex: "a") wo
a2860 72 6b 65 64 20 74 6f 6f 2e 0a 2a 2f 0a 53 51 4c rked too..*/.SQL
a2870 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a2880 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *sqlite3DbMallo
a2890 63 52 61 77 28 73 71 6c 69 74 65 33 20 2a 64 62 cRaw(sqlite3 *db
a28a0 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 , int n){. void
a28b0 20 2a 70 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c *p;.#ifndef SQL
a28c0 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 ITE_OMIT_LOOKASI
a28d0 44 45 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20 DE. if( db ){.
a28e0 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 LookasideSlot
a28f0 20 2a 70 42 75 66 3b 0a 20 20 20 20 69 66 28 20 *pBuf;. if(
a2900 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
a2910 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
a2920 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0;. }. if
a2930 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e ( db->lookaside.
a2940 62 45 6e 61 62 6c 65 64 20 26 26 20 6e 3c 3d 64 bEnabled && n<=d
a2950 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 0a b->lookaside.sz.
a2960 20 20 20 20 20 20 20 20 20 26 26 20 28 70 42 75 && (pBu
a2970 66 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 f = db->lookasid
a2980 65 2e 70 46 72 65 65 29 21 3d 30 20 29 7b 0a 20 e.pFree)!=0 ){.
a2990 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 db->lookasi
a29a0 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 2d de.pFree = pBuf-
a29b0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 64 62 >pNext;. db
a29c0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 ->lookaside.nOut
a29d0 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 ++;. if( db
a29e0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 ->lookaside.nOut
a29f0 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d >db->lookaside.m
a2a00 78 4f 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 xOut ){.
a2a10 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 db->lookaside.mx
a2a20 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 Out = db->lookas
a2a30 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 ide.nOut;.
a2a40 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 }. return (
a2a50 76 6f 69 64 2a 29 70 42 75 66 3b 0a 20 20 20 20 void*)pBuf;.
a2a60 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 }. }.#else. if
a2a70 28 20 64 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c ( db && db->mall
a2a80 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
a2a90 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 return 0;. }.#e
a2aa0 6e 64 69 66 0a 20 20 70 20 3d 20 73 71 6c 69 74 ndif. p = sqlit
a2ab0 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 e3Malloc(n);. i
a2ac0 66 28 20 21 70 20 26 26 20 64 62 20 29 7b 0a 20 f( !p && db ){.
a2ad0 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
a2ae0 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 led = 1;. }. r
a2af0 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
a2b00 2a 20 52 65 73 69 7a 65 20 74 68 65 20 62 6c 6f * Resize the blo
a2b10 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69 ck of memory poi
a2b20 6e 74 65 64 20 74 6f 20 62 79 20 70 20 74 6f 20 nted to by p to
a2b30 6e 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 0a n bytes. If the.
a2b40 2a 2a 20 72 65 73 69 7a 65 20 66 61 69 6c 73 2c ** resize fails,
a2b50 20 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 set the mallocF
a2b60 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 ailed flag in th
a2b70 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a e connection obj
a2b80 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ect..*/.SQLITE_P
a2b90 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
a2ba0 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 ite3DbRealloc(sq
a2bb0 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 lite3 *db, void
a2bc0 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f *p, int n){. vo
a2bd0 69 64 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 id *pNew = 0;.
a2be0 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
a2bf0 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 69 iled==0 ){. i
a2c00 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 f( p==0 ){.
a2c10 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 44 return sqlite3D
a2c20 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e bMallocRaw(db, n
a2c30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
a2c40 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c isLookaside(db,
a2c50 20 70 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 p) ){. if(
a2c60 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 n<=db->lookasid
a2c70 65 2e 73 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 e.sz ){.
a2c80 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 20 return p;.
a2c90 7d 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 }. pNew = s
a2ca0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
a2cb0 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 20 20 w(db, n);.
a2cc0 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 if( pNew ){.
a2cd0 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c memcpy(pNew,
a2ce0 20 70 2c 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 p, db->lookasid
a2cf0 65 2e 73 7a 29 3b 0a 20 20 20 20 20 20 20 20 73 e.sz);. s
a2d00 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
a2d10 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 p);. }.
a2d20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4e }else{. pN
a2d30 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 ew = sqlite3_rea
a2d40 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a 20 20 20 20 lloc(p, n);.
a2d50 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 if( !pNew ){.
a2d60 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f db->mallo
a2d70 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
a2d80 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
a2d90 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
a2da0 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 ./*.** Attempt t
a2db0 6f 20 72 65 61 6c 6c 6f 63 61 74 65 20 70 2e 20 o reallocate p.
a2dc0 20 49 66 20 74 68 65 20 72 65 61 6c 6c 6f 63 61 If the realloca
a2dd0 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 6e tion fails, then
a2de0 20 66 72 65 65 20 70 0a 2a 2a 20 61 6e 64 20 73 free p.** and s
a2df0 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 et the mallocFai
a2e00 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 led flag in the
a2e10 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
a2e20 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
a2e30 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
a2e40 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 ite3DbReallocOrF
a2e50 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ree(sqlite3 *db,
a2e60 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 void *p, int n)
a2e70 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a {. void *pNew;.
a2e80 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
a2e90 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 2c DbRealloc(db, p,
a2ea0 20 6e 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 n);. if( !pNew
a2eb0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 ){. sqlite3D
a2ec0 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 bFree(db, p);.
a2ed0 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b }. return pNew;
a2ee0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 .}../*.** Make a
a2ef0 20 63 6f 70 79 20 6f 66 20 61 20 73 74 72 69 6e copy of a strin
a2f00 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 g in memory obta
a2f10 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
a2f20 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 73 65 20 Malloc(). These
a2f30 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 63 61 .** functions ca
a2f40 6c 6c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 ll sqlite3Malloc
a2f50 52 61 77 28 29 20 64 69 72 65 63 74 6c 79 20 69 Raw() directly i
a2f60 6e 73 74 65 61 64 20 6f 66 20 73 71 6c 69 74 65 nstead of sqlite
a2f70 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 69 73 0a 2a Malloc(). This.*
a2f80 2a 20 69 73 20 62 65 63 61 75 73 65 20 77 68 65 * is because whe
a2f90 6e 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 67 69 n memory debuggi
a2fa0 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 6e 2c ng is turned on,
a2fb0 20 74 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 these two funct
a2fc0 69 6f 6e 73 20 61 72 65 20 0a 2a 2a 20 63 61 6c ions are .** cal
a2fd0 6c 65 64 20 76 69 61 20 6d 61 63 72 6f 73 20 74 led via macros t
a2fe0 68 61 74 20 72 65 63 6f 72 64 20 74 68 65 20 63 hat record the c
a2ff0 75 72 72 65 6e 74 20 66 69 6c 65 20 61 6e 64 20 urrent file and
a3000 6c 69 6e 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 line number in t
a3010 68 65 0a 2a 2a 20 54 68 72 65 61 64 44 61 74 61 he.** ThreadData
a3020 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 structure..*/.S
a3030 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
a3040 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 ar *sqlite3DbStr
a3050 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c Dup(sqlite3 *db,
a3060 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b const char *z){
a3070 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 . char *zNew;.
a3080 20 73 69 7a 65 5f 74 20 6e 3b 0a 20 20 69 66 28 size_t n;. if(
a3090 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 z==0 ){. ret
a30a0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d urn 0;. }. n =
a30b0 20 28 64 62 20 3f 20 73 71 6c 69 74 65 33 53 74 (db ? sqlite3St
a30c0 72 6c 65 6e 28 64 62 2c 20 7a 29 20 3a 20 73 71 rlen(db, z) : sq
a30d0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 lite3Strlen30(z)
a30e0 29 2b 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28 )+1;. assert( (
a30f0 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e n&0x7fffffff)==n
a3100 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c );. zNew = sql
a3110 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
a3120 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 db, (int)n);. i
a3130 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d f( zNew ){. m
a3140 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e emcpy(zNew, z, n
a3150 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
a3160 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 zNew;.}.SQLITE_P
a3170 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
a3180 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 ite3DbStrNDup(sq
a3190 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 lite3 *db, const
a31a0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 char *z, int n)
a31b0 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a {. char *zNew;.
a31c0 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 if( z==0 ){.
a31d0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
a31e0 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 assert( (n&0x7
a31f0 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 fffffff)==n );.
a3200 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 zNew = sqlite3D
a3210 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e bMallocRaw(db, n
a3220 2b 31 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 +1);. if( zNew
a3230 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e ){. memcpy(zN
a3240 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a ew, z, n);. z
a3250 4e 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a New[n] = 0;. }.
a3260 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d return zNew;.}
a3270 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
a3280 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 string from the
a3290 20 7a 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e zFromat argumen
a32a0 74 20 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 t and the va_lis
a32b0 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a t that follows..
a32c0 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 73 74 72 ** Store the str
a32d0 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 ing in memory ob
a32e0 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
a32f0 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d teMalloc() and m
a3300 61 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 ake *pz.** point
a3310 20 74 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e to that string.
a3320 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a3330 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
a3340 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a etString(char **
a3350 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c pz, sqlite3 *db,
a3360 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
a3370 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
a3380 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 _list ap;. char
a3390 20 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 *z;.. va_start
a33a0 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 (ap, zFormat);.
a33b0 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 z = sqlite3VMPr
a33c0 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 intf(db, zFormat
a33d0 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
a33e0 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 ap);. sqlite3Db
a33f0 46 72 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 Free(db, *pz);.
a3400 20 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a *pz = z;.}.../*
a3410 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
a3420 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 n must be called
a3430 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 before exiting
a3440 61 6e 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e any API function
a3450 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 (i.e. .** retur
a3460 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 ning control to
a3470 74 68 65 20 75 73 65 72 29 20 74 68 61 74 20 68 the user) that h
a3480 61 73 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 as called sqlite
a3490 33 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 3_malloc or.** s
a34a0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a qlite3_realloc..
a34b0 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e **.** The return
a34c0 65 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d ed value is norm
a34d0 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 ally a copy of t
a34e0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
a34f0 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 nt to this.** fu
a3500 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c nction. However,
a3510 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 if a malloc() f
a3520 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72 ailure has occur
a3530 65 64 20 73 69 6e 63 65 20 74 68 65 20 70 72 65 ed since the pre
a3540 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 vious.** invocat
a3550 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d ion SQLITE_NOMEM
a3560 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 is returned ins
a3570 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 tead. .**.** If
a3580 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
a3590 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e nt, db, is not N
a35a0 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 ULL and a malloc
a35b0 28 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 () error has occ
a35c0 75 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 ured,.** then th
a35d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72 e connection err
a35e0 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76 61 6c or-code (the val
a35f0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ue returned by s
a3600 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 qlite3_errcode()
a3610 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 ).** is set to S
a3620 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a QLITE_NOMEM..*/.
a3630 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a3640 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 nt sqlite3ApiExi
a3650 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 69 t(sqlite3* db, i
a3660 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20 nt rc){. /* If
a3670 74 68 65 20 64 62 20 68 61 6e 64 6c 65 20 69 73 the db handle is
a3680 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 not NULL, then
a3690 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 65 we must hold the
a36a0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 connection hand
a36b0 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 le. ** mutex he
a36c0 72 65 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 re. Otherwise th
a36d0 65 20 72 65 61 64 20 28 61 6e 64 20 70 6f 73 73 e read (and poss
a36e0 69 62 6c 65 20 77 72 69 74 65 29 20 6f 66 20 64 ible write) of d
a36f0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
a3700 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c . ** is unsafe,
a3710 20 61 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20 as is the call
a3720 74 6f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 to sqlite3Error(
a3730 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ).. */. assert
a3740 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33 ( !db || sqlite3
a3750 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
a3760 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
a3770 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f db && (db->mallo
a3780 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53 cFailed || rc==S
a3790 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 QLITE_IOERR_NOME
a37a0 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 M) ){. sqlite
a37b0 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 3Error(db, SQLIT
a37c0 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 E_NOMEM, 0);.
a37d0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
a37e0 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 d = 0;. rc =
a37f0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
a3800 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 }. return rc &
a3810 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 (db ? db->errMas
a3820 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a k : 0xff);.}../*
a3830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
a3840 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a d of malloc.c **
a3850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
a3880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
a3890 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e gin file printf.
a38a0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
a38b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a38c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
a38d0 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22 .** The "printf"
a38e0 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f code that follo
a38f0 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68 ws dates from th
a3900 65 20 31 39 38 30 27 73 2e 20 20 49 74 20 69 73 e 1980's. It is
a3910 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 in.** the publi
a3920 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f c domain. The o
a3930 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 riginal comments
a3940 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65 are included he
a3950 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 re for.** comple
a3960 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72 teness. They ar
a3970 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 e very out-of-da
a3980 74 65 20 62 75 74 20 6d 69 67 68 74 20 62 65 20 te but might be
a3990 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 useful as.** an
a39a0 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72 historical refer
a39b0 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 ence. Most of t
a39c0 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 he "enhancements
a39d0 22 20 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b " have been back
a39e0 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 ed.** out so tha
a39f0 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c t the functional
a3a00 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73 ity is now the s
a3a10 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72 64 20 ame as standard
a3a20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 printf()..**.**
a3a30 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 2c 76 20 $Id: printf.c,v
a3a40 31 2e 39 39 20 32 30 30 38 2f 31 32 2f 31 30 20 1.99 2008/12/10
a3a50 31 39 3a 32 36 3a 32 34 20 64 72 68 20 45 78 70 19:26:24 drh Exp
a3a60 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a $.**.**********
a3a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3ab0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f .**.** The follo
a3ac0 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20 wing modules is
a3ad0 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c an enhanced repl
a3ae0 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 acement for the
a3af0 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74 "printf" subrout
a3b00 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e ines.** found in
a3b10 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 the standard C
a3b20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f library. The fo
a3b30 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d llowing enhancem
a3b40 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70 ents are.** supp
a3b50 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 orted:.**.**
a3b60 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 + Additional
a3b70 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 functions. The
a3b80 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20 standard set of
a3b90 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f "printf" functio
a3ba0 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e ns.** in
a3bb0 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66 cludes printf, f
a3bc0 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c printf, sprintf,
a3bd0 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e vprintf, vfprin
a3be0 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 tf, and.**
a3bf0 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68 vsprintf. Th
a3c00 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74 is module adds t
a3c10 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a he following:.**
a3c20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 .** *
a3c30 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72 snprintf -- Wor
a3c40 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c ks like sprintf,
a3c50 20 62 75 74 20 68 61 73 20 61 6e 20 65 78 74 72 but has an extr
a3c60 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 a argument.**
a3c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3c80 20 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 which is
a3c90 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
a3ca0 62 75 66 66 65 72 20 77 72 69 74 74 65 6e 20 74 buffer written t
a3cb0 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 o..**.**
a3cc0 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d * mprintf --
a3cd0 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72 Similar to spr
a3ce0 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75 intf. Writes ou
a3cf0 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a tput to memory.*
a3d00 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
a3d10 20 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 obtai
a3d20 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e ned from malloc.
a3d30 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
a3d40 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20 * xprintf --
a3d50 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e Calls a function
a3d60 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f to dispose of o
a3d70 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 utput..**.**
a3d80 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74 * nprint
a3d90 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c f -- No output,
a3da0 20 62 75 74 20 72 65 74 75 72 6e 73 20 74 68 65 but returns the
a3db0 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 number of chara
a3dc0 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 cters.**
a3dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3de0 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 that would hav
a3df0 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79 e been output by
a3e00 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20 printf..**.**
a3e10 20 20 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d * A v-
a3e20 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73 version (ex: vs
a3e30 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72 nprintf) of ever
a3e40 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c y function is al
a3e50 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 so.**
a3e60 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a supplied..**.
a3e70 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66 65 77 ** + A few
a3e80 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74 extensions to t
a3e90 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f he formatting no
a3ea0 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f tation are suppo
a3eb0 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 rted:.**.**
a3ec0 20 20 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22 * The "="
a3ed0 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74 flag (similar t
a3ee0 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20 74 68 o "-") causes th
a3ef0 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a e output to be.*
a3f00 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 * b
a3f10 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68 e centered in th
a3f20 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20 e appropriately
a3f30 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a sized field..**.
a3f40 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 ** *
a3f50 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74 The %b field out
a3f60 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 puts an integer
a3f70 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69 in binary notati
a3f80 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 on..**.**
a3f90 20 20 20 20 2a 20 20 54 68 65 20 25 63 20 66 69 * The %c fi
a3fa0 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20 eld now accepts
a3fb0 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 a precision. Th
a3fc0 65 20 63 68 61 72 61 63 74 65 72 20 6f 75 74 70 e character outp
a3fd0 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ut.**
a3fe0 20 20 20 69 73 20 72 65 70 65 61 74 65 64 20 62 is repeated b
a3ff0 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 y the number of
a4000 74 69 6d 65 73 20 74 68 65 20 70 72 65 63 69 73 times the precis
a4010 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a ion specifies..*
a4020 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a *.** *
a4030 20 20 54 68 65 20 25 27 20 66 69 65 6c 64 20 77 The %' field w
a4040 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75 orks like %c, bu
a4050 74 20 74 61 6b 65 73 20 61 73 20 69 74 73 20 63 t takes as its c
a4060 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20 haracter the.**
a4070 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 nex
a4080 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 t character of t
a4090 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 he format string
a40a0 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 , instead of the
a40b0 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 next.**
a40c0 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20 argument.
a40d0 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70 For example, p
a40e0 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20 rintf("%.78'-")
a40f0 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73 prints 78 minus
a4100 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
a4110 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65 signs, the same
a4120 20 61 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37 as printf("%.7
a4130 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20 8c",'-')..**.**
a4140 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d + When com
a4150 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20 piled using GCC
a4160 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68 69 73 on a SPARC, this
a4170 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e version of prin
a4180 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 tf is.**
a4190 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 65 faster than the
a41a0 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74 66 20 library printf
a41b0 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a for SUN OS 4.1..
a41c0 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c **.** + Al
a41d0 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 l functions are
a41e0 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e fully reentrant.
a41f0 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f .**.*/../*.** Co
a4200 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 nversion types f
a4210 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73 all into various
a4220 20 63 61 74 65 67 6f 72 69 65 73 20 61 73 20 64 categories as d
a4230 65 66 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a efined by the.**
a4240 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65 following enume
a4250 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 ration..*/.#defi
a4260 6e 65 20 65 74 52 41 44 49 58 20 20 20 20 20 20 ne etRADIX
a4270 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79 1 /* Integer ty
a4280 70 65 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f pes. %d, %x, %o
a4290 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a , and so forth *
a42a0 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41 /.#define etFLOA
a42b0 54 20 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f T 2 /* Flo
a42c0 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66 ating point. %f
a42d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58 */.#define etEX
a42e0 50 20 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45 P 3 /* E
a42f0 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74 xponentional not
a4300 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45 ation. %e and %E
a4310 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45 */.#define etGE
a4320 4e 45 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46 NERIC 4 /* F
a4330 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e loating or expon
a4340 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e ential, dependin
a4350 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25 g on exponent. %
a4360 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 g */.#define etS
a4370 49 5a 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20 IZE 5 /*
a4380 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 Return number of
a4390 20 63 68 61 72 61 63 74 65 72 73 20 70 72 6f 63 characters proc
a43a0 65 73 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e essed so far. %n
a43b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54 */.#define etST
a43c0 52 49 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53 RING 6 /* S
a43d0 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64 trings. %s */.#d
a43e0 65 66 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e efine etDYNSTRIN
a43f0 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63 G 7 /* Dynamic
a4400 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 ally allocated s
a4410 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64 trings. %z */.#d
a4420 65 66 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20 efine etPERCENT
a4430 20 20 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74 8 /* Percent
a4440 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23 symbol. %% */.#
a4450 64 65 66 69 6e 65 20 65 74 43 48 41 52 58 20 20 define etCHARX
a4460 20 20 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63 9 /* Charac
a4470 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54 ters. %c */./* T
a4480 68 65 20 72 65 73 74 20 61 72 65 20 65 78 74 65 he rest are exte
a4490 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d nsions, not norm
a44a0 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72 ally found in pr
a44b0 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e intf() */.#defin
a44c0 65 20 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 e etSQLESCAPE 1
a44d0 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 0 /* Strings wit
a44e0 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 h '\'' doubled.
a44f0 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %q */.#define e
a4500 74 53 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f tSQLESCAPE2 11 /
a4510 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 * Strings with '
a4520 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20 \'' doubled and
a4530 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a enclosed in '',.
a4540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4550 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70 NULL p
a4560 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64 ointers replaced
a4570 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25 by SQL NULL. %
a4580 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54 Q */.#define etT
a4590 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20 OKEN 12 /*
a45a0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 a pointer to a T
a45b0 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a oken structure *
a45c0 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c /.#define etSRCL
a45d0 49 53 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70 IST 13 /* a p
a45e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c ointer to a SrcL
a45f0 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 ist */.#define e
a4600 74 50 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f tPOINTER 14 /
a4610 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73 * The %p convers
a4620 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 ion */.#define e
a4630 74 53 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f tSQLESCAPE3 15 /
a4640 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20 * %w -> Strings
a4650 77 69 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65 with '\"' double
a4660 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f d */.#define etO
a4670 52 44 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20 RDINAL 16 /*
a4680 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20 %r -> 1st, 2nd,
a4690 33 72 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20 3rd, 4th, etc.
a46a0 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a English only */.
a46b0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79 ../*.** An "etBy
a46c0 74 65 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20 te" is an 8-bit
a46d0 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a unsigned value..
a46e0 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 */.typedef unsig
a46f0 6e 65 64 20 63 68 61 72 20 65 74 42 79 74 65 3b ned char etByte;
a4700 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69 ../*.** Each bui
a4710 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20 ltin conversion
a4720 63 68 61 72 61 63 74 65 72 20 28 65 78 3a 20 74 character (ex: t
a4730 68 65 20 27 64 27 20 69 6e 20 22 25 64 22 29 20 he 'd' in "%d")
a4740 69 73 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 is described.**
a4750 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f by an instance o
a4760 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
a4770 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 structure.*/.typ
a4780 65 64 65 66 20 73 74 72 75 63 74 20 65 74 5f 69 edef struct et_i
a4790 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72 nfo { /* Infor
a47a0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 mation about eac
a47b0 68 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a h format field *
a47c0 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 79 70 65 /. char fmttype
a47d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
a47e0 54 68 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 The format field
a47f0 20 63 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a code letter */.
a4800 20 20 65 74 42 79 74 65 20 62 61 73 65 3b 20 20 etByte base;
a4810 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
a4820 65 20 62 61 73 65 20 66 6f 72 20 72 61 64 69 78 e base for radix
a4830 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 conversion */.
a4840 20 65 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20 etByte flags;
a4850 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
a4860 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47 or more of FLAG
a4870 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f _ constants belo
a4880 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79 w */. etByte ty
a4890 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pe;
a48a0 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 /* Conversion pa
a48b0 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79 radigm */. etBy
a48c0 74 65 20 63 68 61 72 73 65 74 3b 20 20 20 20 20 te charset;
a48d0 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
a48e0 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66 nto aDigits[] of
a48f0 20 74 68 65 20 64 69 67 69 74 73 20 73 74 72 69 the digits stri
a4900 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70 ng */. etByte p
a4910 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 refix;
a4920 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 /* Offset into
a4930 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65 aPrefix[] of the
a4940 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a prefix string *
a4950 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a /.} et_info;../*
a4960 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
a4970 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 es for et_info.f
a4980 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 lags.*/.#define
a4990 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20 FLAG_SIGNED 1
a49a0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
a49b0 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65 e value to conve
a49c0 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a rt is signed */.
a49d0 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54 #define FLAG_INT
a49e0 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72 ERN 2 /* Tr
a49f0 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e ue if for intern
a4a00 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 al use only */.#
a4a10 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49 define FLAG_STRI
a4a20 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c NG 4 /* All
a4a30 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63 ow infinity prec
a4a40 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a ision */.../*.**
a4a50 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 The following t
a4a60 61 62 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 able is searched
a4a70 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74 linearly, so it
a4a80 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20 is good to put
a4a90 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71 the.** most freq
a4aa0 75 65 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76 uently used conv
a4ab0 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72 ersion types fir
a4ac0 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f st..*/.static co
a4ad0 6e 73 74 20 63 68 61 72 20 61 44 69 67 69 74 73 nst char aDigits
a4ae0 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 [] = "0123456789
a4af0 41 42 43 44 45 46 30 31 32 33 34 35 36 37 38 39 ABCDEF0123456789
a4b00 61 62 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20 abcdef";.static
a4b10 63 6f 6e 73 74 20 63 68 61 72 20 61 50 72 65 66 const char aPref
a4b20 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58 ix[] = "-x0\000X
a4b30 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 0";.static const
a4b40 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f et_info fmtinfo
a4b50 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c [] = {. { 'd',
a4b60 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 10, 1, etRADIX,
a4b70 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 0, 0 },.
a4b80 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 { 's', 0, 4,
a4b90 65 74 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c etSTRING, 0,
a4ba0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c 0 },. { 'g',
a4bb0 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 0, 1, etGENERI
a4bc0 43 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 C, 30, 0 },.
a4bd0 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 { 'z', 0, 4,
a4be0 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c etDYNSTRING, 0,
a4bf0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c 0 },. { 'q',
a4c00 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 0, 4, etSQLESC
a4c10 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 APE, 0, 0 },.
a4c20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20 { 'Q', 0, 4,
a4c30 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c etSQLESCAPE2, 0,
a4c40 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c 0 },. { 'w',
a4c50 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 0, 4, etSQLESC
a4c60 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 APE3, 0, 0 },.
a4c70 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20 { 'c', 0, 0,
a4c80 65 74 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c etCHARX, 0,
a4c90 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c 0 },. { 'o',
a4ca0 20 20 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 8, 0, etRADIX,
a4cb0 20 20 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20 0, 2 },.
a4cc0 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20 { 'u', 10, 0,
a4cd0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c etRADIX, 0,
a4ce0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c 0 },. { 'x',
a4cf0 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 16, 0, etRADIX,
a4d00 20 20 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20 16, 1 },.
a4d10 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20 { 'X', 16, 0,
a4d20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c etRADIX, 0,
a4d30 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53 4 },.#ifndef S
a4d40 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
a4d50 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27 ING_POINT. { '
a4d60 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f f', 0, 1, etFLO
a4d70 41 54 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d AT, 0, 0 }
a4d80 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20 ,. { 'e', 0,
a4d90 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 1, etEXP,
a4da0 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 30, 0 },. { '
a4db0 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 E', 0, 1, etEXP
a4dc0 2c 20 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d , 14, 0 }
a4dd0 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20 ,. { 'G', 0,
a4de0 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 1, etGENERIC,
a4df0 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66 14, 0 },.#endif
a4e00 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 . { 'i', 10, 1
a4e10 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 , etRADIX,
a4e20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e 0, 0 },. { 'n
a4e30 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 ', 0, 0, etSIZE
a4e40 2c 20 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c , 0, 0 },
a4e50 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30 . { '%', 0, 0
a4e60 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20 , etPERCENT,
a4e70 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70 0, 0 },. { 'p
a4e80 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e ', 16, 0, etPOIN
a4e90 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c TER, 0, 1 },
a4ea0 0a 20 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32 . { 'T', 0, 2
a4eb0 2c 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20 , etTOKEN,
a4ec0 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53 0, 0 },. { 'S
a4ed0 27 2c 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c ', 0, 2, etSRCL
a4ee0 49 53 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c IST, 0, 0 },
a4ef0 0a 20 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33 . { 'r', 10, 3
a4f00 2c 20 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20 , etORDINAL,
a4f10 30 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 0, 0 },.};../*.
a4f20 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ** If SQLITE_OMI
a4f30 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
a4f40 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 is defined, the
a4f50 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c n none of the fl
a4f60 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 oating point.**
a4f70 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c conversions will
a4f80 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 work..*/.#ifnde
a4f90 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
a4fa0 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a OATING_POINT./*.
a4fb0 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 61 20 64 ** "*val" is a d
a4fc0 6f 75 62 6c 65 20 73 75 63 68 20 74 68 61 74 20 ouble such that
a4fd0 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30 0.1 <= *val < 10
a4fe0 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 .0.** Return the
a4ff0 20 61 73 63 69 69 20 63 6f 64 65 20 66 6f 72 20 ascii code for
a5000 74 68 65 20 6c 65 61 64 69 6e 67 20 64 69 67 69 the leading digi
a5010 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a t of *val, then.
a5020 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61 ** multiply "*va
a5030 6c 22 20 62 79 20 31 30 2e 30 20 74 6f 20 72 65 l" by 10.0 to re
a5040 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a normalize..**.**
a5050 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20 Example:.**
a5060 20 69 6e 70 75 74 3a 20 20 20 20 20 2a 76 61 6c input: *val
a5070 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20 = 3.14159.**
a5080 20 20 6f 75 74 70 75 74 3a 20 20 20 20 2a 76 61 output: *va
a5090 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 20 66 75 l = 1.4159 fu
a50a0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20 nction return =
a50b0 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f '3'.**.** The co
a50c0 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 69 6e unter *cnt is in
a50d0 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 cremented each t
a50e0 69 6d 65 2e 20 20 41 66 74 65 72 20 63 6f 75 6e ime. After coun
a50f0 74 65 72 20 65 78 63 65 65 64 73 0a 2a 2a 20 31 ter exceeds.** 1
a5100 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 6 (the number of
a5110 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 significant dig
a5120 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69 74 20 its in a 64-bit
a5130 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0a 2a 2a float) '0' is.**
a5140 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65 64 always returned
a5150 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
a5160 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e et_getdigit(LON
a5170 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a 76 61 GDOUBLE_TYPE *va
a5180 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20 l, int *cnt){.
a5190 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e int digit;. LON
a51a0 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a GDOUBLE_TYPE d;.
a51b0 20 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e if( (*cnt)++ >
a51c0 3d 20 31 36 20 29 20 72 65 74 75 72 6e 20 27 30 = 16 ) return '0
a51d0 27 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 69 6e ';. digit = (in
a51e0 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69 t)*val;. d = di
a51f0 67 69 74 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20 git;. digit +=
a5200 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a '0';. *val = (*
a5210 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20 val - d)*10.0;.
a5220 20 72 65 74 75 72 6e 20 28 63 68 61 72 29 64 69 return (char)di
a5230 67 69 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a git;.}.#endif /*
a5240 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
a5250 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a ATING_POINT */..
a5260 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73 /*.** Append N s
a5270 70 61 63 65 20 63 68 61 72 61 63 74 65 72 73 20 pace characters
a5280 74 6f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 to the given str
a5290 69 6e 67 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 ing buffer..*/.s
a52a0 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e tatic void appen
a52b0 64 53 70 61 63 65 28 53 74 72 41 63 63 75 6d 20 dSpace(StrAccum
a52c0 2a 70 41 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b *pAccum, int N){
a52d0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
a52e0 63 68 61 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d char zSpaces[] =
a52f0 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a5300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
a5310 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e 3d 28 69 ;. while( N>=(i
a5320 6e 74 29 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 nt)sizeof(zSpace
a5330 73 29 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 s)-1 ){. sqli
a5340 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
a5350 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63 65 d(pAccum, zSpace
a5360 73 2c 20 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 s, sizeof(zSpace
a5370 73 29 2d 31 29 3b 0a 20 20 20 20 4e 20 2d 3d 20 s)-1);. N -=
a5380 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d sizeof(zSpaces)-
a5390 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3e 30 1;. }. if( N>0
a53a0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
a53b0 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 trAccumAppend(pA
a53c0 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 4e ccum, zSpaces, N
a53d0 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
a53e0 4f 6e 20 6d 61 63 68 69 6e 65 73 20 77 69 74 68 On machines with
a53f0 20 61 20 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73 a small stack s
a5400 69 7a 65 2c 20 79 6f 75 20 63 61 6e 20 72 65 64 ize, you can red
a5410 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20 53 51 4c efine the.** SQL
a5420 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 ITE_PRINT_BUF_SI
a5430 5a 45 20 74 6f 20 62 65 20 6c 65 73 73 20 74 68 ZE to be less th
a5440 61 6e 20 33 35 30 2e 20 20 42 75 74 20 62 65 77 an 350. But bew
a5450 61 72 65 20 2d 20 66 6f 72 0a 2a 2a 20 73 6d 61 are - for.** sma
a5460 6c 6c 65 72 20 76 61 6c 75 65 73 20 73 6f 6d 65 ller values some
a5470 20 25 66 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 %f conversions
a5480 6d 61 79 20 67 6f 20 69 6e 74 6f 20 61 6e 20 69 may go into an i
a5490 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 2e 0a 2a 2f nfinite loop..*/
a54a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a54b0 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 0a 23 PRINT_BUF_SIZE.#
a54c0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 define SQLITE_P
a54d0 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33 35 RINT_BUF_SIZE 35
a54e0 30 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 0.#endif.#define
a54f0 20 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49 54 etBUFSIZE SQLIT
a5500 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 E_PRINT_BUF_SIZE
a5510 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
a5520 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a output buffer *
a5530 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f /../*.** The roo
a5540 74 20 70 72 6f 67 72 61 6d 2e 20 20 41 6c 6c 20 t program. All
a5550 76 61 72 69 61 74 69 6f 6e 73 20 63 61 6c 6c 20 variations call
a5560 74 68 69 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a this core..**.**
a5570 20 49 4e 50 55 54 53 3a 0a 2a 2a 20 20 20 66 75 INPUTS:.** fu
a5580 6e 63 20 20 20 54 68 69 73 20 69 73 20 61 20 70 nc This is a p
a5590 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 ointer to a func
a55a0 74 69 6f 6e 20 74 61 6b 69 6e 67 20 74 68 72 65 tion taking thre
a55b0 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 20 e arguments.**
a55c0 20 20 20 20 20 20 20 20 20 20 31 2e 20 41 20 70 1. A p
a55d0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 ointer to anythi
a55e0 6e 67 2e 20 20 53 61 6d 65 20 61 73 20 74 68 65 ng. Same as the
a55f0 20 22 61 72 67 22 20 70 61 72 61 6d 65 74 65 72 "arg" parameter
a5600 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 ..**
a5610 32 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 2. A pointer to
a5620 74 68 65 20 6c 69 73 74 20 6f 66 20 63 68 61 72 the list of char
a5630 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74 acters to be out
a5640 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 put.**
a5650 20 20 20 20 20 28 4e 6f 74 65 2c 20 74 68 69 73 (Note, this
a5660 20 6c 69 73 74 20 69 73 20 4e 4f 54 20 6e 75 6c list is NOT nul
a5670 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e 29 0a 2a l terminated.).*
a5680 2a 20 20 20 20 20 20 20 20 20 20 20 20 33 2e 20 * 3.
a5690 41 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 An integer numbe
a56a0 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 r of characters
a56b0 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 2a 2a to be output..**
a56c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
a56d0 4e 6f 74 65 3a 20 54 68 69 73 20 6e 75 6d 62 65 Note: This numbe
a56e0 72 20 6d 69 67 68 74 20 62 65 20 7a 65 72 6f 2e r might be zero.
a56f0 29 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 20 20 20 ).**.** arg
a5700 20 54 68 69 73 20 69 73 20 74 68 65 20 70 6f 69 This is the poi
a5710 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 nter to anything
a5720 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20 70 which will be p
a5730 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 assed as the.**
a5740 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20 61 first a
a5750 72 67 75 6d 65 6e 74 20 74 6f 20 22 66 75 6e 63 rgument to "func
a5760 22 2e 20 20 55 73 65 20 69 74 20 66 6f 72 20 77 ". Use it for w
a5770 68 61 74 65 76 65 72 20 79 6f 75 20 6c 69 6b 65 hatever you like
a5780 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 6d 74 20 20 20 ..**.** fmt
a5790 20 54 68 69 73 20 69 73 20 74 68 65 20 66 6f 72 This is the for
a57a0 6d 61 74 20 73 74 72 69 6e 67 2c 20 61 73 20 69 mat string, as i
a57b0 6e 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e n the usual prin
a57c0 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 70 20 20 20 t..**.** ap
a57d0 20 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e This is a poin
a57e0 74 65 72 20 74 6f 20 61 20 6c 69 73 74 20 6f 66 ter to a list of
a57f0 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 53 61 6d arguments. Sam
a5800 65 20 61 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 e as in.**
a5810 20 20 20 20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a vfprint..**.
a5820 2a 2a 20 4f 55 54 50 55 54 53 3a 0a 2a 2a 20 20 ** OUTPUTS:.**
a5830 20 20 20 20 20 20 20 20 54 68 65 20 72 65 74 75 The retu
a5840 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 rn value is the
a5850 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
a5860 63 68 61 72 61 63 74 65 72 73 20 73 65 6e 74 20 characters sent
a5870 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 to.** t
a5880 68 65 20 66 75 6e 63 74 69 6f 6e 20 22 66 75 6e he function "fun
a5890 63 22 2e 20 20 52 65 74 75 72 6e 73 20 2d 31 20 c". Returns -1
a58a0 6f 6e 20 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a on a error..**.*
a58b0 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 * Note that the
a58c0 6f 72 64 65 72 20 69 6e 20 77 68 69 63 68 20 61 order in which a
a58d0 75 74 6f 6d 61 74 69 63 20 76 61 72 69 61 62 6c utomatic variabl
a58e0 65 73 20 61 72 65 20 64 65 63 6c 61 72 65 64 20 es are declared
a58f0 62 65 6c 6f 77 0a 2a 2a 20 73 65 65 6d 73 20 74 below.** seems t
a5900 6f 20 6d 61 6b 65 20 61 20 62 69 67 20 64 69 66 o make a big dif
a5910 66 65 72 65 6e 63 65 20 69 6e 20 64 65 74 65 72 ference in deter
a5920 6d 69 6e 69 6e 67 20 68 6f 77 20 66 61 73 74 20 mining how fast
a5930 74 68 69 73 20 62 65 61 73 74 0a 2a 2a 20 77 69 this beast.** wi
a5940 6c 6c 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ll run..*/.SQLIT
a5950 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a5960 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 0a qlite3VXPrintf(.
a5970 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 StrAccum *pAcc
a5980 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 um,
a5990 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 /* Accumula
a59a0 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 te results here
a59b0 2a 2f 0a 20 20 69 6e 74 20 75 73 65 45 78 74 65 */. int useExte
a59c0 6e 64 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 nded,
a59d0 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 /* Allow
a59e0 20 65 78 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76 extended %-conv
a59f0 65 72 73 69 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e ersions */. con
a5a00 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 20 20 st char *fmt,
a5a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5a20 2f 2a 20 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 /* Format string
a5a30 20 2a 2f 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 */. va_list ap
a5a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5a50 20 20 20 20 20 20 20 20 20 2f 2a 20 61 72 67 75 /* argu
a5a60 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e ments */.){. in
a5a70 74 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 t c;
a5a80 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 /* Next
a5a90 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 character in th
a5aa0 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 e format string
a5ab0 2a 2f 0a 20 20 63 68 61 72 20 2a 62 75 66 70 74 */. char *bufpt
a5ac0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a5ad0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
a5ae0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 e conversion buf
a5af0 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 fer */. int pre
a5b00 63 69 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 cision;
a5b10 20 20 20 20 2f 2a 20 50 72 65 63 69 73 69 6f 6e /* Precision
a5b20 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
a5b30 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c field */. int l
a5b40 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 20 20 ength;
a5b50 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 /* Length
a5b60 6f 66 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a of the field */.
a5b70 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 int idx;
a5b80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a5b90 41 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 A general purpos
a5ba0 65 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a e loop counter *
a5bb0 2f 0a 20 20 69 6e 74 20 77 69 64 74 68 3b 20 20 /. int width;
a5bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a5bd0 2a 20 57 69 64 74 68 20 6f 66 20 74 68 65 20 63 * Width of the c
a5be0 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a urrent field */.
a5bf0 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 65 etByte flag_le
a5c00 66 74 6a 75 73 74 69 66 79 3b 20 20 20 2f 2a 20 ftjustify; /*
a5c10 54 72 75 65 20 69 66 20 22 2d 22 20 66 6c 61 67 True if "-" flag
a5c20 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 is present */.
a5c30 20 65 74 42 79 74 65 20 66 6c 61 67 5f 70 6c 75 etByte flag_plu
a5c40 73 73 69 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54 ssign; /* T
a5c50 72 75 65 20 69 66 20 22 2b 22 20 66 6c 61 67 20 rue if "+" flag
a5c60 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 is present */.
a5c70 65 74 42 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e etByte flag_blan
a5c80 6b 73 69 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72 ksign; /* Tr
a5c90 75 65 20 69 66 20 22 20 22 20 66 6c 61 67 20 69 ue if " " flag i
a5ca0 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 s present */. e
a5cb0 74 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 65 72 tByte flag_alter
a5cc0 6e 61 74 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75 nateform; /* Tru
a5cd0 65 20 69 66 20 22 23 22 20 66 6c 61 67 20 69 73 e if "#" flag is
a5ce0 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 present */. et
a5cf0 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72 Byte flag_altfor
a5d00 6d 32 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 m2; /* True
a5d10 20 69 66 20 22 21 22 20 66 6c 61 67 20 69 73 20 if "!" flag is
a5d20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 present */. etB
a5d30 79 74 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 yte flag_zeropad
a5d40 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 ; /* True
a5d50 69 66 20 66 69 65 6c 64 20 77 69 64 74 68 20 63 if field width c
a5d60 6f 6e 73 74 61 6e 74 20 73 74 61 72 74 73 20 77 onstant starts w
a5d70 69 74 68 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74 ith zero */. et
a5d80 42 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 Byte flag_long;
a5d90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
a5da0 20 69 66 20 22 6c 22 20 66 6c 61 67 20 69 73 20 if "l" flag is
a5db0 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 present */. etB
a5dc0 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e yte flag_longlon
a5dd0 67 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 g; /* True
a5de0 69 66 20 74 68 65 20 22 6c 6c 22 20 66 6c 61 67 if the "ll" flag
a5df0 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 is present */.
a5e00 20 65 74 42 79 74 65 20 64 6f 6e 65 3b 20 20 20 etByte done;
a5e10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
a5e20 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 oop termination
a5e30 66 6c 61 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 flag */. sqlite
a5e40 5f 75 69 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75 _uint64 longvalu
a5e50 65 3b 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f e; /* Value fo
a5e60 72 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 20 r integer types
a5e70 2a 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f */. LONGDOUBLE_
a5e80 54 59 50 45 20 72 65 61 6c 76 61 6c 75 65 3b 20 TYPE realvalue;
a5e90 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 72 65 61 /* Value for rea
a5ea0 6c 20 74 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e l types */. con
a5eb0 73 74 20 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f st et_info *info
a5ec0 70 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 p; /* Point
a5ed0 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 er to the approp
a5ee0 72 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63 riate info struc
a5ef0 74 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 62 ture */. char b
a5f00 75 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20 20 uf[etBUFSIZE];
a5f10 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 /* Conversi
a5f20 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 on buffer */. c
a5f30 68 61 72 20 70 72 65 66 69 78 3b 20 20 20 20 20 har prefix;
a5f40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 /* Pre
a5f50 66 69 78 20 63 68 61 72 61 63 74 65 72 2e 20 20 fix character.
a5f60 22 2b 22 20 6f 72 20 22 2d 22 20 6f 72 20 22 20 "+" or "-" or "
a5f70 22 20 6f 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 " or '\0'. */.
a5f80 65 74 42 79 74 65 20 78 74 79 70 65 20 3d 20 30 etByte xtype = 0
a5f90 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f ; /* Co
a5fa0 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67 nversion paradig
a5fb0 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78 m */. char *zEx
a5fc0 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 tra;
a5fd0 20 20 2f 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72 /* Extra memor
a5fe0 79 20 75 73 65 64 20 66 6f 72 20 65 74 54 43 4c y used for etTCL
a5ff0 45 53 43 41 50 45 20 63 6f 6e 76 65 72 73 69 6f ESCAPE conversio
a6000 6e 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 ns */.#ifndef SQ
a6010 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
a6020 4e 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 20 NG_POINT. int
a6030 65 78 70 2c 20 65 32 3b 20 20 20 20 20 20 20 20 exp, e2;
a6040 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e /* exponen
a6050 74 20 6f 66 20 72 65 61 6c 20 6e 75 6d 62 65 72 t of real number
a6060 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f s */. double ro
a6070 75 6e 64 65 72 3b 20 20 20 20 20 20 20 20 20 20 under;
a6080 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 72 6f /* Used for ro
a6090 75 6e 64 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20 unding floating
a60a0 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a point values */.
a60b0 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 64 70 etByte flag_dp
a60c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
a60d0 54 72 75 65 20 69 66 20 64 65 63 69 6d 61 6c 20 True if decimal
a60e0 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 point should be
a60f0 73 68 6f 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74 shown */. etByt
a6100 65 20 66 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20 e flag_rtz;
a6110 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a6120 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 trailing zeros
a6130 73 68 6f 75 6c 64 20 62 65 20 72 65 6d 6f 76 65 should be remove
a6140 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c d */. etByte fl
a6150 61 67 5f 65 78 70 3b 20 20 20 20 20 20 20 20 20 ag_exp;
a6160 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 66 6f 72 /* True to for
a6170 63 65 20 64 69 73 70 6c 61 79 20 6f 66 20 74 68 ce display of th
a6180 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 e exponent */.
a6190 69 6e 74 20 6e 73 64 3b 20 20 20 20 20 20 20 20 int nsd;
a61a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
a61b0 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63 mber of signific
a61c0 61 6e 74 20 64 69 67 69 74 73 20 72 65 74 75 72 ant digits retur
a61d0 6e 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 ned */.#endif..
a61e0 20 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62 length = 0;. b
a61f0 75 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 ufpt = 0;. for(
a6200 3b 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b ; (c=(*fmt))!=0;
a6210 20 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69 66 28 ++fmt){. if(
a6220 20 63 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 c!='%' ){.
a6230 20 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20 20 20 int amt;.
a6240 62 75 66 70 74 20 3d 20 28 63 68 61 72 20 2a 29 bufpt = (char *)
a6250 66 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d fmt;. amt =
a6260 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 1;. while(
a6270 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 (c=(*++fmt))!='
a6280 25 27 20 26 26 20 63 21 3d 30 20 29 20 61 6d 74 %' && c!=0 ) amt
a6290 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ++;. sqlite
a62a0 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
a62b0 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 61 pAccum, bufpt, a
a62c0 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 mt);. if( c
a62d0 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ==0 ) break;.
a62e0 20 7d 0a 20 20 20 20 69 66 28 20 28 63 3d 28 2a }. if( (c=(*
a62f0 2b 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20 ++fmt))==0 ){.
a6300 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
a6310 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
a6320 2c 20 22 25 22 2c 20 31 29 3b 0a 20 20 20 20 20 , "%", 1);.
a6330 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
a6340 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 /* Find out wh
a6350 61 74 20 66 6c 61 67 73 20 61 72 65 20 70 72 65 at flags are pre
a6360 73 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67 sent */. flag
a6370 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 66 _leftjustify = f
a6380 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66 lag_plussign = f
a6390 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 lag_blanksign =
a63a0 0a 20 20 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 . flag_alter
a63b0 6e 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f nateform = flag_
a63c0 61 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f altform2 = flag_
a63d0 7a 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20 zeropad = 0;.
a63e0 20 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64 done = 0;. d
a63f0 6f 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 o{. switch(
a6400 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 c ){. ca
a6410 73 65 20 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c se '-': flag_l
a6420 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 20 eftjustify = 1;
a6430 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
a6440 20 20 20 63 61 73 65 20 27 2b 27 3a 20 20 20 66 case '+': f
a6450 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31 lag_plussign = 1
a6460 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a ; break;.
a6470 20 20 20 20 20 20 20 20 63 61 73 65 20 27 20 27 case ' '
a6480 3a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 : flag_blanksi
a6490 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 62 72 gn = 1; br
a64a0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
a64b0 65 20 27 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c e '#': flag_al
a64c0 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b ternateform = 1;
a64d0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a64e0 20 20 63 61 73 65 20 27 21 27 3a 20 20 20 66 6c case '!': fl
a64f0 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b ag_altform2 = 1;
a6500 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
a6510 20 20 20 20 20 20 20 63 61 73 65 20 27 30 27 3a case '0':
a6520 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 flag_zeropad
a6530 3d 20 31 3b 20 20 20 20 20 20 20 20 20 62 72 65 = 1; bre
a6540 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61 ak;. defa
a6550 75 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31 ult: done = 1
a6560 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a6570 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
a6580 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 21 64 6f . }while( !do
a6590 6e 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 ne && (c=(*++fmt
a65a0 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20 ))!=0 );. /*
a65b0 47 65 74 20 74 68 65 20 66 69 65 6c 64 20 77 69 Get the field wi
a65c0 64 74 68 20 2a 2f 0a 20 20 20 20 77 69 64 74 68 dth */. width
a65d0 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 63 3d = 0;. if( c=
a65e0 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 77 69 ='*' ){. wi
a65f0 64 74 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c dth = va_arg(ap,
a6600 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 int);. if(
a6610 77 69 64 74 68 3c 30 20 29 7b 0a 20 20 20 20 20 width<0 ){.
a6620 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 flag_leftjust
a6630 69 66 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ify = 1;.
a6640 20 77 69 64 74 68 20 3d 20 2d 77 69 64 74 68 3b width = -width;
a6650 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 . }. c
a6660 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d = *++fmt;. }
a6670 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c else{. whil
a6680 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d e( c>='0' && c<=
a6690 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 '9' ){. w
a66a0 69 64 74 68 20 3d 20 77 69 64 74 68 2a 31 30 20 idth = width*10
a66b0 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 + c - '0';.
a66c0 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 c = *++fmt;.
a66d0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
a66e0 20 69 66 28 20 77 69 64 74 68 20 3e 20 65 74 42 if( width > etB
a66f0 55 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20 UFSIZE-10 ){.
a6700 20 20 20 77 69 64 74 68 20 3d 20 65 74 42 55 46 width = etBUF
a6710 53 49 5a 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 SIZE-10;. }.
a6720 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72 /* Get the pr
a6730 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 ecision */. i
a6740 66 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 f( c=='.' ){.
a6750 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30 precision = 0
a6760 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 ;. c = *++f
a6770 6d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d mt;. if( c=
a6780 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 ='*' ){.
a6790 70 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61 precision = va_a
a67a0 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 rg(ap,int);.
a67b0 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f if( precisio
a67c0 6e 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 n<0 ) precision
a67d0 3d 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 = -precision;.
a67e0 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 c = *++fmt
a67f0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
a6800 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e while( c>
a6810 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 ='0' && c<='9' )
a6820 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63 {. prec
a6830 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f ision = precisio
a6840 6e 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a n*10 + c - '0';.
a6850 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b c = *+
a6860 2b 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a +fmt;. }.
a6870 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
a6880 65 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 e{. precisi
a6890 6f 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 on = -1;. }.
a68a0 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f /* Get the co
a68b0 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f nversion type mo
a68c0 64 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66 difier */. if
a68d0 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 ( c=='l' ){.
a68e0 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b flag_long = 1;
a68f0 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d . c = *++fm
a6900 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d t;. if( c==
a6910 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66 'l' ){. f
a6920 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 lag_longlong = 1
a6930 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b ;. c = *+
a6940 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 +fmt;. }els
a6950 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f e{. flag_
a6960 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 longlong = 0;.
a6970 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
a6980 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 . flag_long
a6990 20 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 = flag_longlong
a69a0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
a69b0 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66 /* Fetch the inf
a69c0 6f 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 o entry for the
a69d0 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66 field */. inf
a69e0 6f 70 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 op = 0;. for(
a69f0 69 64 78 3d 30 3b 20 69 64 78 3c 41 72 72 61 79 idx=0; idx<Array
a6a00 53 69 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20 69 Size(fmtinfo); i
a6a10 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 dx++){. if(
a6a20 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 5d c==fmtinfo[idx]
a6a30 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20 20 20 .fmttype ){.
a6a40 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 infop = &fmt
a6a50 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 20 info[idx];.
a6a60 20 20 20 69 66 28 20 75 73 65 45 78 74 65 6e 64 if( useExtend
a6a70 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c ed || (infop->fl
a6a80 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54 45 52 ags & FLAG_INTER
a6a90 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 N)==0 ){.
a6aa0 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66 6f 70 xtype = infop
a6ab0 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20 20 20 ->type;.
a6ac0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a6ad0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 return;.
a6ae0 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b }. break
a6af0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
a6b00 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b 0a zExtra = 0;.
a6b10 20 20 20 20 69 66 28 20 69 6e 66 6f 70 3d 3d 30 if( infop==0
a6b20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
a6b30 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a ;. }... /*
a6b40 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65 63 69 Limit the preci
a6b50 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20 sion to prevent
a6b60 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b overflowing buf[
a6b70 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 ] during convers
a6b80 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 ion */. if( p
a6b90 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 recision>etBUFSI
a6ba0 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d ZE-40 && (infop-
a6bb0 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54 >flags & FLAG_ST
a6bc0 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 RING)==0 ){.
a6bd0 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 precision = et
a6be0 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20 BUFSIZE-40;.
a6bf0 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a }.. /*. **
a6c00 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 At this point,
a6c10 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e variables are in
a6c20 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c itialized as fol
a6c30 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 lows:. **.
a6c40 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 ** flag_alter
a6c50 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20 nateform
a6c60 20 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20 TRUE if a '#'
a6c70 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 is present..
a6c80 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 ** flag_altfor
a6c90 6d 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m2
a6ca0 20 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69 TRUE if a '!' i
a6cb0 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a s present.. *
a6cc0 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 * flag_plussig
a6cd0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
a6ce0 54 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73 TRUE if a '+' is
a6cf0 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a present.. **
a6d00 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 flag_leftjust
a6d10 69 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54 ify T
a6d20 52 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20 RUE if a '-' is
a6d30 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68 present or if th
a6d40 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 e. **
a6d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6d60 20 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64 field wid
a6d70 74 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e th was negative.
a6d80 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a . ** flag_z
a6d90 65 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20 eropad
a6da0 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 TRUE if th
a6db0 65 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69 e width began wi
a6dc0 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 th 0.. ** f
a6dd0 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 lag_long
a6de0 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 TRUE
a6df0 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c if the letter 'l
a6e00 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64 ' (ell) prefixed
a6e10 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
a6e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6e30 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 the conver
a6e40 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a sion character..
a6e50 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f ** flag_lo
a6e60 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 nglong
a6e70 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 TRUE if the
a6e80 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c letter 'll' (el
a6e90 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a l ell) prefixed.
a6ea0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
a6eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6ec0 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 the convers
a6ed0 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 ion character..
a6ee0 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 ** flag_bla
a6ef0 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 nksign
a6f00 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20 TRUE if a '
a6f10 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 ' is present..
a6f20 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20 ** width
a6f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6f40 20 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64 The specified
a6f50 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54 field width. T
a6f60 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 his is. **
a6f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6f80 20 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 alwa
a6f90 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e ys non-negative.
a6fa0 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 Zero is the de
a6fb0 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 fault.. **
a6fc0 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 precision
a6fd0 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
a6fe0 73 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73 specified precis
a6ff0 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c ion. The defaul
a7000 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 t. **
a7010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7020 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 is -1..
a7030 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20 ** xtype
a7040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7050 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20 The class of
a7060 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a the conversion..
a7070 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 ** infop
a7080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7090 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 Pointer to
a70a0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
a70b0 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 info struct..
a70c0 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20 */. switch(
a70d0 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 xtype ){. c
a70e0 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 ase etPOINTER:.
a70f0 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 flag_long
a7100 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 long = sizeof(ch
a7110 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 ar*)==sizeof(i64
a7120 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f );. flag_
a7130 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 long = sizeof(ch
a7140 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e ar*)==sizeof(lon
a7150 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 g int);.
a7160 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 /* Fall through
a7170 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 into the next ca
a7180 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 se */. case
a7190 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 etORDINAL:.
a71a0 20 20 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a case etRADIX:.
a71b0 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f if( info
a71c0 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f p->flags & FLAG_
a71d0 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 SIGNED ){.
a71e0 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 i64 v;.
a71f0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f if( flag_lo
a7200 6e 67 6c 6f 6e 67 20 29 20 20 20 76 20 3d 20 76 nglong ) v = v
a7210 61 5f 61 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 a_arg(ap,i64);.
a7220 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 else if
a7230 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 20 20 76 ( flag_long ) v
a7240 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 6c 6f 6e = va_arg(ap,lon
a7250 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 g int);.
a7260 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 else
a7270 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 v = va_a
a7280 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 rg(ap,int);.
a7290 20 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 7b if( v<0 ){
a72a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e . lon
a72b0 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 gvalue = -v;.
a72c0 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 prefix
a72d0 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 = '-';.
a72e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a72f0 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 longvalue =
a7300 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 v;. i
a7310 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e f( flag_plussign
a7320 20 29 20 20 20 20 20 20 20 20 70 72 65 66 69 78 ) prefix
a7330 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 = '+';.
a7340 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 else if( fla
a7350 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70 g_blanksign ) p
a7360 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 refix = ' ';.
a7370 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20 else
a7380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7390 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a prefix = 0;.
a73a0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
a73b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a73c0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f if( flag_lo
a73d0 6e 67 6c 6f 6e 67 20 29 20 20 20 6c 6f 6e 67 76 nglong ) longv
a73e0 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 alue = va_arg(ap
a73f0 2c 75 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20 ,u64);.
a7400 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c else if( flag_l
a7410 6f 6e 67 20 29 20 20 6c 6f 6e 67 76 61 6c 75 65 ong ) longvalue
a7420 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 = va_arg(ap,uns
a7430 69 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74 29 3b igned long int);
a7440 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 . else
a7450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7460 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f longvalue = va_
a7470 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 arg(ap,unsigned
a7480 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 int);.
a7490 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 prefix = 0;.
a74a0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
a74b0 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 29 ( longvalue==0 )
a74c0 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 flag_alternatef
a74d0 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 orm = 0;.
a74e0 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 if( flag_zeropa
a74f0 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c 77 d && precision<w
a7500 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 idth-(prefix!=0)
a7510 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 ){. pr
a7520 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68 2d ecision = width-
a7530 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 20 20 (prefix!=0);.
a7540 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 }. b
a7550 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74 42 55 ufpt = &buf[etBU
a7560 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 20 20 FSIZE-1];.
a7570 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 4f if( xtype==etO
a7580 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 20 20 RDINAL ){.
a7590 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
a75a0 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 22 char zOrd[] = "
a75b0 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 20 20 thstndrd";.
a75c0 20 20 20 20 20 69 6e 74 20 78 20 3d 20 28 69 6e int x = (in
a75d0 74 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 20 31 t)(longvalue % 1
a75e0 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 0);. if
a75f0 28 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67 76 ( x>=4 || (longv
a7600 61 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 20 29 alue/10)%10==1 )
a7610 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 20 {. x
a7620 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d = 0;. }
a7630 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 5b 65 . buf[e
a7640 74 42 55 46 53 49 5a 45 2d 33 5d 20 3d 20 7a 4f tBUFSIZE-3] = zO
a7650 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 20 20 20 20 rd[x*2];.
a7660 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 5a 45 buf[etBUFSIZE
a7670 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 2b 31 -2] = zOrd[x*2+1
a7680 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 ];. buf
a7690 70 74 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20 pt -= 2;.
a76a0 20 7d 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20 }. {.
a76b0 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20 register
a76c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73 65 74 const char *cset
a76d0 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 72 65 ; /* Use re
a76e0 67 69 73 74 65 72 73 20 66 6f 72 20 73 70 65 65 gisters for spee
a76f0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 d */. r
a7700 65 67 69 73 74 65 72 20 69 6e 74 20 62 61 73 65 egister int base
a7710 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73 65 74 ;. cset
a7720 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e 66 6f = &aDigits[info
a7730 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20 20 p->charset];.
a7740 20 20 20 20 20 20 20 62 61 73 65 20 3d 20 69 6e base = in
a7750 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20 20 20 fop->base;.
a7760 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20 20 20 do{
a7770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7790 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 6f /* Convert to
a77a0 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20 20 20 ascii */.
a77b0 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 *(--bufpt)
a77c0 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61 6c 75 = cset[longvalu
a77d0 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20 20 20 e%base];.
a77e0 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d longvalue =
a77f0 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73 65 3b longvalue/base;
a7800 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 69 6c . }whil
a7810 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30 20 29 e( longvalue>0 )
a7820 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a7830 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e length = (in
a7840 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a t)(&buf[etBUFSIZ
a7850 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 E-1]-bufpt);.
a7860 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 for(idx=pre
a7870 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 69 cision-length; i
a7880 64 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 20 dx>0; idx--){.
a7890 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 *(--bufp
a78a0 74 29 20 3d 20 27 30 27 3b 20 20 20 20 20 20 20 t) = '0';
a78b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a78c0 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 61 /* Zero pa
a78d0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 d */. }.
a78e0 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 if( prefi
a78f0 78 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d x ) *(--bufpt) =
a7900 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 prefix;
a7910 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 73 69 /* Add si
a7920 67 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 gn */. if
a7930 28 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 ( flag_alternate
a7940 66 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70 form && infop->p
a7950 72 65 66 69 78 20 29 7b 20 20 20 20 20 20 2f 2a refix ){ /*
a7960 20 41 64 64 20 22 30 22 20 6f 72 20 22 30 78 22 Add "0" or "0x"
a7970 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 6f */. co
a7980 6e 73 74 20 63 68 61 72 20 2a 70 72 65 3b 0a 20 nst char *pre;.
a7990 20 20 20 20 20 20 20 20 20 63 68 61 72 20 78 3b char x;
a79a0 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 20 3d . pre =
a79b0 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d &aPrefix[infop-
a79c0 3e 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 20 >prefix];.
a79d0 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a 70 for(; (x=(*p
a79e0 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 20 re))!=0; pre++)
a79f0 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b 0a *(--bufpt) = x;.
a7a00 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a7a10 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74 29 length = (int)
a7a20 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d (&buf[etBUFSIZE-
a7a30 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 20 20 1]-bufpt);.
a7a40 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a7a50 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a 20 20 case etFLOAT:.
a7a60 20 20 20 20 63 61 73 65 20 65 74 45 58 50 3a 0a case etEXP:.
a7a70 20 20 20 20 20 20 63 61 73 65 20 65 74 47 45 4e case etGEN
a7a80 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20 72 65 ERIC:. re
a7a90 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 alvalue = va_arg
a7aa0 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23 69 66 (ap,double);.#if
a7ab0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
a7ac0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
a7ad0 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63 if( prec
a7ae0 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 73 ision<0 ) precis
a7af0 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 20 20 ion = 6;
a7b00 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 20 /* Set default
a7b10 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 precision */.
a7b20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 if( precisi
a7b30 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 on>etBUFSIZE/2-1
a7b40 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 0 ) precision =
a7b50 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30 3b 0a etBUFSIZE/2-10;.
a7b60 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c if( real
a7b70 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20 20 20 value<0.0 ){.
a7b80 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 realvalue
a7b90 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b 0a 20 = -realvalue;.
a7ba0 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 prefix
a7bb0 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 7d = '-';. }
a7bc0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a7bd0 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 if( flag_plussig
a7be0 6e 20 29 20 20 20 20 20 20 20 20 20 20 70 72 65 n ) pre
a7bf0 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 fix = '+';.
a7c00 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c else if( fl
a7c10 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 ag_blanksign )
a7c20 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a prefix = ' ';.
a7c30 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 else
a7c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7c50 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 prefix =
a7c60 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 0;. }.
a7c70 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d if( xtype==
a7c80 65 74 47 45 4e 45 52 49 43 20 26 26 20 70 72 65 etGENERIC && pre
a7c90 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65 63 69 cision>0 ) preci
a7ca0 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a 20 20 sion--;.#if 0.
a7cb0 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 69 6e /* Roundin
a7cc0 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42 53 44 g works like BSD
a7cd0 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 61 when the consta
a7ce0 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75 73 65 nt 0.4999 is use
a7cf0 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a 20 20 d. Wierd! */.
a7d00 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 for(idx=pr
a7d10 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 ecision, rounder
a7d20 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30 3b 20 =0.4999; idx>0;
a7d30 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d idx--, rounder*=
a7d40 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 0.1);.#else.
a7d50 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65 73 20 /* It makes
a7d60 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 75 73 more sense to us
a7d70 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20 20 20 e 0.5 */.
a7d80 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 for(idx=precisi
a7d90 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 35 3b on, rounder=0.5;
a7da0 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 idx>0; idx--, r
a7db0 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d 0a 23 ounder*=0.1){}.#
a7dc0 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66 endif. if
a7dd0 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f 41 54 ( xtype==etFLOAT
a7de0 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20 ) realvalue +=
a7df0 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20 rounder;.
a7e00 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 72 65 /* Normalize re
a7e10 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 68 69 alvalue to withi
a7e20 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 61 6c n 10.0 > realval
a7e30 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20 20 20 ue >= 1.0 */.
a7e40 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a 20 20 exp = 0;.
a7e50 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
a7e60 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65 29 72 3IsNaN((double)r
a7e70 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 ealvalue) ){.
a7e80 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 bufpt = "
a7e90 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 20 NaN";.
a7ea0 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 20 length = 3;.
a7eb0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a7ec0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
a7ed0 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e 30 f( realvalue>0.0
a7ee0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 ){. wh
a7ef0 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d ile( realvalue>=
a7f00 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 30 1e32 && exp<=350
a7f10 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d ){ realvalue *=
a7f20 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 3b 1e-32; exp+=32;
a7f30 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 }. whi
a7f40 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 le( realvalue>=1
a7f50 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29 e8 && exp<=350 )
a7f60 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 { realvalue *= 1
a7f70 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 20 e-8; exp+=8; }.
a7f80 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
a7f90 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 realvalue>=10.0
a7fa0 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 && exp<=350 ){ r
a7fb0 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b ealvalue *= 0.1;
a7fc0 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 exp++; }.
a7fd0 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 while( realv
a7fe0 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 61 alue<1e-8 ){ rea
a7ff0 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65 lvalue *= 1e8; e
a8000 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20 xp-=8; }.
a8010 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 while( realva
a8020 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c 76 lue<1.0 ){ realv
a8030 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78 alue *= 10.0; ex
a8040 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 p--; }.
a8050 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b 0a if( exp>350 ){.
a8060 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
a8070 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20 prefix=='-' ){.
a8080 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 buf
a8090 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20 pt = "-Inf";.
a80a0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 }else i
a80b0 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29 f( prefix=='+' )
a80c0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
a80d0 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a bufpt = "+Inf";.
a80e0 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 }els
a80f0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e{.
a8100 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a bufpt = "Inf";.
a8110 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
a8120 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 length
a8130 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
a8140 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 30(bufpt);.
a8150 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a8160 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a8170 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70 }. bufp
a8180 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 t = buf;.
a8190 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 /*. ** I
a81a0 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 70 65 f the field type
a81b0 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c 20 74 is etGENERIC, t
a81c0 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f 20 65 hen convert to e
a81d0 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 20 20 ither etEXP.
a81e0 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c 4f 41 ** or etFLOA
a81f0 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 T, as appropriat
a8200 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 e.. */.
a8210 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 20 3d flag_exp =
a8220 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b 0a 20 xtype==etEXP;.
a8230 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 if( xtype
a8240 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 20 20 !=etFLOAT ){.
a8250 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 realvalue
a8260 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20 += rounder;.
a8270 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76 if( realv
a8280 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72 65 alue>=10.0 ){ re
a8290 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b 20 alvalue *= 0.1;
a82a0 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 exp++; }.
a82b0 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 }. if( x
a82c0 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 type==etGENERIC
a82d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61 ){. fla
a82e0 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f 61 6c g_rtz = !flag_al
a82f0 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 20 20 ternateform;.
a8300 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3c 2d if( exp<-
a8310 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 73 69 4 || exp>precisi
a8320 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 on ){.
a8330 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 50 3b xtype = etEXP;
a8340 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
a8350 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72 {. pr
a8360 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 ecision = precis
a8370 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 20 20 ion - exp;.
a8380 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 xtype = e
a8390 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 20 20 tFLOAT;.
a83a0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
a83b0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61 e{. fla
a83c0 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 20 20 g_rtz = 0;.
a83d0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
a83e0 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b xtype==etEXP ){
a83f0 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d 20 . e2 =
a8400 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0;. }else
a8410 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d {. e2 =
a8420 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a exp;. }.
a8430 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30 3b nsd = 0;
a8440 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64 70 . flag_dp
a8450 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30 20 = (precision>0
a8460 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61 6c 74 ?1:0) | flag_alt
a8470 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c 61 ernateform | fla
a8480 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20 20 g_altform2;.
a8490 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e 20 /* The sign
a84a0 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65 20 in front of the
a84b0 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 number */.
a84c0 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b 0a if( prefix ){.
a84d0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
a84e0 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a 20 t++) = prefix;.
a84f0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a8500 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f 72 /* Digits prior
a8510 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c 20 to the decimal
a8520 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 point */.
a8530 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20 20 if( e2<0 ){.
a8540 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
a8550 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 ) = '0';.
a8560 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a8570 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20 65 for(; e2>=0; e
a8580 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2--){.
a8590 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65 *(bufpt++) = e
a85a0 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 6c t_getdigit(&real
a85b0 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 20 value,&nsd);.
a85c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a85d0 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 }. /* Th
a85e0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 e decimal point
a85f0 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 */. if( f
a8600 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 20 lag_dp ){.
a8610 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
a8620 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a '.';. }.
a8630 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20 64 /* "0" d
a8640 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 20 igits after the
a8650 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62 75 decimal point bu
a8660 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 t before the fir
a8670 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69 st. ** si
a8680 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 20 gnificant digit
a8690 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f of the number */
a86a0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32 2b . for(e2+
a86b0 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 73 69 +; e2<0; precisi
a86c0 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20 20 on--, e2++){.
a86d0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
a86e0 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a 20 20 recision>0 );.
a86f0 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
a8700 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 +) = '0';.
a8710 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 }. /* S
a8720 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 ignificant digit
a8730 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69 s after the deci
a8740 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 mal point */.
a8750 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 72 65 while( (pre
a8760 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a 20 cision--)>0 ){.
a8770 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
a8780 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69 ++) = et_getdigi
a8790 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 t(&realvalue,&ns
a87a0 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 d);. }.
a87b0 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 /* Remove
a87c0 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 61 trailing zeros a
a87d0 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e 6f nd the "." if no
a87e0 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20 74 digits follow t
a87f0 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20 20 he "." */.
a8800 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20 26 if( flag_rtz &
a8810 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 & flag_dp ){.
a8820 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 62 75 while( bu
a8830 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20 2a fpt[-1]=='0' ) *
a8840 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a 20 (--bufpt) = 0;.
a8850 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
a8860 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20 20 bufpt>buf );.
a8870 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70 if( bufp
a8880 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20 t[-1]=='.' ){.
a8890 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c if( fl
a88a0 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a 20 ag_altform2 ){.
a88b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 *(b
a88c0 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 ufpt++) = '0';.
a88d0 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }else
a88e0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
a88f0 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a *(--bufpt) = 0;.
a8900 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
a8910 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a8920 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 }. /* A
a8930 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73 75 dd the "eNNN" su
a8940 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20 20 ffix */.
a8950 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c 20 if( flag_exp ||
a8960 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b 0a xtype==etEXP ){.
a8970 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
a8980 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b 69 t++) = aDigits[i
a8990 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a nfop->charset];.
a89a0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 if( ex
a89b0 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 p<0 ){.
a89c0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
a89d0 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 3b '-'; exp = -exp;
a89e0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
a89f0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 {. *(
a8a00 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b 0a bufpt++) = '+';.
a8a10 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
a8a20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d 31 if( exp>=1
a8a30 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 00 ){.
a8a40 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 *(bufpt++) = (
a8a50 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 29 2b char)((exp/100)+
a8a60 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 '0'); /*
a8a70 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 100's digit */.
a8a80 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20 25 exp %
a8a90 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20 20 = 100;.
a8aa0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 }. *(b
a8ab0 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 ufpt++) = (char)
a8ac0 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 20 20 (exp/10+'0');
a8ad0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 27 /* 10'
a8ae0 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 s digit */.
a8af0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
a8b00 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 30 2b = (char)(exp%10+
a8b10 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20 '0');
a8b20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20 2a /* 1's digit *
a8b30 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 /. }.
a8b40 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b 0a *bufpt = 0;.
a8b50 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
a8b60 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65 72 converted number
a8b70 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e 64 is in buf[] and
a8b80 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 zero terminated
a8b90 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20 20 . Output it..
a8ba0 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 ** Note tha
a8bb0 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20 t the number is
a8bc0 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72 64 in the usual ord
a8bd0 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 64 er, not reversed
a8be0 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20 20 as with.
a8bf0 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e 76 ** integer conv
a8c00 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 ersions. */.
a8c10 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e length = (in
a8c20 74 29 28 62 75 66 70 74 2d 62 75 66 29 3b 0a 20 t)(bufpt-buf);.
a8c30 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
a8c40 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 uf;.. /*
a8c50 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 41 Special case: A
a8c60 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 dd leading zeros
a8c70 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a 65 72 if the flag_zer
a8c80 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 20 20 opad flag is.
a8c90 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e 64 20 ** set and
a8ca0 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 74 20 we are not left
a8cb0 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 20 20 justified */.
a8cc0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 if( flag_ze
a8cd0 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 5f 6c ropad && !flag_l
a8ce0 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 6c 65 eftjustify && le
a8cf0 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b 0a 20 ngth < width){.
a8d00 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a int i;.
a8d10 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 50 int nP
a8d20 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c 65 6e ad = width - len
a8d30 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 20 66 gth;. f
a8d40 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e 3d 6e or(i=width; i>=n
a8d50 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 Pad; i--){.
a8d60 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 5d 20 bufpt[i]
a8d70 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 5d 3b = bufpt[i-nPad];
a8d80 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
a8d90 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 66 69 i = prefi
a8da0 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 20 20 x!=0;.
a8db0 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 29 20 while( nPad-- )
a8dc0 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 30 27 bufpt[i++] = '0'
a8dd0 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 ;. leng
a8de0 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 20 20 th = width;.
a8df0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
a8e00 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a8e10 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a 0a 20 case etSIZE:.
a8e20 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72 67 28 *(va_arg(
a8e30 61 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41 63 63 ap,int*)) = pAcc
a8e40 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20 um->nChar;.
a8e50 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 length = widt
a8e60 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 h = 0;. b
a8e70 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
a8e80 20 65 74 50 45 52 43 45 4e 54 3a 0a 20 20 20 20 etPERCENT:.
a8e90 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 27 25 27 buf[0] = '%'
a8ea0 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 ;. bufpt
a8eb0 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 6c = buf;. l
a8ec0 65 6e 67 74 68 20 3d 20 31 3b 0a 20 20 20 20 20 ength = 1;.
a8ed0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a8ee0 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a 20 20 case etCHARX:.
a8ef0 20 20 20 20 20 20 63 20 3d 20 76 61 5f 61 72 67 c = va_arg
a8f00 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 (ap,int);.
a8f10 20 20 62 75 66 5b 30 5d 20 3d 20 28 63 68 61 72 buf[0] = (char
a8f20 29 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 )c;. if(
a8f30 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a precision>=0 ){.
a8f40 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 for(id
a8f50 78 3d 31 3b 20 69 64 78 3c 70 72 65 63 69 73 69 x=1; idx<precisi
a8f60 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 75 66 5b 69 on; idx++) buf[i
a8f70 64 78 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a 20 dx] = (char)c;.
a8f80 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 length
a8f90 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 = precision;.
a8fa0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a8fb0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 31 3b length =1;
a8fc0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a8fd0 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a bufpt = buf;.
a8fe0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
a8ff0 20 20 20 20 20 63 61 73 65 20 65 74 53 54 52 49 case etSTRI
a9000 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 NG:. case e
a9010 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20 20 20 tDYNSTRING:.
a9020 20 20 20 20 62 75 66 70 74 20 3d 20 76 61 5f 61 bufpt = va_a
a9030 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20 rg(ap,char*);.
a9040 20 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d if( bufpt=
a9050 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
a9060 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 20 20 20 bufpt = "";.
a9070 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 78 74 }else if( xt
a9080 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 49 4e 47 ype==etDYNSTRING
a9090 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 45 ){. zE
a90a0 78 74 72 61 20 3d 20 62 75 66 70 74 3b 0a 20 20 xtra = bufpt;.
a90b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a90c0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 if( precision>=0
a90d0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f ){. fo
a90e0 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c 65 6e 67 r(length=0; leng
a90f0 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 26 26 20 th<precision &&
a9100 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d 3b 20 6c bufpt[length]; l
a9110 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 20 20 20 ength++){}.
a9120 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a9130 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c length = sql
a9140 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75 66 ite3Strlen30(buf
a9150 70 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 pt);. }.
a9160 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a9170 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 53 case etSQLES
a9180 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61 73 65 CAPE:. case
a9190 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a 0a 20 etSQLESCAPE2:.
a91a0 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 case etSQLE
a91b0 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20 20 20 SCAPE3: {.
a91c0 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 2c 20 69 int i, j, n, i
a91d0 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 snull;. i
a91e0 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 nt needQuote;.
a91f0 20 20 20 20 20 20 63 68 61 72 20 63 68 3b 0a 20 char ch;.
a9200 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d 20 char q =
a9210 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 ((xtype==etSQLES
a9220 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29 CAPE3)?'"':'\'')
a9230 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61 ; /* Quote cha
a9240 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 racter */.
a9250 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d char *escarg =
a9260 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a va_arg(ap,char*
a9270 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c );. isnul
a9280 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20 l = escarg==0;.
a9290 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c if( isnul
a92a0 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78 74 l ) escarg = (xt
a92b0 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 ype==etSQLESCAPE
a92c0 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e 2 ? "NULL" : "(N
a92d0 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20 ULL)");.
a92e0 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65 for(i=n=0; (ch=e
a92f0 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b scarg[i])!=0; i+
a9300 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
a9310 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a ( ch==q ) n++;.
a9320 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a9330 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69 needQuote = !i
a9340 73 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d snull && xtype==
a9350 65 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20 etSQLESCAPE2;.
a9360 20 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31 n += i + 1
a9370 20 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a + needQuote*2;.
a9380 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74 if( n>et
a9390 42 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 BUFSIZE ){.
a93a0 20 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78 bufpt = zEx
a93b0 74 72 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c tra = sqlite3Mal
a93c0 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 loc( n );.
a93d0 20 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 if( bufpt==0
a93e0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a93f0 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 pAccum->mallocFa
a9400 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 iled = 1;.
a9410 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
a9420 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a9430 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a9440 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a bufpt = buf;.
a9450 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a9460 20 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 j = 0;.
a9470 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 if( needQuote )
a9480 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b bufpt[j++] = q;
a9490 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 . for(i=0
a94a0 3b 20 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 ; (ch=escarg[i])
a94b0 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 !=0; i++){.
a94c0 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 bufpt[j++]
a94d0 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 20 20 = ch;.
a94e0 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66 70 if( ch==q ) bufp
a94f0 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 t[j++] = ch;.
a9500 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
a9510 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 62 f( needQuote ) b
a9520 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 ufpt[j++] = q;.
a9530 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d 20 bufpt[j]
a9540 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e = 0;. len
a9550 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 gth = j;.
a9560 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69 6f /* The precisio
a9570 6e 20 69 73 20 69 67 6e 6f 72 65 64 20 6f 6e 20 n is ignored on
a9580 25 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20 20 20 %q and %Q */.
a9590 20 20 20 20 20 2f 2a 20 69 66 28 20 70 72 65 63 /* if( prec
a95a0 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63 ision>=0 && prec
a95b0 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c ision<length ) l
a95c0 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f ength = precisio
a95d0 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 n; */. br
a95e0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
a95f0 20 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a case etTOKEN:
a9600 20 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e {. Token
a9610 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72 *pToken = va_ar
a9620 67 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20 g(ap, Token*);.
a9630 20 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65 if( pToke
a9640 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 n ){. s
a9650 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
a9660 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f pend(pAccum, (co
a9670 6e 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e nst char*)pToken
a9680 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b ->z, pToken->n);
a9690 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a96a0 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 length = widt
a96b0 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 h = 0;. b
a96c0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
a96d0 20 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49 case etSRCLI
a96e0 53 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 ST: {. Sr
a96f0 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61 cList *pSrc = va
a9700 5f 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74 _arg(ap, SrcList
a9710 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 *);. int
a9720 6b 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 k = va_arg(ap, i
a9730 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72 nt);. str
a9740 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
a9750 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d *pItem = &pSrc-
a9760 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61 >a[k];. a
a9770 73 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b ssert( k>=0 && k
a9780 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 <pSrc->nSrc );.
a9790 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d if( pItem
a97a0 2d 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a 20 ->zDatabase ){.
a97b0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
a97c0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
a97d0 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44 Accum, pItem->zD
a97e0 61 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20 atabase, -1);.
a97f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 sqlite3S
a9800 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 trAccumAppend(pA
a9810 63 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20 ccum, ".", 1);.
a9820 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a9830 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a9840 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 Append(pAccum, p
a9850 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29 Item->zName, -1)
a9860 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 ;. length
a9870 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 = width = 0;.
a9880 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a9890 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 }. }/* End
a98a0 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 68 65 switch over the
a98b0 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a format type */.
a98c0 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 /*. ** Th
a98d0 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f e text of the co
a98e0 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e nversion is poin
a98f0 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 70 74 ted to by "bufpt
a9900 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20 " and is. **
a9910 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 63 74 "length" charact
a9920 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 ers long. The f
a9930 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 22 77 ield width is "w
a9940 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a idth". Do. *
a9950 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20 20 * the output..
a9960 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 66 6c */. if( !fl
a9970 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 29 ag_leftjustify )
a9980 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 65 72 {. register
a9990 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 int nspace;.
a99a0 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64 74 nspace = widt
a99b0 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 h-length;.
a99c0 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a if( nspace>0 ){.
a99d0 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 53 70 appendSp
a99e0 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 ace(pAccum, nspa
a99f0 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ce);. }.
a9a00 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74 }. if( lengt
a9a10 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c h>0 ){. sql
a9a20 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
a9a30 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74 nd(pAccum, bufpt
a9a40 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d , length);. }
a9a50 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 . if( flag_le
a9a60 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20 ftjustify ){.
a9a70 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 register int
a9a80 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 nspace;. ns
a9a90 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e pace = width-len
a9aa0 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e gth;. if( n
a9ab0 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 space>0 ){.
a9ac0 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 28 70 appendSpace(p
a9ad0 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a Accum, nspace);.
a9ae0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
a9af0 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a if( zExtra ){.
a9b00 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
a9b10 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 ee(zExtra);.
a9b20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 }. }/* End for
a9b30 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66 6f loop over the fo
a9b40 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d rmat string */.}
a9b50 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 74 /* End of funct
a9b60 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 ion */../*.** Ap
a9b70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f 66 20 pend N bytes of
a9b80 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74 text from z to t
a9b90 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 he StrAccum obje
a9ba0 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ct..*/.SQLITE_PR
a9bb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a9bc0 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
a9bd0 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f (StrAccum *p, co
a9be0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
a9bf0 20 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f N){. if( p->to
a9c00 6f 42 69 67 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 oBig | p->malloc
a9c10 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 Failed ){. re
a9c20 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 turn;. }. if(
a9c30 4e 3c 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 N<0 ){. N = s
a9c40 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
a9c50 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d );. }. if( N==
a9c60 30 20 7c 7c 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 0 || z==0 ){.
a9c70 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
a9c80 66 28 20 70 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d f( p->nChar+N >=
a9c90 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 p->nAlloc ){.
a9ca0 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 char *zNew;.
a9cb0 20 20 69 66 28 20 21 70 2d 3e 75 73 65 4d 61 6c if( !p->useMal
a9cc0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e loc ){. p->
a9cd0 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 tooBig = 1;.
a9ce0 20 20 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 N = p->nAlloc
a9cf0 2d 20 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b 0a - p->nChar - 1;.
a9d00 20 20 20 20 20 20 69 66 28 20 4e 3c 3d 30 20 29 if( N<=0 )
a9d10 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
a9d20 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
a9d30 6c 73 65 7b 0a 20 20 20 20 20 20 69 36 34 20 73 lse{. i64 s
a9d40 7a 4e 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72 3b zNew = p->nChar;
a9d50 0a 20 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d 20 . szNew +=
a9d60 4e 20 2b 20 31 3b 0a 20 20 20 20 20 20 69 66 28 N + 1;. if(
a9d70 20 73 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41 6c szNew > p->mxAl
a9d80 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 loc ){. s
a9d90 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 qlite3StrAccumRe
a9da0 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 set(p);.
a9db0 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 p->tooBig = 1;.
a9dc0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
a9dd0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a9de0 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 p->nAlloc =
a9df0 28 69 6e 74 29 73 7a 4e 65 77 3b 0a 20 20 20 20 (int)szNew;.
a9e00 20 20 7d 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d }. zNew =
a9e10 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
a9e20 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 41 Raw(p->db, p->nA
a9e30 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 20 20 69 66 lloc );. if
a9e40 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 ( zNew ){.
a9e50 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 70 memcpy(zNew, p
a9e60 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 6e 43 68 61 ->zText, p->nCha
a9e70 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 r);. sqli
a9e80 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 te3StrAccumReset
a9e90 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e (p);. p->
a9ea0 7a 54 65 78 74 20 3d 20 7a 4e 65 77 3b 0a 20 20 zText = zNew;.
a9eb0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a9ec0 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c p->mallocFail
a9ed0 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 ed = 1;.
a9ee0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 sqlite3StrAccumR
a9ef0 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 eset(p);.
a9f00 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
a9f10 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d . }. }. mem
a9f20 63 70 79 28 26 70 2d 3e 7a 54 65 78 74 5b 70 2d cpy(&p->zText[p-
a9f30 3e 6e 43 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b 0a >nChar], z, N);.
a9f40 20 20 70 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e 3b p->nChar += N;
a9f50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 69 73 68 .}../*.** Finish
a9f60 20 6f 66 66 20 61 20 73 74 72 69 6e 67 20 62 79 off a string by
a9f70 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 69 74 20 making sure it
a9f80 69 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 is zero-terminat
a9f90 65 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ed..** Return a
a9fa0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 pointer to the r
a9fb0 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 2e esulting string.
a9fc0 20 20 52 65 74 75 72 6e 20 61 20 4e 55 4c 4c 0a Return a NULL.
a9fd0 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 61 6e ** pointer if an
a9fe0 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 y kind of error
a9ff0 77 61 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e was encountered.
aa000 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
aa010 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
aa020 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53 StrAccumFinish(S
aa030 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 trAccum *p){. i
aa040 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 f( p->zText ){.
aa050 20 20 20 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e p->zText[p->n
aa060 43 68 61 72 5d 20 3d 20 30 3b 0a 20 20 20 20 69 Char] = 0;. i
aa070 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 f( p->useMalloc
aa080 26 26 20 70 2d 3e 7a 54 65 78 74 3d 3d 70 2d 3e && p->zText==p->
aa090 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20 70 zBase ){. p
aa0a0 2d 3e 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 65 ->zText = sqlite
aa0b0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 3DbMallocRaw(p->
aa0c0 64 62 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 20 29 db, p->nChar+1 )
aa0d0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 7a ;. if( p->z
aa0e0 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Text ){.
aa0f0 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78 74 2c memcpy(p->zText,
aa100 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e 6e 43 p->zBase, p->nC
aa110 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65 har+1);. }e
aa120 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e lse{. p->
aa130 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
aa140 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
aa150 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e }. return p->
aa160 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 zText;.}../*.**
aa170 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63 75 Reset an StrAccu
aa180 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c 61 m string. Recla
aa190 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64 20 im all malloced
aa1a0 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 memory..*/.SQLIT
aa1b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
aa1c0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 qlite3StrAccumRe
aa1d0 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a 70 29 set(StrAccum *p)
aa1e0 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 {. if( p->zText
aa1f0 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 20 !=p->zBase ){.
aa200 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
aa210 70 2d 3e 64 62 2c 20 70 2d 3e 7a 54 65 78 74 29 p->db, p->zText)
aa220 3b 0a 20 20 7d 0a 20 20 70 2d 3e 7a 54 65 78 74 ;. }. p->zText
aa230 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 = 0;.}../*.** I
aa240 6e 69 74 69 61 6c 69 7a 65 20 61 20 73 74 72 69 nitialize a stri
aa250 6e 67 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 2a ng accumulator.*
aa260 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
aa270 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
aa280 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63 AccumInit(StrAcc
aa290 75 6d 20 2a 70 2c 20 63 68 61 72 20 2a 7a 42 61 um *p, char *zBa
aa2a0 73 65 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6d se, int n, int m
aa2b0 78 29 7b 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d x){. p->zText =
aa2c0 20 70 2d 3e 7a 42 61 73 65 20 3d 20 7a 42 61 73 p->zBase = zBas
aa2d0 65 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 30 3b 0a e;. p->db = 0;.
aa2e0 20 20 70 2d 3e 6e 43 68 61 72 20 3d 20 30 3b 0a p->nChar = 0;.
aa2f0 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b p->nAlloc = n;
aa300 0a 20 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 . p->mxAlloc =
aa310 6d 78 3b 0a 20 20 70 2d 3e 75 73 65 4d 61 6c 6c mx;. p->useMall
aa320 6f 63 20 3d 20 31 3b 0a 20 20 70 2d 3e 74 6f 6f oc = 1;. p->too
aa330 42 69 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 6d 61 Big = 0;. p->ma
aa340 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a llocFailed = 0;.
aa350 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 }../*.** Print i
aa360 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 nto memory obtai
aa370 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d ned from sqliteM
aa380 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68 alloc(). Use th
aa390 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d e internal.** %-
aa3a0 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e conversion exten
aa3b0 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 sions..*/.SQLITE
aa3c0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
aa3d0 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 qlite3VMPrintf(s
aa3e0 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
aa3f0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
aa400 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20 va_list ap){.
aa410 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20 char *z;. char
aa420 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 zBase[SQLITE_PRI
aa430 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 NT_BUF_SIZE];.
aa440 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20 StrAccum acc;.
aa450 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 sqlite3StrAccumI
aa460 6e 69 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c nit(&acc, zBase,
aa470 20 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a sizeof(zBase),.
aa480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aa490 20 20 20 20 20 20 64 62 20 3f 20 64 62 2d 3e 61 db ? db->a
aa4a0 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
aa4b0 49 54 5f 4c 45 4e 47 54 48 5d 20 3a 20 53 51 4c IT_LENGTH] : SQL
aa4c0 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b ITE_MAX_LENGTH);
aa4d0 0a 20 20 61 63 63 2e 64 62 20 3d 20 64 62 3b 0a . acc.db = db;.
aa4e0 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 sqlite3VXPrint
aa4f0 66 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72 6d f(&acc, 1, zForm
aa500 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 at, ap);. z = s
aa510 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 qlite3StrAccumFi
aa520 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69 66 nish(&acc);. if
aa530 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69 6c ( acc.mallocFail
aa540 65 64 20 26 26 20 64 62 20 29 7b 0a 20 20 20 20 ed && db ){.
aa550 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
aa560 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
aa570 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 rn z;.}../*.** P
aa580 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 rint into memory
aa590 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
aa5a0 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 qliteMalloc().
aa5b0 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c Use the internal
aa5c0 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e .** %-conversion
aa5d0 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a extensions..*/.
aa5e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
aa5f0 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 har *sqlite3MPri
aa600 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ntf(sqlite3 *db,
aa610 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
aa620 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
aa630 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 _list ap;. char
aa640 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 *z;. va_start(
aa650 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 ap, zFormat);.
aa660 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 z = sqlite3VMPri
aa670 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c ntf(db, zFormat,
aa680 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
aa690 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a p);. return z;.
aa6a0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 }../*.** Like sq
aa6b0 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 lite3MPrintf(),
aa6c0 62 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 but call sqlite3
aa6d0 44 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 DbFree() on zStr
aa6e0 20 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e after formattin
aa6f0 67 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 g.** the string
aa700 61 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 and before retur
aa710 6e 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 nning. This rou
aa720 74 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 tine is intended
aa730 20 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 to be used.** t
aa740 6f 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 o modify an exis
aa750 74 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f ting string. Fo
aa760 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a r example:.**.**
aa770 20 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 x = sqlit
aa780 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c e3MPrintf(db, x,
aa790 20 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66 "prefix %s suff
aa7a0 69 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 ix", x);.**.*/.S
aa7b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
aa7c0 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 ar *sqlite3MAppe
aa7d0 6e 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ndf(sqlite3 *db,
aa7e0 20 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e char *zStr, con
aa7f0 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
aa800 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 , ...){. va_lis
aa810 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b t ap;. char *z;
aa820 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 . va_start(ap,
aa830 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 zFormat);. z =
aa840 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 sqlite3VMPrintf(
aa850 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 db, zFormat, ap)
aa860 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
aa870 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
aa880 64 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 db, zStr);. ret
aa890 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn z;.}../*.**
aa8a0 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 Print into memor
aa8b0 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 y obtained from
aa8c0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
aa8d0 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 . Omit the inte
aa8e0 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 rnal.** %-conver
aa8f0 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e sion extensions.
aa900 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
aa910 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 har *sqlite3_vmp
aa920 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 rintf(const char
aa930 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 *zFormat, va_li
aa940 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a st ap){. char *
aa950 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b z;. char zBase[
aa960 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 SQLITE_PRINT_BUF
aa970 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 _SIZE];. StrAcc
aa980 75 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 um acc;.#ifndef
aa990 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
aa9a0 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 INIT. if( sqlit
aa9b0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
aa9c0 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 ) return 0;.#end
aa9d0 69 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 if. sqlite3StrA
aa9e0 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a ccumInit(&acc, z
aa9f0 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 Base, sizeof(zBa
aaa00 73 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f se), SQLITE_MAX_
aaa10 4c 45 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 LENGTH);. sqlit
aaa20 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c e3VXPrintf(&acc,
aaa30 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 0, zFormat, ap)
aaa40 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 ;. z = sqlite3S
aaa50 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 trAccumFinish(&a
aaa60 63 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b cc);. return z;
aaa70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 .}../*.** Print
aaa80 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
aaa90 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
aaaa0 33 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 3_malloc()(). O
aaab0 6d 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c mit the internal
aaac0 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e .** %-conversion
aaad0 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a extensions..*/.
aaae0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
aaaf0 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 *sqlite3_mprintf
aab00 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f (const char *zFo
aab10 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
aab20 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 _list ap;. char
aab30 20 2a 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c *z;.#ifndef SQL
aab40 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 ITE_OMIT_AUTOINI
aab50 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f T. if( sqlite3_
aab60 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 initialize() ) r
aab70 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a eturn 0;.#endif.
aab80 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
aab90 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 Format);. z = s
aaba0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 qlite3_vmprintf(
aabb0 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
aabc0 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 va_end(ap);. re
aabd0 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn z;.}../*.**
aabe0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
aabf0 66 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 f() works like s
aac00 6e 70 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 nprintf() except
aac10 20 74 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 that it ignores
aac20 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 the.** current
aac30 6c 6f 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e locale settings.
aac40 20 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 This is import
aac50 61 6e 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 ant for SQLite b
aac60 65 63 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 ecause we.** are
aac70 20 6e 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 not able to use
aac80 20 61 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 a "," as the de
aac90 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 cimal point in p
aaca0 6c 61 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a lace of "." as.*
aacb0 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 73 * specified by s
aacc0 6f 6d 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a ome locales..*/.
aacd0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
aace0 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 *sqlite3_snprint
aacf0 66 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a f(int n, char *z
aad00 42 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Buf, const char
aad10 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
aad20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f char *z;. va_
aad30 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 list ap;. StrAc
aad40 63 75 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 cum acc;.. if(
aad50 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 n<=0 ){. retu
aad60 72 6e 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 rn zBuf;. }. s
aad70 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e qlite3StrAccumIn
aad80 69 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e it(&acc, zBuf, n
aad90 2c 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d , 0);. acc.useM
aada0 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f alloc = 0;. va_
aadb0 73 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 start(ap,zFormat
aadc0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 );. sqlite3VXPr
aadd0 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 intf(&acc, 0, zF
aade0 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 ormat, ap);. va
aadf0 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 _end(ap);. z =
aae00 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
aae10 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 inish(&acc);. r
aae20 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 eturn z;.}..#if
aae30 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
aae40 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 EBUG)./*.** A ve
aae50 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 rsion of printf(
aae60 29 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e ) that understan
aae70 64 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 ds %lld. Used f
aae80 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a or debugging..**
aae90 20 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 The printf() bu
aaea0 69 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 ilt into some ve
aaeb0 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 rsions of window
aaec0 73 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 s does not under
aaed0 73 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e stand %lld.** an
aaee0 64 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 d segfaults if y
aaef0 6f 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e ou give it a lon
aaf00 67 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 g long int..*/.S
aaf10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
aaf20 69 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 id sqlite3DebugP
aaf30 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 rintf(const char
aaf40 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
aaf50 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
aaf60 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 StrAccum acc;.
aaf70 20 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b char zBuf[500];
aaf80 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 . sqlite3StrAcc
aaf90 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 umInit(&acc, zBu
aafa0 66 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c f, sizeof(zBuf),
aafb0 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 0);. acc.useMa
aafc0 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 lloc = 0;. va_s
aafd0 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 tart(ap,zFormat)
aafe0 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 ;. sqlite3VXPri
aaff0 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f ntf(&acc, 0, zFo
ab000 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f rmat, ap);. va_
ab010 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 end(ap);. sqlit
ab020 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 e3StrAccumFinish
ab030 28 26 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 (&acc);. fprint
ab040 66 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a f(stdout,"%s", z
ab050 42 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 Buf);. fflush(s
ab060 74 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 tdout);.}.#endif
ab070 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
ab080 2a 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e * End of printf.
ab090 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
ab0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ab0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ab0c0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
ab0d0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e * Begin file ran
ab0e0 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dom.c **********
ab0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ab100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ab110 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
ab120 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 tember 15.**.**
ab130 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
ab140 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
ab150 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
ab160 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
ab170 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
ab180 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
ab190 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
ab1a0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
ab1b0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
ab1c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
ab1d0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
ab1e0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
ab1f0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
ab200 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
ab210 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
ab220 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
ab230 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
ab240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ab250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ab260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ab270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ab280 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
ab290 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
ab2a0 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 ode to implement
ab2b0 20 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d a pseudo-random
ab2c0 20 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 number.** gener
ab2d0 61 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 ator (PRNG) for
ab2e0 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 SQLite..**.** Ra
ab2f0 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 ndom numbers are
ab300 20 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 used by some of
ab310 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61 the database ba
ab320 63 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a ckends in order.
ab330 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 ** to generate r
ab340 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 andom integer ke
ab350 79 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 ys for tables or
ab360 20 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 random filename
ab370 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61 s..**.** $Id: ra
ab380 6e 64 6f 6d 2e 63 2c 76 20 31 2e 32 39 20 32 30 ndom.c,v 1.29 20
ab390 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 08/12/10 19:26:2
ab3a0 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 4 drh Exp $.*/..
ab3b0 0a 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 ./* All threads
ab3c0 73 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 share a single r
ab3d0 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e andom number gen
ab3e0 65 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 erator..** This
ab3f0 73 74 72 75 63 74 75 72 65 20 69 73 20 74 68 65 structure is the
ab400 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f current state o
ab410 66 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e f the generator.
ab420 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
ab430 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c E_WSD struct sql
ab440 69 74 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20 ite3PrngType {.
ab450 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 unsigned char i
ab460 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 sInit;
ab470 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 /* True if initi
ab480 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69 alized */. unsi
ab490 67 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 gned char i, j;
ab4a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
ab4b0 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f ate variables */
ab4c0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
ab4d0 20 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20 s[256];
ab4e0 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 /* State varia
ab4f0 62 6c 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 bles */.} sqlite
ab500 33 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 3Prng;../*.** Ge
ab510 74 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 t a single 8-bit
ab520 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 random value fr
ab530 6f 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e om the RC4 PRNG.
ab540 20 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d The Mutex.** m
ab550 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c ust be held whil
ab560 65 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 e executing this
ab570 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
ab580 57 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 Why not just use
ab590 20 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f a library rando
ab5a0 6d 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 m generator like
ab5b0 20 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 lrand48() for t
ab5c0 68 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 his?.** Because
ab5d0 74 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 the OP_NewRowid
ab5e0 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 opcode in the VD
ab5f0 42 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 BE depends on ha
ab600 76 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 ving a very.** g
ab610 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 ood source of ra
ab620 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 ndom numbers. T
ab630 68 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 he lrand48() lib
ab640 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 rary function ma
ab650 79 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f y.** well be goo
ab660 64 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d d enough. But m
ab670 61 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 aybe not. Or ma
ab680 79 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 ybe lrand48() ha
ab690 73 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 s some.** subtle
ab6a0 20 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d problems on som
ab6b0 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 e systems that c
ab6c0 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c ould cause probl
ab6d0 65 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 ems. It is hard
ab6e0 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f .** to know. To
ab6f0 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 minimize the ri
ab700 73 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 sk of problems d
ab710 75 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 ue to bad lrand4
ab720 38 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 8().** implement
ab730 61 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 ations, SQLite u
ab740 73 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 ses this random
ab750 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
ab760 20 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 based.** on RC4
ab770 2c 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 , which we know
ab780 77 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e works very well.
ab790 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 .**.** (Later):
ab7a0 20 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 Actually, OP_Ne
ab7b0 77 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 wRowid does not
ab7c0 64 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 depend on a good
ab7d0 20 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 source of.** ra
ab7e0 6e 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 ndomness any mor
ab7f0 65 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 e. But we will
ab800 6c 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 leave this code
ab810 69 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e in all the same.
ab820 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61 .*/.static u8 ra
ab830 6e 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a ndomByte(void){.
ab840 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
ab850 74 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77 t;... /* The "w
ab860 73 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69 sdPrng" macro wi
ab870 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 ll resolve to th
ab880 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 e pseudo-random
ab890 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
ab8a0 0a 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74 . ** state vect
ab8b0 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 or. If writable
ab8c0 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 static data is
ab8d0 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 unsupported on t
ab8e0 68 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20 he target,. **
ab8f0 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 we have to locat
ab900 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 e the state vect
ab910 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 or at run-time.
ab920 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d In the more com
ab930 6d 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 mon. ** case wh
ab940 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 ere writable sta
ab950 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 tic data is supp
ab960 6f 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63 orted, wsdPrng c
ab970 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c an refer directl
ab980 79 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73 y. ** to the "s
ab990 71 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 74 qlite3Prng" stat
ab9a0 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 e vector declare
ab9b0 64 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 d above.. */.#i
ab9c0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
ab9d0 5f 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 71 _WSD. struct sq
ab9e0 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70 lite3PrngType *p
ab9f0 20 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 = &GLOBAL(struc
aba00 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 t sqlite3PrngTyp
aba10 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b e, sqlite3Prng);
aba20 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e .# define wsdPrn
aba30 67 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 g p[0].#else.# d
aba40 65 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71 efine wsdPrng sq
aba50 6c 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66 lite3Prng.#endif
aba60 0a 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 ... /* Initiali
aba70 7a 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 ze the state of
aba80 74 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 the random numbe
aba90 72 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 r generator once
abaa0 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 ,. ** the first
abab0 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 time this routi
abac0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 ne is called. T
abad0 68 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f he seed value do
abae0 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 es. ** not need
abaf0 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f to contain a lo
abb00 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 t of randomness
abb10 73 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74 since we are not
abb20 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 . ** trying to
abb30 64 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70 do secure encryp
abb40 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67 tion or anything
abb50 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20 like that....
abb60 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 **. ** Nothing
abb70 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20 in this file or
abb80 61 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e anywhere else in
abb90 20 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79 SQLite does any
abba0 20 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e kind of. ** en
abbb0 63 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52 cryption. The R
abbc0 43 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 C4 algorithm is
abbd0 62 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20 being used as a
abbe0 50 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e PRNG (pseudo-ran
abbf0 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 dom. ** number
abc00 67 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61 generator) not a
abc10 73 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20 s an encryption
abc20 64 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 device.. */. i
abc30 66 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e f( !wsdPrng.isIn
abc40 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b it ){. int i;
abc50 0a 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d . char k[256]
abc60 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 ;. wsdPrng.j
abc70 3d 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 = 0;. wsdPrng
abc80 2e 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 .i = 0;. sqli
abc90 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 te3OsRandomness(
abca0 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 sqlite3_vfs_find
abcb0 28 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20 (0), 256, k);.
abcc0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 for(i=0; i<256
abcd0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 ; i++){. ws
abce0 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 dPrng.s[i] = (u8
abcf0 29 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f )i;. }. fo
abd00 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b r(i=0; i<256; i+
abd10 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e +){. wsdPrn
abd20 67 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 g.j += wsdPrng.s
abd30 5b 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 [i] + k[i];.
abd40 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b t = wsdPrng.s[
abd50 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 wsdPrng.j];.
abd60 20 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 wsdPrng.s[wsdP
abd70 72 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 rng.j] = wsdPrng
abd80 2e 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 .s[i];. wsd
abd90 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 Prng.s[i] = t;.
abda0 20 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 }. wsdPrng
abdb0 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d .isInit = 1;. }
abdc0 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 .. /* Generate
abdd0 61 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c and return singl
abde0 65 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 e random byte.
abdf0 2a 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b */. wsdPrng.i++
abe00 3b 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e ;. t = wsdPrng.
abe10 73 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 s[wsdPrng.i];.
abe20 77 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a wsdPrng.j += t;.
abe30 20 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 wsdPrng.s[wsdP
abe40 72 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 rng.i] = wsdPrng
abe50 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 .s[wsdPrng.j];.
abe60 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
abe70 6e 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b ng.j] = t;. t +
abe80 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 = wsdPrng.s[wsdP
abe90 72 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e rng.i];. return
abea0 20 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d wsdPrng.s[t];.}
abeb0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e ../*.** Return N
abec0 20 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a random bytes..*
abed0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
abee0 64 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d d sqlite3_random
abef0 6e 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 ness(int N, void
abf00 20 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 *pBuf){. unsig
abf10 6e 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d ned char *zBuf =
abf20 20 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 pBuf;.#if SQLIT
abf30 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 E_THREADSAFE. s
abf40 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
abf50 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
abf60 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
abf70 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 UTEX_STATIC_PRNG
abf80 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 );.#endif. sqli
abf90 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
abfa0 6d 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 mutex);. while(
abfb0 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 N-- ){. *(zB
abfc0 75 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 uf++) = randomBy
abfd0 74 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 te();. }. sqli
abfe0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
abff0 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 mutex);.}..#ifnd
ac000 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 ef SQLITE_OMIT_B
ac010 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a UILTIN_TEST./*.*
ac020 2a 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 * For testing pu
ac030 72 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 rposes, we somet
ac040 69 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 imes want to pre
ac050 73 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20 serve the state
ac060 6f 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 of.** PRNG and r
ac070 65 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 estore the PRNG
ac080 74 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61 to its saved sta
ac090 74 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 te at a later ti
ac0a0 6d 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 me, or.** to res
ac0b0 65 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 et the PRNG to i
ac0c0 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 ts initial state
ac0d0 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
ac0e0 73 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 s accomplish.**
ac0f0 74 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a those tasks..**.
ac100 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 ** The sqlite3_t
ac110 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e est_control() in
ac120 74 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 terface calls th
ac130 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a ese routines to.
ac140 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 ** control the P
ac150 52 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 RNG..*/.static S
ac160 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
ac170 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
ac180 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e sqlite3SavedPrn
ac190 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 g;.SQLITE_PRIVAT
ac1a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 E void sqlite3Pr
ac1b0 6e 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 ngSaveState(void
ac1c0 29 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 ){. memcpy(.
ac1d0 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 &GLOBAL(struct
ac1e0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c sqlite3PrngType,
ac1f0 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e sqlite3SavedPrn
ac200 67 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 g),. &GLOBAL(
ac210 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 struct sqlite3Pr
ac220 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 ngType, sqlite3P
ac230 72 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 rng),. sizeof
ac240 28 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 (sqlite3Prng).
ac250 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
ac260 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
ac270 50 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 PrngRestoreState
ac280 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 (void){. memcpy
ac290 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 (. &GLOBAL(st
ac2a0 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 ruct sqlite3Prng
ac2b0 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e Type, sqlite3Prn
ac2c0 67 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 g),. &GLOBAL(
ac2d0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 struct sqlite3Pr
ac2e0 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 ngType, sqlite3S
ac2f0 61 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73 avedPrng),. s
ac300 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e izeof(sqlite3Prn
ac310 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 g). );.}.SQLITE
ac320 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
ac330 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 lite3PrngResetSt
ac340 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f ate(void){. GLO
ac350 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 BAL(struct sqlit
ac360 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 e3PrngType, sqli
ac370 74 65 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 te3Prng).isInit
ac380 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a = 0;.}.#endif /*
ac390 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 SQLITE_OMIT_BUI
ac3a0 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a LTIN_TEST */../*
ac3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
ac3c0 64 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a d of random.c **
ac3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
ac400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
ac410 67 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a gin file utf.c *
ac420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
ac450 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 .** 2004 April 1
ac460 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 3.**.** The auth
ac470 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
ac480 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
ac490 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
ac4a0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
ac4b0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
ac4c0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
ac4d0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
ac4e0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
ac4f0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
ac500 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
ac510 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
ac520 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
ac530 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
ac540 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
ac550 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
ac560 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
ac570 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
ac580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac5c0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
ac5d0 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 ntains routines
ac5e0 75 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 used to translat
ac5f0 65 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c e between UTF-8,
ac600 20 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 .** UTF-16, UTF
ac610 2d 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 -16BE, and UTF-1
ac620 36 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6LE..**.** $Id:
ac630 75 74 66 2e 63 2c 76 20 31 2e 37 30 20 32 30 30 utf.c,v 1.70 200
ac640 38 2f 31 32 2f 31 30 20 32 32 3a 33 30 3a 32 35 8/12/10 22:30:25
ac650 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2a 0a shane Exp $.**.
ac660 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d ** Notes on UTF-
ac670 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 8:.**.** Byte-
ac680 30 20 20 20 20 42 79 74 65 2d 31 20 20 20 20 42 0 Byte-1 B
ac690 79 74 65 2d 32 20 20 20 20 42 79 74 65 2d 33 20 yte-2 Byte-3
ac6a0 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 Value.** 0xx
ac6b0 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 xxxxx
ac6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac6d0 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 30 00000000 0
ac6e0 30 30 30 30 30 30 30 20 30 78 78 78 78 78 78 78 0000000 0xxxxxxx
ac6f0 0a 2a 2a 20 20 31 31 30 79 79 79 79 79 20 20 31 .** 110yyyyy 1
ac700 30 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 0xxxxxx
ac710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 00
ac720 30 30 30 30 30 30 20 30 30 30 30 30 79 79 79 20 000000 00000yyy
ac730 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 yyxxxxxx.** 111
ac740 30 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 0zzzz 10yyyyyy
ac750 20 31 30 78 78 78 78 78 78 20 20 20 20 20 20 20 10xxxxxx
ac760 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 00000000 z
ac770 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 zzzyyyy yyxxxxxx
ac780 0a 2a 2a 20 20 31 31 31 31 30 75 75 75 20 20 31 .** 11110uuu 1
ac790 30 75 75 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 0uuzzzz 10yyyyy
ac7a0 79 20 20 31 30 78 78 78 78 78 78 20 20 20 30 30 y 10xxxxxx 00
ac7b0 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 0uuuuu zzzzyyyy
ac7c0 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a yyxxxxxx.**.**.*
ac7d0 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 * Notes on UTF-1
ac7e0 36 3a 20 20 28 77 69 74 68 20 77 77 77 77 2b 31 6: (with wwww+1
ac7f0 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 ==uuuuu).**.**
ac800 20 20 20 20 57 6f 72 64 2d 30 20 20 20 20 20 20 Word-0
ac810 20 20 20 20 20 20 20 20 20 57 6f 72 64 2d 31 20 Word-1
ac820 20 20 20 20 20 20 20 20 20 56 61 6c 75 65 0a 2a Value.*
ac830 2a 20 20 31 31 30 31 31 30 77 77 20 77 77 7a 7a * 110110ww wwzz
ac840 7a 7a 79 79 20 20 20 31 31 30 31 31 31 79 79 20 zzyy 110111yy
ac850 79 79 78 78 78 78 78 78 20 20 20 20 30 30 30 75 yyxxxxxx 000u
ac860 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 uuuu zzzzyyyy yy
ac870 78 78 78 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 xxxxxx.** zzzzy
ac880 79 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20 yyy yyxxxxxx
ac890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac8a0 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 00000000 zzz
ac8b0 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a zyyyy yyxxxxxx.*
ac8c0 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 *.**.** BOM or B
ac8d0 79 74 65 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a yte Order Mark:.
ac8e0 2a 2a 20 20 20 20 20 30 78 66 66 20 30 78 66 65 ** 0xff 0xfe
ac8f0 20 20 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e little-endian
ac900 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a utf-16 follows.
ac910 2a 2a 20 20 20 20 20 30 78 66 65 20 30 78 66 66 ** 0xfe 0xff
ac920 20 20 20 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 big-endian ut
ac930 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a f-16 follows.**.
ac940 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
ac950 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 49 ** Include vdbeI
ac960 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 nt.h in the midd
ac970 6c 65 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a le of utf.c ****
ac980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac990 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
ac9a0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 ** Begin file vd
ac9b0 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a beInt.h ********
ac9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac9e0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 */./*.** 2003 Se
ac9f0 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 ptember 6.**.**
aca00 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
aca10 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
aca20 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
aca30 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
aca40 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
aca50 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
aca60 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
aca70 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
aca80 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
aca90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
acaa0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
acab0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
acac0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
acad0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
acae0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
acaf0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
acb00 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
acb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
acb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
acb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
acb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
acb50 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
acb60 20 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66 is the header f
acb70 69 6c 65 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 ile for informat
acb80 69 6f 6e 20 74 68 61 74 20 69 73 20 70 72 69 76 ion that is priv
acb90 61 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 ate to the.** VD
acba0 42 45 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d BE. This inform
acbb0 61 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c ation used to al
acbc0 6c 20 62 65 20 61 74 20 74 68 65 20 74 6f 70 20 l be at the top
acbd0 6f 66 20 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a of the single.**
acbe0 20 73 6f 75 72 63 65 20 63 6f 64 65 20 66 69 6c source code fil
acbf0 65 20 22 76 64 62 65 2e 63 22 2e 20 20 57 68 65 e "vdbe.c". Whe
acc00 6e 20 74 68 61 74 20 66 69 6c 65 20 62 65 63 61 n that file beca
acc10 6d 65 20 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 me too big (over
acc20 0a 2a 2a 20 36 30 30 30 20 6c 69 6e 65 73 20 6c .** 6000 lines l
acc30 6f 6e 67 29 20 69 74 20 77 61 73 20 73 70 6c 69 ong) it was spli
acc40 74 20 75 70 20 69 6e 74 6f 20 73 65 76 65 72 61 t up into severa
acc50 6c 20 73 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 l smaller files
acc60 61 6e 64 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 and.** this head
acc70 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 er information w
acc80 61 73 20 66 61 63 74 6f 72 65 64 20 6f 75 74 2e as factored out.
acc90 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 .**.** $Id: vdbe
acca0 49 6e 74 2e 68 2c 76 20 31 2e 31 36 31 20 32 30 Int.h,v 1.161 20
accb0 30 39 2f 30 31 2f 30 35 20 31 38 3a 30 32 3a 32 09/01/05 18:02:2
accc0 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 7 drh Exp $.*/.#
accd0 69 66 6e 64 65 66 20 5f 56 44 42 45 49 4e 54 5f ifndef _VDBEINT_
acce0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56 44 42 45 H_.#define _VDBE
accf0 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 69 6e INT_H_../*.** in
acd00 74 54 6f 4b 65 79 28 29 20 61 6e 64 20 6b 65 79 tToKey() and key
acd10 54 6f 49 6e 74 28 29 20 75 73 65 64 20 74 6f 20 ToInt() used to
acd20 74 72 61 6e 73 66 6f 72 6d 20 74 68 65 20 72 6f transform the ro
acd30 77 69 64 2e 20 20 42 75 74 20 77 69 74 68 0a 2a wid. But with.*
acd40 2a 20 74 68 65 20 6c 61 74 65 73 74 20 76 65 72 * the latest ver
acd50 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 65 73 sions of the des
acd60 69 67 6e 20 74 68 65 79 20 61 72 65 20 6e 6f 2d ign they are no-
acd70 6f 70 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ops..*/.#define
acd80 6b 65 79 54 6f 49 6e 74 28 58 29 20 20 20 28 58 keyToInt(X) (X
acd90 29 0a 23 64 65 66 69 6e 65 20 69 6e 74 54 6f 4b ).#define intToK
acda0 65 79 28 58 29 20 20 20 28 58 29 0a 0a 0a 2f 2a ey(X) (X).../*
acdb0 0a 2a 2a 20 53 51 4c 20 69 73 20 74 72 61 6e 73 .** SQL is trans
acdc0 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 73 65 71 lated into a seq
acdd0 75 65 6e 63 65 20 6f 66 20 69 6e 73 74 72 75 63 uence of instruc
acde0 74 69 6f 6e 73 20 74 6f 20 62 65 0a 2a 2a 20 65 tions to be.** e
acdf0 78 65 63 75 74 65 64 20 62 79 20 61 20 76 69 72 xecuted by a vir
ace00 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 45 tual machine. E
ace10 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ach instruction
ace20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a is an instance.*
ace30 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
ace40 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f ng structure..*/
ace50 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
ace60 56 64 62 65 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a VdbeOp Op;../*.*
ace70 2a 20 42 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 73 * Boolean values
ace80 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 .*/.typedef unsi
ace90 67 6e 65 64 20 63 68 61 72 20 42 6f 6f 6c 3b 0a gned char Bool;.
acea0 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 ./*.** A cursor
aceb0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 69 6e 74 is a pointer int
acec0 6f 20 61 20 73 69 6e 67 6c 65 20 42 54 72 65 65 o a single BTree
aced0 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 62 61 within a databa
acee0 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 se file..** The
acef0 63 75 72 73 6f 72 20 63 61 6e 20 73 65 65 6b 20 cursor can seek
acf00 74 6f 20 61 20 42 54 72 65 65 20 65 6e 74 72 79 to a BTree entry
acf10 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c with a particul
acf20 61 72 20 6b 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f ar key, or.** lo
acf30 6f 70 20 6f 76 65 72 20 61 6c 6c 20 65 6e 74 72 op over all entr
acf40 69 65 73 20 6f 66 20 74 68 65 20 42 74 72 65 65 ies of the Btree
acf50 2e 20 20 59 6f 75 20 63 61 6e 20 61 6c 73 6f 20 . You can also
acf60 69 6e 73 65 72 74 20 6e 65 77 20 42 54 72 65 65 insert new BTree
acf70 0a 2a 2a 20 65 6e 74 72 69 65 73 20 6f 72 20 72 .** entries or r
acf80 65 74 72 69 65 76 65 20 74 68 65 20 6b 65 79 20 etrieve the key
acf90 6f 72 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 or data from the
acfa0 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 entry that the
acfb0 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 63 75 72 cursor.** is cur
acfc0 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 rently pointing
acfd0 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 to..** .** Every
acfe0 20 63 75 72 73 6f 72 20 74 68 61 74 20 74 68 65 cursor that the
acff0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
ad000 20 68 61 73 20 6f 70 65 6e 20 69 73 20 72 65 70 has open is rep
ad010 72 65 73 65 6e 74 65 64 20 62 79 20 61 6e 0a 2a resented by an.*
ad020 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 * instance of th
ad030 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
ad040 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 cture..**.** If
ad050 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 69 the VdbeCursor.i
ad060 73 54 72 69 67 67 65 72 52 6f 77 20 66 6c 61 67 sTriggerRow flag
ad070 20 69 73 20 73 65 74 20 69 74 20 6d 65 61 6e 73 is set it means
ad080 20 74 68 61 74 20 74 68 69 73 20 63 75 72 73 6f that this curso
ad090 72 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 r is.** really a
ad0a0 20 73 69 6e 67 6c 65 20 72 6f 77 20 74 68 61 74 single row that
ad0b0 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 represents the
ad0c0 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 NEW or OLD pseud
ad0d0 6f 2d 74 61 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 o-table of.** a
ad0e0 72 6f 77 20 74 72 69 67 67 65 72 2e 20 20 54 68 row trigger. Th
ad0f0 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 e data for the r
ad100 6f 77 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 ow is stored in
ad110 56 64 62 65 43 75 72 73 6f 72 2e 70 44 61 74 61 VdbeCursor.pData
ad120 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72 6f 77 69 and.** the rowi
ad130 64 20 69 73 20 69 6e 20 56 64 62 65 43 75 72 73 d is in VdbeCurs
ad140 6f 72 2e 69 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 or.iKey..*/.stru
ad150 63 74 20 56 64 62 65 43 75 72 73 6f 72 20 7b 0a ct VdbeCursor {.
ad160 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
ad170 73 6f 72 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 sor; /* The c
ad180 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 20 ursor structure
ad190 6f 66 20 74 68 65 20 62 61 63 6b 65 6e 64 20 2a of the backend *
ad1a0 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 /. int iDb;
ad1b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
ad1c0 65 78 20 6f 66 20 63 75 72 73 6f 72 20 64 61 74 ex of cursor dat
ad1d0 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 abase in db->aDb
ad1e0 5b 5d 20 28 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 [] (or -1) */.
ad1f0 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 i64 lastRowid;
ad200 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 6f /* Last ro
ad210 77 69 64 20 66 72 6f 6d 20 61 20 4e 65 78 74 20 wid from a Next
ad220 6f 72 20 4e 65 78 74 49 64 78 20 6f 70 65 72 61 or NextIdx opera
ad230 74 69 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 65 tion */. i64 ne
ad240 78 74 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 xtRowid;
ad250 2f 2a 20 4e 65 78 74 20 72 6f 77 69 64 20 72 65 /* Next rowid re
ad260 74 75 72 6e 65 64 20 62 79 20 4f 50 5f 4e 65 77 turned by OP_New
ad270 52 6f 77 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 Rowid */. Bool
ad280 7a 65 72 6f 65 64 3b 20 20 20 20 20 20 20 20 20 zeroed;
ad290 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 65 72 6f /* True if zero
ad2a0 65 64 20 6f 75 74 20 61 6e 64 20 72 65 61 64 79 ed out and ready
ad2b0 20 66 6f 72 20 72 65 75 73 65 20 2a 2f 0a 20 20 for reuse */.
ad2c0 42 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61 6c 69 Bool rowidIsVali
ad2d0 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 d; /* True if
ad2e0 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20 76 61 lastRowid is va
ad2f0 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74 lid */. Bool at
ad300 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 2f First; /
ad310 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74 69 * True if pointi
ad320 6e 67 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72 ng to first entr
ad330 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 75 73 65 52 y */. Bool useR
ad340 61 6e 64 6f 6d 52 6f 77 69 64 3b 20 20 2f 2a 20 andomRowid; /*
ad350 47 65 6e 65 72 61 74 65 20 6e 65 77 20 72 65 63 Generate new rec
ad360 6f 72 64 20 6e 75 6d 62 65 72 73 20 73 65 6d 69 ord numbers semi
ad370 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42 -randomly */. B
ad380 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b 20 20 20 20 ool nullRow;
ad390 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
ad3a0 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f pointing to a ro
ad3b0 77 20 77 69 74 68 20 6e 6f 20 64 61 74 61 20 2a w with no data *
ad3c0 2f 0a 20 20 42 6f 6f 6c 20 6e 65 78 74 52 6f 77 /. Bool nextRow
ad3d0 69 64 56 61 6c 69 64 3b 20 20 2f 2a 20 54 72 75 idValid; /* Tru
ad3e0 65 20 69 66 20 74 68 65 20 6e 65 78 74 52 6f 77 e if the nextRow
ad3f0 69 64 20 66 69 65 6c 64 20 69 73 20 76 61 6c 69 id field is vali
ad400 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 70 73 65 75 d */. Bool pseu
ad410 64 6f 54 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 doTable; /*
ad420 54 68 69 73 20 69 73 20 61 20 4e 45 57 20 6f 72 This is a NEW or
ad430 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c OLD pseudo-tabl
ad440 65 73 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 es of a trigger
ad450 2a 2f 0a 20 20 42 6f 6f 6c 20 65 70 68 65 6d 50 */. Bool ephemP
ad460 73 65 75 64 6f 54 61 62 6c 65 3b 0a 20 20 42 6f seudoTable;. Bo
ad470 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 65 74 ol deferredMovet
ad480 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 74 6f o; /* A call to
ad490 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
ad4a0 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 64 20 eto() is needed
ad4b0 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 62 6c */. Bool isTabl
ad4c0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 e; /* Tr
ad4d0 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 72 65 ue if a table re
ad4e0 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 72 20 quiring integer
ad4f0 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 keys */. Bool i
ad500 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 sIndex;
ad510 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 69 6e /* True if an in
ad520 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6b dex containing k
ad530 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 eys only - no da
ad540 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f 76 65 ta */. i64 move
ad550 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 2f 2a toTarget; /*
ad560 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 Argument to the
ad570 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 74 65 deferred sqlite
ad580 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 2a 3BtreeMoveto() *
ad590 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 /. Btree *pBt;
ad5a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 70 /* Sep
ad5b0 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c 64 69 arate file holdi
ad5c0 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 ng temporary tab
ad5d0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 le */. int nDat
ad5e0 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a a; /*
ad5f0 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
ad600 20 69 6e 20 70 44 61 74 61 20 2a 2f 0a 20 20 63 in pData */. c
ad610 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20 20 har *pData;
ad620 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 /* Data for
ad630 20 61 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 a NEW or OLD ps
ad640 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 eudo-table */.
ad650 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20 i64 iKey;
ad660 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 66 6f 72 /* Key for
ad670 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 the NEW or OLD
ad680 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 72 6f 77 pseudo-table row
ad690 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 */. KeyInfo *p
ad6a0 4b 65 79 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 KeyInfo; /* I
ad6b0 6e 66 6f 20 61 62 6f 75 74 20 69 6e 64 65 78 20 nfo about index
ad6c0 6b 65 79 73 20 6e 65 65 64 65 64 20 62 79 20 69 keys needed by i
ad6d0 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a ndex cursors */.
ad6e0 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 int nField;
ad6f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
ad700 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 r of fields in t
ad710 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 he header */. i
ad720 36 34 20 73 65 71 43 6f 75 6e 74 3b 20 20 20 20 64 seqCount;
ad730 20 20 20 20 20 2f 2a 20 53 65 71 75 65 6e 63 65 /* Sequence
ad740 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71 counter */. sq
ad750 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
ad760 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 20 r *pVtabCursor;
ad770 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 66 /* The cursor f
ad780 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 or a virtual tab
ad790 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 le */. const sq
ad7a0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
ad7b0 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d 6f odule; /* Mo
ad7c0 64 75 6c 65 20 66 6f 72 20 63 75 72 73 6f 72 20 dule for cursor
ad7d0 70 56 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a 0a pVtabCursor */..
ad7e0 20 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66 6f /* Cached info
ad7f0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 rmation about th
ad800 65 20 68 65 61 64 65 72 20 66 6f 72 20 74 68 65 e header for the
ad810 20 64 61 74 61 20 72 65 63 6f 72 64 20 74 68 61 data record tha
ad820 74 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73 6f t the. ** curso
ad830 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 r is currently p
ad840 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c ointing to. Onl
ad850 79 20 76 61 6c 69 64 20 69 66 20 63 61 63 68 65 y valid if cache
ad860 56 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a 20 Valid is true..
ad870 20 2a 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70 ** aRow might p
ad880 6f 69 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72 oint to (ephemer
ad890 61 6c 29 20 64 61 74 61 20 66 6f 72 20 74 68 65 al) data for the
ad8a0 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72 current row, or
ad8b0 20 69 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 it might. ** b
ad8c0 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 e NULL.. */. i
ad8d0 6e 74 20 63 61 63 68 65 53 74 61 74 75 73 3b 20 nt cacheStatus;
ad8e0 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73 /* Cache is
ad8f0 20 76 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d valid if this m
ad900 61 74 63 68 65 73 20 56 64 62 65 2e 63 61 63 68 atches Vdbe.cach
ad910 65 43 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 eCtr */. int pa
ad920 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20 yloadSize;
ad930 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 /* Total number
ad940 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 of bytes in the
ad950 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 record */. u32
ad960 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 *aType;
ad970 20 20 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73 /* Type values
ad980 20 66 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 for all entries
ad990 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a in the record *
ad9a0 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 /. u32 *aOffset
ad9b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 ; /* Cac
ad9c0 68 65 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74 hed offsets to t
ad9d0 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 he start of each
ad9e0 20 63 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f columns data */
ad9f0 0a 20 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20 . u8 *aRow;
ada00 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
ada10 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 for the current
ada20 20 72 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 row, if all on
ada30 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 one page */.};.t
ada40 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 ypedef struct Vd
ada50 62 65 43 75 72 73 6f 72 20 56 64 62 65 43 75 72 beCursor VdbeCur
ada60 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61 sor;../*.** A va
ada70 6c 75 65 20 66 6f 72 20 56 64 62 65 43 75 72 73 lue for VdbeCurs
ada80 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 74 68 or.cacheValid th
ada90 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 61 63 at means the cac
adaa0 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 76 he is always inv
adab0 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 alid..*/.#define
adac0 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 0a 0a CACHE_STALE 0..
adad0 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c 79 /*.** Internally
adae0 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e 69 70 , the vdbe manip
adaf0 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 61 6c ulates nearly al
adb00 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 73 20 l SQL values as
adb10 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 Mem.** structure
adb20 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72 75 s. Each Mem stru
adb30 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75 6c ct may cache mul
adb40 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74 61 tiple representa
adb50 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0a 2a tions (string,.*
adb60 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29 20 * integer etc.)
adb70 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 of the same valu
adb80 65 2e 20 20 41 20 76 61 6c 75 65 20 28 61 6e 64 e. A value (and
adb90 20 74 68 65 72 65 66 6f 72 65 20 4d 65 6d 20 73 therefore Mem s
adba0 74 72 75 63 74 75 72 65 29 0a 2a 2a 20 68 61 73 tructure).** has
adbb0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 the following p
adbc0 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a roperties:.**.**
adbd0 20 45 61 63 68 20 76 61 6c 75 65 20 68 61 73 20 Each value has
adbe0 61 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 2e a manifest type.
adbf0 20 54 68 65 20 6d 61 6e 69 66 65 73 74 20 74 79 The manifest ty
adc00 70 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 pe of the value
adc10 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 20 4d stored.** in a M
adc20 65 6d 20 73 74 72 75 63 74 20 69 73 20 72 65 74 em struct is ret
adc30 75 72 6e 65 64 20 62 79 20 74 68 65 20 4d 65 6d urned by the Mem
adc40 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61 63 72 6f Type(Mem*) macro
adc50 2e 20 54 68 65 20 74 79 70 65 20 69 73 0a 2a 2a . The type is.**
adc60 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e one of SQLITE_N
adc70 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 ULL, SQLITE_INTE
adc80 47 45 52 2c 20 53 51 4c 49 54 45 5f 52 45 41 4c GER, SQLITE_REAL
adc90 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 20 6f 72 , SQLITE_TEXT or
adca0 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c 4f 42 2e .** SQLITE_BLOB.
adcb0 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 20 7b .*/.struct Mem {
adcc0 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 . union {. i
adcd0 36 34 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 64 i;
adce0 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 /* Integer va
adcf0 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 lue. */. int
add00 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 20 20 nZero;
add10 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 62 69 74 /* Used when bit
add20 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 20 73 65 74 MEM_Zero is set
add30 20 69 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 in flags */.
add40 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 FuncDef *pDef;
add50 20 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c /* Used onl
add60 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 y when flags==ME
add70 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 20 52 6f 77 M_Agg */. Row
add80 53 65 74 20 2a 70 52 6f 77 53 65 74 3b 20 20 20 Set *pRowSet;
add90 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 /* Used only wh
adda0 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 52 6f en flags==MEM_Ro
addb0 77 53 65 74 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 wSet */. } u;.
addc0 20 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20 double r;
addd0 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c /* Real val
adde0 75 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 ue */. sqlite3
addf0 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 *db; /* T
ade00 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 he associated da
ade10 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
ade20 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 n */. char *z;
ade30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
ade40 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c ring or BLOB val
ade50 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 ue */. int n;
ade60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
ade70 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
ade80 65 72 73 20 69 6e 20 73 74 72 69 6e 67 20 76 61 ers in string va
ade90 6c 75 65 2c 20 65 78 63 6c 75 64 69 6e 67 20 27 lue, excluding '
adea0 5c 30 27 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 \0' */. u16 fla
adeb0 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 gs; /*
adec0 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e Some combination
aded0 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 of MEM_Null, ME
adee0 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 M_Str, MEM_Dyn,
adef0 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 20 74 79 etc. */. u8 ty
adf00 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a pe; /*
adf10 20 4f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e One of SQLITE_N
adf20 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 ULL, SQLITE_TEXT
adf30 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 , SQLITE_INTEGER
adf40 2c 20 65 74 63 20 2a 2f 0a 20 20 75 38 20 20 65 , etc */. u8 e
adf50 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f nc; /
adf60 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 * SQLITE_UTF8, S
adf70 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 QLITE_UTF16BE, S
adf80 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 2a 2f QLITE_UTF16LE */
adf90 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
adfa0 76 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20 void *); /* If
adfb0 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 not null, call t
adfc0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 his function to
adfd0 64 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a delete Mem.z */.
adfe0 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b char *zMalloc;
adff0 20 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63 /* Dynamic
ae000 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 buffer allocate
ae010 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c d by sqlite3_mal
ae020 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 loc() */.};../*
ae030 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 One or more of t
ae040 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 he following fla
ae050 67 73 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e gs are set to in
ae060 64 69 63 61 74 65 20 74 68 65 20 76 61 6c 69 64 dicate the valid
ae070 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 OK.** representa
ae080 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c tions of the val
ae090 75 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 ue stored in the
ae0a0 20 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a Mem struct..**.
ae0b0 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75 ** If the MEM_Nu
ae0c0 6c 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 ll flag is set,
ae0d0 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 then the value i
ae0e0 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 s an SQL NULL va
ae0f0 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 lue..** No other
ae100 20 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 flags may be se
ae110 74 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a t in this case..
ae120 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d **.** If the MEM
ae130 5f 53 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 _Str flag is set
ae140 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e then Mem.z poin
ae150 74 73 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 ts at a string r
ae160 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a epresentation..*
ae170 2a 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 * Usually this i
ae180 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 s encoded in the
ae190 20 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e same unicode en
ae1a0 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 coding as the ma
ae1b0 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28 in.** database (
ae1c0 73 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78 see below for ex
ae1d0 63 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68 ceptions). If th
ae1e0 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 e MEM_Term flag
ae1f0 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20 is also.** set,
ae200 74 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20 then the string
ae210 69 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 is nul terminate
ae220 64 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61 d. The MEM_Int a
ae230 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20 nd MEM_Real .**
ae240 66 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73 flags may coexis
ae250 74 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53 t with the MEM_S
ae260 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d tr flag..**.** M
ae270 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 73 65 ultiple of these
ae280 20 76 61 6c 75 65 73 20 63 61 6e 20 61 70 70 65 values can appe
ae290 61 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73 2e ar in Mem.flags.
ae2a0 20 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a 2a But only one.*
ae2b0 2a 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 * at a time can
ae2c0 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74 79 appear in Mem.ty
ae2d0 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d pe..*/.#define M
ae2e0 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78 30 EM_Null 0x0
ae2f0 30 30 31 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 001 /* Value i
ae300 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e s NULL */.#defin
ae310 65 20 4d 45 4d 5f 53 74 72 20 20 20 20 20 20 20 e MEM_Str
ae320 30 78 30 30 30 32 20 20 20 2f 2a 20 56 61 6c 75 0x0002 /* Valu
ae330 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 2a 2f e is a string */
ae340 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 74 .#define MEM_Int
ae350 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 20 0x0004
ae360 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 6e 20 69 /* Value is an i
ae370 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e nteger */.#defin
ae380 65 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20 20 e MEM_Real
ae390 30 78 30 30 30 38 20 20 20 2f 2a 20 56 61 6c 75 0x0008 /* Valu
ae3a0 65 20 69 73 20 61 20 72 65 61 6c 20 6e 75 6d 62 e is a real numb
ae3b0 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 er */.#define ME
ae3c0 4d 5f 42 6c 6f 62 20 20 20 20 20 20 30 78 30 30 M_Blob 0x00
ae3d0 31 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 10 /* Value is
ae3e0 20 61 20 42 4c 4f 42 20 2a 2f 0a 23 64 65 66 69 a BLOB */.#defi
ae3f0 6e 65 20 4d 45 4d 5f 52 6f 77 53 65 74 20 20 20 ne MEM_RowSet
ae400 20 30 78 30 30 32 30 20 20 20 2f 2a 20 56 61 6c 0x0020 /* Val
ae410 75 65 20 69 73 20 61 20 52 6f 77 53 65 74 20 6f ue is a RowSet o
ae420 62 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 bject */.#define
ae430 20 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 20 20 30 MEM_TypeMask 0
ae440 78 30 30 66 66 20 20 20 2f 2a 20 4d 61 73 6b 20 x00ff /* Mask
ae450 6f 66 20 74 79 70 65 20 62 69 74 73 20 2a 2f 0a of type bits */.
ae460 0a 2f 2a 20 57 68 65 6e 65 76 65 72 20 4d 65 6d ./* Whenever Mem
ae470 20 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c 69 contains a vali
ae480 64 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 d string or blob
ae490 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c representation,
ae4a0 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 one of.** the f
ae4b0 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 6d ollowing flags m
ae4c0 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 64 65 ust be set to de
ae4d0 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 65 6d 6f termine the memo
ae4e0 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a ry management.**
ae4f0 20 70 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d 2e policy for Mem.
ae500 7a 2e 20 20 54 68 65 20 4d 45 4d 5f 54 65 72 6d z. The MEM_Term
ae510 20 66 6c 61 67 20 74 65 6c 6c 73 20 75 73 20 77 flag tells us w
ae520 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
ae530 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 5c e.** string is \
ae540 30 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74 65 000 or \u0000 te
ae550 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a 23 64 65 66 rminated.*/.#def
ae560 69 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20 20 ine MEM_Term
ae570 20 20 30 78 30 32 30 30 20 20 20 2f 2a 20 53 74 0x0200 /* St
ae580 72 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c 20 ring rep is nul
ae590 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 23 64 terminated */.#d
ae5a0 65 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20 20 20 efine MEM_Dyn
ae5b0 20 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a 20 0x0400 /*
ae5c0 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c Need to call sql
ae5d0 69 74 65 46 72 65 65 28 29 20 6f 6e 20 4d 65 6d iteFree() on Mem
ae5e0 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 .z */.#define ME
ae5f0 4d 5f 53 74 61 74 69 63 20 20 20 20 30 78 30 38 M_Static 0x08
ae600 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 00 /* Mem.z po
ae610 69 6e 74 73 20 74 6f 20 61 20 73 74 61 74 69 63 ints to a static
ae620 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 string */.#defi
ae630 6e 65 20 4d 45 4d 5f 45 70 68 65 6d 20 20 20 20 ne MEM_Ephem
ae640 20 30 78 31 30 30 30 20 20 20 2f 2a 20 4d 65 6d 0x1000 /* Mem
ae650 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 .z points to an
ae660 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 ephemeral string
ae670 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
ae680 41 67 67 20 20 20 20 20 20 20 30 78 32 30 30 30 Agg 0x2000
ae690 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e /* Mem.z poin
ae6a0 74 73 20 74 6f 20 61 6e 20 61 67 67 20 66 75 6e ts to an agg fun
ae6b0 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f ction context */
ae6c0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 .#define MEM_Zer
ae6d0 6f 20 20 20 20 20 20 30 78 34 30 30 30 20 20 20 o 0x4000
ae6e0 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e 74 61 69 6e /* Mem.i contain
ae6f0 73 20 63 6f 75 6e 74 20 6f 66 20 30 73 20 61 70 s count of 0s ap
ae700 70 65 6e 64 65 64 20 74 6f 20 62 6c 6f 62 20 2a pended to blob *
ae710 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 /..#ifdef SQLITE
ae720 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 _OMIT_INCRBLOB.
ae730 20 23 75 6e 64 65 66 20 4d 45 4d 5f 5a 65 72 6f #undef MEM_Zero
ae740 0a 20 20 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a . #define MEM_Z
ae750 65 72 6f 20 30 78 30 30 30 30 0a 23 65 6e 64 69 ero 0x0000.#endi
ae760 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 f.../*.** Clear
ae770 61 6e 79 20 65 78 69 73 74 69 6e 67 20 74 79 70 any existing typ
ae780 65 20 66 6c 61 67 73 20 66 72 6f 6d 20 61 20 4d e flags from a M
ae790 65 6d 20 61 6e 64 20 72 65 70 6c 61 63 65 20 74 em and replace t
ae7a0 68 65 6d 20 77 69 74 68 20 66 0a 2a 2f 0a 23 64 hem with f.*/.#d
ae7b0 65 66 69 6e 65 20 4d 65 6d 53 65 74 54 79 70 65 efine MemSetType
ae7c0 46 6c 61 67 28 70 2c 20 66 29 20 5c 0a 20 20 20 Flag(p, f) \.
ae7d0 28 28 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28 ((p)->flags = ((
ae7e0 70 29 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f p)->flags&~(MEM_
ae7f0 54 79 70 65 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72 TypeMask|MEM_Zer
ae800 6f 29 29 7c 66 29 0a 0a 0a 2f 2a 20 41 20 56 64 o))|f).../* A Vd
ae810 62 65 46 75 6e 63 20 69 73 20 6a 75 73 74 20 61 beFunc is just a
ae820 20 46 75 6e 63 44 65 66 20 28 64 65 66 69 6e 65 FuncDef (define
ae830 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 d in sqliteInt.h
ae840 29 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a ) that contains.
ae850 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e ** additional in
ae860 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
ae870 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d auxiliary inform
ae880 61 74 69 6f 6e 20 62 6f 75 6e 64 20 74 6f 20 61 ation bound to a
ae890 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 rguments.** of t
ae8a0 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 he function. Th
ae8b0 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d is is used to im
ae8c0 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 plement the sqli
ae8d0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
ae8e0 29 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 ).** and sqlite3
ae8f0 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 _set_auxdata() A
ae900 50 49 73 2e 20 20 54 68 65 20 22 61 75 78 64 61 PIs. The "auxda
ae910 74 61 22 20 69 73 20 73 6f 6d 65 20 61 75 78 69 ta" is some auxi
ae920 6c 69 61 72 79 20 64 61 74 61 0a 2a 2a 20 74 68 liary data.** th
ae930 61 74 20 63 61 6e 20 62 65 20 61 73 73 6f 63 69 at can be associ
ae940 61 74 65 64 20 77 69 74 68 20 61 20 63 6f 6e 73 ated with a cons
ae950 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 20 74 6f tant argument to
ae960 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 a function. Th
ae970 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 66 75 6e is.** allows fun
ae980 63 74 69 6f 6e 73 20 73 75 63 68 20 61 73 20 22 ctions such as "
ae990 72 65 67 65 78 70 22 20 74 6f 20 63 6f 6d 70 69 regexp" to compi
ae9a0 6c 65 20 74 68 65 69 72 20 63 6f 6e 73 74 61 6e le their constan
ae9b0 74 20 72 65 67 75 6c 61 72 0a 2a 2a 20 65 78 70 t regular.** exp
ae9c0 72 65 73 73 69 6f 6e 20 61 72 67 75 6d 65 6e 74 ression argument
ae9d0 20 6f 6e 63 65 20 61 6e 64 20 72 65 75 73 65 64 once and reused
ae9e0 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 63 6f the compiled co
ae9f0 64 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 0a de for multiple.
aea00 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a ** invocations..
aea10 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 46 75 */.struct VdbeFu
aea20 6e 63 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a nc {. FuncDef *
aea30 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 pFunc;
aea40 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69 /* The defi
aea50 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 nition of the fu
aea60 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 nction */. int
aea70 6e 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 nAux;
aea80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
aea90 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 ber of entries a
aeaa0 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41 llocated for apA
aeab0 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 ux[] */. struct
aeac0 20 41 75 78 44 61 74 61 20 7b 0a 20 20 20 20 76 AuxData {. v
aead0 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 oid *pAux;
aeae0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
aeaf0 41 75 78 20 64 61 74 61 20 66 6f 72 20 74 68 65 Aux data for the
aeb00 20 69 2d 74 68 20 61 72 67 75 6d 65 6e 74 20 2a i-th argument *
aeb10 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65 /. void (*xDe
aeb20 6c 65 74 65 29 28 76 6f 69 64 20 2a 29 3b 20 20 lete)(void *);
aeb30 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f /* Destructo
aeb40 72 20 66 6f 72 20 74 68 65 20 61 75 78 20 64 61 r for the aux da
aeb50 74 61 20 2a 2f 0a 20 20 7d 20 61 70 41 75 78 5b ta */. } apAux[
aeb60 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 1];
aeb70 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f /* One slo
aeb80 74 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74 t for each funct
aeb90 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a ion argument */.
aeba0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 63 };../*.** The "c
aebb0 6f 6e 74 65 78 74 22 20 61 72 67 75 6d 65 6e 74 ontext" argument
aebc0 20 66 6f 72 20 61 20 69 6e 73 74 61 6c 6c 61 62 for a installab
aebd0 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 le function. A
aebe0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a pointer to an.**
aebf0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
aec00 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74 s structure is t
aec10 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
aec20 74 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 t to the routine
aec30 73 20 75 73 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d s used.** implem
aec40 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 ent the SQL func
aec50 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tions..**.** The
aec60 72 65 20 69 73 20 61 20 74 79 70 65 64 65 66 20 re is a typedef
aec70 66 6f 72 20 74 68 69 73 20 73 74 72 75 63 74 75 for this structu
aec80 72 65 20 69 6e 20 73 71 6c 69 74 65 2e 68 2e 20 re in sqlite.h.
aec90 20 53 6f 20 61 6c 6c 20 72 6f 75 74 69 6e 65 73 So all routines
aeca0 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 65 20 70 75 ,.** even the pu
aecb0 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 blic interface t
aecc0 6f 20 53 51 4c 69 74 65 2c 20 63 61 6e 20 75 73 o SQLite, can us
aecd0 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 e a pointer to t
aece0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a his structure..*
aecf0 2a 20 42 75 74 20 74 68 69 73 20 66 69 6c 65 20 * But this file
aed00 69 73 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63 is the only plac
aed10 65 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 e where the inte
aed20 72 6e 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20 rnal details of
aed30 74 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 this.** structur
aed40 65 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a e are known..**.
aed50 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
aed60 65 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73 e is defined ins
aed70 69 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 ide of vdbeInt.h
aed80 20 62 65 63 61 75 73 65 20 69 74 20 75 73 65 73 because it uses
aed90 20 73 75 62 73 74 72 75 63 74 75 72 65 73 0a 2a substructures.*
aeda0 2a 20 28 4d 65 6d 29 20 77 68 69 63 68 20 61 72 * (Mem) which ar
aedb0 65 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64 20 74 e only defined t
aedc0 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 here..*/.struct
aedd0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
aede0 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 {. FuncDef *pFu
aedf0 6e 63 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 nc; /* Poi
aee00 6e 74 65 72 20 74 6f 20 66 75 6e 63 74 69 6f 6e nter to function
aee10 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d information. M
aee20 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a UST BE FIRST */.
aee30 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 VdbeFunc *pVdb
aee40 65 46 75 6e 63 3b 20 20 2f 2a 20 41 75 78 69 6c eFunc; /* Auxil
aee50 61 72 79 20 64 61 74 61 2c 20 69 66 20 63 72 65 ary data, if cre
aee60 61 74 65 64 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73 ated. */. Mem s
aee70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
aee80 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 /* The return v
aee90 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 68 alue is stored h
aeea0 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d ere */. Mem *pM
aeeb0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f em; /
aeec0 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 * Memory cell us
aeed0 65 64 20 74 6f 20 73 74 6f 72 65 20 61 67 67 72 ed to store aggr
aeee0 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 2a 2f egate context */
aeef0 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 3b 20 . int isError;
aef00 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f /* Erro
aef10 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 r code returned
aef20 62 79 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e by the function.
aef30 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 */. CollSeq *p
aef40 43 6f 6c 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43 Coll; /* C
aef50 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
aef60 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 e */.};../*.** A
aef70 20 53 65 74 20 73 74 72 75 63 74 75 72 65 20 69 Set structure i
aef80 73 20 75 73 65 64 20 66 6f 72 20 71 75 69 63 6b s used for quick
aef90 20 74 65 73 74 69 6e 67 20 74 6f 20 73 65 65 20 testing to see
aefa0 69 66 20 61 20 76 61 6c 75 65 0a 2a 2a 20 69 73 if a value.** is
aefb0 20 70 61 72 74 20 6f 66 20 61 20 73 6d 61 6c 6c part of a small
aefc0 20 73 65 74 2e 20 20 53 65 74 73 20 61 72 65 20 set. Sets are
aefd0 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
aefe0 74 20 63 6f 64 65 20 6c 69 6b 65 0a 2a 2a 20 74 t code like.** t
aeff0 68 69 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 his:.**
af000 20 20 20 78 2e 79 20 49 4e 20 28 27 68 69 27 2c x.y IN ('hi',
af010 27 68 6f 6f 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a 'hoo','hum').*/.
af020 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
af030 65 74 20 53 65 74 3b 0a 73 74 72 75 63 74 20 53 et Set;.struct S
af040 65 74 20 7b 0a 20 20 48 61 73 68 20 68 61 73 68 et {. Hash hash
af050 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
af060 20 41 20 73 65 74 20 69 73 20 6a 75 73 74 20 61 A set is just a
af070 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 hash table */.
af080 20 48 61 73 68 45 6c 65 6d 20 2a 70 72 65 76 3b HashElem *prev;
af090 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 /* Previ
af0a0 6f 75 73 6c 79 20 61 63 63 65 73 73 65 64 20 68 ously accessed h
af0b0 61 73 68 20 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b ash elemen */.};
af0c0 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f 6e 74 65 78 ../*.** A Contex
af0d0 74 20 73 74 6f 72 65 73 20 74 68 65 20 6c 61 73 t stores the las
af0e0 74 20 69 6e 73 65 72 74 20 72 6f 77 69 64 2c 20 t insert rowid,
af0f0 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 the last stateme
af100 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c nt change count,
af110 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 75 72 72 .** and the curr
af120 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68 ent statement ch
af130 61 6e 67 65 20 63 6f 75 6e 74 20 28 69 2e 65 2e ange count (i.e.
af140 20 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20 6c changes since l
af150 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 2e 0a ast statement)..
af160 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 6b ** The current k
af170 65 79 6c 69 73 74 20 69 73 20 61 6c 73 6f 20 73 eylist is also s
af180 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e tored in the con
af190 74 65 78 74 2e 0a 2a 2a 20 45 6c 65 6d 65 6e 74 text..** Element
af1a0 73 20 6f 66 20 43 6f 6e 74 65 78 74 20 73 74 72 s of Context str
af1b0 75 63 74 75 72 65 20 74 79 70 65 20 6d 61 6b 65 ucture type make
af1c0 20 75 70 20 74 68 65 20 43 6f 6e 74 65 78 74 53 up the ContextS
af1d0 74 61 63 6b 2c 20 77 68 69 63 68 20 69 73 0a 2a tack, which is.*
af1e0 2a 20 75 70 64 61 74 65 64 20 62 79 20 74 68 65 * updated by the
af1f0 20 43 6f 6e 74 65 78 74 50 75 73 68 20 61 6e 64 ContextPush and
af200 20 43 6f 6e 74 65 78 74 50 6f 70 20 6f 70 63 6f ContextPop opco
af210 64 65 73 20 28 75 73 65 64 20 62 79 20 74 72 69 des (used by tri
af220 67 67 65 72 73 29 2e 0a 2a 2a 20 54 68 65 20 63 ggers)..** The c
af230 6f 6e 74 65 78 74 20 69 73 20 70 75 73 68 65 64 ontext is pushed
af240 20 62 65 66 6f 72 65 20 65 78 65 63 75 74 69 6e before executin
af250 67 20 61 20 74 72 69 67 67 65 72 20 61 20 70 6f g a trigger a po
af260 70 70 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a pped when the.**
af270 20 74 72 69 67 67 65 72 20 66 69 6e 69 73 68 65 trigger finishe
af280 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 s..*/.typedef st
af290 72 75 63 74 20 43 6f 6e 74 65 78 74 20 43 6f 6e ruct Context Con
af2a0 74 65 78 74 3b 0a 73 74 72 75 63 74 20 43 6f 6e text;.struct Con
af2b0 74 65 78 74 20 7b 0a 20 20 69 36 34 20 6c 61 73 text {. i64 las
af2c0 74 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 4c 61 tRowid; /* La
af2d0 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20 st insert rowid
af2e0 28 73 71 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77 (sqlite3.lastRow
af2f0 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 id) */. int nCh
af300 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a 20 53 74 ange; /* St
af310 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 atement changes
af320 28 56 64 62 65 2e 6e 43 68 61 6e 67 65 73 29 20 (Vdbe.nChanges)
af330 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a */.};../*.**
af340 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
af350 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 the virtual mach
af360 69 6e 65 2e 20 20 54 68 69 73 20 73 74 72 75 63 ine. This struc
af370 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 ture contains th
af380 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 e complete.** st
af390 61 74 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 ate of the virtu
af3a0 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a al machine..**.*
af3b0 2a 20 54 68 65 20 22 73 71 6c 69 74 65 33 5f 73 * The "sqlite3_s
af3c0 74 6d 74 22 20 73 74 72 75 63 74 75 72 65 20 70 tmt" structure p
af3d0 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 72 ointer that is r
af3e0 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
af3f0 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20 e3_compile().**
af400 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e is really a poin
af410 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e ter to an instan
af420 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ce of this struc
af430 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ture..**.** The
af440 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 68 6f Vdbe.inVtabMetho
af450 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 d variable is se
af460 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f t to non-zero fo
af470 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20 6f r the duration o
af480 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75 61 6c f.** any virtual
af490 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69 6e table method in
af4a0 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65 20 62 vocations made b
af4b0 79 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 y the vdbe progr
af4c0 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73 65 74 am. It is.** set
af4d0 20 74 6f 20 32 20 66 6f 72 20 78 44 65 73 74 72 to 2 for xDestr
af4e0 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c 73 20 oy method calls
af4f0 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20 6f 74 and 1 for all ot
af500 68 65 72 20 6d 65 74 68 6f 64 73 2e 20 54 68 69 her methods. Thi
af510 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73 s.** variable is
af520 20 75 73 65 64 20 66 6f 72 20 74 77 6f 20 70 75 used for two pu
af530 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 rposes: to allow
af540 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 xDestroy method
af550 73 20 74 6f 20 65 78 65 63 75 74 65 0a 2a 2a 20 s to execute.**
af560 22 44 52 4f 50 20 54 41 42 4c 45 22 20 73 74 61 "DROP TABLE" sta
af570 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f 20 70 tements and to p
af580 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61 73 74 revent some nast
af590 79 20 73 69 64 65 20 65 66 66 65 63 74 73 20 6f y side effects o
af5a0 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c f.** malloc fail
af5b0 75 72 65 20 77 68 65 6e 20 53 51 4c 69 74 65 20 ure when SQLite
af5c0 69 73 20 69 6e 76 6f 6b 65 64 20 72 65 63 75 72 is invoked recur
af5d0 73 69 76 65 6c 79 20 62 79 20 61 20 76 69 72 74 sively by a virt
af5e0 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 ual table .** me
af5f0 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a thod function..*
af600 2f 0a 73 74 72 75 63 74 20 56 64 62 65 20 7b 0a /.struct Vdbe {.
af610 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
af620 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f /* The who
af630 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 le database */.
af640 20 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e Vdbe *pPrev,*pN
af650 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c ext; /* Linked l
af660 69 73 74 20 6f 66 20 56 44 42 45 73 20 77 69 74 ist of VDBEs wit
af670 68 20 74 68 65 20 73 61 6d 65 20 56 64 62 65 2e h the same Vdbe.
af680 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b db */. int nOp;
af690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
af6a0 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 umber of instruc
af6b0 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72 6f tions in the pro
af6c0 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f gram */. int nO
af6d0 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f 2a pAlloc; /*
af6e0 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 Number of slots
af6f0 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 allocated for a
af700 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61 4f Op[] */. Op *aO
af710 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a p; /*
af720 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
af730 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 he virtual machi
af740 6e 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a ne's program */.
af750 20 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 int nLabel;
af760 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
af770 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64 20 2a of labels used *
af780 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 41 6c /. int nLabelAl
af790 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 loc; /* Numbe
af7a0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 r of slots alloc
af7b0 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b 5d ated in aLabel[]
af7c0 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 */. int *aLabe
af7d0 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 l; /* Spa
af7e0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6c ce to hold the l
af7f0 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a abels */. Mem *
af800 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20 2f *apArg; /
af810 2a 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63 * Arguments to c
af820 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 urrently executi
af830 6e 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e ng user function
af840 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e */. Mem *aColN
af850 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6c ame; /* Col
af860 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 65 74 umn names to ret
af870 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 urn */. int nCu
af880 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 rsor; /*
af890 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 Number of slots
af8a0 69 6e 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20 20 in apCsr[] */.
af8b0 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 VdbeCursor **apC
af8c0 73 72 3b 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d 65 sr; /* One eleme
af8d0 6e 74 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 nt of this array
af8e0 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 63 for each open c
af8f0 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e ursor */. int n
af900 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f Var; /
af910 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
af920 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a 2f ies in aVar[] */
af930 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 20 20 . Mem *aVar;
af940 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 /* Values
af950 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69 for the OP_Vari
af960 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a able opcode. */.
af970 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20 char **azVar;
af980 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
af990 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 variables */.
af9a0 69 6e 74 20 6f 6b 56 61 72 3b 20 20 20 20 20 20 int okVar;
af9b0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
af9c0 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e 20 zVar[] has been
af9d0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
af9e0 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20 u32 magic;
af9f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 /* Magi
afa00 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73 61 6e c number for san
afa10 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a ity checking */.
afa20 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 int nMem;
afa30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
afa40 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 6c 6f ber of memory lo
afa50 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c cations currentl
afa60 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 y allocated */.
afa70 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 Mem *aMem;
afa80 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
afa90 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 memory locations
afaa0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61 6c 6c 62 */. int nCallb
afab0 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ack; /*
afac0 20 4e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 62 Number of callb
afad0 61 63 6b 73 20 69 6e 76 6f 6b 65 64 20 73 6f 20 acks invoked so
afae0 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 far */. int cac
afaf0 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20 heCtr;
afb00 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 72 /* VdbeCursor r
afb10 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 74 ow cache generat
afb20 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 ion counter */.
afb30 20 69 6e 74 20 63 6f 6e 74 65 78 74 53 74 61 63 int contextStac
afb40 6b 54 6f 70 3b 20 20 20 20 2f 2a 20 49 6e 64 65 kTop; /* Inde
afb50 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74 x of top element
afb60 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 20 in the context
afb70 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 63 stack */. int c
afb80 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74 68 ontextStackDepth
afb90 3b 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f ; /* The size o
afba0 66 20 74 68 65 20 22 63 6f 6e 74 65 78 74 22 20 f the "context"
afbb0 73 74 61 63 6b 20 2a 2f 0a 20 20 43 6f 6e 74 65 stack */. Conte
afbc0 78 74 20 2a 63 6f 6e 74 65 78 74 53 74 61 63 6b xt *contextStack
afbd0 3b 20 20 2f 2a 20 53 74 61 63 6b 20 75 73 65 64 ; /* Stack used
afbe0 20 62 79 20 6f 70 63 6f 64 65 73 20 43 6f 6e 74 by opcodes Cont
afbf0 65 78 74 50 75 73 68 20 26 20 43 6f 6e 74 65 78 extPush & Contex
afc00 74 50 6f 70 2a 2f 0a 20 20 69 6e 74 20 70 63 3b tPop*/. int pc;
afc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afc20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20 /* The program
afc30 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
afc40 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
afc50 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f /* Value to
afc60 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 75 6e 73 return */. uns
afc70 69 67 6e 65 64 20 75 6e 69 71 75 65 43 6e 74 3b igned uniqueCnt;
afc80 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 /* Used by
afc90 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 77 68 OP_MakeRecord wh
afca0 65 6e 20 50 32 21 3d 30 20 2a 2f 0a 20 20 69 6e en P2!=0 */. in
afcb0 74 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20 t errorAction;
afcc0 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72 /* Recover
afcd0 79 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69 y action to do i
afce0 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72 n case of an err
afcf0 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 54 65 or */. int inTe
afd00 6d 70 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 mpTrans;
afd10 2f 2a 20 54 72 75 65 20 69 66 20 74 65 6d 70 20 /* True if temp
afd20 64 61 74 61 62 61 73 65 20 69 73 20 74 72 61 6e database is tran
afd30 73 61 63 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 69 sactioned */. i
afd40 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 nt nResColumn;
afd50 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
afd60 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 6f of columns in o
afd70 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65 ne row of the re
afd80 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 63 68 sult set */. ch
afd90 61 72 20 2a 2a 61 7a 52 65 73 43 6f 6c 75 6d 6e ar **azResColumn
afda0 3b 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 ; /* Values
afdb0 66 6f 72 20 6f 6e 65 20 72 6f 77 20 6f 66 20 72 for one row of r
afdc0 65 73 75 6c 74 20 2a 2f 20 0a 20 20 63 68 61 72 esult */ . char
afdd0 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 *zErrMsg;
afde0 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 /* Error mes
afdf0 73 61 67 65 20 77 72 69 74 74 65 6e 20 68 65 72 sage written her
afe00 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 73 e */. Mem *pRes
afe10 75 6c 74 53 65 74 3b 20 20 20 20 20 20 20 20 2f ultSet; /
afe20 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 * Pointer to an
afe30 61 72 72 61 79 20 6f 66 20 72 65 73 75 6c 74 73 array of results
afe40 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e */. u8 explain
afe50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
afe60 20 54 72 75 65 20 69 66 20 45 58 50 4c 41 49 4e True if EXPLAIN
afe70 20 70 72 65 73 65 6e 74 20 6f 6e 20 53 51 4c 20 present on SQL
afe80 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20 75 38 20 command */. u8
afe90 63 68 61 6e 67 65 43 6e 74 4f 6e 3b 20 20 20 20 changeCntOn;
afea0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 /* True to
afeb0 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
afec0 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 e-counter */. u
afed0 38 20 65 78 70 69 72 65 64 3b 20 20 20 20 20 20 8 expired;
afee0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
afef0 66 20 74 68 65 20 56 4d 20 6e 65 65 64 73 20 74 f the VM needs t
aff00 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 o be recompiled
aff10 2a 2f 0a 20 20 75 38 20 6d 69 6e 57 72 69 74 65 */. u8 minWrite
aff20 46 69 6c 65 46 6f 72 6d 61 74 3b 20 20 2f 2a 20 FileFormat; /*
aff30 4d 69 6e 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 Minimum file for
aff40 6d 61 74 20 66 6f 72 20 77 72 69 74 61 62 6c 65 mat for writable
aff50 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 database files
aff60 2a 2f 0a 20 20 75 38 20 69 6e 56 74 61 62 4d 65 */. u8 inVtabMe
aff70 74 68 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 thod; /*
aff80 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f See comments abo
aff90 76 65 20 2a 2f 0a 20 20 75 38 20 75 73 65 73 53 ve */. u8 usesS
affa0 74 6d 74 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 tmtJournal;
affb0 2f 2a 20 54 72 75 65 20 69 66 20 75 73 65 73 20 /* True if uses
affc0 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 a statement jour
affd0 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 nal */. u8 read
affe0 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 Only;
afff0 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 72 65 61 /* True for rea
b0000 64 2d 6f 6e 6c 79 20 73 74 61 74 65 6d 65 6e 74 d-only statement
b0010 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e s */. int nChan
b0020 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ge; /
b0030 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 62 20 63 * Number of db c
b0040 68 61 6e 67 65 73 20 6d 61 64 65 20 73 69 6e 63 hanges made sinc
b0050 65 20 6c 61 73 74 20 72 65 73 65 74 20 2a 2f 0a e last reset */.
b0060 20 20 69 36 34 20 73 74 61 72 74 54 69 6d 65 3b i64 startTime;
b0070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d /* Tim
b0080 65 20 77 68 65 6e 20 71 75 65 72 79 20 73 74 61 e when query sta
b0090 72 74 65 64 20 2d 20 75 73 65 64 20 66 6f 72 20 rted - used for
b00a0 70 72 6f 66 69 6c 69 6e 67 20 2a 2f 0a 20 20 69 profiling */. i
b00b0 6e 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20 nt btreeMask;
b00c0 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 /* Bitmas
b00d0 6b 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65 k of db->aDb[] e
b00e0 6e 74 72 69 65 73 20 72 65 66 65 72 65 6e 63 65 ntries reference
b00f0 64 20 2a 2f 0a 20 20 42 74 72 65 65 4d 75 74 65 d */. BtreeMute
b0100 78 41 72 72 61 79 20 61 4d 75 74 65 78 3b 20 2f xArray aMutex; /
b0110 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 42 74 * An array of Bt
b0120 72 65 65 20 75 73 65 64 20 68 65 72 65 20 61 6e ree used here an
b0130 64 20 6e 65 65 64 69 6e 67 20 6c 6f 63 6b 73 20 d needing locks
b0140 2a 2f 0a 20 20 69 6e 74 20 61 43 6f 75 6e 74 65 */. int aCounte
b0150 72 5b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 r[2]; /*
b0160 43 6f 75 6e 74 65 72 73 20 75 73 65 64 20 62 79 Counters used by
b0170 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 sqlite3_stmt_st
b0180 61 74 75 73 28 29 20 2a 2f 0a 20 20 69 6e 74 20 atus() */. int
b0190 6e 53 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 nSql;
b01a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
b01b0 79 74 65 73 20 69 6e 20 7a 53 71 6c 20 2a 2f 0a ytes in zSql */.
b01c0 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 char *zSql;
b01d0 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 /* Text
b01e0 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 of the SQL state
b01f0 6d 65 6e 74 20 74 68 61 74 20 67 65 6e 65 72 61 ment that genera
b0200 74 65 64 20 74 68 69 73 20 2a 2f 0a 23 69 66 64 ted this */.#ifd
b0210 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
b0220 20 20 46 49 4c 45 20 2a 74 72 61 63 65 3b 20 20 FILE *trace;
b0230 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
b0240 20 61 6e 20 65 78 65 63 75 74 69 6f 6e 20 74 72 an execution tr
b0250 61 63 65 20 68 65 72 65 2c 20 69 66 20 6e 6f 74 ace here, if not
b0260 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e 64 69 66 0a NULL */.#endif.
b0270 20 20 69 6e 74 20 6f 70 65 6e 65 64 53 74 61 74 int openedStat
b0280 65 6d 65 6e 74 3b 20 20 2f 2a 20 54 72 75 65 20 ement; /* True
b0290 69 66 20 74 68 69 73 20 56 4d 20 68 61 73 20 6f if this VM has o
b02a0 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d 65 6e pened a statemen
b02b0 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 69 66 t journal */.#if
b02c0 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20 def SQLITE_SSE.
b02d0 20 69 6e 74 20 66 65 74 63 68 49 64 3b 20 20 20 int fetchId;
b02e0 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d /* Statem
b02f0 65 6e 74 20 6e 75 6d 62 65 72 20 75 73 65 64 20 ent number used
b0300 62 79 20 73 71 6c 69 74 65 33 5f 66 65 74 63 68 by sqlite3_fetch
b0310 5f 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 _statement */.
b0320 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20 20 20 int lru;
b0330 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 /* Counter
b0340 20 75 73 65 64 20 66 6f 72 20 4c 52 55 20 63 61 used for LRU ca
b0350 63 68 65 20 72 65 70 6c 61 63 65 6d 65 6e 74 20 che replacement
b0360 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 */.#endif.#ifdef
b0370 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
b0380 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
b0390 0a 20 20 56 64 62 65 20 2a 70 4c 72 75 50 72 65 . Vdbe *pLruPre
b03a0 76 3b 0a 20 20 56 64 62 65 20 2a 70 4c 72 75 4e v;. Vdbe *pLruN
b03b0 65 78 74 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a ext;.#endif.};..
b03c0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
b03d0 69 6e 67 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 ing are allowed
b03e0 76 61 6c 75 65 73 20 66 6f 72 20 56 64 62 65 2e values for Vdbe.
b03f0 6d 61 67 69 63 0a 2a 2f 0a 23 64 65 66 69 6e 65 magic.*/.#define
b0400 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 VDBE_MAGIC_INIT
b0410 20 20 20 20 20 30 78 32 36 62 63 65 61 61 35 20 0x26bceaa5
b0420 20 20 20 2f 2a 20 42 75 69 6c 64 69 6e 67 20 61 /* Building a
b0430 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 2a 2f VDBE program */
b0440 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 .#define VDBE_MA
b0450 47 49 43 5f 52 55 4e 20 20 20 20 20 20 30 78 62 GIC_RUN 0xb
b0460 64 66 32 30 64 61 33 20 20 20 20 2f 2a 20 56 44 df20da3 /* VD
b0470 42 45 20 69 73 20 72 65 61 64 79 20 74 6f 20 65 BE is ready to e
b0480 78 65 63 75 74 65 20 2a 2f 0a 23 64 65 66 69 6e xecute */.#defin
b0490 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c e VDBE_MAGIC_HAL
b04a0 54 20 20 20 20 20 30 78 35 31 39 63 32 39 37 33 T 0x519c2973
b04b0 20 20 20 20 2f 2a 20 56 44 42 45 20 68 61 73 20 /* VDBE has
b04c0 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74 completed execut
b04d0 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 ion */.#define V
b04e0 44 42 45 5f 4d 41 47 49 43 5f 44 45 41 44 20 20 DBE_MAGIC_DEAD
b04f0 20 20 20 30 78 62 36 30 36 63 33 63 38 20 20 20 0xb606c3c8
b0500 20 2f 2a 20 54 68 65 20 56 44 42 45 20 68 61 73 /* The VDBE has
b0510 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 been deallocate
b0520 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 d */../*.** Func
b0530 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a tion prototypes.
b0540 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b0550 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
b0560 62 65 46 72 65 65 43 75 72 73 6f 72 28 56 64 62 beFreeCursor(Vdb
b0570 65 20 2a 2c 20 56 64 62 65 43 75 72 73 6f 72 2a e *, VdbeCursor*
b0580 29 3b 0a 76 6f 69 64 20 73 71 6c 69 74 65 56 64 );.void sqliteVd
b0590 62 65 50 6f 70 53 74 61 63 6b 28 56 64 62 65 2a bePopStack(Vdbe*
b05a0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
b05b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b05c0 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 3VdbeCursorMovet
b05d0 6f 28 56 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a o(VdbeCursor*);.
b05e0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
b05f0 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66 TE_DEBUG) || def
b0600 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c ined(VDBE_PROFIL
b0610 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 E).SQLITE_PRIVAT
b0620 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
b0630 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 2a 2c bePrintOp(FILE*,
b0640 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a 23 65 6e 64 int, Op*);.#end
b0650 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
b0660 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
b0670 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 eSerialTypeLen(u
b0680 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 32);.SQLITE_PRIV
b0690 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 ATE u32 sqlite3V
b06a0 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 dbeSerialType(Me
b06b0 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 m*, int);.SQLITE
b06c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b06d0 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 ite3VdbeSerialPu
b06e0 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a t(unsigned char*
b06f0 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c 20 69 6e 74 , int, Mem*, int
b0700 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
b0710 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
b0720 65 53 65 72 69 61 6c 47 65 74 28 63 6f 6e 73 74 eSerialGet(const
b0730 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c unsigned char*,
b0740 20 75 33 32 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c u32, Mem*);.SQL
b0750 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b0760 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
b0770 74 65 41 75 78 44 61 74 61 28 56 64 62 65 46 75 teAuxData(VdbeFu
b0780 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a 69 6e 74 20 nc*, int);..int
b0790 73 71 6c 69 74 65 32 42 74 72 65 65 4b 65 79 43 sqlite2BtreeKeyC
b07a0 6f 6d 70 61 72 65 28 42 74 43 75 72 73 6f 72 20 ompare(BtCursor
b07b0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c *, const void *,
b07c0 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 20 2a int, int, int *
b07d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
b07e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
b07f0 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 56 eIdxKeyCompare(V
b0800 64 62 65 43 75 72 73 6f 72 2a 2c 55 6e 70 61 63 dbeCursor*,Unpac
b0810 6b 65 64 52 65 63 6f 72 64 2a 2c 69 6e 74 2a 29 kedRecord*,int*)
b0820 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
b0830 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
b0840 49 64 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f IdxRowid(BtCurso
b0850 72 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 53 51 4c r *, i64 *);.SQL
b0860 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b0870 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 sqlite3MemCompar
b0880 65 28 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f e(const Mem*, co
b0890 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 nst Mem*, const
b08a0 43 6f 6c 6c 53 65 71 2a 29 3b 0a 53 51 4c 49 54 CollSeq*);.SQLIT
b08b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b08c0 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 56 64 lite3VdbeExec(Vd
b08d0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
b08e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b08f0 56 64 62 65 4c 69 73 74 28 56 64 62 65 2a 29 3b VdbeList(Vdbe*);
b0900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b0910 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 48 int sqlite3VdbeH
b0920 61 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 alt(Vdbe*);.SQLI
b0930 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b0940 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
b0950 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a 2c 20 Encoding(Mem *,
b0960 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
b0970 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b0980 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 VdbeMemTooBig(Me
b0990 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
b09a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
b09b0 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c dbeMemCopy(Mem*,
b09c0 20 63 6f 6e 73 74 20 4d 65 6d 2a 29 3b 0a 53 51 const Mem*);.SQ
b09d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b09e0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
b09f0 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a ShallowCopy(Mem*
b0a00 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e , const Mem*, in
b0a10 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
b0a20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
b0a30 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c dbeMemMove(Mem*,
b0a40 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Mem*);.SQLITE_P
b0a50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b0a60 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d e3VdbeMemNulTerm
b0a70 69 6e 61 74 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c inate(Mem*);.SQL
b0a80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b0a90 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
b0aa0 74 53 74 72 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 tStr(Mem*, const
b0ab0 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 75 38 2c char*, int, u8,
b0ac0 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 void(*)(void*))
b0ad0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
b0ae0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
b0af0 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d eMemSetInt64(Mem
b0b00 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f *, i64);.SQLITE_
b0b10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
b0b20 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f ite3VdbeMemSetDo
b0b30 75 62 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c uble(Mem*, doubl
b0b40 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 e);.SQLITE_PRIVA
b0b50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
b0b60 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 dbeMemSetNull(Me
b0b70 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
b0b80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
b0b90 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c VdbeMemSetZeroBl
b0ba0 6f 62 28 4d 65 6d 2a 2c 69 6e 74 29 3b 0a 53 51 ob(Mem*,int);.SQ
b0bb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b0bc0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
b0bd0 53 65 74 52 6f 77 53 65 74 28 4d 65 6d 2a 29 3b SetRowSet(Mem*);
b0be0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b0bf0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
b0c00 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
b0c10 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
b0c20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b0c30 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 3VdbeMemStringif
b0c40 79 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 y(Mem*, int);.SQ
b0c50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 LITE_PRIVATE i64
b0c60 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
b0c70 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 alue(Mem*);.SQLI
b0c80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b0c90 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
b0ca0 65 67 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 egerify(Mem*);.S
b0cb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f QLITE_PRIVATE do
b0cc0 75 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 uble sqlite3Vdbe
b0cd0 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d 2a 29 3b RealValue(Mem*);
b0ce0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b0cf0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
b0d00 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 IntegerAffinity(
b0d10 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
b0d20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b0d30 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 3VdbeMemRealify(
b0d40 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
b0d50 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b0d60 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 3VdbeMemNumerify
b0d70 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 (Mem*);.SQLITE_P
b0d80 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b0d90 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 e3VdbeMemFromBtr
b0da0 65 65 28 42 74 43 75 72 73 6f 72 2a 2c 69 6e 74 ee(BtCursor*,int
b0db0 2c 69 6e 74 2c 69 6e 74 2c 4d 65 6d 2a 29 3b 0a ,int,int,Mem*);.
b0dc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
b0dd0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
b0de0 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 emRelease(Mem *p
b0df0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
b0e00 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
b0e10 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 beMemReleaseExte
b0e20 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 rnal(Mem *p);.SQ
b0e30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b0e40 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 sqlite3VdbeMemF
b0e50 69 6e 61 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75 inalize(Mem*, Fu
b0e60 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f ncDef*);.SQLITE_
b0e70 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
b0e80 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 ar *sqlite3Opcod
b0e90 65 4e 61 6d 65 28 69 6e 74 29 3b 0a 53 51 4c 49 eName(int);.SQLI
b0ea0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b0eb0 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 qlite3VdbeOpcode
b0ec0 48 61 73 50 72 6f 70 65 72 74 79 28 69 6e 74 2c HasProperty(int,
b0ed0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
b0ee0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b0ef0 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 3VdbeMemGrow(Mem
b0f00 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 *pMem, int n, i
b0f10 6e 74 20 70 72 65 73 65 72 76 65 29 3b 0a 23 69 nt preserve);.#i
b0f20 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
b0f30 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 LE_MEMORY_MANAGE
b0f40 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 MENT.SQLITE_PRIV
b0f50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
b0f60 64 62 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 dbeReleaseBuffer
b0f70 73 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 6e 64 s(Vdbe *p);.#end
b0f80 69 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 if..#ifndef NDEB
b0f90 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
b0fa0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
b0fb0 56 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d 65 VdbeMemSanity(Me
b0fc0 6d 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 m*);.#endif.SQLI
b0fd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b0fe0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 qlite3VdbeMemTra
b0ff0 6e 73 6c 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29 nslate(Mem*, u8)
b1000 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
b1010 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
b1020 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
b1030 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 te3VdbePrintSql(
b1040 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Vdbe*);.SQLITE_P
b1050 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
b1060 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 lite3VdbeMemPret
b1070 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 tyPrint(Mem *pMe
b1080 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a m, char *zBuf);.
b1090 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 #endif.SQLITE_PR
b10a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b10b0 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 3VdbeMemHandleBo
b10c0 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 m(Mem *pMem);..#
b10d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
b10e0 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 IT_INCRBLOB.SQLI
b10f0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
b1100 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 sqlite3VdbeMemE
b1110 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 xpandBlob(Mem *)
b1120 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e ;.#else. #defin
b1130 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d e sqlite3VdbeMem
b1140 45 78 70 61 6e 64 42 6c 6f 62 28 78 29 20 53 51 ExpandBlob(x) SQ
b1150 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a LITE_OK.#endif..
b1160 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
b1170 65 64 28 5f 56 44 42 45 49 4e 54 5f 48 5f 29 20 ed(_VDBEINT_H_)
b1180 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
b1190 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 49 *** End of vdbeI
b11a0 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
b11b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b11c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b11d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
b11e0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
b11f0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
b1200 20 69 6e 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a in utf.c ******
b1210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1220 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c **/..#ifndef SQL
b1230 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e ITE_AMALGAMATION
b1240 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
b1250 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 76 61 wing constant va
b1260 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20 74 lue is used by t
b1270 68 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 he SQLITE_BIGEND
b1280 49 41 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 IAN and.** SQLIT
b1290 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d E_LITTLEENDIAN m
b12a0 61 63 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 acros..*/.SQLITE
b12b0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 _PRIVATE const i
b12c0 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 nt sqlite3one =
b12d0 31 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 1;.#endif /* SQL
b12e0 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e ITE_AMALGAMATION
b12f0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 */../*.** This
b1300 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 lookup table is
b1310 75 73 65 64 20 74 6f 20 68 65 6c 70 20 64 65 63 used to help dec
b1320 6f 64 65 20 74 68 65 20 66 69 72 73 74 20 62 79 ode the first by
b1330 74 65 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 te of.** a multi
b1340 2d 62 79 74 65 20 55 54 46 38 20 63 68 61 72 61 -byte UTF8 chara
b1350 63 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 cter..*/.static
b1360 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
b1370 68 61 72 20 73 71 6c 69 74 65 33 55 74 66 38 54 har sqlite3Utf8T
b1380 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 rans1[] = {. 0x
b1390 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 00, 0x01, 0x02,
b13a0 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 0x03, 0x04, 0x05
b13b0 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 , 0x06, 0x07,.
b13c0 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 0x08, 0x09, 0x0a
b13d0 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 , 0x0b, 0x0c, 0x
b13e0 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 0d, 0x0e, 0x0f,.
b13f0 20 20 30 78 31 30 2c 20 30 78 31 31 2c 20 30 78 0x10, 0x11, 0x
b1400 31 32 2c 20 30 78 31 33 2c 20 30 78 31 34 2c 20 12, 0x13, 0x14,
b1410 30 78 31 35 2c 20 30 78 31 36 2c 20 30 78 31 37 0x15, 0x16, 0x17
b1420 2c 0a 20 20 30 78 31 38 2c 20 30 78 31 39 2c 20 ,. 0x18, 0x19,
b1430 30 78 31 61 2c 20 30 78 31 62 2c 20 30 78 31 63 0x1a, 0x1b, 0x1c
b1440 2c 20 30 78 31 64 2c 20 30 78 31 65 2c 20 30 78 , 0x1d, 0x1e, 0x
b1450 31 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 1f,. 0x00, 0x01
b1460 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 , 0x02, 0x03, 0x
b1470 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 04, 0x05, 0x06,
b1480 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 0x07,. 0x08, 0x
b1490 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 09, 0x0a, 0x0b,
b14a0 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 0x0c, 0x0d, 0x0e
b14b0 2c 20 30 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 , 0x0f,. 0x00,
b14c0 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 0x01, 0x02, 0x03
b14d0 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 , 0x04, 0x05, 0x
b14e0 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 30 06, 0x07,. 0x00
b14f0 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 , 0x01, 0x02, 0x
b1500 30 33 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 03, 0x00, 0x01,
b1510 30 78 30 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0x00, 0x00,.};..
b1520 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 .#define WRITE_U
b1530 54 46 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 TF8(zOut, c) {
b1540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1550 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 \. if(
b1560 63 3c 30 78 30 30 30 38 30 20 29 7b 20 20 20 20 c<0x00080 ){
b1570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1590 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
b15a0 20 28 75 38 29 28 63 26 30 78 46 46 29 3b 20 20 (u8)(c&0xFF);
b15b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b15c0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 \. }
b15d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b15e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b15f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1600 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 \. else if(
b1610 63 3c 30 78 30 30 38 30 30 20 29 7b 20 20 20 20 c<0x00800 ){
b1620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1630 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
b1640 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30 *zOut++ = 0xC0
b1650 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 26 30 + (u8)((c>>6)&0
b1660 78 31 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x1F);
b1670 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
b1680 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 ++ = 0x80 + (u8)
b1690 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 (c & 0x3F);
b16a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
b16b0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 }
b16c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b16d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b16e0 20 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 \. else
b16f0 69 66 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b if( c<0x10000 ){
b1700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1720 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
b1730 30 78 45 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 0xE0 + (u8)((c>>
b1740 31 32 29 26 30 78 30 46 29 3b 20 20 20 20 20 20 12)&0x0F);
b1750 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
b1760 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 zOut++ = 0x80 +
b1770 28 75 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78 (u8)((c>>6) & 0x
b1780 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3F);
b1790 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
b17a0 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 = 0x80 + (u8)(c
b17b0 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 & 0x3F);
b17c0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
b17d0 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 else{
b17e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b17f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1800 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
b1810 2b 20 3d 20 30 78 46 30 20 2b 20 28 75 38 29 28 + = 0xF0 + (u8)(
b1820 28 63 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b (c>>18) & 0x07);
b1830 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
b1840 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 *zOut++ = 0x8
b1850 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 0 + (u8)((c>>12)
b1860 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 & 0x3F);
b1870 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
b1880 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 t++ = 0x80 + (u8
b1890 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 )((c>>6) & 0x3F)
b18a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c ; \
b18b0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 . *zOut++ = 0
b18c0 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 x80 + (u8)(c & 0
b18d0 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x3F);
b18e0 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 \. }
b18f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1920 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 \.}..#define WR
b1930 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74 ITE_UTF16LE(zOut
b1940 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 , c) {
b1950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1960 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 \. if
b1970 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 ( c<=0xFFFF ){
b1980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b19a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b19b0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
b19c0 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 (u8)(c&0x00FF);
b19d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b19e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b19f0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
b1a00 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 t++ = (u8)((c>>8
b1a10 29 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 )&0x00FF);
b1a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1a30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
b1a40 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 }else{
b1a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1a80 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
b1a90 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 = (u8)(((c>>10)&
b1aa0 30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 0x003F) + (((c-0
b1ab0 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 x10000)>>10)&0x0
b1ac0 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 0C0)); \. *z
b1ad0 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 Out++ = (u8)(0x0
b1ae0 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30 0D8 + (((c-0x100
b1af0 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 00)>>18)&0x03));
b1b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
b1b10 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
b1b20 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 8)(c&0x00FF);
b1b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1b50 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
b1b60 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 43 20 + = (u8)(0x00DC
b1b70 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29 + ((c>>8)&0x03))
b1b80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
b1b90 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 \. }
b1ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1be0 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 \.}..#define WRI
b1bf0 54 45 5f 55 54 46 31 36 42 45 28 7a 4f 75 74 2c TE_UTF16BE(zOut,
b1c00 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 c) {
b1c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1c20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 \. if(
b1c30 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 c<=0xFFFF ){
b1c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
b1c70 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
b1c80 75 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 u8)((c>>8)&0x00F
b1c90 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
b1ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1cb0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
b1cc0 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 ++ = (u8)(c&0x00
b1cd0 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 FF);
b1ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1cf0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
b1d00 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 else{
b1d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1d40 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
b1d50 20 28 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 (u8)(0x00D8 + (
b1d60 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 ((c-0x10000)>>18
b1d70 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 )&0x03));
b1d80 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
b1d90 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e ut++ = (u8)(((c>
b1da0 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 >10)&0x003F) + (
b1db0 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 ((c-0x10000)>>10
b1dc0 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 )&0x00C0)); \.
b1dd0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
b1de0 29 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e )(0x00DC + ((c>>
b1df0 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 8)&0x03));
b1e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1e10 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
b1e20 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46 = (u8)(c&0x00FF
b1e30 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
b1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1e50 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 \. }
b1e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
b1ea0 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 .}..#define READ
b1eb0 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 _UTF16LE(zIn, c)
b1ec0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
b1ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1ee0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 \. c
b1ef0 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 = (*zIn++);
b1f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1f30 20 20 5c 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 \. c += ((*zI
b1f40 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 n++)<<8);
b1f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1f70 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 \. if
b1f80 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 ( c>=0xD800 && c
b1f90 3c 30 78 45 30 30 30 20 29 7b 20 20 20 20 20 20 <0xE000 ){
b1fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1fc0 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 \. int c2 =
b1fd0 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 (*zIn++);
b1fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2000 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 \. c
b2010 32 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 2 += ((*zIn++)<<
b2020 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 8);
b2030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2050 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 \. c = (c2&0
b2060 78 30 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 x03FF) + ((c&0x0
b2070 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 03F)<<10) + (((c
b2080 26 30 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 &0x03C0)+0x0040)
b2090 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 20 20 69 <<10); \. i
b20a0 66 28 20 28 63 20 26 20 30 78 46 46 46 46 30 30 f( (c & 0xFFFF00
b20b0 30 30 29 3d 3d 30 20 29 20 63 20 3d 20 30 78 46 00)==0 ) c = 0xF
b20c0 46 46 44 3b 20 20 20 20 20 20 20 20 20 20 20 20 FFD;
b20d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b20e0 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 \. }
b20f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2120 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 \.}..#d
b2130 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36 efine READ_UTF16
b2140 42 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 BE(zIn, c){
b2150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2170 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 28 2a 7a \. c = ((*z
b2180 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 In++)<<8);
b2190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b21a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b21b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
b21c0 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 c += (*zIn++);
b21d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b21e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b21f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2200 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 \. if( c>=0
b2210 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 30 xD800 && c<0xE00
b2220 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
b2230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2240 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
b2250 20 69 6e 74 20 63 32 20 3d 20 28 28 2a 7a 49 6e int c2 = ((*zIn
b2260 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 ++)<<8);
b2270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2290 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 \. c2 += (
b22a0 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 *zIn++);
b22b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b22c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b22d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
b22e0 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 c = (c2&0x03FF)
b22f0 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c + ((c&0x003F)<<
b2300 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 10) + (((c&0x03C
b2310 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 0)+0x0040)<<10);
b2320 20 20 20 5c 0a 20 20 20 20 69 66 28 20 28 63 20 \. if( (c
b2330 26 20 30 78 46 46 46 46 30 30 30 30 29 3d 3d 30 & 0xFFFF0000)==0
b2340 20 29 20 63 20 3d 20 30 78 46 46 46 44 3b 20 20 ) c = 0xFFFD;
b2350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2360 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
b2370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b23a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b23b0 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 \.}../*.** Tr
b23c0 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 anslate a single
b23d0 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 UTF-8 character
b23e0 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e . Return the un
b23f0 69 63 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a icode value..**.
b2400 2a 2a 20 44 75 72 69 6e 67 20 74 72 61 6e 73 6c ** During transl
b2410 61 74 69 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68 ation, assume th
b2420 61 74 20 74 68 65 20 62 79 74 65 20 74 68 61 74 at the byte that
b2430 20 7a 54 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a zTerm points.**
b2440 20 69 73 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a is a 0x00..**.*
b2450 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 * Write a pointe
b2460 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 75 6e r to the next un
b2470 72 65 61 64 20 62 79 74 65 20 62 61 63 6b 20 69 read byte back i
b2480 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a nto *pzNext..**.
b2490 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61 ** Notes On Inva
b24a0 6c 69 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a lid UTF-8:.**.**
b24b0 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e * This routin
b24c0 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 e never allows a
b24d0 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 7-bit character
b24e0 20 28 30 78 30 30 20 74 68 72 6f 75 67 68 20 30 (0x00 through 0
b24f0 78 37 66 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62 x7f) to.** b
b2500 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d e encoded as a m
b2510 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 ulti-byte charac
b2520 74 65 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d ter. Any multi-
b2530 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74 byte character t
b2540 68 61 74 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d hat.** attem
b2550 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 pts to encode a
b2560 76 61 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78 value between 0x
b2570 30 30 20 61 6e 64 20 30 78 37 66 20 69 73 20 72 00 and 0x7f is r
b2580 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66 endered as 0xfff
b2590 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 d..**.** * Thi
b25a0 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 s routine never
b25b0 61 6c 6c 6f 77 73 20 61 20 55 54 46 31 36 20 73 allows a UTF16 s
b25c0 75 72 72 6f 67 61 74 65 20 76 61 6c 75 65 20 74 urrogate value t
b25d0 6f 20 62 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a o be encoded..**
b25e0 20 20 20 20 20 49 66 20 61 20 6d 75 6c 74 69 2d If a multi-
b25f0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 61 byte character a
b2600 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 ttempts to encod
b2610 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65 e a value betwee
b2620 6e 0a 2a 2a 20 20 20 20 20 30 78 64 38 30 30 20 n.** 0xd800
b2630 61 6e 64 20 30 78 65 30 30 30 20 74 68 65 6e 20 and 0xe000 then
b2640 69 74 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 it is rendered a
b2650 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 s 0xfffd..**.**
b2660 20 2a 20 20 42 79 74 65 73 20 69 6e 20 74 68 65 * Bytes in the
b2670 20 72 61 6e 67 65 20 6f 66 20 30 78 38 30 20 74 range of 0x80 t
b2680 68 72 6f 75 67 68 20 30 78 62 66 20 77 68 69 63 hrough 0xbf whic
b2690 68 20 6f 63 63 75 72 20 61 73 20 74 68 65 20 66 h occur as the f
b26a0 69 72 73 74 0a 2a 2a 20 20 20 20 20 62 79 74 65 irst.** byte
b26b0 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20 of a character
b26c0 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 are interpreted
b26d0 61 73 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 as single-byte c
b26e0 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 haracters.**
b26f0 20 61 6e 64 20 72 65 6e 64 65 72 65 64 20 61 73 and rendered as
b2700 20 74 68 65 6d 73 65 6c 76 65 73 20 65 76 65 6e themselves even
b2710 20 74 68 6f 75 67 68 20 74 68 65 79 20 61 72 65 though they are
b2720 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 technically.**
b2730 20 20 20 20 69 6e 76 61 6c 69 64 20 63 68 61 72 invalid char
b2740 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a acters..**.** *
b2750 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 This routine a
b2760 63 63 65 70 74 73 20 61 6e 20 69 6e 66 69 6e 69 ccepts an infini
b2770 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 te number of dif
b2780 66 65 72 65 6e 74 20 55 54 46 38 20 65 6e 63 6f ferent UTF8 enco
b2790 64 69 6e 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72 dings.** for
b27a0 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 73 20 unicode values
b27b0 30 78 38 30 20 61 6e 64 20 67 72 65 61 74 65 72 0x80 and greater
b27c0 2e 20 20 49 74 20 64 6f 20 6e 6f 74 20 63 68 61 . It do not cha
b27d0 6e 67 65 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a nge over-length.
b27e0 2a 2a 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 ** encodings
b27f0 20 74 6f 20 30 78 66 66 66 64 20 61 73 20 73 6f to 0xfffd as so
b2800 6d 65 20 73 79 73 74 65 6d 73 20 72 65 63 6f 6d me systems recom
b2810 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 mend..*/.#define
b2820 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 READ_UTF8(zIn,
b2830 7a 54 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20 zTerm, c)
b2840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2850 20 20 20 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 \. c = *(zI
b2860 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 n++);
b2870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2890 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 \. if( c>=0xc0
b28a0 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
b28b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b28c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
b28d0 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 c = sqlite3U
b28e0 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 tf8Trans1[c-0xc0
b28f0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ];
b2900 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
b2910 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 while( zIn!=zTe
b2920 72 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20 30 78 rm && (*zIn & 0x
b2930 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 20 20 20 c0)==0x80 ){
b2940 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 \.
b2950 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 c = (c<<6) + (0x
b2960 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 3f & *(zIn++));
b2970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2980 20 20 20 20 20 5c 0a 20 20 20 20 7d 20 20 20 20 \. }
b2990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b29a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b29b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b29c0 20 20 5c 0a 20 20 20 20 69 66 28 20 63 3c 30 78 \. if( c<0x
b29d0 38 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 80
b29e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b29f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
b2a00 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 . || (c&0
b2a10 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 xFFFFF800)==0xD8
b2a20 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 00
b2a30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
b2a40 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 || (c&0xFF
b2a50 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 FFFFFE)==0xFFFE
b2a60 29 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 ){ c = 0xFFFD;
b2a70 7d 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53 } \. }.S
b2a80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b2a90 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 t sqlite3Utf8Rea
b2aa0 64 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 d(. const unsig
b2ab0 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 20 20 20 ned char *z,
b2ac0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 /* First by
b2ad0 74 65 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72 te of UTF-8 char
b2ae0 61 63 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 acter */. const
b2af0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
b2b00 7a 54 65 72 6d 2c 20 20 20 20 20 2f 2a 20 50 72 zTerm, /* Pr
b2b10 65 74 65 6e 64 20 74 68 69 73 20 62 79 74 65 20 etend this byte
b2b20 69 73 20 30 78 30 30 20 2a 2f 0a 20 20 63 6f 6e is 0x00 */. con
b2b30 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
b2b40 20 2a 2a 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20 **pzNext /*
b2b50 57 72 69 74 65 20 66 69 72 73 74 20 62 79 74 65 Write first byte
b2b60 20 70 61 73 74 20 55 54 46 2d 38 20 63 68 61 72 past UTF-8 char
b2b70 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e here */.){. in
b2b80 74 20 63 3b 0a 20 20 52 45 41 44 5f 55 54 46 38 t c;. READ_UTF8
b2b90 28 7a 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 (z, zTerm, c);.
b2ba0 20 2a 70 7a 4e 65 78 74 20 3d 20 7a 3b 0a 20 20 *pzNext = z;.
b2bb0 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a return c;.}.....
b2bc0 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41 /*.** If the TRA
b2bd0 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63 NSLATE_TRACE mac
b2be0 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 ro is defined, t
b2bf0 68 65 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68 he value of each
b2c00 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 Mem is.** print
b2c10 65 64 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 ed on stderr on
b2c20 74 68 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 the way into and
b2c30 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56 out of sqlite3V
b2c40 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 dbeMemTranslate(
b2c50 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e )..*/ ./* #defin
b2c60 65 20 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 e TRANSLATE_TRAC
b2c70 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 E 1 */..#ifndef
b2c80 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
b2c90 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 6./*.** This rou
b2ca0 74 69 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 tine transforms
b2cb0 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 the internal tex
b2cc0 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 t encoding used
b2cd0 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 by pMem to.** de
b2ce0 73 69 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20 siredEnc. It is
b2cf0 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 an error if the
b2d00 73 74 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64 string is alread
b2d10 79 20 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 y of the desired
b2d20 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 .** encoding, or
b2d30 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e if *pMem does n
b2d40 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 ot contain a str
b2d50 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 ing value..*/.SQ
b2d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b2d70 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 sqlite3VdbeMemT
b2d80 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d ranslate(Mem *pM
b2d90 65 6d 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e em, u8 desiredEn
b2da0 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 c){. int len;
b2db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2dc0 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e /* Maximum len
b2dd0 67 74 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74 gth of output st
b2de0 72 69 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f ring in bytes */
b2df0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
b2e00 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 *zOut;
b2e10 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 /* Outp
b2e20 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 ut buffer */. u
b2e30 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 nsigned char *zI
b2e40 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
b2e50 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 /* Input it
b2e60 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 erator */. unsi
b2e70 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d gned char *zTerm
b2e80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
b2e90 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 /* End of inpu
b2ea0 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 t */. unsigned
b2eb0 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 char *z;
b2ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
b2ed0 4f 75 74 70 75 74 20 69 74 65 72 61 74 6f 72 20 Output iterator
b2ee0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e */. unsigned in
b2ef0 74 20 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 t c;.. assert(
b2f00 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
b2f10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
b2f20 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
b2f30 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
b2f40 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
b2f50 53 74 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 Str );. assert(
b2f60 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 pMem->enc!=desi
b2f70 72 65 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 redEnc );. asse
b2f80 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 rt( pMem->enc!=0
b2f90 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d );. assert( pM
b2fa0 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 em->n>=0 );..#if
b2fb0 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 defined(TRANSLA
b2fc0 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 TE_TRACE) && def
b2fd0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
b2fe0 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 G). {. char
b2ff0 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 zBuf[100];. s
b3000 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 qlite3VdbeMemPre
b3010 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a ttyPrint(pMem, z
b3020 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 Buf);. fprint
b3030 66 28 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54 f(stderr, "INPUT
b3040 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b : %s\n", zBuf);
b3050 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
b3060 2a 20 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61 * If the transla
b3070 74 69 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20 tion is between
b3080 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e UTF-16 little an
b3090 64 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 d big endian, th
b30a0 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 en . ** all tha
b30b0 74 20 69 73 20 72 65 71 75 69 72 65 64 20 69 73 t is required is
b30c0 20 74 6f 20 73 77 61 70 20 74 68 65 20 62 79 74 to swap the byt
b30d0 65 20 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61 e order. This ca
b30e0 73 65 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 se is handled.
b30f0 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 ** differently f
b3100 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a rom the others..
b3110 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d */. if( pMem-
b3120 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 >enc!=SQLITE_UTF
b3130 38 20 26 26 20 64 65 73 69 72 65 64 45 6e 63 21 8 && desiredEnc!
b3140 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a =SQLITE_UTF8 ){.
b3150 20 20 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 u8 temp;.
b3160 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 int rc;. rc
b3170 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
b3180 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d MakeWriteable(pM
b3190 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 em);. if( rc!
b31a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
b31b0 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
b31c0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a SQLITE_NOMEM );.
b31d0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
b31e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
b31f0 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 . zIn = (u8*)
b3200 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 pMem->z;. zTe
b3210 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e rm = &zIn[pMem->
b3220 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 n&~1];. while
b3230 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 ( zIn<zTerm ){.
b3240 20 20 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e temp = *zIn
b3250 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a ;. *zIn = *
b3260 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a (zIn+1);. z
b3270 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e In++;. *zIn
b3280 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d ++ = temp;. }
b3290 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d . pMem->enc =
b32a0 20 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 desiredEnc;.
b32b0 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f goto translate_
b32c0 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 out;. }.. /* S
b32d0 65 74 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 et len to the ma
b32e0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
b32f0 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 69 bytes required i
b3300 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 n the output buf
b3310 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 fer. */. if( de
b3320 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
b3330 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 _UTF8 ){. /*
b3340 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 When converting
b3350 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 from UTF-16, the
b3360 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 maximum growth
b3370 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 results from.
b3380 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 ** translating
b3390 61 20 32 2d 62 79 74 65 20 63 68 61 72 61 63 74 a 2-byte charact
b33a0 65 72 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55 er to a 4-byte U
b33b0 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a TF-8 character..
b33c0 20 20 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 ** A single
b33d0 62 79 74 65 20 69 73 20 72 65 71 75 69 72 65 64 byte is required
b33e0 20 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20 for the output
b33f0 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 string. ** nu
b3400 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 l-terminator..
b3410 20 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e */. pMem->n
b3420 20 26 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 &= ~1;. len
b3430 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 = pMem->n * 2 +
b3440 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
b3450 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 /* When converti
b3460 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f ng from UTF-8 to
b3470 20 55 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69 UTF-16 the maxi
b3480 6d 75 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61 mum growth is ca
b3490 75 73 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e used. ** when
b34a0 20 61 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20 a 1-byte UTF-8
b34b0 63 68 61 72 61 63 74 65 72 20 69 73 20 74 72 61 character is tra
b34c0 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32 nslated into a 2
b34d0 2d 62 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20 -byte UTF-16.
b34e0 20 2a 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54 ** character. T
b34f0 77 6f 20 62 79 74 65 73 20 61 72 65 20 72 65 71 wo bytes are req
b3500 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74 uired in the out
b3510 70 75 74 20 62 75 66 66 65 72 20 66 6f 72 20 74 put buffer for t
b3520 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 he. ** nul-te
b3530 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f rminator.. */
b3540 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d . len = pMem-
b3550 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a >n * 2 + 2;. }.
b3560 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f . /* Set zIn to
b3570 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74 point at the st
b3580 61 72 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 art of the input
b3590 20 62 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72 buffer and zTer
b35a0 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a m to point 1. *
b35b0 2a 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 * byte past the
b35c0 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 end.. **. ** V
b35d0 61 72 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 ariable zOut is
b35e0 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 set to point at
b35f0 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 the output buffe
b3600 72 2c 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 r, space obtaine
b3610 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 d. ** from sqli
b3620 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 te3_malloc()..
b3630 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 */. zIn = (u8*)
b3640 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d pMem->z;. zTerm
b3650 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d = &zIn[pMem->n]
b3660 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 ;. zOut = sqlit
b3670 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d e3DbMallocRaw(pM
b3680 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 em->db, len);.
b3690 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 if( !zOut ){.
b36a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
b36b0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 OMEM;. }. z =
b36c0 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 zOut;.. if( pMe
b36d0 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 m->enc==SQLITE_U
b36e0 54 46 38 20 29 7b 0a 20 20 20 20 69 66 28 20 64 TF8 ){. if( d
b36f0 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 esiredEnc==SQLIT
b3700 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 E_UTF16LE ){.
b3710 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 /* UTF-8 -> U
b3720 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 TF-16 Little-end
b3730 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 ian */. whi
b3740 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b le( zIn<zTerm ){
b3750 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 . /* c =
b3760 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
b3770 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e zIn, zTerm, (con
b3780 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a st u8**)&zIn); *
b3790 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 /. READ_U
b37a0 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 TF8(zIn, zTerm,
b37b0 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 c);. WRIT
b37c0 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b E_UTF16LE(z, c);
b37d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
b37e0 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
b37f0 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 ( desiredEnc==SQ
b3800 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a LITE_UTF16BE );.
b3810 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d /* UTF-8 -
b3820 3e 20 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 > UTF-16 Big-end
b3830 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 ian */. whi
b3840 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b le( zIn<zTerm ){
b3850 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 . /* c =
b3860 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
b3870 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e zIn, zTerm, (con
b3880 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a st u8**)&zIn); *
b3890 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 /. READ_U
b38a0 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 TF8(zIn, zTerm,
b38b0 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 c);. WRIT
b38c0 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b E_UTF16BE(z, c);
b38d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
b38e0 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e pMem->n = (in
b38f0 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 t)(z - zOut);.
b3900 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 *z++ = 0;. }e
b3910 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
b3920 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c desiredEnc==SQL
b3930 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 ITE_UTF8 );.
b3940 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 if( pMem->enc==S
b3950 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b QLITE_UTF16LE ){
b3960 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 . /* UTF-16
b3970 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d Little-endian -
b3980 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 > UTF-8 */.
b3990 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 while( zIn<zTer
b39a0 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41 m ){. REA
b39b0 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 D_UTF16LE(zIn, c
b39c0 29 3b 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 ); . WRIT
b39d0 45 5f 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 E_UTF8(z, c);.
b39e0 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
b39f0 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 . /* UTF-16
b3a00 20 42 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 Big-endian -> U
b3a10 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 TF-8 */. wh
b3a20 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 ile( zIn<zTerm )
b3a30 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 {. READ_U
b3a40 54 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20 TF16BE(zIn, c);
b3a50 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 . WRITE_U
b3a60 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 TF8(z, c);.
b3a70 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 }. }. pMe
b3a80 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d m->n = (int)(z -
b3a90 20 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a zOut);. }. *z
b3aa0 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
b3ab0 28 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 (pMem->n+(desire
b3ac0 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 dEnc==SQLITE_UTF
b3ad0 38 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 8?1:2))<=len );.
b3ae0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
b3af0 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
b3b00 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d pMem->flags &=
b3b10 20 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 ~(MEM_Static|ME
b3b20 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 M_Dyn|MEM_Ephem)
b3b30 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 ;. pMem->enc =
b3b40 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d desiredEnc;. pM
b3b50 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 em->flags |= (ME
b3b60 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b M_Term|MEM_Dyn);
b3b70 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 . pMem->z = (ch
b3b80 61 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d ar*)zOut;. pMem
b3b90 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d ->zMalloc = pMem
b3ba0 2d 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f ->z;..translate_
b3bb0 6f 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 out:.#if defined
b3bc0 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 (TRANSLATE_TRACE
b3bd0 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c ) && defined(SQL
b3be0 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 ITE_DEBUG). {.
b3bf0 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 char zBuf[100
b3c00 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ];. sqlite3Vd
b3c10 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 beMemPrettyPrint
b3c20 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 (pMem, zBuf);.
b3c30 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
b3c40 2c 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 , "OUTPUT: %s\n"
b3c50 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e , zBuf);. }.#en
b3c60 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c dif. return SQL
b3c70 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
b3c80 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
b3c90 65 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d ecks for a byte-
b3ca0 6f 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 order mark at th
b3cb0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
b3cc0 68 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 he .** UTF-16 st
b3cd0 72 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a ring stored in *
b3ce0 70 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 pMem. If one is
b3cf0 70 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 present, it is r
b3d00 65 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 emoved and.** th
b3d10 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 e encoding of th
b3d20 65 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 e Mem adjusted.
b3d30 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 This routine doe
b3d40 73 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 s not do any.**
b3d50 62 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 byte-swapping, i
b3d60 74 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e t just sets Mem.
b3d70 65 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c enc appropriatel
b3d80 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c y..**.** The all
b3d90 6f 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c ocation (static,
b3da0 20 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 dynamic etc.) a
b3db0 6e 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 nd encoding of t
b3dc0 68 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a he Mem may be.**
b3dd0 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 changed by this
b3de0 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 function..*/.SQ
b3df0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b3e00 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 sqlite3VdbeMemH
b3e10 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d andleBom(Mem *pM
b3e20 65 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 em){. int rc =
b3e30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 SQLITE_OK;. u8
b3e40 62 6f 6d 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 bom = 0;.. if(
b3e50 70 4d 65 6d 2d 3e 6e 3c 30 20 7c 7c 20 70 4d 65 pMem->n<0 || pMe
b3e60 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20 20 20 75 38 m->n>1 ){. u8
b3e70 20 62 31 20 3d 20 2a 28 75 38 20 2a 29 70 4d 65 b1 = *(u8 *)pMe
b3e80 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38 20 62 32 20 m->z;. u8 b2
b3e90 3d 20 2a 28 28 28 75 38 20 2a 29 70 4d 65 6d 2d = *(((u8 *)pMem-
b3ea0 3e 7a 29 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 >z) + 1);. if
b3eb0 28 20 62 31 3d 3d 30 78 46 45 20 26 26 20 62 32 ( b1==0xFE && b2
b3ec0 3d 3d 30 78 46 46 20 29 7b 0a 20 20 20 20 20 20 ==0xFF ){.
b3ed0 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 bom = SQLITE_UTF
b3ee0 31 36 42 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 16BE;. }.
b3ef0 69 66 28 20 62 31 3d 3d 30 78 46 46 20 26 26 20 if( b1==0xFF &&
b3f00 62 32 3d 3d 30 78 46 45 20 29 7b 0a 20 20 20 20 b2==0xFE ){.
b3f10 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f 55 bom = SQLITE_U
b3f20 54 46 31 36 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 TF16LE;. }.
b3f30 7d 0a 20 20 0a 20 20 69 66 28 20 62 6f 6d 20 29 }. . if( bom )
b3f40 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
b3f50 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 e3VdbeMemMakeWri
b3f60 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 teable(pMem);.
b3f70 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
b3f80 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 _OK ){. pMe
b3f90 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20 20 20 20 20 m->n -= 2;.
b3fa0 20 6d 65 6d 6d 6f 76 65 28 70 4d 65 6d 2d 3e 7a memmove(pMem->z
b3fb0 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20 70 , &pMem->z[2], p
b3fc0 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 70 Mem->n);. p
b3fd0 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 Mem->z[pMem->n]
b3fe0 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 70 4d = '\0';. pM
b3ff0 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d em->z[pMem->n+1]
b4000 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 70 = '\0';. p
b4010 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Mem->flags |= ME
b4020 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d M_Term;. pM
b4030 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f 6d 3b 0a 20 em->enc = bom;.
b4040 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
b4050 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f n rc;.}.#endif /
b4060 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 * SQLITE_OMIT_UT
b4070 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a F16 */../*.** pZ
b4080 20 69 73 20 61 20 55 54 46 2d 38 20 65 6e 63 6f is a UTF-8 enco
b4090 64 65 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69 ded unicode stri
b40a0 6e 67 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20 ng. If nByte is
b40b0 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 0a less than zero,.
b40c0 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 ** return the nu
b40d0 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 mber of unicode
b40e0 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 70 5a characters in pZ
b40f0 20 75 70 20 74 6f 20 28 62 75 74 20 6e 6f 74 20 up to (but not
b4100 69 6e 63 6c 75 64 69 6e 67 29 0a 2a 2a 20 74 68 including).** th
b4110 65 20 66 69 72 73 74 20 30 78 30 30 20 62 79 74 e first 0x00 byt
b4120 65 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20 6e e. If nByte is n
b4130 6f 74 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 ot less than zer
b4140 6f 2c 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a o, return the.**
b4150 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f number of unico
b4160 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e de characters in
b4170 20 74 68 65 20 66 69 72 73 74 20 6e 42 79 74 65 the first nByte
b4180 20 6f 66 20 70 5a 20 28 6f 72 20 75 70 20 74 6f of pZ (or up to
b4190 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 30 .** the first 0
b41a0 78 30 30 2c 20 77 68 69 63 68 65 76 65 72 20 63 x00, whichever c
b41b0 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a omes first)..*/.
b41c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b41d0 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 nt sqlite3Utf8Ch
b41e0 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 arLen(const char
b41f0 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 42 79 74 65 *zIn, int nByte
b4200 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a ){. int r = 0;.
b4210 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 20 3d 20 const u8 *z =
b4220 28 63 6f 6e 73 74 20 75 38 2a 29 7a 49 6e 3b 0a (const u8*)zIn;.
b4230 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 54 65 72 const u8 *zTer
b4240 6d 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 3d m;. if( nByte>=
b4250 30 20 29 7b 0a 20 20 20 20 7a 54 65 72 6d 20 3d 0 ){. zTerm =
b4260 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a 20 20 7d 65 &z[nByte];. }e
b4270 6c 73 65 7b 0a 20 20 20 20 7a 54 65 72 6d 20 3d lse{. zTerm =
b4280 20 28 63 6f 6e 73 74 20 75 38 2a 29 28 2d 31 29 (const u8*)(-1)
b4290 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
b42a0 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a 20 20 77 68 z<=zTerm );. wh
b42b0 69 6c 65 28 20 2a 7a 21 3d 30 20 26 26 20 7a 3c ile( *z!=0 && z<
b42c0 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 53 51 4c zTerm ){. SQL
b42d0 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 ITE_SKIP_UTF8(z)
b42e0 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20 20 7d 0a 20 ;. r++;. }.
b42f0 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a return r;.}../*
b4300 20 54 68 69 73 20 74 65 73 74 20 66 75 6e 63 74 This test funct
b4310 69 6f 6e 20 69 73 20 6e 6f 74 20 63 75 72 72 65 ion is not curre
b4320 6e 74 6c 79 20 75 73 65 64 20 62 79 20 74 68 65 ntly used by the
b4330 20 61 75 74 6f 6d 61 74 65 64 20 74 65 73 74 2d automated test-
b4340 73 75 69 74 65 2e 20 0a 2a 2a 20 48 65 6e 63 65 suite. .** Hence
b4350 20 69 74 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 it is only avai
b4360 6c 61 62 6c 65 20 69 6e 20 64 65 62 75 67 20 62 lable in debug b
b4370 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 uilds..*/.#if de
b4380 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 fined(SQLITE_TES
b4390 54 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 T) && defined(SQ
b43a0 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a LITE_DEBUG)./*.*
b43b0 2a 20 54 72 61 6e 73 6c 61 74 65 20 55 54 46 2d * Translate UTF-
b43c0 38 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 8 to UTF-8..**.*
b43d0 2a 20 54 68 69 73 20 68 61 73 20 74 68 65 20 65 * This has the e
b43e0 66 66 65 63 74 20 6f 66 20 6d 61 6b 69 6e 67 20 ffect of making
b43f0 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 74 sure that the st
b4400 72 69 6e 67 20 69 73 20 77 65 6c 6c 2d 66 6f 72 ring is well-for
b4410 6d 65 64 0a 2a 2a 20 55 54 46 2d 38 2e 20 20 4d med.** UTF-8. M
b4420 69 73 63 6f 64 65 64 20 63 68 61 72 61 63 74 65 iscoded characte
b4430 72 73 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 0a rs are removed..
b4440 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73 6c **.** The transl
b4450 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e ation is done in
b4460 2d 70 6c 61 63 65 20 28 73 69 6e 63 65 20 69 74 -place (since it
b4470 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 is impossible f
b4480 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63 or the.** correc
b4490 74 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 t UTF-8 encoding
b44a0 20 74 6f 20 62 65 20 6c 6f 6e 67 65 72 20 74 68 to be longer th
b44b0 61 6e 20 61 20 6d 61 6c 66 6f 72 6d 65 64 20 65 an a malformed e
b44c0 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f 0a 53 51 4c ncoding)..*/.SQL
b44d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b44e0 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75 sqlite3Utf8To8(u
b44f0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 nsigned char *zI
b4500 6e 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 n){. unsigned c
b4510 68 61 72 20 2a 7a 4f 75 74 20 3d 20 7a 49 6e 3b har *zOut = zIn;
b4520 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
b4530 20 2a 7a 53 74 61 72 74 20 3d 20 7a 49 6e 3b 0a *zStart = zIn;.
b4540 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
b4550 2a 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 73 71 *zTerm = &zIn[sq
b4560 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 28 63 lite3Strlen30((c
b4570 68 61 72 20 2a 29 7a 49 6e 29 5d 3b 0a 20 20 75 har *)zIn)];. u
b4580 33 32 20 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 32 c;.. while(
b4590 7a 49 6e 5b 30 5d 20 29 7b 0a 20 20 20 20 63 20 zIn[0] ){. c
b45a0 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 = sqlite3Utf8Rea
b45b0 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 d(zIn, zTerm, (c
b45c0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b onst u8**)&zIn);
b45d0 0a 20 20 20 20 69 66 28 20 63 21 3d 30 78 66 66 . if( c!=0xff
b45e0 66 64 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54 fd ){. WRIT
b45f0 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b E_UTF8(zOut, c);
b4600 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f . }. }. *zO
b4610 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e ut = 0;. return
b4620 20 7a 4f 75 74 20 2d 20 7a 53 74 61 72 74 3b 0a zOut - zStart;.
b4630 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 }.#endif..#ifnde
b4640 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
b4650 46 31 36 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 F16./*.** Conver
b4660 74 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e t a UTF-16 strin
b4670 67 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 g in the native
b4680 65 6e 63 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20 encoding into a
b4690 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a UTF-8 string..**
b46a0 20 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 Memory to hold
b46b0 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 the UTF-8 string
b46c0 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f is obtained fro
b46d0 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
b46e0 20 61 6e 64 20 6d 75 73 74 0a 2a 2a 20 62 65 20 and must.** be
b46f0 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c freed by the cal
b4700 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a ling function..*
b4710 2a 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 *.** NULL is ret
b4720 75 72 6e 65 64 20 69 66 20 74 68 65 72 65 20 69 urned if there i
b4730 73 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 s an allocation
b4740 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 error..*/.SQLITE
b4750 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
b4760 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 73 qlite3Utf16to8(s
b4770 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
b4780 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e t void *z, int n
b4790 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a Byte){. Mem m;.
b47a0 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 memset(&m, 0,
b47b0 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e sizeof(m));. m.
b47c0 64 62 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 db = db;. sqlit
b47d0 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
b47e0 26 6d 2c 20 7a 2c 20 6e 42 79 74 65 2c 20 53 51 &m, z, nByte, SQ
b47f0 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 LITE_UTF16NATIVE
b4800 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 , SQLITE_STATIC)
b4810 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 ;. sqlite3VdbeC
b4820 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d hangeEncoding(&m
b4830 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a , SQLITE_UTF8);.
b4840 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
b4850 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 Failed ){. sq
b4860 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
b4870 61 73 65 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a ase(&m);. m.z
b4880 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 = 0;. }. asse
b4890 72 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d rt( (m.flags & M
b48a0 45 4d 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 EM_Term)!=0 || d
b48b0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
b48c0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e );. assert( (m.
b48d0 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 flags & MEM_Str)
b48e0 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f !=0 || db->mallo
b48f0 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 cFailed );. ret
b4900 75 72 6e 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d urn (m.flags & M
b4910 45 4d 5f 44 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a EM_Dyn)!=0 ? m.z
b4920 20 3a 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 : sqlite3DbStrD
b4930 75 70 28 64 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a up(db, m.z);.}..
b4940 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 /*.** pZ is a UT
b4950 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 F-16 encoded uni
b4960 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20 code string. If
b4970 6e 43 68 61 72 20 69 73 20 6c 65 73 73 20 74 68 nChar is less th
b4980 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 an zero,.** retu
b4990 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
b49a0 20 62 79 74 65 73 20 75 70 20 74 6f 20 28 62 75 bytes up to (bu
b49b0 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 t not including)
b49c0 2c 20 74 68 65 20 66 69 72 73 74 20 70 61 69 72 , the first pair
b49d0 0a 2a 2a 20 6f 66 20 63 6f 6e 73 65 63 75 74 69 .** of consecuti
b49e0 76 65 20 30 78 30 30 20 62 79 74 65 73 20 69 6e ve 0x00 bytes in
b49f0 20 70 5a 2e 20 49 66 20 6e 43 68 61 72 20 69 73 pZ. If nChar is
b4a00 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e 20 7a not less than z
b4a10 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 ero,.** then ret
b4a20 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
b4a30 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66 f bytes in the f
b4a40 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69 63 6f irst nChar unico
b4a50 64 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a de characters.**
b4a60 20 69 6e 20 70 5a 20 28 6f 72 20 75 70 20 75 6e in pZ (or up un
b4a70 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 70 61 til the first pa
b4a80 69 72 20 6f 66 20 30 78 30 30 20 62 79 74 65 73 ir of 0x00 bytes
b4a90 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 , whichever come
b4aa0 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c s first)..*/.SQL
b4ab0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b4ac0 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65 sqlite3Utf16Byte
b4ad0 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a Len(const void *
b4ae0 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b zIn, int nChar){
b4af0 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
b4b00 63 20 3d 20 31 3b 0a 20 20 63 68 61 72 20 63 6f c = 1;. char co
b4b10 6e 73 74 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 nst *z = zIn;.
b4b20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28 int n = 0;. if(
b4b30 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
b4b40 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 IVE==SQLITE_UTF1
b4b50 36 42 45 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 6BE ){. /* Us
b4b60 69 6e 67 20 61 6e 20 22 69 66 20 28 53 51 4c 49 ing an "if (SQLI
b4b70 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d 3d TE_UTF16NATIVE==
b4b80 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29 22 SQLITE_UTF16BE)"
b4b90 20 63 6f 6e 73 74 72 75 63 74 20 68 65 72 65 0a construct here.
b4ba0 20 20 20 20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 ** and in ot
b4bb0 68 65 72 20 70 61 72 74 73 20 6f 66 20 74 68 69 her parts of thi
b4bc0 73 20 66 69 6c 65 20 6d 65 61 6e 73 20 74 68 61 s file means tha
b4bd0 74 20 61 74 20 6f 6e 65 20 62 72 61 6e 63 68 20 t at one branch
b4be0 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 will. ** not
b4bf0 62 65 20 63 6f 76 65 72 65 64 20 62 79 20 63 6f be covered by co
b4c00 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 20 6f verage testing o
b4c10 6e 20 61 6e 79 20 73 69 6e 67 6c 65 20 68 6f 73 n any single hos
b4c20 74 2e 20 42 75 74 20 63 6f 76 65 72 61 67 65 0a t. But coverage.
b4c30 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 ** will be c
b4c40 6f 6d 70 6c 65 74 65 20 69 66 20 74 68 65 20 74 omplete if the t
b4c50 65 73 74 73 20 61 72 65 20 72 75 6e 20 6f 6e 20 ests are run on
b4c60 62 6f 74 68 20 61 20 6c 69 74 74 6c 65 2d 65 6e both a little-en
b4c70 64 69 61 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a dian and . **
b4c80 20 62 69 67 2d 65 6e 64 69 61 6e 20 68 6f 73 74 big-endian host
b4c90 2e 20 42 65 63 61 75 73 65 20 62 6f 74 68 20 74 . Because both t
b4ca0 68 65 20 55 54 46 31 36 4e 41 54 49 56 45 20 61 he UTF16NATIVE a
b4cb0 6e 64 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 nd SQLITE_UTF16B
b4cc0 45 0a 20 20 20 20 2a 2a 20 6d 61 63 72 6f 73 20 E. ** macros
b4cd0 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 are constant at
b4ce0 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 74 68 65 compile time the
b4cf0 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e 20 64 65 compiler can de
b4d00 74 65 72 6d 69 6e 65 0a 20 20 20 20 2a 2a 20 77 termine. ** w
b4d10 68 69 63 68 20 62 72 61 6e 63 68 20 77 69 6c 6c hich branch will
b4d20 20 62 65 20 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 be followed. It
b4d30 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 73 is therefore as
b4d40 73 75 6d 65 64 20 74 68 61 74 20 6e 6f 20 72 75 sumed that no ru
b4d50 6e 74 69 6d 65 0a 20 20 20 20 2a 2a 20 70 65 6e ntime. ** pen
b4d60 61 6c 74 79 20 69 73 20 70 61 69 64 20 66 6f 72 alty is paid for
b4d70 20 74 68 69 73 20 22 69 66 22 20 73 74 61 74 65 this "if" state
b4d80 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ment.. */.
b4d90 20 77 68 69 6c 65 28 20 63 20 26 26 20 28 28 6e while( c && ((n
b4da0 43 68 61 72 3c 30 29 20 7c 7c 20 6e 3c 6e 43 68 Char<0) || n<nCh
b4db0 61 72 29 20 29 7b 0a 20 20 20 20 20 20 52 45 41 ar) ){. REA
b4dc0 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b D_UTF16BE(z, c);
b4dd0 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 . n++;.
b4de0 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 }. }else{. w
b4df0 68 69 6c 65 28 20 63 20 26 26 20 28 28 6e 43 68 hile( c && ((nCh
b4e00 61 72 3c 30 29 20 7c 7c 20 6e 3c 6e 43 68 61 72 ar<0) || n<nChar
b4e10 29 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f ) ){. READ_
b4e20 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 UTF16LE(z, c);.
b4e30 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a n++;. }.
b4e40 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e }. return (in
b4e50 74 29 28 7a 2d 28 63 68 61 72 20 63 6f 6e 73 74 t)(z-(char const
b4e60 20 2a 29 7a 49 6e 29 2d 28 28 63 3d 3d 30 29 3f *)zIn)-((c==0)?
b4e70 32 3a 30 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 2:0);.}..#if def
b4e80 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
b4e90 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 )./*.** This rou
b4ea0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 tine is called f
b4eb0 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74 rom the TCL test
b4ec0 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73 function "trans
b4ed0 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a late_selftest"..
b4ee0 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61 ** It checks tha
b4ef0 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73 t the primitives
b4f00 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 for serializing
b4f10 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69 and deserializi
b4f20 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 ng.** characters
b4f30 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e in each encodin
b4f40 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f g are inverses o
b4f50 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f f each other..*/
b4f60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b4f70 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53 void sqlite3UtfS
b4f80 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20 elfTest(void){.
b4f90 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c unsigned int i,
b4fa0 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 t;. unsigned c
b4fb0 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20 har zBuf[20];.
b4fc0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
b4fd0 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
b4fe0 72 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 6e 74 20 r *zTerm;. int
b4ff0 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e n;. unsigned in
b5000 74 20 63 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b t c;.. for(i=0;
b5010 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 i<0x00110000; i
b5020 2b 2b 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 ++){. z = zBu
b5030 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 f;. WRITE_UTF
b5040 38 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 8(z, i);. n =
b5050 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a (int)(z-zBuf);.
b5060 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 assert( n>0
b5070 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a && n<=4 );. z
b5080 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 54 65 [0] = 0;. zTe
b5090 72 6d 20 3d 20 7a 3b 0a 20 20 20 20 7a 20 3d 20 rm = z;. z =
b50a0 7a 42 75 66 3b 0a 20 20 20 20 63 20 3d 20 73 71 zBuf;. c = sq
b50b0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 2c lite3Utf8Read(z,
b50c0 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 zTerm, (const u
b50d0 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 20 3d 8**)&z);. t =
b50e0 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 i;. if( i>=0
b50f0 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 44 46 xD800 && i<=0xDF
b5100 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b FF ) t = 0xFFFD;
b5110 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 46 46 . if( (i&0xFF
b5120 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 FFFFFE)==0xFFFE
b5130 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20 20 ) t = 0xFFFD;.
b5140 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 20 29 assert( c==t )
b5150 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a ;. assert( (z
b5160 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d -zBuf)==n );. }
b5170 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 . for(i=0; i<0x
b5180 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 00110000; i++){.
b5190 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30 if( i>=0xD80
b51a0 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 29 20 0 && i<0xE000 )
b51b0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 continue;. z
b51c0 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 = zBuf;. WRIT
b51d0 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 29 3b E_UTF16LE(z, i);
b51e0 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a . n = (int)(z
b51f0 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 -zBuf);. asse
b5200 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 rt( n>0 && n<=4
b5210 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b );. z[0] = 0;
b5220 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 . z = zBuf;.
b5230 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 READ_UTF16LE(
b5240 7a 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 65 72 z, c);. asser
b5250 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 t( c==i );. a
b5260 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d ssert( (z-zBuf)=
b5270 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 =n );. }. for(
b5280 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 i=0; i<0x0011000
b5290 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 0; i++){. if(
b52a0 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c i>=0xD800 && i<
b52b0 30 78 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 0xE000 ) continu
b52c0 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b e;. z = zBuf;
b52d0 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 . WRITE_UTF16
b52e0 42 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 BE(z, i);. n
b52f0 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b = (int)(z-zBuf);
b5300 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 . assert( n>0
b5310 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 && n<=4 );.
b5320 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 z[0] = 0;. z
b5330 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45 41 44 = zBuf;. READ
b5340 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a _UTF16BE(z, c);.
b5350 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 69 assert( c==i
b5360 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
b5370 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 (z-zBuf)==n );.
b5380 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.}.#endif /* S
b5390 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 23 65 QLITE_TEST */.#e
b53a0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
b53b0 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a MIT_UTF16 */../*
b53c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
b53d0 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a d of utf.c *****
b53e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b53f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
b5410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
b5420 67 69 6e 20 66 69 6c 65 20 75 74 69 6c 2e 63 20 gin file util.c
b5430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
b5460 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
b5470 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 15.**.** The
b5480 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
b5490 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
b54a0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
b54b0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
b54c0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
b54d0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
b54e0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
b54f0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
b5500 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
b5510 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
b5520 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
b5530 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
b5540 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
b5550 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
b5560 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
b5570 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
b5580 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
b5590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b55a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b55b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b55c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b55d0 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69 74 79 20 ****.** Utility
b55e0 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 functions used t
b55f0 68 72 6f 75 67 68 6f 75 74 20 73 71 6c 69 74 65 hroughout sqlite
b5600 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ..**.** This fil
b5610 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 e contains funct
b5620 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 ions for allocat
b5630 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 ing memory, comp
b5640 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69 6e 67 73 aring.** strings
b5650 2c 20 61 6e 64 20 73 74 75 66 66 20 6c 69 6b 65 , and stuff like
b5660 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 that..**.** $Id
b5670 3a 20 75 74 69 6c 2e 63 2c 76 20 31 2e 32 34 36 : util.c,v 1.246
b5680 20 32 30 30 39 2f 30 31 2f 31 30 20 31 36 3a 31 2009/01/10 16:1
b5690 35 3a 32 32 20 64 72 68 20 45 78 70 20 24 0a 2a 5:22 drh Exp $.*
b56a0 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e /.../*.** Routin
b56b0 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70 e needed to supp
b56c0 6f 72 74 20 74 68 65 20 74 65 73 74 63 61 73 65 ort the testcase
b56d0 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 () macro..*/.#if
b56e0 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 def SQLITE_COVER
b56f0 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f AGE_TEST.SQLITE_
b5700 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
b5710 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 ite3Coverage(int
b5720 20 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e x){. static in
b5730 74 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20 64 t dummy = 0;. d
b5740 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65 6e ummy += x;.}.#en
b5750 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 dif../*.** Routi
b5760 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 ne needed to sup
b5770 70 6f 72 74 20 74 68 65 20 41 4c 57 41 59 53 28 port the ALWAYS(
b5780 29 20 61 6e 64 20 4e 45 56 45 52 28 29 20 6d 61 ) and NEVER() ma
b5790 63 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 cros..**.** The
b57a0 61 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c 57 41 argument to ALWA
b57b0 59 53 28 29 20 73 68 6f 75 6c 64 20 61 6c 77 61 YS() should alwa
b57c0 79 73 20 62 65 20 74 72 75 65 20 61 6e 64 20 74 ys be true and t
b57d0 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 he argument.** t
b57e0 6f 20 4e 45 56 45 52 28 29 20 73 68 6f 75 6c 64 o NEVER() should
b57f0 20 61 6c 77 61 79 73 20 62 65 20 66 61 6c 73 65 always be false
b5800 2e 20 20 49 66 20 65 69 74 68 65 72 20 69 73 20 . If either is
b5810 6e 6f 74 20 74 68 65 20 63 61 73 65 0a 2a 2a 20 not the case.**
b5820 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
b5830 65 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 6f e is called in o
b5840 72 64 65 72 20 74 6f 20 74 68 72 6f 77 20 61 6e rder to throw an
b5850 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 error..**.** Th
b5860 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 is routine only
b5870 65 78 69 73 74 73 20 69 66 20 61 73 73 65 72 74 exists if assert
b5880 28 29 20 69 73 20 6f 70 65 72 61 74 69 6f 6e 61 () is operationa
b5890 6c 2e 20 20 49 74 20 61 6c 77 61 79 73 0a 2a 2a l. It always.**
b58a0 20 74 68 72 6f 77 73 20 61 6e 20 61 73 73 65 72 throws an asser
b58b0 74 20 6f 6e 20 69 74 73 20 66 69 72 73 74 20 69 t on its first i
b58c0 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 nvocation. The
b58d0 76 61 72 69 61 62 6c 65 20 68 61 73 20 61 20 6c variable has a l
b58e0 6f 6e 67 0a 2a 2a 20 6e 61 6d 65 20 74 6f 20 68 ong.** name to h
b58f0 65 6c 70 20 74 68 65 20 61 73 73 65 72 74 28 29 elp the assert()
b5900 20 6d 65 73 73 61 67 65 20 62 65 20 6d 6f 72 65 message be more
b5910 20 72 65 61 64 61 62 6c 65 2e 20 20 54 68 65 20 readable. The
b5920 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 75 variable.** is u
b5930 73 65 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 sed to prevent a
b5940 20 74 6f 6f 2d 63 6c 65 76 65 72 20 6f 70 74 69 too-clever opti
b5950 6d 69 7a 65 72 20 66 72 6f 6d 20 6f 70 74 69 6d mizer from optim
b5960 69 7a 69 6e 67 20 6f 75 74 20 74 68 65 0a 2a 2a izing out the.**
b5970 20 65 6e 74 69 72 65 20 63 61 6c 6c 2e 0a 2a 2f entire call..*/
b5980 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
b5990 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b59a0 6e 74 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 nt sqlite3Assert
b59b0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
b59c0 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 41 4c volatile int AL
b59d0 57 41 59 53 5f 77 61 73 5f 66 61 6c 73 65 5f 6f WAYS_was_false_o
b59e0 72 5f 4e 45 56 45 52 5f 77 61 73 5f 74 72 75 65 r_NEVER_was_true
b59f0 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
b5a00 41 4c 57 41 59 53 5f 77 61 73 5f 66 61 6c 73 65 ALWAYS_was_false
b5a10 5f 6f 72 5f 4e 45 56 45 52 5f 77 61 73 5f 74 72 _or_NEVER_was_tr
b5a20 75 65 20 29 3b 20 20 20 20 20 20 2f 2a 20 41 6c ue ); /* Al
b5a30 77 61 79 73 20 66 61 69 6c 73 20 2a 2f 0a 20 20 ways fails */.
b5a40 72 65 74 75 72 6e 20 41 4c 57 41 59 53 5f 77 61 return ALWAYS_wa
b5a50 73 5f 66 61 6c 73 65 5f 6f 72 5f 4e 45 56 45 52 s_false_or_NEVER
b5a60 5f 77 61 73 5f 74 72 75 65 2b 2b 3b 20 20 20 20 _was_true++;
b5a70 20 20 20 2f 2a 20 4e 6f 74 20 52 65 61 63 68 65 /* Not Reache
b5a80 64 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f d */.}.#endif../
b5a90 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
b5aa0 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 if the floating
b5ab0 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20 point value is
b5ac0 4e 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e 61 Not a Number (Na
b5ad0 4e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 N)..*/.SQLITE_PR
b5ae0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b5af0 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29 3IsNaN(double x)
b5b00 7b 0a 20 20 2f 2a 20 54 68 69 73 20 4e 61 4e 20 {. /* This NaN
b5b10 74 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 test sometimes f
b5b20 61 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 ails if compiled
b5b30 20 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 on GCC with -ff
b5b40 61 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f ast-math.. ** O
b5b50 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 n the other hand
b5b60 2c 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 , the use of -ff
b5b70 61 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 ast-math comes w
b5b80 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e ith the followin
b5b90 67 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a g. ** warning:.
b5ba0 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 **. ** T
b5bb0 68 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 his option [-ffa
b5bc0 73 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 st-math] should
b5bd0 6e 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 never be turned
b5be0 6f 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 on by any. **
b5bf0 20 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 -O option si
b5c00 6e 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c nce it can resul
b5c10 74 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f t in incorrect o
b5c20 75 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 utput for progra
b5c30 6d 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 ms. ** whi
b5c40 63 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 ch depend on an
b5c50 65 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 exact implementa
b5c60 74 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 tion of IEEE or
b5c70 49 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 ISO . ** r
b5c80 75 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 ules/specificati
b5c90 6f 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e ons for math fun
b5ca0 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a ctions.. **. *
b5cb0 2a 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 * Under MSVC, th
b5cc0 69 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 is NaN test may
b5cd0 66 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 fail if compiled
b5ce0 20 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 with a floating
b5cf0 2d 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 -. ** point pre
b5d00 63 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 cision mode othe
b5d10 72 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 r than /fp:preci
b5d20 73 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 se. From the MS
b5d30 44 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e DN . ** documen
b5d40 74 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a tation:. **. *
b5d50 2a 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 * The compi
b5d60 6c 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 ler [with /fp:pr
b5d70 65 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 ecise] will prop
b5d80 65 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 erly handle comp
b5d90 61 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 arisons . **
b5da0 20 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e involving NaN
b5db0 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 . For example, x
b5dc0 20 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 != x evaluates
b5dd0 74 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 20 to true if x is
b5de0 4e 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e NaN . ** .
b5df0 2e 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f ... */.#ifdef _
b5e00 5f 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 _FAST_MATH__.# e
b5e10 72 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c rror SQLite will
b5e20 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 not work correc
b5e30 74 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 tly with the -ff
b5e40 61 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 ast-math option
b5e50 6f 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 of GCC..#endif.
b5e60 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 volatile double
b5e70 20 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 y = x;. volati
b5e80 6c 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b le double z = y;
b5e90 0a 20 20 72 65 74 75 72 6e 20 79 21 3d 7a 3b 0a . return y!=z;.
b5ea0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 }../*.** Compute
b5eb0 20 61 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 a string length
b5ec0 20 74 68 61 74 20 69 73 20 6c 69 6d 69 74 65 64 that is limited
b5ed0 20 74 6f 20 77 68 61 74 20 63 61 6e 20 62 65 20 to what can be
b5ee0 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 6c 6f 77 stored in.** low
b5ef0 65 72 20 33 30 20 62 69 74 73 20 6f 66 20 61 20 er 30 bits of a
b5f00 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 32-bit signed in
b5f10 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 teger..*/.SQLITE
b5f20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b5f30 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e ite3Strlen30(con
b5f40 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 st char *z){. c
b5f50 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20 onst char *z2 =
b5f60 7a 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 32 20 z;. while( *z2
b5f70 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72 65 74 ){ z2++; }. ret
b5f80 75 72 6e 20 30 78 33 66 66 66 66 66 66 66 20 26 urn 0x3fffffff &
b5f90 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0a (int)(z2 - z);.
b5fa0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
b5fb0 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 the length of a
b5fc0 73 74 72 69 6e 67 2c 20 65 78 63 65 70 74 20 64 string, except d
b5fd0 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 65 20 o not allow the
b5fe0 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 0a 2a 2a string length.**
b5ff0 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 20 53 to exceed the S
b6000 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
b6010 54 48 20 73 65 74 74 69 6e 67 2e 0a 2a 2f 0a 53 TH setting..*/.S
b6020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b6030 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 t sqlite3Strlen(
b6040 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e sqlite3 *db, con
b6050 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 st char *z){. c
b6060 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20 onst char *z2 =
b6070 7a 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 z;. int len;.
b6080 69 6e 74 20 78 3b 0a 20 20 77 68 69 6c 65 28 20 int x;. while(
b6090 2a 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 *z2 ){ z2++; }.
b60a0 20 78 20 3d 20 28 69 6e 74 29 28 7a 32 20 2d 20 x = (int)(z2 -
b60b0 7a 29 3b 0a 20 20 6c 65 6e 20 3d 20 30 78 37 66 z);. len = 0x7f
b60c0 66 66 66 66 66 66 20 26 20 78 3b 0a 20 20 69 66 ffffff & x;. if
b60d0 28 20 6c 65 6e 21 3d 78 20 7c 7c 20 6c 65 6e 20 ( len!=x || len
b60e0 3e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c > db->aLimit[SQL
b60f0 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
b6100 5d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ] ){. return
b6110 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
b6120 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b E_LIMIT_LENGTH];
b6130 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
b6140 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 7d 0a turn len;. }.}.
b6150 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d ./*.** Set the m
b6160 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 ost recent error
b6170 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 code and error
b6180 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 73 string for the s
b6190 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 qlite.** handle
b61a0 22 64 62 22 2e 20 54 68 65 20 65 72 72 6f 72 20 "db". The error
b61b0 63 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 22 code is set to "
b61c0 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a err_code"..**.**
b61d0 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 If it is not NU
b61e0 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f 72 6d LL, string zForm
b61f0 61 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 at specifies the
b6200 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 0a 2a format of the.*
b6210 2a 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 69 * error string i
b6220 6e 20 74 68 65 20 73 74 79 6c 65 20 6f 66 20 74 n the style of t
b6230 68 65 20 70 72 69 6e 74 66 20 66 75 6e 63 74 69 he printf functi
b6240 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ons: The followi
b6250 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 68 61 ng.** format cha
b6260 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f racters are allo
b6270 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 wed:.**.**
b6280 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 %s Insert a
b6290 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 string.**
b62a0 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 %z A string
b62b0 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
b62c0 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a freed after use.
b62d0 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20 ** %d
b62e0 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 Insert an intege
b62f0 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 r.** %T
b6300 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e Insert a token
b6310 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 .** %S
b6320 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 Insert the firs
b6330 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 t element of a S
b6340 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f rcList.**.** zFo
b6350 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73 74 72 rmat and any str
b6360 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 ing tokens that
b6370 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20 61 73 follow it are as
b6380 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 65 sumed to be.** e
b6390 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e ncoded in UTF-8.
b63a0 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 20 .**.** To clear
b63b0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
b63c0 65 72 72 6f 72 20 66 6f 72 20 73 71 6c 69 74 65 error for sqlite
b63d0 20 68 61 6e 64 6c 65 20 22 64 62 22 2c 20 73 71 handle "db", sq
b63e0 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20 73 68 lite3Error.** sh
b63f0 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 ould be called w
b6400 69 74 68 20 65 72 72 5f 63 6f 64 65 20 73 65 74 ith err_code set
b6410 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e to SQLITE_OK an
b6420 64 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a 2a 2a d zFormat set.**
b6430 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c to NULL..*/.SQL
b6440 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b6450 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73 71 sqlite3Error(sq
b6460 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 lite3 *db, int e
b6470 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 20 63 rr_code, const c
b6480 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e har *zFormat, ..
b6490 2e 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20 .){. if( db &&
b64a0 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28 64 62 (db->pErr || (db
b64b0 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69 74 65 33 ->pErr = sqlite3
b64c0 56 61 6c 75 65 4e 65 77 28 64 62 29 29 21 3d 30 ValueNew(db))!=0
b64d0 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65 72 72 ) ){. db->err
b64e0 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64 65 3b Code = err_code;
b64f0 0a 20 20 20 20 69 66 28 20 7a 46 6f 72 6d 61 74 . if( zFormat
b6500 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a ){. char *
b6510 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69 73 74 z;. va_list
b6520 20 61 70 3b 0a 20 20 20 20 20 20 76 61 5f 73 74 ap;. va_st
b6530 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
b6540 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 ;. z = sqli
b6550 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 te3VMPrintf(db,
b6560 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
b6570 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a va_end(ap);.
b6580 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c sqlite3Val
b6590 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 ueSetStr(db->pEr
b65a0 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45 r, -1, z, SQLITE
b65b0 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 _UTF8, SQLITE_DY
b65c0 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73 NAMIC);. }els
b65d0 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
b65e0 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e ValueSetStr(db->
b65f0 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49 pErr, 0, 0, SQLI
b6600 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
b6610 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 STATIC);. }.
b6620 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 }.}../*.** Add
b6630 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
b6640 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 to pParse->zErr
b6650 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e Msg and incremen
b6660 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a t pParse->nErr..
b6670 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
b6680 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 61 72 formatting char
b6690 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 acters are allow
b66a0 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 ed:.**.** %
b66b0 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 s Insert a
b66c0 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 string.** %
b66d0 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 z A string
b66e0 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 that should be f
b66f0 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a reed after use.*
b6700 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49 * %d I
b6710 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 nsert an integer
b6720 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 .** %T
b6730 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a Insert a token.
b6740 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 20 ** %S
b6750 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 74 Insert the first
b6760 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 element of a Sr
b6770 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 cList.**.** This
b6780 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
b6790 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 70 6f be used to repo
b67a0 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74 68 61 rt any error tha
b67b0 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73 74 0a t occurs whilst.
b67c0 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20 ** compiling an
b67d0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 28 69 SQL statement (i
b67e0 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c 69 74 .e. within sqlit
b67f0 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e 20 54 e3_prepare()). T
b6800 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67 he.** last thing
b6810 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 the sqlite3_pre
b6820 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 pare() function
b6830 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74 68 65 does is copy the
b6840 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64 error.** stored
b6850 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
b6860 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 n into the datab
b6870 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69 6e 67 ase handle using
b6880 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e sqlite3Error().
b6890 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c .** Function sql
b68a0 69 74 65 33 45 72 72 6f 72 28 29 20 73 68 6f 75 ite3Error() shou
b68b0 6c 64 20 62 65 20 75 73 65 64 20 64 75 72 69 6e ld be used durin
b68c0 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78 65 63 g statement exec
b68d0 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65 ution.** (sqlite
b68e0 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29 2e 0a 3_step() etc.)..
b68f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b6900 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 E void sqlite3Er
b6910 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a 70 50 rorMsg(Parse *pP
b6920 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 arse, const char
b6930 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
b6940 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
b6950 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
b6960 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 61 Parse->db;. pPa
b6970 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 73 rse->nErr++;. s
b6980 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
b6990 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 pParse->zErrMsg
b69a0 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 );. va_start(ap
b69b0 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50 , zFormat);. pP
b69c0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 arse->zErrMsg =
b69d0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 sqlite3VMPrintf(
b69e0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 db, zFormat, ap)
b69f0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
b6a00 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 if( pParse->rc
b6a10 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
b6a20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 pParse->rc =
b6a30 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
b6a40 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 }.}../*.** Clear
b6a50 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
b6a60 67 65 20 69 6e 20 70 50 61 72 73 65 2c 20 69 66 ge in pParse, if
b6a70 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 any.*/.SQLITE_P
b6a80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
b6a90 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61 te3ErrorClear(Pa
b6aa0 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 rse *pParse){.
b6ab0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 sqlite3DbFree(pP
b6ac0 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 arse->db, pParse
b6ad0 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50 ->zErrMsg);. pP
b6ae0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 arse->zErrMsg =
b6af0 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 0;. pParse->nEr
b6b00 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 r = 0;.}../*.**
b6b10 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 Convert an SQL-s
b6b20 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69 tyle quoted stri
b6b30 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c ng into a normal
b6b40 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 string by remov
b6b50 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 ing.** the quote
b6b60 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 characters. Th
b6b70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 e conversion is
b6b80 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 done in-place.
b6b90 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 If the.** input
b6ba0 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 does not begin w
b6bb0 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72 ith a quote char
b6bc0 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73 acter, then this
b6bd0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 routine.** is a
b6be0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 32 30 no-op..**.** 20
b6bf0 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69 73 20 02-Feb-14: This
b6c00 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74 65 6e routine is exten
b6c10 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 4d 53 ded to remove MS
b6c20 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a 2a 2a -Access style.**
b6c30 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d 20 61 brackets from a
b6c40 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65 72 73 round identifers
b6c50 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 20 . For example:
b6c60 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63 6f 6d "[a-b-c]" becom
b6c70 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e 0a 2a es.** "a-b-c"..*
b6c80 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b6c90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 71 void sqlite3Deq
b6ca0 75 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0a 20 uote(char *z){.
b6cb0 20 63 68 61 72 20 71 75 6f 74 65 3b 0a 20 20 69 char quote;. i
b6cc0 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a nt i, j;. if( z
b6cd0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
b6ce0 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 quote = z[0];.
b6cf0 73 77 69 74 63 68 28 20 71 75 6f 74 65 20 29 7b switch( quote ){
b6d00 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20 . case '\'':
b6d10 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 break;. case
b6d20 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20 '"': break;.
b6d30 20 20 20 63 61 73 65 20 27 60 27 3a 20 20 20 62 case '`': b
b6d40 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20 reak;
b6d50 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51 /* For MySQ
b6d60 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 L compatibility
b6d70 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a */. case '[':
b6d80 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20 quote = ']';
b6d90 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 break; /* For
b6da0 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63 6f 6d MS SqlServer com
b6db0 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 patibility */.
b6dc0 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 72 65 default: re
b6dd0 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 turn;. }. for(
b6de0 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 i=1, j=0; z[i];
b6df0 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b i++){. if( z[
b6e00 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 i]==quote ){.
b6e10 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 if( z[i+1]==q
b6e20 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 uote ){.
b6e30 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a z[j++] = quote;.
b6e40 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 i++;.
b6e50 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
b6e60 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20 z[j++] = 0;.
b6e70 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
b6e80 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
b6e90 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a z[j++] = z
b6ea0 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d [i];. }. }.}
b6eb0 0a 0a 2f 2a 20 43 6f 6e 76 65 6e 69 65 6e 74 20 ../* Convenient
b6ec0 73 68 6f 72 74 2d 68 61 6e 64 20 2a 2f 0a 23 64 short-hand */.#d
b6ed0 65 66 69 6e 65 20 55 70 70 65 72 54 6f 4c 6f 77 efine UpperToLow
b6ee0 65 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 er sqlite3UpperT
b6ef0 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f oLower../*.** So
b6f00 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76 65 20 me systems have
b6f10 73 74 72 69 63 6d 70 28 29 2e 20 20 4f 74 68 65 stricmp(). Othe
b6f20 72 73 20 68 61 76 65 20 73 74 72 63 61 73 65 63 rs have strcasec
b6f30 6d 70 28 29 2e 20 20 42 65 63 61 75 73 65 0a 2a mp(). Because.*
b6f40 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 6f * there is no co
b6f50 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 20 77 69 nsistency, we wi
b6f60 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 ll define our ow
b6f70 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
b6f80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b6f90 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 StrICmp(const ch
b6fa0 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 ar *zLeft, const
b6fb0 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29 7b 0a char *zRight){.
b6fc0 20 20 72 65 67 69 73 74 65 72 20 75 6e 73 69 67 register unsig
b6fd0 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b ned char *a, *b;
b6fe0 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 . a = (unsigned
b6ff0 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 char *)zLeft;.
b7000 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 b = (unsigned c
b7010 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 har *)zRight;.
b7020 77 68 69 6c 65 28 20 2a 61 21 3d 30 20 26 26 20 while( *a!=0 &&
b7030 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d UpperToLower[*a]
b7040 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a ==UpperToLower[*
b7050 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d b]){ a++; b++; }
b7060 0a 20 20 72 65 74 75 72 6e 20 55 70 70 65 72 54 . return UpperT
b7070 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70 70 oLower[*a] - Upp
b7080 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d erToLower[*b];.}
b7090 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b70a0 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 4e 49 int sqlite3StrNI
b70b0 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a Cmp(const char *
b70c0 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 zLeft, const cha
b70d0 72 20 2a 7a 52 69 67 68 74 2c 20 69 6e 74 20 4e r *zRight, int N
b70e0 29 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e ){. register un
b70f0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 signed char *a,
b7100 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 *b;. a = (unsig
b7110 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 ned char *)zLeft
b7120 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 ;. b = (unsigne
b7130 64 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b d char *)zRight;
b7140 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 . while( N-- >
b7150 30 20 26 26 20 2a 61 21 3d 30 20 26 26 20 55 70 0 && *a!=0 && Up
b7160 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d perToLower[*a]==
b7170 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d UpperToLower[*b]
b7180 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 ){ a++; b++; }.
b7190 20 72 65 74 75 72 6e 20 4e 3c 30 20 3f 20 30 20 return N<0 ? 0
b71a0 3a 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a : UpperToLower[*
b71b0 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 a] - UpperToLowe
b71c0 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 r[*b];.}../*.**
b71d0 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a Return TRUE if z
b71e0 20 69 73 20 61 20 70 75 72 65 20 6e 75 6d 65 72 is a pure numer
b71f0 69 63 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 ic string. Retu
b7200 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 0a rn FALSE if the.
b7210 2a 2a 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 ** string contai
b7220 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 ns any character
b7230 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 70 61 which is not pa
b7240 72 74 20 6f 66 20 61 20 6e 75 6d 62 65 72 2e 20 rt of a number.
b7250 49 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 If.** the string
b7260 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 is numeric and
b7270 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 27 2e 27 contains the '.'
b7280 20 63 68 61 72 61 63 74 65 72 2c 20 73 65 74 20 character, set
b7290 2a 72 65 61 6c 6e 75 6d 0a 2a 2a 20 74 6f 20 54 *realnum.** to T
b72a0 52 55 45 20 28 6f 74 68 65 72 77 69 73 65 20 46 RUE (otherwise F
b72b0 41 4c 53 45 29 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 ALSE)..**.** An
b72c0 65 6d 70 74 79 20 73 74 72 69 6e 67 20 69 73 20 empty string is
b72d0 63 6f 6e 73 69 64 65 72 65 64 20 6e 6f 6e 2d 6e considered non-n
b72e0 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 umeric..*/.SQLIT
b72f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b7300 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 63 6f lite3IsNumber(co
b7310 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
b7320 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 20 65 6e *realnum, u8 en
b7330 63 29 7b 0a 20 20 69 6e 74 20 69 6e 63 72 20 3d c){. int incr =
b7340 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 (enc==SQLITE_UT
b7350 46 38 3f 31 3a 32 29 3b 0a 20 20 69 66 28 20 65 F8?1:2);. if( e
b7360 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 nc==SQLITE_UTF16
b7370 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 BE ) z++;. if(
b7380 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 *z=='-' || *z=='
b7390 2b 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a +' ) z += incr;.
b73a0 20 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a if( !isdigit(*
b73b0 28 75 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20 72 (u8*)z) ){. r
b73c0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a eturn 0;. }. z
b73d0 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66 28 20 += incr;. if(
b73e0 72 65 61 6c 6e 75 6d 20 29 20 2a 72 65 61 6c 6e realnum ) *realn
b73f0 75 6d 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 um = 0;. while(
b7400 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a isdigit(*(u8*)z
b7410 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 ) ){ z += incr;
b7420 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 }. if( *z=='.'
b7430 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 ){. z += incr
b7440 3b 0a 20 20 20 20 69 66 28 20 21 69 73 64 69 67 ;. if( !isdig
b7450 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 20 72 65 it(*(u8*)z) ) re
b7460 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c turn 0;. whil
b7470 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a e( isdigit(*(u8*
b7480 29 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 )z) ){ z += incr
b7490 3b 20 7d 0a 20 20 20 20 69 66 28 20 72 65 61 6c ; }. if( real
b74a0 6e 75 6d 20 29 20 2a 72 65 61 6c 6e 75 6d 20 3d num ) *realnum =
b74b0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 1;. }. if( *z
b74c0 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 =='e' || *z=='E'
b74d0 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 ){. z += inc
b74e0 72 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 r;. if( *z=='
b74f0 2b 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20 +' || *z=='-' )
b7500 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 z += incr;. i
b7510 66 28 20 21 69 73 64 69 67 69 74 28 2a 28 75 38 f( !isdigit(*(u8
b7520 2a 29 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b *)z) ) return 0;
b7530 0a 20 20 20 20 77 68 69 6c 65 28 20 69 73 64 69 . while( isdi
b7540 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 20 git(*(u8*)z) ){
b7550 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 z += incr; }.
b7560 20 69 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a if( realnum ) *
b7570 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d realnum = 1;. }
b7580 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b . return *z==0;
b7590 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 .}../*.** The st
b75a0 72 69 6e 67 20 7a 5b 5d 20 69 73 20 61 6e 20 61 ring z[] is an a
b75b0 73 63 69 69 20 72 65 70 72 65 73 65 6e 74 61 74 scii representat
b75c0 69 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20 6e 75 ion of a real nu
b75d0 6d 62 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 mber..** Convert
b75e0 20 74 68 69 73 20 73 74 72 69 6e 67 20 74 6f 20 this string to
b75f0 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 a double..**.**
b7600 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73 73 This routine ass
b7610 75 6d 65 73 20 74 68 61 74 20 7a 5b 5d 20 72 65 umes that z[] re
b7620 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 ally is a valid
b7630 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 0a 2a number. If it.*
b7640 2a 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 72 65 * is not, the re
b7650 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65 sult is undefine
b7660 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f d..**.** This ro
b7670 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e utine is used in
b7680 73 74 65 61 64 20 6f 66 20 74 68 65 20 6c 69 62 stead of the lib
b7690 72 61 72 79 20 61 74 6f 66 28 29 20 66 75 6e 63 rary atof() func
b76a0 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 tion because.**
b76b0 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 the library atof
b76c0 28 29 20 6d 69 67 68 74 20 77 61 6e 74 20 74 6f () might want to
b76d0 20 75 73 65 20 22 2c 22 20 61 73 20 74 68 65 20 use "," as the
b76e0 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e decimal point in
b76f0 73 74 65 61 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 stead.** of "."
b7700 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 depending on how
b7710 20 6c 6f 63 61 6c 65 20 69 73 20 73 65 74 2e 20 locale is set.
b7720 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64 20 But that would
b7730 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 0a 2a cause problems.*
b7740 2a 20 66 6f 72 20 53 51 4c 2e 20 20 53 6f 20 74 * for SQL. So t
b7750 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 his routine alwa
b7760 79 73 20 75 73 65 73 20 22 2e 22 20 72 65 67 61 ys uses "." rega
b7770 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65 rdless of locale
b7780 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b7790 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 ATE int sqlite3A
b77a0 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a toF(const char *
b77b0 7a 2c 20 64 6f 75 62 6c 65 20 2a 70 52 65 73 75 z, double *pResu
b77c0 6c 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c lt){.#ifndef SQL
b77d0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
b77e0 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 73 69 G_POINT. int si
b77f0 67 6e 20 3d 20 31 3b 0a 20 20 63 6f 6e 73 74 20 gn = 1;. const
b7800 63 68 61 72 20 2a 7a 42 65 67 69 6e 20 3d 20 7a char *zBegin = z
b7810 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 ;. LONGDOUBLE_T
b7820 59 50 45 20 76 31 20 3d 20 30 2e 30 3b 0a 20 20 YPE v1 = 0.0;.
b7830 69 6e 74 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 int nSignificant
b7840 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 = 0;. while( i
b7850 73 73 70 61 63 65 28 2a 28 75 38 2a 29 7a 29 20 sspace(*(u8*)z)
b7860 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d ) z++;. if( *z=
b7870 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e ='-' ){. sign
b7880 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a = -1;. z++;.
b7890 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d }else if( *z==
b78a0 27 2b 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a '+' ){. z++;.
b78b0 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a 5b 30 }. while( z[0
b78c0 5d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 2b ]=='0' ){. z+
b78d0 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 +;. }. while(
b78e0 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 isdigit(*(u8*)z)
b78f0 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 76 31 2a ){. v1 = v1*
b7900 31 30 2e 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 10.0 + (*z - '0'
b7910 29 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 );. z++;.
b7920 6e 53 69 67 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a nSignificant++;.
b7930 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e }. if( *z=='.
b7940 27 20 29 7b 0a 20 20 20 20 4c 4f 4e 47 44 4f 55 ' ){. LONGDOU
b7950 42 4c 45 5f 54 59 50 45 20 64 69 76 69 73 6f 72 BLE_TYPE divisor
b7960 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b 2b 3b = 1.0;. z++;
b7970 0a 20 20 20 20 69 66 28 20 6e 53 69 67 6e 69 66 . if( nSignif
b7980 69 63 61 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 icant==0 ){.
b7990 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 while( z[0]=='
b79a0 30 27 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69 0' ){. di
b79b0 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b 0a 20 visor *= 10.0;.
b79c0 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 z++;.
b79d0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 }. }. wh
b79e0 69 6c 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 ile( isdigit(*(u
b79f0 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20 20 20 69 8*)z) ){. i
b7a00 66 28 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 3c f( nSignificant<
b7a10 31 38 20 29 7b 0a 20 20 20 20 20 20 20 20 76 31 18 ){. v1
b7a20 20 3d 20 76 31 2a 31 30 2e 30 20 2b 20 28 2a 7a = v1*10.0 + (*z
b7a30 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 20 - '0');.
b7a40 20 64 69 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 divisor *= 10.0
b7a50 3b 0a 20 20 20 20 20 20 20 20 6e 53 69 67 6e 69 ;. nSigni
b7a60 66 69 63 61 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 ficant++;.
b7a70 7d 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 }. z++;.
b7a80 20 7d 0a 20 20 20 20 76 31 20 2f 3d 20 64 69 76 }. v1 /= div
b7a90 69 73 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 isor;. }. if(
b7aa0 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 *z=='e' || *z=='
b7ab0 45 27 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 73 E' ){. int es
b7ac0 69 67 6e 20 3d 20 31 3b 0a 20 20 20 20 69 6e 74 ign = 1;. int
b7ad0 20 65 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 4c eval = 0;. L
b7ae0 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 73 ONGDOUBLE_TYPE s
b7af0 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 cale = 1.0;.
b7b00 7a 2b 2b 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d z++;. if( *z=
b7b10 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 65 73 ='-' ){. es
b7b20 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 ign = -1;.
b7b30 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 z++;. }else i
b7b40 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 f( *z=='+' ){.
b7b50 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 z++;. }.
b7b60 20 20 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 while( isdigi
b7b70 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 0a 20 20 t(*(u8*)z) ){.
b7b80 20 20 20 20 65 76 61 6c 20 3d 20 65 76 61 6c 2a eval = eval*
b7b90 31 30 20 2b 20 2a 7a 20 2d 20 27 30 27 3b 0a 20 10 + *z - '0';.
b7ba0 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a z++;. }.
b7bb0 20 20 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e while( eval>
b7bc0 3d 36 34 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 =64 ){ scale *=
b7bd0 31 2e 30 65 2b 36 34 3b 20 65 76 61 6c 20 2d 3d 1.0e+64; eval -=
b7be0 20 36 34 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65 64; }. while
b7bf0 28 20 65 76 61 6c 3e 3d 31 36 20 29 7b 20 73 63 ( eval>=16 ){ sc
b7c00 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 36 3b 20 ale *= 1.0e+16;
b7c10 65 76 61 6c 20 2d 3d 20 31 36 3b 20 7d 0a 20 20 eval -= 16; }.
b7c20 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 34 while( eval>=4
b7c30 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 ){ scale *= 1.0
b7c40 65 2b 34 3b 20 65 76 61 6c 20 2d 3d 20 34 3b 20 e+4; eval -= 4;
b7c50 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76 61 }. while( eva
b7c60 6c 3e 3d 31 20 29 7b 20 73 63 61 6c 65 20 2a 3d l>=1 ){ scale *=
b7c70 20 31 2e 30 65 2b 31 3b 20 65 76 61 6c 20 2d 3d 1.0e+1; eval -=
b7c80 20 31 3b 20 7d 0a 20 20 20 20 69 66 28 20 65 73 1; }. if( es
b7c90 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 76 ign<0 ){. v
b7ca0 31 20 2f 3d 20 73 63 61 6c 65 3b 0a 20 20 20 20 1 /= scale;.
b7cb0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 31 20 }else{. v1
b7cc0 2a 3d 20 73 63 61 6c 65 3b 0a 20 20 20 20 7d 0a *= scale;. }.
b7cd0 20 20 7d 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d }. *pResult =
b7ce0 20 28 64 6f 75 62 6c 65 29 28 73 69 67 6e 3c 30 (double)(sign<0
b7cf0 20 3f 20 2d 76 31 20 3a 20 76 31 29 3b 0a 20 20 ? -v1 : v1);.
b7d00 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 20 2d return (int)(z -
b7d10 20 7a 42 65 67 69 6e 29 3b 0a 23 65 6c 73 65 0a zBegin);.#else.
b7d20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
b7d30 41 74 6f 69 36 34 28 7a 2c 20 70 52 65 73 75 6c Atoi64(z, pResul
b7d40 74 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 t);.#endif /* SQ
b7d50 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
b7d60 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f NG_POINT */.}../
b7d70 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 *.** Compare the
b7d80 20 31 39 2d 63 68 61 72 61 63 74 65 72 20 73 74 19-character st
b7d90 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e 73 ring zNum agains
b7da0 74 20 74 68 65 20 74 65 78 74 20 72 65 70 72 65 t the text repre
b7db0 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61 6c sentation.** val
b7dc0 75 65 20 32 5e 36 33 3a 20 20 39 32 32 33 33 37 ue 2^63: 922337
b7dd0 32 30 33 36 38 35 34 37 37 35 38 30 38 2e 20 20 2036854775808.
b7de0 52 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c Return negative,
b7df0 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 zero, or positi
b7e00 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69 73 ve.** if zNum is
b7e10 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 less than, equa
b7e20 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 l to, or greater
b7e30 20 74 68 61 6e 20 74 68 65 20 73 74 72 69 6e 67 than the string
b7e40 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d ..**.** Unlike m
b7e50 65 6d 63 6d 70 28 29 20 74 68 69 73 20 72 6f 75 emcmp() this rou
b7e60 74 69 6e 65 20 69 73 20 67 75 61 72 61 6e 74 65 tine is guarante
b7e70 65 64 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 ed to return the
b7e80 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 69 difference.** i
b7e90 6e 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 n the values of
b7ea0 74 68 65 20 6c 61 73 74 20 64 69 67 69 74 20 69 the last digit i
b7eb0 66 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 f the only diffe
b7ec0 72 65 6e 63 65 20 69 73 20 69 6e 20 74 68 65 0a rence is in the.
b7ed0 2a 2a 20 6c 61 73 74 20 64 69 67 69 74 2e 20 20 ** last digit.
b7ee0 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c So, for example,
b7ef0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d 70 .**.** comp
b7f00 61 72 65 32 70 6f 77 36 33 28 22 39 32 32 33 33 are2pow63("92233
b7f10 37 32 30 33 36 38 35 34 37 37 35 38 30 30 22 29 72036854775800")
b7f20 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 .**.** will retu
b7f30 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 rn -8..*/.static
b7f40 20 69 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f 77 int compare2pow
b7f50 36 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 63(const char *z
b7f60 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 Num){. int c;.
b7f70 20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75 6d c = memcmp(zNum
b7f80 2c 22 39 32 32 33 33 37 32 30 33 36 38 35 34 37 ,"92233720368547
b7f90 37 35 38 30 22 2c 31 38 29 3b 0a 20 20 69 66 28 7580",18);. if(
b7fa0 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63 20 3d c==0 ){. c =
b7fb0 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27 38 27 3b zNum[18] - '8';
b7fc0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 3b . }. return c;
b7fd0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 .}.../*.** Retur
b7fe0 6e 20 54 52 55 45 20 69 66 20 7a 4e 75 6d 20 69 n TRUE if zNum i
b7ff0 73 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 s a 64-bit signe
b8000 64 20 69 6e 74 65 67 65 72 20 61 6e 64 20 77 72 d integer and wr
b8010 69 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 ite.** the value
b8020 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 of the integer
b8030 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20 49 66 20 into *pNum. If
b8040 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 zNum is not an i
b8050 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20 69 73 20 nteger.** or is
b8060 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 an integer that
b8070 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 is too large to
b8080 62 65 20 65 78 70 72 65 73 73 65 64 20 77 69 74 be expressed wit
b8090 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a 20 74 68 h 64 bits,.** th
b80a0 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e en return false.
b80b0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 .**.** When this
b80c0 20 72 6f 75 74 69 6e 65 20 77 61 73 20 6f 72 69 routine was ori
b80d0 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 ginally written
b80e0 69 74 20 64 65 61 6c 74 20 77 69 74 68 20 6f 6e it dealt with on
b80f0 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20 6e 75 6d ly.** 32-bit num
b8100 62 65 72 73 2e 20 20 41 74 20 74 68 61 74 20 74 bers. At that t
b8110 69 6d 65 2c 20 69 74 20 77 61 73 20 6d 75 63 68 ime, it was much
b8120 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 65 faster than the
b8130 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69 62 72 61 .** atoi() libra
b8140 72 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 52 65 ry routine in Re
b8150 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a 53 51 4c dHat 7.2..*/.SQL
b8160 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b8170 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63 6f sqlite3Atoi64(co
b8180 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 nst char *zNum,
b8190 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20 20 69 36 i64 *pNum){. i6
b81a0 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 4 v = 0;. int n
b81b0 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a eg;. int i, c;.
b81c0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 const char *zS
b81d0 74 61 72 74 3b 0a 20 20 77 68 69 6c 65 28 20 69 tart;. while( i
b81e0 73 73 70 61 63 65 28 2a 28 75 38 2a 29 7a 4e 75 sspace(*(u8*)zNu
b81f0 6d 29 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 69 m) ) zNum++;. i
b8200 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b f( *zNum=='-' ){
b8210 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 . neg = 1;.
b8220 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 zNum++;. }els
b8230 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 e if( *zNum=='+'
b8240 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b ){. neg = 0;
b8250 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d . zNum++;. }
b8260 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 else{. neg =
b8270 30 3b 0a 20 20 7d 0a 20 20 7a 53 74 61 72 74 20 0;. }. zStart
b8280 3d 20 7a 4e 75 6d 3b 0a 20 20 77 68 69 6c 65 28 = zNum;. while(
b8290 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 7b zNum[0]=='0' ){
b82a0 20 7a 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a 20 53 6b zNum++; } /* Sk
b82b0 69 70 20 6f 76 65 72 20 6c 65 61 64 69 6e 67 20 ip over leading
b82c0 7a 65 72 6f 73 2e 20 54 69 63 6b 65 74 20 23 32 zeros. Ticket #2
b82d0 34 35 34 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 454 */. for(i=0
b82e0 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 ; (c=zNum[i])>='
b82f0 30 27 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 0' && c<='9'; i+
b8300 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 +){. v = v*10
b8310 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 7d 0a + c - '0';. }.
b8320 20 20 2a 70 4e 75 6d 20 3d 20 6e 65 67 20 3f 20 *pNum = neg ?
b8330 2d 76 20 3a 20 76 3b 0a 20 20 69 66 28 20 63 21 -v : v;. if( c!
b8340 3d 30 20 7c 7c 20 28 69 3d 3d 30 20 26 26 20 7a =0 || (i==0 && z
b8350 53 74 61 72 74 3d 3d 7a 4e 75 6d 29 20 7c 7c 20 Start==zNum) ||
b8360 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 7a i>19 ){. /* z
b8370 4e 75 6d 20 69 73 20 65 6d 70 74 79 20 6f 72 20 Num is empty or
b8380 63 6f 6e 74 61 69 6e 73 20 6e 6f 6e 2d 6e 75 6d contains non-num
b8390 65 72 69 63 20 74 65 78 74 20 6f 72 20 69 73 20 eric text or is
b83a0 6c 6f 6e 67 65 72 0a 20 20 20 20 2a 2a 20 74 68 longer. ** th
b83b0 61 6e 20 31 39 20 64 69 67 69 74 73 20 28 74 68 an 19 digits (th
b83c0 75 73 20 67 75 61 72 61 6e 74 69 6e 67 20 74 68 us guaranting th
b83d0 61 74 20 69 74 20 69 73 20 74 6f 6f 20 6c 61 72 at it is too lar
b83e0 67 65 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 ge) */. retur
b83f0 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 n 0;. }else if(
b8400 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 i<19 ){. /*
b8410 4c 65 73 73 20 74 68 61 6e 20 31 39 20 64 69 67 Less than 19 dig
b8420 69 74 73 2c 20 73 6f 20 77 65 20 6b 6e 6f 77 20 its, so we know
b8430 74 68 61 74 20 69 74 20 66 69 74 73 20 69 6e 20 that it fits in
b8440 36 34 20 62 69 74 73 20 2a 2f 0a 20 20 20 20 72 64 bits */. r
b8450 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 eturn 1;. }else
b8460 7b 0a 20 20 20 20 2f 2a 20 31 39 2d 64 69 67 69 {. /* 19-digi
b8470 74 20 6e 75 6d 62 65 72 73 20 6d 75 73 74 20 62 t numbers must b
b8480 65 20 6e 6f 20 6c 61 72 67 65 72 20 74 68 61 6e e no larger than
b8490 20 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 922337203685477
b84a0 35 38 30 37 20 69 66 20 70 6f 73 69 74 69 76 65 5807 if positive
b84b0 0a 20 20 20 20 2a 2a 20 6f 72 20 39 32 32 33 33 . ** or 92233
b84c0 37 32 30 33 36 38 35 34 37 37 35 38 30 38 20 69 72036854775808 i
b84d0 66 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 f negative. Not
b84e0 65 20 74 68 61 74 20 39 32 32 33 33 37 32 30 33 e that 922337203
b84f0 36 38 35 34 36 36 35 38 30 38 0a 20 20 20 20 2a 6854665808. *
b8500 2a 20 69 73 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 * is 2^63. */.
b8510 20 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65 return compare
b8520 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 2pow63(zNum)<neg
b8530 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ;. }.}../*.** T
b8540 68 65 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 72 he string zNum r
b8550 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e 74 epresents an int
b8560 65 67 65 72 2e 20 20 54 68 65 72 65 20 6d 69 67 eger. There mig
b8570 68 74 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72 ht be some other
b8580 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
b8590 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 69 6e following the in
b85a0 74 65 67 65 72 20 74 6f 6f 2c 20 62 75 74 20 74 teger too, but t
b85b0 68 61 74 20 70 61 72 74 20 69 73 20 69 67 6e 6f hat part is igno
b85c0 72 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 red..** If the i
b85d0 6e 74 65 67 65 72 20 74 68 61 74 20 74 68 65 20 nteger that the
b85e0 70 72 65 66 69 78 20 6f 66 20 7a 4e 75 6d 20 72 prefix of zNum r
b85f0 65 70 72 65 73 65 6e 74 73 20 77 69 6c 6c 20 66 epresents will f
b8600 69 74 20 69 6e 20 61 0a 2a 2a 20 36 34 2d 62 69 it in a.** 64-bi
b8610 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
b8620 2c 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 , return TRUE.
b8630 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e Otherwise return
b8640 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 FALSE..**.** Th
b8650 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 is routine retur
b8660 6e 73 20 46 41 4c 53 45 20 66 6f 72 20 74 68 65 ns FALSE for the
b8670 20 73 74 72 69 6e 67 20 2d 39 32 32 33 33 37 32 string -9223372
b8680 30 33 36 38 35 34 37 37 35 38 30 38 20 65 76 65 036854775808 eve
b8690 6e 20 74 68 61 74 0a 2a 2a 20 74 68 61 74 20 6e n that.** that n
b86a0 75 6d 62 65 72 20 77 69 6c 6c 2c 20 69 6e 20 74 umber will, in t
b86b0 68 65 6f 72 79 20 66 69 74 20 69 6e 20 61 20 36 heory fit in a 6
b86c0 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 20 20 4-bit integer.
b86d0 50 6f 73 69 74 69 76 65 0a 2a 2a 20 39 32 32 33 Positive.** 9223
b86e0 33 37 33 30 33 36 38 35 34 37 37 35 38 30 38 20 373036854775808
b86f0 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 20 will not fit in
b8700 36 34 20 62 69 74 73 2e 20 20 53 6f 20 69 74 20 64 bits. So it
b8710 73 65 65 6d 73 20 73 61 66 65 72 20 74 6f 20 72 seems safer to r
b8720 65 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 2e 0a eturn.** false..
b8730 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b8740 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74 E int sqlite3Fit
b8750 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20 sIn64Bits(const
b8760 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 char *zNum, int
b8770 6e 65 67 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 negFlag){. int
b8780 69 2c 20 63 3b 0a 20 20 69 6e 74 20 6e 65 67 20 i, c;. int neg
b8790 3d 20 30 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d = 0;. if( *zNum
b87a0 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 =='-' ){. neg
b87b0 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b = 1;. zNum++
b87c0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a ;. }else if( *z
b87d0 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 Num=='+' ){.
b87e0 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 zNum++;. }. if
b87f0 28 20 6e 65 67 46 6c 61 67 20 29 20 6e 65 67 20 ( negFlag ) neg
b8800 3d 20 31 2d 6e 65 67 3b 0a 20 20 77 68 69 6c 65 = 1-neg;. while
b8810 28 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29 7b 0a ( *zNum=='0' ){.
b8820 20 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20 2f 2a zNum++; /*
b8830 20 53 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 Skip leading ze
b8840 72 6f 73 2e 20 20 54 69 63 6b 65 74 20 23 32 34 ros. Ticket #24
b8850 35 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 54 */. }. for(
b8860 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 i=0; (c=zNum[i])
b8870 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 3b >='0' && c<='9';
b8880 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 3c i++){}. if( i<
b8890 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 19 ){. /* Gua
b88a0 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 69 ranteed to fit i
b88b0 66 20 6c 65 73 73 20 74 68 61 6e 20 31 39 20 64 f less than 19 d
b88c0 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 igits */. ret
b88d0 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 urn 1;. }else i
b88e0 66 28 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f f( i>19 ){. /
b88f0 2a 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 * Guaranteed to
b8900 62 65 20 74 6f 6f 20 62 69 67 20 69 66 20 67 72 be too big if gr
b8910 65 61 74 65 72 20 74 68 61 6e 20 31 39 20 64 69 eater than 19 di
b8920 67 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 gits */. retu
b8930 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 rn 0;. }else{.
b8940 20 20 20 2f 2a 20 43 6f 6d 70 61 72 65 20 61 67 /* Compare ag
b8950 61 69 6e 73 74 20 32 5e 36 33 2e 20 2a 2f 0a 20 ainst 2^63. */.
b8960 20 20 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 return compar
b8970 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 e2pow63(zNum)<ne
b8980 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 g;. }.}../*.**
b8990 49 66 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e If zNum represen
b89a0 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 ts an integer th
b89b0 61 74 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 33 at will fit in 3
b89c0 32 2d 62 69 74 73 2c 20 74 68 65 6e 20 73 65 74 2-bits, then set
b89d0 0a 2a 2a 20 2a 70 56 61 6c 75 65 20 74 6f 20 74 .** *pValue to t
b89e0 68 61 74 20 69 6e 74 65 67 65 72 20 61 6e 64 20 hat integer and
b89f0 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20 4f 74 return true. Ot
b8a00 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 66 herwise return f
b8a10 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 alse..**.** Any
b8a20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 63 68 61 72 non-numeric char
b8a30 61 63 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c acters that foll
b8a40 6f 77 69 6e 67 20 7a 4e 75 6d 20 61 72 65 20 69 owing zNum are i
b8a50 67 6e 6f 72 65 64 2e 0a 2a 2a 20 54 68 69 73 20 gnored..** This
b8a60 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f is different fro
b8a70 6d 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 m sqlite3Atoi64(
b8a80 29 20 77 68 69 63 68 20 72 65 71 75 69 72 65 73 ) which requires
b8a90 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 6e 75 the.** input nu
b8aa0 6d 62 65 72 20 74 6f 20 62 65 20 7a 65 72 6f 2d mber to be zero-
b8ab0 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 terminated..*/.S
b8ac0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b8ad0 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 t sqlite3GetInt3
b8ae0 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 2(const char *zN
b8af0 75 6d 2c 20 69 6e 74 20 2a 70 56 61 6c 75 65 29 um, int *pValue)
b8b00 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 {. sqlite_int64
b8b10 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c v = 0;. int i,
b8b20 20 63 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 c;. int neg =
b8b30 30 3b 0a 20 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 0;. if( zNum[0]
b8b40 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 =='-' ){. neg
b8b50 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b = 1;. zNum++
b8b60 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 4e ;. }else if( zN
b8b70 75 6d 5b 30 5d 3d 3d 27 2b 27 20 29 7b 0a 20 20 um[0]=='+' ){.
b8b80 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 zNum++;. }.
b8b90 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d while( zNum[0]==
b8ba0 27 30 27 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 '0' ) zNum++;.
b8bb0 66 6f 72 28 69 3d 30 3b 20 69 3c 31 31 20 26 26 for(i=0; i<11 &&
b8bc0 20 28 63 20 3d 20 7a 4e 75 6d 5b 69 5d 20 2d 20 (c = zNum[i] -
b8bd0 27 30 27 29 3e 3d 30 20 26 26 20 63 3c 3d 39 3b '0')>=0 && c<=9;
b8be0 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 i++){. v = v
b8bf0 2a 31 30 20 2b 20 63 3b 0a 20 20 7d 0a 0a 20 20 *10 + c;. }..
b8c00 2f 2a 20 54 68 65 20 6c 6f 6e 67 65 73 74 20 64 /* The longest d
b8c10 65 63 69 6d 61 6c 20 72 65 70 72 65 73 65 6e 74 ecimal represent
b8c20 61 74 69 6f 6e 20 6f 66 20 61 20 33 32 20 62 69 ation of a 32 bi
b8c30 74 20 69 6e 74 65 67 65 72 20 69 73 20 31 30 20 t integer is 10
b8c40 64 69 67 69 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a digits:. **. *
b8c50 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 * 12
b8c60 33 34 35 36 37 38 39 30 0a 20 20 2a 2a 20 20 20 34567890. **
b8c70 20 20 32 5e 33 31 20 2d 3e 20 32 31 34 37 34 38 2^31 -> 214748
b8c80 33 36 34 38 0a 20 20 2a 2f 0a 20 20 69 66 28 20 3648. */. if(
b8c90 69 3e 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 i>10 ){. retu
b8ca0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 0;. }. if(
b8cb0 76 2d 6e 65 67 3e 32 31 34 37 34 38 33 36 34 37 v-neg>2147483647
b8cc0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
b8cd0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 20 ;. }. if( neg
b8ce0 29 7b 0a 20 20 20 20 76 20 3d 20 2d 76 3b 0a 20 ){. v = -v;.
b8cf0 20 7d 0a 20 20 2a 70 56 61 6c 75 65 20 3d 20 28 }. *pValue = (
b8d00 69 6e 74 29 76 3b 0a 20 20 72 65 74 75 72 6e 20 int)v;. return
b8d10 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 1;.}../*.** The
b8d20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 variable-length
b8d30 69 6e 74 65 67 65 72 20 65 6e 63 6f 64 69 6e 67 integer encoding
b8d40 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a is as follows:.
b8d50 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 **.** KEY:.**
b8d60 20 20 20 20 20 20 41 20 3d 20 30 78 78 78 78 78 A = 0xxxxx
b8d70 78 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 xx 7 bits of
b8d80 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 data and one fla
b8d90 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 g bit.**
b8da0 20 42 20 3d 20 31 78 78 78 78 78 78 78 20 20 20 B = 1xxxxxxx
b8db0 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 7 bits of data
b8dc0 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 and one flag bit
b8dd0 0a 2a 2a 20 20 20 20 20 20 20 20 20 43 20 3d 20 .** C =
b8de0 78 78 78 78 78 78 78 78 20 20 20 20 38 20 62 69 xxxxxxxx 8 bi
b8df0 74 73 20 6f 66 20 64 61 74 61 0a 2a 2a 0a 2a 2a ts of data.**.**
b8e00 20 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 7 bits - A.**
b8e10 31 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 14 bits - BA.**
b8e20 32 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 21 bits - BBA.**
b8e30 20 32 38 20 62 69 74 73 20 2d 20 42 42 42 41 0a 28 bits - BBBA.
b8e40 2a 2a 20 33 35 20 62 69 74 73 20 2d 20 42 42 42 ** 35 bits - BBB
b8e50 42 41 0a 2a 2a 20 34 32 20 62 69 74 73 20 2d 20 BA.** 42 bits -
b8e60 42 42 42 42 42 41 0a 2a 2a 20 34 39 20 62 69 74 BBBBBA.** 49 bit
b8e70 73 20 2d 20 42 42 42 42 42 42 41 0a 2a 2a 20 35 s - BBBBBBA.** 5
b8e80 36 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 6 bits - BBBBBBB
b8e90 41 0a 2a 2a 20 36 34 20 62 69 74 73 20 2d 20 42 A.** 64 bits - B
b8ea0 42 42 42 42 42 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a BBBBBBBC.*/../*.
b8eb0 2a 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62 69 ** Write a 64-bi
b8ec0 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 t variable-lengt
b8ed0 68 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d h integer to mem
b8ee0 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 ory starting at
b8ef0 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e p[0]..** The len
b8f00 67 74 68 20 6f 66 20 64 61 74 61 20 77 72 69 74 gth of data writ
b8f10 65 20 77 69 6c 6c 20 62 65 20 62 65 74 77 65 65 e will be betwee
b8f20 6e 20 31 20 61 6e 64 20 39 20 62 79 74 65 73 2e n 1 and 9 bytes.
b8f30 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 The number.**
b8f40 6f 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e of bytes written
b8f50 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
b8f60 0a 2a 2a 20 41 20 76 61 72 69 61 62 6c 65 2d 6c .** A variable-l
b8f70 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 63 6f ength integer co
b8f80 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 6c 6f nsists of the lo
b8f90 77 65 72 20 37 20 62 69 74 73 20 6f 66 20 65 61 wer 7 bits of ea
b8fa0 63 68 20 62 79 74 65 0a 2a 2a 20 66 6f 72 20 61 ch byte.** for a
b8fb0 6c 6c 20 62 79 74 65 73 20 74 68 61 74 20 68 61 ll bytes that ha
b8fc0 76 65 20 74 68 65 20 38 74 68 20 62 69 74 20 73 ve the 8th bit s
b8fd0 65 74 20 61 6e 64 20 6f 6e 65 20 62 79 74 65 20 et and one byte
b8fe0 77 69 74 68 20 74 68 65 20 38 74 68 0a 2a 2a 20 with the 8th.**
b8ff0 62 69 74 20 63 6c 65 61 72 2e 20 20 45 78 63 65 bit clear. Exce
b9000 70 74 2c 20 69 66 20 77 65 20 67 65 74 20 74 6f pt, if we get to
b9010 20 74 68 65 20 39 74 68 20 62 79 74 65 2c 20 69 the 9th byte, i
b9020 74 20 73 74 6f 72 65 73 20 74 68 65 20 66 75 6c t stores the ful
b9030 6c 0a 2a 2a 20 38 20 62 69 74 73 20 61 6e 64 20 l.** 8 bits and
b9040 69 73 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 is the last byte
b9050 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b9060 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
b9070 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 utVarint(unsigne
b9080 64 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20 76 d char *p, u64 v
b9090 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e ){. int i, j, n
b90a0 3b 0a 20 20 75 38 20 62 75 66 5b 31 30 5d 3b 0a ;. u8 buf[10];.
b90b0 20 20 69 66 28 20 76 20 26 20 28 28 28 75 36 34 if( v & (((u64
b90c0 29 30 78 66 66 30 30 30 30 30 30 29 3c 3c 33 32 )0xff000000)<<32
b90d0 29 20 29 7b 0a 20 20 20 20 70 5b 38 5d 20 3d 20 ) ){. p[8] =
b90e0 28 75 38 29 76 3b 0a 20 20 20 20 76 20 3e 3e 3d (u8)v;. v >>=
b90f0 20 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d 37 3b 8;. for(i=7;
b9100 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 i>=0; i--){.
b9110 20 20 20 70 5b 69 5d 20 3d 20 28 75 38 29 28 28 p[i] = (u8)((
b9120 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 v & 0x7f) | 0x80
b9130 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 37 );. v >>= 7
b9140 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
b9150 72 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 20 rn 9;. } .
b9160 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 n = 0;. do{.
b9170 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75 38 29 buf[n++] = (u8)
b9180 28 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 ((v & 0x7f) | 0x
b9190 38 30 29 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 80);. v >>= 7
b91a0 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 ;. }while( v!=0
b91b0 20 29 3b 0a 20 20 62 75 66 5b 30 5d 20 26 3d 20 );. buf[0] &=
b91c0 30 78 37 66 3b 0a 20 20 61 73 73 65 72 74 28 20 0x7f;. assert(
b91d0 6e 3c 3d 39 20 29 3b 0a 20 20 66 6f 72 28 69 3d n<=9 );. for(i=
b91e0 30 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 0, j=n-1; j>=0;
b91f0 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 70 j--, i++){. p
b9200 5b 69 5d 20 3d 20 62 75 66 5b 6a 5d 3b 0a 20 20 [i] = buf[j];.
b9210 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a }. return n;.}.
b9220 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
b9230 69 6e 65 20 69 73 20 61 20 66 61 73 74 65 72 20 ine is a faster
b9240 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 version of sqlit
b9250 65 33 50 75 74 56 61 72 69 6e 74 28 29 20 74 68 e3PutVarint() th
b9260 61 74 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 at only.** works
b9270 20 66 6f 72 20 33 32 2d 62 69 74 20 70 6f 73 69 for 32-bit posi
b9280 74 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e tive integers an
b9290 64 20 77 68 69 63 68 20 69 73 20 6f 70 74 69 6d d which is optim
b92a0 69 7a 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 ized for.** the
b92b0 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 20 73 common case of s
b92c0 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 20 20 mall integers.
b92d0 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c A MACRO version,
b92e0 20 70 75 74 56 61 72 69 6e 74 33 32 2c 0a 2a 2a putVarint32,.**
b92f0 20 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 is provided whi
b9300 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 73 ch inlines the s
b9310 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e ingle-byte case.
b9320 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c All code shoul
b9330 64 20 75 73 65 0a 2a 2a 20 74 68 65 20 4d 41 43 d use.** the MAC
b9340 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 74 68 RO version as th
b9350 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 is function assu
b9360 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 mes the single-b
b9370 79 74 65 20 63 61 73 65 20 68 61 73 0a 2a 2a 20 yte case has.**
b9380 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e already been han
b9390 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f dled..*/.SQLITE_
b93a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b93b0 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 75 te3PutVarint32(u
b93c0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c nsigned char *p,
b93d0 20 75 33 32 20 76 29 7b 0a 23 69 66 6e 64 65 66 u32 v){.#ifndef
b93e0 20 70 75 74 56 61 72 69 6e 74 33 32 0a 20 20 69 putVarint32. i
b93f0 66 28 20 28 76 20 26 20 7e 30 78 37 66 29 3d 3d f( (v & ~0x7f)==
b9400 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 0 ){. p[0] =
b9410 76 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b v;. return 1;
b9420 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 . }.#endif. if
b9430 28 20 28 76 20 26 20 7e 30 78 33 66 66 66 29 3d ( (v & ~0x3fff)=
b9440 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d =0 ){. p[0] =
b9450 20 28 75 38 29 28 28 76 3e 3e 37 29 20 7c 20 30 (u8)((v>>7) | 0
b9460 78 38 30 29 3b 0a 20 20 20 20 70 5b 31 5d 20 3d x80);. p[1] =
b9470 20 28 75 38 29 28 76 20 26 20 30 78 37 66 29 3b (u8)(v & 0x7f);
b9480 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 . return 2;.
b9490 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 }. return sqli
b94a0 74 65 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 te3PutVarint(p,
b94b0 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 v);.}../*.** Rea
b94c0 64 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 d a 64-bit varia
b94d0 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble-length integ
b94e0 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 er from memory s
b94f0 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e tarting at p[0].
b9500 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
b9510 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 umber of bytes r
b9520 65 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 ead. The value
b9530 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e is stored in *v.
b9540 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b9550 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 TE u8 sqlite3Get
b9560 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 Varint(const uns
b9570 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 igned char *p, u
b9580 36 34 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 64 *v){. u32 a,
b9590 62 2c 73 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a b,s;.. a = *p;.
b95a0 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 /* a: p0 (unma
b95b0 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b95c0 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (a&0x80)). {.
b95d0 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 *v = a;. re
b95e0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 70 turn 1;. }.. p
b95f0 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 ++;. b = *p;.
b9600 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b /* b: p1 (unmask
b9610 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 ed) */. if (!(b
b9620 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 &0x80)). {.
b9630 61 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 a &= 0x7f;. a
b9640 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = a<<7;. a |
b9650 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b = b;. *v = a;
b9660 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 . return 2;.
b9670 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d }.. p++;. a =
b9680 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a a<<14;. a |= *
b9690 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 p;. /* a: p0<<1
b96a0 34 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 4 | p2 (unmasked
b96b0 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 ) */. if (!(a&0
b96c0 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 x80)). {. a
b96d0 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 &= (0x7f<<14)|(0
b96e0 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 x7f);. b &= 0
b96f0 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c x7f;. b = b<<
b9700 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 7;. a |= b;.
b9710 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 *v = a;. r
b9720 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 eturn 3;. }..
b9730 2f 2a 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c /* CSE1 from bel
b9740 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 ow */. a &= (0x
b9750 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b9760 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c p++;. b = b<<
b9770 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 14;. b |= *p;.
b9780 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 /* b: p1<<14 |
b9790 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f p3 (unmasked) */
b97a0 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 . if (!(b&0x80)
b97b0 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 ). {. b &= (
b97c0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b97d0 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 ;. /* moved C
b97e0 53 45 31 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a SE1 up */. /*
b97f0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
b9800 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 |(0x7f); */.
b9810 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 a = a<<7;. a
b9820 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 |= b;. *v = a
b9830 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a ;. return 4;.
b9840 20 20 7d 0a 0a 20 20 2f 2a 20 61 3a 20 70 30 3c }.. /* a: p0<
b9850 3c 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 <14 | p2 (masked
b9860 29 20 2a 2f 0a 20 20 2f 2a 20 62 3a 20 70 31 3c ) */. /* b: p1<
b9870 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b <14 | p3 (unmask
b9880 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 31 3a 73 61 ed) */. /* 1:sa
b9890 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 ve off p0<<21 |
b98a0 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c p1<<14 | p2<<7 |
b98b0 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a p3 (masked) */.
b98c0 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 /* moved CSE1
b98d0 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 up */. /* a &=
b98e0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b98f0 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 ); */. b &= (0x
b9900 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b9910 20 20 73 20 3d 20 61 3b 0a 20 20 2f 2a 20 73 3a s = a;. /* s:
b9920 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 p0<<14 | p2 (ma
b9930 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b sked) */.. p++;
b9940 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 . a = a<<14;.
b9950 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a a |= *p;. /* a:
b9960 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 p0<<28 | p2<<14
b9970 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 | p4 (unmasked)
b9980 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 */. if (!(a&0x
b9990 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 80)). {. /*
b99a0 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65 73 we can skip thes
b99b0 65 20 63 61 75 73 65 20 74 68 65 79 20 77 65 72 e cause they wer
b99c0 65 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 e (effectively)
b99d0 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 done above in ca
b99e0 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 lc'ing s */.
b99f0 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 /* a &= (0x7f<<2
b9a00 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 8)|(0x7f<<14)|(0
b9a10 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 2f 2a 20 x7f); */. /*
b9a20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c b &= (0x7f<<14)|
b9a30 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 62 (0x7f); */. b
b9a40 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = b<<7;. a |
b9a50 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e = b;. s = s>>
b9a60 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 18;. *v = ((u
b9a70 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 64)s)<<32 | a;.
b9a80 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d return 5;. }
b9a90 0a 0a 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66 .. /* 2:save of
b9aa0 66 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 f p0<<21 | p1<<1
b9ab0 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 4 | p2<<7 | p3 (
b9ac0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 73 20 3d masked) */. s =
b9ad0 20 73 3c 3c 37 3b 0a 20 20 73 20 7c 3d 20 62 3b s<<7;. s |= b;
b9ae0 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 32 31 20 . /* s: p0<<21
b9af0 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 | p1<<14 | p2<<7
b9b00 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a | p3 (masked) *
b9b10 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 /.. p++;. b =
b9b20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 b<<14;. b |= *p
b9b30 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 32 38 ;. /* b: p1<<28
b9b40 20 7c 20 70 33 3c 3c 31 34 20 7c 20 70 35 20 28 | p3<<14 | p5 (
b9b50 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
b9b60 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 f (!(b&0x80)).
b9b70 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 {. /* we can
b9b80 73 6b 69 70 20 74 68 69 73 20 63 61 75 73 65 20 skip this cause
b9b90 69 74 20 77 61 73 20 28 65 66 66 65 63 74 69 76 it was (effectiv
b9ba0 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 ely) done above
b9bb0 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f in calc'ing s */
b9bc0 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 . /* b &= (0x
b9bd0 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 7f<<28)|(0x7f<<1
b9be0 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 4)|(0x7f); */.
b9bf0 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 a &= (0x7f<<14
b9c00 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 )|(0x7f);. a
b9c10 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d = a<<7;. a |=
b9c20 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 b;. s = s>>1
b9c30 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 8;. *v = ((u6
b9c40 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 4)s)<<32 | a;.
b9c50 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a return 6;. }.
b9c60 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c . p++;. a = a<
b9c70 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a <14;. a |= *p;.
b9c80 20 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c /* a: p2<<28 |
b9c90 20 70 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e p4<<14 | p6 (un
b9ca0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 masked) */. if
b9cb0 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a (!(a&0x80)). {.
b9cc0 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c a &= (0x7f<<
b9cd0 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 28)|(0x7f<<14)|(
b9ce0 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 0x7f);. b &=
b9cf0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b9d00 29 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b );. b = b<<7;
b9d10 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 . a |= b;.
b9d20 20 73 20 3d 20 73 3e 3e 31 31 3b 0a 20 20 20 20 s = s>>11;.
b9d30 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 *v = ((u64)s)<<3
b9d40 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 2 | a;. retur
b9d50 6e 20 37 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 n 7;. }.. /* C
b9d60 53 45 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a SE2 from below *
b9d70 2f 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c /. a &= (0x7f<<
b9d80 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 14)|(0x7f);. p+
b9d90 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a +;. b = b<<14;.
b9da0 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 b |= *p;. /*
b9db0 62 3a 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c b: p3<<28 | p5<<
b9dc0 31 34 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65 14 | p7 (unmaske
b9dd0 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 d) */. if (!(b&
b9de0 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 0x80)). {. b
b9df0 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 &= (0x7f<<28)|(
b9e00 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b9e10 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 ;. /* moved C
b9e20 53 45 32 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a SE2 up */. /*
b9e30 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
b9e40 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 |(0x7f); */.
b9e50 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 a = a<<7;. a
b9e60 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e |= b;. s = s>
b9e70 3e 34 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 >4;. *v = ((u
b9e80 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 64)s)<<32 | a;.
b9e90 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 7d return 8;. }
b9ea0 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 .. p++;. a = a
b9eb0 3c 3c 31 35 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b <<15;. a |= *p;
b9ec0 0a 20 20 2f 2a 20 61 3a 20 70 34 3c 3c 32 39 20 . /* a: p4<<29
b9ed0 7c 20 70 36 3c 3c 31 35 20 7c 20 70 38 20 28 75 | p6<<15 | p8 (u
b9ee0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f nmasked) */.. /
b9ef0 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 * moved CSE2 up
b9f00 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 */. /* a &= (0x
b9f10 37 66 3c 3c 32 39 29 7c 28 30 78 37 66 3c 3c 31 7f<<29)|(0x7f<<1
b9f20 35 29 7c 28 30 78 66 66 29 3b 20 2a 2f 0a 20 20 5)|(0xff); */.
b9f30 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c b &= (0x7f<<14)|
b9f40 28 30 78 37 66 29 3b 0a 20 20 62 20 3d 20 62 3c (0x7f);. b = b<
b9f50 3c 38 3b 0a 20 20 61 20 7c 3d 20 62 3b 0a 0a 20 <8;. a |= b;..
b9f60 20 73 20 3d 20 73 3c 3c 34 3b 0a 20 20 62 20 3d s = s<<4;. b =
b9f70 20 70 5b 2d 34 5d 3b 0a 20 20 62 20 26 3d 20 30 p[-4];. b &= 0
b9f80 78 37 66 3b 0a 20 20 62 20 3d 20 62 3e 3e 33 3b x7f;. b = b>>3;
b9f90 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a 20 20 2a 76 . s |= b;.. *v
b9fa0 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 = ((u64)s)<<32
b9fb0 7c 20 61 3b 0a 0a 20 20 72 65 74 75 72 6e 20 39 | a;.. return 9
b9fc0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
b9fd0 61 20 33 32 2d 62 69 74 20 76 61 72 69 61 62 6c a 32-bit variabl
b9fe0 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
b9ff0 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 from memory sta
ba000 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a rting at p[0]..*
ba010 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
ba020 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 ber of bytes rea
ba030 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 d. The value is
ba040 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a stored in *v..*
ba050 2a 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f * A MACRO versio
ba060 6e 2c 20 67 65 74 56 61 72 69 6e 74 33 32 2c 20 n, getVarint32,
ba070 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63 is provided whic
ba080 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 0a 2a h inlines the .*
ba090 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 * single-byte ca
ba0a0 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 se. All code sh
ba0b0 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 ould use the MAC
ba0c0 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 0a 2a RO version as .*
ba0d0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
ba0e0 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 assumes the sing
ba0f0 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 le-byte case has
ba100 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 already been ha
ba110 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ndled..*/.SQLITE
ba120 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 _PRIVATE u8 sqli
ba130 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 te3GetVarint32(c
ba140 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
ba150 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29 7b 0a ar *p, u32 *v){.
ba160 20 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20 61 20 u32 a,b;.. a
ba170 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 = *p;. /* a: p0
ba180 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 23 (unmasked) */.#
ba190 69 66 6e 64 65 66 20 67 65 74 56 61 72 69 6e 74 ifndef getVarint
ba1a0 33 32 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 32. if (!(a&0x8
ba1b0 30 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 0)). {. *v =
ba1c0 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 a;. return 1
ba1d0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
ba1e0 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 p++;. b = *p;.
ba1f0 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 /* b: p1 (unmas
ba200 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
ba210 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 b&0x80)). {.
ba220 20 61 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 a &= 0x7f;.
ba230 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 a = a<<7;. *v
ba240 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 = a | b;. re
ba250 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 turn 2;. }.. p
ba260 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b ++;. a = a<<14;
ba270 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . a |= *p;. /*
ba280 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 a: p0<<14 | p2
ba290 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
ba2a0 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 if (!(a&0x80)).
ba2b0 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 {. a &= (0x7
ba2c0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
ba2d0 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 b &= 0x7f;.
ba2e0 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 b = b<<7;.
ba2f0 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 *v = a | b;.
ba300 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 return 3;. }..
ba310 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 p++;. b = b<<1
ba320 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 4;. b |= *p;.
ba330 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 /* b: p1<<14 | p
ba340 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 3 (unmasked) */.
ba350 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 if (!(b&0x80))
ba360 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 . {. b &= (0
ba370 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
ba380 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c . a &= (0x7f<
ba390 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 <14)|(0x7f);.
ba3a0 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a a = a<<7;. *
ba3b0 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 v = a | b;. r
ba3c0 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 eturn 4;. }..
ba3d0 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 p++;. a = a<<14
ba3e0 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f ;. a |= *p;. /
ba3f0 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 * a: p0<<28 | p2
ba400 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 <<14 | p4 (unmas
ba410 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
ba420 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 a&0x80)). {.
ba430 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 a &= (0x7f<<28)
ba440 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 |(0x7f<<14)|(0x7
ba450 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 f);. b &= (0x
ba460 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 7f<<28)|(0x7f<<1
ba470 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 4)|(0x7f);. b
ba480 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 = b<<7;. *v
ba490 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 = a | b;. ret
ba4a0 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 5;. }.. /*
ba4b0 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61 We can only rea
ba4c0 63 68 20 74 68 69 73 20 70 6f 69 6e 74 20 77 68 ch this point wh
ba4d0 65 6e 20 72 65 61 64 69 6e 67 20 61 20 63 6f 72 en reading a cor
ba4e0 72 75 70 74 20 64 61 74 61 62 61 73 65 0a 20 20 rupt database.
ba4f0 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74 68 61 ** file. In tha
ba500 74 20 63 61 73 65 20 77 65 20 61 72 65 20 6e 6f t case we are no
ba510 74 20 69 6e 20 61 6e 79 20 68 75 72 72 79 2e 20 t in any hurry.
ba520 20 55 73 65 20 74 68 65 20 28 72 65 6c 61 74 69 Use the (relati
ba530 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 vely. ** slow)
ba540 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 general-purpose
ba550 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 sqlite3GetVarint
ba560 28 29 20 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 () routine to ex
ba570 74 72 61 63 74 20 74 68 65 0a 20 20 2a 2a 20 76 tract the. ** v
ba580 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 alue. */. {.
ba590 20 75 36 34 20 76 36 34 3b 0a 20 20 20 20 75 38 u64 v64;. u8
ba5a0 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 34 3b n;.. p -= 4;
ba5b0 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 . n = sqlite3
ba5c0 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26 76 36 GetVarint(p, &v6
ba5d0 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 4);. assert(
ba5e0 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 n>5 && n<=9 );.
ba5f0 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34 *v = (u32)v64
ba600 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a ;. return n;.
ba610 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
ba620 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
ba630 66 20 62 79 74 65 73 20 74 68 61 74 20 77 69 6c f bytes that wil
ba640 6c 20 62 65 20 6e 65 65 64 65 64 20 74 6f 20 73 l be needed to s
ba650 74 6f 72 65 20 74 68 65 20 67 69 76 65 6e 0a 2a tore the given.*
ba660 2a 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 * 64-bit integer
ba670 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
ba680 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ba690 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b arintLen(u64 v){
ba6a0 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 . int i = 0;.
ba6b0 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 do{. i++;.
ba6c0 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 v >>= 7;. }whi
ba6d0 6c 65 28 20 76 21 3d 30 20 26 26 20 69 3c 39 20 le( v!=0 && i<9
ba6e0 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d );. return i;.}
ba6f0 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 .../*.** Read or
ba700 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d 62 79 write a four-by
ba710 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e te big-endian in
ba720 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a teger value..*/.
ba730 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 SQLITE_PRIVATE u
ba740 33 32 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 32 sqlite3Get4by
ba750 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70 29 7b te(const u8 *p){
ba760 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c . return (p[0]<
ba770 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 <24) | (p[1]<<16
ba780 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 ) | (p[2]<<8) |
ba790 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 p[3];.}.SQLITE_P
ba7a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
ba7b0 74 65 33 50 75 74 34 62 79 74 65 28 75 6e 73 69 te3Put4byte(unsi
ba7c0 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33 gned char *p, u3
ba7d0 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 2 v){. p[0] = (
ba7e0 75 38 29 28 76 3e 3e 32 34 29 3b 0a 20 20 70 5b u8)(v>>24);. p[
ba7f0 31 5d 20 3d 20 28 75 38 29 28 76 3e 3e 31 36 29 1] = (u8)(v>>16)
ba800 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 75 38 29 28 ;. p[2] = (u8)(
ba810 76 3e 3e 38 29 3b 0a 20 20 70 5b 33 5d 20 3d 20 v>>8);. p[3] =
ba820 28 75 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20 (u8)v;.}....#if
ba830 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
ba840 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 OMIT_BLOB_LITERA
ba850 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 L) || defined(SQ
ba860 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a LITE_HAS_CODEC).
ba870 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 /*.** Translate
ba880 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 a single byte of
ba890 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69 6e 74 Hex into an int
ba8a0 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 72 6f eger..** This ro
ba8b0 75 74 69 6e 65 6e 20 6f 6e 6c 79 20 77 6f 72 6b utinen only work
ba8c0 73 20 69 66 20 68 20 72 65 61 6c 6c 79 20 69 73 s if h really is
ba8d0 20 61 20 76 61 6c 69 64 20 68 65 78 61 64 65 63 a valid hexadec
ba8e0 69 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 imal.** characte
ba8f0 72 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46 r: 0..9a..fA..F
ba900 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 68 65 .*/.static u8 he
ba910 78 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a 20 xToInt(int h){.
ba920 20 61 73 73 65 72 74 28 20 28 68 3e 3d 27 30 27 assert( (h>='0'
ba930 20 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20 20 && h<='9') ||
ba940 28 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27 66 (h>='a' && h<='f
ba950 27 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20 26 ') || (h>='A' &
ba960 26 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69 66 & h<='F') );.#if
ba970 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 def SQLITE_ASCII
ba980 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e . h += 9*(1&(h>
ba990 3e 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 >6));.#endif.#if
ba9a0 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 def SQLITE_EBCDI
ba9b0 43 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28 C. h += 9*(1&~(
ba9c0 68 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a 20 h>>4));.#endif.
ba9d0 20 72 65 74 75 72 6e 20 28 75 38 29 28 68 20 26 return (u8)(h &
ba9e0 20 30 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 0xf);.}.#endif
ba9f0 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* !SQLITE_OMIT_
baa00 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 BLOB_LITERAL ||
baa10 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
baa20 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 */..#if !define
baa30 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c d(SQLITE_OMIT_BL
baa40 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 OB_LITERAL) || d
baa50 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 efined(SQLITE_HA
baa60 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 43 S_CODEC)./*.** C
baa70 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 6c 69 onvert a BLOB li
baa80 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66 6f 72 teral of the for
baa90 6d 20 22 78 27 68 68 68 68 68 68 27 22 20 69 6e m "x'hhhhhh'" in
baaa0 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a 2a 2a to its binary.**
baab0 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72 6e 20 value. Return
baac0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 a pointer to its
baad0 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e 20 20 binary value.
baae0 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 Space to hold th
baaf0 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c 75 e.** binary valu
bab00 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 e has been obtai
bab10 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 ned from malloc
bab20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 and must be free
bab30 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c d by.** the call
bab40 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a ing routine..*/.
bab50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
bab60 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54 oid *sqlite3HexT
bab70 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a 64 oBlob(sqlite3 *d
bab80 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
bab90 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 , int n){. char
baba0 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 69 *zBlob;. int i
babb0 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63 68 ;.. zBlob = (ch
babc0 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 ar *)sqlite3DbMa
babd0 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f 32 20 llocRaw(db, n/2
babe0 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 20 69 + 1);. n--;. i
babf0 66 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 20 20 f( zBlob ){.
bac00 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b for(i=0; i<n; i+
bac10 3d 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 =2){. zBlob
bac20 5b 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f 49 6e [i/2] = (hexToIn
bac30 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 68 65 t(z[i])<<4) | he
bac40 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 3b 0a xToInt(z[i+1]);.
bac50 20 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f 62 5b }. zBlob[
bac60 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 i/2] = 0;. }.
bac70 72 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a return zBlob;.}.
bac80 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 #endif /* !SQLIT
bac90 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 E_OMIT_BLOB_LITE
baca0 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 RAL || SQLITE_HA
bacb0 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a S_CODEC */.../*.
bacc0 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 71 ** Change the sq
bacd0 6c 69 74 65 2e 6d 61 67 69 63 20 66 72 6f 6d 20 lite.magic from
bace0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 SQLITE_MAGIC_OPE
bacf0 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 N to SQLITE_MAGI
bad00 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74 75 72 C_BUSY..** Retur
bad10 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d n an error (non-
bad20 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67 zero) if the mag
bad30 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54 ic was not SQLIT
bad40 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 E_MAGIC_OPEN.**
bad50 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
bad60 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a e is called..**.
bad70 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
bad80 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 65 is called when e
bad90 6e 74 65 72 69 6e 67 20 61 6e 20 53 51 4c 69 74 ntering an SQLit
bada0 65 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c 49 e API. The SQLI
badb0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a TE_MAGIC_OPEN.**
badc0 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 value indicates
badd0 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 that the databa
bade0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 se connection pa
badf0 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20 41 50 ssed into the AP
bae00 49 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 6e 64 I is.** open and
bae10 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 75 73 is not being us
bae20 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20 74 68 ed by another th
bae30 72 65 61 64 2e 20 20 42 79 20 63 68 61 6e 67 69 read. By changi
bae40 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 ng the value.**
bae50 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f to SQLITE_MAGIC_
bae60 42 55 53 59 20 77 65 20 69 6e 64 69 63 61 74 65 BUSY we indicate
bae70 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65 63 that the connec
bae80 74 69 6f 6e 20 69 73 20 69 6e 20 75 73 65 2e 0a tion is in use..
bae90 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 ** sqlite3Safety
baea0 4f 66 66 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c Off() below will
baeb0 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 change the valu
baec0 65 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 e back to SQLITE
baed0 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 _MAGIC_OPEN.** w
baee0 68 65 6e 20 74 68 65 20 41 50 49 20 65 78 69 74 hen the API exit
baef0 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 s. .**.** This r
baf00 6f 75 74 69 6e 65 20 69 73 20 61 20 61 74 74 65 outine is a atte
baf10 6d 70 74 20 74 6f 20 64 65 74 65 63 74 20 69 66 mpt to detect if
baf20 20 74 77 6f 20 74 68 72 65 61 64 73 20 75 73 65 two threads use
baf30 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73 71 6c the.** same sql
baf40 69 74 65 2a 20 70 6f 69 6e 74 65 72 20 61 74 20 ite* pointer at
baf50 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 the same time.
baf60 54 68 65 72 65 20 69 73 20 61 20 72 61 63 65 20 There is a race
baf70 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 73 6f .** condition so
baf80 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 it is possible
baf90 74 68 61 74 20 74 68 65 20 65 72 72 6f 72 20 69 that the error i
bafa0 73 20 6e 6f 74 20 64 65 74 65 63 74 65 64 2e 0a s not detected..
bafb0 2a 2a 20 42 75 74 20 75 73 75 61 6c 6c 79 20 74 ** But usually t
bafc0 68 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c 6c 20 he problem will
bafd0 62 65 20 73 65 65 6e 2e 20 20 54 68 65 20 72 65 be seen. The re
bafe0 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 6e 0a sult will be an.
baff0 2a 2a 20 65 72 72 6f 72 20 77 68 69 63 68 20 63 ** error which c
bb000 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64 65 an be used to de
bb010 62 75 67 20 74 68 65 20 61 70 70 6c 69 63 61 74 bug the applicat
bb020 69 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20 75 ion that is.** u
bb030 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 63 6f sing SQLite inco
bb040 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 rrectly..**.** T
bb050 69 63 6b 65 74 20 23 32 30 32 3a 20 20 49 66 20 icket #202: If
bb060 64 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e 6f 74 db->magic is not
bb070 20 61 20 76 61 6c 69 64 20 6f 70 65 6e 20 76 61 a valid open va
bb080 6c 75 65 2c 20 74 61 6b 65 20 63 61 72 65 20 6e lue, take care n
bb090 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 ot.** to modify
bb0a0 74 68 65 20 64 62 20 73 74 72 75 63 74 75 72 65 the db structure
bb0b0 20 61 74 20 61 6c 6c 2e 20 20 49 74 20 63 6f 75 at all. It cou
bb0c0 6c 64 20 62 65 20 74 68 61 74 20 64 62 20 69 73 ld be that db is
bb0d0 20 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f 69 6e a stale.** poin
bb0e0 74 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 ter. In other w
bb0f0 6f 72 64 73 2c 20 69 74 20 63 6f 75 6c 64 20 62 ords, it could b
bb100 65 20 74 68 61 74 20 74 68 65 72 65 20 68 61 73 e that there has
bb110 20 62 65 65 6e 20 61 20 70 72 69 6f 72 0a 2a 2a been a prior.**
bb120 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
bb130 5f 63 6c 6f 73 65 28 64 62 29 20 61 6e 64 20 64 _close(db) and d
bb140 62 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c b has been deall
bb150 6f 63 61 74 65 64 2e 20 20 41 6e 64 20 77 65 20 ocated. And we
bb160 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 do.** not want t
bb170 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 64 65 61 o write into dea
bb180 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e llocated memory.
bb190 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
bb1a0 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 E_DEBUG.SQLITE_P
bb1b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
bb1c0 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 e3SafetyOn(sqlit
bb1d0 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 e3 *db){. if( d
bb1e0 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 b->magic==SQLITE
bb1f0 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a 20 _MAGIC_OPEN ){.
bb200 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 db->magic = S
bb210 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 QLITE_MAGIC_BUSY
bb220 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 ;. assert( sq
bb230 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
bb240 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 (db->mutex) );.
bb250 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
bb260 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d 61 67 else if( db->mag
bb270 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 ic==SQLITE_MAGIC
bb280 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d _BUSY ){. db-
bb290 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f >magic = SQLITE_
bb2a0 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 MAGIC_ERROR;.
bb2b0 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 db->u1.isInterr
bb2c0 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 upted = 1;. }.
bb2d0 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e return 1;.}.#en
bb2e0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 dif../*.** Chang
bb2f0 65 20 74 68 65 20 6d 61 67 69 63 20 66 72 6f 6d e the magic from
bb300 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 SQLITE_MAGIC_BU
bb310 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 SY to SQLITE_MAG
bb320 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 75 IC_OPEN..** Retu
bb330 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e rn an error (non
bb340 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 -zero) if the ma
bb350 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 gic was not SQLI
bb360 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a 2a TE_MAGIC_BUSY.**
bb370 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
bb380 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f ne is called..*/
bb390 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
bb3a0 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
bb3b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 ATE int sqlite3S
bb3c0 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 afetyOff(sqlite3
bb3d0 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 2d *db){. if( db-
bb3e0 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d >magic==SQLITE_M
bb3f0 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 AGIC_BUSY ){.
bb400 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c db->magic = SQL
bb410 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a ITE_MAGIC_OPEN;.
bb420 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
bb430 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 te3_mutex_held(d
bb440 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 b->mutex) );.
bb450 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c return 0;. }el
bb460 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 se{. db->magi
bb470 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 c = SQLITE_MAGIC
bb480 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e _ERROR;. db->
bb490 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 u1.isInterrupted
bb4a0 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e = 1;. return
bb4b0 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 1;. }.}.#endif
bb4c0 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f ../*.** Check to
bb4d0 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 make sure we ha
bb4e0 76 65 20 61 20 76 61 6c 69 64 20 64 62 20 70 6f ve a valid db po
bb4f0 69 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 inter. This tes
bb500 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c t is not.** fool
bb510 70 72 6f 6f 66 20 62 75 74 20 69 74 20 64 6f 65 proof but it doe
bb520 73 20 70 72 6f 76 69 64 65 20 73 6f 6d 65 20 6d s provide some m
bb530 65 61 73 75 72 65 20 6f 66 20 70 72 6f 74 65 63 easure of protec
bb540 74 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20 tion against.**
bb550 6d 69 73 75 73 65 20 6f 66 20 74 68 65 20 69 6e misuse of the in
bb560 74 65 72 66 61 63 65 20 73 75 63 68 20 61 73 20 terface such as
bb570 70 61 73 73 69 6e 67 20 69 6e 20 64 62 20 70 6f passing in db po
bb580 69 6e 74 65 72 73 20 74 68 61 74 20 61 72 65 0a inters that are.
bb590 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 68 ** NULL or which
bb5a0 20 68 61 76 65 20 62 65 65 6e 20 70 72 65 76 69 have been previ
bb5b0 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 49 ously closed. I
bb5c0 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 f this routine r
bb5d0 65 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74 20 6d eturns.** 1 it m
bb5e0 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 62 eans that the db
bb5f0 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 pointer is vali
bb600 64 20 61 6e 64 20 30 20 69 66 20 69 74 20 73 68 d and 0 if it sh
bb610 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 64 ould not be.** d
bb620 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f 72 20 ereferenced for
bb630 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 68 65 any reason. The
bb640 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f calling functio
bb650 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 0a n should invoke.
bb660 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 ** SQLITE_MISUSE
bb670 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a immediately..**
bb680 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 .** sqlite3Safet
bb690 79 43 68 65 63 6b 4f 6b 28 29 20 72 65 71 75 69 yCheckOk() requi
bb6a0 72 65 73 20 74 68 61 74 20 74 68 65 20 64 62 20 res that the db
bb6b0 70 6f 69 6e 74 65 72 20 62 65 20 76 61 6c 69 64 pointer be valid
bb6c0 20 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 73 71 for.** use. sq
bb6d0 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b lite3SafetyCheck
bb6e0 53 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c 6f 77 SickOrOk() allow
bb6f0 73 20 61 20 64 62 20 70 6f 69 6e 74 65 72 20 74 s a db pointer t
bb700 68 61 74 20 66 61 69 6c 65 64 20 74 6f 0a 2a 2a hat failed to.**
bb710 20 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79 20 61 open properly a
bb720 6e 64 20 69 73 20 6e 6f 74 20 66 69 74 20 66 6f nd is not fit fo
bb730 72 20 67 65 6e 65 72 61 6c 20 75 73 65 20 62 75 r general use bu
bb740 74 20 77 68 69 63 68 20 63 61 6e 20 62 65 0a 2a t which can be.*
bb750 2a 20 75 73 65 64 20 61 73 20 61 6e 20 61 72 67 * used as an arg
bb760 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
bb770 5f 65 72 72 6d 73 67 28 29 20 6f 72 20 73 71 6c _errmsg() or sql
bb780 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a 2a 2f ite3_close()..*/
bb790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
bb7a0 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 int sqlite3Safet
bb7b0 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 yCheckOk(sqlite3
bb7c0 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 67 *db){. u32 mag
bb7d0 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20 ic;. if( db==0
bb7e0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 61 ) return 0;. ma
bb7f0 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b gic = db->magic;
bb800 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 . if( magic!=SQ
bb810 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 LITE_MAGIC_OPEN
bb820 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d &&. magic!=
bb830 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 SQLITE_MAGIC_BUS
bb840 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 Y ) return 0;.
bb850 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 53 51 4c 49 return 1;.}.SQLI
bb860 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
bb870 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 qlite3SafetyChec
bb880 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 74 65 kSickOrOk(sqlite
bb890 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 3 *db){. u32 ma
bb8a0 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 gic;. if( db==0
bb8b0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d ) return 0;. m
bb8c0 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 agic = db->magic
bb8d0 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 ;. if( magic!=S
bb8e0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b QLITE_MAGIC_SICK
bb8f0 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 &&. magic!
bb900 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 =SQLITE_MAGIC_OP
bb910 45 4e 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69 EN &&. magi
bb920 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f c!=SQLITE_MAGIC_
bb930 42 55 53 59 20 29 20 72 65 74 75 72 6e 20 30 3b BUSY ) return 0;
bb940 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a . return 1;.}..
bb950 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
bb960 45 6e 64 20 6f 66 20 75 74 69 6c 2e 63 20 2a 2a End of util.c **
bb970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
bb9a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
bb9b0 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e Begin file hash.
bb9c0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
bb9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
bb9f0 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
bba00 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 mber 22.**.** Th
bba10 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
bba20 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
bba30 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
bba40 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
bba50 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
bba60 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
bba70 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
bba80 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
bba90 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
bbaa0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
bbab0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
bbac0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
bbad0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
bbae0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
bbaf0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
bbb00 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
bbb10 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
bbb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbb60 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ******.** This i
bbb70 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 s the implementa
bbb80 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63 20 tion of generic
bbb90 68 61 73 68 2d 74 61 62 6c 65 73 0a 2a 2a 20 75 hash-tables.** u
bbba0 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a sed in SQLite..*
bbbb0 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e 63 *.** $Id: hash.c
bbbc0 2c 76 20 31 2e 33 33 20 32 30 30 39 2f 30 31 2f ,v 1.33 2009/01/
bbbd0 30 39 20 30 31 3a 31 32 3a 32 38 20 64 72 68 20 09 01:12:28 drh
bbbe0 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 54 75 72 Exp $.*/../* Tur
bbbf0 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e n bulk memory in
bbc00 74 6f 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 to a hash table
bbc10 6f 62 6a 65 63 74 20 62 79 20 69 6e 69 74 69 61 object by initia
bbc20 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 lizing the.** fi
bbc30 65 6c 64 73 20 6f 66 20 74 68 65 20 48 61 73 68 elds of the Hash
bbc40 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a structure..**.*
bbc50 2a 20 22 70 4e 65 77 22 20 69 73 20 61 20 70 6f * "pNew" is a po
bbc60 69 6e 74 65 72 20 74 6f 20 74 68 65 20 68 61 73 inter to the has
bbc70 68 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 h table that is
bbc80 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 to be initialize
bbc90 64 2e 0a 2a 2a 20 22 63 6f 70 79 4b 65 79 22 20 d..** "copyKey"
bbca0 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 68 is true if the h
bbcb0 61 73 68 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 ash table should
bbcc0 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 make its own pr
bbcd0 69 76 61 74 65 0a 2a 2a 20 63 6f 70 79 20 6f 66 ivate.** copy of
bbce0 20 6b 65 79 73 20 61 6e 64 20 66 61 6c 73 65 20 keys and false
bbcf0 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6a 75 73 if it should jus
bbd00 74 20 75 73 65 20 74 68 65 20 73 75 70 70 6c 69 t use the suppli
bbd10 65 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 ed pointer..*/.S
bbd20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
bbd30 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e id sqlite3HashIn
bbd40 69 74 28 48 61 73 68 20 2a 70 4e 65 77 2c 20 69 it(Hash *pNew, i
bbd50 6e 74 20 63 6f 70 79 4b 65 79 29 7b 0a 20 20 61 nt copyKey){. a
bbd60 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 ssert( pNew!=0 )
bbd70 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 70 79 4b 65 ;. pNew->copyKe
bbd80 79 20 3d 20 63 6f 70 79 4b 65 79 21 3d 30 3b 0a y = copyKey!=0;.
bbd90 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20 3d 20 pNew->first =
bbda0 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 0;. pNew->count
bbdb0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 = 0;. pNew->ht
bbdc0 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e 65 77 size = 0;. pNew
bbdd0 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 ->ht = 0;.}../*
bbde0 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 72 69 Remove all entri
bbdf0 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68 20 74 es from a hash t
bbe00 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d 20 61 able. Reclaim a
bbe10 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61 ll memory..** Ca
bbe20 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ll this routine
bbe30 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61 73 68 to delete a hash
bbe40 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72 65 73 table or to res
bbe50 65 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a et a hash table.
bbe60 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74 79 20 ** to the empty
bbe70 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 state..*/.SQLITE
bbe80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
bbe90 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48 lite3HashClear(H
bbea0 61 73 68 20 2a 70 48 29 7b 0a 20 20 48 61 73 68 ash *pH){. Hash
bbeb0 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 Elem *elem;
bbec0 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 /* For loopi
bbed0 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d ng over all elem
bbee0 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c ents of the tabl
bbef0 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
bbf00 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 pH!=0 );. elem
bbf10 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70 = pH->first;. p
bbf20 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 H->first = 0;.
bbf30 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d sqlite3_free(pH-
bbf40 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d >ht);. pH->ht =
bbf50 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 0;. pH->htsize
bbf60 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65 = 0;. while( e
bbf70 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 73 68 45 lem ){. HashE
bbf80 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d lem *next_elem =
bbf90 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 elem->next;.
bbfa0 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 if( pH->copyKey
bbfb0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
bbfc0 33 5f 66 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 3_free(elem->pKe
bbfd0 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 y);. }. sq
bbfe0 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d 29 lite3_free(elem)
bbff0 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e 65 78 ;. elem = nex
bc000 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70 48 t_elem;. }. pH
bc010 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a ->count = 0;.}..
bc020 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 /*.** Hash and c
bc030 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 omparison functi
bc040 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 ons when the mod
bc050 65 20 69 73 20 53 51 4c 49 54 45 5f 48 41 53 48 e is SQLITE_HASH
bc060 5f 53 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69 _STRING.*/.stati
bc070 63 20 69 6e 74 20 73 74 72 48 61 73 68 28 63 6f c int strHash(co
bc080 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
bc090 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e int nKey){. con
bc0a0 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f st char *z = (co
bc0b0 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 3b nst char *)pKey;
bc0c0 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 . int h = 0;.
bc0d0 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b if( nKey<=0 ) nK
bc0e0 65 79 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c ey = sqlite3Strl
bc0f0 65 6e 33 30 28 7a 29 3b 0a 20 20 77 68 69 6c 65 en30(z);. while
bc100 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a 20 ( nKey > 0 ){.
bc110 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20 h = (h<<3) ^
bc120 68 20 5e 20 73 71 6c 69 74 65 33 55 70 70 65 72 h ^ sqlite3Upper
bc130 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 ToLower[(unsigne
bc140 64 20 63 68 61 72 29 2a 7a 2b 2b 5d 3b 0a 20 20 d char)*z++];.
bc150 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20 nKey--;. }.
bc160 72 65 74 75 72 6e 20 68 20 26 20 30 78 37 66 66 return h & 0x7ff
bc170 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74 69 63 20 fffff;.}.static
bc180 69 6e 74 20 73 74 72 43 6f 6d 70 61 72 65 28 63 int strCompare(c
bc190 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 onst void *pKey1
bc1a0 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74 20 , int n1, const
bc1b0 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74 void *pKey2, int
bc1c0 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21 3d n2){. if( n1!=
bc1d0 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 n2 ) return 1;.
bc1e0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 return sqlite3S
bc1f0 74 72 4e 49 43 6d 70 28 28 63 6f 6e 73 74 20 63 trNICmp((const c
bc200 68 61 72 2a 29 70 4b 65 79 31 2c 28 63 6f 6e 73 har*)pKey1,(cons
bc210 74 20 63 68 61 72 2a 29 70 4b 65 79 32 2c 6e 31 t char*)pKey2,n1
bc220 29 3b 0a 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 61 );.}.../* Link a
bc230 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 n element into t
bc240 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f he hash table.*/
bc250 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73 .static void ins
bc260 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20 20 48 61 ertElement(. Ha
bc270 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20 sh *pH,
bc280 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 /* The comp
bc290 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 20 lete hash table
bc2a0 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 */. struct _ht
bc2b0 2a 70 45 6e 74 72 79 2c 20 20 20 20 2f 2a 20 54 *pEntry, /* T
bc2c0 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 77 68 he entry into wh
bc2d0 69 63 68 20 70 4e 65 77 20 69 73 20 69 6e 73 65 ich pNew is inse
bc2e0 72 74 65 64 20 2a 2f 0a 20 20 48 61 73 68 45 6c rted */. HashEl
bc2f0 65 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 em *pNew
bc300 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 /* The element
bc310 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a to be inserted *
bc320 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 /.){. HashElem
bc330 2a 70 48 65 61 64 3b 20 20 20 20 20 20 20 2f 2a *pHead; /*
bc340 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 61 First element a
bc350 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74 72 79 lready in pEntry
bc360 20 2a 2f 0a 20 20 70 48 65 61 64 20 3d 20 70 45 */. pHead = pE
bc370 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 69 ntry->chain;. i
bc380 66 28 20 70 48 65 61 64 20 29 7b 0a 20 20 20 20 f( pHead ){.
bc390 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 65 pNew->next = pHe
bc3a0 61 64 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 ad;. pNew->pr
bc3b0 65 76 20 3d 20 70 48 65 61 64 2d 3e 70 72 65 76 ev = pHead->prev
bc3c0 3b 0a 20 20 20 20 69 66 28 20 70 48 65 61 64 2d ;. if( pHead-
bc3d0 3e 70 72 65 76 20 29 7b 20 70 48 65 61 64 2d 3e >prev ){ pHead->
bc3e0 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 70 4e 65 prev->next = pNe
bc3f0 77 3b 20 7d 0a 20 20 20 20 65 6c 73 65 20 20 20 w; }. else
bc400 20 20 20 20 20 20 20 20 20 20 7b 20 70 48 2d 3e { pH->
bc410 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 20 7d 0a first = pNew; }.
bc420 20 20 20 20 70 48 65 61 64 2d 3e 70 72 65 76 20 pHead->prev
bc430 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b = pNew;. }else{
bc440 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 . pNew->next
bc450 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 = pH->first;.
bc460 20 69 66 28 20 70 48 2d 3e 66 69 72 73 74 20 29 if( pH->first )
bc470 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e 70 72 65 { pH->first->pre
bc480 76 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 v = pNew; }.
bc490 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 30 3b 0a pNew->prev = 0;.
bc4a0 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 pH->first =
bc4b0 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 pNew;. }. pEnt
bc4c0 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 70 ry->count++;. p
bc4d0 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70 Entry->chain = p
bc4e0 4e 65 77 3b 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 New;.}.../* Resi
bc4f0 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62 6c ze the hash tabl
bc500 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e e so that it can
bc510 74 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22 tains "new_size"
bc520 20 62 75 63 6b 65 74 73 2e 0a 2a 2a 20 22 6e 65 buckets..** "ne
bc530 77 5f 73 69 7a 65 22 20 6d 75 73 74 20 62 65 20 w_size" must be
bc540 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 20 54 a power of 2. T
bc550 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69 he hash table mi
bc560 67 68 74 20 66 61 69 6c 20 0a 2a 2a 20 74 6f 20 ght fail .** to
bc570 72 65 73 69 7a 65 20 69 66 20 73 71 6c 69 74 65 resize if sqlite
bc580 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 3_malloc() fails
bc590 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
bc5a0 20 72 65 68 61 73 68 28 48 61 73 68 20 2a 70 48 rehash(Hash *pH
bc5b0 2c 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b , int new_size){
bc5c0 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a 6e . struct _ht *n
bc5d0 65 77 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20 ew_ht;
bc5e0 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 68 61 73 /* The new has
bc5f0 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 h table */. Has
bc600 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 hElem *elem, *ne
bc610 78 74 5f 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 46 xt_elem; /* F
bc620 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 or looping over
bc630 65 78 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74 existing element
bc640 73 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c s */..#ifdef SQL
bc650 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f ITE_MALLOC_SOFT_
bc660 4c 49 4d 49 54 0a 20 20 69 66 28 20 6e 65 77 5f LIMIT. if( new_
bc670 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 size*sizeof(stru
bc680 63 74 20 5f 68 74 29 3e 53 51 4c 49 54 45 5f 4d ct _ht)>SQLITE_M
bc690 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 ALLOC_SOFT_LIMIT
bc6a0 20 29 7b 0a 20 20 20 20 6e 65 77 5f 73 69 7a 65 ){. new_size
bc6b0 20 3d 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 = SQLITE_MALLOC
bc6c0 5f 53 4f 46 54 5f 4c 49 4d 49 54 2f 73 69 7a 65 _SOFT_LIMIT/size
bc6d0 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0a of(struct _ht);.
bc6e0 20 20 7d 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 }. if( new_si
bc6f0 7a 65 3d 3d 70 48 2d 3e 68 74 73 69 7a 65 20 29 ze==pH->htsize )
bc700 20 72 65 74 75 72 6e 3b 0a 23 65 6e 64 69 66 0a return;.#endif.
bc710 0a 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 61 . /* There is a
bc720 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
bc730 5f 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 _malloc() inside
bc740 20 72 65 68 61 73 68 28 29 2e 20 49 66 20 74 68 rehash(). If th
bc750 65 72 65 20 69 73 0a 20 20 2a 2a 20 61 6c 72 65 ere is. ** alre
bc760 61 64 79 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f ady an allocatio
bc770 6e 20 61 74 20 70 48 2d 3e 68 74 2c 20 74 68 65 n at pH->ht, the
bc780 6e 20 69 66 20 74 68 69 73 20 6d 61 6c 6c 6f 63 n if this malloc
bc790 28 29 20 66 61 69 6c 73 20 69 74 0a 20 20 2a 2a () fails it. **
bc7a0 20 69 73 20 62 65 6e 69 67 6e 20 28 73 69 6e 63 is benign (sinc
bc7b0 65 20 66 61 69 6c 69 6e 67 20 74 6f 20 72 65 73 e failing to res
bc7c0 69 7a 65 20 61 20 68 61 73 68 20 74 61 62 6c 65 ize a hash table
bc7d0 20 69 73 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 is a performanc
bc7e0 65 0a 20 20 2a 2a 20 68 69 74 20 6f 6e 6c 79 2c e. ** hit only,
bc7f0 20 6e 6f 74 20 61 20 66 61 74 61 6c 20 65 72 72 not a fatal err
bc800 6f 72 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 or).. */. if(
bc810 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 20 73 pH->htsize>0 ) s
bc820 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
bc830 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 6e 65 77 nMalloc();. new
bc840 5f 68 74 20 3d 20 28 73 74 72 75 63 74 20 5f 68 _ht = (struct _h
bc850 74 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f t *)sqlite3Mallo
bc860 63 5a 65 72 6f 28 20 6e 65 77 5f 73 69 7a 65 2a cZero( new_size*
bc870 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 sizeof(struct _h
bc880 74 29 20 29 3b 0a 20 20 69 66 28 20 70 48 2d 3e t) );. if( pH->
bc890 68 74 73 69 7a 65 3e 30 20 29 20 73 71 6c 69 74 htsize>0 ) sqlit
bc8a0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
bc8b0 63 28 29 3b 0a 0a 20 20 69 66 28 20 6e 65 77 5f c();.. if( new_
bc8c0 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a ht==0 ) return;.
bc8d0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
bc8e0 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 H->ht);. pH->ht
bc8f0 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d = new_ht;. pH-
bc900 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 >htsize = new_si
bc910 7a 65 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 ze;. for(elem=p
bc920 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 H->first, pH->fi
bc930 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 rst=0; elem; ele
bc940 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a m = next_elem){.
bc950 20 20 20 20 69 6e 74 20 68 20 3d 20 73 74 72 48 int h = strH
bc960 61 73 68 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 ash(elem->pKey,
bc970 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20 26 20 28 6e elem->nKey) & (n
bc980 65 77 5f 73 69 7a 65 2d 31 29 3b 0a 20 20 20 20 ew_size-1);.
bc990 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d next_elem = elem
bc9a0 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69 6e 73 65 ->next;. inse
bc9b0 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 6e rtElement(pH, &n
bc9c0 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b ew_ht[h], elem);
bc9d0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 . }.}../* This
bc9e0 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e function (for in
bc9f0 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29 ternal use only)
bca00 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d locates an elem
bca10 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 ent in an.** has
bca20 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 h table that mat
bca30 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b ches the given k
bca40 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f ey. The hash fo
bca50 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a r this key has.*
bca60 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 * already been c
bca70 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20 70 omputed and is p
bca80 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74 68 assed as the 4th
bca90 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 parameter..*/.s
bcaa0 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d 20 2a tatic HashElem *
bcab0 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e findElementGiven
bcac0 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 61 Hash(. const Ha
bcad0 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54 sh *pH, /* T
bcae0 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 72 he pH to be sear
bcaf0 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ched */. const
bcb00 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a void *pKey, /*
bcb10 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20 The key we are
bcb20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f searching for */
bcb30 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0a 20 20 69 . int nKey,. i
bcb40 6e 74 20 68 20 20 20 20 20 20 20 20 20 20 20 20 nt h
bcb50 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 /* The hash f
bcb60 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a or this key. */.
bcb70 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 ){. HashElem *e
bcb80 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 lem;
bcb90 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c /* Used to l
bcba0 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 oop thru the ele
bcbb0 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 ment list */. i
bcbc0 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 nt count;
bcbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
bcbe0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 Number of eleme
bcbf0 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74 nts left to test
bcc00 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 */.. if( pH->h
bcc10 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 t ){. struct
bcc20 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 _ht *pEntry = &p
bcc30 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c H->ht[h];. el
bcc40 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 em = pEntry->cha
bcc50 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 in;. count =
bcc60 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 pEntry->count;.
bcc70 20 20 20 77 68 69 6c 65 28 20 63 6f 75 6e 74 2d while( count-
bcc80 2d 20 26 26 20 65 6c 65 6d 20 29 7b 0a 20 20 20 - && elem ){.
bcc90 20 20 20 69 66 28 20 73 74 72 43 6f 6d 70 61 72 if( strCompar
bcca0 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 65 6c 65 e(elem->pKey,ele
bccb0 6d 2d 3e 6e 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 m->nKey,pKey,nKe
bccc0 79 29 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20 20 y)==0 ){ .
bccd0 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 20 return elem;.
bcce0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 6c 65 }. ele
bccf0 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a m = elem->next;.
bcd00 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
bcd10 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f rn 0;.}../* Remo
bcd20 76 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 ve a single entr
bcd30 79 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 y from the hash
bcd40 74 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f table given a po
bcd50 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a inter to that.**
bcd60 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 element and a h
bcd70 61 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 ash on the eleme
bcd80 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 nt's key..*/.sta
bcd90 74 69 63 20 76 6f 69 64 20 72 65 6d 6f 76 65 45 tic void removeE
bcda0 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 lementGivenHash(
bcdb0 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 . Hash *pH,
bcdc0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63 /* The pH c
bcdd0 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22 ontaining "elem"
bcde0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20 */. HashElem*
bcdf0 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 elem, /* The e
bce00 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d lement to be rem
bce10 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48 oved from the pH
bce20 20 2a 2f 0a 20 20 69 6e 74 20 68 20 20 20 20 20 */. int h
bce30 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 /* Hash
bce40 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c value for the el
bce50 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 ement */.){. st
bce60 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 ruct _ht *pEntry
bce70 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72 ;. if( elem->pr
bce80 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e ev ){. elem->
bce90 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 prev->next = ele
bcea0 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73 m->next; . }els
bceb0 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 e{. pH->first
bcec0 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 = elem->next;.
bced0 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e }. if( elem->n
bcee0 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d ext ){. elem-
bcef0 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c >next->prev = el
bcf00 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20 em->prev;. }.
bcf10 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 pEntry = &pH->ht
bcf20 5b 68 5d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72 [h];. if( pEntr
bcf30 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 y->chain==elem )
bcf40 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 {. pEntry->ch
bcf50 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 ain = elem->next
bcf60 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e ;. }. pEntry->
bcf70 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 count--;. if( p
bcf80 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3c 3d 30 20 Entry->count<=0
bcf90 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 ){. pEntry->c
bcfa0 68 61 69 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 hain = 0;. }.
bcfb0 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 if( pH->copyKey
bcfc0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
bcfd0 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b ree(elem->pKey);
bcfe0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 . }. sqlite3_f
bcff0 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 ree( elem );. p
bd000 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 H->count--;. if
bd010 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 ( pH->count<=0 )
bd020 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48 {. assert( pH
bd030 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 ->first==0 );.
bd040 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f assert( pH->co
bd050 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 unt==0 );. sq
bd060 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 70 lite3HashClear(p
bd070 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 H);. }.}../* At
bd080 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 tempt to locate
bd090 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 an element of th
bd0a0 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 20 e hash table pH
bd0b0 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 with a key.** th
bd0c0 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c at matches pKey,
bd0d0 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 61 20 nKey. Return a
bd0e0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 pointer to the c
bd0f0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 0a 2a 2a orresponding .**
bd100 20 48 61 73 68 45 6c 65 6d 20 73 74 72 75 63 74 HashElem struct
bd110 75 72 65 20 66 6f 72 20 74 68 69 73 20 65 6c 65 ure for this ele
bd120 6d 65 6e 74 20 69 66 20 69 74 20 69 73 20 66 6f ment if it is fo
bd130 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 0a 2a 2a 20 und, or NULL.**
bd140 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 otherwise..*/.SQ
bd150 4c 49 54 45 5f 50 52 49 56 41 54 45 20 48 61 73 LITE_PRIVATE Has
bd160 68 45 6c 65 6d 20 2a 73 71 6c 69 74 65 33 48 61 hElem *sqlite3Ha
bd170 73 68 46 69 6e 64 45 6c 65 6d 28 63 6f 6e 73 74 shFindElem(const
bd180 20 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 Hash *pH, const
bd190 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 void *pKey, int
bd1a0 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 3b nKey){. int h;
bd1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
bd1c0 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f A hash on key */
bd1d0 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 . HashElem *ele
bd1e0 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 m; /* The ele
bd1f0 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68 65 ment that matche
bd200 73 20 6b 65 79 20 2a 2f 0a 0a 20 20 69 66 28 20 s key */.. if(
bd210 70 48 3d 3d 30 20 7c 7c 20 70 48 2d 3e 68 74 3d pH==0 || pH->ht=
bd220 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
bd230 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65 h = strHash(pKe
bd240 79 2c 6e 4b 65 79 29 3b 0a 20 20 65 6c 65 6d 20 y,nKey);. elem
bd250 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 = findElementGiv
bd260 65 6e 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e enHash(pH,pKey,n
bd270 4b 65 79 2c 20 68 20 25 20 70 48 2d 3e 68 74 73 Key, h % pH->hts
bd280 69 7a 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 65 ize);. return e
bd290 6c 65 6d 3b 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d lem;.}../* Attem
bd2a0 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 pt to locate an
bd2b0 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 element of the h
bd2c0 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 74 ash table pH wit
bd2d0 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 h a key.** that
bd2e0 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 matches pKey,nKe
bd2f0 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 64 y. Return the d
bd300 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c 65 ata for this ele
bd310 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a 2a ment if it is.**
bd320 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 found, or NULL
bd330 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d if there is no m
bd340 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f atch..*/.SQLITE_
bd350 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
bd360 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f lite3HashFind(co
bd370 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63 6f nst Hash *pH, co
bd380 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
bd390 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61 73 int nKey){. Has
bd3a0 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 hElem *elem;
bd3b0 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 /* The element t
bd3c0 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 hat matches key
bd3d0 2a 2f 0a 20 20 65 6c 65 6d 20 3d 20 73 71 6c 69 */. elem = sqli
bd3e0 74 65 33 48 61 73 68 46 69 6e 64 45 6c 65 6d 28 te3HashFindElem(
bd3f0 70 48 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b pH, pKey, nKey);
bd400 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f . return elem ?
bd410 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b elem->data : 0;
bd420 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e .}../* Insert an
bd430 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 element into th
bd440 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e e hash table pH.
bd450 20 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 The key is pKe
bd460 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 y,nKey.** and th
bd470 65 20 64 61 74 61 20 69 73 20 22 64 61 74 61 22 e data is "data"
bd480 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c ..**.** If no el
bd490 65 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 ement exists wit
bd4a0 68 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 h a matching key
bd4b0 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 , then a new.**
bd4c0 65 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 element is creat
bd4d0 65 64 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74 ed. A copy of t
bd4e0 68 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69 he key is made i
bd4f0 66 20 74 68 65 20 63 6f 70 79 4b 65 79 0a 2a 2a f the copyKey.**
bd500 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e flag is set. N
bd510 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e ULL is returned.
bd520 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 .**.** If anothe
bd530 72 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 r element alread
bd540 79 20 65 78 69 73 74 73 20 77 69 74 68 20 74 68 y exists with th
bd550 65 20 73 61 6d 65 20 6b 65 79 2c 20 74 68 65 6e e same key, then
bd560 20 74 68 65 0a 2a 2a 20 6e 65 77 20 64 61 74 61 the.** new data
bd570 20 72 65 70 6c 61 63 65 73 20 74 68 65 20 6f 6c replaces the ol
bd580 64 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 6f d data and the o
bd590 6c 64 20 64 61 74 61 20 69 73 20 72 65 74 75 72 ld data is retur
bd5a0 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 ned..** The key
bd5b0 69 73 20 6e 6f 74 20 63 6f 70 69 65 64 20 69 6e is not copied in
bd5c0 20 74 68 69 73 20 69 6e 73 74 61 6e 63 65 2e 20 this instance.
bd5d0 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 If a malloc fai
bd5e0 6c 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 ls, then.** the
bd5f0 6e 65 77 20 64 61 74 61 20 69 73 20 72 65 74 75 new data is retu
bd600 72 6e 65 64 20 61 6e 64 20 74 68 65 20 68 61 73 rned and the has
bd610 68 20 74 61 62 6c 65 20 69 73 20 75 6e 63 68 61 h table is uncha
bd620 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 nged..**.** If t
bd630 68 65 20 22 64 61 74 61 22 20 70 61 72 61 6d 65 he "data" parame
bd640 74 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 ter to this func
bd650 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 tion is NULL, th
bd660 65 6e 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e en the.** elemen
bd670 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 t corresponding
bd680 74 6f 20 22 6b 65 79 22 20 69 73 20 72 65 6d 6f to "key" is remo
bd690 76 65 64 20 66 72 6f 6d 20 74 68 65 20 68 61 73 ved from the has
bd6a0 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 h table..*/.SQLI
bd6b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
bd6c0 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 *sqlite3HashInse
bd6d0 72 74 28 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e rt(Hash *pH, con
bd6e0 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 st void *pKey, i
bd6f0 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 64 nt nKey, void *d
bd700 61 74 61 29 7b 0a 20 20 69 6e 74 20 68 72 61 77 ata){. int hraw
bd710 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
bd720 20 52 61 77 20 68 61 73 68 20 76 61 6c 75 65 20 Raw hash value
bd730 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 of the key */.
bd740 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 int h;
bd750 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 /* the has
bd760 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 h of the key mod
bd770 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73 ulo hash table s
bd780 69 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 ize */. HashEle
bd790 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f m *elem; /
bd7a0 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 * Used to loop t
bd7b0 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 hru the element
bd7c0 6c 69 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c list */. HashEl
bd7d0 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 em *new_elem;
bd7e0 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 /* New element a
bd7f0 64 64 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a dded to the pH *
bd800 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 /.. assert( pH!
bd810 3d 30 20 29 3b 0a 20 20 68 72 61 77 20 3d 20 73 =0 );. hraw = s
bd820 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 trHash(pKey, nKe
bd830 79 29 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 y);. if( pH->ht
bd840 73 69 7a 65 20 29 7b 0a 20 20 20 20 68 20 3d 20 size ){. h =
bd850 68 72 61 77 20 25 20 70 48 2d 3e 68 74 73 69 7a hraw % pH->htsiz
bd860 65 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 66 69 e;. elem = fi
bd870 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 ndElementGivenHa
bd880 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c sh(pH,pKey,nKey,
bd890 68 29 3b 0a 20 20 20 20 69 66 28 20 65 6c 65 6d h);. if( elem
bd8a0 20 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a ){. void *
bd8b0 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d old_data = elem-
bd8c0 3e 64 61 74 61 3b 0a 20 20 20 20 20 20 69 66 28 >data;. if(
bd8d0 20 64 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 data==0 ){.
bd8e0 20 20 20 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e removeElemen
bd8f0 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 65 6c tGivenHash(pH,el
bd900 65 6d 2c 68 29 3b 0a 20 20 20 20 20 20 7d 65 6c em,h);. }el
bd910 73 65 7b 0a 20 20 20 20 20 20 20 20 65 6c 65 6d se{. elem
bd920 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 ->data = data;.
bd930 20 20 20 20 20 20 20 69 66 28 20 21 70 48 2d 3e if( !pH->
bd940 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20 20 20 copyKey ){.
bd950 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 elem->pKey
bd960 3d 20 28 76 6f 69 64 20 2a 29 70 4b 65 79 3b 0a = (void *)pKey;.
bd970 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
bd980 20 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 assert(nKey==e
bd990 6c 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 lem->nKey);.
bd9a0 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e }. return
bd9b0 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 20 20 7d old_data;. }
bd9c0 0a 20 20 7d 0a 20 20 69 66 28 20 64 61 74 61 3d . }. if( data=
bd9d0 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
bd9e0 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 28 48 61 73 new_elem = (Has
bd9f0 68 45 6c 65 6d 2a 29 73 71 6c 69 74 65 33 4d 61 hElem*)sqlite3Ma
bda00 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 48 61 73 lloc( sizeof(Has
bda10 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69 66 28 20 hElem) );. if(
bda20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 20 72 65 new_elem==0 ) re
bda30 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 69 66 28 turn data;. if(
bda40 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20 pH->copyKey &&
bda50 70 4b 65 79 21 3d 30 20 29 7b 0a 20 20 20 20 6e pKey!=0 ){. n
bda60 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 ew_elem->pKey =
bda70 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e sqlite3Malloc( n
bda80 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 6e Key );. if( n
bda90 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 3d 3d 30 ew_elem->pKey==0
bdaa0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
bdab0 33 5f 66 72 65 65 28 6e 65 77 5f 65 6c 65 6d 29 3_free(new_elem)
bdac0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 64 ;. return d
bdad0 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d ata;. }. m
bdae0 65 6d 63 70 79 28 28 76 6f 69 64 2a 29 6e 65 77 emcpy((void*)new
bdaf0 5f 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 70 4b 65 _elem->pKey, pKe
bdb00 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 y, nKey);. }els
bdb10 65 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d e{. new_elem-
bdb20 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 2a 29 70 >pKey = (void*)p
bdb30 4b 65 79 3b 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 Key;. }. new_e
bdb40 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 lem->nKey = nKey
bdb50 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b ;. pH->count++;
bdb60 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a . if( pH->htsiz
bdb70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 68 61 e==0 ){. reha
bdb80 73 68 28 70 48 2c 20 31 32 38 2f 73 69 7a 65 6f sh(pH, 128/sizeo
bdb90 66 28 70 48 2d 3e 68 74 5b 30 5d 29 29 3b 0a 20 f(pH->ht[0]));.
bdba0 20 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a if( pH->htsiz
bdbb0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 48 e==0 ){. pH
bdbc0 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 20 ->count = 0;.
bdbd0 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b if( pH->copyK
bdbe0 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ey ){. sq
bdbf0 6c 69 74 65 33 5f 66 72 65 65 28 6e 65 77 5f 65 lite3_free(new_e
bdc00 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 lem->pKey);.
bdc10 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
bdc20 33 5f 66 72 65 65 28 6e 65 77 5f 65 6c 65 6d 29 3_free(new_elem)
bdc30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 64 ;. return d
bdc40 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ata;. }. }.
bdc50 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 20 3e if( pH->count >
bdc60 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 pH->htsize ){.
bdc70 20 20 20 72 65 68 61 73 68 28 70 48 2c 70 48 2d rehash(pH,pH-
bdc80 3e 68 74 73 69 7a 65 2a 32 29 3b 0a 20 20 7d 0a >htsize*2);. }.
bdc90 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 68 74 assert( pH->ht
bdca0 73 69 7a 65 3e 30 20 29 3b 0a 20 20 68 20 3d 20 size>0 );. h =
bdcb0 68 72 61 77 20 25 20 70 48 2d 3e 68 74 73 69 7a hraw % pH->htsiz
bdcc0 65 3b 0a 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 e;. insertEleme
bdcd0 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74 5b 68 nt(pH, &pH->ht[h
bdce0 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 ], new_elem);.
bdcf0 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61 20 3d new_elem->data =
bdd00 20 64 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 data;. return
bdd10 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 0;.}../*********
bdd20 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 ***** End of has
bdd30 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h.c ************
bdd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdd60 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
bdd70 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
bdd80 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a opcodes.c *****
bdd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bddb0 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 ****/./* Automat
bddc0 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 ically generated
bddd0 2e 20 20 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a . Do not edit *
bdde0 2f 0a 2f 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f /./* See the mko
bddf0 70 63 6f 64 65 63 2e 61 77 6b 20 73 63 72 69 70 pcodec.awk scrip
bde00 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a t for details. *
bde10 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
bde20 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 QLITE_OMIT_EXPLA
bde30 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 IN) || !defined(
bde40 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e NDEBUG) || defin
bde50 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 ed(VDBE_PROFILE)
bde60 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
bde70 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 TE_DEBUG).SQLITE
bde80 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
bde90 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f har *sqlite3Opco
bdea0 64 65 4e 61 6d 65 28 69 6e 74 20 69 29 7b 0a 20 deName(int i){.
bdeb0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
bdec0 72 20 2a 63 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b r *const azName[
bded0 5d 20 3d 20 7b 20 22 3f 22 2c 0a 20 20 20 20 20 ] = { "?",.
bdee0 2f 2a 20 20 20 31 20 2a 2f 20 22 56 4e 65 78 74 /* 1 */ "VNext
bdef0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 32 20 2a ",. /* 2 *
bdf00 2f 20 22 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 / "Affinity",.
bdf10 20 20 20 2f 2a 20 20 20 33 20 2a 2f 20 22 43 6f /* 3 */ "Co
bdf20 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 lumn",. /*
bdf30 20 34 20 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65 4 */ "SetCookie
bdf40 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 35 20 2a ",. /* 5 *
bdf50 2f 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f / "Seek",. /
bdf60 2a 20 20 20 36 20 2a 2f 20 22 53 65 71 75 65 6e * 6 */ "Sequen
bdf70 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37 ce",. /* 7
bdf80 20 2a 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c */ "Savepoint",
bdf90 0a 20 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 . /* 8 */
bdfa0 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f "RowKey",. /
bdfb0 2a 20 20 20 39 20 2a 2f 20 22 53 43 6f 70 79 22 * 9 */ "SCopy"
bdfc0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f ,. /* 10 */
bdfd0 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 20 "OpenWrite",.
bdfe0 20 20 20 2f 2a 20 20 31 31 20 2a 2f 20 22 49 66 /* 11 */ "If
bdff0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a ",. /* 12 *
be000 2f 20 22 56 52 6f 77 69 64 22 2c 0a 20 20 20 20 / "VRowid",.
be010 20 2f 2a 20 20 31 33 20 2a 2f 20 22 43 6f 6c 6c /* 13 */ "Coll
be020 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 Seq",. /* 1
be030 34 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64 22 2c 4 */ "OpenRead",
be040 0a 20 20 20 20 20 2f 2a 20 20 31 35 20 2a 2f 20 . /* 15 */
be050 22 45 78 70 69 72 65 22 2c 0a 20 20 20 20 20 2f "Expire",. /
be060 2a 20 20 31 36 20 2a 2f 20 22 41 75 74 6f 43 6f * 16 */ "AutoCo
be070 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 mmit",. /*
be080 31 37 20 2a 2f 20 22 50 61 67 65 63 6f 75 6e 74 17 */ "Pagecount
be090 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 38 20 2a ",. /* 18 *
be0a0 2f 20 22 49 6e 74 65 67 72 69 74 79 43 6b 22 2c / "IntegrityCk",
be0b0 0a 20 20 20 20 20 2f 2a 20 20 31 39 20 2a 2f 20 . /* 19 */
be0c0 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 "Not",. /*
be0d0 32 30 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 */ "Sort",.
be0e0 20 20 20 2f 2a 20 20 32 31 20 2a 2f 20 22 43 6f /* 21 */ "Co
be0f0 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 32 py",. /* 22
be100 20 2a 2f 20 22 54 72 61 63 65 22 2c 0a 20 20 20 */ "Trace",.
be110 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 46 75 6e /* 23 */ "Fun
be120 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 ction",. /*
be130 20 32 34 20 2a 2f 20 22 49 66 4e 65 67 22 2c 0a 24 */ "IfNeg",.
be140 20 20 20 20 20 2f 2a 20 20 32 35 20 2a 2f 20 22 /* 25 */ "
be150 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Noop",. /*
be160 32 36 20 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 26 */ "Return",.
be170 20 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 /* 27 */ "
be180 4e 65 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 NewRowid",.
be190 2f 2a 20 20 32 38 20 2a 2f 20 22 56 61 72 69 61 /* 28 */ "Varia
be1a0 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 ble",. /* 2
be1b0 39 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c 0a 20 9 */ "String",.
be1c0 20 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22 52 /* 30 */ "R
be1d0 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 ealAffinity",.
be1e0 20 20 20 2f 2a 20 20 33 31 20 2a 2f 20 22 56 52 /* 31 */ "VR
be1f0 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f 2a 20 ename",. /*
be200 20 33 32 20 2a 2f 20 22 50 61 72 73 65 53 63 68 32 */ "ParseSch
be210 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 ema",. /* 3
be220 33 20 2a 2f 20 22 56 4f 70 65 6e 22 2c 0a 20 20 3 */ "VOpen",.
be230 20 20 20 2f 2a 20 20 33 34 20 2a 2f 20 22 43 6c /* 34 */ "Cl
be240 6f 73 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 ose",. /* 3
be250 35 20 2a 2f 20 22 43 72 65 61 74 65 49 6e 64 65 5 */ "CreateInde
be260 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 x",. /* 36
be270 2a 2f 20 22 49 73 55 6e 69 71 75 65 22 2c 0a 20 */ "IsUnique",.
be280 20 20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22 4e /* 37 */ "N
be290 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f otFound",. /
be2a0 2a 20 20 33 38 20 2a 2f 20 22 49 6e 74 36 34 22 * 38 */ "Int64"
be2b0 2c 0a 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f ,. /* 39 */
be2c0 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 "MustBeInt",.
be2d0 20 20 20 2f 2a 20 20 34 30 20 2a 2f 20 22 48 61 /* 40 */ "Ha
be2e0 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 31 lt",. /* 41
be2f0 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 */ "Rowid",.
be300 20 20 2f 2a 20 20 34 32 20 2a 2f 20 22 49 64 78 /* 42 */ "Idx
be310 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 33 LT",. /* 43
be320 20 2a 2f 20 22 41 64 64 49 6d 6d 22 2c 0a 20 20 */ "AddImm",.
be330 20 20 20 2f 2a 20 20 34 34 20 2a 2f 20 22 53 74 /* 44 */ "St
be340 61 74 65 6d 65 6e 74 22 2c 0a 20 20 20 20 20 2f atement",. /
be350 2a 20 20 34 35 20 2a 2f 20 22 52 6f 77 44 61 74 * 45 */ "RowDat
be360 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 36 20 a",. /* 46
be370 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 */ "MemMax",.
be380 20 20 2f 2a 20 20 34 37 20 2a 2f 20 22 4e 6f 74 /* 47 */ "Not
be390 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20 2f 2a Exists",. /*
be3a0 20 20 34 38 20 2a 2f 20 22 47 6f 73 75 62 22 2c 48 */ "Gosub",
be3b0 0a 20 20 20 20 20 2f 2a 20 20 34 39 20 2a 2f 20 . /* 49 */
be3c0 22 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 20 20 "Integer",.
be3d0 2f 2a 20 20 35 30 20 2a 2f 20 22 50 72 65 76 22 /* 50 */ "Prev"
be3e0 2c 0a 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f ,. /* 51 */
be3f0 20 22 52 6f 77 53 65 74 52 65 61 64 22 2c 0a 20 "RowSetRead",.
be400 20 20 20 20 2f 2a 20 20 35 32 20 2a 2f 20 22 52 /* 52 */ "R
be410 6f 77 53 65 74 41 64 64 22 2c 0a 20 20 20 20 20 owSetAdd",.
be420 2f 2a 20 20 35 33 20 2a 2f 20 22 56 43 6f 6c 75 /* 53 */ "VColu
be430 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 34 mn",. /* 54
be440 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62 6c 65 */ "CreateTable
be450 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 35 20 2a ",. /* 55 *
be460 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20 20 20 2f / "Last",. /
be470 2a 20 20 35 36 20 2a 2f 20 22 53 65 65 6b 4c 65 * 56 */ "SeekLe
be480 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a ",. /* 57 *
be490 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c 0a / "IncrVacuum",.
be4a0 20 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22 /* 58 */ "
be4b0 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 IdxRowid",.
be4c0 2f 2a 20 20 35 39 20 2a 2f 20 22 52 65 73 65 74 /* 59 */ "Reset
be4d0 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 Count",. /*
be4e0 20 36 30 20 2a 2f 20 22 43 6f 6e 74 65 78 74 50 60 */ "ContextP
be4f0 75 73 68 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 ush",. /* 6
be500 31 20 2a 2f 20 22 59 69 65 6c 64 22 2c 0a 20 20 1 */ "Yield",.
be510 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 44 72 /* 62 */ "Dr
be520 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20 20 20 opTrigger",.
be530 20 2f 2a 20 20 36 33 20 2a 2f 20 22 4f 72 22 2c /* 63 */ "Or",
be540 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 2a 2f 20 . /* 64 */
be550 22 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 "And",. /*
be560 36 35 20 2a 2f 20 22 44 72 6f 70 49 6e 64 65 78 65 */ "DropIndex
be570 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36 20 2a ",. /* 66 *
be580 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 20 20 20 / "IdxGE",.
be590 2f 2a 20 20 36 37 20 2a 2f 20 22 49 64 78 44 65 /* 67 */ "IdxDe
be5a0 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 lete",. /*
be5b0 36 38 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 68 */ "IsNull",.
be5c0 20 20 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22 /* 69 */ "
be5d0 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f NotNull",. /
be5e0 2a 20 20 37 30 20 2a 2f 20 22 4e 65 22 2c 0a 20 * 70 */ "Ne",.
be5f0 20 20 20 20 2f 2a 20 20 37 31 20 2a 2f 20 22 45 /* 71 */ "E
be600 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 32 20 q",. /* 72
be610 2a 2f 20 22 47 74 22 2c 0a 20 20 20 20 20 2f 2a */ "Gt",. /*
be620 20 20 37 33 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 73 */ "Le",.
be630 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4c 74 /* 74 */ "Lt
be640 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 35 20 2a ",. /* 75 *
be650 2f 20 22 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 / "Ge",. /*
be660 20 37 36 20 2a 2f 20 22 56 61 63 75 75 6d 22 2c 76 */ "Vacuum",
be670 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 . /* 77 */
be680 22 42 69 74 41 6e 64 22 2c 0a 20 20 20 20 20 2f "BitAnd",. /
be690 2a 20 20 37 38 20 2a 2f 20 22 42 69 74 4f 72 22 * 78 */ "BitOr"
be6a0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 39 20 2a 2f ,. /* 79 */
be6b0 20 22 53 68 69 66 74 4c 65 66 74 22 2c 0a 20 20 "ShiftLeft",.
be6c0 20 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22 53 68 /* 80 */ "Sh
be6d0 69 66 74 52 69 67 68 74 22 2c 0a 20 20 20 20 20 iftRight",.
be6e0 2f 2a 20 20 38 31 20 2a 2f 20 22 41 64 64 22 2c /* 81 */ "Add",
be6f0 0a 20 20 20 20 20 2f 2a 20 20 38 32 20 2a 2f 20 . /* 82 */
be700 22 53 75 62 74 72 61 63 74 22 2c 0a 20 20 20 20 "Subtract",.
be710 20 2f 2a 20 20 38 33 20 2a 2f 20 22 4d 75 6c 74 /* 83 */ "Mult
be720 69 70 6c 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 iply",. /*
be730 38 34 20 2a 2f 20 22 44 69 76 69 64 65 22 2c 0a 84 */ "Divide",.
be740 20 20 20 20 20 2f 2a 20 20 38 35 20 2a 2f 20 22 /* 85 */ "
be750 52 65 6d 61 69 6e 64 65 72 22 2c 0a 20 20 20 20 Remainder",.
be760 20 2f 2a 20 20 38 36 20 2a 2f 20 22 43 6f 6e 63 /* 86 */ "Conc
be770 61 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 37 at",. /* 87
be780 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 */ "IfNot",.
be790 20 20 2f 2a 20 20 38 38 20 2a 2f 20 22 44 72 6f /* 88 */ "Dro
be7a0 70 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a pTable",. /*
be7b0 20 20 38 39 20 2a 2f 20 22 53 65 65 6b 4c 74 22 89 */ "SeekLt"
be7c0 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30 20 2a 2f ,. /* 90 */
be7d0 20 22 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20 "BitNot",.
be7e0 2f 2a 20 20 39 31 20 2a 2f 20 22 53 74 72 69 6e /* 91 */ "Strin
be7f0 67 38 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 32 g8",. /* 92
be800 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 22 */ "MakeRecord"
be810 2c 0a 20 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f ,. /* 93 */
be820 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a 20 20 "ResultRow",.
be830 20 20 20 2f 2a 20 20 39 34 20 2a 2f 20 22 44 65 /* 94 */ "De
be840 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 lete",. /*
be850 39 35 20 2a 2f 20 22 41 67 67 46 69 6e 61 6c 22 95 */ "AggFinal"
be860 2c 0a 20 20 20 20 20 2f 2a 20 20 39 36 20 2a 2f ,. /* 96 */
be870 20 22 43 6f 6d 70 61 72 65 22 2c 0a 20 20 20 20 "Compare",.
be880 20 2f 2a 20 20 39 37 20 2a 2f 20 22 47 6f 74 6f /* 97 */ "Goto
be890 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 38 20 2a ",. /* 98 *
be8a0 2f 20 22 54 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20 / "TableLock",.
be8b0 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22 43 /* 99 */ "C
be8c0 6c 65 61 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 lear",. /* 1
be8d0 30 30 20 2a 2f 20 22 56 65 72 69 66 79 43 6f 6f 00 */ "VerifyCoo
be8e0 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 kie",. /* 10
be8f0 31 20 2a 2f 20 22 41 67 67 53 74 65 70 22 2c 0a 1 */ "AggStep",.
be900 20 20 20 20 20 2f 2a 20 31 30 32 20 2a 2f 20 22 /* 102 */ "
be910 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 22 2c 0a SetNumColumns",.
be920 20 20 20 20 20 2f 2a 20 31 30 33 20 2a 2f 20 22 /* 103 */ "
be930 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a 20 20 Transaction",.
be940 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 56 46 /* 104 */ "VF
be950 69 6c 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 ilter",. /*
be960 31 30 35 20 2a 2f 20 22 56 44 65 73 74 72 6f 79 105 */ "VDestroy
be970 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 36 20 2a ",. /* 106 *
be980 2f 20 22 43 6f 6e 74 65 78 74 50 6f 70 22 2c 0a / "ContextPop",.
be990 20 20 20 20 20 2f 2a 20 31 30 37 20 2a 2f 20 22 /* 107 */ "
be9a0 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Next",. /* 1
be9b0 30 38 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74 08 */ "IdxInsert
be9c0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 39 20 2a ",. /* 109 *
be9d0 2f 20 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 / "SeekGe",.
be9e0 20 2f 2a 20 31 31 30 20 2a 2f 20 22 49 6e 73 65 /* 110 */ "Inse
be9f0 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 rt",. /* 111
bea00 20 2a 2f 20 22 44 65 73 74 72 6f 79 22 2c 0a 20 */ "Destroy",.
bea10 20 20 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 52 /* 112 */ "R
bea20 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 eadCookie",.
bea30 20 2f 2a 20 31 31 33 20 2a 2f 20 22 4c 6f 61 64 /* 113 */ "Load
bea40 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 Analysis",.
bea50 2f 2a 20 31 31 34 20 2a 2f 20 22 45 78 70 6c 61 /* 114 */ "Expla
bea60 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 in",. /* 115
bea70 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64 6f 22 */ "OpenPseudo"
bea80 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f ,. /* 116 */
bea90 20 22 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 "OpenEphemeral"
beaa0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f ,. /* 117 */
beab0 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a "Null",. /*
beac0 20 31 31 38 20 2a 2f 20 22 4d 6f 76 65 22 2c 0a 118 */ "Move",.
bead0 20 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 22 /* 119 */ "
beae0 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Blob",. /* 1
beaf0 32 30 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a 20 */ "Rewind",.
beb00 20 20 20 20 20 2f 2a 20 31 32 31 20 2a 2f 20 22 /* 121 */ "
beb10 53 65 65 6b 47 74 22 2c 0a 20 20 20 20 20 2f 2a SeekGt",. /*
beb20 20 31 32 32 20 2a 2f 20 22 56 42 65 67 69 6e 22 122 */ "VBegin"
beb30 2c 0a 20 20 20 20 20 2f 2a 20 31 32 33 20 2a 2f ,. /* 123 */
beb40 20 22 56 55 70 64 61 74 65 22 2c 0a 20 20 20 20 "VUpdate",.
beb50 20 2f 2a 20 31 32 34 20 2a 2f 20 22 49 66 5a 65 /* 124 */ "IfZe
beb60 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 35 ro",. /* 125
beb70 20 2a 2f 20 22 56 43 72 65 61 74 65 22 2c 0a 20 */ "VCreate",.
beb80 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 46 /* 126 */ "F
beb90 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 ound",. /* 1
beba0 32 37 20 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 20 27 */ "IfPos",.
bebb0 20 20 20 20 2f 2a 20 31 32 38 20 2a 2f 20 22 4e /* 128 */ "N
bebc0 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a ullRow",. /*
bebd0 20 31 32 39 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 129 */ "Real",.
bebe0 20 20 20 20 20 2f 2a 20 31 33 30 20 2a 2f 20 22 /* 130 */ "
bebf0 4a 75 6d 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Jump",. /* 1
bec00 33 31 20 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 31 */ "Permutati
bec10 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 32 on",. /* 132
bec20 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 32 */ "NotUsed_132
bec30 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 2a ",. /* 133 *
bec40 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 33 22 2c / "NotUsed_133",
bec50 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f 20 . /* 134 */
bec60 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a 20 "NotUsed_134",.
bec70 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e /* 135 */ "N
bec80 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 20 otUsed_135",.
bec90 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74 /* 136 */ "Not
beca0 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 20 Used_136",.
becb0 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73 /* 137 */ "NotUs
becc0 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a ed_137",. /*
becd0 20 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64 138 */ "NotUsed
bece0 5f 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31 _138",. /* 1
becf0 33 39 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 39 */ "NotUsed_1
bed00 33 39 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 30 39",. /* 140
bed10 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34 30 */ "NotUsed_140
bed20 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 2a ",. /* 141 *
bed30 2f 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 20 / "ToText",.
bed40 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42 6c /* 142 */ "ToBl
bed50 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 33 ob",. /* 143
bed60 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c */ "ToNumeric",
bed70 0a 20 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f 20 . /* 144 */
bed80 22 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a "ToInt",. /*
bed90 20 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c 22 145 */ "ToReal"
beda0 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 ,. };. return
bedb0 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e azName[i];.}.#en
bedc0 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
bedd0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f **** End of opco
bede0 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a des.c **********
bedf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bee00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bee10 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
bee20 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
bee30 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_os2.c *******
bee40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bee50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bee60 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 ***/./*.** 2006
bee70 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 Feb 14.**.** The
bee80 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
bee90 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
beea0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
beeb0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
beec0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
beed0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
beee0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
beef0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
bef00 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
bef10 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
bef20 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
bef30 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
bef40 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
bef50 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
bef60 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
bef70 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
bef80 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
bef90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
befa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
befb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
befc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
befd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
befe0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
beff0 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 ins code that is
bf000 20 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53 2f specific to OS/
bf010 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2..**.** $Id: os
bf020 5f 6f 73 32 2e 63 2c 76 20 31 2e 36 33 20 32 30 _os2.c,v 1.63 20
bf030 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 08/12/10 19:26:2
bf040 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 4 drh Exp $.*/..
bf050 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f .#if SQLITE_OS_O
bf060 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 S2../*.** A Note
bf070 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c About Memory Al
bf080 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 location:.**.**
bf090 54 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73 This driver uses
bf0a0 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 malloc()/free()
bf0b0 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 directly rather
bf0c0 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f than going thro
bf0d0 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 ugh.** the SQLit
bf0e0 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 e-wrappers sqlit
bf0f0 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 e3_malloc()/sqli
bf100 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f te3_free(). Tho
bf110 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 se wrappers.** a
bf120 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 re designed for
bf130 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 use on embedded
bf140 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 systems where me
bf150 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 mory is scarce a
bf160 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 nd.** malloc fai
bf170 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 lures happen fre
bf180 71 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64 quently. OS/2 d
bf190 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c oes not typicall
bf1a0 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 y run on.** embe
bf1b0 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e dded systems, an
bf1c0 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 d when it does t
bf1d0 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f he developers no
bf1e0 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 rmally have bigg
bf1f0 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 er.** problems t
bf200 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 o worry about th
bf210 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f an running out o
bf220 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 f memory. So th
bf230 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 ere is not.** a
bf240 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 compelling need
bf250 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70 to use the wrapp
bf260 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 ers..**.** But t
bf270 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 here is a good r
bf280 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 eason to not use
bf290 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 the wrappers.
bf2a0 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a If we use the.**
bf2b0 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 wrappers then w
bf2c0 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c e will get simul
bf2d0 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 ated malloc() fa
bf2e0 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 ilures within th
bf2f0 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 is.** driver. A
bf300 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61 nd that causes a
bf310 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 ll kinds of prob
bf320 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 lems for our tes
bf330 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 ts. We.** could
bf340 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 enhance SQLite
bf350 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d to deal with sim
bf360 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 ulated malloc fa
bf370 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a ilures within.**
bf380 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 the OS driver,
bf390 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 but the code to
bf3a0 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 deal with those
bf3b0 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f failure would no
bf3c0 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 t.** be exercise
bf3d0 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 d on Linux (whic
bf3e0 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 h does not need
bf3f0 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 to malloc() in t
bf400 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e he driver).** an
bf410 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 d so we would ha
bf420 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 ve difficulty wr
bf430 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 iting coverage t
bf440 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a ests for that.**
bf450 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 code. Better t
bf460 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 o leave the code
bf470 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a out, we think..
bf480 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 **.** The point
bf490 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69 of this discussi
bf4a0 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 on is as follows
bf4b0 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 : When creating
bf4c0 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 a new.** OS lay
bf4d0 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 er for an embedd
bf4e0 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f ed system, if yo
bf4f0 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 u use this file
bf500 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a as an example,.*
bf510 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 * avoid the use
bf520 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 of malloc()/free
bf530 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 (). Those routi
bf540 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f nes work ok on O
bf550 53 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 S/2.** desktops
bf560 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 but not so well
bf570 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 in embedded syst
bf580 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d ems..*/../*.** M
bf590 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 acros used to de
bf5a0 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 termine whether
bf5b0 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 or not to use th
bf5c0 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 reads..*/.#if de
bf5d0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 fined(SQLITE_THR
bf5e0 45 41 44 53 41 46 45 29 20 26 26 20 53 51 4c 49 EADSAFE) && SQLI
bf5f0 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 TE_THREADSAFE.#
bf600 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
bf610 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 2_THREADS 1.#end
bf620 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 if../*.** Includ
bf630 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 e code that is c
bf640 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f ommon to all os_
bf650 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a *.c files.*/./**
bf660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
bf670 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 lude os_common.h
bf680 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
bf690 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a f os_os2.c *****
bf6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
bf6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
bf6c0 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f in file os_commo
bf6d0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
bf6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
bf700 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a ** 2004 May 22.*
bf710 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
bf720 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
bf730 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
bf740 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
bf750 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
bf760 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
bf770 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
bf780 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
bf790 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
bf7a0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
bf7b0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
bf7c0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
bf7d0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
bf7e0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
bf7f0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
bf800 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
bf810 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
bf820 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
bf830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf870 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
bf880 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 le contains macr
bf890 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 os and a little
bf8a0 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 bit of code that
bf8b0 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a is common to.**
bf8c0 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 all of the plat
bf8d0 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 form-specific fi
bf8e0 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 les (os_*.c) and
bf8f0 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e is #included in
bf900 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 to those.** file
bf910 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 s..**.** This fi
bf920 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e le should be #in
bf930 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 cluded by the os
bf940 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e _*.c files only.
bf950 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a It is not a.**
bf960 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 general purpose
bf970 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a header file..**
bf980 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d .** $Id: os_comm
bf990 6f 6e 2e 68 2c 76 20 31 2e 33 37 20 32 30 30 38 on.h,v 1.37 2008
bf9a0 2f 30 35 2f 32 39 20 32 30 3a 32 32 3a 33 37 20 /05/29 20:22:37
bf9b0 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 shane Exp $.*/.#
bf9c0 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f ifndef _OS_COMMO
bf9d0 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 N_H_.#define _OS
bf9e0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a _COMMON_H_../*.*
bf9f0 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 * At least two b
bfa00 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 ugs have slipped
bfa10 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 in because we c
bfa20 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 hanged the MEMOR
bfa30 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f Y_DEBUG.** macro
bfa40 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 to SQLITE_DEBUG
bfa50 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 and some older
bfa60 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e makefiles have n
bfa70 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a ot yet made the.
bfa80 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 ** switch. The
bfa90 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 following code s
bfaa0 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 hould catch this
bfab0 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 problem at comp
bfac0 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 ile-time..*/.#if
bfad0 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 def MEMORY_DEBUG
bfae0 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 .# error "The ME
bfaf0 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f MORY_DEBUG macro
bfb00 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 is obsolete. U
bfb10 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 se SQLITE_DEBUG
bfb20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 instead.".#endif
bfb30 0a 0a 0a 2f 2a 0a 20 2a 20 57 68 65 6e 20 74 65 .../*. * When te
bfb40 73 74 69 6e 67 2c 20 74 68 69 73 20 67 6c 6f 62 sting, this glob
bfb50 61 6c 20 76 61 72 69 61 62 6c 65 20 73 74 6f 72 al variable stor
bfb60 65 73 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 es the location
bfb70 6f 66 20 74 68 65 0a 20 2a 20 70 65 6e 64 69 6e of the. * pendin
bfb80 67 2d 62 79 74 65 20 69 6e 20 74 68 65 20 64 61 g-byte in the da
bfb90 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 2a 2f tabase file.. */
bfba0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
bfbb0 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 75 EST.SQLITE_API u
bfbc0 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 71 6c 69 nsigned int sqli
bfbd0 74 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74 65 te3_pending_byte
bfbe0 20 3d 20 30 78 34 30 30 30 30 30 30 30 3b 0a 23 = 0x40000000;.#
bfbf0 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
bfc00 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 LITE_DEBUG.SQLIT
bfc10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
bfc20 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30 lite3OSTrace = 0
bfc30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ;.#define OSTRAC
bfc40 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66 E1(X) if
bfc50 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
bfc60 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
bfc70 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 rintf(X).#define
bfc80 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 OSTRACE2(X,Y)
bfc90 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
bfca0 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 OSTrace ) sqlite
bfcb0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
bfcc0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
bfcd0 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 E3(X,Y,Z) if
bfce0 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
bfcf0 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
bfd00 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 rintf(X,Y,Z).#de
bfd10 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c fine OSTRACE4(X,
bfd20 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c Y,Z,A) if( sql
bfd30 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
bfd40 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
bfd50 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e (X,Y,Z,A).#defin
bfd60 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a e OSTRACE5(X,Y,Z
bfd70 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65 ,A,B) if( sqlite
bfd80 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
bfd90 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
bfda0 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 Y,Z,A,B).#define
bfdb0 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c OSTRACE6(X,Y,Z,
bfdc0 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 A,B,C) \. if(
bfdd0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 sqlite3OSTrace)
bfde0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
bfdf0 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a tf(X,Y,Z,A,B,C).
bfe00 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 #define OSTRACE7
bfe10 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 (X,Y,Z,A,B,C,D)
bfe20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 \. if(sqlite3
bfe30 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 OSTrace) sqlite3
bfe40 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
bfe50 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 Z,A,B,C,D).#else
bfe60 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
bfe70 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 1(X).#define OST
bfe80 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69 RACE2(X,Y).#defi
bfe90 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c ne OSTRACE3(X,Y,
bfea0 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Z).#define OSTRA
bfeb0 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 CE4(X,Y,Z,A).#de
bfec0 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c fine OSTRACE5(X,
bfed0 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 Y,Z,A,B).#define
bfee0 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c OSTRACE6(X,Y,Z,
bfef0 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f A,B,C).#define O
bff00 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c STRACE7(X,Y,Z,A,
bff10 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f B,C,D).#endif../
bff20 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 *.** Macros for
bff30 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 performance trac
bff40 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 ing. Normally t
bff50 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 urned off. Only
bff60 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 works.** on i48
bff70 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 6 hardware..*/.#
bff80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 ifdef SQLITE_PER
bff90 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a FORMANCE_TRACE..
bffa0 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 /* .** hwtime.h
bffb0 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 contains inline
bffc0 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 assembler code f
bffd0 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 or implementing
bffe0 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d .** high-perform
bfff0 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 ance timing rout
c0000 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a ines..*/./******
c0010 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
c0020 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 hwtime.h in the
c0030 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f middle of os_co
c0040 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
c0050 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
c0060 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
c0070 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a ile hwtime.h ***
c0080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c00a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
c00b0 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 008 May 27.**.**
c00c0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
c00d0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
c00e0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
c00f0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
c0100 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
c0110 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
c0120 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
c0130 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
c0140 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
c0150 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
c0160 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
c0170 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
c0180 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
c0190 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
c01a0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
c01b0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
c01c0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
c01d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c01e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c01f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
c0220 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
c0230 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 ontains inline a
c0240 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 sm code for retr
c0250 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 ieving "high-per
c0260 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 formance".** cou
c0270 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c nters for x86 cl
c0280 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 ass CPUs..**.**
c0290 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 $Id: hwtime.h,v
c02a0 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 1.3 2008/08/01 1
c02b0 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 4:33:15 shane Ex
c02c0 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f p $.*/.#ifndef _
c02d0 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e HWTIME_H_.#defin
c02e0 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a e _HWTIME_H_../*
c02f0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
c0300 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 g routine only w
c0310 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d orks on pentium-
c0320 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 class (or newer)
c0330 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 processors..**
c0340 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54 53 It uses the RDTS
c0350 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 C opcode to read
c0360 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 the cycle count
c0370 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 value out of th
c0380 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 e.** processor a
c0390 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 nd returns that
c03a0 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e value. This can
c03b0 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 be used for hig
c03c0 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 h-res.** profili
c03d0 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 ng..*/.#if (defi
c03e0 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c ned(__GNUC__) ||
c03f0 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 defined(_MSC_VE
c0400 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 R)) && \. (
c0410 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c defined(i386) ||
c0420 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f defined(__i386_
c0430 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d _) || defined(_M
c0440 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 _IX86)).. #if d
c0450 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
c0460 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
c0470 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
c0480 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
c0490 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 {. unsigned
c04a0 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 int lo, hi;.
c04b0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
c04c0 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a ile__ ("rdtsc" :
c04d0 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 "=a" (lo), "=d"
c04e0 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 (hi));. ret
c04f0 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 urn (sqlite_uint
c0500 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 64)hi << 32 | lo
c0510 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 ;. }.. #elif d
c0520 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 efined(_MSC_VER)
c0530 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e .. __declspec(n
c0540 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 aked) __inline s
c0550 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 qlite_uint64 __c
c0560 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 decl sqlite3Hwti
c0570 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f me(void){. _
c0580 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 _asm {. r
c0590 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 dtsc. ret
c05a0 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 ; return
c05b0 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 value at EDX:EAX
c05c0 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 . }. }.. #
c05d0 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 endif..#elif (de
c05e0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
c05f0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 && defined(__x86
c0600 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c _64__)).. __inl
c0610 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
c0620 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
c0630 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 e(void){. u
c0640 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c nsigned long val
c0650 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 ;. __asm__
c0660 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 __volatile__ ("r
c0670 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 dtsc" : "=A" (va
c0680 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 l));. retur
c0690 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c n val;. }. .#el
c06a0 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
c06b0 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 UC__) && defined
c06c0 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f (__ppc__)).. __
c06d0 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f inline__ sqlite_
c06e0 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
c06f0 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
c0700 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
c0710 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 long retval;.
c0720 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 unsigned long
c0730 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 junk;. __a
c0740 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f sm__ __volatile_
c0750 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 _ ("\n\.
c0760 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 1: mftbu
c0770 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 %1\n\.
c0780 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 mftb
c0790 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 %L0\n\.
c07a0 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 mftbu
c07b0 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 %0\n\.
c07c0 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 cmpw
c07d0 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 %0,%1\n\.
c07e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e bn
c07f0 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 e 1b".
c0800 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d : "=
c0810 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 r" (retval), "=r
c0820 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 " (junk));.
c0830 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a return retval;.
c0840 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 }..#else.. #e
c0850 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d rror Need implem
c0860 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 entation of sqli
c0870 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 te3Hwtime() for
c0880 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a your platform...
c0890 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d /*. ** To com
c08a0 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 pile without imp
c08b0 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 lementing sqlite
c08c0 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 3Hwtime() for yo
c08d0 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a ur platform,. *
c08e0 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 * you can remove
c08f0 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f the above #erro
c0900 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f r and use the fo
c0910 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 llowing. ** stu
c0920 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 b function. You
c0930 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e will lose timin
c0940 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 g support for ma
c0950 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 ny. ** of the d
c0960 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 ebugging and tes
c0970 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 ting utilities,
c0980 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 but it should at
c0990 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 . ** least comp
c09a0 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a ile and run.. *
c09b0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
c09c0 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 sqlite_uint64
c09d0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
c09e0 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 oid){ return ((s
c09f0 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b qlite_uint64)0);
c0a00 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 }..#endif..#end
c0a10 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
c0a20 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f HWTIME_H_) */../
c0a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
c0a40 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a nd of hwtime.h *
c0a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
c0a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
c0a90 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
c0aa0 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f we left off in o
c0ab0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a s_common.h *****
c0ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
c0ad0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 static sqlite_ui
c0ae0 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 nt64 g_start;.st
c0af0 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 atic sqlite_uint
c0b00 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 64 g_elapsed;.#d
c0b10 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 efine TIMER_STAR
c0b20 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d T g_start=
c0b30 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a sqlite3Hwtime().
c0b40 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e #define TIMER_EN
c0b50 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 D g_elap
c0b60 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d sed=sqlite3Hwtim
c0b70 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 e()-g_start.#def
c0b80 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 ine TIMER_ELAPSE
c0b90 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a D g_elapsed.
c0ba0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 #else.#define TI
c0bb0 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e MER_START.#defin
c0bc0 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 e TIMER_END.#def
c0bd0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 ine TIMER_ELAPSE
c0be0 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 D ((sqlite_u
c0bf0 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a int64)0).#endif.
c0c00 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d ./*.** If we com
c0c10 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 pile with the SQ
c0c20 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 LITE_TEST macro
c0c30 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f set, then the fo
c0c40 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a llowing block.**
c0c50 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 of code will gi
c0c60 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 ve us the abilit
c0c70 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 y to simulate a
c0c80 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 disk I/O error.
c0c90 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 This.** is used
c0ca0 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 for testing the
c0cb0 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f I/O recovery lo
c0cc0 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 gic..*/.#ifdef S
c0cd0 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
c0ce0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c0cf0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 3_io_error_hit =
c0d00 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
c0d10 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
c0d20 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a f I/O Errors */.
c0d30 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
c0d40 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c0d50 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 hardhit = 0;
c0d60 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
c0d70 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f non-benign erro
c0d80 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 rs */.SQLITE_API
c0d90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
c0da0 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 error_pending =
c0db0 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 0; /* Cou
c0dc0 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 nt down to first
c0dd0 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 I/O error */.SQ
c0de0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
c0df0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
c0e00 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 rsist = 0;
c0e10 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f /* True if I/O
c0e20 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 errors persist
c0e30 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
c0e40 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
c0e50 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 or_benign = 0;
c0e60 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
c0e70 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e f errors are ben
c0e80 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ign */.SQLITE_AP
c0e90 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 I int sqlite3_di
c0ea0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d skfull_pending =
c0eb0 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 0;.SQLITE_API i
c0ec0 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 nt sqlite3_diskf
c0ed0 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 ull = 0;.#define
c0ee0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
c0ef0 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 Benign(X) sqlite
c0f00 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 3_io_error_benig
c0f10 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 n=(X).#define Si
c0f20 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f mulateIOError(CO
c0f30 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 DE) \. if( (sq
c0f40 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
c0f50 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 ersist && sqlite
c0f60 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 3_io_error_hit)
c0f70 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 \. || sqli
c0f80 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
c0f90 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c ding-- == 1 ) \
c0fa0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b . {
c0fb0 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 local_ioerr();
c0fc0 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 CODE; }.static v
c0fd0 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 oid local_ioerr(
c0fe0 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 ){. IOTRACE(("I
c0ff0 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c OERR\n"));. sql
c1000 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 ite3_io_error_hi
c1010 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 t++;. if( !sqli
c1020 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e te3_io_error_ben
c1030 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f ign ) sqlite3_io
c1040 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b _error_hardhit++
c1050 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 ;.}.#define Simu
c1060 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
c1070 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 r(CODE) \. if(
c1080 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
c1090 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 l_pending ){ \.
c10a0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f if( sqlite3_
c10b0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
c10c0 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 == 1 ){ \.
c10d0 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b local_ioerr();
c10e0 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 \. sqlite
c10f0 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 3_diskfull = 1;
c1100 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 \. sqlite3
c1110 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 _io_error_hit =
c1120 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 1; \. CODE
c1130 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 ; \. }else{
c1140 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 \. sqlite3
c1150 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e _diskfull_pendin
c1160 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a g--; \. } \.
c1170 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 }.#else.#defi
c1180 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
c1190 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 orBenign(X).#def
c11a0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ine SimulateIOEr
c11b0 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 ror(A).#define S
c11c0 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 imulateDiskfullE
c11d0 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a rror(A).#endif..
c11e0 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 /*.** When testi
c11f0 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 ng, keep a count
c1200 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f of the number o
c1210 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f f open files..*/
c1220 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
c1230 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
c1240 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f nt sqlite3_open_
c1250 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a file_count = 0;.
c1260 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e #define OpenCoun
c1270 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f ter(X) sqlite3_
c1280 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b open_file_count+
c1290 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 =(X).#else.#defi
c12a0 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 ne OpenCounter(X
c12b0 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 ).#endif..#endif
c12c0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 /* !defined(_OS
c12d0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a _COMMON_H_) */..
c12e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
c12f0 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e End of os_common
c1300 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
c1310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
c1330 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
c1340 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
c1350 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
c1360 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_os2.c *******
c1370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
c1380 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 46 69 ./*.** The os2Fi
c1390 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 le structure is
c13a0 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 subclass of sqli
c13b0 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66 69 te3_file specifi
c13c0 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 0a 2a c for the OS/2.*
c13d0 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 6c 61 * protability la
c13e0 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 yer..*/.typedef
c13f0 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 6f struct os2File o
c1400 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 6f s2File;.struct o
c1410 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 s2File {. const
c1420 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
c1430 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f ods *pMethod; /
c1440 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 69 72 * Always the fir
c1450 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 48 46 st entry */. HF
c1460 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 ILE h;
c1470 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c /* Handl
c1480 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 e for accessing
c1490 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 the file */. ch
c14a0 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20 20 ar* pathToDel;
c14b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
c14c0 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 of file to delet
c14d0 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c 4c e on close, NULL
c14e0 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e 73 if not */. uns
c14f0 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 igned char lockt
c1500 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 20 6f ype; /* Type o
c1510 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 f lock currently
c1520 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 held on this fi
c1530 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e le */.};..#defin
c1540 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 31 e LOCK_TIMEOUT 1
c1550 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 75 6c 0L /* the defaul
c1560 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 6f 75 t locking timeou
c1570 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a t */../*********
c1580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c15c0 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 ****.** The next
c15d0 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e group of routin
c15e0 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 es implement the
c15f0 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 I/O methods spe
c1600 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 cified.** by the
c1610 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
c1620 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a ods object..****
c1630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
c1680 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e ** Close a file.
c1690 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
c16a0 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 65 33 s2Close( sqlite3
c16b0 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 41 _file *id ){. A
c16c0 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 PIRET rc = NO_ER
c16d0 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a ROR;. os2File *
c16e0 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 64 20 pFile;. if( id
c16f0 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f 73 32 && (pFile = (os2
c1700 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 20 29 File*)id) != 0 )
c1710 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 {. OSTRACE2(
c1720 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 "CLOSE %d\n", pF
c1730 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72 63 ile->h );. rc
c1740 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 46 69 = DosClose( pFi
c1750 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46 69 le->h );. pFi
c1760 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e le->locktype = N
c1770 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28 20 O_LOCK;. if(
c1780 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c pFile->pathToDel
c1790 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 != NULL ){.
c17a0 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 65 44 rc = DosForceD
c17b0 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 69 6c elete( (PSZ)pFil
c17c0 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a e->pathToDel );.
c17d0 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69 6c free( pFil
c17e0 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a e->pathToDel );.
c17f0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 pFile->pat
c1800 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 hToDel = NULL;.
c1810 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 30 3b }. id = 0;
c1820 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 . OpenCounter
c1830 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 ( -1 );. }.. r
c1840 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 eturn rc == NO_E
c1850 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b RROR ? SQLITE_OK
c1860 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b : SQLITE_IOERR;
c1870 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 .}../*.** Read d
c1880 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 ata from a file
c1890 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 into a buffer.
c18a0 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
c18b0 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 if all.** bytes
c18c0 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65 were read succe
c18d0 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 ssfully and SQLI
c18e0 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 TE_IOERR if anyt
c18f0 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f hing goes.** wro
c1900 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
c1910 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 71 6c t os2Read(. sql
c1920 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 ite3_file *id,
c1930 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c1940 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f File to read fro
c1950 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 m */. void *pBu
c1960 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f,
c1970 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
c1980 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 content into thi
c1990 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e s buffer */. in
c19a0 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 t amt,
c19b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c19c0 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
c19d0 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 to read */. sq
c19e0 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
c19f0 65 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a et /*
c1a00 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 Begin reading a
c1a10 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f t this offset */
c1a20 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 .){. ULONG file
c1a30 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 Location = 0L;.
c1a40 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f 73 ULONG got;. os
c1a50 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
c1a60 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 os2File*)id;. a
c1a70 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
c1a80 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
c1a90 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
c1aa0 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a 20 _IOERR_READ );.
c1ab0 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 41 44 OSTRACE3( "READ
c1ac0 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 %d lock=%d\n",
c1ad0 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d pFile->h, pFile-
c1ae0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 >locktype );. i
c1af0 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72 f( DosSetFilePtr
c1b00 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 (pFile->h, offse
c1b10 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 t, FILE_BEGIN, &
c1b20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d fileLocation) !=
c1b30 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
c1b40 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
c1b50 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 OERR;. }. if(
c1b60 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 2d 3e DosRead( pFile->
c1b70 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 h, pBuf, amt, &g
c1b80 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 ot ) != NO_ERROR
c1b90 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
c1ba0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 QLITE_IOERR_READ
c1bb0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 20 ;. }. if( got
c1bc0 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29 0a == (ULONG)amt ).
c1bd0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
c1be0 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a 20 E_OK;. else {.
c1bf0 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 6f 72 /* Unread por
c1c00 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 6e 70 tions of the inp
c1c10 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 ut buffer must b
c1c20 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f e zero-filled */
c1c30 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 . memset(&((c
c1c40 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c har*)pBuf)[got],
c1c50 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 0, amt-got);.
c1c60 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
c1c70 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
c1c80 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 ;. }.}../*.** W
c1c90 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61 rite data from a
c1ca0 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66 buffer into a f
c1cb0 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c ile. Return SQL
c1cc0 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
c1cd0 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 s.** or some oth
c1ce0 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e er error code on
c1cf0 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 failure..*/.sta
c1d00 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 74 65 tic int os2Write
c1d10 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
c1d20 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 *id,
c1d30 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 /* File to w
c1d40 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 rite into */. c
c1d50 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c onst void *pBuf,
c1d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c1d70 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 * The bytes to b
c1d80 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 e written */. i
c1d90 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 nt amt,
c1da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c1db0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
c1dc0 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 s to write */.
c1dd0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 sqlite3_int64 of
c1de0 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 fset
c1df0 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 /* Offset into t
c1e00 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69 6e he file to begin
c1e10 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29 writing at */.)
c1e20 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f {. ULONG fileLo
c1e30 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 41 cation = 0L;. A
c1e40 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 PIRET rc = NO_ER
c1e50 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72 6f ROR;. ULONG wro
c1e60 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 te;. os2File *p
c1e70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
c1e80 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 )id;. assert( i
c1e90 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 d!=0 );. Simula
c1ea0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
c1eb0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 n SQLITE_IOERR_W
c1ec0 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 6c 61 RITE );. Simula
c1ed0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 teDiskfullError(
c1ee0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 return SQLITE_F
c1ef0 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 ULL );. OSTRACE
c1f00 33 28 20 22 57 52 49 54 45 20 25 64 20 6c 6f 63 3( "WRITE %d loc
c1f10 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e k=%d\n", pFile->
c1f20 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 h, pFile->lockty
c1f30 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 pe );. if( DosS
c1f40 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d etFilePtr(pFile-
c1f50 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 >h, offset, FILE
c1f60 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 _BEGIN, &fileLoc
c1f70 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 ation) != NO_ERR
c1f80 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OR ){. return
c1f90 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 SQLITE_IOERR;.
c1fa0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 }. assert( amt
c1fb0 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 61 >0 );. while( a
c1fc0 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 20 20 mt > 0 &&.
c1fd0 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 72 69 ( rc = DosWri
c1fe0 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 28 50 te( pFile->h, (P
c1ff0 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 2c 20 VOID)pBuf, amt,
c2000 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 4e 4f &wrote ) ) == NO
c2010 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 20 20 _ERROR &&.
c2020 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 20 29 wrote > 0. )
c2030 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f {. amt -= wro
c2040 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 te;. pBuf = &
c2050 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 ((char*)pBuf)[wr
c2060 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 ote];. }.. ret
c2070 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f 5f 45 urn ( rc != NO_E
c2080 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 28 69 RROR || amt > (i
c2090 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 51 4c nt)wrote ) ? SQL
c20a0 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 54 ITE_FULL : SQLIT
c20b0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
c20c0 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 runcate an open
c20d0 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 file to a specif
c20e0 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 ied size.*/.stat
c20f0 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e 63 61 ic int os2Trunca
c2100 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 te( sqlite3_file
c2110 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 20 *id, i64 nByte
c2120 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d ){. APIRET rc =
c2130 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 NO_ERROR;. os2
c2140 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f File *pFile = (o
c2150 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 s2File*)id;. OS
c2160 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 41 54 TRACE3( "TRUNCAT
c2170 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 E %d %lld\n", pF
c2180 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b ile->h, nByte );
c2190 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
c21a0 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
c21b0 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 E_IOERR_TRUNCATE
c21c0 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 65 );. rc = DosSe
c21d0 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 6c 65 tFileSize( pFile
c21e0 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 ->h, nByte );.
c21f0 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f return rc == NO_
c2200 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f ERROR ? SQLITE_O
c2210 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 K : SQLITE_IOERR
c2220 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23 69 _TRUNCATE;.}..#i
c2230 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
c2240 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 ./*.** Count the
c2250 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 number of fulls
c2260 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 yncs and normal
c2270 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 syncs. This is
c2280 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 used to test.**
c2290 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 that syncs and f
c22a0 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 ullsyncs are occ
c22b0 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 uring at the rig
c22c0 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c ht times..*/.SQL
c22d0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c22e0 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d te3_sync_count =
c22f0 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 0;.SQLITE_API i
c2300 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 nt sqlite3_fulls
c2310 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 ync_count = 0;.#
c2320 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b endif../*.** Mak
c2330 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 e sure all write
c2340 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 s to a particula
c2350 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 r file are commi
c2360 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f tted to disk..*/
c2370 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 .static int os2S
c2380 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 69 6c ync( sqlite3_fil
c2390 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 e *id, int flags
c23a0 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 ){. os2File *p
c23b0 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
c23c0 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 )id;. OSTRACE3(
c23d0 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 "SYNC %d lock=%
c23e0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
c23f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
c2400 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
c2410 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61 67 _TEST. if( flag
c2420 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f s & SQLITE_SYNC_
c2430 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69 74 FULL){. sqlit
c2440 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e e3_fullsync_coun
c2450 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 t++;. }. sqlit
c2460 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b e3_sync_count++;
c2470 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 .#endif. /* If
c2480 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 we compiled with
c2490 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 the SQLITE_NO_S
c24a0 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 YNC flag, then s
c24b0 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a yncing is a. **
c24c0 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 no-op. */.#ifd
c24d0 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e ef SQLITE_NO_SYN
c24e0 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d C. UNUSED_PARAM
c24f0 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 20 72 ETER(pFile);. r
c2500 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
c2510 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 .#else. return
c2520 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 28 20 DosResetBuffer(
c2530 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f pFile->h ) == NO
c2540 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f _ERROR ? SQLITE_
c2550 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
c2560 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a R;.#endif.}../*.
c2570 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 ** Determine the
c2580 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 current size of
c2590 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 a file in bytes
c25a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
c25b0 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 6c 69 s2FileSize( sqli
c25c0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 te3_file *id, sq
c25d0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 lite3_int64 *pSi
c25e0 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 ze ){. APIRET r
c25f0 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
c2600 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 FILESTATUS3 fsts
c2610 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 3FileInfo;. mem
c2620 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 49 6e set(&fsts3FileIn
c2630 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 fo, 0, sizeof(fs
c2640 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 ts3FileInfo));.
c2650 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
c2660 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
c2670 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
c2680 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 29 TE_IOERR_FSTAT )
c2690 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 ;. rc = DosQuer
c26a0 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32 yFileInfo( ((os2
c26b0 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49 File*)id)->h, FI
c26c0 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 73 74 L_STANDARD, &fst
c26d0 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 s3FileInfo, size
c26e0 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20 of(FILESTATUS3)
c26f0 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 4e );. if( rc == N
c2700 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2a O_ERROR ){. *
c2710 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 69 6c pSize = fsts3Fil
c2720 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 eInfo.cbFile;.
c2730 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
c2740 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
c2750 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
c2760 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a OERR_FSTAT;. }.
c2770 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 }../*.** Acquire
c2780 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a a reader lock..
c2790 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
c27a0 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 tReadLock( os2Fi
c27b0 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20 46 le *pFile ){. F
c27c0 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 ILELOCK LockAre
c27d0 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 a,. U
c27e0 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 nlockArea;. API
c27f0 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 RET res;. memse
c2800 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 t(&LockArea, 0,
c2810 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 sizeof(LockArea)
c2820 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c );. memset(&Unl
c2830 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 ockArea, 0, size
c2840 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b of(UnlockArea));
c2850 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 . LockArea.lOff
c2860 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 set = SHARED_FIR
c2870 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ST;. LockArea.l
c2880 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 Range = SHARED_S
c2890 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 IZE;. UnlockAre
c28a0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a a.lOffset = 0L;.
c28b0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
c28c0 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73 20 nge = 0L;. res
c28d0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
c28e0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
c28f0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
c2900 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
c2910 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 T, 1L );. OSTRA
c2920 43 45 33 28 20 22 47 45 54 52 45 41 44 4c 4f 43 CE3( "GETREADLOC
c2930 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 K %d res=%d\n",
c2940 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
c2950 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d . return res;.}
c2960 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 ../*.** Undo a r
c2970 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 eadlock.*/.stati
c2980 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 c int unlockRead
c2990 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69 Lock( os2File *i
c29a0 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 d ){. FILELOCK
c29b0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 LockArea,.
c29c0 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 UnlockAre
c29d0 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b a;. APIRET res;
c29e0 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 . memset(&LockA
c29f0 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c rea, 0, sizeof(L
c2a00 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d ockArea));. mem
c2a10 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c set(&UnlockArea,
c2a20 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 0, sizeof(Unloc
c2a30 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 kArea));. LockA
c2a40 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
c2a50 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 ;. LockArea.lRa
c2a60 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f nge = 0L;. Unlo
c2a70 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c2a80 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 SHARED_FIRST;.
c2a90 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e UnlockArea.lRan
c2aa0 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 ge = SHARED_SIZE
c2ab0 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 ;. res = DosSet
c2ac0 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 FileLocks( id->h
c2ad0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 , &UnlockArea, &
c2ae0 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 LockArea, LOCK_T
c2af0 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 IMEOUT, 1L );.
c2b00 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 OSTRACE3( "UNLOC
c2b10 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65 20 K-READLOCK file
c2b20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25 64 handle=%d res=%d
c2b30 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65 73 ?\n", id->h, res
c2b40 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 );. return res
c2b50 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 ;.}../*.** Lock
c2b60 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 the file with th
c2b70 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 e lock specified
c2b80 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f by parameter lo
c2b90 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 cktype - one.**
c2ba0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
c2bb0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 :.**.** (1)
c2bc0 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 SHARED_LOCK.**
c2bd0 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f (2) RESERVED_
c2be0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 LOCK.** (3)
c2bf0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 PENDING_LOCK.**
c2c00 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 (4) EXCLUSIV
c2c10 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d E_LOCK.**.** Som
c2c20 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 etimes when requ
c2c30 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 esting one lock
c2c40 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 state, additiona
c2c50 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a l lock states.**
c2c60 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e are inserted in
c2c70 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c between. The l
c2c80 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 ocking might fai
c2c90 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 l on one of the
c2ca0 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 later.** transit
c2cb0 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 ions leaving the
c2cc0 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 lock state diff
c2cd0 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 erent from what
c2ce0 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a it started but.*
c2cf0 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 * still short of
c2d00 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 its goal. The
c2d10 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 following chart
c2d20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 shows the allowe
c2d30 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 d.** transitions
c2d40 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 and the inserte
c2d50 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 d intermediate s
c2d60 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 tates:.**.**
c2d70 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 UNLOCKED -> SHAR
c2d80 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 ED.** SHARED
c2d90 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 -> RESERVED.**
c2da0 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e SHARED -> (PEN
c2db0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 DING) -> EXCLUSI
c2dc0 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 VE.** RESERVE
c2dd0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d D -> (PENDING) -
c2de0 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 > EXCLUSIVE.**
c2df0 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 PENDING -> EXC
c2e00 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 LUSIVE.**.** Thi
c2e10 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f s routine will o
c2e20 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c nly increase a l
c2e30 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e 6c ock. The os2Unl
c2e40 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a ock() routine.**
c2e50 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b erases all lock
c2e60 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 s at once and re
c2e70 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 turns us immedia
c2e80 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 tely to locking
c2e90 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 level 0..** It i
c2ea0 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 s not possible t
c2eb0 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b o lower the lock
c2ec0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 ing level one st
c2ed0 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 ep at a time. Y
c2ee0 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 ou.** must go st
c2ef0 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e raight to lockin
c2f00 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 g level 0..*/.st
c2f10 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b atic int os2Lock
c2f20 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ( sqlite3_file *
c2f30 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
c2f40 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ){. int rc = S
c2f50 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
c2f60 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 /* Return code f
c2f70 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20 rom subroutines
c2f80 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 73 20 */. APIRET res
c2f90 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f = NO_ERROR; /
c2fa0 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f * Result of an O
c2fb0 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f S/2 lock call */
c2fc0 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 . int newLockty
c2fd0 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74 pe; /* Set
c2fe0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c2ff0 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 62 to this value b
c3000 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f efore exiting */
c3010 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e . int gotPendin
c3020 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 gLock = 0;/* Tru
c3030 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65 64 e if we acquired
c3040 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 a PENDING lock
c3050 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46 this time */. F
c3060 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 ILELOCK LockAre
c3070 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 a,. U
c3080 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32 nlockArea;. os2
c3090 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f File *pFile = (o
c30a0 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 s2File*)id;. me
c30b0 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 mset(&LockArea,
c30c0 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 0, sizeof(LockAr
c30d0 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 ea));. memset(&
c30e0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 UnlockArea, 0, s
c30f0 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 izeof(UnlockArea
c3100 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 ));. assert( pF
c3110 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 ile!=0 );. OSTR
c3120 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 25 ACE4( "LOCK %d %
c3130 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 d was %d\n", pFi
c3140 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c le->h, locktype,
c3150 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c3160 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
c3170 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 re is already a
c3180 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 lock of this typ
c3190 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 e or more restri
c31a0 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a ctive on the. *
c31b0 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f * os2File, do no
c31c0 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 thing. Don't use
c31d0 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 the end_lock: e
c31e0 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a xit path, as. *
c31f0 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f * sqlite3_mutex_
c3200 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 20 62 enter() hasn't b
c3210 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a een called yet..
c3220 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 */. if( pFile
c3230 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b ->locktype>=lock
c3240 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 type ){. OSTR
c3250 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 25 ACE3( "LOCK %d %
c3260 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 d ok (already he
c3270 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 ld)\n", pFile->h
c3280 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 , locktype );.
c3290 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
c32a0 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 OK;. }.. /* Ma
c32b0 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b ke sure the lock
c32c0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 ing sequence is
c32d0 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 correct. */. a
c32e0 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f ssert( pFile->lo
c32f0 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 cktype!=NO_LOCK
c3300 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 || locktype==SHA
c3310 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 RED_LOCK );. as
c3320 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d sert( locktype!=
c3330 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a PENDING_LOCK );.
c3340 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
c3350 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe!=RESERVED_LOC
c3360 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b K || pFile->lock
c3370 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
c3380 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 K );.. /* Lock
c3390 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b the PENDING_LOCK
c33a0 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 byte if we need
c33b0 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 to acquire a PE
c33c0 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 NDING lock or.
c33d0 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b ** a SHARED lock
c33e0 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63 71 . If we are acq
c33f0 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 uiring a SHARED
c3400 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 lock, the acquis
c3410 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 ition of. ** th
c3420 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 e PENDING_LOCK b
c3430 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 yte is temporary
c3440 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b .. */. newLock
c3450 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f type = pFile->lo
c3460 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 cktype;. if( pF
c3470 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e ile->locktype==N
c3480 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20 O_LOCK. ||
c3490 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 (locktype==EXCLU
c34a0 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 SIVE_LOCK && pFi
c34b0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 le->locktype==RE
c34c0 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 SERVED_LOCK). )
c34d0 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c {. LockArea.l
c34e0 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 Offset = PENDING
c34f0 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 _BYTE;. LockA
c3500 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b rea.lRange = 1L;
c3510 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e . UnlockArea.
c3520 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 lOffset = 0L;.
c3530 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
c3540 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f nge = 0L;.. /
c3550 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 68 * wait longer th
c3560 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 an LOCK_TIMEOUT
c3570 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 65 here not to have
c3580 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c 65 to try multiple
c3590 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65 times */. re
c35a0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f s = DosSetFileLo
c35b0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 cks( pFile->h, &
c35c0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
c35d0 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20 kArea, 100L, 0L
c35e0 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d );. if( res =
c35f0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
c3600 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f gotPendingLo
c3610 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53 ck = 1;. OS
c3620 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 TRACE3( "LOCK %d
c3630 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f pending lock bo
c3640 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 73 3d olean set. res=
c3650 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
c3660 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20 res );. }.
c3670 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 }.. /* Acquire
c3680 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 a shared lock.
c3690 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
c36a0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 e==SHARED_LOCK &
c36b0 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f & res == NO_ERRO
c36c0 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 R ){. assert(
c36d0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c36e0 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 ==NO_LOCK );.
c36f0 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f res = getReadLo
c3700 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 ck(pFile);. i
c3710 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 f( res == NO_ERR
c3720 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c OR ){. newL
c3730 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 ocktype = SHARED
c3740 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 _LOCK;. }.
c3750 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b OSTRACE3( "LOCK
c3760 20 25 64 20 61 63 71 75 69 72 65 20 73 68 61 72 %d acquire shar
c3770 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c ed lock. res=%d\
c3780 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 n", pFile->h, re
c3790 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 s );. }.. /* A
c37a0 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45 cquire a RESERVE
c37b0 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 D lock. */. if
c37c0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 ( locktype==RESE
c37d0 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 RVED_LOCK && res
c37e0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
c37f0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
c3800 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 e->locktype==SHA
c3810 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 RED_LOCK );.
c3820 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 LockArea.lOffset
c3830 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 = RESERVED_BYTE
c3840 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ;. LockArea.l
c3850 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 Range = 1L;.
c3860 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 UnlockArea.lOffs
c3870 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c et = 0L;. Unl
c3880 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c3890 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 0L;. res = D
c38a0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 osSetFileLocks(
c38b0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 pFile->h, &Unloc
c38c0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 kArea, &LockArea
c38d0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 , LOCK_TIMEOUT,
c38e0 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 0L );. if( re
c38f0 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b s == NO_ERROR ){
c3900 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 . newLockty
c3910 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f pe = RESERVED_LO
c3920 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 CK;. }. OS
c3930 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 TRACE3( "LOCK %d
c3940 20 61 63 71 75 69 72 65 20 72 65 73 65 72 76 65 acquire reserve
c3950 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e d lock. res=%d\n
c3960 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 ", pFile->h, res
c3970 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 );. }.. /* Ac
c3980 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 quire a PENDING
c3990 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
c39a0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 locktype==EXCLUS
c39b0 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 IVE_LOCK && res
c39c0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 == NO_ERROR ){.
c39d0 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d newLocktype =
c39e0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 PENDING_LOCK;.
c39f0 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 gotPendingLoc
c3a00 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 41 k = 0;. OSTRA
c3a10 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 CE2( "LOCK %d ac
c3a20 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f quire pending lo
c3a30 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b ck. pending lock
c3a40 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c boolean unset.\
c3a50 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a n", pFile->h );.
c3a60 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 }.. /* Acquir
c3a70 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c e an EXCLUSIVE l
c3a80 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c ock. */. if( l
c3a90 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
c3aa0 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d VE_LOCK && res =
c3ab0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
c3ac0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
c3ad0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 >locktype>=SHARE
c3ae0 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 D_LOCK );. re
c3af0 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f s = unlockReadLo
c3b00 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f ck(pFile);. O
c3b10 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 61 64 STRACE2( "unread
c3b20 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 lock = %d\n", re
c3b30 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 s );. LockAre
c3b40 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 a.lOffset = SHAR
c3b50 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f ED_FIRST;. Lo
c3b60 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c3b70 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 SHARED_SIZE;.
c3b80 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 UnlockArea.lOff
c3b90 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e set = 0L;. Un
c3ba0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
c3bb0 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 = 0L;. res =
c3bc0 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c3bd0 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c3be0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c3bf0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
c3c00 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 0L );. if( r
c3c10 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 es == NO_ERROR )
c3c20 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 {. newLockt
c3c30 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f ype = EXCLUSIVE_
c3c40 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b LOCK;. }else{
c3c50 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 . OSTRACE2(
c3c60 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64 "OS/2 error-cod
c3c70 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 e = %d\n", res )
c3c80 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 4c ;. getReadL
c3c90 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 ock(pFile);.
c3ca0 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 }. OSTRACE3(
c3cb0 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 "LOCK %d acquire
c3cc0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e exclusive lock.
c3cd0 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 res=%d\n", pFi
c3ce0 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 le->h, res );.
c3cf0 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 }.. /* If we ar
c3d00 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 e holding a PEND
c3d10 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 ING lock that ou
c3d20 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 ght to be releas
c3d30 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 ed, then. ** re
c3d40 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 lease it now..
c3d50 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 */. if( gotPend
c3d60 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 ingLock && lockt
c3d70 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
c3d80 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20 ){. int r;.
c3d90 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 LockArea.lOff
c3da0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f set = 0L;. Lo
c3db0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c3dc0 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 0L;. UnlockAr
c3dd0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e ea.lOffset = PEN
c3de0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 DING_BYTE;. U
c3df0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
c3e00 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44 = 1L;. r = D
c3e10 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 osSetFileLocks(
c3e20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 pFile->h, &Unloc
c3e30 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 kArea, &LockArea
c3e40 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 , LOCK_TIMEOUT,
c3e50 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 0L );. OSTRAC
c3e60 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c E3( "LOCK %d unl
c3e70 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69 ocking pending/i
c3e80 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e s shared. r=%d\n
c3e90 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 ", pFile->h, r )
c3ea0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 ;. }.. /* Upda
c3eb0 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 te the state of
c3ec0 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c the lock has hel
c3ed0 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 d in the file de
c3ee0 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 scriptor then.
c3ef0 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 ** return the ap
c3f00 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 propriate result
c3f10 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 code.. */. if
c3f20 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f ( res == NO_ERRO
c3f30 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 R ){. rc = SQ
c3f40 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
c3f50 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 20 {. OSTRACE4(
c3f60 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 "LOCK FAILED %d
c3f70 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 trying for %d bu
c3f80 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 t got %d\n", pFi
c3f90 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
c3fa0 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e locktype, n
c3fb0 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 ewLocktype );.
c3fc0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 rc = SQLITE_BU
c3fd0 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d SY;. }. pFile-
c3fe0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c >locktype = newL
c3ff0 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 ocktype;. OSTRA
c4000 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f CE3( "LOCK %d no
c4010 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e w %d\n", pFile->
c4020 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 h, pFile->lockty
c4030 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 pe );. return r
c4040 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
c4050 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 routine checks
c4060 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 if there is a RE
c4070 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 SERVED lock held
c4080 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 on the specifie
c4090 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 d.** file by thi
c40a0 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 s or any other p
c40b0 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 rocess. If such
c40c0 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 a lock is held,
c40d0 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 return.** non-ze
c40e0 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65 ro, otherwise ze
c40f0 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ro..*/.static in
c4100 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76 t os2CheckReserv
c4110 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f edLock( sqlite3_
c4120 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
c4130 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d Out ){. int r =
c4140 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 0;. os2File *p
c4150 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
c4160 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 70 )id;. assert( p
c4170 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 File!=0 );. if(
c4180 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c4190 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 >=RESERVED_LOCK
c41a0 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 20 ){. r = 1;.
c41b0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 OSTRACE3( "TES
c41c0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
c41d0 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c (local)\n", pFil
c41e0 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c e->h, r );. }el
c41f0 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b se{. FILELOCK
c4200 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 LockArea,.
c4210 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b Unlock
c4220 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 45 54 Area;. APIRET
c4230 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a rc = NO_ERROR;.
c4240 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b memset(&Lock
c4250 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
c4260 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 LockArea));.
c4270 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 memset(&UnlockAr
c4280 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e ea, 0, sizeof(Un
c4290 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 lockArea));.
c42a0 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 LockArea.lOffset
c42b0 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 = RESERVED_BYTE
c42c0 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ;. LockArea.l
c42d0 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 Range = 1L;.
c42e0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 UnlockArea.lOffs
c42f0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c et = 0L;. Unl
c4300 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c4310 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 0L;. rc = Do
c4320 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 sSetFileLocks( p
c4330 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b File->h, &Unlock
c4340 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c Area, &LockArea,
c4350 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 LOCK_TIMEOUT, 0
c4360 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 L );. OSTRACE
c4370 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 3( "TEST WR-LOCK
c4380 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 65 %d lock reserve
c4390 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c d byte rc=%d\n",
c43a0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b pFile->h, rc );
c43b0 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e . if( rc == N
c43c0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 O_ERROR ){.
c43d0 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 4e 4f APIRET rcu = NO
c43e0 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72 _ERROR; /* retur
c43f0 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63 n code for unloc
c4400 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f king */. Lo
c4410 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c4420 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41 0L;. LockA
c4430 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b rea.lRange = 0L;
c4440 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 . UnlockAre
c4450 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 a.lOffset = RESE
c4460 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 RVED_BYTE;.
c4470 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e UnlockArea.lRan
c4480 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72 ge = 1L;. r
c4490 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c cu = DosSetFileL
c44a0 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 ocks( pFile->h,
c44b0 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f &UnlockArea, &Lo
c44c0 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d ckArea, LOCK_TIM
c44d0 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 EOUT, 0L );.
c44e0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 OSTRACE3( "TES
c44f0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c T WR-LOCK %d unl
c4500 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 79 74 ock reserved byt
c4510 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 e r=%d\n", pFile
c4520 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 20 20 ->h, rcu );.
c4530 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 20 3d }. r = !(rc =
c4540 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 = NO_ERROR);.
c4550 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 OSTRACE3( "TEST
c4560 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 WR-LOCK %d %d (
c4570 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c remote)\n", pFil
c4580 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 e->h, r );. }.
c4590 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72 65 *pOut = r;. re
c45a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c45b0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 }../*.** Lower t
c45c0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
c45d0 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 on file descrip
c45e0 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 tor id to lockty
c45f0 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a pe. locktype.**
c4600 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
c4610 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 NO_LOCK or SHARE
c4620 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 D_LOCK..**.** If
c4630 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
c4640 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 el of the file d
c4650 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 escriptor is alr
c4660 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 eady at or below
c4670 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 .** the requeste
c4680 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c d locking level,
c4690 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
c46a0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
c46b0 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 It is not possib
c46c0 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 le for this rout
c46d0 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 ine to fail if t
c46e0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
c46f0 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b nt.** is NO_LOCK
c4700 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 . If the second
c4710 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 argument is SHA
c4720 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 RED_LOCK then th
c4730 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 is routine.** mi
c4740 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ght return SQLIT
c4750 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 E_IOERR;.*/.stat
c4760 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b ic int os2Unlock
c4770 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ( sqlite3_file *
c4780 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
c4790 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a ){. int type;.
c47a0 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 os2File *pFile
c47b0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b = (os2File*)id;
c47c0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 53 . APIRET rc = S
c47d0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 52 QLITE_OK;. APIR
c47e0 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f ET res = NO_ERRO
c47f0 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c R;. FILELOCK L
c4800 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 ockArea,.
c4810 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b UnlockArea;
c4820 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 . memset(&LockA
c4830 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c rea, 0, sizeof(L
c4840 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d ockArea));. mem
c4850 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c set(&UnlockArea,
c4860 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 0, sizeof(Unloc
c4870 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65 72 kArea));. asser
c4880 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 t( pFile!=0 );.
c4890 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
c48a0 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e<=SHARED_LOCK )
c48b0 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 55 ;. OSTRACE4( "U
c48c0 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 NLOCK %d to %d w
c48d0 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d as %d\n", pFile-
c48e0 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 >h, locktype, pF
c48f0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b ile->locktype );
c4900 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d . type = pFile-
c4910 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 >locktype;. if(
c4920 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 type>=EXCLUSIVE
c4930 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 _LOCK ){. Loc
c4940 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
c4950 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 0L;. LockArea
c4960 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 .lRange = 0L;.
c4970 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
c4980 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 fset = SHARED_FI
c4990 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 RST;. UnlockA
c49a0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 rea.lRange = SHA
c49b0 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 65 RED_SIZE;. re
c49c0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f s = DosSetFileLo
c49d0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 cks( pFile->h, &
c49e0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
c49f0 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 kArea, LOCK_TIME
c4a00 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f OUT, 0L );. O
c4a10 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b STRACE3( "UNLOCK
c4a20 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 6c 6f %d exclusive lo
c4a30 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 ck res=%d\n", pF
c4a40 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 ile->h, res );.
c4a50 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d if( locktype=
c4a60 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 =SHARED_LOCK &&
c4a70 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c getReadLock(pFil
c4a80 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 e) != NO_ERROR )
c4a90 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 {. /* This
c4aa0 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 should never hap
c4ab0 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 pen. We should
c4ac0 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 always be able t
c4ad0 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 o. ** reacq
c4ae0 75 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f uire the read lo
c4af0 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 ck */. OSTR
c4b00 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE3( "UNLOCK %d
c4b10 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 4c 6f to %d getReadLo
c4b20 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c 20 ck() failed\n",
c4b30 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 pFile->h, lockty
c4b40 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d pe );. rc =
c4b50 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
c4b60 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a LOCK;. }. }.
c4b70 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 if( type>=RESE
c4b80 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 RVED_LOCK ){.
c4b90 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
c4ba0 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b t = 0L;. Lock
c4bb0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
c4bc0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
c4bd0 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 .lOffset = RESER
c4be0 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e VED_BYTE;. Un
c4bf0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
c4c00 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 = 1L;. res =
c4c10 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c4c20 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c4c30 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c4c40 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
c4c50 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 0L );. OSTRA
c4c60 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 CE3( "UNLOCK %d
c4c70 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 64 5c reserved res=%d\
c4c80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 n", pFile->h, re
c4c90 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c s );. }. if( l
c4ca0 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b ocktype==NO_LOCK
c4cb0 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44 && type>=SHARED
c4cc0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 _LOCK ){. res
c4cd0 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 = unlockReadLoc
c4ce0 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 k(pFile);. OS
c4cf0 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b 20 TRACE5( "UNLOCK
c4d00 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 25 64 %d is %d want %d
c4d10 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c res=%d\n", pFil
c4d20 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63 6b e->h, type, lock
c4d30 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 20 7d type, res );. }
c4d40 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e . if( type>=PEN
c4d50 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 DING_LOCK ){.
c4d60 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
c4d70 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b t = 0L;. Lock
c4d80 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
c4d90 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
c4da0 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 .lOffset = PENDI
c4db0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c NG_BYTE;. Unl
c4dc0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c4dd0 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 1L;. res = D
c4de0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 osSetFileLocks(
c4df0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 pFile->h, &Unloc
c4e00 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 kArea, &LockArea
c4e10 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 , LOCK_TIMEOUT,
c4e20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 0L );. OSTRAC
c4e30 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70 E3( "UNLOCK %d p
c4e40 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22 ending res=%d\n"
c4e50 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 , pFile->h, res
c4e60 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e );. }. pFile->
c4e70 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
c4e80 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 ype;. OSTRACE3(
c4e90 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 "UNLOCK %d now
c4ea0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
c4eb0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c4ec0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
c4ed0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f .}../*.** Contro
c4ee0 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 l and query of t
c4ef0 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e he open file han
c4f00 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 dle..*/.static i
c4f10 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f nt os2FileContro
c4f20 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a l(sqlite3_file *
c4f30 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 id, int op, void
c4f40 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 *pArg){. switc
c4f50 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 h( op ){. cas
c4f60 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c e SQLITE_FCNTL_L
c4f70 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 OCKSTATE: {.
c4f80 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 *(int*)pArg =
c4f90 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e ((os2File*)id)->
c4fa0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 locktype;.
c4fb0 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e 54 4c OSTRACE3( "FCNTL
c4fc0 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 6c 6f _LOCKSTATE %d lo
c4fd0 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46 ck=%d\n", ((os2F
c4fe0 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f ile*)id)->h, ((o
c4ff0 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 s2File*)id)->loc
c5000 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 ktype );. r
c5010 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
c5020 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
c5030 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
c5040 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
c5050 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a n the sector siz
c5060 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 e in bytes of th
c5070 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f e underlying blo
c5080 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a ck device for.**
c5090 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 the specified f
c50a0 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d ile. This is alm
c50b0 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 ost always 512 b
c50c0 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 ytes, but may be
c50d0 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 .** larger for s
c50e0 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a ome devices..**.
c50f0 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 ** SQLite code a
c5100 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 ssumes this func
c5110 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c tion cannot fail
c5120 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 . It also assume
c5130 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f s that.** if two
c5140 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 files are creat
c5150 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 ed in the same f
c5160 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 ile-system direc
c5170 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 tory (i.e..** a
c5180 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 database and its
c5190 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 journal file) t
c51a0 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 hat the sector s
c51b0 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a ize will be the.
c51c0 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 ** same for both
c51d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c51e0 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 73 71 os2SectorSize(sq
c51f0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b lite3_file *id){
c5200 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
c5210 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
c5220 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 SIZE;.}../*.** R
c5230 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f eturn a vector o
c5240 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74 f device charact
c5250 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 eristics..*/.sta
c5260 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 69 63 tic int os2Devic
c5270 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
c5280 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
c5290 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a d){. return 0;.
c52a0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 }.../*.** Charac
c52b0 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 73 69 ter set conversi
c52c0 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20 on objects used
c52d0 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 6f by conversion ro
c52e0 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 utines..*/.stati
c52f0 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 c UconvObject uc
c5300 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20 Utf8 = NULL; /*
c5310 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20 convert between
c5320 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d 32 20 UTF-8 and UCS-2
c5330 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 4f */.static UconvO
c5340 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 4e 55 bject uclCp = NU
c5350 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74 20 LL; /* convert
c5360 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63 6f between local co
c5370 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 2d 32 depage and UCS-2
c5380 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 */../*.** Helpe
c5390 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e r function to in
c53a0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e itialize the con
c53b0 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 version objects
c53c0 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d from and to UTF-
c53d0 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 8..*/.static voi
c53e0 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 d initUconvObjec
c53f0 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 ts( void ){. if
c5400 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 ( UniCreateUconv
c5410 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c 20 26 Object( UTF_8, &
c5420 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c 53 5f ucUtf8 ) != ULS_
c5430 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 63 SUCCESS ). uc
c5440 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 Utf8 = NULL;. i
c5450 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f f ( UniCreateUco
c5460 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 68 nvObject( (UniCh
c5470 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73 ar *)L"@path=yes
c5480 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d 20 55 ", &uclCp ) != U
c5490 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 LS_SUCCESS ).
c54a0 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d uclCp = NULL;.}
c54b0 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 ../*.** Helper f
c54c0 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 65 20 unction to free
c54d0 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f the conversion o
c54e0 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 20 bjects from and
c54f0 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 to UTF-8..*/.sta
c5500 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 63 6f tic void freeUco
c5510 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 20 nvObjects( void
c5520 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 66 38 ){. if ( ucUtf8
c5530 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 ). UniFreeUc
c5540 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 74 66 onvObject( ucUtf
c5550 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 6c 43 8 );. if ( uclC
c5560 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 p ). UniFreeU
c5570 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c 43 convObject( uclC
c5580 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 3d 20 p );. ucUtf8 =
c5590 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d 20 NULL;. uclCp =
c55a0 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 NULL;.}../*.** H
c55b0 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 elper function t
c55c0 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 o convert UTF-8
c55d0 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 filenames to loc
c55e0 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 65 al OS/2 codepage
c55f0 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 ..** The two-ste
c5600 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 p process: first
c5610 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 convert the inc
c5620 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69 oming UTF-8 stri
c5630 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d 32 ng.** into UCS-2
c5640 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 and then from U
c5650 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 72 72 CS-2 to the curr
c5660 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a ent codepage..**
c5670 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 The returned ch
c5680 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 ar pointer has t
c5690 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 o be freed..*/.s
c56a0 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f 6e 76 tatic char *conv
c56b0 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 ertUtf8PathToCp(
c56c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 const char *in
c56d0 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d ){. UniChar tem
c56e0 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48 pPath[CCHMAXPATH
c56f0 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d ];. char *out =
c5700 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 (char *)calloc(
c5710 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29 CCHMAXPATH, 1 )
c5720 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a ;.. if( !out ).
c5730 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b return NULL;
c5740 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 20 .. if( !ucUtf8
c5750 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20 || !uclCp ).
c5760 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 initUconvObjects
c5770 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d ();.. /* determ
c5780 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 ine string for t
c5790 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 he conversion of
c57a0 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 UTF-8 which is
c57b0 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 28 20 CP1208 */. if(
c57c0 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63 55 UniStrToUcs( ucU
c57d0 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 tf8, tempPath, (
c57e0 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 char *)in, CCHMA
c57f0 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 XPATH ) != ULS_S
c5800 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 65 74 UCCESS ). ret
c5810 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 urn out; /* if c
c5820 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c onversion fails,
c5830 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74 return the empt
c5840 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f y string */.. /
c5850 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 * conversion for
c5860 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 current codepag
c5870 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 e which can be u
c5880 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f sed for paths */
c5890 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 . UniStrFromUcs
c58a0 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74 65 ( uclCp, out, te
c58b0 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41 mpPath, CCHMAXPA
c58c0 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 TH );.. return
c58d0 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 out;.}../*.** He
c58e0 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f lper function to
c58f0 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d convert filenam
c5900 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f es from local co
c5910 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d 38 2e depage to UTF-8.
c5920 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 .** The two-step
c5930 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 process: first
c5940 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f convert the inco
c5950 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 70 ming codepage-sp
c5960 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67 ecific.** string
c5970 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 into UCS-2 and
c5980 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 then from UCS-2
c5990 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 65 20 to the codepage
c59a0 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 of UTF-8..** The
c59b0 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 70 returned char p
c59c0 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65 ointer has to be
c59d0 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 freed..**.** Th
c59e0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e is function is n
c59f0 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 65 20 on-static to be
c5a00 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 69 73 able to use this
c5a10 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0a in shell.c and.
c5a20 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 6c 69 ** similar appli
c5a30 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 61 6b cations that tak
c5a40 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 e command line a
c5a50 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61 rguments..*/.cha
c5a60 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 68 r *convertCpPath
c5a70 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 63 68 ToUtf8( const ch
c5a80 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43 ar *in ){. UniC
c5a90 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48 har tempPath[CCH
c5aa0 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 72 MAXPATH];. char
c5ab0 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29 *out = (char *)
c5ac0 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 calloc( CCHMAXPA
c5ad0 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 20 TH, 1 );.. if(
c5ae0 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 75 72 !out ). retur
c5af0 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 n NULL;.. if( !
c5b00 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 70 ucUtf8 || !uclCp
c5b10 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e 76 ). initUconv
c5b20 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a Objects();.. /*
c5b30 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 conversion for
c5b40 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 current codepage
c5b50 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 which can be us
c5b60 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a ed for paths */.
c5b70 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f 55 63 if( UniStrToUc
c5b80 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 50 61 s( uclCp, tempPa
c5b90 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 th, (char *)in,
c5ba0 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 3d 20 CCHMAXPATH ) !=
c5bb0 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 ULS_SUCCESS ).
c5bc0 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a return out; /*
c5bd0 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 if conversion f
c5be0 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 65 ails, return the
c5bf0 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 2a 2f empty string */
c5c00 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 .. /* determine
c5c10 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 string for the
c5c20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 conversion of UT
c5c30 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 31 F-8 which is CP1
c5c40 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46 208 */. UniStrF
c5c50 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 2c 20 romUcs( ucUtf8,
c5c60 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43 out, tempPath, C
c5c70 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 20 CHMAXPATH );..
c5c80 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f return out;.}../
c5c90 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 *.** This vector
c5ca0 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 defines all the
c5cb0 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 methods that ca
c5cc0 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a n operate on an.
c5cd0 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ** sqlite3_file
c5ce0 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 for os2..*/.stat
c5cf0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
c5d00 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32 49 _io_methods os2I
c5d10 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c oMethod = {. 1,
c5d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c5d30 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 /* iVers
c5d40 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73 ion */. os2Clos
c5d50 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20 20 e,. os2Read,.
c5d60 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 32 54 os2Write,. os2T
c5d70 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 79 runcate,. os2Sy
c5d80 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69 7a nc,. os2FileSiz
c5d90 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20 e,. os2Lock,.
c5da0 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32 os2Unlock,. os2
c5db0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
c5dc0 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e 74 k,. os2FileCont
c5dd0 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f 72 rol,. os2Sector
c5de0 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69 63 Size,. os2Devic
c5df0 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
c5e00 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .};../**********
c5e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e50 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 *.** Here ends t
c5e60 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 he I/O methods t
c5e70 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c hat form the sql
c5e80 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
c5e90 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 object..**.** Th
c5ea0 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 e next block of
c5eb0 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 code implements
c5ec0 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e the VFS methods.
c5ed0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
c5ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
c5f20 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 /*.** Create a t
c5f30 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 emporary file na
c5f40 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 me in zBuf. zBu
c5f50 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e f must be big en
c5f60 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 ough to.** hold
c5f70 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e at pVfs->mxPathn
c5f80 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a ame characters..
c5f90 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
c5fa0 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 tTempname(int nB
c5fb0 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29 uf, char *zBuf )
c5fc0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 {. static const
c5fd0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a unsigned char z
c5fe0 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 Chars[] =. "a
c5ff0 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 bcdefghijklmnopq
c6000 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 rstuvwxyz". "
c6010 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 ABCDEFGHIJKLMNOP
c6020 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 QRSTUVWXYZ".
c6030 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 "0123456789";.
c6040 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 int i, j;. char
c6050 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b 33 5d zTempPathBuf[3]
c6060 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 74 ;. PSZ zTempPat
c6070 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70 50 h = (PSZ)&zTempP
c6080 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 73 71 athBuf;. if( sq
c6090 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 lite3_temp_direc
c60a0 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 65 6d tory ){. zTem
c60b0 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 5f pPath = sqlite3_
c60c0 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a temp_directory;.
c60d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 }else{. if(
c60e0 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 DosScanEnv( (PS
c60f0 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70 Z)"TEMP", &zTemp
c6100 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 Path ) ){.
c6110 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 if( DosScanEnv(
c6120 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65 (PSZ)"TMP", &zTe
c6130 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 mpPath ) ){.
c6140 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 if( DosScanE
c6150 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 49 52 nv( (PSZ)"TMPDIR
c6160 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 ", &zTempPath )
c6170 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 55 4c ){. UL
c6180 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d 20 3d ONG ulDriveNum =
c6190 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 20 3d 0, ulDriveMap =
c61a0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 44 0;. D
c61b0 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 44 69 osQueryCurrentDi
c61c0 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 6d 2c sk( &ulDriveNum,
c61d0 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 3b 0a &ulDriveMap );.
c61e0 20 20 20 20 20 20 20 20 20 20 20 73 70 72 69 6e sprin
c61f0 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 6d 70 tf( (char*)zTemp
c6200 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 63 68 Path, "%c:", (ch
c6210 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 72 69 ar)( 'A' + ulDri
c6220 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a 20 veNum - 1 ) );.
c6230 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
c6240 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 . }. }. /*
c6250 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 61 69 Strip off a trai
c6260 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72 20 ling slashes or
c6270 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74 68 backslashes, oth
c6280 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64 20 erwise we would
c6290 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 69 get *. * multi
c62a0 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 68 65 ple (back)slashe
c62b0 73 20 77 68 69 63 68 20 63 61 75 73 65 73 20 44 s which causes D
c62c0 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 69 6c osOpen() to fail
c62d0 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a . *
c62e0 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 20 73 . * Trailing s
c62f0 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 61 6c paces are not al
c6300 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e 20 20 lowed, either.
c6310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6320 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 6a */. j
c6330 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
c6340 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 30(zTempPath);.
c6350 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 26 26 while( j > 0 &&
c6360 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 ( zTempPath[j-1
c6370 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 65 ] == '\\' || zTe
c6380 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 mpPath[j-1] == '
c6390 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 /'.
c63a0 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d 70 50 || zTempP
c63b0 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27 20 ath[j-1] == ' '
c63c0 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 20 ) ){. j--;.
c63d0 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a 5d }. zTempPath[j]
c63e0 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 21 = '\0';. if( !
c63f0 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 sqlite3_temp_dir
c6400 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 63 68 ectory ){. ch
c6410 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 54 46 ar *zTempPathUTF
c6420 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74 68 = convertCpPath
c6430 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 61 74 ToUtf8( zTempPat
c6440 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 h );. sqlite3
c6450 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d _snprintf( nBuf-
c6460 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 30, zBuf,.
c6470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6480 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d "%s\\"SQLITE_TEM
c6490 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a P_FILE_PREFIX, z
c64a0 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a 20 TempPathUTF );.
c64b0 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 50 61 free( zTempPa
c64c0 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c 73 65 thUTF );. }else
c64d0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e {. sqlite3_sn
c64e0 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 2c printf( nBuf-30,
c64f0 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 zBuf,.
c6500 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 73 "%s
c6510 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 \\"SQLITE_TEMP_F
c6520 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d ILE_PREFIX, zTem
c6530 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 20 6a pPath );. }. j
c6540 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
c6550 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 73 71 30( zBuf );. sq
c6560 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
c6570 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 20 29 ( 20, &zBuf[j] )
c6580 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 ;. for( i = 0;
c6590 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b i < 20; i++, j++
c65a0 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 ){. zBuf[j]
c65b0 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20 = (char)zChars[
c65c0 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 ((unsigned char)
c65d0 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 zBuf[j])%(sizeof
c65e0 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 (zChars)-1) ];.
c65f0 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 }. zBuf[j] = 0
c6600 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 54 ;. OSTRACE2( "T
c6610 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 EMP FILENAME: %s
c6620 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20 72 \n", zBuf );. r
c6630 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
c6640 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 .}.../*.** Turn
c6650 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e a relative pathn
c6660 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 ame into a full
c6670 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 pathname. Write
c6680 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 the full.** pat
c6690 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 6c 6c hname into zFull
c66a0 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 69 6c []. zFull[] wil
c66b0 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 56 l be at least pV
c66c0 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a fs->mxPathname.*
c66d0 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e * bytes in size.
c66e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
c66f0 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a s2FullPathname(.
c6700 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
c6710 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Vfs, /*
c6720 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 Pointer to vfs
c6730 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 object */. cons
c6740 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 t char *zRelativ
c6750 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 e, /* Possi
c6760 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 bly relative inp
c6770 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 ut path */. int
c6780 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 nFull,
c6790 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
c67a0 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 65 of output buffe
c67b0 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 r in bytes */.
c67c0 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 char *zFull
c67d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
c67e0 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a utput buffer */.
c67f0 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 6c 61 ){. char *zRela
c6800 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 tiveCp = convert
c6810 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 52 Utf8PathToCp( zR
c6820 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 68 61 elative );. cha
c6830 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41 58 r zFullCp[CCHMAX
c6840 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a 20 20 PATH] = "\0";.
c6850 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a char *zFullUTF;.
c6860 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 44 6f APIRET rc = Do
c6870 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20 sQueryPathInfo(
c6880 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 49 4c zRelativeCp, FIL
c6890 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 2c 20 _QUERYFULLNAME,
c68a0 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 20 20 zFullCp,.
c68b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c68c0 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 58 50 CCHMAXP
c68d0 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 20 7a ATH );. free( z
c68e0 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a 20 20 RelativeCp );.
c68f0 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 65 zFullUTF = conve
c6900 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 20 rtCpPathToUtf8(
c6910 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 71 6c zFullCp );. sql
c6920 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e ite3_snprintf( n
c6930 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75 Full, zFull, zFu
c6940 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 65 28 llUTF );. free(
c6950 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 72 zFullUTF );. r
c6960 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 eturn rc == NO_E
c6970 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b RROR ? SQLITE_OK
c6980 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b : SQLITE_IOERR;
c6990 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 .}.../*.** Open
c69a0 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 a file..*/.stati
c69b0 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 0a 20 c int os2Open(.
c69c0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
c69d0 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f fs, /
c69e0 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 * Not used */.
c69f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
c6a00 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e, /*
c6a10 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
c6a20 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 e */. sqlite3_f
c6a30 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 ile *id,
c6a40 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 /* Write th
c6a50 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 e SQLite file ha
c6a60 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 ndle here */. i
c6a70 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
c6a80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c6a90 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 Open mode flags
c6aa0 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c */. int *pOutFl
c6ab0 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20 ags
c6ac0 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65 74 /* Status ret
c6ad0 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a urn flags */.){.
c6ae0 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 4c 4f HFILE h;. ULO
c6af0 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 NG ulFileAttribu
c6b00 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c te = FILE_NORMAL
c6b10 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e ;. ULONG ulOpen
c6b20 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 4c 4f Flags = 0;. ULO
c6b30 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20 NG ulOpenMode =
c6b40 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 0;. os2File *pF
c6b50 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 ile = (os2File*)
c6b60 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 id;. APIRET rc
c6b70 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c = NO_ERROR;. UL
c6b80 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 20 ONG ulAction;.
c6b90 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a 20 char *zNameCp;.
c6ba0 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 43 char zTmpname[C
c6bb0 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 20 20 CHMAXPATH+1];
c6bc0 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 68 6f /* Buffer to ho
c6bd0 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d 70 20 ld name of temp
c6be0 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 file */.. /* If
c6bf0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
c6c00 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e ment to this fun
c6c10 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 ction is NULL, g
c6c20 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 enerate a . **
c6c30 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e temporary file n
c6c40 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f ame to use . */
c6c50 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b . if( !zName ){
c6c60 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65 . int rc = ge
c6c70 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d 41 58 tTempname(CCHMAX
c6c80 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 PATH+1, zTmpname
c6c90 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
c6ca0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
c6cb0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
c6cc0 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a }. zName = z
c6cd0 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a 20 Tmpname;. }...
c6ce0 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 2c 20 memset( pFile,
c6cf0 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 0, sizeof(*pFile
c6d00 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 32 ) );.. OSTRACE2
c6d10 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 64 5c ( "OPEN want %d\
c6d20 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a 20 20 n", flags );..
c6d30 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
c6d40 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
c6d50 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d E ){. ulOpenM
c6d60 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 ode |= OPEN_ACCE
c6d70 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 SS_READWRITE;.
c6d80 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 OSTRACE1( "OPE
c6d90 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 20 N read/write\n"
c6da0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
c6db0 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 ulOpenMode |= OP
c6dc0 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e EN_ACCESS_READON
c6dd0 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 LY;. OSTRACE1
c6de0 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f 6e 6c ( "OPEN read onl
c6df0 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 y\n" );. }.. i
c6e00 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
c6e10 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b E_OPEN_CREATE ){
c6e20 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 . ulOpenFlags
c6e30 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f |= OPEN_ACTION_
c6e40 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c OPEN_IF_EXISTS |
c6e50 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 OPEN_ACTION_CRE
c6e60 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 ATE_IF_NEW;.
c6e70 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 OSTRACE1( "OPEN
c6e80 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 65 5c open new/create\
c6e90 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 n" );. }else{.
c6ea0 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c ulOpenFlags |
c6eb0 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 = OPEN_ACTION_OP
c6ec0 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f EN_IF_EXISTS | O
c6ed0 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c 5f PEN_ACTION_FAIL_
c6ee0 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 IF_NEW;. OSTR
c6ef0 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e ACE1( "OPEN open
c6f00 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b 0a existing\n" );.
c6f10 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 }.. if( flags
c6f20 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d & SQLITE_OPEN_M
c6f30 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 75 6c AIN_DB ){. ul
c6f40 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e OpenMode |= OPEN
c6f50 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 3b _SHARE_DENYNONE;
c6f60 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 . OSTRACE1( "
c6f70 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 64 2f OPEN share read/
c6f80 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 write\n" );. }e
c6f90 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d lse{. ulOpenM
c6fa0 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 ode |= OPEN_SHAR
c6fb0 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 20 20 E_DENYWRITE;.
c6fc0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e OSTRACE1( "OPEN
c6fd0 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e 6c 79 share read only
c6fe0 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 \n" );. }.. if
c6ff0 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
c7000 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
c7010 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 72 20 OSE ){. char
c7020 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 58 50 pathUtf8[CCHMAXP
c7030 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e 44 45 ATH];.#ifdef NDE
c7040 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 62 75 BUG /* when debu
c7050 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 74 6f gging we want to
c7060 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 make sure it is
c7070 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 20 20 deleted */.
c7080 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 20 ulFileAttribute
c7090 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b 0a 23 = FILE_HIDDEN;.#
c70a0 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 75 6c endif. os2Ful
c70b0 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 73 2c lPathname( pVfs,
c70c0 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 50 41 zName, CCHMAXPA
c70d0 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 3b 0a TH, pathUtf8 );.
c70e0 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 pFile->pathT
c70f0 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 55 74 oDel = convertUt
c7100 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 74 68 f8PathToCp( path
c7110 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 54 52 Utf8 );. OSTR
c7120 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 64 64 ACE1( "OPEN hidd
c7130 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f en/delete on clo
c7140 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 se file attribut
c7150 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 es\n" );. }else
c7160 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 {. pFile->pat
c7170 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 hToDel = NULL;.
c7180 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 OSTRACE1( "OP
c7190 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 61 EN normal file a
c71a0 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a 20 ttribute\n" );.
c71b0 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73 20 }.. /* always
c71c0 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 61 open in random a
c71d0 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 20 70 ccess mode for p
c71e0 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 20 73 ossibly better s
c71f0 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e peed */. ulOpen
c7200 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 Mode |= OPEN_FLA
c7210 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f GS_RANDOM;. ulO
c7220 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f penMode |= OPEN_
c7230 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 FLAGS_FAIL_ON_ER
c7240 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 ROR;. ulOpenMod
c7250 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f e |= OPEN_FLAGS_
c7260 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 7a 4e NOINHERIT;.. zN
c7270 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 ameCp = convertU
c7280 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 4e 61 tf8PathToCp( zNa
c7290 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 me );. rc = Dos
c72a0 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 6d 65 Open( (PSZ)zName
c72b0 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 Cp,.
c72c0 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 20 20 &h,.
c72d0 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 69 6f &ulActio
c72e0 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n,.
c72f0 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 20 20 0L,.
c7300 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 74 74 ulFileAtt
c7310 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 20 20 ribute,.
c7320 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 46 6c ulOpenFl
c7330 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ags,.
c7340 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 2c ulOpenMode,
c7350 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c7360 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b (PEAOP2)NULL );
c7370 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 43 70 . free( zNameCp
c7380 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20 );. if( rc !=
c7390 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
c73a0 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 4e 20 OSTRACE7( "OPEN
c73b0 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 20 72 Invalid handle r
c73c0 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20 c=%d: zName=%s,
c73d0 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 75 ulAction=%#lx, u
c73e0 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c 46 6c lAttr=%#lx, ulFl
c73f0 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 65 ags=%#lx, ulMode
c7400 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 20 20 =%#lx\n",.
c7410 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e 61 6d rc, zNam
c7420 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 6c 46 e, ulAction, ulF
c7430 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 75 6c ileAttribute, ul
c7440 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f 70 65 OpenFlags, ulOpe
c7450 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 66 28 nMode );. if(
c7460 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 pFile->pathToDe
c7470 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 28 20 l ). free(
c7480 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c pFile->pathToDel
c7490 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 );. pFile->p
c74a0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b athToDel = NULL;
c74b0 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 . if( flags &
c74c0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
c74d0 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 DWRITE ){.
c74e0 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e 20 OSTRACE2( "OPEN
c74f0 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c %d Invalid handl
c7500 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 7c 20 e\n", ((flags |
c7510 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
c7520 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 5f ONLY) & ~SQLITE_
c7530 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 20 OPEN_READWRITE)
c7540 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
c7550 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c 20 7a os2Open( pVfs, z
c7560 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 20 20 Name, id,.
c7570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7580 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 ((flags | SQLITE
c7590 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 _OPEN_READONLY)
c75a0 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & ~SQLITE_OPEN_R
c75b0 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 20 20 EADWRITE),.
c75c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c75d0 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 pOutFlags );.
c75e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
c75f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e eturn SQLITE_CAN
c7600 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d TOPEN;. }. }
c7610 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 .. if( pOutFlag
c7620 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c s ){. *pOutFl
c7630 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 53 51 ags = flags & SQ
c7640 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
c7650 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f 50 45 ITE ? SQLITE_OPE
c7660 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 53 51 N_READWRITE : SQ
c7670 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
c7680 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 6c 65 LY;. }.. pFile
c7690 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73 32 ->pMethod = &os2
c76a0 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c IoMethod;. pFil
c76b0 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 65 6e e->h = h;. Open
c76c0 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 4f Counter(+1);. O
c76d0 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e 20 25 STRACE3( "OPEN %
c76e0 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 5c 6e d pOutFlags=%d\n
c76f0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 4f 75 ", pFile->h, pOu
c7700 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 74 75 tFlags );. retu
c7710 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
c7720 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 ./*.** Delete th
c7730 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2f e named file..*/
c7740 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 .static int os2D
c7750 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 elete(. sqlite3
c7760 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
c7770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7780 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f /* Not used on o
c7790 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 s2 */. const ch
c77a0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 ar *zFilename,
c77b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c77c0 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 * Name of file t
c77d0 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e o delete */. in
c77e0 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 20 20 t syncDir
c77f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7800 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 /* Not used
c7810 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20 on os2 */.){.
c7820 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 APIRET rc = NO_E
c7830 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 RROR;. char *zF
c7840 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 ilenameCp = conv
c7850 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 ertUtf8PathToCp(
c7860 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 zFilename );.
c7870 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
c7880 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
c7890 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b 0a 20 OERR_DELETE );.
c78a0 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 65 28 rc = DosDelete(
c78b0 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 (PSZ)zFilenameC
c78c0 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 p );. free( zFi
c78d0 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 lenameCp );. OS
c78e0 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 45 20 TRACE2( "DELETE
c78f0 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 \"%s\"\n", zFile
c7900 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 72 6e name );. return
c7910 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc == NO_ERROR
c7920 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 ? SQLITE_OK : SQ
c7930 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 LITE_IOERR_DELET
c7940 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 E;.}../*.** Chec
c7950 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 k the existance
c7960 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61 20 and status of a
c7970 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
c7980 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 0a 20 int os2Access(.
c7990 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
c79a0 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f fs, /* No
c79b0 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f t used on os2 */
c79c0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
c79d0 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 Filename, /*
c79e0 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
c79f0 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 check */. int f
c7a00 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
c7a10 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 /* Type of
c7a20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 test to make on
c7a30 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 this file */. i
c7a40 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 20 20 nt *pOut
c7a50 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
c7a60 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a e results here *
c7a70 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 54 55 /.){. FILESTATU
c7a80 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e S3 fsts3ConfigIn
c7a90 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 fo;. APIRET rc
c7aa0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 68 = NO_ERROR;. ch
c7ab0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 ar *zFilenameCp
c7ac0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 = convertUtf8Pat
c7ad0 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d 65 hToCp( zFilename
c7ae0 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 20 26 );.. memset( &
c7af0 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c fsts3ConfigInfo,
c7b00 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33 0, sizeof(fsts3
c7b10 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b 0a 20 ConfigInfo) );.
c7b20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 61 rc = DosQueryPa
c7b30 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46 69 thInfo( (PSZ)zFi
c7b40 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53 54 lenameCp, FIL_ST
c7b50 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 20 20 ANDARD,.
c7b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7b70 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 &fsts3ConfigInf
c7b80 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 54 o, sizeof(FILEST
c7b90 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 65 65 ATUS3) );. free
c7ba0 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b ( zFilenameCp );
c7bb0 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 41 43 . OSTRACE4( "AC
c7bc0 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 69 67 CESS fsts3Config
c7bd0 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25 64 Info.attrFile=%d
c7be0 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 64 5c flags=%d rc=%d\
c7bf0 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
c7c00 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e fsts3ConfigInfo.
c7c10 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 73 2c attrFile, flags,
c7c20 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 68 28 rc );. switch(
c7c30 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 flags ){. ca
c7c40 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
c7c50 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20 _READ:. case
c7c60 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
c7c70 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d ISTS:. rc =
c7c80 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 (rc == NO_ERROR
c7c90 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 );. OSTRACE
c7ca0 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61 63 3( "ACCESS %s ac
c7cb0 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 6e 64 cess of read and
c7cc0 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 5c 6e exists rc=%d\n
c7cd0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 ", zFilename, rc
c7ce0 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
c7cf0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
c7d00 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 _ACCESS_READWRIT
c7d10 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 72 E:. rc = (r
c7d20 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 26 c == NO_ERROR) &
c7d30 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 69 67 & ( (fsts3Config
c7d40 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 26 20 Info.attrFile &
c7d50 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 20 3d FILE_READONLY) =
c7d60 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f 53 54 = 0 );. OST
c7d70 52 41 43 45 33 28 20 22 41 43 43 45 53 53 20 25 RACE3( "ACCESS %
c7d80 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 61 64 s access of read
c7d90 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c 6e 22 /write rc=%d\n"
c7da0 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 , zFilename, rc
c7db0 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
c7dc0 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 default:.
c7dd0 20 20 20 61 73 73 65 72 74 28 20 21 22 49 6e 76 assert( !"Inv
c7de0 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d alid flags argum
c7df0 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 ent" );. }. *p
c7e00 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 Out = rc;. retu
c7e10 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
c7e20 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
c7e30 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e _OMIT_LOAD_EXTEN
c7e40 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 SION./*.** Inter
c7e50 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e faces for openin
c7e60 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 g a shared libra
c7e70 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 ry, finding entr
c7e80 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 y points.** with
c7e90 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 in the shared li
c7ea0 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 brary, and closi
c7eb0 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 ng the shared li
c7ec0 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 brary..*/./*.**
c7ed0 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f Interfaces for o
c7ee0 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 pening a shared
c7ef0 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 library, finding
c7f00 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a entry points.**
c7f10 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 within the shar
c7f20 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 ed library, and
c7f30 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 closing the shar
c7f40 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 ed library..*/.s
c7f50 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 tatic void *os2D
c7f60 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 lOpen(sqlite3_vf
c7f70 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 s *pVfs, const c
c7f80 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
c7f90 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 72 72 . UCHAR loadErr
c7fa0 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45 [256];. HMODULE
c7fb0 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54 20 hmod;. APIRET
c7fc0 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c rc;. char *zFil
c7fd0 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 enameCp = conver
c7fe0 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 7a 46 tUtf8PathToCp(zF
c7ff0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20 3d ilename);. rc =
c8000 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 28 DosLoadModule((
c8010 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69 7a PSZ)loadErr, siz
c8020 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a 46 eof(loadErr), zF
c8030 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f 64 ilenameCp, &hmod
c8040 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c 65 6e );. free(zFilen
c8050 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 72 6e ameCp);. return
c8060 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc != NO_ERROR
c8070 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d 6f ? 0 : (void*)hmo
c8080 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d d;.}./*.** A no-
c8090 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 72 72 op since the err
c80a0 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
c80b0 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c 6f ned on the DosLo
c80c0 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a adModule call..*
c80d0 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74 75 * os2Dlopen retu
c80e0 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f 73 4c rns zero if DosL
c80f0 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 oadModule is not
c8100 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a successful..*/.
c8110 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 static void os2D
c8120 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 lError(sqlite3_v
c8130 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 fs *pVfs, int nB
c8140 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 uf, char *zBufOu
c8150 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a t){./* no-op */.
c8160 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f }.static void *o
c8170 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f s2DlSym(sqlite3_
c8180 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 vfs *pVfs, void
c8190 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 *pHandle, const
c81a0 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a char *zSymbol){.
c81b0 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 50 49 PFN pfn;. API
c81c0 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d 20 44 RET rc;. rc = D
c81d0 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72 28 osQueryProcAddr(
c81e0 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 (HMODULE)pHandle
c81f0 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26 , 0L, zSymbol, &
c8200 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 20 21 pfn);. if( rc !
c8210 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
c8220 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 6d 62 /* if the symb
c8230 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 6e 6f ol itself was no
c8240 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 t found, search
c8250 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 73 61 again for the sa
c8260 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 6f 6c me. * symbol
c8270 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 20 75 with an extra u
c8280 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 74 20 nderscore, that
c8290 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 64 20 might be needed
c82a0 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 20 2a depending. *
c82b0 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 on the calling
c82c0 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20 convention */.
c82d0 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b char _zSymbol[
c82e0 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 20 256] = "_";.
c82f0 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 6f 6c strncat(_zSymbol
c8300 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 29 3b , zSymbol, 255);
c8310 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 75 65 . rc = DosQue
c8320 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 ryProcAddr((HMOD
c8330 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c ULE)pHandle, 0L,
c8340 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 _zSymbol, &pfn)
c8350 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
c8360 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 c != NO_ERROR ?
c8370 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e 3b 0a 0 : (void*)pfn;.
c8380 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 }.static void os
c8390 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 2DlClose(sqlite3
c83a0 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 _vfs *pVfs, void
c83b0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44 6f *pHandle){. Do
c83c0 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d 4f sFreeModule((HMO
c83d0 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d DULE)pHandle);.}
c83e0 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c .#else /* if SQL
c83f0 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 ITE_OMIT_LOAD_EX
c8400 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e TENSION is defin
c8410 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 ed: */. #define
c8420 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20 23 os2DlOpen 0. #
c8430 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 72 6f define os2DlErro
c8440 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 r 0. #define os
c8450 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 66 69 2DlSym 0. #defi
c8460 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30 0a ne os2DlClose 0.
c8470 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 #endif.../*.** W
c8480 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66 20 rite up to nBuf
c8490 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e bytes of randomn
c84a0 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a ess into zBuf..*
c84b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
c84c0 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 Randomness(sqlit
c84d0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
c84e0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
c84f0 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 uf ){. int n =
c8500 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 0;.#if defined(S
c8510 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 QLITE_TEST). n
c8520 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 = nBuf;. memset
c8530 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b (zBuf, 0, nBuf);
c8540 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 69 7a .#else. int siz
c8550 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 6f eofULong = sizeo
c8560 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 28 20 f(ULONG);. if(
c8570 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 54 45 (int)sizeof(DATE
c8580 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 2d 20 TIME) <= nBuf -
c8590 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 49 4d n ){. DATETIM
c85a0 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 74 44 E x;. DosGetD
c85b0 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 20 20 ateTime(&x);.
c85c0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
c85d0 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 , &x, sizeof(x))
c85e0 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f ;. n += sizeo
c85f0 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 f(x);. }.. if(
c8600 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 sizeofULong <=
c8610 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 nBuf - n ){.
c8620 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 44 PPIB ppib;. D
c8630 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 osGetInfoBlocks(
c8640 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a 20 20 NULL, &ppib);.
c8650 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
c8660 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f 75 6c ], &ppib->pib_ul
c8670 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 pid, sizeofULong
c8680 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
c8690 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 ofULong;. }..
c86a0 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 if( sizeofULong
c86b0 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 <= nBuf - n ){.
c86c0 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 PTIB ptib;.
c86d0 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 DosGetInfoBloc
c86e0 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b ks(&ptib, NULL);
c86f0 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 . memcpy(&zBu
c8700 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 69 62 f[n], &ptib->tib
c8710 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 _ptib2->tib2_ult
c8720 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 id, sizeofULong)
c8730 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f ;. n += sizeo
c8740 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 2f fULong;. }.. /
c8750 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 68 61 * if we still ha
c8760 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 68 65 ven't filled the
c8770 20 62 75 66 66 65 72 20 79 65 74 20 74 68 65 20 buffer yet the
c8780 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c 20 2a following will *
c8790 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 65 72 /. /* grab ever
c87a0 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e 73 74 ything once inst
c87b0 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 73 65 ead of making se
c87c0 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f 72 20 veral calls for
c87d0 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 2a 2f a single item */
c87e0 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f . if( sizeofULo
c87f0 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 ng <= nBuf - n )
c8800 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c 53 79 {. ULONG ulSy
c8810 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b 0a sInfo[QSV_MAX];.
c8820 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 73 49 DosQuerySysI
c8830 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 58 2c nfo(1L, QSV_MAX,
c8840 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 7a 65 ulSysInfo, size
c8850 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d 41 ofULong * QSV_MA
c8860 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 X);.. memcpy(
c8870 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 &zBuf[n], &ulSys
c8880 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55 4e Info[QSV_MS_COUN
c8890 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c T - 1], sizeofUL
c88a0 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 ong);. n += s
c88b0 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20 20 izeofULong;..
c88c0 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 if( sizeofULong
c88d0 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a <= nBuf - n ){.
c88e0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 memcpy(&zB
c88f0 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 uf[n], &ulSysInf
c8900 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e 54 45 o[QSV_TIMER_INTE
c8910 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a 65 6f RVAL - 1], sizeo
c8920 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e fULong);. n
c8930 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b += sizeofULong;
c8940 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
c8950 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 izeofULong <= nB
c8960 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 uf - n ){.
c8970 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c memcpy(&zBuf[n],
c8980 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f &ulSysInfo[QSV_
c8990 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20 73 TIME_LOW - 1], s
c89a0 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 izeofULong);.
c89b0 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c n += sizeofUL
c89c0 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ong;. }. i
c89d0 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c f( sizeofULong <
c89e0 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 = nBuf - n ){.
c89f0 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 memcpy(&zBuf
c8a00 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b [n], &ulSysInfo[
c8a10 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 2d 20 QSV_TIME_HIGH -
c8a20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 1], sizeofULong)
c8a30 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a ;. n += siz
c8a40 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a eofULong;. }.
c8a50 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c if( sizeofUL
c8a60 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 ong <= nBuf - n
c8a70 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
c8a80 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 &zBuf[n], &ulSys
c8a90 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 41 49 Info[QSV_TOTAVAI
c8aa0 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a 65 6f LMEM - 1], sizeo
c8ab0 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e fULong);. n
c8ac0 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b += sizeofULong;
c8ad0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
c8ae0 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d f.. return n;.}
c8af0 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f ../*.** Sleep fo
c8b00 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 r a little while
c8b10 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d . Return the am
c8b20 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 ount of time sle
c8b30 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d pt..** The argum
c8b40 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ent is the numbe
c8b50 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 r of microsecond
c8b60 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 s we want to sle
c8b70 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 ep..** The retur
c8b80 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e n value is the n
c8b90 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 umber of microse
c8ba0 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 conds of sleep a
c8bb0 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 ctually.** reque
c8bc0 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e sted from the un
c8bd0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
c8be0 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d ng system, a num
c8bf0 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 ber which.** mig
c8c00 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68 ht be greater th
c8c10 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
c8c20 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 he argument, but
c8c30 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 not less.** tha
c8c40 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a n the argument..
c8c50 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c8c60 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 33 5f 2Sleep( sqlite3_
c8c70 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d vfs *pVfs, int m
c8c80 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f 73 icrosec ){. Dos
c8c90 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 65 63 Sleep( (microsec
c8ca0 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 74 75 /1000) );. retu
c8cb0 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a rn microsec;.}..
c8cc0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
c8cd0 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 ing variable, if
c8ce0 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 set to a non-ze
c8cf0 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 ro value, become
c8d00 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 s the result.**
c8d10 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 returned from sq
c8d20 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 lite3OsCurrentTi
c8d30 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 me(). This is u
c8d40 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e sed for testing.
c8d50 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
c8d60 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
c8d70 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 I int sqlite3_cu
c8d80 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a rrent_time = 0;.
c8d90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 #endif../*.** Fi
c8da0 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 nd the current t
c8db0 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 ime (in Universa
c8dc0 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 l Coordinated Ti
c8dd0 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a me). Write the.
c8de0 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 ** current time
c8df0 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 and date as a Ju
c8e00 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 lian Day number
c8e10 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a into *prNow and.
c8e20 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 ** return 0. Re
c8e30 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 turn 1 if the ti
c8e40 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e me and date cann
c8e50 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a ot be found..*/.
c8e60 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 54 69 int os2CurrentTi
c8e70 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20 me( sqlite3_vfs
c8e80 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 *pVfs, double *p
c8e90 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65 rNow ){. double
c8ea0 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d 69 now;. SHORT mi
c8eb0 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20 74 nute; /* needs t
c8ec0 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 6f 70 o be able to cop
c8ed0 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 65 20 e with negative
c8ee0 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 timezone offset
c8ef0 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 63 6f */. USHORT seco
c8f00 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20 20 nd, hour,.
c8f10 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 day, month, y
c8f20 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d 45 20 ear;. DATETIME
c8f30 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 74 65 dt;. DosGetDate
c8f40 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 20 73 Time( &dt );. s
c8f50 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54 29 econd = (USHORT)
c8f60 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d 69 dt.seconds;. mi
c8f70 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 64 74 nute = (SHORT)dt
c8f80 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e 74 69 .minutes + dt.ti
c8f90 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 3d mezone;. hour =
c8fa0 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f 75 72 (USHORT)dt.hour
c8fb0 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 48 4f s;. day = (USHO
c8fc0 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e RT)dt.day;. mon
c8fd0 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e th = (USHORT)dt.
c8fe0 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d 20 month;. year =
c8ff0 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 72 3b (USHORT)dt.year;
c9000 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69 .. /* Calculati
c9010 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f ons from http://
c9020 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e www.astro.keele.
c9030 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f ac.uk/~rno/Astro
c9040 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20 nomy/hjd.html.
c9050 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 http://www.as
c9060 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f tro.keele.ac.uk/
c9070 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 ~rno/Astronomy/h
c9080 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a jd-0.1.c */. /*
c9090 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 4a Calculate the J
c90a0 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20 20 ulian days */.
c90b0 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 30 37 now = day - 3207
c90c0 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 79 65 6 +. 1461*(ye
c90d0 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e ar + 4800 + (mon
c90e0 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b th - 14)/12)/4 +
c90f0 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68 20 . 367*(month
c9100 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 31 - 2 - (month - 1
c9110 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a 20 4)/12*12)/12 -.
c9120 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 34 39 3*((year + 49
c9130 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 00 + (month - 14
c9140 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 20 )/12)/100)/4;..
c9150 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 61 63 /* Add the frac
c9160 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d 69 tional hours, mi
c9170 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 20 2a ns and seconds *
c9180 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 72 /. now += (hour
c9190 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a 20 + 12.0)/24.0;.
c91a0 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f 31 now += minute/1
c91b0 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 440.0;. now +=
c91c0 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b 0a second/86400.0;.
c91d0 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a *prNow = now;.
c91e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
c91f0 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ST. if( sqlite3
c9200 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b _current_time ){
c9210 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 . *prNow = sq
c9220 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 lite3_current_ti
c9230 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 me/86400.0 + 244
c9240 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 0587.5;. }.#end
c9250 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d if. return 0;.}
c9260 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 ..static int os2
c9270 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c GetLastError(sql
c9280 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
c9290 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
c92a0 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 6e 20 zBuf){. return
c92b0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 0;.}../*.** Init
c92c0 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 ialize and deini
c92d0 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 tialize the oper
c92e0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 ating system int
c92f0 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erface..*/.SQLIT
c9300 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c9310 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 3_os_init(void){
c9320 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 . static sqlite
c9330 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d 20 7b 3_vfs os2Vfs = {
c9340 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 . 1,
c9350 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 /* iVers
c9360 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f ion */. sizeo
c9370 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a f(os2File), /*
c9380 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 szOsFile */.
c9390 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 20 20 CCHMAXPATH,
c93a0 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d /* mxPathnam
c93b0 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 e */. 0,
c93c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 /* p
c93d0 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32 Next */. "os2
c93e0 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ", /
c93f0 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 * zName */. 0
c9400 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
c9410 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f /* pAppData */
c9420 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20 .. os2Open,
c9430 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 /* xOpe
c9440 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65 n */. os2Dele
c9450 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 te, /* x
c9460 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73 Delete */. os
c9470 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 2Access,
c9480 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 /* xAccess */.
c9490 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 os2FullPathna
c94a0 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 me, /* xFullPa
c94b0 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 thname */. os
c94c0 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 2DlOpen,
c94d0 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 /* xDlOpen */.
c94e0 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20 20 os2DlError,
c94f0 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f /* xDlErro
c9500 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53 79 r */. os2DlSy
c9510 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 m, /* x
c9520 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 32 DlSym */. os2
c9530 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 DlClose,
c9540 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 /* xDlClose */.
c9550 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73 os2Randomness
c9560 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d , /* xRandom
c9570 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 53 ness */. os2S
c9580 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f leep, /
c9590 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 * xSleep */.
c95a0 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 os2CurrentTime,
c95b0 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 /* xCurrentTi
c95c0 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65 74 me */. os2Get
c95d0 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 LastError /*
c95e0 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f xGetLastError */
c95f0 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f . };. sqlite3_
c9600 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 6f 73 vfs_register(&os
c9610 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69 74 2Vfs, 1);. init
c9620 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a UconvObjects();.
c9630 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
c9640 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 OK;.}.SQLITE_API
c9650 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f int sqlite3_os_
c9660 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 72 65 end(void){. fre
c9670 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b eUconvObjects();
c9680 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
c9690 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f _OK;.}..#endif /
c96a0 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 * SQLITE_OS_OS2
c96b0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
c96c0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 *** End of os_os
c96d0 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2.c ************
c96e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c96f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9700 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
c9710 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f *** Begin file o
c9720 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a s_unix.c *******
c9730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9750 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d **/./*.** 2004 M
c9760 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 22.**.** The
c9770 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
c9780 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
c9790 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
c97a0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
c97b0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
c97c0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
c97d0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
c97e0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
c97f0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
c9800 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
c9810 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
c9820 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
c9830 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
c9840 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
c9850 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
c9860 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
c9870 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
c9880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c98a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c98b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c98c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
c98d0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
c98e0 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 6c 65 ns the VFS imple
c98f0 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 75 6e mentation for un
c9900 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e ix-like operatin
c9910 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 6e 63 g systems.** inc
c9920 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 63 4f lude Linux, MacO
c9930 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c 20 56 SX, *BSD, QNX, V
c9940 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 50 55 xWorks, AIX, HPU
c9950 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e 0a 2a X, and others..*
c9960 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61 *.** There are a
c9970 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 6c 20 ctually several
c9980 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 69 6d different VFS im
c9990 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 6e plementations in
c99a0 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 54 this file..** T
c99b0 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 61 he differences a
c99c0 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 74 68 re in the way th
c99d0 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 at file locking
c99e0 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 64 65 is done. The de
c99f0 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 fault.** impleme
c9a00 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 6f 73 ntation uses Pos
c9a10 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b ix Advisory Lock
c9a20 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 65 20 s. Alternative
c9a30 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a implementations.
c9a40 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c 20 ** use flock(),
c9a50 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 69 6f dot-files, vario
c9a60 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 20 6c us proprietary l
c9a70 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c 20 ocking schemas,
c9a80 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b 69 or simply.** ski
c9a90 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 74 6f p locking all to
c9aa0 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 gether..**.** Th
c9ab0 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 is source file i
c9ac0 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e 74 6f s organized into
c9ad0 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 72 65 divisions where
c9ae0 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 20 76 the logic for v
c9af0 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 75 6e arious.** subfun
c9b00 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 61 69 ctions is contai
c9b10 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 61 ned within the a
c9b20 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 69 73 ppropriate divis
c9b30 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a 2a 20 ion. PLEASE.**
c9b40 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 54 55 KEEP THE STRUCTU
c9b50 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c 45 20 RE OF THIS FILE
c9b60 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 6f 64 INTACT. New cod
c9b70 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c 61 63 e should be plac
c9b80 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 ed.** in the cor
c9b90 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 61 6e rect division an
c9ba0 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 65 61 d should be clea
c9bb0 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a 0a rly labeled..**.
c9bc0 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 6f 66 ** The layout of
c9bd0 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 61 73 divisions is as
c9be0 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
c9bf0 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 75 72 * General-pur
c9c00 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 6f 6e pose declaration
c9c10 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 66 75 s and utility fu
c9c20 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 nctions..** *
c9c30 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 Unique file ID
c9c40 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 56 78 logic used by Vx
c9c50 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20 56 Works..** * V
c9c60 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 70 arious locking p
c9c70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 6d 65 rimitive impleme
c9c80 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 65 78 ntations (all ex
c9c90 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 6b 69 cept proxy locki
c9ca0 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b 20 66 ng):.** + f
c9cb0 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 or Posix Advisor
c9cc0 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 y Locks.**
c9cd0 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 6b + for no-op lock
c9ce0 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 s.** + for
c9cf0 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a dot-file locks.*
c9d00 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 6c 6f * + for flo
c9d10 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 ck() locking.**
c9d20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d 65 64 + for named
c9d30 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 semaphore locks
c9d40 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 29 0a (VxWorks only).
c9d50 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 41 46 ** + for AF
c9d60 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c 6f 63 P filesystem loc
c9d70 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 ks (MacOSX only)
c9d80 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 .** * sqlite3
c9d90 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 6e 6f _file methods no
c9da0 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 t associated wit
c9db0 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 h locking..**
c9dc0 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f * Definitions o
c9dd0 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 f sqlite3_io_met
c9de0 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 hods objects for
c9df0 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 all locking.**
c9e00 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 6c 75 methods plu
c9e10 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 s "finder" funct
c9e20 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 6c 6f ions for each lo
c9e30 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a 2a cking method..**
c9e40 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 76 66 * sqlite3_vf
c9e50 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 6d 65 s method impleme
c9e60 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a ntations..** *
c9e70 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 Locking primit
c9e80 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 72 6f ives for the pro
c9e90 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d xy uber-locking-
c9ea0 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 58 20 method. (MacOSX
c9eb0 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 44 65 only).** * De
c9ec0 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c finitions of sql
c9ed0 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 ite3_vfs objects
c9ee0 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 for all locking
c9ef0 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 20 20 methods.**
c9f00 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e 74 61 plus implementa
c9f10 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 tions of sqlite3
c9f20 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 _os_init() and s
c9f30 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e qlite3_os_end().
c9f40 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 75 .**.** $Id: os_u
c9f50 6e 69 78 2e 63 2c 76 20 31 2e 32 33 37 20 32 30 nix.c,v 1.237 20
c9f60 30 39 2f 30 31 2f 31 35 20 30 34 3a 33 30 3a 30 09/01/15 04:30:0
c9f70 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 3 drh Exp $.*/.#
c9f80 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 if SQLITE_OS_UNI
c9f90 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f X /
c9fa0 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 75 * This file is u
c9fb0 73 65 64 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c 79 sed on unix only
c9fc0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 */../*.** There
c9fd0 20 61 72 65 20 76 61 72 69 6f 75 73 20 6d 65 74 are various met
c9fe0 68 6f 64 73 20 66 6f 72 20 66 69 6c 65 20 6c 6f hods for file lo
c9ff0 63 6b 69 6e 67 20 75 73 65 64 20 66 6f 72 20 63 cking used for c
ca000 6f 6e 63 75 72 72 65 6e 63 79 0a 2a 2a 20 63 6f oncurrency.** co
ca010 6e 74 72 6f 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 ntrol:.**.** 1
ca020 2e 20 50 4f 53 49 58 20 6c 6f 63 6b 69 6e 67 20 . POSIX locking
ca030 28 74 68 65 20 64 65 66 61 75 6c 74 29 2c 0a 2a (the default),.*
ca040 2a 20 20 20 32 2e 20 4e 6f 20 6c 6f 63 6b 69 6e * 2. No lockin
ca050 67 2c 0a 2a 2a 20 20 20 33 2e 20 44 6f 74 2d 66 g,.** 3. Dot-f
ca060 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 ile locking,.**
ca070 20 20 34 2e 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 4. flock() loc
ca080 6b 69 6e 67 2c 0a 2a 2a 20 20 20 35 2e 20 41 46 king,.** 5. AF
ca090 50 20 6c 6f 63 6b 69 6e 67 20 28 4f 53 58 20 6f P locking (OSX o
ca0a0 6e 6c 79 29 2c 0a 2a 2a 20 20 20 36 2e 20 4e 61 nly),.** 6. Na
ca0b0 6d 65 64 20 50 4f 53 49 58 20 73 65 6d 61 70 68 med POSIX semaph
ca0c0 6f 72 65 73 20 28 56 58 57 6f 72 6b 73 20 6f 6e ores (VXWorks on
ca0d0 6c 79 29 2c 0a 2a 2a 20 20 20 37 2e 20 70 72 6f ly),.** 7. pro
ca0e0 78 79 20 6c 6f 63 6b 69 6e 67 2e 20 28 4f 53 58 xy locking. (OSX
ca0f0 20 6f 6e 6c 79 29 0a 2a 2a 0a 2a 2a 20 53 74 79 only).**.** Sty
ca100 6c 65 73 20 34 2c 20 35 2c 20 61 6e 64 20 37 20 les 4, 5, and 7
ca110 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 are only availab
ca120 6c 65 20 6f 66 20 53 51 4c 49 54 45 5f 45 4e 41 le of SQLITE_ENA
ca130 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
ca140 45 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 20 E.** is defined
ca150 74 6f 20 31 2e 20 20 54 68 65 20 53 51 4c 49 54 to 1. The SQLIT
ca160 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
ca170 5f 53 54 59 4c 45 20 61 6c 73 6f 20 65 6e 61 62 _STYLE also enab
ca180 6c 65 73 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a les automatic.**
ca190 20 73 65 6c 65 63 74 69 6f 6e 20 6f 66 20 74 68 selection of th
ca1a0 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f e appropriate lo
ca1b0 63 6b 69 6e 67 20 73 74 79 6c 65 20 62 61 73 65 cking style base
ca1c0 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 73 79 73 d on the filesys
ca1d0 74 65 6d 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 tem.** where the
ca1e0 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 database is loc
ca1f0 61 74 65 64 2e 20 20 0a 2a 2f 0a 23 69 66 20 21 ated. .*/.#if !
ca200 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 defined(SQLITE_E
ca210 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
ca220 59 4c 45 29 0a 23 20 20 69 66 20 64 65 66 69 6e YLE).# if defin
ca230 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 23 20 ed(__APPLE__).#
ca240 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
ca250 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
ca260 53 54 59 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a STYLE 1.# else.
ca270 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 # define SQLI
ca280 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
ca290 47 5f 53 54 59 4c 45 20 30 0a 23 20 20 65 6e 64 G_STYLE 0.# end
ca2a0 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a if.#endif../*.**
ca2b0 20 44 65 66 69 6e 65 20 74 68 65 20 4f 53 5f 56 Define the OS_V
ca2c0 58 57 4f 52 4b 53 20 70 72 65 2d 70 72 6f 63 65 XWORKS pre-proce
ca2d0 73 73 6f 72 20 6d 61 63 72 6f 20 74 6f 20 31 20 ssor macro to 1
ca2e0 69 66 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 0a if building on .
ca2f0 2a 2a 20 76 78 77 6f 72 6b 73 2c 20 6f 72 20 30 ** vxworks, or 0
ca300 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 23 otherwise..*/.#
ca310 69 66 6e 64 65 66 20 4f 53 5f 56 58 57 4f 52 4b ifndef OS_VXWORK
ca320 53 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 28 S.# if defined(
ca330 5f 5f 52 54 50 5f 5f 29 20 7c 7c 20 64 65 66 69 __RTP__) || defi
ca340 6e 65 64 28 5f 57 52 53 5f 4b 45 52 4e 45 4c 29 ned(_WRS_KERNEL)
ca350 0a 23 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f .# define OS_
ca360 56 58 57 4f 52 4b 53 20 31 0a 23 20 20 65 6c 73 VXWORKS 1.# els
ca370 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 4f 53 e.# define OS
ca380 5f 56 58 57 4f 52 4b 53 20 30 0a 23 20 20 65 6e _VXWORKS 0.# en
ca390 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a dif.#endif../*.*
ca3a0 2a 20 54 68 65 73 65 20 23 64 65 66 69 6e 65 73 * These #defines
ca3b0 20 73 68 6f 75 6c 64 20 65 6e 61 62 6c 65 20 3e should enable >
ca3c0 32 47 42 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2GB file support
ca3d0 20 6f 6e 20 50 6f 73 69 78 20 69 66 20 74 68 65 on Posix if the
ca3e0 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f .** underlying o
ca3f0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
ca400 73 75 70 70 6f 72 74 73 20 69 74 2e 20 20 49 66 supports it. If
ca410 20 74 68 65 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a the OS lacks.**
ca420 20 6c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70 large file supp
ca430 6f 72 74 2c 20 74 68 65 73 65 20 73 68 6f 75 6c ort, these shoul
ca440 64 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a d be no-ops..**.
ca450 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65 20 73 75 ** Large file su
ca460 70 70 6f 72 74 20 63 61 6e 20 62 65 20 64 69 73 pport can be dis
ca470 61 62 6c 65 64 20 75 73 69 6e 67 20 74 68 65 20 abled using the
ca480 2d 44 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 -DSQLITE_DISABLE
ca490 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a 2a 20 6f _LFS switch.** o
ca4a0 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 n the compiler c
ca4b0 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20 20 54 68 ommand line. Th
ca4c0 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 is is necessary
ca4d0 69 66 20 79 6f 75 20 61 72 65 20 63 6f 6d 70 69 if you are compi
ca4e0 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20 72 65 63 ling.** on a rec
ca4f0 65 6e 74 20 6d 61 63 68 69 6e 65 20 28 65 78 3a ent machine (ex:
ca500 20 52 65 64 48 61 74 20 37 2e 32 29 20 62 75 74 RedHat 7.2) but
ca510 20 79 6f 75 20 77 61 6e 74 20 79 6f 75 72 20 63 you want your c
ca520 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f ode to work.** o
ca530 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61 63 68 69 n an older machi
ca540 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 36 ne (ex: RedHat 6
ca550 2e 30 29 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d .0). If you com
ca560 70 69 6c 65 20 6f 6e 20 52 65 64 48 61 74 20 37 pile on RedHat 7
ca570 2e 32 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 .2.** without th
ca580 69 73 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 is option, LFS i
ca590 73 20 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c s enable. But L
ca5a0 46 53 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 FS does not exis
ca5b0 74 20 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a t in the kernel.
ca5c0 2a 2a 20 69 6e 20 52 65 64 48 61 74 20 36 2e 30 ** in RedHat 6.0
ca5d0 2c 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f , so the code wo
ca5e0 6e 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 n't work. Hence
ca5f0 2c 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 , for maximum bi
ca600 6e 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c nary.** portabil
ca610 69 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f ity you should o
ca620 6d 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 54 mit LFS..**.** T
ca630 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72 61 he previous para
ca640 67 72 61 70 68 20 77 61 73 20 77 72 69 74 74 65 graph was writte
ca650 6e 20 69 6e 20 32 30 30 35 2e 20 20 28 54 68 69 n in 2005. (Thi
ca660 73 20 70 61 72 61 67 72 61 70 68 20 69 73 20 77 s paragraph is w
ca670 72 69 74 74 65 6e 0a 2a 2a 20 6f 6e 20 32 30 30 ritten.** on 200
ca680 38 2d 31 31 2d 32 38 2e 29 20 54 68 65 73 65 20 8-11-28.) These
ca690 64 61 79 73 2c 20 61 6c 6c 20 4c 69 6e 75 78 20 days, all Linux
ca6a0 6b 65 72 6e 65 6c 73 20 73 75 70 70 6f 72 74 20 kernels support
ca6b0 6c 61 72 67 65 20 66 69 6c 65 73 2c 20 73 6f 0a large files, so.
ca6c0 2a 2a 20 79 6f 75 20 73 68 6f 75 6c 64 20 70 72 ** you should pr
ca6d0 6f 62 61 62 6c 79 20 6c 65 61 76 65 20 4c 46 53 obably leave LFS
ca6e0 20 65 6e 61 62 6c 65 64 2e 20 20 42 75 74 20 73 enabled. But s
ca6f0 6f 6d 65 20 65 6d 62 65 64 64 65 64 20 70 6c 61 ome embedded pla
ca700 74 66 6f 72 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 tforms might.**
ca710 6c 61 63 6b 20 4c 46 53 20 69 6e 20 77 68 69 63 lack LFS in whic
ca720 68 20 63 61 73 65 20 74 68 65 20 53 51 4c 49 54 h case the SQLIT
ca730 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 6d 61 E_DISABLE_LFS ma
ca740 63 72 6f 20 6d 69 67 68 74 20 73 74 69 6c 6c 20 cro might still
ca750 62 65 20 75 73 65 66 75 6c 2e 0a 2a 2f 0a 23 69 be useful..*/.#i
ca760 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 fndef SQLITE_DIS
ca770 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e ABLE_LFS.# defin
ca780 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 e _LARGE_FILE
ca790 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 1.# ifndef _
ca7a0 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 FILE_OFFSET_BITS
ca7b0 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c .# define _FIL
ca7c0 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34 E_OFFSET_BITS 64
ca7d0 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e .# endif.# defin
ca7e0 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 e _LARGEFILE_SOU
ca7f0 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a RCE 1.#endif../*
ca800 0a 2a 2a 20 73 74 61 6e 64 61 72 64 20 69 6e 63 .** standard inc
ca810 6c 75 64 65 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 lude files..*/.#
ca820 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74 79 70 include <sys/typ
ca830 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c es.h>.#include <
ca840 73 79 73 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 sys/stat.h>.#inc
ca850 6c 75 64 65 20 3c 66 63 6e 74 6c 2e 68 3e 0a 23 lude <fcntl.h>.#
ca860 69 6e 63 6c 75 64 65 20 3c 75 6e 69 73 74 64 2e include <unistd.
ca870 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 h>.#include <sys
ca880 2f 74 69 6d 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 /time.h>.#includ
ca890 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 0a 23 69 66 e <errno.h>..#if
ca8a0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
ca8b0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 23 20 69 OCKING_STYLE.# i
ca8c0 6e 63 6c 75 64 65 20 3c 73 79 73 2f 69 6f 63 74 nclude <sys/ioct
ca8d0 6c 2e 68 3e 0a 23 20 69 66 20 4f 53 5f 56 58 57 l.h>.# if OS_VXW
ca8e0 4f 52 4b 53 0a 23 20 20 69 6e 63 6c 75 64 65 20 ORKS.# include
ca8f0 3c 73 65 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20 <semaphore.h>.#
ca900 20 69 6e 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73 include <limits
ca910 2e 68 3e 0a 23 20 65 6c 73 65 0a 23 20 20 69 6e .h>.# else.# in
ca920 63 6c 75 64 65 20 3c 73 79 73 2f 66 69 6c 65 2e clude <sys/file.
ca930 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 h>.# include <s
ca940 79 73 2f 70 61 72 61 6d 2e 68 3e 0a 23 20 20 69 ys/param.h>.# i
ca950 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6f 75 6e nclude <sys/moun
ca960 74 2e 68 3e 0a 23 20 65 6e 64 69 66 0a 23 65 6e t.h>.# endif.#en
ca970 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e dif /* SQLITE_EN
ca980 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
ca990 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 LE */../*.** If
ca9a0 77 65 20 61 72 65 20 74 6f 20 62 65 20 74 68 72 we are to be thr
ca9b0 65 61 64 2d 73 61 66 65 2c 20 69 6e 63 6c 75 64 ead-safe, includ
ca9c0 65 20 74 68 65 20 70 74 68 72 65 61 64 73 20 68 e the pthreads h
ca9d0 65 61 64 65 72 20 61 6e 64 20 64 65 66 69 6e 65 eader and define
ca9e0 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 55 .** the SQLITE_U
ca9f0 4e 49 58 5f 54 48 52 45 41 44 53 20 6d 61 63 72 NIX_THREADS macr
caa00 6f 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 o..*/.#if SQLITE
caa10 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 _THREADSAFE.# de
caa20 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 fine SQLITE_UNIX
caa30 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 _THREADS 1.#endi
caa40 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 f../*.** Default
caa50 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 permissions whe
caa60 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 n creating a new
caa70 20 66 69 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 file.*/.#ifndef
caa80 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
caa90 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 FILE_PERMISSIONS
caaa0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
caab0 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 _DEFAULT_FILE_PE
caac0 52 4d 49 53 53 49 4f 4e 53 20 30 36 34 34 0a 23 RMISSIONS 0644.#
caad0 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 2a 20 44 65 endif../*. ** De
caae0 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e fault permission
caaf0 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 s when creating
cab00 61 75 74 6f 20 70 72 6f 78 79 20 64 69 72 0a 20 auto proxy dir.
cab10 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
cab20 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 E_DEFAULT_PROXYD
cab30 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 IR_PERMISSIONS.#
cab40 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 define SQLITE_D
cab50 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f EFAULT_PROXYDIR_
cab60 50 45 52 4d 49 53 53 49 4f 4e 53 20 30 37 35 35 PERMISSIONS 0755
cab70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d .#endif../*.** M
cab80 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 aximum supported
cab90 20 70 61 74 68 2d 6c 65 6e 67 74 68 2e 0a 2a 2f path-length..*/
caba0 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f 50 41 54 .#define MAX_PAT
cabb0 48 4e 41 4d 45 20 35 31 32 0a 0a 2f 2a 0a 2a 2a HNAME 512../*.**
cabc0 20 4f 6e 6c 79 20 73 65 74 20 74 68 65 20 6c 61 Only set the la
cabd0 73 74 45 72 72 6e 6f 20 69 66 20 74 68 65 20 65 stErrno if the e
cabe0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 61 20 72 rror code is a r
cabf0 65 61 6c 20 65 72 72 6f 72 20 61 6e 64 20 6e 6f eal error and no
cac00 74 20 0a 2a 2a 20 61 20 6e 6f 72 6d 61 6c 20 65 t .** a normal e
cac10 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 20 63 xpected return c
cac20 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 42 55 ode of SQLITE_BU
cac30 53 59 20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 0a SY or SQLITE_OK.
cac40 2a 2f 0a 23 64 65 66 69 6e 65 20 49 53 5f 4c 4f */.#define IS_LO
cac50 43 4b 5f 45 52 52 4f 52 28 78 29 20 20 28 28 78 CK_ERROR(x) ((x
cac60 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 20 26 != SQLITE_OK) &
cac70 26 20 28 78 20 21 3d 20 53 51 4c 49 54 45 5f 42 & (x != SQLITE_B
cac80 55 53 59 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 USY)).../*.** Th
cac90 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 e unixFile struc
caca0 74 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 ture is subclass
cacb0 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 of sqlite3_file
cacc0 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 specific to the
cacd0 20 75 6e 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 unix.** VFS imp
cace0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f lementations..*/
cacf0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
cad00 75 6e 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c unixFile unixFil
cad10 65 3b 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 e;.struct unixFi
cad20 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 le {. sqlite3_i
cad30 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 o_methods const
cad40 2a 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c *pMethod; /* Al
cad50 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20 65 ways the first e
cad60 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 ntry */. struct
cad70 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f unixOpenCnt *pO
cad80 70 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e pen; /* In
cad90 66 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 fo about all ope
cada0 6e 20 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 n fd's on this i
cadb0 6e 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 node */. struct
cadc0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 unixLockInfo *p
cadd0 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e Lock; /* In
cade0 66 6f 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f fo about locks o
cadf0 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a n this inode */.
cae00 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 int h;
cae10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cae20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 /* The file d
cae30 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 escriptor */. i
cae40 6e 74 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 nt dirfd;
cae50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cae60 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 /* File descript
cae70 6f 72 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 or for the direc
cae80 74 6f 72 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e tory */. unsign
cae90 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 ed char locktype
caea0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ; /* Th
caeb0 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 e type of lock h
caec0 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a eld on this fd *
caed0 2f 0a 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e /. int lastErrn
caee0 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o;
caef0 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 /* The unix
caf00 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 errno from the
caf10 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a last I/O error *
caf20 2f 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e /. void *lockin
caf30 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 gContext;
caf40 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 /* Locking
caf50 73 74 79 6c 65 20 73 70 65 63 69 66 69 63 20 73 style specific s
caf60 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 tate */. int op
caf70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 enFlags;
caf80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
caf90 65 20 66 6c 61 67 73 20 73 70 65 63 69 66 69 65 e flags specifie
cafa0 64 20 61 74 20 6f 70 65 6e 20 2a 2f 0a 23 69 66 d at open */.#if
cafb0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
cafc0 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f FE && defined(__
cafd0 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 68 72 65 linux__). pthre
cafe0 61 64 5f 74 20 74 69 64 3b 20 20 20 20 20 20 20 ad_t tid;
caff0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
cb000 68 65 20 74 68 72 65 61 64 20 74 68 61 74 20 22 he thread that "
cb010 6f 77 6e 73 22 20 74 68 69 73 20 75 6e 69 78 46 owns" this unixF
cb020 69 6c 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 ile */.#endif.#i
cb030 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 f OS_VXWORKS. i
cb040 6e 74 20 69 73 44 65 6c 65 74 65 3b 20 20 20 20 nt isDelete;
cb050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb060 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c 6f /* Delete on clo
cb070 73 65 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 se if true */.
cb080 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 struct vxworksFi
cb090 6c 65 49 64 20 2a 70 49 64 3b 20 20 20 20 20 20 leId *pId;
cb0a0 20 2f 2a 20 55 6e 69 71 75 65 20 66 69 6c 65 20 /* Unique file
cb0b0 49 44 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 ID */.#endif.#if
cb0c0 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a ndef NDEBUG. /*
cb0d0 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 The next group
cb0e0 6f 66 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 of variables are
cb0f0 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20 77 used to track w
cb100 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
cb110 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 e. ** transacti
cb120 6f 6e 20 63 6f 75 6e 74 65 72 20 69 6e 20 62 79 on counter in by
cb130 74 65 73 20 32 34 2d 32 37 20 6f 66 20 64 61 74 tes 24-27 of dat
cb140 61 62 61 73 65 20 66 69 6c 65 73 20 61 72 65 20 abase files are
cb150 75 70 64 61 74 65 64 0a 20 20 2a 2a 20 77 68 65 updated. ** whe
cb160 6e 65 76 65 72 20 61 6e 79 20 70 61 72 74 20 6f never any part o
cb170 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 f the database c
cb180 68 61 6e 67 65 73 2e 20 20 41 6e 20 61 73 73 65 hanges. An asse
cb190 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c rtion fault will
cb1a0 0a 20 20 2a 2a 20 6f 63 63 75 72 20 69 66 20 61 . ** occur if a
cb1b0 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 file is updated
cb1c0 20 77 69 74 68 6f 75 74 20 61 6c 73 6f 20 75 70 without also up
cb1d0 64 61 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 dating the trans
cb1e0 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 6f 75 6e action. ** coun
cb1f0 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 74 20 ter. This test
cb200 69 73 20 6d 61 64 65 20 74 6f 20 61 76 6f 69 64 is made to avoid
cb210 20 6e 65 77 20 70 72 6f 62 6c 65 6d 73 20 73 69 new problems si
cb220 6d 69 6c 61 72 20 74 6f 20 74 68 65 0a 20 20 2a milar to the. *
cb230 2a 20 6f 6e 65 20 64 65 73 63 72 69 62 65 64 20 * one described
cb240 62 79 20 74 69 63 6b 65 74 20 23 33 35 38 34 2e by ticket #3584.
cb250 20 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 . */. unsigne
cb260 64 20 63 68 61 72 20 74 72 61 6e 73 43 6e 74 72 d char transCntr
cb270 43 68 6e 67 3b 20 20 20 2f 2a 20 54 72 75 65 20 Chng; /* True
cb280 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 if the transacti
cb290 6f 6e 20 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 on counter chang
cb2a0 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ed */. unsigned
cb2b0 20 63 68 61 72 20 64 62 55 70 64 61 74 65 3b 20 char dbUpdate;
cb2c0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
cb2d0 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20 64 61 f any part of da
cb2e0 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e tabase file chan
cb2f0 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ged */. unsigne
cb300 64 20 63 68 61 72 20 69 6e 4e 6f 72 6d 61 6c 57 d char inNormalW
cb310 72 69 74 65 3b 20 20 20 2f 2a 20 54 72 75 65 20 rite; /* True
cb320 69 66 20 69 6e 20 61 20 6e 6f 72 6d 61 6c 20 77 if in a normal w
cb330 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 2a rite operation *
cb340 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 /.#endif.#ifdef
cb350 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a SQLITE_TEST. /*
cb360 20 49 6e 20 74 65 73 74 20 6d 6f 64 65 2c 20 69 In test mode, i
cb370 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 ncrease the size
cb380 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
cb390 72 65 20 61 20 62 69 74 20 73 6f 20 74 68 61 74 re a bit so that
cb3a0 20 0a 20 20 2a 2a 20 69 74 20 69 73 20 6c 61 72 . ** it is lar
cb3b0 67 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 72 ger than the str
cb3c0 75 63 74 20 43 72 61 73 68 46 69 6c 65 20 64 65 uct CrashFile de
cb3d0 66 69 6e 65 64 20 69 6e 20 74 65 73 74 36 2e 63 fined in test6.c
cb3e0 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 61 50 .. */. char aP
cb3f0 61 64 64 69 6e 67 5b 33 32 5d 3b 0a 23 65 6e 64 adding[32];.#end
cb400 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 if.};../*.** Inc
cb410 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 lude code that i
cb420 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 s common to all
cb430 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a os_*.c files.*/.
cb440 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
cb450 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f Include os_commo
cb460 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c n.h in the middl
cb470 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 2a e of os_unix.c *
cb480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
cb490 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
cb4a0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f Begin file os_co
cb4b0 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
cb4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
cb4e0 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 /*.** 2004 May 2
cb4f0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
cb500 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
cb510 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
cb520 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
cb530 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
cb540 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
cb550 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
cb560 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
cb570 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
cb580 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
cb590 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
cb5a0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
cb5b0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
cb5c0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
cb5d0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
cb5e0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
cb5f0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
cb600 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
cb610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb650 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
cb660 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d file contains m
cb670 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 acros and a litt
cb680 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 le bit of code t
cb690 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f hat is common to
cb6a0 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 .** all of the p
cb6b0 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 latform-specific
cb6c0 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 files (os_*.c)
cb6d0 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 and is #included
cb6e0 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 into those.** f
cb6f0 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 iles..**.** This
cb700 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 file should be
cb710 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 #included by the
cb720 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e os_*.c files on
cb730 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 ly. It is not a
cb740 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 .** general purp
cb750 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e ose header file.
cb760 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 .**.** $Id: os_c
cb770 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 37 20 32 ommon.h,v 1.37 2
cb780 30 30 38 2f 30 35 2f 32 39 20 32 30 3a 32 32 3a 008/05/29 20:22:
cb790 33 37 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 37 shane Exp $.*
cb7a0 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f /.#ifndef _OS_CO
cb7b0 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 MMON_H_.#define
cb7c0 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f _OS_COMMON_H_../
cb7d0 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77 *.** At least tw
cb7e0 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c 69 70 o bugs have slip
cb7f0 70 65 64 20 69 6e 20 62 65 63 61 75 73 65 20 77 ped in because w
cb800 65 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45 e changed the ME
cb810 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 MORY_DEBUG.** ma
cb820 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 cro to SQLITE_DE
cb830 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 BUG and some old
cb840 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 er makefiles hav
cb850 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74 e not yet made t
cb860 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 he.** switch. T
cb870 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 he following cod
cb880 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74 e should catch t
cb890 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 his problem at c
cb8a0 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a ompile-time..*/.
cb8b0 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 #ifdef MEMORY_DE
cb8c0 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65 BUG.# error "The
cb8d0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 MEMORY_DEBUG ma
cb8e0 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e cro is obsolete.
cb8f0 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42 Use SQLITE_DEB
cb900 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e UG instead.".#en
cb910 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20 57 68 65 6e dif.../*. * When
cb920 20 74 65 73 74 69 6e 67 2c 20 74 68 69 73 20 67 testing, this g
cb930 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 73 lobal variable s
cb940 74 6f 72 65 73 20 74 68 65 20 6c 6f 63 61 74 69 tores the locati
cb950 6f 6e 20 6f 66 20 74 68 65 0a 20 2a 20 70 65 6e on of the. * pen
cb960 64 69 6e 67 2d 62 79 74 65 20 69 6e 20 74 68 65 ding-byte in the
cb970 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
cb980 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
cb990 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
cb9a0 49 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 I unsigned int s
cb9b0 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f 62 qlite3_pending_b
cb9c0 79 74 65 20 3d 20 30 78 34 30 30 30 30 30 30 30 yte = 0x40000000
cb9d0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
cb9e0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 SQLITE_DEBUG.SQ
cb9f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
cba00 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 sqlite3OSTrace
cba10 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 = 0;.#define OST
cba20 52 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 RACE1(X)
cba30 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 if( sqlite3OSTr
cba40 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 ace ) sqlite3Deb
cba50 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 ugPrintf(X).#def
cba60 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 ine OSTRACE2(X,Y
cba70 29 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ) if( sqli
cba80 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c te3OSTrace ) sql
cba90 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
cbaa0 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 X,Y).#define OST
cbab0 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 RACE3(X,Y,Z)
cbac0 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 if( sqlite3OSTr
cbad0 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 ace ) sqlite3Deb
cbae0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a ugPrintf(X,Y,Z).
cbaf0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 #define OSTRACE4
cbb00 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 (X,Y,Z,A) if(
cbb10 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
cbb20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
cbb30 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 ntf(X,Y,Z,A).#de
cbb40 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c fine OSTRACE5(X,
cbb50 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c Y,Z,A,B) if( sql
cbb60 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
cbb70 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
cbb80 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 (X,Y,Z,A,B).#def
cbb90 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 ine OSTRACE6(X,Y
cbba0 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 ,Z,A,B,C) \.
cbbb0 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 if(sqlite3OSTrac
cbbc0 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 e) sqlite3DebugP
cbbd0 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c rintf(X,Y,Z,A,B,
cbbe0 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 C).#define OSTRA
cbbf0 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c CE7(X,Y,Z,A,B,C,
cbc00 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 D) \. if(sqli
cbc10 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 te3OSTrace) sqli
cbc20 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
cbc30 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 ,Y,Z,A,B,C,D).#e
cbc40 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 lse.#define OSTR
cbc50 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 ACE1(X).#define
cbc60 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 OSTRACE2(X,Y).#d
cbc70 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 efine OSTRACE3(X
cbc80 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,Y,Z).#define OS
cbc90 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a TRACE4(X,Y,Z,A).
cbca0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 #define OSTRACE5
cbcb0 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 (X,Y,Z,A,B).#def
cbcc0 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 ine OSTRACE6(X,Y
cbcd0 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e ,Z,A,B,C).#defin
cbce0 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a e OSTRACE7(X,Y,Z
cbcf0 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 ,A,B,C,D).#endif
cbd00 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 ../*.** Macros f
cbd10 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 or performance t
cbd20 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c racing. Normall
cbd30 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f y turned off. O
cbd40 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 nly works.** on
cbd50 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a i486 hardware..*
cbd60 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
cbd70 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 PERFORMANCE_TRAC
cbd80 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 E../* .** hwtime
cbd90 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 .h contains inli
cbda0 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 ne assembler cod
cbdb0 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 e for implementi
cbdc0 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 ng .** high-perf
cbdd0 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 ormance timing r
cbde0 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a outines..*/./***
cbdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
cbe00 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 ude hwtime.h in
cbe10 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 the middle of os
cbe20 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a _common.h ******
cbe30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
cbe40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
cbe50 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 n file hwtime.h
cbe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
cbe90 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a * 2008 May 27.**
cbea0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
cbeb0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
cbec0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
cbed0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
cbee0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
cbef0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
cbf00 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
cbf10 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
cbf20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
cbf30 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
cbf40 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
cbf50 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
cbf60 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
cbf70 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
cbf80 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
cbf90 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
cbfa0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
cbfb0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
cbfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cbff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc000 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
cc010 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e e contains inlin
cc020 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 e asm code for r
cc030 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d etrieving "high-
cc040 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 performance".**
cc050 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 counters for x86
cc060 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a class CPUs..**.
cc070 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 ** $Id: hwtime.h
cc080 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 ,v 1.3 2008/08/0
cc090 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 1 14:33:15 shane
cc0a0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 Exp $.*/.#ifnde
cc0b0 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 f _HWTIME_H_.#de
cc0c0 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a fine _HWTIME_H_.
cc0d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
cc0e0 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c wing routine onl
cc0f0 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 y works on penti
cc100 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 um-class (or new
cc110 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a er) processors..
cc120 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 ** It uses the R
cc130 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 DTSC opcode to r
cc140 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f ead the cycle co
cc150 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 unt value out of
cc160 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f the.** processo
cc170 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 r and returns th
cc180 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 at value. This
cc190 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 can be used for
cc1a0 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 high-res.** prof
cc1b0 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 iling..*/.#if (d
cc1c0 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
cc1d0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 || defined(_MSC
cc1e0 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 _VER)) && \.
cc1f0 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 (defined(i386)
cc200 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 || defined(__i3
cc210 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 86__) || defined
cc220 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 (_M_IX86)).. #i
cc230 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 f defined(__GNUC
cc240 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f __).. __inline_
cc250 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 _ sqlite_uint64
cc260 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
cc270 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e id){. unsign
cc280 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 ed int lo, hi;.
cc290 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f __asm__ __vo
cc2a0 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 latile__ ("rdtsc
cc2b0 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 " : "=a" (lo), "
cc2c0 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 =d" (hi));.
cc2d0 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 return (sqlite_u
cc2e0 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c int64)hi << 32 |
cc2f0 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 lo;. }.. #eli
cc300 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 f defined(_MSC_V
cc310 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 ER).. __declspe
cc320 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e c(naked) __inlin
cc330 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 e sqlite_uint64
cc340 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 __cdecl sqlite3H
cc350 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
cc360 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 __asm {.
cc370 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 rdtsc.
cc380 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 ret ; retu
cc390 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a rn value at EDX:
cc3a0 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a EAX. }. }..
cc3b0 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 #endif..#elif
cc3c0 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f (defined(__GNUC_
cc3d0 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f _) && defined(__
cc3e0 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f x86_64__)).. __
cc3f0 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f inline__ sqlite_
cc400 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
cc410 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
cc420 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
cc430 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d val;. __asm
cc440 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 __ __volatile__
cc450 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 ("rdtsc" : "=A"
cc460 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 (val));. re
cc470 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a turn val;. }. .
cc480 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f #elif (defined(_
cc490 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 _GNUC__) && defi
cc4a0 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 ned(__ppc__))..
cc4b0 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
cc4c0 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
cc4d0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
cc4e0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
cc4f0 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a ng long retval;.
cc500 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c unsigned l
cc510 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 ong junk;.
cc520 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 __asm__ __volati
cc530 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 le__ ("\n\.
cc540 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 1: mft
cc550 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 bu %1\n\.
cc560 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 mft
cc570 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 b %L0\n\.
cc580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 mf
cc590 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 tbu %0\n\.
cc5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d cm
cc5b0 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 pw %0,%1\n\.
cc5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cc5d0 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 bne 1b".
cc5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a :
cc5f0 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 "=r" (retval),
cc600 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 "=r" (junk));.
cc610 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 return retva
cc620 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 l;. }..#else..
cc630 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 #error Need imp
cc640 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 lementation of s
cc650 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 qlite3Hwtime() f
cc660 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d or your platform
cc670 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 ... /*. ** To
cc680 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 compile without
cc690 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c implementing sql
cc6a0 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 ite3Hwtime() for
cc6b0 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a your platform,.
cc6c0 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d ** you can rem
cc6d0 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 ove the above #e
cc6e0 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 rror and use the
cc6f0 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 following. **
cc700 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 stub function.
cc710 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 You will lose ti
cc720 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 ming support for
cc730 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 many. ** of th
cc740 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 e debugging and
cc750 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 testing utilitie
cc760 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 s, but it should
cc770 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 at. ** least c
cc780 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a ompile and run..
cc790 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 */.SQLITE_PRIV
cc7a0 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e ATE sqlite_uin
cc7b0 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
cc7c0 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 e(void){ return
cc7d0 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 ((sqlite_uint64)
cc7e0 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 0); }..#endif..#
cc7f0 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 endif /* !define
cc800 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f d(_HWTIME_H_) */
cc810 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
cc820 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e * End of hwtime.
cc830 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
cc840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc860 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
cc870 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
cc880 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
cc890 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a n os_common.h **
cc8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc8b0 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 /..static sqlite
cc8c0 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b _uint64 g_start;
cc8d0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 .static sqlite_u
cc8e0 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b int64 g_elapsed;
cc8f0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 .#define TIMER_S
cc900 54 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 TART g_sta
cc910 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 rt=sqlite3Hwtime
cc920 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 ().#define TIMER
cc930 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 _END g_e
cc940 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 lapsed=sqlite3Hw
cc950 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 time()-g_start.#
cc960 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 define TIMER_ELA
cc970 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 PSED g_elaps
cc980 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 ed.#else.#define
cc990 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 TIMER_START.#de
cc9a0 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 fine TIMER_END.#
cc9b0 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 define TIMER_ELA
cc9c0 50 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 PSED ((sqlit
cc9d0 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 e_uint64)0).#end
cc9e0 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 if../*.** If we
cc9f0 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 compile with the
cca00 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 SQLITE_TEST mac
cca10 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 ro set, then the
cca20 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b following block
cca30 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c .** of code will
cca40 20 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 give us the abi
cca50 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 lity to simulate
cca60 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f a disk I/O erro
cca70 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 r. This.** is u
cca80 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 sed for testing
cca90 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 the I/O recovery
ccaa0 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 logic..*/.#ifde
ccab0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
ccac0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
ccad0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 ite3_io_error_hi
ccae0 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 t = 0;
ccaf0 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 /* Total numbe
ccb00 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 r of I/O Errors
ccb10 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
ccb20 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
ccb30 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 or_hardhit = 0;
ccb40 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
ccb50 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 of non-benign e
ccb60 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f rrors */.SQLITE_
ccb70 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
ccb80 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 io_error_pending
ccb90 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
ccba0 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 Count down to fi
ccbb0 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f rst I/O error */
ccbc0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
ccbd0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
ccbe0 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 _persist = 0;
ccbf0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
ccc00 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 I/O errors persi
ccc10 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 st */.SQLITE_API
ccc20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
ccc30 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 error_benign = 0
ccc40 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
ccc50 65 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 e if errors are
ccc60 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 benign */.SQLITE
ccc70 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
ccc80 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e _diskfull_pendin
ccc90 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 g = 0;.SQLITE_AP
ccca0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 I int sqlite3_di
cccb0 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 skfull = 0;.#def
cccc0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ine SimulateIOEr
cccd0 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c rorBenign(X) sql
ccce0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 ite3_io_error_be
cccf0 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 nign=(X).#define
ccd00 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
ccd10 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 (CODE) \. if(
ccd20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f (sqlite3_io_erro
ccd30 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c r_persist && sql
ccd40 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 ite3_io_error_hi
ccd50 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 t) \. || s
ccd60 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
ccd70 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 pending-- == 1 )
ccd80 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \.
ccd90 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 { local_ioerr(
ccda0 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 ); CODE; }.stati
ccdb0 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 c void local_ioe
ccdc0 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 rr(){. IOTRACE(
ccdd0 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 ("IOERR\n"));.
ccde0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
ccdf0 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 _hit++;. if( !s
cce00 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
cce10 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 benign ) sqlite3
cce20 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 _io_error_hardhi
cce30 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 t++;.}.#define S
cce40 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 imulateDiskfullE
cce50 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 rror(CODE) \.
cce60 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b if( sqlite3_disk
cce70 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 full_pending ){
cce80 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 \. if( sqlit
cce90 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 e3_diskfull_pend
ccea0 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 ing == 1 ){ \.
cceb0 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 local_ioerr
ccec0 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c (); \. sql
cced0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 ite3_diskfull =
ccee0 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 1; \. sqli
ccef0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
ccf00 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 = 1; \. C
ccf10 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 ODE; \. }els
ccf20 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 e{ \. sqli
ccf30 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
ccf40 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d ding--; \. }
ccf50 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 \. }.#else.#d
ccf60 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
ccf70 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 ErrorBenign(X).#
ccf80 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
ccf90 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e OError(A).#defin
ccfa0 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 e SimulateDiskfu
ccfb0 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 llError(A).#endi
ccfc0 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 f../*.** When te
ccfd0 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f sting, keep a co
ccfe0 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 unt of the numbe
ccff0 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e r of open files.
cd000 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
cd010 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
cd020 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 I int sqlite3_op
cd030 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 en_file_count =
cd040 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 0;.#define OpenC
cd050 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 ounter(X) sqlit
cd060 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 e3_open_file_cou
cd070 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 nt+=(X).#else.#d
cd080 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 efine OpenCounte
cd090 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e r(X).#endif..#en
cd0a0 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
cd0b0 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a _OS_COMMON_H_) *
cd0c0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
cd0d0 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d ** End of os_com
cd0e0 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mon.h **********
cd0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd110 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
cd120 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
cd130 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
cd140 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a in os_unix.c ***
cd150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd160 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 */../*.** Define
cd170 20 76 61 72 69 6f 75 73 20 6d 61 63 72 6f 73 20 various macros
cd180 74 68 61 74 20 61 72 65 20 6d 69 73 73 69 6e 67 that are missing
cd190 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73 74 65 from some syste
cd1a0 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f ms..*/.#ifndef O
cd1b0 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 _LARGEFILE.# def
cd1c0 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 ine O_LARGEFILE
cd1d0 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 0.#endif.#ifdef
cd1e0 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c SQLITE_DISABLE_L
cd1f0 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c 41 52 FS.# undef O_LAR
cd200 47 45 46 49 4c 45 0a 23 20 64 65 66 69 6e 65 20 GEFILE.# define
cd210 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a 23 65 O_LARGEFILE 0.#e
cd220 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f 4e ndif.#ifndef O_N
cd230 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69 6e 65 OFOLLOW.# define
cd240 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a 23 65 O_NOFOLLOW 0.#e
cd250 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f 42 ndif.#ifndef O_B
cd260 49 4e 41 52 59 0a 23 20 64 65 66 69 6e 65 20 4f INARY.# define O
cd270 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e 64 69 66 _BINARY 0.#endif
cd280 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a 47 50 ../*.** The DJGP
cd290 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76 69 72 P compiler envir
cd2a0 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d 6f 73 onment looks mos
cd2b0 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c 20 62 tly like Unix, b
cd2c0 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73 20 74 ut it.** lacks t
cd2d0 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65 he fcntl() syste
cd2e0 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65 64 65 m call. So rede
cd2f0 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 74 6f 20 fine fcntl() to
cd300 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 be something.**
cd310 74 68 61 74 20 61 6c 77 61 79 73 20 73 75 63 63 that always succ
cd320 65 65 64 73 2e 20 20 54 68 69 73 20 6d 65 61 6e eeds. This mean
cd330 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67 20 64 s that locking d
cd340 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 75 6e oes not occur un
cd350 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20 20 42 der.** DJGPP. B
cd360 75 74 20 69 74 20 69 73 20 44 4f 53 20 2d 20 77 ut it is DOS - w
cd370 68 61 74 20 64 69 64 20 79 6f 75 20 65 78 70 65 hat did you expe
cd380 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f ct?.*/.#ifdef __
cd390 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69 6e 65 DJGPP__.# define
cd3a0 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20 30 0a fcntl(A,B,C) 0.
cd3b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
cd3c0 65 20 74 68 72 65 61 64 69 64 20 6d 61 63 72 6f e threadid macro
cd3d0 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 68 65 resolves to the
cd3e0 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20 74 6f thread-id or to
cd3f0 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a 2a 2a 0. Used for.**
cd400 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 testing and deb
cd410 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a ugging only..*/.
cd420 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
cd430 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20 74 68 DSAFE.#define th
cd440 72 65 61 64 69 64 20 70 74 68 72 65 61 64 5f 73 readid pthread_s
cd450 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 65 66 elf().#else.#def
cd460 69 6e 65 20 74 68 72 65 61 64 69 64 20 30 0a 23 ine threadid 0.#
cd470 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 48 65 endif.../*.** He
cd480 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 lper functions t
cd490 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c o obtain and rel
cd4a0 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c 6f 62 inquish the glob
cd4b0 61 6c 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 al mutex..*/.sta
cd4c0 74 69 63 20 76 6f 69 64 20 75 6e 69 78 45 6e 74 tic void unixEnt
cd4d0 65 72 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 erMutex(void){.
cd4e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
cd4f0 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 nter(sqlite3Mute
cd500 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
cd510 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
cd520 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f R));.}.static vo
cd530 69 64 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 id unixLeaveMute
cd540 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 x(void){. sqlit
cd550 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 e3_mutex_leave(s
cd560 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
cd570 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
cd580 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d ATIC_MASTER));.}
cd590 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ...#ifdef SQLITE
cd5a0 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c _DEBUG./*.** Hel
cd5b0 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 per function for
cd5c0 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 74 72 printing out tr
cd5d0 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ace information
cd5e0 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a from debugging.*
cd5f0 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68 69 73 * binaries. This
cd600 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 74 72 returns the str
cd610 69 6e 67 20 72 65 70 72 65 73 65 74 61 74 69 6f ing represetatio
cd620 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 n of the supplie
cd630 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63 d.** integer loc
cd640 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 k-type..*/.stati
cd650 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f c const char *lo
cd660 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c cktypeName(int l
cd670 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 69 74 ocktype){. swit
cd680 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a ch( locktype ){.
cd690 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 case NO_LOCK:
cd6a0 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20 return "NONE";.
cd6b0 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c 4f 43 case SHARED_LOC
cd6c0 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41 52 45 K: return "SHARE
cd6d0 44 22 3b 0a 20 20 63 61 73 65 20 52 45 53 45 52 D";. case RESER
cd6e0 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e VED_LOCK: return
cd6f0 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20 20 63 "RESERVED";. c
cd700 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b ase PENDING_LOCK
cd710 3a 20 72 65 74 75 72 6e 20 22 50 45 4e 44 49 4e : return "PENDIN
cd720 47 22 3b 0a 20 20 63 61 73 65 20 45 58 43 4c 55 G";. case EXCLU
cd730 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 SIVE_LOCK: retur
cd740 6e 20 22 45 58 43 4c 55 53 49 56 45 22 3b 0a 20 n "EXCLUSIVE";.
cd750 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 45 52 52 }. return "ERR
cd760 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 OR";.}.#endif..#
cd770 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c 4f 43 ifdef SQLITE_LOC
cd780 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a 20 50 72 K_TRACE./*.** Pr
cd790 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 6d 61 74 int out informat
cd7a0 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c 20 6c 6f ion about all lo
cd7b0 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 cking operations
cd7c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
cd7d0 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 tine is used for
cd7e0 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 troubleshooting
cd7f0 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c 74 69 74 locks on multit
cd800 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c 61 74 66 hreaded.** platf
cd810 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 20 62 79 orms. Enable by
cd820 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 compiling with
cd830 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 4c 4f 43 the -DSQLITE_LOC
cd840 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f 6d 6d 61 K_TRACE.** comma
cd850 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f 6e 20 6f nd-line option o
cd860 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 n the compiler.
cd870 20 54 68 69 73 20 63 6f 64 65 20 69 73 20 6e 6f This code is no
cd880 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 6e 65 64 rmally.** turned
cd890 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 off..*/.static
cd8a0 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 28 69 6e int lockTrace(in
cd8b0 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c 20 73 74 t fd, int op, st
cd8c0 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 29 7b 0a ruct flock *p){.
cd8d0 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 6d 65 2c char *zOpName,
cd8e0 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e 74 20 73 *zType;. int s
cd8f0 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 45 72 72 ;. int savedErr
cd900 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 46 5f no;. if( op==F_
cd910 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 GETLK ){. zOp
cd920 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b 22 3b 0a Name = "GETLK";.
cd930 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d }else if( op==
cd940 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a F_SETLK ){. z
cd950 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 4c 4b 22 OpName = "SETLK"
cd960 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
cd970 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 2c = fcntl(fd, op,
cd980 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 p);. sqlite3
cd990 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e DebugPrintf("fcn
cd9a0 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 20 25 64 tl unknown %d %d
cd9b0 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f 70 2c 20 %d\n", fd, op,
cd9c0 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 s);. return s
cd9d0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 6c ;. }. if( p->l
cd9e0 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 _type==F_RDLCK )
cd9f0 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 52 {. zType = "R
cda00 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 DLCK";. }else i
cda10 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f f( p->l_type==F_
cda20 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 WRLCK ){. zTy
cda30 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 pe = "WRLCK";.
cda40 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74 }else if( p->l_t
cda50 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a ype==F_UNLCK ){.
cda60 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c zType = "UNL
cda70 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 CK";. }else{.
cda80 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 assert( 0 );.
cda90 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e }. assert( p->
cdaa0 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45 4b 5f 53 l_whence==SEEK_S
cdab0 45 54 20 29 3b 0a 20 20 73 20 3d 20 66 63 6e 74 ET );. s = fcnt
cdac0 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 l(fd, op, p);.
cdad0 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 65 72 72 savedErrno = err
cdae0 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 no;. sqlite3Deb
cdaf0 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 ugPrintf("fcntl
cdb00 25 64 20 25 64 20 25 73 20 25 73 20 25 64 20 25 %d %d %s %s %d %
cdb10 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 d %d %d\n",.
cdb20 20 74 68 72 65 61 64 69 64 2c 20 66 64 2c 20 7a threadid, fd, z
cdb30 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65 2c 20 28 OpName, zType, (
cdb40 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72 74 2c 20 int)p->l_start,
cdb50 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e 2c 0a 20 (int)p->l_len,.
cdb60 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c 5f 70 69 (int)p->l_pi
cdb70 64 2c 20 73 29 3b 0a 20 20 69 66 28 20 73 3d 3d d, s);. if( s==
cdb80 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46 5f 53 45 (-1) && op==F_SE
cdb90 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f 74 79 70 TLK && (p->l_typ
cdba0 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c 20 70 2d e==F_RDLCK || p-
cdbb0 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b >l_type==F_WRLCK
cdbc0 29 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 ) ){. struct
cdbd0 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20 20 6c 32 flock l2;. l2
cdbe0 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63 6e 74 6c = *p;. fcntl
cdbf0 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c (fd, F_GETLK, &l
cdc00 32 29 3b 0a 20 20 20 20 69 66 28 20 6c 32 2e 6c 2);. if( l2.l
cdc10 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 _type==F_RDLCK )
cdc20 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 {. zType =
cdc30 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c "RDLCK";. }el
cdc40 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 se if( l2.l_type
cdc50 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20 ==F_WRLCK ){.
cdc60 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c 43 zType = "WRLC
cdc70 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 K";. }else if
cdc80 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 ( l2.l_type==F_U
cdc90 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 NLCK ){. zT
cdca0 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 ype = "UNLCK";.
cdcb0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
cdcc0 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 assert( 0 );.
cdcd0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 }. sqlite3De
cdce0 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c bugPrintf("fcntl
cdcf0 2d 66 61 69 6c 75 72 65 2d 72 65 61 73 6f 6e 3a -failure-reason:
cdd00 20 25 73 20 25 64 20 25 64 20 25 64 5c 6e 22 2c %s %d %d %d\n",
cdd10 0a 20 20 20 20 20 20 20 7a 54 79 70 65 2c 20 28 . zType, (
cdd20 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72 74 2c 20 int)l2.l_start,
cdd30 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e 2c 20 28 (int)l2.l_len, (
cdd40 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29 3b 0a 20 int)l2.l_pid);.
cdd50 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20 73 61 76 }. errno = sav
cdd60 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65 74 75 72 edErrno;. retur
cdd70 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 66 n s;.}.#define f
cdd80 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63 65 0a 23 cntl lockTrace.#
cdd90 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
cdda0 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f 0a 0a 0a LOCK_TRACE */...
cddb0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
cddc0 69 6e 65 20 74 72 61 6e 73 6c 61 74 65 73 20 61 ine translates a
cddd0 20 73 74 61 6e 64 61 72 64 20 50 4f 53 49 58 20 standard POSIX
cdde0 65 72 72 6e 6f 20 63 6f 64 65 20 69 6e 74 6f 20 errno code into
cddf0 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 75 73 65 something.** use
cde00 66 75 6c 20 74 6f 20 74 68 65 20 63 6c 69 65 6e ful to the clien
cde10 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 ts of the sqlite
cde20 33 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 53 70 3 functions. Sp
cde30 65 63 69 66 69 63 61 6c 6c 79 2c 20 69 74 20 69 ecifically, it i
cde40 73 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 74 6f s.** intended to
cde50 20 74 72 61 6e 73 6c 61 74 65 20 61 20 76 61 72 translate a var
cde60 69 65 74 79 20 6f 66 20 22 74 72 79 20 61 67 61 iety of "try aga
cde70 69 6e 22 20 65 72 72 6f 72 73 20 69 6e 74 6f 20 in" errors into
cde80 53 51 4c 49 54 45 5f 42 55 53 59 0a 2a 2a 20 61 SQLITE_BUSY.** a
cde90 6e 64 20 61 20 76 61 72 69 65 74 79 20 6f 66 20 nd a variety of
cdea0 22 70 6c 65 61 73 65 20 63 6c 6f 73 65 20 74 68 "please close th
cdeb0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
cdec0 72 20 4e 4f 57 22 20 65 72 72 6f 72 73 20 69 6e r NOW" errors in
cded0 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f to .** SQLITE_IO
cdee0 45 52 52 0a 2a 2a 20 0a 2a 2a 20 45 72 72 6f 72 ERR.** .** Error
cdef0 73 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c s during initial
cdf00 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 6f 63 6b 73 ization of locks
cdf10 2c 20 6f 72 20 66 69 6c 65 20 73 79 73 74 65 6d , or file system
cdf20 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6c 6f 63 support for loc
cdf30 6b 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 68 61 ks,.** should ha
cdf40 6e 64 6c 65 20 45 4e 4f 4c 43 4b 2c 20 45 4e 4f ndle ENOLCK, ENO
cdf50 54 53 55 50 2c 20 45 4f 50 4e 4f 54 53 55 50 50 TSUP, EOPNOTSUPP
cdf60 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2f 0a separately..*/.
cdf70 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 static int sqlit
cdf80 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
cdf90 72 72 6f 72 28 69 6e 74 20 70 6f 73 69 78 45 72 rror(int posixEr
cdfa0 72 6f 72 2c 20 69 6e 74 20 73 71 6c 69 74 65 49 ror, int sqliteI
cdfb0 4f 45 72 72 29 20 7b 0a 20 20 73 77 69 74 63 68 OErr) {. switch
cdfc0 20 28 70 6f 73 69 78 45 72 72 6f 72 29 20 7b 0a (posixError) {.
cdfd0 20 20 63 61 73 65 20 30 3a 20 0a 20 20 20 20 72 case 0: . r
cdfe0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
cdff0 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 41 47 . . case EAG
ce000 41 49 4e 3a 0a 20 20 63 61 73 65 20 45 54 49 4d AIN:. case ETIM
ce010 45 44 4f 55 54 3a 0a 20 20 63 61 73 65 20 45 42 EDOUT:. case EB
ce020 55 53 59 3a 0a 20 20 63 61 73 65 20 45 49 4e 54 USY:. case EINT
ce030 52 3a 0a 20 20 63 61 73 65 20 45 4e 4f 4c 43 4b R:. case ENOLCK
ce040 3a 20 20 0a 20 20 20 20 2f 2a 20 72 61 6e 64 6f : . /* rando
ce050 6d 20 4e 46 53 20 72 65 74 72 79 20 65 72 72 6f m NFS retry erro
ce060 72 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 r, unless during
ce070 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 file system sup
ce080 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 port . * int
ce090 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 rospection, in w
ce0a0 68 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79 hich it actually
ce0b0 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 means what it s
ce0c0 61 79 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 ays */. retur
ce0d0 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 n SQLITE_BUSY;.
ce0e0 20 20 20 0a 20 20 63 61 73 65 20 45 41 43 43 45 . case EACCE
ce0f0 53 3a 20 0a 20 20 20 20 2f 2a 20 45 41 43 43 45 S: . /* EACCE
ce100 53 20 69 73 20 6c 69 6b 65 20 45 41 47 41 49 4e S is like EAGAIN
ce110 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 during locking
ce120 6f 70 65 72 61 74 69 6f 6e 73 2c 20 62 75 74 20 operations, but
ce130 6e 6f 74 20 61 6e 79 20 6f 74 68 65 72 20 74 69 not any other ti
ce140 6d 65 2a 2f 0a 20 20 20 20 69 66 28 20 28 73 71 me*/. if( (sq
ce150 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c liteIOErr == SQL
ce160 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 20 ITE_IOERR_LOCK)
ce170 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 || ..(sqliteIOEr
ce180 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 r == SQLITE_IOER
ce190 52 5f 55 4e 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 R_UNLOCK) || ..(
ce1a0 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 sqliteIOErr == S
ce1b0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f QLITE_IOERR_RDLO
ce1c0 43 4b 29 20 7c 7c 0a 09 28 73 71 6c 69 74 65 49 CK) ||..(sqliteI
ce1d0 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 OErr == SQLITE_I
ce1e0 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 OERR_CHECKRESERV
ce1f0 45 44 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 20 EDLOCK) ){.
ce200 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
ce210 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f USY;. }. /
ce220 2a 20 65 6c 73 65 20 66 61 6c 6c 20 74 68 72 6f * else fall thro
ce230 75 67 68 20 2a 2f 0a 20 20 63 61 73 65 20 45 50 ugh */. case EP
ce240 45 52 4d 3a 20 0a 20 20 20 20 72 65 74 75 72 6e ERM: . return
ce250 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 SQLITE_PERM;.
ce260 20 20 0a 20 20 63 61 73 65 20 45 44 45 41 44 4c . case EDEADL
ce270 4b 3a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 K:. return SQ
ce280 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b LITE_IOERR_BLOCK
ce290 45 44 3b 0a 20 20 20 20 0a 23 69 66 20 45 4f 50 ED;. .#if EOP
ce2a0 4e 4f 54 53 55 50 50 21 3d 45 4e 4f 54 53 55 50 NOTSUPP!=ENOTSUP
ce2b0 0a 20 20 63 61 73 65 20 45 4f 50 4e 4f 54 53 55 . case EOPNOTSU
ce2c0 50 50 3a 20 0a 20 20 20 20 2f 2a 20 73 6f 6d 65 PP: . /* some
ce2d0 74 68 69 6e 67 20 77 65 6e 74 20 74 65 72 72 69 thing went terri
ce2e0 62 6c 79 20 61 77 72 79 2c 20 75 6e 6c 65 73 73 bly awry, unless
ce2f0 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 during file sys
ce300 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20 tem support .
ce310 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f * introspectio
ce320 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 61 n, in which it a
ce330 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 ctually means wh
ce340 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 23 65 at it says */.#e
ce350 6e 64 69 66 0a 23 69 66 64 65 66 20 45 4e 4f 54 ndif.#ifdef ENOT
ce360 53 55 50 0a 20 20 63 61 73 65 20 45 4e 4f 54 53 SUP. case ENOTS
ce370 55 50 3a 20 0a 20 20 20 20 2f 2a 20 69 6e 76 61 UP: . /* inva
ce380 6c 69 64 20 66 64 2c 20 75 6e 6c 65 73 73 20 64 lid fd, unless d
ce390 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65 uring file syste
ce3a0 6d 20 73 75 70 70 6f 72 74 20 69 6e 74 72 6f 73 m support intros
ce3b0 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 pection, in whic
ce3c0 68 20 0a 20 20 20 20 20 2a 20 69 74 20 61 63 74 h . * it act
ce3d0 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 ually means what
ce3e0 20 69 74 20 73 61 79 73 20 2a 2f 0a 23 65 6e 64 it says */.#end
ce3f0 69 66 0a 20 20 63 61 73 65 20 45 49 4f 3a 0a 20 if. case EIO:.
ce400 20 63 61 73 65 20 45 42 41 44 46 3a 0a 20 20 63 case EBADF:. c
ce410 61 73 65 20 45 49 4e 56 41 4c 3a 0a 20 20 63 61 ase EINVAL:. ca
ce420 73 65 20 45 4e 4f 54 43 4f 4e 4e 3a 0a 20 20 63 se ENOTCONN:. c
ce430 61 73 65 20 45 4e 4f 44 45 56 3a 0a 20 20 63 61 ase ENODEV:. ca
ce440 73 65 20 45 4e 58 49 4f 3a 0a 20 20 63 61 73 65 se ENXIO:. case
ce450 20 45 4e 4f 45 4e 54 3a 0a 20 20 63 61 73 65 20 ENOENT:. case
ce460 45 53 54 41 4c 45 3a 0a 20 20 63 61 73 65 20 45 ESTALE:. case E
ce470 4e 4f 53 59 53 3a 0a 20 20 20 20 2f 2a 20 74 68 NOSYS:. /* th
ce480 65 73 65 20 73 68 6f 75 6c 64 20 66 6f 72 63 65 ese should force
ce490 20 74 68 65 20 63 6c 69 65 6e 74 20 74 6f 20 63 the client to c
ce4a0 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e lose the file an
ce4b0 64 20 72 65 63 6f 6e 6e 65 63 74 20 2a 2f 0a 20 d reconnect */.
ce4c0 20 20 20 0a 20 20 64 65 66 61 75 6c 74 3a 20 0a . default: .
ce4d0 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
ce4e0 65 49 4f 45 72 72 3b 0a 20 20 7d 0a 7d 0a 0a 0a eIOErr;. }.}...
ce4f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
ce500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ce510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ce520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ce530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ce540 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
ce550 2a 2a 2a 20 42 65 67 69 6e 20 55 6e 69 71 75 65 *** Begin Unique
ce560 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74 79 File ID Utility
ce570 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b 73 Used By VxWorks
ce580 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
ce590 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f 73 74 20 76 .**.** On most v
ce5a0 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e 69 78 2c ersions of unix,
ce5b0 20 77 65 20 63 61 6e 20 67 65 74 20 61 20 75 6e we can get a un
ce5c0 69 71 75 65 20 49 44 20 66 6f 72 20 61 20 66 69 ique ID for a fi
ce5d0 6c 65 20 62 79 20 63 6f 6e 63 61 74 65 6e 61 74 le by concatenat
ce5e0 69 6e 67 0a 2a 2a 20 74 68 65 20 64 65 76 69 63 ing.** the devic
ce5f0 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 e number and the
ce600 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 20 inode number.
ce610 42 75 74 20 74 68 69 73 20 64 6f 65 73 20 6e 6f But this does no
ce620 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57 6f 72 6b t work on VxWork
ce630 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57 6f 72 6b 73 s..** On VxWorks
ce640 2c 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65 20 , a unique file
ce650 69 64 20 6d 75 73 74 20 62 65 20 62 61 73 65 64 id must be based
ce660 20 6f 6e 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 on the canonica
ce670 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 0a 2a l filename..**.*
ce680 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 * A pointer to a
ce690 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
ce6a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
ce6b0 63 74 75 72 65 20 63 61 6e 20 62 65 20 75 73 65 cture can be use
ce6c0 64 20 61 73 20 61 0a 2a 2a 20 75 6e 69 71 75 65 d as a.** unique
ce6d0 20 66 69 6c 65 20 49 44 20 69 6e 20 56 78 57 6f file ID in VxWo
ce6e0 72 6b 73 2e 20 20 45 61 63 68 20 69 6e 73 74 61 rks. Each insta
ce6f0 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 nce of this stru
ce700 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a cture contains.*
ce710 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 * a copy of the
ce720 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 canonical filena
ce730 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c me. There is al
ce740 73 6f 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 so a reference c
ce750 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54 68 65 20 73 ount. .** The s
ce760 74 72 75 63 74 75 72 65 20 69 73 20 72 65 63 6c tructure is recl
ce770 61 69 6d 65 64 20 77 68 65 6e 20 74 68 65 20 6e aimed when the n
ce780 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 umber of pointer
ce790 73 20 74 6f 20 69 74 20 64 72 6f 70 73 20 74 6f s to it drops to
ce7a0 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 .** zero..**.**
ce7b0 54 68 65 72 65 20 61 72 65 20 6e 65 76 65 72 20 There are never
ce7c0 76 65 72 79 20 6d 61 6e 79 20 66 69 6c 65 73 20 very many files
ce7d0 6f 70 65 6e 20 61 74 20 6f 6e 65 20 74 69 6d 65 open at one time
ce7e0 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 20 61 72 65 and lookups are
ce7f0 20 6e 6f 74 0a 2a 2a 20 61 20 70 65 72 66 6f 72 not.** a perfor
ce800 6d 61 6e 63 65 2d 63 72 69 74 69 63 61 6c 20 70 mance-critical p
ce810 61 74 68 2c 20 73 6f 20 69 74 20 69 73 20 73 75 ath, so it is su
ce820 66 66 69 63 69 65 6e 74 20 74 6f 20 70 75 74 20 fficient to put
ce830 74 68 65 73 65 0a 2a 2a 20 73 74 72 75 63 74 75 these.** structu
ce840 72 65 73 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 res on a linked
ce850 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 list..*/.struct
ce860 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 7b 0a vxworksFileId {.
ce870 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 struct vxworks
ce880 46 69 6c 65 49 64 20 2a 70 4e 65 78 74 3b 20 20 FileId *pNext;
ce890 2f 2a 20 4e 65 78 74 20 69 6e 20 61 20 6c 69 73 /* Next in a lis
ce8a0 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f t of them all */
ce8b0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
ce8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce8d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 /* Number of re
ce8e0 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 ferences to this
ce8f0 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e one */. int nN
ce900 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
ce910 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 /* Lengt
ce920 68 20 6f 66 20 74 68 65 20 7a 43 61 6e 6f 6e 69 h of the zCanoni
ce930 63 61 6c 4e 61 6d 65 5b 5d 20 73 74 72 69 6e 67 calName[] string
ce940 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 61 6e */. char *zCan
ce950 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20 20 20 20 20 onicalName;
ce960 20 20 20 20 2f 2a 20 43 61 6e 6f 6e 69 63 61 6c /* Canonical
ce970 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 7d 3b 0a filename */.};.
ce980 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
ce990 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75 6e 69 71 75 /* .** All uniqu
ce9a0 65 20 66 69 6c 65 6e 61 6d 65 73 20 61 72 65 20 e filenames are
ce9b0 68 65 6c 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 held on a linked
ce9c0 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 20 list headed by
ce9d0 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 this.** variable
ce9e0 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 :.*/.static stru
ce9f0 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 ct vxworksFileId
cea00 20 2a 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 *vxworksFileLis
cea10 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 t = 0;../*.** Si
cea20 6d 70 6c 69 66 79 20 61 20 66 69 6c 65 6e 61 6d mplify a filenam
cea30 65 20 69 6e 74 6f 20 69 74 73 20 63 61 6e 6f 6e e into its canon
cea40 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a 20 62 79 20 ical form.** by
cea50 6d 61 6b 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f making the follo
cea60 77 69 6e 67 20 63 68 61 6e 67 65 73 3a 0a 2a 2a wing changes:.**
cea70 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76 69 6e 67 20 .** * removing
cea80 61 6e 79 20 74 72 61 69 6c 69 6e 67 20 61 6e 64 any trailing and
cea90 20 64 75 70 6c 69 63 61 74 65 20 2f 0a 2a 2a 20 duplicate /.**
ceaa0 20 2a 20 63 6f 6e 76 65 72 74 20 2f 2e 2f 20 69 * convert /./ i
ceab0 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 20 20 2a nto just /.** *
ceac0 20 63 6f 6e 76 65 72 74 20 2f 41 2f 2e 2e 2f 20 convert /A/../
cead0 77 68 65 72 65 20 41 20 69 73 20 61 6e 79 20 73 where A is any s
ceae0 69 6d 70 6c 65 20 6e 61 6d 65 20 69 6e 74 6f 20 imple name into
ceaf0 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a 20 43 68 61 just /.**.** Cha
ceb00 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 6e nges are made in
ceb10 2d 70 6c 61 63 65 2e 20 20 52 65 74 75 72 6e 20 -place. Return
ceb20 74 68 65 20 6e 65 77 20 6e 61 6d 65 20 6c 65 6e the new name len
ceb30 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f gth..**.** The o
ceb40 72 69 67 69 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 riginal filename
ceb50 20 69 73 20 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d is in z[0..n-1]
ceb60 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 . Return the nu
ceb70 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 mber of.** chara
ceb80 63 74 65 72 73 20 69 6e 20 74 68 65 20 73 69 6d cters in the sim
ceb90 70 6c 69 66 69 65 64 20 6e 61 6d 65 2e 0a 2a 2f plified name..*/
ceba0 0a 73 74 61 74 69 63 20 69 6e 74 20 76 78 77 6f .static int vxwo
cebb0 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28 rksSimplifyName(
cebc0 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b char *z, int n){
cebd0 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 77 . int i, j;. w
cebe0 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 7a 5b 6e hile( n>1 && z[n
cebf0 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e 2d 2d 3b -1]=='/' ){ n--;
cec00 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 }. for(i=j=0;
cec10 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 i<n; i++){. i
cec20 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a f( z[i]=='/' ){.
cec30 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d if( z[i+1]
cec40 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 75 65 =='/' ) continue
cec50 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b ;. if( z[i+
cec60 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 32 3c 6e 1]=='.' && i+2<n
cec70 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2f 27 20 && z[i+2]=='/'
cec80 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20 ){. i +=
cec90 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 1;. conti
ceca0 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nue;. }.
cecb0 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 if( z[i+1]=='
cecc0 2e 27 20 26 26 20 69 2b 33 3c 6e 20 26 26 20 7a .' && i+3<n && z
cecd0 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26 26 20 7a 5b [i+2]=='.' && z[
cece0 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 i+3]=='/' ){.
cecf0 20 20 20 20 20 77 68 69 6c 65 28 20 6a 3e 30 20 while( j>0
ced00 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27 2f 27 20 29 && z[j-1]!='/' )
ced10 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 { j--; }.
ced20 20 69 66 28 20 6a 3e 30 20 29 7b 20 6a 2d 2d 3b if( j>0 ){ j--;
ced30 20 7d 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20 }. i +=
ced40 32 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 2;. conti
ced50 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nue;. }.
ced60 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 }. z[j++] =
ced70 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6a 5d z[i];. }. z[j]
ced80 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6a = 0;. return j
ced90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 ;.}../*.** Find
ceda0 61 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49 44 a unique file ID
cedb0 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 61 for the given a
cedc0 62 73 6f 6c 75 74 65 20 70 61 74 68 6e 61 6d 65 bsolute pathname
cedd0 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 . Return.** a p
cede0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 76 78 ointer to the vx
cedf0 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 worksFileId obje
cee00 63 74 2e 20 20 54 68 69 73 20 70 6f 69 6e 74 65 ct. This pointe
cee10 72 20 69 73 20 74 68 65 20 75 6e 69 71 75 65 0a r is the unique.
cee20 2a 2a 20 66 69 6c 65 20 49 44 2e 0a 2a 2a 0a 2a ** file ID..**.*
cee30 2a 20 54 68 65 20 6e 52 65 66 20 66 69 65 6c 64 * The nRef field
cee40 20 6f 66 20 74 68 65 20 76 78 77 6f 72 6b 73 46 of the vxworksF
cee50 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73 20 ileId object is
cee60 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 65 66 6f incremented befo
cee70 72 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74 re.** the object
cee80 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 41 is returned. A
cee90 20 6e 65 77 20 76 78 77 6f 72 6b 73 46 69 6c 65 new vxworksFile
ceea0 49 64 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 Id object is cre
ceeb0 61 74 65 64 0a 2a 2a 20 61 6e 64 20 61 64 64 65 ated.** and adde
ceec0 64 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 d to the global
ceed0 6c 69 73 74 20 69 66 20 6e 65 63 65 73 73 61 72 list if necessar
ceee0 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 y..**.** If a me
ceef0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
cef00 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 error occurs, re
cef10 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 turn NULL..*/.st
cef20 61 74 69 63 20 73 74 72 75 63 74 20 76 78 77 6f atic struct vxwo
cef30 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f 72 rksFileId *vxwor
cef40 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 63 6f 6e ksFindFileId(con
cef50 73 74 20 63 68 61 72 20 2a 7a 41 62 73 6f 6c 75 st char *zAbsolu
cef60 74 65 4e 61 6d 65 29 7b 0a 20 20 73 74 72 75 63 teName){. struc
cef70 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
cef80 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 2f *pNew; /
cef90 2a 20 73 65 61 72 63 68 20 6b 65 79 20 61 6e 64 * search key and
cefa0 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a 2f 0a new file ID */.
cefb0 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 struct vxworks
cefc0 46 69 6c 65 49 64 20 2a 70 43 61 6e 64 69 64 61 FileId *pCandida
cefd0 74 65 3b 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f te; /* For loo
cefe0 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 ping over existi
ceff0 6e 67 20 66 69 6c 65 20 49 44 73 20 2a 2f 0a 20 ng file IDs */.
cf000 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 int n;
cf010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cf020 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f /* Length o
cf030 66 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 20 f zAbsoluteName
cf040 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 61 73 73 string */.. ass
cf050 65 72 74 28 20 7a 41 62 73 6f 6c 75 74 65 4e 61 ert( zAbsoluteNa
cf060 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 3b 0a 20 20 me[0]=='/' );.
cf070 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 n = (int)strlen(
cf080 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 3b 0a zAbsoluteName);.
cf090 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
cf0a0 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 _malloc( sizeof(
cf0b0 2a 70 4e 65 77 29 20 2b 20 28 6e 2b 31 29 20 29 *pNew) + (n+1) )
cf0c0 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 ;. if( pNew==0
cf0d0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e ) return 0;. pN
cf0e0 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 ew->zCanonicalNa
cf0f0 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70 4e 65 me = (char*)&pNe
cf100 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 28 70 w[1];. memcpy(p
cf110 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e New->zCanonicalN
cf120 61 6d 65 2c 20 7a 41 62 73 6f 6c 75 74 65 4e 61 ame, zAbsoluteNa
cf130 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20 6e 20 3d 20 me, n+1);. n =
cf140 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e vxworksSimplifyN
cf150 61 6d 65 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e ame(pNew->zCanon
cf160 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3b 0a 0a 20 icalName, n);..
cf170 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 /* Search for a
cf180 6e 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 n existing entry
cf190 20 74 68 61 74 20 6d 61 74 63 68 69 6e 67 20 74 that matching t
cf1a0 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 6e 61 6d he canonical nam
cf1b0 65 2e 0a 20 20 2a 2a 20 49 66 20 66 6f 75 6e 64 e.. ** If found
cf1c0 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 , increment the
cf1d0 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 reference count
cf1e0 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 and return a poi
cf1f0 6e 74 65 72 20 74 6f 0a 20 20 2a 2a 20 74 68 65 nter to. ** the
cf200 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20 49 existing file I
cf210 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e D.. */. unixEn
cf220 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 66 6f terMutex();. fo
cf230 72 28 70 43 61 6e 64 69 64 61 74 65 3d 76 78 77 r(pCandidate=vxw
cf240 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20 70 43 orksFileList; pC
cf250 61 6e 64 69 64 61 74 65 3b 20 70 43 61 6e 64 69 andidate; pCandi
cf260 64 61 74 65 3d 70 43 61 6e 64 69 64 61 74 65 2d date=pCandidate-
cf270 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
cf280 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 4e 61 pCandidate->nNa
cf290 6d 65 3d 3d 6e 20 0a 20 20 20 20 20 26 26 20 6d me==n . && m
cf2a0 65 6d 63 6d 70 28 70 43 61 6e 64 69 64 61 74 65 emcmp(pCandidate
cf2b0 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 ->zCanonicalName
cf2c0 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 , pNew->zCanonic
cf2d0 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d 30 0a 20 20 alName, n)==0.
cf2e0 20 20 29 7b 0a 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
cf2f0 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a te3_free(pNew);.
cf300 20 20 20 20 20 20 20 70 43 61 6e 64 69 64 61 74 pCandidat
cf310 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 e->nRef++;.
cf320 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
cf330 28 29 3b 0a 20 20 20 20 20 20 20 72 65 74 75 72 ();. retur
cf340 6e 20 70 43 61 6e 64 69 64 61 74 65 3b 0a 20 20 n pCandidate;.
cf350 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f }. }.. /* No
cf360 20 6d 61 74 63 68 20 77 61 73 20 66 6f 75 6e 64 match was found
cf370 2e 20 20 57 65 20 77 69 6c 6c 20 6d 61 6b 65 20 . We will make
cf380 61 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a 2f a new file ID */
cf390 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 . pNew->nRef =
cf3a0 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 4e 61 6d 65 1;. pNew->nName
cf3b0 20 3d 20 6e 3b 0a 20 20 70 4e 65 77 2d 3e 70 4e = n;. pNew->pN
cf3c0 65 78 74 20 3d 20 76 78 77 6f 72 6b 73 46 69 6c ext = vxworksFil
cf3d0 65 4c 69 73 74 3b 0a 20 20 76 78 77 6f 72 6b 73 eList;. vxworks
cf3e0 46 69 6c 65 4c 69 73 74 20 3d 20 70 4e 65 77 3b FileList = pNew;
cf3f0 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 . unixLeaveMute
cf400 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e x();. return pN
cf410 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 ew;.}../*.** Dec
cf420 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 rement the refer
cf430 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e 20 61 20 ence count on a
cf440 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 vxworksFileId ob
cf450 6a 65 63 74 2e 20 20 46 72 65 65 0a 2a 2a 20 74 ject. Free.** t
cf460 68 65 20 6f 62 6a 65 63 74 20 77 68 65 6e 20 74 he object when t
cf470 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
cf480 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e nt reaches zero.
cf490 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
cf4a0 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 vxworksReleaseFi
cf4b0 6c 65 49 64 28 73 74 72 75 63 74 20 76 78 77 6f leId(struct vxwo
cf4c0 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 29 7b rksFileId *pId){
cf4d0 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 . unixEnterMute
cf4e0 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 x();. assert( p
cf4f0 49 64 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 Id->nRef>0 );.
cf500 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 pId->nRef--;. i
cf510 66 28 20 70 49 64 2d 3e 6e 52 65 66 3d 3d 30 20 f( pId->nRef==0
cf520 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 76 78 ){. struct vx
cf530 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 2a 70 70 worksFileId **pp
cf540 3b 0a 20 20 20 20 66 6f 72 28 70 70 3d 26 76 78 ;. for(pp=&vx
cf550 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20 2a worksFileList; *
cf560 70 70 20 26 26 20 2a 70 70 21 3d 70 49 64 3b 20 pp && *pp!=pId;
cf570 70 70 20 3d 20 26 28 28 2a 70 70 29 2d 3e 70 4e pp = &((*pp)->pN
cf580 65 78 74 29 29 7b 7d 0a 20 20 20 20 61 73 73 65 ext)){}. asse
cf590 72 74 28 20 2a 70 70 3d 3d 70 49 64 20 29 3b 0a rt( *pp==pId );.
cf5a0 20 20 20 20 2a 70 70 20 3d 20 70 49 64 2d 3e 70 *pp = pId->p
cf5b0 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 Next;. sqlite
cf5c0 33 5f 66 72 65 65 28 70 49 64 29 3b 0a 20 20 7d 3_free(pId);. }
cf5d0 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 . unixLeaveMute
cf5e0 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a x();.}.#endif /*
cf5f0 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f OS_VXWORKS */./
cf600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
cf610 45 6e 64 20 6f 66 20 55 6e 69 71 75 65 20 46 69 End of Unique Fi
cf620 6c 65 20 49 44 20 55 74 69 6c 69 74 79 20 55 73 le ID Utility Us
cf630 65 64 20 42 79 20 56 78 57 6f 72 6b 73 20 2a 2a ed By VxWorks **
cf640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
cf650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
cf6a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
cf6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf6f0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
cf700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 50 6f 73 ************ Pos
cf710 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b ix Advisory Lock
cf720 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ing ************
cf730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf740 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 61 64 76 .**.** POSIX adv
cf750 69 73 6f 72 79 20 6c 6f 63 6b 73 20 61 72 65 20 isory locks are
cf760 62 72 6f 6b 65 6e 20 62 79 20 64 65 73 69 67 6e broken by design
cf770 2e 20 20 41 4e 53 49 20 53 54 44 20 31 30 30 33 . ANSI STD 1003
cf780 2e 31 20 28 31 39 39 36 29 0a 2a 2a 20 73 65 63 .1 (1996).** sec
cf790 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 20 6c 69 6e tion 6.5.2.2 lin
cf7a0 65 73 20 34 38 33 20 74 68 72 6f 75 67 68 20 34 es 483 through 4
cf7b0 39 30 20 73 70 65 63 69 66 79 20 74 68 61 74 20 90 specify that
cf7c0 77 68 65 6e 20 61 20 70 72 6f 63 65 73 73 0a 2a when a process.*
cf7d0 2a 20 73 65 74 73 20 6f 72 20 63 6c 65 61 72 73 * sets or clears
cf7e0 20 61 20 6c 6f 63 6b 2c 20 74 68 61 74 20 6f 70 a lock, that op
cf7f0 65 72 61 74 69 6f 6e 20 6f 76 65 72 72 69 64 65 eration override
cf800 73 20 61 6e 79 20 70 72 69 6f 72 20 6c 6f 63 6b s any prior lock
cf810 73 20 73 65 74 0a 2a 2a 20 62 79 20 74 68 65 20 s set.** by the
cf820 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 49 same process. I
cf830 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 70 6c 69 t does not expli
cf840 63 69 74 6c 79 20 73 61 79 20 73 6f 2c 20 62 75 citly say so, bu
cf850 74 20 74 68 69 73 20 69 6d 70 6c 69 65 73 0a 2a t this implies.*
cf860 2a 20 74 68 61 74 20 69 74 20 6f 76 65 72 72 69 * that it overri
cf870 64 65 73 20 6c 6f 63 6b 73 20 73 65 74 20 62 79 des locks set by
cf880 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 the same proces
cf890 73 20 75 73 69 6e 67 20 61 20 64 69 66 66 65 72 s using a differ
cf8a0 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 ent.** file desc
cf8b0 72 69 70 74 6f 72 2e 20 20 43 6f 6e 73 69 64 65 riptor. Conside
cf8c0 72 20 74 68 69 73 20 74 65 73 74 20 63 61 73 65 r this test case
cf8d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 69 6e :.**.** in
cf8e0 74 20 66 64 31 20 3d 20 6f 70 65 6e 28 22 2e 2f t fd1 = open("./
cf8f0 66 69 6c 65 31 22 2c 20 4f 5f 52 44 57 52 7c 4f file1", O_RDWR|O
cf900 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a _CREAT, 0644);.*
cf910 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 32 20 * int fd2
cf920 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 32 22 = open("./file2"
cf930 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 , O_RDWR|O_CREAT
cf940 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a 2a 20 53 , 0644);.**.** S
cf950 75 70 70 6f 73 65 20 2e 2f 66 69 6c 65 31 20 61 uppose ./file1 a
cf960 6e 64 20 2e 2f 66 69 6c 65 32 20 61 72 65 20 72 nd ./file2 are r
cf970 65 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 66 eally the same f
cf980 69 6c 65 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 ile (because.**
cf990 6f 6e 65 20 69 73 20 61 20 68 61 72 64 20 6f 72 one is a hard or
cf9a0 20 73 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b 20 74 symbolic link t
cf9b0 6f 20 74 68 65 20 6f 74 68 65 72 29 20 74 68 65 o the other) the
cf9c0 6e 20 69 66 20 79 6f 75 20 73 65 74 0a 2a 2a 20 n if you set.**
cf9d0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
cf9e0 6b 20 6f 6e 20 66 64 31 2c 20 74 68 65 6e 20 74 k on fd1, then t
cf9f0 72 79 20 74 6f 20 67 65 74 20 61 6e 20 65 78 63 ry to get an exc
cfa00 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 6f lusive lock.** o
cfa10 6e 20 66 64 32 2c 20 69 74 20 77 6f 72 6b 73 2e n fd2, it works.
cfa20 20 20 49 20 77 6f 75 6c 64 20 68 61 76 65 20 65 I would have e
cfa30 78 70 65 63 74 65 64 20 74 68 65 20 73 65 63 6f xpected the seco
cfa40 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a 20 66 61 nd lock to.** fa
cfa50 69 6c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 il since there w
cfa60 61 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 as already a loc
cfa70 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 64 75 k on the file du
cfa80 65 20 74 6f 20 66 64 31 2e 0a 2a 2a 20 42 75 74 e to fd1..** But
cfa90 20 6e 6f 74 20 73 6f 2e 20 20 53 69 6e 63 65 20 not so. Since
cfaa0 62 6f 74 68 20 6c 6f 63 6b 73 20 63 61 6d 65 20 both locks came
cfab0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 70 72 from the same pr
cfac0 6f 63 65 73 73 2c 20 74 68 65 0a 2a 2a 20 73 65 ocess, the.** se
cfad0 63 6f 6e 64 20 6f 76 65 72 72 69 64 65 73 20 74 cond overrides t
cfae0 68 65 20 66 69 72 73 74 2c 20 65 76 65 6e 20 74 he first, even t
cfaf0 68 6f 75 67 68 20 74 68 65 79 20 77 65 72 65 20 hough they were
cfb00 6f 6e 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 on different.**
cfb10 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
cfb20 20 6f 70 65 6e 65 64 20 6f 6e 20 64 69 66 66 65 opened on diffe
cfb30 72 65 6e 74 20 66 69 6c 65 20 6e 61 6d 65 73 2e rent file names.
cfb40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 61 6e .**.** This mean
cfb50 73 20 74 68 61 74 20 77 65 20 63 61 6e 6e 6f 74 s that we cannot
cfb60 20 75 73 65 20 50 4f 53 49 58 20 6c 6f 63 6b 73 use POSIX locks
cfb70 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 to synchronize
cfb80 66 69 6c 65 20 61 63 63 65 73 73 0a 2a 2a 20 61 file access.** a
cfb90 6d 6f 6e 67 20 63 6f 6d 70 65 74 69 6e 67 20 74 mong competing t
cfba0 68 72 65 61 64 73 20 6f 66 20 74 68 65 20 73 61 hreads of the sa
cfbb0 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 50 4f 53 me process. POS
cfbc0 49 58 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 77 6f IX locks will wo
cfbd0 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 6f 20 73 79 rk fine.** to sy
cfbe0 6e 63 68 72 6f 6e 69 7a 65 20 61 63 63 65 73 73 nchronize access
cfbf0 20 66 6f 72 20 74 68 72 65 61 64 73 20 69 6e 20 for threads in
cfc00 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 separate process
cfc10 65 73 2c 20 62 75 74 20 6e 6f 74 0a 2a 2a 20 74 es, but not.** t
cfc20 68 72 65 61 64 73 20 77 69 74 68 69 6e 20 74 68 hreads within th
cfc30 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 0a e same process..
cfc40 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 **.** To work ar
cfc50 6f 75 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 6d ound the problem
cfc60 2c 20 53 51 4c 69 74 65 20 68 61 73 20 74 6f 20 , SQLite has to
cfc70 6d 61 6e 61 67 65 20 66 69 6c 65 20 6c 6f 63 6b manage file lock
cfc80 73 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 s internally.**
cfc90 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 20 57 68 65 on its own. Whe
cfca0 6e 65 76 65 72 20 61 20 6e 65 77 20 64 61 74 61 never a new data
cfcb0 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2c 20 base is opened,
cfcc0 77 65 20 68 61 76 65 20 74 6f 20 66 69 6e 64 20 we have to find
cfcd0 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 the.** specific
cfce0 69 6e 6f 64 65 20 6f 66 20 74 68 65 20 64 61 74 inode of the dat
cfcf0 61 62 61 73 65 20 66 69 6c 65 20 28 74 68 65 20 abase file (the
cfd00 69 6e 6f 64 65 20 69 73 20 64 65 74 65 72 6d 69 inode is determi
cfd10 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73 74 ned by the.** st
cfd20 5f 64 65 76 20 61 6e 64 20 73 74 5f 69 6e 6f 20 _dev and st_ino
cfd30 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 73 74 fields of the st
cfd40 61 74 20 73 74 72 75 63 74 75 72 65 20 74 68 61 at structure tha
cfd50 74 20 66 73 74 61 74 28 29 20 66 69 6c 6c 73 20 t fstat() fills
cfd60 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 68 65 63 6b in).** and check
cfd70 20 66 6f 72 20 6c 6f 63 6b 73 20 61 6c 72 65 61 for locks alrea
cfd80 64 79 20 65 78 69 73 74 69 6e 67 20 6f 6e 20 74 dy existing on t
cfd90 68 61 74 20 69 6e 6f 64 65 2e 20 20 57 68 65 6e hat inode. When
cfda0 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 63 72 locks are.** cr
cfdb0 65 61 74 65 64 20 6f 72 20 72 65 6d 6f 76 65 64 eated or removed
cfdc0 2c 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 6f , we have to loo
cfdd0 6b 20 61 74 20 6f 75 72 20 6f 77 6e 20 69 6e 74 k at our own int
cfde0 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 20 ernal record of
cfdf0 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 20 74 6f 20 the.** locks to
cfe00 73 65 65 20 69 66 20 61 6e 6f 74 68 65 72 20 74 see if another t
cfe10 68 72 65 61 64 20 68 61 73 20 70 72 65 76 69 6f hread has previo
cfe20 75 73 6c 79 20 73 65 74 20 61 20 6c 6f 63 6b 20 usly set a lock
cfe30 6f 6e 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20 on that same.**
cfe40 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 28 41 73 inode..**.** (As
cfe50 69 64 65 3a 20 54 68 65 20 75 73 65 20 6f 66 20 ide: The use of
cfe60 69 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20 61 73 inode numbers as
cfe70 20 75 6e 69 71 75 65 20 49 44 73 20 64 6f 65 73 unique IDs does
cfe80 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57 not work on VxW
cfe90 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72 20 56 78 57 orks..** For VxW
cfea0 6f 72 6b 73 2c 20 77 65 20 68 61 76 65 20 74 6f orks, we have to
cfeb0 20 75 73 65 20 74 68 65 20 61 6c 74 65 72 6e 61 use the alterna
cfec0 74 69 76 65 20 75 6e 69 71 75 65 20 49 44 20 73 tive unique ID s
cfed0 79 73 74 65 6d 20 62 61 73 65 64 20 6f 6e 0a 2a ystem based on.*
cfee0 2a 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 * canonical file
cfef0 6e 61 6d 65 20 61 6e 64 20 69 6d 70 6c 65 6d 65 name and impleme
cff00 6e 74 65 64 20 69 6e 20 74 68 65 20 70 72 65 76 nted in the prev
cff10 69 6f 75 73 20 64 69 76 69 73 69 6f 6e 2e 29 0a ious division.).
cff20 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
cff30 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 3_file structure
cff40 20 66 6f 72 20 50 4f 53 49 58 20 69 73 20 6e 6f for POSIX is no
cff50 20 6c 6f 6e 67 65 72 20 6a 75 73 74 20 61 6e 20 longer just an
cff60 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 integer file.**
cff70 64 65 73 63 72 69 70 74 6f 72 2e 20 20 49 74 20 descriptor. It
cff80 69 73 20 6e 6f 77 20 61 20 73 74 72 75 63 74 75 is now a structu
cff90 72 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 re that holds th
cffa0 65 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a e integer file.*
cffb0 2a 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e 64 * descriptor and
cffc0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
cffd0 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 structure that d
cffe0 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6e 74 escribes the int
cfff0 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b 73 20 6f ernal.** locks o
d0000 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 n the correspond
d0010 69 6e 67 20 69 6e 6f 64 65 2e 20 20 54 68 65 72 ing inode. Ther
d0020 65 20 69 73 20 6f 6e 65 20 6c 6f 63 6b 69 6e 67 e is one locking
d0030 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 65 structure.** pe
d0040 72 20 69 6e 6f 64 65 2c 20 73 6f 20 69 66 20 74 r inode, so if t
d0050 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 69 73 he same inode is
d0060 20 6f 70 65 6e 65 64 20 74 77 69 63 65 2c 20 62 opened twice, b
d0070 6f 74 68 20 75 6e 69 78 46 69 6c 65 20 73 74 72 oth unixFile str
d0080 75 63 74 75 72 65 73 0a 2a 2a 20 70 6f 69 6e 74 uctures.** point
d0090 20 74 6f 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 to the same loc
d00a0 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 20 king structure.
d00b0 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 The locking str
d00c0 75 63 74 75 72 65 20 6b 65 65 70 73 0a 2a 2a 20 ucture keeps.**
d00d0 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e a reference coun
d00e0 74 20 28 73 6f 20 77 65 20 77 69 6c 6c 20 6b 6e t (so we will kn
d00f0 6f 77 20 77 68 65 6e 20 74 6f 20 64 65 6c 65 74 ow when to delet
d0100 65 20 69 74 29 20 61 6e 64 20 61 20 22 63 6e 74 e it) and a "cnt
d0110 22 0a 2a 2a 20 66 69 65 6c 64 20 74 68 61 74 20 ".** field that
d0120 74 65 6c 6c 73 20 75 73 20 69 74 73 20 69 6e 74 tells us its int
d0130 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 75 ernal lock statu
d0140 73 2e 20 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 s. cnt==0 means
d0150 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 the.** file is
d0160 75 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e 74 3d 3d unlocked. cnt==
d0170 2d 31 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c -1 means the fil
d0180 65 20 68 61 73 20 61 6e 20 65 78 63 6c 75 73 69 e has an exclusi
d0190 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63 6e 74 3e ve lock..** cnt>
d01a0 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20 61 72 0 means there ar
d01b0 65 20 63 6e 74 20 73 68 61 72 65 64 20 6c 6f 63 e cnt shared loc
d01c0 6b 73 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a ks on the file..
d01d0 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 **.** Any attemp
d01e0 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 6e 6c t to lock or unl
d01f0 6f 63 6b 20 61 20 66 69 6c 65 20 66 69 72 73 74 ock a file first
d0200 20 63 68 65 63 6b 73 20 74 68 65 20 6c 6f 63 6b checks the lock
d0210 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 ing.** structure
d0220 2e 20 20 54 68 65 20 66 63 6e 74 6c 28 29 20 73 . The fcntl() s
d0230 79 73 74 65 6d 20 63 61 6c 6c 20 69 73 20 6f 6e ystem call is on
d0240 6c 79 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73 65 ly invoked to se
d0250 74 20 61 20 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f t a .** POSIX lo
d0260 63 6b 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e ck if the intern
d0270 61 6c 20 6c 6f 63 6b 20 73 74 72 75 63 74 75 72 al lock structur
d0280 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62 65 e transitions be
d0290 74 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f 63 6b 65 tween.** a locke
d02a0 64 20 61 6e 64 20 61 6e 20 75 6e 6c 6f 63 6b 65 d and an unlocke
d02b0 64 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 42 d state..**.** B
d02c0 75 74 20 77 61 69 74 3a 20 20 74 68 65 72 65 20 ut wait: there
d02d0 61 72 65 20 79 65 74 20 6d 6f 72 65 20 70 72 6f are yet more pro
d02e0 62 6c 65 6d 73 20 77 69 74 68 20 50 4f 53 49 58 blems with POSIX
d02f0 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 2e advisory locks.
d0300 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75 20 63 6c .**.** If you cl
d0310 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73 63 72 ose a file descr
d0320 69 70 74 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 iptor that point
d0330 73 20 74 6f 20 61 20 66 69 6c 65 20 74 68 61 74 s to a file that
d0340 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 61 has locks,.** a
d0350 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 74 ll locks on that
d0360 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20 6f file that are o
d0370 77 6e 65 64 20 62 79 20 74 68 65 20 63 75 72 72 wned by the curr
d0380 65 6e 74 20 70 72 6f 63 65 73 73 20 61 72 65 0a ent process are.
d0390 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20 20 54 6f ** released. To
d03a0 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 work around thi
d03b0 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61 63 68 20 s problem, each
d03c0 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 unixFile structu
d03d0 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 re contains.** a
d03e0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 75 pointer to an u
d03f0 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 nixOpenCnt struc
d0400 74 75 72 65 2e 20 20 54 68 65 72 65 20 69 73 20 ture. There is
d0410 6f 6e 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 one unixOpenCnt
d0420 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 65 72 structure.** per
d0430 20 6f 70 65 6e 20 69 6e 6f 64 65 2c 20 77 68 69 open inode, whi
d0440 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6d 75 ch means that mu
d0450 6c 74 69 70 6c 65 20 75 6e 69 78 46 69 6c 65 20 ltiple unixFile
d0460 63 61 6e 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 can point to a s
d0470 69 6e 67 6c 65 0a 2a 2a 20 75 6e 69 78 4f 70 65 ingle.** unixOpe
d0480 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61 6e 20 61 nCnt. When an a
d0490 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 ttempt is made t
d04a0 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e 69 78 46 o close an unixF
d04b0 69 6c 65 2c 20 69 66 20 74 68 65 72 65 20 61 72 ile, if there ar
d04c0 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e 69 78 46 e.** other unixF
d04d0 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 ile open on the
d04e0 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 61 74 20 same inode that
d04f0 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c 6f 63 6b are holding lock
d0500 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a 2a 20 74 s, the call.** t
d0510 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 20 66 69 o close() the fi
d0520 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
d0530 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 deferred until
d0540 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f 63 6b 73 all of the locks
d0550 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68 65 20 75 clear..** The u
d0560 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 nixOpenCnt struc
d0570 74 75 72 65 20 6b 65 65 70 73 20 61 20 6c 69 73 ture keeps a lis
d0580 74 20 6f 66 20 66 69 6c 65 20 64 65 73 63 72 69 t of file descri
d0590 70 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64 20 ptors that need
d05a0 74 6f 0a 2a 2a 20 62 65 20 63 6c 6f 73 65 64 20 to.** be closed
d05b0 61 6e 64 20 74 68 61 74 20 6c 69 73 74 20 69 73 and that list is
d05c0 20 77 61 6c 6b 65 64 20 28 61 6e 64 20 63 6c 65 walked (and cle
d05d0 61 72 65 64 29 20 77 68 65 6e 20 74 68 65 20 6c ared) when the l
d05e0 61 73 74 20 6c 6f 63 6b 0a 2a 2a 20 63 6c 65 61 ast lock.** clea
d05f0 72 73 2e 0a 2a 2a 0a 2a 2a 20 59 65 74 20 61 6e rs..**.** Yet an
d0600 6f 74 68 65 72 20 70 72 6f 62 6c 65 6d 3a 20 20 other problem:
d0610 4c 69 6e 75 78 54 68 72 65 61 64 73 20 64 6f 20 LinuxThreads do
d0620 6e 6f 74 20 70 6c 61 79 20 77 65 6c 6c 20 77 69 not play well wi
d0630 74 68 20 70 6f 73 69 78 20 6c 6f 63 6b 73 2e 0a th posix locks..
d0640 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 6f 6c 64 65 72 **.** Many older
d0650 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 6c 69 6e versions of lin
d0660 75 78 20 75 73 65 20 74 68 65 20 4c 69 6e 75 78 ux use the Linux
d0670 54 68 72 65 61 64 73 20 6c 69 62 72 61 72 79 20 Threads library
d0680 77 68 69 63 68 20 69 73 0a 2a 2a 20 6e 6f 74 20 which is.** not
d0690 70 6f 73 69 78 20 63 6f 6d 70 6c 69 61 6e 74 2e posix compliant.
d06a0 20 20 55 6e 64 65 72 20 4c 69 6e 75 78 54 68 72 Under LinuxThr
d06b0 65 61 64 73 2c 20 61 20 6c 6f 63 6b 20 63 72 65 eads, a lock cre
d06c0 61 74 65 64 20 62 79 20 74 68 72 65 61 64 0a 2a ated by thread.*
d06d0 2a 20 41 20 63 61 6e 6e 6f 74 20 62 65 20 6d 6f * A cannot be mo
d06e0 64 69 66 69 65 64 20 6f 72 20 6f 76 65 72 72 69 dified or overri
d06f0 64 64 65 6e 20 62 79 20 61 20 64 69 66 66 65 72 dden by a differ
d0700 65 6e 74 20 74 68 72 65 61 64 20 42 2e 0a 2a 2a ent thread B..**
d0710 20 4f 6e 6c 79 20 74 68 72 65 61 64 20 41 20 63 Only thread A c
d0720 61 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 6c 6f an modify the lo
d0730 63 6b 2e 20 20 4c 6f 63 6b 69 6e 67 20 62 65 68 ck. Locking beh
d0740 61 76 69 6f 72 20 69 73 20 63 6f 72 72 65 63 74 avior is correct
d0750 0a 2a 2a 20 69 66 20 74 68 65 20 61 70 70 6c 69 .** if the appli
d0760 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 6e ation uses the n
d0770 65 77 65 72 20 4e 61 74 69 76 65 20 50 6f 73 69 ewer Native Posi
d0780 78 20 54 68 72 65 61 64 20 4c 69 62 72 61 72 79 x Thread Library
d0790 20 28 4e 50 54 4c 29 0a 2a 2a 20 6f 6e 20 6c 69 (NPTL).** on li
d07a0 6e 75 78 20 2d 20 77 69 74 68 20 4e 50 54 4c 20 nux - with NPTL
d07b0 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62 a lock created b
d07c0 79 20 74 68 72 65 61 64 20 41 20 63 61 6e 20 6f y thread A can o
d07d0 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 0a 2a 2a verride locks.**
d07e0 20 69 6e 20 74 68 72 65 61 64 20 42 2e 20 20 42 in thread B. B
d07f0 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 ut there is no w
d0800 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 20 63 6f ay to know at co
d0810 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 69 63 68 mpile-time which
d0820 0a 2a 2a 20 74 68 72 65 61 64 69 6e 67 20 6c 69 .** threading li
d0830 62 72 61 72 79 20 69 73 20 62 65 69 6e 67 20 75 brary is being u
d0840 73 65 64 2e 20 20 53 6f 20 74 68 65 72 65 20 69 sed. So there i
d0850 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f 77 s no way to know
d0860 20 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 at.** compile-t
d0870 69 6d 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e ime whether or n
d0880 6f 74 20 74 68 72 65 61 64 20 41 20 63 61 6e 20 ot thread A can
d0890 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 20 6f override locks o
d08a0 6e 20 74 68 72 65 61 64 20 42 2e 0a 2a 2a 20 57 n thread B..** W
d08b0 65 20 68 61 76 65 20 74 6f 20 64 6f 20 61 20 72 e have to do a r
d08c0 75 6e 2d 74 69 6d 65 20 63 68 65 63 6b 20 74 6f un-time check to
d08d0 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 62 65 discover the be
d08e0 68 61 76 69 6f 72 20 6f 66 20 74 68 65 0a 2a 2a havior of the.**
d08f0 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 current process
d0900 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 79 73 74 65 ..**.** On syste
d0910 6d 73 20 77 68 65 72 65 20 74 68 72 65 61 64 20 ms where thread
d0920 41 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 6d A is unable to m
d0930 6f 64 69 66 79 20 6c 6f 63 6b 73 20 63 72 65 61 odify locks crea
d0940 74 65 64 20 62 79 0a 2a 2a 20 74 68 72 65 61 64 ted by.** thread
d0950 20 42 2c 20 77 65 20 68 61 76 65 20 74 6f 20 6b B, we have to k
d0960 65 65 70 20 74 72 61 63 6b 20 6f 66 20 77 68 69 eep track of whi
d0970 63 68 20 74 68 72 65 61 64 20 63 72 65 61 74 65 ch thread create
d0980 64 20 65 61 63 68 0a 2a 2a 20 6c 6f 63 6b 2e 20 d each.** lock.
d0990 20 48 65 6e 63 65 20 74 68 65 72 65 20 69 73 20 Hence there is
d09a0 61 6e 20 65 78 74 72 61 20 66 69 65 6c 64 20 69 an extra field i
d09b0 6e 20 74 68 65 20 6b 65 79 20 74 6f 20 74 68 65 n the key to the
d09c0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 0a 2a 2a unixLockInfo.**
d09d0 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 72 65 structure to re
d09e0 63 6f 72 64 20 74 68 69 73 20 69 6e 66 6f 72 6d cord this inform
d09f0 61 74 69 6f 6e 2e 20 20 41 6e 64 20 6f 6e 20 74 ation. And on t
d0a00 68 6f 73 65 20 73 79 73 74 65 6d 73 20 69 74 0a hose systems it.
d0a10 2a 2a 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f ** is illegal to
d0a20 20 62 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 begin a transac
d0a30 74 69 6f 6e 20 69 6e 20 6f 6e 65 20 74 68 72 65 tion in one thre
d0a40 61 64 20 61 6e 64 20 66 69 6e 69 73 68 20 69 74 ad and finish it
d0a50 0a 2a 2a 20 69 6e 20 61 6e 6f 74 68 65 72 2e 20 .** in another.
d0a60 20 46 6f 72 20 74 68 69 73 20 6c 61 74 74 65 72 For this latter
d0a70 20 72 65 73 74 72 69 63 74 69 6f 6e 2c 20 74 68 restriction, th
d0a80 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 2d 61 ere is no work-a
d0a90 72 6f 75 6e 64 2e 0a 2a 2a 20 49 74 20 69 73 20 round..** It is
d0aa0 61 20 6c 69 6d 69 74 61 74 69 6f 6e 20 6f 66 20 a limitation of
d0ab0 4c 69 6e 75 78 54 68 72 65 61 64 73 2e 0a 2a 2f LinuxThreads..*/
d0ac0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 63 ../*.** Set or c
d0ad0 68 65 63 6b 20 74 68 65 20 75 6e 69 78 46 69 6c heck the unixFil
d0ae0 65 2e 74 69 64 20 66 69 65 6c 64 2e 20 20 54 68 e.tid field. Th
d0af0 69 73 20 66 69 65 6c 64 20 69 73 20 73 65 74 20 is field is set
d0b00 77 68 65 6e 20 61 6e 20 75 6e 69 78 46 69 6c 65 when an unixFile
d0b10 0a 2a 2a 20 69 73 20 66 69 72 73 74 20 6f 70 65 .** is first ope
d0b20 6e 65 64 2e 20 20 41 6c 6c 20 73 75 62 73 65 71 ned. All subseq
d0b30 75 65 6e 74 20 75 73 65 73 20 6f 66 20 74 68 65 uent uses of the
d0b40 20 75 6e 69 78 46 69 6c 65 20 76 65 72 69 66 79 unixFile verify
d0b50 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d that the.** sam
d0b60 65 20 74 68 72 65 61 64 20 69 73 20 6f 70 65 72 e thread is oper
d0b70 61 74 69 6e 67 20 6f 6e 20 74 68 65 20 75 6e 69 ating on the uni
d0b80 78 46 69 6c 65 2e 20 20 53 6f 6d 65 20 6f 70 65 xFile. Some ope
d0b90 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 64 rating systems d
d0ba0 6f 0a 2a 2a 20 6e 6f 74 20 61 6c 6c 6f 77 20 6c o.** not allow l
d0bb0 6f 63 6b 73 20 74 6f 20 62 65 20 6f 76 65 72 72 ocks to be overr
d0bc0 69 64 64 65 6e 20 62 79 20 6f 74 68 65 72 20 74 idden by other t
d0bd0 68 72 65 61 64 73 20 61 6e 64 20 74 68 61 74 20 hreads and that
d0be0 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 6d restriction.** m
d0bf0 65 61 6e 73 20 74 68 61 74 20 73 71 6c 69 74 65 eans that sqlite
d0c00 33 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 3* database hand
d0c10 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 6d 6f les cannot be mo
d0c20 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74 68 72 ved from one thr
d0c30 65 61 64 0a 2a 2a 20 74 6f 20 61 6e 6f 74 68 65 ead.** to anothe
d0c40 72 20 77 68 69 6c 65 20 6c 6f 63 6b 73 20 61 72 r while locks ar
d0c50 65 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 56 65 e held..**.** Ve
d0c60 72 73 69 6f 6e 20 33 2e 33 2e 31 20 28 32 30 30 rsion 3.3.1 (200
d0c70 36 2d 30 31 2d 31 35 29 3a 20 20 75 6e 69 78 46 6-01-15): unixF
d0c80 69 6c 65 20 63 61 6e 20 62 65 20 6d 6f 76 65 64 ile can be moved
d0c90 20 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 from one thread
d0ca0 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 61 to.** another a
d0cb0 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 61 72 65 s long as we are
d0cc0 20 72 75 6e 6e 69 6e 67 20 6f 6e 20 61 20 73 79 running on a sy
d0cd0 73 74 65 6d 20 74 68 61 74 20 73 75 70 70 6f 72 stem that suppor
d0ce0 74 73 20 74 68 72 65 61 64 73 0a 2a 2a 20 6f 76 ts threads.** ov
d0cf0 65 72 72 69 64 69 6e 67 20 65 61 63 68 20 6f 74 erriding each ot
d0d00 68 65 72 73 20 6c 6f 63 6b 73 20 28 77 68 69 63 hers locks (whic
d0d10 68 20 69 73 20 6e 6f 77 20 74 68 65 20 6d 6f 73 h is now the mos
d0d20 74 20 63 6f 6d 6d 6f 6e 20 62 65 68 61 76 69 6f t common behavio
d0d30 72 29 0a 2a 2a 20 6f 72 20 69 66 20 6e 6f 20 6c r).** or if no l
d0d40 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 20 20 ocks are held.
d0d50 42 75 74 20 74 68 65 20 75 6e 69 78 46 69 6c 65 But the unixFile
d0d60 2e 70 4c 6f 63 6b 20 66 69 65 6c 64 20 6e 65 65 .pLock field nee
d0d70 64 73 20 74 6f 20 62 65 0a 2a 2a 20 72 65 63 6f ds to be.** reco
d0d80 6d 70 75 74 65 64 20 62 65 63 61 75 73 65 20 69 mputed because i
d0d90 74 73 20 6b 65 79 20 69 6e 63 6c 75 64 65 73 20 ts key includes
d0da0 74 68 65 20 74 68 72 65 61 64 2d 69 64 2e 20 20 the thread-id.
d0db0 53 65 65 20 74 68 65 20 0a 2a 2a 20 74 72 61 6e See the .** tran
d0dc0 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 29 20 sferOwnership()
d0dd0 66 75 6e 63 74 69 6f 6e 20 62 65 6c 6f 77 20 66 function below f
d0de0 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
d0df0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2f 0a 23 69 66 formation.*/.#if
d0e00 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
d0e10 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f FE && defined(__
d0e20 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e linux__).# defin
d0e30 65 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 58 e SET_THREADID(X
d0e40 29 20 20 20 28 58 29 2d 3e 74 69 64 20 3d 20 70 ) (X)->tid = p
d0e50 74 68 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 20 thread_self().#
d0e60 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 define CHECK_THR
d0e70 45 41 44 49 44 28 58 29 20 28 74 68 72 65 61 64 EADID(X) (thread
d0e80 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 sOverrideEachOth
d0e90 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26 26 20 5c ersLocks==0 && \
d0ea0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
d0eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 21 70 74 !pt
d0ec0 68 72 65 61 64 5f 65 71 75 61 6c 28 28 58 29 2d hread_equal((X)-
d0ed0 3e 74 69 64 2c 20 70 74 68 72 65 61 64 5f 73 65 >tid, pthread_se
d0ee0 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a 23 20 64 lf())).#else.# d
d0ef0 65 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44 efine SET_THREAD
d0f00 49 44 28 58 29 0a 23 20 64 65 66 69 6e 65 20 43 ID(X).# define C
d0f10 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58 29 HECK_THREADID(X)
d0f20 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
d0f30 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
d0f40 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
d0f50 72 75 63 74 75 72 65 20 73 65 72 76 65 73 20 61 ructure serves a
d0f60 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 0a 2a s the key used.*
d0f70 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 61 * to locate a pa
d0f80 72 74 69 63 75 6c 61 72 20 75 6e 69 78 4f 70 65 rticular unixOpe
d0f90 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 67 nCnt structure g
d0fa0 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 20 iven its inode.
d0fb0 20 54 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 This.** is the
d0fc0 73 61 6d 65 20 61 73 20 74 68 65 20 75 6e 69 78 same as the unix
d0fd0 4c 6f 63 6b 4b 65 79 20 65 78 63 65 70 74 20 74 LockKey except t
d0fe0 68 61 74 20 74 68 65 20 74 68 72 65 61 64 20 49 hat the thread I
d0ff0 44 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2f D is omitted..*/
d1000 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 .struct unixFile
d1010 49 64 20 7b 0a 20 20 64 65 76 5f 74 20 64 65 76 Id {. dev_t dev
d1020 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
d1030 20 20 20 2f 2a 20 44 65 76 69 63 65 20 6e 75 6d /* Device num
d1040 62 65 72 20 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 ber */.#if OS_VX
d1050 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 76 WORKS. struct v
d1060 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 xworksFileId *pI
d1070 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20 66 69 d; /* Unique fi
d1080 6c 65 20 49 44 20 66 6f 72 20 76 78 77 6f 72 6b le ID for vxwork
d1090 73 2e 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 69 6e s. */.#else. in
d10a0 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 20 20 20 o_t ino;
d10b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 6f /* Ino
d10c0 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 65 6e de number */.#en
d10d0 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e dif.};../*.** An
d10e0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
d10f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
d1100 74 75 72 65 20 73 65 72 76 65 73 20 61 73 20 74 ture serves as t
d1110 68 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74 he key used.** t
d1120 6f 20 6c 6f 63 61 74 65 20 61 20 70 61 72 74 69 o locate a parti
d1130 63 75 6c 61 72 20 75 6e 69 78 4c 6f 63 6b 49 6e cular unixLockIn
d1140 66 6f 20 73 74 72 75 63 74 75 72 65 20 67 69 76 fo structure giv
d1150 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 0a 2a 2a en its inode..**
d1160 0a 2a 2a 20 49 66 20 74 68 72 65 61 64 73 20 63 .** If threads c
d1170 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65 annot override e
d1180 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 ach others locks
d1190 20 28 4c 69 6e 75 78 54 68 72 65 61 64 73 29 2c (LinuxThreads),
d11a0 20 74 68 65 6e 20 77 65 0a 2a 2a 20 73 65 74 20 then we.** set
d11b0 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 2e the unixLockKey.
d11c0 74 69 64 20 66 69 65 6c 64 20 74 6f 20 74 68 65 tid field to the
d11d0 20 74 68 72 65 61 64 20 49 44 2e 20 20 49 66 20 thread ID. If
d11e0 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 threads can over
d11f0 72 69 64 65 0a 2a 2a 20 65 61 63 68 20 6f 74 68 ride.** each oth
d1200 65 72 73 20 6c 6f 63 6b 73 20 28 50 6f 73 69 78 ers locks (Posix
d1210 20 61 6e 64 20 4e 50 54 4c 29 20 74 68 65 6e 20 and NPTL) then
d1220 74 69 64 20 69 73 20 61 6c 77 61 79 73 20 73 65 tid is always se
d1230 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 20 74 69 t to zero..** ti
d1240 64 20 69 73 20 6f 6d 69 74 74 65 64 20 69 66 20 d is omitted if
d1250 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f we compile witho
d1260 75 74 20 74 68 72 65 61 64 69 6e 67 20 73 75 70 ut threading sup
d1270 70 6f 72 74 20 6f 72 20 6f 6e 20 61 6e 20 4f 53 port or on an OS
d1280 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 6c .** other than l
d1290 69 6e 75 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 inux..*/.struct
d12a0 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 7b 0a 20 20 unixLockKey {.
d12b0 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 struct unixFileI
d12c0 64 20 66 69 64 3b 20 20 2f 2a 20 55 6e 69 71 75 d fid; /* Uniqu
d12d0 65 20 69 64 65 6e 74 69 66 69 65 72 20 66 6f 72 e identifier for
d12e0 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 23 69 66 the file */.#if
d12f0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
d1300 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f FE && defined(__
d1310 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 68 72 65 linux__). pthre
d1320 61 64 5f 74 20 74 69 64 3b 20 20 2f 2a 20 54 68 ad_t tid; /* Th
d1330 72 65 61 64 20 49 44 20 6f 66 20 6c 6f 63 6b 20 read ID of lock
d1340 6f 77 6e 65 72 2e 20 5a 65 72 6f 20 69 66 20 6e owner. Zero if n
d1350 6f 74 20 75 73 69 6e 67 20 4c 69 6e 75 78 54 68 ot using LinuxTh
d1360 72 65 61 64 73 20 2a 2f 0a 23 65 6e 64 69 66 0a reads */.#endif.
d1370 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 };../*.** An ins
d1380 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
d1390 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
d13a0 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f is allocated fo
d13b0 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 r each open.** i
d13c0 6e 6f 64 65 2e 20 20 4f 72 2c 20 6f 6e 20 4c 69 node. Or, on Li
d13d0 6e 75 78 54 68 72 65 61 64 73 2c 20 74 68 65 72 nuxThreads, ther
d13e0 65 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 e is one of thes
d13f0 65 20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 e structures for
d1400 0a 2a 2a 20 65 61 63 68 20 69 6e 6f 64 65 20 6f .** each inode o
d1410 70 65 6e 65 64 20 62 79 20 65 61 63 68 20 74 68 pened by each th
d1420 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 69 read..**.** A si
d1430 6e 67 6c 65 20 69 6e 6f 64 65 20 63 61 6e 20 68 ngle inode can h
d1440 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 66 69 6c ave multiple fil
d1450 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20 73 e descriptors, s
d1460 6f 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65 0a o each unixFile.
d1470 2a 2a 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e ** structure con
d1480 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 tains a pointer
d1490 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f to an instance o
d14a0 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 61 6e f this object an
d14b0 64 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 d this.** object
d14c0 20 6b 65 65 70 73 20 61 20 63 6f 75 6e 74 20 6f keeps a count o
d14d0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
d14e0 75 6e 69 78 46 69 6c 65 20 70 6f 69 6e 74 69 6e unixFile pointin
d14f0 67 20 74 6f 20 69 74 2e 0a 2a 2f 0a 73 74 72 75 g to it..*/.stru
d1500 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ct unixLockInfo
d1510 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c {. struct unixL
d1520 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b 65 79 3b 20 ockKey lockKey;
d1530 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 /* The looku
d1540 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 63 p key */. int c
d1550 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nt;
d1560 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
d1570 6d 62 65 72 20 6f 66 20 53 48 41 52 45 44 20 6c mber of SHARED l
d1580 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20 20 69 ocks held */. i
d1590 6e 74 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 nt locktype;
d15a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
d15b0 2a 20 4f 6e 65 20 6f 66 20 53 48 41 52 45 44 5f * One of SHARED_
d15c0 4c 4f 43 4b 2c 20 52 45 53 45 52 56 45 44 5f 4c LOCK, RESERVED_L
d15d0 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a 20 20 69 6e OCK etc. */. in
d15e0 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
d15f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d1600 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 Number of point
d1610 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 ers to this stru
d1620 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 cture */. struc
d1630 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a t unixLockInfo *
d1640 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a 20 4c 69 pNext; /* Li
d1650 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f st of all unixLo
d1660 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 73 20 2a ckInfo objects *
d1670 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c /. struct unixL
d1680 6f 63 6b 49 6e 66 6f 20 2a 70 50 72 65 76 3b 20 ockInfo *pPrev;
d1690 20 20 20 20 2f 2a 20 20 20 20 2e 2e 2e 2e 20 64 /* .... d
d16a0 6f 75 62 6c 79 20 6c 69 6e 6b 65 64 20 2a 2f 0a oubly linked */.
d16b0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 };../*.** An ins
d16c0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
d16d0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
d16e0 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f is allocated fo
d16f0 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 r each open.** i
d1700 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74 72 75 node. This stru
d1710 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 63 cture keeps trac
d1720 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 k of the number
d1730 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 74 of locks on that
d1740 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66 20 61 .** inode. If a
d1750 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65 6d 70 close is attemp
d1760 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e 20 69 ted against an i
d1770 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68 6f 6c node that is hol
d1780 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c 20 74 ding.** locks, t
d1790 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65 66 65 he close is defe
d17a0 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6c rred until all l
d17b0 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20 61 64 ocks clear by ad
d17c0 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 ding the.** file
d17d0 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 62 descriptor to b
d17e0 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68 65 20 e closed to the
d17f0 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a 2a 2a pending list..**
d1800 0a 2a 2a 20 54 4f 44 4f 3a 20 20 43 6f 6e 73 69 .** TODO: Consi
d1810 64 65 72 20 63 68 61 6e 67 69 6e 67 20 74 68 69 der changing thi
d1820 73 20 73 6f 20 74 68 61 74 20 74 68 65 72 65 20 s so that there
d1830 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 is only a single
d1840 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 file.** descrip
d1850 74 6f 72 20 66 6f 72 20 65 61 63 68 20 6f 70 65 tor for each ope
d1860 6e 20 66 69 6c 65 2c 20 65 76 65 6e 20 77 68 65 n file, even whe
d1870 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64 20 6d n it is opened m
d1880 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 0a 2a ultiple times..*
d1890 2a 20 54 68 65 20 63 6c 6f 73 65 28 29 20 73 79 * The close() sy
d18a0 73 74 65 6d 20 63 61 6c 6c 20 77 6f 75 6c 64 20 stem call would
d18b0 6f 6e 6c 79 20 6f 63 63 75 72 20 77 68 65 6e 20 only occur when
d18c0 74 68 65 20 6c 61 73 74 20 64 61 74 61 62 61 73 the last databas
d18d0 65 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 66 e.** using the f
d18e0 69 6c 65 20 63 6c 6f 73 65 73 2e 0a 2a 2f 0a 73 ile closes..*/.s
d18f0 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
d1900 74 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 t {. struct uni
d1910 78 46 69 6c 65 49 64 20 66 69 6c 65 49 64 3b 20 xFileId fileId;
d1920 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20 /* The lookup
d1930 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 key */. int nRe
d1940 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
d1950 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
d1960 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 f pointers to th
d1970 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a is structure */.
d1980 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 20 20 int nLock;
d1990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d19a0 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 Number of outst
d19b0 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a anding locks */.
d19c0 20 20 69 6e 74 20 6e 50 65 6e 64 69 6e 67 3b 20 int nPending;
d19d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d19e0 20 4e 75 6d 62 65 72 20 6f 66 20 70 65 6e 64 69 Number of pendi
d19f0 6e 67 20 63 6c 6f 73 65 28 29 20 6f 70 65 72 61 ng close() opera
d1a00 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 2a tions */. int *
d1a10 61 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 20 aPending;
d1a20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 65 64 /* Malloced
d1a30 20 73 70 61 63 65 20 68 6f 6c 64 69 6e 67 20 66 space holding f
d1a40 64 27 73 20 61 77 61 69 74 69 6e 67 20 61 20 63 d's awaiting a c
d1a50 6c 6f 73 65 28 29 20 2a 2f 0a 23 69 66 20 4f 53 lose() */.#if OS
d1a60 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 6d 5f 74 _VXWORKS. sem_t
d1a70 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 20 20 20 *pSem;
d1a80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
d1a90 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d 61 70 amed POSIX semap
d1aa0 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 61 hore */. char a
d1ab0 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 SemName[MAX_PATH
d1ac0 4e 41 4d 45 2b 31 5d 3b 20 20 20 2f 2a 20 4e 61 NAME+1]; /* Na
d1ad0 6d 65 20 6f 66 20 74 68 61 74 20 73 65 6d 61 70 me of that semap
d1ae0 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 hore */.#endif.
d1af0 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e struct unixOpen
d1b00 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 Cnt *pNext, *pPr
d1b10 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 ev; /* List of
d1b20 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 43 6e 74 all unixOpenCnt
d1b30 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a objects */.};..
d1b40 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f 66 20 61 /*.** Lists of a
d1b50 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ll unixLockInfo
d1b60 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 and unixOpenCnt
d1b70 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 73 65 20 objects. These
d1b80 75 73 65 64 20 74 6f 20 62 65 20 68 61 73 68 0a used to be hash.
d1b90 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42 75 74 20 ** tables. But
d1ba0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 62 the number of ob
d1bb0 6a 65 63 74 73 20 69 73 20 72 61 72 65 6c 79 20 jects is rarely
d1bc0 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 more than a doze
d1bd0 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65 72 20 65 n and.** never e
d1be0 78 63 65 65 64 73 20 61 20 66 65 77 20 74 68 6f xceeds a few tho
d1bf0 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c 6f 6f 6b usand. And look
d1c00 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 63 up is not on a c
d1c10 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61 74 68 20 ritical.** path
d1c20 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c 69 6e 6b so a simple link
d1c30 65 64 20 6c 69 73 74 20 77 69 6c 6c 20 73 75 66 ed list will suf
d1c40 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 fice..*/.static
d1c50 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 struct unixLockI
d1c60 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 20 3d 20 nfo *lockList =
d1c70 30 3b 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 0;.static struct
d1c80 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 6f 70 unixOpenCnt *op
d1c90 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a enList = 0;../*.
d1ca0 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 ** This variable
d1cb0 20 72 65 6d 65 6d 62 65 72 73 20 77 68 65 74 68 remembers wheth
d1cc0 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 er or not thread
d1cd0 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 s can override e
d1ce0 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a 20 6c 6f ach others.** lo
d1cf0 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 3a cks..**.** 0:
d1d00 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 73 20 63 No. Threads c
d1d10 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65 annot override e
d1d20 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 ach others locks
d1d30 2e 20 20 28 4c 69 6e 75 78 54 68 72 65 61 64 73 . (LinuxThreads
d1d40 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59 65 73 2e ).** 1: Yes.
d1d50 20 20 54 68 72 65 61 64 73 20 63 61 6e 20 6f 76 Threads can ov
d1d60 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 erride each othe
d1d70 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50 6f 73 69 rs locks. (Posi
d1d80 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20 20 20 2d x & NLPT).** -
d1d90 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 6b 6e 6f 1: We don't kno
d1da0 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 w yet..**.** On
d1db0 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 77 65 some systems, we
d1dc0 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 know at compile
d1dd0 2d 74 69 6d 65 20 69 66 20 74 68 72 65 61 64 73 -time if threads
d1de0 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 can override ea
d1df0 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 6c 6f 63 ch.** others loc
d1e00 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 79 ks. On those sy
d1e10 73 74 65 6d 73 2c 20 74 68 65 20 53 51 4c 49 54 stems, the SQLIT
d1e20 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 E_THREAD_OVERRID
d1e30 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a 2a 20 E_LOCK macro.**
d1e40 77 69 6c 6c 20 62 65 20 73 65 74 20 61 70 70 72 will be set appr
d1e50 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f 6e 20 6f opriately. On o
d1e60 74 68 65 72 20 73 79 73 74 65 6d 73 2c 20 77 65 ther systems, we
d1e70 20 68 61 76 65 20 74 6f 20 63 68 65 63 6b 20 61 have to check a
d1e80 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e 20 20 4f t.** runtime. O
d1e90 6e 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 73 n these latter s
d1ea0 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 45 5f 54 ystems, SQLTIE_T
d1eb0 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c HREAD_OVERRIDE_L
d1ec0 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69 OCK is.** undefi
d1ed0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ned..**.** This
d1ee0 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d 61 6c 6c variable normall
d1ef0 79 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 y has file scope
d1f00 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 75 72 69 only. But duri
d1f10 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 65 20 6d ng testing, we m
d1f20 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 6c 6f 62 ake.** it a glob
d1f30 61 6c 20 73 6f 20 74 68 61 74 20 74 68 65 20 74 al so that the t
d1f40 65 73 74 20 63 6f 64 65 20 63 61 6e 20 63 68 61 est code can cha
d1f50 6e 67 65 20 69 74 73 20 76 61 6c 75 65 20 69 6e nge its value in
d1f60 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 order to verify
d1f70 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72 69 67 .** that the rig
d1f80 68 74 20 73 74 75 66 66 20 68 61 70 70 65 6e 73 ht stuff happens
d1f90 20 69 6e 20 65 69 74 68 65 72 20 63 61 73 65 2e in either case.
d1fa0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 .*/.#if SQLITE_T
d1fb0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
d1fc0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
d1fd0 23 20 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 # ifndef SQLITE
d1fe0 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 _THREAD_OVERRIDE
d1ff0 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65 66 69 6e _LOCK.# defin
d2000 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f e SQLITE_THREAD_
d2010 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 2d 31 OVERRIDE_LOCK -1
d2020 0a 23 20 20 65 6e 64 69 66 0a 23 20 20 69 66 64 .# endif.# ifd
d2030 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 ef SQLITE_TEST.i
d2040 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 nt threadsOverri
d2050 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b deEachOthersLock
d2060 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 45 41 s = SQLITE_THREA
d2070 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 3b D_OVERRIDE_LOCK;
d2080 0a 23 20 20 65 6c 73 65 0a 73 74 61 74 69 63 20 .# else.static
d2090 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 72 int threadsOverr
d20a0 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 ideEachOthersLoc
d20b0 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 45 ks = SQLITE_THRE
d20c0 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b AD_OVERRIDE_LOCK
d20d0 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 ;.# endif.#endi
d20e0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 f../*.** This st
d20f0 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e ructure holds in
d2100 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 formation passed
d2110 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c into individual
d2120 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 61 64 73 test.** threads
d2130 20 62 79 20 74 68 65 20 74 65 73 74 54 68 72 65 by the testThre
d2140 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f adLockingBehavio
d2150 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a r() routine..*/.
d2160 73 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 struct threadTes
d2170 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 20 66 64 tData {. int fd
d2180 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
d2190 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 65 20 6c /* File to be l
d21a0 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 ocked */. struc
d21b0 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 t flock lock;
d21c0 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 /* The locking
d21d0 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 operation */.
d21e0 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 20 20 20 int result;
d21f0 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
d2200 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 of the locking
d2210 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a operation */.};.
d2220 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
d2230 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 ADSAFE && define
d2240 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f 2a 0a d(__linux__)./*.
d2250 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
d2260 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 is used as the
d2270 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20 66 6f 72 main routine for
d2280 20 61 20 74 68 72 65 61 64 20 6c 61 75 6e 63 68 a thread launch
d2290 65 64 20 62 79 0a 2a 2a 20 74 65 73 74 54 68 72 ed by.** testThr
d22a0 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 eadLockingBehavi
d22b0 6f 72 28 29 2e 20 49 74 20 74 65 73 74 73 20 77 or(). It tests w
d22c0 68 65 74 68 65 72 20 74 68 65 20 73 68 61 72 65 hether the share
d22d0 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a d-lock obtained.
d22e0 2a 2a 20 62 79 20 74 68 65 20 6d 61 69 6e 20 74 ** by the main t
d22f0 68 72 65 61 64 20 69 6e 20 74 65 73 74 54 68 72 hread in testThr
d2300 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 eadLockingBehavi
d2310 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74 73 20 77 or() conflicts w
d2320 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f 74 68 65 ith a.** hypothe
d2330 74 69 63 61 6c 20 77 72 69 74 65 2d 6c 6f 63 6b tical write-lock
d2340 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 obtained by thi
d2350 73 20 74 68 72 65 61 64 20 6f 6e 20 74 68 65 20 s thread on the
d2360 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a same file..**.**
d2370 20 54 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 The write-lock
d2380 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 is not actually
d2390 61 63 71 75 69 72 65 64 2c 20 61 73 20 74 68 69 acquired, as thi
d23a0 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c s is not possibl
d23b0 65 20 69 66 20 0a 2a 2a 20 74 68 65 20 66 69 6c e if .** the fil
d23c0 65 20 69 73 20 6f 70 65 6e 20 69 6e 20 72 65 61 e is open in rea
d23d0 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28 73 65 65 d-only mode (see
d23e0 20 74 69 63 6b 65 74 20 23 33 34 37 32 29 2e 0a ticket #3472)..
d23f0 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 */ .static void
d2400 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 *threadLockingTe
d2410 73 74 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a st(void *pArg){.
d2420 20 20 73 74 72 75 63 74 20 74 68 72 65 61 64 54 struct threadT
d2430 65 73 74 44 61 74 61 20 2a 70 44 61 74 61 20 3d estData *pData =
d2440 20 28 73 74 72 75 63 74 20 74 68 72 65 61 64 54 (struct threadT
d2450 65 73 74 44 61 74 61 2a 29 70 41 72 67 3b 0a 20 estData*)pArg;.
d2460 20 70 44 61 74 61 2d 3e 72 65 73 75 6c 74 20 3d pData->result =
d2470 20 66 63 6e 74 6c 28 70 44 61 74 61 2d 3e 66 64 fcntl(pData->fd
d2480 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70 44 61 74 , F_GETLK, &pDat
d2490 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 65 74 75 a->lock);. retu
d24a0 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65 6e 64 69 rn pArg;.}.#endi
d24b0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 f /* SQLITE_THRE
d24c0 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 ADSAFE && define
d24d0 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a d(__linux__) */.
d24e0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 ..#if SQLITE_THR
d24f0 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e EADSAFE && defin
d2500 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f 2a ed(__linux__)./*
d2510 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 64 75 .** This procedu
d2520 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64 re attempts to d
d2530 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
d2540 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 73 0a or not threads.
d2550 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 ** can override
d2560 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b each others lock
d2570 73 20 74 68 65 6e 20 73 65 74 73 20 74 68 65 20 s then sets the
d2580 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65 72 72 .** threadsOverr
d2590 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 ideEachOthersLoc
d25a0 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70 70 72 ks variable appr
d25b0 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 opriately..*/.st
d25c0 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 54 68 atic void testTh
d25d0 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 readLockingBehav
d25e0 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69 67 29 ior(int fd_orig)
d25f0 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 69 6e {. int fd;. in
d2600 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 74 t rc;. struct t
d2610 68 72 65 61 64 54 65 73 74 44 61 74 61 20 64 3b hreadTestData d;
d2620 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 . struct flock
d2630 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 l;. pthread_t t
d2640 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70 28 66 64 ;.. fd = dup(fd
d2650 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28 20 66 64 _orig);. if( fd
d2660 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d <0 ) return;. m
d2670 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20 73 69 7a emset(&l, 0, siz
d2680 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e 6c 5f 74 eof(l));. l.l_t
d2690 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 ype = F_RDLCK;.
d26a0 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 l.l_len = 1;.
d26b0 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 l.l_start = 0;.
d26c0 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 l.l_whence = SE
d26d0 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20 3d 20 66 EK_SET;. rc = f
d26e0 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c 20 46 5f cntl(fd_orig, F_
d26f0 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20 20 69 66 SETLK, &l);. if
d2700 28 20 72 63 21 3d 30 20 29 20 72 65 74 75 72 6e ( rc!=0 ) return
d2710 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 2c 20 30 ;. memset(&d, 0
d2720 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b 0a 20 20 , sizeof(d));.
d2730 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20 64 2e 6c d.fd = fd;. d.l
d2740 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e 6c 6f 63 ock = l;. d.loc
d2750 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c k.l_type = F_WRL
d2760 43 4b 3b 0a 20 20 70 74 68 72 65 61 64 5f 63 72 CK;. pthread_cr
d2770 65 61 74 65 28 26 74 2c 20 30 2c 20 74 68 72 65 eate(&t, 0, thre
d2780 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 2c 20 26 adLockingTest, &
d2790 64 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6a 6f d);. pthread_jo
d27a0 69 6e 28 74 2c 20 30 29 3b 0a 20 20 63 6c 6f 73 in(t, 0);. clos
d27b0 65 28 66 64 29 3b 0a 20 20 69 66 28 20 64 2e 72 e(fd);. if( d.r
d27c0 65 73 75 6c 74 21 3d 30 20 29 20 72 65 74 75 72 esult!=0 ) retur
d27d0 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f 76 65 72 n;. threadsOver
d27e0 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f rideEachOthersLo
d27f0 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b 2e 6c 5f cks = (d.lock.l_
d2800 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 29 3b 0a type==F_UNLCK);.
d2810 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
d2820 54 45 5f 54 48 45 52 41 44 53 41 46 45 20 26 26 TE_THERADSAFE &&
d2830 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 defined(__linux
d2840 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 __) */../*.** Re
d2850 6c 65 61 73 65 20 61 20 75 6e 69 78 4c 6f 63 6b lease a unixLock
d2860 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 70 Info structure p
d2870 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 reviously alloca
d2880 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 ted by findLockI
d2890 6e 66 6f 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 nfo()..*/.static
d28a0 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4c 6f 63 void releaseLoc
d28b0 6b 49 6e 66 6f 28 73 74 72 75 63 74 20 75 6e 69 kInfo(struct uni
d28c0 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b xLockInfo *pLock
d28d0 29 7b 0a 20 20 69 66 28 20 70 4c 6f 63 6b 20 29 ){. if( pLock )
d28e0 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 {. pLock->nRe
d28f0 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f f--;. if( pLo
d2900 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 ck->nRef==0 ){.
d2910 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e if( pLock->
d2920 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20 pPrev ){.
d2930 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
d2940 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 4c pPrev->pNext==pL
d2950 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 70 ock );. p
d2960 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 Lock->pPrev->pNe
d2970 78 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 xt = pLock->pNex
d2980 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a t;. }else{.
d2990 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
d29a0 6c 6f 63 6b 4c 69 73 74 3d 3d 70 4c 6f 63 6b 20 lockList==pLock
d29b0 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 4c );. lockL
d29c0 69 73 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 ist = pLock->pNe
d29d0 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 xt;. }.
d29e0 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65 if( pLock->pNe
d29f0 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 xt ){. as
d2a00 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65 sert( pLock->pNe
d2a10 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4c 6f 63 6b xt->pPrev==pLock
d2a20 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 );. pLoc
d2a30 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 k->pNext->pPrev
d2a40 3d 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 3b 0a = pLock->pPrev;.
d2a50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
d2a60 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b lite3_free(pLock
d2a70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
d2a80 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 /*.** Release a
d2a90 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 unixOpenCnt stru
d2aa0 63 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 cture previously
d2ab0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 allocated by fi
d2ac0 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f ndLockInfo()..*/
d2ad0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c .static void rel
d2ae0 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 74 72 75 easeOpenCnt(stru
d2af0 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a ct unixOpenCnt *
d2b00 70 4f 70 65 6e 29 7b 0a 20 20 69 66 28 20 70 4f pOpen){. if( pO
d2b10 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e pen ){. pOpen
d2b20 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 ->nRef--;. if
d2b30 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d 30 ( pOpen->nRef==0
d2b40 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f ){. if( pO
d2b50 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 pen->pPrev ){.
d2b60 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
d2b70 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 pen->pPrev->pNex
d2b80 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 t==pOpen );.
d2b90 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 pOpen->pPrev
d2ba0 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e 2d ->pNext = pOpen-
d2bb0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 >pNext;. }e
d2bc0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 lse{. ass
d2bd0 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d 3d 70 ert( openList==p
d2be0 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 Open );.
d2bf0 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e openList = pOpen
d2c00 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
d2c10 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e . if( pOpen
d2c20 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 ->pNext ){.
d2c30 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e assert( pOpen
d2c40 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d ->pNext->pPrev==
d2c50 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 pOpen );.
d2c60 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 pOpen->pNext->p
d2c70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e 70 50 Prev = pOpen->pP
d2c80 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rev;. }.
d2c90 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
d2ca0 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 29 pOpen->aPending)
d2cb0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
d2cc0 66 72 65 65 28 70 4f 70 65 6e 29 3b 0a 20 20 20 free(pOpen);.
d2cd0 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
d2ce0 47 69 76 65 6e 20 61 20 66 69 6c 65 20 64 65 73 Given a file des
d2cf0 63 72 69 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20 criptor, locate
d2d00 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 unixLockInfo and
d2d10 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 unixOpenCnt str
d2d20 75 63 74 75 72 65 73 20 74 68 61 74 0a 2a 2a 20 uctures that.**
d2d30 64 65 73 63 72 69 62 65 73 20 74 68 61 74 20 66 describes that f
d2d40 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 ile descriptor.
d2d50 20 43 72 65 61 74 65 20 6e 65 77 20 6f 6e 65 73 Create new ones
d2d60 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 20 if necessary.
d2d70 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 The.** return va
d2d80 6c 75 65 73 20 6d 69 67 68 74 20 62 65 20 75 6e lues might be un
d2d90 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61 initialized if a
d2da0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a n error occurs..
d2db0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 **.** Return an
d2dc0 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f appropriate erro
d2dd0 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 r code..*/.stati
d2de0 63 20 69 6e 74 20 66 69 6e 64 4c 6f 63 6b 49 6e c int findLockIn
d2df0 66 6f 28 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a fo(. unixFile *
d2e00 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20 pFile,
d2e10 20 20 20 20 20 2f 2a 20 55 6e 69 78 20 66 69 6c /* Unix fil
d2e20 65 20 77 69 74 68 20 66 69 6c 65 20 64 65 73 63 e with file desc
d2e30 20 75 73 65 64 20 69 6e 20 74 68 65 20 6b 65 79 used in the key
d2e40 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 */. struct uni
d2e50 78 4c 6f 63 6b 49 6e 66 6f 20 2a 2a 70 70 4c 6f xLockInfo **ppLo
d2e60 63 6b 2c 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 ck, /* Return t
d2e70 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 he unixLockInfo
d2e80 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a structure here *
d2e90 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f /. struct unixO
d2ea0 70 65 6e 43 6e 74 20 2a 2a 70 70 4f 70 65 6e 20 penCnt **ppOpen
d2eb0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 /* Return the
d2ec0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 unixOpenCnt str
d2ed0 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 29 ucture here */.)
d2ee0 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
d2ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d2f00 20 20 20 2f 2a 20 53 79 73 74 65 6d 20 63 61 6c /* System cal
d2f10 6c 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f l return code */
d2f20 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 . int fd;
d2f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d2f40 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 /* The file de
d2f50 73 63 72 69 70 74 6f 72 20 66 6f 72 20 70 46 69 scriptor for pFi
d2f60 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 le */. struct u
d2f70 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b nixLockKey lockK
d2f80 65 79 3b 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70 ey; /* Lookup
d2f90 20 6b 65 79 20 66 6f 72 20 74 68 65 20 75 6e 69 key for the uni
d2fa0 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 xLockInfo struct
d2fb0 75 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ure */. struct
d2fc0 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49 unixFileId fileI
d2fd0 64 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75 d; /* Looku
d2fe0 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20 75 6e p key for the un
d2ff0 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 ixOpenCnt struct
d3000 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 74 61 */. struct sta
d3010 74 20 73 74 61 74 62 75 66 3b 20 20 20 20 20 20 t statbuf;
d3020 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 /* Low-leve
d3030 6c 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 l file informati
d3040 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 on */. struct u
d3050 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f nixLockInfo *pLo
d3060 63 6b 3b 20 20 20 20 2f 2a 20 43 61 6e 64 69 64 ck; /* Candid
d3070 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f ate unixLockInfo
d3080 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 74 72 object */. str
d3090 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 uct unixOpenCnt
d30a0 2a 70 4f 70 65 6e 3b 20 20 20 20 20 2f 2a 20 43 *pOpen; /* C
d30b0 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4f 70 65 andidate unixOpe
d30c0 6e 43 6e 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 0a nCnt object */..
d30d0 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c 65 76 /* Get low-lev
d30e0 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 el information a
d30f0 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 74 68 bout the file th
d3100 61 74 20 77 65 20 63 61 6e 20 75 73 65 64 20 74 at we can used t
d3110 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 61 20 o. ** create a
d3120 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f 72 20 unique name for
d3130 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 the file.. */.
d3140 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a fd = pFile->h;.
d3150 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 2c rc = fstat(fd,
d3160 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 69 66 &statbuf);. if
d3170 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 70 ( rc!=0 ){. p
d3180 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d3190 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64 65 66 20 = errno;.#ifdef
d31a0 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20 69 66 EOVERFLOW. if
d31b0 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 ( pFile->lastErr
d31c0 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57 20 29 20 no==EOVERFLOW )
d31d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
d31e0 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 LFS;.#endif.
d31f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
d3200 45 52 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f ERR;. }.. /* O
d3210 6e 20 4f 53 20 58 20 6f 6e 20 61 6e 20 6d 73 64 n OS X on an msd
d3220 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2c 20 74 os filesystem, t
d3230 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 20 he inode number
d3240 69 73 20 72 65 70 6f 72 74 65 64 0a 20 20 2a 2a is reported. **
d3250 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 66 6f 72 incorrectly for
d3260 20 7a 65 72 6f 2d 73 69 7a 65 20 66 69 6c 65 73 zero-size files
d3270 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 33 . See ticket #3
d3280 32 36 30 2e 20 20 54 6f 20 77 6f 72 6b 0a 20 20 260. To work.
d3290 2a 2a 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 ** around this p
d32a0 72 6f 62 6c 65 6d 20 28 77 65 20 63 6f 6e 73 69 roblem (we consi
d32b0 64 65 72 20 69 74 20 61 20 62 75 67 20 69 6e 20 der it a bug in
d32c0 4f 53 20 58 2c 20 6e 6f 74 20 53 51 4c 69 74 65 OS X, not SQLite
d32d0 29 0a 20 20 2a 2a 20 77 65 20 61 6c 77 61 79 73 ). ** we always
d32e0 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 66 69 increase the fi
d32f0 6c 65 20 73 69 7a 65 20 74 6f 20 31 20 62 79 20 le size to 1 by
d3300 77 72 69 74 69 6e 67 20 61 20 73 69 6e 67 6c 65 writing a single
d3310 20 62 79 74 65 0a 20 20 2a 2a 20 70 72 69 6f 72 byte. ** prior
d3320 20 74 6f 20 61 63 63 65 73 73 69 6e 67 20 74 68 to accessing th
d3330 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 e inode number.
d3340 20 54 68 65 20 6f 6e 65 20 62 79 74 65 20 77 72 The one byte wr
d3350 69 74 74 65 6e 20 69 73 0a 20 20 2a 2a 20 61 6e itten is. ** an
d3360 20 41 53 43 49 49 20 27 53 27 20 63 68 61 72 61 ASCII 'S' chara
d3370 63 74 65 72 20 77 68 69 63 68 20 61 6c 73 6f 20 cter which also
d3380 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 74 68 happens to be th
d3390 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20 2a e first byte. *
d33a0 2a 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 * in the header
d33b0 6f 66 20 65 76 65 72 79 20 53 51 4c 69 74 65 20 of every SQLite
d33c0 64 61 74 61 62 61 73 65 2e 20 20 49 6e 20 74 68 database. In th
d33d0 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 72 65 is way, if there
d33e0 0a 20 20 2a 2a 20 69 73 20 61 20 72 61 63 65 20 . ** is a race
d33f0 63 6f 6e 64 69 74 69 6f 6e 20 73 75 63 68 20 74 condition such t
d3400 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 hat another thre
d3410 61 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 ad has already p
d3420 6f 70 75 6c 61 74 65 64 0a 20 20 2a 2a 20 74 68 opulated. ** th
d3430 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 e first page of
d3440 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 6e 6f the database, no
d3450 20 64 61 6d 61 67 65 20 69 73 20 64 6f 6e 65 2e damage is done.
d3460 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 74 61 74 . */. if( stat
d3470 62 75 66 2e 73 74 5f 73 69 7a 65 3d 3d 30 20 29 buf.st_size==0 )
d3480 7b 0a 20 20 20 20 77 72 69 74 65 28 66 64 2c 20 {. write(fd,
d3490 22 53 22 2c 20 31 29 3b 0a 20 20 20 20 72 63 20 "S", 1);. rc
d34a0 3d 20 66 73 74 61 74 28 66 64 2c 20 26 73 74 61 = fstat(fd, &sta
d34b0 74 62 75 66 29 3b 0a 20 20 20 20 69 66 28 20 72 tbuf);. if( r
d34c0 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 46 c!=0 ){. pF
d34d0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d34e0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 errno;. re
d34f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
d3500 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 R;. }. }..
d3510 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b 65 79 2c memset(&lockKey,
d3520 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 6b 4b 0, sizeof(lockK
d3530 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b 65 79 2e ey));. lockKey.
d3540 66 69 64 2e 64 65 76 20 3d 20 73 74 61 74 62 75 fid.dev = statbu
d3550 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66 20 4f 53 f.st_dev;.#if OS
d3560 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f 63 6b 4b _VXWORKS. lockK
d3570 65 79 2e 66 69 64 2e 70 49 64 20 3d 20 70 46 69 ey.fid.pId = pFi
d3580 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a 20 le->pId;.#else.
d3590 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 69 6e 6f lockKey.fid.ino
d35a0 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 69 6e = statbuf.st_in
d35b0 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 o;.#endif.#if SQ
d35c0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
d35d0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e && defined(__lin
d35e0 75 78 5f 5f 29 0a 20 20 69 66 28 20 74 68 72 65 ux__). if( thre
d35f0 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f adsOverrideEachO
d3600 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b 0a thersLocks<0 ){.
d3610 20 20 20 20 74 65 73 74 54 68 72 65 61 64 4c 6f testThreadLo
d3620 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66 64 ckingBehavior(fd
d3630 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b 4b 65 79 );. }. lockKey
d3640 2e 74 69 64 20 3d 20 74 68 72 65 61 64 73 4f 76 .tid = threadsOv
d3650 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
d3660 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70 74 68 72 Locks ? 0 : pthr
d3670 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23 65 6e 64 ead_self();.#end
d3680 69 66 0a 20 20 66 69 6c 65 49 64 20 3d 20 6c 6f if. fileId = lo
d3690 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 69 66 28 ckKey.fid;. if(
d36a0 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b 0a 20 20 ppLock!=0 ){.
d36b0 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 6b 4c 69 pLock = lockLi
d36c0 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 st;. while( p
d36d0 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d 70 28 26 Lock && memcmp(&
d36e0 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f 63 6b 2d lockKey, &pLock-
d36f0 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a 65 6f 66 >lockKey, sizeof
d3700 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b 0a 20 20 (lockKey)) ){.
d3710 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 pLock = pLoc
d3720 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a k->pNext;. }.
d3730 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d 30 if( pLock==0
d3740 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 ){. pLock
d3750 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 = sqlite3_malloc
d3760 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f 63 6b 29 ( sizeof(*pLock)
d3770 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c );. if( pL
d3780 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ock==0 ){.
d3790 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
d37a0 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 MEM;. got
d37b0 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 o exit_findlocki
d37c0 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nfo;. }.
d37d0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 pLock->lockKe
d37e0 79 20 3d 20 6c 6f 63 6b 4b 65 79 3b 0a 20 20 20 y = lockKey;.
d37f0 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 20 3d pLock->nRef =
d3800 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 1;. pLock-
d3810 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 >cnt = 0;.
d3820 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 pLock->locktype
d3830 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b = 0;. pLock
d3840 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c 69 ->pNext = lockLi
d3850 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d st;. pLock-
d3860 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 >pPrev = 0;.
d3870 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73 74 20 29 if( lockList )
d3880 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50 72 65 76 lockList->pPrev
d3890 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 = pLock;.
d38a0 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b lockList = pLock
d38b0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
d38c0 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2b 2b pLock->nRef++
d38d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4c ;. }. *ppL
d38e0 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d ock = pLock;. }
d38f0 0a 20 20 69 66 28 20 70 70 4f 70 65 6e 21 3d 30 . if( ppOpen!=0
d3900 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 ){. pOpen =
d3910 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 77 68 openList;. wh
d3920 69 6c 65 28 20 70 4f 70 65 6e 20 26 26 20 6d 65 ile( pOpen && me
d3930 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c 20 26 70 mcmp(&fileId, &p
d3940 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c 20 73 69 Open->fileId, si
d3950 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 20 29 7b zeof(fileId)) ){
d3960 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 . pOpen = p
d3970 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Open->pNext;.
d3980 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70 65 6e }. if( pOpen
d3990 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 ==0 ){. pOp
d39a0 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c en = sqlite3_mal
d39b0 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4f 70 loc( sizeof(*pOp
d39c0 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 en) );. if(
d39d0 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 pOpen==0 ){.
d39e0 20 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b releaseLock
d39f0 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 Info(pLock);.
d3a00 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
d3a10 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
d3a20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f goto exit_findlo
d3a30 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a ckinfo;. }.
d3a40 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 66 69 6c pOpen->fil
d3a50 65 49 64 20 3d 20 66 69 6c 65 49 64 3b 0a 20 20 eId = fileId;.
d3a60 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 20 pOpen->nRef
d3a70 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e = 1;. pOpen
d3a80 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 ->nLock = 0;.
d3a90 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 pOpen->nPendi
d3aa0 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f ng = 0;. pO
d3ab0 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20 pen->aPending =
d3ac0 30 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 0;. pOpen->
d3ad0 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69 73 74 pNext = openList
d3ae0 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 ;. pOpen->p
d3af0 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 Prev = 0;.
d3b00 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 20 6f if( openList ) o
d3b10 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d penList->pPrev =
d3b20 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f 70 pOpen;. op
d3b30 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b 0a enList = pOpen;.
d3b40 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
d3b50 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 53 65 6d pOpen->pSem
d3b60 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 70 = NULL;. p
d3b70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b 30 Open->aSemName[0
d3b80 5d 20 3d 20 27 5c 30 27 3b 0a 23 65 6e 64 69 66 ] = '\0';.#endif
d3b90 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
d3ba0 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b 3b pOpen->nRef++;
d3bb0 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f 70 . }. *ppOp
d3bc0 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d 0a en = pOpen;. }.
d3bd0 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e .exit_findlockin
d3be0 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b fo:. return rc;
d3bf0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 .}../*.** If we
d3c00 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e are currently in
d3c10 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 a different thr
d3c20 65 61 64 20 74 68 61 6e 20 74 68 65 20 74 68 72 ead than the thr
d3c30 65 61 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ead that the.**
d3c40 75 6e 69 78 46 69 6c 65 20 61 72 67 75 6d 65 6e unixFile argumen
d3c50 74 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 t belongs to, th
d3c60 65 6e 20 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 en transfer owne
d3c70 72 73 68 69 70 20 6f 66 20 74 68 65 20 75 6e 69 rship of the uni
d3c80 78 46 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f xFile.** over to
d3c90 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 the current thr
d3ca0 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 ead..**.** A uni
d3cb0 78 46 69 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 xFile is only ow
d3cc0 6e 65 64 20 62 79 20 61 20 74 68 72 65 61 64 20 ned by a thread
d3cd0 6f 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 on systems that
d3ce0 75 73 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 use LinuxThreads
d3cf0 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 69 ..**.** Ownershi
d3d00 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 6f 6e p transfer is on
d3d10 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 74 68 ly allowed if th
d3d20 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20 63 75 e unixFile is cu
d3d30 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 rrently unlocked
d3d40 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 69 78 ..** If the unix
d3d50 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 61 File is locked a
d3d60 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 70 20 nd an ownership
d3d70 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20 72 is wrong, then r
d3d80 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f eturn.** SQLITE_
d3d90 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54 45 5f MISUSE. SQLITE_
d3da0 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 OK is returned i
d3db0 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 f everything wor
d3dc0 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ks..*/.#if SQLIT
d3dd0 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
d3de0 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
d3df0 5f 29 0a 73 74 61 74 69 63 20 69 6e 74 20 74 72 _).static int tr
d3e00 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 ansferOwnership(
d3e10 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 unixFile *pFile)
d3e20 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 74 {. int rc;. pt
d3e30 68 72 65 61 64 5f 74 20 68 53 65 6c 66 3b 0a 20 hread_t hSelf;.
d3e40 20 69 66 28 20 74 68 72 65 61 64 73 4f 76 65 72 if( threadsOver
d3e50 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f rideEachOthersLo
d3e60 63 6b 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 77 cks ){. /* Ow
d3e70 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 nership transfer
d3e80 73 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e 20 s not needed on
d3e90 74 68 69 73 20 73 79 73 74 65 6d 20 2a 2f 0a 20 this system */.
d3ea0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d3eb0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 6c 66 _OK;. }. hSelf
d3ec0 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 = pthread_self(
d3ed0 29 3b 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 );. if( pthread
d3ee0 5f 65 71 75 61 6c 28 70 46 69 6c 65 2d 3e 74 69 _equal(pFile->ti
d3ef0 64 2c 20 68 53 65 6c 66 29 20 29 7b 0a 20 20 20 d, hSelf) ){.
d3f00 20 2f 2a 20 57 65 20 61 72 65 20 73 74 69 6c 6c /* We are still
d3f10 20 69 6e 20 74 68 65 20 73 61 6d 65 20 74 68 72 in the same thr
d3f20 65 61 64 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 ead */. OSTRA
d3f30 43 45 31 28 22 4e 6f 2d 74 72 61 6e 73 66 65 72 CE1("No-transfer
d3f40 2c 20 73 61 6d 65 20 74 68 72 65 61 64 5c 6e 22 , same thread\n"
d3f50 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
d3f60 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 LITE_OK;. }. i
d3f70 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d3f80 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 pe!=NO_LOCK ){.
d3f90 20 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 /* We cannot
d3fa0 63 68 61 6e 67 65 20 6f 77 6e 65 72 73 68 69 70 change ownership
d3fb0 20 77 68 69 6c 65 20 77 65 20 61 72 65 20 68 6f while we are ho
d3fc0 6c 64 69 6e 67 20 61 20 6c 6f 63 6b 21 20 2a 2f lding a lock! */
d3fd0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d3fe0 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 TE_MISUSE;. }.
d3ff0 20 4f 53 54 52 41 43 45 34 28 22 54 72 61 6e 73 OSTRACE4("Trans
d4000 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 fer ownership of
d4010 20 25 64 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 %d from %d to %
d4020 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
d4030 20 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c pFile->h, pFil
d4040 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 3b 0a e->tid, hSelf);.
d4050 20 20 70 46 69 6c 65 2d 3e 74 69 64 20 3d 20 68 pFile->tid = h
d4060 53 65 6c 66 3b 0a 20 20 69 66 20 28 70 46 69 6c Self;. if (pFil
d4070 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c 4c e->pLock != NULL
d4080 29 20 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c ) {. releaseL
d4090 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 ockInfo(pFile->p
d40a0 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 Lock);. rc =
d40b0 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 findLockInfo(pFi
d40c0 6c 65 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 le, &pFile->pLoc
d40d0 6b 2c 20 30 29 3b 0a 20 20 20 20 4f 53 54 52 41 k, 0);. OSTRA
d40e0 43 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 CE5("LOCK %d
d40f0 69 73 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 is now %s(%s,%d)
d4100 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 \n", pFile->h,.
d4110 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 lockty
d4120 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f peName(pFile->lo
d4130 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 20 cktype),.
d4140 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 locktypeName
d4150 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c (pFile->pLock->l
d4160 6f 63 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 2d ocktype), pFile-
d4170 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 >pLock->cnt);.
d4180 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
d4190 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 else {. retu
d41a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
d41b0 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66 }.}.#else /* if
d41c0 20 6e 6f 74 20 53 51 4c 49 54 45 5f 54 48 52 45 not SQLITE_THRE
d41d0 41 44 53 41 46 45 20 2a 2f 0a 20 20 2f 2a 20 4f ADSAFE */. /* O
d41e0 6e 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 n single-threade
d41f0 64 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 d builds, owners
d4200 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 hip transfer is
d4210 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 a no-op */.# def
d4220 69 6e 65 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 ine transferOwne
d4230 72 73 68 69 70 28 58 29 20 53 51 4c 49 54 45 5f rship(X) SQLITE_
d4240 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c OK.#endif /* SQL
d4250 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a ITE_THREADSAFE *
d4260 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 /.../*.** This r
d4270 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 outine checks if
d4280 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 there is a RESE
d4290 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f RVED lock held o
d42a0 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a n the specified.
d42b0 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 ** file by this
d42c0 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f or any other pro
d42d0 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 cess. If such a
d42e0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 lock is held, se
d42f0 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f t *pResOut.** to
d4300 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 a non-zero valu
d4310 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 e otherwise *pRe
d4320 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a sOut is set to z
d4330 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e ero. The return
d4340 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 value.** is set
d4350 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e to SQLITE_OK un
d4360 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f less an I/O erro
d4370 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 r occurs during
d4380 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a lock checking..*
d4390 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
d43a0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
d43b0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d43c0 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 *id, int *pResOu
d43d0 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
d43e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 QLITE_OK;. int
d43f0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 reserved = 0;.
d4400 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d4410 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d4420 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 .. SimulateIOEr
d4430 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
d4440 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 TE_IOERR_CHECKRE
d4450 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 0a SERVEDLOCK; );..
d4460 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
d4470 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 );. unixEnterMu
d4480 74 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 tex(); /* Becaus
d4490 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 e pFile->pLock i
d44a0 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 s shared across
d44b0 74 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a threads */.. /*
d44c0 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 Check if a thre
d44d0 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 ad in this proce
d44e0 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 ss holds such a
d44f0 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 lock */. if( pF
d4500 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b ile->pLock->lock
d4510 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
d4520 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 ){. reserved
d4530 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 1;. }.. /*
d4540 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 Otherwise see if
d4550 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
d4560 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 ess holds it..
d4570 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 */. if( !reserv
d4580 65 64 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 ed ){. struct
d4590 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20 flock lock;.
d45a0 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d lock.l_whence =
d45b0 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c SEEK_SET;. l
d45c0 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 ock.l_start = RE
d45d0 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 SERVED_BYTE;.
d45e0 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b lock.l_len = 1;
d45f0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 . lock.l_type
d4600 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 = F_WRLCK;.
d4610 69 66 20 28 2d 31 20 3d 3d 20 66 63 6e 74 6c 28 if (-1 == fcntl(
d4620 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c pFile->h, F_GETL
d4630 4b 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 20 K, &lock)) {.
d4640 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 int tErrno =
d4650 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 errno;. rc
d4660 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
d4670 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
d4680 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
d4690 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f _CHECKRESERVEDLO
d46a0 43 4b 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 CK);. pFile
d46b0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
d46c0 72 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 65 rrno;. } else
d46d0 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 if( lock.l_type
d46e0 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 !=F_UNLCK ){.
d46f0 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b reserved = 1;
d4700 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 . }. }. .
d4710 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
d4720 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 ;. OSTRACE4("TE
d4730 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
d4740 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
d4750 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b , rc, reserved);
d4760 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 .. *pResOut = r
d4770 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 eserved;. retur
d4780 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
d4790 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 ock the file wit
d47a0 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 h the lock speci
d47b0 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
d47c0 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 r locktype - one
d47d0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f .** of the follo
d47e0 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 wing:.**.**
d47f0 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a (1) SHARED_LOCK.
d4800 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 ** (2) RESER
d4810 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 VED_LOCK.**
d4820 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b (3) PENDING_LOCK
d4830 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c .** (4) EXCL
d4840 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a USIVE_LOCK.**.**
d4850 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 Sometimes when
d4860 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c requesting one l
d4870 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 ock state, addit
d4880 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 ional lock state
d4890 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 s.** are inserte
d48a0 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 d in between. T
d48b0 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 he locking might
d48c0 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 fail on one of
d48d0 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 the later.** tra
d48e0 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 nsitions leaving
d48f0 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 the lock state
d4900 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 different from w
d4910 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 hat it started b
d4920 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 ut.** still shor
d4930 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 t of its goal.
d4940 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 The following ch
d4950 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c art shows the al
d4960 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 lowed.** transit
d4970 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 ions and the ins
d4980 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 erted intermedia
d4990 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a te states:.**.**
d49a0 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 UNLOCKED ->
d49b0 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 SHARED.** SHA
d49c0 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a RED -> RESERVED.
d49d0 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
d49e0 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
d49f0 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 LUSIVE.** RES
d4a00 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e ERVED -> (PENDIN
d4a10 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
d4a20 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e ** PENDING ->
d4a30 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a EXCLUSIVE.**.**
d4a40 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 This routine wi
d4a50 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 ll only increase
d4a60 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 a lock. Use th
d4a70 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 e sqlite3OsUnloc
d4a80 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 k().** routine t
d4a90 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e o lower a lockin
d4aa0 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 g level..*/.stat
d4ab0 69 63 20 69 6e 74 20 75 6e 69 78 4c 6f 63 6b 28 ic int unixLock(
d4ac0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d4ad0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b , int locktype){
d4ae0 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . /* The follow
d4af0 69 6e 67 20 64 65 73 63 72 69 62 65 73 20 74 68 ing describes th
d4b00 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
d4b10 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 of the various
d4b20 6c 6f 63 6b 73 20 61 6e 64 0a 20 20 2a 2a 20 6c locks and. ** l
d4b30 6f 63 6b 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 ock transitions
d4b40 69 6e 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 in terms of the
d4b50 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 73 POSIX advisory s
d4b60 68 61 72 65 64 20 61 6e 64 20 65 78 63 6c 75 73 hared and exclus
d4b70 69 76 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 70 72 ive. ** lock pr
d4b80 69 6d 69 74 69 76 65 73 20 28 63 61 6c 6c 65 64 imitives (called
d4b90 20 72 65 61 64 2d 6c 6f 63 6b 73 20 61 6e 64 20 read-locks and
d4ba0 77 72 69 74 65 2d 6c 6f 63 6b 73 20 62 65 6c 6f write-locks belo
d4bb0 77 2c 20 74 6f 20 61 76 6f 69 64 0a 20 20 2a 2a w, to avoid. **
d4bc0 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 confusion with
d4bd0 53 51 4c 69 74 65 20 6c 6f 63 6b 20 6e 61 6d 65 SQLite lock name
d4be0 73 29 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68 s). The algorith
d4bf0 6d 73 20 61 72 65 20 63 6f 6d 70 6c 69 63 61 74 ms are complicat
d4c00 65 64 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 ed. ** slightly
d4c10 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 in order to be
d4c20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 compatible with
d4c30 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 20 windows systems
d4c40 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 0a 20 simultaneously.
d4c50 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 ** accessing th
d4c60 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
d4c70 66 69 6c 65 2c 20 69 6e 20 63 61 73 65 20 74 68 file, in case th
d4c80 61 74 20 69 73 20 65 76 65 72 20 72 65 71 75 69 at is ever requi
d4c90 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 red.. **. ** S
d4ca0 79 6d 62 6f 6c 73 20 64 65 66 69 6e 65 64 20 69 ymbols defined i
d4cb0 6e 20 6f 73 2e 68 20 69 6e 64 65 6e 74 69 66 79 n os.h indentify
d4cc0 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 the 'pending by
d4cd0 74 65 27 20 61 6e 64 20 74 68 65 20 27 72 65 73 te' and the 'res
d4ce0 65 72 76 65 64 0a 20 20 2a 2a 20 62 79 74 65 27 erved. ** byte'
d4cf0 2c 20 65 61 63 68 20 73 69 6e 67 6c 65 20 62 79 , each single by
d4d00 74 65 73 20 61 74 20 77 65 6c 6c 20 6b 6e 6f 77 tes at well know
d4d10 6e 20 6f 66 66 73 65 74 73 2c 20 61 6e 64 20 74 n offsets, and t
d4d20 68 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a he 'shared byte.
d4d30 20 20 2a 2a 20 72 61 6e 67 65 27 2c 20 61 20 72 ** range', a r
d4d40 61 6e 67 65 20 6f 66 20 35 31 30 20 62 79 74 65 ange of 510 byte
d4d50 73 20 61 74 20 61 20 77 65 6c 6c 20 6b 6e 6f 77 s at a well know
d4d60 6e 20 6f 66 66 73 65 74 2e 0a 20 20 2a 2a 0a 20 n offset.. **.
d4d70 20 2a 2a 20 54 6f 20 6f 62 74 61 69 6e 20 61 20 ** To obtain a
d4d80 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 61 20 72 SHARED lock, a r
d4d90 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 ead-lock is obta
d4da0 69 6e 65 64 20 6f 6e 20 74 68 65 20 27 70 65 6e ined on the 'pen
d4db0 64 69 6e 67 0a 20 20 2a 2a 20 62 79 74 65 27 2e ding. ** byte'.
d4dc0 20 20 49 66 20 74 68 69 73 20 69 73 20 73 75 63 If this is suc
d4dd0 63 65 73 73 66 75 6c 2c 20 61 20 72 61 6e 64 6f cessful, a rando
d4de0 6d 20 62 79 74 65 20 66 72 6f 6d 20 74 68 65 20 m byte from the
d4df0 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 'shared byte. *
d4e00 2a 20 72 61 6e 67 65 27 20 69 73 20 72 65 61 64 * range' is read
d4e10 2d 6c 6f 63 6b 65 64 20 61 6e 64 20 74 68 65 20 -locked and the
d4e20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 27 70 65 6e lock on the 'pen
d4e30 64 69 6e 67 20 62 79 74 65 27 20 72 65 6c 65 61 ding byte' relea
d4e40 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 sed.. **. ** A
d4e50 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c process may onl
d4e60 79 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 y obtain a RESER
d4e70 56 45 44 20 6c 6f 63 6b 20 61 66 74 65 72 20 69 VED lock after i
d4e80 74 20 68 61 73 20 61 20 53 48 41 52 45 44 20 6c t has a SHARED l
d4e90 6f 63 6b 2e 0a 20 20 2a 2a 20 41 20 52 45 53 45 ock.. ** A RESE
d4ea0 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 69 6d 70 RVED lock is imp
d4eb0 6c 65 6d 65 6e 74 65 64 20 62 79 20 67 72 61 62 lemented by grab
d4ec0 62 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 bing a write-loc
d4ed0 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 27 72 k on the. ** 'r
d4ee0 65 73 65 72 76 65 64 20 62 79 74 65 27 2e 20 0a eserved byte'. .
d4ef0 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 **. ** A proc
d4f00 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 ess may only obt
d4f10 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f ain a PENDING lo
d4f20 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 20 ck after it has
d4f30 6f 62 74 61 69 6e 65 64 20 61 0a 20 20 2a 2a 20 obtained a. **
d4f40 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 41 20 50 SHARED lock. A P
d4f50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 69 ENDING lock is i
d4f60 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 mplemented by ob
d4f70 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d taining a write-
d4f80 6c 6f 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 lock. ** on the
d4f90 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 2e 'pending byte'.
d4fa0 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 This ensures th
d4fb0 61 74 20 6e 6f 20 6e 65 77 20 53 48 41 52 45 44 at no new SHARED
d4fc0 20 6c 6f 63 6b 73 20 63 61 6e 20 62 65 0a 20 20 locks can be.
d4fd0 2a 2a 20 6f 62 74 61 69 6e 65 64 2c 20 62 75 74 ** obtained, but
d4fe0 20 65 78 69 73 74 69 6e 67 20 53 48 41 52 45 44 existing SHARED
d4ff0 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c 6c 6f 77 locks are allow
d5000 65 64 20 74 6f 20 70 65 72 73 69 73 74 2e 20 41 ed to persist. A
d5010 20 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 64 6f process. ** do
d5020 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6f es not have to o
d5030 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 btain a RESERVED
d5040 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 77 61 79 lock on the way
d5050 20 74 6f 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f to a PENDING lo
d5060 63 6b 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 ck.. ** This pr
d5070 6f 70 65 72 74 79 20 69 73 20 75 73 65 64 20 62 operty is used b
d5080 79 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 y the algorithm
d5090 66 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b for rolling back
d50a0 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a a journal file.
d50b0 20 20 2a 2a 20 61 66 74 65 72 20 61 20 63 72 61 ** after a cra
d50c0 73 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e sh.. **. ** An
d50d0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c EXCLUSIVE lock,
d50e0 20 6f 62 74 61 69 6e 65 64 20 61 66 74 65 72 20 obtained after
d50f0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 a PENDING lock i
d5100 73 20 68 65 6c 64 2c 20 69 73 0a 20 20 2a 2a 20 s held, is. **
d5110 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f implemented by o
d5120 62 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 btaining a write
d5130 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 65 6e 74 -lock on the ent
d5140 69 72 65 20 27 73 68 61 72 65 64 20 62 79 74 65 ire 'shared byte
d5150 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2e 20 53 69 . ** range'. Si
d5160 6e 63 65 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f nce all other lo
d5170 63 6b 73 20 72 65 71 75 69 72 65 20 61 20 72 65 cks require a re
d5180 61 64 2d 6c 6f 63 6b 20 6f 6e 20 6f 6e 65 20 6f ad-lock on one o
d5190 66 20 74 68 65 20 62 79 74 65 73 0a 20 20 2a 2a f the bytes. **
d51a0 20 77 69 74 68 69 6e 20 74 68 69 73 20 72 61 6e within this ran
d51b0 67 65 2c 20 74 68 69 73 20 65 6e 73 75 72 65 73 ge, this ensures
d51c0 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 6c that no other l
d51d0 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f 6e ocks are held on
d51e0 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 the. ** databa
d51f0 73 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 se. . **. ** T
d5200 68 65 20 72 65 61 73 6f 6e 20 61 20 73 69 6e 67 he reason a sing
d5210 6c 65 20 62 79 74 65 20 63 61 6e 6e 6f 74 20 62 le byte cannot b
d5220 65 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f e used instead o
d5230 66 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79 f the 'shared by
d5240 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69 te. ** range' i
d5250 73 20 74 68 61 74 20 73 6f 6d 65 20 76 65 72 73 s that some vers
d5260 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 ions of windows
d5270 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 do not support r
d5280 65 61 64 2d 6c 6f 63 6b 73 2e 20 42 79 0a 20 20 ead-locks. By.
d5290 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 20 72 61 6e ** locking a ran
d52a0 64 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 61 20 dom byte from a
d52b0 72 61 6e 67 65 2c 20 63 6f 6e 63 75 72 72 65 6e range, concurren
d52c0 74 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d t SHARED locks m
d52d0 61 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 65 76 ay exist. ** ev
d52e0 65 6e 20 69 66 20 74 68 65 20 6c 6f 63 6b 69 6e en if the lockin
d52f0 67 20 70 72 69 6d 69 74 69 76 65 20 75 73 65 64 g primitive used
d5300 20 69 73 20 61 6c 77 61 79 73 20 61 20 77 72 69 is always a wri
d5310 74 65 2d 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 te-lock.. */.
d5320 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
d5330 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a OK;. unixFile *
d5340 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d5350 65 2a 29 69 64 3b 0a 20 20 73 74 72 75 63 74 20 e*)id;. struct
d5360 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c unixLockInfo *pL
d5370 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f ock = pFile->pLo
d5380 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f ck;. struct flo
d5390 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 73 ck lock;. int s
d53a0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 ;.. assert( pFi
d53b0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 le );. OSTRACE7
d53c0 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 ("LOCK %d %s
d53d0 77 61 73 20 25 73 28 25 73 2c 25 64 29 20 70 69 was %s(%s,%d) pi
d53e0 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d=%d\n", pFile->
d53f0 68 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 h,. locktyp
d5400 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c eName(locktype),
d5410 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 locktypeName(pF
d5420 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a ile->locktype),.
d5430 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 locktypeNa
d5440 6d 65 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 me(pLock->lockty
d5450 70 65 29 2c 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 pe), pLock->cnt
d5460 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 , getpid());..
d5470 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 /* If there is a
d5480 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 lready a lock of
d5490 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f this type or mo
d54a0 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f re restrictive o
d54b0 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 n the. ** unixF
d54c0 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e ile, do nothing.
d54d0 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 Don't use the e
d54e0 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 nd_lock: exit pa
d54f0 74 68 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 th, as. ** unix
d5500 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 EnterMutex() has
d5510 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 n't been called
d5520 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 yet.. */. if(
d5530 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
d5540 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
d5550 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 OSTRACE3("LOCK
d5560 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 %d %s ok (alr
d5570 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 eady held)\n", p
d5580 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
d5590 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d locktypeNam
d55a0 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 e(locktype));.
d55b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d55c0 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 OK;. }.. /* Ma
d55d0 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b ke sure the lock
d55e0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 ing sequence is
d55f0 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 correct. */. a
d5600 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f ssert( pFile->lo
d5610 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 cktype!=NO_LOCK
d5620 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 || locktype==SHA
d5630 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 RED_LOCK );. as
d5640 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d sert( locktype!=
d5650 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a PENDING_LOCK );.
d5660 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
d5670 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe!=RESERVED_LOC
d5680 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b K || pFile->lock
d5690 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
d56a0 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 K );.. /* This
d56b0 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 20 mutex is needed
d56c0 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 because pFile->p
d56d0 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 Lock is shared a
d56e0 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20 cross threads.
d56f0 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 */. unixEnterMu
d5700 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b tex();.. /* Mak
d5710 65 20 73 75 72 65 20 74 68 65 20 63 75 72 72 65 e sure the curre
d5720 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 nt thread owns t
d5730 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 he pFile.. */.
d5740 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 rc = transferOw
d5750 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a nership(pFile);.
d5760 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
d5770 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c _OK ){. unixL
d5780 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 eaveMutex();.
d5790 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
d57a0 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d pLock = pFile-
d57b0 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 >pLock;.. /* If
d57c0 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 73 69 some thread usi
d57d0 6e 67 20 74 68 69 73 20 50 49 44 20 68 61 73 20 ng this PID has
d57e0 61 20 6c 6f 63 6b 20 76 69 61 20 61 20 64 69 66 a lock via a dif
d57f0 66 65 72 65 6e 74 20 75 6e 69 78 46 69 6c 65 2a ferent unixFile*
d5800 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74 68 61 . ** handle tha
d5810 74 20 70 72 65 63 6c 75 64 65 73 20 74 68 65 20 t precludes the
d5820 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2c 20 requested lock,
d5830 72 65 74 75 72 6e 20 42 55 53 59 2e 0a 20 20 2a return BUSY.. *
d5840 2f 0a 20 20 69 66 28 20 28 70 46 69 6c 65 2d 3e /. if( (pFile->
d5850 6c 6f 63 6b 74 79 70 65 21 3d 70 4c 6f 63 6b 2d locktype!=pLock-
d5860 3e 6c 6f 63 6b 74 79 70 65 20 26 26 20 0a 20 20 >locktype && .
d5870 20 20 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e (pLock->
d5880 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e locktype>=PENDIN
d5890 47 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 G_LOCK || lockty
d58a0 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29 29 pe>SHARED_LOCK))
d58b0 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 . ){. rc = S
d58c0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
d58d0 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 goto end_lock;.
d58e0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 53 48 }.. /* If a SH
d58f0 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72 65 71 ARED lock is req
d5900 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f 6d 65 uested, and some
d5910 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 thread using th
d5920 69 73 20 50 49 44 20 61 6c 72 65 61 64 79 0a 20 is PID already.
d5930 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52 45 44 ** has a SHARED
d5940 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c 6f 63 or RESERVED loc
d5950 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e k, then incremen
d5960 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e t reference coun
d5970 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65 74 75 ts and. ** retu
d5980 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 rn SQLITE_OK..
d5990 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
d59a0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 e==SHARED_LOCK &
d59b0 26 20 0a 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d & . (pLock-
d59c0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 >locktype==SHARE
d59d0 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d D_LOCK || pLock-
d59e0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 >locktype==RESER
d59f0 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 VED_LOCK) ){.
d5a00 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
d5a10 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
d5a20 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 ;. assert( pF
d5a30 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 ile->locktype==0
d5a40 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
d5a50 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a pLock->cnt>0 );.
d5a60 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d5a70 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 ype = SHARED_LOC
d5a80 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e K;. pLock->cn
d5a90 74 2b 2b 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e t++;. pFile->
d5aa0 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a pOpen->nLock++;.
d5ab0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 goto end_loc
d5ac0 6b 3b 0a 20 20 7d 0a 0a 20 20 6c 6f 63 6b 2e 6c k;. }.. lock.l
d5ad0 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 0a 20 20 6c 6f _len = 1L;.. lo
d5ae0 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 ck.l_whence = SE
d5af0 45 4b 5f 53 45 54 3b 0a 0a 20 20 2f 2a 20 41 20 EK_SET;.. /* A
d5b00 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 PENDING lock is
d5b10 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20 61 63 needed before ac
d5b20 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 quiring a SHARED
d5b30 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 lock and before
d5b40 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 . ** acquiring
d5b50 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
d5b60 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 k. For the SHAR
d5b70 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e ED lock, the PEN
d5b80 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 DING will. ** b
d5b90 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f e released.. */
d5ba0 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
d5bb0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 =SHARED_LOCK .
d5bc0 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 || (locktype
d5bd0 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
d5be0 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 && pFile->lockt
d5bf0 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b ype<PENDING_LOCK
d5c00 29 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e ). ){. lock.
d5c10 6c 5f 74 79 70 65 20 3d 20 28 6c 6f 63 6b 74 79 l_type = (lockty
d5c20 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 3f pe==SHARED_LOCK?
d5c30 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b 29 F_RDLCK:F_WRLCK)
d5c40 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 ;. lock.l_sta
d5c50 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 rt = PENDING_BYT
d5c60 45 3b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c E;. s = fcntl
d5c70 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 (pFile->h, F_SET
d5c80 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 LK, &lock);.
d5c90 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 if( s==(-1) ){.
d5ca0 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 int tErrno
d5cb0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 = errno;. r
d5cc0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
d5cd0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
d5ce0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
d5cf0 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 RR_LOCK);.
d5d00 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
d5d10 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 R(rc) ){.
d5d20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d5d30 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d5d40 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 }. goto e
d5d50 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 nd_lock;. }.
d5d60 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e }... /* If con
d5d70 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69 trol gets to thi
d5d80 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 s point, then ac
d5d90 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 tually go ahead
d5da0 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 and make. ** op
d5db0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 erating system c
d5dc0 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65 alls for the spe
d5dd0 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a cified lock.. *
d5de0 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 /. if( locktype
d5df0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b ==SHARED_LOCK ){
d5e00 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 . int tErrno
d5e10 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 = 0;. assert(
d5e20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 pLock->cnt==0 )
d5e30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c ;. assert( pL
d5e40 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 ock->locktype==0
d5e50 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 );.. /* Now
d5e60 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 get the read-loc
d5e70 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f k */. lock.l_
d5e80 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 start = SHARED_F
d5e90 49 52 53 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c IRST;. lock.l
d5ea0 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 _len = SHARED_SI
d5eb0 5a 45 3b 0a 20 20 20 20 69 66 28 20 28 73 20 3d ZE;. if( (s =
d5ec0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c fcntl(pFile->h,
d5ed0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_SETLK, &lock)
d5ee0 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 )==(-1) ){.
d5ef0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
d5f00 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 . }. /* Dr
d5f10 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 op the temporary
d5f20 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f PENDING lock */
d5f30 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 . lock.l_star
d5f40 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 t = PENDING_BYTE
d5f50 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e ;. lock.l_len
d5f60 20 3d 20 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e = 1L;. lock.
d5f70 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b l_type = F_UNLCK
d5f80 3b 0a 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 ;. if( fcntl(
d5f90 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c pFile->h, F_SETL
d5fa0 4b 2c 20 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a K, &lock)!=0 ){.
d5fb0 20 20 20 20 20 20 69 66 28 20 73 20 21 3d 20 2d if( s != -
d5fc0 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 1 ){. /*
d5fd0 54 68 69 73 20 63 6f 75 6c 64 20 68 61 70 70 65 This could happe
d5fe0 6e 20 77 69 74 68 20 61 20 6e 65 74 77 6f 72 6b n with a network
d5ff0 20 6d 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 mount */.
d6000 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f tErrno = errno
d6010 3b 20 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ; . rc =
d6020 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d6030 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d6040 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 , SQLITE_IOERR_U
d6050 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 NLOCK); .
d6060 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
d6070 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 OR(rc) ){.
d6080 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d6090 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
d60a0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
d60b0 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a goto end_lock;.
d60c0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
d60d0 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b if( s==(-1) ){
d60e0 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
d60f0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
d6100 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
d6110 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 LITE_IOERR_LOCK)
d6120 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c ;. if( IS_L
d6130 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b OCK_ERROR(rc) ){
d6140 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
d6150 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
d6160 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 no;. }.
d6170 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 }else{. pFi
d6180 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 le->locktype = S
d6190 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 HARED_LOCK;.
d61a0 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e pFile->pOpen->
d61b0 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 nLock++;. p
d61c0 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20 Lock->cnt = 1;.
d61d0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
d61e0 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
d61f0 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f SIVE_LOCK && pLo
d6200 63 6b 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20 ck->cnt>1 ){.
d6210 20 2f 2a 20 57 65 20 61 72 65 20 74 72 79 69 6e /* We are tryin
d6220 67 20 66 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 g for an exclusi
d6230 76 65 20 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74 ve lock but anot
d6240 68 65 72 20 74 68 72 65 61 64 20 69 6e 20 74 68 her thread in th
d6250 69 73 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 is. ** same p
d6260 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 rocess is still
d6270 68 6f 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 holding a shared
d6280 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63 lock. */. rc
d6290 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a = SQLITE_BUSY;.
d62a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
d62b0 54 68 65 20 72 65 71 75 65 73 74 20 77 61 73 20 The request was
d62c0 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 20 6f for a RESERVED o
d62d0 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b r EXCLUSIVE lock
d62e0 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 . It is. **
d62f0 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 assumed that the
d6300 72 65 20 69 73 20 61 20 53 48 41 52 45 44 20 6f re is a SHARED o
d6310 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f r greater lock o
d6320 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a n the file. *
d6330 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a * already.. *
d6340 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 21 /. assert( 0!
d6350 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 =pFile->locktype
d6360 20 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 );. lock.l_t
d6370 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 ype = F_WRLCK;.
d6380 20 20 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 switch( lockt
d6390 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 ype ){. cas
d63a0 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a e RESERVED_LOCK:
d63b0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f . lock.l_
d63c0 73 74 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 start = RESERVED
d63d0 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 20 20 62 _BYTE;. b
d63e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
d63f0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a EXCLUSIVE_LOCK:
d6400 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f . lock.l_
d6410 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 start = SHARED_F
d6420 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f IRST;. lo
d6430 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 ck.l_len = SHARE
d6440 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 D_SIZE;.
d6450 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 break;. def
d6460 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 61 73 ault:. as
d6470 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20 sert(0);. }.
d6480 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 s = fcntl(pFi
d6490 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 le->h, F_SETLK,
d64a0 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 &lock);. if(
d64b0 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 s==(-1) ){.
d64c0 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 int tErrno = er
d64d0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 rno;. rc =
d64e0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d64f0 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d6500 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c , SQLITE_IOERR_L
d6510 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 OCK);. if(
d6520 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
d6530 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 ) ){. pFi
d6540 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d6550 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a tErrno;. }.
d6560 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 0a 23 69 }. }. ..#i
d6570 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f fndef NDEBUG. /
d6580 2a 20 53 65 74 20 75 70 20 74 68 65 20 74 72 61 * Set up the tra
d6590 6e 73 61 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72 nsaction-counter
d65a0 20 63 68 61 6e 67 65 20 63 68 65 63 6b 69 6e 67 change checking
d65b0 20 66 6c 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a flags when. **
d65c0 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 transitioning f
d65d0 72 6f 6d 20 61 20 53 48 41 52 45 44 20 74 6f 20 rom a SHARED to
d65e0 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e a RESERVED lock.
d65f0 20 20 54 68 65 20 63 68 61 6e 67 65 0a 20 20 2a The change. *
d6600 2a 20 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f * from SHARED to
d6610 20 52 45 53 45 52 56 45 44 20 6d 61 72 6b 73 20 RESERVED marks
d6620 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
d6630 20 61 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77 a normal. ** w
d6640 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 rite operation (
d6650 6e 6f 74 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 not a hot journa
d6660 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a l rollback).. *
d6670 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
d6680 54 45 5f 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c TE_OK. && pFil
d6690 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 e->locktype<=SHA
d66a0 52 45 44 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 6c RED_LOCK. && l
d66b0 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 ocktype==RESERVE
d66c0 44 5f 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 20 D_LOCK. ){.
d66d0 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 pFile->transCntr
d66e0 43 68 6e 67 20 3d 20 30 3b 0a 20 20 20 20 70 46 Chng = 0;. pF
d66f0 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 ile->dbUpdate =
d6700 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 0;. pFile->in
d6710 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 31 3b NormalWrite = 1;
d6720 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 . }.#endif...
d6730 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
d6740 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K ){. pFile->
d6750 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
d6760 79 70 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e ype;. pLock->
d6770 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
d6780 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ype;. }else if(
d6790 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
d67a0 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 SIVE_LOCK ){.
d67b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d67c0 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b = PENDING_LOCK;
d67d0 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b . pLock->lock
d67e0 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c type = PENDING_L
d67f0 4f 43 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6c 6f OCK;. }..end_lo
d6800 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d ck:. unixLeaveM
d6810 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 utex();. OSTRAC
d6820 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 E4("LOCK %d %
d6830 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e s %s\n", pFile->
d6840 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 h, locktypeName(
d6850 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 locktype), .
d6860 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 rc==SQLITE_OK
d6870 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 ? "ok" : "failed
d6880 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ");. return rc;
d6890 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 .}../*.** Lower
d68a0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
d68b0 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 l on file descri
d68c0 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f ptor pFile to lo
d68d0 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 cktype. locktyp
d68e0 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 e.** must be eit
d68f0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 her NO_LOCK or S
d6900 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
d6910 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 * If the locking
d6920 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 level of the fi
d6930 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
d6940 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 already at or b
d6950 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 elow.** the requ
d6960 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 ested locking le
d6970 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e vel, this routin
d6980 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f e is a no-op..*/
d6990 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
d69a0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
d69b0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
d69c0 6b 74 79 70 65 29 7b 0a 20 20 73 74 72 75 63 74 ktype){. struct
d69d0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 unixLockInfo *p
d69e0 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 Lock;. struct f
d69f0 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 lock lock;. int
d6a00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d6a10 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d6a20 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d6a30 69 64 3b 0a 20 20 69 6e 74 20 68 3b 0a 0a 20 20 id;. int h;..
d6a40 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
d6a50 0a 20 20 4f 53 54 52 41 43 45 37 28 22 55 4e 4c . OSTRACE7("UNL
d6a60 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 OCK %d %d was %
d6a70 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25 64 5c d(%d,%d) pid=%d\
d6a80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
d6a90 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 70 46 cktype,. pF
d6aa0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 ile->locktype, p
d6ab0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 File->pLock->loc
d6ac0 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c ktype, pFile->pL
d6ad0 6f 63 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 69 64 ock->cnt, getpid
d6ae0 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ());.. assert(
d6af0 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 locktype<=SHARED
d6b00 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 _LOCK );. if( p
d6b10 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d File->locktype<=
d6b20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 locktype ){.
d6b30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d6b40 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43 ;. }. if( CHEC
d6b50 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 6c 65 K_THREADID(pFile
d6b60 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
d6b70 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
d6b80 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 }. unixEnterMu
d6b90 74 65 78 28 29 3b 0a 20 20 68 20 3d 20 70 46 69 tex();. h = pFi
d6ba0 6c 65 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d le->h;. pLock =
d6bb0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 pFile->pLock;.
d6bc0 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
d6bd0 63 6e 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 cnt!=0 );. if(
d6be0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
d6bf0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
d6c00 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
d6c10 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c ->locktype==pFil
d6c20 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
d6c30 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 SimulateIOErr
d6c40 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 orBenign(1);.
d6c50 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d6c60 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53 ( h=(-1) ). S
d6c70 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 imulateIOErrorBe
d6c80 6e 69 67 6e 28 30 29 3b 0a 0a 23 69 66 6e 64 65 nign(0);..#ifnde
d6c90 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 f NDEBUG. /*
d6ca0 57 68 65 6e 20 72 65 64 75 63 69 6e 67 20 61 20 When reducing a
d6cb0 6c 6f 63 6b 20 73 75 63 68 20 74 68 61 74 20 6f lock such that o
d6cc0 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 63 ther processes c
d6cd0 61 6e 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 an start. **
d6ce0 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 reading the data
d6cf0 62 61 73 65 20 66 69 6c 65 20 61 67 61 69 6e 2c base file again,
d6d00 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
d6d10 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 the. ** trans
d6d20 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 action counter w
d6d30 61 73 20 75 70 64 61 74 65 64 20 69 66 20 61 6e as updated if an
d6d40 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 y part of the da
d6d50 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 69 tabase. ** fi
d6d60 6c 65 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20 le changed. If
d6d70 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
d6d80 63 6f 75 6e 74 65 72 20 69 73 20 6e 6f 74 20 75 counter is not u
d6d90 70 64 61 74 65 64 2c 0a 20 20 20 20 2a 2a 20 6f pdated,. ** o
d6da0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 ther connections
d6db0 20 74 6f 20 74 68 65 20 73 61 6d 65 20 66 69 6c to the same fil
d6dc0 65 20 6d 69 67 68 74 20 6e 6f 74 20 72 65 61 6c e might not real
d6dd0 69 7a 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 ize that. **
d6de0 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 the file has cha
d6df0 6e 67 65 64 20 61 6e 64 20 68 65 6e 63 65 20 6d nged and hence m
d6e00 69 67 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 74 6f ight not know to
d6e10 20 66 6c 75 73 68 20 74 68 65 69 72 0a 20 20 20 flush their.
d6e20 20 2a 2a 20 63 61 63 68 65 2e 20 20 54 68 65 20 ** cache. The
d6e30 75 73 65 20 6f 66 20 61 20 73 74 61 6c 65 20 63 use of a stale c
d6e40 61 63 68 65 20 63 61 6e 20 6c 65 61 64 20 74 6f ache can lead to
d6e50 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 database corrup
d6e60 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tion.. */.
d6e70 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
d6e80 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3d 3d 30 inNormalWrite==0
d6e90 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 . || pFi
d6ea0 6c 65 2d 3e 64 62 55 70 64 61 74 65 3d 3d 30 0a le->dbUpdate==0.
d6eb0 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c || pFil
d6ec0 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 e->transCntrChng
d6ed0 3d 3d 31 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 ==1 );. pFile
d6ee0 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 ->inNormalWrite
d6ef0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 = 0;.#endif...
d6f00 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
d6f10 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
d6f20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 lock.l_type
d6f30 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 20 20 = F_RDLCK;.
d6f40 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 lock.l_whence
d6f50 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 = SEEK_SET;.
d6f60 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d lock.l_start =
d6f70 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 SHARED_FIRST;.
d6f80 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 lock.l_len
d6f90 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 = SHARED_SIZE;.
d6fa0 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 if( fcntl(h
d6fb0 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_SETLK, &lock
d6fc0 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 )==(-1) ){.
d6fd0 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 int tErrno =
d6fe0 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 errno;. r
d6ff0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
d7000 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
d7010 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
d7020 52 52 5f 52 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 RR_RDLOCK);.
d7030 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
d7040 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 ERROR(rc) ){.
d7050 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
d7060 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d7070 3b 0a 20 20 20 20 20 20 20 20 7d 0a 09 09 09 09 ;. }.....
d7080 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b goto end_unlock;
d7090 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d70a0 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d lock.l_type =
d70b0 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f F_UNLCK;. lo
d70c0 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 ck.l_whence = SE
d70d0 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b EK_SET;. lock
d70e0 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 .l_start = PENDI
d70f0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 NG_BYTE;. loc
d7100 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61 k.l_len = 2L; a
d7110 73 73 65 72 74 28 20 50 45 4e 44 49 4e 47 5f 42 ssert( PENDING_B
d7120 59 54 45 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f YTE+1==RESERVED_
d7130 42 59 54 45 20 29 3b 0a 20 20 20 20 69 66 28 20 BYTE );. if(
d7140 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b fcntl(h, F_SETLK
d7150 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 , &lock)!=(-1) )
d7160 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c {. pLock->l
d7170 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 ocktype = SHARED
d7180 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 _LOCK;. }else
d7190 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 {. int tErr
d71a0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d71b0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
d71c0 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d71d0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d71e0 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 IOERR_UNLOCK);.
d71f0 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b if( IS_LOCK
d7200 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
d7210 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d7220 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d7230 0a 20 20 20 20 20 20 7d 0a 09 09 09 67 6f 74 6f . }....goto
d7240 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 end_unlock;.
d7250 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 }. }. if( loc
d7260 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 ktype==NO_LOCK )
d7270 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 {. struct uni
d7280 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b xOpenCnt *pOpen;
d7290 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 .. /* Decreme
d72a0 6e 74 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f nt the shared lo
d72b0 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20 52 65 6c ck counter. Rel
d72c0 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 20 75 73 ease the lock us
d72d0 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f 53 ing an. ** OS
d72e0 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e 20 call only when
d72f0 61 6c 6c 20 74 68 72 65 61 64 73 20 69 6e 20 74 all threads in t
d7300 68 69 73 20 73 61 6d 65 20 70 72 6f 63 65 73 73 his same process
d7310 20 68 61 76 65 20 72 65 6c 65 61 73 65 64 0a 20 have released.
d7320 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a ** the lock..
d7330 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c 6f 63 6b */. pLock
d7340 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28 ->cnt--;. if(
d7350 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 pLock->cnt==0 )
d7360 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 {. lock.l_t
d7370 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 ype = F_UNLCK;.
d7380 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e lock.l_when
d7390 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 ce = SEEK_SET;.
d73a0 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 lock.l_star
d73b0 74 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d t = lock.l_len =
d73c0 20 30 4c 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 0L;. Simul
d73d0 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
d73e0 28 31 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c (1);. Simul
d73f0 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d ateIOError( h=(-
d7400 31 29 20 29 0a 20 20 20 20 20 20 53 69 6d 75 6c 1) ). Simul
d7410 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
d7420 28 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 (0);. if( f
d7430 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c cntl(h, F_SETLK,
d7440 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b &lock)!=(-1) ){
d7450 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e . pLock->
d7460 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f locktype = NO_LO
d7470 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b CK;. }else{
d7480 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74 45 72 . int tEr
d7490 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d74a0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d74b0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d74c0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d74d0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 TE_IOERR_UNLOCK)
d74e0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 ;. if( IS
d74f0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
d7500 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 ){. pFi
d7510 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d7520 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 tErrno;.
d7530 7d 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d }. pLock-
d7540 3e 63 6e 74 20 3d 20 31 3b 0a 09 09 09 09 67 6f >cnt = 1;.....go
d7550 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 to end_unlock;.
d7560 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
d7570 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 /* Decrement t
d7580 68 65 20 63 6f 75 6e 74 20 6f 66 20 6c 6f 63 6b he count of lock
d7590 73 20 61 67 61 69 6e 73 74 20 74 68 69 73 20 73 s against this s
d75a0 61 6d 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 20 ame file. When
d75b0 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e 74 the. ** count
d75c0 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 63 reaches zero, c
d75d0 6c 6f 73 65 20 61 6e 79 20 6f 74 68 65 72 20 66 lose any other f
d75e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 ile descriptors
d75f0 77 68 6f 73 65 20 63 6c 6f 73 65 0a 20 20 20 20 whose close.
d7600 2a 2a 20 77 61 73 20 64 65 66 65 72 72 65 64 20 ** was deferred
d7610 62 65 63 61 75 73 65 20 6f 66 20 6f 75 74 73 74 because of outst
d7620 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e 0a 20 20 anding locks..
d7630 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
d7640 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
d7650 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 46 69 6c pOpen = pFil
d7660 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 e->pOpen;.
d7670 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a pOpen->nLock--;.
d7680 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
d7690 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b pen->nLock>=0 );
d76a0 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e . if( pOpen
d76b0 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 4f ->nLock==0 && pO
d76c0 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3e 30 20 pen->nPending>0
d76d0 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 ){. int i
d76e0 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d ;. for(i=
d76f0 30 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50 65 6e 0; i<pOpen->nPen
d7700 64 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 ding; i++){.
d7710 20 20 20 20 20 20 2f 2a 20 63 6c 6f 73 65 20 70 /* close p
d7720 65 6e 64 69 6e 67 20 66 64 73 2c 20 62 75 74 20 ending fds, but
d7730 69 66 20 63 6c 6f 73 69 6e 67 20 66 61 69 6c 73 if closing fails
d7740 20 64 6f 6e 27 74 20 66 72 65 65 20 74 68 65 20 don't free the
d7750 61 72 72 61 79 0a 20 20 20 20 20 20 20 20 20 20 array.
d7760 2a 2a 20 61 73 73 69 67 6e 20 2d 31 20 74 6f 20 ** assign -1 to
d7770 74 68 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 the successfully
d7780 20 63 6c 6f 73 65 64 20 64 65 73 63 72 69 70 74 closed descript
d7790 6f 72 73 20 61 6e 64 20 72 65 63 6f 72 64 20 74 ors and record t
d77a0 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 he. **
d77b0 65 72 72 6f 72 2e 20 20 54 68 65 20 6e 65 78 74 error. The next
d77c0 20 61 74 74 65 6d 70 74 20 74 6f 20 75 6e 6c 6f attempt to unlo
d77d0 63 6b 20 77 69 6c 6c 20 74 72 79 20 61 67 61 69 ck will try agai
d77e0 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 n. */.
d77f0 69 66 28 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 if( pOpen->aPend
d7800 69 6e 67 5b 69 5d 20 3c 20 30 20 29 20 63 6f 6e ing[i] < 0 ) con
d7810 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 tinue;.
d7820 20 69 66 28 20 63 6c 6f 73 65 28 70 4f 70 65 6e if( close(pOpen
d7830 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 29 20 29 ->aPending[i]) )
d7840 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 46 {. pF
d7850 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d7860 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 errno;.
d7870 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
d7880 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 IOERR_CLOSE;.
d7890 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
d78a0 20 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d pOpen-
d78b0 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 2d >aPending[i] = -
d78c0 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 1;. }.
d78d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
d78e0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
d78f0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
d7900 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 70 sqlite3_free(pOp
d7910 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 29 3b 0a 20 en->aPending);.
d7920 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e pOpen->
d7930 6e 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 nPending = 0;.
d7940 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 pOpen->a
d7950 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 Pending = 0;.
d7960 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
d7970 20 20 20 7d 0a 20 20 7d 0a 09 0a 65 6e 64 5f 75 }. }...end_u
d7980 6e 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 nlock:. unixLea
d7990 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 veMutex();. if(
d79a0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
d79b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d79c0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 = locktype;. r
d79d0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d79e0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
d79f0 20 70 65 72 66 6f 72 6d 73 20 74 68 65 20 70 61 performs the pa
d7a00 72 74 73 20 6f 66 20 74 68 65 20 22 63 6c 6f 73 rts of the "clos
d7a10 65 20 66 69 6c 65 22 20 6f 70 65 72 61 74 69 6f e file" operatio
d7a20 6e 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 n .** common to
d7a30 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 all locking sche
d7a40 6d 65 73 2e 20 49 74 20 63 6c 6f 73 65 73 20 74 mes. It closes t
d7a50 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 6e 64 he directory and
d7a60 20 66 69 6c 65 0a 2a 2a 20 68 61 6e 64 6c 65 73 file.** handles
d7a70 2c 20 69 66 20 74 68 65 79 20 61 72 65 20 76 61 , if they are va
d7a80 6c 69 64 2c 20 61 6e 64 20 73 65 74 73 20 61 6c lid, and sets al
d7a90 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 l fields of the
d7aa0 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 unixFile.** stru
d7ab0 63 74 75 72 65 20 74 6f 20 30 2e 0a 2a 2a 0a 2a cture to 0..**.*
d7ac0 2a 20 49 74 20 69 73 20 2a 6e 6f 74 2a 20 6e 65 * It is *not* ne
d7ad0 63 65 73 73 61 72 79 20 74 6f 20 68 6f 6c 64 20 cessary to hold
d7ae0 74 68 65 20 6d 75 74 65 78 20 77 68 65 6e 20 74 the mutex when t
d7af0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
d7b00 61 6c 6c 65 64 2c 0a 2a 2a 20 65 76 65 6e 20 6f alled,.** even o
d7b10 6e 20 56 78 57 6f 72 6b 73 2e 20 20 41 20 6d 75 n VxWorks. A mu
d7b20 74 65 78 20 77 69 6c 6c 20 62 65 20 61 63 71 75 tex will be acqu
d7b30 69 72 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73 20 ired on VxWorks
d7b40 62 79 20 74 68 65 0a 2a 2a 20 76 78 77 6f 72 6b by the.** vxwork
d7b50 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 29 sReleaseFileId()
d7b60 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61 routine..*/.sta
d7b70 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 55 6e 69 tic int closeUni
d7b80 78 46 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 69 xFile(sqlite3_fi
d7b90 6c 65 20 2a 69 64 29 7b 0a 20 20 75 6e 69 78 46 le *id){. unixF
d7ba0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
d7bb0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 66 ixFile*)id;. if
d7bc0 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 ( pFile ){. i
d7bd0 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e f( pFile->dirfd>
d7be0 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 =0 ){. int
d7bf0 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c err = close(pFil
d7c00 65 2d 3e 64 69 72 66 64 29 3b 0a 20 20 20 20 20 e->dirfd);.
d7c10 20 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20 20 if( err ){.
d7c20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d7c30 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d7c40 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
d7c50 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c ITE_IOERR_DIR_CL
d7c60 4f 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 OSE;. }else
d7c70 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
d7c80 3e 64 69 72 66 64 3d 2d 31 3b 0a 20 20 20 20 20 >dirfd=-1;.
d7c90 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
d7ca0 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0a pFile->h>=0 ){.
d7cb0 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 int err =
d7cc0 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b close(pFile->h);
d7cd0 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 20 29 . if( err )
d7ce0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
d7cf0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
d7d00 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 no;. retu
d7d10 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
d7d20 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 0a 20 CLOSE;. }.
d7d30 20 20 20 7d 0a 23 69 66 20 4f 53 5f 56 58 57 4f }.#if OS_VXWO
d7d40 52 4b 53 0a 20 20 20 20 69 66 28 20 70 46 69 6c RKS. if( pFil
d7d50 65 2d 3e 70 49 64 20 29 7b 0a 20 20 20 20 20 20 e->pId ){.
d7d60 69 66 28 20 70 46 69 6c 65 2d 3e 69 73 44 65 6c if( pFile->isDel
d7d70 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 75 ete ){. u
d7d80 6e 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e 70 49 64 nlink(pFile->pId
d7d90 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 ->zCanonicalName
d7da0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
d7db0 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 vxworksReleaseF
d7dc0 69 6c 65 49 64 28 70 46 69 6c 65 2d 3e 70 49 64 ileId(pFile->pId
d7dd0 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e );. pFile->
d7de0 70 49 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 23 pId = 0;. }.#
d7df0 65 6e 64 69 66 0a 20 20 20 20 4f 53 54 52 41 43 endif. OSTRAC
d7e00 45 32 28 22 43 4c 4f 53 45 20 20 20 25 2d 33 64 E2("CLOSE %-3d
d7e10 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a \n", pFile->h);.
d7e20 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 OpenCounter(
d7e30 2d 31 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 -1);. memset(
d7e40 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 pFile, 0, sizeof
d7e50 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 7d (unixFile));. }
d7e60 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
d7e70 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
d7e80 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 ose a file..*/.s
d7e90 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 6c tatic int unixCl
d7ea0 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
d7eb0 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 *id){. int rc
d7ec0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
d7ed0 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 f( id ){. uni
d7ee0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
d7ef0 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 unixFile *)id;.
d7f00 20 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 69 64 unixUnlock(id
d7f10 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 , NO_LOCK);.
d7f20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
d7f30 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d ;. if( pFile-
d7f40 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 2d >pOpen && pFile-
d7f50 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b >pOpen->nLock ){
d7f60 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
d7f70 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 re are outstandi
d7f80 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 ng locks, do not
d7f90 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 actually close
d7fa0 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 the file just.
d7fb0 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75 ** yet becau
d7fc0 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c se that would cl
d7fd0 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e ear those locks.
d7fe0 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74 Instead, add t
d7ff0 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a he file. **
d8000 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70 descriptor to p
d8010 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 Open->aPending.
d8020 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f It will be auto
d8030 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 matically closed
d8040 20 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74 when. ** t
d8050 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 he last lock is
d8060 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 20 2a cleared.. *
d8070 2f 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 4e 65 /. int *aNe
d8080 77 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 w;. struct
d8090 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 unixOpenCnt *pOp
d80a0 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 en = pFile->pOpe
d80b0 6e 3b 0a 20 20 20 20 20 20 61 4e 65 77 20 3d 20 n;. aNew =
d80c0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
d80d0 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2c pOpen->aPending,
d80e0 20 28 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e (pOpen->nPendin
d80f0 67 2b 31 29 2a 73 69 7a 65 6f 66 28 69 6e 74 29 g+1)*sizeof(int)
d8100 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61 4e );. if( aN
d8110 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ew==0 ){.
d8120 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20 /* If a malloc
d8130 66 61 69 6c 73 2c 20 6a 75 73 74 20 6c 65 61 6b fails, just leak
d8140 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
d8150 70 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 7d 65 ptor */. }e
d8160 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 4f 70 lse{. pOp
d8170 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20 61 en->aPending = a
d8180 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 New;. pOp
d8190 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 70 4f 70 en->aPending[pOp
d81a0 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 5d 20 3d 20 en->nPending] =
d81b0 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 20 20 20 20 pFile->h;.
d81c0 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e pOpen->nPendin
d81d0 67 2b 2b 3b 0a 20 20 20 20 20 20 20 20 70 46 69 g++;. pFi
d81e0 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 20 20 le->h = -1;.
d81f0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 }. }. re
d8200 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 leaseLockInfo(pF
d8210 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 ile->pLock);.
d8220 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 releaseOpenCnt(
d8230 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 pFile->pOpen);.
d8240 20 20 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e 69 rc = closeUni
d8250 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 xFile(id);. u
d8260 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
d8270 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
d8280 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
d8290 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 **** End of the
d82a0 70 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20 6c posix advisory l
d82b0 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ock implementati
d82c0 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on *************
d82d0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
d82e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d82f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8320 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/../*********
d8330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8370 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
d8380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8390 2a 2a 2a 2a 20 4e 6f 2d 6f 70 20 4c 6f 63 6b 69 **** No-op Locki
d83a0 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ng *************
d83b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d83c0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 66 20 74 *****.**.** Of t
d83d0 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 69 he various locki
d83e0 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ng implementatio
d83f0 6e 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 ns available, th
d8400 69 73 20 69 73 20 62 79 20 66 61 72 20 74 68 65 is is by far the
d8410 0a 2a 2a 20 73 69 6d 70 6c 65 73 74 3a 20 20 6c .** simplest: l
d8420 6f 63 6b 69 6e 67 20 69 73 20 69 67 6e 6f 72 65 ocking is ignore
d8430 64 2e 20 20 4e 6f 20 61 74 74 65 6d 70 74 20 69 d. No attempt i
d8440 73 20 6d 61 64 65 20 74 6f 20 6c 6f 63 6b 20 74 s made to lock t
d8450 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 he database.** f
d8460 69 6c 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20 ile for reading
d8470 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a or writing..**.*
d8480 2a 20 54 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d * This locking m
d8490 6f 64 65 20 69 73 20 61 70 70 72 6f 70 72 69 61 ode is appropria
d84a0 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 72 65 te for use on re
d84b0 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 ad-only database
d84c0 73 0a 2a 2a 20 28 65 78 3a 20 64 61 74 61 62 61 s.** (ex: databa
d84d0 73 65 73 20 74 68 61 74 20 61 72 65 20 62 75 72 ses that are bur
d84e0 6e 65 64 20 69 6e 74 6f 20 43 44 2d 52 4f 4d 2c ned into CD-ROM,
d84f0 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 29 20 20 for example.)
d8500 49 74 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 It can.** also b
d8510 65 20 75 73 65 64 20 69 66 20 74 68 65 20 61 70 e used if the ap
d8520 70 6c 69 63 61 74 69 6f 6e 20 65 6d 70 6c 6f 79 plication employ
d8530 73 20 73 6f 6d 65 20 65 78 74 65 72 6e 61 6c 20 s some external
d8540 6d 65 63 68 61 6e 69 73 6d 20 74 6f 0a 2a 2a 20 mechanism to.**
d8550 70 72 65 76 65 6e 74 20 73 69 6d 75 6c 74 61 6e prevent simultan
d8560 65 6f 75 73 20 61 63 63 65 73 73 20 6f 66 20 74 eous access of t
d8570 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
d8580 20 62 79 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a by two or more.
d8590 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ** database conn
d85a0 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 74 68 ections. But th
d85b0 65 72 65 20 69 73 20 61 20 73 65 72 69 6f 75 73 ere is a serious
d85c0 20 72 69 73 6b 20 6f 66 20 64 61 74 61 62 61 73 risk of databas
d85d0 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 e.** corruption
d85e0 69 66 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 if this locking
d85f0 6d 6f 64 65 20 69 73 20 75 73 65 64 20 69 6e 20 mode is used in
d8600 73 69 74 75 61 74 69 6f 6e 73 20 77 68 65 72 65 situations where
d8610 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 64 61 74 multiple.** dat
d8620 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
d8630 73 20 61 72 65 20 61 63 63 65 73 73 69 6e 67 20 s are accessing
d8640 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
d8650 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 61 e file at the sa
d8660 6d 65 0a 2a 2a 20 74 69 6d 65 20 61 6e 64 20 6f me.** time and o
d8670 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 ne or more of th
d8680 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ose connections
d8690 61 72 65 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a are writing..*/.
d86a0 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f .static int nolo
d86b0 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c ckCheckReservedL
d86c0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
d86d0 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 2a *NotUsed, int *
d86e0 70 52 65 73 4f 75 74 29 7b 0a 20 20 55 4e 55 53 pResOut){. UNUS
d86f0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
d8700 55 73 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f 75 Used);. *pResOu
d8710 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 t = 0;. return
d8720 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 SQLITE_OK;.}.sta
d8730 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f tic int nolockLo
d8740 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d8750 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f *NotUsed, int No
d8760 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 tUsed2){. UNUSE
d8770 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 D_PARAMETER2(Not
d8780 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b Used, NotUsed2);
d8790 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
d87a0 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e _OK;.}.static in
d87b0 74 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 t nolockUnlock(s
d87c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 qlite3_file *Not
d87d0 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 Used, int NotUse
d87e0 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 d2){. UNUSED_PA
d87f0 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 RAMETER2(NotUsed
d8800 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 , NotUsed2);. r
d8810 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d8820 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
d8830 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
d8840 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 6c tic int nolockCl
d8850 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
d8860 20 2a 69 64 29 20 7b 0a 20 20 72 65 74 75 72 6e *id) {. return
d8870 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 closeUnixFile(i
d8880 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a d);.}../********
d8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
d88a0 6f 66 20 74 68 65 20 6e 6f 2d 6f 70 20 6c 6f 63 of the no-op loc
d88b0 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
d88c0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
d88d0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
d88e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d88f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8920 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a *****/../*******
d8930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8970 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
d8980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8990 2a 20 42 65 67 69 6e 20 64 6f 74 2d 66 69 6c 65 * Begin dot-file
d89a0 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a Locking *******
d89b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d89c0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
d89d0 65 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e e dotfile lockin
d89e0 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e g implementation
d89f0 20 75 73 65 73 20 74 68 65 20 65 78 69 73 74 69 uses the existi
d8a00 6e 67 20 6f 66 20 73 65 70 61 72 61 74 65 20 6c ng of separate l
d8a10 6f 63 6b 0a 2a 2a 20 66 69 6c 65 73 20 69 6e 20 ock.** files in
d8a20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 72 6f 6c order to control
d8a30 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 64 access to the d
d8a40 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 77 atabase. This w
d8a50 6f 72 6b 73 20 6f 6e 20 6a 75 73 74 0a 2a 2a 20 orks on just.**
d8a60 61 62 6f 75 74 20 65 76 65 72 79 20 66 69 6c 65 about every file
d8a70 73 79 73 74 65 6d 20 69 6d 61 67 69 6e 61 62 6c system imaginabl
d8a80 65 2e 20 20 42 75 74 20 74 68 65 72 65 20 61 72 e. But there ar
d8a90 65 20 73 65 72 69 6f 75 73 20 64 6f 77 6e 73 69 e serious downsi
d8aa0 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 des:.**.** (1
d8ab0 29 20 20 54 68 65 72 65 20 69 73 20 7a 65 72 6f ) There is zero
d8ac0 20 63 6f 6e 63 75 72 72 65 6e 63 79 2e 20 20 41 concurrency. A
d8ad0 20 73 69 6e 67 6c 65 20 72 65 61 64 65 72 20 62 single reader b
d8ae0 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74 68 65 72 0a locks all other.
d8af0 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e 6e 65 ** conne
d8b00 63 74 69 6f 6e 73 20 66 72 6f 6d 20 72 65 61 64 ctions from read
d8b10 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74 ing or writing t
d8b20 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a he database..**.
d8b30 2a 2a 20 20 20 20 28 32 29 20 20 41 6e 20 61 70 ** (2) An ap
d8b40 70 6c 69 63 61 74 69 6f 6e 20 63 72 61 73 68 20 plication crash
d8b50 6f 72 20 70 6f 77 65 72 20 6c 6f 73 73 20 63 61 or power loss ca
d8b60 6e 20 6c 65 61 76 65 20 73 74 61 6c 65 20 6c 6f n leave stale lo
d8b70 63 6b 20 66 69 6c 65 73 0a 2a 2a 20 20 20 20 20 ck files.**
d8b80 20 20 20 20 73 69 74 74 69 6e 67 20 61 72 6f 75 sitting arou
d8b90 6e 64 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 nd that need to
d8ba0 62 65 20 63 6c 65 61 72 65 64 20 6d 61 6e 75 61 be cleared manua
d8bb0 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76 65 72 lly..**.** Never
d8bc0 74 68 65 6c 65 73 73 2c 20 61 20 64 6f 74 6c 6f theless, a dotlo
d8bd0 63 6b 20 69 73 20 61 6e 20 61 70 70 72 6f 70 72 ck is an appropr
d8be0 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 iate locking mod
d8bf0 65 20 66 6f 72 20 75 73 65 20 69 66 20 6e 6f 0a e for use if no.
d8c00 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 ** other locking
d8c10 20 73 74 72 61 74 65 67 79 20 69 73 20 61 76 61 strategy is ava
d8c20 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f ilable..**.** Do
d8c30 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 77 6f tfile locking wo
d8c40 72 6b 73 20 62 79 20 63 72 65 61 74 69 6e 67 20 rks by creating
d8c50 61 20 66 69 6c 65 20 69 6e 20 74 68 65 20 73 61 a file in the sa
d8c60 6d 65 20 64 69 72 65 63 74 6f 72 79 20 61 73 20 me directory as
d8c70 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
d8c80 61 6e 64 20 77 69 74 68 20 74 68 65 20 73 61 6d and with the sam
d8c90 65 20 6e 61 6d 65 20 62 75 74 20 77 69 74 68 20 e name but with
d8ca0 61 20 22 2e 6c 6f 63 6b 22 20 65 78 74 65 6e 73 a ".lock" extens
d8cb0 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2a 20 54 68 ion added..** Th
d8cc0 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 61 e existance of a
d8cd0 20 6c 6f 63 6b 20 66 69 6c 65 20 69 6d 70 6c 69 lock file impli
d8ce0 65 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 es an EXCLUSIVE
d8cf0 6c 6f 63 6b 2e 20 20 41 6c 6c 20 6f 74 68 65 72 lock. All other
d8d00 20 6c 6f 63 6b 0a 2a 2a 20 74 79 70 65 73 20 28 lock.** types (
d8d10 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 44 SHARED, RESERVED
d8d20 2c 20 50 45 4e 44 49 4e 47 29 20 61 72 65 20 6d , PENDING) are m
d8d30 61 70 70 65 64 20 69 6e 74 6f 20 45 58 43 4c 55 apped into EXCLU
d8d40 53 49 56 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 SIVE..*/../*.**
d8d50 54 68 65 20 66 69 6c 65 20 73 75 66 66 69 78 20 The file suffix
d8d60 61 64 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 added to the dat
d8d70 61 20 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 a base filename
d8d80 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 in order to crea
d8d90 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 66 te the.** lock f
d8da0 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ile..*/.#define
d8db0 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 20 22 DOTLOCK_SUFFIX "
d8dc0 2e 6c 6f 63 6b 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 .lock"../*.** Th
d8dd0 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
d8de0 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 s if there is a
d8df0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 RESERVED lock he
d8e00 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 ld on the specif
d8e10 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 ied.** file by t
d8e20 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 his or any other
d8e30 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 process. If suc
d8e40 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 h a lock is held
d8e50 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a , set *pResOut.*
d8e60 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 * to a non-zero
d8e70 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 value otherwise
d8e80 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 *pResOut is set
d8e90 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 to zero. The re
d8ea0 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 turn value.** is
d8eb0 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f set to SQLITE_O
d8ec0 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 K unless an I/O
d8ed0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
d8ee0 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e ing lock checkin
d8ef0 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 64 6f 74 66 g..**.** In dotf
d8f00 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 65 69 74 ile locking, eit
d8f10 68 65 72 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 her a lock exist
d8f20 73 20 6f 72 20 69 74 20 64 6f 65 73 20 6e 6f 74 s or it does not
d8f30 2e 20 20 53 6f 20 69 6e 20 74 68 69 73 0a 2a 2a . So in this.**
d8f40 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20 43 68 variation of Ch
d8f50 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
d8f60 29 2c 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 ), *pResOut is s
d8f70 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 6e et to true if an
d8f80 79 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 68 65 6c y lock.** is hel
d8f90 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 61 6e d on the file an
d8fa0 64 20 66 61 6c 73 65 20 69 66 20 74 68 65 20 66 d false if the f
d8fb0 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e ile is unlocked.
d8fc0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
d8fd0 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 otlockCheckReser
d8fe0 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
d8ff0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
d9000 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20 ResOut) {. int
d9010 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d9020 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d int reserved =
d9030 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 0;. unixFile *
d9040 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d9050 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 e*)id;.. Simula
d9060 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
d9070 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 n SQLITE_IOERR_C
d9080 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b HECKRESERVEDLOCK
d9090 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 ; );. . assert
d90a0 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a ( pFile );.. /*
d90b0 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 Check if a thre
d90c0 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 ad in this proce
d90d0 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 ss holds such a
d90e0 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 lock */. if( pF
d90f0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 ile->locktype>SH
d9100 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
d9110 20 2f 2a 20 45 69 74 68 65 72 20 74 68 69 73 20 /* Either this
d9120 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 73 6f connection or so
d9130 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 me other connect
d9140 69 6f 6e 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ion in the same
d9150 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 68 process. ** h
d9160 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 olds a lock on t
d9170 68 65 20 66 69 6c 65 2e 20 20 4e 6f 20 6e 65 65 he file. No nee
d9180 64 20 74 6f 20 63 68 65 63 6b 20 66 75 72 74 68 d to check furth
d9190 65 72 2e 20 2a 2f 0a 20 20 20 20 72 65 73 65 72 er. */. reser
d91a0 76 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 ved = 1;. }else
d91b0 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 {. /* The loc
d91c0 6b 20 69 73 20 68 65 6c 64 20 69 66 20 61 6e 64 k is held if and
d91d0 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 6c 6f 63 only if the loc
d91e0 6b 66 69 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a kfile exists */.
d91f0 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
d9200 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 6f 6e zLockFile = (con
d9210 73 74 20 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e st char*)pFile->
d9220 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
d9230 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 61 reserved = a
d9240 63 63 65 73 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c ccess(zLockFile,
d9250 20 30 29 3d 3d 30 3b 0a 20 20 7d 0a 20 20 4f 53 0)==0;. }. OS
d9260 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d TRACE4("TEST WR-
d9270 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 LOCK %d %d %d\n"
d9280 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 , pFile->h, rc,
d9290 72 65 73 65 72 76 65 64 29 3b 0a 20 20 2a 70 52 reserved);. *pR
d92a0 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 esOut = reserved
d92b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
d92c0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 ../*.** Lock the
d92d0 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c file with the l
d92e0 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 ock specified by
d92f0 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 parameter lockt
d9300 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 ype - one.** of
d9310 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a the following:.*
d9320 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 *.** (1) SHA
d9330 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 RED_LOCK.**
d9340 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 (2) RESERVED_LOC
d9350 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e K.** (3) PEN
d9360 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 DING_LOCK.**
d9370 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c (4) EXCLUSIVE_L
d9380 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 OCK.**.** Someti
d9390 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 mes when request
d93a0 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 ing one lock sta
d93b0 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c te, additional l
d93c0 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 ock states.** ar
d93d0 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 e inserted in be
d93e0 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b tween. The lock
d93f0 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f ing might fail o
d9400 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 n one of the lat
d9410 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e er.** transition
d9420 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f s leaving the lo
d9430 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 ck state differe
d9440 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 nt from what it
d9450 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 started but.** s
d9460 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 till short of it
d9470 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c s goal. The fol
d9480 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f lowing chart sho
d9490 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a ws the allowed.*
d94a0 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e * transitions an
d94b0 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 d the inserted i
d94c0 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 ntermediate stat
d94d0 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c es:.**.** UNL
d94e0 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a OCKED -> SHARED.
d94f0 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
d9500 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 RESERVED.** S
d9510 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e HARED -> (PENDIN
d9520 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
d9530 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d ** RESERVED -
d9540 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
d9550 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 XCLUSIVE.** P
d9560 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 ENDING -> EXCLUS
d9570 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 IVE.**.** This r
d9580 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 outine will only
d9590 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b increase a lock
d95a0 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 . Use the sqlit
d95b0 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 e3OsUnlock().**
d95c0 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 routine to lower
d95d0 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c a locking level
d95e0 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 64 6f 74 ..**.** With dot
d95f0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 77 65 file locking, we
d9600 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 20 73 75 70 really only sup
d9610 70 6f 72 74 20 73 74 61 74 65 20 28 34 29 3a 20 port state (4):
d9620 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2a 20 42 75 EXCLUSIVE..** Bu
d9630 74 20 77 65 20 74 72 61 63 6b 20 74 68 65 20 6f t we track the o
d9640 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 ther locking lev
d9650 65 6c 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a els internally..
d9660 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f */.static int do
d9670 74 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 tlockLock(sqlite
d9680 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
d9690 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e locktype) {. un
d96a0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
d96b0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
d96c0 20 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20 int fd;. char
d96d0 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 *zLockFile = (ch
d96e0 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b ar *)pFile->lock
d96f0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e ingContext;. in
d9700 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
d9710 3b 0a 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 ;... /* If we h
d9720 61 76 65 20 61 6e 79 20 6c 6f 63 6b 2c 20 74 68 ave any lock, th
d9730 65 6e 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 en the lock file
d9740 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e already exists.
d9750 20 20 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20 All we have.
d9760 2a 2a 20 74 6f 20 64 6f 20 69 73 20 61 64 6a 75 ** to do is adju
d9770 73 74 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 st our internal
d9780 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 6c 6f record of the lo
d9790 63 6b 20 6c 65 76 65 6c 2e 0a 20 20 2a 2f 0a 20 ck level.. */.
d97a0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d97b0 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 type > NO_LOCK )
d97c0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 {. pFile->loc
d97d0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
d97e0 3b 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b ;.#if !OS_VXWORK
d97f0 53 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 S. /* Always
d9800 75 70 64 61 74 65 20 74 68 65 20 74 69 6d 65 73 update the times
d9810 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20 tamp on the old
d9820 66 69 6c 65 20 2a 2f 0a 20 20 20 20 75 74 69 6d file */. utim
d9830 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55 es(zLockFile, NU
d9840 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 LL);.#endif.
d9850 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d9860 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 ;. }. . /* gr
d9870 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 ab an exclusive
d9880 6c 6f 63 6b 20 2a 2f 0a 20 20 66 64 20 3d 20 6f lock */. fd = o
d9890 70 65 6e 28 7a 4c 6f 63 6b 46 69 6c 65 2c 4f 5f pen(zLockFile,O_
d98a0 52 44 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f RDONLY|O_CREAT|O
d98b0 5f 45 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69 _EXCL,0600);. i
d98c0 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f f( fd<0 ){. /
d98d0 2a 20 66 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e * failed to open
d98e0 2f 63 72 65 61 74 65 20 74 68 65 20 66 69 6c 65 /create the file
d98f0 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d , someone else m
d9900 61 79 20 68 61 76 65 20 73 74 6f 6c 65 6e 20 74 ay have stolen t
d9910 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 he lock */. i
d9920 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
d9930 6f 3b 0a 20 20 20 20 69 66 28 20 45 45 58 49 53 o;. if( EEXIS
d9940 54 20 3d 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 T == tErrno ){.
d9950 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
d9960 5f 42 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 _BUSY;. } els
d9970 65 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 e {. rc = s
d9980 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d9990 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d99a0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
d99b0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 CK);. if( I
d99c0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d99d0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
d99e0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d99f0 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 Errno;. }.
d9a00 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
d9a10 72 63 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 63 rc;. } . if( c
d9a20 6c 6f 73 65 28 66 64 29 20 29 7b 0a 20 20 20 20 lose(fd) ){.
d9a30 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d9a40 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 = errno;. rc
d9a50 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
d9a60 43 4c 4f 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 CLOSE;. }. .
d9a70 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 /* got it, set t
d9a80 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 he type and retu
d9a90 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 rn ok */. pFile
d9aa0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
d9ab0 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 ktype;. return
d9ac0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 rc;.}../*.** Low
d9ad0 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c er the locking l
d9ae0 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 evel on file des
d9af0 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f criptor pFile to
d9b00 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b locktype. lock
d9b10 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 type.** must be
d9b20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f either NO_LOCK o
d9b30 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a r SHARED_LOCK..*
d9b40 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b *.** If the lock
d9b50 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 ing level of the
d9b60 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d9b70 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f is already at o
d9b80 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 r below.** the r
d9b90 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 equested locking
d9ba0 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 level, this rou
d9bb0 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tine is a no-op.
d9bc0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 .**.** When the
d9bd0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 72 65 locking level re
d9be0 61 63 68 65 73 20 4e 4f 5f 4c 4f 43 4b 2c 20 64 aches NO_LOCK, d
d9bf0 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 elete the lock f
d9c00 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
d9c10 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b nt dotlockUnlock
d9c20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d9c30 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
d9c40 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 {. unixFile *p
d9c50 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d9c60 2a 29 69 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c *)id;. char *zL
d9c70 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 ockFile = (char
d9c80 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
d9c90 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 Context;.. asse
d9ca0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f rt( pFile );. O
d9cb0 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 STRACE5("UNLOCK
d9cc0 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70 69 %d %d was %d pi
d9cd0 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d=%d\n", pFile->
d9ce0 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 h, locktype,..
d9cf0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d9d00 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 61 , getpid());. a
d9d10 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c ssert( locktype<
d9d20 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
d9d30 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 . /* no-op if
d9d40 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 possible */. i
d9d50 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d9d60 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a pe==locktype ){.
d9d70 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d9d80 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
d9d90 54 6f 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 To downgrade to
d9da0 73 68 61 72 65 64 2c 20 73 69 6d 70 6c 79 20 75 shared, simply u
d9db0 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e pdate our intern
d9dc0 61 6c 20 6e 6f 74 69 6f 6e 20 6f 66 20 74 68 65 al notion of the
d9dd0 0a 20 20 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 . ** lock state
d9de0 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 6d 65 . No need to me
d9df0 73 73 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 ss with the file
d9e00 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 on disk.. */.
d9e10 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
d9e20 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
d9e30 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d9e40 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b e = SHARED_LOCK;
d9e50 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d9e60 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
d9e70 2f 2a 20 54 6f 20 66 75 6c 6c 79 20 75 6e 6c 6f /* To fully unlo
d9e80 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c ck the database,
d9e90 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b delete the lock
d9ea0 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 file */. asser
d9eb0 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f t( locktype==NO_
d9ec0 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 75 6e LOCK );. if( un
d9ed0 6c 69 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65 29 20 link(zLockFile)
d9ee0 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 74 ){. int rc, t
d9ef0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d9f00 20 20 20 69 66 28 20 45 4e 4f 45 4e 54 20 21 3d if( ENOENT !=
d9f10 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 tErrno ){.
d9f20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
d9f30 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
d9f40 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
d9f50 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 OERR_UNLOCK);.
d9f60 20 20 7d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c }. if( IS_L
d9f70 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b OCK_ERROR(rc) ){
d9f80 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
d9f90 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d9fa0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
d9fb0 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 rn rc; . }. pF
d9fc0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
d9fd0 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 NO_LOCK;. retur
d9fe0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
d9ff0 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 /*.** Close a fi
da000 6c 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 le. Make sure t
da010 68 65 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e he lock has been
da020 20 72 65 6c 65 61 73 65 64 20 62 65 66 6f 72 65 released before
da030 20 63 6c 6f 73 69 6e 67 2e 0a 2a 2f 0a 73 74 61 closing..*/.sta
da040 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 tic int dotlockC
da050 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
da060 65 20 2a 69 64 29 20 7b 0a 20 20 69 6e 74 20 72 e *id) {. int r
da070 63 3b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 c;. if( id ){.
da080 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 unixFile *pFi
da090 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
da0a0 69 64 3b 0a 20 20 20 20 64 6f 74 6c 6f 63 6b 55 id;. dotlockU
da0b0 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 nlock(id, NO_LOC
da0c0 4b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f K);. sqlite3_
da0d0 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b free(pFile->lock
da0e0 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d ingContext);. }
da0f0 0a 20 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e 69 . rc = closeUni
da100 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 72 65 74 xFile(id);. ret
da110 75 72 6e 20 72 63 3b 0a 7d 0a 2f 2a 2a 2a 2a 2a urn rc;.}./*****
da120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
da130 64 20 6f 66 20 74 68 65 20 64 6f 74 2d 66 69 6c d of the dot-fil
da140 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 e lock implement
da150 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ation **********
da160 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
da170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da1b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a ********/../****
da1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
da210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da220 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 6c 6f 63 ***** Begin floc
da230 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a k Locking ******
da240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
da260 20 55 73 65 20 74 68 65 20 66 6c 6f 63 6b 28 29 Use the flock()
da270 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74 6f 20 system call to
da280 64 6f 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e do file locking.
da290 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6c .**.** flock() l
da2a0 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20 64 ocking is like d
da2b0 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 ot-file locking
da2c0 69 6e 20 74 68 61 74 20 74 68 65 20 76 61 72 69 in that the vari
da2d0 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d 67 72 61 69 ous.** fine-grai
da2e0 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 73 n locking levels
da2f0 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 53 51 supported by SQ
da300 4c 69 74 65 20 61 72 65 20 63 6f 6c 6c 61 70 73 Lite are collaps
da310 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e ed into.** a sin
da320 67 6c 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f gle exclusive lo
da330 63 6b 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f ck. In other wo
da340 72 64 73 2c 20 53 48 41 52 45 44 2c 20 52 45 53 rds, SHARED, RES
da350 45 52 56 45 44 2c 20 61 6e 64 0a 2a 2a 20 50 45 ERVED, and.** PE
da360 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 61 72 65 20 NDING locks are
da370 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 the same thing a
da380 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c s an EXCLUSIVE l
da390 6f 63 6b 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 ock. SQLite.**
da3a0 73 74 69 6c 6c 20 77 6f 72 6b 73 20 77 68 65 6e still works when
da3b0 20 79 6f 75 20 64 6f 20 74 68 69 73 2c 20 62 75 you do this, bu
da3c0 74 20 63 6f 6e 63 75 72 72 65 6e 63 79 20 69 73 t concurrency is
da3d0 20 72 65 64 75 63 65 64 20 73 69 6e 63 65 0a 2a reduced since.*
da3e0 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 * only a single
da3f0 70 72 6f 63 65 73 73 20 63 61 6e 20 62 65 20 72 process can be r
da400 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 eading the datab
da410 61 73 65 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a ase at a time..*
da420 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68 69 73 20 73 *.** Omit this s
da430 65 63 74 69 6f 6e 20 69 66 20 53 51 4c 49 54 45 ection if SQLITE
da440 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
da450 53 54 59 4c 45 20 69 73 20 74 75 72 6e 65 64 20 STYLE is turned
da460 6f 66 66 20 6f 72 20 69 66 0a 2a 2a 20 63 6f 6d off or if.** com
da470 70 69 6c 69 6e 67 20 66 6f 72 20 56 58 57 4f 52 piling for VXWOR
da480 4b 53 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 KS..*/.#if SQLIT
da490 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
da4a0 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 _STYLE && !OS_VX
da4b0 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 WORKS../*.** Thi
da4c0 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
da4d0 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 if there is a R
da4e0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c ESERVED lock hel
da4f0 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 d on the specifi
da500 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 ed.** file by th
da510 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 is or any other
da520 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 process. If such
da530 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c a lock is held,
da540 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a set *pResOut.**
da550 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 to a non-zero v
da560 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a alue otherwise *
da570 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 pResOut is set t
da580 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 o zero. The ret
da590 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 urn value.** is
da5a0 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b set to SQLITE_OK
da5b0 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 unless an I/O e
da5c0 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
da5d0 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 ng lock checking
da5e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
da5f0 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 flockCheckReserv
da600 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
da610 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 ile *id, int *pR
da620 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 esOut){. int rc
da630 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
da640 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 int reserved = 0
da650 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
da660 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
da670 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 )id;. . Simula
da680 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
da690 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 n SQLITE_IOERR_C
da6a0 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b HECKRESERVEDLOCK
da6b0 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 ; );. . assert
da6c0 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 0a 20 20 ( pFile );. .
da6d0 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 /* Check if a th
da6e0 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f read in this pro
da6f0 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 cess holds such
da700 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 a lock */. if(
da710 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
da720 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
da730 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b reserved = 1;
da740 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 . }. . /* Oth
da750 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f erwise see if so
da760 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 me other process
da770 20 68 6f 6c 64 73 20 69 74 2e 20 2a 2f 0a 20 20 holds it. */.
da780 69 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b if( !reserved ){
da790 0a 20 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 . /* attempt
da7a0 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 to get the lock
da7b0 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 63 20 3d */. int lrc =
da7c0 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c flock(pFile->h,
da7d0 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f LOCK_EX | LOCK_
da7e0 4e 42 29 3b 0a 20 20 20 20 69 66 28 20 21 6c 72 NB);. if( !lr
da7f0 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 67 6f c ){. /* go
da800 74 20 74 68 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f t the lock, unlo
da810 63 6b 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 6c ck it */. l
da820 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 rc = flock(pFile
da830 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 ->h, LOCK_UN);.
da840 20 20 20 20 20 69 66 20 28 20 6c 72 63 20 29 20 if ( lrc )
da850 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74 45 {. int tE
da860 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
da870 20 20 20 20 20 20 2f 2a 20 75 6e 6c 6f 63 6b 20 /* unlock
da880 66 61 69 6c 65 64 20 77 69 74 68 20 61 6e 20 65 failed with an e
da890 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 rror */.
da8a0 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f lrc = sqliteErro
da8b0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
da8c0 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
da8d0 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 OERR_UNLOCK); .
da8e0 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f if( IS_LO
da8f0 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b CK_ERROR(lrc) ){
da900 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 . pFile
da910 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
da920 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 rrno;.
da930 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 rc = lrc;.
da940 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
da950 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 69 } else {. i
da960 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
da970 6f 3b 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 o;. reserve
da980 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a 20 d = 1;. /*
da990 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 69 67 someone else mig
da9a0 68 74 20 68 61 76 65 20 69 74 20 72 65 73 65 72 ht have it reser
da9b0 76 65 64 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 ved */. lrc
da9c0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 = sqliteErrorFr
da9d0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 omPosixError(tEr
da9e0 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 rno, SQLITE_IOER
da9f0 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 R_LOCK); .
daa00 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
daa10 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20 R(lrc) ){.
daa20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
daa30 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
daa40 20 20 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 rc = lrc;.
daa50 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
daa60 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 . OSTRACE4("TES
daa70 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
daa80 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
daa90 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a rc, reserved);.
daaa0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 .#ifdef SQLITE_I
daab0 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b GNORE_FLOCK_LOCK
daac0 5f 45 52 52 4f 52 53 0a 20 20 69 66 28 20 28 72 _ERRORS. if( (r
daad0 63 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c & SQLITE_IOERR
daae0 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 ) == SQLITE_IOER
daaf0 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 R ){. rc = SQ
dab00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 65 73 LITE_OK;. res
dab10 65 72 76 65 64 3d 31 3b 0a 20 20 7d 0a 23 65 6e erved=1;. }.#en
dab20 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 dif /* SQLITE_IG
dab30 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f NORE_FLOCK_LOCK_
dab40 45 52 52 4f 52 53 20 2a 2f 0a 20 20 2a 70 52 65 ERRORS */. *pRe
dab50 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b sOut = reserved;
dab60 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
dab70 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 ./*.** Lock the
dab80 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f file with the lo
dab90 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ck specified by
daba0 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 parameter lockty
dabb0 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 pe - one.** of t
dabc0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a he following:.**
dabd0 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 .** (1) SHAR
dabe0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
dabf0 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 2) RESERVED_LOCK
dac00 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 .** (3) PEND
dac10 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 ING_LOCK.**
dac20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f (4) EXCLUSIVE_LO
dac30 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d CK.**.** Sometim
dac40 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 es when requesti
dac50 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 ng one lock stat
dac60 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f e, additional lo
dac70 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 ck states.** are
dac80 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 inserted in bet
dac90 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 ween. The locki
daca0 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e ng might fail on
dacb0 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 one of the late
dacc0 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 r.** transitions
dacd0 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 leaving the loc
dace0 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e k state differen
dacf0 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 t from what it s
dad00 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 tarted but.** st
dad10 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 ill short of its
dad20 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c goal. The foll
dad30 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 owing chart show
dad40 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a s the allowed.**
dad50 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 transitions and
dad60 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e the inserted in
dad70 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 termediate state
dad80 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f s:.**.** UNLO
dad90 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a CKED -> SHARED.*
dada0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 * SHARED -> R
dadb0 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 ESERVED.** SH
dadc0 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 ARED -> (PENDING
dadd0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
dade0 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e * RESERVED ->
dadf0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 (PENDING) -> EX
dae00 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 CLUSIVE.** PE
dae10 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 NDING -> EXCLUSI
dae20 56 45 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 VE.**.** flock()
dae30 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 only really sup
dae40 70 6f 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c port EXCLUSIVE l
dae50 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 ocks. We track
dae60 69 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 intermediate.**
dae70 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 lock states in t
dae80 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 he sqlite3_file
dae90 73 74 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 structure, but a
daea0 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 ll locks SHARED
daeb0 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 or.** above are
daec0 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 really EXCLUSIVE
daed0 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 locks and exclu
daee0 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f de all other pro
daef0 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 cesses from.** a
daf00 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a ccess the file..
daf10 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
daf20 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 ne will only inc
daf30 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 rease a lock. U
daf40 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 se the sqlite3Os
daf50 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 Unlock().** rout
daf60 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c ine to lower a l
daf70 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f ocking level..*/
daf80 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 .static int floc
daf90 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 kLock(sqlite3_fi
dafa0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
dafb0 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 20 72 63 type) {. int rc
dafc0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
dafd0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
dafe0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
daff0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c .. assert( pFil
db000 65 20 29 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65 e );.. /* if we
db010 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 already have a
db020 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 63 6c lock, it is excl
db030 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 usive. . ** Ju
db040 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65 6c 20 st adjust level
db050 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 and punt on outt
db060 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20 a here. */. if
db070 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 (pFile->locktype
db080 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 > NO_LOCK) {.
db090 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
db0a0 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
db0b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
db0c0 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 OK;. }. . /*
db0d0 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 grab an exclusiv
db0e0 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a 20 20 69 e lock */. . i
db0f0 66 20 28 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e f (flock(pFile->
db100 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 h, LOCK_EX | LOC
db110 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20 69 6e 74 K_NB)) {. int
db120 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
db130 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27 74 20 67 . /* didn't g
db140 65 74 2c 20 6d 75 73 74 20 62 65 20 62 75 73 79 et, must be busy
db150 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
db160 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
db170 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
db180 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b QLITE_IOERR_LOCK
db190 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f );. if( IS_LO
db1a0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
db1b0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
db1c0 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
db1d0 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 . }. } else
db1e0 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 20 69 74 2c {. /* got it,
db1f0 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e set the type an
db200 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 d return ok */.
db210 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
db220 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 pe = locktype;.
db230 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c }. OSTRACE4("L
db240 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c OCK %d %s %s\
db250 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
db260 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 cktypeName(lockt
db270 79 70 65 29 2c 20 0a 20 20 20 20 20 20 20 20 20 ype), .
db280 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 rc==SQLITE_OK
db290 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 ? "ok" : "failed
db2a0 22 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ");.#ifdef SQLIT
db2b0 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c E_IGNORE_FLOCK_L
db2c0 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28 OCK_ERRORS. if(
db2d0 20 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f (rc & SQLITE_IO
db2e0 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 ERR) == SQLITE_I
db2f0 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d OERR ){. rc =
db300 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
db310 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
db320 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f TE_IGNORE_FLOCK_
db330 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 LOCK_ERRORS */.
db340 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a return rc;.}...
db350 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 /*.** Lower the
db360 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e locking level on
db370 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
db380 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 pFile to lockty
db390 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a pe. locktype.**
db3a0 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
db3b0 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 NO_LOCK or SHARE
db3c0 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 D_LOCK..**.** If
db3d0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
db3e0 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 el of the file d
db3f0 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 escriptor is alr
db400 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 eady at or below
db410 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 .** the requeste
db420 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c d locking level,
db430 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
db440 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 a no-op..*/.sta
db450 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 6c tic int flockUnl
db460 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
db470 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
db480 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 pe) {. unixFile
db490 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
db4a0 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 61 73 ile*)id;. . as
db4b0 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 sert( pFile );.
db4c0 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 OSTRACE5("UNLOC
db4d0 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20 K %d %d was %d
db4e0 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 pid=%d\n", pFile
db4f0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 ->h, locktype,.
db500 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d pFile-
db510 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 >locktype, getpi
db520 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 d());. assert(
db530 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 locktype<=SHARED
db540 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a _LOCK );. . /*
db550 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 no-op if possib
db560 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c le */. if( pFil
db570 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 e->locktype==loc
db580 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 ktype ){. ret
db590 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
db5a0 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 }. . /* share
db5b0 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65 d can just be se
db5c0 74 20 62 65 63 61 75 73 65 20 77 65 20 61 6c 77 t because we alw
db5d0 61 79 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c ays have an excl
db5e0 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c usive */. if (l
db5f0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
db600 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c LOCK) {. pFil
db610 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f e->locktype = lo
db620 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 cktype;. retu
db630 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
db640 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 }. . /* no, re
db650 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f ally, unlock. */
db660 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 6c 6f 63 . int rc = floc
db670 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b k(pFile->h, LOCK
db680 5f 55 4e 29 3b 0a 20 20 69 66 20 28 72 63 29 20 _UN);. if (rc)
db690 7b 0a 20 20 20 20 69 6e 74 20 72 2c 20 74 45 72 {. int r, tEr
db6a0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
db6b0 20 72 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 r = sqliteError
db6c0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
db6d0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
db6e0 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 ERR_UNLOCK);.
db6f0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
db700 4f 52 28 72 29 20 29 7b 0a 20 20 20 20 20 20 70 OR(r) ){. p
db710 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
db720 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a = tErrno;. }.
db730 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 #ifdef SQLITE_IG
db740 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f NORE_FLOCK_LOCK_
db750 45 52 52 4f 52 53 0a 20 20 20 20 69 66 28 20 28 ERRORS. if( (
db760 72 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 r & SQLITE_IOERR
db770 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 ) == SQLITE_IOER
db780 52 20 29 7b 0a 20 20 20 20 20 20 72 20 3d 20 53 R ){. r = S
db790 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
db7a0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
db7b0 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f TE_IGNORE_FLOCK_
db7c0 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 LOCK_ERRORS */.
db7d0 20 20 20 0a 20 20 20 20 72 65 74 75 72 6e 20 72 . return r
db7e0 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 ;. } else {.
db7f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
db800 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 = NO_LOCK;.
db810 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
db820 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
db830 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a lose a file..*/.
db840 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b static int flock
db850 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 Close(sqlite3_fi
db860 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 le *id) {. if(
db870 69 64 20 29 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 id ){. flockU
db880 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 nlock(id, NO_LOC
db890 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e K);. }. return
db8a0 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 closeUnixFile(i
db8b0 64 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a d);.}..#endif /*
db8c0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
db8d0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 OCKING_STYLE &&
db8e0 21 4f 53 5f 56 58 57 4f 52 4b 20 2a 2f 0a 0a 2f !OS_VXWORK */../
db8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db900 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 66 *** End of the f
db910 6c 6f 63 6b 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d lock lock implem
db920 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a entation *******
db930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
db940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
db990 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
db9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
db9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db9f0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 4e ******** Begin N
dba00 61 6d 65 64 20 53 65 6d 61 70 68 6f 72 65 20 4c amed Semaphore L
dba10 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ocking *********
dba20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
dba30 2a 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 **.** Named sema
dba40 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 phore locking is
dba50 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64 20 only supported
dba60 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a on VxWorks..**.*
dba70 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b * Semaphore lock
dba80 69 6e 67 20 69 73 20 6c 69 6b 65 20 64 6f 74 2d ing is like dot-
dba90 6c 6f 63 6b 20 61 6e 64 20 66 6c 6f 63 6b 20 69 lock and flock i
dbaa0 6e 20 74 68 61 74 20 69 74 20 72 65 61 6c 6c 79 n that it really
dbab0 20 6f 6e 6c 79 0a 2a 2a 20 73 75 70 70 6f 72 74 only.** support
dbac0 73 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b s EXCLUSIVE lock
dbad0 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 20 73 69 6e ing. Only a sin
dbae0 67 6c 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 gle process can
dbaf0 72 65 61 64 20 6f 72 20 77 72 69 74 65 0a 2a 2a read or write.**
dbb00 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
dbb10 6c 65 20 61 74 20 61 20 74 69 6d 65 2e 20 20 54 le at a time. T
dbb20 68 69 73 20 72 65 64 75 63 65 73 20 70 6f 74 65 his reduces pote
dbb30 6e 74 69 61 6c 20 63 6f 6e 63 75 72 72 65 6e 63 ntial concurrenc
dbb40 79 2c 20 62 75 74 0a 2a 2a 20 6d 61 6b 65 73 20 y, but.** makes
dbb50 74 68 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 the lock impleme
dbb60 6e 74 61 74 69 6f 6e 20 6d 75 63 68 20 65 61 73 ntation much eas
dbb70 69 65 72 2e 0a 2a 2f 0a 23 69 66 20 4f 53 5f 56 ier..*/.#if OS_V
dbb80 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 XWORKS../*.** Th
dbb90 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
dbba0 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 s if there is a
dbbb0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 RESERVED lock he
dbbc0 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 ld on the specif
dbbd0 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 ied.** file by t
dbbe0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 his or any other
dbbf0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 process. If suc
dbc00 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 h a lock is held
dbc10 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a , set *pResOut.*
dbc20 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 * to a non-zero
dbc30 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 value otherwise
dbc40 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 *pResOut is set
dbc50 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 to zero. The re
dbc60 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 turn value.** is
dbc70 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f set to SQLITE_O
dbc80 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 K unless an I/O
dbc90 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
dbca0 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e ing lock checkin
dbcb0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
dbcc0 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 semCheckReserve
dbcd0 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 dLock(sqlite3_fi
dbce0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 le *id, int *pRe
dbcf0 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 sOut) {. int rc
dbd00 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
dbd10 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 int reserved = 0
dbd20 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
dbd30 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
dbd40 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 )id;.. Simulate
dbd50 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
dbd60 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
dbd70 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 CKRESERVEDLOCK;
dbd80 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 );. . assert(
dbd90 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 pFile );.. /* C
dbda0 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 heck if a thread
dbdb0 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 in this process
dbdc0 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f holds such a lo
dbdd0 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c ck */. if( pFil
dbde0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 e->locktype>SHAR
dbdf0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 ED_LOCK ){. r
dbe00 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d eserved = 1;. }
dbe10 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 . . /* Otherwi
dbe20 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f se see if some o
dbe30 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c ther process hol
dbe40 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 ds it. */. if(
dbe50 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 !reserved ){.
dbe60 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 sem_t *pSem = p
dbe70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 File->pOpen->pSe
dbe80 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 m;. struct st
dbe90 61 74 20 73 74 61 74 42 75 66 3b 0a 0a 20 20 20 at statBuf;..
dbea0 20 69 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74 if( sem_trywait
dbeb0 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 (pSem)==-1 ){.
dbec0 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d int tErrno =
dbed0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 69 66 errno;. if
dbee0 28 20 45 41 47 41 49 4e 20 21 3d 20 74 45 72 72 ( EAGAIN != tErr
dbef0 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 no ){. rc
dbf00 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 = sqliteErrorFr
dbf10 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 omPosixError(tEr
dbf20 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 rno, SQLITE_IOER
dbf30 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c R_CHECKRESERVEDL
dbf40 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 70 46 OCK);. pF
dbf50 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
dbf60 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d tErrno;. }
dbf70 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 else {.
dbf80 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 /* someone else
dbf90 68 61 73 20 74 68 65 20 6c 6f 63 6b 20 77 68 65 has the lock whe
dbfa0 6e 20 77 65 20 61 72 65 20 69 6e 20 4e 4f 5f 4c n we are in NO_L
dbfb0 4f 43 4b 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 OCK */. r
dbfc0 65 73 65 72 76 65 64 20 3d 20 28 70 46 69 6c 65 eserved = (pFile
dbfd0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 53 48 41 ->locktype < SHA
dbfe0 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 RED_LOCK);.
dbff0 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
dc000 20 20 20 20 2f 2a 20 77 65 20 63 6f 75 6c 64 20 /* we could
dc010 68 61 76 65 20 69 74 20 69 66 20 77 65 20 77 61 have it if we wa
dc020 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 73 nt it */. s
dc030 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3b 0a 20 em_post(pSem);.
dc040 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 }. }. OSTRA
dc050 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 CE4("TEST WR-LOC
dc060 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 K %d %d %d\n", p
dc070 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 File->h, rc, res
dc080 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 erved);.. *pRes
dc090 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a Out = reserved;.
dc0a0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
dc0b0 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 /*.** Lock the f
dc0c0 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 ile with the loc
dc0d0 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 k specified by p
dc0e0 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 arameter locktyp
dc0f0 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 e - one.** of th
dc100 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
dc110 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 ** (1) SHARE
dc120 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 D_LOCK.** (2
dc130 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a ) RESERVED_LOCK.
dc140 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 ** (3) PENDI
dc150 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 NG_LOCK.** (
dc160 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4) EXCLUSIVE_LOC
dc170 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 K.**.** Sometime
dc180 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e s when requestin
dc190 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 g one lock state
dc1a0 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 , additional loc
dc1b0 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 k states.** are
dc1c0 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 inserted in betw
dc1d0 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e een. The lockin
dc1e0 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 g might fail on
dc1f0 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 one of the later
dc200 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
dc210 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b leaving the lock
dc220 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 state different
dc230 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 from what it st
dc240 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 arted but.** sti
dc250 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 ll short of its
dc260 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f goal. The follo
dc270 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 wing chart shows
dc280 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 the allowed.**
dc290 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 transitions and
dc2a0 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 the inserted int
dc2b0 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 ermediate states
dc2c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 :.**.** UNLOC
dc2d0 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a KED -> SHARED.**
dc2e0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 SHARED -> RE
dc2f0 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 SERVED.** SHA
dc300 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 RED -> (PENDING)
dc310 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
dc320 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 RESERVED ->
dc330 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
dc340 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e LUSIVE.** PEN
dc350 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 DING -> EXCLUSIV
dc360 45 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 E.**.** Semaphor
dc370 65 20 6c 6f 63 6b 73 20 6f 6e 6c 79 20 72 65 61 e locks only rea
dc380 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c lly support EXCL
dc390 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65 USIVE locks. We
dc3a0 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69 track intermedi
dc3b0 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 ate.** lock stat
dc3c0 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 es in the sqlite
dc3d0 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 3_file structure
dc3e0 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 , but all locks
dc3f0 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 6f SHARED or.** abo
dc400 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58 ve are really EX
dc410 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e CLUSIVE locks an
dc420 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74 d exclude all ot
dc430 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 66 72 her processes fr
dc440 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65 om.** access the
dc450 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 file..**.** Thi
dc460 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f s routine will o
dc470 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c nly increase a l
dc480 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 ock. Use the sq
dc490 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a lite3OsUnlock().
dc4a0 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f ** routine to lo
dc4b0 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 wer a locking le
dc4c0 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 vel..*/.static i
dc4d0 6e 74 20 73 65 6d 4c 6f 63 6b 28 73 71 6c 69 74 nt semLock(sqlit
dc4e0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
dc4f0 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 locktype) {. u
dc500 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
dc510 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
dc520 20 20 69 6e 74 20 66 64 3b 0a 20 20 73 65 6d 5f int fd;. sem_
dc530 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d t *pSem = pFile-
dc540 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 >pOpen->pSem;.
dc550 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
dc560 4f 4b 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 OK;.. /* if we
dc570 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 6c already have a l
dc580 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 63 6c 75 ock, it is exclu
dc590 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 sive. . ** Jus
dc5a0 74 20 61 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 t adjust level a
dc5b0 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 nd punt on outta
dc5c0 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 here. */. if (
dc5d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
dc5e0 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 > NO_LOCK) {.
dc5f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dc600 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 = locktype;.
dc610 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
dc620 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d 5f 65 6e . goto sem_en
dc630 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 d_lock;. }. .
dc640 20 2f 2a 20 6c 6f 63 6b 20 73 65 6d 61 70 68 6f /* lock semapho
dc650 72 65 20 6e 6f 77 20 62 75 74 20 62 61 69 6c 20 re now but bail
dc660 6f 75 74 20 77 68 65 6e 20 61 6c 72 65 61 64 79 out when already
dc670 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 69 66 locked. */. if
dc680 28 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 ( sem_trywait(pS
dc690 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 72 em)==-1 ){. r
dc6a0 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
dc6b0 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d 5f 65 6e . goto sem_en
dc6c0 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f d_lock;. }.. /
dc6d0 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 * got it, set th
dc6e0 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 e type and retur
dc6f0 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d n ok */. pFile-
dc700 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
dc710 74 79 70 65 3b 0a 0a 20 73 65 6d 5f 65 6e 64 5f type;.. sem_end_
dc720 6c 6f 63 6b 3a 0a 20 20 72 65 74 75 72 6e 20 72 lock:. return r
dc730 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 c;.}../*.** Lowe
dc740 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 r the locking le
dc750 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 vel on file desc
dc760 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 riptor pFile to
dc770 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 locktype. lockt
dc780 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 ype.** must be e
dc790 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 ither NO_LOCK or
dc7a0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a SHARED_LOCK..**
dc7b0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 .** If the locki
dc7c0 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 ng level of the
dc7d0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
dc7e0 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 is already at or
dc7f0 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 below.** the re
dc800 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 quested locking
dc810 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 level, this rout
dc820 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
dc830 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 */.static int se
dc840 6d 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f mUnlock(sqlite3_
dc850 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
dc860 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 cktype) {. unix
dc870 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
dc880 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73 nixFile*)id;. s
dc890 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 em_t *pSem = pFi
dc8a0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b le->pOpen->pSem;
dc8b0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c .. assert( pFil
dc8c0 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 e );. assert( p
dc8d0 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 Sem );. OSTRACE
dc8e0 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 5("UNLOCK %d %d
dc8f0 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e was %d pid=%d\n
dc900 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 ", pFile->h, loc
dc910 6b 74 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 ktype,.. pFile
dc920 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 ->locktype, getp
dc930 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 id());. assert(
dc940 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 locktype<=SHARE
dc950 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f D_LOCK );. . /
dc960 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 * no-op if possi
dc970 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 ble */. if( pFi
dc980 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f le->locktype==lo
dc990 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 cktype ){. re
dc9a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
dc9b0 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 }. . /* shar
dc9c0 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73 ed can just be s
dc9d0 65 74 20 62 65 63 61 75 73 65 20 77 65 20 61 6c et because we al
dc9e0 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 78 63 ways have an exc
dc9f0 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 lusive */. if (
dca00 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
dca10 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 _LOCK) {. pFi
dca20 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
dca30 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 ocktype;. ret
dca40 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
dca50 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 }. . /* no, r
dca60 65 61 6c 6c 79 20 75 6e 6c 6f 63 6b 2e 20 2a 2f eally unlock. */
dca70 0a 20 20 69 66 20 28 20 73 65 6d 5f 70 6f 73 74 . if ( sem_post
dca80 28 70 53 65 6d 29 3d 3d 2d 31 20 29 20 7b 0a 20 (pSem)==-1 ) {.
dca90 20 20 20 69 6e 74 20 72 63 2c 20 74 45 72 72 6e int rc, tErrn
dcaa0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 o = errno;. r
dcab0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
dcac0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
dcad0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
dcae0 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 RR_UNLOCK);.
dcaf0 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
dcb00 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 R(rc) ){. p
dcb10 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
dcb20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a = tErrno;. }.
dcb30 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a return rc; .
dcb40 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 }. pFile->loc
dcb50 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b ktype = NO_LOCK;
dcb60 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
dcb70 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 _OK;.}../*. ** C
dcb80 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 20 2a 2f lose a file.. */
dcb90 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43 .static int semC
dcba0 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
dcbb0 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 e *id) {. if( i
dcbc0 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c d ){. unixFil
dcbd0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
dcbe0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 73 65 File*)id;. se
dcbf0 6d 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c mUnlock(id, NO_L
dcc00 4f 43 4b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 OCK);. assert
dcc10 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 75 ( pFile );. u
dcc20 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
dcc30 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b . releaseLock
dcc40 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 Info(pFile->pLoc
dcc50 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f k);. releaseO
dcc60 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f penCnt(pFile->pO
dcc70 70 65 6e 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 pen);. closeU
dcc80 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 nixFile(id);.
dcc90 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
dcca0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
dccb0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 SQLITE_OK;.}..#e
dccc0 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 ndif /* OS_VXWOR
dccd0 4b 53 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 KS */./*.** Name
dcce0 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b d semaphore lock
dccf0 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 ing is only avai
dcd00 6c 61 62 6c 65 20 6f 6e 20 56 78 57 6f 72 6b 73 lable on VxWorks
dcd10 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
dcd20 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 **** End of the
dcd30 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 named semaphore
dcd40 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 lock implementat
dcd50 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ion ************
dcd60 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
dcd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcdb0 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ***/.../********
dcdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dce00 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
dce10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dce20 2a 2a 20 42 65 67 69 6e 20 41 46 50 20 4c 6f 63 ** Begin AFP Loc
dce30 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a king ***********
dce40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dce50 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 ******.**.** AFP
dce60 20 69 73 20 74 68 65 20 41 70 70 6c 65 20 46 69 is the Apple Fi
dce70 6c 69 6e 67 20 50 72 6f 74 6f 63 6f 6c 2e 20 20 ling Protocol.
dce80 41 46 50 20 69 73 20 61 20 6e 65 74 77 6f 72 6b AFP is a network
dce90 20 66 69 6c 65 73 79 73 74 65 6d 20 66 6f 75 6e filesystem foun
dcea0 64 0a 2a 2a 20 6f 6e 20 41 70 70 6c 65 20 4d 61 d.** on Apple Ma
dceb0 63 69 6e 74 6f 73 68 20 63 6f 6d 70 75 74 65 72 cintosh computer
dcec0 73 20 2d 20 62 6f 74 68 20 4f 53 39 20 61 6e 64 s - both OS9 and
dced0 20 4f 53 58 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 72 OSX..**.** Thir
dcee0 64 2d 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e d-party implemen
dcef0 74 61 74 69 6f 6e 73 20 6f 66 20 41 46 50 20 61 tations of AFP a
dcf00 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 42 re available. B
dcf10 75 74 20 74 68 69 73 20 63 6f 64 65 20 68 65 72 ut this code her
dcf20 65 0a 2a 2a 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 e.** only works
dcf30 6f 6e 20 4f 53 58 2e 0a 2a 2f 0a 0a 23 69 66 20 on OSX..*/..#if
dcf40 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
dcf50 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 _) && SQLITE_ENA
dcf60 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
dcf70 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 66 70 4c E./*.** The afpL
dcf80 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 ockingContext st
dcf90 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
dcfa0 20 61 6c 6c 20 61 66 70 20 6c 6f 63 6b 20 73 70 all afp lock sp
dcfb0 65 63 69 66 69 63 20 73 74 61 74 65 0a 2a 2f 0a ecific state.*/.
dcfc0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 61 typedef struct a
dcfd0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
dcfe0 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 afpLockingConte
dcff0 78 74 3b 0a 73 74 72 75 63 74 20 61 66 70 4c 6f xt;.struct afpLo
dd000 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 ckingContext {.
dd010 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c unsigned long l
dd020 6f 6e 67 20 73 68 61 72 65 64 42 79 74 65 3b 0a ong sharedByte;.
dd030 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 62 const char *db
dd040 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 Path;
dd050 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
dd060 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b open file */.};
dd070 0a 0a 73 74 72 75 63 74 20 42 79 74 65 52 61 6e ..struct ByteRan
dd080 67 65 4c 6f 63 6b 50 42 32 0a 7b 0a 20 20 75 6e geLockPB2.{. un
dd090 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
dd0a0 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 offset;
dd0b0 2f 2a 20 6f 66 66 73 65 74 20 74 6f 20 66 69 72 /* offset to fir
dd0c0 73 74 20 62 79 74 65 20 74 6f 20 6c 6f 63 6b 20 st byte to lock
dd0d0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f */. unsigned lo
dd0e0 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 3b 20 ng long length;
dd0f0 20 20 20 20 20 20 20 2f 2a 20 6e 62 72 20 6f 66 /* nbr of
dd100 20 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a bytes to lock *
dd110 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e /. unsigned lon
dd120 67 20 6c 6f 6e 67 20 72 65 74 52 61 6e 67 65 53 g long retRangeS
dd130 74 61 72 74 3b 20 2f 2a 20 6e 62 72 20 6f 66 20 tart; /* nbr of
dd140 31 73 74 20 62 79 74 65 20 6c 6f 63 6b 65 64 20 1st byte locked
dd150 69 66 20 73 75 63 63 65 73 73 66 75 6c 20 2a 2f if successful */
dd160 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
dd170 20 75 6e 4c 6f 63 6b 46 6c 61 67 3b 20 20 20 20 unLockFlag;
dd180 20 20 20 20 20 2f 2a 20 31 20 3d 20 75 6e 6c 6f /* 1 = unlo
dd190 63 6b 2c 20 30 20 3d 20 6c 6f 63 6b 20 2a 2f 0a ck, 0 = lock */.
dd1a0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
dd1b0 73 74 61 72 74 45 6e 64 46 6c 61 67 3b 20 20 20 startEndFlag;
dd1c0 20 20 20 20 2f 2a 20 31 3d 72 65 6c 20 74 6f 20 /* 1=rel to
dd1d0 65 6e 64 20 6f 66 20 66 6f 72 6b 2c 20 30 3d 72 end of fork, 0=r
dd1e0 65 6c 20 74 6f 20 73 74 61 72 74 20 2a 2f 0a 20 el to start */.
dd1f0 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 int fd;
dd200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd210 20 20 20 2f 2a 20 66 69 6c 65 20 64 65 73 63 20 /* file desc
dd220 74 6f 20 61 73 73 6f 63 20 74 68 69 73 20 6c 6f to assoc this lo
dd230 63 6b 20 77 69 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 ck with */.};..#
dd240 64 65 66 69 6e 65 20 61 66 70 66 73 42 79 74 65 define afpfsByte
dd250 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 20 RangeLock2FSCTL
dd260 20 20 20 20 20 20 20 5f 49 4f 57 52 28 27 7a 27 _IOWR('z'
dd270 2c 20 32 33 2c 20 73 74 72 75 63 74 20 42 79 74 , 23, struct Byt
dd280 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 29 0a 0a eRangeLockPB2)..
dd290 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 /*.** This is a
dd2a0 75 74 69 6c 69 74 79 20 66 6f 72 20 73 65 74 74 utility for sett
dd2b0 69 6e 67 20 6f 72 20 63 6c 65 61 72 69 6e 67 20 ing or clearing
dd2c0 61 20 62 69 74 2d 72 61 6e 67 65 20 6c 6f 63 6b a bit-range lock
dd2d0 20 6f 6e 20 61 6e 0a 2a 2a 20 41 46 50 20 66 69 on an.** AFP fi
dd2e0 6c 65 73 79 73 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a lesystem..** .**
dd2f0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
dd300 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 K on success, SQ
dd310 4c 49 54 45 5f 42 55 53 59 20 6f 6e 20 66 61 69 LITE_BUSY on fai
dd320 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lure..*/.static
dd330 69 6e 74 20 61 66 70 53 65 74 4c 6f 63 6b 28 0a int afpSetLock(.
dd340 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 const char *pa
dd350 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 th,
dd360 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
dd370 66 69 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 file to be locke
dd380 64 20 6f 72 20 75 6e 6c 6f 63 6b 65 64 20 2a 2f d or unlocked */
dd390 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
dd3a0 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 le,
dd3b0 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 /* Open file d
dd3c0 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 70 61 74 escriptor on pat
dd3d0 68 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 h */. unsigned
dd3e0 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 long long offset
dd3f0 2c 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 , /* First b
dd400 79 74 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 yte to be locked
dd410 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c */. unsigned l
dd420 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c ong long length,
dd430 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
dd440 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 f bytes to lock
dd450 2a 2f 0a 20 20 69 6e 74 20 73 65 74 4c 6f 63 6b */. int setLock
dd460 46 6c 61 67 20 20 20 20 20 20 20 20 20 20 20 20 Flag
dd470 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 /* True to s
dd480 65 74 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 et lock. False
dd490 74 6f 20 63 6c 65 61 72 20 6c 6f 63 6b 20 2a 2f to clear lock */
dd4a0 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 42 79 74 .){. struct Byt
dd4b0 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 20 70 62 eRangeLockPB2 pb
dd4c0 3b 0a 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 0a ;. int err;. .
dd4d0 20 20 70 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 pb.unLockFlag
dd4e0 3d 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 = setLockFlag ?
dd4f0 30 20 3a 20 31 3b 0a 20 20 70 62 2e 73 74 61 72 0 : 1;. pb.star
dd500 74 45 6e 64 46 6c 61 67 20 3d 20 30 3b 0a 20 20 tEndFlag = 0;.
dd510 70 62 2e 6f 66 66 73 65 74 20 3d 20 6f 66 66 73 pb.offset = offs
dd520 65 74 3b 0a 20 20 70 62 2e 6c 65 6e 67 74 68 20 et;. pb.length
dd530 3d 20 6c 65 6e 67 74 68 3b 20 0a 20 20 70 62 2e = length; . pb.
dd540 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 fd = pFile->h;.
dd550 20 0a 20 20 4f 53 54 52 41 43 45 36 28 22 41 46 . OSTRACE6("AF
dd560 50 53 45 54 4c 4f 43 4b 20 5b 25 73 5d 20 66 6f PSETLOCK [%s] fo
dd570 72 20 25 64 25 73 20 69 6e 20 72 61 6e 67 65 20 r %d%s in range
dd580 25 6c 6c 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 %llx:%llx\n", .
dd590 20 20 20 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f (setLockFlag?
dd5a0 22 4f 4e 22 3a 22 4f 46 46 22 29 2c 20 70 46 69 "ON":"OFF"), pFi
dd5b0 6c 65 2d 3e 68 2c 20 28 70 62 2e 66 64 3d 3d 2d le->h, (pb.fd==-
dd5c0 31 3f 22 5b 74 65 73 74 76 61 6c 2d 31 5d 22 3a 1?"[testval-1]":
dd5d0 22 22 29 2c 0a 20 20 20 20 6f 66 66 73 65 74 2c ""),. offset,
dd5e0 20 6c 65 6e 67 74 68 29 3b 0a 20 20 65 72 72 20 length);. err
dd5f0 3d 20 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66 = fsctl(path, af
dd600 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b pfsByteRangeLock
dd610 32 46 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b 2FSCTL, &pb, 0);
dd620 0a 20 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20 . if ( err==-1
dd630 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a ) {. int rc;.
dd640 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d int tErrno =
dd650 20 65 72 72 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 errno;. OSTR
dd660 41 43 45 34 28 22 41 46 50 53 45 54 4c 4f 43 4b ACE4("AFPSETLOCK
dd670 20 66 61 69 6c 65 64 20 74 6f 20 66 73 63 74 6c failed to fsctl
dd680 28 29 20 27 25 73 27 20 25 64 20 25 73 5c 6e 22 () '%s' %d %s\n"
dd690 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 ,. p
dd6a0 61 74 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72 ath, tErrno, str
dd6b0 65 72 72 6f 72 28 74 45 72 72 6e 6f 29 29 3b 0a error(tErrno));.
dd6c0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 #ifdef SQLITE_IG
dd6d0 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 NORE_AFP_LOCK_ER
dd6e0 52 4f 52 53 0a 20 20 20 20 72 63 20 3d 20 53 51 RORS. rc = SQ
dd6f0 4c 49 54 45 5f 42 55 53 59 3b 0a 23 65 6c 73 65 LITE_BUSY;.#else
dd700 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
dd710 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
dd720 72 6f 72 28 74 45 72 72 6e 6f 2c 0a 20 20 20 20 ror(tErrno,.
dd730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd740 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 53 51 setLockFlag ? SQ
dd750 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 LITE_IOERR_LOCK
dd760 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 : SQLITE_IOERR_U
dd770 4e 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 20 2f NLOCK);.#endif /
dd780 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f * SQLITE_IGNORE_
dd790 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 AFP_LOCK_ERRORS
dd7a0 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f */. if( IS_LO
dd7b0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
dd7c0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
dd7d0 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
dd7e0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
dd7f0 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b n rc;. } else {
dd800 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
dd810 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a TE_OK;. }.}../*
dd820 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
dd830 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
dd840 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
dd850 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
dd860 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
dd870 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
dd880 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
dd890 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
dd8a0 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 s held, set *pRe
dd8b0 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e sOut.** to a non
dd8c0 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 -zero value othe
dd8d0 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 rwise *pResOut i
dd8e0 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 s set to zero.
dd8f0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
dd900 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 .** is set to SQ
dd910 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 LITE_OK unless a
dd920 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 n I/O error occu
dd930 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 rs during lock c
dd940 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 hecking..*/.stat
dd950 69 63 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52 ic int afpCheckR
dd960 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
dd970 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
dd980 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 t *pResOut){. i
dd990 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
dd9a0 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 K;. int reserve
dd9b0 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c d = 0;. unixFil
dd9c0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
dd9d0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 File*)id;. . S
dd9e0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
dd9f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
dda00 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 ERR_CHECKRESERVE
dda10 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 DLOCK; );. . a
dda20 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
dda30 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 afpLockingCont
dda40 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 ext *context = (
dda50 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 afpLockingContex
dda60 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b t *) pFile->lock
dda70 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 ingContext;. .
dda80 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 /* Check if a t
dda90 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 hread in this pr
ddaa0 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 ocess holds such
ddab0 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 a lock */. if(
ddac0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
ddad0 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a >SHARED_LOCK ){.
ddae0 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 reserved = 1
ddaf0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 ;. }. . /* Ot
ddb00 68 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 herwise see if s
ddb10 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ome other proces
ddb20 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 20 2a s holds it.. *
ddb30 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 /. if( !reserve
ddb40 64 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 6f 63 6b d ){. /* lock
ddb50 20 74 68 65 20 52 45 53 45 52 56 45 44 20 62 79 the RESERVED by
ddb60 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 te */. int lr
ddb70 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 c = afpSetLock(c
ddb80 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
ddb90 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f pFile, RESERVED_
ddba0 42 59 54 45 2c 20 31 2c 31 29 3b 20 20 0a 20 20 BYTE, 1,1); .
ddbb0 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
ddbc0 3d 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a =lrc ){. /*
ddbd0 20 69 66 20 77 65 20 73 75 63 63 65 65 64 65 64 if we succeeded
ddbe0 20 69 6e 20 74 61 6b 69 6e 67 20 74 68 65 20 72 in taking the r
ddbf0 65 73 65 72 76 65 64 20 6c 6f 63 6b 2c 20 75 6e eserved lock, un
ddc00 6c 6f 63 6b 20 69 74 20 74 6f 20 72 65 73 74 6f lock it to resto
ddc10 72 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 re. ** the
ddc20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 2a original state *
ddc30 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 61 66 /. lrc = af
ddc40 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 pSetLock(context
ddc50 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
ddc60 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 RESERVED_BYTE,
ddc70 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 65 6c 73 1, 0);. } els
ddc80 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 e {. /* if
ddc90 77 65 20 66 61 69 6c 65 64 20 74 6f 20 67 65 74 we failed to get
ddca0 20 74 68 65 20 6c 6f 63 6b 20 74 68 65 6e 20 73 the lock then s
ddcb0 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 75 73 74 omeone else must
ddcc0 20 68 61 76 65 20 69 74 20 2a 2f 0a 20 20 20 20 have it */.
ddcd0 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
ddce0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 49 53 }. if( IS
ddcf0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 _LOCK_ERROR(lrc)
ddd00 20 29 7b 0a 20 20 20 20 20 20 72 63 3d 6c 72 63 ){. rc=lrc
ddd10 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 ;. }. }. .
ddd20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20 OSTRACE4("TEST
ddd30 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 WR-LOCK %d %d %d
ddd40 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
ddd50 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 20 20 c, reserved);.
ddd60 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 . *pResOut = re
ddd70 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e served;. return
ddd80 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
ddd90 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 ck the file with
ddda0 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 the lock specif
dddb0 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
dddc0 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a locktype - one.
dddd0 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** of the follow
ddde0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 ing:.**.** (
dddf0 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 1) SHARED_LOCK.*
dde00 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 * (2) RESERV
dde10 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
dde20 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 3) PENDING_LOCK.
dde30 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 ** (4) EXCLU
dde40 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 SIVE_LOCK.**.**
dde50 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 Sometimes when r
dde60 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f equesting one lo
dde70 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 ck state, additi
dde80 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 onal lock states
dde90 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 .** are inserted
ddea0 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 in between. Th
ddeb0 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 e locking might
ddec0 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 fail on one of t
dded0 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e he later.** tran
ddee0 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 sitions leaving
ddef0 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 the lock state d
ddf00 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 ifferent from wh
ddf10 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 at it started bu
ddf20 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 t.** still short
ddf30 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 of its goal. T
ddf40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
ddf50 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c rt shows the all
ddf60 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 owed.** transiti
ddf70 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 ons and the inse
ddf80 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 rted intermediat
ddf90 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 e states:.**.**
ddfa0 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 UNLOCKED -> S
ddfb0 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 HARED.** SHAR
ddfc0 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a ED -> RESERVED.*
ddfd0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 * SHARED -> (
ddfe0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
ddff0 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 USIVE.** RESE
de000 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 RVED -> (PENDING
de010 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
de020 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 * PENDING ->
de030 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 EXCLUSIVE.**.**
de040 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
de050 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 l only increase
de060 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 a lock. Use the
de070 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b sqlite3OsUnlock
de080 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f ().** routine to
de090 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 lower a locking
de0a0 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 level..*/.stati
de0b0 63 20 69 6e 74 20 61 66 70 4c 6f 63 6b 28 73 71 c int afpLock(sq
de0c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
de0d0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 int locktype){.
de0e0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
de0f0 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 _OK;. unixFile
de100 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
de110 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 le*)id;. afpLoc
de120 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e kingContext *con
de130 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 text = (afpLocki
de140 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 ngContext *) pFi
de150 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
de160 78 74 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 xt;. . assert(
de170 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 pFile );. OSTR
de180 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE5("LOCK %d
de190 20 25 73 20 77 61 73 20 25 73 20 70 69 64 3d 25 %s was %s pid=%
de1a0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a d\n", pFile->h,.
de1b0 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 locktyp
de1c0 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c eName(locktype),
de1d0 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 locktypeName(pF
de1e0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 ile->locktype),
de1f0 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a getpid());.. /*
de200 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 If there is alr
de210 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 eady a lock of t
de220 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 his type or more
de230 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 restrictive on
de240 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c the. ** unixFil
de250 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 e, do nothing. D
de260 6f 6e 27 74 20 75 73 65 20 74 68 65 20 61 66 70 on't use the afp
de270 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 _end_lock: exit
de280 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 75 6e path, as. ** un
de290 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 ixEnterMutex() h
de2a0 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 asn't been calle
de2b0 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 d yet.. */. if
de2c0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
de2d0 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 e>=locktype ){.
de2e0 20 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 OSTRACE3("LOC
de2f0 4b 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 K %d %s ok (a
de300 6c 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c lready held)\n",
de310 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 pFile->h,.
de320 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 locktypeNa
de330 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 me(locktype));.
de340 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
de350 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d _OK;. }.. /* M
de360 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 ake sure the loc
de370 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 king sequence is
de380 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 correct. */.
de390 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
de3a0 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
de3b0 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 || locktype==SH
de3c0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 ARED_LOCK );. a
de3d0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
de3e0 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b =PENDING_LOCK );
de3f0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
de400 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype!=RESERVED_LO
de410 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK || pFile->loc
de420 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
de430 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 CK );. . /* Th
de440 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 is mutex is need
de450 65 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 ed because pFile
de460 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 ->pLock is share
de470 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 d across threads
de480 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 . */. unixEnte
de490 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 rMutex();.. /*
de4a0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 Make sure the cu
de4b0 72 72 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e rrent thread own
de4c0 73 20 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a s the pFile.. *
de4d0 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65 /. rc = transfe
de4e0 72 4f 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65 rOwnership(pFile
de4f0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
de500 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e ITE_OK ){. un
de510 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
de520 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
de530 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20 41 20 50 }. . /* A P
de540 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e ENDING lock is n
de550 65 65 64 65 64 20 62 65 66 6f 72 65 20 61 63 71 eeded before acq
de560 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 uiring a SHARED
de570 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a lock and before.
de580 20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61 ** acquiring a
de590 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
de5a0 2e 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 45 . For the SHARE
de5b0 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 D lock, the PEND
de5c0 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 ING will. ** be
de5d0 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a released.. */.
de5e0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
de5f0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 SHARED_LOCK .
de600 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d || (locktype=
de610 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
de620 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 && pFile->lockty
de630 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 pe<PENDING_LOCK)
de640 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 . ){. int fa
de650 69 6c 65 64 3b 0a 20 20 20 20 66 61 69 6c 65 64 iled;. failed
de660 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f = afpSetLock(co
de670 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 ntext->dbPath, p
de680 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 File, PENDING_BY
de690 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 TE, 1, 1);. i
de6a0 66 20 28 66 61 69 6c 65 64 29 20 7b 0a 20 20 20 f (failed) {.
de6b0 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a rc = failed;.
de6c0 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 goto afp_e
de6d0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 nd_lock;. }.
de6e0 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 20 63 6f }. . /* If co
de6f0 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 ntrol gets to th
de700 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 is point, then a
de710 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 ctually go ahead
de720 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f and make. ** o
de730 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
de740 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70 calls for the sp
de750 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 ecified lock..
de760 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
de770 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
de780 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c 20 6c 72 {. int lk, lr
de790 63 31 2c 20 6c 72 63 32 2c 20 6c 72 63 31 45 72 c1, lrc2, lrc1Er
de7a0 72 6e 6f 3b 0a 20 20 20 20 0a 20 20 20 20 2f 2a rno;. . /*
de7b0 20 4e 6f 77 20 67 65 74 20 74 68 65 20 72 65 61 Now get the rea
de7c0 64 2d 6c 6f 63 6b 20 53 48 41 52 45 44 5f 4c 4f d-lock SHARED_LO
de7d0 43 4b 20 2a 2f 0a 20 20 20 20 2f 2a 20 6e 6f 74 CK */. /* not
de7e0 65 20 74 68 61 74 20 74 68 65 20 71 75 61 6c 69 e that the quali
de7f0 74 79 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d ty of the random
de800 6e 65 73 73 20 64 6f 65 73 6e 27 74 20 6d 61 74 ness doesn't mat
de810 74 65 72 20 74 68 61 74 20 6d 75 63 68 20 2a 2f ter that much */
de820 0a 20 20 20 20 6c 6b 20 3d 20 72 61 6e 64 6f 6d . lk = random
de830 28 29 3b 20 0a 20 20 20 20 63 6f 6e 74 65 78 74 (); . context
de840 2d 3e 73 68 61 72 65 64 42 79 74 65 20 3d 20 28 ->sharedByte = (
de850 6c 6b 20 26 20 30 78 37 66 66 66 66 66 66 66 29 lk & 0x7fffffff)
de860 25 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 %(SHARED_SIZE -
de870 31 29 3b 0a 20 20 20 20 6c 72 63 31 20 3d 20 61 1);. lrc1 = a
de880 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 fpSetLock(contex
de890 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 t->dbPath, pFile
de8a0 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53 48 41 , . SHA
de8b0 52 45 44 5f 46 49 52 53 54 2b 63 6f 6e 74 65 78 RED_FIRST+contex
de8c0 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 t->sharedByte, 1
de8d0 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 49 53 , 1);. if( IS
de8e0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 _LOCK_ERROR(lrc1
de8f0 29 20 29 7b 0a 20 20 20 20 20 20 6c 72 63 31 45 ) ){. lrc1E
de900 72 72 6e 6f 20 3d 20 70 46 69 6c 65 2d 3e 6c 61 rrno = pFile->la
de910 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 stErrno;. }.
de920 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 /* Drop the t
de930 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e 47 emporary PENDING
de940 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 72 63 lock */. lrc
de950 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 2 = afpSetLock(c
de960 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
de970 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 pFile, PENDING_B
de980 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 YTE, 1, 0);.
de990 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b . if( IS_LOCK
de9a0 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 29 20 7b _ERROR(lrc1) ) {
de9b0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
de9c0 73 74 45 72 72 6e 6f 20 3d 20 6c 72 63 31 45 72 stErrno = lrc1Er
de9d0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 rno;. rc =
de9e0 6c 72 63 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f lrc1;. goto
de9f0 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 afp_end_lock;.
dea00 20 20 20 7d 20 65 6c 73 65 20 69 66 28 20 49 53 } else if( IS
dea10 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 32 _LOCK_ERROR(lrc2
dea20 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) ){. rc =
dea30 6c 72 63 32 3b 0a 20 20 20 20 20 20 67 6f 74 6f lrc2;. goto
dea40 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 afp_end_lock;.
dea50 20 20 20 7d 20 65 6c 73 65 20 69 66 28 20 6c 72 } else if( lr
dea60 63 31 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 c1 != SQLITE_OK
dea70 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c ) {. rc = l
dea80 72 63 31 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 rc1;. } else
dea90 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
deaa0 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 ocktype = SHARED
deab0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 _LOCK;. pFi
deac0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b le->pOpen->nLock
dead0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 ++;. }. }els
deae0 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 e{. /* The re
deaf0 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 20 quest was for a
deb00 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c RESERVED or EXCL
deb10 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 USIVE lock. It
deb20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 is. ** assume
deb30 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 d that there is
deb40 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 a SHARED or grea
deb50 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ter lock on the
deb60 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 file. ** alre
deb70 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ady.. */.
deb80 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 30 3b 0a int failed = 0;.
deb90 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70 assert( 0!=p
deba0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
debb0 3b 0a 20 20 20 20 69 66 20 28 6c 6f 63 6b 74 79 ;. if (lockty
debc0 70 65 20 3e 3d 20 52 45 53 45 52 56 45 44 5f 4c pe >= RESERVED_L
debd0 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f OCK && pFile->lo
debe0 63 6b 74 79 70 65 20 3c 20 52 45 53 45 52 56 45 cktype < RESERVE
debf0 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 D_LOCK) {.
dec00 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52 /* Acquire a R
dec10 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a ESERVED lock */.
dec20 20 20 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d failed =
dec30 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 afpSetLock(cont
dec40 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 ext->dbPath, pFi
dec50 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 le, RESERVED_BYT
dec60 45 2c 20 31 2c 31 29 3b 0a 20 20 20 20 7d 0a 20 E, 1,1);. }.
dec70 20 20 20 69 66 20 28 21 66 61 69 6c 65 64 20 26 if (!failed &
dec80 26 20 6c 6f 63 6b 74 79 70 65 20 3d 3d 20 45 58 & locktype == EX
dec90 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 20 7b 0a CLUSIVE_LOCK) {.
deca0 20 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65 /* Acquire
decb0 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
decc0 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 0a 20 ck */. .
decd0 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 /* Remove t
dece0 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 62 he shared lock b
decf0 65 66 6f 72 65 20 74 72 79 69 6e 67 20 74 68 65 efore trying the
ded00 20 72 61 6e 67 65 2e 20 20 77 65 27 6c 6c 20 6e range. we'll n
ded10 65 65 64 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a eed to . **
ded20 20 72 65 65 73 74 61 62 6c 69 73 68 20 74 68 65 reestablish the
ded30 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 66 20 shared lock if
ded40 77 65 20 63 61 6e 27 74 20 67 65 74 20 74 68 65 we can't get the
ded50 20 20 61 66 70 55 6e 6c 6f 63 6b 0a 20 20 20 20 afpUnlock.
ded60 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 */. if( !
ded70 28 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 (failed = afpSet
ded80 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 Lock(context->db
ded90 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 Path, pFile, SHA
deda0 52 45 44 5f 46 49 52 53 54 20 2b 0a 20 20 20 20 RED_FIRST +.
dedb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dedc0 20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 context->sh
dedd0 61 72 65 64 42 79 74 65 2c 20 31 2c 20 30 29 29 aredByte, 1, 0))
dede0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
dedf0 66 61 69 6c 65 64 32 20 3d 20 53 51 4c 49 54 45 failed2 = SQLITE
dee00 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 _OK;. /*
dee10 6e 6f 77 20 61 74 74 65 6d 6d 70 74 20 74 6f 20 now attemmpt to
dee20 67 65 74 20 74 68 65 20 65 78 63 6c 75 73 69 76 get the exclusiv
dee30 65 20 6c 6f 63 6b 20 72 61 6e 67 65 20 2a 2f 0a e lock range */.
dee40 20 20 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d failed =
dee50 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 afpSetLock(cont
dee60 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 ext->dbPath, pFi
dee70 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 le, SHARED_FIRST
dee80 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
dee90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
deea0 20 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 31 SHARED_SIZE, 1
deeb0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 );. if( f
deec0 61 69 6c 65 64 20 26 26 20 28 66 61 69 6c 65 64 ailed && (failed
deed0 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 2 = afpSetLock(c
deee0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
deef0 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 pFile, .
def00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 S
def10 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 63 6f HARED_FIRST + co
def20 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 ntext->sharedByt
def30 65 2c 20 31 2c 20 31 29 29 20 29 7b 0a 20 20 20 e, 1, 1)) ){.
def40 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 27 74 20 /* Can't
def50 72 65 65 73 74 61 62 6c 69 73 68 20 74 68 65 20 reestablish the
def60 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20 53 71 shared lock. Sq
def70 6c 69 74 65 20 63 61 6e 27 74 20 64 65 61 6c 2c lite can't deal,
def80 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 20 20 this is.
def90 20 20 20 2a 2a 20 61 20 63 72 69 74 69 63 61 6c ** a critical
defa0 20 49 2f 4f 20 65 72 72 6f 72 0a 20 20 20 20 20 I/O error.
defb0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
defc0 20 20 72 63 20 3d 20 28 28 66 61 69 6c 65 64 20 rc = ((failed
defd0 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 & SQLITE_IOERR)
defe0 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 == SQLITE_IOERR)
deff0 20 3f 20 66 61 69 6c 65 64 32 20 3a 20 0a 20 20 ? failed2 : .
df000 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c SQL
df010 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 3b 0a ITE_IOERR_LOCK;.
df020 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 goto a
df030 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 fp_end_lock;.
df040 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 7d 65 } . }e
df050 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 lse{. rc
df060 3d 20 66 61 69 6c 65 64 3b 20 0a 20 20 20 20 20 = failed; .
df070 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
df080 20 66 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 failed ){.
df090 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 rc = failed;.
df0a0 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 }. }. . if(
df0b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
df0c0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 {. pFile->loc
df0d0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
df0e0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f ;. }else if( lo
df0f0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
df100 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 E_LOCK ){. pF
df110 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
df120 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 PENDING_LOCK;.
df130 7d 0a 20 20 0a 61 66 70 5f 65 6e 64 5f 6c 6f 63 }. .afp_end_loc
df140 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 k:. unixLeaveMu
df150 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 tex();. OSTRACE
df160 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 4("LOCK %d %s
df170 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %s\n", pFile->h
df180 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c , locktypeName(l
df190 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 ocktype), .
df1a0 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rc==SQLITE_O
df1b0 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c K ? "ok" : "fail
df1c0 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ed");. return r
df1d0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 c;.}../*.** Lowe
df1e0 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 r the locking le
df1f0 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 vel on file desc
df200 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 riptor pFile to
df210 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 locktype. lockt
df220 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 ype.** must be e
df230 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 ither NO_LOCK or
df240 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a SHARED_LOCK..**
df250 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 .** If the locki
df260 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 ng level of the
df270 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
df280 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 is already at or
df290 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 below.** the re
df2a0 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 quested locking
df2b0 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 level, this rout
df2c0 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
df2d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 */.static int af
df2e0 70 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f pUnlock(sqlite3_
df2f0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
df300 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 20 cktype) {. int
df310 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
df320 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
df330 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
df340 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 d;. afpLockingC
df350 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 ontext *pCtx = (
df360 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 afpLockingContex
df370 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b t *) pFile->lock
df380 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 ingContext;.. a
df390 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
df3a0 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f OSTRACE5("UNLO
df3b0 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 CK %d %d was %d
df3c0 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c pid=%d\n", pFil
df3d0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a e->h, locktype,.
df3e0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e pFile->
df3f0 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 locktype, getpid
df400 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ());.. assert(
df410 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 locktype<=SHARED
df420 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 _LOCK );. if( p
df430 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d File->locktype<=
df440 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 locktype ){.
df450 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
df460 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43 ;. }. if( CHEC
df470 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 6c 65 K_THREADID(pFile
df480 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
df490 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
df4a0 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 }. unixEnterMu
df4b0 74 65 78 28 29 3b 0a 20 20 69 66 28 20 70 46 69 tex();. if( pFi
df4c0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 le->locktype>SHA
df4d0 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
df4e0 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e . if( pFile->
df4f0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 locktype==EXCLUS
df500 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 IVE_LOCK ){.
df510 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 rc = afpSetLoc
df520 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 k(pCtx->dbPath,
df530 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 pFile, SHARED_FI
df540 52 53 54 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 RST, SHARED_SIZE
df550 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
df560 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
df570 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
df580 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 D_LOCK ){.
df590 20 20 2f 2a 20 6f 6e 6c 79 20 72 65 2d 65 73 74 /* only re-est
df5a0 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 ablish the share
df5b0 64 20 6c 6f 63 6b 20 69 66 20 6e 65 63 65 73 73 d lock if necess
df5c0 61 72 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 ary */. i
df5d0 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 nt sharedLockByt
df5e0 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 e = SHARED_FIRST
df5f0 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 42 79 74 +pCtx->sharedByt
df600 65 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 e;. rc =
df610 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d afpSetLock(pCtx-
df620 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 >dbPath, pFile,
df630 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 sharedLockByte,
df640 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 1, 1);. }.
df650 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
df660 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 =SQLITE_OK && pF
df670 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50 ile->locktype>=P
df680 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 ENDING_LOCK ){.
df690 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 rc = afpSet
df6a0 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 Lock(pCtx->dbPat
df6b0 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e h, pFile, PENDIN
df6c0 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 G_BYTE, 1, 0);.
df6d0 20 20 20 7d 20 0a 20 20 20 20 69 66 28 20 72 63 } . if( rc
df6e0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
df6f0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d File->locktype>=
df700 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b RESERVED_LOCK ){
df710 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 . rc = afpS
df720 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 etLock(pCtx->dbP
df730 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 ath, pFile, RESE
df740 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 RVED_BYTE, 1, 0)
df750 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 ;. }. }else
df760 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f if( locktype==NO
df770 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 _LOCK ){. /*
df780 63 6c 65 61 72 20 74 68 65 20 73 68 61 72 65 64 clear the shared
df790 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 lock */. int
df7a0 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 sharedLockByte
df7b0 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 70 = SHARED_FIRST+p
df7c0 43 74 78 2d 3e 73 68 61 72 65 64 42 79 74 65 3b Ctx->sharedByte;
df7d0 0a 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 . rc = afpSet
df7e0 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 Lock(pCtx->dbPat
df7f0 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 72 65 64 h, pFile, shared
df800 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 30 29 3b LockByte, 1, 0);
df810 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d . }.. if( rc==
df820 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
df830 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e if( locktype==N
df840 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 O_LOCK ){.
df850 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 struct unixOpenC
df860 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c nt *pOpen = pFil
df870 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 e->pOpen;.
df880 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a pOpen->nLock--;.
df890 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
df8a0 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b pen->nLock>=0 );
df8b0 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e . if( pOpen
df8c0 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 4f ->nLock==0 && pO
df8d0 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3e 30 20 pen->nPending>0
df8e0 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 ){. int i
df8f0 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d ;. for(i=
df900 30 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50 65 6e 0; i<pOpen->nPen
df910 64 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 ding; i++){.
df920 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d if( pOpen-
df930 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3c 20 30 >aPending[i] < 0
df940 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
df950 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 if( close
df960 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 (pOpen->aPending
df970 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 [i]) ){.
df980 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
df990 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
df9a0 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
df9b0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 QLITE_IOERR_CLOS
df9c0 45 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c E;. }el
df9d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
df9e0 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b pOpen->aPending[
df9f0 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 i] = -1;.
dfa00 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
dfa10 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
dfa20 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
dfa30 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
dfa40 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 ee(pOpen->aPendi
dfa50 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 ng);. p
dfa60 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d Open->nPending =
dfa70 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 0;. pO
dfa80 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20 pen->aPending =
dfa90 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 0;. }.
dfaa0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
dfab0 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
dfac0 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
dfad0 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e ITE_OK ) pFile->
dfae0 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
dfaf0 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ype;. return rc
dfb00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 ;.}../*.** Close
dfb10 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61 6e 75 a file & cleanu
dfb20 70 20 41 46 50 20 73 70 65 63 69 66 69 63 20 6c p AFP specific l
dfb30 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 0a ocking context .
dfb40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 */.static int af
dfb50 70 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 pClose(sqlite3_f
dfb60 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 ile *id) {. if(
dfb70 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 id ){. unixF
dfb80 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
dfb90 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 ixFile*)id;.
dfba0 61 66 70 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f afpUnlock(id, NO
dfbb0 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e 69 78 _LOCK);. unix
dfbc0 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
dfbd0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4f 70 if( pFile->pOp
dfbe0 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 4f 70 en && pFile->pOp
dfbf0 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 en->nLock ){.
dfc00 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 /* If there a
dfc10 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c re outstanding l
dfc20 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74 ocks, do not act
dfc30 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 20 ually close the
dfc40 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 20 file just.
dfc50 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74 ** yet because t
dfc60 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 hat would clear
dfc70 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e those locks. In
dfc80 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66 stead, add the f
dfc90 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 ile. ** des
dfca0 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e criptor to pOpen
dfcb0 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 74 20 ->aPending. It
dfcc0 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 will be automati
dfcd0 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 68 65 cally closed whe
dfce0 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c n. ** the l
dfcf0 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 ast lock is clea
dfd00 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 red.. */.
dfd10 20 20 20 20 69 6e 74 20 2a 61 4e 65 77 3b 0a 20 int *aNew;.
dfd20 20 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 struct unix
dfd30 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d OpenCnt *pOpen =
dfd40 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 pFile->pOpen;.
dfd50 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 aNew = sqli
dfd60 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 4f 70 65 te3_realloc(pOpe
dfd70 6e 2d 3e 61 50 65 6e 64 69 6e 67 2c 20 28 70 4f n->aPending, (pO
dfd80 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 31 29 pen->nPending+1)
dfd90 2a 73 69 7a 65 6f 66 28 69 6e 74 29 20 29 3b 0a *sizeof(int) );.
dfda0 20 20 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d if( aNew==
dfdb0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 0 ){. /*
dfdc0 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c If a malloc fail
dfdd0 73 2c 20 6a 75 73 74 20 6c 65 61 6b 20 74 68 65 s, just leak the
dfde0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
dfdf0 20 2a 2f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b */. }else{
dfe00 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e . pOpen->
dfe10 61 50 65 6e 64 69 6e 67 20 3d 20 61 4e 65 77 3b aPending = aNew;
dfe20 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e . pOpen->
dfe30 61 50 65 6e 64 69 6e 67 5b 70 4f 70 65 6e 2d 3e aPending[pOpen->
dfe40 6e 50 65 6e 64 69 6e 67 5d 20 3d 20 70 46 69 6c nPending] = pFil
dfe50 65 2d 3e 68 3b 0a 20 20 20 20 20 20 20 20 70 4f e->h;. pO
dfe60 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 2b 3b pen->nPending++;
dfe70 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
dfe80 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a h = -1;. }.
dfe90 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 }. releas
dfea0 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e eOpenCnt(pFile->
dfeb0 70 4f 70 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69 pOpen);. sqli
dfec0 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e te3_free(pFile->
dfed0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b lockingContext);
dfee0 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 . closeUnixFi
dfef0 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 le(id);. unix
dff00 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
dff10 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
dff20 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 E_OK;.}..#endif
dff30 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 /* defined(__APP
dff40 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f LE__) && SQLITE_
dff50 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
dff60 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 TYLE */./*.** Th
dff70 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73 20 e code above is
dff80 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70 the AFP lock imp
dff90 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 lementation. Th
dffa0 65 20 63 6f 64 65 20 69 73 20 73 70 65 63 69 66 e code is specif
dffb0 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20 ic.** to MacOSX
dffc0 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 and does not wor
dffd0 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 20 k on other unix
dffe0 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61 platforms. No a
dfff0 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73 lternative.** is
e0000 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 available. If
e0010 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c you don't compil
e0020 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 65 e for a mac, the
e0030 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 22 n the "unix-afp"
e0040 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20 61 .** VFS is not a
e0050 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a vailable..**.***
e0060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0070 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41 46 ** End of the AF
e0080 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 P lock implement
e0090 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ation **********
e00a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
e00b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e00c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e00d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e00e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e00f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f ***********/.../
e0100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
e0150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
e0160 4e 6f 6e 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 Non-locking sqli
e0170 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 te3_file methods
e0180 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
e0190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
e01a0 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 64 69 *.** The next di
e01b0 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 vision contains
e01c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
e01d0 66 6f 72 20 61 6c 6c 20 6d 65 74 68 6f 64 73 20 for all methods
e01e0 6f 66 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 of the .** sqlit
e01f0 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 6f e3_file object o
e0200 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6c 6f ther than the lo
e0210 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 2e 20 20 cking methods.
e0220 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 6d The locking.** m
e0230 65 74 68 6f 64 73 20 77 65 72 65 20 64 65 66 69 ethods were defi
e0240 6e 65 64 20 69 6e 20 64 69 76 69 73 69 6f 6e 73 ned in divisions
e0250 20 61 62 6f 76 65 20 28 6f 6e 65 20 6c 6f 63 6b above (one lock
e0260 69 6e 67 20 6d 65 74 68 6f 64 20 70 65 72 0a 2a ing method per.*
e0270 2a 20 64 69 76 69 73 69 6f 6e 29 2e 20 20 54 68 * division). Th
e0280 6f 73 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 ose methods that
e0290 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 are common to a
e02a0 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 73 ll locking modes
e02b0 0a 2a 2a 20 61 72 65 20 67 61 74 68 65 72 20 74 .** are gather t
e02c0 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 74 68 69 ogether into thi
e02d0 73 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a s division..*/..
e02e0 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 /*.** Seek to th
e02f0 65 20 6f 66 66 73 65 74 20 70 61 73 73 65 64 20 e offset passed
e0300 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
e0310 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72 65 61 gument, then rea
e0320 64 20 63 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20 d cnt .** bytes
e0330 69 6e 74 6f 20 70 42 75 66 2e 20 52 65 74 75 72 into pBuf. Retur
e0340 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
e0350 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72 bytes actually r
e0360 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 ead..**.** NB:
e0370 49 66 20 79 6f 75 20 64 65 66 69 6e 65 20 55 53 If you define US
e0380 45 5f 50 52 45 41 44 20 6f 72 20 55 53 45 5f 50 E_PREAD or USE_P
e0390 52 45 41 44 36 34 2c 20 74 68 65 6e 20 69 74 20 READ64, then it
e03a0 6d 69 67 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65 might also.** be
e03b0 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 64 65 necessary to de
e03c0 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 fine _XOPEN_SOUR
e03d0 43 45 20 74 6f 20 62 65 20 35 30 30 2e 20 20 54 CE to be 500. T
e03e0 68 69 73 20 76 61 72 69 65 73 20 66 72 6f 6d 0a his varies from.
e03f0 2a 2a 20 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f ** one system to
e0400 20 61 6e 6f 74 68 65 72 2e 20 20 53 69 6e 63 65 another. Since
e0410 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 SQLite does not
e0420 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 define USE_PREA
e0430 44 0a 2a 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72 D.** any any for
e0440 6d 20 62 79 20 64 65 66 61 75 6c 74 2c 20 77 65 m by default, we
e0450 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 will not attemp
e0460 74 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 t to define _XOP
e0470 45 4e 5f 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65 EN_SOURCE..** Se
e0480 65 20 74 69 63 6b 65 74 73 20 23 32 37 34 31 20 e tickets #2741
e0490 61 6e 64 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a and #2681..**.**
e04a0 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 To avoid stompi
e04b0 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c ng the errno val
e04c0 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 72 ue on a failed r
e04d0 65 61 64 20 74 68 65 20 6c 61 73 74 45 72 72 6e ead the lastErrn
e04e0 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 o value.** is se
e04f0 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 t before returni
e0500 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
e0510 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 6e t seekAndRead(un
e0520 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 ixFile *id, sqli
e0530 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 te3_int64 offset
e0540 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e , void *pBuf, in
e0550 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f t cnt){. int go
e0560 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73 t;. i64 newOffs
e0570 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 et;. TIMER_STAR
e0580 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 T;.#if defined(U
e0590 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20 SE_PREAD). got
e05a0 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 = pread(id->h, p
e05b0 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 Buf, cnt, offset
e05c0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
e05d0 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 rror( got = -1 )
e05e0 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 ;.#elif defined(
e05f0 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 USE_PREAD64). g
e0600 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 2d ot = pread64(id-
e0610 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f >h, pBuf, cnt, o
e0620 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 ffset);. Simula
e0630 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d teIOError( got =
e0640 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e -1 );.#else. n
e0650 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b ewOffset = lseek
e0660 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 (id->h, offset,
e0670 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d SEEK_SET);. Sim
e0680 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e 65 ulateIOError( ne
e0690 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69 wOffset-- );. i
e06a0 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 f( newOffset!=of
e06b0 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 fset ){. if(
e06c0 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 newOffset == -1
e06d0 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 ){. ((unixF
e06e0 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 ile*)id)->lastEr
e06f0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
e0700 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 }else{. ((
e0710 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c unixFile*)id)->l
e0720 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 astErrno = 0;...
e0730 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
e0740 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 n -1;. }. got
e0750 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 = read(id->h, pB
e0760 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 uf, cnt);.#endif
e0770 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 . TIMER_END;.
e0780 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 if( got<0 ){.
e0790 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
e07a0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
e07b0 72 6e 6f 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 rno;. }. OSTRA
e07c0 43 45 35 28 22 52 45 41 44 20 20 20 20 25 2d 33 CE5("READ %-3
e07d0 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 d %5d %7lld %llu
e07e0 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c \n", id->h, got,
e07f0 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45 offset, TIMER_E
e0800 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 72 LAPSED);. retur
e0810 6e 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n got;.}../*.**
e0820 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 Read data from a
e0830 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 file into a buf
e0840 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c fer. Return SQL
e0850 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a ITE_OK if all.**
e0860 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 bytes were read
e0870 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e successfully an
e0880 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 d SQLITE_IOERR i
e0890 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a f anything goes.
e08a0 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 ** wrong..*/.sta
e08b0 74 69 63 20 69 6e 74 20 75 6e 69 78 52 65 61 64 tic int unixRead
e08c0 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
e08d0 20 2a 69 64 2c 20 0a 20 20 76 6f 69 64 20 2a 70 *id, . void *p
e08e0 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c Buf, . int amt,
e08f0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
e0900 20 6f 66 66 73 65 74 0a 29 7b 0a 20 20 69 6e 74 offset.){. int
e0910 20 67 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 20 got;. assert(
e0920 69 64 20 29 3b 0a 20 20 67 6f 74 20 3d 20 73 65 id );. got = se
e0930 65 6b 41 6e 64 52 65 61 64 28 28 75 6e 69 78 46 ekAndRead((unixF
e0940 69 6c 65 2a 29 69 64 2c 20 6f 66 66 73 65 74 2c ile*)id, offset,
e0950 20 70 42 75 66 2c 20 61 6d 74 29 3b 0a 20 20 69 pBuf, amt);. i
e0960 66 28 20 67 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 f( got==amt ){.
e0970 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e0980 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 _OK;. }else if(
e0990 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20 2f 2a got<0 ){. /*
e09a0 20 6c 61 73 74 45 72 72 6e 6f 20 73 65 74 20 62 lastErrno set b
e09b0 79 20 73 65 65 6b 41 6e 64 52 65 61 64 20 2a 2f y seekAndRead */
e09c0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
e09d0 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 TE_IOERR_READ;.
e09e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 28 28 75 6e }else{. ((un
e09f0 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 ixFile*)id)->las
e0a00 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e tErrno = 0; /* n
e0a10 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 6f ot a system erro
e0a20 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 6e 72 65 r */. /* Unre
e0a30 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 ad parts of the
e0a40 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a buffer must be z
e0a50 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 ero-filled */.
e0a60 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 memset(&((char
e0a70 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c *)pBuf)[got], 0,
e0a80 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 amt-got);. r
e0a90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
e0aa0 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 RR_SHORT_READ;.
e0ab0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b }.}../*.** Seek
e0ac0 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 69 to the offset i
e0ad0 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74 68 65 n id->offset the
e0ae0 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74 65 73 n read cnt bytes
e0af0 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 into pBuf..** R
e0b00 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
e0b10 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c of bytes actual
e0b20 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61 74 65 ly read. Update
e0b30 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a the offset..**.
e0b40 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d ** To avoid stom
e0b50 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76 ping the errno v
e0b60 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 alue on a failed
e0b70 20 77 72 69 74 65 20 74 68 65 20 6c 61 73 74 45 write the lastE
e0b80 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 rrno value.** is
e0b90 20 73 65 74 20 62 65 66 6f 72 65 20 72 65 74 75 set before retu
e0ba0 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 rning..*/.static
e0bb0 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 72 69 74 int seekAndWrit
e0bc0 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 e(unixFile *id,
e0bd0 69 36 34 20 6f 66 66 73 65 74 2c 20 63 6f 6e 73 i64 offset, cons
e0be0 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e t void *pBuf, in
e0bf0 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f t cnt){. int go
e0c00 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73 t;. i64 newOffs
e0c10 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 et;. TIMER_STAR
e0c20 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 T;.#if defined(U
e0c30 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20 SE_PREAD). got
e0c40 3d 20 70 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 = pwrite(id->h,
e0c50 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 pBuf, cnt, offse
e0c60 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 t);.#elif define
e0c70 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 d(USE_PREAD64).
e0c80 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36 34 28 got = pwrite64(
e0c90 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 id->h, pBuf, cnt
e0ca0 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 73 65 , offset);.#else
e0cb0 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c . newOffset = l
e0cc0 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 seek(id->h, offs
e0cd0 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 et, SEEK_SET);.
e0ce0 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d if( newOffset!=
e0cf0 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66 offset ){. if
e0d00 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d ( newOffset == -
e0d10 31 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 1 ){. ((uni
e0d20 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 xFile*)id)->last
e0d30 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
e0d40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
e0d50 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
e0d60 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 >lastErrno = 0;.
e0d70 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 ... }. ret
e0d80 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f urn -1;. }. go
e0d90 74 20 3d 20 77 72 69 74 65 28 69 64 2d 3e 68 2c t = write(id->h,
e0da0 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e pBuf, cnt);.#en
e0db0 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b dif. TIMER_END;
e0dc0 0a 20 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a . if( got<0 ){.
e0dd0 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
e0de0 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d id)->lastErrno =
e0df0 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f errno;. }.. O
e0e00 53 54 52 41 43 45 35 28 22 57 52 49 54 45 20 20 STRACE5("WRITE
e0e10 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 20 %-3d %5d %7lld
e0e20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 %llu\n", id->h,
e0e30 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d got, offset, TIM
e0e40 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 ER_ELAPSED);. r
e0e50 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f eturn got;.}.../
e0e60 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 *.** Write data
e0e70 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e from a buffer in
e0e80 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 to a file. Retu
e0e90 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 rn SQLITE_OK on
e0ea0 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f success.** or so
e0eb0 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 me other error c
e0ec0 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a ode on failure..
e0ed0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
e0ee0 69 78 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 ixWrite(. sqlit
e0ef0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 e3_file *id, .
e0f00 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 const void *pBuf
e0f10 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 , . int amt,.
e0f20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 sqlite3_int64 of
e0f30 66 73 65 74 20 0a 29 7b 0a 20 20 69 6e 74 20 77 fset .){. int w
e0f40 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 rote = 0;. asse
e0f50 72 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65 rt( id );. asse
e0f60 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 23 69 rt( amt>0 );..#i
e0f70 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f fndef NDEBUG. /
e0f80 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e * If we are doin
e0f90 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 g a normal write
e0fa0 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 to a database f
e0fb0 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 ile (as opposed
e0fc0 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 to. ** doing a
e0fd0 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c hot-journal roll
e0fe0 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 back or a write
e0ff0 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 to some file oth
e1000 65 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e er than a. ** n
e1010 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 ormal database f
e1020 69 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 ile) then record
e1030 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 the fact that t
e1040 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a he database. **
e1050 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 has changed. I
e1060 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f f the transactio
e1070 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 n counter is mod
e1080 69 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 ified, record th
e1090 61 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f at. ** fact too
e10a0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 28 75 .. */. if( ((u
e10b0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 6e nixFile*)id)->in
e10c0 4e 6f 72 6d 61 6c 57 72 69 74 65 20 29 7b 0a 20 NormalWrite ){.
e10d0 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 unixFile *pFi
e10e0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
e10f0 69 64 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 id;. pFile->d
e1100 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a bUpdate = 1; /*
e1110 20 54 68 65 20 64 61 74 61 62 61 73 65 20 68 61 The database ha
e1120 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 s been modified
e1130 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65 */. if( offse
e1140 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b t<=24 && offset+
e1150 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20 amt>=27 ){.
e1160 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d char oldCntr[4]
e1170 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 ;. Simulate
e1180 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 IOErrorBenign(1)
e1190 3b 0a 20 20 20 20 20 20 73 65 65 6b 41 6e 64 52 ;. seekAndR
e11a0 65 61 64 28 70 46 69 6c 65 2c 20 32 34 2c 20 6f ead(pFile, 24, o
e11b0 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20 20 20 20 ldCntr, 4);.
e11c0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
e11d0 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 20 20 rBenign(0);.
e11e0 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 6f 6c 64 if( memcmp(old
e11f0 43 6e 74 72 2c 20 26 28 28 63 68 61 72 2a 29 70 Cntr, &((char*)p
e1200 42 75 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c Buf)[24-offset],
e1210 20 34 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 4)!=0 ){.
e1220 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e pFile->transCn
e1230 74 72 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 trChng = 1; /*
e1240 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 The transaction
e1250 63 6f 75 6e 74 65 72 20 68 61 73 20 63 68 61 6e counter has chan
e1260 67 65 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 ged */. }.
e1270 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
e1280 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20 . while( amt>0
e1290 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b && (wrote = seek
e12a0 41 6e 64 57 72 69 74 65 28 28 75 6e 69 78 46 69 AndWrite((unixFi
e12b0 6c 65 2a 29 69 64 2c 20 6f 66 66 73 65 74 2c 20 le*)id, offset,
e12c0 70 42 75 66 2c 20 61 6d 74 29 29 3e 30 20 29 7b pBuf, amt))>0 ){
e12d0 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 . amt -= wrot
e12e0 65 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 2b 3d e;. offset +=
e12f0 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 wrote;. pBuf
e1300 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 = &((char*)pBuf
e1310 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20 )[wrote];. }.
e1320 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
e1330 28 20 77 72 6f 74 65 3d 28 2d 31 29 2c 20 61 6d ( wrote=(-1), am
e1340 74 3d 31 20 29 29 3b 0a 20 20 53 69 6d 75 6c 61 t=1 ));. Simula
e1350 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 teDiskfullError(
e1360 28 20 77 72 6f 74 65 3d 30 2c 20 61 6d 74 3d 31 ( wrote=0, amt=1
e1370 20 29 29 3b 0a 20 20 69 66 28 20 61 6d 74 3e 30 ));. if( amt>0
e1380 20 29 7b 0a 20 20 20 20 69 66 28 20 77 72 6f 74 ){. if( wrot
e1390 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 e<0 ){. /*
e13a0 6c 61 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79 lastErrno set by
e13b0 20 73 65 65 6b 41 6e 64 57 72 69 74 65 20 2a 2f seekAndWrite */
e13c0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
e13d0 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 LITE_IOERR_WRITE
e13e0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
e13f0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
e1400 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 d)->lastErrno =
e1410 30 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 0; /* not a syst
e1420 65 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 em error */.
e1430 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e1440 46 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a FULL;. }. }.
e1450 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e1460 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 OK;.}..#ifdef SQ
e1470 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 LITE_TEST./*.**
e1480 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 Count the number
e1490 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e of fullsyncs an
e14a0 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 d normal syncs.
e14b0 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f This is used to
e14c0 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 test.** that sy
e14d0 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 ncs and fullsync
e14e0 73 20 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20 s are occurring
e14f0 61 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d at the right tim
e1500 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 es..*/.SQLITE_AP
e1510 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 I int sqlite3_sy
e1520 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 nc_count = 0;.SQ
e1530 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
e1540 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f ite3_fullsync_co
e1550 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a unt = 0;.#endif.
e1560 0a 2f 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 ./*.** Use the f
e1570 64 61 74 61 73 79 6e 63 28 29 20 41 50 49 20 6f datasync() API o
e1580 6e 6c 79 20 69 66 20 74 68 65 20 48 41 56 45 5f nly if the HAVE_
e1590 46 44 41 54 41 53 59 4e 43 20 6d 61 63 72 6f 20 FDATASYNC macro
e15a0 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 4f is defined..** O
e15b0 74 68 65 72 77 69 73 65 20 75 73 65 20 66 73 79 therwise use fsy
e15c0 6e 63 28 29 20 69 6e 20 69 74 73 20 70 6c 61 63 nc() in its plac
e15d0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 48 41 e..*/.#ifndef HA
e15e0 56 45 5f 46 44 41 54 41 53 59 4e 43 0a 23 20 64 VE_FDATASYNC.# d
e15f0 65 66 69 6e 65 20 66 64 61 74 61 73 79 6e 63 20 efine fdatasync
e1600 66 73 79 6e 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a fsync.#endif../*
e1610 0a 2a 2a 20 44 65 66 69 6e 65 20 48 41 56 45 5f .** Define HAVE_
e1620 46 55 4c 4c 46 53 59 4e 43 20 74 6f 20 30 20 6f FULLFSYNC to 0 o
e1630 72 20 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e r 1 depending on
e1640 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a whether or not.
e1650 2a 2a 20 74 68 65 20 46 5f 46 55 4c 4c 46 53 59 ** the F_FULLFSY
e1660 4e 43 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 NC macro is defi
e1670 6e 65 64 2e 20 20 46 5f 46 55 4c 4c 46 53 59 4e ned. F_FULLFSYN
e1680 43 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a C is currently.*
e1690 2a 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 * only available
e16a0 20 6f 6e 20 4d 61 63 20 4f 53 20 58 2e 20 20 42 on Mac OS X. B
e16b0 75 74 20 74 68 61 74 20 63 6f 75 6c 64 20 63 68 ut that could ch
e16c0 61 6e 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ange..*/.#ifdef
e16d0 46 5f 46 55 4c 4c 46 53 59 4e 43 0a 23 20 64 65 F_FULLFSYNC.# de
e16e0 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 fine HAVE_FULLFS
e16f0 59 4e 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 YNC 1.#else.# de
e1700 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 fine HAVE_FULLFS
e1710 59 4e 43 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f YNC 0.#endif.../
e1720 2a 0a 2a 2a 20 54 68 65 20 66 73 79 6e 63 28 29 *.** The fsync()
e1730 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 64 6f 65 system call doe
e1740 73 20 6e 6f 74 20 77 6f 72 6b 20 61 73 20 61 64 s not work as ad
e1750 76 65 72 74 69 73 65 64 20 6f 6e 20 6d 61 6e 79 vertised on many
e1760 0a 2a 2a 20 75 6e 69 78 20 73 79 73 74 65 6d 73 .** unix systems
e1770 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
e1780 20 70 72 6f 63 65 64 75 72 65 20 69 73 20 61 6e procedure is an
e1790 20 61 74 74 65 6d 70 74 20 74 6f 20 6d 61 6b 65 attempt to make
e17a0 0a 2a 2a 20 69 74 20 77 6f 72 6b 20 62 65 74 74 .** it work bett
e17b0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 er..**.** The SQ
e17c0 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 LITE_NO_SYNC mac
e17d0 72 6f 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 ro disables all
e17e0 66 73 79 6e 63 28 29 73 2e 20 20 54 68 69 73 20 fsync()s. This
e17f0 69 73 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 is useful.** for
e1800 20 74 65 73 74 69 6e 67 20 77 68 65 6e 20 77 65 testing when we
e1810 20 77 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 72 want to run thr
e1820 6f 75 67 68 20 74 68 65 20 74 65 73 74 20 73 75 ough the test su
e1830 69 74 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 ite quickly..**
e1840 59 6f 75 20 61 72 65 20 73 74 72 6f 6e 67 6c 79 You are strongly
e1850 20 61 64 76 69 73 65 64 20 2a 6e 6f 74 2a 20 74 advised *not* t
e1860 6f 20 64 65 70 6c 6f 79 20 77 69 74 68 20 53 51 o deploy with SQ
e1870 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 LITE_NO_SYNC.**
e1880 65 6e 61 62 6c 65 64 2c 20 68 6f 77 65 76 65 72 enabled, however
e1890 2c 20 73 69 6e 63 65 20 77 69 74 68 20 53 51 4c , since with SQL
e18a0 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 ITE_NO_SYNC enab
e18b0 6c 65 64 2c 20 61 6e 20 4f 53 20 63 72 61 73 68 led, an OS crash
e18c0 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 .** or power fai
e18d0 6c 75 72 65 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 lure will likely
e18e0 20 63 6f 72 72 75 70 74 20 74 68 65 20 64 61 74 corrupt the dat
e18f0 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 abase file..*/.s
e1900 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66 tatic int full_f
e1910 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74 sync(int fd, int
e1920 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64 fullSync, int d
e1930 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20 ataOnly){. int
e1940 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f rc;.. /* The fo
e1950 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65 llowing "ifdef/e
e1960 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b lif/else/" block
e1970 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74 has the same st
e1980 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20 ructure as. **
e1990 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49 the one below. I
e19a0 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20 t is replicated
e19b0 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61 here solely to a
e19c0 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20 void cluttering
e19d0 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61 . ** up the rea
e19e0 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20 l code with the
e19f0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e1a00 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a () macros.. */.
e1a10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f #ifdef SQLITE_NO
e1a20 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 _SYNC. UNUSED_P
e1a30 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20 ARAMETER(fd);.
e1a40 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e1a50 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e (fullSync);. UN
e1a60 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 USED_PARAMETER(d
e1a70 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20 ataOnly);.#elif
e1a80 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20 HAVE_FULLFSYNC.
e1a90 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e1aa0 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c R(dataOnly);.#el
e1ab0 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 se. UNUSED_PARA
e1ac0 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b METER(fullSync);
e1ad0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 .#endif.. /* Re
e1ae0 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 cord the number
e1af0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 of times that we
e1b00 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 do a normal fsy
e1b10 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 nc() and . ** F
e1b20 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 ULLSYNC. This i
e1b30 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 s used during te
e1b40 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 sting to verify
e1b50 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 that this proced
e1b60 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 ure. ** gets ca
e1b70 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f lled with the co
e1b80 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e rrect arguments.
e1b90 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
e1ba0 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 ITE_TEST. if( f
e1bb0 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 ullSync ) sqlite
e1bc0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 3_fullsync_count
e1bd0 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 ++;. sqlite3_sy
e1be0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 nc_count++;.#end
e1bf0 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 if.. /* If we c
e1c00 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
e1c10 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 SQLITE_NO_SYNC
e1c20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 flag, then synci
e1c30 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d ng is a. ** no-
e1c40 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 op. */.#ifdef S
e1c50 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 QLITE_NO_SYNC.
e1c60 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
e1c70 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 #elif HAVE_FULLF
e1c80 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 SYNC. if( fullS
e1c90 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ync ){. rc =
e1ca0 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c fcntl(fd, F_FULL
e1cb0 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c FSYNC, 0);. }el
e1cc0 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a se{. rc = 1;.
e1cd0 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }. /* If the
e1ce0 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 FULLFSYNC failed
e1cf0 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 , fall back to a
e1d00 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 ttempting an fsy
e1d10 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 nc().. ** It sh
e1d20 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 ouldn't be possi
e1d30 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e ble for fullfsyn
e1d40 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 c to fail on the
e1d50 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c local . ** fil
e1d60 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 e system (on OSX
e1d70 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e ), so failure in
e1d80 64 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c dicates that FUL
e1d90 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 LFSYNC. ** isn'
e1da0 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 t supported for
e1db0 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d this file system
e1dc0 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e . So, attempt an
e1dd0 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 fsync . ** and
e1de0 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 (for now) ignor
e1df0 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f e the overhead o
e1e00 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 f a superfluous
e1e10 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 fcntl call. .
e1e20 2a 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65 ** It'd be bette
e1e30 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c r to detect full
e1e40 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e fsync support on
e1e50 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 ce and avoid .
e1e60 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c ** the fcntl cal
e1e70 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e l every time syn
e1e80 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a c is called.. *
e1e90 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 63 20 /. if( rc ) rc
e1ea0 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 = fsync(fd);..#e
e1eb0 6c 73 65 20 0a 20 20 69 66 28 20 64 61 74 61 4f lse . if( dataO
e1ec0 6e 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 nly ){. rc =
e1ed0 66 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23 fdatasync(fd);.#
e1ee0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
e1ef0 20 20 69 66 28 20 72 63 3d 3d 2d 31 20 26 26 20 if( rc==-1 &&
e1f00 65 72 72 6e 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 errno==ENOTSUP )
e1f10 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 73 79 {. rc = fsy
e1f20 6e 63 28 66 64 29 3b 0a 20 20 20 20 7d 0a 23 65 nc(fd);. }.#e
e1f30 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ndif. }else{.
e1f40 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 rc = fsync(fd)
e1f50 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
e1f60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f ifdef SQLITE_NO_
e1f70 53 59 4e 43 20 65 6c 69 66 20 48 41 56 45 5f 46 SYNC elif HAVE_F
e1f80 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a 0a 20 20 69 ULLFSYNC */.. i
e1f90 66 28 20 4f 53 5f 56 58 57 4f 52 4b 53 20 26 26 f( OS_VXWORKS &&
e1fa0 20 72 63 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 rc!= -1 ){.
e1fb0 72 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 rc = 0;. }. re
e1fc0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
e1fd0 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 * Make sure all
e1fe0 77 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 writes to a part
e1ff0 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 icular file are
e2000 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 committed to dis
e2010 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 61 74 61 k..**.** If data
e2020 4f 6e 6c 79 3d 3d 30 20 74 68 65 6e 20 62 6f 74 Only==0 then bot
e2030 68 20 74 68 65 20 66 69 6c 65 20 69 74 73 65 6c h the file itsel
e2040 66 20 61 6e 64 20 69 74 73 20 6d 65 74 61 64 61 f and its metada
e2050 74 61 20 28 66 69 6c 65 0a 2a 2a 20 73 69 7a 65 ta (file.** size
e2060 2c 20 61 63 63 65 73 73 20 74 69 6d 65 2c 20 65 , access time, e
e2070 74 63 29 20 61 72 65 20 73 79 6e 63 65 64 2e 20 tc) are synced.
e2080 20 49 66 20 64 61 74 61 4f 6e 6c 79 21 3d 30 20 If dataOnly!=0
e2090 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a then only the.**
e20a0 20 66 69 6c 65 20 64 61 74 61 20 69 73 20 73 79 file data is sy
e20b0 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e 64 65 nced..**.** Unde
e20c0 72 20 55 6e 69 78 2c 20 61 6c 73 6f 20 6d 61 6b r Unix, also mak
e20d0 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 e sure that the
e20e0 64 69 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 directory entry
e20f0 66 6f 72 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 for the file.**
e2100 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 has been created
e2110 20 62 79 20 66 73 79 6e 63 2d 69 6e 67 20 74 68 by fsync-ing th
e2120 65 20 64 69 72 65 63 74 6f 72 79 20 74 68 61 74 e directory that
e2130 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 69 contains the fi
e2140 6c 65 2e 0a 2a 2a 20 49 66 20 77 65 20 64 6f 20 le..** If we do
e2150 6e 6f 74 20 64 6f 20 74 68 69 73 20 61 6e 64 20 not do this and
e2160 77 65 20 65 6e 63 6f 75 6e 74 65 72 20 61 20 70 we encounter a p
e2170 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68 ower failure, th
e2180 65 20 64 69 72 65 63 74 6f 72 79 0a 2a 2a 20 65 e directory.** e
e2190 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6a 6f 75 ntry for the jou
e21a0 72 6e 61 6c 20 6d 69 67 68 74 20 6e 6f 74 20 65 rnal might not e
e21b0 78 69 73 74 20 61 66 74 65 72 20 77 65 20 72 65 xist after we re
e21c0 62 6f 6f 74 2e 20 20 54 68 65 20 6e 65 78 74 0a boot. The next.
e21d0 2a 2a 20 53 51 4c 69 74 65 20 74 6f 20 61 63 63 ** SQLite to acc
e21e0 65 73 73 20 74 68 65 20 66 69 6c 65 20 77 69 6c ess the file wil
e21f0 6c 20 6e 6f 74 20 6b 6e 6f 77 20 74 68 61 74 20 l not know that
e2200 74 68 65 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 the journal exis
e2210 74 73 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 ts (because.** t
e2220 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e 74 he directory ent
e2230 72 79 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e ry for the journ
e2240 61 6c 20 77 61 73 20 6e 65 76 65 72 20 63 72 65 al was never cre
e2250 61 74 65 64 29 20 61 6e 64 20 74 68 65 20 74 72 ated) and the tr
e2260 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c ansaction.** wil
e2270 6c 20 6e 6f 74 20 72 6f 6c 6c 20 62 61 63 6b 20 l not roll back
e2280 2d 20 70 6f 73 73 69 62 6c 79 20 6c 65 61 64 69 - possibly leadi
e2290 6e 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 63 ng to database c
e22a0 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 orruption..*/.st
e22b0 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 79 6e atic int unixSyn
e22c0 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a c(sqlite3_file *
e22d0 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a id, int flags){.
e22e0 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 69 78 int rc;. unix
e22f0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
e2300 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 nixFile*)id;..
e2310 69 6e 74 20 69 73 44 61 74 61 4f 6e 6c 79 20 3d int isDataOnly =
e2320 20 28 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 53 (flags&SQLITE_S
e2330 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 29 3b 0a 20 YNC_DATAONLY);.
e2340 20 69 6e 74 20 69 73 46 75 6c 6c 73 79 6e 63 20 int isFullsync
e2350 3d 20 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d = (flags&0x0F)==
e2360 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c SQLITE_SYNC_FULL
e2370 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 ;.. /* Check th
e2380 61 74 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 at one of SQLITE
e2390 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 _SYNC_NORMAL or
e23a0 46 55 4c 4c 20 77 61 73 20 70 61 73 73 65 64 20 FULL was passed
e23b0 2a 2f 0a 20 20 61 73 73 65 72 74 28 28 66 6c 61 */. assert((fla
e23c0 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 gs&0x0F)==SQLITE
e23d0 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 0a 20 20 20 _SYNC_NORMAL.
e23e0 20 20 20 7c 7c 20 28 66 6c 61 67 73 26 30 78 30 || (flags&0x0
e23f0 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f F)==SQLITE_SYNC_
e2400 46 55 4c 4c 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 FULL. );.. /*
e2410 55 6e 69 78 20 63 61 6e 6e 6f 74 2c 20 62 75 74 Unix cannot, but
e2420 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 6d 61 some systems ma
e2430 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f y return SQLITE_
e2440 46 55 4c 4c 20 66 72 6f 6d 20 68 65 72 65 2e 20 FULL from here.
e2450 54 68 69 73 0a 20 20 2a 2a 20 6c 69 6e 65 20 69 This. ** line i
e2460 73 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 64 s to test that d
e2470 6f 69 6e 67 20 73 6f 20 64 6f 65 73 20 6e 6f 74 oing so does not
e2480 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f 62 6c cause any probl
e2490 65 6d 73 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 ems.. */. Simu
e24a0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
e24b0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
e24c0 5f 46 55 4c 4c 20 29 3b 0a 0a 20 20 61 73 73 65 _FULL );.. asse
e24d0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f rt( pFile );. O
e24e0 53 54 52 41 43 45 32 28 22 53 59 4e 43 20 20 20 STRACE2("SYNC
e24f0 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d %-3d\n", pFile-
e2500 3e 68 29 3b 0a 20 20 72 63 20 3d 20 66 75 6c 6c >h);. rc = full
e2510 5f 66 73 79 6e 63 28 70 46 69 6c 65 2d 3e 68 2c _fsync(pFile->h,
e2520 20 69 73 46 75 6c 6c 73 79 6e 63 2c 20 69 73 44 isFullsync, isD
e2530 61 74 61 4f 6e 6c 79 29 3b 0a 20 20 53 69 6d 75 ataOnly);. Simu
e2540 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d lateIOError( rc=
e2550 31 20 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 1 );. if( rc ){
e2560 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
e2570 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
e2580 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e2590 5f 49 4f 45 52 52 5f 46 53 59 4e 43 3b 0a 20 20 _IOERR_FSYNC;.
e25a0 7d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 64 }. if( pFile->d
e25b0 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 69 irfd>=0 ){. i
e25c0 6e 74 20 65 72 72 3b 0a 20 20 20 20 4f 53 54 52 nt err;. OSTR
e25d0 41 43 45 34 28 22 44 49 52 53 59 4e 43 20 25 2d ACE4("DIRSYNC %-
e25e0 33 64 20 28 68 61 76 65 5f 66 75 6c 6c 66 73 79 3d (have_fullfsy
e25f0 6e 63 3d 25 64 20 66 75 6c 6c 73 79 6e 63 3d 25 nc=%d fullsync=%
e2600 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 64 69 d)\n", pFile->di
e2610 72 66 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rfd,.
e2620 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 2c HAVE_FULLFSYNC,
e2630 20 69 73 46 75 6c 6c 73 79 6e 63 29 3b 0a 23 69 isFullsync);.#i
e2640 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 fndef SQLITE_DIS
e2650 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a 20 20 20 ABLE_DIRSYNC.
e2660 20 2f 2a 20 54 68 65 20 64 69 72 65 63 74 6f 72 /* The director
e2670 79 20 73 79 6e 63 20 69 73 20 6f 6e 6c 79 20 61 y sync is only a
e2680 74 74 65 6d 70 74 65 64 20 69 66 20 66 75 6c 6c ttempted if full
e2690 5f 66 73 79 6e 63 20 69 73 0a 20 20 20 20 2a 2a _fsync is. **
e26a0 20 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 75 turned off or u
e26b0 6e 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 navailable. If
e26c0 61 20 66 75 6c 6c 5f 66 73 79 6e 63 20 6f 63 63 a full_fsync occ
e26d0 75 72 72 65 64 20 61 62 6f 76 65 2c 0a 20 20 20 urred above,.
e26e0 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 64 69 72 ** then the dir
e26f0 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 73 ectory sync is s
e2700 75 70 65 72 66 6c 75 6f 75 73 2e 0a 20 20 20 20 uperfluous..
e2710 2a 2f 0a 20 20 20 20 69 66 28 20 28 21 48 41 56 */. if( (!HAV
e2720 45 5f 46 55 4c 4c 46 53 59 4e 43 20 7c 7c 20 21 E_FULLFSYNC || !
e2730 69 73 46 75 6c 6c 73 79 6e 63 29 20 26 26 20 66 isFullsync) && f
e2740 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65 2d ull_fsync(pFile-
e2750 3e 64 69 72 66 64 2c 30 2c 30 29 20 29 7b 0a 20 >dirfd,0,0) ){.
e2760 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 /*.
e2770 2a 2a 20 57 65 20 68 61 76 65 20 72 65 63 65 69 ** We have recei
e2780 76 65 64 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 ved multiple rep
e2790 6f 72 74 73 20 6f 66 20 66 73 79 6e 63 28 29 20 orts of fsync()
e27a0 72 65 74 75 72 6e 69 6e 67 0a 20 20 20 20 20 20 returning.
e27b0 20 2a 2a 20 65 72 72 6f 72 73 20 77 68 65 6e 20 ** errors when
e27c0 61 70 70 6c 69 65 64 20 74 6f 20 64 69 72 65 63 applied to direc
e27d0 74 6f 72 69 65 73 20 6f 6e 20 63 65 72 74 61 69 tories on certai
e27e0 6e 20 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 0a n file systems..
e27f0 20 20 20 20 20 20 20 2a 2a 20 41 20 66 61 69 6c ** A fail
e2800 65 64 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e ed directory syn
e2810 63 20 69 73 20 6e 6f 74 20 61 20 62 69 67 20 64 c is not a big d
e2820 65 61 6c 2e 20 20 53 6f 20 69 74 20 73 65 65 6d eal. So it seem
e2830 73 0a 20 20 20 20 20 20 20 2a 2a 20 62 65 74 74 s. ** bett
e2840 65 72 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 65 er to ignore the
e2850 20 65 72 72 6f 72 2e 20 20 54 69 63 6b 65 74 20 error. Ticket
e2860 23 31 36 35 37 0a 20 20 20 20 20 20 20 2a 2f 0a #1657. */.
e2870 20 20 20 20 20 20 20 2f 2a 20 70 46 69 6c 65 2d /* pFile-
e2880 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
e2890 6e 6f 3b 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a no; */. /*
e28a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
e28b0 4f 45 52 52 3b 20 2a 2f 0a 20 20 20 20 7d 0a 23 OERR; */. }.#
e28c0 65 6e 64 69 66 0a 20 20 20 20 65 72 72 20 3d 20 endif. err =
e28d0 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 close(pFile->dir
e28e0 66 64 29 3b 20 2f 2a 20 4f 6e 6c 79 20 6e 65 65 fd); /* Only nee
e28f0 64 20 74 6f 20 73 79 6e 63 20 6f 6e 63 65 2c 20 d to sync once,
e2900 73 6f 20 63 6c 6f 73 65 20 74 68 65 20 2a 2f 0a so close the */.
e2910 20 20 20 20 69 66 28 20 65 72 72 3d 3d 30 20 29 if( err==0 )
e2920 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
e2930 2a 20 64 69 72 65 63 74 6f 72 79 20 77 68 65 6e * directory when
e2940 20 77 65 20 61 72 65 20 64 6f 6e 65 20 2a 2f 0a we are done */.
e2950 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 69 72 pFile->dir
e2960 66 64 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c fd = -1;. }el
e2970 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d se{. pFile-
e2980 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
e2990 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 no;. rc = S
e29a0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f QLITE_IOERR_DIR_
e29b0 43 4c 4f 53 45 3b 0a 20 20 20 20 7d 0a 20 20 7d CLOSE;. }. }
e29c0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
e29d0 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 ./*.** Truncate
e29e0 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 an open file to
e29f0 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 a specified size
e2a00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
e2a10 6e 69 78 54 72 75 6e 63 61 74 65 28 73 71 6c 69 nixTruncate(sqli
e2a20 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 te3_file *id, i6
e2a30 34 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 4 nByte){. int
e2a40 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 rc;. assert( id
e2a50 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f );. SimulateIO
e2a60 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
e2a70 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 LITE_IOERR_TRUNC
e2a80 41 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 66 74 ATE );. rc = ft
e2a90 72 75 6e 63 61 74 65 28 28 28 75 6e 69 78 46 69 runcate(((unixFi
e2aa0 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 6f 66 66 le*)id)->h, (off
e2ab0 5f 74 29 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 _t)nByte);. if(
e2ac0 20 72 63 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 rc ){. ((uni
e2ad0 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 xFile*)id)->last
e2ae0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
e2af0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e2b00 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b _IOERR_TRUNCATE;
e2b10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
e2b20 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
e2b30 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 }.}../*.** Det
e2b40 65 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 ermine the curre
e2b50 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c nt size of a fil
e2b60 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 e in bytes.*/.st
e2b70 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c atic int unixFil
e2b80 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 eSize(sqlite3_fi
e2b90 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 le *id, i64 *pSi
e2ba0 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ze){. int rc;.
e2bb0 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66 struct stat buf
e2bc0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29 ;. assert( id )
e2bd0 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28 28 ;. rc = fstat((
e2be0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
e2bf0 68 2c 20 26 62 75 66 29 3b 0a 20 20 53 69 6d 75 h, &buf);. Simu
e2c00 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d lateIOError( rc=
e2c10 31 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 1 );. if( rc!=0
e2c20 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 ){. ((unixFi
e2c30 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 le*)id)->lastErr
e2c40 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
e2c50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
e2c60 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 ERR_FSTAT;. }.
e2c70 20 2a 70 53 69 7a 65 20 3d 20 62 75 66 2e 73 74 *pSize = buf.st
e2c80 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 57 68 65 _size;.. /* Whe
e2c90 6e 20 6f 70 65 6e 69 6e 67 20 61 20 7a 65 72 6f n opening a zero
e2ca0 2d 73 69 7a 65 20 64 61 74 61 62 61 73 65 2c 20 -size database,
e2cb0 74 68 65 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f the findLockInfo
e2cc0 28 29 20 70 72 6f 63 65 64 75 72 65 0a 20 20 2a () procedure. *
e2cd0 2a 20 77 72 69 74 65 73 20 61 20 73 69 6e 67 6c * writes a singl
e2ce0 65 20 62 79 74 65 20 69 6e 74 6f 20 74 68 61 74 e byte into that
e2cf0 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 file in order t
e2d00 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 61 20 o work around a
e2d10 62 75 67 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 bug. ** in the
e2d20 4f 53 2d 58 20 6d 73 64 6f 73 20 66 69 6c 65 73 OS-X msdos files
e2d30 79 73 74 65 6d 2e 20 20 49 6e 20 6f 72 64 65 72 ystem. In order
e2d40 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 to avoid proble
e2d50 6d 73 20 77 69 74 68 20 75 70 70 65 72 0a 20 20 ms with upper.
e2d60 2a 2a 20 6c 61 79 65 72 73 2c 20 77 65 20 6e 65 ** layers, we ne
e2d70 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68 69 ed to report thi
e2d80 73 20 66 69 6c 65 20 73 69 7a 65 20 61 73 20 7a s file size as z
e2d90 65 72 6f 20 65 76 65 6e 20 74 68 6f 75 67 68 20 ero even though
e2da0 69 74 20 69 73 0a 20 20 2a 2a 20 72 65 61 6c 6c it is. ** reall
e2db0 79 20 31 2e 20 20 20 54 69 63 6b 65 74 20 23 33 y 1. Ticket #3
e2dc0 32 36 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 260.. */. if(
e2dd0 2a 70 53 69 7a 65 3d 3d 31 20 29 20 2a 70 53 69 *pSize==1 ) *pSi
e2de0 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20 72 65 74 75 ze = 0;... retu
e2df0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
e2e00 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
e2e10 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e2e20 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 && defined(__AP
e2e30 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 48 61 6e PLE__)./*.** Han
e2e40 64 6c 65 72 20 66 6f 72 20 70 72 6f 78 79 2d 6c dler for proxy-l
e2e50 6f 63 6b 69 6e 67 20 66 69 6c 65 2d 63 6f 6e 74 ocking file-cont
e2e60 72 6f 6c 20 76 65 72 62 73 2e 20 20 44 65 66 69 rol verbs. Defi
e2e70 6e 65 64 20 62 65 6c 6f 77 20 69 6e 20 74 68 65 ned below in the
e2e80 0a 2a 2a 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63 .** proxying loc
e2e90 6b 69 6e 67 20 64 69 76 69 73 69 6f 6e 2e 0a 2a king division..*
e2ea0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
e2eb0 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 xyFileControl(sq
e2ec0 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c lite3_file*,int,
e2ed0 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a void*);.#endif..
e2ee0 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69 ./*.** Informati
e2ef0 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 6f on and control o
e2f00 66 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 68 f an open file h
e2f10 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 andle..*/.static
e2f20 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 43 6f 6e int unixFileCon
e2f30 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c trol(sqlite3_fil
e2f40 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 e *id, int op, v
e2f50 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 oid *pArg){. sw
e2f60 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 itch( op ){.
e2f70 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 case SQLITE_FCNT
e2f80 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 L_LOCKSTATE: {.
e2f90 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 *(int*)pArg
e2fa0 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 = ((unixFile*)i
e2fb0 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 d)->locktype;.
e2fc0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e2fd0 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 E_OK;. }.
e2fe0 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 case SQLITE_LAST
e2ff0 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 _ERRNO: {.
e3000 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 *(int*)pArg = ((
e3010 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c unixFile*)id)->l
e3020 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 astErrno;.
e3030 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e3040 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
e3050 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 54 68 NDEBUG. /* Th
e3060 65 20 70 61 67 65 72 20 63 61 6c 6c 73 20 74 68 e pager calls th
e3070 69 73 20 6d 65 74 68 6f 64 20 74 6f 20 73 69 67 is method to sig
e3080 6e 61 6c 20 74 68 61 74 20 69 74 20 68 61 73 20 nal that it has
e3090 64 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 20 72 6f done. ** a ro
e30a0 6c 6c 62 61 63 6b 20 61 6e 64 20 74 68 61 74 20 llback and that
e30b0 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
e30c0 74 68 65 72 65 66 6f 72 65 20 75 6e 63 68 61 6e therefore unchan
e30d0 67 65 64 20 61 6e 64 0a 20 20 20 20 2a 2a 20 69 ged and. ** i
e30e0 74 20 68 65 6e 63 65 20 69 74 20 69 73 20 4f 4b t hence it is OK
e30f0 20 66 6f 72 20 74 68 65 20 74 72 61 6e 73 61 63 for the transac
e3100 74 69 6f 6e 20 63 68 61 6e 67 65 20 63 6f 75 6e tion change coun
e3110 74 65 72 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a ter to be. **
e3120 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 20 20 unchanged..
e3130 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 */. case SQLI
e3140 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 TE_FCNTL_DB_UNCH
e3150 41 4e 47 45 44 3a 20 7b 0a 20 20 20 20 20 20 28 ANGED: {. (
e3160 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
e3170 64 62 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 dbUpdate = 0;.
e3180 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e3190 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 E_OK;. }.#end
e31a0 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e if.#if SQLITE_EN
e31b0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
e31c0 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f LE && defined(__
e31d0 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 63 61 73 APPLE__). cas
e31e0 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 e SQLITE_SET_LOC
e31f0 4b 50 52 4f 58 59 46 49 4c 45 3a 0a 20 20 20 20 KPROXYFILE:.
e3200 63 61 73 65 20 53 51 4c 49 54 45 5f 47 45 54 5f case SQLITE_GET_
e3210 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b LOCKPROXYFILE: {
e3220 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 72 . return pr
e3230 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 oxyFileControl(i
e3240 64 2c 6f 70 2c 70 41 72 67 29 3b 0a 20 20 20 20 d,op,pArg);.
e3250 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
e3260 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
e3270 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e G_STYLE && defin
e3280 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 2a 2f ed(__APPLE__) */
e3290 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
e32a0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f LITE_ERROR;.}../
e32b0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
e32c0 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 sector size in b
e32d0 79 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 ytes of the unde
e32e0 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 rlying block dev
e32f0 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 ice for.** the s
e3300 70 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 pecified file. T
e3310 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c his is almost al
e3320 77 61 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 ways 512 bytes,
e3330 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 but may be.** la
e3340 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 rger for some de
e3350 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c vices..**.** SQL
e3360 69 74 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 ite code assumes
e3370 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 this function c
e3380 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 annot fail. It a
e3390 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 lso assumes that
e33a0 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 .** if two files
e33b0 20 61 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 are created in
e33c0 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 the same file-sy
e33d0 73 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 stem directory (
e33e0 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 i.e..** a databa
e33f0 73 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e se and its journ
e3400 61 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 al file) that th
e3410 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 e sector size wi
e3420 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d ll be the.** sam
e3430 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 e for both..*/.s
e3440 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 65 tatic int unixSe
e3450 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 ctorSize(sqlite3
e3460 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 29 7b _file *NotUsed){
e3470 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
e3480 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
e3490 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 45 return SQLITE_DE
e34a0 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a FAULT_SECTOR_SIZ
e34b0 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 E;.}../*.** Retu
e34c0 72 6e 20 74 68 65 20 64 65 76 69 63 65 20 63 68 rn the device ch
e34d0 61 72 61 63 74 65 72 69 73 74 69 63 73 20 66 6f aracteristics fo
e34e0 72 20 74 68 65 20 66 69 6c 65 2e 20 54 68 69 73 r the file. This
e34f0 20 69 73 20 61 6c 77 61 79 73 20 30 20 66 6f 72 is always 0 for
e3500 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 unix..*/.static
e3510 20 69 6e 74 20 75 6e 69 78 44 65 76 69 63 65 43 int unixDeviceC
e3520 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 haracteristics(s
e3530 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 qlite3_file *Not
e3540 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
e3550 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e3560 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a d);. return 0;.
e3570 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 65 6e }../*.** Here en
e3580 64 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 ds the implement
e3590 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 71 6c ation of all sql
e35a0 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 ite3_file method
e35b0 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s..**.**********
e35c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
e35d0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 4d 65 sqlite3_file Me
e35e0 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a thods **********
e35f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3600 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
e3610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3650 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ****/../*.** Thi
e3660 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 s division conta
e3670 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 ins definitions
e3680 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 of sqlite3_io_me
e3690 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 74 68 thods objects th
e36a0 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 at.** implement
e36b0 76 61 72 69 6f 75 73 20 66 69 6c 65 20 6c 6f 63 various file loc
e36c0 6b 69 6e 67 20 73 74 72 61 74 65 67 69 65 73 2e king strategies.
e36d0 20 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 It also contai
e36e0 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a ns definitions.*
e36f0 2a 20 6f 66 20 22 66 69 6e 64 65 72 22 20 66 75 * of "finder" fu
e3700 6e 63 74 69 6f 6e 73 2e 20 20 41 20 66 69 6e 64 nctions. A find
e3710 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 er-function is u
e3720 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 sed to locate th
e3730 65 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a e appropriate.**
e3740 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e3750 6f 64 73 20 6f 62 6a 65 63 74 20 66 6f 72 20 61 ods object for a
e3760 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 particular data
e3770 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 base file. The
e3780 70 41 70 70 44 61 74 61 0a 2a 2a 20 66 69 65 6c pAppData.** fiel
e3790 64 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 d of the sqlite3
e37a0 5f 76 66 73 20 56 46 53 20 6f 62 6a 65 63 74 73 _vfs VFS objects
e37b0 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 are initialized
e37c0 20 74 6f 20 62 65 20 70 6f 69 6e 74 65 72 73 20 to be pointers
e37d0 74 6f 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 to.** the correc
e37e0 74 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f t finder-functio
e37f0 6e 20 66 6f 72 20 74 68 61 74 20 56 46 53 2e 0a n for that VFS..
e3800 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65 **.** Most finde
e3810 72 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 r functions retu
e3820 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
e3830 61 20 66 69 78 65 64 20 73 71 6c 69 74 65 33 5f a fixed sqlite3_
e3840 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 io_methods.** ob
e3850 6a 65 63 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 ject. The only
e3860 69 6e 74 65 72 65 73 74 69 6e 67 20 66 69 6e 64 interesting find
e3870 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 er-function is a
e3880 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c utolockIoFinder,
e3890 20 77 68 69 63 68 0a 2a 2a 20 6c 6f 6f 6b 73 20 which.** looks
e38a0 61 74 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 at the filesyste
e38b0 6d 20 74 79 70 65 20 61 6e 64 20 74 72 69 65 73 m type and tries
e38c0 20 74 6f 20 67 75 65 73 73 20 74 68 65 20 62 65 to guess the be
e38d0 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 st locking.** st
e38e0 72 61 74 65 67 79 20 66 72 6f 6d 20 74 68 61 74 rategy from that
e38f0 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 69 6e 64 ..**.** For find
e3900 65 72 2d 66 75 6e 74 69 6f 6e 20 46 2c 20 74 77 er-funtion F, tw
e3910 6f 20 6f 62 6a 65 63 74 73 20 61 72 65 20 63 72 o objects are cr
e3920 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 eated:.**.**
e3930 28 31 29 20 54 68 65 20 72 65 61 6c 20 66 69 6e (1) The real fin
e3940 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 6e 61 6d der-function nam
e3950 65 64 20 22 46 49 6d 70 74 28 29 22 2e 0a 2a 2a ed "FImpt()"..**
e3960 0a 2a 2a 20 20 20 20 28 32 29 20 41 20 63 6f 6e .** (2) A con
e3970 73 74 61 6e 74 20 70 6f 69 6e 74 65 72 20 74 6f stant pointer to
e3980 20 74 68 69 73 20 66 75 6e 63 74 69 6f 20 6e 61 this functio na
e3990 6d 65 64 20 6a 75 73 74 20 22 46 22 2e 0a 2a 2a med just "F"..**
e39a0 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 .**.** A pointer
e39b0 20 74 6f 20 74 68 65 20 46 20 70 6f 69 6e 74 65 to the F pointe
e39c0 72 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 r is used as the
e39d0 20 70 41 70 70 44 61 74 61 20 76 61 6c 75 65 20 pAppData value
e39e0 66 6f 72 20 56 46 53 0a 2a 2a 20 6f 62 6a 65 63 for VFS.** objec
e39f0 74 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 ts. We have to
e3a00 64 6f 20 74 68 69 73 20 69 6e 73 74 65 61 64 20 do this instead
e3a10 6f 66 20 6c 65 74 74 69 6e 67 20 70 41 70 70 44 of letting pAppD
e3a20 61 74 61 20 70 6f 69 6e 74 0a 2a 2a 20 64 69 72 ata point.** dir
e3a30 65 63 74 6c 79 20 61 74 20 74 68 65 20 66 69 6e ectly at the fin
e3a40 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 73 69 6e der-function sin
e3a50 63 65 20 43 39 30 20 72 75 6c 65 73 20 70 72 65 ce C90 rules pre
e3a60 76 65 6e 74 20 61 20 76 6f 69 64 2a 0a 2a 2a 20 vent a void*.**
e3a70 66 72 6f 6d 20 62 65 20 63 61 73 74 20 69 6e 74 from be cast int
e3a80 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 o a function poi
e3a90 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 45 nter..**.**.** E
e3aa0 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 ach instance of
e3ab0 74 68 69 73 20 6d 61 63 72 6f 20 67 65 6e 65 72 this macro gener
e3ac0 61 74 65 73 20 74 77 6f 20 6f 62 6a 65 63 74 73 ates two objects
e3ad0 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 20 63 :.**.** * A c
e3ae0 6f 6e 73 74 61 6e 74 20 73 71 6c 69 74 65 33 5f onstant sqlite3_
e3af0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 io_methods objec
e3b00 74 20 63 61 6c 6c 20 4d 45 54 48 4f 44 20 74 68 t call METHOD th
e3b10 61 74 20 68 61 73 20 6c 6f 63 6b 69 6e 67 0a 2a at has locking.*
e3b20 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 43 * methods C
e3b30 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f LOSE, LOCK, UNLO
e3b40 43 4b 2c 20 43 4b 52 45 53 4c 4f 43 4b 2e 0a 2a CK, CKRESLOCK..*
e3b50 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 49 2f 4f *.** * An I/O
e3b60 20 6d 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66 method finder f
e3b70 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65 64 20 46 unction called F
e3b80 49 4e 44 45 52 20 74 68 61 74 20 72 65 74 75 72 INDER that retur
e3b90 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 ns a pointer.**
e3ba0 20 20 20 20 20 74 6f 20 74 68 65 20 4d 45 54 48 to the METH
e3bb0 4f 44 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 OD object in the
e3bc0 20 70 72 65 76 69 6f 75 73 20 62 75 6c 6c 65 74 previous bullet
e3bd0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 49 4f 4d ..*/.#define IOM
e3be0 45 54 48 4f 44 53 28 46 49 4e 44 45 52 2c 20 4d ETHODS(FINDER, M
e3bf0 45 54 48 4f 44 2c 20 43 4c 4f 53 45 2c 20 4c 4f ETHOD, CLOSE, LO
e3c00 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 4c 4f CK, UNLOCK, CKLO
e3c10 43 4b 29 20 20 20 20 20 20 20 20 20 20 20 20 20 CK)
e3c20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 \.static const
e3c30 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e3c40 6f 64 73 20 4d 45 54 48 4f 44 20 3d 20 7b 20 20 ods METHOD = {
e3c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3c70 20 5c 0a 20 20 20 31 2c 20 20 20 20 20 20 20 20 \. 1,
e3c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3c90 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f /* iVersion */
e3ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3cc0 5c 0a 20 20 20 43 4c 4f 53 45 2c 20 20 20 20 20 \. CLOSE,
e3cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3ce0 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 20 20 20 /* xClose */
e3cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e3d10 0a 20 20 20 75 6e 69 78 52 65 61 64 2c 20 20 20 . unixRead,
e3d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3d30 2f 2a 20 78 52 65 61 64 20 2a 2f 20 20 20 20 20 /* xRead */
e3d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e3d60 20 20 20 75 6e 69 78 57 72 69 74 65 2c 20 20 20 unixWrite,
e3d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e3d80 2a 20 78 57 72 69 74 65 20 2a 2f 20 20 20 20 20 * xWrite */
e3d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3da0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e3db0 20 20 75 6e 69 78 54 72 75 6e 63 61 74 65 2c 20 unixTruncate,
e3dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e3dd0 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 20 20 20 xTruncate */
e3de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3df0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e3e00 20 75 6e 69 78 53 79 6e 63 2c 20 20 20 20 20 20 unixSync,
e3e10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e3e20 78 53 79 6e 63 20 2a 2f 20 20 20 20 20 20 20 20 xSync */
e3e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3e40 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
e3e50 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 20 20 20 unixFileSize,
e3e60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e3e70 46 69 6c 65 53 69 7a 65 20 2a 2f 20 20 20 20 20 FileSize */
e3e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3e90 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 4c \. L
e3ea0 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 OCK,
e3eb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c /* xL
e3ec0 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ock */
e3ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3ee0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 55 4e \. UN
e3ef0 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 LOCK,
e3f00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e /* xUn
e3f10 6c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 lock */
e3f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3f30 20 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4b 4c \. CKL
e3f40 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 OCK,
e3f50 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 68 65 /* xChe
e3f60 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 2a ckReservedLock *
e3f70 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e3f80 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 \. unix
e3f90 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 FileControl,
e3fa0 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 /* xFile
e3fb0 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 20 20 20 20 Control */
e3fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3fd0 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 \. unixS
e3fe0 65 63 74 6f 72 53 69 7a 65 2c 20 20 20 20 20 20 ectorSize,
e3ff0 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f /* xSecto
e4000 72 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20 rSize */
e4010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4020 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 44 65 \. unixDe
e4030 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
e4040 69 63 73 20 20 20 2f 2a 20 78 44 65 76 69 63 65 ics /* xDevice
e4050 43 61 70 61 62 69 6c 69 74 69 65 73 20 2a 2f 20 Capabilities */
e4060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4070 20 20 20 20 5c 0a 7d 3b 20 20 20 20 20 20 20 20 \.};
e4080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e40a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e40b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e40c0 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 \.static cons
e40d0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e40e0 68 6f 64 73 20 2a 46 49 4e 44 45 52 23 23 49 6d hods *FINDER##Im
e40f0 70 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a pl(const char *z
e4100 2c 20 69 6e 74 20 68 29 7b 20 20 20 20 20 20 20 , int h){
e4110 20 20 5c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 \. UNUSED_PAR
e4120 41 4d 45 54 45 52 28 7a 29 3b 20 55 4e 55 53 45 AMETER(z); UNUSE
e4130 44 5f 50 41 52 41 4d 45 54 45 52 28 68 29 3b 20 D_PARAMETER(h);
e4140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4160 20 5c 0a 20 20 72 65 74 75 72 6e 20 26 4d 45 54 \. return &MET
e4170 48 4f 44 3b 20 20 20 20 20 20 20 20 20 20 20 20 HOD;
e4180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e41a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e41b0 5c 0a 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 \.}
e41c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e41d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e41e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e41f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e4200 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 .static const sq
e4210 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
e4220 20 2a 28 2a 63 6f 6e 73 74 20 46 49 4e 44 45 52 *(*const FINDER
e4230 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e )(const char*,in
e4240 74 29 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a t) \.
e4250 20 20 20 20 3d 20 46 49 4e 44 45 52 23 23 49 6d = FINDER##Im
e4260 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 pl;../*.** Here
e4270 61 72 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 73 are all of the s
e4280 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e4290 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 65 61 s objects for ea
e42a0 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 ch of the.** loc
e42b0 6b 69 6e 67 20 73 74 72 61 74 65 67 69 65 73 2e king strategies.
e42c0 20 20 46 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 Functions that
e42d0 20 72 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 73 return pointers
e42e0 20 74 6f 20 74 68 65 73 65 20 6d 65 74 68 6f 64 to these method
e42f0 73 0a 2a 2a 20 61 72 65 20 61 6c 73 6f 20 63 72 s.** are also cr
e4300 65 61 74 65 64 2e 0a 2a 2f 0a 49 4f 4d 45 54 48 eated..*/.IOMETH
e4310 4f 44 53 28 0a 20 20 70 6f 73 69 78 49 6f 46 69 ODS(. posixIoFi
e4320 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 nder,
e4330 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 /* Finder funct
e4340 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 6f ion name */. po
e4350 73 69 78 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 sixIoMethods,
e4360 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 /* sqlit
e4370 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
e4380 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 ject name */. u
e4390 6e 69 78 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 nixClose,
e43a0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f /* xClo
e43b0 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 se method */. u
e43c0 6e 69 78 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 nixLock,
e43d0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
e43e0 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e k method */. un
e43f0 69 78 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 ixUnlock,
e4400 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f /* xUnlo
e4410 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 ck method */. u
e4420 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 nixCheckReserved
e4430 4c 6f 63 6b 20 20 20 20 20 2f 2a 20 78 43 68 65 Lock /* xChe
e4440 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d ckReservedLock m
e4450 65 74 68 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 ethod */.).IOMET
e4460 48 4f 44 53 28 0a 20 20 6e 6f 6c 6f 63 6b 49 6f HODS(. nolockIo
e4470 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 Finder,
e4480 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 /* Finder func
e4490 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e tion name */. n
e44a0 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 olockIoMethods,
e44b0 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 /* sqli
e44c0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f te3_io_methods o
e44d0 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 bject name */.
e44e0 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 nolockClose,
e44f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c /* xCl
e4500 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ose method */.
e4510 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 nolockLock,
e4520 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f /* xLo
e4530 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e ck method */. n
e4540 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 olockUnlock,
e4550 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c /* xUnl
e4560 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ock method */.
e4570 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 nolockCheckReser
e4580 76 65 64 4c 6f 63 6b 20 20 20 2f 2a 20 78 43 68 vedLock /* xCh
e4590 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
e45a0 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 method */.).IOME
e45b0 54 48 4f 44 53 28 0a 20 20 64 6f 74 6c 6f 63 6b THODS(. dotlock
e45c0 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 IoFinder,
e45d0 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e /* Finder fun
e45e0 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 ction name */.
e45f0 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 dotlockIoMethods
e4600 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c , /* sql
e4610 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
e4620 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 object name */.
e4630 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 dotlockClose,
e4640 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 /* xC
e4650 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 lose method */.
e4660 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 dotlockLock,
e4670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c /* xL
e4680 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ock method */.
e4690 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 dotlockUnlock,
e46a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e /* xUn
e46b0 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 lock method */.
e46c0 20 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 dotlockCheckRes
e46d0 65 72 76 65 64 4c 6f 63 6b 20 20 2f 2a 20 78 43 ervedLock /* xC
e46e0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
e46f0 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 0a 23 69 method */.)..#i
e4700 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
e4710 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f LOCKING_STYLE.IO
e4720 4d 45 54 48 4f 44 53 28 0a 20 20 66 6c 6f 63 6b METHODS(. flock
e4730 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 IoFinder,
e4740 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 /* Finder f
e4750 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
e4760 20 20 66 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 flockIoMethods
e4770 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 , /* s
e4780 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e4790 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f s object name */
e47a0 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 . flockClose,
e47b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e47c0 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f xClose method */
e47d0 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 . flockLock,
e47e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e47f0 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
e4800 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 flockUnlock,
e4810 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e4820 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f Unlock method */
e4830 0a 20 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73 . flockCheckRes
e4840 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a 20 ervedLock /*
e4850 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
e4860 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 ck method */.).#
e4870 65 6e 64 69 66 0a 0a 23 69 66 20 4f 53 5f 56 58 endif..#if OS_VX
e4880 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f 44 53 28 WORKS.IOMETHODS(
e4890 0a 20 20 73 65 6d 49 6f 46 69 6e 64 65 72 2c 20 . semIoFinder,
e48a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e48b0 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 Finder function
e48c0 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 6d 49 6f 4d name */. semIoM
e48d0 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 ethods,
e48e0 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 /* sqlite3_i
e48f0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
e4900 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 6d 43 6c name */. semCl
e4910 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ose,
e4920 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d /* xClose m
e4930 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d 4c 6f ethod */. semLo
e4940 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
e4950 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 /* xLock me
e4960 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d 55 6e 6c thod */. semUnl
e4970 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
e4980 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d /* xUnlock m
e4990 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d 43 68 ethod */. semCh
e49a0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
e49b0 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 /* xCheckRe
e49c0 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f servedLock metho
e49d0 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 d */.).#endif..#
e49e0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 if defined(__APP
e49f0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f LE__) && SQLITE_
e4a00 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
e4a10 54 59 4c 45 0a 49 4f 4d 45 54 48 4f 44 53 28 0a TYLE.IOMETHODS(.
e4a20 20 20 61 66 70 49 6f 46 69 6e 64 65 72 2c 20 20 afpIoFinder,
e4a30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
e4a40 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e inder function n
e4a50 61 6d 65 20 2a 2f 0a 20 20 61 66 70 49 6f 4d 65 ame */. afpIoMe
e4a60 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 thods,
e4a70 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f /* sqlite3_io
e4a80 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 _methods object
e4a90 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 43 6c 6f name */. afpClo
e4aa0 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 se,
e4ab0 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 /* xClose me
e4ac0 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 4c 6f 63 thod */. afpLoc
e4ad0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
e4ae0 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 /* xLock met
e4af0 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55 6e 6c 6f hod */. afpUnlo
e4b00 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
e4b10 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 /* xUnlock me
e4b20 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 43 68 65 thod */. afpChe
e4b30 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 ckReservedLock
e4b40 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 /* xCheckRes
e4b50 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 ervedLock method
e4b60 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a */.).#endif../*
e4b70 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f .** The proxy lo
e4b80 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 69 73 20 cking method is
e4b90 61 20 22 73 75 70 65 72 2d 6d 65 74 68 6f 64 22 a "super-method"
e4ba0 20 69 6e 20 74 68 65 20 73 65 6e 73 65 20 74 68 in the sense th
e4bb0 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73 at it.** opens s
e4bc0 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 20 64 65 econdary file de
e4bd0 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20 74 68 scriptors for th
e4be0 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b e conch and lock
e4bf0 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74 files and.** it
e4c00 20 75 73 65 73 20 70 72 6f 78 79 2c 20 64 6f 74 uses proxy, dot
e4c10 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20 -file, AFP, and
e4c20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 flock() locking
e4c30 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65 methods on those
e4c40 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79 20 66 69 .** secondary fi
e4c50 6c 65 73 2e 20 20 46 6f 72 20 74 68 69 73 20 72 les. For this r
e4c60 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 eason, the divis
e4c70 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ion that impleme
e4c80 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 nts.** proxy loc
e4c90 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74 65 64 20 king is located
e4ca0 6d 75 63 68 20 66 75 72 74 68 65 72 20 64 6f 77 much further dow
e4cb0 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 n in the file.
e4cc0 42 75 74 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74 But we need.** t
e4cd0 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 64 o go ahead and d
e4ce0 65 66 69 6e 65 20 74 68 65 20 73 71 6c 69 74 65 efine the sqlite
e4cf0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 3_io_methods and
e4d00 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e finder function
e4d10 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f .** for proxy lo
e4d20 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 53 6f 20 cking here. So
e4d30 77 65 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 we forward decla
e4d40 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f re the I/O metho
e4d50 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e ds..*/.#if defin
e4d60 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 ed(__APPLE__) &&
e4d70 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
e4d80 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 OCKING_STYLE.sta
e4d90 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f tic int proxyClo
e4da0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a se(sqlite3_file*
e4db0 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 );.static int pr
e4dc0 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f oxyLock(sqlite3_
e4dd0 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 file*, int);.sta
e4de0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c tic int proxyUnl
e4df0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
e4e00 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 *, int);.static
e4e10 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65 int proxyCheckRe
e4e20 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
e4e30 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b e3_file*, int*);
e4e40 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 72 .IOMETHODS(. pr
e4e50 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 oxyIoFinder,
e4e60 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 /* Finde
e4e70 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 r function name
e4e80 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d 65 74 68 */. proxyIoMeth
e4e90 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f ods, /
e4ea0 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 * sqlite3_io_met
e4eb0 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hods object name
e4ec0 20 2a 2f 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 */. proxyClose
e4ed0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e4ee0 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
e4ef0 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c */. proxyLock,
e4f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4f10 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
e4f20 2a 2f 0a 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b */. proxyUnlock
e4f30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e4f40 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
e4f50 20 2a 2f 0a 20 20 70 72 6f 78 79 43 68 65 63 6b */. proxyCheck
e4f60 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 ReservedLock
e4f70 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
e4f80 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
e4f90 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64 ).#endif...#if d
e4fa0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
e4fb0 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 ) && SQLITE_ENAB
e4fc0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e4fd0 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 ./* .** This "fi
e4fe0 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 nder" function a
e4ff0 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 ttempts to deter
e5000 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f mine the best lo
e5010 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a cking strategy .
e5020 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 ** for the datab
e5030 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 ase file "filePa
e5040 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 th". It then re
e5050 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 turns the sqlite
e5060 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 3_io_methods.**
e5070 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c object that impl
e5080 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 ements that stra
e5090 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tegy..**.** This
e50a0 20 69 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f is for MacOSX o
e50b0 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 nly..*/.static c
e50c0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
e50d0 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 methods *autoloc
e50e0 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 kIoFinderImpl(.
e50f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c const char *fil
e5100 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d ePath, /* nam
e5110 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
e5120 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 e file */. int
e5130 66 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 fd
e5140 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 64 65 73 /* file des
e5150 63 72 69 70 74 6f 72 20 6f 70 65 6e 20 6f 6e 20 criptor open on
e5160 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
e5170 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 e */.){. static
e5180 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 4d 61 const struct Ma
e5190 70 70 69 6e 67 20 7b 0a 20 20 20 20 63 6f 6e 73 pping {. cons
e51a0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 73 79 73 t char *zFilesys
e51b0 74 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 tem;
e51c0 20 20 2f 2a 20 46 69 6c 65 73 79 73 74 65 6d 20 /* Filesystem
e51d0 74 79 70 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 type name */.
e51e0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e51f0 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 o_methods *pMeth
e5200 6f 64 73 3b 20 20 20 2f 2a 20 41 70 70 72 6f 70 ods; /* Approp
e5210 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 riate locking me
e5220 74 68 6f 64 20 2a 2f 0a 20 20 7d 20 61 4d 61 70 thod */. } aMap
e5230 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66 [] = {. { "hf
e5240 73 22 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d s", &posixIoM
e5250 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 ethods },. {
e5260 22 75 66 73 22 2c 20 20 20 20 26 70 6f 73 69 78 "ufs", &posix
e5270 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 IoMethods },.
e5280 20 7b 20 22 61 66 70 66 73 22 2c 20 20 26 61 66 { "afpfs", &af
e5290 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 69 pIoMethods },.#i
e52a0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
e52b0 4c 45 5f 41 46 50 5f 4c 4f 43 4b 49 4e 47 5f 53 LE_AFP_LOCKING_S
e52c0 4d 42 0a 20 20 20 20 7b 20 22 73 6d 62 66 73 22 MB. { "smbfs"
e52d0 2c 20 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 , &afpIoMethods
e52e0 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 20 20 7b 20 },.#else. {
e52f0 22 73 6d 62 66 73 22 2c 20 20 26 66 6c 6f 63 6b "smbfs", &flock
e5300 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 65 6e IoMethods },.#en
e5310 64 69 66 0a 20 20 20 20 7b 20 22 77 65 62 64 61 dif. { "webda
e5320 76 22 2c 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 v", &nolockIoMet
e5330 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 30 2c hods },. { 0,
e5340 20 30 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 0 }. };. int
e5350 69 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 i;. struct stat
e5360 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 73 74 72 fs fsInfo;. str
e5370 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e uct flock lockIn
e5380 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 69 6c 65 fo;.. if( !file
e5390 50 61 74 68 20 29 7b 0a 20 20 20 20 2f 2a 20 49 Path ){. /* I
e53a0 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c f filePath==NULL
e53b0 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65 20 61 that means we a
e53c0 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 re dealing with
e53d0 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 a transient file
e53e0 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 64 6f 65 . ** that doe
e53f0 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 s not need to be
e5400 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 locked. */.
e5410 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f return &nolockIo
e5420 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 20 20 69 Methods;. }. i
e5430 66 28 20 73 74 61 74 66 73 28 66 69 6c 65 50 61 f( statfs(filePa
e5440 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 21 3d 20 th, &fsInfo) !=
e5450 2d 31 20 29 7b 0a 20 20 20 20 69 66 28 20 66 73 -1 ){. if( fs
e5460 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 20 26 20 4d Info.f_flags & M
e5470 4e 54 5f 52 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 NT_RDONLY ){.
e5480 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 return &noloc
e5490 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20 kIoMethods;.
e54a0 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 }. for(i=0; a
e54b0 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 Map[i].zFilesyst
e54c0 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 em; i++){.
e54d0 69 66 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66 if( strcmp(fsInf
e54e0 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 o.f_fstypename,
e54f0 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 aMap[i].zFilesys
e5500 74 65 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 tem)==0 ){.
e5510 20 20 20 72 65 74 75 72 6e 20 61 4d 61 70 5b 69 return aMap[i
e5520 5d 2e 70 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20 ].pMethods;.
e5530 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
e5540 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 61 73 65 /* Default case
e5550 2e 20 48 61 6e 64 6c 65 73 2c 20 61 6d 6f 6e 67 . Handles, among
e5560 73 74 20 6f 74 68 65 72 73 2c 20 22 6e 66 73 22 st others, "nfs"
e5570 2e 0a 20 20 2a 2a 20 54 65 73 74 20 62 79 74 65 .. ** Test byte
e5580 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 75 73 69 6e -range lock usin
e5590 67 20 66 63 6e 74 6c 28 29 2e 20 49 66 20 74 68 g fcntl(). If th
e55a0 65 20 63 61 6c 6c 20 73 75 63 63 65 65 64 73 2c e call succeeds,
e55b0 20 0a 20 20 2a 2a 20 61 73 73 75 6d 65 20 74 68 . ** assume th
e55c0 61 74 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 at the file-syst
e55d0 65 6d 20 73 75 70 70 6f 72 74 73 20 50 4f 53 49 em supports POSI
e55e0 58 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e 20 0a X style locks. .
e55f0 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e */. lockInfo.
e5600 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 l_len = 1;. loc
e5610 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 kInfo.l_start =
e5620 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 0;. lockInfo.l_
e5630 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 whence = SEEK_SE
e5640 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f T;. lockInfo.l_
e5650 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a type = F_RDLCK;.
e5660 20 20 69 66 28 20 66 63 6e 74 6c 28 66 64 2c 20 if( fcntl(fd,
e5670 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e F_GETLK, &lockIn
e5680 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20 fo)!=-1 ) {.
e5690 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f 4d return &posixIoM
e56a0 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b ethods;. }else{
e56b0 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f 74 . return &dot
e56c0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 lockIoMethods;.
e56d0 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 }.}.static cons
e56e0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e56f0 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 61 75 hods *(*const au
e5700 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28 tolockIoFinder)(
e5710 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 const char*,int)
e5720 0a 20 20 20 20 20 20 20 20 3d 20 61 75 74 6f 6c . = autol
e5730 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b ockIoFinderImpl;
e5740 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 ..#endif /* defi
e5750 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 ned(__APPLE__) &
e5760 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
e5770 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f LOCKING_STYLE */
e5780 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 61 62 73 74 72 ../*.** An abstr
e5790 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20 70 act type for a p
e57a0 6f 69 6e 74 65 72 20 74 6f 20 61 20 49 4f 20 6d ointer to a IO m
e57b0 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66 75 6e ethod finder fun
e57c0 63 74 69 6f 6e 3a 0a 2a 2f 0a 74 79 70 65 64 65 ction:.*/.typede
e57d0 66 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f f const sqlite3_
e57e0 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 66 69 io_methods *(*fi
e57f0 6e 64 65 72 5f 74 79 70 65 29 28 63 6f 6e 73 74 nder_type)(const
e5800 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 0a 0a 2f char*,int);.../
e5810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e5820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e5830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e5840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e5850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
e5860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e5870 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 ********* sqlite
e5880 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 3_vfs methods **
e5890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e58a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
e58b0 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 This division c
e58c0 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c ontains the impl
e58d0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65 ementation of me
e58e0 74 68 6f 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 thods on the.**
e58f0 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 sqlite3_vfs obje
e5900 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e ct..*/../*.** In
e5910 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e itialize the con
e5920 74 65 6e 74 73 20 6f 66 20 74 68 65 20 75 6e 69 tents of the uni
e5930 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 xFile structure
e5940 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 49 pointed to by pI
e5950 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
e5960 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 fillInUnixFile(
e5970 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
e5980 70 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f pVfs, /* Po
e5990 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a inter to vfs obj
e59a0 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 ect */. int h,
e59b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e59c0 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 /* Open file de
e59d0 73 63 72 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 scriptor of file
e59e0 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f being opened */
e59f0 0a 20 20 69 6e 74 20 64 69 72 66 64 2c 20 20 20 . int dirfd,
e5a00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 /* Di
e5a10 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 rectory file des
e5a20 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c criptor */. sql
e5a30 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 2c 20 ite3_file *pId,
e5a40 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 6f /* Write to
e5a50 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 the unixFile st
e5a60 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a ructure here */.
e5a70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
e5a80 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d ilename, /* Nam
e5a90 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 62 65 e of the file be
e5aa0 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 ing opened */.
e5ab0 69 6e 74 20 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 int noLock,
e5ac0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 /* Omit
e5ad0 6c 6f 63 6b 69 6e 67 20 69 66 20 74 72 75 65 20 locking if true
e5ae0 2a 2f 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 */. int isDelet
e5af0 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e /*
e5b00 44 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 Delete on close
e5b10 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 if true */.){.
e5b20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
e5b30 5f 6d 65 74 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 _methods *pLocki
e5b40 6e 67 53 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46 ngStyle;. unixF
e5b50 69 6c 65 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69 ile *pNew = (uni
e5b60 78 46 69 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69 xFile *)pId;. i
e5b70 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
e5b80 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e K;.. assert( pN
e5b90 65 77 2d 3e 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 ew->pLock==NULL
e5ba0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 );. assert( pNe
e5bb0 77 2d 3e 70 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 w->pOpen==NULL )
e5bc0 3b 0a 0a 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 ;.. /* Paramete
e5bd0 72 20 69 73 44 65 6c 65 74 65 20 69 73 20 6f 6e r isDelete is on
e5be0 6c 79 20 75 73 65 64 20 6f 6e 20 76 78 77 6f 72 ly used on vxwor
e5bf0 6b 73 2e 0a 20 20 2a 2a 20 45 78 70 72 65 73 73 ks.. ** Express
e5c00 20 74 68 69 73 20 65 78 70 6c 69 63 69 74 6c 79 this explicitly
e5c10 20 68 65 72 65 20 74 6f 20 70 72 65 76 65 6e 74 here to prevent
e5c20 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e compiler warnin
e5c30 67 73 0a 20 20 2a 2a 20 61 62 6f 75 74 20 75 6e gs. ** about un
e5c40 75 73 65 64 20 70 61 72 61 6d 65 74 65 72 73 2e used parameters.
e5c50 0a 20 20 2a 2f 0a 23 69 66 20 21 4f 53 5f 56 58 . */.#if !OS_VX
e5c60 57 4f 52 4b 53 0a 20 20 55 4e 55 53 45 44 5f 50 WORKS. UNUSED_P
e5c70 41 52 41 4d 45 54 45 52 28 69 73 44 65 6c 65 74 ARAMETER(isDelet
e5c80 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 4f 53 e);.#endif.. OS
e5c90 54 52 41 43 45 33 28 22 4f 50 45 4e 20 20 20 20 TRACE3("OPEN
e5ca0 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c 20 7a %-3d %s\n", h, z
e5cb0 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 20 0a 20 Filename); .
e5cc0 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a 20 20 pNew->h = h;.
e5cd0 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20 64 69 pNew->dirfd = di
e5ce0 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 52 45 41 rfd;. SET_THREA
e5cf0 44 49 44 28 70 4e 65 77 29 3b 0a 0a 23 69 66 20 DID(pNew);..#if
e5d00 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 70 4e 65 OS_VXWORKS. pNe
e5d10 77 2d 3e 70 49 64 20 3d 20 76 78 77 6f 72 6b 73 w->pId = vxworks
e5d20 46 69 6e 64 46 69 6c 65 49 64 28 7a 46 69 6c 65 FindFileId(zFile
e5d30 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 name);. if( pNe
e5d40 77 2d 3e 70 49 64 3d 3d 30 20 29 7b 0a 20 20 20 w->pId==0 ){.
e5d50 20 6e 6f 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 noLock = 1;.
e5d60 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
e5d70 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a EM;. }.#endif..
e5d80 20 20 69 66 28 20 6e 6f 4c 6f 63 6b 20 29 7b 0a if( noLock ){.
e5d90 20 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c pLockingStyl
e5da0 65 20 3d 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 e = &nolockIoMet
e5db0 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 hods;. }else{.
e5dc0 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 pLockingStyle
e5dd0 20 3d 20 28 2a 2a 28 66 69 6e 64 65 72 5f 74 79 = (**(finder_ty
e5de0 70 65 2a 29 70 56 66 73 2d 3e 70 41 70 70 44 61 pe*)pVfs->pAppDa
e5df0 74 61 29 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 68 ta)(zFilename, h
e5e00 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e );.#if SQLITE_EN
e5e10 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
e5e20 4c 45 0a 20 20 20 20 2f 2a 20 43 61 63 68 65 20 LE. /* Cache
e5e30 7a 46 69 6c 65 6e 61 6d 65 20 69 6e 20 74 68 65 zFilename in the
e5e40 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
e5e50 20 28 41 46 50 20 61 6e 64 20 64 6f 74 6c 6f 63 (AFP and dotloc
e5e60 6b 20 6f 76 65 72 72 69 64 65 29 20 66 6f 72 0a k override) for.
e5e70 20 20 20 20 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b ** proxyLock
e5e80 20 61 63 74 69 76 61 74 69 6f 6e 20 69 73 20 70 activation is p
e5e90 6f 73 73 69 62 6c 65 20 28 72 65 6d 6f 74 65 20 ossible (remote
e5ea0 70 72 6f 78 79 20 69 73 20 62 61 73 65 64 20 6f proxy is based o
e5eb0 6e 20 64 62 20 6e 61 6d 65 29 0a 20 20 20 20 2a n db name). *
e5ec0 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 72 65 6d 61 * zFilename rema
e5ed0 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 ins valid until
e5ee0 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 2c 20 file is closed,
e5ef0 74 6f 20 73 75 70 70 6f 72 74 20 2a 2f 0a 20 20 to support */.
e5f00 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 pNew->lockingC
e5f10 6f 6e 74 65 78 74 20 3d 20 28 76 6f 69 64 2a 29 ontext = (void*)
e5f20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 23 65 6e 64 69 zFilename;.#endi
e5f30 66 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4c 6f f. }.. if( pLo
e5f40 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 70 ckingStyle == &p
e5f50 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 29 7b osixIoMethods ){
e5f60 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 . unixEnterMu
e5f70 74 65 78 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 tex();. rc =
e5f80 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 findLockInfo(pNe
e5f90 77 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c w, &pNew->pLock,
e5fa0 20 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a &pNew->pOpen);.
e5fb0 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 unixLeaveMut
e5fc0 65 78 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 20 53 ex();. }..#if S
e5fd0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
e5fe0 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 KING_STYLE && de
e5ff0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
e6000 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 . else if( pLoc
e6010 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 61 66 kingStyle == &af
e6020 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 pIoMethods ){.
e6030 20 20 2f 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67 /* AFP locking
e6040 20 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 uses the file p
e6050 61 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 ath so it needs
e6060 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 to be included i
e6070 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 61 66 70 n. ** the afp
e6080 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2e 0a LockingContext..
e6090 20 20 20 20 2a 2f 0a 20 20 20 20 61 66 70 4c 6f */. afpLo
e60a0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
e60b0 74 78 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f tx;. pNew->lo
e60c0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 ckingContext = p
e60d0 43 74 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 Ctx = sqlite3_ma
e60e0 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 lloc( sizeof(*pC
e60f0 74 78 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 tx) );. if( p
e6100 43 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Ctx==0 ){.
e6110 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
e6120 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 M;. }else{.
e6130 20 20 20 20 2f 2a 20 4e 42 3a 20 7a 46 69 6c 65 /* NB: zFile
e6140 6e 61 6d 65 20 65 78 69 73 74 73 20 61 6e 64 20 name exists and
e6150 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e remains valid un
e6160 74 69 6c 20 74 68 65 20 66 69 6c 65 20 69 73 20 til the file is
e6170 63 6c 6f 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 closed. **
e6180 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 72 65 71 according to req
e6190 75 69 72 65 6d 65 6e 74 20 46 31 31 31 34 31 2e uirement F11141.
e61a0 20 20 53 6f 20 77 65 20 64 6f 20 6e 6f 74 20 6e So we do not n
e61b0 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 0a 20 20 eed to make a.
e61c0 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74 ** copy of t
e61d0 68 65 20 66 69 6c 65 6e 61 6d 65 2e 20 2a 2f 0a he filename. */.
e61e0 20 20 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 pCtx->dbPa
e61f0 74 68 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a th = zFilename;.
e6200 20 20 20 20 20 20 73 72 61 6e 64 6f 6d 64 65 76 srandomdev
e6210 28 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 45 6e ();. unixEn
e6220 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 terMutex();.
e6230 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 rc = findLockI
e6240 6e 66 6f 28 70 4e 65 77 2c 20 4e 55 4c 4c 2c 20 nfo(pNew, NULL,
e6250 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 &pNew->pOpen);.
e6260 20 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 unixLeaveMu
e6270 74 65 78 28 29 3b 20 20 20 20 20 20 20 20 0a 20 tex(); .
e6280 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
e6290 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 . else if( pLoc
e62a0 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 64 6f kingStyle == &do
e62b0 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29 tlockIoMethods )
e62c0 7b 0a 20 20 20 20 2f 2a 20 44 6f 74 66 69 6c 65 {. /* Dotfile
e62d0 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 locking uses th
e62e0 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 e file path so i
e62f0 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e t needs to be in
e6300 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a cluded in. **
e6310 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b the dotlockLock
e6320 69 6e 67 43 6f 6e 74 65 78 74 20 0a 20 20 20 20 ingContext .
e6330 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4c 6f */. char *zLo
e6340 63 6b 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 20 ckFile;. int
e6350 6e 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 6e nFilename;. n
e6360 46 69 6c 65 6e 61 6d 65 20 3d 20 28 69 6e 74 29 Filename = (int)
e6370 73 74 72 6c 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 strlen(zFilename
e6380 29 20 2b 20 36 3b 0a 20 20 20 20 7a 4c 6f 63 6b ) + 6;. zLock
e6390 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 73 File = (char *)s
e63a0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 46 qlite3_malloc(nF
e63b0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 ilename);. if
e63c0 28 20 7a 4c 6f 63 6b 46 69 6c 65 3d 3d 30 20 29 ( zLockFile==0 )
e63d0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
e63e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
e63f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
e6400 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 69 te3_snprintf(nFi
e6410 6c 65 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 6c lename, zLockFil
e6420 65 2c 20 22 25 73 22 20 44 4f 54 4c 4f 43 4b 5f e, "%s" DOTLOCK_
e6430 53 55 46 46 49 58 2c 20 7a 46 69 6c 65 6e 61 6d SUFFIX, zFilenam
e6440 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e e);. }. pN
e6450 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 ew->lockingConte
e6460 78 74 20 3d 20 7a 4c 6f 63 6b 46 69 6c 65 3b 0a xt = zLockFile;.
e6470 20 20 7d 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f }..#if OS_VXWO
e6480 52 4b 53 0a 20 20 65 6c 73 65 20 69 66 28 20 70 RKS. else if( p
e6490 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 LockingStyle ==
e64a0 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73 20 29 7b &semIoMethods ){
e64b0 0a 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 73 65 . /* Named se
e64c0 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 maphore locking
e64d0 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 uses the file pa
e64e0 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 th so it needs t
e64f0 6f 20 62 65 0a 20 20 20 20 2a 2a 20 69 6e 63 6c o be. ** incl
e6500 75 64 65 64 20 69 6e 20 74 68 65 20 73 65 6d 4c uded in the semL
e6510 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 20 20 ockingContext.
e6520 20 20 2a 2f 0a 20 20 20 20 75 6e 69 78 45 6e 74 */. unixEnt
e6530 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 erMutex();. r
e6540 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f c = findLockInfo
e6550 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 4c (pNew, &pNew->pL
e6560 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 65 ock, &pNew->pOpe
e6570 6e 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 3d n);. if( (rc=
e6580 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 =SQLITE_OK) && (
e6590 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 pNew->pOpen->pSe
e65a0 6d 3d 3d 4e 55 4c 4c 29 20 29 7b 0a 20 20 20 20 m==NULL) ){.
e65b0 20 20 63 68 61 72 20 2a 7a 53 65 6d 4e 61 6d 65 char *zSemName
e65c0 20 3d 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e = pNew->pOpen->
e65d0 61 53 65 6d 4e 61 6d 65 3b 0a 20 20 20 20 20 20 aSemName;.
e65e0 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 73 71 6c int n;. sql
e65f0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 ite3_snprintf(MA
e6600 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 53 65 6d X_PATHNAME, zSem
e6610 4e 61 6d 65 2c 20 22 25 73 2e 73 65 6d 22 2c 0a Name, "%s.sem",.
e6620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6630 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 49 64 pNew->pId
e6640 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 ->zCanonicalName
e6650 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 20 6e 3d );. for( n=
e6660 30 3b 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3b 20 0; zSemName[n];
e6670 6e 2b 2b 20 29 0a 20 20 20 20 20 20 20 20 69 66 n++ ). if
e6680 28 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3d 3d 27 ( zSemName[n]=='
e6690 2f 27 20 29 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d /' ) zSemName[n]
e66a0 20 3d 20 27 5f 27 3b 0a 20 20 20 20 20 20 70 4e = '_';. pN
e66b0 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 ew->pOpen->pSem
e66c0 3d 20 73 65 6d 5f 6f 70 65 6e 28 7a 53 65 6d 4e = sem_open(zSemN
e66d0 61 6d 65 2c 20 4f 5f 43 52 45 41 54 2c 20 30 36 ame, O_CREAT, 06
e66e0 36 36 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66 66, 1);. if
e66f0 28 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 ( pNew->pOpen->p
e6700 53 65 6d 20 3d 3d 20 53 45 4d 5f 46 41 49 4c 45 Sem == SEM_FAILE
e6710 44 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 D ){. rc
e6720 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
e6730 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f pNew->pO
e6740 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b 30 5d pen->aSemName[0]
e6750 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 7d = '\0';. }
e6760 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c . }. unixL
e6770 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d eaveMutex();. }
e6780 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 70 4e 65 .#endif. . pNe
e6790 77 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 w->lastErrno = 0
e67a0 3b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 ;.#if OS_VXWORKS
e67b0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
e67c0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 69 E_OK ){. unli
e67d0 6e 6b 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 nk(zFilename);.
e67e0 20 20 20 69 73 44 65 6c 65 74 65 20 3d 20 30 3b isDelete = 0;
e67f0 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 69 73 44 . }. pNew->isD
e6800 65 6c 65 74 65 20 3d 20 69 73 44 65 6c 65 74 65 elete = isDelete
e6810 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 72 ;.#endif. if( r
e6820 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
e6830 20 20 20 20 69 66 28 20 64 69 72 66 64 3e 3d 30 if( dirfd>=0
e6840 20 29 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b ) close(dirfd);
e6850 20 2f 2a 20 73 69 6c 65 6e 74 20 6c 65 61 6b 20 /* silent leak
e6860 69 66 20 66 61 69 6c 2c 20 61 6c 72 65 61 64 79 if fail, already
e6870 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 in error */.
e6880 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 7d 65 6c close(h);. }el
e6890 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4d se{. pNew->pM
e68a0 65 74 68 6f 64 20 3d 20 70 4c 6f 63 6b 69 6e 67 ethod = pLocking
e68b0 53 74 79 6c 65 3b 0a 20 20 20 20 4f 70 65 6e 43 Style;. OpenC
e68c0 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 7d 0a ounter(+1);. }.
e68d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
e68e0 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c /*.** Open a fil
e68f0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 e descriptor to
e6900 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 63 6f the directory co
e6910 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 20 7a 46 ntaining file zF
e6920 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 66 20 73 ilename..** If s
e6930 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 46 64 20 uccessful, *pFd
e6940 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f 70 is set to the op
e6950 65 6e 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 ened file descri
e6960 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 ptor and.** SQLI
e6970 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
e6980 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f d. If an error o
e6990 63 63 75 72 73 2c 20 65 69 74 68 65 72 20 53 51 ccurs, either SQ
e69a0 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 6f 72 LITE_NOMEM.** or
e69b0 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
e69c0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
e69d0 20 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 *pFd is set to
e69e0 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 an undefined.**
e69f0 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 value..**.** If
e6a00 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
e6a10 75 72 6e 65 64 2c 20 74 68 65 20 63 61 6c 6c 65 urned, the calle
e6a20 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 r is responsible
e6a30 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a 2a 2a 20 for closing.**
e6a40 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
e6a50 74 6f 72 20 2a 70 46 64 20 75 73 69 6e 67 20 63 tor *pFd using c
e6a60 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 lose()..*/.stati
e6a70 63 20 69 6e 74 20 6f 70 65 6e 44 69 72 65 63 74 c int openDirect
e6a80 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ory(const char *
e6a90 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e 74 20 2a zFilename, int *
e6aa0 70 46 64 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a pFd){. int ii;.
e6ab0 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b 0a 20 int fd = -1;.
e6ac0 20 63 68 61 72 20 7a 44 69 72 6e 61 6d 65 5b 4d char zDirname[M
e6ad0 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b 0a AX_PATHNAME+1];.
e6ae0 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
e6af0 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 ntf(MAX_PATHNAME
e6b00 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22 25 73 22 , zDirname, "%s"
e6b10 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 , zFilename);.
e6b20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73 74 72 6c for(ii=(int)strl
e6b30 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20 69 69 en(zDirname); ii
e6b40 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d 65 5b 69 >1 && zDirname[i
e6b50 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29 3b 0a i]!='/'; ii--);.
e6b60 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0a 20 20 if( ii>0 ){.
e6b70 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 20 3d zDirname[ii] =
e6b80 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 20 3d 20 '\0';. fd =
e6b90 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c 20 4f open(zDirname, O
e6ba0 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 52 59 _RDONLY|O_BINARY
e6bb0 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 66 64 , 0);. if( fd
e6bc0 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 46 44 >=0 ){.#ifdef FD
e6bd0 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 20 20 66 _CLOEXEC. f
e6be0 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 44 cntl(fd, F_SETFD
e6bf0 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 , fcntl(fd, F_GE
e6c00 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f TFD, 0) | FD_CLO
e6c10 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a 20 20 EXEC);.#endif.
e6c20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50 OSTRACE3("OP
e6c30 45 4e 44 49 52 20 25 2d 33 64 20 25 73 5c 6e 22 ENDIR %-3d %s\n"
e6c40 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d 65 29 3b , fd, zDirname);
e6c50 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 46 . }. }. *pF
e6c60 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 75 72 6e d = fd;. return
e6c70 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 45 5f 4f (fd>=0?SQLITE_O
e6c80 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 K:SQLITE_CANTOPE
e6c90 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 N);.}../*.** Cre
e6ca0 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 ate a temporary
e6cb0 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 file name in zBu
e6cc0 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 f. zBuf must be
e6cd0 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79 allocated.** by
e6ce0 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f the calling pro
e6cf0 63 65 73 73 20 61 6e 64 20 6d 75 73 74 20 62 65 cess and must be
e6d00 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 big enough to h
e6d10 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 old at least.**
e6d20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
e6d30 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 bytes..*/.stati
e6d40 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d c int getTempnam
e6d50 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 e(int nBuf, char
e6d60 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 *zBuf){. stati
e6d70 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a c const char *az
e6d80 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 Dirs[] = {.
e6d90 30 2c 0a 20 20 20 20 20 30 2c 0a 20 20 20 20 20 0,. 0,.
e6da0 22 2f 76 61 72 2f 74 6d 70 22 2c 0a 20 20 20 20 "/var/tmp",.
e6db0 20 22 2f 75 73 72 2f 74 6d 70 22 2c 0a 20 20 20 "/usr/tmp",.
e6dc0 20 20 22 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 "/tmp",. "
e6dd0 2e 22 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 .",. };. stati
e6de0 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
e6df0 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d char zChars[] =
e6e00 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a . "abcdefghij
e6e10 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a klmnopqrstuvwxyz
e6e20 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 49 ". "ABCDEFGHI
e6e30 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 JKLMNOPQRSTUVWXY
e6e40 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 Z". "01234567
e6e50 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 89";. unsigned
e6e60 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75 int i, j;. stru
e6e70 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 63 ct stat buf;. c
e6e80 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 20 onst char *zDir
e6e90 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 49 74 27 = ".";.. /* It'
e6ea0 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 s odd to simulat
e6eb0 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 e an io-error he
e6ec0 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 re, but really t
e6ed0 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a his is just. **
e6ee0 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 using the io-er
e6ef0 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 ror infrastructu
e6f00 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 re to test that
e6f10 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 SQLite handles t
e6f20 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f his. ** functio
e6f30 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f n failing. . */
e6f40 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
e6f50 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
e6f60 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 61 7a E_IOERR );.. az
e6f70 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c 69 74 65 Dirs[0] = sqlite
e6f80 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3_temp_directory
e6f90 3b 0a 20 20 69 66 20 28 4e 55 4c 4c 20 3d 3d 20 ;. if (NULL ==
e6fa0 61 7a 44 69 72 73 5b 31 5d 29 20 7b 0a 20 20 20 azDirs[1]) {.
e6fb0 20 61 7a 44 69 72 73 5b 31 5d 20 3d 20 67 65 74 azDirs[1] = get
e6fc0 65 6e 76 28 22 54 4d 50 44 49 52 22 29 3b 0a 20 env("TMPDIR");.
e6fd0 20 7d 0a 20 20 0a 20 20 66 6f 72 28 69 3d 30 3b }. . for(i=0;
e6fe0 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44 69 72 73 i<sizeof(azDirs
e6ff0 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 72 73 5b )/sizeof(azDirs[
e7000 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 0]); i++){. i
e7010 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d 3d 30 20 f( azDirs[i]==0
e7020 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
e7030 69 66 28 20 73 74 61 74 28 61 7a 44 69 72 73 5b if( stat(azDirs[
e7040 69 5d 2c 20 26 62 75 66 29 20 29 20 63 6f 6e 74 i], &buf) ) cont
e7050 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 21 53 inue;. if( !S
e7060 5f 49 53 44 49 52 28 62 75 66 2e 73 74 5f 6d 6f _ISDIR(buf.st_mo
e7070 64 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a de) ) continue;.
e7080 20 20 20 20 69 66 28 20 61 63 63 65 73 73 28 61 if( access(a
e7090 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29 20 29 20 zDirs[i], 07) )
e70a0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 44 continue;. zD
e70b0 69 72 20 3d 20 61 7a 44 69 72 73 5b 69 5d 3b 0a ir = azDirs[i];.
e70c0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a break;. }..
e70d0 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 /* Check that
e70e0 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 the output buffe
e70f0 72 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 r is large enoug
e7100 68 20 66 6f 72 20 74 68 65 20 74 65 6d 70 6f 72 h for the tempor
e7110 61 72 79 20 66 69 6c 65 20 0a 20 20 2a 2a 20 6e ary file . ** n
e7120 61 6d 65 2e 20 49 66 20 69 74 20 69 73 20 6e 6f ame. If it is no
e7130 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 t, return SQLITE
e7140 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 _ERROR.. */. i
e7150 66 28 20 28 73 74 72 6c 65 6e 28 7a 44 69 72 29 f( (strlen(zDir)
e7160 20 2b 20 73 74 72 6c 65 6e 28 53 51 4c 49 54 45 + strlen(SQLITE
e7170 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 _TEMP_FILE_PREFI
e7180 58 29 20 2b 20 31 37 29 20 3e 3d 20 28 73 69 7a X) + 17) >= (siz
e7190 65 5f 74 29 6e 42 75 66 20 29 7b 0a 20 20 20 20 e_t)nBuf ){.
e71a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
e71b0 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a ROR;. }.. do{.
e71c0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
e71d0 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c 20 7a 42 intf(nBuf-17, zB
e71e0 75 66 2c 20 22 25 73 2f 22 53 51 4c 49 54 45 5f uf, "%s/"SQLITE_
e71f0 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 TEMP_FILE_PREFIX
e7200 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20 6a 20 3d , zDir);. j =
e7210 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 42 75 (int)strlen(zBu
e7220 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f f);. sqlite3_
e7230 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35 2c 20 26 randomness(15, &
e7240 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 20 20 66 6f zBuf[j]);. fo
e7250 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20 69 2b 2b r(i=0; i<15; i++
e7260 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 42 , j++){. zB
e7270 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 uf[j] = (char)zC
e7280 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 hars[ ((unsigned
e7290 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 char)zBuf[j])%(
e72a0 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 sizeof(zChars)-1
e72b0 29 20 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a ) ];. }. z
e72c0 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 7d 77 Buf[j] = 0;. }w
e72d0 68 69 6c 65 28 20 61 63 63 65 73 73 28 7a 42 75 hile( access(zBu
e72e0 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20 72 65 74 f,0)==0 );. ret
e72f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
e7300 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ..#if SQLITE_ENA
e7310 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
e7320 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 E && defined(__A
e7330 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 52 6f PPLE__)./*.** Ro
e7340 75 74 69 6e 65 20 74 6f 20 74 72 61 6e 73 66 6f utine to transfo
e7350 72 6d 20 61 20 75 6e 69 78 46 69 6c 65 20 69 6e rm a unixFile in
e7360 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 to a proxy-locki
e7370 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a 2a 2a 20 ng unixFile..**
e7380 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 Implementation i
e7390 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c 6f 63 6b n the proxy-lock
e73a0 20 64 69 76 69 73 69 6f 6e 2c 20 62 75 74 20 75 division, but u
e73b0 73 65 64 20 62 79 20 75 6e 69 78 4f 70 65 6e 28 sed by unixOpen(
e73c0 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 50 ).** if SQLITE_P
e73d0 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b REFER_PROXY_LOCK
e73e0 49 4e 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a ING is defined..
e73f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 */.static int pr
e7400 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 oxyTransformUnix
e7410 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 2a 2c 20 File(unixFile*,
e7420 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 const char*);.#e
e7430 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 ndif.../*.** Ope
e7440 6e 20 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68 n the file zPath
e7450 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 ..** .** Previou
e7460 73 6c 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20 sly, the SQLite
e7470 4f 53 20 6c 61 79 65 72 20 75 73 65 64 20 74 68 OS layer used th
e7480 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e ree functions in
e7490 20 70 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a place of this.*
e74a0 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 * one:.**.**
e74b0 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 sqlite3OsOpenRe
e74c0 61 64 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 adWrite();.**
e74d0 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 sqlite3OsOpenR
e74e0 65 61 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 eadOnly();.**
e74f0 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 sqlite3OsOpenE
e7500 78 63 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a xclusive();.**.*
e7510 2a 20 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f * These calls co
e7520 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 rrespond to the
e7530 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e following combin
e7540 61 74 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a ations of flags:
e7550 0a 2a 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 .**.** ReadW
e7560 72 69 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52 rite() -> (R
e7570 45 41 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 EADWRITE | CREAT
e7580 45 29 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e E).** ReadOn
e7590 6c 79 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45 ly() -> (RE
e75a0 41 44 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 ADONLY) .**
e75b0 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 OpenExclusive()
e75c0 2d 3e 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 -> (READWRITE |
e75d0 43 52 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49 CREATE | EXCLUSI
e75e0 56 45 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c VE).**.** The ol
e75f0 64 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 d OpenExclusive(
e7600 29 20 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f ) accepted a boo
e7610 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 lean argument -
e7620 22 64 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a "delFlag". If.**
e7630 20 74 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20 true, the file
e7640 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74 was configured t
e7650 6f 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c o be automatical
e7660 6c 79 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 ly deleted when
e7670 74 68 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 the.** file hand
e7680 6c 65 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 le closed. To ac
e7690 68 69 65 76 65 20 74 68 65 20 73 61 6d 65 20 65 hieve the same e
e76a0 66 66 65 63 74 20 75 73 69 6e 67 20 74 68 69 73 ffect using this
e76b0 20 6e 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 new .** interfa
e76c0 63 65 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45 ce, add the DELE
e76d0 54 45 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 TEONCLOSE flag t
e76e0 6f 20 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 o those specifie
e76f0 64 20 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 d above for .**
e7700 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e OpenExclusive().
e7710 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
e7720 6e 69 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 nixOpen(. sqlit
e7730 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
e7740 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
e7750 46 53 20 66 6f 72 20 77 68 69 63 68 20 74 68 69 FS for which thi
e7760 73 20 69 73 20 74 68 65 20 78 4f 70 65 6e 20 6d s is the xOpen m
e7770 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 ethod */. const
e7780 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 char *zPath,
e7790 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74 68 6e /* Pathn
e77a0 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 ame of file to b
e77b0 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 e opened */. sq
e77c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c lite3_file *pFil
e77d0 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 e, /* Th
e77e0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
e77f0 72 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 r to be filled i
e7800 6e 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 n */. int flags
e7810 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e7820 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 /* Input fla
e7830 67 73 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 gs to control th
e7840 65 20 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 e opening */. i
e7850 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 nt *pOutFlags
e7860 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
e7870 75 74 70 75 74 20 66 6c 61 67 73 20 72 65 74 75 utput flags retu
e7880 72 6e 65 64 20 74 6f 20 53 51 4c 69 74 65 20 63 rned to SQLite c
e7890 6f 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ore */.){. int
e78a0 66 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 fd = 0;
e78b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
e78c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72 65 le descriptor re
e78d0 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28 29 turned by open()
e78e0 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 20 */. int dirfd
e78f0 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 = -1;
e7900 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f 72 /* Director
e7910 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f y file descripto
e7920 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46 r */. int openF
e7930 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20 lags = 0;
e7940 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 /* Flags t
e7950 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28 29 o pass to open()
e7960 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 20 */. int eType
e7970 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46 46 = flags&0xFFFFFF
e7980 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 00; /* Type of
e7990 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a file to open */.
e79a0 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20 int noLock;
e79b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e79c0 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 /* True to omit
e79d0 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 locking primiti
e79e0 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 ves */. int rc
e79f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
e7a00 69 6e 74 20 69 73 45 78 63 6c 75 73 69 76 65 20 int isExclusive
e7a10 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 = (flags & SQLI
e7a20 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 TE_OPEN_EXCLUSIV
e7a30 45 29 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 E);. int isDele
e7a40 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 te = (flags
e7a50 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 & SQLITE_OPEN_DE
e7a60 4c 45 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20 LETEONCLOSE);.
e7a70 69 6e 74 20 69 73 43 72 65 61 74 65 20 20 20 20 int isCreate
e7a80 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 = (flags & SQLI
e7a90 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b TE_OPEN_CREATE);
e7aa0 0a 20 20 69 6e 74 20 69 73 52 65 61 64 6f 6e 6c . int isReadonl
e7ab0 79 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 y = (flags & S
e7ac0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
e7ad0 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 NLY);. int isRe
e7ae0 61 64 57 72 69 74 65 20 20 3d 20 28 66 6c 61 67 adWrite = (flag
e7af0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
e7b00 52 45 41 44 57 52 49 54 45 29 3b 0a 0a 20 20 2f READWRITE);.. /
e7b10 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 20 * If creating a
e7b20 6d 61 73 74 65 72 20 6f 72 20 6d 61 69 6e 2d 66 master or main-f
e7b30 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 69 ile journal, thi
e7b40 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 s function will
e7b50 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 66 69 6c 65 open. ** a file
e7b60 2d 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74 -descriptor on t
e7b70 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f 6f he directory too
e7b80 2e 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 . The first time
e7b90 20 75 6e 69 78 53 79 6e 63 28 29 0a 20 20 2a 2a unixSync(). **
e7ba0 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 64 is called the d
e7bb0 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 irectory file de
e7bc0 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 scriptor will be
e7bd0 20 66 73 79 6e 63 28 29 65 64 20 61 6e 64 20 63 fsync()ed and c
e7be0 6c 6f 73 65 28 29 64 2e 0a 20 20 2a 2f 0a 20 20 lose()d.. */.
e7bf0 69 6e 74 20 69 73 4f 70 65 6e 44 69 72 65 63 74 int isOpenDirect
e7c00 6f 72 79 20 3d 20 28 69 73 43 72 65 61 74 65 20 ory = (isCreate
e7c10 26 26 20 0a 20 20 20 20 20 20 28 65 54 79 70 65 && . (eType
e7c20 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 ==SQLITE_OPEN_MA
e7c30 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 STER_JOURNAL ||
e7c40 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
e7c50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 EN_MAIN_JOURNAL)
e7c60 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 . );.. /* If a
e7c70 72 67 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73 rgument zPath is
e7c80 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
e7c90 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
e7ca0 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70 s required to op
e7cb0 65 6e 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72 en. ** a tempor
e7cc0 61 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68 ary file. Use th
e7cd0 69 73 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f is buffer to sto
e7ce0 72 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 re the file name
e7cf0 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 in.. */. char
e7d00 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 zTmpname[MAX_PA
e7d10 54 48 4e 41 4d 45 2b 31 5d 3b 0a 20 20 63 6f 6e THNAME+1];. con
e7d20 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d st char *zName =
e7d30 20 7a 50 61 74 68 3b 0a 0a 20 20 2f 2a 20 43 68 zPath;.. /* Ch
e7d40 65 63 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e eck the followin
e7d50 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 g statements are
e7d60 20 74 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a true: . **. *
e7d70 2a 20 20 20 28 61 29 20 45 78 61 63 74 6c 79 20 * (a) Exactly
e7d80 6f 6e 65 20 6f 66 20 74 68 65 20 52 45 41 44 57 one of the READW
e7d90 52 49 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c RITE and READONL
e7da0 59 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20 Y flags must be
e7db0 73 65 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 20 set, and . **
e7dc0 20 28 62 29 20 69 66 20 43 52 45 41 54 45 20 69 (b) if CREATE i
e7dd0 73 20 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44 s set, then READ
e7de0 57 52 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 WRITE must also
e7df0 62 65 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a 2a be set, and. **
e7e00 20 20 20 28 63 29 20 69 66 20 45 58 43 4c 55 53 (c) if EXCLUS
e7e10 49 56 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e IVE is set, then
e7e20 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 CREATE must als
e7e30 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 20 o be set.. **
e7e40 20 28 64 29 20 69 66 20 44 45 4c 45 54 45 4f 4e (d) if DELETEON
e7e50 43 4c 4f 53 45 20 69 73 20 73 65 74 2c 20 74 68 CLOSE is set, th
e7e60 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 en CREATE must a
e7e70 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2f lso be set.. */
e7e80 0a 20 20 61 73 73 65 72 74 28 28 69 73 52 65 61 . assert((isRea
e7e90 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 65 donly==0 || isRe
e7ea0 61 64 57 72 69 74 65 3d 3d 30 29 20 26 26 20 28 adWrite==0) && (
e7eb0 69 73 52 65 61 64 57 72 69 74 65 20 7c 7c 20 69 isReadWrite || i
e7ec0 73 52 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 61 sReadonly));. a
e7ed0 73 73 65 72 74 28 69 73 43 72 65 61 74 65 3d 3d ssert(isCreate==
e7ee0 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 0 || isReadWrite
e7ef0 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 45 78 );. assert(isEx
e7f00 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73 clusive==0 || is
e7f10 43 72 65 61 74 65 29 3b 0a 20 20 61 73 73 65 72 Create);. asser
e7f20 74 28 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c t(isDelete==0 ||
e7f30 20 69 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f isCreate);.. /
e7f40 2a 20 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d * The main DB, m
e7f50 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 61 6e 64 ain journal, and
e7f60 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
e7f70 61 72 65 20 6e 65 76 65 72 20 61 75 74 6f 6d 61 are never automa
e7f80 74 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 64 65 6c tically. ** del
e7f90 65 74 65 64 0a 20 20 2a 2f 0a 20 20 61 73 73 65 eted. */. asse
e7fa0 72 74 28 20 65 54 79 70 65 21 3d 53 51 4c 49 54 rt( eType!=SQLIT
e7fb0 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 7c E_OPEN_MAIN_DB |
e7fc0 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b 0a 20 | !isDelete );.
e7fd0 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d assert( eType!=
e7fe0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
e7ff0 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 21 69 73 44 _JOURNAL || !isD
e8000 65 6c 65 74 65 20 29 3b 0a 20 20 61 73 73 65 72 elete );. asser
e8010 74 28 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 t( eType!=SQLITE
e8020 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 _OPEN_MASTER_JOU
e8030 52 4e 41 4c 20 7c 7c 20 21 69 73 44 65 6c 65 74 RNAL || !isDelet
e8040 65 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 e );.. /* Asser
e8050 74 20 74 68 61 74 20 74 68 65 20 75 70 70 65 72 t that the upper
e8060 20 6c 61 79 65 72 20 68 61 73 20 73 65 74 20 6f layer has set o
e8070 6e 65 20 6f 66 20 74 68 65 20 22 66 69 6c 65 2d ne of the "file-
e8080 74 79 70 65 22 20 66 6c 61 67 73 2e 20 2a 2f 0a type" flags. */.
e8090 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d assert( eType=
e80a0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 =SQLITE_OPEN_MAI
e80b0 4e 5f 44 42 20 20 20 20 20 20 7c 7c 20 65 54 79 N_DB || eTy
e80c0 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe==SQLITE_OPEN_
e80d0 54 45 4d 50 5f 44 42 20 0a 20 20 20 20 20 20 20 TEMP_DB .
e80e0 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 || eType==SQLITE
e80f0 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e _OPEN_MAIN_JOURN
e8100 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c AL || eType==SQL
e8110 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f ITE_OPEN_TEMP_JO
e8120 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c URNAL . ||
e8130 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f eType==SQLITE_O
e8140 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 PEN_SUBJOURNAL
e8150 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 || eType==SQLIT
e8160 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f E_OPEN_MASTER_JO
e8170 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c URNAL . ||
e8180 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f eType==SQLITE_O
e8190 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 PEN_TRANSIENT_DB
e81a0 0a 20 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 . );.. memset(
e81b0 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 pFile, 0, sizeof
e81c0 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 (unixFile));..
e81d0 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 if( !zName ){.
e81e0 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 assert(isDelet
e81f0 65 20 26 26 20 21 69 73 4f 70 65 6e 44 69 72 65 e && !isOpenDire
e8200 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 63 20 3d ctory);. rc =
e8210 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58 getTempname(MAX
e8220 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 7a 54 6d _PATHNAME+1, zTm
e8230 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 pname);. if(
e8240 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
e8250 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
e8260 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d ;. }. zNam
e8270 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 e = zTmpname;.
e8280 7d 0a 0a 20 20 69 66 28 20 69 73 52 65 61 64 6f }.. if( isReado
e8290 6e 6c 79 20 29 20 20 6f 70 65 6e 46 6c 61 67 73 nly ) openFlags
e82a0 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 |= O_RDONLY;.
e82b0 69 66 28 20 69 73 52 65 61 64 57 72 69 74 65 20 if( isReadWrite
e82c0 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f ) openFlags |= O
e82d0 5f 52 44 57 52 3b 0a 20 20 69 66 28 20 69 73 43 _RDWR;. if( isC
e82e0 72 65 61 74 65 20 29 20 20 20 20 6f 70 65 6e 46 reate ) openF
e82f0 6c 61 67 73 20 7c 3d 20 4f 5f 43 52 45 41 54 3b lags |= O_CREAT;
e8300 0a 20 20 69 66 28 20 69 73 45 78 63 6c 75 73 69 . if( isExclusi
e8310 76 65 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c ve ) openFlags |
e8320 3d 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f = (O_EXCL|O_NOFO
e8330 4c 4c 4f 57 29 3b 0a 20 20 6f 70 65 6e 46 6c 61 LLOW);. openFla
e8340 67 73 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 49 gs |= (O_LARGEFI
e8350 4c 45 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 LE|O_BINARY);..
e8360 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 fd = open(zName
e8370 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 69 73 44 , openFlags, isD
e8380 65 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54 elete?0600:SQLIT
e8390 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 E_DEFAULT_FILE_P
e83a0 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 4f ERMISSIONS);. O
e83b0 53 54 52 41 43 45 34 28 22 4f 50 45 4e 58 20 20 STRACE4("OPENX
e83c0 20 25 2d 33 64 20 25 73 20 30 25 6f 5c 6e 22 2c %-3d %s 0%o\n",
e83d0 20 66 64 2c 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e fd, zName, open
e83e0 46 6c 61 67 73 29 3b 0a 20 20 69 66 28 20 66 64 Flags);. if( fd
e83f0 3c 30 20 26 26 20 65 72 72 6e 6f 21 3d 45 49 53 <0 && errno!=EIS
e8400 44 49 52 20 26 26 20 69 73 52 65 61 64 57 72 69 DIR && isReadWri
e8410 74 65 20 26 26 20 21 69 73 45 78 63 6c 75 73 69 te && !isExclusi
e8420 76 65 20 29 7b 0a 20 20 20 20 2f 2a 20 46 61 69 ve ){. /* Fai
e8430 6c 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 led to open the
e8440 66 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72 file for read/wr
e8450 69 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20 ite access. Try
e8460 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 read-only. */.
e8470 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53 51 4c flags &= ~(SQL
e8480 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
e8490 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 TE|SQLITE_OPEN_C
e84a0 52 45 41 54 45 29 3b 0a 20 20 20 20 66 6c 61 67 REATE);. flag
e84b0 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e s |= SQLITE_OPEN
e84c0 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 72 _READONLY;. r
e84d0 65 74 75 72 6e 20 75 6e 69 78 4f 70 65 6e 28 70 eturn unixOpen(p
e84e0 56 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c Vfs, zPath, pFil
e84f0 65 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c e, flags, pOutFl
e8500 61 67 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ags);. }. if(
e8510 66 64 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 fd<0 ){. retu
e8520 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 rn SQLITE_CANTOP
e8530 45 4e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 EN;. }. if( is
e8540 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 20 4f 53 Delete ){.#if OS
e8550 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 7a 50 61 _VXWORKS. zPa
e8560 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 65 6c 73 th = zName;.#els
e8570 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e 61 e. unlink(zNa
e8580 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a me);.#endif. }.
e8590 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
e85a0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
e85b0 20 20 65 6c 73 65 7b 0a 20 20 20 20 28 28 75 6e else{. ((un
e85c0 69 78 46 69 6c 65 2a 29 70 46 69 6c 65 29 2d 3e ixFile*)pFile)->
e85d0 6f 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e openFlags = open
e85e0 46 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 Flags;. }.#endi
e85f0 66 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 f. if( pOutFlag
e8600 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c s ){. *pOutFl
e8610 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d ags = flags;. }
e8620 0a 0a 20 20 61 73 73 65 72 74 28 66 64 21 3d 30 .. assert(fd!=0
e8630 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44 );. if( isOpenD
e8640 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 irectory ){.
e8650 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f rc = openDirecto
e8660 72 79 28 7a 50 61 74 68 2c 20 26 64 69 72 66 64 ry(zPath, &dirfd
e8670 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
e8680 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
e8690 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 close(fd); /*
e86a0 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66 silently leak if
e86b0 20 66 61 69 6c 2c 20 61 6c 72 65 61 64 79 20 69 fail, already i
e86c0 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 n error */.
e86d0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
e86e0 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 44 }. }..#ifdef FD
e86f0 5f 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c _CLOEXEC. fcntl
e8700 28 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 (fd, F_SETFD, fc
e8710 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c ntl(fd, F_GETFD,
e8720 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 0) | FD_CLOEXEC
e8730 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c );.#endif.. noL
e8740 6f 63 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 4c ock = eType!=SQL
e8750 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
e8760 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 52 ;..#if SQLITE_PR
e8770 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 EFER_PROXY_LOCKI
e8780 4e 47 0a 20 20 69 66 28 20 7a 50 61 74 68 21 3d NG. if( zPath!=
e8790 4e 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b 20 NULL && !noLock
e87a0 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 65 6e 76 ){. char *env
e87b0 66 6f 72 63 65 20 3d 20 67 65 74 65 6e 76 28 22 force = getenv("
e87c0 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f SQLITE_FORCE_PRO
e87d0 58 59 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 XY_LOCKING");.
e87e0 20 20 69 6e 74 20 75 73 65 50 72 6f 78 79 20 3d int useProxy =
e87f0 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 0;.. /* SQLI
e8800 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c TE_FORCE_PROXY_L
e8810 4f 43 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 OCKING==1 means
e8820 66 6f 72 63 65 20 61 6c 77 61 79 73 20 75 73 65 force always use
e8830 20 70 72 6f 78 79 2c 20 0a 20 20 20 20 2a 2a 20 proxy, . **
e8840 30 20 6d 65 61 6e 73 20 6e 65 76 65 72 20 75 73 0 means never us
e8850 65 20 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 e proxy, NULL me
e8860 61 6e 73 20 75 73 65 20 70 72 6f 78 79 20 66 6f ans use proxy fo
e8870 72 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 r non-local file
e8880 73 20 6f 6e 6c 79 0a 20 20 20 20 2a 2f 0a 20 20 s only. */.
e8890 20 20 69 66 28 20 65 6e 76 66 6f 72 63 65 21 3d if( envforce!=
e88a0 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 75 73 NULL ){. us
e88b0 65 50 72 6f 78 79 20 3d 20 61 74 6f 69 28 65 6e eProxy = atoi(en
e88c0 76 66 6f 72 63 65 29 3e 30 3b 0a 20 20 20 20 7d vforce)>0;. }
e88d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 72 75 else{. stru
e88e0 63 74 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f ct statfs fsInfo
e88f0 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 73 74 61 ;.. if( sta
e8900 74 66 73 28 7a 50 61 74 68 2c 20 26 66 73 49 6e tfs(zPath, &fsIn
e8910 66 6f 29 20 3d 3d 20 2d 31 20 29 7b 0a 09 09 09 fo) == -1 ){....
e8920 09 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 .((unixFile*)pFi
e8930 6c 65 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d le)->lastErrno =
e8940 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 errno;.
e8950 69 66 28 20 64 69 72 66 64 3e 3d 30 20 29 20 63 if( dirfd>=0 ) c
e8960 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f 2a 20 lose(dirfd); /*
e8970 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66 silently leak if
e8980 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f 72 20 fail, in error
e8990 2a 2f 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65 */. close
e89a0 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c (fd); /* silentl
e89b0 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 y leak if fail,
e89c0 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 in error */.
e89d0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e89e0 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 3b 0a E_IOERR_ACCESS;.
e89f0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 73 }. us
e8a00 65 50 72 6f 78 79 20 3d 20 21 28 66 73 49 6e 66 eProxy = !(fsInf
e8a10 6f 2e 66 5f 66 6c 61 67 73 26 4d 4e 54 5f 4c 4f o.f_flags&MNT_LO
e8a20 43 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 CAL);. }.
e8a30 69 66 28 20 75 73 65 50 72 6f 78 79 20 29 7b 0a if( useProxy ){.
e8a40 20 20 20 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 rc = fillI
e8a50 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 nUnixFile(pVfs,
e8a60 66 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 fd, dirfd, pFile
e8a70 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c , zPath, noLock,
e8a80 20 69 73 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 isDelete);.
e8a90 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
e8aa0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
e8ab0 63 20 3d 20 70 72 6f 78 79 54 72 61 6e 73 66 6f c = proxyTransfo
e8ac0 72 6d 55 6e 69 78 46 69 6c 65 28 28 75 6e 69 78 rmUnixFile((unix
e8ad0 46 69 6c 65 2a 29 70 46 69 6c 65 2c 20 22 3a 61 File*)pFile, ":a
e8ae0 75 74 6f 3a 22 29 3b 0a 20 20 20 20 20 20 7d 0a uto:");. }.
e8af0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
e8b00 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
e8b10 66 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 66 69 f. . return fi
e8b20 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 llInUnixFile(pVf
e8b30 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 70 46 s, fd, dirfd, pF
e8b40 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f ile, zPath, noLo
e8b50 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b 0a 7d ck, isDelete);.}
e8b60 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 ../*.** Delete t
e8b70 68 65 20 66 69 6c 65 20 61 74 20 7a 50 61 74 68 he file at zPath
e8b80 2e 20 49 66 20 74 68 65 20 64 69 72 53 79 6e 63 . If the dirSync
e8b90 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 argument is tru
e8ba0 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a 20 74 68 e, fsync().** th
e8bb0 65 20 64 69 72 65 63 74 6f 72 79 20 61 66 74 65 e directory afte
e8bc0 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 66 r deleting the f
e8bd0 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
e8be0 6e 74 20 75 6e 69 78 44 65 6c 65 74 65 28 0a 20 nt unixDelete(.
e8bf0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f sqlite3_vfs *No
e8c00 74 55 73 65 64 2c 20 20 20 20 20 2f 2a 20 56 46 tUsed, /* VF
e8c10 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 S containing thi
e8c20 73 20 61 73 20 74 68 65 20 78 44 65 6c 65 74 65 s as the xDelete
e8c30 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e method */. con
e8c40 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
e8c50 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
e8c60 66 20 66 69 6c 65 20 74 6f 20 62 65 20 64 65 6c f file to be del
e8c70 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 eted */. int di
e8c80 72 53 79 6e 63 20 20 20 20 20 20 20 20 20 20 20 rSync
e8c90 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 /* If true,
e8ca0 66 73 79 6e 63 28 29 20 64 69 72 65 63 74 6f 72 fsync() director
e8cb0 79 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e 67 y after deleting
e8cc0 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e file */.){. in
e8cd0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
e8ce0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
e8cf0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
e8d00 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
e8d10 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 (return SQLITE_I
e8d20 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 OERR_DELETE);.
e8d30 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3b 0a 23 unlink(zPath);.#
e8d40 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 ifndef SQLITE_DI
e8d50 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a 20 20 SABLE_DIRSYNC.
e8d60 69 66 28 20 64 69 72 53 79 6e 63 20 29 7b 0a 20 if( dirSync ){.
e8d70 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 20 20 72 int fd;. r
e8d80 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f 72 c = openDirector
e8d90 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b 0a 20 y(zPath, &fd);.
e8da0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
e8db0 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f 53 5f 56 E_OK ){.#if OS_V
e8dc0 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 69 66 28 XWORKS. if(
e8dd0 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d 31 20 29 fsync(fd)==-1 )
e8de0 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 69 66 28 .#else. if(
e8df0 20 66 73 79 6e 63 28 66 64 29 20 29 0a 23 65 6e fsync(fd) ).#en
e8e00 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 dif. {.
e8e10 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
e8e20 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e 43 3b IOERR_DIR_FSYNC;
e8e30 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
e8e40 66 28 20 63 6c 6f 73 65 28 66 64 29 26 26 21 72 f( close(fd)&&!r
e8e50 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 c ){. rc
e8e60 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 = SQLITE_IOERR_D
e8e70 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 IR_CLOSE;.
e8e80 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 }. }. }.#end
e8e90 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a if. return rc;.
e8ea0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 68 }../*.** Test th
e8eb0 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 6f e existance of o
e8ec0 72 20 61 63 63 65 73 73 20 70 65 72 6d 69 73 73 r access permiss
e8ed0 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a 50 61 ions of file zPa
e8ee0 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73 74 20 th. The.** test
e8ef0 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65 6e 64 performed depend
e8f00 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f s on the value o
e8f10 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 f flags:.**.**
e8f20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 SQLITE_ACCESS
e8f30 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72 6e 20 _EXISTS: Return
e8f40 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 65 78 1 if the file ex
e8f50 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51 4c 49 ists.** SQLI
e8f60 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 TE_ACCESS_READWR
e8f70 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20 69 66 ITE: Return 1 if
e8f80 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 61 the file is rea
e8f90 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2e 0a d and writable..
e8fa0 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 ** SQLITE_AC
e8fb0 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a 20 52 CESS_READONLY: R
e8fc0 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 66 eturn 1 if the f
e8fd0 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e ile is readable.
e8fe0 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 .**.** Otherwise
e8ff0 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 return 0..*/.st
e9000 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41 63 63 atic int unixAcc
e9010 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 ess(. sqlite3_v
e9020 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20 2f fs *NotUsed, /
e9030 2a 20 54 68 65 20 56 46 53 20 63 6f 6e 74 61 69 * The VFS contai
e9040 6e 69 6e 67 20 74 68 69 73 20 78 41 63 63 65 73 ning this xAcces
e9050 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f s method */. co
e9060 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c nst char *zPath,
e9070 20 20 20 20 20 20 2f 2a 20 50 61 74 68 20 6f 66 /* Path of
e9080 20 74 68 65 20 66 69 6c 65 20 74 6f 20 65 78 61 the file to exa
e9090 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c mine */. int fl
e90a0 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ags,
e90b0 20 20 2f 2a 20 57 68 61 74 20 64 6f 20 77 65 20 /* What do we
e90c0 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e 20 61 62 want to learn ab
e90d0 6f 75 74 20 74 68 65 20 7a 50 61 74 68 20 66 69 out the zPath fi
e90e0 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 le? */. int *pR
e90f0 65 73 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 esOut
e9100 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75 6c 74 /* Write result
e9110 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65 20 2a 2f boolean here */
e9120 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f 64 65 20 .){. int amode
e9130 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 = 0;. UNUSED_PA
e9140 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e9150 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
e9160 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
e9170 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 3b TE_IOERR_ACCESS;
e9180 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 66 6c );. switch( fl
e9190 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20 ags ){. case
e91a0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
e91b0 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d 6f 64 ISTS:. amod
e91c0 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20 20 20 e = F_OK;.
e91d0 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
e91e0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 SQLITE_ACCESS_RE
e91f0 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 61 ADWRITE:. a
e9200 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b mode = W_OK|R_OK
e9210 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
e9220 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 case SQLITE_A
e9230 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 CCESS_READ:.
e9240 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b 3b 0a amode = R_OK;.
e9250 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 break;..
e9260 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 default:.
e9270 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 6c 69 assert(!"Invali
e9280 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 d flags argument
e9290 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f ");. }. *pResO
e92a0 75 74 20 3d 20 28 61 63 63 65 73 73 28 7a 50 61 ut = (access(zPa
e92b0 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 29 3b 0a th, amode)==0);.
e92c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e92d0 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 OK;.}.../*.** Tu
e92e0 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 rn a relative pa
e92f0 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 thname into a fu
e9300 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54 68 65 ll pathname. The
e9310 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 0a 2a relative path.*
e9320 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 61 * is stored as a
e9330 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 nul-terminated
e9340 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 62 75 string in the bu
e9350 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 ffer pointed to
e9360 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a 2a 2a by.** zPath. .**
e9370 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74 73 20 .** zOut points
e9380 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 61 to a buffer of a
e9390 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65 33 5f t least sqlite3_
e93a0 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 20 62 vfs.mxPathname b
e93b0 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74 68 69 ytes .** (in thi
e93c0 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41 54 48 s case, MAX_PATH
e93d0 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54 68 65 NAME bytes). The
e93e0 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20 77 72 full-path is wr
e93f0 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68 69 73 itten to.** this
e9400 20 62 75 66 66 65 72 20 62 65 66 6f 72 65 20 72 buffer before r
e9410 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 eturning..*/.sta
e9420 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75 6c 6c tic int unixFull
e9430 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 Pathname(. sqli
e9440 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 te3_vfs *pVfs,
e9450 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
e9460 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 nter to vfs obje
e9470 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ct */. const ch
e9480 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 ar *zPath,
e9490 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c /* Possibl
e94a0 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74 y relative input
e94b0 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e path */. int n
e94c0 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 Out,
e94d0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
e94e0 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 65 of output buffe
e94f0 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 r in bytes */.
e9500 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 20 20 20 char *zOut
e9510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e9520 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a Output buffer *
e9530 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 27 73 20 /.){.. /* It's
e9540 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 odd to simulate
e9550 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 72 65 an io-error here
e9560 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 68 69 , but really thi
e9570 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a 20 75 s is just. ** u
e9580 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 72 6f sing the io-erro
e9590 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 72 65 r infrastructure
e95a0 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 53 51 to test that SQ
e95b0 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 68 69 Lite handles thi
e95c0 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 s. ** function
e95d0 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20 66 75 failing. This fu
e95e0 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66 61 69 nction could fai
e95f0 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d 70 6c l if, for exampl
e9600 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72 e, the. ** curr
e9610 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69 72 65 ent working dire
e9620 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e 20 75 ctory has been u
e9630 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a 20 20 nlinked.. */.
e9640 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
e9650 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
e9660 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 73 65 72 RROR );.. asser
e9670 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e t( pVfs->mxPathn
e9680 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 4e 41 4d ame==MAX_PATHNAM
e9690 45 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 E );. UNUSED_PA
e96a0 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 0a RAMETER(pVfs);..
e96b0 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d 20 3d zOut[nOut-1] =
e96c0 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 7a 50 61 '\0';. if( zPa
e96d0 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 th[0]=='/' ){.
e96e0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
e96f0 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74 2c 20 22 tf(nOut, zOut, "
e9700 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20 20 7d %s", zPath);. }
e9710 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 43 else{. int nC
e9720 77 64 3b 0a 20 20 20 20 69 66 28 20 67 65 74 63 wd;. if( getc
e9730 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 2d 31 29 wd(zOut, nOut-1)
e9740 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
e9750 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f urn SQLITE_CANTO
e9760 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e PEN;. }. n
e9770 43 77 64 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 Cwd = (int)strle
e9780 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c n(zOut);. sql
e9790 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4f ite3_snprintf(nO
e97a0 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74 5b 6e ut-nCwd, &zOut[n
e97b0 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a 50 61 Cwd], "/%s", zPa
e97c0 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 th);. }. retur
e97d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
e97e0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
e97f0 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
e9800 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 ION./*.** Interf
e9810 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 aces for opening
e9820 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 a shared librar
e9830 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 y, finding entry
e9840 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 points.** withi
e9850 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 n the shared lib
e9860 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e rary, and closin
e9870 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 g the shared lib
e9880 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 rary..*/.#includ
e9890 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73 74 61 74 e <dlfcn.h>.stat
e98a0 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 44 6c 4f ic void *unixDlO
e98b0 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 pen(sqlite3_vfs
e98c0 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e 73 74 20 *NotUsed, const
e98d0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 char *zFilename)
e98e0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
e98f0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
e9900 20 72 65 74 75 72 6e 20 64 6c 6f 70 65 6e 28 7a return dlopen(z
e9910 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c 44 5f 4e Filename, RTLD_N
e9920 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f 42 41 4c OW | RTLD_GLOBAL
e9930 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 );.}../*.** SQLi
e9940 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75 te calls this fu
e9950 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69 61 74 65 nction immediate
e9960 6c 79 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 ly after a call
e9970 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28 29 20 6f to unixDlSym() o
e9980 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70 65 6e 28 r.** unixDlOpen(
e9990 29 20 66 61 69 6c 73 20 28 72 65 74 75 72 6e 73 ) fails (returns
e99a0 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 29 a null pointer)
e99b0 2e 20 49 66 20 61 20 6d 6f 72 65 20 64 65 74 61 . If a more deta
e99c0 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 iled error.** me
e99d0 73 73 61 67 65 20 69 73 20 61 76 61 69 6c 61 62 ssage is availab
e99e0 6c 65 2c 20 69 74 20 69 73 20 77 72 69 74 74 65 le, it is writte
e99f0 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e 20 49 66 n to zBufOut. If
e9a00 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 61 67 no error messag
e9a10 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61 62 6c e.** is availabl
e9a20 65 2c 20 7a 42 75 66 4f 75 74 20 69 73 20 6c 65 e, zBufOut is le
e9a30 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 20 61 6e ft unmodified an
e9a40 64 20 53 51 4c 69 74 65 20 75 73 65 73 20 61 20 d SQLite uses a
e9a50 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72 72 6f 72 default.** error
e9a60 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 73 74 61 message..*/.sta
e9a70 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 45 tic void unixDlE
e9a80 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 rror(sqlite3_vfs
e9a90 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e *NotUsed, int n
e9aa0 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f Buf, char *zBufO
e9ab0 75 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 45 72 ut){. char *zEr
e9ac0 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 r;. UNUSED_PARA
e9ad0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
e9ae0 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
e9af0 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64 6c 65 ();. zErr = dle
e9b00 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20 7a 45 rror();. if( zE
e9b10 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 rr ){. sqlite
e9b20 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 3_snprintf(nBuf,
e9b30 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22 2c 20 zBufOut, "%s",
e9b40 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 zErr);. }. uni
e9b50 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d xLeaveMutex();.}
e9b60 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28 2a 75 .static void (*u
e9b70 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 nixDlSym(sqlite3
e9b80 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 76 _vfs *NotUsed, v
e9b90 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 oid *p, const ch
e9ba0 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 7b ar*zSym))(void){
e9bb0 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43 43 20 . /* . ** GCC
e9bc0 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 with -pedantic-e
e9bd0 72 72 6f 72 73 20 73 61 79 73 20 74 68 61 74 20 rrors says that
e9be0 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c C90 does not all
e9bf0 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20 62 65 ow a void* to be
e9c00 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74 6f 20 . ** cast into
e9c10 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 a pointer to a f
e9c20 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20 79 65 unction. And ye
e9c30 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 64 6c t the library dl
e9c40 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a 20 20 sym() routine.
e9c50 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 6f 69 ** returns a voi
e9c60 64 2a 20 77 68 69 63 68 20 69 73 20 72 65 61 6c d* which is real
e9c70 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ly a pointer to
e9c80 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 a function. So
e9c90 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a 20 75 how do we. ** u
e9ca0 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74 68 20 se dlsym() with
e9cb0 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 73 -pedantic-errors
e9cc0 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 ?. **. ** Vari
e9cd0 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69 73 20 able x below is
e9ce0 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 61 20 defined to be a
e9cf0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e pointer to a fun
e9d00 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20 20 2a ction taking. *
e9d10 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76 6f 69 * parameters voi
e9d20 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63 68 61 d* and const cha
e9d30 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 r* and returning
e9d40 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
e9d50 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 57 function.. ** W
e9d60 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78 20 62 e initialize x b
e9d70 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74 20 61 y assigning it a
e9d80 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
e9d90 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69 6f 6e dlsym() function
e9da0 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61 73 73 .. ** (That ass
e9db0 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 73 ignment requires
e9dc0 20 61 20 63 61 73 74 2e 29 20 20 54 68 65 6e 20 a cast.) Then
e9dd0 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75 6e 63 we call the func
e9de0 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a 20 78 tion that. ** x
e9df0 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a 20 20 points to. .
e9e00 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77 6f 72 **. ** This wor
e9e10 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e 6c 69 k-around is unli
e9e20 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63 6f 72 kely to work cor
e9e30 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20 73 79 rectly on any sy
e9e40 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a 2a 20 stem where. **
e9e50 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e 6e 6f you really canno
e9e60 74 20 63 61 73 74 20 61 20 66 75 6e 63 74 69 6f t cast a functio
e9e70 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 76 n pointer into v
e9e80 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65 6e 2c oid*. But then,
e9e90 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 on the. ** oth
e9ea0 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d 28 29 er hand, dlsym()
e9eb0 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 6f will not work o
e9ec0 6e 20 73 75 63 68 20 61 20 73 79 73 74 65 6d 20 n such a system
e9ed0 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20 68 61 either, so we ha
e9ee0 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 61 6c ve. ** not real
e9ef0 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69 6e 67 ly lost anything
e9f00 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a .. */. void (*
e9f10 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 (*x)(void*,const
e9f20 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 3b 0a char*))(void);.
e9f30 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
e9f40 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 78 ER(NotUsed);. x
e9f50 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28 76 6f = (void(*(*)(vo
e9f60 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 id*,const char*)
e9f70 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b 0a 20 )(void))dlsym;.
e9f80 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70 2c 20 return (*x)(p,
e9f90 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69 63 20 zSym);.}.static
e9fa0 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f 73 65 void unixDlClose
e9fb0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f (sqlite3_vfs *No
e9fc0 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 48 61 tUsed, void *pHa
e9fd0 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f ndle){. UNUSED_
e9fe0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e9ff0 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28 70 48 d);. dlclose(pH
ea000 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 andle);.}.#else
ea010 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 /* if SQLITE_OMI
ea020 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
ea030 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a is defined: */.
ea040 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c #define unixDl
ea050 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e Open 0. #defin
ea060 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20 30 0a e unixDlError 0.
ea070 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c #define unixDl
ea080 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 69 6e Sym 0. #defin
ea090 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20 30 0a e unixDlClose 0.
ea0a0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 #endif../*.** Wr
ea0b0 69 74 65 20 6e 42 75 66 20 62 79 74 65 73 20 6f ite nBuf bytes o
ea0c0 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20 74 6f f random data to
ea0d0 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 the supplied bu
ea0e0 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 ffer zBuf..*/.st
ea0f0 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52 61 6e atic int unixRan
ea100 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f domness(sqlite3_
ea110 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e vfs *NotUsed, in
ea120 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
ea130 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 uf){. UNUSED_PA
ea140 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
ea150 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69 7a 65 ;. assert((size
ea160 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65 6f 66 _t)nBuf>=(sizeof
ea170 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f 66 28 (time_t)+sizeof(
ea180 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 57 65 int)));.. /* We
ea190 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69 61 6c have to initial
ea1a0 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 65 76 ize zBuf to prev
ea1b0 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 72 6f ent valgrind fro
ea1c0 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 2a 2a m reporting. **
ea1d0 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20 72 65 errors. The re
ea1e0 70 6f 72 74 73 20 69 73 73 75 65 64 20 62 79 20 ports issued by
ea1f0 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69 6e 63 valgrind are inc
ea200 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f 75 6c orrect - we woul
ea210 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 74 68 d. ** prefer th
ea220 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 at the randomnes
ea230 73 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 62 s be increased b
ea240 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f 66 20 y making use of
ea250 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 74 69 the. ** uniniti
ea260 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69 6e 20 alized space in
ea270 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c 67 72 zBuf - but valgr
ea280 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e 64 20 ind errors tend
ea290 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 73 6f to worry. ** so
ea2a0 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74 68 65 me users. Rathe
ea2b0 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20 69 74 r than argue, it
ea2c0 20 73 65 65 6d 73 20 65 61 73 69 65 72 20 6a 75 seems easier ju
ea2d0 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 st to initialize
ea2e0 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c 65 20 . ** the whole
ea2f0 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65 6e 63 array and silenc
ea300 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 65 6e e valgrind, even
ea310 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 6c if that means l
ea320 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 0a 20 ess randomness.
ea330 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e 64 6f ** in the rando
ea340 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a m seed.. **. *
ea350 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 * When testing,
ea360 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a 42 75 initializing zBu
ea370 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 20 61 f[] to zero is a
ea380 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 74 20 ll we do. That
ea390 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 74 20 means. ** that
ea3a0 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 68 we always use th
ea3b0 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 6e 75 e same random nu
ea3c0 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e 20 20 mber sequence.
ea3d0 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 0a 20 This makes the.
ea3e0 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65 61 74 ** tests repeat
ea3f0 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d able.. */. mem
ea400 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 set(zBuf, 0, nBu
ea410 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 f);.#if !defined
ea420 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 (SQLITE_TEST).
ea430 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c 20 66 {. int pid, f
ea440 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e d;. fd = open
ea450 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c ("/dev/urandom",
ea460 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 20 20 O_RDONLY);.
ea470 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 if( fd<0 ){.
ea480 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20 time_t t;.
ea490 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 time(&t);.
ea4a0 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 memcpy(zBuf, &
ea4b0 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 t, sizeof(t));.
ea4c0 20 20 20 20 20 70 69 64 20 3d 20 67 65 74 70 69 pid = getpi
ea4d0 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 d();. memcp
ea4e0 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66 28 74 y(&zBuf[sizeof(t
ea4f0 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 )], &pid, sizeof
ea500 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20 61 73 (pid));. as
ea510 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74 29 2b sert( sizeof(t)+
ea520 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28 73 69 sizeof(pid)<=(si
ea530 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20 20 20 ze_t)nBuf );.
ea540 20 20 20 6e 42 75 66 20 3d 20 73 69 7a 65 6f 66 nBuf = sizeof
ea550 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 70 69 64 (t) + sizeof(pid
ea560 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
ea570 20 20 20 20 6e 42 75 66 20 3d 20 72 65 61 64 28 nBuf = read(
ea580 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66 29 3b fd, zBuf, nBuf);
ea590 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 . close(fd)
ea5a0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
ea5b0 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 42 75 66 if. return nBuf
ea5c0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 ;.}.../*.** Slee
ea5d0 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 p for a little w
ea5e0 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 hile. Return th
ea5f0 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 e amount of time
ea600 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 slept..** The a
ea610 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e rgument is the n
ea620 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 umber of microse
ea630 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f conds we want to
ea640 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 sleep..** The r
ea650 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 eturn value is t
ea660 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 he number of mic
ea670 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 roseconds of sle
ea680 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 ep actually.** r
ea690 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 equested from th
ea6a0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 e underlying ope
ea6b0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 rating system, a
ea6c0 20 6e 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a number which.**
ea6d0 20 6d 69 67 68 74 20 62 65 20 67 72 65 61 74 65 might be greate
ea6e0 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 r than or equal
ea6f0 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c to the argument,
ea700 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a but not less.**
ea710 20 74 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65 than the argume
ea720 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e nt..*/.static in
ea730 74 20 75 6e 69 78 53 6c 65 65 70 28 73 71 6c 69 t unixSleep(sqli
ea740 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
ea750 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e , int microsecon
ea760 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f ds){.#if OS_VXWO
ea770 52 4b 53 0a 20 20 73 74 72 75 63 74 20 74 69 6d RKS. struct tim
ea780 65 73 70 65 63 20 73 70 3b 0a 0a 20 20 73 70 2e espec sp;.. sp.
ea790 74 76 5f 73 65 63 20 3d 20 6d 69 63 72 6f 73 65 tv_sec = microse
ea7a0 63 6f 6e 64 73 20 2f 20 31 30 30 30 30 30 30 3b conds / 1000000;
ea7b0 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63 20 3d 20 . sp.tv_nsec =
ea7c0 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 25 20 (microseconds %
ea7d0 31 30 30 30 30 30 30 29 20 2a 20 31 30 30 30 3b 1000000) * 1000;
ea7e0 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26 73 70 . nanosleep(&sp
ea7f0 2c 20 4e 55 4c 4c 29 3b 0a 20 20 72 65 74 75 72 , NULL);. retur
ea800 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a n microseconds;.
ea810 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 #elif defined(HA
ea820 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 VE_USLEEP) && HA
ea830 56 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 VE_USLEEP. usle
ea840 65 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 ep(microseconds)
ea850 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f ;. return micro
ea860 73 65 63 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 seconds;.#else.
ea870 20 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20 28 int seconds = (
ea880 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39 39 microseconds+999
ea890 39 39 39 29 2f 31 30 30 30 30 30 30 3b 0a 20 20 999)/1000000;.
ea8a0 73 6c 65 65 70 28 73 65 63 6f 6e 64 73 29 3b 0a sleep(seconds);.
ea8b0 20 20 72 65 74 75 72 6e 20 73 65 63 6f 6e 64 73 return seconds
ea8c0 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 *1000000;.#endif
ea8d0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ea8e0 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 7d 0a TER(NotUsed);.}.
ea8f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
ea900 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 wing variable, i
ea910 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a f set to a non-z
ea920 65 72 6f 20 76 61 6c 75 65 2c 20 69 73 20 69 6e ero value, is in
ea930 74 65 72 70 72 65 74 65 64 20 61 73 0a 2a 2a 20 terpreted as.**
ea940 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 the number of se
ea950 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 conds since 1970
ea960 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 and is used to
ea970 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 6f set the result o
ea980 66 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 43 75 f.** sqlite3OsCu
ea990 72 72 65 6e 74 54 69 6d 65 28 29 20 64 75 72 69 rrentTime() duri
ea9a0 6e 67 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 ng testing..*/.#
ea9b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
ea9c0 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
ea9d0 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 sqlite3_current
ea9e0 5f 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46 _time = 0; /* F
ea9f0 61 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65 20 ake system time
eaa00 69 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 in seconds since
eaa10 20 31 39 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 1970. */.#endif
eaa20 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 ../*.** Find the
eaa30 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 current time (i
eaa40 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 n Universal Coor
eaa50 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 dinated Time).
eaa60 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 Write the.** cur
eaa70 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 rent time and da
eaa80 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 te as a Julian D
eaa90 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a ay number into *
eaaa0 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 prNow and.** ret
eaab0 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 urn 0. Return 1
eaac0 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 if the time and
eaad0 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 date cannot be
eaae0 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 found..*/.static
eaaf0 20 69 6e 74 20 75 6e 69 78 43 75 72 72 65 6e 74 int unixCurrent
eab00 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 Time(sqlite3_vfs
eab10 20 2a 4e 6f 74 55 73 65 64 2c 20 64 6f 75 62 6c *NotUsed, doubl
eab20 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 20 64 e *prNow){.#if d
eab30 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 54 4f 44 efined(NO_GETTOD
eab40 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 ). time_t t;.
eab50 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e time(&t);. *prN
eab60 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e 30 20 2b ow = t/86400.0 +
eab70 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 65 6c 69 2440587.5;.#eli
eab80 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 f OS_VXWORKS. s
eab90 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 20 73 truct timespec s
eaba0 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f 67 65 74 Now;. clock_get
eabb0 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 41 4c 54 time(CLOCK_REALT
eabc0 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 2a IME, &sNow);. *
eabd0 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37 2e prNow = 2440587.
eabe0 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 5 + sNow.tv_sec/
eabf0 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 86400.0 + sNow.t
eac00 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 30 30 30 v_nsec/864000000
eac10 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73 65 0a 20 00000.0;.#else.
eac20 20 73 74 72 75 63 74 20 74 69 6d 65 76 61 6c 20 struct timeval
eac30 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69 6d 65 6f sNow;. gettimeo
eac40 66 64 61 79 28 26 73 4e 6f 77 2c 20 30 29 3b 0a fday(&sNow, 0);.
eac50 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 *prNow = 24405
eac60 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 87.5 + sNow.tv_s
eac70 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f ec/86400.0 + sNo
eac80 77 2e 74 76 5f 75 73 65 63 2f 38 36 34 30 30 30 w.tv_usec/864000
eac90 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 00000.0;.#endif.
eaca0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
eacb0 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 EST. if( sqlite
eacc0 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 3_current_time )
eacd0 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 {. *prNow = s
eace0 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 qlite3_current_t
eacf0 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 ime/86400.0 + 24
ead00 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 40587.5;. }.#en
ead10 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 dif. UNUSED_PAR
ead20 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
ead30 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
ead40 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 64 20 74 /*.** We added t
ead50 68 65 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 he xGetLastError
ead60 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68 20 74 () method with t
ead70 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 6f 66 20 he intention of
ead80 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 62 65 74 providing.** bet
ead90 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c 20 65 72 ter low-level er
eada0 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 68 65 ror messages whe
eadb0 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74 n operating-syst
eadc0 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 6f 6d 65 em problems come
eadd0 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 20 53 51 up.** during SQ
eade0 4c 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 Lite operation.
eadf0 20 42 75 74 20 73 6f 20 66 61 72 2c 20 6e 6f 6e But so far, non
eae00 65 20 6f 66 20 74 68 61 74 20 68 61 73 20 62 65 e of that has be
eae10 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a en implemented.*
eae20 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 2e 20 20 * in the core.
eae30 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 So this routine
eae40 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e is never called.
eae50 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 20 69 73 For now, it is
eae60 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 70 6c 61 merely.** a pla
eae70 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f 0a 73 74 ce-holder..*/.st
eae80 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47 65 74 atic int unixGet
eae90 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 LastError(sqlite
eaea0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 3_vfs *NotUsed,
eaeb0 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c 20 63 68 int NotUsed2, ch
eaec0 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 7b 0a 20 ar *NotUsed3){.
eaed0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
eaee0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 55 4e R(NotUsed);. UN
eaef0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
eaf00 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 4e 55 53 otUsed2);. UNUS
eaf10 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
eaf20 55 73 65 64 33 29 3b 0a 20 20 72 65 74 75 72 6e Used3);. return
eaf30 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 0;.}../*.******
eaf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaf50 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 ** End of sqlite
eaf60 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 3_vfs methods **
eaf70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaf80 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
eaf90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eafa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eafb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eafc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eafd0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a ********/../****
eafe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
eb030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb040 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50 72 6f 78 ***** Begin Prox
eb050 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a y Locking ******
eb060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
eb080 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 Proxy locking i
eb090 73 20 61 20 22 75 62 65 72 2d 6c 6f 63 6b 69 6e s a "uber-lockin
eb0a0 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20 74 68 69 g-method" in thi
eb0b0 73 20 73 65 6e 73 65 3a 20 20 49 74 20 75 73 65 s sense: It use
eb0c0 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65 72 20 6c s the.** other l
eb0d0 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f ocking methods o
eb0e0 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c 6f 63 6b n secondary lock
eb0f0 20 66 69 6c 65 73 2e 20 20 50 72 6f 78 79 20 6c files. Proxy l
eb100 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a 2a 20 6d ocking is a.** m
eb110 65 74 61 2d 6c 61 79 65 72 20 6f 76 65 72 20 74 eta-layer over t
eb120 6f 70 20 6f 66 20 74 68 65 20 70 72 69 6d 69 74 op of the primit
eb130 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c ive locking impl
eb140 65 6d 65 6e 74 65 64 20 61 62 6f 76 65 2e 20 20 emented above.
eb150 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72 65 61 73 For.** this reas
eb160 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f 6e on, the division
eb170 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
eb180 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e of proxy lockin
eb190 67 20 69 73 20 64 65 66 65 72 72 65 64 0a 2a 2a g is deferred.**
eb1a0 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 6e 20 74 until late in t
eb1b0 68 65 20 66 69 6c 65 20 28 68 65 72 65 29 20 61 he file (here) a
eb1c0 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 fter all of the
eb1d0 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 68 6f 64 other I/O method
eb1e0 73 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 64 s have.** been d
eb1f0 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 68 61 74 efined - so that
eb200 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 6c the primitive l
eb210 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 61 ocking methods a
eb220 72 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 re available.**
eb230 61 73 20 73 65 72 76 69 63 65 73 20 74 6f 20 68 as services to h
eb240 65 6c 70 20 77 69 74 68 20 74 68 65 20 69 6d 70 elp with the imp
eb250 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 lementation of p
eb260 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a roxy locking..**
eb270 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 .****.**.** The
eb280 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 default locking
eb290 73 63 68 65 6d 65 73 20 69 6e 20 53 51 4c 69 74 schemes in SQLit
eb2a0 65 20 75 73 65 20 62 79 74 65 2d 72 61 6e 67 65 e use byte-range
eb2b0 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 0a 2a 2a locks on the.**
eb2c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
eb2d0 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 73 61 66 o coordinate saf
eb2e0 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 61 63 e, concurrent ac
eb2f0 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70 6c 65 cess by multiple
eb300 20 72 65 61 64 65 72 73 0a 2a 2a 20 61 6e 64 20 readers.** and
eb310 77 72 69 74 65 72 73 20 5b 68 74 74 70 3a 2f 2f writers [http://
eb320 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f 63 6b 69 sqlite.org/locki
eb330 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20 54 68 65 ngv3.html]. The
eb340 20 66 69 76 65 20 66 69 6c 65 20 6c 6f 63 6b 69 five file locki
eb350 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20 28 55 4e ng.** states (UN
eb360 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49 4e 47 2c LOCKED, PENDING,
eb370 20 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 SHARED, RESERVE
eb380 44 2c 20 45 58 43 4c 55 53 49 56 45 29 20 61 72 D, EXCLUSIVE) ar
eb390 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a e implemented.**
eb3a0 20 61 73 20 50 4f 53 49 58 20 72 65 61 64 20 26 as POSIX read &
eb3b0 20 77 72 69 74 65 20 6c 6f 63 6b 73 20 6f 76 65 write locks ove
eb3c0 72 20 66 69 78 65 64 20 73 65 74 20 6f 66 20 6c r fixed set of l
eb3d0 6f 63 61 74 69 6f 6e 73 20 28 76 69 61 20 66 73 ocations (via fs
eb3e0 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41 46 50 20 ctl),.** on AFP
eb3f0 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20 65 78 63 and SMB only exc
eb400 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61 6e 67 lusive byte-rang
eb410 65 20 6c 6f 63 6b 73 20 61 72 65 20 61 76 61 69 e locks are avai
eb420 6c 61 62 6c 65 20 76 69 61 20 66 73 63 74 6c 0a lable via fsctl.
eb430 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52 28 27 7a ** with _IOWR('z
eb440 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20 42 79 ', 23, struct By
eb450 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 29 20 teRangeLockPB2)
eb460 74 6f 20 74 72 61 63 6b 20 74 68 65 20 73 61 6d to track the sam
eb470 65 20 35 20 73 74 61 74 65 73 2e 0a 2a 2a 20 54 e 5 states..** T
eb480 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 46 5f 52 o simulate a F_R
eb490 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73 68 61 72 DLCK on the shar
eb4a0 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20 41 46 50 ed range, on AFP
eb4b0 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73 65 6c 65 a randomly sele
eb4c0 63 74 65 64 0a 2a 2a 20 61 64 64 72 65 73 73 20 cted.** address
eb4d0 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 72 61 in the shared ra
eb4e0 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 66 6f 72 nge is taken for
eb4f0 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 a SHARED lock,
eb500 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 73 68 the entire.** sh
eb510 61 72 65 64 20 72 61 6e 67 65 20 69 73 20 74 61 ared range is ta
eb520 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58 43 4c 55 ken for an EXCLU
eb530 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a 2a 0a 2a SIVE lock):.**.*
eb540 2a 20 20 20 20 20 20 50 45 4e 44 49 4e 47 5f 42 * PENDING_B
eb550 59 54 45 20 20 20 20 20 20 20 20 30 78 34 30 30 YTE 0x400
eb560 30 30 30 30 30 09 09 20 20 20 09 0a 2a 2a 20 20 00000.. ..**
eb570 20 20 20 20 52 45 53 45 52 56 45 44 5f 42 59 54 RESERVED_BYT
eb580 45 20 20 20 20 20 20 20 30 78 34 30 30 30 30 30 E 0x400000
eb590 30 31 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45 01.** SHARE
eb5a0 44 5f 52 41 4e 47 45 20 20 20 20 20 20 20 20 30 D_RANGE 0
eb5b0 78 34 30 30 30 30 30 30 32 20 2d 3e 20 30 78 34 x40000002 -> 0x4
eb5c0 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a 20 54 68 0000200.**.** Th
eb5d0 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c 20 6f 6e is works well on
eb5e0 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65 20 the local file
eb5f0 73 79 73 74 65 6d 2c 20 62 75 74 20 73 68 6f 77 system, but show
eb600 73 20 61 20 6e 65 61 72 6c 79 20 31 30 30 78 0a s a nearly 100x.
eb610 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69 6e 20 72 ** slowdown in r
eb620 65 61 64 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 ead performance
eb630 6f 6e 20 41 46 50 20 62 65 63 61 75 73 65 20 74 on AFP because t
eb640 68 65 20 41 46 50 20 63 6c 69 65 6e 74 20 64 69 he AFP client di
eb650 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65 20 72 65 sables.** the re
eb660 61 64 20 63 61 63 68 65 20 77 68 65 6e 20 62 79 ad cache when by
eb670 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61 te-range locks a
eb680 72 65 20 70 72 65 73 65 6e 74 2e 20 20 45 6e 61 re present. Ena
eb690 62 6c 69 6e 67 20 74 68 65 20 72 65 61 64 0a 2a bling the read.*
eb6a0 2a 20 63 61 63 68 65 20 65 78 70 6f 73 65 73 20 * cache exposes
eb6b0 61 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 a cache coherenc
eb6c0 79 20 70 72 6f 62 6c 65 6d 20 74 68 61 74 20 69 y problem that i
eb6d0 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 61 6c 6c s present on all
eb6e0 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70 6f 72 74 OS X.** support
eb6f0 65 64 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 20 ed network file
eb700 73 79 73 74 65 6d 73 2e 20 20 4e 46 53 20 61 6e systems. NFS an
eb710 64 20 41 46 50 20 62 6f 74 68 20 6f 62 73 65 72 d AFP both obser
eb720 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f 73 65 2d ve the.** close-
eb730 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e 74 69 63 to-open semantic
eb740 73 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 63 s for ensuring c
eb750 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 0a 2a ache coherency.*
eb760 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73 2e 73 6f * [http://nfs.so
eb770 75 72 63 65 66 6f 72 67 65 2e 6e 65 74 2f 23 66 urceforge.net/#f
eb780 61 71 5f 61 38 5d 2c 20 77 68 69 63 68 20 64 6f aq_a8], which do
eb790 65 73 20 6e 6f 74 20 65 66 66 65 63 74 69 76 65 es not effective
eb7a0 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 68 ly.** address th
eb7b0 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 20 66 e requirements f
eb7c0 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74 20 64 61 or concurrent da
eb7d0 74 61 62 61 73 65 20 61 63 63 65 73 73 20 62 79 tabase access by
eb7e0 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 72 65 61 multiple.** rea
eb7f0 64 65 72 73 20 61 6e 64 20 77 72 69 74 65 72 73 ders and writers
eb800 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77 77 77 2e .** [http://www.
eb810 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c 69 74 nabble.com/SQLit
eb820 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68 65 2d 63 e-on-NFS-cache-c
eb830 6f 68 65 72 65 6e 63 79 2d 74 64 31 35 36 35 35 oherency-td15655
eb840 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 0a 2a 2a 701.html]..**.**
eb850 20 54 6f 20 61 64 64 72 65 73 73 20 74 68 65 20 To address the
eb860 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 6e 64 20 performance and
eb870 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20 cache coherency
eb880 69 73 73 75 65 73 2c 20 70 72 6f 78 79 20 66 69 issues, proxy fi
eb890 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 63 68 le locking.** ch
eb8a0 61 6e 67 65 73 20 74 68 65 20 77 61 79 20 64 61 anges the way da
eb8b0 74 61 62 61 73 65 20 61 63 63 65 73 73 20 69 73 tabase access is
eb8c0 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 6c controlled by l
eb8d0 69 6d 69 74 69 6e 67 20 61 63 63 65 73 73 20 74 imiting access t
eb8e0 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 68 6f o a.** single ho
eb8f0 73 74 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 st at a time and
eb900 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 6c 6f 63 moving file loc
eb910 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 20 64 61 ks off of the da
eb920 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 tabase file.** a
eb930 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f 78 79 20 nd onto a proxy
eb940 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 file on the loca
eb950 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20 20 l file system.
eb960 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 6e 67 20 .**.**.** Using
eb970 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a 2a 20 2d proxy locks.** -
eb980 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
eb990 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73 0a 2a 2a .**.** C APIs.**
eb9a0 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c .** sqlite3_fil
eb9b0 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 e_control(db, db
eb9c0 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 53 45 54 name, SQLITE_SET
eb9d0 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 0a _LOCKPROXYFILE,.
eb9e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
eb9f0 20 20 20 20 20 20 20 20 20 3c 70 72 6f 78 79 5f <proxy_
eba00 70 61 74 68 3e 20 7c 20 22 3a 61 75 74 6f 3a 22 path> | ":auto:"
eba10 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 );.** sqlite3_f
eba20 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 ile_control(db,
eba30 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 47 dbname, SQLITE_G
eba40 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 ET_LOCKPROXYFILE
eba50 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74 68 3e 29 , &<proxy_path>)
eba60 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 4c 20 70 ;.**.**.** SQL p
eba70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 20 50 52 ragmas.**.** PR
eba80 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d AGMA [database.]
eba90 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 3d lock_proxy_file=
ebaa0 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 3a <proxy_path> | :
ebab0 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41 47 4d 41 auto:.** PRAGMA
ebac0 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b [database.]lock
ebad0 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a 2a 0a 2a _proxy_file.**.*
ebae0 2a 20 53 70 65 63 69 66 79 69 6e 67 20 22 3a 61 * Specifying ":a
ebaf0 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74 68 61 74 uto:" means that
ebb00 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 63 if there is a c
ebb10 6f 6e 63 68 20 66 69 6c 65 20 77 69 74 68 20 61 onch file with a
ebb20 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 68 6f 73 matching.** hos
ebb30 74 20 49 44 20 69 6e 20 69 74 2c 20 74 68 65 20 t ID in it, the
ebb40 70 72 6f 78 79 20 70 61 74 68 20 69 6e 20 74 68 proxy path in th
ebb50 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 6c e conch file wil
ebb60 6c 20 62 65 20 75 73 65 64 2c 20 6f 74 68 65 72 l be used, other
ebb70 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f 78 79 20 wise.** a proxy
ebb80 70 61 74 68 20 62 61 73 65 64 20 6f 6e 20 74 68 path based on th
ebb90 65 20 75 73 65 72 27 73 20 74 65 6d 70 20 64 69 e user's temp di
ebba0 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e 66 73 74 r.** (via confst
ebbb0 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 r(_CS_DARWIN_USE
ebbc0 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e 2e 29 29 R_TEMP_DIR,...))
ebbd0 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 61 6e will be used an
ebbe0 64 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 6c 20 d the.** actual
ebbf0 70 72 6f 78 79 20 66 69 6c 65 20 6e 61 6d 65 20 proxy file name
ebc00 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f is generated fro
ebc10 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 70 m the name and p
ebc20 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 ath of the.** da
ebc30 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 46 6f tabase file. Fo
ebc40 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a r example:.**.**
ebc50 20 20 20 20 20 20 20 46 6f 72 20 64 61 74 61 62 For datab
ebc60 61 73 65 20 70 61 74 68 20 22 2f 55 73 65 72 73 ase path "/Users
ebc70 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a 2a 2a 20 /me/foo.db" .**
ebc80 20 20 20 20 20 20 54 68 65 20 6c 6f 63 6b 20 70 The lock p
ebc90 61 74 68 20 77 69 6c 6c 20 62 65 20 22 3c 74 6d ath will be "<tm
ebca0 70 64 69 72 3e 2f 73 71 6c 69 74 65 70 6c 6f 63 pdir>/sqliteploc
ebcb0 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f 66 6f 6f ks/_Users_me_foo
ebcc0 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a 2a 0a 2a .db:auto:").**.*
ebcd0 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b 20 70 72 * Once a lock pr
ebce0 6f 78 79 20 69 73 20 63 6f 6e 66 69 67 75 72 65 oxy is configure
ebcf0 64 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 d for a database
ebd00 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69 74 20 connection, it
ebd10 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65 20 72 65 can not.** be re
ebd20 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 72 20 69 moved, however i
ebd30 74 20 6d 61 79 20 62 65 20 73 77 69 74 63 68 65 t may be switche
ebd40 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74 d to a different
ebd50 20 70 72 6f 78 79 20 70 61 74 68 20 76 69 61 0a proxy path via.
ebd60 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 41 50 49 ** the above API
ebd70 73 20 28 61 73 73 75 6d 69 6e 67 20 74 68 65 20 s (assuming the
ebd80 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 20 6e 6f conch file is no
ebd90 74 20 62 65 69 6e 67 20 68 65 6c 64 20 62 79 20 t being held by
ebda0 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e 6e 65 another.** conne
ebdb0 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 65 73 73 ction or process
ebdc0 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f 77 ). .**.**.** How
ebdd0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 77 proxy locking w
ebde0 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d orks.** --------
ebdf0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a ---------------.
ebe00 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66 69 6c 65 **.** Proxy file
ebe10 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69 65 73 20 locking relies
ebe20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20 74 77 6f primarily on two
ebe30 20 6e 65 77 20 73 75 70 70 6f 72 74 69 6e 67 20 new supporting
ebe40 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 20 files: .**.**
ebe50 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65 20 74 6f * conch file to
ebe60 20 6c 69 6d 69 74 20 61 63 63 65 73 73 20 74 6f limit access to
ebe70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
ebe80 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 68 le to a single h
ebe90 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61 74 20 61 ost.** at a
ebea0 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 time.**.** *
ebeb0 20 70 72 6f 78 79 20 66 69 6c 65 20 74 6f 20 61 proxy file to a
ebec0 63 74 20 61 73 20 61 20 70 72 6f 78 79 20 66 6f ct as a proxy fo
ebed0 72 20 74 68 65 20 61 64 76 69 73 6f 72 79 20 6c r the advisory l
ebee0 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a ocks normally.**
ebef0 20 20 20 20 20 20 74 61 6b 65 6e 20 6f 6e 20 74 taken on t
ebf00 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 0a 2a he database.**.*
ebf10 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 * The conch file
ebf20 20 2d 20 74 6f 20 75 73 65 20 61 20 70 72 6f 78 - to use a prox
ebf30 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 65 20 6d y file, sqlite m
ebf40 75 73 74 20 66 69 72 73 74 20 22 68 6f 6c 64 20 ust first "hold
ebf50 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a 20 62 79 the conch".** by
ebf60 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 6c 69 74 taking an sqlit
ebf70 65 2d 73 74 79 6c 65 20 73 68 61 72 65 64 20 6c e-style shared l
ebf80 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 68 ock on the conch
ebf90 20 66 69 6c 65 2c 20 72 65 61 64 69 6e 67 20 74 file, reading t
ebfa0 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 61 he.** contents a
ebfb0 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 65 nd comparing the
ebfc0 20 68 6f 73 74 27 73 20 75 6e 69 71 75 65 20 68 host's unique h
ebfd0 6f 73 74 20 49 44 20 28 73 65 65 20 62 65 6c 6f ost ID (see belo
ebfe0 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a 2a 20 70 w) and lock.** p
ebff0 72 6f 78 79 20 70 61 74 68 20 61 67 61 69 6e 73 roxy path agains
ec000 74 20 74 68 65 20 76 61 6c 75 65 73 20 73 74 6f t the values sto
ec010 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 red in the conch
ec020 2e 20 20 54 68 65 20 63 6f 6e 63 68 20 66 69 6c . The conch fil
ec030 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69 e is.** stored i
ec040 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 n the same direc
ec050 74 6f 72 79 20 61 73 20 74 68 65 20 64 61 74 61 tory as the data
ec060 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68 base file and th
ec070 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 69 e file name.** i
ec080 73 20 70 61 74 74 65 72 6e 65 64 20 61 66 74 65 s patterned afte
ec090 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 r the database f
ec0a0 69 6c 65 20 6e 61 6d 65 20 61 73 20 22 2e 3c 64 ile name as ".<d
ec0b0 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d 63 6f 6e atabasename>-con
ec0c0 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 ch"..** If the c
ec0d0 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65 73 20 6e onch file does n
ec0e0 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 69 74 27 ot exist, or it'
ec0f0 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f 20 6e 6f s contents do no
ec100 74 20 6d 61 74 63 68 20 74 68 65 0a 2a 2a 20 68 t match the.** h
ec110 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72 20 70 72 ost ID and/or pr
ec120 6f 78 79 20 70 61 74 68 2c 20 74 68 65 6e 20 74 oxy path, then t
ec130 68 65 20 6c 6f 63 6b 20 69 73 20 65 73 63 61 6c he lock is escal
ec140 61 74 65 64 20 74 6f 20 61 6e 20 65 78 63 6c 75 ated to an exclu
ec150 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20 61 6e 64 sive.** lock and
ec160 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
ec170 63 6f 6e 74 65 6e 74 73 20 69 73 20 75 70 64 61 contents is upda
ec180 74 65 64 20 77 69 74 68 20 74 68 65 20 68 6f 73 ted with the hos
ec190 74 20 49 44 20 61 6e 64 20 70 72 6f 78 79 0a 2a t ID and proxy.*
ec1a0 2a 20 70 61 74 68 20 61 6e 64 20 74 68 65 20 6c * path and the l
ec1b0 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72 61 64 65 ock is downgrade
ec1c0 64 20 74 6f 20 61 20 73 68 61 72 65 64 20 6c 6f d to a shared lo
ec1d0 63 6b 20 61 67 61 69 6e 2e 20 20 49 66 20 74 68 ck again. If th
ec1e0 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73 20 68 65 e conch.** is he
ec1f0 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20 70 72 ld by another pr
ec200 6f 63 65 73 73 20 28 77 69 74 68 20 61 20 73 68 ocess (with a sh
ec210 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74 68 65 20 ared lock), the
ec220 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a exclusive lock.*
ec230 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 * will fail and
ec240 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 SQLITE_BUSY is r
ec250 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
ec260 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20 2d 20 he proxy file -
ec270 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 66 69 a single-byte fi
ec280 6c 65 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 le used for all
ec290 61 64 76 69 73 6f 72 79 20 66 69 6c 65 20 6c 6f advisory file lo
ec2a0 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 cks.** normally
ec2b0 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 taken on the dat
ec2c0 61 62 61 73 65 20 66 69 6c 65 2e 20 20 20 54 68 abase file. Th
ec2d0 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72 20 73 61 is allows for sa
ec2e0 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 6f 66 fe sharing.** of
ec2f0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
ec300 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 20 le for multiple
ec310 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 readers and writ
ec320 65 72 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a ers on the same.
ec330 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 63 6f 6e ** host (the con
ec340 63 68 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 ch ensures that
ec350 74 68 65 79 20 61 6c 6c 20 75 73 65 20 74 68 65 they all use the
ec360 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b same local lock
ec370 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 file)..**.** Th
ec380 65 72 65 20 69 73 20 61 20 74 68 69 72 64 20 66 ere is a third f
ec390 69 6c 65 20 2d 20 74 68 65 20 68 6f 73 74 20 49 ile - the host I
ec3a0 44 20 66 69 6c 65 20 2d 20 75 73 65 64 20 61 73 D file - used as
ec3b0 20 61 20 70 65 72 73 69 73 74 65 6e 74 20 72 65 a persistent re
ec3c0 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20 75 6e 69 cord.** of a uni
ec3d0 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 20 66 que identifier f
ec3e0 6f 72 20 74 68 65 20 68 6f 73 74 2c 20 61 20 31 or the host, a 1
ec3f0 32 38 2d 62 79 74 65 20 75 6e 69 71 75 65 20 68 28-byte unique h
ec400 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a 20 69 ost id file.** i
ec410 6e 20 74 68 65 20 70 61 74 68 20 64 65 66 69 6e n the path defin
ec420 65 64 20 62 79 20 74 68 65 20 48 4f 53 54 49 44 ed by the HOSTID
ec430 50 41 54 48 20 6d 61 63 72 6f 20 28 64 65 66 61 PATH macro (defa
ec440 75 6c 74 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 ult value is.**
ec450 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 65 73 2f /Library/Caches/
ec460 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c 69 74 .com.apple.sqlit
ec470 65 43 6f 6e 63 68 48 6f 73 74 49 64 29 2e 0a 2a eConchHostId)..*
ec480 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69 6e 67 20 *.** Requesting
ec490 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78 79 20 64 the lock proxy d
ec4a0 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64 69 61 74 oes not immediat
ec4b0 65 6c 79 20 74 61 6b 65 20 74 68 65 20 63 6f 6e ely take the con
ec4c0 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20 6f 6e 6c ch, it is.** onl
ec4d0 79 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 68 65 y taken when the
ec4e0 20 66 69 72 73 74 20 72 65 71 75 65 73 74 20 74 first request t
ec4f0 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61 73 65 20 o lock database
ec500 66 69 6c 65 20 69 73 20 6d 61 64 65 2e 20 20 0a file is made. .
ec510 2a 2a 20 54 68 69 73 20 6d 61 74 63 68 65 73 20 ** This matches
ec520 74 68 65 20 73 65 6d 61 6e 74 69 63 73 20 6f 66 the semantics of
ec530 20 74 68 65 20 74 72 61 64 69 74 69 6f 6e 61 6c the traditional
ec540 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61 76 69 6f locking behavio
ec550 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f 70 65 6e r, where.** open
ec560 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e ing a connection
ec570 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 to a database f
ec580 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 74 61 6b ile does not tak
ec590 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69 74 2e 0a e a lock on it..
ec5a0 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 6c 6f ** The shared lo
ec5b0 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65 6e 20 66 ck and an open f
ec5c0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 61 ile descriptor a
ec5d0 72 65 20 6d 61 69 6e 74 61 69 6e 65 64 20 75 6e re maintained un
ec5e0 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e til .** the conn
ec5f0 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 61 ection to the da
ec600 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 64 tabase is closed
ec610 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f . .**.** The pro
ec620 78 79 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 xy file and the
ec630 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 20 6e 65 lock file are ne
ec640 76 65 72 20 64 65 6c 65 74 65 64 20 73 6f 20 74 ver deleted so t
ec650 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 0a 2a 2a hey only need.**
ec660 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 74 to be created t
ec670 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 he first time th
ec680 65 79 20 61 72 65 20 75 73 65 64 2e 0a 2a 2a 0a ey are used..**.
ec690 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e ** Configuration
ec6a0 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d 2d 2d 2d options.** ----
ec6b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
ec6c0 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54 45 5f -.**.** SQLITE_
ec6d0 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 PREFER_PROXY_LOC
ec6e0 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 KING.**.**
ec6f0 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 Database files
ec700 61 63 63 65 73 73 65 64 20 6f 6e 20 6e 6f 6e 2d accessed on non-
ec710 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 local file syste
ec720 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 20 ms are.**
ec730 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6f automatically co
ec740 6e 66 69 67 75 72 65 64 20 66 6f 72 20 70 72 6f nfigured for pro
ec750 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c 6f 63 6b xy locking, lock
ec760 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a 20 20 20 files are.**
ec770 20 20 20 20 6e 61 6d 65 64 20 61 75 74 6f 6d 61 named automa
ec780 74 69 63 61 6c 6c 79 20 75 73 69 6e 67 20 74 68 tically using th
ec790 65 20 73 61 6d 65 20 6c 6f 67 69 63 20 61 73 0a e same logic as.
ec7a0 2a 2a 20 20 20 20 20 20 20 50 52 41 47 4d 41 20 ** PRAGMA
ec7b0 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 3d lock_proxy_file=
ec7c0 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20 20 20 0a ":auto:".** .
ec7d0 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 ** SQLITE_PROXY
ec7e0 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20 20 20 20 _DEBUG.**.**
ec7f0 20 20 20 45 6e 61 62 6c 65 73 20 74 68 65 20 6c Enables the l
ec800 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 6f 72 20 ogging of error
ec810 6d 65 73 73 61 67 65 73 20 64 75 72 69 6e 67 20 messages during
ec820 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a 20 host id file.**
ec830 20 20 20 20 20 20 72 65 74 72 69 65 76 61 6c 20 retrieval
ec840 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a 2a 2a 0a and creation.**.
ec850 2a 2a 20 20 48 4f 53 54 49 44 50 41 54 48 0a 2a ** HOSTIDPATH.*
ec860 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65 72 72 *.** Overr
ec870 69 64 65 73 20 74 68 65 20 64 65 66 61 75 6c 74 ides the default
ec880 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 70 61 host ID file pa
ec890 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 0a 2a th location.**.*
ec8a0 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 0a * LOCKPROXYDIR.
ec8b0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65 72 **.** Over
ec8c0 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75 6c rides the defaul
ec8d0 74 20 64 69 72 65 63 74 6f 72 79 20 75 73 65 64 t directory used
ec8e0 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f 78 79 20 for lock proxy
ec8f0 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 files that.**
ec900 20 20 20 20 61 72 65 20 6e 61 6d 65 64 20 61 75 are named au
ec910 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76 69 61 20 tomatically via
ec920 74 68 65 20 22 3a 61 75 74 6f 3a 22 20 73 65 74 the ":auto:" set
ec930 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 ting.**.** SQLI
ec940 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 TE_DEFAULT_PROXY
ec950 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a DIR_PERMISSIONS.
ec960 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50 65 72 6d **.** Perm
ec970 69 73 73 69 6f 6e 73 20 74 6f 20 75 73 65 20 77 issions to use w
ec980 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 64 hen creating a d
ec990 69 72 65 63 74 6f 72 79 20 66 6f 72 20 73 74 6f irectory for sto
ec9a0 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20 20 20 20 ring the.**
ec9b0 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c lock proxy fil
ec9c0 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 es, only used wh
ec9d0 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 20 en LOCKPROXYDIR
ec9e0 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a 2a 20 20 is not set..**
ec9f0 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20 41 73 20 .** .** As
eca00 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f 76 65 2c mentioned above,
eca10 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 64 20 77 when compiled w
eca20 69 74 68 20 53 51 4c 49 54 45 5f 50 52 45 46 45 ith SQLITE_PREFE
eca30 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 2c R_PROXY_LOCKING,
eca40 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 68 65 20 .** setting the
eca50 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 72 69 environment vari
eca60 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 4f 52 43 able SQLITE_FORC
eca70 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 E_PROXY_LOCKING
eca80 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20 66 6f 72 to 1 will.** for
eca90 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 ce proxy locking
ecaa0 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 to be used for
ecab0 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20 66 every database f
ecac0 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 6e 64 20 ile opened, and
ecad0 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72 63 65 20 0.** will force
ecae0 61 75 74 6f 6d 61 74 69 63 20 70 72 6f 78 79 20 automatic proxy
ecaf0 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20 64 69 locking to be di
ecb00 73 61 62 6c 65 64 20 66 6f 72 20 61 6c 6c 20 64 sabled for all d
ecb10 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 73 atabase.** files
ecb20 20 28 65 78 70 6c 69 63 69 74 79 20 63 61 6c 6c (explicity call
ecb30 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 53 ing the SQLITE_S
ecb40 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 ET_LOCKPROXYFILE
ecb50 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a 20 73 71 pragma or.** sq
ecb60 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f lite_file_contro
ecb70 6c 20 41 50 49 20 69 73 20 6e 6f 74 20 61 66 66 l API is not aff
ecb80 65 63 74 65 64 20 62 79 20 53 51 4c 49 54 45 5f ected by SQLITE_
ecb90 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b FORCE_PROXY_LOCK
ecba0 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 ING)..*/../*.**
ecbb0 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 Proxy locking is
ecbc0 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
ecbd0 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f 0a 23 69 on MacOSX .*/.#i
ecbe0 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
ecbf0 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
ecc00 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
ecc10 59 4c 45 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 YLE..#ifdef SQLI
ecc20 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69 6d 75 6c TE_TEST./* simul
ecc30 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 68 6f 73 ate multiple hos
ecc40 74 73 20 62 79 20 63 72 65 61 74 69 6e 67 20 75 ts by creating u
ecc50 6e 69 71 75 65 20 68 6f 73 74 69 64 20 66 69 6c nique hostid fil
ecc60 65 20 70 61 74 68 73 20 2a 2f 0a 53 51 4c 49 54 e paths */.SQLIT
ecc70 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
ecc80 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 3d 20 30 3_hostid_num = 0
ecc90 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
ecca0 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 The proxyLocking
eccb0 43 6f 6e 74 65 78 74 20 68 61 73 20 74 68 65 20 Context has the
eccc0 70 61 74 68 20 61 6e 64 20 66 69 6c 65 20 73 74 path and file st
eccd0 72 75 63 74 75 72 65 73 20 66 6f 72 20 74 68 65 ructures for the
ecce0 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 6e 64 20 remote .** and
eccf0 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 69 6c 65 local proxy file
ecd00 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 70 65 64 s in it.*/.typed
ecd10 65 66 20 73 74 72 75 63 74 20 70 72 6f 78 79 4c ef struct proxyL
ecd20 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 70 72 ockingContext pr
ecd30 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
ecd40 74 3b 0a 73 74 72 75 63 74 20 70 72 6f 78 79 4c t;.struct proxyL
ecd50 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a ockingContext {.
ecd60 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 unixFile *conc
ecd70 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 2f hFile; /
ecd80 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 66 69 6c * Open conch fil
ecd90 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63 6f 6e e */. char *con
ecda0 63 68 46 69 6c 65 50 61 74 68 3b 20 20 20 20 20 chFilePath;
ecdb0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
ecdc0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f he conch file */
ecdd0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 . unixFile *loc
ecde0 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 20 20 20 kProxy;
ecdf0 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 20 6c 6f /* Open proxy lo
ece00 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 ck file */. cha
ece10 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 r *lockProxyPath
ece20 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d ; /* Nam
ece30 65 20 6f 66 20 74 68 65 20 70 72 6f 78 79 20 6c e of the proxy l
ece40 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 ock file */. ch
ece50 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 20 20 ar *dbPath;
ece60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
ece70 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 me of the open f
ece80 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e ile */. int con
ece90 63 68 48 65 6c 64 3b 20 20 20 20 20 20 20 20 20 chHeld;
ecea0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
eceb0 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 63 75 the conch is cu
ecec0 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 2a 2f 0a rrently held */.
eced0 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f 63 6b 69 void *oldLocki
ecee0 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 2f ngContext; /
ecef0 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 * Original locki
ecf00 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73 ngcontext to res
ecf10 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f tore on close */
ecf20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 . sqlite3_io_me
ecf30 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4f 6c thods const *pOl
ecf40 64 4d 65 74 68 6f 64 3b 20 20 20 20 20 2f 2a 20 dMethod; /*
ecf50 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20 6d 65 74 Original I/O met
ecf60 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73 65 20 2a hods for close *
ecf70 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54 49 44 4c /.};../* HOSTIDL
ecf80 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c 45 4e 20 EN and CONCHLEN
ecf90 62 6f 74 68 20 69 6e 63 6c 75 64 65 20 73 70 61 both include spa
ecfa0 63 65 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e ce for the strin
ecfb0 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61 74 69 6e g .** terminatin
ecfc0 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65 66 69 6e g nul .*/.#defin
ecfd0 65 20 48 4f 53 54 49 44 4c 45 4e 20 20 20 20 20 e HOSTIDLEN
ecfe0 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 128.#define
ecff0 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20 20 20 20 CONCHLEN
ed000 20 20 28 4d 41 58 50 41 54 48 4c 45 4e 2b 48 4f (MAXPATHLEN+HO
ed010 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69 66 6e 64 STIDLEN+1).#ifnd
ed020 65 66 20 48 4f 53 54 49 44 50 41 54 48 0a 23 20 ef HOSTIDPATH.#
ed030 64 65 66 69 6e 65 20 48 4f 53 54 49 44 50 41 54 define HOSTIDPAT
ed040 48 20 20 20 20 20 20 20 22 2f 4c 69 62 72 61 72 H "/Librar
ed050 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 y/Caches/.com.ap
ed060 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e 63 68 48 ple.sqliteConchH
ed070 6f 73 74 49 64 22 0a 23 65 6e 64 69 66 0a 0a 2f ostId".#endif../
ed080 2a 20 62 61 73 69 63 61 6c 6c 79 20 61 20 63 6f * basically a co
ed090 70 79 20 6f 66 20 75 6e 69 78 52 61 6e 64 6f 6d py of unixRandom
ed0a0 6e 65 73 73 20 77 69 74 68 20 64 69 66 66 65 72 ness with differ
ed0b0 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62 65 68 61 ent.** test beha
ed0c0 76 69 6f 72 20 62 75 69 6c 74 20 69 6e 20 2a 2f vior built in */
ed0d0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 .static int prox
ed0e0 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 44 28 yGenerateHostID(
ed0f0 63 68 61 72 20 2a 70 48 6f 73 74 49 44 29 7b 0a char *pHostID){.
ed100 20 20 69 6e 74 20 70 69 64 2c 20 66 64 2c 20 6c int pid, fd, l
ed110 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 en;. unsigned c
ed120 68 61 72 20 2a 6b 65 79 20 3d 20 28 75 6e 73 69 har *key = (unsi
ed130 67 6e 65 64 20 63 68 61 72 20 2a 29 70 48 6f 73 gned char *)pHos
ed140 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d 73 65 74 tID;. . memset
ed150 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54 49 44 4c (key, 0, HOSTIDL
ed160 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20 30 3b 0a EN);. len = 0;.
ed170 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65 fd = open("/de
ed180 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 v/urandom", O_RD
ed190 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20 66 64 3e ONLY);. if( fd>
ed1a0 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 =0 ){. len =
ed1b0 72 65 61 64 28 66 64 2c 20 6b 65 79 2c 20 48 4f read(fd, key, HO
ed1c0 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 20 63 6c STIDLEN);. cl
ed1d0 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 ose(fd); /* sile
ed1e0 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 66 64 ntly leak the fd
ed1f0 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a 2f 0a if it fails */.
ed200 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e 20 3c 20 }. if( len <
ed210 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 HOSTIDLEN ){.
ed220 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20 74 time_t t;. t
ed230 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 6d 65 6d ime(&t);. mem
ed240 63 70 79 28 6b 65 79 2c 20 26 74 2c 20 73 69 7a cpy(key, &t, siz
ed250 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 70 69 64 eof(t));. pid
ed260 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 20 20 = getpid();.
ed270 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b 73 69 7a memcpy(&key[siz
ed280 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 eof(t)], &pid, s
ed290 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 7d izeof(pid));. }
ed2a0 0a 20 20 0a 23 69 66 64 65 66 20 4d 41 4b 45 5f . .#ifdef MAKE_
ed2b0 50 52 45 54 54 59 5f 48 4f 53 54 49 44 0a 20 20 PRETTY_HOSTID.
ed2c0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
ed2d0 20 2f 2a 20 66 69 6c 74 65 72 20 74 68 65 20 62 /* filter the b
ed2e0 79 74 65 73 20 69 6e 74 6f 20 70 72 69 6e 74 61 ytes into printa
ed2f0 62 6c 65 20 61 73 63 69 69 20 63 68 61 72 61 63 ble ascii charac
ed300 74 65 72 73 20 61 6e 64 20 4e 55 4c 20 74 65 72 ters and NUL ter
ed310 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20 20 6b 65 minate */. ke
ed320 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d 31 29 5d y[(HOSTIDLEN-1)]
ed330 20 3d 20 30 78 30 30 3b 0a 20 20 20 20 66 6f 72 = 0x00;. for
ed340 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53 54 49 44 ( i=0; i<(HOSTID
ed350 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29 7b 0a 20 LEN-1); i++ ){.
ed360 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 unsigned ch
ed370 61 72 20 70 61 20 3d 20 6b 65 79 5b 69 5d 26 30 ar pa = key[i]&0
ed380 78 37 46 3b 0a 20 20 20 20 20 20 69 66 28 20 70 x7F;. if( p
ed390 61 3c 30 78 32 30 20 29 7b 0a 20 20 20 20 20 20 a<0x20 ){.
ed3a0 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65 79 5b key[i] = (key[
ed3b0 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 38 30 29 i]&0x80 == 0x80)
ed3c0 20 3f 20 70 61 2b 30 78 34 30 20 3a 20 70 61 2b ? pa+0x40 : pa+
ed3d0 30 78 32 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 0x20;. }els
ed3e0 65 20 69 66 28 20 70 61 3d 3d 30 78 37 46 20 29 e if( pa==0x7F )
ed3f0 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 5b 69 5d {. key[i]
ed400 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38 30 20 = (key[i]&0x80
ed410 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 3d 30 78 == 0x80) ? pa=0x
ed420 32 30 20 3a 20 70 61 2b 30 78 37 45 3b 0a 20 20 20 : pa+0x7E;.
ed430 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
ed440 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
ed450 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
ed460 20 77 72 69 74 65 73 20 74 68 65 20 68 6f 73 74 writes the host
ed470 20 69 64 20 70 61 74 68 20 74 6f 20 70 61 74 68 id path to path
ed480 2c 20 70 61 74 68 20 73 68 6f 75 6c 64 20 62 65 , path should be
ed490 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63 61 74 65 an pre-allocate
ed4a0 64 20 62 75 66 66 65 72 0a 2a 2a 20 77 69 74 68 d buffer.** with
ed4b0 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 66 6f enough space fo
ed4c0 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a 73 74 61 r a path .*/.sta
ed4d0 74 69 63 20 76 6f 69 64 20 70 72 6f 78 79 47 65 tic void proxyGe
ed4e0 74 48 6f 73 74 49 44 50 61 74 68 28 63 68 61 72 tHostIDPath(char
ed4f0 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f 74 20 6c *path, size_t l
ed500 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70 79 28 70 en){. strlcpy(p
ed510 61 74 68 2c 20 48 4f 53 54 49 44 50 41 54 48 2c ath, HOSTIDPATH,
ed520 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66 20 53 51 len);.#ifdef SQ
ed530 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 LITE_TEST. if(
ed540 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e sqlite3_hostid_n
ed550 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63 68 61 72 um>0 ){. char
ed560 20 73 75 66 66 69 78 5b 32 5d 20 3d 20 22 31 22 suffix[2] = "1"
ed570 3b 0a 20 20 20 20 73 75 66 66 69 78 5b 30 5d 20 ;. suffix[0]
ed580 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b 20 73 71 = suffix[0] + sq
ed590 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d lite3_hostid_num
ed5a0 3b 0a 20 20 20 20 73 74 72 6c 63 61 74 28 70 61 ;. strlcat(pa
ed5b0 74 68 2c 20 73 75 66 66 69 78 2c 20 6c 65 6e 29 th, suffix, len)
ed5c0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 4f ;. }.#endif. O
ed5d0 53 54 52 41 43 45 33 28 22 47 45 54 48 4f 53 54 STRACE3("GETHOST
ed5e0 49 44 50 41 54 48 20 20 25 73 20 70 69 64 3d 25 IDPATH %s pid=%
ed5f0 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67 65 74 70 d\n", path, getp
ed600 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20 67 65 74 id());.}../* get
ed610 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 72 6f the host ID fro
ed620 6d 20 61 20 73 71 6c 69 74 65 20 68 6f 73 74 69 m a sqlite hosti
ed630 64 20 66 69 6c 65 20 73 74 6f 72 65 64 20 69 6e d file stored in
ed640 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 2d 73 70 the .** user-sp
ed650 65 63 69 66 69 63 20 74 6d 70 20 64 69 72 65 63 ecific tmp direc
ed660 74 6f 72 79 2c 20 63 72 65 61 74 65 20 74 68 65 tory, create the
ed670 20 49 44 20 69 66 20 69 74 27 73 20 6e 6f 74 20 ID if it's not
ed680 74 68 65 72 65 20 61 6c 72 65 61 64 79 20 0a 2a there already .*
ed690 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
ed6a0 78 79 47 65 74 48 6f 73 74 49 44 28 63 68 61 72 xyGetHostID(char
ed6b0 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e 74 20 2a *pHostID, int *
ed6c0 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e 74 20 66 pError){. int f
ed6d0 64 3b 0a 20 20 63 68 61 72 20 70 61 74 68 5b 4d d;. char path[M
ed6e0 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a 20 20 73 AXPATHLEN]; . s
ed6f0 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 ize_t len;. int
ed700 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a rc=SQLITE_OK;..
ed710 20 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 proxyGetHostID
ed720 50 61 74 68 28 70 61 74 68 2c 20 4d 41 58 50 41 Path(path, MAXPA
ed730 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20 74 72 79 THLEN);. /* try
ed740 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 68 to create the h
ed750 6f 73 74 20 49 44 20 66 69 6c 65 2c 20 69 66 20 ost ID file, if
ed760 69 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 it already exist
ed770 73 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 s read the conte
ed780 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 nts */. fd = op
ed790 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52 45 41 54 en(path, O_CREAT
ed7a0 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45 58 43 4c |O_WRONLY|O_EXCL
ed7b0 2c 20 30 36 34 34 29 3b 0a 20 20 69 66 28 20 66 , 0644);. if( f
ed7c0 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 d<0 ){. int e
ed7d0 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a 20 20 20 rr=errno;....
ed7e0 20 69 66 28 20 65 72 72 21 3d 45 45 58 49 53 54 if( err!=EEXIST
ed7f0 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ){.#ifdef SQLIT
ed800 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f 2a E_PROXY_DEBUG /*
ed810 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 20 set the sqlite
ed820 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e error message in
ed830 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20 66 stead */. f
ed840 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
ed850 73 71 6c 69 74 65 20 65 72 72 6f 72 20 63 72 65 sqlite error cre
ed860 61 74 69 6e 67 20 68 6f 73 74 20 49 44 20 66 69 ating host ID fi
ed870 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a 20 20 le %s: %s\n",.
ed880 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74 68 path
ed890 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 , strerror(err))
ed8a0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 ;.#endif. r
ed8b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 eturn SQLITE_PER
ed8c0 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 M;. }. /*
ed8d0 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 74 65 20 couldn't create
ed8e0 74 68 65 20 66 69 6c 65 2c 20 72 65 61 64 20 69 the file, read i
ed8f0 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 t instead */.
ed900 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c fd = open(path,
ed910 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45 58 43 4c O_RDONLY|O_EXCL
ed920 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20 );. if( fd<0
ed930 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ){.#ifdef SQLITE
ed940 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f 2a 20 _PROXY_DEBUG /*
ed950 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 20 65 set the sqlite e
ed960 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 73 rror message ins
ed970 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e tead */. in
ed980 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 t err = errno;.
ed990 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
ed9a0 65 72 72 2c 20 22 73 71 6c 69 74 65 20 65 72 72 err, "sqlite err
ed9b0 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f 73 74 20 or opening host
ed9c0 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e ID file %s: %s\n
ed9d0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
ed9e0 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f 72 28 path, strerror(
ed9f0 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 err));.#endif.
eda00 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
eda10 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 E_PERM;. }.
eda20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64 28 66 64 len = pread(fd
eda30 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f 53 54 49 , pHostID, HOSTI
eda40 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 DLEN, 0);. if
eda50 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20 20 20 ( len<0 ){.
eda60 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 72 6e 6f *pError = errno
eda70 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
eda80 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a ITE_IOERR_READ;.
eda90 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 }else if( le
edaa0 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 n<HOSTIDLEN ){.
edab0 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 30 *pError = 0
edac0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
edad0 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
edae0 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 READ;. }.
edaf0 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 close(fd); /* si
edb00 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 lently leak the
edb10 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a fd if it fails *
edb20 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 /. OSTRACE3("
edb30 47 45 54 48 4f 53 54 49 44 20 20 72 65 61 64 20 GETHOSTID read
edb40 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 48 %s pid=%d\n", pH
edb50 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28 29 29 ostID, getpid())
edb60 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
edb70 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
edb80 20 77 65 27 72 65 20 63 72 65 61 74 69 6e 67 20 we're creating
edb90 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65 the host ID file
edba0 20 28 75 73 65 20 61 20 72 61 6e 64 6f 6d 20 73 (use a random s
edbb0 74 72 69 6e 67 20 6f 66 20 62 79 74 65 73 29 20 tring of bytes)
edbc0 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47 65 6e 65 */. proxyGene
edbd0 72 61 74 65 48 6f 73 74 49 44 28 70 48 6f 73 74 rateHostID(pHost
edbe0 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 ID);. len = p
edbf0 77 72 69 74 65 28 66 64 2c 20 70 48 6f 73 74 49 write(fd, pHostI
edc00 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20 30 29 D, HOSTIDLEN, 0)
edc10 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c 30 20 ;. if( len<0
edc20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 ){. *pError
edc30 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
edc40 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc = SQLITE_IOER
edc50 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c R_WRITE;. }el
edc60 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f 53 54 49 se if( len<HOSTI
edc70 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 20 2a 70 DLEN ){. *p
edc80 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 Error = 0;.
edc90 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
edca0 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 0a RR_WRITE;. }.
edcb0 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f close(fd); /
edcc0 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 * silently leak
edcd0 74 68 65 20 66 64 20 69 66 20 69 74 20 66 61 69 the fd if it fai
edce0 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 ls */. OSTRAC
edcf0 45 33 28 22 47 45 54 48 4f 53 54 49 44 20 20 77 E3("GETHOSTID w
edd00 72 6f 74 65 20 25 73 20 70 69 64 3d 25 64 5c 6e rote %s pid=%d\n
edd10 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 74 70 ", pHostID, getp
edd20 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72 id());. retur
edd30 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 n rc;. }.}..sta
edd40 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 74 tic int proxyGet
edd50 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 74 20 63 LockPath(const c
edd60 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61 har *dbPath, cha
edd70 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a 65 5f 74 r *lPath, size_t
edd80 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 6e 74 20 maxLen){. int
edd90 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 4c 65 6e len;. int dbLen
edda0 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 69 66 64 ;. int i;..#ifd
eddb0 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 0a ef LOCKPROXYDIR.
eddc0 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 28 len = strlcpy(
eddd0 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 4f 58 59 lPath, LOCKPROXY
edde0 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 65 DIR, maxLen);.#e
eddf0 6c 73 65 0a 23 20 69 66 64 65 66 20 5f 43 53 5f lse.# ifdef _CS_
ede00 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50 DARWIN_USER_TEMP
ede10 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 63 6f 6e _DIR. {. con
ede20 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f fstr(_CS_DARWIN_
ede30 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 20 6c USER_TEMP_DIR, l
ede40 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 Path, maxLen);.
ede50 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61 74 len = strlcat
ede60 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 74 65 70 (lPath, "sqlitep
ede70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 6e 29 3b locks", maxLen);
ede80 0a 20 20 20 20 69 66 28 20 6d 6b 64 69 72 28 6c . if( mkdir(l
ede90 50 61 74 68 2c 20 53 51 4c 49 54 45 5f 44 45 46 Path, SQLITE_DEF
edea0 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 AULT_PROXYDIR_PE
edeb0 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b 0a 20 20 RMISSIONS) ){.
edec0 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64 69 72 20 /* if mkdir
eded0 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65 20 61 73 fails, handle as
edee0 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72 65 61 74 lock file creat
edef0 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 ion failure */.
edf00 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 65 int err = e
edf10 72 72 6e 6f 3b 0a 23 20 20 69 66 64 65 66 20 53 rrno;.# ifdef S
edf20 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
edf30 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 49 53 if( err!=EEXIS
edf40 54 20 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72 T ){. fpr
edf50 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 70 72 intf(stderr, "pr
edf60 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 3a 20 oxyGetLockPath:
edf70 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29 20 65 72 mkdir(%s,0%o) er
edf80 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c 20 6c 50 ror %d %s\n", lP
edf90 61 74 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ath,.
edfa0 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41 SQLITE_DEFA
edfb0 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 ULT_PROXYDIR_PER
edfc0 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72 2c 20 73 MISSIONS, err, s
edfd0 74 72 65 72 72 6f 72 28 65 72 72 29 29 3b 0a 20 trerror(err));.
edfe0 20 20 20 20 20 7d 0a 23 20 20 65 6e 64 69 66 0a }.# endif.
edff0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
ee000 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 4c 4f OSTRACE3("GETLO
ee010 43 4b 50 41 54 48 20 20 6d 6b 64 69 72 20 25 73 CKPATH mkdir %s
ee020 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c 50 61 74 pid=%d\n", lPat
ee030 68 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 h, getpid());.
ee040 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a 23 20 65 }. . }.# e
ee050 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c lse. len = strl
ee060 63 70 79 28 6c 50 61 74 68 2c 20 22 2f 74 6d 70 cpy(lPath, "/tmp
ee070 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 20 65 /", maxLen);.# e
ee080 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 69 ndif.#endif.. i
ee090 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31 5d 21 f( lPath[len-1]!
ee0a0 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 65 6e 20 ='/' ){. len
ee0b0 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c = strlcat(lPath,
ee0c0 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 "/", maxLen);.
ee0d0 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 61 6e 73 }. . /* trans
ee0e0 66 6f 72 6d 20 74 68 65 20 64 62 20 70 61 74 68 form the db path
ee0f0 20 74 6f 20 61 20 75 6e 69 71 75 65 20 63 61 63 to a unique cac
ee100 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 62 4c he name */. dbL
ee110 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e en = (int)strlen
ee120 28 64 62 50 61 74 68 29 3b 0a 20 20 66 6f 72 28 (dbPath);. for(
ee130 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e 20 26 26 i=0; i<dbLen &&
ee140 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61 78 4c 65 (i+len+7)<maxLe
ee150 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 n; i++){. cha
ee160 72 20 63 20 3d 20 64 62 50 61 74 68 5b 69 5d 3b r c = dbPath[i];
ee170 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b 6c 65 6e . lPath[i+len
ee180 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f 27 5f 27 ] = (c=='/')?'_'
ee190 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 74 68 5b :c;. }. lPath[
ee1a0 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a 20 20 73 i+len]='\0';. s
ee1b0 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 3a trlcat(lPath, ":
ee1c0 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 6e 29 3b auto:", maxLen);
ee1d0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
ee1e0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 _OK;.}../*.** Cr
ee1f0 65 61 74 65 20 61 20 6e 65 77 20 56 46 53 20 66 eate a new VFS f
ee200 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 28 ile descriptor (
ee210 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 stored in memory
ee220 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
ee230 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 * sqlite3_malloc
ee240 29 20 61 6e 64 20 6f 70 65 6e 20 74 68 65 20 66 ) and open the f
ee250 69 6c 65 20 6e 61 6d 65 64 20 22 70 61 74 68 22 ile named "path"
ee260 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 in the file des
ee270 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 criptor..**.** T
ee280 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 he caller is res
ee290 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 6f 6e 6c ponsible not onl
ee2a0 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67 20 74 68 y for closing th
ee2b0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
ee2c0 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f 20 66 6f r.** but also fo
ee2d0 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 6d 65 r freeing the me
ee2e0 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 mory associated
ee2f0 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 64 65 with the file de
ee300 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a 73 74 61 scriptor..*/.sta
ee310 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 72 65 tic int proxyCre
ee320 61 74 65 55 6e 69 78 46 69 6c 65 28 63 6f 6e 73 ateUnixFile(cons
ee330 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 75 6e t char *path, un
ee340 69 78 46 69 6c 65 20 2a 2a 70 70 46 69 6c 65 29 ixFile **ppFile)
ee350 20 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 69 {. int fd;. i
ee360 6e 74 20 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 nt dirfd = -1;.
ee370 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 3b unixFile *pNew;
ee380 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
ee390 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 TE_OK;. sqlite3
ee3a0 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 3b 0a 0a _vfs dummyVfs;..
ee3b0 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 68 fd = open(path
ee3c0 2c 20 4f 5f 52 44 57 52 20 7c 20 4f 5f 43 52 45 , O_RDWR | O_CRE
ee3d0 41 54 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 AT, SQLITE_DEFAU
ee3e0 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 LT_FILE_PERMISSI
ee3f0 4f 4e 53 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 ONS);. if( fd<0
ee400 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
ee410 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
ee420 20 20 7d 0a 20 20 0a 20 20 70 4e 65 77 20 3d 20 }. . pNew =
ee430 28 75 6e 69 78 46 69 6c 65 20 2a 29 73 71 6c 69 (unixFile *)sqli
ee440 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f te3_malloc(sizeo
ee450 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 f(unixFile));.
ee460 69 66 28 20 70 4e 65 77 3d 3d 4e 55 4c 4c 20 29 if( pNew==NULL )
ee470 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
ee480 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 E_NOMEM;. got
ee490 6f 20 65 6e 64 5f 63 72 65 61 74 65 5f 70 72 6f o end_create_pro
ee4a0 78 79 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 xy;. }. memset
ee4b0 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 (pNew, 0, sizeof
ee4c0 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 (unixFile));..
ee4d0 64 75 6d 6d 79 56 66 73 2e 70 41 70 70 44 61 74 dummyVfs.pAppDat
ee4e0 61 20 3d 20 28 76 6f 69 64 2a 29 26 61 75 74 6f a = (void*)&auto
ee4f0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b 0a 20 20 lockIoFinder;.
ee500 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 rc = fillInUnixF
ee510 69 6c 65 28 26 64 75 6d 6d 79 56 66 73 2c 20 66 ile(&dummyVfs, f
ee520 64 2c 20 64 69 72 66 64 2c 20 28 73 71 6c 69 74 d, dirfd, (sqlit
ee530 65 33 5f 66 69 6c 65 2a 29 70 4e 65 77 2c 20 70 e3_file*)pNew, p
ee540 61 74 68 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 ath, 0, 0);. if
ee550 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
ee560 29 7b 0a 20 20 20 20 2a 70 70 46 69 6c 65 20 3d ){. *ppFile =
ee570 20 70 4e 65 77 3b 0a 20 20 20 20 72 65 74 75 72 pNew;. retur
ee580 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
ee590 0a 65 6e 64 5f 63 72 65 61 74 65 5f 70 72 6f 78 .end_create_prox
ee5a0 79 3a 20 20 20 20 0a 20 20 63 6c 6f 73 65 28 66 y: . close(f
ee5b0 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 d); /* silently
ee5c0 6c 65 61 6b 20 66 64 20 69 66 20 65 72 72 6f 72 leak fd if error
ee5d0 2c 20 77 65 27 72 65 20 61 6c 72 65 61 64 79 20 , we're already
ee5e0 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73 71 in error */. sq
ee5f0 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 lite3_free(pNew)
ee600 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
ee610 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 63 ../* takes the c
ee620 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61 onch by taking a
ee630 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 shared lock and
ee640 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e read the conten
ee650 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a ts conch, if .**
ee660 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e lockPath is non
ee670 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20 -NULL, the host
ee680 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 ID and lock file
ee690 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68 path must match
ee6a0 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f . A NULL .** lo
ee6b0 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61 ckPath means tha
ee6c0 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69 t the lockPath i
ee6d0 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 n the conch file
ee6e0 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66 will be used if
ee6f0 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44 the .** host ID
ee700 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65 s match, or a ne
ee710 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c w lock path will
ee720 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75 be generated au
ee730 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 tomatically .**
ee740 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74 and written to t
ee750 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a he conch file..*
ee760 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
ee770 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78 xyTakeConch(unix
ee780 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 File *pFile){.
ee790 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
ee7a0 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
ee7b0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
ee7c0 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
ee7d0 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20 gContext; . .
ee7e0 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 if( pCtx->conchH
ee7f0 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 eld>0 ){. ret
ee800 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
ee810 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 }else{. unix
ee820 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 File *conchFile
ee830 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c = pCtx->conchFil
ee840 65 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 74 e;. char test
ee850 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b Value[CONCHLEN];
ee860 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 56 . char conchV
ee870 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a alue[CONCHLEN];.
ee880 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 74 char lockPat
ee890 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 h[MAXPATHLEN];.
ee8a0 20 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 61 char *tLockPa
ee8b0 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 th = NULL;. i
ee8c0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
ee8d0 4b 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 52 K;. int readR
ee8e0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
ee8f0 20 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d 73 int syncPerms
ee900 20 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 41 = 0;.. OSTRA
ee910 43 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 CE4("TAKECONCH
ee920 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 %d for %s pid=%d
ee930 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e \n", conchFile->
ee940 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 h,.
ee950 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 (pCtx->lockProxy
ee960 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 Path ? pCtx->loc
ee970 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61 kProxyPath : ":a
ee980 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 uto:"), getpid()
ee990 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e );.. rc = con
ee9a0 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d chFile->pMethod-
ee9b0 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f >xLock((sqlite3_
ee9c0 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c file*)conchFile,
ee9d0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 SHARED_LOCK);.
ee9e0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
ee9f0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e E_OK ){. in
eea00 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 t pError = 0;.
eea10 20 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 56 memset(testV
eea20 61 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c 45 alue, 0, CONCHLE
eea30 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 20 N); /* conch is
eea40 66 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 20 fixed size */.
eea50 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 65 rc = proxyGe
eea60 74 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c 75 tHostID(testValu
eea70 65 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 20 e, &pError);.
eea80 20 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 if( (rc&0xff)
eea90 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 ==SQLITE_IOERR )
eeaa0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
eeab0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 72 >lastErrno = pEr
eeac0 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ror;. }.
eead0 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 if( pCtx->loc
eeae0 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 kProxyPath ){.
eeaf0 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74 strlcpy(&t
eeb00 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c estValue[HOSTIDL
eeb10 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 EN], pCtx->lockP
eeb20 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 roxyPath, MAXPAT
eeb30 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 HLEN);. }.
eeb40 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
eeb50 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
eeb60 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b goto end_tak
eeb70 65 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 econch;. }.
eeb80 20 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d 20 . readRc =
eeb90 75 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 65 unixRead((sqlite
eeba0 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 3_file *)conchFi
eebb0 6c 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 le, conchValue,
eebc0 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 CONCHLEN, 0);.
eebd0 20 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 if( readRc!=SQ
eebe0 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
eebf0 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 69 _READ ){. i
eec00 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54 f( readRc!=SQLIT
eec10 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
eec20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 if( (rc&0xff)==S
eec30 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 QLITE_IOERR ){.
eec40 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e pFile->
eec50 6c 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e 63 lastErrno = conc
eec60 68 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f hFile->lastErrno
eec70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
eec80 20 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 3b rc = readRc;
eec90 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e . goto en
eeca0 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 d_takeconch;.
eecb0 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 66 }. /* if
eecc0 20 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 64 the conch has d
eecd0 61 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 20 ata compare the
eece0 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 20 contents */.
eecf0 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 if( !pCtx->loc
eed00 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 kProxyPath ){.
eed10 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74 /* for aut
eed20 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f o-named local lo
eed30 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68 ck file, just ch
eed40 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20 eck the host ID
eed50 61 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20 and we'll.
eed60 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c 6f ** use the lo
eed70 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 cal lock file pa
eed80 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 64 th that's alread
eed90 79 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 20 y in there */.
eeda0 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d if( !memcm
eedb0 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e p(testValue, con
eedc0 63 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 4c chValue, HOSTIDL
eedd0 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 EN) ){.
eede0 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68 tLockPath = (ch
eedf0 61 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 65 ar *)&conchValue
eee00 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 20 [HOSTIDLEN];.
eee10 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
eee20 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 takeconch;.
eee30 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
eee40 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27 {. /* we'
eee50 76 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 68 ve got the conch
eee60 20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 6d if conchValue m
eee70 61 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 20 atches our path
eee80 61 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a 20 and host ID */.
eee90 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 if( !memc
eeea0 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f mp(testValue, co
eeeb0 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c nchValue, CONCHL
eeec0 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 EN) ){.
eeed0 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f goto end_takeco
eeee0 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 nch;. }.
eeef0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
eef00 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 6f {. /* a sho
eef10 72 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 65 rt read means we
eef20 27 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 74 're "creating" t
eef30 68 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 74 he conch (even t
eef40 68 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 0a hough it could .
eef50 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 65 ** have be
eef60 65 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 6e en user-interven
eef70 74 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 71 tion), if we acq
eef80 75 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 69 uire the exclusi
eef90 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 2a ve lock,. *
eefa0 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 6d * we'll try to m
eefb0 61 74 63 68 20 74 68 65 20 63 75 72 72 65 6e 74 atch the current
eefc0 20 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 73 on-disk permiss
eefd0 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 61 ions of the data
eefe0 62 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 base. */.
eeff0 20 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d 20 syncPerms =
ef000 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 1;. }. .
ef010 20 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e 63 /* either conc
ef020 68 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 64 h was emtpy or d
ef030 69 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a 20 idn't match */.
ef040 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f if( !pCtx->lo
ef050 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 ckProxyPath ){.
ef060 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f 63 proxyGetLoc
ef070 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 61 kPath(pCtx->dbPa
ef080 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 th, lockPath, MA
ef090 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 XPATHLEN);.
ef0a0 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f 63 tLockPath = loc
ef0b0 6b 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 72 kPath;. str
ef0c0 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b lcpy(&testValue[
ef0d0 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b HOSTIDLEN], lock
ef0e0 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e Path, MAXPATHLEN
ef0f0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 );. }. .
ef100 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e 63 /* update conc
ef110 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 20 h with host and
ef120 70 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c 20 path (this will
ef130 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 72 fail if other pr
ef140 6f 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 61 ocess. ** ha
ef150 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 s a shared lock
ef160 61 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 20 already) */.
ef170 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e rc = conchFile->
ef180 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 pMethod->xLock((
ef190 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f sqlite3_file*)co
ef1a0 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49 nchFile, EXCLUSI
ef1b0 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 VE_LOCK);. if
ef1c0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
ef1d0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e ){. rc = un
ef1e0 69 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33 ixWrite((sqlite3
ef1f0 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c _file *)conchFil
ef200 65 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 4f e, testValue, CO
ef210 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 NCHLEN, 0);.
ef220 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
ef230 5f 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d 73 _OK && syncPerms
ef240 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 ){. stru
ef250 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20 ct stat buf;.
ef260 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 66 int err = f
ef270 73 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 stat(pFile->h, &
ef280 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 buf);. if
ef290 28 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 ( err==0 ){.
ef2a0 20 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f 20 /* try to
ef2b0 6d 61 74 63 68 20 74 68 65 20 64 61 74 61 62 61 match the databa
ef2c0 73 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 69 se file permissi
ef2d0 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 6c ons, ignore fail
ef2e0 75 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ure */.#ifndef S
ef2f0 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 QLITE_PROXY_DEBU
ef300 47 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 6d G. fchm
ef310 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c od(conchFile->h,
ef320 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23 buf.st_mode);.#
ef330 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 69 else. i
ef340 66 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 f( fchmod(conchF
ef350 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d ile->h, buf.st_m
ef360 6f 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ode)!=0 ){.
ef370 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 int code
ef380 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
ef390 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
ef3a0 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 err, "fchmod %o
ef3b0 46 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 25 FAILED with %d %
ef3c0 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\n",.
ef3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef3e0 20 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c 20 buf.st_mode,
ef3f0 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 code, strerror(c
ef400 6f 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 ode));.
ef410 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
ef420 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 fprintf(st
ef430 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f derr, "fchmod %o
ef440 20 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 66 SUCCEDED\n",buf
ef450 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 20 .st_mode);.
ef460 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
ef470 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
ef480 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f int code = errno
ef490 3b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 ;. fpri
ef4a0 6e 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 41 ntf(stderr, "STA
ef4b0 54 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 74 T FAILED[%d] wit
ef4c0 68 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 20 h %d %s\n", .
ef4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef4e0 20 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 65 err, code
ef4f0 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29 , strerror(code)
ef500 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 );.#endif.
ef510 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
ef520 7d 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d }. conchFile-
ef530 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 >pMethod->xUnloc
ef540 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
ef550 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 )conchFile, SHAR
ef560 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64 ED_LOCK);. .end
ef570 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 20 _takeconch:.
ef580 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50 OSTRACE2("TRANSP
ef590 52 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c ROXY: CLOSE %d\
ef5a0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 n", pFile->h);.
ef5b0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
ef5c0 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6f E_OK && pFile->o
ef5d0 70 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 penFlags ){.
ef5e0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d if( pFile->h>=
ef5f0 30 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 49 0 ){.#ifdef STRI
ef600 43 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a 20 CT_CLOSE_ERROR.
ef610 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 if( close
ef620 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 (pFile->h) ){.
ef630 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
ef640 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
ef650 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
ef660 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
ef670 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 7d CLOSE;. }
ef680 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 63 .#else. c
ef690 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20 lose(pFile->h);
ef6a0 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b /* silently leak
ef6b0 20 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a 23 fd if fail */.#
ef6c0 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 endif. }.
ef6d0 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d pFile->h = -
ef6e0 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 20 1;. int fd
ef6f0 3d 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 50 = open(pCtx->dbP
ef700 61 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 6e ath, pFile->open
ef710 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 Flags,.
ef720 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 SQLIT
ef730 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 E_DEFAULT_FILE_P
ef740 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 ERMISSIONS);.
ef750 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 OSTRACE2("TRA
ef760 4e 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 25 NSPROXY: OPEN %
ef770 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 20 d\n", fd);.
ef780 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 if( fd>=0 ){.
ef790 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d pFile->h =
ef7a0 20 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 fd;. }else
ef7b0 7b 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c {. rc=SQL
ef7c0 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a ITE_CANTOPEN; /*
ef7d0 20 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72 SQLITE_BUSY? pr
ef7e0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c oxyTakeConch cal
ef7f0 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 led.
ef800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef810 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e during lockin
ef820 67 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 g */. }.
ef830 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
ef840 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 74 QLITE_OK && !pCt
ef850 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a x->lockProxy ){.
ef860 20 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 68 char *path
ef870 20 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 74 = tLockPath ? t
ef880 4c 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d LockPath : pCtx-
ef890 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a >lockProxyPath;.
ef8a0 20 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e 65 /* ACS: Ne
ef8b0 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70 ed to make a cop
ef8c0 79 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 69 y of path someti
ef8d0 6d 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 mes */. rc
ef8e0 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 = proxyCreateUni
ef8f0 78 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 74 xFile(path, &pCt
ef900 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 x->lockProxy);.
ef910 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
ef920 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
ef930 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 pCtx->conchH
ef940 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 eld = 1;..
ef950 69 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 7b if( tLockPath ){
ef960 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c . pCtx->l
ef970 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 ockProxyPath = s
ef980 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 qlite3DbStrDup(0
ef990 2c 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 20 , tLockPath);.
ef9a0 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e if( pCtx->
ef9b0 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 lockProxy->pMeth
ef9c0 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 od == &afpIoMeth
ef9d0 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ods ){.
ef9e0 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e ((afpLockingCon
ef9f0 74 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63 text *)pCtx->loc
efa00 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43 kProxy->lockingC
efa10 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 20 ontext)->dbPath
efa20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 =.
efa30 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 pCtx->loc
efa40 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 kProxyPath;.
efa50 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
efa60 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
efa70 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 conchFile->pMet
efa80 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 hod->xUnlock((sq
efa90 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 lite3_file*)conc
efaa0 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b hFile, NO_LOCK);
efab0 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 . }. OSTRA
efac0 43 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 CE3("TAKECONCH
efad0 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 %d %s\n", conchF
efae0 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c 49 ile->h, rc==SQLI
efaf0 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c TE_OK?"ok":"fail
efb00 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ed");. return
efb10 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a rc;. }.}../*.*
efb20 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73 * If pFile holds
efb30 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e a lock on a con
efb40 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65 ch file, then re
efb50 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e lease that lock.
efb60 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
efb70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 roxyReleaseConch
efb80 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
efb90 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
efba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
efbb0 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 72 /* Subroutine r
efbc0 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
efbd0 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
efbe0 65 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 54 ext *pCtx; /* T
efbf0 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 he locking conte
efc00 78 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 xt for the proxy
efc10 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 46 lock */. unixF
efc20 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 ile *conchFile;
efc30 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
efc40 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 f the conch file
efc50 20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 70 */.. pCtx = (p
efc60 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
efc70 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b xt *)pFile->lock
efc80 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 6f ingContext;. co
efc90 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e nchFile = pCtx->
efca0 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 54 conchFile;. OST
efcb0 52 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 4f RACE4("RELEASECO
efcc0 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 NCH %d for %s p
efcd0 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 id=%d\n", conchF
efce0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
efcf0 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 (pCtx->lockPr
efd00 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e oxyPath ? pCtx->
efd10 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 lockProxyPath :
efd20 22 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 20 ":auto:"), .
efd30 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29 getpid())
efd40 3b 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 ;. pCtx->conchH
efd50 65 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 eld = 0;. rc =
efd60 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 conchFile->pMeth
efd70 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c od->xUnlock((sql
efd80 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 ite3_file*)conch
efd90 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a File, NO_LOCK);.
efda0 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c 45 OSTRACE3("RELE
efdb0 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c ASECONCH %d %s\
efdc0 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 n", conchFile->h
efdd0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63 ,. (rc
efde0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f ==SQLITE_OK ? "o
efdf0 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b k" : "failed"));
efe00 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
efe10 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 ./*.** Given the
efe20 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 name of a datab
efe30 61 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 74 ase file, comput
efe40 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 74 e the name of it
efe50 73 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a s conch file..**
efe60 20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 68 Store the conch
efe70 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 6d filename in mem
efe80 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
efe90 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
efea0 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f ()..** Make *pCo
efeb0 6e 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 6f nchPath point to
efec0 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 20 the new name.
efed0 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
efee0 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f on success.** o
efef0 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 r SQLITE_NOMEM i
eff00 66 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 f unable to obta
eff10 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a in memory..**.**
eff20 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 The caller is r
eff30 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65 esponsible for e
eff40 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65 nsuring that the
eff50 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 allocated memor
eff60 79 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76 y.** space is ev
eff70 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a entually freed..
eff80 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 74 **.** *pConchPat
eff90 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c h is set to NULL
effa0 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c if a memory all
effb0 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 ocation error oc
effc0 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 curs..*/.static
effd0 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 43 int proxyCreateC
effe0 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 61 onchPathname(cha
efff0 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 r *dbPath, char
f0000 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a 20 **pConchPath){.
f0010 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
f0020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f0030 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
f0040 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 69 /. int len = (i
f0050 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68 nt)strlen(dbPath
f0060 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 ); /* Length of
f0070 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d database filenam
f0080 65 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 20 e - dbPath */.
f0090 63 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 3b char *conchPath;
f00a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f00b0 20 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 68 buffer in which
f00c0 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 6f to construct co
f00d0 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f nch name */.. /
f00e0 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 * Allocate space
f00f0 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66 for the conch f
f0100 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 74 ilename and init
f0110 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 20 ialize the name
f0120 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 to. ** the name
f0130 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c of the original
f0140 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
f0150 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 61 */ . *pConchPa
f0160 74 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 3d th = conchPath =
f0170 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 (char *)sqlite3
f0180 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 29 _malloc(len + 8)
f0190 3b 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 ;. if( conchPat
f01a0 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 h==0 ){. retu
f01b0 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
f01c0 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 6f . }. memcpy(co
f01d0 6e 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c nchPath, dbPath,
f01e0 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a len+1);. . /*
f01f0 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e now insert a ".
f0200 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73 " before the las
f0210 74 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f t / character */
f0220 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d 31 . for( i=(len-1
f0230 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a ); i>=0; i-- ){.
f0240 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 if( conchPat
f0250 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 h[i]=='/' ){.
f0260 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 72 i++;. br
f0270 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
f0280 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 2e conchPath[i]='.
f0290 27 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c 6c ';. while ( i<l
f02a0 65 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 50 en ){. conchP
f02b0 61 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 5b ath[i+1]=dbPath[
f02c0 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d i];. i++;. }
f02d0 0a 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 68 .. /* append th
f02e0 65 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 69 e "-conch" suffi
f02f0 78 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f x to the file */
f0300 0a 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68 . memcpy(&conch
f0310 50 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e Path[i+1], "-con
f0320 63 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 72 ch", 7);. asser
f0330 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63 t( (int)strlen(c
f0340 6f 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e onchPath) == len
f0350 2b 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 +7 );.. return
f0360 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
f0370 2a 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 20 * Takes a fully
f0380 63 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 79 configured proxy
f0390 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 75 locking-style u
f03a0 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 69 nix file and swi
f03b0 74 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f 63 tches.** the loc
f03c0 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 al lock file pat
f03d0 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 h .*/.static int
f03e0 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 switchLockProxy
f03f0 50 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 Path(unixFile *p
f0400 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 File, const char
f0410 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 *path) {. prox
f0420 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f0430 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f *pCtx = (proxyLo
f0440 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 ckingContext*)pF
f0450 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
f0460 65 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c 64 ext;. char *old
f0470 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 Path = pCtx->loc
f0480 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 6e kProxyPath;. in
f0490 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
f04a0 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e ;.. if( pFile->
f04b0 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 locktype!=NO_LOC
f04c0 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
f04d0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
f04e0 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 .. /* nothing
f04f0 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 61 to do if the pa
f0500 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 th is NULL, :aut
f0510 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 68 o: or matches th
f0520 65 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 20 e existing path
f0530 2a 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c */. if( !path |
f0540 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 | path[0]=='\0'
f0550 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c || !strcmp(path,
f0560 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 ":auto:") ||.
f0570 20 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 73 (oldPath && !s
f0580 74 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c 20 trncmp(oldPath,
f0590 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e path, MAXPATHLEN
f05a0 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e )) ){. return
f05b0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
f05c0 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c lse{. unixFil
f05d0 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 e *lockProxy = p
f05e0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a Ctx->lockProxy;.
f05f0 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 pCtx->lockPr
f0600 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 oxy=NULL;. pC
f0610 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 tx->conchHeld =
f0620 30 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 0;. if( lockP
f0630 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 roxy!=NULL ){.
f0640 20 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 rc=lockProxy
f0650 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 ->pMethod->xClos
f0660 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 e((sqlite3_file
f0670 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 *)lockProxy);.
f0680 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
f0690 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 urn rc;. sq
f06a0 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 lite3_free(lockP
f06b0 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 roxy);. }.
f06c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c sqlite3_free(ol
f06d0 64 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78 dPath);. pCtx
f06e0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
f06f0 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
f0700 70 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d 0a p(0, path);. }.
f0710 20 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
f0720 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 }../*.** pFile i
f0730 73 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 s a file that ha
f0740 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 s been opened by
f0750 20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 63 a prior xOpen c
f0760 61 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a 20 all. dbPath.**
f0770 69 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 is a string buff
f0780 65 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 50 er at least MAXP
f0790 41 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 74 ATHLEN+1 charact
f07a0 65 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a ers in size..**.
f07b0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
f07c0 66 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d find the filenam
f07d0 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
f07e0 68 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 74 h pFile and writ
f07f0 65 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 50 es it.** int dbP
f0800 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ath..*/.static i
f0810 6e 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 74 nt proxyGetDbPat
f0820 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e 69 hForUnixFile(uni
f0830 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68 xFile *pFile, ch
f0840 61 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 66 ar *dbPath){.#if
f0850 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
f0860 5f 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 2d __). if( pFile-
f0870 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 >pMethod == &afp
f0880 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 IoMethods ){.
f0890 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65 /* afp style ke
f08a0 65 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 eps a reference
f08b0 74 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69 to the db path i
f08c0 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66 n the filePath f
f08d0 69 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 ield . ** of
f08e0 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 the struct */.
f08f0 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73 assert( (int)s
f0900 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 trlen((char*)pFi
f0910 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f0920 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 xt)<=MAXPATHLEN
f0930 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 );. strcpy(db
f0940 50 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69 Path, ((afpLocki
f0950 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
f0960 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f0970 74 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 7d t)->dbPath);. }
f0980 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 66 else.#endif. if
f0990 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 ( pFile->pMethod
f09a0 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 == &dotlockIoMe
f09b0 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 thods ){. /*
f09c0 64 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 75 dot lock style u
f09d0 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ses the locking
f09e0 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 context to store
f09f0 20 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 20 the dot lock.
f0a00 20 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 2a ** file path *
f0a10 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 /. int len =
f0a20 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70 strlen((char *)p
f0a30 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
f0a40 74 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 44 text) - strlen(D
f0a50 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b 0a OTLOCK_SUFFIX);.
f0a60 20 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 74 memcpy(dbPat
f0a70 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 h, (char *)pFile
f0a80 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
f0a90 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d 65 , len + 1);. }e
f0aa0 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 lse{. /* all
f0ab0 6f 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 65 other styles use
f0ac0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e the locking con
f0ad0 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68 text to store th
f0ae0 65 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 2a e db file path *
f0af0 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74 /. assert( st
f0b00 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c rlen((char*)pFil
f0b10 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f0b20 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 t)<=MAXPATHLEN )
f0b30 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50 ;. strcpy(dbP
f0b40 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 ath, (char *)pFi
f0b50 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f0b60 78 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 xt);. }. retur
f0b70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
f0b80 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61 /*.** Takes an a
f0b90 6c 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e lready filled in
f0ba0 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61 unix file and a
f0bb0 6c 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20 lters it so all
f0bc0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a file locking .**
f0bd0 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d will be perform
f0be0 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 ed on the local
f0bf0 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e proxy lock file.
f0c00 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
f0c10 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 72 fields.** are pr
f0c20 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 6c eserved in the l
f0c30 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 73 ocking context s
f0c40 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20 o that they can
f0c50 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20 be restored and
f0c60 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 72 .** the unix str
f0c70 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 20 ucture properly
f0c80 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 6c cleaned up at cl
f0c90 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e ose time:.** ->
f0ca0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 2a lockingContext.*
f0cb0 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a * ->pMethod.*/.
f0cc0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
f0cd0 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c TransformUnixFil
f0ce0 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c e(unixFile *pFil
f0cf0 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 e, const char *p
f0d00 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f ath) {. proxyLo
f0d10 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
f0d20 74 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 74 tx;. char dbPat
f0d30 68 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d 3b h[MAXPATHLEN+1];
f0d40 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
f0d50 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
f0d60 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c ile */. char *l
f0d70 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 ockPath=NULL;.
f0d80 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
f0d90 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 69 OK;. . if( pFi
f0da0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f le->locktype!=NO
f0db0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 _LOCK ){. ret
f0dc0 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b urn SQLITE_BUSY;
f0dd0 0a 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 44 . }. proxyGetD
f0de0 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 bPathForUnixFile
f0df0 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b (pFile, dbPath);
f0e00 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 . if( !path ||
f0e10 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c path[0]=='\0' ||
f0e20 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 !strcmp(path, "
f0e30 3a 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 20 :auto:") ){.
f0e40 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 lockPath=NULL;.
f0e50 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b }else{. lock
f0e60 50 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 74 Path=(char *)pat
f0e70 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 h;. }. . OSTR
f0e80 41 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 59 ACE4("TRANSPROXY
f0e90 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d %d for %s pid=
f0ea0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
f0eb0 0a 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f 63 . (loc
f0ec0 6b 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 68 kPath ? lockPath
f0ed0 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 : ":auto:"), ge
f0ee0 74 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 78 tpid());.. pCtx
f0ef0 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
f0f00 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 c( sizeof(*pCtx)
f0f10 20 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d 3d );. if( pCtx==
f0f20 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
f0f30 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
f0f40 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c }. memset(pCtx,
f0f50 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 0, sizeof(*pCtx
f0f60 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f 78 ));.. rc = prox
f0f70 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68 yCreateConchPath
f0f80 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 43 name(dbPath, &pC
f0f90 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 tx->conchFilePat
f0fa0 68 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 h);. if( rc==SQ
f0fb0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
f0fc0 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 c = proxyCreateU
f0fd0 6e 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 6f nixFile(pCtx->co
f0fe0 6e 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 43 nchFilePath, &pC
f0ff0 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a tx->conchFile);.
f1000 20 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d 3d } . if( rc==
f1010 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 SQLITE_OK && loc
f1020 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 74 kPath ){. pCt
f1030 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
f1040 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
f1050 75 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 3b up(0, lockPath);
f1060 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d . }.. if( rc==
f1070 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
f1080 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 /* all memory i
f1090 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f s allocated, pro
f10a0 78 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20 xys are created
f10b0 61 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20 and assigned, .
f10c0 20 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65 ** switch the
f10d0 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
f10e0 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 and pMethod the
f10f0 6e 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f n return.. */
f1100 0a 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74 . pCtx->dbPat
f1110 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 h = sqlite3DbStr
f1120 44 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b 0a Dup(0, dbPath);.
f1130 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 pCtx->oldLoc
f1140 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46 kingContext = pF
f1150 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
f1160 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ext;. pFile->
f1170 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d lockingContext =
f1180 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d pCtx;. pCtx-
f1190 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46 >pOldMethod = pF
f11a0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20 ile->pMethod;.
f11b0 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 pFile->pMethod
f11c0 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f = &proxyIoMetho
f11d0 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ds;. }else{.
f11e0 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 if( pCtx->conch
f11f0 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 72 File ){ . r
f1200 63 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 c = pCtx->conchF
f1210 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 ile->pMethod->xC
f1220 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 lose((sqlite3_fi
f1230 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68 le *)pCtx->conch
f1240 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 File);. if(
f1250 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
f1260 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
f1270 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 ree(pCtx->conchF
f1280 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ile);. }.
f1290 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 sqlite3_free(pCt
f12a0 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 x->conchFilePath
f12b0 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ); . sqlite3_
f12c0 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d 0a free(pCtx);. }.
f12d0 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 4e OSTRACE3("TRAN
f12e0 53 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e 22 SPROXY %d %s\n"
f12f0 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 , pFile->h,.
f1300 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 (rc==SQLI
f1310 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 TE_OK ? "ok" : "
f1320 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 failed"));. ret
f1330 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
f1340 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 * This routine h
f1350 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66 andles sqlite3_f
f1360 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61 ile_control() ca
f1370 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65 lls that are spe
f1380 63 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 cific.** to prox
f1390 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 y locking..*/.st
f13a0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 atic int proxyFi
f13b0 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
f13c0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
f13d0 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b op, void *pArg){
f13e0 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b . switch( op ){
f13f0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
f1400 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _GET_LOCKPROXYFI
f1410 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 LE: {. unix
f1420 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
f1430 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 nixFile*)id;.
f1440 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d if( pFile->pM
f1450 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 ethod == &proxyI
f1460 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
f1470 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 proxyLocking
f1480 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 Context *pCtx =
f1490 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e (proxyLockingCon
f14a0 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 text*)pFile->loc
f14b0 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 kingContext;.
f14c0 20 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 6f proxyTakeCo
f14d0 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 nch(pFile);.
f14e0 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f if( pCtx->lo
f14f0 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 ckProxyPath ){.
f1500 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 *(const
f1510 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 char **)pArg =
f1520 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
f1530 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c ath;. }el
f1540 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 se{. *(
f1550 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 const char **)pA
f1560 72 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f rg = ":auto: (no
f1570 74 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 20 t held)";.
f1580 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 }. } else
f1590 20 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e {. *(con
f15a0 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 st char **)pArg
f15b0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a = NULL;. }.
f15c0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
f15d0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
f15e0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 case SQLITE_SE
f15f0 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a T_LOCKPROXYFILE:
f1600 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c {. unixFil
f1610 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
f1620 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 File*)id;.
f1630 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
f1640 4f 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 OK;. int is
f1650 50 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 46 ProxyStyle = (pF
f1660 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 ile->pMethod ==
f1670 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29 &proxyIoMethods)
f1680 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 67 ;. if( pArg
f1690 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 ==NULL || (const
f16a0 20 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 20 char *)pArg==0
f16b0 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 ){. if( i
f16c0 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 sProxyStyle ){.
f16d0 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e /* turn
f16e0 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 off proxy locki
f16f0 6e 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74 ng - not support
f1700 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ed */.
f1710 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
f1720 52 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f R /*SQLITE_PROTO
f1730 43 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55 COL? SQLITE_MISU
f1740 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d SE?*/;. }
f1750 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
f1760 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 /* turn off prox
f1770 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65 y locking - alre
f1780 61 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a ady off - NOOP *
f1790 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d /. rc =
f17a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
f17b0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
f17c0 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 e{. const
f17d0 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 68 char *proxyPath
f17e0 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a = (const char *
f17f0 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 69 )pArg;. i
f1800 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 f( isProxyStyle
f1810 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 6f ){. pro
f1820 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f1830 20 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 20 *pCtx = .
f1840 20 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b (proxyLock
f1850 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c ingContext*)pFil
f1860 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f1870 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 t;. if(
f1880 20 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 22 !strcmp(pArg, "
f1890 3a 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 20 :auto:") .
f18a0 20 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c || (pCtx->l
f18b0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 0a ockProxyPath &&.
f18c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 !
f18d0 73 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f strncmp(pCtx->lo
f18e0 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 6f ckProxyPath, pro
f18f0 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c xyPath, MAXPATHL
f1900 45 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 29 EN)). )
f1910 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
f1920 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
f1930 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
f1940 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
f1950 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 switchLockProxyP
f1960 61 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 ath(pFile, proxy
f1970 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 Path);.
f1980 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
f1990 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 {. /* t
f19a0 75 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c urn on proxy fil
f19b0 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 e locking */.
f19c0 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 rc = prox
f19d0 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 yTransformUnixFi
f19e0 6c 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 le(pFile, proxyP
f19f0 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a ath);. }.
f1a00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
f1a10 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
f1a20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
f1a30 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b assert( 0 );
f1a40 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 73 /* The call as
f1a50 73 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 20 sures that only
f1a60 76 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 72 valid opcodes ar
f1a70 65 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a e sent */. }.
f1a80 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 }. /*NOTREACH
f1a90 45 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 ED*/. return SQ
f1aa0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f LITE_ERROR;.}../
f1ab0 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 *.** Within this
f1ac0 20 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 70 division (the p
f1ad0 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 roxying locking
f1ae0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20 implementation)
f1af0 74 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a 2a the procedures.*
f1b00 2a 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69 * above this poi
f1b10 6e 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69 nt are all utili
f1b20 74 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d ties. The lock-
f1b30 72 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20 related methods
f1b40 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d of the.** proxy-
f1b50 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f locking sqlite3_
f1b60 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74 io_method object
f1b70 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a follow..*/.../*
f1b80 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
f1b90 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
f1ba0 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
f1bb0 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
f1bc0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
f1bd0 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
f1be0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
f1bf0 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
f1c00 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 s held, set *pRe
f1c10 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e sOut.** to a non
f1c20 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 -zero value othe
f1c30 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 rwise *pResOut i
f1c40 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 s set to zero.
f1c50 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
f1c60 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 .** is set to SQ
f1c70 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 LITE_OK unless a
f1c80 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 n I/O error occu
f1c90 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 rs during lock c
f1ca0 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 hecking..*/.stat
f1cb0 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 ic int proxyChec
f1cc0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
f1cd0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
f1ce0 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a int *pResOut) {.
f1cf0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
f1d00 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
f1d10 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 d;. int rc = pr
f1d20 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 oxyTakeConch(pFi
f1d30 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 le);. if( rc==S
f1d40 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
f1d50 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f1d60 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
f1d70 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f1d80 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
f1d90 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e gContext;. un
f1da0 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 ixFile *proxy =
f1db0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b pCtx->lockProxy;
f1dc0 0a 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 . return prox
f1dd0 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65 y->pMethod->xChe
f1de0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28 ckReservedLock((
f1df0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 sqlite3_file*)pr
f1e00 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20 oxy, pResOut);.
f1e10 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
f1e20 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
f1e30 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
f1e40 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
f1e50 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
f1e60 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
f1e70 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
f1e80 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
f1e90 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
f1ea0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
f1eb0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
f1ec0 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
f1ed0 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
f1ee0 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
f1ef0 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
f1f00 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
f1f10 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
f1f20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
f1f30 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
f1f40 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
f1f50 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
f1f60 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
f1f70 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
f1f80 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
f1f90 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
f1fa0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
f1fb0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
f1fc0 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
f1fd0 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
f1fe0 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
f1ff0 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
f2000 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
f2010 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
f2020 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
f2030 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
f2040 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
f2050 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
f2060 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
f2070 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
f2080 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
f2090 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
f20a0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
f20b0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
f20c0 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
f20d0 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 SIVE.**.** This
f20e0 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
f20f0 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
f2100 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 k. Use the sqli
f2110 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a te3OsUnlock().**
f2120 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 routine to lowe
f2130 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 r a locking leve
f2140 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
f2150 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 proxyLock(sqlit
f2160 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
f2170 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 locktype) {. u
f2180 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
f2190 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
f21a0 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 int rc = proxy
f21b0 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 TakeConch(pFile)
f21c0 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
f21d0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f TE_OK ){. pro
f21e0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f21f0 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c *pCtx = (proxyL
f2200 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 ockingContext *)
f2210 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
f2220 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 ntext;. unixF
f2230 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 ile *proxy = pCt
f2240 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 x->lockProxy;.
f2250 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d rc = proxy->pM
f2260 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 ethod->xLock((sq
f2270 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 lite3_file*)prox
f2280 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 y, locktype);.
f2290 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
f22a0 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 e = proxy->lockt
f22b0 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ype;. }. retur
f22c0 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
f22d0 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e Lower the lockin
f22e0 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 g level on file
f22f0 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 descriptor pFile
f2300 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c to locktype. l
f2310 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 ocktype.** must
f2320 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 be either NO_LOC
f2330 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b K or SHARED_LOCK
f2340 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c ..**.** If the l
f2350 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 ocking level of
f2360 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
f2370 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 tor is already a
f2380 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 t or below.** th
f2390 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
f23a0 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 ing level, this
f23b0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
f23c0 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e op..*/.static in
f23d0 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 t proxyUnlock(sq
f23e0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
f23f0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a int locktype) {.
f2400 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
f2410 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
f2420 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 d;. int rc = pr
f2430 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 oxyTakeConch(pFi
f2440 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 le);. if( rc==S
f2450 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
f2460 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f2470 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
f2480 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f2490 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
f24a0 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e gContext;. un
f24b0 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 ixFile *proxy =
f24c0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b pCtx->lockProxy;
f24d0 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d . rc = proxy-
f24e0 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 >pMethod->xUnloc
f24f0 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
f2500 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 )proxy, locktype
f2510 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f );. pFile->lo
f2520 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e cktype = proxy->
f2530 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 locktype;. }.
f2540 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
f2550 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 .** Close a file
f2560 20 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79 that uses proxy
f2570 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 locks..*/.stati
f2580 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 c int proxyClose
f2590 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
f25a0 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b d) {. if( id ){
f25b0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 . unixFile *p
f25c0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
f25d0 2a 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c *)id;. proxyL
f25e0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 ockingContext *p
f25f0 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b Ctx = (proxyLock
f2600 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 ingContext *)pFi
f2610 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f2620 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 xt;. unixFile
f2630 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 *lockProxy = pC
f2640 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 tx->lockProxy;.
f2650 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e unixFile *con
f2660 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 chFile = pCtx->c
f2670 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e onchFile;. in
f2680 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
f2690 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c ;. . if( l
f26a0 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 ockProxy ){.
f26b0 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 rc = lockProxy
f26c0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f ->pMethod->xUnlo
f26d0 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck((sqlite3_file
f26e0 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f *)lockProxy, NO_
f26f0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 LOCK);. if(
f2700 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
f2710 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b . rc = lock
f2720 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e Proxy->pMethod->
f2730 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f xClose((sqlite3_
f2740 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 file*)lockProxy)
f2750 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
f2760 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
f2770 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c sqlite3_free(l
f2780 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 ockProxy);.
f2790 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
f27a0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
f27b0 69 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b if( conchFile ){
f27c0 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d . if( pCtx-
f27d0 3e 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20 >conchHeld ){.
f27e0 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 rc = proxy
f27f0 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69 ReleaseConch(pFi
f2800 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 le);. if(
f2810 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
f2820 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
f2830 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 c = conchFile->p
f2840 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 Method->xClose((
f2850 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f sqlite3_file*)co
f2860 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 nchFile);.
f2870 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
f2880 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 rc;. sqlite
f2890 33 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65 3_free(conchFile
f28a0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
f28b0 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e ite3_free(pCtx->
f28c0 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0a lockProxyPath);.
f28d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
f28e0 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 (pCtx->conchFile
f28f0 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 Path);. sqlit
f2900 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 62 e3_free(pCtx->db
f2910 50 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 65 Path);. /* re
f2920 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e store the origin
f2930 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 al locking conte
f2940 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 xt and pMethod t
f2950 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f 0a hen close it */.
f2960 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 pFile->locki
f2970 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 ngContext = pCtx
f2980 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 ->oldLockingCont
f2990 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ext;. pFile->
f29a0 70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d 3e pMethod = pCtx->
f29b0 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 20 pOldMethod;.
f29c0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 sqlite3_free(pCt
f29d0 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 x);. return p
f29e0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 File->pMethod->x
f29f0 43 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a 20 Close(id);. }.
f2a00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f2a10 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f K;.}....#endif /
f2a20 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c * defined(__APPL
f2a30 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
f2a40 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
f2a50 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 YLE */./*.** The
f2a60 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73 proxy locking s
f2a70 74 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64 tyle is intended
f2a80 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 46 for use with AF
f2a90 50 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a 2a P filesystems..*
f2aa0 2a 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20 * And since AFP
f2ab0 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 is only supporte
f2ac0 64 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65 d on MacOSX, the
f2ad0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 proxy locking i
f2ae0 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 69 s also.** restri
f2af0 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0a cted to MacOSX..
f2b00 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ** .**.*********
f2b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
f2b20 66 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b f the proxy lock
f2b30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
f2b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2b50 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
f2b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2ba0 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e *****/../*.** In
f2bb0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 itialize the ope
f2bc0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e rating system in
f2bd0 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 terface..**.** T
f2be0 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69 his routine regi
f2bf0 73 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 6d sters all VFS im
f2c00 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f plementations fo
f2c10 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 r unix-like oper
f2c20 61 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d 73 ating.** systems
f2c30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 2c . This routine,
f2c40 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 and the sqlite3
f2c50 5f 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 6e _os_end() routin
f2c60 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a e that follows,.
f2c70 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 ** should be the
f2c80 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 69 only routines i
f2c90 6e 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 74 n this file that
f2ca0 20 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 6f are visible fro
f2cb0 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 73 m other.** files
f2cc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
f2cd0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f tine is called o
f2ce0 6e 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74 nce during SQLit
f2cf0 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e e initialization
f2d00 20 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 6e and by a.** sin
f2d10 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 gle thread. The
f2d20 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
f2d30 6f 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62 on and mutex sub
f2d40 73 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74 systems have not
f2d50 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20 .** necessarily
f2d60 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 been initialized
f2d70 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
f2d80 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e ne is called, an
f2d90 64 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 6f d so they.** sho
f2da0 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 2e uld not be used.
f2db0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
f2dc0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e nt sqlite3_os_in
f2dd0 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 it(void){ . /*
f2de0 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . ** The follow
f2df0 69 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 ing macro define
f2e00 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 72 s an initializer
f2e10 20 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 5f for an sqlite3_
f2e20 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a vfs object.. **
f2e30 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 The name of the
f2e40 20 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 54 VFS is NAME. T
f2e50 68 65 20 70 41 70 70 44 61 74 61 20 69 73 20 61 he pAppData is a
f2e60 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f pointer to a po
f2e70 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 inter. ** to th
f2e80 65 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 e "finder" funct
f2e90 69 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 20 ion. (pAppData
f2ea0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
f2eb0 61 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 73 a pointer becaus
f2ec0 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 30 e. ** silly C90
f2ed0 20 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 20 rules prohibit
f2ee0 61 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 69 a void* from bei
f2ef0 6e 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 6e ng cast to a fun
f2f00 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 20 ction pointer.
f2f10 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76 ** and so we hav
f2f20 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 e to go through
f2f30 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 the intermediate
f2f40 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f 69 pointer to avoi
f2f50 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 d problems. **
f2f60 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 77 when compiling w
f2f70 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 ith -pedantic-er
f2f80 72 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 20 rors on GCC.).
f2f90 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e 44 **. ** The FIND
f2fa0 45 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 ER parameter to
f2fb0 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 68 this macro is th
f2fc0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 6f e name of the po
f2fd0 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a inter to the. *
f2fe0 2a 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f * finder-functio
f2ff0 6e 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d 66 n. The finder-f
f3000 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
f3010 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
f3020 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f . ** sqlite_io_
f3030 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74 methods object t
f3040 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 hat implements t
f3050 68 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69 he desired locki
f3060 6e 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 ng. ** behavior
f3070 73 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69 s. See the divi
f3080 73 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20 sion above that
f3090 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d contains the IOM
f30a0 45 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 72 ETHODS. ** macr
f30b0 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69 o for addition i
f30c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69 nformation on fi
f30d0 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a nder-functions..
f30e0 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 66 **. ** Most f
f30f0 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 65 inders simply re
f3100 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
f3110 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 65 o a fixed sqlite
f3120 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 2a 3_io_methods. *
f3130 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 74 * object. But t
f3140 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 he "autolockIoFi
f3150 6e 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 20 nder" available
f3160 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 61 on MacOSX does a
f3170 20 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72 little. ** mor
f3180 65 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 20 e than that; it
f3190 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c looks at the fil
f31a0 65 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 61 esystem type tha
f31b0 74 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 2a t hosts the . *
f31c0 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 * database file
f31d0 61 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 6f and tries to cho
f31e0 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d ose an locking m
f31f0 65 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 74 ethod appropriat
f3200 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 20 e for. ** that
f3210 66 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 2e filesystem time.
f3220 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 . */. #define
f3230 55 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 2c UNIXVFS(VFSNAME,
f3240 20 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 20 FINDER) {
f3250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3260 20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 20 \. 1,
f3270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f3280 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 iVersion */
f3290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f32a0 5c 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69 \. sizeof(uni
f32b0 78 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73 xFile), /* s
f32c0 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 20 zOsFile */
f32d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f32e0 20 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 MAX_PATHNAME
f32f0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 , /* mxP
f3300 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 athname */
f3310 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
f3320 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
f3330 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 /* pNext
f3340 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f3350 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
f3360 56 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 VFSNAME,
f3370 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a /* zName *
f3380 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f3390 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 76 \. (v
f33a0 6f 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20 oid*)&FINDER,
f33b0 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 /* pAppData
f33c0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f33d0 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
f33e0 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 Open,
f33f0 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20 /* xOpen */
f3400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3410 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 65 \. unixDe
f3420 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 lete,
f3430 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20 /* xDelete */
f3440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3450 20 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 65 \. unixAcce
f3460 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ss, /*
f3470 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 20 xAccess */
f3480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3490 5c 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 61 \. unixFullPa
f34a0 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78 thname, /* x
f34b0 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 20 FullPathname */
f34c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f34d0 20 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 unixDlOpen,
f34e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
f34f0 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 Open */
f3500 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
f3510 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20 unixDlError,
f3520 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 /* xDlEr
f3530 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ror */
f3540 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
f3550 75 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 20 unixDlSym,
f3560 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 /* xDlSym
f3570 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f3580 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
f3590 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 ixDlClose,
f35a0 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 /* xDlClose
f35b0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f35c0 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
f35d0 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 Randomness,
f35e0 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 /* xRandomness
f35f0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f3600 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 6c \. unixSl
f3610 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 eep,
f3620 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 20 /* xSleep */
f3630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3640 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 72 \. unixCurr
f3650 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a entTime, /*
f3660 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f xCurrentTime */
f3670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3680 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 73 \. unixGetLas
f3690 74 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 78 tError /* x
f36a0 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 20 GetLastError */
f36b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f36c0 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 }.. /*. ** A
f36d0 6c 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73 ll default VFSes
f36e0 20 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f for unix are co
f36f0 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 ntained in the f
f3700 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a ollowing array..
f3710 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 **. ** Note t
f3720 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f hat the sqlite3_
f3730 76 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20 vfs.pNext field
f3740 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 of the VFS objec
f3750 74 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20 t is modified.
f3760 2a 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 ** by the SQLite
f3770 20 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56 core when the V
f3780 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 FS is registered
f3790 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 . So the follow
f37a0 69 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63 ing. ** array c
f37b0 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a annot be const..
f37c0 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71 */. static sq
f37d0 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d lite3_vfs aVfs[]
f37e0 20 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f = {.#if SQLITE_
f37f0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
f3800 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 TYLE && defined(
f3810 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 __APPLE__). U
f3820 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 NIXVFS("unix",
f3830 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b autolock
f3840 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73 IoFinder ),.#els
f3850 65 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 e. UNIXVFS("u
f3860 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70 nix", p
f3870 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a osixIoFinder ),.
f3880 23 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56 #endif. UNIXV
f3890 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 FS("unix-none",
f38a0 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 nolockIoFind
f38b0 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 er ),. UNIXVF
f38c0 53 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 S("unix-dotfile"
f38d0 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 , dotlockIoFind
f38e0 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58 57 er ),.#if OS_VXW
f38f0 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 53 ORKS. UNIXVFS
f3900 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d 22 ("unix-namedsem"
f3910 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29 2c , semIoFinder ),
f3920 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 .#endif.#if SQLI
f3930 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
f3940 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49 58 G_STYLE. UNIX
f3950 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 22 VFS("unix-posix"
f3960 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 , posixIoFind
f3970 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 er ),. UNIXVF
f3980 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20 S("unix-flock",
f3990 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 flockIoFinder
f39a0 20 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 ),.#endif.#if S
f39b0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
f39c0 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 KING_STYLE && de
f39d0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
f39e0 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e . UNIXVFS("un
f39f0 69 78 2d 61 66 70 22 2c 20 20 20 20 20 20 61 66 ix-afp", af
f3a00 70 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 pIoFinder ),.
f3a10 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70 UNIXVFS("unix-p
f3a20 72 6f 78 79 22 2c 20 20 20 20 70 72 6f 78 79 49 roxy", proxyI
f3a30 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 oFinder ),.#endi
f3a40 66 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 f. };. unsigne
f3a50 64 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 d int i;
f3a60 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
f3a70 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73 r */.. /* Regis
f3a80 74 65 72 20 61 6c 6c 20 56 46 53 65 73 20 64 65 ter all VFSes de
f3a90 66 69 6e 65 64 20 69 6e 20 74 68 65 20 61 56 66 fined in the aVf
f3aa0 73 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20 20 66 s[] array */. f
f3ab0 6f 72 28 69 3d 30 3b 20 69 3c 28 73 69 7a 65 6f or(i=0; i<(sizeo
f3ac0 66 28 61 56 66 73 29 2f 73 69 7a 65 6f 66 28 73 f(aVfs)/sizeof(s
f3ad0 71 6c 69 74 65 33 5f 76 66 73 29 29 3b 20 69 2b qlite3_vfs)); i+
f3ae0 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f +){. sqlite3_
f3af0 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 61 56 vfs_register(&aV
f3b00 66 73 5b 69 5d 2c 20 69 3d 3d 30 29 3b 0a 20 20 fs[i], i==0);.
f3b10 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
f3b20 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 E_OK; .}../*.**
f3b30 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6f 70 65 Shutdown the ope
f3b40 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e rating system in
f3b50 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 53 terface..**.** S
f3b60 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 ome operating sy
f3b70 73 74 65 6d 73 20 6d 69 67 68 74 20 6e 65 65 64 stems might need
f3b80 20 74 6f 20 64 6f 20 73 6f 6d 65 20 63 6c 65 61 to do some clea
f3b90 6e 75 70 20 69 6e 20 74 68 69 73 20 72 6f 75 74 nup in this rout
f3ba0 69 6e 65 2c 0a 2a 2a 20 74 6f 20 72 65 6c 65 61 ine,.** to relea
f3bb0 73 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 se dynamically a
f3bc0 6c 6c 6f 63 61 74 65 64 20 6f 62 6a 65 63 74 73 llocated objects
f3bd0 2e 20 20 42 75 74 20 6e 6f 74 20 6f 6e 20 75 6e . But not on un
f3be0 69 78 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ix..** This rout
f3bf0 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 ine is a no-op f
f3c00 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 53 51 4c 49 or unix..*/.SQLI
f3c10 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
f3c20 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b e3_os_end(void){
f3c30 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 . return SQLIT
f3c40 45 5f 4f 4b 3b 20 0a 7d 0a 20 0a 23 65 6e 64 69 E_OK; .}. .#endi
f3c50 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 55 f /* SQLITE_OS_U
f3c60 4e 49 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a NIX */../*******
f3c70 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f ******* End of o
f3c80 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a s_unix.c *******
f3c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3cb0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
f3cc0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
f3cd0 6c 65 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a le os_win.c ****
f3ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3d00 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
f3d10 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 04 May 22.**.**
f3d20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
f3d30 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
f3d40 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
f3d50 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
f3d60 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
f3d70 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
f3d80 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
f3d90 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
f3da0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
f3db0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
f3dc0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
f3dd0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
f3de0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
f3df0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
f3e00 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
f3e10 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
f3e20 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
f3e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
f3e80 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
f3e90 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 ntains code that
f3ea0 20 69 73 20 73 70 65 63 69 66 69 63 20 74 6f 20 is specific to
f3eb0 77 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 24 windows..**.** $
f3ec0 49 64 3a 20 6f 73 5f 77 69 6e 2e 63 2c 76 20 31 Id: os_win.c,v 1
f3ed0 2e 31 34 35 20 32 30 30 38 2f 31 32 2f 31 31 20 .145 2008/12/11
f3ee0 30 32 3a 35 38 3a 32 37 20 73 68 61 6e 65 20 45 02:58:27 shane E
f3ef0 78 70 20 24 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 xp $.*/.#if SQLI
f3f00 54 45 5f 4f 53 5f 57 49 4e 20 20 20 20 20 20 20 TE_OS_WIN
f3f10 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
f3f20 66 69 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72 file is used for
f3f30 20 77 69 6e 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f windows only */
f3f40 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 .../*.** A Note
f3f50 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c About Memory All
f3f60 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 ocation:.**.** T
f3f70 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73 20 his driver uses
f3f80 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 malloc()/free()
f3f90 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 directly rather
f3fa0 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 than going throu
f3fb0 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 gh.** the SQLite
f3fc0 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 -wrappers sqlite
f3fd0 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 3_malloc()/sqlit
f3fe0 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 e3_free(). Thos
f3ff0 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 e wrappers.** ar
f4000 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 e designed for u
f4010 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 se on embedded s
f4020 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d ystems where mem
f4030 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e ory is scarce an
f4040 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c d.** malloc fail
f4050 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 71 ures happen freq
f4060 75 65 6e 74 6c 79 2e 20 20 57 69 6e 33 32 20 64 uently. Win32 d
f4070 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c oes not typicall
f4080 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 y run on.** embe
f4090 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e dded systems, an
f40a0 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 d when it does t
f40b0 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f he developers no
f40c0 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 rmally have bigg
f40d0 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 er.** problems t
f40e0 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 o worry about th
f40f0 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f an running out o
f4100 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 f memory. So th
f4110 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 ere is not.** a
f4120 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 compelling need
f4130 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70 to use the wrapp
f4140 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 ers..**.** But t
f4150 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 here is a good r
f4160 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 eason to not use
f4170 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 the wrappers.
f4180 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a If we use the.**
f4190 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 wrappers then w
f41a0 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c e will get simul
f41b0 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 ated malloc() fa
f41c0 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 ilures within th
f41d0 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 is.** driver. A
f41e0 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61 nd that causes a
f41f0 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 ll kinds of prob
f4200 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 lems for our tes
f4210 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 ts. We.** could
f4220 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 enhance SQLite
f4230 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d to deal with sim
f4240 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 ulated malloc fa
f4250 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a ilures within.**
f4260 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 the OS driver,
f4270 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 but the code to
f4280 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 deal with those
f4290 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f failure would no
f42a0 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 t.** be exercise
f42b0 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 d on Linux (whic
f42c0 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 h does not need
f42d0 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 to malloc() in t
f42e0 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e he driver).** an
f42f0 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 d so we would ha
f4300 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 ve difficulty wr
f4310 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 iting coverage t
f4320 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a ests for that.**
f4330 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 code. Better t
f4340 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 o leave the code
f4350 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a out, we think..
f4360 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 **.** The point
f4370 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69 of this discussi
f4380 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 on is as follows
f4390 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 : When creating
f43a0 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 a new.** OS lay
f43b0 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 er for an embedd
f43c0 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f ed system, if yo
f43d0 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 u use this file
f43e0 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a as an example,.*
f43f0 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 * avoid the use
f4400 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 of malloc()/free
f4410 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 (). Those routi
f4420 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 nes work ok on w
f4430 69 6e 64 6f 77 73 0a 2a 2a 20 64 65 73 6b 74 6f indows.** deskto
f4440 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 ps but not so we
f4450 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73 ll in embedded s
f4460 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 ystems..*/..#inc
f4470 6c 75 64 65 20 3c 77 69 6e 62 61 73 65 2e 68 3e lude <winbase.h>
f4480 0a 0a 23 69 66 64 65 66 20 5f 5f 43 59 47 57 49 ..#ifdef __CYGWI
f4490 4e 5f 5f 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 N__.# include <s
f44a0 79 73 2f 63 79 67 77 69 6e 2e 68 3e 0a 23 65 6e ys/cygwin.h>.#en
f44b0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f dif../*.** Macro
f44c0 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
f44d0 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e ine whether or n
f44e0 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 61 64 ot to use thread
f44f0 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 s..*/.#if define
f4500 64 28 54 48 52 45 41 44 53 41 46 45 29 20 26 26 d(THREADSAFE) &&
f4510 20 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 THREADSAFE.# de
f4520 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 33 32 5f fine SQLITE_W32_
f4530 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 THREADS 1.#endif
f4540 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 ../*.** Include
f4550 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d code that is com
f4560 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e mon to all os_*.
f4570 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a c files.*/./****
f4580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
f4590 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 de os_common.h i
f45a0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
f45b0 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_win.c *******
f45c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
f45d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
f45e0 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e file os_common.
f45f0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
f4600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
f4620 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2004 May 22.**.
f4630 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
f4640 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
f4650 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
f4660 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
f4670 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
f4680 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
f4690 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
f46a0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
f46b0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
f46c0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
f46d0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
f46e0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
f46f0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
f4700 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
f4710 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
f4720 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
f4730 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
f4740 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
f4750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4790 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
f47a0 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 contains macros
f47b0 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 and a little bi
f47c0 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 t of code that i
f47d0 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 s common to.** a
f47e0 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f ll of the platfo
f47f0 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 rm-specific file
f4800 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 s (os_*.c) and i
f4810 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f s #included into
f4820 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e those.** files.
f4830 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
f4840 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c should be #incl
f4850 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a uded by the os_*
f4860 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 .c files only.
f4870 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 It is not a.** g
f4880 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68 eneral purpose h
f4890 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a eader file..**.*
f48a0 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e * $Id: os_common
f48b0 2e 68 2c 76 20 31 2e 33 37 20 32 30 30 38 2f 30 .h,v 1.37 2008/0
f48c0 35 2f 32 39 20 32 30 3a 32 32 3a 33 37 20 73 68 5/29 20:22:37 sh
f48d0 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 ane Exp $.*/.#if
f48e0 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f ndef _OS_COMMON_
f48f0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 H_.#define _OS_C
f4900 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 OMMON_H_../*.**
f4910 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 At least two bug
f4920 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 s have slipped i
f4930 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61 n because we cha
f4940 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f nged the MEMORY_
f4950 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 DEBUG.** macro t
f4960 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 o SQLITE_DEBUG a
f4970 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 nd some older ma
f4980 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 kefiles have not
f4990 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a yet made the.**
f49a0 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f switch. The fo
f49b0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f llowing code sho
f49c0 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70 uld catch this p
f49d0 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c roblem at compil
f49e0 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 e-time..*/.#ifde
f49f0 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 f MEMORY_DEBUG.#
f4a00 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f error "The MEMO
f4a10 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 RY_DEBUG macro i
f4a20 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 s obsolete. Use
f4a30 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e SQLITE_DEBUG in
f4a40 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a stead.".#endif..
f4a50 0a 2f 2a 0a 20 2a 20 57 68 65 6e 20 74 65 73 74 ./*. * When test
f4a60 69 6e 67 2c 20 74 68 69 73 20 67 6c 6f 62 61 6c ing, this global
f4a70 20 76 61 72 69 61 62 6c 65 20 73 74 6f 72 65 73 variable stores
f4a80 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 the location of
f4a90 20 74 68 65 0a 20 2a 20 70 65 6e 64 69 6e 67 2d the. * pending-
f4aa0 62 79 74 65 20 69 6e 20 74 68 65 20 64 61 74 61 byte in the data
f4ab0 62 61 73 65 20 66 69 6c 65 2e 0a 20 2a 2f 0a 23 base file.. */.#
f4ac0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
f4ad0 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 75 6e 73 T.SQLITE_API uns
f4ae0 69 67 6e 65 64 20 69 6e 74 20 73 71 6c 69 74 65 igned int sqlite
f4af0 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74 65 20 3d 3_pending_byte =
f4b00 20 30 78 34 30 30 30 30 30 30 30 3b 0a 23 65 6e 0x40000000;.#en
f4b10 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
f4b20 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
f4b30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
f4b40 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a te3OSTrace = 0;.
f4b50 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 #define OSTRACE1
f4b60 28 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20 (X) if(
f4b70 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
f4b80 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
f4b90 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f ntf(X).#define O
f4ba0 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 20 STRACE2(X,Y)
f4bb0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 if( sqlite3OS
f4bc0 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
f4bd0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a ebugPrintf(X,Y).
f4be0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 #define OSTRACE3
f4bf0 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 (X,Y,Z) if(
f4c00 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
f4c10 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
f4c20 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 ntf(X,Y,Z).#defi
f4c30 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c ne OSTRACE4(X,Y,
f4c40 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 74 Z,A) if( sqlit
f4c50 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
f4c60 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
f4c70 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 ,Y,Z,A).#define
f4c80 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 OSTRACE5(X,Y,Z,A
f4c90 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f ,B) if( sqlite3O
f4ca0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
f4cb0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
f4cc0 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f Z,A,B).#define O
f4cd0 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c STRACE6(X,Y,Z,A,
f4ce0 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71 B,C) \. if(sq
f4cf0 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 lite3OSTrace) sq
f4d00 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
f4d10 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 (X,Y,Z,A,B,C).#d
f4d20 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 efine OSTRACE7(X
f4d30 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a ,Y,Z,A,B,C,D) \.
f4d40 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 if(sqlite3OS
f4d50 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 Trace) sqlite3De
f4d60 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c bugPrintf(X,Y,Z,
f4d70 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 A,B,C,D).#else.#
f4d80 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 define OSTRACE1(
f4d90 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 X).#define OSTRA
f4da0 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 CE2(X,Y).#define
f4db0 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 OSTRACE3(X,Y,Z)
f4dc0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
f4dd0 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 4(X,Y,Z,A).#defi
f4de0 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c ne OSTRACE5(X,Y,
f4df0 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f Z,A,B).#define O
f4e00 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c STRACE6(X,Y,Z,A,
f4e10 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 B,C).#define OST
f4e20 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c RACE7(X,Y,Z,A,B,
f4e30 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a C,D).#endif../*.
f4e40 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 ** Macros for pe
f4e50 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e rformance tracin
f4e60 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 g. Normally tur
f4e70 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 ned off. Only w
f4e80 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 orks.** on i486
f4e90 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 hardware..*/.#if
f4ea0 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f def SQLITE_PERFO
f4eb0 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a RMANCE_TRACE../*
f4ec0 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f .** hwtime.h co
f4ed0 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 ntains inline as
f4ee0 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 sembler code for
f4ef0 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a implementing .*
f4f00 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e * high-performan
f4f10 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e ce timing routin
f4f20 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a es..*/./********
f4f30 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 ****** Include h
f4f40 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d wtime.h in the m
f4f50 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d iddle of os_comm
f4f60 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
f4f70 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
f4f80 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
f4f90 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a e hwtime.h *****
f4fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4fc0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
f4fd0 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 8 May 27.**.** T
f4fe0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
f4ff0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
f5000 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
f5010 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
f5020 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
f5030 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
f5040 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
f5050 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
f5060 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
f5070 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
f5080 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
f5090 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
f50a0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
f50b0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
f50c0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
f50d0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
f50e0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
f50f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
f5140 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
f5150 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d tains inline asm
f5160 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 code for retrie
f5170 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f ving "high-perfo
f5180 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 rmance".** count
f5190 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 ers for x86 clas
f51a0 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 s CPUs..**.** $I
f51b0 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e d: hwtime.h,v 1.
f51c0 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 3 2008/08/01 14:
f51d0 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20 33:15 shane Exp
f51e0 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 $.*/.#ifndef _HW
f51f0 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 TIME_H_.#define
f5200 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a _HWTIME_H_../*.*
f5210 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
f5220 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 routine only wor
f5230 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c ks on pentium-cl
f5240 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 ass (or newer) p
f5250 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 rocessors..** It
f5260 20 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 uses the RDTSC
f5270 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 opcode to read t
f5280 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 he cycle count v
f5290 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a alue out of the.
f52a0 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 ** processor and
f52b0 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 returns that va
f52c0 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 lue. This can b
f52d0 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d e used for high-
f52e0 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 res.** profiling
f52f0 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 ..*/.#if (define
f5300 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 d(__GNUC__) || d
f5310 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 efined(_MSC_VER)
f5320 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 ) && \. (de
f5330 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 fined(i386) || d
f5340 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 efined(__i386__)
f5350 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 || defined(_M_I
f5360 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 X86)).. #if def
f5370 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a ined(__GNUC__)..
f5380 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
f5390 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
f53a0 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
f53b0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e unsigned in
f53c0 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f t lo, hi;. _
f53d0 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
f53e0 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 e__ ("rdtsc" : "
f53f0 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 =a" (lo), "=d" (
f5400 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 hi));. retur
f5410 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 n (sqlite_uint64
f5420 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a )hi << 32 | lo;.
f5430 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 }.. #elif def
f5440 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a ined(_MSC_VER)..
f5450 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b __declspec(nak
f5460 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c ed) __inline sql
f5470 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 ite_uint64 __cde
f5480 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 cl sqlite3Hwtime
f5490 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 (void){. __a
f54a0 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 sm {. rdt
f54b0 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 sc. ret
f54c0 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 ; return va
f54d0 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 lue at EDX:EAX.
f54e0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e }. }.. #en
f54f0 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 dif..#elif (defi
f5500 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 ned(__GNUC__) &&
f5510 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 defined(__x86_6
f5520 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 4__)).. __inlin
f5530 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
f5540 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
f5550 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 void){. uns
f5560 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a igned long val;.
f5570 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f __asm__ __
f5580 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 volatile__ ("rdt
f5590 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 sc" : "=A" (val)
f55a0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
f55b0 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 val;. }. .#elif
f55c0 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
f55d0 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f __) && defined(_
f55e0 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e _ppc__)).. __in
f55f0 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 line__ sqlite_ui
f5600 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
f5610 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 me(void){.
f5620 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
f5630 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 ng retval;.
f5640 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a unsigned long j
f5650 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d unk;. __asm
f5660 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 __ __volatile__
f5670 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 ("\n\.
f5680 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 1: mftbu
f5690 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %1\n\.
f56a0 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 mftb
f56b0 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 %L0\n\.
f56c0 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 mftbu
f56d0 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 %0\n\.
f56e0 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 cmpw
f56f0 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 %0,%1\n\.
f5700 20 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 bne
f5710 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 1b".
f5720 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 : "=r"
f5730 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 (retval), "=r"
f5740 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 (junk));. r
f5750 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 eturn retval;.
f5760 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 }..#else.. #err
f5770 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e or Need implemen
f5780 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 tation of sqlite
f5790 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 3Hwtime() for yo
f57a0 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 ur platform...
f57b0 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 /*. ** To compi
f57c0 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 le without imple
f57d0 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 menting sqlite3H
f57e0 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 wtime() for your
f57f0 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 platform,. **
f5800 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 you can remove t
f5810 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 he above #error
f5820 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c and use the foll
f5830 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 owing. ** stub
f5840 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 function. You w
f5850 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 ill lose timing
f5860 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 support for many
f5870 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 . ** of the deb
f5880 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 ugging and testi
f5890 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 ng utilities, bu
f58a0 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 t it should at.
f58b0 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c ** least compil
f58c0 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a e and run.. */.
f58d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
f58e0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
f58f0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
f5900 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c d){ return ((sql
f5910 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d ite_uint64)0); }
f5920 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 ..#endif..#endif
f5930 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 /* !defined(_HW
f5940 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a TIME_H_) */../**
f5950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
f5960 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a of hwtime.h ***
f5970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
f59a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
f59b0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
f59c0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f left off in os_
f59d0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
f59e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 ***********/..st
f59f0 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 atic sqlite_uint
f5a00 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 64 g_start;.stat
f5a10 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ic sqlite_uint64
f5a20 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 g_elapsed;.#def
f5a30 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 ine TIMER_START
f5a40 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 g_start=sq
f5a50 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 lite3Hwtime().#d
f5a60 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 efine TIMER_END
f5a70 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 g_elapse
f5a80 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 d=sqlite3Hwtime(
f5a90 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e )-g_start.#defin
f5aa0 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 e TIMER_ELAPSED
f5ab0 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 g_elapsed.#e
f5ac0 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 lse.#define TIME
f5ad0 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 R_START.#define
f5ae0 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e TIMER_END.#defin
f5af0 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 e TIMER_ELAPSED
f5b00 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e ((sqlite_uin
f5b10 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f t64)0).#endif../
f5b20 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 *.** If we compi
f5b30 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 le with the SQLI
f5b40 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 TE_TEST macro se
f5b50 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c t, then the foll
f5b60 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f owing block.** o
f5b70 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 f code will give
f5b80 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 us the ability
f5b90 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 to simulate a di
f5ba0 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 sk I/O error. T
f5bb0 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 his.** is used f
f5bc0 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 or testing the I
f5bd0 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 /O recovery logi
f5be0 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c c..*/.#ifdef SQL
f5bf0 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
f5c00 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
f5c10 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 io_error_hit = 0
f5c20 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
f5c30 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 Total number of
f5c40 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 I/O Errors */.SQ
f5c50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
f5c60 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 ite3_io_error_ha
f5c70 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 rdhit = 0;
f5c80 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e /* Number of n
f5c90 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 on-benign errors
f5ca0 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
f5cb0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
f5cc0 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b ror_pending = 0;
f5cd0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 /* Count
f5ce0 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 down to first I
f5cf0 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 /O error */.SQLI
f5d00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
f5d10 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 e3_io_error_pers
f5d20 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ist = 0;
f5d30 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 /* True if I/O e
f5d40 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f rrors persist */
f5d50 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
f5d60 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
f5d70 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 _benign = 0;
f5d80 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
f5d90 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 errors are benig
f5da0 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 n */.SQLITE_API
f5db0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b int sqlite3_disk
f5dc0 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 full_pending = 0
f5dd0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
f5de0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
f5df0 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 l = 0;.#define S
f5e00 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 imulateIOErrorBe
f5e10 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f nign(X) sqlite3_
f5e20 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d io_error_benign=
f5e30 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 (X).#define Simu
f5e40 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 lateIOError(CODE
f5e50 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 ) \. if( (sqli
f5e60 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 te3_io_error_per
f5e70 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f sist && sqlite3_
f5e80 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a io_error_hit) \.
f5e90 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
f5ea0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
f5eb0 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 ng-- == 1 ) \.
f5ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c { l
f5ed0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f ocal_ioerr(); CO
f5ee0 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 DE; }.static voi
f5ef0 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b d local_ioerr(){
f5f00 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 . IOTRACE(("IOE
f5f10 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 RR\n"));. sqlit
f5f20 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b e3_io_error_hit+
f5f30 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 +;. if( !sqlite
f5f40 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 3_io_error_benig
f5f50 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 n ) sqlite3_io_e
f5f60 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a rror_hardhit++;.
f5f70 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 }.#define Simula
f5f80 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 teDiskfullError(
f5f90 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 CODE) \. if( s
f5fa0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
f5fb0 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 pending ){ \.
f5fc0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 if( sqlite3_di
f5fd0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d skfull_pending =
f5fe0 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 = 1 ){ \.
f5ff0 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c local_ioerr(); \
f6000 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f . sqlite3_
f6010 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a diskfull = 1; \.
f6020 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 sqlite3_i
f6030 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b o_error_hit = 1;
f6040 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 \. CODE;
f6050 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a \. }else{ \.
f6060 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 sqlite3_d
f6070 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d iskfull_pending-
f6080 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 -; \. } \.
f6090 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 }.#else.#define
f60a0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
f60b0 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e Benign(X).#defin
f60c0 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f e SimulateIOErro
f60d0 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d r(A).#define Sim
f60e0 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 ulateDiskfullErr
f60f0 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a or(A).#endif../*
f6100 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 .** When testing
f6110 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f , keep a count o
f6120 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
f6130 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 open files..*/.#
f6140 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
f6150 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
f6160 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 sqlite3_open_fi
f6170 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 le_count = 0;.#d
f6180 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 efine OpenCounte
f6190 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 r(X) sqlite3_op
f61a0 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 en_file_count+=(
f61b0 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 X).#else.#define
f61c0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a OpenCounter(X).
f61d0 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f #endif..#endif /
f61e0 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 * !defined(_OS_C
f61f0 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a OMMON_H_) */../*
f6200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
f6210 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 d of os_common.h
f6220 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
f6230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
f6250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
f6260 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
f6270 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 e left off in os
f6280 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _win.c *********
f6290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
f62a0 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 *.** Some micros
f62b0 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 oft compilers la
f62c0 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 ck this definiti
f62d0 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 on..*/.#ifndef I
f62e0 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 NVALID_FILE_ATTR
f62f0 49 42 55 54 45 53 0a 23 20 64 65 66 69 6e 65 20 IBUTES.# define
f6300 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
f6310 52 49 42 55 54 45 53 20 28 28 44 57 4f 52 44 29 RIBUTES ((DWORD)
f6320 2d 31 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a -1) .#endif../*.
f6330 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 ** Determine if
f6340 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 we are dealing w
f6350 69 74 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 ith WindowsCE -
f6360 77 68 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 which has a much
f6370 0a 2a 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e .** reduced API.
f6380 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
f6390 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 S_WINCE.# define
f63a0 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 AreFileApisANSI
f63b0 28 29 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a () 1.#endif../*.
f63c0 2a 2a 20 57 69 6e 43 45 20 6c 61 63 6b 73 20 6e ** WinCE lacks n
f63d0 61 74 69 76 65 20 73 75 70 70 6f 72 74 20 66 6f ative support fo
f63e0 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 r file locking s
f63f0 6f 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6b o we have to fak
f6400 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 73 6f 6d e it.** with som
f6410 65 20 63 6f 64 65 20 6f 66 20 6f 75 72 20 6f 77 e code of our ow
f6420 6e 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 n..*/.#if SQLITE
f6430 5f 4f 53 5f 57 49 4e 43 45 0a 74 79 70 65 64 65 _OS_WINCE.typede
f6440 66 20 73 74 72 75 63 74 20 77 69 6e 63 65 4c 6f f struct winceLo
f6450 63 6b 20 7b 0a 20 20 69 6e 74 20 6e 52 65 61 64 ck {. int nRead
f6460 65 72 73 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 ers; /* Nu
f6470 6d 62 65 72 20 6f 66 20 72 65 61 64 65 72 20 6c mber of reader l
f6480 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 2a 2f ocks obtained */
f6490 0a 20 20 42 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 . BOOL bPending
f64a0 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 ; /* Indica
f64b0 74 65 73 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f tes a pending lo
f64c0 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 ck has been obta
f64d0 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 ined */. BOOL b
f64e0 52 65 73 65 72 76 65 64 3b 20 20 20 20 20 2f 2a Reserved; /*
f64f0 20 49 6e 64 69 63 61 74 65 73 20 61 20 72 65 73 Indicates a res
f6500 65 72 76 65 64 20 6c 6f 63 6b 20 68 61 73 20 62 erved lock has b
f6510 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a een obtained */.
f6520 20 20 42 4f 4f 4c 20 62 45 78 63 6c 75 73 69 76 BOOL bExclusiv
f6530 65 3b 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 e; /* Indicat
f6540 65 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 es an exclusive
f6550 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 lock has been ob
f6560 74 61 69 6e 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 tained */.} winc
f6570 65 4c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f eLock;.#endif../
f6580 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 46 69 6c 65 *.** The winFile
f6590 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 20 structure is a
f65a0 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 subclass of sqli
f65b0 74 65 33 5f 66 69 6c 65 2a 20 73 70 65 63 69 66 te3_file* specif
f65c0 69 63 20 74 6f 20 74 68 65 20 77 69 6e 33 32 0a ic to the win32.
f65d0 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 6c ** portability l
f65e0 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ayer..*/.typedef
f65f0 20 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 struct winFile
f6600 77 69 6e 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 winFile;.struct
f6610 77 69 6e 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 winFile {. cons
f6620 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
f6630 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a hods *pMethod;/*
f6640 20 4d 75 73 74 20 62 65 20 66 69 72 73 74 20 2a Must be first *
f6650 2f 0a 20 20 48 41 4e 44 4c 45 20 68 3b 20 20 20 /. HANDLE h;
f6660 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
f6670 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 andle for access
f6680 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a ing the file */.
f6690 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
f66a0 6c 6f 63 6b 74 79 70 65 3b 20 2f 2a 20 54 79 70 locktype; /* Typ
f66b0 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e e of lock curren
f66c0 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 tly held on this
f66d0 20 66 69 6c 65 20 2a 2f 0a 20 20 73 68 6f 72 74 file */. short
f66e0 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 3b sharedLockByte;
f66f0 20 20 20 2f 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 /* Randomly c
f6700 68 6f 73 65 6e 20 62 79 74 65 20 75 73 65 64 20 hosen byte used
f6710 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b as a shared lock
f6720 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f */.#if SQLITE_O
f6730 53 5f 57 49 4e 43 45 0a 20 20 57 43 48 41 52 20 S_WINCE. WCHAR
f6740 2a 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 3b *zDeleteOnClose;
f6750 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c /* Name of fil
f6760 65 20 74 6f 20 64 65 6c 65 74 65 20 77 68 65 6e e to delete when
f6770 20 63 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20 48 41 closing */. HA
f6780 4e 44 4c 45 20 68 4d 75 74 65 78 3b 20 20 20 20 NDLE hMutex;
f6790 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 /* Mutex u
f67a0 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 sed to control a
f67b0 63 63 65 73 73 20 74 6f 20 73 68 61 72 65 64 20 ccess to shared
f67c0 6c 6f 63 6b 20 2a 2f 20 20 0a 20 20 48 41 4e 44 lock */ . HAND
f67d0 4c 45 20 68 53 68 61 72 65 64 3b 20 20 20 20 20 LE hShared;
f67e0 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 6d 65 /* Shared me
f67f0 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 75 73 65 mory segment use
f6800 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f d for locking */
f6810 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63 . winceLock loc
f6820 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f al; /* Lo
f6830 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 cks obtained by
f6840 74 68 69 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 this instance of
f6850 20 77 69 6e 46 69 6c 65 20 2a 2f 0a 20 20 77 69 winFile */. wi
f6860 6e 63 65 4c 6f 63 6b 20 2a 73 68 61 72 65 64 3b nceLock *shared;
f6870 20 20 20 20 20 20 2f 2a 20 47 6c 6f 62 61 6c 20 /* Global
f6880 73 68 61 72 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f shared lock memo
f6890 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 ry for the file
f68a0 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 0a */.#endif.};...
f68b0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
f68c0 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20 ing variable is
f68d0 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f (normally) set o
f68e0 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68 nce and never ch
f68f0 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66 anges.** thereaf
f6900 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73 ter. It records
f6910 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65 whether the ope
f6920 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 rating system is
f6930 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e Win95.** or Win
f6940 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f NT..**.** 0: O
f6950 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
f6960 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 unknown..** 1:
f6970 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 Operating syste
f6980 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32 m is Win95..** 2
f6990 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 : Operating sy
f69a0 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a stem is WinNT..*
f69b0 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f *.** In order to
f69c0 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74 facilitate test
f69d0 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73 ing on a WinNT s
f69e0 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20 ystem, the test
f69f0 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d fixture.** can m
f6a00 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73 anually set this
f6a10 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65 value to 1 to e
f6a20 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68 mulate Win98 beh
f6a30 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 avior..*/.#ifdef
f6a40 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
f6a50 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f6a60 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b te3_os_type = 0;
f6a70 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e .#else.static in
f6a80 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 t sqlite3_os_typ
f6a90 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f e = 0;.#endif../
f6aa0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
f6ab0 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 (non-zero) if w
f6ac0 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e e are running un
f6ad0 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b der WinNT, Win2K
f6ae0 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 , WinXP,.** or W
f6af0 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 inCE. Return fa
f6b00 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 lse (zero) for W
f6b10 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 in95, Win98, or
f6b20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 WinME..**.** Her
f6b30 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 e is an interest
f6b40 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a ing observation:
f6b50 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 Win95, Win98,
f6b60 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a and WinME lack.*
f6b70 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 * the LockFileEx
f6b80 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 () API. But we
f6b90 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 can still static
f6ba0 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 ally link agains
f6bb0 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 t that.** API as
f6bc0 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 long as we don'
f6bd0 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 t call it win ru
f6be0 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d nning Win95/98/M
f6bf0 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a E. A call to.**
f6c00 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
f6c10 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 used to determi
f6c20 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 ne if the host i
f6c30 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 s Win95/98/ME or
f6c40 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 .** WinNT/2K/XP
f6c50 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 so that we will
f6c60 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 know whether or
f6c70 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c not we can safel
f6c80 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f y call.** the Lo
f6c90 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a ckFileEx() API..
f6ca0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
f6cb0 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 _WINCE.# define
f6cc0 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c 73 isNT() (1).#els
f6cd0 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 69 e. static int i
f6ce0 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 69 sNT(void){. i
f6cf0 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 f( sqlite3_os_ty
f6d00 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f pe==0 ){. O
f6d10 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e SVERSIONINFO sIn
f6d20 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e fo;. sInfo.
f6d30 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 dwOSVersionInfoS
f6d40 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e ize = sizeof(sIn
f6d50 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56 65 fo);. GetVe
f6d60 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b rsionEx(&sInfo);
f6d70 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6f . sqlite3_o
f6d80 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 s_type = sInfo.d
f6d90 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 wPlatformId==VER
f6da0 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f _PLATFORM_WIN32_
f6db0 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 NT ? 2 : 1;.
f6dc0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c }. return sql
f6dd0 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b ite3_os_type==2;
f6de0 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
f6df0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a QLITE_OS_WINCE *
f6e00 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 /../*.** Convert
f6e10 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 a UTF-8 string
f6e20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 to microsoft uni
f6e30 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e 20 code (UTF-16?).
f6e40 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 .**.** Space to
f6e50 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 hold the returne
f6e60 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 d string is obta
f6e70 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 ined from malloc
f6e80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 ..*/.static WCHA
f6e90 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 R *utf8ToUnicode
f6ea0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 (const char *zFi
f6eb0 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e lename){. int n
f6ec0 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a 7a Char;. WCHAR *z
f6ed0 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 WideFilename;..
f6ee0 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 nChar = MultiBy
f6ef0 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50 5f teToWideChar(CP_
f6f00 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 UTF8, 0, zFilena
f6f10 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 me, -1, NULL, 0)
f6f20 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d ;. zWideFilenam
f6f30 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 61 e = malloc( nCha
f6f40 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46 69 r*sizeof(zWideFi
f6f50 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 lename[0]) );.
f6f60 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d if( zWideFilenam
f6f70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 e==0 ){. retu
f6f80 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 rn 0;. }. nCha
f6f90 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 r = MultiByteToW
f6fa0 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 2c ideChar(CP_UTF8,
f6fb0 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 0, zFilename, -
f6fc0 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 1, zWideFilename
f6fd0 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28 20 , nChar);. if(
f6fe0 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 nChar==0 ){.
f6ff0 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e 61 free(zWideFilena
f7000 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46 69 me);. zWideFi
f7010 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a lename = 0;. }.
f7020 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46 69 return zWideFi
f7030 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a lename;.}../*.**
f7040 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f Convert microso
f7050 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55 54 ft unicode to UT
f7060 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 68 F-8. Space to h
f7070 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 old the returned
f7080 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 string is.** ob
f7090 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c tained from mall
f70a0 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 oc()..*/.static
f70b0 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 55 char *unicodeToU
f70c0 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52 20 tf8(const WCHAR
f70d0 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b *zWideFilename){
f70e0 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 . int nByte;.
f70f0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b char *zFilename;
f7100 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 .. nByte = Wide
f7110 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 CharToMultiByte(
f7120 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 CP_UTF8, 0, zWid
f7130 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 eFilename, -1, 0
f7140 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 , 0, 0, 0);. zF
f7150 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 ilename = malloc
f7160 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 ( nByte );. if(
f7170 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b zFilename==0 ){
f7180 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
f7190 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 }. nByte = Wid
f71a0 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 eCharToMultiByte
f71b0 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 (CP_UTF8, 0, zWi
f71c0 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 deFilename, -1,
f71d0 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 zFilename, nByte
f71e0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
f71f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7200 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 0, 0);. if( nBy
f7210 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 te == 0 ){. f
f7220 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a ree(zFilename);.
f7230 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 zFilename =
f7240 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
f7250 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a zFilename;.}../*
f7260 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 61 .** Convert an a
f7270 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d 69 nsi string to mi
f7280 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 2c crosoft unicode,
f7290 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a based on the.**
f72a0 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 current codepag
f72b0 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20 66 e settings for f
f72c0 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a ile apis..** .**
f72d0 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
f72e0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 he returned stri
f72f0 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a ng is obtained.*
f7300 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a * from malloc..*
f7310 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a /.static WCHAR *
f7320 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63 6f mbcsToUnicode(co
f7330 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
f7340 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 ame){. int nByt
f7350 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62 63 e;. WCHAR *zMbc
f7360 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 sFilename;. int
f7370 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 46 codepage = AreF
f7380 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f 20 ileApisANSI() ?
f7390 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 CP_ACP : CP_OEMC
f73a0 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75 P;.. nByte = Mu
f73b0 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 ltiByteToWideCha
f73c0 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a r(codepage, 0, z
f73d0 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 Filename, -1, NU
f73e0 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43 48 LL,0)*sizeof(WCH
f73f0 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c 65 AR);. zMbcsFile
f7400 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e name = malloc( n
f7410 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 63 Byte*sizeof(zMbc
f7420 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b sFilename[0]) );
f7430 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c 65 . if( zMbcsFile
f7440 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 name==0 ){. r
f7450 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e eturn 0;. }. n
f7460 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74 65 Byte = MultiByte
f7470 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65 70 ToWideChar(codep
f7480 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d age, 0, zFilenam
f7490 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c 65 e, -1, zMbcsFile
f74a0 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20 20 name, nByte);.
f74b0 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a if( nByte==0 ){.
f74c0 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46 69 free(zMbcsFi
f74d0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 lename);. zMb
f74e0 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a csFilename = 0;.
f74f0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d 62 }. return zMb
f7500 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f csFilename;.}../
f7510 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 *.** Convert mic
f7520 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 rosoft unicode t
f7530 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 o multibyte char
f7540 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62 61 acter string, ba
f7550 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 73 sed on the.** us
f7560 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70 61 er's Ansi codepa
f7570 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 ge..**.** Space
f7580 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 to hold the retu
f7590 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f rned string is o
f75a0 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 btained from.**
f75b0 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 malloc()..*/.sta
f75c0 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 tic char *unicod
f75d0 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57 43 eToMbcs(const WC
f75e0 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 HAR *zWideFilena
f75f0 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 me){. int nByte
f7600 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e ;. char *zFilen
f7610 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65 70 ame;. int codep
f7620 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70 69 age = AreFileApi
f7630 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43 50 sANSI() ? CP_ACP
f7640 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 : CP_OEMCP;..
f7650 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 nByte = WideChar
f7660 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65 ToMultiByte(code
f7670 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 page, 0, zWideFi
f7680 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 lename, -1, 0, 0
f7690 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 , 0, 0);. zFile
f76a0 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e name = malloc( n
f76b0 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 Byte );. if( zF
f76c0 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 ilename==0 ){.
f76d0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
f76e0 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 nByte = WideCh
f76f0 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f arToMultiByte(co
f7700 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 depage, 0, zWide
f7710 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 Filename, -1, zF
f7720 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a ilename, nByte,.
f7730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0,
f7750 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 0);. if( nByte
f7760 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65 == 0 ){. fre
f7770 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 e(zFilename);.
f7780 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b zFilename = 0;
f7790 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 . }. return zF
f77a0 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ilename;.}../*.*
f77b0 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69 62 * Convert multib
f77c0 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 74 yte character st
f77d0 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20 20 ring to UTF-8.
f77e0 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 Space to hold th
f77f0 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 e.** returned st
f7800 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 ring is obtained
f7810 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a from malloc()..
f7820 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 */.SQLITE_API ch
f7830 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e 33 ar *sqlite3_win3
f7840 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 63 2_mbcs_to_utf8(c
f7850 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
f7860 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a name){. char *z
f7870 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20 20 FilenameUtf8;.
f7880 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b WCHAR *zTmpWide;
f7890 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 6d .. zTmpWide = m
f78a0 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 bcsToUnicode(zFi
f78b0 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a lename);. if( z
f78c0 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 TmpWide==0 ){.
f78d0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
f78e0 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 20 zFilenameUtf8
f78f0 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 = unicodeToUtf8(
f7900 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 65 zTmpWide);. fre
f7910 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 e(zTmpWide);. r
f7920 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 eturn zFilenameU
f7930 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f tf8;.}../*.** Co
f7940 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20 6d nvert UTF-8 to m
f7950 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 ultibyte charact
f7960 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61 63 er string. Spac
f7970 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a 2a e to hold the .*
f7980 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e * returned strin
f7990 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 g is obtained fr
f79a0 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a om malloc()..*/.
f79b0 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74 66 static char *utf
f79c0 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63 68 8ToMbcs(const ch
f79d0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a ar *zFilename){.
f79e0 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d char *zFilenam
f79f0 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20 2a eMbcs;. WCHAR *
f7a00 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d zTmpWide;.. zTm
f7a10 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55 6e pWide = utf8ToUn
f7a20 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 icode(zFilename)
f7a30 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64 65 ;. if( zTmpWide
f7a40 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
f7a50 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 n 0;. }. zFile
f7a60 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f nameMbcs = unico
f7a70 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64 deToMbcs(zTmpWid
f7a80 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70 57 e);. free(zTmpW
f7a90 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a ide);. return z
f7aa0 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a FilenameMbcs;.}.
f7ab0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
f7ac0 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a INCE./**********
f7ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
f7b10 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20 ** This section
f7b20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f contains code fo
f7b30 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f r WinCE only..*/
f7b40 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 45 ./*.** WindowsCE
f7b50 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 does not have a
f7b60 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 6e localtime() fun
f7b70 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74 ction. So creat
f7b80 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 e a.** substitut
f7b90 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d 20 e..*/.struct tm
f7ba0 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74 69 *__cdecl localti
f7bb0 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 me(const time_t
f7bc0 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20 73 *t).{. static s
f7bd0 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46 49 truct tm y;. FI
f7be0 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b LETIME uTm, lTm;
f7bf0 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70 54 . SYSTEMTIME pT
f7c00 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 m;. sqlite3_int
f7c10 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 3d 20 64 t64;. t64 =
f7c20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 36 34 *t;. t64 = (t64
f7c30 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 29 2a + 11644473600)*
f7c40 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 6d 2e 10000000;. uTm.
f7c50 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d 20 dwLowDateTime =
f7c60 74 36 34 20 26 20 30 78 46 46 46 46 46 46 46 46 t64 & 0xFFFFFFFF
f7c70 3b 0a 20 20 75 54 6d 2e 64 77 48 69 67 68 44 61 ;. uTm.dwHighDa
f7c80 74 65 54 69 6d 65 3d 20 74 36 34 20 3e 3e 20 33 teTime= t64 >> 3
f7c90 32 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 2;. FileTimeToL
f7ca0 6f 63 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 ocalFileTime(&uT
f7cb0 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 m,&lTm);. FileT
f7cc0 69 6d 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 imeToSystemTime(
f7cd0 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e &lTm,&pTm);. y.
f7ce0 74 6d 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 tm_year = pTm.wY
f7cf0 65 61 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e ear - 1900;. y.
f7d00 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f tm_mon = pTm.wMo
f7d10 6e 74 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f nth - 1;. y.tm_
f7d20 77 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f wday = pTm.wDayO
f7d30 66 57 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 fWeek;. y.tm_md
f7d40 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 ay = pTm.wDay;.
f7d50 20 79 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d y.tm_hour = pTm
f7d60 2e 77 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d .wHour;. y.tm_m
f7d70 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 in = pTm.wMinute
f7d80 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 ;. y.tm_sec = p
f7d90 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 Tm.wSecond;. re
f7da0 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 turn &y;.}../* T
f7db0 68 69 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 his will never b
f7dc0 65 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 e called, but de
f7dd0 66 69 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 fined to make th
f7de0 65 20 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a e code compile *
f7df0 2f 0a 23 64 65 66 69 6e 65 20 47 65 74 54 65 6d /.#define GetTem
f7e00 70 50 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 pPathA(a,b)..#de
f7e10 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c fine LockFile(a,
f7e20 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 20 20 77 b,c,d,e) w
f7e30 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c inceLockFile(&a,
f7e40 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 b, c, d, e).#de
f7e50 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 fine UnlockFile(
f7e60 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 a,b,c,d,e) w
f7e70 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 inceUnlockFile(&
f7e80 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 a, b, c, d, e).#
f7e90 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 define LockFileE
f7ea0 78 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 x(a,b,c,d,e,f)
f7eb0 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 winceLockFileEx
f7ec0 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c (&a, b, c, d, e,
f7ed0 20 66 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e f)..#define HAN
f7ee0 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 DLE_TO_WINFILE(a
f7ef0 29 20 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 ) (winFile*)&((c
f7f00 68 61 72 2a 29 61 29 5b 2d 6f 66 66 73 65 74 6f har*)a)[-offseto
f7f10 66 28 77 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a 2f f(winFile,h)]../
f7f20 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 6c *.** Acquire a l
f7f30 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61 6e 64 6c ock on the handl
f7f40 65 20 68 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f e h.*/.static vo
f7f50 69 64 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 id winceMutexAcq
f7f60 75 69 72 65 28 48 41 4e 44 4c 45 20 68 29 7b 0a uire(HANDLE h){.
f7f70 20 20 20 44 57 4f 52 44 20 64 77 45 72 72 3b 0a DWORD dwErr;.
f7f80 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 64 77 45 do {. dwE
f7f90 72 72 20 3d 20 57 61 69 74 46 6f 72 53 69 6e 67 rr = WaitForSing
f7fa0 6c 65 4f 62 6a 65 63 74 28 68 2c 20 49 4e 46 49 leObject(h, INFI
f7fb0 4e 49 54 45 29 3b 0a 20 20 20 7d 20 77 68 69 6c NITE);. } whil
f7fc0 65 20 28 64 77 45 72 72 20 21 3d 20 57 41 49 54 e (dwErr != WAIT
f7fd0 5f 4f 42 4a 45 43 54 5f 30 20 26 26 20 64 77 45 _OBJECT_0 && dwE
f7fe0 72 72 20 21 3d 20 57 41 49 54 5f 41 42 41 4e 44 rr != WAIT_ABAND
f7ff0 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 ONED);.}./*.** R
f8000 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b 20 61 63 elease a lock ac
f8010 71 75 69 72 65 64 20 62 79 20 77 69 6e 63 65 4d quired by winceM
f8020 75 74 65 78 41 63 71 75 69 72 65 28 29 0a 2a 2f utexAcquire().*/
f8030 0a 23 64 65 66 69 6e 65 20 77 69 6e 63 65 4d 75 .#define winceMu
f8040 74 65 78 52 65 6c 65 61 73 65 28 68 29 20 52 65 texRelease(h) Re
f8050 6c 65 61 73 65 4d 75 74 65 78 28 68 29 0a 0a 2f leaseMutex(h)../
f8060 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20 *.** Create the
f8070 6d 75 74 65 78 20 61 6e 64 20 73 68 61 72 65 64 mutex and shared
f8080 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f 72 memory used for
f8090 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 locking in the
f80a0 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 file.** descript
f80b0 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 or pFile.*/.stat
f80c0 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 43 72 65 ic BOOL winceCre
f80d0 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74 20 63 68 ateLock(const ch
f80e0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 ar *zFilename, w
f80f0 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a inFile *pFile){.
f8100 20 20 57 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a 20 WCHAR *zTok;.
f8110 20 57 43 48 41 52 20 2a 7a 4e 61 6d 65 20 3d 20 WCHAR *zName =
f8120 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 utf8ToUnicode(zF
f8130 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f 4c ilename);. BOOL
f8140 20 62 49 6e 69 74 20 3d 20 54 52 55 45 3b 0a 0a bInit = TRUE;..
f8150 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 /* Initialize
f8160 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 61 the local lockda
f8170 74 61 20 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d 6f ta */. ZeroMemo
f8180 72 79 28 26 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c ry(&pFile->local
f8190 2c 20 73 69 7a 65 6f 66 28 70 46 69 6c 65 2d 3e , sizeof(pFile->
f81a0 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20 52 local));.. /* R
f81b0 65 70 6c 61 63 65 20 74 68 65 20 62 61 63 6b 73 eplace the backs
f81c0 6c 61 73 68 65 73 20 66 72 6f 6d 20 74 68 65 20 lashes from the
f81d0 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 6c 6f 77 filename and low
f81e0 65 72 63 61 73 65 20 69 74 0a 20 20 2a 2a 20 74 ercase it. ** t
f81f0 6f 20 64 65 72 69 76 65 20 61 20 6d 75 74 65 78 o derive a mutex
f8200 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f 6b name. */. zTok
f8210 20 3d 20 43 68 61 72 4c 6f 77 65 72 57 28 7a 4e = CharLowerW(zN
f8220 61 6d 65 29 3b 0a 20 20 66 6f 72 20 28 3b 2a 7a ame);. for (;*z
f8230 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 20 Tok;zTok++){.
f8240 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c if (*zTok == '\
f8250 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b \') *zTok = '_';
f8260 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 . }.. /* Creat
f8270 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61 6d 65 64 e/open the named
f8280 20 6d 75 74 65 78 20 2a 2f 0a 20 20 70 46 69 6c mutex */. pFil
f8290 65 2d 3e 68 4d 75 74 65 78 20 3d 20 43 72 65 61 e->hMutex = Crea
f82a0 74 65 4d 75 74 65 78 57 28 4e 55 4c 4c 2c 20 46 teMutexW(NULL, F
f82b0 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 ALSE, zName);.
f82c0 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 if (!pFile->hMut
f82d0 65 78 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 4e ex){. free(zN
f82e0 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ame);. return
f82f0 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f FALSE;. }.. /
f8300 2a 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 * Acquire the mu
f8310 74 65 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 tex before conti
f8320 6e 75 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 nuing */. wince
f8330 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 MutexAcquire(pFi
f8340 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 0a le->hMutex);. .
f8350 20 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 6e /* Since the n
f8360 61 6d 65 73 20 6f 66 20 6e 61 6d 65 64 20 6d 75 ames of named mu
f8370 74 65 78 65 73 2c 20 73 65 6d 61 70 68 6f 72 65 texes, semaphore
f8380 73 2c 20 66 69 6c 65 20 6d 61 70 70 69 6e 67 73 s, file mappings
f8390 20 65 74 63 20 61 72 65 20 0a 20 20 2a 2a 20 63 etc are . ** c
f83a0 61 73 65 2d 73 65 6e 73 69 74 69 76 65 2c 20 74 ase-sensitive, t
f83b0 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 ake advantage of
f83c0 20 74 68 61 74 20 62 79 20 75 70 70 65 72 63 61 that by upperca
f83d0 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 20 6e sing the mutex n
f83e0 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 75 73 69 ame. ** and usi
f83f0 6e 67 20 74 68 61 74 20 61 73 20 74 68 65 20 73 ng that as the s
f8400 68 61 72 65 64 20 66 69 6c 65 6d 61 70 70 69 6e hared filemappin
f8410 67 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43 g name.. */. C
f8420 68 61 72 55 70 70 65 72 57 28 7a 4e 61 6d 65 29 harUpperW(zName)
f8430 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 ;. pFile->hShar
f8440 65 64 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d ed = CreateFileM
f8450 61 70 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f appingW(INVALID_
f8460 48 41 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 HANDLE_VALUE, NU
f8470 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 LL,.
f8480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8490 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 5f PAGE_
f84a0 52 45 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69 READWRITE, 0, si
f84b0 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c zeof(winceLock),
f84c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
f84d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f84e0 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20 zName);
f84f0 20 0a 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c .. /* Set a fl
f8500 61 67 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 ag that indicate
f8510 73 20 77 65 27 72 65 20 74 68 65 20 66 69 72 73 s we're the firs
f8520 74 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 t to create the
f8530 6d 65 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20 memory so it .
f8540 2a 2a 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d ** must be zero-
f8550 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
f8560 20 69 66 20 28 47 65 74 4c 61 73 74 45 72 72 6f if (GetLastErro
f8570 72 28 29 20 3d 3d 20 45 52 52 4f 52 5f 41 4c 52 r() == ERROR_ALR
f8580 45 41 44 59 5f 45 58 49 53 54 53 29 7b 0a 20 20 EADY_EXISTS){.
f8590 20 20 62 49 6e 69 74 20 3d 20 46 41 4c 53 45 3b bInit = FALSE;
f85a0 0a 20 20 7d 0a 0a 20 20 66 72 65 65 28 7a 4e 61 . }.. free(zNa
f85b0 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 me);.. /* If we
f85c0 20 73 75 63 63 65 65 64 65 64 20 69 6e 20 6d 61 succeeded in ma
f85d0 6b 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 king the shared
f85e0 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 2c 20 6d memory handle, m
f85f0 61 70 20 69 74 2e 20 2a 2f 0a 20 20 69 66 20 28 ap it. */. if (
f8600 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 7b pFile->hShared){
f8610 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 . pFile->shar
f8620 65 64 20 3d 20 28 77 69 6e 63 65 4c 6f 63 6b 2a ed = (winceLock*
f8630 29 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70 )MapViewOfFile(p
f8640 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 2c 20 0a File->hShared, .
f8650 20 20 20 20 20 20 20 20 20 20 20 20 20 46 49 4c FIL
f8660 45 5f 4d 41 50 5f 52 45 41 44 7c 46 49 4c 45 5f E_MAP_READ|FILE_
f8670 4d 41 50 5f 57 52 49 54 45 2c 20 30 2c 20 30 2c MAP_WRITE, 0, 0,
f8680 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 sizeof(winceLoc
f8690 6b 29 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 6d k));. /* If m
f86a0 61 70 70 69 6e 67 20 66 61 69 6c 65 64 2c 20 63 apping failed, c
f86b0 6c 6f 73 65 20 74 68 65 20 73 68 61 72 65 64 20 lose the shared
f86c0 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 61 6e memory handle an
f86d0 64 20 65 72 61 73 65 20 69 74 20 2a 2f 0a 20 20 d erase it */.
f86e0 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 73 68 if (!pFile->sh
f86f0 61 72 65 64 29 7b 0a 20 20 20 20 20 20 43 6c 6f ared){. Clo
f8700 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e seHandle(pFile->
f8710 68 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 hShared);.
f8720 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d pFile->hShared =
f8730 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d NULL;. }. }
f8740 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61 72 65 64 .. /* If shared
f8750 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64 20 6e 6f memory could no
f8760 74 20 62 65 20 63 72 65 61 74 65 64 2c 20 74 68 t be created, th
f8770 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 6d 75 74 en close the mut
f8780 65 78 20 61 6e 64 20 66 61 69 6c 20 2a 2f 0a 20 ex and fail */.
f8790 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 if (pFile->hSha
f87a0 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 20 red == NULL){.
f87b0 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 winceMutexRele
f87c0 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 ase(pFile->hMute
f87d0 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e x);. CloseHan
f87e0 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 dle(pFile->hMute
f87f0 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 x);. pFile->h
f8800 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 Mutex = NULL;.
f8810 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a return FALSE;.
f8820 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 6e 69 74 }. . /* Init
f8830 69 61 6c 69 7a 65 20 74 68 65 20 73 68 61 72 65 ialize the share
f8840 64 20 6d 65 6d 6f 72 79 20 69 66 20 77 65 27 72 d memory if we'r
f8850 65 20 73 75 70 70 6f 73 65 64 20 74 6f 20 2a 2f e supposed to */
f8860 0a 20 20 69 66 20 28 62 49 6e 69 74 29 20 7b 0a . if (bInit) {.
f8870 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 70 ZeroMemory(p
f8880 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c 20 73 69 File->shared, si
f8890 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 zeof(winceLock))
f88a0 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 ;. }.. winceMu
f88b0 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 texRelease(pFile
f88c0 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 ->hMutex);. ret
f88d0 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a urn TRUE;.}../*.
f88e0 2a 2a 20 44 65 73 74 72 6f 79 20 74 68 65 20 70 ** Destroy the p
f88f0 61 72 74 20 6f 66 20 77 69 6e 46 69 6c 65 20 74 art of winFile t
f8900 68 61 74 20 64 65 61 6c 73 20 77 69 74 68 20 77 hat deals with w
f8910 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 ince locks.*/.st
f8920 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65 44 atic void winceD
f8930 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69 6e 46 69 estroyLock(winFi
f8940 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 66 le *pFile){. if
f8950 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 (pFile->hMutex)
f8960 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65 {. /* Acquire
f8970 20 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 the mutex */.
f8980 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 winceMutexAcqu
f8990 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 ire(pFile->hMute
f89a0 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 x);.. /* The
f89b0 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 73 following blocks
f89c0 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 should probably
f89d0 20 61 73 73 65 72 74 20 69 6e 20 64 65 62 75 67 assert in debug
f89e0 20 6d 6f 64 65 2c 20 62 75 74 20 74 68 65 79 0a mode, but they.
f89f0 20 20 20 20 20 20 20 61 72 65 20 74 6f 20 63 6c are to cl
f8a00 65 61 6e 75 70 20 69 6e 20 63 61 73 65 20 61 6e eanup in case an
f8a10 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69 6e 65 64 y locks remained
f8a20 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69 66 20 open */. if
f8a30 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 (pFile->local.nR
f8a40 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 70 eaders){. p
f8a50 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 File->shared->nR
f8a60 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 7d eaders --;. }
f8a70 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f8a80 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 local.bReserved)
f8a90 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 {. pFile->s
f8aa0 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 hared->bReserved
f8ab0 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a = FALSE;. }.
f8ac0 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c if (pFile->l
f8ad0 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b 0a ocal.bPending){.
f8ae0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
f8af0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 red->bPending =
f8b00 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 FALSE;. }.
f8b10 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 if (pFile->loca
f8b20 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 l.bExclusive){.
f8b30 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 pFile->shar
f8b40 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d ed->bExclusive =
f8b50 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 FALSE;. }..
f8b60 20 20 20 2f 2a 20 44 65 2d 72 65 66 65 72 65 6e /* De-referen
f8b70 63 65 20 61 6e 64 20 63 6c 6f 73 65 20 6f 75 72 ce and close our
f8b80 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 68 61 copy of the sha
f8b90 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c red memory handl
f8ba0 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61 70 56 69 e */. UnmapVi
f8bb0 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e ewOfFile(pFile->
f8bc0 73 68 61 72 65 64 29 3b 0a 20 20 20 20 43 6c 6f shared);. Clo
f8bd0 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e seHandle(pFile->
f8be0 68 53 68 61 72 65 64 29 3b 0a 0a 20 20 20 20 2f hShared);.. /
f8bf0 2a 20 44 6f 6e 65 20 77 69 74 68 20 74 68 65 20 * Done with the
f8c00 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e mutex */. win
f8c10 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 ceMutexRelease(p
f8c20 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20 File->hMutex);
f8c30 20 20 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 . CloseHand
f8c40 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 le(pFile->hMutex
f8c50 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d );. pFile->hM
f8c60 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d utex = NULL;. }
f8c70 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d .}../* .** An im
f8c80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
f8c90 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20 41 the LockFile() A
f8ca0 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f PI of windows fo
f8cb0 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 r wince.*/.stati
f8cc0 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b c BOOL winceLock
f8cd0 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a File(. HANDLE *
f8ce0 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 phFile,. DWORD
f8cf0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c dwFileOffsetLow,
f8d00 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f . DWORD dwFileO
f8d10 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f ffsetHigh,. DWO
f8d20 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 RD nNumberOfByte
f8d30 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 sToLockLow,. DW
f8d40 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 ORD nNumberOfByt
f8d50 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a esToLockHigh.){.
f8d60 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
f8d70 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e = HANDLE_TO_WIN
f8d80 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 FILE(phFile);.
f8d90 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 BOOL bReturn = F
f8da0 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 ALSE;.. if (!pF
f8db0 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 ile->hMutex) ret
f8dc0 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 urn TRUE;. winc
f8dd0 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 eMutexAcquire(pF
f8de0 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 ile->hMutex);..
f8df0 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 /* Wanting an e
f8e00 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a xclusive lock? *
f8e10 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66 /. if (dwFileOf
f8e20 66 73 65 74 4c 6f 77 20 3d 3d 20 53 48 41 52 45 fsetLow == SHARE
f8e30 44 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20 26 D_FIRST. &
f8e40 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 & nNumberOfBytes
f8e50 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 ToLockLow == SHA
f8e60 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 RED_SIZE){. i
f8e70 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 f (pFile->shared
f8e80 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 20 ->nReaders == 0
f8e90 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 && pFile->shared
f8ea0 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 ->bExclusive ==
f8eb0 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 0){. pFile
f8ec0 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 ->shared->bExclu
f8ed0 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 sive = TRUE;.
f8ee0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c pFile->local
f8ef0 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 .bExclusive = TR
f8f00 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74 75 UE;. bRetu
f8f10 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d rn = TRUE;. }
f8f20 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 . }.. /* Want
f8f30 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b a read-only lock
f8f40 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 ? */. else if (
f8f50 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 (dwFileOffsetLow
f8f60 20 3e 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 >= SHARED_FIRST
f8f70 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 &&.
f8f80 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
f8f90 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b < SHARED_FIRST +
f8fa0 20 53 48 41 52 45 44 5f 53 49 5a 45 29 20 26 26 SHARED_SIZE) &&
f8fb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 . nNu
f8fc0 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 mberOfBytesToLoc
f8fd0 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 kLow == 1){.
f8fe0 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 if (pFile->share
f8ff0 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d d->bExclusive ==
f9000 20 30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 0){. pFile
f9010 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 ->local.nReaders
f9020 20 2b 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 ++;. if (p
f9030 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 File->local.nRea
f9040 64 65 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 ders == 1){.
f9050 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
f9060 64 2d 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a d->nReaders ++;.
f9070 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 }. bR
f9080 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 eturn = TRUE;.
f9090 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 }. }.. /* Wa
f90a0 6e 74 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 nt a pending loc
f90b0 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 k? */. else if
f90c0 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 (dwFileOffsetLow
f90d0 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 == PENDING_BYTE
f90e0 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 && nNumberOfByt
f90f0 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 esToLockLow == 1
f9100 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 ){. /* If no
f9110 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 pending lock has
f9120 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20 been acquired,
f9130 74 68 65 6e 20 61 63 71 75 69 72 65 20 69 74 20 then acquire it
f9140 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 */. if (pFile
f9150 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 ->shared->bPendi
f9160 6e 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 ng == 0) {.
f9170 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
f9180 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b bPending = TRUE;
f9190 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f . pFile->lo
f91a0 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 cal.bPending = T
f91b0 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 RUE;. bRetu
f91c0 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d rn = TRUE;. }
f91d0 0a 20 20 7d 0a 20 20 2f 2a 20 57 61 6e 74 20 61 . }. /* Want a
f91e0 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 reserved lock?
f91f0 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 */. else if (dw
f9200 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d FileOffsetLow ==
f9210 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 RESERVED_BYTE &
f9220 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 & nNumberOfBytes
f9230 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b ToLockLow == 1){
f9240 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f9250 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 shared->bReserve
f9260 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 d == 0) {.
f9270 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
f9280 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b Reserved = TRUE;
f9290 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f . pFile->lo
f92a0 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20 cal.bReserved =
f92b0 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 TRUE;. bRet
f92c0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
f92d0 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 }. }.. winceMu
f92e0 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 texRelease(pFile
f92f0 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 ->hMutex);. ret
f9300 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a urn bReturn;.}..
f9310 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 /*.** An impleme
f9320 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 ntation of the U
f9330 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 nlockFile API of
f9340 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e windows for win
f9350 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f ce.*/.static BOO
f9360 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c L winceUnlockFil
f9370 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 e(. HANDLE *phF
f9380 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 ile,. DWORD dwF
f9390 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 ileOffsetLow,.
f93a0 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 DWORD dwFileOffs
f93b0 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 etHigh,. DWORD
f93c0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f93d0 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f UnlockLow,. DWO
f93e0 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 RD nNumberOfByte
f93f0 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b sToUnlockHigh.){
f9400 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c . winFile *pFil
f9410 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 e = HANDLE_TO_WI
f9420 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 NFILE(phFile);.
f9430 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 BOOL bReturn =
f9440 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 FALSE;.. if (!p
f9450 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 File->hMutex) re
f9460 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e turn TRUE;. win
f9470 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 ceMutexAcquire(p
f9480 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a File->hMutex);..
f9490 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 /* Releasing a
f94a0 20 72 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 reader lock or
f94b0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
f94c0 6b 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c k */. if (dwFil
f94d0 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53 48 eOffsetLow >= SH
f94e0 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20 20 ARED_FIRST &&.
f94f0 20 20 20 20 20 64 77 46 69 6c 65 4f 66 66 73 65 dwFileOffse
f9500 74 4c 6f 77 20 3c 20 53 48 41 52 45 44 5f 46 49 tLow < SHARED_FI
f9510 52 53 54 20 2b 20 53 48 41 52 45 44 5f 53 49 5a RST + SHARED_SIZ
f9520 45 29 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 E){. /* Did w
f9530 65 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 e have an exclus
f9540 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 ive lock? */.
f9550 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 if (pFile->loca
f9560 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 l.bExclusive){.
f9570 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
f9580 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 l.bExclusive = F
f9590 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c ALSE;. pFil
f95a0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c e->shared->bExcl
f95b0 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 usive = FALSE;.
f95c0 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 bReturn = T
f95d0 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 RUE;. }..
f95e0 2f 2a 20 44 69 64 20 77 65 20 6a 75 73 74 20 68 /* Did we just h
f95f0 61 76 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 ave a reader loc
f9600 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 k? */. else i
f9610 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
f9620 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20 nReaders){.
f9630 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 pFile->local.nR
f9640 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 eaders --;.
f9650 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 if (pFile->loca
f9660 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 29 l.nReaders == 0)
f9670 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 . {.
f9680 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
f9690 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 nReaders --;.
f96a0 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 }. bRetu
f96b0 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d rn = TRUE;. }
f96c0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 . }.. /* Relea
f96d0 73 69 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 6c sing a pending l
f96e0 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 ock */. else if
f96f0 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f (dwFileOffsetLo
f9700 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 w == PENDING_BYT
f9710 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 E && nNumberOfBy
f9720 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d tesToUnlockLow =
f9730 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 = 1){. if (pF
f9740 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 ile->local.bPend
f9750 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69 6c ing){. pFil
f9760 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e e->local.bPendin
f9770 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 g = FALSE;.
f9780 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
f9790 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 bPending = FALSE
f97a0 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 ;. bReturn
f97b0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 = TRUE;. }.
f97c0 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 }. /* Releasing
f97d0 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b a reserved lock
f97e0 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
f97f0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
f9800 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 = RESERVED_BYTE
f9810 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 && nNumberOfByte
f9820 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 sToUnlockLow ==
f9830 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 1){. if (pFil
f9840 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 e->local.bReserv
f9850 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c ed) {. pFil
f9860 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 e->local.bReserv
f9870 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 ed = FALSE;.
f9880 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f9890 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c >bReserved = FAL
f98a0 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 SE;. bRetur
f98b0 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a n = TRUE;. }.
f98c0 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 }.. winceMute
f98d0 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e xRelease(pFile->
f98e0 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 hMutex);. retur
f98f0 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a n bReturn;.}../*
f9900 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 .** An implement
f9910 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 ation of the Loc
f9920 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20 6f 66 kFileEx() API of
f9930 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e windows for win
f9940 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f ce.*/.static BOO
f9950 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 L winceLockFileE
f9960 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 x(. HANDLE *phF
f9970 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 ile,. DWORD dwF
f9980 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 64 77 lags,. DWORD dw
f9990 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57 4f 52 Reserved,. DWOR
f99a0 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 D nNumberOfBytes
f99b0 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f ToLockLow,. DWO
f99c0 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 RD nNumberOfByte
f99d0 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 20 4c sToLockHigh,. L
f99e0 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70 4f 76 POVERLAPPED lpOv
f99f0 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20 2f 2a erlapped.){. /*
f9a00 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 77 If the caller w
f9a10 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72 65 ants a shared re
f9a20 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 64 ad lock, forward
f9a30 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a 20 this call. **
f9a40 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 to winceLockFile
f9a50 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65 72 */. if (lpOver
f9a60 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20 3d lapped->Offset =
f9a70 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 = SHARED_FIRST &
f9a80 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67 73 20 &. dwFlags
f9a90 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20 6e 4e == 1 &&. nN
f9aa0 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f umberOfBytesToLo
f9ab0 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f ckLow == SHARED_
f9ac0 53 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72 SIZE){. retur
f9ad0 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 n winceLockFile(
f9ae0 70 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 phFile, SHARED_F
f9af0 49 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a IRST, 0, 1, 0);.
f9b00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c }. return FAL
f9b10 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 SE;.}./*.** End
f9b20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 of the special c
f9b30 6f 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a ode for wince.**
f9b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e ***********/.#en
f9b90 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 dif /* SQLITE_OS
f9ba0 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a _WINCE */../****
f9bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 *********.** The
f9c00 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 next group of r
f9c10 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e outines implemen
f9c20 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 t the I/O method
f9c30 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 s specified.** b
f9c40 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f y the sqlite3_io
f9c50 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e _methods object.
f9c60 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
f9c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
f9cb0 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 ../*.** Close a
f9cc0 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 file..**.** It i
f9cd0 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 s reported that
f9ce0 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c an attempt to cl
f9cf0 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 ose a handle mig
f9d00 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 ht sometimes.**
f9d10 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61 fail. This is a
f9d20 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 very unreasonab
f9d30 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 77 le result, but w
f9d40 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 indows is notori
f9d50 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 ous.** for being
f9d60 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f unreasonable so
f9d70 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 I do not doubt
f9d80 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 61 that it might ha
f9d90 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 ppen. If.** the
f9da0 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 close fails, we
f9db0 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d pause for 100 m
f9dc0 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 illiseconds and
f9dd0 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a try again. As.*
f9de0 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f * many as MX_CLO
f9df0 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d SE_ATTEMPT attem
f9e00 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 pts to close the
f9e10 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 handle are made
f9e20 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e before.** givin
f9e30 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 g up and returni
f9e40 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a ng an error..*/.
f9e50 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 #define MX_CLOSE
f9e60 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69 _ATTEMPT 3.stati
f9e70 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 c int winClose(s
f9e80 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
f9e90 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 {. int rc, cnt
f9ea0 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a = 0;. winFile *
f9eb0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
f9ec0 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 32 *)id;. OSTRACE2
f9ed0 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 ("CLOSE %d\n", p
f9ee0 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 64 6f 7b 0a File->h);. do{.
f9ef0 20 20 20 20 72 63 20 3d 20 43 6c 6f 73 65 48 61 rc = CloseHa
f9f00 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a ndle(pFile->h);.
f9f10 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 30 20 }while( rc==0
f9f20 26 26 20 2b 2b 63 6e 74 20 3c 20 4d 58 5f 43 4c && ++cnt < MX_CL
f9f30 4f 53 45 5f 41 54 54 45 4d 50 54 20 26 26 20 28 OSE_ATTEMPT && (
f9f40 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 Sleep(100), 1) )
f9f50 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ;.#if SQLITE_OS_
f9f60 57 49 4e 43 45 0a 23 64 65 66 69 6e 65 20 57 49 WINCE.#define WI
f9f70 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 NCE_DELETION_ATT
f9f80 45 4d 50 54 53 20 33 0a 20 20 77 69 6e 63 65 44 EMPTS 3. winceD
f9f90 65 73 74 72 6f 79 4c 6f 63 6b 28 70 46 69 6c 65 estroyLock(pFile
f9fa0 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e );. if( pFile->
f9fb0 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 29 zDeleteOnClose )
f9fc0 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 {. int cnt =
f9fd0 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 0a 20 20 0;. while(.
f9fe0 20 20 20 20 20 20 20 20 20 44 65 6c 65 74 65 46 DeleteF
f9ff0 69 6c 65 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c ileW(pFile->zDel
fa000 65 74 65 4f 6e 43 6c 6f 73 65 29 3d 3d 30 0a 20 eteOnClose)==0.
fa010 20 20 20 20 20 20 20 26 26 20 47 65 74 46 69 6c && GetFil
fa020 65 41 74 74 72 69 62 75 74 65 73 57 28 70 46 69 eAttributesW(pFi
fa030 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f le->zDeleteOnClo
fa040 73 65 29 21 3d 30 78 66 66 66 66 66 66 66 66 20 se)!=0xffffffff
fa050 0a 20 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b . && cnt+
fa060 2b 20 3c 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 + < WINCE_DELETI
fa070 4f 4e 5f 41 54 54 45 4d 50 54 53 0a 20 20 20 20 ON_ATTEMPTS.
fa080 29 7b 0a 20 20 20 20 20 20 20 53 6c 65 65 70 28 ){. Sleep(
fa090 31 30 30 29 3b 20 20 2f 2a 20 57 61 69 74 20 61 100); /* Wait a
fa0a0 20 6c 69 74 74 6c 65 20 62 65 66 6f 72 65 20 74 little before t
fa0b0 72 79 69 6e 67 20 61 67 61 69 6e 20 2a 2f 0a 20 rying again */.
fa0c0 20 20 20 7d 0a 20 20 20 20 66 72 65 65 28 70 46 }. free(pF
fa0d0 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c ile->zDeleteOnCl
fa0e0 6f 73 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 ose);. }.#endif
fa0f0 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d . OpenCounter(-
fa100 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 1);. return rc
fa110 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 ? SQLITE_OK : SQ
fa120 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f LITE_IOERR;.}../
fa130 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 *.** Some micros
fa140 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 oft compilers la
fa150 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 ck this definiti
fa160 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 on..*/.#ifndef I
fa170 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f NVALID_SET_FILE_
fa180 50 4f 49 4e 54 45 52 0a 23 20 64 65 66 69 6e 65 POINTER.# define
fa190 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c INVALID_SET_FIL
fa1a0 45 5f 50 4f 49 4e 54 45 52 20 28 28 44 57 4f 52 E_POINTER ((DWOR
fa1b0 44 29 2d 31 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a D)-1).#endif../*
fa1c0 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 .** Read data fr
fa1d0 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 om a file into a
fa1e0 20 62 75 66 66 65 72 2e 20 20 52 65 74 75 72 6e buffer. Return
fa1f0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c SQLITE_OK if al
fa200 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 72 65 20 l.** bytes were
fa210 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c read successfull
fa220 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 y and SQLITE_IOE
fa230 52 52 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 RR if anything g
fa240 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f oes.** wrong..*/
fa250 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 52 .static int winR
fa260 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 ead(. sqlite3_f
fa270 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 ile *id,
fa280 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 /* File to rea
fa290 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 d from */. void
fa2a0 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 *pBuf,
fa2b0 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
fa2c0 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 content into thi
fa2d0 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e s buffer */. in
fa2e0 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 t amt,
fa2f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
fa300 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 er of bytes to r
fa310 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ead */. sqlite3
fa320 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 _int64 offset
fa330 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 /* Begin rea
fa340 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 ding at this off
fa350 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 set */.){. LONG
fa360 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f upperBits = (LO
fa370 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32 29 NG)((offset>>32)
fa380 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a & 0x7fffffff);.
fa390 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 LONG lowerBits
fa3a0 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74 = (LONG)(offset
fa3b0 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a & 0xffffffff);.
fa3c0 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 DWORD rc;. DW
fa3d0 4f 52 44 20 67 6f 74 3b 0a 20 20 77 69 6e 46 69 ORD got;. winFi
fa3e0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
fa3f0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 File*)id;. asse
fa400 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 rt( id!=0 );. S
fa410 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 imulateIOError(r
fa420 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
fa430 52 52 5f 52 45 41 44 29 3b 0a 20 20 4f 53 54 52 RR_READ);. OSTR
fa440 41 43 45 33 28 22 52 45 41 44 20 25 64 20 6c 6f ACE3("READ %d lo
fa450 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ck=%d\n", pFile-
fa460 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 >h, pFile->lockt
fa470 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 ype);. rc = Set
fa480 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c FilePointer(pFil
fa490 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c e->h, lowerBits,
fa4a0 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c &upperBits, FIL
fa4b0 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 E_BEGIN);. if(
fa4c0 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f rc==INVALID_SET_
fa4d0 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 FILE_POINTER &&
fa4e0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 21 3d GetLastError()!=
fa4f0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
fa500 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 return SQLITE_FU
fa510 4c 4c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 52 LL;. }. if( !R
fa520 65 61 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 eadFile(pFile->h
fa530 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f , pBuf, amt, &go
fa540 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 t, 0) ){. ret
fa550 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
fa560 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 _READ;. }. if(
fa570 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d 74 got==(DWORD)amt
fa580 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
fa590 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
fa5a0 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 e{. /* Unread
fa5b0 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 parts of the bu
fa5c0 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 ffer must be zer
fa5d0 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 o-filled */.
fa5e0 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 memset(&((char*)
fa5f0 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 pBuf)[got], 0, a
fa600 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 mt-got);. ret
fa610 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
fa620 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d _SHORT_READ;. }
fa630 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 .}../*.** Write
fa640 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 data from a buff
fa650 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 er into a file.
fa660 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
fa670 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 K on success.**
fa680 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 or some other er
fa690 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c ror code on fail
fa6a0 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ure..*/.static i
fa6b0 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73 nt winWrite(. s
fa6c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fa6d0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
fa6e0 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a to write into *
fa6f0 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
fa700 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 2f 2a pBuf, /*
fa710 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 The bytes to be
fa720 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e written */. in
fa730 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 t amt,
fa740 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
fa750 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 r of bytes to wr
fa760 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ite */. sqlite3
fa770 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 _int64 offset
fa780 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 /* Offset int
fa790 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 o the file to be
fa7a0 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a gin writing at *
fa7b0 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 /.){. LONG uppe
fa7c0 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 rBits = (LONG)((
fa7d0 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 offset>>32) & 0x
fa7e0 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 7fffffff);. LON
fa7f0 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 4c G lowerBits = (L
fa800 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30 78 ONG)(offset & 0x
fa810 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f ffffffff);. DWO
fa820 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 77 RD rc;. DWORD w
fa830 72 6f 74 65 20 3d 20 30 3b 0a 20 20 77 69 6e 46 rote = 0;. winF
fa840 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 ile *pFile = (wi
fa850 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 nFile*)id;. ass
fa860 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
fa870 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
fa880 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
fa890 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 69 ERR_WRITE);. Si
fa8a0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
fa8b0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
fa8c0 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41 E_FULL);. OSTRA
fa8d0 43 45 33 28 22 57 52 49 54 45 20 25 64 20 6c 6f CE3("WRITE %d lo
fa8e0 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ck=%d\n", pFile-
fa8f0 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 >h, pFile->lockt
fa900 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 ype);. rc = Set
fa910 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c FilePointer(pFil
fa920 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c e->h, lowerBits,
fa930 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c &upperBits, FIL
fa940 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 E_BEGIN);. if(
fa950 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f rc==INVALID_SET_
fa960 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 FILE_POINTER &&
fa970 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 21 3d GetLastError()!=
fa980 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
fa990 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 return SQLITE_FU
fa9a0 4c 4c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 LL;. }. assert
fa9b0 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 ( amt>0 );. whi
fa9c0 6c 65 28 0a 20 20 20 20 20 61 6d 74 3e 30 0a 20 le(. amt>0.
fa9d0 20 20 20 20 26 26 20 28 72 63 20 3d 20 57 72 69 && (rc = Wri
fa9e0 74 65 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c teFile(pFile->h,
fa9f0 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f pBuf, amt, &wro
faa00 74 65 2c 20 30 29 29 21 3d 30 0a 20 20 20 20 20 te, 0))!=0.
faa10 26 26 20 77 72 6f 74 65 3e 30 0a 20 20 29 7b 0a && wrote>0. ){.
faa20 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 amt -= wrote
faa30 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 ;. pBuf = &((
faa40 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 char*)pBuf)[wrot
faa50 65 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 72 e];. }. if( !r
faa60 63 20 7c 7c 20 61 6d 74 3e 28 69 6e 74 29 77 72 c || amt>(int)wr
faa70 6f 74 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ote ){. retur
faa80 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 n SQLITE_FULL;.
faa90 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
faaa0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
faab0 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e Truncate an open
faac0 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 file to a speci
faad0 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 fied size.*/.sta
faae0 74 69 63 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 tic int winTrunc
faaf0 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ate(sqlite3_file
fab00 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e *id, sqlite3_in
fab10 74 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 44 57 t64 nByte){. DW
fab20 4f 52 44 20 72 63 3b 0a 20 20 4c 4f 4e 47 20 75 ORD rc;. LONG u
fab30 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 pperBits = (LONG
fab40 29 28 28 6e 42 79 74 65 3e 3e 33 32 29 20 26 20 )((nByte>>32) &
fab50 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 0x7fffffff);. L
fab60 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 ONG lowerBits =
fab70 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20 26 20 30 (LONG)(nByte & 0
fab80 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 77 69 xffffffff);. wi
fab90 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 nFile *pFile = (
faba0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f winFile*)id;. O
fabb0 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 41 54 STRACE3("TRUNCAT
fabc0 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 E %d %lld\n", pF
fabd0 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b 0a ile->h, nByte);.
fabe0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
fabf0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f r(return SQLITE_
fac00 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29 3b IOERR_TRUNCATE);
fac10 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 . rc = SetFileP
fac20 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c ointer(pFile->h,
fac30 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 lowerBits, &upp
fac40 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 erBits, FILE_BEG
fac50 49 4e 29 3b 0a 20 20 69 66 28 20 49 4e 56 41 4c IN);. if( INVAL
fac60 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e ID_SET_FILE_POIN
fac70 54 45 52 20 21 3d 20 72 63 20 29 7b 0a 20 20 20 TER != rc ){.
fac80 20 2f 2a 20 53 65 74 45 6e 64 4f 66 46 69 6c 65 /* SetEndOfFile
fac90 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 20 6e 42 will fail if nB
faca0 79 74 65 20 69 73 20 6e 65 67 61 74 69 76 65 20 yte is negative
facb0 2a 2f 0a 20 20 20 20 69 66 28 20 53 65 74 45 6e */. if( SetEn
facc0 64 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 dOfFile(pFile->h
facd0 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
face0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
facf0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
fad00 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 SQLITE_IOERR_TRU
fad10 4e 43 41 54 45 3b 0a 7d 0a 0a 23 69 66 64 65 66 NCATE;.}..#ifdef
fad20 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a SQLITE_TEST./*.
fad30 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d ** Count the num
fad40 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 ber of fullsyncs
fad50 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 and normal sync
fad60 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 s. This is used
fad70 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 to test.** that
fad80 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 syncs and fulls
fad90 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72 69 6e yncs are occurin
fada0 67 20 61 74 20 74 68 65 20 72 69 67 68 74 20 74 g at the right t
fadb0 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f imes..*/.SQLITE_
fadc0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
fadd0 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a sync_count = 0;.
fade0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
fadf0 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f qlite3_fullsync_
fae00 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 count = 0;.#endi
fae10 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 f../*.** Make su
fae20 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f re all writes to
fae30 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 a particular fi
fae40 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 le are committed
fae50 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 to disk..*/.sta
fae60 74 69 63 20 69 6e 74 20 77 69 6e 53 79 6e 63 28 tic int winSync(
fae70 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
fae80 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 23 69 , int flags){.#i
fae90 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f fndef SQLITE_NO_
faea0 53 59 4e 43 0a 20 20 77 69 6e 46 69 6c 65 20 2a SYNC. winFile *
faeb0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
faec0 2a 29 69 64 3b 0a 23 65 6c 73 65 0a 20 20 55 4e *)id;.#else. UN
faed0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 USED_PARAMETER(i
faee0 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 4f 53 54 d);.#endif. OST
faef0 52 41 43 45 33 28 22 53 59 4e 43 20 25 64 20 6c RACE3("SYNC %d l
faf00 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 ock=%d\n", pFile
faf10 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
faf20 74 79 70 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 type);.#ifndef S
faf30 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 55 4e 55 QLITE_TEST. UNU
faf40 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 6c SED_PARAMETER(fl
faf50 61 67 73 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 ags);.#else. if
faf60 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
faf70 5f 53 59 4e 43 5f 46 55 4c 4c 20 29 7b 0a 20 20 _SYNC_FULL ){.
faf80 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 sqlite3_fullsy
faf90 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a nc_count++;. }.
fafa0 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 sqlite3_sync_c
fafb0 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 ount++;.#endif.
fafc0 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c /* If we compil
fafd0 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 ed with the SQLI
fafe0 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c TE_NO_SYNC flag,
faff0 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73 then syncing is
fb000 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 a. ** no-op.
fb010 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
fb020 5f 4e 4f 5f 53 59 4e 43 0a 20 20 20 20 72 65 74 _NO_SYNC. ret
fb030 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 urn SQLITE_OK;.#
fb040 65 6c 73 65 0a 20 20 69 66 28 20 46 6c 75 73 68 else. if( Flush
fb050 46 69 6c 65 42 75 66 66 65 72 73 28 70 46 69 6c FileBuffers(pFil
fb060 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 72 65 74 e->h) ){. ret
fb070 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
fb080 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
fb090 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
fb0a0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f . }.#endif.}../
fb0b0 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 *.** Determine t
fb0c0 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
fb0d0 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 of a file in byt
fb0e0 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 es.*/.static int
fb0f0 20 77 69 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c winFileSize(sql
fb100 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 ite3_file *id, s
fb110 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 qlite3_int64 *pS
fb120 69 7a 65 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 ize){. winFile
fb130 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c *pFile = (winFil
fb140 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 75 e*)id;. DWORD u
fb150 70 70 65 72 42 69 74 73 2c 20 6c 6f 77 65 72 42 pperBits, lowerB
fb160 69 74 73 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 its;. SimulateI
fb170 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 OError(return SQ
fb180 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 LITE_IOERR_FSTAT
fb190 29 3b 0a 20 20 6c 6f 77 65 72 42 69 74 73 20 3d );. lowerBits =
fb1a0 20 47 65 74 46 69 6c 65 53 69 7a 65 28 70 46 69 GetFileSize(pFi
fb1b0 6c 65 2d 3e 68 2c 20 26 75 70 70 65 72 42 69 74 le->h, &upperBit
fb1c0 73 29 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 s);. *pSize = (
fb1d0 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 ((sqlite3_int64)
fb1e0 75 70 70 65 72 42 69 74 73 29 3c 3c 33 32 29 20 upperBits)<<32)
fb1f0 2b 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 72 + lowerBits;. r
fb200 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
fb210 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 .}../*.** LOCKFI
fb220 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 LE_FAIL_IMMEDIAT
fb230 45 4c 59 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ELY is undefined
fb240 20 6f 6e 20 73 6f 6d 65 20 57 69 6e 64 6f 77 73 on some Windows
fb250 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 systems..*/.#if
fb260 6e 64 65 66 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 ndef LOCKFILE_FA
fb270 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 0a 23 IL_IMMEDIATELY.#
fb280 20 64 65 66 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 define LOCKFILE
fb290 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c _FAIL_IMMEDIATEL
fb2a0 59 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a Y 1.#endif../*.*
fb2b0 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64 * Acquire a read
fb2c0 65 72 20 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 er lock..** Diff
fb2d0 65 72 65 6e 74 20 41 50 49 20 72 6f 75 74 69 6e erent API routin
fb2e0 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 64 65 es are called de
fb2f0 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 pending on wheth
fb300 65 72 20 6f 72 20 6e 6f 74 20 74 68 69 73 0a 2a er or not this.*
fb310 2a 20 69 73 20 57 69 6e 39 35 20 6f 72 20 57 69 * is Win95 or Wi
fb320 6e 4e 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 nNT..*/.static i
fb330 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 77 nt getReadLock(w
fb340 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a inFile *pFile){.
fb350 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 int res;. if(
fb360 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 4f isNT() ){. O
fb370 56 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b 0a VERLAPPED ovlp;.
fb380 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20 ovlp.Offset
fb390 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a = SHARED_FIRST;.
fb3a0 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48 ovlp.OffsetH
fb3b0 69 67 68 20 3d 20 30 3b 0a 20 20 20 20 6f 76 6c igh = 0;. ovl
fb3c0 70 2e 68 45 76 65 6e 74 20 3d 20 30 3b 0a 20 20 p.hEvent = 0;.
fb3d0 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 res = LockFile
fb3e0 45 78 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 Ex(pFile->h, LOC
fb3f0 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 KFILE_FAIL_IMMED
fb400 49 41 54 45 4c 59 2c 0a 20 20 20 20 20 20 20 20 IATELY,.
fb410 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 0,
fb420 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 2c 20 SHARED_SIZE, 0,
fb430 26 6f 76 6c 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 &ovlp);./* isNT(
fb440 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
fb450 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
fb460 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
fb470 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
fb480 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
fb490 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 WINCE==0. }else
fb4a0 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 {. int lk;.
fb4b0 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d sqlite3_random
fb4c0 6e 65 73 73 28 73 69 7a 65 6f 66 28 6c 6b 29 2c ness(sizeof(lk),
fb4d0 20 26 6c 6b 29 3b 0a 20 20 20 20 70 46 69 6c 65 &lk);. pFile
fb4e0 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 ->sharedLockByte
fb4f0 20 3d 20 28 73 68 6f 72 74 29 28 28 6c 6b 20 26 = (short)((lk &
fb500 20 30 78 37 66 66 66 66 66 66 66 29 25 28 53 48 0x7fffffff)%(SH
fb510 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 29 3b ARED_SIZE - 1));
fb520 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 . res = LockF
fb530 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 ile(pFile->h, SH
fb540 41 52 45 44 5f 46 49 52 53 54 2b 70 46 69 6c 65 ARED_FIRST+pFile
fb550 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 ->sharedLockByte
fb560 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 , 0, 1, 0);.#end
fb570 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 if. }. return
fb580 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e res;.}../*.** Un
fb590 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f do a readlock.*/
fb5a0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f .static int unlo
fb5b0 63 6b 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 ckReadLock(winFi
fb5c0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e le *pFile){. in
fb5d0 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e t res;. if( isN
fb5e0 54 28 29 20 29 7b 0a 20 20 20 20 72 65 73 20 3d T() ){. res =
fb5f0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c UnlockFile(pFil
fb600 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 e->h, SHARED_FIR
fb610 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 ST, 0, SHARED_SI
fb620 5a 45 2c 20 30 29 3b 0a 2f 2a 20 69 73 4e 54 28 ZE, 0);./* isNT(
fb630 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
fb640 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
fb650 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
fb660 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
fb670 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
fb680 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 WINCE==0. }else
fb690 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f {. res = Unlo
fb6a0 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
fb6b0 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 SHARED_FIRST +
fb6c0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 pFile->sharedLoc
fb6d0 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b kByte, 0, 1, 0);
fb6e0 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 .#endif. }. re
fb6f0 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a turn res;.}../*.
fb700 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 ** Lock the file
fb710 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 with the lock s
fb720 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 pecified by para
fb730 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d meter locktype -
fb740 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 one.** of the f
fb750 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
fb760 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c (1) SHARED_L
fb770 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 OCK.** (2) R
fb780 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 ESERVED_LOCK.**
fb790 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f (3) PENDING_
fb7a0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 LOCK.** (4)
fb7b0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a EXCLUSIVE_LOCK.*
fb7c0 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
fb7d0 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f hen requesting o
fb7e0 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 ne lock state, a
fb7f0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 dditional lock s
fb800 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 tates.** are ins
fb810 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e erted in between
fb820 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d . The locking m
fb830 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 ight fail on one
fb840 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a of the later.**
fb850 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 transitions lea
fb860 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 ving the lock st
fb870 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 ate different fr
fb880 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 om what it start
fb890 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 ed but.** still
fb8a0 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 short of its goa
fb8b0 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e l. The followin
fb8c0 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 g chart shows th
fb8d0 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 e allowed.** tra
fb8e0 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
fb8f0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d inserted interm
fb900 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a ediate states:.*
fb910 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 *.** UNLOCKED
fb920 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 -> SHARED.**
fb930 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 SHARED -> RESER
fb940 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 VED.** SHARED
fb950 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
fb960 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
fb970 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 RESERVED -> (PE
fb980 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
fb990 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e IVE.** PENDIN
fb9a0 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a G -> EXCLUSIVE.*
fb9b0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
fb9c0 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 e will only incr
fb9d0 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68 ease a lock. Th
fb9e0 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72 6f e winUnlock() ro
fb9f0 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 utine.** erases
fba00 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 all locks at onc
fba10 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73 e and returns us
fba20 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 immediately to
fba30 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e locking level 0.
fba40 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f .** It is not po
fba50 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 ssible to lower
fba60 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
fba70 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20 l one step at a
fba80 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 time. You.** mu
fba90 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 74 st go straight t
fbaa0 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 o locking level
fbab0 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0..*/.static int
fbac0 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 65 33 winLock(sqlite3
fbad0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
fbae0 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 ocktype){. int
fbaf0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
fbb00 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
fbb10 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e e from subroutin
fbb20 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 es */. int res
fbb30 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 2f = 1; /
fbb40 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77 69 * Result of a wi
fbb50 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c 20 ndows lock call
fbb60 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b */. int newLock
fbb70 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 type; /* S
fbb80 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 et pFile->lockty
fbb90 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 pe to this value
fbba0 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 before exiting
fbbb0 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 */. int gotPend
fbbc0 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 ingLock = 0;/* T
fbbd0 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 rue if we acquir
fbbe0 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 ed a PENDING loc
fbbf0 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 k this time */.
fbc00 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
fbc10 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
fbc20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
fbc30 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 !=0 );. OSTRACE
fbc40 35 28 22 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 5("LOCK %d %d wa
fbc50 73 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 s %d(%d)\n",.
fbc60 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c pFile->h,
fbc70 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 locktype, pFile
fbc80 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c ->locktype, pFil
fbc90 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 e->sharedLockByt
fbca0 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 e);.. /* If the
fbcb0 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 re is already a
fbcc0 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 lock of this typ
fbcd0 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 e or more restri
fbce0 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a ctive on the. *
fbcf0 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 * OsFile, do not
fbd00 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 hing. Don't use
fbd10 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 the end_lock: ex
fbd20 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a it path, as. **
fbd30 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d sqlite3OsEnterM
fbd40 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 utex() hasn't be
fbd50 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 en called yet..
fbd60 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
fbd70 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 >locktype>=lockt
fbd80 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ype ){. retur
fbd90 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
fbda0 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
fbdb0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 the locking seq
fbdc0 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 uence is correct
fbdd0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
fbde0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 pFile->locktype!
fbdf0 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b =NO_LOCK || lock
fbe00 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
fbe10 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c K );. assert( l
fbe20 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 ocktype!=PENDING
fbe30 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
fbe40 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 t( locktype!=RES
fbe50 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 ERVED_LOCK || pF
fbe60 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 ile->locktype==S
fbe70 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 HARED_LOCK );..
fbe80 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e /* Lock the PEN
fbe90 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 DING_LOCK byte i
fbea0 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71 f we need to acq
fbeb0 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c uire a PENDING l
fbec0 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 ock or. ** a SH
fbed0 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 ARED lock. If w
fbee0 65 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 20 e are acquiring
fbef0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 a SHARED lock, t
fbf00 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f he acquisition o
fbf10 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 f. ** the PENDI
fbf20 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 NG_LOCK byte is
fbf30 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a temporary.. */.
fbf40 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 newLocktype =
fbf50 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b pFile->locktype;
fbf60 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
fbf70 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 0a cktype==NO_LOCK.
fbf80 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d || (locktype=
fbf90 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
fbfa0 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 && pFile->lockty
fbfb0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe==RESERVED_LOC
fbfc0 4b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 K). ){. int
fbfd0 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 cnt = 3;. whi
fbfe0 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 le( cnt-->0 && (
fbff0 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 res = LockFile(p
fc000 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 File->h, PENDING
fc010 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 _BYTE, 0, 1, 0))
fc020 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ==0 ){. /*
fc030 54 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 Try 3 times to g
fc040 65 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c et the pending l
fc050 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e ock. The pendin
fc060 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a g lock might be.
fc070 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 ** held by
fc080 20 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 another reader
fc090 70 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c process who will
fc0a0 20 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 release it mome
fc0b0 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a ntarily.. *
fc0c0 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 /. OSTRACE2
fc0d0 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 ("could not get
fc0e0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 a PENDING lock.
fc0f0 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b cnt=%d\n", cnt);
fc100 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b . Sleep(1);
fc110 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 . }. gotPe
fc120 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b ndingLock = res;
fc130 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
fc140 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b re a shared lock
fc150 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
fc160 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
fc170 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 K && res ){.
fc180 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
fc190 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b ocktype==NO_LOCK
fc1a0 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65 );. res = ge
fc1b0 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 tReadLock(pFile)
fc1c0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b ;. if( res ){
fc1d0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 . newLockty
fc1e0 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b pe = SHARED_LOCK
fc1f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
fc200 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 * Acquire a RESE
fc210 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 RVED lock. */.
fc220 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 if( locktype==R
fc230 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 ESERVED_LOCK &&
fc240 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72 res ){. asser
fc250 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
fc260 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
fc270 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 );. res = Loc
fc280 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
fc290 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 RESERVED_BYTE, 0
fc2a0 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 , 1, 0);. if(
fc2b0 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 res ){. ne
fc2c0 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 45 wLocktype = RESE
fc2d0 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d RVED_LOCK;. }
fc2e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
fc2f0 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 re a PENDING loc
fc300 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 k. */. if( loc
fc310 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
fc320 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a _LOCK && res ){.
fc330 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 newLocktype
fc340 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a = PENDING_LOCK;.
fc350 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f gotPendingLo
fc360 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f ck = 0;. }.. /
fc370 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 * Acquire an EXC
fc380 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f LUSIVE lock. */
fc390 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
fc3a0 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
fc3b0 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 && res ){. as
fc3c0 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 sert( pFile->loc
fc3d0 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f ktype>=SHARED_LO
fc3e0 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 CK );. res =
fc3f0 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 unlockReadLock(p
fc400 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 File);. OSTRA
fc410 43 45 32 28 22 75 6e 72 65 61 64 6c 6f 63 6b 20 CE2("unreadlock
fc420 3d 20 25 64 5c 6e 22 2c 20 72 65 73 29 3b 0a 20 = %d\n", res);.
fc430 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c res = LockFil
fc440 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 e(pFile->h, SHAR
fc450 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 ED_FIRST, 0, SHA
fc460 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 RED_SIZE, 0);.
fc470 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
fc480 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d newLocktype =
fc490 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b EXCLUSIVE_LOCK;
fc4a0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
fc4b0 20 20 4f 53 54 52 41 43 45 32 28 22 65 72 72 6f OSTRACE2("erro
fc4c0 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 r-code = %d\n",
fc4d0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 3b GetLastError());
fc4e0 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f . getReadLo
fc4f0 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d ck(pFile);. }
fc500 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 . }.. /* If we
fc510 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 are holding a P
fc520 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 ENDING lock that
fc530 20 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c ought to be rel
fc540 65 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a eased, then. **
fc550 20 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e release it now.
fc560 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 . */. if( gotP
fc570 65 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f endingLock && lo
fc580 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
fc590 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 OCK ){. Unloc
fc5a0 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
fc5b0 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c PENDING_BYTE, 0,
fc5c0 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 1, 0);. }.. /
fc5d0 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 74 61 * Update the sta
fc5e0 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 te of the lock h
fc5f0 61 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 as held in the f
fc600 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
fc610 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 hen. ** return
fc620 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
fc630 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a result code.. *
fc640 2f 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 /. if( res ){.
fc650 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
fc660 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
fc670 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 46 OSTRACE4("LOCK F
fc680 41 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 AILED %d trying
fc690 66 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 for %d but got %
fc6a0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a d\n", pFile->h,.
fc6b0 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 lockt
fc6c0 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 ype, newLocktype
fc6d0 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
fc6e0 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 TE_BUSY;. }. p
fc6f0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
fc700 20 28 75 38 29 6e 65 77 4c 6f 63 6b 74 79 70 65 (u8)newLocktype
fc710 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
fc720 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
fc730 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 tine checks if t
fc740 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 here is a RESERV
fc750 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 ED lock held on
fc760 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a the specified.**
fc770 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 file by this or
fc780 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 any other proce
fc790 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f ss. If such a lo
fc7a0 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74 75 ck is held, retu
fc7b0 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 rn.** non-zero,
fc7c0 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a otherwise zero..
fc7d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
fc7e0 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f nCheckReservedLo
fc7f0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
fc800 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 *id, int *pResOu
fc810 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 t){. int rc;.
fc820 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
fc830 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 (winFile*)id;.
fc840 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d assert( pFile!=
fc850 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 0 );. if( pFile
fc860 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 ->locktype>=RESE
fc870 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 RVED_LOCK ){.
fc880 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 rc = 1;. OST
fc890 52 41 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c RACE3("TEST WR-L
fc8a0 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c OCK %d %d (local
fc8b0 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 )\n", pFile->h,
fc8c0 72 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 rc);. }else{.
fc8d0 20 20 72 63 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 rc = LockFile(
fc8e0 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 pFile->h, RESERV
fc8f0 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 ED_BYTE, 0, 1, 0
fc900 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b );. if( rc ){
fc910 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c . UnlockFil
fc920 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 e(pFile->h, RESE
fc930 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c RVED_BYTE, 0, 1,
fc940 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 0);. }. r
fc950 63 20 3d 20 21 72 63 3b 0a 20 20 20 20 4f 53 54 c = !rc;. OST
fc960 52 41 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c RACE3("TEST WR-L
fc970 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d 6f 74 OCK %d %d (remot
fc980 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c e)\n", pFile->h,
fc990 20 72 63 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 rc);. }. *pRe
fc9a0 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 sOut = rc;. ret
fc9b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
fc9c0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 ../*.** Lower th
fc9d0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
fc9e0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 on file descript
fc9f0 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 or id to locktyp
fca00 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 e. locktype.**
fca10 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e must be either N
fca20 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 O_LOCK or SHARED
fca30 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 _LOCK..**.** If
fca40 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
fca50 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 l of the file de
fca60 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 scriptor is alre
fca70 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a ady at or below.
fca80 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 ** the requested
fca90 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 locking level,
fcaa0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
fcab0 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 a no-op..**.** I
fcac0 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
fcad0 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 e for this routi
fcae0 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 ne to fail if th
fcaf0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
fcb00 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e t.** is NO_LOCK.
fcb10 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 If the second
fcb20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 argument is SHAR
fcb30 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 ED_LOCK then thi
fcb40 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 s routine.** mig
fcb50 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ht return SQLITE
fcb60 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 _IOERR;.*/.stati
fcb70 63 20 69 6e 74 20 77 69 6e 55 6e 6c 6f 63 6b 28 c int winUnlock(
fcb80 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
fcb90 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b , int locktype){
fcba0 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 77 . int type;. w
fcbb0 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 inFile *pFile =
fcbc0 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (winFile*)id;.
fcbd0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
fcbe0 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 OK;. assert( pF
fcbf0 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ile!=0 );. asse
fcc00 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 rt( locktype<=SH
fcc10 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f ARED_LOCK );. O
fcc20 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 STRACE5("UNLOCK
fcc30 25 64 20 74 6f 20 25 64 20 77 61 73 20 25 64 28 %d to %d was %d(
fcc40 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d)\n", pFile->h
fcc50 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 , locktype,.
fcc60 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
fcc70 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 ktype, pFile->sh
fcc80 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 20 aredLockByte);.
fcc90 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c type = pFile->l
fcca0 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 ocktype;. if( t
fccb0 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype>=EXCLUSIVE_L
fccc0 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 OCK ){. Unloc
fccd0 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
fcce0 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c SHARED_FIRST, 0,
fccf0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 SHARED_SIZE, 0)
fcd00 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 ;. if( lockty
fcd10 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
fcd20 26 26 20 21 67 65 74 52 65 61 64 4c 6f 63 6b 28 && !getReadLock(
fcd30 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 20 20 pFile) ){.
fcd40 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e /* This should n
fcd50 65 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65 ever happen. We
fcd60 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 should always b
fcd70 65 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 e able to.
fcd80 2a 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 65 ** reacquire the
fcd90 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 read lock */.
fcda0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
fcdb0 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 IOERR_UNLOCK;.
fcdc0 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 }. }. if( ty
fcdd0 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe>=RESERVED_LOC
fcde0 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 K ){. UnlockF
fcdf0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 ile(pFile->h, RE
fce00 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 SERVED_BYTE, 0,
fce10 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 1, 0);. }. if(
fce20 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f locktype==NO_LO
fce30 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 CK && type>=SHAR
fce40 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 ED_LOCK ){. u
fce50 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 nlockReadLock(pF
fce60 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ile);. }. if(
fce70 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f type>=PENDING_LO
fce80 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b CK ){. Unlock
fce90 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 File(pFile->h, P
fcea0 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 ENDING_BYTE, 0,
fceb0 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 46 69 1, 0);. }. pFi
fcec0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 le->locktype = (
fced0 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 u8)locktype;. r
fcee0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
fcef0 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 ** Control and q
fcf00 75 65 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e uery of the open
fcf10 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f file handle..*/
fcf20 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 .static int winF
fcf30 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 ileControl(sqlit
fcf40 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
fcf50 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 op, void *pArg)
fcf60 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 {. switch( op )
fcf70 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
fcf80 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 E_FCNTL_LOCKSTAT
fcf90 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 E: {. *(int
fcfa0 2a 29 70 41 72 67 20 3d 20 28 28 77 69 6e 46 69 *)pArg = ((winFi
fcfb0 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 le*)id)->locktyp
fcfc0 65 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e;. return
fcfd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
fcfe0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
fcff0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f LITE_ERROR;.}../
fd000 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
fd010 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 sector size in b
fd020 79 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 ytes of the unde
fd030 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 rlying block dev
fd040 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 ice for.** the s
fd050 70 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 pecified file. T
fd060 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c his is almost al
fd070 77 61 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 ways 512 bytes,
fd080 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 but may be.** la
fd090 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 rger for some de
fd0a0 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c vices..**.** SQL
fd0b0 69 74 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 ite code assumes
fd0c0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 this function c
fd0d0 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 annot fail. It a
fd0e0 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 lso assumes that
fd0f0 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 .** if two files
fd100 20 61 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 are created in
fd110 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 the same file-sy
fd120 73 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 stem directory (
fd130 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 i.e..** a databa
fd140 73 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e se and its journ
fd150 61 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 al file) that th
fd160 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 e sector size wi
fd170 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d ll be the.** sam
fd180 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 e for both..*/.s
fd190 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 65 63 tatic int winSec
fd1a0 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f torSize(sqlite3_
fd1b0 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 55 4e 55 file *id){. UNU
fd1c0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 64 SED_PARAMETER(id
fd1d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
fd1e0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f TE_DEFAULT_SECTO
fd1f0 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a R_SIZE;.}../*.**
fd200 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 Return a vector
fd210 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 of device chara
fd220 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 cteristics..*/.s
fd230 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 76 tatic int winDev
fd240 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
fd250 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
fd260 2a 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 *id){. UNUSED_P
fd270 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0a 20 20 ARAMETER(id);.
fd280 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
fd290 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 ** This vector d
fd2a0 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d efines all the m
fd2b0 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 ethods that can
fd2c0 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a operate on an.**
fd2d0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f sqlite3_file fo
fd2e0 72 20 77 69 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 r win32..*/.stat
fd2f0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
fd300 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 77 69 6e 49 _io_methods winI
fd310 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c oMethod = {. 1,
fd320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd330 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 /* iVers
fd340 69 6f 6e 20 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 ion */. winClos
fd350 65 2c 0a 20 20 77 69 6e 52 65 61 64 2c 0a 20 20 e,. winRead,.
fd360 77 69 6e 57 72 69 74 65 2c 0a 20 20 77 69 6e 54 winWrite,. winT
fd370 72 75 6e 63 61 74 65 2c 0a 20 20 77 69 6e 53 79 runcate,. winSy
fd380 6e 63 2c 0a 20 20 77 69 6e 46 69 6c 65 53 69 7a nc,. winFileSiz
fd390 65 2c 0a 20 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 e,. winLock,.
fd3a0 77 69 6e 55 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e winUnlock,. win
fd3b0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
fd3c0 6b 2c 0a 20 20 77 69 6e 46 69 6c 65 43 6f 6e 74 k,. winFileCont
fd3d0 72 6f 6c 2c 0a 20 20 77 69 6e 53 65 63 74 6f 72 rol,. winSector
fd3e0 53 69 7a 65 2c 0a 20 20 77 69 6e 44 65 76 69 63 Size,. winDevic
fd3f0 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
fd400 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .};../**********
fd410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd450 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 *.** Here ends t
fd460 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 he I/O methods t
fd470 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c hat form the sql
fd480 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
fd490 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 object..**.** Th
fd4a0 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 e next block of
fd4b0 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 code implements
fd4c0 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e the VFS methods.
fd4d0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
fd4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
fd520 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 /*.** Convert a
fd530 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65 20 69 UTF-8 filename i
fd540 6e 74 6f 20 77 68 61 74 65 76 65 72 20 66 6f 72 nto whatever for
fd550 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 m the underlying
fd560 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 .** operating sy
fd570 73 74 65 6d 20 77 61 6e 74 73 20 66 69 6c 65 6e stem wants filen
fd580 61 6d 65 73 20 69 6e 2e 20 20 53 70 61 63 65 20 ames in. Space
fd590 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 to hold the resu
fd5a0 6c 74 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 lt.** is obtaine
fd5b0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e d from malloc an
fd5c0 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 d must be freed
fd5d0 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a by the calling.*
fd5e0 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 * function..*/.s
fd5f0 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e 76 tatic void *conv
fd600 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 ertUtf8Filename(
fd610 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
fd620 65 6e 61 6d 65 29 7b 0a 20 20 76 6f 69 64 20 2a ename){. void *
fd630 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 30 3b 0a zConverted = 0;.
fd640 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
fd650 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d zConverted =
fd660 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a utf8ToUnicode(z
fd670 46 69 6c 65 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73 Filename);./* is
fd680 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c NT() is 1 if SQL
fd690 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c ITE_OS_WINCE==1,
fd6a0 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 so this else is
fd6b0 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e never executed.
fd6c0 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f .*/.#if SQLITE_
fd6d0 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 OS_WINCE==0. }e
fd6e0 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72 lse{. zConver
fd6f0 74 65 64 20 3d 20 75 74 66 38 54 6f 4d 62 63 73 ted = utf8ToMbcs
fd700 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e (zFilename);.#en
fd710 64 69 66 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 6c dif. }. /* cal
fd720 6c 65 72 20 77 69 6c 6c 20 68 61 6e 64 6c 65 20 ler will handle
fd730 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f out of memory */
fd740 0a 20 20 72 65 74 75 72 6e 20 7a 43 6f 6e 76 65 . return zConve
fd750 72 74 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rted;.}../*.** C
fd760 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 reate a temporar
fd770 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a y file name in z
fd780 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 Buf. zBuf must
fd790 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f be big enough to
fd7a0 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73 .** hold at pVfs
fd7b0 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61 ->mxPathname cha
fd7c0 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 racters..*/.stat
fd7d0 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 ic int getTempna
fd7e0 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 me(int nBuf, cha
fd7f0 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 r *zBuf){. stat
fd800 69 63 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d ic char zChars[]
fd810 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 =. "abcdefgh
fd820 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 ijklmnopqrstuvwx
fd830 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 yz". "ABCDEFG
fd840 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 HIJKLMNOPQRSTUVW
fd850 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 XYZ". "012345
fd860 36 37 38 39 22 3b 0a 20 20 73 69 7a 65 5f 74 20 6789";. size_t
fd870 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65 i, j;. char zTe
fd880 6d 70 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b mpPath[MAX_PATH+
fd890 31 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 1];. if( sqlite
fd8a0 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3_temp_directory
fd8b0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
fd8c0 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 snprintf(MAX_PAT
fd8d0 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c H-30, zTempPath,
fd8e0 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 74 "%s", sqlite3_t
fd8f0 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a emp_directory);.
fd900 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4e 54 }else if( isNT
fd910 28 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a () ){. char *
fd920 7a 4d 75 6c 74 69 3b 0a 20 20 20 20 57 43 48 41 zMulti;. WCHA
fd930 52 20 7a 57 69 64 65 50 61 74 68 5b 4d 41 58 5f R zWidePath[MAX_
fd940 50 41 54 48 5d 3b 0a 20 20 20 20 47 65 74 54 65 PATH];. GetTe
fd950 6d 70 50 61 74 68 57 28 4d 41 58 5f 50 41 54 48 mpPathW(MAX_PATH
fd960 2d 33 30 2c 20 7a 57 69 64 65 50 61 74 68 29 3b -30, zWidePath);
fd970 0a 20 20 20 20 7a 4d 75 6c 74 69 20 3d 20 75 6e . zMulti = un
fd980 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 57 69 64 icodeToUtf8(zWid
fd990 65 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 ePath);. if(
fd9a0 7a 4d 75 6c 74 69 20 29 7b 0a 20 20 20 20 20 20 zMulti ){.
fd9b0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
fd9c0 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 (MAX_PATH-30, zT
fd9d0 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a empPath, "%s", z
fd9e0 4d 75 6c 74 69 29 3b 0a 20 20 20 20 20 20 66 72 Multi);. fr
fd9f0 65 65 28 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 ee(zMulti);.
fda00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 }else{. ret
fda10 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
fda20 3b 0a 20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 ;. }./* isNT(
fda30 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
fda40 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
fda50 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
fda60 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
fda70 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 * Since the ASCI
fda80 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 I version of the
fda90 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 se Windows API d
fdaa0 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 o not exist for
fdab0 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 WINCE,.** it's i
fdac0 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 mportant to not
fdad0 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 reference them f
fdae0 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e or WINCE builds.
fdaf0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
fdb00 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c S_WINCE==0. }el
fdb10 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 55 se{. char *zU
fdb20 74 66 38 3b 0a 20 20 20 20 63 68 61 72 20 7a 4d tf8;. char zM
fdb30 62 63 73 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 bcsPath[MAX_PATH
fdb40 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61 ];. GetTempPa
fdb50 74 68 41 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c thA(MAX_PATH-30,
fdb60 20 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20 zMbcsPath);.
fdb70 20 7a 55 74 66 38 20 3d 20 73 71 6c 69 74 65 33 zUtf8 = sqlite3
fdb80 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 _win32_mbcs_to_u
fdb90 74 66 38 28 7a 4d 62 63 73 50 61 74 68 29 3b 0a tf8(zMbcsPath);.
fdba0 20 20 20 20 69 66 28 20 7a 55 74 66 38 20 29 7b if( zUtf8 ){
fdbb0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
fdbc0 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 nprintf(MAX_PATH
fdbd0 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 -30, zTempPath,
fdbe0 22 25 73 22 2c 20 7a 55 74 66 38 29 3b 0a 20 20 "%s", zUtf8);.
fdbf0 20 20 20 20 66 72 65 65 28 7a 55 74 66 38 29 3b free(zUtf8);
fdc00 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
fdc10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fdc20 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e NOMEM;. }.#en
fdc30 64 69 66 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d dif. }. for(i=
fdc40 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
fdc50 7a 54 65 6d 70 50 61 74 68 29 3b 20 69 3e 30 20 zTempPath); i>0
fdc60 26 26 20 7a 54 65 6d 70 50 61 74 68 5b 69 2d 31 && zTempPath[i-1
fdc70 5d 3d 3d 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a ]=='\\'; i--){}.
fdc80 20 20 7a 54 65 6d 70 50 61 74 68 5b 69 5d 20 3d zTempPath[i] =
fdc90 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 0;. sqlite3_sn
fdca0 70 72 69 6e 74 66 28 6e 42 75 66 2d 33 30 2c 20 printf(nBuf-30,
fdcb0 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 zBuf,.
fdcc0 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 53 "%s\\"S
fdcd0 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f QLITE_TEMP_FILE_
fdce0 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 PREFIX, zTempPat
fdcf0 68 29 3b 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65 h);. j = sqlite
fdd00 33 53 74 72 6c 65 6e 33 30 28 7a 42 75 66 29 3b 3Strlen30(zBuf);
fdd10 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f . sqlite3_rando
fdd20 6d 6e 65 73 73 28 32 30 2c 20 26 7a 42 75 66 5b mness(20, &zBuf[
fdd30 6a 5d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 j]);. for(i=0;
fdd40 69 3c 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b i<20; i++, j++){
fdd50 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 . zBuf[j] = (
fdd60 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 char)zChars[ ((u
fdd70 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 nsigned char)zBu
fdd80 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 f[j])%(sizeof(zC
fdd90 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a hars)-1) ];. }.
fdda0 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 zBuf[j] = 0;.
fddb0 20 4f 53 54 52 41 43 45 32 28 22 54 45 4d 50 20 OSTRACE2("TEMP
fddc0 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c FILENAME: %s\n",
fddd0 20 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 6e zBuf);. return
fdde0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a SQLITE_OK; .}..
fddf0 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e /*.** The return
fde00 20 76 61 6c 75 65 20 6f 66 20 67 65 74 4c 61 73 value of getLas
fde10 74 45 72 72 6f 72 4d 73 67 0a 2a 2a 20 69 73 20 tErrorMsg.** is
fde20 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f zero if the erro
fde30 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 20 69 r message fits i
fde40 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72 n the buffer, or
fde50 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 non-zero.** oth
fde60 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20 6d erwise (if the m
fde70 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e 63 essage was trunc
fde80 61 74 65 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 ated)..*/.static
fde90 20 69 6e 74 20 67 65 74 4c 61 73 74 45 72 72 6f int getLastErro
fdea0 72 4d 73 67 28 69 6e 74 20 6e 42 75 66 2c 20 63 rMsg(int nBuf, c
fdeb0 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 44 57 har *zBuf){. DW
fdec0 4f 52 44 20 65 72 72 6f 72 20 3d 20 47 65 74 4c ORD error = GetL
fded0 61 73 74 45 72 72 6f 72 28 29 3b 0a 0a 23 69 66 astError();..#if
fdee0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
fdef0 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
fdf00 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 ntf(nBuf, zBuf,
fdf10 22 4f 73 45 72 72 6f 72 20 30 78 25 78 20 28 25 "OsError 0x%x (%
fdf20 75 29 22 2c 20 65 72 72 6f 72 2c 20 65 72 72 6f u)", error, erro
fdf30 72 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 46 r);.#else. /* F
fdf40 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 72 65 74 ormatMessage ret
fdf50 75 72 6e 73 20 30 20 6f 6e 20 66 61 69 6c 75 72 urns 0 on failur
fdf60 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74 e. Otherwise it
fdf70 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 74 68 . ** returns th
fdf80 65 20 6e 75 6d 62 65 72 20 6f 66 20 54 43 48 41 e number of TCHA
fdf90 52 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 Rs written to th
fdfa0 65 20 6f 75 74 70 75 74 0a 20 20 2a 2a 20 62 75 e output. ** bu
fdfb0 66 66 65 72 2c 20 65 78 63 6c 75 64 69 6e 67 20 ffer, excluding
fdfc0 74 68 65 20 74 65 72 6d 69 6e 61 74 69 6e 67 20 the terminating
fdfd0 6e 75 6c 6c 20 63 68 61 72 2e 0a 20 20 2a 2f 0a null char.. */.
fdfe0 20 20 69 66 20 28 21 46 6f 72 6d 61 74 4d 65 73 if (!FormatMes
fdff0 73 61 67 65 41 28 46 4f 52 4d 41 54 5f 4d 45 53 sageA(FORMAT_MES
fe000 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54 45 4d SAGE_FROM_SYSTEM
fe010 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
fe020 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 NULL,.
fe030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe040 20 20 20 20 65 72 72 6f 72 2c 0a 20 20 20 20 20 error,.
fe050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe060 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 0,.
fe070 20 20 20 20 20 20 20 20 20 20 7a 42 75 66 2c 0a zBuf,.
fe080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe090 20 20 20 20 20 20 6e 42 75 66 2d 31 2c 0a 20 20 nBuf-1,.
fe0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe0b0 20 20 20 20 30 29 29 0a 20 20 7b 0a 20 20 20 20 0)). {.
fe0c0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
fe0d0 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f 73 (nBuf, zBuf, "Os
fe0e0 45 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 22 Error 0x%x (%u)"
fe0f0 2c 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 3b , error, error);
fe100 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 . }.#endif.. r
fe110 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn 0;.}.../*.
fe120 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a ** Open a file..
fe130 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
fe140 6e 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 nOpen(. sqlite3
fe150 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
fe160 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a /* Not used *
fe170 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
fe180 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a zName, /*
fe190 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
fe1a0 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 e (UTF-8) */. s
fe1b0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fe1c0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
fe1d0 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c e the SQLite fil
fe1e0 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f e handle here */
fe1f0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
fe200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
fe210 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 Open mode flags
fe220 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c */. int *pOutFl
fe230 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f ags /
fe240 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20 * Status return
fe250 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 flags */.){. HA
fe260 4e 44 4c 45 20 68 3b 0a 20 20 44 57 4f 52 44 20 NDLE h;. DWORD
fe270 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b dwDesiredAccess;
fe280 0a 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65 . DWORD dwShare
fe290 4d 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77 Mode;. DWORD dw
fe2a0 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 CreationDisposit
fe2b0 69 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46 ion;. DWORD dwF
fe2c0 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 lagsAndAttribute
fe2d0 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 s = 0;.#if SQLIT
fe2e0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74 E_OS_WINCE. int
fe2f0 20 69 73 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e isTemp = 0;.#en
fe300 64 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 dif. winFile *p
fe310 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
fe320 29 69 64 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f )id;. void *zCo
fe330 6e 76 65 72 74 65 64 3b 20 20 20 20 20 20 20 20 nverted;
fe340 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
fe350 6e 61 6d 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64 name in OS encod
fe360 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ing */. const c
fe370 68 61 72 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d har *zUtf8Name =
fe380 20 7a 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 46 69 zName; /* Fi
fe390 6c 65 6e 61 6d 65 20 69 6e 20 55 54 46 2d 38 20 lename in UTF-8
fe3a0 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 68 encoding */. ch
fe3b0 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f ar zTmpname[MAX_
fe3c0 50 41 54 48 2b 31 5d 3b 20 20 20 20 20 20 20 20 PATH+1];
fe3d0 2f 2a 20 42 75 66 66 65 72 20 75 73 65 64 20 74 /* Buffer used t
fe3e0 6f 20 63 72 65 61 74 65 20 74 65 6d 70 20 66 69 o create temp fi
fe3f0 6c 65 6e 61 6d 65 20 2a 2f 0a 0a 20 20 55 4e 55 lename */.. UNU
fe400 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 SED_PARAMETER(pV
fe410 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 fs);.. /* If th
fe420 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
fe430 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 t to this functi
fe440 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 on is NULL, gene
fe450 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d rate a . ** tem
fe460 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 porary file name
fe470 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 to use . */.
fe480 69 66 28 20 21 7a 55 74 66 38 4e 61 6d 65 20 29 if( !zUtf8Name )
fe490 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 {. int rc = g
fe4a0 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 etTempname(MAX_P
fe4b0 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 ATH+1, zTmpname)
fe4c0 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
fe4d0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
fe4e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
fe4f0 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 20 }. zUtf8Name
fe500 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a = zTmpname;. }.
fe510 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 . /* Convert th
fe520 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74 68 e filename to th
fe530 65 20 73 79 73 74 65 6d 20 65 6e 63 6f 64 69 6e e system encodin
fe540 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 74 g. */. zConvert
fe550 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 ed = convertUtf8
fe560 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66 38 4e 61 Filename(zUtf8Na
fe570 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 me);. if( zConv
fe580 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 erted==0 ){.
fe590 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
fe5a0 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 MEM;. }.. if(
fe5b0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
fe5c0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b PEN_READWRITE ){
fe5d0 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 . dwDesiredAc
fe5e0 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 cess = GENERIC_R
fe5f0 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 EAD | GENERIC_WR
fe600 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ITE;. }else{.
fe610 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73 dwDesiredAcces
fe620 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 s = GENERIC_READ
fe630 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 ;. }. if( flag
fe640 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
fe650 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 64 77 CREATE ){. dw
fe660 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 CreationDisposit
fe670 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c 57 41 59 ion = OPEN_ALWAY
fe680 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 S;. }else{.
fe690 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 dwCreationDispos
fe6a0 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 49 ition = OPEN_EXI
fe6b0 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20 69 66 28 STING;. }. if(
fe6c0 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f flags & SQLITE_
fe6d0 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a OPEN_MAIN_DB ){.
fe6e0 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 20 dwShareMode
fe6f0 3d 20 46 49 4c 45 5f 53 48 41 52 45 5f 52 45 41 = FILE_SHARE_REA
fe700 44 20 7c 20 46 49 4c 45 5f 53 48 41 52 45 5f 57 D | FILE_SHARE_W
fe710 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 RITE;. }else{.
fe720 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 20 3d dwShareMode =
fe730 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 0;. }. if( fl
fe740 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
fe750 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 N_DELETEONCLOSE
fe760 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 ){.#if SQLITE_OS
fe770 5f 57 49 4e 43 45 0a 20 20 20 20 64 77 46 6c 61 _WINCE. dwFla
fe780 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 gsAndAttributes
fe790 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 = FILE_ATTRIBUTE
fe7a0 5f 48 49 44 44 45 4e 3b 0a 20 20 20 20 69 73 54 _HIDDEN;. isT
fe7b0 65 6d 70 20 3d 20 31 3b 0a 23 65 6c 73 65 0a 20 emp = 1;.#else.
fe7c0 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 dwFlagsAndAtt
fe7d0 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 ributes = FILE_A
fe7e0 54 54 52 49 42 55 54 45 5f 54 45 4d 50 4f 52 41 TTRIBUTE_TEMPORA
fe7f0 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 RY.
fe800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe810 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49 42 55 | FILE_ATTRIBU
fe820 54 45 5f 48 49 44 44 45 4e 0a 20 20 20 20 20 20 TE_HIDDEN.
fe830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe840 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f | FILE_
fe850 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f 43 FLAG_DELETE_ON_C
fe860 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 0a 20 20 7d LOSE;.#endif. }
fe870 65 6c 73 65 7b 0a 20 20 20 20 64 77 46 6c 61 67 else{. dwFlag
fe880 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d sAndAttributes =
fe890 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f FILE_ATTRIBUTE_
fe8a0 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f 2a NORMAL;. }. /*
fe8b0 20 52 65 70 6f 72 74 73 20 66 72 6f 6d 20 74 68 Reports from th
fe8c0 65 20 69 6e 74 65 72 6e 65 74 20 61 72 65 20 74 e internet are t
fe8d0 68 61 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 hat performance
fe8e0 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 62 is always. ** b
fe8f0 65 74 74 65 72 20 69 66 20 46 49 4c 45 5f 46 4c etter if FILE_FL
fe900 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 53 AG_RANDOM_ACCESS
fe910 20 69 73 20 75 73 65 64 2e 20 20 54 69 63 6b 65 is used. Ticke
fe920 74 20 23 32 36 39 39 2e 20 2a 2f 0a 23 69 66 20 t #2699. */.#if
fe930 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
fe940 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 dwFlagsAndAttr
fe950 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f 46 ibutes |= FILE_F
fe960 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 LAG_RANDOM_ACCES
fe970 53 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 S;.#endif. if(
fe980 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 20 isNT() ){. h
fe990 3d 20 43 72 65 61 74 65 46 69 6c 65 57 28 28 57 = CreateFileW((W
fe9a0 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 CHAR*)zConverted
fe9b0 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 72 ,. dwDesir
fe9c0 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20 edAccess,.
fe9d0 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 dwShareMode,.
fe9e0 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 NULL,.
fe9f0 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 dwCreationDisp
fea00 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 osition,.
fea10 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 dwFlagsAndAttrib
fea20 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c utes,. NUL
fea30 4c 0a 20 20 20 20 29 3b 0a 2f 2a 20 69 73 4e 54 L. );./* isNT
fea40 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 () is 1 if SQLIT
fea50 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 E_OS_WINCE==1, s
fea60 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e o this else is n
fea70 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a ever executed. .
fea80 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 ** Since the ASC
fea90 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 II version of th
feaa0 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 ese Windows API
feab0 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 do not exist for
feac0 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 WINCE,.** it's
fead0 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 important to not
feae0 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 reference them
feaf0 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 for WINCE builds
feb00 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
feb10 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 OS_WINCE==0. }e
feb20 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 lse{. h = Cre
feb30 61 74 65 46 69 6c 65 41 28 28 63 68 61 72 2a 29 ateFileA((char*)
feb40 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 zConverted,.
feb50 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 dwDesiredAcce
feb60 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61 ss,. dwSha
feb70 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e reMode,. N
feb80 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72 ULL,. dwCr
feb90 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f eationDispositio
feba0 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67 n,. dwFlag
febb0 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a sAndAttributes,.
febc0 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 NULL.
febd0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 );.#endif. }.
febe0 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49 44 5f 48 if( h==INVALID_H
febf0 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 7b 0a 20 ANDLE_VALUE ){.
fec00 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 free(zConvert
fec10 65 64 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 ed);. if( fla
fec20 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
fec30 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 _READWRITE ){.
fec40 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e 4f 70 return winOp
fec50 65 6e 28 30 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c en(0, zName, id,
fec60 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 . (
fec70 28 66 6c 61 67 73 7c 53 51 4c 49 54 45 5f 4f 50 (flags|SQLITE_OP
fec80 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 26 7e 53 51 EN_READONLY)&~SQ
fec90 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
feca0 49 54 45 29 2c 20 70 4f 75 74 46 6c 61 67 73 29 ITE), pOutFlags)
fecb0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
fecc0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fecd0 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d _CANTOPEN;. }
fece0 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 46 . }. if( pOutF
fecf0 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 66 28 20 lags ){. if(
fed00 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
fed10 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b PEN_READWRITE ){
fed20 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 . *pOutFlag
fed30 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s = SQLITE_OPEN_
fed40 52 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20 7d READWRITE;. }
fed50 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 4f 75 else{. *pOu
fed60 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f tFlags = SQLITE_
fed70 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 OPEN_READONLY;.
fed80 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 }. }. memse
fed90 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 t(pFile, 0, size
feda0 6f 66 28 2a 70 46 69 6c 65 29 29 3b 0a 20 20 70 of(*pFile));. p
fedb0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 File->pMethod =
fedc0 26 77 69 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 &winIoMethod;.
fedd0 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 23 69 pFile->h = h;.#i
fede0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
fedf0 45 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20 26 E. if( (flags &
fee00 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 (SQLITE_OPEN_RE
fee10 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f ADWRITE|SQLITE_O
fee20 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 3d 3d PEN_MAIN_DB)) ==
fee30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
fee40 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 (SQLITE_OPEN_REA
fee50 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 DWRITE|SQLITE_OP
fee60 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 20 20 EN_MAIN_DB).
fee70 20 20 20 26 26 20 21 77 69 6e 63 65 43 72 65 61 && !winceCrea
fee80 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 teLock(zName, pF
fee90 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20 43 6c ile). ){. Cl
feea0 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20 oseHandle(h);.
feeb0 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 free(zConverte
feec0 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 d);. return S
feed0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
feee0 20 20 7d 0a 20 20 69 66 28 20 69 73 54 65 6d 70 }. if( isTemp
feef0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 7a ){. pFile->z
fef00 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 DeleteOnClose =
fef10 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 7d 65 zConverted;. }e
fef20 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 lse.#endif. {.
fef30 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 free(zConvert
fef40 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 ed);. }. OpenC
fef50 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 72 65 ounter(+1);. re
fef60 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
fef70 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
fef80 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a the named file..
fef90 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
fefa0 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 windows does not
fefb0 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20 74 6f allow a file to
fefc0 20 62 65 20 64 65 6c 65 74 65 64 20 69 66 20 73 be deleted if s
fefd0 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f ome other.** pro
fefe0 63 65 73 73 20 68 61 73 20 69 74 20 6f 70 65 6e cess has it open
feff0 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 61 20 76 . Sometimes a v
ff000 69 72 75 73 20 73 63 61 6e 6e 65 72 20 6f 72 20 irus scanner or
ff010 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 72 61 6d indexing program
ff020 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 .** will open a
ff030 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68 6f journal file sho
ff040 72 74 6c 79 20 61 66 74 65 72 20 69 74 20 69 73 rtly after it is
ff050 20 63 72 65 61 74 65 64 20 69 6e 20 6f 72 64 65 created in orde
ff060 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 65 r to do.** whate
ff070 76 65 72 20 69 74 20 64 6f 65 73 2e 20 20 57 68 ver it does. Wh
ff080 69 6c 65 20 74 68 69 73 20 6f 74 68 65 72 20 70 ile this other p
ff090 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e rocess is holdin
ff0a0 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 g the.** file op
ff0b0 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65 20 75 en, we will be u
ff0c0 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 nable to delete
ff0d0 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f it. To work aro
ff0e0 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62 und this.** prob
ff0f0 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 20 31 30 lem, we delay 10
ff100 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 0 milliseconds a
ff110 6e 64 20 74 72 79 20 74 6f 20 64 65 6c 65 74 65 nd try to delete
ff120 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 again. Up.** t
ff130 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 o MX_DELETION_AT
ff140 54 45 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e 20 TEMPTs deletion
ff150 61 74 74 65 6d 70 74 73 20 61 72 65 20 72 75 6e attempts are run
ff160 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a before giving.*
ff170 2a 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 * up and returni
ff180 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a ng an error..*/.
ff190 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c 45 54 #define MX_DELET
ff1a0 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35 0a 73 ION_ATTEMPTS 5.s
ff1b0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c tatic int winDel
ff1c0 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 ete(. sqlite3_v
ff1d0 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 fs *pVfs,
ff1e0 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f /* Not used o
ff1f0 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e n win32 */. con
ff200 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
ff210 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 me, /* Name
ff220 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 of file to dele
ff230 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 te */. int sync
ff240 44 69 72 20 20 20 20 20 20 20 20 20 20 20 20 20 Dir
ff250 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 /* Not used
ff260 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 on win32 */.){.
ff270 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 int cnt = 0;.
ff280 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 DWORD rc;. DWOR
ff290 44 20 65 72 72 6f 72 20 3d 20 30 3b 0a 20 20 76 D error = 0;. v
ff2a0 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 oid *zConverted
ff2b0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c = convertUtf8Fil
ff2c0 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 ename(zFilename)
ff2d0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
ff2e0 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 55 4e ETER(pVfs);. UN
ff2f0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73 USED_PARAMETER(s
ff300 79 6e 63 44 69 72 29 3b 0a 20 20 69 66 28 20 7a yncDir);. if( z
ff310 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a Converted==0 ){.
ff320 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ff330 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 E_NOMEM;. }. S
ff340 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 imulateIOError(r
ff350 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
ff360 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 RR_DELETE);. if
ff370 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 ( isNT() ){.
ff380 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 65 do{. Delete
ff390 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 65 64 FileW(zConverted
ff3a0 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 20 );. }while(
ff3b0 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 74 46 ( ((rc = GetF
ff3c0 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 7a ileAttributesW(z
ff3d0 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20 49 Converted)) != I
ff3e0 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 NVALID_FILE_ATTR
ff3f0 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 20 20 IBUTES).
ff400 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72 6f || ((erro
ff410 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 r = GetLastError
ff420 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 ()) == ERROR_ACC
ff430 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 ESS_DENIED)).
ff440 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63 6e && (++cn
ff450 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f t < MX_DELETION_
ff460 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20 20 ATTEMPTS).
ff470 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 28 31 && (Sleep(1
ff480 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 73 00), 1) );./* is
ff490 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c NT() is 1 if SQL
ff4a0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c ITE_OS_WINCE==1,
ff4b0 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 so this else is
ff4c0 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e never executed.
ff4d0 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 .** Since the A
ff4e0 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 SCII version of
ff4f0 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 these Windows AP
ff500 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 I do not exist f
ff510 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 or WINCE,.** it'
ff520 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e s important to n
ff530 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 ot reference the
ff540 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c m for WINCE buil
ff550 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ds..*/.#if SQLIT
ff560 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 E_OS_WINCE==0.
ff570 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b 0a 20 }else{. do{.
ff580 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 41 DeleteFileA
ff590 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 (zConverted);.
ff5a0 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20 20 }while( (
ff5b0 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41 74 ((rc = GetFileAt
ff5c0 74 72 69 62 75 74 65 73 41 28 7a 43 6f 6e 76 65 tributesA(zConve
ff5d0 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 rted)) != INVALI
ff5e0 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 D_FILE_ATTRIBUTE
ff5f0 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 S).
ff600 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 || ((error = G
ff610 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 3d etLastError()) =
ff620 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 = ERROR_ACCESS_D
ff630 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20 20 ENIED)).
ff640 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d && (++cnt < M
ff650 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d X_DELETION_ATTEM
ff660 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 PTS).
ff670 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 && (Sleep(100),
ff680 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 1) );.#endif. }
ff690 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 . free(zConvert
ff6a0 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 ed);. OSTRACE2(
ff6b0 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e "DELETE \"%s\"\n
ff6c0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 ", zFilename);.
ff6d0 20 72 65 74 75 72 6e 20 28 20 20 20 28 72 63 20 return ( (rc
ff6e0 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f == INVALID_FILE_
ff6f0 41 54 54 52 49 42 55 54 45 53 29 20 0a 20 20 20 ATTRIBUTES) .
ff700 20 20 20 20 20 20 20 26 26 20 28 65 72 72 6f 72 && (error
ff710 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45 5f 4e == ERROR_FILE_N
ff720 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53 51 4c OT_FOUND)) ? SQL
ff730 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f ITE_OK : SQLITE_
ff740 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a IOERR_DELETE;.}.
ff750 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 ./*.** Check the
ff760 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 73 existance and s
ff770 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 2e tatus of a file.
ff780 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
ff790 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 inAccess(. sqli
ff7a0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 te3_vfs *pVfs,
ff7b0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 /* Not us
ff7c0 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 ed on win32 */.
ff7d0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
ff7e0 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e lename, /* N
ff7f0 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 ame of file to c
ff800 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c heck */. int fl
ff810 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ags,
ff820 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 /* Type of
ff830 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 test to make on
ff840 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 this file */. i
ff850 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 20 nt *pResOut
ff860 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
ff870 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20 : Result */.){.
ff880 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20 69 DWORD attr;. i
ff890 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 76 6f 69 nt rc = 0;. voi
ff8a0 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 d *zConverted =
ff8b0 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e convertUtf8Filen
ff8c0 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a ame(zFilename);.
ff8d0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
ff8e0 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 ER(pVfs);. if(
ff8f0 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b zConverted==0 ){
ff900 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
ff910 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
ff920 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 if( isNT() ){.
ff930 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65 attr = GetFile
ff940 41 74 74 72 69 62 75 74 65 73 57 28 28 57 43 48 AttributesW((WCH
ff950 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b AR*)zConverted);
ff960 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
ff970 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ff980 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
ff990 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
ff9a0 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 cuted. .** Since
ff9b0 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 the ASCII versi
ff9c0 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 on of these Wind
ff9d0 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 ows API do not e
ff9e0 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a xist for WINCE,.
ff9f0 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e ** it's importan
ffa00 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e t to not referen
ffa10 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 ce them for WINC
ffa20 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 E builds..*/.#if
ffa30 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ffa40 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ==0. }else{.
ffa50 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41 attr = GetFileA
ffa60 74 74 72 69 62 75 74 65 73 41 28 28 63 68 61 72 ttributesA((char
ffa70 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 *)zConverted);.#
ffa80 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 endif. }. free
ffa90 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 (zConverted);.
ffaa0 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b switch( flags ){
ffab0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
ffac0 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 _ACCESS_READ:.
ffad0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 case SQLITE_AC
ffae0 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 CESS_EXISTS:.
ffaf0 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d 49 4e rc = attr!=IN
ffb00 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 VALID_FILE_ATTRI
ffb10 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62 72 65 BUTES;. bre
ffb20 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ak;. case SQL
ffb30 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 ITE_ACCESS_READW
ffb40 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d RITE:. rc =
ffb50 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f 41 54 (attr & FILE_AT
ffb60 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e 4c 59 TRIBUTE_READONLY
ffb70 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72 65 61 )==0;. brea
ffb80 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a k;. default:.
ffb90 20 20 20 20 20 20 61 73 73 65 72 74 28 21 22 49 assert(!"I
ffba0 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 nvalid flags arg
ffbb0 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a ument");. }. *
ffbc0 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 pResOut = rc;.
ffbd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
ffbe0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e ;.}.../*.** Turn
ffbf0 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 a relative path
ffc00 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c name into a full
ffc10 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 pathname. Writ
ffc20 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 e the full.** pa
ffc30 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 thname into zOut
ffc40 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c []. zOut[] will
ffc50 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 56 66 be at least pVf
ffc60 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a s->mxPathname.**
ffc70 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a bytes in size..
ffc80 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
ffc90 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 nFullPathname(.
ffca0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
ffcb0 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f fs, /
ffcc0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 * Pointer to vfs
ffcd0 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e object */. con
ffce0 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 st char *zRelati
ffcf0 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f ve, /* Po
ffd00 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 ssibly relative
ffd10 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 input path */.
ffd20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 int nFull,
ffd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ffd40 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 Size of output
ffd50 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 buffer in bytes
ffd60 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c */. char *zFull
ffd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffd80 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 /* Output buf
ffd90 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a 23 69 66 fer */.){. .#if
ffda0 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 defined(__CYGWI
ffdb0 4e 5f 5f 29 0a 20 20 55 4e 55 53 45 44 5f 50 41 N__). UNUSED_PA
ffdc0 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a RAMETER(nFull);.
ffdd0 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f cygwin_conv_to
ffde0 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68 _full_win32_path
ffdf0 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c (zRelative, zFul
ffe00 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c l);. return SQL
ffe10 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a ITE_OK;.#endif..
ffe20 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
ffe30 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 NCE. UNUSED_PAR
ffe40 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 AMETER(nFull);.
ffe50 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 6e 6f /* WinCE has no
ffe60 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 72 65 concept of a re
ffe70 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 2c lative pathname,
ffe80 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64 or so I am told
ffe90 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 . */. sqlite3_s
ffea0 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 nprintf(pVfs->mx
ffeb0 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c Pathname, zFull,
ffec0 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 69 76 65 "%s", zRelative
ffed0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
ffee0 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 TE_OK;.#endif..#
ffef0 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 if !SQLITE_OS_WI
fff00 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65 64 28 NCE && !defined(
fff10 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e __CYGWIN__). in
fff20 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 64 20 t nByte;. void
fff30 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63 *zConverted;. c
fff40 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 55 4e 55 har *zOut;. UNU
fff50 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 SED_PARAMETER(nF
fff60 75 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 ull);. zConvert
fff70 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 ed = convertUtf8
fff80 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61 74 69 Filename(zRelati
fff90 76 65 29 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 ve);. if( isNT(
fffa0 29 20 29 7b 0a 20 20 20 20 57 43 48 41 52 20 2a ) ){. WCHAR *
fffb0 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 zTemp;. nByte
fffc0 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 = GetFullPathNa
fffd0 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e meW((WCHAR*)zCon
fffe0 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 verted, 0, 0, 0)
ffff0 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 + 3;. zTemp
10000 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a = malloc( nByte*
10001 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 sizeof(zTemp[0])
10002 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d );. if( zTem
10003 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 72 p==0 ){. fr
10004 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
10005 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
10006 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
10007 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74 68 . GetFullPath
10008 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 NameW((WCHAR*)zC
10009 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c onverted, nByte,
1000a 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 zTemp, 0);.
1000b 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 free(zConverted)
1000c 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 ;. zOut = uni
1000d 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 codeToUtf8(zTemp
1000e 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65 6d );. free(zTem
1000f 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 p);./* isNT() is
10010 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 1 if SQLITE_OS_
10011 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 WINCE==1, so thi
10012 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 s else is never
10013 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 executed. .** Si
10014 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 nce the ASCII ve
10015 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 rsion of these W
10016 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f indows API do no
10017 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 t exist for WINC
10018 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 E,.** it's impor
10019 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 tant to not refe
1001a 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 rence them for W
1001b 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a INCE builds..*/.
1001c 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
1001d 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a NCE==0. }else{.
1001e 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b char *zTemp;
1001f 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74 . nByte = Get
10020 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 FullPathNameA((c
10021 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c har*)zConverted,
10022 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 0, 0, 0) + 3;.
10023 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f zTemp = mallo
10024 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 c( nByte*sizeof(
10025 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 zTemp[0]) );.
10026 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b if( zTemp==0 ){
10027 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e . free(zCon
10028 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72 verted);. r
10029 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1002a 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 EM;. }. Ge
1002b 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 tFullPathNameA((
1002c 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 char*)zConverted
1002d 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 , nByte, zTemp,
1002e 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 0);. free(zCo
1002f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f nverted);. zO
10030 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e ut = sqlite3_win
10031 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 32_mbcs_to_utf8(
10032 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 zTemp);. free
10033 28 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69 66 0a (zTemp);.#endif.
10034 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 }. if( zOut )
10035 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e {. sqlite3_sn
10036 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 printf(pVfs->mxP
10037 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 athname, zFull,
10038 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 "%s", zOut);.
10039 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 free(zOut);.
1003a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1003b 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
1003c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1003d 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a MEM;. }.#endif.
1003e 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1003f 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
10040 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 NSION./*.** Inte
10041 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 rfaces for openi
10042 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 ng a shared libr
10043 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 ary, finding ent
10044 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 ry points.** wit
10045 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c hin the shared l
10046 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 ibrary, and clos
10047 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c ing the shared l
10048 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a ibrary..*/./*.**
10049 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 Interfaces for
1004a 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 opening a shared
1004b 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e library, findin
1004c 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a g entry points.*
1004d 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 * within the sha
1004e 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 red library, and
1004f 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 closing the sha
10050 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a red library..*/.
10051 73 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69 6e static void *win
10052 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 DlOpen(sqlite3_v
10053 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 fs *pVfs, const
10054 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 char *zFilename)
10055 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 {. HANDLE h;.
10056 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 void *zConverted
10057 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 = convertUtf8Fi
10058 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 lename(zFilename
10059 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
1005a 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69 METER(pVfs);. i
1005b 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 f( zConverted==0
1005c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
1005d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 ;. }. if( isNT
1005e 28 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 4c 6f () ){. h = Lo
1005f 61 64 4c 69 62 72 61 72 79 57 28 28 57 43 48 41 adLibraryW((WCHA
10060 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a R*)zConverted);.
10061 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 /* isNT() is 1 i
10062 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
10063 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c E==1, so this el
10064 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 se is never exec
10065 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 uted. .** Since
10066 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f the ASCII versio
10067 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f n of these Windo
10068 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 ws API do not ex
10069 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a ist for WINCE,.*
1006a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 * it's important
1006b 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 to not referenc
1006c 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 e them for WINCE
1006d 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 builds..*/.#if
1006e 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
1006f 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
10070 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79 41 h = LoadLibraryA
10071 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 ((char*)zConvert
10072 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a ed);.#endif. }.
10073 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 free(zConverte
10074 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f d);. return (vo
10075 69 64 2a 29 68 3b 0a 7d 0a 73 74 61 74 69 63 20 id*)h;.}.static
10076 76 6f 69 64 20 77 69 6e 44 6c 45 72 72 6f 72 28 void winDlError(
10077 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
10078 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 s, int nBuf, cha
10079 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 55 r *zBufOut){. U
1007a 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
1007b 70 56 66 73 29 3b 0a 20 20 67 65 74 4c 61 73 74 pVfs);. getLast
1007c 45 72 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a ErrorMsg(nBuf, z
1007d 42 75 66 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64 20 BufOut);.}.void
1007e 28 2a 77 69 6e 44 6c 53 79 6d 28 73 71 6c 69 74 (*winDlSym(sqlit
1007f 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f e3_vfs *pVfs, vo
10080 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e id *pHandle, con
10081 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c st char *zSymbol
10082 29 29 28 76 6f 69 64 29 7b 0a 20 20 55 4e 55 53 ))(void){. UNUS
10083 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
10084 73 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f s);.#if SQLITE_O
10085 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 54 68 65 S_WINCE. /* The
10086 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 41 GetProcAddressA
10087 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e () routine is on
10088 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 ly available on
10089 77 69 6e 63 65 2e 20 2a 2f 0a 20 20 72 65 74 75 wince. */. retu
1008a 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 rn (void(*)(void
1008b 29 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 ))GetProcAddress
1008c 41 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c A((HANDLE)pHandl
1008d 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c e, zSymbol);.#el
1008e 73 65 0a 20 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 se. /* All othe
1008f 72 20 77 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f r windows platfo
10090 72 6d 73 20 65 78 70 65 63 74 20 47 65 74 50 72 rms expect GetPr
10091 6f 63 41 64 64 72 65 73 73 28 29 20 74 6f 20 74 ocAddress() to t
10092 61 6b 65 0a 20 20 2a 2a 20 61 6e 20 41 6e 73 69 ake. ** an Ansi
10093 20 73 74 72 69 6e 67 20 72 65 67 61 72 64 6c 65 string regardle
10094 73 73 20 6f 66 20 74 68 65 20 5f 55 4e 49 43 4f ss of the _UNICO
10095 44 45 20 73 65 74 74 69 6e 67 20 2a 2f 0a 20 20 DE setting */.
10096 72 65 74 75 72 6e 20 28 76 6f 69 64 28 2a 29 28 return (void(*)(
10097 76 6f 69 64 29 29 47 65 74 50 72 6f 63 41 64 64 void))GetProcAdd
10098 72 65 73 73 28 28 48 41 4e 44 4c 45 29 70 48 61 ress((HANDLE)pHa
10099 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a ndle, zSymbol);.
1009a 23 65 6e 64 69 66 0a 7d 0a 76 6f 69 64 20 77 69 #endif.}.void wi
1009b 6e 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 nDlClose(sqlite3
1009c 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 _vfs *pVfs, void
1009d 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e *pHandle){. UN
1009e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
1009f 56 66 73 29 3b 0a 20 20 46 72 65 65 4c 69 62 72 Vfs);. FreeLibr
100a0 61 72 79 28 28 48 41 4e 44 4c 45 29 70 48 61 6e ary((HANDLE)pHan
100a1 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a dle);.}.#else /*
100a2 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f if SQLITE_OMIT_
100a3 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 LOAD_EXTENSION i
100a4 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 s defined: */.
100a5 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 4f 70 65 #define winDlOpe
100a6 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 n 0. #define w
100a7 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 inDlError 0. #d
100a8 65 66 69 6e 65 20 77 69 6e 44 6c 53 79 6d 20 20 efine winDlSym
100a9 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 0. #define win
100aa 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 DlClose 0.#endif
100ab 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 .../*.** Write u
100ac 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20 p to nBuf bytes
100ad 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e of randomness in
100ae 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 to zBuf..*/.stat
100af 69 63 20 69 6e 74 20 77 69 6e 52 61 6e 64 6f 6d ic int winRandom
100b0 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 ness(sqlite3_vfs
100b1 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 *pVfs, int nBuf
100b2 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 , char *zBuf){.
100b3 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 55 4e int n = 0;. UN
100b4 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
100b5 56 66 73 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 Vfs);.#if define
100b6 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 d(SQLITE_TEST).
100b7 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d n = nBuf;. mem
100b8 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 set(zBuf, 0, nBu
100b9 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 f);.#else. if(
100ba 73 69 7a 65 6f 66 28 53 59 53 54 45 4d 54 49 4d sizeof(SYSTEMTIM
100bb 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 E)<=nBuf-n ){.
100bc 20 20 53 59 53 54 45 4d 54 49 4d 45 20 78 3b 0a SYSTEMTIME x;.
100bd 20 20 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d GetSystemTim
100be 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70 e(&x);. memcp
100bf 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 y(&zBuf[n], &x,
100c0 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 sizeof(x));.
100c1 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a n += sizeof(x);.
100c2 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 }. if( sizeof
100c3 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 (DWORD)<=nBuf-n
100c4 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 70 69 64 ){. DWORD pid
100c5 20 3d 20 47 65 74 43 75 72 72 65 6e 74 50 72 6f = GetCurrentPro
100c6 63 65 73 73 49 64 28 29 3b 0a 20 20 20 20 6d 65 cessId();. me
100c7 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
100c8 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 pid, sizeof(pid)
100c9 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
100ca 6f 66 28 70 69 64 29 3b 0a 20 20 7d 0a 20 20 69 of(pid);. }. i
100cb 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 f( sizeof(DWORD)
100cc 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 <=nBuf-n ){.
100cd 44 57 4f 52 44 20 63 6e 74 20 3d 20 47 65 74 54 DWORD cnt = GetT
100ce 69 63 6b 43 6f 75 6e 74 28 29 3b 0a 20 20 20 20 ickCount();.
100cf 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c memcpy(&zBuf[n],
100d0 20 26 63 6e 74 2c 20 73 69 7a 65 6f 66 28 63 6e &cnt, sizeof(cn
100d1 74 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 t));. n += si
100d2 7a 65 6f 66 28 63 6e 74 29 3b 0a 20 20 7d 0a 20 zeof(cnt);. }.
100d3 20 69 66 28 20 73 69 7a 65 6f 66 28 4c 41 52 47 if( sizeof(LARG
100d4 45 5f 49 4e 54 45 47 45 52 29 3c 3d 6e 42 75 66 E_INTEGER)<=nBuf
100d5 2d 6e 20 29 7b 0a 20 20 20 20 4c 41 52 47 45 5f -n ){. LARGE_
100d6 49 4e 54 45 47 45 52 20 69 3b 0a 20 20 20 20 51 INTEGER i;. Q
100d7 75 65 72 79 50 65 72 66 6f 72 6d 61 6e 63 65 43 ueryPerformanceC
100d8 6f 75 6e 74 65 72 28 26 69 29 3b 0a 20 20 20 20 ounter(&i);.
100d9 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c memcpy(&zBuf[n],
100da 20 26 69 2c 20 73 69 7a 65 6f 66 28 69 29 29 3b &i, sizeof(i));
100db 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 . n += sizeof
100dc 28 69 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a (i);. }.#endif.
100dd 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a return n;.}...
100de 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 /*.** Sleep for
100df 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 a little while.
100e0 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 Return the amou
100e1 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 nt of time slept
100e2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
100e3 77 69 6e 53 6c 65 65 70 28 73 71 6c 69 74 65 33 winSleep(sqlite3
100e4 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
100e5 6d 69 63 72 6f 73 65 63 29 7b 0a 20 20 53 6c 65 microsec){. Sle
100e6 65 70 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 ep((microsec+999
100e7 29 2f 31 30 30 30 29 3b 0a 20 20 55 4e 55 53 45 )/1000);. UNUSE
100e8 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
100e9 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6d 69 );. return ((mi
100ea 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30 30 30 crosec+999)/1000
100eb 29 2a 31 30 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a )*1000;.}../*.**
100ec 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 The following v
100ed 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 74 20 ariable, if set
100ee 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 to a non-zero va
100ef 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74 68 65 lue, becomes the
100f0 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72 result.** retur
100f1 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
100f2 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29 2e OsCurrentTime().
100f3 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66 This is used f
100f4 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 or testing..*/.#
100f5 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
100f6 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
100f7 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 sqlite3_current
100f8 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69 _time = 0;.#endi
100f9 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 f../*.** Find th
100fa 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 e current time (
100fb 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f in Universal Coo
100fc 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 rdinated Time).
100fd 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 Write the.** cu
100fe 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 rrent time and d
100ff 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 ate as a Julian
10100 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 Day number into
10101 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 *prNow and.** re
10102 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 turn 0. Return
10103 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 1 if the time an
10104 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 d date cannot be
10105 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 77 found..*/.int w
10106 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71 inCurrentTime(sq
10107 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
10108 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b double *prNow){
10109 0a 20 20 46 49 4c 45 54 49 4d 45 20 66 74 3b 0a . FILETIME ft;.
1010a 20 20 2f 2a 20 46 49 4c 45 54 49 4d 45 20 73 74 /* FILETIME st
1010b 72 75 63 74 75 72 65 20 69 73 20 61 20 36 34 2d ructure is a 64-
1010c 62 69 74 20 76 61 6c 75 65 20 72 65 70 72 65 73 bit value repres
1010d 65 6e 74 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 enting the numbe
1010e 72 20 6f 66 20 0a 20 20 20 20 20 31 30 30 2d 6e r of . 100-n
1010f 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 anosecond interv
10110 61 6c 73 20 73 69 6e 63 65 20 4a 61 6e 75 61 72 als since Januar
10111 79 20 31 2c 20 31 36 30 31 20 28 3d 20 4a 44 20 y 1, 1601 (= JD
10112 32 33 30 35 38 31 33 2e 35 29 2e 20 0a 20 20 2a 2305813.5). . *
10113 2f 0a 20 20 64 6f 75 62 6c 65 20 6e 6f 77 3b 0a /. double now;.
10114 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
10115 4e 43 45 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 NCE. SYSTEMTIME
10116 20 74 69 6d 65 3b 0a 20 20 47 65 74 53 79 73 74 time;. GetSyst
10117 65 6d 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a 20 emTime(&time);.
10118 20 2f 2a 20 69 66 20 53 79 73 74 65 6d 54 69 6d /* if SystemTim
10119 65 54 6f 46 69 6c 65 54 69 6d 65 28 29 20 66 61 eToFileTime() fa
1011a 69 6c 73 2c 20 69 74 20 72 65 74 75 72 6e 73 20 ils, it returns
1011b 7a 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28 21 zero. */. if (!
1011c 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 SystemTimeToFile
1011d 54 69 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 29 Time(&time,&ft))
1011e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a {. return 1;.
1011f 20 20 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74 53 }.#else. GetS
10120 79 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 54 ystemTimeAsFileT
10121 69 6d 65 28 20 26 66 74 20 29 3b 0a 23 65 6e 64 ime( &ft );.#end
10122 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 if. UNUSED_PARA
10123 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 6e METER(pVfs);. n
10124 6f 77 20 3d 20 28 28 64 6f 75 62 6c 65 29 66 74 ow = ((double)ft
10125 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d 65 29 .dwHighDateTime)
10126 20 2a 20 34 32 39 34 39 36 37 32 39 36 2e 30 3b * 4294967296.0;
10127 20 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 6e 6f . *prNow = (no
10128 77 20 2b 20 66 74 2e 64 77 4c 6f 77 44 61 74 65 w + ft.dwLowDate
10129 54 69 6d 65 29 2f 38 36 34 30 30 30 30 30 30 30 Time)/8640000000
1012a 30 30 2e 30 20 2b 20 32 33 30 35 38 31 33 2e 35 00.0 + 2305813.5
1012b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
1012c 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 TEST. if( sqlit
1012d 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 e3_current_time
1012e 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 ){. *prNow =
1012f 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f sqlite3_current_
10130 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 time/86400.0 + 2
10131 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 440587.5;. }.#e
10132 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b ndif. return 0;
10133 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 64 .}../*.** The id
10134 65 61 20 69 73 20 74 68 61 74 20 74 68 69 73 20 ea is that this
10135 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 6c function works l
10136 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f ike a combinatio
10137 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61 73 74 45 n of.** GetLastE
10138 72 72 6f 72 28 29 20 61 6e 64 20 46 6f 72 6d 61 rror() and Forma
10139 74 4d 65 73 73 61 67 65 28 29 20 6f 6e 20 77 69 tMessage() on wi
1013a 6e 64 6f 77 73 20 28 6f 72 20 65 72 72 6e 6f 20 ndows (or errno
1013b 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72 6f 72 5f and.** strerror_
1013c 72 28 29 20 6f 6e 20 75 6e 69 78 29 2e 20 41 66 r() on unix). Af
1013d 74 65 72 20 61 6e 20 65 72 72 6f 72 20 69 73 20 ter an error is
1013e 72 65 74 75 72 6e 65 64 20 62 79 20 61 6e 20 4f returned by an O
1013f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c 20 53 S.** function, S
10140 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 QLite calls this
10141 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 7a function with z
10142 42 75 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a Buf pointing to.
10143 2a 2a 20 61 20 62 75 66 66 65 72 20 6f 66 20 6e ** a buffer of n
10144 42 75 66 20 62 79 74 65 73 2e 20 54 68 65 20 4f Buf bytes. The O
10145 53 20 6c 61 79 65 72 20 73 68 6f 75 6c 64 20 70 S layer should p
10146 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20 62 opulate the.** b
10147 75 66 66 65 72 20 77 69 74 68 20 61 20 6e 75 6c uffer with a nul
10148 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d -terminated UTF-
10149 38 20 65 6e 63 6f 64 65 64 20 65 72 72 6f 72 20 8 encoded error
1014a 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65 73 63 72 message.** descr
1014b 69 62 69 6e 67 20 74 68 65 20 6c 61 73 74 20 49 ibing the last I
1014c 4f 20 65 72 72 6f 72 20 74 6f 20 68 61 76 65 20 O error to have
1014d 6f 63 63 75 72 65 64 20 77 69 74 68 69 6e 20 74 occured within t
1014e 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 he calling.** th
1014f 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 read..**.** If t
10150 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
10151 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f is too large fo
10152 72 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 r the supplied b
10153 75 66 66 65 72 2c 0a 2a 2a 20 69 74 20 73 68 6f uffer,.** it sho
10154 75 6c 64 20 62 65 20 74 72 75 6e 63 61 74 65 64 uld be truncated
10155 2e 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c . The return val
10156 75 65 20 6f 66 20 78 47 65 74 4c 61 73 74 45 72 ue of xGetLastEr
10157 72 6f 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 ror.** is zero i
10158 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 f the error mess
10159 61 67 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 age fits in the
1015a 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a buffer, or non-z
1015b 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 ero.** otherwise
1015c 20 28 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 (if the message
1015d 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e was truncated).
1015e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 If non-zero is
1015f 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 74 68 65 returned,.** the
10160 6e 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 n it is not nece
10161 73 73 61 72 79 20 74 6f 20 69 6e 63 6c 75 64 65 ssary to include
10162 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 the nul-termina
10163 74 6f 72 20 63 68 61 72 61 63 74 65 72 0a 2a 2a tor character.**
10164 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 in the output b
10165 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 uffer..**.** Not
10166 20 73 75 70 70 6c 79 69 6e 67 20 61 6e 20 65 72 supplying an er
10167 72 6f 72 20 6d 65 73 73 61 67 65 20 77 69 6c 6c ror message will
10168 20 68 61 76 65 20 6e 6f 20 61 64 76 65 72 73 65 have no adverse
10169 20 65 66 66 65 63 74 0a 2a 2a 20 6f 6e 20 53 51 effect.** on SQ
1016a 4c 69 74 65 2e 20 49 74 20 69 73 20 66 69 6e 65 Lite. It is fine
1016b 20 74 6f 20 68 61 76 65 20 61 6e 20 69 6d 70 6c to have an impl
1016c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 ementation that
1016d 6e 65 76 65 72 0a 2a 2a 20 72 65 74 75 72 6e 73 never.** returns
1016e 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1016f 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 6e 74 20 78 e:.**.** int x
10170 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c GetLastError(sql
10171 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
10172 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
10173 7a 42 75 66 29 7b 0a 2a 2a 20 20 20 20 20 61 73 zBuf){.** as
10174 73 65 72 74 28 7a 42 75 66 5b 30 5d 3d 3d 27 5c sert(zBuf[0]=='\
10175 30 27 29 3b 0a 2a 2a 20 20 20 20 20 72 65 74 75 0');.** retu
10176 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a rn 0;.** }.**.
10177 2a 2a 20 48 6f 77 65 76 65 72 20 69 66 20 61 6e ** However if an
10178 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 error message i
10179 73 20 73 75 70 70 6c 69 65 64 2c 20 69 74 20 77 s supplied, it w
1017a 69 6c 6c 20 62 65 20 69 6e 63 6f 72 70 6f 72 61 ill be incorpora
1017b 74 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 ted.** by sqlite
1017c 20 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 20 into the error
1017d 6d 65 73 73 61 67 65 20 61 76 61 69 6c 61 62 6c message availabl
1017e 65 20 74 6f 20 74 68 65 20 75 73 65 72 20 75 73 e to the user us
1017f 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 ing.** sqlite3_e
10180 72 72 6d 73 67 28 29 2c 20 70 6f 73 73 69 62 6c rrmsg(), possibl
10181 79 20 6d 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f y making IO erro
10182 72 73 20 65 61 73 69 65 72 20 74 6f 20 64 65 62 rs easier to deb
10183 75 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ug..*/.static in
10184 74 20 77 69 6e 47 65 74 4c 61 73 74 45 72 72 6f t winGetLastErro
10185 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 r(sqlite3_vfs *p
10186 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 Vfs, int nBuf, c
10187 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e har *zBuf){. UN
10188 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
10189 56 66 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 Vfs);. return g
1018a 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 6e etLastErrorMsg(n
1018b 42 75 66 2c 20 7a 42 75 66 29 3b 0a 7d 0a 0a 2f Buf, zBuf);.}../
1018c 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
1018d 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 and deinitialize
1018e 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 the operating s
1018f 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e ystem interface.
10190 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
10191 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e nt sqlite3_os_in
10192 69 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 it(void){. stat
10193 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 77 ic sqlite3_vfs w
10194 69 6e 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c inVfs = {. 1,
10195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10196 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
10197 20 20 20 20 73 69 7a 65 6f 66 28 77 69 6e 46 69 sizeof(winFi
10198 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69 le), /* szOsFi
10199 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 58 5f 50 41 le */. MAX_PA
1019a 54 48 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 TH, /*
1019b 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 mxPathname */.
1019c 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
1019d 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f /* pNext */
1019e 0a 20 20 20 20 22 77 69 6e 33 32 22 2c 20 20 20 . "win32",
1019f 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 /* zName
101a0 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 */. 0,
101a1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 /* pA
101a2 70 70 44 61 74 61 20 2a 2f 0a 20 0a 20 20 20 20 ppData */. .
101a3 77 69 6e 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 winOpen,
101a4 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 /* xOpen */.
101a5 20 20 20 77 69 6e 44 65 6c 65 74 65 2c 20 20 20 winDelete,
101a6 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 /* xDelete
101a7 20 2a 2f 0a 20 20 20 20 77 69 6e 41 63 63 65 73 */. winAcces
101a8 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 s, /* xA
101a9 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e ccess */. win
101aa 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 FullPathname,
101ab 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 /* xFullPathname
101ac 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 4f 70 65 */. winDlOpe
101ad 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 n, /* xD
101ae 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e lOpen */. win
101af 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 DlError,
101b0 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20 /* xDlError */.
101b1 20 20 20 77 69 6e 44 6c 53 79 6d 2c 20 20 20 20 winDlSym,
101b2 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 /* xDlSym
101b3 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 43 6c 6f 73 */. winDlClos
101b4 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c e, /* xDl
101b5 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 77 69 6e Close */. win
101b6 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 Randomness,
101b7 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a /* xRandomness *
101b8 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65 70 2c 20 /. winSleep,
101b9 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 /* xSle
101ba 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e 43 75 72 ep */. winCur
101bb 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20 rentTime, /*
101bc 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a xCurrentTime */.
101bd 20 20 20 20 77 69 6e 47 65 74 4c 61 73 74 45 72 winGetLastEr
101be 72 6f 72 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 ror /* xGetLa
101bf 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20 7d 3b 0a stError */. };.
101c0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 sqlite3_vfs_re
101c1 67 69 73 74 65 72 28 26 77 69 6e 56 66 73 2c 20 gister(&winVfs,
101c2 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 1);. return SQL
101c3 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53 51 4c 49 54 ITE_OK; .}.SQLIT
101c4 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
101c5 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 3_os_end(void){
101c6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
101c7 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f _OK;.}..#endif /
101c8 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 * SQLITE_OS_WIN
101c9 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
101ca 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 77 69 *** End of os_wi
101cb 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
101cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101ce 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
101cf 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 *** Begin file b
101d0 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a itvec.c ********
101d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101d3 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 46 **/./*.** 2008 F
101d4 65 62 72 75 61 72 79 20 31 36 0a 2a 2a 0a 2a 2a ebruary 16.**.**
101d5 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
101d6 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
101d7 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
101d8 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
101d9 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
101da 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
101db 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
101dc 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
101dd 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
101de 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
101df 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
101e0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
101e1 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
101e2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
101e3 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
101e4 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
101e5 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
101e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
101eb 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 s file implement
101ec 73 20 61 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 s an object that
101ed 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 66 69 represents a fi
101ee 78 65 64 2d 6c 65 6e 67 74 68 0a 2a 2a 20 62 69 xed-length.** bi
101ef 74 6d 61 70 2e 20 20 42 69 74 73 20 61 72 65 20 tmap. Bits are
101f0 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e numbered startin
101f1 67 20 77 69 74 68 20 31 2e 0a 2a 2a 0a 2a 2a 20 g with 1..**.**
101f2 41 20 62 69 74 6d 61 70 20 69 73 20 75 73 65 64 A bitmap is used
101f3 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 63 68 to record which
101f4 20 70 61 67 65 73 20 6f 66 20 61 20 64 61 74 61 pages of a data
101f5 62 61 73 65 20 66 69 6c 65 20 68 61 76 65 20 62 base file have b
101f6 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 een.** journalle
101f7 64 20 64 75 72 69 6e 67 20 61 20 74 72 61 6e 73 d during a trans
101f8 61 63 74 69 6f 6e 2c 20 6f 72 20 77 68 69 63 68 action, or which
101f9 20 70 61 67 65 73 20 68 61 76 65 20 74 68 65 20 pages have the
101fa 22 64 6f 6e 74 2d 77 72 69 74 65 22 0a 2a 2a 20 "dont-write".**
101fb 70 72 6f 70 65 72 74 79 2e 20 20 55 73 75 61 6c property. Usual
101fc 6c 79 20 6f 6e 6c 79 20 61 20 66 65 77 20 70 61 ly only a few pa
101fd 67 65 73 20 61 72 65 20 6d 65 65 74 20 65 69 74 ges are meet eit
101fe 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a her condition..*
101ff 2a 20 53 6f 20 74 68 65 20 62 69 74 6d 61 70 20 * So the bitmap
10200 69 73 20 75 73 75 61 6c 6c 79 20 73 70 61 72 73 is usually spars
10201 65 20 61 6e 64 20 68 61 73 20 6c 6f 77 20 63 61 e and has low ca
10202 72 64 69 6e 61 6c 69 74 79 2e 0a 2a 2a 20 42 75 rdinality..** Bu
10203 74 20 73 6f 6d 65 74 69 6d 65 73 20 28 66 6f 72 t sometimes (for
10204 20 65 78 61 6d 70 6c 65 20 77 68 65 6e 20 64 75 example when du
10205 72 69 6e 67 20 61 20 44 52 4f 50 20 6f 66 20 61 ring a DROP of a
10206 20 6c 61 72 67 65 20 74 61 62 6c 65 29 20 6d 6f large table) mo
10207 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c 20 6f 66 20 st.** or all of
10208 74 68 65 20 70 61 67 65 73 20 69 6e 20 61 20 64 the pages in a d
10209 61 74 61 62 61 73 65 20 63 61 6e 20 67 65 74 20 atabase can get
1020a 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 20 49 6e 20 journalled. In
1020b 74 68 6f 73 65 20 63 61 73 65 73 2c 20 0a 2a 2a those cases, .**
1020c 20 74 68 65 20 62 69 74 6d 61 70 20 62 65 63 6f the bitmap beco
1020d 6d 65 73 20 64 65 6e 73 65 20 77 69 74 68 20 68 mes dense with h
1020e 69 67 68 20 63 61 72 64 69 6e 61 6c 69 74 79 2e igh cardinality.
1020f 20 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 The algorithm
10210 6e 65 65 64 73 20 0a 2a 2a 20 74 6f 20 68 61 6e needs .** to han
10211 64 6c 65 20 62 6f 74 68 20 63 61 73 65 73 20 77 dle both cases w
10212 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ell..**.** The s
10213 69 7a 65 20 6f 66 20 74 68 65 20 62 69 74 6d 61 ize of the bitma
10214 70 20 69 73 20 66 69 78 65 64 20 77 68 65 6e 20 p is fixed when
10215 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 63 72 the object is cr
10216 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c eated..**.** All
10217 20 62 69 74 73 20 61 72 65 20 63 6c 65 61 72 20 bits are clear
10218 77 68 65 6e 20 74 68 65 20 62 69 74 6d 61 70 20 when the bitmap
10219 69 73 20 63 72 65 61 74 65 64 2e 20 20 49 6e 64 is created. Ind
1021a 69 76 69 64 75 61 6c 20 62 69 74 73 0a 2a 2a 20 ividual bits.**
1021b 6d 61 79 20 62 65 20 73 65 74 20 6f 72 20 63 6c may be set or cl
1021c 65 61 72 65 64 20 6f 6e 65 20 61 74 20 61 20 74 eared one at a t
1021d 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74 20 ime..**.** Test
1021e 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 61 operations are a
1021f 62 6f 75 74 20 31 30 30 20 74 69 6d 65 73 20 6d bout 100 times m
10220 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20 ore common that
10221 73 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a set operations..
10222 2a 2a 20 43 6c 65 61 72 20 6f 70 65 72 61 74 69 ** Clear operati
10223 6f 6e 73 20 61 72 65 20 65 78 63 65 65 64 69 6e ons are exceedin
10224 67 6c 79 20 72 61 72 65 2e 20 20 54 68 65 72 65 gly rare. There
10225 20 61 72 65 20 75 73 75 61 6c 6c 79 20 62 65 74 are usually bet
10226 77 65 65 6e 0a 2a 2a 20 35 20 61 6e 64 20 35 30 ween.** 5 and 50
10227 30 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 0 set operations
10228 20 70 65 72 20 42 69 74 76 65 63 20 6f 62 6a 65 per Bitvec obje
10229 63 74 2c 20 74 68 6f 75 67 68 20 74 68 65 20 6e ct, though the n
1022a 75 6d 62 65 72 20 6f 66 20 73 65 74 73 20 63 61 umber of sets ca
1022b 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20 67 n.** sometimes g
1022c 72 6f 77 20 69 6e 74 6f 20 74 65 6e 73 20 6f 66 row into tens of
1022d 20 74 68 6f 75 73 61 6e 64 73 20 6f 72 20 6c 61 thousands or la
1022e 72 67 65 72 2e 20 20 54 68 65 20 73 69 7a 65 20 rger. The size
1022f 6f 66 20 74 68 65 0a 2a 2a 20 42 69 74 76 65 63 of the.** Bitvec
10230 20 6f 62 6a 65 63 74 20 69 73 20 74 68 65 20 6e object is the n
10231 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
10232 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
10233 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 73 74 ile at the.** st
10234 61 72 74 20 6f 66 20 61 20 74 72 61 6e 73 61 63 art of a transac
10235 74 69 6f 6e 2c 20 61 6e 64 20 69 73 20 74 68 75 tion, and is thu
10236 73 20 75 73 75 61 6c 6c 79 20 6c 65 73 73 20 74 s usually less t
10237 68 61 6e 20 61 20 66 65 77 20 74 68 6f 75 73 61 han a few thousa
10238 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 61 6e 20 62 nd,.** but can b
10239 65 20 61 73 20 6c 61 72 67 65 20 61 73 20 32 20 e as large as 2
1023a 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 61 20 72 65 billion for a re
1023b 61 6c 6c 79 20 62 69 67 20 64 61 74 61 62 61 73 ally big databas
1023c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 e..**.** @(#) $I
1023d 64 3a 20 62 69 74 76 65 63 2e 63 2c 76 20 31 2e d: bitvec.c,v 1.
1023e 31 30 20 32 30 30 39 2f 30 31 2f 30 32 20 32 31 10 2009/01/02 21
1023f 3a 33 39 3a 33 39 20 64 72 68 20 45 78 70 20 24 :39:39 drh Exp $
10240 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 .*/../* Size of
10241 74 68 65 20 42 69 74 76 65 63 20 73 74 72 75 63 the Bitvec struc
10242 74 75 72 65 20 69 6e 20 62 79 74 65 73 2e 20 2a ture in bytes. *
10243 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 /.#define BITVEC
10244 5f 53 5a 20 20 20 20 20 20 20 20 35 31 32 0a 0a _SZ 512..
10245 2f 2a 20 52 6f 75 6e 64 20 74 68 65 20 75 6e 69 /* Round the uni
10246 6f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20 on size down to
10247 74 68 65 20 6e 65 61 72 65 73 74 20 70 6f 69 6e the nearest poin
10248 74 65 72 20 62 6f 75 6e 64 61 72 79 2c 20 73 69 ter boundary, si
10249 6e 63 65 20 74 68 61 74 27 73 20 68 6f 77 20 0a nce that's how .
1024a 2a 2a 20 69 74 20 77 69 6c 6c 20 62 65 20 61 6c ** it will be al
1024b 69 67 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 igned within the
1024c 20 42 69 74 76 65 63 20 73 74 72 75 63 74 2e 20 Bitvec struct.
1024d 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 */.#define BITVE
1024e 43 5f 55 53 49 5a 45 20 20 20 20 20 28 28 28 42 C_USIZE (((B
1024f 49 54 56 45 43 5f 53 5a 2d 28 33 2a 73 69 7a 65 ITVEC_SZ-(3*size
10250 6f 66 28 75 33 32 29 29 29 2f 73 69 7a 65 6f 66 of(u32)))/sizeof
10251 28 42 69 74 76 65 63 2a 29 29 2a 73 69 7a 65 6f (Bitvec*))*sizeo
10252 66 28 42 69 74 76 65 63 2a 29 29 0a 0a 2f 2a 20 f(Bitvec*))../*
10253 54 79 70 65 20 6f 66 20 74 68 65 20 61 72 72 61 Type of the arra
10254 79 20 22 65 6c 65 6d 65 6e 74 22 20 66 6f 72 20 y "element" for
10255 74 68 65 20 62 69 74 6d 61 70 20 72 65 70 72 65 the bitmap repre
10256 73 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 53 sentation. .** S
10257 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 77 65 72 hould be a power
10258 20 6f 66 20 32 2c 20 61 6e 64 20 69 64 65 61 6c of 2, and ideal
10259 6c 79 2c 20 65 76 65 6e 6c 79 20 64 69 76 69 64 ly, evenly divid
1025a 65 20 69 6e 74 6f 20 42 49 54 56 45 43 5f 55 53 e into BITVEC_US
1025b 49 5a 45 2e 20 0a 2a 2a 20 53 65 74 74 69 6e 67 IZE. .** Setting
1025c 20 74 68 69 73 20 74 6f 20 74 68 65 20 22 6e 61 this to the "na
1025d 74 75 72 61 6c 20 77 6f 72 64 22 20 73 69 7a 65 tural word" size
1025e 20 6f 66 20 79 6f 75 72 20 43 50 55 20 6d 61 79 of your CPU may
1025f 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 70 65 72 66 improve.** perf
10260 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a 23 64 65 66 ormance. */.#def
10261 69 6e 65 20 42 49 54 56 45 43 5f 54 45 4c 45 4d ine BITVEC_TELEM
10262 20 20 20 20 20 75 38 0a 2f 2a 20 53 69 7a 65 2c u8./* Size,
10263 20 69 6e 20 62 69 74 73 2c 20 6f 66 20 74 68 65 in bits, of the
10264 20 62 69 74 6d 61 70 20 65 6c 65 6d 65 6e 74 2e bitmap element.
10265 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 */.#define BITV
10266 45 43 5f 53 5a 45 4c 45 4d 20 20 20 20 38 0a 2f EC_SZELEM 8./
10267 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d * Number of elem
10268 65 6e 74 73 20 69 6e 20 61 20 62 69 74 6d 61 70 ents in a bitmap
10269 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 array. */.#defi
1026a 6e 65 20 42 49 54 56 45 43 5f 4e 45 4c 45 4d 20 ne BITVEC_NELEM
1026b 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a (BITVEC_USIZ
1026c 45 2f 73 69 7a 65 6f 66 28 42 49 54 56 45 43 5f E/sizeof(BITVEC_
1026d 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e 75 6d 62 65 TELEM))./* Numbe
1026e 72 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 r of bits in the
1026f 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a bitmap array. *
10270 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 /.#define BITVEC
10271 5f 4e 42 49 54 20 20 20 20 20 20 28 42 49 54 56 _NBIT (BITV
10272 45 43 5f 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f EC_NELEM*BITVEC_
10273 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 4e 75 6d 62 SZELEM)../* Numb
10274 65 72 20 6f 66 20 75 33 32 20 76 61 6c 75 65 73 er of u32 values
10275 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20 in hash table.
10276 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 */.#define BITVE
10277 43 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49 54 C_NINT (BIT
10278 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 VEC_USIZE/sizeof
10279 28 75 33 32 29 29 0a 2f 2a 20 4d 61 78 69 6d 75 (u32))./* Maximu
1027a 6d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 m number of entr
1027b 69 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 6c ies in hash tabl
1027c 65 20 62 65 66 6f 72 65 20 0a 2a 2a 20 73 75 62 e before .** sub
1027d 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65 -dividing and re
1027e 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 23 64 65 -hashing. */.#de
1027f 66 69 6e 65 20 42 49 54 56 45 43 5f 4d 58 48 41 fine BITVEC_MXHA
10280 53 48 20 20 20 20 28 42 49 54 56 45 43 5f 4e 49 SH (BITVEC_NI
10281 4e 54 2f 32 29 0a 2f 2a 20 48 61 73 68 69 6e 67 NT/2)./* Hashing
10282 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 function for th
10283 65 20 61 48 61 73 68 20 72 65 70 72 65 73 65 6e e aHash represen
10284 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45 6d 70 69 72 tation..** Empir
10285 69 63 61 6c 20 74 65 73 74 69 6e 67 20 73 68 6f ical testing sho
10286 77 65 64 20 74 68 61 74 20 74 68 65 20 2a 33 37 wed that the *37
10287 20 6d 75 6c 74 69 70 6c 69 65 72 20 0a 2a 2a 20 multiplier .**
10288 28 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 72 (an arbitrary pr
10289 69 6d 65 29 69 6e 20 74 68 65 20 68 61 73 68 20 ime)in the hash
1028a 66 75 6e 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 function provide
1028b 64 20 0a 2a 2a 20 6e 6f 20 66 65 77 65 72 20 63 d .** no fewer c
1028c 6f 6c 6c 69 73 69 6f 6e 73 20 74 68 61 6e 20 74 ollisions than t
1028d 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e 20 2a 2f 0a he no-op *1. */.
1028e 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 48 #define BITVEC_H
1028f 41 53 48 28 58 29 20 20 20 28 28 28 58 29 2a 31 ASH(X) (((X)*1
10290 29 25 42 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a )%BITVEC_NINT)..
10291 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e #define BITVEC_N
10292 50 54 52 20 20 20 20 20 20 28 42 49 54 56 45 43 PTR (BITVEC
10293 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 69 _USIZE/sizeof(Bi
10294 74 76 65 63 20 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a tvec *)).../*.**
10295 20 41 20 62 69 74 6d 61 70 20 69 73 20 61 6e 20 A bitmap is an
10296 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
10297 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
10298 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ure..**.** This
10299 62 69 74 6d 61 70 20 72 65 63 6f 72 64 73 20 74 bitmap records t
1029a 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 he existance of
1029b 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 62 69 74 zero or more bit
1029c 73 0a 2a 2a 20 77 69 74 68 20 76 61 6c 75 65 73 s.** with values
1029d 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 between 1 and i
1029e 53 69 7a 65 2c 20 69 6e 63 6c 75 73 69 76 65 2e Size, inclusive.
1029f 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
102a0 20 74 68 72 65 65 20 70 6f 73 73 69 62 6c 65 20 three possible
102a1 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 representations
102a2 6f 66 20 74 68 65 20 62 69 74 6d 61 70 2e 0a 2a of the bitmap..*
102a3 2a 20 49 66 20 69 53 69 7a 65 3c 3d 42 49 54 56 * If iSize<=BITV
102a4 45 43 5f 4e 42 49 54 2c 20 74 68 65 6e 20 42 69 EC_NBIT, then Bi
102a5 74 76 65 63 2e 75 2e 61 42 69 74 6d 61 70 5b 5d tvec.u.aBitmap[]
102a6 20 69 73 20 61 20 73 74 72 61 69 67 68 74 0a 2a is a straight.*
102a7 2a 20 62 69 74 6d 61 70 2e 20 20 54 68 65 20 6c * bitmap. The l
102a8 65 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 east significant
102a9 20 62 69 74 20 69 73 20 62 69 74 20 31 2e 0a 2a bit is bit 1..*
102aa 2a 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3e 42 49 *.** If iSize>BI
102ab 54 56 45 43 5f 4e 42 49 54 20 61 6e 64 20 69 44 TVEC_NBIT and iD
102ac 69 76 69 73 6f 72 3d 3d 30 20 74 68 65 6e 20 42 ivisor==0 then B
102ad 69 74 76 65 63 2e 75 2e 61 48 61 73 68 5b 5d 20 itvec.u.aHash[]
102ae 69 73 0a 2a 2a 20 61 20 68 61 73 68 20 74 61 62 is.** a hash tab
102af 6c 65 20 74 68 61 74 20 77 69 6c 6c 20 68 6f 6c le that will hol
102b0 64 20 75 70 20 74 6f 20 42 49 54 56 45 43 5f 4d d up to BITVEC_M
102b1 58 48 41 53 48 20 64 69 73 74 69 6e 63 74 20 76 XHASH distinct v
102b2 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 alues..**.** Oth
102b3 65 72 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75 erwise, the valu
102b4 65 20 69 20 69 73 20 72 65 64 69 72 65 63 74 65 e i is redirecte
102b5 64 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 42 49 d into one of BI
102b6 54 56 45 43 5f 4e 50 54 52 0a 2a 2a 20 73 75 62 TVEC_NPTR.** sub
102b7 2d 62 69 74 6d 61 70 73 20 70 6f 69 6e 74 65 64 -bitmaps pointed
102b8 20 74 6f 20 62 79 20 42 69 74 76 65 63 2e 75 2e to by Bitvec.u.
102b9 61 70 53 75 62 5b 5d 2e 20 20 45 61 63 68 20 73 apSub[]. Each s
102ba 75 62 62 69 74 6d 61 70 0a 2a 2a 20 68 61 6e 64 ubbitmap.** hand
102bb 6c 65 73 20 75 70 20 74 6f 20 69 44 69 76 69 73 les up to iDivis
102bc 6f 72 20 73 65 70 61 72 61 74 65 20 76 61 6c 75 or separate valu
102bd 65 73 20 6f 66 20 69 2e 20 20 61 70 53 75 62 5b es of i. apSub[
102be 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20 76 61 6c 75 0] holds.** valu
102bf 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 es between 1 and
102c0 20 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75 iDivisor. apSu
102c1 62 5b 31 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 b[1] holds value
102c2 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 69 44 69 s between.** iDi
102c3 76 69 73 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44 visor+1 and 2*iD
102c4 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e ivisor. apSub[N
102c5 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 ] holds values b
102c6 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a 69 44 69 76 etween.** N*iDiv
102c7 69 73 6f 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29 isor+1 and (N+1)
102c8 2a 69 44 69 76 69 73 6f 72 2e 20 20 45 61 63 68 *iDivisor. Each
102c9 20 73 75 62 62 69 74 6d 61 70 20 69 73 20 6e 6f subbitmap is no
102ca 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 68 rmalized.** to h
102cb 6f 6c 64 20 64 65 61 6c 20 77 69 74 68 20 76 61 old deal with va
102cc 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 lues between 1 a
102cd 6e 64 20 69 44 69 76 69 73 6f 72 2e 0a 2a 2f 0a nd iDivisor..*/.
102ce 73 74 72 75 63 74 20 42 69 74 76 65 63 20 7b 0a struct Bitvec {.
102cf 20 20 75 33 32 20 69 53 69 7a 65 3b 20 20 20 20 u32 iSize;
102d0 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 62 69 74 /* Maximum bit
102d1 20 69 6e 64 65 78 2e 20 20 4d 61 78 20 69 53 69 index. Max iSi
102d2 7a 65 20 69 73 20 34 2c 32 39 34 2c 39 36 37 2c ze is 4,294,967,
102d3 32 39 36 2e 20 2a 2f 0a 20 20 75 33 32 20 6e 53 296. */. u32 nS
102d4 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d et; /* Num
102d5 62 65 72 20 6f 66 20 62 69 74 73 20 74 68 61 74 ber of bits that
102d6 20 61 72 65 20 73 65 74 20 2d 20 6f 6e 6c 79 20 are set - only
102d7 76 61 6c 69 64 20 66 6f 72 20 61 48 61 73 68 20 valid for aHash
102d8 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 element */.
102d9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
102da 4d 61 78 20 6e 53 65 74 20 69 73 20 42 49 54 56 Max nSet is BITV
102db 45 43 5f 4e 49 4e 54 2e 20 20 46 6f 72 20 42 49 EC_NINT. For BI
102dc 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c 20 TVEC_SZ of 512,
102dd 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 31 32 this would be 12
102de 35 2e 20 2a 2f 0a 20 20 75 33 32 20 69 44 69 76 5. */. u32 iDiv
102df 69 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 isor; /* Numbe
102e0 72 20 6f 66 20 62 69 74 73 20 68 61 6e 64 6c 65 r of bits handle
102e1 64 20 62 79 20 65 61 63 68 20 61 70 53 75 62 5b d by each apSub[
102e2 5d 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20 ] entry. */.
102e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
102e4 20 53 68 6f 75 6c 64 20 3e 3d 30 20 66 6f 72 20 Should >=0 for
102e5 61 70 53 75 62 20 65 6c 65 6d 65 6e 74 2e 20 2a apSub element. *
102e6 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /.
102e7 20 20 20 20 2f 2a 20 4d 61 78 20 69 44 69 76 69 /* Max iDivi
102e8 73 6f 72 20 69 73 20 6d 61 78 28 75 33 32 29 20 sor is max(u32)
102e9 2f 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2b 20 / BITVEC_NPTR +
102ea 31 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 1. */.
102eb 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 /* For
102ec 61 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 a BITVEC_SZ of 5
102ed 31 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 62 12, this would b
102ee 65 20 33 34 2c 33 35 39 2c 37 33 39 2e 20 2a 2f e 34,359,739. */
102ef 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 42 . union {. B
102f0 49 54 56 45 43 5f 54 45 4c 45 4d 20 61 42 69 74 ITVEC_TELEM aBit
102f1 6d 61 70 5b 42 49 54 56 45 43 5f 4e 45 4c 45 4d map[BITVEC_NELEM
102f2 5d 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 ]; /* Bitmap
102f3 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a representation *
102f4 2f 0a 20 20 20 20 75 33 32 20 61 48 61 73 68 5b /. u32 aHash[
102f5 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 BITVEC_NINT];
102f6 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 /* Hash table
102f7 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
102f8 2a 2f 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61 */. Bitvec *a
102f9 70 53 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52 pSub[BITVEC_NPTR
102fa 5d 3b 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 ]; /* Recursive
102fb 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
102fc 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a */. } u;.};../*
102fd 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
102fe 20 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 20 61 bitmap object a
102ff 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 62 69 ble to handle bi
10300 74 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 ts between 0 and
10301 20 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 iSize,.** inclu
10302 73 69 76 65 2e 20 20 52 65 74 75 72 6e 20 61 20 sive. Return a
10303 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
10304 65 77 20 6f 62 6a 65 63 74 2e 20 20 52 65 74 75 ew object. Retu
10305 72 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d rn NULL if .** m
10306 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a alloc fails..*/.
10307 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 SQLITE_PRIVATE B
10308 69 74 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 itvec *sqlite3Bi
10309 74 76 65 63 43 72 65 61 74 65 28 75 33 32 20 69 tvecCreate(u32 i
1030a 53 69 7a 65 29 7b 0a 20 20 42 69 74 76 65 63 20 Size){. Bitvec
1030b 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 *p;. assert( si
1030c 7a 65 6f 66 28 2a 70 29 3d 3d 42 49 54 56 45 43 zeof(*p)==BITVEC
1030d 5f 53 5a 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c _SZ );. p = sql
1030e 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 ite3MallocZero(
1030f 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 sizeof(*p) );.
10310 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e if( p ){. p->
10311 69 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b 0a 20 iSize = iSize;.
10312 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
10313 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f ../*.** Check to
10314 20 73 65 65 20 69 66 20 74 68 65 20 69 2d 74 68 see if the i-th
10315 20 62 69 74 20 69 73 20 73 65 74 2e 20 20 52 65 bit is set. Re
10316 74 75 72 6e 20 74 72 75 65 20 6f 72 20 66 61 6c turn true or fal
10317 73 65 2e 0a 2a 2a 20 49 66 20 70 20 69 73 20 4e se..** If p is N
10318 55 4c 4c 20 28 69 66 20 74 68 65 20 62 69 74 6d ULL (if the bitm
10319 61 70 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 ap has not been
1031a 63 72 65 61 74 65 64 29 20 6f 72 20 69 66 0a 2a created) or if.*
1031b 2a 20 69 20 69 73 20 6f 75 74 20 6f 66 20 72 61 * i is out of ra
1031c 6e 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e nge, then return
1031d 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 false..*/.SQLIT
1031e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1031f 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
10320 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 Bitvec *p, u32 i
10321 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 ){. if( p==0 )
10322 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
10323 69 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 69 3d i>p->iSize || i=
10324 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
10325 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 i--;. while( p
10326 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 ->iDivisor ){.
10327 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d u32 bin = i/p-
10328 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 >iDivisor;. i
10329 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 = i%p->iDivisor
1032a 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 ;. p = p->u.a
1032b 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 pSub[bin];. i
1032c 66 20 28 21 70 29 20 7b 0a 20 20 20 20 20 20 72 f (!p) {. r
1032d 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 eturn 0;. }.
1032e 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a }. if( p->iSiz
1032f 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 e<=BITVEC_NBIT )
10330 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 2d {. return (p-
10331 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 >u.aBitmap[i/BIT
10332 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 20 28 31 VEC_SZELEM] & (1
10333 3c 3c 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 <<(i&(BITVEC_SZE
10334 4c 45 4d 2d 31 29 29 29 29 21 3d 30 3b 0a 20 20 LEM-1))))!=0;.
10335 7d 20 65 6c 73 65 7b 0a 20 20 20 20 75 33 32 20 } else{. u32
10336 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 h = BITVEC_HASH(
10337 69 2b 2b 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 i++);. while(
10338 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 p->u.aHash[h] )
10339 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 {. if( p->u
1033a 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 .aHash[h]==i ) r
1033b 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 68 eturn 1;. h
1033c 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 68 3e ++;. if( h>
1033d 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 =BITVEC_NINT ) h
1033e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
1033f 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a return 0;. }.}.
10340 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 69 ./*.** Set the i
10341 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 72 6e -th bit. Return
10342 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 0 on success an
10343 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 d an error code
10344 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 67 if.** anything g
10345 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a oes wrong..**.**
10346 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 This routine mi
10347 67 68 74 20 63 61 75 73 65 20 73 75 62 2d 62 69 ght cause sub-bi
10348 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c 6c 6f tmaps to be allo
10349 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e 67 0a cated. Failing.
1034a 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 6d 65 ** to get the me
1034b 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f 20 68 mory needed to h
1034c 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69 74 6d old the sub-bitm
1034d 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79 0a 2a ap is the only.*
1034e 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20 77 72 * that can go wr
1034f 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 73 65 ong with an inse
10350 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70 20 61 rt, assuming p a
10351 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64 2e 0a nd i are valid..
10352 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e **.** The callin
10353 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 g function must
10354 65 6e 73 75 72 65 20 74 68 61 74 20 70 20 69 73 ensure that p is
10355 20 61 20 76 61 6c 69 64 20 42 69 74 76 65 63 20 a valid Bitvec
10356 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 74 68 object.** and th
10357 61 74 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 at the value for
10358 20 22 69 22 20 69 73 20 77 69 74 68 69 6e 20 72 "i" is within r
10359 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69 74 76 ange of the Bitv
1035a 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 74 ec object..** Ot
1035b 68 65 72 77 69 73 65 20 74 68 65 20 62 65 68 61 herwise the beha
1035c 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 vior is undefine
1035d 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1035e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1035f 42 69 74 76 65 63 53 65 74 28 42 69 74 76 65 63 BitvecSet(Bitvec
10360 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 75 *p, u32 i){. u
10361 33 32 20 68 3b 0a 20 20 61 73 73 65 72 74 28 20 32 h;. assert(
10362 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 p!=0 );. assert
10363 28 20 69 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 ( i>0 );. asser
10364 74 28 20 69 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 t( i<=p->iSize )
10365 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 ;. i--;. while
10366 28 28 70 2d 3e 69 53 69 7a 65 20 3e 20 42 49 54 ((p->iSize > BIT
10367 56 45 43 5f 4e 42 49 54 29 20 26 26 20 70 2d 3e VEC_NBIT) && p->
10368 69 44 69 76 69 73 6f 72 29 20 7b 0a 20 20 20 20 iDivisor) {.
10369 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 u32 bin = i/p->i
1036a 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d Divisor;. i =
1036b 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a i%p->iDivisor;.
1036c 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 if( p->u.apS
1036d 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a 20 20 ub[bin]==0 ){.
1036e 20 20 20 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 p->u.apSub[b
1036f 69 6e 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 74 in] = sqlite3Bit
10370 76 65 63 43 72 65 61 74 65 28 20 70 2d 3e 69 44 vecCreate( p->iD
10371 69 76 69 73 6f 72 20 29 3b 0a 20 20 20 20 20 20 ivisor );.
10372 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 if( p->u.apSub[b
10373 69 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 in]==0 ) return
10374 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
10375 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 }. p = p->u
10376 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 7d .apSub[bin];. }
10377 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c . if( p->iSize<
10378 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a =BITVEC_NBIT ){.
10379 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 p->u.aBitmap
1037a 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d [i/BITVEC_SZELEM
1037b 5d 20 7c 3d 20 31 20 3c 3c 20 28 69 26 28 42 49 ] |= 1 << (i&(BI
1037c 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 3b TVEC_SZELEM-1));
1037d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1037e 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 20 3d TE_OK;. }. h =
1037f 20 42 49 54 56 45 43 5f 48 41 53 48 28 69 2b 2b BITVEC_HASH(i++
10380 29 3b 0a 20 20 2f 2a 20 69 66 20 74 68 65 72 65 );. /* if there
10381 20 77 61 73 6e 27 74 20 61 20 68 61 73 68 20 63 wasn't a hash c
10382 6f 6c 6c 69 73 69 6f 6e 2c 20 61 6e 64 20 74 68 ollision, and th
10383 69 73 20 64 6f 65 73 6e 27 74 20 2a 2f 0a 20 20 is doesn't */.
10384 2f 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20 66 69 /* completely fi
10385 6c 6c 20 74 68 65 20 68 61 73 68 2c 20 74 68 65 ll the hash, the
10386 6e 20 6a 75 73 74 20 61 64 64 20 69 74 20 77 69 n just add it wi
10387 74 68 6f 75 74 20 2a 2f 0a 20 20 2f 2a 20 77 6f thout */. /* wo
10388 72 72 69 6e 67 20 61 62 6f 75 74 20 73 75 62 2d rring about sub-
10389 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d dividing and re-
1038a 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 hashing. */. if
1038b 28 20 21 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d ( !p->u.aHash[h]
1038c 20 29 7b 0a 20 20 20 20 69 66 20 28 70 2d 3e 6e ){. if (p->n
1038d 53 65 74 3c 28 42 49 54 56 45 43 5f 4e 49 4e 54 Set<(BITVEC_NINT
1038e 2d 31 29 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 -1)) {. got
1038f 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 o bitvec_set_end
10390 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 ;. } else {.
10391 20 20 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 goto bitvec
10392 5f 73 65 74 5f 72 65 68 61 73 68 3b 0a 20 20 20 _set_rehash;.
10393 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 74 68 65 72 }. }. /* ther
10394 65 20 77 61 73 20 61 20 63 6f 6c 6c 69 73 69 6f e was a collisio
10395 6e 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 n, check to see
10396 69 66 20 69 74 27 73 20 61 6c 72 65 61 64 79 20 if it's already
10397 2a 2f 0a 20 20 2f 2a 20 69 6e 20 68 61 73 68 2c */. /* in hash,
10398 20 69 66 20 6e 6f 74 2c 20 74 72 79 20 74 6f 20 if not, try to
10399 66 69 6e 64 20 61 20 73 70 6f 74 20 66 6f 72 20 find a spot for
1039a 69 74 20 2a 2f 0a 20 20 64 6f 20 7b 0a 20 20 20 it */. do {.
1039b 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b if( p->u.aHash[
1039c 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 53 h]==i ) return S
1039d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 68 2b QLITE_OK;. h+
1039e 2b 3b 0a 20 20 20 20 69 66 28 20 68 3e 3d 42 49 +;. if( h>=BI
1039f 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 TVEC_NINT ) h =
103a0 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 70 2d 0;. } while( p-
103a1 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 3b 0a 20 >u.aHash[h] );.
103a2 20 2f 2a 20 77 65 20 64 69 64 6e 27 74 20 66 69 /* we didn't fi
103a3 6e 64 20 69 74 20 69 6e 20 74 68 65 20 68 61 73 nd it in the has
103a4 68 2e 20 20 68 20 70 6f 69 6e 74 73 20 74 6f 20 h. h points to
103a5 74 68 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 2f the first */. /
103a6 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 72 65 65 * available free
103a7 20 73 70 6f 74 2e 20 63 68 65 63 6b 20 74 6f 20 spot. check to
103a8 73 65 65 20 69 66 20 74 68 69 73 20 69 73 20 67 see if this is g
103a9 6f 69 6e 67 20 74 6f 20 2a 2f 0a 20 20 2f 2a 20 oing to */. /*
103aa 6d 61 6b 65 20 6f 75 72 20 68 61 73 68 20 74 6f make our hash to
103ab 6f 20 22 66 75 6c 6c 22 2e 20 20 2a 2f 0a 62 69 o "full". */.bi
103ac 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3a tvec_set_rehash:
103ad 0a 20 20 69 66 28 20 70 2d 3e 6e 53 65 74 3e 3d . if( p->nSet>=
103ae 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 29 7b BITVEC_MXHASH ){
103af 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e . unsigned in
103b0 74 20 6a 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b t j;. int rc;
103b1 0a 20 20 20 20 75 33 32 20 61 69 56 61 6c 75 65 . u32 aiValue
103b2 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 0a s[BITVEC_NINT];.
103b3 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c memcpy(aiVal
103b4 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c ues, p->u.aHash,
103b5 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 sizeof(aiValues
103b6 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 ));. memset(p
103b7 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20 73 69 ->u.apSub, 0, si
103b8 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29 29 3b zeof(aiValues));
103b9 0a 20 20 20 20 70 2d 3e 69 44 69 76 69 73 6f 72 . p->iDivisor
103ba 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b 20 42 = (p->iSize + B
103bb 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 29 2f ITVEC_NPTR - 1)/
103bc 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 20 20 BITVEC_NPTR;.
103bd 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 rc = sqlite3Bit
103be 76 65 63 53 65 74 28 70 2c 20 69 29 3b 0a 20 20 vecSet(p, i);.
103bf 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54 for(j=0; j<BIT
103c0 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a VEC_NINT; j++){.
103c1 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c 75 if( aiValu
103c2 65 73 5b 6a 5d 20 29 20 72 63 20 7c 3d 20 73 71 es[j] ) rc |= sq
103c3 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 lite3BitvecSet(p
103c4 2c 20 61 69 56 61 6c 75 65 73 5b 6a 5d 29 3b 0a , aiValues[j]);.
103c5 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
103c6 20 72 63 3b 0a 20 20 7d 0a 62 69 74 76 65 63 5f rc;. }.bitvec_
103c7 73 65 74 5f 65 6e 64 3a 0a 20 20 70 2d 3e 6e 53 set_end:. p->nS
103c8 65 74 2b 2b 3b 0a 20 20 70 2d 3e 75 2e 61 48 61 et++;. p->u.aHa
103c9 73 68 5b 68 5d 20 3d 20 69 3b 0a 20 20 72 65 74 sh[h] = i;. ret
103ca 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
103cb 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 ../*.** Clear th
103cc 65 20 69 2d 74 68 20 62 69 74 2e 0a 2a 2f 0a 53 e i-th bit..*/.S
103cd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
103ce 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 id sqlite3Bitvec
103cf 43 6c 65 61 72 28 42 69 74 76 65 63 20 2a 70 2c Clear(Bitvec *p,
103d0 20 75 33 32 20 69 29 7b 0a 20 20 61 73 73 65 72 u32 i){. asser
103d1 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 t( p!=0 );. ass
103d2 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 69 2d ert( i>0 );. i-
103d3 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 -;. while( p->i
103d4 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 Divisor ){. u
103d5 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 32 bin = i/p->iD
103d6 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 ivisor;. i =
103d7 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 i%p->iDivisor;.
103d8 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 p = p->u.apSu
103d9 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 b[bin];. if (
103da 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 !p) {. retu
103db 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rn;. }. }.
103dc 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 if( p->iSize<=BI
103dd 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 TVEC_NBIT ){.
103de 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f p->u.aBitmap[i/
103df 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 BITVEC_SZELEM] &
103e0 3d 20 7e 28 31 20 3c 3c 20 28 69 26 28 42 49 54 = ~(1 << (i&(BIT
103e1 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b VEC_SZELEM-1)));
103e2 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e . }else{. un
103e3 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 signed int j;.
103e4 20 20 75 33 32 20 61 69 56 61 6c 75 65 73 5b 42 u32 aiValues[B
103e5 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 0a 20 20 20 ITVEC_NINT];.
103e6 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 65 73 memcpy(aiValues
103e7 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 73 69 , p->u.aHash, si
103e8 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29 29 3b zeof(aiValues));
103e9 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75 . memset(p->u
103ea 2e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f .aHash, 0, sizeo
103eb 66 28 61 69 56 61 6c 75 65 73 29 29 3b 0a 20 20 f(aiValues));.
103ec 20 20 70 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 p->nSet = 0;.
103ed 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 for(j=0; j<BI
103ee 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b TVEC_NINT; j++){
103ef 0a 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c . if( aiVal
103f0 75 65 73 5b 6a 5d 20 26 26 20 61 69 56 61 6c 75 ues[j] && aiValu
103f1 65 73 5b 6a 5d 21 3d 28 69 2b 31 29 20 29 7b 0a es[j]!=(i+1) ){.
103f2 20 20 20 20 20 20 20 20 75 33 32 20 68 20 3d 20 u32 h =
103f3 42 49 54 56 45 43 5f 48 41 53 48 28 61 69 56 61 BITVEC_HASH(aiVa
103f4 6c 75 65 73 5b 6a 5d 2d 31 29 3b 0a 20 20 20 20 lues[j]-1);.
103f5 20 20 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 p->nSet++;.
103f6 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d while( p-
103f7 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 >u.aHash[h] ){.
103f8 20 20 20 20 20 20 20 20 20 68 2b 2b 3b 0a 20 20 h++;.
103f9 20 20 20 20 20 20 20 20 69 66 28 20 68 3e 3d 42 if( h>=B
103fa 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d ITVEC_NINT ) h =
103fb 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0;. }.
103fc 20 20 20 20 20 20 70 2d 3e 75 2e 61 48 61 73 68 p->u.aHash
103fd 5b 68 5d 20 3d 20 61 69 56 61 6c 75 65 73 5b 6a [h] = aiValues[j
103fe 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d ];. }. }
103ff 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 . }.}../*.** De
10400 73 74 72 6f 79 20 61 20 62 69 74 6d 61 70 20 6f stroy a bitmap o
10401 62 6a 65 63 74 2e 20 20 52 65 63 6c 61 69 6d 20 bject. Reclaim
10402 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 2e all memory used.
10403 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
10404 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
10405 69 74 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 itvecDestroy(Bit
10406 76 65 63 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 vec *p){. if( p
10407 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
10408 69 66 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 if( p->iDivisor
10409 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 ){. unsigned
1040a 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 int i;. for(i
1040b 3d 30 3b 20 69 3c 42 49 54 56 45 43 5f 4e 50 54 =0; i<BITVEC_NPT
1040c 52 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 R; i++){. s
1040d 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 qlite3BitvecDest
1040e 72 6f 79 28 70 2d 3e 75 2e 61 70 53 75 62 5b 69 roy(p->u.apSub[i
1040f 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ]);. }. }.
10410 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
10411 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
10412 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f TE_OMIT_BUILTIN_
10413 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56 TEST./*.** Let V
10414 5b 5d 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f [] be an array o
10415 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 f unsigned chara
10416 63 74 65 72 73 20 73 75 66 66 69 63 69 65 6e 74 cters sufficient
10417 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74 to hold.** up t
10418 6f 20 4e 20 62 69 74 73 2e 20 20 4c 65 74 20 49 o N bits. Let I
10419 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62 be an integer b
1041a 65 74 77 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20 etween 0 and N.
1041b 20 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 0<=I<N..** Then
1041c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d the following m
1041d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 acros can be use
1041e 64 20 74 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c d to set, clear,
1041f 20 6f 72 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69 or test.** indi
10420 76 69 64 75 61 6c 20 62 69 74 73 20 77 69 74 68 vidual bits with
10421 69 6e 20 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 in V..*/.#define
10422 20 53 45 54 42 49 54 28 56 2c 49 29 20 20 20 20 SETBIT(V,I)
10423 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c V[I>>3] |= (1<
10424 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20 <(I&7)).#define
10425 43 4c 45 41 52 42 49 54 28 56 2c 49 29 20 20 20 CLEARBIT(V,I)
10426 20 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c V[I>>3] &= ~(1<
10427 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20 <(I&7)).#define
10428 54 45 53 54 42 49 54 28 56 2c 49 29 20 20 20 20 TESTBIT(V,I)
10429 20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49 (V[I>>3]&(1<<(I
1042a 26 37 29 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 &7)))!=0../*.**
1042b 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e This routine run
1042c 73 20 61 6e 20 65 78 74 65 6e 73 69 76 65 20 74 s an extensive t
1042d 65 73 74 20 6f 66 20 74 68 65 20 42 69 74 76 65 est of the Bitve
1042e 63 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 c code..**.** Th
1042f 65 20 69 6e 70 75 74 20 69 73 20 61 6e 20 61 72 e input is an ar
10430 72 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20 ray of integers
10431 74 68 61 74 20 61 63 74 73 20 61 73 20 61 20 70 that acts as a p
10432 72 6f 67 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73 rogram.** to tes
10433 74 20 74 68 65 20 42 69 74 76 65 63 2e 20 20 54 t the Bitvec. T
10434 68 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 he integers are
10435 6f 70 63 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64 opcodes followed
10436 0a 2a 2a 20 62 79 20 30 2c 20 31 2c 20 6f 72 20 .** by 0, 1, or
10437 33 20 6f 70 65 72 61 6e 64 73 2c 20 64 65 70 65 3 operands, depe
10438 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63 nding on the opc
10439 6f 64 65 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a ode. Another.**
1043a 20 6f 70 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 opcode follows
1043b 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 immediately afte
1043c 72 20 74 68 65 20 6c 61 73 74 20 6f 70 65 72 61 r the last opera
1043d 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 nd..**.** There
1043e 61 72 65 20 36 20 6f 70 63 6f 64 65 73 20 6e 75 are 6 opcodes nu
1043f 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 20 74 68 mbered from 0 th
10440 72 6f 75 67 68 20 35 2e 20 20 30 20 69 73 20 74 rough 5. 0 is t
10441 68 65 0a 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63 he.** "halt" opc
10442 6f 64 65 20 61 6e 64 20 63 61 75 73 65 73 20 74 ode and causes t
10443 68 65 20 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a he test to end..
10444 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 **.** 0
10445 20 20 20 20 48 61 6c 74 20 61 6e 64 20 72 65 74 Halt and ret
10446 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
10447 66 20 65 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31 f errors.** 1
10448 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20 N S X Set N
10449 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 bits beginning w
1044a 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d ith S and increm
1044b 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 enting by X.**
1044c 20 20 32 20 4e 20 53 20 58 20 20 20 20 43 6c 65 2 N S X Cle
1044d 61 72 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e ar N bits beginn
1044e 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 69 ing with S and i
1044f 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 ncrementing by X
10450 0a 2a 2a 20 20 20 20 33 20 4e 20 20 20 20 20 20 .** 3 N
10451 20 20 53 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79 Set N randomly
10452 20 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 chosen bits.**
10453 20 20 20 34 20 4e 20 20 20 20 20 20 20 20 43 6c 4 N Cl
10454 65 61 72 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 ear N randomly c
10455 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 hosen bits.**
10456 20 35 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 5 N S X Set
10457 4e 20 62 69 74 73 20 66 72 6f 6d 20 53 20 69 6e N bits from S in
10458 63 72 65 6d 65 6e 74 20 58 20 69 6e 20 61 72 72 crement X in arr
10459 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20 ay only, not in
1045a 62 69 74 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65 bitvec.**.** The
1045b 20 6f 70 63 6f 64 65 73 20 31 20 74 68 72 6f 75 opcodes 1 throu
1045c 67 68 20 34 20 70 65 72 66 6f 72 6d 20 73 65 74 gh 4 perform set
1045d 20 61 6e 64 20 63 6c 65 61 72 20 6f 70 65 72 61 and clear opera
1045e 74 69 6f 6e 73 20 61 72 65 20 70 65 72 66 6f 72 tions are perfor
1045f 6d 65 64 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61 med.** on both a
10460 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 61 Bitvec object a
10461 6e 64 20 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61 nd on a linear a
10462 72 72 61 79 20 6f 66 20 62 69 74 73 20 6f 62 74 rray of bits obt
10463 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
10464 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77 c..** Opcode 5 w
10465 6f 72 6b 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65 orks on the line
10466 61 72 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e ar array only, n
10467 6f 74 20 6f 6e 20 74 68 65 20 42 69 74 76 65 63 ot on the Bitvec
10468 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 69 73 ..** Opcode 5 is
10469 20 75 73 65 64 20 74 6f 20 64 65 6c 69 62 65 72 used to deliber
1046a 61 74 65 6c 79 20 69 6e 64 75 63 65 20 61 20 66 ately induce a f
1046b 61 75 6c 74 20 69 6e 20 6f 72 64 65 72 20 74 6f ault in order to
1046c 0a 2a 2a 20 63 6f 6e 66 69 72 6d 20 74 68 61 74 .** confirm that
1046d 20 65 72 72 6f 72 20 64 65 74 65 63 74 69 6f 6e error detection
1046e 20 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 works..**.** At
1046f 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 the conclusion
10470 6f 66 20 74 68 65 20 74 65 73 74 20 74 68 65 20 of the test the
10471 6c 69 6e 65 61 72 20 61 72 72 61 79 20 69 73 20 linear array is
10472 63 6f 6d 70 61 72 65 64 0a 2a 2a 20 61 67 61 69 compared.** agai
10473 6e 73 74 20 74 68 65 20 42 69 74 76 65 63 20 6f nst the Bitvec o
10474 62 6a 65 63 74 2e 20 20 49 66 20 74 68 65 72 65 bject. If there
10475 20 61 72 65 20 61 6e 79 20 64 69 66 66 65 72 65 are any differe
10476 6e 63 65 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f nces,.** an erro
10477 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 r is returned.
10478 49 66 20 74 68 65 79 20 61 72 65 20 74 68 65 20 If they are the
10479 73 61 6d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 same, zero is re
1047a 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
1047b 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
1047c 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 tion error occur
1047d 73 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f s, return -1..*/
1047e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1047f 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 int sqlite3Bitve
10480 63 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 cBuiltinTest(int
10481 20 73 7a 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a sz, int *aOp){.
10482 20 20 42 69 74 76 65 63 20 2a 70 42 69 74 76 65 Bitvec *pBitve
10483 63 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 c = 0;. unsigne
10484 64 20 63 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a d char *pV = 0;.
10485 20 20 69 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20 int rc = -1;.
10486 20 69 6e 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20 int i, nx, pc,
10487 6f 70 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 op;.. /* Alloca
10488 74 65 20 74 68 65 20 42 69 74 76 65 63 20 74 6f te the Bitvec to
10489 20 62 65 20 74 65 73 74 65 64 20 61 6e 64 20 61 be tested and a
1048a 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66 linear array of
1048b 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f 20 61 63 . ** bits to ac
1048c 74 20 61 73 20 74 68 65 20 72 65 66 65 72 65 6e t as the referen
1048d 63 65 20 2a 2f 0a 20 20 70 42 69 74 76 65 63 20 ce */. pBitvec
1048e 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 = sqlite3BitvecC
1048f 72 65 61 74 65 28 20 73 7a 20 29 3b 0a 20 20 70 reate( sz );. p
10490 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c V = sqlite3_mall
10491 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 oc( (sz+7)/8 + 1
10492 20 29 3b 0a 20 20 69 66 28 20 70 42 69 74 76 65 );. if( pBitve
10493 63 3d 3d 30 20 7c 7c 20 70 56 3d 3d 30 20 29 20 c==0 || pV==0 )
10494 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b goto bitvec_end;
10495 0a 20 20 6d 65 6d 73 65 74 28 70 56 2c 20 30 2c . memset(pV, 0,
10496 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 29 3b 0a (sz+7)/8 + 1);.
10497 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 70 72 . /* Run the pr
10498 6f 67 72 61 6d 20 2a 2f 0a 20 20 70 63 20 3d 20 ogram */. pc =
10499 30 3b 0a 20 20 77 68 69 6c 65 28 20 28 6f 70 20 0;. while( (op
1049a 3d 20 61 4f 70 5b 70 63 5d 29 21 3d 30 20 29 7b = aOp[pc])!=0 ){
1049b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 70 20 . switch( op
1049c 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 31 3a ){. case 1:
1049d 0a 20 20 20 20 20 20 63 61 73 65 20 32 3a 0a 20 . case 2:.
1049e 20 20 20 20 20 63 61 73 65 20 35 3a 20 7b 0a 20 case 5: {.
1049f 20 20 20 20 20 20 20 6e 78 20 3d 20 34 3b 0a 20 nx = 4;.
104a0 20 20 20 20 20 20 20 69 20 3d 20 61 4f 70 5b 70 i = aOp[p
104a1 63 2b 32 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 c+2] - 1;.
104a2 20 20 61 4f 70 5b 70 63 2b 32 5d 20 2b 3d 20 61 aOp[pc+2] += a
104a3 4f 70 5b 70 63 2b 33 5d 3b 0a 20 20 20 20 20 20 Op[pc+3];.
104a4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
104a5 0a 20 20 20 20 20 20 63 61 73 65 20 33 3a 0a 20 . case 3:.
104a6 20 20 20 20 20 63 61 73 65 20 34 3a 20 0a 20 20 case 4: .
104a7 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
104a8 20 20 20 20 20 20 20 6e 78 20 3d 20 32 3b 0a 20 nx = 2;.
104a9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 sqlite3_r
104aa 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 andomness(sizeof
104ab 28 69 29 2c 20 26 69 29 3b 0a 20 20 20 20 20 20 (i), &i);.
104ac 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
104ad 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 . }. if( (
104ae 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29 20 3e 20 30 --aOp[pc+1]) > 0
104af 20 29 20 6e 78 20 3d 20 30 3b 0a 20 20 20 20 70 ) nx = 0;. p
104b0 63 20 2b 3d 20 6e 78 3b 0a 20 20 20 20 69 20 3d c += nx;. i =
104b1 20 28 69 20 26 20 30 78 37 66 66 66 66 66 66 66 (i & 0x7fffffff
104b2 29 25 73 7a 3b 0a 20 20 20 20 69 66 28 20 28 6f )%sz;. if( (o
104b3 70 20 26 20 31 29 21 3d 30 20 29 7b 0a 20 20 20 p & 1)!=0 ){.
104b4 20 20 20 53 45 54 42 49 54 28 70 56 2c 20 28 69 SETBIT(pV, (i
104b5 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 +1));. if(
104b6 6f 70 21 3d 35 20 29 7b 0a 20 20 20 20 20 20 20 op!=5 ){.
104b7 20 69 66 28 20 73 71 6c 69 74 65 33 42 69 74 76 if( sqlite3Bitv
104b8 65 63 53 65 74 28 70 42 69 74 76 65 63 2c 20 69 ecSet(pBitvec, i
104b9 2b 31 29 20 29 20 67 6f 74 6f 20 62 69 74 76 65 +1) ) goto bitve
104ba 63 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 c_end;. }.
104bb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
104bc 43 4c 45 41 52 42 49 54 28 70 56 2c 20 28 69 2b CLEARBIT(pV, (i+
104bd 31 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 1));. sqlit
104be 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 70 42 e3BitvecClear(pB
104bf 69 74 76 65 63 2c 20 69 2b 31 29 3b 0a 20 20 20 itvec, i+1);.
104c0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 }. }.. /* Tes
104c1 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 t to make sure t
104c2 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 he linear array
104c3 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 exactly matches
104c4 74 68 65 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 the. ** Bitvec
104c5 6f 62 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77 object. Start w
104c6 69 74 68 20 74 68 65 20 61 73 73 75 6d 70 74 69 ith the assumpti
104c7 6f 6e 20 74 68 61 74 20 74 68 65 79 20 64 6f 0a on that they do.
104c8 20 20 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d ** match (rc==
104c9 30 29 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74 0). Change rc t
104ca 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 o non-zero if a
104cb 64 69 73 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a discrepancy. **
104cc 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a is found.. */.
104cd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 rc = sqlite3Bi
104ce 74 76 65 63 54 65 73 74 28 30 2c 30 29 20 2b 20 tvecTest(0,0) +
104cf 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 sqlite3BitvecTes
104d0 74 28 70 42 69 74 76 65 63 2c 20 73 7a 2b 31 29 t(pBitvec, sz+1)
104d1 0a 20 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c . + sql
104d2 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
104d3 42 69 74 76 65 63 2c 20 30 29 3b 0a 20 20 66 6f Bitvec, 0);. fo
104d4 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b r(i=1; i<=sz; i+
104d5 2b 29 7b 0a 20 20 20 20 69 66 28 20 20 28 54 45 +){. if( (TE
104d6 53 54 42 49 54 28 70 56 2c 69 29 29 21 3d 73 71 STBIT(pV,i))!=sq
104d7 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
104d8 70 42 69 74 76 65 63 2c 69 29 20 29 7b 0a 20 20 pBitvec,i) ){.
104d9 20 20 20 20 72 63 20 3d 20 69 3b 0a 20 20 20 20 rc = i;.
104da 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
104db 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c }.. /* Free al
104dc 6c 6f 63 61 74 65 64 20 73 74 72 75 63 74 75 72 located structur
104dd 65 20 2a 2f 0a 62 69 74 76 65 63 5f 65 6e 64 3a e */.bitvec_end:
104de 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
104df 70 56 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 pV);. sqlite3Bi
104e0 74 76 65 63 44 65 73 74 72 6f 79 28 70 42 69 74 tvecDestroy(pBit
104e1 76 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 vec);. return r
104e2 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 c;.}.#endif /* S
104e3 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 QLITE_OMIT_BUILT
104e4 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a IN_TEST */../***
104e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
104e6 6f 66 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a of bitvec.c ****
104e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
104e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
104e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
104ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
104eb 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 63 20 n file pcache.c
104ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
104ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
104ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
104ef 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30 35 * 2008 August 05
104f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
104f1 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
104f2 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
104f3 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
104f4 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
104f5 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
104f6 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
104f7 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
104f8 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
104f9 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
104fa 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
104fb 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
104fc 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
104fd 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
104fe 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
104ff 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
10500 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
10501 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
10502 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10503 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10504 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10505 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
10506 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 ** This file imp
10507 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 70 61 67 lements that pag
10508 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 40 e cache..**.** @
10509 28 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 2e (#) $Id: pcache.
1050a 63 2c 76 20 31 2e 33 39 20 32 30 30 38 2f 31 32 c,v 1.39 2008/12
1050b 2f 30 34 20 32 30 3a 34 30 3a 31 30 20 64 72 68 /04 20:40:10 drh
1050c 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
1050d 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65 A complete page
1050e 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 cache is an ins
1050f 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
10510 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 ructure..*/.stru
10511 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67 ct PCache {. Pg
10512 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 Hdr *pDirty, *pD
10513 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 irtyTail;
10514 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 /* List of dir
10515 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 ty pages in LRU
10516 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 order */. PgHdr
10517 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 *pSynced;
10518 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10519 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 * Last synced pa
1051a 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 ge in dirty page
1051b 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e list */. int n
1051c 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
1051d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1051e 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 * Number of refe
1051f 72 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a renced pages */.
10520 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 int nMax;
10521 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10522 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 /* Configu
10523 72 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a red cache size *
10524 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e 3b 20 20 20 /. int nMin;
10525 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10526 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 /* Confi
10527 67 75 72 65 64 20 6d 69 6e 69 6d 75 6d 20 63 61 gured minimum ca
10528 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e che size */. in
10529 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 t szPage;
1052a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1052b 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 /* Size of eve
1052c 72 79 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 ry page in this
1052d 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 73 cache */. int s
1052e 7a 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 zExtra;
1052f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10530 2a 20 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20 * Size of extra
10531 73 70 61 63 65 20 66 6f 72 20 65 61 63 68 20 70 space for each p
10532 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 age */. int bPu
10533 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20 rgeable;
10534 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10535 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 True if pages ar
10536 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f e on backing sto
10537 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 re */. int (*xS
10538 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48 tress)(void*,PgH
10539 64 72 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43 dr*); /* C
1053a 61 6c 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20 all to try make
1053b 61 20 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a a page clean */.
1053c 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b void *pStress;
1053d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1053e 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
1053f 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a t to xStress */.
10540 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 sqlite3_pcache
10541 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 *pCache;
10542 20 20 20 20 20 20 2f 2a 20 50 6c 75 67 67 61 62 /* Pluggab
10543 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 le cache module
10544 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67 */. PgHdr *pPag
10545 65 31 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 6f e1;.};../*.** So
10546 6d 65 20 6f 66 20 74 68 65 20 61 73 73 65 72 74 me of the assert
10547 28 29 20 6d 61 63 72 6f 73 20 69 6e 20 74 68 69 () macros in thi
10548 73 20 63 6f 64 65 20 61 72 65 20 74 6f 6f 20 65 s code are too e
10549 78 70 65 6e 73 69 76 65 20 74 6f 20 72 75 6e 0a xpensive to run.
1054a 2a 2a 20 65 76 65 6e 20 64 75 72 69 6e 67 20 6e ** even during n
1054b 6f 72 6d 61 6c 20 64 65 62 75 67 67 69 6e 67 2e ormal debugging.
1054c 20 20 55 73 65 20 74 68 65 6d 20 6f 6e 6c 79 20 Use them only
1054d 72 61 72 65 6c 79 20 6f 6e 20 6c 6f 6e 67 2d 72 rarely on long-r
1054e 75 6e 6e 69 6e 67 0a 2a 2a 20 74 65 73 74 73 2e unning.** tests.
1054f 20 20 45 6e 61 62 6c 65 20 74 68 65 20 65 78 70 Enable the exp
10550 65 6e 73 69 76 65 20 61 73 73 65 72 74 73 20 75 ensive asserts u
10551 73 69 6e 67 20 74 68 65 0a 2a 2a 20 2d 44 53 51 sing the.** -DSQ
10552 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 LITE_ENABLE_EXPE
10553 4e 53 49 56 45 5f 41 53 53 45 52 54 3d 31 20 63 NSIVE_ASSERT=1 c
10554 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 ompile-time opti
10555 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 on..*/.#ifdef SQ
10556 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 LITE_ENABLE_EXPE
10557 4e 53 49 56 45 5f 41 53 53 45 52 54 0a 23 20 64 NSIVE_ASSERT.# d
10558 65 66 69 6e 65 20 65 78 70 65 6e 73 69 76 65 5f efine expensive_
10559 61 73 73 65 72 74 28 58 29 20 20 61 73 73 65 72 assert(X) asser
1055a 74 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 t(X).#else.# def
1055b 69 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 ine expensive_as
1055c 73 65 72 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a sert(X).#endif..
1055d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
1055e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1055f 2a 2a 2a 20 4c 69 6e 6b 65 64 20 4c 69 73 74 20 *** Linked List
10560 4d 61 6e 61 67 65 6d 65 6e 74 20 2a 2a 2a 2a 2a Management *****
10561 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
10562 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e ..#if !defined(N
10563 44 45 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 DEBUG) && define
10564 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
10565 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 EXPENSIVE_ASSERT
10566 29 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 )./*.** Check th
10567 61 74 20 74 68 65 20 70 43 61 63 68 65 2d 3e 70 at the pCache->p
10568 53 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 Synced variable
10569 69 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 is set correctly
1056a 2e 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f . If it.** is no
1056b 74 2c 20 65 69 74 68 65 72 20 66 61 69 6c 20 61 t, either fail a
1056c 6e 20 61 73 73 65 72 74 20 6f 72 20 72 65 74 75 n assert or retu
1056d 72 6e 20 7a 65 72 6f 2e 20 4f 74 68 65 72 77 69 rn zero. Otherwi
1056e 73 65 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f se, return.** no
1056f 6e 2d 7a 65 72 6f 2e 20 54 68 69 73 20 69 73 20 n-zero. This is
10570 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 64 65 62 only used in deb
10571 75 67 67 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 ugging builds, a
10572 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
10573 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 expensive_ass
10574 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b ert( pcacheCheck
10575 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 Synced(pCache) )
10576 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ;.*/.static int
10577 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 pcacheCheckSynce
10578 64 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 d(PCache *pCache
10579 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 ){. PgHdr *p;.
1057a 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 for(p=pCache->p
1057b 44 69 72 74 79 54 61 69 6c 3b 20 70 21 3d 70 43 DirtyTail; p!=pC
1057c 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 70 ache->pSynced; p
1057d 3d 70 2d 3e 70 44 69 72 74 79 50 72 65 76 29 7b =p->pDirtyPrev){
1057e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
1057f 6e 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 nRef || (p->flag
10580 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
10581 43 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 C) );. }. retu
10582 72 6e 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e rn (p==0 || p->n
10583 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 Ref || (p->flags
10584 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
10585 29 3d 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 )==0);.}.#endif
10586 2f 2a 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 /* !NDEBUG && SQ
10587 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 LITE_ENABLE_EXPE
10588 4e 53 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a NSIVE_ASSERT */.
10589 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 70 61 ./*.** Remove pa
1058a 67 65 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 ge pPage from th
1058b 65 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 e list of dirty
1058c 70 61 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 pages..*/.static
1058d 20 76 6f 69 64 20 70 63 61 63 68 65 52 65 6d 6f void pcacheRemo
1058e 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 veFromDirtyList(
1058f 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 PgHdr *pPage){.
10590 20 50 43 61 63 68 65 20 2a 70 20 3d 20 70 50 61 PCache *p = pPa
10591 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 ge->pCache;.. a
10592 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 ssert( pPage->pD
10593 69 72 74 79 4e 65 78 74 20 7c 7c 20 70 50 61 67 irtyNext || pPag
10594 65 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c e==p->pDirtyTail
10595 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
10596 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 age->pDirtyPrev
10597 7c 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 || pPage==p->pDi
10598 72 74 79 20 29 3b 0a 0a 20 20 2f 2a 20 55 70 64 rty );.. /* Upd
10599 61 74 65 20 74 68 65 20 50 43 61 63 68 65 31 2e ate the PCache1.
1059a 70 53 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 pSynced variable
1059b 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 2a if necessary. *
1059c 2f 0a 20 20 69 66 28 20 70 2d 3e 70 53 79 6e 63 /. if( p->pSync
1059d 65 64 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 ed==pPage ){.
1059e 20 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 20 PgHdr *pSynced
1059f 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 = pPage->pDirtyP
105a0 72 65 76 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 rev;. while(
105a1 70 53 79 6e 63 65 64 20 26 26 20 28 70 53 79 6e pSynced && (pSyn
105a2 63 65 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 ced->flags&PGHDR
105a3 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 _NEED_SYNC) ){.
105a4 20 20 20 20 20 70 53 79 6e 63 65 64 20 3d 20 70 pSynced = p
105a5 53 79 6e 63 65 64 2d 3e 70 44 69 72 74 79 50 72 Synced->pDirtyPr
105a6 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d ev;. }. p-
105a7 3e 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 >pSynced = pSync
105a8 65 64 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 ed;. }.. if( p
105a9 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
105aa 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 ){. pPage->p
105ab 44 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 DirtyNext->pDirt
105ac 79 50 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 yPrev = pPage->p
105ad 44 69 72 74 79 50 72 65 76 3b 0a 20 20 7d 65 6c DirtyPrev;. }el
105ae 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
105af 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 pPage==p->pDirty
105b0 54 61 69 6c 20 29 3b 0a 20 20 20 20 70 2d 3e 70 Tail );. p->p
105b1 44 69 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 DirtyTail = pPag
105b2 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 e->pDirtyPrev;.
105b3 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e }. if( pPage->
105b4 70 44 69 72 74 79 50 72 65 76 20 29 7b 0a 20 20 pDirtyPrev ){.
105b5 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 pPage->pDirtyP
105b6 72 65 76 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 rev->pDirtyNext
105b7 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e = pPage->pDirtyN
105b8 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ext;. }else{.
105b9 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 3d assert( pPage=
105ba 3d 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 20 20 =p->pDirty );.
105bb 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 p->pDirty = pP
105bc 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b age->pDirtyNext;
105bd 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 70 44 . }. pPage->pD
105be 69 72 74 79 4e 65 78 74 20 3d 20 30 3b 0a 20 20 irtyNext = 0;.
105bf 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 pPage->pDirtyPre
105c0 76 20 3d 20 30 3b 0a 0a 20 20 65 78 70 65 6e 73 v = 0;.. expens
105c1 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 ive_assert( pcac
105c2 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 heCheckSynced(p)
105c3 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 );.}../*.** Add
105c4 20 70 61 67 65 20 70 50 61 67 65 20 74 6f 20 74 page pPage to t
105c5 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 he head of the d
105c6 69 72 74 79 20 6c 69 73 74 20 28 50 43 61 63 68 irty list (PCach
105c7 65 31 2e 70 44 69 72 74 79 20 69 73 20 73 65 74 e1.pDirty is set
105c8 20 74 6f 0a 2a 2a 20 70 50 61 67 65 29 2e 0a 2a to.** pPage)..*
105c9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
105ca 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 acheAddToDirtyLi
105cb 73 74 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 st(PgHdr *pPage)
105cc 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 20 3d 20 {. PCache *p =
105cd 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a pPage->pCache;..
105ce 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
105cf 3e 70 44 69 72 74 79 4e 65 78 74 3d 3d 30 20 26 >pDirtyNext==0 &
105d0 26 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 & pPage->pDirtyP
105d1 72 65 76 3d 3d 30 20 26 26 20 70 2d 3e 70 44 69 rev==0 && p->pDi
105d2 72 74 79 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20 rty!=pPage );..
105d3 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 pPage->pDirtyNe
105d4 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a xt = p->pDirty;.
105d5 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 if( pPage->pDi
105d6 72 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 rtyNext ){. a
105d7 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 ssert( pPage->pD
105d8 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 irtyNext->pDirty
105d9 50 72 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 70 Prev==0 );. p
105da 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
105db 2d 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 ->pDirtyPrev = p
105dc 50 61 67 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 Page;. }. p->p
105dd 44 69 72 74 79 20 3d 20 70 50 61 67 65 3b 0a 20 Dirty = pPage;.
105de 20 69 66 28 20 21 70 2d 3e 70 44 69 72 74 79 54 if( !p->pDirtyT
105df 61 69 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 70 44 ail ){. p->pD
105e0 69 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 irtyTail = pPage
105e1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 2d 3e ;. }. if( !p->
105e2 70 53 79 6e 63 65 64 20 26 26 20 30 3d 3d 28 70 pSynced && 0==(p
105e3 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 Page->flags&PGHD
105e4 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a R_NEED_SYNC) ){.
105e5 20 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d p->pSynced =
105e6 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 65 78 pPage;. }. ex
105e7 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 pensive_assert(
105e8 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 pcacheCheckSynce
105e9 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a d(p) );.}../*.**
105ea 20 57 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 Wrapper around
105eb 74 68 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61 the pluggable ca
105ec 63 68 65 73 20 78 55 6e 70 69 6e 20 6d 65 74 68 ches xUnpin meth
105ed 6f 64 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 od. If the cache
105ee 20 69 73 0a 2a 2a 20 62 65 69 6e 67 20 75 73 65 is.** being use
105ef 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f d for an in-memo
105f0 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 69 ry database, thi
105f1 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 s function is a
105f2 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
105f3 20 76 6f 69 64 20 70 63 61 63 68 65 55 6e 70 69 void pcacheUnpi
105f4 6e 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 n(PgHdr *p){. P
105f5 43 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 Cache *pCache =
105f6 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 69 66 28 p->pCache;. if(
105f7 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 pCache->bPurgea
105f8 62 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ble ){. if( p
105f9 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 ->pgno==1 ){.
105fa 20 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 pCache->pPage
105fb 31 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 1 = 0;. }.
105fc 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
105fd 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 nfig.pcache.xUnp
105fe 69 6e 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 in(pCache->pCach
105ff 65 2c 20 70 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a e, p, 0);. }.}.
10600 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
10601 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10602 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10603 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6e ***** General In
10604 74 65 72 66 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a terfaces ******.
10605 2a 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 **.** Initialize
10606 20 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 and shutdown th
10607 65 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 e page cache sub
10608 73 79 73 74 65 6d 2e 20 4e 65 69 74 68 65 72 20 system. Neither
10609 6f 66 20 74 68 65 73 65 20 0a 2a 2a 20 66 75 6e of these .** fun
1060a 63 74 69 6f 6e 73 20 61 72 65 20 74 68 72 65 61 ctions are threa
1060b 64 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 dsafe..*/.SQLITE
1060c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1060d 69 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 ite3PcacheInitia
1060e 6c 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 69 66 lize(void){. if
1060f 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
10610 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e onfig.pcache.xIn
10611 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c it==0 ){. sql
10612 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 ite3PCacheSetDef
10613 61 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 72 65 ault();. }. re
10614 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 turn sqlite3Glob
10615 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
10616 78 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f xInit(sqlite3Glo
10617 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
10618 2e 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 .pArg);.}.SQLITE
10619 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1061a 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74 64 lite3PcacheShutd
1061b 6f 77 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 own(void){. if(
1061c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
1061d 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75 nfig.pcache.xShu
1061e 74 64 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c tdown ){. sql
1061f 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
10620 2e 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 .pcache.xShutdow
10621 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 n(sqlite3GlobalC
10622 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 onfig.pcache.pAr
10623 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a g);. }.}../*.**
10624 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 Return the size
10625 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 in bytes of a P
10626 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f Cache object..*/
10627 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10628 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
10629 65 53 69 7a 65 28 76 6f 69 64 29 7b 20 72 65 74 eSize(void){ ret
1062a 75 72 6e 20 73 69 7a 65 6f 66 28 50 43 61 63 68 urn sizeof(PCach
1062b 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 e); }../*.** Cre
1062c 61 74 65 20 61 20 6e 65 77 20 50 43 61 63 68 65 ate a new PCache
1062d 20 6f 62 6a 65 63 74 2e 20 53 74 6f 72 61 67 65 object. Storage
1062e 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 space to hold t
1062f 68 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 68 61 73 he object.** has
10630 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c already been al
10631 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 73 20 70 located and is p
10632 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 assed in as the
10633 70 20 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20 54 p pointer. .** T
10634 68 65 20 63 61 6c 6c 65 72 20 64 69 73 63 6f 76 he caller discov
10635 65 72 73 20 68 6f 77 20 6d 75 63 68 20 73 70 61 ers how much spa
10636 63 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 ce needs to be a
10637 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 llocated by .**
10638 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 calling sqlite3P
10639 63 61 63 68 65 53 69 7a 65 28 29 2e 0a 2a 2f 0a cacheSize()..*/.
1063a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1063b 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
1063c 65 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 eOpen(. int szP
1063d 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 age,
1063e 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1063f 20 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 every page */.
10640 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 int szExtra,
10641 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10642 20 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73 Extra space ass
10643 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 ociated with eac
10644 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 h page */. int
10645 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 bPurgeable,
10646 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
10647 20 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e if pages are on
10648 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a backing store *
10649 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 /. int (*xStres
1064a 73 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 s)(void*,PgHdr*)
1064b 2c 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 ,/* Call to try
1064c 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c to make pages cl
1064d 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ean */. void *p
1064e 53 74 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 Stress,
1064f 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
10650 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a t to xStress */.
10651 20 20 50 43 61 63 68 65 20 2a 70 20 20 20 20 20 PCache *p
10652 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10653 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 * Preallocated s
10654 70 61 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 pace for the PCa
10655 63 68 65 20 2a 2f 0a 29 7b 0a 20 20 6d 65 6d 73 che */.){. mems
10656 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 et(p, 0, sizeof(
10657 50 43 61 63 68 65 29 29 3b 0a 20 20 70 2d 3e 73 PCache));. p->s
10658 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a zPage = szPage;.
10659 20 20 70 2d 3e 73 7a 45 78 74 72 61 20 3d 20 73 p->szExtra = s
1065a 7a 45 78 74 72 61 3b 0a 20 20 70 2d 3e 62 50 75 zExtra;. p->bPu
1065b 72 67 65 61 62 6c 65 20 3d 20 62 50 75 72 67 65 rgeable = bPurge
1065c 61 62 6c 65 3b 0a 20 20 70 2d 3e 78 53 74 72 65 able;. p->xStre
1065d 73 73 20 3d 20 78 53 74 72 65 73 73 3b 0a 20 20 ss = xStress;.
1065e 70 2d 3e 70 53 74 72 65 73 73 20 3d 20 70 53 74 p->pStress = pSt
1065f 72 65 73 73 3b 0a 20 20 70 2d 3e 6e 4d 61 78 20 ress;. p->nMax
10660 3d 20 31 30 30 3b 0a 20 20 70 2d 3e 6e 4d 69 6e = 100;. p->nMin
10661 20 3d 20 31 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 = 10;.}../*.**
10662 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 Change the page
10663 73 69 7a 65 20 66 6f 72 20 50 43 61 63 68 65 20 size for PCache
10664 6f 62 6a 65 63 74 2e 20 54 68 65 20 63 61 6c 6c object. The call
10665 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 er must ensure t
10666 68 61 74 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 hat there.** are
10667 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 no outstanding
10668 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 page references
10669 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
1066a 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f on is called..*/
1066b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1066c 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
1066d 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 heSetPageSize(PC
1066e 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 69 6e ache *pCache, in
1066f 74 20 73 7a 50 61 67 65 29 7b 0a 20 20 61 73 73 t szPage){. ass
10670 65 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 52 65 ert( pCache->nRe
10671 66 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e f==0 && pCache->
10672 70 44 69 72 74 79 3d 3d 30 20 29 3b 0a 20 20 69 pDirty==0 );. i
10673 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 f( pCache->pCach
10674 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 e ){. sqlite3
10675 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
10676 63 68 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 che.xDestroy(pCa
10677 63 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 che->pCache);.
10678 20 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 pCache->pCache
10679 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 43 61 63 = 0;. }. pCac
1067a 68 65 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 he->szPage = szP
1067b 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 age;.}../*.** Tr
1067c 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 61 y to obtain a pa
1067d 67 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 ge from the cach
1067e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1067f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
10680 50 63 61 63 68 65 46 65 74 63 68 28 0a 20 20 50 PcacheFetch(. P
10681 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 20 Cache *pCache,
10682 20 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 /* Obtain t
10683 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 69 he page from thi
10684 73 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e s cache */. Pgn
10685 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 o pgno,
10686 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
10687 72 20 74 6f 20 6f 62 74 61 69 6e 20 2a 2f 0a 20 r to obtain */.
10688 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c int createFlag,
10689 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 /* If tru
1068a 65 2c 20 63 72 65 61 74 65 20 70 61 67 65 20 69 e, create page i
1068b 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 f it does not ex
1068c 69 73 74 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 ist already */.
1068d 20 50 67 48 64 72 20 2a 2a 70 70 50 61 67 65 20 PgHdr **ppPage
1068e 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
1068f 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f the page here */
10690 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 61 .){. PgHdr *pPa
10691 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 65 43 ge = 0;. int eC
10692 72 65 61 74 65 3b 0a 0a 20 20 61 73 73 65 72 74 reate;.. assert
10693 28 20 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 ( pCache!=0 );.
10694 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e 30 20 assert( pgno>0
10695 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
10696 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 pluggable cache
10697 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
10698 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 ) has not been a
10699 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20 61 llocated,. ** a
1069a 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 0a llocate it now..
1069b 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 61 63 */. if( !pCac
1069c 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 63 72 he->pCache && cr
1069d 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 eateFlag ){.
1069e 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
1069f 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 p;. int nByte
106a0 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70 43 ;. nByte = pC
106a1 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20 70 ache->szPage + p
106a2 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 2b Cache->szExtra +
106a3 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 3b 0a sizeof(PgHdr);.
106a4 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 p = sqlite3G
106a5 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
106a6 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 74 65 he.xCreate(nByte
106a7 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 , pCache->bPurge
106a8 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 21 able);. if( !
106a9 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 p ){. retur
106aa 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
106ab 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
106ac 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
106ad 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 ache.xCachesize(
106ae 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 p, pCache->nMax)
106af 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43 ;. pCache->pC
106b0 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 ache = p;. }..
106b1 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 61 74 eCreate = creat
106b2 65 46 6c 61 67 20 3f 20 31 20 3a 20 30 3b 0a 20 eFlag ? 1 : 0;.
106b3 20 69 66 28 20 65 43 72 65 61 74 65 20 26 26 20 if( eCreate &&
106b4 28 21 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 (!pCache->bPurge
106b5 61 62 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d able || !pCache-
106b6 3e 70 44 69 72 74 79 29 20 29 7b 0a 20 20 20 20 >pDirty) ){.
106b7 65 43 72 65 61 74 65 20 3d 20 32 3b 0a 20 20 7d eCreate = 2;. }
106b8 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 . if( pCache->p
106b9 43 61 63 68 65 20 29 7b 0a 20 20 20 20 70 50 61 Cache ){. pPa
106ba 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 ge = sqlite3Glob
106bb 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
106bc 78 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e 70 xFetch(pCache->p
106bd 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 65 43 72 Cache, pgno, eCr
106be 65 61 74 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 eate);. }.. if
106bf 28 20 21 70 50 61 67 65 20 26 26 20 65 43 72 65 ( !pPage && eCre
106c0 61 74 65 3d 3d 31 20 29 7b 0a 20 20 20 20 50 67 ate==1 ){. Pg
106c1 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 20 20 2f Hdr *pPg;.. /
106c2 2a 20 46 69 6e 64 20 61 20 64 69 72 74 79 20 70 * Find a dirty p
106c3 61 67 65 20 74 6f 20 77 72 69 74 65 2d 6f 75 74 age to write-out
106c4 20 61 6e 64 20 72 65 63 79 63 6c 65 2e 20 46 69 and recycle. Fi
106c5 72 73 74 20 74 72 79 20 74 6f 20 66 69 6e 64 20 rst try to find
106c6 61 20 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 74 a . ** page t
106c7 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 hat does not req
106c8 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 uire a journal-s
106c9 79 6e 63 20 28 6f 6e 65 20 77 69 74 68 20 50 47 ync (one with PG
106ca 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 0a 20 20 HDR_NEED_SYNC.
106cb 20 20 2a 2a 20 63 6c 65 61 72 65 64 29 2c 20 62 ** cleared), b
106cc 75 74 20 69 66 20 74 68 61 74 20 69 73 20 6e 6f ut if that is no
106cd 74 20 70 6f 73 73 69 62 6c 65 20 73 65 74 74 6c t possible settl
106ce 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 e for any other
106cf 0a 20 20 20 20 2a 2a 20 75 6e 72 65 66 65 72 65 . ** unrefere
106d0 6e 63 65 64 20 64 69 72 74 79 20 70 61 67 65 2e nced dirty page.
106d1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 78 70 65 . */. expe
106d2 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 nsive_assert( pc
106d3 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 acheCheckSynced(
106d4 70 43 61 63 68 65 29 20 29 3b 0a 20 20 20 20 66 pCache) );. f
106d5 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d 3e 70 or(pPg=pCache->p
106d6 53 79 6e 63 65 64 3b 20 0a 20 20 20 20 20 20 20 Synced; .
106d7 20 70 50 67 20 26 26 20 28 70 50 67 2d 3e 6e 52 pPg && (pPg->nR
106d8 65 66 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 ef || (pPg->flag
106d9 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
106da 43 29 29 3b 20 0a 20 20 20 20 20 20 20 20 70 50 C)); . pP
106db 67 3d 70 50 67 2d 3e 70 44 69 72 74 79 50 72 65 g=pPg->pDirtyPre
106dc 76 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 v. );. if(
106dd 20 21 70 50 67 20 29 7b 0a 20 20 20 20 20 20 66 !pPg ){. f
106de 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d 3e 70 or(pPg=pCache->p
106df 44 69 72 74 79 54 61 69 6c 3b 20 70 50 67 20 26 DirtyTail; pPg &
106e0 26 20 70 50 67 2d 3e 6e 52 65 66 3b 20 70 50 67 & pPg->nRef; pPg
106e1 3d 70 50 67 2d 3e 70 44 69 72 74 79 50 72 65 76 =pPg->pDirtyPrev
106e2 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
106e3 20 70 50 67 20 29 7b 0a 20 20 20 20 20 20 69 6e pPg ){. in
106e4 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d t rc;. rc =
106e5 20 70 43 61 63 68 65 2d 3e 78 53 74 72 65 73 73 pCache->xStress
106e6 28 70 43 61 63 68 65 2d 3e 70 53 74 72 65 73 73 (pCache->pStress
106e7 2c 20 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66 , pPg);. if
106e8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
106e9 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 && rc!=SQLITE_BU
106ea 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 SY ){. re
106eb 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
106ec 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 . }.. pPag
106ed 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 e = sqlite3Globa
106ee 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 lConfig.pcache.x
106ef 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e 70 43 Fetch(pCache->pC
106f0 61 63 68 65 2c 20 70 67 6e 6f 2c 20 32 29 3b 0a ache, pgno, 2);.
106f1 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 }.. if( pPage
106f2 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 ){. if( 0==p
106f3 50 61 67 65 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 Page->nRef ){.
106f4 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 pCache->nRef
106f5 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 ++;. }. pP
106f6 61 67 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 age->nRef++;.
106f7 20 70 50 61 67 65 2d 3e 70 44 61 74 61 20 3d 20 pPage->pData =
106f8 28 76 6f 69 64 2a 29 26 70 50 61 67 65 5b 31 5d (void*)&pPage[1]
106f9 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78 ;. pPage->pEx
106fa 74 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 tra = (void*)&((
106fb 63 68 61 72 2a 29 70 50 61 67 65 2d 3e 70 44 61 char*)pPage->pDa
106fc 74 61 29 5b 70 43 61 63 68 65 2d 3e 73 7a 50 61 ta)[pCache->szPa
106fd 67 65 5d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e ge];. pPage->
106fe 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 65 3b pCache = pCache;
106ff 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f . pPage->pgno
10700 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 69 66 28 = pgno;. if(
10701 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 pgno==1 ){.
10702 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 pCache->pPage1
10703 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a = pPage;. }.
10704 20 20 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 }. *ppPage =
10705 70 50 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 pPage;. return
10706 28 70 50 61 67 65 3d 3d 30 20 26 26 20 65 43 72 (pPage==0 && eCr
10707 65 61 74 65 29 20 3f 20 53 51 4c 49 54 45 5f 4e eate) ? SQLITE_N
10708 4f 4d 45 4d 20 3a 20 53 51 4c 49 54 45 5f 4f 4b OMEM : SQLITE_OK
10709 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 ;.}../*.** Decre
1070a 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e ment the referen
1070b 63 65 20 63 6f 75 6e 74 20 6f 6e 20 61 20 70 61 ce count on a pa
1070c 67 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 ge. If the page
1070d 69 73 20 63 6c 65 61 6e 20 61 6e 64 20 74 68 65 is clean and the
1070e 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 63 6f .** reference co
1070f 75 6e 74 20 64 72 6f 70 73 20 74 6f 20 30 2c 20 unt drops to 0,
10710 74 68 65 6e 20 69 74 20 69 73 20 6d 61 64 65 20 then it is made
10711 65 6c 69 62 6c 65 20 66 6f 72 20 72 65 63 79 63 elible for recyc
10712 6c 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ling..*/.SQLITE_
10713 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
10714 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 ite3PcacheReleas
10715 65 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 e(PgHdr *p){. a
10716 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 ssert( p->nRef>0
10717 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b );. p->nRef--;
10718 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d . if( p->nRef==
10719 30 20 29 7b 0a 20 20 20 20 50 43 61 63 68 65 20 0 ){. PCache
1071a 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 *pCache = p->pCa
1071b 63 68 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d che;. pCache-
1071c 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 >nRef--;. if(
1071d 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 (p->flags&PGHDR
1071e 5f 44 49 52 54 59 29 3d 3d 30 20 29 7b 0a 20 20 _DIRTY)==0 ){.
1071f 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28 pcacheUnpin(
10720 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 p);. }else{.
10721 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 /* Move the
10722 20 70 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 page to the hea
10723 64 20 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c d of the dirty l
10724 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 63 ist. */. pc
10725 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 acheRemoveFromDi
10726 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 rtyList(p);.
10727 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 pcacheAddToDir
10728 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 7d tyList(p);. }
10729 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e . }.}../*.** In
1072a 63 72 65 61 73 65 20 74 68 65 20 72 65 66 65 72 crease the refer
1072b 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 ence count of a
1072c 73 75 70 70 6c 69 65 64 20 70 61 67 65 20 62 79 supplied page by
1072d 20 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 1..*/.SQLITE_PR
1072e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1072f 65 33 50 63 61 63 68 65 52 65 66 28 50 67 48 64 e3PcacheRef(PgHd
10730 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 r *p){. assert(
10731 70 2d 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d p->nRef>0);. p-
10732 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a >nRef++;.}../*.*
10733 2a 20 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 * Drop a page fr
10734 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 om the cache. Th
10735 65 72 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 ere must be exac
10736 74 6c 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 tly one referenc
10737 65 20 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 e to the.** page
10738 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
10739 64 65 6c 65 74 65 73 20 74 68 61 74 20 72 65 66 deletes that ref
1073a 65 72 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 erence, so after
1073b 20 69 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a it returns the.
1073c 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 ** page pointed
1073d 74 6f 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c to by p is inval
1073e 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 id..*/.SQLITE_PR
1073f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
10740 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 e3PcacheDrop(PgH
10741 64 72 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 dr *p){. PCache
10742 20 2a 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 *pCache;. asse
10743 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 rt( p->nRef==1 )
10744 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 ;. if( p->flags
10745 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 7b 0a &PGHDR_DIRTY ){.
10746 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 pcacheRemove
10747 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 FromDirtyList(p)
10748 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 65 20 3d ;. }. pCache =
10749 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 70 43 p->pCache;. pC
1074a 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 ache->nRef--;.
1074b 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 if( p->pgno==1 )
1074c 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 50 {. pCache->pP
1074d 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 age1 = 0;. }.
1074e 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
1074f 66 69 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 fig.pcache.xUnpi
10750 6e 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 n(pCache->pCache
10751 2c 20 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a , p, 1);.}../*.*
10752 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
10753 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 page is marked a
10754 73 20 64 69 72 74 79 2e 20 49 66 20 69 74 20 69 s dirty. If it i
10755 73 6e 27 74 20 64 69 72 74 79 20 61 6c 72 65 61 sn't dirty alrea
10756 64 79 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 dy,.** make it s
10757 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 o..*/.SQLITE_PRI
10758 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
10759 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 3PcacheMakeDirty
1075a 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43 (PgHdr *p){. PC
1075b 61 63 68 65 20 2a 70 43 61 63 68 65 3b 0a 20 20 ache *pCache;.
1075c 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 p->flags &= ~PGH
1075d 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 20 DR_DONT_WRITE;.
1075e 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
1075f 3e 30 20 29 3b 0a 20 20 69 66 28 20 30 3d 3d 28 >0 );. if( 0==(
10760 70 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 p->flags & PGHDR
10761 5f 44 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70 _DIRTY) ){. p
10762 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 Cache = p->pCach
10763 65 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 e;. p->flags
10764 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0a |= PGHDR_DIRTY;.
10765 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 pcacheAddToD
10766 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 20 irtyList( p);.
10767 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 }.}../*.** Make
10768 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 73 sure the page is
10769 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 6e marked as clean
1076a 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c . If it isn't cl
1076b 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 ean already,.**
1076c 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 make it so..*/.S
1076d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1076e 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
1076f 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20 MakeClean(PgHdr
10770 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 *p){. if( (p->f
10771 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 lags & PGHDR_DIR
10772 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 TY) ){. pcach
10773 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 eRemoveFromDirty
10774 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d 3e List(p);. p->
10775 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52 flags &= ~(PGHDR
10776 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 45 _DIRTY|PGHDR_NEE
10777 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 D_SYNC);. if(
10778 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 p->nRef==0 ){.
10779 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e pcacheUnpin
1077a 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d (p);. }. }.}
1077b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 65 ../*.** Make eve
1077c 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 ry page in the c
1077d 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 ache clean..*/.S
1077e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1077f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
10780 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 20 CleanAll(PCache
10781 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 *pCache){. PgHd
10782 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 28 r *p;. while( (
10783 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 p = pCache->pDir
10784 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 ty)!=0 ){. sq
10785 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
10786 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a lean(p);. }.}..
10787 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 /*.** Clear the
10788 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
10789 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69 flag from all di
1078a 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 51 rty pages..*/.SQ
1078b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1078c 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 d sqlite3PcacheC
1078d 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 learSyncFlags(PC
1078e 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
1078f 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 PgHdr *p;. for
10790 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 (p=pCache->pDirt
10791 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 y; p; p=p->pDirt
10792 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66 yNext){. p->f
10793 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e lags &= ~PGHDR_N
10794 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 EED_SYNC;. }.
10795 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 pCache->pSynced
10796 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 = pCache->pDirty
10797 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 Tail;.}../*.** C
10798 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 6e hange the page n
10799 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 20 umber of page p
1079a 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a to newPgno. .*/.
1079b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1079c 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
1079d 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20 eMove(PgHdr *p,
1079e 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 Pgno newPgno){.
1079f 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 20 PCache *pCache
107a0 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61 = p->pCache;. a
107a1 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 ssert( p->nRef>0
107a2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65 );. assert( ne
107a3 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 6c wPgno>0 );. sql
107a4 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
107a5 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28 70 .pcache.xRekey(p
107a6 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 Cache->pCache, p
107a7 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 67 , p->pgno, newPg
107a8 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 3d no);. p->pgno =
107a9 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 20 newPgno;. if(
107aa 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f (p->flags&PGHDR_
107ab 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66 6c DIRTY) && (p->fl
107ac 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
107ad 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61 63 YNC) ){. pcac
107ae 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
107af 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 63 yList(p);. pc
107b0 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 acheAddToDirtyLi
107b1 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a st(p);. }.}../*
107b2 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63 .** Drop every c
107b3 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65 ache entry whose
107b4 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 page number is
107b5 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 67 greater than "pg
107b6 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c no". The.** call
107b7 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 er must ensure t
107b8 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f hat there are no
107b9 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 outstanding ref
107ba 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 erences to any p
107bb 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 68 ages.** other th
107bc 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 61 an page 1 with a
107bd 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65 page number gre
107be 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0a ater than pgno..
107bf 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 **.** If there i
107c0 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f s a reference to
107c1 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 20 page 1 and the
107c2 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 70 pgno parameter p
107c3 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a assed to this.**
107c4 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c 20 function is 0,
107c5 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 72 then the data ar
107c6 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ea associated wi
107c7 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 72 th page 1 is zer
107c8 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20 oed, but.** the
107c9 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 6e page object is n
107ca 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 ot dropped..*/.S
107cb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
107cc 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
107cd 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 20 Truncate(PCache
107ce 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 67 *pCache, Pgno pg
107cf 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 no){. if( pCach
107d0 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 e->pCache ){.
107d1 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50 PgHdr *p;. P
107d2 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 20 gHdr *pNext;.
107d3 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 for(p=pCache->p
107d4 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 78 Dirty; p; p=pNex
107d5 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20 t){. pNext
107d6 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b = p->pDirtyNext;
107d7 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67 . if( p->pg
107d8 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 no>pgno ){.
107d9 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c assert( p->fl
107da 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 ags&PGHDR_DIRTY
107db 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
107dc 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 e3PcacheMakeClea
107dd 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n(p);. }.
107de 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f }. if( pgno
107df 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 ==0 && pCache->p
107e0 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20 6d Page1 ){. m
107e1 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70 50 emset(pCache->pP
107e2 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c 20 age1->pData, 0,
107e3 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b pCache->szPage);
107e4 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 3b . pgno = 1;
107e5 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
107e6 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
107e7 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 28 cache.xTruncate(
107e8 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
107e9 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a pgno+1);. }.}..
107ea 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 61 /*.** Close a ca
107eb 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 che..*/.SQLITE_P
107ec 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
107ed 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50 te3PcacheClose(P
107ee 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
107ef 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 if( pCache->pC
107f0 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 ache ){. sqli
107f1 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
107f2 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 28 pcache.xDestroy(
107f3 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b pCache->pCache);
107f4 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 . }.}../* .** D
107f5 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 iscard the conte
107f6 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 nts of the cache
107f7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
107f8 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
107f9 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68 cacheClear(PCach
107fa 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 73 71 e *pCache){. sq
107fb 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63 lite3PcacheTrunc
107fc 61 74 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a ate(pCache, 0);.
107fd 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
107fe 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 OK;.}../*.** Mer
107ff 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 ge two lists of
10800 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 pages connected
10801 62 79 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e by pDirty and in
10802 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 pgno order..**
10803 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 Do not both fixi
10804 6e 67 20 74 68 65 20 70 44 69 72 74 79 50 72 65 ng the pDirtyPre
10805 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 v pointers..*/.s
10806 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 tatic PgHdr *pca
10807 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 cheMergeDirtyLis
10808 74 28 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 t(PgHdr *pA, PgH
10809 64 72 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 dr *pB){. PgHdr
1080a 20 72 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b result, *pTail;
1080b 0a 20 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 . pTail = &resu
1080c 6c 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 lt;. while( pA
1080d 26 26 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 && pB ){. if(
1080e 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 pA->pgno<pB->pg
1080f 6e 6f 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 no ){. pTai
10810 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a l->pDirty = pA;.
10811 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 pTail = pA
10812 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d ;. pA = pA-
10813 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c >pDirty;. }el
10814 73 65 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d se{. pTail-
10815 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 >pDirty = pB;.
10816 20 20 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a pTail = pB;.
10817 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 pB = pB->p
10818 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d Dirty;. }. }
10819 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 . if( pA ){.
1081a 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d pTail->pDirty =
1081b 20 70 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 pA;. }else if(
1081c 20 70 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c pB ){. pTail
1081d 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 ->pDirty = pB;.
1081e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 }else{. pTai
1081f 6c 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 l->pDirty = 0;.
10820 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 }. return resu
10821 6c 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a lt.pDirty;.}../*
10822 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 .** Sort the lis
10823 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 t of pages in ac
10824 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 cending order by
10825 20 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 pgno. Pages ar
10826 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 e.** connected b
10827 79 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 y pDirty pointer
10828 73 2e 20 20 54 68 65 20 70 44 69 72 74 79 50 72 s. The pDirtyPr
10829 65 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a ev pointers are.
1082a 2a 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 ** corrupted by
1082b 74 68 69 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64 this sort..*/.#d
1082c 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 efine N_SORT_BUC
1082d 4b 45 54 5f 41 4c 4c 4f 43 20 32 35 0a 23 64 65 KET_ALLOC 25.#de
1082e 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b fine N_SORT_BUCK
1082f 45 54 20 20 20 20 20 20 20 32 35 0a 23 69 66 64 ET 25.#ifd
10830 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
10831 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 int sqlite3_pag
10832 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 er_n_sort_bucket
10833 20 3d 20 30 3b 0a 20 20 23 75 6e 64 65 66 20 4e = 0;. #undef N
10834 5f 53 4f 52 54 5f 42 55 43 4b 45 54 0a 20 20 23 _SORT_BUCKET. #
10835 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 define N_SORT_BU
10836 43 4b 45 54 20 5c 0a 20 20 20 28 73 71 6c 69 74 CKET \. (sqlit
10837 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f e3_pager_n_sort_
10838 62 75 63 6b 65 74 3f 73 71 6c 69 74 65 33 5f 70 bucket?sqlite3_p
10839 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b ager_n_sort_buck
1083a 65 74 3a 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 et:N_SORT_BUCKET
1083b 5f 41 4c 4c 4f 43 29 0a 23 65 6e 64 69 66 0a 73 _ALLOC).#endif.s
1083c 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 tatic PgHdr *pca
1083d 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 cheSortDirtyList
1083e 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 (PgHdr *pIn){.
1083f 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f PgHdr *a[N_SORT_
10840 42 55 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20 2a BUCKET_ALLOC], *
10841 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65 p;. int i;. me
10842 6d 73 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f mset(a, 0, sizeo
10843 66 28 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 f(a));. while(
10844 70 49 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 pIn ){. p = p
10845 49 6e 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d In;. pIn = p-
10846 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e >pDirty;. p->
10847 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 pDirty = 0;.
10848 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f 52 for(i=0; i<N_SOR
10849 54 5f 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b 29 T_BUCKET-1; i++)
1084a 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69 5d {. if( a[i]
1084b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ==0 ){. a
1084c 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 [i] = p;.
1084d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 break;. }e
1084e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20 3d lse{. p =
1084f 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 pcacheMergeDirt
10850 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a yList(a[i], p);.
10851 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 30 a[i] = 0
10852 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
10853 20 20 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f 52 if( i==N_SOR
10854 54 5f 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20 20 T_BUCKET-1 ){.
10855 20 20 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a /* Coverage:
10856 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 To get here, th
10857 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 ere need to be 2
10858 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 ^(N_SORT_BUCKET)
10859 20 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 . ** eleme
1085a 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 nts in the input
1085b 20 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 70 list. This is p
1085c 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 69 6d 70 ossible, but imp
1085d 72 61 63 74 69 63 61 6c 2e 0a 20 20 20 20 20 20 ractical..
1085e 2a 2a 20 54 65 73 74 69 6e 67 20 74 68 69 73 20 ** Testing this
1085f 6c 69 6e 65 20 69 73 20 74 68 65 20 70 6f 69 6e line is the poin
10860 74 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 t of global vari
10861 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 71 able. ** sq
10862 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f lite3_pager_n_so
10863 72 74 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20 20 rt_bucket..
10864 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d */. a[i] =
10865 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 pcacheMergeDirt
10866 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a yList(a[i], p);.
10867 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 }. }. p =
10868 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b a[0];. for(i=1;
10869 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 i<N_SORT_BUCKET
1086a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 ; i++){. p =
1086b 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 pcacheMergeDirty
1086c 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 List(p, a[i]);.
1086d 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
1086e 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
1086f 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 list of all dir
10870 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 ty pages in the
10871 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79 cache, sorted by
10872 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f page number..*/
10873 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10874 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 PgHdr *sqlite3Pc
10875 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 acheDirtyList(PC
10876 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
10877 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 PgHdr *p;. for
10878 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 (p=pCache->pDirt
10879 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 y; p; p=p->pDirt
1087a 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 70 yNext){. p->p
1087b 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 69 72 74 Dirty = p->pDirt
1087c 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 yNext;. }. ret
1087d 75 72 6e 20 70 63 61 63 68 65 53 6f 72 74 44 69 urn pcacheSortDi
1087e 72 74 79 4c 69 73 74 28 70 43 61 63 68 65 2d 3e rtyList(pCache->
1087f 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a pDirty);.}../* .
10880 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f ** Return the to
10881 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 tal number of re
10882 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 68 ferenced pages h
10883 65 6c 64 20 62 79 20 74 68 65 20 63 61 63 68 65 eld by the cache
10884 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10885 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
10886 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43 cacheRefCount(PC
10887 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
10888 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 2d 3e return pCache->
10889 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 nRef;.}../*.** R
1088a 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
1088b 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 of references t
1088c 6f 20 74 68 65 20 70 61 67 65 20 73 75 70 70 6c o the page suppl
1088d 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 ied as an argume
1088e 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
1088f 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
10890 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 3PcachePageRefco
10891 75 6e 74 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 unt(PgHdr *p){.
10892 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b return p->nRef;
10893 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 .}../* .** Retur
10894 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 n the total numb
10895 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
10896 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c he cache..*/.SQL
10897 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
10898 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 sqlite3PcachePag
10899 65 63 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70 ecount(PCache *p
1089a 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 6e 50 Cache){. int nP
1089b 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 age = 0;. if( p
1089c 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b Cache->pCache ){
1089d 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73 71 6c . nPage = sql
1089e 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
1089f 2e 70 63 61 63 68 65 2e 78 50 61 67 65 63 6f 75 .pcache.xPagecou
108a0 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 nt(pCache->pCach
108a1 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e e);. }. return
108a2 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 65 nPage;.}..#ifde
108a3 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a f SQLITE_TEST./*
108a4 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 75 67 67 .** Get the sugg
108a5 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 ested cache-size
108a6 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 value..*/.SQLIT
108a7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
108a8 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61 lite3PcacheGetCa
108a9 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a chesize(PCache *
108aa 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 75 72 pCache){. retur
108ab 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a n pCache->nMax;.
108ac 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
108ad 53 65 74 20 74 68 65 20 73 75 67 67 65 73 74 65 Set the suggeste
108ae 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61 6c d cache-size val
108af 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ue..*/.SQLITE_PR
108b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
108b1 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65 e3PcacheSetCache
108b2 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 size(PCache *pCa
108b3 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 che, int mxPage)
108b4 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 {. pCache->nMax
108b5 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 69 66 28 = mxPage;. if(
108b6 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
108b7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
108b8 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
108b9 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70 43 61 e.xCachesize(pCa
108ba 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 6d 78 50 che->pCache, mxP
108bb 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 age);. }.}..#if
108bc 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b def SQLITE_CHECK
108bd 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20 46 6f 72 _PAGES./*.** For
108be 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
108bf 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 currently in th
108c0 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65 20 e cache, invoke
108c1 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a the specified.**
108c2 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73 20 callback. This
108c3 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 is only used if
108c4 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 43 4b the SQLITE_CHECK
108c5 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 73 0a _PAGES macro is.
108c6 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 ** defined..*/.S
108c7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
108c8 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
108c9 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43 61 IterateDirty(PCa
108ca 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f 69 che *pCache, voi
108cb 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64 72 d (*xIter)(PgHdr
108cc 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 *)){. PgHdr *p
108cd 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 44 69 Dirty;. for(pDi
108ce 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 rty=pCache->pDir
108cf 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69 72 ty; pDirty; pDir
108d0 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72 74 ty=pDirty->pDirt
108d1 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 74 65 yNext){. xIte
108d2 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a 7d r(pDirty);. }.}
108d3 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
108d4 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
108d5 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a pcache.c *******
108d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
108d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
108d8 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
108d9 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
108da 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a 2a ile pcache1.c **
108db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
108dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
108dd 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
108de 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35 0a 008 November 05.
108df 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
108e0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
108e1 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
108e2 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
108e3 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
108e4 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
108e5 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
108e6 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
108e7 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
108e8 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
108e9 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
108ea 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
108eb 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
108ec 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
108ed 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
108ee 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
108ef 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
108f0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
108f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
108f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
108f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
108f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
108f5 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 *.** This file i
108f6 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 65 mplements the de
108f7 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 fault page cache
108f8 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
108f9 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f (the.** sqlite3_
108fa 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 65 pcache interface
108fb 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61 ). It also conta
108fc 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 ins part of the
108fd 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
108fe 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f * of the SQLITE_
108ff 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10900 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 6c and sqlite3_rel
10901 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66 65 ease_memory() fe
10902 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74 68 atures..** If th
10903 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 e default page c
10904 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ache implementat
10905 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65 6e ion is overriden
10906 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 6f , then neither o
10907 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20 66 f.** these two f
10908 65 61 74 75 72 65 73 20 61 72 65 20 61 76 61 69 eatures are avai
10909 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 lable..**.** @(#
1090a 29 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e 63 ) $Id: pcache1.c
1090b 2c 76 20 31 2e 37 20 32 30 30 39 2f 30 31 2f 30 ,v 1.7 2009/01/0
1090c 37 20 31 35 3a 31 38 3a 32 31 20 64 61 6e 69 65 7 15:18:21 danie
1090d 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
1090e 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ..typedef struct
1090f 20 50 43 61 63 68 65 31 20 50 43 61 63 68 65 31 PCache1 PCache1
10910 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
10911 20 50 67 48 64 72 31 20 50 67 48 64 72 31 3b 0a PgHdr1 PgHdr1;.
10912 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 typedef struct P
10913 67 46 72 65 65 73 6c 6f 74 20 50 67 46 72 65 65 gFreeslot PgFree
10914 73 6c 6f 74 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 slot;../* Pointe
10915 72 73 20 74 6f 20 73 74 72 75 63 74 75 72 65 73 rs to structures
10916 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 61 72 of this type ar
10917 65 20 63 61 73 74 20 61 6e 64 20 72 65 74 75 72 e cast and retur
10918 6e 65 64 20 61 73 20 0a 2a 2a 20 6f 70 61 71 75 ned as .** opaqu
10919 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
1091a 2a 20 68 61 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 * handles.*/.str
1091b 75 63 74 20 50 43 61 63 68 65 31 20 7b 0a 20 20 uct PCache1 {.
1091c 2f 2a 20 43 61 63 68 65 20 63 6f 6e 66 69 67 75 /* Cache configu
1091d 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 ration parameter
1091e 73 2e 20 50 61 67 65 20 73 69 7a 65 20 28 73 7a s. Page size (sz
1091f 50 61 67 65 29 20 61 6e 64 20 74 68 65 20 70 75 Page) and the pu
10920 72 67 65 61 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 rgeable. ** fla
10921 67 20 28 62 50 75 72 67 65 61 62 6c 65 29 20 61 g (bPurgeable) a
10922 72 65 20 73 65 74 20 77 68 65 6e 20 74 68 65 20 re set when the
10923 63 61 63 68 65 20 69 73 20 63 72 65 61 74 65 64 cache is created
10924 2e 20 6e 4d 61 78 20 6d 61 79 20 62 65 20 0a 20 . nMax may be .
10925 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61 74 20 ** modified at
10926 61 6e 79 20 74 69 6d 65 20 62 79 20 61 20 63 61 any time by a ca
10927 6c 6c 20 74 6f 20 74 68 65 20 70 63 61 63 68 65 ll to the pcache
10928 31 43 61 63 68 65 53 69 7a 65 28 29 20 6d 65 74 1CacheSize() met
10929 68 6f 64 2e 0a 20 20 2a 2a 20 54 68 65 20 67 6c hod.. ** The gl
1092a 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 obal mutex must
1092b 62 65 20 68 65 6c 64 20 77 68 65 6e 20 61 63 63 be held when acc
1092c 65 73 73 69 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a essing nMax.. *
1092d 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 /. int szPage;
1092e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1092f 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
10930 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 of allocated pag
10931 65 73 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 es in bytes */.
10932 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b int bPurgeable;
10933 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10934 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
10935 63 61 63 68 65 20 69 73 20 70 75 72 67 65 61 62 cache is purgeab
10936 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 le */. unsigned
10937 20 69 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 int nMin;
10938 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
10939 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 inimum number of
1093a 20 70 61 67 65 73 20 72 65 73 65 72 76 65 64 20 pages reserved
1093b 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e */. unsigned in
1093c 74 20 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 t nMax;
1093d 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 /* Conf
1093e 69 67 75 72 65 64 20 22 63 61 63 68 65 5f 73 69 igured "cache_si
1093f 7a 65 22 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 ze" value */..
10940 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 /* Hash table of
10941 20 61 6c 6c 20 70 61 67 65 73 2e 20 54 68 65 20 all pages. The
10942 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 following variab
10943 6c 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 les may only be
10944 61 63 63 65 73 73 65 64 0a 20 20 2a 2a 20 77 68 accessed. ** wh
10945 65 6e 20 74 68 65 20 61 63 63 65 73 73 6f 72 20 en the accessor
10946 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 67 is holding the g
10947 6c 6f 62 61 6c 20 6d 75 74 65 78 20 28 73 65 65 lobal mutex (see
10948 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 pcache1EnterMut
10949 65 78 28 29 20 0a 20 20 2a 2a 20 61 6e 64 20 70 ex() . ** and p
1094a 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
1094b 28 29 29 2e 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 ()).. */. unsi
1094c 67 6e 65 64 20 69 6e 74 20 6e 52 65 63 79 63 6c gned int nRecycl
1094d 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 able;
1094e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
1094f 65 73 20 69 6e 20 74 68 65 20 4c 52 55 20 6c 69 es in the LRU li
10950 73 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 st */. unsigned
10951 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 int nPage;
10952 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
10953 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
10954 61 67 65 73 20 69 6e 20 61 70 48 61 73 68 20 2a ages in apHash *
10955 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
10956 20 6e 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 nHash;
10957 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
10958 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 r of slots in ap
10959 48 61 73 68 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 Hash[] */. PgHd
1095a 72 31 20 2a 2a 61 70 48 61 73 68 3b 20 20 20 20 r1 **apHash;
1095b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1095c 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f /* Hash table fo
1095d 72 20 66 61 73 74 20 6c 6f 6f 6b 75 70 20 62 79 r fast lookup by
1095e 20 6b 65 79 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 key */.. unsig
1095f 6e 65 64 20 69 6e 74 20 69 4d 61 78 4b 65 79 3b ned int iMaxKey;
10960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10961 2a 20 4c 61 72 67 65 73 74 20 6b 65 79 20 73 65 * Largest key se
10962 65 6e 20 73 69 6e 63 65 20 78 54 72 75 6e 63 61 en since xTrunca
10963 74 65 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a te() */.};../*.*
10964 2a 20 45 61 63 68 20 63 61 63 68 65 20 65 6e 74 * Each cache ent
10965 72 79 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 ry is represente
10966 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 d by an instance
10967 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
10968 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e g .** structure.
10969 20 41 20 62 75 66 66 65 72 20 6f 66 20 50 67 48 A buffer of PgH
1096a 64 72 31 2e 70 43 61 63 68 65 2d 3e 73 7a 50 61 dr1.pCache->szPa
1096b 67 65 20 62 79 74 65 73 20 69 73 20 61 6c 6c 6f ge bytes is allo
1096c 63 61 74 65 64 20 0a 2a 2a 20 64 69 72 65 63 74 cated .** direct
1096d 6c 79 20 61 66 74 65 72 20 74 68 65 20 73 74 72 ly after the str
1096e 75 63 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 ucture in memory
1096f 20 28 73 65 65 20 74 68 65 20 50 47 48 44 52 31 (see the PGHDR1
10970 5f 54 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d _TO_PAGE() .** m
10971 61 63 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a acro below)..*/.
10972 73 74 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a struct PgHdr1 {.
10973 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 unsigned int i
10974 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 Key;
10975 20 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 /* Key value (p
10976 61 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 age number) */.
10977 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 PgHdr1 *pNext;
10978 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10979 2f 2a 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20 /* Next in hash
1097a 74 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 table chain */.
1097b 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 PCache1 *pCache
1097c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1097d 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 63 75 /* Cache that cu
1097e 72 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 rrently owns thi
1097f 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 s page */. PgHd
10980 72 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 r1 *pLruNext;
10981 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
10982 78 74 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f xt in LRU list o
10983 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 f unpinned pages
10984 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c */. PgHdr1 *pL
10985 72 75 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 ruPrev;
10986 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 /* Previous
10987 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 in LRU list of
10988 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a unpinned pages *
10989 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 /.};../*.** Free
1098a 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c slots in the al
1098b 6c 6f 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20 locator used to
1098c 64 69 76 69 64 65 20 75 70 20 74 68 65 20 62 75 divide up the bu
1098d 66 66 65 72 20 70 72 6f 76 69 64 65 64 20 75 73 ffer provided us
1098e 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 ing.** the SQLIT
1098f 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
10990 48 45 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f HE mechanism..*/
10991 0a 73 74 72 75 63 74 20 50 67 46 72 65 65 73 6c .struct PgFreesl
10992 6f 74 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f ot {. PgFreeslo
10993 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 t *pNext; /* Ne
10994 78 74 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a xt free slot */.
10995 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c };../*.** Global
10996 20 64 61 74 61 20 75 73 65 64 20 62 79 20 74 68 data used by th
10997 69 73 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 is cache..*/.sta
10998 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 tic SQLITE_WSD s
10999 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 truct PCacheGlob
1099a 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d al {. sqlite3_m
1099b 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 utex *mutex;
1099c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 /* st
1099d 61 74 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58 atic mutex MUTEX
1099e 5f 53 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a _STATIC_LRU */..
1099f 20 20 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 int nMaxPage;
109a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
109a1 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 /* Sum of
109a2 6e 4d 61 78 50 61 67 65 20 66 6f 72 20 70 75 72 nMaxPage for pur
109a3 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f geable caches */
109a4 0a 20 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b . int nMinPage;
109a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
109a6 20 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 /* Sum of
109a7 20 6e 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75 nMinPage for pu
109a8 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a rgeable caches *
109a9 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 /. int nCurrent
109aa 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
109ab 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
109ac 72 20 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 r of purgeable p
109ad 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a ages allocated *
109ae 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 /. PgHdr1 *pLru
109af 48 65 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b Head, *pLruTail;
109b0 20 20 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c /* LRU l
109b1 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 ist of unpinned
109b2 70 61 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 pages */.. /* V
109b3 61 72 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64 ariables related
109b4 20 74 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 to SQLITE_CONFI
109b5 47 5f 50 41 47 45 43 41 43 48 45 20 73 65 74 74 G_PAGECACHE sett
109b6 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 ings. */. int s
109b7 7a 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 zSlot;
109b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
109b9 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66 * Size of each f
109ba 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f ree slot */. vo
109bb 69 64 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e id *pStart, *pEn
109bc 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
109bd 20 20 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 /* Bounds of p
109be 61 67 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 agecache malloc
109bf 72 61 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 range */. PgFre
109c0 65 73 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 eslot *pFree;
109c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
109c2 2a 20 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 * Free page bloc
109c3 6b 73 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f ks */.} pcache1_
109c4 67 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f g;../*.** All co
109c5 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
109c6 73 68 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68 should access th
109c7 65 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 e global structu
109c8 72 65 20 61 62 6f 76 65 20 76 69 61 20 74 68 65 re above via the
109c9 0a 2a 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 .** alias "pcach
109ca 65 31 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 e1". This ensure
109cb 73 20 74 68 61 74 20 74 68 65 20 57 53 44 20 65 s that the WSD e
109cc 6d 75 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 mulation is used
109cd 20 77 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 when.** compili
109ce 6e 67 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 ng for systems t
109cf 68 61 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f hat do not suppo
109d0 72 74 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a rt real WSD..*/.
109d1 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 20 #define pcache1
109d2 28 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50 (GLOBAL(struct P
109d3 43 61 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 CacheGlobal, pca
109d4 63 68 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 che1_g))../*.**
109d5 57 68 65 6e 20 61 20 50 67 48 64 72 31 20 73 74 When a PgHdr1 st
109d6 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 ructure is alloc
109d7 61 74 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69 ated, the associ
109d8 61 74 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50 ated PCache1.szP
109d9 61 67 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 age.** bytes of
109da 64 61 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64 data are located
109db 20 64 69 72 65 63 74 6c 79 20 61 66 74 65 72 20 directly after
109dc 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e it in memory (i.
109dd 65 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 e. the total.**
109de 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f size of the allo
109df 63 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 cation is sizeof
109e0 28 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 31 (PgHdr1)+PCache1
109e1 2e 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 54 .szPage byte). T
109e2 68 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f he.** PGHDR1_TO_
109e3 50 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 6b PAGE() macro tak
109e4 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 es a pointer to
109e5 61 20 50 67 48 64 72 31 20 73 74 72 75 63 74 75 a PgHdr1 structu
109e6 72 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 re as.** an argu
109e7 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 73 ment and returns
109e8 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
109e9 65 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c 6f e associated blo
109ea 63 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 ck of szPage.**
109eb 62 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 5f bytes. The PAGE_
109ec 54 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 72 TO_PGHDR1() macr
109ed 6f 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 o does the oppos
109ee 69 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 6e ite: its argumen
109ef 74 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 t is.** a pointe
109f0 72 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 r to a block of
109f1 73 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 20 szPage bytes of
109f2 64 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 74 data and the ret
109f3 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 urn value is.**
109f4 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
109f5 20 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 64 associated PgHd
109f6 72 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a r1 structure..**
109f7 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 47 .** assert( PG
109f8 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 HDR1_TO_PAGE(PAG
109f9 45 5f 54 4f 5f 50 47 48 44 52 31 28 58 29 29 3d E_TO_PGHDR1(X))=
109fa 3d 58 20 29 3b 0a 2a 2f 0a 23 64 65 66 69 6e 65 =X );.*/.#define
109fb 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 PGHDR1_TO_PAGE(
109fc 70 29 20 28 76 6f 69 64 20 2a 29 28 26 28 28 75 p) (void *)(&((u
109fd 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 nsigned char *)p
109fe 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29 )[sizeof(PgHdr1)
109ff 5d 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 5f ]).#define PAGE_
10a00 54 4f 5f 50 47 48 44 52 31 28 70 29 20 28 50 67 TO_PGHDR1(p) (Pg
10a01 48 64 72 31 20 2a 29 28 26 28 28 75 6e 73 69 67 Hdr1 *)(&((unsig
10a02 6e 65 64 20 63 68 61 72 20 2a 29 70 29 5b 2d 31 ned char *)p)[-1
10a03 2a 28 69 6e 74 29 73 69 7a 65 6f 66 28 50 67 48 *(int)sizeof(PgH
10a04 64 72 31 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 dr1)])../*.** Ma
10a05 63 72 6f 73 20 74 6f 20 65 6e 74 65 72 20 61 6e cros to enter an
10a06 64 20 6c 65 61 76 65 20 74 68 65 20 67 6c 6f 62 d leave the glob
10a07 61 6c 20 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f al LRU mutex..*/
10a08 0a 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 .#define pcache1
10a09 45 6e 74 65 72 4d 75 74 65 78 28 29 20 73 71 6c EnterMutex() sql
10a0a 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
10a0b 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 0a (pcache1.mutex).
10a0c 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 4c #define pcache1L
10a0d 65 61 76 65 4d 75 74 65 78 28 29 20 73 71 6c 69 eaveMutex() sqli
10a0e 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
10a0f 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a pcache1.mutex)..
10a10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
10a11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
10a15 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 ./******** Page
10a16 41 6c 6c 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 Allocation/SQLIT
10a17 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 E_CONFIG_PCACHE
10a18 52 65 6c 61 74 65 64 20 46 75 6e 63 74 69 6f 6e Related Function
10a19 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s **************
10a1a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 /../*.** This fu
10a1b 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
10a1c 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 during initiali
10a1d 7a 61 74 69 6f 6e 20 69 66 20 61 20 73 74 61 74 zation if a stat
10a1e 69 63 20 62 75 66 66 65 72 20 69 73 20 0a 2a 2a ic buffer is .**
10a1f 20 73 75 70 70 6c 69 65 64 20 74 6f 20 75 73 65 supplied to use
10a20 20 66 6f 72 20 74 68 65 20 70 61 67 65 2d 63 61 for the page-ca
10a21 63 68 65 20 62 79 20 70 61 73 73 69 6e 67 20 74 che by passing t
10a22 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 he SQLITE_CONFIG
10a23 5f 50 41 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 _PAGECACHE.** ve
10a24 72 62 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f rb to sqlite3_co
10a25 6e 66 69 67 28 29 2e 20 50 61 72 61 6d 65 74 65 nfig(). Paramete
10a26 72 20 70 42 75 66 20 70 6f 69 6e 74 73 20 74 6f r pBuf points to
10a27 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c an allocation l
10a28 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 arge.** enough t
10a29 6f 20 63 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 o contain 'n' bu
10a2a 66 66 65 72 73 20 6f 66 20 27 73 7a 27 20 62 79 ffers of 'sz' by
10a2b 74 65 73 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c tes each..*/.SQL
10a2c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
10a2d 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 42 75 sqlite3PCacheBu
10a2e 66 66 65 72 53 65 74 75 70 28 76 6f 69 64 20 2a fferSetup(void *
10a2f 70 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e pBuf, int sz, in
10a30 74 20 6e 29 7b 0a 20 20 50 67 46 72 65 65 73 6c t n){. PgFreesl
10a31 6f 74 20 2a 70 3b 0a 20 20 73 7a 20 26 3d 20 7e ot *p;. sz &= ~
10a32 37 3b 0a 20 20 70 63 61 63 68 65 31 2e 73 7a 53 7;. pcache1.szS
10a33 6c 6f 74 20 3d 20 73 7a 3b 0a 20 20 70 63 61 63 lot = sz;. pcac
10a34 68 65 31 2e 70 53 74 61 72 74 20 3d 20 70 42 75 he1.pStart = pBu
10a35 66 3b 0a 20 20 70 63 61 63 68 65 31 2e 70 46 72 f;. pcache1.pFr
10a36 65 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 ee = 0;. while(
10a37 20 6e 2d 2d 20 29 7b 0a 20 20 20 20 70 20 3d 20 n-- ){. p =
10a38 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 (PgFreeslot*)pBu
10a39 66 3b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 f;. p->pNext
10a3a 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b = pcache1.pFree;
10a3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72 . pcache1.pFr
10a3c 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 42 75 66 ee = p;. pBuf
10a3d 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 = (void*)&((cha
10a3e 72 2a 29 70 42 75 66 29 5b 73 7a 5d 3b 0a 20 20 r*)pBuf)[sz];.
10a3f 7d 0a 20 20 70 63 61 63 68 65 31 2e 70 45 6e 64 }. pcache1.pEnd
10a40 20 3d 20 70 42 75 66 3b 0a 7d 0a 0a 2f 2a 0a 2a = pBuf;.}../*.*
10a41 2a 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f * Malloc functio
10a42 6e 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 n used within th
10a43 69 73 20 66 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 is file to alloc
10a44 61 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 74 ate space from t
10a45 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 63 6f 6e he buffer.** con
10a46 66 69 67 75 72 65 64 20 75 73 69 6e 67 20 73 71 figured using sq
10a47 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c lite3_config(SQL
10a48 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
10a49 41 43 48 45 29 20 6f 70 74 69 6f 6e 2e 20 49 66 ACHE) option. If
10a4a 20 6e 6f 20 0a 2a 2a 20 73 75 63 68 20 62 75 66 no .** such buf
10a4b 66 65 72 20 65 78 69 73 74 73 20 6f 72 20 74 68 fer exists or th
10a4c 65 72 65 20 69 73 20 6e 6f 20 73 70 61 63 65 20 ere is no space
10a4d 6c 65 66 74 20 69 6e 20 69 74 2c 20 74 68 69 73 left in it, this
10a4e 20 66 75 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20 function falls
10a4f 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 73 71 6c 69 .** back to sqli
10a50 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a te3Malloc()..*/.
10a51 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61 static void *pca
10a52 63 68 65 31 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 che1Alloc(int nB
10a53 79 74 65 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b yte){. void *p;
10a54 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
10a55 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
10a56 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
10a57 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 70 63 61 if( nByte<=pca
10a58 63 68 65 31 2e 73 7a 53 6c 6f 74 20 26 26 20 70 che1.szSlot && p
10a59 63 61 63 68 65 31 2e 70 46 72 65 65 20 29 7b 0a cache1.pFree ){.
10a5a 20 20 20 20 70 20 3d 20 28 50 67 48 64 72 31 20 p = (PgHdr1
10a5b 2a 29 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b *)pcache1.pFree;
10a5c 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72 . pcache1.pFr
10a5d 65 65 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72 ee = pcache1.pFr
10a5e 65 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 ee->pNext;. s
10a5f 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 qlite3StatusSet(
10a60 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
10a61 47 45 43 41 43 48 45 5f 53 49 5a 45 2c 20 6e 42 GECACHE_SIZE, nB
10a62 79 74 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 yte);. sqlite
10a63 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
10a64 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 E_STATUS_PAGECAC
10a65 48 45 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 7d HE_USED, 1);. }
10a66 65 6c 73 65 7b 0a 0a 20 20 20 20 2f 2a 20 41 6c else{.. /* Al
10a67 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 62 75 66 locate a new buf
10a68 66 65 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 fer using sqlite
10a69 33 4d 61 6c 6c 6f 63 2e 20 42 65 66 6f 72 65 20 3Malloc. Before
10a6a 64 6f 69 6e 67 20 73 6f 2c 20 65 78 69 74 20 74 doing so, exit t
10a6b 68 65 0a 20 20 20 20 2a 2a 20 67 6c 6f 62 61 6c he. ** global
10a6c 20 70 63 61 63 68 65 20 6d 75 74 65 78 20 61 6e pcache mutex an
10a6d 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 d unlock the pag
10a6e 65 72 2d 63 61 63 68 65 20 6f 62 6a 65 63 74 20 er-cache object
10a6f 70 43 61 63 68 65 2e 20 54 68 69 73 20 69 73 20 pCache. This is
10a70 0a 20 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 . ** so that
10a71 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 if the attempt t
10a72 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 o allocate a new
10a73 20 62 75 66 66 65 72 20 63 61 75 73 65 73 20 74 buffer causes t
10a74 68 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 he the . ** c
10a75 6f 6e 66 69 67 75 72 65 64 20 73 6f 66 74 2d 68 onfigured soft-h
10a76 65 61 70 2d 6c 69 6d 69 74 20 74 6f 20 62 65 20 eap-limit to be
10a77 62 72 65 61 63 68 65 64 2c 20 69 74 20 77 69 6c breached, it wil
10a78 6c 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f l be possible to
10a79 0a 20 20 20 20 2a 2a 20 72 65 63 6c 61 69 6d 20 . ** reclaim
10a7a 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 69 73 memory from this
10a7b 20 70 61 67 65 72 2d 63 61 63 68 65 2e 0a 20 20 pager-cache..
10a7c 20 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 */. pcache1
10a7d 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
10a7e 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
10a7f 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 loc(nByte);.
10a80 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
10a81 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 x();. if( p )
10a82 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 20 3d {. int sz =
10a83 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 sqlite3MallocSi
10a84 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 73 71 6c ze(p);. sql
10a85 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 ite3StatusAdd(SQ
10a86 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
10a87 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 CACHE_OVERFLOW,
10a88 73 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 sz);. }. }.
10a89 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
10a8a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61 6c 6c 6f .** Free an allo
10a8b 63 61 74 65 64 20 62 75 66 66 65 72 20 6f 62 74 cated buffer obt
10a8c 61 69 6e 65 64 20 66 72 6f 6d 20 70 63 61 63 68 ained from pcach
10a8d 65 31 41 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 e1Alloc()..*/.st
10a8e 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
10a8f 31 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 1Free(void *p){.
10a90 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
10a91 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 3_mutex_held(pca
10a92 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 che1.mutex) );.
10a93 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
10a94 72 6e 3b 0a 20 20 69 66 28 20 70 3e 3d 70 63 61 rn;. if( p>=pca
10a95 63 68 65 31 2e 70 53 74 61 72 74 20 26 26 20 70 che1.pStart && p
10a96 3c 70 63 61 63 68 65 31 2e 70 45 6e 64 20 29 7b <pcache1.pEnd ){
10a97 0a 20 20 20 20 50 67 46 72 65 65 73 6c 6f 74 20 . PgFreeslot
10a98 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 73 71 6c 69 *pSlot;. sqli
10a99 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
10a9a 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
10a9b 41 43 48 45 5f 55 53 45 44 2c 20 2d 31 29 3b 0a ACHE_USED, -1);.
10a9c 20 20 20 20 70 53 6c 6f 74 20 3d 20 28 50 67 46 pSlot = (PgF
10a9d 72 65 65 73 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 reeslot*)p;.
10a9e 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20 3d 20 70 pSlot->pNext = p
10a9f 63 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 cache1.pFree;.
10aa0 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 pcache1.pFree
10aa1 3d 20 70 53 6c 6f 74 3b 0a 20 20 7d 65 6c 73 65 = pSlot;. }else
10aa2 7b 0a 20 20 20 20 69 6e 74 20 69 53 69 7a 65 20 {. int iSize
10aa3 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 = sqlite3MallocS
10aa4 69 7a 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 ize(p);. sqli
10aa5 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
10aa6 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
10aa7 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d ACHE_OVERFLOW, -
10aa8 69 53 69 7a 65 29 3b 0a 20 20 20 20 73 71 6c 69 iSize);. sqli
10aa9 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d te3_free(p);. }
10aaa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
10aab 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62 te a new page ob
10aac 6a 65 63 74 20 69 6e 69 74 69 61 6c 6c 79 20 61 ject initially a
10aad 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 ssociated with c
10aae 61 63 68 65 20 70 43 61 63 68 65 2e 0a 2a 2f 0a ache pCache..*/.
10aaf 73 74 61 74 69 63 20 50 67 48 64 72 31 20 2a 70 static PgHdr1 *p
10ab0 63 61 63 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 cache1AllocPage(
10ab1 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 29 PCache1 *pCache)
10ab2 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 {. int nByte =
10ab3 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29 20 2b sizeof(PgHdr1) +
10ab4 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 3b pCache->szPage;
10ab5 0a 20 20 50 67 48 64 72 31 20 2a 70 20 3d 20 28 . PgHdr1 *p = (
10ab6 50 67 48 64 72 31 20 2a 29 70 63 61 63 68 65 31 PgHdr1 *)pcache1
10ab7 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 Alloc(nByte);.
10ab8 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d if( p ){. mem
10ab9 73 65 74 28 70 2c 20 30 2c 20 6e 42 79 74 65 29 set(p, 0, nByte)
10aba 3b 0a 20 20 20 20 69 66 28 20 70 43 61 63 68 65 ;. if( pCache
10abb 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a ->bPurgeable ){.
10abc 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 6e 43 pcache1.nC
10abd 75 72 72 65 6e 74 50 61 67 65 2b 2b 3b 0a 20 20 urrentPage++;.
10abe 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
10abf 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p;.}../*.** Fre
10ac0 65 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 e a page object
10ac1 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 allocated by pca
10ac2 63 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e che1AllocPage().
10ac3 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10ac4 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 pcache1FreePage(
10ac5 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 69 66 PgHdr1 *p){. if
10ac6 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ( p ){. if( p
10ac7 2d 3e 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 ->pCache->bPurge
10ac8 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 63 able ){. pc
10ac9 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 ache1.nCurrentPa
10aca 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ge--;. }.
10acb 70 63 61 63 68 65 31 46 72 65 65 28 70 29 3b 0a pcache1Free(p);.
10acc 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c }.}../*.** Mal
10acd 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 loc function use
10ace 64 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 6f d by SQLite to o
10acf 62 74 61 69 6e 20 73 70 61 63 65 20 66 72 6f 6d btain space from
10ad0 20 74 68 65 20 62 75 66 66 65 72 20 63 6f 6e 66 the buffer conf
10ad1 69 67 75 72 65 64 0a 2a 2a 20 75 73 69 6e 67 20 igured.** using
10ad2 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 sqlite3_config(S
10ad3 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
10ad4 45 43 41 43 48 45 29 20 6f 70 74 69 6f 6e 2e 20 ECACHE) option.
10ad5 49 66 20 6e 6f 20 73 75 63 68 20 62 75 66 66 65 If no such buffe
10ad6 72 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 69 r.** exists, thi
10ad7 73 20 66 75 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 s function falls
10ad8 20 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 back to sqlite3
10ad9 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c Malloc()..*/.SQL
10ada 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
10adb 20 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c *sqlite3PageMal
10adc 6c 6f 63 28 69 6e 74 20 73 7a 29 7b 0a 20 20 76 loc(int sz){. v
10add 6f 69 64 20 2a 70 3b 0a 20 20 70 63 61 63 68 65 oid *p;. pcache
10ade 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
10adf 20 70 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f p = pcache1Allo
10ae0 63 28 73 7a 29 3b 0a 20 20 70 63 61 63 68 65 31 c(sz);. pcache1
10ae1 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
10ae2 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
10ae3 2a 2a 20 46 72 65 65 20 61 6e 20 61 6c 6c 6f 63 ** Free an alloc
10ae4 61 74 65 64 20 62 75 66 66 65 72 20 6f 62 74 61 ated buffer obta
10ae5 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
10ae6 33 50 61 67 65 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 3PageMalloc()..*
10ae7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10ae8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
10ae9 65 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a eFree(void *p){.
10aea 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
10aeb 74 65 78 28 29 3b 0a 20 20 70 63 61 63 68 65 31 tex();. pcache1
10aec 46 72 65 65 28 70 29 3b 0a 20 20 70 63 61 63 68 Free(p);. pcach
10aed 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a e1LeaveMutex();.
10aee 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
10aef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10af1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10af2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10af3 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 **/./******** Ge
10af4 6e 65 72 61 6c 20 49 6d 70 6c 65 6d 65 6e 74 61 neral Implementa
10af5 74 69 6f 6e 20 46 75 6e 63 74 69 6f 6e 73 20 2a tion Functions *
10af6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10af7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10af8 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ***/../*.** This
10af9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
10afa 64 20 74 6f 20 72 65 73 69 7a 65 20 74 68 65 20 d to resize the
10afb 68 61 73 68 20 74 61 62 6c 65 20 75 73 65 64 20 hash table used
10afc 62 79 20 74 68 65 20 63 61 63 68 65 20 70 61 73 by the cache pas
10afd 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 sed.** as the fi
10afe 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a rst argument..**
10aff 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d .** The global m
10b00 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c utex must be hel
10b01 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 d when this func
10b02 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a tion is called..
10b03 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 */.static int pc
10b04 61 63 68 65 31 52 65 73 69 7a 65 48 61 73 68 28 ache1ResizeHash(
10b05 50 43 61 63 68 65 31 20 2a 70 29 7b 0a 20 20 50 PCache1 *p){. P
10b06 67 48 64 72 31 20 2a 2a 61 70 4e 65 77 3b 0a 20 gHdr1 **apNew;.
10b07 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4e unsigned int nN
10b08 65 77 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 ew;. unsigned i
10b09 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 nt i;.. assert(
10b0a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
10b0b 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 eld(pcache1.mute
10b0c 78 29 20 29 3b 0a 0a 20 20 6e 4e 65 77 20 3d 20 x) );.. nNew =
10b0d 70 2d 3e 6e 48 61 73 68 2a 32 3b 0a 20 20 69 66 p->nHash*2;. if
10b0e 28 20 6e 4e 65 77 3c 32 35 36 20 29 7b 0a 20 20 ( nNew<256 ){.
10b0f 20 20 6e 4e 65 77 20 3d 20 32 35 36 3b 0a 20 20 nNew = 256;.
10b10 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 }.. pcache1Leav
10b11 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 eMutex();. if(
10b12 70 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71 6c 69 p->nHash ){ sqli
10b13 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
10b14 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 61 70 4e 65 lloc(); }. apNe
10b15 77 20 3d 20 28 50 67 48 64 72 31 20 2a 2a 29 73 w = (PgHdr1 **)s
10b16 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 qlite3_malloc(si
10b17 7a 65 6f 66 28 50 67 48 64 72 31 20 2a 29 2a 6e zeof(PgHdr1 *)*n
10b18 4e 65 77 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e New);. if( p->n
10b19 48 61 73 68 20 29 7b 20 73 71 6c 69 74 65 33 45 Hash ){ sqlite3E
10b1a 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
10b1b 3b 20 7d 0a 20 20 70 63 61 63 68 65 31 45 6e 74 ; }. pcache1Ent
10b1c 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 erMutex();. if(
10b1d 20 61 70 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 apNew ){. me
10b1e 6d 73 65 74 28 61 70 4e 65 77 2c 20 30 2c 20 73 mset(apNew, 0, s
10b1f 69 7a 65 6f 66 28 50 67 48 64 72 31 20 2a 29 2a izeof(PgHdr1 *)*
10b20 6e 4e 65 77 29 3b 0a 20 20 20 20 66 6f 72 28 69 nNew);. for(i
10b21 3d 30 3b 20 69 3c 70 2d 3e 6e 48 61 73 68 3b 20 =0; i<p->nHash;
10b22 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 48 64 i++){. PgHd
10b23 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 r1 *pPage;.
10b24 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74 20 3d PgHdr1 *pNext =
10b25 20 70 2d 3e 61 70 48 61 73 68 5b 69 5d 3b 0a 20 p->apHash[i];.
10b26 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 50 61 while( (pPa
10b27 67 65 20 3d 20 70 4e 65 78 74 29 21 3d 30 20 29 ge = pNext)!=0 )
10b28 7b 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e {. unsign
10b29 65 64 20 69 6e 74 20 68 20 3d 20 70 50 61 67 65 ed int h = pPage
10b2a 2d 3e 69 4b 65 79 20 25 20 6e 4e 65 77 3b 0a 20 ->iKey % nNew;.
10b2b 20 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70 pNext = p
10b2c 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Page->pNext;.
10b2d 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 pPage->pNex
10b2e 74 20 3d 20 61 70 4e 65 77 5b 68 5d 3b 0a 20 20 t = apNew[h];.
10b2f 20 20 20 20 20 20 61 70 4e 65 77 5b 68 5d 20 3d apNew[h] =
10b30 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a pPage;. }.
10b31 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
10b32 33 5f 66 72 65 65 28 70 2d 3e 61 70 48 61 73 68 3_free(p->apHash
10b33 29 3b 0a 20 20 20 20 70 2d 3e 61 70 48 61 73 68 );. p->apHash
10b34 20 3d 20 61 70 4e 65 77 3b 0a 20 20 20 20 70 2d = apNew;. p-
10b35 3e 6e 48 61 73 68 20 3d 20 6e 4e 65 77 3b 0a 20 >nHash = nNew;.
10b36 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 70 2d }.. return (p-
10b37 3e 61 70 48 61 73 68 20 3f 20 53 51 4c 49 54 45 >apHash ? SQLITE
10b38 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d _OK : SQLITE_NOM
10b39 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 EM);.}../*.** Th
10b3a 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 is function is u
10b3b 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 sed internally t
10b3c 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 70 61 67 o remove the pag
10b3d 65 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65 e pPage from the
10b3e 20 0a 2a 2a 20 67 6c 6f 62 61 6c 20 4c 52 55 20 .** global LRU
10b3f 6c 69 73 74 2c 20 69 66 20 69 73 20 70 61 72 74 list, if is part
10b40 20 6f 66 20 69 74 2e 20 49 66 20 70 50 61 67 65 of it. If pPage
10b41 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 is not part of
10b42 74 68 65 20 67 6c 6f 62 61 6c 0a 2a 2a 20 4c 52 the global.** LR
10b43 55 20 6c 69 73 74 2c 20 74 68 65 6e 20 74 68 69 U list, then thi
10b44 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 s function is a
10b45 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 no-op..**.** The
10b46 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 global mutex mu
10b47 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 st be held when
10b48 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
10b49 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 called..*/.stat
10b4a 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 50 ic void pcache1P
10b4b 69 6e 50 61 67 65 28 50 67 48 64 72 31 20 2a 70 inPage(PgHdr1 *p
10b4c 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Page){. assert(
10b4d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
10b4e 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 eld(pcache1.mute
10b4f 78 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 x) );. if( pPag
10b50 65 20 26 26 20 28 70 50 61 67 65 2d 3e 70 4c 72 e && (pPage->pLr
10b51 75 4e 65 78 74 20 7c 7c 20 70 50 61 67 65 3d 3d uNext || pPage==
10b52 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c pcache1.pLruTail
10b53 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 ) ){. if( pPa
10b54 67 65 2d 3e 70 4c 72 75 50 72 65 76 20 29 7b 0a ge->pLruPrev ){.
10b55 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 pPage->pLr
10b56 75 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78 74 20 uPrev->pLruNext
10b57 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 = pPage->pLruNex
10b58 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 t;. }. if(
10b59 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10b5a 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d ){. pPage-
10b5b 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50 >pLruNext->pLruP
10b5c 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 rev = pPage->pLr
10b5d 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 uPrev;. }.
10b5e 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 4c 72 if( pcache1.pLr
10b5f 75 48 65 61 64 3d 3d 70 50 61 67 65 20 29 7b 0a uHead==pPage ){.
10b60 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c pcache1.pL
10b61 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 2d 3e ruHead = pPage->
10b62 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a pLruNext;. }.
10b63 20 20 20 20 69 66 28 20 70 63 61 63 68 65 31 2e if( pcache1.
10b64 70 4c 72 75 54 61 69 6c 3d 3d 70 50 61 67 65 20 pLruTail==pPage
10b65 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 ){. pcache1
10b66 2e 70 4c 72 75 54 61 69 6c 20 3d 20 70 50 61 67 .pLruTail = pPag
10b67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 e->pLruPrev;.
10b68 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c }. pPage->pL
10b69 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 ruNext = 0;.
10b6a 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 20 pPage->pLruPrev
10b6b 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e = 0;. pPage->
10b6c 70 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 pCache->nRecycla
10b6d 62 6c 65 2d 2d 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f ble--;. }.}.../
10b6e 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 *.** Remove the
10b6f 70 61 67 65 20 73 75 70 70 6c 69 65 64 20 61 73 page supplied as
10b70 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 66 72 6f an argument fro
10b71 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 m the hash table
10b72 20 0a 2a 2a 20 28 50 43 61 63 68 65 31 2e 61 70 .** (PCache1.ap
10b73 48 61 73 68 20 73 74 72 75 63 74 75 72 65 29 20 Hash structure)
10b74 74 68 61 74 20 69 74 20 69 73 20 63 75 72 72 65 that it is curre
10b75 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 2e 0a ntly stored in..
10b76 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c **.** The global
10b77 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 mutex must be h
10b78 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 eld when this fu
10b79 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
10b7a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
10b7b 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 pcache1RemoveFr
10b7c 6f 6d 48 61 73 68 28 50 67 48 64 72 31 20 2a 70 omHash(PgHdr1 *p
10b7d 50 61 67 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 Page){. unsigne
10b7e 64 20 69 6e 74 20 68 3b 0a 20 20 50 43 61 63 68 d int h;. PCach
10b7f 65 31 20 2a 70 43 61 63 68 65 20 3d 20 70 50 61 e1 *pCache = pPa
10b80 67 65 2d 3e 70 43 61 63 68 65 3b 0a 20 20 50 67 ge->pCache;. Pg
10b81 48 64 72 31 20 2a 2a 70 70 3b 0a 0a 20 20 68 20 Hdr1 **pp;.. h
10b82 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25 20 = pPage->iKey %
10b83 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 pCache->nHash;.
10b84 20 66 6f 72 28 70 70 3d 26 70 43 61 63 68 65 2d for(pp=&pCache-
10b85 3e 61 70 48 61 73 68 5b 68 5d 3b 20 28 2a 70 70 >apHash[h]; (*pp
10b86 29 21 3d 70 50 61 67 65 3b 20 70 70 3d 26 28 2a )!=pPage; pp=&(*
10b87 70 70 29 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 2a pp)->pNext);. *
10b88 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 pp = (*pp)->pNex
10b89 74 3b 0a 0a 20 20 70 43 61 63 68 65 2d 3e 6e 50 t;.. pCache->nP
10b8a 61 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 age--;.}../*.**
10b8b 49 66 20 74 68 65 72 65 20 61 72 65 20 63 75 72 If there are cur
10b8c 72 65 6e 74 6c 79 20 6d 6f 72 65 20 74 68 61 6e rently more than
10b8d 20 70 63 61 63 68 65 2e 6e 4d 61 78 50 61 67 65 pcache.nMaxPage
10b8e 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 pages allocated
10b8f 2c 20 74 72 79 0a 2a 2a 20 74 6f 20 72 65 63 79 , try.** to recy
10b90 63 6c 65 20 70 61 67 65 73 20 74 6f 20 72 65 64 cle pages to red
10b91 75 63 65 20 74 68 65 20 6e 75 6d 62 65 72 20 61 uce the number a
10b92 6c 6c 6f 63 61 74 65 64 20 74 6f 20 70 63 61 63 llocated to pcac
10b93 68 65 2e 6e 4d 61 78 50 61 67 65 2e 0a 2a 2f 0a he.nMaxPage..*/.
10b94 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
10b95 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 he1EnforceMaxPag
10b96 65 28 76 6f 69 64 29 7b 0a 20 20 61 73 73 65 72 e(void){. asser
10b97 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
10b98 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 _held(pcache1.mu
10b99 74 65 78 29 20 29 3b 0a 20 20 77 68 69 6c 65 28 tex) );. while(
10b9a 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
10b9b 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d tPage>pcache1.nM
10b9c 61 78 50 61 67 65 20 26 26 20 70 63 61 63 68 65 axPage && pcache
10b9d 31 2e 70 4c 72 75 54 61 69 6c 20 29 7b 0a 20 20 1.pLruTail ){.
10b9e 20 20 50 67 48 64 72 31 20 2a 70 20 3d 20 70 63 PgHdr1 *p = pc
10b9f 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a ache1.pLruTail;.
10ba0 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 pcache1PinPa
10ba1 67 65 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 ge(p);. pcach
10ba2 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 e1RemoveFromHash
10ba3 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 (p);. pcache1
10ba4 46 72 65 65 50 61 67 65 28 70 29 3b 0a 20 20 7d FreePage(p);. }
10ba5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 63 61 72 .}../*.** Discar
10ba6 64 20 61 6c 6c 20 70 61 67 65 73 20 66 72 6f 6d d all pages from
10ba7 20 63 61 63 68 65 20 70 43 61 63 68 65 20 77 69 cache pCache wi
10ba8 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 th a page number
10ba9 20 28 6b 65 79 20 76 61 6c 75 65 29 20 0a 2a 2a (key value) .**
10baa 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 greater than or
10bab 20 65 71 75 61 6c 20 74 6f 20 69 4c 69 6d 69 74 equal to iLimit
10bac 2e 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 . Any pinned pag
10bad 65 73 20 74 68 61 74 20 6d 65 65 74 20 74 68 69 es that meet thi
10bae 73 20 0a 2a 2a 20 63 72 69 74 65 72 69 61 20 61 s .** criteria a
10baf 72 65 20 75 6e 70 69 6e 6e 65 64 20 62 65 66 6f re unpinned befo
10bb0 72 65 20 74 68 65 79 20 61 72 65 20 64 69 73 63 re they are disc
10bb1 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 arded..**.** The
10bb2 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 global mutex mu
10bb3 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 st be held when
10bb4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
10bb5 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 called..*/.stat
10bb6 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 54 ic void pcache1T
10bb7 72 75 6e 63 61 74 65 55 6e 73 61 66 65 28 0a 20 runcateUnsafe(.
10bb8 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 PCache1 *pCache
10bb9 2c 20 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e , . unsigned in
10bba 74 20 69 4c 69 6d 69 74 20 0a 29 7b 0a 20 20 75 t iLimit .){. u
10bbb 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 nsigned int h;.
10bbc 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
10bbd 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 _mutex_held(pcac
10bbe 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 he1.mutex) );.
10bbf 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43 61 63 68 for(h=0; h<pCach
10bc0 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b 29 7b 0a e->nHash; h++){.
10bc1 20 20 20 20 50 67 48 64 72 31 20 2a 2a 70 70 20 PgHdr1 **pp
10bc2 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 = &pCache->apHas
10bc3 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67 48 64 72 h[h]; . PgHdr
10bc4 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 77 68 1 *pPage;. wh
10bc5 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 2a 70 ile( (pPage = *p
10bc6 70 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 p)!=0 ){. i
10bc7 66 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3e 3d f( pPage->iKey>=
10bc8 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 iLimit ){.
10bc9 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 pcache1PinPage
10bca 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
10bcb 20 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e *pp = pPage->pN
10bcc 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 63 61 ext;. pca
10bcd 63 68 65 31 46 72 65 65 50 61 67 65 28 70 50 61 che1FreePage(pPa
10bce 67 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ge);. }else
10bcf 7b 0a 20 20 20 20 20 20 20 20 70 70 20 3d 20 26 {. pp = &
10bd0 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 pPage->pNext;.
10bd1 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
10bd2 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
10bd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bd7 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 **/./******** sq
10bd8 6c 69 74 65 33 5f 70 63 61 63 68 65 20 4d 65 74 lite3_pcache Met
10bd9 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a hods ***********
10bda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bdc 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c ***/../*.** Impl
10bdd 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
10bde 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
10bdf 2e 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e 0a 2a .xInit method..*
10be0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 61 /.static int pca
10be1 63 68 65 31 49 6e 69 74 28 76 6f 69 64 20 2a 4e che1Init(void *N
10be2 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 otUsed){. UNUSE
10be3 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
10be4 73 65 64 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 sed);. memset(&
10be5 70 63 61 63 68 65 31 2c 20 30 2c 20 73 69 7a 65 pcache1, 0, size
10be6 6f 66 28 70 63 61 63 68 65 31 29 29 3b 0a 20 20 of(pcache1));.
10be7 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
10be8 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 lConfig.bCoreMut
10be9 65 78 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 ex ){. pcache
10bea 31 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 1.mutex = sqlite
10beb 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 3_mutex_alloc(SQ
10bec 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
10bed 43 5f 4c 52 55 29 3b 0a 20 20 7d 0a 20 20 72 65 C_LRU);. }. re
10bee 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
10bef 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
10bf0 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ntation of the s
10bf1 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53 qlite3_pcache.xS
10bf2 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a hutdown method..
10bf3 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
10bf4 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76 cache1Shutdown(v
10bf5 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 oid *NotUsed){.
10bf6 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
10bf7 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2f 2a R(NotUsed);. /*
10bf8 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a 0a 2f 2a 0a no-op */.}../*.
10bf9 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
10bfa 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
10bfb 5f 70 63 61 63 68 65 2e 78 43 72 65 61 74 65 20 _pcache.xCreate
10bfc 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c method..**.** Al
10bfd 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 63 61 63 locate a new cac
10bfe 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 he..*/.static sq
10bff 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 63 lite3_pcache *pc
10c00 61 63 68 65 31 43 72 65 61 74 65 28 69 6e 74 20 ache1Create(int
10c01 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 50 75 72 szPage, int bPur
10c02 67 65 61 62 6c 65 29 7b 0a 20 20 50 43 61 63 68 geable){. PCach
10c03 65 31 20 2a 70 43 61 63 68 65 3b 0a 0a 20 20 70 e1 *pCache;.. p
10c04 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 Cache = (PCache1
10c05 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f *)sqlite3_mallo
10c06 63 28 73 69 7a 65 6f 66 28 50 43 61 63 68 65 31 c(sizeof(PCache1
10c07 29 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 ));. if( pCache
10c08 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 ){. memset(p
10c09 43 61 63 68 65 2c 20 30 2c 20 73 69 7a 65 6f 66 Cache, 0, sizeof
10c0a 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20 20 20 (PCache1));.
10c0b 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 3d pCache->szPage =
10c0c 20 73 7a 50 61 67 65 3b 0a 20 20 20 20 70 43 61 szPage;. pCa
10c0d 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 che->bPurgeable
10c0e 3d 20 28 62 50 75 72 67 65 61 62 6c 65 20 3f 20 = (bPurgeable ?
10c0f 31 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20 1 : 0);. if(
10c10 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 bPurgeable ){.
10c11 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e pCache->nMin
10c12 20 3d 20 31 30 3b 0a 20 20 20 20 20 20 70 63 61 = 10;. pca
10c13 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10c14 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e ;. pcache1.
10c15 6e 4d 69 6e 50 61 67 65 20 2b 3d 20 70 43 61 63 nMinPage += pCac
10c16 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 20 20 20 20 he->nMin;.
10c17 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 pcache1LeaveMute
10c18 78 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 x();. }. }.
10c19 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 return (sqlite3
10c1a 5f 70 63 61 63 68 65 20 2a 29 70 43 61 63 68 65 _pcache *)pCache
10c1b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
10c1c 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
10c1d 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e sqlite3_pcache.
10c1e 78 43 61 63 68 65 73 69 7a 65 20 6d 65 74 68 6f xCachesize metho
10c1f 64 2e 20 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 d. .**.** Config
10c20 75 72 65 20 74 68 65 20 63 61 63 68 65 5f 73 69 ure the cache_si
10c21 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 61 20 63 ze limit for a c
10c22 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ache..*/.static
10c23 76 6f 69 64 20 70 63 61 63 68 65 31 43 61 63 68 void pcache1Cach
10c24 65 73 69 7a 65 28 73 71 6c 69 74 65 33 5f 70 63 esize(sqlite3_pc
10c25 61 63 68 65 20 2a 70 2c 20 69 6e 74 20 6e 4d 61 ache *p, int nMa
10c26 78 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 x){. PCache1 *p
10c27 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 Cache = (PCache1
10c28 20 2a 29 70 3b 0a 20 20 69 66 28 20 70 43 61 63 *)p;. if( pCac
10c29 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 he->bPurgeable )
10c2a 7b 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 {. pcache1Ent
10c2b 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 70 erMutex();. p
10c2c 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 cache1.nMaxPage
10c2d 2b 3d 20 28 6e 4d 61 78 20 2d 20 70 43 61 63 68 += (nMax - pCach
10c2e 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20 20 70 43 e->nMax);. pC
10c2f 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20 6e 4d 61 ache->nMax = nMa
10c30 78 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e x;. pcache1En
10c31 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 3b 0a forceMaxPage();.
10c32 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 pcache1Leave
10c33 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 7d 0a 0a Mutex();. }.}..
10c34 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
10c35 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
10c36 74 65 33 5f 70 63 61 63 68 65 2e 78 50 61 67 65 te3_pcache.xPage
10c37 63 6f 75 6e 74 20 6d 65 74 68 6f 64 2e 20 0a 2a count method. .*
10c38 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 61 /.static int pca
10c39 63 68 65 31 50 61 67 65 63 6f 75 6e 74 28 73 71 che1Pagecount(sq
10c3a 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29 lite3_pcache *p)
10c3b 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 70 63 61 {. int n;. pca
10c3c 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10c3d 3b 0a 20 20 6e 20 3d 20 28 28 50 43 61 63 68 65 ;. n = ((PCache
10c3e 31 20 2a 29 70 29 2d 3e 6e 50 61 67 65 3b 0a 20 1 *)p)->nPage;.
10c3f 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10c40 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e ex();. return n
10c41 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
10c42 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
10c43 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e sqlite3_pcache.
10c44 78 46 65 74 63 68 20 6d 65 74 68 6f 64 2e 20 0a xFetch method. .
10c45 2a 2a 0a 2a 2a 20 46 65 74 63 68 20 61 20 70 61 **.** Fetch a pa
10c46 67 65 20 62 79 20 6b 65 79 20 76 61 6c 75 65 2e ge by key value.
10c47 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f .**.** Whether o
10c48 72 20 6e 6f 74 20 61 20 6e 65 77 20 70 61 67 65 r not a new page
10c49 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 may be allocate
10c4a 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
10c4b 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 0a 2a 2a on depends on.**
10c4c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
10c4d 65 20 63 72 65 61 74 65 46 6c 61 67 20 61 72 67 e createFlag arg
10c4e 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ument..**.** The
10c4f 72 65 20 61 72 65 20 74 68 72 65 65 20 64 69 66 re are three dif
10c50 66 65 72 65 6e 74 20 61 70 70 72 6f 61 63 68 65 ferent approache
10c51 73 20 74 6f 20 6f 62 74 61 69 6e 69 6e 67 20 73 s to obtaining s
10c52 70 61 63 65 20 66 6f 72 20 61 20 70 61 67 65 2c pace for a page,
10c53 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e .** depending on
10c54 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61 the value of pa
10c55 72 61 6d 65 74 65 72 20 63 72 65 61 74 65 46 6c rameter createFl
10c56 61 67 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 ag (which may be
10c57 20 30 2c 20 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 0, 1 or 2)..**.
10c58 2a 2a 20 20 20 31 2e 20 52 65 67 61 72 64 6c 65 ** 1. Regardle
10c59 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 ss of the value
10c5a 6f 66 20 63 72 65 61 74 65 46 6c 61 67 2c 20 74 of createFlag, t
10c5b 68 65 20 63 61 63 68 65 20 69 73 20 73 65 61 72 he cache is sear
10c5c 63 68 65 64 20 66 6f 72 20 61 20 0a 2a 2a 20 20 ched for a .**
10c5d 20 20 20 20 63 6f 70 79 20 6f 66 20 74 68 65 20 copy of the
10c5e 72 65 71 75 65 73 74 65 64 20 70 61 67 65 2e 20 requested page.
10c5f 49 66 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c If one is found,
10c60 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e it is returned.
10c61 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 49 66 20 63 .**.** 2. If c
10c62 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 61 6e 64 reateFlag==0 and
10c63 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
10c64 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
10c65 63 61 63 68 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a cache, NULL is.*
10c66 2a 20 20 20 20 20 20 72 65 74 75 72 6e 65 64 2e * returned.
10c67 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63 .**.** 3. If c
10c68 72 65 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20 reateFlag is 1,
10c69 74 68 65 20 63 61 63 68 65 20 69 73 20 6d 61 72 the cache is mar
10c6a 6b 65 64 20 61 73 20 70 75 72 67 65 61 62 6c 65 ked as purgeable
10c6b 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 and the page is
10c6c 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 74 20 61 6c .** not al
10c6d 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 ready in the cac
10c6e 68 65 2c 20 61 6e 64 20 69 66 20 65 69 74 68 65 he, and if eithe
10c6f 72 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 r of the followi
10c70 6e 67 20 61 72 65 20 74 72 75 65 2c 20 0a 2a 2a ng are true, .**
10c71 20 20 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c return NUL
10c72 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 L:.**.** (
10c73 61 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 a) the number of
10c74 20 70 61 67 65 73 20 70 69 6e 6e 65 64 20 62 79 pages pinned by
10c75 20 74 68 65 20 63 61 63 68 65 20 69 73 20 67 72 the cache is gr
10c76 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 eater than.**
10c77 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31 2e PCache1.
10c78 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 nMax, or.**
10c79 20 20 28 62 29 20 74 68 65 20 6e 75 6d 62 65 72 (b) the number
10c7a 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64 of pages pinned
10c7b 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 73 by the cache is
10c7c 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a greater than.**
10c7d 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 the s
10c7e 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72 20 61 um of nMax for a
10c7f 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 ll purgeable cac
10c80 68 65 73 2c 20 6c 65 73 73 20 74 68 65 20 73 75 hes, less the su
10c81 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 m of .**
10c82 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61 6c 6c 20 nMin for all
10c83 6f 74 68 65 72 20 70 75 72 67 65 61 62 6c 65 20 other purgeable
10c84 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 20 caches. .**.**
10c85 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 4. If none of t
10c86 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 63 he first three c
10c87 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70 6c 79 20 onditions apply
10c88 61 6e 64 20 74 68 65 20 63 61 63 68 65 20 69 73 and the cache is
10c89 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20 20 20 20 marked.**
10c8a 61 73 20 70 75 72 67 65 61 62 6c 65 2c 20 61 6e as purgeable, an
10c8b 64 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 d if one of the
10c8c 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 following is tru
10c8d 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 e:.**.** (
10c8e 61 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 a) The number of
10c8f 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 pages allocated
10c90 20 66 6f 72 20 74 68 65 20 63 61 63 68 65 20 69 for the cache i
10c91 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 20 20 s already .**
10c92 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31 2e PCache1.
10c93 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 nMax, or.**.**
10c94 20 20 20 20 20 28 62 29 20 54 68 65 20 6e 75 6d (b) The num
10c95 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c ber of pages all
10c96 6f 63 61 74 65 64 20 66 6f 72 20 61 6c 6c 20 70 ocated for all p
10c97 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 urgeable caches
10c98 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 is.**
10c99 61 6c 72 65 61 64 79 20 65 71 75 61 6c 20 74 6f already equal to
10c9a 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e or greater than
10c9b 20 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 the sum of nMax
10c9c 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 for all.**
10c9d 20 20 20 20 20 20 70 75 72 67 65 61 62 6c 65 20 purgeable
10c9e 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a 20 20 20 caches,.**.**
10c9f 20 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 then attempt
10ca0 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67 to recycle a pag
10ca1 65 20 66 72 6f 6d 20 74 68 65 20 4c 52 55 20 6c e from the LRU l
10ca2 69 73 74 2e 20 49 66 20 69 74 20 69 73 20 74 68 ist. If it is th
10ca3 65 20 72 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 e right.**
10ca4 73 69 7a 65 2c 20 72 65 74 75 72 6e 20 74 68 65 size, return the
10ca5 20 72 65 63 79 63 6c 65 64 20 62 75 66 66 65 72 recycled buffer
10ca6 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 72 65 . Otherwise, fre
10ca7 65 20 74 68 65 20 62 75 66 66 65 72 20 61 6e 64 e the buffer and
10ca8 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63 65 65 64 .** proceed
10ca9 20 74 6f 20 73 74 65 70 20 35 2e 20 0a 2a 2a 0a to step 5. .**.
10caa 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72 77 69 73 ** 5. Otherwis
10cab 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 e, allocate and
10cac 72 65 74 75 72 6e 20 61 20 6e 65 77 20 70 61 67 return a new pag
10cad 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 e buffer..*/.sta
10cae 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65 tic void *pcache
10caf 31 46 65 74 63 68 28 73 71 6c 69 74 65 33 5f 70 1Fetch(sqlite3_p
10cb0 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 67 6e cache *p, unsign
10cb1 65 64 20 69 6e 74 20 69 4b 65 79 2c 20 69 6e 74 ed int iKey, int
10cb2 20 63 72 65 61 74 65 46 6c 61 67 29 7b 0a 20 20 createFlag){.
10cb3 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 69 unsigned int nPi
10cb4 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68 65 31 20 nned;. PCache1
10cb5 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 *pCache = (PCach
10cb6 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 e1 *)p;. PgHdr1
10cb7 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 *pPage = 0;..
10cb8 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
10cb9 78 28 29 3b 0a 20 20 69 66 28 20 63 72 65 61 74 x();. if( creat
10cba 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 eFlag==1 ) sqlit
10cbb 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
10cbc 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 loc();.. /* Sea
10cbd 72 63 68 20 74 68 65 20 68 61 73 68 20 74 61 62 rch the hash tab
10cbe 6c 65 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 le for an existi
10cbf 6e 67 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 69 ng entry. */. i
10cc0 66 28 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 f( pCache->nHash
10cc1 3e 30 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e >0 ){. unsign
10cc2 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 20 ed int h = iKey
10cc3 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b % pCache->nHash;
10cc4 0a 20 20 20 20 66 6f 72 28 70 50 61 67 65 3d 70 . for(pPage=p
10cc5 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d Cache->apHash[h]
10cc6 3b 20 70 50 61 67 65 26 26 70 50 61 67 65 2d 3e ; pPage&&pPage->
10cc7 69 4b 65 79 21 3d 69 4b 65 79 3b 20 70 50 61 67 iKey!=iKey; pPag
10cc8 65 3d 70 50 61 67 65 2d 3e 70 4e 65 78 74 29 3b e=pPage->pNext);
10cc9 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 . }.. if( pPag
10cca 65 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 3d e || createFlag=
10ccb 3d 30 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 =0 ){. pcache
10ccc 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 1PinPage(pPage);
10ccd 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f . goto fetch_
10cce 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 out;. }.. /* S
10ccf 74 65 70 20 33 20 6f 66 20 68 65 61 64 65 72 20 tep 3 of header
10cd0 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a 20 20 6e 50 comment. */. nP
10cd1 69 6e 6e 65 64 20 3d 20 70 43 61 63 68 65 2d 3e inned = pCache->
10cd2 6e 50 61 67 65 20 2d 20 70 43 61 63 68 65 2d 3e nPage - pCache->
10cd3 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a 20 20 69 nRecyclable;. i
10cd4 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 f( createFlag==1
10cd5 20 26 26 20 70 43 61 63 68 65 2d 3e 62 50 75 72 && pCache->bPur
10cd6 67 65 61 62 6c 65 20 26 26 20 28 0a 20 20 20 20 geable && (.
10cd7 20 20 20 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 nPinned>=(pc
10cd8 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 2b 70 ache1.nMaxPage+p
10cd9 43 61 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 Cache->nMin-pcac
10cda 68 65 31 2e 6e 4d 69 6e 50 61 67 65 29 0a 20 20 he1.nMinPage).
10cdb 20 20 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 || nPinned>=(
10cdc 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 0a 20 20 pCache->nMax).
10cdd 29 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 )){. goto fet
10cde 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 69 ch_out;. }.. i
10cdf 66 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 f( pCache->nPage
10ce0 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 20 >=pCache->nHash
10ce1 26 26 20 70 63 61 63 68 65 31 52 65 73 69 7a 65 && pcache1Resize
10ce2 48 61 73 68 28 70 43 61 63 68 65 29 20 29 7b 0a Hash(pCache) ){.
10ce3 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f goto fetch_o
10ce4 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 ut;. }.. /* St
10ce5 65 70 20 34 2e 20 54 72 79 20 74 6f 20 72 65 63 ep 4. Try to rec
10ce6 79 63 6c 65 20 61 20 70 61 67 65 20 62 75 66 66 ycle a page buff
10ce7 65 72 20 69 66 20 61 70 70 72 6f 70 72 69 61 74 er if appropriat
10ce8 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 e. */. if( pCac
10ce9 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26 he->bPurgeable &
10cea 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 & pcache1.pLruTa
10ceb 69 6c 20 26 26 20 28 0a 20 20 20 20 20 20 70 43 il && (. pC
10cec 61 63 68 65 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 ache->nPage>=pCa
10ced 63 68 65 2d 3e 6e 4d 61 78 2d 31 20 7c 7c 20 70 che->nMax-1 || p
10cee 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 cache1.nCurrentP
10cef 61 67 65 3e 3d 70 63 61 63 68 65 31 2e 6e 4d 61 age>=pcache1.nMa
10cf0 78 50 61 67 65 0a 20 20 29 29 7b 0a 20 20 20 20 xPage. )){.
10cf1 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31 2e pPage = pcache1.
10cf2 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 pLruTail;. pc
10cf3 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 ache1RemoveFromH
10cf4 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ash(pPage);.
10cf5 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 pcache1PinPage(p
10cf6 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 70 Page);. if( p
10cf7 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e 73 7a Page->pCache->sz
10cf8 50 61 67 65 21 3d 70 43 61 63 68 65 2d 3e 73 7a Page!=pCache->sz
10cf9 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63 Page ){. pc
10cfa 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 50 ache1FreePage(pP
10cfb 61 67 65 29 3b 0a 20 20 20 20 20 20 70 50 61 67 age);. pPag
10cfc 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 e = 0;. }else
10cfd 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e {. pcache1.
10cfe 6e 43 75 72 72 65 6e 74 50 61 67 65 20 2d 3d 20 nCurrentPage -=
10cff 28 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e (pPage->pCache->
10d00 62 50 75 72 67 65 61 62 6c 65 20 2d 20 70 43 61 bPurgeable - pCa
10d01 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 29 che->bPurgeable)
10d02 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
10d03 2a 20 53 74 65 70 20 35 2e 20 49 66 20 61 20 75 * Step 5. If a u
10d04 73 61 62 6c 65 20 70 61 67 65 20 62 75 66 66 65 sable page buffe
10d05 72 20 68 61 73 20 73 74 69 6c 6c 20 6e 6f 74 20 r has still not
10d06 62 65 65 6e 20 66 6f 75 6e 64 2c 20 0a 20 20 2a been found, . *
10d07 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c * attempt to all
10d08 6f 63 61 74 65 20 61 20 6e 65 77 20 6f 6e 65 2e ocate a new one.
10d09 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 . */. if( !pP
10d0a 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 age ){. pPage
10d0b 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 = pcache1AllocP
10d0c 61 67 65 28 70 43 61 63 68 65 29 3b 0a 20 20 7d age(pCache);. }
10d0d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b .. if( pPage ){
10d0e 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e . unsigned in
10d0f 74 20 68 20 3d 20 69 4b 65 79 20 25 20 70 43 61 t h = iKey % pCa
10d10 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 20 20 che->nHash;.
10d11 6d 65 6d 73 65 74 28 70 50 61 67 65 2c 20 30 2c memset(pPage, 0,
10d12 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 pCache->szPage
10d13 2b 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29 + sizeof(PgHdr1)
10d14 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e );. pCache->n
10d15 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61 67 Page++;. pPag
10d16 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b 0a e->iKey = iKey;.
10d17 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 pPage->pNext
10d18 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 = pCache->apHas
10d19 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 2d h[h];. pPage-
10d1a 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 65 >pCache = pCache
10d1b 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 61 70 ;. pCache->ap
10d1c 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b Hash[h] = pPage;
10d1d 0a 20 20 7d 0a 0a 66 65 74 63 68 5f 6f 75 74 3a . }..fetch_out:
10d1e 0a 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20 . if( pPage &&
10d1f 69 4b 65 79 3e 70 43 61 63 68 65 2d 3e 69 4d 61 iKey>pCache->iMa
10d20 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63 xKey ){. pCac
10d21 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4b he->iMaxKey = iK
10d22 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 72 ey;. }. if( cr
10d23 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 eateFlag==1 ) sq
10d24 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
10d25 6c 6c 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65 lloc();. pcache
10d26 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 1LeaveMutex();.
10d27 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f return (pPage ?
10d28 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 PGHDR1_TO_PAGE(
10d29 70 50 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a pPage) : 0);.}..
10d2a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
10d2b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
10d2c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70 ite3_pcache.xUnp
10d2d 69 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a in method..**.**
10d2e 20 4d 61 72 6b 20 61 20 70 61 67 65 20 61 73 20 Mark a page as
10d2f 75 6e 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62 unpinned (eligib
10d30 6c 65 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e le for asynchron
10d31 6f 75 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a ous recycling)..
10d32 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
10d33 63 61 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69 cache1Unpin(sqli
10d34 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76 te3_pcache *p, v
10d35 6f 69 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65 oid *pPg, int re
10d36 75 73 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 useUnlikely){.
10d37 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 PCache1 *pCache
10d38 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a = (PCache1 *)p;.
10d39 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 PgHdr1 *pPage
10d3a 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 = PAGE_TO_PGHDR1
10d3b 28 70 50 67 29 3b 0a 0a 20 20 70 63 61 63 68 65 (pPg);.. pcache
10d3c 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 1EnterMutex();..
10d3d 20 20 2f 2a 20 49 74 20 69 73 20 61 6e 20 65 72 /* It is an er
10d3e 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 ror to call this
10d3f 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 function if the
10d40 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 page is already
10d41 20 0a 20 20 2a 2a 20 70 61 72 74 20 6f 66 20 74 . ** part of t
10d42 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 he global LRU li
10d43 73 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 st.. */. asser
10d44 74 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 t( pPage->pLruPr
10d45 65 76 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e ev==0 && pPage->
10d46 70 4c 72 75 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 pLruNext==0 );.
10d47 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 assert( pcache1
10d48 2e 70 4c 72 75 48 65 61 64 21 3d 70 50 61 67 65 .pLruHead!=pPage
10d49 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 && pcache1.pLru
10d4a 54 61 69 6c 21 3d 70 50 61 67 65 20 29 3b 0a 0a Tail!=pPage );..
10d4b 20 20 69 66 28 20 72 65 75 73 65 55 6e 6c 69 6b if( reuseUnlik
10d4c 65 6c 79 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e ely || pcache1.n
10d4d 43 75 72 72 65 6e 74 50 61 67 65 3e 70 63 61 63 CurrentPage>pcac
10d4e 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 29 7b 0a he1.nMaxPage ){.
10d4f 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 pcache1Remov
10d50 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29 eFromHash(pPage)
10d51 3b 0a 20 20 20 20 70 63 61 63 68 65 31 46 72 65 ;. pcache1Fre
10d52 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
10d53 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 41 64 }else{. /* Ad
10d54 64 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 d the page to th
10d55 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 e global LRU lis
10d56 74 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 65 t. Normally, the
10d57 20 70 61 67 65 20 69 73 20 61 64 64 65 64 20 74 page is added t
10d58 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 68 65 61 o. ** the hea
10d59 64 20 6f 66 20 74 68 65 20 6c 69 73 74 20 28 6c d of the list (l
10d5a 61 73 74 20 70 61 67 65 20 74 6f 20 62 65 20 72 ast page to be r
10d5b 65 63 79 63 6c 65 64 29 2e 20 48 6f 77 65 76 65 ecycled). Howeve
10d5c 72 2c 20 69 66 20 74 68 65 20 0a 20 20 20 20 2a r, if the . *
10d5d 2a 20 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 * reuseUnlikely
10d5e 66 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 74 flag passed to t
10d5f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
10d60 74 72 75 65 2c 20 74 68 65 20 70 61 67 65 20 69 true, the page i
10d61 73 20 61 64 64 65 64 0a 20 20 20 20 2a 2a 20 74 s added. ** t
10d62 6f 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 o the tail of th
10d63 65 20 6c 69 73 74 20 28 66 69 72 73 74 20 70 61 e list (first pa
10d64 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 ge to be recycle
10d65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 d).. */. i
10d66 66 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 f( pcache1.pLruH
10d67 65 61 64 20 29 7b 0a 20 20 20 20 20 20 70 63 61 ead ){. pca
10d68 63 68 65 31 2e 70 4c 72 75 48 65 61 64 2d 3e 70 che1.pLruHead->p
10d69 4c 72 75 50 72 65 76 20 3d 20 70 50 61 67 65 3b LruPrev = pPage;
10d6a 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4c . pPage->pL
10d6b 72 75 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 ruNext = pcache1
10d6c 2e 70 4c 72 75 48 65 61 64 3b 0a 20 20 20 20 20 .pLruHead;.
10d6d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 pcache1.pLruHea
10d6e 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d d = pPage;. }
10d6f 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 61 63 else{. pcac
10d70 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 3d 20 70 he1.pLruTail = p
10d71 50 61 67 65 3b 0a 20 20 20 20 20 20 70 63 61 63 Page;. pcac
10d72 68 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 he1.pLruHead = p
10d73 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Page;. }.
10d74 70 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 pCache->nRecycla
10d75 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 70 63 ble++;. }.. pc
10d76 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 ache1LeaveMutex(
10d77 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c );.}../*.** Impl
10d78 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
10d79 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
10d7a 2e 78 52 65 6b 65 79 20 6d 65 74 68 6f 64 2e 20 .xRekey method.
10d7b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10d7c 70 63 61 63 68 65 31 52 65 6b 65 79 28 0a 20 20 pcache1Rekey(.
10d7d 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
10d7e 70 2c 0a 20 20 76 6f 69 64 20 2a 70 50 67 2c 0a p,. void *pPg,.
10d7f 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 unsigned int i
10d80 4f 6c 64 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 Old,. unsigned
10d81 69 6e 74 20 69 4e 65 77 0a 29 7b 0a 20 20 50 43 int iNew.){. PC
10d82 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 ache1 *pCache =
10d83 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 (PCache1 *)p;.
10d84 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 PgHdr1 *pPage =
10d85 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 PAGE_TO_PGHDR1(p
10d86 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a Pg);. PgHdr1 **
10d87 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 pp;. unsigned i
10d88 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72 74 28 nt h; . assert(
10d89 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f pPage->iKey==iO
10d8a 6c 64 20 29 3b 0a 0a 20 20 70 63 61 63 68 65 31 ld );.. pcache1
10d8b 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 EnterMutex();..
10d8c 20 68 20 3d 20 69 4f 6c 64 25 70 43 61 63 68 65 h = iOld%pCache
10d8d 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 70 20 3d 20 ->nHash;. pp =
10d8e 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b &pCache->apHash[
10d8f 68 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28 2a 70 h];. while( (*p
10d90 70 29 21 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 p)!=pPage ){.
10d91 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e pp = &(*pp)->pN
10d92 65 78 74 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d ext;. }. *pp =
10d93 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 0a pPage->pNext;..
10d94 20 20 68 20 3d 20 69 4e 65 77 25 70 43 61 63 68 h = iNew%pCach
10d95 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 50 61 67 e->nHash;. pPag
10d96 65 2d 3e 69 4b 65 79 20 3d 20 69 4e 65 77 3b 0a e->iKey = iNew;.
10d97 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d pPage->pNext =
10d98 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b pCache->apHash[
10d99 68 5d 3b 0a 20 20 70 43 61 63 68 65 2d 3e 61 70 h];. pCache->ap
10d9a 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b Hash[h] = pPage;
10d9b 0a 0a 20 20 69 66 28 20 69 4e 65 77 3e 70 43 61 .. if( iNew>pCa
10d9c 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a che->iMaxKey ){.
10d9d 20 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 pCache->iMax
10d9e 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 20 7d 0a Key = iNew;. }.
10d9f 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
10da0 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a utex();.}../*.**
10da1 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
10da2 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 of the sqlite3_p
10da3 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 20 cache.xTruncate
10da4 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 method. .**.** D
10da5 69 73 63 61 72 64 20 61 6c 6c 20 75 6e 70 69 6e iscard all unpin
10da6 6e 65 64 20 70 61 67 65 73 20 69 6e 20 74 68 65 ned pages in the
10da7 20 63 61 63 68 65 20 77 69 74 68 20 61 20 70 61 cache with a pa
10da8 67 65 20 6e 75 6d 62 65 72 20 65 71 75 61 6c 20 ge number equal
10da9 74 6f 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 to.** or greater
10daa 20 74 68 61 6e 20 70 61 72 61 6d 65 74 65 72 20 than parameter
10dab 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e iLimit. Any pinn
10dac 65 64 20 70 61 67 65 73 20 77 69 74 68 20 61 20 ed pages with a
10dad 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 65 page number.** e
10dae 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 qual to or great
10daf 65 72 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 61 er than iLimit a
10db0 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e re implicitly un
10db1 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 pinned..*/.stati
10db2 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 54 72 c void pcache1Tr
10db3 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 70 uncate(sqlite3_p
10db4 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 67 6e cache *p, unsign
10db5 65 64 20 69 6e 74 20 69 4c 69 6d 69 74 29 7b 0a ed int iLimit){.
10db6 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
10db7 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 e = (PCache1 *)p
10db8 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10db9 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 69 Mutex();. if( i
10dba 4c 69 6d 69 74 3c 3d 70 43 61 63 68 65 2d 3e 69 Limit<=pCache->i
10dbb 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 63 MaxKey ){. pc
10dbc 61 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 ache1TruncateUns
10dbd 61 66 65 28 70 43 61 63 68 65 2c 20 69 4c 69 6d afe(pCache, iLim
10dbe 69 74 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d it);. pCache-
10dbf 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4c 69 6d 69 >iMaxKey = iLimi
10dc0 74 2d 31 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 t-1;. }. pcach
10dc1 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a e1LeaveMutex();.
10dc2 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
10dc3 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ntation of the s
10dc4 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 44 qlite3_pcache.xD
10dc5 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 2e 20 0a estroy method. .
10dc6 2a 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 **.** Destroy a
10dc7 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 cache allocated
10dc8 75 73 69 6e 67 20 70 63 61 63 68 65 31 43 72 65 using pcache1Cre
10dc9 61 74 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 ate()..*/.static
10dca 20 76 6f 69 64 20 70 63 61 63 68 65 31 44 65 73 void pcache1Des
10dcb 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 70 63 61 troy(sqlite3_pca
10dcc 63 68 65 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 che *p){. PCach
10dcd 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 e1 *pCache = (PC
10dce 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61 ache1 *)p;. pca
10dcf 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10dd0 3b 0a 20 20 70 63 61 63 68 65 31 54 72 75 6e 63 ;. pcache1Trunc
10dd1 61 74 65 55 6e 73 61 66 65 28 70 43 61 63 68 65 ateUnsafe(pCache
10dd2 2c 20 30 29 3b 0a 20 20 70 63 61 63 68 65 31 2e , 0);. pcache1.
10dd3 6e 4d 61 78 50 61 67 65 20 2d 3d 20 70 43 61 63 nMaxPage -= pCac
10dd4 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20 70 63 61 63 he->nMax;. pcac
10dd5 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 2d 3d 20 he1.nMinPage -=
10dd6 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 pCache->nMin;.
10dd7 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 pcache1EnforceMa
10dd8 78 50 61 67 65 28 29 3b 0a 20 20 70 63 61 63 68 xPage();. pcach
10dd9 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a e1LeaveMutex();.
10dda 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
10ddb 43 61 63 68 65 2d 3e 61 70 48 61 73 68 29 3b 0a Cache->apHash);.
10ddc 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
10ddd 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Cache);.}../*.**
10dde 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
10ddf 73 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 s called during
10de0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 28 initialization (
10de1 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
10de2 7a 65 28 29 29 20 74 6f 0a 2a 2a 20 69 6e 73 74 ze()) to.** inst
10de3 61 6c 6c 20 74 68 65 20 64 65 66 61 75 6c 74 20 all the default
10de4 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 pluggable cache
10de5 6d 6f 64 75 6c 65 2c 20 61 73 73 75 6d 69 6e 67 module, assuming
10de6 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6e 6f the user has no
10de7 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 70 72 6f t.** already pro
10de8 76 69 64 65 64 20 61 6e 20 61 6c 74 65 72 6e 61 vided an alterna
10de9 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tive..*/.SQLITE_
10dea 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
10deb 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 ite3PCacheSetDef
10dec 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 ault(void){. st
10ded 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61 atic sqlite3_pca
10dee 63 68 65 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 che_methods defa
10def 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 ultMethods = {.
10df0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
10df1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 /* p
10df2 41 72 67 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 Arg */. pcach
10df3 65 31 49 6e 69 74 2c 20 20 20 20 20 20 20 20 20 e1Init,
10df4 20 20 20 20 2f 2a 20 78 49 6e 69 74 20 2a 2f 0a /* xInit */.
10df5 20 20 20 20 70 63 61 63 68 65 31 53 68 75 74 64 pcache1Shutd
10df6 6f 77 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 own, /*
10df7 78 53 68 75 74 64 6f 77 6e 20 2a 2f 0a 20 20 20 xShutdown */.
10df8 20 70 63 61 63 68 65 31 43 72 65 61 74 65 2c 20 pcache1Create,
10df9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72 /* xCr
10dfa 65 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 eate */. pcac
10dfb 68 65 31 43 61 63 68 65 73 69 7a 65 2c 20 20 20 he1Cachesize,
10dfc 20 20 20 20 20 2f 2a 20 78 43 61 63 68 65 73 69 /* xCachesi
10dfd 7a 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 ze */. pcache
10dfe 31 50 61 67 65 63 6f 75 6e 74 2c 20 20 20 20 20 1Pagecount,
10dff 20 20 20 2f 2a 20 78 50 61 67 65 63 6f 75 6e 74 /* xPagecount
10e00 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 46 */. pcache1F
10e01 65 74 63 68 2c 20 20 20 20 20 20 20 20 20 20 20 etch,
10e02 20 2f 2a 20 78 46 65 74 63 68 20 2a 2f 0a 20 20 /* xFetch */.
10e03 20 20 70 63 61 63 68 65 31 55 6e 70 69 6e 2c 20 pcache1Unpin,
10e04 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 /* xU
10e05 6e 70 69 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63 npin */. pcac
10e06 68 65 31 52 65 6b 65 79 2c 20 20 20 20 20 20 20 he1Rekey,
10e07 20 20 20 20 20 2f 2a 20 78 52 65 6b 65 79 20 2a /* xRekey *
10e08 2f 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75 /. pcache1Tru
10e09 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 2f ncate, /
10e0a 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a 20 * xTruncate */.
10e0b 20 20 20 70 63 61 63 68 65 31 44 65 73 74 72 6f pcache1Destro
10e0c 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 y /* x
10e0d 44 65 73 74 72 6f 79 20 2a 2f 0a 20 20 7d 3b 0a Destroy */. };.
10e0e 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 sqlite3_config
10e0f 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 (SQLITE_CONFIG_P
10e10 43 41 43 48 45 2c 20 26 64 65 66 61 75 6c 74 4d CACHE, &defaultM
10e11 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 69 66 64 ethods);.}..#ifd
10e12 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
10e13 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
10e14 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 NT./*.** This fu
10e15 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
10e16 20 74 6f 20 66 72 65 65 20 73 75 70 65 72 66 6c to free superfl
10e17 75 6f 75 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 uous dynamically
10e18 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 allocated memor
10e19 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 74 68 65 y.** held by the
10e1a 20 70 61 67 65 72 20 73 79 73 74 65 6d 2e 20 4d pager system. M
10e1b 65 6d 6f 72 79 20 69 6e 20 75 73 65 20 62 79 20 emory in use by
10e1c 61 6e 79 20 53 51 4c 69 74 65 20 70 61 67 65 72 any SQLite pager
10e1d 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79 allocated.** by
10e1e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 the current thr
10e1f 65 61 64 20 6d 61 79 20 62 65 20 73 71 6c 69 74 ead may be sqlit
10e20 65 33 5f 66 72 65 65 28 29 65 64 2e 0a 2a 2a 0a e3_free()ed..**.
10e21 2a 2a 20 6e 52 65 71 20 69 73 20 74 68 65 20 6e ** nReq is the n
10e22 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
10e23 66 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 f memory require
10e24 64 2e 20 4f 6e 63 65 20 74 68 69 73 20 6d 75 63 d. Once this muc
10e25 68 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 72 65 h has.** been re
10e26 6c 65 61 73 65 64 2c 20 74 68 65 20 66 75 6e 63 leased, the func
10e27 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20 54 68 tion returns. Th
10e28 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 e return value i
10e29 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 s the total numb
10e2a 65 72 20 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 er .** of bytes
10e2b 6f 66 20 6d 65 6d 6f 72 79 20 72 65 6c 65 61 73 of memory releas
10e2c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
10e2d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
10e2e 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 3PcacheReleaseMe
10e2f 6d 6f 72 79 28 69 6e 74 20 6e 52 65 71 29 7b 0a mory(int nReq){.
10e30 20 20 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b int nFree = 0;
10e31 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 . if( pcache1.p
10e32 53 74 61 72 74 3d 3d 30 20 29 7b 0a 20 20 20 20 Start==0 ){.
10e33 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 20 20 70 PgHdr1 *p;. p
10e34 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
10e35 28 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 ();. while( (
10e36 6e 52 65 71 3c 30 20 7c 7c 20 6e 46 72 65 65 3c nReq<0 || nFree<
10e37 6e 52 65 71 29 20 26 26 20 28 70 3d 70 63 61 63 nReq) && (p=pcac
10e38 68 65 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b he1.pLruTail) ){
10e39 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 . nFree +=
10e3a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a sqlite3MallocSiz
10e3b 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 e(p);. pcac
10e3c 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b 0a 20 he1PinPage(p);.
10e3d 20 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f pcache1Remo
10e3e 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 veFromHash(p);.
10e3f 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 pcache1Free
10e40 50 61 67 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20 Page(p);. }.
10e41 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d pcache1LeaveM
10e42 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 utex();. }. re
10e43 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65 turn nFree;.}.#e
10e44 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 ndif /* SQLITE_E
10e45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e NABLE_MEMORY_MAN
10e46 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a 23 69 66 64 AGEMENT */..#ifd
10e47 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f ef SQLITE_TEST./
10e48 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
10e49 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74 65 on is used by te
10e4a 73 74 20 70 72 6f 63 65 64 75 72 65 73 20 74 6f st procedures to
10e4b 20 69 6e 73 70 65 63 74 20 74 68 65 20 69 6e 74 inspect the int
10e4c 65 72 6e 61 6c 20 73 74 61 74 65 0a 2a 2a 20 6f ernal state.** o
10e4d 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 61 63 f the global cac
10e4e 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 he..*/.SQLITE_PR
10e4f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
10e50 65 33 50 63 61 63 68 65 53 74 61 74 73 28 0a 20 e3PcacheStats(.
10e51 20 69 6e 74 20 2a 70 6e 43 75 72 72 65 6e 74 2c int *pnCurrent,
10e52 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f /* OUT: To
10e53 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 tal number of pa
10e54 67 65 73 20 63 61 63 68 65 64 20 2a 2f 0a 20 20 ges cached */.
10e55 69 6e 74 20 2a 70 6e 4d 61 78 2c 20 20 20 20 20 int *pnMax,
10e56 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 47 6c 6f /* OUT: Glo
10e57 62 61 6c 20 6d 61 78 69 6d 75 6d 20 63 61 63 68 bal maximum cach
10e58 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 e size */. int
10e59 2a 70 6e 4d 69 6e 2c 20 20 20 20 20 20 20 20 20 *pnMin,
10e5a 20 2f 2a 20 4f 55 54 3a 20 53 75 6d 20 6f 66 20 /* OUT: Sum of
10e5b 50 43 61 63 68 65 31 2e 6e 4d 69 6e 20 66 6f 72 PCache1.nMin for
10e5c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 purgeable cache
10e5d 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 65 s */. int *pnRe
10e5e 63 79 63 6c 61 62 6c 65 20 20 20 20 2f 2a 20 4f cyclable /* O
10e5f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 UT: Total number
10e60 20 6f 66 20 70 61 67 65 73 20 61 76 61 69 6c 61 of pages availa
10e61 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e ble for recyclin
10e62 67 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 31 g */.){. PgHdr1
10e63 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 52 65 63 79 *p;. int nRecy
10e64 63 6c 61 62 6c 65 20 3d 20 30 3b 0a 20 20 66 6f clable = 0;. fo
10e65 72 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 r(p=pcache1.pLru
10e66 48 65 61 64 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c Head; p; p=p->pL
10e67 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 6e 52 65 ruNext){. nRe
10e68 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a cyclable++;. }.
10e69 20 20 2a 70 6e 43 75 72 72 65 6e 74 20 3d 20 70 *pnCurrent = p
10e6a 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 cache1.nCurrentP
10e6b 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 78 20 3d 20 age;. *pnMax =
10e6c 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 pcache1.nMaxPage
10e6d 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d 20 70 63 61 ;. *pnMin = pca
10e6e 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 3b 0a 20 che1.nMinPage;.
10e6f 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d *pnRecyclable =
10e70 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a 7d 0a nRecyclable;.}.
10e71 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a #endif../*******
10e72 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 ******* End of p
10e73 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a cache1.c *******
10e74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e76 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
10e77 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
10e78 6c 65 20 72 6f 77 73 65 74 2e 63 20 2a 2a 2a 2a le rowset.c ****
10e79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e7b 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
10e7c 30 38 20 44 65 63 65 6d 62 65 72 20 33 0a 2a 2a 08 December 3.**
10e7d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
10e7e 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
10e7f 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
10e80 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
10e81 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
10e82 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
10e83 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
10e84 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
10e85 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
10e86 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
10e87 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
10e88 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
10e89 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
10e8a 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
10e8b 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
10e8c 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
10e8d 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
10e8e 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
10e8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
10e93 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 ** This module i
10e94 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a mplements an obj
10e95 65 63 74 20 77 65 20 63 61 6c 6c 20 61 20 22 52 ect we call a "R
10e96 6f 77 20 53 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 ow Set"..**.** T
10e97 68 65 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 he RowSet object
10e98 20 69 73 20 61 20 62 61 67 20 6f 66 20 72 6f 77 is a bag of row
10e99 69 64 73 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20 ids. Rowids.**
10e9a 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 are inserted int
10e9b 6f 20 74 68 65 20 62 61 67 20 69 6e 20 61 6e 20 o the bag in an
10e9c 61 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 2e arbitrary order.
10e9d 20 20 54 68 65 6e 20 74 68 65 79 20 61 72 65 0a Then they are.
10e9e 2a 2a 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 74 ** pulled from t
10e9f 68 65 20 62 61 67 20 69 6e 20 73 6f 72 74 65 64 he bag in sorted
10ea0 20 6f 72 64 65 72 2e 20 20 52 6f 77 69 64 73 20 order. Rowids
10ea1 6f 6e 6c 79 20 61 70 70 65 61 72 20 69 6e 20 74 only appear in t
10ea2 68 65 0a 2a 2a 20 62 61 67 20 6f 6e 63 65 2e 20 he.** bag once.
10ea3 20 49 66 20 74 68 65 20 73 61 6d 65 20 72 6f 77 If the same row
10ea4 69 64 20 69 73 20 69 6e 73 65 72 74 65 64 20 6d id is inserted m
10ea5 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2c 20 74 ultiple times, t
10ea6 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 6e 64 he.** second and
10ea7 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73 65 subsequent inse
10ea8 72 74 73 20 6d 61 6b 65 20 6e 6f 20 64 69 66 66 rts make no diff
10ea9 65 72 65 6e 63 65 20 6f 6e 20 74 68 65 20 6f 75 erence on the ou
10eaa 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tput..**.** This
10eab 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
10eac 61 63 63 75 6d 75 6c 61 74 65 73 20 72 6f 77 69 accumulates rowi
10ead 64 73 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c ds in a linked l
10eae 69 73 74 2e 20 20 46 6f 72 0a 2a 2a 20 6f 75 74 ist. For.** out
10eaf 70 75 74 2c 20 69 74 20 66 69 72 73 74 20 73 6f put, it first so
10eb0 72 74 73 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c rts the linked l
10eb1 69 73 74 20 28 72 65 6d 6f 76 69 6e 67 20 64 75 ist (removing du
10eb2 70 6c 69 63 61 74 65 73 20 64 75 72 69 6e 67 0a plicates during.
10eb3 2a 2a 20 74 68 65 20 73 6f 72 74 29 20 74 68 65 ** the sort) the
10eb4 6e 20 72 65 74 75 72 6e 73 20 65 6c 65 6d 65 6e n returns elemen
10eb5 74 73 20 6f 6e 65 20 62 79 20 6f 6e 65 20 62 79 ts one by one by
10eb6 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 6c 69 73 walking the lis
10eb7 74 2e 0a 2a 2a 0a 2a 2a 20 42 69 67 20 63 68 75 t..**.** Big chu
10eb8 6e 6b 73 20 6f 66 20 72 6f 77 69 64 2f 6e 65 78 nks of rowid/nex
10eb9 74 2d 70 74 72 20 70 61 69 72 73 20 61 72 65 20 t-ptr pairs are
10eba 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 61 20 74 allocated at a t
10ebb 69 6d 65 2c 20 74 6f 0a 2a 2a 20 72 65 64 75 63 ime, to.** reduc
10ebc 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 6f 76 65 e the malloc ove
10ebd 72 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 rhead..**.** $Id
10ebe 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 31 2e 33 : rowset.c,v 1.3
10ebf 20 32 30 30 39 2f 30 31 2f 31 33 20 32 30 3a 31 2009/01/13 20:1
10ec0 34 3a 31 36 20 64 72 68 20 45 78 70 20 24 0a 2a 4:16 drh Exp $.*
10ec1 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d /../*.** The num
10ec2 62 65 72 20 6f 66 20 72 6f 77 73 65 74 20 65 6e ber of rowset en
10ec3 74 72 69 65 73 20 70 65 72 20 61 6c 6c 6f 63 61 tries per alloca
10ec4 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23 tion chunk..*/.#
10ec5 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 45 4e define ROWSET_EN
10ec6 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20 20 36 TRY_PER_CHUNK 6
10ec7 33 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 3../*.** Each en
10ec8 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74 20 try in a RowSet
10ec9 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
10eca 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a f the following.
10ecb 2a 2a 20 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f ** structure:.*/
10ecc 0a 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e .struct RowSetEn
10ecd 74 72 79 20 7b 20 20 20 20 20 20 20 20 20 20 20 try {
10ece 20 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 . i64 v;
10ecf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ed0 20 20 2f 2a 20 52 4f 57 49 44 20 76 61 6c 75 65 /* ROWID value
10ed1 20 66 6f 72 20 74 68 69 73 20 65 6e 74 72 79 20 for this entry
10ed2 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
10ed3 65 74 45 6e 74 72 79 20 2a 70 4e 65 78 74 3b 20 etEntry *pNext;
10ed4 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79 /* Next entry
10ed5 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c on a list of al
10ed6 6c 20 65 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a l entries */.};.
10ed7 0a 2f 2a 0a 2a 2a 20 49 6e 64 65 78 20 65 6e 74 ./*.** Index ent
10ed8 72 69 65 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 ries are allocat
10ed9 65 64 20 69 6e 20 6c 61 72 67 65 20 63 68 75 6e ed in large chun
10eda 6b 73 20 28 69 6e 73 74 61 6e 63 65 73 20 6f 66 ks (instances of
10edb 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e the.** followin
10edc 67 20 73 74 72 75 63 74 75 72 65 29 20 74 6f 20 g structure) to
10edd 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 20 61 6c reduce memory al
10ede 6c 6f 63 61 74 69 6f 6e 20 6f 76 65 72 68 65 61 location overhea
10edf 64 2e 20 20 54 68 65 0a 2a 2a 20 63 68 75 6e 6b d. The.** chunk
10ee0 73 20 61 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 s are kept on a
10ee1 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 6f 20 74 linked list so t
10ee2 68 61 74 20 74 68 65 79 20 63 61 6e 20 62 65 20 hat they can be
10ee3 64 65 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 77 deallocated.** w
10ee4 68 65 6e 20 74 68 65 20 52 6f 77 53 65 74 20 69 hen the RowSet i
10ee5 73 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a s destroyed..*/.
10ee6 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 struct RowSetChu
10ee7 6e 6b 20 7b 0a 20 20 73 74 72 75 63 74 20 52 6f nk {. struct Ro
10ee8 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e 65 78 74 wSetChunk *pNext
10ee9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
10eea 20 4e 65 78 74 20 63 68 75 6e 6b 20 6f 6e 20 6c Next chunk on l
10eeb 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 ist of them all
10eec 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
10eed 65 74 45 6e 74 72 79 20 61 45 6e 74 72 79 5b 52 etEntry aEntry[R
10eee 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f OWSET_ENTRY_PER_
10eef 43 48 55 4e 4b 5d 3b 20 2f 2a 20 41 6c 6c 6f 63 CHUNK]; /* Alloc
10ef0 61 74 65 64 20 65 6e 74 72 69 65 73 20 2a 2f 0a ated entries */.
10ef1 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 52 6f 77 53 };../*.** A RowS
10ef2 65 74 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 et in an instanc
10ef3 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
10ef4 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
10ef5 0a 2a 2a 20 41 20 74 79 70 65 64 65 66 20 6f 66 .** A typedef of
10ef6 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
10ef7 69 66 20 66 6f 75 6e 64 20 69 6e 20 73 71 6c 69 if found in sqli
10ef8 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 73 74 72 75 teInt.h..*/.stru
10ef9 63 74 20 52 6f 77 53 65 74 20 7b 0a 20 20 73 74 ct RowSet {. st
10efa 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b ruct RowSetChunk
10efb 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20 2f 2a 20 *pChunk; /*
10efc 4c 69 73 74 20 6f 66 20 61 6c 6c 20 63 68 75 6e List of all chun
10efd 6b 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f k allocations */
10efe 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 . sqlite3 *db;
10eff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10f00 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
10f01 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
10f02 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
10f03 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b 20 20 20 ntry *pEntry;
10f04 20 2f 2a 20 4c 69 73 74 20 6f 66 20 65 6e 74 72 /* List of entr
10f05 69 65 73 20 69 6e 20 74 68 65 20 72 6f 77 73 65 ies in the rowse
10f06 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f t */. struct Ro
10f07 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 61 73 74 wSetEntry *pLast
10f08 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 65 6e ; /* Last en
10f09 74 72 79 20 6f 6e 20 74 68 65 20 70 45 6e 74 72 try on the pEntr
10f0a 79 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 y list */. stru
10f0b 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
10f0c 70 46 72 65 73 68 3b 20 20 20 20 2f 2a 20 53 6f pFresh; /* So
10f0d 75 72 63 65 20 6f 66 20 6e 65 77 20 65 6e 74 72 urce of new entr
10f0e 79 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 75 y objects */. u
10f0f 31 36 20 6e 46 72 65 73 68 3b 20 20 20 20 20 20 16 nFresh;
10f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10f11 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 63 Number of objec
10f12 74 73 20 6f 6e 20 70 46 72 65 73 68 20 2a 2f 0a ts on pFresh */.
10f13 20 20 75 38 20 69 73 53 6f 72 74 65 64 3b 20 20 u8 isSorted;
10f14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10f15 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 6e 74 /* True if cont
10f16 65 6e 74 20 69 73 20 73 6f 72 74 65 64 20 2a 2f ent is sorted */
10f17 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 .};../*.** Turn
10f18 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f bulk memory into
10f19 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 a RowSet object
10f1a 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 . N bytes of me
10f1b 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 61 69 mory.** are avai
10f1c 6c 61 62 6c 65 20 61 74 20 70 53 70 61 63 65 2e lable at pSpace.
10f1d 20 20 54 68 65 20 64 62 20 70 6f 69 6e 74 65 72 The db pointer
10f1e 20 69 73 20 75 73 65 64 20 61 73 20 61 20 6d 65 is used as a me
10f1f 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 mory context.**
10f20 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75 65 for any subseque
10f21 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 nt allocations t
10f22 68 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 63 75 hat need to occu
10f23 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 r..** Return a p
10f24 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
10f25 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e w RowSet object.
10f26 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 20 62 .**.** It must b
10f27 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 e the case that
10f28 4e 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 N is sufficient
10f29 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 65 74 to make a Rowset
10f2a 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 6e 20 . If not.** an
10f2b 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 assertion fault
10f2c 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 occurs..** .** I
10f2d 66 20 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68 f N is larger th
10f2e 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20 an the minimum,
10f2f 75 73 65 20 74 68 65 20 73 75 72 70 6c 75 73 20 use the surplus
10f30 61 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a 2a as an initial.**
10f31 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65 allocation of e
10f32 6e 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65 ntries available
10f33 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0a 2a to be filled..*
10f34 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10f35 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 RowSet *sqlite3
10f36 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 RowSetInit(sqlit
10f37 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 53 e3 *db, void *pS
10f38 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20 69 pace, unsigned i
10f39 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 74 20 nt N){. RowSet
10f3a 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 20 *p;. assert( N
10f3b 3e 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b >= sizeof(*p) );
10f3c 0a 20 20 70 20 3d 20 70 53 70 61 63 65 3b 0a 20 . p = pSpace;.
10f3d 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a p->pChunk = 0;.
10f3e 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 p->db = db;.
10f3f 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 p->pEntry = 0;.
10f40 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 p->pLast = 0;.
10f41 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 28 73 74 p->pFresh = (st
10f42 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
10f43 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d 3e 6e 46 *)&p[1];. p->nF
10f44 72 65 73 68 20 3d 20 28 75 31 36 29 28 28 4e 20 resh = (u16)((N
10f45 2d 20 73 69 7a 65 6f 66 28 2a 70 29 29 2f 73 69 - sizeof(*p))/si
10f46 7a 65 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53 zeof(struct RowS
10f47 65 74 45 6e 74 72 79 29 29 3b 0a 20 20 70 2d 3e etEntry));. p->
10f48 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 20 20 isSorted = 1;.
10f49 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
10f4a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c ** Deallocate al
10f4b 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20 l chunks from a
10f4c 52 6f 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 RowSet..*/.SQLIT
10f4d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
10f4e 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 qlite3RowSetClea
10f4f 72 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 r(RowSet *p){.
10f50 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 struct RowSetChu
10f51 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65 nk *pChunk, *pNe
10f52 78 74 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 70 xtChunk;. for(p
10f53 43 68 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b 3b Chunk=p->pChunk;
10f54 20 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 20 pChunk; pChunk
10f55 3d 20 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0a 20 = pNextChunk){.
10f56 20 20 20 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 pNextChunk =
10f57 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 pChunk->pNext;.
10f58 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
10f59 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e 6b 29 3b (p->db, pChunk);
10f5a 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68 75 6e 6b . }. p->pChunk
10f5b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 = 0;. p->nFres
10f5c 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 45 6e 74 h = 0;. p->pEnt
10f5d 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 ry = 0;. p->pLa
10f5e 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 st = 0;. p->isS
10f5f 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a orted = 1;.}../*
10f60 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77 .** Insert a new
10f61 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f value into a Ro
10f62 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 wSet..**.** The
10f63 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 mallocFailed fla
10f64 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 g of the databas
10f65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
10f66 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f set if a.** memo
10f67 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ry allocation fa
10f68 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ils..*/.SQLITE_P
10f69 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
10f6a 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 te3RowSetInsert(
10f6b 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 20 72 RowSet *p, i64 r
10f6c 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 owid){. struct
10f6d 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e RowSetEntry *pEn
10f6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 52 6f try;. struct Ro
10f6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 61 73 74 wSetEntry *pLast
10f70 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
10f71 65 74 75 72 6e 3b 20 20 2f 2a 20 4d 75 73 74 20 eturn; /* Must
10f72 68 61 76 65 20 62 65 65 6e 20 61 20 6d 61 6c 6c have been a mall
10f73 6f 63 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 oc failure */.
10f74 69 66 28 20 70 2d 3e 6e 46 72 65 73 68 3d 3d 30 if( p->nFresh==0
10f75 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 ){. struct R
10f76 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e 65 77 owSetChunk *pNew
10f77 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c ;. pNew = sql
10f78 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
10f79 70 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 p->db, sizeof(*p
10f7a 4e 65 77 29 29 3b 0a 20 20 20 20 69 66 28 20 70 New));. if( p
10f7b 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 New==0 ){.
10f7c 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
10f7d 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 pNew->pNext =
10f7e 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 p->pChunk;. p
10f7f 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b ->pChunk = pNew;
10f80 0a 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20 3d . p->pFresh =
10f81 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0a 20 pNew->aEntry;.
10f82 20 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 52 p->nFresh = R
10f83 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f OWSET_ENTRY_PER_
10f84 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 6e CHUNK;. }. pEn
10f85 74 72 79 20 3d 20 70 2d 3e 70 46 72 65 73 68 2b try = p->pFresh+
10f86 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d +;. p->nFresh--
10f87 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20 ;. pEntry->v =
10f88 72 6f 77 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d rowid;. pEntry-
10f89 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70 4c >pNext = 0;. pL
10f8a 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a ast = p->pLast;.
10f8b 20 20 69 66 28 20 70 4c 61 73 74 20 29 7b 0a 20 if( pLast ){.
10f8c 20 20 20 69 66 28 20 70 2d 3e 69 73 53 6f 72 74 if( p->isSort
10f8d 65 64 20 26 26 20 72 6f 77 69 64 3c 3d 70 4c 61 ed && rowid<=pLa
10f8e 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 st->v ){. p
10f8f 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 30 3b 0a ->isSorted = 0;.
10f90 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 74 2d }. pLast-
10f91 3e 70 4e 65 78 74 20 3d 20 70 45 6e 74 72 79 3b >pNext = pEntry;
10f92 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
10f93 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 72 79 3d sert( p->pEntry=
10f94 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 70 45 6e =0 );. p->pEn
10f95 74 72 79 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 try = pEntry;.
10f96 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 70 }. p->pLast = p
10f97 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Entry;.}../*.**
10f98 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20 Merge two lists
10f99 6f 66 20 52 6f 77 53 65 74 20 65 6e 74 72 69 65 of RowSet entrie
10f9a 73 2e 20 20 52 65 6d 6f 76 65 20 64 75 70 6c 69 s. Remove dupli
10f9b 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 cates..**.** The
10f9c 20 69 6e 70 75 74 20 6c 69 73 74 73 20 61 72 65 input lists are
10f9d 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20 69 assumed to be i
10f9e 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a n sorted order..
10f9f 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 */.static struct
10fa0 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 62 6f RowSetEntry *bo
10fa1 6f 6c 69 64 78 4d 65 72 67 65 28 0a 20 20 73 74 olidxMerge(. st
10fa2 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
10fa3 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46 69 72 73 *pA, /* Firs
10fa4 74 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f t sorted list to
10fa5 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20 be merged */.
10fa6 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
10fa7 72 79 20 2a 70 42 20 20 20 20 20 2f 2a 20 53 65 ry *pB /* Se
10fa8 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69 73 74 cond sorted list
10fa9 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f to be merged */
10faa 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 .){. struct Row
10fab 53 65 74 45 6e 74 72 79 20 68 65 61 64 3b 0a 20 SetEntry head;.
10fac 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
10fad 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a 20 20 70 try *pTail;.. p
10fae 54 61 69 6c 20 3d 20 26 68 65 61 64 3b 0a 20 20 Tail = &head;.
10faf 77 68 69 6c 65 28 20 70 41 20 26 26 20 70 42 20 while( pA && pB
10fb0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
10fb1 41 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 A->pNext==0 || p
10fb2 41 2d 3e 76 3c 3d 70 41 2d 3e 70 4e 65 78 74 2d A->v<=pA->pNext-
10fb3 3e 76 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 >v );. assert
10fb4 28 20 70 42 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c ( pB->pNext==0 |
10fb5 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 4e 65 | pB->v<=pB->pNe
10fb6 78 74 2d 3e 76 20 29 3b 0a 20 20 20 20 69 66 28 xt->v );. if(
10fb7 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20 29 7b 0a pA->v<pB->v ){.
10fb8 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 4e 65 pTail->pNe
10fb9 78 74 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 70 xt = pA;. p
10fba 41 20 3d 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 A = pA->pNext;.
10fbb 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61 pTail = pTa
10fbc 69 6c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d il->pNext;. }
10fbd 65 6c 73 65 20 69 66 28 20 70 42 2d 3e 76 3c 70 else if( pB->v<p
10fbe 41 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 A->v ){. pT
10fbf 61 69 6c 2d 3e 70 4e 65 78 74 20 3d 20 70 42 3b ail->pNext = pB;
10fc0 0a 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e . pB = pB->
10fc1 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 54 61 pNext;. pTa
10fc2 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 4e 65 78 il = pTail->pNex
10fc3 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 t;. }else{.
10fc4 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 4e 65 pA = pA->pNe
10fc5 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 xt;. }. }.
10fc6 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 61 73 if( pA ){. as
10fc7 73 65 72 74 28 20 70 41 2d 3e 70 4e 65 78 74 3d sert( pA->pNext=
10fc8 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d =0 || pA->v<=pA-
10fc9 3e 70 4e 65 78 74 2d 3e 76 20 29 3b 0a 20 20 20 >pNext->v );.
10fca 20 70 54 61 69 6c 2d 3e 70 4e 65 78 74 20 3d 20 pTail->pNext =
10fcb 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 pA;. }else{.
10fcc 20 61 73 73 65 72 74 28 20 70 42 3d 3d 30 20 7c assert( pB==0 |
10fcd 7c 20 70 42 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c | pB->pNext==0 |
10fce 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 4e 65 | pB->v<=pB->pNe
10fcf 78 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 xt->v );. pTa
10fd0 69 6c 2d 3e 70 4e 65 78 74 20 3d 20 70 42 3b 0a il->pNext = pB;.
10fd1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 65 61 }. return hea
10fd2 64 2e 70 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a d.pNext;.}../*.*
10fd3 2a 20 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d 65 * Sort all eleme
10fd4 6e 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65 nts of the RowSe
10fd5 74 20 69 6e 74 6f 20 61 73 63 65 6e 64 69 6e 67 t into ascending
10fd6 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a 73 74 61 74 order..*/ .stat
10fd7 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 ic void sqlite3R
10fd8 6f 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65 74 owSetSort(RowSet
10fd9 20 2a 70 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 *p){. unsigned
10fda 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
10fdb 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 RowSetEntry *pE
10fdc 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 52 ntry;. struct R
10fdd 6f 77 53 65 74 45 6e 74 72 79 20 2a 61 42 75 63 owSetEntry *aBuc
10fde 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 65 ket[40];.. asse
10fdf 72 74 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 3d rt( p->isSorted=
10fe0 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61 =0 );. memset(a
10fe1 42 75 63 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f Bucket, 0, sizeo
10fe2 66 28 61 42 75 63 6b 65 74 29 29 3b 0a 20 20 77 f(aBucket));. w
10fe3 68 69 6c 65 28 20 70 2d 3e 70 45 6e 74 72 79 20 hile( p->pEntry
10fe4 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 ){. pEntry =
10fe5 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 20 20 70 p->pEntry;. p
10fe6 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 ->pEntry = pEntr
10fe7 79 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70 45 y->pNext;. pE
10fe8 6e 74 72 79 2d 3e 70 4e 65 78 74 20 3d 20 30 3b ntry->pNext = 0;
10fe9 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 42 . for(i=0; aB
10fea 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a ucket[i]; i++){.
10feb 20 20 20 20 20 20 70 45 6e 74 72 79 20 3d 20 62 pEntry = b
10fec 6f 6f 6c 69 64 78 4d 65 72 67 65 28 61 42 75 63 oolidxMerge(aBuc
10fed 6b 65 74 5b 69 5d 2c 70 45 6e 74 72 79 29 3b 0a ket[i],pEntry);.
10fee 20 20 20 20 20 20 61 42 75 63 6b 65 74 5b 69 5d aBucket[i]
10fef 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
10ff0 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70 45 6e aBucket[i] = pEn
10ff1 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 try;. }. pEntr
10ff2 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 y = 0;. for(i=0
10ff3 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 75 63 6b ; i<sizeof(aBuck
10ff4 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 75 63 6b et)/sizeof(aBuck
10ff5 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 et[0]); i++){.
10ff6 20 20 70 45 6e 74 72 79 20 3d 20 62 6f 6f 6c 69 pEntry = booli
10ff7 64 78 4d 65 72 67 65 28 70 45 6e 74 72 79 2c 61 dxMerge(pEntry,a
10ff8 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a Bucket[i]);. }.
10ff9 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 p->pEntry = pE
10ffa 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 ntry;. p->pLast
10ffb 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 = 0;. p->isSor
10ffc 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a ted = 1;.}../*.*
10ffd 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 6e 65 * Extract the ne
10ffe 78 74 20 28 73 6d 61 6c 6c 65 73 74 29 20 65 6c xt (smallest) el
10fff 65 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 52 ement from the R
11000 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 owSet..** Write
11001 74 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f the element into
11002 20 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 72 *pRowid. Retur
11003 6e 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 n 1 on success.
11004 20 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20 Return.** 0 if
11005 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 6c the RowSet is al
11006 72 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2f 0a ready empty..*/.
11007 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
11008 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 nt sqlite3RowSet
11009 4e 65 78 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 Next(RowSet *p,
1100a 69 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20 i64 *pRowid){.
1100b 69 66 28 20 21 70 2d 3e 69 73 53 6f 72 74 65 64 if( !p->isSorted
1100c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 ){. sqlite3R
1100d 6f 77 53 65 74 53 6f 72 74 28 70 29 3b 0a 20 20 owSetSort(p);.
1100e 7d 0a 20 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 }. if( p->pEntr
1100f 79 20 29 7b 0a 20 20 20 20 2a 70 52 6f 77 69 64 y ){. *pRowid
11010 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e 76 3b = p->pEntry->v;
11011 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d . p->pEntry =
11012 20 70 2d 3e 70 45 6e 74 72 79 2d 3e 70 4e 65 78 p->pEntry->pNex
11013 74 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 45 t;. if( p->pE
11014 6e 74 72 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ntry==0 ){.
11015 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c sqlite3RowSetCl
11016 65 61 72 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 ear(p);. }.
11017 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 return 1;. }e
11018 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
11019 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 0;. }.}../*****
1101a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1101b 20 72 6f 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a rowset.c ******
1101c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1101d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1101e 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1101f 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
11020 66 69 6c 65 20 70 61 67 65 72 2e 63 20 2a 2a 2a file pager.c ***
11021 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11022 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11023 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
11024 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
11025 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
11026 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
11027 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
11028 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
11029 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
1102a 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
1102b 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
1102c 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1102d 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
1102e 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1102f 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
11030 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
11031 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
11032 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
11033 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
11034 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
11035 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
11036 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
11037 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11038 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11039 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1103a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1103b 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
1103c 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1103d 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 f the page cache
1103e 20 73 75 62 73 79 73 74 65 6d 20 6f 72 20 22 70 subsystem or "p
1103f 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 ager"..** .** Th
11040 65 20 70 61 67 65 72 20 69 73 20 75 73 65 64 20 e pager is used
11041 74 6f 20 61 63 63 65 73 73 20 61 20 64 61 74 61 to access a data
11042 62 61 73 65 20 64 69 73 6b 20 66 69 6c 65 2e 20 base disk file.
11043 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a It implements.*
11044 2a 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20 * atomic commit
11045 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 72 and rollback thr
11046 6f 75 67 68 20 74 68 65 20 75 73 65 20 6f 66 20 ough the use of
11047 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 a journal file t
11048 68 61 74 0a 2a 2a 20 69 73 20 73 65 70 61 72 61 hat.** is separa
11049 74 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 te from the data
1104a 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 base file. The
1104b 70 61 67 65 72 20 61 6c 73 6f 20 69 6d 70 6c 65 pager also imple
1104c 6d 65 6e 74 73 20 66 69 6c 65 0a 2a 2a 20 6c 6f ments file.** lo
1104d 63 6b 69 6e 67 20 74 6f 20 70 72 65 76 65 6e 74 cking to prevent
1104e 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 66 two processes f
1104f 72 6f 6d 20 77 72 69 74 69 6e 67 20 74 68 65 20 rom writing the
11050 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a same database.**
11051 20 66 69 6c 65 20 73 69 6d 75 6c 74 61 6e 65 6f file simultaneo
11052 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 20 70 72 6f usly, or one pro
11053 63 65 73 73 20 66 72 6f 6d 20 72 65 61 64 69 6e cess from readin
11054 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 g the database w
11055 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 hile.** another
11056 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a is writing..**.*
11057 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65 * @(#) $Id: page
11058 72 2e 63 2c 76 20 31 2e 35 35 31 20 32 30 30 39 r.c,v 1.551 2009
11059 2f 30 31 2f 31 34 20 32 33 3a 30 33 3a 34 31 20 /01/14 23:03:41
1105a 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 drh Exp $.*/.#if
1105b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1105c 5f 44 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d _DISKIO../*.** M
1105d 61 63 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c acros for troubl
1105e 65 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d eshooting. Norm
1105f 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a ally turned off.
11060 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c */.#if 0.int sql
11061 69 74 65 33 50 61 67 65 72 54 72 61 63 65 3d 31 ite3PagerTrace=1
11062 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e ; /* True to en
11063 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a able tracing */.
11064 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 #define sqlite3D
11065 65 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74 ebugPrintf print
11066 66 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 f.#define PAGERT
11067 52 41 43 45 28 58 29 20 20 20 20 20 69 66 28 20 RACE(X) if(
11068 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 sqlite3PagerTrac
11069 65 20 29 7b 20 73 71 6c 69 74 65 33 44 65 62 75 e ){ sqlite3Debu
1106a 67 50 72 69 6e 74 66 20 58 3b 20 7d 0a 23 65 6c gPrintf X; }.#el
1106b 73 65 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 se.#define PAGER
1106c 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a TRACE(X).#endif.
1106d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
1106e 77 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f 73 20 wing two macros
1106f 61 72 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 are used within
11070 74 68 65 20 50 41 47 45 52 54 52 41 43 45 28 29 the PAGERTRACE()
11071 20 6d 61 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a macros above.**
11072 20 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 66 69 to print out fi
11073 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 73 2e 20 le-descriptors.
11074 0a 2a 2a 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 .**.** PAGERID()
11075 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 takes a pointer
11076 20 74 6f 20 61 20 50 61 67 65 72 20 73 74 72 75 to a Pager stru
11077 63 74 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 ct as its argume
11078 6e 74 2e 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 nt. The.** assoc
11079 69 61 74 65 64 20 66 69 6c 65 2d 64 65 73 63 72 iated file-descr
1107a 69 70 74 6f 72 20 69 73 20 72 65 74 75 72 6e 65 iptor is returne
1107b 64 2e 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 d. FILEHANDLEID(
1107c 29 20 74 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 ) takes an sqlit
1107d 65 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 e3_file.** struc
1107e 74 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e t as its argumen
1107f 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 t..*/.#define PA
11080 47 45 52 49 44 28 70 29 20 28 28 69 6e 74 29 28 GERID(p) ((int)(
11081 70 2d 3e 66 64 29 29 0a 23 64 65 66 69 6e 65 20 p->fd)).#define
11082 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29 FILEHANDLEID(fd)
11083 20 28 28 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a ((int)fd)../*.*
11084 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 * The page cache
11085 20 61 73 20 61 20 77 68 6f 6c 65 20 69 73 20 61 as a whole is a
11086 6c 77 61 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20 lways in one of
11087 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a the following.**
11088 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
11089 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 PAGER_UNLOCK
1108a 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 The page ca
1108b 63 68 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 che is not curre
1108c 6e 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20 ntly reading or
1108d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1108e 20 20 20 20 20 20 20 20 20 20 77 72 69 74 69 6e writin
1108f 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 g the database f
11090 69 6c 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e ile. There is n
11091 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 o.**
11092 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 20 data
11093 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 held in memory.
11094 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6e 69 This is the ini
11095 74 69 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 tial.**
11096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 st
11097 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 ate..**.** PAG
11098 45 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 20 ER_SHARED
11099 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 The page cache
1109a 69 73 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 is reading the d
1109b 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 atabase..**
1109c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1109d 20 20 57 72 69 74 69 6e 67 20 69 73 20 6e 6f 74 Writing is not
1109e 20 70 65 72 6d 69 74 74 65 64 2e 20 20 54 68 65 permitted. The
1109f 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20 re can be.**
110a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
110a1 20 20 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 multiple read
110a2 65 72 73 20 61 63 63 65 73 73 69 6e 67 20 74 68 ers accessing th
110a3 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a e same database.
110a4 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
110a5 20 20 20 20 20 20 20 20 20 66 69 6c 65 20 61 74 file at
110a6 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a the same time..
110a7 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 45 **.** PAGER_RE
110a8 53 45 52 56 45 44 20 20 20 20 20 20 54 68 69 73 SERVED This
110a9 20 70 72 6f 63 65 73 73 20 68 61 73 20 72 65 73 process has res
110aa 65 72 76 65 64 20 74 68 65 20 64 61 74 61 62 61 erved the databa
110ab 73 65 20 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a se for writing.*
110ac 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
110ad 20 20 20 20 20 20 20 20 62 75 74 20 68 61 73 20 but has
110ae 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 61 6e 79 not yet made any
110af 20 63 68 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 changes. Only
110b0 6f 6e 65 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20 one process.**
110b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
110b2 20 20 20 20 20 61 74 20 61 20 74 69 6d 65 20 63 at a time c
110b3 61 6e 20 72 65 73 65 72 76 65 20 74 68 65 20 64 an reserve the d
110b4 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 6f 72 atabase. The or
110b5 69 67 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 iginal.**
110b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
110b7 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
110b8 73 20 6e 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 s not been modif
110b9 69 65 64 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 ied so other.**
110ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
110bb 20 20 20 20 20 20 70 72 6f 63 65 73 73 65 73 20 processes
110bc 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61 may still be rea
110bd 64 69 6e 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b ding the on-disk
110be 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
110bf 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 databa
110c0 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 se file..**.**
110c1 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
110c2 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 The page ca
110c3 63 68 65 20 69 73 20 77 72 69 74 69 6e 67 20 74 che is writing t
110c4 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 he database..**
110c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
110c6 20 20 20 20 20 20 41 63 63 65 73 73 20 69 73 20 Access is
110c7 65 78 63 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f exclusive. No o
110c8 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 6f ther processes o
110c9 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 r.**
110ca 20 20 20 20 20 20 20 20 20 20 20 74 68 72 65 61 threa
110cb 64 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e ds can be readin
110cc 67 20 6f 72 20 77 72 69 74 69 6e 67 20 77 68 69 g or writing whi
110cd 6c 65 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20 le one.**
110ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
110cf 70 72 6f 63 65 73 73 20 69 73 20 77 72 69 74 69 process is writi
110d0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 ng..**.** PAGE
110d1 52 5f 53 59 4e 43 45 44 20 20 20 20 20 20 20 20 R_SYNCED
110d2 54 68 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20 The pager moves
110d3 74 6f 20 74 68 69 73 20 73 74 61 74 65 20 66 72 to this state fr
110d4 6f 6d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 om PAGER_EXCLUSI
110d5 56 45 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 VE.**
110d6 20 20 20 20 20 20 20 20 20 20 20 20 61 66 74 65 afte
110d7 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 r all dirty page
110d8 73 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 s have been writ
110d9 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 ten to the.**
110da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
110db 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c database fil
110dc 65 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68 e and the file h
110dd 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 as been synced t
110de 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 o.**
110df 20 20 20 20 20 20 20 20 20 20 20 64 69 73 6b 2e disk.
110e0 20 41 6c 6c 20 74 68 61 74 20 72 65 6d 61 69 6e All that remain
110e1 73 20 74 6f 20 64 6f 20 69 73 20 74 6f 20 72 65 s to do is to re
110e2 6d 6f 76 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20 move or.**
110e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
110e4 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f truncate the jo
110e5 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 74 urnal file and t
110e6 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a he transaction .
110e7 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
110e8 20 20 20 20 20 20 20 20 20 77 69 6c 6c 20 62 65 will be
110e9 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a committed..**.*
110ea 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 * The page cache
110eb 20 63 6f 6d 65 73 20 75 70 20 69 6e 20 50 41 47 comes up in PAG
110ec 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 ER_UNLOCK. The
110ed 66 69 72 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20 first time a.**
110ee 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
110ef 29 20 6f 63 63 75 72 73 2c 20 74 68 65 20 73 74 ) occurs, the st
110f0 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 ate transitions
110f1 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 2e to PAGER_SHARED.
110f2 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 70 61 .** After all pa
110f3 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 ges have been re
110f4 6c 65 61 73 65 64 20 75 73 69 6e 67 20 73 71 6c leased using sql
110f5 69 74 65 5f 70 61 67 65 5f 75 6e 72 65 66 28 29 ite_page_unref()
110f6 2c 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 74 ,.** the state t
110f7 72 61 6e 73 69 74 69 6f 6e 73 20 62 61 63 6b 20 ransitions back
110f8 74 6f 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e to PAGER_UNLOCK.
110f9 20 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 The first time
110fa 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 .** that sqlite3
110fb 50 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 PagerWrite() is
110fc 63 61 6c 6c 65 64 2c 20 74 68 65 20 73 74 61 74 called, the stat
110fd 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f e transitions to
110fe 0a 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 .** PAGER_RESERV
110ff 45 44 2e 20 20 28 4e 6f 74 65 20 74 68 61 74 20 ED. (Note that
11100 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
11101 65 28 29 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a e() can only be.
11102 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 ** called on an
11103 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
11104 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 which means tha
11105 74 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 t the pager must
11106 0a 2a 2a 20 62 65 20 69 6e 20 50 41 47 45 52 5f .** be in PAGER_
11107 53 48 41 52 45 44 20 62 65 66 6f 72 65 20 69 74 SHARED before it
11108 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 transitions to
11109 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 29 PAGER_RESERVED.)
1110a 0a 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 .** PAGER_RESERV
1110b 45 44 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 ED means that th
1110c 65 72 65 20 69 73 20 61 6e 20 6f 70 65 6e 20 72 ere is an open r
1110d 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
1110e 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73 69 74 69 .** The transiti
1110f 6f 6e 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c on to PAGER_EXCL
11110 55 53 49 56 45 20 6f 63 63 75 72 73 20 62 65 66 USIVE occurs bef
11111 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a ore any changes.
11112 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 ** are made to t
11113 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
11114 2c 20 74 68 6f 75 67 68 20 77 72 69 74 65 73 20 , though writes
11115 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a to the rollback.
11116 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72 ** journal occur
11117 73 20 77 69 74 68 20 6a 75 73 74 20 50 41 47 45 s with just PAGE
11118 52 5f 52 45 53 45 52 56 45 44 2e 20 20 41 66 74 R_RESERVED. Aft
11119 65 72 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 er an sqlite3Pag
1111a 65 72 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 erRollback().**
1111b 6f 72 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 or sqlite3PagerC
1111c 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 2c ommitPhaseTwo(),
1111d 20 74 68 65 20 73 74 61 74 65 20 63 61 6e 20 67 the state can g
1111e 6f 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f o back to PAGER_
1111f 53 48 41 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 SHARED,.** or it
11120 20 63 61 6e 20 73 74 61 79 20 61 74 20 50 41 47 can stay at PAG
11121 45 52 5f 45 58 43 4c 55 53 49 56 45 20 69 66 20 ER_EXCLUSIVE if
11122 77 65 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 we are in exclus
11123 69 76 65 20 61 63 63 65 73 73 20 6d 6f 64 65 2e ive access mode.
11124 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 .*/.#define PAGE
11125 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a R_UNLOCK 0.
11126 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 48 #define PAGER_SH
11127 41 52 45 44 20 20 20 20 20 20 31 20 20 20 2f 2a ARED 1 /*
11128 20 73 61 6d 65 20 61 73 20 53 48 41 52 45 44 5f same as SHARED_
11129 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 LOCK */.#define
1112a 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 PAGER_RESERVED
1112b 20 20 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 2 /* same as
1112c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a RESERVED_LOCK *
1112d 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
1112e 45 58 43 4c 55 53 49 56 45 20 20 20 34 20 20 20 EXCLUSIVE 4
1112f 2f 2a 20 73 61 6d 65 20 61 73 20 45 58 43 4c 55 /* same as EXCLU
11130 53 49 56 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 SIVE_LOCK */.#de
11131 66 69 6e 65 20 50 41 47 45 52 5f 53 59 4e 43 45 fine PAGER_SYNCE
11132 44 20 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 D 5../*.**
11133 54 68 69 73 20 6d 61 63 72 6f 20 72 6f 75 6e 64 This macro round
11134 73 20 76 61 6c 75 65 73 20 75 70 20 73 6f 20 74 s values up so t
11135 68 61 74 20 69 66 20 74 68 65 20 76 61 6c 75 65 hat if the value
11136 20 69 73 20 61 6e 20 61 64 64 72 65 73 73 20 69 is an address i
11137 74 0a 2a 2a 20 69 73 20 67 75 61 72 61 6e 74 65 t.** is guarante
11138 65 64 20 74 6f 20 62 65 20 61 6e 20 61 64 64 72 ed to be an addr
11139 65 73 73 20 74 68 61 74 20 69 73 20 61 6c 69 67 ess that is alig
1113a 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 ned to an 8-byte
1113b 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 23 64 boundary..*/.#d
1113c 65 66 69 6e 65 20 46 4f 52 43 45 5f 41 4c 49 47 efine FORCE_ALIG
1113d 4e 4d 45 4e 54 28 58 29 20 20 20 28 28 28 58 29 NMENT(X) (((X)
1113e 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 41 +7)&~7)../*.** A
1113f 20 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72 20 macro used for
11140 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 64 invoking the cod
11141 65 63 20 69 66 20 74 68 65 72 65 20 69 73 20 6f ec if there is o
11142 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ne.*/.#ifdef SQL
11143 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 20 ITE_HAS_CODEC.#
11144 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c define CODEC1(P,
11145 44 2c 4e 2c 58 29 20 69 66 28 20 50 2d 3e 78 43 D,N,X) if( P->xC
11146 6f 64 65 63 21 3d 30 20 29 7b 20 50 2d 3e 78 43 odec!=0 ){ P->xC
11147 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72 odec(P->pCodecAr
11148 67 2c 44 2c 4e 2c 58 29 3b 20 7d 0a 23 20 64 65 g,D,N,X); }.# de
11149 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c fine CODEC2(P,D,
1114a 4e 2c 58 29 20 28 28 63 68 61 72 2a 29 28 50 2d N,X) ((char*)(P-
1114b 3e 78 43 6f 64 65 63 21 3d 30 3f 50 2d 3e 78 43 >xCodec!=0?P->xC
1114c 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72 odec(P->pCodecAr
1114d 67 2c 44 2c 4e 2c 58 29 3a 44 29 29 0a 23 65 6c g,D,N,X):D)).#el
1114e 73 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 se.# define CODE
1114f 43 31 28 50 2c 44 2c 4e 2c 58 29 20 2f 2a 20 4e C1(P,D,N,X) /* N
11150 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e 65 O-OP */.# define
11151 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 29 CODEC2(P,D,N,X)
11152 20 28 28 63 68 61 72 2a 29 44 29 0a 23 65 6e 64 ((char*)D).#end
11153 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 if../*.** The ma
11154 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65 ximum allowed se
11155 63 74 6f 72 20 73 69 7a 65 2e 20 31 36 4d 42 2e ctor size. 16MB.
11156 20 49 66 20 74 68 65 20 78 53 65 63 74 6f 72 73 If the xSectors
11157 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a ize() method .**
11158 20 72 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65 returns a value
11159 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 larger than thi
1115a 73 2c 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 s, then MAX_SECT
1115b 4f 52 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20 OR_SIZE is used
1115c 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 instead..** This
1115d 20 63 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 could conceivab
1115e 6c 79 20 63 61 75 73 65 20 63 6f 72 72 75 70 74 ly cause corrupt
1115f 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 ion following a
11160 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e power failure on
11161 0a 2a 2a 20 73 75 63 68 20 61 20 73 79 73 74 65 .** such a syste
11162 6d 2e 20 54 68 69 73 20 69 73 20 63 75 72 72 65 m. This is curre
11163 6e 74 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 ntly an undocume
11164 6e 74 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 nted limit..*/.#
11165 64 65 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f define MAX_SECTO
11166 52 5f 53 49 5a 45 20 30 78 30 31 30 30 30 30 30 R_SIZE 0x0100000
11167 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
11168 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
11169 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 wing structure i
1116a 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
1116b 65 61 63 68 20 61 63 74 69 76 65 0a 2a 2a 20 73 each active.** s
1116c 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 73 74 61 avepoint and sta
1116d 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1116e 6f 6e 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d on in the system
1116f 2e 20 41 6c 6c 20 73 75 63 68 20 73 74 72 75 63 . All such struc
11170 74 75 72 65 73 0a 2a 2a 20 61 72 65 20 73 74 6f tures.** are sto
11171 72 65 64 20 69 6e 20 74 68 65 20 50 61 67 65 72 red in the Pager
11172 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 .aSavepoint[] ar
11173 72 61 79 2c 20 77 68 69 63 68 20 69 73 20 61 6c ray, which is al
11174 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 72 located and.** r
11175 65 73 69 7a 65 64 20 75 73 69 6e 67 20 73 71 6c esized using sql
11176 69 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a ite3Realloc()..*
11177 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61 76 65 *.** When a save
11178 70 6f 69 6e 74 20 69 73 20 63 72 65 61 74 65 64 point is created
11179 2c 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 , the PagerSavep
1117a 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 oint.iHdrOffset
1117b 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 field is.** set
1117c 74 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75 72 6e to 0. If a journ
1117d 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 al-header is wri
1117e 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 tten into the ma
1117f 69 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69 6c 65 in journal while
11180 0a 2a 2a 20 74 68 65 20 73 61 76 65 70 6f 69 6e .** the savepoin
11181 74 20 69 73 20 61 63 74 69 76 65 2c 20 74 68 65 t is active, the
11182 6e 20 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 n iHdrOffset is
11183 73 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 set to the byte
11184 6f 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 offset .** immed
11185 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 iately following
11186 20 74 68 65 20 6c 61 73 74 20 6a 6f 75 72 6e 61 the last journa
11187 6c 20 72 65 63 6f 72 64 20 77 72 69 74 74 65 6e l record written
11188 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a into the main.*
11189 2a 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 * journal before
1118a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 the journal-hea
1118b 64 65 72 2e 20 54 68 69 73 20 69 73 20 72 65 71 der. This is req
1118c 75 69 72 65 64 20 64 75 72 69 6e 67 20 73 61 76 uired during sav
1118d 65 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 epoint.** rollba
1118e 63 6b 20 28 73 65 65 20 70 61 67 65 72 50 6c 61 ck (see pagerPla
1118f 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 29 ybackSavepoint()
11190 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 )..*/.typedef st
11191 72 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f ruct PagerSavepo
11192 69 6e 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 int PagerSavepoi
11193 6e 74 3b 0a 73 74 72 75 63 74 20 50 61 67 65 72 nt;.struct Pager
11194 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 69 36 Savepoint {. i6
11195 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 4 iOffset;
11196 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
11197 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 69 6e arting offset in
11198 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f main journal */
11199 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 73 65 . i64 iHdrOffse
1119a 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
1119b 2f 2a 20 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a /* See above */.
1119c 20 20 42 69 74 76 65 63 20 2a 70 49 6e 53 61 76 Bitvec *pInSav
1119d 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f epoint; /
1119e 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 69 * Set of pages i
1119f 6e 20 74 68 69 73 20 73 61 76 65 70 6f 69 6e 74 n this savepoint
111a0 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67 */. Pgno nOrig
111a1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
111a2 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e /* Original n
111a3 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
111a4 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f n file */. Pgno
111a5 20 69 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 iSubRec;
111a6 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
111a7 78 20 6f 66 20 66 69 72 73 74 20 72 65 63 6f 72 x of first recor
111a8 64 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c d in sub-journal
111a9 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 */.};../*.** A
111aa 6f 70 65 6e 20 70 61 67 65 20 63 61 63 68 65 20 open page cache
111ab 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
111ac 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
111ad 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
111ae 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 6d Pager.errCode m
111af 61 79 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c ay be set to SQL
111b0 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 ITE_IOERR, SQLIT
111b1 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a E_CORRUPT, or.**
111b2 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e or SQLITE_FULL.
111b3 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 Once one of the
111b4 20 66 69 72 73 74 20 74 68 72 65 65 20 65 72 72 first three err
111b5 6f 72 73 20 6f 63 63 75 72 73 2c 20 69 74 20 70 ors occurs, it p
111b6 65 72 73 69 73 74 73 0a 2a 2a 20 61 6e 64 20 69 ersists.** and i
111b7 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 74 68 s returned as th
111b8 65 20 72 65 73 75 6c 74 20 6f 66 20 65 76 65 72 e result of ever
111b9 79 20 6d 61 6a 6f 72 20 70 61 67 65 72 20 41 50 y major pager AP
111ba 49 20 63 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a 20 I call. The.**
111bb 53 51 4c 49 54 45 5f 46 55 4c 4c 20 72 65 74 75 SQLITE_FULL retu
111bc 72 6e 20 63 6f 64 65 20 69 73 20 73 6c 69 67 68 rn code is sligh
111bd 74 6c 79 20 64 69 66 66 65 72 65 6e 74 2e 20 49 tly different. I
111be 74 20 70 65 72 73 69 73 74 73 20 6f 6e 6c 79 20 t persists only
111bf 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 6e 65 78 until the.** nex
111c0 74 20 73 75 63 63 65 73 73 66 75 6c 20 72 6f 6c t successful rol
111c1 6c 62 61 63 6b 20 69 73 20 70 65 72 66 6f 72 6d lback is perform
111c2 65 64 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 ed on the pager
111c3 63 61 63 68 65 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 cache. Also,.**
111c4 53 51 4c 49 54 45 5f 46 55 4c 4c 20 64 6f 65 73 SQLITE_FULL does
111c5 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65 20 not affect the
111c6 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
111c7 29 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 ) and sqlite3Pag
111c8 65 72 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 41 50 erLookup().** AP
111c9 49 73 2c 20 74 68 65 79 20 6d 61 79 20 73 74 69 Is, they may sti
111ca 6c 6c 20 62 65 20 75 73 65 64 20 73 75 63 63 65 ll be used succe
111cb 73 73 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4d ssfully..**.** M
111cc 61 6e 61 67 69 6e 67 20 74 68 65 20 73 69 7a 65 anaging the size
111cd 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
111ce 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 69 file in pages i
111cf 73 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d 70 6c s a little compl
111d0 69 63 61 74 65 64 2e 0a 2a 2a 20 54 68 65 20 76 icated..** The v
111d1 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62 ariable Pager.db
111d2 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 Size contains th
111d3 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
111d4 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 s that the datab
111d5 61 73 65 0a 2a 2a 20 69 6d 61 67 65 20 63 75 72 ase.** image cur
111d6 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 2e rently contains.
111d7 20 41 73 20 74 68 65 20 64 61 74 61 62 61 73 65 As the database
111d8 20 69 6d 61 67 65 20 67 72 6f 77 73 20 6f 72 20 image grows or
111d9 73 68 72 69 6e 6b 73 20 74 68 69 73 0a 2a 2a 20 shrinks this.**
111da 76 61 72 69 61 62 6c 65 20 69 73 20 75 70 64 61 variable is upda
111db 74 65 64 2e 20 54 68 65 20 76 61 72 69 61 62 6c ted. The variabl
111dc 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69 e Pager.dbFileSi
111dd 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 ze contains the
111de 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 70 61 67 number.** of pag
111df 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 es in the databa
111e0 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 6d 61 se file. This ma
111e1 79 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 66 y be different f
111e2 72 6f 6d 20 50 61 67 65 72 2e 64 62 53 69 7a 65 rom Pager.dbSize
111e3 0a 2a 2a 20 69 66 20 73 6f 6d 65 20 70 61 67 65 .** if some page
111e4 73 20 68 61 76 65 20 62 65 65 6e 20 61 70 70 65 s have been appe
111e5 6e 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 nded to the data
111e6 62 61 73 65 20 69 6d 61 67 65 20 62 75 74 20 6e base image but n
111e7 6f 74 20 79 65 74 20 77 72 69 74 74 65 6e 0a 2a ot yet written.*
111e8 2a 20 6f 75 74 20 66 72 6f 6d 20 74 68 65 20 63 * out from the c
111e9 61 63 68 65 20 74 6f 20 74 68 65 20 61 63 74 75 ache to the actu
111ea 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e al file on disk.
111eb 20 4f 72 20 69 66 20 74 68 65 20 69 6d 61 67 65 Or if the image
111ec 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 74 72 75 has been.** tru
111ed 6e 63 61 74 65 64 20 62 79 20 61 6e 20 69 6e 63 ncated by an inc
111ee 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 remental-vacuum
111ef 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 50 operation. The P
111f0 61 67 65 72 2e 64 62 4f 72 69 67 53 69 7a 65 20 ager.dbOrigSize
111f1 76 61 72 69 61 62 6c 65 0a 2a 2a 20 63 6f 6e 74 variable.** cont
111f2 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 ains the number
111f3 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
111f4 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 database image w
111f5 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a hen the current.
111f6 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 ** transaction w
111f7 61 73 20 6f 70 65 6e 65 64 2e 20 54 68 65 20 63 as opened. The c
111f8 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20 74 ontents of all t
111f9 68 72 65 65 20 6f 66 20 74 68 65 73 65 20 76 61 hree of these va
111fa 72 69 61 62 6c 65 73 20 69 73 0a 2a 2a 20 6f 6e riables is.** on
111fb 6c 79 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f ly guaranteed to
111fc 20 62 65 20 63 6f 72 72 65 63 74 20 69 66 20 74 be correct if t
111fd 68 65 20 62 6f 6f 6c 65 61 6e 20 50 61 67 65 72 he boolean Pager
111fe 2e 64 62 53 69 7a 65 56 61 6c 69 64 20 69 73 20 .dbSizeValid is
111ff 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 true..*/.struct
11200 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 Pager {. sqlite
11201 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 3_vfs *pVfs;
11202 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e 63 /* OS func
11203 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f 72 tions to use for
11204 20 49 4f 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 IO */. u8 jour
11205 6e 61 6c 4f 70 65 6e 3b 20 20 20 20 20 20 20 20 nalOpen;
11206 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
11207 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 journal file des
11208 63 72 69 70 74 6f 72 73 20 69 73 20 76 61 6c 69 criptors is vali
11209 64 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 d */. u8 journa
1120a 6c 53 74 61 72 74 65 64 3b 20 20 20 20 20 20 20 lStarted;
1120b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65 /* True if he
1120c 61 64 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 ader of journal
1120d 69 73 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75 is synced */. u
1120e 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 8 useJournal;
1120f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
11210 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 e a rollback jou
11211 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c rnal on this fil
11212 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 64 e */. u8 noRead
11213 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 lock;
11214 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 /* Do not bot
11215 68 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 65 her to obtain re
11216 61 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 20 adlocks */. u8
11217 6e 6f 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 noSync;
11218 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e /* Do n
11219 6f 74 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 ot sync the jour
1121a 6e 61 6c 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 nal if true */.
1121b 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b 20 20 20 u8 fullSync;
1121c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1121d 44 6f 20 65 78 74 72 61 20 73 79 6e 63 73 20 6f Do extra syncs o
1121e 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f f the journal fo
1121f 72 20 72 6f 62 75 73 74 6e 65 73 73 20 2a 2f 0a r robustness */.
11220 20 20 75 38 20 73 79 6e 63 5f 66 6c 61 67 73 3b u8 sync_flags;
11221 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11222 20 4f 6e 65 20 6f 66 20 53 59 4e 43 5f 4e 4f 52 One of SYNC_NOR
11223 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c 4c MAL or SYNC_FULL
11224 20 2a 2f 0a 20 20 75 38 20 73 74 61 74 65 3b 20 */. u8 state;
11225 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11226 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 /* PAGER_UNLOC
11227 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f 52 45 53 K, _SHARED, _RES
11228 45 52 56 45 44 2c 20 65 74 63 2e 20 2a 2f 0a 20 ERVED, etc. */.
11229 20 75 38 20 74 65 6d 70 46 69 6c 65 3b 20 20 20 u8 tempFile;
1122a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1122b 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 74 zFilename is a t
1122c 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f emporary file */
1122d 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 . u8 readOnly;
1122e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1122f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 72 65 61 * True for a rea
11230 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 d-only database
11231 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53 79 6e 63 */. u8 needSync
11232 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11233 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 66 /* True if an f
11234 73 79 6e 63 28 29 20 69 73 20 6e 65 65 64 65 64 sync() is needed
11235 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 on the journal
11236 2a 2f 0a 20 20 75 38 20 64 69 72 74 79 43 61 63 */. u8 dirtyCac
11237 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 he;
11238 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 68 /* True if cach
11239 65 64 20 70 61 67 65 73 20 68 61 76 65 20 63 68 ed pages have ch
1123a 61 6e 67 65 64 20 2a 2f 0a 20 20 75 38 20 6d 65 anged */. u8 me
1123b 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 mDb;
1123c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
1123d 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66 69 o inhibit all fi
1123e 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 75 38 20 73 le I/O */. u8 s
1123f 65 74 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20 etMaster;
11240 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
11241 69 66 20 61 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 if a m-j name ha
11242 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 s been written t
11243 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 o jrnl */. u8 d
11244 6f 4e 6f 74 53 79 6e 63 3b 20 20 20 20 20 20 20 oNotSync;
11245 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 /* Boole
11246 61 6e 2e 20 57 68 69 6c 65 20 74 72 75 65 2c 20 an. While true,
11247 64 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68 65 do not spill the
11248 20 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20 65 cache */. u8 e
11249 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20 20 xclusiveMode;
1124a 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 /* Boole
1124b 61 6e 2e 20 54 72 75 65 20 69 66 20 6c 6f 63 6b an. True if lock
1124c 69 6e 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 ing_mode==EXCLUS
1124d 49 56 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 IVE */. u8 jour
1124e 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 nalMode;
1124f 20 20 20 20 20 2f 2a 20 4f 6e 20 6f 66 20 74 68 /* On of th
11250 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d e PAGER_JOURNALM
11251 4f 44 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a ODE_* values */.
11252 20 20 75 38 20 64 62 4d 6f 64 69 66 69 65 64 3b u8 dbModified;
11253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11254 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 61 True if there a
11255 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74 re any changes t
11256 6f 20 74 68 65 20 44 62 20 2a 2f 0a 20 20 75 38 o the Db */. u8
11257 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 changeCountDone
11258 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 ; /* Set
11259 20 61 66 74 65 72 20 69 6e 63 72 65 6d 65 6e 74 after increment
1125a 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63 ing the change-c
1125b 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 64 ounter */. u8 d
1125c 62 53 69 7a 65 56 61 6c 69 64 3b 20 20 20 20 20 bSizeValid;
1125d 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 77 /* Set w
1125e 68 65 6e 20 64 62 53 69 7a 65 20 69 73 20 63 6f hen dbSize is co
1125f 72 72 65 63 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 rrect */. Pgno
11260 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 dbSize;
11261 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
11262 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
11263 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 50 database */. P
11264 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b 20 gno dbOrigSize;
11265 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62 /* db
11266 53 69 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20 Size before the
11267 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
11268 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 ion */. Pgno db
11269 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 FileSize;
1126a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1126b 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
1126c 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
1126d 20 20 75 33 32 20 76 66 73 46 6c 61 67 73 3b 20 u32 vfsFlags;
1126e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1126f 20 46 6c 61 67 73 20 66 6f 72 20 73 71 6c 69 74 Flags for sqlit
11270 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a e3_vfs.xOpen() *
11271 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b /. int errCode;
11272 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11273 2f 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 /* One of severa
11274 6c 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 l kinds of error
11275 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b s */. int nRec;
11276 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11277 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
11278 70 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f pages written to
11279 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a the journal */.
1127a 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69 74 3b u32 cksumInit;
1127b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1127c 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20 76 61 Quasi-random va
1127d 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65 76 65 lue added to eve
1127e 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20 ry checksum */.
1127f 20 69 6e 74 20 73 74 6d 74 4e 52 65 63 3b 20 20 int stmtNRec;
11280 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11281 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 Number of record
11282 73 20 69 6e 20 73 74 6d 74 20 73 75 62 6a 6f 75 s in stmt subjou
11283 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 rnal */. int nE
11284 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 xtra;
11285 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 69 /* Add thi
11286 73 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f 20 s many bytes to
11287 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 each in-memory p
11288 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67 age */. int pag
11289 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 eSize;
1128a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1128b 66 20 62 79 74 65 73 20 69 6e 20 61 20 70 61 67 f bytes in a pag
1128c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 e */. int nPage
1128d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1128e 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 /* Total numb
1128f 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 er of in-memory
11290 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6d pages */. int m
11291 78 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 xPage;
11292 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
11293 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 m number of page
11294 73 20 74 6f 20 68 6f 6c 64 20 69 6e 20 63 61 63 s to hold in cac
11295 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 he */. Pgno mxP
11296 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 gno;
11297 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 /* Maximum a
11298 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20 74 llowed size of t
11299 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 he database */.
1129a 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72 Bitvec *pInJour
1129b 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 nal; /*
1129c 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68 One bit for each
1129d 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 page in the dat
1129e 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
1129f 42 69 74 76 65 63 20 2a 70 41 6c 77 61 79 73 52 Bitvec *pAlwaysR
112a0 6f 6c 6c 62 61 63 6b 3b 20 20 20 20 2f 2a 20 4f ollback; /* O
112a1 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 ne bit for each
112a2 70 61 67 65 20 6d 61 72 6b 65 64 20 61 6c 77 61 page marked alwa
112a3 79 73 2d 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 ys-rollback */.
112a4 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
112a5 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
112a6 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 Name of the data
112a7 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 base file */. c
112a8 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 har *zJournal;
112a9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
112aa 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 me of the journa
112ab 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 l file */. char
112ac 20 2a 7a 44 69 72 65 63 74 6f 72 79 3b 20 20 20 *zDirectory;
112ad 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 /* Direc
112ae 74 6f 72 79 20 68 6f 6c 64 20 64 61 74 61 62 61 tory hold databa
112af 73 65 20 61 6e 64 20 6a 6f 75 72 6e 61 6c 20 66 se and journal f
112b0 69 6c 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 iles */. sqlite
112b1 33 5f 66 69 6c 65 20 2a 66 64 2c 20 2a 6a 66 64 3_file *fd, *jfd
112b2 3b 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 ; /* File de
112b3 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20 64 61 scriptors for da
112b4 74 61 62 61 73 65 20 61 6e 64 20 6a 6f 75 72 6e tabase and journ
112b5 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f al */. sqlite3_
112b6 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 file *sjfd;
112b7 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 /* File desc
112b8 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 20 73 riptor for the s
112b9 75 62 2d 6a 6f 75 72 6e 61 6c 2a 2f 0a 20 20 69 ub-journal*/. i
112ba 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 nt (*xBusyHandle
112bb 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 r)(void*); /* Fu
112bc 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 nction to call w
112bd 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f hen busy */. vo
112be 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 id *pBusyHandler
112bf 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e Arg; /* Con
112c0 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f text argument fo
112c1 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a r xBusyHandler *
112c2 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 4f /. i64 journalO
112c3 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ff;
112c4 2f 2a 20 43 75 72 72 65 6e 74 20 62 79 74 65 20 /* Current byte
112c5 6f 66 66 73 65 74 20 69 6e 20 74 68 65 20 6a 6f offset in the jo
112c6 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 urnal file */.
112c7 69 36 34 20 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 i64 journalHdr;
112c8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
112c9 79 74 65 20 6f 66 66 73 65 74 20 74 6f 20 70 72 yte offset to pr
112ca 65 76 69 6f 75 73 20 6a 6f 75 72 6e 61 6c 20 68 evious journal h
112cb 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 73 eader */. u32 s
112cc 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 ectorSize;
112cd 20 20 20 20 20 20 20 2f 2a 20 41 73 73 75 6d 65 /* Assume
112ce 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 64 75 d sector size du
112cf 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f ring rollback */
112d0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
112d1 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 EST. int nHit,
112d2 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20 20 nMiss;
112d3 20 20 2f 2a 20 43 61 63 68 65 20 68 69 74 73 20 /* Cache hits
112d4 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20 and missing */.
112d5 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72 69 int nRead, nWri
112d6 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 te; /*
112d7 44 61 74 61 62 61 73 65 20 70 61 67 65 73 20 72 Database pages r
112d8 65 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23 ead/written */.#
112d9 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 2a 78 endif. void (*x
112da 52 65 69 6e 69 74 65 72 29 28 44 62 50 61 67 65 Reiniter)(DbPage
112db 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 *); /* Call this
112dc 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 65 routine when re
112dd 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 20 2a 2f loading pages */
112de 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 .#ifdef SQLITE_H
112df 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 AS_CODEC. void
112e0 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a *(*xCodec)(void*
112e1 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 ,void*,Pgno,int)
112e2 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 ; /* Routine for
112e3 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 en/decoding dat
112e4 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f a */. void *pCo
112e5 64 65 63 41 72 67 3b 20 20 20 20 20 20 20 20 20 decArg;
112e6 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 /* First argu
112e7 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 28 29 ment to xCodec()
112e8 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 61 */.#endif. cha
112e9 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 r *pTmpSpace;
112ea 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
112eb 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 r.pageSize bytes
112ec 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d of space for tm
112ed 70 20 75 73 65 20 2a 2f 0a 20 20 63 68 61 72 20 p use */. char
112ee 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 dbFileVers[16];
112ef 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 /* Change
112f0 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 61 62 s whenever datab
112f1 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 73 ase file changes
112f2 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 */. i64 journa
112f3 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20 lSizeLimit;
112f4 20 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20 /* Size limit
112f5 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a for persistent j
112f6 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a ournal files */.
112f7 20 20 50 43 61 63 68 65 20 2a 70 50 43 61 63 68 PCache *pPCach
112f8 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
112f9 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 Pointer to page
112fa 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f cache object */
112fb 0a 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e . PagerSavepoin
112fc 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f t *aSavepoint; /
112fd 2a 20 41 72 72 61 79 20 6f 66 20 61 63 74 69 76 * Array of activ
112fe 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a e savepoints */.
112ff 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 int nSavepoint
11300 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
11301 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 Number of eleme
11302 6e 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e nts in aSavepoin
11303 74 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a t[] */.};../*.**
11304 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 The following g
11305 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 lobal variables
11306 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 73 hold counters us
11307 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e ed for.** testin
11308 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e g purposes only.
11309 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c 65 These variable
1130a 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 69 s do not exist i
1130b 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69 n.** a non-testi
1130c 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73 65 ng build. These
1130d 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6e variables are n
1130e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e 0a ot thread-safe..
1130f 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
11310 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
11311 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 int sqlite3_pag
11312 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 20 er_readdb_count
11313 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 = 0; /* Numbe
11314 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 r of full pages
11315 72 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0a read from DB */.
11316 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
11317 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 qlite3_pager_wri
11318 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 tedb_count = 0;
11319 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 /* Number of f
1131a 75 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 65 ull pages writte
1131b 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 54 n to DB */.SQLIT
1131c 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1131d 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 3_pager_writej_c
1131e 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 ount = 0; /*
1131f 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
11320 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e written to journ
11321 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 al */.# define P
11322 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 2b AGER_INCR(v) v+
11323 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 +.#else.# define
11324 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a 23 PAGER_INCR(v).#
11325 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a endif..../*.** J
11326 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 67 ournal files beg
11327 69 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c in with the foll
11328 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 69 owing magic stri
11329 6e 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a 2a ng. The data.**
1132a 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 was obtained fr
1132b 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 om /dev/random.
1132c 20 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 It is used only
1132d 20 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 65 as a sanity che
1132e 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 ck..**.** Since
1132f 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 version 2.8.0, t
11330 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 he journal forma
11331 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 t contains addit
11332 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 ional sanity.**
11333 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 checking informa
11334 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 6f tion. If the po
11335 77 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 20 wer fails while
11336 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 the journal is b
11337 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c eing.** written,
11338 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 semi-random gar
11339 62 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 20 bage data might
1133a 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a 6f appear in the jo
1133b 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 urnal.** file af
1133c 74 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 73 ter power is res
1133d 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 74 tored. If an at
1133e 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d 61 tempt is then ma
1133f 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 de.** to roll th
11340 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20 e journal back,
11341 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 75 the database cou
11342 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e ld be corrupted.
11343 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 6c The additional
11344 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 6b .** sanity check
11345 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 61 ing data is an a
11346 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f 76 ttempt to discov
11347 65 72 20 74 68 65 20 67 61 72 62 61 67 65 20 69 er the garbage i
11348 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c n the.** journal
11349 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a and ignore it..
1134a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 79 **.** The sanity
1134b 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d checking inform
1134c 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65 ation for the ne
1134d 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 w journal format
1134e 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20 consists.** of
1134f 61 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 75 a 32-bit checksu
11350 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f m on each page o
11351 66 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 65 f data. The che
11352 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f 74 cksum covers bot
11353 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 h.** the page nu
11354 6d 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 61 mber and the pPa
11355 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 ger->pageSize by
11356 74 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 20 tes of data for
11357 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69 the page..** Thi
11358 73 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 69 s cksum is initi
11359 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d 62 alized to a 32-b
1135a 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 it random value
1135b 74 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 20 that appears in
1135c 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 the.** journal f
1135d 69 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 20 ile right after
1135e 74 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 the header. The
1135f 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69 random initiali
11360 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 zer is important
11361 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 72 ,.** because gar
11362 62 61 67 65 20 64 61 74 61 20 74 68 61 74 20 61 bage data that a
11363 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65 6e ppears at the en
11364 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69 d of a journal i
11365 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 s likely.** data
11366 20 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 69 that was once i
11367 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 68 n other files th
11368 61 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 6e at have now been
11369 20 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 68 deleted. If th
1136a 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 74 e.** garbage dat
1136b 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f a came from an o
1136c 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 bsolete journal
1136d 66 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b 73 file, the checks
1136e 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 ums might.** be
1136f 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 79 correct. But by
11370 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 initializing th
11371 65 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 e checksum to ra
11372 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 68 ndom value which
11373 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e 74 .** is different
11374 20 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 6e for every journ
11375 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 al, we minimize
11376 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 that risk..*/.st
11377 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
11378 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e 61 ned char aJourna
11379 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 lMagic[] = {. 0
1137a 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 2c xd9, 0xd5, 0x05,
1137b 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 61 0xf9, 0x20, 0xa
1137c 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d 1, 0x63, 0xd7,.}
1137d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a ;../*.** The siz
1137e 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 e of the header
1137f 61 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 and of each page
11380 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 in the journal
11381 69 73 20 64 65 74 65 72 6d 69 6e 65 64 0a 2a 2a is determined.**
11382 20 62 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e by the followin
11383 67 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 g macros..*/.#de
11384 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f fine JOURNAL_PG_
11385 53 5a 28 70 50 61 67 65 72 29 20 20 28 28 70 50 SZ(pPager) ((pP
11386 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 ager->pageSize)
11387 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 + 8)../*.** The
11388 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 journal header s
11389 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 ize for this pag
1138a 65 72 2e 20 49 6e 20 74 68 65 20 66 75 74 75 72 er. In the futur
1138b 65 2c 20 74 68 69 73 20 63 6f 75 6c 64 20 62 65 e, this could be
1138c 0a 2a 2a 20 73 65 74 20 74 6f 20 73 6f 6d 65 20 .** set to some
1138d 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 value read from
1138e 74 68 65 20 64 69 73 6b 20 63 6f 6e 74 72 6f 6c the disk control
1138f 6c 65 72 2e 20 54 68 65 20 69 6d 70 6f 72 74 61 ler. The importa
11390 6e 74 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 69 nt.** characteri
11391 73 74 69 63 20 69 73 20 74 68 61 74 20 69 74 20 stic is that it
11392 69 73 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 is the same size
11393 20 61 73 20 61 20 64 69 73 6b 20 73 65 63 74 6f as a disk secto
11394 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f r..*/.#define JO
11395 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
11396 67 65 72 29 20 28 70 50 61 67 65 72 2d 3e 73 65 ger) (pPager->se
11397 63 74 6f 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a ctorSize)../*.**
11398 20 54 68 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42 The macro MEMDB
11399 20 69 73 20 74 72 75 65 20 69 66 20 77 65 20 61 is true if we a
1139a 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 re dealing with
1139b 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
1139c 61 62 61 73 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 abase..** We do
1139d 74 68 69 73 20 61 73 20 61 20 6d 61 63 72 6f 20 this as a macro
1139e 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 53 so that if the S
1139f 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 QLITE_OMIT_MEMOR
113a0 59 44 42 20 6d 61 63 72 6f 20 69 73 20 73 65 74 YDB macro is set
113a1 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f ,.** the value o
113a2 66 20 4d 45 4d 44 42 20 77 69 6c 6c 20 62 65 20 f MEMDB will be
113a3 61 20 63 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74 a constant and t
113a4 68 65 20 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c he compiler will
113a5 20 6f 70 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 optimize.** out
113a6 20 63 6f 64 65 20 74 68 61 74 20 77 6f 75 6c 64 code that would
113a7 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 2e 0a never execute..
113a8 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
113a9 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 _OMIT_MEMORYDB.#
113aa 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 30 0a define MEMDB 0.
113ab 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 4d #else.# define M
113ac 45 4d 44 42 20 70 50 61 67 65 72 2d 3e 6d 65 6d EMDB pPager->mem
113ad 44 62 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a Db.#endif../*.**
113ae 20 50 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47 Page number PAG
113af 45 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 ER_MJ_PGNO is ne
113b0 76 65 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53 ver used in an S
113b1 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 28 QLite database (
113b2 69 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 it is.** reserve
113b3 64 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 d for working ar
113b4 6f 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 ound a windows/p
113b5 6f 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 osix incompatibi
113b6 6c 69 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 lity). It is.**
113b7 75 73 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 used in the jour
113b8 6e 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74 nal to signify t
113b9 68 61 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65 hat the remainde
113ba 72 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c r of the journal
113bb 20 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 file .** is dev
113bc 6f 74 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 oted to storing
113bd 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
113be 20 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72 name - there ar
113bf 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 e no more pages
113c0 74 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e to.** roll back.
113c1 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f See comments fo
113c2 72 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 r function write
113c3 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 MasterJournal()
113c4 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a for details..*/.
113c5 2f 2a 20 23 64 65 66 69 6e 65 20 50 41 47 45 52 /* #define PAGER
113c6 5f 4d 4a 5f 50 47 4e 4f 28 78 29 20 28 50 45 4e _MJ_PGNO(x) (PEN
113c7 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e DING_BYTE/((x)->
113c8 70 61 67 65 53 69 7a 65 29 29 20 2a 2f 0a 23 64 pageSize)) */.#d
113c9 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 efine PAGER_MJ_P
113ca 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28 GNO(x) ((Pgno)((
113cb 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 PENDING_BYTE/((x
113cc 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29 )->pageSize))+1)
113cd 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 )../*.** The max
113ce 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 67 65 20 imum legal page
113cf 6e 75 6d 62 65 72 20 69 73 20 28 32 5e 33 31 20 number is (2^31
113d0 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 - 1)..*/.#define
113d1 20 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 PAGER_MAX_PGNO
113d2 32 31 34 37 34 38 33 36 34 37 0a 0a 2f 2a 0a 2a 2147483647../*.*
113d3 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
113d4 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 it is necessary
113d5 20 74 6f 20 77 72 69 74 65 20 70 61 67 65 20 2a to write page *
113d6 70 50 67 20 69 6e 74 6f 20 74 68 65 20 73 75 62 pPg into the sub
113d7 2d 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 -journal..** A p
113d8 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 age needs to be
113d9 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
113da 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 sub-journal if
113db 74 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 there exists one
113dc 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e .** or more open
113dd 20 73 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 savepoints for
113de 77 68 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a which:.**.** *
113df 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 The page-number
113e0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 is less than or
113e1 20 65 71 75 61 6c 20 74 6f 20 50 61 67 65 72 53 equal to PagerS
113e2 61 76 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 avepoint.nOrig,
113e3 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 and.** * The b
113e4 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 it corresponding
113e5 20 74 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 6d to the page-num
113e6 62 65 72 20 69 73 20 6e 6f 74 20 73 65 74 20 69 ber is not set i
113e7 6e 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 n.** PagerSa
113e8 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 vepoint.pInSavep
113e9 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 oint..*/.static
113ea 69 6e 74 20 73 75 62 6a 52 65 71 75 69 72 65 73 int subjRequires
113eb 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 Page(PgHdr *pPg)
113ec 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 {. Pgno pgno =
113ed 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 pPg->pgno;. Pag
113ee 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 er *pPager = pPg
113ef 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 ->pPager;. int
113f0 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
113f1 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 pPager->nSavepoi
113f2 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 nt; i++){. Pa
113f3 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 gerSavepoint *p
113f4 3d 20 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 = &pPager->aSave
113f5 70 6f 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 point[i];. if
113f6 28 20 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f ( p->nOrig>=pgno
113f7 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 && 0==sqlite3Bi
113f8 74 76 65 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 tvecTest(p->pInS
113f9 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 avepoint, pgno)
113fa 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
113fb 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 1;. }. }. r
113fc 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
113fd 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
113fe 20 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 the page is alr
113ff 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 eady in the jour
11400 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 nal file..*/.sta
11401 74 69 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f tic int pageInJo
11402 75 72 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 urnal(PgHdr *pPg
11403 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
11404 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 te3BitvecTest(pP
11405 67 2d 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f g->pPager->pInJo
11406 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f urnal, pPg->pgno
11407 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 );.}../*.** Read
11408 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 a 32-bit intege
11409 72 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e r from the given
1140a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
1140b 2e 20 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74 . Store the int
1140c 65 67 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 eger.** that is
1140d 72 65 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 read in *pRes.
1140e 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
1140f 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 if everything w
11410 6f 72 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 orked, or an.**
11411 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f error code is so
11412 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f mething goes wro
11413 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 ng..**.** All va
11414 6c 75 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 lues are stored
11415 6f 6e 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65 on disk as big-e
11416 6e 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ndian..*/.static
11417 20 69 6e 74 20 72 65 61 64 33 32 62 69 74 73 28 int read32bits(
11418 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 sqlite3_file *fd
11419 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 , i64 offset, u3
1141a 32 20 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 2 *pRes){. unsi
1141b 67 6e 65 64 20 63 68 61 72 20 61 63 5b 34 5d 3b gned char ac[4];
1141c 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 . int rc = sqli
1141d 74 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 63 te3OsRead(fd, ac
1141e 2c 20 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 , sizeof(ac), of
1141f 66 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d fset);. if( rc=
11420 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
11421 20 20 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65 *pRes = sqlite
11422 33 47 65 74 34 62 79 74 65 28 61 63 29 3b 0a 20 3Get4byte(ac);.
11423 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
11424 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 }../*.** Write a
11425 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
11426 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 62 75 into a string bu
11427 66 66 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 ffer in big-endi
11428 61 6e 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a an byte order..*
11429 2f 0a 23 64 65 66 69 6e 65 20 70 75 74 33 32 62 /.#define put32b
1142a 69 74 73 28 41 2c 42 29 20 20 73 71 6c 69 74 65 its(A,B) sqlite
1142b 33 50 75 74 34 62 79 74 65 28 28 75 38 2a 29 41 3Put4byte((u8*)A
1142c 2c 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ,B)../*.** Write
1142d 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 a 32-bit intege
1142e 72 20 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e r into the given
1142f 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
11430 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
11431 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 _OK.** on succes
11432 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f s or an error co
11433 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 de is something
11434 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 goes wrong..*/.s
11435 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 33 tatic int write3
11436 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 2bits(sqlite3_fi
11437 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 le *fd, i64 offs
11438 65 74 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 et, u32 val){.
11439 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 char ac[4];. pu
1143a 74 33 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29 t32bits(ac, val)
1143b 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 ;. return sqlit
1143c 65 33 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63 e3OsWrite(fd, ac
1143d 2c 20 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a , 4, offset);.}.
1143e 0a 2f 2a 0a 2a 2a 20 49 66 20 66 69 6c 65 20 70 ./*.** If file p
1143f 46 64 20 69 73 20 6f 70 65 6e 2c 20 63 61 6c 6c Fd is open, call
11440 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b sqlite3OsUnlock
11441 28 29 20 6f 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 () on it..*/.sta
11442 74 69 63 20 69 6e 74 20 6f 73 55 6e 6c 6f 63 6b tic int osUnlock
11443 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
11444 46 64 2c 20 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a Fd, int eLock){.
11445 20 20 69 66 28 20 21 70 46 64 2d 3e 70 4d 65 74 if( !pFd->pMet
11446 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75 hods ){. retu
11447 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
11448 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
11449 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46 64 2c 20 e3OsUnlock(pFd,
1144a 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a eLock);.}../*.**
1144b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 This function d
1144c 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 etermines whethe
1144d 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 6f r or not the ato
1144e 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 mic-write optimi
1144f 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62 65 zation.** can be
11450 20 75 73 65 64 20 77 69 74 68 20 74 68 69 73 20 used with this
11451 70 61 67 65 72 2e 20 54 68 65 20 6f 70 74 69 6d pager. The optim
11452 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 ization can be u
11453 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 28 sed if:.**.** (
11454 61 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 a) the value ret
11455 75 72 6e 65 64 20 62 79 20 4f 73 44 65 76 69 63 urned by OsDevic
11456 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
11457 28 29 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 () indicates tha
11458 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 61 74 61 t.** a data
11459 62 61 73 65 20 70 61 67 65 20 6d 61 79 20 62 65 base page may be
1145a 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 61 written atomica
1145b 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 28 62 29 lly, and.** (b)
1145c 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
1145d 6e 65 64 20 62 79 20 4f 73 53 65 63 74 6f 72 53 ned by OsSectorS
1145e 69 7a 65 28 29 20 69 73 20 6c 65 73 73 20 74 68 ize() is less th
1145f 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 20 an or equal.**
11460 20 20 20 20 74 6f 20 74 68 65 20 70 61 67 65 20 to the page
11461 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 size..**.** If t
11462 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 he optimization
11463 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 cannot be used,
11464 30 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 0 is returned. I
11465 66 20 69 74 20 63 61 6e 20 62 65 20 75 73 65 64 f it can be used
11466 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 ,.** then the va
11467 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 lue returned is
11468 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
11469 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 journal file whe
1146a 6e 20 69 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 n it.** contains
1146b 20 72 6f 6c 6c 62 61 63 6b 20 64 61 74 61 20 66 rollback data f
1146c 6f 72 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 or exactly one p
1146d 61 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 age..*/.#ifdef S
1146e 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
1146f 4d 49 43 5f 57 52 49 54 45 0a 73 74 61 74 69 63 MIC_WRITE.static
11470 20 69 6e 74 20 6a 72 6e 6c 42 75 66 66 65 72 53 int jrnlBufferS
11471 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ize(Pager *pPage
11472 72 29 7b 0a 20 20 69 6e 74 20 64 63 3b 20 20 20 r){. int dc;
11473 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 /* Devic
11474 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
11475 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 63 74 s */. int nSect
11476 6f 72 3b 20 20 20 20 20 20 2f 2a 20 53 65 63 74 or; /* Sect
11477 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 or size */. int
11478 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 szPage;
11479 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 2a 2f 0a /* Page size */.
1147a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1147b 66 64 20 3d 20 70 50 61 67 65 72 2d 3e 66 64 3b fd = pPager->fd;
1147c 0a 0a 20 20 69 66 28 20 66 64 2d 3e 70 4d 65 74 .. if( fd->pMet
1147d 68 6f 64 73 20 29 7b 0a 20 20 20 20 64 63 20 3d hods ){. dc =
1147e 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 sqlite3OsDevice
1147f 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
11480 66 64 29 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72 fd);. nSector
11481 20 3d 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f = pPager->secto
11482 72 53 69 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67 rSize;. szPag
11483 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 e = pPager->page
11484 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 Size;. }.. ass
11485 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 ert(SQLITE_IOCAP
11486 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 _ATOMIC512==(512
11487 3e 3e 38 29 29 3b 0a 20 20 61 73 73 65 72 74 28 >>8));. assert(
11488 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
11489 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e MIC64K==(65536>>
1148a 38 29 29 3b 0a 0a 20 20 69 66 28 20 21 66 64 2d 8));.. if( !fd-
1148b 3e 70 4d 65 74 68 6f 64 73 20 7c 7c 20 0a 20 20 >pMethods || .
1148c 20 20 20 20 20 28 64 63 20 26 20 28 53 51 4c 49 (dc & (SQLI
1148d 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c TE_IOCAP_ATOMIC|
1148e 28 73 7a 50 61 67 65 3e 3e 38 29 29 20 26 26 20 (szPage>>8)) &&
1148f 6e 53 65 63 74 6f 72 3c 3d 73 7a 50 61 67 65 29 nSector<=szPage)
11490 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 4a ){. return J
11491 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
11492 61 67 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f ager) + JOURNAL_
11493 50 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 PG_SZ(pPager);.
11494 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
11495 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
11496 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f his function sho
11497 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 uld be called wh
11498 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 en an error occu
11499 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 rs within the pa
1149a 67 65 72 0a 2a 2a 20 63 6f 64 65 2e 20 54 68 65 ger.** code. The
1149b 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
1149c 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1149d 74 68 65 20 70 61 67 65 72 20 73 74 72 75 63 74 the pager struct
1149e 75 72 65 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f ure, the.** seco
1149f 6e 64 20 74 68 65 20 65 72 72 6f 72 2d 63 6f 64 nd the error-cod
114a0 65 20 61 62 6f 75 74 20 74 6f 20 62 65 20 72 65 e about to be re
114a1 74 75 72 6e 65 64 20 62 79 20 61 20 70 61 67 65 turned by a page
114a2 72 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20 r API function.
114a3 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 .** The value re
114a4 74 75 72 6e 65 64 20 69 73 20 61 20 63 6f 70 79 turned is a copy
114a5 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 of the second a
114a6 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 rgument to this
114a7 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a function. .**.**
114a8 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 If the second a
114a9 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 rgument is SQLIT
114aa 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f E_IOERR, SQLITE_
114ab 43 4f 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49 CORRUPT, or SQLI
114ac 54 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 TE_FULL.** the e
114ad 72 72 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72 rror becomes per
114ae 73 69 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74 sistent. Until t
114af 68 65 20 70 65 72 73 69 73 74 65 6e 20 65 72 72 he persisten err
114b0 6f 72 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a or is cleared,.*
114b1 2a 20 73 75 62 73 65 71 75 65 6e 74 20 41 50 49 * subsequent API
114b2 20 63 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50 calls on this P
114b3 61 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69 ager will immedi
114b4 61 74 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65 ately return the
114b5 20 73 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20 same .** error
114b6 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 code..**.** A pe
114b7 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69 rsistent error i
114b8 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 ndicates that th
114b9 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
114ba 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a e pager-cache .*
114bb 2a 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 * cannot be trus
114bc 74 65 64 2e 20 54 68 69 73 20 73 74 61 74 65 20 ted. This state
114bd 63 61 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62 can be cleared b
114be 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 y completely dis
114bf 63 61 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20 carding .** the
114c0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
114c1 70 61 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20 pager-cache. If
114c2 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 a transaction wa
114c3 73 20 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a s active when.**
114c4 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 the persistent
114c5 65 72 72 6f 72 20 6f 63 63 75 72 65 64 2c 20 74 error occured, t
114c6 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b hen the rollback
114c7 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65 journal may nee
114c8 64 0a 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c 61 d.** to be repla
114c9 79 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 yed..*/.static v
114ca 6f 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b oid pager_unlock
114cb 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b (Pager *pPager);
114cc 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
114cd 72 5f 65 72 72 6f 72 28 50 61 67 65 72 20 2a 70 r_error(Pager *p
114ce 50 61 67 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a Pager, int rc){.
114cf 20 20 69 6e 74 20 72 63 32 20 3d 20 72 63 20 26 int rc2 = rc &
114d0 20 30 78 66 66 3b 0a 20 20 61 73 73 65 72 74 28 0xff;. assert(
114d1 0a 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e . pPager->
114d2 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f errCode==SQLITE_
114d3 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20 70 FULL ||. p
114d4 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d Pager->errCode==
114d5 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20 SQLITE_OK ||.
114d6 20 20 20 20 28 70 50 61 67 65 72 2d 3e 65 72 72 (pPager->err
114d7 43 6f 64 65 20 26 20 30 78 66 66 29 3d 3d 53 51 Code & 0xff)==SQ
114d8 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 29 3b 0a LITE_IOERR. );.
114d9 20 20 69 66 28 0a 20 20 20 20 72 63 32 3d 3d 53 if(. rc2==S
114da 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 QLITE_FULL ||.
114db 20 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 49 4f rc2==SQLITE_IO
114dc 45 52 52 20 7c 7c 0a 20 20 20 20 72 63 32 3d 3d ERR ||. rc2==
114dd 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 0a 20 SQLITE_CORRUPT.
114de 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
114df 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 errCode = rc;.
114e0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 if( pPager->st
114e1 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 ate==PAGER_UNLOC
114e2 4b 20 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 K . && sqlit
114e3 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 e3PcacheRefCount
114e4 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
114e5 29 3d 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 )==0 . ){.
114e6 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 /* If the pag
114e7 65 72 20 69 73 20 61 6c 72 65 61 64 79 20 75 6e er is already un
114e8 6c 6f 63 6b 65 64 2c 20 63 61 6c 6c 20 70 61 67 locked, call pag
114e9 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 6e 6f 77 20 er_unlock() now
114ea 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 to. ** clea
114eb 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 r the error stat
114ec 65 20 61 6e 64 20 65 6e 73 75 72 65 20 74 68 61 e and ensure tha
114ed 74 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 t the pager-cach
114ee 65 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 63 e is . ** c
114ef 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e ompletely empty.
114f0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
114f1 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 pager_unlock(pPa
114f2 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ger);. }. }.
114f3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
114f4 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f /*.** If SQLITE_
114f5 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 CHECK_PAGES is d
114f6 65 66 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 efined then we d
114f7 6f 20 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 o some sanity ch
114f8 65 63 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 ecking.** on the
114f9 20 63 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 cache using a h
114fa 61 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 ash function. T
114fb 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 his is used for
114fc 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 testing.** and d
114fd 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a ebugging only..*
114fe 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
114ff 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a CHECK_PAGES./*.*
11500 2a 20 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 * Return a 32-bi
11501 74 20 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 t hash of the pa
11502 67 65 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 ge data for pPag
11503 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 e..*/.static u32
11504 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 pager_datahash(
11505 69 6e 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 int nByte, unsig
11506 6e 65 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 ned char *pData)
11507 7b 0a 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 {. u32 hash = 0
11508 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 ;. int i;. for
11509 28 69 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 (i=0; i<nByte; i
1150a 2b 2b 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 ++){. hash =
1150b 28 68 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 (hash*1039) + pD
1150c 61 74 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 ata[i];. }. re
1150d 74 75 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 turn hash;.}.sta
1150e 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 tic u32 pager_pa
1150f 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 gehash(PgHdr *pP
11510 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 age){. return p
11511 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 ager_datahash(pP
11512 61 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 age->pPager->pag
11513 65 53 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 eSize, (unsigned
11514 20 63 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 char *)pPage->p
11515 44 61 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 Data);.}.static
11516 76 6f 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70 void pager_set_p
11517 61 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 agehash(PgHdr *p
11518 50 61 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e Page){. pPage->
11519 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 pageHash = pager
1151a 5f 70 61 67 65 68 61 73 68 28 70 50 61 67 65 29 _pagehash(pPage)
1151b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 ;.}../*.** The C
1151c 48 45 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 HECK_PAGE macro
1151d 74 61 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61 takes a PgHdr* a
1151e 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 s an argument. I
1151f 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
11520 41 47 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e AGES.** is defin
11521 65 64 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69 ed, and NDEBUG i
11522 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 s not defined, a
11523 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 n assert() state
11524 6d 65 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 ment checks.** t
11525 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 hat the page is
11526 65 69 74 68 65 72 20 64 69 72 74 79 20 6f 72 20 either dirty or
11527 73 74 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 still matches th
11528 65 20 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67 e calculated pag
11529 65 2d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 e-hash..*/.#defi
1152a 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 ne CHECK_PAGE(x)
1152b 20 63 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74 checkPage(x).st
1152c 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 atic void checkP
1152d 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b age(PgHdr *pPg){
1152e 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
1152f 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
11530 20 20 61 73 73 65 72 74 28 20 21 70 50 67 2d 3e assert( !pPg->
11531 70 61 67 65 48 61 73 68 20 7c 7c 20 70 50 61 67 pageHash || pPag
11532 65 72 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 20 er->errCode.
11533 20 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 || (pPg->flags
11534 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c &PGHDR_DIRTY) ||
11535 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d pPg->pageHash==
11536 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
11537 50 67 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a Pg) );.}..#else.
11538 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61 #define pager_da
11539 74 61 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23 tahash(X,Y) 0.#
1153a 64 65 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67 define pager_pag
1153b 65 68 61 73 68 28 58 29 20 20 30 0a 23 64 65 66 ehash(X) 0.#def
1153c 69 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 ine CHECK_PAGE(x
1153d 29 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c ).#endif /* SQL
1153e 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 ITE_CHECK_PAGES
1153f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 */../*.** When t
11540 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68 his is called th
11541 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 e journal file f
11542 6f 72 20 70 61 67 65 72 20 70 50 61 67 65 72 20 or pager pPager
11543 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a must be open..**
11544 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 The master jour
11545 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 nal file name is
11546 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 65 read from the e
11547 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 nd of the file a
11548 6e 64 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 nd .** written i
11549 6e 74 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c nto memory suppl
1154a 69 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 ied by the calle
1154b 72 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65 r. .**.** zMaste
1154c 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 r must point to
1154d 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c a buffer of at l
1154e 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 74 east nMaster byt
1154f 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a es allocated by.
11550 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 ** the caller. T
11551 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 71 his should be sq
11552 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 lite3_vfs.mxPath
11553 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 72 name+1 (to ensur
11554 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e e there is.** en
11555 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 72 ough space to wr
11556 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ite the master j
11557 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66 ournal name). If
11558 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
11559 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 nal.** name in t
1155a 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f he journal is lo
1155b 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 65 nger than nMaste
1155c 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 69 r bytes (includi
1155d 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d ng a.** nul-term
1155e 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 inator), then th
1155f 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 73 is is handled as
11560 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f if no master jo
11561 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65 urnal name.** we
11562 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 re present in th
11563 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a e journal..**.**
11564 20 49 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f If no master jo
11565 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
11566 69 73 20 70 72 65 73 65 6e 74 20 7a 4d 61 73 74 is present zMast
11567 65 72 5b 30 5d 20 69 73 20 73 65 74 20 74 6f 20 er[0] is set to
11568 30 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 0 and.** SQLITE_
11569 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a OK returned..*/.
1156a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 4d static int readM
1156b 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c asterJournal(sql
1156c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c ite3_file *pJrnl
1156d 2c 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c , char *zMaster,
1156e 20 75 33 32 20 6e 4d 61 73 74 65 72 29 7b 0a 20 u32 nMaster){.
1156f 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 20 6c int rc;. u32 l
11570 65 6e 3b 0a 20 20 69 36 34 20 73 7a 4a 3b 0a 20 en;. i64 szJ;.
11571 20 75 33 32 20 63 6b 73 75 6d 3b 0a 20 20 75 33 u32 cksum;. u3
11572 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 2 u;
11573 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e /* Unsign
11574 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 ed loop counter
11575 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
11576 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 2f 2a ar aMagic[8]; /*
11577 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c A buffer to hol
11578 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64 d the magic head
11579 65 72 20 2a 2f 0a 0a 20 20 7a 4d 61 73 74 65 72 er */.. zMaster
1157a 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 72 [0] = '\0';.. r
1157b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
1157c 65 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a eSize(pJrnl, &sz
1157d 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 J);. if( rc!=SQ
1157e 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3c 31 LITE_OK || szJ<1
1157f 36 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 6 ) return rc;..
11580 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 rc = read32bit
11581 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 2c s(pJrnl, szJ-16,
11582 20 26 6c 65 6e 29 3b 0a 20 20 69 66 28 20 72 63 &len);. if( rc
11583 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
11584 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 66 28 20 turn rc;.. if(
11585 6c 65 6e 3e 3d 6e 4d 61 73 74 65 72 20 29 7b 0a len>=nMaster ){.
11586 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
11587 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 E_OK;. }.. rc
11588 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a 72 = read32bits(pJr
11589 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63 6b 73 nl, szJ-12, &cks
1158a 75 6d 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 um);. if( rc!=S
1158b 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
1158c 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 n rc;.. rc = sq
1158d 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e lite3OsRead(pJrn
1158e 6c 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a l, aMagic, 8, sz
1158f 4a 2d 38 29 3b 0a 20 20 69 66 28 20 72 63 21 3d J-8);. if( rc!=
11590 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6d 65 6d SQLITE_OK || mem
11591 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 cmp(aMagic, aJou
11592 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29 20 29 20 rnalMagic, 8) )
11593 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 return rc;.. rc
11594 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
11595 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 72 2c (pJrnl, zMaster,
11596 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65 6e len, szJ-16-len
11597 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
11598 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 ITE_OK ){. re
11599 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 7a turn rc;. }. z
1159a 4d 61 73 74 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c Master[len] = '\
1159b 30 27 3b 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 0';.. /* See if
1159c 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 the checksum ma
1159d 74 63 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 tches the master
1159e 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f journal name */
1159f 0a 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 . for(u=0; u<le
115a0 6e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 n; u++){. cks
115a1 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d um -= zMaster[u]
115a2 3b 0a 20 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 ;. }. if( cks
115a3 75 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 um ){. /* If
115a4 74 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 the checksum doe
115a5 73 6e 27 74 20 61 64 64 20 75 70 2c 20 74 68 65 sn't add up, the
115a6 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 n one or more of
115a7 20 74 68 65 20 64 69 73 6b 20 73 65 63 74 6f 72 the disk sector
115a8 73 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e s. ** contain
115a9 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a ing the master j
115aa 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 ournal filename
115ab 69 73 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 is corrupted. Th
115ac 69 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 is means. **
115ad 64 65 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 definitely roll
115ae 62 61 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 back, so just re
115af 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 turn SQLITE_OK a
115b0 6e 64 20 72 65 70 6f 72 74 20 61 20 28 6e 75 6c nd report a (nul
115b1 29 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d ). ** master-
115b2 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 journal filename
115b3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61 .. */. zMa
115b4 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a ster[0] = '\0';.
115b5 20 20 7d 0a 20 20 20 0a 20 20 72 65 74 75 72 6e }. . return
115b6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
115b7 2a 0a 2a 2a 20 53 65 65 6b 20 74 68 65 20 6a 6f *.** Seek the jo
115b8 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 urnal file descr
115b9 69 70 74 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 iptor to the nex
115ba 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 t sector boundar
115bb 79 20 77 68 65 72 65 20 61 0a 2a 2a 20 6a 6f 75 y where a.** jou
115bc 72 6e 61 6c 20 68 65 61 64 65 72 20 6d 61 79 20 rnal header may
115bd 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 be read or writt
115be 65 6e 2e 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 en. Pager.journa
115bf 6c 4f 66 66 20 69 73 20 75 70 64 61 74 65 64 20 lOff is updated
115c0 77 69 74 68 0a 2a 2a 20 74 68 65 20 6e 65 77 20 with.** the new
115c1 73 65 65 6b 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a seek offset..**.
115c2 2a 2a 20 69 2e 65 20 66 6f 72 20 61 20 73 65 63 ** i.e for a sec
115c3 74 6f 72 20 73 69 7a 65 20 6f 66 20 35 31 32 3a tor size of 512:
115c4 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 20 4f 66 66 .**.** Input Off
115c5 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 20 set
115c6 20 4f 75 74 70 75 74 20 4f 66 66 73 65 74 0a 2a Output Offset.*
115c7 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * --------------
115c8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
115c9 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 30 20 20 ---------.** 0
115ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
115cb 20 20 20 20 20 20 20 30 0a 2a 2a 20 35 31 32 20 0.** 512
115cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
115cd 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 31 30 30 512.** 100
115ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
115cf 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 32 30 512.** 20
115d0 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 00
115d1 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 2048.**
115d2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a .*/.static i64 j
115d3 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 ournalHdrOffset(
115d4 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
115d5 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30 i64 offset = 0
115d6 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 67 ;. i64 c = pPag
115d7 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a er->journalOff;.
115d8 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f if( c ){. o
115d9 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a ffset = ((c-1)/J
115da 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
115db 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55 ager) + 1) * JOU
115dc 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
115dd 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 er);. }. asser
115de 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41 t( offset%JOURNA
115df 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
115e0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
115e1 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20 offset>=c );.
115e2 61 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d assert( (offset-
115e3 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 c)<JOURNAL_HDR_S
115e4 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 Z(pPager) );. r
115e5 65 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a eturn offset;.}.
115e6 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 65 6b static void seek
115e7 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 JournalHdr(Pager
115e8 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 70 50 61 *pPager){. pPa
115e9 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
115ea 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 = journalHdrOffs
115eb 65 74 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f et(pPager);.}../
115ec 2a 0a 2a 2a 20 57 72 69 74 65 20 7a 65 72 6f 73 *.** Write zeros
115ed 20 6f 76 65 72 20 74 68 65 20 68 65 61 64 65 72 over the header
115ee 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
115ef 66 69 6c 65 2e 20 20 54 68 69 73 20 68 61 73 20 file. This has
115f0 74 68 65 0a 2a 2a 20 65 66 66 65 63 74 20 6f 66 the.** effect of
115f1 20 69 6e 76 61 6c 69 64 61 74 69 6e 67 20 74 68 invalidating th
115f2 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 e journal file a
115f3 6e 64 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68 nd committing th
115f4 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
115f5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
115f6 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50 zeroJournalHdr(P
115f7 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
115f8 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20 t doTruncate){.
115f9 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
115fa 5f 4f 4b 3b 0a 20 20 73 74 61 74 69 63 20 63 6f _OK;. static co
115fb 6e 73 74 20 63 68 61 72 20 7a 65 72 6f 48 64 72 nst char zeroHdr
115fc 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 0a 20 20 69 [28] = {0};.. i
115fd 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
115fe 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 69 36 34 alOff ){. i64
115ff 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72 iLimit = pPager
11600 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d ->journalSizeLim
11601 69 74 3b 0a 0a 20 20 20 20 49 4f 54 52 41 43 45 it;.. IOTRACE
11602 28 28 22 4a 5a 45 52 4f 48 44 52 20 25 70 5c 6e (("JZEROHDR %p\n
11603 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 ", pPager)).
11604 69 66 28 20 64 6f 54 72 75 6e 63 61 74 65 20 7c if( doTruncate |
11605 7c 20 69 4c 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 | iLimit==0 ){.
11606 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
11607 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 3OsTruncate(pPag
11608 65 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 er->jfd, 0);.
11609 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
1160a 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
1160b 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a e(pPager->jfd, z
1160c 65 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28 7a eroHdr, sizeof(z
1160d 65 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20 20 eroHdr), 0);.
1160e 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
1160f 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 61 QLITE_OK && !pPa
11610 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 ger->noSync ){.
11611 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
11612 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 3OsSync(pPager->
11613 6a 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 jfd, SQLITE_SYNC
11614 5f 44 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65 72 _DATAONLY|pPager
11615 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 ->sync_flags);.
11616 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 }.. /* At
11617 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 74 this point the t
11618 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
11619 6d 6d 69 74 74 65 64 20 62 75 74 20 74 68 65 20 mmitted but the
1161a 77 72 69 74 65 20 6c 6f 63 6b 20 0a 20 20 20 20 write lock .
1161b 2a 2a 20 69 73 20 73 74 69 6c 6c 20 68 65 6c 64 ** is still held
1161c 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 49 66 on the file. If
1161d 20 74 68 65 72 65 20 69 73 20 61 20 73 69 7a 65 there is a size
1161e 20 6c 69 6d 69 74 20 63 6f 6e 66 69 67 75 72 65 limit configure
1161f 64 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 d for . ** th
11620 65 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 e persistent jou
11621 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 6a 6f 75 rnal and the jou
11622 72 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e rnal file curren
11623 74 6c 79 20 63 6f 6e 73 75 6d 65 73 20 6d 6f 72 tly consumes mor
11624 65 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 74 e. ** space t
11625 68 61 6e 20 74 68 61 74 20 6c 69 6d 69 74 20 61 han that limit a
11626 6c 6c 6f 77 73 20 66 6f 72 2c 20 74 72 75 6e 63 llows for, trunc
11627 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 72 ate it now. Ther
11628 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 e is no need.
11629 20 2a 2a 20 74 6f 20 73 79 6e 63 20 74 68 65 20 ** to sync the
1162a 66 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 file following t
1162b 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 his operation..
1162c 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 */. if( rc
1162d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
1162e 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 20 Limit>0 ){.
1162f 20 69 36 34 20 73 7a 3b 0a 20 20 20 20 20 20 72 i64 sz;. r
11630 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
11631 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 eSize(pPager->jf
11632 64 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20 69 d, &sz);. i
11633 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
11634 20 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29 7b && sz>iLimit ){
11635 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
11636 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 lite3OsTruncate(
11637 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c 69 pPager->jfd, iLi
11638 6d 69 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 mit);. }.
11639 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1163a 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
1163b 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d e journal file m
1163c 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e ust be open when
1163d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1163e 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e called. A journ
1163f 61 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a 4f al.** header (JO
11640 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 URNAL_HDR_SZ byt
11641 65 73 29 20 69 73 20 77 72 69 74 74 65 6e 20 69 es) is written i
11642 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 nto the journal
11643 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 63 file at the.** c
11644 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e urrent location.
11645 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 .**.** The forma
11646 74 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 t for the journa
11647 6c 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 l header is as f
11648 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20 62 ollows:.** - 8 b
11649 79 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65 6e ytes: Magic iden
1164a 74 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 tifying journal
1164b 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20 62 format..** - 4 b
1164c 79 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 20 ytes: Number of
1164d 72 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 6e records in journ
1164e 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e al, or -1 no-syn
1164f 63 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a c mode is on..**
11650 20 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e 64 - 4 bytes: Rand
11651 6f 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66 om number used f
11652 6f 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a 2a or page hash..**
11653 20 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69 74 - 4 bytes: Init
11654 69 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67 ial database pag
11655 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20 e count..** - 4
11656 62 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73 69 bytes: Sector si
11657 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20 70 ze used by the p
11658 72 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 rocess that wrot
11659 65 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a e this journal..
1165a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44 61 ** - 4 bytes: Da
1165b 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 tabase page size
1165c 2e 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 ..** .** Followe
1165d 64 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 d by (JOURNAL_HD
1165e 52 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 R_SZ - 28) bytes
1165f 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 of unused space
11660 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11661 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
11662 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
11663 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
11664 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 48 E_OK;. char *zH
11665 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d 3e eader = pPager->
11666 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 75 33 32 pTmpSpace;. u32
11667 20 6e 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 nHeader = pPage
11668 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 75 r->pageSize;. u
11669 33 32 20 6e 57 72 69 74 65 3b 0a 20 20 69 6e 74 32 nWrite;. int
1166a 20 69 69 3b 0a 0a 20 20 69 66 28 20 6e 48 65 61 ii;.. if( nHea
1166b 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f der>JOURNAL_HDR_
1166c 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 SZ(pPager) ){.
1166d 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f 55 52 nHeader = JOUR
1166e 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
1166f 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 r);. }.. /* If
11670 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 there are activ
11671 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 e savepoints and
11672 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 77 65 72 any of them wer
11673 65 20 63 72 65 61 74 65 64 20 73 69 6e 63 65 20 e created since
11674 74 68 65 0a 20 20 2a 2a 20 6d 6f 73 74 20 72 65 the. ** most re
11675 63 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 cent journal hea
11676 64 65 72 20 77 61 73 20 77 72 69 74 74 65 6e 2c der was written,
11677 20 75 70 64 61 74 65 20 74 68 65 20 50 61 67 65 update the Page
11678 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f rSavepoint.iHdrO
11679 66 66 0a 20 20 2a 2a 20 66 69 65 6c 64 73 20 6e ff. ** fields n
1167a 6f 77 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 ow.. */. for(i
1167b 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e i=0; ii<pPager->
1167c 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b nSavepoint; ii++
1167d 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 ){. if( pPage
1167e 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 r->aSavepoint[ii
1167f 5d 2e 69 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 ].iHdrOffset==0
11680 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d ){. pPager-
11681 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e >aSavepoint[ii].
11682 69 48 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 iHdrOffset = pPa
11683 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
11684 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 65 . }. }.. se
11685 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 ekJournalHdr(pPa
11686 67 65 72 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e ger);. pPager->
11687 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 50 61 journalHdr = pPa
11688 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
11689 0a 0a 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 .. memcpy(zHead
1168a 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 er, aJournalMagi
1168b 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e c, sizeof(aJourn
1168c 61 6c 4d 61 67 69 63 29 29 3b 0a 0a 20 20 2f 2a alMagic));.. /*
1168d 20 0a 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65 . ** Write the
1168e 20 6e 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68 nRec Field - th
1168f 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
11690 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f records that fo
11691 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a llow this. ** j
11692 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e ournal header. N
11693 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 ormally, zero is
11694 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73 written to this
11695 20 76 61 6c 75 65 20 61 74 20 74 68 69 73 20 74 value at this t
11696 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 ime.. ** After
11697 74 68 65 20 72 65 63 6f 72 64 73 20 61 72 65 20 the records are
11698 61 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 added to the jou
11699 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f rnal (and the jo
1169a 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 urnal synced, .
1169b 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 ** if in full-s
1169c 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a ync mode), the z
1169d 65 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74 ero is overwritt
1169e 65 6e 20 77 69 74 68 20 74 68 65 20 74 72 75 65 en with the true
1169f 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 number. ** of
116a0 72 65 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e records (see syn
116a1 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a cJournal()).. *
116a2 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20 *. ** A faster
116a3 61 6c 74 65 72 6e 61 74 69 76 65 20 69 73 20 74 alternative is t
116a4 6f 20 77 72 69 74 65 20 30 78 46 46 46 46 46 46 o write 0xFFFFFF
116a5 46 46 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66 FF to the nRec f
116a6 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 ield. When. **
116a7 72 65 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 reading the jour
116a8 6e 61 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74 nal this value t
116a9 65 6c 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61 ells SQLite to a
116aa 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20 ssume that the.
116ab 20 2a 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20 ** rest of the
116ac 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e journal file con
116ad 74 61 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65 tains valid page
116ae 20 72 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61 records. This a
116af 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 ssumption. ** i
116b0 73 20 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 s dangerous, as
116b1 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 if a failure occ
116b2 75 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 ured whilst writ
116b3 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ing to the journ
116b4 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 al. ** file it
116b5 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 may contain some
116b6 20 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 garbage data. T
116b7 68 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 here are two sce
116b8 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 narios. ** wher
116b9 65 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 e this risk can
116ba 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a be ignored:. **
116bb 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 . ** * When t
116bc 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e he pager is in n
116bd 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 o-sync mode. Cor
116be 72 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c ruption can foll
116bf 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f ow a. ** po
116c0 77 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 wer failure in t
116c1 68 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e his case anyway.
116c2 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 . **. ** * W
116c3 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 hen the SQLITE_I
116c4 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
116c5 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 flag is set. Th
116c6 69 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 is guarantees.
116c7 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62 ** that garb
116c8 61 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 age data is neve
116c9 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 r appended to th
116ca 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
116cb 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 70 50 */. assert(pP
116cc 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f ager->fd->pMetho
116cd 64 73 7c 7c 70 50 61 67 65 72 2d 3e 6e 6f 53 79 ds||pPager->noSy
116ce 6e 63 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 nc);. if( (pPag
116cf 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28 er->noSync) || (
116d0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
116d1 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
116d2 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20 ALMODE_MEMORY).
116d3 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44 || (sqlite3OsD
116d4 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
116d5 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 tics(pPager->fd)
116d6 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 &SQLITE_IOCAP_SA
116d7 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b FE_APPEND) . ){
116d8 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26 . put32bits(&
116d9 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
116da 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 JournalMagic)],
116db 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 7d 0xffffffff);. }
116dc 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33 32 62 else{. put32b
116dd 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
116de 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
116df 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 c)], 0);. }..
116e0 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68 /* The random ch
116e1 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c eck-hash initial
116e2 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74 iser */ . sqlit
116e3 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 e3_randomness(si
116e4 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73 zeof(pPager->cks
116e5 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72 umInit), &pPager
116e6 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 ->cksumInit);.
116e7 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
116e8 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
116e9 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61 alMagic)+4], pPa
116ea 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b ger->cksumInit);
116eb 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61 . /* The initia
116ec 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 l database size
116ed 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 */. put32bits(&
116ee 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
116ef 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d JournalMagic)+8]
116f0 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 , pPager->dbOrig
116f1 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20 Size);. /* The
116f2 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 assumed sector s
116f3 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f ize for this pro
116f4 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32 62 cess */. put32b
116f5 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
116f6 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
116f7 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d 3e c)+12], pPager->
116f8 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 sectorSize);..
116f9 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 69 6e 67 20 /* Initializing
116fa 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 the tail of the
116fb 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 6e 65 buffer is not ne
116fc 63 65 73 73 61 72 79 2e 20 20 45 76 65 72 79 74 cessary. Everyt
116fd 68 69 6e 67 0a 20 20 2a 2a 20 77 6f 72 6b 73 20 hing. ** works
116fe 66 69 6e 64 20 69 66 20 74 68 65 20 66 6f 6c 6c find if the foll
116ff 6f 77 69 6e 67 20 6d 65 6d 73 65 74 28 29 20 69 owing memset() i
11700 73 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20 s omitted. But
11701 69 6e 69 74 69 61 6c 69 7a 69 6e 67 0a 20 20 2a initializing. *
11702 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 72 65 * the memory pre
11703 76 65 6e 74 73 20 76 61 6c 67 72 69 6e 64 20 66 vents valgrind f
11704 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e 69 6e 67 2c rom complaining,
11705 20 73 6f 20 77 65 20 61 72 65 20 77 69 6c 6c 69 so we are willi
11706 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 61 6b 65 20 ng to. ** take
11707 74 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 the performance
11708 68 69 74 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 hit.. */. mems
11709 65 74 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 et(&zHeader[size
1170a 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
1170b 29 2b 31 36 5d 2c 20 30 2c 0a 20 20 20 20 20 20 )+16], 0,.
1170c 20 20 20 6e 48 65 61 64 65 72 2d 28 73 69 7a 65 nHeader-(size
1170d 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
1170e 29 2b 31 36 29 29 3b 0a 0a 20 20 69 66 28 20 70 )+16));.. if( p
1170f 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
11710 72 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 r==0 ){. /* T
11711 68 65 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a he page size */.
11712 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26 7a put32bits(&z
11713 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a Header[sizeof(aJ
11714 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36 5d ournalMagic)+16]
11715 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
11716 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 ze);. }.. for(
11717 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51 nWrite=0; rc==SQ
11718 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c LITE_OK&&nWrite<
11719 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
1171a 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d Pager); nWrite+=
1171b 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 4f nHeader){. IO
1171c 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 TRACE(("JHDR %p
1171d 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 %lld %d\n", pPag
1171e 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 er, pPager->jour
1171f 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 29 nalHdr, nHeader)
11720 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ). rc = sqlit
11721 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
11722 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20 ->jfd, zHeader,
11723 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 2d nHeader, pPager-
11724 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 >journalOff);.
11725 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11726 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b lOff += nHeader;
11727 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 . }.. return r
11728 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 c;.}../*.** The
11729 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 journal file mus
1172a 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 t be open when t
1172b 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 his is called. A
1172c 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
1172d 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c file.** (JOURNAL
1172e 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 _HDR_SZ bytes) i
1172f 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 s read from the
11730 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e current location
11731 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a in the journal.
11732 2a 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 72 ** file. The cur
11733 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e rent location in
11734 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11735 65 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a e is given by.**
11736 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11737 4f 66 66 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e Off. See commen
11738 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f ts above functio
11739 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 n writeJournalHd
1173a 72 28 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73 r() for.** a des
1173b 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 cription of the
1173c 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 journal header f
1173d 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ormat..**.** If
1173e 74 68 65 20 68 65 61 64 65 72 20 69 73 20 72 65 the header is re
1173f 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c ad successfully,
11740 20 2a 6e 52 65 63 20 69 73 20 73 65 74 20 74 6f *nRec is set to
11741 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
11742 2a 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 * page records f
11743 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65 ollowing this he
11744 61 64 65 72 20 61 6e 64 20 2a 64 62 53 69 7a 65 ader and *dbSize
11745 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 73 is set to the s
11746 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 ize of the.** da
11747 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 74 68 tabase before th
11748 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 e transaction be
11749 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 41 gan, in pages. A
1174a 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 lso, pPager->cks
1174b 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 74 umInit.** is set
1174c 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65 to the value re
1174d 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 ad from the jour
1174e 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 4c 49 nal header. SQLI
1174f 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
11750 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 d.** in this cas
11751 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
11752 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 journal header f
11753 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 ile appears to b
11754 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c e corrupted, SQL
11755 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 72 ITE_DONE is.** r
11756 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 6e 52 65 eturned and *nRe
11757 63 20 61 6e 64 20 2a 64 62 53 69 7a 65 20 61 72 c and *dbSize ar
11758 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 66 e undefined. If
11759 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 JOURNAL_HDR_SZ
1175a 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 bytes.** cannot
1175b 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 be read from the
1175c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e journal file an
1175d 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
1175e 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
1175f 69 63 20 69 6e 74 20 72 65 61 64 4a 6f 75 72 6e ic int readJourn
11760 61 6c 48 64 72 28 0a 20 20 50 61 67 65 72 20 2a alHdr(. Pager *
11761 70 50 61 67 65 72 2c 20 0a 20 20 69 36 34 20 6a pPager, . i64 j
11762 6f 75 72 6e 61 6c 53 69 7a 65 2c 0a 20 20 75 33 ournalSize,. u3
11763 32 20 2a 70 4e 52 65 63 2c 20 0a 20 20 75 33 32 2 *pNRec, . u32
11764 20 2a 70 44 62 53 69 7a 65 0a 29 7b 0a 20 20 69 *pDbSize.){. i
11765 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65 nt rc;. unsigne
11766 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38 5d d char aMagic[8]
11767 3b 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f ; /* A buffer to
11768 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 hold the magic
11769 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 header */. i64
1176a 6a 72 6e 6c 4f 66 66 3b 0a 20 20 75 33 32 20 69 jrnlOff;. u32 i
1176b 50 61 67 65 53 69 7a 65 3b 0a 20 20 75 33 32 20 PageSize;. u32
1176c 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a 0a 20 20 iSectorSize;..
1176d 73 65 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70 seekJournalHdr(p
1176e 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50 Pager);. if( pP
1176f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11770 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 +JOURNAL_HDR_SZ(
11771 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61 pPager) > journa
11772 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 lSize ){. ret
11773 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b urn SQLITE_DONE;
11774 0a 20 20 7d 0a 20 20 6a 72 6e 6c 4f 66 66 20 3d . }. jrnlOff =
11775 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11776 4f 66 66 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c Off;.. rc = sql
11777 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 ite3OsRead(pPage
11778 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 r->jfd, aMagic,
11779 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c 20 sizeof(aMagic),
1177a 6a 72 6e 6c 4f 66 66 29 3b 0a 20 20 69 66 28 20 jrnlOff);. if(
1177b 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1177c 20 20 6a 72 6e 6c 4f 66 66 20 2b 3d 20 73 69 7a jrnlOff += siz
1177d 65 6f 66 28 61 4d 61 67 69 63 29 3b 0a 0a 20 20 eof(aMagic);..
1177e 69 66 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 if( memcmp(aMagi
1177f 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 c, aJournalMagic
11780 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 , sizeof(aMagic)
11781 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 )!=0 ){. retu
11782 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a rn SQLITE_DONE;.
11783 20 20 7d 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 }.. rc = read
11784 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
11785 66 64 2c 20 6a 72 6e 6c 4f 66 66 2c 20 70 4e 52 fd, jrnlOff, pNR
11786 65 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 ec);. if( rc )
11787 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 return rc;.. rc
11788 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 = read32bits(pP
11789 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f ager->jfd, jrnlO
1178a 66 66 2b 34 2c 20 26 70 50 61 67 65 72 2d 3e 63 ff+4, &pPager->c
1178b 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 69 66 28 ksumInit);. if(
1178c 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
1178d 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 .. rc = read32b
1178e 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c its(pPager->jfd,
1178f 20 6a 72 6e 6c 4f 66 66 2b 38 2c 20 70 44 62 53 jrnlOff+8, pDbS
11790 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 ize);. if( rc )
11791 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 return rc;.. i
11792 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
11793 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 alOff==0 ){.
11794 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
11795 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e pPager->jfd, jrn
11796 6c 4f 66 66 2b 31 36 2c 20 26 69 50 61 67 65 53 lOff+16, &iPageS
11797 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ize);. if( rc
11798 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 ) return rc;..
11799 20 20 20 69 66 28 20 69 50 61 67 65 53 69 7a 65 if( iPageSize
1179a 3c 35 31 32 20 0a 20 20 20 20 20 7c 7c 20 69 50 <512 . || iP
1179b 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d ageSize>SQLITE_M
1179c 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 0a 20 20 AX_PAGE_SIZE .
1179d 20 20 20 7c 7c 20 28 28 69 50 61 67 65 53 69 7a || ((iPageSiz
1179e 65 2d 31 29 26 69 50 61 67 65 53 69 7a 65 29 21 e-1)&iPageSize)!
1179f 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 =0 . ){.
117a0 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 2d /* If the page-
117a1 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 size in the jour
117a2 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 69 6e nal-header is in
117a3 76 61 6c 69 64 2c 20 74 68 65 6e 20 74 68 65 20 valid, then the
117a4 70 72 6f 63 65 73 73 0a 20 20 20 20 20 20 2a 2a process. **
117a5 20 74 68 61 74 20 77 72 6f 74 65 20 74 68 65 20 that wrote the
117a6 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 6d journal-header m
117a7 75 73 74 20 68 61 76 65 20 63 72 61 73 68 65 64 ust have crashed
117a8 20 62 65 66 6f 72 65 20 74 68 65 20 68 65 61 64 before the head
117a9 65 72 0a 20 20 20 20 20 20 2a 2a 20 77 61 73 20 er. ** was
117aa 73 79 6e 63 65 64 2e 20 49 6e 20 74 68 69 73 20 synced. In this
117ab 63 61 73 65 20 73 74 6f 70 20 72 65 61 64 69 6e case stop readin
117ac 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 g the journal fi
117ad 6c 65 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a le here.. *
117ae 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c /. rc = SQL
117af 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 ITE_DONE;. }e
117b0 6c 73 65 7b 0a 20 20 20 20 20 20 75 31 36 20 70 lse{. u16 p
117b1 61 67 65 73 69 7a 65 20 3d 20 28 75 31 36 29 69 agesize = (u16)i
117b2 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 PageSize;.
117b3 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
117b4 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61 rSetPagesize(pPa
117b5 67 65 72 2c 20 26 70 61 67 65 73 69 7a 65 29 3b ger, &pagesize);
117b6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 . assert( r
117b7 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
117b8 70 61 67 65 73 69 7a 65 3d 3d 28 75 31 36 29 69 pagesize==(u16)i
117b9 50 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 PageSize );.
117ba 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 }. if( rc ) r
117bb 65 74 75 72 6e 20 72 63 3b 0a 20 20 0a 20 20 20 eturn rc;. .
117bc 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 /* Update the a
117bd 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 69 ssumed sector-si
117be 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 ze to match the
117bf 76 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a 20 value used by .
117c0 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 ** the proces
117c1 73 20 74 68 61 74 20 63 72 65 61 74 65 64 20 74 s that created t
117c2 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 his journal. If
117c3 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 this journal was
117c4 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 . ** created
117c5 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74 68 by a process oth
117c6 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e 65 er than this one
117c7 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 , then this rout
117c8 69 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20 62 65 ine. ** is be
117c9 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 ing called from
117ca 77 69 74 68 69 6e 20 70 61 67 65 72 5f 70 6c 61 within pager_pla
117cb 79 62 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63 yback(). The loc
117cc 61 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20 al value. **
117cd 6f 66 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 of Pager.sectorS
117ce 69 7a 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 ize is restored
117cf 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 at the end of th
117d0 61 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20 20 20 at routine..
117d1 2a 2f 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 */. rc = read
117d2 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
117d3 66 64 2c 20 6a 72 6e 6c 4f 66 66 2b 31 32 2c 20 fd, jrnlOff+12,
117d4 26 69 53 65 63 74 6f 72 53 69 7a 65 29 3b 0a 20 &iSectorSize);.
117d5 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
117d6 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 28 rn rc;. if( (
117d7 69 53 65 63 74 6f 72 53 69 7a 65 26 28 69 53 65 iSectorSize&(iSe
117d8 63 74 6f 72 53 69 7a 65 2d 31 29 29 0a 20 20 20 ctorSize-1)).
117d9 20 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a || iSectorSiz
117da 65 3c 35 31 32 0a 20 20 20 20 20 20 7c 7c 20 69 e<512. || i
117db 53 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 SectorSize>MAX_S
117dc 45 43 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 29 ECTOR_SIZE. )
117dd 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
117de 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 QLITE_DONE;.
117df 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 }. pPager->se
117e0 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 65 63 74 ctorSize = iSect
117e1 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70 orSize;. }.. p
117e2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
117e3 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 f += JOURNAL_HDR
117e4 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 72 _SZ(pPager);. r
117e5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
117e6 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 .}.../*.** Write
117e7 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6d 61 the supplied ma
117e8 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
117e9 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e e into the journ
117ea 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 al file for page
117eb 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 20 74 r.** pPager at t
117ec 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 he current locat
117ed 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 72 20 ion. The master
117ee 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d 75 73 journal name mus
117ef 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a 2a 2a t be the last.**
117f0 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e 20 74 thing written t
117f1 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 o a journal file
117f2 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 . If the pager i
117f3 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d s in full-sync m
117f4 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 ode, the.** jour
117f5 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 nal file descrip
117f6 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65 64 20 tor is advanced
117f7 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63 74 to the next sect
117f8 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65 66 6f or boundary befo
117f9 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 69 re.** anything i
117fa 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65 20 66 s written. The f
117fb 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 ormat is:.**.**
117fc 2b 20 34 20 62 79 74 65 73 3a 20 50 41 47 45 52 + 4 bytes: PAGER
117fd 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 2b 20 4e _MJ_PGNO..** + N
117fe 20 62 79 74 65 73 3a 20 6c 65 6e 67 74 68 20 6f bytes: length o
117ff 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c f master journal
11800 20 6e 61 6d 65 2e 0a 2a 2a 20 2b 20 34 20 62 79 name..** + 4 by
11801 74 65 73 3a 20 4e 0a 2a 2a 20 2b 20 34 20 62 79 tes: N.** + 4 by
11802 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 tes: Master jour
11803 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 75 nal name checksu
11804 6d 2e 0a 2a 2a 20 2b 20 38 20 62 79 74 65 73 3a m..** + 8 bytes:
11805 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d aJournalMagic[]
11806 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74 ..**.** The mast
11807 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 er journal page
11808 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68 65 20 checksum is the
11809 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74 65 73 sum of the bytes
1180a 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 0a 2a in the master.*
1180b 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 0a * journal name..
1180c 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72 **.** If zMaster
1180d 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 is a NULL point
1180e 65 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61 er (occurs for a
1180f 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 single database
11810 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a transaction), .
11811 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 ** this call is
11812 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 a no-op..*/.stat
11813 69 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74 ic int writeMast
11814 65 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 erJournal(Pager
11815 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 *pPager, const c
11816 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 har *zMaster){.
11817 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6c int rc;. int l
11818 65 6e 3b 20 0a 20 20 69 6e 74 20 69 3b 20 0a 20 en; . int i; .
11819 20 69 36 34 20 6a 72 6e 6c 4f 66 66 3b 0a 20 20 i64 jrnlOff;.
1181a 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 0a 20 20 i64 jrnlSize;.
1181b 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b 0a 20 u32 cksum = 0;.
1181c 20 63 68 61 72 20 7a 42 75 66 5b 73 69 7a 65 6f char zBuf[sizeo
1181d 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
1181e 2b 32 2a 34 5d 3b 0a 0a 20 20 69 66 28 20 21 7a +2*4];.. if( !z
1181f 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 67 65 72 Master || pPager
11820 2d 3e 73 65 74 4d 61 73 74 65 72 20 29 20 72 65 ->setMaster ) re
11821 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
11822 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f if( pPager->jo
11823 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
11824 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
11825 4f 52 59 20 29 20 72 65 74 75 72 6e 20 53 51 4c ORY ) return SQL
11826 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72 ITE_OK;. pPager
11827 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 31 3b ->setMaster = 1;
11828 0a 0a 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 .. len = sqlite
11829 33 53 74 72 6c 65 6e 33 30 28 7a 4d 61 73 74 65 3Strlen30(zMaste
1182a 72 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 r);. for(i=0; i
1182b 3c 6c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 <len; i++){.
1182c 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72 cksum += zMaster
1182d 5b 69 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 [i];. }.. /* I
1182e 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d f in full-sync m
1182f 6f 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f 20 ode, advance to
11830 74 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73 65 the next disk se
11831 63 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69 74 ctor before writ
11832 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 73 ing. ** the mas
11833 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
11834 2e 20 54 68 69 73 20 69 73 20 69 6e 20 63 61 73 . This is in cas
11835 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 e the previous p
11836 61 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 age written to.
11837 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ** the journal
11838 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
11839 20 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 20 synced.. */.
1183a 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c if( pPager->full
1183b 53 79 6e 63 20 29 7b 0a 20 20 20 20 73 65 65 6b Sync ){. seek
1183c 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 JournalHdr(pPage
1183d 72 29 3b 0a 20 20 7d 0a 20 20 6a 72 6e 6c 4f 66 r);. }. jrnlOf
1183e 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 f = pPager->jour
1183f 6e 61 6c 4f 66 66 3b 0a 20 20 70 50 61 67 65 72 nalOff;. pPager
11840 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 ->journalOff +=
11841 28 6c 65 6e 2b 32 30 29 3b 0a 0a 20 20 72 63 20 (len+20);.. rc
11842 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 = write32bits(pP
11843 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f ager->jfd, jrnlO
11844 66 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e ff, PAGER_MJ_PGN
11845 4f 28 70 50 61 67 65 72 29 29 3b 0a 20 20 69 66 O(pPager));. if
11846 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
11847 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6a ) return rc;. j
11848 72 6e 6c 4f 66 66 20 2b 3d 20 34 3b 0a 0a 20 20 rnlOff += 4;..
11849 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
1184a 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ite(pPager->jfd,
1184b 20 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 6a zMaster, len, j
1184c 72 6e 6c 4f 66 66 29 3b 0a 20 20 69 66 28 20 72 rnlOff);. if( r
1184d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
1184e 65 74 75 72 6e 20 72 63 3b 0a 20 20 6a 72 6e 6c eturn rc;. jrnl
1184f 4f 66 66 20 2b 3d 20 6c 65 6e 3b 0a 0a 20 20 70 Off += len;.. p
11850 75 74 33 32 62 69 74 73 28 7a 42 75 66 2c 20 6c ut32bits(zBuf, l
11851 65 6e 29 3b 0a 20 20 70 75 74 33 32 62 69 74 73 en);. put32bits
11852 28 26 7a 42 75 66 5b 34 5d 2c 20 63 6b 73 75 6d (&zBuf[4], cksum
11853 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 );. memcpy(&zBu
11854 66 5b 38 5d 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 f[8], aJournalMa
11855 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 gic, sizeof(aJou
11856 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 72 rnalMagic));. r
11857 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
11858 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 te(pPager->jfd,
11859 7a 42 75 66 2c 20 38 2b 73 69 7a 65 6f 66 28 61 zBuf, 8+sizeof(a
1185a 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2c 20 6a JournalMagic), j
1185b 72 6e 6c 4f 66 66 29 3b 0a 20 20 6a 72 6e 6c 4f rnlOff);. jrnlO
1185c 66 66 20 2b 3d 20 38 2b 73 69 7a 65 6f 66 28 61 ff += 8+sizeof(a
1185d 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 20 JournalMagic);.
1185e 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
1185f 63 20 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 c = !pPager->noS
11860 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ync;.. /* If th
11861 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 70 65 e pager is in pe
11862 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 ristent-journal
11863 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70 mode, then the p
11864 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f hysical . ** jo
11865 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 urnal-file may e
11866 78 74 65 6e 64 20 70 61 73 74 20 74 68 65 20 65 xtend past the e
11867 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 nd of the master
11868 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 -journal name.
11869 2a 2a 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f ** and 8 bytes o
1186a 66 20 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73 f magic data jus
1186b 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 t written to the
1186c 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a file. This is .
1186d 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 ** dangerous b
1186e 65 63 61 75 73 65 20 74 68 65 20 63 6f 64 65 20 ecause the code
1186f 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f to rollback a ho
11870 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 t-journal file.
11871 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 ** will not be
11872 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65 able to find the
11873 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 master-journal
11874 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e name to determin
11875 65 20 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20 e . ** whether
11876 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e or not the journ
11877 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a al is hot. . **
11878 0a 20 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68 . ** Easiest th
11879 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 ing to do in thi
1187a 73 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f s scenario is to
1187b 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f truncate the jo
1187c 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 urnal . ** file
1187d 20 74 6f 20 74 68 65 20 72 65 71 75 69 72 65 64 to the required
1187e 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 size.. */ . i
1187f 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f f( (rc==SQLITE_O
11880 4b 29 0a 20 20 20 26 26 20 28 72 63 20 3d 20 73 K). && (rc = s
11881 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
11882 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a (pPager->jfd, &j
11883 72 6e 6c 53 69 7a 65 29 29 3d 3d 53 51 4c 49 54 rnlSize))==SQLIT
11884 45 5f 4f 4b 0a 20 20 20 26 26 20 6a 72 6e 6c 53 E_OK. && jrnlS
11885 69 7a 65 3e 6a 72 6e 6c 4f 66 66 0a 20 20 29 7b ize>jrnlOff. ){
11886 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
11887 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 3OsTruncate(pPag
11888 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 er->jfd, jrnlOff
11889 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1188a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e rc;.}../*.** Fin
1188b 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 d a page in the
1188c 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e hash table given
1188d 20 69 74 73 20 70 61 67 65 20 6e 75 6d 62 65 72 its page number
1188e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 . Return.** a p
1188f 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 ointer to the pa
11890 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6e 6f ge or NULL if no
11891 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 t found..*/.stat
11892 69 63 20 50 67 48 64 72 20 2a 70 61 67 65 72 5f ic PgHdr *pager_
11893 6c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 lookup(Pager *pP
11894 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 ager, Pgno pgno)
11895 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 {. PgHdr *p;.
11896 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 sqlite3PcacheFet
11897 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ch(pPager->pPCac
11898 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 he, pgno, 0, &p)
11899 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
1189a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 ./*.** Clear the
1189b 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 in-memory cache
1189c 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a . This routine.
1189d 2a 2a 20 73 65 74 73 20 74 68 65 20 73 74 61 74 ** sets the stat
1189e 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 62 e of the pager b
1189f 61 63 6b 20 74 6f 20 77 68 61 74 20 69 74 20 77 ack to what it w
118a0 61 73 20 77 68 65 6e 20 69 74 20 77 61 73 20 66 as when it was f
118a1 69 72 73 74 0a 2a 2a 20 6f 70 65 6e 65 64 2e 20 irst.** opened.
118a2 20 41 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 Any outstanding
118a3 20 70 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c pages are inval
118a4 69 64 61 74 65 64 20 61 6e 64 20 73 75 62 73 65 idated and subse
118a5 71 75 65 6e 74 20 61 74 74 65 6d 70 74 73 0a 2a quent attempts.*
118a6 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68 6f 73 * to access thos
118a7 65 20 70 61 67 65 73 20 77 69 6c 6c 20 6c 69 6b e pages will lik
118a8 65 6c 79 20 72 65 73 75 6c 74 20 69 6e 20 61 20 ely result in a
118a9 63 6f 72 65 64 75 6d 70 2e 0a 2a 2f 0a 73 74 61 coredump..*/.sta
118aa 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 72 tic void pager_r
118ab 65 73 65 74 28 50 61 67 65 72 20 2a 70 50 61 67 eset(Pager *pPag
118ac 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 er){. if( pPage
118ad 72 2d 3e 65 72 72 43 6f 64 65 20 29 20 72 65 74 r->errCode ) ret
118ae 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 urn;. sqlite3Pc
118af 61 63 68 65 43 6c 65 61 72 28 70 50 61 67 65 72 acheClear(pPager
118b0 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f ->pPCache);.}../
118b1 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 73 74 *.** Free all st
118b2 72 75 63 74 75 72 65 73 20 69 6e 20 74 68 65 20 ructures in the
118b3 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 Pager.aSavepoint
118b4 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 73 65 74 [] array and set
118b5 20 62 6f 74 68 0a 2a 2a 20 50 61 67 65 72 2e 61 both.** Pager.a
118b6 53 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 50 61 Savepoint and Pa
118b7 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 20 74 ger.nSavepoint t
118b8 6f 20 7a 65 72 6f 2e 20 43 6c 6f 73 65 20 74 68 o zero. Close th
118b9 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a e sub-journal.**
118ba 20 69 66 20 69 74 20 69 73 20 6f 70 65 6e 20 61 if it is open a
118bb 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 73 20 nd the pager is
118bc 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65 not in exclusive
118bd 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 mode..*/.static
118be 20 76 6f 69 64 20 72 65 6c 65 61 73 65 41 6c 6c void releaseAll
118bf 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 Savepoint(Pager
118c0 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 *pPager){. int
118c1 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 ii;. for(ii=0;
118c2 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 ii<pPager->nSave
118c3 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 point; ii++){.
118c4 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
118c5 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 estroy(pPager->a
118c6 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 Savepoint[ii].pI
118c7 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d nSavepoint);. }
118c8 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e . if( !pPager->
118c9 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b exclusiveMode ){
118ca 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
118cb 6f 73 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 ose(pPager->sjfd
118cc 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
118cd 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e 61 53 _free(pPager->aS
118ce 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 70 50 61 avepoint);. pPa
118cf 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 ger->aSavepoint
118d0 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e = 0;. pPager->n
118d1 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 Savepoint = 0;.
118d2 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 pPager->stmtNRe
118d3 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 c = 0;.}../*.**
118d4 53 65 74 20 74 68 65 20 62 69 74 20 6e 75 6d 62 Set the bit numb
118d5 65 72 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 50 er pgno in the P
118d6 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 agerSavepoint.pI
118d7 6e 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 nSavepoint bitve
118d8 63 73 20 6f 66 0a 2a 2a 20 61 6c 6c 20 6f 70 65 cs of.** all ope
118d9 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 0a 2a 2f n savepoints..*/
118da 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 54 .static int addT
118db 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 oSavepointBitvec
118dc 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c s(Pager *pPager,
118dd 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 Pgno pgno){. i
118de 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
118df 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
118e0 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
118e1 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
118e2 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c ; /* Resul
118e3 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 66 6f 72 t code */.. for
118e4 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 (ii=0; ii<pPager
118e5 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 ->nSavepoint; ii
118e6 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 53 61 ++){. PagerSa
118e7 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 vepoint *p = &pP
118e8 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 ager->aSavepoint
118e9 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 67 [ii];. if( pg
118ea 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 29 7b 0a no<=p->nOrig ){.
118eb 20 20 20 20 20 20 72 63 20 7c 3d 20 73 71 6c 69 rc |= sqli
118ec 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2d 3e te3BitvecSet(p->
118ed 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 70 67 pInSavepoint, pg
118ee 6e 6f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 no);. asser
118ef 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
118f0 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e || rc==SQLITE_N
118f1 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 OMEM );. }.
118f2 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
118f3 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 ../*.** Unlock t
118f4 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
118f5 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 . .**.** If the
118f6 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 pager is current
118f7 6c 79 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 ly in error stat
118f8 65 2c 20 64 69 73 63 61 72 64 20 74 68 65 20 63 e, discard the c
118f9 6f 6e 74 65 6e 74 73 20 6f 66 20 0a 2a 2a 20 74 ontents of .** t
118fa 68 65 20 63 61 63 68 65 20 61 6e 64 20 72 65 73 he cache and res
118fb 65 74 20 74 68 65 20 50 61 67 65 72 20 73 74 72 et the Pager str
118fc 75 63 74 75 72 65 20 69 6e 74 65 72 6e 61 6c 20 ucture internal
118fd 73 74 61 74 65 2e 20 49 66 20 74 68 65 72 65 20 state. If there
118fe 69 73 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a 6f is.** an open jo
118ff 75 72 6e 61 6c 2d 66 69 6c 65 2c 20 74 68 65 6e urnal-file, then
11900 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 61 the next time a
11901 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 shared-lock is
11902 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 obtained.** on t
11903 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 28 62 he pager file (b
11904 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 y this or any ot
11905 68 65 72 20 70 72 6f 63 65 73 73 29 2c 20 69 74 her process), it
11906 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 65 61 will be.** trea
11907 74 65 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 ted as a hot-jou
11908 72 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20 rnal and rolled
11909 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 back..*/.static
1190a 76 6f 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 void pager_unloc
1190b 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 k(Pager *pPager)
1190c 7b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d {. if( !pPager-
1190d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 >exclusiveMode )
1190e 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 0a 20 {. int rc;..
1190f 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 63 6c 6f /* Always clo
11910 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 se the journal f
11911 69 6c 65 20 77 68 65 6e 20 64 72 6f 70 70 69 6e ile when droppin
11912 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c g the database l
11913 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 ock.. ** Othe
11914 72 77 69 73 65 2c 20 61 6e 6f 74 68 65 72 20 63 rwise, another c
11915 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 6a onnection with j
11916 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 ournal_mode=dele
11917 74 65 20 6d 69 67 68 74 0a 20 20 20 20 2a 2a 20 te might. **
11918 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20 delete the file
11919 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75 out from under u
1191a 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 s.. */. if
1191b 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
1191c 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 20 20 73 lOpen ){. s
1191d 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
1191e 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 ager->jfd);.
1191f 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11920 6c 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 lOpen = 0;.
11921 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
11922 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 stroy(pPager->pI
11923 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 nJournal);.
11924 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
11925 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 nal = 0;. s
11926 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 qlite3BitvecDest
11927 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 41 6c 77 roy(pPager->pAlw
11928 61 79 73 52 6f 6c 6c 62 61 63 6b 29 3b 0a 20 20 aysRollback);.
11929 20 20 20 20 70 50 61 67 65 72 2d 3e 70 41 6c 77 pPager->pAlw
1192a 61 79 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 30 3b aysRollback = 0;
1192b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d . }.. rc =
1192c 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 osUnlock(pPager
1192d 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a ->fd, NO_LOCK);.
1192e 20 20 20 20 69 66 28 20 72 63 20 29 20 70 50 61 if( rc ) pPa
1192f 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 ger->errCode = r
11930 63 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 c;. pPager->d
11931 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a bSizeValid = 0;.
11932 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 55 4e IOTRACE(("UN
11933 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 50 61 67 LOCK %p\n", pPag
11934 65 72 29 29 0a 0a 20 20 20 20 2f 2a 20 49 66 20 er)).. /* If
11935 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69 73 Pager.errCode is
11936 20 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e set, the conten
11937 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 ts of the pager
11938 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 0a cache cannot be.
11939 20 20 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 ** trusted.
1193a 4e 6f 77 20 74 68 61 74 20 74 68 65 20 70 61 67 Now that the pag
1193b 65 72 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 er file is unloc
1193c 6b 65 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 ked, the content
1193d 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 s of the. **
1193e 63 61 63 68 65 20 63 61 6e 20 62 65 20 64 69 73 cache can be dis
1193f 63 61 72 64 65 64 20 61 6e 64 20 74 68 65 20 65 carded and the e
11940 72 72 6f 72 20 63 6f 64 65 20 73 61 66 65 6c 79 rror code safely
11941 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f cleared.. */
11942 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
11943 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 >errCode ){.
11944 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
11945 5f 4f 4b 20 29 20 70 50 61 67 65 72 2d 3e 65 72 _OK ) pPager->er
11946 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f rCode = SQLITE_O
11947 4b 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 72 K;. pager_r
11948 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 eset(pPager);.
11949 20 20 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 releaseAllSa
1194a 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 29 3b vepoint(pPager);
1194b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a . pPager->j
1194c 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 ournalOff = 0;.
1194d 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
1194e 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b rnalStarted = 0;
1194f 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 . pPager->d
11950 62 4f 72 69 67 53 69 7a 65 20 3d 20 30 3b 0a 20 bOrigSize = 0;.
11951 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 72 }.. pPager
11952 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
11953 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 70 50 61 67 UNLOCK;. pPag
11954 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 er->changeCountD
11955 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a one = 0;. }.}..
11956 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 20 /*.** Execute a
11957 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74 72 rollback if a tr
11958 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
11959 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 ive and unlock t
1195a 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 he .** database
1195b 66 69 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 file. If the pag
1195c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 65 er has already e
1195d 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 ntered the error
1195e 20 73 74 61 74 65 2c 20 0a 2a 2a 20 64 6f 20 6e state, .** do n
1195f 6f 74 20 61 74 74 65 6d 70 74 20 74 68 65 20 72 ot attempt the r
11960 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 ollback..*/.stat
11961 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c ic void pagerUnl
11962 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 50 ockAndRollback(P
11963 61 67 65 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 ager *p){. if(
11964 70 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 p->errCode==SQLI
11965 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 73 74 61 74 TE_OK && p->stat
11966 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 e>=PAGER_RESERVE
11967 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 D ){. sqlite3
11968 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f BeginBenignMallo
11969 63 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 c();. sqlite3
1196a 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 29 PagerRollback(p)
1196b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 ;. sqlite3End
1196c 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
1196d 20 20 7d 0a 20 20 70 61 67 65 72 5f 75 6e 6c 6f }. pager_unlo
1196e 63 6b 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ck(p);.}../*.**
1196f 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 64 This routine end
11970 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e s a transaction.
11971 20 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 A transaction
11972 69 73 20 65 6e 64 65 64 20 62 79 20 65 69 74 68 is ended by eith
11973 65 72 0a 2a 2a 20 61 20 43 4f 4d 4d 49 54 20 6f er.** a COMMIT o
11974 72 20 61 20 52 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a r a ROLLBACK..**
11975 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f .** When this ro
11976 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
11977 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 74 the pager has t
11978 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11979 6f 70 65 6e 20 61 6e 64 0a 2a 2a 20 61 20 52 45 open and.** a RE
1197a 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 SERVED or EXCLUS
1197b 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 IVE lock on the
1197c 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 database. This
1197d 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 6c routine will rel
1197e 65 61 73 65 0a 2a 2a 20 74 68 65 20 64 61 74 61 ease.** the data
1197f 62 61 73 65 20 6c 6f 63 6b 20 61 6e 64 20 61 63 base lock and ac
11980 71 75 69 72 65 73 20 61 20 53 48 41 52 45 44 20 quires a SHARED
11981 6c 6f 63 6b 20 69 6e 20 69 74 73 20 70 6c 61 63 lock in its plac
11982 65 20 69 66 20 74 68 61 74 20 69 73 0a 2a 2a 20 e if that is.**
11983 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
11984 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 52 65 thing to do. Re
11985 6c 65 61 73 65 20 6c 6f 63 6b 73 20 75 73 75 61 lease locks usua
11986 6c 6c 79 20 69 73 20 61 70 70 72 6f 70 72 69 61 lly is appropria
11987 74 65 2c 0a 2a 2a 20 75 6e 6c 65 73 73 20 77 65 te,.** unless we
11988 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69 76 are in exclusiv
11989 65 20 61 63 63 65 73 73 20 6d 6f 64 65 20 6f 72 e access mode or
1198a 20 75 6e 6c 65 73 73 20 74 68 69 73 20 69 73 20 unless this is
1198b 61 20 0a 2a 2a 20 43 4f 4d 4d 49 54 20 41 4e 44 a .** COMMIT AND
1198c 20 42 45 47 49 4e 20 6f 72 20 52 4f 4c 4c 42 41 BEGIN or ROLLBA
1198d 43 4b 20 41 4e 44 20 42 45 47 49 4e 20 6f 70 65 CK AND BEGIN ope
1198e 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ration..**.** Th
1198f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
11990 73 20 65 69 74 68 65 72 20 64 65 6c 65 74 65 64 s either deleted
11991 20 6f 72 20 74 72 75 6e 63 61 74 65 64 2e 0a 2a or truncated..*
11992 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 43 6f 6e 73 69 *.** TODO: Consi
11993 64 65 72 20 6b 65 65 70 69 6e 67 20 74 68 65 20 der keeping the
11994 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 70 65 journal file ope
11995 6e 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 n for temporary
11996 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 20 54 68 databases..** Th
11997 69 73 20 6d 69 67 68 74 20 67 69 76 65 20 61 20 is might give a
11998 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70 72 performance impr
11999 6f 76 65 6d 65 6e 74 20 6f 6e 20 77 69 6e 64 6f ovement on windo
1199a 77 73 20 77 68 65 72 65 20 6f 70 65 6e 69 6e 67 ws where opening
1199b 0a 2a 2a 20 61 20 66 69 6c 65 20 69 73 20 61 6e .** a file is an
1199c 20 65 78 70 65 6e 73 69 76 65 20 6f 70 65 72 61 expensive opera
1199d 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
1199e 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 int pager_end_tr
1199f 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 65 72 20 ansaction(Pager
119a0 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 68 61 73 *pPager, int has
119a1 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 Master){. int r
119a2 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
119a3 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 54 int rc2 = SQLIT
119a4 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67 E_OK;. if( pPag
119a5 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f er->state<PAGER_
119a6 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 RESERVED ){.
119a7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
119a8 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 41 ;. }. releaseA
119a9 6c 6c 53 61 76 65 70 6f 69 6e 74 28 70 50 61 67 llSavepoint(pPag
119aa 65 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 er);. if( pPage
119ab 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 r->journalOpen )
119ac 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 {. if( pPager
119ad 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
119ae 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
119af 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 _MEMORY ){.
119b0 20 69 6e 74 20 69 73 4d 65 6d 6f 72 79 4a 6f 75 int isMemoryJou
119b1 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 49 73 rnal = sqlite3Is
119b2 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 MemJournal(pPage
119b3 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 73 r->jfd);. s
119b4 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
119b5 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 ager->jfd);.
119b6 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
119b7 6c 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 lOpen = 0;.
119b8 20 69 66 28 20 21 69 73 4d 65 6d 6f 72 79 4a 6f if( !isMemoryJo
119b9 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 urnal ){.
119ba 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 rc = sqlite3OsD
119bb 65 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 70 56 elete(pPager->pV
119bc 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 fs, pPager->zJou
119bd 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 rnal, 0);.
119be 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
119bf 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
119c0 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
119c1 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a ALMODE_TRUNCATE.
119c2 20 20 20 20 20 20 20 20 20 26 26 20 28 72 63 20 && (rc
119c3 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
119c4 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ate(pPager->jfd,
119c5 20 30 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 0))==SQLITE_OK
119c6 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d ){. pPager-
119c7 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b >journalOff = 0;
119c8 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a . pPager->j
119c9 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 ournalStarted =
119ca 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0;. }else if(
119cb 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
119cc 76 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c 7c 20 veMode . ||
119cd 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
119ce 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
119cf 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 ALMODE_PERSIST.
119d0 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ){. rc =
119d1 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 zeroJournalHdr(
119d2 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74 65 pPager, hasMaste
119d3 72 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f r);. pager_
119d4 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 error(pPager, rc
119d5 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d );. pPager-
119d6 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b >journalOff = 0;
119d7 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a . pPager->j
119d8 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 ournalStarted =
119d9 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
119da 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
119db 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
119dc 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
119dd 44 45 5f 44 45 4c 45 54 45 20 7c 7c 20 72 63 20 DE_DELETE || rc
119de 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
119df 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
119e0 6a 66 64 29 3b 0a 20 20 20 20 20 20 70 50 61 67 jfd);. pPag
119e1 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 er->journalOpen
119e2 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 72 = 0;. if( r
119e3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
119e4 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
119e5 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 e ){. rc
119e6 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 = sqlite3OsDelet
119e7 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 e(pPager->pVfs,
119e8 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
119e9 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
119ea 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 }. sqlite3B
119eb 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 itvecDestroy(pPa
119ec 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 ger->pInJournal)
119ed 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 ;. pPager->pI
119ee 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 nJournal = 0;.
119ef 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
119f0 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 estroy(pPager->p
119f1 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 29 3b AlwaysRollback);
119f2 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 41 6c . pPager->pAl
119f3 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 30 waysRollback = 0
119f4 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
119f5 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 CHECK_PAGES.
119f6 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 sqlite3PcacheIte
119f7 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72 rateDirty(pPager
119f8 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 72 ->pPCache, pager
119f9 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b 0a _set_pagehash);.
119fa 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 #endif. sqlit
119fb 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c e3PcacheCleanAll
119fc 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
119fd 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 );. pPager->d
119fe 69 72 74 79 43 61 63 68 65 20 3d 20 30 3b 0a 20 irtyCache = 0;.
119ff 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 pPager->nRec
11a00 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
11a01 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
11a02 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 ->pInJournal==0
11a03 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 );. }.. if( !p
11a04 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
11a05 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32 20 Mode ){. rc2
11a06 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 = osUnlock(pPage
11a07 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f r->fd, SHARED_LO
11a08 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d CK);. pPager-
11a09 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 >state = PAGER_S
11a0a 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67 65 HARED;. pPage
11a0b 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
11a0c 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 ne = 0;. }else
11a0d 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 if( pPager->stat
11a0e 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 e==PAGER_SYNCED
11a0f 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 ){. pPager->s
11a10 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 tate = PAGER_EXC
11a11 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70 50 LUSIVE;. }. pP
11a12 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
11a13 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e = 0;. pPager->
11a14 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 setMaster = 0;.
11a15 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
11a16 63 20 3d 20 30 3b 0a 20 20 2f 2a 20 6c 72 75 4c c = 0;. /* lruL
11a17 69 73 74 53 65 74 46 69 72 73 74 53 79 6e 63 65 istSetFirstSynce
11a18 64 28 70 50 61 67 65 72 29 3b 20 2a 2f 0a 20 20 d(pPager); */.
11a19 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 sqlite3PcacheTru
11a1a 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50 ncate(pPager->pP
11a1b 43 61 63 68 65 2c 20 70 50 61 67 65 72 2d 3e 64 Cache, pPager->d
11a1c 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 21 4d bSize);. if( !M
11a1d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 61 67 EMDB ){. pPag
11a1e 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
11a1f 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 = 0;. }. pPage
11a20 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 r->dbModified =
11a21 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 28 72 63 0;.. return (rc
11a22 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a ==SQLITE_OK?rc2:
11a23 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f rc);.}../*.** Co
11a24 6d 70 75 74 65 20 61 6e 64 20 72 65 74 75 72 6e mpute and return
11a25 20 61 20 63 68 65 63 6b 73 75 6d 20 66 6f 72 20 a checksum for
11a26 74 68 65 20 70 61 67 65 20 6f 66 20 64 61 74 61 the page of data
11a27 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ..**.** This is
11a28 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b not a real check
11a29 73 75 6d 2e 20 20 49 74 20 69 73 20 72 65 61 6c sum. It is real
11a2a 6c 79 20 6a 75 73 74 20 74 68 65 20 73 75 6d 20 ly just the sum
11a2b 6f 66 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f of the .** rando
11a2c 6d 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 m initial value
11a2d 61 6e 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d and the page num
11a2e 62 65 72 2e 20 20 57 65 20 65 78 70 65 72 69 6d ber. We experim
11a2f 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 ented with.** a
11a30 63 68 65 63 6b 73 75 6d 20 6f 66 20 74 68 65 20 checksum of the
11a31 65 6e 74 69 72 65 20 64 61 74 61 2c 20 62 75 74 entire data, but
11a32 20 74 68 61 74 20 77 61 73 20 66 6f 75 6e 64 20 that was found
11a33 74 6f 20 62 65 20 74 6f 6f 20 73 6c 6f 77 2e 0a to be too slow..
11a34 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
11a35 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
11a36 69 73 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 is stored at the
11a37 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 64 61 beginning of da
11a38 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 68 ta and.** the ch
11a39 65 63 6b 73 75 6d 20 69 73 20 73 74 6f 72 65 64 ecksum is stored
11a3a 20 61 74 20 74 68 65 20 65 6e 64 2e 20 20 54 68 at the end. Th
11a3b 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e is is important.
11a3c 20 20 49 66 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 If journal.**
11a3d 63 6f 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 corruption occur
11a3e 73 20 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72 s due to a power
11a3f 20 66 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f failure, the mo
11a40 73 74 20 6c 69 6b 65 6c 79 20 73 63 65 6e 61 72 st likely scenar
11a41 69 6f 0a 2a 2a 20 69 73 20 74 68 61 74 20 6f 6e io.** is that on
11a42 65 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 e end or the oth
11a43 65 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 er of the record
11a44 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 will be changed
11a45 2e 20 20 49 74 20 69 73 0a 2a 2a 20 6d 75 63 68 . It is.** much
11a46 20 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 less likely tha
11a47 74 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20 6f t the two ends o
11a48 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 f the journal re
11a49 63 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 cord will be.**
11a4a 63 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65 20 correct and the
11a4b 6d 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75 70 middle be corrup
11a4c 74 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20 22 t. Thus, this "
11a4d 63 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 checksum" scheme
11a4e 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 74 ,.** though fast
11a4f 20 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 and simple, cat
11a50 63 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 ches the mostly
11a51 6c 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 likely kind of c
11a52 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a orruption..**.**
11a53 20 46 49 58 20 4d 45 3a 20 20 43 6f 6e 73 69 64 FIX ME: Consid
11a54 65 72 20 61 64 64 69 6e 67 20 65 76 65 72 79 20 er adding every
11a55 32 30 30 74 68 20 28 6f 72 20 73 6f 29 20 62 79 200th (or so) by
11a56 74 65 20 6f 66 20 74 68 65 20 64 61 74 61 20 74 te of the data t
11a57 6f 20 74 68 65 0a 2a 2a 20 63 68 65 63 6b 73 75 o the.** checksu
11a58 6d 2e 20 20 54 68 61 74 20 77 61 79 20 69 66 20 m. That way if
11a59 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 73 70 a single page sp
11a5a 61 6e 73 20 33 20 6f 72 20 6d 6f 72 65 20 64 69 ans 3 or more di
11a5b 73 6b 20 73 65 63 74 6f 72 73 20 61 6e 64 0a 2a sk sectors and.*
11a5c 2a 20 6f 6e 6c 79 20 74 68 65 20 6d 69 64 64 6c * only the middl
11a5d 65 20 73 65 63 74 6f 72 20 69 73 20 63 6f 72 72 e sector is corr
11a5e 75 70 74 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 upt, we will sti
11a5f 6c 6c 20 68 61 76 65 20 61 20 72 65 61 73 6f 6e ll have a reason
11a60 61 62 6c 65 0a 2a 2a 20 63 68 61 6e 63 65 20 6f able.** chance o
11a61 66 20 66 61 69 6c 69 6e 67 20 74 68 65 20 63 68 f failing the ch
11a62 65 63 6b 73 75 6d 20 61 6e 64 20 74 68 75 73 20 ecksum and thus
11a63 64 65 74 65 63 74 69 6e 67 20 74 68 65 20 70 72 detecting the pr
11a64 6f 62 6c 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 oblem..*/.static
11a65 20 75 33 32 20 70 61 67 65 72 5f 63 6b 73 75 6d u32 pager_cksum
11a66 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
11a67 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61 29 const u8 *aData)
11a68 7b 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 {. u32 cksum =
11a69 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
11a6a 74 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61 t;. int i = pPa
11a6b 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 ger->pageSize-20
11a6c 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 0;. while( i>0
11a6d 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 ){. cksum +=
11a6e 61 44 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 aData[i];. i
11a6f 2d 3d 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 -= 200;. }. re
11a70 74 75 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f turn cksum;.}../
11a71 2a 0a 2a 2a 20 52 65 61 64 20 61 20 73 69 6e 67 *.** Read a sing
11a72 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 65 69 74 le page from eit
11a73 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 her the journal
11a74 66 69 6c 65 20 28 69 66 20 69 73 4d 61 69 6e 4a file (if isMainJ
11a75 72 6e 6c 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 rnl==1) or.** fr
11a76 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e om the sub-journ
11a77 61 6c 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e al (if isMainJrn
11a78 6c 3d 3d 30 29 20 61 6e 64 20 70 6c 61 79 62 61 l==0) and playba
11a79 63 6b 20 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a ck that page..**
11a7a 20 54 68 65 20 70 61 67 65 20 62 65 67 69 6e 73 The page begins
11a7b 20 61 74 20 6f 66 66 73 65 74 20 2a 70 4f 66 66 at offset *pOff
11a7c 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c set into the fil
11a7d 65 2e 20 20 54 68 65 20 20 2a 70 4f 66 66 73 65 e. The *pOffse
11a7e 74 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 69 6e t.** value is in
11a7f 63 72 65 61 73 65 64 20 74 6f 20 74 68 65 20 73 creased to the s
11a80 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 tart of the next
11a81 20 70 61 67 65 20 69 6e 20 74 68 65 20 6a 6f 75 page in the jou
11a82 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rnal..**.** The
11a83 69 73 4d 61 69 6e 4a 72 6e 6c 20 66 6c 61 67 20 isMainJrnl flag
11a84 69 73 20 74 72 75 65 20 69 66 20 74 68 69 73 20 is true if this
11a85 69 73 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c is the main roll
11a86 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 back journal and
11a87 0a 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 74 68 .** false for th
11a88 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 e statement jour
11a89 6e 61 6c 2e 20 20 54 68 65 20 6d 61 69 6e 20 72 nal. The main r
11a8a 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
11a8b 75 73 65 73 0a 2a 2a 20 63 68 65 63 6b 73 75 6d uses.** checksum
11a8c 73 20 2d 20 74 68 65 20 73 74 61 74 65 6d 65 6e s - the statemen
11a8d 74 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e t journal does n
11a8e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f ot..**.** If pDo
11a8f 6e 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 ne is not NULL,
11a90 74 68 65 6e 20 69 74 20 69 73 20 61 20 72 65 63 then it is a rec
11a91 6f 72 64 20 6f 66 20 70 61 67 65 73 20 74 68 61 ord of pages tha
11a92 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a t have already.*
11a93 2a 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61 * been played ba
11a94 63 6b 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 ck. If the page
11a95 20 61 74 20 2a 70 4f 66 66 73 65 74 20 68 61 73 at *pOffset has
11a96 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c already been pl
11a97 61 79 65 64 20 62 61 63 6b 0a 2a 2a 20 28 69 66 ayed back.** (if
11a98 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
11a99 6e 67 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20 ng pDone bit is
11a9a 73 65 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74 set) then skip t
11a9b 68 65 20 70 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20 he playback..**
11a9c 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 44 Make sure the pD
11a9d 6f 6e 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f one bit correspo
11a9e 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f nding to the *pO
11a9f 66 66 73 65 74 20 70 61 67 65 20 69 73 20 73 65 ffset page is se
11aa0 74 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 t.** prior to re
11aa1 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 turning..*/.stat
11aa2 69 63 20 69 6e 74 20 70 61 67 65 72 5f 70 6c 61 ic int pager_pla
11aa3 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a yback_one_page(.
11aa4 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c Pager *pPager,
11aa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11aa6 2f 2a 20 54 68 65 20 70 61 67 65 72 20 62 65 69 /* The pager bei
11aa7 6e 67 20 70 6c 61 79 65 64 20 62 61 63 6b 20 2a ng played back *
11aa8 2f 0a 20 20 69 6e 74 20 69 73 4d 61 69 6e 4a 72 /. int isMainJr
11aa9 6e 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 nl,
11aaa 20 20 2f 2a 20 31 20 2d 3e 20 6d 61 69 6e 20 6a /* 1 -> main j
11aab 6f 75 72 6e 61 6c 2e 20 30 20 2d 3e 20 73 75 62 ournal. 0 -> sub
11aac 2d 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 69 -journal. */. i
11aad 36 34 20 2a 70 4f 66 66 73 65 74 2c 20 20 20 20 64 *pOffset,
11aae 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11aaf 4f 66 66 73 65 74 20 6f 66 20 72 65 63 6f 72 64 Offset of record
11ab0 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 2a 2f 0a to playback */.
11ab1 20 20 69 6e 74 20 69 73 53 61 76 65 70 6e 74 2c int isSavepnt,
11ab2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11ab3 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 73 61 /* True for a sa
11ab4 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b vepoint rollback
11ab5 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 44 */. Bitvec *pD
11ab6 6f 6e 65 20 20 20 20 20 20 20 20 20 20 20 20 20 one
11ab7 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20 6f 66 /* Bitvec of
11ab8 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 70 pages already p
11ab9 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 29 7b layed back */.){
11aba 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 . int rc;. PgH
11abb 64 72 20 2a 70 50 67 3b 20 20 20 20 20 20 20 20 dr *pPg;
11abc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e /* An
11abd 20 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 69 existing page i
11abe 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 n the cache */.
11abf 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 Pgno pgno;
11ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11ac1 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 * The page numbe
11ac2 72 20 6f 66 20 61 20 70 61 67 65 20 69 6e 20 6a r of a page in j
11ac3 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 ournal */. u32
11ac4 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 cksum;
11ac5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 /* Che
11ac6 63 6b 73 75 6d 20 75 73 65 64 20 66 6f 72 20 73 cksum used for s
11ac7 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a anity checking *
11ac8 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20 /. u8 *aData;
11ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11aca 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 /* Temporary s
11acb 74 6f 72 61 67 65 20 66 6f 72 20 74 68 65 20 70 torage for the p
11acc 61 67 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 age */. sqlite3
11acd 5f 66 69 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 _file *jfd;
11ace 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 /* The fi
11acf 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f le descriptor fo
11ad0 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 r the journal fi
11ad1 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 le */.. assert(
11ad2 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29 (isMainJrnl&~1)
11ad3 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f 2a 20 69 ==0 ); /* i
11ad4 73 4d 61 69 6e 4a 72 6e 6c 20 69 73 20 30 20 6f sMainJrnl is 0 o
11ad5 72 20 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 r 1 */. assert(
11ad6 20 28 69 73 53 61 76 65 70 6e 74 26 7e 31 29 3d (isSavepnt&~1)=
11ad7 3d 30 20 29 3b 20 20 20 20 20 20 20 2f 2a 20 69 =0 ); /* i
11ad8 73 53 61 76 65 70 6e 74 20 69 73 20 30 20 6f 72 sSavepnt is 0 or
11ad9 20 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 1 */. assert(
11ada 69 73 4d 61 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44 isMainJrnl || pD
11adb 6f 6e 65 20 29 3b 20 20 20 20 20 2f 2a 20 70 44 one ); /* pD
11adc 6f 6e 65 20 61 6c 77 61 79 73 20 75 73 65 64 20 one always used
11add 6f 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 on sub-journals
11ade 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 53 */. assert( isS
11adf 61 76 65 70 6e 74 20 7c 7c 20 70 44 6f 6e 65 3d avepnt || pDone=
11ae0 3d 30 20 29 3b 20 20 20 2f 2a 20 70 44 6f 6e 65 =0 ); /* pDone
11ae1 20 6e 65 76 65 72 20 75 73 65 64 20 6f 6e 20 6e never used on n
11ae2 6f 6e 2d 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a on-savepoint */.
11ae3 0a 20 20 61 44 61 74 61 20 3d 20 28 75 38 2a 29 . aData = (u8*)
11ae4 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
11ae5 65 3b 0a 20 20 61 73 73 65 72 74 28 20 61 44 61 e;. assert( aDa
11ae6 74 61 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a ta ); /*
11ae7 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 6d 75 Temp storage mu
11ae8 73 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 st have already
11ae9 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a been allocated *
11aea 2f 0a 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 /.. jfd = isMai
11aeb 6e 4a 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e nJrnl ? pPager->
11aec 6a 66 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a jfd : pPager->sj
11aed 66 64 3b 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 fd;.. rc = read
11aee 33 32 62 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66 32bits(jfd, *pOf
11aef 66 73 65 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 fset, &pgno);.
11af0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
11af1 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
11af2 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
11af3 65 61 64 28 6a 66 64 2c 20 61 44 61 74 61 2c 20 ead(jfd, aData,
11af4 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
11af5 2c 20 28 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b , (*pOffset)+4);
11af6 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
11af7 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 E_OK ) return rc
11af8 3b 0a 20 20 2a 70 4f 66 66 73 65 74 20 2b 3d 20 ;. *pOffset +=
11af9 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
11afa 20 2b 20 34 20 2b 20 69 73 4d 61 69 6e 4a 72 6e + 4 + isMainJrn
11afb 6c 2a 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 l*4;.. /* Sanit
11afc 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 y checking on th
11afd 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 69 73 e page. This is
11afe 20 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 more important
11aff 74 68 61 74 20 49 20 6f 72 69 67 69 6e 61 6c 6c that I originall
11b00 79 0a 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e 20 y. ** thought.
11b01 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c If a power fail
11b02 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ure occurs while
11b03 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
11b04 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a 20 being written,.
11b05 20 2a 2a 20 69 74 20 63 6f 75 6c 64 20 63 61 75 ** it could cau
11b06 73 65 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 se invalid data
11b07 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e to be written in
11b08 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 to the journal.
11b09 20 57 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a 2a We need to. **
11b0a 20 64 65 74 65 63 74 20 74 68 69 73 20 69 6e 76 detect this inv
11b0b 61 6c 69 64 20 64 61 74 61 20 28 77 69 74 68 20 alid data (with
11b0c 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 high probability
11b0d 29 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e ) and ignore it.
11b0e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f . */. if( pgno
11b0f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 ==0 || pgno==PAG
11b10 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 ER_MJ_PGNO(pPage
11b11 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e r) ){. return
11b12 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
11b13 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 50 67 }. if( pgno>(Pg
11b14 6e 6f 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a no)pPager->dbSiz
11b15 65 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76 e || sqlite3Bitv
11b16 65 63 54 65 73 74 28 70 44 6f 6e 65 2c 20 70 67 ecTest(pDone, pg
11b17 6e 6f 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 no) ){. retur
11b18 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
11b19 0a 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e . if( isMainJrn
11b1a 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 65 l ){. rc = re
11b1b 61 64 33 32 62 69 74 73 28 6a 66 64 2c 20 28 2a ad32bits(jfd, (*
11b1c 70 4f 66 66 73 65 74 29 2d 34 2c 20 26 63 6b 73 pOffset)-4, &cks
11b1d 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 um);. if( rc
11b1e 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
11b1f 20 69 66 28 20 21 69 73 53 61 76 65 70 6e 74 20 if( !isSavepnt
11b20 26 26 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 && pager_cksum(p
11b21 50 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d 63 Pager, aData)!=c
11b22 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 ksum ){. re
11b23 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
11b24 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
11b25 28 20 70 44 6f 6e 65 20 26 26 20 28 72 63 20 3d ( pDone && (rc =
11b26 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 sqlite3BitvecSe
11b27 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 29 20 t(pDone, pgno))
11b28 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
11b29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 ;. }.. assert(
11b2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
11b2b 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 7c PAGER_RESERVED |
11b2c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e | pPager->state>
11b2d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 =PAGER_EXCLUSIVE
11b2e 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
11b2f 20 70 61 67 65 72 20 69 73 20 69 6e 20 52 45 53 pager is in RES
11b30 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 ERVED state, the
11b31 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 n there must be
11b32 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73 0a 20 a copy of this.
11b33 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 ** page in the
11b34 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 6e 20 pager cache. In
11b35 74 68 69 73 20 63 61 73 65 20 6a 75 73 74 20 75 this case just u
11b36 70 64 61 74 65 20 74 68 65 20 70 61 67 65 72 20 pdate the pager
11b37 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20 cache,. ** not
11b38 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
11b39 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 6c e. The page is l
11b3a 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72 74 79 eft marked dirty
11b3b 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 in this case..
11b3c 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 63 65 **. ** An exce
11b3d 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 62 6f ption to the abo
11b3e 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68 65 20 ve rule: If the
11b3f 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 6e database is in n
11b40 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a o-sync mode. **
11b41 20 61 6e 64 20 61 20 70 61 67 65 20 69 73 20 6d and a page is m
11b42 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e 20 69 oved during an i
11b43 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 ncremental vacuu
11b44 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 m then the page
11b45 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 may. ** not be
11b46 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 in the pager cac
11b47 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 61 20 he. Later: if a
11b48 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 65 malloc() or IO e
11b49 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 2a 2a rror occurs. **
11b4a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 70 61 during a Movepa
11b4b 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 6e 20 ge() call, then
11b4c 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e 6f 74 the page may not
11b4d 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 be in the cache
11b4e 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 53 6f . ** either. So
11b4f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 the condition d
11b50 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 20 escribed in the
11b51 61 62 6f 76 65 20 70 61 72 61 67 72 61 70 68 20 above paragraph
11b52 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 73 65 is not. ** asse
11b53 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 rt()able.. **.
11b54 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c 55 53 ** If in EXCLUS
11b55 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e 20 IVE state, then
11b56 77 65 20 75 70 64 61 74 65 20 74 68 65 20 70 61 we update the pa
11b57 67 65 72 20 63 61 63 68 65 20 69 66 20 69 74 20 ger cache if it
11b58 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e 64 20 exists. ** and
11b59 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e 20 54 the main file. T
11b5a 68 65 20 70 61 67 65 20 69 73 20 74 68 65 6e 20 he page is then
11b5b 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 74 79 marked not dirty
11b5c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b .. **. ** Tick
11b5d 65 74 20 23 31 31 37 31 3a 20 20 54 68 65 20 73 et #1171: The s
11b5e 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c tatement journal
11b5f 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 70 might contain p
11b60 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 61 74 age content that
11b61 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 is. ** differe
11b62 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 nt from the page
11b63 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 65 20 content at the
11b64 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 start of the tra
11b65 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 nsaction.. ** T
11b66 68 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20 his occurs when
11b67 61 20 70 61 67 65 20 69 73 20 63 68 61 6e 67 65 a page is change
11b68 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 d prior to the s
11b69 74 61 72 74 20 6f 66 20 61 20 73 74 61 74 65 6d tart of a statem
11b6a 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 63 68 ent. ** then ch
11b6b 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69 74 68 anged again with
11b6c 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 in the statement
11b6d 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 . When rolling
11b6e 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20 2a 2a back such a. **
11b6f 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20 6d 75 statement we mu
11b70 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 st not write to
11b71 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 the original dat
11b72 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77 65 20 abase unless we
11b73 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 63 65 know. ** for ce
11b74 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69 67 69 rtain that origi
11b75 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 nal page content
11b76 73 20 61 72 65 20 73 79 6e 63 65 64 20 69 6e 74 s are synced int
11b77 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 o the main rollb
11b78 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c ack. ** journal
11b79 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 . Otherwise, a
11b7a 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 68 74 power loss might
11b7b 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 64 20 leave modified
11b7c 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20 2a 2a data in the. **
11b7d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
11b7e 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 79 20 ithout an entry
11b7f 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 in the rollback
11b80 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 61 6e journal that can
11b81 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 74 68 . ** restore th
11b82 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 e database to it
11b83 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e s original form.
11b84 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 Two conditions
11b85 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6d 65 must be. ** me
11b86 74 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 t before writing
11b87 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
11b88 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68 65 20 files. (1) the
11b89 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 database must be
11b8a 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 . ** locked. (
11b8b 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 2) we know that
11b8c 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 the original pag
11b8d 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c e content is ful
11b8e 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a 20 69 ly synced. ** i
11b8f 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e n the main journ
11b90 61 6c 20 65 69 74 68 65 72 20 62 65 63 61 75 73 al either becaus
11b91 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f e the page is no
11b92 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65 6c t in cache or el
11b93 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 se. ** the page
11b94 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e 65 is marked as ne
11b95 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a edSync==0.. **.
11b96 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 34 3a ** 2008-04-14:
11b97 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74 69 6e When attemptin
11b98 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20 63 6f g to vacuum a co
11b99 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 rrupt database f
11b9a 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20 ile, it. ** is
11b9b 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 69 6c possible to fail
11b9c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 a statement on
11b9d 61 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 a database that
11b9e 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 78 69 does not yet exi
11b9f 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 st.. ** Do not
11ba0 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 attempt to write
11ba1 20 69 66 20 64 61 74 61 62 61 73 65 20 66 69 6c if database fil
11ba2 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e e has never been
11ba3 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 opened.. */.
11ba4 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b pPg = pager_look
11ba5 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 up(pPager, pgno)
11ba6 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 ;. PAGERTRACE((
11ba7 22 50 4c 41 59 42 41 43 4b 20 25 64 20 70 61 67 "PLAYBACK %d pag
11ba8 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 20 e %d hash(%08x)
11ba9 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 %s\n",.
11baa 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 PAGERID(pP
11bab 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 ager), pgno, pag
11bac 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67 er_datahash(pPag
11bad 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 61 44 er->pageSize, aD
11bae 61 74 61 29 2c 0a 20 20 20 20 20 20 20 20 20 20 ata),.
11baf 20 20 20 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c (isMainJrnl
11bb0 3f 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a ?"main-journal":
11bb1 22 73 75 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 "sub-journal").
11bb2 20 29 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 ));. if( (pPag
11bb3 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
11bb4 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 20 26 _EXCLUSIVE). &
11bb5 26 20 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d & (pPg==0 || 0==
11bb6 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 (pPg->flags&PGHD
11bb7 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 R_NEED_SYNC)).
11bb8 20 26 26 20 28 70 50 61 67 65 72 2d 3e 66 64 2d && (pPager->fd-
11bb9 3e 70 4d 65 74 68 6f 64 73 29 0a 20 20 29 7b 0a >pMethods). ){.
11bba 20 20 20 20 69 36 34 20 6f 66 73 74 20 3d 20 28 i64 ofst = (
11bbb 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 pgno-1)*(i64)pPa
11bbc 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 ger->pageSize;.
11bbd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
11bbe 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 sWrite(pPager->f
11bbf 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 d, aData, pPager
11bc0 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 73 74 ->pageSize, ofst
11bc1 29 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e );. if( pgno>
11bc2 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 pPager->dbFileSi
11bc3 7a 65 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 ze ){. pPag
11bc4 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d er->dbFileSize =
11bc5 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d pgno;. }. }
11bc6 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 6e else if( !isMain
11bc7 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 29 Jrnl && pPg==0 )
11bc8 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 {. /* If this
11bc9 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f is a rollback o
11bca 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 6e f a savepoint an
11bcb 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 77 d data was not w
11bcc 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a 2a ritten to. **
11bcd 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e the database an
11bce 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f d the page is no
11bcf 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 t in-memory, the
11bd0 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 61 re is a potentia
11bd1 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d l. ** problem
11bd2 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 20 . When the page
11bd3 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 20 is next fetched
11bd4 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 by the b-tree la
11bd5 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20 yer, it . **
11bd6 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 6f will be read fro
11bd7 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
11bd8 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 6f ile, which may o
11bd9 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 20 r may not be .
11bda 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a 20 ** current. .
11bdb 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
11bdc 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 20 re are a couple
11bdd 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 of different way
11bde 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 s this can happe
11bdf 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 65 n. All are quite
11be0 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 2e . ** obscure.
11be1 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e When running in
11be2 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 synchronous mod
11be3 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 e, this can only
11be4 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a 20 happen . **
11be5 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f if the page is o
11be6 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 n the free-list
11be7 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
11be8 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c the transaction,
11be9 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f 70 then. ** pop
11bea 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f 76 ulated, then mov
11beb 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 ed using sqlite3
11bec 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 2e PagerMovepage().
11bed 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
11bee 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 he solution is t
11bef 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f o add an in-memo
11bf0 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 ry page to the c
11bf1 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 0a ache containing.
11bf2 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 20 ** the data
11bf3 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 74 just read from t
11bf4 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 he sub-journal.
11bf5 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 Mark the page as
11bf6 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 61 dirty . ** a
11bf7 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20 nd if the pager
11bf8 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 6e requires a journ
11bf9 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d 61 al-sync, then ma
11bfa 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 0a rk the page as .
11bfb 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67 ** requiring
11bfc 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 a journal-sync
11bfd 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 69 before it is wri
11bfe 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tten.. */.
11bff 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 assert( isSavep
11c00 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28 72 nt );. if( (r
11c01 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
11c02 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20 Acquire(pPager,
11c03 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 20 pgno, &pPg, 1))
11c04 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
11c05 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 rc;. }. pP
11c06 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 g->flags &= ~PGH
11c07 44 52 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20 DR_NEED_READ;.
11c08 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
11c09 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 akeDirty(pPg);.
11c0a 20 7d 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a }. if( pPg ){.
11c0b 20 20 20 20 2f 2a 20 4e 6f 20 70 61 67 65 20 73 /* No page s
11c0c 68 6f 75 6c 64 20 65 76 65 72 20 62 65 20 65 78 hould ever be ex
11c0d 70 6c 69 63 69 74 6c 79 20 72 6f 6c 6c 65 64 20 plicitly rolled
11c0e 62 61 63 6b 20 74 68 61 74 20 69 73 20 69 6e 20 back that is in
11c0f 75 73 65 2c 20 65 78 63 65 70 74 0a 20 20 20 20 use, except.
11c10 2a 2a 20 66 6f 72 20 70 61 67 65 20 31 20 77 68 ** for page 1 wh
11c11 69 63 68 20 69 73 20 68 65 6c 64 20 69 6e 20 75 ich is held in u
11c12 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b se in order to k
11c13 65 65 70 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 eep the lock on
11c14 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 the. ** datab
11c15 61 73 65 20 61 63 74 69 76 65 2e 20 48 6f 77 65 ase active. Howe
11c16 76 65 72 20 73 75 63 68 20 61 20 70 61 67 65 20 ver such a page
11c17 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 may be rolled ba
11c18 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 0a 20 ck as a result.
11c19 20 20 20 2a 2a 20 6f 66 20 61 6e 20 69 6e 74 65 ** of an inte
11c1a 72 6e 61 6c 20 65 72 72 6f 72 20 72 65 73 75 6c rnal error resul
11c1b 74 69 6e 67 20 69 6e 20 61 6e 20 61 75 74 6f 6d ting in an autom
11c1c 61 74 69 63 20 63 61 6c 6c 20 74 6f 0a 20 20 20 atic call to.
11c1d 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 ** sqlite3Pager
11c1e 52 6f 6c 6c 62 61 63 6b 28 29 2e 0a 20 20 20 20 Rollback()..
11c1f 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 61 */. void *pDa
11c20 74 61 3b 0a 20 20 20 20 70 44 61 74 61 20 3d 20 ta;. pData =
11c21 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 pPg->pData;.
11c22 6d 65 6d 63 70 79 28 70 44 61 74 61 2c 20 61 44 memcpy(pData, aD
11c23 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 ata, pPager->pag
11c24 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 eSize);. if(
11c25 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 pPager->xReinite
11c26 72 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 r ){. pPage
11c27 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67 r->xReiniter(pPg
11c28 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
11c29 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20 28 isMainJrnl && (
11c2a 21 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 70 50 !isSavepnt || pP
11c2b 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11c2c 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 <=pPager->journa
11c2d 6c 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20 2f lHdr) ){. /
11c2e 2a 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 * If the content
11c2f 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 77 s of this page w
11c30 65 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72 65 ere just restore
11c31 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 d from the main
11c32 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 . ** journa
11c33 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 l file, then its
11c34 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 content must be
11c35 20 61 73 20 74 68 65 79 20 77 65 72 65 20 77 68 as they were wh
11c36 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a en the . **
11c37 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
11c38 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 first opened. I
11c39 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 63 n this case we c
11c3a 61 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 an mark the page
11c3b 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 . ** as cle
11c3c 61 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 an, since there
11c3d 77 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 will be no need
11c3e 74 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 to write it out
11c3f 74 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a to the.. **
11c40 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 . ** There
11c41 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e is one exception
11c42 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 49 to this rule. I
11c43 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62 65 f the page is be
11c44 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 ing rolled.
11c45 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 74 ** back as part
11c46 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 of a savepoint
11c47 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 72 (or statement) r
11c48 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 ollback from an
11c49 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 . ** unsync
11c4a 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 ed portion of th
11c4b 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 e main journal f
11c4c 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 ile, then it is
11c4d 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 2a not safe. *
11c4e 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 * to mark the pa
11c4f 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 ge as clean. Thi
11c50 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 72 s is because mar
11c51 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 73 king the page as
11c52 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 . ** clean
11c53 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 50 will clear the P
11c54 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 GHDR_NEED_SYNC f
11c55 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 70 lag. Since the p
11c56 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 age is. **
11c57 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a already in the j
11c58 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 ournal file (rec
11c59 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e 70 orded in Pager.p
11c5a 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 InJournal) and.
11c5b 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 ** the PGHD
11c5c 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
11c5d 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 20 is cleared, if
11c5e 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 the page is writ
11c5f 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 ten to. **
11c60 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69 again within thi
11c61 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 s transaction, i
11c62 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 t will be marked
11c63 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 20 as dirty but.
11c64 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 ** the PGHDR
11c65 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 _NEED_SYNC flag
11c66 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e will not be set.
11c67 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 It could then p
11c68 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 otentially.
11c69 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 6f ** be written o
11c6a 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 ut into the data
11c6b 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 base file before
11c6c 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c its journal fil
11c6d 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 e. ** segme
11c6e 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 66 nt is synced. If
11c6f 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 20 a crash occurs
11c70 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 during or follow
11c71 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 20 ing this,.
11c72 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 ** database corr
11c73 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 uption may ensue
11c74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
11c75 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 sqlite3PcacheMa
11c76 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 keClean(pPg);.
11c77 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }.#ifdef SQLIT
11c78 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
11c79 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 pPg->pageHash
11c7a 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 = pager_pagehash
11c7b 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 (pPg);.#endif.
11c7c 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 /* If this was
11c7d 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72 65 page 1, then re
11c7e 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 store the value
11c7f 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 of Pager.dbFileV
11c80 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 ers.. ** Do t
11c81 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20 64 his before any d
11c82 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 ecoding. */.
11c83 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 if( pgno==1 ){.
11c84 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 memcpy(&pPa
11c85 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c ger->dbFileVers,
11c86 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b 32 &((u8*)pData)[2
11c87 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65 72 4],sizeof(pPager
11c88 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a ->dbFileVers));.
11c89 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 }.. /* De
11c8a 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a 75 code the page ju
11c8b 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73 st read from dis
11c8c 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 k */. CODEC1(
11c8d 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 pPager, pData, p
11c8e 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a 20 20 Pg->pgno, 3);.
11c8f 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 sqlite3PcacheR
11c90 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d elease(pPg);. }
11c91 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
11c92 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
11c93 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 EBUG) || defined
11c94 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 (SQLITE_COVERAGE
11c95 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 _TEST)./*.** Thi
11c96 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 s routine looks
11c97 61 68 65 61 64 20 69 6e 74 6f 20 74 68 65 20 6d ahead into the m
11c98 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ain journal file
11c99 20 61 6e 64 20 64 65 74 65 72 6d 69 6e 65 73 0a and determines.
11c9a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ** whether or no
11c9b 74 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 t the next recor
11c9c 64 20 28 74 68 65 20 72 65 63 6f 72 64 20 74 68 d (the record th
11c9d 61 74 20 62 65 67 69 6e 73 20 61 74 20 66 69 6c at begins at fil
11c9e 65 0a 2a 2a 20 6f 66 66 73 65 74 20 70 50 61 67 e.** offset pPag
11c9f 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 er->journalOff)
11ca0 69 73 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 is a well-formed
11ca1 20 70 61 67 65 20 72 65 63 6f 72 64 20 63 6f 6e page record con
11ca2 73 69 73 74 69 6e 67 0a 2a 2a 20 6f 66 20 61 20 sisting.** of a
11ca3 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62 65 valid page numbe
11ca4 72 2c 20 70 50 61 67 65 2d 3e 70 61 67 65 53 69 r, pPage->pageSi
11ca5 7a 65 20 62 79 74 65 73 20 6f 66 20 63 6f 6e 74 ze bytes of cont
11ca6 65 6e 74 2c 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a ent, followed.**
11ca7 20 62 79 20 61 20 76 61 6c 69 64 20 63 68 65 63 by a valid chec
11ca8 6b 73 75 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ksum..**.** The
11ca9 70 61 67 65 72 20 6e 65 76 65 72 20 6e 65 65 64 pager never need
11caa 73 20 74 6f 20 6b 6e 6f 77 20 74 68 69 73 20 69 s to know this i
11cab 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 20 69 74 n order to do it
11cac 73 20 6a 6f 62 2e 20 20 20 54 68 69 73 0a 2a 2a s job. This.**
11cad 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 routine is only
11cae 20 75 73 65 64 20 66 72 6f 6d 20 77 69 74 68 20 used from with
11caf 61 73 73 65 72 74 28 29 20 61 6e 64 20 74 65 73 assert() and tes
11cb0 74 63 61 73 65 28 29 20 6d 61 63 72 6f 73 2e 0a tcase() macros..
11cb1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
11cb2 67 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c 50 61 gerNextJournalPa
11cb3 67 65 49 73 56 61 6c 69 64 28 50 61 67 65 72 20 geIsValid(Pager
11cb4 2a 70 50 61 67 65 72 29 7b 0a 20 20 50 67 6e 6f *pPager){. Pgno
11cb5 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 pgno;
11cb6 20 2f 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d /* The page num
11cb7 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 ber of the page
11cb8 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 */. u32 cksum;
11cb9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
11cba 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 2a page checksum *
11cbb 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
11cbc 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
11cbd 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 72 65 61 rn code from rea
11cbe 64 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a d operations */.
11cbf 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
11cc0 66 64 3b 20 20 20 20 2f 2a 20 54 68 65 20 66 69 fd; /* The fi
11cc1 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 72 le descriptor fr
11cc2 6f 6d 20 77 68 69 63 68 20 77 65 20 61 72 65 20 om which we are
11cc3 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 75 38 20 reading */. u8
11cc4 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 *aData;
11cc5 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 /* Content of
11cc6 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20 2f the page */.. /
11cc7 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 20 * Read the page
11cc8 6e 75 6d 62 65 72 20 68 65 61 64 65 72 20 2a 2f number header */
11cc9 0a 20 20 66 64 20 3d 20 70 50 61 67 65 72 2d 3e . fd = pPager->
11cca 6a 66 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 jfd;. rc = read
11ccb 33 32 62 69 74 73 28 66 64 2c 20 70 50 61 67 65 32bits(fd, pPage
11ccc 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 26 r->journalOff, &
11ccd 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 21 pgno);. if( rc!
11cce 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 72 65 =SQLITE_OK ){ re
11ccf 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 20 turn 0; }
11cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11cd1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f /*NO_
11cd2 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67 6e TEST*/. if( pgn
11cd3 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 o==0 || pgno==PA
11cd4 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
11cd5 65 72 29 20 29 7b 20 72 65 74 75 72 6e 20 30 3b er) ){ return 0;
11cd6 20 7d 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f } /*NO_
11cd7 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67 6e TEST*/. if( pgn
11cd8 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d 3e o>(Pgno)pPager->
11cd9 64 62 53 69 7a 65 20 29 7b 20 72 65 74 75 72 6e dbSize ){ return
11cda 20 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 0; }
11cdb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f /*NO_
11cdc 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 TEST*/.. /* Rea
11cdd 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 2a d the checksum *
11cde 2f 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 /. rc = read32b
11cdf 69 74 73 28 66 64 2c 20 70 50 61 67 65 72 2d 3e its(fd, pPager->
11ce0 6a 6f 75 72 6e 61 6c 4f 66 66 2b 70 50 61 67 65 journalOff+pPage
11ce1 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 2c 20 26 r->pageSize+4, &
11ce2 63 6b 73 75 6d 29 3b 0a 20 20 69 66 28 20 72 63 cksum);. if( rc
11ce3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 72 !=SQLITE_OK ){ r
11ce4 65 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 eturn 0; }
11ce5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11ce6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f /*NO
11ce7 5f 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 65 _TEST*/.. /* Re
11ce8 61 64 20 74 68 65 20 64 61 74 61 20 61 6e 64 20 ad the data and
11ce9 76 65 72 69 66 79 20 74 68 65 20 63 68 65 63 6b verify the check
11cea 73 75 6d 20 2a 2f 0a 20 20 61 44 61 74 61 20 3d sum */. aData =
11ceb 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 54 (u8*)pPager->pT
11cec 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 mpSpace;. rc =
11ced 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 66 64 sqlite3OsRead(fd
11cee 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d , aData, pPager-
11cef 3e 70 61 67 65 53 69 7a 65 2c 20 70 50 61 67 65 >pageSize, pPage
11cf0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 34 29 r->journalOff+4)
11cf1 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
11cf2 54 45 5f 4f 4b 20 29 7b 20 72 65 74 75 72 6e 20 TE_OK ){ return
11cf3 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 0; }
11cf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11cf5 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
11cf6 2f 0a 20 20 69 66 28 20 70 61 67 65 72 5f 63 6b /. if( pager_ck
11cf7 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 74 sum(pPager, aDat
11cf8 61 29 21 3d 63 6b 73 75 6d 20 29 7b 20 72 65 74 a)!=cksum ){ ret
11cf9 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 20 20 urn 0; }
11cfa 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
11cfb 2f 0a 0a 20 20 2f 2a 20 52 65 61 63 68 20 74 68 /.. /* Reach th
11cfc 69 73 20 70 6f 69 6e 74 20 6f 6e 6c 79 20 69 66 is point only if
11cfd 20 74 68 65 20 70 61 67 65 20 69 73 20 76 61 6c the page is val
11cfe 69 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 id */. return 1
11cff 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 ;.}.#endif /* !d
11d00 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c efined(NDEBUG) |
11d01 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
11d02 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 20 _COVERAGE_TEST)
11d03 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 */../*.** Parame
11d04 74 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 74 ter zMaster is t
11d05 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 he name of a mas
11d06 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
11d07 2e 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e . A single journ
11d08 61 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 al.** file that
11d09 72 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 20 referred to the
11d0a 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
11d0b 69 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 ile has just bee
11d0c 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a n rolled back..*
11d0d 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
11d0e 68 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 70 hecks if it is p
11d0f 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 ossible to delet
11d10 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
11d11 72 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e rnal file,.** an
11d12 64 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20 d does so if it
11d13 69 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 is..**.** Argume
11d14 6e 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 70 nt zMaster may p
11d15 6f 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 54 oint to Pager.pT
11d16 6d 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 74 mpSpace. So that
11d17 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 0a buffer is not .
11d18 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 ** available for
11d19 20 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 73 use within this
11d1a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
11d1b 0a 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a .** The master j
11d1c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 ournal file cont
11d1d 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 73 20 6f ains the names o
11d1e 66 20 61 6c 6c 20 63 68 69 6c 64 20 6a 6f 75 72 f all child jour
11d1f 6e 61 6c 73 2e 0a 2a 2a 20 54 6f 20 74 65 6c 6c nals..** To tell
11d20 20 69 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 if a master jou
11d21 72 6e 61 6c 20 63 61 6e 20 62 65 20 64 65 6c 65 rnal can be dele
11d22 74 65 64 2c 20 63 68 65 63 6b 20 74 6f 20 65 61 ted, check to ea
11d23 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 63 68 69 ch of the.** chi
11d24 6c 64 72 65 6e 2e 20 20 49 66 20 61 6c 6c 20 63 ldren. If all c
11d25 68 69 6c 64 72 65 6e 20 61 72 65 20 65 69 74 68 hildren are eith
11d26 65 72 20 6d 69 73 73 69 6e 67 20 6f 72 20 64 6f er missing or do
11d27 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 0a 2a 2a not refer to.**
11d28 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 61 73 a different mas
11d29 74 65 72 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 ter journal, the
11d2a 6e 20 74 68 69 73 20 6d 61 73 74 65 72 20 6a 6f n this master jo
11d2b 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 64 65 6c urnal can be del
11d2c 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 eted..*/.static
11d2d 69 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 int pager_delmas
11d2e 74 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 ter(Pager *pPage
11d2f 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a r, const char *z
11d30 4d 61 73 74 65 72 29 7b 0a 20 20 73 71 6c 69 74 Master){. sqlit
11d31 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 e3_vfs *pVfs = p
11d32 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 Pager->pVfs;. i
11d33 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6d 61 73 nt rc;. int mas
11d34 74 65 72 5f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 ter_open = 0;.
11d35 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d sqlite3_file *pM
11d36 61 73 74 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 aster;. sqlite3
11d37 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b _file *pJournal;
11d38 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 . char *zMaster
11d39 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 Journal = 0; /*
11d3a 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61 73 74 Contents of mast
11d3b 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
11d3c 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74 65 72 */. i64 nMaster
11d3d 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 2f Journal; /
11d3e 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 74 65 72 * Size of master
11d3f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f journal file */
11d40 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 .. /* Open the
11d41 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
11d42 69 6c 65 20 65 78 63 6c 75 73 69 76 65 6c 79 20 ile exclusively
11d43 69 6e 20 63 61 73 65 20 73 6f 6d 65 20 6f 74 68 in case some oth
11d44 65 72 20 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 er process. **
11d45 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 69 73 20 is running this
11d46 72 6f 75 74 69 6e 65 20 61 6c 73 6f 2e 20 4e 6f routine also. No
11d47 74 20 74 68 61 74 20 69 74 20 6d 61 6b 65 73 20 t that it makes
11d48 74 6f 6f 20 6d 75 63 68 20 64 69 66 66 65 72 65 too much differe
11d49 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 nce.. */. pMas
11d4a 74 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 ter = (sqlite3_f
11d4b 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c ile *)sqlite3Mal
11d4c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 loc(pVfs->szOsFi
11d4d 6c 65 20 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 le * 2);. pJour
11d4e 6e 61 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 nal = (sqlite3_f
11d4f 69 6c 65 20 2a 29 28 28 28 75 38 20 2a 29 70 4d ile *)(((u8 *)pM
11d50 61 73 74 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 aster) + pVfs->s
11d51 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 zOsFile);. if(
11d52 21 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 !pMaster ){.
11d53 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
11d54 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 M;. }else{.
11d55 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c int flags = (SQL
11d56 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
11d57 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 Y|SQLITE_OPEN_MA
11d58 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 STER_JOURNAL);.
11d59 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
11d5a 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 sOpen(pVfs, zMas
11d5b 74 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c ter, pMaster, fl
11d5c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 ags, 0);. }. i
11d5d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
11d5e 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 ) goto delmaste
11d5f 72 5f 6f 75 74 3b 0a 20 20 6d 61 73 74 65 72 5f r_out;. master_
11d60 6f 70 65 6e 20 3d 20 31 3b 0a 0a 20 20 72 63 20 open = 1;.. rc
11d61 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
11d62 69 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d ize(pMaster, &nM
11d63 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 asterJournal);.
11d64 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
11d65 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 OK ) goto delmas
11d66 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20 ter_out;.. if(
11d67 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 nMasterJournal>0
11d68 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a ){. char *zJ
11d69 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72 ournal;. char
11d6a 20 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 30 *zMasterPtr = 0
11d6b 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 65 ;. int nMaste
11d6c 72 50 74 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 rPtr = pPager->p
11d6d 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
11d6e 31 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 1;.. /* Load
11d6f 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 the entire maste
11d70 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 r journal file i
11d71 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e nto space obtain
11d72 65 64 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 73 ed from. ** s
11d73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
11d74 61 6e 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 and pointed to b
11d75 79 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c y zMasterJournal
11d76 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d . . */. zM
11d77 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 28 asterJournal = (
11d78 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 char *)sqlite3Ma
11d79 6c 6c 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74 65 lloc((int)nMaste
11d7a 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 rJournal + nMast
11d7b 65 72 50 74 72 29 3b 0a 20 20 20 20 69 66 28 20 erPtr);. if(
11d7c 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 !zMasterJournal
11d7d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
11d7e 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
11d7f 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 goto delmaster
11d80 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _out;. }.
11d81 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 7a 4d zMasterPtr = &zM
11d82 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 asterJournal[nMa
11d83 73 74 65 72 4a 6f 75 72 6e 61 6c 5d 3b 0a 20 20 sterJournal];.
11d84 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
11d85 52 65 61 64 28 70 4d 61 73 74 65 72 2c 20 7a 4d Read(pMaster, zM
11d86 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 28 69 asterJournal, (i
11d87 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 nt)nMasterJourna
11d88 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 l, 0);. if( r
11d89 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 c!=SQLITE_OK ) g
11d8a 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 oto delmaster_ou
11d8b 74 3b 0a 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c t;.. zJournal
11d8c 20 3d 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 = zMasterJourna
11d8d 6c 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a l;. while( (z
11d8e 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a Journal-zMasterJ
11d8f 6f 75 72 6e 61 6c 29 3c 6e 4d 61 73 74 65 72 4a ournal)<nMasterJ
11d90 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 ournal ){.
11d91 69 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20 int exists;.
11d92 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
11d93 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4a 6f Access(pVfs, zJo
11d94 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 urnal, SQLITE_AC
11d95 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 CESS_EXISTS, &ex
11d96 69 73 74 73 29 3b 0a 20 20 20 20 20 20 69 66 28 ists);. if(
11d97 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
11d98 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 {. goto d
11d99 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 elmaster_out;.
11d9a 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
11d9b 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 exists ){.
11d9c 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 /* One of the
11d9d 6a 6f 75 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64 journals pointed
11d9e 20 74 6f 20 62 79 20 74 68 65 20 6d 61 73 74 65 to by the maste
11d9f 72 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 r journal exists
11da0 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 70 65 .. ** Ope
11da1 6e 20 69 74 20 61 6e 64 20 63 68 65 63 6b 20 69 n it and check i
11da2 66 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20 74 f it points at t
11da3 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
11da4 6c 2e 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a l. If. **
11da5 20 73 6f 2c 20 72 65 74 75 72 6e 20 77 69 74 68 so, return with
11da6 6f 75 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 out deleting the
11da7 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11da8 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f file.. */
11da9 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a . int c;.
11daa 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 int flag
11dab 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e s = (SQLITE_OPEN
11dac 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 _READONLY|SQLITE
11dad 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e _OPEN_MAIN_JOURN
11dae 41 4c 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 AL);. rc
11daf 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 = sqlite3OsOpen(
11db0 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 pVfs, zJournal,
11db1 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c 61 67 73 2c pJournal, flags,
11db2 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0);. if(
11db3 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
11db4 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f {. goto
11db5 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
11db6 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }..
11db7 20 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 rc = readMast
11db8 65 72 4a 6f 75 72 6e 61 6c 28 70 4a 6f 75 72 6e erJournal(pJourn
11db9 61 6c 2c 20 7a 4d 61 73 74 65 72 50 74 72 2c 20 al, zMasterPtr,
11dba 6e 4d 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 nMasterPtr);.
11dbb 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c sqlite3OsCl
11dbc 6f 73 65 28 70 4a 6f 75 72 6e 61 6c 29 3b 0a 20 ose(pJournal);.
11dbd 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
11dbe 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
11dbf 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
11dc0 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 ster_out;.
11dc1 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 63 20 3d }.. c =
11dc2 20 7a 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d zMasterPtr[0]!=
11dc3 30 20 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73 0 && strcmp(zMas
11dc4 74 65 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29 terPtr, zMaster)
11dc5 3d 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ==0;. if(
11dc6 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 c ){.
11dc7 2f 2a 20 57 65 20 68 61 76 65 20 61 20 6d 61 74 /* We have a mat
11dc8 63 68 2e 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 ch. Do not delet
11dc9 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
11dca 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 rnal file. */.
11dcb 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c goto del
11dcc 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 master_out;.
11dcd 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
11dce 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20 zJournal +=
11dcf 28 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 (sqlite3Strlen30
11dd0 28 7a 4a 6f 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 (zJournal)+1);.
11dd1 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 72 63 }. }. . rc
11dd2 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 = sqlite3OsDele
11dd3 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 te(pVfs, zMaster
11dd4 2c 20 30 29 3b 0a 0a 64 65 6c 6d 61 73 74 65 72 , 0);..delmaster
11dd5 5f 6f 75 74 3a 0a 20 20 69 66 28 20 7a 4d 61 73 _out:. if( zMas
11dd6 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 terJournal ){.
11dd7 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
11dd8 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a MasterJournal);.
11dd9 20 20 7d 20 20 0a 20 20 69 66 28 20 6d 61 73 74 } . if( mast
11dda 65 72 5f 6f 70 65 6e 20 29 7b 0a 20 20 20 20 73 er_open ){. s
11ddb 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4d qlite3OsClose(pM
11ddc 61 73 74 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 aster);. }. sq
11ddd 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61 73 74 lite3_free(pMast
11dde 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 er);. return rc
11ddf 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 ;.}.../*.** If t
11de0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
11de1 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 61 6e file is open an
11de2 64 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c d an exclusive l
11de3 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 0a 2a 2a ock is held, .**
11de4 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 6d 61 truncate the ma
11de5 69 6e 20 66 69 6c 65 20 6f 66 20 74 68 65 20 67 in file of the g
11de6 69 76 65 6e 20 70 61 67 65 72 20 74 6f 20 74 68 iven pager to th
11de7 65 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62 e specified numb
11de8 65 72 20 0a 2a 2a 20 6f 66 20 70 61 67 65 73 2e er .** of pages.
11de9 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20 .**.** It might
11dea 6d 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73 might be the cas
11deb 65 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 e that the file
11dec 6f 6e 20 64 69 73 6b 20 69 73 20 73 6d 61 6c 6c on disk is small
11ded 65 72 20 74 68 61 6e 20 6e 50 61 67 65 2e 0a 2a er than nPage..*
11dee 2a 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 * This can happe
11def 6e 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 n, for example,
11df0 69 66 20 77 65 20 61 72 65 20 69 6e 20 74 68 65 if we are in the
11df1 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 74 72 61 middle of a tra
11df2 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 63 nsaction.** whic
11df3 68 20 68 61 73 20 65 78 74 65 6e 64 65 64 20 74 h has extended t
11df4 68 65 20 66 69 6c 65 20 73 69 7a 65 20 61 6e 64 he file size and
11df5 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 61 the new pages a
11df6 72 65 20 73 74 69 6c 6c 20 61 6c 6c 20 68 65 6c re still all hel
11df7 64 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2c 20 74 d.** in cache, t
11df8 68 65 6e 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 hen an INSERT or
11df9 20 55 50 44 41 54 45 20 64 6f 65 73 20 61 20 73 UPDATE does a s
11dfa 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61 63 tatement rollbac
11dfb 6b 2e 20 20 53 6f 6d 65 0a 2a 2a 20 6f 70 65 72 k. Some.** oper
11dfc 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70 ating system imp
11dfd 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e lementations can
11dfe 20 67 65 74 20 63 6f 6e 66 75 73 65 64 20 69 66 get confused if
11dff 20 79 6f 75 20 74 72 79 20 74 6f 0a 2a 2a 20 74 you try to.** t
11e00 72 75 6e 63 61 74 65 20 61 20 66 69 6c 65 20 74 runcate a file t
11e01 6f 20 73 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 o some size that
11e02 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
11e03 69 74 20 63 75 72 72 65 6e 74 6c 79 20 69 73 2c it currently is,
11e04 0a 2a 2a 20 73 6f 20 64 65 74 65 63 74 20 74 68 .** so detect th
11e05 69 73 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 is case and writ
11e06 65 20 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 e a single zero
11e07 62 79 74 65 20 74 6f 20 74 68 65 20 65 6e 64 20 byte to the end
11e08 6f 66 20 74 68 65 20 6e 65 77 0a 2a 2a 20 66 69 of the new.** fi
11e09 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 73 le instead..*/.s
11e0a 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f tatic int pager_
11e0b 74 72 75 6e 63 61 74 65 28 50 61 67 65 72 20 2a truncate(Pager *
11e0c 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 61 pPager, Pgno nPa
11e0d 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ge){. int rc =
11e0e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 SQLITE_OK;. if(
11e0f 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
11e10 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 PAGER_EXCLUSIVE
11e11 26 26 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 && pPager->fd->p
11e12 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 69 Methods ){. i
11e13 36 34 20 63 75 72 72 65 6e 74 53 69 7a 65 2c 20 64 currentSize,
11e14 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 newSize;. rc
11e15 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
11e16 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ize(pPager->fd,
11e17 26 63 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 ¤tSize);.
11e18 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 newSize = pPa
11e19 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 ger->pageSize*(i
11e1a 36 34 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 64)nPage;. if
11e1b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
11e1c 26 26 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d && currentSize!=
11e1d 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 newSize ){.
11e1e 20 69 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 if( currentSize
11e1f 3e 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 >newSize ){.
11e20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
11e21 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 OsTruncate(pPage
11e22 72 2d 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b r->fd, newSize);
11e23 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
11e24 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
11e25 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
11e26 2d 3e 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 ->fd, "", 1, new
11e27 53 69 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d Size-1);. }
11e28 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
11e29 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
11e2a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 pPager->dbFi
11e2b 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a leSize = nPage;.
11e2c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
11e2d 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
11e2e 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
11e2f 73 65 63 74 6f 72 53 69 7a 65 20 66 6f 72 20 74 sectorSize for t
11e30 68 65 20 67 69 76 65 6e 20 70 61 67 65 72 2e 0a he given pager..
11e31 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 74 6f 72 **.** The sector
11e32 20 73 69 7a 65 20 69 73 20 61 74 20 6c 65 61 73 size is at leas
11e33 74 20 61 73 20 62 69 67 20 61 73 20 74 68 65 20 t as big as the
11e34 73 65 63 74 6f 72 20 73 69 7a 65 20 72 65 70 6f sector size repo
11e35 72 74 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 rted.** by sqlit
11e36 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 e3OsSectorSize()
11e37 2e 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 65 . The minimum se
11e38 63 74 6f 72 20 73 69 7a 65 20 69 73 20 35 31 32 ctor size is 512
11e39 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
11e3a 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 50 setSectorSize(P
11e3b 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
11e3c 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e assert(pPager->
11e3d 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 fd->pMethods||pP
11e3e 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b ager->tempFile);
11e3f 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e . if( !pPager->
11e40 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 tempFile ){.
11e41 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 64 /* Sector size d
11e42 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 66 6f oesn't matter fo
11e43 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 r temporary file
11e44 73 2e 20 41 6c 73 6f 2c 20 74 68 65 20 66 69 6c s. Also, the fil
11e45 65 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 e. ** may not
11e46 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 have been opene
11e47 64 20 79 65 74 2c 20 69 6e 20 77 68 63 69 68 20 d yet, in whcih
11e48 63 61 73 65 20 74 68 65 20 4f 73 53 65 63 74 6f case the OsSecto
11e49 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 63 rSize(). ** c
11e4a 61 6c 6c 20 77 69 6c 6c 20 73 65 67 66 61 75 6c all will segfaul
11e4b 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 t.. */. pP
11e4c 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
11e4d 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 = sqlite3OsSect
11e4e 6f 72 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 orSize(pPager->f
11e4f 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 d);. }. if( pP
11e50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
11e51 3c 35 31 32 20 29 7b 0a 20 20 20 20 70 50 61 67 <512 ){. pPag
11e52 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d er->sectorSize =
11e53 20 35 31 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 512;. }. if(
11e54 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
11e55 7a 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 ze>MAX_SECTOR_SI
11e56 5a 45 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 ZE ){. pPager
11e57 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 4d ->sectorSize = M
11e58 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a AX_SECTOR_SIZE;.
11e59 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 }.}../*.** Pla
11e5a 79 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 yback the journa
11e5b 6c 20 61 6e 64 20 74 68 75 73 20 72 65 73 74 6f l and thus resto
11e5c 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 re the database
11e5d 66 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 73 file to.** the s
11e5e 74 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 62 tate it was in b
11e5f 65 66 6f 72 65 20 77 65 20 73 74 61 72 74 65 64 efore we started
11e60 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 2e making changes.
11e61 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 .**.** The jou
11e62 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 rnal file format
11e63 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 is as follows:
11e64 0a 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38 20 62 .**.** (1) 8 b
11e65 79 74 65 20 70 72 65 66 69 78 2e 20 20 41 20 63 yte prefix. A c
11e66 6f 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d opy of aJournalM
11e67 61 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 agic[]..** (2)
11e68 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 4 byte big-endi
11e69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 an integer which
11e6a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
11e6b 66 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63 f valid page rec
11e6c 6f 72 64 73 0a 2a 2a 20 20 20 20 20 20 20 69 6e ords.** in
11e6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 the journal. I
11e6e 66 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 f this value is
11e6f 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 0xffffffff, then
11e70 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 compute the.**
11e71 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 number of
11e72 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 72 6f page records fro
11e73 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73 69 m the journal si
11e74 7a 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34 20 62 ze..** (3) 4 b
11e75 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 yte big-endian i
11e76 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 nteger which is
11e77 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 the initial valu
11e78 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 e for the .**
11e79 20 20 20 20 73 61 6e 69 74 79 20 63 68 65 63 6b sanity check
11e7a 73 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20 34 20 sum..** (4) 4
11e7b 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 69 byte integer whi
11e7c 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ch is the number
11e7d 20 6f 66 20 70 61 67 65 73 20 74 6f 20 74 72 75 of pages to tru
11e7e 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 ncate the.**
11e7f 20 20 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 database to d
11e80 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b uring a rollback
11e81 2e 0a 2a 2a 20 20 28 35 29 20 20 34 20 62 79 74 ..** (5) 4 byt
11e82 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 e big-endian int
11e83 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 eger which is th
11e84 65 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 20 e sector size.
11e85 54 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 The header.**
11e86 20 20 20 20 69 73 20 74 68 69 73 20 6d 61 6e 79 is this many
11e87 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a bytes in size..
11e88 2a 2a 20 20 28 36 29 20 20 34 20 62 79 74 65 20 ** (6) 4 byte
11e89 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 big-endian integ
11e8a 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 er which is the
11e8b 70 61 67 65 20 63 61 73 65 2e 0a 2a 2a 20 20 28 page case..** (
11e8c 37 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67 7) 4 byte integ
11e8d 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 er which is the
11e8e 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
11e8f 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f in the master jo
11e90 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 6e urnal.** n
11e91 61 6d 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 ame. The value
11e92 6d 61 79 20 62 65 20 7a 65 72 6f 20 28 69 6e 64 may be zero (ind
11e93 69 63 61 74 65 20 74 68 61 74 20 74 68 65 72 65 icate that there
11e94 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a is no master.**
11e95 20 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c 2e 29 journal.)
11e96 0a 2a 2a 20 20 28 38 29 20 20 4e 20 62 79 74 65 .** (8) N byte
11e97 73 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 s of the master
11e98 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 20 54 journal name. T
11e99 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20 he name will be
11e9a 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a nul-terminated.*
11e9b 2a 20 20 20 20 20 20 20 61 6e 64 20 6d 69 67 68 * and migh
11e9c 74 20 62 65 20 73 68 6f 72 74 65 72 20 74 68 61 t be shorter tha
11e9d 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 n the value read
11e9e 20 66 72 6f 6d 20 28 35 29 2e 20 20 49 66 20 74 from (5). If t
11e9f 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 2a 2a he first byte.**
11ea0 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6e 61 of the na
11ea1 6d 65 20 69 73 20 5c 30 30 30 20 74 68 65 6e 20 me is \000 then
11ea2 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 there is no mast
11ea3 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 er journal. The
11ea4 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20 20 master.**
11ea5 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73 journal name is
11ea6 20 73 74 6f 72 65 64 20 69 6e 20 55 54 46 2d 38 stored in UTF-8
11ea7 2e 0a 2a 2a 20 20 28 39 29 20 20 5a 65 72 6f 20 ..** (9) Zero
11ea8 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20 69 6e or more pages in
11ea9 73 74 61 6e 63 65 73 2c 20 65 61 63 68 20 61 73 stances, each as
11eaa 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 follows:.**
11eab 20 20 20 20 2b 20 20 34 20 62 79 74 65 20 70 61 + 4 byte pa
11eac 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 ge number..**
11ead 20 20 20 20 20 2b 20 20 70 50 61 67 65 72 2d 3e + pPager->
11eae 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f pageSize bytes o
11eaf 66 20 64 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 f data..**
11eb0 20 20 2b 20 20 34 20 62 79 74 65 20 63 68 65 63 + 4 byte chec
11eb1 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ksum.**.** When
11eb2 77 65 20 73 70 65 61 6b 20 6f 66 20 74 68 65 20 we speak of the
11eb3 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 journal header,
11eb4 77 65 20 6d 65 61 6e 20 74 68 65 20 66 69 72 73 we mean the firs
11eb5 74 20 38 20 69 74 65 6d 73 20 61 62 6f 76 65 2e t 8 items above.
11eb6 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69 .** Each entry i
11eb7 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 n the journal is
11eb8 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
11eb9 74 68 65 20 39 74 68 20 69 74 65 6d 2e 0a 2a 2a the 9th item..**
11eba 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76 61 6c .** Call the val
11ebb 75 65 20 66 72 6f 6d 20 74 68 65 20 73 65 63 6f ue from the seco
11ebc 6e 64 20 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 nd bullet "nRec"
11ebd 2e 20 20 6e 52 65 63 20 69 73 20 74 68 65 20 6e . nRec is the n
11ebe 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 umber of.** vali
11ebf 64 20 70 61 67 65 20 65 6e 74 72 69 65 73 20 69 d page entries i
11ec0 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 n the journal.
11ec1 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 In most cases, y
11ec2 6f 75 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 ou can compute t
11ec3 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e he.** value of n
11ec4 52 65 63 20 66 72 6f 6d 20 74 68 65 20 73 69 7a Rec from the siz
11ec5 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
11ec6 20 66 69 6c 65 2e 20 20 42 75 74 20 69 66 20 61 file. But if a
11ec7 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 power.** failur
11ec8 65 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 e occurred while
11ec9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 the journal was
11eca 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 being written,
11ecb 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 65 0a it could be the.
11ecc 2a 2a 20 63 61 73 65 20 74 68 61 74 20 74 68 65 ** case that the
11ecd 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 size of the jou
11ece 72 6e 61 6c 20 66 69 6c 65 20 68 61 64 20 61 6c rnal file had al
11ecf 72 65 61 64 79 20 62 65 65 6e 20 69 6e 63 72 65 ready been incre
11ed0 61 73 65 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 ased but.** the
11ed1 65 78 74 72 61 20 65 6e 74 72 69 65 73 20 68 61 extra entries ha
11ed2 64 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 69 d not yet made i
11ed3 74 20 73 61 66 65 6c 79 20 74 6f 20 64 69 73 6b t safely to disk
11ed4 2e 20 20 49 6e 20 73 75 63 68 20 61 20 63 61 73 . In such a cas
11ed5 65 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 e,.** the value
11ed6 6f 66 20 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 of nRec computed
11ed7 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 from the file s
11ed8 69 7a 65 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f ize would be too
11ed9 20 6c 61 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 large. For.**
11eda 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 that reason, we
11edb 61 6c 77 61 79 73 20 75 73 65 20 74 68 65 20 6e always use the n
11edc 52 65 63 20 76 61 6c 75 65 20 69 6e 20 74 68 65 Rec value in the
11edd 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 header..**.** I
11ede 66 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 f the nRec value
11edf 20 69 73 20 30 78 66 66 66 66 66 66 66 66 20 69 is 0xffffffff i
11ee0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 52 65 t means that nRe
11ee1 63 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 c should be comp
11ee2 75 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 uted.** from the
11ee3 20 66 69 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 file size. Thi
11ee4 73 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 s value is used
11ee5 77 68 65 6e 20 74 68 65 20 75 73 65 72 20 73 65 when the user se
11ee6 6c 65 63 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d lects the.** no-
11ee7 73 79 6e 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 sync option for
11ee8 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 the journal. A
11ee9 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f power failure co
11eea 75 6c 64 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 uld lead to corr
11eeb 75 70 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 uption.** in thi
11eec 73 20 63 61 73 65 2e 20 20 42 75 74 20 66 6f 72 s case. But for
11eed 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d things like tem
11eee 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28 77 68 porary table (wh
11eef 69 63 68 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 ich will be.** d
11ef0 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 eleted when the
11ef1 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 power is restore
11ef2 64 29 20 77 65 20 64 6f 6e 27 74 20 63 61 72 65 d) we don't care
11ef3 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 . .**.** If the
11ef4 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 file opened as
11ef5 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11ef6 20 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 is not a well-f
11ef7 6f 72 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c ormed.** journal
11ef8 20 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 file then all p
11ef9 61 67 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 ages up to the f
11efa 69 72 73 74 20 63 6f 72 72 75 70 74 65 64 20 70 irst corrupted p
11efb 61 67 65 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a age are rolled.*
11efc 2a 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 * back (or no pa
11efd 67 65 73 20 69 66 20 74 68 65 20 6a 6f 75 72 6e ges if the journ
11efe 61 6c 20 68 65 61 64 65 72 20 69 73 20 63 6f 72 al header is cor
11eff 72 75 70 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 rupted). The jou
11f00 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 rnal file.** is
11f01 74 68 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 then deleted and
11f02 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 SQLITE_OK retur
11f03 6e 65 64 2c 20 6a 75 73 74 20 61 73 20 69 66 20 ned, just as if
11f04 6e 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 no corruption ha
11f05 64 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e d.** been encoun
11f06 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 tered..**.** If
11f07 61 6e 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 an I/O or malloc
11f08 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c () error occurs,
11f09 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c the journal-fil
11f0a 65 20 69 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 e is not deleted
11f0b 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 .** and an error
11f0c 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
11f0d 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
11f0e 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
11f0f 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
11f10 6e 74 20 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c nt isHot){. sql
11f11 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d ite3_vfs *pVfs =
11f12 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 pPager->pVfs;.
11f13 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 i64 szJ;
11f14 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
11f15 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
11f16 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a file in bytes *
11f17 2f 0a 20 20 75 33 32 20 6e 52 65 63 3b 20 20 20 /. u32 nRec;
11f18 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11f19 4e 75 6d 62 65 72 20 6f 66 20 52 65 63 6f 72 64 Number of Record
11f1a 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c s in the journal
11f1b 20 2a 2f 0a 20 20 75 33 32 20 75 3b 20 20 20 20 */. u32 u;
11f1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11f1d 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 * Unsigned loop
11f1e 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 50 67 6e counter */. Pgn
11f1f 6f 20 6d 78 50 67 20 3d 20 30 3b 20 20 20 20 20 o mxPg = 0;
11f20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
11f21 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 the original fi
11f22 6c 65 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 le in pages */.
11f23 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
11f24 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
11f25 75 6c 74 20 63 6f 64 65 20 6f 66 20 61 20 73 75 ult code of a su
11f26 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e broutine */. in
11f27 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20 t res = 1;
11f28 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 /* Value
11f29 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
11f2a 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 2a 2f te3OsAccess() */
11f2b 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 . char *zMaster
11f2c 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e = 0; /* N
11f2d 61 6d 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f ame of master jo
11f2e 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 61 6e urnal file if an
11f2f 79 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 y */.. /* Figur
11f30 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 e out how many r
11f31 65 63 6f 72 64 73 20 61 72 65 20 69 6e 20 74 68 ecords are in th
11f32 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 e journal. Abor
11f33 74 20 65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 t early if. **
11f34 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 the journal is e
11f35 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 mpty.. */. ass
11f36 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ert( pPager->jou
11f37 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 20 20 72 63 rnalOpen );. rc
11f38 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 = sqlite3OsFile
11f39 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 Size(pPager->jfd
11f3a 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 , &szJ);. if( r
11f3b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
11f3c 73 7a 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f szJ==0 ){. go
11f3d 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b to end_playback;
11f3e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 . }.. /* Read
11f3f 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
11f40 61 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 al name from the
11f41 20 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 journal, if it
11f42 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a is present.. **
11f43 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 If a master jou
11f44 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 rnal file name i
11f45 73 20 73 70 65 63 69 66 69 65 64 2c 20 62 75 74 s specified, but
11f46 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 the file is not
11f47 0a 20 20 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e . ** present on
11f48 20 64 69 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 disk, then the
11f49 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 journal is not h
11f4a 6f 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 ot and does not
11f4b 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 need to be. **
11f4c 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a played back.. *
11f4d 2f 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 /. zMaster = pP
11f4e 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b ager->pTmpSpace;
11f4f 0a 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 . rc = readMast
11f50 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 erJournal(pPager
11f51 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 ->jfd, zMaster,
11f52 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 pPager->pVfs->mx
11f53 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 69 Pathname+1);. i
11f54 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
11f55 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 29 && zMaster[0] )
11f56 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
11f57 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c e3OsAccess(pVfs,
11f58 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 zMaster, SQLITE
11f59 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 _ACCESS_EXISTS,
11f5a 26 72 65 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d 61 &res);. }. zMa
11f5b 73 74 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 ster = 0;. if(
11f5c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
11f5d 20 21 72 65 73 20 29 7b 0a 20 20 20 20 67 6f 74 !res ){. got
11f5e 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
11f5f 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f }. pPager->jo
11f60 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 0a 20 urnalOff = 0;..
11f61 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74 65 /* This loop te
11f62 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72 20 rminates either
11f63 77 68 65 6e 20 74 68 65 20 72 65 61 64 4a 6f 75 when the readJou
11f64 72 6e 61 6c 48 64 72 28 29 20 63 61 6c 6c 20 72 rnalHdr() call r
11f65 65 74 75 72 6e 73 0a 20 20 2a 2a 20 53 51 4c 49 eturns. ** SQLI
11f66 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f TE_DONE or an IO
11f67 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a error occurs. *
11f68 2f 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a /. while( 1 ){.
11f69 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 . /* Read the
11f6a 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 next journal he
11f6b 61 64 65 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f ader from the jo
11f6c 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 urnal file. If
11f6d 74 68 65 72 65 20 61 72 65 0a 20 20 20 20 2a 2a there are. **
11f6e 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 74 65 not enough byte
11f6f 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 6a 6f s left in the jo
11f70 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 urnal file for a
11f71 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 complete header
11f72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 , or. ** it i
11f73 73 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65 s corrupted, the
11f74 6e 20 61 20 70 72 6f 63 65 73 73 20 6d 75 73 74 n a process must
11f75 20 6f 66 20 66 61 69 6c 65 64 20 77 68 69 6c 65 of failed while
11f76 20 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 writing it..
11f77 20 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 ** This indicat
11f78 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 es nothing more
11f79 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c needs to be roll
11f7a 65 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a ed back.. */.
11f7b 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 rc = readJou
11f7c 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 rnalHdr(pPager,
11f7d 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d 78 50 szJ, &nRec, &mxP
11f7e 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d g);. if( rc!=
11f7f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a 20 20 SQLITE_OK ){ .
11f80 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
11f81 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 TE_DONE ){.
11f82 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
11f83 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 K;. }.
11f84 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 goto end_playba
11f85 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f ck;. }.. /
11f86 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 78 66 * If nRec is 0xf
11f87 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 74 68 fffffff, then th
11f88 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 63 is journal was c
11f89 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 reated by a proc
11f8a 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 69 ess. ** worki
11f8b 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f ng in no-sync mo
11f8c 64 65 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 de. This means t
11f8d 68 61 74 20 74 68 65 20 72 65 73 74 20 6f 66 20 hat the rest of
11f8e 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 the journal.
11f8f 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73 74 73 ** file consists
11f90 20 6f 66 20 70 61 67 65 73 2c 20 74 68 65 72 65 of pages, there
11f91 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75 are no more jou
11f92 72 6e 61 6c 20 68 65 61 64 65 72 73 2e 20 43 6f rnal headers. Co
11f93 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 74 68 65 mpute. ** the
11f94 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 62 value of nRec b
11f95 61 73 65 64 20 6f 6e 20 74 68 69 73 20 61 73 73 ased on this ass
11f96 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a umption.. */.
11f97 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30 78 if( nRec==0x
11f98 66 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 ffffffff ){.
11f99 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
11f9a 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f ->journalOff==JO
11f9b 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
11f9c 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 6e 52 ger) );. nR
11f9d 65 63 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 ec = (int)((szJ
11f9e 2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a - JOURNAL_HDR_SZ
11f9f 28 70 50 61 67 65 72 29 29 2f 4a 4f 55 52 4e 41 (pPager))/JOURNA
11fa0 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 L_PG_SZ(pPager))
11fa1 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
11fa2 49 66 20 6e 52 65 63 20 69 73 20 30 20 61 6e 64 If nRec is 0 and
11fa3 20 74 68 69 73 20 72 6f 6c 6c 62 61 63 6b 20 69 this rollback i
11fa4 73 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 s of a transacti
11fa5 6f 6e 20 63 72 65 61 74 65 64 20 62 79 20 74 68 on created by th
11fa6 69 73 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 is. ** proces
11fa7 73 20 61 6e 64 20 69 66 20 74 68 69 73 20 69 73 s and if this is
11fa8 20 74 68 65 20 66 69 6e 61 6c 20 68 65 61 64 65 the final heade
11fa9 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c r in the journal
11faa 2c 20 74 68 65 6e 20 69 74 20 6d 65 61 6e 73 0a , then it means.
11fab 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 69 73 ** that this
11fac 20 70 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 part of the jou
11fad 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 20 66 rnal was being f
11fae 69 6c 6c 65 64 20 62 75 74 20 68 61 73 20 6e 6f illed but has no
11faf 74 20 79 65 74 20 62 65 65 6e 0a 20 20 20 20 2a t yet been. *
11fb0 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b * synced to disk
11fb1 2e 20 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6e . Compute the n
11fb2 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 62 umber of pages b
11fb3 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65 6d 61 ased on the rema
11fb4 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 73 69 7a ining. ** siz
11fb5 65 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 20 e of the file..
11fb6 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
11fb7 20 74 68 69 72 64 20 74 65 72 6d 20 6f 66 20 74 third term of t
11fb8 68 65 20 74 65 73 74 20 77 61 73 20 61 64 64 65 he test was adde
11fb9 64 20 74 6f 20 66 69 78 20 74 69 63 6b 65 74 20 d to fix ticket
11fba 23 32 35 36 35 2e 0a 20 20 20 20 2a 2a 20 57 68 #2565.. ** Wh
11fbb 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 en rolling back
11fbc 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2c 20 6e a hot journal, n
11fbd 52 65 63 3d 3d 30 20 61 6c 77 61 79 73 20 6d 65 Rec==0 always me
11fbe 61 6e 73 20 74 68 61 74 20 74 68 65 20 6e 65 78 ans that the nex
11fbf 74 0a 20 20 20 20 2a 2a 20 63 68 75 6e 6b 20 6f t. ** chunk o
11fc0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 63 6f f the journal co
11fc1 6e 74 61 69 6e 73 20 7a 65 72 6f 20 70 61 67 65 ntains zero page
11fc2 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 s to be rolled b
11fc3 61 63 6b 2e 20 20 42 75 74 0a 20 20 20 20 2a 2a ack. But. **
11fc4 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 52 4f when doing a RO
11fc5 4c 4c 42 41 43 4b 20 61 6e 64 20 74 68 65 20 6e LLBACK and the n
11fc6 52 65 63 3d 3d 30 20 63 68 75 6e 6b 20 69 73 20 Rec==0 chunk is
11fc7 74 68 65 20 6c 61 73 74 20 63 68 75 6e 6b 20 69 the last chunk i
11fc8 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 n. ** the jou
11fc9 72 6e 61 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 rnal, it means t
11fca 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
11fcb 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 61 64 might contain ad
11fcc 64 69 74 69 6f 6e 61 6c 0a 20 20 20 20 2a 2a 20 ditional. **
11fcd 70 61 67 65 73 20 74 68 61 74 20 6e 65 65 64 20 pages that need
11fce 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 to be rolled bac
11fcf 6b 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 6e k and that the n
11fd0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 0a umber of pages .
11fd1 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 ** should be
11fd2 20 63 6f 6d 70 75 74 65 64 20 62 61 73 65 64 20 computed based
11fd3 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 on the journal f
11fd4 69 6c 65 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f ile size.. */
11fd5 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e . testcase( n
11fd6 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 Rec==0 && !isHot
11fd7 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 . && pPa
11fd8 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b ger->journalHdr+
11fd9 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
11fda 50 61 67 65 72 29 21 3d 70 50 61 67 65 72 2d 3e Pager)!=pPager->
11fdb 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20 20 journalOff.
11fdc 20 20 20 20 26 26 20 28 28 73 7a 4a 20 2d 20 70 && ((szJ - p
11fdd 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11fde 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f f) / JOURNAL_PG_
11fdf 53 5a 28 70 50 61 67 65 72 29 29 3e 30 0a 20 20 SZ(pPager))>0.
11fe0 20 20 20 20 20 20 20 26 26 20 70 61 67 65 72 4e && pagerN
11fe1 65 78 74 4a 6f 75 72 6e 61 6c 50 61 67 65 49 73 extJournalPageIs
11fe2 56 61 6c 69 64 28 70 50 61 67 65 72 29 0a 20 20 Valid(pPager).
11fe3 20 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 52 65 );. if( nRe
11fe4 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20 26 c==0 && !isHot &
11fe5 26 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 &. pPager
11fe6 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 ->journalHdr+JOU
11fe7 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
11fe8 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 er)==pPager->jou
11fe9 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 20 rnalOff ){.
11fea 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73 nRec = (int)((s
11feb 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 zJ - pPager->jou
11fec 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e rnalOff) / JOURN
11fed 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 AL_PG_SZ(pPager)
11fee 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a );. }.. /*
11fef 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
11ff0 66 69 72 73 74 20 68 65 61 64 65 72 20 72 65 61 first header rea
11ff1 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e d from the journ
11ff2 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 74 68 65 al, truncate the
11ff3 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 . ** database
11ff4 20 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74 file back to it
11ff5 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e s original size.
11ff6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
11ff7 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11ff8 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ff==JOURNAL_HDR_
11ff9 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 SZ(pPager) ){.
11ffa 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 rc = pager_t
11ffb 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 runcate(pPager,
11ffc 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 mxPg);. if(
11ffd 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
11ffe 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 {. goto e
11fff 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 nd_playback;.
12000 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 }. pPage
12001 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 78 50 67 r->dbSize = mxPg
12002 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
12003 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61 Copy original pa
12004 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a ges out of the j
12005 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 ournal and back
12006 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
12007 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 e file.. */.
12008 20 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 for(u=0; u<nR
12009 65 63 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20 ec; u++){.
1200a 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 rc = pager_playb
1200b 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 ack_one_page(pPa
1200c 67 65 72 2c 20 31 2c 20 26 70 50 61 67 65 72 2d ger, 1, &pPager-
1200d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 30 2c 20 >journalOff, 0,
1200e 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 0);. if( rc
1200f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
12010 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
12011 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 QLITE_DONE ){.
12012 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
12013 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 ITE_OK;.
12014 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
12015 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 lOff = szJ;.
12016 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
12017 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
12018 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 /* If we a
12019 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f 6c re unable to rol
1201a 6c 62 61 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 lback, then the
1201b 64 61 74 61 62 61 73 65 20 69 73 20 70 72 6f 62 database is prob
1201c 61 62 6c 79 0a 20 20 20 20 20 20 20 20 20 20 2a ably. *
1201d 2a 20 67 6f 69 6e 67 20 74 6f 20 65 6e 64 20 75 * going to end u
1201e 70 20 62 65 69 6e 67 20 63 6f 72 72 75 70 74 2e p being corrupt.
1201f 20 20 49 74 20 69 73 20 63 6f 72 72 75 70 74 20 It is corrupt
12020 74 6f 20 75 73 2c 20 61 6e 79 68 6f 77 2e 0a 20 to us, anyhow..
12021 20 20 20 20 20 20 20 20 20 2a 2a 20 50 65 72 68 ** Perh
12022 61 70 73 20 74 68 65 20 6e 65 78 74 20 70 72 6f aps the next pro
12023 63 65 73 73 20 74 6f 20 63 6f 6d 65 20 61 6c 6f cess to come alo
12024 6e 67 20 63 61 6e 20 66 69 78 20 69 74 2e 2e 2e ng can fix it...
12025 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
12026 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
12027 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
12028 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 T;. got
12029 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
1202a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1202b 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a }. }. }. /*
1202c 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 NOTREACHED*/. a
1202d 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 ssert( 0 );..end
1202e 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 _playback:. /*
1202f 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c Following a roll
12030 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61 back, the databa
12031 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 se file should b
12032 65 20 62 61 63 6b 20 69 6e 20 69 74 73 20 6f 72 e back in its or
12033 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74 iginal. ** stat
12034 65 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 e prior to the s
12035 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e tart of the tran
12036 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f saction, so invo
12037 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 ke the. ** SQLI
12038 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 TE_FCNTL_DB_UNCH
12039 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72 ANGED file-contr
1203a 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 64 69 73 ol method to dis
1203b 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a 20 61 73 able the. ** as
1203c 73 65 72 74 69 6f 6e 20 74 68 61 74 20 74 68 65 sertion that the
1203d 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 transaction cou
1203e 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 66 69 65 nter was modifie
1203f 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 d.. */. assert
12040 28 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 66 64 (. pPager->fd
12041 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 7c 7c ->pMethods==0 ||
12042 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 . sqlite3OsFi
12043 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72 leControl(pPager
12044 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 43 4e 54 ->fd,SQLITE_FCNT
12045 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 2c 30 L_DB_UNCHANGED,0
12046 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 )>=SQLITE_OK. )
12047 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c ;.. if( rc==SQL
12048 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d ITE_OK ){. zM
12049 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e aster = pPager->
1204a 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72 pTmpSpace;. r
1204b 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f c = readMasterJo
1204c 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 urnal(pPager->jf
1204d 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 d, zMaster, pPag
1204e 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 er->pVfs->mxPath
1204f 6e 61 6d 65 2b 31 29 3b 0a 20 20 7d 0a 20 20 69 name+1);. }. i
12050 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12051 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 ){. rc = pag
12052 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 er_end_transacti
12053 6f 6e 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 on(pPager, zMast
12054 65 72 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 er[0]!='\0');.
12055 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 }. if( rc==SQLI
12056 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 TE_OK && zMaster
12057 5b 30 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 20 [0] && res ){.
12058 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 /* If there wa
12059 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e s a master journ
1205a 61 6c 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 al and this rout
1205b 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ine will return
1205c 73 75 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 success,. **
1205d 73 65 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 see if it is pos
1205e 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 sible to delete
1205f 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
12060 61 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 al.. */. r
12061 63 20 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 c = pager_delmas
12062 74 65 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 ter(pPager, zMas
12063 74 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ter);. }.. /*
12064 54 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 The Pager.sector
12065 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 Size variable ma
12066 79 20 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 y have been upda
12067 74 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e ted while rollin
12068 67 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f g. ** back a jo
12069 75 72 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79 urnal created by
1206a 20 61 20 70 72 6f 63 65 73 73 20 77 69 74 68 20 a process with
1206b 61 20 64 69 66 66 65 72 65 6e 74 20 73 65 63 74 a different sect
1206c 6f 72 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c or size. ** val
1206d 75 65 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20 ue. Reset it to
1206e 74 68 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75 the correct valu
1206f 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 e for this proce
12070 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 ss.. */. setSe
12071 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 ctorSize(pPager)
12072 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
12073 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b ../*.** Playback
12074 20 73 61 76 65 70 6f 69 6e 74 20 70 53 61 76 65 savepoint pSave
12075 70 6f 69 6e 74 2e 20 20 4f 72 2c 20 69 66 20 70 point. Or, if p
12076 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c Savepoint==NULL,
12077 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 0a 2a then playback.*
12078 2a 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 * the entire mas
12079 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
1207a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 73 65 ..**.** The case
1207b 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c pSavepoint==NUL
1207c 4c 20 6f 63 63 75 72 73 20 77 68 65 6e 20 61 20 L occurs when a
1207d 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 63 6f 6d 6d ROLLBACK TO comm
1207e 61 6e 64 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a and is invoked.*
1207f 2a 20 6f 6e 20 61 20 53 41 56 45 50 4f 49 4e 54 * on a SAVEPOINT
12080 20 74 68 61 74 20 69 73 20 61 20 74 72 61 6e 73 that is a trans
12081 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 action savepoint
12082 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
12083 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 pagerPlaybackSav
12084 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 epoint(Pager *pP
12085 61 67 65 72 2c 20 50 61 67 65 72 53 61 76 65 70 ager, PagerSavep
12086 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 oint *pSavepoint
12087 29 7b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 ){. i64 szJ;
12088 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12089 20 45 66 66 65 63 74 69 76 65 20 73 69 7a 65 20 Effective size
1208a 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 of the main jour
1208b 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 nal */. i64 iHd
1208c 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 rOff;
1208d 20 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69 72 73 /* End of firs
1208e 74 20 73 65 67 6d 65 6e 74 20 6f 66 20 6d 61 69 t segment of mai
1208f 6e 2d 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 n-journal record
12090 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 69 3b 20 s */. Pgno ii;
12091 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12092 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
12093 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
12094 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a LITE_OK; /*
12095 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
12096 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 Bitvec *pDone
12097 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 = 0; /* Bi
12098 74 76 65 63 20 74 6f 20 65 6e 73 75 72 65 20 70 tvec to ensure p
12099 61 67 65 73 20 70 6c 61 79 65 64 20 62 61 63 6b ages played back
1209a 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20 only once */..
1209b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62 /* Allocate a b
1209c 69 74 76 65 63 20 74 6f 20 75 73 65 20 74 6f 20 itvec to use to
1209d 73 74 6f 72 65 20 74 68 65 20 73 65 74 20 6f 66 store the set of
1209e 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61 pages rolled ba
1209f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 ck */. if( pSav
120a0 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 70 44 epoint ){. pD
120a1 6f 6e 65 20 3d 20 73 71 6c 69 74 65 33 42 69 74 one = sqlite3Bit
120a2 76 65 63 43 72 65 61 74 65 28 70 53 61 76 65 70 vecCreate(pSavep
120a3 6f 69 6e 74 2d 3e 6e 4f 72 69 67 29 3b 0a 20 20 oint->nOrig);.
120a4 20 20 69 66 28 20 21 70 44 6f 6e 65 20 29 7b 0a if( !pDone ){.
120a5 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
120a6 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
120a7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 75 6e 63 . }.. /* Trunc
120a8 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ate the database
120a9 20 62 61 63 6b 20 74 6f 20 74 68 65 20 73 69 7a back to the siz
120aa 65 20 69 74 20 77 61 73 20 62 65 66 6f 72 65 20 e it was before
120ab 74 68 65 20 0a 20 20 2a 2a 20 73 61 76 65 70 6f the . ** savepo
120ac 69 6e 74 20 62 65 69 6e 67 20 72 65 76 65 72 74 int being revert
120ad 65 64 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 20 ed was opened..
120ae 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 */. pPager->db
120af 53 69 7a 65 20 3d 20 70 53 61 76 65 70 6f 69 6e Size = pSavepoin
120b0 74 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e t ? pSavepoint->
120b1 6e 4f 72 69 67 20 3a 20 70 50 61 67 65 72 2d 3e nOrig : pPager->
120b2 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20 61 73 dbOrigSize;. as
120b3 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
120b4 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 ate>=PAGER_SHARE
120b5 44 20 29 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 70 D );.. /* Use p
120b6 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
120b7 66 20 61 73 20 74 68 65 20 65 66 66 65 63 74 69 f as the effecti
120b8 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d ve size of the m
120b9 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a ain rollback. *
120ba 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 * journal. The
120bb 61 63 74 75 61 6c 20 66 69 6c 65 20 6d 69 67 68 actual file migh
120bc 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e t be larger than
120bd 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a 20 50 41 this in. ** PA
120be 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
120bf 54 52 55 4e 43 41 54 45 20 6f 72 20 50 41 47 45 TRUNCATE or PAGE
120c0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 R_JOURNALMODE_PE
120c1 52 53 49 53 54 2e 20 20 42 75 74 20 61 6e 79 74 RSIST. But anyt
120c2 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 74 20 70 hing. ** past p
120c3 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
120c4 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 74 73 20 f is off-limits
120c5 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a to us.. */. sz
120c6 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 J = pPager->jour
120c7 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 65 nalOff;.. /* Be
120c8 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62 gin by rolling b
120c9 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 6f 6d ack records from
120ca 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
120cb 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 l starting at.
120cc 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e ** PagerSavepoin
120cd 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 6f t.iOffset and co
120ce 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 ntinuing to the
120cf 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 next journal hea
120d0 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20 der.. ** There
120d1 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 64 73 might be records
120d2 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 in the main jou
120d3 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 20 61 rnal that have a
120d4 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a page number. *
120d5 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 * greater than t
120d6 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62 he current datab
120d7 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 65 72 ase size (pPager
120d8 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 74 68 ->dbSize) but th
120d9 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 ose. ** will be
120da 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d 61 74 skipped automat
120db 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 20 61 ically. Pages a
120dc 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 6f 6e re added to pDon
120dd 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a 20 61 e as they. ** a
120de 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a re played back..
120df 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 */. if( pSave
120e0 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 48 64 point ){. iHd
120e1 72 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e rOff = pSavepoin
120e2 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3f 20 t->iHdrOffset ?
120e3 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 pSavepoint->iHdr
120e4 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 20 Offset : szJ;.
120e5 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
120e6 6c 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e lOff = pSavepoin
120e7 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 t->iOffset;.
120e8 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 while( rc==SQLIT
120e9 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e E_OK && pPager->
120ea 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f journalOff<iHdrO
120eb 66 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ff ){. rc =
120ec 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
120ed 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c one_page(pPager,
120ee 20 31 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 1, &pPager->jou
120ef 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e rnalOff, 1, pDon
120f0 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 e);. assert
120f1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e ( rc!=SQLITE_DON
120f2 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c E );. }. }el
120f3 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e se{. pPager->
120f4 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
120f5 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69 6e }.. /* Contin
120f6 75 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 ue rolling back
120f7 72 65 63 6f 72 64 73 20 6f 75 74 20 6f 66 20 74 records out of t
120f8 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
120f9 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a starting at. **
120fa 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e the first journ
120fb 61 6c 20 68 65 61 64 65 72 20 73 65 65 6e 20 61 al header seen a
120fc 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 75 6e nd continuing un
120fd 74 69 6c 20 74 68 65 20 65 66 66 65 63 74 69 76 til the effectiv
120fe 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 e end. ** of th
120ff 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 e main journal f
12100 69 6c 65 2e 20 20 43 6f 6e 74 69 6e 75 65 20 74 ile. Continue t
12101 6f 20 73 6b 69 70 20 6f 75 74 2d 6f 66 2d 72 61 o skip out-of-ra
12102 6e 67 65 20 70 61 67 65 73 20 61 6e 64 0a 20 20 nge pages and.
12103 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 61 64 64 69 ** continue addi
12104 6e 67 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 ng pages rolled
12105 62 61 63 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a 20 back to pDone..
12106 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72 63 3d */. while( rc=
12107 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 =SQLITE_OK && pP
12108 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
12109 3c 73 7a 4a 20 29 7b 0a 20 20 20 20 75 33 32 20 <szJ ){. u32
1210a 6e 4a 52 65 63 20 3d 20 30 3b 20 20 20 20 20 2f nJRec = 0; /
1210b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f 75 72 * Number of Jour
1210c 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 nal Records */.
1210d 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 u32 dummy;.
1210e 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e rc = readJourn
1210f 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 73 7a alHdr(pPager, sz
12110 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d 6d J, &nJRec, &dumm
12111 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 y);. assert(
12112 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc!=SQLITE_DONE
12113 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a );.. /*. *
12114 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d 3e 6a * The "pPager->j
12115 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 ournalHdr+JOURNA
12116 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
12117 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ==pPager->journa
12118 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 73 lOff". ** tes
12119 74 20 69 73 20 72 65 6c 61 74 65 64 20 74 6f 20 t is related to
1211a 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20 20 53 ticket #2565. S
1211b 65 65 20 74 68 65 20 64 69 73 63 75 73 73 69 6f ee the discussio
1211c 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 n in the. **
1211d 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 pager_playback()
1211e 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 64 function for ad
1211f 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
12120 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tion.. */.
12121 20 61 73 73 65 72 74 28 20 21 28 6e 4a 52 65 63 assert( !(nJRec
12122 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 ==0. &&
12123 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
12124 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 dr+JOURNAL_HDR_S
12125 5a 28 70 50 61 67 65 72 29 21 3d 70 50 61 67 65 Z(pPager)!=pPage
12126 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 r->journalOff.
12127 20 20 20 20 20 20 20 26 26 20 28 28 73 7a 4a 20 && ((szJ
12128 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 - pPager->journa
12129 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f lOff) / JOURNAL_
1212a 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3e 30 PG_SZ(pPager))>0
1212b 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 61 67 . && pag
1212c 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c 50 61 67 erNextJournalPag
1212d 65 49 73 56 61 6c 69 64 28 70 50 61 67 65 72 29 eIsValid(pPager)
1212e 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 ). );. if(
1212f 20 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 20 20 20 nJRec==0 .
12130 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
12131 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 alHdr+JOURNAL_HD
12132 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 R_SZ(pPager)==pP
12133 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
12134 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 4a . ){. nJ
12135 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 70 50 61 Rec = (szJ - pPa
12136 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 ger->journalOff)
12137 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 /JOURNAL_PG_SZ(p
12138 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 Pager);. }.
12139 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d for(ii=0; rc==
1213a 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c SQLITE_OK && ii<
1213b 6e 4a 52 65 63 20 26 26 20 70 50 61 67 65 72 2d nJRec && pPager-
1213c 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b >journalOff<szJ;
1213d 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 ii++){. rc
1213e 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 = pager_playbac
1213f 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 k_one_page(pPage
12140 72 2c 20 31 2c 20 26 70 50 61 67 65 72 2d 3e 6a r, 1, &pPager->j
12141 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 ournalOff, 1, pD
12142 6f 6e 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 one);. asse
12143 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 rt( rc!=SQLITE_D
12144 4f 4e 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ONE );. }. }
12145 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 . assert( rc!=S
12146 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 QLITE_OK || pPag
12147 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d er->journalOff==
12148 73 7a 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e szJ );.. /* Fin
12149 61 6c 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 ally, rollback
1214a 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73 pages from the s
1214b 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 ub-journal. Pag
1214c 65 20 74 68 61 74 20 77 65 72 65 0a 20 20 2a 2a e that were. **
1214d 20 70 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c previously roll
1214e 65 64 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 ed back out of t
1214f 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
12150 28 61 6e 64 20 61 72 65 20 68 65 6e 63 65 20 69 (and are hence i
12151 6e 20 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 n pDone). ** wi
12152 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 2e 20 20 ll be skipped.
12153 4f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 Out-of-range pag
12154 65 73 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 es are also skip
12155 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ped.. */. if(
12156 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 pSavepoint ){.
12157 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 70 i64 offset = p
12158 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 Savepoint->iSubR
12159 65 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 ec*(4+pPager->pa
1215a 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 66 6f 72 geSize);. for
1215b 28 69 69 3d 70 53 61 76 65 70 6f 69 6e 74 2d 3e (ii=pSavepoint->
1215c 69 53 75 62 52 65 63 3b 20 72 63 3d 3d 53 51 4c iSubRec; rc==SQL
1215d 49 54 45 5f 4f 4b 26 26 69 69 3c 28 75 33 32 29 ITE_OK&&ii<(u32)
1215e 70 50 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63 pPager->stmtNRec
1215f 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 ; ii++){. a
12160 73 73 65 72 74 28 20 6f 66 66 73 65 74 20 3d 3d ssert( offset ==
12161 20 69 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 ii*(4+pPager->p
12162 61 67 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 20 ageSize) );.
12163 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 rc = pager_pla
12164 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 yback_one_page(p
12165 50 61 67 65 72 2c 20 30 2c 20 26 6f 66 66 73 65 Pager, 0, &offse
12166 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 t, 1, pDone);.
12167 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d assert( rc!=
12168 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 SQLITE_DONE );.
12169 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 }. }.. sqli
1216a 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
1216b 28 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20 72 (pDone);. if( r
1216c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1216d 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
1216e 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 nalOff = szJ;.
1216f 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
12170 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
12171 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
12172 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 r of in-memory p
12173 61 67 65 73 20 74 68 61 74 20 61 72 65 20 61 6c ages that are al
12174 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 lowed..*/.SQLITE
12175 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
12176 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 lite3PagerSetCac
12177 68 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 hesize(Pager *pP
12178 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 ager, int mxPage
12179 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 ){. sqlite3Pcac
1217a 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 heSetCachesize(p
1217b 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 Pager->pPCache,
1217c 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a mxPage);.}../*.*
1217d 2a 20 41 64 6a 75 73 74 20 74 68 65 20 72 6f 62 * Adjust the rob
1217e 75 73 74 6e 65 73 73 20 6f 66 20 74 68 65 20 64 ustness of the d
1217f 61 74 61 62 61 73 65 20 74 6f 20 64 61 6d 61 67 atabase to damag
12180 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 e due to OS cras
12181 68 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 hes.** or power
12182 66 61 69 6c 75 72 65 73 20 62 79 20 63 68 61 6e failures by chan
12183 67 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 ging the number
12184 6f 66 20 73 79 6e 63 73 28 29 73 20 77 68 65 6e of syncs()s when
12185 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 writing.** the
12186 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
12187 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 68 72 . There are thr
12188 65 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a ee levels:.**.**
12189 20 20 20 20 4f 46 46 20 20 20 20 20 20 20 73 71 OFF sq
1218a 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 20 69 73 lite3OsSync() is
1218b 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 never called.
1218c 54 68 69 73 20 69 73 20 74 68 65 20 64 65 66 61 This is the defa
1218d 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ult.**
1218e 20 20 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 for temporar
1218f 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 y and transient
12190 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 files..**.**
12191 4e 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f NORMAL The jo
12192 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 urnal is synced
12193 6f 6e 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 once before writ
12194 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a es begin on the.
12195 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
12196 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 database. This
12197 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 is normally adeq
12198 75 61 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c uate protection,
12199 20 62 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 but.**
1219a 20 20 20 20 20 69 74 20 69 73 20 74 68 65 6f 72 it is theor
1219b 65 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c etically possibl
1219c 65 2c 20 74 68 6f 75 67 68 20 76 65 72 79 20 75 e, though very u
1219d 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 nlikely,.**
1219e 20 20 20 20 20 20 20 20 20 74 68 61 74 20 61 6e that an
1219f 20 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 inopertune powe
121a0 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 r failure could
121a1 6c 65 61 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 leave the journa
121a2 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 l.**
121a3 20 20 69 6e 20 61 20 73 74 61 74 65 20 77 68 69 in a state whi
121a4 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 64 ch would cause d
121a5 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 amage to the dat
121a6 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 abase.**
121a7 20 20 20 20 20 20 77 68 65 6e 20 69 74 20 69 73 when it is
121a8 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a rolled back..**
121a9 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 20 .** FULL
121aa 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 The journal is
121ab 73 79 6e 63 65 64 20 74 77 69 63 65 20 62 65 66 synced twice bef
121ac 6f 72 65 20 77 72 69 74 65 73 20 62 65 67 69 6e ore writes begin
121ad 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 on the.**
121ae 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 database
121af 20 28 77 69 74 68 20 73 6f 6d 65 20 61 64 64 69 (with some addi
121b0 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
121b1 6f 6e 20 2d 20 74 68 65 20 6e 52 65 63 20 66 69 on - the nRec fi
121b2 65 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 eld.**
121b3 20 20 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e of the journ
121b4 61 6c 20 68 65 61 64 65 72 20 2d 20 62 65 69 6e al header - bein
121b5 67 20 77 72 69 74 74 65 6e 20 69 6e 20 62 65 74 g written in bet
121b6 77 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 ween the two.**
121b7 20 20 20 20 20 20 20 20 20 20 20 20 20 73 79 6e syn
121b8 63 73 29 2e 20 20 49 66 20 77 65 20 61 73 73 75 cs). If we assu
121b9 6d 65 20 74 68 61 74 20 77 72 69 74 69 6e 67 20 me that writing
121ba 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 a.**
121bb 20 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 single disk se
121bc 63 74 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 ctor is atomic,
121bd 74 68 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 then this mode p
121be 72 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 rovides.**
121bf 20 20 20 20 20 20 20 20 61 73 73 75 72 61 6e 63 assuranc
121c0 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e e that the journ
121c1 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 al will not be c
121c2 6f 72 72 75 70 74 65 64 20 74 6f 20 74 68 65 0a orrupted to the.
121c3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
121c4 70 6f 69 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 point of causing
121c5 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 damage to the d
121c6 61 74 61 62 61 73 65 20 64 75 72 69 6e 67 20 72 atabase during r
121c7 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e ollback..**.** N
121c8 75 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 73 umeric values as
121c9 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
121ca 65 73 65 20 73 74 61 74 65 73 20 61 72 65 20 4f ese states are O
121cb 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c FF==1, NORMAL=2,
121cc 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a .** and FULL=3..
121cd 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
121ce 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 E_OMIT_PAGER_PRA
121cf 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 GMAS.SQLITE_PRIV
121d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
121d1 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 PagerSetSafetyLe
121d2 76 65 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 vel(Pager *pPage
121d3 72 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e r, int level, in
121d4 74 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 t bFullFsync){.
121d5 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 pPager->noSync
121d6 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 = (level==1 ||
121d7 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
121d8 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 ) ?1:0;. pPager
121d9 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c 65 ->fullSync = (le
121da 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65 vel==3 && !pPage
121db 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a r->tempFile) ?1:
121dc 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 0;. pPager->syn
121dd 63 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c c_flags = (bFull
121de 46 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e Fsync?SQLITE_SYN
121df 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 C_FULL:SQLITE_SY
121e0 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 NC_NORMAL);. if
121e1 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 ( pPager->noSync
121e2 20 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 ) pPager->needS
121e3 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 ync = 0;.}.#endi
121e4 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c f../*.** The fol
121e5 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 lowing global va
121e6 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d riable is increm
121e7 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 ented whenever t
121e8 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 he library.** at
121e9 74 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 tempts to open a
121ea 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e temporary file.
121eb 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 This informati
121ec 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a on is used for.*
121ed 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e * testing and an
121ee 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a alysis only. .*
121ef 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
121f0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
121f1 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e int sqlite3_open
121f2 74 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a temp_count = 0;.
121f3 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 #endif../*.** Op
121f4 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 en a temporary f
121f5 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 72 69 74 ile. .**.** Writ
121f6 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 e the file descr
121f7 69 70 74 6f 72 20 69 6e 74 6f 20 2a 66 64 2e 20 iptor into *fd.
121f8 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
121f9 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 K on success or
121fa 73 6f 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 65 72 some.** other er
121fb 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 66 ror code if we f
121fc 61 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c ail. The OS will
121fd 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 automatically d
121fe 65 6c 65 74 65 20 74 68 65 20 74 65 6d 70 6f 72 elete the tempor
121ff 61 72 79 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e ary.** file when
12200 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a it is closed..*
12201 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c /.static int sql
12202 69 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d ite3PagerOpentem
12203 70 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 p(. Pager *pPag
12204 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 er, /* Th
12205 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 2a e pager object *
12206 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
12207 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 *pFile, /* Wri
12208 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 te the file desc
12209 72 69 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 riptor here */.
1220a 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 int vfsFlags
1220b 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
1220c 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 passed through t
1220d 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a o the VFS */.){.
1220e 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 64 65 int rc;..#ifde
1220f 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
12210 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 sqlite3_opentemp
12211 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 _count++; /* Us
12212 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 ed for testing a
12213 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 nd analysis only
12214 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66 */.#endif.. vf
12215 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 54 sFlags |= SQLIT
12216 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
12217 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 | SQLITE_OPEN_C
12218 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 REATE |.
12219 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
1221a 45 58 43 4c 55 53 49 56 45 20 7c 20 53 51 4c 49 EXCLUSIVE | SQLI
1221b 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
1221c 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 71 CLOSE;. rc = sq
1221d 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 lite3OsOpen(pPag
1221e 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 er->pVfs, 0, pFi
1221f 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 29 le, vfsFlags, 0)
12220 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d ;. assert( rc!=
12221 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 46 69 SQLITE_OK || pFi
12222 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 20 29 3b 0a le->pMethods );.
12223 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
12224 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
12225 53 74 72 65 73 73 28 76 6f 69 64 20 2a 2c 50 67 Stress(void *,Pg
12226 48 64 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 Hdr *);../*.** C
12227 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 reate a new page
12228 20 63 61 63 68 65 20 61 6e 64 20 70 75 74 20 61 cache and put a
12229 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
1222a 70 61 67 65 20 63 61 63 68 65 20 69 6e 20 2a 70 page cache in *p
1222b 70 50 61 67 65 72 2e 0a 2a 2a 20 54 68 65 20 66 pPager..** The f
1222c 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64 ile to be cached
1222d 20 6e 65 65 64 20 6e 6f 74 20 65 78 69 73 74 2e need not exist.
1222e 20 20 54 68 65 20 66 69 6c 65 20 69 73 20 6e 6f The file is no
1222f 74 20 6c 6f 63 6b 65 64 20 75 6e 74 69 6c 0a 2a t locked until.*
12230 2a 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c * the first call
12231 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 to sqlite3Pager
12232 47 65 74 28 29 20 61 6e 64 20 69 73 20 6f 6e 6c Get() and is onl
12233 79 20 68 65 6c 64 20 6f 70 65 6e 20 75 6e 74 69 y held open unti
12234 6c 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61 l the.** last pa
12235 67 65 20 69 73 20 72 65 6c 65 61 73 65 64 20 75 ge is released u
12236 73 69 6e 67 20 73 71 6c 69 74 65 33 50 61 67 65 sing sqlite3Page
12237 72 55 6e 72 65 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 rUnref()..**.**
12238 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 If zFilename is
12239 4e 55 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 NULL then a rand
1223a 6f 6d 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f omly-named tempo
1223b 72 61 72 79 20 66 69 6c 65 20 69 73 20 63 72 65 rary file is cre
1223c 61 74 65 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 ated.** and used
1223d 20 61 73 20 74 68 65 20 66 69 6c 65 20 74 6f 20 as the file to
1223e 62 65 20 63 61 63 68 65 64 2e 20 20 54 68 65 20 be cached. The
1223f 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c file will be del
12240 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 eted.** automati
12241 63 61 6c 6c 79 20 77 68 65 6e 20 69 74 20 69 73 cally when it is
12242 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 closed..**.** I
12243 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 f zFilename is "
12244 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 :memory:" then a
12245 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 ll information i
12246 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 2e s held in cache.
12247 0a 2a 2a 20 49 74 20 69 73 20 6e 65 76 65 72 20 .** It is never
12248 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e written to disk.
12249 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 This can be us
1224a 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
1224b 61 6e 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 an.** in-memory
1224c 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c database..*/.SQL
1224d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1224e 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
1224f 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
12250 2a 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 *pVfs, /*
12251 54 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 The virtual file
12252 20 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a system to use *
12253 2f 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 /. Pager **ppPa
12254 67 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ger, /*
12255 52 65 74 75 72 6e 20 74 68 65 20 50 61 67 65 72 Return the Pager
12256 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 structure here
12257 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
12258 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a *zFilename, /*
12259 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 Name of the dat
1225a 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 abase file to op
1225b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 en */. int nExt
1225c 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ra,
1225d 20 2f 2a 20 45 78 74 72 61 20 62 79 74 65 73 20 /* Extra bytes
1225e 61 70 70 65 6e 64 20 74 6f 20 65 61 63 68 20 69 append to each i
1225f 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f n-memory page */
12260 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
12261 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 /* f
12262 6c 61 67 73 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 lags controlling
12263 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 this file */.
12264 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 int vfsFlags
12265 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 /* flag
12266 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 s passed through
12267 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e to sqlite3_vfs.
12268 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20 xOpen() */.){.
12269 75 38 20 2a 70 50 74 72 3b 0a 20 20 50 61 67 65 u8 *pPtr;. Page
1226a 72 20 2a 70 50 61 67 65 72 20 3d 20 30 3b 0a 20 r *pPager = 0;.
1226b 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1226c 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 _OK;. int i;.
1226d 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20 30 int tempFile = 0
1226e 3b 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20 ;. int memDb =
1226f 30 3b 0a 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 0;. int readOnl
12270 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 75 73 65 y = 0;. int use
12271 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73 Journal = (flags
12272 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f & PAGER_OMIT_JO
12273 55 52 4e 41 4c 29 3d 3d 30 3b 0a 20 20 69 6e 74 URNAL)==0;. int
12274 20 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 noReadlock = (f
12275 6c 61 67 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f lags & PAGER_NO_
12276 52 45 41 44 4c 4f 43 4b 29 21 3d 30 3b 0a 20 20 READLOCK)!=0;.
12277 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 int journalFileS
12278 69 7a 65 3b 0a 20 20 69 6e 74 20 70 63 61 63 68 ize;. int pcach
12279 65 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 eSize = sqlite3P
1227a 63 61 63 68 65 53 69 7a 65 28 29 3b 0a 20 20 69 cacheSize();. i
1227b 6e 74 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 nt szPageDflt =
1227c 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 SQLITE_DEFAULT_P
1227d 41 47 45 5f 53 49 5a 45 3b 0a 20 20 63 68 61 72 AGE_SIZE;. char
1227e 20 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b *zPathname = 0;
1227f 0a 20 20 69 6e 74 20 6e 50 61 74 68 6e 61 6d 65 . int nPathname
12280 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 73 71 6c = 0;.. if( sql
12281 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 ite3JournalSize(
12282 70 56 66 73 29 3e 73 71 6c 69 74 65 33 4d 65 6d pVfs)>sqlite3Mem
12283 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 20 29 7b JournalSize() ){
12284 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 . journalFile
12285 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4a 6f Size = sqlite3Jo
12286 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 3b urnalSize(pVfs);
12287 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6a 6f . }else{. jo
12288 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20 urnalFileSize =
12289 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
1228a 6c 53 69 7a 65 28 29 3b 0a 20 20 7d 0a 0a 20 20 lSize();. }..
1228b 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 72 /* The default r
1228c 65 74 75 72 6e 20 69 73 20 61 20 4e 55 4c 4c 20 eturn is a NULL
1228d 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 2a 70 70 pointer */. *pp
1228e 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a Pager = 0;.. /*
1228f 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f Compute and sto
12290 72 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 re the full path
12291 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 name in an alloc
12292 61 74 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e ated buffer poin
12293 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a ted. ** to by z
12294 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 Pathname, length
12295 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 nPathname. Or,
12296 69 66 20 74 68 69 73 20 69 73 20 61 20 74 65 6d if this is a tem
12297 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a porary file,. *
12298 2a 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61 * leave both nPa
12299 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68 thname and zPath
1229a 6e 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 name set to 0..
1229b 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e */. if( zFilen
1229c 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 ame && zFilename
1229d 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 68 [0] ){. nPath
1229e 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 name = pVfs->mxP
1229f 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a athname+1;. z
122a0 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 Pathname = sqlit
122a1 65 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61 e3Malloc(nPathna
122a2 6d 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 7a me*2);. if( z
122a3 50 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 Pathname==0 ){.
122a4 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
122a5 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
122a6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
122a7 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 MIT_MEMORYDB.
122a8 20 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69 6c if( strcmp(zFil
122a9 65 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a 22 ename,":memory:"
122aa 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 )==0 ){. me
122ab 6d 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a mDb = 1;. z
122ac 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b Pathname[0] = 0;
122ad 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 . }else.#endi
122ae 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 72 63 f. {. rc
122af 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c = sqlite3OsFull
122b0 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a Pathname(pVfs, z
122b1 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68 6e Filename, nPathn
122b2 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29 3b ame, zPathname);
122b3 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
122b4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
122b5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
122b6 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 ee(zPathname);.
122b7 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
122b8 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 74 68 6e }. nPathn
122b9 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 ame = sqlite3Str
122ba 6c 65 6e 33 30 28 7a 50 61 74 68 6e 61 6d 65 29 len30(zPathname)
122bb 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f ;. }.. /* Allo
122bc 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 cate memory for
122bd 74 68 65 20 70 61 67 65 72 20 73 74 72 75 63 74 the pager struct
122be 75 72 65 20 2a 2f 0a 20 20 70 50 61 67 65 72 20 ure */. pPager
122bf 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a = sqlite3MallocZ
122c0 65 72 6f 28 0a 20 20 20 20 73 69 7a 65 6f 66 28 ero(. sizeof(
122c1 2a 70 50 61 67 65 72 29 20 2b 20 20 20 20 20 20 *pPager) +
122c2 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 73 74 /* Pager st
122c3 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 70 ructure */. p
122c4 63 61 63 68 65 53 69 7a 65 20 20 20 20 20 20 2b cacheSize +
122c5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43 /* PC
122c6 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 ache object */.
122c7 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 journalFileSi
122c8 7a 65 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f ze + /
122c9 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * The journal fi
122ca 6c 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 20 le structure */
122cb 0a 20 20 20 20 70 56 66 73 2d 3e 73 7a 4f 73 46 . pVfs->szOsF
122cc 69 6c 65 20 20 2b 20 20 20 20 20 20 20 20 20 20 ile +
122cd 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20 /* The main db
122ce 66 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 file */. jour
122cf 6e 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 nalFileSize * 2
122d0 2b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 + /* The t
122d1 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 wo journal files
122d2 20 2a 2f 20 0a 20 20 20 20 33 2a 6e 50 61 74 68 */ . 3*nPath
122d3 6e 61 6d 65 20 2b 20 34 30 20 20 20 20 20 20 20 name + 40
122d4 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d /* zFilenam
122d5 65 2c 20 7a 44 69 72 65 63 74 6f 72 79 2c 20 7a e, zDirectory, z
122d6 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a Journal */. );.
122d7 20 20 69 66 28 20 21 70 50 61 67 65 72 20 29 7b if( !pPager ){
122d8 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
122d9 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 e(zPathname);.
122da 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
122db 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 61 NOMEM;. }. pPa
122dc 67 65 72 2d 3e 70 50 43 61 63 68 65 20 3d 20 28 ger->pPCache = (
122dd 50 43 61 63 68 65 20 2a 29 26 70 50 61 67 65 72 PCache *)&pPager
122de 5b 31 5d 3b 0a 20 20 70 50 74 72 20 3d 20 28 28 [1];. pPtr = ((
122df 75 38 20 2a 29 26 70 50 61 67 65 72 5b 31 5d 29 u8 *)&pPager[1])
122e0 20 2b 20 70 63 61 63 68 65 53 69 7a 65 3b 0a 20 + pcacheSize;.
122e1 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 pPager->vfsFlag
122e2 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a 20 20 s = vfsFlags;.
122e3 70 50 61 67 65 72 2d 3e 66 64 20 3d 20 28 73 71 pPager->fd = (sq
122e4 6c 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50 74 lite3_file*)&pPt
122e5 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 r[pVfs->szOsFile
122e6 2a 30 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 *0];. pPager->s
122e7 6a 66 64 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 jfd = (sqlite3_f
122e8 69 6c 65 2a 29 26 70 50 74 72 5b 70 56 66 73 2d ile*)&pPtr[pVfs-
122e9 3e 73 7a 4f 73 46 69 6c 65 5d 3b 0a 20 20 70 50 >szOsFile];. pP
122ea 61 67 65 72 2d 3e 6a 66 64 20 3d 20 28 73 71 6c ager->jfd = (sql
122eb 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50 74 72 ite3_file*)&pPtr
122ec 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 2b [pVfs->szOsFile+
122ed 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 5d journalFileSize]
122ee 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c ;. pPager->zFil
122ef 65 6e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 ename = (char*)&
122f0 70 50 74 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 pPtr[pVfs->szOsF
122f1 69 6c 65 2b 32 2a 6a 6f 75 72 6e 61 6c 46 69 6c ile+2*journalFil
122f2 65 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67 65 72 eSize];. pPager
122f3 2d 3e 7a 44 69 72 65 63 74 6f 72 79 20 3d 20 26 ->zDirectory = &
122f4 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
122f5 65 5b 6e 50 61 74 68 6e 61 6d 65 2b 31 5d 3b 0a e[nPathname+1];.
122f6 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e pPager->zJourn
122f7 61 6c 20 3d 20 26 70 50 61 67 65 72 2d 3e 7a 44 al = &pPager->zD
122f8 69 72 65 63 74 6f 72 79 5b 6e 50 61 74 68 6e 61 irectory[nPathna
122f9 6d 65 2b 31 5d 3b 0a 20 20 70 50 61 67 65 72 2d me+1];. pPager-
122fa 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 >pVfs = pVfs;.
122fb 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b if( zPathname ){
122fc 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 . memcpy(pPag
122fd 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a er->zFilename, z
122fe 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e Pathname, nPathn
122ff 61 6d 65 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 ame+1);. sqli
12300 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 te3_free(zPathna
12301 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f me);. }.. /* O
12302 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 pen the pager fi
12303 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a le.. */. if( z
12304 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c Filename && zFil
12305 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 6d ename[0] && !mem
12306 44 62 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 50 Db ){. if( nP
12307 61 74 68 6e 61 6d 65 3e 28 70 56 66 73 2d 3e 6d athname>(pVfs->m
12308 78 50 61 74 68 6e 61 6d 65 20 2d 20 28 69 6e 74 xPathname - (int
12309 29 73 69 7a 65 6f 66 28 22 2d 6a 6f 75 72 6e 61 )sizeof("-journa
1230a 6c 22 29 29 20 29 7b 0a 20 20 20 20 20 20 72 63 l")) ){. rc
1230b 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 = SQLITE_CANTOP
1230c 45 4e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 EN;. }else{.
1230d 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 int fout =
1230e 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 0;. rc = sq
1230f 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 lite3OsOpen(pVfs
12310 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e , pPager->zFilen
12311 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c ame, pPager->fd,
12312 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
12313 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
12314 2d 3e 76 66 73 46 6c 61 67 73 2c 20 26 66 6f 75 ->vfsFlags, &fou
12315 74 29 3b 0a 20 20 20 20 20 20 72 65 61 64 4f 6e t);. readOn
12316 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 54 ly = (fout&SQLIT
12317 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 E_OPEN_READONLY)
12318 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 ;.. /* If t
12319 68 65 20 66 69 6c 65 20 77 61 73 20 73 75 63 63 he file was succ
1231a 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 essfully opened
1231b 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 for read/write a
1231c 63 63 65 73 73 2c 0a 20 20 20 20 20 20 2a 2a 20 ccess,. **
1231d 63 68 6f 6f 73 65 20 61 20 64 65 66 61 75 6c 74 choose a default
1231e 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 63 61 page size in ca
1231f 73 65 20 77 65 20 68 61 76 65 20 74 6f 20 63 72 se we have to cr
12320 65 61 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a eate the. *
12321 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e * database file.
12322 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 The default pag
12323 65 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d 61 e size is the ma
12324 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 20 20 ximum of:.
12325 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 2b **. ** +
12326 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
12327 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20 20 PAGE_SIZE,.
12328 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61 6c ** + The val
12329 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ue returned by s
1232a 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 qlite3OsSectorSi
1232b 7a 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 20 20 ze(). **
1232c 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20 70 + The largest p
1232d 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 63 61 age size that ca
1232e 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f n be written ato
1232f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 20 20 2a mically.. *
12330 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d /. if( rc==
12331 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 65 SQLITE_OK && !re
12332 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 adOnly ){.
12333 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 setSectorSize(
12334 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 pPager);.
12335 20 69 66 28 20 73 7a 50 61 67 65 44 66 6c 74 3c if( szPageDflt<
12336 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
12337 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ze ){.
12338 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 70 50 61 szPageDflt = pPa
12339 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b ger->sectorSize;
1233a 0a 20 20 20 20 20 20 20 20 7d 0a 23 69 66 64 65 . }.#ifde
1233b 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
1233c 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 ATOMIC_WRITE.
1233d 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 {.
1233e 20 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 int iDc = sqlit
1233f 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 e3OsDeviceCharac
12340 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65 72 teristics(pPager
12341 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 ->fd);.
12342 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 int ii;.
12343 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 assert(SQLITE
12344 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 _IOCAP_ATOMIC512
12345 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 ==(512>>8));.
12346 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 assert(SQ
12347 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
12348 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 C64K==(65536>>8)
12349 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 );. ass
1234a 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 ert(SQLITE_MAX_D
1234b 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
1234c 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20 <=65536);.
1234d 20 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 for(ii=szPag
1234e 65 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 eDflt; ii<=SQLIT
1234f 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 E_MAX_DEFAULT_PA
12350 47 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 GE_SIZE; ii=ii*2
12351 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
12352 66 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 f( iDc&(SQLITE_I
12353 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e OCAP_ATOMIC|(ii>
12354 3e 38 29 29 20 29 20 73 7a 50 61 67 65 44 66 6c >8)) ) szPageDfl
12355 74 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 t = ii;.
12356 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 }. }.#e
12357 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66 28 ndif. if(
12358 20 73 7a 50 61 67 65 44 66 6c 74 3e 53 51 4c 49 szPageDflt>SQLI
12359 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 TE_MAX_DEFAULT_P
1235a 41 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 AGE_SIZE ){.
1235b 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 szPageDflt
1235c 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 = SQLITE_MAX_DE
1235d 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b FAULT_PAGE_SIZE;
1235e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1235f 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 }. }. }else
12360 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65 {. /* If a te
12361 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20 mporary file is
12362 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 requested, it is
12363 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65 not opened imme
12364 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 diately.. **
12365 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 In this case we
12366 61 63 63 65 70 74 20 74 68 65 20 64 65 66 61 75 accept the defau
12367 6c 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 lt page size and
12368 20 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a delay actually.
12369 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 ** opening t
1236a 68 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68 he file until th
1236b 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 e first call to
1236c 4f 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a OsWrite().. *
1236d 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72 *. ** This br
1236e 61 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e anch is also run
1236f 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 for an in-memor
12370 79 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69 y database. An i
12371 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 n-memory. **
12372 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20 database is the
12373 73 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66 same as a temp-f
12374 69 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76 65 ile that is neve
12375 72 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f r written out to
12376 0a 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64 . ** disk and
12377 20 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f uses an in-memo
12378 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 ry rollback jour
12379 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 nal.. */ .
1237a 20 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20 tempFile = 1;.
1237b 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 pPager->state
1237c 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 = PAGER_EXCLUSI
1237d 56 45 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 VE;. }.. if( p
1237e 50 61 67 65 72 20 26 26 20 72 63 3d 3d 53 51 4c Pager && rc==SQL
1237f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 ITE_OK ){. pP
12380 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 ager->pTmpSpace
12381 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c = sqlite3PageMal
12382 6c 6f 63 28 73 7a 50 61 67 65 44 66 6c 74 29 3b loc(szPageDflt);
12383 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e . }.. /* If an
12384 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 69 error occured i
12385 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 n either of the
12386 62 6c 6f 63 6b 73 20 61 62 6f 76 65 2e 0a 20 20 blocks above..
12387 2a 2a 20 46 72 65 65 20 74 68 65 20 50 61 67 65 ** Free the Page
12388 72 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 r structure and
12389 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a close the file..
1238a 20 20 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 70 ** Since the p
1238b 61 67 65 72 20 69 73 20 6e 6f 74 20 61 6c 6c 6f ager is not allo
1238c 63 61 74 65 64 20 74 68 65 72 65 20 69 73 20 6e cated there is n
1238d 6f 20 6e 65 65 64 20 74 6f 20 73 65 74 20 0a 20 o need to set .
1238e 20 2a 2a 20 61 6e 79 20 50 61 67 65 72 2e 65 72 ** any Pager.er
1238f 72 4d 61 73 6b 20 76 61 72 69 61 62 6c 65 73 2e rMask variables.
12390 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 . */. if( !pPa
12391 67 65 72 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e ger || !pPager->
12392 70 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 pTmpSpace ){.
12393 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
12394 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 pPager->fd);.
12395 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 sqlite3_free(pP
12396 61 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 ager);. retur
12397 6e 20 28 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f n ((rc==SQLITE_O
12398 4b 29 3f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3a K)?SQLITE_NOMEM:
12399 72 63 29 3b 0a 20 20 7d 0a 20 20 6e 45 78 74 72 rc);. }. nExtr
1239a 61 20 3d 20 46 4f 52 43 45 5f 41 4c 49 47 4e 4d a = FORCE_ALIGNM
1239b 45 4e 54 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 ENT(nExtra);. s
1239c 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e qlite3PcacheOpen
1239d 28 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 (szPageDflt, nEx
1239e 74 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 tra, !memDb,.
1239f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
123a0 20 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 72 !memDb?pagerStr
123a1 65 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 ess:0, (void *)p
123a2 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 Pager, pPager->p
123a3 50 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 PCache);.. PAGE
123a4 52 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 64 RTRACE(("OPEN %d
123a5 20 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 %s\n", FILEHAND
123a6 4c 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29 LEID(pPager->fd)
123a7 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e , pPager->zFilen
123a8 61 6d 65 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 ame));. IOTRACE
123a9 28 28 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 (("OPEN %p %s\n"
123aa 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 , pPager, pPager
123ab 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 ->zFilename))..
123ac 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 50 61 67 65 /* Fill in Page
123ad 72 2e 7a 44 69 72 65 63 74 6f 72 79 5b 5d 20 2a r.zDirectory[] *
123ae 2f 0a 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 /. memcpy(pPage
123af 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 2c 20 70 r->zDirectory, p
123b0 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 Pager->zFilename
123b1 2c 20 6e 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a , nPathname+1);.
123b2 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33 53 for(i=sqlite3S
123b3 74 72 6c 65 6e 33 30 28 70 50 61 67 65 72 2d 3e trlen30(pPager->
123b4 7a 44 69 72 65 63 74 6f 72 79 29 3b 20 0a 20 20 zDirectory); .
123b5 20 20 20 20 69 3e 30 20 26 26 20 70 50 61 67 65 i>0 && pPage
123b6 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d r->zDirectory[i-
123b7 31 5d 21 3d 27 2f 27 3b 20 69 2d 2d 29 7b 7d 0a 1]!='/'; i--){}.
123b8 20 20 69 66 28 20 69 3e 30 20 29 20 70 50 61 67 if( i>0 ) pPag
123b9 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 er->zDirectory[i
123ba 2d 31 5d 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 46 -1] = 0;.. /* F
123bb 69 6c 6c 20 69 6e 20 50 61 67 65 72 2e 7a 4a 6f ill in Pager.zJo
123bc 75 72 6e 61 6c 5b 5d 20 2a 2f 0a 20 20 69 66 28 urnal[] */. if(
123bd 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 zPathname ){.
123be 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d memcpy(pPager-
123bf 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 >zJournal, pPage
123c0 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 r->zFilename, nP
123c1 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 athname);. me
123c2 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a mcpy(&pPager->zJ
123c3 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 ournal[nPathname
123c4 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 20 39 ], "-journal", 9
123c5 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
123c6 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
123c7 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 0;. }.. /*
123c8 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
123c9 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 pen = 0; */. pP
123ca 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c ager->useJournal
123cb 20 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 6e 61 = (u8)useJourna
123cc 6c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 52 l;. pPager->noR
123cd 65 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 65 61 eadlock = (noRea
123ce 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e 6c dlock && readOnl
123cf 79 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 70 50 y) ?1:0;. /* pP
123d0 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d ager->stmtOpen =
123d1 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
123d2 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20 er->stmtInUse =
123d3 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 0; */. /* pPage
123d4 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a 2f 0a r->nRef = 0; */.
123d5 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
123d6 56 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 6d 44 Valid = (u8)memD
123d7 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 61 67 b;. pPager->pag
123d8 65 53 69 7a 65 20 3d 20 73 7a 50 61 67 65 44 66 eSize = szPageDf
123d9 6c 74 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d lt;. /* pPager-
123da 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a >stmtSize = 0; *
123db 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 /. /* pPager->s
123dc 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f tmtJSize = 0; */
123dd 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50 . /* pPager->nP
123de 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 age = 0; */. pP
123df 61 67 65 72 2d 3e 6d 78 50 61 67 65 20 3d 20 31 ager->mxPage = 1
123e0 30 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78 00;. pPager->mx
123e1 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41 Pgno = SQLITE_MA
123e2 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 X_PAGE_COUNT;.
123e3 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 /* pPager->state
123e4 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b = PAGER_UNLOCK;
123e5 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
123e6 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28 ager->state == (
123e7 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 52 tempFile ? PAGER
123e8 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 47 _EXCLUSIVE : PAG
123e9 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20 ER_UNLOCK) );.
123ea 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 4d 61 /* pPager->errMa
123eb 73 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 sk = 0; */. pPa
123ec 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3d 20 ger->tempFile =
123ed 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 0a 20 20 (u8)tempFile;.
123ee 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 6c 65 assert( tempFile
123ef 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d ==PAGER_LOCKINGM
123f0 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20 20 ODE_NORMAL .
123f1 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 69 6c || tempFil
123f2 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 e==PAGER_LOCKING
123f3 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 29 MODE_EXCLUSIVE )
123f4 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 ;. assert( PAGE
123f5 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 R_LOCKINGMODE_EX
123f6 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a 20 20 CLUSIVE==1 );.
123f7 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
123f8 65 4d 6f 64 65 20 3d 20 28 75 38 29 74 65 6d 70 eMode = (u8)temp
123f9 46 69 6c 65 3b 20 0a 20 20 70 50 61 67 65 72 2d File; . pPager-
123fa 3e 6d 65 6d 44 62 20 3d 20 28 75 38 29 6d 65 6d >memDb = (u8)mem
123fb 44 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65 Db;. pPager->re
123fc 61 64 4f 6e 6c 79 20 3d 20 28 75 38 29 72 65 61 adOnly = (u8)rea
123fd 64 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 dOnly;. /* pPag
123fe 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 er->needSync = 0
123ff 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e ; */. pPager->n
12400 6f 53 79 6e 63 20 3d 20 28 70 50 61 67 65 72 2d oSync = (pPager-
12401 3e 74 65 6d 70 46 69 6c 65 20 7c 7c 20 21 75 73 >tempFile || !us
12402 65 4a 6f 75 72 6e 61 6c 29 20 3f 31 3a 30 3b 0a eJournal) ?1:0;.
12403 20 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 pPager->fullSy
12404 6e 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 nc = pPager->noS
12405 79 6e 63 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67 ync ?0:1;. pPag
12406 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d er->sync_flags =
12407 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 SQLITE_SYNC_NOR
12408 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 MAL;. /* pPager
12409 2d 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f ->pFirst = 0; */
1240a 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 . /* pPager->pF
1240b 69 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 irstSynced = 0;
1240c 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e */. /* pPager->
1240d 70 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 pLast = 0; */.
1240e 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d pPager->nExtra =
1240f 20 6e 45 78 74 72 61 3b 0a 20 20 70 50 61 67 65 nExtra;. pPage
12410 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 r->journalSizeLi
12411 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 mit = SQLITE_DEF
12412 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a AULT_JOURNAL_SIZ
12413 45 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73 65 72 E_LIMIT;. asser
12414 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d t(pPager->fd->pM
12415 65 74 68 6f 64 73 7c 7c 74 65 6d 70 46 69 6c 65 ethods||tempFile
12416 29 3b 0a 20 20 73 65 74 53 65 63 74 6f 72 53 69 );. setSectorSi
12417 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 ze(pPager);. if
12418 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 70 ( memDb ){. p
12419 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
1241a 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e de = PAGER_JOURN
1241b 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a 20 ALMODE_MEMORY;.
1241c 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e }. /* pPager->
1241d 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 30 xBusyHandler = 0
1241e 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
1241f 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 ->pBusyHandlerAr
12420 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 6d g = 0; */. /* m
12421 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61 48 emset(pPager->aH
12422 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 ash, 0, sizeof(p
12423 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b 20 Pager->aHash));
12424 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 */. *ppPager =
12425 70 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72 6e pPager;. return
12426 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
12427 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75 73 *.** Set the bus
12428 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 y handler functi
12429 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
1242a 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1242b 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 e3PagerSetBusyha
1242c 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 72 20 2a ndler(. Pager *
1242d 70 50 61 67 65 72 2c 20 0a 20 20 69 6e 74 20 28 pPager, . int (
1242e 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28 76 *xBusyHandler)(v
1242f 6f 69 64 20 2a 29 2c 0a 20 20 76 6f 69 64 20 2a oid *),. void *
12430 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 0a pBusyHandlerArg.
12431 29 7b 20 20 0a 20 20 70 50 61 67 65 72 2d 3e 78 ){ . pPager->x
12432 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 42 BusyHandler = xB
12433 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50 usyHandler;. pP
12434 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c ager->pBusyHandl
12435 65 72 41 72 67 20 3d 20 70 42 75 73 79 48 61 6e erArg = pBusyHan
12436 64 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a dlerArg;.}../*.*
12437 2a 20 53 65 74 20 74 68 65 20 72 65 69 6e 69 74 * Set the reinit
12438 69 61 6c 69 7a 65 72 20 66 6f 72 20 74 68 69 73 ializer for this
12439 20 70 61 67 65 72 2e 20 20 49 66 20 6e 6f 74 20 pager. If not
1243a 4e 55 4c 4c 2c 20 74 68 65 20 72 65 69 6e 69 74 NULL, the reinit
1243b 69 61 6c 69 7a 65 72 0a 2a 2a 20 69 73 20 63 61 ializer.** is ca
1243c 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 63 6f lled when the co
1243d 6e 74 65 6e 74 20 6f 66 20 61 20 70 61 67 65 20 ntent of a page
1243e 69 6e 20 63 61 63 68 65 20 69 73 20 72 65 73 74 in cache is rest
1243f 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 ored to its orig
12440 69 6e 61 6c 0a 2a 2a 20 76 61 6c 75 65 20 61 73 inal.** value as
12441 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 20 72 a result of a r
12442 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 63 61 ollback. The ca
12443 6c 6c 62 61 63 6b 20 67 69 76 65 73 20 68 69 67 llback gives hig
12444 68 65 72 2d 6c 65 76 65 6c 20 63 6f 64 65 0a 2a her-level code.*
12445 2a 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 * an opportunity
12446 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 to restore the
12447 45 58 54 52 41 20 73 65 63 74 69 6f 6e 20 74 6f EXTRA section to
12448 20 61 67 72 65 65 20 77 69 74 68 20 74 68 65 20 agree with the
12449 72 65 73 74 6f 72 65 64 0a 2a 2a 20 70 61 67 65 restored.** page
1244a 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 data..*/.SQLITE
1244b 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1244c 6c 69 74 65 33 50 61 67 65 72 53 65 74 52 65 69 lite3PagerSetRei
1244d 6e 69 74 65 72 28 50 61 67 65 72 20 2a 70 50 61 niter(Pager *pPa
1244e 67 65 72 2c 20 76 6f 69 64 20 28 2a 78 52 65 69 ger, void (*xRei
1244f 6e 69 74 29 28 44 62 50 61 67 65 2a 29 29 7b 0a nit)(DbPage*)){.
12450 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 pPager->xReini
12451 74 65 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a 7d ter = xReinit;.}
12452 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
12453 70 61 67 65 20 73 69 7a 65 20 74 6f 20 2a 70 50 page size to *pP
12454 61 67 65 53 69 7a 65 2e 20 49 66 20 74 68 65 20 ageSize. If the
12455 73 75 67 67 65 73 74 20 6e 65 77 20 70 61 67 65 suggest new page
12456 20 73 69 7a 65 20 69 73 0a 2a 2a 20 69 6e 61 70 size is.** inap
12457 70 72 6f 70 72 69 61 74 65 2c 20 74 68 65 6e 20 propriate, then
12458 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70 an alternative p
12459 61 67 65 20 73 69 7a 65 20 69 73 20 73 65 74 20 age size is set
1245a 74 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 to that.** value
1245b 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
1245c 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
1245d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1245e 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
1245f 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
12460 75 31 36 20 2a 70 50 61 67 65 53 69 7a 65 29 7b u16 *pPageSize){
12461 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67 . int rc = pPag
12462 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 69 er->errCode;. i
12463 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12464 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67 65 ){. u16 page
12465 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 53 69 7a Size = *pPageSiz
12466 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e;. assert( p
12467 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 ageSize==0 || (p
12468 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 ageSize>=512 &&
12469 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 pageSize<=SQLITE
1246a 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 20 _MAX_PAGE_SIZE)
1246b 29 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 53 );. if( pageS
1246c 69 7a 65 20 26 26 20 70 61 67 65 53 69 7a 65 21 ize && pageSize!
1246d 3d 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a =pPager->pageSiz
1246e 65 20 0a 20 20 20 20 20 26 26 20 28 70 50 61 67 e . && (pPag
1246f 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 er->memDb==0 ||
12470 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d pPager->dbSize==
12471 30 29 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 0). && sqlit
12472 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 e3PcacheRefCount
12473 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
12474 29 3d 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 )==0 . ){.
12475 20 20 20 63 68 61 72 20 2a 70 4e 65 77 20 3d 20 char *pNew =
12476 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 (char *)sqlite3P
12477 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69 ageMalloc(pageSi
12478 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 ze);. if( !
12479 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 pNew ){.
1247a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
1247b 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a M;. }else{.
1247c 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 pager_re
1247d 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 set(pPager);.
1247e 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67 pPager->pag
1247f 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 eSize = pageSize
12480 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 ;. if( !p
12481 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 20 73 Pager->memDb ) s
12482 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 etSectorSize(pPa
12483 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 ger);. sq
12484 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 50 lite3PageFree(pP
12485 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 ager->pTmpSpace)
12486 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 ;. pPager
12487 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 70 4e ->pTmpSpace = pN
12488 65 77 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ew;. sqli
12489 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65 te3PcacheSetPage
1248a 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 Size(pPager->pPC
1248b 61 63 68 65 2c 20 70 61 67 65 53 69 7a 65 29 3b ache, pageSize);
1248c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1248d 20 20 20 2a 70 50 61 67 65 53 69 7a 65 20 3d 20 *pPageSize =
1248e 28 75 31 36 29 70 50 61 67 65 72 2d 3e 70 61 67 (u16)pPager->pag
1248f 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 eSize;. }. ret
12490 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
12491 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
12492 72 20 74 6f 20 74 68 65 20 22 74 65 6d 70 6f 72 r to the "tempor
12493 61 72 79 20 70 61 67 65 22 20 62 75 66 66 65 72 ary page" buffer
12494 20 68 65 6c 64 20 69 6e 74 65 72 6e 61 6c 6c 79 held internally
12495 0a 2a 2a 20 62 79 20 74 68 65 20 70 61 67 65 72 .** by the pager
12496 2e 20 20 54 68 69 73 20 69 73 20 61 20 62 75 66 . This is a buf
12497 66 65 72 20 74 68 61 74 20 69 73 20 62 69 67 20 fer that is big
12498 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 enough to hold t
12499 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e he.** entire con
1249a 74 65 6e 74 20 6f 66 20 61 20 64 61 74 61 62 61 tent of a databa
1249b 73 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 62 se page. This b
1249c 75 66 66 65 72 20 69 73 20 75 73 65 64 20 69 6e uffer is used in
1249d 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 69 ternally.** duri
1249e 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 ng rollback and
1249f 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 will be overwrit
124a0 74 65 6e 20 77 68 65 6e 65 76 65 72 20 61 20 72 ten whenever a r
124a1 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 ollback.** occur
124a2 73 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d 6f s. But other mo
124a3 64 75 6c 65 73 20 61 72 65 20 66 72 65 65 20 74 dules are free t
124a4 6f 20 75 73 65 20 69 74 20 74 6f 6f 2c 20 61 73 o use it too, as
124a5 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 72 long as.** no r
124a6 6f 6c 6c 62 61 63 6b 73 20 61 72 65 20 68 61 70 ollbacks are hap
124a7 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 pening..*/.SQLIT
124a8 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
124a9 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 sqlite3PagerTemp
124aa 53 70 61 63 65 28 50 61 67 65 72 20 2a 70 50 61 Space(Pager *pPa
124ab 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 ger){. return p
124ac 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
124ad 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d ;.}../*.** Attem
124ae 70 74 20 74 6f 20 73 65 74 20 74 68 65 20 6d 61 pt to set the ma
124af 78 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20 70 ximum database p
124b0 61 67 65 20 63 6f 75 6e 74 20 69 66 20 6d 78 50 age count if mxP
124b1 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e age is positive.
124b2 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 .** Make no cha
124b3 6e 67 65 73 20 69 66 20 6d 78 50 61 67 65 20 69 nges if mxPage i
124b4 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 s zero or negati
124b5 76 65 2e 20 20 41 6e 64 20 6e 65 76 65 72 20 72 ve. And never r
124b6 65 64 75 63 65 20 74 68 65 0a 2a 2a 20 6d 61 78 educe the.** max
124b7 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 imum page count
124b8 62 65 6c 6f 77 20 74 68 65 20 63 75 72 72 65 6e below the curren
124b9 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 t size of the da
124ba 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 tabase..**.** Re
124bb 67 61 72 64 6c 65 73 73 20 6f 66 20 6d 78 50 61 gardless of mxPa
124bc 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 63 ge, return the c
124bd 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20 70 urrent maximum p
124be 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 age count..*/.SQ
124bf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
124c0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 78 sqlite3PagerMax
124c1 50 61 67 65 43 6f 75 6e 74 28 50 61 67 65 72 20 PageCount(Pager
124c2 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 *pPager, int mxP
124c3 61 67 65 29 7b 0a 20 20 69 66 28 20 6d 78 50 61 age){. if( mxPa
124c4 67 65 3e 30 20 29 7b 0a 20 20 20 20 70 50 61 67 ge>0 ){. pPag
124c5 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78 50 er->mxPgno = mxP
124c6 61 67 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 age;. }. sqlit
124c7 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
124c8 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 72 (pPager, 0);. r
124c9 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6d 78 eturn pPager->mx
124ca 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 Pgno;.}../*.** T
124cb 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 74 he following set
124cc 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 65 of routines are
124cd 20 75 73 65 64 20 74 6f 20 64 69 73 61 62 6c 65 used to disable
124ce 20 74 68 65 20 73 69 6d 75 6c 61 74 65 64 0a 2a the simulated.*
124cf 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65 63 68 * I/O error mech
124d0 61 6e 69 73 6d 2e 20 20 54 68 65 73 65 20 72 6f anism. These ro
124d1 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 utines are used
124d2 74 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c 61 74 to avoid simulat
124d3 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69 6e 20 ed.** errors in
124d4 70 6c 61 63 65 73 20 77 68 65 72 65 20 77 65 20 places where we
124d5 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 do not care abou
124d6 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 t errors..**.**
124d7 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54 45 5f Unless -DSQLITE_
124d8 54 45 53 54 3d 31 20 69 73 20 75 73 65 64 2c 20 TEST=1 is used,
124d9 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 these routines a
124da 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a re all no-ops.**
124db 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 6e 6f and generate no
124dc 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 code..*/.#ifdef
124dd 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
124de 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 69 ITE_API extern i
124df 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
124e0 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 53 51 4c ror_pending;.SQL
124e1 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 69 ITE_API extern i
124e2 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
124e3 72 6f 72 5f 68 69 74 3b 0a 73 74 61 74 69 63 20 ror_hit;.static
124e4 69 6e 74 20 73 61 76 65 64 5f 63 6e 74 3b 0a 76 int saved_cnt;.v
124e5 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 oid disable_simu
124e6 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
124e7 76 6f 69 64 29 7b 0a 20 20 73 61 76 65 64 5f 63 void){. saved_c
124e8 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f 69 6f 5f nt = sqlite3_io_
124e9 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 error_pending;.
124ea 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
124eb 72 5f 70 65 6e 64 69 6e 67 20 3d 20 2d 31 3b 0a r_pending = -1;.
124ec 7d 0a 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 }.void enable_si
124ed 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
124ee 73 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 s(void){. sqlit
124ef 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
124f0 69 6e 67 20 3d 20 73 61 76 65 64 5f 63 6e 74 3b ing = saved_cnt;
124f1 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e .}.#else.# defin
124f2 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 e disable_simula
124f3 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a ted_io_errors().
124f4 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f # define enable_
124f5 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
124f6 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a ors().#endif../*
124f7 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 66 69 72 .** Read the fir
124f8 73 74 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 st N bytes from
124f9 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
124fa 20 74 68 65 20 66 69 6c 65 20 69 6e 74 6f 20 6d the file into m
124fb 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 74 20 70 44 emory.** that pD
124fc 65 73 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 0a est points to. .
124fd 2a 2a 0a 2a 2a 20 4e 6f 20 65 72 72 6f 72 20 63 **.** No error c
124fe 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 2e hecking is done.
124ff 20 54 68 65 20 72 61 74 69 6f 6e 61 6c 20 66 6f The rational fo
12500 72 20 74 68 69 73 20 69 73 20 74 68 61 74 20 74 r this is that t
12501 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a his function .**
12502 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 65 may be called e
12503 76 65 6e 20 69 66 20 74 68 65 20 66 69 6c 65 20 ven if the file
12504 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 6f does not exist o
12505 72 20 63 6f 6e 74 61 69 6e 20 61 20 68 65 61 64 r contain a head
12506 65 72 2e 20 49 6e 20 0a 2a 2a 20 74 68 65 73 65 er. In .** these
12507 20 63 61 73 65 73 20 73 71 6c 69 74 65 33 4f 73 cases sqlite3Os
12508 52 65 61 64 28 29 20 77 69 6c 6c 20 72 65 74 75 Read() will retu
12509 72 6e 20 61 6e 20 65 72 72 6f 72 2c 20 74 6f 20 rn an error, to
1250a 77 68 69 63 68 20 74 68 65 20 63 6f 72 72 65 63 which the correc
1250b 74 20 0a 2a 2a 20 72 65 73 70 6f 6e 73 65 20 69 t .** response i
1250c 73 20 74 6f 20 7a 65 72 6f 20 74 68 65 20 6d 65 s to zero the me
1250d 6d 6f 72 79 20 61 74 20 70 44 65 73 74 20 61 6e mory at pDest an
1250e 64 20 63 6f 6e 74 69 6e 75 65 2e 20 20 41 20 72 d continue. A r
1250f 65 61 6c 20 49 4f 20 65 72 72 6f 72 20 0a 2a 2a eal IO error .**
12510 20 77 69 6c 6c 20 70 72 65 73 75 6d 61 62 6c 79 will presumably
12511 20 72 65 63 75 72 20 61 6e 64 20 62 65 20 70 69 recur and be pi
12512 63 6b 65 64 20 75 70 20 6c 61 74 65 72 20 28 54 cked up later (T
12513 6f 64 6f 3a 20 54 68 69 6e 6b 20 61 62 6f 75 74 odo: Think about
12514 20 74 68 69 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54 this)..*/.SQLIT
12515 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
12516 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69 lite3PagerReadFi
12517 6c 65 68 65 61 64 65 72 28 50 61 67 65 72 20 2a leheader(Pager *
12518 70 50 61 67 65 72 2c 20 69 6e 74 20 4e 2c 20 75 pPager, int N, u
12519 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 44 nsigned char *pD
1251a 65 73 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d est){. int rc =
1251b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6d 65 SQLITE_OK;. me
1251c 6d 73 65 74 28 70 44 65 73 74 2c 20 30 2c 20 4e mset(pDest, 0, N
1251d 29 3b 0a 20 20 61 73 73 65 72 74 28 70 50 61 67 );. assert(pPag
1251e 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 er->fd->pMethods
1251f 7c 7c 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 ||pPager->tempFi
12520 6c 65 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 le);. if( pPage
12521 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 r->fd->pMethods
12522 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 ){. IOTRACE((
12523 22 44 42 48 44 52 20 25 70 20 30 20 25 64 5c 6e "DBHDR %p 0 %d\n
12524 22 2c 20 70 50 61 67 65 72 2c 20 4e 29 29 0a 20 ", pPager, N)).
12525 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
12526 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 sRead(pPager->fd
12527 2c 20 70 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a , pDest, N, 0);.
12528 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
12529 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 TE_IOERR_SHORT_R
1252a 45 41 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 EAD ){. rc
1252b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
1252c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1252d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
1252e 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 urn the total nu
1252f 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
12530 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61 the disk file a
12531 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a ssociated with.*
12532 2a 20 70 50 61 67 65 72 2e 20 0a 2a 2a 0a 2a 2a * pPager. .**.**
12533 20 49 66 20 74 68 65 20 50 45 4e 44 49 4e 47 5f If the PENDING_
12534 42 59 54 45 20 6c 69 65 73 20 6f 6e 20 74 68 65 BYTE lies on the
12535 20 70 61 67 65 20 64 69 72 65 63 74 6c 79 20 61 page directly a
12536 66 74 65 72 20 74 68 65 20 65 6e 64 20 6f 66 20 fter the end of
12537 74 68 65 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65 the.** file, the
12538 6e 20 63 6f 6e 73 69 64 65 72 20 74 68 69 73 20 n consider this
12539 70 61 67 65 20 70 61 72 74 20 6f 66 20 74 68 65 page part of the
1253a 20 66 69 6c 65 20 74 6f 6f 2e 20 46 6f 72 20 65 file too. For e
1253b 78 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 50 45 xample, if.** PE
1253c 4e 44 49 4e 47 5f 42 59 54 45 20 69 73 20 62 79 NDING_BYTE is by
1253d 74 65 20 34 30 39 36 20 28 74 68 65 20 66 69 72 te 4096 (the fir
1253e 73 74 20 62 79 74 65 20 6f 66 20 70 61 67 65 20 st byte of page
1253f 35 29 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 5) and the size
12540 6f 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69 of the.** file i
12541 73 20 34 30 39 36 20 62 79 74 65 73 2c 20 35 20 s 4096 bytes, 5
12542 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 is returned inst
12543 65 61 64 20 6f 66 20 34 2e 0a 2a 2f 0a 53 51 4c ead of 4..*/.SQL
12544 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
12545 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
12546 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 count(Pager *pPa
12547 67 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 67 65 ger, int *pnPage
12548 29 7b 0a 20 20 69 36 34 20 6e 20 3d 20 30 3b 0a ){. i64 n = 0;.
12549 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
1254a 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b rt( pPager!=0 );
1254b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 . if( pPager->e
1254c 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 rrCode ){. rc
1254d 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f = pPager->errCo
1254e 64 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 de;. return r
1254f 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 c;. }. if( pPa
12550 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
12551 20 29 7b 0a 20 20 20 20 6e 20 3d 20 70 50 61 67 ){. n = pPag
12552 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 20 er->dbSize;. }
12553 65 6c 73 65 20 7b 0a 20 20 20 20 61 73 73 65 72 else {. asser
12554 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d t(pPager->fd->pM
12555 65 74 68 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e ethods||pPager->
12556 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 tempFile);. i
12557 66 28 20 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e f( (pPager->fd->
12558 70 4d 65 74 68 6f 64 73 29 0a 20 20 20 20 20 26 pMethods). &
12559 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f & (rc = sqlite3O
1255a 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 sFileSize(pPager
1255b 2d 3e 66 64 2c 20 26 6e 29 29 21 3d 53 51 4c 49 ->fd, &n))!=SQLI
1255c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 TE_OK ){. p
1255d 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 ager_error(pPage
1255e 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 72 65 r, rc);. re
1255f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
12560 20 20 20 69 66 28 20 6e 3e 30 20 26 26 20 6e 3c if( n>0 && n<
12561 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
12562 20 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 31 3b ){. n = 1;
12563 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
12564 20 20 6e 20 2f 3d 20 70 50 61 67 65 72 2d 3e 70 n /= pPager->p
12565 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 ageSize;. }.
12566 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 if( pPager->s
12567 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f tate!=PAGER_UNLO
12568 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 CK ){. pPag
12569 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 28 50 67 er->dbSize = (Pg
1256a 6e 6f 29 6e 3b 0a 20 20 20 20 20 20 70 50 61 67 no)n;. pPag
1256b 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d er->dbFileSize =
1256c 20 28 50 67 6e 6f 29 6e 3b 0a 20 20 20 20 20 20 (Pgno)n;.
1256d 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
1256e 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 lid = 1;. }.
1256f 20 7d 0a 20 20 69 66 28 20 6e 3d 3d 28 50 45 4e }. if( n==(PEN
12570 44 49 4e 47 5f 42 59 54 45 2f 70 50 61 67 65 72 DING_BYTE/pPager
12571 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 7b 0a 20 ->pageSize) ){.
12572 20 20 20 6e 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 n++;. }. if
12573 28 20 6e 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 ( n>pPager->mxPg
12574 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 no ){. pPager
12575 2d 3e 6d 78 50 67 6e 6f 20 3d 20 28 50 67 6e 6f ->mxPgno = (Pgno
12576 29 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 6e )n;. }. if( pn
12577 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 6e 50 Page ){. *pnP
12578 61 67 65 20 3d 20 28 69 6e 74 29 6e 3b 0a 20 20 age = (int)n;.
12579 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
1257a 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 E_OK;.}../*.** F
1257b 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
1257c 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 on.*/.static int
1257d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 syncJournal(Pag
1257e 65 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 er*);../*.** Try
1257f 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 to obtain a loc
12580 6b 20 6f 6e 20 61 20 66 69 6c 65 2e 20 20 49 6e k on a file. In
12581 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 63 61 voke the busy ca
12582 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 20 6c 6f llback if the lo
12583 63 6b 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 ck.** is current
12584 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 ly not available
12585 2e 20 20 52 65 70 65 61 74 20 75 6e 74 69 6c 20 . Repeat until
12586 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 the busy callbac
12587 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 66 61 6c k returns.** fal
12588 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 se or until the
12589 6c 6f 63 6b 20 73 75 63 63 65 65 64 73 2e 0a 2a lock succeeds..*
1258a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
1258b 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
1258c 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f and an error co
1258d 64 65 20 69 66 20 77 65 20 63 61 6e 6e 6f 74 20 de if we cannot
1258e 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f obtain.** the lo
1258f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ck..*/.static in
12590 74 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f t pager_wait_on_
12591 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 lock(Pager *pPag
12592 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 er, int locktype
12593 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 ){. int rc;..
12594 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20 76 /* The OS lock v
12595 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 74 68 alues must be th
12596 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 50 61 e same as the Pa
12597 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 ger lock values
12598 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 */. assert( PAG
12599 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 41 52 45 ER_SHARED==SHARE
1259a 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 D_LOCK );. asse
1259b 72 74 28 20 50 41 47 45 52 5f 52 45 53 45 52 56 rt( PAGER_RESERV
1259c 45 44 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 ED==RESERVED_LOC
1259d 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 K );. assert( P
1259e 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3d 3d AGER_EXCLUSIVE==
1259f 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 EXCLUSIVE_LOCK )
125a0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 ;.. /* If the f
125a1 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ile is currently
125a2 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20 74 unlocked then t
125a3 68 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 he size must be
125a4 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 61 73 73 unknown */. ass
125a5 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
125a6 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 te>=PAGER_SHARED
125a7 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 || pPager->dbSi
125a8 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 0a 20 zeValid==0 );..
125a9 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
125aa 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a te>=locktype ){.
125ab 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
125ac 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
125ad 20 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20 3d do {. rc =
125ae 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 sqlite3OsLock(p
125af 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74 Pager->fd, lockt
125b0 79 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 ype);. }while
125b1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 ( rc==SQLITE_BUS
125b2 59 20 26 26 20 70 50 61 67 65 72 2d 3e 78 42 75 Y && pPager->xBu
125b3 73 79 48 61 6e 64 6c 65 72 28 70 50 61 67 65 72 syHandler(pPager
125b4 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 ->pBusyHandlerAr
125b5 67 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 g) );. if( rc
125b6 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
125b7 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 pPager->sta
125b8 74 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 te = (u8)locktyp
125b9 65 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 e;. IOTRACE
125ba 28 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22 (("LOCK %p %d\n"
125bb 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 , pPager, lockty
125bc 70 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 pe)). }. }.
125bd 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
125be 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
125bf 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a IT_AUTOVACUUM./*
125c0 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 .** Truncate the
125c1 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
125c2 61 73 65 20 66 69 6c 65 20 69 6d 61 67 65 20 74 ase file image t
125c3 6f 20 6e 50 61 67 65 20 70 61 67 65 73 2e 20 54 o nPage pages. T
125c4 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e his .** function
125c5 20 64 6f 65 73 20 6e 6f 74 20 61 63 74 75 61 6c does not actual
125c6 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 ly modify the da
125c7 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 64 tabase file on d
125c8 69 73 6b 2e 20 49 74 20 0a 2a 2a 20 6a 75 73 74 isk. It .** just
125c9 20 73 65 74 73 20 74 68 65 20 69 6e 74 65 72 6e sets the intern
125ca 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 al state of the
125cb 70 61 67 65 72 20 6f 62 6a 65 63 74 20 73 6f 20 pager object so
125cc 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 74 72 75 that the .** tru
125cd 6e 63 61 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 ncation will be
125ce 64 6f 6e 65 20 77 68 65 6e 20 74 68 65 20 63 75 done when the cu
125cf 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f rrent transactio
125d0 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a n is committed..
125d1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
125d2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
125d3 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 gerTruncateImage
125d4 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
125d5 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 61 Pgno nPage){. a
125d6 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
125d7 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 bSizeValid );.
125d8 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
125d9 64 62 53 69 7a 65 3e 3d 6e 50 61 67 65 20 29 3b dbSize>=nPage );
125da 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a . pPager->dbSiz
125db 65 20 3d 20 6e 50 61 67 65 3b 0a 7d 0a 0a 2f 2a e = nPage;.}../*
125dc 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 .** Return the c
125dd 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 urrent size of t
125de 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
125df 20 69 6d 61 67 65 20 69 6e 20 70 61 67 65 73 2e image in pages.
125e0 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f This.** functio
125e1 6e 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 73 n differs from s
125e2 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
125e3 6f 75 6e 74 28 29 20 69 6e 20 74 77 6f 20 77 61 ount() in two wa
125e4 79 73 3a 0a 2a 2a 0a 2a 2a 20 20 61 29 20 49 74 ys:.**.** a) It
125e5 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c may only be cal
125e6 6c 65 64 20 77 68 65 6e 20 61 74 20 6c 65 61 73 led when at leas
125e7 74 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 t one reference
125e8 74 6f 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a to a database.**
125e9 20 20 20 20 20 70 61 67 65 20 69 73 20 68 65 6c page is hel
125ea 64 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 65 d. This guarante
125eb 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 es that the data
125ec 62 61 73 65 20 73 69 7a 65 20 69 73 20 61 6c 72 base size is alr
125ed 65 61 64 79 0a 2a 2a 20 20 20 20 20 6b 6e 6f 77 eady.** know
125ee 6e 20 61 6e 64 20 61 20 63 61 6c 6c 20 74 6f 20 n and a call to
125ef 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
125f0 65 28 29 20 69 73 20 6e 6f 74 20 72 65 71 75 69 e() is not requi
125f1 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 62 29 20 54 red..**.** b) T
125f2 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
125f3 69 73 20 6e 6f 74 20 61 64 6a 75 73 74 65 64 20 is not adjusted
125f4 66 6f 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 for the locking
125f5 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f page..*/.SQLITE_
125f6 50 52 49 56 41 54 45 20 50 67 6e 6f 20 73 71 6c PRIVATE Pgno sql
125f7 69 74 65 33 50 61 67 65 72 49 6d 61 67 65 53 69 ite3PagerImageSi
125f8 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ze(Pager *pPager
125f9 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ){. assert( pPa
125fa 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
125fb 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 );. return pPa
125fc 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 7d 0a 23 ger->dbSize;.}.#
125fd 65 6e 64 69 66 20 20 2f 2a 20 69 66 6e 64 65 66 endif /* ifndef
125fe 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
125ff 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a 0a 2a OVACUUM */../*.*
12600 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 70 * Shutdown the p
12601 61 67 65 20 63 61 63 68 65 2e 20 20 46 72 65 65 age cache. Free
12602 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 20 all memory and
12603 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 2e close all files.
12604 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 6e .**.** If a tran
12605 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 70 saction was in p
12606 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74 68 69 rogress when thi
12607 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
12608 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 61 led, that.** tra
12609 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c nsaction is roll
1260a 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f 75 ed back. All ou
1260b 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20 tstanding pages
1260c 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a are invalidated.
1260d 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d 65 6d ** and their mem
1260e 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20 20 41 ory is freed. A
1260f 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 ny attempt to us
12610 65 20 61 20 70 61 67 65 20 61 73 73 6f 63 69 61 e a page associa
12611 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 73 ted.** with this
12612 20 70 61 67 65 20 63 61 63 68 65 20 61 66 74 65 page cache afte
12613 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 r this function
12614 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b returns will lik
12615 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e ely.** result in
12616 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a a coredump..**.
12617 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
12618 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73 always succeeds
12619 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 . If a transacti
1261a 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e 20 on is active an
1261b 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 attempt.** is ma
1261c 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 de to roll it ba
1261d 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 ck. If an error
1261e 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 occurs during th
1261f 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61 e rollback .** a
12620 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 hot journal may
12621 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 20 be left in the
12622 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74 20 6e filesystem but n
12623 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 o error is retur
12624 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 ned.** to the ca
12625 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ller..*/.SQLITE_
12626 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
12627 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 te3PagerClose(Pa
12628 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 ger *pPager){..
12629 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 disable_simulat
1262a 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a ed_io_errors();.
1262b 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
1262c 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
1262d 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
1262e 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 = 0;. pPager->e
1262f 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 30 xclusiveMode = 0
12630 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 ;. pager_reset(
12631 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21 pPager);. if( !
12632 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 2f 2a 20 MEMDB ){. /*
12633 53 65 74 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 Set Pager.journa
12634 6c 48 64 72 20 74 6f 20 2d 31 20 66 6f 72 20 74 lHdr to -1 for t
12635 68 65 20 62 65 6e 65 66 69 74 20 6f 66 20 74 68 he benefit of th
12636 65 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b e pager_playback
12637 28 29 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 () . ** call
12638 77 68 69 63 68 20 6d 61 79 20 62 65 20 6d 61 64 which may be mad
12639 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 e from within pa
1263a 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c gerUnlockAndRoll
1263b 62 61 63 6b 28 29 2e 20 49 66 20 69 74 0a 20 20 back(). If it.
1263c 20 20 2a 2a 20 69 73 20 6e 6f 74 20 2d 31 2c 20 ** is not -1,
1263d 74 68 65 6e 20 74 68 65 20 75 6e 73 79 6e 63 65 then the unsynce
1263e 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 61 6e 20 d portion of an
1263f 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c open journal fil
12640 65 20 6d 61 79 0a 20 20 20 20 2a 2a 20 62 65 20 e may. ** be
12641 70 6c 61 79 65 64 20 62 61 63 6b 20 69 6e 74 6f played back into
12642 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 the database. I
12643 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 f a power failur
12644 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 0a 20 e occurs while.
12645 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 ** this is ha
12646 70 70 65 6e 69 6e 67 2c 20 74 68 65 20 64 61 74 ppening, the dat
12647 61 62 61 73 65 20 6d 61 79 20 62 65 63 6f 6d 65 abase may become
12648 20 63 6f 72 72 75 70 74 2e 0a 20 20 20 20 2a 2f corrupt.. */
12649 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
1264a 72 6e 61 6c 48 64 72 20 3d 20 2d 31 3b 0a 20 20 rnalHdr = -1;.
1264b 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 pagerUnlockAnd
1264c 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 Rollback(pPager)
1264d 3b 0a 20 20 7d 0a 20 20 65 6e 61 62 6c 65 5f 73 ;. }. enable_s
1264e 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
1264f 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 rs();. sqlite3E
12650 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
12651 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 ;. PAGERTRACE((
12652 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50 41 "CLOSE %d\n", PA
12653 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b GERID(pPager)));
12654 0a 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c 4f . IOTRACE(("CLO
12655 53 45 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 SE %p\n", pPager
12656 29 29 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d )). if( pPager-
12657 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a >journalOpen ){.
12658 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
12659 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b se(pPager->jfd);
1265a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 69 . }. sqlite3Bi
1265b 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 tvecDestroy(pPag
1265c 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b er->pInJournal);
1265d 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 . sqlite3Bitvec
1265e 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e Destroy(pPager->
1265f 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 29 pAlwaysRollback)
12660 3b 0a 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 ;. releaseAllSa
12661 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 29 3b vepoint(pPager);
12662 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 . sqlite3OsClos
12663 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 e(pPager->fd);.
12664 20 2f 2a 20 54 65 6d 70 20 66 69 6c 65 73 20 61 /* Temp files a
12665 72 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 re automatically
12666 20 64 65 6c 65 74 65 64 20 62 79 20 74 68 65 20 deleted by the
12667 4f 53 0a 20 20 2a 2a 20 69 66 28 20 70 50 61 67 OS. ** if( pPag
12668 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a er->tempFile ){.
12669 20 20 2a 2a 20 20 20 73 71 6c 69 74 65 33 4f 73 ** sqlite3Os
1266a 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 7a Delete(pPager->z
1266b 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 2a 2a 20 Filename);. **
1266c 7d 0a 20 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 }. */.. sqlite
1266d 33 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72 3PageFree(pPager
1266e 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 ->pTmpSpace);.
1266f 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f sqlite3PcacheClo
12670 73 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 se(pPager->pPCac
12671 68 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 he);. sqlite3_f
12672 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 ree(pPager);. r
12673 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
12674 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .}..#if !defined
12675 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 (NDEBUG) || defi
12676 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 ned(SQLITE_TEST)
12677 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
12678 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f e page number fo
12679 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 r the given page
1267a 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 data..*/.SQLITE
1267b 5f 50 52 49 56 41 54 45 20 50 67 6e 6f 20 73 71 _PRIVATE Pgno sq
1267c 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 lite3PagerPagenu
1267d 6d 62 65 72 28 44 62 50 61 67 65 20 2a 70 29 7b mber(DbPage *p){
1267e 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 67 6e . return p->pgn
1267f 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a o;.}.#endif../*.
12680 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 ** Increment the
12681 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
12682 20 66 6f 72 20 61 20 70 61 67 65 2e 20 20 54 68 for a page. Th
12683 65 20 69 6e 70 75 74 20 70 6f 69 6e 74 65 72 20 e input pointer
12684 69 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 is.** a referenc
12685 65 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 61 e to the page da
12686 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ta..*/.SQLITE_PR
12687 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
12688 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67 65 3PagerRef(DbPage
12689 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74 65 *pPg){. sqlite
1268a 33 50 63 61 63 68 65 52 65 66 28 70 50 67 29 3b 3PcacheRef(pPg);
1268b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1268c 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 _OK;.}../*.** Sy
1268d 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 nc the journal.
1268e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
1268f 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 make sure all t
12690 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68 61 he pages that ha
12691 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74 74 ve.** been writt
12692 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 en to the journa
12693 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79 20 l have actually
12694 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72 66 reached the surf
12695 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 69 ace of the.** di
12696 73 6b 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 73 sk. It is not s
12697 61 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 afe to modify th
12698 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 e original datab
12699 61 73 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 61 ase file until a
1269a 66 74 65 72 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 fter.** the jour
1269b 6e 61 6c 20 68 61 73 20 62 65 65 6e 20 73 79 6e nal has been syn
1269c 63 65 64 2e 20 20 49 66 20 74 68 65 20 6f 72 69 ced. If the ori
1269d 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 69 ginal database i
1269e 73 20 6d 6f 64 69 66 69 65 64 20 62 65 66 6f 72 s modified befor
1269f 65 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c e.** the journal
126a0 20 69 73 20 73 79 6e 63 65 64 20 61 6e 64 20 61 is synced and a
126a1 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f power failure o
126a2 63 63 75 72 73 2c 20 74 68 65 20 75 6e 73 79 6e ccurs, the unsyn
126a3 63 65 64 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 64 ced journal.** d
126a4 61 74 61 20 77 6f 75 6c 64 20 62 65 20 6c 6f 73 ata would be los
126a5 74 20 61 6e 64 20 77 65 20 77 6f 75 6c 64 20 62 t and we would b
126a6 65 20 75 6e 61 62 6c 65 20 74 6f 20 63 6f 6d 70 e unable to comp
126a7 6c 65 74 65 6c 79 20 72 6f 6c 6c 62 61 63 6b 20 letely rollback
126a8 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
126a9 63 68 61 6e 67 65 73 2e 20 20 44 61 74 61 62 61 changes. Databa
126aa 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 6f se corruption wo
126ab 75 6c 64 20 6f 63 63 75 72 2e 0a 2a 2a 20 0a 2a uld occur..** .*
126ac 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 * This routine a
126ad 6c 73 6f 20 75 70 64 61 74 65 73 20 74 68 65 20 lso updates the
126ae 6e 52 65 63 20 66 69 65 6c 64 20 69 6e 20 74 68 nRec field in th
126af 65 20 68 65 61 64 65 72 20 6f 66 20 74 68 65 20 e header of the
126b0 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 28 53 65 65 journal..** (See
126b1 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 comments on the
126b2 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
126b3 29 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 64 ) routine for ad
126b4 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
126b5 74 69 6f 6e 2e 29 0a 2a 2a 20 49 66 20 74 68 65 tion.).** If the
126b6 20 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 46 55 sync mode is FU
126b7 4c 4c 2c 20 74 77 6f 20 73 79 6e 63 73 20 77 69 LL, two syncs wi
126b8 6c 6c 20 6f 63 63 75 72 2e 20 20 46 69 72 73 74 ll occur. First
126b9 20 74 68 65 20 77 68 6f 6c 65 20 6a 6f 75 72 6e the whole journ
126ba 61 6c 0a 2a 2a 20 69 73 20 73 79 6e 63 65 64 2c al.** is synced,
126bb 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66 then the nRec f
126bc 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2c ield is updated,
126bd 20 74 68 65 6e 20 61 20 73 65 63 6f 6e 64 20 73 then a second s
126be 79 6e 63 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a ync occurs..**.*
126bf 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 * For temporary
126c0 64 61 74 61 62 61 73 65 73 2c 20 77 65 20 64 6f databases, we do
126c1 20 6e 6f 74 20 63 61 72 65 20 69 66 20 77 65 20 not care if we
126c2 61 72 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c are able to roll
126c3 62 61 63 6b 0a 2a 2a 20 61 66 74 65 72 20 61 20 back.** after a
126c4 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 73 power failure, s
126c5 6f 20 6e 6f 20 73 79 6e 63 20 6f 63 63 75 72 73 o no sync occurs
126c6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 49 ..**.** If the I
126c7 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 OCAP_SEQUENTIAL
126c8 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 20 flag is set for
126c9 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d the persistent m
126ca 65 64 69 61 20 6f 6e 20 77 68 69 63 68 0a 2a 2a edia on which.**
126cb 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
126cc 20 73 74 6f 72 65 64 2c 20 74 68 65 6e 20 4f 73 stored, then Os
126cd 53 79 6e 63 28 29 20 69 73 20 6e 65 76 65 72 20 Sync() is never
126ce 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6a 6f called on the jo
126cf 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 49 urnal.** file. I
126d0 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20 n this case all
126d1 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 that is required
126d2 20 69 73 20 74 6f 20 75 70 64 61 74 65 20 74 68 is to update th
126d3 65 20 6e 52 65 63 20 66 69 65 6c 64 20 69 6e 0a e nRec field in.
126d4 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 ** the journal h
126d5 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 eader..**.** Thi
126d6 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 s routine clears
126d7 20 74 68 65 20 6e 65 65 64 53 79 6e 63 20 66 69 the needSync fi
126d8 65 6c 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 eld of every pag
126d9 65 20 63 75 72 72 65 6e 74 20 68 65 6c 64 20 69 e current held i
126da 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a n.** memory..*/.
126db 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 63 4a static int syncJ
126dc 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 ournal(Pager *pP
126dd 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 ager){. int rc
126de 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
126df 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 /* Sync the jour
126e0 6e 61 6c 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 nal before modif
126e1 79 69 6e 67 20 74 68 65 20 6d 61 69 6e 20 64 61 ying the main da
126e2 74 61 62 61 73 65 0a 20 20 2a 2a 20 28 61 73 73 tabase. ** (ass
126e3 75 6d 69 6e 67 20 74 68 65 72 65 20 69 73 20 61 uming there is a
126e4 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 74 20 journal and it
126e5 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 needs to be sync
126e6 65 64 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ed.). */. if(
126e7 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
126e8 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
126e9 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
126ea 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 e );. if( pPa
126eb 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
126ec 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d !=PAGER_JOURNALM
126ed 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 ODE_MEMORY ){.
126ee 20 20 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71 int iDc = sq
126ef 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
126f0 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 racteristics(pPa
126f1 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 ger->fd);.
126f2 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
126f3 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 0a journalOpen );..
126f4 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 if( 0==(iD
126f5 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 c&SQLITE_IOCAP_S
126f6 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20 AFE_APPEND) ){.
126f7 20 20 20 20 20 20 20 69 36 34 20 6a 72 6e 6c 4f i64 jrnlO
126f8 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f ff = journalHdrO
126f9 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 ffset(pPager);.
126fa 20 20 20 20 20 20 20 75 38 20 7a 4d 61 67 69 63 u8 zMagic
126fb 5b 38 5d 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a [8];.. /*
126fc 20 54 68 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c This block deal
126fd 73 20 77 69 74 68 20 61 6e 20 6f 62 73 63 75 72 s with an obscur
126fe 65 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 e problem. If th
126ff 65 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f e last connectio
12700 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 n. ** tha
12701 74 20 77 72 6f 74 65 20 74 6f 20 74 68 69 73 20 t wrote to this
12702 64 61 74 61 62 61 73 65 20 77 61 73 20 6f 70 65 database was ope
12703 72 61 74 69 6e 67 20 69 6e 20 70 65 72 73 69 73 rating in persis
12704 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 tent-journal.
12705 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 ** mode, th
12706 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 en the journal f
12707 69 6c 65 20 6d 61 79 20 61 74 20 74 68 69 73 20 ile may at this
12708 70 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 point actually b
12709 65 20 6c 61 72 67 65 72 0a 20 20 20 20 20 20 20 e larger.
1270a 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 6a ** than Pager.j
1270b 6f 75 72 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e ournalOff bytes.
1270c 20 49 66 20 74 68 65 20 6e 65 78 74 20 74 68 69 If the next thi
1270d 6e 67 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ng in the journa
1270e 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c l. ** fil
1270f 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 e happens to be
12710 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 a journal-header
12711 20 28 77 72 69 74 74 65 6e 20 61 73 20 70 61 72 (written as par
12712 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 t of the.
12713 20 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 6f 6e ** previous con
12714 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 nections transac
12715 74 69 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72 61 tion), and a cra
12716 73 68 20 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c sh or power-fail
12717 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ure . **
12718 6f 63 63 75 72 73 20 61 66 74 65 72 20 6e 52 65 occurs after nRe
12719 63 20 69 73 20 75 70 64 61 74 65 64 20 62 75 74 c is updated but
1271a 20 62 65 66 6f 72 65 20 74 68 69 73 20 63 6f 6e before this con
1271b 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20 0a nection writes .
1271c 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 ** anyth
1271d 69 6e 67 20 65 6c 73 65 20 74 6f 20 74 68 65 20 ing else to the
1271e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 journal file (or
1271f 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 commits/rolls b
12720 61 63 6b 20 69 74 73 20 0a 20 20 20 20 20 20 20 ack its .
12721 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 ** transaction)
12722 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 , then SQLite ma
12723 79 20 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 y become confuse
12724 64 20 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 d when doing the
12725 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 . ** hot
12726 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 -journal rollbac
12727 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f k following reco
12728 76 65 72 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c very. It may rol
12729 6c 20 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20 l back all.
1272a 20 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f ** of this co
1272b 6e 6e 65 63 74 69 6f 6e 73 20 64 61 74 61 2c 20 nnections data,
1272c 74 68 65 6e 20 70 72 6f 63 65 65 64 20 74 6f 20 then proceed to
1272d 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 65 rolling back the
1272e 20 6f 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a old,. **
1272f 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 64 61 74 out-of-date dat
12730 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 a that follows i
12731 74 2e 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 t. Database corr
12732 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 uption..
12733 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 6f **. ** To
12734 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 work around thi
12735 73 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 s, if the journa
12736 6c 20 66 69 6c 65 20 64 6f 65 73 20 61 70 70 65 l file does appe
12737 61 72 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 ar to contain.
12738 20 20 20 20 20 20 2a 2a 20 61 20 76 61 6c 69 64 ** a valid
12739 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e header followin
1273a 67 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f g Pager.journalO
1273b 66 66 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 ff, then write a
1273c 20 30 78 30 30 0a 20 20 20 20 20 20 20 20 2a 2a 0x00. **
1273d 20 62 79 74 65 20 74 6f 20 74 68 65 20 73 74 61 byte to the sta
1273e 72 74 20 6f 66 20 69 74 20 74 6f 20 70 72 65 76 rt of it to prev
1273f 65 6e 74 20 69 74 20 66 72 6f 6d 20 62 65 69 6e ent it from bein
12740 67 20 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20 g recognized..
12741 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
12742 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
12743 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ead(pPager->jfd,
12744 20 7a 4d 61 67 69 63 2c 20 38 2c 20 6a 72 6e 6c zMagic, 8, jrnl
12745 4f 66 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 Off);. if
12746 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
12747 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 4d 61 && 0==memcmp(zMa
12748 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 gic, aJournalMag
12749 69 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 20 ic, 8) ){.
1274a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
1274b 20 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 30 u8 zerobyte = 0
1274c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
1274d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
1274e 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 pPager->jfd, &ze
1274f 72 6f 62 79 74 65 2c 20 31 2c 20 6a 72 6e 6c 4f robyte, 1, jrnlO
12750 66 66 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ff);. }.
12751 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
12752 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d QLITE_OK && rc!=
12753 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
12754 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 RT_READ ){.
12755 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
12756 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }..
12757 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
12758 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 74 6f 20 nRec value into
12759 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1275a 20 68 65 61 64 65 72 2e 20 49 66 20 69 6e 0a 20 header. If in.
1275b 20 20 20 20 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 ** full-s
1275c 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c ynchronous mode,
1275d 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 sync the journa
1275e 6c 20 66 69 72 73 74 2e 20 54 68 69 73 20 65 6e l first. This en
1275f 73 75 72 65 73 20 74 68 61 74 0a 20 20 20 20 20 sures that.
12760 20 20 20 2a 2a 20 61 6c 6c 20 64 61 74 61 20 68 ** all data h
12761 61 73 20 72 65 61 6c 6c 79 20 68 69 74 20 74 68 as really hit th
12762 65 20 64 69 73 6b 20 62 65 66 6f 72 65 20 6e 52 e disk before nR
12763 65 63 20 69 73 20 75 70 64 61 74 65 64 20 74 6f ec is updated to
12764 20 6d 61 72 6b 0a 20 20 20 20 20 20 20 20 2a 2a mark. **
12765 20 69 74 20 61 73 20 61 20 63 61 6e 64 69 64 61 it as a candida
12766 74 65 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e te for rollback.
12767 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
12768 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 6e ** This is n
12769 6f 74 20 72 65 71 75 69 72 65 64 20 69 66 20 74 ot required if t
1276a 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d 65 he persistent me
1276b 64 69 61 20 73 75 70 70 6f 72 74 73 20 74 68 65 dia supports the
1276c 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 41 46 45 . ** SAFE
1276d 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 _APPEND property
1276e 2e 20 42 65 63 61 75 73 65 20 69 6e 20 74 68 69 . Because in thi
1276f 73 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f 74 s case it is not
12770 20 70 6f 73 73 69 62 6c 65 20 0a 20 20 20 20 20 possible .
12771 20 20 20 2a 2a 20 66 6f 72 20 67 61 72 62 61 67 ** for garbag
12772 65 20 64 61 74 61 20 74 6f 20 62 65 20 61 70 70 e data to be app
12773 65 6e 64 65 64 20 74 6f 20 74 68 65 20 66 69 6c ended to the fil
12774 65 2c 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c e, the nRec fiel
12775 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 d. ** is
12776 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 30 populated with 0
12777 78 46 46 46 46 46 46 46 46 20 77 68 65 6e 20 74 xFFFFFFFF when t
12778 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
12779 72 20 69 73 20 77 72 69 74 74 65 6e 0a 20 20 20 r is written.
1277a 20 20 20 20 20 2a 2a 20 61 6e 64 20 6e 65 76 65 ** and neve
1277b 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 r needs to be up
1277c 64 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a dated.. *
1277d 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 /. if( pP
1277e 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 ager->fullSync &
1277f 26 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 & 0==(iDc&SQLITE
12780 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 _IOCAP_SEQUENTIA
12781 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 L) ){.
12782 50 41 47 45 52 54 52 41 43 45 28 28 22 53 59 4e PAGERTRACE(("SYN
12783 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c C journal of %d\
12784 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
12785 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20 20 er)));.
12786 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e 43 IOTRACE(("JSYNC
12787 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 %p\n", pPager))
12788 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
12789 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 sqlite3OsSync(pP
1278a 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 ager->jfd, pPage
1278b 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a r->sync_flags);.
1278c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
1278d 21 3d 30 20 29 20 72 65 74 75 72 6e 20 72 63 3b !=0 ) return rc;
1278e 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 . }..
1278f 20 20 20 20 6a 72 6e 6c 4f 66 66 20 3d 20 70 50 jrnlOff = pP
12790 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
12791 20 2b 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e + sizeof(aJourn
12792 61 6c 4d 61 67 69 63 29 3b 0a 20 20 20 20 20 20 alMagic);.
12793 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 IOTRACE(("JHDR
12794 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 %p %lld %d\n",
12795 70 50 61 67 65 72 2c 20 6a 72 6e 6c 4f 66 66 2c pPager, jrnlOff,
12796 20 34 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 4));. rc
12797 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
12798 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c Pager->jfd, jrnl
12799 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65 Off, pPager->nRe
1279a 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 c);. if(
1279b 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1279c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
1279d 28 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 ( 0==(iDc&SQLITE
1279e 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 _IOCAP_SEQUENTIA
1279f 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 50 41 L) ){. PA
127a0 47 45 52 54 52 41 43 45 28 28 22 53 59 4e 43 20 GERTRACE(("SYNC
127a1 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 journal of %d\n"
127a2 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 , PAGERID(pPager
127a3 29 29 29 3b 0a 20 20 20 20 20 20 20 20 49 4f 54 )));. IOT
127a4 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70 5c RACE(("JSYNC %p\
127a5 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 n", pPager)).
127a6 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
127a7 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 3OsSync(pPager->
127a8 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e jfd, pPager->syn
127a9 63 5f 66 6c 61 67 73 7c 20 0a 20 20 20 20 20 20 c_flags| .
127aa 20 20 20 20 28 70 50 61 67 65 72 2d 3e 73 79 6e (pPager->syn
127ab 63 5f 66 6c 61 67 73 3d 3d 53 51 4c 49 54 45 5f c_flags==SQLITE_
127ac 53 59 4e 43 5f 46 55 4c 4c 3f 53 51 4c 49 54 45 SYNC_FULL?SQLITE
127ad 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 3a 30 _SYNC_DATAONLY:0
127ae 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 ). );.
127af 20 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 if( rc!=0 )
127b0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
127b1 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 }. pPager
127b2 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 ->journalStarted
127b3 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 1;. }.
127b4 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
127b5 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 45 72 = 0;.. /* Er
127b6 61 73 65 20 74 68 65 20 6e 65 65 64 53 79 6e 63 ase the needSync
127b7 20 66 6c 61 67 20 66 72 6f 6d 20 65 76 65 72 79 flag from every
127b8 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 page.. */.
127b9 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 sqlite3PcacheC
127ba 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 70 50 learSyncFlags(pP
127bb 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
127bc 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
127bd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e ;.}../*.** Given
127be 20 61 20 6c 69 73 74 20 6f 66 20 70 61 67 65 73 a list of pages
127bf 20 28 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 74 (connected by t
127c0 68 65 20 50 67 48 64 72 2e 70 44 69 72 74 79 20 he PgHdr.pDirty
127c1 70 6f 69 6e 74 65 72 29 20 77 72 69 74 65 0a 2a pointer) write.*
127c2 2a 20 65 76 65 72 79 20 6f 6e 65 20 6f 66 20 74 * every one of t
127c3 68 6f 73 65 20 70 61 67 65 73 20 6f 75 74 20 74 hose pages out t
127c4 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
127c5 69 6c 65 2e 20 4e 6f 20 63 61 6c 6c 73 20 61 72 ile. No calls ar
127c6 65 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 65 e made.** to the
127c7 20 70 61 67 65 2d 63 61 63 68 65 20 74 6f 20 6d page-cache to m
127c8 61 72 6b 20 74 68 65 20 70 61 67 65 73 20 61 73 ark the pages as
127c9 20 63 6c 65 61 6e 2e 20 49 74 20 69 73 20 74 68 clean. It is th
127ca 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 e responsibility
127cb 0a 2a 2a 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 .** of the calle
127cc 72 20 74 6f 20 75 73 65 20 50 63 61 63 68 65 43 r to use PcacheC
127cd 6c 65 61 6e 41 6c 6c 28 29 20 6f 72 20 50 63 61 leanAll() or Pca
127ce 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 74 cheMakeClean() t
127cf 6f 20 6d 61 72 6b 0a 2a 2a 20 74 68 65 20 70 61 o mark.** the pa
127d0 67 65 73 20 61 73 20 63 6c 65 61 6e 2e 0a 2a 2f ges as clean..*/
127d1 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
127d2 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 r_write_pagelist
127d3 28 50 67 48 64 72 20 2a 70 4c 69 73 74 29 7b 0a (PgHdr *pList){.
127d4 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b Pager *pPager;
127d5 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 . int rc;.. if
127d6 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 ( pList==0 ) ret
127d7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
127d8 20 70 50 61 67 65 72 20 3d 20 70 4c 69 73 74 2d pPager = pList-
127d9 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a 20 41 >pPager;.. /* A
127da 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 t this point the
127db 72 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 re may be either
127dc 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 a RESERVED or E
127dd 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e XCLUSIVE lock on
127de 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 the. ** databa
127df 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 se file. If ther
127e0 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e 20 e is already an
127e1 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 EXCLUSIVE lock,
127e2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 the following.
127e3 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 ** calls to sqli
127e4 74 65 33 4f 73 4c 6f 63 6b 28 29 20 61 72 65 20 te3OsLock() are
127e5 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2a 0a 20 20 2a no-ops.. **. *
127e6 2a 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f 63 * Moving the loc
127e7 6b 20 66 72 6f 6d 20 52 45 53 45 52 56 45 44 20 k from RESERVED
127e8 74 6f 20 45 58 43 4c 55 53 49 56 45 20 61 63 74 to EXCLUSIVE act
127e9 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20 67 ually involves g
127ea 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75 67 oing. ** throug
127eb 68 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 h an intermediat
127ec 65 20 73 74 61 74 65 20 50 45 4e 44 49 4e 47 2e e state PENDING.
127ed 20 20 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 A PENDING loc
127ee 6b 20 70 72 65 76 65 6e 74 73 20 6e 65 77 0a 20 k prevents new.
127ef 20 2a 2a 20 72 65 61 64 65 72 73 20 66 72 6f 6d ** readers from
127f0 20 61 74 74 61 63 68 69 6e 67 20 74 6f 20 74 68 attaching to th
127f1 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 69 e database but i
127f2 73 20 75 6e 73 75 66 66 69 63 69 65 6e 74 20 66 s unsufficient f
127f3 6f 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77 72 or us to. ** wr
127f4 69 74 65 2e 20 20 54 68 65 20 69 64 65 61 20 6f ite. The idea o
127f5 66 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b f a PENDING lock
127f6 20 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20 6e is to prevent n
127f7 65 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d 0a ew readers from.
127f8 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 77 ** coming in w
127f9 68 69 6c 65 20 77 65 20 77 61 69 74 20 66 6f 72 hile we wait for
127fa 20 65 78 69 73 74 69 6e 67 20 72 65 61 64 65 72 existing reader
127fb 73 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a 2a s to clear.. **
127fc 0a 20 20 2a 2a 20 57 68 69 6c 65 20 74 68 65 20 . ** While the
127fd 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 pager is in the
127fe 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 RESERVED state,
127ff 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 the original dat
12800 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 abase file. **
12801 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 is unchanged and
12802 20 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63 6b we can rollback
12803 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
12804 74 6f 20 70 6c 61 79 62 61 63 6b 20 74 68 65 0a to playback the.
12805 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e 74 ** journal int
12806 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 o the original d
12807 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4f atabase file. O
12808 6e 63 65 20 77 65 20 74 72 61 6e 73 69 74 69 6f nce we transitio
12809 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 53 n to. ** EXCLUS
1280a 49 56 45 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 IVE, it means th
1280b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1280c 68 61 73 20 62 65 65 6e 20 63 68 61 6e 67 65 64 has been changed
1280d 20 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61 63 and any rollbac
1280e 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71 75 k. ** will requ
1280f 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70 6c ire a journal pl
12810 61 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 72 ayback.. */. r
12811 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f c = pager_wait_o
12812 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 n_lock(pPager, E
12813 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a XCLUSIVE_LOCK);.
12814 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12815 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 _OK ){. retur
12816 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 n rc;. }.. whi
12817 6c 65 28 20 70 4c 69 73 74 20 29 7b 0a 0a 20 20 le( pList ){..
12818 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 /* If the file
12819 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 has not yet bee
1281a 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69 n opened, open i
1281b 74 20 6e 6f 77 2e 20 2a 2f 0a 20 20 20 20 69 66 t now. */. if
1281c 28 20 21 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 ( !pPager->fd->p
1281d 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 Methods ){.
1281e 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e assert(pPager->
1281f 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 tempFile);.
12820 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
12821 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65 erOpentemp(pPage
12822 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 r, pPager->fd, p
12823 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 29 Pager->vfsFlags)
12824 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
12825 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
12826 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
12827 72 65 20 61 72 65 20 64 69 72 74 79 20 70 61 67 re are dirty pag
12828 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 es in the page c
12829 61 63 68 65 20 77 69 74 68 20 70 61 67 65 20 6e ache with page n
1282a 75 6d 62 65 72 73 20 67 72 65 61 74 65 72 0a 20 umbers greater.
1282b 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 ** than Pager
1282c 2e 64 62 53 69 7a 65 2c 20 74 68 69 73 20 6d 65 .dbSize, this me
1282d 61 6e 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 ans sqlite3Pager
1282e 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20 TruncateImage()
1282f 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 20 was called to.
12830 20 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 69 ** make the fi
12831 6c 65 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 73 le smaller (pres
12832 75 6d 61 62 6c 79 20 62 79 20 61 75 74 6f 2d 76 umably by auto-v
12833 61 63 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f 20 acuum code). Do
12834 6e 6f 74 20 77 72 69 74 65 0a 20 20 20 20 2a 2a not write. **
12835 20 61 6e 79 20 73 75 63 68 20 70 61 67 65 73 20 any such pages
12836 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 to the file..
12837 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c 69 73 */. if( pLis
12838 74 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d t->pgno<=pPager-
12839 3e 64 62 53 69 7a 65 20 26 26 20 30 3d 3d 28 70 >dbSize && 0==(p
1283a 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 47 48 44 List->flags&PGHD
1283b 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20 29 7b R_DONT_WRITE) ){
1283c 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66 73 65 . i64 offse
1283d 74 20 3d 20 28 70 4c 69 73 74 2d 3e 70 67 6e 6f t = (pList->pgno
1283e 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d -1)*(i64)pPager-
1283f 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 >pageSize;.
12840 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 43 char *pData = C
12841 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 4c ODEC2(pPager, pL
12842 69 73 74 2d 3e 70 44 61 74 61 2c 20 70 4c 69 73 ist->pData, pLis
12843 74 2d 3e 70 67 6e 6f 2c 20 36 29 3b 0a 0a 20 20 t->pgno, 6);..
12844 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 PAGERTRACE((
12845 22 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 "STORE %d page %
12846 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c d hash(%08x)\n",
12847 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
12848 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 PAGERID(pPag
12849 65 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f er), pList->pgno
1284a 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 , pager_pagehash
1284b 28 70 4c 69 73 74 29 29 29 3b 0a 20 20 20 20 20 (pList)));.
1284c 20 49 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54 IOTRACE(("PGOUT
1284d 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 %p %d\n", pPage
1284e 72 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 29 29 r, pList->pgno))
1284f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
12850 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
12851 65 72 2d 3e 66 64 2c 20 70 44 61 74 61 2c 20 70 er->fd, pData, p
12852 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c Pager->pageSize,
12853 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 offset);.
12854 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 PAGER_INCR(sqlit
12855 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 e3_pager_writedb
12856 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 _count);. P
12857 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 AGER_INCR(pPager
12858 2d 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 20 ->nWrite);.
12859 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 67 6e 6f if( pList->pgno
1285a 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6d ==1 ){. m
1285b 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 emcpy(&pPager->d
1285c 62 46 69 6c 65 56 65 72 73 2c 20 26 70 44 61 74 bFileVers, &pDat
1285d 61 5b 32 34 5d 2c 20 73 69 7a 65 6f 66 28 70 50 a[24], sizeof(pP
1285e 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 ager->dbFileVers
1285f 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ));. }.
12860 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 67 6e if( pList->pgn
12861 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 o>pPager->dbFile
12862 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Size ){.
12863 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 pPager->dbFileSi
12864 7a 65 20 3d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f ze = pList->pgno
12865 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
12866 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
12867 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 else{. P
12868 41 47 45 52 54 52 41 43 45 28 28 22 4e 4f 53 54 AGERTRACE(("NOST
12869 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 5c 6e ORE %d page %d\n
1286a 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
1286b 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 29 r), pList->pgno)
1286c 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
1286d 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
1286e 75 72 6e 20 72 63 3b 0a 23 69 66 64 65 66 20 53 urn rc;.#ifdef S
1286f 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
12870 53 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61 67 S. pList->pag
12871 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 eHash = pager_pa
12872 67 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a 23 gehash(pList);.#
12873 65 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74 20 endif. pList
12874 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79 3b = pList->pDirty;
12875 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 . }.. return S
12876 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
12877 2a 2a 20 41 64 64 20 74 68 65 20 70 61 67 65 20 ** Add the page
12878 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e to the sub-journ
12879 61 6c 2e 20 49 74 20 69 73 20 74 68 65 20 63 61 al. It is the ca
1287a 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69 llers responsibi
1287b 6c 69 74 79 20 74 6f 0a 2a 2a 20 75 73 65 20 73 lity to.** use s
1287c 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 ubjRequiresPage(
1287d 29 20 74 6f 20 63 68 65 63 6b 20 74 68 61 74 20 ) to check that
1287e 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72 65 71 it is really req
1287f 75 69 72 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a uired before .**
12880 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 calling this fu
12881 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 nction..*/.stati
12882 63 20 69 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c c int subjournal
12883 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 Page(PgHdr *pPg)
12884 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 76 6f {. int rc;. vo
12885 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d id *pData = pPg-
12886 3e 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 >pData;. Pager
12887 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
12888 50 61 67 65 72 3b 0a 20 20 69 36 34 20 6f 66 66 Pager;. i64 off
12889 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 set = pPager->st
1288a 6d 74 4e 52 65 63 2a 28 34 2b 70 50 61 67 65 72 mtNRec*(4+pPager
1288b 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 63 ->pageSize);. c
1288c 68 61 72 20 2a 70 44 61 74 61 32 20 3d 20 43 4f har *pData2 = CO
1288d 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 61 DEC2(pPager, pDa
1288e 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 ta, pPg->pgno, 7
1288f 29 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 );.. PAGERTRACE
12890 28 28 22 53 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 (("STMT-JOURNAL
12891 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 %d page %d\n", P
12892 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
12893 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 0a 20 20 pPg->pgno));..
12894 61 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f assert( pageInJo
12895 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 urnal(pPg) || pP
12896 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e g->pgno>pPager->
12897 64 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 dbOrigSize );.
12898 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 rc = write32bits
12899 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f (pPager->sjfd, o
1289a 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f ffset, pPg->pgno
1289b 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1289c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 ITE_OK ){. rc
1289d 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
1289e 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 e(pPager->sjfd,
1289f 70 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e pData2, pPager->
128a0 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 pageSize, offset
128a1 2b 34 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 +4);. }. if( r
128a2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
128a3 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 pPager->stmt
128a4 4e 52 65 63 2b 2b 3b 0a 20 20 20 20 61 73 73 65 NRec++;. asse
128a5 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 rt( pPager->nSav
128a6 65 70 6f 69 6e 74 3e 30 20 29 3b 0a 20 20 20 20 epoint>0 );.
128a7 72 63 20 3d 20 61 64 64 54 6f 53 61 76 65 70 6f rc = addToSavepo
128a8 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 intBitvecs(pPage
128a9 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 r, pPg->pgno);.
128aa 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
128ab 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 }.../*.** This f
128ac 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
128ad 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20 d by the pcache
128ae 6c 61 79 65 72 20 77 68 65 6e 20 69 74 20 68 61 layer when it ha
128af 73 20 72 65 61 63 68 65 64 20 73 6f 6d 65 0a 2a s reached some.*
128b0 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c 69 * soft memory li
128b1 6d 69 74 2e 20 54 68 65 20 61 72 67 75 6d 65 6e mit. The argumen
128b2 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 t is a pointer t
128b3 6f 20 61 20 70 75 72 67 65 61 62 6c 65 20 50 61 o a purgeable Pa
128b4 67 65 72 20 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 ger .** object.
128b5 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 This function at
128b6 74 65 6d 70 74 73 20 74 6f 20 6d 61 6b 65 20 61 tempts to make a
128b7 20 73 69 6e 67 6c 65 20 64 69 72 74 79 20 70 61 single dirty pa
128b8 67 65 20 74 68 61 74 20 68 61 73 20 6e 6f 0a 2a ge that has no.*
128b9 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 * outstanding re
128ba 66 65 72 65 6e 63 65 73 20 28 69 66 20 6f 6e 65 ferences (if one
128bb 20 65 78 69 73 74 73 29 20 63 6c 65 61 6e 20 73 exists) clean s
128bc 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 o that it can be
128bd 20 72 65 63 79 63 6c 65 64 20 0a 2a 2a 20 62 79 recycled .** by
128be 20 74 68 65 20 70 63 61 63 68 65 20 6c 61 79 65 the pcache laye
128bf 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
128c0 20 70 61 67 65 72 53 74 72 65 73 73 28 76 6f 69 pagerStress(voi
128c1 64 20 2a 70 2c 20 50 67 48 64 72 20 2a 70 50 67 d *p, PgHdr *pPg
128c2 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 ){. Pager *pPag
128c3 65 72 20 3d 20 28 50 61 67 65 72 20 2a 29 70 3b er = (Pager *)p;
128c4 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
128c5 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 50 TE_OK;.. if( pP
128c6 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 ager->doNotSync
128c7 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
128c8 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
128c9 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 assert( pPg->fla
128ca 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 gs&PGHDR_DIRTY )
128cb 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
128cc 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f errCode==SQLITE_
128cd 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 OK ){. if( pP
128ce 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
128cf 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 EED_SYNC ){.
128d0 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e rc = syncJourn
128d1 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 al(pPager);.
128d2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
128d3 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 66 _OK && pPager->f
128d4 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 20 20 20 ullSync && .
128d5 20 20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f !(pPager->jo
128d6 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
128d7 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
128d8 4f 52 59 29 20 26 26 0a 20 20 20 20 20 20 20 20 ORY) &&.
128d9 21 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 !(sqlite3OsDevic
128da 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
128db 28 70 50 61 67 65 72 2d 3e 66 64 29 26 53 51 4c (pPager->fd)&SQL
128dc 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 ITE_IOCAP_SAFE_A
128dd 50 50 45 4e 44 29 0a 20 20 20 20 20 20 29 7b 0a PPEND). ){.
128de 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
128df 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 nRec = 0;.
128e0 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 rc = writeJour
128e1 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a nalHdr(pPager);.
128e2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
128e3 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
128e4 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 67 _OK ){. pPg
128e5 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 ->pDirty = 0;.
128e6 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e if( pPg->pgn
128e7 6f 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 o>pPager->dbSize
128e8 20 26 26 20 73 75 62 6a 52 65 71 75 69 72 65 73 && subjRequires
128e9 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 Page(pPg) ){.
128ea 20 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 rc = subjou
128eb 72 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 rnalPage(pPg);.
128ec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
128ed 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
128ee 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 {. rc = p
128ef 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c ager_write_pagel
128f0 69 73 74 28 70 50 67 29 3b 0a 20 20 20 20 20 20 ist(pPg);.
128f1 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
128f2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
128f3 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 . pager_err
128f4 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a or(pPager, rc);.
128f5 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
128f6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
128f7 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 {. PAGERTRACE
128f8 28 28 22 53 54 52 45 53 53 20 25 64 20 70 61 67 (("STRESS %d pag
128f9 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 e %d\n", PAGERID
128fa 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 (pPager), pPg->p
128fb 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 gno));. sqlit
128fc 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 e3PcacheMakeClea
128fd 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 n(pPg);. }. re
128fe 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
128ff 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 ** Return 1 if t
12900 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f here is a hot jo
12901 75 72 6e 61 6c 20 6f 6e 20 74 68 65 20 67 69 76 urnal on the giv
12902 65 6e 20 70 61 67 65 72 2e 0a 2a 2a 20 41 20 68 en pager..** A h
12903 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e ot journal is on
12904 65 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 e that needs to
12905 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a be played back..
12906 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 **.** If the cur
12907 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 rent size of the
12908 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
12909 73 20 30 20 62 75 74 20 61 20 6a 6f 75 72 6e 61 s 0 but a journa
1290a 6c 20 66 69 6c 65 0a 2a 2a 20 65 78 69 73 74 73 l file.** exists
1290b 2c 20 74 68 61 74 20 69 73 20 70 72 6f 62 61 62 , that is probab
1290c 6c 79 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 ly an old journa
1290d 6c 20 6c 65 66 74 20 6f 76 65 72 20 66 72 6f 6d l left over from
1290e 20 61 20 70 72 69 6f 72 0a 2a 2a 20 64 61 74 61 a prior.** data
1290f 62 61 73 65 20 77 69 74 68 20 74 68 65 20 73 61 base with the sa
12910 6d 65 20 6e 61 6d 65 2e 20 20 4a 75 73 74 20 64 me name. Just d
12911 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 elete the journa
12912 6c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 l..**.** Return
12913 6e 65 67 61 74 69 76 65 20 69 66 20 75 6e 61 62 negative if unab
12914 6c 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 le to determine
12915 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 74 68 the status of th
12916 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a e journal..**.**
12917 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f This routine do
12918 65 73 20 6e 6f 74 20 6f 70 65 6e 20 74 68 65 20 es not open the
12919 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 journal file to
1291a 65 78 61 6d 69 6e 65 20 69 74 73 0a 2a 2a 20 63 examine its.** c
1291b 6f 6e 74 65 6e 74 2e 20 20 48 65 6e 63 65 2c 20 ontent. Hence,
1291c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 the journal migh
1291d 74 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 t contain the na
1291e 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 0a 2a me of a master.*
1291f 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 * journal file t
12920 68 61 74 20 68 61 73 20 62 65 65 6e 20 64 65 6c hat has been del
12921 65 74 65 64 2c 20 61 6e 64 20 68 65 6e 63 65 20 eted, and hence
12922 6e 6f 74 20 62 65 20 68 6f 74 2e 20 20 4f 72 0a not be hot. Or.
12923 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 ** the header of
12924 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 the journal mig
12925 68 74 20 62 65 20 7a 65 72 6f 65 64 20 6f 75 74 ht be zeroed out
12926 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a . This routine.
12927 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 64 69 73 63 ** does not disc
12928 6f 76 65 72 20 74 68 65 73 65 20 63 61 73 65 73 over these cases
12929 20 6f 66 20 61 20 6e 6f 6e 2d 68 6f 74 20 6a 6f of a non-hot jo
1292a 75 72 6e 61 6c 20 2d 20 69 66 20 74 68 65 0a 2a urnal - if the.*
1292b 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 * journal file e
1292c 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74 xists and is not
1292d 20 65 6d 70 74 79 20 74 68 69 73 20 72 6f 75 74 empty this rout
1292e 69 6e 65 20 61 73 73 75 6d 65 73 20 69 74 0a 2a ine assumes it.*
1292f 2a 20 69 73 20 68 6f 74 2e 20 20 54 68 65 20 70 * is hot. The p
12930 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 ager_playback()
12931 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73 routine will dis
12932 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 0a 2a cover that the.*
12933 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 * journal file i
12934 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 s not really hot
12935 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 2d 6f 70 2e and will no-op.
12936 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 .*/.static int h
12937 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 50 61 67 asHotJournal(Pag
12938 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
12939 2a 70 45 78 69 73 74 73 29 7b 0a 20 20 73 71 6c *pExists){. sql
1293a 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d ite3_vfs *pVfs =
1293b 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 pPager->pVfs;.
1293c 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1293d 5f 4f 4b 3b 0a 20 20 69 6e 74 20 65 78 69 73 74 _OK;. int exist
1293e 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6c 6f 63 s = 0;. int loc
1293f 6b 65 64 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 ked = 0;. asser
12940 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a t( pPager!=0 );.
12941 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12942 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a ->useJournal );.
12943 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12944 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 ->fd->pMethods )
12945 3b 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 ;. *pExists = 0
12946 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
12947 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 70 OsAccess(pVfs, p
12948 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
12949 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
1294a 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b XISTS, &exists);
1294b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1294c 45 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 29 E_OK && exists )
1294d 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1294e 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 e3OsCheckReserve
1294f 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 dLock(pPager->fd
12950 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 7d 0a , &locked);. }.
12951 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
12952 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 26 26 _OK && exists &&
12953 20 21 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 !locked ){.
12954 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 72 int nPage;. r
12955 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
12956 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
12957 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 , &nPage);. i
12958 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12959 20 29 7b 0a 20 20 20 20 20 69 66 28 20 6e 50 61 ){. if( nPa
1295a 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ge==0 ){.
1295b 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
1295c 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a (pVfs, pPager->z
1295d 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 Journal, 0);.
1295e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1295f 20 20 2a 70 45 78 69 73 74 73 20 3d 20 31 3b 0a *pExists = 1;.
12960 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
12961 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
12962 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 ../*.** Read the
12963 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 content of page
12964 20 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20 pPg out of the
12965 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
12966 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 /.static int rea
12967 64 44 62 50 61 67 65 28 50 61 67 65 72 20 2a 70 dDbPage(Pager *p
12968 50 61 67 65 72 2c 20 50 67 48 64 72 20 2a 70 50 Pager, PgHdr *pP
12969 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 g, Pgno pgno){.
1296a 20 69 6e 74 20 72 63 3b 0a 20 20 69 36 34 20 6f int rc;. i64 o
1296b 66 66 73 65 74 3b 0a 20 20 61 73 73 65 72 74 28 ffset;. assert(
1296c 20 4d 45 4d 44 42 3d 3d 30 20 29 3b 0a 20 20 61 MEMDB==0 );. a
1296d 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 66 64 ssert(pPager->fd
1296e 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61 67 ->pMethods||pPag
1296f 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20 er->tempFile);.
12970 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 66 64 if( !pPager->fd
12971 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 ->pMethods ){.
12972 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
12973 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
12974 3b 0a 20 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d ;. }. offset =
12975 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 (pgno-1)*(i64)p
12976 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b Pager->pageSize;
12977 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
12978 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 sRead(pPager->fd
12979 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 50 , pPg->pData, pP
1297a 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
1297b 6f 66 66 73 65 74 29 3b 0a 20 20 50 41 47 45 52 offset);. PAGER
1297c 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 _INCR(sqlite3_pa
1297d 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 ger_readdb_count
1297e 29 3b 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 );. PAGER_INCR(
1297f 70 50 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a pPager->nRead);.
12980 20 20 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e IOTRACE(("PGIN
12981 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 %p %d\n", pPage
12982 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 69 66 28 r, pgno));. if(
12983 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 pgno==1 ){.
12984 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e memcpy(&pPager->
12985 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28 75 dbFileVers, &((u
12986 38 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 5b 32 8*)pPg->pData)[2
12987 34 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 4],.
12988 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12989 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1298a 20 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d sizeof(pPager-
1298b 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 >dbFileVers));.
1298c 20 7d 0a 20 20 43 4f 44 45 43 31 28 70 50 61 67 }. CODEC1(pPag
1298d 65 72 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 er, pPg->pData,
1298e 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a 20 pPg->pgno, 3);.
1298f 20 50 41 47 45 52 54 52 41 43 45 28 28 22 46 45 PAGERTRACE(("FE
12990 54 43 48 20 25 64 20 70 61 67 65 20 25 64 20 68 TCH %d page %d h
12991 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 ash(%08x)\n",.
12992 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 PAG
12993 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 ERID(pPager), pP
12994 67 2d 3e 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 g->pgno, pager_p
12995 61 67 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a agehash(pPg)));.
12996 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
12997 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
12998 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 tion is called t
12999 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 73 68 61 o obtain the sha
1299a 72 65 64 20 6c 6f 63 6b 20 72 65 71 75 69 72 65 red lock require
1299b 64 20 62 65 66 6f 72 65 0a 2a 2a 20 64 61 74 61 d before.** data
1299c 20 6d 61 79 20 62 65 20 72 65 61 64 20 66 72 6f may be read fro
1299d 6d 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 m the pager cach
1299e 65 2e 20 49 66 20 74 68 65 20 73 68 61 72 65 64 e. If the shared
1299f 20 6c 6f 63 6b 20 68 61 73 20 61 6c 72 65 61 64 lock has alread
129a0 79 0a 2a 2a 20 62 65 65 6e 20 6f 62 74 61 69 6e y.** been obtain
129a1 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f ed, this functio
129a2 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a n is a no-op..**
129a3 0a 2a 2a 20 49 6d 6d 65 64 69 61 74 65 6c 79 20 .** Immediately
129a4 61 66 74 65 72 20 6f 62 74 61 69 6e 69 6e 67 20 after obtaining
129a5 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 the shared lock
129a6 28 69 66 20 72 65 71 75 69 72 65 64 29 2c 20 74 (if required), t
129a7 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 his function.**
129a8 63 68 65 63 6b 73 20 66 6f 72 20 61 20 68 6f 74 checks for a hot
129a9 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 -journal file. I
129aa 66 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 f one is found,
129ab 61 6e 20 65 6d 65 72 67 65 6e 63 79 20 72 6f 6c an emergency rol
129ac 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 70 65 72 66 lback.** is perf
129ad 6f 72 6d 65 64 20 69 6d 6d 65 64 69 61 74 65 6c ormed immediatel
129ae 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 y..*/.static int
129af 20 70 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b pagerSharedLock
129b0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
129b1 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
129b2 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 73 45 TE_OK;. int isE
129b3 72 72 6f 72 52 65 73 65 74 20 3d 20 30 3b 0a 0a rrorReset = 0;..
129b4 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 /* If this dat
129b5 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 abase is opened
129b6 66 6f 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 for exclusive ac
129b7 63 65 73 73 2c 20 68 61 73 20 6e 6f 20 6f 75 74 cess, has no out
129b8 73 74 61 6e 64 69 6e 67 20 0a 20 20 2a 2a 20 70 standing . ** p
129b9 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 61 age references a
129ba 6e 64 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f nd is in an erro
129bb 72 2d 73 74 61 74 65 2c 20 6e 6f 77 20 69 73 20 r-state, now is
129bc 74 68 65 20 63 68 61 6e 63 65 20 74 6f 20 63 6c the chance to cl
129bd 65 61 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 72 ear. ** the err
129be 6f 72 2e 20 44 69 73 63 61 72 64 20 74 68 65 20 or. Discard the
129bf 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
129c0 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20 pager-cache and
129c1 74 72 65 61 74 20 61 6e 79 0a 20 20 2a 2a 20 6f treat any. ** o
129c2 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 pen journal file
129c3 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 as a hot-journa
129c4 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 4d l.. */. if( !M
129c5 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e EMDB && pPager->
129c6 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 exclusiveMode .
129c7 20 20 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 && sqlite3Pcac
129c8 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 heRefCount(pPage
129c9 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 26 r->pPCache)==0 &
129ca 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 & pPager->errCod
129cb 65 20 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 e . ){. if(
129cc 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
129cd 70 65 6e 20 29 7b 0a 20 20 20 20 20 20 69 73 45 pen ){. isE
129ce 72 72 6f 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 rrorReset = 1;.
129cf 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d }. pPager-
129d0 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 >errCode = SQLIT
129d1 45 5f 4f 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f E_OK;. pager_
129d2 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 reset(pPager);.
129d3 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
129d4 70 61 67 65 72 20 69 73 20 73 74 69 6c 6c 20 69 pager is still i
129d5 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 n an error state
129d6 2c 20 64 6f 20 6e 6f 74 20 70 72 6f 63 65 65 64 , do not proceed
129d7 2e 20 54 68 65 20 65 72 72 6f 72 20 0a 20 20 2a . The error . *
129d8 2a 20 73 74 61 74 65 20 77 69 6c 6c 20 62 65 20 * state will be
129d9 63 6c 65 61 72 65 64 20 61 74 20 73 6f 6d 65 20 cleared at some
129da 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 point in the fut
129db 75 72 65 20 77 68 65 6e 20 61 6c 6c 20 70 61 67 ure when all pag
129dc 65 20 0a 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 e . ** referenc
129dd 65 73 20 61 72 65 20 64 72 6f 70 70 65 64 20 61 es are dropped a
129de 6e 64 20 74 68 65 20 63 61 63 68 65 20 63 61 6e nd the cache can
129df 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 0a 20 be discarded..
129e0 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 */. if( pPager
129e1 2d 3e 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 ->errCode && pPa
129e2 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 ger->errCode!=SQ
129e3 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 LITE_FULL ){.
129e4 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
129e5 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 errCode;. }..
129e6 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 if( pPager->stat
129e7 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 e==PAGER_UNLOCK
129e8 7c 7c 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 || isErrorReset
129e9 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 ){. sqlite3_v
129ea 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 fs *pVfs = pPage
129eb 72 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 r->pVfs;. int
129ec 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 3d 20 isHotJournal =
129ed 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 0;. assert( !
129ee 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 73 MEMDB );. ass
129ef 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 ert( sqlite3Pcac
129f0 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 heRefCount(pPage
129f1 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 r->pPCache)==0 )
129f2 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 ;. if( !pPage
129f3 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b r->noReadlock ){
129f4 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
129f5 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 r_wait_on_lock(p
129f6 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c 4f Pager, SHARED_LO
129f7 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 CK);. if( r
129f8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
129f9 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
129fa 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
129fb 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 AGER_UNLOCK );.
129fc 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 61 return pa
129fd 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 ger_error(pPager
129fe 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 , rc);. }.
129ff 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 }else if( pPa
12a00 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 ger->state==PAGE
12a01 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 R_UNLOCK ){.
12a02 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
12a03 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a = PAGER_SHARED;.
12a04 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
12a05 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
12a06 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
12a07 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 6a 6f 75 . /* If a jou
12a08 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 rnal file exists
12a09 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e , and there is n
12a0a 6f 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 o RESERVED lock
12a0b 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 on the. ** da
12a0c 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 tabase file, the
12a0d 6e 20 69 74 20 65 69 74 68 65 72 20 6e 65 65 64 n it either need
12a0e 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62 s to be played b
12a0f 61 63 6b 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a ack or deleted..
12a10 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
12a11 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b 0a isErrorReset ){.
12a12 20 20 20 20 20 20 72 63 20 3d 20 68 61 73 48 6f rc = hasHo
12a13 74 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c tJournal(pPager,
12a14 20 26 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b &isHotJournal);
12a15 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
12a16 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
12a17 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b goto failed;
12a18 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
12a19 20 20 20 69 66 28 20 69 73 45 72 72 6f 72 52 65 if( isErrorRe
12a1a 73 65 74 20 7c 7c 20 69 73 48 6f 74 4a 6f 75 72 set || isHotJour
12a1b 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 nal ){. /*
12a1c 47 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 45 Get an EXCLUSIVE
12a1d 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
12a1e 61 62 61 73 65 20 66 69 6c 65 2e 20 41 74 20 74 abase file. At t
12a1f 68 69 73 20 70 6f 69 6e 74 20 69 74 20 69 73 0a his point it is.
12a20 20 20 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74 61 ** importa
12a21 6e 74 20 74 68 61 74 20 61 20 52 45 53 45 52 56 nt that a RESERV
12a22 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 6f ED lock is not o
12a23 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 77 btained on the w
12a24 61 79 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20 ay to the.
12a25 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 ** EXCLUSIVE loc
12a26 6b 2e 20 49 66 20 69 74 20 77 65 72 65 2c 20 61 k. If it were, a
12a27 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d nother process m
12a28 69 67 68 74 20 6f 70 65 6e 20 74 68 65 0a 20 20 ight open the.
12a29 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
12a2a 66 69 6c 65 2c 20 64 65 74 65 63 74 20 74 68 65 file, detect the
12a2b 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 RESERVED lock,
12a2c 61 6e 64 20 63 6f 6e 63 6c 75 64 65 20 74 68 61 and conclude tha
12a2d 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 t the. ** d
12a2e 61 74 61 62 61 73 65 20 69 73 20 73 61 66 65 20 atabase is safe
12a2f 74 6f 20 72 65 61 64 20 77 68 69 6c 65 20 74 68 to read while th
12a30 69 73 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 is process is st
12a31 69 6c 6c 20 72 6f 6c 6c 69 6e 67 20 69 74 20 0a ill rolling it .
12a32 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 2e 0a 20 ** back..
12a33 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 2a ** . *
12a34 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 69 6e * Because the in
12a35 74 65 72 6d 65 64 69 61 74 65 20 52 45 53 45 52 termediate RESER
12a36 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 VED lock is not
12a37 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 0a 20 requested, the.
12a38 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 20 70 ** second p
12a39 72 6f 63 65 73 73 20 77 69 6c 6c 20 67 65 74 20 rocess will get
12a3a 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e to this point in
12a3b 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20 66 61 the code and fa
12a3c 69 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 6f il to. ** o
12a3d 62 74 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58 btain its own EX
12a3e 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 CLUSIVE lock on
12a3f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12a40 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
12a41 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 if( pPager->st
12a42 61 74 65 3c 45 58 43 4c 55 53 49 56 45 5f 4c 4f ate<EXCLUSIVE_LO
12a43 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 CK ){. rc
12a44 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b = sqlite3OsLock
12a45 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 45 58 43 (pPager->fd, EXC
12a46 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 LUSIVE_LOCK);.
12a47 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
12a48 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
12a49 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
12a4a 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 error(pPager, rc
12a4b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 );. got
12a4c 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 o failed;.
12a4d 20 20 7d 0a 20 20 20 20 20 20 20 20 70 50 61 67 }. pPag
12a4e 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
12a4f 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 R_EXCLUSIVE;.
12a50 20 20 20 7d 0a 20 0a 20 20 20 20 20 20 2f 2a 20 }. . /*
12a51 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c Open the journal
12a52 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 for read/write
12a53 61 63 63 65 73 73 2e 20 54 68 69 73 20 69 73 20 access. This is
12a54 62 65 63 61 75 73 65 20 69 6e 20 0a 20 20 20 20 because in .
12a55 20 20 2a 2a 20 65 78 63 6c 75 73 69 76 65 2d 61 ** exclusive-a
12a56 63 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20 66 ccess mode the f
12a57 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 ile descriptor w
12a58 69 6c 6c 20 62 65 20 6b 65 70 74 20 6f 70 65 6e ill be kept open
12a59 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 70 6f and. ** po
12a5a 73 73 69 62 6c 79 20 75 73 65 64 20 66 6f 72 20 ssibly used for
12a5b 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6c 61 a transaction la
12a5c 74 65 72 20 6f 6e 2e 20 4f 6e 20 73 6f 6d 65 20 ter on. On some
12a5d 73 79 73 74 65 6d 73 2c 20 74 68 65 0a 20 20 20 systems, the.
12a5e 20 20 20 2a 2a 20 4f 73 54 72 75 6e 63 61 74 65 ** OsTruncate
12a5f 28 29 20 63 61 6c 6c 20 75 73 65 64 20 69 6e 20 () call used in
12a60 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 exclusive-access
12a61 20 6d 6f 64 65 20 61 6c 73 6f 20 72 65 71 75 69 mode also requi
12a62 72 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 72 res. ** a r
12a63 65 61 64 2f 77 72 69 74 65 20 66 69 6c 65 20 68 ead/write file h
12a64 61 6e 64 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a andle.. */.
12a65 20 20 20 20 20 20 69 66 28 20 21 69 73 45 72 72 if( !isErr
12a66 6f 72 52 65 73 65 74 20 26 26 20 70 50 61 67 65 orReset && pPage
12a67 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d r->journalOpen==
12a68 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 0 ){. int
12a69 20 72 65 73 3b 0a 20 20 20 20 20 20 20 20 72 63 res;. rc
12a6a 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 = sqlite3OsAcce
12a6b 73 73 28 70 56 66 73 2c 70 50 61 67 65 72 2d 3e ss(pVfs,pPager->
12a6c 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49 54 45 5f zJournal,SQLITE_
12a6d 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 26 72 ACCESS_EXISTS,&r
12a6e 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 es);. if(
12a6f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
12a70 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
12a71 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 res ){.
12a72 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b int fout = 0;
12a73 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 . int
12a74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e f = SQLITE_OPEN
12a75 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 _READWRITE|SQLIT
12a76 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 E_OPEN_MAIN_JOUR
12a77 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 NAL;.
12a78 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 assert( !pPager
12a79 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 ->tempFile );.
12a7a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
12a7b 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
12a7c 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
12a7d 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 nal, pPager->jfd
12a7e 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 , f, &fout);.
12a7f 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
12a80 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
12a81 7c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d 3e 70 | pPager->jfd->p
12a82 4d 65 74 68 6f 64 73 20 29 3b 0a 20 20 20 20 20 Methods );.
12a83 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
12a84 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75 74 QLITE_OK && fout
12a85 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 &SQLITE_OPEN_REA
12a86 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 20 DONLY ){.
12a87 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
12a88 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 TE_CANTOPEN;.
12a89 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
12a8a 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 e3OsClose(pPager
12a8b 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 ->jfd);.
12a8c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
12a8d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
12a8e 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 /* If the jou
12a8f 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65 78 rnal does not ex
12a90 69 73 74 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 ist, that means
12a91 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 some other proce
12a92 73 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a ss. *
12a93 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 72 6f * has already ro
12a94 6c 6c 65 64 20 69 74 20 62 61 63 6b 20 2a 2f 0a lled it back */.
12a95 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
12a96 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
12a97 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
12a98 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
12a99 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12a9a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
12a9b 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
12a9c 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 }. pPager
12a9d 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 3d 20 ->journalOpen =
12a9e 31 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 1;. pPager-
12a9f 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 >journalStarted
12aa0 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 = 0;. pPage
12aa1 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
12aa2 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 0;. pPager-
12aa3 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a >setMaster = 0;.
12aa4 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
12aa5 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 0a urnalHdr = 0;. .
12aa6 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 63 /* Playbac
12aa7 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 k and delete the
12aa8 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 20 journal. Drop
12aa9 74 68 65 20 64 61 74 61 62 61 73 65 20 77 72 69 the database wri
12aaa 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b te. ** lock
12aab 20 61 6e 64 20 72 65 61 63 71 75 69 72 65 20 74 and reacquire t
12aac 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50 75 he read lock. Pu
12aad 72 67 65 20 74 68 65 20 63 61 63 68 65 20 62 65 rge the cache be
12aae 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 70 6c fore. ** pl
12aaf 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20 68 aying back the h
12ab0 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74 68 ot-journal so th
12ab1 61 74 20 77 65 20 64 6f 6e 27 74 20 65 6e 64 20 at we don't end
12ab2 75 70 20 77 69 74 68 0a 20 20 20 20 20 20 2a 2a up with. **
12ab3 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 an inconsistent
12ab4 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2f cache.. */
12ab5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 . sqlite3Pc
12ab6 61 63 68 65 43 6c 65 61 72 28 70 50 61 67 65 72 acheClear(pPager
12ab7 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20 ->pPCache);.
12ab8 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 rc = pager_pla
12ab9 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31 29 yback(pPager, 1)
12aba 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
12abb 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
12abc 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
12abd 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 error(pPager, rc
12abe 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 );. goto
12abf 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a failed;. }.
12ac0 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61 assert(pPa
12ac1 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 ger->state==PAGE
12ac2 52 5f 53 48 41 52 45 44 20 7c 7c 20 0a 20 20 20 R_SHARED || .
12ac3 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e (pPager->
12ac4 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 exclusiveMode &&
12ac5 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50 pPager->state>P
12ac6 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 AGER_SHARED).
12ac7 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 );. }..
12ac8 20 69 66 28 20 73 71 6c 69 74 65 33 50 63 61 63 if( sqlite3Pcac
12ac9 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 hePagecount(pPag
12aca 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 er->pPCache)>0 )
12acb 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 {. /* The s
12acc 68 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a hared-lock has j
12acd 75 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65 ust been acquire
12ace 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 d on the databas
12acf 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 e file. **
12ad0 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c and there are al
12ad1 72 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74 ready pages in t
12ad2 68 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61 he cache (from a
12ad3 20 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 previous.
12ad4 2a 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 ** read or write
12ad5 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 transaction).
12ad6 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 Check to see if
12ad7 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 the database.
12ad8 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d ** has been m
12ad9 6f 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 odified. If the
12ada 20 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68 database has ch
12adb 61 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65 anged, flush the
12adc 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e . ** cache.
12add 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
12ade 2a 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e ** Database chan
12adf 67 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 ges is detected
12ae0 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 by looking at 15
12ae1 20 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 bytes beginning
12ae2 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 . ** at off
12ae3 73 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20 set 24 into the
12ae4 66 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 file. The first
12ae5 20 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62 4 of these 16 b
12ae6 79 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a ytes are. *
12ae7 2a 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 * a 32-bit count
12ae8 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 er that is incre
12ae9 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 mented with each
12aea 20 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 change. The.
12aeb 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 ** other byt
12aec 65 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d es change random
12aed 6c 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c ly with each fil
12aee 65 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 e change when.
12aef 20 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 ** a codec i
12af0 73 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 s in use..
12af1 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 ** . ** The
12af2 72 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e re is a vanishin
12af3 67 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 gly small chance
12af4 20 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77 that a change w
12af5 69 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 ill not be .
12af6 20 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 ** detected.
12af7 54 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e The chance of an
12af8 20 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e undetected chan
12af9 67 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 ge is so small t
12afa 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 hat. ** it
12afb 63 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 can be neglected
12afc 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
12afd 20 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 char dbFileVers
12afe 5b 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e [sizeof(pPager->
12aff 64 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 dbFileVers)];.
12b00 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
12b01 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
12b02 2c 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 , 0);.. if(
12b03 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
12b04 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
12b05 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
12b06 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 ;. goto f
12b07 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a ailed;. }..
12b08 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
12b09 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 ager->dbSizeVali
12b0a 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 d );. if( p
12b0b 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 Pager->dbSize>0
12b0c 29 7b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 ){. IOTRA
12b0d 43 45 28 28 22 43 4b 56 45 52 53 20 25 70 20 25 CE(("CKVERS %p %
12b0e 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 73 69 d\n", pPager, si
12b0f 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 zeof(dbFileVers)
12b10 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ));. rc =
12b11 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 sqlite3OsRead(p
12b12 50 61 67 65 72 2d 3e 66 64 2c 20 26 64 62 46 69 Pager->fd, &dbFi
12b13 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 leVers, sizeof(d
12b14 62 46 69 6c 65 56 65 72 73 29 2c 20 32 34 29 3b bFileVers), 24);
12b15 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
12b16 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
12b17 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 goto fai
12b18 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 led;. }.
12b19 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
12b1a 20 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c memset(dbFil
12b1b 65 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 eVers, 0, sizeof
12b1c 28 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 (dbFileVers));.
12b1d 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 }.. if
12b1e 28 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 2d ( memcmp(pPager-
12b1f 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46 >dbFileVers, dbF
12b20 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 ileVers, sizeof(
12b21 64 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30 20 dbFileVers))!=0
12b22 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 ){. pager
12b23 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a _reset(pPager);.
12b24 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
12b25 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12b26 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 ->exclusiveMode
12b27 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 || pPager->state
12b28 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 ==PAGER_SHARED )
12b29 3b 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a ;. }.. failed:.
12b2a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12b2b 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 _OK ){. /* pa
12b2c 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 ger_unlock() is
12b2d 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c a no-op for excl
12b2e 75 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 usive mode and i
12b2f 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
12b30 65 73 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 es. */. pager
12b31 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b _unlock(pPager);
12b32 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
12b33 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 ;.}../*.** Make
12b34 73 75 72 65 20 77 65 20 68 61 76 65 20 74 68 65 sure we have the
12b35 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 61 20 70 content for a p
12b36 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61 67 age. If the pag
12b37 65 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 e was.** previou
12b38 73 6c 79 20 61 63 71 75 69 72 65 64 20 77 69 74 sly acquired wit
12b39 68 20 6e 6f 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20 h noContent==1,
12b3a 74 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 then the content
12b3b 20 77 61 73 0a 2a 2a 20 6a 75 73 74 20 69 6e 69 was.** just ini
12b3c 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f tialized to zero
12b3d 73 20 69 6e 73 74 65 61 64 20 6f 66 20 62 65 69 s instead of bei
12b3e 6e 67 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73 ng read from dis
12b3f 6b 2e 0a 2a 2a 20 42 75 74 20 6e 6f 77 20 77 65 k..** But now we
12b40 20 6e 65 65 64 20 74 68 65 20 72 65 61 6c 20 64 need the real d
12b41 61 74 61 20 6f 66 66 20 6f 66 20 64 69 73 6b 2e ata off of disk.
12b42 20 20 53 6f 20 6d 61 6b 65 20 73 75 72 65 20 77 So make sure w
12b43 65 0a 2a 2a 20 68 61 76 65 20 69 74 2e 20 20 52 e.** have it. R
12b44 65 61 64 20 69 74 20 69 6e 20 69 66 20 77 65 20 ead it in if we
12b45 64 6f 20 6e 6f 74 20 68 61 76 65 20 69 74 20 61 do not have it a
12b46 6c 72 65 61 64 79 2e 0a 2a 2f 0a 73 74 61 74 69 lready..*/.stati
12b47 63 20 69 6e 74 20 70 61 67 65 72 5f 67 65 74 5f c int pager_get_
12b48 63 6f 6e 74 65 6e 74 28 50 67 48 64 72 20 2a 70 content(PgHdr *p
12b49 50 67 29 7b 0a 20 20 69 66 28 20 70 50 67 2d 3e Pg){. if( pPg->
12b4a 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
12b4b 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 69 6e 74 _READ ){. int
12b4c 20 72 63 20 3d 20 72 65 61 64 44 62 50 61 67 65 rc = readDbPage
12b4d 28 70 50 67 2d 3e 70 50 61 67 65 72 2c 20 70 50 (pPg->pPager, pP
12b4e 67 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 g, pPg->pgno);.
12b4f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
12b50 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 E_OK ){. pP
12b51 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 g->flags &= ~PGH
12b52 44 52 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20 DR_NEED_READ;.
12b53 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
12b54 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
12b55 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
12b56 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
12b57 20 49 66 20 74 68 65 20 72 65 66 65 72 65 6e 63 If the referenc
12b58 65 20 63 6f 75 6e 74 20 68 61 73 20 72 65 61 63 e count has reac
12b59 68 65 64 20 7a 65 72 6f 2c 20 61 6e 64 20 74 68 hed zero, and th
12b5a 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 e pager is not i
12b5b 6e 20 74 68 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 n the.** middle
12b5c 6f 66 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 of a write trans
12b5d 61 63 74 69 6f 6e 20 6f 72 20 6f 70 65 6e 65 64 action or opened
12b5e 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
12b5f 64 65 2c 20 75 6e 6c 6f 63 6b 20 69 74 2e 0a 2a de, unlock it..*
12b60 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 / .static void p
12b61 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 agerUnlockIfUnus
12b62 65 64 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ed(Pager *pPager
12b63 29 7b 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 ){. if( (sqlite
12b64 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 3PcacheRefCount(
12b65 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
12b66 3d 3d 30 29 0a 20 20 20 20 26 26 20 28 21 70 50 ==0). && (!pP
12b67 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d ager->exclusiveM
12b68 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a ode || pPager->j
12b69 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20 0a 20 20 ournalOff>0) .
12b6a 29 7b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f ){. pagerUnlo
12b6b 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 ckAndRollback(pP
12b6c 61 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ager);. }.}../*
12b6d 0a 2a 2a 20 44 72 6f 70 20 61 20 70 61 67 65 20 .** Drop a page
12b6e 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75 from the cache u
12b6f 73 69 6e 67 20 73 71 6c 69 74 65 33 50 63 61 63 sing sqlite3Pcac
12b70 68 65 44 72 6f 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 heDrop()..**.**
12b71 49 66 20 74 68 69 73 20 6d 65 61 6e 73 20 74 68 If this means th
12b72 65 72 65 20 61 72 65 20 6e 6f 77 20 6e 6f 20 70 ere are now no p
12b73 61 67 65 73 20 77 69 74 68 20 72 65 66 65 72 65 ages with refere
12b74 6e 63 65 73 20 74 6f 20 74 68 65 6d 2c 20 61 20 nces to them, a
12b75 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 rollback.** occu
12b76 72 73 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 rs and the lock
12b77 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
12b78 69 73 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 is removed..*/.s
12b79 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 tatic void pager
12b7a 44 72 6f 70 50 61 67 65 28 44 62 50 61 67 65 20 DropPage(DbPage
12b7b 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a *pPg){. Pager *
12b7c 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 pPager = pPg->pP
12b7d 61 67 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 50 ager;. sqlite3P
12b7e 63 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b 0a cacheDrop(pPg);.
12b7f 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 pagerUnlockIfU
12b80 6e 75 73 65 64 28 70 50 61 67 65 72 29 3b 0a 7d nused(pPager);.}
12b81 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 ../*.** Acquire
12b82 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 a page..**.** A
12b83 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 read lock on the
12b84 20 64 69 73 6b 20 66 69 6c 65 20 69 73 20 6f 62 disk file is ob
12b85 74 61 69 6e 65 64 20 77 68 65 6e 20 74 68 65 20 tained when the
12b86 66 69 72 73 74 20 70 61 67 65 20 69 73 20 61 63 first page is ac
12b87 71 75 69 72 65 64 2e 20 0a 2a 2a 20 54 68 69 73 quired. .** This
12b88 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 64 72 read lock is dr
12b89 6f 70 70 65 64 20 77 68 65 6e 20 74 68 65 20 6c opped when the l
12b8a 61 73 74 20 70 61 67 65 20 69 73 20 72 65 6c 65 ast page is rele
12b8b 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ased..**.** This
12b8c 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 66 routine works f
12b8d 6f 72 20 61 6e 79 20 70 61 67 65 20 6e 75 6d 62 or any page numb
12b8e 65 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 er greater than
12b8f 30 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62 0. If the datab
12b90 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 73 ase.** file is s
12b91 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 maller than the
12b92 72 65 71 75 65 73 74 65 64 20 70 61 67 65 2c 20 requested page,
12b93 74 68 65 6e 20 6e 6f 20 61 63 74 75 61 6c 20 64 then no actual d
12b94 69 73 6b 0a 2a 2a 20 72 65 61 64 20 6f 63 63 75 isk.** read occu
12b95 72 73 20 61 6e 64 20 74 68 65 20 6d 65 6d 6f 72 rs and the memor
12b96 79 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 70 y image of the p
12b97 61 67 65 20 69 73 20 69 6e 69 74 69 61 6c 69 7a age is initializ
12b98 65 64 20 74 6f 0a 2a 2a 20 61 6c 6c 20 7a 65 72 ed to.** all zer
12b99 6f 73 2e 20 20 54 68 65 20 65 78 74 72 61 20 64 os. The extra d
12b9a 61 74 61 20 61 70 70 65 6e 64 65 64 20 74 6f 20 ata appended to
12b9b 61 20 70 61 67 65 20 69 73 20 61 6c 77 61 79 73 a page is always
12b9c 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 initialized.**
12b9d 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 66 69 72 to zeros the fir
12b9e 73 74 20 74 69 6d 65 20 61 20 70 61 67 65 20 69 st time a page i
12b9f 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 s loaded into me
12ba0 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 mory..**.** The
12ba1 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68 acquisition migh
12ba2 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72 t fail for sever
12ba3 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20 al reasons. In
12ba4 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e all cases,.** an
12ba5 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 appropriate err
12ba6 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
12ba7 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20 ned and *ppPage
12ba8 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a is set to NULL..
12ba9 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 **.** See also s
12baa 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
12bab 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20 p(). Both this
12bac 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b routine and Look
12bad 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20 up() attempt.**
12bae 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20 69 to find a page i
12baf 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 n the in-memory
12bb0 63 61 63 68 65 20 66 69 72 73 74 2e 20 20 49 66 cache first. If
12bb1 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
12bb2 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d already.** in m
12bb3 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74 emory, this rout
12bb4 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b ine goes to disk
12bb5 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77 to read it in w
12bb6 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a hereas Lookup().
12bb7 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20 ** just returns
12bb8 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0. This routine
12bb9 20 61 63 71 75 69 72 65 73 20 61 20 72 65 61 64 acquires a read
12bba 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20 -lock the first
12bbb 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74 time it.** has t
12bbc 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e o go to disk, an
12bbd 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61 d could also pla
12bbe 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 yback an old jou
12bbf 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 rnal if necessar
12bc0 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b y..** Since Look
12bc1 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20 up() never goes
12bc2 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65 to disk, it neve
12bc3 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69 r has to deal wi
12bc4 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a th locks.** or j
12bc5 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a ournal files..**
12bc6 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 .** If noContent
12bc7 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 20 70 is false, the p
12bc8 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 61 72 65 age contents are
12bc9 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 20 66 actually read f
12bca 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2a 20 49 66 20 rom disk..** If
12bcb 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 75 noContent is tru
12bcc 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 e, it means that
12bcd 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 we do not care
12bce 61 62 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e about the conten
12bcf 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 ts.** of the pag
12bd0 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2c 20 e at this time,
12bd1 73 6f 20 64 6f 20 6e 6f 74 20 64 6f 20 61 20 64 so do not do a d
12bd2 69 73 6b 20 72 65 61 64 2e 20 20 4a 75 73 74 20 isk read. Just
12bd3 66 69 6c 6c 20 69 6e 20 74 68 65 0a 2a 2a 20 70 fill in the.** p
12bd4 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 age content with
12bd5 20 7a 65 72 6f 73 2e 20 20 42 75 74 20 6d 61 72 zeros. But mar
12bd6 6b 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 k the fact that
12bd7 77 65 20 68 61 76 65 20 6e 6f 74 20 72 65 61 64 we have not read
12bd8 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 the.** content
12bd9 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 by setting the P
12bda 67 48 64 72 2e 6e 65 65 64 52 65 61 64 20 66 6c gHdr.needRead fl
12bdb 61 67 2e 20 20 4c 61 74 65 72 20 6f 6e 2c 20 69 ag. Later on, i
12bdc 66 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 f .** sqlite3Pag
12bdd 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c erWrite() is cal
12bde 6c 65 64 20 6f 6e 20 74 68 69 73 20 70 61 67 65 led on this page
12bdf 20 6f 72 20 69 66 20 74 68 69 73 20 72 6f 75 74 or if this rout
12be0 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 ine is.** called
12be1 20 61 67 61 69 6e 20 77 69 74 68 20 6e 6f 43 6f again with noCo
12be2 6e 74 65 6e 74 3d 3d 30 2c 20 74 68 61 74 20 6d ntent==0, that m
12be3 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f eans that the co
12be4 6e 74 65 6e 74 20 69 73 20 6e 65 65 64 65 64 0a ntent is needed.
12be5 2a 2a 20 61 6e 64 20 74 68 65 20 64 69 73 6b 20 ** and the disk
12be6 72 65 61 64 20 73 68 6f 75 6c 64 20 6f 63 63 75 read should occu
12be7 72 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e r at that point.
12be8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
12be9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
12bea 67 65 72 41 63 71 75 69 72 65 28 0a 20 20 50 61 gerAcquire(. Pa
12beb 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 ger *pPager,
12bec 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f /* The pager o
12bed 70 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 pen on the datab
12bee 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 ase file */. Pg
12bef 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 no pgno,
12bf0 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
12bf1 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 to fetch */. D
12bf2 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 bPage **ppPage,
12bf3 20 20 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f /* Write a po
12bf4 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 inter to the pag
12bf5 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 e here */. int
12bf6 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 noContent
12bf7 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 /* Do not bother
12bf8 20 72 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 reading content
12bf9 20 66 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 from disk if tr
12bfa 75 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 ue */.){. PgHdr
12bfb 20 2a 70 50 67 20 3d 20 30 3b 0a 20 20 69 6e 74 *pPg = 0;. int
12bfc 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
12bfd 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
12bfe 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 0a 20 20 20 AGER_UNLOCK .
12bff 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 63 || sqlite3Pc
12c00 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 acheRefCount(pPa
12c01 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 ger->pPCache)>0
12c02 0a 20 20 20 20 20 20 20 7c 7c 20 70 67 6e 6f 3d . || pgno=
12c03 3d 31 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 =1. );.. /* Th
12c04 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 6e e maximum page n
12c05 75 6d 62 65 72 20 69 73 20 32 5e 33 31 2e 20 52 umber is 2^31. R
12c06 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
12c07 52 55 50 54 20 69 66 20 61 20 70 61 67 65 0a 20 RUPT if a page.
12c08 20 2a 2a 20 6e 75 6d 62 65 72 20 67 72 65 61 74 ** number great
12c09 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20 6f 72 er than this, or
12c0a 20 7a 65 72 6f 2c 20 69 73 20 72 65 71 75 65 73 zero, is reques
12c0b 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ted.. */. if(
12c0c 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50 pgno>PAGER_MAX_P
12c0d 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 30 20 7c GNO || pgno==0 |
12c0e 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a | pgno==PAGER_MJ
12c0f 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b _PGNO(pPager) ){
12c10 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
12c11 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
12c12 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 . }.. /* Make
12c13 73 75 72 65 20 77 65 20 68 61 76 65 20 6e 6f 74 sure we have not
12c14 20 68 69 74 20 61 6e 79 20 63 72 69 74 69 63 61 hit any critica
12c15 6c 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 20 0a l errors.. */ .
12c16 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12c17 21 3d 30 20 29 3b 0a 20 20 2a 70 70 50 61 67 65 !=0 );. *ppPage
12c18 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 = 0;.. /* If t
12c19 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 his is the first
12c1a 20 70 61 67 65 20 61 63 63 65 73 73 65 64 2c 20 page accessed,
12c1b 74 68 65 6e 20 67 65 74 20 61 20 53 48 41 52 45 then get a SHARE
12c1c 44 20 6c 6f 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 D lock. ** on t
12c1d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12c1e 2e 20 70 61 67 65 72 53 68 61 72 65 64 4c 6f 63 . pagerSharedLoc
12c1f 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 k() is a no-op i
12c20 66 20 0a 20 20 2a 2a 20 61 20 64 61 74 61 62 61 f . ** a databa
12c21 73 65 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 se lock is alrea
12c22 64 79 20 68 65 6c 64 2e 0a 20 20 2a 2f 0a 20 20 dy held.. */.
12c23 72 63 20 3d 20 70 61 67 65 72 53 68 61 72 65 64 rc = pagerShared
12c24 4c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 Lock(pPager);.
12c25 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12c26 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
12c27 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rc;. }. assert
12c28 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 ( pPager->state!
12c29 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b =PAGER_UNLOCK );
12c2a 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 .. rc = sqlite3
12c2b 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 PcacheFetch(pPag
12c2c 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e er->pPCache, pgn
12c2d 6f 2c 20 31 2c 20 26 70 50 67 29 3b 0a 20 20 69 o, 1, &pPg);. i
12c2e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
12c2f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
12c30 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 67 c;. }. if( pPg
12c31 2d 3e 70 50 61 67 65 72 3d 3d 30 20 29 7b 0a 20 ->pPager==0 ){.
12c32 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 /* The pager
12c33 63 61 63 68 65 20 68 61 73 20 63 72 65 61 74 65 cache has create
12c34 64 20 61 20 6e 65 77 20 70 61 67 65 2e 20 49 74 d a new page. It
12c35 73 20 63 6f 6e 74 65 6e 74 20 6e 65 65 64 73 20 s content needs
12c36 74 6f 20 0a 20 20 20 20 2a 2a 20 62 65 20 69 6e to . ** be in
12c37 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 20 20 2a itialized.. *
12c38 2f 0a 20 20 20 20 69 6e 74 20 6e 4d 61 78 3b 0a /. int nMax;.
12c39 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 PAGER_INCR(p
12c3a 50 61 67 65 72 2d 3e 6e 4d 69 73 73 29 3b 0a 20 Pager->nMiss);.
12c3b 20 20 20 70 50 67 2d 3e 70 50 61 67 65 72 20 3d pPg->pPager =
12c3c 20 70 50 61 67 65 72 3b 0a 20 20 20 20 6d 65 6d pPager;. mem
12c3d 73 65 74 28 70 50 67 2d 3e 70 45 78 74 72 61 2c set(pPg->pExtra,
12c3e 20 30 2c 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 0, pPager->nExt
12c3f 72 61 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 ra);.. rc = s
12c40 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
12c41 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 4d ount(pPager, &nM
12c42 61 78 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ax);. if( rc!
12c43 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
12c44 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
12c45 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20 Unref(pPg);.
12c46 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
12c47 20 7d 0a 0a 20 20 20 20 69 66 28 20 6e 4d 61 78 }.. if( nMax
12c48 3c 28 69 6e 74 29 70 67 6e 6f 20 7c 7c 20 4d 45 <(int)pgno || ME
12c49 4d 44 42 20 7c 7c 20 6e 6f 43 6f 6e 74 65 6e 74 MDB || noContent
12c4a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 67 ){. if( pg
12c4b 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e no>pPager->mxPgn
12c4c 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c o ){. sql
12c4d 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
12c4e 50 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 Pg);. ret
12c4f 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b urn SQLITE_FULL;
12c50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d . }. m
12c51 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 emset(pPg->pData
12c52 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 , 0, pPager->pag
12c53 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 eSize);. if
12c54 28 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 ( noContent ){.
12c55 20 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 pPg->flag
12c56 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f s |= PGHDR_NEED_
12c57 52 45 41 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 READ;. }.
12c58 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 5a 45 IOTRACE(("ZE
12c59 52 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 RO %p %d\n", pPa
12c5a 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 ger, pgno));.
12c5b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
12c5c 20 3d 20 72 65 61 64 44 62 50 61 67 65 28 70 50 = readDbPage(pP
12c5d 61 67 65 72 2c 20 70 50 67 2c 20 70 67 6e 6f 29 ager, pPg, pgno)
12c5e 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
12c5f 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 SQLITE_OK && rc!
12c60 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 =SQLITE_IOERR_SH
12c61 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 ORT_READ ){.
12c62 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 50 61 /* sqlite3Pa
12c63 67 65 72 55 6e 72 65 66 28 70 50 67 29 3b 20 2a gerUnref(pPg); *
12c64 2f 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 44 /. pagerD
12c65 72 6f 70 50 61 67 65 28 70 50 67 29 3b 0a 20 20 ropPage(pPg);.
12c66 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
12c67 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
12c68 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 ifdef SQLITE_CHE
12c69 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 CK_PAGES. pPg
12c6a 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 ->pageHash = pag
12c6b 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 er_pagehash(pPg)
12c6c 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 ;.#endif. }else
12c6d 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 {. /* The req
12c6e 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 69 uested page is i
12c6f 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 n the page cache
12c70 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 . */. assert(
12c71 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
12c72 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 Count(pPager->pP
12c73 43 61 63 68 65 29 3e 30 20 7c 7c 20 70 67 6e 6f Cache)>0 || pgno
12c74 3d 3d 31 29 3b 0a 20 20 20 20 50 41 47 45 52 5f ==1);. PAGER_
12c75 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 48 69 INCR(pPager->nHi
12c76 74 29 3b 0a 20 20 20 20 69 66 28 20 21 6e 6f 43 t);. if( !noC
12c77 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 ontent ){.
12c78 72 63 20 3d 20 70 61 67 65 72 5f 67 65 74 5f 63 rc = pager_get_c
12c79 6f 6e 74 65 6e 74 28 70 50 67 29 3b 0a 20 20 20 ontent(pPg);.
12c7a 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
12c7b 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
12c7c 72 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 rUnref(pPg);.
12c7d 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
12c7e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
12c7f 7d 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 }.. *ppPage = p
12c80 50 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c Pg;. return SQL
12c81 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
12c82 20 41 63 71 75 69 72 65 20 61 20 70 61 67 65 20 Acquire a page
12c83 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 if it is already
12c84 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 in the in-memor
12c85 79 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 y cache. Do.**
12c86 6e 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 67 not read the pag
12c87 65 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 e from disk. Re
12c88 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
12c89 6f 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f o the page,.** o
12c8a 72 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20 r 0 if the page
12c8b 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e is not in cache.
12c8c 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 .**.** See also
12c8d 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
12c8e 29 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e ). The differen
12c8f 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 ce between this
12c90 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 routine.** and s
12c91 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
12c92 20 69 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 is that _get()
12c93 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 will go to the d
12c94 69 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 isk and read.**
12c95 69 6e 20 74 68 65 20 70 61 67 65 20 69 66 20 74 in the page if t
12c96 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 he page is not a
12c97 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e lready in cache.
12c98 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a This routine.*
12c99 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 * returns NULL i
12c9a 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f f the page is no
12c9b 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 t in cache or if
12c9c 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f a disk I/O erro
12c9d 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 r .** has ever h
12c9e 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 appened..*/.SQLI
12c9f 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 TE_PRIVATE DbPag
12ca0 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c e *sqlite3PagerL
12ca1 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 ookup(Pager *pPa
12ca2 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b ger, Pgno pgno){
12ca3 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 . PgHdr *pPg =
12ca4 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 0;. assert( pPa
12ca5 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ger!=0 );. asse
12ca6 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 0a rt( pgno!=0 );..
12ca7 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 73 if( (pPager->s
12ca8 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f tate!=PAGER_UNLO
12ca9 43 4b 29 0a 20 20 20 26 26 20 28 70 50 61 67 65 CK). && (pPage
12caa 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 r->errCode==SQLI
12cab 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d TE_OK || pPager-
12cac 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 >errCode==SQLITE
12cad 5f 46 55 4c 4c 29 0a 20 20 29 7b 0a 20 20 20 20 _FULL). ){.
12cae 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 sqlite3PcacheFet
12caf 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ch(pPager->pPCac
12cb0 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 50 he, pgno, 0, &pP
12cb1 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 g);. }.. retur
12cb2 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n pPg;.}../*.**
12cb3 52 65 6c 65 61 73 65 20 61 20 70 61 67 65 2e 0a Release a page..
12cb4 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d **.** If the num
12cb5 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 ber of reference
12cb6 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 s to the page dr
12cb7 6f 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e op to zero, then
12cb8 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 the.** page is
12cb9 61 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 added to the LRU
12cba 20 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c list. When all
12cbb 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 references to a
12cbc 6c 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 ll pages.** are
12cbd 72 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c released, a roll
12cbe 62 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 back occurs and
12cbf 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 the lock on the
12cc0 64 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 database is.** r
12cc1 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 emoved..*/.SQLIT
12cc2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
12cc3 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
12cc4 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 DbPage *pPg){.
12cc5 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 if( pPg ){. P
12cc6 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
12cc7 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 Pg->pPager;.
12cc8 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c sqlite3PcacheRel
12cc9 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 ease(pPg);. p
12cca 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 agerUnlockIfUnus
12ccb 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a ed(pPager);. }.
12ccc 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
12ccd 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 OK;.}../*.** If
12cce 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
12ccf 20 66 69 6c 65 20 68 61 73 20 61 6c 72 65 61 64 file has alread
12cd0 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 65 y been opened, e
12cd1 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a nsure that the.*
12cd2 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 * sub-journal fi
12cd3 6c 65 20 69 73 20 6f 70 65 6e 20 74 6f 6f 2e 20 le is open too.
12cd4 49 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 If the main jour
12cd5 6e 61 6c 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c nal is not open,
12cd6 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f .** this functio
12cd7 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a n is a no-op..**
12cd8 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 .** SQLITE_OK is
12cd9 20 72 65 74 75 72 6e 65 64 20 69 66 20 65 76 65 returned if eve
12cda 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61 63 63 rything goes acc
12cdb 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 ording to plan.
12cdc 41 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f An .** SQLITE_IO
12cdd 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f ERR_XXX error co
12cde 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 de is returned i
12cdf 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 0a 2a f the call to .*
12ce0 2a 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 * sqlite3OsOpen(
12ce1 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 ) fails..*/.stat
12ce2 69 63 20 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f ic int openSubJo
12ce3 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 urnal(Pager *pPa
12ce4 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ger){. int rc =
12ce5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
12ce6 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
12ce7 6c 4f 70 65 6e 20 26 26 20 21 70 50 61 67 65 72 lOpen && !pPager
12ce8 2d 3e 73 6a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 ->sjfd->pMethods
12ce9 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 ){. if( pPag
12cea 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
12ceb 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
12cec 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 DE_MEMORY ){.
12ced 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 sqlite3MemJou
12cee 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d rnalOpen(pPager-
12cef 3e 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 >sjfd);. }els
12cf0 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 e{. rc = sq
12cf1 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65 lite3PagerOpente
12cf2 6d 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 mp(pPager, pPage
12cf3 72 2d 3e 73 6a 66 64 2c 20 53 51 4c 49 54 45 5f r->sjfd, SQLITE_
12cf4 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 OPEN_SUBJOURNAL)
12cf5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
12cf6 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
12cf7 2a 20 43 72 65 61 74 65 20 61 20 6a 6f 75 72 6e * Create a journ
12cf8 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 50 61 67 al file for pPag
12cf9 65 72 2e 20 20 54 68 65 72 65 20 73 68 6f 75 6c er. There shoul
12cfa 64 20 61 6c 72 65 61 64 79 20 62 65 20 61 20 52 d already be a R
12cfb 45 53 45 52 56 45 44 0a 2a 2a 20 6f 72 20 45 58 ESERVED.** or EX
12cfc 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 CLUSIVE lock on
12cfd 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12cfe 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 e when this rout
12cff 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a ine is called..*
12d00 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
12d01 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 TE_OK if everyth
12d02 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 ing. Return an
12d03 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 72 error code and r
12d04 65 6c 65 61 73 65 20 74 68 65 0a 2a 2a 20 77 72 elease the.** wr
12d05 69 74 65 20 6c 6f 63 6b 20 69 66 20 61 6e 79 74 ite lock if anyt
12d06 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e hing goes wrong.
12d07 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
12d08 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 ager_open_journa
12d09 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 l(Pager *pPager)
12d0a 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
12d0b 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e *pVfs = pPager->
12d0c 70 56 66 73 3b 0a 20 20 69 6e 74 20 66 6c 61 67 pVfs;. int flag
12d0d 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e s = (SQLITE_OPEN
12d0e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 _READWRITE|SQLIT
12d0f 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
12d10 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 |SQLITE_OPEN_CRE
12d11 41 54 45 29 3b 0a 0a 20 20 69 6e 74 20 72 63 3b ATE);.. int rc;
12d12 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
12d13 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
12d14 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 61 73 RESERVED );. as
12d15 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 sert( pPager->us
12d16 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 eJournal );. as
12d17 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 sert( pPager->pI
12d18 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 nJournal==0 );.
12d19 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
12d1a 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 ecount(pPager, 0
12d1b 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 6e );. pPager->pIn
12d1c 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 Journal = sqlite
12d1d 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 50 3BitvecCreate(pP
12d1e 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 ager->dbSize);.
12d1f 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e if( pPager->pIn
12d20 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 Journal==0 ){.
12d21 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
12d22 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 66 61 MEM;. goto fa
12d23 69 6c 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 iled_to_open_jou
12d24 72 6e 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 rnal;. }.. if(
12d25 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12d26 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 Open==0 ){. i
12d27 66 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 f( pPager->tempF
12d28 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 ile ){. fla
12d29 67 73 20 7c 3d 20 28 53 51 4c 49 54 45 5f 4f 50 gs |= (SQLITE_OP
12d2a 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
12d2b 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d |SQLITE_OPEN_TEM
12d2c 50 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 P_JOURNAL);.
12d2d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c 61 }else{. fla
12d2e 67 73 20 7c 3d 20 28 53 51 4c 49 54 45 5f 4f 50 gs |= (SQLITE_OP
12d2f 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 EN_MAIN_JOURNAL)
12d30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
12d31 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
12d32 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
12d33 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b ALMODE_MEMORY ){
12d34 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 . sqlite3Me
12d35 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 mJournalOpen(pPa
12d36 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 ger->jfd);.
12d37 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
12d38 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69 66 64 . }else{.#ifd
12d39 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
12d3a 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 _ATOMIC_WRITE.
12d3b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12d3c 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 20 JournalOpen(.
12d3d 20 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 61 pVfs, pPa
12d3e 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 ger->zJournal, p
12d3f 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 Pager->jfd, flag
12d40 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a s, jrnlBufferSiz
12d41 65 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 20 e(pPager).
12d42 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 72 );.#else. r
12d43 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 c = sqlite3OsOpe
12d44 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e n(pVfs, pPager->
12d45 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 zJournal, pPager
12d46 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29 ->jfd, flags, 0)
12d47 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 ;.#endif. }.
12d48 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
12d49 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 QLITE_OK || pPag
12d4a 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f 64 er->jfd->pMethod
12d4b 73 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d s );. pPager-
12d4c 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b >journalOff = 0;
12d4d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 . pPager->set
12d4e 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
12d4f 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
12d50 64 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 dr = 0;. if(
12d51 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
12d52 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
12d53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 QLITE_NOMEM ){.
12d54 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 sqlite3Os
12d55 44 65 6c 65 74 65 28 70 56 66 73 2c 20 70 50 61 Delete(pVfs, pPa
12d56 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 ger->zJournal, 0
12d57 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
12d58 20 67 6f 74 6f 20 66 61 69 6c 65 64 5f 74 6f 5f goto failed_to_
12d59 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a 20 20 open_journal;.
12d5a 20 20 7d 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 }. }. pPager
12d5b 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 3d 20 ->journalOpen =
12d5c 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 1;. pPager->jou
12d5d 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b rnalStarted = 0;
12d5e 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 . pPager->needS
12d5f 79 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 ync = 0;. pPage
12d60 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 69 r->nRec = 0;. i
12d61 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f f( pPager->errCo
12d62 64 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 de ){. rc = p
12d63 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
12d64 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 5f goto failed_
12d65 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b to_open_journal;
12d66 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 64 . }. pPager->d
12d67 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 50 61 67 bOrigSize = pPag
12d68 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 0a 20 20 72 er->dbSize;.. r
12d69 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c c = writeJournal
12d6a 48 64 72 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 Hdr(pPager);..
12d6b 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 if( pPager->nSav
12d6c 65 70 6f 69 6e 74 20 26 26 20 72 63 3d 3d 53 51 epoint && rc==SQ
12d6d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
12d6e 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e c = openSubJourn
12d6f 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a al(pPager);. }.
12d70 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12d71 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 _OK && rc!=SQLIT
12d72 45 5f 4e 4f 4d 45 4d 20 26 26 20 72 63 21 3d 53 E_NOMEM && rc!=S
12d73 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 QLITE_IOERR_NOME
12d74 4d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 M ){. rc = pa
12d75 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 ger_end_transact
12d76 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a ion(pPager, 0);.
12d77 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
12d78 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
12d79 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b c = SQLITE_FULL;
12d7a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
12d7b 75 72 6e 20 72 63 3b 0a 0a 66 61 69 6c 65 64 5f urn rc;..failed_
12d7c 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3a to_open_journal:
12d7d 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 . sqlite3Bitvec
12d7e 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e Destroy(pPager->
12d7f 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 70 pInJournal);. p
12d80 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
12d81 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 l = 0;. return
12d82 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 rc;.}../*.** Acq
12d83 75 69 72 65 20 61 20 77 72 69 74 65 2d 6c 6f 63 uire a write-loc
12d84 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
12d85 65 2e 20 20 54 68 65 20 6c 6f 63 6b 20 69 73 20 e. The lock is
12d86 72 65 6d 6f 76 65 64 20 77 68 65 6e 0a 2a 2a 20 removed when.**
12d87 74 68 65 20 61 6e 79 20 6f 66 20 74 68 65 20 66 the any of the f
12d88 6f 6c 6c 6f 77 69 6e 67 20 68 61 70 70 65 6e 3a ollowing happen:
12d89 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 .**.** * sqli
12d8a 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 te3PagerCommitPh
12d8b 61 73 65 54 77 6f 28 29 20 69 73 20 63 61 6c 6c aseTwo() is call
12d8c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 ed..** * sqli
12d8d 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
12d8e 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a () is called..**
12d8f 20 20 20 2a 20 20 73 71 6c 69 74 65 33 50 61 67 * sqlite3Pag
12d90 65 72 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c erClose() is cal
12d91 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71 6c led..** * sql
12d92 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 ite3PagerUnref()
12d93 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 6f 6e is called to on
12d94 20 65 76 65 72 79 20 6f 75 74 73 74 61 6e 64 69 every outstandi
12d95 6e 67 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 ng page..**.** T
12d96 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
12d97 65 72 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 er to this routi
12d98 6e 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 ne is a pointer
12d99 74 6f 20 61 6e 79 20 6f 70 65 6e 20 70 61 67 65 to any open page
12d9a 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 of the.** datab
12d9b 61 73 65 20 66 69 6c 65 2e 20 20 4e 6f 74 68 69 ase file. Nothi
12d9c 6e 67 20 63 68 61 6e 67 65 73 20 61 62 6f 75 74 ng changes about
12d9d 20 74 68 65 20 70 61 67 65 20 2d 20 69 74 20 69 the page - it i
12d9e 73 20 75 73 65 64 20 6d 65 72 65 6c 79 20 74 6f s used merely to
12d9f 0a 2a 2a 20 61 63 71 75 69 72 65 20 61 20 70 6f .** acquire a po
12da0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61 67 inter to the Pag
12da1 65 72 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 er structure and
12da2 20 61 73 20 70 72 6f 6f 66 20 74 68 61 74 20 74 as proof that t
12da3 68 65 72 65 20 69 73 0a 2a 2a 20 61 6c 72 65 61 here is.** alrea
12da4 64 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f dy a read-lock o
12da5 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a n the database..
12da6 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 **.** The second
12da7 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 parameter indic
12da8 61 74 65 73 20 68 6f 77 20 6d 75 63 68 20 73 70 ates how much sp
12da9 61 63 65 20 69 6e 20 62 79 74 65 73 20 74 6f 20 ace in bytes to
12daa 72 65 73 65 72 76 65 20 66 6f 72 20 61 0a 2a 2a reserve for a.**
12dab 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
12dac 66 69 6c 65 2d 6e 61 6d 65 20 61 74 20 74 68 65 file-name at the
12dad 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f start of the jo
12dae 75 72 6e 61 6c 20 77 68 65 6e 20 69 74 20 69 73 urnal when it is
12daf 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 created..**.**
12db0 41 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 A journal file i
12db1 73 20 6f 70 65 6e 65 64 20 69 66 20 74 68 69 73 s opened if this
12db2 20 69 73 20 6e 6f 74 20 61 20 74 65 6d 70 6f 72 is not a tempor
12db3 61 72 79 20 66 69 6c 65 2e 20 20 46 6f 72 20 74 ary file. For t
12db4 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 66 69 6c 65 emporary.** file
12db5 73 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 6f s, the opening o
12db6 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
12db7 6c 65 20 69 73 20 64 65 66 65 72 72 65 64 20 75 le is deferred u
12db8 6e 74 69 6c 20 74 68 65 72 65 20 69 73 20 61 6e ntil there is an
12db9 0a 2a 2a 20 61 63 74 75 61 6c 20 6e 65 65 64 20 .** actual need
12dba 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 to write to the
12dbb 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 journal..**.** I
12dbc 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 f the database i
12dbd 73 20 61 6c 72 65 61 64 79 20 72 65 73 65 72 76 s already reserv
12dbe 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 20 ed for writing,
12dbf 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
12dc0 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 a no-op..**.** I
12dc1 66 20 65 78 46 6c 61 67 20 69 73 20 74 72 75 65 f exFlag is true
12dc2 2c 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 67 , go ahead and g
12dc3 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 et an EXCLUSIVE
12dc4 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 lock on the file
12dc5 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 .** immediately
12dc6 69 6e 73 74 65 61 64 20 6f 66 20 77 61 69 74 69 instead of waiti
12dc7 6e 67 20 75 6e 74 69 6c 20 77 65 20 74 72 79 20 ng until we try
12dc8 74 6f 20 66 6c 75 73 68 20 74 68 65 20 63 61 63 to flush the cac
12dc9 68 65 2e 20 20 54 68 65 0a 2a 2a 20 65 78 46 6c he. The.** exFl
12dca 61 67 20 69 73 20 69 67 6e 6f 72 65 64 20 69 66 ag is ignored if
12dcb 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
12dcc 73 20 61 6c 72 65 61 64 79 20 61 63 74 69 76 65 s already active
12dcd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
12dce 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
12dcf 61 67 65 72 42 65 67 69 6e 28 44 62 50 61 67 65 agerBegin(DbPage
12dd0 20 2a 70 50 67 2c 20 69 6e 74 20 65 78 46 6c 61 *pPg, int exFla
12dd1 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 g){. Pager *pPa
12dd2 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 ger = pPg->pPage
12dd3 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 r;. int rc = SQ
12dd4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 LITE_OK;. asser
12dd5 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 t( pPg->nRef>0 )
12dd6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
12dd7 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 er->state!=PAGER
12dd8 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 _UNLOCK );. if(
12dd9 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
12dda 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 7b 0a PAGER_SHARED ){.
12ddb 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
12ddc 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d er->pInJournal==
12ddd 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
12dde 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 72 !MEMDB );. r
12ddf 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 c = sqlite3OsLoc
12de0 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 k(pPager->fd, RE
12de1 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SERVED_LOCK);.
12de2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
12de3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 _OK ){. pPa
12de4 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 ger->state = PAG
12de5 45 52 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20 ER_RESERVED;.
12de6 20 20 20 69 66 28 20 65 78 46 6c 61 67 20 29 7b if( exFlag ){
12de7 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 . rc = pa
12de8 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b ger_wait_on_lock
12de9 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 (pPager, EXCLUSI
12dea 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 VE_LOCK);.
12deb 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
12dec 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
12ded 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
12dee 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 ;. }. pPag
12def 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 3d er->dirtyCache =
12df0 20 30 3b 0a 20 20 20 20 50 41 47 45 52 54 52 41 0;. PAGERTRA
12df1 43 45 28 28 22 54 52 41 4e 53 41 43 54 49 4f 4e CE(("TRANSACTION
12df2 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
12df3 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 69 pPager)));. i
12df4 66 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f f( pPager->useJo
12df5 75 72 6e 61 6c 20 26 26 20 21 70 50 61 67 65 72 urnal && !pPager
12df6 2d 3e 74 65 6d 70 46 69 6c 65 0a 20 20 20 20 20 ->tempFile.
12df7 20 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d && pPager-
12df8 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 >journalMode!=PA
12df9 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
12dfa 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 72 63 20 OFF ){. rc
12dfb 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 = pager_open_jou
12dfc 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 rnal(pPager);.
12dfd 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
12dfe 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
12dff 70 65 6e 20 26 26 20 70 50 61 67 65 72 2d 3e 6a pen && pPager->j
12e00 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a ournalOff==0 ){.
12e01 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 /* This happ
12e02 65 6e 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 ens when the pag
12e03 65 72 20 77 61 73 20 69 6e 20 65 78 63 6c 75 73 er was in exclus
12e04 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 ive-access mode
12e05 74 68 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 the last. **
12e06 74 69 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20 time a (read or
12e07 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 write) transacti
12e08 6f 6e 20 77 61 73 20 73 75 63 63 65 73 73 66 75 on was successfu
12e09 6c 6c 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 lly concluded.
12e0a 20 20 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e ** by this con
12e0b 6e 65 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 nection. Instead
12e0c 20 6f 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65 of deleting the
12e0d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 journal file it
12e0e 20 77 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 was . ** kep
12e0f 74 20 6f 70 65 6e 20 61 6e 64 20 65 69 74 68 65 t open and eithe
12e10 72 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 20 r was truncated
12e11 74 6f 20 30 20 62 79 74 65 73 20 6f 72 20 69 74 to 0 bytes or it
12e12 73 20 68 65 61 64 65 72 20 77 61 73 0a 20 20 20 s header was.
12e13 20 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 ** overwritten
12e14 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 with zeros..
12e15 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
12e16 50 61 67 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 Pager->nRec==0 )
12e17 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 ;. assert( pP
12e18 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
12e19 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
12e1a 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f t( pPager->pInJo
12e1b 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 urnal==0 );.
12e1c 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
12e1d 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29 count(pPager, 0)
12e1e 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 ;. pPager->pI
12e1f 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 nJournal = sqlit
12e20 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 20 e3BitvecCreate(
12e21 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 pPager->dbSize )
12e22 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 ;. if( !pPage
12e23 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 7b r->pInJournal ){
12e24 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
12e25 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 TE_NOMEM;. }e
12e26 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 67 65 lse{. pPage
12e27 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 r->dbOrigSize =
12e28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a pPager->dbSize;.
12e29 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 rc = write
12e2a 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 JournalHdr(pPage
12e2b 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 r);. }. }.
12e2c 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d assert( !pPager-
12e2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 7c 7c 20 >journalOpen ||
12e2e 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
12e2f 66 66 3e 30 20 7c 7c 20 72 63 21 3d 53 51 4c 49 ff>0 || rc!=SQLI
12e30 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 65 74 75 72 TE_OK );. retur
12e31 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d n rc;.}../*.** M
12e32 61 72 6b 20 61 20 64 61 74 61 20 70 61 67 65 20 ark a data page
12e33 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 20 54 as writeable. T
12e34 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 he page is writt
12e35 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 en into the jour
12e36 6e 61 6c 20 0a 2a 2a 20 69 66 20 69 74 20 69 73 nal .** if it is
12e37 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 not there alrea
12e38 64 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e dy. This routin
12e39 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 e must be called
12e3a 20 62 65 66 6f 72 65 20 6d 61 6b 69 6e 67 0a 2a before making.*
12e3b 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 70 * changes to a p
12e3c 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 age..**.** The f
12e3d 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 irst time this r
12e3e 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
12e3f 2c 20 74 68 65 20 70 61 67 65 72 20 63 72 65 61 , the pager crea
12e40 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6a 6f 75 tes a new.** jou
12e41 72 6e 61 6c 20 61 6e 64 20 61 63 71 75 69 72 65 rnal and acquire
12e42 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 s a RESERVED loc
12e43 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
12e44 65 2e 20 20 49 66 20 74 68 65 20 52 45 53 45 52 e. If the RESER
12e45 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 75 6c VED.** lock coul
12e46 64 20 6e 6f 74 20 62 65 20 61 63 71 75 69 72 65 d not be acquire
12e47 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 d, this routine
12e48 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 42 returns SQLITE_B
12e49 55 53 59 2e 20 20 54 68 65 0a 2a 2a 20 63 61 6c USY. The.** cal
12e4a 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 ling routine mus
12e4b 74 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61 74 t check for that
12e4c 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 6e return value an
12e4d 64 20 62 65 20 63 61 72 65 66 75 6c 20 6e 6f 74 d be careful not
12e4e 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 61 6e to.** change an
12e4f 79 20 70 61 67 65 20 64 61 74 61 20 75 6e 74 69 y page data unti
12e50 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 l this routine r
12e51 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
12e52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a ..**.** If the j
12e53 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 75 6c ournal file coul
12e54 64 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e d not be written
12e55 20 62 65 63 61 75 73 65 20 74 68 65 20 64 69 73 because the dis
12e56 6b 20 69 73 20 66 75 6c 6c 2c 0a 2a 2a 20 74 68 k is full,.** th
12e57 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
12e58 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 46 returns SQLITE_F
12e59 55 4c 4c 20 61 6e 64 20 64 6f 65 73 20 61 6e 20 ULL and does an
12e5a 69 6d 6d 65 64 69 61 74 65 20 72 6f 6c 6c 62 61 immediate rollba
12e5b 63 6b 2e 0a 2a 2a 20 41 6c 6c 20 73 75 62 73 65 ck..** All subse
12e5c 71 75 65 6e 74 20 77 72 69 74 65 20 61 74 74 65 quent write atte
12e5d 6d 70 74 73 20 61 6c 73 6f 20 72 65 74 75 72 6e mpts also return
12e5e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 75 6e 74 SQLITE_FULL unt
12e5f 69 6c 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61 il there.** is a
12e60 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
12e61 50 61 67 65 72 43 6f 6d 6d 69 74 28 29 20 6f 72 PagerCommit() or
12e62 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c sqlite3PagerRol
12e63 6c 62 61 63 6b 28 29 20 74 6f 0a 2a 2a 20 72 65 lback() to.** re
12e64 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 set..*/.static i
12e65 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 nt pager_write(P
12e66 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f gHdr *pPg){. vo
12e67 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d id *pData = pPg-
12e68 3e 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 >pData;. Pager
12e69 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
12e6a 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 Pager;. int rc
12e6b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
12e6c 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 65 72 72 /* Check for err
12e6d 6f 72 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ors. */. if( p
12e6e 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 Pager->errCode )
12e6f 7b 20 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 { . return pP
12e70 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
12e71 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d }. if( pPager-
12e72 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 >readOnly ){.
12e73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 return SQLITE_P
12e74 45 52 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 ERM;. }.. asse
12e75 72 74 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 rt( !pPager->set
12e76 4d 61 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 Master );.. CHE
12e77 43 4b 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 CK_PAGE(pPg);..
12e78 20 2f 2a 20 49 66 20 74 68 69 73 20 70 61 67 65 /* If this page
12e79 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 was previously
12e7a 61 63 71 75 69 72 65 64 20 77 69 74 68 20 6e 6f acquired with no
12e7b 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20 74 68 61 74 Content==1, that
12e7c 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 77 65 20 64 means. ** we d
12e7d 69 64 6e 27 74 20 72 65 61 6c 6c 79 20 72 65 61 idn't really rea
12e7e 64 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 d in the content
12e7f 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20 54 of the page. T
12e80 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 20 his can happen.
12e81 20 2a 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 ** (for example
12e82 29 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 20 ) when the page
12e83 69 73 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 74 is being moved t
12e84 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 o the freelist.
12e85 20 42 75 74 0a 20 20 2a 2a 20 6e 6f 77 20 77 65 But. ** now we
12e86 20 61 72 65 20 28 70 65 72 68 61 70 73 29 20 6d are (perhaps) m
12e87 6f 76 69 6e 67 20 74 68 65 20 70 61 67 65 20 6f oving the page o
12e88 66 66 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 ff of the freeli
12e89 73 74 20 66 6f 72 0a 20 20 2a 2a 20 72 65 75 73 st for. ** reus
12e8a 65 20 61 6e 64 20 77 65 20 6e 65 65 64 20 74 6f e and we need to
12e8b 20 6b 6e 6f 77 20 69 74 73 20 6f 72 69 67 69 6e know its origin
12e8c 61 6c 20 63 6f 6e 74 65 6e 74 20 73 6f 20 74 68 al content so th
12e8d 61 74 20 63 6f 6e 74 65 6e 74 0a 20 20 2a 2a 20 at content. **
12e8e 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e can be stored in
12e8f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
12e90 75 72 6e 61 6c 2e 20 20 53 6f 20 64 6f 20 74 68 urnal. So do th
12e91 65 20 72 65 61 64 20 61 74 20 74 68 69 73 0a 20 e read at this.
12e92 20 2a 2a 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 ** time.. */.
12e93 20 72 63 20 3d 20 70 61 67 65 72 5f 67 65 74 5f rc = pager_get_
12e94 63 6f 6e 74 65 6e 74 28 70 50 67 29 3b 0a 20 20 content(pPg);.
12e95 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 if( rc ){. re
12e96 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
12e97 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 /* Mark the page
12e98 20 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 as dirty. If t
12e99 68 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 he page has alre
12e9a 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e ady been written
12e9b 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 . ** to the jou
12e9c 72 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e rnal then we can
12e9d 20 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 return right aw
12e9e 61 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ay.. */. sqlit
12e9f 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 e3PcacheMakeDirt
12ea0 79 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 y(pPg);. if( pa
12ea1 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 geInJournal(pPg)
12ea2 20 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 && !subjRequire
12ea3 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 sPage(pPg) ){.
12ea4 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 pPager->dirtyC
12ea5 61 63 68 65 20 3d 20 31 3b 0a 20 20 20 20 70 50 ache = 1;. pP
12ea6 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
12ea7 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a = 1;. }else{..
12ea8 20 20 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74 /* If we get
12ea9 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65 this far, it me
12eaa 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67 ans that the pag
12eab 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 e needs to be.
12eac 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 ** written to
12ead 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
12eae 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 63 journal or the c
12eaf 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 keckpoint journa
12eb0 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68 l. ** or both
12eb1 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
12eb2 46 69 72 73 74 20 63 68 65 63 6b 20 74 6f 20 73 First check to s
12eb3 65 65 20 74 68 61 74 20 74 68 65 20 74 72 61 6e ee that the tran
12eb4 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 saction journal
12eb5 65 78 69 73 74 73 20 61 6e 64 0a 20 20 20 20 2a exists and. *
12eb6 2a 20 63 72 65 61 74 65 20 69 74 20 69 66 20 69 * create it if i
12eb7 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20 20 20 t does not..
12eb8 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
12eb9 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 Pager->state!=PA
12eba 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 GER_UNLOCK );.
12ebb 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
12ebc 67 65 72 42 65 67 69 6e 28 70 50 67 2c 20 30 29 gerBegin(pPg, 0)
12ebd 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
12ebe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
12ebf 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
12ec0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 }. assert( pP
12ec1 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
12ec2 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 ER_RESERVED );.
12ec3 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e if( !pPager->
12ec4 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 26 26 20 70 journalOpen && p
12ec5 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 Pager->useJourna
12ec6 6c 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 70 l. && p
12ec7 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
12ec8 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de!=PAGER_JOURNA
12ec9 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 LMODE_OFF ){.
12eca 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 rc = pager_op
12ecb 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 en_journal(pPage
12ecc 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 r);. if( rc
12ecd 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
12ece 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
12ecf 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 pPager->dirty
12ed0 43 61 63 68 65 20 3d 20 31 3b 0a 20 20 20 20 70 Cache = 1;. p
12ed1 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 Pager->dbModifie
12ed2 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f 2a d = 1;. . /*
12ed3 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e The transaction
12ed4 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 69 journal now exi
12ed5 73 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 20 sts and we have
12ed6 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 6e a RESERVED or an
12ed7 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 . ** EXCLUSIV
12ed8 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61 E lock on the ma
12ed9 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 in database file
12eda 2e 20 20 57 72 69 74 65 20 74 68 65 20 63 75 72 . Write the cur
12edb 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 20 rent page to.
12edc 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 ** the transact
12edd 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 69 ion journal if i
12ede 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 t is not there a
12edf 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 lready.. */.
12ee0 20 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a 6f if( !pageInJo
12ee1 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 70 50 urnal(pPg) && pP
12ee2 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 ager->journalOpe
12ee3 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 n ){. if( p
12ee4 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 Pg->pgno<=pPager
12ee5 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a ->dbOrigSize ){.
12ee6 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75 u32 cksu
12ee7 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 m;. char
12ee8 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 20 *pData2;..
12ee9 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 6e /* We should n
12eea 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 68 ever write to th
12eeb 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 e journal file t
12eec 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 20 he page that.
12eed 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 ** contains
12eee 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 6f the database lo
12eef 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 cks. The follow
12ef0 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 66 ing assert verif
12ef1 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 ies. ** t
12ef2 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 2a hat we do not. *
12ef3 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 /. assert
12ef4 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 47 ( pPg->pgno!=PAG
12ef5 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 ER_MJ_PGNO(pPage
12ef6 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 44 r) );. pD
12ef7 61 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70 50 ata2 = CODEC2(pP
12ef8 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 ager, pData, pPg
12ef9 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 20 20 ->pgno, 7);.
12efa 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 67 65 cksum = page
12efb 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 r_cksum(pPager,
12efc 28 75 38 2a 29 70 44 61 74 61 32 29 3b 0a 20 20 (u8*)pData2);.
12efd 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 rc = write
12efe 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
12eff 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 fd, pPager->jour
12f00 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67 6e nalOff, pPg->pgn
12f01 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 o);. if(
12f02 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
12f03 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
12f04 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
12f05 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61 74 Pager->jfd, pDat
12f06 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 a2, pPager->page
12f07 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20 Size,.
12f08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f09 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
12f0a 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 20 20 nalOff + 4);.
12f0b 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
12f0c 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 50 61 ournalOff += pPa
12f0d 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 3b ger->pageSize+4;
12f0e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
12f0f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
12f10 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
12f11 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 rc = write32bi
12f12 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ts(pPager->jfd,
12f13 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
12f14 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20 ff, cksum);.
12f15 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
12f16 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20 urnalOff += 4;.
12f17 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
12f18 20 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 54 20 IOTRACE(("JOUT
12f19 25 70 20 25 64 20 25 6c 6c 64 20 25 64 5c 6e 22 %p %d %lld %d\n"
12f1a 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 , pPager, pPg->p
12f1b 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20 gno, .
12f1c 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
12f1d 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67 65 ournalOff, pPage
12f1e 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b 0a 20 r->pageSize));.
12f1f 20 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 PAGER_INC
12f20 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f R(sqlite3_pager_
12f21 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0a 20 writej_count);.
12f22 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 PAGERTRAC
12f23 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20 70 E(("JOURNAL %d p
12f24 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d age %d needSync=
12f25 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 %d hash(%08x)\n"
12f26 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 50 ,. P
12f27 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
12f28 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 pPg->pgno, .
12f29 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e ((pPg->
12f2a 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
12f2b 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 61 67 _SYNC)?1:0), pag
12f2c 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 er_pagehash(pPg)
12f2d 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 ));.. /*
12f2e 45 76 65 6e 20 69 66 20 61 6e 20 49 4f 20 6f 72 Even if an IO or
12f2f 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 6f 72 20 diskfull error
12f30 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 6a occurred while j
12f31 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a 20 ournalling the.
12f32 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 ** page i
12f33 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f 76 n the block abov
12f34 65 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64 2d e, set the need-
12f35 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74 68 sync flag for th
12f36 65 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 e page..
12f37 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 68 ** Otherwise, wh
12f38 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 en the transacti
12f39 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 on is rolled bac
12f3a 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e 0a k, the logic in.
12f3b 20 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 62 ** playb
12f3c 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 77 ack_one_page() w
12f3d 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20 74 ill think that t
12f3e 68 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f he page needs to
12f3f 20 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20 20 be restored.
12f40 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 64 ** in the d
12f41 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 6e atabase file. An
12f42 64 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 d if an IO error
12f43 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f occurs while do
12f44 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20 20 ing so,.
12f45 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74 69 ** then corrupti
12f46 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a 20 on may follow..
12f47 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
12f48 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e if( !pPager->n
12f49 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 oSync ){.
12f4a 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d pPg->flags |=
12f4b 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
12f4c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 ;. pPag
12f4d 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 er->needSync = 1
12f4e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 ;. }..
12f4f 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 /* An error
12f50 20 68 61 73 20 6f 63 63 75 72 65 64 20 77 72 69 has occured wri
12f51 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 ting to the jour
12f52 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 0a 20 nal file. The .
12f53 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 ** transa
12f54 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72 6f ction will be ro
12f55 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 lled back by the
12f56 20 6c 61 79 65 72 20 61 62 6f 76 65 2e 0a 20 20 layer above..
12f57 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
12f58 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12f59 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
12f5a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
12f5b 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 70 50 }.. pP
12f5c 61 67 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 ager->nRec++;.
12f5d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
12f5e 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
12f5f 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 !=0 );. r
12f60 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 c = sqlite3Bitve
12f61 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e cSet(pPager->pIn
12f62 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 Journal, pPg->pg
12f63 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 no);. tes
12f64 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 tcase( rc==SQLIT
12f65 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 E_NOMEM );.
12f66 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
12f67 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d QLITE_OK || rc==
12f68 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a SQLITE_NOMEM );.
12f69 20 20 20 20 20 20 20 20 72 63 20 7c 3d 20 61 64 rc |= ad
12f6a 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 dToSavepointBitv
12f6b 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d ecs(pPager, pPg-
12f6c 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 >pgno);.
12f6d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12f6e 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 K ){. a
12f6f 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
12f70 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 E_NOMEM );.
12f71 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
12f72 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
12f73 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 }else{. i
12f74 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 f( !pPager->jour
12f75 6e 61 6c 53 74 61 72 74 65 64 20 26 26 20 21 70 nalStarted && !p
12f76 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b Pager->noSync ){
12f77 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 2d 3e . pPg->
12f78 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e flags |= PGHDR_N
12f79 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 EED_SYNC;.
12f7a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 pPager->need
12f7b 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 Sync = 1;.
12f7c 20 20 7d 0a 20 20 20 20 20 20 20 20 50 41 47 45 }. PAGE
12f7d 52 54 52 41 43 45 28 28 22 41 50 50 45 4e 44 20 RTRACE(("APPEND
12f7e 25 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 %d page %d needS
12f7f 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 ync=%d\n",.
12f80 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 PAGER
12f81 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d ID(pPager), pPg-
12f82 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 >pgno,.
12f83 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 ((pPg->fla
12f84 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
12f85 4e 43 29 3f 31 3a 30 29 29 29 3b 0a 20 20 20 20 NC)?1:0)));.
12f86 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
12f87 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 /* If the state
12f88 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 ment journal is
12f89 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61 67 open and the pag
12f8a 65 20 69 73 20 6e 6f 74 20 69 6e 20 69 74 2c 0a e is not in it,.
12f8b 20 20 20 20 2a 2a 20 74 68 65 6e 20 77 72 69 74 ** then writ
12f8c 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 e the current pa
12f8d 67 65 20 74 6f 20 74 68 65 20 73 74 61 74 65 6d ge to the statem
12f8e 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f ent journal. No
12f8f 74 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 te that. ** t
12f90 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 he statement jou
12f91 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64 69 66 66 rnal format diff
12f92 65 72 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 ers from the sta
12f93 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c 20 66 6f ndard journal fo
12f94 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69 6e 20 74 rmat. ** in t
12f95 68 61 74 20 69 74 20 6f 6d 69 74 73 20 74 68 65 hat it omits the
12f96 20 63 68 65 63 6b 73 75 6d 73 20 61 6e 64 20 74 checksums and t
12f97 68 65 20 68 65 61 64 65 72 2e 0a 20 20 20 20 2a he header.. *
12f98 2f 0a 20 20 20 20 69 66 28 20 73 75 62 6a 52 65 /. if( subjRe
12f99 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20 quiresPage(pPg)
12f9a 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 75 ){. rc = su
12f9b 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 bjournalPage(pPg
12f9c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
12f9d 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 61 /* Update the da
12f9e 74 61 62 61 73 65 20 73 69 7a 65 20 61 6e 64 20 tabase size and
12f9f 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 61 return.. */. a
12fa0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
12fa1 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 tate>=PAGER_SHAR
12fa2 45 44 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 ED );. if( pPag
12fa3 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 67 2d 3e er->dbSize<pPg->
12fa4 70 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 pgno ){. pPag
12fa5 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 er->dbSize = pPg
12fa6 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 69 66 28 20 ->pgno;. if(
12fa7 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d pPager->dbSize==
12fa8 28 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 (PAGER_MJ_PGNO(p
12fa9 50 61 67 65 72 29 2d 31 29 20 29 7b 0a 20 20 20 Pager)-1) ){.
12faa 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a pPager->dbSiz
12fab 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 e++;. }. }.
12fac 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
12fad 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
12fae 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 on is used to ma
12faf 72 6b 20 61 20 64 61 74 61 2d 70 61 67 65 20 61 rk a data-page a
12fb0 73 20 77 72 69 74 61 62 6c 65 2e 20 49 74 20 75 s writable. It u
12fb1 73 65 73 20 0a 2a 2a 20 70 61 67 65 72 5f 77 72 ses .** pager_wr
12fb2 69 74 65 28 29 20 74 6f 20 6f 70 65 6e 20 61 20 ite() to open a
12fb3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 69 66 journal file (if
12fb4 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 it is not alrea
12fb5 64 79 20 6f 70 65 6e 29 0a 2a 2a 20 61 6e 64 20 dy open).** and
12fb6 77 72 69 74 65 20 74 68 65 20 70 61 67 65 20 2a write the page *
12fb7 70 44 61 74 61 20 74 6f 20 74 68 65 20 6a 6f 75 pData to the jou
12fb8 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rnal..**.** The
12fb9 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 difference betwe
12fba 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
12fbb 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69 74 65 and pager_write
12fbc 28 29 20 69 73 20 74 68 61 74 20 74 68 69 73 0a () is that this.
12fbd 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f ** function also
12fbe 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 20 deals with the
12fbf 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 special case whe
12fc0 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70 61 67 re 2 or more pag
12fc1 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73 es.** fit on a s
12fc2 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f ingle disk secto
12fc3 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 r. In this case
12fc4 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e 74 20 all co-resident
12fc5 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61 pages.** must ha
12fc6 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 ve been written
12fc7 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
12fc8 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 ile before retur
12fc9 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ning..*/.SQLITE_
12fca 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
12fcb 74 65 33 50 61 67 65 72 57 72 69 74 65 28 44 62 te3PagerWrite(Db
12fcc 50 61 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a Page *pDbPage){.
12fcd 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
12fce 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a E_OK;.. PgHdr *
12fcf 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 pPg = pDbPage;.
12fd0 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
12fd1 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pPg->pPager;.
12fd2 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 53 65 63 Pgno nPagePerSec
12fd3 74 6f 72 20 3d 20 28 70 50 61 67 65 72 2d 3e 73 tor = (pPager->s
12fd4 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 67 65 72 ectorSize/pPager
12fd5 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 ->pageSize);..
12fd6 69 66 28 20 6e 50 61 67 65 50 65 72 53 65 63 74 if( nPagePerSect
12fd7 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f or>1 ){. Pgno
12fd8 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 nPageCount;
12fd9 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
12fda 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
12fdb 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 n database file
12fdc 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b */. Pgno pg1;
12fdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12fde 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f /* First page o
12fdf 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 f the sector pPg
12fe0 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 is located on.
12fe1 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 */. int nPage
12fe2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
12fe3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
12fe4 67 65 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 ges starting at
12fe5 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a pg1 to journal *
12fe6 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 /. int ii;.
12fe7 20 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d int needSync =
12fe8 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 0;.. /* Set
12fe9 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c the doNotSync fl
12fea 61 67 20 74 6f 20 31 2e 20 54 68 69 73 20 69 73 ag to 1. This is
12feb 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e 6e because we cann
12fec 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 6e ot allow a journ
12fed 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 64 65 72 al. ** header
12fee 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 62 to be written b
12fef 65 74 77 65 65 6e 20 74 68 65 20 70 61 67 65 73 etween the pages
12ff0 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 20 74 68 journaled by th
12ff1 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 is function..
12ff2 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
12ff3 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 !MEMDB );. as
12ff4 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f sert( pPager->do
12ff5 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 NotSync==0 );.
12ff6 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 pPager->doNotS
12ff7 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a ync = 1;.. /*
12ff8 20 54 68 69 73 20 74 72 69 63 6b 20 61 73 73 75 This trick assu
12ff9 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20 74 68 mes that both th
12ffa 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 e page-size and
12ffb 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72 65 0a sector-size are.
12ffc 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 65 ** an intege
12ffd 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 49 74 r power of 2. It
12ffe 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20 70 sets variable p
12fff 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 g1 to the identi
13000 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 fier. ** of t
13001 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 he first page of
13002 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 the sector pPg
13003 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a 20 is located on..
13004 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d 20 */. pg1 =
13005 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 26 ((pPg->pgno-1) &
13006 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63 74 6f ~(nPagePerSecto
13007 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20 20 r-1)) + 1;..
13008 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
13009 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 28 69 count(pPager, (i
1300a 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e 74 nt *)&nPageCount
1300b 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e );. if( pPg->
1300c 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 20 pgno>nPageCount
1300d 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d ){. nPage =
1300e 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 67 (pPg->pgno - pg
1300f 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 1)+1;. }else
13010 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65 50 65 if( (pg1+nPagePe
13011 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67 65 rSector-1)>nPage
13012 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e Count ){. n
13013 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f 75 6e Page = nPageCoun
13014 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65 6c t+1-pg1;. }el
13015 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 se{. nPage
13016 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 = nPagePerSector
13017 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
13018 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20 20 rt(nPage>0);.
13019 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70 50 67 assert(pg1<=pPg
1301a 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73 73 ->pgno);. ass
1301b 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65 29 3e ert((pg1+nPage)>
1301c 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 pPg->pgno);..
1301d 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 for(ii=0; ii<nP
1301e 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 age && rc==SQLIT
1301f 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 E_OK; ii++){.
13020 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 67 31 Pgno pg = pg1
13021 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48 64 72 +ii;. PgHdr
13022 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20 69 *pPage;. i
13023 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f f( pg==pPg->pgno
13024 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 74 76 || !sqlite3Bitv
13025 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 ecTest(pPager->p
13026 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20 29 InJournal, pg) )
13027 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 67 {. if( pg
13028 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 !=PAGER_MJ_PGNO(
13029 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 pPager) ){.
1302a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1302b 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 3PagerGet(pPager
1302c 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b 0a 20 , pg, &pPage);.
1302d 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d if( rc=
1302e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1302f 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 rc = p
13030 61 67 65 72 5f 77 72 69 74 65 28 70 50 61 67 65 ager_write(pPage
13031 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
13032 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 f( pPage->flags&
13033 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
13034 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
13035 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 needSync = 1;.
13036 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 ass
13037 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64 ert(pPager->need
13038 53 79 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 20 Sync);.
13039 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
1303a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
1303b 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ef(pPage);.
1303c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1303d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
1303e 20 28 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f (pPage = pager_
1303f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 lookup(pPager, p
13040 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 g))!=0 ){.
13041 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 if( pPage->fla
13042 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
13043 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 NC ){.
13044 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 needSync = 1;.
13045 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13046 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
13047 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 f(pPage);.
13048 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
13049 49 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 If the PGHDR_NEE
1304a 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20 73 D_SYNC flag is s
1304b 65 74 20 66 6f 72 20 61 6e 79 20 6f 66 20 74 68 et for any of th
1304c 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 0a 20 e nPage pages .
1304d 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 61 ** starting a
1304e 74 20 70 67 31 2c 20 74 68 65 6e 20 69 74 20 6e t pg1, then it n
1304f 65 65 64 73 20 74 6f 20 62 65 20 73 65 74 20 66 eeds to be set f
13050 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e 20 or all of them.
13051 42 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 77 Because. ** w
13052 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f 66 riting to any of
13053 20 74 68 65 73 65 20 6e 50 61 67 65 20 70 61 67 these nPage pag
13054 65 73 20 6d 61 79 20 64 61 6d 61 67 65 20 74 68 es may damage th
13055 65 20 6f 74 68 65 72 73 2c 20 74 68 65 0a 20 20 e others, the.
13056 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ** journal fil
13057 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 73 e must contain s
13058 79 6e 63 28 29 65 64 20 63 6f 70 69 65 73 20 6f ync()ed copies o
13059 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20 20 f all of them.
1305a 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79 20 ** before any
1305b 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 65 20 77 of them can be w
1305c 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 ritten out to th
1305d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1305e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1305f 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 needSync ){.
13060 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 assert( !MEMDB
13061 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 && pPager->noSy
13062 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 66 nc==0 );. f
13063 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 or(ii=0; ii<nPag
13064 65 20 26 26 20 6e 65 65 64 53 79 6e 63 3b 20 69 e && needSync; i
13065 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50 67 i++){. Pg
13066 48 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61 67 Hdr *pPage = pag
13067 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 er_lookup(pPager
13068 2c 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20 20 , pg1+ii);.
13069 20 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a if( pPage ){.
1306a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 2d pPage-
1306b 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f >flags |= PGHDR_
1306c 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 NEED_SYNC;.
1306d 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
1306e 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 rUnref(pPage);.
1306f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
13070 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 . assert(pP
13071 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b ager->needSync);
13072 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 . }.. asse
13073 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f rt( pPager->doNo
13074 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 tSync==1 );.
13075 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e pPager->doNotSyn
13076 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a c = 0;. }else{.
13077 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 rc = pager_w
13078 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 rite(pDbPage);.
13079 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1307a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1307b 54 52 55 45 20 69 66 20 74 68 65 20 70 61 67 65 TRUE if the page
1307c 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 given in the ar
1307d 67 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76 69 gument was previ
1307e 6f 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a 20 ously passed.**
1307f 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 to sqlite3PagerW
13080 72 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68 65 rite(). In othe
13081 72 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 r words, return
13082 54 52 55 45 20 69 66 20 69 74 20 69 73 20 6f 6b TRUE if it is ok
13083 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68 .** to change th
13084 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 e content of the
13085 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 page..*/.#ifnde
13086 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f f NDEBUG.SQLITE_
13087 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
13088 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
13089 62 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67 29 ble(DbPage *pPg)
1308a 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e {. return pPg->
1308b 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
1308c 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a Y;.}.#endif../*.
1308d 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 ** A call to thi
1308e 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20 s routine tells
1308f 74 68 65 20 70 61 67 65 72 20 74 68 61 74 20 69 the pager that i
13090 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 t is not necessa
13091 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 74 ry to.** write t
13092 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f he information o
13093 6e 20 70 61 67 65 20 70 50 67 20 62 61 63 6b 20 n page pPg back
13094 74 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 76 65 to the disk, eve
13095 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61 74 n though.** that
13096 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 6d page might be m
13097 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 arked as dirty.
13098 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 This happens, f
13099 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e or example, when
1309a 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 68 61 73 .** the page has
1309b 20 62 65 65 6e 20 61 64 64 65 64 20 61 73 20 61 been added as a
1309c 20 6c 65 61 66 20 6f 66 20 74 68 65 20 66 72 65 leaf of the fre
1309d 65 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 74 73 elist and so its
1309e 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 6c .** content no l
1309f 6f 6e 67 65 72 20 6d 61 74 74 65 72 73 2e 0a 2a onger matters..*
130a0 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79 69 *.** The overlyi
130a1 6e 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79 65 ng software laye
130a2 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 r calls this rou
130a3 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f 66 tine when all of
130a4 20 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e 20 the data.** on
130a5 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 20 69 the given page i
130a6 73 20 75 6e 75 73 65 64 2e 20 20 54 68 65 20 70 s unused. The p
130a7 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70 ager marks the p
130a8 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a age as clean so.
130a9 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 ** that it does
130aa 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65 6e 20 not get written
130ab 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 to disk..**.** T
130ac 65 73 74 73 20 73 68 6f 77 20 74 68 61 74 20 74 ests show that t
130ad 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e his optimization
130ae 2c 20 74 6f 67 65 74 68 65 72 20 77 69 74 68 20 , together with
130af 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 the.** sqlite3Pa
130b0 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 gerDontRollback(
130b1 29 20 62 65 6c 6f 77 2c 20 6d 6f 72 65 20 74 68 ) below, more th
130b2 61 6e 20 64 6f 75 62 6c 65 20 74 68 65 20 73 70 an double the sp
130b3 65 65 64 0a 2a 2a 20 6f 66 20 6c 61 72 67 65 20 eed.** of large
130b4 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f 6e INSERT operation
130b5 73 20 61 6e 64 20 71 75 61 64 72 75 70 6c 65 20 s and quadruple
130b6 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61 72 the speed of lar
130b7 67 65 20 44 45 4c 45 54 45 73 2e 0a 2a 2a 0a 2a ge DELETEs..**.*
130b8 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f 75 74 * When this rout
130b9 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 ine is called, s
130ba 65 74 20 74 68 65 20 62 69 74 20 63 6f 72 72 65 et the bit corre
130bb 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 44 62 50 sponding to pDbP
130bc 61 67 65 20 69 6e 0a 2a 2a 20 74 68 65 20 50 61 age in.** the Pa
130bd 67 65 72 2e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 ger.pAlwaysRollb
130be 61 63 6b 20 62 69 74 76 65 63 2e 20 20 53 75 62 ack bitvec. Sub
130bf 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f sequent calls to
130c0 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 .** sqlite3Pager
130c1 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 66 DontRollback() f
130c2 6f 72 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 or the same page
130c3 20 77 69 6c 6c 20 74 68 65 72 65 61 66 74 65 72 will thereafter
130c4 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 be ignored..**
130c5 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 This is necessar
130c6 79 20 74 6f 20 61 76 6f 69 64 20 61 20 70 72 6f y to avoid a pro
130c7 62 6c 65 6d 20 77 68 65 72 65 20 61 20 70 61 67 blem where a pag
130c8 65 20 77 69 74 68 20 64 61 74 61 20 69 73 20 61 e with data is a
130c9 64 64 65 64 20 74 6f 0a 2a 2a 20 74 68 65 20 66 dded to.** the f
130ca 72 65 65 6c 69 73 74 20 64 75 72 69 6e 67 20 6f reelist during o
130cb 6e 65 20 70 61 72 74 20 6f 66 20 61 20 74 72 61 ne part of a tra
130cc 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 72 65 nsaction then re
130cd 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a moved from the.*
130ce 2a 20 66 72 65 65 6c 69 73 74 20 64 75 72 69 6e * freelist durin
130cf 67 20 61 20 6c 61 74 65 72 20 70 61 72 74 20 6f g a later part o
130d0 66 20 74 68 65 20 73 61 6d 65 20 74 72 61 6e 73 f the same trans
130d1 61 63 74 69 6f 6e 20 61 6e 64 20 72 65 75 73 65 action and reuse
130d2 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 6f 74 d for some.** ot
130d3 68 65 72 20 70 75 72 70 6f 73 65 2e 20 20 57 68 her purpose. Wh
130d4 65 6e 20 69 74 20 69 73 20 66 69 72 73 74 20 61 en it is first a
130d5 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 dded to the free
130d6 6c 69 73 74 2c 20 74 68 69 73 20 72 6f 75 74 69 list, this routi
130d7 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e ne is.** called.
130d8 20 20 57 68 65 6e 20 72 65 75 73 65 64 2c 20 74 When reused, t
130d9 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 he sqlite3PagerD
130da 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 72 6f ontRollback() ro
130db 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e utine is called.
130dc 0a 2a 2a 20 42 75 74 20 62 65 63 61 75 73 65 20 .** But because
130dd 74 68 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e the page contain
130de 73 20 63 72 69 74 69 63 61 6c 20 64 61 74 61 2c s critical data,
130df 20 77 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 74 we still need t
130e0 6f 20 62 65 20 73 75 72 65 20 69 74 0a 2a 2a 20 o be sure it.**
130e1 67 65 74 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b gets rolled back
130e2 20 69 6e 20 73 70 69 74 65 20 6f 66 20 74 68 65 in spite of the
130e3 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e sqlite3PagerDon
130e4 74 52 6f 6c 6c 62 61 63 6b 28 29 20 63 61 6c 6c tRollback() call
130e5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
130e6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
130e7 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 44 62 agerDontWrite(Db
130e8 50 61 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a Page *pDbPage){.
130e9 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70 PgHdr *pPg = p
130ea 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 DbPage;. Pager
130eb 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
130ec 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b Pager;. int rc;
130ed 0a 0a 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e .. if( pPg->pgn
130ee 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 o>pPager->dbOrig
130ef 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 75 Size ){. retu
130f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
130f1 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e }. if( pPager->
130f2 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 3d pAlwaysRollback=
130f3 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 =0 ){. assert
130f4 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 ( pPager->pInJou
130f5 72 6e 61 6c 20 29 3b 0a 20 20 20 20 70 50 61 67 rnal );. pPag
130f6 65 72 2d 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 er->pAlwaysRollb
130f7 61 63 6b 20 3d 20 73 71 6c 69 74 65 33 42 69 74 ack = sqlite3Bit
130f8 76 65 63 43 72 65 61 74 65 28 70 50 61 67 65 72 vecCreate(pPager
130f9 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b 0a 20 ->dbOrigSize);.
130fa 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e if( !pPager->
130fb 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 pAlwaysRollback
130fc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
130fd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
130fe 20 20 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 }. }. rc = s
130ff 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
13100 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52 pPager->pAlwaysR
13101 6f 6c 6c 62 61 63 6b 2c 20 70 50 67 2d 3e 70 67 ollback, pPg->pg
13102 6e 6f 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d no);.. if( rc==
13103 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 70 50 SQLITE_OK && (pP
13104 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 g->flags&PGHDR_D
13105 49 52 54 59 29 20 26 26 20 70 50 61 67 65 72 2d IRTY) && pPager-
13106 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 >nSavepoint==0 )
13107 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
13108 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
13109 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 20 ER_SHARED );.
1310a 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 if( pPager->dbS
1310b 69 7a 65 3d 3d 70 50 67 2d 3e 70 67 6e 6f 20 26 ize==pPg->pgno &
1310c 26 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 & pPager->dbOrig
1310d 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 53 Size<pPager->dbS
1310e 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ize ){. /*
1310f 49 66 20 74 68 69 73 20 70 61 67 65 73 20 69 73 If this pages is
13110 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 the last page i
13111 6e 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 74 n the file and t
13112 68 65 20 66 69 6c 65 20 68 61 73 20 67 72 6f 77 he file has grow
13113 6e 0a 20 20 20 20 20 20 2a 2a 20 64 75 72 69 6e n. ** durin
13114 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 g the current tr
13115 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 ansaction, then
13116 64 6f 20 4e 4f 54 20 6d 61 72 6b 20 74 68 65 20 do NOT mark the
13117 70 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 0a 20 page as clean..
13118 20 20 20 20 20 2a 2a 20 57 68 65 6e 20 74 68 65 ** When the
13119 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 67 database file g
1311a 72 6f 77 73 2c 20 77 65 20 6d 75 73 74 20 6d 61 rows, we must ma
1311b 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 ke sure that the
1311c 20 6c 61 73 74 20 70 61 67 65 0a 20 20 20 20 20 last page.
1311d 20 2a 2a 20 67 65 74 73 20 77 72 69 74 74 65 6e ** gets written
1311e 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 20 73 at least once s
1311f 6f 20 74 68 61 74 20 74 68 65 20 64 69 73 6b 20 o that the disk
13120 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 74 68 65 file will be the
13121 20 63 6f 72 72 65 63 74 0a 20 20 20 20 20 20 2a correct. *
13122 2a 20 73 69 7a 65 2e 20 49 66 20 79 6f 75 20 64 * size. If you d
13123 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 68 69 73 o not write this
13124 20 70 61 67 65 20 61 6e 64 20 74 68 65 20 73 69 page and the si
13125 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 ze of the file.
13126 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 ** on the d
13127 69 73 6b 20 65 6e 64 73 20 75 70 20 62 65 69 6e isk ends up bein
13128 67 20 74 6f 6f 20 73 6d 61 6c 6c 2c 20 74 68 61 g too small, tha
13129 74 20 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 61 t can lead to da
1312a 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 tabase. **
1312b 63 6f 72 72 75 70 74 69 6f 6e 20 64 75 72 69 6e corruption durin
1312c 67 20 74 68 65 20 6e 65 78 74 20 74 72 61 6e 73 g the next trans
1312d 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f action.. */
1312e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1312f 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 44 PAGERTRACE(("D
13130 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20 25 ONT_WRITE page %
13131 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d d of %d\n", pPg-
13132 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 >pgno, PAGERID(p
13133 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 Pager)));.
13134 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e 20 IOTRACE(("CLEAN
13135 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 %p %d\n", pPager
13136 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 , pPg->pgno)).
13137 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c pPg->flags |
13138 3d 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 = PGHDR_DONT_WRI
13139 54 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 TE;.#ifdef SQLIT
1313a 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
1313b 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 pPg->pageHas
1313c 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 h = pager_pageha
1313d 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a sh(pPg);.#endif.
1313e 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1313f 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
13140 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72 A call to this r
13141 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20 74 68 65 outine tells the
13142 20 70 61 67 65 72 20 74 68 61 74 20 69 66 20 61 pager that if a
13143 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 rollback occurs
13144 2c 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 6e ,.** it is not n
13145 65 63 65 73 73 61 72 79 20 74 6f 20 72 65 73 74 ecessary to rest
13146 6f 72 65 20 74 68 65 20 64 61 74 61 20 6f 6e 20 ore the data on
13147 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 2e 20 the given page.
13148 20 54 68 69 73 0a 2a 2a 20 6d 65 61 6e 73 20 74 This.** means t
13149 68 61 74 20 74 68 65 20 70 61 67 65 72 20 64 6f hat the pager do
1314a 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 72 es not have to r
1314b 65 63 6f 72 64 20 74 68 65 20 67 69 76 65 6e 20 ecord the given
1314c 70 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a 20 72 page in the.** r
1314d 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
1314e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 65 20 68 61 76 .**.** If we hav
1314f 65 20 6e 6f 74 20 79 65 74 20 61 63 74 75 61 6c e not yet actual
13150 6c 79 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 ly read the cont
13151 65 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67 65 ent of this page
13152 20 28 69 66 0a 2a 2a 20 74 68 65 20 50 67 48 64 (if.** the PgHd
13153 72 2e 6e 65 65 64 52 65 61 64 20 66 6c 61 67 20 r.needRead flag
13154 69 73 20 73 65 74 29 20 74 68 65 6e 20 74 68 69 is set) then thi
13155 73 20 72 6f 75 74 69 6e 65 20 61 63 74 73 20 61 s routine acts a
13156 73 20 61 20 70 72 6f 6d 69 73 65 0a 2a 2a 20 74 s a promise.** t
13157 68 61 74 20 77 65 20 77 69 6c 6c 20 6e 65 76 65 hat we will neve
13158 72 20 6e 65 65 64 20 74 6f 20 72 65 61 64 20 74 r need to read t
13159 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 he page content
1315a 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 2a in the future..*
1315b 2a 20 73 6f 20 74 68 65 20 6e 65 65 64 52 65 61 * so the needRea
1315c 64 20 66 6c 61 67 20 63 61 6e 20 62 65 20 63 6c d flag can be cl
1315d 65 61 72 65 64 20 61 74 20 74 68 69 73 20 70 6f eared at this po
1315e 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 int..*/.SQLITE_P
1315f 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
13160 74 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c te3PagerDontRoll
13161 62 61 63 6b 28 44 62 50 61 67 65 20 2a 70 50 67 back(DbPage *pPg
13162 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 ){. Pager *pPag
13163 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
13164 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e ;. TESTONLY( in
13165 74 20 72 63 3b 20 29 20 20 2f 2a 20 52 65 74 75 t rc; ) /* Retu
13166 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 rn value from sq
13167 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 29 lite3BitvecSet()
13168 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
13169 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
1316a 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a GER_RESERVED );.
1316b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 . /* If the jou
1316c 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 rnal file is not
1316d 20 6f 70 65 6e 2c 20 6f 72 20 44 6f 6e 74 57 72 open, or DontWr
1316e 69 74 65 28 29 20 68 61 73 20 62 65 65 6e 20 63 ite() has been c
1316f 61 6c 6c 65 64 20 6f 6e 0a 20 20 2a 2a 20 74 68 alled on. ** th
13170 69 73 20 70 61 67 65 20 28 44 6f 6e 74 57 72 69 is page (DontWri
13171 74 65 28 29 20 73 65 74 73 20 74 68 65 20 50 61 te() sets the Pa
13172 67 65 72 2e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 ger.pAlwaysRollb
13173 61 63 6b 20 62 69 74 29 2c 20 74 68 65 6e 20 74 ack bit), then t
13174 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f his. ** functio
13175 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 n is a no-op..
13176 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d */. if( pPager-
13177 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 >journalOpen==0
13178 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 . || sqlite3Bi
13179 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d tvecTest(pPager-
1317a 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b >pAlwaysRollback
1317b 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 0a 20 20 20 , pPg->pgno).
1317c 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 || pPg->pgno>pPa
1317d 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 0a ger->dbOrigSize.
1317e 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b ){. return;
1317f 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c . }..#ifdef SQL
13180 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 ITE_SECURE_DELET
13181 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 E. if( sqlite3B
13182 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72 itvecTest(pPager
13183 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 ->pInJournal, pP
13184 67 2d 3e 70 67 6e 6f 29 21 3d 30 0a 20 20 20 7c g->pgno)!=0. |
13185 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 | pPg->pgno>pPag
13186 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 er->dbOrigSize )
13187 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
13188 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 }.#endif.. /* I
13189 66 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 f SECURE_DELETE
1318a 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 is disabled, the
1318b 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 n there is no wa
1318c 79 20 74 68 61 74 20 74 68 69 73 0a 20 20 2a 2a y that this. **
1318d 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 routine can be
1318e 63 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 65 called on a page
1318f 20 66 6f 72 20 77 68 69 63 68 20 73 71 6c 69 74 for which sqlit
13190 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 e3PagerDontWrite
13191 28 29 0a 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 (). ** has not
13192 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 been previously
13193 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74 68 called during th
13194 65 20 73 61 6d 65 20 74 72 61 6e 73 61 63 74 69 e same transacti
13195 6f 6e 2e 0a 20 20 2a 2a 20 41 6e 64 20 69 66 20 on.. ** And if
13196 44 6f 6e 74 57 72 69 74 65 28 29 20 68 61 73 20 DontWrite() has
13197 70 72 65 76 69 6f 75 73 6c 79 20 62 65 65 6e 20 previously been
13198 63 61 6c 6c 65 64 2c 20 74 68 65 20 66 6f 6c 6c called, the foll
13199 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 6f 6e 64 69 owing. ** condi
1319a 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 20 6d 65 tions must be me
1319b 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 28 4c 61 t.. **. ** (La
1319c 74 65 72 3a 29 20 20 4e 6f 74 20 74 72 75 65 2e ter:) Not true.
1319d 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 If the databas
1319e 65 20 69 73 20 63 6f 72 72 75 70 74 65 64 20 62 e is corrupted b
1319f 79 20 68 61 76 69 6e 67 20 64 75 70 6c 69 63 61 y having duplica
131a0 74 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6f 6e te. ** pages on
131a1 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 28 65 the freelist (e
131a2 78 3a 20 63 6f 72 72 75 70 74 39 2e 74 65 73 74 x: corrupt9.test
131a3 29 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f ) then the follo
131a4 77 69 6e 67 20 69 73 20 6e 6f 74 0a 20 20 2a 2a wing is not. **
131a5 20 6e 65 63 65 73 73 61 72 69 6c 79 20 74 72 75 necessarily tru
131a6 65 3a 0a 20 20 2a 2f 0a 20 20 2f 2a 20 61 73 73 e:. */. /* ass
131a7 65 72 74 28 20 21 70 50 67 2d 3e 69 6e 4a 6f 75 ert( !pPg->inJou
131a8 72 6e 61 6c 20 26 26 20 28 69 6e 74 29 70 50 67 rnal && (int)pPg
131a9 2d 3e 70 67 6e 6f 20 3c 3d 20 70 50 61 67 65 72 ->pgno <= pPager
131aa 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 3b 20 ->dbOrigSize );
131ab 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 */.. assert( pP
131ac 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
131ad 21 3d 30 20 29 3b 0a 20 20 70 50 67 2d 3e 66 6c !=0 );. pPg->fl
131ae 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 ags &= ~PGHDR_NE
131af 45 44 5f 52 45 41 44 3b 0a 0a 20 20 2f 2a 20 46 ED_READ;.. /* F
131b0 61 69 6c 75 72 65 20 74 6f 20 73 65 74 20 74 68 ailure to set th
131b1 65 20 62 69 74 73 20 69 6e 20 74 68 65 20 49 6e e bits in the In
131b2 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76 65 63 74 Journal bit-vect
131b3 6f 72 73 20 69 73 20 62 65 6e 69 67 6e 2e 0a 20 ors is benign..
131b4 20 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 ** It merely me
131b5 61 6e 73 20 74 68 61 74 20 77 65 20 6d 69 67 68 ans that we migh
131b6 74 20 64 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 t do some extra
131b7 77 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 work to journal
131b8 61 20 70 61 67 65 0a 20 20 2a 2a 20 74 68 61 74 a page. ** that
131b9 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
131ba 6f 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 o be journaled.
131bb 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 62 Nevertheless, b
131bc 65 20 73 75 72 65 20 74 6f 20 74 65 73 74 20 74 e sure to test t
131bd 68 65 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 65 he. ** case whe
131be 72 65 20 61 20 6d 61 6c 6c 6f 63 20 65 72 72 6f re a malloc erro
131bf 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 r occurs while t
131c0 72 79 69 6e 67 20 74 6f 20 73 65 74 20 61 20 62 rying to set a b
131c1 69 74 20 69 6e 20 61 20 0a 20 20 2a 2a 20 62 69 it in a . ** bi
131c2 74 20 76 65 63 74 6f 72 2e 0a 20 20 2a 2f 0a 20 t vector.. */.
131c3 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
131c4 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 54 ignMalloc();. T
131c5 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 ESTONLY( rc = )
131c6 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 sqlite3BitvecSet
131c7 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 (pPager->pInJour
131c8 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b nal, pPg->pgno);
131c9 0a 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d . testcase( rc=
131ca 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
131cb 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 . TESTONLY( rc
131cc 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69 = ) addToSavepoi
131cd 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72 ntBitvecs(pPager
131ce 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 , pPg->pgno);.
131cf 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 testcase( rc==SQ
131d0 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 LITE_NOMEM );.
131d1 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
131d2 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 0a 20 20 50 41 Malloc();... PA
131d3 47 45 52 54 52 41 43 45 28 28 22 44 4f 4e 54 5f GERTRACE(("DONT_
131d4 52 4f 4c 4c 42 41 43 4b 20 70 61 67 65 20 25 64 ROLLBACK page %d
131d5 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e of %d\n", pPg->
131d6 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50 pgno, PAGERID(pP
131d7 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54 52 41 ager)));. IOTRA
131d8 43 45 28 28 22 47 41 52 42 41 47 45 20 25 70 20 CE(("GARBAGE %p
131d9 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 %d\n", pPager, p
131da 50 67 2d 3e 70 67 6e 6f 29 29 0a 7d 0a 0a 0a 2f Pg->pgno)).}.../
131db 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
131dc 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 e is called to i
131dd 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 64 61 74 ncrement the dat
131de 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 abase file chang
131df 65 2d 63 6f 75 6e 74 65 72 2c 0a 2a 2a 20 73 74 e-counter,.** st
131e0 6f 72 65 64 20 61 74 20 62 79 74 65 20 32 34 20 ored at byte 24
131e1 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c of the pager fil
131e2 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
131e3 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e pager_incr_chan
131e4 67 65 63 6f 75 6e 74 65 72 28 50 61 67 65 72 20 gecounter(Pager
131e5 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 44 *pPager, int isD
131e6 69 72 65 63 74 29 7b 0a 20 20 50 67 48 64 72 20 irect){. PgHdr
131e7 2a 70 50 67 48 64 72 3b 0a 20 20 75 33 32 20 63 *pPgHdr;. u32 c
131e8 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 3b 0a 20 hange_counter;.
131e9 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
131ea 5f 4f 4b 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 _OK;..#ifndef SQ
131eb 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d LITE_ENABLE_ATOM
131ec 49 43 5f 57 52 49 54 45 0a 20 20 61 73 73 65 72 IC_WRITE. asser
131ed 74 28 20 69 73 44 69 72 65 63 74 3d 3d 30 20 29 t( isDirect==0 )
131ee 3b 20 20 2f 2a 20 69 73 44 69 72 65 63 74 20 69 ; /* isDirect i
131ef 73 20 6f 6e 6c 79 20 74 72 75 65 20 66 6f 72 20 s only true for
131f0 61 74 6f 6d 69 63 20 77 72 69 74 65 73 20 2a 2f atomic writes */
131f1 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 70 .#endif. if( !p
131f2 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
131f3 6e 74 44 6f 6e 65 20 26 26 20 70 50 61 67 65 72 ntDone && pPager
131f4 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20 20 ->dbSize>0 ){.
131f5 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 31 /* Open page 1
131f6 20 6f 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 of the file for
131f7 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 writing. */.
131f8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
131f9 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 31 2c erGet(pPager, 1,
131fa 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20 20 69 &pPgHdr);. i
131fb 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
131fc 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 ) return rc;..
131fd 20 20 20 69 66 28 20 21 69 73 44 69 72 65 63 74 if( !isDirect
131fe 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
131ff 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
13200 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 20 20 (pPgHdr);.
13201 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
13202 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c K ){. sql
13203 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
13204 50 67 48 64 72 29 3b 0a 20 20 20 20 20 20 20 20 PgHdr);.
13205 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
13206 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a }. }.. /*
13207 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 Increment the v
13208 61 6c 75 65 20 6a 75 73 74 20 72 65 61 64 20 61 alue just read a
13209 6e 64 20 77 72 69 74 65 20 69 74 20 62 61 63 6b nd write it back
1320a 20 74 6f 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a to byte 24. */.
1320b 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 change_count
1320c 65 72 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 er = sqlite3Get4
1320d 62 79 74 65 28 28 75 38 2a 29 70 50 61 67 65 72 byte((u8*)pPager
1320e 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 3b 0a 20 ->dbFileVers);.
1320f 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 change_counte
13210 72 2b 2b 3b 0a 20 20 20 20 70 75 74 33 32 62 69 r++;. put32bi
13211 74 73 28 28 28 63 68 61 72 2a 29 70 50 67 48 64 ts(((char*)pPgHd
13212 72 2d 3e 70 44 61 74 61 29 2b 32 34 2c 20 63 68 r->pData)+24, ch
13213 61 6e 67 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a ange_counter);..
13214 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
13215 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 ABLE_ATOMIC_WRIT
13216 45 0a 20 20 20 20 69 66 28 20 69 73 44 69 72 65 E. if( isDire
13217 63 74 20 26 26 20 70 50 61 67 65 72 2d 3e 66 64 ct && pPager->fd
13218 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 ->pMethods ){.
13219 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a const void *
1321a 7a 42 75 66 20 3d 20 70 50 67 48 64 72 2d 3e 70 zBuf = pPgHdr->p
1321b 44 61 74 61 3b 0a 20 20 20 20 20 20 61 73 73 65 Data;. asse
1321c 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 46 69 rt( pPager->dbFi
1321d 6c 65 53 69 7a 65 3e 30 20 29 3b 0a 20 20 20 20 leSize>0 );.
1321e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1321f 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 Write(pPager->fd
13220 2c 20 7a 42 75 66 2c 20 70 50 61 67 65 72 2d 3e , zBuf, pPager->
13221 70 61 67 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20 pageSize, 0);.
13222 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
13223 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 70 /* Release the p
13224 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 2a age reference. *
13225 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 /. sqlite3Pag
13226 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b erUnref(pPgHdr);
13227 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 . pPager->cha
13228 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 ngeCountDone = 1
13229 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1322a 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 c;.}../*.** Sync
1322b 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 the pager file
1322c 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 to disk..*/.SQLI
1322d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1322e 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 qlite3PagerSync(
1322f 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
13230 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 int rc;. if(
13231 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 72 63 20 MEMDB ){. rc
13232 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
13233 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 else{. rc = s
13234 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 qlite3OsSync(pPa
13235 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d ger->fd, pPager-
13236 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 >sync_flags);.
13237 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
13238 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 ../*.** Sync the
13239 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 database file f
1323a 6f 72 20 74 68 65 20 70 61 67 65 72 20 70 50 61 or the pager pPa
1323b 67 65 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f 69 ger. zMaster poi
1323c 6e 74 73 20 74 6f 20 74 68 65 20 6e 61 6d 65 0a nts to the name.
1323d 2a 2a 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a ** of a master j
1323e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 ournal file that
1323f 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 should be writt
13240 65 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 69 en into the indi
13241 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e 61 vidual.** journa
13242 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72 20 l file. zMaster
13243 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68 69 may be NULL, whi
13244 63 68 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 ch is interprete
13245 64 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a 2a d as no master.*
13246 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69 6e * journal (a sin
13247 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 gle database tra
13248 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a nsaction)..**.**
13249 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e This routine en
1324a 73 75 72 65 73 20 74 68 61 74 20 74 68 65 20 6a sures that the j
1324b 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 ournal is synced
1324c 2c 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 , all dirty page
1324d 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 s written.** to
1324e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1324f 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 e and the databa
13250 73 65 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20 se file synced.
13251 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74 The only thing t
13252 68 61 74 0a 2a 2a 20 72 65 6d 61 69 6e 73 20 74 hat.** remains t
13253 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 o commit the tra
13254 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f 20 64 nsaction is to d
13255 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 elete the journa
13256 6c 20 66 69 6c 65 20 28 6f 72 0a 2a 2a 20 6d 61 l file (or.** ma
13257 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
13258 65 20 69 66 20 73 70 65 63 69 66 69 65 64 29 2e e if specified).
13259 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
1325a 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 4c if zMaster==NUL
1325b 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 L, this does not
1325c 20 6f 76 65 72 77 72 69 74 65 20 61 20 70 72 65 overwrite a pre
1325d 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20 70 vious value.** p
1325e 61 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c 69 assed to an sqli
1325f 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 te3PagerCommitPh
13260 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a 2a aseOne() call..*
13261 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e 61 *.** If the fina
13262 6c 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e 6f l parameter - no
13263 53 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c 20 Sync - is true,
13264 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 then the databas
13265 65 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a 2a e file itself.**
13266 20 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e 20 is not synced.
13267 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 The caller must
13268 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 call sqlite3Page
13269 72 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c 79 rSync() directly
1326a 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65 20 to.** sync the
1326b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 database file be
1326c 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f 6d fore calling Com
1326d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 74 6f mitPhaseTwo() to
1326e 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 6a delete the.** j
1326f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 74 ournal file in t
13270 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c his case..*/.SQL
13271 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
13272 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
13273 69 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 50 61 itPhaseOne(. Pa
13274 67 65 72 20 2a 70 50 61 67 65 72 2c 20 0a 20 20 ger *pPager, .
13275 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
13276 74 65 72 2c 20 0a 20 20 69 6e 74 20 6e 6f 53 79 ter, . int noSy
13277 6e 63 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d nc.){. int rc =
13278 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 SQLITE_OK;.. i
13279 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f f( pPager->errCo
1327a 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e de ){. return
1327b 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
1327c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e ;. }.. /* If n
1327d 6f 20 63 68 61 6e 67 65 73 20 68 61 76 65 20 62 o changes have b
1327e 65 65 6e 20 6d 61 64 65 2c 20 77 65 20 63 61 6e een made, we can
1327f 20 6c 65 61 76 65 20 74 68 65 20 74 72 61 6e 73 leave the trans
13280 61 63 74 69 6f 6e 20 65 61 72 6c 79 2e 0a 20 20 action early..
13281 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d */. if( pPager-
13282 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26 >dbModified==0 &
13283 26 0a 20 20 20 20 20 20 20 20 28 70 50 61 67 65 &. (pPage
13284 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d r->journalMode!=
13285 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
13286 45 5f 44 45 4c 45 54 45 20 7c 7c 0a 20 20 20 20 E_DELETE ||.
13287 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78 pPager->ex
13288 63 6c 75 73 69 76 65 4d 6f 64 65 21 3d 30 29 20 clusiveMode!=0)
13289 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
1328a 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 Pager->dirtyCach
1328b 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e e==0 || pPager->
1328c 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29 journalOpen==0 )
1328d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
1328e 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50 ITE_OK;. }.. P
1328f 41 47 45 52 54 52 41 43 45 28 28 22 44 41 54 41 AGERTRACE(("DATA
13290 42 41 53 45 20 53 59 4e 43 3a 20 46 69 6c 65 3d BASE SYNC: File=
13291 25 73 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e 53 %s zMaster=%s nS
13292 69 7a 65 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20 ize=%d\n", .
13293 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e pPager->zFilen
13294 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 ame, zMaster, pP
13295 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 29 3b 0a ager->dbSize));.
13296 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 . /* If this is
13297 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 an in-memory db
13298 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 61 , or no pages ha
13299 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 ve been written
1329a 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20 20 2a 2a to, or this. **
1329b 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c function has al
1329c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 ready been calle
1329d 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 d, it is a no-op
1329e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 .. */. if( pPa
1329f 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 ger->state!=PAGE
132a0 52 5f 53 59 4e 43 45 44 20 26 26 20 21 4d 45 4d R_SYNCED && !MEM
132a1 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 64 69 DB && pPager->di
132a2 72 74 79 43 61 63 68 65 20 29 7b 0a 20 20 20 20 rtyCache ){.
132a3 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 23 69 66 PgHdr *pPg;..#if
132a4 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
132a5 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 E_ATOMIC_WRITE.
132a6 20 20 20 2f 2a 20 54 68 65 20 61 74 6f 6d 69 63 /* The atomic
132a7 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 -write optimizat
132a8 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 ion can be used
132a9 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 if all of the.
132aa 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 ** following a
132ab 72 65 20 74 72 75 65 3a 0a 20 20 20 20 2a 2a 0a re true:. **.
132ac 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 ** + The
132ad 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 file-system supp
132ae 6f 72 74 73 20 74 68 65 20 61 74 6f 6d 69 63 2d orts the atomic-
132af 77 72 69 74 65 20 70 72 6f 70 65 72 74 79 20 66 write property f
132b0 6f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 or. ** b
132b1 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 locks of size pa
132b2 67 65 2d 73 69 7a 65 2c 20 61 6e 64 0a 20 20 20 ge-size, and.
132b3 20 2a 2a 20 20 20 20 2b 20 54 68 69 73 20 63 6f ** + This co
132b4 6d 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 mmit is not part
132b5 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 of a multi-file
132b6 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e transaction, an
132b7 64 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 45 78 d. ** + Ex
132b8 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68 actly one page h
132b9 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 as been modified
132ba 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68 and store in th
132bb 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
132bc 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 **. ** If
132bd 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f the optimizatio
132be 6e 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 74 n can be used, t
132bf 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hen the journal
132c0 66 69 6c 65 20 77 69 6c 6c 20 6e 65 76 65 72 0a file will never.
132c1 20 20 20 20 2a 2a 20 62 65 20 63 72 65 61 74 65 ** be create
132c2 64 20 66 6f 72 20 74 68 69 73 20 74 72 61 6e 73 d for this trans
132c3 61 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 action.. */.
132c4 20 20 20 69 6e 74 20 75 73 65 41 74 6f 6d 69 63 int useAtomic
132c5 57 72 69 74 65 3b 0a 20 20 20 20 70 50 67 20 3d Write;. pPg =
132c6 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 sqlite3PcacheDi
132c7 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e rtyList(pPager->
132c8 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20 75 73 pPCache);. us
132c9 65 41 74 6f 6d 69 63 57 72 69 74 65 20 3d 20 28 eAtomicWrite = (
132ca 0a 20 20 20 20 20 20 20 20 21 7a 4d 61 73 74 65 . !zMaste
132cb 72 20 26 26 20 0a 20 20 20 20 20 20 20 20 70 50 r && . pP
132cc 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 ager->journalOpe
132cd 6e 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 61 n &&. pPa
132ce 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d ger->journalOff=
132cf 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 28 =jrnlBufferSize(
132d0 70 50 61 67 65 72 29 20 26 26 20 0a 20 20 20 20 pPager) && .
132d1 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
132d2 7a 65 3e 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 ze>=pPager->dbFi
132d3 6c 65 53 69 7a 65 20 26 26 20 0a 20 20 20 20 20 leSize && .
132d4 20 20 20 28 70 50 67 3d 3d 30 20 7c 7c 20 70 50 (pPg==0 || pP
132d5 67 2d 3e 70 44 69 72 74 79 3d 3d 30 29 0a 20 20 g->pDirty==0).
132d6 20 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 );. assert(
132d7 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
132d8 4f 70 65 6e 20 7c 7c 20 70 50 61 67 65 72 2d 3e Open || pPager->
132d9 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
132da 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
132db 46 46 20 29 3b 0a 20 20 20 20 69 66 28 20 75 73 FF );. if( us
132dc 65 41 74 6f 6d 69 63 57 72 69 74 65 20 29 7b 0a eAtomicWrite ){.
132dd 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 /* Update
132de 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 69 the nRec field i
132df 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
132e0 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 le. */. int
132e1 20 6f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 offset = pPager
132e2 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 2b 20 73 ->journalHdr + s
132e3 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
132e4 67 69 63 29 3b 0a 20 20 20 20 20 20 61 73 73 65 gic);. asse
132e5 72 74 28 70 50 61 67 65 72 2d 3e 6e 52 65 63 3d rt(pPager->nRec=
132e6 3d 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 =1);. rc =
132e7 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 write32bits(pPag
132e8 65 72 2d 3e 6a 66 64 2c 20 6f 66 66 73 65 74 2c er->jfd, offset,
132e9 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a pPager->nRec);.
132ea 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 . /* Update
132eb 20 74 68 65 20 64 62 20 66 69 6c 65 20 63 68 61 the db file cha
132ec 6e 67 65 20 63 6f 75 6e 74 65 72 2e 20 54 68 65 nge counter. The
132ed 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 following call
132ee 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 20 20 20 20 will modify.
132ef 20 20 2a 2a 20 74 68 65 20 69 6e 2d 6d 65 6d 6f ** the in-memo
132f0 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ry representatio
132f1 6e 20 6f 66 20 70 61 67 65 20 31 20 74 6f 20 69 n of page 1 to i
132f2 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64 61 74 nclude the updat
132f3 65 64 0a 20 20 20 20 20 20 2a 2a 20 63 68 61 6e ed. ** chan
132f4 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 20 74 ge counter and t
132f5 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 20 31 hen write page 1
132f6 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65 directly to the
132f7 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 database.
132f8 2a 2a 20 66 69 6c 65 2e 20 42 65 63 61 75 73 65 ** file. Because
132f9 20 6f 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 of the atomic-w
132fa 72 69 74 65 20 70 72 6f 70 65 72 74 79 20 6f 66 rite property of
132fb 20 74 68 65 20 68 6f 73 74 20 66 69 6c 65 2d 73 the host file-s
132fc 79 73 74 65 6d 2c 20 0a 20 20 20 20 20 20 2a 2a ystem, . **
132fd 20 74 68 69 73 20 69 73 20 73 61 66 65 2e 0a 20 this is safe..
132fe 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
132ff 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
13300 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
13301 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 pager_incr_chang
13302 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 2c ecounter(pPager,
13303 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 1);. }.
13304 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
13305 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 = sqlite3Journa
13306 6c 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e lCreate(pPager->
13307 6a 66 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 jfd);. }..
13308 20 69 66 28 20 21 75 73 65 41 74 6f 6d 69 63 57 if( !useAtomicW
13309 72 69 74 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 rite && rc==SQLI
1330a 54 45 5f 4f 4b 20 29 0a 23 65 6e 64 69 66 0a 0a TE_OK ).#endif..
1330b 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 73 74 /* If a mast
1330c 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
1330d 6e 61 6d 65 20 68 61 73 20 61 6c 72 65 61 64 79 name has already
1330e 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
1330f 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 the. ** jour
13310 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 6e nal file, then n
13311 6f 20 73 79 6e 63 20 69 73 20 72 65 71 75 69 72 o sync is requir
13312 65 64 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 ed. This happens
13313 20 77 68 65 6e 20 69 74 20 69 73 0a 20 20 20 20 when it is.
13314 2a 2a 20 77 72 69 74 74 65 6e 2c 20 74 68 65 6e ** written, then
13315 20 74 68 65 20 70 72 6f 63 65 73 73 20 66 61 69 the process fai
13316 6c 73 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 ls to upgrade fr
13317 6f 6d 20 61 20 52 45 53 45 52 56 45 44 20 74 6f om a RESERVED to
13318 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 an. ** EXCLU
13319 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 65 20 6e SIVE lock. The n
1331a 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 72 6f ext time the pro
1331b 63 65 73 73 20 74 72 69 65 73 20 74 6f 20 63 6f cess tries to co
1331c 6d 6d 69 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 mmit the. **
1331d 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 68 65 20 transaction the
1331e 6d 2d 6a 20 6e 61 6d 65 20 77 69 6c 6c 20 68 61 m-j name will ha
1331f 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ve already been
13320 77 72 69 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a written.. */.
13321 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d if( !pPager-
13322 3e 73 65 74 4d 61 73 74 65 72 20 29 7b 0a 20 20 >setMaster ){.
13323 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69 rc = pager_i
13324 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 ncr_changecounte
13325 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 r(pPager, 0);.
13326 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
13327 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e TE_OK ) goto syn
13328 63 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 69 66 c_exit;. if
13329 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
1332a 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 lMode!=PAGER_JOU
1332b 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a RNALMODE_OFF ){.
1332c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1332d 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
1332e 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
1332f 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 67 65 72 r->dbSize<pPager
13330 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a ->dbOrigSize ){.
13331 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 /* If
13332 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
13333 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61 has made the da
13334 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20 tabase smaller,
13335 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20 then all pages.
13336 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 69 6e ** bein
13337 67 20 64 69 73 63 61 72 64 65 64 20 62 79 20 74 g discarded by t
13338 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 6d 75 he truncation mu
13339 73 74 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f st be written to
1333a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 the journal.
1333b 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 0a ** file..
1333c 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
1333d 20 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 0a 20 Pgno i;.
1333e 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 53 Pgno iS
1333f 6b 69 70 20 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 kip = PAGER_MJ_P
13340 47 4e 4f 28 70 50 61 67 65 72 29 3b 0a 20 20 20 GNO(pPager);.
13341 20 20 20 20 20 20 20 50 67 6e 6f 20 64 62 53 69 Pgno dbSi
13342 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 ze = pPager->dbS
13343 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 ize;. p
13344 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 Pager->dbSize =
13345 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
13346 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f ze;. fo
13347 72 28 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 69 r( i=dbSize+1; i
13348 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 <=pPager->dbOrig
13349 53 69 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 Size; i++ ){.
1334a 20 20 20 20 20 20 20 20 20 69 66 28 20 21 73 71 if( !sq
1334b 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
1334c 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
1334d 61 6c 2c 20 69 29 20 26 26 20 69 21 3d 69 53 6b al, i) && i!=iSk
1334e 69 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ip ){.
1334f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13350 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c PagerGet(pPager,
13351 20 69 2c 20 26 70 50 67 29 3b 0a 20 20 20 20 20 i, &pPg);.
13352 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 if( rc!
13353 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
13354 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 o sync_exit;.
13355 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
13356 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
13357 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 e(pPg);.
13358 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
13359 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 erUnref(pPg);.
1335a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
1335b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
1335c 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a goto sync_exit;.
1335d 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1335e 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 } .
1335f 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 pPager->dbS
13360 69 7a 65 20 3d 20 64 62 53 69 7a 65 3b 0a 20 20 ize = dbSize;.
13361 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
13362 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 rc = writ
13363 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 eMasterJournal(p
13364 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b Pager, zMaster);
13365 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
13366 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
13367 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 o sync_exit;.
13368 20 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f rc = syncJo
13369 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
1336a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1336b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1336c 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 OK ) goto sync_e
1336d 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 57 72 69 xit;.. /* Wri
1336e 74 65 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 te all dirty pag
1336f 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 es to the databa
13370 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 70 se file */. p
13371 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 Pg = sqlite3Pcac
13372 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 heDirtyList(pPag
13373 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
13374 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 rc = pager_wri
13375 74 65 5f 70 61 67 65 6c 69 73 74 28 70 50 67 29 te_pagelist(pPg)
13376 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
13377 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13378 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
13379 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
1337a 44 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 D );. /* Th
1337b 65 20 65 72 72 6f 72 20 6d 69 67 68 74 20 68 61 e error might ha
1337c 76 65 20 6c 65 66 74 20 74 68 65 20 64 69 72 74 ve left the dirt
1337d 79 20 6c 69 73 74 20 61 6c 6c 20 66 6f 75 6c 65 y list all foule
1337e 64 20 75 70 20 68 65 72 65 2c 0a 20 20 20 20 20 d up here,.
1337f 20 2a 2a 20 62 75 74 20 74 68 61 74 20 64 6f 65 ** but that doe
13380 73 20 6e 6f 74 20 6d 61 74 74 65 72 20 62 65 63 s not matter bec
13381 61 75 73 65 20 69 66 20 74 68 65 20 69 66 20 74 ause if the if t
13382 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 64 69 he dirty list di
13383 64 0a 20 20 20 20 20 20 2a 2a 20 67 65 74 20 63 d. ** get c
13384 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 74 orrupted, then t
13385 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 he transaction w
13386 69 6c 6c 20 72 6f 6c 6c 20 62 61 63 6b 20 61 6e ill roll back an
13387 64 0a 20 20 20 20 20 20 2a 2a 20 64 69 73 63 61 d. ** disca
13388 72 64 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 rd the dirty lis
13389 74 2e 20 20 54 68 65 72 65 20 69 73 20 61 6e 20 t. There is an
1338a 61 73 73 65 72 74 20 69 6e 0a 20 20 20 20 20 20 assert in.
1338b 2a 2a 20 70 61 67 65 72 5f 67 65 74 5f 61 6c 6c ** pager_get_all
1338c 5f 64 69 72 74 79 5f 70 61 67 65 73 28 29 20 74 _dirty_pages() t
1338d 68 61 74 20 76 65 72 69 66 69 65 73 20 74 68 61 hat verifies tha
1338e 74 20 6e 6f 20 61 74 74 65 6d 70 74 0a 20 20 20 t no attempt.
1338f 20 20 20 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f ** is made to
13390 20 75 73 65 20 61 6e 20 69 6e 76 61 6c 69 64 20 use an invalid
13391 64 69 72 74 79 20 6c 69 73 74 2e 0a 20 20 20 20 dirty list..
13392 20 20 2a 2f 0a 20 20 20 20 20 20 67 6f 74 6f 20 */. goto
13393 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 7d sync_exit;. }
13394 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 . sqlite3Pcac
13395 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 heCleanAll(pPage
13396 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 r->pPCache);..
13397 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 if( pPager->db
13398 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 46 Size<pPager->dbF
13399 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 ileSize ){.
1339a 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
1339b 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 >state>=PAGER_EX
1339c 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 20 20 20 CLUSIVE );.
1339d 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e rc = pager_trun
1339e 63 61 74 65 28 70 50 61 67 65 72 2c 20 70 50 61 cate(pPager, pPa
1339f 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 ger->dbSize);.
133a0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
133a1 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e TE_OK ) goto syn
133a2 63 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a 0a 20 c_exit;. }..
133a3 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 64 /* Sync the d
133a4 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f atabase file. */
133a5 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 . if( !pPager
133a6 2d 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f 53 ->noSync && !noS
133a7 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ync ){. rc
133a8 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
133a9 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 pPager->fd, pPag
133aa 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b er->sync_flags);
133ab 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 . }. IOTRA
133ac 43 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c 6e CE(("DBSYNC %p\n
133ad 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 ", pPager))..
133ae 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d pPager->state =
133af 20 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a 20 PAGER_SYNCED;.
133b0 20 7d 0a 0a 73 79 6e 63 5f 65 78 69 74 3a 0a 20 }..sync_exit:.
133b1 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
133b2 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 7b IOERR_BLOCKED ){
133b3 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f 69 6e . /* pager_in
133b4 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 cr_changecounter
133b5 28 29 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 () may attempt t
133b6 6f 20 6f 62 74 61 69 6e 20 61 6e 20 65 78 63 6c o obtain an excl
133b7 75 73 69 76 65 0a 20 20 20 20 20 2a 20 6c 6f 63 usive. * loc
133b8 6b 20 74 6f 20 73 70 69 6c 6c 20 74 68 65 20 63 k to spill the c
133b9 61 63 68 65 20 61 6e 64 20 72 65 74 75 72 6e 20 ache and return
133ba 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 2e 20 42 IOERR_BLOCKED. B
133bb 75 74 20 73 69 6e 63 65 20 0a 20 20 20 20 20 2a ut since . *
133bc 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 there is no cha
133bd 6e 63 65 20 74 68 65 20 63 61 63 68 65 20 69 73 nce the cache is
133be 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2c 20 69 inconsistent, i
133bf 74 20 69 73 0a 20 20 20 20 20 2a 20 62 65 74 74 t is. * bett
133c0 65 72 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c er to return SQL
133c1 49 54 45 5f 42 55 53 59 2e 0a 20 20 20 20 20 2a ITE_BUSY.. *
133c2 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 /. rc = SQLIT
133c3 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 72 65 E_BUSY;. }. re
133c4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
133c5 2a 2a 20 43 6f 6d 6d 69 74 20 61 6c 6c 20 63 68 ** Commit all ch
133c6 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 anges to the dat
133c7 61 62 61 73 65 20 61 6e 64 20 72 65 6c 65 61 73 abase and releas
133c8 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b e the write lock
133c9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 ..**.** If the c
133ca 6f 6d 6d 69 74 20 66 61 69 6c 73 20 66 6f 72 20 ommit fails for
133cb 61 6e 79 20 72 65 61 73 6f 6e 2c 20 61 20 72 6f any reason, a ro
133cc 6c 6c 62 61 63 6b 20 61 74 74 65 6d 70 74 20 69 llback attempt i
133cd 73 20 6d 61 64 65 0a 2a 2a 20 61 6e 64 20 61 6e s made.** and an
133ce 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
133cf 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65 eturned. If the
133d0 20 63 6f 6d 6d 69 74 20 77 6f 72 6b 65 64 2c 20 commit worked,
133d1 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 SQLITE_OK.** is
133d2 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c returned..*/.SQL
133d3 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
133d4 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
133d5 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 65 72 itPhaseTwo(Pager
133d6 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 *pPager){. int
133d7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
133d8 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e .. if( pPager->
133d9 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 errCode ){. r
133da 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 eturn pPager->er
133db 72 43 6f 64 65 3b 0a 20 20 7d 0a 20 20 69 66 28 rCode;. }. if(
133dc 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 pPager->state<P
133dd 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b AGER_RESERVED ){
133de 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
133df 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 TE_ERROR;. }.
133e0 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f if( pPager->dbMo
133e1 64 69 66 69 65 64 3d 3d 30 20 26 26 0a 20 20 20 dified==0 &&.
133e2 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 6a 6f (pPager->jo
133e3 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 urnalMode!=PAGER
133e4 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c _JOURNALMODE_DEL
133e5 45 54 45 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 ETE ||.
133e6 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
133e7 76 65 4d 6f 64 65 21 3d 30 29 20 29 7b 0a 20 20 veMode!=0) ){.
133e8 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
133e9 2d 3e 64 69 72 74 79 43 61 63 68 65 3d 3d 30 20 ->dirtyCache==0
133ea 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e || pPager->journ
133eb 61 6c 4f 70 65 6e 3d 3d 30 20 29 3b 0a 20 20 20 alOpen==0 );.
133ec 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
133ed 4b 3b 0a 20 20 7d 0a 20 20 50 41 47 45 52 54 52 K;. }. PAGERTR
133ee 41 43 45 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c ACE(("COMMIT %d\
133ef 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
133f0 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 er)));. assert(
133f1 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
133f2 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20 PAGER_SYNCED ||
133f3 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65 72 MEMDB || !pPager
133f4 2d 3e 64 69 72 74 79 43 61 63 68 65 20 29 3b 0a ->dirtyCache );.
133f5 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 rc = pager_end
133f6 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 _transaction(pPa
133f7 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 ger, pPager->set
133f8 4d 61 73 74 65 72 29 3b 0a 20 20 72 63 20 3d 20 Master);. rc =
133f9 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
133fa 65 72 2c 20 72 63 29 3b 0a 20 20 72 65 74 75 72 er, rc);. retur
133fb 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
133fc 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e ollback all chan
133fd 67 65 73 2e 20 20 54 68 65 20 64 61 74 61 62 61 ges. The databa
133fe 73 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f se falls back to
133ff 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d 6f PAGER_SHARED mo
13400 64 65 2e 0a 2a 2a 20 41 6c 6c 20 69 6e 2d 6d 65 de..** All in-me
13401 6d 6f 72 79 20 63 61 63 68 65 20 70 61 67 65 73 mory cache pages
13402 20 72 65 76 65 72 74 20 74 6f 20 74 68 65 69 72 revert to their
13403 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 20 63 original data c
13404 6f 6e 74 65 6e 74 73 2e 0a 2a 2a 20 54 68 65 20 ontents..** The
13405 6a 6f 75 72 6e 61 6c 20 69 73 20 64 65 6c 65 74 journal is delet
13406 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ed..**.** This r
13407 6f 75 74 69 6e 65 20 63 61 6e 6e 6f 74 20 66 61 outine cannot fa
13408 69 6c 20 75 6e 6c 65 73 73 20 73 6f 6d 65 20 6f il unless some o
13409 74 68 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 ther process is
1340a 6e 6f 74 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a not following.**
1340b 20 74 68 65 20 63 6f 72 72 65 63 74 20 6c 6f 63 the correct loc
1340c 6b 69 6e 67 20 70 72 6f 74 6f 63 6f 6c 20 6f 72 king protocol or
1340d 20 75 6e 6c 65 73 73 20 73 6f 6d 65 20 6f 74 68 unless some oth
1340e 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69 73 er.** process is
1340f 20 77 72 69 74 69 6e 67 20 74 72 61 73 68 20 69 writing trash i
13410 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 nto the journal
13411 66 69 6c 65 20 28 53 51 4c 49 54 45 5f 43 4f 52 file (SQLITE_COR
13412 52 55 50 54 29 20 6f 72 0a 2a 2a 20 75 6e 6c 65 RUPT) or.** unle
13413 73 73 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f ss a prior mallo
13414 63 28 29 20 66 61 69 6c 65 64 20 28 53 51 4c 49 c() failed (SQLI
13415 54 45 5f 4e 4f 4d 45 4d 29 2e 20 20 41 70 70 72 TE_NOMEM). Appr
13416 6f 70 72 69 61 74 65 20 65 72 72 6f 72 0a 2a 2a opriate error.**
13417 20 63 6f 64 65 73 20 61 72 65 20 72 65 74 75 72 codes are retur
13418 6e 65 64 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 ned for all thes
13419 65 20 6f 63 63 61 73 69 6f 6e 73 2e 20 20 4f 74 e occasions. Ot
1341a 68 65 72 77 69 73 65 2c 0a 2a 2a 20 53 51 4c 49 herwise,.** SQLI
1341b 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
1341c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1341d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1341e 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 PagerRollback(Pa
1341f 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
13420 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
13421 4f 4b 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 OK;. PAGERTRACE
13422 28 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e (("ROLLBACK %d\n
13423 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
13424 72 29 29 29 3b 0a 20 20 69 66 28 20 21 70 50 61 r)));. if( !pPa
13425 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 ger->dirtyCache
13426 7c 7c 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || !pPager->jour
13427 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 72 nalOpen ){. r
13428 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 c = pager_end_tr
13429 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 ansaction(pPager
1342a 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 , pPager->setMas
1342b 74 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ter);. }else if
1342c 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
1342d 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 e && pPager->err
1342e 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c Code!=SQLITE_FUL
1342f 4c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 L ){. if( pPa
13430 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
13431 52 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 R_EXCLUSIVE ){.
13432 20 20 20 20 20 70 61 67 65 72 5f 70 6c 61 79 62 pager_playb
13433 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a ack(pPager, 0);.
13434 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70 }. rc = p
13435 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
13436 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 }else{. if(
13437 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
13438 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 PAGER_RESERVED )
13439 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b {. int rc2;
1343a 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
1343b 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 r_playback(pPage
1343c 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32 r, 0);. rc2
1343d 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 = pager_end_tra
1343e 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c nsaction(pPager,
1343f 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
13440 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 er);. if( r
13441 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
13442 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 rc = rc2
13443 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
13444 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
13445 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 pager_playback(p
13446 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d Pager, 0);. }
13447 0a 0a 20 20 20 20 69 66 28 20 21 4d 45 4d 44 42 .. if( !MEMDB
13448 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
13449 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 ->dbSizeValid =
1344a 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0;. }.. /*
1344b 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
1344c 75 72 73 20 64 75 72 69 6e 67 20 61 20 52 4f 4c urs during a ROL
1344d 4c 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f LBACK, we can no
1344e 20 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 74 68 longer trust th
1344f 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a 20 63 e pager. ** c
13450 61 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61 ache. So call pa
13451 67 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74 ger_error() on t
13452 68 65 20 77 61 79 20 6f 75 74 20 74 6f 20 6d 61 he way out to ma
13453 6b 65 20 61 6e 79 20 65 72 72 6f 72 20 0a 20 20 ke any error .
13454 20 20 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 2e ** persistent.
13455 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
13456 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 pager_error(pPa
13457 67 65 72 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 ger, rc);. }.
13458 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
13459 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
1345a 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 if the database
1345b 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 72 file is opened r
1345c 65 61 64 2d 6f 6e 6c 79 2e 20 20 52 65 74 75 72 ead-only. Retur
1345d 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68 n FALSE.** if th
1345e 65 20 64 61 74 61 62 61 73 65 20 69 73 20 28 69 e database is (i
1345f 6e 20 74 68 65 6f 72 79 29 20 77 72 69 74 61 62 n theory) writab
13460 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
13461 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 IVATE u8 sqlite3
13462 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 PagerIsreadonly(
13463 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
13464 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
13465 3e 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a >readOnly;.}../*
13466 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
13467 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e umber of referen
13468 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 72 ces to the pager
13469 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1346a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
1346b 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 67 agerRefcount(Pag
1346c 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
1346d 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61 eturn sqlite3Pca
1346e 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
1346f 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a er->pPCache);.}.
13470 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
13471 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 e number of refe
13472 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 70 rences to the sp
13473 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f ecified page..*/
13474 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13475 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
13476 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 PageRefcount(DbP
13477 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 age *pPage){. r
13478 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61 eturn sqlite3Pca
13479 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 chePageRefcount(
1347a 70 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 pPage);.}..#ifde
1347b 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a f SQLITE_TEST./*
1347c 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1347d 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 is used for tes
1347e 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 ting and analysi
1347f 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 s only..*/.SQLIT
13480 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 2a 73 E_PRIVATE int *s
13481 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 74 73 qlite3PagerStats
13482 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
13483 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 61 5b . static int a[
13484 31 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71 11];. a[0] = sq
13485 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
13486 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
13487 63 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 che);. a[1] = s
13488 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 qlite3PcachePage
13489 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 count(pPager->pP
1348a 43 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d Cache);. a[2] =
1348b 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 sqlite3PcacheGe
1348c 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 65 tCachesize(pPage
1348d 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 r->pPCache);. a
1348e 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 [3] = pPager->db
1348f 53 69 7a 65 56 61 6c 69 64 20 3f 20 28 69 6e 74 SizeValid ? (int
13490 29 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ) pPager->dbSize
13491 20 3a 20 2d 31 3b 0a 20 20 61 5b 34 5d 20 3d 20 : -1;. a[4] =
13492 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3b 0a 20 pPager->state;.
13493 20 61 5b 35 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[5] = pPager->
13494 65 72 72 43 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 errCode;. a[6]
13495 3d 20 70 50 61 67 65 72 2d 3e 6e 48 69 74 3b 0a = pPager->nHit;.
13496 20 20 61 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d a[7] = pPager-
13497 3e 6e 4d 69 73 73 3b 0a 20 20 61 5b 38 5d 20 3d >nMiss;. a[8] =
13498 20 30 3b 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 0; /* Used to
13499 62 65 20 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c be pPager->nOvfl
1349a 20 2a 2f 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61 */. a[9] = pPa
1349b 67 65 72 2d 3e 6e 52 65 61 64 3b 0a 20 20 61 5b ger->nRead;. a[
1349c 31 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57 10] = pPager->nW
1349d 72 69 74 65 3b 0a 20 20 72 65 74 75 72 6e 20 61 rite;. return a
1349e 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
1349f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
134a0 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 gerIsMemdb(Pager
134a1 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 *pPager){. ret
134a2 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d 0a 23 65 6e urn MEMDB;.}.#en
134a3 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72 dif../*.** Ensur
134a4 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65 e that there are
134a5 20 61 74 20 6c 65 61 73 74 20 6e 53 61 76 65 70 at least nSavep
134a6 6f 69 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20 oint savepoints
134a7 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f open..*/.SQLITE_
134a8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
134a9 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 te3PagerOpenSave
134aa 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 point(Pager *pPa
134ab 67 65 72 2c 20 69 6e 74 20 6e 53 61 76 65 70 6f ger, int nSavepo
134ac 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d int){. int rc =
134ad 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 SQLITE_OK;.. i
134ae 66 28 20 6e 53 61 76 65 70 6f 69 6e 74 3e 70 50 f( nSavepoint>pP
134af 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
134b0 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 4a && pPager->useJ
134b1 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69 6e ournal ){. in
134b2 74 20 69 69 3b 0a 20 20 20 20 50 61 67 65 72 53 t ii;. PagerS
134b3 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65 77 3b 0a avepoint *aNew;.
134b4 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74 . /* Either t
134b5 68 65 72 65 20 69 73 20 6e 6f 20 61 63 74 69 76 here is no activ
134b6 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 e journal or the
134b7 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 sub-journal is
134b8 6f 70 65 6e 20 6f 72 20 0a 20 20 20 20 2a 2a 20 open or . **
134b9 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 the journal is a
134ba 6c 77 61 79 73 20 73 74 6f 72 65 64 20 69 6e 20 lways stored in
134bb 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 61 73 memory */. as
134bc 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 sert( pPager->nS
134bd 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 70 avepoint==0 || p
134be 50 61 67 65 72 2d 3e 73 6a 66 64 2d 3e 70 4d 65 Pager->sjfd->pMe
134bf 74 68 6f 64 73 20 7c 7c 0a 20 20 20 20 20 20 20 thods ||.
134c0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
134c1 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
134c2 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
134c3 52 59 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 72 RY );.. /* Gr
134c4 6f 77 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 ow the Pager.aSa
134c5 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20 75 73 vepoint array us
134c6 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29 2e 20 52 ing realloc(). R
134c7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
134c8 45 4d 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 EM. ** if the
134c9 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c allocation fail
134ca 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 7a 65 s. Otherwise, ze
134cb 72 6f 20 74 68 65 20 6e 65 77 20 70 6f 72 74 69 ro the new porti
134cc 6f 6e 20 69 6e 20 63 61 73 65 20 61 20 0a 20 20 on in case a .
134cd 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c ** malloc fail
134ce 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ure occurs while
134cf 20 70 6f 70 75 6c 61 74 69 6e 67 20 69 74 20 69 populating it i
134d0 6e 20 74 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c n the for(...) l
134d1 6f 6f 70 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a oop below.. *
134d2 2f 0a 20 20 20 20 61 4e 65 77 20 3d 20 28 50 61 /. aNew = (Pa
134d3 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 29 73 gerSavepoint *)s
134d4 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 qlite3Realloc(.
134d5 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 pPager->a
134d6 53 61 76 65 70 6f 69 6e 74 2c 20 73 69 7a 65 6f Savepoint, sizeo
134d7 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 f(PagerSavepoint
134d8 29 2a 6e 53 61 76 65 70 6f 69 6e 74 0a 20 20 20 )*nSavepoint.
134d9 20 29 3b 0a 20 20 20 20 69 66 28 20 21 61 4e 65 );. if( !aNe
134da 77 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 w ){. retur
134db 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
134dc 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 }. memset
134dd 28 26 61 4e 65 77 5b 70 50 61 67 65 72 2d 3e 6e (&aNew[pPager->n
134de 53 61 76 65 70 6f 69 6e 74 5d 2c 20 30 2c 0a 20 Savepoint], 0,.
134df 20 20 20 20 20 20 20 28 6e 53 61 76 65 70 6f 69 (nSavepoi
134e0 6e 74 20 2d 20 70 50 61 67 65 72 2d 3e 6e 53 61 nt - pPager->nSa
134e1 76 65 70 6f 69 6e 74 29 20 2a 20 73 69 7a 65 6f vepoint) * sizeo
134e2 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 f(PagerSavepoint
134e3 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 70 50 61 ). );. pPa
134e4 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 ger->aSavepoint
134e5 3d 20 61 4e 65 77 3b 0a 20 20 20 20 69 69 20 3d = aNew;. ii =
134e6 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f pPager->nSavepo
134e7 69 6e 74 3b 0a 20 20 20 20 70 50 61 67 65 72 2d int;. pPager-
134e8 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e 53 >nSavepoint = nS
134e9 61 76 65 70 6f 69 6e 74 3b 0a 0a 20 20 20 20 2f avepoint;.. /
134ea 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 50 * Populate the P
134eb 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 73 74 agerSavepoint st
134ec 72 75 63 74 75 72 65 73 20 6a 75 73 74 20 61 6c ructures just al
134ed 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 20 20 20 20 located. */.
134ee 66 6f 72 28 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 3b for(/* no-op */;
134ef 20 69 69 3c 6e 53 61 76 65 70 6f 69 6e 74 3b 20 ii<nSavepoint;
134f0 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 ii++){. ass
134f1 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 ert( pPager->dbS
134f2 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 izeValid );.
134f3 20 20 61 4e 65 77 5b 69 69 5d 2e 6e 4f 72 69 67 aNew[ii].nOrig
134f4 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a = pPager->dbSiz
134f5 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 e;. if( pPa
134f6 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e ger->journalOpen
134f7 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 && pPager->jour
134f8 6e 61 6c 4f 66 66 3e 30 20 29 7b 0a 20 20 20 20 nalOff>0 ){.
134f9 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 aNew[ii].iOf
134fa 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a fset = pPager->j
134fb 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 20 ournalOff;.
134fc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
134fd 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 aNew[ii].iOffset
134fe 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 = JOURNAL_HDR_S
134ff 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 Z(pPager);.
13500 20 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69 }. aNew[ii
13501 5d 2e 69 53 75 62 52 65 63 20 3d 20 70 50 61 67 ].iSubRec = pPag
13502 65 72 2d 3e 73 74 6d 74 4e 52 65 63 3b 0a 20 20 er->stmtNRec;.
13503 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49 6e aNew[ii].pIn
13504 53 61 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c 69 Savepoint = sqli
13505 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 te3BitvecCreate(
13506 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b pPager->dbSize);
13507 0a 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 77 . if( !aNew
13508 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e [ii].pInSavepoin
13509 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 t ){. ret
1350a 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1350b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1350c 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 . /* Open the
1350d 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 66 sub-journal, if
1350e 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 it is not alrea
1350f 64 79 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 dy opened. */.
13510 20 20 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f rc = openSubJo
13511 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
13512 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
13513 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 .}../*.** Parame
13514 74 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79 73 ter op is always
13515 20 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e either SAVEPOIN
13516 54 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41 T_ROLLBACK or SA
13517 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e VEPOINT_RELEASE.
13518 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41 56 .** If it is SAV
13519 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 EPOINT_RELEASE,
1351a 74 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e 64 then release and
1351b 20 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 destroy the sav
1351c 65 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69 epoint with.** i
1351d 6e 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74 2e ndex iSavepoint.
1351e 20 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f If it is SAVEPO
1351f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 INT_ROLLBACK, th
13520 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 en rollback all
13521 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20 changes.** that
13522 68 61 76 65 20 6f 63 63 75 72 65 64 20 73 69 6e have occured sin
13523 63 65 20 73 61 76 65 70 6f 69 6e 74 20 69 53 61 ce savepoint iSa
13524 76 65 70 6f 69 6e 74 20 77 61 73 20 63 72 65 61 vepoint was crea
13525 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 ted..**.** In ei
13526 74 68 65 72 20 63 61 73 65 2c 20 61 6c 6c 20 73 ther case, all s
13527 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68 20 61 avepoints with a
13528 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 72 20 n index greater
13529 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e 74 20 than iSavepoint
1352a 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f 79 65 .** are destroye
1352b 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 d..**.** If ther
1352c 65 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 e are less than
1352d 28 69 53 61 76 65 70 6f 69 6e 74 2b 31 29 20 61 (iSavepoint+1) a
1352e 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 ctive savepoints
1352f 20 77 68 65 6e 20 74 68 69 73 20 0a 2a 2a 20 66 when this .** f
13530 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
13531 64 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e d it is a no-op.
13532 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/ .SQLITE_PRIV
13533 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
13534 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 50 61 agerSavepoint(Pa
13535 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
13536 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f op, int iSavepo
13537 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d int){. int rc =
13538 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 SQLITE_OK;.. a
13539 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 45 50 ssert( op==SAVEP
1353a 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20 OINT_RELEASE ||
1353b 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f op==SAVEPOINT_RO
1353c 4c 4c 42 41 43 4b 20 29 3b 0a 0a 20 20 69 66 28 LLBACK );.. if(
1353d 20 69 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67 iSavepoint<pPag
1353e 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 er->nSavepoint )
1353f 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 {. int ii;.
13540 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 69 53 61 int nNew = iSa
13541 76 65 70 6f 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 vepoint + (op==S
13542 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
13543 4b 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e K);. for(ii=n
13544 4e 65 77 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e New; ii<pPager->
13545 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b nSavepoint; ii++
13546 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
13547 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 BitvecDestroy(pP
13548 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 ager->aSavepoint
13549 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e [ii].pInSavepoin
1354a 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 t);. }. pP
1354b 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
1354c 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 69 66 = nNew;.. if
1354d 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f ( op==SAVEPOINT_
1354e 52 4f 4c 4c 42 41 43 4b 20 26 26 20 70 50 61 67 ROLLBACK && pPag
1354f 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f 64 er->jfd->pMethod
13550 73 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 s ){. Pager
13551 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 Savepoint *pSave
13552 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 point = (nNew==0
13553 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61 )?0:&pPager->aSa
13554 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b vepoint[nNew-1];
13555 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
13556 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 rPlaybackSavepoi
13557 6e 74 28 70 50 61 67 65 72 2c 20 70 53 61 76 65 nt(pPager, pSave
13558 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73 point);. as
13559 73 65 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f sert(rc!=SQLITE_
1355a 44 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a DONE);. }. .
1355b 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
1355c 73 20 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74 s a release of t
1355d 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 he outermost sav
1355e 65 70 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65 epoint, truncate
1355f 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 . ** the sub
13560 2d 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 -journal. */.
13561 20 69 66 28 20 6e 4e 65 77 3d 3d 30 20 26 26 20 if( nNew==0 &&
13562 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 op==SAVEPOINT_RE
13563 4c 45 41 53 45 20 26 26 20 70 50 61 67 65 72 2d LEASE && pPager-
13564 3e 73 6a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 >sjfd->pMethods
13565 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
13566 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13567 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
13568 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 ite3OsTruncate(p
13569 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30 29 3b Pager->sjfd, 0);
1356a 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 . pPager->s
1356b 74 6d 74 4e 52 65 63 20 3d 20 30 3b 0a 20 20 20 tmtNRec = 0;.
1356c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1356d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
1356e 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 urn the full pat
1356f 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 hname of the dat
13570 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 53 abase file..*/.S
13571 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
13572 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
13573 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 3PagerFilename(P
13574 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
13575 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
13576 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a zFilename;.}../*
13577 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 56 .** Return the V
13578 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 FS structure for
13579 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 the pager..*/.S
1357a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
1357b 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 20 nst sqlite3_vfs
1357c 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 73 *sqlite3PagerVfs
1357d 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
1357e 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 . return pPager
1357f 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ->pVfs;.}../*.**
13580 20 52 65 74 75 72 6e 20 74 68 65 20 66 69 6c 65 Return the file
13581 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 handle for the
13582 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 73 database file as
13583 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 sociated.** with
13584 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 69 the pager. Thi
13585 73 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 4e s might return N
13586 55 4c 4c 20 69 66 20 74 68 65 20 66 69 6c 65 20 ULL if the file
13587 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 62 has.** not yet b
13588 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a 53 een opened..*/.S
13589 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
1358a 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 lite3_file *sqli
1358b 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 te3PagerFile(Pag
1358c 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
1358d 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 66 64 eturn pPager->fd
1358e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1358f 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 n the directory
13590 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
13591 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f file..*/.SQLITE_
13592 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
13593 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 ar *sqlite3Pager
13594 44 69 72 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 Dirname(Pager *p
13595 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
13596 20 70 50 61 67 65 72 2d 3e 7a 44 69 72 65 63 74 pPager->zDirect
13597 6f 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ory;.}../*.** Re
13598 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 turn the full pa
13599 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f thname of the jo
1359a 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 urnal file..*/.S
1359b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
1359c 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1359d 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 3PagerJournalnam
1359e 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 e(Pager *pPager)
1359f 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
135a0 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a r->zJournal;.}..
135a1 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
135a2 65 20 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c e if fsync() cal
135a3 6c 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 ls are disabled
135a4 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 for this pager.
135a5 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a Return FALSE.**
135a6 20 69 66 20 66 73 79 6e 63 28 29 73 20 61 72 65 if fsync()s are
135a7 20 65 78 65 63 75 74 65 64 20 6e 6f 72 6d 61 6c executed normal
135a8 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly..*/.SQLITE_PR
135a9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
135aa 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 3PagerNosync(Pag
135ab 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
135ac 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 6f eturn pPager->no
135ad 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 Sync;.}..#ifdef
135ae 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
135af 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 ./*.** Set the c
135b0 6f 64 65 63 20 66 6f 72 20 74 68 69 73 20 70 61 odec for this pa
135b1 67 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ger.*/.SQLITE_PR
135b2 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
135b3 65 33 50 61 67 65 72 53 65 74 43 6f 64 65 63 28 e3PagerSetCodec(
135b4 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
135b5 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64 ,. void *(*xCod
135b6 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c ec)(void*,void*,
135b7 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 Pgno,int),. voi
135b8 64 20 2a 70 43 6f 64 65 63 41 72 67 0a 29 7b 0a d *pCodecArg.){.
135b9 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 pPager->xCodec
135ba 20 3d 20 78 43 6f 64 65 63 3b 0a 20 20 70 50 61 = xCodec;. pPa
135bb 67 65 72 2d 3e 70 43 6f 64 65 63 41 72 67 20 3d ger->pCodecArg =
135bc 20 70 43 6f 64 65 63 41 72 67 3b 0a 7d 0a 23 65 pCodecArg;.}.#e
135bd 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
135be 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
135bf 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 CUUM./*.** Move
135c0 74 68 65 20 70 61 67 65 20 70 50 67 20 74 6f 20 the page pPg to
135c1 6c 6f 63 61 74 69 6f 6e 20 70 67 6e 6f 20 69 6e location pgno in
135c2 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a the file..**.**
135c3 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e There must be n
135c4 6f 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 o references to
135c5 74 68 65 20 70 61 67 65 20 70 72 65 76 69 6f 75 the page previou
135c6 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a sly located at.*
135c7 2a 20 70 67 6e 6f 20 28 77 68 69 63 68 20 77 65 * pgno (which we
135c8 20 63 61 6c 6c 20 70 50 67 4f 6c 64 29 20 74 68 call pPgOld) th
135c9 6f 75 67 68 20 74 68 61 74 20 70 61 67 65 20 69 ough that page i
135ca 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 0a s allowed to be.
135cb 2a 2a 20 69 6e 20 63 61 63 68 65 2e 20 20 49 66 ** in cache. If
135cc 20 74 68 65 20 70 61 67 65 20 70 72 65 76 69 6f the page previo
135cd 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 20 usly located at
135ce 70 67 6e 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65 pgno is not alre
135cf 61 64 79 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f ady.** in the ro
135d0 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 llback journal,
135d1 69 74 20 69 73 20 6e 6f 74 20 70 75 74 20 74 68 it is not put th
135d2 65 72 65 20 62 79 20 62 79 20 74 68 69 73 20 72 ere by by this r
135d3 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 outine..**.** Re
135d4 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 ferences to the
135d5 70 61 67 65 20 70 50 67 20 72 65 6d 61 69 6e 20 page pPg remain
135d6 76 61 6c 69 64 2e 20 55 70 64 61 74 69 6e 67 20 valid. Updating
135d7 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d 64 61 74 61 any.** meta-data
135d8 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
135d9 20 70 50 67 20 28 69 2e 65 2e 20 64 61 74 61 20 pPg (i.e. data
135da 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 45 stored in the nE
135db 78 74 72 61 20 62 79 74 65 73 0a 2a 2a 20 61 6c xtra bytes.** al
135dc 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 located along wi
135dd 74 68 20 74 68 65 20 70 61 67 65 29 20 69 73 20 th the page) is
135de 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 the responsibili
135df 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 ty of the caller
135e0 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 ..**.** A transa
135e1 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 63 ction must be ac
135e2 74 69 76 65 20 77 68 65 6e 20 74 68 69 73 20 72 tive when this r
135e3 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
135e4 2e 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 0a . It used to be.
135e5 2a 2a 20 72 65 71 75 69 72 65 64 20 74 68 61 74 ** required that
135e6 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 a statement tra
135e7 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 nsaction was not
135e8 20 61 63 74 69 76 65 2c 20 62 75 74 20 74 68 69 active, but thi
135e9 73 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a s restriction.**
135ea 20 68 61 73 20 62 65 65 6e 20 72 65 6d 6f 76 65 has been remove
135eb 64 20 28 43 52 45 41 54 45 20 49 4e 44 45 58 20 d (CREATE INDEX
135ec 6e 65 65 64 73 20 74 6f 20 6d 6f 76 65 20 61 20 needs to move a
135ed 70 61 67 65 20 77 68 65 6e 20 61 20 73 74 61 74 page when a stat
135ee 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 ement.** transac
135ef 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 29 2e tion is active).
135f0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f .**.** If the fo
135f1 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 urth argument, i
135f2 73 43 6f 6d 6d 69 74 2c 20 69 73 20 6e 6f 6e 2d sCommit, is non-
135f3 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 zero, then this
135f4 70 61 67 65 20 69 73 20 62 65 69 6e 67 0a 2a 2a page is being.**
135f5 20 6d 6f 76 65 64 20 61 73 20 70 61 72 74 20 6f moved as part o
135f6 66 20 61 20 64 61 74 61 62 61 73 65 20 72 65 6f f a database reo
135f7 72 67 61 6e 69 7a 61 74 69 6f 6e 20 6a 75 73 74 rganization just
135f8 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e before the tran
135f9 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 62 saction .** is b
135fa 65 69 6e 67 20 63 6f 6d 6d 69 74 74 65 64 2e 20 eing committed.
135fb 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 69 74 In this case, it
135fc 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
135fd 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
135fe 20 70 61 67 65 20 0a 2a 2a 20 70 50 67 20 72 65 page .** pPg re
135ff 66 65 72 73 20 74 6f 20 77 69 6c 6c 20 6e 6f 74 fers to will not
13600 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 61 be written to a
13601 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 gain within this
13602 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f transaction..*/
13603 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13604 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
13605 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 20 2a Movepage(Pager *
13606 70 50 61 67 65 72 2c 20 44 62 50 61 67 65 20 2a pPager, DbPage *
13607 70 50 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 pPg, Pgno pgno,
13608 69 6e 74 20 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 int isCommit){.
13609 20 50 67 48 64 72 20 2a 70 50 67 4f 6c 64 3b 20 PgHdr *pPgOld;
1360a 20 2f 2a 20 54 68 65 20 70 61 67 65 20 62 65 69 /* The page bei
1360b 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 ng overwritten.
1360c 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 65 64 53 79 */. Pgno needSy
1360d 6e 63 50 67 6e 6f 20 3d 20 30 3b 0a 20 20 69 6e ncPgno = 0;. in
1360e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
1360f 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a pPg->nRef>0 );.
13610 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 . /* If the pag
13611 65 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 69 73 e being moved is
13612 20 64 69 72 74 79 20 61 6e 64 20 68 61 73 20 6e dirty and has n
13613 6f 74 20 62 65 65 6e 20 73 61 76 65 64 20 62 79 ot been saved by
13614 20 74 68 65 20 6c 61 74 65 73 74 0a 20 20 2a 2a the latest. **
13615 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e savepoint, then
13616 20 73 61 76 65 20 74 68 65 20 63 75 72 72 65 6e save the curren
13617 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 t contents of th
13618 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 e page into the
13619 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 . ** sub-journa
1361a 6c 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20 72 l now. This is r
1361b 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c equired to handl
1361c 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 e the following
1361d 73 63 65 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a 20 scenario:. **.
1361e 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a ** BEGIN;. *
1361f 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 * <journal p
13620 61 67 65 20 58 2c 20 74 68 65 6e 20 6d 6f 64 69 age X, then modi
13621 66 79 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 3e fy it in memory>
13622 0a 20 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f . ** SAVEPO
13623 49 4e 54 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 20 INT one;. **
13624 20 20 20 20 3c 4d 6f 76 65 20 70 61 67 65 20 58 <Move page X
13625 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 59 3e 0a to location Y>.
13626 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 ** ROLLBAC
13627 4b 20 54 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 K TO one;. **.
13628 20 2a 2a 20 49 66 20 70 61 67 65 20 58 20 77 65 ** If page X we
13629 72 65 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 re not written t
1362a 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 o the sub-journa
1362b 6c 20 68 65 72 65 2c 20 69 74 20 77 6f 75 6c 64 l here, it would
1362c 20 6e 6f 74 0a 20 20 2a 2a 20 62 65 20 70 6f 73 not. ** be pos
1362d 73 69 62 6c 65 20 74 6f 20 72 65 73 74 6f 72 65 sible to restore
1362e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 77 68 its contents wh
1362f 65 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b en the "ROLLBACK
13630 20 54 4f 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 74 TO one". ** st
13631 61 74 65 6d 65 6e 74 20 77 65 72 65 20 70 72 6f atement were pro
13632 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 cessed.. */. i
13633 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 f( pPg->flags&PG
13634 48 44 52 5f 44 49 52 54 59 20 0a 20 20 20 26 26 HDR_DIRTY . &&
13635 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
13636 65 28 70 50 67 29 0a 20 20 20 26 26 20 53 51 4c e(pPg). && SQL
13637 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 75 ITE_OK!=(rc = su
13638 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 bjournalPage(pPg
13639 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 )). ){. retu
1363a 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 50 41 rn rc;. }.. PA
1363b 47 45 52 54 52 41 43 45 28 28 22 4d 4f 56 45 20 GERTRACE(("MOVE
1363c 25 64 20 70 61 67 65 20 25 64 20 28 6e 65 65 64 %d page %d (need
1363d 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20 74 Sync=%d) moves t
1363e 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 o %d\n", .
1363f 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c PAGERID(pPager),
13640 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 50 67 pPg->pgno, (pPg
13641 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
13642 45 44 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 70 67 ED_SYNC)?1:0, pg
13643 6e 6f 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 no));. IOTRACE(
13644 28 22 4d 4f 56 45 20 25 70 20 25 64 20 25 64 5c ("MOVE %p %d %d\
13645 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d n", pPager, pPg-
13646 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20 >pgno, pgno))..
13647 20 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e 74 65 pager_get_conte
13648 6e 74 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 49 nt(pPg);.. /* I
13649 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 f the journal ne
1364a 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 eds to be sync()
1364b 65 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 ed before page p
1364c 50 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a Pg->pgno can. *
1364d 2a 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c * be written to,
1364e 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f store pPg->pgno
1364f 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 in local variab
13650 6c 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e le needSyncPgno.
13651 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 . **. ** If th
13652 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 e isCommit flag
13653 69 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 is set, there is
13654 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 no need to reme
13655 6d 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 mber that. ** t
13656 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 he journal needs
13657 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 to be sync()ed
13658 62 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 before database
13659 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a page pPg->pgno .
1365a 20 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 ** can be writ
1365b 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c ten to. The call
1365c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 er has already p
1365d 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 romised not to w
1365e 72 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f rite to it.. */
1365f 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 . if( (pPg->fla
13660 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
13661 4e 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 NC) && !isCommit
13662 20 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 ){. needSync
13663 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f Pgno = pPg->pgno
13664 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 ;. assert( pa
13665 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 geInJournal(pPg)
13666 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 || pPg->pgno>pP
13667 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
13668 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
13669 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
1366a 5f 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73 _DIRTY );. as
1366b 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 sert( pPager->ne
1366c 65 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 edSync );. }..
1366d 20 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 /* If the cache
1366e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 contains a page
1366f 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 with page-numbe
13670 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 r pgno, remove i
13671 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 t. ** from its
13672 68 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f hash chain. Also
13673 2c 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e , if the PgHdr.n
13674 65 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20 eedSync was set
13675 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 for . ** page p
13676 67 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27 gno before the '
13677 6d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c move' operation,
13678 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 it needs to be
13679 72 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 retained . ** f
1367a 6f 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65 or the page move
1367b 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 d there.. */.
1367c 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 pPg->flags &= ~P
1367d 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a GHDR_NEED_SYNC;.
1367e 20 20 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 pPgOld = pager
1367f 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 _lookup(pPager,
13680 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 pgno);. assert(
13681 20 21 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f !pPgOld || pPgO
13682 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 ld->nRef==1 );.
13683 20 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 if( pPgOld ){.
13684 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d pPg->flags |=
13685 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26 (pPgOld->flags&
13686 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
13687 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 ;. }.. sqlite3
13688 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 2c 20 PcacheMove(pPg,
13689 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 50 67 pgno);. if( pPg
1368a 4f 6c 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Old ){. sqlit
1368b 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 e3PcacheDrop(pPg
1368c 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c Old);. }.. sql
1368d 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 ite3PcacheMakeDi
1368e 72 74 79 28 70 50 67 29 3b 0a 20 20 70 50 61 67 rty(pPg);. pPag
1368f 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 3d er->dirtyCache =
13690 20 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 1;. pPager->db
13691 4d 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 0a 20 Modified = 1;..
13692 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e if( needSyncPgn
13693 6f 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e o ){. /* If n
13694 65 65 64 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e eedSyncPgno is n
13695 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 on-zero, then th
13696 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e e journal file n
13697 65 65 64 73 20 74 6f 20 62 65 20 0a 20 20 20 20 eeds to be .
13698 2a 2a 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f ** sync()ed befo
13699 72 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 77 re any data is w
1369a 72 69 74 74 65 6e 20 74 6f 20 64 61 74 61 62 61 ritten to databa
1369b 73 65 20 66 69 6c 65 20 70 61 67 65 20 6e 65 65 se file page nee
1369c 64 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a dSyncPgno.. *
1369d 2a 20 43 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 * Currently, no
1369e 73 75 63 68 20 70 61 67 65 20 65 78 69 73 74 73 such page exists
1369f 20 69 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 in the page-cac
136a0 68 65 20 61 6e 64 20 74 68 65 20 0a 20 20 20 20 he and the .
136a1 2a 2a 20 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64 ** "is journaled
136a2 22 20 62 69 74 76 65 63 20 66 6c 61 67 20 68 61 " bitvec flag ha
136a3 73 20 62 65 65 6e 20 73 65 74 2e 20 54 68 69 73 s been set. This
136a4 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d needs to be rem
136a5 65 64 69 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 edied by. **
136a6 6c 6f 61 64 69 6e 67 20 74 68 65 20 70 61 67 65 loading the page
136a7 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65 72 2d into the pager-
136a8 63 61 63 68 65 20 61 6e 64 20 73 65 74 74 69 6e cache and settin
136a9 67 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 g the PgHdr.need
136aa 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 66 6c 61 Sync . ** fla
136ab 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a g.. **. **
136ac 20 49 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 If the attempt
136ad 74 6f 20 6c 6f 61 64 20 74 68 65 20 70 61 67 65 to load the page
136ae 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65 2d 63 into the page-c
136af 61 63 68 65 20 66 61 69 6c 73 2c 20 28 64 75 65 ache fails, (due
136b0 0a 20 20 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c . ** to a mal
136b1 6c 6f 63 28 29 20 6f 72 20 49 4f 20 66 61 69 6c loc() or IO fail
136b2 75 72 65 29 2c 20 63 6c 65 61 72 20 74 68 65 20 ure), clear the
136b3 62 69 74 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f bit in the pInJo
136b4 75 72 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 urnal[]. ** a
136b5 72 72 61 79 2e 20 4f 74 68 65 72 77 69 73 65 2c rray. Otherwise,
136b6 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
136b7 6c 6f 61 64 65 64 20 61 6e 64 20 77 72 69 74 74 loaded and writt
136b8 65 6e 20 61 67 61 69 6e 20 69 6e 0a 20 20 20 20 en again in.
136b9 2a 2a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 ** this transact
136ba 69 6f 6e 2c 20 69 74 20 6d 61 79 20 62 65 20 77 ion, it may be w
136bb 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 ritten to the da
136bc 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f tabase file befo
136bd 72 65 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 re. ** it is
136be 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 synced into the
136bf 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 journal file. Th
136c0 69 73 20 77 61 79 2c 20 69 74 20 6d 61 79 20 65 is way, it may e
136c1 6e 64 20 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20 nd up in. **
136c2 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
136c3 20 74 77 69 63 65 2c 20 62 75 74 20 74 68 61 74 twice, but that
136c4 20 69 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 is not a proble
136c5 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a m.. **. **
136c6 20 54 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 The sqlite3Page
136c7 72 47 65 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 rGet() call may
136c8 63 61 75 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 cause the journa
136c9 6c 20 74 6f 20 73 79 6e 63 2e 20 53 6f 20 6d 61 l to sync. So ma
136ca 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 74 ke. ** sure t
136cb 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e he Pager.needSyn
136cc 63 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f c flag is set to
136cd 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 50 67 o.. */. Pg
136ce 48 64 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 20 Hdr *pPgHdr;.
136cf 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
136d0 3e 6e 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 20 >needSync );.
136d1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
136d2 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 6e 65 erGet(pPager, ne
136d3 65 64 53 79 6e 63 50 67 6e 6f 2c 20 26 70 50 67 edSyncPgno, &pPg
136d4 48 64 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 Hdr);. if( rc
136d5 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
136d6 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d if( pPager-
136d7 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 26 26 20 6e >pInJournal && n
136d8 65 65 64 53 79 6e 63 50 67 6e 6f 3c 3d 70 50 61 eedSyncPgno<=pPa
136d9 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
136da 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
136db 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 70 50 e3BitvecClear(pP
136dc 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
136dd 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 29 3b , needSyncPgno);
136de 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
136df 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
136e0 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 pPager->need
136e1 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 61 73 Sync = 1;. as
136e2 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 6f sert( pPager->no
136e3 53 79 6e 63 3d 3d 30 20 26 26 20 21 4d 45 4d 44 Sync==0 && !MEMD
136e4 42 20 29 3b 0a 20 20 20 20 70 50 67 48 64 72 2d B );. pPgHdr-
136e5 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f >flags |= PGHDR_
136e6 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73 NEED_SYNC;. s
136e7 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
136e8 44 69 72 74 79 28 70 50 67 48 64 72 29 3b 0a 20 Dirty(pPgHdr);.
136e9 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
136ea 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 20 nref(pPgHdr);.
136eb 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }.. return SQLI
136ec 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a TE_OK;.}.#endif.
136ed 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
136ee 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 64 pointer to the d
136ef 61 74 61 20 66 6f 72 20 74 68 65 20 73 70 65 63 ata for the spec
136f0 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 ified page..*/.S
136f1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
136f2 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
136f3 47 65 74 44 61 74 61 28 44 62 50 61 67 65 20 2a GetData(DbPage *
136f4 70 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28 20 pPg){. assert(
136f5 70 50 67 2d 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 pPg->nRef>0 || p
136f6 50 67 2d 3e 70 50 61 67 65 72 2d 3e 6d 65 6d 44 Pg->pPager->memD
136f7 62 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 b );. return pP
136f8 67 2d 3e 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a g->pData;.}../*.
136f9 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
136fa 74 65 72 20 74 6f 20 74 68 65 20 50 61 67 65 72 ter to the Pager
136fb 2e 6e 45 78 74 72 61 20 62 79 74 65 73 20 6f 66 .nExtra bytes of
136fc 20 22 65 78 74 72 61 22 20 73 70 61 63 65 20 0a "extra" space .
136fd 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f ** allocated alo
136fe 6e 67 20 77 69 74 68 20 74 68 65 20 73 70 65 63 ng with the spec
136ff 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 ified page..*/.S
13700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
13701 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
13702 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20 GetExtra(DbPage
13703 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a *pPg){. Pager *
13704 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 pPager = pPg->pP
13705 61 67 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 28 ager;. return (
13706 70 50 61 67 65 72 3f 70 50 67 2d 3e 70 45 78 74 pPager?pPg->pExt
13707 72 61 3a 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ra:0);.}../*.**
13708 47 65 74 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b Get/set the lock
13709 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 ing-mode for thi
1370a 73 20 70 61 67 65 72 2e 20 50 61 72 61 6d 65 74 s pager. Paramet
1370b 65 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 er eMode must be
1370c 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52 one.** of PAGER
1370d 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 _LOCKINGMODE_QUE
1370e 52 59 2c 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e RY, PAGER_LOCKIN
1370f 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 GMODE_NORMAL or
13710 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e .** PAGER_LOCKIN
13711 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2e GMODE_EXCLUSIVE.
13712 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 If the paramete
13713 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c r is not _QUERY,
13714 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 then.** the loc
13715 6b 69 6e 67 2d 6d 6f 64 65 20 69 73 20 73 65 74 king-mode is set
13716 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 70 to the value sp
13717 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 ecified..**.** T
13718 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 he returned valu
13719 65 20 69 73 20 65 69 74 68 65 72 20 50 41 47 45 e is either PAGE
1371a 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f R_LOCKINGMODE_NO
1371b 52 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52 RMAL or.** PAGER
1371c 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 _LOCKINGMODE_EXC
1371d 4c 55 53 49 56 45 2c 20 69 6e 64 69 63 61 74 69 LUSIVE, indicati
1371e 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 ng the current (
1371f 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 possibly updated
13720 29 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 ).** locking-mod
13721 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
13722 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
13723 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 PagerLockingMode
13724 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
13725 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 int eMode){. as
13726 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 sert( eMode==PAG
13727 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 ER_LOCKINGMODE_Q
13728 55 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 UERY.
13729 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 || eMode==PAGER
1372a 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 _LOCKINGMODE_NOR
1372b 4d 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 MAL.
1372c 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f || eMode==PAGER_
1372d 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c LOCKINGMODE_EXCL
1372e 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 USIVE );. asser
1372f 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 t( PAGER_LOCKING
13730 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a MODE_QUERY<0 );.
13731 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f assert( PAGER_
13732 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d LOCKINGMODE_NORM
13733 41 4c 3e 3d 30 20 26 26 20 50 41 47 45 52 5f 4c AL>=0 && PAGER_L
13734 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 OCKINGMODE_EXCLU
13735 53 49 56 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28 SIVE>=0 );. if(
13736 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 eMode>=0 && !pP
13737 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 ager->tempFile )
13738 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78 {. pPager->ex
13739 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 clusiveMode = (u
1373a 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 8)eMode;. }. r
1373b 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 eturn (int)pPage
1373c 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
1373d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 ;.}../*.** Get/s
1373e 65 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d et the journal-m
1373f 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 ode for this pag
13740 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d er. Parameter eM
13741 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 ode must be one
13742 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 of:.**.** PAG
13743 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 ER_JOURNALMODE_Q
13744 55 45 52 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 UERY.** PAGER
13745 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c _JOURNALMODE_DEL
13746 45 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f ETE.** PAGER_
13747 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e JOURNALMODE_TRUN
13748 43 41 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 CATE.** PAGER
13749 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 _JOURNALMODE_PER
1374a 53 49 53 54 0a 2a 2a 20 20 20 20 50 41 47 45 52 SIST.** PAGER
1374b 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 _JOURNALMODE_OFF
1374c 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
1374d 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 5f rameter is not _
1374e 51 55 45 52 59 2c 20 74 68 65 6e 20 74 68 65 20 QUERY, then the
1374f 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 69 73 20 journal-mode is
13750 73 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61 set to the.** va
13751 6c 75 65 20 73 70 65 63 69 66 69 65 64 2e 0a 2a lue specified..*
13752 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 *.** The returne
13753 64 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 63 d indicate the c
13754 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62 6c 79 urrent (possibly
13755 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6a 6f 75 updated).** jou
13756 72 6e 61 6c 2d 6d 6f 64 65 2e 0a 2a 2f 0a 53 51 rnal-mode..*/.SQ
13757 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
13758 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 sqlite3PagerJou
13759 72 6e 61 6c 4d 6f 64 65 28 50 61 67 65 72 20 2a rnalMode(Pager *
1375a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d 6f 64 pPager, int eMod
1375b 65 29 7b 0a 20 20 69 66 28 20 21 4d 45 4d 44 42 e){. if( !MEMDB
1375c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1375d 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 eMode==PAGER_JOU
1375e 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 20 RNALMODE_QUERY.
1375f 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
13760 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 eMode==PAGER_JOU
13761 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a RNALMODE_DELETE.
13762 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
13763 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f eMode==PAGER_JO
13764 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 URNALMODE_TRUNCA
13765 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 TE.
13766 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 || eMode==PAGER
13767 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 _JOURNALMODE_PER
13768 53 49 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 SIST.
13769 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
1376a 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
1376b 46 46 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 FF .
1376c 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 || eMode==PAGE
1376d 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
1376e 4d 4f 52 59 20 29 3b 0a 20 20 20 20 61 73 73 65 MORY );. asse
1376f 72 74 28 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 rt( PAGER_JOURNA
13770 4c 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b LMODE_QUERY<0 );
13771 0a 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3e 3d . if( eMode>=
13772 30 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 0 ){. pPage
13773 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d r->journalMode =
13774 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 20 20 (u8)eMode;.
13775 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
13776 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 ert( eMode==PAGE
13777 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 R_JOURNALMODE_QU
13778 45 52 59 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ERY );. }. }
13779 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 . return (int)p
1377a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
1377b 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 de;.}../*.** Get
1377c 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 /set the size-li
1377d 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70 65 72 mit used for per
1377e 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 sistent journal
1377f 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 files..*/.SQLITE
13780 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71 6c _PRIVATE i64 sql
13781 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c ite3PagerJournal
13782 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 72 20 SizeLimit(Pager
13783 2a 70 50 61 67 65 72 2c 20 69 36 34 20 69 4c 69 *pPager, i64 iLi
13784 6d 69 74 29 7b 0a 20 20 69 66 28 20 69 4c 69 6d mit){. if( iLim
13785 69 74 3e 3d 2d 31 20 29 7b 0a 20 20 20 20 70 50 it>=-1 ){. pP
13786 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a ager->journalSiz
13787 65 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69 74 3b eLimit = iLimit;
13788 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 50 . }. return pP
13789 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a ager->journalSiz
1378a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a 23 65 6e 64 69 eLimit;.}..#endi
1378b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1378c 5f 44 49 53 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a _DISKIO */../***
1378d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1378e 6f 66 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a of pager.c *****
1378f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13791 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
13792 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
13793 6e 20 66 69 6c 65 20 62 74 6d 75 74 65 78 2e 63 n file btmutex.c
13794 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
13795 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13796 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
13797 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 37 * 2007 August 27
13798 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
13799 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1379a 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1379b 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1379c 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1379d 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1379e 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1379f 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
137a0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
137a1 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
137a2 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
137a3 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
137a4 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
137a5 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
137a6 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
137a7 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
137a8 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
137a9 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
137aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
137ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
137ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
137ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
137ae 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 6d 75 74 **.** $Id: btmut
137af 65 78 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 38 ex.c,v 1.12 2008
137b0 2f 31 31 2f 31 37 20 31 39 3a 31 38 3a 35 35 20 /11/17 19:18:55
137b1 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
137b2 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c $.**.** This fil
137b3 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
137b4 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
137b5 74 20 6d 75 74 65 78 65 73 20 6f 6e 20 42 74 72 t mutexes on Btr
137b6 65 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 54 ee objects..** T
137b7 68 69 73 20 63 6f 64 65 20 72 65 61 6c 6c 79 20 his code really
137b8 62 65 6c 6f 6e 67 73 20 69 6e 20 62 74 72 65 65 belongs in btree
137b9 2e 63 2e 20 20 42 75 74 20 62 74 72 65 65 2e 63 .c. But btree.c
137ba 20 69 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 0a is getting too.
137bb 2a 2a 20 62 69 67 20 61 6e 64 20 77 65 20 77 61 ** big and we wa
137bc 6e 74 20 74 6f 20 62 72 65 61 6b 20 69 74 20 64 nt to break it d
137bd 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 68 69 73 20 own some. This
137be 70 61 63 6b 61 67 65 64 20 73 65 65 6d 65 64 20 packaged seemed
137bf 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f 6f 64 20 62 like.** a good b
137c0 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a 2f 2a 2a 2a reakout..*/./***
137c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
137c2 75 64 65 20 62 74 72 65 65 49 6e 74 2e 68 20 69 ude btreeInt.h i
137c3 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
137c4 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a btmutex.c ******
137c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
137c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
137c7 6e 20 66 69 6c 65 20 62 74 72 65 65 49 6e 74 2e n file btreeInt.
137c8 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
137c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
137ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
137cb 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a * 2004 April 6.*
137cc 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
137cd 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
137ce 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
137cf 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
137d0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
137d1 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
137d2 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
137d3 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
137d4 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
137d5 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
137d6 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
137d7 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
137d8 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
137d9 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
137da 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
137db 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
137dc 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
137dd 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
137de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
137df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
137e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
137e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
137e2 20 24 49 64 3a 20 62 74 72 65 65 49 6e 74 2e 68 $Id: btreeInt.h
137e3 2c 76 20 31 2e 33 38 20 32 30 30 38 2f 31 32 2f ,v 1.38 2008/12/
137e4 32 37 20 31 35 3a 32 33 3a 31 33 20 64 61 6e 69 27 15:23:13 dani
137e5 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a elk1977 Exp $.**
137e6 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d .** This file im
137e7 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65 72 plements a exter
137e8 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64 29 nal (disk-based)
137e9 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 database using
137ea 42 54 72 65 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 BTrees..** For a
137eb 20 64 65 74 61 69 6c 65 64 20 64 69 73 63 75 73 detailed discus
137ec 73 69 6f 6e 20 6f 66 20 42 54 72 65 65 73 2c 20 sion of BTrees,
137ed 72 65 66 65 72 20 74 6f 0a 2a 2a 0a 2a 2a 20 20 refer to.**.**
137ee 20 20 20 44 6f 6e 61 6c 64 20 45 2e 20 4b 6e 75 Donald E. Knu
137ef 74 68 2c 20 54 48 45 20 41 52 54 20 4f 46 20 43 th, THE ART OF C
137f0 4f 4d 50 55 54 45 52 20 50 52 4f 47 52 41 4d 4d OMPUTER PROGRAMM
137f1 49 4e 47 2c 20 56 6f 6c 75 6d 65 20 33 3a 0a 2a ING, Volume 3:.*
137f2 2a 20 20 20 20 20 22 53 6f 72 74 69 6e 67 20 41 * "Sorting A
137f3 6e 64 20 53 65 61 72 63 68 69 6e 67 22 2c 20 70 nd Searching", p
137f4 61 67 65 73 20 34 37 33 2d 34 38 30 2e 20 41 64 ages 473-480. Ad
137f5 64 69 73 6f 6e 2d 57 65 73 6c 65 79 0a 2a 2a 20 dison-Wesley.**
137f6 20 20 20 20 50 75 62 6c 69 73 68 69 6e 67 20 43 Publishing C
137f7 6f 6d 70 61 6e 79 2c 20 52 65 61 64 69 6e 67 2c ompany, Reading,
137f8 20 4d 61 73 73 61 63 68 75 73 65 74 74 73 2e 0a Massachusetts..
137f9 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20 **.** The basic
137fa 69 64 65 61 20 69 73 20 74 68 61 74 20 65 61 63 idea is that eac
137fb 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 h page of the fi
137fc 6c 65 20 63 6f 6e 74 61 69 6e 73 20 4e 20 64 61 le contains N da
137fd 74 61 62 61 73 65 0a 2a 2a 20 65 6e 74 72 69 65 tabase.** entrie
137fe 73 20 61 6e 64 20 4e 2b 31 20 70 6f 69 6e 74 65 s and N+1 pointe
137ff 72 73 20 74 6f 20 73 75 62 70 61 67 65 73 2e 0a rs to subpages..
13800 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d **.** --------
13801 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13802 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13803 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13804 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 7c 20 --------.** |
13805 20 50 74 72 28 30 29 20 7c 20 4b 65 79 28 30 29 Ptr(0) | Key(0)
13806 20 7c 20 50 74 72 28 31 29 20 7c 20 4b 65 79 28 | Ptr(1) | Key(
13807 31 29 20 7c 20 2e 2e 2e 20 7c 20 4b 65 79 28 4e 1) | ... | Key(N
13808 2d 31 29 20 7c 20 50 74 72 28 4e 29 20 7c 0a 2a -1) | Ptr(N) |.*
13809 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * ------------
1380a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1380b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1380c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1380d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f ----.**.** All o
1380e 66 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 74 68 f the keys on th
1380f 65 20 70 61 67 65 20 74 68 61 74 20 50 74 72 28 e page that Ptr(
13810 30 29 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 76 0) points to hav
13811 65 20 76 61 6c 75 65 73 20 6c 65 73 73 0a 2a 2a e values less.**
13812 20 74 68 61 6e 20 4b 65 79 28 30 29 2e 20 20 41 than Key(0). A
13813 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20 6f ll of the keys o
13814 6e 20 70 61 67 65 20 50 74 72 28 31 29 20 61 6e n page Ptr(1) an
13815 64 20 69 74 73 20 73 75 62 70 61 67 65 73 20 68 d its subpages h
13816 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73 20 67 72 ave.** values gr
13817 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 30 eater than Key(0
13818 29 20 61 6e 64 20 6c 65 73 73 20 74 68 61 6e 20 ) and less than
13819 4b 65 79 28 31 29 2e 20 20 41 6c 6c 20 6f 66 20 Key(1). All of
1381a 74 68 65 20 6b 65 79 73 0a 2a 2a 20 6f 6e 20 50 the keys.** on P
1381b 74 72 28 4e 29 20 61 6e 64 20 69 74 73 20 73 75 tr(N) and its su
1381c 62 70 61 67 65 73 20 68 61 76 65 20 76 61 6c 75 bpages have valu
1381d 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 es greater than
1381e 4b 65 79 28 4e 2d 31 29 2e 20 20 41 6e 64 0a 2a Key(N-1). And.*
1381f 2a 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 0a 2a * so forth..**.*
13820 2a 20 46 69 6e 64 69 6e 67 20 61 20 70 61 72 74 * Finding a part
13821 69 63 75 6c 61 72 20 6b 65 79 20 72 65 71 75 69 icular key requi
13822 72 65 73 20 72 65 61 64 69 6e 67 20 4f 28 6c 6f res reading O(lo
13823 67 28 4d 29 29 20 70 61 67 65 73 20 66 72 6f 6d g(M)) pages from
13824 20 74 68 65 20 0a 2a 2a 20 64 69 73 6b 20 77 68 the .** disk wh
13825 65 72 65 20 4d 20 69 73 20 74 68 65 20 6e 75 6d ere M is the num
13826 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
13827 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a n the tree..**.*
13828 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c 65 6d * In this implem
13829 65 6e 74 61 74 69 6f 6e 2c 20 61 20 73 69 6e 67 entation, a sing
1382a 6c 65 20 66 69 6c 65 20 63 61 6e 20 68 6f 6c 64 le file can hold
1382b 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 65 70 one or more sep
1382c 61 72 61 74 65 20 0a 2a 2a 20 42 54 72 65 65 73 arate .** BTrees
1382d 2e 20 20 45 61 63 68 20 42 54 72 65 65 20 69 73 . Each BTree is
1382e 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 identified by t
1382f 68 65 20 69 6e 64 65 78 20 6f 66 20 69 74 73 20 he index of its
13830 72 6f 6f 74 20 70 61 67 65 2e 20 20 54 68 65 0a root page. The.
13831 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 ** key and data
13832 66 6f 72 20 61 6e 79 20 65 6e 74 72 79 20 61 72 for any entry ar
13833 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66 6f e combined to fo
13834 72 6d 20 74 68 65 20 22 70 61 79 6c 6f 61 64 22 rm the "payload"
13835 2e 20 20 41 0a 2a 2a 20 66 69 78 65 64 20 61 6d . A.** fixed am
13836 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 ount of payload
13837 63 61 6e 20 62 65 20 63 61 72 72 69 65 64 20 64 can be carried d
13838 69 72 65 63 74 6c 79 20 6f 6e 20 74 68 65 20 64 irectly on the d
13839 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67 65 2e atabase.** page.
1383a 20 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 If the payload
1383b 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
1383c 74 68 65 20 70 72 65 73 65 74 20 61 6d 6f 75 6e the preset amoun
1383d 74 20 74 68 65 6e 20 73 75 72 70 6c 75 73 0a 2a t then surplus.*
1383e 2a 20 62 79 74 65 73 20 61 72 65 20 73 74 6f 72 * bytes are stor
1383f 65 64 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 ed on overflow p
13840 61 67 65 73 2e 20 20 54 68 65 20 70 61 79 6c 6f ages. The paylo
13841 61 64 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 0a ad for an entry.
13842 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 65 63 65 ** and the prece
13843 64 69 6e 67 20 70 6f 69 6e 74 65 72 20 61 72 65 ding pointer are
13844 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66 6f 72 combined to for
13845 6d 20 61 20 22 43 65 6c 6c 22 2e 20 20 45 61 63 m a "Cell". Eac
13846 68 20 0a 2a 2a 20 70 61 67 65 20 68 61 73 20 61 h .** page has a
13847 20 73 6d 61 6c 6c 20 68 65 61 64 65 72 20 77 68 small header wh
13848 69 63 68 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ich contains the
13849 20 50 74 72 28 4e 29 20 70 6f 69 6e 74 65 72 20 Ptr(N) pointer
1384a 61 6e 64 20 6f 74 68 65 72 0a 2a 2a 20 69 6e 66 and other.** inf
1384b 6f 72 6d 61 74 69 6f 6e 20 73 75 63 68 20 61 73 ormation such as
1384c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 6b 65 79 the size of key
1384d 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a and data..**.**
1384e 20 46 4f 52 4d 41 54 20 44 45 54 41 49 4c 53 0a FORMAT DETAILS.
1384f 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 69 **.** The file i
13850 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 70 s divided into p
13851 61 67 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 ages. The first
13852 20 70 61 67 65 20 69 73 20 63 61 6c 6c 65 64 20 page is called
13853 70 61 67 65 20 31 2c 0a 2a 2a 20 74 68 65 20 73 page 1,.** the s
13854 65 63 6f 6e 64 20 69 73 20 70 61 67 65 20 32 2c econd is page 2,
13855 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 and so forth.
13856 41 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 A page number of
13857 20 7a 65 72 6f 20 69 6e 64 69 63 61 74 65 73 0a zero indicates.
13858 2a 2a 20 22 6e 6f 20 73 75 63 68 20 70 61 67 65 ** "no such page
13859 22 2e 20 20 54 68 65 20 70 61 67 65 20 73 69 7a ". The page siz
1385a 65 20 63 61 6e 20 62 65 20 61 6e 79 74 68 69 6e e can be anythin
1385b 67 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e g between 512 an
1385c 64 20 36 35 35 33 36 2e 0a 2a 2a 20 45 61 63 68 d 65536..** Each
1385d 20 70 61 67 65 20 63 61 6e 20 62 65 20 65 69 74 page can be eit
1385e 68 65 72 20 61 20 62 74 72 65 65 20 70 61 67 65 her a btree page
1385f 2c 20 61 20 66 72 65 65 6c 69 73 74 20 70 61 67 , a freelist pag
13860 65 20 6f 72 20 61 6e 20 6f 76 65 72 66 6c 6f 77 e or an overflow
13861 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 .** page..**.**
13862 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 The first page i
13863 73 20 61 6c 77 61 79 73 20 61 20 62 74 72 65 65 s always a btree
13864 20 70 61 67 65 2e 20 20 54 68 65 20 66 69 72 73 page. The firs
13865 74 20 31 30 30 20 62 79 74 65 73 20 6f 66 20 74 t 100 bytes of t
13866 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 67 65 he first.** page
13867 20 63 6f 6e 74 61 69 6e 20 61 20 73 70 65 63 69 contain a speci
13868 61 6c 20 68 65 61 64 65 72 20 28 74 68 65 20 22 al header (the "
13869 66 69 6c 65 20 68 65 61 64 65 72 22 29 20 74 68 file header") th
1386a 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 at describes the
1386b 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f file..** The fo
1386c 72 6d 61 74 20 6f 66 20 74 68 65 20 66 69 6c 65 rmat of the file
1386d 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f header is as fo
1386e 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f llows:.**.** O
1386f 46 46 53 45 54 20 20 20 53 49 5a 45 20 20 20 20 FFSET SIZE
13870 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 DESCRIPTION.**
13871 20 20 20 20 30 20 20 20 20 20 20 31 36 20 20 20 0 16
13872 20 20 48 65 61 64 65 72 20 73 74 72 69 6e 67 3a Header string:
13873 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 "SQLite format
13874 33 5c 30 30 30 22 0a 2a 2a 20 20 20 20 20 31 36 3\000".** 16
13875 20 20 20 20 20 20 20 32 20 20 20 20 20 50 61 67 2 Pag
13876 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e e size in bytes.
13877 20 20 0a 2a 2a 20 20 20 20 20 31 38 20 20 20 20 .** 18
13878 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 1 File fo
13879 72 6d 61 74 20 77 72 69 74 65 20 76 65 72 73 69 rmat write versi
1387a 6f 6e 0a 2a 2a 20 20 20 20 20 31 39 20 20 20 20 on.** 19
1387b 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 1 File fo
1387c 72 6d 61 74 20 72 65 61 64 20 76 65 72 73 69 6f rmat read versio
1387d 6e 0a 2a 2a 20 20 20 20 20 32 30 20 20 20 20 20 n.** 20
1387e 20 20 31 20 20 20 20 20 42 79 74 65 73 20 6f 66 1 Bytes of
1387f 20 75 6e 75 73 65 64 20 73 70 61 63 65 20 61 74 unused space at
13880 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 the end of each
13881 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 32 31 20 page.** 21
13882 20 20 20 20 20 20 31 20 20 20 20 20 4d 61 78 20 1 Max
13883 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 embedded payload
13884 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 fraction.**
13885 20 32 32 20 20 20 20 20 20 20 31 20 20 20 20 20 22 1
13886 4d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 Min embedded pay
13887 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a load fraction.**
13888 20 20 20 20 20 32 33 20 20 20 20 20 20 20 31 20 23 1
13889 20 20 20 20 4d 69 6e 20 6c 65 61 66 20 70 61 79 Min leaf pay
1388a 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a load fraction.**
1388b 20 20 20 20 20 32 34 20 20 20 20 20 20 20 34 20 24 4
1388c 20 20 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20 File change
1388d 63 6f 75 6e 74 65 72 0a 2a 2a 20 20 20 20 20 32 counter.** 2
1388e 38 20 20 20 20 20 20 20 34 20 20 20 20 20 52 65 8 4 Re
1388f 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 served for futur
13890 65 20 75 73 65 0a 2a 2a 20 20 20 20 20 33 32 20 e use.** 32
13891 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73 4 Firs
13892 74 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 0a t freelist page.
13893 2a 2a 20 20 20 20 20 33 36 20 20 20 20 20 20 20 ** 36
13894 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 4 Number of
13895 66 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 69 freelist pages i
13896 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20 n the file.**
13897 20 20 34 30 20 20 20 20 20 20 36 30 20 20 20 20 40 60
13898 20 31 35 20 34 2d 62 79 74 65 20 6d 65 74 61 20 15 4-byte meta
13899 76 61 6c 75 65 73 20 70 61 73 73 65 64 20 74 6f values passed to
1389a 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 0a 2a higher layers.*
1389b 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 *.** All of the
1389c 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 61 integer values a
1389d 72 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 28 6d re big-endian (m
1389e 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 ost significant
1389f 62 79 74 65 20 66 69 72 73 74 29 2e 0a 2a 2a 0a byte first)..**.
138a0 2a 2a 20 54 68 65 20 66 69 6c 65 20 63 68 61 6e ** The file chan
138a1 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 69 6e ge counter is in
138a2 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 20 74 cremented when t
138a3 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 he database is c
138a4 68 61 6e 67 65 64 0a 2a 2a 20 54 68 69 73 20 63 hanged.** This c
138a5 6f 75 6e 74 65 72 20 61 6c 6c 6f 77 73 20 6f 74 ounter allows ot
138a6 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74 6f her processes to
138a7 20 6b 6e 6f 77 20 77 68 65 6e 20 74 68 65 20 66 know when the f
138a8 69 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a ile has changed.
138a9 2a 2a 20 61 6e 64 20 74 68 75 73 20 77 68 65 6e ** and thus when
138aa 20 74 68 65 79 20 6e 65 65 64 20 74 6f 20 66 6c they need to fl
138ab 75 73 68 20 74 68 65 69 72 20 63 61 63 68 65 2e ush their cache.
138ac 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 20 65 .**.** The max e
138ad 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 mbedded payload
138ae 66 72 61 63 74 69 6f 6e 20 69 73 20 74 68 65 20 fraction is the
138af 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 74 6f amount of the to
138b0 74 61 6c 20 75 73 61 62 6c 65 0a 2a 2a 20 73 70 tal usable.** sp
138b1 61 63 65 20 69 6e 20 61 20 70 61 67 65 20 74 68 ace in a page th
138b2 61 74 20 63 61 6e 20 62 65 20 63 6f 6e 73 75 6d at can be consum
138b3 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 63 ed by a single c
138b4 65 6c 6c 20 66 6f 72 20 73 74 61 6e 64 61 72 64 ell for standard
138b5 0a 2a 2a 20 42 2d 74 72 65 65 20 28 6e 6f 6e 2d .** B-tree (non-
138b6 4c 45 41 46 44 41 54 41 29 20 74 61 62 6c 65 73 LEAFDATA) tables
138b7 2e 20 20 41 20 76 61 6c 75 65 20 6f 66 20 32 35 . A value of 25
138b8 35 20 6d 65 61 6e 73 20 31 30 30 25 2e 20 20 54 5 means 100%. T
138b9 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69 73 he default.** is
138ba 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 to limit the ma
138bb 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 ximum cell size
138bc 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 so that at least
138bd 20 34 20 63 65 6c 6c 73 20 77 69 6c 6c 20 66 69 4 cells will fi
138be 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 70 61 67 65 t.** on one page
138bf 2e 20 20 54 68 75 73 20 74 68 65 20 64 65 66 61 . Thus the defa
138c0 75 6c 74 20 6d 61 78 20 65 6d 62 65 64 64 65 64 ult max embedded
138c1 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
138c2 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a 2a 2a 20 49 n is 64..**.** I
138c3 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 66 6f f the payload fo
138c4 72 20 61 20 63 65 6c 6c 20 69 73 20 6c 61 72 67 r a cell is larg
138c5 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 20 er than the max
138c6 70 61 79 6c 6f 61 64 2c 20 74 68 65 6e 20 65 78 payload, then ex
138c7 74 72 61 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 69 tra.** payload i
138c8 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 6f 76 65 s spilled to ove
138c9 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 4f 6e rflow pages. On
138ca 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 ce an overflow p
138cb 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 age is allocated
138cc 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79 20 62 79 74 ,.** as many byt
138cd 65 73 20 61 73 20 70 6f 73 73 69 62 6c 65 20 61 es as possible a
138ce 72 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 re moved into th
138cf 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 e overflow pages
138d0 20 77 69 74 68 6f 75 74 20 6c 65 74 74 69 6e 67 without letting
138d1 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 73 69 7a .** the cell siz
138d2 65 20 64 72 6f 70 20 62 65 6c 6f 77 20 74 68 65 e drop below the
138d3 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 min embedded pa
138d4 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 2e 0a yload fraction..
138d5 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 20 6c 65 **.** The min le
138d6 61 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 af payload fract
138d7 69 6f 6e 20 69 73 20 6c 69 6b 65 20 74 68 65 20 ion is like the
138d8 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 min embedded pay
138d9 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a load fraction.**
138da 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 except that it
138db 61 70 70 6c 69 65 73 20 74 6f 20 6c 65 61 66 20 applies to leaf
138dc 6e 6f 64 65 73 20 69 6e 20 61 20 4c 45 41 46 44 nodes in a LEAFD
138dd 41 54 41 20 74 72 65 65 2e 20 20 54 68 65 20 6d ATA tree. The m
138de 61 78 69 6d 75 6d 0a 2a 2a 20 70 61 79 6c 6f 61 aximum.** payloa
138df 64 20 66 72 61 63 74 69 6f 6e 20 66 6f 72 20 61 d fraction for a
138e0 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20 69 LEAFDATA tree i
138e1 73 20 61 6c 77 61 79 73 20 31 30 30 25 20 28 6f s always 100% (o
138e2 72 20 32 35 35 29 20 61 6e 64 20 69 74 0a 2a 2a r 255) and it.**
138e3 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 69 not specified i
138e4 6e 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a n the header..**
138e5 0a 2a 2a 20 45 61 63 68 20 62 74 72 65 65 20 70 .** Each btree p
138e6 61 67 65 73 20 69 73 20 64 69 76 69 64 65 64 20 ages is divided
138e7 69 6e 74 6f 20 74 68 72 65 65 20 73 65 63 74 69 into three secti
138e8 6f 6e 73 3a 20 20 54 68 65 20 68 65 61 64 65 72 ons: The header
138e9 2c 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 20 70 6f , the.** cell po
138ea 69 6e 74 65 72 20 61 72 72 61 79 2c 20 61 6e 64 inter array, and
138eb 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
138ec 74 20 61 72 65 61 2e 20 20 50 61 67 65 20 31 20 t area. Page 1
138ed 61 6c 73 6f 20 68 61 73 20 61 20 31 30 30 2d 62 also has a 100-b
138ee 79 74 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 yte.** file head
138ef 65 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 62 er that occurs b
138f0 65 66 6f 72 65 20 74 68 65 20 70 61 67 65 20 68 efore the page h
138f1 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 eader..**.**
138f2 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-------------
138f3 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 66 ---|.** | f
138f4 69 6c 65 20 68 65 61 64 65 72 20 20 20 20 7c 20 ile header |
138f5 20 20 31 30 30 20 62 79 74 65 73 2e 20 20 50 61 100 bytes. Pa
138f6 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a 2a 20 20 20 ge 1 only..**
138f7 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |------------
138f8 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 ----|.** |
138f9 70 61 67 65 20 68 65 61 64 65 72 20 20 20 20 7c page header |
138fa 20 20 20 38 20 62 79 74 65 73 20 66 6f 72 20 6c 8 bytes for l
138fb 65 61 76 65 73 2e 20 20 31 32 20 62 79 74 65 73 eaves. 12 bytes
138fc 20 66 6f 72 20 69 6e 74 65 72 69 6f 72 20 6e 6f for interior no
138fd 64 65 73 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d des.** |---
138fe 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a -------------|.*
138ff 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 70 6f * | cell po
13900 69 6e 74 65 72 20 20 20 7c 20 20 20 7c 20 20 32 inter | | 2
13901 20 62 79 74 65 73 20 70 65 72 20 63 65 6c 6c 2e bytes per cell.
13902 20 20 53 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a Sorted order..
13903 2a 2a 20 20 20 20 20 20 7c 20 61 72 72 61 79 20 ** | array
13904 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 | |
13905 47 72 6f 77 73 20 64 6f 77 6e 77 61 72 64 0a 2a Grows downward.*
13906 2a 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 * |
13907 20 20 20 20 20 20 20 20 7c 20 20 20 76 0a 2a 2a | v.**
13908 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d |---------
13909 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 -------|.**
1390a 20 7c 20 75 6e 61 6c 6c 6f 63 61 74 65 64 20 20 | unallocated
1390b 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 73 70 |.** | sp
1390c 61 63 65 20 20 20 20 20 20 20 20 20 20 7c 0a 2a ace |.*
1390d 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
1390e 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20 5e 20 20 47 --------| ^ G
1390f 72 6f 77 73 20 75 70 77 61 72 64 73 0a 2a 2a 20 rows upwards.**
13910 20 20 20 20 20 7c 20 63 65 6c 6c 20 63 6f 6e 74 | cell cont
13911 65 6e 74 20 20 20 7c 20 20 20 7c 20 20 41 72 62 ent | | Arb
13912 69 74 72 61 72 79 20 6f 72 64 65 72 20 69 6e 74 itrary order int
13913 65 72 73 70 65 72 73 65 64 20 77 69 74 68 20 66 erspersed with f
13914 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20 reeblocks..**
13915 20 20 20 7c 20 61 72 65 61 20 20 20 20 20 20 20 | area
13916 20 20 20 20 7c 20 20 20 7c 20 20 61 6e 64 20 66 | | and f
13917 72 65 65 20 73 70 61 63 65 20 66 72 61 67 6d 65 ree space fragme
13918 6e 74 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d nts..** |--
13919 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a --------------|.
1391a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 68 **.** The page h
1391b 65 61 64 65 72 73 20 6c 6f 6f 6b 73 20 6c 69 6b eaders looks lik
1391c 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 e this:.**.**
1391d 4f 46 46 53 45 54 20 20 20 53 49 5a 45 20 20 20 OFFSET SIZE
1391e 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a DESCRIPTION.**
1391f 20 20 20 20 20 20 30 20 20 20 20 20 20 20 31 20 0 1
13920 20 20 20 20 20 46 6c 61 67 73 2e 20 31 3a 20 69 Flags. 1: i
13921 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 72 6f 64 61 ntkey, 2: zeroda
13922 74 61 2c 20 34 3a 20 6c 65 61 66 64 61 74 61 2c ta, 4: leafdata,
13923 20 38 3a 20 6c 65 61 66 0a 2a 2a 20 20 20 20 20 8: leaf.**
13924 20 31 20 20 20 20 20 20 20 32 20 20 20 20 20 20 1 2
13925 62 79 74 65 20 6f 66 66 73 65 74 20 74 6f 20 74 byte offset to t
13926 68 65 20 66 69 72 73 74 20 66 72 65 65 62 6c 6f he first freeblo
13927 63 6b 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 20 ck.** 3
13928 20 20 20 32 20 20 20 20 20 20 6e 75 6d 62 65 72 2 number
13929 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 of cells on thi
1392a 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 35 s page.** 5
1392b 20 20 20 20 20 20 20 32 20 20 20 20 20 20 66 69 2 fi
1392c 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 rst byte of the
1392d 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
1392e 61 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20 20 a.** 7
1392f 20 20 31 20 20 20 20 20 20 6e 75 6d 62 65 72 20 1 number
13930 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 66 72 of fragmented fr
13931 65 65 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20 ee bytes.**
13932 20 38 20 20 20 20 20 20 20 34 20 20 20 20 20 20 8 4
13933 52 69 67 68 74 20 63 68 69 6c 64 20 28 74 68 65 Right child (the
13934 20 50 74 72 28 4e 29 20 76 61 6c 75 65 29 2e 20 Ptr(N) value).
13935 20 4f 6d 69 74 74 65 64 20 6f 6e 20 6c 65 61 76 Omitted on leav
13936 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c es..**.** The fl
13937 61 67 73 20 64 65 66 69 6e 65 20 74 68 65 20 66 ags define the f
13938 6f 72 6d 61 74 20 6f 66 20 74 68 69 73 20 62 74 ormat of this bt
13939 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 20 6c ree page. The l
1393a 65 61 66 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 eaf flag means t
1393b 68 61 74 0a 2a 2a 20 74 68 69 73 20 70 61 67 65 hat.** this page
1393c 20 68 61 73 20 6e 6f 20 63 68 69 6c 64 72 65 6e has no children
1393d 2e 20 20 54 68 65 20 7a 65 72 6f 64 61 74 61 20 . The zerodata
1393e 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 20 flag means that
1393f 74 68 69 73 20 70 61 67 65 20 63 61 72 72 69 65 this page carrie
13940 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65 79 73 20 61 s.** only keys a
13941 6e 64 20 6e 6f 20 64 61 74 61 2e 20 20 54 68 65 nd no data. The
13942 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 6d 65 61 intkey flag mea
13943 6e 73 20 74 68 61 74 20 74 68 65 20 6b 65 79 20 ns that the key
13944 69 73 20 61 20 69 6e 74 65 67 65 72 0a 2a 2a 20 is a integer.**
13945 77 68 69 63 68 20 69 73 20 73 74 6f 72 65 64 20 which is stored
13946 69 6e 20 74 68 65 20 6b 65 79 20 73 69 7a 65 20 in the key size
13947 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 65 6c entry of the cel
13948 6c 20 68 65 61 64 65 72 20 72 61 74 68 65 72 20 l header rather
13949 74 68 61 6e 20 69 6e 0a 2a 2a 20 74 68 65 20 70 than in.** the p
1394a 61 79 6c 6f 61 64 20 61 72 65 61 2e 0a 2a 2a 0a ayload area..**.
1394b 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e ** The cell poin
1394c 74 65 72 20 61 72 72 61 79 20 62 65 67 69 6e 73 ter array begins
1394d 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 62 79 on the first by
1394e 74 65 20 61 66 74 65 72 20 74 68 65 20 70 61 67 te after the pag
1394f 65 20 68 65 61 64 65 72 2e 0a 2a 2a 20 54 68 65 e header..** The
13950 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 cell pointer ar
13951 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 ray contains zer
13952 6f 20 6f 72 20 6d 6f 72 65 20 32 2d 62 79 74 65 o or more 2-byte
13953 20 6e 75 6d 62 65 72 73 20 77 68 69 63 68 20 61 numbers which a
13954 72 65 0a 2a 2a 20 6f 66 66 73 65 74 73 20 66 72 re.** offsets fr
13955 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 om the beginning
13956 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 of the page to
13957 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
13958 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 in the cell.**
13959 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 54 content area. T
1395a 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 he cell pointers
1395b 20 6f 63 63 75 72 20 69 6e 20 73 6f 72 74 65 64 occur in sorted
1395c 20 6f 72 64 65 72 2e 20 20 54 68 65 20 73 79 73 order. The sys
1395d 74 65 6d 20 73 74 72 69 76 65 73 0a 2a 2a 20 74 tem strives.** t
1395e 6f 20 6b 65 65 70 20 66 72 65 65 20 73 70 61 63 o keep free spac
1395f 65 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 e after the last
13960 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 73 6f cell pointer so
13961 20 74 68 61 74 20 6e 65 77 20 63 65 6c 6c 73 20 that new cells
13962 63 61 6e 0a 2a 2a 20 62 65 20 65 61 73 69 6c 79 can.** be easily
13963 20 61 64 64 65 64 20 77 69 74 68 6f 75 74 20 68 added without h
13964 61 76 69 6e 67 20 74 6f 20 64 65 66 72 61 67 6d aving to defragm
13965 65 6e 74 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a ent the page..**
13966 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 .** Cell content
13967 20 69 73 20 73 74 6f 72 65 64 20 61 74 20 74 68 is stored at th
13968 65 20 76 65 72 79 20 65 6e 64 20 6f 66 20 74 68 e very end of th
13969 65 20 70 61 67 65 20 61 6e 64 20 67 72 6f 77 73 e page and grows
1396a 20 74 6f 77 61 72 64 20 74 68 65 0a 2a 2a 20 62 toward the.** b
1396b 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
1396c 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 55 6e 75 73 page..**.** Unus
1396d 65 64 20 73 70 61 63 65 20 77 69 74 68 69 6e 20 ed space within
1396e 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
1396f 20 61 72 65 61 20 69 73 20 63 6f 6c 6c 65 63 74 area is collect
13970 65 64 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 ed into a linked
13971 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 66 72 65 65 list of.** free
13972 62 6c 6f 63 6b 73 2e 20 20 45 61 63 68 20 66 72 blocks. Each fr
13973 65 65 62 6c 6f 63 6b 20 69 73 20 61 74 20 6c 65 eeblock is at le
13974 61 73 74 20 34 20 62 79 74 65 73 20 69 6e 20 73 ast 4 bytes in s
13975 69 7a 65 2e 20 20 54 68 65 20 62 79 74 65 20 6f ize. The byte o
13976 66 66 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65 20 ffset.** to the
13977 66 69 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 20 first freeblock
13978 69 73 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 is given in the
13979 68 65 61 64 65 72 2e 20 20 46 72 65 65 62 6c 6f header. Freeblo
1397a 63 6b 73 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 cks occur in.**
1397b 69 6e 63 72 65 61 73 69 6e 67 20 6f 72 64 65 72 increasing order
1397c 2e 20 20 42 65 63 61 75 73 65 20 61 20 66 72 65 . Because a fre
1397d 65 62 6c 6f 63 6b 20 6d 75 73 74 20 62 65 20 61 eblock must be a
1397e 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 20 t least 4 bytes
1397f 69 6e 20 73 69 7a 65 2c 0a 2a 2a 20 61 6e 79 20 in size,.** any
13980 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65 group of 3 or fe
13981 77 65 72 20 75 6e 75 73 65 64 20 62 79 74 65 73 wer unused bytes
13982 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e in the cell con
13983 74 65 6e 74 20 61 72 65 61 20 63 61 6e 6e 6f 74 tent area cannot
13984 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 65 .** exist on the
13985 20 66 72 65 65 62 6c 6f 63 6b 20 63 68 61 69 6e freeblock chain
13986 2e 20 20 41 20 67 72 6f 75 70 20 6f 66 20 33 20 . A group of 3
13987 6f 72 20 66 65 77 65 72 20 66 72 65 65 20 62 79 or fewer free by
13988 74 65 73 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a tes is called.**
13989 20 61 20 66 72 61 67 6d 65 6e 74 2e 20 20 54 68 a fragment. Th
1398a 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
1398b 66 20 62 79 74 65 73 20 69 6e 20 61 6c 6c 20 66 f bytes in all f
1398c 72 61 67 6d 65 6e 74 73 20 69 73 20 72 65 63 6f ragments is reco
1398d 72 64 65 64 2e 0a 2a 2a 20 69 6e 20 74 68 65 20 rded..** in the
1398e 70 61 67 65 20 68 65 61 64 65 72 20 61 74 20 6f page header at o
1398f 66 66 73 65 74 20 37 2e 0a 2a 2a 0a 2a 2a 20 20 ffset 7..**.**
13990 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 SIZE DESCRI
13991 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 32 20 PTION.** 2
13992 20 20 20 20 42 79 74 65 20 6f 66 66 73 65 74 20 Byte offset
13993 6f 66 20 74 68 65 20 6e 65 78 74 20 66 72 65 65 of the next free
13994 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 32 20 block.** 2
13995 20 20 20 20 42 79 74 65 73 20 69 6e 20 74 68 69 Bytes in thi
13996 73 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 0a 2a s freeblock.**.*
13997 2a 20 43 65 6c 6c 73 20 61 72 65 20 6f 66 20 76 * Cells are of v
13998 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 2e 20 ariable length.
13999 20 43 65 6c 6c 73 20 61 72 65 20 73 74 6f 72 65 Cells are store
1399a 64 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f d in the cell co
1399b 6e 74 65 6e 74 20 61 72 65 61 20 61 74 0a 2a 2a ntent area at.**
1399c 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
1399d 70 61 67 65 2e 20 20 50 6f 69 6e 74 65 72 73 20 page. Pointers
1399e 74 6f 20 74 68 65 20 63 65 6c 6c 73 20 61 72 65 to the cells are
1399f 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 in the cell poi
139a0 6e 74 65 72 20 61 72 72 61 79 0a 2a 2a 20 74 68 nter array.** th
139a1 61 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 at immediately f
139a2 6f 6c 6c 6f 77 73 20 74 68 65 20 70 61 67 65 20 ollows the page
139a3 68 65 61 64 65 72 2e 20 20 43 65 6c 6c 73 20 69 header. Cells i
139a4 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c s not necessaril
139a5 79 0a 2a 2a 20 63 6f 6e 74 69 67 75 6f 75 73 20 y.** contiguous
139a6 6f 72 20 69 6e 20 6f 72 64 65 72 2c 20 62 75 74 or in order, but
139a7 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 cell pointers a
139a8 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 6e re contiguous an
139a9 64 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a d in order..**.*
139aa 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6d * Cell content m
139ab 61 6b 65 73 20 75 73 65 20 6f 66 20 76 61 72 69 akes use of vari
139ac 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 able length inte
139ad 67 65 72 73 2e 20 20 41 20 76 61 72 69 61 62 6c gers. A variabl
139ae 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 69 6e 74 65 e.** length inte
139af 67 65 72 20 69 73 20 31 20 74 6f 20 39 20 62 79 ger is 1 to 9 by
139b0 74 65 73 20 77 68 65 72 65 20 74 68 65 20 6c 6f tes where the lo
139b1 77 65 72 20 37 20 62 69 74 73 20 6f 66 20 65 61 wer 7 bits of ea
139b2 63 68 20 0a 2a 2a 20 62 79 74 65 20 61 72 65 20 ch .** byte are
139b3 75 73 65 64 2e 20 20 54 68 65 20 69 6e 74 65 67 used. The integ
139b4 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 er consists of a
139b5 6c 6c 20 62 79 74 65 73 20 74 68 61 74 20 68 61 ll bytes that ha
139b6 76 65 20 62 69 74 20 38 20 73 65 74 20 61 6e 64 ve bit 8 set and
139b7 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 .** the first by
139b8 74 65 20 77 69 74 68 20 62 69 74 20 38 20 63 6c te with bit 8 cl
139b9 65 61 72 2e 20 20 54 68 65 20 6d 6f 73 74 20 73 ear. The most s
139ba 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 ignificant byte
139bb 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a of the integer.*
139bc 2a 20 61 70 70 65 61 72 73 20 66 69 72 73 74 2e * appears first.
139bd 20 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e A variable-len
139be 67 74 68 20 69 6e 74 65 67 65 72 20 6d 61 79 20 gth integer may
139bf 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e not be more than
139c0 20 39 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a 9 bytes long..*
139c1 2a 20 41 73 20 61 20 73 70 65 63 69 61 6c 20 63 * As a special c
139c2 61 73 65 2c 20 61 6c 6c 20 38 20 62 79 74 65 73 ase, all 8 bytes
139c3 20 6f 66 20 74 68 65 20 39 74 68 20 62 79 74 65 of the 9th byte
139c4 20 61 72 65 20 75 73 65 64 20 61 73 20 64 61 74 are used as dat
139c5 61 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f a. This.** allo
139c6 77 73 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 ws a 64-bit inte
139c7 67 65 72 20 74 6f 20 62 65 20 65 6e 63 6f 64 65 ger to be encode
139c8 64 20 69 6e 20 39 20 62 79 74 65 73 2e 0a 2a 2a d in 9 bytes..**
139c9 0a 2a 2a 20 20 20 20 30 78 30 30 20 20 20 20 20 .** 0x00
139ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139cb 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 becomes 0x0000
139cc 30 30 30 30 0a 2a 2a 20 20 20 20 30 78 37 66 20 0000.** 0x7f
139cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139ce 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 becomes 0x
139cf 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 30 0000007f.** 0
139d0 78 38 31 20 30 78 30 30 20 20 20 20 20 20 20 20 x81 0x00
139d1 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 becomes
139d2 20 20 30 78 30 30 30 30 30 30 38 30 0a 2a 2a 20 0x00000080.**
139d3 20 20 20 30 78 38 32 20 30 78 30 30 20 20 20 20 0x82 0x00
139d4 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 bec
139d5 6f 6d 65 73 20 20 30 78 30 30 30 30 30 31 30 30 omes 0x00000100
139d6 0a 2a 2a 20 20 20 20 30 78 38 30 20 30 78 37 66 .** 0x80 0x7f
139d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139d8 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 becomes 0x0000
139d9 30 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 61 20 007f.** 0x8a
139da 30 78 39 31 20 30 78 64 31 20 30 78 61 63 20 30 0x91 0xd1 0xac 0
139db 78 37 38 20 20 62 65 63 6f 6d 65 73 20 20 30 78 x78 becomes 0x
139dc 31 32 33 34 35 36 37 38 0a 2a 2a 20 20 20 20 30 12345678.** 0
139dd 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30 78 x81 0x81 0x81 0x
139de 38 31 20 30 78 30 31 20 20 62 65 63 6f 6d 65 73 81 0x01 becomes
139df 20 20 30 78 31 30 32 30 34 30 38 31 0a 2a 2a 0a 0x10204081.**.
139e0 2a 2a 20 56 61 72 69 61 62 6c 65 20 6c 65 6e 67 ** Variable leng
139e1 74 68 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 th integers are
139e2 75 73 65 64 20 66 6f 72 20 72 6f 77 69 64 73 20 used for rowids
139e3 61 6e 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 and to hold the
139e4 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 number of.** byt
139e5 65 73 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 61 es of key and da
139e6 74 61 20 69 6e 20 61 20 62 74 72 65 65 20 63 65 ta in a btree ce
139e7 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f ll..**.** The co
139e8 6e 74 65 6e 74 20 6f 66 20 61 20 63 65 6c 6c 20 ntent of a cell
139e9 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a looks like this:
139ea 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 .**.** SIZE
139eb 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a DESCRIPTION.**
139ec 20 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 4 Page
139ed 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c number of the l
139ee 65 66 74 20 63 68 69 6c 64 2e 20 4f 6d 69 74 74 eft child. Omitt
139ef 65 64 20 69 66 20 6c 65 61 66 20 66 6c 61 67 20 ed if leaf flag
139f0 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76 is set..** v
139f1 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 ar Number of
139f2 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e 20 4f bytes of data. O
139f3 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 7a 65 mitted if the ze
139f4 72 6f 64 61 74 61 20 66 6c 61 67 20 69 73 20 73 rodata flag is s
139f5 65 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 et..** var
139f6 20 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 Number of byte
139f7 73 20 6f 66 20 6b 65 79 2e 20 4f 72 20 74 68 65 s of key. Or the
139f8 20 6b 65 79 20 69 74 73 65 6c 66 20 69 66 20 69 key itself if i
139f9 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73 65 ntkey flag is se
139fa 74 2e 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 t..** *
139fb 20 50 61 79 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 Payload.**
139fc 20 34 20 20 20 20 20 46 69 72 73 74 20 70 61 67 4 First pag
139fd 65 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f e of the overflo
139fe 77 20 63 68 61 69 6e 2e 20 20 4f 6d 69 74 74 65 w chain. Omitte
139ff 64 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 d if no overflow
13a00 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 66 6c 6f 77 20 .**.** Overflow
13a01 70 61 67 65 73 20 66 6f 72 6d 20 61 20 6c 69 6e pages form a lin
13a02 6b 65 64 20 6c 69 73 74 2e 20 20 45 61 63 68 20 ked list. Each
13a03 70 61 67 65 20 65 78 63 65 70 74 20 74 68 65 20 page except the
13a04 6c 61 73 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 last is complete
13a05 6c 79 0a 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 ly.** filled wit
13a06 68 20 64 61 74 61 20 28 70 61 67 65 73 69 7a 65 h data (pagesize
13a07 20 2d 20 34 20 62 79 74 65 73 29 2e 20 20 54 68 - 4 bytes). Th
13a08 65 20 6c 61 73 74 20 70 61 67 65 20 63 61 6e 20 e last page can
13a09 68 61 76 65 20 61 73 20 6c 69 74 74 6c 65 0a 2a have as little.*
13a0a 2a 20 61 73 20 31 20 62 79 74 65 20 6f 66 20 64 * as 1 byte of d
13a0b 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 ata..**.** SI
13a0c 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f ZE DESCRIPTIO
13a0d 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 N.** 4
13a0e 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e Page number of n
13a0f 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ext overflow pag
13a10 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20 e.** *
13a11 44 61 74 61 0a 2a 2a 0a 2a 2a 20 46 72 65 65 6c Data.**.** Freel
13a12 69 73 74 20 70 61 67 65 73 20 63 6f 6d 65 20 69 ist pages come i
13a13 6e 20 74 77 6f 20 73 75 62 74 79 70 65 73 3a 20 n two subtypes:
13a14 74 72 75 6e 6b 20 70 61 67 65 73 20 61 6e 64 20 trunk pages and
13a15 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 leaf pages. The
13a16 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 65 72 20 .** file header
13a17 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 points to the fi
13a18 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 rst in a linked
13a19 6c 69 73 74 20 6f 66 20 74 72 75 6e 6b 20 70 61 list of trunk pa
13a1a 67 65 2e 20 20 45 61 63 68 20 74 72 75 6e 6b 0a ge. Each trunk.
13a1b 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 73 20 74 ** page points t
13a1c 6f 20 6d 75 6c 74 69 70 6c 65 20 6c 65 61 66 20 o multiple leaf
13a1d 70 61 67 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 pages. The cont
13a1e 65 6e 74 20 6f 66 20 61 20 6c 65 61 66 20 70 61 ent of a leaf pa
13a1f 67 65 20 69 73 0a 2a 2a 20 75 6e 73 70 65 63 69 ge is.** unspeci
13a20 66 69 65 64 2e 20 20 41 20 74 72 75 6e 6b 20 70 fied. A trunk p
13a21 61 67 65 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 age looks like t
13a22 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 his:.**.** SI
13a23 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f ZE DESCRIPTIO
13a24 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 N.** 4
13a25 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e Page number of n
13a26 65 78 74 20 74 72 75 6e 6b 20 70 61 67 65 0a 2a ext trunk page.*
13a27 2a 20 20 20 20 20 20 34 20 20 20 20 20 4e 75 6d * 4 Num
13a28 62 65 72 20 6f 66 20 6c 65 61 66 20 70 6f 69 6e ber of leaf poin
13a29 74 65 72 73 20 6f 6e 20 74 68 69 73 20 70 61 67 ters on this pag
13a2a 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20 e.** *
13a2b 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 zero or more pag
13a2c 65 73 20 6e 75 6d 62 65 72 73 20 6f 66 20 6c 65 es numbers of le
13a2d 61 76 65 73 0a 2a 2f 0a 0a 2f 2a 20 52 6f 75 6e aves.*/../* Roun
13a2e 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74 6f d up a number to
13a2f 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 the next larger
13a30 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20 multiple of 8.
13a31 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a 2a This is used.**
13a32 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74 65 to force 8-byte
13a33 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36 34 alignment on 64
13a34 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75 72 -bit architectur
13a35 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 es..*/.#define R
13a36 4f 55 4e 44 38 28 78 29 20 20 20 28 28 78 2b 37 OUND8(x) ((x+7
13a37 29 26 7e 37 29 0a 0a 0a 2f 2a 20 54 68 65 20 66 )&~7).../* The f
13a38 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 69 ollowing value i
13a39 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 s the maximum ce
13a3a 6c 6c 20 73 69 7a 65 20 61 73 73 75 6d 69 6e 67 ll size assuming
13a3b 20 61 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 0a a maximum page.
13a3c 2a 2a 20 73 69 7a 65 20 67 69 76 65 20 61 62 6f ** size give abo
13a3d 76 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d ve..*/.#define M
13a3e 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 X_CELL_SIZE(pBt)
13a3f 20 20 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 (pBt->pageSize
13a40 2d 38 29 0a 0a 2f 2a 20 54 68 65 20 6d 61 78 69 -8)../* The maxi
13a41 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 mum number of ce
13a42 6c 6c 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 lls on a single
13a43 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
13a44 62 61 73 65 2e 20 20 54 68 69 73 0a 2a 2a 20 61 base. This.** a
13a45 73 73 75 6d 65 73 20 61 20 6d 69 6e 69 6d 75 6d ssumes a minimum
13a46 20 63 65 6c 6c 20 73 69 7a 65 20 6f 66 20 36 20 cell size of 6
13a47 62 79 74 65 73 20 20 28 34 20 62 79 74 65 73 20 bytes (4 bytes
13a48 66 6f 72 20 74 68 65 20 63 65 6c 6c 20 69 74 73 for the cell its
13a49 65 6c 66 0a 2a 2a 20 70 6c 75 73 20 32 20 62 79 elf.** plus 2 by
13a4a 74 65 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65 tes for the inde
13a4b 78 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e x to the cell in
13a4c 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
13a4d 29 2e 20 20 53 75 63 68 0a 2a 2a 20 73 6d 61 6c ). Such.** smal
13a4e 6c 20 63 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20 l cells will be
13a4f 72 61 72 65 2c 20 62 75 74 20 74 68 65 79 20 61 rare, but they a
13a50 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a re possible..*/.
13a51 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28 #define MX_CELL(
13a52 70 42 74 29 20 28 28 70 42 74 2d 3e 70 61 67 65 pBt) ((pBt->page
13a53 53 69 7a 65 2d 38 29 2f 36 29 0a 0a 2f 2a 20 46 Size-8)/6)../* F
13a54 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
13a55 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 ons */.typedef s
13a56 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 4d 65 truct MemPage Me
13a57 6d 50 61 67 65 3b 0a 74 79 70 65 64 65 66 20 73 mPage;.typedef s
13a58 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 42 74 4c truct BtLock BtL
13a59 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ock;../*.** This
13a5a 20 69 73 20 61 20 6d 61 67 69 63 20 73 74 72 69 is a magic stri
13a5b 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20 ng that appears
13a5c 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
13a5d 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c of every.** SQL
13a5e 69 74 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 ite database in
13a5f 6f 72 64 65 72 20 74 6f 20 69 64 65 6e 74 69 66 order to identif
13a60 79 20 74 68 65 20 66 69 6c 65 20 61 73 20 61 20 y the file as a
13a61 72 65 61 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a real database..*
13a62 2a 0a 2a 2a 20 59 6f 75 20 63 61 6e 20 63 68 61 *.** You can cha
13a63 6e 67 65 20 74 68 69 73 20 76 61 6c 75 65 20 61 nge this value a
13a64 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62 t compile-time b
13a65 79 20 73 70 65 63 69 66 79 69 6e 67 20 61 0a 2a y specifying a.*
13a66 2a 20 2d 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f * -DSQLITE_FILE_
13a67 48 45 41 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20 HEADER="..." on
13a68 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d the compiler com
13a69 6d 61 6e 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a mand-line. The.
13a6a 2a 2a 20 68 65 61 64 65 72 20 6d 75 73 74 20 62 ** header must b
13a6b 65 20 65 78 61 63 74 6c 79 20 31 36 20 62 79 74 e exactly 16 byt
13a6c 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 es including the
13a6d 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 zero-terminator
13a6e 20 73 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e so.** the strin
13a6f 67 20 69 74 73 65 6c 66 20 73 68 6f 75 6c 64 20 g itself should
13a70 62 65 20 31 35 20 63 68 61 72 61 63 74 65 72 73 be 15 characters
13a71 20 6c 6f 6e 67 2e 20 20 49 66 20 79 6f 75 20 63 long. If you c
13a72 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20 68 65 61 hange.** the hea
13a73 64 65 72 2c 20 74 68 65 6e 20 79 6f 75 72 20 63 der, then your c
13a74 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 20 77 69 ustom library wi
13a75 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 ll not be able t
13a76 6f 20 72 65 61 64 20 0a 2a 2a 20 64 61 74 61 62 o read .** datab
13a77 61 73 65 73 20 67 65 6e 65 72 61 74 65 64 20 62 ases generated b
13a78 79 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 74 y the standard t
13a79 6f 6f 6c 73 20 61 6e 64 20 74 68 65 20 73 74 61 ools and the sta
13a7a 6e 64 61 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77 ndard tools.** w
13a7b 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 ill not be able
13a7c 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65 to read database
13a7d 73 20 63 72 65 61 74 65 64 20 62 79 20 79 6f 75 s created by you
13a7e 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 r custom library
13a7f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
13a80 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 ITE_FILE_HEADER
13a81 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31 32 33 /* 123456789 123
13a82 34 35 36 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65 456 */.# define
13a83 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 SQLITE_FILE_HEA
13a84 44 45 52 20 22 53 51 4c 69 74 65 20 66 6f 72 6d DER "SQLite form
13a85 61 74 20 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a at 3".#endif../*
13a86 0a 2a 2a 20 50 61 67 65 20 74 79 70 65 20 66 6c .** Page type fl
13a87 61 67 73 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f ags. An ORed co
13a88 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 mbination of the
13a89 73 65 20 66 6c 61 67 73 20 61 70 70 65 61 72 20 se flags appear
13a8a 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 as the.** first
13a8b 62 79 74 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20 byte of on-disk
13a8c 69 6d 61 67 65 20 6f 66 20 65 76 65 72 79 20 42 image of every B
13a8d 54 72 65 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64 Tree page..*/.#d
13a8e 65 66 69 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59 efine PTF_INTKEY
13a8f 20 20 20 20 30 78 30 31 0a 23 64 65 66 69 6e 65 0x01.#define
13a90 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 20 30 PTF_ZERODATA 0
13a91 78 30 32 0a 23 64 65 66 69 6e 65 20 50 54 46 5f x02.#define PTF_
13a92 4c 45 41 46 44 41 54 41 20 20 30 78 30 34 0a 23 LEAFDATA 0x04.#
13a93 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 20 define PTF_LEAF
13a94 20 20 20 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 0x08../*.**
13a95 20 41 73 20 65 61 63 68 20 70 61 67 65 20 6f 66 As each page of
13a96 20 74 68 65 20 66 69 6c 65 20 69 73 20 6c 6f 61 the file is loa
13a97 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c ded into memory,
13a98 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
13a99 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a the following.**
13a9a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70 structure is ap
13a9b 70 65 6e 64 65 64 20 61 6e 64 20 69 6e 69 74 69 pended and initi
13a9c 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20 alized to zero.
13a9d 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
13a9e 73 74 6f 72 65 73 0a 2a 2a 20 69 6e 66 6f 72 6d stores.** inform
13a9f 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
13aa0 70 61 67 65 20 74 68 61 74 20 69 73 20 64 65 63 page that is dec
13aa1 6f 64 65 64 20 66 72 6f 6d 20 74 68 65 20 72 61 oded from the ra
13aa2 77 20 66 69 6c 65 20 70 61 67 65 2e 0a 2a 2a 0a w file page..**.
13aa3 2a 2a 20 54 68 65 20 70 50 61 72 65 6e 74 20 66 ** The pParent f
13aa4 69 65 6c 64 20 70 6f 69 6e 74 73 20 62 61 63 6b ield points back
13aa5 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 to the parent p
13aa6 61 67 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 age. This allow
13aa7 73 20 75 73 20 74 6f 0a 2a 2a 20 77 61 6c 6b 20 s us to.** walk
13aa8 75 70 20 74 68 65 20 42 54 72 65 65 20 66 72 6f up the BTree fro
13aa9 6d 20 61 6e 79 20 6c 65 61 66 20 74 6f 20 74 68 m any leaf to th
13aaa 65 20 72 6f 6f 74 2e 20 20 43 61 72 65 20 6d 75 e root. Care mu
13aab 73 74 20 62 65 20 74 61 6b 65 6e 20 74 6f 0a 2a st be taken to.*
13aac 2a 20 75 6e 72 65 66 28 29 20 74 68 65 20 70 61 * unref() the pa
13aad 72 65 6e 74 20 70 61 67 65 20 70 6f 69 6e 74 65 rent page pointe
13aae 72 20 77 68 65 6e 20 74 68 69 73 20 70 61 67 65 r when this page
13aaf 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 is no longer re
13ab0 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 20 54 68 65 ferenced..** The
13ab1 20 70 61 67 65 44 65 73 74 72 75 63 74 6f 72 28 pageDestructor(
13ab2 29 20 72 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65 ) routine handle
13ab3 73 20 74 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2a s that chore..**
13ab4 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f 20 61 6c .** Access to al
13ab5 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 l fields of this
13ab6 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 6f structure is co
13ab7 6e 74 72 6f 6c 6c 65 64 20 62 79 20 74 68 65 20 ntrolled by the
13ab8 6d 75 74 65 78 0a 2a 2a 20 73 74 6f 72 65 64 20 mutex.** stored
13ab9 69 6e 20 4d 65 6d 50 61 67 65 2e 70 42 74 2d 3e in MemPage.pBt->
13aba 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 mutex..*/.struct
13abb 20 4d 65 6d 50 61 67 65 20 7b 0a 20 20 75 38 20 MemPage {. u8
13abc 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 isInit;
13abd 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 65 /* True if pre
13abe 76 69 6f 75 73 6c 79 20 69 6e 69 74 69 61 6c 69 viously initiali
13abf 7a 65 64 2e 20 4d 55 53 54 20 42 45 20 46 49 52 zed. MUST BE FIR
13ac0 53 54 21 20 2a 2f 0a 20 20 75 38 20 6e 4f 76 65 ST! */. u8 nOve
13ac1 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a rflow; /*
13ac2 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76 65 72 66 Number of overf
13ac3 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 low cell bodies
13ac4 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 in aCell[] */.
13ac5 75 38 20 69 6e 74 4b 65 79 3b 20 20 20 20 20 20 u8 intKey;
13ac6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
13ac7 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73 intkey flag is s
13ac8 65 74 20 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b et */. u8 leaf;
13ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13aca 54 72 75 65 20 69 66 20 6c 65 61 66 20 66 6c 61 True if leaf fla
13acb 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 g is set */. u8
13acc 20 68 61 73 44 61 74 61 3b 20 20 20 20 20 20 20 hasData;
13acd 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
13ace 69 73 20 70 61 67 65 20 73 74 6f 72 65 73 20 64 is page stores d
13acf 61 74 61 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f ata */. u8 hdrO
13ad0 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a ffset; /*
13ad1 20 31 30 30 20 66 6f 72 20 70 61 67 65 20 31 2e 100 for page 1.
13ad2 20 20 30 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0 otherwise */
13ad3 0a 20 20 75 38 20 63 68 69 6c 64 50 74 72 53 69 . u8 childPtrSi
13ad4 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20 69 66 20 ze; /* 0 if
13ad5 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c leaf==1. 4 if l
13ad6 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75 31 36 20 eaf==0 */. u16
13ad7 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 maxLocal;
13ad8 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 /* Copy of BtSh
13ad9 61 72 65 64 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72 ared.maxLocal or
13ada 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 65 61 BtShared.maxLea
13adb 66 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f f */. u16 minLo
13adc 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 cal; /* C
13add 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e opy of BtShared.
13ade 6d 69 6e 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 minLocal or BtSh
13adf 61 72 65 64 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a ared.minLeaf */.
13ae0 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 u16 cellOffset
13ae1 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 ; /* Index
13ae2 69 6e 20 61 44 61 74 61 20 6f 66 20 66 69 72 73 in aData of firs
13ae3 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a t cell pointer *
13ae4 2f 0a 20 20 75 31 36 20 6e 46 72 65 65 3b 20 20 /. u16 nFree;
13ae5 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
13ae6 65 72 20 6f 66 20 66 72 65 65 20 62 79 74 65 73 er of free bytes
13ae7 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a on the page */.
13ae8 20 20 75 31 36 20 6e 43 65 6c 6c 3b 20 20 20 20 u16 nCell;
13ae9 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
13aea 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 of cells on thi
13aeb 73 20 70 61 67 65 2c 20 6c 6f 63 61 6c 20 61 6e s page, local an
13aec 64 20 6f 76 66 6c 20 2a 2f 0a 20 20 75 31 36 20 d ovfl */. u16
13aed 6d 61 73 6b 50 61 67 65 3b 20 20 20 20 20 20 20 maskPage;
13aee 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 70 61 67 /* Mask for pag
13aef 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 73 74 e offset */. st
13af0 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 7b ruct _OvflCell {
13af1 20 20 20 2f 2a 20 43 65 6c 6c 73 20 74 68 61 74 /* Cells that
13af2 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e will not fit on
13af3 20 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20 aData[] */.
13af4 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 u8 *pCell;
13af5 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 /* Pointers
13af6 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 to the body of t
13af7 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c he overflow cell
13af8 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 64 78 3b */. u16 idx;
13af9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
13afa 6e 73 65 72 74 20 74 68 69 73 20 63 65 6c 6c 20 nsert this cell
13afb 62 65 66 6f 72 65 20 69 64 78 2d 74 68 20 6e 6f before idx-th no
13afc 6e 2d 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 n-overflow cell
13afd 2a 2f 0a 20 20 7d 20 61 4f 76 66 6c 5b 35 5d 3b */. } aOvfl[5];
13afe 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
13aff 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 ; /* Point
13b00 65 72 20 74 6f 20 42 74 53 68 61 72 65 64 20 74 er to BtShared t
13b01 68 61 74 20 74 68 69 73 20 70 61 67 65 20 69 73 hat this page is
13b02 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 75 38 part of */. u8
13b03 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 *aData;
13b04 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
13b05 20 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 74 disk image of t
13b06 68 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a he page data */.
13b07 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
13b08 65 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 e; /* Pager
13b09 70 61 67 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 page handle */.
13b0a 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 Pgno pgno;
13b0b 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
13b0c 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61 mber for this pa
13b0d 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ge */.};../*.**
13b0e 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d The in-memory im
13b0f 61 67 65 20 6f 66 20 61 20 64 69 73 6b 20 70 61 age of a disk pa
13b10 67 65 20 68 61 73 20 74 68 65 20 61 75 78 69 6c ge has the auxil
13b11 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e iary information
13b12 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20 appended.** to
13b13 74 68 65 20 65 6e 64 2e 20 20 45 58 54 52 41 5f the end. EXTRA_
13b14 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62 SIZE is the numb
13b15 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
13b16 70 61 63 65 20 6e 65 65 64 65 64 20 74 6f 20 68 pace needed to h
13b17 6f 6c 64 0a 2a 2a 20 74 68 61 74 20 65 78 74 72 old.** that extr
13b18 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a a information..*
13b19 2f 0a 23 64 65 66 69 6e 65 20 45 58 54 52 41 5f /.#define EXTRA_
13b1a 53 49 5a 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50 SIZE sizeof(MemP
13b1b 61 67 65 29 0a 0a 2f 2a 20 41 20 42 74 72 65 65 age)../* A Btree
13b1c 20 68 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 handle.**.** A
13b1d 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
13b1e 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 ion contains a p
13b1f 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 ointer to an ins
13b20 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 tance of.** this
13b21 20 6f 62 6a 65 63 74 20 66 6f 72 20 65 76 65 72 object for ever
13b22 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 y database file
13b23 74 68 61 74 20 69 74 20 68 61 73 20 6f 70 65 6e that it has open
13b24 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 . This structur
13b25 65 0a 2a 2a 20 69 73 20 6f 70 61 71 75 65 20 74 e.** is opaque t
13b26 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 o the database c
13b27 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 onnection. The
13b28 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
13b29 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 73 65 ion cannot.** se
13b2a 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 e the internals
13b2b 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
13b2c 65 20 61 6e 64 20 6f 6e 6c 79 20 64 65 61 6c 73 e and only deals
13b2d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 with pointers t
13b2e 6f 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 o.** this struct
13b2f 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 ure..**.** For s
13b30 6f 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c ome database fil
13b31 65 73 2c 20 74 68 65 20 73 61 6d 65 20 75 6e 64 es, the same und
13b32 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 erlying database
13b33 20 63 61 63 68 65 20 6d 69 67 68 74 20 62 65 20 cache might be
13b34 0a 2a 2a 20 73 68 61 72 65 64 20 62 65 74 77 65 .** shared betwe
13b35 65 6e 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 6e en multiple conn
13b36 65 63 74 69 6f 6e 73 2e 20 20 49 6e 20 74 68 61 ections. In tha
13b37 74 20 63 61 73 65 2c 20 65 61 63 68 20 63 6f 6e t case, each con
13b38 74 65 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 69 tection.** has i
13b39 74 20 6f 77 6e 20 70 6f 69 6e 74 65 72 20 74 6f t own pointer to
13b3a 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 20 20 42 this object. B
13b3b 75 74 20 65 61 63 68 20 69 6e 73 74 61 6e 63 65 ut each instance
13b3c 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 0a of this object.
13b3d 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 ** points to the
13b3e 20 73 61 6d 65 20 42 74 53 68 61 72 65 64 20 6f same BtShared o
13b3f 62 6a 65 63 74 2e 20 20 54 68 65 20 64 61 74 61 bject. The data
13b40 62 61 73 65 20 63 61 63 68 65 20 61 6e 64 20 74 base cache and t
13b41 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 61 73 73 he.** schema ass
13b42 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
13b43 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
13b44 72 65 20 61 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 re all contained
13b45 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 42 within.** the B
13b46 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a tShared object..
13b47 2a 2a 0a 2a 2a 20 41 6c 6c 20 66 69 65 6c 64 73 **.** All fields
13b48 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 in this structu
13b49 72 65 20 61 72 65 20 61 63 63 65 73 73 65 64 20 re are accessed
13b4a 75 6e 64 65 72 20 73 71 6c 69 74 65 33 2e 6d 75 under sqlite3.mu
13b4b 74 65 78 2e 0a 2a 2a 20 54 68 65 20 70 42 74 20 tex..** The pBt
13b4c 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 20 6d pointer itself m
13b4d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 ay not be change
13b4e 64 20 77 68 69 6c 65 20 74 68 65 72 65 20 65 78 d while there ex
13b4f 69 73 74 73 20 63 75 72 73 6f 72 73 20 0a 2a 2a ists cursors .**
13b50 20 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 in the referenc
13b51 65 64 20 42 74 53 68 61 72 65 64 20 74 68 61 74 ed BtShared that
13b52 20 70 6f 69 6e 74 20 62 61 63 6b 20 74 6f 20 74 point back to t
13b53 68 69 73 20 42 74 72 65 65 20 73 69 6e 63 65 20 his Btree since
13b54 74 68 6f 73 65 0a 2a 2a 20 63 75 72 73 6f 72 73 those.** cursors
13b55 20 68 61 76 65 20 74 6f 20 64 6f 20 67 6f 20 74 have to do go t
13b56 68 72 6f 75 67 68 20 74 68 69 73 20 42 74 72 65 hrough this Btre
13b57 65 20 74 6f 20 66 69 6e 64 20 74 68 65 69 72 20 e to find their
13b58 42 74 53 68 61 72 65 64 20 61 6e 64 0a 2a 2a 20 BtShared and.**
13b59 74 68 65 79 20 6f 66 74 65 6e 20 64 6f 20 73 6f they often do so
13b5a 20 77 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 without holding
13b5b 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a sqlite3.mutex..
13b5c 2a 2f 0a 73 74 72 75 63 74 20 42 74 72 65 65 20 */.struct Btree
13b5d 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
13b5e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
13b5f 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
13b60 6e 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 62 n holding this b
13b61 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 tree */. BtShar
13b62 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 ed *pBt; /*
13b63 53 68 61 72 61 62 6c 65 20 63 6f 6e 74 65 6e 74 Sharable content
13b64 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 2a of this btree *
13b65 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 3b 20 /. u8 inTrans;
13b66 20 20 20 20 20 20 20 2f 2a 20 54 52 41 4e 53 5f /* TRANS_
13b67 4e 4f 4e 45 2c 20 54 52 41 4e 53 5f 52 45 41 44 NONE, TRANS_READ
13b68 20 6f 72 20 54 52 41 4e 53 5f 57 52 49 54 45 20 or TRANS_WRITE
13b69 2a 2f 0a 20 20 75 38 20 73 68 61 72 61 62 6c 65 */. u8 sharable
13b6a 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 ; /* True
13b6b 69 66 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 if we can share
13b6c 70 42 74 20 77 69 74 68 20 61 6e 6f 74 68 65 72 pBt with another
13b6d 20 64 62 20 2a 2f 0a 20 20 75 38 20 6c 6f 63 6b db */. u8 lock
13b6e 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ed; /* T
13b6f 72 75 65 20 69 66 20 64 62 20 63 75 72 72 65 6e rue if db curren
13b70 74 6c 79 20 68 61 73 20 70 42 74 20 6c 6f 63 6b tly has pBt lock
13b71 65 64 20 2a 2f 0a 20 20 69 6e 74 20 77 61 6e 74 ed */. int want
13b72 54 6f 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e 75 ToLock; /* Nu
13b73 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 mber of nested c
13b74 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 alls to sqlite3B
13b75 74 72 65 65 45 6e 74 65 72 28 29 20 2a 2f 0a 20 treeEnter() */.
13b76 20 42 74 72 65 65 20 2a 70 4e 65 78 74 3b 20 20 Btree *pNext;
13b77 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f /* List of o
13b78 74 68 65 72 20 73 68 61 72 61 62 6c 65 20 42 74 ther sharable Bt
13b79 72 65 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 rees from the sa
13b7a 6d 65 20 64 62 20 2a 2f 0a 20 20 42 74 72 65 65 me db */. Btree
13b7b 20 2a 70 50 72 65 76 3b 20 20 20 20 20 20 2f 2a *pPrev; /*
13b7c 20 42 61 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66 Back pointer of
13b7d 20 74 68 65 20 73 61 6d 65 20 6c 69 73 74 20 2a the same list *
13b7e 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 65 /.};../*.** Btre
13b7f 65 2e 69 6e 54 72 61 6e 73 20 6d 61 79 20 74 61 e.inTrans may ta
13b80 6b 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f ke one of the fo
13b81 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a llowing values..
13b82 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 61 **.** If the sha
13b83 72 65 64 2d 64 61 74 61 20 65 78 74 65 6e 73 69 red-data extensi
13b84 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 on is enabled, t
13b85 68 65 72 65 20 6d 61 79 20 62 65 20 6d 75 6c 74 here may be mult
13b86 69 70 6c 65 20 75 73 65 72 73 0a 2a 2a 20 6f 66 iple users.** of
13b87 20 74 68 65 20 42 74 72 65 65 20 73 74 72 75 63 the Btree struc
13b88 74 75 72 65 2e 20 41 74 20 6d 6f 73 74 20 6f 6e ture. At most on
13b89 65 20 6f 66 20 74 68 65 73 65 20 6d 61 79 20 6f e of these may o
13b8a 70 65 6e 20 61 20 77 72 69 74 65 20 74 72 61 6e pen a write tran
13b8b 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 saction,.** but
13b8c 61 6e 79 20 6e 75 6d 62 65 72 20 6d 61 79 20 68 any number may h
13b8d 61 76 65 20 61 63 74 69 76 65 20 72 65 61 64 20 ave active read
13b8e 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a 2f transactions..*/
13b8f 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 4e .#define TRANS_N
13b90 4f 4e 45 20 20 30 0a 23 64 65 66 69 6e 65 20 54 ONE 0.#define T
13b91 52 41 4e 53 5f 52 45 41 44 20 20 31 0a 23 64 65 RANS_READ 1.#de
13b92 66 69 6e 65 20 54 52 41 4e 53 5f 57 52 49 54 45 fine TRANS_WRITE
13b93 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 2../*.** An ins
13b94 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
13b95 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 ject represents
13b96 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 a single databas
13b97 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 e file..** .** A
13b98 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 single database
13b99 20 66 69 6c 65 20 63 61 6e 20 62 65 20 69 6e 20 file can be in
13b9a 75 73 65 20 61 73 20 74 68 65 20 73 61 6d 65 20 use as the same
13b9b 74 69 6d 65 20 62 79 20 74 77 6f 0a 2a 2a 20 6f time by two.** o
13b9c 72 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 r more database
13b9d 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 57 68 connections. Wh
13b9e 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 en two or more c
13b9f 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a onnections are.*
13ba0 2a 20 73 68 61 72 69 6e 67 20 74 68 65 20 73 61 * sharing the sa
13ba1 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 me database file
13ba2 2c 20 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f , each connectio
13ba3 6e 20 68 61 73 20 69 74 20 6f 77 6e 0a 2a 2a 20 n has it own.**
13ba4 70 72 69 76 61 74 65 20 42 74 72 65 65 20 6f 62 private Btree ob
13ba5 6a 65 63 74 20 66 6f 72 20 74 68 65 20 66 69 6c ject for the fil
13ba6 65 20 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68 e and each of th
13ba7 6f 73 65 20 42 74 72 65 65 73 20 70 6f 69 6e 74 ose Btrees point
13ba8 73 0a 2a 2a 20 74 6f 20 74 68 69 73 20 6f 6e 65 s.** to this one
13ba9 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
13baa 2e 20 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 . BtShared.nRef
13bab 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
13bac 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 f.** connections
13bad 20 63 75 72 72 65 6e 74 6c 79 20 73 68 61 72 69 currently shari
13bae 6e 67 20 74 68 69 73 20 64 61 74 61 62 61 73 65 ng this database
13baf 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 file..**.** Fie
13bb0 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 lds in this stru
13bb1 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73 cture are access
13bb2 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 53 ed under the BtS
13bb3 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 6d hared.mutex.** m
13bb4 75 74 65 78 2c 20 65 78 63 65 70 74 20 66 6f 72 utex, except for
13bb5 20 6e 52 65 66 20 61 6e 64 20 70 4e 65 78 74 20 nRef and pNext
13bb6 77 68 69 63 68 20 61 72 65 20 61 63 63 65 73 73 which are access
13bb7 65 64 20 75 6e 64 65 72 20 74 68 65 0a 2a 2a 20 ed under the.**
13bb8 67 6c 6f 62 61 6c 20 53 51 4c 49 54 45 5f 4d 55 global SQLITE_MU
13bb9 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
13bba 52 20 6d 75 74 65 78 2e 20 20 54 68 65 20 70 50 R mutex. The pP
13bbb 61 67 65 72 20 66 69 65 6c 64 0a 2a 2a 20 6d 61 ager field.** ma
13bbc 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 y not be modifie
13bbd 64 20 6f 6e 63 65 20 69 74 20 69 73 20 69 6e 69 d once it is ini
13bbe 74 69 61 6c 6c 79 20 73 65 74 20 61 73 20 6c 6f tially set as lo
13bbf 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a ng as nRef>0..**
13bc0 20 54 68 65 20 70 53 63 68 65 6d 61 20 66 69 65 The pSchema fie
13bc1 6c 64 20 6d 61 79 20 62 65 20 73 65 74 20 6f 6e ld may be set on
13bc2 63 65 20 75 6e 64 65 72 20 42 74 53 68 61 72 65 ce under BtShare
13bc3 64 2e 6d 75 74 65 78 20 61 6e 64 0a 2a 2a 20 74 d.mutex and.** t
13bc4 68 65 72 65 61 66 74 65 72 20 69 73 20 75 6e 63 hereafter is unc
13bc5 68 61 6e 67 65 64 20 61 73 20 6c 6f 6e 67 20 61 hanged as long a
13bc6 73 20 6e 52 65 66 3e 30 2e 0a 2a 2f 0a 73 74 72 s nRef>0..*/.str
13bc7 75 63 74 20 42 74 53 68 61 72 65 64 20 7b 0a 20 uct BtShared {.
13bc8 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 Pager *pPager;
13bc9 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
13bca 67 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 73 71 ge cache */. sq
13bcb 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
13bcc 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
13bcd 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 connection curre
13bce 6e 74 6c 79 20 75 73 69 6e 67 20 74 68 69 73 20 ntly using this
13bcf 42 74 72 65 65 20 2a 2f 0a 20 20 42 74 43 75 72 Btree */. BtCur
13bd0 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 sor *pCursor;
13bd1 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 61 6c /* A list of al
13bd2 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 2a l open cursors *
13bd3 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 /. MemPage *pPa
13bd4 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 46 69 72 ge1; /* Fir
13bd5 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 st page of the d
13bd6 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 atabase */. u8
13bd7 69 6e 53 74 6d 74 3b 20 20 20 20 20 20 20 20 20 inStmt;
13bd8 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 /* True if we
13bd9 20 61 72 65 20 69 6e 20 61 20 73 74 61 74 65 6d are in a statem
13bda 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 ent subtransacti
13bdb 6f 6e 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f on */. u8 readO
13bdc 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a nly; /*
13bdd 20 54 72 75 65 20 69 66 20 74 68 65 20 75 6e 64 True if the und
13bde 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 69 73 20 erlying file is
13bdf 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 readonly */. u8
13be0 20 70 61 67 65 53 69 7a 65 46 69 78 65 64 3b 20 pageSizeFixed;
13be1 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
13be2 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e he page size can
13be3 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68 no longer be ch
13be4 61 6e 67 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66 anged */.#ifndef
13be5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
13be6 4f 56 41 43 55 55 4d 0a 20 20 75 38 20 61 75 74 OVACUUM. u8 aut
13be7 6f 56 61 63 75 75 6d 3b 20 20 20 20 20 20 20 20 oVacuum;
13be8 2f 2a 20 54 72 75 65 20 69 66 20 61 75 74 6f 2d /* True if auto-
13be9 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 vacuum is enable
13bea 64 20 2a 2f 0a 20 20 75 38 20 69 6e 63 72 56 61 d */. u8 incrVa
13beb 63 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 cuum; /*
13bec 54 72 75 65 20 69 66 20 69 6e 63 72 2d 76 61 63 True if incr-vac
13bed 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a uum is enabled *
13bee 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 20 70 /.#endif. u16 p
13bef 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 ageSize;
13bf0 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
13bf1 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 61 20 70 of bytes on a p
13bf2 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 75 73 61 age */. u16 usa
13bf3 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 2f bleSize; /
13bf4 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 * Number of usab
13bf5 6c 65 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 le bytes on each
13bf6 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6d page */. u16 m
13bf7 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 axLocal;
13bf8 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 /* Maximum loca
13bf9 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e l payload in non
13bfa 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 -LEAFDATA tables
13bfb 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 */. u16 minLoc
13bfc 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d al; /* M
13bfd 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 inimum local pay
13bfe 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 load in non-LEAF
13bff 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 DATA tables */.
13c00 20 75 31 36 20 6d 61 78 4c 65 61 66 3b 20 20 20 u16 maxLeaf;
13c01 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
13c02 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 m local payload
13c03 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 in a LEAFDATA ta
13c04 62 6c 65 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e ble */. u16 min
13c05 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f Leaf; /
13c06 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 * Minimum local
13c07 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 payload in a LEA
13c08 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 FDATA table */.
13c09 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74 69 6f u8 inTransactio
13c0a 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 61 n; /* Transa
13c0b 63 74 69 6f 6e 20 73 74 61 74 65 20 2a 2f 0a 20 ction state */.
13c0c 20 69 6e 74 20 6e 54 72 61 6e 73 61 63 74 69 6f int nTransactio
13c0d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 n; /* Number
13c0e 20 6f 66 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 of open transac
13c0f 74 69 6f 6e 73 20 28 72 65 61 64 20 2b 20 77 72 tions (read + wr
13c10 69 74 65 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a ite) */. void *
13c11 70 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 pSchema;
13c12 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 73 70 /* Pointer to sp
13c13 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 ace allocated by
13c14 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 sqlite3BtreeSch
13c15 65 6d 61 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 ema() */. void
13c16 28 2a 78 46 72 65 65 53 63 68 65 6d 61 29 28 76 (*xFreeSchema)(v
13c17 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 oid*); /* Destr
13c18 75 63 74 6f 72 20 66 6f 72 20 42 74 53 68 61 72 uctor for BtShar
13c19 65 64 2e 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 ed.pSchema */.
13c1a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
13c1b 75 74 65 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 utex; /* Non-rec
13c1c 75 72 73 69 76 65 20 6d 75 74 65 78 20 72 65 71 ursive mutex req
13c1d 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 73 20 uired to access
13c1e 74 68 69 73 20 73 74 72 75 63 74 20 2a 2f 0a 23 this struct */.#
13c1f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
13c20 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
13c21 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 int nRef;
13c22 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
13c23 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
13c24 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 to this structur
13c25 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 e */. BtShared
13c26 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 *pNext; /*
13c27 4e 65 78 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f Next on a list o
13c28 66 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 61 f sharable BtSha
13c29 72 65 64 20 73 74 72 75 63 74 73 20 2a 2f 0a 20 red structs */.
13c2a 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 BtLock *pLock;
13c2b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f /* List o
13c2c 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 f locks held on
13c2d 74 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 65 this shared-btre
13c2e 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 74 e struct */. Bt
13c2f 72 65 65 20 2a 70 45 78 63 6c 75 73 69 76 65 3b ree *pExclusive;
13c30 20 20 20 20 2f 2a 20 42 74 72 65 65 20 77 69 74 /* Btree wit
13c31 68 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c h an EXCLUSIVE l
13c32 6f 63 6b 20 6f 6e 20 74 68 65 20 77 68 6f 6c 65 ock on the whole
13c33 20 64 62 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 db */.#endif.
13c34 75 38 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 u8 *pTmpSpace;
13c35 20 20 20 20 20 20 2f 2a 20 42 74 53 68 61 72 65 /* BtShare
13c36 64 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 d.pageSize bytes
13c37 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d of space for tm
13c38 70 20 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a p use */.};../*.
13c39 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
13c3a 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
13c3b 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 structure is use
13c3c 64 20 74 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 6d d to hold inform
13c3d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 61 ation.** about a
13c3e 20 63 65 6c 6c 2e 20 20 54 68 65 20 70 61 72 73 cell. The pars
13c3f 65 43 65 6c 6c 50 74 72 28 29 20 66 75 6e 63 74 eCellPtr() funct
13c40 69 6f 6e 20 66 69 6c 6c 73 20 69 6e 20 74 68 69 ion fills in thi
13c41 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 62 s structure.** b
13c42 61 73 65 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 74 ased on informat
13c43 69 6f 6e 20 65 78 74 72 61 63 74 20 66 72 6f 6d ion extract from
13c44 20 74 68 65 20 72 61 77 20 64 69 73 6b 20 70 61 the raw disk pa
13c45 67 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ge..*/.typedef s
13c46 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 43 truct CellInfo C
13c47 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20 ellInfo;.struct
13c48 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20 20 75 38 20 CellInfo {. u8
13c49 2a 70 43 65 6c 6c 3b 20 20 20 20 20 2f 2a 20 50 *pCell; /* P
13c4a 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 74 ointer to the st
13c4b 61 72 74 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 art of cell cont
13c4c 65 6e 74 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 ent */. i64 nKe
13c4d 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6b y; /* The k
13c4e 65 79 20 66 6f 72 20 49 4e 54 4b 45 59 20 74 61 ey for INTKEY ta
13c4f 62 6c 65 73 2c 20 6f 72 20 6e 75 6d 62 65 72 20 bles, or number
13c50 6f 66 20 62 79 74 65 73 20 69 6e 20 6b 65 79 20 of bytes in key
13c51 2a 2f 0a 20 20 75 33 32 20 6e 44 61 74 61 3b 20 */. u32 nData;
13c52 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
13c53 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 2a bytes of data *
13c54 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 /. u32 nPayload
13c55 3b 20 20 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f 75 ; /* Total amou
13c56 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 2a 2f nt of payload */
13c57 0a 20 20 75 31 36 20 6e 48 65 61 64 65 72 3b 20 . u16 nHeader;
13c58 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
13c59 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 cell content he
13c5a 61 64 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f ader in bytes */
13c5b 0a 20 20 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 20 . u16 nLocal;
13c5c 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 70 /* Amount of p
13c5d 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 ayload held loca
13c5e 6c 6c 79 20 2a 2f 0a 20 20 75 31 36 20 69 4f 76 lly */. u16 iOv
13c5f 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 65 erflow; /* Offse
13c60 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 t to overflow pa
13c61 67 65 20 6e 75 6d 62 65 72 2e 20 20 5a 65 72 6f ge number. Zero
13c62 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 if no overflow
13c63 2a 2f 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b 20 */. u16 nSize;
13c64 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
13c65 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
13c66 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 72 on the main b-tr
13c67 65 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f ee page */.};../
13c68 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 *.** Maximum dep
13c69 74 68 20 6f 66 20 61 6e 20 53 51 4c 69 74 65 20 th of an SQLite
13c6a 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 65 B-Tree structure
13c6b 2e 20 41 6e 79 20 42 2d 54 72 65 65 20 64 65 65 . Any B-Tree dee
13c6c 70 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 69 73 per than.** this
13c6d 20 77 69 6c 6c 20 62 65 20 64 65 63 6c 61 72 65 will be declare
13c6e 64 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73 20 d corrupt. This
13c6f 76 61 6c 75 65 20 69 73 20 63 61 6c 63 75 6c 61 value is calcula
13c70 74 65 64 20 62 61 73 65 64 20 6f 6e 20 61 0a 2a ted based on a.*
13c71 2a 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61 * maximum databa
13c72 73 65 20 73 69 7a 65 20 6f 66 20 32 5e 33 31 20 se size of 2^31
13c73 70 61 67 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20 pages a minimum
13c74 66 61 6e 6f 75 74 20 6f 66 20 32 20 66 6f 72 20 fanout of 2 for
13c75 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20 61 a.** root-node a
13c76 6e 64 20 33 20 66 6f 72 20 61 6c 6c 20 6f 74 68 nd 3 for all oth
13c77 65 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 er internal node
13c78 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 s..**.** If a tr
13c79 65 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20 ee that appears
13c7a 74 6f 20 62 65 20 74 61 6c 6c 65 72 20 74 68 61 to be taller tha
13c7b 6e 20 74 68 69 73 20 69 73 20 65 6e 63 6f 75 6e n this is encoun
13c7c 74 65 72 65 64 2c 20 69 74 20 69 73 0a 2a 2a 20 tered, it is.**
13c7d 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 assumed that the
13c7e 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 database is cor
13c7f 72 75 70 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 rupt..*/.#define
13c80 20 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 BTCURSOR_MAX_DE
13c81 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41 20 PTH 20../*.** A
13c82 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e cursor is a poin
13c83 74 65 72 20 74 6f 20 61 20 70 61 72 74 69 63 75 ter to a particu
13c84 6c 61 72 20 65 6e 74 72 79 20 77 69 74 68 69 6e lar entry within
13c85 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
13c86 20 62 2d 74 72 65 65 20 77 69 74 68 69 6e 20 61 b-tree within a
13c87 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
13c88 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79 20 **.** The entry
13c89 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 is identified by
13c8a 20 69 74 73 20 4d 65 6d 50 61 67 65 20 61 6e 64 its MemPage and
13c8b 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a the index in.**
13c8c 20 4d 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b 5d MemPage.aCell[]
13c8d 20 6f 66 20 74 68 65 20 65 6e 74 72 79 2e 0a 2a of the entry..*
13c8e 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 69 6e 67 *.** When a sing
13c8f 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 le database file
13c90 20 63 61 6e 20 73 68 61 72 65 64 20 62 79 20 74 can shared by t
13c91 77 6f 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 wo more database
13c92 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a connections,.**
13c93 20 62 75 74 20 63 75 72 73 6f 72 73 20 63 61 6e but cursors can
13c94 6e 6f 74 20 62 65 20 73 68 61 72 65 64 2e 20 20 not be shared.
13c95 45 61 63 68 20 63 75 72 73 6f 72 20 69 73 20 61 Each cursor is a
13c96 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
13c97 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 64 .** particular d
13c98 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
13c99 6f 6e 20 69 64 65 6e 74 69 66 69 65 64 20 42 74 on identified Bt
13c9a 43 75 72 73 6f 72 2e 70 42 74 72 65 65 2e 64 62 Cursor.pBtree.db
13c9b 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 ..**.** Fields i
13c9c 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
13c9d 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e are accessed un
13c9e 64 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 der the BtShared
13c9f 2e 6d 75 74 65 78 0a 2a 2a 20 66 6f 75 6e 64 20 .mutex.** found
13ca0 61 74 20 73 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75 at self->pBt->mu
13ca1 74 65 78 2e 20 0a 2a 2f 0a 73 74 72 75 63 74 20 tex. .*/.struct
13ca2 42 74 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 72 BtCursor {. Btr
13ca3 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 ee *pBtree;
13ca4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 /* The Bt
13ca5 72 65 65 20 74 6f 20 77 68 69 63 68 20 74 68 69 ree to which thi
13ca6 73 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 s cursor belongs
13ca7 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a */. BtShared *
13ca8 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 pBt;
13ca9 2f 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 20 /* The BtShared
13caa 74 68 69 73 20 63 75 72 73 6f 72 20 70 6f 69 6e this cursor poin
13cab 74 73 20 74 6f 20 2a 2f 0a 20 20 42 74 43 75 72 ts to */. BtCur
13cac 73 6f 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 sor *pNext, *pPr
13cad 65 76 3b 20 20 2f 2a 20 46 6f 72 6d 73 20 61 20 ev; /* Forms a
13cae 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 linked list of a
13caf 6c 6c 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 ll cursors */.
13cb0 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a struct KeyInfo *
13cb1 70 4b 65 79 49 6e 66 6f 3b 20 2f 2a 20 41 72 67 pKeyInfo; /* Arg
13cb2 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 ument passed to
13cb3 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 comparison funct
13cb4 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 ion */. Pgno pg
13cb5 6e 6f 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20 noRoot;
13cb6 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 /* The root p
13cb7 61 67 65 20 6f 66 20 74 68 69 73 20 74 72 65 65 age of this tree
13cb8 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 */. CellInfo i
13cb9 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 nfo;
13cba 2f 2a 20 41 20 70 61 72 73 65 20 6f 66 20 74 68 /* A parse of th
13cbb 65 20 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f e cell we are po
13cbc 69 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 inting at */. u
13cbd 38 20 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20 8 wrFlag;
13cbe 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
13cbf 20 69 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a if writable */.
13cc0 20 20 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20 u8 atLast;
13cc1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
13cc2 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 ursor pointing t
13cc3 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 o the last entry
13cc4 20 2a 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b */. u8 validNK
13cc5 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ey;
13cc6 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e /* True if info.
13cc7 6e 4b 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f nKey is valid */
13cc8 0a 20 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 . u8 eState;
13cc9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13cca 4f 6e 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f One of the CURSO
13ccb 52 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 R_XXX constants
13ccc 28 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 (see below) */.
13ccd 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 void *pKey;
13cce 20 20 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74 /* Saved key t
13ccf 68 61 74 20 77 61 73 20 63 75 72 73 6f 72 27 73 hat was cursor's
13cd0 20 6c 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 last known posi
13cd1 74 69 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b tion */. i64 nK
13cd2 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 ey; /* Si
13cd3 7a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c ze of pKey, or l
13cd4 61 73 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 ast integer key
13cd5 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 3b 20 20 */. int skip;
13cd6 20 20 20 20 20 20 2f 2a 20 28 73 6b 69 70 3c 30 /* (skip<0
13cd7 29 20 2d 3e 20 50 72 65 76 28 29 20 69 73 20 61 ) -> Prev() is a
13cd8 20 6e 6f 2d 6f 70 2e 20 28 73 6b 69 70 3e 30 29 no-op. (skip>0)
13cd9 20 2d 3e 20 4e 65 78 74 28 29 20 69 73 20 2a 2f -> Next() is */
13cda 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
13cdb 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 OMIT_INCRBLOB.
13cdc 75 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e u8 isIncrblobHan
13cdd 64 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 dle; /* Tru
13cde 65 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72 e if this cursor
13cdf 20 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 is an incr. io
13ce0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f handle */. Pgno
13ce1 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 *aOverflow;
13ce2 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f /* Cache o
13ce3 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 f overflow page
13ce4 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e locations */.#en
13ce5 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 dif.#ifndef NDEB
13ce6 55 47 0a 20 20 75 38 20 70 61 67 65 73 53 68 75 UG. u8 pagesShu
13ce7 66 66 6c 65 64 3b 20 20 20 20 20 20 20 20 20 2f ffled; /
13ce8 2a 20 54 72 75 65 20 69 66 20 42 74 72 65 65 20 * True if Btree
13ce9 70 61 67 65 73 20 61 72 65 20 72 65 61 72 72 61 pages are rearra
13cea 6e 67 65 64 20 62 79 20 62 61 6c 61 6e 63 65 28 nged by balance(
13ceb 29 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 31 36 )*/.#endif. i16
13cec 20 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 iPage;
13ced 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13cee 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 /* Index of c
13cef 75 72 72 65 6e 74 20 70 61 67 65 20 69 6e 20 61 urrent page in a
13cf0 70 50 61 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 pPage */. MemPa
13cf1 67 65 20 2a 61 70 50 61 67 65 5b 42 54 43 55 52 ge *apPage[BTCUR
13cf2 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 SOR_MAX_DEPTH];
13cf3 20 2f 2a 20 50 61 67 65 73 20 66 72 6f 6d 20 72 /* Pages from r
13cf4 6f 6f 74 20 74 6f 20 63 75 72 72 65 6e 74 20 70 oot to current p
13cf5 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 61 69 49 age */. u16 aiI
13cf6 64 78 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f dx[BTCURSOR_MAX_
13cf7 44 45 50 54 48 5d 3b 20 20 20 20 20 20 20 20 2f DEPTH]; /
13cf8 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 65 78 20 * Current index
13cf9 69 6e 20 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a in apPage[i] */.
13cfa 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 };../*.** Potent
13cfb 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 42 ial values for B
13cfc 74 43 75 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a tCursor.eState..
13cfd 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c **.** CURSOR_VAL
13cfe 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 ID:.** Cursor
13cff 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 6c 69 points to a vali
13d00 64 20 65 6e 74 72 79 2e 20 67 65 74 50 61 79 6c d entry. getPayl
13d01 6f 61 64 28 29 20 65 74 63 2e 20 6d 61 79 20 62 oad() etc. may b
13d02 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 e called..**.**
13d03 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a CURSOR_INVALID:.
13d04 2a 2a 20 20 20 43 75 72 73 6f 72 20 64 6f 65 73 ** Cursor does
13d05 20 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 not point to a
13d06 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 54 68 69 valid entry. Thi
13d07 73 20 63 61 6e 20 68 61 70 70 65 6e 20 28 66 6f s can happen (fo
13d08 72 20 65 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20 r example) .**
13d09 20 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62 because the tab
13d0a 6c 65 20 69 73 20 65 6d 70 74 79 20 6f 72 20 62 le is empty or b
13d0b 65 63 61 75 73 65 20 42 74 72 65 65 43 75 72 73 ecause BtreeCurs
13d0c 6f 72 46 69 72 73 74 28 29 20 68 61 73 20 6e 6f orFirst() has no
13d0d 74 20 62 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c t been.** call
13d0e 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 ed..**.** CURSOR
13d0f 5f 52 45 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a _REQUIRESEEK:.**
13d10 20 20 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 The table tha
13d11 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 77 61 t this cursor wa
13d12 73 20 6f 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c s opened on stil
13d13 6c 20 65 78 69 73 74 73 2c 20 62 75 74 20 68 61 l exists, but ha
13d14 73 20 62 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64 s been .** mod
13d15 69 66 69 65 64 20 73 69 6e 63 65 20 74 68 65 20 ified since the
13d16 63 75 72 73 6f 72 20 77 61 73 20 6c 61 73 74 20 cursor was last
13d17 75 73 65 64 2e 20 54 68 65 20 63 75 72 73 6f 72 used. The cursor
13d18 20 70 6f 73 69 74 69 6f 6e 20 69 73 20 73 61 76 position is sav
13d19 65 64 0a 2a 2a 20 20 20 69 6e 20 76 61 72 69 61 ed.** in varia
13d1a 62 6c 65 73 20 42 74 43 75 72 73 6f 72 2e 70 4b bles BtCursor.pK
13d1b 65 79 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e ey and BtCursor.
13d1c 6e 4b 65 79 2e 20 57 68 65 6e 20 61 20 63 75 72 nKey. When a cur
13d1d 73 6f 72 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20 sor is in .**
13d1e 74 68 69 73 20 73 74 61 74 65 2c 20 72 65 73 74 this state, rest
13d1f 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
13d20 6e 28 29 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 n() can be calle
13d21 64 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 0a d to attempt to.
13d22 2a 2a 20 20 20 73 65 65 6b 20 74 68 65 20 63 75 ** seek the cu
13d23 72 73 6f 72 20 74 6f 20 74 68 65 20 73 61 76 65 rsor to the save
13d24 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a d position..**.*
13d25 2a 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3a 0a * CURSOR_FAULT:.
13d26 2a 2a 20 20 20 41 20 75 6e 72 65 63 6f 76 65 72 ** A unrecover
13d27 61 62 6c 65 20 65 72 72 6f 72 20 28 61 6e 20 49 able error (an I
13d28 2f 4f 20 65 72 72 6f 72 20 6f 72 20 61 20 6d 61 /O error or a ma
13d29 6c 6c 6f 63 20 66 61 69 6c 75 72 65 29 20 68 61 lloc failure) ha
13d2a 73 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 20 20 s occurred.**
13d2b 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 63 on a different c
13d2c 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 73 onnection that s
13d2d 68 61 72 65 73 20 74 68 65 20 42 74 53 68 61 72 hares the BtShar
13d2e 65 64 20 63 61 63 68 65 20 77 69 74 68 20 74 68 ed cache with th
13d2f 69 73 0a 2a 2a 20 20 20 63 75 72 73 6f 72 2e 20 is.** cursor.
13d30 20 54 68 65 20 65 72 72 6f 72 20 68 61 73 20 6c The error has l
13d31 65 66 74 20 74 68 65 20 63 61 63 68 65 20 69 6e eft the cache in
13d32 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 an inconsistent
13d33 20 73 74 61 74 65 2e 0a 2a 2a 20 20 20 44 6f 20 state..** Do
13d34 6e 6f 74 68 69 6e 67 20 65 6c 73 65 20 77 69 74 nothing else wit
13d35 68 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 20 h this cursor.
13d36 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 Any attempt to u
13d37 73 65 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a se the cursor.**
13d38 20 20 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e should return
13d39 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
13d3a 73 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72 73 stored in BtCurs
13d3b 6f 72 2e 73 6b 69 70 0a 2a 2f 0a 23 64 65 66 69 or.skip.*/.#defi
13d3c 6e 65 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 ne CURSOR_INVALI
13d3d 44 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 D 0.#d
13d3e 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c efine CURSOR_VAL
13d3f 49 44 20 20 20 20 20 20 20 20 20 20 20 20 20 31 ID 1
13d40 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f .#define CURSOR_
13d41 52 45 51 55 49 52 45 53 45 45 4b 20 20 20 20 20 REQUIRESEEK
13d42 20 20 32 0a 23 64 65 66 69 6e 65 20 43 55 52 53 2.#define CURS
13d43 4f 52 5f 46 41 55 4c 54 20 20 20 20 20 20 20 20 OR_FAULT
13d44 20 20 20 20 20 33 0a 0a 2f 2a 20 54 68 65 20 64 3../* The d
13d45 61 74 61 62 61 73 65 20 70 61 67 65 20 74 68 65 atabase page the
13d46 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6f 63 PENDING_BYTE oc
13d47 63 75 70 69 65 73 2e 20 54 68 69 73 20 70 61 67 cupies. This pag
13d48 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e e is never used.
13d49 0a 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 6d .** TODO: This m
13d4a 61 63 72 6f 20 69 73 20 76 65 72 79 20 73 69 6d acro is very sim
13d4b 69 6c 61 72 79 20 74 6f 20 50 41 47 45 52 5f 4d ilary to PAGER_M
13d4c 4a 5f 50 47 4e 4f 28 29 20 69 6e 20 70 61 67 65 J_PGNO() in page
13d4d 72 2e 63 2e 20 54 68 65 79 0a 2a 2a 20 73 68 6f r.c. They.** sho
13d4e 75 6c 64 20 70 6f 73 73 69 62 6c 79 20 62 65 20 uld possibly be
13d4f 63 6f 6e 73 6f 6c 69 64 61 74 65 64 20 28 70 72 consolidated (pr
13d50 65 73 75 6d 61 62 6c 79 20 69 6e 20 70 61 67 65 esumably in page
13d51 72 2e 68 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 r.h)..**.** If d
13d52 69 73 6b 20 49 2f 4f 20 69 73 20 6f 6d 69 74 74 isk I/O is omitt
13d53 65 64 20 28 6d 65 61 6e 69 6e 67 20 74 68 61 74 ed (meaning that
13d54 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
13d55 20 73 74 6f 72 65 64 20 70 75 72 65 6c 79 0a 2a stored purely.*
13d56 2a 20 69 6e 20 6d 65 6d 6f 72 79 29 20 74 68 65 * in memory) the
13d57 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 65 n there is no pe
13d58 6e 64 69 6e 67 20 62 79 74 65 2e 0a 2a 2f 0a 23 nding byte..*/.#
13d59 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
13d5a 54 5f 44 49 53 4b 49 4f 0a 23 20 64 65 66 69 6e T_DISKIO.# defin
13d5b 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 e PENDING_BYTE_P
13d5c 41 47 45 28 70 42 74 29 20 20 30 78 37 66 66 66 AGE(pBt) 0x7fff
13d5d 66 66 66 66 0a 23 65 6c 73 65 0a 23 20 64 65 66 ffff.#else.# def
13d5e 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 ine PENDING_BYTE
13d5f 5f 50 41 47 45 28 70 42 74 29 20 28 28 50 67 6e _PAGE(pBt) ((Pgn
13d60 6f 29 28 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 o)((PENDING_BYTE
13d61 2f 28 70 42 74 29 2d 3e 70 61 67 65 53 69 7a 65 /(pBt)->pageSize
13d62 29 2b 31 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a )+1)).#endif../*
13d63 0a 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c 69 73 .** A linked lis
13d64 74 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 t of the followi
13d65 6e 67 20 73 74 72 75 63 74 75 72 65 73 20 69 73 ng structures is
13d66 20 73 74 6f 72 65 64 20 61 74 20 42 74 53 68 61 stored at BtSha
13d67 72 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f red.pLock..** Lo
13d68 63 6b 73 20 61 72 65 20 61 64 64 65 64 20 28 6f cks are added (o
13d69 72 20 75 70 67 72 61 64 65 64 20 66 72 6f 6d 20 r upgraded from
13d6a 52 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 49 READ_LOCK to WRI
13d6b 54 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 61 20 TE_LOCK) when a
13d6c 63 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 6f 70 cursor .** is op
13d6d 65 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 62 6c ened on the tabl
13d6e 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 e with root page
13d6f 20 42 74 53 68 61 72 65 64 2e 69 54 61 62 6c 65 BtShared.iTable
13d70 2e 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 6d 6f . Locks are remo
13d71 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 ved.** from this
13d72 20 6c 69 73 74 20 77 68 65 6e 20 61 20 74 72 61 list when a tra
13d73 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d nsaction is comm
13d74 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 itted or rolled
13d75 62 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a 2a back, or when.**
13d76 20 61 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 a btree handle
13d77 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 is closed..*/.st
13d78 72 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 20 ruct BtLock {.
13d79 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20 20 Btree *pBtree;
13d7a 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 68 /* Btree h
13d7b 61 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 andle holding th
13d7c 69 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e is lock */. Pgn
13d7d 6f 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 o iTable;
13d7e 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 /* Root page
13d7f 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 of table */. u8
13d80 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 eLock;
13d81 20 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 4b /* READ_LOCK
13d82 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20 2a or WRITE_LOCK *
13d83 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 78 /. BtLock *pNex
13d84 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 t; /* Nex
13d85 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 4c t in BtShared.pL
13d86 6f 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a ock list */.};..
13d87 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c /* Candidate val
13d88 75 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b 2e 65 ues for BtLock.e
13d89 4c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 Lock */.#define
13d8a 52 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a READ_LOCK 1.
13d8b 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 4c 4f #define WRITE_LO
13d8c 43 4b 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 54 CK 2../*.** T
13d8d 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65 66 69 hese macros defi
13d8e 6e 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 ne the location
13d8f 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d of the pointer-m
13d90 61 70 20 65 6e 74 72 79 20 66 6f 72 20 61 20 0a ap entry for a .
13d91 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ** database page
13d92 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 . The first argu
13d93 6d 65 6e 74 20 74 6f 20 65 61 63 68 20 69 73 20 ment to each is
13d94 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 the number of us
13d95 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73 20 6f 6e able.** bytes on
13d96 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 each page of th
13d97 65 20 64 61 74 61 62 61 73 65 20 28 6f 66 74 65 e database (ofte
13d98 6e 20 31 30 32 34 29 2e 20 54 68 65 20 73 65 63 n 1024). The sec
13d99 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 ond is the.** pa
13d9a 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 6f ge number to loo
13d9b 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f 69 6e k up in the poin
13d9c 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 ter map..**.** P
13d9d 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72 65 74 TRMAP_PAGENO ret
13d9e 75 72 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 urns the databas
13d9f 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 e page number of
13da0 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
13da1 0a 2a 2a 20 70 61 67 65 20 74 68 61 74 20 73 74 .** page that st
13da2 6f 72 65 73 20 74 68 65 20 72 65 71 75 69 72 65 ores the require
13da3 64 20 70 6f 69 6e 74 65 72 2e 20 50 54 52 4d 41 d pointer. PTRMA
13da4 50 5f 50 54 52 4f 46 46 53 45 54 20 72 65 74 75 P_PTROFFSET retu
13da5 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 rns.** the offse
13da6 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 t of the request
13da7 65 64 20 6d 61 70 20 65 6e 74 72 79 2e 0a 2a 2a ed map entry..**
13da8 0a 2a 2a 20 49 66 20 74 68 65 20 70 67 6e 6f 20 .** If the pgno
13da9 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 argument passed
13daa 74 6f 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f to PTRMAP_PAGENO
13dab 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 is a pointer-ma
13dac 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 p page,.** then
13dad 70 67 6e 6f 20 69 73 20 72 65 74 75 72 6e 65 64 pgno is returned
13dae 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50 54 52 4d . So (pgno==PTRM
13daf 41 50 5f 50 41 47 45 4e 4f 28 70 67 73 7a 2c 20 AP_PAGENO(pgsz,
13db0 70 67 6e 6f 29 29 20 63 61 6e 20 62 65 0a 2a 2a pgno)) can be.**
13db1 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 69 66 used to test if
13db2 20 70 67 6e 6f 20 69 73 20 61 20 70 6f 69 6e 74 pgno is a point
13db3 65 72 2d 6d 61 70 20 70 61 67 65 2e 20 50 54 52 er-map page. PTR
13db4 4d 41 50 5f 49 53 50 41 47 45 20 69 6d 70 6c 65 MAP_ISPAGE imple
13db5 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73 20 74 65 ments.** this te
13db6 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 st..*/.#define P
13db7 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 TRMAP_PAGENO(pBt
13db8 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61 70 50 61 , pgno) ptrmapPa
13db9 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e 6f 29 0a geno(pBt, pgno).
13dba 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 #define PTRMAP_P
13dbb 54 52 4f 46 46 53 45 54 28 70 67 70 74 72 6d 61 TROFFSET(pgptrma
13dbc 70 2c 20 70 67 6e 6f 29 20 28 35 2a 28 70 67 6e p, pgno) (5*(pgn
13dbd 6f 2d 70 67 70 74 72 6d 61 70 2d 31 29 29 0a 23 o-pgptrmap-1)).#
13dbe 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 49 53 define PTRMAP_IS
13dbf 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f 29 20 PAGE(pBt, pgno)
13dc0 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 28 (PTRMAP_PAGENO((
13dc1 70 42 74 29 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 pBt),(pgno))==(p
13dc2 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 gno))../*.** The
13dc3 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 pointer map is
13dc4 61 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 74 a lookup table t
13dc5 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 hat identifies t
13dc6 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 66 he parent page f
13dc7 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 or.** each child
13dc8 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 page in the dat
13dc9 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 abase file. The
13dca 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 parent page is
13dcb 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a the page that.**
13dcc 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e contains a poin
13dcd 74 65 72 20 74 6f 20 74 68 65 20 63 68 69 6c 64 ter to the child
13dce 2e 20 20 45 76 65 72 79 20 70 61 67 65 20 69 6e . Every page in
13dcf 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
13dd0 6e 74 61 69 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 ntains.** 0 or 1
13dd1 20 70 61 72 65 6e 74 20 70 61 67 65 73 2e 20 20 parent pages.
13dd2 28 49 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74 (In this context
13dd3 20 27 64 61 74 61 62 61 73 65 20 70 61 67 65 27 'database page'
13dd4 20 72 65 66 65 72 73 0a 2a 2a 20 74 6f 20 61 6e refers.** to an
13dd5 79 20 70 61 67 65 20 74 68 61 74 20 69 73 20 6e y page that is n
13dd6 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 ot part of the p
13dd7 6f 69 6e 74 65 72 20 6d 61 70 20 69 74 73 65 6c ointer map itsel
13dd8 66 2e 29 20 20 45 61 63 68 20 70 6f 69 6e 74 65 f.) Each pointe
13dd9 72 20 6d 61 70 0a 2a 2a 20 65 6e 74 72 79 20 63 r map.** entry c
13dda 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 69 6e onsists of a sin
13ddb 67 6c 65 20 62 79 74 65 20 27 74 79 70 65 27 20 gle byte 'type'
13ddc 61 6e 64 20 61 20 34 20 62 79 74 65 20 70 61 72 and a 4 byte par
13ddd 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e ent page number.
13dde 0a 2a 2a 20 54 68 65 20 50 54 52 4d 41 50 5f 58 .** The PTRMAP_X
13ddf 58 58 20 69 64 65 6e 74 69 66 69 65 72 73 20 62 XX identifiers b
13de0 65 6c 6f 77 20 61 72 65 20 74 68 65 20 76 61 6c elow are the val
13de1 69 64 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 id types..**.**
13de2 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 The purpose of t
13de3 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 he pointer map i
13de4 73 20 74 6f 20 66 61 63 69 6c 69 74 79 20 6d 6f s to facility mo
13de5 76 69 6e 67 20 70 61 67 65 73 20 66 72 6f 6d 20 ving pages from
13de6 6f 6e 65 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 one.** position
13de7 69 6e 20 74 68 65 20 66 69 6c 65 20 74 6f 20 61 in the file to a
13de8 6e 6f 74 68 65 72 20 61 73 20 70 61 72 74 20 6f nother as part o
13de9 66 20 61 75 74 6f 76 61 63 75 75 6d 2e 20 20 57 f autovacuum. W
13dea 68 65 6e 20 61 20 70 61 67 65 0a 2a 2a 20 69 73 hen a page.** is
13deb 20 6d 6f 76 65 64 2c 20 74 68 65 20 70 6f 69 6e moved, the poin
13dec 74 65 72 20 69 6e 20 69 74 73 20 70 61 72 65 6e ter in its paren
13ded 74 20 6d 75 73 74 20 62 65 20 75 70 64 61 74 65 t must be update
13dee 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 d to point to th
13def 65 0a 2a 2a 20 6e 65 77 20 6c 6f 63 61 74 69 6f e.** new locatio
13df0 6e 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 n. The pointer
13df1 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f 20 6c map is used to l
13df2 6f 63 61 74 65 20 74 68 65 20 70 61 72 65 6e 74 ocate the parent
13df3 20 70 61 67 65 20 71 75 69 63 6b 6c 79 2e 0a 2a page quickly..*
13df4 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 52 4f 4f 54 *.** PTRMAP_ROOT
13df5 50 41 47 45 3a 20 54 68 65 20 64 61 74 61 62 61 PAGE: The databa
13df6 73 65 20 70 61 67 65 20 69 73 20 61 20 72 6f 6f se page is a roo
13df7 74 2d 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 t-page. The page
13df8 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 0a 2a -number is not.*
13df9 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
13dfa 20 20 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 used in this
13dfb 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d case..**.** PTRM
13dfc 41 50 5f 46 52 45 45 50 41 47 45 3a 20 54 68 65 AP_FREEPAGE: The
13dfd 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 database page i
13dfe 73 20 61 6e 20 75 6e 75 73 65 64 20 28 66 72 65 s an unused (fre
13dff 65 29 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 e) page. The pag
13e00 65 2d 6e 75 6d 62 65 72 20 0a 2a 2a 20 20 20 20 e-number .**
13e01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 is
13e02 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 74 68 69 not used in thi
13e03 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 s case..**.** PT
13e04 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 RMAP_OVERFLOW1:
13e05 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 The database pag
13e06 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70 e is the first p
13e07 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 age in a list of
13e08 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 .**
13e09 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 overflow
13e0a 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 65 20 pages. The page
13e0b 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65 number identifie
13e0c 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a s the page that.
13e0d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
13e0e 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 74 68 contains th
13e0f 65 20 63 65 6c 6c 20 77 69 74 68 20 61 20 70 6f e cell with a po
13e10 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 6f 76 inter to this ov
13e11 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a erflow page..**.
13e12 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c ** PTRMAP_OVERFL
13e13 4f 57 32 3a 20 54 68 65 20 64 61 74 61 62 61 73 OW2: The databas
13e14 65 20 70 61 67 65 20 69 73 20 74 68 65 20 73 65 e page is the se
13e15 63 6f 6e 64 20 6f 72 20 6c 61 74 65 72 20 70 61 cond or later pa
13e16 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 0a ge in a list of.
13e17 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
13e18 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 overflow pa
13e19 67 65 73 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 ges. The page-nu
13e1a 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 mber identifies
13e1b 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 the previous.**
13e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13e1d 20 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 page in the ov
13e1e 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 erflow page list
13e1f 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 ..**.** PTRMAP_B
13e20 54 52 45 45 3a 20 54 68 65 20 64 61 74 61 62 61 TREE: The databa
13e21 73 65 20 70 61 67 65 20 69 73 20 61 20 6e 6f 6e se page is a non
13e22 2d 72 6f 6f 74 20 62 74 72 65 65 20 70 61 67 65 -root btree page
13e23 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 . The page numbe
13e24 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 r.**
13e25 20 20 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 identifies th
13e26 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 6e e parent page in
13e27 20 74 68 65 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 the btree..*/.#
13e28 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 52 4f define PTRMAP_RO
13e29 4f 54 50 41 47 45 20 31 0a 23 64 65 66 69 6e 65 OTPAGE 1.#define
13e2a 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 PTRMAP_FREEPAGE
13e2b 20 32 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 2.#define PTRMA
13e2c 50 5f 4f 56 45 52 46 4c 4f 57 31 20 33 0a 23 64 P_OVERFLOW1 3.#d
13e2d 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 efine PTRMAP_OVE
13e2e 52 46 4c 4f 57 32 20 34 0a 23 64 65 66 69 6e 65 RFLOW2 4.#define
13e2f 20 50 54 52 4d 41 50 5f 42 54 52 45 45 20 35 0a PTRMAP_BTREE 5.
13e30 0a 2f 2a 20 41 20 62 75 6e 63 68 20 6f 66 20 61 ./* A bunch of a
13e31 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
13e32 74 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 ts to check the
13e33 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 transaction stat
13e34 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f e variables.** o
13e35 66 20 68 61 6e 64 6c 65 20 70 20 28 74 79 70 65 f handle p (type
13e36 20 42 74 72 65 65 2a 29 20 61 72 65 20 69 6e 74 Btree*) are int
13e37 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65 ernally consiste
13e38 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 62 nt..*/.#define b
13e39 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 treeIntegrity(p)
13e3a 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e \. assert( p->
13e3b 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
13e3c 6f 6e 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c on!=TRANS_NONE |
13e3d 7c 20 70 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 | p->pBt->nTrans
13e3e 61 63 74 69 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 action==0 ); \.
13e3f 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d assert( p->pBt-
13e40 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 3d >inTransaction>=
13e41 70 2d 3e 69 6e 54 72 61 6e 73 20 29 3b 20 0a 0a p->inTrans ); ..
13e42 0a 2f 2a 0a 2a 2a 20 54 68 65 20 49 53 41 55 54 ./*.** The ISAUT
13e43 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 20 69 73 OVACUUM macro is
13e44 20 75 73 65 64 20 77 69 74 68 69 6e 20 62 61 6c used within bal
13e45 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 ance_nonroot() t
13e46 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 o determine.** i
13e47 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 f the database s
13e48 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 upports auto-vac
13e49 75 75 6d 20 6f 72 20 6e 6f 74 2e 20 42 65 63 61 uum or not. Beca
13e4a 75 73 65 20 69 74 20 69 73 20 75 73 65 64 0a 2a use it is used.*
13e4b 2a 20 77 69 74 68 69 6e 20 61 6e 20 65 78 70 72 * within an expr
13e4c 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 ession that is a
13e4d 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 6e n argument to an
13e4e 6f 74 68 65 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 other macro .**
13e4f 28 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 (sqliteMallocRaw
13e50 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 ), it is not pos
13e51 73 69 62 6c 65 20 74 6f 20 75 73 65 20 63 6f 6e sible to use con
13e52 64 69 74 69 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 ditional compila
13e53 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 tion..** So, thi
13e54 73 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e s macro is defin
13e55 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 ed instead..*/.#
13e56 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
13e57 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 23 64 IT_AUTOVACUUM.#d
13e58 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55 efine ISAUTOVACU
13e59 55 4d 20 28 70 42 74 2d 3e 61 75 74 6f 56 61 63 UM (pBt->autoVac
13e5a 75 75 6d 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 uum).#else.#defi
13e5b 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 ne ISAUTOVACUUM
13e5c 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
13e5d 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
13e5e 69 73 20 70 61 73 73 65 64 20 61 72 6f 75 6e 64 is passed around
13e5f 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 through all the
13e60 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 sanity checking
13e61 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 routines.** in
13e62 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 72 order to keep tr
13e63 61 63 6b 20 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 ack of some glob
13e64 61 6c 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 al state informa
13e65 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 tion..*/.typedef
13e66 20 73 74 72 75 63 74 20 49 6e 74 65 67 72 69 74 struct Integrit
13e67 79 43 6b 20 49 6e 74 65 67 72 69 74 79 43 6b 3b yCk IntegrityCk;
13e68 0a 73 74 72 75 63 74 20 49 6e 74 65 67 72 69 74 .struct Integrit
13e69 79 43 6b 20 7b 0a 20 20 42 74 53 68 61 72 65 64 yCk {. BtShared
13e6a 20 2a 70 42 74 3b 20 20 20 20 2f 2a 20 54 68 65 *pBt; /* The
13e6b 20 74 72 65 65 20 62 65 69 6e 67 20 63 68 65 63 tree being chec
13e6c 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 50 61 67 ked out */. Pag
13e6d 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 2f er *pPager; /
13e6e 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 * The associated
13e6f 20 70 61 67 65 72 2e 20 20 41 6c 73 6f 20 61 63 pager. Also ac
13e70 63 65 73 73 69 62 6c 65 20 62 79 20 70 42 74 2d cessible by pBt-
13e71 3e 70 50 61 67 65 72 20 2a 2f 0a 20 20 50 67 6e >pPager */. Pgn
13e72 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 2f o nPage; /
13e73 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
13e74 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 s in the databas
13e75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 6e 52 65 e */. int *anRe
13e76 66 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 f; /* Numb
13e77 65 72 20 6f 66 20 74 69 6d 65 73 20 65 61 63 68 er of times each
13e78 20 70 61 67 65 20 69 73 20 72 65 66 65 72 65 6e page is referen
13e79 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 ced */. int mxE
13e7a 72 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 74 rr; /* St
13e7b 6f 70 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 op accumulating
13e7c 65 72 72 6f 72 73 20 77 68 65 6e 20 74 68 69 73 errors when this
13e7d 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 2a 2f reaches zero */
13e7e 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 . int nErr;
13e7f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
13e80 66 20 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 f messages writt
13e81 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f en to zErrMsg so
13e82 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 far */. int ma
13e83 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 2f 2a 20 41 llocFailed; /* A
13e84 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
13e85 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 on error has occ
13e86 75 72 72 65 64 20 2a 2f 0a 20 20 53 74 72 41 63 urred */. StrAc
13e87 63 75 6d 20 65 72 72 4d 73 67 3b 20 20 2f 2a 20 cum errMsg; /*
13e88 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 65 Accumulate the e
13e89 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 rror message tex
13e8a 74 20 68 65 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a t here */.};../*
13e8b 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 69 74 .** Read or writ
13e8c 65 20 61 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75 e a two- and fou
13e8d 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 r-byte big-endia
13e8e 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 n integer values
13e8f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 ..*/.#define get
13e90 32 62 79 74 65 28 78 29 20 20 20 28 28 78 29 5b 2byte(x) ((x)[
13e91 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a 0]<<8 | (x)[1]).
13e92 23 64 65 66 69 6e 65 20 70 75 74 32 62 79 74 65 #define put2byte
13e93 28 70 2c 76 29 20 28 28 70 29 5b 30 5d 20 3d 20 (p,v) ((p)[0] =
13e94 28 75 38 29 28 28 76 29 3e 3e 38 29 2c 20 28 70 (u8)((v)>>8), (p
13e95 29 5b 31 5d 20 3d 20 28 75 38 29 28 76 29 29 0a )[1] = (u8)(v)).
13e96 23 64 65 66 69 6e 65 20 67 65 74 34 62 79 74 65 #define get4byte
13e97 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 sqlite3Get4byte
13e98 0a 23 64 65 66 69 6e 65 20 70 75 74 34 62 79 74 .#define put4byt
13e99 65 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 e sqlite3Put4byt
13e9a 65 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 e../*.** Interna
13e9b 6c 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 l routines that
13e9c 73 68 6f 75 6c 64 20 62 65 20 61 63 63 65 73 73 should be access
13e9d 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20 ed by the btree
13e9e 6c 61 79 65 72 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 layer only..*/.S
13e9f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
13ea0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 t sqlite3BtreeGe
13ea1 74 50 61 67 65 28 42 74 53 68 61 72 65 64 2a 2c tPage(BtShared*,
13ea2 20 50 67 6e 6f 2c 20 4d 65 6d 50 61 67 65 2a 2a Pgno, MemPage**
13ea3 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
13ea4 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
13ea5 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 e3BtreeInitPage(
13ea6 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b MemPage *pPage);
13ea7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13ea8 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
13ea9 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 4d 65 eParseCellPtr(Me
13eaa 6d 50 61 67 65 2a 2c 20 75 38 2a 2c 20 43 65 6c mPage*, u8*, Cel
13eab 6c 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f lInfo*);.SQLITE_
13eac 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
13ead 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
13eae 6c 6c 28 4d 65 6d 50 61 67 65 2a 2c 20 69 6e 74 ll(MemPage*, int
13eaf 2c 20 43 65 6c 6c 49 6e 66 6f 2a 29 3b 0a 53 51 , CellInfo*);.SQ
13eb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
13eb1 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 73 sqlite3BtreeRes
13eb2 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
13eb3 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 on(BtCursor *pCu
13eb4 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 r);.SQLITE_PRIVA
13eb5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
13eb6 74 72 65 65 47 65 74 54 65 6d 70 43 75 72 73 6f treeGetTempCurso
13eb7 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 r(BtCursor *pCur
13eb8 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 54 65 6d , BtCursor *pTem
13eb9 70 43 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pCur);.SQLITE_PR
13eba 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
13ebb 65 33 42 74 72 65 65 52 65 6c 65 61 73 65 54 65 e3BtreeReleaseTe
13ebc 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f mpCursor(BtCurso
13ebd 72 20 2a 70 43 75 72 29 3b 0a 53 51 4c 49 54 45 r *pCur);.SQLITE
13ebe 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
13ebf 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 54 6f lite3BtreeMoveTo
13ec0 50 61 72 65 6e 74 28 42 74 43 75 72 73 6f 72 20 Parent(BtCursor
13ec1 2a 70 43 75 72 29 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a *pCur);../******
13ec2 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
13ec3 62 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a btreeInt.h *****
13ec4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13ec5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13ec6 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
13ec7 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
13ec8 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
13ec9 74 20 6f 66 66 20 69 6e 20 62 74 6d 75 74 65 78 t off in btmutex
13eca 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
13ecb 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66 20 53 51 4c *******/.#if SQL
13ecc 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 ITE_THREADSAFE &
13ecd 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
13ece 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
13ecf 43 48 45 29 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 CHE).../*.** Ent
13ed0 65 72 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 er a mutex on th
13ed1 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 6f 62 e given BTree ob
13ed2 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ject..**.** If t
13ed3 68 65 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 he object is not
13ed4 20 73 68 61 72 61 62 6c 65 2c 20 74 68 65 6e 20 sharable, then
13ed5 6e 6f 20 6d 75 74 65 78 20 69 73 20 65 76 65 72 no mutex is ever
13ed6 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 61 6e 64 required.** and
13ed7 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
13ed8 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 20 75 a no-op. The u
13ed9 6e 64 65 72 6c 79 69 6e 67 20 6d 75 74 65 78 20 nderlying mutex
13eda 69 73 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 is non-recursive
13edb 2e 0a 2a 2a 20 42 75 74 20 77 65 20 6b 65 65 70 ..** But we keep
13edc 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 a reference cou
13edd 6e 74 20 69 6e 20 42 74 72 65 65 2e 77 61 6e 74 nt in Btree.want
13ede 54 6f 4c 6f 63 6b 20 73 6f 20 74 68 65 20 62 65 ToLock so the be
13edf 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 havior.** of thi
13ee0 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 s interface is r
13ee1 65 63 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 ecursive..**.**
13ee2 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 To avoid deadloc
13ee3 6b 73 2c 20 6d 75 6c 74 69 70 6c 65 20 42 74 72 ks, multiple Btr
13ee4 65 65 73 20 61 72 65 20 6c 6f 63 6b 65 64 20 69 ees are locked i
13ee5 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 n the same order
13ee6 0a 2a 2a 20 62 79 20 61 6c 6c 20 64 61 74 61 62 .** by all datab
13ee7 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ase connections.
13ee8 20 20 54 68 65 20 70 2d 3e 70 4e 65 78 74 20 69 The p->pNext i
13ee9 73 20 61 20 6c 69 73 74 20 6f 66 20 6f 74 68 65 s a list of othe
13eea 72 0a 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f r.** Btrees belo
13eeb 6e 67 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d nging to the sam
13eec 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
13eed 63 74 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42 ction as the p B
13eee 74 72 65 65 0a 2a 2a 20 77 68 69 63 68 20 6e 65 tree.** which ne
13eef 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 ed to be locked
13ef0 61 66 74 65 72 20 70 2e 20 20 49 66 20 77 65 20 after p. If we
13ef1 63 61 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63 cannot get a loc
13ef2 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 k on.** p, then
13ef3 66 69 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c first unlock all
13ef4 20 6f 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f of the others o
13ef5 6e 20 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e n p->pNext, then
13ef6 20 77 61 69 74 0a 2a 2a 20 66 6f 72 20 74 68 65 wait.** for the
13ef7 20 6c 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65 20 lock to become
13ef8 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 available on p,
13ef9 74 68 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 then relock all
13efa 6f 66 20 74 68 65 0a 2a 2a 20 73 75 62 73 65 71 of the.** subseq
13efb 75 65 6e 74 20 42 74 72 65 65 73 20 74 68 61 74 uent Btrees that
13efc 20 64 65 73 69 72 65 20 61 20 6c 6f 63 6b 2e 0a desire a lock..
13efd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
13efe 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
13eff 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a reeEnter(Btree *
13f00 70 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 4c 61 p){. Btree *pLa
13f01 74 65 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 ter;.. /* Some
13f02 62 61 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 basic sanity che
13f03 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 42 74 72 cking on the Btr
13f04 65 65 2e 20 20 54 68 65 20 6c 69 73 74 20 6f 66 ee. The list of
13f05 20 42 74 72 65 65 73 0a 20 20 2a 2a 20 63 6f 6e Btrees. ** con
13f06 6e 65 63 74 65 64 20 62 79 20 70 4e 65 78 74 20 nected by pNext
13f07 61 6e 64 20 70 50 72 65 76 20 73 68 6f 75 6c 64 and pPrev should
13f08 20 62 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 be in sorted or
13f09 64 65 72 20 62 79 0a 20 20 2a 2a 20 42 74 72 65 der by. ** Btre
13f0a 65 2e 70 42 74 20 76 61 6c 75 65 2e 20 41 6c 6c e.pBt value. All
13f0b 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 elements of the
13f0c 20 6c 69 73 74 20 73 68 6f 75 6c 64 20 62 65 6c list should bel
13f0d 6f 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 ong to. ** the
13f0e 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e same connection.
13f0f 20 4f 6e 6c 79 20 73 68 61 72 65 64 20 42 74 72 Only shared Btr
13f10 65 65 73 20 61 72 65 20 6f 6e 20 74 68 65 20 6c ees are on the l
13f11 69 73 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ist. */. assert
13f12 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c ( p->pNext==0 ||
13f13 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 p->pNext->pBt>p
13f14 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 ->pBt );. asser
13f15 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c t( p->pPrev==0 |
13f16 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 70 42 74 3c | p->pPrev->pBt<
13f17 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 p->pBt );. asse
13f18 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 rt( p->pNext==0
13f19 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d || p->pNext->db=
13f1a 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 =p->db );. asse
13f1b 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 rt( p->pPrev==0
13f1c 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d || p->pPrev->db=
13f1d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 =p->db );. asse
13f1e 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 rt( p->sharable
13f1f 7c 7c 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 || (p->pNext==0
13f20 26 26 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20 && p->pPrev==0)
13f21 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 );.. /* Check f
13f22 6f 72 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 or locking consi
13f23 73 74 65 6e 63 79 20 2a 2f 0a 20 20 61 73 73 65 stency */. asse
13f24 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c rt( !p->locked |
13f25 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e | p->wantToLock>
13f26 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
13f27 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 70 2d ->sharable || p-
13f28 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 >wantToLock==0 )
13f29 3b 0a 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c ;.. /* We shoul
13f2a 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 d already hold a
13f2b 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
13f2c 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
13f2d 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 */. assert( sq
13f2e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
13f2f 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
13f30 3b 0a 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 ;.. if( !p->sha
13f31 72 61 62 6c 65 20 29 20 72 65 74 75 72 6e 3b 0a rable ) return;.
13f32 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b p->wantToLock+
13f33 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63 6b +;. if( p->lock
13f34 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 ed ) return;..
13f35 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 /* In most cases
13f36 2c 20 77 65 20 73 68 6f 75 6c 64 20 62 65 20 61 , we should be a
13f37 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74 ble to acquire t
13f38 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 he lock we. **
13f39 77 61 6e 74 20 77 69 74 68 6f 75 74 20 68 61 76 want without hav
13f3a 69 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 ing to go throug
13f3b 68 74 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67 ht the ascending
13f3c 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 lock. ** proce
13f3d 64 75 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 dure that follow
13f3e 73 2e 20 20 4a 75 73 74 20 62 65 20 73 75 72 65 s. Just be sure
13f3f 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 not to block..
13f40 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
13f41 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e 70 3_mutex_try(p->p
13f42 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 4c 49 Bt->mutex)==SQLI
13f43 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 2d 3e TE_OK ){. p->
13f44 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 locked = 1;.
13f45 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f return;. }.. /
13f46 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c * To avoid deadl
13f47 6f 63 6b 2c 20 66 69 72 73 74 20 72 65 6c 65 61 ock, first relea
13f48 73 65 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 74 se all locks wit
13f49 68 20 61 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 h a larger. **
13f4a 42 74 53 68 61 72 65 64 20 61 64 64 72 65 73 73 BtShared address
13f4b 2e 20 20 54 68 65 6e 20 61 63 71 75 69 72 65 20 . Then acquire
13f4c 6f 75 72 20 6c 6f 63 6b 2e 20 20 54 68 65 6e 20 our lock. Then
13f4d 72 65 61 63 71 75 69 72 65 0a 20 20 2a 2a 20 74 reacquire. ** t
13f4e 68 65 20 6f 74 68 65 72 20 42 74 53 68 61 72 65 he other BtShare
13f4f 64 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 65 20 d locks that we
13f50 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69 6e 20 used to hold in
13f51 61 73 63 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 6f ascending. ** o
13f52 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 rder.. */. for
13f53 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74 (pLater=p->pNext
13f54 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 ; pLater; pLater
13f55 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b =pLater->pNext){
13f56 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 61 . assert( pLa
13f57 74 65 72 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b ter->sharable );
13f58 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 61 . assert( pLa
13f59 74 65 72 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c ter->pNext==0 ||
13f5a 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 2d 3e pLater->pNext->
13f5b 70 42 74 3e 70 4c 61 74 65 72 2d 3e 70 42 74 20 pBt>pLater->pBt
13f5c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
13f5d 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 7c pLater->locked |
13f5e 7c 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74 54 6f | pLater->wantTo
13f5f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 69 66 Lock>0 );. if
13f60 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 ( pLater->locked
13f61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
13f62 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4c 3_mutex_leave(pL
13f63 61 74 65 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 ater->pBt->mutex
13f64 29 3b 0a 20 20 20 20 20 20 70 4c 61 74 65 72 2d );. pLater-
13f65 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 >locked = 0;.
13f66 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 }. }. sqlite3
13f67 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e _mutex_enter(p->
13f68 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 pBt->mutex);. p
13f69 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 ->locked = 1;.
13f6a 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e for(pLater=p->pN
13f6b 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 ext; pLater; pLa
13f6c 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 ter=pLater->pNex
13f6d 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 t){. if( pLat
13f6e 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 er->wantToLock )
13f6f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
13f70 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4c 61 74 mutex_enter(pLat
13f71 65 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b er->pBt->mutex);
13f72 0a 20 20 20 20 20 20 70 4c 61 74 65 72 2d 3e 6c . pLater->l
13f73 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d ocked = 1;. }
13f74 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 . }.}../*.** Ex
13f75 69 74 20 74 68 65 20 72 65 63 75 72 73 69 76 65 it the recursive
13f76 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 mutex on a Btre
13f77 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
13f78 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
13f79 33 42 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 3BtreeLeave(Btre
13f7a 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e e *p){. if( p->
13f7b 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
13f7c 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 assert( p->wantT
13f7d 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70 oLock>0 );. p
13f7e 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a ->wantToLock--;.
13f7f 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 if( p->wantT
13f80 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 oLock==0 ){.
13f81 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 assert( p->loc
13f82 6b 65 64 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ked );. sql
13f83 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
13f84 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b (p->pBt->mutex);
13f85 0a 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 . p->locked
13f86 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
13f87 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 }..#ifndef NDEBU
13f88 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 G./*.** Return t
13f89 72 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 rue if the BtSha
13f8a 72 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c red mutex is hel
13f8b 64 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2e 20 d on the btree.
13f8c 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 .**.** This rou
13f8d 74 69 6e 65 20 6d 61 6b 65 73 20 6e 6f 20 64 65 tine makes no de
13f8e 74 65 72 6d 69 6e 61 74 69 6f 6e 20 6f 6e 65 20 termination one
13f8f 77 68 79 20 6f 72 20 61 6e 6f 74 68 65 72 20 69 why or another i
13f90 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 f the.** databas
13f91 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 e connection mut
13f92 65 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2a 0a 2a ex is held..**.*
13f93 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
13f94 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 72 6f 6d s used only from
13f95 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 within assert()
13f96 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a statements..*/.
13f97 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
13f98 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 nt sqlite3BtreeH
13f99 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 20 oldsMutex(Btree
13f9a 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 *p){. return (p
13f9b 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c ->sharable==0 ||
13f9c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 70 . (p
13f9d 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 77 ->locked && p->w
13f9e 61 6e 74 54 6f 4c 6f 63 6b 20 26 26 20 73 71 6c antToLock && sql
13f9f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
13fa0 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 29 p->pBt->mutex)))
13fa1 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 ;.}.#endif...#if
13fa2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
13fa3 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 _INCRBLOB./*.**
13fa4 45 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 Enter and leave
13fa5 61 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 a mutex on a Btr
13fa6 65 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 6f ee given a curso
13fa7 72 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 0a r owned by that.
13fa8 2a 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 65 ** Btree. These
13fa9 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 72 entry points ar
13faa 65 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 6d e used by increm
13fab 65 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 ental I/O and ca
13fac 6e 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 n be.** omitted
13fad 69 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 if that module i
13fae 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 s not used..*/.S
13faf 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
13fb0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
13fb1 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 nterCursor(BtCur
13fb2 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 sor *pCur){. sq
13fb3 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
13fb4 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d pCur->pBtree);.}
13fb5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13fb6 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
13fb7 65 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 eLeaveCursor(BtC
13fb8 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
13fb9 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
13fba 65 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b e(pCur->pBtree);
13fbb 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
13fbc 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
13fbd 42 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 B */.../*.** Ent
13fbe 65 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 er the mutex on
13fbf 65 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f every Btree asso
13fc0 63 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 ciated with a da
13fc1 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 tabase.** connec
13fc2 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e tion. This is n
13fc3 65 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 eeded (for examp
13fc4 6c 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72 le) prior to par
13fc5 73 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d sing.** a statem
13fc6 65 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 6c ent since we wil
13fc7 6c 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 l be comparing t
13fc8 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 able and column
13fc9 6e 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 names.** against
13fca 20 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 all schemas and
13fcb 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 we do not want
13fcc 74 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 65 those schemas be
13fcd 69 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 ing.** reset out
13fce 20 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a from under us..
13fcf 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 **.** There is a
13fd0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c corresponding l
13fd1 65 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 eave-all procedu
13fd2 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 res..**.** Enter
13fd3 20 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e 20 the mutexes in
13fd4 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 accending order
13fd5 62 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 6e by BtShared poin
13fd6 74 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 ter address.** t
13fd7 6f 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 73 o avoid the poss
13fd8 69 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 6c ibility of deadl
13fd9 6f 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 72 ock when two thr
13fda 65 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f eads with.** two
13fdb 20 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 20 or more btrees
13fdc 69 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 in common both t
13fdd 72 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 ry to lock all t
13fde 68 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 heir btrees.** a
13fdf 74 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 61 t the same insta
13fe0 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
13fe1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
13fe2 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 e3BtreeEnterAll(
13fe3 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
13fe4 69 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a int i;. Btree *
13fe5 70 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 p, *pLater;. as
13fe6 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
13fe7 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
13fe8 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 ex) );. for(i=0
13fe9 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
13fea 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 ){. p = db->a
13feb 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 Db[i].pBt;. i
13fec 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 f( p && p->shara
13fed 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e ble ){. p->
13fee 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 wantToLock++;.
13fef 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b if( !p->lock
13ff0 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 ed ){. as
13ff1 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c sert( p->wantToL
13ff2 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 ock==1 );.
13ff3 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 72 65 while( p->pPre
13ff4 76 20 29 20 70 20 3d 20 70 2d 3e 70 50 72 65 76 v ) p = p->pPrev
13ff5 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 ;. while(
13ff6 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 70 2d p->locked && p-
13ff7 3e 70 4e 65 78 74 20 29 20 70 20 3d 20 70 2d 3e >pNext ) p = p->
13ff8 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 66 pNext;. f
13ff9 6f 72 28 70 4c 61 74 65 72 20 3d 20 70 2d 3e 70 or(pLater = p->p
13ffa 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c Next; pLater; pL
13ffb 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 ater=pLater->pNe
13ffc 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 xt){. i
13ffd 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 f( pLater->locke
13ffe 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 d ){.
13fff 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
14000 65 61 76 65 28 70 4c 61 74 65 72 2d 3e 70 42 74 eave(pLater->pBt
14001 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ->mutex);.
14002 20 20 20 20 20 20 70 4c 61 74 65 72 2d 3e 6c 6f pLater->lo
14003 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 cked = 0;.
14004 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
14005 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 while( p
14006 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
14007 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
14008 72 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 r(p->pBt->mutex)
14009 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6c ;. p->l
1400a 6f 63 6b 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20 ocked++;.
1400b 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b p = p->pNext;
1400c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1400d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 }. }. }.}.S
1400e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1400f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c id sqlite3BtreeL
14010 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 20 eaveAll(sqlite3
14011 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *db){. int i;.
14012 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 61 73 73 Btree *p;. ass
14013 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14014 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
14015 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b x) );. for(i=0;
14016 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
14017 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 {. p = db->aD
14018 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 b[i].pBt;. if
14019 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 ( p && p->sharab
1401a 6c 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 le ){. asse
1401b 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 rt( p->wantToLoc
1401c 6b 3e 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e k>0 );. p->
1401d 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 wantToLock--;.
1401e 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 if( p->wantT
1401f 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 oLock==0 ){.
14020 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c assert( p->l
14021 6f 63 6b 65 64 20 29 3b 0a 20 20 20 20 20 20 20 ocked );.
14022 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
14023 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 eave(p->pBt->mut
14024 65 78 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e ex);. p->
14025 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 locked = 0;.
14026 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
14027 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
14028 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
14029 65 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 e if the current
1402a 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 thread holds th
1402b 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1402c 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61 ction.** mutex a
1402d 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20 nd all required
1402e 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73 BtShared mutexes
1402f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
14030 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 tine is used ins
14031 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
14032 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f tements only..*/
14033 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14034 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
14035 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 HoldsAllMutexes(
14036 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
14037 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71 int i;. if( !sq
14038 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14039 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 (db->mutex) ){.
1403a 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1403b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 . for(i=0; i<db
1403c 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
1403d 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70 Btree *p;. p
1403e 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
1403f 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 t;. if( p &&
14040 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20 p->sharable &&.
14041 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74 (p->want
14042 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71 ToLock==0 || !sq
14043 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14044 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 (p->pBt->mutex))
14045 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
14046 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0;. }. }.
14047 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 return 1;.}.#end
14048 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a if /* NDEBUG */.
14049 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 ./*.** Add a new
1404a 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72 20 74 Btree pointer t
1404b 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78 41 72 o a BtreeMutexAr
1404c 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68 65 20 ray. .** if the
1404d 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f 73 73 pointer can poss
1404e 69 62 6c 79 20 62 65 20 73 68 61 72 65 64 20 77 ibly be shared w
1404f 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 64 ith.** another d
14050 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14051 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f on..**.** The po
14052 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70 74 20 inters are kept
14053 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20 in sorted order
14054 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74 2e 20 by pBtree->pBt.
14055 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77 68 65 That.** way whe
14056 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74 65 72 n we go to enter
14057 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78 65 73 all the mutexes
14058 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72 20 74 , we can enter t
14059 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 hem.** in order
1405a 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61 without every ha
1405b 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70 20 61 ving to backup a
1405c 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77 69 74 nd retry and wit
1405d 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69 6e 67 hout.** worrying
1405e 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63 6b 2e about deadlock.
1405f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 .**.** The numbe
14060 72 20 6f 66 20 73 68 61 72 65 64 20 62 74 72 65 r of shared btre
14061 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 es will always b
14062 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c 6c 79 e small (usually
14063 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f 20 61 0 or 1).** so a
14064 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 n insertion sort
14065 20 69 73 20 61 6e 20 61 64 65 71 75 61 74 65 20 is an adequate
14066 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65 2e 0a algorithm here..
14067 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14068 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14069 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 reeMutexArrayIns
1406a 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 72 ert(BtreeMutexAr
1406b 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42 74 72 ray *pArray, Btr
1406c 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20 20 69 ee *pBtree){. i
1406d 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53 68 61 nt i, j;. BtSha
1406e 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66 28 20 red *pBt;. if(
1406f 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70 42 74 pBtree==0 || pBt
14070 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 ree->sharable==0
14071 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 6e 64 ) return;.#ifnd
14072 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20 ef NDEBUG. {.
14073 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 for(i=0; i<pAr
14074 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b ray->nMutex; i++
14075 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
14076 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14077 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 i]!=pBtree );.
14078 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
14079 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d assert( pArray-
1407a 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a 20 20 >nMutex>=0 );.
1407b 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e assert( pArray->
1407c 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69 7a 65 nMutex<ArraySize
1407d 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 29 (pArray->aBtree)
1407e 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42 -1 );. pBt = pB
1407f 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 tree->pBt;. for
14080 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e (i=0; i<pArray->
14081 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 nMutex; i++){.
14082 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 assert( pArray
14083 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 ->aBtree[i]!=pBt
14084 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 ree );. if( p
14085 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d Array->aBtree[i]
14086 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20 ->pBt>pBt ){.
14087 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d for(j=pArray-
14088 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d >nMutex; j>i; j-
14089 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72 -){. pArr
1408a 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20 ay->aBtree[j] =
1408b 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a pArray->aBtree[j
1408c 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 -1];. }.
1408d 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 pArray->aBtre
1408e 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20 e[i] = pBtree;.
1408f 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75 pArray->nMu
14090 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 tex++;. ret
14091 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 urn;. }. }.
14092 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14093 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b pArray->nMutex++
14094 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f ] = pBtree;.}../
14095 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d *.** Enter the m
14096 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 utex of every bt
14097 72 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79 ree in the array
14098 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
14099 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 is.** called at
1409a 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
1409b 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 sqlite3VdbeExec
1409c 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 (). The mutexes
1409d 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61 are.** exited a
1409e 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
1409f 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a same function..
140a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
140a1 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
140a2 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 reeMutexArrayEnt
140a3 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 er(BtreeMutexArr
140a4 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 ay *pArray){. i
140a5 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
140a6 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 i<pArray->nMute
140a7 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 x; i++){. Btr
140a8 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e ee *p = pArray->
140a9 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f aBtree[i];. /
140aa 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e * Some basic san
140ab 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a ity checking */.
140ac 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 assert( i==0
140ad 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 || pArray->aBtr
140ae 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e ee[i-1]->pBt<p->
140af 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
140b0 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c t( !p->locked ||
140b1 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 p->wantToLock>0
140b2 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 );.. /* We s
140b3 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f hould already ho
140b4 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ld a lock on the
140b5 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
140b6 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 tion */. asse
140b7 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
140b8 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
140b9 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e tex) );.. p->
140ba 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 wantToLock++;.
140bb 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 if( !p->locked
140bc 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 && p->sharable
140bd 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
140be 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e _mutex_enter(p->
140bf 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 pBt->mutex);.
140c0 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 p->locked = 1
140c1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
140c2 2a 0a 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d *.** Leave the m
140c3 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 utex of every bt
140c4 72 65 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70 ree in the group
140c5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
140c6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
140c7 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c BtreeMutexArrayL
140c8 65 61 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 eave(BtreeMutexA
140c9 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 rray *pArray){.
140ca 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
140cb 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 0; i<pArray->nMu
140cc 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 tex; i++){. B
140cd 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 tree *p = pArray
140ce 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 ->aBtree[i];.
140cf 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 /* Some basic s
140d0 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a anity checking *
140d1 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d /. assert( i=
140d2 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 =0 || pArray->aB
140d3 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 tree[i-1]->pBt<p
140d4 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 ->pBt );. ass
140d5 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c ert( p->locked |
140d6 7c 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 | !p->sharable )
140d7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d ;. assert( p-
140d8 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b >wantToLock>0 );
140d9 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 .. /* We shou
140da 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 ld already hold
140db 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 a lock on the da
140dc 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
140dd 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 n */. assert(
140de 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
140df 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
140e0 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e ) );.. p->wan
140e1 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 tToLock--;. i
140e2 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b f( p->wantToLock
140e3 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 ==0 && p->locked
140e4 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
140e5 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
140e6 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 >pBt->mutex);.
140e7 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 p->locked =
140e8 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
140e9 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 .#endif /* SQLI
140ea 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
140eb 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 !SQLITE_OMIT_SH
140ec 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f ARED_CACHE */../
140ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
140ee 6e 64 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 nd of btmutex.c
140ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
140f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
140f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
140f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
140f3 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e egin file btree.
140f4 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
140f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
140f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
140f7 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 *.** 2004 April
140f8 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6.**.** The auth
140f9 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
140fa 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
140fb 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
140fc 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
140fd 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
140fe 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
140ff 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
14100 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
14101 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
14102 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
14103 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
14104 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
14105 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
14106 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
14107 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
14108 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
14109 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
1410a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1410b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1410c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1410d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1410e 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 2e 63 .** $Id: btree.c
1410f 2c 76 20 31 2e 35 35 38 20 32 30 30 39 2f 30 31 ,v 1.558 2009/01
14110 2f 31 30 20 31 36 3a 31 35 3a 32 31 20 64 72 68 /10 16:15:21 drh
14111 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 Exp $.**.** Thi
14112 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 s file implement
14113 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 s a external (di
14114 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 sk-based) databa
14115 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e se using BTrees.
14116 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 61 64 .** See the head
14117 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 er comment on "b
14118 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 treeInt.h" for a
14119 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
1411a 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 ation..** Includ
1411b 69 6e 67 20 61 20 64 65 73 63 72 69 70 74 69 6f ing a descriptio
1411c 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 n of file format
1411d 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77 and an overview
1411e 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a of operation..*
1411f 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 /../*.** The hea
14120 64 65 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 der string that
14121 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62 appears at the b
14122 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 eginning of ever
14123 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 y.** SQLite data
14124 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 base..*/.static
14125 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 const char zMagi
14126 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 cHeader[] = SQLI
14127 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a TE_FILE_HEADER;.
14128 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 ./*.** Set this
14129 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1412a 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 to 1 to enable t
1412b 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 racing using the
1412c 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e TRACE.** macro.
1412d 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 .*/.#if 0.int sq
1412e 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65 3d lite3BtreeTrace=
1412f 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 0; /* True to e
14130 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f nable tracing */
14131 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 .# define TRACE(
14132 58 29 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 X) if(sqlite3Bt
14133 72 65 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 reeTrace){printf
14134 20 58 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 X;fflush(stdout
14135 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 );}.#else.# defi
14136 6e 65 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 ne TRACE(X).#end
14137 69 66 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 if....#ifndef SQ
14138 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
14139 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c _CACHE./*.** A l
1413a 69 73 74 20 6f 66 20 42 74 53 68 61 72 65 64 20 ist of BtShared
1413b 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 objects that are
1413c 20 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61 eligible for pa
1413d 72 74 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 rticipation.** i
1413e 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 n shared cache.
1413f 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 This variable h
14140 61 73 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75 as file scope du
14141 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c ring normal buil
14142 64 73 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 ds,.** but the t
14143 65 73 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64 est harness need
14144 73 20 74 6f 20 61 63 63 65 73 73 20 69 74 20 73 s to access it s
14145 6f 20 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f o we make it glo
14146 62 61 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 bal for .** test
14147 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 64 builds..*/.#ifd
14148 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
14149 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 74 QLITE_PRIVATE Bt
1414a 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 Shared *SQLITE_W
1414b 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 SD sqlite3Shared
1414c 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 CacheList = 0;.#
1414d 65 6c 73 65 0a 73 74 61 74 69 63 20 42 74 53 68 else.static BtSh
1414e 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 ared *SQLITE_WSD
1414f 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 sqlite3SharedCa
14150 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6e cheList = 0;.#en
14151 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 dif.#endif /* SQ
14152 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
14153 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 _CACHE */..#ifnd
14154 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
14155 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a HARED_CACHE./*.*
14156 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 * Enable or disa
14157 62 6c 65 20 74 68 65 20 73 68 61 72 65 64 20 70 ble the shared p
14158 61 67 65 72 20 61 6e 64 20 73 63 68 65 6d 61 20 ager and schema
14159 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 features..**.**
1415a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 This routine has
1415b 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 65 78 no effect on ex
1415c 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 isting database
1415d 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20 connections..**
1415e 54 68 65 20 73 68 61 72 65 64 20 63 61 63 68 65 The shared cache
1415f 20 73 65 74 74 69 6e 67 20 65 66 66 65 63 74 73 setting effects
14160 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 63 61 6c only future cal
14161 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 ls to.** sqlite3
14162 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 74 65 33 _open(), sqlite3
14163 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 20 73 71 _open16(), or sq
14164 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e lite3_open_v2().
14165 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
14166 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c nt sqlite3_enabl
14167 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 69 e_shared_cache(i
14168 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20 20 73 71 nt enable){. sq
14169 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
1416a 67 2e 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 g.sharedCacheEna
1416b 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 bled = enable;.
1416c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1416d 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a K;.}.#endif.../*
1416e 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c .** Forward decl
1416f 61 72 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 aration.*/.stati
14170 63 20 69 6e 74 20 63 68 65 63 6b 52 65 61 64 4c c int checkReadL
14171 6f 63 6b 73 28 42 74 72 65 65 2a 2c 20 50 67 6e ocks(Btree*, Pgn
14172 6f 2c 20 42 74 43 75 72 73 6f 72 2a 2c 20 69 36 o, BtCursor*, i6
14173 34 29 3b 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 4);...#ifdef SQL
14174 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
14175 43 41 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 CACHE. /*. **
14176 54 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 71 75 The functions qu
14177 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 eryTableLock(),
14178 6c 6f 63 6b 54 61 62 6c 65 28 29 20 61 6e 64 20 lockTable() and
14179 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 unlockAllTables(
1417a 29 0a 20 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 ). ** manipulat
1417b 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 e entries in the
1417c 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 BtShared.pLock
1417d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 75 73 65 64 linked list used
1417e 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73 to store. ** s
1417f 68 61 72 65 64 2d 63 61 63 68 65 20 74 61 62 6c hared-cache tabl
14180 65 20 6c 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 e level locks. I
14181 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 f the library is
14182 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
14183 68 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 he. ** shared-c
14184 61 63 68 65 20 66 65 61 74 75 72 65 20 64 69 73 ache feature dis
14185 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 72 abled, then ther
14186 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f e is only ever o
14187 6e 65 20 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20 ne user. ** of
14188 65 61 63 68 20 42 74 53 68 61 72 65 64 20 73 74 each BtShared st
14189 72 75 63 74 75 72 65 20 61 6e 64 20 73 6f 20 74 ructure and so t
1418a 68 69 73 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e his locking is n
1418b 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 20 ot necessary. .
1418c 20 2a 2a 20 53 6f 20 64 65 66 69 6e 65 20 74 68 ** So define th
1418d 65 20 6c 6f 63 6b 20 72 65 6c 61 74 65 64 20 66 e lock related f
1418e 75 6e 63 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f unctions as no-o
1418f 70 73 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 ps.. */. #defi
14190 6e 65 20 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 ne queryTableLoc
14191 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f k(a,b,c) SQLITE_
14192 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 6c 6f 63 OK. #define loc
14193 6b 54 61 62 6c 65 28 61 2c 62 2c 63 29 20 53 51 kTable(a,b,c) SQ
14194 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e LITE_OK. #defin
14195 65 20 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 e unlockAllTable
14196 73 28 61 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 s(a).#endif..#if
14197 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
14198 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a _SHARED_CACHE./*
14199 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65 .** Query to see
1419a 20 69 66 20 62 74 72 65 65 20 68 61 6e 64 6c 65 if btree handle
1419b 20 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 p may obtain a
1419c 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f lock of type eLo
1419d 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 ck .** (READ_LOC
1419e 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 K or WRITE_LOCK)
1419f 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 on the table wi
141a0 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 th root-page iTa
141a1 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c b. Return.** SQL
141a2 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f ITE_OK if the lo
141a3 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e ck may be obtain
141a4 65 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 20 6c ed (by calling l
141a5 6f 63 6b 54 61 62 6c 65 28 29 29 2c 20 6f 72 0a ockTable()), or.
141a6 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 ** SQLITE_LOCKED
141a7 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 if not..*/.stat
141a8 69 63 20 69 6e 74 20 71 75 65 72 79 54 61 62 6c ic int queryTabl
141a9 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 eLock(Btree *p,
141aa 50 67 6e 6f 20 69 54 61 62 2c 20 75 38 20 65 4c Pgno iTab, u8 eL
141ab 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 ock){. BtShared
141ac 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
141ad 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b BtLock *pIter;
141ae 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
141af 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
141b0 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 ex(p) );. asser
141b1 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c t( eLock==READ_L
141b2 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 OCK || eLock==WR
141b3 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 ITE_LOCK );. as
141b4 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 sert( p->db!=0 )
141b5 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 69 ;. . /* This i
141b6 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 s a no-op if the
141b7 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 shared-cache is
141b8 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a not enabled */.
141b9 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 if( !p->sharab
141ba 6c 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e le ){. return
141bb 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
141bc 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 . /* If some ot
141bd 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 her connection i
141be 73 20 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 s holding an exc
141bf 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 lusive lock, the
141c0 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 20 . ** requested
141c1 6c 6f 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 lock may not be
141c2 6f 62 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 obtained.. */.
141c3 20 69 66 28 20 70 42 74 2d 3e 70 45 78 63 6c 75 if( pBt->pExclu
141c4 73 69 76 65 20 26 26 20 70 42 74 2d 3e 70 45 78 sive && pBt->pEx
141c5 63 6c 75 73 69 76 65 21 3d 70 20 29 7b 0a 20 20 clusive!=p ){.
141c6 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
141c7 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a 0a 20 20 2f LOCKED;. }.. /
141c8 2a 20 54 68 69 73 20 28 61 6c 6f 6e 67 20 77 69 * This (along wi
141c9 74 68 20 6c 6f 63 6b 54 61 62 6c 65 28 29 29 20 th lockTable())
141ca 69 73 20 77 68 65 72 65 20 74 68 65 20 52 65 61 is where the Rea
141cb 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 dUncommitted fla
141cc 67 20 69 73 0a 20 20 2a 2a 20 64 65 61 6c 74 20 g is. ** dealt
141cd 77 69 74 68 2e 20 49 66 20 74 68 65 20 63 61 6c with. If the cal
141ce 6c 65 72 20 69 73 20 71 75 65 72 79 69 6e 67 20 ler is querying
141cf 66 6f 72 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 for a read-lock
141d0 61 6e 64 20 74 68 65 20 66 6c 61 67 20 69 73 0a and the flag is.
141d1 20 20 2a 2a 20 73 65 74 2c 20 69 74 20 69 73 20 ** set, it is
141d2 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 unconditionally
141d3 67 72 61 6e 74 65 64 20 2d 20 65 76 65 6e 20 69 granted - even i
141d4 66 20 74 68 65 72 65 20 61 72 65 20 77 72 69 74 f there are writ
141d5 65 2d 6c 6f 63 6b 73 0a 20 20 2a 2a 20 6f 6e 20 e-locks. ** on
141d6 74 68 65 20 74 61 62 6c 65 2e 20 49 66 20 61 20 the table. If a
141d7 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73 20 72 65 write-lock is re
141d8 71 75 65 73 74 65 64 2c 20 74 68 65 20 52 65 61 quested, the Rea
141d9 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 dUncommitted fla
141da 67 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 63 6f g. ** is not co
141db 6e 73 69 64 65 72 65 64 2e 0a 20 20 2a 2a 0a 20 nsidered.. **.
141dc 20 2a 2a 20 49 6e 20 66 75 6e 63 74 69 6f 6e 20 ** In function
141dd 6c 6f 63 6b 54 61 62 6c 65 28 29 2c 20 69 66 20 lockTable(), if
141de 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 64 a read-lock is d
141df 65 6d 61 6e 64 65 64 20 61 6e 64 20 74 68 65 20 emanded and the
141e0 0a 20 20 2a 2a 20 52 65 61 64 55 6e 63 6f 6d 6d . ** ReadUncomm
141e1 69 74 74 65 64 20 66 6c 61 67 20 69 73 20 73 65 itted flag is se
141e2 74 2c 20 6e 6f 20 65 6e 74 72 79 20 69 73 20 61 t, no entry is a
141e3 64 64 65 64 20 74 6f 20 74 68 65 20 6c 6f 63 6b dded to the lock
141e4 73 20 6c 69 73 74 20 0a 20 20 2a 2a 20 28 42 74 s list . ** (Bt
141e5 53 68 61 72 65 64 2e 70 4c 6f 63 6b 29 2e 0a 20 Shared.pLock)..
141e6 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 73 75 6d 6d **. ** To summ
141e7 61 72 69 7a 65 3a 20 49 66 20 74 68 65 20 52 65 arize: If the Re
141e8 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c adUncommitted fl
141e9 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 ag is set, then
141ea 72 65 61 64 20 63 75 72 73 6f 72 73 20 64 6f 0a read cursors do.
141eb 20 20 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 ** not create
141ec 6f 72 20 72 65 73 70 65 63 74 20 74 61 62 6c 65 or respect table
141ed 20 6c 6f 63 6b 73 2e 20 54 68 65 20 6c 6f 63 6b locks. The lock
141ee 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 66 6f ing procedure fo
141ef 72 20 61 20 0a 20 20 2a 2a 20 77 72 69 74 65 2d r a . ** write-
141f0 63 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 cursor does not
141f1 63 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 change.. */. i
141f2 66 28 20 0a 20 20 20 20 30 3d 3d 28 70 2d 3e 64 f( . 0==(p->d
141f3 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f b->flags&SQLITE_
141f4 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 ReadUncommitted)
141f5 20 7c 7c 20 0a 20 20 20 20 65 4c 6f 63 6b 3d 3d || . eLock==
141f6 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c 0a 20 20 WRITE_LOCK ||.
141f7 20 20 69 54 61 62 3d 3d 4d 41 53 54 45 52 5f 52 iTab==MASTER_R
141f8 4f 4f 54 0a 20 20 29 7b 0a 20 20 20 20 66 6f 72 OOT. ){. for
141f9 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 (pIter=pBt->pLoc
141fa 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d k; pIter; pIter=
141fb 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 pIter->pNext){.
141fc 20 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e if( pIter->
141fd 70 42 74 72 65 65 21 3d 70 20 26 26 20 70 49 74 pBtree!=p && pIt
141fe 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 er->iTable==iTab
141ff 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 28 && . (
14200 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c pIter->eLock!=eL
14201 6f 63 6b 20 7c 7c 20 65 4c 6f 63 6b 21 3d 52 45 ock || eLock!=RE
14202 41 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 AD_LOCK) ){.
14203 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
14204 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20 E_LOCKED;.
14205 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
14206 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
14207 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
14208 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
14209 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 CACHE */..#ifnde
1420a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
1420b 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
1420c 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 Add a lock on t
1420d 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f he table with ro
1420e 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74 ot-page iTable t
1420f 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 o the shared-btr
14210 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 ee used.** by Bt
14211 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 ree handle p. Pa
14212 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 rameter eLock mu
14213 73 74 20 62 65 20 65 69 74 68 65 72 20 52 45 41 st be either REA
14214 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 D_LOCK or .** WR
14215 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 ITE_LOCK..**.**
14216 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
14217 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 urned if the loc
14218 6b 20 69 73 20 61 64 64 65 64 20 73 75 63 63 65 k is added succe
14219 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f ssfully. SQLITE_
1421a 42 55 53 59 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 BUSY and.** SQLI
1421b 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61 6c 73 TE_NOMEM may als
1421c 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a o be returned..*
1421d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 /.static int loc
1421e 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c kTable(Btree *p,
1421f 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 Pgno iTable, u8
14220 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 eLock){. BtSha
14221 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
14222 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f t;. BtLock *pLo
14223 63 6b 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b ck = 0;. BtLock
14224 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 *pIter;.. asse
14225 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
14226 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b HoldsMutex(p) );
14227 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b . assert( eLock
14228 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 ==READ_LOCK || e
14229 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
1422a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1422b 3e 64 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 >db!=0 );.. /*
1422c 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 This is a no-op
1422d 69 66 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 if the shared-ca
1422e 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c che is not enabl
1422f 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e ed */. if( !p->
14230 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
14231 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
14232 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 ;. }.. assert(
14233 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 SQLITE_OK==quer
14234 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 yTableLock(p, iT
14235 61 62 6c 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a able, eLock) );.
14236 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 61 . /* If the rea
14237 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c d-uncommitted fl
14238 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 61 20 ag is set and a
14239 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 72 65 71 read-lock is req
1423a 75 65 73 74 65 64 2c 0a 20 20 2a 2a 20 72 65 74 uested,. ** ret
1423b 75 72 6e 20 65 61 72 6c 79 20 77 69 74 68 6f 75 urn early withou
1423c 74 20 61 64 64 69 6e 67 20 61 6e 20 65 6e 74 72 t adding an entr
1423d 79 20 74 6f 20 74 68 65 20 42 74 53 68 61 72 65 y to the BtShare
1423e 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 2e 20 53 65 d.pLock list. Se
1423f 65 0a 20 20 2a 2a 20 63 6f 6d 6d 65 6e 74 20 69 e. ** comment i
14240 6e 20 66 75 6e 63 74 69 6f 6e 20 71 75 65 72 79 n function query
14241 54 61 62 6c 65 4c 6f 63 6b 28 29 20 66 6f 72 20 TableLock() for
14242 6d 6f 72 65 20 69 6e 66 6f 20 6f 6e 20 68 61 6e more info on han
14243 64 6c 69 6e 67 20 0a 20 20 2a 2a 20 74 68 65 20 dling . ** the
14244 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 ReadUncommitted
14245 66 6c 61 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 flag.. */. if(
14246 20 0a 20 20 20 20 28 70 2d 3e 64 62 2d 3e 66 6c . (p->db->fl
14247 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 ags&SQLITE_ReadU
14248 6e 63 6f 6d 6d 69 74 74 65 64 29 20 26 26 20 0a ncommitted) && .
14249 20 20 20 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44 (eLock==READ
1424a 5f 4c 4f 43 4b 29 20 26 26 0a 20 20 20 20 69 54 _LOCK) &&. iT
1424b 61 62 6c 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f able!=MASTER_ROO
1424c 54 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 T. ){. retur
1424d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
1424e 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 .. /* First sea
1424f 72 63 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72 rch the list for
14250 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 an existing loc
14251 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e k on this table.
14252 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d */. for(pIter=
14253 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 pBt->pLock; pIte
14254 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e r; pIter=pIter->
14255 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 pNext){. if(
14256 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 pIter->iTable==i
14257 54 61 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e Table && pIter->
14258 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 pBtree==p ){.
14259 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 pLock = pIter
1425a 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1425b 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
1425c 66 20 74 68 65 20 61 62 6f 76 65 20 73 65 61 72 f the above sear
1425d 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 ch did not find
1425e 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 a BtLock struct
1425f 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65 associating Btre
14260 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 e p. ** with ta
14261 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f ble iTable, allo
14262 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e cate one and lin
14263 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 k it into the li
14264 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 st.. */. if( !
14265 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f pLock ){. pLo
14266 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 ck = (BtLock *)s
14267 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
14268 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 (sizeof(BtLock))
14269 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b ;. if( !pLock
1426a 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1426b 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1426c 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e }. pLock->
1426d 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b iTable = iTable;
1426e 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 . pLock->pBtr
1426f 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 ee = p;. pLoc
14270 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e k->pNext = pBt->
14271 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e pLock;. pBt->
14272 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 pLock = pLock;.
14273 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 }.. /* Set the
14274 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 BtLock.eLock va
14275 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 riable to the ma
14276 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72 ximum of the cur
14277 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 rent lock. ** a
14278 6e 64 20 74 68 65 20 72 65 71 75 65 73 74 65 64 nd the requested
14279 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e lock. This mean
1427a 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 s if a write-loc
1427b 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20 68 65 k was already he
1427c 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 ld. ** and a re
1427d 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 ad-lock requeste
1427e 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f d, we don't inco
1427f 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 rrectly downgrad
14280 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f e the lock.. */
14281 0a 20 20 61 73 73 65 72 74 28 20 57 52 49 54 45 . assert( WRITE
14282 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 _LOCK>READ_LOCK
14283 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 );. if( eLock>p
14284 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 Lock->eLock ){.
14285 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 pLock->eLock
14286 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 = eLock;. }..
14287 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
14288 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 ;.}.#endif /* !S
14289 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
1428a 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e D_CACHE */..#ifn
1428b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1428c 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a SHARED_CACHE./*.
1428d 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 ** Release all t
1428e 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 he table locks (
1428f 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 locks obtained v
14290 69 61 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 ia calls to the
14291 6c 6f 63 6b 54 61 62 6c 65 28 29 0a 2a 2a 20 70 lockTable().** p
14292 72 6f 63 65 64 75 72 65 29 20 68 65 6c 64 20 62 rocedure) held b
14293 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 y Btree handle p
14294 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
14295 20 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 unlockAllTables
14296 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 (Btree *p){. Bt
14297 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
14298 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a >pBt;. BtLock *
14299 2a 70 70 49 74 65 72 20 3d 20 26 70 42 74 2d 3e *ppIter = &pBt->
1429a 70 4c 6f 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 pLock;.. assert
1429b 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
1429c 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 ldsMutex(p) );.
1429d 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 assert( p->shar
1429e 61 62 6c 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 able || 0==*ppIt
1429f 65 72 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 er );.. while(
142a0 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 42 *ppIter ){. B
142a1 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a tLock *pLock = *
142a2 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 65 ppIter;. asse
142a3 72 74 28 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 rt( pBt->pExclus
142a4 69 76 65 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 ive==0 || pBt->p
142a5 45 78 63 6c 75 73 69 76 65 3d 3d 70 4c 6f 63 6b Exclusive==pLock
142a6 2d 3e 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 ->pBtree );.
142a7 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 if( pLock->pBtre
142a8 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a 70 e==p ){. *p
142a9 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70 pIter = pLock->p
142aa 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 Next;. sqli
142ab 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b te3_free(pLock);
142ac 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
142ad 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 ppIter = &pLoc
142ae 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a k->pNext;. }.
142af 20 20 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e }.. if( pBt->
142b0 70 45 78 63 6c 75 73 69 76 65 3d 3d 70 20 29 7b pExclusive==p ){
142b1 0a 20 20 20 20 70 42 74 2d 3e 70 45 78 63 6c 75 . pBt->pExclu
142b2 73 69 76 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a sive = 0;. }.}.
142b3 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
142b4 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
142b5 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f HE */..static vo
142b6 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d id releasePage(M
142b7 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b 20 emPage *pPage);
142b8 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 /* Forward refe
142b9 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 rence */../*.**
142ba 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 Verify that the
142bb 63 75 72 73 6f 72 20 68 6f 6c 64 73 20 61 20 6d cursor holds a m
142bc 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 53 68 utex on the BtSh
142bd 61 72 65 64 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ared.*/.#ifndef
142be 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e NDEBUG.static in
142bf 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 t cursorHoldsMut
142c0 65 78 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b ex(BtCursor *p){
142c1 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
142c2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
142c3 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 pBt->mutex);.}.#
142c4 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 endif...#ifndef
142c5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
142c6 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c BLOB./*.** Inval
142c7 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c idate the overfl
142c8 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 ow page-list cac
142c9 68 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 43 he for cursor pC
142ca 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 73 ur, if any..*/.s
142cb 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c tatic void inval
142cc 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 idateOverflowCac
142cd 68 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 he(BtCursor *pCu
142ce 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 r){. assert( cu
142cf 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
142d0 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 Cur) );. sqlite
142d1 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f 76 3_free(pCur->aOv
142d2 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d erflow);. pCur-
142d3 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a >aOverflow = 0;.
142d4 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 }../*.** Invalid
142d5 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 ate the overflow
142d6 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 page-list cache
142d7 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f 72 73 for all cursors
142d8 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 opened.** on th
142d9 65 20 73 68 61 72 65 64 20 62 74 72 65 65 20 73 e shared btree s
142da 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a 2a 2f tructure pBt..*/
142db 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 .static void inv
142dc 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c alidateAllOverfl
142dd 6f 77 43 61 63 68 65 28 42 74 53 68 61 72 65 64 owCache(BtShared
142de 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 *pBt){. BtCurs
142df 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 or *p;. assert(
142e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
142e1 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
142e2 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e );. for(p=pBt->
142e3 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d pCursor; p; p=p-
142e4 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 76 >pNext){. inv
142e5 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 alidateOverflowC
142e6 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 23 ache(p);. }.}.#
142e7 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 69 else. #define i
142e8 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f nvalidateOverflo
142e9 77 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 wCache(x). #def
142ea 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 41 6c ine invalidateAl
142eb 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 lOverflowCache(x
142ec 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
142ed 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 Save the current
142ee 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e cursor position
142ef 20 69 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65 in the variable
142f0 73 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 s BtCursor.nKey
142f1 0a 2a 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72 .** and BtCursor
142f2 2e 70 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f .pKey. The curso
142f3 72 27 73 20 73 74 61 74 65 20 69 73 20 73 65 74 r's state is set
142f4 20 74 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49 to CURSOR_REQUI
142f5 52 45 53 45 45 4b 2e 0a 2a 2f 0a 73 74 61 74 69 RESEEK..*/.stati
142f6 63 20 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 c int saveCursor
142f7 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f Position(BtCurso
142f8 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 r *pCur){. int
142f9 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 rc;.. assert( C
142fa 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 URSOR_VALID==pCu
142fb 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 r->eState );. a
142fc 73 73 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e ssert( 0==pCur->
142fd 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 pKey );. assert
142fe 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
142ff 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 ex(pCur) );.. r
14300 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
14301 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 KeySize(pCur, &p
14302 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 0a 20 20 2f Cur->nKey);.. /
14303 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 * If this is an
14304 69 6e 74 4b 65 79 20 74 61 62 6c 65 2c 20 74 68 intKey table, th
14305 65 6e 20 74 68 65 20 61 62 6f 76 65 20 63 61 6c en the above cal
14306 6c 20 74 6f 20 42 74 72 65 65 4b 65 79 53 69 7a l to BtreeKeySiz
14307 65 28 29 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 e(). ** stores
14308 74 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 the integer key
14309 69 6e 20 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 in pCur->nKey. I
1430a 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 69 73 n this case this
1430b 20 76 61 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61 value is. ** a
1430c 6c 6c 20 74 68 61 74 20 69 73 20 72 65 71 75 69 ll that is requi
1430d 72 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 red. Otherwise,
1430e 69 66 20 70 43 75 72 20 69 73 20 6e 6f 74 20 6f if pCur is not o
1430f 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 pen on an intKey
14310 0a 20 20 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65 . ** table, the
14311 6e 20 6d 61 6c 6c 6f 63 20 73 70 61 63 65 20 66 n malloc space f
14312 6f 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 or and store the
14313 20 70 43 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65 pCur->nKey byte
14314 73 20 6f 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64 s of key . ** d
14315 61 74 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ata.. */. if(
14316 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
14317 20 30 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 0==pCur->apPage
14318 5b 30 5d 2d 3e 69 6e 74 4b 65 79 29 7b 0a 20 20 [0]->intKey){.
14319 20 20 76 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 void *pKey = s
1431a 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 69 qlite3Malloc( (i
1431b 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 20 29 3b nt)pCur->nKey );
1431c 0a 20 20 20 20 69 66 28 20 70 4b 65 79 20 29 7b . if( pKey ){
1431d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1431e 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 te3BtreeKey(pCur
1431f 2c 20 30 2c 20 28 69 6e 74 29 70 43 75 72 2d 3e , 0, (int)pCur->
14320 6e 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20 20 20 nKey, pKey);.
14321 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
14322 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
14323 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 pCur->pKey = pKe
14324 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a y;. }else{.
14325 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
14326 66 72 65 65 28 70 4b 65 79 29 3b 0a 20 20 20 20 free(pKey);.
14327 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
14328 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
14329 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
1432a 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70 43 75 }. assert( !pCu
1432b 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
1432c 74 4b 65 79 20 7c 7c 20 21 70 43 75 72 2d 3e 70 tKey || !pCur->p
1432d 4b 65 79 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 Key );.. if( rc
1432e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1432f 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
14330 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e r(i=0; i<=pCur->
14331 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 iPage; i++){.
14332 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
14333 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b Cur->apPage[i]);
14334 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 70 50 . pCur->apP
14335 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 age[i] = 0;.
14336 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 }. pCur->iPag
14337 65 20 3d 20 2d 31 3b 0a 20 20 20 20 70 43 75 72 e = -1;. pCur
14338 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
14339 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0a 20 R_REQUIRESEEK;.
1433a 20 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 }.. invalidate
1433b 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 OverflowCache(pC
1433c 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ur);. return rc
1433d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 ;.}../*.** Save
1433e 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 the positions of
1433f 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78 63 all cursors exc
14340 65 70 74 20 70 45 78 63 65 70 74 20 6f 70 65 6e ept pExcept open
14341 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a on the table .*
14342 2a 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 * with root-page
14343 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c 6c 79 2c iRoot. Usually,
14344 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 this is called
14345 6a 75 73 74 20 62 65 66 6f 72 65 20 63 75 72 73 just before curs
14346 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74 20 69 73 or.** pExcept is
14347 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 79 20 used to modify
14348 74 68 65 20 74 61 62 6c 65 20 28 42 74 72 65 65 the table (Btree
14349 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 72 65 Delete() or Btre
1434a 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a 2f 0a 73 eInsert())..*/.s
1434b 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 41 6c tatic int saveAl
1434c 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 lCursors(BtShare
1434d 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52 6f d *pBt, Pgno iRo
1434e 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 45 ot, BtCursor *pE
1434f 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75 72 73 xcept){. BtCurs
14350 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 or *p;. assert(
14351 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14352 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
14353 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 );. assert( pEx
14354 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63 65 cept==0 || pExce
14355 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a pt->pBt==pBt );.
14356 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 for(p=pBt->pCu
14357 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e rsor; p; p=p->pN
14358 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 21 ext){. if( p!
14359 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d 3d =pExcept && (0==
1435a 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f iRoot || p->pgno
1435b 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 26 20 Root==iRoot) &&
1435c 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 . p->eSta
1435d 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
1435e 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 ){. int rc
1435f 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 = saveCursorPos
14360 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 20 ition(p);.
14361 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 if( SQLITE_OK!=r
14362 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 c ){. ret
14363 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
14364 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
14365 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
14366 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 ./*.** Clear the
14367 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 current cursor
14368 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c position..*/.SQL
14369 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1436a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
1436b 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f arCursor(BtCurso
1436c 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 r *pCur){. asse
1436d 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
1436e 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
1436f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 sqlite3_free(pCu
14370 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43 75 72 r->pKey);. pCur
14371 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20 70 43 ->pKey = 0;. pC
14372 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
14373 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a 0a SOR_INVALID;.}..
14374 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 /*.** Restore th
14375 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
14376 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 position it was
14377 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 in (or as close
14378 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a to as possible).
14379 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 ** when saveCurs
1437a 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 orPosition() was
1437b 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 called. Note th
1437c 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c at this call del
1437d 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 etes the .** sav
1437e 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f ed position info
1437f 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 stored by saveC
14380 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c ursorPosition(),
14381 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 so there can be
14382 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 .** at most one
14383 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 effective restor
14384 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
14385 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 ) call after eac
14386 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 h .** saveCursor
14387 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 53 Position()..*/.S
14388 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
14389 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 t sqlite3BtreeRe
1438a 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
1438b 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ion(BtCursor *pC
1438c 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ur){. int rc;.
1438d 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
1438e 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
1438f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
14390 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f r->eState>=CURSO
14391 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b R_REQUIRESEEK );
14392 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 . if( pCur->eSt
14393 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c ate==CURSOR_FAUL
14394 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 T ){. return
14395 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a pCur->skip;. }.
14396 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
14397 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b CURSOR_INVALID;
14398 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
14399 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c treeMoveto(pCur,
1439a 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 pCur->pKey, pCu
1439b 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 r->nKey, 0, &pCu
1439c 72 2d 3e 73 6b 69 70 29 3b 0a 20 20 69 66 28 20 r->skip);. if(
1439d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1439e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
1439f 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 e(pCur->pKey);.
143a0 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 pCur->pKey =
143a1 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 0;. assert( p
143a2 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
143a3 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 SOR_VALID || pCu
143a4 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
143a5 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 7d R_INVALID );. }
143a6 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
143a7 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f 72 65 .#define restore
143a8 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
143a9 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 65 ) \. (p->eState
143aa 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 >=CURSOR_REQUIRE
143ab 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 20 SEEK ? \.
143ac 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 sqlite3BtreeRe
143ad 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
143ae 69 6f 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20 20 ion(p) : \.
143af 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a SQLITE_OK)..
143b0 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 /*.** Determine
143b1 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 whether or not a
143b2 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 cursor has move
143b3 64 20 66 72 6f 6d 20 74 68 65 20 70 6f 73 69 74 d from the posit
143b4 69 6f 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c 61 ion it.** was la
143b5 73 74 20 70 6c 61 63 65 64 20 61 74 2e 20 20 43 st placed at. C
143b6 75 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 ursors can move
143b7 77 68 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 when the row the
143b8 79 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a 2a y are pointing.*
143b9 2a 20 61 74 20 69 73 20 64 65 6c 65 74 65 64 20 * at is deleted
143ba 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 out from under t
143bb 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 hem..**.** This
143bc 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
143bd 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 an error code if
143be 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 something goes
143bf 77 72 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 69 wrong. The.** i
143c0 6e 74 65 67 65 72 20 2a 70 48 61 73 4d 6f 76 65 nteger *pHasMove
143c1 64 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 d is set to one
143c2 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 if the cursor ha
143c3 73 20 6d 6f 76 65 64 20 61 6e 64 20 30 20 69 66 s moved and 0 if
143c4 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f not..*/.SQLITE_
143c5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
143c6 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 te3BtreeCursorHa
143c7 73 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72 20 sMoved(BtCursor
143c8 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 61 73 *pCur, int *pHas
143c9 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 Moved){. int rc
143ca 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 ;.. rc = restor
143cb 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
143cc 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 pCur);. if( rc
143cd 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 ){. *pHasMove
143ce 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 d = 1;. retur
143cf 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 n rc;. }. if(
143d0 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 pCur->eState!=CU
143d1 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 RSOR_VALID || pC
143d2 75 72 2d 3e 73 6b 69 70 21 3d 30 20 29 7b 0a 20 ur->skip!=0 ){.
143d3 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 *pHasMoved =
143d4 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
143d5 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a *pHasMoved = 0;.
143d6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
143d7 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 ITE_OK;.}..#ifnd
143d8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
143d9 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 UTOVACUUM./*.**
143da 47 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d Given a page num
143db 62 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 ber of a regular
143dc 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 database page,
143dd 72 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a return the page.
143de 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 ** number for th
143df 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 e pointer-map pa
143e0 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 ge that contains
143e1 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 the entry for t
143e2 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 he.** input page
143e3 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 number..*/.stat
143e4 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 ic Pgno ptrmapPa
143e5 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 geno(BtShared *p
143e6 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a Bt, Pgno pgno){.
143e7 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d int nPagesPerM
143e8 61 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 apPage;. Pgno i
143e9 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 PtrMap, ret;. a
143ea 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
143eb 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
143ec 75 74 65 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 utex) );. nPage
143ed 73 50 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 sPerMapPage = (p
143ee 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 Bt->usableSize/5
143ef 29 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d )+1;. iPtrMap =
143f0 20 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 (pgno-2)/nPages
143f1 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 PerMapPage;. re
143f2 74 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 t = (iPtrMap*nPa
143f3 67 65 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b gesPerMapPage) +
143f4 20 32 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 2; . if( ret==
143f5 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
143f6 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 E(pBt) ){. re
143f7 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 t++;. }. retur
143f8 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n ret;.}../*.**
143f9 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 Write an entry i
143fa 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 nto the pointer
143fb 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 map..**.** This
143fc 72 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 routine updates
143fd 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 the pointer map
143fe 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e entry for page n
143ff 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 umber 'key'.** s
14400 6f 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 o that it maps t
14401 6f 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61 o type 'eType' a
14402 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e nd parent page n
14403 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a umber 'pgno'..**
14404 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 An error code i
14405 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f s returned if so
14406 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f mething goes wro
14407 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51 ng, otherwise SQ
14408 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 LITE_OK..*/.stat
14409 69 63 20 69 6e 74 20 70 74 72 6d 61 70 50 75 74 ic int ptrmapPut
1440a 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
1440b 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65 54 79 Pgno key, u8 eTy
1440c 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e 74 29 pe, Pgno parent)
1440d 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 {. DbPage *pDbP
1440e 61 67 65 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 age; /* The poi
1440f 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f nter map page */
14410 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 . u8 *pPtrmap;
14411 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e /* The poin
14412 74 65 72 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a ter map data */.
14413 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 Pgno iPtrmap;
14414 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 /* The point
14415 65 72 20 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 er map page numb
14416 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 er */. int offs
14417 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 et; /* Off
14418 73 65 74 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d set in pointer m
14419 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ap page */. int
1441a 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
1441b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1441c 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
1441d 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 65 ;. /* The maste
1441e 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e r-journal page n
1441f 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65 72 umber must never
14420 20 62 65 20 75 73 65 64 20 61 73 20 61 20 70 6f be used as a po
14421 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a inter map page *
14422 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 50 /. assert( 0==P
14423 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
14424 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 , PENDING_BYTE_P
14425 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 AGE(pBt)) );..
14426 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 assert( pBt->aut
14427 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 28 oVacuum );. if(
14428 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 72 key==0 ){. r
14429 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
1442a 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 RUPT_BKPT;. }.
1442b 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 iPtrmap = PTRMA
1442c 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 P_PAGENO(pBt, ke
1442d 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 y);. rc = sqlit
1442e 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e e3PagerGet(pBt->
1442f 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c pPager, iPtrmap,
14430 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 &pDbPage);. if
14431 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
14432 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
14433 3b 0a 20 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d ;. }. offset =
14434 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 PTRMAP_PTROFFSE
14435 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b T(iPtrmap, key);
14436 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38 . pPtrmap = (u8
14437 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 *)sqlite3PagerG
14438 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b etData(pDbPage);
14439 0a 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 70 .. if( eType!=p
1443a 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 7c Ptrmap[offset] |
1443b 7c 20 67 65 74 34 62 79 74 65 28 26 70 50 74 72 | get4byte(&pPtr
1443c 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 21 3d map[offset+1])!=
1443d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 54 52 parent ){. TR
1443e 41 43 45 28 28 22 50 54 52 4d 41 50 5f 55 50 44 ACE(("PTRMAP_UPD
1443f 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c 25 64 29 ATE: %d->(%d,%d)
14440 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79 70 65 2c \n", key, eType,
14441 20 70 61 72 65 6e 74 29 29 3b 0a 20 20 20 20 72 parent));. r
14442 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
14443 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a Write(pDbPage);.
14444 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
14445 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 TE_OK ){. p
14446 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 3d Ptrmap[offset] =
14447 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 70 75 eType;. pu
14448 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b t4byte(&pPtrmap[
14449 6f 66 66 73 65 74 2b 31 5d 2c 20 70 61 72 65 6e offset+1], paren
1444a 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 t);. }. }..
1444b 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
1444c 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 72 ef(pDbPage);. r
1444d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1444e 2a 2a 20 52 65 61 64 20 61 6e 20 65 6e 74 72 79 ** Read an entry
1444f 20 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 65 from the pointe
14450 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 r map..**.** Thi
14451 73 20 72 6f 75 74 69 6e 65 20 72 65 74 72 69 65 s routine retrie
14452 76 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 ves the pointer
14453 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 map entry for pa
14454 67 65 20 27 6b 65 79 27 2c 20 77 72 69 74 69 6e ge 'key', writin
14455 67 0a 2a 2a 20 74 68 65 20 74 79 70 65 20 61 6e g.** the type an
14456 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 d parent page nu
14457 6d 62 65 72 20 74 6f 20 2a 70 45 54 79 70 65 20 mber to *pEType
14458 61 6e 64 20 2a 70 50 67 6e 6f 20 72 65 73 70 65 and *pPgno respe
14459 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e 20 65 ctively..** An e
1445a 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
1445b 75 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 urned if somethi
1445c 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f ng goes wrong, o
1445d 74 68 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f therwise SQLITE_
1445e 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e OK..*/.static in
1445f 74 20 70 74 72 6d 61 70 47 65 74 28 42 74 53 68 t ptrmapGet(BtSh
14460 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 ared *pBt, Pgno
14461 6b 65 79 2c 20 75 38 20 2a 70 45 54 79 70 65 2c key, u8 *pEType,
14462 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b 0a 20 Pgno *pPgno){.
14463 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 DbPage *pDbPage
14464 3b 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 ; /* The point
14465 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 er map page */.
14466 20 69 6e 74 20 69 50 74 72 6d 61 70 3b 20 20 20 int iPtrmap;
14467 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d /* Pointer m
14468 61 70 20 70 61 67 65 20 69 6e 64 65 78 20 2a 2f ap page index */
14469 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 . u8 *pPtrmap;
1446a 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
1446b 20 6d 61 70 20 70 61 67 65 20 64 61 74 61 20 2a map page data *
1446c 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 /. int offset;
1446d 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
1446e 20 6f 66 20 65 6e 74 72 79 20 69 6e 20 70 6f 69 of entry in poi
1446f 6e 74 65 72 20 6d 61 70 20 2a 2f 0a 20 20 69 6e nter map */. in
14470 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
14471 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14472 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
14473 29 3b 0a 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 );.. iPtrmap =
14474 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 PTRMAP_PAGENO(pB
14475 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 t, key);. rc =
14476 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
14477 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 pBt->pPager, iPt
14478 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b rmap, &pDbPage);
14479 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a . if( rc!=0 ){.
1447a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1447b 20 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 }. pPtrmap = (
1447c 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 u8 *)sqlite3Page
1447d 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 rGetData(pDbPage
1447e 29 3b 0a 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 );.. offset = P
1447f 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 TRMAP_PTROFFSET(
14480 69 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 iPtrmap, key);.
14481 20 61 73 73 65 72 74 28 20 70 45 54 79 70 65 21 assert( pEType!
14482 3d 30 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20 =0 );. *pEType
14483 3d 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 = pPtrmap[offset
14484 5d 3b 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29 ];. if( pPgno )
14485 20 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 *pPgno = get4by
14486 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 te(&pPtrmap[offs
14487 65 74 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 et+1]);.. sqlit
14488 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 e3PagerUnref(pDb
14489 50 61 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45 Page);. if( *pE
1448a 54 79 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70 Type<1 || *pETyp
1448b 65 3e 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c e>5 ) return SQL
1448c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1448d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1448e 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f E_OK;.}..#else /
1448f 2a 20 69 66 20 64 65 66 69 6e 65 64 20 53 51 4c * if defined SQL
14490 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
14491 55 55 4d 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 UUM */. #define
14492 20 70 74 72 6d 61 70 50 75 74 28 77 2c 78 2c 79 ptrmapPut(w,x,y
14493 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 ,z) SQLITE_OK.
14494 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 47 65 #define ptrmapGe
14495 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54 t(w,x,y,z) SQLIT
14496 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70 E_OK. #define p
14497 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 79 2c 7a trmapPutOvfl(y,z
14498 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 ) SQLITE_OK.#end
14499 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 if../*.** Given
1449a 61 20 62 74 72 65 65 20 70 61 67 65 20 61 6e 64 a btree page and
1449b 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20 28 30 a cell index (0
1449c 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72 73 74 means the first
1449d 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 cell on.** the
1449e 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20 74 68 page, 1 means th
1449f 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 e second cell, a
144a0 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72 65 74 nd so forth) ret
144a1 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a urn a pointer.**
144a2 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e to the cell con
144a3 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tent..**.** This
144a4 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6f routine works o
144a5 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20 74 68 nly for pages th
144a6 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 at do not contai
144a7 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 n overflow cells
144a8 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6e ..*/.#define fin
144a9 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 20 28 dCell(P,I) \. (
144aa 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28 28 50 (P)->aData + ((P
144ab 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 67 65 )->maskPage & ge
144ac 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61 44 61 t2byte(&(P)->aDa
144ad 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 66 73 ta[(P)->cellOffs
144ae 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a 2f 2a et+2*(I)])))../*
144af 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20 .** This a more
144b0 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20 complex version
144b1 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 68 of findCell() th
144b2 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20 at works for.**
144b3 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 63 6f pages that do co
144b4 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 ntain overflow c
144b5 65 6c 6c 73 2e 20 20 53 65 65 20 69 6e 73 65 72 ells. See inser
144b6 74 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 2a t.*/.static u8 *
144b7 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c findOverflowCell
144b8 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
144b9 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 69 int iCell){. i
144ba 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 nt i;. assert(
144bb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
144bc 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
144bd 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 utex) );. for(i
144be 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f =pPage->nOverflo
144bf 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b w-1; i>=0; i--){
144c0 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 . int k;.
144c1 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c struct _OvflCell
144c2 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 70 4f 76 *pOvfl;. pOv
144c3 66 6c 20 3d 20 26 70 50 61 67 65 2d 3e 61 4f 76 fl = &pPage->aOv
144c4 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d 20 70 fl[i];. k = p
144c5 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20 20 20 20 69 Ovfl->idx;. i
144c6 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b 0a 20 f( k<=iCell ){.
144c7 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43 65 6c if( k==iCel
144c8 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 l ){. ret
144c9 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 43 65 6c 6c urn pOvfl->pCell
144ca 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
144cb 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d 0a 20 iCell--;. }.
144cc 20 7d 0a 20 20 72 65 74 75 72 6e 20 66 69 6e 64 }. return find
144cd 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c Cell(pPage, iCel
144ce 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 l);.}../*.** Par
144cf 73 65 20 61 20 63 65 6c 6c 20 63 6f 6e 74 65 6e se a cell conten
144d0 74 20 62 6c 6f 63 6b 20 61 6e 64 20 66 69 6c 6c t block and fill
144d1 20 69 6e 20 74 68 65 20 43 65 6c 6c 49 6e 66 6f in the CellInfo
144d2 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 structure. The
144d3 72 65 0a 2a 2a 20 61 72 65 20 74 77 6f 20 76 65 re.** are two ve
144d4 72 73 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 66 rsions of this f
144d5 75 6e 63 74 69 6f 6e 2e 20 20 73 71 6c 69 74 65 unction. sqlite
144d6 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 3BtreeParseCell(
144d7 29 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 ) takes a .** ce
144d8 6c 6c 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 ll index as the
144d9 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
144da 61 6e 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 and sqlite3Btree
144db 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 0a ParseCellPtr() .
144dc 2a 2a 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 ** takes a point
144dd 65 72 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f er to the body o
144de 66 20 74 68 65 20 63 65 6c 6c 20 61 73 20 69 74 f the cell as it
144df 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e s second argumen
144e0 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 t..**.** Within
144e1 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 20 70 this file, the p
144e2 61 72 73 65 43 65 6c 6c 28 29 20 6d 61 63 72 6f arseCell() macro
144e3 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 69 can be called i
144e4 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 73 71 6c nstead of.** sql
144e5 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
144e6 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20 73 llPtr(). Using s
144e7 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 74 ome compilers, t
144e8 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73 74 his will be fast
144e9 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
144ea 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
144eb 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c e3BtreeParseCell
144ec 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a Ptr(. MemPage *
144ed 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f pPage, /
144ee 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e * Page containin
144ef 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 g the cell */.
144f0 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 u8 *pCell,
144f1 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
144f2 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 74 er to the cell t
144f3 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e ext. */. CellIn
144f4 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 fo *pInfo
144f5 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 /* Fill in thi
144f6 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 s structure */.)
144f7 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20 20 20 {. u16 n;
144f8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
144f9 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e 20 63 umber bytes in c
144fa 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64 ell content head
144fb 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 er */. u32 nPay
144fc 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20 20 20 load;
144fd 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
144fe 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79 6c 6f es of cell paylo
144ff 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 ad */.. assert(
14500 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14501 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
14502 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 49 6e mutex) );.. pIn
14503 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 65 6c fo->pCell = pCel
14504 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 l;. assert( pPa
14505 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 70 ge->leaf==0 || p
14506 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b Page->leaf==1 );
14507 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e 63 68 . n = pPage->ch
14508 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 61 73 ildPtrSize;. as
14509 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 50 61 sert( n==4-4*pPa
1450a 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 69 66 ge->leaf );. if
1450b 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
1450c 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 ){. if( pPage
1450d 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 ->hasData ){.
1450e 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e n += getVarin
1450f 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e t32(&pCell[n], n
14510 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 7d 65 Payload);. }e
14511 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 79 6c lse{. nPayl
14512 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 oad = 0;. }.
14513 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e n += getVarin
14514 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 75 36 t(&pCell[n], (u6
14515 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 4*)&pInfo->nKey)
14516 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 ;. pInfo->nDa
14517 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 ta = nPayload;.
14518 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 }else{. pInf
14519 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 o->nData = 0;.
1451a 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 n += getVarint
1451b 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 32(&pCell[n], nP
1451c 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 49 6e ayload);. pIn
1451d 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 79 6c fo->nKey = nPayl
1451e 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e 66 6f oad;. }. pInfo
1451f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 50 61 ->nPayload = nPa
14520 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e yload;. pInfo->
14521 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20 20 69 nHeader = n;. i
14522 66 28 20 6c 69 6b 65 6c 79 28 6e 50 61 79 6c 6f f( likely(nPaylo
14523 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ad<=pPage->maxLo
14524 63 61 6c 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 cal) ){. /* T
14525 68 69 73 20 69 73 20 74 68 65 20 28 65 61 73 79 his is the (easy
14526 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 ) common case wh
14527 65 72 65 20 74 68 65 20 65 6e 74 69 72 65 20 70 ere the entire p
14528 61 79 6c 6f 61 64 20 66 69 74 73 0a 20 20 20 20 ayload fits.
14529 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 ** on the local
1452a 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72 66 6c page. No overfl
1452b 6f 77 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a ow is required..
1452c 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e */. int n
1452d 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 2f Size; /
1452e 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 * Total size of
1452f 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 cell content in
14530 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 6e 53 69 bytes */. nSi
14531 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 64 20 2b 20 ze = nPayload +
14532 6e 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c n;. pInfo->nL
14533 6f 63 61 6c 20 3d 20 28 75 31 36 29 6e 50 61 79 ocal = (u16)nPay
14534 6c 6f 61 64 3b 0a 20 20 20 20 70 49 6e 66 6f 2d load;. pInfo-
14535 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a >iOverflow = 0;.
14536 20 20 20 20 69 66 28 20 28 6e 53 69 7a 65 20 26 if( (nSize &
14537 20 7e 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ~3)==0 ){.
14538 20 6e 53 69 7a 65 20 3d 20 34 3b 20 20 20 20 20 nSize = 4;
14539 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 /* Minimum ce
1453a 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a ll size is 4 */.
1453b 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d }. pInfo-
1453c 3e 6e 53 69 7a 65 20 3d 20 28 75 31 36 29 6e 53 >nSize = (u16)nS
1453d 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ize;. }else{.
1453e 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 79 6c /* If the payl
1453f 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 oad will not fit
14540 20 63 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 74 completely on t
14541 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2c 20 77 he local page, w
14542 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f e have. ** to
14543 20 64 65 63 69 64 65 20 68 6f 77 20 6d 75 63 68 decide how much
14544 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c to store locall
14545 79 20 61 6e 64 20 68 6f 77 20 6d 75 63 68 20 74 y and how much t
14546 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 20 20 o spill onto.
14547 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ** overflow pag
14548 65 73 2e 20 20 54 68 65 20 73 74 72 61 74 65 67 es. The strateg
14549 79 20 69 73 20 74 6f 20 6d 69 6e 69 6d 69 7a 65 y is to minimize
1454a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 75 the amount of u
1454b 6e 75 73 65 64 0a 20 20 20 20 2a 2a 20 73 70 61 nused. ** spa
1454c 63 65 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 ce on overflow p
1454d 61 67 65 73 20 77 68 69 6c 65 20 6b 65 65 70 69 ages while keepi
1454e 6e 67 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 ng the amount of
1454f 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 0a 20 local storage.
14550 20 20 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e ** in between
14551 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d 61 minLocal and ma
14552 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a 0a 20 xLocal.. **.
14553 20 20 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 20 ** Warning:
14554 63 68 61 6e 67 69 6e 67 20 74 68 65 20 77 61 79 changing the way
14555 20 6f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 overflow payloa
14556 64 20 69 73 20 64 69 73 74 72 69 62 75 74 65 64 d is distributed
14557 20 69 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 77 in any. ** w
14558 61 79 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 ay will result i
14559 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c n an incompatibl
1455a 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 20 e file format..
1455b 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 69 */. int mi
1455c 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e 69 nLocal; /* Mini
1455d 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 mum amount of pa
1455e 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c yload held local
1455f 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61 ly */. int ma
14560 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 78 69 xLocal; /* Maxi
14561 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 mum amount of pa
14562 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c yload held local
14563 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 75 ly */. int su
14564 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 65 72 rplus; /* Over
14565 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 61 76 61 flow payload ava
14566 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f 63 61 6c ilable for local
14567 20 73 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 20 20 storage */..
14568 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 minLocal = pPag
14569 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 e->minLocal;.
1456a 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 67 maxLocal = pPag
1456b 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20 e->maxLocal;.
1456c 20 73 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c 6f surplus = minLo
1456d 63 61 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 64 20 cal + (nPayload
1456e 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 50 61 - minLocal)%(pPa
1456f 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
14570 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 69 66 ize - 4);. if
14571 28 20 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 ( surplus <= max
14572 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 Local ){. p
14573 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 Info->nLocal = (
14574 75 31 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 u16)surplus;.
14575 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 }else{. pI
14576 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 nfo->nLocal = (u
14577 31 36 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 16)minLocal;.
14578 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f }. pInfo->iO
14579 76 65 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 verflow = (u16)(
1457a 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 pInfo->nLocal +
1457b 6e 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e n);. pInfo->n
1457c 53 69 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f Size = pInfo->iO
1457d 76 65 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d verflow + 4;. }
1457e 0a 7d 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 .}.#define parse
1457f 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c Cell(pPage, iCel
14580 6c 2c 20 70 49 6e 66 6f 29 20 5c 0a 20 20 73 71 l, pInfo) \. sq
14581 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 lite3BtreeParseC
14582 65 6c 6c 50 74 72 28 28 70 50 61 67 65 29 2c 20 ellPtr((pPage),
14583 66 69 6e 64 43 65 6c 6c 28 28 70 50 61 67 65 29 findCell((pPage)
14584 2c 20 28 69 43 65 6c 6c 29 29 2c 20 28 70 49 6e , (iCell)), (pIn
14585 66 6f 29 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 fo)).SQLITE_PRIV
14586 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
14587 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 0a BtreeParseCell(.
14588 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
14589 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 , /* Pag
1458a 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
1458b 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 cell */. int i
1458c 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 Cell,
1458d 20 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 69 /* The cell i
1458e 6e 64 65 78 2e 20 20 46 69 72 73 74 20 63 65 6c ndex. First cel
1458f 6c 20 69 73 20 30 20 2a 2f 0a 20 20 43 65 6c 6c l is 0 */. Cell
14590 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 Info *pInfo
14591 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 /* Fill in t
14592 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f his structure */
14593 0a 29 7b 0a 20 20 70 61 72 73 65 43 65 6c 6c 28 .){. parseCell(
14594 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 pPage, iCell, pI
14595 6e 66 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 nfo);.}../*.** C
14596 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 61 6c ompute the total
14597 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
14598 20 74 68 61 74 20 61 20 43 65 6c 6c 20 6e 65 65 that a Cell nee
14599 64 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a ds in the cell.*
1459a 2a 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 74 * data area of t
1459b 68 65 20 62 74 72 65 65 2d 70 61 67 65 2e 20 20 he btree-page.
1459c 54 68 65 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 The return numbe
1459d 72 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 63 r includes the c
1459e 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 68 65 61 64 ell.** data head
1459f 65 72 20 61 6e 64 20 74 68 65 20 6c 6f 63 61 6c er and the local
145a0 20 70 61 79 6c 6f 61 64 2c 20 62 75 74 20 6e 6f payload, but no
145a1 74 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 t any overflow p
145a2 61 67 65 20 6f 72 0a 2a 2a 20 74 68 65 20 73 70 age or.** the sp
145a3 61 63 65 20 75 73 65 64 20 62 79 20 74 68 65 20 ace used by the
145a4 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f cell pointer..*/
145a5 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
145a6 73 74 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 static u16 cellS
145a7 69 7a 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 ize(MemPage *pPa
145a8 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a ge, int iCell){.
145a9 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b CellInfo info;
145aa 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 . sqlite3BtreeP
145ab 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 arseCell(pPage,
145ac 69 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 iCell, &info);.
145ad 20 72 65 74 75 72 6e 20 69 6e 66 6f 2e 6e 53 69 return info.nSi
145ae 7a 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 73 74 61 ze;.}.#endif.sta
145af 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 tic u16 cellSize
145b0 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 Ptr(MemPage *pPa
145b1 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a ge, u8 *pCell){.
145b2 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b CellInfo info;
145b3 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 . sqlite3BtreeP
145b4 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
145b5 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 e, pCell, &info)
145b6 3b 0a 20 20 72 65 74 75 72 6e 20 69 6e 66 6f 2e ;. return info.
145b7 6e 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 nSize;.}..#ifnde
145b8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
145b9 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 TOVACUUM./*.** I
145ba 66 20 74 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c f the cell pCell
145bb 2c 20 70 61 72 74 20 6f 66 20 70 61 67 65 20 70 , part of page p
145bc 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 Page contains a
145bd 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e pointer.** to an
145be 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 overflow page,
145bf 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 79 20 insert an entry
145c0 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 into the pointer
145c1 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 -map.** for the
145c2 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a overflow page..*
145c3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 /.static int ptr
145c4 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 mapPutOvflPtr(Me
145c5 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 mPage *pPage, u8
145c6 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c *pCell){. Cell
145c7 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 61 73 73 Info info;. ass
145c8 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b ert( pCell!=0 );
145c9 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 . sqlite3BtreeP
145ca 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
145cb 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 e, pCell, &info)
145cc 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 ;. assert( (inf
145cd 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e o.nData+(pPage->
145ce 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b intKey?0:info.nK
145cf 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c ey))==info.nPayl
145d0 6f 61 64 20 29 3b 0a 20 20 69 66 28 20 28 69 6e oad );. if( (in
145d1 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d fo.nData+(pPage-
145d2 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e >intKey?0:info.n
145d3 4b 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 Key))>info.nLoca
145d4 6c 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76 l ){. Pgno ov
145d5 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 fl = get4byte(&p
145d6 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 Cell[info.iOverf
145d7 6c 6f 77 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 low]);. retur
145d8 6e 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 n ptrmapPut(pPag
145d9 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 e->pBt, ovfl, PT
145da 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 RMAP_OVERFLOW1,
145db 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 pPage->pgno);.
145dc 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
145dd 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 49 66 E_OK;.}./*.** If
145de 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69 the cell with i
145df 6e 64 65 78 20 69 43 65 6c 6c 20 6f 6e 20 70 61 ndex iCell on pa
145e0 67 65 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e ge pPage contain
145e1 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 s a pointer.** t
145e2 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 o an overflow pa
145e3 67 65 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e ge, insert an en
145e4 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 try into the poi
145e5 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 nter-map.** for
145e6 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 the overflow pag
145e7 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
145e8 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 4d ptrmapPutOvfl(M
145e9 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
145ea 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38 20 nt iCell){. u8
145eb 2a 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 *pCell;. assert
145ec 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
145ed 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
145ee 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 65 >mutex) );. pCe
145ef 6c 6c 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f ll = findOverflo
145f0 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 wCell(pPage, iCe
145f1 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 74 ll);. return pt
145f2 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 rmapPutOvflPtr(p
145f3 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 7d 0a Page, pCell);.}.
145f4 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 #endif.../*.** D
145f5 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 efragment the pa
145f6 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43 ge given. All C
145f7 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 74 ells are moved t
145f8 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 o the.** end of
145f9 74 68 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c the page and all
145fa 20 66 72 65 65 20 73 70 61 63 65 20 69 73 20 63 free space is c
145fb 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e ollected into on
145fc 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42 6c 6b e.** big FreeBlk
145fd 20 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 that occurs in
145fe 62 65 74 77 65 65 6e 20 74 68 65 20 68 65 61 64 between the head
145ff 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 er and cell.** p
14600 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61 6e 64 ointer array and
14601 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
14602 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 69 t area..*/.stati
14603 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65 6e 74 c int defragment
14604 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 Page(MemPage *pP
14605 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 age){. int i;
14606 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14607 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
14608 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 er */. int pc;
14609 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1460a 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
1460b 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a a i-th cell */.
1460c 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 int addr;
1460d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1460e 4f 66 66 73 65 74 20 6f 66 20 66 69 72 73 74 20 Offset of first
1460f 62 79 74 65 20 61 66 74 65 72 20 63 65 6c 6c 20 byte after cell
14610 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f pointer array */
14611 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 . int hdr;
14612 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14613 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 70 Offset to the p
14614 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 age header */.
14615 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20 int size;
14616 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
14617 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a ze of a cell */.
14618 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 int usableSize
14619 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1461a 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 Number of usable
1461b 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 bytes on a page
1461c 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 */. int cellOf
1461d 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 fset;
1461e 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 /* Offset to th
1461f 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 e cell pointer a
14620 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62 rray */. int cb
14621 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rk;
14622 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
14623 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 o the cell conte
14624 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 nt area */. int
14625 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 nCell;
14626 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
14627 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 r of cells on th
14628 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 e page */. unsi
14629 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b gned char *data;
1462a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
1462b 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73 ge data */. uns
1462c 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70 igned char *temp
1462d 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 ; /* Temp
1462e 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f area for cell co
1462f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 ntent */.. asse
14630 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
14631 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
14632 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
14633 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
14634 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 pBt!=0 );. asse
14635 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e rt( pPage->pBt->
14636 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 51 usableSize <= SQ
14637 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
14638 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ZE );. assert(
14639 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
1463a 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
1463b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1463c 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
1463d 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d 70 mutex) );. temp
1463e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 = sqlite3PagerT
1463f 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d 3e empSpace(pPage->
14640 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
14641 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
14642 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 ata;. hdr = pPa
14643 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 ge->hdrOffset;.
14644 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 cellOffset = pP
14645 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b age->cellOffset;
14646 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 . nCell = pPage
14647 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 ->nCell;. asser
14648 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 79 t( nCell==get2by
14649 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 te(&data[hdr+3])
1464a 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 );. usableSize
1464b 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 = pPage->pBt->u
1464c 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 72 sableSize;. cbr
1464d 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 k = get2byte(&da
1464e 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 ta[hdr+5]);. me
1464f 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b 5d mcpy(&temp[cbrk]
14650 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 75 , &data[cbrk], u
14651 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 6b sableSize - cbrk
14652 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 62 );. cbrk = usab
14653 6c 65 53 69 7a 65 3b 0a 20 20 66 6f 72 28 69 3d leSize;. for(i=
14654 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 0; i<nCell; i++)
14655 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b {. u8 *pAddr;
14656 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 /* The i-th
14657 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f cell pointer */
14658 0a 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 61 . pAddr = &da
14659 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 ta[cellOffset +
1465a 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 i*2];. pc = g
1465b 65 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a et2byte(pAddr);.
1465c 20 20 20 20 69 66 28 20 70 63 3e 3d 75 73 61 62 if( pc>=usab
1465d 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 leSize ){.
1465e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
1465f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
14660 7d 0a 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c }. size = cel
14661 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 lSizePtr(pPage,
14662 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 20 &temp[pc]);.
14663 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 cbrk -= size;.
14664 20 20 69 66 28 20 63 62 72 6b 3c 63 65 6c 6c 4f if( cbrk<cellO
14665 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 7c 7c ffset+2*nCell ||
14666 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 pc+size>usableS
14667 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ize ){. ret
14668 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
14669 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 PT_BKPT;. }.
1466a 20 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 2b assert( cbrk+
1466b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 size<=usableSize
1466c 20 26 26 20 63 62 72 6b 3e 3d 30 20 29 3b 0a 20 && cbrk>=0 );.
1466d 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b memcpy(&data[
1466e 63 62 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d cbrk], &temp[pc]
1466f 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 70 75 74 , size);. put
14670 32 62 79 74 65 28 70 41 64 64 72 2c 20 63 62 72 2byte(pAddr, cbr
14671 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 k);. }. assert
14672 28 20 63 62 72 6b 3e 3d 63 65 6c 6c 4f 66 66 73 ( cbrk>=cellOffs
14673 65 74 2b 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20 20 et+2*nCell );.
14674 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 put2byte(&data[h
14675 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20 20 dr+5], cbrk);.
14676 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30 3b data[hdr+1] = 0;
14677 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20 3d . data[hdr+2] =
14678 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 0;. data[hdr+7
14679 5d 20 3d 20 30 3b 0a 20 20 61 64 64 72 20 3d 20 ] = 0;. addr =
1467a 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65 cellOffset+2*nCe
1467b 6c 6c 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 ll;. memset(&da
1467c 74 61 5b 61 64 64 72 5d 2c 20 30 2c 20 63 62 72 ta[addr], 0, cbr
1467d 6b 2d 61 64 64 72 29 3b 0a 20 20 61 73 73 65 72 k-addr);. asser
1467e 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
1467f 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
14680 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
14681 69 66 28 20 63 62 72 6b 2d 61 64 64 72 21 3d 70 if( cbrk-addr!=p
14682 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 Page->nFree ){.
14683 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
14684 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
14685 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
14686 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
14687 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 Allocate nByte b
14688 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 6f 6e ytes of space on
14689 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 52 a page..**.** R
1468a 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 eturn the index
1468b 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 into pPage->aDat
1468c 61 5b 5d 20 6f 66 20 74 68 65 20 66 69 72 73 74 a[] of the first
1468d 20 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 byte of.** the
1468e 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 new allocation.
1468f 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 The caller guar
14690 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 72 antees that ther
14691 65 20 69 73 20 65 6e 6f 75 67 68 0a 2a 2a 20 73 e is enough.** s
14692 70 61 63 65 2e 20 20 54 68 69 73 20 72 6f 75 74 pace. This rout
14693 69 6e 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 66 ine will never f
14694 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ail..**.** If th
14695 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 e page contains
14696 6e 42 79 74 65 73 20 6f 66 20 66 72 65 65 20 73 nBytes of free s
14697 70 61 63 65 20 62 75 74 20 64 6f 65 73 20 6e 6f pace but does no
14698 74 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 42 79 t contain.** nBy
14699 74 65 73 20 6f 66 20 63 6f 6e 74 69 67 75 6f 75 tes of contiguou
1469a 73 20 66 72 65 65 20 73 70 61 63 65 2c 20 74 68 s free space, th
1469b 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
1469c 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a automatically.**
1469d 20 63 61 6c 6c 73 20 64 65 66 72 61 67 65 6d 65 calls defrageme
1469e 6e 74 50 61 67 65 28 29 20 74 6f 20 63 6f 6e 73 ntPage() to cons
1469f 6f 6c 69 64 61 74 65 20 61 6c 6c 20 66 72 65 65 olidate all free
146a0 20 73 70 61 63 65 20 62 65 66 6f 72 65 20 0a 2a space before .*
146a1 2a 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65 * allocating the
146a2 20 6e 65 77 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 73 new chunk..*/.s
146a3 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 tatic int alloca
146a4 74 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 teSpace(MemPage
146a5 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e 42 79 74 *pPage, int nByt
146a6 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c 20 e){. int addr,
146a7 70 63 2c 20 68 64 72 3b 0a 20 20 69 6e 74 20 73 pc, hdr;. int s
146a8 69 7a 65 3b 0a 20 20 69 6e 74 20 6e 46 72 61 67 ize;. int nFrag
146a9 3b 0a 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 69 ;. int top;. i
146aa 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 nt nCell;. int
146ab 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 75 6e cellOffset;. un
146ac 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 signed char *dat
146ad 61 3b 0a 20 20 0a 20 20 64 61 74 61 20 3d 20 70 a;. . data = p
146ae 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 61 Page->aData;. a
146af 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
146b0 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
146b1 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
146b2 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
146b3 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 e->pBt );. asse
146b4 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
146b5 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
146b6 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
146b7 73 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 ssert( nByte>=0
146b8 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 ); /* Minimum c
146b9 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f ell size is 4 */
146ba 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
146bb 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 ->nFree>=nByte )
146bc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
146bd 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 e->nOverflow==0
146be 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 );. pPage->nFre
146bf 65 20 2d 3d 20 28 75 31 36 29 6e 42 79 74 65 3b e -= (u16)nByte;
146c0 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e . hdr = pPage->
146c1 68 64 72 4f 66 66 73 65 74 3b 0a 0a 20 20 6e 46 hdrOffset;.. nF
146c2 72 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 rag = data[hdr+7
146c3 5d 3b 0a 20 20 69 66 28 20 6e 46 72 61 67 3c 36 ];. if( nFrag<6
146c4 30 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 61 72 0 ){. /* Sear
146c5 63 68 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 ch the freelist
146c6 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 73 6c looking for a sl
146c7 6f 74 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f ot big enough to
146c8 20 73 61 74 69 73 66 79 20 74 68 65 0a 20 20 20 satisfy the.
146c9 20 2a 2a 20 73 70 61 63 65 20 72 65 71 75 65 73 ** space reques
146ca 74 2e 20 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d t. */. addr =
146cb 20 68 64 72 2b 31 3b 0a 20 20 20 20 77 68 69 6c hdr+1;. whil
146cc 65 28 20 28 70 63 20 3d 20 67 65 74 32 62 79 74 e( (pc = get2byt
146cd 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e e(&data[addr]))>
146ce 30 20 29 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 0 ){. size
146cf 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
146d0 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 [pc+2]);. i
146d1 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29 f( size>=nByte )
146d2 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 {. int x
146d3 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b 0a = size - nByte;.
146d4 20 20 20 20 20 20 20 20 69 66 28 20 73 69 7a 65 if( size
146d5 3c 6e 42 79 74 65 2b 34 20 29 7b 0a 20 20 20 20 <nByte+4 ){.
146d6 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 memcpy(&da
146d7 74 61 5b 61 64 64 72 5d 2c 20 26 64 61 74 61 5b ta[addr], &data[
146d8 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 pc], 2);.
146d9 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d data[hdr+7] =
146da 20 28 75 38 29 28 6e 46 72 61 67 20 2b 20 78 29 (u8)(nFrag + x)
146db 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
146dc 72 6e 20 70 63 3b 0a 20 20 20 20 20 20 20 20 7d rn pc;. }
146dd 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
146de 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 put2byte(&data[p
146df 63 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20 c+2], x);.
146e0 20 20 20 20 72 65 74 75 72 6e 20 70 63 20 2b 20 return pc +
146e1 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 x;. }.
146e2 20 20 20 7d 0a 20 20 20 20 20 20 61 64 64 72 20 }. addr
146e3 3d 20 70 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = pc;. }. }.
146e4 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d . /* Allocate m
146e5 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 67 emory from the g
146e6 61 70 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 ap in between th
146e7 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 e cell pointer a
146e8 72 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 rray. ** and th
146e9 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 e cell content a
146ea 72 65 61 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 rea.. */. top
146eb 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
146ec 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6e 43 65 6c [hdr+5]);. nCel
146ed 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 l = get2byte(&da
146ee 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 63 65 ta[hdr+3]);. ce
146ef 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 llOffset = pPage
146f0 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 ->cellOffset;.
146f1 69 66 28 20 6e 46 72 61 67 3e 3d 36 30 20 7c 7c if( nFrag>=60 ||
146f2 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a cellOffset + 2*
146f3 6e 43 65 6c 6c 20 3e 20 74 6f 70 20 2d 20 6e 42 nCell > top - nB
146f4 79 74 65 20 29 7b 0a 20 20 20 20 64 65 66 72 61 yte ){. defra
146f5 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 gmentPage(pPage)
146f6 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 ;. top = get2
146f7 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
146f8 5d 29 3b 0a 20 20 7d 0a 20 20 74 6f 70 20 2d 3d ]);. }. top -=
146f9 20 6e 42 79 74 65 3b 0a 20 20 61 73 73 65 72 74 nByte;. assert
146fa 28 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 ( cellOffset + 2
146fb 2a 6e 43 65 6c 6c 20 3c 3d 20 74 6f 70 20 29 3b *nCell <= top );
146fc 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
146fd 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a a[hdr+5], top);.
146fe 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
146ff 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
14700 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
14701 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 74 6f ) );. return to
14702 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 p;.}../*.** Retu
14703 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 rn a section of
14704 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61 the pPage->aData
14705 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 to the freelist
14706 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 62 ..** The first b
14707 79 74 65 20 6f 66 20 74 68 65 20 6e 65 77 20 66 yte of the new f
14708 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 ree block is pPa
14709 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d ge->aDisk[start]
1470a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69 7a 65 .** and the size
1470b 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 69 73 of the block is
1470c 20 22 73 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a "size" bytes..*
1470d 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 *.** Most of the
1470e 20 65 66 66 6f 72 74 20 68 65 72 65 20 69 73 20 effort here is
1470f 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c involved in coal
14710 65 73 69 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a esing adjacent.*
14711 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e * free blocks in
14712 74 6f 20 61 20 73 69 6e 67 6c 65 20 62 69 67 20 to a single big
14713 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 free block..*/.s
14714 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 53 70 tatic int freeSp
14715 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 ace(MemPage *pPa
14716 67 65 2c 20 69 6e 74 20 73 74 61 72 74 2c 20 69 ge, int start, i
14717 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 nt size){. int
14718 61 64 64 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 addr, pbegin, hd
14719 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 r;. unsigned ch
1471a 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65 ar *data = pPage
1471b 2d 3e 61 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 ->aData;.. asse
1471c 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d rt( pPage->pBt!=
1471d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
1471e 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1471f 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
14720 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
14721 72 74 28 20 73 74 61 72 74 3e 3d 70 50 61 67 65 rt( start>=pPage
14722 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b 28 70 ->hdrOffset+6+(p
14723 50 61 67 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 20 Page->leaf?0:4)
14724 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 74 );. assert( (st
14725 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61 art + size)<=pPa
14726 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
14727 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ize );. assert(
14728 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14729 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
1472a 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
1472b 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20 rt( size>=0 );
1472c 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c /* Minimum cell
1472d 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23 size is 4 */..#
1472e 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 ifdef SQLITE_SEC
1472f 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 URE_DELETE. /*
14730 4f 76 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 Overwrite delete
14731 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 d information wi
14732 74 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 th zeros when th
14733 65 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 e SECURE_DELETE
14734 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 . ** option is
14735 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 enabled at compi
14736 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d le-time */. mem
14737 73 65 74 28 26 64 61 74 61 5b 73 74 61 72 74 5d set(&data[start]
14738 2c 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 , 0, size);.#end
14739 69 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 if.. /* Add the
1473a 20 73 70 61 63 65 20 62 61 63 6b 20 69 6e 74 6f space back into
1473b 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 the linked list
1473c 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 20 2a of freeblocks *
1473d 2f 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d /. hdr = pPage-
1473e 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 61 64 >hdrOffset;. ad
1473f 64 72 20 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 dr = hdr + 1;.
14740 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d while( (pbegin =
14741 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
14742 61 64 64 72 5d 29 29 3c 73 74 61 72 74 20 26 26 addr]))<start &&
14743 20 70 62 65 67 69 6e 3e 30 20 29 7b 0a 20 20 20 pbegin>0 ){.
14744 20 61 73 73 65 72 74 28 20 70 62 65 67 69 6e 3c assert( pbegin<
14745 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 =pPage->pBt->usa
14746 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 bleSize-4 );.
14747 20 69 66 28 20 70 62 65 67 69 6e 3c 3d 61 64 64 if( pbegin<=add
14748 72 20 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 r ) {. retu
14749 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
1474a 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 T_BKPT;. }.
1474b 20 20 61 64 64 72 20 3d 20 70 62 65 67 69 6e 3b addr = pbegin;
1474c 0a 20 20 7d 0a 20 20 69 66 20 28 20 70 62 65 67 . }. if ( pbeg
1474d 69 6e 3e 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 in>pPage->pBt->u
1474e 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 20 7b 0a sableSize-4 ) {.
1474f 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
14750 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
14751 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 62 }. assert( pb
14752 65 67 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 egin>addr || pbe
14753 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 gin==0 );. put2
14754 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d byte(&data[addr]
14755 2c 20 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32 , start);. put2
14756 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 byte(&data[start
14757 5d 2c 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 ], pbegin);. pu
14758 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 t2byte(&data[sta
14759 72 74 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 rt+2], size);.
1475a 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 pPage->nFree +=
1475b 28 75 31 36 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a (u16)size;.. /*
1475c 20 43 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 Coalesce adjace
1475d 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a nt free blocks *
1475e 2f 0a 20 20 61 64 64 72 20 3d 20 70 50 61 67 65 /. addr = pPage
1475f 2d 3e 68 64 72 4f 66 66 73 65 74 20 2b 20 31 3b ->hdrOffset + 1;
14760 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 . while( (pbegi
14761 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 n = get2byte(&da
14762 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a ta[addr]))>0 ){.
14763 20 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 int pnext, p
14764 73 69 7a 65 2c 20 78 3b 0a 20 20 20 20 61 73 73 size, x;. ass
14765 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 ert( pbegin>addr
14766 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
14767 70 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 pbegin<=pPage->p
14768 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 Bt->usableSize-4
14769 20 29 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d 20 );. pnext =
1476a 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
1476b 62 65 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69 begin]);. psi
1476c 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 ze = get2byte(&d
1476d 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a ata[pbegin+2]);.
1476e 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 20 2b if( pbegin +
1476f 20 70 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e psize + 3 >= pn
14770 65 78 74 20 26 26 20 70 6e 65 78 74 3e 30 20 29 ext && pnext>0 )
14771 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 72 61 67 {. int frag
14772 20 3d 20 70 6e 65 78 74 20 2d 20 28 70 62 65 67 = pnext - (pbeg
14773 69 6e 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20 20 in+psize);.
14774 20 69 66 28 20 28 66 72 61 67 3c 30 29 20 7c 7c if( (frag<0) ||
14775 20 28 66 72 61 67 3e 28 69 6e 74 29 64 61 74 61 (frag>(int)data
14776 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
14777 74 2b 37 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 t+7]) ){.
14778 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
14779 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1477a 20 20 20 7d 0a 20 20 20 20 20 20 64 61 74 61 5b }. data[
1477b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
1477c 2b 37 5d 20 2d 3d 20 28 75 38 29 66 72 61 67 3b +7] -= (u8)frag;
1477d 0a 20 20 20 20 20 20 78 20 3d 20 67 65 74 32 62 . x = get2b
1477e 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74 5d yte(&data[pnext]
1477f 29 3b 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 );. put2byt
14780 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c e(&data[pbegin],
14781 20 78 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 70 x);. x = p
14782 6e 65 78 74 20 2b 20 67 65 74 32 62 79 74 65 28 next + get2byte(
14783 26 64 61 74 61 5b 70 6e 65 78 74 2b 32 5d 29 20 &data[pnext+2])
14784 2d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 20 20 - pbegin;.
14785 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 put2byte(&data[p
14786 62 65 67 69 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 begin+2], x);.
14787 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
14788 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 ddr = pbegin;.
14789 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
1478a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
1478b 74 20 61 72 65 61 20 62 65 67 69 6e 73 20 77 69 t area begins wi
1478c 74 68 20 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20 th a freeblock,
1478d 72 65 6d 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20 remove it. */.
1478e 69 66 28 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d if( data[hdr+1]=
1478f 3d 64 61 74 61 5b 68 64 72 2b 35 5d 20 26 26 20 =data[hdr+5] &&
14790 64 61 74 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 data[hdr+2]==dat
14791 61 5b 68 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20 a[hdr+6] ){.
14792 69 6e 74 20 74 6f 70 3b 0a 20 20 20 20 70 62 65 int top;. pbe
14793 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 gin = get2byte(&
14794 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 data[hdr+1]);.
14795 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 68 memcpy(&data[h
14796 64 72 2b 31 5d 2c 20 26 64 61 74 61 5b 70 62 65 dr+1], &data[pbe
14797 67 69 6e 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f gin], 2);. to
14798 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 p = get2byte(&da
14799 74 61 5b 68 64 72 2b 35 5d 29 20 2b 20 67 65 74 ta[hdr+5]) + get
1479a 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 2byte(&data[pbeg
1479b 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 70 75 74 32 in+2]);. put2
1479c 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
1479d 5d 2c 20 74 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 ], top);. }. a
1479e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
1479f 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
147a0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
147a1 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
147a2 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 E_OK;.}../*.** D
147a3 65 63 6f 64 65 20 74 68 65 20 66 6c 61 67 73 20 ecode the flags
147a4 62 79 74 65 20 28 74 68 65 20 66 69 72 73 74 20 byte (the first
147a5 62 79 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 byte of the head
147a6 65 72 29 20 66 6f 72 20 61 20 70 61 67 65 0a 2a er) for a page.*
147a7 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 * and initialize
147a8 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 4d fields of the M
147a9 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 emPage structure
147aa 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a accordingly..**
147ab 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c .** Only the fol
147ac 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 lowing combinati
147ad 6f 6e 73 20 61 72 65 20 73 75 70 70 6f 72 74 65 ons are supporte
147ae 64 2e 20 20 41 6e 79 74 68 69 6e 67 20 64 69 66 d. Anything dif
147af 66 65 72 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 ferent.** indica
147b0 74 65 73 20 61 20 63 6f 72 72 75 70 74 20 64 61 tes a corrupt da
147b1 74 61 62 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a tabase files:.**
147b2 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f .** PTF_
147b3 5a 45 52 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20 ZERODATA.**
147b4 20 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 PTF_ZERODATA
147b5 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 | PTF_LEAF.**
147b6 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44 PTF_LEAFD
147b7 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 ATA | PTF_INTKEY
147b8 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f .** PTF_
147b9 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 LEAFDATA | PTF_I
147ba 4e 54 4b 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 NTKEY | PTF_LEAF
147bb 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
147bc 65 63 6f 64 65 46 6c 61 67 73 28 4d 65 6d 50 61 ecodeFlags(MemPa
147bd 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 ge *pPage, int f
147be 6c 61 67 42 79 74 65 29 7b 0a 20 20 42 74 53 68 lagByte){. BtSh
147bf 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f ared *pBt; /
147c0 2a 20 41 20 63 6f 70 79 20 6f 66 20 70 50 61 67 * A copy of pPag
147c1 65 2d 3e 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73 e->pBt */.. ass
147c2 65 72 74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f ert( pPage->hdrO
147c3 66 66 73 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 ffset==(pPage->p
147c4 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 gno==1 ? 100 : 0
147c5 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
147c6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
147c7 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
147c8 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 2d tex) );. pPage-
147c9 3e 6c 65 61 66 20 3d 20 28 75 38 29 28 66 6c 61 >leaf = (u8)(fla
147ca 67 42 79 74 65 3e 3e 33 29 3b 20 20 61 73 73 65 gByte>>3); asse
147cb 72 74 28 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20 rt( PTF_LEAF ==
147cc 31 3c 3c 33 20 29 3b 0a 20 20 66 6c 61 67 42 79 1<<3 );. flagBy
147cd 74 65 20 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b te &= ~PTF_LEAF;
147ce 0a 20 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 . pPage->childP
147cf 74 72 53 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61 trSize = 4-4*pPa
147d0 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20 ge->leaf;. pBt
147d1 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
147d2 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 28 50 if( flagByte==(P
147d3 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 TF_LEAFDATA | PT
147d4 46 5f 49 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20 F_INTKEY) ){.
147d5 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d pPage->intKey =
147d6 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 1;. pPage->h
147d7 61 73 44 61 74 61 20 3d 20 70 50 61 67 65 2d 3e asData = pPage->
147d8 6c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d leaf;. pPage-
147d9 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d >maxLocal = pBt-
147da 3e 6d 61 78 4c 65 61 66 3b 0a 20 20 20 20 70 50 >maxLeaf;. pP
147db 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 age->minLocal =
147dc 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 pBt->minLeaf;.
147dd 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 42 79 }else if( flagBy
147de 74 65 3d 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41 te==PTF_ZERODATA
147df 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 ){. pPage->i
147e0 6e 74 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 ntKey = 0;. p
147e1 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 Page->hasData =
147e2 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 0;. pPage->ma
147e3 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 xLocal = pBt->ma
147e4 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 70 50 61 67 xLocal;. pPag
147e5 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 e->minLocal = pB
147e6 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d t->minLocal;. }
147e7 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
147e8 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
147e9 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 BKPT;. }. retu
147ea 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
147eb 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
147ec 65 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 e the auxiliary
147ed 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 information for
147ee 61 20 64 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a a disk block..**
147ef 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
147f0 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e E_OK on success.
147f1 20 20 49 66 20 77 65 20 73 65 65 20 74 68 61 74 If we see that
147f2 20 74 68 65 20 70 61 67 65 20 64 6f 65 73 0a 2a the page does.*
147f3 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 * not contain a
147f4 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 well-formed data
147f5 62 61 73 65 20 70 61 67 65 2c 20 74 68 65 6e 20 base page, then
147f6 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 return .** SQLIT
147f7 45 5f 43 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 E_CORRUPT. Note
147f8 20 74 68 61 74 20 61 20 72 65 74 75 72 6e 20 6f that a return o
147f9 66 20 53 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 f SQLITE_OK does
147fa 20 6e 6f 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 not.** guarante
147fb 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
147fc 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 is well-formed.
147fd 20 49 74 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 It only shows t
147fe 68 61 74 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 hat.** we failed
147ff 20 74 6f 20 64 65 74 65 63 74 20 61 6e 79 20 63 to detect any c
14800 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 orruption..*/.SQ
14801 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
14802 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 sqlite3BtreeIni
14803 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 tPage(MemPage *p
14804 50 61 67 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 Page){.. assert
14805 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 ( pPage->pBt!=0
14806 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
14807 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14808 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
14809 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
1480a 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c pPage->pgno==sql
1480b 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d ite3PagerPagenum
1480c 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ber(pPage->pDbPa
1480d 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
1480e 20 70 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 pPage == sqlite
1480f 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 3PagerGetExtra(p
14810 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
14811 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
14812 65 2d 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 e->aData == sqli
14813 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
14814 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
14815 29 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 );.. if( !pPage
14816 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 ->isInit ){.
14817 75 31 36 20 70 63 3b 20 20 20 20 20 20 20 20 20 u16 pc;
14818 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
14819 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 a freeblock wit
1481a 68 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 hin pPage->aData
1481b 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 [] */. u8 hdr
1481c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1481d 4f 66 66 73 65 74 20 74 6f 20 62 65 67 69 6e 6e Offset to beginn
1481e 69 6e 67 20 6f 66 20 70 61 67 65 20 68 65 61 64 ing of page head
1481f 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 er */. u8 *da
14820 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ta; /*
14821 45 71 75 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e Equal to pPage->
14822 61 44 61 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 aData */. BtS
14823 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 hared *pBt;
14824 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 /* The main b
14825 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a tree structure *
14826 2f 0a 20 20 20 20 75 31 36 20 75 73 61 62 6c 65 /. u16 usable
14827 53 69 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 Size; /* Amou
14828 6e 74 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61 nt of usable spa
14829 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 ce on each page
1482a 2a 2f 0a 20 20 20 20 75 31 36 20 63 65 6c 6c 4f */. u16 cellO
1482b 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 ffset; /* Off
1482c 73 65 74 20 66 72 6f 6d 20 73 74 61 72 74 20 6f set from start o
1482d 66 20 70 61 67 65 20 74 6f 20 66 69 72 73 74 20 f page to first
1482e 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a cell pointer */.
1482f 20 20 20 20 75 31 36 20 6e 46 72 65 65 3b 20 20 u16 nFree;
14830 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
14831 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 of unused bytes
14832 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a on the page */.
14833 20 20 20 20 75 31 36 20 74 6f 70 3b 20 20 20 20 u16 top;
14834 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
14835 62 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c byte of the cell
14836 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f content area */
14837 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50 61 67 .. pBt = pPag
14838 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68 64 72 e->pBt;.. hdr
14839 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
1483a 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 set;. data =
1483b 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 pPage->aData;.
1483c 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c 61 67 if( decodeFlag
1483d 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b 68 64 s(pPage, data[hd
1483e 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c r]) ) return SQL
1483f 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
14840 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
14841 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 t->pageSize>=512
14842 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a && pBt->pageSiz
14843 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 20 20 e<=32768 );.
14844 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 pPage->maskPage
14845 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 = pBt->pageSize
14846 2d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e - 1;. pPage->
14847 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 nOverflow = 0;.
14848 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 usableSize =
14849 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
1484a 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c . pPage->cell
1484b 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 Offset = cellOff
1484c 73 65 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d set = hdr + 12 -
1484d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 4*pPage->leaf;.
1484e 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 top = get2by
1484f 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
14850 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 ;. pPage->nCe
14851 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 ll = get2byte(&d
14852 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 ata[hdr+3]);.
14853 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c if( pPage->nCel
14854 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 l>MX_CELL(pBt) )
14855 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 6d 61 {. /* To ma
14856 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 20 73 ny cells for a s
14857 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54 68 65 ingle page. The
14858 20 70 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f page must be co
14859 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20 20 72 rrupt */. r
1485a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
1485b 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
1485c 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 . . /* Compu
1485d 74 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72 65 te the total fre
1485e 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70 e space on the p
1485f 61 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 age */. pc =
14860 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
14861 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65 dr+1]);. nFre
14862 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 e = data[hdr+7]
14863 2b 20 74 6f 70 20 2d 20 28 63 65 6c 6c 4f 66 66 + top - (cellOff
14864 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e set + 2*pPage->n
14865 43 65 6c 6c 29 3b 0a 20 20 20 20 77 68 69 6c 65 Cell);. while
14866 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 ( pc>0 ){.
14867 75 31 36 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a u16 next, size;.
14868 20 20 20 20 20 20 69 66 28 20 70 63 3e 75 73 61 if( pc>usa
14869 62 6c 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 bleSize-4 ){.
1486a 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f /* Free blo
1486b 63 6b 20 69 73 20 6f 66 66 20 74 68 65 20 70 61 ck is off the pa
1486c 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 ge */. re
1486d 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1486e 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 UPT_BKPT; .
1486f 20 7d 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 }. next =
14870 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
14871 63 5d 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 c]);. size
14872 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
14873 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 [pc+2]);. i
14874 66 28 20 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 f( next>0 && nex
14875 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 20 29 7b 0a t<=pc+size+3 ){.
14876 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 /* Free
14877 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69 blocks must be i
14878 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 n accending orde
14879 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 r */. ret
1487a 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
1487b 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 PT_BKPT; .
1487c 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d }. nFree +=
1487d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 size;. pc
1487e 3d 20 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 = next;. }.
1487f 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d pPage->nFree =
14880 20 28 75 31 36 29 6e 46 72 65 65 3b 0a 20 20 20 (u16)nFree;.
14881 20 69 66 28 20 6e 46 72 65 65 3e 3d 75 73 61 62 if( nFree>=usab
14882 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 leSize ){.
14883 2f 2a 20 46 72 65 65 20 73 70 61 63 65 20 63 61 /* Free space ca
14884 6e 6e 6f 74 20 65 78 63 65 65 64 20 74 6f 74 61 nnot exceed tota
14885 6c 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 l page size */.
14886 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
14887 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
14888 20 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 . }..#if 0.
14889 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 /* Check that a
1488a 6c 6c 20 74 68 65 20 6f 66 66 73 65 74 73 20 69 ll the offsets i
1488b 6e 20 74 68 65 20 63 65 6c 6c 20 6f 66 66 73 65 n the cell offse
1488c 74 20 61 72 72 61 79 20 61 72 65 20 77 69 74 68 t array are with
1488d 69 6e 20 72 61 6e 67 65 2e 20 0a 20 20 2a 2a 20 in range. . **
1488e 0a 20 20 2a 2a 20 4f 6d 69 74 74 69 6e 67 20 74 . ** Omitting t
1488f 68 69 73 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 his consistency
14890 63 68 65 63 6b 20 61 6e 64 20 75 73 69 6e 67 20 check and using
14891 74 68 65 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 the pPage->maskP
14892 61 67 65 20 6d 61 73 6b 0a 20 20 2a 2a 20 74 6f age mask. ** to
14893 20 70 72 65 76 65 6e 74 20 6f 76 65 72 72 75 6e prevent overrun
14894 6e 69 6e 67 20 74 68 65 20 70 61 67 65 20 62 75 ning the page bu
14895 66 66 65 72 20 69 6e 20 66 69 6e 64 43 65 6c 6c ffer in findCell
14896 28 29 20 72 65 73 75 6c 74 73 20 69 6e 20 61 0a () results in a.
14897 20 20 2a 2a 20 32 2e 35 25 20 70 65 72 66 6f 72 ** 2.5% perfor
14898 6d 61 6e 63 65 20 67 61 69 6e 2e 0a 20 20 2a 2f mance gain.. */
14899 0a 20 20 7b 0a 20 20 20 20 75 38 20 2a 70 4f 66 . {. u8 *pOf
1489a 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 f; /* Ite
1489b 72 61 74 6f 72 20 75 73 65 64 20 74 6f 20 63 68 rator used to ch
1489c 65 63 6b 20 61 6c 6c 20 63 65 6c 6c 20 6f 66 66 eck all cell off
1489d 73 65 74 73 20 61 72 65 20 69 6e 20 72 61 6e 67 sets are in rang
1489e 65 20 2a 2f 0a 20 20 20 20 75 38 20 2a 70 45 6e e */. u8 *pEn
1489f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 d; /* Poi
148a0 6e 74 65 72 20 74 6f 20 65 6e 64 20 6f 66 20 63 nter to end of c
148a1 65 6c 6c 20 6f 66 66 73 65 74 20 61 72 72 61 79 ell offset array
148a2 20 2a 2f 0a 20 20 20 20 75 38 20 6d 61 73 6b 3b */. u8 mask;
148a3 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b /* Mask
148a4 20 6f 66 20 62 69 74 73 20 74 68 61 74 20 6d 75 of bits that mu
148a5 73 74 20 62 65 20 7a 65 72 6f 20 69 6e 20 4d 53 st be zero in MS
148a6 42 20 6f 66 20 63 65 6c 6c 20 6f 66 66 73 65 74 B of cell offset
148a7 73 20 2a 2f 0a 20 20 20 20 6d 61 73 6b 20 3d 20 s */. mask =
148a8 7e 28 28 28 75 38 29 28 70 42 74 2d 3e 70 61 67 ~(((u8)(pBt->pag
148a9 65 53 69 7a 65 3e 3e 38 29 29 2d 31 29 3b 0a 20 eSize>>8))-1);.
148aa 20 20 20 70 45 6e 64 20 3d 20 26 64 61 74 61 5b pEnd = &data[
148ab 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 70 50 61 cellOffset + pPa
148ac 67 65 2d 3e 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 ge->nCell*2];.
148ad 20 20 66 6f 72 28 70 4f 66 66 3d 26 64 61 74 61 for(pOff=&data
148ae 5b 63 65 6c 6c 4f 66 66 73 65 74 5d 3b 20 70 4f [cellOffset]; pO
148af 66 66 21 3d 70 45 6e 64 20 26 26 20 21 28 28 2a ff!=pEnd && !((*
148b0 70 4f 66 66 29 26 6d 61 73 6b 29 3b 20 70 4f 66 pOff)&mask); pOf
148b1 66 2b 3d 32 29 3b 0a 20 20 20 20 69 66 28 20 70 f+=2);. if( p
148b2 4f 66 66 21 3d 70 45 6e 64 20 29 7b 0a 20 20 20 Off!=pEnd ){.
148b3 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
148b4 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
148b5 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
148b6 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e . pPage->isIn
148b7 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 it = 1;. }. re
148b8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
148b9 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 }../*.** Set up
148ba 61 20 72 61 77 20 70 61 67 65 20 73 6f 20 74 68 a raw page so th
148bb 61 74 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 at it looks like
148bc 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 a database page
148bd 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 holding.** no e
148be 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 ntries..*/.stati
148bf 63 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 c void zeroPage(
148c0 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
148c1 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e int flags){. un
148c2 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 signed char *dat
148c3 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 a = pPage->aData
148c4 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
148c5 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
148c6 20 20 75 38 20 68 64 72 20 3d 20 70 50 61 67 65 u8 hdr = pPage
148c7 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 ->hdrOffset;. u
148c8 31 36 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 16 first;.. ass
148c9 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
148ca 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 rPagenumber(pPag
148cb 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 e->pDbPage)==pPa
148cc 67 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 ge->pgno );. as
148cd 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
148ce 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 erGetExtra(pPage
148cf 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 ->pDbPage) == (v
148d0 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 oid*)pPage );.
148d1 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
148d2 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 agerGetData(pPag
148d3 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 64 e->pDbPage) == d
148d4 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ata );. assert(
148d5 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
148d6 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
148d7 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 pDbPage) );. as
148d8 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
148d9 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
148da 74 65 78 29 20 29 3b 0a 20 20 2f 2a 6d 65 6d 73 tex) );. /*mems
148db 65 74 28 26 64 61 74 61 5b 68 64 72 5d 2c 20 30 et(&data[hdr], 0
148dc 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a , pBt->usableSiz
148dd 65 20 2d 20 68 64 72 29 3b 2a 2f 0a 20 20 64 61 e - hdr);*/. da
148de 74 61 5b 68 64 72 5d 20 3d 20 28 63 68 61 72 29 ta[hdr] = (char)
148df 66 6c 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d flags;. first =
148e0 20 68 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 hdr + 8 + 4*((f
148e1 6c 61 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d lags&PTF_LEAF)==
148e2 30 20 3f 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 0 ?1:0);. memse
148e3 74 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 t(&data[hdr+1],
148e4 30 2c 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 0, 4);. data[hd
148e5 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 r+7] = 0;. put2
148e6 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
148e7 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 ], pBt->usableSi
148e8 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 ze);. pPage->nF
148e9 72 65 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c ree = pBt->usabl
148ea 65 53 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 eSize - first;.
148eb 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 decodeFlags(pPa
148ec 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 ge, flags);. pP
148ed 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d age->hdrOffset =
148ee 20 68 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 hdr;. pPage->c
148ef 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 73 ellOffset = firs
148f0 74 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 t;. pPage->nOve
148f1 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 rflow = 0;. ass
148f2 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 ert( pBt->pageSi
148f3 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e ze>=512 && pBt->
148f4 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 pageSize<=32768
148f5 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b );. pPage->mask
148f6 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 Page = pBt->page
148f7 53 69 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 Size - 1;. pPag
148f8 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 e->nCell = 0;.
148f9 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
148fa 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 1;.}.../*.** Con
148fb 76 65 72 74 20 61 20 44 62 50 61 67 65 20 6f 62 vert a DbPage ob
148fc 74 61 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 tained from the
148fd 70 61 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d pager into a Mem
148fe 50 61 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 Page used by.**
148ff 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e the btree layer.
14900 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 .*/.static MemPa
14901 67 65 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f ge *btreePageFro
14902 6d 44 62 50 61 67 65 28 44 62 50 61 67 65 20 2a mDbPage(DbPage *
14903 70 44 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 pDbPage, Pgno pg
14904 6e 6f 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42 no, BtShared *pB
14905 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 t){. MemPage *p
14906 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a Page = (MemPage*
14907 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 )sqlite3PagerGet
14908 45 78 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a Extra(pDbPage);.
14909 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d pPage->aData =
1490a 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
1490b 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 Data(pDbPage);.
1490c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20 pPage->pDbPage
1490d 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61 = pDbPage;. pPa
1490e 67 65 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 ge->pBt = pBt;.
1490f 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 pPage->pgno = p
14910 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 gno;. pPage->hd
14911 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d rOffset = pPage-
14912 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a >pgno==1 ? 100 :
14913 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 0;. return pPa
14914 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 ge; .}../*.** Ge
14915 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 t a page from th
14916 65 20 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61 e pager. Initia
14917 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 lize the MemPage
14918 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 .pBt and.** MemP
14919 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e age.aData elemen
1491a 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a ts if needed..**
1491b 0a 2a 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e .** If the noCon
1491c 74 65 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74 tent flag is set
1491d 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
1491e 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 we do not care a
1491f 62 6f 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 bout.** the cont
14920 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 ent of the page
14921 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 at this time. S
14922 6f 20 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 o do not go to t
14923 68 65 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 he disk.** to fe
14924 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e tch the content.
14925 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 Just fill in t
14926 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 he content with
14927 7a 65 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a zeros for now..*
14928 2a 20 49 66 20 69 6e 20 74 68 65 20 66 75 74 75 * If in the futu
14929 72 65 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 re we call sqlit
1492a 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 6f e3PagerWrite() o
1492b 6e 20 74 68 69 73 20 70 61 67 65 2c 20 74 68 61 n this page, tha
1492c 74 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 t.** means we ha
1492d 76 65 20 73 74 61 72 74 65 64 20 74 6f 20 62 65 ve started to be
1492e 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 concerned about
1492f 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 content and the
14930 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 disk.** read sh
14931 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 ould occur at th
14932 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c at point..*/.SQL
14933 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
14934 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
14935 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 age(. BtShared
14936 2a 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 *pBt, /* T
14937 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 he btree */. Pg
14938 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 no pgno,
14939 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1493a 74 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 the page to fetc
1493b 68 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a h */. MemPage *
1493c 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 *ppPage, /* R
1493d 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 eturn the page i
1493e 6e 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 n this parameter
1493f 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 */. int noCont
14940 65 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44 6f ent /* Do
14941 20 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 63 not load page c
14942 6f 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20 2a ontent if true *
14943 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 /.){. int rc;.
14944 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 DbPage *pDbPage
14945 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
14946 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14947 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
14948 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
14949 65 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e 70 erAcquire(pBt->p
1494a 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 Pager, pgno, (Db
1494b 50 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c Page**)&pDbPage,
1494c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 noContent);. i
1494d 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1494e 63 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 62 c;. *ppPage = b
1494f 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 treePageFromDbPa
14950 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f ge(pDbPage, pgno
14951 2c 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 6e , pBt);. return
14952 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
14953 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
14954 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
14955 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 base file in pag
14956 65 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 es. If there is
14957 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 any kind of.** e
14958 72 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 75 rror, return ((u
14959 6e 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e nsigned int)-1).
1495a 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 .*/.static Pgno
1495b 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 42 pagerPagecount(B
1495c 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
1495d 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b int nPage = -1;
1495e 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 . int rc;. ass
1495f 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 ert( pBt->pPage1
14960 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
14961 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
14962 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e (pBt->pPager, &n
14963 50 61 67 65 29 3b 0a 20 20 61 73 73 65 72 74 28 Page);. assert(
14964 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc==SQLITE_OK |
14965 7c 20 6e 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 | nPage==-1 );.
14966 20 72 65 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 return (Pgno)nP
14967 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 age;.}../*.** Ge
14968 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 t a page from th
14969 65 20 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 e pager and init
1496a 69 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 ialize it. This
1496b 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6a routine.** is j
1496c 75 73 74 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 ust a convenienc
1496d 65 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 e wrapper around
1496e 20 73 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20 separate calls
1496f 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 to.** sqlite3Btr
14970 65 65 47 65 74 50 61 67 65 28 29 20 61 6e 64 20 eeGetPage() and
14971 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 sqlite3BtreeInit
14972 50 61 67 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 Page()..*/.stati
14973 63 20 69 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 c int getAndInit
14974 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 Page(. BtShared
14975 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 *pBt,
14976 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
14977 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 file */. Pgno p
14978 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f gno, /
14979 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 * Number of the
1497a 70 61 67 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 page to get */.
1497b 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 MemPage **ppPag
1497c 65 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 e /* Write t
1497d 68 65 20 70 61 67 65 20 70 6f 69 6e 74 65 72 20 he page pointer
1497e 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
1497f 20 72 63 3b 0a 20 20 44 62 50 61 67 65 20 2a 70 rc;. DbPage *p
14980 44 62 50 61 67 65 3b 0a 20 20 4d 65 6d 50 61 67 DbPage;. MemPag
14981 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 e *pPage;.. ass
14982 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14983 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
14984 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 67 6e ex) );. if( pgn
14985 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 o==0 ){. retu
14986 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
14987 54 5f 42 4b 50 54 3b 20 0a 20 20 7d 0a 0a 20 20 T_BKPT; . }..
14988 2f 2a 20 49 74 20 69 73 20 6f 66 74 65 6e 20 74 /* It is often t
14989 68 65 20 63 61 73 65 20 74 68 61 74 20 74 68 65 he case that the
1498a 20 70 61 67 65 20 77 65 20 77 61 6e 74 20 69 73 page we want is
1498b 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 already in cach
1498c 65 2e 0a 20 20 2a 2a 20 49 66 20 73 6f 2c 20 67 e.. ** If so, g
1498d 65 74 20 69 74 20 64 69 72 65 63 74 6c 79 2e 20 et it directly.
1498e 20 54 68 69 73 20 73 61 76 65 73 20 75 73 20 66 This saves us f
1498f 72 6f 6d 20 68 61 76 69 6e 67 20 74 6f 20 63 61 rom having to ca
14990 6c 6c 0a 20 20 2a 2a 20 70 61 67 65 72 50 61 67 ll. ** pagerPag
14991 65 63 6f 75 6e 74 28 29 20 74 6f 20 6d 61 6b 65 ecount() to make
14992 20 73 75 72 65 20 70 67 6e 6f 20 69 73 20 77 69 sure pgno is wi
14993 74 68 69 6e 20 6c 69 6d 69 74 73 2c 20 77 68 69 thin limits, whi
14994 63 68 20 72 65 73 75 6c 74 73 0a 20 20 2a 2a 20 ch results. **
14995 69 6e 20 61 20 6d 65 61 73 75 72 65 61 62 6c 65 in a measureable
14996 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70 performance imp
14997 72 6f 76 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a rovements.. */.
14998 20 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69 pDbPage = sqli
14999 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 te3PagerLookup(p
1499a 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f Bt->pPager, pgno
1499b 29 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 65 );. if( pDbPage
1499c 20 29 7b 0a 20 20 20 20 2f 2a 20 50 61 67 65 20 ){. /* Page
1499d 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 is already in ca
1499e 63 68 65 20 2a 2f 0a 20 20 20 20 2a 70 70 50 61 che */. *ppPa
1499f 67 65 20 3d 20 70 50 61 67 65 20 3d 20 62 74 72 ge = pPage = btr
149a0 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 eePageFromDbPage
149a1 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 (pDbPage, pgno,
149a2 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 pBt);. rc = S
149a3 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
149a4 65 7b 0a 20 20 20 20 2f 2a 20 50 61 67 65 20 6e e{. /* Page n
149a5 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 20 41 63 ot in cache. Ac
149a6 71 75 69 72 65 20 69 74 2e 20 2a 2f 0a 20 20 20 quire it. */.
149a7 20 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 72 50 if( pgno>pagerP
149a8 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b agecount(pBt) ){
149a9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
149aa 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
149ab 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 T; . }. rc
149ac 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
149ad 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f etPage(pBt, pgno
149ae 2c 20 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 , ppPage, 0);.
149af 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
149b0 6e 20 72 63 3b 0a 20 20 20 20 70 50 61 67 65 20 n rc;. pPage
149b1 3d 20 2a 70 70 50 61 67 65 3b 0a 20 20 7d 0a 20 = *ppPage;. }.
149b2 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 73 49 if( !pPage->isI
149b3 6e 69 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 nit ){. rc =
149b4 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 sqlite3BtreeInit
149b5 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d Page(pPage);. }
149b6 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
149b7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 6c 65 E_OK ){. rele
149b8 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
149b9 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b *ppPage = 0;
149ba 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
149bb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 ;.}../*.** Relea
149bc 73 65 20 61 20 4d 65 6d 50 61 67 65 2e 20 20 54 se a MemPage. T
149bd 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 63 61 his should be ca
149be 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 lled once for ea
149bf 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c ch prior.** call
149c0 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
149c1 47 65 74 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 GetPage..*/.stat
149c2 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 ic void releaseP
149c3 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 age(MemPage *pPa
149c4 67 65 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 ge){. if( pPage
149c5 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
149c6 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
149c7 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 ==0 || sqlite3Pa
149c8 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 gerPageRefcount(
149c9 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3e pPage->pDbPage)>
149ca 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 1 );. assert(
149cb 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b pPage->aData );
149cc 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
149cd 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 ge->pBt );. a
149ce 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
149cf 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 gerGetExtra(pPag
149d0 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 e->pDbPage) == (
149d1 76 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 void*)pPage );.
149d2 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
149d3 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
149d4 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d Page->pDbPage)==
149d5 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a pPage->aData );.
149d6 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
149d7 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
149d8 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
149d9 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) );. sqlite3
149da 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 PagerUnref(pPage
149db 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a ->pDbPage);. }.
149dc 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 }../*.** During
149dd 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 77 68 65 6e a rollback, when
149de 20 74 68 65 20 70 61 67 65 72 20 72 65 6c 6f 61 the pager reloa
149df 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 ds information i
149e0 6e 74 6f 20 74 68 65 20 63 61 63 68 65 0a 2a 2a nto the cache.**
149e1 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 61 63 so that the cac
149e2 68 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 74 he is restored t
149e3 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 o its original s
149e4 74 61 74 65 20 61 74 20 74 68 65 20 73 74 61 72 tate at the star
149e5 74 20 6f 66 0a 2a 2a 20 74 68 65 20 74 72 61 6e t of.** the tran
149e6 73 61 63 74 69 6f 6e 2c 20 66 6f 72 20 65 61 63 saction, for eac
149e7 68 20 70 61 67 65 20 72 65 73 74 6f 72 65 64 20 h page restored
149e8 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
149e9 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 called..**.** Th
149ea 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 65 64 73 is routine needs
149eb 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 65 78 to reset the ex
149ec 74 72 61 20 64 61 74 61 20 73 65 63 74 69 6f 6e tra data section
149ed 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
149ee 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20 61 67 he.** page to ag
149ef 72 65 65 20 77 69 74 68 20 74 68 65 20 72 65 73 ree with the res
149f0 74 6f 72 65 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 tored data..*/.s
149f1 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 52 tatic void pageR
149f2 65 69 6e 69 74 28 44 62 50 61 67 65 20 2a 70 44 einit(DbPage *pD
149f3 61 74 61 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 ata){. MemPage
149f4 2a 70 50 61 67 65 3b 0a 20 20 70 50 61 67 65 20 *pPage;. pPage
149f5 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71 6c = (MemPage *)sql
149f6 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 ite3PagerGetExtr
149f7 61 28 70 44 61 74 61 29 3b 0a 20 20 69 66 28 20 a(pData);. if(
149f8 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b pPage->isInit ){
149f9 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
149fa 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
149fb 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
149fc 78 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d x) );. pPage-
149fd 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 >isInit = 0;.
149fe 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 if( sqlite3Page
149ff 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 44 rPageRefcount(pD
14a00 61 74 61 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 ata)>0 ){.
14a01 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 sqlite3BtreeInit
14a02 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
14a03 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
14a04 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 Invoke the busy
14a05 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62 74 handler for a bt
14a06 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ree..*/.static i
14a07 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 nt btreeInvokeBu
14a08 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a syHandler(void *
14a09 70 41 72 67 29 7b 0a 20 20 42 74 53 68 61 72 65 pArg){. BtShare
14a0a 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61 72 d *pBt = (BtShar
14a0b 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73 65 ed*)pArg;. asse
14a0c 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 rt( pBt->db );.
14a0d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
14a0e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
14a0f 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
14a10 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 return sqlite3I
14a11 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 nvokeBusyHandler
14a12 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 (&pBt->db->busyH
14a13 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a andler);.}../*.*
14a14 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73 * Open a databas
14a15 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a e file..** .** z
14a16 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 Filename is the
14a17 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
14a18 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a base file. If z
14a19 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c Filename is NULL
14a1a 0a 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 62 61 .** a new databa
14a1b 73 65 20 77 69 74 68 20 61 20 72 61 6e 64 6f 6d se with a random
14a1c 20 6e 61 6d 65 20 69 73 20 63 72 65 61 74 65 64 name is created
14a1d 2e 20 20 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 . This randomly
14a1e 20 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 named.** databa
14a1f 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 se file will be
14a20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 73 71 6c deleted when sql
14a21 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 29 ite3BtreeClose()
14a22 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 is called..** I
14a23 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 f zFilename is "
14a24 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 :memory:" then a
14a25 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
14a26 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 0a base is created.
14a27 2a 2a 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d ** that is autom
14a28 61 74 69 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 atically destroy
14a29 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c ed when it is cl
14a2a 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f osed..*/.SQLITE_
14a2b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14a2c 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 te3BtreeOpen(.
14a2d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
14a2e 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 ename, /* Name
14a2f 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 of the file cont
14a30 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65 aining the BTree
14a31 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 database */. s
14a32 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
14a33 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 /* Associ
14a34 61 74 65 64 20 64 61 74 61 62 61 73 65 20 68 61 ated database ha
14a35 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 ndle */. Btree
14a36 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 20 **ppBtree,
14a37 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
14a38 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 new Btree object
14a39 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f written here */
14a3a 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
14a3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
14a3c 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 76 tions */. int v
14a3d 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 fsFlags
14a3e 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 /* Flags pass
14a3f 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 ed through to sq
14a40 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 lite3_vfs.xOpen(
14a41 29 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 ) */.){. sqlite
14a42 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 3_vfs *pVfs;
14a43 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 /* The VFS to
14a44 75 73 65 20 66 6f 72 20 74 68 69 73 20 62 74 72 use for this btr
14a45 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 ee */. BtShared
14a46 20 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 20 20 *pBt = 0;
14a47 2f 2a 20 53 68 61 72 65 64 20 70 61 72 74 20 6f /* Shared part o
14a48 66 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 f btree structur
14a49 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b e */. Btree *p;
14a4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
14a4b 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74 75 * Handle to retu
14a4c 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d rn */. int rc =
14a4d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 SQLITE_OK;. u8
14a4e 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 75 6e 73 nReserve;. uns
14a4f 69 67 6e 65 64 20 63 68 61 72 20 7a 44 62 48 65 igned char zDbHe
14a50 61 64 65 72 5b 31 30 30 5d 3b 0a 0a 20 20 2f 2a ader[100];.. /*
14a51 20 53 65 74 20 74 68 65 20 76 61 72 69 61 62 6c Set the variabl
14a52 65 20 69 73 4d 65 6d 64 62 20 74 6f 20 74 72 75 e isMemdb to tru
14a53 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f e for an in-memo
14a54 72 79 20 64 61 74 61 62 61 73 65 2c 20 6f 72 20 ry database, or
14a55 0a 20 20 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 . ** false for
14a56 61 20 66 69 6c 65 2d 62 61 73 65 64 20 64 61 74 a file-based dat
14a57 61 62 61 73 65 2e 20 54 68 69 73 20 73 79 6d 62 abase. This symb
14a58 6f 6c 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 ol is only requi
14a59 72 65 64 20 69 66 0a 20 20 2a 2a 20 65 69 74 68 red if. ** eith
14a5a 65 72 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 er of the shared
14a5b 2d 64 61 74 61 20 6f 72 20 61 75 74 6f 76 61 63 -data or autovac
14a5c 75 75 6d 20 66 65 61 74 75 72 65 73 20 61 72 65 uum features are
14a5d 20 63 6f 6d 70 69 6c 65 64 20 0a 20 20 2a 2a 20 compiled . **
14a5e 69 6e 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 into the library
14a5f 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 .. */.#if !defi
14a60 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
14a61 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 7c 7c SHARED_CACHE) ||
14a62 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
14a63 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
14a64 29 0a 20 20 23 69 66 64 65 66 20 53 51 4c 49 54 ). #ifdef SQLIT
14a65 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a E_OMIT_MEMORYDB.
14a66 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 const int is
14a67 4d 65 6d 64 62 20 3d 20 30 3b 0a 20 20 23 65 6c Memdb = 0;. #el
14a68 73 65 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 se. const int
14a69 20 69 73 4d 65 6d 64 62 20 3d 20 7a 46 69 6c 65 isMemdb = zFile
14a6a 6e 61 6d 65 20 26 26 20 21 73 74 72 63 6d 70 28 name && !strcmp(
14a6b 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d zFilename, ":mem
14a6c 6f 72 79 3a 22 29 3b 0a 20 20 23 65 6e 64 69 66 ory:");. #endif
14a6d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 .#endif.. asser
14a6e 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 t( db!=0 );. as
14a6f 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14a70 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
14a71 65 78 29 20 29 3b 0a 0a 20 20 70 56 66 73 20 3d ex) );.. pVfs =
14a72 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 70 20 3d db->pVfs;. p =
14a73 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 sqlite3MallocZe
14a74 72 6f 28 73 69 7a 65 6f 66 28 42 74 72 65 65 29 ro(sizeof(Btree)
14a75 29 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 );. if( !p ){.
14a76 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
14a77 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d _NOMEM;. }. p-
14a78 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 >inTrans = TRANS
14a79 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 64 62 20 3d _NONE;. p->db =
14a7a 20 64 62 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e db;..#if !defin
14a7b 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
14a7c 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 HARED_CACHE) &&
14a7d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
14a7e 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f OMIT_DISKIO). /
14a7f 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 42 *. ** If this B
14a80 74 72 65 65 20 69 73 20 61 20 63 61 6e 64 69 64 tree is a candid
14a81 61 74 65 20 66 6f 72 20 73 68 61 72 65 64 20 63 ate for shared c
14a82 61 63 68 65 2c 20 74 72 79 20 74 6f 20 66 69 6e ache, try to fin
14a83 64 20 61 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69 d an. ** existi
14a84 6e 67 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 ng BtShared obje
14a85 63 74 20 74 68 61 74 20 77 65 20 63 61 6e 20 73 ct that we can s
14a86 68 61 72 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20 hare with. */.
14a87 20 69 66 28 20 69 73 4d 65 6d 64 62 3d 3d 30 0a if( isMemdb==0.
14a88 20 20 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 && (db->flags
14a89 20 26 20 53 51 4c 49 54 45 5f 56 74 61 62 29 3d & SQLITE_Vtab)=
14a8a 3d 30 0a 20 20 20 26 26 20 7a 46 69 6c 65 6e 61 =0. && zFilena
14a8b 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b me && zFilename[
14a8c 30 5d 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 0]. ){. if(
14a8d 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
14a8e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 45 fig.sharedCacheE
14a8f 6e 61 62 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 nabled ){.
14a90 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d int nFullPathnam
14a91 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 e = pVfs->mxPath
14a92 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63 68 name+1;. ch
14a93 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d ar *zFullPathnam
14a94 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f e = sqlite3Mallo
14a95 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 c(nFullPathname)
14a96 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
14a97 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 mutex *mutexShar
14a98 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 ed;. p->sha
14a99 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 rable = 1;.
14a9a 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 db->flags |= SQ
14a9b 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68 65 LITE_SharedCache
14a9c 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 46 75 ;. if( !zFu
14a9d 6c 6c 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 llPathname ){.
14a9e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
14a9f 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 ee(p);. r
14aa0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
14aa1 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 EM;. }.
14aa2 20 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 sqlite3OsFullP
14aa3 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 athname(pVfs, zF
14aa4 69 6c 65 6e 61 6d 65 2c 20 6e 46 75 6c 6c 50 61 ilename, nFullPa
14aa5 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74 thname, zFullPat
14aa6 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 6d 75 hname);. mu
14aa7 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 texShared = sqli
14aa8 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
14aa9 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
14aaa 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 C_MASTER);.
14aab 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
14aac 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 nter(mutexShared
14aad 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 );. for(pBt
14aae 3d 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 =GLOBAL(BtShared
14aaf 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 *,sqlite3SharedC
14ab0 61 63 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 acheList); pBt;
14ab1 70 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b pBt=pBt->pNext){
14ab2 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
14ab3 20 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a pBt->nRef>0 );.
14ab4 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 if( 0==s
14ab5 74 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e trcmp(zFullPathn
14ab6 61 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65 ame, sqlite3Page
14ab7 72 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70 rFilename(pBt->p
14ab8 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 Pager)).
14ab9 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 && sqli
14aba 74 65 33 50 61 67 65 72 56 66 73 28 70 42 74 2d te3PagerVfs(pBt-
14abb 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 73 20 29 >pPager)==pVfs )
14abc 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 {. p->p
14abd 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 Bt = pBt;.
14abe 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b pBt->nRef++;
14abf 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
14ac0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
14ac1 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
14ac2 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
14ac3 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 texShared);.
14ac4 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
14ac5 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 FullPathname);.
14ac6 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 }.#ifdef SQLI
14ac7 54 45 5f 44 45 42 55 47 0a 20 20 20 20 65 6c 73 TE_DEBUG. els
14ac8 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 e{. /* In d
14ac9 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61 ebug mode, we ma
14aca 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 74 65 6e rk all persisten
14acb 74 20 64 61 74 61 62 61 73 65 73 20 61 73 20 73 t databases as s
14acc 68 61 72 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a harable. **
14acd 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 79 20 even when they
14ace 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 73 20 65 are not. This e
14acf 78 65 72 63 69 73 65 73 20 74 68 65 20 6c 6f 63 xercises the loc
14ad0 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20 20 king code and.
14ad1 20 20 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72 ** gives mor
14ad2 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 6f e opportunity fo
14ad3 72 20 61 73 73 65 72 74 73 28 73 71 6c 69 74 65 r asserts(sqlite
14ad4 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 29 0a 3_mutex_held()).
14ad5 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 ** stateme
14ad6 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b nts to find lock
14ad7 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 ing problems..
14ad8 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e */. p->
14ad9 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 sharable = 1;.
14ada 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 }.#endif. }.#
14adb 65 6e 64 69 66 0a 20 20 69 66 28 20 70 42 74 3d endif. if( pBt=
14adc 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 =0 ){. /*.
14add 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e ** The followin
14ade 67 20 61 73 73 65 72 74 73 20 6d 61 6b 65 20 73 g asserts make s
14adf 75 72 65 20 74 68 61 74 20 73 74 72 75 63 74 75 ure that structu
14ae0 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 res used by the
14ae1 62 74 72 65 65 20 61 72 65 0a 20 20 20 20 2a 2a btree are. **
14ae2 20 74 68 65 20 72 69 67 68 74 20 73 69 7a 65 2e the right size.
14ae3 20 20 54 68 69 73 20 69 73 20 74 6f 20 67 75 61 This is to gua
14ae4 72 64 20 61 67 61 69 6e 73 74 20 73 69 7a 65 20 rd against size
14ae5 63 68 61 6e 67 65 73 20 74 68 61 74 20 72 65 73 changes that res
14ae6 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 ult. ** when
14ae7 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64 compiling on a d
14ae8 69 66 66 65 72 65 6e 74 20 61 72 63 68 69 74 65 ifferent archite
14ae9 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 cture.. */.
14aea 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
14aeb 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 (i64)==8 || size
14aec 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20 of(i64)==4 );.
14aed 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
14aee 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 (u64)==8 || size
14aef 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20 of(u64)==4 );.
14af0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
14af1 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20 (u32)==4 );.
14af2 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 assert( sizeof(u
14af3 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73 16)==2 );. as
14af4 73 65 72 74 28 20 73 69 7a 65 6f 66 28 50 67 6e sert( sizeof(Pgn
14af5 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20 o)==4 );. .
14af6 70 42 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c pBt = sqlite3Mal
14af7 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 locZero( sizeof(
14af8 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28 *pBt) );. if(
14af9 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pBt==0 ){.
14afa 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
14afb 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 EM;. goto b
14afc 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 tree_open_out;.
14afd 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
14afe 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70 lite3PagerOpen(p
14aff 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 Vfs, &pBt->pPage
14b00 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 r, zFilename,.
14b01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14b02 20 20 20 20 20 20 20 20 45 58 54 52 41 5f 53 49 EXTRA_SI
14b03 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c ZE, flags, vfsFl
14b04 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ags);. if( rc
14b05 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
14b06 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
14b07 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 3PagerReadFilehe
14b08 61 64 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 ader(pBt->pPager
14b09 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 64 65 ,sizeof(zDbHeade
14b0a 72 29 2c 7a 44 62 48 65 61 64 65 72 29 3b 0a 20 r),zDbHeader);.
14b0b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
14b0c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
14b0d 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f goto btree_o
14b0e 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 pen_out;. }.
14b0f 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 sqlite3PagerS
14b10 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 70 42 etBusyhandler(pB
14b11 74 2d 3e 70 50 61 67 65 72 2c 20 62 74 72 65 65 t->pPager, btree
14b12 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 InvokeBusyHandle
14b13 72 2c 20 70 42 74 29 3b 0a 20 20 20 20 70 2d 3e r, pBt);. p->
14b14 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a 20 20 pBt = pBt;. .
14b15 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 sqlite3PagerSe
14b16 74 52 65 69 6e 69 74 65 72 28 70 42 74 2d 3e 70 tReiniter(pBt->p
14b17 50 61 67 65 72 2c 20 70 61 67 65 52 65 69 6e 69 Pager, pageReini
14b18 74 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 43 75 t);. pBt->pCu
14b19 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 rsor = 0;. pB
14b1a 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 t->pPage1 = 0;.
14b1b 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 pBt->readOnly
14b1c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 = sqlite3PagerI
14b1d 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d 3e 70 sreadonly(pBt->p
14b1e 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42 74 2d Pager);. pBt-
14b1f 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32 >pageSize = get2
14b20 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b byte(&zDbHeader[
14b21 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 42 16]);. if( pB
14b22 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31 32 20 t->pageSize<512
14b23 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 || pBt->pageSize
14b24 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 >SQLITE_MAX_PAGE
14b25 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20 20 7c _SIZE. |
14b26 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a | ((pBt->pageSiz
14b27 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 53 69 e-1)&pBt->pageSi
14b28 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ze)!=0 ){.
14b29 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 pBt->pageSize =
14b2a 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 0;. sqlite3
14b2b 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
14b2c 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 (pBt->pPager, &p
14b2d 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23 Bt->pageSize);.#
14b2e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14b2f 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
14b30 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6d 61 /* If the ma
14b31 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 gic name ":memor
14b32 79 3a 22 20 77 69 6c 6c 20 63 72 65 61 74 65 20 y:" will create
14b33 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
14b34 61 62 61 73 65 2c 20 74 68 65 6e 0a 20 20 20 20 abase, then.
14b35 20 20 2a 2a 20 6c 65 61 76 65 20 74 68 65 20 61 ** leave the a
14b36 75 74 6f 56 61 63 75 75 6d 20 6d 6f 64 65 20 61 utoVacuum mode a
14b37 74 20 30 20 28 64 6f 20 6e 6f 74 20 61 75 74 6f t 0 (do not auto
14b38 2d 76 61 63 75 75 6d 29 2c 20 65 76 65 6e 20 69 -vacuum), even i
14b39 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 f. ** SQLIT
14b3a 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 E_DEFAULT_AUTOVA
14b3b 43 55 55 4d 20 69 73 20 74 72 75 65 2e 20 4f 6e CUUM is true. On
14b3c 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
14b3d 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c if. ** SQL
14b3e 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 ITE_OMIT_MEMORYD
14b3f 42 20 68 61 73 20 62 65 65 6e 20 64 65 66 69 6e B has been defin
14b40 65 64 2c 20 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 ed, then ":memor
14b41 79 3a 22 20 69 73 20 6a 75 73 74 20 61 0a 20 20 y:" is just a.
14b42 20 20 20 20 2a 2a 20 72 65 67 75 6c 61 72 20 66 ** regular f
14b43 69 6c 65 2d 6e 61 6d 65 2e 20 49 6e 20 74 68 69 ile-name. In thi
14b44 73 20 63 61 73 65 20 74 68 65 20 61 75 74 6f 2d s case the auto-
14b45 76 61 63 75 75 6d 20 61 70 70 6c 69 65 73 20 61 vacuum applies a
14b46 73 20 70 65 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 s per normal..
14b47 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
14b48 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 69 zFilename && !i
14b49 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20 20 20 sMemdb ){.
14b4a 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 pBt->autoVacuu
14b4b 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 m = (SQLITE_DEFA
14b4c 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 3f ULT_AUTOVACUUM ?
14b4d 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 1 : 0);.
14b4e 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d pBt->incrVacuum
14b4f 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 = (SQLITE_DEFAU
14b50 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3d 3d 32 LT_AUTOVACUUM==2
14b51 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 ? 1 : 0);.
14b52 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 }.#endif.
14b53 6e 52 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 nReserve = 0;.
14b54 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e }else{. n
14b55 52 65 73 65 72 76 65 20 3d 20 7a 44 62 48 65 61 Reserve = zDbHea
14b56 64 65 72 5b 32 30 5d 3b 0a 20 20 20 20 20 20 70 der[20];. p
14b57 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 Bt->pageSizeFixe
14b58 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 d = 1;.#ifndef S
14b59 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
14b5a 41 43 55 55 4d 0a 20 20 20 20 20 20 70 42 74 2d ACUUM. pBt-
14b5b 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 >autoVacuum = (g
14b5c 65 74 34 62 79 74 65 28 26 7a 44 62 48 65 61 64 et4byte(&zDbHead
14b5d 65 72 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a er[36 + 4*4])?1:
14b5e 30 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 0);. pBt->i
14b5f 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 ncrVacuum = (get
14b60 34 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 4byte(&zDbHeader
14b61 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 [36 + 7*4])?1:0)
14b62 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 ;.#endif. }.
14b63 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 pBt->usableSi
14b64 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 ze = pBt->pageSi
14b65 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a 20 ze - nReserve;.
14b66 20 20 20 61 73 73 65 72 74 28 20 28 70 42 74 2d assert( (pBt-
14b67 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d >pageSize & 7)==
14b68 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 20 0 ); /* 8-byte
14b69 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 67 alignment of pag
14b6a 65 53 69 7a 65 20 2a 2f 0a 20 20 20 20 73 71 6c eSize */. sql
14b6b 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 ite3PagerSetPage
14b6c 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 size(pBt->pPager
14b6d 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 , &pBt->pageSize
14b6e 29 3b 0a 20 20 20 0a 23 69 66 20 21 64 65 66 69 );. .#if !defi
14b6f 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
14b70 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 SHARED_CACHE) &&
14b71 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
14b72 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 _OMIT_DISKIO).
14b73 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77 /* Add the new
14b74 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
14b75 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c to the linked l
14b76 69 73 74 20 73 68 61 72 61 62 6c 65 20 42 74 53 ist sharable BtS
14b77 68 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 hareds.. */.
14b78 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 if( p->sharab
14b79 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 le ){. sqli
14b7a 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
14b7b 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20 70 42 Shared;. pB
14b7c 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 t->nRef = 1;.
14b7d 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d mutexShared =
14b7e 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
14b7f 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
14b80 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a STATIC_MASTER);.
14b81 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 if( SQLITE
14b82 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 73 _THREADSAFE && s
14b83 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
14b84 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b ig.bCoreMutex ){
14b85 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6d 75 . pBt->mu
14b86 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
14b87 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
14b88 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 20 UTEX_FAST);.
14b89 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d 75 74 if( pBt->mut
14b8a 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ex==0 ){.
14b8b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
14b8c 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 OMEM;.
14b8d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
14b8e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
14b8f 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f goto btree_open_
14b90 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 out;. }.
14b91 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
14b92 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
14b93 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 (mutexShared);.
14b94 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78 74 20 pBt->pNext
14b95 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 = GLOBAL(BtShare
14b96 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 d*,sqlite3Shared
14b97 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20 CacheList);.
14b98 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 GLOBAL(BtShare
14b99 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 d*,sqlite3Shared
14b9a 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74 CacheList) = pBt
14b9b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
14b9c 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
14b9d 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 7d 0a xShared);. }.
14b9e 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 66 20 #endif. }..#if
14b9f 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
14ba0 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
14ba1 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 E) && !defined(S
14ba2 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 QLITE_OMIT_DISKI
14ba3 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e O). /* If the n
14ba4 65 77 20 42 74 72 65 65 20 75 73 65 73 20 61 20 ew Btree uses a
14ba5 73 68 61 72 61 62 6c 65 20 70 42 74 53 68 61 72 sharable pBtShar
14ba6 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 74 68 ed, then link th
14ba7 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72 65 65 e new. ** Btree
14ba8 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 20 6f into the list o
14ba9 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65 20 42 f all sharable B
14baa 74 72 65 65 73 20 66 6f 72 20 74 68 65 20 73 61 trees for the sa
14bab 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 20 me connection..
14bac 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20 ** The list is
14bad 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64 69 6e kept in ascendin
14bae 67 20 6f 72 64 65 72 20 62 79 20 70 42 74 20 61 g order by pBt a
14baf 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 ddress.. */. i
14bb0 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 f( p->sharable )
14bb1 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
14bb2 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a 20 20 Btree *pSib;.
14bb3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
14bb4 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nDb; i++){.
14bb5 20 20 69 66 28 20 28 70 53 69 62 20 3d 20 64 62 if( (pSib = db
14bb6 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21 3d 30 ->aDb[i].pBt)!=0
14bb7 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72 61 62 && pSib->sharab
14bb8 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 le ){. wh
14bb9 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72 65 76 ile( pSib->pPrev
14bba 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69 62 2d ){ pSib = pSib-
14bbb 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20 20 20 >pPrev; }.
14bbc 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70 53 69 if( p->pBt<pSi
14bbd 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 b->pBt ){.
14bbe 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 p->pNext = p
14bbf 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70 Sib;. p
14bc0 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 ->pPrev = 0;.
14bc1 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 50 72 pSib->pPr
14bc2 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 ev = p;.
14bc3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
14bc4 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 4e while( pSib->pN
14bc5 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70 4e 65 ext && pSib->pNe
14bc6 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 xt->pBt<p->pBt )
14bc7 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53 {. pS
14bc8 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 ib = pSib->pNext
14bc9 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
14bca 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 p->pNext
14bcb 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a = pSib->pNext;.
14bcc 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 p->pPr
14bcd 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 ev = pSib;.
14bce 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 if( p->pNex
14bcf 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 t ){.
14bd0 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 p->pNext->pPrev
14bd1 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 = p;.
14bd2 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 62 }. pSib
14bd3 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20 20 20 ->pNext = p;.
14bd4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 }. b
14bd5 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
14bd6 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
14bd7 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b 0a 0a *ppBtree = p;..
14bd8 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3a 0a btree_open_out:.
14bd9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
14bda 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 _OK ){. if( p
14bdb 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 Bt && pBt->pPage
14bdc 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 r ){. sqlit
14bdd 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 e3PagerClose(pBt
14bde 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d ->pPager);. }
14bdf 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
14be0 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 e(pBt);. sqli
14be1 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 te3_free(p);.
14be2 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b 0a 20 *ppBtree = 0;.
14be3 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
14be4 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 }../*.** Decreme
14be5 6e 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e nt the BtShared.
14be6 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 20 57 nRef counter. W
14be7 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 20 7a hen it reaches z
14be8 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 ero,.** remove t
14be9 68 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 he BtShared stru
14bea 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 73 cture from the s
14beb 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 52 65 haring list. Re
14bec 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 turn.** true if
14bed 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 the BtShared.nRe
14bee 66 20 63 6f 75 6e 74 65 72 20 72 65 61 63 68 65 f counter reache
14bef 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 s zero and retur
14bf0 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 n.** false if it
14bf1 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 74 69 is still positi
14bf2 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ve..*/.static in
14bf3 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 t removeFromShar
14bf4 69 6e 67 4c 69 73 74 28 42 74 53 68 61 72 65 64 ingList(BtShared
14bf5 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 66 20 *pBt){.#ifndef
14bf6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
14bf7 45 44 5f 43 41 43 48 45 0a 20 20 73 71 6c 69 74 ED_CACHE. sqlit
14bf8 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 e3_mutex *pMaste
14bf9 72 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 r;. BtShared *p
14bfa 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f List;. int remo
14bfb 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 ved = 0;.. asse
14bfc 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
14bfd 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d x_notheld(pBt->m
14bfe 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 61 73 74 utex) );. pMast
14bff 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 er = sqlite3Mute
14c00 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
14c01 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
14c02 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 R);. sqlite3_mu
14c03 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 tex_enter(pMaste
14c04 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d r);. pBt->nRef-
14c05 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 -;. if( pBt->nR
14c06 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 ef<=0 ){. if(
14c07 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 GLOBAL(BtShared
14c08 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 *,sqlite3SharedC
14c09 61 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 acheList)==pBt )
14c0a 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 {. GLOBAL(B
14c0b 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 tShared*,sqlite3
14c0c 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 SharedCacheList)
14c0d 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 = pBt->pNext;.
14c0e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
14c0f 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 pList = GLOBAL(B
14c10 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 tShared*,sqlite3
14c11 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 SharedCacheList)
14c12 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 ;. while( A
14c13 4c 57 41 59 53 28 70 4c 69 73 74 29 20 26 26 20 LWAYS(pList) &&
14c14 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 pList->pNext!=pB
14c15 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 t ){. pLi
14c16 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b st=pList->pNext;
14c17 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
14c18 66 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 f( ALWAYS(pList)
14c19 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 ){. pLis
14c1a 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e t->pNext = pBt->
14c1b 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 pNext;. }.
14c1c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c }. if( SQL
14c1d 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 29 ITE_THREADSAFE )
14c1e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
14c1f 6d 75 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e mutex_free(pBt->
14c20 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 mutex);. }.
14c21 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 removed = 1;.
14c22 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 }. sqlite3_mut
14c23 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 ex_leave(pMaster
14c24 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 6d 6f );. return remo
14c25 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 ved;.#else. ret
14c26 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a urn 1;.#endif.}.
14c27 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
14c28 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 pBt->pTmpSpace
14c29 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c points to an all
14c2a 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d ocation of .** M
14c2b 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 X_CELL_SIZE(pBt)
14c2c 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 bytes..*/.stati
14c2d 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 c void allocateT
14c2e 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 65 empSpace(BtShare
14c2f 64 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 21 d *pBt){. if( !
14c30 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 pBt->pTmpSpace )
14c31 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 {. pBt->pTmpS
14c32 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 pace = sqlite3Pa
14c33 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 geMalloc( pBt->p
14c34 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d ageSize );. }.}
14c35 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 ../*.** Free the
14c36 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 pBt->pTmpSpace
14c37 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 allocation.*/.st
14c38 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 54 65 atic void freeTe
14c39 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 mpSpace(BtShared
14c3a 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 *pBt){. sqlite
14c3b 33 50 61 67 65 46 72 65 65 28 20 70 42 74 2d 3e 3PageFree( pBt->
14c3c 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 70 42 pTmpSpace);. pB
14c3d 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 30 t->pTmpSpace = 0
14c3e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 ;.}../*.** Close
14c3f 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 an open databas
14c40 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 e and invalidate
14c41 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f all cursors..*/
14c42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14c43 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
14c44 43 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b Close(Btree *p){
14c45 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
14c46 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 = p->pBt;. BtC
14c47 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 ursor *pCur;..
14c48 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 /* Close all cur
14c49 73 6f 72 73 20 6f 70 65 6e 65 64 20 76 69 61 20 sors opened via
14c4a 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f this handle. */
14c4b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
14c4c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
14c4d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
14c4e 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
14c4f 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 er(p);. pBt->db
14c50 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 70 43 75 72 = p->db;. pCur
14c51 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b = pBt->pCursor;
14c52 0a 20 20 77 68 69 6c 65 28 20 70 43 75 72 20 29 . while( pCur )
14c53 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a {. BtCursor *
14c54 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 20 pTmp = pCur;.
14c55 20 70 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 4e pCur = pCur->pN
14c56 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 54 6d ext;. if( pTm
14c57 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a p->pBtree==p ){.
14c58 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
14c59 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 54 eeCloseCursor(pT
14c5a 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a mp);. }. }..
14c5b 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6e /* Rollback an
14c5c 79 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 y active transac
14c5d 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 68 tion and free th
14c5e 65 20 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75 e handle structu
14c5f 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c re.. ** The cal
14c60 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 l to sqlite3Btre
14c61 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70 eRollback() drop
14c62 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b s any table-lock
14c63 73 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 74 s held by. ** t
14c64 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f his handle.. */
14c65 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 . sqlite3BtreeR
14c66 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 73 71 ollback(p);. sq
14c67 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
14c68 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 p);.. /* If the
14c69 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68 re are still oth
14c6a 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 er outstanding r
14c6b 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 eferences to the
14c6c 20 73 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20 shared-btree.
14c6d 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 72 65 ** structure, re
14c6e 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65 turn now. The re
14c6f 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 mainder of this
14c70 70 72 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73 procedure cleans
14c71 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68 . ** up the sh
14c72 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f ared-btree.. */
14c73 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 . assert( p->wa
14c74 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 ntToLock==0 && p
14c75 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 ->locked==0 );.
14c76 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
14c77 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53 e || removeFromS
14c78 68 61 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20 haringList(pBt)
14c79 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42 ){. /* The pB
14c7a 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f t is no longer o
14c7b 6e 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 n the sharing li
14c7c 73 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63 st, so we can ac
14c7d 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77 cess. ** it w
14c7e 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f ithout having to
14c7f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e hold the mutex.
14c80 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 . **. ** C
14c81 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c lean out and del
14c82 65 74 65 20 74 68 65 20 42 74 53 68 61 72 65 64 ete the BtShared
14c83 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a object.. */.
14c84 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 assert( !pBt
14c85 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 ->pCursor );.
14c86 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
14c87 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b se(pBt->pPager);
14c88 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46 . if( pBt->xF
14c89 72 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74 reeSchema && pBt
14c8a 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 ->pSchema ){.
14c8b 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 pBt->xFreeSch
14c8c 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 ema(pBt->pSchema
14c8d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
14c8e 69 74 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70 ite3_free(pBt->p
14c8f 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 66 72 65 Schema);. fre
14c90 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
14c91 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
14c92 65 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 e(pBt);. }..#if
14c93 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
14c94 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
14c95 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 assert( p->wantT
14c96 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 oLock==0 );. as
14c97 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d sert( p->locked=
14c98 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 =0 );. if( p->p
14c99 50 72 65 76 20 29 20 70 2d 3e 70 50 72 65 76 2d Prev ) p->pPrev-
14c9a 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 >pNext = p->pNex
14c9b 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 t;. if( p->pNex
14c9c 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 t ) p->pNext->pP
14c9d 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a rev = p->pPrev;.
14c9e 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 #endif.. sqlite
14c9f 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74 3_free(p);. ret
14ca0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
14ca1 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
14ca2 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 he limit on the
14ca3 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
14ca4 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65 20 63 allowed in the c
14ca5 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ache..**.** The
14ca6 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
14ca7 66 20 63 61 63 68 65 20 70 61 67 65 73 20 69 73 f cache pages is
14ca8 20 73 65 74 20 74 6f 20 74 68 65 20 61 62 73 6f set to the abso
14ca9 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 lute.** value of
14caa 20 6d 78 50 61 67 65 2e 20 20 49 66 20 6d 78 50 mxPage. If mxP
14cab 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c age is negative,
14cac 20 74 68 65 20 70 61 67 65 72 20 77 69 6c 6c 0a the pager will.
14cad 2a 2a 20 6f 70 65 72 61 74 65 20 61 73 79 6e 63 ** operate async
14cae 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74 20 77 hronously - it w
14caf 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74 6f 20 ill not stop to
14cb0 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a 20 74 do fsync()s.** t
14cb1 6f 20 69 6e 73 75 72 65 20 64 61 74 61 20 69 73 o insure data is
14cb2 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
14cb3 64 69 73 6b 20 73 75 72 66 61 63 65 20 62 65 66 disk surface bef
14cb4 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e ore.** continuin
14cb5 67 2e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73 g. Transactions
14cb6 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66 20 73 still work if s
14cb7 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 ynchronous is of
14cb8 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 f,.** and the da
14cb9 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 tabase cannot be
14cba 20 63 6f 72 72 75 70 74 65 64 20 69 66 20 74 68 corrupted if th
14cbb 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 63 72 is program.** cr
14cbc 61 73 68 65 73 2e 20 20 42 75 74 20 69 66 20 74 ashes. But if t
14cbd 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 he operating sys
14cbe 74 65 6d 20 63 72 61 73 68 65 73 20 6f 72 20 74 tem crashes or t
14cbf 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 61 62 here is.** an ab
14cc0 72 75 70 74 20 70 6f 77 65 72 20 66 61 69 6c 75 rupt power failu
14cc1 72 65 20 77 68 65 6e 20 73 79 6e 63 68 72 6f 6e re when synchron
14cc2 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 68 65 20 ous is off, the
14cc3 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 75 6c database.** coul
14cc4 64 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 d be left in an
14cc5 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61 6e 64 inconsistent and
14cc6 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 73 unrecoverable s
14cc7 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f tate..** Synchro
14cc8 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20 64 65 nous is on by de
14cc9 66 61 75 6c 74 20 73 6f 20 64 61 74 61 62 61 73 fault so databas
14cca 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 e corruption is
14ccb 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 not.** normally
14ccc 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49 a worry..*/.SQLI
14ccd 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
14cce 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 qlite3BtreeSetCa
14ccf 63 68 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 cheSize(Btree *p
14cd0 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
14cd1 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
14cd2 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 p->pBt;. asser
14cd3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14cd4 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
14cd5 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
14cd6 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
14cd7 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
14cd8 43 61 63 68 65 73 69 7a 65 28 70 42 74 2d 3e 70 Cachesize(pBt->p
14cd9 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a Pager, mxPage);.
14cda 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
14cdb 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
14cdc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
14cdd 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
14cde 77 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63 way data is sync
14cdf 65 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72 ed to disk in or
14ce0 64 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20 der to increase
14ce1 6f 72 20 64 65 63 72 65 61 73 65 0a 2a 2a 20 68 or decrease.** h
14ce2 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74 61 ow well the data
14ce3 62 61 73 65 20 72 65 73 69 73 74 73 20 64 61 6d base resists dam
14ce4 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 age due to OS cr
14ce5 61 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72 0a ashes and power.
14ce6 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65 ** failures. Le
14ce7 76 65 6c 20 31 20 69 73 20 74 68 65 20 73 61 6d vel 1 is the sam
14ce8 65 20 61 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75 e as asynchronou
14ce9 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63 s (no syncs() oc
14cea 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 cur and.** there
14ceb 20 69 73 20 61 20 68 69 67 68 20 70 72 6f 62 61 is a high proba
14cec 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 bility of damage
14ced 29 20 20 4c 65 76 65 6c 20 32 20 69 73 20 74 68 ) Level 2 is th
14cee 65 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 72 e default. Ther
14cef 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 20 6c e.** is a very l
14cf0 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 ow but non-zero
14cf1 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 probability of d
14cf2 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 33 20 amage. Level 3
14cf3 72 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 20 70 reduces the.** p
14cf4 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 robability of da
14cf5 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 7a 65 72 mage to near zer
14cf6 6f 20 62 75 74 20 77 69 74 68 20 61 20 77 72 69 o but with a wri
14cf7 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72 te performance r
14cf8 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 eduction..*/.#if
14cf9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
14cfa 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53 _PAGER_PRAGMAS.S
14cfb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
14cfc 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
14cfd 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 tSafetyLevel(Btr
14cfe 65 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76 65 6c ee *p, int level
14cff 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 29 7b , int fullSync){
14d00 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
14d01 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 = p->pBt;. ass
14d02 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14d03 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
14d04 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
14d05 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
14d06 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 . sqlite3PagerS
14d07 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 42 etSafetyLevel(pB
14d08 74 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76 65 6c t->pPager, level
14d09 2c 20 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 73 , fullSync);. s
14d0a 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
14d0b 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
14d0c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 LITE_OK;.}.#endi
14d0d 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 f../*.** Return
14d0e 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 TRUE if the give
14d0f 6e 20 62 74 72 65 65 20 69 73 20 73 65 74 20 74 n btree is set t
14d10 6f 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 31 o safety level 1
14d11 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 . In other.** w
14d12 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 ords, return TRU
14d13 45 20 69 66 20 6e 6f 20 73 79 6e 63 28 29 20 6f E if no sync() o
14d14 63 63 75 72 73 20 6f 6e 20 74 68 65 20 64 69 73 ccurs on the dis
14d15 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 k files..*/.SQLI
14d16 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
14d17 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 qlite3BtreeSyncD
14d18 69 73 61 62 6c 65 64 28 42 74 72 65 65 20 2a 70 isabled(Btree *p
14d19 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
14d1a 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 Bt = p->pBt;. i
14d1b 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 nt rc;. assert(
14d1c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14d1d 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
14d1e 29 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 ) ); . sqlite3
14d1f 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
14d20 20 61 73 73 65 72 74 28 20 70 42 74 20 26 26 20 assert( pBt &&
14d21 70 42 74 2d 3e 70 50 61 67 65 72 20 29 3b 0a 20 pBt->pPager );.
14d22 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
14d23 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e 70 50 erNosync(pBt->pP
14d24 61 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 ager);. sqlite3
14d25 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
14d26 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
14d27 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
14d28 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 TE_OMIT_PAGER_PR
14d29 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e AGMAS) || !defin
14d2a 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
14d2b 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61 ACUUM)./*.** Cha
14d2c 6e 67 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 nge the default
14d2d 70 61 67 65 73 20 73 69 7a 65 20 61 6e 64 20 74 pages size and t
14d2e 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 he number of res
14d2f 65 72 76 65 64 20 62 79 74 65 73 20 70 65 72 20 erved bytes per
14d30 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 page..**.** The
14d31 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20 62 page size must b
14d32 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20 62 e a power of 2 b
14d33 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 etween 512 and 6
14d34 35 35 33 36 2e 20 20 49 66 20 74 68 65 20 70 61 5536. If the pa
14d35 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c ge.** size suppl
14d36 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65 ied does not mee
14d37 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e t this constrain
14d38 74 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 t then the page
14d39 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 size is not.** c
14d3a 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 hanged..**.** Pa
14d3b 67 65 20 73 69 7a 65 73 20 61 72 65 20 63 6f 6e ge sizes are con
14d3c 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65 20 61 strained to be a
14d3d 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 73 6f power of two so
14d3e 20 74 68 61 74 20 74 68 65 20 72 65 67 69 6f 6e that the region
14d3f 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 .** of the datab
14d40 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f ase file used fo
14d41 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e r locking (begin
14d42 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e 47 5f ning at PENDING_
14d43 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72 BYTE,.** the fir
14d44 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 st byte past the
14d45 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c 20 30 1GB boundary, 0
14d46 78 34 30 30 30 30 30 30 30 29 20 6e 65 65 64 73 x40000000) needs
14d47 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 to occur.** at
14d48 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
14d49 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 a page..**.** I
14d4a 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52 65 73 f parameter nRes
14d4b 65 72 76 65 20 69 73 20 6c 65 73 73 20 74 68 61 erve is less tha
14d4c 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 n zero, then the
14d4d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 number of reser
14d4e 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 65 72 ved.** bytes per
14d4f 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e page is left un
14d50 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 changed..*/.SQLI
14d51 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
14d52 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 qlite3BtreeSetPa
14d53 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c geSize(Btree *p,
14d54 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c 20 69 int pageSize, i
14d55 6e 74 20 6e 52 65 73 65 72 76 65 29 7b 0a 20 20 nt nReserve){.
14d56 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
14d57 4f 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a OK;. BtShared *
14d58 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
14d59 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 assert( nReserve
14d5a 3e 3d 2d 31 20 26 26 20 6e 52 65 73 65 72 76 65 >=-1 && nReserve
14d5b 3c 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c 69 74 <=255 );. sqlit
14d5c 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
14d5d 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 . if( pBt->page
14d5e 53 69 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20 SizeFixed ){.
14d5f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
14d60 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 ve(p);. retur
14d61 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c n SQLITE_READONL
14d62 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 Y;. }. if( nRe
14d63 73 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e serve<0 ){. n
14d64 52 65 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 Reserve = pBt->p
14d65 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 ageSize - pBt->u
14d66 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 sableSize;. }.
14d67 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 assert( nReserv
14d68 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 e>=0 && nReserve
14d69 3c 3d 32 35 35 20 29 3b 0a 20 20 69 66 28 20 70 <=255 );. if( p
14d6a 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 ageSize>=512 &&
14d6b 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 pageSize<=SQLITE
14d6c 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 _MAX_PAGE_SIZE &
14d6d 26 0a 20 20 20 20 20 20 20 20 28 28 70 61 67 65 &. ((page
14d6e 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 Size-1)&pageSize
14d6f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 )==0 ){. asse
14d70 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 rt( (pageSize &
14d71 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 7)==0 );. ass
14d72 65 72 74 28 20 21 70 42 74 2d 3e 70 50 61 67 65 ert( !pBt->pPage
14d73 31 20 26 26 20 21 70 42 74 2d 3e 70 43 75 72 73 1 && !pBt->pCurs
14d74 6f 72 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 or );. pBt->p
14d75 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 ageSize = (u16)p
14d76 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65 ageSize;. fre
14d77 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
14d78 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
14d79 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 3PagerSetPagesiz
14d7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 e(pBt->pPager, &
14d7b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a pBt->pageSize);.
14d7c 20 20 7d 0a 20 20 70 42 74 2d 3e 75 73 61 62 6c }. pBt->usabl
14d7d 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 eSize = pBt->pag
14d7e 65 53 69 7a 65 20 2d 20 28 75 31 36 29 6e 52 65 eSize - (u16)nRe
14d7f 73 65 72 76 65 3b 0a 20 20 73 71 6c 69 74 65 33 serve;. sqlite3
14d80 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
14d81 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
14d82 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
14d83 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 currently define
14d84 64 20 70 61 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 d page size.*/.S
14d85 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
14d86 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 t sqlite3BtreeGe
14d87 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 tPageSize(Btree
14d88 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
14d89 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b 0a >pBt->pageSize;.
14d8a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
14d8b 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
14d8c 65 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65 eGetReserve(Btre
14d8d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a e *p){. int n;.
14d8e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
14d8f 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d ter(p);. n = p-
14d90 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d >pBt->pageSize -
14d91 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 p->pBt->usableS
14d92 69 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ize;. sqlite3Bt
14d93 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
14d94 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn n;.}../*.*
14d95 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 * Set the maximu
14d96 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 m page count for
14d97 20 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d a database if m
14d98 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 xPage is positiv
14d99 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 e..** No changes
14d9a 20 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 are made if mxP
14d9b 61 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 age is 0 or nega
14d9c 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c tive..** Regardl
14d9d 65 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 ess of the value
14d9e 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 of mxPage, retu
14d9f 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 rn the maximum p
14da0 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 age count..*/.SQ
14da1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
14da2 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 sqlite3BtreeMax
14da3 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 PageCount(Btree
14da4 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b *p, int mxPage){
14da5 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 . int n;. sqli
14da6 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
14da7 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 ;. n = sqlite3P
14da8 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 agerMaxPageCount
14da9 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c (p->pBt->pPager,
14daa 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 mxPage);. sqli
14dab 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
14dac 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a ;. return n;.}.
14dad 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
14dae 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 ed(SQLITE_OMIT_P
14daf 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c AGER_PRAGMAS) ||
14db0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
14db1 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f _OMIT_VACUUM) */
14db2 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
14db3 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 he 'auto-vacuum'
14db4 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 property of the
14db5 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 database. If th
14db6 65 20 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a e 'autoVacuum'.*
14db7 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e * parameter is n
14db8 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 on-zero, then au
14db9 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 to-vacuum mode i
14dba 73 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 s enabled. If ze
14dbb 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 ro, it.** is dis
14dbc 61 62 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75 abled. The defau
14dbd 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
14dbe 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f auto-vacuum pro
14dbf 70 65 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 perty is .** det
14dc0 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53 ermined by the S
14dc1 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 QLITE_DEFAULT_AU
14dc2 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a TOVACUUM macro..
14dc3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14dc4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
14dc5 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 eeSetAutoVacuum(
14dc6 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 Btree *p, int au
14dc7 74 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 toVacuum){.#ifde
14dc8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
14dc9 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 TOVACUUM. retur
14dca 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c n SQLITE_READONL
14dcb 59 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 Y;.#else. BtSha
14dcc 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
14dcd 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 t;. int rc = SQ
14dce 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 LITE_OK;. u8 av
14dcf 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 20 3f 31 = autoVacuum ?1
14dd0 3a 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 :0;.. sqlite3Bt
14dd1 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 reeEnter(p);. i
14dd2 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 f( pBt->pageSize
14dd3 46 69 78 65 64 20 26 26 20 61 76 21 3d 70 42 74 Fixed && av!=pBt
14dd4 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
14dd5 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
14dd6 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c 73 READONLY;. }els
14dd7 65 7b 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f e{. pBt->auto
14dd8 56 61 63 75 75 6d 20 3d 20 61 76 3b 0a 20 20 7d Vacuum = av;. }
14dd9 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
14dda 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
14ddb 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a n rc;.#endif.}..
14ddc 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
14ddd 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 27 61 value of the 'a
14dde 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 uto-vacuum' prop
14ddf 65 72 74 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 erty. If auto-va
14de0 63 75 75 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 cuum is .** enab
14de1 6c 65 64 20 31 20 69 73 20 72 65 74 75 72 6e 65 led 1 is returne
14de2 64 2e 20 4f 74 68 65 72 77 69 73 65 20 30 2e 0a d. Otherwise 0..
14de3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14de4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
14de5 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 eeGetAutoVacuum(
14de6 42 74 72 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 Btree *p){.#ifde
14de7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
14de8 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 TOVACUUM. retur
14de9 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 n BTREE_AUTOVACU
14dea 55 4d 5f 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 UM_NONE;.#else.
14deb 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
14dec 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
14ded 0a 20 20 72 63 20 3d 20 28 0a 20 20 20 20 28 21 . rc = (. (!
14dee 70 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 p->pBt->autoVacu
14def 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 um)?BTREE_AUTOVA
14df0 43 55 55 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 CUUM_NONE:. (
14df1 21 70 2d 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 !p->pBt->incrVac
14df2 75 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 uum)?BTREE_AUTOV
14df3 41 43 55 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 ACUUM_FULL:.
14df4 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
14df5 5f 49 4e 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c _INCR. );. sql
14df6 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
14df7 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
14df8 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a #endif.}.../*.**
14df9 20 47 65 74 20 61 20 72 65 66 65 72 65 6e 63 65 Get a reference
14dfa 20 74 6f 20 70 50 61 67 65 31 20 6f 66 20 74 68 to pPage1 of th
14dfb 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
14dfc 20 20 54 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 This will.** a
14dfd 6c 73 6f 20 61 63 71 75 69 72 65 20 61 20 72 65 lso acquire a re
14dfe 61 64 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 66 adlock on that f
14dff 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 ile..**.** SQLIT
14e00 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
14e01 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 on success. If
14e02 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 the file is not
14e03 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 a.** well-forme
14e04 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c d database file,
14e05 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 43 4f 52 then SQLITE_COR
14e06 52 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 RUPT is returned
14e07 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 ..** SQLITE_BUSY
14e08 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
14e09 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
14e0a 6c 6f 63 6b 65 64 2e 20 20 53 51 4c 49 54 45 5f locked. SQLITE_
14e0b 4e 4f 4d 45 4d 0a 2a 2a 20 69 73 20 72 65 74 75 NOMEM.** is retu
14e0c 72 6e 65 64 20 69 66 20 77 65 20 72 75 6e 20 6f rned if we run o
14e0d 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a ut of memory. .*
14e0e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 /.static int loc
14e0f 6b 42 74 72 65 65 28 42 74 53 68 61 72 65 64 20 kBtree(BtShared
14e10 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b *pBt){. int rc;
14e11 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
14e12 65 31 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b e1;. int nPage;
14e13 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
14e14 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14e15 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
14e16 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 if( pBt->pPage1
14e17 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
14e18 4f 4b 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 OK;. rc = sqlit
14e19 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 e3BtreeGetPage(p
14e1a 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c 20 Bt, 1, &pPage1,
14e1b 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 0);. if( rc!=SQ
14e1c 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e LITE_OK ) return
14e1d 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f rc;.. /* Do so
14e1e 6d 65 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 me checking to h
14e1f 65 6c 70 20 69 6e 73 75 72 65 20 74 68 65 20 66 elp insure the f
14e20 69 6c 65 20 77 65 20 6f 70 65 6e 65 64 20 72 65 ile we opened re
14e21 61 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 61 20 76 ally is. ** a v
14e22 61 6c 69 64 20 64 61 74 61 62 61 73 65 20 66 69 alid database fi
14e23 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 72 63 20 3d le. . */. rc =
14e24 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
14e25 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 ecount(pBt->pPag
14e26 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 er, &nPage);. i
14e27 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
14e28 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 ){. goto pag
14e29 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a e1_init_failed;.
14e2a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 50 61 67 }else if( nPag
14e2b 65 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 e>0 ){. int p
14e2c 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 69 6e 74 ageSize;. int
14e2d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 usableSize;.
14e2e 20 75 38 20 2a 70 61 67 65 31 20 3d 20 70 50 61 u8 *page1 = pPa
14e2f 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 ge1->aData;.
14e30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 41 rc = SQLITE_NOTA
14e31 44 42 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 DB;. if( memc
14e32 6d 70 28 70 61 67 65 31 2c 20 7a 4d 61 67 69 63 mp(page1, zMagic
14e33 48 65 61 64 65 72 2c 20 31 36 29 21 3d 30 20 29 Header, 16)!=0 )
14e34 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 {. goto pag
14e35 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a e1_init_failed;.
14e36 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 61 }. if( pa
14e37 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 20 ge1[18]>1 ){.
14e38 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 pBt->readOnly
14e39 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 1;. }.
14e3a 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e 31 20 if( page1[19]>1
14e3b 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 ){. goto pa
14e3c 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b ge1_init_failed;
14e3d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 . }.. /* T
14e3e 68 65 20 6d 61 78 69 6d 75 6d 20 65 6d 62 65 64 he maximum embed
14e3f 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 ded fraction mus
14e40 74 20 62 65 20 65 78 61 63 74 6c 79 20 32 35 25 t be exactly 25%
14e41 2e 20 20 41 6e 64 20 74 68 65 20 6d 69 6e 69 6d . And the minim
14e42 75 6d 0a 20 20 20 20 2a 2a 20 65 6d 62 65 64 64 um. ** embedd
14e43 65 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 ed fraction must
14e44 20 62 65 20 31 32 2e 35 25 20 66 6f 72 20 62 6f be 12.5% for bo
14e45 74 68 20 6c 65 61 66 2d 64 61 74 61 20 61 6e 64 th leaf-data and
14e46 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 2e 0a non-leaf-data..
14e47 20 20 20 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 ** The origi
14e48 6e 61 6c 20 64 65 73 69 67 6e 20 61 6c 6c 6f 77 nal design allow
14e49 65 64 20 74 68 65 73 65 20 61 6d 6f 75 6e 74 73 ed these amounts
14e4a 20 74 6f 20 76 61 72 79 2c 20 62 75 74 20 61 73 to vary, but as
14e4b 20 6f 66 0a 20 20 20 20 2a 2a 20 76 65 72 73 69 of. ** versi
14e4c 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 20 72 65 71 on 3.6.0, we req
14e4d 75 69 72 65 20 74 68 65 6d 20 74 6f 20 62 65 20 uire them to be
14e4e 66 69 78 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 fixed.. */.
14e4f 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26 70 61 if( memcmp(&pa
14e50 67 65 31 5b 32 31 5d 2c 20 22 5c 31 30 30 5c 30 ge1[21], "\100\0
14e51 34 30 5c 30 34 30 22 2c 33 29 21 3d 30 20 29 7b 40\040",3)!=0 ){
14e52 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 . goto page
14e53 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 1_init_failed;.
14e54 20 20 20 7d 0a 20 20 20 20 70 61 67 65 53 69 7a }. pageSiz
14e55 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 70 61 e = get2byte(&pa
14e56 67 65 31 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 ge1[16]);. if
14e57 28 20 28 28 70 61 67 65 53 69 7a 65 2d 31 29 26 ( ((pageSize-1)&
14e58 70 61 67 65 53 69 7a 65 29 21 3d 30 20 7c 7c 20 pageSize)!=0 ||
14e59 70 61 67 65 53 69 7a 65 3c 35 31 32 20 7c 7c 0a pageSize<512 ||.
14e5a 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
14e5b 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 3c 33 32 MAX_PAGE_SIZE<32
14e5c 37 36 38 20 26 26 20 70 61 67 65 53 69 7a 65 3e 768 && pageSize>
14e5d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
14e5e 53 49 5a 45 29 0a 20 20 20 20 29 7b 0a 20 20 20 SIZE). ){.
14e5f 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e goto page1_in
14e60 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d it_failed;. }
14e61 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 61 . assert( (pa
14e62 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 geSize & 7)==0 )
14e63 3b 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65 ;. usableSize
14e64 20 3d 20 70 61 67 65 53 69 7a 65 20 2d 20 70 61 = pageSize - pa
14e65 67 65 31 5b 32 30 5d 3b 0a 20 20 20 20 69 66 28 ge1[20];. if(
14e66 20 70 61 67 65 53 69 7a 65 21 3d 70 42 74 2d 3e pageSize!=pBt->
14e67 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 pageSize ){.
14e68 20 20 2f 2a 20 41 66 74 65 72 20 72 65 61 64 69 /* After readi
14e69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 67 ng the first pag
14e6a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
14e6b 65 20 61 73 73 75 6d 69 6e 67 20 61 20 70 61 67 e assuming a pag
14e6c 65 20 73 69 7a 65 0a 20 20 20 20 20 20 2a 2a 20 e size. **
14e6d 6f 66 20 42 74 53 68 61 72 65 64 2e 70 61 67 65 of BtShared.page
14e6e 53 69 7a 65 2c 20 77 65 20 68 61 76 65 20 64 69 Size, we have di
14e6f 73 63 6f 76 65 72 65 64 20 74 68 61 74 20 74 68 scovered that th
14e70 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73 0a 20 e page-size is.
14e71 20 20 20 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 ** actually
14e72 20 70 61 67 65 53 69 7a 65 2e 20 55 6e 6c 6f 63 pageSize. Unloc
14e73 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 k the database,
14e74 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61 67 65 leave pBt->pPage
14e75 31 20 61 74 0a 20 20 20 20 20 20 2a 2a 20 7a 65 1 at. ** ze
14e76 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 ro and return SQ
14e77 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20 63 61 6c LITE_OK. The cal
14e78 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c 20 74 68 ler will call th
14e79 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 is function.
14e7a 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 74 68 20 ** again with
14e7b 74 68 65 20 63 6f 72 72 65 63 74 20 70 61 67 65 the correct page
14e7c 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20 2a 2f 0a -size.. */.
14e7d 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
14e7e 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20 20 e(pPage1);.
14e7f 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
14e80 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53 69 = (u16)usableSi
14e81 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 ze;. pBt->p
14e82 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 ageSize = (u16)p
14e83 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 ageSize;. f
14e84 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 reeTempSpace(pBt
14e85 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
14e86 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
14e87 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 (pBt->pPager, &p
14e88 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 Bt->pageSize);.
14e89 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
14e8a 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 TE_OK;. }.
14e8b 20 69 66 28 20 75 73 61 62 6c 65 53 69 7a 65 3c if( usableSize<
14e8c 35 30 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 500 ){. got
14e8d 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 o page1_init_fai
14e8e 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 led;. }. p
14e8f 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 Bt->pageSize = (
14e90 75 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 u16)pageSize;.
14e91 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a pBt->usableSiz
14e92 65 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53 e = (u16)usableS
14e93 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c ize;.#ifndef SQL
14e94 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
14e95 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 UUM. pBt->aut
14e96 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 oVacuum = (get4b
14e97 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20 yte(&page1[36 +
14e98 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 4*4])?1:0);.
14e99 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 pBt->incrVacuum
14e9a 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61 67 = (get4byte(&pag
14e9b 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a e1[36 + 7*4])?1:
14e9c 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0);.#endif. }..
14e9d 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 /* maxLocal is
14e9e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f the maximum amo
14e9f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 unt of payload t
14ea0 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 o store locally
14ea1 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e for. ** a cell.
14ea2 20 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 Make sure it i
14ea3 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 s small enough s
14ea4 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 o that at least
14ea5 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 minFanout. ** c
14ea6 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 ells can will fi
14ea7 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 t on one page.
14ea8 57 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d 62 We assume a 10-b
14ea9 79 74 65 20 70 61 67 65 20 68 65 61 64 65 72 2e yte page header.
14eaa 0a 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74 68 . ** Besides th
14eab 65 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 e payload, the c
14eac 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a ell must store:.
14ead 20 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 20 ** 2-byte
14eae 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 pointer to the c
14eaf 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 ell. ** 4-b
14eb0 79 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 yte child pointe
14eb1 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 r. ** 9-byt
14eb2 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a e nKey value. *
14eb3 2a 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44 61 * 4-byte nDa
14eb4 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 ta value. **
14eb5 20 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 4-byte overflo
14eb6 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 w page pointer.
14eb7 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f ** So a cell co
14eb8 6e 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62 79 nsists of a 2-by
14eb9 74 65 20 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 te poiner, a hea
14eba 64 65 72 20 77 68 69 63 68 20 69 73 20 61 73 20 der which is as
14ebb 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 much as. ** 17
14ebc 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f bytes long, 0 to
14ebd 20 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79 6c N bytes of payl
14ebe 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 oad, and an opti
14ebf 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65 72 onal 4 byte over
14ec0 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 flow. ** page p
14ec1 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 ointer.. */. p
14ec2 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 Bt->maxLocal = (
14ec3 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d pBt->usableSize-
14ec4 31 32 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 12)*64/255 - 23;
14ec5 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c . pBt->minLocal
14ec6 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 = (pBt->usableS
14ec7 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d ize-12)*32/255 -
14ec8 20 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 23;. pBt->maxL
14ec9 65 61 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c eaf = pBt->usabl
14eca 65 53 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 eSize - 35;. pB
14ecb 74 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 t->minLeaf = (pB
14ecc 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 t->usableSize-12
14ecd 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 )*32/255 - 23;.
14ece 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 assert( pBt->ma
14ecf 78 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 xLeaf + 23 <= MX
14ed0 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 _CELL_SIZE(pBt)
14ed1 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 );. pBt->pPage1
14ed2 20 3d 20 70 50 61 67 65 31 3b 0a 20 20 72 65 74 = pPage1;. ret
14ed3 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a urn SQLITE_OK;..
14ed4 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
14ed5 64 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 d:. releasePage
14ed6 28 70 50 61 67 65 31 29 3b 0a 20 20 70 42 74 2d (pPage1);. pBt-
14ed7 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 >pPage1 = 0;. r
14ed8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
14ed9 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
14eda 77 6f 72 6b 73 20 6c 69 6b 65 20 6c 6f 63 6b 42 works like lockB
14edb 74 72 65 65 28 29 20 65 78 63 65 70 74 20 74 68 tree() except th
14edc 61 74 20 69 74 20 61 6c 73 6f 20 69 6e 76 6f 6b at it also invok
14edd 65 73 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 es the.** busy c
14ede 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72 65 allback if there
14edf 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 is lock content
14ee0 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ion..*/.static i
14ee1 6e 74 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 nt lockBtreeWith
14ee2 52 65 74 72 79 28 42 74 72 65 65 20 2a 70 52 65 Retry(Btree *pRe
14ee3 66 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 f){. int rc = S
14ee4 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 QLITE_OK;.. ass
14ee5 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
14ee6 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 52 65 66 eHoldsMutex(pRef
14ee7 29 20 29 3b 0a 20 20 69 66 28 20 70 52 65 66 2d ) );. if( pRef-
14ee8 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
14ee9 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 75 38 20 69 NONE ){. u8 i
14eea 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 nTransaction = p
14eeb 52 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e Ref->pBt->inTran
14eec 73 61 63 74 69 6f 6e 3b 0a 20 20 20 20 62 74 72 saction;. btr
14eed 65 65 49 6e 74 65 67 72 69 74 79 28 70 52 65 66 eeIntegrity(pRef
14eee 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
14eef 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 te3BtreeBeginTra
14ef0 6e 73 28 70 52 65 66 2c 20 30 29 3b 0a 20 20 20 ns(pRef, 0);.
14ef1 20 70 52 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 pRef->pBt->inTr
14ef2 61 6e 73 61 63 74 69 6f 6e 20 3d 20 69 6e 54 72 ansaction = inTr
14ef3 61 6e 73 61 63 74 69 6f 6e 3b 0a 20 20 20 20 70 ansaction;. p
14ef4 52 65 66 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 Ref->inTrans = T
14ef5 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 69 RANS_NONE;. i
14ef6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
14ef7 20 29 7b 0a 20 20 20 20 20 20 70 52 65 66 2d 3e ){. pRef->
14ef8 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f pBt->nTransactio
14ef9 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 n--;. }. b
14efa 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 52 treeIntegrity(pR
14efb 65 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ef);. }. retur
14efc 6e 20 72 63 3b 0a 7d 0a 20 20 20 20 20 20 20 0a n rc;.}. .
14efd 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 ./*.** If there
14efe 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 are no outstandi
14eff 6e 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20 77 ng cursors and w
14f00 65 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 e are not in the
14f01 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20 middle.** of a
14f02 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74 20 transaction but
14f03 74 68 65 72 65 20 69 73 20 61 20 72 65 61 64 20 there is a read
14f04 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
14f05 62 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 base, then.** th
14f06 69 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65 66 is routine unref
14f07 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 s the first page
14f08 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
14f09 20 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20 file which .**
14f0a 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f has the effect o
14f0b 66 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20 f releasing the
14f0c 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a read lock..**.**
14f0d 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e If there are an
14f0e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 y outstanding cu
14f0f 72 73 6f 72 73 2c 20 74 68 69 73 20 72 6f 75 74 rsors, this rout
14f10 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
14f11 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 **.** If there i
14f12 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 s a transaction
14f13 69 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 69 in progress, thi
14f14 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
14f15 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 o-op..*/.static
14f16 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 void unlockBtree
14f17 49 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 65 IfUnused(BtShare
14f18 64 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 72 d *pBt){. asser
14f19 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14f1a 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
14f1b 29 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e ) );. if( pBt->
14f1c 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
14f1d 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 RANS_NONE && pBt
14f1e 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 26 26 20 ->pCursor==0 &&
14f1f 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 pBt->pPage1!=0 )
14f20 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
14f21 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 3PagerRefcount(p
14f22 42 74 2d 3e 70 50 61 67 65 72 29 3e 3d 31 20 29 Bt->pPager)>=1 )
14f23 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
14f24 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
14f25 74 61 20 29 3b 0a 23 69 66 20 30 0a 20 20 20 20 ta );.#if 0.
14f26 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 if( pBt->pPage
14f27 31 2d 3e 61 44 61 74 61 3d 3d 30 20 29 7b 0a 20 1->aData==0 ){.
14f28 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a MemPage *
14f29 70 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 50 61 pPage = pBt->pPa
14f2a 67 65 31 3b 0a 20 20 20 20 20 20 20 20 70 50 61 ge1;. pPa
14f2b 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 ge->aData = sqli
14f2c 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
14f2d 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b pPage->pDbPage);
14f2e 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e . pPage->
14f2f 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 pBt = pBt;.
14f30 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d pPage->pgno =
14f31 20 31 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 1;. }.#end
14f32 69 66 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 if. release
14f33 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 31 Page(pBt->pPage1
14f34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 );. }. pBt
14f35 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 ->pPage1 = 0;.
14f36 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 pBt->inStmt =
14f37 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0;. }.}../*.**
14f38 43 72 65 61 74 65 20 61 20 6e 65 77 20 64 61 74 Create a new dat
14f39 61 62 61 73 65 20 62 79 20 69 6e 69 74 69 61 6c abase by initial
14f3a 69 7a 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 izing the first
14f3b 70 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 page of the.** f
14f3c 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
14f3d 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28 42 nt newDatabase(B
14f3e 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
14f3f 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20 MemPage *pP1;.
14f40 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
14f41 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a data;. int rc;.
14f42 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 int nPage;..
14f43 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14f44 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
14f45 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d mutex) );. rc =
14f46 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
14f47 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 ecount(pBt->pPag
14f48 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 er, &nPage);. i
14f49 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
14f4a 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 || nPage>0 ){.
14f4b 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
14f4c 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 }. pP1 = pBt->p
14f4d 50 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74 28 Page1;. assert(
14f4e 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 74 pP1!=0 );. dat
14f4f 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a a = pP1->aData;.
14f50 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
14f51 67 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70 44 gerWrite(pP1->pD
14f52 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 bPage);. if( rc
14f53 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
14f54 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d 61 memcpy(data, zMa
14f55 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65 6f gicHeader, sizeo
14f56 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 29 f(zMagicHeader))
14f57 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 ;. assert( size
14f58 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 of(zMagicHeader)
14f59 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62 79 ==16 );. put2by
14f5a 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 42 te(&data[16], pB
14f5b 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 t->pageSize);.
14f5c 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 data[18] = 1;.
14f5d 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 data[19] = 1;.
14f5e 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 assert( pBt->usa
14f5f 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 bleSize<=pBt->pa
14f60 67 65 53 69 7a 65 20 26 26 20 70 42 74 2d 3e 75 geSize && pBt->u
14f61 73 61 62 6c 65 53 69 7a 65 2b 32 35 35 3e 3d 70 sableSize+255>=p
14f62 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 Bt->pageSize);.
14f63 20 64 61 74 61 5b 32 30 5d 20 3d 20 28 75 38 29 data[20] = (u8)
14f64 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d (pBt->pageSize -
14f65 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
14f66 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20 3d 20 );. data[21] =
14f67 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d 20 3d 64;. data[22] =
14f68 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33 5d 20 32;. data[23]
14f69 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74 28 26 = 32;. memset(&
14f6a 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31 30 30 data[24], 0, 100
14f6b 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 -24);. zeroPage
14f6c 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b 45 59 (pP1, PTF_INTKEY
14f6d 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 |PTF_LEAF|PTF_LE
14f6e 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42 74 2d AFDATA );. pBt-
14f6f 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d >pageSizeFixed =
14f70 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 1;.#ifndef SQLI
14f71 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
14f72 55 4d 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 UM. assert( pBt
14f73 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 31 20 ->autoVacuum==1
14f74 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 || pBt->autoVacu
14f75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 um==0 );. asser
14f76 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 t( pBt->incrVacu
14f77 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 6e um==1 || pBt->in
14f78 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 crVacuum==0 );.
14f79 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b put4byte(&data[
14f7a 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74 2d 3e 36 + 4*4], pBt->
14f7b 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 70 autoVacuum);. p
14f7c 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 33 36 ut4byte(&data[36
14f7d 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e + 7*4], pBt->in
14f7e 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 6e 64 69 crVacuum);.#endi
14f7f 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 f. return SQLIT
14f80 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 E_OK;.}../*.** A
14f81 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74 20 ttempt to start
14f82 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f a new transactio
14f83 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 n. A write-trans
14f84 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 action.** is sta
14f85 72 74 65 64 20 69 66 20 74 68 65 20 73 65 63 6f rted if the seco
14f86 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e nd argument is n
14f87 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 onzero, otherwis
14f88 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61 e a read-.** tra
14f89 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 nsaction. If th
14f8a 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
14f8b 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 t is 2 or more a
14f8c 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 nd exclusive.**
14f8d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
14f8e 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 tarted, meaning
14f8f 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 that no other pr
14f90 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 ocess is allowed
14f91 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68 .** to access th
14f92 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 70 e database. A p
14f93 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e 73 reexisting trans
14f94 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 action may not b
14f95 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74 6f e.** upgraded to
14f96 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 63 61 exclusive by ca
14f97 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 lling this routi
14f98 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 ne a second time
14f99 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73 - the.** exclus
14f9a 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20 ivity flag only
14f9b 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 20 works for a new
14f9c 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
14f9d 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 ** A write-trans
14f9e 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 action must be s
14f9f 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 74 tarted before at
14fa0 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a tempting any .**
14fa1 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 changes to the
14fa2 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 database. None
14fa3 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
14fa4 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69 routines .** wi
14fa5 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61 ll work unless a
14fa6 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
14fa7 73 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a 2a started first:.*
14fa8 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 *.** sqlite
14fa9 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 3BtreeCreateTabl
14faa 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 e().** sqli
14fab 74 65 33 42 74 72 65 65 43 72 65 61 74 65 49 6e te3BtreeCreateIn
14fac 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 dex().** sq
14fad 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 lite3BtreeClearT
14fae 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 able().** s
14faf 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 qlite3BtreeDropT
14fb0 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 able().** s
14fb1 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 qlite3BtreeInser
14fb2 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 t().** sqli
14fb3 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 29 te3BtreeDelete()
14fb4 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
14fb5 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 BtreeUpdateMeta(
14fb6 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e ).**.** If an in
14fb7 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f itial attempt to
14fb8 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63 acquire the loc
14fb9 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 20 k fails because
14fba 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 of lock contenti
14fbb 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 on.** and the da
14fbc 74 61 62 61 73 65 20 77 61 73 20 70 72 65 76 69 tabase was previ
14fbd 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 ously unlocked,
14fbe 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20 then invoke the
14fbf 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 busy handler.**
14fc0 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e if there is one.
14fc1 20 20 42 75 74 20 69 66 20 74 68 65 72 65 20 77 But if there w
14fc2 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 20 as previously a
14fc3 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f read-lock, do no
14fc4 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 t.** invoke the
14fc5 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a busy handler - j
14fc6 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ust return SQLIT
14fc7 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45 5f E_BUSY. SQLITE_
14fc8 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 75 BUSY is .** retu
14fc9 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 rned when there
14fca 69 73 20 61 6c 72 65 61 64 79 20 61 20 72 65 61 is already a rea
14fcb 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20 d-lock in order
14fcc 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64 6c to avoid a deadl
14fcd 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f ock..**.** Suppo
14fce 73 65 20 74 68 65 72 65 20 61 72 65 20 74 77 6f se there are two
14fcf 20 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e 64 processes A and
14fd0 20 42 2e 20 20 41 20 68 61 73 20 61 20 72 65 61 B. A has a rea
14fd1 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 73 d lock and B has
14fd2 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20 6c .** a reserved l
14fd3 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74 6f ock. B tries to
14fd4 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c promote to excl
14fd5 75 73 69 76 65 20 62 75 74 20 69 73 20 62 6c 6f usive but is blo
14fd6 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 cked because.**
14fd7 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63 6b of A's read lock
14fd8 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20 70 72 . A tries to pr
14fd9 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76 65 omote to reserve
14fda 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 d but is blocked
14fdb 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 by B..** One or
14fdc 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 the other of th
14fdd 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 e two processes
14fde 6d 75 73 74 20 67 69 76 65 20 77 61 79 20 6f 72 must give way or
14fdf 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a there can be.**
14fe0 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 42 no progress. B
14fe1 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 y returning SQLI
14fe2 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20 TE_BUSY and not
14fe3 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 invoking the bus
14fe4 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 y callback.** wh
14fe5 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61 73 en A already has
14fe6 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65 a read lock, we
14fe7 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f 20 encourage A to
14fe8 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74 20 give up and let
14fe9 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f B.** proceed..*/
14fea 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14feb 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
14fec 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 BeginTrans(Btree
14fed 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 29 *p, int wrflag)
14fee 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
14fef 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e t = p->pBt;. in
14ff0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
14ff1 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ;.. sqlite3Btre
14ff2 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 eEnter(p);. pBt
14ff3 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 ->db = p->db;.
14ff4 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
14ff5 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
14ff6 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 btree is already
14ff7 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e in a write-tran
14ff8 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20 saction, or it.
14ff9 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69 ** is already i
14ffa 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 n a read-transac
14ffb 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d tion and a read-
14ffc 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a transaction. **
14ffd 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 is requested, t
14ffe 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a his is a no-op..
14fff 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e */. if( p->in
15000 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 Trans==TRANS_WRI
15001 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e TE || (p->inTran
15002 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26 s==TRANS_READ &&
15003 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20 !wrflag) ){.
15004 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 goto trans_begu
15005 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 n;. }.. /* Wri
15006 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 te transactions
15007 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 are not possible
15008 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 on a read-only
15009 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66 database */. if
1500a 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 ( pBt->readOnly
1500b 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 && wrflag ){.
1500c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 rc = SQLITE_REA
1500d 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20 DONLY;. goto
1500e 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d trans_begun;. }
1500f 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65 .. /* If anothe
15010 72 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c r database handl
15011 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f 70 e has already op
15012 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 72 61 ened a write tra
15013 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f nsaction . ** o
15014 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 74 n this shared-bt
15015 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e ree structure an
15016 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74 65 d a second write
15017 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a transaction is.
15018 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c 20 ** requested,
15019 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
1501a 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 SY.. */. if( p
1501b 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
1501c 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 n==TRANS_WRITE &
1501d 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 & wrflag ){.
1501e 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
1501f 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 ;. goto trans
15020 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23 69 66 _begun;. }..#if
15021 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
15022 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
15023 69 66 28 20 77 72 66 6c 61 67 3e 31 20 29 7b 0a if( wrflag>1 ){.
15024 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 BtLock *pIte
15025 72 3b 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72 r;. for(pIter
15026 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 =pBt->pLock; pIt
15027 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d er; pIter=pIter-
15028 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 >pNext){. i
15029 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 f( pIter->pBtree
1502a 21 3d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 72 !=p ){. r
1502b 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
1502c 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 74 72 . goto tr
1502d 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 20 20 20 ans_begun;.
1502e 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e }. }. }.#en
1502f 64 69 66 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 dif.. do {.
15030 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d if( pBt->pPage1=
15031 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 7b 0a =0 ){. do{.
15032 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 rc = loc
15033 6b 42 74 72 65 65 28 70 42 74 29 3b 0a 20 20 20 kBtree(pBt);.
15034 20 20 20 7d 77 68 69 6c 65 28 20 70 42 74 2d 3e }while( pBt->
15035 70 50 61 67 65 31 3d 3d 30 20 26 26 20 72 63 3d pPage1==0 && rc=
15036 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
15037 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d }.. if( rc=
15038 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72 =SQLITE_OK && wr
15039 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 69 66 flag ){. if
1503a 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 ( pBt->readOnly
1503b 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
1503c 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
1503d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1503e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1503f 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 42 74 e3PagerBegin(pBt
15040 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 ->pPage1->pDbPag
15041 65 2c 20 77 72 66 6c 61 67 3e 31 29 3b 0a 20 20 e, wrflag>1);.
15042 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
15043 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
15044 20 20 20 20 20 72 63 20 3d 20 6e 65 77 44 61 74 rc = newDat
15045 61 62 61 73 65 28 70 42 74 29 3b 0a 20 20 20 20 abase(pBt);.
15046 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
15047 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 72 }. . if( r
15048 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
15049 20 20 20 20 20 20 69 66 28 20 77 72 66 6c 61 67 if( wrflag
1504a 20 29 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d ) pBt->inStmt =
1504b 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
1504c 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 unlockBtree
1504d 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 IfUnused(pBt);.
1504e 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 72 }. }while( r
1504f 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 c==SQLITE_BUSY &
15050 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 & pBt->inTransac
15051 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 tion==TRANS_NONE
15052 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 62 74 &&. bt
15053 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e reeInvokeBusyHan
15054 64 6c 65 72 28 70 42 74 29 20 29 3b 0a 0a 20 20 dler(pBt) );..
15055 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
15056 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e K ){. if( p->
15057 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e inTrans==TRANS_N
15058 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ONE ){. pBt
15059 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2b 2b ->nTransaction++
1505a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 ;. }. p->i
1505b 6e 54 72 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 nTrans = (wrflag
1505c 3f 54 52 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 ?TRANS_WRITE:TRA
1505d 4e 53 5f 52 45 41 44 29 3b 0a 20 20 20 20 69 66 NS_READ);. if
1505e 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 ( p->inTrans>pBt
1505f 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 ->inTransaction
15060 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e ){. pBt->in
15061 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d Transaction = p-
15062 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a >inTrans;. }.
15063 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
15064 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
15065 0a 20 20 20 20 69 66 28 20 77 72 66 6c 61 67 3e . if( wrflag>
15066 31 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 1 ){. asser
15067 74 28 20 21 70 42 74 2d 3e 70 45 78 63 6c 75 73 t( !pBt->pExclus
15068 69 76 65 20 29 3b 0a 20 20 20 20 20 20 70 42 74 ive );. pBt
15069 2d 3e 70 45 78 63 6c 75 73 69 76 65 20 3d 20 70 ->pExclusive = p
1506a 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
1506b 20 7d 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e }...trans_begun
1506c 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 :. if( rc==SQLI
1506d 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 TE_OK && wrflag
1506e 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 ){. /* This c
1506f 61 6c 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 74 all makes sure t
15070 68 61 74 20 74 68 65 20 70 61 67 65 72 20 68 61 hat the pager ha
15071 73 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 s the correct nu
15072 6d 62 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f mber of. ** o
15073 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 pen savepoints.
15074 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 If the second pa
15075 72 61 6d 65 74 65 72 20 69 73 20 67 72 65 61 74 rameter is great
15076 65 72 20 74 68 61 6e 20 30 20 61 6e 64 0a 20 20 er than 0 and.
15077 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 ** the sub-jou
15078 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65 rnal is not alre
15079 61 64 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 ady open, then i
1507a 74 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 t will be opened
1507b 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 here.. */.
1507c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1507d 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 gerOpenSavepoint
1507e 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d (pBt->pPager, p-
1507f 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 >db->nSavepoint)
15080 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e ;. }.. btreeIn
15081 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 tegrity(p);. sq
15082 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
15083 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
15084 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
15085 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
15086 55 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 UM../*.** Set th
15087 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e e pointer-map en
15088 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 tries for all ch
15089 69 6c 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70 ildren of page p
1508a 50 61 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a Page. Also, if.*
1508b 2a 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 * pPage contains
1508c 20 63 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e cells that poin
1508d 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 t to overflow pa
1508e 67 65 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69 ges, set the poi
1508f 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 nter.** map entr
15090 69 65 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72 ies for the over
15091 66 6c 6f 77 20 70 61 67 65 73 20 61 73 20 77 65 flow pages as we
15092 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ll..*/.static in
15093 74 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 t setChildPtrmap
15094 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 s(MemPage *pPage
15095 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 ){. int i;
15096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15097 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 /* Count
15098 65 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 er variable */.
15099 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 int nCell;
1509a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1509b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1509c 20 63 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 cells in page p
1509d 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 Page */. int rc
1509e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1509f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
150a0 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
150a1 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
150a2 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 pPage->pBt;. u
150a3 38 20 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 8 isInitOrig = p
150a4 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 Page->isInit;.
150a5 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 Pgno pgno = pPag
150a6 65 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 e->pgno;.. asse
150a7 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
150a8 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
150a9 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 t->mutex) );. r
150aa 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
150ab 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b InitPage(pPage);
150ac 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
150ad 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f E_OK ){. goto
150ae 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 set_child_ptrma
150af 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e 43 ps_out;. }. nC
150b0 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 ell = pPage->nCe
150b1 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 ll;.. for(i=0;
150b2 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 i<nCell; i++){.
150b3 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 u8 *pCell = f
150b4 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 indCell(pPage, i
150b5 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72 );.. rc = ptr
150b6 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 mapPutOvflPtr(pP
150b7 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 age, pCell);.
150b8 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
150b9 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f OK ){. goto
150ba 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 set_child_ptrma
150bb 70 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 ps_out;. }..
150bc 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
150bd 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e eaf ){. Pgn
150be 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 o childPgno = ge
150bf 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20 t4byte(pCell);.
150c0 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 rc = ptrmap
150c1 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 Put(pBt, childPg
150c2 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 no, PTRMAP_BTREE
150c3 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 , pgno);. i
150c4 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
150c5 20 29 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c ) goto set_chil
150c6 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 d_ptrmaps_out;.
150c7 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
150c8 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
150c9 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 Pgno childPg
150ca 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
150cb 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
150cc 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
150cd 3b 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 ;. rc = ptrma
150ce 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 pPut(pBt, childP
150cf 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
150d0 45 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 0a 73 E, pgno);. }..s
150d1 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 et_child_ptrmaps
150d2 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 _out:. pPage->i
150d3 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 sInit = isInitOr
150d4 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ig;. return rc;
150d5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 .}../*.** Somewh
150d6 65 72 65 20 6f 6e 20 70 50 61 67 65 2c 20 77 68 ere on pPage, wh
150d7 69 63 68 20 69 73 20 67 75 61 72 65 6e 74 65 65 ich is guarentee
150d8 64 20 74 6f 20 62 65 20 61 20 62 74 72 65 65 20 d to be a btree
150d9 70 61 67 65 2c 20 6e 6f 74 20 61 6e 20 6f 76 65 page, not an ove
150da 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 69 rflow.** page, i
150db 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 s a pointer to p
150dc 61 67 65 20 69 46 72 6f 6d 2e 20 4d 6f 64 69 66 age iFrom. Modif
150dd 79 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73 y this pointer s
150de 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 o that it points
150df 20 74 6f 0a 2a 2a 20 69 54 6f 2e 20 50 61 72 61 to.** iTo. Para
150e0 6d 65 74 65 72 20 65 54 79 70 65 20 64 65 73 63 meter eType desc
150e1 72 69 62 65 73 20 74 68 65 20 74 79 70 65 20 6f ribes the type o
150e2 66 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 f pointer to be
150e3 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 0a 2a 2a modified, as .**
150e4 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
150e5 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 PTRMAP_BTREE:
150e6 20 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 pPage is a btr
150e7 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 ee-page. The poi
150e8 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 nter points at a
150e9 20 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 child .**
150ea 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 pag
150eb 65 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a e of pPage..**.*
150ec 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f * PTRMAP_OVERFLO
150ed 57 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 W1: pPage is a b
150ee 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 tree-page. The p
150ef 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 ointer points at
150f0 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 an overflow.**
150f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
150f2 20 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 page pointed t
150f3 6f 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 o by one of the
150f4 63 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a cells on pPage..
150f5 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 **.** PTRMAP_OVE
150f6 52 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73 RFLOW2: pPage is
150f7 20 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 an overflow-pag
150f8 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 e. The pointer p
150f9 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78 oints at the nex
150fa 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
150fb 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 overflow
150fc 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 page in the list
150fd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
150fe 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65 modifyPagePointe
150ff 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 r(MemPage *pPage
15100 2c 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 , Pgno iFrom, Pg
15101 6e 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65 no iTo, u8 eType
15102 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
15103 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15104 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
15105 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
15106 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
15107 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
15108 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 DbPage) );. if(
15109 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f eType==PTRMAP_O
1510a 56 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20 VERFLOW2 ){.
1510b 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 /* The pointer i
1510c 73 20 61 6c 77 61 79 73 20 74 68 65 20 66 69 72 s always the fir
1510d 73 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68 st 4 bytes of th
1510e 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 e page in this c
1510f 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 ase. */. if(
15110 20 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d get4byte(pPage-
15111 3e 61 44 61 74 61 29 21 3d 69 46 72 6f 6d 20 29 >aData)!=iFrom )
15112 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
15113 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
15114 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 PT;. }. pu
15115 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 t4byte(pPage->aD
15116 61 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c ata, iTo);. }el
15117 73 65 7b 0a 20 20 20 20 75 38 20 69 73 49 6e 69 se{. u8 isIni
15118 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 tOrig = pPage->i
15119 73 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 sInit;. int i
1511a 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b ;. int nCell;
1511b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 .. sqlite3Btr
1511c 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 eeInitPage(pPage
1511d 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 70 );. nCell = p
1511e 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 Page->nCell;..
1511f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 for(i=0; i<nCe
15120 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ll; i++){.
15121 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 u8 *pCell = find
15122 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a Cell(pPage, i);.
15123 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d if( eType=
15124 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 =PTRMAP_OVERFLOW
15125 31 20 29 7b 0a 20 20 20 20 20 20 20 20 43 65 6c 1 ){. Cel
15126 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 lInfo info;.
15127 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
15128 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 ParseCellPtr(pPa
15129 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f ge, pCell, &info
1512a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 );. if( i
1512b 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b nfo.iOverflow ){
1512c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 . if( i
1512d 46 72 6f 6d 3d 3d 67 65 74 34 62 79 74 65 28 26 From==get4byte(&
1512e 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 pCell[info.iOver
1512f 66 6c 6f 77 5d 29 20 29 7b 0a 20 20 20 20 20 20 flow]) ){.
15130 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
15131 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 pCell[info.iOver
15132 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 flow], iTo);.
15133 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
15134 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
15135 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
15136 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 67 e{. if( g
15137 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d et4byte(pCell)==
15138 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 iFrom ){.
15139 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65 6c put4byte(pCel
1513a 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 l, iTo);.
1513b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1513c 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1513d 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 69 3d 3d }. . if( i==
1513e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 nCell ){. i
1513f 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 f( eType!=PTRMAP
15140 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 _BTREE || .
15141 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26 70 get4byte(&p
15142 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
15143 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
15144 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 !=iFrom ){.
15145 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15146 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
15147 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 75 74 }. put
15148 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
15149 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 ata[pPage->hdrOf
1514a 66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 fset+8], iTo);.
1514b 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d }.. pPage-
1514c 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 >isInit = isInit
1514d 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Orig;. }. retu
1514e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1514f 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 ../*.** Move the
15150 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 70 open database p
15151 61 67 65 20 70 44 62 50 61 67 65 20 74 6f 20 6c age pDbPage to l
15152 6f 63 61 74 69 6f 6e 20 69 46 72 65 65 50 61 67 ocation iFreePag
15153 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 e in the .** dat
15154 61 62 61 73 65 2e 20 54 68 65 20 70 44 62 50 61 abase. The pDbPa
15155 67 65 20 72 65 66 65 72 65 6e 63 65 20 72 65 6d ge reference rem
15156 61 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2f 0a 73 ains valid..*/.s
15157 74 61 74 69 63 20 69 6e 74 20 72 65 6c 6f 63 61 tatic int reloca
15158 74 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 tePage(. BtShar
15159 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 ed *pBt,
1515a 20 20 20 2f 2a 20 42 74 72 65 65 20 2a 2f 0a 20 /* Btree */.
1515b 20 4d 65 6d 50 61 67 65 20 2a 70 44 62 50 61 67 MemPage *pDbPag
1515c 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 e, /* Ope
1515d 6e 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 2a n page to move *
1515e 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20 /. u8 eType,
1515f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15160 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 74 79 70 Pointer map 'typ
15161 65 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62 e' entry for pDb
15162 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 Page */. Pgno i
15163 50 74 72 50 61 67 65 2c 20 20 20 20 20 20 20 20 PtrPage,
15164 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 /* Pointer ma
15165 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 6e 74 72 p 'page-no' entr
15166 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f y for pDbPage */
15167 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 50 61 67 . Pgno iFreePag
15168 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 e, /* T
15169 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 6d he location to m
1516a 6f 76 65 20 70 44 62 50 61 67 65 20 74 6f 20 2a ove pDbPage to *
1516b 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 /. int isCommit
1516c 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 .){. MemPage *p
1516d 50 74 72 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 PtrPage; /* Th
1516e 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 e page that cont
1516f 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 ains a pointer t
15170 6f 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 o pDbPage */. P
15171 67 6e 6f 20 69 44 62 50 61 67 65 20 3d 20 70 44 gno iDbPage = pD
15172 62 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 50 bPage->pgno;. P
15173 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
15174 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e Bt->pPager;. in
15175 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
15176 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f eType==PTRMAP_O
15177 56 45 52 46 4c 4f 57 32 20 7c 7c 20 65 54 79 70 VERFLOW2 || eTyp
15178 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c e==PTRMAP_OVERFL
15179 4f 57 31 20 7c 7c 20 0a 20 20 20 20 20 20 65 54 OW1 || . eT
1517a 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 ype==PTRMAP_BTRE
1517b 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d E || eType==PTRM
1517c 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 AP_ROOTPAGE );.
1517d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1517e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
1517f 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
15180 65 72 74 28 20 70 44 62 50 61 67 65 2d 3e 70 42 ert( pDbPage->pB
15181 74 3d 3d 70 42 74 20 29 3b 0a 0a 20 20 2f 2a 20 t==pBt );.. /*
15182 4d 6f 76 65 20 70 61 67 65 20 69 44 62 50 61 67 Move page iDbPag
15183 65 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72 65 e from its curre
15184 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 70 nt location to p
15185 61 67 65 20 6e 75 6d 62 65 72 20 69 46 72 65 65 age number iFree
15186 50 61 67 65 20 2a 2f 0a 20 20 54 52 41 43 45 28 Page */. TRACE(
15187 28 22 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d 6f ("AUTOVACUUM: Mo
15188 76 69 6e 67 20 25 64 20 74 6f 20 66 72 65 65 20 ving %d to free
15189 70 61 67 65 20 25 64 20 28 70 74 72 20 70 61 67 page %d (ptr pag
1518a 65 20 25 64 20 74 79 70 65 20 25 64 29 5c 6e 22 e %d type %d)\n"
1518b 2c 20 0a 20 20 20 20 20 20 69 44 62 50 61 67 65 , . iDbPage
1518c 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 50 74 , iFreePage, iPt
1518d 72 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b 0a rPage, eType));.
1518e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1518f 67 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61 67 gerMovepage(pPag
15190 65 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44 62 er, pDbPage->pDb
15191 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c Page, iFreePage,
15192 20 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 66 isCommit);. if
15193 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
15194 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
15195 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65 2d ;. }. pDbPage-
15196 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61 67 >pgno = iFreePag
15197 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62 50 e;.. /* If pDbP
15198 61 67 65 20 77 61 73 20 61 20 62 74 72 65 65 2d age was a btree-
15199 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d 61 page, then it ma
1519a 79 20 68 61 76 65 20 63 68 69 6c 64 20 70 61 67 y have child pag
1519b 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a es and/or cells.
1519c 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 20 ** that point
1519d 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 to overflow page
1519e 73 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d s. The pointer m
1519f 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61 ap entries for a
151a0 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70 61 ll these. ** pa
151a1 67 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 63 ges need to be c
151a2 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a hanged.. **. *
151a3 2a 20 49 66 20 70 44 62 50 61 67 65 20 69 73 20 * If pDbPage is
151a4 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
151a5 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 , then the first
151a6 20 34 20 62 79 74 65 73 20 6d 61 79 20 73 74 6f 4 bytes may sto
151a7 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 re a. ** pointe
151a8 72 20 74 6f 20 61 20 73 75 62 73 65 71 75 65 6e r to a subsequen
151a9 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e t overflow page.
151aa 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
151ab 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 case, then. **
151ac 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 the pointer map
151ad 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 needs to be upda
151ae 74 65 64 20 66 6f 72 20 74 68 65 20 73 75 62 73 ted for the subs
151af 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 equent overflow
151b0 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 page.. */. if(
151b1 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 eType==PTRMAP_B
151b2 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 TREE || eType==P
151b3 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 TRMAP_ROOTPAGE )
151b4 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 68 {. rc = setCh
151b5 69 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 61 ildPtrmaps(pDbPa
151b6 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
151b7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
151b8 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
151b9 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
151ba 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20 Pgno nextOvfl
151bb 3d 20 67 65 74 34 62 79 74 65 28 70 44 62 50 61 = get4byte(pDbPa
151bc 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 ge->aData);.
151bd 69 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20 if( nextOvfl!=0
151be 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 ){. rc = pt
151bf 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65 78 rmapPut(pBt, nex
151c0 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 tOvfl, PTRMAP_OV
151c1 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 61 ERFLOW2, iFreePa
151c2 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
151c3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
151c4 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
151c5 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
151c6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74 . }.. /* Fix t
151c7 68 65 20 64 61 74 61 62 61 73 65 20 70 6f 69 6e he database poin
151c8 74 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74 72 ter on page iPtr
151c9 50 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74 65 Page that pointe
151ca 64 20 61 74 20 69 44 62 50 61 67 65 20 73 6f 0a d at iDbPage so.
151cb 20 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 ** that it poi
151cc 6e 74 73 20 61 74 20 69 46 72 65 65 50 61 67 65 nts at iFreePage
151cd 2e 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20 70 . Also fix the p
151ce 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 ointer map entry
151cf 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50 61 for. ** iPtrPa
151d0 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 ge.. */. if( e
151d1 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f Type!=PTRMAP_ROO
151d2 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 TPAGE ){. rc
151d3 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
151d4 74 50 61 67 65 28 70 42 74 2c 20 69 50 74 72 50 tPage(pBt, iPtrP
151d5 61 67 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20 age, &pPtrPage,
151d6 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 0);. if( rc!=
151d7 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
151d8 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
151d9 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c }. rc = sql
151da 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
151db 50 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 PtrPage->pDbPage
151dc 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
151dd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
151de 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
151df 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 trPage);. r
151e0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
151e1 20 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 rc = modifyP
151e2 61 67 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50 agePointer(pPtrP
151e3 61 67 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46 age, iDbPage, iF
151e4 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b reePage, eType);
151e5 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
151e6 28 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 (pPtrPage);.
151e7 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
151e8 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
151e9 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 69 ptrmapPut(pBt, i
151ea 46 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 2c FreePage, eType,
151eb 20 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 iPtrPage);.
151ec 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
151ed 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 c;.}../* Forward
151ee 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 declaration req
151ef 75 69 72 65 64 20 62 79 20 69 6e 63 72 56 61 63 uired by incrVac
151f0 75 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 uumStep(). */.st
151f1 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 atic int allocat
151f2 65 42 74 72 65 65 50 61 67 65 28 42 74 53 68 61 eBtreePage(BtSha
151f3 72 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a red *, MemPage *
151f4 2a 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c *, Pgno *, Pgno,
151f5 20 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 u8);../*.** Per
151f6 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 form a single st
151f7 65 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 ep of an increme
151f8 6e 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 ntal-vacuum. If
151f9 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 successful,.** r
151fa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e eturn SQLITE_OK.
151fb 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
151fc 77 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 work to do (and
151fd 74 68 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 therefore no.**
151fe 70 6f 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 point in calling
151ff 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 this function a
15200 67 61 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 gain), return SQ
15201 4c 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a LITE_DONE..**.**
15202 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63 6c 79 More specificly
15203 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
15204 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f attempts to re-o
15205 72 67 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 rganize the .**
15206 64 61 74 61 62 61 73 65 20 73 6f 20 74 68 61 74 database so that
15207 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f the last page o
15208 66 20 74 68 65 20 66 69 6c 65 20 63 75 72 72 65 f the file curre
15209 6e 74 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 ntly in use.** i
1520a 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 s no longer in u
1520b 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 se..**.** If the
1520c 20 6e 46 69 6e 20 70 61 72 61 6d 65 74 65 72 20 nFin parameter
1520d 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
1520e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
1520f 61 73 73 75 6d 65 73 0a 2a 2a 20 74 68 61 74 20 assumes.** that
15210 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 the caller will
15211 6b 65 65 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63 keep calling inc
15212 72 56 61 63 75 75 6d 53 74 65 70 28 29 20 75 6e rVacuumStep() un
15213 74 69 6c 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e til.** it return
15214 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 s SQLITE_DONE or
15215 20 61 6e 20 65 72 72 6f 72 2c 20 61 6e 64 20 74 an error, and t
15216 68 61 74 20 6e 46 69 6e 20 69 73 20 74 68 65 0a hat nFin is the.
15217 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 ** number of pag
15218 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 es the database
15219 66 69 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 file will contai
1521a 6e 20 61 66 74 65 72 20 74 68 69 73 20 0a 2a 2a n after this .**
1521b 20 70 72 6f 63 65 73 73 20 69 73 20 63 6f 6d 70 process is comp
1521c 6c 65 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lete..*/.static
1521d 69 6e 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74 int incrVacuumSt
1521e 65 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ep(BtShared *pBt
1521f 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e , Pgno nFin, Pgn
15220 6f 20 69 4c 61 73 74 50 67 29 7b 0a 20 20 50 67 o iLastPg){. Pg
15221 6e 6f 20 6e 46 72 65 65 4c 69 73 74 3b 20 20 20 no nFreeList;
15222 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
15223 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c r of pages still
15224 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 on the free-lis
15225 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
15226 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
15227 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
15228 3b 0a 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50 ;.. if( !PTRMAP
15229 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 _ISPAGE(pBt, iLa
1522a 73 74 50 67 29 20 26 26 20 69 4c 61 73 74 50 67 stPg) && iLastPg
1522b 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 !=PENDING_BYTE_P
1522c 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
1522d 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 38 20 65 int rc;. u8 e
1522e 54 79 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69 Type;. Pgno i
1522f 50 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46 PtrPage;.. nF
15230 72 65 65 4c 69 73 74 20 3d 20 67 65 74 34 62 79 reeList = get4by
15231 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d te(&pBt->pPage1-
15232 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 >aData[36]);.
15233 20 69 66 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d if( nFreeList==
15234 30 20 7c 7c 20 6e 46 69 6e 3d 3d 69 4c 61 73 74 0 || nFin==iLast
15235 50 67 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 Pg ){. retu
15236 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a rn SQLITE_DONE;.
15237 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 }.. rc =
15238 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 ptrmapGet(pBt, i
15239 4c 61 73 74 50 67 2c 20 26 65 54 79 70 65 2c 20 LastPg, &eType,
1523a 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 &iPtrPage);.
1523b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1523c 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
1523d 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
1523e 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 if( eType==PTRMA
1523f 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 P_ROOTPAGE ){.
15240 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
15241 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
15242 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 65 }.. if( e
15243 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 Type==PTRMAP_FRE
15244 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 69 EPAGE ){. i
15245 66 28 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 f( nFin==0 ){.
15246 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 /* Remove
15247 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 the page from th
15248 65 20 66 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 e files free-lis
15249 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72 t. This is not r
1524a 65 71 75 69 72 65 64 0a 20 20 20 20 20 20 20 20 equired.
1524b 2a 2a 20 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f ** if nFin is no
1524c 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 61 74 20 n-zero. In that
1524d 63 61 73 65 2c 20 74 68 65 20 66 72 65 65 2d 6c case, the free-l
1524e 69 73 74 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 ist will be.
1524f 20 20 20 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 ** truncated
15250 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72 20 74 to zero after t
15251 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
15252 75 72 6e 73 2c 20 73 6f 20 69 74 20 64 6f 65 73 urns, so it does
15253 6e 27 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 n't . **
15254 6d 61 74 74 65 72 20 69 66 20 69 74 20 73 74 69 matter if it sti
15255 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 ll contains some
15256 20 67 61 72 62 61 67 65 20 65 6e 74 72 69 65 73 garbage entries
15257 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
15258 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 Pgno iFreeP
15259 67 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 g;. MemPa
1525a 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 ge *pFreePg;.
1525b 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 rc = alloca
1525c 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
1525d 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 &pFreePg, &iFre
1525e 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20 31 29 ePg, iLastPg, 1)
1525f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
15260 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
15261 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
15262 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
15263 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46 assert( iF
15264 72 65 65 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 reePg==iLastPg )
15265 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 ;. releas
15266 65 50 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a ePage(pFreePg);.
15267 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c }. } el
15268 73 65 20 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 se {. Pgno
15269 69 46 72 65 65 50 67 3b 20 20 20 20 20 20 20 20 iFreePg;
1526a 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
1526b 20 66 72 65 65 20 70 61 67 65 20 74 6f 20 6d 6f free page to mo
1526c 76 65 20 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f ve pLastPg to */
1526d 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a . MemPage *
1526e 70 4c 61 73 74 50 67 3b 0a 0a 20 20 20 20 20 20 pLastPg;..
1526f 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
15270 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c eGetPage(pBt, iL
15271 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50 67 2c astPg, &pLastPg,
15272 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
15273 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
15274 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
15275 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 c;. }..
15276 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69 73 20 /* If nFin is
15277 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f 70 20 zero, this loop
15278 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f 6e 63 runs exactly onc
15279 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61 73 74 e and page pLast
1527a 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 Pg. ** is s
1527b 77 61 70 70 65 64 20 77 69 74 68 20 74 68 65 20 wapped with the
1527c 66 69 72 73 74 20 66 72 65 65 20 70 61 67 65 20 first free page
1527d 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65 20 66 pulled off the f
1527e 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20 20 20 ree list..
1527f 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e 20 74 **. ** On t
15280 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 he other hand, i
15281 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61 74 65 f nFin is greate
15282 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 r than zero, the
15283 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a 2a 20 n keep. **
15284 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 20 looping until a
15285 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61 74 65 free-page locate
15286 64 20 77 69 74 68 69 6e 20 74 68 65 20 66 69 72 d within the fir
15287 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a 20 20 st nFin pages.
15288 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 ** of the fi
15289 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 20 le is found..
1528a 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b */. do {
1528b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 . MemPage
1528c 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 *pFreePg;.
1528d 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 rc = allocate
1528e 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 BtreePage(pBt, &
1528f 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 pFreePg, &iFreeP
15290 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 g, 0, 0);.
15291 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
15292 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
15293 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 releasePage(pLa
15294 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 stPg);.
15295 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
15296 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65 }. re
15297 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 65 50 leasePage(pFreeP
15298 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 g);. }while
15299 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69 46 72 ( nFin!=0 && iFr
1529a 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20 20 20 eePg>nFin );.
1529b 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65 assert( iFree
1529c 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 Pg<iLastPg );.
1529d 20 20 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20 . rc =
1529e 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1529f 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62 50 61 e(pLastPg->pDbPa
152a0 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
152a1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
152a2 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c rc = rel
152a3 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 70 ocatePage(pBt, p
152a4 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c 20 69 LastPg, eType, i
152a5 50 74 72 50 61 67 65 2c 20 69 46 72 65 65 50 67 PtrPage, iFreePg
152a6 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a 20 20 20 20 , nFin!=0);.
152a7 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 }. releas
152a8 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a ePage(pLastPg);.
152a9 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
152aa 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
152ab 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
152ac 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
152ad 0a 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30 20 29 . if( nFin==0 )
152ae 7b 0a 20 20 20 20 69 4c 61 73 74 50 67 2d 2d 3b {. iLastPg--;
152af 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c 61 73 . while( iLas
152b0 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 tPg==PENDING_BYT
152b1 45 5f 50 41 47 45 28 70 42 74 29 7c 7c 50 54 52 E_PAGE(pBt)||PTR
152b2 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
152b3 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20 iLastPg) ){.
152b4 20 20 69 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 iLastPg--;.
152b5 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 }. sqlite3Pa
152b6 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 gerTruncateImage
152b7 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c (pBt->pPager, iL
152b8 61 73 74 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 astPg);. }. re
152b9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
152ba 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65 }../*.** A write
152bb 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 -transaction mus
152bc 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 6f t be opened befo
152bd 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 re calling this
152be 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20 function..** It
152bf 70 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c performs a singl
152c0 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74 e unit of work t
152c1 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65 6d owards an increm
152c2 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a ental vacuum..**
152c3 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72 65 .** If the incre
152c4 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69 73 mental vacuum is
152c5 20 66 69 6e 69 73 68 65 64 20 61 66 74 65 72 20 finished after
152c6 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 this function ha
152c7 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45 s run,.** SQLITE
152c8 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 _DONE is returne
152c9 64 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 d. If it is not
152ca 66 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e 6f finished, but no
152cb 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 2c 0a error occured,.
152cc 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
152cd 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 returned. Otherw
152ce 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72 ise an SQLite er
152cf 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 51 ror code. .*/.SQ
152d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
152d1 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 sqlite3BtreeInc
152d2 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 rVacuum(Btree *p
152d3 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
152d4 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
152d5 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 ->pBt;.. sqlite
152d6 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
152d7 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 pBt->db = p->d
152d8 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 b;. assert( pBt
152d9 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
152da 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26 20 =TRANS_WRITE &&
152db 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
152dc 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 28 S_WRITE );. if(
152dd 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 !pBt->autoVacuu
152de 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 m ){. rc = SQ
152df 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c LITE_DONE;. }el
152e0 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 se{. invalida
152e1 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 teAllOverflowCac
152e2 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63 20 he(pBt);. rc
152e3 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 = incrVacuumStep
152e4 28 70 42 74 2c 20 30 2c 20 73 71 6c 69 74 65 33 (pBt, 0, sqlite3
152e5 50 61 67 65 72 49 6d 61 67 65 53 69 7a 65 28 70 PagerImageSize(p
152e6 42 74 2d 3e 70 50 61 67 65 72 29 29 3b 0a 20 20 Bt->pPager));.
152e7 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 }. sqlite3Btree
152e8 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
152e9 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
152ea 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
152eb 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 called prior to
152ec 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
152ed 69 74 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 it when a transa
152ee 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d ction.** is comm
152ef 69 74 65 64 20 66 6f 72 20 61 6e 20 61 75 74 6f ited for an auto
152f0 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 -vacuum database
152f1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 ..**.** If SQLIT
152f2 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
152f3 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 , then *pnTrunc
152f4 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 is set to the nu
152f5 6d 62 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a mber of pages.**
152f6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
152f7 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 le should be tru
152f8 6e 63 61 74 65 64 20 74 6f 20 64 75 72 69 6e 67 ncated to during
152f9 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 the commit proc
152fa 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 ess. .** i.e. th
152fb 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62 e database has b
152fc 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 een reorganized
152fd 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 so that only the
152fe 20 66 69 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a first *pnTrunc.
152ff 2a 2a 20 70 61 67 65 73 20 61 72 65 20 69 6e 20 ** pages are in
15300 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 use..*/.static i
15301 6e 74 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d nt autoVacuumCom
15302 6d 69 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 mit(BtShared *pB
15303 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
15304 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 QLITE_OK;. Page
15305 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d r *pPager = pBt-
15306 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 41 5f 4f >pPager;. VVA_O
15307 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 20 3d 20 NLY( int nRef =
15308 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 sqlite3PagerRefc
15309 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a ount(pPager) );.
1530a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1530b 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
1530c 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 t->mutex) );. i
1530d 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 nvalidateAllOver
1530e 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a flowCache(pBt);.
1530f 20 20 61 73 73 65 72 74 28 70 42 74 2d 3e 61 75 assert(pBt->au
15310 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 69 66 28 toVacuum);. if(
15311 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 !pBt->incrVacuu
15312 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 m ){. Pgno nF
15313 69 6e 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 72 in;. Pgno nFr
15314 65 65 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 50 74 ee;. Pgno nPt
15315 72 6d 61 70 3b 0a 20 20 20 20 50 67 6e 6f 20 69 rmap;. Pgno i
15316 46 72 65 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 Free;. const
15317 69 6e 74 20 70 67 73 7a 20 3d 20 70 42 74 2d 3e int pgsz = pBt->
15318 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 50 67 pageSize;. Pg
15319 6e 6f 20 6e 4f 72 69 67 20 3d 20 70 61 67 65 72 no nOrig = pager
1531a 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a Pagecount(pBt);.
1531b 0a 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f . if( PTRMAP_
1531c 49 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 ISPAGE(pBt, nOri
1531d 67 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 g) ){. retu
1531e 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
1531f 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 T_BKPT;. }.
15320 20 20 69 66 28 20 6e 4f 72 69 67 3d 3d 50 45 4e if( nOrig==PEN
15321 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
15322 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 4f 72 Bt) ){. nOr
15323 69 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ig--;. }.
15324 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 nFree = get4byte
15325 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 (&pBt->pPage1->a
15326 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 6e Data[36]);. n
15327 50 74 72 6d 61 70 20 3d 20 28 6e 46 72 65 65 2d Ptrmap = (nFree-
15328 6e 4f 72 69 67 2b 50 54 52 4d 41 50 5f 50 41 47 nOrig+PTRMAP_PAG
15329 45 4e 4f 28 70 42 74 2c 20 6e 4f 72 69 67 29 2b ENO(pBt, nOrig)+
1532a 70 67 73 7a 2f 35 29 2f 28 70 67 73 7a 2f 35 29 pgsz/5)/(pgsz/5)
1532b 3b 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 ;. nFin = nOr
1532c 69 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 ig - nFree - nPt
1532d 72 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 6e 4f rmap;. if( nO
1532e 72 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 rig>PENDING_BYTE
1532f 5f 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 _PAGE(pBt) && nF
15330 69 6e 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 in<=PENDING_BYTE
15331 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 _PAGE(pBt) ){.
15332 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 nFin--;.
15333 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 }. while( PTR
15334 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
15335 6e 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 nFin) || nFin==P
15336 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
15337 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e (pBt) ){. n
15338 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 0a 20 20 Fin--;. }..
15339 20 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69 for(iFree=nOri
1533a 67 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26 g; iFree>nFin &&
1533b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc==SQLITE_OK;
1533c 69 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20 iFree--){.
1533d 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 rc = incrVacuumS
1533e 74 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69 tep(pBt, nFin, i
1533f 46 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Free);. }.
15340 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 if( (rc==SQLITE
15341 5f 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c _DONE || rc==SQL
15342 49 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65 ITE_OK) && nFree
15343 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d >0 ){. rc =
15344 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
15345 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
15346 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 gerWrite(pBt->pP
15347 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a age1->pDbPage);.
15348 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
15349 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
1534a 74 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20 ta[32], 0);.
1534b 20 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d put4byte(&pBt-
1534c 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 >pPage1->aData[3
1534d 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6], 0);. sq
1534e 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 lite3PagerTrunca
1534f 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 teImage(pBt->pPa
15350 67 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 ger, nFin);.
15351 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 }. if( rc!=SQ
15352 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
15353 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c sqlite3PagerRol
15354 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 lback(pPager);.
15355 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 }. }.. asse
15356 72 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65 rt( nRef==sqlite
15357 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 3PagerRefcount(p
15358 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75 Pager) );. retu
15359 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 rn rc;.}..#endif
1535a 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 /* ifndef SQLIT
1535b 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1535c 4d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 M */../*.** This
1535d 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 routine does th
1535e 65 20 66 69 72 73 74 20 70 68 61 73 65 20 6f 66 e first phase of
1535f 20 61 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d a two-phase com
15360 6d 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 mit. This routi
15361 6e 65 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72 ne.** causes a r
15362 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
15363 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 28 69 to be created (i
15364 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c f it does not al
15365 72 65 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20 ready exist).**
15366 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 and populated wi
15367 74 68 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d th enough inform
15368 61 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66 ation so that if
15369 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63 a power loss oc
1536a 63 75 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 curs.** the data
1536b 62 61 73 65 20 63 61 6e 20 62 65 20 72 65 73 74 base can be rest
1536c 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 ored to its orig
1536d 69 6e 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c inal state by pl
1536e 61 79 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68 aying back.** th
1536f 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e e journal. Then
15370 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
15371 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65 the journal are
15372 20 66 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a flushed out to.
15373 2a 2a 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66 ** the disk. Af
15374 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ter the journal
15375 69 73 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69 is safely on oxi
15376 64 65 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20 de, the changes
15377 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 to the.** databa
15378 73 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 se are written i
15379 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
1537a 20 66 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65 file and flushe
1537b 64 20 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41 d to oxide..** A
1537c 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 t the end of thi
1537d 73 20 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c s call, the roll
1537e 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69 back journal sti
1537f 6c 6c 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65 ll exists on the
15380 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20 .** disk and we
15381 61 72 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e are still holdin
15382 67 20 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20 g all locks, so
15383 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
15384 68 61 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 has not.** commi
15385 74 74 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74 tted. See sqlit
15386 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 29 20 e3BtreeCommit()
15387 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 for the second p
15388 68 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 hase of the.** c
15389 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a ommit process..*
1538a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 *.** This call i
1538b 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 s a no-op if no
1538c 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1538d 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 n is currently a
1538e 63 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a ctive on pBt..**
1538f 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 .** Otherwise, s
15390 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 ync the database
15391 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 file for the bt
15392 72 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 ree pBt. zMaster
15393 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 points to.** th
15394 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 e name of a mast
15395 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
15396 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 that should be w
15397 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a ritten into the.
15398 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f ** individual jo
15399 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 urnal file, or i
1539a 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 s NULL, indicati
1539b 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 ng no master jou
1539c 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 rnal file .** (s
1539d 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 ingle database t
1539e 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a ransaction)..**.
1539f 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 ** When this is
153a0 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 called, the mast
153a1 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c er journal shoul
153a2 64 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 d already have b
153a3 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 een.** created,
153a4 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 populated with t
153a5 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e his journal poin
153a6 74 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 ter and synced t
153a7 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e o disk..**.** On
153a8 63 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 ce this is routi
153a9 6e 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c ne has returned,
153aa 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 the only thing
153ab 72 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d required to comm
153ac 69 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d it.** the write-
153ad 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 transaction for
153ae 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 this database fi
153af 6c 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 le is to delete
153b0 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a the journal..*/.
153b1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
153b2 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
153b3 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 ommitPhaseOne(Bt
153b4 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 ree *p, const ch
153b5 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 ar *zMaster){.
153b6 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
153b7 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 OK;. if( p->inT
153b8 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
153b9 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 E ){. BtShare
153ba 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
153bb 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
153bc 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 70 eEnter(p);. p
153bd 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a Bt->db = p->db;.
153be 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
153bf 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
153c0 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f if( pBt->auto
153c1 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 Vacuum ){.
153c2 72 63 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 rc = autoVacuumC
153c3 6f 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 ommit(pBt);.
153c4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
153c5 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 _OK ){. s
153c6 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
153c7 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 (p);. ret
153c8 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
153c9 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
153ca 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
153cb 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 erCommitPhaseOne
153cc 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d (pBt->pPager, zM
153cd 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 aster, 0);. s
153ce 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
153cf 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 (p);. }. retur
153d0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
153d1 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 ommit the transa
153d2 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 ction currently
153d3 69 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a in progress..**.
153d4 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
153d5 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 implements the s
153d6 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 econd phase of a
153d7 20 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 2-phase commit.
153d8 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 The.** sqlite3
153d9 42 74 72 65 65 53 79 6e 63 28 29 20 72 6f 75 74 BtreeSync() rout
153da 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 ine does the fir
153db 73 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f st phase and sho
153dc 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a uld be invoked.*
153dd 2a 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 * prior to calli
153de 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e ng this routine.
153df 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 The sqlite3Btr
153e0 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65 eeSync() routine
153e1 20 64 69 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 did.** all the
153e2 77 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 work of writing
153e3 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 information out
153e4 74 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 to disk and flus
153e5 68 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 hing the.** cont
153e6 65 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 ents so that the
153e7 79 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e y are written on
153e8 74 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 to the disk plat
153e9 74 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a ter. All this.*
153ea 2a 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f * routine has to
153eb 20 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 do is delete or
153ec 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 72 6f truncate the ro
153ed 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a llback journal.*
153ee 2a 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20 * (which causes
153ef 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
153f0 74 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64 to commit) and d
153f1 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a rop locks..**.**
153f2 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 This will relea
153f3 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 se the write loc
153f4 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
153f5 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 e file. If ther
153f6 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 e.** are no acti
153f7 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 ve cursors, it a
153f8 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 lso releases the
153f9 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 read lock..*/.S
153fa 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
153fb 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
153fc 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 mmitPhaseTwo(Btr
153fd 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 ee *p){. BtShar
153fe 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
153ff 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ;.. sqlite3Btre
15400 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 eEnter(p);. pBt
15401 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 ->db = p->db;.
15402 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
15403 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
15404 68 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69 handle has a wri
15405 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f te-transaction o
15406 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 pen, commit the
15407 73 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20 shared-btrees .
15408 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** transaction
15409 61 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72 and set the shar
1540a 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e ed state to TRAN
1540b 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 S_READ.. */. i
1540c 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 f( p->inTrans==T
1540d 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 RANS_WRITE ){.
1540e 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 int rc;. as
1540f 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 sert( pBt->inTra
15410 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
15411 57 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73 WRITE );. ass
15412 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 ert( pBt->nTrans
15413 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 action>0 );.
15414 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15415 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 rCommitPhaseTwo(
15416 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
15417 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
15418 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
15419 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1541a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1541b 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 rc;. }. pB
1541c 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
1541d 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 = TRANS_READ;.
1541e 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d pBt->inStmt =
1541f 20 30 3b 0a 20 20 7d 0a 20 20 75 6e 6c 6f 63 6b 0;. }. unlock
15420 41 6c 6c 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20 AllTables(p);..
15421 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c /* If the handl
15422 65 20 68 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f e has any kind o
15423 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 f transaction op
15424 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 en, decrement th
15425 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 e transaction.
15426 2a 2a 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 ** count of the
15427 73 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 shared btree. If
15428 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
15429 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 30 count reaches 0
1542a 2c 20 73 65 74 0a 20 20 2a 2a 20 74 68 65 20 73 , set. ** the s
1542b 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 hared state to T
1542c 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 RANS_NONE. The u
1542d 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 nlockBtreeIfUnus
1542e 65 64 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a ed() call below.
1542f 20 20 2a 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b ** will unlock
15430 20 74 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f the pager.. */
15431 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e . if( p->inTran
15432 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b s!=TRANS_NONE ){
15433 0a 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 . pBt->nTrans
15434 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66 action--;. if
15435 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 ( 0==pBt->nTrans
15436 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 action ){.
15437 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
15438 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b on = TRANS_NONE;
15439 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1543a 20 53 65 74 20 74 68 65 20 68 61 6e 64 6c 65 73 Set the handles
1543b 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
1543c 74 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52 tion state to TR
1543d 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c ANS_NONE and unl
1543e 6f 63 6b 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 ock. ** the pag
1543f 65 72 20 69 66 20 74 68 69 73 20 63 61 6c 6c 20 er if this call
15440 63 6c 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 20 closed the only
15441 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 read or write tr
15442 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a ansaction.. */.
15443 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 p->inTrans = T
15444 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c RANS_NONE;. unl
15445 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 ockBtreeIfUnused
15446 28 70 42 74 29 3b 0a 0a 20 20 62 74 72 65 65 49 (pBt);.. btreeI
15447 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 ntegrity(p);. s
15448 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
15449 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
1544a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1544b 2a 20 44 6f 20 62 6f 74 68 20 70 68 61 73 65 73 * Do both phases
1544c 20 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f of a commit..*/
1544d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1544e 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
1544f 43 6f 6d 6d 69 74 28 42 74 72 65 65 20 2a 70 29 Commit(Btree *p)
15450 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 {. int rc;. sq
15451 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
15452 70 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 p);. rc = sqlit
15453 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
15454 73 65 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20 69 seOne(p, 0);. i
15455 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
15456 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
15457 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
15458 68 61 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d 0a haseTwo(p);. }.
15459 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1545a 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
1545b 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 rc;.}..#ifndef
1545c 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 NDEBUG./*.** Ret
1545d 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1545e 66 20 77 72 69 74 65 2d 63 75 72 73 6f 72 73 20 f write-cursors
1545f 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 68 61 6e open on this han
15460 64 6c 65 2e 20 54 68 69 73 20 69 73 20 66 6f 72 dle. This is for
15461 20 75 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65 72 use.** in asser
15462 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e 73 2c t() expressions,
15463 20 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20 63 so it is only c
15464 6f 6d 70 69 6c 65 64 20 69 66 20 4e 44 45 42 55 ompiled if NDEBU
15465 47 20 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 G is not.** defi
15466 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 ned..**.** For t
15467 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 he purposes of t
15468 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 20 77 his routine, a w
15469 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 61 rite-cursor is a
1546a 6e 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a ny cursor that.*
1546b 2a 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 * is capable of
1546c 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 writing to the d
1546d 61 74 61 62 73 65 2e 20 20 54 68 61 74 20 6d 65 atabse. That me
1546e 61 6e 73 20 74 68 65 20 63 75 72 73 6f 72 20 77 ans the cursor w
1546f 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 as.** originally
15470 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 opened for writ
15471 69 6e 67 20 61 6e 64 20 74 68 65 20 63 75 72 73 ing and the curs
15472 6f 72 20 68 61 73 20 6e 6f 74 20 62 65 20 64 69 or has not be di
15473 73 61 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61 76 sabled.** by hav
15474 69 6e 67 20 69 74 73 20 73 74 61 74 65 20 63 68 ing its state ch
15475 61 6e 67 65 64 20 74 6f 20 43 55 52 53 4f 52 5f anged to CURSOR_
15476 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 FAULT..*/.static
15477 20 69 6e 74 20 63 6f 75 6e 74 57 72 69 74 65 43 int countWriteC
15478 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64 20 ursors(BtShared
15479 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f *pBt){. BtCurso
1547a 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72 r *pCur;. int r
1547b 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 75 72 = 0;. for(pCur
1547c 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 =pBt->pCursor; p
1547d 43 75 72 3b 20 70 43 75 72 3d 70 43 75 72 2d 3e Cur; pCur=pCur->
1547e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 pNext){. if(
1547f 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20 pCur->wrFlag &&
15480 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 pCur->eState!=CU
15481 52 53 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b RSOR_FAULT ) r++
15482 3b 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 ; . }. return
15483 72 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a r;.}.#endif../*.
15484 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
15485 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20 74 sets the state t
15486 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 61 o CURSOR_FAULT a
15487 6e 64 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 nd the error.**
15488 63 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 65 20 code to errCode
15489 66 6f 72 20 65 76 65 72 79 20 63 75 72 73 6f 72 for every cursor
1548a 20 6f 6e 20 42 74 53 68 61 72 65 64 20 74 68 61 on BtShared tha
1548b 74 20 70 42 74 72 65 65 0a 2a 2a 20 72 65 66 65 t pBtree.** refe
1548c 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 rences..**.** Ev
1548d 65 72 79 20 63 75 72 73 6f 72 20 69 73 20 74 72 ery cursor is tr
1548e 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e 67 ipped, including
1548f 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 62 65 cursors that be
15490 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 72 long.** to other
15491 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
15492 74 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70 65 tions that happe
15493 6e 20 74 6f 20 62 65 20 73 68 61 72 69 6e 67 0a n to be sharing.
15494 2a 2a 20 74 68 65 20 63 61 63 68 65 20 77 69 74 ** the cache wit
15495 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 h pBtree..**.**
15496 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 74 This routine get
15497 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 s called when a
15498 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 2e rollback occurs.
15499 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 .** All cursors
1549a 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63 using the same c
1549b 61 63 68 65 20 6d 75 73 74 20 62 65 20 74 72 69 ache must be tri
1549c 70 70 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76 65 pped.** to preve
1549d 6e 74 20 74 68 65 6d 20 66 72 6f 6d 20 74 72 79 nt them from try
1549e 69 6e 67 20 74 6f 20 75 73 65 20 74 68 65 20 62 ing to use the b
1549f 74 72 65 65 20 61 66 74 65 72 0a 2a 2a 20 74 68 tree after.** th
154a0 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 e rollback. The
154a1 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 rollback may ha
154a2 76 65 20 64 65 6c 65 74 65 64 20 74 61 62 6c 65 ve deleted table
154a3 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72 6f s.** or moved ro
154a4 6f 74 20 70 61 67 65 73 2c 20 73 6f 20 69 74 20 ot pages, so it
154a5 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e is not sufficien
154a6 74 20 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68 65 t to.** save the
154a7 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 75 state of the cu
154a8 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f rsor. The curso
154a9 72 20 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e 76 r must be.** inv
154aa 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c alidated..*/.SQL
154ab 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
154ac 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 sqlite3BtreeTri
154ad 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 65 pAllCursors(Btre
154ae 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 65 e *pBtree, int e
154af 72 72 43 6f 64 65 29 7b 0a 20 20 42 74 43 75 72 rrCode){. BtCur
154b0 73 6f 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 sor *p;. sqlite
154b1 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 3BtreeEnter(pBtr
154b2 65 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 ee);. for(p=pBt
154b3 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f ree->pBt->pCurso
154b4 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 r; p; p=p->pNext
154b5 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
154b6 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
154b7 65 61 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 20 earCursor(p);.
154b8 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 p->eState = CU
154b9 52 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20 20 RSOR_FAULT;.
154ba 70 2d 3e 73 6b 69 70 20 3d 20 65 72 72 43 6f 64 p->skip = errCod
154bb 65 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 e;. for(i=0;
154bc 69 3c 3d 70 2d 3e 69 50 61 67 65 3b 20 69 2b 2b i<=p->iPage; i++
154bd 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
154be 50 61 67 65 28 70 2d 3e 61 70 50 61 67 65 5b 69 Page(p->apPage[i
154bf 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70 50 ]);. p->apP
154c0 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 age[i] = 0;.
154c1 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 }. }. sqlite3B
154c2 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 treeLeave(pBtree
154c3 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c );.}../*.** Roll
154c4 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 back the transac
154c5 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 tion in progress
154c6 2e 20 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 77 . All cursors w
154c7 69 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 ill be.** invali
154c8 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 ded by this oper
154c9 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65 ation. Any atte
154ca 6d 70 74 20 74 6f 20 75 73 65 20 61 20 63 75 72 mpt to use a cur
154cb 73 6f 72 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 sor.** that was
154cc 6f 70 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 open at the begi
154cd 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 6f 70 nning of this op
154ce 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 eration will res
154cf 75 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20 65 72 72 ult.** in an err
154d0 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 or..**.** This w
154d1 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 ill release the
154d2 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 write lock on th
154d3 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
154d4 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 If there.** ar
154d5 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73 e no active curs
154d6 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c ors, it also rel
154d7 65 61 73 65 73 20 74 68 65 20 72 65 61 64 20 6c eases the read l
154d8 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ock..*/.SQLITE_P
154d9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
154da 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 e3BtreeRollback(
154db 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 Btree *p){. int
154dc 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 rc;. BtShared
154dd 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
154de 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 MemPage *pPage1
154df 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ;.. sqlite3Btre
154e0 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 eEnter(p);. pBt
154e1 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 ->db = p->db;.
154e2 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 rc = saveAllCurs
154e3 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a ors(pBt, 0, 0);.
154e4 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
154e5 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
154e6 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
154e7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 54 E_OK ){. /* T
154e8 68 69 73 20 69 73 20 61 20 68 6f 72 72 69 62 6c his is a horribl
154e9 65 20 73 69 74 75 61 74 69 6f 6e 2e 20 41 6e 20 e situation. An
154ea 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 IO or malloc() e
154eb 72 72 6f 72 20 6f 63 63 75 72 65 64 20 77 68 69 rror occured whi
154ec 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72 79 69 6e lst. ** tryin
154ed 67 20 74 6f 20 73 61 76 65 20 63 75 72 73 6f 72 g to save cursor
154ee 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49 66 20 74 positions. If t
154ef 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 6d 61 his is an automa
154f0 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 28 61 73 tic rollback (as
154f1 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 65 73 75 . ** the resu
154f2 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74 72 61 69 lt of a constrai
154f3 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 nt, malloc() fai
154f4 6c 75 72 65 20 6f 72 20 49 4f 20 65 72 72 6f 72 lure or IO error
154f5 29 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 ) then . ** t
154f6 68 65 20 63 61 63 68 65 20 6d 61 79 20 62 65 20 he cache may be
154f7 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e 63 6f 6e internally incon
154f8 73 69 73 74 65 6e 74 20 28 6e 6f 74 20 63 6f 6e sistent (not con
154f9 74 61 69 6e 20 76 61 6c 69 64 20 74 72 65 65 73 tain valid trees
154fa 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77 65 20 63 ) so. ** we c
154fb 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 72 65 74 annot simply ret
154fc 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 74 6f urn the error to
154fd 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 73 the caller. Ins
154fe 74 65 61 64 2c 20 61 62 6f 72 74 20 0a 20 20 20 tead, abort .
154ff 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69 65 73 20 ** all queries
15500 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 69 6e that may be usin
15501 67 20 61 6e 79 20 6f 66 20 74 68 65 20 63 75 72 g any of the cur
15502 73 6f 72 73 20 74 68 61 74 20 66 61 69 6c 65 64 sors that failed
15503 20 74 6f 20 73 61 76 65 2e 0a 20 20 20 20 2a 2f to save.. */
15504 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
15505 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 eTripAllCursors(
15506 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23 65 6e 64 p, rc);. }.#end
15507 69 66 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 if. btreeIntegr
15508 69 74 79 28 70 29 3b 0a 20 20 75 6e 6c 6f 63 6b ity(p);. unlock
15509 41 6c 6c 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20 AllTables(p);..
1550a 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d if( p->inTrans=
1550b 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a =TRANS_WRITE ){.
1550c 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20 int rc2;..
1550d 20 20 61 73 73 65 72 74 28 20 54 52 41 4e 53 5f assert( TRANS_
1550e 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 WRITE==pBt->inTr
1550f 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 ansaction );.
15510 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50 61 rc2 = sqlite3Pa
15511 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d gerRollback(pBt-
15512 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 >pPager);. if
15513 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b ( rc2!=SQLITE_OK
15514 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 ){. rc = r
15515 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f c2;. }.. /
15516 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d * The rollback m
15517 61 79 20 68 61 76 65 20 64 65 73 74 72 6f 79 65 ay have destroye
15518 64 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61 44 d the pPage1->aD
15519 61 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 ata value. So.
1551a 20 20 20 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 74 ** call sqlit
1551b 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 29 e3BtreeGetPage()
1551c 20 6f 6e 20 70 61 67 65 20 31 20 61 67 61 69 6e on page 1 again
1551d 20 74 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 to make. **
1551e 73 75 72 65 20 70 50 61 67 65 31 2d 3e 61 44 61 sure pPage1->aDa
1551f 74 61 20 69 73 20 73 65 74 20 63 6f 72 72 65 63 ta is set correc
15520 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 tly. */. if(
15521 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
15522 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 age(pBt, 1, &pPa
15523 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f ge1, 0)==SQLITE_
15524 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 OK ){. rele
15525 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b asePage(pPage1);
15526 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
15527 74 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 t( countWriteCur
15528 73 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b 0a sors(pBt)==0 );.
15529 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 pBt->inTrans
1552a 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 action = TRANS_R
1552b 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 EAD;. }.. if(
1552c 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e p->inTrans!=TRAN
1552d 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 S_NONE ){. as
1552e 73 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e sert( pBt->nTran
1552f 73 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 saction>0 );.
15530 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 pBt->nTransacti
15531 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66 28 20 30 3d on--;. if( 0=
15532 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 =pBt->nTransacti
15533 6f 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d on ){. pBt-
15534 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d >inTransaction =
15535 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 TRANS_NONE;.
15536 20 7d 0a 20 20 7d 0a 0a 20 20 70 2d 3e 69 6e 54 }. }.. p->inT
15537 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e rans = TRANS_NON
15538 45 3b 0a 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 E;. pBt->inStmt
15539 20 3d 20 30 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 = 0;. unlockBt
1553a 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
1553b 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 ;.. btreeIntegr
1553c 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 ity(p);. sqlite
1553d 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1553e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1553f 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 /*.** Start a st
15540 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 atement subtrans
15541 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 73 75 62 action. The sub
15542 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 0a transaction can.
15543 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 ** can be rolled
15544 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e back independen
15545 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 tly of the main
15546 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 transaction..**
15547 59 6f 75 20 6d 75 73 74 20 73 74 61 72 74 20 61 You must start a
15548 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 transaction bef
15549 6f 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 ore starting a s
1554a 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a ubtransaction..*
1554b 2a 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 * The subtransac
1554c 74 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 tion is ended au
1554d 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 66 20 74 tomatically if t
1554e 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 he main transact
1554f 69 6f 6e 0a 2a 2a 20 63 6f 6d 6d 69 74 73 20 6f ion.** commits o
15550 72 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a r rolls back..**
15551 0a 2a 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73 75 62 .** Only one sub
15552 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 transaction may
15553 62 65 20 61 63 74 69 76 65 20 61 74 20 61 20 74 be active at a t
15554 69 6d 65 2e 20 20 49 74 20 69 73 20 61 6e 20 65 ime. It is an e
15555 72 72 6f 72 20 74 6f 20 74 72 79 0a 2a 2a 20 74 rror to try.** t
15556 6f 20 73 74 61 72 74 20 61 20 6e 65 77 20 73 75 o start a new su
15557 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 btransaction if
15558 61 6e 6f 74 68 65 72 20 73 75 62 74 72 61 6e 73 another subtrans
15559 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 action is alread
1555a 79 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 y active..**.**
1555b 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 Statement subtra
1555c 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73 nsactions are us
1555d 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 ed around indivi
1555e 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 dual SQL stateme
1555f 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 nts.** that are
15560 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e contained within
15561 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 a BEGIN...COMMI
15562 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 T block. If a c
15563 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 onstraint.** err
15564 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e or occurs within
15565 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 the statement,
15566 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 68 the effect of th
15567 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 at one statement
15568 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 .** can be rolle
15569 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 d back without h
1556a 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 aving to rollbac
1556b 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 k the entire tra
1556c 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c nsaction..*/.SQL
1556d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1556e 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
1556f 6e 53 74 6d 74 28 42 74 72 65 65 20 2a 70 29 7b nStmt(Btree *p){
15570 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 . int rc;. BtS
15571 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
15572 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 pBt;. sqlite3Bt
15573 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 reeEnter(p);. p
15574 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a Bt->db = p->db;.
15575 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 assert( p->inT
15576 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
15577 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 E );. assert( !
15578 70 42 74 2d 3e 69 6e 53 74 6d 74 20 29 3b 0a 20 pBt->inStmt );.
15579 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 assert( pBt->re
1557a 61 64 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 69 adOnly==0 );. i
1557b 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e 54 72 f( NEVER(p->inTr
1557c 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans!=TRANS_WRITE
1557d 20 7c 7c 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 || pBt->inStmt
1557e 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 || pBt->readOnly
1557f 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 ) ){. rc = SQ
15580 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a 20 LITE_INTERNAL;.
15581 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
15582 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 rt( pBt->inTrans
15583 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 action==TRANS_WR
15584 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a 20 41 74 ITE );. /* At
15585 20 74 68 65 20 70 61 67 65 72 20 6c 65 76 65 6c the pager level
15586 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 , a statement tr
15587 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 20 73 ansaction is a s
15588 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 20 20 avepoint with.
15589 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 67 72 ** an index gr
1558a 65 61 74 65 72 20 74 68 61 6e 20 61 6c 6c 20 73 eater than all s
1558b 61 76 65 70 6f 69 6e 74 73 20 63 72 65 61 74 65 avepoints create
1558c 64 20 65 78 70 6c 69 63 69 74 6c 79 20 75 73 69 d explicitly usi
1558d 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c 20 73 74 ng. ** SQL st
1558e 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 atements. It is
1558f 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 65 6e 2c illegal to open,
15590 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c release or roll
15591 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 2a 2a 20 back any. **
15592 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 73 20 such savepoints
15593 77 68 69 6c 65 20 74 68 65 20 73 74 61 74 65 6d while the statem
15594 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
15595 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63 74 savepoint is act
15596 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ive.. */.
15597 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15598 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 rOpenSavepoint(p
15599 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 Bt->pPager, p->d
1559a 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 31 29 b->nSavepoint+1)
1559b 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d ;. pBt->inStm
1559c 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c t = 1;. }. sql
1559d 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1559e 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1559f 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 }../*.** Commit
155a0 74 68 65 20 73 74 61 74 6d 65 6e 74 20 73 75 62 the statment sub
155a1 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 transaction curr
155a2 65 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73 ently in progres
155a3 73 2e 20 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 62 s. If no.** sub
155a4 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
155a5 63 74 69 76 65 2c 20 74 68 69 73 20 69 73 20 61 ctive, this is a
155a6 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 no-op..*/.SQLIT
155a7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
155a8 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
155a9 53 74 6d 74 28 42 74 72 65 65 20 2a 70 29 7b 0a Stmt(Btree *p){.
155aa 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 int rc;. BtSh
155ab 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
155ac 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 Bt;. sqlite3Btr
155ad 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 eeEnter(p);. pB
155ae 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 t->db = p->db;.
155af 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 assert( pBt->re
155b0 61 64 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 69 adOnly==0 );. i
155b1 66 28 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 29 f( pBt->inStmt )
155b2 7b 0a 20 20 20 20 69 6e 74 20 69 53 74 6d 74 70 {. int iStmtp
155b3 6f 69 6e 74 20 3d 20 70 2d 3e 64 62 2d 3e 6e 53 oint = p->db->nS
155b4 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 72 63 avepoint;. rc
155b5 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 = sqlite3PagerS
155b6 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 avepoint(pBt->pP
155b7 61 67 65 72 2c 20 53 41 56 45 50 4f 49 4e 54 5f ager, SAVEPOINT_
155b8 52 45 4c 45 41 53 45 2c 20 69 53 74 6d 74 70 6f RELEASE, iStmtpo
155b9 69 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 int);. }else{.
155ba 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
155bb 4b 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 69 6e K;. }. pBt->in
155bc 53 74 6d 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 Stmt = 0;. sqli
155bd 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
155be 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
155bf 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b ../*.** Rollback
155c0 20 74 68 65 20 61 63 74 69 76 65 20 73 74 61 74 the active stat
155c1 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 ement subtransac
155c2 74 69 6f 6e 2e 20 20 49 66 20 6e 6f 20 73 75 62 tion. If no sub
155c3 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 transaction.** i
155c4 73 20 61 63 74 69 76 65 20 74 68 69 73 20 72 6f s active this ro
155c5 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
155c6 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 ..**.** All curs
155c7 6f 72 73 20 77 69 6c 6c 20 62 65 20 69 6e 76 61 ors will be inva
155c8 6c 69 64 61 74 65 64 20 62 79 20 74 68 69 73 20 lidated by this
155c9 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 operation. Any
155ca 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 75 73 attempt.** to us
155cb 65 20 61 20 63 75 72 73 6f 72 20 74 68 61 74 20 e a cursor that
155cc 77 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65 20 was open at the
155cd 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 beginning of thi
155ce 73 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 77 s operation.** w
155cf 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 6e ill result in an
155d0 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 error..*/.SQLIT
155d1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
155d2 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
155d3 63 6b 53 74 6d 74 28 42 74 72 65 65 20 2a 70 29 ckStmt(Btree *p)
155d4 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
155d5 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72 ITE_OK;. BtShar
155d6 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
155d7 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
155d8 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d Enter(p);. pBt-
155d9 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 >db = p->db;. a
155da 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64 ssert( pBt->read
155db 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28 Only==0 );. if(
155dc 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 29 7b 0a pBt->inStmt ){.
155dd 20 20 20 20 69 6e 74 20 69 53 74 6d 74 70 6f 69 int iStmtpoi
155de 6e 74 20 3d 20 70 2d 3e 64 62 2d 3e 6e 53 61 76 nt = p->db->nSav
155df 65 70 6f 69 6e 74 3b 0a 20 20 20 20 72 63 20 3d epoint;. rc =
155e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 sqlite3PagerSav
155e1 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 epoint(pBt->pPag
155e2 65 72 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f er, SAVEPOINT_RO
155e3 4c 4c 42 41 43 4b 2c 20 69 53 74 6d 74 70 6f 69 LLBACK, iStmtpoi
155e4 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d nt);. if( rc=
155e5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
155e6 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
155e7 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 70 PagerSavepoint(p
155e8 42 74 2d 3e 70 50 61 67 65 72 2c 20 53 41 56 45 Bt->pPager, SAVE
155e9 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 69 POINT_RELEASE, i
155ea 53 74 6d 74 70 6f 69 6e 74 29 3b 0a 20 20 20 20 Stmtpoint);.
155eb 7d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d }. pBt->inStm
155ec 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c t = 0;. }. sql
155ed 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
155ee 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
155ef 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 }../*.** The sec
155f0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
155f1 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6f this function, o
155f2 70 2c 20 69 73 20 61 6c 77 61 79 73 20 53 41 56 p, is always SAV
155f3 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 0a EPOINT_ROLLBACK.
155f4 2a 2a 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f ** or SAVEPOINT_
155f5 52 45 4c 45 41 53 45 2e 20 54 68 69 73 20 66 75 RELEASE. This fu
155f6 6e 63 74 69 6f 6e 20 65 69 74 68 65 72 20 72 65 nction either re
155f7 6c 65 61 73 65 73 20 6f 72 20 72 6f 6c 6c 73 20 leases or rolls
155f8 62 61 63 6b 20 74 68 65 0a 2a 2a 20 73 61 76 65 back the.** save
155f9 70 6f 69 6e 74 20 69 64 65 6e 74 69 66 69 65 64 point identified
155fa 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 69 53 by parameter iS
155fb 61 76 65 70 6f 69 6e 74 2c 20 64 65 70 65 6e 64 avepoint, depend
155fc 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 ing on the value
155fd 20 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a .** of op..**.*
155fe 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 53 61 76 * Normally, iSav
155ff 65 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 epoint is greate
15600 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 r than or equal
15601 74 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 76 65 72 to zero. However
15602 2c 20 69 66 20 6f 70 20 69 73 0a 2a 2a 20 53 41 , if op is.** SA
15603 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
15604 2c 20 74 68 65 6e 20 69 53 61 76 65 70 6f 69 6e , then iSavepoin
15605 74 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 2d 31 t may also be -1
15606 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
15607 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 he .** contents
15608 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 of the entire tr
15609 61 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 72 6f ansaction are ro
1560a 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 lled back. This
1560b 69 73 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 is different.**
1560c 66 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20 74 72 from a normal tr
1560d 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 62 61 ansaction rollba
1560e 63 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b 73 20 ck, as no locks
1560f 61 72 65 20 72 65 6c 65 61 73 65 64 20 61 6e 64 are released and
15610 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 the.** transact
15611 69 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70 65 6e ion remains open
15612 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
15613 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
15614 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 74 treeSavepoint(Bt
15615 72 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 ree *p, int op,
15616 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b int iSavepoint){
15617 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
15618 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 26 TE_OK;. if( p &
15619 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 & p->inTrans==TR
1561a 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 ANS_WRITE ){.
1561b 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
1561c 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 p->pBt;. ass
1561d 65 72 74 28 20 70 42 74 2d 3e 69 6e 53 74 6d 74 ert( pBt->inStmt
1561e 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
1561f 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 t( op==SAVEPOINT
15620 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d _RELEASE || op==
15621 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
15622 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 CK );. assert
15623 28 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 ( iSavepoint>=0
15624 7c 7c 20 28 69 53 61 76 65 70 6f 69 6e 74 3d 3d || (iSavepoint==
15625 2d 31 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f -1 && op==SAVEPO
15626 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29 3b INT_ROLLBACK) );
15627 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
15628 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 70 eEnter(p);. p
15629 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a Bt->db = p->db;.
1562a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1562b 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 70 PagerSavepoint(p
1562c 42 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70 2c 20 Bt->pPager, op,
1562d 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 iSavepoint);.
1562e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1562f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
15630 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 newDatabase(pBt
15631 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
15632 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
15633 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
15634 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 rc;.}../*.** Cre
15635 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72 ate a new cursor
15636 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 20 77 for the BTree w
15637 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20 hose root is on
15638 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62 the page.** iTab
15639 6c 65 2e 20 20 54 68 65 20 61 63 74 20 6f 66 20 le. The act of
1563a 61 63 71 75 69 72 69 6e 67 20 61 20 63 75 72 73 acquiring a curs
1563b 6f 72 20 67 65 74 73 20 61 20 72 65 61 64 20 6c or gets a read l
1563c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20 74 68 65 20 64 ock on .** the d
1563d 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
1563e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 30 .** If wrFlag==0
1563f 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f , then the curso
15640 72 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 r can only be us
15641 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a ed for reading..
15642 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 31 2c ** If wrFlag==1,
15643 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 then the cursor
15644 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 can be used for
15645 20 72 65 61 64 69 6e 67 20 6f 72 20 66 6f 72 0a reading or for.
15646 2a 2a 20 77 72 69 74 69 6e 67 20 69 66 20 6f 74 ** writing if ot
15647 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 73 20 66 her conditions f
15648 6f 72 20 77 72 69 74 69 6e 67 20 61 72 65 20 61 or writing are a
15649 6c 73 6f 20 6d 65 74 2e 20 20 54 68 65 73 65 0a lso met. These.
1564a 2a 2a 20 61 72 65 20 74 68 65 20 63 6f 6e 64 69 ** are the condi
1564b 74 69 6f 6e 73 20 74 68 61 74 20 6d 75 73 74 20 tions that must
1564c 62 65 20 6d 65 74 20 69 6e 20 6f 72 64 65 72 20 be met in order
1564d 66 6f 72 20 77 72 69 74 69 6e 67 20 74 6f 0a 2a for writing to.*
1564e 2a 20 62 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a * be allowed:.**
1564f 0a 2a 2a 20 31 3a 20 20 54 68 65 20 63 75 72 73 .** 1: The curs
15650 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 or must have bee
15651 6e 20 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 n opened with wr
15652 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20 32 3a Flag==1.**.** 2:
15653 20 20 4f 74 68 65 72 20 64 61 74 61 62 61 73 65 Other database
15654 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 connections tha
15655 74 20 73 68 61 72 65 20 74 68 65 20 73 61 6d 65 t share the same
15656 20 70 61 67 65 72 20 63 61 63 68 65 0a 2a 2a 20 pager cache.**
15657 20 20 20 20 62 75 74 20 77 68 69 63 68 20 61 72 but which ar
15658 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 52 45 41 e not in the REA
15659 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 73 74 D_UNCOMMITTED st
1565a 61 74 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 ate may not have
1565b 0a 2a 2a 20 20 20 20 20 63 75 72 73 6f 72 73 20 .** cursors
1565c 6f 70 65 6e 20 77 69 74 68 20 77 72 46 6c 61 67 open with wrFlag
1565d 3d 3d 30 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 ==0 on the same
1565e 74 61 62 6c 65 2e 20 20 4f 74 68 65 72 77 69 73 table. Otherwis
1565f 65 0a 2a 2a 20 20 20 20 20 74 68 65 20 63 68 61 e.** the cha
15660 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 69 nges made by thi
15661 73 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 77 s write cursor w
15662 6f 75 6c 64 20 62 65 20 76 69 73 69 62 6c 65 20 ould be visible
15663 74 6f 0a 2a 2a 20 20 20 20 20 74 68 65 20 72 65 to.** the re
15664 61 64 20 63 75 72 73 6f 72 73 20 69 6e 20 74 68 ad cursors in th
15665 65 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 e other database
15666 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a connection..**.
15667 2a 2a 20 33 3a 20 20 54 68 65 20 64 61 74 61 62 ** 3: The datab
15668 61 73 65 20 6d 75 73 74 20 62 65 20 77 72 69 74 ase must be writ
15669 61 62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72 65 61 able (not on rea
1566a 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 29 0a 2a 2a d-only media).**
1566b 0a 2a 2a 20 34 3a 20 20 54 68 65 72 65 20 6d 75 .** 4: There mu
1566c 73 74 20 62 65 20 61 6e 20 61 63 74 69 76 65 20 st be an active
1566d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
1566e 2a 2a 20 4e 6f 20 63 68 65 63 6b 69 6e 67 20 69 ** No checking i
1566f 73 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20 73 s done to make s
15670 75 72 65 20 74 68 61 74 20 70 61 67 65 20 69 54 ure that page iT
15671 61 62 6c 65 20 72 65 61 6c 6c 79 20 69 73 20 74 able really is t
15672 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 he.** root page
15673 6f 66 20 61 20 62 2d 74 72 65 65 2e 20 20 49 66 of a b-tree. If
15674 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 6e it is not, then
15675 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 71 75 the cursor acqu
15676 69 72 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 ired.** will not
15677 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 2e work correctly.
15678 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 .**.** It is ass
15679 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 73 71 umed that the sq
1567a 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
1567b 53 69 7a 65 28 29 20 62 79 74 65 73 20 6f 66 20 Size() bytes of
1567c 6d 65 6d 6f 72 79 20 0a 2a 2a 20 70 6f 69 6e 74 memory .** point
1567d 65 64 20 74 6f 20 62 79 20 70 43 75 72 20 68 61 ed to by pCur ha
1567e 76 65 20 62 65 65 6e 20 7a 65 72 6f 65 64 20 62 ve been zeroed b
1567f 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f y the caller..*/
15680 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
15681 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 eCursor(. Btree
15682 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
15683 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15684 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a /* The btree *
15685 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 /. int iTable,
15686 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15687 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
15688 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 ot page of table
15689 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e to open */. in
1568a 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 t wrFlag,
1568b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1568c 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 /* 1 to wri
1568d 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 te. 0 read-only
1568e 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 */. struct KeyI
1568f 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 nfo *pKeyInfo,
15690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
15691 69 72 73 74 20 61 72 67 20 74 6f 20 63 6f 6d 70 irst arg to comp
15692 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 arison function
15693 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 */. BtCursor *p
15694 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20 20 Cur
15695 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
15696 70 61 63 65 20 66 6f 72 20 6e 65 77 20 63 75 72 pace for new cur
15697 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 sor */.){. int
15698 72 63 3b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 rc;. Pgno nPage
15699 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
1569a 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 t = p->pBt;.. a
1569b 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
1569c 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 reeHoldsMutex(p)
1569d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 );. assert( wr
1569e 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c 61 Flag==0 || wrFla
1569f 67 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 77 72 g==1 );. if( wr
156a0 46 6c 61 67 20 29 7b 0a 20 20 20 20 61 73 73 65 Flag ){. asse
156a1 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e rt( !pBt->readOn
156a2 6c 79 20 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 ly );. if( NE
156a3 56 45 52 28 70 42 74 2d 3e 72 65 61 64 4f 6e 6c VER(pBt->readOnl
156a4 79 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 y) ){. retu
156a5 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e rn SQLITE_READON
156a6 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 LY;. }. if
156a7 28 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 ( checkReadLocks
156a8 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 2c 20 30 (p, iTable, 0, 0
156a9 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
156aa 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b n SQLITE_LOCKED;
156ab 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
156ac 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 ( pBt->pPage1==0
156ad 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6c 6f 63 ){. rc = loc
156ae 6b 42 74 72 65 65 57 69 74 68 52 65 74 72 79 28 kBtreeWithRetry(
156af 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d p);. if( rc!=
156b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
156b1 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
156b2 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e }. }. pCur->
156b3 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f pgnoRoot = (Pgno
156b4 29 69 54 61 62 6c 65 3b 0a 20 20 72 63 20 3d 20 )iTable;. rc =
156b5 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
156b6 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 count(pBt->pPage
156b7 72 2c 20 28 69 6e 74 20 2a 29 26 6e 50 61 67 65 r, (int *)&nPage
156b8 29 3b 20 0a 20 20 69 66 28 20 72 63 21 3d 53 51 ); . if( rc!=SQ
156b9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
156ba 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
156bb 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20 26 26 if( iTable==1 &&
156bc 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 nPage==0 ){.
156bd 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 4d 50 rc = SQLITE_EMP
156be 54 59 3b 0a 20 20 20 20 67 6f 74 6f 20 63 72 65 TY;. goto cre
156bf 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70 ate_cursor_excep
156c0 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 72 63 20 3d tion;. }. rc =
156c1 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 getAndInitPage(
156c2 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 pBt, pCur->pgnoR
156c3 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 oot, &pCur->apPa
156c4 67 65 5b 30 5d 29 3b 0a 20 20 69 66 28 20 72 63 ge[0]);. if( rc
156c5 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
156c6 20 20 20 67 6f 74 6f 20 63 72 65 61 74 65 5f 63 goto create_c
156c7 75 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3b ursor_exception;
156c8 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74 . }.. /* Now t
156c9 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 72 hat no other err
156ca 6f 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 66 ors can occur, f
156cb 69 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 6e inish filling in
156cc 20 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20 20 the BtCursor.
156cd 2a 2a 20 76 61 72 69 61 62 6c 65 73 2c 20 6c 69 ** variables, li
156ce 6e 6b 20 74 68 65 20 63 75 72 73 6f 72 20 69 6e nk the cursor in
156cf 74 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 20 to the BtShared
156d0 6c 69 73 74 20 61 6e 64 20 73 65 74 20 2a 70 70 list and set *pp
156d1 43 75 72 20 28 74 68 65 0a 20 20 2a 2a 20 6f 75 Cur (the. ** ou
156d2 74 70 75 74 20 61 72 67 75 6d 65 6e 74 20 74 6f tput argument to
156d3 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 29 2e this function).
156d4 0a 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 4b . */. pCur->pK
156d5 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 eyInfo = pKeyInf
156d6 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 72 65 o;. pCur->pBtre
156d7 65 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e 70 e = p;. pCur->p
156d8 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 43 75 72 Bt = pBt;. pCur
156d9 2d 3e 77 72 46 6c 61 67 20 3d 20 28 75 38 29 77 ->wrFlag = (u8)w
156da 72 46 6c 61 67 3b 0a 20 20 70 43 75 72 2d 3e 70 rFlag;. pCur->p
156db 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 43 75 72 Next = pBt->pCur
156dc 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 75 72 2d sor;. if( pCur-
156dd 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 43 >pNext ){. pC
156de 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 ur->pNext->pPrev
156df 20 3d 20 70 43 75 72 3b 0a 20 20 7d 0a 20 20 70 = pCur;. }. p
156e0 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 Bt->pCursor = pC
156e1 75 72 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 ur;. pCur->eSta
156e2 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 te = CURSOR_INVA
156e3 4c 49 44 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 LID;.. return S
156e4 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 63 72 65 61 74 QLITE_OK;..creat
156e5 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70 74 69 e_cursor_excepti
156e6 6f 6e 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 on:. releasePag
156e7 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 e(pCur->apPage[0
156e8 5d 29 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 ]);. unlockBtre
156e9 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a eIfUnused(pBt);.
156ea 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 return rc;.}.S
156eb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
156ec 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
156ed 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 rsor(. Btree *p
156ee 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
156ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156f0 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 /* The btree
156f1 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 */. int iTable
156f2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
156f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156f4 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f /* Root page o
156f5 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 f table to open
156f6 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c */. int wrFlag,
156f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156f9 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 /* 1 to write.
156fa 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 0 read-only */.
156fb 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 struct KeyInfo
156fc 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 *pKeyInfo,
156fd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
156fe 46 69 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f First arg to xCo
156ff 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 mpare() */. BtC
15700 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 ursor *pCur
15701 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15702 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
15703 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72 e new cursor her
15704 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
15705 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
15706 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 2d 3e 70 Enter(p);. p->p
15707 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a Bt->db = p->db;.
15708 20 20 72 63 20 3d 20 62 74 72 65 65 43 75 72 73 rc = btreeCurs
15709 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c 20 77 72 or(p, iTable, wr
1570a 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 Flag, pKeyInfo,
1570b 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 pCur);. sqlite3
1570c 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
1570d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 return rc;.}.SQ
1570e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1570f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
15710 73 6f 72 53 69 7a 65 28 29 7b 0a 20 20 72 65 74 sorSize(){. ret
15711 75 72 6e 20 73 69 7a 65 6f 66 28 42 74 43 75 72 urn sizeof(BtCur
15712 73 6f 72 29 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a sor);.}..../*.**
15713 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 2e Close a cursor.
15714 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 The read lock
15715 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
15716 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65 64 file is released
15717 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 .** when the las
15718 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 t cursor is clos
15719 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1571a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1571b 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 3BtreeCloseCurso
1571c 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 r(BtCursor *pCur
1571d 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 ){. Btree *pBtr
1571e 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 ee = pCur->pBtre
1571f 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 20 e;. if( pBtree
15720 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
15721 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
15722 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 20 = pCur->pBt;.
15723 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
15724 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 er(pBtree);.
15725 70 42 74 2d 3e 64 62 20 3d 20 70 42 74 72 65 65 pBt->db = pBtree
15726 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 ->db;. sqlite
15727 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 3BtreeClearCurso
15728 72 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28 r(pCur);. if(
15729 20 70 43 75 72 2d 3e 70 50 72 65 76 20 29 7b 0a pCur->pPrev ){.
1572a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 50 72 65 pCur->pPre
1572b 76 2d 3e 70 4e 65 78 74 20 3d 20 70 43 75 72 2d v->pNext = pCur-
1572c 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 >pNext;. }els
1572d 65 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 43 e{. pBt->pC
1572e 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 4e ursor = pCur->pN
1572f 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ext;. }. i
15730 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 f( pCur->pNext )
15731 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4e {. pCur->pN
15732 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 ext->pPrev = pCu
15733 72 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 7d 0a r->pPrev;. }.
15734 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d for(i=0; i<=
15735 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b pCur->iPage; i++
15736 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
15737 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 Page(pCur->apPag
15738 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 e[i]);. }.
15739 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
1573a 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 69 used(pBt);. i
1573b 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f nvalidateOverflo
1573c 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 wCache(pCur);.
1573d 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 /* sqlite3_fre
1573e 65 28 70 43 75 72 29 3b 20 2a 2f 0a 20 20 20 20 e(pCur); */.
1573f 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
15740 65 28 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a 20 e(pBtree);. }.
15741 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
15742 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 K;.}../*.** Make
15743 20 61 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 a temporary cur
15744 73 6f 72 20 62 79 20 66 69 6c 6c 69 6e 67 20 69 sor by filling i
15745 6e 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20 n the fields of
15746 70 54 65 6d 70 43 75 72 2e 0a 2a 2a 20 54 68 65 pTempCur..** The
15747 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f temporary curso
15748 72 20 69 73 20 6e 6f 74 20 6f 6e 20 74 68 65 20 r is not on the
15749 63 75 72 73 6f 72 20 6c 69 73 74 20 66 6f 72 20 cursor list for
1574a 74 68 65 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 the Btree..*/.SQ
1574b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1574c 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 d sqlite3BtreeGe
1574d 74 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 tTempCursor(BtCu
1574e 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 74 43 75 rsor *pCur, BtCu
1574f 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 72 29 7b rsor *pTempCur){
15750 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 . int i;. asse
15751 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
15752 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
15753 6d 65 6d 63 70 79 28 70 54 65 6d 70 43 75 72 2c memcpy(pTempCur,
15754 20 70 43 75 72 2c 20 73 69 7a 65 6f 66 28 42 74 pCur, sizeof(Bt
15755 43 75 72 73 6f 72 29 29 3b 0a 20 20 70 54 65 6d Cursor));. pTem
15756 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b pCur->pNext = 0;
15757 0a 20 20 70 54 65 6d 70 43 75 72 2d 3e 70 50 72 . pTempCur->pPr
15758 65 76 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d ev = 0;. for(i=
15759 30 3b 20 69 3c 3d 70 54 65 6d 70 43 75 72 2d 3e 0; i<=pTempCur->
1575a 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 iPage; i++){.
1575b 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 sqlite3PagerRef
1575c 28 70 54 65 6d 70 43 75 72 2d 3e 61 70 50 61 67 (pTempCur->apPag
1575d 65 5b 69 5d 2d 3e 70 44 62 50 61 67 65 29 3b 0a e[i]->pDbPage);.
1575e 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 }. assert( pT
1575f 65 6d 70 43 75 72 2d 3e 70 4b 65 79 3d 3d 30 20 empCur->pKey==0
15760 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 );.}../*.** Dele
15761 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 63 te a temporary c
15762 75 72 73 6f 72 20 73 75 63 68 20 61 73 20 77 61 ursor such as wa
15763 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 43 72 s made by the Cr
15764 65 61 74 65 54 65 6d 70 6f 72 61 72 79 43 75 72 eateTemporaryCur
15765 73 6f 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f sor().** functio
15766 6e 20 61 62 6f 76 65 2e 0a 2a 2f 0a 53 51 4c 49 n above..*/.SQLI
15767 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
15768 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 6c 65 sqlite3BtreeRele
15769 61 73 65 54 65 6d 70 43 75 72 73 6f 72 28 42 74 aseTempCursor(Bt
1576a 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
1576b 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 int i;. assert
1576c 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
1576d 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 66 6f ex(pCur) );. fo
1576e 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e r(i=0; i<=pCur->
1576f 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 iPage; i++){.
15770 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
15771 65 66 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b ef(pCur->apPage[
15772 69 5d 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 i]->pDbPage);.
15773 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 }. sqlite3_free
15774 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 7d 0a (pCur->pKey);.}.
15775 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
15776 20 74 68 65 20 42 74 43 75 72 73 6f 72 2a 20 67 the BtCursor* g
15777 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 iven in the argu
15778 6d 65 6e 74 20 68 61 73 20 61 20 76 61 6c 69 64 ment has a valid
15779 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 .** BtCursor.inf
1577a 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 o structure. If
1577b 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 it is not alrea
1577c 64 79 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a dy valid, call.*
1577d 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 * sqlite3BtreePa
1577e 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c rseCell() to fil
1577f 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42 l it in..**.** B
15780 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20 tCursor.info is
15781 61 20 63 61 63 68 65 20 6f 66 20 74 68 65 20 69 a cache of the i
15782 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 nformation in th
15783 65 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a e current cell..
15784 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 20 63 61 ** Using this ca
15785 63 68 65 20 72 65 64 75 63 65 73 20 74 68 65 20 che reduces the
15786 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 number of calls
15787 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 to sqlite3BtreeP
15788 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a arseCell()..**.*
15789 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 * 2007-06-25: T
1578a 68 65 72 65 20 69 73 20 61 20 62 75 67 20 69 6e here is a bug in
1578b 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f some versions o
1578c 66 20 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 f MSVC that caus
1578d 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 e the.** compile
1578e 72 20 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 r to crash when
1578f 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 getCellInfo() is
15790 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
15791 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 a macro..** But
15792 74 68 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 there is a measu
15793 72 65 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 reable speed adv
15794 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 antage to using
15795 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 the macro on gcc
15796 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 .** (when less c
15797 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 ompiler optimiza
15798 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f tions like -Os o
15799 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 r -O0 are used a
1579a 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c nd the.** compil
1579b 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 er is not doing
1579c 61 67 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 agressive inlini
1579d 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 ng.) So we use
1579e 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a a real function.
1579f 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 ** for MSVC and
157a0 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 a macro for ever
157a1 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 ything else. Ti
157a2 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 cket #2457..*/.#
157a3 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
157a4 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 static void asse
157a5 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 rtCellInfo(BtCur
157a6 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 sor *pCur){.
157a7 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
157a8 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 int iPage = p
157a9 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 Cur->iPage;.
157aa 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c memset(&info, 0,
157ab 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a sizeof(info));.
157ac 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
157ad 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e ParseCell(pCur->
157ae 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 70 apPage[iPage], p
157af 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 Cur->aiIdx[iPage
157b0 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 ], &info);. a
157b1 73 73 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 69 ssert( memcmp(&i
157b2 6e 66 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f nfo, &pCur->info
157b3 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3d , sizeof(info))=
157b4 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a =0 );. }.#else.
157b5 20 20 23 64 65 66 69 6e 65 20 61 73 73 65 72 74 #define assert
157b6 43 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64 CellInfo(x).#end
157b7 69 66 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f 56 if.#ifdef _MSC_V
157b8 45 52 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 65 ER. /* Use a re
157b9 61 6c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 4d al function in M
157ba 53 56 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 SVC to work arou
157bb 6e 64 20 62 75 67 73 20 69 6e 20 74 68 61 74 20 nd bugs in that
157bc 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 73 compiler. */. s
157bd 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 65 tatic void getCe
157be 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20 llInfo(BtCursor
157bf 2a 70 43 75 72 29 7b 0a 20 20 20 20 69 66 28 20 *pCur){. if(
157c0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
157c1 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ==0 ){. int
157c2 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 iPage = pCur->i
157c3 50 61 67 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 Page;. sqli
157c4 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c te3BtreeParseCel
157c5 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 l(pCur->apPage[i
157c6 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 Page],pCur->aiId
157c7 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e x[iPage],&pCur->
157c8 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 info);. pCu
157c9 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 r->validNKey = 1
157ca 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
157cb 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 assertCellInf
157cc 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 o(pCur);. }.
157cd 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e }.#else /* if n
157ce 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 ot _MSC_VER */.
157cf 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 /* Use a macro
157d0 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d in all other com
157d1 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 pilers so that t
157d2 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 he function is i
157d3 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e nlined */.#defin
157d4 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 e getCellInfo(pC
157d5 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20 ur)
157d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157d8 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 \. if(
157d9 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
157da 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 e==0 ){
157db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157dd 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
157de 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 int iPage = pCur
157df 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 ->iPage;
157e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157e2 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
157e3 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
157e4 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 seCell(pCur->apP
157e5 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d age[iPage],pCur-
157e6 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 >aiIdx[iPage],&p
157e7 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20 20 Cur->info); \.
157e8 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
157e9 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 y = 1;
157ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157ec 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
157ed 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 }else{
157ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
157f2 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e assertCellIn
157f3 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20 20 fo(pCur);
157f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
157f7 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f . }.#endif /* _
157f8 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a MSC_VER */../*.*
157f9 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 * Set *pSize to
157fa 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
157fb 62 75 66 66 65 72 20 6e 65 65 64 65 64 20 74 6f buffer needed to
157fc 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 20 hold the value
157fd 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f of.** the key fo
157fe 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 65 6e r the current en
157ff 74 72 79 2e 20 20 49 66 20 74 68 65 20 63 75 72 try. If the cur
15800 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 sor is not point
15801 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 ing.** to a vali
15802 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 d entry, *pSize
15803 69 73 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a is set to 0. .**
15804 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 20 .** For a table
15805 77 69 74 68 20 74 68 65 20 49 4e 54 4b 45 59 20 with the INTKEY
15806 66 6c 61 67 20 73 65 74 2c 20 74 68 69 73 20 72 flag set, this r
15807 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 outine returns t
15808 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 he key.** itself
15809 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 , not the number
1580a 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
1580b 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f key..*/.SQLITE_
1580c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1580d 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 te3BtreeKeySize(
1580e 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
1580f 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 i64 *pSize){. i
15810 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
15811 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
15812 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 ex(pCur) );. rc
15813 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
15814 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
15815 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
15816 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 _OK ){. asser
15817 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
15818 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
15819 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d || pCur->eState=
1581a 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
1581b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 . if( pCur->e
1581c 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e State==CURSOR_IN
1581d 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 2a VALID ){. *
1581e 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d pSize = 0;. }
1581f 65 6c 73 65 7b 0a 20 20 20 20 20 20 67 65 74 43 else{. getC
15820 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 ellInfo(pCur);.
15821 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 *pSize = pC
15822 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 ur->info.nKey;.
15823 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
15824 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n rc;.}../*.** S
15825 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 et *pSize to the
15826 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
15827 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68 65 20 of data in the
15828 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 63 75 72 entry the.** cur
15829 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f sor currently po
1582a 69 6e 74 73 20 74 6f 2e 20 20 41 6c 77 61 79 73 ints to. Always
1582b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1582c 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73 K..** Failure is
1582d 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 not possible.
1582e 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 If the cursor is
1582f 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 0a 2a not currently.*
15830 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e * pointing to an
15831 20 65 6e 74 72 79 20 28 77 68 69 63 68 20 63 61 entry (which ca
15832 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72 20 65 78 n happen, for ex
15833 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 74 68 65 ample, if.** the
15834 20 64 61 74 61 62 61 73 65 20 69 73 20 65 6d 70 database is emp
15835 74 79 29 20 74 68 65 6e 20 2a 70 53 69 7a 65 20 ty) then *pSize
15836 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a is set to 0..*/.
15837 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
15838 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
15839 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 ataSize(BtCursor
1583a 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69 *pCur, u32 *pSi
1583b 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a ze){. int rc;..
1583c 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
1583d 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
1583e 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f );. rc = resto
1583f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
15840 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
15841 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
15842 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
15843 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
15844 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d INVALID || pCur-
15845 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
15846 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 69 66 28 VALID );. if(
15847 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
15848 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b URSOR_INVALID ){
15849 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 70 6f . /* Not po
1584a 69 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 inting at a vali
1584b 64 20 65 6e 74 72 79 20 2d 20 73 65 74 20 2a 70 d entry - set *p
1584c 53 69 7a 65 20 74 6f 20 30 2e 20 2a 2f 0a 20 20 Size to 0. */.
1584d 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a *pSize = 0;.
1584e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1584f 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 getCellInfo(pCu
15850 72 29 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 r);. *pSize
15851 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 = pCur->info.nD
15852 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ata;. }. }.
15853 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
15854 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 70 *.** Given the p
15855 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e age number of an
15856 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
15857 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 n the database (
15858 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 parameter.** ovf
15859 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f l), this functio
1585a 6e 20 66 69 6e 64 73 20 74 68 65 20 70 61 67 65 n finds the page
1585b 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e number of the n
1585c 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 ext page in the
1585d 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 .** linked list
1585e 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 of overflow page
1585f 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 s. If possible,
15860 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74 6f it uses the auto
15861 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 -vacuum.** point
15862 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 74 er-map data inst
15863 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20 74 ead of reading t
15864 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 he content of pa
15865 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f ge ovfl to do so
15866 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 . .**.** If an e
15867 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 53 rror occurs an S
15868 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 QLite error code
15869 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 is returned. Ot
1586a 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 55 herwise:.**.** U
1586b 6e 6c 65 73 73 20 70 50 67 6e 6f 4e 65 78 74 20 nless pPgnoNext
1586c 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 70 61 67 is NULL, the pag
1586d 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
1586e 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 0a 2a next overflow .*
1586f 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 * page in the li
15870 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 77 72 69 nked list is wri
15871 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f 4e 65 tten to *pPgnoNe
15872 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 66 6c xt. If page ovfl
15873 0a 2a 2a 20 69 73 20 74 68 65 20 6c 61 73 74 20 .** is the last
15874 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b page in its link
15875 65 64 20 6c 69 73 74 2c 20 2a 70 50 67 6e 6f 4e ed list, *pPgnoN
15876 65 78 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 ext is set to ze
15877 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70 70 ro. .**.** If pp
15878 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c Page is not NULL
15879 2c 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 , *ppPage is set
1587a 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 65 2a to the MemPage*
1587b 20 68 61 6e 64 6c 65 0a 2a 2a 20 66 6f 72 20 70 handle.** for p
1587c 61 67 65 20 6f 76 66 6c 2e 20 54 68 65 20 75 6e age ovfl. The un
1587d 64 65 72 6c 79 69 6e 67 20 70 61 67 65 72 20 70 derlying pager p
1587e 61 67 65 20 6d 61 79 20 68 61 76 65 20 62 65 65 age may have bee
1587f 6e 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 77 n requested.** w
15880 69 74 68 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e ith the noConten
15881 74 20 66 6c 61 67 20 73 65 74 2c 20 73 6f 20 74 t flag set, so t
15882 68 65 20 70 61 67 65 20 64 61 74 61 20 61 63 63 he page data acc
15883 65 73 73 61 62 6c 65 20 76 69 61 0a 2a 2a 20 74 essable via.** t
15884 68 69 73 20 68 61 6e 64 6c 65 20 6d 61 79 20 6e his handle may n
15885 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 0a 2a ot be trusted..*
15886 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
15887 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 OverflowPage(.
15888 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a BtShared *pBt, .
15889 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20 Pgno ovfl,
1588a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1588b 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 * Overflow page
1588c 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 */. MemPage **p
1588d 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 pPage,
1588e 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67 /* OUT: MemPag
1588f 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 e handle */. Pg
15890 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 no *pPgnoNext
15891 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 /* OU
15892 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 T: Next overflow
15893 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
15894 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d ){. Pgno next =
15895 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 0;. int rc = S
15896 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 QLITE_OK;.. ass
15897 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15898 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
15899 65 78 29 20 29 3b 0a 20 20 2f 2a 20 4f 6e 65 20 ex) );. /* One
1589a 6f 66 20 74 68 65 73 65 20 6d 75 73 74 20 6e 6f of these must no
1589b 74 20 62 65 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 t be NULL. Other
1589c 77 69 73 65 2c 20 77 68 79 20 63 61 6c 6c 20 74 wise, why call t
1589d 68 69 73 20 66 75 6e 63 74 69 6f 6e 3f 20 2a 2f his function? */
1589e 0a 20 20 61 73 73 65 72 74 28 70 70 50 61 67 65 . assert(ppPage
1589f 20 7c 7c 20 70 50 67 6e 6f 4e 65 78 74 29 3b 0a || pPgnoNext);.
158a0 0a 20 20 2f 2a 20 49 66 20 70 50 67 6e 6f 4e 65 . /* If pPgnoNe
158a1 78 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e xt is NULL, then
158a2 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
158a3 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 74 s being called t
158a4 6f 20 6f 62 74 61 69 6e 0a 20 20 2a 2a 20 61 20 o obtain. ** a
158a5 4d 65 6d 50 61 67 65 2a 20 72 65 66 65 72 65 6e MemPage* referen
158a6 63 65 20 6f 6e 6c 79 2e 20 4e 6f 20 70 61 67 65 ce only. No page
158a7 2d 64 61 74 61 20 69 73 20 72 65 71 75 69 72 65 -data is require
158a8 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a d in this case..
158a9 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 67 6e */. if( !pPgn
158aa 6f 4e 65 78 74 20 29 7b 0a 20 20 20 20 72 65 74 oNext ){. ret
158ab 75 72 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 urn sqlite3Btree
158ac 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 GetPage(pBt, ovf
158ad 6c 2c 20 70 70 50 61 67 65 2c 20 31 29 3b 0a 20 l, ppPage, 1);.
158ae 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }..#ifndef SQLI
158af 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
158b0 55 4d 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 UM. /* Try to f
158b1 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 ind the next pag
158b2 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f e in the overflo
158b3 77 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 w list using the
158b4 0a 20 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d . ** autovacuum
158b5 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
158b6 65 73 2e 20 47 75 65 73 73 20 74 68 61 74 20 74 es. Guess that t
158b7 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 he next page in
158b8 0a 20 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c . ** the overfl
158b9 6f 77 20 6c 69 73 74 20 69 73 20 70 61 67 65 20 ow list is page
158ba 6e 75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e number (ovfl+1).
158bb 20 49 66 20 74 68 61 74 20 67 75 65 73 73 20 74 If that guess t
158bc 75 72 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 urns . ** out t
158bd 6f 20 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c o be wrong, fall
158be 20 62 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 back to loading
158bf 20 74 68 65 20 64 61 74 61 20 6f 66 20 70 61 67 the data of pag
158c0 65 20 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f e . ** number o
158c1 76 66 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 vfl to determine
158c2 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e the next page n
158c3 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 umber.. */. if
158c4 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
158c5 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 m ){. Pgno pg
158c6 6e 6f 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 no;. Pgno iGu
158c7 65 73 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 ess = ovfl+1;.
158c8 20 20 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 u8 eType;..
158c9 20 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 while( PTRMAP_I
158ca 53 50 41 47 45 28 70 42 74 2c 20 69 47 75 65 73 SPAGE(pBt, iGues
158cb 73 29 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 s) || iGuess==PE
158cc 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
158cd 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47 pBt) ){. iG
158ce 75 65 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 uess++;. }..
158cf 20 20 20 69 66 28 20 69 47 75 65 73 73 3c 3d 70 if( iGuess<=p
158d0 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
158d1 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d t) ){. rc =
158d2 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 ptrmapGet(pBt,
158d3 69 47 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20 iGuess, &eType,
158d4 26 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 &pgno);. if
158d5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
158d6 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
158d7 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
158d8 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 if( eType==P
158d9 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 TRMAP_OVERFLOW2
158da 26 26 20 70 67 6e 6f 3d 3d 6f 76 66 6c 20 29 7b && pgno==ovfl ){
158db 0a 20 20 20 20 20 20 20 20 6e 65 78 74 20 3d 20 . next =
158dc 69 47 75 65 73 73 3b 0a 20 20 20 20 20 20 7d 0a iGuess;. }.
158dd 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
158de 0a 0a 20 20 69 66 28 20 6e 65 78 74 3d 3d 30 20 .. if( next==0
158df 7c 7c 20 70 70 50 61 67 65 20 29 7b 0a 20 20 20 || ppPage ){.
158e0 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
158e1 3d 20 30 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 = 0;.. rc = s
158e2 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
158e3 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 70 ge(pBt, ovfl, &p
158e4 50 61 67 65 2c 20 6e 65 78 74 21 3d 30 29 3b 0a Page, next!=0);.
158e5 20 20 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 assert(rc==S
158e6 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 QLITE_OK || pPag
158e7 65 3d 3d 30 29 3b 0a 20 20 20 20 69 66 28 20 6e e==0);. if( n
158e8 65 78 74 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 ext==0 && rc==SQ
158e9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
158ea 20 6e 65 78 74 20 3d 20 67 65 74 34 62 79 74 65 next = get4byte
158eb 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a (pPage->aData);.
158ec 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 }.. if( p
158ed 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 2a pPage ){. *
158ee 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a ppPage = pPage;.
158ef 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
158f0 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
158f1 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ge);. }. }.
158f2 20 2a 70 50 67 6e 6f 4e 65 78 74 20 3d 20 6e 65 *pPgnoNext = ne
158f3 78 74 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 xt;.. return rc
158f4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 ;.}../*.** Copy
158f5 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 data from a buff
158f6 65 72 20 74 6f 20 61 20 70 61 67 65 2c 20 6f 72 er to a page, or
158f7 20 66 72 6f 6d 20 61 20 70 61 67 65 20 74 6f 20 from a page to
158f8 61 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 a buffer..**.**
158f9 70 50 61 79 6c 6f 61 64 20 69 73 20 61 20 70 6f pPayload is a po
158fa 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 73 74 inter to data st
158fb 6f 72 65 64 20 6f 6e 20 64 61 74 61 62 61 73 65 ored on database
158fc 20 70 61 67 65 20 70 44 62 50 61 67 65 2e 0a 2a page pDbPage..*
158fd 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 65 4f * If argument eO
158fe 70 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e p is false, then
158ff 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 nByte bytes of
15900 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 0a data are copied.
15901 2a 2a 20 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 ** from pPayload
15902 20 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 70 to the buffer p
15903 6f 69 6e 74 65 64 20 61 74 20 62 79 20 70 42 75 ointed at by pBu
15904 66 2e 20 49 66 20 65 4f 70 20 69 73 20 74 72 75 f. If eOp is tru
15905 65 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 e,.** then sqlit
15906 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 69 e3PagerWrite() i
15907 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 s called on pDbP
15908 61 67 65 20 61 6e 64 20 6e 42 79 74 65 20 62 79 age and nByte by
15909 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61 tes.** of data a
1590a 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 re copied from t
1590b 68 65 20 62 75 66 66 65 72 20 70 42 75 66 20 74 he buffer pBuf t
1590c 6f 20 70 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a o pPayload..**.*
1590d 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
1590e 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 eturned on succe
1590f 73 73 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e ss, otherwise an
15910 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a error code..*/.
15911 73 74 61 74 69 63 20 69 6e 74 20 63 6f 70 79 50 static int copyP
15912 61 79 6c 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a ayload(. void *
15913 70 50 61 79 6c 6f 61 64 2c 20 20 20 20 20 20 20 pPayload,
15914 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
15915 6f 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 o page data */.
15916 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 void *pBuf,
15917 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
15918 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 inter to buffer
15919 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 */. int nByte,
1591a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1591b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1591c 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 s to copy */. i
1591d 6e 74 20 65 4f 70 2c 20 20 20 20 20 20 20 20 20 nt eOp,
1591e 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e /* 0 ->
1591f 20 63 6f 70 79 20 66 72 6f 6d 20 70 61 67 65 2c copy from page,
15920 20 31 20 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61 1 -> copy to pa
15921 67 65 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a ge */. DbPage *
15922 70 44 62 50 61 67 65 20 20 20 20 20 20 20 20 20 pDbPage
15923 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 /* Page contai
15924 6e 69 6e 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f ning pPayload */
15925 0a 29 7b 0a 20 20 69 66 28 20 65 4f 70 20 29 7b .){. if( eOp ){
15926 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 . /* Copy dat
15927 61 20 66 72 6f 6d 20 62 75 66 66 65 72 20 74 6f a from buffer to
15928 20 70 61 67 65 20 28 61 20 77 72 69 74 65 20 6f page (a write o
15929 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 peration) */.
1592a 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
1592b 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50 3PagerWrite(pDbP
1592c 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
1592d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1592e 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1592f 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 }. memcpy
15930 28 70 50 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c (pPayload, pBuf,
15931 20 6e 42 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65 nByte);. }else
15932 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 {. /* Copy da
15933 74 61 20 66 72 6f 6d 20 70 61 67 65 20 74 6f 20 ta from page to
15934 62 75 66 66 65 72 20 28 61 20 72 65 61 64 20 6f buffer (a read o
15935 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 peration) */.
15936 20 6d 65 6d 63 70 79 28 70 42 75 66 2c 20 70 50 memcpy(pBuf, pP
15937 61 79 6c 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a ayload, nByte);.
15938 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
15939 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
1593a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
1593b 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f s used to read o
1593c 72 20 6f 76 65 72 77 72 69 74 65 20 70 61 79 6c r overwrite payl
1593d 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a oad information.
1593e 2a 2a 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 ** for the entry
1593f 20 74 68 61 74 20 74 68 65 20 70 43 75 72 20 63 that the pCur c
15940 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e ursor is pointin
15941 67 20 74 6f 2e 20 49 66 20 74 68 65 20 65 4f 70 g to. If the eOp
15942 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 .** parameter is
15943 20 30 2c 20 74 68 69 73 20 69 73 20 61 20 72 65 0, this is a re
15944 61 64 20 6f 70 65 72 61 74 69 6f 6e 20 28 64 61 ad operation (da
15945 74 61 20 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a ta copied into.*
15946 2a 20 62 75 66 66 65 72 20 70 42 75 66 29 2e 20 * buffer pBuf).
15947 49 66 20 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 If it is non-zer
15948 6f 2c 20 61 20 77 72 69 74 65 20 28 64 61 74 61 o, a write (data
15949 20 63 6f 70 69 65 64 20 66 72 6f 6d 0a 2a 2a 20 copied from.**
1594a 62 75 66 66 65 72 20 70 42 75 66 29 2e 0a 2a 2a buffer pBuf)..**
1594b 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 6f 66 20 22 .** A total of "
1594c 61 6d 74 22 20 62 79 74 65 73 20 61 72 65 20 72 amt" bytes are r
1594d 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 20 62 ead or written b
1594e 65 67 69 6e 6e 69 6e 67 20 61 74 20 22 6f 66 66 eginning at "off
1594f 73 65 74 22 2e 0a 2a 2a 20 44 61 74 61 20 69 73 set"..** Data is
15950 20 72 65 61 64 20 74 6f 20 6f 72 20 66 72 6f 6d read to or from
15951 20 74 68 65 20 62 75 66 66 65 72 20 70 42 75 66 the buffer pBuf
15952 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
15953 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 tine does not ma
15954 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e ke a distinction
15955 20 62 65 74 77 65 65 6e 20 6b 65 79 20 61 6e 64 between key and
15956 20 64 61 74 61 2e 0a 2a 2a 20 49 74 20 6a 75 73 data..** It jus
15957 74 20 72 65 61 64 73 20 6f 72 20 77 72 69 74 65 t reads or write
15958 73 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 s bytes from the
15959 20 70 61 79 6c 6f 61 64 20 61 72 65 61 2e 20 20 payload area.
1595a 44 61 74 61 20 6d 69 67 68 74 20 0a 2a 2a 20 61 Data might .** a
1595b 70 70 65 61 72 20 6f 6e 20 74 68 65 20 6d 61 69 ppear on the mai
1595c 6e 20 70 61 67 65 20 6f 72 20 62 65 20 73 63 61 n page or be sca
1595d 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75 ttered out on mu
1595e 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20 ltiple overflow
1595f 0a 2a 2a 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a .** pages..**.**
15960 20 49 66 20 74 68 65 20 42 74 43 75 72 73 6f 72 If the BtCursor
15961 2e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c .isIncrblobHandl
15962 65 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 61 e flag is set, a
15963 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a nd the current.*
15964 2a 20 63 75 72 73 6f 72 20 65 6e 74 72 79 20 75 * cursor entry u
15965 73 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ses one or more
15966 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 overflow pages,
15967 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a this function.**
15968 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 65 allocates space
15969 20 66 6f 72 20 61 6e 64 20 6c 61 7a 69 6c 79 20 for and lazily
1596a 70 6f 70 6c 75 61 74 65 73 20 74 68 65 20 6f 76 popluates the ov
1596b 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 erflow page-list
1596c 20 0a 2a 2a 20 63 61 63 68 65 20 61 72 72 61 79 .** cache array
1596d 20 28 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 (BtCursor.aOver
1596e 66 6c 6f 77 29 2e 20 53 75 62 73 65 71 75 65 6e flow). Subsequen
1596f 74 20 63 61 6c 6c 73 20 75 73 65 20 74 68 69 73 t calls use this
15970 0a 2a 2a 20 63 61 63 68 65 20 74 6f 20 6d 61 6b .** cache to mak
15971 65 20 73 65 65 6b 69 6e 67 20 74 6f 20 74 68 65 e seeking to the
15972 20 73 75 70 70 6c 69 65 64 20 6f 66 66 73 65 74 supplied offset
15973 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 2e more efficient.
15974 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f .**.** Once an o
15975 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 verflow page-lis
15976 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 6e t cache has been
15977 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d allocated, it m
15978 61 79 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 ay be.** invalid
15979 61 74 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 ated if some oth
1597a 65 72 20 63 75 72 73 6f 72 20 77 72 69 74 65 73 er cursor writes
1597b 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 61 62 to the same tab
1597c 6c 65 2c 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65 le, or if.** the
1597d 20 63 75 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 cursor is moved
1597e 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74 20 to a different
1597f 72 6f 77 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c row. Additionall
15980 79 2c 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 y, in auto-vacuu
15981 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 m.** mode, the f
15982 6f 6c 6c 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 ollowing events
15983 6d 61 79 20 69 6e 76 61 6c 69 64 61 74 65 20 61 may invalidate a
15984 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d n overflow page-
15985 6c 69 73 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a list cache..**.*
15986 2a 20 20 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65 * * An increme
15987 6e 74 61 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 ntal vacuum,.**
15988 20 20 2a 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20 * A commit in
15989 61 75 74 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c auto_vacuum="ful
1598a 6c 22 20 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 l" mode,.** *
1598b 43 72 65 61 74 69 6e 67 20 61 20 74 61 62 6c 65 Creating a table
1598c 20 28 6d 61 79 20 72 65 71 75 69 72 65 20 6d 6f (may require mo
1598d 76 69 6e 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 ving an overflow
1598e 20 70 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 page)..*/.stati
1598f 63 20 69 6e 74 20 61 63 63 65 73 73 50 61 79 6c c int accessPayl
15990 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 oad(. BtCursor
15991 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 *pCur, /* C
15992 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 ursor pointing t
15993 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 o entry to read
15994 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 66 from */. u32 of
15995 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 2f fset, /
15996 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 * Begin reading
15997 74 68 69 73 20 66 61 72 20 69 6e 74 6f 20 70 61 this far into pa
15998 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 33 32 20 61 yload */. u32 a
15999 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
1599a 2f 2a 20 52 65 61 64 20 74 68 69 73 20 6d 61 6e /* Read this man
1599b 79 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 y bytes */. uns
1599c 69 67 6e 65 64 20 63 68 61 72 20 2a 70 42 75 66 igned char *pBuf
1599d 2c 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 62 , /* Write the b
1599e 79 74 65 73 20 69 6e 74 6f 20 74 68 69 73 20 62 ytes into this b
1599f 75 66 66 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20 uffer */ . int
159a0 73 6b 69 70 4b 65 79 2c 20 20 20 20 20 20 20 20 skipKey,
159a1 20 2f 2a 20 6f 66 66 73 65 74 20 62 65 67 69 6e /* offset begin
159a2 73 20 61 74 20 64 61 74 61 20 69 66 20 74 68 69 s at data if thi
159a3 73 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 69 s is true */. i
159a4 6e 74 20 65 4f 70 20 20 20 20 20 20 20 20 20 20 nt eOp
159a5 20 20 20 20 2f 2a 20 7a 65 72 6f 20 74 6f 20 72 /* zero to r
159a6 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 6f 20 74 6f ead. non-zero to
159a7 20 77 72 69 74 65 2e 20 2a 2f 0a 29 7b 0a 20 20 write. */.){.
159a8 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 unsigned char *a
159a9 50 61 79 6c 6f 61 64 3b 0a 20 20 69 6e 74 20 72 Payload;. int r
159aa 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
159ab 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 69 6e 74 u32 nKey;. int
159ac 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 4d 65 6d iIdx = 0;. Mem
159ad 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 Page *pPage = pC
159ae 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
159af 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 42 74 72 65 >iPage]; /* Btre
159b0 65 20 70 61 67 65 20 6f 66 20 63 75 72 72 65 6e e page of curren
159b1 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 74 53 t entry */. BtS
159b2 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 hared *pBt = pCu
159b3 72 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 r->pBt;
159b4 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 /* Btre
159b5 65 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 e this cursor be
159b6 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20 20 61 longs to */.. a
159b7 73 73 65 72 74 28 20 70 50 61 67 65 20 29 3b 0a ssert( pPage );.
159b8 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
159b9 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
159ba 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 ALID );. assert
159bb 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 ( pCur->aiIdx[pC
159bc 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 ur->iPage]<pPage
159bd 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 ->nCell );. ass
159be 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
159bf 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a Mutex(pCur) );..
159c0 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 getCellInfo(pC
159c1 75 72 29 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 ur);. aPayload
159c2 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 65 = pCur->info.pCe
159c3 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 6e 66 6f 2e ll + pCur->info.
159c4 6e 48 65 61 64 65 72 3b 0a 20 20 6e 4b 65 79 20 nHeader;. nKey
159c5 3d 20 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 = (pPage->intKey
159c6 20 3f 20 30 20 3a 20 28 69 6e 74 29 70 43 75 72 ? 0 : (int)pCur
159c7 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 0a 20 ->info.nKey);..
159c8 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a if( skipKey ){.
159c9 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 6e 4b offset += nK
159ca 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f 66 ey;. }. if( of
159cb 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 79 2b fset+amt > nKey+
159cc 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 pCur->info.nData
159cd 20 0a 20 20 20 7c 7c 20 26 61 50 61 79 6c 6f 61 . || &aPayloa
159ce 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f d[pCur->info.nLo
159cf 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d 3e 61 cal] > &pPage->a
159d0 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 Data[pBt->usable
159d1 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 20 20 20 2f Size]. ){. /
159d2 2a 20 54 72 79 69 6e 67 20 74 6f 20 72 65 61 64 * Trying to read
159d3 20 6f 72 20 77 72 69 74 65 20 70 61 73 74 20 74 or write past t
159d4 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61 he end of the da
159d5 74 61 20 69 73 20 61 6e 20 65 72 72 6f 72 20 2a ta is an error *
159d6 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c /. return SQL
159d7 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
159d8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 ;. }.. /* Chec
159d9 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74 20 62 k if data must b
159da 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 74 e read/written t
159db 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 o/from the btree
159dc 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20 2a 2f page itself. */
159dd 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70 43 . if( offset<pC
159de 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 ur->info.nLocal
159df 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 61 ){. int a = a
159e0 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b 6f 66 mt;. if( a+of
159e1 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e fset>pCur->info.
159e2 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 nLocal ){.
159e3 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e a = pCur->info.n
159e4 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 3b 0a Local - offset;.
159e5 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 }. rc = c
159e6 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 opyPayload(&aPay
159e7 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 70 42 load[offset], pB
159e8 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 61 67 uf, a, eOp, pPag
159e9 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
159ea 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 offset = 0;.
159eb 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 pBuf += a;.
159ec 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73 amt -= a;. }els
159ed 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2d 3d e{. offset -=
159ee 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 pCur->info.nLoc
159ef 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 al;. }.. if( r
159f0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
159f1 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63 6f 6e amt>0 ){. con
159f2 73 74 20 75 33 32 20 6f 76 66 6c 53 69 7a 65 20 st u32 ovflSize
159f3 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
159f4 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74 65 73 e - 4; /* Bytes
159f5 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f 76 66 content per ovf
159f6 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 50 67 l page */. Pg
159f7 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a 20 20 no nextPage;..
159f8 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74 nextPage = get
159f9 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 64 5b 4byte(&aPayload[
159fa 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
159fb 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 l]);..#ifndef SQ
159fc 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
159fd 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 OB. /* If the
159fe 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c isIncrblobHandl
159ff 65 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e e flag is set an
15a00 64 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 61 d the BtCursor.a
15a01 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20 20 2a Overflow[]. *
15a02 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 * has not been a
15a03 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f 63 61 llocated, alloca
15a04 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 61 te it now. The a
15a05 72 72 61 79 20 69 73 20 73 69 7a 65 64 20 61 74 rray is sized at
15a06 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e 74 72 . ** one entr
15a07 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 72 66 y for each overf
15a08 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 low page in the
15a09 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 overflow chain.
15a0a 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 The. ** page
15a0b 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 69 number of the fi
15a0c 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 rst overflow pag
15a0d 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 e is stored in a
15a0e 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20 20 20 Overflow[0],.
15a0f 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c 75 65 ** etc. A value
15a10 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 4f 76 of 0 in the aOv
15a11 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20 6d erflow[] array m
15a12 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20 6b 6e eans "not yet kn
15a13 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74 68 65 own". ** (the
15a14 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 6c 79 cache is lazily
15a15 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 20 20 populated)..
15a16 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 */. if( pCur
15a17 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 ->isIncrblobHand
15a18 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 le && !pCur->aOv
15a19 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 erflow ){.
15a1a 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70 43 75 int nOvfl = (pCu
15a1b 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 r->info.nPayload
15a1c 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 -pCur->info.nLoc
15a1d 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29 2f 6f al+ovflSize-1)/o
15a1e 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 vflSize;. p
15a1f 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d Cur->aOverflow =
15a20 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74 65 33 (Pgno *)sqlite3
15a21 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f MallocZero(sizeo
15a22 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29 3b 0a f(Pgno)*nOvfl);.
15a23 20 20 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 if( nOvfl
15a24 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 && !pCur->aOverf
15a25 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 low ){. r
15a26 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
15a27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
15a28 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f . /* If the o
15a29 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 verflow page-lis
15a2a 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 6e t cache has been
15a2b 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 allocated and t
15a2c 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 he. ** entry
15a2d 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 72 65 for the first re
15a2e 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f 77 20 quired overflow
15a2f 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c 20 73 page is valid, s
15a30 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 kip. ** direc
15a31 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a tly to it.. *
15a32 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e /. if( pCur->
15a33 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 75 aOverflow && pCu
15a34 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 r->aOverflow[off
15a35 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20 29 7b set/ovflSize] ){
15a36 0a 20 20 20 20 20 20 69 49 64 78 20 3d 20 28 6f . iIdx = (o
15a37 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b ffset/ovflSize);
15a38 0a 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 . nextPage
15a39 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f = pCur->aOverflo
15a3a 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f w[iIdx];. o
15a3b 66 66 73 65 74 20 3d 20 28 6f 66 66 73 65 74 25 ffset = (offset%
15a3c 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d ovflSize);. }
15a3d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 66 6f 72 .#endif.. for
15a3e 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f ( ; rc==SQLITE_O
15a3f 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 20 6e 65 K && amt>0 && ne
15a40 78 74 50 61 67 65 3b 20 69 49 64 78 2b 2b 29 7b xtPage; iIdx++){
15a41 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
15a42 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 _OMIT_INCRBLOB.
15a43 20 20 20 20 20 2f 2a 20 49 66 20 72 65 71 75 69 /* If requi
15a44 72 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 red, populate th
15a45 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d e overflow page-
15a46 6c 69 73 74 20 63 61 63 68 65 2e 20 2a 2f 0a 20 list cache. */.
15a47 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 if( pCur->a
15a48 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 Overflow ){.
15a49 20 20 20 20 61 73 73 65 72 74 28 21 70 43 75 72 assert(!pCur
15a4a 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
15a4b 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72 ] || pCur->aOver
15a4c 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 flow[iIdx]==next
15a4d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70 Page);. p
15a4e 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
15a4f 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b Idx] = nextPage;
15a50 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
15a51 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66 73 65 . if( offse
15a52 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 t>=ovflSize ){.
15a53 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e /* The on
15a54 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72 65 61 ly reason to rea
15a55 64 20 74 68 69 73 20 70 61 67 65 20 69 73 20 74 d this page is t
15a56 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70 61 67 o obtain the pag
15a57 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d e. ** num
15a58 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 78 74 ber for the next
15a59 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 page in the ove
15a5a 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 rflow chain. The
15a5b 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a page. **
15a5c 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72 65 71 data is not req
15a5d 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73 74 20 uired. So first
15a5e 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 try to lookup th
15a5f 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 20 e overflow.
15a60 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73 74 20 ** page-list
15a61 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c 20 74 cache, if any, t
15a62 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f hen fall back to
15a63 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c 6f 77 the getOverflow
15a64 50 61 67 65 28 29 0a 20 20 20 20 20 20 20 20 2a Page(). *
15a65 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 * function..
15a66 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 */.#ifndef S
15a67 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
15a68 4c 4f 42 0a 20 20 20 20 20 20 20 20 69 66 28 20 LOB. if(
15a69 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 pCur->aOverflow
15a6a 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c && pCur->aOverfl
15a6b 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a 20 20 ow[iIdx+1] ){.
15a6c 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 nextPage
15a6d 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c = pCur->aOverfl
15a6e 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20 20 20 ow[iIdx+1];.
15a6f 20 20 20 20 7d 20 65 6c 73 65 20 0a 23 65 6e 64 } else .#end
15a70 69 66 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 if. rc
15a71 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 = getOverflowPag
15a72 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67 65 2c e(pBt, nextPage,
15a73 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29 3b 0a 0, &nextPage);.
15a74 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2d offset -
15a75 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 = ovflSize;.
15a76 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
15a77 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65 61 64 /* Need to read
15a78 20 74 68 69 73 20 70 61 67 65 20 70 72 6f 70 65 this page prope
15a79 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69 6e 73 rly. It contains
15a7a 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20 20 20 some of the.
15a7b 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 ** range of
15a7c 20 64 61 74 61 20 74 68 61 74 20 69 73 20 62 65 data that is be
15a7d 69 6e 67 20 72 65 61 64 20 28 65 4f 70 3d 3d 30 ing read (eOp==0
15a7e 29 20 6f 72 20 77 72 69 74 74 65 6e 20 28 65 4f ) or written (eO
15a7f 70 21 3d 30 29 2e 0a 20 20 20 20 20 20 20 20 2a p!=0).. *
15a80 2f 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 /. DbPage
15a81 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 20 20 *pDbPage;.
15a82 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a int a = amt;.
15a83 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
15a84 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 ite3PagerGet(pBt
15a85 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 74 50 61 ->pPager, nextPa
15a86 67 65 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 ge, &pDbPage);.
15a87 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
15a88 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
15a89 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d aPayload =
15a8a 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
15a8b 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 Data(pDbPage);.
15a8c 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 nextPag
15a8d 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 50 61 e = get4byte(aPa
15a8e 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20 20 yload);.
15a8f 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73 65 74 if( a + offset
15a90 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 > ovflSize ){.
15a91 20 20 20 20 20 20 20 20 20 20 20 61 20 3d 20 6f a = o
15a92 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73 65 74 vflSize - offset
15a93 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
15a94 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f 70 rc = cop
15a95 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f yPayload(&aPaylo
15a96 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42 ad[offset+4], pB
15a97 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44 62 50 uf, a, eOp, pDbP
15a98 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
15a99 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
15a9a 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 f(pDbPage);.
15a9b 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 offset = 0
15a9c 3b 0a 20 20 20 20 20 20 20 20 20 20 61 6d 74 20 ;. amt
15a9d 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 20 20 -= a;.
15a9e 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 20 pBuf += a;.
15a9f 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
15aa0 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 }. }.. if( rc
15aa1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 ==SQLITE_OK && a
15aa2 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 mt>0 ){. retu
15aa3 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
15aa4 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 T_BKPT;. }. re
15aa5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
15aa6 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74 * Read part of t
15aa7 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61 74 65 he key associate
15aa8 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 d with cursor pC
15aa9 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 ur. Exactly.**
15aaa 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c "amt" bytes will
15aab 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69 be transfered i
15aac 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 nto pBuf[]. The
15aad 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 transfer.** beg
15aae 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e ins at "offset".
15aaf 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
15ab0 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
15ab1 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 ss or an error c
15ab2 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 ode if anything
15ab3 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 goes.** wrong.
15ab4 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 An error is retu
15ab5 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b rned if "offset+
15ab6 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 amt" is larger t
15ab7 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c han.** the avail
15ab8 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f able payload..*/
15ab9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
15aba 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
15abb 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 Key(BtCursor *pC
15abc 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 ur, u32 offset,
15abd 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 u32 amt, void *p
15abe 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Buf){. int rc;.
15abf 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
15ac0 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
15ac1 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 ) );. rc = rest
15ac2 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
15ac3 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 n(pCur);. if( r
15ac4 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
15ac5 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
15ac6 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
15ac7 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 61 73 _VALID );. as
15ac8 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 sert( pCur->iPag
15ac9 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 e>=0 && pCur->ap
15aca 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
15acb 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 ] );. if( pCu
15acc 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
15acd 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 65 tKey ){. re
15ace 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
15acf 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a UPT_BKPT;. }.
15ad0 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
15ad1 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
15ad2 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 age]<pCur->apPag
15ad3 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
15ad4 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 nCell );. rc
15ad5 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 = accessPayload(
15ad6 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d pCur, offset, am
15ad7 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 t, (unsigned cha
15ad8 72 2a 29 70 42 75 66 2c 20 30 2c 20 30 29 3b 0a r*)pBuf, 0, 0);.
15ad9 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
15ada 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 .}../*.** Read p
15adb 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 art of the data
15adc 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
15add 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78 cursor pCur. Ex
15ade 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62 actly.** "amt" b
15adf 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61 ytes will be tra
15ae0 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75 nsfered into pBu
15ae1 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66 f[]. The transf
15ae2 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20 er.** begins at
15ae3 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 "offset"..**.**
15ae4 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
15ae5 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 on success or a
15ae6 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 n error code if
15ae7 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a anything goes.**
15ae8 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f wrong. An erro
15ae9 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 r is returned if
15aea 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 73 "offset+amt" is
15aeb 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a 20 larger than.**
15aec 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 70 61 the available pa
15aed 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 yload..*/.SQLITE
15aee 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
15aef 69 74 65 33 42 74 72 65 65 44 61 74 61 28 42 74 ite3BtreeData(Bt
15af0 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 Cursor *pCur, u3
15af1 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 2 offset, u32 am
15af2 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a t, void *pBuf){.
15af3 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 6e 64 int rc;..#ifnd
15af4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
15af5 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 20 70 NCRBLOB. if ( p
15af6 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
15af7 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 SOR_INVALID ){.
15af8 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15af9 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 6e 64 _ABORT;. }.#end
15afa 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 if.. assert( cu
15afb 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
15afc 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 Cur) );. rc = r
15afd 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
15afe 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 tion(pCur);. if
15aff 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
15b00 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
15b01 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
15b02 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 SOR_VALID );.
15b03 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 assert( pCur->i
15b04 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d Page>=0 && pCur-
15b05 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
15b06 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 73 65 age] );. asse
15b07 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
15b08 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 pCur->iPage]<pCu
15b09 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
15b0a 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b iPage]->nCell );
15b0b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73 . rc = access
15b0c 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 Payload(pCur, of
15b0d 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 66 2c fset, amt, pBuf,
15b0e 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 1, 0);. }. re
15b0f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
15b10 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
15b11 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e er to payload in
15b12 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 formation from t
15b13 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 he entry that th
15b14 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f e .** pCur curso
15b15 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
15b16 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 . The pointer i
15b17 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 s to the beginni
15b18 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 ng of.** the key
15b19 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61 if skipKey==0 a
15b1a 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 nd it points to
15b1b 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
15b1c 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70 data if.** skip
15b1d 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d Key==1. The num
15b1e 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
15b1f 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61 available key/da
15b20 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a ta is written.**
15b21 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66 into *pAmt. If
15b22 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 *pAmt==0, then
15b23 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
15b24 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a ed will not be.*
15b25 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 * a valid pointe
15b26 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f r..**.** This ro
15b27 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69 utine is an opti
15b28 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73 mization. It is
15b29 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20 common for the
15b2a 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e entire key.** an
15b2b 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e d data to fit on
15b2c 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20 the local page
15b2d 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f and for there to
15b2e 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a be no overflow.
15b2f 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20 ** pages. When
15b30 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73 that is so, this
15b31 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 routine can be
15b32 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 used to access t
15b33 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 he.** key and da
15b34 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e ta without makin
15b35 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68 g a copy. If th
15b36 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74 e key and/or dat
15b37 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f a spills.** onto
15b38 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c overflow pages,
15b39 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c then accessPayl
15b3a 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 73 oad() must be us
15b3b 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 79 ed to reassembly
15b3c 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61 .** the key/data
15b3d 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74 and copy it int
15b3e 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 o a preallocated
15b3f 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 buffer..**.** T
15b40 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 he pointer retur
15b41 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 ned by this rout
15b42 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74 ine looks direct
15b43 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 ly into the cach
15b44 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68 ed.** page of th
15b45 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 e database. The
15b46 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e data might chan
15b47 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e ge or move the n
15b48 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 ext time.** any
15b49 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73 btree routine is
15b4a 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 called..*/.stat
15b4b 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 ic const unsigne
15b4c 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 61 79 d char *fetchPay
15b4d 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 load(. BtCursor
15b4e 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 *pCur, /*
15b4f 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 Cursor pointing
15b50 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 to entry to read
15b51 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a from */. int *
15b52 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 pAmt,
15b53 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d /* Write the num
15b54 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 ber of available
15b55 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20 bytes here */.
15b56 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20 int skipKey
15b57 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65 /* read be
15b58 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20 ginning at data
15b59 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65 20 if this is true
15b5a 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 */.){. unsigned
15b5b 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b char *aPayload;
15b5c 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
15b5d 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 e;. u32 nKey;.
15b5e 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20 u32 nLocal;..
15b5f 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 20 assert( pCur!=0
15b60 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d && pCur->iPage>=
15b61 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 0 && pCur->apPag
15b62 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b e[pCur->iPage]);
15b63 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
15b64 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
15b65 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 VALID );. asser
15b66 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
15b67 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70 tex(pCur) );. p
15b68 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
15b69 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
15b6a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
15b6b 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
15b6c 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c age]<pPage->nCel
15b6d 6c 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e l );. getCellIn
15b6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 fo(pCur);. aPay
15b6f 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 load = pCur->inf
15b70 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c o.pCell;. aPayl
15b71 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 oad += pCur->inf
15b72 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69 66 28 o.nHeader;. if(
15b73 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 pPage->intKey )
15b74 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a {. nKey = 0;.
15b75 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 }else{. nKe
15b76 79 20 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 y = (int)pCur->i
15b77 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 nfo.nKey;. }.
15b78 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a 20 if( skipKey ){.
15b79 20 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e aPayload += n
15b7a 4b 65 79 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 Key;. nLocal
15b7b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f = pCur->info.nLo
15b7c 63 61 6c 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 cal - nKey;. }e
15b7d 6c 73 65 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 lse{. nLocal
15b7e 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f = pCur->info.nLo
15b7f 63 61 6c 3b 0a 20 20 20 20 69 66 28 20 6e 4c 6f cal;. if( nLo
15b80 63 61 6c 3e 6e 4b 65 79 20 29 7b 0a 20 20 20 20 cal>nKey ){.
15b81 20 20 6e 4c 6f 63 61 6c 20 3d 20 6e 4b 65 79 3b nLocal = nKey;
15b82 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 41 . }. }. *pA
15b83 6d 74 20 3d 20 6e 4c 6f 63 61 6c 3b 0a 20 20 72 mt = nLocal;. r
15b84 65 74 75 72 6e 20 61 50 61 79 6c 6f 61 64 3b 0a eturn aPayload;.
15b85 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 }.../*.** For th
15b86 65 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 e entry that cur
15b87 73 6f 72 20 70 43 75 72 20 69 73 20 70 6f 69 6e sor pCur is poin
15b88 74 20 74 6f 2c 20 72 65 74 75 72 6e 20 61 73 0a t to, return as.
15b89 2a 2a 20 6d 61 6e 79 20 62 79 74 65 73 20 6f 66 ** many bytes of
15b8a 20 74 68 65 20 6b 65 79 20 6f 72 20 64 61 74 61 the key or data
15b8b 20 61 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c as are availabl
15b8c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 0a 2a e on the local.*
15b8d 2a 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 20 * b-tree page.
15b8e 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 Write the number
15b8f 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 79 of available by
15b90 74 65 73 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 0a tes into *pAmt..
15b91 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 **.** The pointe
15b92 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 65 70 r returned is ep
15b93 68 65 6d 65 72 61 6c 2e 20 20 54 68 65 20 6b 65 hemeral. The ke
15b94 79 2f 64 61 74 61 20 6d 61 79 20 6d 6f 76 65 0a y/data may move.
15b95 2a 2a 20 6f 72 20 62 65 20 64 65 73 74 72 6f 79 ** or be destroy
15b96 65 64 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 63 ed on the next c
15b97 61 6c 6c 20 74 6f 20 61 6e 79 20 42 74 72 65 65 all to any Btree
15b98 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 69 6e 63 routine,.** inc
15b99 6c 75 64 69 6e 67 20 63 61 6c 6c 73 20 66 72 6f luding calls fro
15b9a 6d 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 m other threads
15b9b 61 67 61 69 6e 73 74 20 74 68 65 20 73 61 6d 65 against the same
15b9c 20 63 61 63 68 65 2e 0a 2a 2a 20 48 65 6e 63 65 cache..** Hence
15b9d 2c 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 , a mutex on the
15b9e 20 42 74 53 68 61 72 65 64 20 73 68 6f 75 6c 64 BtShared should
15b9f 20 62 65 20 68 65 6c 64 20 70 72 69 6f 72 20 74 be held prior t
15ba0 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 o calling.** thi
15ba1 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a s routine..**.**
15ba2 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
15ba3 69 73 20 75 73 65 64 20 74 6f 20 67 65 74 20 71 is used to get q
15ba4 75 69 63 6b 20 61 63 63 65 73 73 20 74 6f 20 6b uick access to k
15ba5 65 79 20 61 6e 64 20 64 61 74 61 0a 2a 2a 20 69 ey and data.** i
15ba6 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 n the common cas
15ba7 65 20 77 68 65 72 65 20 6e 6f 20 6f 76 65 72 66 e where no overf
15ba8 6c 6f 77 20 70 61 67 65 73 20 61 72 65 20 75 73 low pages are us
15ba9 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
15baa 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 IVATE const void
15bab 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 *sqlite3BtreeKe
15bac 79 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 20 yFetch(BtCursor
15bad 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74 *pCur, int *pAmt
15bae 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ){. assert( cur
15baf 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
15bb0 75 72 29 20 29 3b 0a 20 20 69 66 28 20 70 43 75 ur) );. if( pCu
15bb1 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
15bb2 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 R_VALID ){. r
15bb3 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 76 6f 69 eturn (const voi
15bb4 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28 d*)fetchPayload(
15bb5 70 43 75 72 2c 20 70 41 6d 74 2c 20 30 29 3b 0a pCur, pAmt, 0);.
15bb6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
15bb7 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
15bb8 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
15bb9 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74 ite3BtreeDataFet
15bba 63 68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ch(BtCursor *pCu
15bbb 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 r, int *pAmt){.
15bbc 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
15bbd 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
15bbe 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 );. if( pCur->e
15bbf 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
15bc0 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 LID ){. retur
15bc1 6e 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 n (const void*)f
15bc2 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 etchPayload(pCur
15bc3 2c 20 70 41 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a , pAmt, 1);. }.
15bc4 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a return 0;.}...
15bc5 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 /*.** Move the c
15bc6 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20 ursor down to a
15bc7 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 2e 20 new child page.
15bc8 20 54 68 65 20 6e 65 77 50 67 6e 6f 20 61 72 67 The newPgno arg
15bc9 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 ument is the.**
15bca 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 page number of t
15bcb 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 74 6f he child page to
15bcc 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2f 0a 73 74 61 move to..*/.sta
15bcd 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 43 68 tic int moveToCh
15bce 69 6c 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ild(BtCursor *pC
15bcf 75 72 2c 20 75 33 32 20 6e 65 77 50 67 6e 6f 29 ur, u32 newPgno)
15bd0 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e {. int rc;. in
15bd1 74 20 69 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 t i = pCur->iPag
15bd2 65 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e e;. MemPage *pN
15bd3 65 77 50 61 67 65 3b 0a 20 20 42 74 53 68 61 72 ewPage;. BtShar
15bd4 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e ed *pBt = pCur->
15bd5 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 pBt;.. assert(
15bd6 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
15bd7 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
15bd8 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
15bd9 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
15bda 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
15bdb 2d 3e 69 50 61 67 65 3c 42 54 43 55 52 53 4f 52 ->iPage<BTCURSOR
15bdc 5f 4d 41 58 5f 44 45 50 54 48 20 29 3b 0a 20 20 _MAX_DEPTH );.
15bdd 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e if( pCur->iPage>
15bde 3d 28 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 =(BTCURSOR_MAX_D
15bdf 45 50 54 48 2d 31 29 20 29 7b 0a 20 20 20 20 72 EPTH-1) ){. r
15be0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15be1 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 RUPT_BKPT;. }.
15be2 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 rc = getAndInit
15be3 50 61 67 65 28 70 42 74 2c 20 6e 65 77 50 67 6e Page(pBt, newPgn
15be4 6f 2c 20 26 70 4e 65 77 50 61 67 65 29 3b 0a 20 o, &pNewPage);.
15be5 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
15be6 20 72 63 3b 0a 20 20 70 43 75 72 2d 3e 61 70 50 rc;. pCur->apP
15be7 61 67 65 5b 69 2b 31 5d 20 3d 20 70 4e 65 77 50 age[i+1] = pNewP
15be8 61 67 65 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49 age;. pCur->aiI
15be9 64 78 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 dx[i+1] = 0;. p
15bea 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a 0a 20 Cur->iPage++;..
15beb 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
15bec 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 e = 0;. pCur->v
15bed 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 alidNKey = 0;.
15bee 69 66 28 20 70 4e 65 77 50 61 67 65 2d 3e 6e 43 if( pNewPage->nC
15bef 65 6c 6c 3c 31 20 29 7b 0a 20 20 20 20 72 65 74 ell<1 ){. ret
15bf0 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
15bf1 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 PT_BKPT;. }. r
15bf2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
15bf3 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 .}..#ifndef NDEB
15bf4 55 47 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 70 50 UG./*.** Page pP
15bf5 61 72 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 arent is an inte
15bf6 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 61 66 29 20 rnal (non-leaf)
15bf7 74 72 65 65 20 70 61 67 65 2e 20 54 68 69 73 20 tree page. This
15bf8 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 61 73 73 function .** ass
15bf9 65 72 74 73 20 74 68 61 74 20 70 61 67 65 20 6e erts that page n
15bfa 75 6d 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 umber iChild is
15bfb 74 68 65 20 6c 65 66 74 2d 63 68 69 6c 64 20 69 the left-child i
15bfc 66 20 74 68 65 20 69 49 64 78 27 74 68 0a 2a 2a f the iIdx'th.**
15bfd 20 63 65 6c 6c 20 69 6e 20 70 61 67 65 20 70 50 cell in page pP
15bfe 61 72 65 6e 74 2e 20 4f 72 2c 20 69 66 20 69 49 arent. Or, if iI
15bff 64 78 20 69 73 20 65 71 75 61 6c 20 74 6f 20 74 dx is equal to t
15c00 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
15c01 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 69 6e 20 70 of.** cells in p
15c02 50 61 72 65 6e 74 2c 20 74 68 61 74 20 70 61 67 Parent, that pag
15c03 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 20 e number iChild
15c04 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 is the right-chi
15c05 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 20 70 61 67 ld of.** the pag
15c06 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
15c07 64 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e d assertParentIn
15c08 64 65 78 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 dex(MemPage *pPa
15c09 72 65 6e 74 2c 20 69 6e 74 20 69 49 64 78 2c 20 rent, int iIdx,
15c0a 50 67 6e 6f 20 69 43 68 69 6c 64 29 7b 0a 20 20 Pgno iChild){.
15c0b 61 73 73 65 72 74 28 20 69 49 64 78 3c 3d 70 50 assert( iIdx<=pP
15c0c 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a arent->nCell );.
15c0d 20 20 69 66 28 20 69 49 64 78 3d 3d 70 50 61 72 if( iIdx==pPar
15c0e 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 ent->nCell ){.
15c0f 20 20 61 73 73 65 72 74 28 20 67 65 74 34 62 79 assert( get4by
15c10 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 te(&pParent->aDa
15c11 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f ta[pParent->hdrO
15c12 66 66 73 65 74 2b 38 5d 29 3d 3d 69 43 68 69 6c ffset+8])==iChil
15c13 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 d );. }else{.
15c14 20 20 61 73 73 65 72 74 28 20 67 65 74 34 62 79 assert( get4by
15c15 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 te(findCell(pPar
15c16 65 6e 74 2c 20 69 49 64 78 29 29 3d 3d 69 43 68 ent, iIdx))==iCh
15c17 69 6c 64 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c ild );. }.}.#el
15c18 73 65 0a 23 20 20 64 65 66 69 6e 65 20 61 73 73 se.# define ass
15c19 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 78 ertParentIndex(x
15c1a 2c 79 2c 7a 29 20 0a 23 65 6e 64 69 66 0a 0a 2f ,y,z) .#endif../
15c1b 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 *.** Move the cu
15c1c 72 73 6f 72 20 75 70 20 74 6f 20 74 68 65 20 70 rsor up to the p
15c1d 61 72 65 6e 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a arent page..**.*
15c1e 2a 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 * pCur->idx is s
15c1f 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 et to the cell i
15c20 6e 64 65 78 20 74 68 61 74 20 63 6f 6e 74 61 69 ndex that contai
15c21 6e 73 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a ns the pointer.*
15c22 2a 20 74 6f 20 74 68 65 20 70 61 67 65 20 77 65 * to the page we
15c23 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d are coming from
15c24 2e 20 20 49 66 20 77 65 20 61 72 65 20 63 6f 6d . If we are com
15c25 69 6e 67 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 ing from the.**
15c26 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64 right-most child
15c27 20 70 61 67 65 20 74 68 65 6e 20 70 43 75 72 2d page then pCur-
15c28 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f 20 6f >idx is set to o
15c29 6e 65 20 6d 6f 72 65 20 74 68 61 6e 0a 2a 2a 20 ne more than.**
15c2a 74 68 65 20 6c 61 72 67 65 73 74 20 63 65 6c 6c the largest cell
15c2b 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 index..*/.SQLIT
15c2c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
15c2d 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 54 qlite3BtreeMoveT
15c2e 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73 6f 72 oParent(BtCursor
15c2f 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 *pCur){. asser
15c30 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
15c31 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 tex(pCur) );. a
15c32 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
15c33 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
15c34 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 D );. assert( p
15c35 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a Cur->iPage>0 );.
15c36 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
15c37 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
15c38 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 50 ge] );. assertP
15c39 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 20 20 20 arentIndex(.
15c3a 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
15c3b 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 r->iPage-1], .
15c3c 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
15c3d 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 ur->iPage-1], .
15c3e 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b pCur->apPage[
15c3f 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 67 pCur->iPage]->pg
15c40 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c 65 61 73 no. );. releas
15c41 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 ePage(pCur->apPa
15c42 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 ge[pCur->iPage])
15c43 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d ;. pCur->iPage-
15c44 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e -;. pCur->info.
15c45 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 nSize = 0;. pCu
15c46 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 r->validNKey = 0
15c47 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 ;.}../*.** Move
15c48 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
15c49 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2f 0a 73 e root page.*/.s
15c4a 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f tatic int moveTo
15c4b 52 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 Root(BtCursor *p
15c4c 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 Cur){. MemPage
15c4d 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 *pRoot;. int rc
15c4e 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
15c4f 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d Btree *p = pCur-
15c50 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 >pBtree;. BtSha
15c51 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
15c52 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 t;.. assert( cu
15c53 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
15c54 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
15c55 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ( CURSOR_INVALID
15c56 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 < CURSOR_REQUIR
15c57 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 ESEEK );. asser
15c58 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 t( CURSOR_VALID
15c59 20 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 < CURSOR_REQUI
15c5a 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 RESEEK );. asse
15c5b 72 74 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 rt( CURSOR_FAULT
15c5c 20 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 > CURSOR_REQU
15c5d 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 IRESEEK );. if(
15c5e 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 pCur->eState>=C
15c5f 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
15c60 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 K ){. if( pCu
15c61 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
15c62 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 R_FAULT ){.
15c63 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b return pCur->sk
15c64 69 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 ip;. }. sq
15c65 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 lite3BtreeClearC
15c66 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d ursor(pCur);. }
15c67 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 .. if( pCur->iP
15c68 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e age>=0 ){. in
15c69 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 t i;. for(i=1
15c6a 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 ; i<=pCur->iPage
15c6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 ; i++){. re
15c6c 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e leasePage(pCur->
15c6d 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 apPage[i]);.
15c6e 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 }. }else{. i
15c6f 66 28 20 0a 20 20 20 20 20 20 53 51 4c 49 54 45 f( . SQLITE
15c70 5f 4f 4b 21 3d 28 72 63 20 3d 20 67 65 74 41 6e _OK!=(rc = getAn
15c71 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 dInitPage(pBt, p
15c72 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 Cur->pgnoRoot, &
15c73 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 pCur->apPage[0])
15c74 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70 ). ){. p
15c75 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 Cur->eState = CU
15c76 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 RSOR_INVALID;.
15c77 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
15c78 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 52 6f 6f }. }.. pRoo
15c79 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 t = pCur->apPage
15c7a 5b 30 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 [0];. assert( p
15c7b 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72 Root->pgno==pCur
15c7c 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b 0a 20 20 ->pgnoRoot );.
15c7d 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 30 3b pCur->iPage = 0;
15c7e 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 . pCur->aiIdx[0
15c7f 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 ] = 0;. pCur->i
15c80 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 nfo.nSize = 0;.
15c81 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 pCur->atLast =
15c82 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 0;. pCur->valid
15c83 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 69 66 28 NKey = 0;.. if(
15c84 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 pRoot->nCell==0
15c85 20 26 26 20 21 70 52 6f 6f 74 2d 3e 6c 65 61 66 && !pRoot->leaf
15c86 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73 75 62 ){. Pgno sub
15c87 70 61 67 65 3b 0a 20 20 20 20 61 73 73 65 72 74 page;. assert
15c88 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 31 ( pRoot->pgno==1
15c89 20 29 3b 0a 20 20 20 20 73 75 62 70 61 67 65 20 );. subpage
15c8a 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 6f 6f = get4byte(&pRoo
15c8b 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e t->aData[pRoot->
15c8c 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 hdrOffset+8]);.
15c8d 20 20 20 61 73 73 65 72 74 28 20 73 75 62 70 61 assert( subpa
15c8e 67 65 3e 30 20 29 3b 0a 20 20 20 20 70 43 75 72 ge>0 );. pCur
15c8f 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
15c90 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72 63 20 R_VALID;. rc
15c91 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
15c92 75 72 2c 20 73 75 62 70 61 67 65 29 3b 0a 20 20 ur, subpage);.
15c93 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 75 72 2d }else{. pCur-
15c94 3e 65 53 74 61 74 65 20 3d 20 28 28 70 52 6f 6f >eState = ((pRoo
15c95 74 2d 3e 6e 43 65 6c 6c 3e 30 29 3f 43 55 52 53 t->nCell>0)?CURS
15c96 4f 52 5f 56 41 4c 49 44 3a 43 55 52 53 4f 52 5f OR_VALID:CURSOR_
15c97 49 4e 56 41 4c 49 44 29 3b 0a 20 20 7d 0a 20 20 INVALID);. }.
15c98 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
15c99 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 .** Move the cur
15c9a 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 sor down to the
15c9b 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 left-most leaf e
15c9c 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
15c9d 0a 2a 2a 20 65 6e 74 72 79 20 74 6f 20 77 68 69 .** entry to whi
15c9e 63 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 ch it is current
15c9f 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a ly pointing..**.
15ca0 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 ** The left-most
15ca1 20 6c 65 61 66 20 69 73 20 74 68 65 20 6f 6e 65 leaf is the one
15ca2 20 77 69 74 68 20 74 68 65 20 73 6d 61 6c 6c 65 with the smalle
15ca3 73 74 20 6b 65 79 20 2d 20 74 68 65 20 66 69 72 st key - the fir
15ca4 73 74 0a 2a 2a 20 69 6e 20 61 73 63 65 6e 64 69 st.** in ascendi
15ca5 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 ng order..*/.sta
15ca6 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 4c 65 tic int moveToLe
15ca7 66 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72 20 ftmost(BtCursor
15ca8 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 *pCur){. Pgno p
15ca9 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 gno;. int rc =
15caa 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d SQLITE_OK;. Mem
15cab 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 Page *pPage;..
15cac 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
15cad 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
15cae 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
15caf 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
15cb0 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c _VALID );. whil
15cb1 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b e( rc==SQLITE_OK
15cb2 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 70 43 && !(pPage = pC
15cb3 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
15cb4 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29 >iPage])->leaf )
15cb5 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 {. assert( pC
15cb6 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
15cb7 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 iPage]<pPage->nC
15cb8 65 6c 6c 20 29 3b 0a 20 20 20 20 70 67 6e 6f 20 ell );. pgno
15cb9 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 = get4byte(findC
15cba 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 72 2d ell(pPage, pCur-
15cbb 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
15cbc 67 65 5d 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 ge]));. rc =
15cbd 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
15cbe 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 , pgno);. }. r
15cbf 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
15cc0 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 ** Move the curs
15cc1 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 72 or down to the r
15cc2 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 ight-most leaf e
15cc3 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
15cc4 0a 2a 2a 20 70 61 67 65 20 74 6f 20 77 68 69 63 .** page to whic
15cc5 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c h it is currentl
15cc6 79 20 70 6f 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 y pointing. Not
15cc7 69 63 65 20 74 68 65 20 64 69 66 66 65 72 65 6e ice the differen
15cc8 63 65 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6d 6f ce.** between mo
15cc9 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 20 61 veToLeftmost() a
15cca 6e 64 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f nd moveToRightmo
15ccb 73 74 28 29 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 st(). moveToLef
15ccc 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 tmost().** finds
15ccd 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 65 the left-most e
15cce 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
15ccf 20 2a 65 6e 74 72 79 2a 20 77 68 65 72 65 61 73 *entry* whereas
15cd0 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 moveToRightmost
15cd1 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 ().** finds the
15cd2 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 right-most entry
15cd3 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 70 61 beneath the *pa
15cd4 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 ge*..**.** The r
15cd5 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 ight-most entry
15cd6 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 is the one with
15cd7 74 68 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20 the largest key
15cd8 2d 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 - the last.** ke
15cd9 79 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f y in ascending o
15cda 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rder..*/.static
15cdb 69 6e 74 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d int moveToRightm
15cdc 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ost(BtCursor *pC
15cdd 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f ur){. Pgno pgno
15cde 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
15cdf 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 ITE_OK;. MemPag
15ce0 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 e *pPage = 0;..
15ce1 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
15ce2 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
15ce3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
15ce4 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
15ce5 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69 R_VALID );. whi
15ce6 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
15ce7 4b 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 70 K && !(pPage = p
15ce8 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
15ce9 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 20 ->iPage])->leaf
15cea 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 ){. pgno = ge
15ceb 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
15cec 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
15ced 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70 ffset+8]);. p
15cee 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
15cef 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d >iPage] = pPage-
15cf0 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 72 63 20 3d >nCell;. rc =
15cf1 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 moveToChild(pCu
15cf2 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 r, pgno);. }.
15cf3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
15cf4 4b 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 61 K ){. pCur->a
15cf5 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
15cf6 5d 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ] = pPage->nCell
15cf7 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e -1;. pCur->in
15cf8 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
15cf9 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
15cfa 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 y = 0;. }. ret
15cfb 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f urn rc;.}../* Mo
15cfc 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ve the cursor to
15cfd 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
15cfe 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 in the table.
15cff 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
15d00 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 .** on success.
15d01 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 Set *pRes to 0
15d02 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 if the cursor ac
15d03 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f tually points to
15d04 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 something.** or
15d05 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 set *pRes to 1
15d06 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 if the table is
15d07 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 empty..*/.SQLITE
15d08 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
15d09 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28 42 ite3BtreeFirst(B
15d0a 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 tCursor *pCur, i
15d0b 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 nt *pRes){. int
15d0c 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
15d0d 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
15d0e 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
15d0f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
15d10 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 x_held(pCur->pBt
15d11 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 ree->db->mutex)
15d12 29 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f );. rc = moveTo
15d13 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 Root(pCur);. if
15d14 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
15d15 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d ){. if( pCur-
15d16 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
15d17 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 INVALID ){.
15d18 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
15d19 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
15d1a 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a e]->nCell==0 );.
15d1b 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b *pRes = 1;
15d1c 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
15d1d 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 TE_OK;. }else
15d1e 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
15d1f 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
15d20 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
15d21 3e 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 >0 );. *pRe
15d22 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 s = 0;. rc
15d23 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 = moveToLeftmost
15d24 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 (pCur);. }.
15d25 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
15d26 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 ../* Move the cu
15d27 72 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 73 74 rsor to the last
15d28 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 entry in the ta
15d29 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c ble. Return SQL
15d2a 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 ITE_OK.** on suc
15d2b 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 cess. Set *pRes
15d2c 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 to 0 if the cur
15d2d 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 sor actually poi
15d2e 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 nts to something
15d2f 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 .** or set *pRes
15d30 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 to 1 if the tab
15d31 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a le is empty..*/.
15d32 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
15d33 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c nt sqlite3BtreeL
15d34 61 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ast(BtCursor *pC
15d35 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a ur, int *pRes){.
15d36 20 20 69 6e 74 20 72 63 3b 0a 20 0a 20 20 61 73 int rc;. . as
15d37 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
15d38 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
15d39 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15d3a 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 3_mutex_held(pCu
15d3b 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d r->pBtree->db->m
15d3c 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 utex) );. rc =
15d3d 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 moveToRoot(pCur)
15d3e 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
15d3f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
15d40 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d CURSOR_INVALID=
15d41 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b =pCur->eState ){
15d42 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
15d43 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
15d44 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d ->iPage]->nCell=
15d45 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 =0 );. *pRe
15d46 73 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 s = 1;. }else
15d47 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
15d48 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
15d49 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
15d4a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
15d4b 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f rc = moveTo
15d4c 52 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b Rightmost(pCur);
15d4d 0a 20 20 20 20 20 20 67 65 74 43 65 6c 6c 49 6e . getCellIn
15d4e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 fo(pCur);.
15d4f 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 72 pCur->atLast = r
15d50 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 31 3a c==SQLITE_OK ?1:
15d51 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 0;. }. }. r
15d52 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 eturn rc;.}../*
15d53 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
15d54 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 so that it point
15d55 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65 s to an entry ne
15d56 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a 20 73 ar the key .** s
15d57 70 65 63 69 66 69 65 64 20 62 79 20 70 49 64 78 pecified by pIdx
15d58 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20 Key or intKey.
15d59 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 Return a succes
15d5a 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f s code..**.** Fo
15d5b 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c r INTKEY tables,
15d5c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 72 61 the intKey para
15d5d 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 meter is used.
15d5e 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 74 pIdxKey .** must
15d5f 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 be NULL. For i
15d60 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 49 64 ndex tables, pId
15d61 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 6e 64 xKey is used and
15d62 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 69 67 intKey.** is ig
15d63 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 nored..**.** If
15d64 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 20 69 an exact match i
15d65 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 68 65 s not found, the
15d66 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 n the cursor is
15d67 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 20 70 always.** left p
15d68 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c 65 61 ointing at a lea
15d69 66 20 70 61 67 65 20 77 68 69 63 68 20 77 6f 75 f page which wou
15d6a 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 72 ld hold the entr
15d6b 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 65 20 y if it.** were
15d6c 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 63 75 present. The cu
15d6d 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 6e 74 rsor might point
15d6e 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 68 61 to an entry tha
15d6f 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 t comes.** befor
15d70 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 20 6b e or after the k
15d71 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 ey..**.** An int
15d72 65 67 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 eger is written
15d73 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 63 68 into *pRes which
15d74 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f is the result o
15d75 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 20 74 f.** comparing t
15d76 68 65 20 6b 65 79 20 77 69 74 68 20 74 68 65 20 he key with the
15d77 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 entry to which t
15d78 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a 2a 2a he cursor is .**
15d79 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 65 20 pointing. The
15d7a 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 20 69 meaning of the i
15d7b 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e 20 69 nteger written i
15d7c 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 73 20 nto.** *pRes is
15d7d 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
15d7e 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 20 20 * *pRes<0
15d7f 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 The cursor is
15d80 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 left pointing a
15d81 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a t an entry that.
15d82 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
15d83 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 is smaller t
15d84 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b han intKey/pIdxK
15d85 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 ey or if the tab
15d86 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 le is empty.**
15d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d88 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 69 and the cursor i
15d89 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74 s therefore left
15d8a 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e point to nothin
15d8b 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 g..**.** *pR
15d8c 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75 es==0 The cu
15d8d 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 rsor is left poi
15d8e 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 nting at an entr
15d8f 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 y that.**
15d90 20 20 20 20 20 20 20 20 20 20 20 65 78 61 63 74 exact
15d91 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74 4b 65 ly matches intKe
15d92 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a y/pIdxKey..**.**
15d93 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 20 20 *pRes>0
15d94 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 The cursor is
15d95 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
15d96 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a an entry that.*
15d97 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
15d98 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 is larger tha
15d99 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 n intKey/pIdxKey
15d9a 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ..**.*/.SQLITE_P
15d9b 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15d9c 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
15d9d 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f acked(. BtCurso
15d9e 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 r *pCur,
15d9f 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 /* The cursor
15da0 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20 to be moved */.
15da1 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
15da2 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e 70 *pIdxKey, /* Unp
15da3 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 acked index key
15da4 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c */. i64 intKey,
15da5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15da6 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79 20 2a The table key *
15da7 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69 67 68 /. int biasRigh
15da8 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t, /*
15da9 49 66 20 74 72 75 65 2c 20 62 69 61 73 20 74 68 If true, bias th
15daa 65 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 e search to the
15dab 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e high end */. in
15dac 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 t *pRes
15dad 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
15dae 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 search results h
15daf 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
15db0 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 rc;.. assert( c
15db1 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
15db2 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
15db3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
15db4 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 _held(pCur->pBtr
15db5 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ee->db->mutex) )
15db6 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 ;.. /* If the c
15db7 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 ursor is already
15db8 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 74 20 74 positioned at t
15db9 68 65 20 70 6f 69 6e 74 20 77 65 20 61 72 65 20 he point we are
15dba 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 6d trying. ** to m
15dbb 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a 75 73 ove to, then jus
15dbc 74 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 t return without
15dbd 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 20 doing any work
15dbe 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 */. if( pCur->e
15dbf 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
15dc0 4c 49 44 20 26 26 20 70 43 75 72 2d 3e 76 61 6c LID && pCur->val
15dc1 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 20 70 43 idNKey . && pC
15dc2 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 ur->apPage[0]->i
15dc3 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 20 20 20 ntKey . ){.
15dc4 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e if( pCur->info.n
15dc5 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 Key==intKey ){.
15dc6 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a *pRes = 0;.
15dc7 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15dc8 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
15dc9 20 20 69 66 28 20 70 43 75 72 2d 3e 61 74 4c 61 if( pCur->atLa
15dca 73 74 20 26 26 20 70 43 75 72 2d 3e 69 6e 66 6f st && pCur->info
15dcb 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a .nKey<intKey ){.
15dcc 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 *pRes = -1
15dcd 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
15dce 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
15dcf 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 }.. rc = move
15dd0 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 ToRoot(pCur);.
15dd1 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 if( rc ){. re
15dd2 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 turn rc;. }. a
15dd3 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
15dd4 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
15dd5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
15dd6 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
15dd7 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e 69 74 20 >iPage]->isInit
15dd8 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 );. if( pCur->e
15dd9 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e State==CURSOR_IN
15dda 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 52 VALID ){. *pR
15ddb 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73 es = -1;. ass
15ddc 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 ert( pCur->apPag
15ddd 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
15dde 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 nCell==0 );.
15ddf 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
15de0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
15de1 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d pCur->apPage[0]-
15de2 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78 4b >intKey || pIdxK
15de3 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b ey );. for(;;){
15de4 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20 75 70 . int lwr, up
15de5 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68 6c 64 r;. Pgno chld
15de6 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 Pg;. MemPage
15de7 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 *pPage = pCur->a
15de8 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
15de9 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63 20 3d 20 e];. int c =
15dea 2d 31 3b 20 20 2f 2a 20 70 52 65 73 20 72 65 74 -1; /* pRes ret
15deb 75 72 6e 20 69 66 20 74 61 62 6c 65 20 69 73 20 urn if table is
15dec 65 6d 70 74 79 20 6d 75 73 74 20 62 65 20 2d 31 empty must be -1
15ded 20 2a 2f 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b */. lwr = 0;
15dee 0a 20 20 20 20 75 70 72 20 3d 20 70 50 61 67 65 . upr = pPage
15def 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 ->nCell-1;. i
15df0 66 28 20 28 21 70 50 61 67 65 2d 3e 69 6e 74 4b f( (!pPage->intK
15df1 65 79 20 26 26 20 70 49 64 78 4b 65 79 3d 3d 30 ey && pIdxKey==0
15df2 29 20 7c 7c 20 75 70 72 3c 30 20 29 7b 0a 20 20 ) || upr<0 ){.
15df3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
15df4 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
15df5 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f goto moveto_
15df6 66 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 finish;. }.
15df7 20 20 69 66 28 20 62 69 61 73 52 69 67 68 74 20 if( biasRight
15df8 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 ){. pCur->a
15df9 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
15dfa 5d 20 3d 20 28 75 31 36 29 75 70 72 3b 0a 20 20 ] = (u16)upr;.
15dfb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
15dfc 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
15dfd 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 >iPage] = (u16)(
15dfe 28 75 70 72 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 (upr+lwr)/2);.
15dff 20 20 7d 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b }. for(;;){
15e00 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 . void *pCe
15e01 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 69 36 34 llKey;. i64
15e02 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 nCellKey;.
15e03 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d int idx = pCur-
15e04 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
15e05 67 65 5d 3b 0a 20 20 20 20 20 20 70 43 75 72 2d ge];. pCur-
15e06 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b >info.nSize = 0;
15e07 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c . pCur->val
15e08 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 idNKey = 1;.
15e09 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 if( pPage->int
15e0a 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 75 Key ){. u
15e0b 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 8 *pCell;.
15e0c 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 pCell = findCe
15e0d 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b ll(pPage, idx) +
15e0e 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
15e0f 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 69 66 Size;. if
15e10 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 ( pPage->hasData
15e11 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 ){. u3
15e12 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 2 dummy;.
15e13 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 pCell += getV
15e14 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 arint32(pCell, d
15e15 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d ummy);. }
15e16 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69 . getVari
15e17 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 nt(pCell, (u64*)
15e18 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 &nCellKey);.
15e19 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 if( nCellKey
15e1a 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ==intKey ){.
15e1b 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 c = 0;.
15e1c 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e }else if( n
15e1d 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 CellKey<intKey )
15e1e 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 {. c =
15e1f 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 -1;. }els
15e20 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 e{. ass
15e21 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e ert( nCellKey>in
15e22 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 tKey );.
15e23 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 c = +1;.
15e24 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
15e25 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 76 61 . int ava
15e26 69 6c 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 ilable;.
15e27 70 43 65 6c 6c 4b 65 79 20 3d 20 28 76 6f 69 64 pCellKey = (void
15e28 20 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28 *)fetchPayload(
15e29 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 pCur, &available
15e2a 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 6e 43 , 0);. nC
15e2b 65 6c 6c 4b 65 79 20 3d 20 70 43 75 72 2d 3e 69 ellKey = pCur->i
15e2c 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 nfo.nKey;.
15e2d 20 20 69 66 28 20 61 76 61 69 6c 61 62 6c 65 3e if( available>
15e2e 3d 6e 43 65 6c 6c 4b 65 79 20 29 7b 0a 20 20 20 =nCellKey ){.
15e2f 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 c = sqlit
15e30 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 e3VdbeRecordComp
15e31 61 72 65 28 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 are((int)nCellKe
15e32 79 2c 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 y, pCellKey, pId
15e33 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d xKey);. }
15e34 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
15e35 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74 pCellKey = sqlit
15e36 65 33 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 6e e3Malloc( (int)n
15e37 43 65 6c 6c 4b 65 79 20 29 3b 0a 20 20 20 20 20 CellKey );.
15e38 20 20 20 20 20 69 66 28 20 70 43 65 6c 6c 4b 65 if( pCellKe
15e39 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 y==0 ){.
15e3a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
15e3b 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 NOMEM;.
15e3c 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 goto moveto_f
15e3d 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 20 inish;.
15e3e 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 }. rc
15e3f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 = sqlite3BtreeKe
15e40 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 y(pCur, 0, (int)
15e41 6e 43 65 6c 6c 4b 65 79 2c 20 28 76 6f 69 64 2a nCellKey, (void*
15e42 29 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 )pCellKey);.
15e43 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 c = sqlite
15e44 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 3VdbeRecordCompa
15e45 72 65 28 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 re((int)nCellKey
15e46 2c 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 , pCellKey, pIdx
15e47 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Key);.
15e48 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 sqlite3_free(pCe
15e49 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 llKey);.
15e4a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
15e4b 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
15e4c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
15e4d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 . if( c==0
15e4e 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d ){. pCur-
15e4f 3e 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 >info.nKey = nCe
15e50 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 llKey;. i
15e51 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 f( pPage->intKey
15e52 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 && !pPage->leaf
15e53 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 77 ){. lw
15e54 72 20 3d 20 69 64 78 3b 0a 20 20 20 20 20 20 20 r = idx;.
15e55 20 20 20 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 upr = lwr - 1
15e56 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
15e57 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 k;. }else
15e58 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 52 65 {. *pRe
15e59 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 s = 0;.
15e5a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
15e5b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
15e5c 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
15e5d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
15e5e 0a 20 20 20 20 20 20 69 66 28 20 63 3c 30 20 29 . if( c<0 )
15e5f 7b 0a 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 {. lwr =
15e60 69 64 78 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c idx+1;. }el
15e61 73 65 7b 0a 20 20 20 20 20 20 20 20 75 70 72 20 se{. upr
15e62 3d 20 69 64 78 2d 31 3b 0a 20 20 20 20 20 20 7d = idx-1;. }
15e63 0a 20 20 20 20 20 20 69 66 28 20 6c 77 72 3e 75 . if( lwr>u
15e64 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 pr ){. pC
15e65 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 ur->info.nKey =
15e66 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 nCellKey;.
15e67 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
15e68 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 . pCur->aiI
15e69 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 dx[pCur->iPage]
15e6a 3d 20 28 75 31 36 29 28 28 6c 77 72 2b 75 70 72 = (u16)((lwr+upr
15e6b 29 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 )/2);. }.
15e6c 61 73 73 65 72 74 28 20 6c 77 72 3d 3d 75 70 72 assert( lwr==upr
15e6d 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 +1 );. assert
15e6e 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 ( pPage->isInit
15e6f 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 );. if( pPage
15e70 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
15e71 63 68 6c 64 50 67 20 3d 20 30 3b 0a 20 20 20 20 chldPg = 0;.
15e72 7d 65 6c 73 65 20 69 66 28 20 6c 77 72 3e 3d 70 }else if( lwr>=p
15e73 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 Page->nCell ){.
15e74 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 chldPg = ge
15e75 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
15e76 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
15e77 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 7d ffset+8]);. }
15e78 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 6c 64 else{. chld
15e79 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 66 69 Pg = get4byte(fi
15e7a 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6c 77 ndCell(pPage, lw
15e7b 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 r));. }. i
15e7c 66 28 20 63 68 6c 64 50 67 3d 3d 30 20 29 7b 0a f( chldPg==0 ){.
15e7d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 assert( pC
15e7e 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
15e7f 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 iPage]<pCur->apP
15e80 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
15e81 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 20 ->nCell );.
15e82 20 69 66 28 20 70 52 65 73 20 29 20 2a 70 52 65 if( pRes ) *pRe
15e83 73 20 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 20 s = c;. rc
15e84 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
15e85 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 goto moveto_f
15e86 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 inish;. }.
15e87 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
15e88 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 r->iPage] = (u16
15e89 29 6c 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e )lwr;. pCur->
15e8a 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a info.nSize = 0;.
15e8b 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e pCur->validN
15e8c 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 Key = 0;. rc
15e8d 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
15e8e 75 72 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 ur, chldPg);.
15e8f 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d if( rc ) goto m
15e90 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 oveto_finish;.
15e91 7d 0a 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a }.moveto_finish:
15e92 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
15e93 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 76 ./*.** In this v
15e94 65 72 73 69 6f 6e 20 6f 66 20 42 74 72 65 65 4d ersion of BtreeM
15e95 6f 76 65 74 6f 2c 20 70 4b 65 79 20 69 73 20 61 oveto, pKey is a
15e96 20 70 61 63 6b 65 64 20 69 6e 64 65 78 20 72 65 packed index re
15e97 63 6f 72 64 0a 2a 2a 20 73 75 63 68 20 61 73 20 cord.** such as
15e98 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 is generated by
15e99 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 the OP_MakeRecor
15e9a 64 20 6f 70 63 6f 64 65 2e 20 20 55 6e 70 61 63 d opcode. Unpac
15e9b 6b 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 k the.** record
15e9c 61 6e 64 20 74 68 65 6e 20 63 61 6c 6c 20 42 74 and then call Bt
15e9d 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
15e9e 64 28 29 20 74 6f 20 64 6f 20 74 68 65 20 77 6f d() to do the wo
15e9f 72 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 rk..*/.SQLITE_PR
15ea0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
15ea1 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20 20 3BtreeMoveto(.
15ea2 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
15ea3 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 70 /* Cursor op
15ea4 65 6e 20 6f 6e 20 74 68 65 20 62 74 72 65 65 20 en on the btree
15ea5 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 20 2a to be searched *
15ea6 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
15ea7 70 4b 65 79 2c 20 20 20 2f 2a 20 50 61 63 6b 65 pKey, /* Packe
15ea8 64 20 6b 65 79 20 69 66 20 74 68 65 20 62 74 72 d key if the btr
15ea9 65 65 20 69 73 20 61 6e 20 69 6e 64 65 78 20 2a ee is an index *
15eaa 2f 0a 20 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 /. i64 nKey,
15eab 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
15eac 65 72 20 6b 65 79 20 66 6f 72 20 74 61 62 6c 65 er key for table
15ead 73 2e 20 20 53 69 7a 65 20 6f 66 20 70 4b 65 79 s. Size of pKey
15eae 20 66 6f 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a for indices */.
15eaf 20 20 69 6e 74 20 62 69 61 73 2c 20 20 20 20 20 int bias,
15eb0 20 20 20 20 20 20 2f 2a 20 42 69 61 73 20 73 65 /* Bias se
15eb1 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 67 68 arch to the high
15eb2 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 end */. int *p
15eb3 52 65 73 20 20 20 20 20 20 20 20 20 20 20 2f 2a Res /*
15eb4 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72 65 Write search re
15eb5 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b sults here */.){
15eb6 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
15eb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15eb8 20 53 74 61 74 75 73 20 63 6f 64 65 20 2a 2f 0a Status code */.
15eb9 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
15eba 20 2a 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20 *pIdxKey; /*
15ebb 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b Unpacked index k
15ebc 65 79 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 ey */. Unpacked
15ebd 52 65 63 6f 72 64 20 61 53 70 61 63 65 5b 31 36 Record aSpace[16
15ebe 5d 3b 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 65 ]; /* Temp space
15ebf 20 66 6f 72 20 70 49 64 78 4b 65 79 20 2d 20 74 for pIdxKey - t
15ec0 6f 20 61 76 6f 69 64 20 61 20 6d 61 6c 6c 6f 63 o avoid a malloc
15ec1 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4b 65 79 20 */.. if( pKey
15ec2 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e ){. assert( n
15ec3 4b 65 79 3d 3d 28 69 36 34 29 28 69 6e 74 29 6e Key==(i64)(int)n
15ec4 4b 65 79 20 29 3b 0a 20 20 20 20 70 49 64 78 4b Key );. pIdxK
15ec5 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 ey = sqlite3Vdbe
15ec6 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 70 43 75 RecordUnpack(pCu
15ec7 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 28 69 6e r->pKeyInfo, (in
15ec8 74 29 6e 4b 65 79 2c 20 70 4b 65 79 2c 0a 20 20 t)nKey, pKey,.
15ec9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15eca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15ecb 20 20 20 20 61 53 70 61 63 65 2c 20 73 69 7a 65 aSpace, size
15ecc 6f 66 28 61 53 70 61 63 65 29 29 3b 0a 20 20 20 of(aSpace));.
15ecd 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30 20 if( pIdxKey==0
15ece 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
15ecf 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a NOMEM;. }else{.
15ed0 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 30 3b pIdxKey = 0;
15ed1 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 . }. rc = sqli
15ed2 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e te3BtreeMovetoUn
15ed3 70 61 63 6b 65 64 28 70 43 75 72 2c 20 70 49 64 packed(pCur, pId
15ed4 78 4b 65 79 2c 20 6e 4b 65 79 2c 20 62 69 61 73 xKey, nKey, bias
15ed5 2c 20 70 52 65 73 29 3b 0a 20 20 69 66 28 20 70 , pRes);. if( p
15ed6 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Key ){. sqlit
15ed7 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 e3VdbeDeleteUnpa
15ed8 63 6b 65 64 52 65 63 6f 72 64 28 70 49 64 78 4b ckedRecord(pIdxK
15ed9 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ey);. }. retur
15eda 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
15edb 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 Return TRUE if t
15edc 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 he cursor is not
15edd 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 pointing at an
15ede 65 6e 74 72 79 20 6f 66 20 74 68 65 20 74 61 62 entry of the tab
15edf 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52 55 45 20 77 le..**.** TRUE w
15ee0 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 ill be returned
15ee1 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 after a call to
15ee2 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
15ee3 28 29 20 6d 6f 76 65 73 0a 2a 2a 20 70 61 73 74 () moves.** past
15ee4 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
15ee5 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 in the table or
15ee6 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 sqlite3BtreePrev
15ee7 28 29 20 6d 6f 76 65 73 20 70 61 73 74 0a 2a 2a () moves past.**
15ee8 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
15ee9 2e 20 20 54 52 55 45 20 69 73 20 61 6c 73 6f 20 . TRUE is also
15eea 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
15eeb 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a table is empty..
15eec 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
15eed 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
15eee 65 65 45 6f 66 28 42 74 43 75 72 73 6f 72 20 2a eeEof(BtCursor *
15eef 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f pCur){. /* TODO
15ef0 3a 20 57 68 61 74 20 69 66 20 74 68 65 20 63 75 : What if the cu
15ef1 72 73 6f 72 20 69 73 20 69 6e 20 43 55 52 53 4f rsor is in CURSO
15ef2 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 62 75 R_REQUIRESEEK bu
15ef3 74 20 61 6c 6c 20 74 61 62 6c 65 20 65 6e 74 72 t all table entr
15ef4 69 65 73 0a 20 20 2a 2a 20 68 61 76 65 20 62 65 ies. ** have be
15ef5 65 6e 20 64 65 6c 65 74 65 64 3f 20 54 68 69 73 en deleted? This
15ef6 20 41 50 49 20 77 69 6c 6c 20 6e 65 65 64 20 74 API will need t
15ef7 6f 20 63 68 61 6e 67 65 20 74 6f 20 72 65 74 75 o change to retu
15ef8 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 rn an error code
15ef9 0a 20 20 2a 2a 20 61 73 20 77 65 6c 6c 20 61 73 . ** as well as
15efa 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 72 65 73 the boolean res
15efb 75 6c 74 20 76 61 6c 75 65 2e 0a 20 20 2a 2f 0a ult value.. */.
15efc 20 20 72 65 74 75 72 6e 20 28 43 55 52 53 4f 52 return (CURSOR
15efd 5f 56 41 4c 49 44 21 3d 70 43 75 72 2d 3e 65 53 _VALID!=pCur->eS
15efe 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tate);.}../*.**
15eff 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 62 Return the datab
15f00 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 ase connection h
15f01 61 6e 64 6c 65 20 66 6f 72 20 61 20 63 75 72 73 andle for a curs
15f02 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 or..*/.SQLITE_PR
15f03 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 IVATE sqlite3 *s
15f04 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
15f05 72 44 62 28 63 6f 6e 73 74 20 42 74 43 75 72 73 rDb(const BtCurs
15f06 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
15f07 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15f08 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 ex_held(pCur->pB
15f09 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
15f0a 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 43 75 );. return pCu
15f0b 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 3b 0a 7d r->pBtree->db;.}
15f0c 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 ../*.** Advance
15f0d 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
15f0e 65 20 6e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 e next entry in
15f0f 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 the database. I
15f10 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 f.** successful
15f11 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 then set *pRes=0
15f12 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 . If the cursor
15f13 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 .** was already
15f14 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
15f15 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 last entry in th
15f16 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 e database befor
15f17 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e e.** this routin
15f18 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 e was called, th
15f19 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a en set *pRes=1..
15f1a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
15f1b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
15f1c 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72 20 eeNext(BtCursor
15f1d 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 *pCur, int *pRes
15f1e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 ){. int rc;. i
15f1f 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 nt idx;. MemPag
15f20 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 e *pPage;.. ass
15f21 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
15f22 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
15f23 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 rc = restoreCur
15f24 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 sorPosition(pCur
15f25 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
15f26 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 ITE_OK ){. re
15f27 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 turn rc;. }. a
15f28 73 73 65 72 74 28 20 70 52 65 73 21 3d 30 20 29 ssert( pRes!=0 )
15f29 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 ;. if( CURSOR_I
15f2a 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
15f2b 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 tate ){. *pRe
15f2c 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 s = 1;. retur
15f2d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
15f2e 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 . if( pCur->ski
15f2f 70 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d p>0 ){. pCur-
15f30 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 20 20 2a >skip = 0;. *
15f31 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 pRes = 0;. re
15f32 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
15f33 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 }. pCur->skip
15f34 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d = 0;.. pPage =
15f35 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
15f36 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 64 ur->iPage];. id
15f37 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64 x = ++pCur->aiId
15f38 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a x[pCur->iPage];.
15f39 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15f3a 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 >isInit );. ass
15f3b 65 72 74 28 20 69 64 78 3c 3d 70 50 61 67 65 2d ert( idx<=pPage-
15f3c 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 70 43 75 >nCell );.. pCu
15f3d 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 r->info.nSize =
15f3e 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 0;. pCur->valid
15f3f 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 NKey = 0;. if(
15f40 69 64 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c idx>=pPage->nCel
15f41 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 50 l ){. if( !pP
15f42 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
15f43 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
15f44 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 ild(pCur, get4by
15f45 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
15f46 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
15f47 74 2b 38 5d 29 29 3b 0a 20 20 20 20 20 20 69 66 t+8]));. if
15f48 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
15f49 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 ;. rc = mov
15f4a 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 eToLeftmost(pCur
15f4b 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d );. *pRes =
15f4c 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 0;. return
15f4d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 rc;. }. d
15f4e 6f 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 o{. if( pCu
15f4f 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 r->iPage==0 ){.
15f50 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 *pRes = 1
15f51 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e ;. pCur->
15f52 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
15f53 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 INVALID;.
15f54 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
15f55 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 K;. }.
15f56 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
15f57 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b eToParent(pCur);
15f58 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 . pPage = p
15f59 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
15f5a 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 77 ->iPage];. }w
15f5b 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 hile( pCur->aiId
15f5c 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d x[pCur->iPage]>=
15f5d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pPage->nCell );.
15f5e 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
15f5f 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e if( pPage->in
15f60 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 tKey ){. rc
15f61 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e = sqlite3BtreeN
15f62 65 78 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b ext(pCur, pRes);
15f63 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
15f64 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
15f65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
15f66 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 rn rc;. }. *pR
15f67 65 73 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 es = 0;. if( pP
15f68 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
15f69 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
15f6a 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f K;. }. rc = mo
15f6b 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 veToLeftmost(pCu
15f6c 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b r);. return rc;
15f6d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 .}.../*.** Step
15f6e 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
15f6f 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72 e back to the pr
15f70 65 76 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 evious entry in
15f71 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 the database. I
15f72 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 f.** successful
15f73 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 then set *pRes=0
15f74 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 . If the cursor
15f75 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 .** was already
15f76 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
15f77 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 first entry in t
15f78 68 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f he database befo
15f79 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 re.** this routi
15f7a 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 ne was called, t
15f7b 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e hen set *pRes=1.
15f7c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
15f7d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
15f7e 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 reePrevious(BtCu
15f7f 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
15f80 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 *pRes){. int rc
15f81 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
15f82 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 ge;.. assert( c
15f83 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
15f84 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 pCur) );. rc =
15f85 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 restoreCursorPos
15f86 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 ition(pCur);. i
15f87 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
15f88 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
15f89 63 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61 c;. }. pCur->a
15f8a 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 tLast = 0;. if(
15f8b 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d CURSOR_INVALID=
15f8c 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b =pCur->eState ){
15f8d 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a . *pRes = 1;.
15f8e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
15f8f 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 E_OK;. }. if(
15f90 70 43 75 72 2d 3e 73 6b 69 70 3c 30 20 29 7b 0a pCur->skip<0 ){.
15f91 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d pCur->skip =
15f92 20 30 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 0;. *pRes =
15f93 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 0;. return SQ
15f94 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 LITE_OK;. }. p
15f95 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a Cur->skip = 0;..
15f96 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
15f97 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
15f98 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ge];. assert( p
15f99 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a Page->isInit );.
15f9a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
15f9b 61 66 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 64 af ){. int id
15f9c 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b x = pCur->aiIdx[
15f9d 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
15f9e 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 rc = moveToChi
15f9f 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 ld(pCur, get4byt
15fa0 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 e(findCell(pPage
15fa1 2c 20 69 64 78 29 29 29 3b 0a 20 20 20 20 69 66 , idx)));. if
15fa2 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 ( rc ){. re
15fa3 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
15fa4 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 rc = moveToRi
15fa5 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 ghtmost(pCur);.
15fa6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c }else{. whil
15fa7 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 e( pCur->aiIdx[p
15fa8 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 30 20 29 Cur->iPage]==0 )
15fa9 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 {. if( pCur
15faa 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 ->iPage==0 ){.
15fab 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 pCur->eSta
15fac 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 te = CURSOR_INVA
15fad 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 2a 70 52 LID;. *pR
15fae 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 es = 1;.
15faf 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
15fb0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
15fb1 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
15fb2 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a ToParent(pCur);.
15fb3 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e }. pCur->
15fb4 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a info.nSize = 0;.
15fb5 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e pCur->validN
15fb6 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 20 20 70 43 Key = 0;.. pC
15fb7 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
15fb8 69 50 61 67 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 iPage]--;. pP
15fb9 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
15fba 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b ge[pCur->iPage];
15fbb 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
15fbc 69 6e 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 intKey && !pPage
15fbd 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
15fbe 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
15fbf 65 50 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20 ePrevious(pCur,
15fc0 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 pRes);. }else
15fc1 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
15fc2 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
15fc3 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 }. *pRes = 0;.
15fc4 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
15fc5 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 *.** Allocate a
15fc6 6e 65 77 20 70 61 67 65 20 66 72 6f 6d 20 74 68 new page from th
15fc7 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
15fc8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 .**.** The new p
15fc9 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 age is marked as
15fca 20 64 69 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 dirty. (In oth
15fcb 65 72 20 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 er words, sqlite
15fcc 33 50 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 3PagerWrite().**
15fcd 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
15fce 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 n called on the
15fcf 6e 65 77 20 70 61 67 65 2e 29 20 20 54 68 65 20 new page.) The
15fd0 6e 65 77 20 70 61 67 65 20 68 61 73 20 61 6c 73 new page has als
15fd1 6f 0a 2a 2a 20 62 65 65 6e 20 72 65 66 65 72 65 o.** been refere
15fd2 6e 63 65 64 20 61 6e 64 20 74 68 65 20 63 61 6c nced and the cal
15fd3 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 ling routine is
15fd4 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
15fd5 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 calling.** sqlit
15fd6 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f e3PagerUnref() o
15fd7 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 77 n the new page w
15fd8 68 65 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a hen it is done..
15fd9 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 **.** SQLITE_OK
15fda 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 is returned on s
15fdb 75 63 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 uccess. Any oth
15fdc 65 72 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 er return value
15fdd 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 indicates.** an
15fde 65 72 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 error. *ppPage
15fdf 61 6e 64 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 and *pPgno are u
15fe0 6e 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 ndefined in the
15fe1 65 76 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f event of an erro
15fe2 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 r..** Do not inv
15fe3 6f 6b 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 oke sqlite3Pager
15fe4 55 6e 72 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 Unref() on *ppPa
15fe5 67 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 ge if an error i
15fe6 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
15fe7 2a 20 49 66 20 74 68 65 20 22 6e 65 61 72 62 79 * If the "nearby
15fe8 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e " parameter is n
15fe9 6f 74 20 30 2c 20 74 68 65 6e 20 61 20 28 66 65 ot 0, then a (fe
15fea 65 62 6c 65 29 20 65 66 66 6f 72 74 20 69 73 20 eble) effort is
15feb 6d 61 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 made to .** loca
15fec 74 65 20 61 20 70 61 67 65 20 63 6c 6f 73 65 20 te a page close
15fed 74 6f 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 to the page numb
15fee 65 72 20 22 6e 65 61 72 62 79 22 2e 20 20 54 68 er "nearby". Th
15fef 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 is can be used i
15ff0 6e 20 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 n an.** attempt
15ff1 74 6f 20 6b 65 65 70 20 72 65 6c 61 74 65 64 20 to keep related
15ff2 70 61 67 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 pages close to e
15ff3 61 63 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 ach other in the
15ff4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a database file,.
15ff5 2a 2a 20 77 68 69 63 68 20 69 6e 20 74 75 72 6e ** which in turn
15ff6 20 63 61 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 can make databa
15ff7 73 65 20 61 63 63 65 73 73 20 66 61 73 74 65 72 se access faster
15ff8 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 ..**.** If the "
15ff9 65 78 61 63 74 22 20 70 61 72 61 6d 65 74 65 72 exact" parameter
15ffa 20 69 73 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74 is not 0, and t
15ffb 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e he page-number n
15ffc 65 61 72 62 79 20 65 78 69 73 74 73 20 0a 2a 2a earby exists .**
15ffd 20 61 6e 79 77 68 65 72 65 20 6f 6e 20 74 68 65 anywhere on the
15ffe 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e free-list, then
15fff 20 69 74 20 69 73 20 67 75 61 72 65 6e 74 65 65 it is guarentee
16000 64 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 d to be returned
16001 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c . This.** is onl
16002 79 20 75 73 65 64 20 62 79 20 61 75 74 6f 2d 76 y used by auto-v
16003 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 73 20 acuum databases
16004 77 68 65 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 when allocating
16005 61 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a a new table..*/.
16006 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 static int alloc
16007 61 74 65 42 74 72 65 65 50 61 67 65 28 0a 20 20 ateBtreePage(.
16008 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a BtShared *pBt, .
16009 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 MemPage **ppPa
1600a 67 65 2c 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67 ge, . Pgno *pPg
1600b 6e 6f 2c 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72 no, . Pgno near
1600c 62 79 2c 0a 20 20 75 38 20 65 78 61 63 74 0a 29 by,. u8 exact.)
1600d 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
1600e 67 65 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ge1;. int rc;.
1600f 20 69 6e 74 20 6e 3b 20 20 20 20 20 2f 2a 20 4e int n; /* N
16010 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f umber of pages o
16011 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a n the freelist *
16012 2f 0a 20 20 69 6e 74 20 6b 3b 20 20 20 20 20 2f /. int k; /
16013 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 * Number of leav
16014 65 73 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 es on the trunk
16015 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 of the freelist
16016 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 */. MemPage *pT
16017 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 runk = 0;. MemP
16018 61 67 65 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 age *pPrevTrunk
16019 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 = 0;.. assert(
1601a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1601b 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
1601c 3b 0a 20 20 70 50 61 67 65 31 20 3d 20 70 42 74 ;. pPage1 = pBt
1601d 2d 3e 70 50 61 67 65 31 3b 0a 20 20 6e 20 3d 20 ->pPage1;. n =
1601e 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 get4byte(&pPage1
1601f 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 ->aData[36]);.
16020 69 66 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 2f if( n>0 ){. /
16021 2a 20 54 68 65 72 65 20 61 72 65 20 70 61 67 65 * There are page
16022 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 s on the freelis
16023 74 2e 20 20 52 65 75 73 65 20 6f 6e 65 20 6f 66 t. Reuse one of
16024 20 74 68 6f 73 65 20 70 61 67 65 73 2e 20 2a 2f those pages. */
16025 0a 20 20 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b . Pgno iTrunk
16026 3b 0a 20 20 20 20 75 38 20 73 65 61 72 63 68 4c ;. u8 searchL
16027 69 73 74 20 3d 20 30 3b 20 2f 2a 20 49 66 20 74 ist = 0; /* If t
16028 68 65 20 66 72 65 65 2d 6c 69 73 74 20 6d 75 73 he free-list mus
16029 74 20 62 65 20 73 65 61 72 63 68 65 64 20 66 6f t be searched fo
1602a 72 20 27 6e 65 61 72 62 79 27 20 2a 2f 0a 20 20 r 'nearby' */.
1602b 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
1602c 20 27 65 78 61 63 74 27 20 70 61 72 61 6d 65 74 'exact' paramet
1602d 65 72 20 77 61 73 20 74 72 75 65 20 61 6e 64 20 er was true and
1602e 61 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 70 a query of the p
1602f 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a ointer-map. *
16030 2a 20 73 68 6f 77 73 20 74 68 61 74 20 74 68 65 * shows that the
16031 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69 page 'nearby' i
16032 73 20 73 6f 6d 65 77 68 65 72 65 20 6f 6e 20 74 s somewhere on t
16033 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 he free-list, th
16034 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 6e en. ** the en
16035 74 69 72 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62 tire-list will b
16036 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 e searched for t
16037 68 61 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f hat page.. */
16038 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
16039 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
1603a 20 20 20 20 69 66 28 20 65 78 61 63 74 20 26 26 if( exact &&
1603b 20 6e 65 61 72 62 79 3c 3d 70 61 67 65 72 50 61 nearby<=pagerPa
1603c 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a gecount(pBt) ){.
1603d 20 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a u8 eType;.
1603e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 65 assert( ne
1603f 61 72 62 79 3e 30 20 29 3b 0a 20 20 20 20 20 20 arby>0 );.
16040 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 assert( pBt->aut
16041 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 20 20 20 oVacuum );.
16042 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 rc = ptrmapGet(
16043 70 42 74 2c 20 6e 65 61 72 62 79 2c 20 26 65 54 pBt, nearby, &eT
16044 79 70 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 ype, 0);. i
16045 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
16046 63 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 c;. if( eTy
16047 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 pe==PTRMAP_FREEP
16048 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 73 AGE ){. s
16049 65 61 72 63 68 4c 69 73 74 20 3d 20 31 3b 0a 20 earchList = 1;.
1604a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 50 }. *pP
1604b 67 6e 6f 20 3d 20 6e 65 61 72 62 79 3b 0a 20 20 gno = nearby;.
1604c 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
1604d 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 /* Decrement the
1604e 20 66 72 65 65 2d 6c 69 73 74 20 63 6f 75 6e 74 free-list count
1604f 20 62 79 20 31 2e 20 53 65 74 20 69 54 72 75 6e by 1. Set iTrun
16050 6b 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f k to the index o
16051 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72 f the. ** fir
16052 73 74 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75 st free-list tru
16053 6e 6b 20 70 61 67 65 2e 20 69 50 72 65 76 54 72 nk page. iPrevTr
16054 75 6e 6b 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 unk is initially
16055 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 1.. */. r
16056 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
16057 57 72 69 74 65 28 70 50 61 67 65 31 2d 3e 70 44 Write(pPage1->pD
16058 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 bPage);. if(
16059 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1605a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 put4byte(&pP
1605b 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c age1->aData[36],
1605c 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 n-1);.. /* T
1605d 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 20 74 he code within t
1605e 68 69 73 20 6c 6f 6f 70 20 69 73 20 72 75 6e 20 his loop is run
1605f 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 20 74 68 65 only once if the
16060 20 27 73 65 61 72 63 68 4c 69 73 74 27 20 76 61 'searchList' va
16061 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 riable. ** is
16062 20 6e 6f 74 20 74 72 75 65 2e 20 4f 74 68 65 72 not true. Other
16063 77 69 73 65 2c 20 69 74 20 72 75 6e 73 20 6f 6e wise, it runs on
16064 63 65 20 66 6f 72 20 65 61 63 68 20 74 72 75 6e ce for each trun
16065 6b 2d 70 61 67 65 20 6f 6e 20 74 68 65 0a 20 20 k-page on the.
16066 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 75 ** free-list u
16067 6e 74 69 6c 20 74 68 65 20 70 61 67 65 20 27 6e ntil the page 'n
16068 65 61 72 62 79 27 20 69 73 20 6c 6f 63 61 74 65 earby' is locate
16069 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 6f d.. */. do
1606a 20 7b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 {. pPrevTr
1606b 75 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 unk = pTrunk;.
1606c 20 20 20 20 69 66 28 20 70 50 72 65 76 54 72 75 if( pPrevTru
1606d 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 54 nk ){. iT
1606e 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 runk = get4byte(
1606f 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 &pPrevTrunk->aDa
16070 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 65 ta[0]);. }e
16071 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 54 72 lse{. iTr
16072 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 unk = get4byte(&
16073 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
16074 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ]);. }.
16075 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
16076 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
16077 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c iTrunk, &pTrunk,
16078 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
16079 63 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 72 c ){. pTr
1607a 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 unk = 0;.
1607b 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
1607c 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 7d te_page;. }
1607d 0a 0a 20 20 20 20 20 20 6b 20 3d 20 67 65 74 34 .. k = get4
1607e 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 byte(&pTrunk->aD
1607f 61 74 61 5b 34 5d 29 3b 0a 20 20 20 20 20 20 69 ata[4]);. i
16080 66 28 20 6b 3d 3d 30 20 26 26 20 21 73 65 61 72 f( k==0 && !sear
16081 63 68 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 chList ){.
16082 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 68 /* The trunk h
16083 61 73 20 6e 6f 20 6c 65 61 76 65 73 20 61 6e 64 as no leaves and
16084 20 74 68 65 20 6c 69 73 74 20 69 73 20 6e 6f 74 the list is not
16085 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 2e being searched.
16086 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 6f 20 . ** So
16087 65 78 74 72 61 63 74 20 74 68 65 20 74 72 75 6e extract the trun
16088 6b 20 70 61 67 65 20 69 74 73 65 6c 66 20 61 6e k page itself an
16089 64 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20 d use it as the
1608a 6e 65 77 6c 79 20 0a 20 20 20 20 20 20 20 20 2a newly . *
1608b 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 * allocated page
1608c 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 */. asse
1608d 72 74 28 20 70 50 72 65 76 54 72 75 6e 6b 3d 3d rt( pPrevTrunk==
1608e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 0 );. rc
1608f 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
16090 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 ite(pTrunk->pDbP
16091 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 age);. if
16092 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 ( rc ){.
16093 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
16094 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
16095 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 50 67 }. *pPg
16096 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a 20 20 20 no = iTrunk;.
16097 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 memcpy(&pPa
16098 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 ge1->aData[32],
16099 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 &pTrunk->aData[0
1609a 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 2a ], 4);. *
1609b 70 70 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b ppPage = pTrunk;
1609c 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 . pTrunk
1609d 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41 = 0;. TRA
1609e 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 CE(("ALLOCATE: %
1609f 64 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 d trunk - %d fre
160a0 65 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c e pages left\n",
160a1 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a *pPgno, n-1));.
160a2 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
160a3 6b 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a k>pBt->usableSiz
160a4 65 2f 34 20 2d 20 32 20 29 7b 0a 20 20 20 20 20 e/4 - 2 ){.
160a5 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b /* Value of k
160a6 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 is out of range
160a7 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 . Database corr
160a8 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 uption */.
160a9 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
160aa 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
160ab 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
160ac 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 69 66 6e ocate_page;.#ifn
160ad 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
160ae 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
160af 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61 72 63 }else if( searc
160b0 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62 79 3d hList && nearby=
160b1 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 =iTrunk ){.
160b2 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 69 /* The list i
160b3 73 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 s being searched
160b4 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e 6b 20 and this trunk
160b5 70 61 67 65 20 69 73 20 74 68 65 20 70 61 67 65 page is the page
160b6 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 . ** to a
160b7 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 64 6c llocate, regardl
160b8 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 69 ess of whether i
160b9 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a 20 20 t has leaves..
160ba 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
160bb 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 3d assert( *pPgno=
160bc 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20 20 20 =iTrunk );.
160bd 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 *ppPage = pTr
160be 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 65 61 unk;. sea
160bf 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 rchList = 0;.
160c0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
160c1 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 3PagerWrite(pTru
160c2 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 nk->pDbPage);.
160c3 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
160c4 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
160c5 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
160c6 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
160c7 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 7b 0a if( k==0 ){.
160c8 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 if( !p
160c9 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 PrevTrunk ){.
160ca 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
160cb 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
160cc 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 2], &pTrunk->aDa
160cd 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 ta[0], 4);.
160ce 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
160cf 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
160d0 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 pPrevTrunk->aDat
160d1 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 a[0], &pTrunk->a
160d2 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 Data[0], 4);.
160d3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
160d4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
160d5 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 70 /* The trunk p
160d6 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64 20 age is required
160d7 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 62 75 by the caller bu
160d8 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 0a 20 t it contains .
160d9 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e ** poin
160da 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c 69 73 ters to free-lis
160db 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20 66 69 t leaves. The fi
160dc 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d 65 73 rst leaf becomes
160dd 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 20 20 a trunk.
160de 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 ** page in th
160df 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 is case..
160e0 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
160e1 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 72 75 MemPage *pNewTru
160e2 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 50 67 nk;. Pg
160e3 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d 20 67 no iNewTrunk = g
160e4 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d et4byte(&pTrunk-
160e5 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 >aData[8]);.
160e6 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
160e7 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 e3BtreeGetPage(p
160e8 42 74 2c 20 69 4e 65 77 54 72 75 6e 6b 2c 20 26 Bt, iNewTrunk, &
160e9 70 4e 65 77 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 pNewTrunk, 0);.
160ea 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 if( rc!
160eb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
160ec 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
160ed 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
160ee 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
160ef 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
160f0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
160f1 4e 65 77 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 NewTrunk->pDbPag
160f2 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 e);. if
160f3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
160f4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 ){. r
160f5 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 54 eleasePage(pNewT
160f6 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 runk);.
160f7 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
160f8 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 cate_page;.
160f9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
160fa 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 72 75 memcpy(&pNewTru
160fb 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 nk->aData[0], &p
160fc 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c Trunk->aData[0],
160fd 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4);. p
160fe 75 74 34 62 79 74 65 28 26 70 4e 65 77 54 72 75 ut4byte(&pNewTru
160ff 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2d nk->aData[4], k-
16100 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 1);. me
16101 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d mcpy(&pNewTrunk-
16102 3e 61 44 61 74 61 5b 38 5d 2c 20 26 70 54 72 75 >aData[8], &pTru
16103 6e 6b 2d 3e 61 44 61 74 61 5b 31 32 5d 2c 20 28 nk->aData[12], (
16104 6b 2d 31 29 2a 34 29 3b 0a 20 20 20 20 20 20 20 k-1)*4);.
16105 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
16106 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 NewTrunk);.
16107 20 20 20 20 20 69 66 28 20 21 70 50 72 65 76 54 if( !pPrevT
16108 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 runk ){.
16109 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
1610a 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
1610b 62 6c 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 ble(pPage1->pDbP
1610c 61 67 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 age) );.
1610d 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 put4byte(&pP
1610e 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c age1->aData[32],
1610f 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 iNewTrunk);.
16110 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
16111 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
16112 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
16113 28 70 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 (pPrevTrunk->pDb
16114 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
16115 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
16116 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
16117 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
16118 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d e;. }
16119 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 74 . put
1611a 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75 6e 4byte(&pPrevTrun
1611b 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 69 4e 65 k->aData[0], iNe
1611c 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 wTrunk);.
1611d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1611e 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 pTrunk =
1611f 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 0;. TRACE
16120 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 (("ALLOCATE: %d
16121 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 trunk - %d free
16122 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a pages left\n", *
16123 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 pPgno, n-1));.#e
16124 6e 64 69 66 0a 20 20 20 20 20 20 7d 65 6c 73 65 ndif. }else
16125 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 {. /* Ext
16126 72 61 63 74 20 61 20 6c 65 61 66 20 66 72 6f 6d ract a leaf from
16127 20 74 68 65 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 the trunk */.
16128 20 20 20 20 20 20 69 6e 74 20 63 6c 6f 73 65 73 int closes
16129 74 3b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 t;. Pgno
1612a 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 75 iPage;. u
1612b 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 44 nsigned char *aD
1612c 61 74 61 20 3d 20 70 54 72 75 6e 6b 2d 3e 61 44 ata = pTrunk->aD
1612d 61 74 61 3b 0a 20 20 20 20 20 20 20 20 72 63 20 ata;. rc
1612e 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1612f 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 ite(pTrunk->pDbP
16130 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 age);. if
16131 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 ( rc ){.
16132 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
16133 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
16134 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
16135 6e 65 61 72 62 79 3e 30 20 29 7b 0a 20 20 20 20 nearby>0 ){.
16136 20 20 20 20 20 20 69 6e 74 20 69 2c 20 64 69 73 int i, dis
16137 74 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f t;. clo
16138 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 sest = 0;.
16139 20 20 20 20 64 69 73 74 20 3d 20 67 65 74 34 62 dist = get4b
1613a 79 74 65 28 26 61 44 61 74 61 5b 38 5d 29 20 2d yte(&aData[8]) -
1613b 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20 nearby;.
1613c 20 20 20 69 66 28 20 64 69 73 74 3c 30 20 29 20 if( dist<0 )
1613d 64 69 73 74 20 3d 20 2d 64 69 73 74 3b 0a 20 20 dist = -dist;.
1613e 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b for(i=1;
1613f 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 i<k; i++){.
16140 20 20 20 20 20 20 20 20 69 6e 74 20 64 32 20 3d int d2 =
16141 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 get4byte(&aData
16142 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e 65 61 72 62 [8+i*4]) - nearb
16143 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 y;. i
16144 66 28 20 64 32 3c 30 20 29 20 64 32 20 3d 20 2d f( d2<0 ) d2 = -
16145 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 d2;.
16146 69 66 28 20 64 32 3c 64 69 73 74 20 29 7b 0a 20 if( d2<dist ){.
16147 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6c 6f clo
16148 73 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 20 20 sest = i;.
16149 20 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 64 dist = d
1614a 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 2;. }
1614b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1614c 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1614d 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 closest =
1614e 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 0;. }..
1614f 20 20 20 20 20 20 69 50 61 67 65 20 3d 20 67 65 iPage = ge
16150 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 2b t4byte(&aData[8+
16151 63 6c 6f 73 65 73 74 2a 34 5d 29 3b 0a 20 20 20 closest*4]);.
16152 20 20 20 20 20 69 66 28 20 21 73 65 61 72 63 68 if( !search
16153 4c 69 73 74 20 7c 7c 20 69 50 61 67 65 3d 3d 6e List || iPage==n
16154 65 61 72 62 79 20 29 7b 0a 20 20 20 20 20 20 20 earby ){.
16155 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 0a 20 Pgno nPage;.
16156 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 *pPgno
16157 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 = iPage;.
16158 20 20 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 nPage = pager
16159 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a Pagecount(pBt);.
1615a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 if( *p
1615b 50 67 6e 6f 3e 6e 50 61 67 65 20 29 7b 0a 20 20 Pgno>nPage ){.
1615c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 /* Fre
1615d 65 20 70 61 67 65 20 6f 66 66 20 74 68 65 20 65 e page off the e
1615e 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a nd of the file *
1615f 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 /. rc
16160 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
16161 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 T_BKPT;.
16162 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
16163 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
16164 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
16165 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 TRACE(("ALLOCA
16166 54 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20 TE: %d was leaf
16167 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e %d of %d on trun
16168 6b 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20 k %d".
16169 20 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72 ": %d mor
1616a 65 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c e free pages\n",
1616b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1616c 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 *pPgno, closes
1616d 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e t+1, k, pTrunk->
1616e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 pgno, n-1));.
1616f 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 if( close
16170 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20 st<k-1 ){.
16171 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 44 memcpy(&aD
16172 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d ata[8+closest*4]
16173 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c , &aData[4+k*4],
16174 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 4);. }
16175 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 . put4b
16176 79 74 65 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b yte(&aData[4], k
16177 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 -1);. a
16178 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
16179 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
1617a 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 20 Trunk->pDbPage)
1617b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 );. rc
1617c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
1617d 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e tPage(pBt, *pPgn
1617e 6f 2c 20 70 70 50 61 67 65 2c 20 31 29 3b 0a 20 o, ppPage, 1);.
1617f 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d if( rc=
16180 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16181 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
16182 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 3PagerDontRollba
16183 63 6b 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 ck((*ppPage)->pD
16184 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
16185 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16186 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 PagerWrite((*ppP
16187 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a age)->pDbPage);.
16188 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
16189 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1618a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 . r
1618b 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 eleasePage(*ppPa
1618c 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ge);.
1618d 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1618e 20 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c searchL
1618f 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 ist = 0;.
16190 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
16191 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 releasePage(pPr
16192 65 76 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 evTrunk);.
16193 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a pPrevTrunk = 0;.
16194 20 20 20 20 7d 77 68 69 6c 65 28 20 73 65 61 72 }while( sear
16195 63 68 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73 chList );. }els
16196 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 e{. /* There
16197 61 72 65 20 6e 6f 20 70 61 67 65 73 20 6f 6e 20 are no pages on
16198 74 68 65 20 66 72 65 65 6c 69 73 74 2c 20 73 6f the freelist, so
16199 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 create a new pa
1619a 67 65 20 61 74 20 74 68 65 0a 20 20 20 20 2a 2a ge at the. **
1619b 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 end of the file
1619c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 */. int nPag
1619d 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 e = pagerPagecou
1619e 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 2a 70 50 nt(pBt);. *pP
1619f 67 6e 6f 20 3d 20 6e 50 61 67 65 20 2b 20 31 3b gno = nPage + 1;
161a0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
161a1 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
161a2 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 . if( pBt->au
161a3 74 6f 56 61 63 75 75 6d 20 26 26 20 50 54 52 4d toVacuum && PTRM
161a4 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 2a AP_ISPAGE(pBt, *
161a5 70 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 pPgno) ){.
161a6 2f 2a 20 49 66 20 2a 70 50 67 6e 6f 20 72 65 66 /* If *pPgno ref
161a7 65 72 73 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 ers to a pointer
161a8 2d 6d 61 70 20 70 61 67 65 2c 20 61 6c 6c 6f 63 -map page, alloc
161a9 61 74 65 20 74 77 6f 20 6e 65 77 20 70 61 67 65 ate two new page
161aa 73 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 74 68 s. ** at th
161ab 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c e end of the fil
161ac 65 20 69 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65 e instead of one
161ad 2e 20 54 68 65 20 66 69 72 73 74 20 61 6c 6c 6f . The first allo
161ae 63 61 74 65 64 20 70 61 67 65 0a 20 20 20 20 20 cated page.
161af 20 2a 2a 20 62 65 63 6f 6d 65 73 20 61 20 6e 65 ** becomes a ne
161b0 77 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 w pointer-map pa
161b1 67 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 69 ge, the second i
161b2 73 20 75 73 65 64 20 62 79 20 74 68 65 20 63 61 s used by the ca
161b3 6c 6c 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ller.. */.
161b4 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c TRACE(("ALL
161b5 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 OCATE: %d from e
161b6 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 6f 69 6e nd of file (poin
161b7 74 65 72 2d 6d 61 70 20 70 61 67 65 29 5c 6e 22 ter-map page)\n"
161b8 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 20 20 , *pPgno));.
161b9 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f assert( *pPgno
161ba 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 !=PENDING_BYTE_P
161bb 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 20 AGE(pBt) );.
161bc 20 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 (*pPgno)++;.
161bd 20 20 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d if( *pPgno==
161be 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
161bf 45 28 70 42 74 29 20 29 7b 20 28 2a 70 50 67 6e E(pBt) ){ (*pPgn
161c0 6f 29 2b 2b 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 o)++; }. }.#e
161c1 6e 64 69 66 0a 0a 20 20 20 20 61 73 73 65 72 74 ndif.. assert
161c2 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e ( *pPgno!=PENDIN
161c3 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
161c4 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
161c5 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
161c6 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 (pBt, *pPgno, pp
161c7 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 Page, 0);. if
161c8 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
161c9 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
161ca 65 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 e3PagerWrite((*p
161cb 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 pPage)->pDbPage)
161cc 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
161cd 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
161ce 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 releasePage(*pp
161cf 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Page);. }.
161d0 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 TRACE(("ALLOCAT
161d1 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f E: %d from end o
161d2 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e f file\n", *pPgn
161d3 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 o));. }.. asse
161d4 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 rt( *pPgno!=PEND
161d5 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
161d6 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 t) );..end_alloc
161d7 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 ate_page:. rele
161d8 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b asePage(pTrunk);
161d9 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 . releasePage(p
161da 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66 PrevTrunk);. if
161db 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
161dc 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
161dd 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f e3PagerPageRefco
161de 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 unt((*ppPage)->p
161df 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 20 DbPage)>1 ){.
161e0 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a releasePage(*
161e1 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 ppPage);. r
161e2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
161e3 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
161e4 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 2d 3e . (*ppPage)->
161e5 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a isInit = 0;. }.
161e6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
161e7 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 70 61 67 65 /*.** Add a page
161e8 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
161e9 20 66 69 6c 65 20 74 6f 20 74 68 65 20 66 72 65 file to the fre
161ea 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c elist..**.** sql
161eb 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 ite3PagerUnref()
161ec 20 69 73 20 4e 4f 54 20 63 61 6c 6c 65 64 20 66 is NOT called f
161ed 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 or pPage..*/.sta
161ee 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65 tic int freePage
161ef 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 (MemPage *pPage)
161f0 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
161f1 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
161f2 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
161f3 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 1 = pBt->pPage1;
161f4 0a 20 20 69 6e 74 20 72 63 2c 20 6e 2c 20 6b 3b . int rc, n, k;
161f5 0a 0a 20 20 2f 2a 20 50 72 65 70 61 72 65 20 74 .. /* Prepare t
161f6 68 65 20 70 61 67 65 20 66 6f 72 20 66 72 65 65 he page for free
161f7 69 6e 67 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ing */. assert(
161f8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
161f9 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
161fa 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
161fb 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3e rt( pPage->pgno>
161fc 31 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 69 73 1 );. pPage->is
161fd 49 6e 69 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 Init = 0;.. /*
161fe 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 Increment the fr
161ff 65 65 20 70 61 67 65 20 63 6f 75 6e 74 20 6f 6e ee page count on
16200 20 70 50 61 67 65 31 20 2a 2f 0a 20 20 72 63 20 pPage1 */. rc
16201 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
16202 69 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 ite(pPage1->pDbP
16203 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 age);. if( rc )
16204 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6e 20 return rc;. n
16205 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
16206 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a e1->aData[36]);.
16207 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 put4byte(&pPag
16208 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e e1->aData[36], n
16209 2b 31 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c +1);..#ifdef SQL
1620a 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 ITE_SECURE_DELET
1620b 45 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 53 51 E. /* If the SQ
1620c 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 LITE_SECURE_DELE
1620d 54 45 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 TE compile-time
1620e 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 option is enable
1620f 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 61 6c 77 d, then. ** alw
16210 61 79 73 20 66 75 6c 6c 79 20 6f 76 65 72 77 72 ays fully overwr
16211 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f ite deleted info
16212 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 rmation with zer
16213 6f 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 os.. */. rc =
16214 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
16215 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
16216 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
16217 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 73 65 turn rc;. memse
16218 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 t(pPage->aData,
16219 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 0, pPage->pBt->p
1621a 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69 66 ageSize);.#endif
1621b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 .. /* If the da
1621c 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 tabase supports
1621d 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 69 auto-vacuum, wri
1621e 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 te an entry in t
1621f 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 he pointer-map.
16220 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20 ** to indicate
16221 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 that the page is
16222 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 free.. */. if
16223 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 ( ISAUTOVACUUM )
16224 7b 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 {. rc = ptrma
16225 70 50 75 74 28 70 42 74 2c 20 70 50 61 67 65 2d pPut(pBt, pPage-
16226 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 46 52 >pgno, PTRMAP_FR
16227 45 45 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 EEPAGE, 0);.
16228 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
16229 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e rc;. }.. if( n
1622a 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 ==0 ){. /* Th
1622b 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 is is the first
1622c 66 72 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 free page */.
1622d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1622e 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 erWrite(pPage->p
1622f 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 DbPage);. if(
16230 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
16231 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 . memset(pPag
16232 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20 38 29 3b e->aData, 0, 8);
16233 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 . put4byte(&p
16234 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
16235 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a , pPage->pgno);.
16236 20 20 20 20 54 52 41 43 45 28 28 22 46 52 45 45 TRACE(("FREE
16237 2d 50 41 47 45 3a 20 25 64 20 66 69 72 73 74 5c -PAGE: %d first\
16238 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 n", pPage->pgno)
16239 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1623a 2f 2a 20 4f 74 68 65 72 20 66 72 65 65 20 70 61 /* Other free pa
1623b 67 65 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 ges already exis
1623c 74 2e 20 20 52 65 74 72 69 76 65 20 74 68 65 20 t. Retrive the
1623d 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61 67 65 first trunk page
1623e 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 . ** of the f
1623f 72 65 65 6c 69 73 74 20 61 6e 64 20 66 69 6e 64 reelist and find
16240 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 6c 65 out how many le
16241 61 76 65 73 20 69 74 20 68 61 73 2e 20 2a 2f 0a aves it has. */.
16242 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 MemPage *pTr
16243 75 6e 6b 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 unk;. rc = sq
16244 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
16245 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 e(pBt, get4byte(
16246 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
16247 32 5d 29 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 2]), &pTrunk, 0)
16248 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
16249 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6b 20 eturn rc;. k
1624a 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 = get4byte(&pTru
1624b 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 nk->aData[4]);.
1624c 20 20 20 69 66 28 20 6b 3e 3d 70 42 74 2d 3e 75 if( k>=pBt->u
1624d 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 sableSize/4 - 8
1624e 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ){. /* The
1624f 74 72 75 6e 6b 20 69 73 20 66 75 6c 6c 2e 20 20 trunk is full.
16250 54 75 72 6e 20 74 68 65 20 70 61 67 65 20 62 65 Turn the page be
16251 69 6e 67 20 66 72 65 65 64 20 69 6e 74 6f 20 61 ing freed into a
16252 20 6e 65 77 0a 20 20 20 20 20 20 2a 2a 20 74 72 new. ** tr
16253 75 6e 6b 20 70 61 67 65 20 77 69 74 68 20 6e 6f unk page with no
16254 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20 2a leaves.. *
16255 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 *. ** Note
16256 74 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20 70 that the trunk p
16257 61 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c age is not reall
16258 79 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20 y full until it
16259 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a contains. *
1625a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d * usableSize/4 -
1625b 20 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20 2 entries, not
1625c 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 usableSize/4 - 8
1625d 20 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 68 entries as we h
1625e 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64 ave. ** cod
1625f 65 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f 20 ed. But due to
16260 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 69 a coding error i
16261 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 n versions of SQ
16262 4c 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20 20 Lite prior to.
16263 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61 ** 3.6.0, da
16264 74 61 62 61 73 65 73 20 77 69 74 68 20 66 72 65 tabases with fre
16265 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 elist trunk page
16266 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74 s holding more t
16267 68 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 han. ** usa
16268 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e bleSize/4 - 8 en
16269 74 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72 65 tries will be re
1626a 70 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75 70 ported as corrup
1626b 74 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20 20 t. In order.
1626c 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69 ** to maintai
1626d 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 n backwards comp
1626e 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f atibility with o
1626f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 lder versions of
16270 20 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a SQLite,. *
16271 2a 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 * we will contai
16272 6e 20 74 6f 20 72 65 73 74 72 69 63 74 20 74 68 n to restrict th
16273 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
16274 69 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a ies to usableSiz
16275 65 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a e/4 - 8. **
16276 20 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f for now. At so
16277 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 me point in the
16278 66 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65 future (once eve
16279 72 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64 ryone has upgrad
1627a 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33 ed. ** to 3
1627b 2e 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77 .6.0 or later) w
1627c 65 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65 e should conside
1627d 72 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e r fixing the con
1627e 64 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20 ditional above.
1627f 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 ** to read
16280 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22 "usableSize/4-2"
16281 20 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61 instead of "usa
16282 62 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20 bleSize/4-8"..
16283 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 */. rc
16284 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
16285 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
16286 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
16287 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
16288 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
16289 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 70 (pPage->aData, p
1628a 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 3b 0a 20 20 Trunk->pgno);.
1628b 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
1628c 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c pPage->aData[4],
1628d 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 0);. put
1628e 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
1628f 44 61 74 61 5b 33 32 5d 2c 20 70 50 61 67 65 2d Data[32], pPage-
16290 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 >pgno);.
16291 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 TRACE(("FREE-PAG
16292 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 E: %d new trunk
16293 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 page replacing %
16294 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
16295 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e pPage->pgn
16296 6f 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 o, pTrunk->pgno)
16297 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
16298 65 6c 73 65 20 69 66 28 20 6b 3c 30 20 29 7b 0a else if( k<0 ){.
16299 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1629a 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 7d E_CORRUPT;. }
1629b 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 else{. /* A
1629c 64 64 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 dd the newly fre
1629d 65 64 20 70 61 67 65 20 61 73 20 61 20 6c 65 61 ed page as a lea
1629e 66 20 6f 6e 20 74 68 65 20 63 75 72 72 65 6e 74 f on the current
1629f 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 trunk */.
162a0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
162a1 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 rWrite(pTrunk->p
162a2 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 DbPage);. i
162a3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
162a4 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34 ){. put4
162a5 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 byte(&pTrunk->aD
162a6 61 74 61 5b 34 5d 2c 20 6b 2b 31 29 3b 0a 20 20 ata[4], k+1);.
162a7 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
162a8 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b pTrunk->aData[8+
162a9 6b 2a 34 5d 2c 20 70 50 61 67 65 2d 3e 70 67 6e k*4], pPage->pgn
162aa 6f 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 o);.#ifndef SQLI
162ab 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 TE_SECURE_DELETE
162ac 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
162ad 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 lite3PagerDontWr
162ae 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
162af 67 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 ge);.#endif.
162b0 20 20 7d 0a 20 20 20 20 20 20 54 52 41 43 45 28 }. TRACE(
162b1 28 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 ("FREE-PAGE: %d
162b2 6c 65 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61 leaf on trunk pa
162b3 67 65 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e ge %d\n",pPage->
162b4 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e pgno,pTrunk->pgn
162b5 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 o));. }. r
162b6 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e eleasePage(pTrun
162b7 6b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e k);. }. return
162b8 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 rc;.}../*.** Fr
162b9 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 ee any overflow
162ba 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 pages associated
162bb 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 with the given
162bc 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Cell..*/.static
162bd 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 4d 65 int clearCell(Me
162be 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 6e mPage *pPage, un
162bf 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 signed char *pCe
162c0 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 ll){. BtShared
162c1 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 *pBt = pPage->pB
162c2 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e t;. CellInfo in
162c3 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 50 fo;. Pgno ovflP
162c4 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 gno;. int rc;.
162c5 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 69 6e int nOvfl;. in
162c6 74 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a t ovflPageSize;.
162c7 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
162c8 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
162c9 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
162ca 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );. sqlite3Btr
162cb 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
162cc 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
162cd 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e fo);. if( info.
162ce 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a iOverflow==0 ){.
162cf 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
162d0 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65 E_OK; /* No ove
162d1 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 52 65 74 rflow pages. Ret
162d2 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e urn without doin
162d3 67 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20 g anything */.
162d4 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 67 }. ovflPgno = g
162d5 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 et4byte(&pCell[i
162d6 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b nfo.iOverflow]);
162d7 0a 20 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 . ovflPageSize
162d8 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
162d9 65 20 2d 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d e - 4;. nOvfl =
162da 20 28 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 (info.nPayload
162db 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 - info.nLocal +
162dc 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 2d 20 31 ovflPageSize - 1
162dd 29 2f 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a )/ovflPageSize;.
162de 20 20 61 73 73 65 72 74 28 20 6f 76 66 6c 50 67 assert( ovflPg
162df 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 no==0 || nOvfl>0
162e0 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76 );. while( nOv
162e1 66 6c 2d 2d 20 29 7b 0a 20 20 20 20 4d 65 6d 50 fl-- ){. MemP
162e2 61 67 65 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 age *pOvfl;.
162e3 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 if( ovflPgno==0
162e4 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 65 || ovflPgno>page
162e5 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 rPagecount(pBt)
162e6 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
162e7 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
162e8 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 KPT;. }..
162e9 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 rc = getOverflow
162ea 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 50 67 Page(pBt, ovflPg
162eb 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20 28 6e 4f 76 no, &pOvfl, (nOv
162ec 66 6c 3d 3d 30 29 3f 30 3a 26 6f 76 66 6c 50 67 fl==0)?0:&ovflPg
162ed 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 no);. if( rc
162ee 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
162ef 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 rc = freePage(p
162f0 4f 76 66 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 Ovfl);. sqlit
162f1 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 e3PagerUnref(pOv
162f2 66 6c 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 fl->pDbPage);.
162f3 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
162f4 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 n rc;. }. retu
162f5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
162f6 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 ./*.** Create th
162f7 65 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 20 e byte sequence
162f8 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e used to represen
162f9 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65 t a cell on page
162fa 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77 72 pPage.** and wr
162fb 69 74 65 20 74 68 61 74 20 62 79 74 65 20 73 65 ite that byte se
162fc 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c quence into pCel
162fd 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70 l[]. Overflow p
162fe 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f ages are.** allo
162ff 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65 64 cated and filled
16300 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72 79 in as necessary
16301 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 . The calling p
16302 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20 72 rocedure.** is r
16303 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d esponsible for m
16304 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66 69 aking sure suffi
16305 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 73 20 cient space has
16306 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 2a been allocated.*
16307 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a 2a * for pCell[]..*
16308 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 70 *.** Note that p
16309 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e 65 Cell does not ne
1630a 63 65 73 73 61 72 79 20 6e 65 65 64 20 74 6f 20 cessary need to
1630b 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 50 61 point to the pPa
1630c 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72 65 ge->aData.** are
1630d 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 a. pCell might
1630e 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74 65 point to some te
1630f 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 2e mporary storage.
16310 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c 0a The cell will.
16311 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65 ** be constructe
16312 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70 6f 72 d in this tempor
16313 61 72 79 20 61 72 65 61 20 74 68 65 6e 20 63 6f ary area then co
16314 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67 65 2d pied into pPage-
16315 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72 2e >aData.** later.
16316 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
16317 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 6d illInCell(. Mem
16318 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 Page *pPage,
16319 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1631a 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e he page that con
1631b 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 2a tains the cell *
1631c 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
1631d 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 r *pCell,
1631e 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 /* Complete t
1631f 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 ext of the cell
16320 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
16321 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c *pKey, i64 nKey,
16322 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a /* The key *
16323 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
16324 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 61 2c pData,int nData,
16325 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a /* The data *
16326 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 /. int nZero,
16327 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16328 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 72 6f /* Extra zero
16329 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 bytes to append
1632a 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 20 69 to pData */. i
1632b 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 nt *pnSize
1632c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1632d 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69 7a 65 Write cell size
1632e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e here */.){. in
1632f 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 63 6f t nPayload;. co
16330 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a 20 20 nst u8 *pSrc;.
16331 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 63 3b int nSrc, n, rc;
16332 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66 74 . int spaceLeft
16333 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 ;. MemPage *pOv
16334 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 fl = 0;. MemPag
16335 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 e *pToRelease =
16336 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 0;. unsigned ch
16337 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75 6e ar *pPrior;. un
16338 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 61 signed char *pPa
16339 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 72 65 yload;. BtShare
1633a 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e d *pBt = pPage->
1633b 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f pBt;. Pgno pgno
1633c 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 Ovfl = 0;. int
1633d 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c 6c 49 nHeader;. CellI
1633e 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 73 nfo info;.. ass
1633f 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
16340 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
16341 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 Bt->mutex) );..
16342 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f 74 /* pPage is not
16343 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72 69 necessarily wri
16344 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43 65 teable since pCe
16345 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78 69 ll might be auxi
16346 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 66 65 liary. ** buffe
16347 72 20 73 70 61 63 65 20 74 68 61 74 20 69 73 20 r space that is
16348 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 separate from th
16349 65 20 70 50 61 67 65 20 62 75 66 66 65 72 20 61 e pPage buffer a
1634a 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 rea */. assert(
1634b 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 44 pCell<pPage->aD
1634c 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 70 ata || pCell>=&p
1634d 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d Page->aData[pBt-
1634e 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 >pageSize].
1634f 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
16350 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
16351 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
16352 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 ) );.. /* Fill
16353 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 2a in the header. *
16354 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30 3b /. nHeader = 0;
16355 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c . if( !pPage->l
16356 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 eaf ){. nHead
16357 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69 er += 4;. }. i
16358 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 f( pPage->hasDat
16359 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 a ){. nHeader
1635a 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70 += putVarint(&p
1635b 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e Cell[nHeader], n
1635c 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d Data+nZero);. }
1635d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 20 else{. nData
1635e 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d = nZero = 0;. }
1635f 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 . nHeader += pu
16360 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e tVarint(&pCell[n
16361 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29 Header], *(u64*)
16362 26 6e 4b 65 79 29 3b 0a 20 20 73 71 6c 69 74 65 &nKey);. sqlite
16363 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 3BtreeParseCellP
16364 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
16365 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 &info);. asser
16366 74 28 20 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3d t( info.nHeader=
16367 3d 6e 48 65 61 64 65 72 20 29 3b 0a 20 20 61 73 =nHeader );. as
16368 73 65 72 74 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d sert( info.nKey=
16369 3d 6e 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 =nKey );. asser
1636a 74 28 20 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d 28 t( info.nData==(
1636b 75 33 32 29 28 6e 44 61 74 61 2b 6e 5a 65 72 6f u32)(nData+nZero
1636c 29 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 46 69 6c ) );. . /* Fil
1636d 6c 20 69 6e 20 74 68 65 20 70 61 79 6c 6f 61 64 l in the payload
1636e 20 2a 2f 0a 20 20 6e 50 61 79 6c 6f 61 64 20 3d */. nPayload =
1636f 20 6e 44 61 74 61 20 2b 20 6e 5a 65 72 6f 3b 0a nData + nZero;.
16370 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 if( pPage->int
16371 4b 65 79 20 29 7b 0a 20 20 20 20 70 53 72 63 20 Key ){. pSrc
16372 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 6e 53 72 = pData;. nSr
16373 63 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 6e c = nData;. n
16374 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 Data = 0;. }els
16375 65 7b 20 0a 20 20 20 20 2f 2a 20 54 42 44 3a 20 e{ . /* TBD:
16376 20 50 65 72 68 61 70 73 20 72 61 69 73 65 20 53 Perhaps raise S
16377 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 QLITE_CORRUPT if
16378 20 6e 4b 65 79 20 69 73 20 6c 61 72 67 65 72 20 nKey is larger
16379 74 68 61 6e 20 33 31 20 62 69 74 73 3f 20 2a 2f than 31 bits? */
1637a 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2b 3d . nPayload +=
1637b 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 20 20 (int)nKey;.
1637c 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 20 20 pSrc = pKey;.
1637d 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e 4b 65 nSrc = (int)nKe
1637e 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 7a 65 y;. }. *pnSize
1637f 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 20 = info.nSize;.
16380 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 6e 66 spaceLeft = inf
16381 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 61 79 o.nLocal;. pPay
16382 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b 6e 48 load = &pCell[nH
16383 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 6f 72 eader];. pPrior
16384 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 = &pCell[info.i
16385 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 77 68 Overflow];.. wh
16386 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e 30 20 ile( nPayload>0
16387 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 63 65 ){. if( space
16388 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66 6e 64 Left==0 ){.#ifnd
16389 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1638a 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
1638b 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61 70 20 Pgno pgnoPtrmap
1638c 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a 20 4f = pgnoOvfl; /* O
1638d 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 verflow page poi
1638e 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 70 nter-map entry p
1638f 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 age */. if(
16390 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
16391 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 7b 0a ){. do{.
16392 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f 4f 76 pgnoOv
16393 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 20 fl++;. }
16394 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20 20 20 while( .
16395 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 PTRMAP_ISPAGE(
16396 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 20 7c pBt, pgnoOvfl) |
16397 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 4e 44 | pgnoOvfl==PEND
16398 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
16399 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 t) . );.
1639a 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
1639b 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 rc = allocat
1639c 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
1639d 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f 76 66 &pOvfl, &pgnoOvf
1639e 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30 29 3b l, pgnoOvfl, 0);
1639f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
163a0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
163a1 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
163a2 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 database support
163a3 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 61 s auto-vacuum, a
163a4 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 nd the second or
163a5 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 20 20 subsequent.
163a6 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 ** overflow pa
163a7 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c 6c 6f ge is being allo
163a8 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 65 6e cated, add an en
163a9 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 try to the point
163aa 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a 2a 20 er-map. **
163ab 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 6e 6f for that page no
163ac 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 w. . **.
163ad 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 ** If this is
163ae 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 the first overf
163af 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 77 low page, then w
163b0 72 69 74 65 20 61 20 70 61 72 74 69 61 6c 20 65 rite a partial e
163b1 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 74 ntry . ** t
163b2 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 o the pointer-ma
163b3 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 20 6e p. If we write n
163b4 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 20 70 othing to this p
163b5 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f 74 2c ointer-map slot,
163b6 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 . ** then t
163b7 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 6f 76 he optimistic ov
163b8 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 72 6f erflow chain pro
163b9 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 61 72 cessing in clear
163ba 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a 2a 20 Cell(). **
163bb 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 65 74 may misinterpret
163bc 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c 69 73 the uninitialis
163bd 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 64 65 ed values and de
163be 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a lete the. *
163bf 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 66 72 * wrong pages fr
163c0 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e om the database.
163c1 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
163c2 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
163c3 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 uum && rc==SQLIT
163c4 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
163c5 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e 6f u8 eType = (pgno
163c6 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f 56 Ptrmap?PTRMAP_OV
163c7 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f 4f ERFLOW2:PTRMAP_O
163c8 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 20 20 VERFLOW1);.
163c9 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 rc = ptrmapPu
163ca 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c t(pBt, pgnoOvfl,
163cb 20 65 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d eType, pgnoPtrm
163cc 61 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ap);. if(
163cd 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
163ce 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f 76 releasePage(pOv
163cf 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 fl);. }.
163d0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
163d1 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
163d2 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
163d3 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 e(pToRelease);.
163d4 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
163d5 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
163d6 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 73 /* If pToReleas
163d7 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 e is not zero th
163d8 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e 74 73 an pPrior points
163d9 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 61 into the data a
163da 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 rea. ** of
163db 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b pToRelease. Mak
163dc 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 73 e sure pToReleas
163dd 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 e is still write
163de 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61 able. */. a
163df 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65 61 73 ssert( pToReleas
163e0 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 e==0 || sqlite3P
163e1 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
163e2 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62 50 pToRelease->pDbP
163e3 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 2f age) );.. /
163e4 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 20 70 * If pPrior is p
163e5 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 art of the data
163e6 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c 20 74 area of pPage, t
163e7 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 70 50 hen make sure pP
163e8 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 age. ** is
163e9 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 20 still writeable
163ea 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
163eb 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d 3e 61 pPrior<pPage->a
163ec 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72 3e 3d Data || pPrior>=
163ed 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 &pPage->aData[pB
163ee 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 t->pageSize].
163ef 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 || sqli
163f0 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
163f1 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
163f2 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 70 75 ge) );.. pu
163f3 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 70 t4byte(pPrior, p
163f4 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 gnoOvfl);.
163f5 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 releasePage(pToR
163f6 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 70 elease);. p
163f7 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f 76 66 ToRelease = pOvf
163f8 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 20 l;. pPrior
163f9 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 3b 0a = pOvfl->aData;.
163fa 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 put4byte(p
163fb 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 20 20 Prior, 0);.
163fc 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 4f 76 pPayload = &pOv
163fd 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a 20 20 fl->aData[4];.
163fe 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 spaceLeft =
163ff 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
16400 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e - 4;. }. n
16401 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 = nPayload;.
16402 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 66 74 if( n>spaceLeft
16403 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 66 74 ) n = spaceLeft
16404 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 54 6f ;.. /* If pTo
16405 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 7a Release is not z
16406 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c 6f 61 ero than pPayloa
16407 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 68 d points into th
16408 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 20 e data area.
16409 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 65 ** of pToRelease
1640a 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54 6f . Make sure pTo
1640b 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c 6c Release is still
1640c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 writeable. */.
1640d 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 assert( pToRe
1640e 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 lease==0 || sqli
1640f 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
16410 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e ble(pToRelease->
16411 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 pDbPage) );..
16412 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61 64 20 /* If pPayload
16413 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 is part of the d
16414 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 ata area of pPag
16415 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 e, then make sur
16416 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a 20 69 e pPage. ** i
16417 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c s still writeabl
16418 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 e */. assert(
16419 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67 65 2d pPayload<pPage-
1641a 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79 6c 6f >aData || pPaylo
1641b 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 ad>=&pPage->aDat
1641c 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d a[pBt->pageSize]
1641d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
1641e 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
1641f 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
16420 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 DbPage) );..
16421 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a 20 20 if( nSrc>0 ){.
16422 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63 20 29 if( n>nSrc )
16423 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 20 20 n = nSrc;.
16424 20 61 73 73 65 72 74 28 20 70 53 72 63 20 29 3b assert( pSrc );
16425 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 50 . memcpy(pP
16426 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 6e 29 ayload, pSrc, n)
16427 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
16428 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 6c 6f memset(pPaylo
16429 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20 20 7d ad, 0, n);. }
1642a 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2d 3d . nPayload -=
1642b 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f 61 64 n;. pPayload
1642c 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72 63 20 += n;. pSrc
1642d 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63 20 2d += n;. nSrc -
1642e 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65 4c 65 = n;. spaceLe
1642f 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69 66 28 ft -= n;. if(
16430 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 nSrc==0 ){.
16431 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b 0a nSrc = nData;.
16432 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 pSrc = pDa
16433 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ta;. }. }.
16434 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 releasePage(pToR
16435 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74 75 72 elease);. retur
16436 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
16437 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 /*.** Remove the
16438 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f 6d 20 i-th cell from
16439 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 6f 75 pPage. This rou
1643a 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 50 61 tine effects pPa
1643b 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 65 20 ge only..** The
1643c 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 cell content is
1643d 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 65 61 not freed or dea
1643e 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 69 73 llocated. It is
1643f 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a assumed that.**
16440 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
16441 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 69 65 t has been copie
16442 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c 73 65 d someplace else
16443 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
16444 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 73 20 just.** removes
16445 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f the reference to
16446 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 70 the cell from p
16447 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 7a 22 Page..**.** "sz"
16448 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e 75 6d must be the num
16449 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
1644a 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 the cell..*/.sta
1644b 74 69 63 20 69 6e 74 20 64 72 6f 70 43 65 6c 6c tic int dropCell
1644c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
1644d 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20 73 7a int idx, int sz
1644e 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 ){. int i;
1644f 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
16450 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 nter */. int pc
16451 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 ; /* Off
16452 73 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 set to cell cont
16453 65 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e ent of cell bein
16454 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 g deleted */. u
16455 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 8 *data; /
16456 2a 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a * pPage->aData *
16457 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 /. u8 *ptr;
16458 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d /* Used to m
16459 6f 76 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64 ove bytes around
1645a 20 77 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a within data[] *
1645b 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
1645c 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 /* The retur
1645d 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 61 73 73 n code */.. ass
1645e 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 ert( idx>=0 && i
1645f 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 dx<pPage->nCell
16460 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d );. assert( sz=
16461 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c =cellSize(pPage,
16462 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 idx) );. asser
16463 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
16464 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
16465 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
16466 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
16467 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
16468 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
16469 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d . data = pPage-
1646a 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 >aData;. ptr =
1646b 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c &data[pPage->cel
1646c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78 5d lOffset + 2*idx]
1646d 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 ;. pc = get2byt
1646e 65 28 70 74 72 29 3b 0a 20 20 69 66 28 20 28 70 e(ptr);. if( (p
1646f 63 3c 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 c<pPage->hdrOffs
16470 65 74 2b 36 2b 28 70 50 61 67 65 2d 3e 6c 65 61 et+6+(pPage->lea
16471 66 3f 30 3a 34 29 29 0a 20 20 20 20 20 7c 7c 20 f?0:4)). ||
16472 28 70 63 2b 73 7a 3e 70 50 61 67 65 2d 3e 70 42 (pc+sz>pPage->pB
16473 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 20 29 t->usableSize) )
16474 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
16475 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
16476 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66 72 65 ;. }. rc = fre
16477 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 70 63 eSpace(pPage, pc
16478 2c 20 73 7a 29 3b 0a 20 20 69 66 28 20 72 63 21 , sz);. if( rc!
16479 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1647a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
1647b 0a 20 20 66 6f 72 28 69 3d 69 64 78 2b 31 3b 20 . for(i=idx+1;
1647c 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 i<pPage->nCell;
1647d 69 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b 0a 20 20 i++, ptr+=2){.
1647e 20 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 32 ptr[0] = ptr[2
1647f 5d 3b 0a 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 ];. ptr[1] =
16480 70 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20 20 70 50 ptr[3];. }. pP
16481 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 age->nCell--;.
16482 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 put2byte(&data[p
16483 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
16484 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3], pPage->nCell
16485 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 );. pPage->nFre
16486 65 20 2b 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e e += 2;. return
16487 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
16488 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 *.** Insert a ne
16489 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20 w cell on pPage
1648a 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20 22 69 at cell index "i
1648b 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e 74 73 ". pCell points
1648c 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 to the.** conte
1648d 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2e 0a nt of the cell..
1648e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c **.** If the cel
1648f 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 66 l content will f
16490 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2c 20 it on the page,
16491 74 68 65 6e 20 70 75 74 20 69 74 20 74 68 65 72 then put it ther
16492 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 77 69 6c e. If it.** wil
16493 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 65 6e 20 l not fit, then
16494 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 make a copy of t
16495 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
16496 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 0a 2a 2a into pTemp if.**
16497 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 20 6e 75 pTemp is not nu
16498 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 73 73 20 ll. Regardless
16499 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c 6f 63 61 of pTemp, alloca
1649a 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 0a 2a te a new entry.*
1649b 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 4f 76 66 * in pPage->aOvf
1649c 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 20 69 74 20 l[] and make it
1649d 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 63 65 6c point to the cel
1649e 6c 20 63 6f 6e 74 65 6e 74 20 28 65 69 74 68 65 l content (eithe
1649f 72 0a 2a 2a 20 69 6e 20 70 54 65 6d 70 20 6f 72 r.** in pTemp or
164a0 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 43 the original pC
164a1 65 6c 6c 29 20 61 6e 64 20 61 6c 73 6f 20 72 65 ell) and also re
164a2 63 6f 72 64 20 69 74 73 20 69 6e 64 65 78 2e 20 cord its index.
164a3 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67 20 61 .** Allocating a
164a4 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 70 50 new entry in pP
164a5 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69 6d 70 age->aCell[] imp
164a6 6c 69 65 73 20 74 68 61 74 20 0a 2a 2a 20 70 50 lies that .** pP
164a7 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 69 age->nOverflow i
164a8 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a s incremented..*
164a9 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 70 20 69 73 *.** If nSkip is
164aa 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
164ab 64 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68 65 20 do not copy the
164ac 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79 74 65 first nSkip byte
164ad 73 20 6f 66 20 74 68 65 0a 2a 2a 20 63 65 6c 6c s of the.** cell
164ae 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c . The caller wil
164af 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68 65 6d l overwrite them
164b0 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 after this func
164b1 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20 49 66 tion returns. If
164b2 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e .** nSkip is non
164b3 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 70 43 65 6c -zero, then pCel
164b4 6c 20 6d 61 79 20 6e 6f 74 20 70 6f 69 6e 74 20 l may not point
164b5 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 6d 65 to an invalid me
164b6 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 0a 2a mory location .*
164b7 2a 20 28 62 75 74 20 70 43 65 6c 6c 2b 6e 53 6b * (but pCell+nSk
164b8 69 70 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c ip is always val
164b9 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 id)..*/.static i
164ba 6e 74 20 69 6e 73 65 72 74 43 65 6c 6c 28 0a 20 nt insertCell(.
164bb 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c MemPage *pPage,
164bc 20 20 20 2f 2a 20 50 61 67 65 20 69 6e 74 6f 20 /* Page into
164bd 77 68 69 63 68 20 77 65 20 61 72 65 20 63 6f 70 which we are cop
164be 79 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 2c ying */. int i,
164bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
164c0 65 77 20 63 65 6c 6c 20 62 65 63 6f 6d 65 73 20 ew cell becomes
164c1 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 6f 66 the i-th cell of
164c2 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 the page */. u
164c3 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 8 *pCell,
164c4 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 /* Content of t
164c5 68 65 20 6e 65 77 20 63 65 6c 6c 20 2a 2f 0a 20 he new cell */.
164c6 20 69 6e 74 20 73 7a 2c 20 20 20 20 20 20 20 20 int sz,
164c7 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 63 /* Bytes of c
164c8 6f 6e 74 65 6e 74 20 69 6e 20 70 43 65 6c 6c 20 ontent in pCell
164c9 2a 2f 0a 20 20 75 38 20 2a 70 54 65 6d 70 2c 20 */. u8 *pTemp,
164ca 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 /* Temp s
164cb 74 6f 72 61 67 65 20 73 70 61 63 65 20 66 6f 72 torage space for
164cc 20 70 43 65 6c 6c 2c 20 69 66 20 6e 65 65 64 65 pCell, if neede
164cd 64 20 2a 2f 0a 20 20 75 38 20 6e 53 6b 69 70 20 d */. u8 nSkip
164ce 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e /* Do n
164cf 6f 74 20 77 72 69 74 65 20 74 68 65 20 66 69 72 ot write the fir
164d0 73 74 20 6e 53 6b 69 70 20 62 79 74 65 73 20 6f st nSkip bytes o
164d1 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 29 7b f the cell */.){
164d2 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 . int idx;
164d3 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f /* Where to
164d4 20 77 72 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 write new cell
164d5 63 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74 61 5b content in data[
164d6 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 ] */. int j;
164d7 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
164d8 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
164d9 74 20 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 t top;
164da 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 /* First byte of
164db 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 61 6e 79 content for any
164dc 20 63 65 6c 6c 20 69 6e 20 64 61 74 61 5b 5d 20 cell in data[]
164dd 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20 20 20 */. int end;
164de 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
164df 62 79 74 65 20 70 61 73 74 20 74 68 65 20 6c 61 byte past the la
164e0 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 st cell pointer
164e1 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 in data[] */. i
164e2 6e 74 20 69 6e 73 3b 20 20 20 20 20 20 20 20 20 nt ins;
164e3 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64 61 74 /* Index in dat
164e4 61 5b 5d 20 77 68 65 72 65 20 6e 65 77 20 63 65 a[] where new ce
164e5 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 69 6e ll pointer is in
164e6 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 serted */. int
164e7 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a hdr; /*
164e8 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 64 61 74 Offset into dat
164e9 61 5b 5d 20 6f 66 20 74 68 65 20 70 61 67 65 20 a[] of the page
164ea 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 header */. int
164eb 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 2f 2a cellOffset; /*
164ec 20 41 64 64 72 65 73 73 20 6f 66 20 66 69 72 73 Address of firs
164ed 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 t cell pointer i
164ee 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 38 n data[] */. u8
164ef 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 *data;
164f0 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f /* The content o
164f1 66 20 74 68 65 20 77 68 6f 6c 65 20 70 61 67 65 f the whole page
164f2 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 */. u8 *ptr;
164f3 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
164f4 66 6f 72 20 6d 6f 76 69 6e 67 20 69 6e 66 6f 72 for moving infor
164f5 6d 61 74 69 6f 6e 20 61 72 6f 75 6e 64 20 69 6e mation around in
164f6 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a 20 20 61 73 data[] */.. as
164f7 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
164f8 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 =pPage->nCell+pP
164f9 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 age->nOverflow )
164fa 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
164fb 65 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c e->nCell<=MX_CEL
164fc 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 L(pPage->pBt) &&
164fd 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e MX_CELL(pPage->
164fe 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 pBt)<=5460 );.
164ff 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
16500 4f 76 65 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 Overflow<=ArrayS
16501 69 7a 65 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c ize(pPage->aOvfl
16502 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
16503 7a 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 z==cellSizePtr(p
16504 50 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b 0a Page, pCell) );.
16505 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16506 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
16507 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
16508 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e );. if( pPage->
16509 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b nOverflow || sz+
1650a 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 2>pPage->nFree )
1650b 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70 20 {. if( pTemp
1650c 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
1650d 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 65 pTemp+nSkip, pCe
1650e 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b ll+nSkip, sz-nSk
1650f 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c ip);. pCell
16510 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a = pTemp;. }.
16511 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d 3e 6e j = pPage->n
16512 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 Overflow++;.
16513 61 73 73 65 72 74 28 20 6a 3c 28 69 6e 74 29 28 assert( j<(int)(
16514 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f sizeof(pPage->aO
16515 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70 50 61 67 vfl)/sizeof(pPag
16516 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 20 29 3b e->aOvfl[0])) );
16517 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f 76 66 . pPage->aOvf
16518 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20 70 43 65 l[j].pCell = pCe
16519 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 ll;. pPage->a
1651a 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d 20 28 75 Ovfl[j].idx = (u
1651b 31 36 29 69 3b 0a 20 20 20 20 70 50 61 67 65 2d 16)i;. pPage-
1651c 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 7d 65 >nFree = 0;. }e
1651d 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 lse{. int rc
1651e 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1651f 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
16520 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
16521 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16522 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
16523 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
16524 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
16525 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
16526 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 pDbPage) );.
16527 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
16528 61 74 61 3b 0a 20 20 20 20 68 64 72 20 3d 20 70 ata;. hdr = p
16529 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
1652a 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 . top = get2b
1652b 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
1652c 29 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 );. cellOffse
1652d 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f t = pPage->cellO
1652e 66 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d ffset;. end =
1652f 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a cellOffset + 2*
16530 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 2b 20 32 pPage->nCell + 2
16531 3b 0a 20 20 20 20 69 6e 73 20 3d 20 63 65 6c 6c ;. ins = cell
16532 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b 0a 20 20 Offset + 2*i;.
16533 20 20 69 66 28 20 65 6e 64 20 3e 20 74 6f 70 20 if( end > top
16534 2d 20 73 7a 20 29 7b 0a 20 20 20 20 20 20 72 63 - sz ){. rc
16535 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 = defragmentPag
16536 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
16537 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
16538 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 K ){. ret
16539 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
1653a 20 20 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 top = get2
1653b 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
1653c 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ]);. assert
1653d 28 20 65 6e 64 20 2b 20 73 7a 20 3c 3d 20 74 6f ( end + sz <= to
1653e 70 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 p );. }. i
1653f 64 78 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61 dx = allocateSpa
16540 63 65 28 70 50 61 67 65 2c 20 73 7a 29 3b 0a 20 ce(pPage, sz);.
16541 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 30 assert( idx>0
16542 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
16543 65 6e 64 20 3c 3d 20 67 65 74 32 62 79 74 65 28 end <= get2byte(
16544 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 29 3b &data[hdr+5]) );
16545 0a 20 20 20 20 69 66 20 28 69 64 78 2b 73 7a 20 . if (idx+sz
16546 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 > pPage->pBt->us
16547 61 62 6c 65 53 69 7a 65 29 20 7b 0a 20 20 20 20 ableSize) {.
16548 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
16549 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1654a 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e }. pPage->n
1654b 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 61 67 Cell++;. pPag
1654c 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 32 3b 0a 20 e->nFree -= 2;.
1654d 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b memcpy(&data[
1654e 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65 6c idx+nSkip], pCel
1654f 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 l+nSkip, sz-nSki
16550 70 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e p);. for(j=en
16551 64 2d 32 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a d-2, ptr=&data[j
16552 5d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 ]; j>ins; j-=2,
16553 70 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70 ptr-=2){. p
16554 74 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b tr[0] = ptr[-2];
16555 0a 20 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 . ptr[1] =
16556 70 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 ptr[-1];. }.
16557 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 put2byte(&dat
16558 61 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 a[ins], idx);.
16559 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
1655a 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 2d 3e [hdr+3], pPage->
1655b 6e 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 nCell);.#ifndef
1655c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
1655d 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 VACUUM. if( p
1655e 50 61 67 65 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 Page->pBt->autoV
1655f 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 2f acuum ){. /
16560 2a 20 54 68 65 20 63 65 6c 6c 20 6d 61 79 20 63 * The cell may c
16561 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72 ontain a pointer
16562 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 to an overflow
16563 70 61 67 65 2e 20 49 66 20 73 6f 2c 20 77 72 69 page. If so, wri
16564 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 te. ** the
16565 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 entry for the ov
16566 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 74 6f erflow page into
16567 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
16568 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
16569 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
1656a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
1656b 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
1656c 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
1656d 66 6f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 fo);. asser
1656e 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 t( (info.nData+(
1656f 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a pPage->intKey?0:
16570 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 info.nKey))==inf
16571 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 o.nPayload );.
16572 20 20 20 20 69 66 28 20 28 69 6e 66 6f 2e 6e 44 if( (info.nD
16573 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b ata+(pPage->intK
16574 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 ey?0:info.nKey))
16575 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a >info.nLocal ){.
16576 20 20 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e Pgno pgn
16577 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 oOvfl = get4byte
16578 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 (&pCell[info.iOv
16579 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20 erflow]);.
1657a 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 rc = ptrmapPut
1657b 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 67 6e (pPage->pBt, pgn
1657c 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 oOvfl, PTRMAP_OV
1657d 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e ERFLOW1, pPage->
1657e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 pgno);. i
1657f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
16580 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
16581 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 }. }.#end
16582 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e if. }.. return
16583 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
16584 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 73 74 20 *.** Add a list
16585 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 20 70 61 of cells to a pa
16586 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 73 68 ge. The page sh
16587 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 6c ould be initiall
16588 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 68 65 20 y empty..** The
16589 63 65 6c 6c 73 20 61 72 65 20 67 75 61 72 61 6e cells are guaran
1658a 74 65 65 64 20 74 6f 20 66 69 74 20 6f 6e 20 74 teed to fit on t
1658b 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 he page..*/.stat
1658c 69 63 20 76 6f 69 64 20 61 73 73 65 6d 62 6c 65 ic void assemble
1658d 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67 65 20 Page(. MemPage
1658e 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 54 68 65 *pPage, /* The
1658f 20 70 61 67 65 20 74 6f 20 62 65 20 61 73 73 65 page to be asse
16590 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 6e 74 20 mblied */. int
16591 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a nCell, /*
16592 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 The number of c
16593 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 6f 20 74 ells to add to t
16594 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 his page */. u8
16595 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 20 **apCell,
16596 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 63 /* Pointers to c
16597 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f 0a 20 20 ell bodies */.
16598 75 31 36 20 2a 61 53 69 7a 65 20 20 20 20 20 20 u16 *aSize
16599 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 74 68 /* Sizes of th
1659a 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a 20 20 e cells */.){.
1659b 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
1659c 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
1659d 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 74 61 6c r */. int total
1659e 53 69 7a 65 3b 20 20 20 20 2f 2a 20 54 6f 74 61 Size; /* Tota
1659f 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 l size of all ce
165a0 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 lls */. int hdr
165a1 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e ; /* In
165a2 64 65 78 20 6f 66 20 70 61 67 65 20 68 65 61 64 dex of page head
165a3 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c er */. int cell
165a4 70 74 72 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 ptr; /* Add
165a5 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c ress of next cel
165a6 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 l pointer */. i
165a7 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20 20 20 nt cellbody;
165a8 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e /* Address of n
165a9 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20 2a 2f ext cell body */
165aa 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 . u8 *data;
165ab 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 /* Data for
165ac 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20 the page */..
165ad 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
165ae 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 Overflow==0 );.
165af 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
165b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
165b1 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
165b2 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c ;. assert( nCel
165b3 6c 3e 3d 30 20 26 26 20 6e 43 65 6c 6c 3c 3d 4d l>=0 && nCell<=M
165b4 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 X_CELL(pPage->pB
165b5 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 t) && MX_CELL(pP
165b6 61 67 65 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 age->pBt)<=5460
165b7 29 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 3d );. totalSize =
165b8 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0;. for(i=0; i
165b9 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 <nCell; i++){.
165ba 20 20 74 6f 74 61 6c 53 69 7a 65 20 2b 3d 20 61 totalSize += a
165bb 53 69 7a 65 5b 69 5d 3b 0a 20 20 7d 0a 20 20 61 Size[i];. }. a
165bc 73 73 65 72 74 28 20 74 6f 74 61 6c 53 69 7a 65 ssert( totalSize
165bd 2b 32 2a 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d +2*nCell<=pPage-
165be 3e 6e 46 72 65 65 20 29 3b 0a 20 20 61 73 73 65 >nFree );. asse
165bf 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c rt( pPage->nCell
165c0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
165c1 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
165c2 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
165c3 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 63 65 pDbPage) );. ce
165c4 6c 6c 70 74 72 20 3d 20 70 50 61 67 65 2d 3e 63 llptr = pPage->c
165c5 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 64 61 74 ellOffset;. dat
165c6 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 a = pPage->aData
165c7 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d ;. hdr = pPage-
165c8 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 70 75 >hdrOffset;. pu
165c9 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
165ca 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 69 +3], nCell);. i
165cb 66 28 20 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 f( nCell ){.
165cc 63 65 6c 6c 62 6f 64 79 20 3d 20 61 6c 6c 6f 63 cellbody = alloc
165cd 61 74 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 ateSpace(pPage,
165ce 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 20 20 totalSize);.
165cf 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 79 assert( cellbody
165d0 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 >0 );. assert
165d1 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e ( pPage->nFree >
165d2 3d 20 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 = 2*nCell );.
165d3 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d pPage->nFree -=
165d4 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 20 20 66 6f 2*nCell;. fo
165d5 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
165d6 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 75 74 32 i++){. put2
165d7 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c 70 byte(&data[cellp
165d8 74 72 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a tr], cellbody);.
165d9 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 memcpy(&da
165da 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 ta[cellbody], ap
165db 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 Cell[i], aSize[i
165dc 5d 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 70 74 ]);. cellpt
165dd 72 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 63 65 r += 2;. ce
165de 6c 6c 62 6f 64 79 20 2b 3d 20 61 53 69 7a 65 5b llbody += aSize[
165df 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 i];. }. as
165e0 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 79 3d 3d sert( cellbody==
165e1 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
165e2 6c 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 20 20 leSize );. }.
165e3 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 pPage->nCell = (
165e4 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a u16)nCell;.}../*
165e5 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
165e6 67 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 g parameters det
165e7 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 ermine how many
165e8 61 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 adjacent pages g
165e9 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 et involved.** i
165ea 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 n a balancing op
165eb 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 eration. NN is
165ec 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 the number of ne
165ed 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 ighbors on eithe
165ee 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 r side.** of the
165ef 20 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 page that parti
165f0 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 cipate in the ba
165f1 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f lancing operatio
165f2 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a n. NB is the.**
165f3 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
165f4 20 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 pages that part
165f5 69 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 icipate, includi
165f6 6e 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 ng the target pa
165f7 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 ge and.** NN nei
165f8 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 ghbors on either
165f9 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 side..**.** The
165fa 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f minimum value o
165fb 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f f NN is 1 (of co
165fc 75 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 urse). Increasi
165fd 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a ng NN above 1.**
165fe 20 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 (to 2 or 3) giv
165ff 65 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 es a modest impr
16600 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 ovement in SELEC
16601 54 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 T and DELETE per
16602 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 formance.** in e
16603 78 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 xchange for a la
16604 72 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e rger degradation
16605 20 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 in INSERT and U
16606 50 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 PDATE performanc
16607 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 e..** The value
16608 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f of NN appears to
16609 20 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 give the best r
1660a 65 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a esults overall..
1660b 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 */.#define NN 1
1660c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1660d 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f umber of neighbo
1660e 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 rs on either sid
1660f 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 e of pPage */.#d
16610 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 efine NB (NN*2+1
16611 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 ) /* Total
16612 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 pages involved i
16613 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f n the balance */
16614 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 ../* Forward ref
16615 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 erence */.static
16616 20 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 74 43 int balance(BtC
16617 75 72 73 6f 72 2a 2c 20 69 6e 74 29 3b 0a 0a 23 ursor*, int);..#
16618 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
16619 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a IT_QUICKBALANCE.
1661a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 /*.** This versi
1661b 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65 28 29 20 on of balance()
1661c 68 61 6e 64 6c 65 73 20 74 68 65 20 63 6f 6d 6d handles the comm
1661d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 on special case
1661e 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65 77 20 65 where.** a new e
1661f 6e 74 72 79 20 69 73 20 62 65 69 6e 67 20 69 6e ntry is being in
16620 73 65 72 74 65 64 20 6f 6e 20 74 68 65 20 65 78 serted on the ex
16621 74 72 65 6d 65 20 72 69 67 68 74 2d 65 6e 64 20 treme right-end
16622 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65 65 2c 20 of the.** tree,
16623 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 in other words,
16624 77 68 65 6e 20 74 68 65 20 6e 65 77 20 65 6e 74 when the new ent
16625 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 ry will become t
16626 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20 65 6e he largest.** en
16627 74 72 79 20 69 6e 20 74 68 65 20 74 72 65 65 2e try in the tree.
16628 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f .**.** Instead o
16629 66 20 74 72 79 69 6e 67 20 62 61 6c 61 6e 63 65 f trying balance
1662a 20 74 68 65 20 33 20 72 69 67 68 74 2d 6d 6f 73 the 3 right-mos
1662b 74 20 6c 65 61 66 20 70 61 67 65 73 2c 20 6a 75 t leaf pages, ju
1662c 73 74 20 61 64 64 0a 2a 2a 20 61 20 6e 65 77 20 st add.** a new
1662d 70 61 67 65 20 74 6f 20 74 68 65 20 72 69 67 68 page to the righ
1662e 74 2d 68 61 6e 64 20 73 69 64 65 20 61 6e 64 20 t-hand side and
1662f 70 75 74 20 74 68 65 20 6f 6e 65 20 6e 65 77 20 put the one new
16630 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68 61 74 entry in.** that
16631 20 70 61 67 65 2e 20 20 54 68 69 73 20 6c 65 61 page. This lea
16632 76 65 73 20 74 68 65 20 72 69 67 68 74 20 73 69 ves the right si
16633 64 65 20 6f 66 20 74 68 65 20 74 72 65 65 20 73 de of the tree s
16634 6f 6d 65 77 68 61 74 0a 2a 2a 20 75 6e 62 61 6c omewhat.** unbal
16635 61 6e 63 65 64 2e 20 20 42 75 74 20 6f 64 64 73 anced. But odds
16636 20 61 72 65 20 74 68 61 74 20 77 65 20 77 69 6c are that we wil
16637 6c 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20 6e l be inserting n
16638 65 77 20 65 6e 74 72 69 65 73 0a 2a 2a 20 61 74 ew entries.** at
16639 20 74 68 65 20 65 6e 64 20 73 6f 6f 6e 20 61 66 the end soon af
1663a 74 65 72 77 61 72 64 73 20 73 6f 20 74 68 65 20 terwards so the
1663b 6e 65 61 72 6c 79 20 65 6d 70 74 79 20 70 61 67 nearly empty pag
1663c 65 20 77 69 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a e will quickly.*
1663d 2a 20 66 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 * fill up. On a
1663e 76 65 72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 verage..**.** pP
1663f 61 67 65 20 69 73 20 74 68 65 20 6c 65 61 66 20 age is the leaf
16640 70 61 67 65 20 77 68 69 63 68 20 69 73 20 74 68 page which is th
16641 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 70 61 67 e right-most pag
16642 65 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a e in the tree..*
16643 2a 20 70 50 61 72 65 6e 74 20 69 73 20 69 74 73 * pParent is its
16644 20 70 61 72 65 6e 74 2e 20 20 70 50 61 67 65 20 parent. pPage
16645 6d 75 73 74 20 68 61 76 65 20 61 20 73 69 6e 67 must have a sing
16646 6c 65 20 6f 76 65 72 66 6c 6f 77 20 65 6e 74 72 le overflow entr
16647 79 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61 6c y.** which is al
16648 73 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 so the right-mos
16649 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 t entry on the p
1664a 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 age..*/.static i
1664b 6e 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b nt balance_quick
1664c 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
1664d 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 {. int rc;. Me
1664e 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d 20 30 3b mPage *pNew = 0;
1664f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b . Pgno pgnoNew;
16650 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 . u8 *pCell;.
16651 75 31 36 20 73 7a 43 65 6c 6c 3b 0a 20 20 43 65 u16 szCell;. Ce
16652 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 4d llInfo info;. M
16653 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 emPage *pPage =
16654 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
16655 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 4d 65 6d r->iPage];. Mem
16656 50 61 67 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 Page *pParent =
16657 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
16658 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 42 r->iPage-1];. B
16659 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
1665a 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 Page->pBt;. int
1665b 20 70 61 72 65 6e 74 49 64 78 20 3d 20 70 50 61 parentIdx = pPa
1665c 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 20 20 20 2f rent->nCell; /
1665d 2a 20 70 50 61 72 65 6e 74 20 6e 65 77 20 64 69 * pParent new di
1665e 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 64 65 78 vider cell index
1665f 20 2a 2f 0a 20 20 69 6e 74 20 70 61 72 65 6e 74 */. int parent
16660 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
16661 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
16662 6f 66 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 of new divider c
16663 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 70 61 72 65 ell */. u8 pare
16664 6e 74 43 65 6c 6c 5b 36 34 5d 3b 20 20 20 20 20 ntCell[64];
16665 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 /* Sp
16666 61 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 ace for the new
16667 64 69 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a divider cell */.
16668 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
16669 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
1666a 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
1666b 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 );.. /* Alloca
1666c 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 49 te a new page. I
1666d 6e 73 65 72 74 20 74 68 65 20 6f 76 65 72 66 6c nsert the overfl
1666e 6f 77 20 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 ow cell from pPa
1666f 67 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 69 74 2e ge. ** into it.
16670 20 54 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 Then remove the
16671 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 66 overflow cell f
16672 72 6f 6d 20 70 50 61 67 65 2e 0a 20 20 2a 2f 0a rom pPage.. */.
16673 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 rc = allocateB
16674 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 treePage(pBt, &p
16675 4e 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 New, &pgnoNew, 0
16676 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d , 0);. if( rc==
16677 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16678 20 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e pCell = pPage->
16679 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a aOvfl[0].pCell;.
1667a 20 20 20 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c szCell = cel
1667b 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 lSizePtr(pPage,
1667c 70 43 65 6c 6c 29 3b 0a 20 20 20 20 61 73 73 65 pCell);. asse
1667d 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
1667e 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 77 Iswriteable(pNew
1667f 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
16680 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c zeroPage(pNew,
16681 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d pPage->aData[0]
16682 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 );. assembleP
16683 61 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 age(pNew, 1, &pC
16684 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 20 ell, &szCell);.
16685 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 pPage->nOverf
16686 6c 6f 77 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 low = 0;. .
16687 2f 2a 20 70 50 61 67 65 20 69 73 20 63 75 72 72 /* pPage is curr
16688 65 6e 74 6c 79 20 74 68 65 20 72 69 67 68 74 2d ently the right-
16689 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 child of pParent
1668a 2e 20 43 68 61 6e 67 65 20 74 68 69 73 0a 20 20 . Change this.
1668b 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 ** so that the
1668c 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69 73 20 right-child is
1668d 74 68 65 20 6e 65 77 20 70 61 67 65 20 61 6c 6c the new page all
1668e 6f 63 61 74 65 64 20 61 62 6f 76 65 20 61 6e 64 ocated above and
1668f 0a 20 20 20 20 2a 2a 20 70 50 61 67 65 20 69 73 . ** pPage is
16690 20 74 68 65 20 6e 65 78 74 2d 74 6f 2d 72 69 67 the next-to-rig
16691 68 74 20 63 68 69 6c 64 2e 20 0a 20 20 20 20 2a ht child. . *
16692 2a 0a 20 20 20 20 2a 2a 20 49 67 6e 6f 72 65 20 *. ** Ignore
16693 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
16694 20 6f 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 of the call to
16695 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 2e 20 66 69 fillInCell(). fi
16696 6c 6c 49 6e 43 65 6c 6c 28 29 0a 20 20 20 20 2a llInCell(). *
16697 2a 20 6d 61 79 20 6f 6e 6c 79 20 72 65 74 75 72 * may only retur
16698 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c n other than SQL
16699 49 54 45 5f 4f 4b 20 69 66 20 69 74 20 69 73 20 ITE_OK if it is
1669a 72 65 71 75 69 72 65 64 20 74 6f 20 61 6c 6c 6f required to allo
1669b 63 61 74 65 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 cate. ** one
1669c 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 or more overflow
1669d 20 70 61 67 65 73 2e 20 53 69 6e 63 65 20 61 6e pages. Since an
1669e 20 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 internal table
1669f 42 2d 54 72 65 65 20 63 65 6c 6c 20 0a 20 20 20 B-Tree cell .
166a0 20 2a 2a 20 6d 61 79 20 6e 65 76 65 72 20 73 70 ** may never sp
166a1 69 6c 6c 20 6f 76 65 72 20 6f 6e 74 6f 20 61 6e ill over onto an
166a2 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 28 overflow page (
166a3 69 74 20 69 73 20 61 20 6d 61 78 69 6d 75 6d 20 it is a maximum
166a4 6f 66 20 0a 20 20 20 20 2a 2a 20 31 33 20 62 79 of . ** 13 by
166a5 74 65 73 20 69 6e 20 73 69 7a 65 29 2c 20 69 74 tes in size), it
166a6 20 69 73 20 6e 6f 74 20 6e 65 63 63 65 73 73 61 is not neccessa
166a7 72 79 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 ry to check the
166a8 72 65 74 75 72 6e 20 63 6f 64 65 2e 0a 20 20 20 return code..
166a9 20 2a 2a 0a 20 20 20 20 2a 2a 20 53 69 6d 69 6c **. ** Simil
166aa 61 72 6c 79 2c 20 74 68 65 20 69 6e 73 65 72 74 arly, the insert
166ab 43 65 6c 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 Cell() function
166ac 63 61 6e 6e 6f 74 20 66 61 69 6c 20 69 66 20 74 cannot fail if t
166ad 68 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 62 he page. ** b
166ae 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e eing inserted in
166af 74 6f 20 69 73 20 61 6c 72 65 61 64 79 20 77 72 to is already wr
166b0 69 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 63 itable and the c
166b1 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 0a 20 20 ell does not .
166b2 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 6e 20 ** contain an
166b3 6f 76 65 72 66 6c 6f 77 20 70 6f 69 6e 74 65 72 overflow pointer
166b4 2e 20 53 6f 20 69 67 6e 6f 72 65 20 74 68 69 73 . So ignore this
166b5 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f 6f return code too
166b6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
166b7 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c ert( pPage->nCel
166b8 6c 3e 30 20 29 3b 0a 20 20 20 20 70 43 65 6c 6c l>0 );. pCell
166b9 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 = findCell(pPag
166ba 65 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d e, pPage->nCell-
166bb 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 1);. sqlite3B
166bc 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
166bd 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 (pPage, pCell, &
166be 69 6e 66 6f 29 3b 0a 20 20 20 20 66 69 6c 6c 49 info);. fillI
166bf 6e 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 nCell(pParent, p
166c0 61 72 65 6e 74 43 65 6c 6c 2c 20 30 2c 20 69 6e arentCell, 0, in
166c1 66 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30 2c 20 30 fo.nKey, 0, 0, 0
166c2 2c 20 26 70 61 72 65 6e 74 53 69 7a 65 29 3b 0a , &parentSize);.
166c3 20 20 20 20 61 73 73 65 72 74 28 20 70 61 72 65 assert( pare
166c4 6e 74 53 69 7a 65 3c 36 34 20 29 3b 0a 20 20 20 ntSize<64 );.
166c5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
166c6 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
166c7 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 (pParent->pDbPag
166c8 65 29 20 29 3b 0a 20 20 20 20 69 6e 73 65 72 74 e) );. insert
166c9 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 Cell(pParent, pa
166ca 72 65 6e 74 49 64 78 2c 20 70 61 72 65 6e 74 43 rentIdx, parentC
166cb 65 6c 6c 2c 20 70 61 72 65 6e 74 53 69 7a 65 2c ell, parentSize,
166cc 20 30 2c 20 34 29 3b 0a 20 20 20 20 70 75 74 34 0, 4);. put4
166cd 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f byte(findOverflo
166ce 77 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 70 61 wCell(pParent,pa
166cf 72 65 6e 74 49 64 78 29 2c 20 70 50 61 67 65 2d rentIdx), pPage-
166d0 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 70 75 74 34 >pgno);. put4
166d1 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 byte(&pParent->a
166d2 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 Data[pParent->hd
166d3 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f rOffset+8], pgno
166d4 4e 65 77 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 New);. . /*
166d5 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 If this is an au
166d6 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 to-vacuum databa
166d7 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 se, update the p
166d8 6f 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a ointer map. *
166d9 2a 20 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 * with entries f
166da 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c or the new page,
166db 20 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 and any pointer
166dc 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a from the . *
166dd 2a 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 * cell on the pa
166de 67 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f ge to an overflo
166df 77 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 w page.. */.
166e0 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
166e1 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 UUM ){. rc
166e2 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c = ptrmapPut(pBt,
166e3 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 pgnoNew, PTRMAP
166e4 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d _BTREE, pParent-
166e5 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 >pgno);. if
166e6 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
166e7 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
166e8 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e ptrmapPutOvfl(pN
166e9 65 77 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a ew, 0);. }.
166ea 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 }.. /* Re
166eb 6c 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 lease the refere
166ec 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 77 20 70 nce to the new p
166ed 61 67 65 2e 20 2a 2f 0a 20 20 20 20 72 65 6c 65 age. */. rele
166ee 61 73 65 50 61 67 65 28 70 4e 65 77 29 3b 0a 20 asePage(pNew);.
166ef 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 }.. /* At this
166f0 20 70 6f 69 6e 74 20 74 68 65 20 70 50 61 67 65 point the pPage
166f1 2d 3e 6e 46 72 65 65 20 76 61 72 69 61 62 6c 65 ->nFree variable
166f2 20 69 73 20 6e 6f 74 20 73 65 74 20 63 6f 72 72 is not set corr
166f3 65 63 74 6c 79 20 77 69 74 68 0a 20 20 2a 2a 20 ectly with. **
166f4 72 65 73 70 65 63 74 20 74 6f 20 74 68 65 20 63 respect to the c
166f5 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 ontent of the pa
166f6 67 65 20 28 62 65 63 61 75 73 65 20 69 74 20 77 ge (because it w
166f7 61 73 20 73 65 74 20 74 6f 20 30 20 62 79 20 0a as set to 0 by .
166f8 20 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c 29 ** insertCell)
166f9 2e 20 53 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 . So call sqlite
166fa 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 3BtreeInitPage()
166fb 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 to make sure it
166fc 20 69 73 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 is. ** correct
166fd 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 .. **. ** This
166fe 20 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 20 has to be done
166ff 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 even if an error
16700 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 will be returne
16701 64 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 0a d. Normally, if.
16702 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 ** an error oc
16703 63 75 72 73 20 64 75 72 69 6e 67 20 74 72 65 65 curs during tree
16704 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 20 balancing, the
16705 63 6f 6e 74 65 6e 74 73 20 6f 66 20 4d 65 6d 50 contents of MemP
16706 61 67 65 20 61 72 65 0a 20 20 2a 2a 20 6e 6f 74 age are. ** not
16707 20 69 6d 70 6f 72 74 61 6e 74 2c 20 61 73 20 74 important, as t
16708 68 65 79 20 77 69 6c 6c 20 62 65 20 72 65 63 61 hey will be reca
16709 6c 63 75 6c 61 74 65 64 20 77 68 65 6e 20 74 68 lculated when th
1670a 65 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 e page is rolled
1670b 0a 20 20 2a 2a 20 62 61 63 6b 2e 20 42 75 74 20 . ** back. But
1670c 68 65 72 65 2c 20 69 6e 20 62 61 6c 61 6e 63 65 here, in balance
1670d 5f 71 75 69 63 6b 28 29 2c 20 69 74 20 69 73 20 _quick(), it is
1670e 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 70 50 possible that pP
1670f 61 67 65 20 68 61 73 20 0a 20 20 2a 2a 20 6e 6f age has . ** no
16710 74 20 79 65 74 20 62 65 65 6e 20 6d 61 72 6b 65 t yet been marke
16711 64 20 64 69 72 74 79 20 6f 72 20 77 72 69 74 74 d dirty or writt
16712 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 en into the jour
16713 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 66 nal file. Theref
16714 6f 72 65 0a 20 20 2a 2a 20 69 74 20 77 69 6c 6c ore. ** it will
16715 20 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 not be rolled b
16716 61 63 6b 20 61 6e 64 20 73 6f 20 69 74 20 69 73 ack and so it is
16717 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6d 61 important to ma
16718 6b 65 20 73 75 72 65 20 74 68 61 74 0a 20 20 2a ke sure that. *
16719 2a 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 * the page data
1671a 61 6e 64 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 and contents of
1671b 4d 65 6d 50 61 67 65 20 61 72 65 20 63 6f 6e 73 MemPage are cons
1671c 69 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 istent.. */. p
1671d 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 Page->isInit = 0
1671e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1671f 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b InitPage(pPage);
16720 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
16721 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 ->nOverflow==0 )
16722 3b 0a 0a 20 20 2f 2a 20 49 66 20 65 76 65 72 79 ;.. /* If every
16723 74 68 69 6e 67 20 65 6c 73 65 20 73 75 63 63 65 thing else succe
16724 65 64 65 64 2c 20 62 61 6c 61 6e 63 65 20 74 68 eded, balance th
16725 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 69 e parent page, i
16726 6e 20 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65 n . ** case the
16727 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e divider cell in
16728 73 65 72 74 65 64 20 63 61 75 73 65 64 20 69 74 serted caused it
16729 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 to become overf
1672a 75 6c 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ull.. */. if(
1672b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1672c 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
1672d 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 43 75 (pPage);. pCu
1672e 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 20 20 r->iPage--;.
1672f 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 rc = balance(pCu
16730 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 r, 0);. }. ret
16731 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 urn rc;.}.#endif
16732 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
16733 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a QUICKBALANCE */.
16734 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
16735 69 6e 65 20 72 65 64 69 73 74 72 69 62 75 74 65 ine redistribute
16736 73 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 s Cells on pPage
16737 20 61 6e 64 20 75 70 20 74 6f 20 4e 4e 2a 32 20 and up to NN*2
16738 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 6f 66 20 70 siblings.** of p
16739 50 61 67 65 20 73 6f 20 74 68 61 74 20 61 6c 6c Page so that all
1673a 20 70 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 pages have abou
1673b 74 20 74 68 65 20 73 61 6d 65 20 61 6d 6f 75 6e t the same amoun
1673c 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 2e t of free space.
1673d 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 4e 4e 20 73 .** Usually NN s
1673e 69 62 6c 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 iblings on eithe
1673f 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 20 r side of pPage
16740 69 73 20 75 73 65 64 20 69 6e 20 74 68 65 20 62 is used in the b
16741 61 6c 61 6e 63 69 6e 67 2c 0a 2a 2a 20 74 68 6f alancing,.** tho
16742 75 67 68 20 6d 6f 72 65 20 73 69 62 6c 69 6e 67 ugh more sibling
16743 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 72 6f s might come fro
16744 6d 20 6f 6e 65 20 73 69 64 65 20 69 66 20 70 50 m one side if pP
16745 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74 age is the first
16746 0a 2a 2a 20 6f 72 20 6c 61 73 74 20 63 68 69 6c .** or last chil
16747 64 20 6f 66 20 69 74 73 20 70 61 72 65 6e 74 2e d of its parent.
16748 20 20 49 66 20 70 50 61 67 65 20 68 61 73 20 66 If pPage has f
16749 65 77 65 72 20 74 68 61 6e 20 32 2a 4e 4e 20 73 ewer than 2*NN s
1674a 69 62 6c 69 6e 67 73 0a 2a 2a 20 28 73 6f 6d 65 iblings.** (some
1674b 74 68 69 6e 67 20 77 68 69 63 68 20 63 61 6e 20 thing which can
1674c 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 70 only happen if p
1674d 50 61 67 65 20 69 73 20 74 68 65 20 72 6f 6f 74 Page is the root
1674e 20 70 61 67 65 20 6f 72 20 61 20 0a 2a 2a 20 63 page or a .** c
1674f 68 69 6c 64 20 6f 66 20 72 6f 6f 74 29 20 74 68 hild of root) th
16750 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 en all available
16751 20 73 69 62 6c 69 6e 67 73 20 70 61 72 74 69 63 siblings partic
16752 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c ipate in the bal
16753 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 ancing..**.** Th
16754 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c e number of sibl
16755 69 6e 67 73 20 6f 66 20 70 50 61 67 65 20 6d 69 ings of pPage mi
16756 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 65 64 ght be increased
16757 20 6f 72 20 64 65 63 72 65 61 73 65 64 20 62 79 or decreased by
16758 20 6f 6e 65 20 6f 72 0a 2a 2a 20 74 77 6f 20 69 one or.** two i
16759 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b n an effort to k
1675a 65 65 70 20 70 61 67 65 73 20 6e 65 61 72 6c 79 eep pages nearly
1675b 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76 full but not ov
1675c 65 72 20 66 75 6c 6c 2e 20 54 68 65 20 72 6f 6f er full. The roo
1675d 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 73 70 65 t page.** is spe
1675e 63 69 61 6c 20 61 6e 64 20 69 73 20 61 6c 6c 6f cial and is allo
1675f 77 65 64 20 74 6f 20 62 65 20 6e 65 61 72 6c 79 wed to be nearly
16760 20 65 6d 70 74 79 2e 20 49 66 20 70 50 61 67 65 empty. If pPage
16761 20 69 73 20 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 is .** the root
16762 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 page, then the
16763 64 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 depth of the tre
16764 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 e might be incre
16765 61 73 65 64 0a 2a 2a 20 6f 72 20 64 65 63 72 65 ased.** or decre
16766 61 73 65 64 20 62 79 20 6f 6e 65 2c 20 61 73 20 ased by one, as
16767 6e 65 63 65 73 73 61 72 79 2c 20 74 6f 20 6b 65 necessary, to ke
16768 65 70 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 ep the root page
16769 20 66 72 6f 6d 20 62 65 69 6e 67 0a 2a 2a 20 6f from being.** o
1676a 76 65 72 66 75 6c 6c 20 6f 72 20 63 6f 6d 70 6c verfull or compl
1676b 65 74 65 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a etely empty..**.
1676c 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 ** Note that whe
1676d 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
1676e 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f s called, some o
1676f 66 20 74 68 65 20 43 65 6c 6c 73 20 6f 6e 20 70 f the Cells on p
16770 50 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f Page.** might no
16771 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 73 74 t actually be st
16772 6f 72 65 64 20 69 6e 20 70 50 61 67 65 2d 3e 61 ored in pPage->a
16773 44 61 74 61 5b 5d 2e 20 20 54 68 69 73 20 63 61 Data[]. This ca
16774 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74 n happen.** if t
16775 68 65 20 70 61 67 65 20 69 73 20 6f 76 65 72 66 he page is overf
16776 75 6c 6c 2e 20 20 50 61 72 74 20 6f 66 20 74 68 ull. Part of th
16777 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f e job of this ro
16778 75 74 69 6e 65 20 69 73 20 74 6f 0a 2a 2a 20 6d utine is to.** m
16779 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 43 65 6c ake sure all Cel
1677a 6c 73 20 66 6f 72 20 70 50 61 67 65 20 6f 6e 63 ls for pPage onc
1677b 65 20 61 67 61 69 6e 20 66 69 74 20 69 6e 20 70 e again fit in p
1677c 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 2e 0a 2a Page->aData[]..*
1677d 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75 72 *.** In the cour
1677e 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20 se of balancing
1677f 74 68 65 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 the siblings of
16780 70 50 61 67 65 2c 20 74 68 65 20 70 61 72 65 6e pPage, the paren
16781 74 20 6f 66 20 70 50 61 67 65 0a 2a 2a 20 6d 69 t of pPage.** mi
16782 67 68 74 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 ght become overf
16783 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c ull or underfull
16784 2e 20 20 49 66 20 74 68 61 74 20 68 61 70 70 65 . If that happe
16785 6e 73 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f ns, then this ro
16786 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c utine.** is call
16787 65 64 20 72 65 63 75 72 73 69 76 65 6c 79 20 6f ed recursively o
16788 6e 20 74 68 65 20 70 61 72 65 6e 74 2e 0a 2a 2a n the parent..**
16789 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 .** If this rout
1678a 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e ine fails for an
1678b 79 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 y reason, it mig
1678c 68 74 20 6c 65 61 76 65 20 74 68 65 20 64 61 74 ht leave the dat
1678d 61 62 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f abase.** in a co
1678e 72 72 75 70 74 65 64 20 73 74 61 74 65 2e 20 20 rrupted state.
1678f 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 So if this routi
16790 6e 65 20 66 61 69 6c 73 2c 20 74 68 65 20 64 61 ne fails, the da
16791 74 61 62 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a tabase should.**
16792 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e be rolled back.
16793 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
16794 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 42 alance_nonroot(B
16795 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
16796 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
16797 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
16798 2a 20 54 68 65 20 6f 76 65 72 20 6f 72 20 75 6e * The over or un
16799 64 65 72 66 75 6c 6c 20 70 61 67 65 20 74 6f 20 derfull page to
1679a 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 4d 65 6d balance */. Mem
1679b 50 61 67 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 Page *pParent;
1679c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1679d 20 70 61 72 65 6e 74 20 6f 66 20 70 50 61 67 65 parent of pPage
1679e 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a */. BtShared *
1679f 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 pBt;
167a0 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 /* The whole
167a1 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e database */. in
167a2 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 t nCell = 0;
167a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
167a4 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e mber of cells in
167a5 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 apCell[] */. i
167a6 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 nt nMaxCells = 0
167a7 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 ; /* A
167a8 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 llocated size of
167a9 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c apCell, szCell,
167aa 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 aFrom. */. int
167ab 20 6e 4f 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 nOld = 0;
167ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
167ad 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
167ae 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 apOld[] */. int
167af 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20 nNew = 0;
167b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
167b1 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
167b2 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 apNew[] */. int
167b3 20 6e 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 nDiv;
167b4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
167b5 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 ber of cells in
167b6 61 70 44 69 76 5b 5d 20 2a 2f 0a 20 20 69 6e 74 apDiv[] */. int
167b7 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 i, j, k;
167b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
167b9 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 p counters */.
167ba 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 int idx;
167bb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
167bc 49 6e 64 65 78 20 6f 66 20 70 50 61 67 65 20 69 Index of pPage i
167bd 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c n pParent->aCell
167be 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 44 69 [] */. int nxDi
167bf 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
167c0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 64 69 76 /* Next div
167c1 69 64 65 72 20 73 6c 6f 74 20 69 6e 20 70 50 61 ider slot in pPa
167c2 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f rent->aCell[] */
167c3 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
167c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
167c5 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f /* The return co
167c6 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 de */. int leaf
167c7 43 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20 20 20 Correction;
167c8 20 20 20 20 20 2f 2a 20 34 20 69 66 20 70 50 61 /* 4 if pPa
167c9 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 ge is a leaf. 0
167ca 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74 if not */. int
167cb 20 6c 65 61 66 44 61 74 61 3b 20 20 20 20 20 20 leafData;
167cc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
167cd 65 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 e if pPage is a
167ce 6c 65 61 66 20 6f 66 20 61 20 4c 45 41 46 44 41 leaf of a LEAFDA
167cf 54 41 20 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 TA tree */. int
167d0 20 75 73 61 62 6c 65 53 70 61 63 65 3b 20 20 20 usableSpace;
167d1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
167d2 65 73 20 69 6e 20 70 50 61 67 65 20 62 65 79 6f es in pPage beyo
167d3 6e 64 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f nd the header */
167d4 0a 20 20 69 6e 74 20 70 61 67 65 46 6c 61 67 73 . int pageFlags
167d5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
167d6 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 50 61 67 /* Value of pPag
167d7 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f 0a 20 e->aData[0] */.
167d8 20 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b 20 20 int subtotal;
167d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
167da 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 62 79 74 Subtotal of byt
167db 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f es in cells on o
167dc 6e 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ne page */. int
167dd 20 69 53 70 61 63 65 31 20 3d 20 30 3b 20 20 20 iSpace1 = 0;
167de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
167df 73 74 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f st unused byte o
167e0 66 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f 0a 20 f aSpace1[] */.
167e1 20 69 6e 74 20 69 53 70 61 63 65 32 20 3d 20 30 int iSpace2 = 0
167e2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
167e3 20 46 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 First unused by
167e4 74 65 20 6f 66 20 61 53 70 61 63 65 32 5b 5d 20 te of aSpace2[]
167e5 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 */. int szScrat
167e6 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ch;
167e7 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 /* Size of scr
167e8 61 74 63 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 atch memory requ
167e9 65 73 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 ested */. MemPa
167ea 67 65 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 ge *apOld[NB];
167eb 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 /* pPage
167ec 20 61 6e 64 20 75 70 20 74 6f 20 74 77 6f 20 73 and up to two s
167ed 69 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 50 67 6e iblings */. Pgn
167ee 6f 20 70 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b 20 20 o pgnoOld[NB];
167ef 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
167f0 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 e numbers for ea
167f1 63 68 20 70 61 67 65 20 69 6e 20 61 70 4f 6c 64 ch page in apOld
167f2 5b 5d 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 [] */. MemPage
167f3 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 *apCopy[NB];
167f4 20 20 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20 /* Private
167f5 63 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b copies of apOld[
167f6 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d ] pages */. Mem
167f7 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 Page *apNew[NB+2
167f8 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 ]; /* pPa
167f9 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 ge and up to NB
167fa 73 69 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62 siblings after b
167fb 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 50 67 alancing */. Pg
167fc 6e 6f 20 70 67 6e 6f 4e 65 77 5b 4e 42 2b 32 5d no pgnoNew[NB+2]
167fd 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 ; /* Pa
167fe 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 ge numbers for e
167ff 61 63 68 20 70 61 67 65 20 69 6e 20 61 70 4e 65 ach page in apNe
16800 77 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44 w[] */. u8 *apD
16801 69 76 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 iv[NB];
16802 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 65 72 /* Divider
16803 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e cells in pParen
16804 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65 t */. int cntNe
16805 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 w[NB+2];
16806 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 /* Index in
16807 61 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 aCell[] of cell
16808 61 66 74 65 72 20 69 2d 74 68 20 70 61 67 65 20 after i-th page
16809 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e */. int szNew[N
1680a 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 B+2];
1680b 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69 /* Combined si
1680c 7a 65 20 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63 ze of cells plac
1680d 65 20 6f 6e 20 69 2d 74 68 20 70 61 67 65 20 2a e on i-th page *
1680e 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20 /. u8 **apCell
1680f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
16810 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65 /* All cells be
16811 67 69 6e 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a gin balanced */.
16812 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 u16 *szCell;
16813 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16814 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 * Local size of
16815 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 all cells in apC
16816 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 ell[] */. u8 *a
16817 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 Copy[NB];
16818 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 68 /* Space for h
16819 6f 6c 64 69 6e 67 20 64 61 74 61 20 6f 66 20 61 olding data of a
1681a 70 43 6f 70 79 5b 5d 20 2a 2f 0a 20 20 75 38 20 pCopy[] */. u8
1681b 2a 61 53 70 61 63 65 31 3b 20 20 20 20 20 20 20 *aSpace1;
1681c 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 /* Space for
1681d 20 63 6f 70 69 65 73 20 6f 66 20 64 69 76 69 64 copies of divid
1681e 65 72 73 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 ers cells before
1681f 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 balance */. u8
16820 20 2a 61 53 70 61 63 65 32 20 3d 20 30 3b 20 20 *aSpace2 = 0;
16821 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f /* Space fo
16822 72 20 6f 76 65 72 66 6c 6f 77 20 64 69 76 69 64 r overflow divid
16823 65 72 73 20 63 65 6c 6c 73 20 61 66 74 65 72 20 ers cells after
16824 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 balance */. u8
16825 2a 61 46 72 6f 6d 20 3d 20 30 3b 0a 0a 20 20 70 *aFrom = 0;.. p
16826 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
16827 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
16828 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
16829 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1682a 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
1682b 29 20 29 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 ) );. VVA_ONLY(
1682c 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68 75 66 pCur->pagesShuf
1682d 66 6c 65 64 20 3d 20 31 20 29 3b 0a 0a 20 20 2f fled = 1 );.. /
1682e 2a 20 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 * . ** Find the
1682f 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 parent page..
16830 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 */. assert( pCu
16831 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 r->iPage>0 );.
16832 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
16833 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 sInit );. asser
16834 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
16835 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
16836 2d 3e 70 44 62 50 61 67 65 29 20 7c 7c 20 70 50 ->pDbPage) || pP
16837 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d age->nOverflow==
16838 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 1 );. pBt = pPa
16839 67 65 2d 3e 70 42 74 3b 0a 20 20 70 50 61 72 65 ge->pBt;. pPare
1683a 6e 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 nt = pCur->apPag
1683b 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d e[pCur->iPage-1]
1683c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 ;. assert( pPar
1683d 65 6e 74 20 29 3b 0a 20 20 69 66 28 20 53 51 4c ent );. if( SQL
1683e 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 ITE_OK!=(rc = sq
1683f 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
16840 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 pParent->pDbPage
16841 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 )) ){. goto b
16842 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a alance_cleanup;.
16843 20 20 7d 0a 0a 20 20 54 52 41 43 45 28 28 22 42 }.. TRACE(("B
16844 41 4c 41 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 ALANCE: begin pa
16845 67 65 20 25 64 20 63 68 69 6c 64 20 6f 66 20 25 ge %d child of %
16846 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e d\n", pPage->pgn
16847 6f 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f o, pParent->pgno
16848 29 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ));..#ifndef SQL
16849 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 ITE_OMIT_QUICKBA
1684a 4c 41 4e 43 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 LANCE. /*. **
1684b 41 20 73 70 65 63 69 61 6c 20 63 61 73 65 3a 20 A special case:
1684c 20 49 66 20 61 20 6e 65 77 20 65 6e 74 72 79 20 If a new entry
1684d 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 69 6e has just been in
1684e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 0a 20 20 serted into a.
1684f 2a 2a 20 74 61 62 6c 65 20 28 74 68 61 74 20 69 ** table (that i
16850 73 2c 20 61 20 62 74 72 65 65 20 77 69 74 68 20 s, a btree with
16851 69 6e 74 65 67 65 72 20 6b 65 79 73 20 61 6e 64 integer keys and
16852 20 61 6c 6c 20 64 61 74 61 20 61 74 20 74 68 65 all data at the
16853 20 6c 65 61 76 65 73 29 0a 20 20 2a 2a 20 61 6e leaves). ** an
16854 64 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 d the new entry
16855 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 is the right-mos
16856 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 t entry in the t
16857 72 65 65 20 28 69 74 20 68 61 73 20 74 68 65 0a ree (it has the.
16858 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b 65 79 ** largest key
16859 29 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 73 ) then use the s
1685a 70 65 63 69 61 6c 20 62 61 6c 61 6e 63 65 5f 71 pecial balance_q
1685b 75 69 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 66 uick() routine f
1685c 6f 72 0a 20 20 2a 2a 20 62 61 6c 61 6e 63 69 6e or. ** balancin
1685d 67 2e 20 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 g. balance_quic
1685e 6b 28 29 20 69 73 20 6d 75 63 68 20 66 61 73 74 k() is much fast
1685f 65 72 20 61 6e 64 20 72 65 73 75 6c 74 73 20 69 er and results i
16860 6e 20 61 20 74 69 67 68 74 65 72 0a 20 20 2a 2a n a tighter. **
16861 20 70 61 63 6b 69 6e 67 20 6f 66 20 64 61 74 61 packing of data
16862 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 in the common c
16863 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ase.. */. if(
16864 70 50 61 67 65 2d 3e 6c 65 61 66 20 26 26 0a 20 pPage->leaf &&.
16865 20 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b pPage->intK
16866 65 79 20 26 26 0a 20 20 20 20 20 20 70 50 61 67 ey &&. pPag
16867 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 e->nOverflow==1
16868 26 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e &&. pPage->
16869 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 aOvfl[0].idx==pP
1686a 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 0a 20 20 age->nCell &&.
1686b 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e pParent->pgn
1686c 6f 21 3d 31 20 26 26 0a 20 20 20 20 20 20 67 65 o!=1 &&. ge
1686d 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d t4byte(&pParent-
1686e 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e >aData[pParent->
1686f 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 70 hdrOffset+8])==p
16870 50 61 67 65 2d 3e 70 67 6e 6f 0a 20 20 29 7b 0a Page->pgno. ){.
16871 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
16872 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 e->intKey );.
16873 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 4f 44 4f 3a /*. ** TODO:
16874 20 43 68 65 63 6b 20 74 68 65 20 73 69 62 6c 69 Check the sibli
16875 6e 67 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 ngs to the left
16876 6f 66 20 70 50 61 67 65 2e 20 49 74 20 6d 61 79 of pPage. It may
16877 20 62 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 be that. **
16878 74 68 65 79 20 61 72 65 20 6e 6f 74 20 66 75 6c they are not ful
16879 6c 20 61 6e 64 20 6e 6f 20 6e 65 77 20 70 61 67 l and no new pag
1687a 65 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 e is required..
1687b 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e */. return
1687c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 70 balance_quick(p
1687d 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 Cur);. }.#endif
1687e 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f .. if( SQLITE_O
1687f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K!=(rc = sqlite3
16880 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
16881 2d 3e 70 44 62 50 61 67 65 29 29 20 29 7b 0a 20 ->pDbPage)) ){.
16882 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f goto balance_
16883 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 cleanup;. }..
16884 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 /*. ** Find the
16885 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 cell in the par
16886 65 6e 74 20 70 61 67 65 20 77 68 6f 73 65 20 6c ent page whose l
16887 65 66 74 20 63 68 69 6c 64 20 70 6f 69 6e 74 73 eft child points
16888 20 62 61 63 6b 0a 20 20 2a 2a 20 74 6f 20 70 50 back. ** to pP
16889 61 67 65 2e 20 20 54 68 65 20 22 69 64 78 22 20 age. The "idx"
1688a 76 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 variable is the
1688b 69 6e 64 65 78 20 6f 66 20 74 68 61 74 20 63 65 index of that ce
1688c 6c 6c 2e 20 20 49 66 20 70 50 61 67 65 0a 20 20 ll. If pPage.
1688d 2a 2a 20 69 73 20 74 68 65 20 72 69 67 68 74 6d ** is the rightm
1688e 6f 73 74 20 63 68 69 6c 64 20 6f 66 20 70 50 61 ost child of pPa
1688f 72 65 6e 74 20 74 68 65 6e 20 73 65 74 20 69 64 rent then set id
16890 78 20 74 6f 20 70 50 61 72 65 6e 74 2d 3e 6e 43 x to pParent->nC
16891 65 6c 6c 20 0a 20 20 2a 2f 0a 20 20 69 64 78 20 ell . */. idx
16892 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 = pCur->aiIdx[pC
16893 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 ur->iPage-1];.
16894 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 assertParentInde
16895 78 28 70 50 61 72 65 6e 74 2c 20 69 64 78 2c 20 x(pParent, idx,
16896 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 pPage->pgno);..
16897 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 73 69 /*. ** Find si
16898 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f 20 70 bling pages to p
16899 50 61 67 65 20 61 6e 64 20 74 68 65 20 63 65 6c Page and the cel
1689a 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 74 68 ls in pParent th
1689b 61 74 20 64 69 76 69 64 65 0a 20 20 2a 2a 20 74 at divide. ** t
1689c 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 20 41 6e he siblings. An
1689d 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
1689e 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c to find NN sibl
1689f 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 0a 20 ings on either.
168a0 20 2a 2a 20 73 69 64 65 20 6f 66 20 70 50 61 67 ** side of pPag
168a1 65 2e 20 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 e. More sibling
168a2 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d s are taken from
168a3 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76 one side, howev
168a4 65 72 2c 20 69 66 0a 20 20 2a 2a 20 70 50 61 67 er, if. ** pPag
168a5 65 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65 e there are fewe
168a6 72 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e r than NN siblin
168a7 67 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 gs on the other
168a8 73 69 64 65 2e 20 20 49 66 20 70 50 61 72 65 6e side. If pParen
168a9 74 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72 t. ** has NB or
168aa 20 66 65 77 65 72 20 63 68 69 6c 64 72 65 6e 20 fewer children
168ab 74 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 then all childre
168ac 6e 20 6f 66 20 70 50 61 72 65 6e 74 20 61 72 65 n of pParent are
168ad 20 74 61 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 6e taken.. */. n
168ae 78 44 69 76 20 3d 20 69 64 78 20 2d 20 4e 4e 3b xDiv = idx - NN;
168af 0a 20 20 69 66 28 20 6e 78 44 69 76 20 2b 20 4e . if( nxDiv + N
168b0 42 20 3e 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 B > pParent->nCe
168b1 6c 6c 20 29 7b 0a 20 20 20 20 6e 78 44 69 76 20 ll ){. nxDiv
168b2 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c = pParent->nCell
168b3 20 2d 20 4e 42 20 2b 20 31 3b 0a 20 20 7d 0a 20 - NB + 1;. }.
168b4 20 69 66 28 20 6e 78 44 69 76 3c 30 20 29 7b 0a if( nxDiv<0 ){.
168b5 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 nxDiv = 0;.
168b6 20 7d 0a 20 20 6e 44 69 76 20 3d 20 30 3b 0a 20 }. nDiv = 0;.
168b7 20 66 6f 72 28 69 3d 30 2c 20 6b 3d 6e 78 44 69 for(i=0, k=nxDi
168b8 76 3b 20 69 3c 4e 42 3b 20 69 2b 2b 2c 20 6b 2b v; i<NB; i++, k+
168b9 2b 29 7b 0a 20 20 20 20 69 66 28 20 6b 3c 70 50 +){. if( k<pP
168ba 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a arent->nCell ){.
168bb 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d apDiv[i] =
168bc 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e findCell(pParen
168bd 74 2c 20 6b 29 3b 0a 20 20 20 20 20 20 6e 44 69 t, k);. nDi
168be 76 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 v++;. asser
168bf 74 28 20 21 70 50 61 72 65 6e 74 2d 3e 6c 65 61 t( !pParent->lea
168c0 66 20 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4f f );. pgnoO
168c1 6c 64 5b 69 5d 20 3d 20 67 65 74 34 62 79 74 65 ld[i] = get4byte
168c2 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 (apDiv[i]);.
168c3 7d 65 6c 73 65 20 69 66 28 20 6b 3d 3d 70 50 61 }else if( k==pPa
168c4 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 rent->nCell ){.
168c5 20 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 pgnoOld[i]
168c6 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 = get4byte(&pPar
168c7 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 ent->aData[pPare
168c8 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d nt->hdrOffset+8]
168c9 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
168ca 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
168cb 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64 . rc = getAnd
168cc 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 67 InitPage(pBt, pg
168cd 6e 6f 4f 6c 64 5b 69 5d 2c 20 26 61 70 4f 6c 64 noOld[i], &apOld
168ce 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 [i]);. if( rc
168cf 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f ) goto balance_
168d0 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 2f 2a 20 cleanup;. /*
168d1 61 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50 61 72 apOld[i]->idxPar
168d2 65 6e 74 20 3d 20 6b 3b 20 2a 2f 0a 20 20 20 20 ent = k; */.
168d3 61 70 43 6f 70 79 5b 69 5d 20 3d 20 30 3b 0a 20 apCopy[i] = 0;.
168d4 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 4f assert( i==nO
168d5 6c 64 20 29 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b ld );. nOld++
168d6 3b 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 ;. nMaxCells
168d7 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e += 1+apOld[i]->n
168d8 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e Cell+apOld[i]->n
168d9 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20 Overflow;. }..
168da 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c /* Make nMaxCel
168db 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 ls a multiple of
168dc 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 4 in order to p
168dd 72 65 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20 reserve 8-byte.
168de 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f ** alignment */
168df 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 . nMaxCells = (
168e0 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e nMaxCells + 3)&~
168e1 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 3;.. /*. ** Al
168e2 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 locate space for
168e3 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 memory structur
168e4 65 73 0a 20 20 2a 2f 0a 20 20 73 7a 53 63 72 61 es. */. szScra
168e5 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61 tch =. nMa
168e6 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38 xCells*sizeof(u8
168e7 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *)
168e8 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65 /* apCe
168e9 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 ll */. + nMa
168ea 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31 xCells*sizeof(u1
168eb 36 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6)
168ec 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 65 /* szCe
168ed 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 28 52 4f ll */. + (RO
168ee 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 UND8(sizeof(MemP
168ef 61 67 65 29 29 2b 70 42 74 2d 3e 70 61 67 65 53 age))+pBt->pageS
168f0 69 7a 65 29 2a 4e 42 20 20 2f 2a 20 61 43 6f 70 ize)*NB /* aCop
168f1 79 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d y */. + pBt-
168f2 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20 20 20 >pageSize
168f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
168f4 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 63 /* aSpac
168f5 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 28 49 53 e1 */. + (IS
168f6 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 6e 4d 61 AUTOVACUUM ? nMa
168f7 78 43 65 6c 6c 73 20 3a 20 30 29 3b 20 20 20 20 xCells : 0);
168f8 20 20 20 20 20 20 20 20 20 2f 2a 20 61 46 72 6f /* aFro
168f9 6d 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 m */. apCell =
168fa 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 sqlite3ScratchMa
168fb 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68 20 lloc( szScratch
168fc 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c 6c ); . if( apCell
168fd 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ==0 ){. rc =
168fe 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
168ff 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 goto balance_c
16900 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a leanup;. }. sz
16901 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 Cell = (u16*)&ap
16902 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b Cell[nMaxCells];
16903 0a 20 20 61 43 6f 70 79 5b 30 5d 20 3d 20 28 75 . aCopy[0] = (u
16904 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 8*)&szCell[nMaxC
16905 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 ells];. assert(
16906 20 28 28 61 43 6f 70 79 5b 30 5d 20 2d 20 28 75 ((aCopy[0] - (u
16907 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 29 3b 8*)0) & 7)==0 );
16908 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67 6e /* 8-byte align
16909 6d 65 6e 74 20 72 65 71 75 69 72 65 64 20 2a 2f ment required */
1690a 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4e 42 . for(i=1; i<NB
1690b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 43 6f 70 ; i++){. aCop
1690c 79 5b 69 5d 20 3d 20 26 61 43 6f 70 79 5b 69 2d y[i] = &aCopy[i-
1690d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 1][pBt->pageSize
1690e 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d +ROUND8(sizeof(M
1690f 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 20 20 61 emPage))];. a
16910 73 73 65 72 74 28 20 28 28 61 43 6f 70 79 5b 69 ssert( ((aCopy[i
16911 5d 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29 ] - (u8*)0) & 7)
16912 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 ==0 ); /* 8-byte
16913 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 alignment requi
16914 72 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 53 70 red */. }. aSp
16915 61 63 65 31 20 3d 20 26 61 43 6f 70 79 5b 4e 42 ace1 = &aCopy[NB
16916 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a -1][pBt->pageSiz
16917 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 e+ROUND8(sizeof(
16918 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 61 73 MemPage))];. as
16919 73 65 72 74 28 20 28 28 61 53 70 61 63 65 31 20 sert( ((aSpace1
1691a 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d 3d - (u8*)0) & 7)==
1691b 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61 0 ); /* 8-byte a
1691c 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 lignment require
1691d 64 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 55 54 d */. if( ISAUT
1691e 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 61 OVACUUM ){. a
1691f 46 72 6f 6d 20 3d 20 26 61 53 70 61 63 65 31 5b From = &aSpace1[
16920 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 3b 0a pBt->pageSize];.
16921 20 20 7d 0a 20 20 61 53 70 61 63 65 32 20 3d 20 }. aSpace2 =
16922 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f sqlite3PageMallo
16923 63 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 c(pBt->pageSize)
16924 3b 0a 20 20 69 66 28 20 61 53 70 61 63 65 32 3d ;. if( aSpace2=
16925 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 =0 ){. rc = S
16926 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
16927 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
16928 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 0a 20 20 eanup;. }. .
16929 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20 63 6f 70 /*. ** Make cop
1692a 69 65 73 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 ies of the conte
1692b 6e 74 20 6f 66 20 70 50 61 67 65 20 61 6e 64 20 nt of pPage and
1692c 69 74 73 20 73 69 62 6c 69 6e 67 73 20 69 6e 74 its siblings int
1692d 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a 2a 20 54 o aOld[].. ** T
1692e 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 73 20 he rest of this
1692f 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 75 73 function will us
16930 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 e data from the
16931 63 6f 70 69 65 73 20 72 61 74 68 65 72 0a 20 20 copies rather.
16932 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 ** that the orig
16933 69 6e 61 6c 20 70 61 67 65 73 20 73 69 6e 63 65 inal pages since
16934 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 the original pa
16935 67 65 73 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 ges will be in t
16936 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 he. ** process
16937 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 of being overwri
16938 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 tten.. */. for
16939 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b (i=0; i<nOld; i+
1693a 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 +){. MemPage
1693b 2a 70 20 3d 20 61 70 43 6f 70 79 5b 69 5d 20 3d *p = apCopy[i] =
1693c 20 28 4d 65 6d 50 61 67 65 2a 29 61 43 6f 70 79 (MemPage*)aCopy
1693d 5b 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 [i];. memcpy(
1693e 70 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a p, apOld[i], siz
1693f 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 eof(MemPage));.
16940 20 20 20 70 2d 3e 61 44 61 74 61 20 3d 20 28 76 p->aData = (v
16941 6f 69 64 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 oid*)&p[1];.
16942 6d 65 6d 63 70 79 28 70 2d 3e 61 44 61 74 61 2c memcpy(p->aData,
16943 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74 61 apOld[i]->aData
16944 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 , pBt->pageSize)
16945 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a ;. }.. /*. **
16946 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20 74 Load pointers t
16947 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 73 o all cells on s
16948 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e 64 ibling pages and
16949 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c the divider cel
1694a 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 ls. ** into the
1694b 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d 20 local apCell[]
1694c 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f 70 array. Make cop
1694d 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 69 64 ies of the divid
1694e 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e er cells. ** in
1694f 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 to space obtaine
16950 64 20 66 6f 72 6d 20 61 53 70 61 63 65 31 5b 5d d form aSpace1[]
16951 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20 and remove the
16952 74 68 65 20 64 69 76 69 64 65 72 20 43 65 6c 6c the divider Cell
16953 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72 s. ** from pPar
16954 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 ent.. **. ** I
16955 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 f the siblings a
16956 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 73 re on leaf pages
16957 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64 , then the child
16958 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 65 pointers of the
16959 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63 65 . ** divider ce
1695a 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 65 64 lls are stripped
1695b 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20 from the cells
1695c 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 before they are
1695d 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f copied. ** into
1695e 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e 20 aSpace1[]. In
1695f 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 65 this way, all ce
16960 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 lls in apCell[]
16961 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a are without. **
16962 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e child pointers.
16963 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61 72 If siblings ar
16964 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 e not leaves, th
16965 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 en all cell in.
16966 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 ** apCell[] inc
16967 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 lude child point
16968 65 72 73 2e 20 20 45 69 74 68 65 72 20 77 61 79 ers. Either way
16969 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 , all cells in a
1696a 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 pCell[]. ** are
1696b 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a alike.. **. *
1696c 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e * leafCorrection
1696d 3a 20 20 34 20 69 66 20 70 50 61 67 65 20 69 73 : 4 if pPage is
1696e 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 70 a leaf. 0 if p
1696f 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 Page is not a le
16970 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c af.. ** l
16971 65 61 66 44 61 74 61 3a 20 20 31 20 69 66 20 70 eafData: 1 if p
16972 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64 Page holds key+d
16973 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74 20 ata and pParent
16974 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e holds only keys.
16975 0a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20 3d 20 . */. nCell =
16976 30 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74 0;. leafCorrect
16977 69 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 ion = pPage->lea
16978 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 20 f*4;. leafData
16979 3d 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 = pPage->hasData
1697a 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e ;. for(i=0; i<n
1697b 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d Old; i++){. M
1697c 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 emPage *pOld = a
1697d 70 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 69 6e pCopy[i];. in
1697e 74 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e t limit = pOld->
1697f 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 nCell+pOld->nOve
16980 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28 6a rflow;. for(j
16981 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b =0; j<limit; j++
16982 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
16983 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 nCell<nMaxCells
16984 20 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c 6c );. apCell
16985 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76 [nCell] = findOv
16986 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c erflowCell(pOld,
16987 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c j);. szCel
16988 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53 l[nCell] = cellS
16989 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70 43 izePtr(pOld, apC
1698a 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 20 20 ell[nCell]);.
1698b 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
1698c 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 UUM ){. i
1698d 6e 74 20 61 3b 0a 20 20 20 20 20 20 20 20 61 46 nt a;. aF
1698e 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 28 75 38 rom[nCell] = (u8
1698f 29 69 3b 20 20 20 61 73 73 65 72 74 28 20 69 3e )i; assert( i>
16990 3d 30 20 26 26 20 69 3c 36 20 29 3b 0a 20 20 20 =0 && i<6 );.
16991 20 20 20 20 20 66 6f 72 28 61 3d 30 3b 20 61 3c for(a=0; a<
16992 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b pOld->nOverflow;
16993 20 61 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 a++){.
16994 20 69 66 28 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c if( pOld->aOvfl
16995 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61 70 43 65 6c [a].pCell==apCel
16996 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a 20 20 20 20 l[nCell] ){.
16997 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43 aFrom[nC
16998 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20 ell] = 0xFF;.
16999 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
1699a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1699b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1699c 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 nCell++;.
1699d 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c }. if( i<nOl
1699e 64 2d 31 20 29 7b 0a 20 20 20 20 20 20 75 31 36 d-1 ){. u16
1699f 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 sz = cellSizePt
169a0 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 69 76 r(pParent, apDiv
169a1 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 [i]);. if(
169a2 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 leafData ){.
169a3 20 20 20 20 2f 2a 20 57 69 74 68 20 74 68 65 20 /* With the
169a4 4c 45 41 46 44 41 54 41 20 66 6c 61 67 2c 20 70 LEAFDATA flag, p
169a5 50 61 72 65 6e 74 20 63 65 6c 6c 73 20 68 6f 6c Parent cells hol
169a6 64 20 6f 6e 6c 79 20 49 4e 54 4b 45 59 73 20 74 d only INTKEYs t
169a7 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 hat. ** a
169a8 72 65 20 64 75 70 6c 69 63 61 74 65 73 20 6f 66 re duplicates of
169a9 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 63 68 69 keys on the chi
169aa 6c 64 20 70 61 67 65 73 2e 20 20 57 65 20 6e 65 ld pages. We ne
169ab 65 64 20 74 6f 20 72 65 6d 6f 76 65 0a 20 20 20 ed to remove.
169ac 20 20 20 20 20 2a 2a 20 74 68 65 20 64 69 76 69 ** the divi
169ad 64 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70 der cells from p
169ae 50 61 72 65 6e 74 2c 20 62 75 74 20 74 68 65 20 Parent, but the
169af 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 61 dividers cells a
169b0 72 65 20 6e 6f 74 0a 20 20 20 20 20 20 20 20 2a re not. *
169b1 2a 20 61 64 64 65 64 20 74 6f 20 61 70 43 65 6c * added to apCel
169b2 6c 5b 5d 20 62 65 63 61 75 73 65 20 74 68 65 79 l[] because they
169b3 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73 20 are duplicates
169b4 6f 66 20 63 68 69 6c 64 20 63 65 6c 6c 73 2e 0a of child cells..
169b5 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
169b6 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 72 dropCell(pPar
169b7 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 7a 29 3b ent, nxDiv, sz);
169b8 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
169b9 20 20 20 20 20 20 75 38 20 2a 70 54 65 6d 70 3b u8 *pTemp;
169ba 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
169bb 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 nCell<nMaxCells
169bc 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 65 );. szCe
169bd 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 3b 0a ll[nCell] = sz;.
169be 20 20 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 pTemp =
169bf 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 65 31 &aSpace1[iSpace1
169c0 5d 3b 0a 20 20 20 20 20 20 20 20 69 53 70 61 63 ];. iSpac
169c1 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 e1 += sz;.
169c2 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 assert( sz<=pB
169c3 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b t->pageSize/4 );
169c4 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
169c5 20 69 53 70 61 63 65 31 3c 3d 70 42 74 2d 3e 70 iSpace1<=pBt->p
169c6 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 ageSize );.
169c7 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c memcpy(pTemp,
169c8 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a apDiv[i], sz);.
169c9 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e apCell[n
169ca 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b 6c 65 Cell] = pTemp+le
169cb 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 afCorrection;.
169cc 20 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f if( ISAUTO
169cd 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 VACUUM ){.
169ce 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d aFrom[nCell]
169cf 20 3d 20 30 78 46 46 3b 0a 20 20 20 20 20 20 20 = 0xFF;.
169d0 20 7d 0a 20 20 20 20 20 20 20 20 64 72 6f 70 43 }. dropC
169d1 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 ell(pParent, nxD
169d2 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 20 iv, sz);.
169d3 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 assert( leafCor
169d4 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 rection==0 || le
169d5 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 afCorrection==4
169d6 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 65 6c );. szCel
169d7 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 28 75 31 36 l[nCell] -= (u16
169d8 29 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b )leafCorrection;
169d9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
169da 20 67 65 74 34 62 79 74 65 28 70 54 65 6d 70 29 get4byte(pTemp)
169db 3d 3d 70 67 6e 6f 4f 6c 64 5b 69 5d 20 29 3b 0a ==pgnoOld[i] );.
169dc 20 20 20 20 20 20 20 20 69 66 28 20 21 70 4f 6c if( !pOl
169dd 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 d->leaf ){.
169de 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 assert( lea
169df 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 29 fCorrection==0 )
169e0 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ;. /* T
169e1 68 65 20 72 69 67 68 74 20 70 6f 69 6e 74 65 72 he right pointer
169e2 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 61 of the child pa
169e3 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d 65 73 20 ge pOld becomes
169e4 74 68 65 20 6c 65 66 74 0a 20 20 20 20 20 20 20 the left.
169e5 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 ** pointer of
169e6 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c the divider cel
169e7 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d l */. m
169e8 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43 65 emcpy(apCell[nCe
169e9 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61 74 ll], &pOld->aDat
169ea 61 5b 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73 65 a[pOld->hdrOffse
169eb 74 2b 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 t+8], 4);.
169ec 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
169ed 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 assert( leafC
169ee 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a orrection==4 );.
169ef 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 7a if( sz
169f0 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b Cell[nCell]<4 ){
169f1 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 . /*
169f2 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 Do not allow any
169f3 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 72 20 74 cells smaller t
169f4 68 61 6e 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a han 4 bytes. */.
169f5 20 20 20 20 20 20 20 20 20 20 20 20 73 7a 43 65 szCe
169f6 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 3b 0a 20 ll[nCell] = 4;.
169f7 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
169f8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 43 65 }. nCe
169f9 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ll++;. }.
169fa 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 }. }.. /*.
169fb 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 ** Figure out th
169fc 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
169fd 73 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 s needed to hold
169fe 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 all nCell cells
169ff 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20 74 68 69 .. ** Store thi
16a00 73 20 6e 75 6d 62 65 72 20 69 6e 20 22 6b 22 2e s number in "k".
16a01 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74 65 20 73 Also compute s
16a02 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20 zNew[] which is
16a03 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a 2a 20 73 the total. ** s
16a04 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 ize of all cells
16a05 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 70 61 67 on the i-th pag
16a06 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b 5d 20 77 e and cntNew[] w
16a07 68 69 63 68 20 69 73 20 74 68 65 20 69 6e 64 65 hich is the inde
16a08 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43 65 6c 6c x. ** in apCell
16a09 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 [] of the cell t
16a0a 68 61 74 20 64 69 76 69 64 65 73 20 70 61 67 65 hat divides page
16a0b 20 69 20 66 72 6f 6d 20 70 61 67 65 20 69 2b 31 i from page i+1
16a0c 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e 65 77 5b . . ** cntNew[
16a0d 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75 61 6c 20 k] should equal
16a0e 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a nCell.. **. **
16a0f 20 56 61 6c 75 65 73 20 63 6f 6d 70 75 74 65 64 Values computed
16a10 20 62 79 20 74 68 69 73 20 62 6c 6f 63 6b 3a 0a by this block:.
16a11 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 **. **
16a12 20 20 20 20 6b 3a 20 54 68 65 20 74 6f 74 61 6c k: The total
16a13 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 number of sibli
16a14 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a 20 20 20 ng pages. **
16a15 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 61 63 65 szNew[i]: Space
16a16 64 20 75 73 65 64 20 6f 6e 20 74 68 65 20 69 2d d used on the i-
16a17 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e th sibling page.
16a18 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65 77 5b 69 . ** cntNew[i
16a19 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61 70 43 65 ]: Index in apCe
16a1a 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 6c 6c 5b ll[] and szCell[
16a1b 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 ] for the first
16a1c 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20 20 20 cell to. **
16a1d 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72 69 the ri
16a1e 67 68 74 20 6f 66 20 74 68 65 20 69 2d 74 68 20 ght of the i-th
16a1f 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 sibling page..
16a20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63 65 3a 20 ** usableSpace:
16a21 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
16a22 6f 66 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 of space availab
16a23 6c 65 20 6f 6e 20 65 61 63 68 20 73 69 62 6c 69 le on each sibli
16a24 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 ng.. ** . */.
16a25 20 75 73 61 62 6c 65 53 70 61 63 65 20 3d 20 70 usableSpace = p
16a26 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d Bt->usableSize -
16a27 20 31 32 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 12 + leafCorrec
16a28 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 75 62 74 tion;. for(subt
16a29 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 otal=k=i=0; i<nC
16a2a 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 ell; i++){. a
16a2b 73 73 65 72 74 28 20 69 3c 6e 4d 61 78 43 65 6c ssert( i<nMaxCel
16a2c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62 74 6f 74 ls );. subtot
16a2d 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 al += szCell[i]
16a2e 2b 20 32 3b 0a 20 20 20 20 69 66 28 20 73 75 62 + 2;. if( sub
16a2f 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c 65 53 70 total > usableSp
16a30 61 63 65 20 29 7b 0a 20 20 20 20 20 20 73 7a 4e ace ){. szN
16a31 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c ew[k] = subtotal
16a32 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 - szCell[i];.
16a33 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 cntNew[k] =
16a34 69 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 61 i;. if( lea
16a35 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a fData ){ i--; }.
16a36 20 20 20 20 20 20 73 75 62 74 6f 74 61 6c 20 3d subtotal =
16a37 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b 0a 20 0;. k++;.
16a38 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 }. }. szNew
16a39 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 3b 0a [k] = subtotal;.
16a3a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 cntNew[k] = nC
16a3b 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f ell;. k++;.. /
16a3c 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 63 6b 69 *. ** The packi
16a3d 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74 ng computed by t
16a3e 68 65 20 70 72 65 76 69 6f 75 73 20 62 6c 6f 63 he previous bloc
16a3f 6b 20 69 73 20 62 69 61 73 65 64 20 74 6f 77 61 k is biased towa
16a40 72 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73 0a rd the siblings.
16a41 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 66 74 ** on the left
16a42 20 73 69 64 65 2e 20 20 54 68 65 20 6c 65 66 74 side. The left
16a43 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 61 6c siblings are al
16a44 77 61 79 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c ways nearly full
16a45 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 20 2a 2a , while the. **
16a46 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c right-most sibl
16a47 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6e 65 61 ing might be nea
16a48 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73 rly empty. This
16a49 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 61 block of code a
16a4a 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 74 6f 20 ttempts. ** to
16a4b 61 64 6a 75 73 74 20 74 68 65 20 70 61 63 6b 69 adjust the packi
16a4c 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 74 ng of siblings t
16a4d 6f 20 67 65 74 20 61 20 62 65 74 74 65 72 20 62 o get a better b
16a4e 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a alance.. **. *
16a4f 2a 20 54 68 69 73 20 61 64 6a 75 73 74 6d 65 6e * This adjustmen
16a50 74 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20 61 t is more than a
16a51 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 n optimization.
16a52 20 54 68 65 20 70 61 63 6b 69 6e 67 20 61 62 6f The packing abo
16a53 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 ve might. ** be
16a54 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 6c 61 6e so out of balan
16a55 63 65 20 61 73 20 74 6f 20 62 65 20 69 6c 6c 65 ce as to be ille
16a56 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c gal. For exampl
16a57 65 2c 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 e, the right-mos
16a58 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 6d t. ** sibling m
16a59 69 67 68 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 ight be complete
16a5a 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73 20 ly empty. This
16a5b 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6e 6f adjustment is no
16a5c 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f t optional.. */
16a5d 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e . for(i=k-1; i>
16a5e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 0; i--){. int
16a5f 20 73 7a 52 69 67 68 74 20 3d 20 73 7a 4e 65 77 szRight = szNew
16a60 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 20 6f 66 [i]; /* Size of
16a61 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 sibling on the
16a62 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 69 6e 74 right */. int
16a63 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e 65 77 5b szLeft = szNew[
16a64 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 i-1]; /* Size of
16a65 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 sibling on the
16a66 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 left */. int
16a67 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
16a68 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 69 67 68 /* Index of righ
16a69 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 6e 20 6c t-most cell in l
16a6a 65 66 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 eft sibling */.
16a6b 20 20 20 69 6e 74 20 64 3b 20 20 20 20 20 20 20 int d;
16a6c 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
16a6d 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 74 6f of first cell to
16a6e 20 74 68 65 20 6c 65 66 74 20 6f 66 20 72 69 67 the left of rig
16a6f 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 ht sibling */..
16a70 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d r = cntNew[i-
16a71 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 20 3d 20 1] - 1;. d =
16a72 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 61 r + 1 - leafData
16a73 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 3c ;. assert( d<
16a74 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 nMaxCells );.
16a75 20 61 73 73 65 72 74 28 20 72 3c 6e 4d 61 78 43 assert( r<nMaxC
16a76 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 68 69 6c ells );. whil
16a77 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 20 7c 7c e( szRight==0 ||
16a78 20 73 7a 52 69 67 68 74 2b 73 7a 43 65 6c 6c 5b szRight+szCell[
16a79 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a d]+2<=szLeft-(sz
16a7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 Cell[r]+2) ){.
16a7b 20 20 20 20 73 7a 52 69 67 68 74 20 2b 3d 20 73 szRight += s
16a7c 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 zCell[d] + 2;.
16a7d 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d 20 73 7a szLeft -= sz
16a7e 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 Cell[r] + 2;.
16a7f 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d cntNew[i-1]--
16a80 3b 0a 20 20 20 20 20 20 72 20 3d 20 63 6e 74 4e ;. r = cntN
16a81 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 ew[i-1] - 1;.
16a82 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c d = r + 1 - l
16a83 65 61 66 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 eafData;. }.
16a84 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a szNew[i] = sz
16a85 52 69 67 68 74 3b 0a 20 20 20 20 73 7a 4e 65 77 Right;. szNew
16a86 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a [i-1] = szLeft;.
16a87 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74 68 65 72 }.. /* Either
16a88 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 we found one or
16a89 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 63 6e 74 more cells (cnt
16a8a 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 20 77 65 new[0])>0) or we
16a8b 20 61 72 65 20 74 68 65 0a 20 20 2a 2a 20 61 20 are the. ** a
16a8c 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 virtual root pag
16a8d 65 2e 20 20 41 20 76 69 72 74 75 61 6c 20 72 6f e. A virtual ro
16a8e 6f 74 20 70 61 67 65 20 69 73 20 77 68 65 6e 20 ot page is when
16a8f 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 0a 20 20 the real root.
16a90 2a 2a 20 70 61 67 65 20 69 73 20 70 61 67 65 20 ** page is page
16a91 31 20 61 6e 64 20 77 65 20 61 72 65 20 74 68 65 1 and we are the
16a92 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f 66 20 74 only child of t
16a93 68 61 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 hat page.. */.
16a94 20 61 73 73 65 72 74 28 20 63 6e 74 4e 65 77 5b assert( cntNew[
16a95 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72 65 6e 74 0]>0 || (pParent
16a96 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20 70 50 61 ->pgno==1 && pPa
16a97 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 29 20 rent->nCell==0)
16a98 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c );.. /*. ** Al
16a99 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 70 61 67 locate k new pag
16a9a 65 73 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 70 es. Reuse old p
16a9b 61 67 65 73 20 77 68 65 72 65 20 70 6f 73 73 69 ages where possi
16a9c 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ble.. */. asse
16a9d 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3e rt( pPage->pgno>
16a9e 31 20 29 3b 0a 20 20 70 61 67 65 46 6c 61 67 73 1 );. pageFlags
16a9f 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b = pPage->aData[
16aa0 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0];. for(i=0; i
16aa1 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 <k; i++){. Me
16aa2 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 20 mPage *pNew;.
16aa3 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 if( i<nOld ){.
16aa4 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e 65 pNew = apNe
16aa5 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69 5d 3b w[i] = apOld[i];
16aa6 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69 . pgnoNew[i
16aa7 5d 20 3d 20 70 67 6e 6f 4f 6c 64 5b 69 5d 3b 0a ] = pgnoOld[i];.
16aa8 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d apOld[i] =
16aa9 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 0;. rc = s
16aaa 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
16aab 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 3b (pNew->pDbPage);
16aac 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 20 . nNew++;.
16aad 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f if( rc ) go
16aae 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
16aaf 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 up;. }else{.
16ab0 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 30 assert( i>0
16ab1 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 );. rc = a
16ab2 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 llocateBtreePage
16ab3 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 (pBt, &pNew, &pg
16ab4 6e 6f 4e 65 77 5b 69 5d 2c 20 70 67 6e 6f 4e 65 noNew[i], pgnoNe
16ab5 77 5b 69 2d 31 5d 2c 20 30 29 3b 0a 20 20 20 20 w[i-1], 0);.
16ab6 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
16ab7 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b balance_cleanup;
16ab8 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20 . apNew[i]
16ab9 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 6e 4e = pNew;. nN
16aba 65 77 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ew++;. }. }.
16abb 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20 6f . /* Free any o
16abc 6c 64 20 70 61 67 65 73 20 74 68 61 74 20 77 65 ld pages that we
16abd 72 65 20 6e 6f 74 20 72 65 75 73 65 64 20 61 73 re not reused as
16abe 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2f new pages.. */
16abf 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4f 6c 64 . while( i<nOld
16ac0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 72 65 ){. rc = fre
16ac1 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b ePage(apOld[i]);
16ac2 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
16ac3 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
16ac4 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 up;. releaseP
16ac5 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 age(apOld[i]);.
16ac6 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b apOld[i] = 0;
16ac7 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 . i++;. }..
16ac8 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 74 68 65 /*. ** Put the
16ac9 20 6e 65 77 20 70 61 67 65 73 20 69 6e 20 61 63 new pages in ac
16aca 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 20 20 cending order.
16acb 54 68 69 73 20 68 65 6c 70 73 20 74 6f 0a 20 20 This helps to.
16acc 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 65 73 20 ** keep entries
16acd 69 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 in the disk file
16ace 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 74 68 61 in order so tha
16acf 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a 20 6f 66 t a scan. ** of
16ad0 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 the table is a
16ad1 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72 6f linear scan thro
16ad2 75 67 68 20 74 68 65 20 66 69 6c 65 2e 20 20 54 ugh the file. T
16ad3 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 75 72 6e hat. ** in turn
16ad4 20 68 65 6c 70 73 20 74 68 65 20 6f 70 65 72 61 helps the opera
16ad5 74 69 6e 67 20 73 79 73 74 65 6d 20 74 6f 20 64 ting system to d
16ad6 65 6c 69 76 65 72 20 70 61 67 65 73 0a 20 20 2a eliver pages. *
16ad7 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 20 * from the disk
16ad8 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e 0a 20 20 more rapidly..
16ad9 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 6e 5e 32 **. ** An O(n^2
16ada 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 ) insertion sort
16adb 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 75 73 algorithm is us
16adc 65 64 2c 20 62 75 74 20 73 69 6e 63 65 0a 20 20 ed, but since.
16add 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 20 6d 6f ** n is never mo
16ade 72 65 20 74 68 61 6e 20 4e 42 20 28 61 20 73 6d re than NB (a sm
16adf 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 2c 20 74 all constant), t
16ae0 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 2a 2a 20 hat should. **
16ae1 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d not be a problem
16ae2 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e .. **. ** When
16ae3 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 6f 6e 65 NB==3, this one
16ae4 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d 61 optimization ma
16ae5 6b 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 kes the database
16ae6 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 35 25 20 . ** about 25%
16ae7 66 61 73 74 65 72 20 66 6f 72 20 6c 61 72 67 65 faster for large
16ae8 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 6e 64 20 insertions and
16ae9 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a deletions.. */.
16aea 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 2d 31 for(i=0; i<k-1
16aeb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 ; i++){. int
16aec 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77 5b 69 minV = pgnoNew[i
16aed 5d 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 49 20 ];. int minI
16aee 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69 = i;. for(j=i
16aef 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 +1; j<k; j++){.
16af0 20 20 20 20 20 69 66 28 20 70 67 6e 6f 4e 65 77 if( pgnoNew
16af1 5b 6a 5d 3c 28 75 6e 73 69 67 6e 65 64 29 6d 69 [j]<(unsigned)mi
16af2 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 69 nV ){. mi
16af3 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 nI = j;.
16af4 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77 5b 6a minV = pgnoNew[j
16af5 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d ];. }. }
16af6 0a 20 20 20 20 69 66 28 20 6d 69 6e 49 3e 69 20 . if( minI>i
16af7 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 3b 0a ){. int t;.
16af8 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
16af9 54 3b 0a 20 20 20 20 20 20 74 20 3d 20 70 67 6e T;. t = pgn
16afa 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 70 oNew[i];. p
16afb 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 T = apNew[i];.
16afc 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69 5d 20 3d pgnoNew[i] =
16afd 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49 5d 3b 0a pgnoNew[minI];.
16afe 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d apNew[i] =
16aff 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20 20 apNew[minI];.
16b00 20 20 20 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49 pgnoNew[minI
16b01 5d 20 3d 20 74 3b 0a 20 20 20 20 20 20 61 70 4e ] = t;. apN
16b02 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20 ew[minI] = pT;.
16b03 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45 }. }. TRACE
16b04 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f 6c 64 3a (("BALANCE: old:
16b05 20 25 64 20 25 64 20 25 64 20 20 6e 65 77 3a 20 %d %d %d new:
16b06 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 %d(%d) %d(%d) %d
16b07 28 25 64 29 20 25 64 28 25 64 29 20 25 64 28 25 (%d) %d(%d) %d(%
16b08 64 29 5c 6e 22 2c 0a 20 20 20 20 70 67 6e 6f 4f d)\n",. pgnoO
16b09 6c 64 5b 30 5d 2c 20 0a 20 20 20 20 6e 4f 6c 64 ld[0], . nOld
16b0a 3e 3d 32 20 3f 20 70 67 6e 6f 4f 6c 64 5b 31 5d >=2 ? pgnoOld[1]
16b0b 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64 3e 3d : 0,. nOld>=
16b0c 33 20 3f 20 70 67 6e 6f 4f 6c 64 5b 32 5d 20 3a 3 ? pgnoOld[2] :
16b0d 20 30 2c 0a 20 20 20 20 70 67 6e 6f 4e 65 77 5b 0,. pgnoNew[
16b0e 30 5d 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 0], szNew[0],.
16b0f 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 70 67 6e 6f nNew>=2 ? pgno
16b10 4e 65 77 5b 31 5d 20 3a 20 30 2c 20 6e 4e 65 77 New[1] : 0, nNew
16b11 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20 3a >=2 ? szNew[1] :
16b12 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33 20 0,. nNew>=3
16b13 3f 20 70 67 6e 6f 4e 65 77 5b 32 5d 20 3a 20 30 ? pgnoNew[2] : 0
16b14 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 7a 4e 65 , nNew>=3 ? szNe
16b15 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e w[2] : 0,. nN
16b16 65 77 3e 3d 34 20 3f 20 70 67 6e 6f 4e 65 77 5b ew>=4 ? pgnoNew[
16b17 33 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 34 20 3] : 0, nNew>=4
16b18 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 30 2c 0a ? szNew[3] : 0,.
16b19 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 20 70 67 nNew>=5 ? pg
16b1a 6e 6f 4e 65 77 5b 34 5d 20 3a 20 30 2c 20 6e 4e noNew[4] : 0, nN
16b1b 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d ew>=5 ? szNew[4]
16b1c 20 3a 20 30 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20 : 0));.. /*.
16b1d 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72 69 ** Evenly distri
16b1e 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69 6e bute the data in
16b1f 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73 apCell[] across
16b20 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e 0a the new pages..
16b21 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 69 ** Insert divi
16b22 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70 der cells into p
16b23 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73 73 Parent as necess
16b24 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20 ary.. */. j =
16b25 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 0;. for(i=0; i<
16b26 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nNew; i++){.
16b27 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65 20 /* Assemble the
16b28 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 new sibling page
16b29 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 . */. MemPage
16b2a 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 *pNew = apNew[i
16b2b 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a ];. assert( j
16b2c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 <nMaxCells );.
16b2d 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e assert( pNew->
16b2e 70 67 6e 6f 3d 3d 70 67 6e 6f 4e 65 77 5b 69 5d pgno==pgnoNew[i]
16b2f 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 );. zeroPage
16b30 28 70 4e 65 77 2c 20 70 61 67 65 46 6c 61 67 73 (pNew, pageFlags
16b31 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 );. assembleP
16b32 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 4e 65 77 age(pNew, cntNew
16b33 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c 5b 6a [i]-j, &apCell[j
16b34 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a ], &szCell[j]);.
16b35 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 assert( pNew
16b36 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28 6e 4e ->nCell>0 || (nN
16b37 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e 65 77 5b ew==1 && cntNew[
16b38 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20 20 61 73 0]==0) );. as
16b39 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 4f 76 65 sert( pNew->nOve
16b3a 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 20 20 20 rflow==0 );..
16b3b 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 /* If this is a
16b3c 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 n auto-vacuum da
16b3d 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 20 74 tabase, update t
16b3e 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 he pointer map e
16b3f 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 74 68 ntries. ** th
16b40 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 at point to the
16b41 73 69 62 6c 69 6e 67 73 20 74 68 61 74 20 77 65 siblings that we
16b42 72 65 20 72 65 61 72 72 61 6e 67 65 64 2e 20 54 re rearranged. T
16b43 68 65 73 65 20 63 61 6e 20 62 65 3a 20 6c 65 66 hese can be: lef
16b44 74 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 72 65 t. ** childre
16b45 6e 20 6f 66 20 63 65 6c 6c 73 2c 20 74 68 65 20 n of cells, the
16b46 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 74 right-child of t
16b47 68 65 20 70 61 67 65 2c 20 6f 72 20 6f 76 65 72 he page, or over
16b48 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 20 20 2a flow pages. *
16b49 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 * pointed to by
16b4a 63 65 6c 6c 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 cells.. */.
16b4b 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
16b4c 55 4d 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 UM ){. for(
16b4d 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77 5b 69 5d k=j; k<cntNew[i]
16b4e 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; k++){.
16b4f 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61 78 43 65 assert( k<nMaxCe
16b50 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20 69 lls );. i
16b51 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d 30 78 46 f( aFrom[k]==0xF
16b52 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61 46 72 6f F || apCopy[aFro
16b53 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d 70 4e 65 m[k]]->pgno!=pNe
16b54 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 w->pgno ){.
16b55 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 rc = ptrmap
16b56 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c 20 6b 2d PutOvfl(pNew, k-
16b57 6a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 j);. if
16b58 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
16b59 26 26 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f && leafCorrectio
16b5a 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 n==0 ){.
16b5b 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 rc = ptrmapP
16b5c 75 74 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 ut(pBt, get4byte
16b5d 28 61 70 43 65 6c 6c 5b 6b 5d 29 2c 20 50 54 52 (apCell[k]), PTR
16b5e 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 2d MAP_BTREE, pNew-
16b5f 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 >pgno);.
16b60 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 }. if
16b61 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
16b62 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 ){. g
16b63 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 oto balance_clea
16b64 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d nup;. }
16b65 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
16b66 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6a 20 }. }.. j
16b67 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 20 = cntNew[i];..
16b68 20 20 2f 2a 20 49 66 20 74 68 65 20 73 69 62 6c /* If the sibl
16b69 69 6e 67 20 70 61 67 65 20 61 73 73 65 6d 62 6c ing page assembl
16b6a 65 64 20 61 62 6f 76 65 20 77 61 73 20 6e 6f 74 ed above was not
16b6b 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 the right-most
16b6c 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a 20 sibling,. **
16b6d 69 6e 73 65 72 74 20 61 20 64 69 76 69 64 65 72 insert a divider
16b6e 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 70 cell into the p
16b6f 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 arent page..
16b70 2a 2f 0a 20 20 20 20 69 66 28 20 69 3c 6e 4e 65 */. if( i<nNe
16b71 77 2d 31 20 26 26 20 6a 3c 6e 43 65 6c 6c 20 29 w-1 && j<nCell )
16b72 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c {. u8 *pCel
16b73 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 54 65 l;. u8 *pTe
16b74 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a mp;. int sz
16b75 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ;.. assert(
16b76 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a j<nMaxCells );.
16b77 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 61 70 pCell = ap
16b78 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 73 Cell[j];. s
16b79 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d 20 2b 20 z = szCell[j] +
16b7a 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a leafCorrection;.
16b7b 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 26 61 pTemp = &a
16b7c 53 70 61 63 65 32 5b 69 53 70 61 63 65 32 5d 3b Space2[iSpace2];
16b7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 . if( !pNew
16b7e 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
16b7f 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 2d 3e memcpy(&pNew->
16b80 61 44 61 74 61 5b 38 5d 2c 20 70 43 65 6c 6c 2c aData[8], pCell,
16b81 20 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 4);. if(
16b82 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 0a 20 ISAUTOVACUUM .
16b83 20 20 20 20 20 20 20 20 26 26 20 28 61 46 72 6f && (aFro
16b84 6d 5b 6a 5d 3d 3d 30 78 46 46 20 7c 7c 20 61 70 m[j]==0xFF || ap
16b85 43 6f 70 79 5b 61 46 72 6f 6d 5b 6a 5d 5d 2d 3e Copy[aFrom[j]]->
16b86 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 6e 6f pgno!=pNew->pgno
16b87 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 ). ){.
16b88 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d rc = ptrm
16b89 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 62 apPut(pBt, get4b
16b8a 79 74 65 28 70 43 65 6c 6c 29 2c 20 50 54 52 4d yte(pCell), PTRM
16b8b 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 2d 3e AP_BTREE, pNew->
16b8c 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 pgno);.
16b8d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16b8e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
16b8f 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 goto balance_c
16b90 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 leanup;.
16b91 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
16b92 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 }else if( le
16b93 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 afData ){.
16b94 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 65 65 /* If the tree
16b95 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 61 20 is a leaf-data
16b96 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 73 69 tree, and the si
16b97 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 76 65 blings are leave
16b98 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 s, . ** t
16b99 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 hen there is no
16b9a 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 20 divider cell in
16b9b 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61 apCell[]. Instea
16b9c 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 20 0a d, the divider .
16b9d 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20 ** cell
16b9e 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 consists of the
16b9f 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20 integer key for
16ba0 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 the right-most c
16ba1 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 20 20 ell of .
16ba2 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d 70 ** the sibling-p
16ba3 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 age assembled ab
16ba4 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20 ove only..
16ba5 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 65 6c */. Cel
16ba6 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 lInfo info;.
16ba7 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20 j--;.
16ba8 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
16ba9 73 65 43 65 6c 6c 50 74 72 28 70 4e 65 77 2c 20 seCellPtr(pNew,
16baa 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f apCell[j], &info
16bab 29 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c );. pCell
16bac 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 = pTemp;.
16bad 20 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50 61 fillInCell(pPa
16bae 72 65 6e 74 2c 20 70 43 65 6c 6c 2c 20 30 2c 20 rent, pCell, 0,
16baf 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30 2c info.nKey, 0, 0,
16bb0 20 30 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20 0, &sz);.
16bb1 20 20 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 pTemp = 0;.
16bb2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
16bb3 20 20 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 pCell -= 4;.
16bb4 20 20 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 /* Obscure
16bb5 20 63 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 case for non-le
16bb6 61 66 2d 64 61 74 61 20 74 72 65 65 73 3a 20 49 af-data trees: I
16bb7 66 20 74 68 65 20 63 65 6c 6c 20 61 74 20 70 43 f the cell at pC
16bb8 65 6c 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20 ell was.
16bb9 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74 ** previously st
16bba 6f 72 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e ored on a leaf n
16bbb 6f 64 65 2c 20 61 6e 64 20 69 74 73 20 72 65 70 ode, and its rep
16bbc 6f 72 74 65 64 20 73 69 7a 65 20 77 61 73 20 34 orted size was 4
16bbd 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 . ** byte
16bbe 73 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61 s, then it may a
16bbf 63 74 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c ctually be small
16bc0 65 72 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20 er than this .
16bc1 20 20 20 20 20 20 2a 2a 20 28 73 65 65 20 73 71 ** (see sq
16bc2 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 lite3BtreeParseC
16bc3 65 6c 6c 50 74 72 28 29 2c 20 34 20 62 79 74 65 ellPtr(), 4 byte
16bc4 73 20 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d s is the minimum
16bc5 20 73 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 20 size of.
16bc6 20 2a 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42 ** any cell). B
16bc7 75 74 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 ut it is importa
16bc8 6e 74 20 74 6f 20 70 61 73 73 20 74 68 65 20 63 nt to pass the c
16bc9 6f 72 72 65 63 74 20 73 69 7a 65 20 74 6f 20 0a orrect size to .
16bca 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 65 72 ** inser
16bcb 74 43 65 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61 tCell(), so repa
16bcc 72 73 65 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77 rse the cell now
16bcd 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 .. **.
16bce 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 ** Note tha
16bcf 74 20 74 68 69 73 20 63 61 6e 20 6e 65 76 65 72 t this can never
16bd0 20 68 61 70 70 65 6e 20 69 6e 20 61 6e 20 53 51 happen in an SQ
16bd1 4c 69 74 65 20 64 61 74 61 20 66 69 6c 65 2c 20 Lite data file,
16bd2 61 73 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a as all. *
16bd3 2a 20 63 65 6c 6c 73 20 61 72 65 20 61 74 20 6c * cells are at l
16bd4 65 61 73 74 20 34 20 62 79 74 65 73 2e 20 49 74 east 4 bytes. It
16bd5 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 6e only happens in
16bd6 20 62 2d 74 72 65 65 73 20 75 73 65 64 0a 20 20 b-trees used.
16bd7 20 20 20 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c ** to eval
16bd8 75 61 74 65 20 22 49 4e 20 28 53 45 4c 45 43 54 uate "IN (SELECT
16bd9 20 2e 2e 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c ...)" and simil
16bda 61 72 20 63 6c 61 75 73 65 73 2e 0a 20 20 20 20 ar clauses..
16bdb 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 */. i
16bdc 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 f( szCell[j]==4
16bdd 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 ){. ass
16bde 65 72 74 28 6c 65 61 66 43 6f 72 72 65 63 74 69 ert(leafCorrecti
16bdf 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20 20 20 20 20 on==4);.
16be0 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 sz = cellSizeP
16be1 74 72 28 70 50 61 72 65 6e 74 2c 20 70 43 65 6c tr(pParent, pCel
16be2 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 l);. }.
16be3 20 20 20 20 7d 0a 20 20 20 20 20 20 69 53 70 61 }. iSpa
16be4 63 65 32 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 ce2 += sz;.
16be5 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 assert( sz<=pBt
16be6 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a ->pageSize/4 );.
16be7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 53 assert( iS
16be8 70 61 63 65 32 3c 3d 70 42 74 2d 3e 70 61 67 65 pace2<=pBt->page
16be9 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 72 63 Size );. rc
16bea 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 = insertCell(pP
16beb 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 arent, nxDiv, pC
16bec 65 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 ell, sz, pTemp,
16bed 34 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 4);. if( rc
16bee 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
16bef 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
16bf0 75 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 up;. assert
16bf1 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
16bf2 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e writeable(pParen
16bf3 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 t->pDbPage) );.
16bf4 20 20 20 20 20 70 75 74 34 62 79 74 65 28 66 69 put4byte(fi
16bf5 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 ndOverflowCell(p
16bf6 50 61 72 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70 Parent,nxDiv), p
16bf7 4e 65 77 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 New->pgno);..
16bf8 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 /* If this is
16bf9 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 an auto-vacuum
16bfa 64 61 74 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f database, and no
16bfb 74 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72 t a leaf-data tr
16bfc 65 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 ee,. ** the
16bfd 6e 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 n update the poi
16bfe 6e 74 65 72 20 6d 61 70 20 77 69 74 68 20 61 6e nter map with an
16bff 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f entry for the o
16c00 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 verflow page.
16c01 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 ** that the c
16c02 65 6c 6c 20 6a 75 73 74 20 69 6e 73 65 72 74 65 ell just inserte
16c03 64 20 70 6f 69 6e 74 73 20 74 6f 20 28 69 66 20 d points to (if
16c04 61 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 any).. */.
16c05 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 if( ISAUTOV
16c06 41 43 55 55 4d 20 26 26 20 21 6c 65 61 66 44 61 ACUUM && !leafDa
16c07 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 ta ){. rc
16c08 20 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c = ptrmapPutOvfl
16c09 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 (pParent, nxDiv)
16c0a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
16c0b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
16c0c 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 goto ba
16c0d 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
16c0e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
16c0f 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 . j++;.
16c10 20 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d nxDiv++;. }
16c11 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 .. /* Set the
16c12 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 pointer-map ent
16c13 72 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73 ry for the new s
16c14 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a ibling page. */.
16c15 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 if( ISAUTOVA
16c16 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 CUUM ){. rc
16c17 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 = ptrmapPut(pBt
16c18 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 , pNew->pgno, PT
16c19 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 RMAP_BTREE, pPar
16c1a 65 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 ent->pgno);.
16c1b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16c1c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
16c1d 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 oto balance_clea
16c1e 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nup;. }.
16c1f 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
16c20 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 j==nCell );. a
16c21 73 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b ssert( nOld>0 );
16c22 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e . assert( nNew>
16c23 30 20 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65 0 );. if( (page
16c24 46 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46 Flags & PTF_LEAF
16c25 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a )==0 ){. u8 *
16c26 7a 43 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 zChild = &apCopy
16c27 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b [nOld-1]->aData[
16c28 38 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 8];. memcpy(&
16c29 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 apNew[nNew-1]->a
16c2a 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c Data[8], zChild,
16c2b 20 34 29 3b 0a 20 20 20 20 69 66 28 20 49 53 41 4);. if( ISA
16c2c 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 UTOVACUUM ){.
16c2d 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 rc = ptrmapPu
16c2e 74 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 t(pBt, get4byte(
16c2f 7a 43 68 69 6c 64 29 2c 20 50 54 52 4d 41 50 5f zChild), PTRMAP_
16c30 42 54 52 45 45 2c 20 61 70 4e 65 77 5b 6e 4e 65 BTREE, apNew[nNe
16c31 77 2d 31 5d 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 w-1]->pgno);.
16c32 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
16c33 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
16c34 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
16c35 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 anup;. }.
16c36 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 }. }. assert
16c37 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
16c38 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e writeable(pParen
16c39 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 t->pDbPage) );.
16c3a 20 69 66 28 20 6e 78 44 69 76 3d 3d 70 50 61 72 if( nxDiv==pPar
16c3b 65 6e 74 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72 65 ent->nCell+pPare
16c3c 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b nt->nOverflow ){
16c3d 0a 20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f . /* Right-mo
16c3e 73 74 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 st sibling is th
16c3f 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 e right-most chi
16c40 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 20 2a 2f ld of pParent */
16c41 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 . put4byte(&p
16c42 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 Parent->aData[pP
16c43 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 arent->hdrOffset
16c44 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 +8], pgnoNew[nNe
16c45 77 2d 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a w-1]);. }else{.
16c46 20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 /* Right-mos
16c47 74 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 t sibling is the
16c48 20 6c 65 66 74 20 63 68 69 6c 64 20 6f 66 20 74 left child of t
16c49 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 he first entry i
16c4a 6e 20 70 50 61 72 65 6e 74 0a 20 20 20 20 2a 2a n pParent. **
16c4b 20 70 61 73 74 20 74 68 65 20 72 69 67 68 74 2d past the right-
16c4c 6d 6f 73 74 20 64 69 76 69 64 65 72 20 65 6e 74 most divider ent
16c4d 72 79 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 79 ry */. put4by
16c4e 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 te(findOverflowC
16c4f 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 ell(pParent, nxD
16c50 69 76 29 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 iv), pgnoNew[nNe
16c51 77 2d 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a w-1]);. }.. /*
16c52 0a 20 20 2a 2a 20 42 61 6c 61 6e 63 65 20 74 68 . ** Balance th
16c53 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 20 e parent page.
16c54 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 75 Note that the cu
16c55 72 72 65 6e 74 20 70 61 67 65 20 28 70 50 61 67 rrent page (pPag
16c56 65 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 68 61 e) might. ** ha
16c57 76 65 20 62 65 65 6e 20 61 64 64 65 64 20 74 6f ve been added to
16c58 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 73 6f the freelist so
16c59 20 69 74 20 6d 69 67 68 74 20 6e 6f 20 6c 6f 6e it might no lon
16c5a 67 65 72 20 62 65 20 69 6e 69 74 69 61 6c 69 7a ger be initializ
16c5b 65 64 2e 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 ed.. ** But the
16c5c 20 70 61 72 65 6e 74 20 70 61 67 65 20 77 69 6c parent page wil
16c5d 6c 20 61 6c 77 61 79 73 20 62 65 20 69 6e 69 74 l always be init
16c5e 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 ialized.. */.
16c5f 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d assert( pParent-
16c60 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 73 71 6c >isInit );. sql
16c61 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28 ite3ScratchFree(
16c62 61 70 43 65 6c 6c 29 3b 0a 20 20 61 70 43 65 6c apCell);. apCel
16c63 6c 20 3d 20 30 3b 0a 20 20 54 52 41 43 45 28 28 l = 0;. TRACE((
16c64 22 42 41 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 "BALANCE: finish
16c65 65 64 20 77 69 74 68 20 25 64 3a 20 6f 6c 64 3d ed with %d: old=
16c66 25 64 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d %d new=%d cells=
16c67 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 %d\n",.
16c68 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 6e 4f pPage->pgno, nO
16c69 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 ld, nNew, nCell)
16c6a 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 );. pPage->nOve
16c6b 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 72 65 6c rflow = 0;. rel
16c6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
16c6d 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d . pCur->iPage--
16c6e 3b 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 ;. rc = balance
16c6f 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 0a 20 20 (pCur, 0);. .
16c70 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 /*. ** Cleanup
16c71 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 before returning
16c72 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 .. */.balance_c
16c73 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 leanup:. sqlite
16c74 33 50 61 67 65 46 72 65 65 28 61 53 70 61 63 65 3PageFree(aSpace
16c75 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 63 72 2);. sqlite3Scr
16c76 61 74 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29 atchFree(apCell)
16c77 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e ;. for(i=0; i<n
16c78 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 Old; i++){. r
16c79 65 6c 65 61 73 65 50 61 67 65 28 61 70 4f 6c 64 eleasePage(apOld
16c7a 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 [i]);. }. for(
16c7b 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b i=0; i<nNew; i++
16c7c 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 ){. releasePa
16c7d 67 65 28 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 ge(apNew[i]);.
16c7e 7d 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 }. pCur->apPage
16c7f 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
16c80 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 Overflow = 0;..
16c81 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
16c82 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
16c83 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 e is called for
16c84 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 the root page of
16c85 20 61 20 62 74 72 65 65 20 77 68 65 6e 20 74 68 a btree when th
16c86 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 63 e root.** page c
16c87 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 ontains no cells
16c88 2e 20 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70 . This is an op
16c89 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 6d 61 6b portunity to mak
16c8a 65 20 74 68 65 20 74 72 65 65 0a 2a 2a 20 73 68 e the tree.** sh
16c8b 61 6c 6c 6f 77 65 72 20 62 79 20 6f 6e 65 20 6c allower by one l
16c8c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 evel..*/.static
16c8d 69 6e 74 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c int balance_shal
16c8e 6c 6f 77 65 72 28 42 74 43 75 72 73 6f 72 20 2a lower(BtCursor *
16c8f 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 pCur){. MemPage
16c90 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 *pPage;
16c91 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 /* Root pa
16c92 67 65 20 6f 66 20 42 2d 54 72 65 65 20 2a 2f 0a ge of B-Tree */.
16c93 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c MemPage *pChil
16c94 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f d; /
16c95 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 * The only child
16c96 20 70 61 67 65 20 6f 66 20 70 50 61 67 65 20 2a page of pPage *
16c97 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 /. Pgno pgnoChi
16c98 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ld;
16c99 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
16c9a 66 6f 72 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 for pChild */.
16c9b 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
16c9c 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 OK; /*
16c9d 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d Return code from
16c9e 20 73 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a subprocedures *
16c9f 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
16ca0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
16ca1 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 /* The main
16ca2 42 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 BTree structure
16ca3 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 65 6c 6c 50 */. int mxCellP
16ca4 65 72 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 erPage;
16ca5 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d /* Maximum num
16ca6 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 70 65 72 ber of cells per
16ca7 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a page */. u8 **
16ca8 61 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 apCell;
16ca9 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 /* All c
16caa 65 6c 6c 73 20 66 72 6f 6d 20 70 61 67 65 73 20 ells from pages
16cab 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a being balanced *
16cac 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b /. u16 *szCell;
16cad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16cae 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f /* Local size o
16caf 66 20 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a f all cells */..
16cb0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16cb1 69 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 70 50 iPage==0 );. pP
16cb2 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
16cb3 67 65 5b 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 ge[0];.. assert
16cb4 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d ( pPage->nCell==
16cb5 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
16cb6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
16cb7 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
16cb8 74 65 78 29 20 29 3b 0a 20 20 70 42 74 20 3d 20 tex) );. pBt =
16cb9 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 6d 78 pPage->pBt;. mx
16cba 43 65 6c 6c 50 65 72 50 61 67 65 20 3d 20 4d 58 CellPerPage = MX
16cbb 5f 43 45 4c 4c 28 70 42 74 29 3b 0a 20 20 61 70 _CELL(pBt);. ap
16cbc 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 Cell = sqlite3Ma
16cbd 6c 6c 6f 63 28 20 6d 78 43 65 6c 6c 50 65 72 50 lloc( mxCellPerP
16cbe 61 67 65 2a 28 73 69 7a 65 6f 66 28 75 38 2a 29 age*(sizeof(u8*)
16cbf 2b 73 69 7a 65 6f 66 28 75 31 36 29 29 20 29 3b +sizeof(u16)) );
16cc0 0a 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 . if( apCell==0
16cc1 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
16cc2 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c _NOMEM;. szCell
16cc3 20 3d 20 28 75 31 36 2a 29 26 61 70 43 65 6c 6c = (u16*)&apCell
16cc4 5b 6d 78 43 65 6c 6c 50 65 72 50 61 67 65 5d 3b [mxCellPerPage];
16cc5 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 . if( pPage->le
16cc6 61 66 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 af ){. /* The
16cc7 20 74 61 62 6c 65 20 69 73 20 63 6f 6d 70 6c 65 table is comple
16cc8 74 65 6c 79 20 65 6d 70 74 79 20 2a 2f 0a 20 20 tely empty */.
16cc9 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 TRACE(("BALANC
16cca 45 3a 20 65 6d 70 74 79 20 74 61 62 6c 65 20 25 E: empty table %
16ccb 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e d\n", pPage->pgn
16ccc 6f 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 o));. }else{.
16ccd 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 /* The root pa
16cce 67 65 20 69 73 20 65 6d 70 74 79 20 62 75 74 20 ge is empty but
16ccf 68 61 73 20 6f 6e 65 20 63 68 69 6c 64 2e 20 20 has one child.
16cd0 54 72 61 6e 73 66 65 72 20 74 68 65 0a 20 20 20 Transfer the.
16cd1 20 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ** information
16cd2 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 63 68 from that one ch
16cd3 69 6c 64 20 69 6e 74 6f 20 74 68 65 20 72 6f 6f ild into the roo
16cd4 74 20 70 61 67 65 20 69 66 20 69 74 20 0a 20 20 t page if it .
16cd5 20 20 2a 2a 20 77 69 6c 6c 20 66 69 74 2e 20 20 ** will fit.
16cd6 54 68 69 73 20 72 65 64 75 63 65 73 20 74 68 65 This reduces the
16cd7 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 depth of the tr
16cd8 65 65 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a ee by one.. *
16cd9 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 *. ** If the
16cda 72 6f 6f 74 20 70 61 67 65 20 69 73 20 70 61 67 root page is pag
16cdb 65 20 31 2c 20 69 74 20 68 61 73 20 6c 65 73 73 e 1, it has less
16cdc 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 space available
16cdd 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 69 74 73 than. ** its
16cde 20 63 68 69 6c 64 20 28 64 75 65 20 74 6f 20 74 child (due to t
16cdf 68 65 20 31 30 30 20 62 79 74 65 20 68 65 61 64 he 100 byte head
16ce0 65 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 er that occurs a
16ce1 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a t the beginning.
16ce2 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 61 ** of the da
16ce3 74 61 62 61 73 65 20 66 6c 65 29 2c 20 73 6f 20 tabase fle), so
16ce4 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 it might not be
16ce5 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c able to hold all
16ce6 20 6f 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 of the . **
16ce7 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 75 72 72 information curr
16ce8 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 ently contained
16ce9 69 6e 20 74 68 65 20 63 68 69 6c 64 2e 20 20 49 in the child. I
16cea 66 20 74 68 69 73 20 69 73 20 74 68 65 20 0a 20 f this is the .
16ceb 20 20 20 2a 2a 20 63 61 73 65 2c 20 74 68 65 6e ** case, then
16cec 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 65 20 74 do not do the t
16ced 72 61 6e 73 66 65 72 2e 20 20 4c 65 61 76 65 20 ransfer. Leave
16cee 70 61 67 65 20 31 20 65 6d 70 74 79 20 65 78 63 page 1 empty exc
16cef 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74 ept. ** for t
16cf0 68 65 20 72 69 67 68 74 2d 70 6f 69 6e 74 65 72 he right-pointer
16cf1 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20 70 61 to the child pa
16cf2 67 65 2e 20 20 54 68 65 20 63 68 69 6c 64 20 70 ge. The child p
16cf3 61 67 65 20 62 65 63 6f 6d 65 73 0a 20 20 20 20 age becomes.
16cf4 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 72 ** the virtual r
16cf5 6f 6f 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e oot of the tree.
16cf6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 56 56 41 5f . */. VVA_
16cf7 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 ONLY( pCur->page
16cf8 73 53 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b sShuffled = 1 );
16cf9 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 20 3d . pgnoChild =
16cfa 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
16cfb 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
16cfc 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 drOffset+8]);.
16cfd 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68 assert( pgnoCh
16cfe 69 6c 64 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 ild>0 );. ass
16cff 65 72 74 28 20 70 67 6e 6f 43 68 69 6c 64 3c 3d ert( pgnoChild<=
16d00 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
16d01 50 61 67 65 2d 3e 70 42 74 29 20 29 3b 0a 20 20 Page->pBt) );.
16d02 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
16d03 72 65 65 47 65 74 50 61 67 65 28 70 50 61 67 65 reeGetPage(pPage
16d04 2d 3e 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 ->pBt, pgnoChild
16d05 2c 20 26 70 43 68 69 6c 64 2c 20 30 29 3b 0a 20 , &pChild, 0);.
16d06 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
16d07 20 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c end_shallow_bal
16d08 61 6e 63 65 3b 0a 20 20 20 20 69 66 28 20 70 50 ance;. if( pP
16d09 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a age->pgno==1 ){.
16d0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
16d0b 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 e3BtreeInitPage(
16d0c 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 69 pChild);. i
16d0d 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 f( rc ) goto end
16d0e 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 _shallow_balance
16d0f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
16d10 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f pChild->nOverflo
16d11 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 w==0 );. if
16d12 28 20 70 43 68 69 6c 64 2d 3e 6e 46 72 65 65 3e ( pChild->nFree>
16d13 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 =100 ){.
16d14 2f 2a 20 54 68 65 20 63 68 69 6c 64 20 69 6e 66 /* The child inf
16d15 6f 72 6d 61 74 69 6f 6e 20 77 69 6c 6c 20 66 69 ormation will fi
16d16 74 20 6f 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 t on the root pa
16d17 67 65 2c 20 73 6f 20 64 6f 20 74 68 65 0a 20 20 ge, so do the.
16d18 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 2a 2f ** copy */
16d19 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a . int i;.
16d1a 20 20 20 20 20 20 20 20 7a 65 72 6f 50 61 67 65 zeroPage
16d1b 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e (pPage, pChild->
16d1c 61 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 aData[0]);.
16d1d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 for(i=0; i<pC
16d1e 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b hild->nCell; i++
16d1f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 43 ){. apC
16d20 65 6c 6c 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c ell[i] = findCel
16d21 6c 28 70 43 68 69 6c 64 2c 69 29 3b 0a 20 20 20 l(pChild,i);.
16d22 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 69 5d szCell[i]
16d23 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 = cellSizePtr(p
16d24 43 68 69 6c 64 2c 20 61 70 43 65 6c 6c 5b 69 5d Child, apCell[i]
16d25 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
16d26 20 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67 assemblePag
16d27 65 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d e(pPage, pChild-
16d28 3e 6e 43 65 6c 6c 2c 20 61 70 43 65 6c 6c 2c 20 >nCell, apCell,
16d29 73 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 szCell);.
16d2a 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 69 67 /* Copy the rig
16d2b 68 74 2d 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 ht-pointer of th
16d2c 65 20 63 68 69 6c 64 20 74 6f 20 74 68 65 20 70 e child to the p
16d2d 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 arent. */.
16d2e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16d2f 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
16d30 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
16d31 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 ) );. put
16d32 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
16d33 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 ata[pPage->hdrOf
16d34 66 73 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20 fset+8], .
16d35 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26 get4byte(&
16d36 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43 pChild->aData[pC
16d37 68 69 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b hild->hdrOffset+
16d38 38 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 8]));. rc
16d39 20 3d 20 66 72 65 65 50 61 67 65 28 70 43 68 69 = freePage(pChi
16d3a 6c 64 29 3b 0a 20 20 20 20 20 20 20 20 54 52 41 ld);. TRA
16d3b 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 CE(("BALANCE: ch
16d3c 69 6c 64 20 25 64 20 74 72 61 6e 73 66 65 72 20 ild %d transfer
16d3d 74 6f 20 70 61 67 65 20 31 5c 6e 22 2c 20 70 43 to page 1\n", pC
16d3e 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 hild->pgno));.
16d3f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
16d40 20 20 20 2f 2a 20 54 68 65 20 63 68 69 6c 64 20 /* The child
16d41 68 61 73 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 has more informa
16d42 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 66 tion that will f
16d43 69 74 20 6f 6e 20 74 68 65 20 72 6f 6f 74 2e 0a it on the root..
16d44 20 20 20 20 20 20 20 20 2a 2a 20 54 68 65 20 74 ** The t
16d45 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 62 ree is already b
16d46 61 6c 61 6e 63 65 64 2e 20 20 44 6f 20 6e 6f 74 alanced. Do not
16d47 68 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 20 hing. */.
16d48 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 TRACE(("BALANCE
16d49 3a 20 63 68 69 6c 64 20 25 64 20 77 69 6c 6c 20 : child %d will
16d4a 6e 6f 74 20 66 69 74 20 6f 6e 20 70 61 67 65 20 not fit on page
16d4b 31 5c 6e 22 2c 20 70 43 68 69 6c 64 2d 3e 70 67 1\n", pChild->pg
16d4c 6e 6f 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 no));. }.
16d4d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d }else{. m
16d4e 65 6d 63 70 79 28 70 50 61 67 65 2d 3e 61 44 61 emcpy(pPage->aDa
16d4f 74 61 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 ta, pChild->aDat
16d50 61 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 a, pPage->pBt->u
16d51 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 20 20 sableSize);.
16d52 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 pPage->isInit
16d53 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
16d54 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 sqlite3BtreeInit
16d55 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
16d56 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
16d57 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 QLITE_OK );.
16d58 20 20 66 72 65 65 50 61 67 65 28 70 43 68 69 6c freePage(pChil
16d59 64 29 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 d);. TRACE(
16d5a 28 22 42 41 4c 41 4e 43 45 3a 20 74 72 61 6e 73 ("BALANCE: trans
16d5b 66 65 72 20 63 68 69 6c 64 20 25 64 20 69 6e 74 fer child %d int
16d5c 6f 20 72 6f 6f 74 20 25 64 5c 6e 22 2c 0a 20 20 o root %d\n",.
16d5d 20 20 20 20 20 20 20 20 20 20 20 20 70 43 68 69 pChi
16d5e 6c 64 2d 3e 70 67 6e 6f 2c 20 70 50 61 67 65 2d ld->pgno, pPage-
16d5f 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 >pgno));. }.
16d60 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
16d61 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 ->nOverflow==0 )
16d62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
16d63 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
16d64 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 . if( ISAUTOV
16d65 41 43 55 55 4d 20 26 26 20 72 63 3d 3d 53 51 4c ACUUM && rc==SQL
16d66 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
16d67 72 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 rc = setChildPtr
16d68 6d 61 70 73 28 70 50 61 67 65 29 3b 0a 20 20 20 maps(pPage);.
16d69 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 }.#endif. re
16d6a 6c 65 61 73 65 50 61 67 65 28 70 43 68 69 6c 64 leasePage(pChild
16d6b 29 3b 0a 20 20 7d 0a 65 6e 64 5f 73 68 61 6c 6c );. }.end_shall
16d6c 6f 77 5f 62 61 6c 61 6e 63 65 3a 0a 20 20 73 71 ow_balance:. sq
16d6d 6c 69 74 65 33 5f 66 72 65 65 28 61 70 43 65 6c lite3_free(apCel
16d6e 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b l);. return rc;
16d6f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 .}.../*.** The r
16d70 6f 6f 74 20 70 61 67 65 20 69 73 20 6f 76 65 72 oot page is over
16d71 66 75 6c 6c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 full.**.** When
16d72 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 43 72 this happens, Cr
16d73 65 61 74 65 20 61 20 6e 65 77 20 63 68 69 6c 64 eate a new child
16d74 20 70 61 67 65 20 61 6e 64 20 63 6f 70 79 20 74 page and copy t
16d75 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f he.** contents o
16d76 66 20 74 68 65 20 72 6f 6f 74 20 69 6e 74 6f 20 f the root into
16d77 74 68 65 20 63 68 69 6c 64 2e 20 20 54 68 65 6e the child. Then
16d78 20 6d 61 6b 65 20 74 68 65 20 72 6f 6f 74 0a 2a make the root.*
16d79 2a 20 70 61 67 65 20 61 6e 20 65 6d 70 74 79 20 * page an empty
16d7a 70 61 67 65 20 77 69 74 68 20 72 69 67 68 74 43 page with rightC
16d7b 68 69 6c 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f hild pointing to
16d7c 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63 68 69 6c the new.** chil
16d7d 64 2e 20 20 20 46 69 6e 61 6c 6c 79 2c 20 63 61 d. Finally, ca
16d7e 6c 6c 20 62 61 6c 61 6e 63 65 5f 69 6e 74 65 72 ll balance_inter
16d7f 6e 61 6c 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 nal() on the new
16d80 20 63 68 69 6c 64 0a 2a 2a 20 74 6f 20 63 61 75 child.** to cau
16d81 73 65 20 69 74 20 74 6f 20 73 70 6c 69 74 2e 0a se it to split..
16d82 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 */.static int ba
16d83 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 42 74 43 lance_deeper(BtC
16d84 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
16d85 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
16d86 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 /* Return va
16d87 6c 75 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 lue from subproc
16d88 65 64 75 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 edures */. MemP
16d89 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 age *pPage;
16d8a 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
16d8b 65 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 e root page */.
16d8c 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 MemPage *pChild
16d8d 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 ; /* Pointer
16d8e 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 to a new child p
16d8f 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 age */. Pgno pg
16d90 6e 6f 43 68 69 6c 64 3b 20 20 20 20 20 2f 2a 20 noChild; /*
16d91 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 Page number of t
16d92 68 65 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 he new child pag
16d93 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 e */. BtShared
16d94 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 2f 2a *pBt; /*
16d95 20 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 20 20 The BTree */.
16d96 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 int usableSize;
16d97 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 75 73 61 /* Total usa
16d98 62 6c 65 20 73 69 7a 65 20 6f 66 20 61 20 70 61 ble size of a pa
16d99 67 65 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 ge */. u8 *data
16d9a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 ; /* C
16d9b 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 ontent of the pa
16d9c 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20 75 rent page */. u
16d9d 38 20 2a 63 64 61 74 61 3b 20 20 20 20 20 20 20 8 *cdata;
16d9e 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 /* Content of
16d9f 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 the child page
16da0 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 */. int hdr;
16da1 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
16da2 65 74 20 74 6f 20 70 61 67 65 20 68 65 61 64 65 et to page heade
16da3 72 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 r in parent */.
16da4 20 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20 int cbrk;
16da5 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
16da6 6f 20 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 o content of fir
16da7 73 74 20 63 65 6c 6c 20 69 6e 20 70 61 72 65 6e st cell in paren
16da8 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
16da9 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 pCur->iPage==0 )
16daa 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
16dab 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 6e 4f 76 ->apPage[0]->nOv
16dac 65 72 66 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 56 erflow>0 );.. V
16dad 56 41 5f 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 VA_ONLY( pCur->p
16dae 61 67 65 73 53 68 75 66 66 6c 65 64 20 3d 20 31 agesShuffled = 1
16daf 20 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 );. pPage = pC
16db0 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 ur->apPage[0];.
16db1 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 pBt = pPage->pB
16db2 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c t;. assert( sql
16db3 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
16db4 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
16db5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16db6 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
16db7 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
16db8 20 29 3b 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 );. rc = alloc
16db9 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
16dba 2c 20 26 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f , &pChild, &pgno
16dbb 43 68 69 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67 Child, pPage->pg
16dbc 6e 6f 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 no, 0);. if( rc
16dbd 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
16dbe 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
16dbf 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
16dc0 70 43 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 pChild->pDbPage)
16dc1 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 );. usableSize
16dc2 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
16dc3 7a 65 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 ze;. data = pPa
16dc4 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 ge->aData;. hdr
16dc5 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
16dc6 73 65 74 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 set;. cbrk = ge
16dc7 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
16dc8 2b 35 5d 29 3b 0a 20 20 63 64 61 74 61 20 3d 20 +5]);. cdata =
16dc9 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20 pChild->aData;.
16dca 20 6d 65 6d 63 70 79 28 63 64 61 74 61 2c 20 26 memcpy(cdata, &
16dcb 64 61 74 61 5b 68 64 72 5d 2c 20 70 50 61 67 65 data[hdr], pPage
16dcc 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70 ->cellOffset+2*p
16dcd 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29 Page->nCell-hdr)
16dce 3b 0a 20 20 6d 65 6d 63 70 79 28 26 63 64 61 74 ;. memcpy(&cdat
16dcf 61 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 a[cbrk], &data[c
16dd0 62 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 brk], usableSize
16dd1 2d 63 62 72 6b 29 3b 0a 0a 20 20 61 73 73 65 72 -cbrk);.. asser
16dd2 74 28 20 70 43 68 69 6c 64 2d 3e 69 73 49 6e 69 t( pChild->isIni
16dd3 74 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 t==0 );. rc = s
16dd4 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 qlite3BtreeInitP
16dd5 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 69 age(pChild);. i
16dd6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
16dd7 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 ){. int nCop
16dd8 79 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 y = pPage->nOver
16dd9 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 50 61 67 flow*sizeof(pPag
16dda 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 3b 0a 20 20 e->aOvfl[0]);.
16ddb 20 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d memcpy(pChild-
16ddc 3e 61 4f 76 66 6c 2c 20 70 50 61 67 65 2d 3e 61 >aOvfl, pPage->a
16ddd 4f 76 66 6c 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 Ovfl, nCopy);.
16dde 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 pChild->nOverf
16ddf 6c 6f 77 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 low = pPage->nOv
16de0 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 erflow;. if(
16de1 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f pChild->nOverflo
16de2 77 20 29 7b 0a 20 20 20 20 20 20 70 43 68 69 6c w ){. pChil
16de3 64 2d 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 d->nFree = 0;.
16de4 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
16de5 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 pChild->nCell==p
16de6 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 Page->nCell );.
16de7 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
16de8 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
16de9 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
16dea 65 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 e) );. zeroPa
16deb 67 65 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 ge(pPage, pChild
16dec 2d 3e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 ->aData[0] & ~PT
16ded 46 5f 4c 45 41 46 29 3b 0a 20 20 20 20 70 75 74 F_LEAF);. put
16dee 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
16def 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 ata[pPage->hdrOf
16df0 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 fset+8], pgnoChi
16df1 6c 64 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28 ld);. TRACE((
16df2 22 42 41 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 "BALANCE: copy r
16df3 6f 6f 74 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e oot %d into %d\n
16df4 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 ", pPage->pgno,
16df5 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a pChild->pgno));.
16df6 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 if( ISAUTOVA
16df7 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 CUUM ){. rc
16df8 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 = ptrmapPut(pBt
16df9 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 , pChild->pgno,
16dfa 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 PTRMAP_BTREE, pP
16dfb 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e age->pgno);.#ifn
16dfc 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
16dfd 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
16dfe 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
16dff 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
16e00 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 = setChildPtrma
16e01 70 73 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 ps(pChild);.
16e02 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
16e03 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 68 69 ){. pChi
16e04 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 ld->nOverflow =
16e05 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 0;. }.#endi
16e06 66 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 f. }. }.. i
16e07 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
16e08 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 ){. pCur->iP
16e09 61 67 65 2b 2b 3b 0a 20 20 20 20 70 43 75 72 2d age++;. pCur-
16e0a 3e 61 70 50 61 67 65 5b 31 5d 20 3d 20 70 43 68 >apPage[1] = pCh
16e0b 69 6c 64 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 ild;. pCur->a
16e0c 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 iIdx[0] = 0;.
16e0d 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f rc = balance_no
16e0e 6e 72 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d nroot(pCur);. }
16e0f 65 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 else{. releas
16e10 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 ePage(pChild);.
16e11 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
16e12 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 .}../*.** The pa
16e13 67 65 20 74 68 61 74 20 70 43 75 72 20 63 75 72 ge that pCur cur
16e14 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f rently points to
16e15 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 6d has just been m
16e16 6f 64 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f odified in.** so
16e17 6d 65 20 77 61 79 2e 20 54 68 69 73 20 66 75 6e me way. This fun
16e18 63 74 69 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 ction figures ou
16e19 74 20 69 66 20 74 68 69 73 20 6d 6f 64 69 66 69 t if this modifi
16e1a 63 61 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 cation means the
16e1b 0a 2a 2a 20 74 72 65 65 20 6e 65 65 64 73 20 74 .** tree needs t
16e1c 6f 20 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 o be balanced, a
16e1d 6e 64 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 nd if so calls t
16e1e 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 62 he appropriate b
16e1f 61 6c 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 alancing .** rou
16e20 74 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 50 61 72 tine..** .** Par
16e21 61 6d 65 74 65 72 20 69 73 49 6e 73 65 72 74 20 ameter isInsert
16e22 69 73 20 74 72 75 65 20 69 66 20 61 20 6e 65 77 is true if a new
16e23 20 63 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 cell was just i
16e24 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 nserted into the
16e25 0a 2a 2a 20 70 61 67 65 2c 20 6f 72 20 66 61 6c .** page, or fal
16e26 73 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f se otherwise..*/
16e27 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 .static int bala
16e28 6e 63 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 nce(BtCursor *pC
16e29 75 72 2c 20 69 6e 74 20 69 73 49 6e 73 65 72 74 ur, int isInsert
16e2a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
16e2b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 LITE_OK;. MemPa
16e2c 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 ge *pPage = pCur
16e2d 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
16e2e 50 61 67 65 5d 3b 0a 0a 20 20 61 73 73 65 72 74 Page];.. assert
16e2f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
16e30 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
16e31 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 >mutex) );. if(
16e32 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 pCur->iPage==0
16e33 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
16e34 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
16e35 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
16e36 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
16e37 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e E_OK && pPage->n
16e38 4f 76 65 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20 Overflow>0 ){.
16e39 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 rc = balance
16e3a 5f 64 65 65 70 65 72 28 70 43 75 72 29 3b 0a 20 _deeper(pCur);.
16e3b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
16e3c 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3d 3d 70 50 r->apPage[0]==pP
16e3d 61 67 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 age );. ass
16e3e 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ert( pPage->nOve
16e3f 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 72 63 21 3d rflow==0 || rc!=
16e40 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 SQLITE_OK );.
16e41 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
16e42 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 QLITE_OK && pPag
16e43 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 e->nCell==0 ){.
16e44 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 rc = balanc
16e45 65 5f 73 68 61 6c 6c 6f 77 65 72 28 70 43 75 72 e_shallower(pCur
16e46 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
16e47 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d pCur->apPage[0]
16e48 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 20 ==pPage );.
16e49 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
16e4a 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 nOverflow==0 ||
16e4b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc!=SQLITE_OK );
16e4c 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
16e4d 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e if( pPage->n
16e4e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20 Overflow>0 || .
16e4f 20 20 20 20 20 20 20 28 21 69 73 49 6e 73 65 72 (!isInser
16e50 74 20 26 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 t && pPage->nFre
16e51 65 3e 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 e>pPage->pBt->us
16e52 61 62 6c 65 53 69 7a 65 2a 32 2f 33 29 20 29 7b ableSize*2/3) ){
16e53 0a 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 . rc = bala
16e54 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43 75 72 nce_nonroot(pCur
16e55 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 );. }. }. r
16e56 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
16e57 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
16e58 63 68 65 63 6b 73 20 61 6c 6c 20 63 75 72 73 6f checks all curso
16e59 72 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f rs that point to
16e5a 20 74 61 62 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e table pgnoRoot.
16e5b 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 .** If any of th
16e5c 6f 73 65 20 63 75 72 73 6f 72 73 20 77 65 72 65 ose cursors were
16e5d 20 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 46 opened with wrF
16e5e 6c 61 67 3d 3d 30 20 69 6e 20 61 20 64 69 66 66 lag==0 in a diff
16e5f 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62 61 73 erent.** databas
16e60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 61 20 e connection (a
16e61 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
16e62 69 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 ion that shares
16e63 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 63 61 63 the pager.** cac
16e64 68 65 20 77 69 74 68 20 74 68 65 20 63 75 72 72 he with the curr
16e65 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 ent connection)
16e66 61 6e 64 20 74 68 61 74 20 6f 74 68 65 72 20 63 and that other c
16e67 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 onnection .** is
16e68 20 6e 6f 74 20 69 6e 20 74 68 65 20 52 65 61 64 not in the Read
16e69 55 6e 63 6f 6d 6d 6d 69 74 74 65 64 20 73 74 61 Uncommmitted sta
16e6a 74 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f te, then this ro
16e6b 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 0a 2a utine returns .*
16e6c 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e * SQLITE_LOCKED.
16e6d 0a 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 .**.** As well a
16e6e 73 20 63 75 72 73 6f 72 73 20 77 69 74 68 20 77 s cursors with w
16e6f 72 46 6c 61 67 3d 3d 30 2c 20 63 75 72 73 6f 72 rFlag==0, cursor
16e70 73 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 s with wrFlag==1
16e71 20 61 6e 64 20 0a 2a 2a 20 69 73 49 6e 63 72 62 and .** isIncrb
16e72 6c 6f 62 48 61 6e 64 6c 65 3d 3d 31 20 61 72 65 lobHandle==1 are
16e73 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64 also considered
16e74 20 27 72 65 61 64 27 20 63 75 72 73 6f 72 73 2e 'read' cursors.
16e75 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 0a 2a 2a Incremental .**
16e76 20 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 61 72 blob cursors ar
16e77 65 20 75 73 65 64 20 66 6f 72 20 62 6f 74 68 20 e used for both
16e78 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 reading and writ
16e79 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ing..**.** When
16e7a 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74 68 65 20 pgnoRoot is the
16e7b 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 6e 20 root page of an
16e7c 69 6e 74 6b 65 79 20 74 61 62 6c 65 2c 20 74 68 intkey table, th
16e7d 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
16e7e 6c 73 6f 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 62 lso.** responsib
16e7f 6c 65 20 66 6f 72 20 69 6e 76 61 6c 69 64 61 74 le for invalidat
16e80 69 6e 67 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 ing incremental
16e81 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 77 68 65 blob cursors whe
16e82 6e 20 74 68 65 20 74 61 62 6c 65 20 72 6f 77 0a n the table row.
16e83 2a 2a 20 6f 6e 20 77 68 69 63 68 20 74 68 65 79 ** on which they
16e84 20 61 72 65 20 6f 70 65 6e 65 64 20 69 73 20 64 are opened is d
16e85 65 6c 65 74 65 64 20 6f 72 20 6d 6f 64 69 66 69 eleted or modifi
16e86 65 64 2e 20 43 75 72 73 6f 72 73 20 61 72 65 20 ed. Cursors are
16e87 69 6e 76 61 6c 69 64 61 74 65 64 0a 2a 2a 20 61 invalidated.** a
16e88 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 ccording to the
16e89 66 6f 6c 6c 6f 77 69 6e 67 20 72 75 6c 65 73 3a following rules:
16e8a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65 6e .**.** 1) When
16e8b 20 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 BtreeClearTable
16e8c 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 () is called to
16e8d 63 6f 6d 70 6c 65 74 65 6c 79 20 64 65 6c 65 74 completely delet
16e8e 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a e the contents.*
16e8f 2a 20 20 20 20 20 20 6f 66 20 61 20 42 2d 54 72 * of a B-Tr
16e90 65 65 20 74 61 62 6c 65 2c 20 70 45 78 63 6c 75 ee table, pExclu
16e91 64 65 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 de is set to zer
16e92 6f 20 61 6e 64 20 70 61 72 61 6d 65 74 65 72 20 o and parameter
16e93 69 52 6f 77 20 69 73 20 0a 2a 2a 20 20 20 20 20 iRow is .**
16e94 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f set to non-zero
16e95 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 . In this case a
16e96 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 ll incremental b
16e97 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e lob cursors open
16e98 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68 65 20 .** on the
16e99 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 61 74 20 table rooted at
16e9a 70 67 6e 6f 52 6f 6f 74 20 61 72 65 20 69 6e 76 pgnoRoot are inv
16e9b 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 alidated..**.**
16e9c 20 20 32 29 20 57 68 65 6e 20 42 74 72 65 65 49 2) When BtreeI
16e9d 6e 73 65 72 74 28 29 2c 20 42 74 72 65 65 44 65 nsert(), BtreeDe
16e9e 6c 65 74 65 28 29 20 6f 72 20 42 74 72 65 65 50 lete() or BtreeP
16e9f 75 74 44 61 74 61 28 29 20 69 73 20 63 61 6c 6c utData() is call
16ea0 65 64 20 74 6f 20 0a 2a 2a 20 20 20 20 20 20 6d ed to .** m
16ea1 6f 64 69 66 79 20 61 20 74 61 62 6c 65 20 72 6f odify a table ro
16ea2 77 20 76 69 61 20 61 6e 20 53 51 4c 20 73 74 61 w via an SQL sta
16ea3 74 65 6d 65 6e 74 2c 20 70 45 78 63 6c 75 64 65 tement, pExclude
16ea4 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a is set to the .
16ea5 2a 2a 20 20 20 20 20 20 77 72 69 74 65 20 63 75 ** write cu
16ea6 72 73 6f 72 20 75 73 65 64 20 74 6f 20 64 6f 20 rsor used to do
16ea7 74 68 65 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e the modification
16ea8 20 61 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 and parameter i
16ea9 52 6f 77 20 69 73 20 73 65 74 0a 2a 2a 20 20 20 Row is set.**
16eaa 20 20 20 74 6f 20 74 68 65 20 69 6e 74 65 67 65 to the intege
16eab 72 20 72 6f 77 20 69 64 20 6f 66 20 74 68 65 20 r row id of the
16eac 42 2d 54 72 65 65 20 65 6e 74 72 79 20 62 65 69 B-Tree entry bei
16ead 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20 55 6e 6c ng modified. Unl
16eae 65 73 73 0a 2a 2a 20 20 20 20 20 20 70 45 78 63 ess.** pExc
16eaf 6c 75 64 65 20 69 73 20 69 74 73 65 6c 66 20 61 lude is itself a
16eb0 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c n incremental bl
16eb1 6f 62 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 ob cursor, then
16eb2 61 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 0a all incremental.
16eb3 2a 2a 20 20 20 20 20 20 62 6c 6f 62 20 63 75 72 ** blob cur
16eb4 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 72 6f 77 sors open on row
16eb5 20 69 52 6f 77 20 6f 66 20 74 68 65 20 42 2d 54 iRow of the B-T
16eb6 72 65 65 20 61 72 65 20 69 6e 76 61 6c 69 64 61 ree are invalida
16eb7 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 ted..**.** 3)
16eb8 49 66 20 62 6f 74 68 20 70 45 78 63 6c 75 64 65 If both pExclude
16eb9 20 61 6e 64 20 69 52 6f 77 20 61 72 65 20 73 65 and iRow are se
16eba 74 20 74 6f 20 7a 65 72 6f 2c 20 6e 6f 20 69 6e t to zero, no in
16ebb 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 0a cremental blob .
16ebc 2a 2a 20 20 20 20 20 20 63 75 72 73 6f 72 73 20 ** cursors
16ebd 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e are invalidated.
16ebe 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
16ebf 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 0a 20 heckReadLocks(.
16ec0 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 Btree *pBtree,
16ec1 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 . Pgno pgnoRoot
16ec2 2c 20 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 , . BtCursor *p
16ec3 45 78 63 6c 75 64 65 2c 0a 20 20 69 36 34 20 69 Exclude,. i64 i
16ec4 52 6f 77 0a 29 7b 0a 20 20 42 74 43 75 72 73 6f Row.){. BtCurso
16ec5 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72 65 64 r *p;. BtShared
16ec6 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65 2d 3e *pBt = pBtree->
16ec7 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a pBt;. sqlite3 *
16ec8 64 62 20 3d 20 70 42 74 72 65 65 2d 3e 64 62 3b db = pBtree->db;
16ec9 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
16eca 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
16ecb 78 28 70 42 74 72 65 65 29 20 29 3b 0a 20 20 66 x(pBtree) );. f
16ecc 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f or(p=pBt->pCurso
16ecd 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 r; p; p=p->pNext
16ece 29 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d 70 45 ){. if( p==pE
16ecf 78 63 6c 75 64 65 20 29 20 63 6f 6e 74 69 6e 75 xclude ) continu
16ed0 65 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 e;. if( p->pg
16ed1 6e 6f 52 6f 6f 74 21 3d 70 67 6e 6f 52 6f 6f 74 noRoot!=pgnoRoot
16ed2 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 ) continue;.#if
16ed3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16ed4 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 69 66 _INCRBLOB. if
16ed5 28 20 70 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 ( p->isIncrblobH
16ed6 61 6e 64 6c 65 20 26 26 20 28 20 0a 20 20 20 20 andle && ( .
16ed7 20 20 20 20 20 28 21 70 45 78 63 6c 75 64 65 20 (!pExclude
16ed8 26 26 20 69 52 6f 77 29 0a 20 20 20 20 20 20 7c && iRow). |
16ed9 7c 20 28 70 45 78 63 6c 75 64 65 20 26 26 20 21 | (pExclude && !
16eda 70 45 78 63 6c 75 64 65 2d 3e 69 73 49 6e 63 72 pExclude->isIncr
16edb 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 70 2d blobHandle && p-
16edc 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 >info.nKey==iRow
16edd 29 0a 20 20 20 20 29 29 7b 0a 20 20 20 20 20 20 ). )){.
16ede 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 p->eState = CURS
16edf 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 OR_INVALID;.
16ee0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 }.#endif. if(
16ee1 20 70 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 p->eState!=CURS
16ee2 4f 52 5f 56 41 4c 49 44 20 29 20 63 6f 6e 74 69 OR_VALID ) conti
16ee3 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e nue;. if( p->
16ee4 77 72 46 6c 61 67 3d 3d 30 20 0a 23 69 66 6e 64 wrFlag==0 .#ifnd
16ee5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
16ee6 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 7c 7c 20 NCRBLOB. ||
16ee7 70 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e p->isIncrblobHan
16ee8 64 6c 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 29 dle.#endif. )
16ee9 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 {. sqlite3
16eea 2a 64 62 4f 74 68 65 72 20 3d 20 70 2d 3e 70 42 *dbOther = p->pB
16eeb 74 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 tree->db;.
16eec 69 66 28 20 64 62 4f 74 68 65 72 3d 3d 30 20 7c if( dbOther==0 |
16eed 7c 0a 20 20 20 20 20 20 20 20 20 28 64 62 4f 74 |. (dbOt
16eee 68 65 72 21 3d 64 62 20 26 26 20 28 64 62 4f 74 her!=db && (dbOt
16eef 68 65 72 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c her->flags & SQL
16ef0 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 ITE_ReadUncommit
16ef1 74 65 64 29 3d 3d 30 29 20 29 7b 0a 20 20 20 20 ted)==0) ){.
16ef2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16ef3 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20 E_LOCKED;.
16ef4 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
16ef5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
16ef6 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 }../*.** Insert
16ef7 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e 74 a new record int
16ef8 6f 20 74 68 65 20 42 54 72 65 65 2e 20 20 54 68 o the BTree. Th
16ef9 65 20 6b 65 79 20 69 73 20 67 69 76 65 6e 20 62 e key is given b
16efa 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0a 2a 2a y (pKey,nKey).**
16efb 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 69 73 and the data is
16efc 20 67 69 76 65 6e 20 62 79 20 28 70 44 61 74 61 given by (pData
16efd 2c 6e 44 61 74 61 29 2e 20 20 54 68 65 20 63 75 ,nData). The cu
16efe 72 73 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c rsor is used onl
16eff 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e 65 20 77 y to.** define w
16f00 68 61 74 20 74 61 62 6c 65 20 74 68 65 20 72 65 hat table the re
16f01 63 6f 72 64 20 73 68 6f 75 6c 64 20 62 65 20 69 cord should be i
16f02 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 20 20 54 nserted into. T
16f03 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 he cursor.** is
16f04 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
16f05 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61 74 69 a random locati
16f06 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e on..**.** For an
16f07 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 2c 20 6f INTKEY table, o
16f08 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20 76 61 6c nly the nKey val
16f09 75 65 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73 ue of the key is
16f0a 20 75 73 65 64 2e 20 20 70 4b 65 79 20 69 73 0a used. pKey is.
16f0b 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 46 6f 72 ** ignored. For
16f0c 20 61 20 5a 45 52 4f 44 41 54 41 20 74 61 62 6c a ZERODATA tabl
16f0d 65 2c 20 74 68 65 20 70 44 61 74 61 20 61 6e 64 e, the pData and
16f0e 20 6e 44 61 74 61 20 61 72 65 20 62 6f 74 68 20 nData are both
16f0f 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 ignored..*/.SQLI
16f10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
16f11 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 qlite3BtreeInser
16f12 74 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 t(. BtCursor *p
16f13 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 Cur,
16f14 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 64 61 /* Insert da
16f15 74 61 20 69 6e 74 6f 20 74 68 65 20 74 61 62 6c ta into the tabl
16f16 65 20 6f 66 20 74 68 69 73 20 63 75 72 73 6f 72 e of this cursor
16f17 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
16f18 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 *pKey, i64 nKey
16f19 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 , /* The key
16f1a 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 of the new recor
16f1b 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 d */. const voi
16f1c 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 d *pData, int nD
16f1d 61 74 61 2c 20 20 2f 2a 20 54 68 65 20 64 61 74 ata, /* The dat
16f1e 61 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 a of the new rec
16f1f 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 ord */. int nZe
16f20 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ro,
16f21 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
16f22 72 20 6f 66 20 65 78 74 72 61 20 30 20 62 79 74 r of extra 0 byt
16f23 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 es to append to
16f24 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 61 70 data */. int ap
16f25 70 65 6e 64 42 69 61 73 20 20 20 20 20 20 20 20 pendBias
16f26 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
16f27 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65 if this is like
16f28 6c 79 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a ly an append */.
16f29 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 ){. int rc;. i
16f2a 6e 74 20 6c 6f 63 3b 0a 20 20 69 6e 74 20 73 7a nt loc;. int sz
16f2b 4e 65 77 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a New;. int idx;.
16f2c 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
16f2d 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 ;. Btree *p = p
16f2e 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 Cur->pBtree;. B
16f2f 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
16f30 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e 65 ->pBt;. unsigne
16f31 64 20 63 68 61 72 20 2a 6f 6c 64 43 65 6c 6c 3b d char *oldCell;
16f32 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
16f33 20 2a 6e 65 77 43 65 6c 6c 20 3d 20 30 3b 0a 0a *newCell = 0;..
16f34 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
16f35 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
16f36 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 );. assert( pB
16f37 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
16f38 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
16f39 0a 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d . assert( !pBt-
16f3a 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 >readOnly );. a
16f3b 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 ssert( pCur->wrF
16f3c 6c 61 67 20 29 3b 0a 20 20 69 66 28 20 63 68 65 lag );. if( che
16f3d 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 43 75 72 ckReadLocks(pCur
16f3e 2d 3e 70 42 74 72 65 65 2c 20 70 43 75 72 2d 3e ->pBtree, pCur->
16f3f 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2c 20 pgnoRoot, pCur,
16f40 6e 4b 65 79 29 20 29 7b 0a 20 20 20 20 72 65 74 nKey) ){. ret
16f41 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 urn SQLITE_LOCKE
16f42 44 3b 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 D; /* The table
16f43 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 68 pCur points to h
16f44 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a as a read lock *
16f45 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 /. }. if( pCur
16f46 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
16f47 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 _FAULT ){. re
16f48 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b turn pCur->skip;
16f49 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 . }.. /* Save
16f4a 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 the positions of
16f4b 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73 6f any other curso
16f4c 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 rs open on this
16f4d 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 table */. sqlit
16f4e 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 e3BtreeClearCurs
16f4f 6f 72 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 or(pCur);. if(
16f50 0a 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d . SQLITE_OK!=
16f51 28 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 (rc = saveAllCur
16f52 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e sors(pBt, pCur->
16f53 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 29 pgnoRoot, pCur))
16f54 20 7c 7c 0a 20 20 20 20 53 51 4c 49 54 45 5f 4f ||. SQLITE_O
16f55 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K!=(rc = sqlite3
16f56 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 BtreeMoveto(pCur
16f57 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 70 , pKey, nKey, ap
16f58 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 29 pendBias, &loc))
16f59 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e . ){. return
16f5a 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 67 rc;. }.. pPag
16f5b 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
16f5c 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
16f5d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
16f5e 69 6e 74 4b 65 79 20 7c 7c 20 6e 4b 65 79 3e 3d intKey || nKey>=
16f5f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
16f60 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21 70 Page->leaf || !p
16f61 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a Page->intKey );.
16f62 20 20 54 52 41 43 45 28 28 22 49 4e 53 45 52 54 TRACE(("INSERT
16f63 3a 20 74 61 62 6c 65 3d 25 64 20 6e 6b 65 79 3d : table=%d nkey=
16f64 25 6c 6c 64 20 6e 64 61 74 61 3d 25 64 20 70 61 %lld ndata=%d pa
16f65 67 65 3d 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 ge=%d %s\n",.
16f66 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e pCur->pgn
16f67 6f 52 6f 6f 74 2c 20 6e 4b 65 79 2c 20 6e 44 61 oRoot, nKey, nDa
16f68 74 61 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c ta, pPage->pgno,
16f69 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 3d 3d . loc==
16f6a 30 20 3f 20 22 6f 76 65 72 77 72 69 74 65 22 20 0 ? "overwrite"
16f6b 3a 20 22 6e 65 77 20 65 6e 74 72 79 22 29 29 3b : "new entry"));
16f6c 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
16f6d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 6c ->isInit );. al
16f6e 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 28 locateTempSpace(
16f6f 70 42 74 29 3b 0a 20 20 6e 65 77 43 65 6c 6c 20 pBt);. newCell
16f70 3d 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 = pBt->pTmpSpace
16f71 3b 0a 20 20 69 66 28 20 6e 65 77 43 65 6c 6c 3d ;. if( newCell=
16f72 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =0 ) return SQLI
16f73 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 63 20 3d TE_NOMEM;. rc =
16f74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50 61 67 fillInCell(pPag
16f75 65 2c 20 6e 65 77 43 65 6c 6c 2c 20 70 4b 65 79 e, newCell, pKey
16f76 2c 20 6e 4b 65 79 2c 20 70 44 61 74 61 2c 20 6e , nKey, pData, n
16f77 44 61 74 61 2c 20 6e 5a 65 72 6f 2c 20 26 73 7a Data, nZero, &sz
16f78 4e 65 77 29 3b 0a 20 20 69 66 28 20 72 63 20 29 New);. if( rc )
16f79 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 goto end_insert
16f7a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 4e 65 ;. assert( szNe
16f7b 77 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 w==cellSizePtr(p
16f7c 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c 29 20 29 Page, newCell) )
16f7d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 4e 65 ;. assert( szNe
16f7e 77 3c 3d 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 w<=MX_CELL_SIZE(
16f7f 70 42 74 29 20 29 3b 0a 20 20 69 64 78 20 3d 20 pBt) );. idx =
16f80 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
16f81 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 66 28 20 ->iPage];. if(
16f82 6c 6f 63 3d 3d 30 20 26 26 20 43 55 52 53 4f 52 loc==0 && CURSOR
16f83 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 _VALID==pCur->eS
16f84 74 61 74 65 20 29 7b 0a 20 20 20 20 75 31 36 20 tate ){. u16
16f85 73 7a 4f 6c 64 3b 0a 20 20 20 20 61 73 73 65 72 szOld;. asser
16f86 74 28 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 t( idx<pPage->nC
16f87 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ell );. rc =
16f88 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
16f89 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
16f8a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b );. if( rc ){
16f8b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f . goto end_
16f8c 69 6e 73 65 72 74 3b 0a 20 20 20 20 7d 0a 20 20 insert;. }.
16f8d 20 20 6f 6c 64 43 65 6c 6c 20 3d 20 66 69 6e 64 oldCell = find
16f8e 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 Cell(pPage, idx)
16f8f 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 ;. if( !pPage
16f90 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
16f91 6d 65 6d 63 70 79 28 6e 65 77 43 65 6c 6c 2c 20 memcpy(newCell,
16f92 6f 6c 64 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 oldCell, 4);.
16f93 20 7d 0a 20 20 20 20 73 7a 4f 6c 64 20 3d 20 63 }. szOld = c
16f94 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 ellSizePtr(pPage
16f95 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 , oldCell);.
16f96 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 rc = clearCell(p
16f97 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a Page, oldCell);.
16f98 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
16f99 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 o end_insert;.
16f9a 20 20 72 63 20 3d 20 64 72 6f 70 43 65 6c 6c 28 rc = dropCell(
16f9b 70 50 61 67 65 2c 20 69 64 78 2c 20 73 7a 4f 6c pPage, idx, szOl
16f9c 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d d);. if( rc!=
16f9d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 SQLITE_OK ) {.
16f9e 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 goto end_ins
16f9f 65 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c ert;. }. }el
16fa0 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26 26 20 se if( loc<0 &&
16fa1 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 pPage->nCell>0 )
16fa2 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
16fa3 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 20 age->leaf );.
16fa4 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 idx = ++pCur->a
16fa5 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
16fa6 5d 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 ];. pCur->inf
16fa7 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 o.nSize = 0;.
16fa8 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 pCur->validNKey
16fa9 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
16faa 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
16fab 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20 ->leaf );. }.
16fac 72 63 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 rc = insertCell(
16fad 70 50 61 67 65 2c 20 69 64 78 2c 20 6e 65 77 43 pPage, idx, newC
16fae 65 6c 6c 2c 20 73 7a 4e 65 77 2c 20 30 2c 20 30 ell, szNew, 0, 0
16faf 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
16fb0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 ITE_OK ){. rc
16fb1 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c = balance(pCur,
16fb2 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 1);. }.. /* M
16fb3 75 73 74 20 6d 61 6b 65 20 73 75 72 65 20 6e 4f ust make sure nO
16fb4 76 65 72 66 6c 6f 77 20 69 73 20 72 65 73 65 74 verflow is reset
16fb5 20 74 6f 20 7a 65 72 6f 20 65 76 65 6e 20 69 66 to zero even if
16fb6 20 74 68 65 20 62 61 6c 61 6e 63 65 28 29 0a 20 the balance().
16fb7 20 2a 2a 20 66 61 69 6c 73 2e 20 20 49 6e 74 65 ** fails. Inte
16fb8 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 rnal data struct
16fb9 75 72 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 ure corruption w
16fba 69 6c 6c 20 72 65 73 75 6c 74 20 6f 74 68 65 72 ill result other
16fbb 77 69 73 65 2e 20 2a 2f 0a 20 20 70 43 75 72 2d wise. */. pCur-
16fbc 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
16fbd 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 age]->nOverflow
16fbe 3d 20 30 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d = 0;.. if( rc==
16fbf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16fc0 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 moveToRoot(pCur
16fc1 29 3b 0a 20 20 7d 0a 65 6e 64 5f 69 6e 73 65 72 );. }.end_inser
16fc2 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a t:. return rc;.
16fc3 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
16fc4 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 the entry that t
16fc5 68 65 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 he cursor is poi
16fc6 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 63 nting to. The c
16fc7 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74 ursor.** is left
16fc8 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 61 pointing at a a
16fc9 72 62 69 74 72 61 72 79 20 6c 6f 63 61 74 69 6f rbitrary locatio
16fca 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
16fcb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16fcc 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43 75 BtreeDelete(BtCu
16fcd 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d rsor *pCur){. M
16fce 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 emPage *pPage =
16fcf 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
16fd0 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 6e 74 r->iPage];. int
16fd1 20 69 64 78 3b 0a 20 20 75 6e 73 69 67 6e 65 64 idx;. unsigned
16fd2 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 char *pCell;.
16fd3 69 6e 74 20 72 63 3b 0a 20 20 50 67 6e 6f 20 70 int rc;. Pgno p
16fd4 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b 0a 20 20 gnoChild = 0;.
16fd5 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d Btree *p = pCur-
16fd6 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 >pBtree;. BtSha
16fd7 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
16fd8 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 t;.. assert( cu
16fd9 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
16fda 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
16fdb 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 ( pPage->isInit
16fdc 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 );. assert( pBt
16fdd 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
16fde 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
16fdf 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e assert( !pBt->
16fe0 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 69 66 readOnly );. if
16fe1 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
16fe2 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a CURSOR_FAULT ){.
16fe3 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d return pCur-
16fe4 3e 73 6b 69 70 3b 0a 20 20 7d 0a 20 20 69 66 28 >skip;. }. if(
16fe5 20 4e 45 56 45 52 28 70 43 75 72 2d 3e 61 69 49 NEVER(pCur->aiI
16fe6 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e dx[pCur->iPage]>
16fe7 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 20 29 =pPage->nCell) )
16fe8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
16fe9 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f 2a 20 54 ITE_ERROR; /* T
16fea 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 he cursor is not
16feb 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e 79 pointing to any
16fec 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 61 thing */. }. a
16fed 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 ssert( pCur->wrF
16fee 6c 61 67 20 29 3b 0a 20 20 69 66 28 20 63 68 65 lag );. if( che
16fef 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 43 75 72 ckReadLocks(pCur
16ff0 2d 3e 70 42 74 72 65 65 2c 20 70 43 75 72 2d 3e ->pBtree, pCur->
16ff1 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2c 20 pgnoRoot, pCur,
16ff2 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 pCur->info.nKey)
16ff3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
16ff4 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a QLITE_LOCKED; /*
16ff5 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20 The table pCur
16ff6 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20 points to has a
16ff7 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d read lock */. }
16ff8 0a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20 74 .. /* Restore t
16ff9 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f he current curso
16ffa 72 20 70 6f 73 69 74 69 6f 6e 20 28 61 20 6e 6f r position (a no
16ffb 2d 6f 70 20 69 66 20 74 68 65 20 63 75 72 73 6f -op if the curso
16ffc 72 20 69 73 20 6e 6f 74 20 69 6e 20 0a 20 20 2a r is not in . *
16ffd 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 * CURSOR_REQUIRE
16ffe 53 45 45 4b 20 73 74 61 74 65 29 20 61 6e 64 20 SEEK state) and
16fff 73 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f save the positio
17000 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 ns of any other
17001 63 75 72 73 6f 72 73 20 0a 20 20 2a 2a 20 6f 70 cursors . ** op
17002 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 en on the same t
17003 61 62 6c 65 2e 20 54 68 65 6e 20 63 61 6c 6c 20 able. Then call
17004 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
17005 65 28 29 20 6f 6e 20 74 68 65 20 70 61 67 65 0a e() on the page.
17006 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 65 6e ** that the en
17007 74 72 79 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 try will be dele
17008 74 65 64 20 66 72 6f 6d 2e 0a 20 20 2a 2f 0a 20 ted from.. */.
17009 20 69 66 28 20 0a 20 20 20 20 28 72 63 20 3d 20 if( . (rc =
1700a 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 restoreCursorPos
1700b 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d 30 20 ition(pCur))!=0
1700c 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73 61 76 ||. (rc = sav
1700d 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c eAllCursors(pBt,
1700e 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c pCur->pgnoRoot,
1700f 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a 20 20 pCur))!=0 ||.
17010 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 (rc = sqlite3P
17011 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d agerWrite(pPage-
17012 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a 20 20 >pDbPage))!=0.
17013 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
17014 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 ;. }.. /* Loca
17015 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 te the cell with
17016 69 6e 20 69 74 73 20 70 61 67 65 20 61 6e 64 20 in its page and
17017 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f 69 6e leave pCell poin
17018 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20 2a 2a ting to the. **
17019 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65 61 72 data. The clear
1701a 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72 65 65 Cell() call free
1701b 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 s any overflow p
1701c 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 ages associated
1701d 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 63 65 with the. ** ce
1701e 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69 74 73 ll. The cell its
1701f 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69 6e 74 elf is still int
17020 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 64 78 20 act.. */. idx
17021 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 = pCur->aiIdx[pC
17022 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 70 43 ur->iPage];. pC
17023 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
17024 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 69 66 Page, idx);. if
17025 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
17026 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 20 {. pgnoChild
17027 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c = get4byte(pCell
17028 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c );. }. rc = cl
17029 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 earCell(pPage, p
1702a 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72 63 20 Cell);. if( rc
1702b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
1702c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 ;. }.. if( !pP
1702d 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
1702e 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 65 /*. ** The e
1702f 6e 74 72 79 20 77 65 20 61 72 65 20 61 62 6f 75 ntry we are abou
17030 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e t to delete is n
17031 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69 66 20 ot a leaf so if
17032 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20 2a 2a we do not. **
17033 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 do something we
17034 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20 68 6f will leave a ho
17035 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61 le on an interna
17036 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a 20 57 l page.. ** W
17037 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c 20 74 e have to fill t
17038 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76 69 6e he hole by movin
17039 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72 6f 6d g in a cell from
1703a 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a 20 20 a leaf. The.
1703b 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c 20 61 ** next Cell a
1703c 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74 6f 20 fter the one to
1703d 62 65 20 64 65 6c 65 74 65 64 20 69 73 20 67 75 be deleted is gu
1703e 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 73 aranteed to exis
1703f 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 6f 20 t and. ** to
17040 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77 65 20 be a leaf so we
17041 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20 20 20 can use it..
17042 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 */. BtCursor
17043 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 4d 65 6d leafCur;. Mem
17044 50 61 67 65 20 2a 70 4c 65 61 66 50 61 67 65 20 Page *pLeafPage
17045 3d 20 30 3b 0a 0a 20 20 20 20 75 6e 73 69 67 6e = 0;.. unsign
17046 65 64 20 63 68 61 72 20 2a 70 4e 65 78 74 3b 0a ed char *pNext;.
17047 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b int notUsed;
17048 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 . unsigned ch
17049 61 72 20 2a 74 65 6d 70 43 65 6c 6c 20 3d 20 30 ar *tempCell = 0
1704a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 ;. assert( !p
1704b 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a Page->intKey );.
1704c 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1704d 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 70 43 GetTempCursor(pC
1704e 75 72 2c 20 26 6c 65 61 66 43 75 72 29 3b 0a 20 ur, &leafCur);.
1704f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
17050 74 72 65 65 4e 65 78 74 28 26 6c 65 61 66 43 75 treeNext(&leafCu
17051 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20 r, ¬Used);.
17052 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
17053 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 _OK ){. ass
17054 65 72 74 28 20 6c 65 61 66 43 75 72 2e 61 69 49 ert( leafCur.aiI
17055 64 78 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 dx[leafCur.iPage
17056 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4c ]==0 );. pL
17057 65 61 66 50 61 67 65 20 3d 20 6c 65 61 66 43 75 eafPage = leafCu
17058 72 2e 61 70 50 61 67 65 5b 6c 65 61 66 43 75 72 r.apPage[leafCur
17059 2e 69 50 61 67 65 5d 3b 0a 20 20 20 20 20 20 72 .iPage];. r
1705a 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1705b 57 72 69 74 65 28 70 4c 65 61 66 50 61 67 65 2d Write(pLeafPage-
1705c 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 7d >pDbPage);. }
1705d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1705e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1705f 69 6e 74 20 6c 65 61 66 43 75 72 73 6f 72 49 6e int leafCursorIn
17060 76 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 valid = 0;.
17061 20 75 31 36 20 73 7a 4e 65 78 74 3b 0a 20 20 20 u16 szNext;.
17062 20 20 20 54 52 41 43 45 28 28 22 44 45 4c 45 54 TRACE(("DELET
17063 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65 6c 65 E: table=%d dele
17064 74 65 20 69 6e 74 65 72 6e 61 6c 20 66 72 6f 6d te internal from
17065 20 25 64 20 72 65 70 6c 61 63 65 20 66 72 6f 6d %d replace from
17066 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 leaf %d\n",.
17067 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f pCur->pgno
17068 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e Root, pPage->pgn
17069 6f 2c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67 o, pLeafPage->pg
1706a 6e 6f 29 29 3b 0a 20 20 20 20 20 20 64 72 6f 70 no));. drop
1706b 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c Cell(pPage, idx,
1706c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
1706d 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 ge, pCell));.
1706e 20 20 20 70 4e 65 78 74 20 3d 20 66 69 6e 64 43 pNext = findC
1706f 65 6c 6c 28 70 4c 65 61 66 50 61 67 65 2c 20 30 ell(pLeafPage, 0
17070 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 78 74 20 );. szNext
17071 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c = cellSizePtr(pL
17072 65 61 66 50 61 67 65 2c 20 70 4e 65 78 74 29 3b eafPage, pNext);
17073 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 4d . assert( M
17074 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 X_CELL_SIZE(pBt)
17075 3e 3d 73 7a 4e 65 78 74 2b 34 20 29 3b 0a 20 20 >=szNext+4 );.
17076 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 allocateTemp
17077 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 Space(pBt);.
17078 20 20 74 65 6d 70 43 65 6c 6c 20 3d 20 70 42 74 tempCell = pBt
17079 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 ->pTmpSpace;.
1707a 20 20 20 69 66 28 20 74 65 6d 70 43 65 6c 6c 3d if( tempCell=
1707b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 =0 ){. rc
1707c 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
1707d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
1707e 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1707f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
17080 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 insertCell(pPag
17081 65 2c 20 69 64 78 2c 20 70 4e 65 78 74 2d 34 2c e, idx, pNext-4,
17082 20 73 7a 4e 65 78 74 2b 34 2c 20 74 65 6d 70 43 szNext+4, tempC
17083 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d ell, 0);. }
17084 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ... /* The
17085 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74 20 69 "if" statement i
17086 6e 20 74 68 65 20 6e 65 78 74 20 63 6f 64 65 20 n the next code
17087 62 6c 6f 63 6b 20 69 73 20 63 72 69 74 69 63 61 block is critica
17088 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a l. The. **
17089 20 73 6c 69 67 68 74 65 73 74 20 65 72 72 6f 72 slightest error
1708a 20 69 6e 20 74 68 61 74 20 73 74 61 74 65 6d 65 in that stateme
1708b 6e 74 20 77 6f 75 6c 64 20 61 6c 6c 6f 77 20 53 nt would allow S
1708c 51 4c 69 74 65 20 74 6f 20 6f 70 65 72 61 74 65 QLite to operate
1708d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 63 . ** correc
1708e 74 6c 79 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 tly most of the
1708f 74 69 6d 65 20 62 75 74 20 70 72 6f 64 75 63 65 time but produce
17090 20 76 65 72 79 20 72 61 72 65 20 66 61 69 6c 75 very rare failu
17091 72 65 73 2e 20 20 54 6f 0a 20 20 20 20 20 20 2a res. To. *
17092 2a 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 * guard against
17093 74 68 69 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 this, the follow
17094 69 6e 67 20 6d 61 63 72 6f 73 20 68 65 6c 70 20 ing macros help
17095 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 0a 20 to verify that.
17096 20 20 20 20 20 2a 2a 20 74 68 65 20 22 69 66 22 ** the "if"
17097 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 77 65 statement is we
17098 6c 6c 20 74 65 73 74 65 64 2e 0a 20 20 20 20 20 ll tested..
17099 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 */. testca
1709a 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 se( pPage->nOver
1709b 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 flow==0 && pPage
1709c 2d 3e 6e 46 72 65 65 3c 70 42 74 2d 3e 75 73 61 ->nFree<pBt->usa
1709d 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 20 bleSize*2/3 .
1709e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 &&
1709f 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 pLeafPage->nFre
170a0 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 74 e+2+szNext > pBt
170a1 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 ->usableSize*2/3
170a2 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
170a3 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 se( pPage->nOver
170a4 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 flow==0 && pPage
170a5 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 73 ->nFree==pBt->us
170a6 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 ableSize*2/3 .
170a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
170a8 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 & pLeafPage->nFr
170a9 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 ee+2+szNext > pB
170aa 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f t->usableSize*2/
170ab 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 3 );. testc
170ac 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ase( pPage->nOve
170ad 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 rflow==0 && pPag
170ae 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 e->nFree==pBt->u
170af 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 sableSize*2/3+1
170b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
170b1 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e && pLeafPage->
170b2 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e nFree+2+szNext >
170b3 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
170b4 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 *2/3 );. te
170b5 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e stcase( pPage->n
170b6 4f 76 65 72 66 6c 6f 77 3e 30 20 26 26 20 70 50 Overflow>0 && pP
170b7 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 70 42 74 2d age->nFree<=pBt-
170b8 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 0a >usableSize*2/3.
170b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
170ba 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e && pLeafPage->n
170bb 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 Free+2+szNext >
170bc 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a pBt->usableSize*
170bd 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 2/3 );. tes
170be 74 63 61 73 65 28 20 28 70 50 61 67 65 2d 3e 6e tcase( (pPage->n
170bf 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 70 Overflow>0 || (p
170c0 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 42 Page->nFree > pB
170c1 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f t->usableSize*2/
170c2 33 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 3)).
170c3 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 && pLeafPag
170c4 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 e->nFree+2+szNex
170c5 74 20 3d 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 t == pBt->usable
170c6 53 69 7a 65 2a 32 2f 33 20 29 3b 0a 0a 0a 20 20 Size*2/3 );...
170c7 20 20 20 20 69 66 28 20 28 70 50 61 67 65 2d 3e if( (pPage->
170c8 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 nOverflow>0 || (
170c9 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 pPage->nFree > p
170ca 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 Bt->usableSize*2
170cb 2f 33 29 29 20 26 26 0a 20 20 20 20 20 20 20 20 /3)) &&.
170cc 20 20 28 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 (pLeafPage->nF
170cd 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 ree+2+szNext > p
170ce 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 Bt->usableSize*2
170cf 2f 33 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 /3). ){.
170d0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
170d1 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 69 66 20 nch is taken if
170d2 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 the internal nod
170d3 65 20 69 73 20 6e 6f 77 20 65 69 74 68 65 72 20 e is now either
170d4 6f 76 65 72 66 6c 6f 77 69 6e 67 0a 20 20 20 20 overflowing.
170d5 20 20 20 20 2a 2a 20 6f 72 20 75 6e 64 65 72 66 ** or underf
170d6 75 6c 6c 20 61 6e 64 20 74 68 65 20 6c 65 61 66 ull and the leaf
170d7 20 6e 6f 64 65 20 77 69 6c 6c 20 62 65 20 75 6e node will be un
170d8 64 65 72 66 75 6c 6c 20 61 66 74 65 72 20 74 68 derfull after th
170d9 65 20 6a 75 73 74 20 63 65 6c 6c 20 0a 20 20 20 e just cell .
170da 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 74 ** copied t
170db 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e o the internal n
170dc 6f 64 65 20 69 73 20 64 65 6c 65 74 65 64 20 66 ode is deleted f
170dd 72 6f 6d 20 69 74 2e 20 54 68 69 73 20 69 73 20 rom it. This is
170de 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 a special.
170df 20 20 2a 2a 20 63 61 73 65 20 62 65 63 61 75 73 ** case becaus
170e0 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 62 61 e the call to ba
170e1 6c 61 6e 63 65 28 29 20 74 6f 20 63 6f 72 72 65 lance() to corre
170e2 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ct the internal
170e3 6e 6f 64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 node. **
170e4 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 74 may change the t
170e5 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e ree structure an
170e6 64 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 d invalidate the
170e7 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 20 20 20 contents of.
170e8 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 61 66 ** the leaf
170e9 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64 Cur.apPage[] and
170ea 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b 5d leafCur.aiIdx[]
170eb 20 61 72 72 61 79 73 2c 20 77 68 69 63 68 20 77 arrays, which w
170ec 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a ill be. *
170ed 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 62 61 * used by the ba
170ee 6c 61 6e 63 65 28 29 20 72 65 71 75 69 72 65 64 lance() required
170ef 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 65 20 to correct the
170f0 75 6e 64 65 72 66 75 6c 6c 20 6c 65 61 66 0a 20 underfull leaf.
170f1 20 20 20 20 20 20 20 2a 2a 20 6e 6f 64 65 2e 0a ** node..
170f2 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
170f3 20 20 20 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c ** The formul
170f4 61 20 75 73 65 64 20 69 6e 20 74 68 65 20 65 78 a used in the ex
170f5 70 72 65 73 73 69 6f 6e 20 61 62 6f 76 65 20 61 pression above a
170f6 72 65 20 62 61 73 65 64 20 6f 6e 20 66 61 63 65 re based on face
170f7 74 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a ts of. **
170f8 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 the SQLite file
170f9 2d 66 6f 72 6d 61 74 20 74 68 61 74 20 64 6f 20 -format that do
170fa 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 72 20 not change over
170fb 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f time.. */
170fc 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
170fd 65 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3d e( pPage->nFree=
170fe 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 =pBt->usableSize
170ff 2a 32 2f 33 2b 31 20 29 3b 0a 20 20 20 20 20 20 *2/3+1 );.
17100 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 61 testcase( pLea
17101 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 fPage->nFree+2+s
17102 7a 4e 65 78 74 3d 3d 70 42 74 2d 3e 75 73 61 62 zNext==pBt->usab
17103 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 29 3b 0a leSize*2/3+1 );.
17104 20 20 20 20 20 20 20 20 6c 65 61 66 43 75 72 73 leafCurs
17105 6f 72 49 6e 76 61 6c 69 64 20 3d 20 31 3b 0a 20 orInvalid = 1;.
17106 20 20 20 20 20 7d 20 20 20 20 20 20 20 20 0a 0a } ..
17107 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
17108 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17109 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
1710a 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
1710b 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
1710c 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 75 e) );. pu
1710d 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66 t4byte(findOverf
1710e 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 lowCell(pPage, i
1710f 64 78 29 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b dx), pgnoChild);
17110 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c . VVA_ONL
17111 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68 Y( pCur->pagesSh
17112 75 66 66 6c 65 64 20 3d 20 30 20 29 3b 0a 20 20 uffled = 0 );.
17113 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e rc = balan
17114 63 65 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 20 ce(pCur, 0);.
17115 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 }.. if(
17116 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
17117 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 61 6c leafCursorInval
17118 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a id ){. /*
17119 20 54 68 65 20 6c 65 61 66 2d 6e 6f 64 65 20 69 The leaf-node i
1711a 73 20 6e 6f 77 20 75 6e 64 65 72 66 75 6c 6c 20 s now underfull
1711b 61 6e 64 20 73 6f 20 74 68 65 20 74 72 65 65 20 and so the tree
1711c 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 20 20 20 needs to be .
1711d 20 20 20 20 20 2a 2a 20 72 65 62 61 6c 61 6e 63 ** rebalanc
1711e 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74 68 65 ed. However, the
1711f 20 62 61 6c 61 6e 63 65 28 29 20 6f 70 65 72 61 balance() opera
17120 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e 74 65 tion on the inte
17121 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 rnal. **
17122 6e 6f 64 65 20 61 62 6f 76 65 20 6d 61 79 20 68 node above may h
17123 61 76 65 20 6d 6f 64 69 66 69 65 64 20 74 68 65 ave modified the
17124 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 structure of th
17125 65 20 42 2d 54 72 65 65 20 61 6e 64 0a 20 20 20 e B-Tree and.
17126 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 65 20 63 ** so the c
17127 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 urrent contents
17128 6f 66 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67 of leafCur.apPag
17129 65 5b 5d 20 61 6e 64 20 6c 65 61 66 43 75 72 2e e[] and leafCur.
1712a 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 aiIdx[].
1712b 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 72 ** may not be tr
1712c 75 73 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a usted.. *
1712d 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74 20 *. ** It
1712e 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
1712f 74 6f 20 63 6f 70 79 20 74 68 65 20 61 6e 63 65 to copy the ance
17130 73 74 72 79 20 66 72 6f 6d 20 70 43 75 72 2c 20 stry from pCur,
17131 61 73 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 as the same.
17132 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 28 29 ** balance()
17133 20 63 61 6c 6c 20 68 61 73 20 69 6e 76 61 6c 69 call has invali
17134 64 61 74 65 64 20 74 68 65 20 70 43 75 72 2d 3e dated the pCur->
17135 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 61 69 49 apPage[] and aiI
17136 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 2a 2a 20 dx[]. **
17137 61 72 72 61 79 73 2e 20 0a 20 20 20 20 20 20 20 arrays. .
17138 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 **. ** T
17139 68 65 20 63 61 6c 6c 20 74 6f 20 73 61 76 65 43 he call to saveC
1713a 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 ursorPosition()
1713b 62 65 6c 6f 77 20 69 6e 74 65 72 6e 61 6c 6c 79 below internally
1713c 20 73 61 76 65 73 20 74 68 65 20 0a 20 20 20 20 saves the .
1713d 20 20 20 20 2a 2a 20 6b 65 79 20 74 68 61 74 20 ** key that
1713e 6c 65 61 66 43 75 72 20 69 73 20 63 75 72 72 65 leafCur is curre
1713f 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f ntly pointing to
17140 2e 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 . Currently, the
17141 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 72 re. ** ar
17142 65 20 74 77 6f 20 63 6f 70 69 65 73 20 6f 66 20 e two copies of
17143 74 68 61 74 20 6b 65 79 20 69 6e 20 74 68 65 20 that key in the
17144 74 72 65 65 20 2d 20 6f 6e 65 20 68 65 72 65 20 tree - one here
17145 6f 6e 20 74 68 65 20 6c 65 61 66 0a 20 20 20 20 on the leaf.
17146 20 20 20 20 2a 2a 20 70 61 67 65 20 61 6e 64 20 ** page and
17147 6f 6e 65 20 6f 6e 20 73 6f 6d 65 20 69 6e 74 65 one on some inte
17148 72 6e 61 6c 20 6e 6f 64 65 20 69 6e 20 74 68 65 rnal node in the
17149 20 74 72 65 65 2e 20 54 68 65 20 63 6f 70 79 20 tree. The copy
1714a 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 on. ** th
1714b 65 20 6c 65 61 66 20 6e 6f 64 65 20 69 73 20 61 e leaf node is a
1714c 6c 77 61 79 73 20 74 68 65 20 6e 65 78 74 20 6b lways the next k
1714d 65 79 20 69 6e 20 74 72 65 65 2d 6f 72 64 65 72 ey in tree-order
1714e 20 61 66 74 65 72 20 74 68 65 20 0a 20 20 20 20 after the .
1714f 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 6e 20 74 ** copy on t
17150 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 he internal node
17151 2e 20 53 6f 2c 20 74 68 65 20 63 61 6c 6c 20 74 . So, the call t
17152 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 o sqlite3BtreeNe
17153 78 74 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 xt(). **
17154 63 61 6c 6c 73 20 72 65 73 74 6f 72 65 43 75 72 calls restoreCur
17155 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 74 6f sorPosition() to
17156 20 70 6f 69 6e 74 20 74 68 65 20 63 75 72 73 6f point the curso
17157 72 20 74 6f 20 74 68 65 20 63 6f 70 79 0a 20 20 r to the copy.
17158 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 ** stored
17159 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 on the internal
1715a 6e 6f 64 65 2c 20 74 68 65 6e 20 61 64 76 61 6e node, then advan
1715b 63 65 73 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ces to the next
1715c 65 6e 74 72 79 2c 0a 20 20 20 20 20 20 20 20 2a entry,. *
1715d 2a 20 77 68 69 63 68 20 68 61 70 70 65 6e 73 20 * which happens
1715e 74 6f 20 62 65 20 74 68 65 20 63 6f 70 79 20 6f to be the copy o
1715f 66 20 74 68 65 20 6b 65 79 20 6f 6e 20 74 68 65 f the key on the
17160 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 0a internal node..
17161 20 20 20 20 20 20 20 20 2a 2a 20 4e 65 74 20 65 ** Net e
17162 66 66 65 63 74 3a 20 6c 65 61 66 43 75 72 20 69 ffect: leafCur i
17163 73 20 70 6f 69 6e 74 69 6e 67 20 62 61 63 6b 20 s pointing back
17164 74 6f 20 74 68 65 20 64 75 70 6c 69 63 61 74 65 to the duplicate
17165 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a cell. **
17166 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 that needs to b
17167 65 20 72 65 6d 6f 76 65 64 2c 20 61 6e 64 20 74 e removed, and t
17168 68 65 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67 he leafCur.apPag
17169 65 5b 5d 20 61 6e 64 0a 20 20 20 20 20 20 20 20 e[] and.
1716a 2a 2a 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 ** leafCur.aiIdx
1716b 5b 5d 20 61 72 72 61 79 73 20 61 72 65 20 63 6f [] arrays are co
1716c 72 72 65 63 74 2e 0a 20 20 20 20 20 20 20 20 2a rrect.. *
1716d 2f 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e /. VVA_ON
1716e 4c 59 28 20 50 67 6e 6f 20 6c 65 61 66 50 67 6e LY( Pgno leafPgn
1716f 6f 20 3d 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 o = pLeafPage->p
17170 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 20 20 72 gno );. r
17171 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f c = saveCursorPo
17172 73 69 74 69 6f 6e 28 26 6c 65 61 66 43 75 72 29 sition(&leafCur)
17173 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
17174 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
17175 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
17176 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 26 lite3BtreeNext(&
17177 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74 55 73 65 leafCur, ¬Use
17178 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 d);. }.
17179 20 20 20 20 20 20 70 4c 65 61 66 50 61 67 65 20 pLeafPage
1717a 3d 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67 65 = leafCur.apPage
1717b 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 5d 3b [leafCur.iPage];
1717c 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1717d 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67 6e 6f pLeafPage->pgno
1717e 3d 3d 6c 65 61 66 50 67 6e 6f 20 29 3b 0a 20 20 ==leafPgno );.
1717f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 assert( le
17180 61 66 43 75 72 2e 61 69 49 64 78 5b 6c 65 61 66 afCur.aiIdx[leaf
17181 43 75 72 2e 69 50 61 67 65 5d 3d 3d 30 20 29 3b Cur.iPage]==0 );
17182 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
17183 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 if( SQLITE_OK==r
17184 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 c. && SQLI
17185 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c TE_OK==(rc = sql
17186 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
17187 4c 65 61 66 50 61 67 65 2d 3e 70 44 62 50 61 67 LeafPage->pDbPag
17188 65 29 29 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 e)) . ){.
17189 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 dropCell(p
1718a 4c 65 61 66 50 61 67 65 2c 20 30 2c 20 73 7a 4e LeafPage, 0, szN
1718b 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 56 56 ext);. VV
1718c 41 5f 4f 4e 4c 59 28 20 6c 65 61 66 43 75 72 2e A_ONLY( leafCur.
1718d 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 3d 20 pagesShuffled =
1718e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 0 );. rc
1718f 3d 20 62 61 6c 61 6e 63 65 28 26 6c 65 61 66 43 = balance(&leafC
17190 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 ur, 0);.
17191 61 73 73 65 72 74 28 20 6c 65 61 66 43 75 72 73 assert( leafCurs
17192 6f 72 49 6e 76 61 6c 69 64 20 7c 7c 20 21 6c 65 orInvalid || !le
17193 61 66 43 75 72 2e 70 61 67 65 73 53 68 75 66 66 afCur.pagesShuff
17194 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 led.
17195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17196 20 20 20 20 20 20 20 7c 7c 20 21 70 43 75 72 2d || !pCur-
17197 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 29 >pagesShuffled )
17198 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
17199 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1719a 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72 73 6f ReleaseTempCurso
1719b 72 28 26 6c 65 61 66 43 75 72 29 3b 0a 20 20 7d r(&leafCur);. }
1719c 65 6c 73 65 7b 0a 20 20 20 20 54 52 41 43 45 28 else{. TRACE(
1719d 28 22 44 45 4c 45 54 45 3a 20 74 61 62 6c 65 3d ("DELETE: table=
1719e 25 64 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 6c %d delete from l
1719f 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 eaf %d\n",.
171a0 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 pCur->pgnoRoot
171a1 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b , pPage->pgno));
171a2 0a 20 20 20 20 72 63 20 3d 20 64 72 6f 70 43 65 . rc = dropCe
171a3 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20 63 ll(pPage, idx, c
171a4 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 ellSizePtr(pPage
171a5 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 20 69 , pCell));. i
171a6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
171a7 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 ){. rc = b
171a8 61 6c 61 6e 63 65 28 70 43 75 72 2c 20 30 29 3b alance(pCur, 0);
171a9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
171aa 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
171ab 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74 {. moveToRoot
171ac 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 72 65 (pCur);. }. re
171ad 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
171ae 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 42 * Create a new B
171af 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 72 69 Tree table. Wri
171b0 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 6c 65 te into *piTable
171b1 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d the page.** num
171b2 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 ber for the root
171b3 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65 77 page of the new
171b4 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 table..**.** Th
171b5 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 20 69 e type of type i
171b6 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 s determined by
171b7 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 the flags parame
171b8 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 0a 2a ter. Only the.*
171b9 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 * following valu
171ba 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 65 20 es of flags are
171bb 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 currently in use
171bc 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20 . Other values
171bd 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d 69 67 for.** flags mig
171be 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a 2a 0a ht not work:.**.
171bf 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 ** BTREE_INT
171c0 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 44 41 KEY|BTREE_LEAFDA
171c1 54 41 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 TA Used for
171c2 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 68 20 SQL tables with
171c3 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 20 20 rowid keys.**
171c4 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 BTREE_ZERODATA
171c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
171c6 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c 20 69 Used for SQL i
171c7 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 69 63 ndices.*/.static
171c8 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 74 65 int btreeCreate
171c9 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 Table(Btree *p,
171ca 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 69 6e int *piTable, in
171cb 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 53 68 t flags){. BtSh
171cc 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
171cd 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 Bt;. MemPage *p
171ce 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e Root;. Pgno pgn
171cf 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 3b oRoot;. int rc;
171d0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
171d1 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
171d2 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 ex(p) );. asser
171d3 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 t( pBt->inTransa
171d4 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
171d5 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 TE );. assert(
171d6 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 !pBt->readOnly )
171d7 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
171d8 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
171d9 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 . rc = allocate
171da 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 BtreePage(pBt, &
171db 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 pRoot, &pgnoRoot
171dc 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 , 1, 0);. if( r
171dd 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 c ){. return
171de 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 rc;. }.#else.
171df 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
171e0 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 uum ){. Pgno
171e1 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 20 2f pgnoMove; /
171e2 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 68 65 * Move a page he
171e3 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 re to make room
171e4 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 for the root-pag
171e5 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 e */. MemPage
171e6 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f 2a 20 *pPageMove; /*
171e7 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 The page to move
171e8 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 to. */.. /*
171e9 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 74 Creating a new t
171ea 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 62 6c able may probabl
171eb 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 y require moving
171ec 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 61 74 an existing dat
171ed 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 6f 20 abase. ** to
171ee 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 make room for th
171ef 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 6f 6f e new tables roo
171f0 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 65 20 t page. In case
171f1 74 68 69 73 20 70 61 67 65 20 74 75 72 6e 73 0a this page turns.
171f2 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 ** out to be
171f3 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
171f4 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f 76 e, delete all ov
171f5 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70 20 erflow page-map
171f6 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 68 65 caches. ** he
171f7 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 73 6f ld by open curso
171f8 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 rs.. */. i
171f9 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 nvalidateAllOver
171fa 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a flowCache(pBt);.
171fb 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 . /* Read the
171fc 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b 33 value of meta[3
171fd 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 ] from the datab
171fe 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 ase to determine
171ff 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 20 2a where the. *
17200 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 * root page of t
17201 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 68 6f he new table sho
17202 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 5d 20 uld go. meta[3]
17203 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 is the largest r
17204 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a 20 oot-page. **
17205 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 2c 20 created so far,
17206 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 2d so the new root-
17207 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b 33 5d page is (meta[3]
17208 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 +1).. */.
17209 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1720a 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c 20 26 eGetMeta(p, 4, &
1720b 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 pgnoRoot);. i
1720c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1720d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1720e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 rc;. }. p
1720f 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 20 20 gnoRoot++;..
17210 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f 74 2d /* The new root-
17211 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 page may not be
17212 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 70 allocated on a p
17213 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c ointer-map page,
17214 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 50 or the. ** P
17215 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 61 67 65 ENDING_BYTE page
17216 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 .. */. whi
17217 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 54 le( pgnoRoot==PT
17218 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c RMAP_PAGENO(pBt,
17219 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a 20 20 pgnoRoot) ||.
1721a 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d 3d pgnoRoot==
1721b 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
1721c 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 E(pBt) ){.
1721d 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 20 20 pgnoRoot++;.
1721e 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67 }. assert( pg
1721f 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a 20 20 noRoot>=3 );..
17220 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 /* Allocate a
17221 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 20 74 page. The page t
17222 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 72 65 hat currently re
17223 73 69 64 65 73 20 61 74 20 70 67 6e 6f 52 6f 6f sides at pgnoRoo
17224 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 t will. ** be
17225 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 61 6c moved to the al
17226 6c 6f 63 61 74 65 64 20 70 61 67 65 20 28 75 6e located page (un
17227 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f 63 61 74 less the allocat
17228 65 64 20 70 61 67 65 20 68 61 70 70 65 6e 73 0a ed page happens.
17229 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 69 64 65 ** to reside
1722a 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e 0a 20 at pgnoRoot)..
1722b 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 61 */. rc = a
1722c 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 llocateBtreePage
1722d 28 70 42 74 2c 20 26 70 50 61 67 65 4d 6f 76 65 (pBt, &pPageMove
1722e 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 67 6e , &pgnoMove, pgn
1722f 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 20 69 oRoot, 1);. i
17230 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
17231 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
17232 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 rc;. }..
17233 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d 70 67 if( pgnoMove!=pg
17234 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 noRoot ){.
17235 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74 /* pgnoRoot is t
17236 68 65 20 70 61 67 65 20 74 68 61 74 20 77 69 6c he page that wil
17237 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 l be used for th
17238 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0a 20 e root-page of.
17239 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 ** the new
1723a 74 61 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 20 table (assuming
1723b 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6e 6f 74 an error did not
1723c 20 6f 63 63 75 72 29 2e 20 42 75 74 20 77 65 20 occur). But we
1723d 77 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 6c were. ** al
1723e 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76 65 located pgnoMove
1723f 2e 20 49 66 20 72 65 71 75 69 72 65 64 20 28 69 . If required (i
17240 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20 6e 6f .e. if it was no
17241 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 t allocated.
17242 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 69 6e ** by extendin
17243 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74 68 65 g the file), the
17244 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 61 74 current page at
17245 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d 6f position pgnoMo
17246 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61 ve. ** is a
17247 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c 65 64 lready journaled
17248 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
17249 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20 u8 eType;.
1724a 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b 0a Pgno iPtrPage;.
1724b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
1724c 67 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0a 0a ge(pPageMove);..
1724d 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 /* Move th
1724e 65 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c 79 e page currently
1724f 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f 20 at pgnoRoot to
17250 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20 20 pgnoMove. */.
17251 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
17252 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
17253 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f pgnoRoot, &pRoo
17254 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 t, 0);. if(
17255 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
17256 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
17257 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
17258 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 rc = ptrmapGe
17259 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c t(pBt, pgnoRoot,
1725a 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 &eType, &iPtrPa
1725b 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
1725c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
1725d 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f eType==PTRMAP_RO
1725e 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 70 65 3d OTPAGE || eType=
1725f 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 =PTRMAP_FREEPAGE
17260 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 ){. rele
17261 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a asePage(pRoot);.
17262 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
17263 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 c;. }.
17264 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d assert( eType!=
17265 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 PTRMAP_ROOTPAGE
17266 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
17267 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 46 eType!=PTRMAP_F
17268 52 45 45 50 41 47 45 20 29 3b 0a 20 20 20 20 20 REEPAGE );.
17269 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1726a 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 erWrite(pRoot->p
1726b 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 DbPage);. i
1726c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1726d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 ){. rele
1726e 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a asePage(pRoot);.
1726f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
17270 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 c;. }.
17271 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 rc = relocatePa
17272 67 65 28 70 42 74 2c 20 70 52 6f 6f 74 2c 20 65 ge(pBt, pRoot, e
17273 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 Type, iPtrPage,
17274 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 pgnoMove, 0);.
17275 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
17276 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 20 20 2f pRoot);.. /
17277 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61 67 * Obtain the pag
17278 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 2a 2f e at pgnoRoot */
17279 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1727a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1727b 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1727c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 }. rc
1727d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
1727e 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 52 tPage(pBt, pgnoR
1727f 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b oot, &pRoot, 0);
17280 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
17281 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17282 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
17283 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 }. rc
17284 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
17285 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 ite(pRoot->pDbPa
17286 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
17287 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
17288 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
17289 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 age(pRoot);.
1728a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1728b 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
1728c 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d 20 {. pRoot =
1728d 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20 7d pPageMove;. }
1728e 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 .. /* Update
1728f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
17290 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20 77 and meta-data w
17291 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 ith the new root
17292 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a 2f -page number. */
17293 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 . rc = ptrmap
17294 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f Put(pBt, pgnoRoo
17295 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 t, PTRMAP_ROOTPA
17296 47 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 GE, 0);. if(
17297 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 rc ){. rele
17298 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a asePage(pRoot);.
17299 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1729a 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
1729b 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 sqlite3BtreeUpda
1729c 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67 6e teMeta(p, 4, pgn
1729d 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28 20 oRoot);. if(
1729e 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 rc ){. rele
1729f 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a asePage(pRoot);.
172a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
172a1 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 7b . }.. }else{
172a2 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 . rc = alloca
172a3 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
172a4 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f &pRoot, &pgnoRo
172a5 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 ot, 1, 0);. i
172a6 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
172a7 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 c;. }.#endif.
172a8 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
172a9 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
172aa 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20 pRoot->pDbPage)
172ab 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 52 );. zeroPage(pR
172ac 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54 46 oot, flags | PTF
172ad 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74 65 _LEAF);. sqlite
172ae 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f 6f 3PagerUnref(pRoo
172af 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 2a t->pDbPage);. *
172b0 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29 70 piTable = (int)p
172b1 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75 72 gnoRoot;. retur
172b2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 n SQLITE_OK;.}.S
172b3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
172b4 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 t sqlite3BtreeCr
172b5 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 eateTable(Btree
172b6 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 *p, int *piTable
172b7 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
172b8 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 int rc;. sqlite
172b9 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
172ba 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 p->pBt->db = p
172bb 2d 3e 64 62 3b 0a 20 20 72 63 20 3d 20 62 74 72 ->db;. rc = btr
172bc 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 2c eeCreateTable(p,
172bd 20 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 29 piTable, flags)
172be 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
172bf 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
172c0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
172c1 45 72 61 73 65 20 74 68 65 20 67 69 76 65 6e 20 Erase the given
172c2 64 61 74 61 62 61 73 65 20 70 61 67 65 20 61 6e database page an
172c3 64 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 72 d all its childr
172c4 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 en. Return.** t
172c5 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 66 he page to the f
172c6 72 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 reelist..*/.stat
172c7 69 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74 61 ic int clearData
172c8 62 61 73 65 50 61 67 65 28 0a 20 20 42 74 53 68 basePage(. BtSh
172c9 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 ared *pBt,
172ca 20 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 /* The BTre
172cb 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
172cc 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 the table */. P
172cd 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 gno pgno,
172ce 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d /* Page num
172cf 62 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a ber to clear */.
172d0 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c int freePageFl
172d1 61 67 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c ag, /* Deall
172d2 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74 72 ocate page if tr
172d3 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 ue */. int *pnC
172d4 68 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50 61 hange.){. MemPa
172d5 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 ge *pPage = 0;.
172d6 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67 int rc;. unsig
172d7 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b ned char *pCell;
172d8 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 . int i;.. ass
172d9 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
172da 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
172db 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 67 6e ex) );. if( pgn
172dc 6f 3e 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 o>pagerPagecount
172dd 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 (pBt) ){. ret
172de 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
172df 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 PT_BKPT;. }..
172e0 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 rc = getAndInitP
172e1 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 26 age(pBt, pgno, &
172e2 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 pPage);. if( rc
172e3 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 ) goto cleardat
172e4 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 abasepage_out;.
172e5 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 for(i=0; i<pPag
172e6 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a e->nCell; i++){.
172e7 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 pCell = find
172e8 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a Cell(pPage, i);.
172e9 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e if( !pPage->
172ea 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 leaf ){. rc
172eb 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 = clearDatabase
172ec 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 79 Page(pBt, get4by
172ed 74 65 28 70 43 65 6c 6c 29 2c 20 31 2c 20 70 6e te(pCell), 1, pn
172ee 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 69 Change);. i
172ef 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 f( rc ) goto cle
172f0 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f ardatabasepage_o
172f1 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 ut;. }. rc
172f2 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 = clearCell(pPa
172f3 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 ge, pCell);.
172f4 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c if( rc ) goto cl
172f5 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f eardatabasepage_
172f6 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 out;. }. if( !
172f7 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
172f8 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61 74 rc = clearDat
172f9 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20 67 abasePage(pBt, g
172fa 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
172fb 61 44 61 74 61 5b 38 5d 29 2c 20 31 2c 20 70 6e aData[8]), 1, pn
172fc 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 69 66 28 Change);. if(
172fd 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 rc ) goto clear
172fe 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 databasepage_out
172ff 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 6e ;. }else if( pn
17300 43 68 61 6e 67 65 20 29 7b 0a 20 20 20 20 61 73 Change ){. as
17301 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 sert( pPage->int
17302 4b 65 79 20 29 3b 0a 20 20 20 20 2a 70 6e 43 68 Key );. *pnCh
17303 61 6e 67 65 20 2b 3d 20 70 50 61 67 65 2d 3e 6e ange += pPage->n
17304 43 65 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Cell;. }. if(
17305 66 72 65 65 50 61 67 65 46 6c 61 67 20 29 7b 0a freePageFlag ){.
17306 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 rc = freePag
17307 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 e(pPage);. }els
17308 65 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 e if( (rc = sqli
17309 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
1730a 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 3d 3d age->pDbPage))==
1730b 30 20 29 7b 0a 20 20 20 20 7a 65 72 6f 50 61 67 0 ){. zeroPag
1730c 65 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e e(pPage, pPage->
1730d 61 44 61 74 61 5b 30 5d 20 7c 20 50 54 46 5f 4c aData[0] | PTF_L
1730e 45 41 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65 61 72 EAF);. }..clear
1730f 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 databasepage_out
17310 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 :. releasePage(
17311 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e pPage);. return
17312 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 rc;.}../*.** De
17313 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 lete all informa
17314 74 69 6f 6e 20 66 72 6f 6d 20 61 20 73 69 6e 67 tion from a sing
17315 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 le table in the
17316 64 61 74 61 62 61 73 65 2e 20 20 69 54 61 62 6c database. iTabl
17317 65 20 69 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 e is.** the page
17318 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 number of the r
17319 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 oot of the table
1731a 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72 6f . After this ro
1731b 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2c 0a 2a utine returns,.*
1731c 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 * the root page
1731d 69 73 20 65 6d 70 74 79 2c 20 62 75 74 20 73 74 is empty, but st
1731e 69 6c 6c 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a ill exists..**.*
1731f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
17320 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53 51 ill fail with SQ
17321 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 LITE_LOCKED if t
17322 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70 65 here are any ope
17323 6e 0a 2a 2a 20 72 65 61 64 20 63 75 72 73 6f 72 n.** read cursor
17324 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 s on the table.
17325 20 4f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 Open write curs
17326 6f 72 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f ors are moved to
17327 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f 66 20 the.** root of
17328 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a the table..**.**
17329 20 49 66 20 70 6e 43 68 61 6e 67 65 20 69 73 20 If pnChange is
1732a 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 not NULL, then t
1732b 61 62 6c 65 20 69 54 61 62 6c 65 20 6d 75 73 74 able iTable must
1732c 20 62 65 20 61 6e 20 69 6e 74 6b 65 79 20 74 61 be an intkey ta
1732d 62 6c 65 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65 ble. The.** inte
1732e 67 65 72 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 ger value pointe
1732f 64 20 74 6f 20 62 79 20 70 6e 43 68 61 6e 67 65 d to by pnChange
17330 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
17331 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 by the number of
17332 0a 2a 2a 20 65 6e 74 72 69 65 73 20 69 6e 20 74 .** entries in t
17333 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c he table..*/.SQL
17334 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
17335 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 sqlite3BtreeClea
17336 72 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c rTable(Btree *p,
17337 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 int iTable, int
17338 20 2a 70 6e 43 68 61 6e 67 65 29 7b 0a 20 20 69 *pnChange){. i
17339 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 nt rc;. BtShare
1733a 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
1733b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1733c 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e nter(p);. pBt->
1733d 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 73 db = p->db;. as
1733e 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 sert( p->inTrans
1733f 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
17340 0a 20 20 69 66 28 20 28 72 63 20 3d 20 63 68 65 . if( (rc = che
17341 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 2c 20 69 ckReadLocks(p, i
17342 54 61 62 6c 65 2c 20 30 2c 20 31 29 29 21 3d 53 Table, 0, 1))!=S
17343 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17344 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f /* nothing to do
17345 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 */. }else if(
17346 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
17347 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 saveAllCursors(
17348 70 42 74 2c 20 69 54 61 62 6c 65 2c 20 30 29 29 pBt, iTable, 0))
17349 20 29 7b 0a 20 20 20 20 2f 2a 20 6e 6f 74 68 69 ){. /* nothi
1734a 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65 ng to do */. }e
1734b 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c lse{. rc = cl
1734c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 earDatabasePage(
1734d 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c pBt, (Pgno)iTabl
1734e 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65 29 3b e, 0, pnChange);
1734f 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
17350 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
17351 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
17352 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69 6e 66 ** Erase all inf
17353 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 74 61 ormation in a ta
17354 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68 65 20 ble and add the
17355 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c root of the tabl
17356 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72 65 65 e to.** the free
17357 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c 20 74 list. Except, t
17358 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 70 he root of the p
17359 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65 20 28 rinciple table (
1735a 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61 the one on.** pa
1735b 67 65 20 31 29 20 69 73 20 6e 65 76 65 72 20 61 ge 1) is never a
1735c 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 dded to the free
1735d 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 list..**.** This
1735e 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 routine will fa
1735f 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c il with SQLITE_L
17360 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 OCKED if there a
17361 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63 re any open.** c
17362 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 74 61 ursors on the ta
17363 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 ble..**.** If AU
17364 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 TOVACUUM is enab
17365 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 led and the page
17366 20 61 74 20 69 54 61 62 6c 65 20 69 73 20 6e 6f at iTable is no
17367 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 72 6f t the last.** ro
17368 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 ot page in the d
17369 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
1736a 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 en the last root
1736b 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74 68 65 page .** in the
1736c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
1736d 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 s moved into the
1736e 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f slot formerly o
1736f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20 69 54 ccupied by.** iT
17370 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20 6c 61 able and that la
17371 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 st slot formerly
17372 20 6f 63 63 75 70 69 65 64 20 62 79 20 74 68 65 occupied by the
17373 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 0a last root page.
17374 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 ** is added to t
17375 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e 73 74 he freelist inst
17376 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e 20 20 ead of iTable.
17377 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61 6c 6c In this say, all
17378 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 61 .** root pages a
17379 72 65 20 6b 65 70 74 20 61 74 20 74 68 65 20 62 re kept at the b
1737a 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
1737b 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 database file, w
1737c 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63 65 73 hich.** is neces
1737d 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56 41 43 sary for AUTOVAC
1737e 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69 67 68 UUM to work righ
1737f 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 t. *piMoved is
17380 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 set to the .** p
17381 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61 74 20 age number that
17382 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 6c used to be the l
17383 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 69 6e ast root page in
17384 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f 72 65 the file before
17385 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20 20 49 .** the move. I
17386 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73 20 6d f no page gets m
17387 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64 20 69 oved, *piMoved i
17388 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 54 s set to 0..** T
17389 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 he last root pag
1738a 65 20 69 73 20 72 65 63 6f 72 64 65 64 20 69 6e e is recorded in
1738b 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74 68 65 meta[3] and the
1738c 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d 65 74 value of.** met
1738d 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65 64 20 a[3] is updated
1738e 62 79 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 by this procedur
1738f 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
17390 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 btreeDropTable(
17391 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 Btree *p, Pgno i
17392 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f Table, int *piMo
17393 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ved){. int rc;.
17394 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
17395 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 = 0;. BtShared
17396 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
17397 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
17398 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
17399 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 x(p) );. assert
1739a 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
1739b 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a 20 20 ANS_WRITE );..
1739c 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c /* It is illegal
1739d 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 6c 65 to drop a table
1739e 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 73 20 if any cursors
1739f 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 0a are open on the.
173a0 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 ** database. T
173a1 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 69 his is because i
173a2 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f n auto-vacuum mo
173a3 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64 20 6d de the backend m
173a4 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 ay. ** need to
173a5 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72 6f 6f move another roo
173a6 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c 20 61 t-page to fill a
173a7 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 gap left by the
173a8 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 72 6f deleted. ** ro
173a9 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e 20 6f ot page. If an o
173aa 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73 20 75 pen cursor was u
173ab 73 69 6e 67 20 74 68 69 73 20 70 61 67 65 20 61 sing this page a
173ac 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 20 0a problem would .
173ad 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 2a 2f ** occur.. */
173ae 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 43 75 72 . if( pBt->pCur
173af 73 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 sor ){. retur
173b0 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b n SQLITE_LOCKED;
173b1 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c . }.. rc = sql
173b2 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
173b3 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 (pBt, (Pgno)iTab
173b4 6c 65 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a le, &pPage, 0);.
173b5 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
173b6 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c n rc;. rc = sql
173b7 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 ite3BtreeClearTa
173b8 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 ble(p, iTable, 0
173b9 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 );. if( rc ){.
173ba 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
173bb 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Page);. retur
173bc 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 n rc;. }.. *pi
173bd 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 69 66 Moved = 0;.. if
173be 28 20 69 54 61 62 6c 65 3e 31 20 29 7b 0a 23 69 ( iTable>1 ){.#i
173bf 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
173c0 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
173c1 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 50 rc = freePage(pP
173c2 61 67 65 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 age);. releas
173c3 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 23 65 ePage(pPage);.#e
173c4 6c 73 65 0a 20 20 20 20 69 66 28 20 70 42 74 2d lse. if( pBt-
173c5 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 >autoVacuum ){.
173c6 20 20 20 20 20 50 67 6e 6f 20 6d 61 78 52 6f 6f Pgno maxRoo
173c7 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 tPgno;. rc
173c8 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
173c9 74 4d 65 74 61 28 70 2c 20 34 2c 20 26 6d 61 78 tMeta(p, 4, &max
173ca 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 20 20 RootPgno);.
173cb 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
173cc 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
173cd 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
173ce 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
173cf 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 rc;. }..
173d0 20 20 20 20 69 66 28 20 69 54 61 62 6c 65 3d 3d if( iTable==
173d1 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29 7b 0a 20 maxRootPgno ){.
173d2 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 /* If the
173d3 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f table being dro
173d4 70 70 65 64 20 69 73 20 74 68 65 20 74 61 62 6c pped is the tabl
173d5 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 65 e with the large
173d6 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 st root-page.
173d7 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 ** number i
173d8 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 n the database,
173d9 70 75 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 put the root pag
173da 65 20 6f 6e 20 74 68 65 20 66 72 65 65 20 6c 69 e on the free li
173db 73 74 2e 20 0a 20 20 20 20 20 20 20 20 2a 2f 0a st. . */.
173dc 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 72 65 rc = fre
173dd 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
173de 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
173df 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
173e0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
173e1 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
173e2 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
173e3 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
173e4 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 e{. /* Th
173e5 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 e table being dr
173e6 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f 74 20 68 opped does not h
173e7 61 76 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 ave the largest
173e8 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 root-page.
173e9 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 ** number in t
173ea 68 65 20 64 61 74 61 62 61 73 65 2e 20 53 6f 20 he database. So
173eb 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 74 68 move the page th
173ec 61 74 20 64 6f 65 73 20 69 6e 74 6f 20 74 68 65 at does into the
173ed 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 67 61 70 . ** gap
173ee 20 6c 65 66 74 20 62 79 20 74 68 65 20 64 65 6c left by the del
173ef 65 74 65 64 20 72 6f 6f 74 2d 70 61 67 65 2e 0a eted root-page..
173f0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
173f1 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4d 6f 76 MemPage *pMov
173f2 65 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 e;. relea
173f3 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
173f4 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
173f5 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 te3BtreeGetPage(
173f6 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f pBt, maxRootPgno
173f7 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 , &pMove, 0);.
173f8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
173f9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
173fa 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
173fb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
173fc 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 rc = relocateP
173fd 61 67 65 28 70 42 74 2c 20 70 4d 6f 76 65 2c 20 age(pBt, pMove,
173fe 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c PTRMAP_ROOTPAGE,
173ff 20 30 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a 0, iTable, 0);.
17400 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
17401 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 age(pMove);.
17402 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
17403 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
17404 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
17405 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
17406 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
17407 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6d 61 eGetPage(pBt, ma
17408 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 xRootPgno, &pMov
17409 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 e, 0);. i
1740a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1740b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
1740c 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
1740d 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
1740e 66 72 65 65 50 61 67 65 28 70 4d 6f 76 65 29 3b freePage(pMove);
1740f 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
17410 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 Page(pMove);.
17411 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
17412 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
17413 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
17414 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
17415 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78 52 *piMoved = maxR
17416 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 7d ootPgno;. }
17417 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 .. /* Set t
17418 68 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f 74 he new 'max-root
17419 2d 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e 20 -page' value in
1741a 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61 the database hea
1741b 64 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20 20 der. This.
1741c 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76 61 ** is the old va
1741d 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 lue less one, le
1741e 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20 74 ss one more if t
1741f 68 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a 20 hat happens to.
17420 20 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f 6f ** be a roo
17421 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20 6c t-page number, l
17422 65 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69 66 ess one again if
17423 20 74 68 61 74 20 69 73 20 74 68 65 0a 20 20 20 that is the.
17424 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 ** PENDING_BY
17425 54 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20 2a TE_PAGE.. *
17426 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 /. maxRootP
17427 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 gno--;. if(
17428 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45 maxRootPgno==PE
17429 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
1742a 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 pBt) ){.
1742b 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 maxRootPgno--;.
1742c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1742d 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 54 maxRootPgno==PT
1742e 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c RMAP_PAGENO(pBt,
1742f 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 20 29 7b maxRootPgno) ){
17430 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 . maxRoot
17431 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a Pgno--;. }.
17432 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 61 assert( ma
17433 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45 4e 44 49 xRootPgno!=PENDI
17434 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
17435 29 20 29 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d ) );.. rc =
17436 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
17437 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 6d 61 ateMeta(p, 4, ma
17438 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 20 xRootPgno);.
17439 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
1743a 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67 65 = freePage(pPage
1743b 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 );. release
1743c 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
1743d 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 }.#endif. }els
1743e 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73 71 6c e{. /* If sql
1743f 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 ite3BtreeDropTab
17440 6c 65 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e le was called on
17441 20 70 61 67 65 20 31 2e 20 2a 2f 0a 20 20 20 20 page 1. */.
17442 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20 zeroPage(pPage,
17443 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c PTF_INTKEY|PTF_L
17444 45 41 46 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 EAF );. relea
17445 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
17446 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 20 }. return rc;
17447 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .}.SQLITE_PRIVA
17448 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
17449 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 reeDropTable(Btr
1744a 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c ee *p, int iTabl
1744b 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64 29 e, int *piMoved)
1744c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 {. int rc;. sq
1744d 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
1744e 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 p);. p->pBt->db
1744f 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 63 20 3d = p->db;. rc =
17450 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 btreeDropTable(
17451 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d 6f 76 p, iTable, piMov
17452 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ed);. sqlite3Bt
17453 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
17454 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
17455 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 6d 65 74 .** Read the met
17456 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 a-information ou
17457 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 t of a database
17458 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a file. Meta[0].*
17459 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 * is the number
1745a 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 63 75 of free pages cu
1745b 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 64 rrently in the d
1745c 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 31 atabase. Meta[1
1745d 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 6d 65 74 ].** through met
1745e 61 5b 31 35 5d 20 61 72 65 20 61 76 61 69 6c 61 a[15] are availa
1745f 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79 20 68 ble for use by h
17460 69 67 68 65 72 20 6c 61 79 65 72 73 2e 20 20 4d igher layers. M
17461 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 72 65 61 eta[0].** is rea
17462 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f 74 68 65 d-only, the othe
17463 72 73 20 61 72 65 20 72 65 61 64 2f 77 72 69 74 rs are read/writ
17464 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 73 63 e..** .** The sc
17465 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 6d 62 65 hema layer numbe
17466 72 73 20 6d 65 74 61 20 76 61 6c 75 65 73 20 64 rs meta values d
17467 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41 74 20 ifferently. At
17468 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20 6c 61 the schema.** la
17469 79 65 72 20 28 61 6e 64 20 74 68 65 20 53 65 74 yer (and the Set
1746a 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 61 64 43 Cookie and ReadC
1746b 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 29 20 74 ookie opcodes) t
1746c 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
1746d 66 72 65 65 20 70 61 67 65 73 20 69 73 20 6e 6f free pages is no
1746e 74 20 76 69 73 69 62 6c 65 2e 20 20 53 6f 20 43 t visible. So C
1746f 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 68 65 20 ookie[0] is the
17470 73 61 6d 65 20 61 73 20 4d 65 74 61 5b 31 5d 2e same as Meta[1].
17471 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
17472 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
17473 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 reeGetMeta(Btree
17474 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 *p, int idx, u3
17475 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20 44 62 50 2 *pMeta){. DbP
17476 61 67 65 20 2a 70 44 62 50 61 67 65 20 3d 20 30 age *pDbPage = 0
17477 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e ;. int rc;. un
17478 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 31 signed char *pP1
17479 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
1747a 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 t = p->pBt;.. s
1747b 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1747c 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d (p);. pBt->db =
1747d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 52 65 p->db;.. /* Re
1747e 61 64 69 6e 67 20 61 20 6d 65 74 61 2d 64 61 74 ading a meta-dat
1747f 61 20 76 61 6c 75 65 20 72 65 71 75 69 72 65 73 a value requires
17480 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 a read-lock on
17481 70 61 67 65 20 31 20 28 61 6e 64 20 68 65 6e 63 page 1 (and henc
17482 65 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 e. ** the sqlit
17483 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 e_master table.
17484 57 65 20 67 72 61 62 20 74 68 69 73 20 6c 6f 63 We grab this loc
17485 6b 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 k regardless of
17486 77 68 65 74 68 65 72 20 6f 72 0a 20 20 2a 2a 20 whether or. **
17487 6e 6f 74 20 74 68 65 20 53 51 4c 49 54 45 5f 52 not the SQLITE_R
17488 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 eadUncommitted f
17489 6c 61 67 20 69 73 20 73 65 74 20 28 74 68 65 20 lag is set (the
1748a 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 61 74 20 table rooted at
1748b 70 61 67 65 0a 20 20 2a 2a 20 31 20 69 73 20 74 page. ** 1 is t
1748c 72 65 61 74 65 64 20 61 73 20 61 20 73 70 65 63 reated as a spec
1748d 69 61 6c 20 63 61 73 65 20 62 79 20 71 75 65 72 ial case by quer
1748e 79 54 61 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64 yTableLock() and
1748f 20 6c 6f 63 6b 54 61 62 6c 65 28 29 29 2e 0a 20 lockTable())..
17490 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79 */. rc = query
17491 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 31 2c 20 TableLock(p, 1,
17492 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 READ_LOCK);. if
17493 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
17494 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
17495 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 reeLeave(p);.
17496 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
17497 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d . assert( idx>=
17498 30 20 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 0 && idx<=15 );.
17499 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 if( pBt->pPage
1749a 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 1 ){. /* The
1749b 62 2d 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 b-tree is alread
1749c 79 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 66 65 y holding a refe
1749d 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 rence to page 1
1749e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a of the database.
1749f 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 49 6e 20 ** file. In
174a0 74 68 69 73 20 63 61 73 65 20 74 68 65 20 72 65 this case the re
174a1 71 75 69 72 65 64 20 6d 65 74 61 2d 64 61 74 61 quired meta-data
174a2 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 72 65 value can be re
174a3 61 64 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20 ad directly.
174a4 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 ** from the page
174a5 20 64 61 74 61 20 6f 66 20 74 68 69 73 20 72 65 data of this re
174a6 66 65 72 65 6e 63 65 2e 20 54 68 69 73 20 69 73 ference. This is
174a7 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 65 72 slightly faster
174a8 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 72 65 71 than. ** req
174a9 75 65 73 74 69 6e 67 20 61 20 6e 65 77 20 72 65 uesting a new re
174aa 66 65 72 65 6e 63 65 20 66 72 6f 6d 20 74 68 65 ference from the
174ab 20 70 61 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 pager layer..
174ac 20 20 2a 2f 0a 20 20 20 20 70 50 31 20 3d 20 28 */. pP1 = (
174ad 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
174ae 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
174af 74 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ta;. }else{.
174b0 20 2f 2a 20 54 68 65 20 62 2d 74 72 65 65 20 64 /* The b-tree d
174b1 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 20 72 oes not have a r
174b2 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 eference to page
174b3 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 1 of the databa
174b4 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 20 se file.. **
174b5 4f 62 74 61 69 6e 20 6f 6e 65 20 66 72 6f 6d 20 Obtain one from
174b6 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 2e the pager layer.
174b7 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
174b8 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
174b9 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 31 2c (pBt->pPager, 1,
174ba 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 &pDbPage);.
174bb 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
174bc 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
174bd 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74 75 e(p);. retu
174be 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
174bf 20 70 50 31 20 3d 20 28 75 6e 73 69 67 6e 65 64 pP1 = (unsigned
174c0 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 char *)sqlite3P
174c1 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 agerGetData(pDbP
174c2 61 67 65 29 3b 0a 20 20 7d 0a 20 20 2a 70 4d 65 age);. }. *pMe
174c3 74 61 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 ta = get4byte(&p
174c4 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b P1[36 + idx*4]);
174c5 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 62 2d .. /* If the b-
174c6 74 72 65 65 20 69 73 20 6e 6f 74 20 68 6f 6c 64 tree is not hold
174c7 69 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 20 ing a reference
174c8 74 6f 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 to page 1, then
174c9 6f 6e 65 20 77 61 73 20 0a 20 20 2a 2a 20 72 65 one was . ** re
174ca 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 quested from the
174cb 20 70 61 67 65 72 20 6c 61 79 65 72 20 69 6e 20 pager layer in
174cc 74 68 65 20 61 62 6f 76 65 20 62 6c 6f 63 6b 2e the above block.
174cd 20 52 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e Release it now.
174ce 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 42 74 . */. if( !pBt
174cf 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20 20 ->pPage1 ){.
174d0 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
174d1 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a f(pDbPage);. }.
174d2 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f 76 61 63 . /* If autovac
174d3 75 75 6d 65 64 20 69 73 20 64 69 73 61 62 6c 65 uumed is disable
174d4 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 20 d in this build
174d5 62 75 74 20 77 65 20 61 72 65 20 74 72 79 69 6e but we are tryin
174d6 67 20 74 6f 20 0a 20 20 2a 2a 20 61 63 63 65 73 g to . ** acces
174d7 73 20 61 6e 20 61 75 74 6f 76 61 63 75 75 6d 65 s an autovacuume
174d8 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e d database, then
174d9 20 6d 61 6b 65 20 74 68 65 20 64 61 74 61 62 61 make the databa
174da 73 65 20 72 65 61 64 6f 6e 6c 79 2e 20 0a 20 20 se readonly. .
174db 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
174dc 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
174dd 0a 20 20 69 66 28 20 69 64 78 3d 3d 34 20 26 26 . if( idx==4 &&
174de 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 42 74 2d *pMeta>0 ) pBt-
174df 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 >readOnly = 1;.#
174e0 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 72 61 62 endif.. /* Grab
174e1 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 6f the read-lock o
174e2 6e 20 70 61 67 65 20 31 2e 20 2a 2f 0a 20 20 72 n page 1. */. r
174e3 63 20 3d 20 6c 6f 63 6b 54 61 62 6c 65 28 70 2c c = lockTable(p,
174e4 20 31 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 1, READ_LOCK);.
174e5 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
174e6 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
174e7 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 rc;.}../*.** Wr
174e8 69 74 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 ite meta-informa
174e9 74 69 6f 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 tion back into t
174ea 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 he database. Me
174eb 74 61 5b 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64 ta[0] is.** read
174ec 2d 6f 6e 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f -only and may no
174ed 74 20 62 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f t be written..*/
174ee 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
174ef 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
174f0 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 65 UpdateMeta(Btree
174f1 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 *p, int idx, u3
174f2 32 20 69 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 2 iMeta){. BtSh
174f3 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
174f4 42 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 Bt;. unsigned c
174f5 68 61 72 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20 har *pP1;. int
174f6 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 rc;. assert( id
174f7 78 3e 3d 31 20 26 26 20 69 64 78 3c 3d 31 35 20 x>=1 && idx<=15
174f8 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
174f9 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 eEnter(p);. pBt
174fa 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 ->db = p->db;.
174fb 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 assert( p->inTra
174fc 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
174fd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 );. assert( pBt
174fe 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 3b 0a 20 ->pPage1!=0 );.
174ff 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 pP1 = pBt->pPag
17500 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20 e1->aData;. rc
17501 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
17502 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d ite(pBt->pPage1-
17503 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 >pDbPage);. if(
17504 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
17505 7b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 {. put4byte(&
17506 70 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 5d 2c pP1[36 + idx*4],
17507 20 69 4d 65 74 61 29 3b 0a 23 69 66 6e 64 65 66 iMeta);.#ifndef
17508 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
17509 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 OVACUUM. if(
1750a 69 64 78 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20 idx==7 ){.
1750b 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 assert( pBt->aut
1750c 6f 56 61 63 75 75 6d 20 7c 7c 20 69 4d 65 74 61 oVacuum || iMeta
1750d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 ==0 );. ass
1750e 65 72 74 28 20 69 4d 65 74 61 3d 3d 30 20 7c 7c ert( iMeta==0 ||
1750f 20 69 4d 65 74 61 3d 3d 31 20 29 3b 0a 20 20 20 iMeta==1 );.
17510 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 pBt->incrVacu
17511 75 6d 20 3d 20 28 75 38 29 69 4d 65 74 61 3b 0a um = (u8)iMeta;.
17512 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
17513 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
17514 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
17515 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
17516 65 74 75 72 6e 20 74 68 65 20 66 6c 61 67 20 62 eturn the flag b
17517 79 74 65 20 61 74 20 74 68 65 20 62 65 67 69 6e yte at the begin
17518 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 ning of the page
17519 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 that the cursor
1751a 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 .** is currently
1751b 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2f pointing to..*/
1751c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1751d 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
1751e 46 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20 2a Flags(BtCursor *
1751f 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f pCur){. /* TODO
17520 3a 20 57 68 61 74 20 61 62 6f 75 74 20 43 55 52 : What about CUR
17521 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 SOR_REQUIRESEEK
17522 73 74 61 74 65 3f 20 50 72 6f 62 61 62 6c 79 20 state? Probably
17523 6e 65 65 64 20 74 6f 20 63 61 6c 6c 0a 20 20 2a need to call. *
17524 2a 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 * restoreCursorP
17525 6f 73 69 74 69 6f 6e 28 29 20 68 65 72 65 2e 0a osition() here..
17526 20 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a */. MemPage *
17527 70 50 61 67 65 3b 0a 20 20 72 65 73 74 6f 72 65 pPage;. restore
17528 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
17529 43 75 72 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 Cur);. pPage =
1752a 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
1752b 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 r->iPage];. ass
1752c 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
1752d 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
1752e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 21 3d assert( pPage!=
1752f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
17530 50 61 67 65 2d 3e 70 42 74 3d 3d 70 43 75 72 2d Page->pBt==pCur-
17531 3e 70 42 74 20 29 3b 0a 20 20 72 65 74 75 72 6e >pBt );. return
17532 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 pPage->aData[pP
17533 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 5d 3b age->hdrOffset];
17534 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 .}.../*.** Retur
17535 6e 20 74 68 65 20 70 61 67 65 72 20 61 73 73 6f n the pager asso
17536 63 69 61 74 65 64 20 77 69 74 68 20 61 20 42 54 ciated with a BT
17537 72 65 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ree. This routi
17538 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a ne is used for.*
17539 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 * testing and de
1753a 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f bugging only..*/
1753b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1753c 50 61 67 65 72 20 2a 73 71 6c 69 74 65 33 42 74 Pager *sqlite3Bt
1753d 72 65 65 50 61 67 65 72 28 42 74 72 65 65 20 2a reePager(Btree *
1753e 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p){. return p->
1753f 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a 0a pBt->pPager;.}..
17540 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
17541 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 MIT_INTEGRITY_CH
17542 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 ECK./*.** Append
17543 20 61 20 6d 65 73 73 61 67 65 20 74 6f 20 74 68 a message to th
17544 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
17545 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 string..*/.stati
17546 63 20 76 6f 69 64 20 63 68 65 63 6b 41 70 70 65 c void checkAppe
17547 6e 64 4d 73 67 28 0a 20 20 49 6e 74 65 67 72 69 ndMsg(. Integri
17548 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 0a 20 20 tyCk *pCheck,.
17549 63 68 61 72 20 2a 7a 4d 73 67 31 2c 0a 20 20 63 char *zMsg1,. c
1754a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
1754b 61 74 2c 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20 76 at,. ....){. v
1754c 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 a_list ap;. if(
1754d 20 21 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 !pCheck->mxErr
1754e 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43 68 65 ) return;. pChe
1754f 63 6b 2d 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20 70 ck->mxErr--;. p
17550 43 68 65 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 Check->nErr++;.
17551 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
17552 6f 72 6d 61 74 29 3b 0a 20 20 69 66 28 20 70 43 ormat);. if( pC
17553 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6e 43 68 heck->errMsg.nCh
17554 61 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ar ){. sqlite
17555 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
17556 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c &pCheck->errMsg,
17557 20 22 5c 6e 22 2c 20 31 29 3b 0a 20 20 7d 0a 20 "\n", 1);. }.
17558 20 69 66 28 20 7a 4d 73 67 31 20 29 7b 0a 20 20 if( zMsg1 ){.
17559 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
1755a 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b 2d mAppend(&pCheck-
1755b 3e 65 72 72 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 >errMsg, zMsg1,
1755c 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 -1);. }. sqlit
1755d 65 33 56 58 50 72 69 6e 74 66 28 26 70 43 68 65 e3VXPrintf(&pChe
1755e 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 31 2c 20 7a ck->errMsg, 1, z
1755f 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 Format, ap);. v
17560 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 a_end(ap);. if(
17561 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e pCheck->errMsg.
17562 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
17563 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c pCheck->mall
17564 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
17565 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }.}.#endif /* SQ
17566 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 LITE_OMIT_INTEGR
17567 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 ITY_CHECK */..#i
17568 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
17569 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
1756a 4b 0a 2f 2a 0a 2a 2a 20 41 64 64 20 31 20 74 6f K./*.** Add 1 to
1756b 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 the reference c
1756c 6f 75 6e 74 20 66 6f 72 20 70 61 67 65 20 69 50 ount for page iP
1756d 61 67 65 2e 20 20 49 66 20 74 68 69 73 20 69 73 age. If this is
1756e 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 72 the second.** r
1756f 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 eference to the
17570 70 61 67 65 2c 20 61 64 64 20 61 6e 20 65 72 72 page, add an err
17571 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 43 or message to pC
17572 68 65 63 6b 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a heck->zErrMsg..*
17573 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 * Return 1 if th
17574 65 72 65 20 61 72 65 20 32 20 6f 72 65 20 6d 6f ere are 2 ore mo
17575 72 65 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f re references to
17576 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 30 20 the page and 0
17577 69 66 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73 if.** if this is
17578 20 74 68 65 20 66 69 72 73 74 20 72 65 66 65 72 the first refer
17579 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67 65 ence to the page
1757a 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 ..**.** Also che
1757b 63 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 65 ck that the page
1757c 20 6e 75 6d 62 65 72 20 69 73 20 69 6e 20 62 6f number is in bo
1757d 75 6e 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 unds..*/.static
1757e 69 6e 74 20 63 68 65 63 6b 52 65 66 28 49 6e 74 int checkRef(Int
1757f 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b egrityCk *pCheck
17580 2c 20 50 67 6e 6f 20 69 50 61 67 65 2c 20 63 68 , Pgno iPage, ch
17581 61 72 20 2a 7a 43 6f 6e 74 65 78 74 29 7b 0a 20 ar *zContext){.
17582 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20 if( iPage==0 )
17583 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 return 1;. if(
17584 69 50 61 67 65 3e 70 43 68 65 63 6b 2d 3e 6e 50 iPage>pCheck->nP
17585 61 67 65 20 29 7b 0a 20 20 20 20 63 68 65 63 6b age ){. check
17586 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
17587 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 69 6e 76 , zContext, "inv
17588 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62 65 72 alid page number
17589 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 %d", iPage);.
1758a 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
1758b 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 61 6e if( pCheck->an
1758c 52 65 66 5b 69 50 61 67 65 5d 3d 3d 31 20 29 7b Ref[iPage]==1 ){
1758d 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 . checkAppend
1758e 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e Msg(pCheck, zCon
1758f 74 65 78 74 2c 20 22 32 6e 64 20 72 65 66 65 72 text, "2nd refer
17590 65 6e 63 65 20 74 6f 20 70 61 67 65 20 25 64 22 ence to page %d"
17591 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65 , iPage);. re
17592 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 turn 1;. }. re
17593 74 75 72 6e 20 20 28 70 43 68 65 63 6b 2d 3e 61 turn (pCheck->a
17594 6e 52 65 66 5b 69 50 61 67 65 5d 2b 2b 29 3e 31 nRef[iPage]++)>1
17595 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
17596 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
17597 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 UUM./*.** Check
17598 74 68 61 74 20 74 68 65 20 65 6e 74 72 79 20 69 that the entry i
17599 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 n the pointer-ma
1759a 70 20 66 6f 72 20 70 61 67 65 20 69 43 68 69 6c p for page iChil
1759b 64 20 6d 61 70 73 20 74 6f 20 0a 2a 2a 20 70 61 d maps to .** pa
1759c 67 65 20 69 50 61 72 65 6e 74 2c 20 70 6f 69 6e ge iParent, poin
1759d 74 65 72 20 74 79 70 65 20 70 74 72 54 79 70 65 ter type ptrType
1759e 2e 20 49 66 20 6e 6f 74 2c 20 61 70 70 65 6e 64 . If not, append
1759f 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
175a0 65 0a 2a 2a 20 74 6f 20 70 43 68 65 63 6b 2e 0a e.** to pCheck..
175a1 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
175a2 68 65 63 6b 50 74 72 6d 61 70 28 0a 20 20 49 6e heckPtrmap(. In
175a3 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 tegrityCk *pChec
175a4 6b 2c 20 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 k, /* Integrit
175a5 79 20 63 68 65 63 6b 20 63 6f 6e 74 65 78 74 20 y check context
175a6 2a 2f 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 */. Pgno iChild
175a7 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 , /* C
175a8 68 69 6c 64 20 70 61 67 65 20 6e 75 6d 62 65 72 hild page number
175a9 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20 */. u8 eType,
175aa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
175ab 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74 65 72 Expected pointer
175ac 20 6d 61 70 20 74 79 70 65 20 2a 2f 0a 20 20 50 map type */. P
175ad 67 6e 6f 20 69 50 61 72 65 6e 74 2c 20 20 20 20 gno iParent,
175ae 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 /* Expecte
175af 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 d pointer map pa
175b0 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 rent page number
175b1 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e */. char *zCon
175b2 74 65 78 74 20 20 20 20 20 20 20 20 20 2f 2a 20 text /*
175b3 43 6f 6e 74 65 78 74 20 64 65 73 63 72 69 70 74 Context descript
175b4 69 6f 6e 20 28 75 73 65 64 20 66 6f 72 20 65 72 ion (used for er
175b5 72 6f 72 20 6d 73 67 29 20 2a 2f 0a 29 7b 0a 20 ror msg) */.){.
175b6 20 69 6e 74 20 72 63 3b 0a 20 20 75 38 20 65 50 int rc;. u8 eP
175b7 74 72 6d 61 70 54 79 70 65 3b 0a 20 20 50 67 6e trmapType;. Pgn
175b8 6f 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 3b o iPtrmapParent;
175b9 0a 0a 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 .. rc = ptrmapG
175ba 65 74 28 70 43 68 65 63 6b 2d 3e 70 42 74 2c 20 et(pCheck->pBt,
175bb 69 43 68 69 6c 64 2c 20 26 65 50 74 72 6d 61 70 iChild, &ePtrmap
175bc 54 79 70 65 2c 20 26 69 50 74 72 6d 61 70 50 61 Type, &iPtrmapPa
175bd 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 21 rent);. if( rc!
175be 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
175bf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
175c0 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65 63 6b 2d _NOMEM ) pCheck-
175c1 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
175c2 31 3b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 1;. checkAppe
175c3 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
175c4 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c 65 64 20 ontext, "Failed
175c5 74 6f 20 72 65 61 64 20 70 74 72 6d 61 70 20 6b to read ptrmap k
175c6 65 79 3d 25 64 22 2c 20 69 43 68 69 6c 64 29 3b ey=%d", iChild);
175c7 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
175c8 0a 0a 20 20 69 66 28 20 65 50 74 72 6d 61 70 54 .. if( ePtrmapT
175c9 79 70 65 21 3d 65 54 79 70 65 20 7c 7c 20 69 50 ype!=eType || iP
175ca 74 72 6d 61 70 50 61 72 65 6e 74 21 3d 69 50 61 trmapParent!=iPa
175cb 72 65 6e 74 20 29 7b 0a 20 20 20 20 63 68 65 63 rent ){. chec
175cc 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
175cd 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 k, zContext, .
175ce 20 20 20 20 22 42 61 64 20 70 74 72 20 6d 61 70 "Bad ptr map
175cf 20 65 6e 74 72 79 20 6b 65 79 3d 25 64 20 65 78 entry key=%d ex
175d0 70 65 63 74 65 64 3d 28 25 64 2c 25 64 29 20 67 pected=(%d,%d) g
175d1 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20 0a 20 20 ot=(%d,%d)", .
175d2 20 20 20 20 69 43 68 69 6c 64 2c 20 65 54 79 70 iChild, eTyp
175d3 65 2c 20 69 50 61 72 65 6e 74 2c 20 65 50 74 72 e, iParent, ePtr
175d4 6d 61 70 54 79 70 65 2c 20 69 50 74 72 6d 61 70 mapType, iPtrmap
175d5 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 23 Parent);. }.}.#
175d6 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 endif../*.** Che
175d7 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 ck the integrity
175d8 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 of the freelist
175d9 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65 72 66 6c or of an overfl
175da 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a ow page list..**
175db 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 Verify that the
175dc 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
175dd 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 on the list is
175de 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 N..*/.static voi
175df 64 20 63 68 65 63 6b 4c 69 73 74 28 0a 20 20 49 d checkList(. I
175e0 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 ntegrityCk *pChe
175e1 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 ck, /* Integrit
175e2 79 20 63 68 65 63 6b 69 6e 67 20 63 6f 6e 74 65 y checking conte
175e3 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 46 72 xt */. int isFr
175e4 65 65 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a eeList, /*
175e5 20 54 72 75 65 20 66 6f 72 20 61 20 66 72 65 65 True for a free
175e6 6c 69 73 74 2e 20 20 46 61 6c 73 65 20 66 6f 72 list. False for
175e7 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c overflow page l
175e8 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 ist */. int iPa
175e9 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ge, /
175ea 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f * Page number fo
175eb 72 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 r first page in
175ec 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e the list */. in
175ed 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20 20 20 t N,
175ee 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 /* Expected
175ef 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
175f0 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 in the list */.
175f1 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 char *zContext
175f2 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 /* Contex
175f3 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 t for error mess
175f4 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 ages */.){. int
175f5 20 69 3b 0a 20 20 69 6e 74 20 65 78 70 65 63 74 i;. int expect
175f6 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 20 69 46 ed = N;. int iF
175f7 69 72 73 74 20 3d 20 69 50 61 67 65 3b 0a 20 20 irst = iPage;.
175f8 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26 while( N-- > 0 &
175f9 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 & pCheck->mxErr
175fa 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70 ){. DbPage *p
175fb 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20 20 75 6e OvflPage;. un
175fc 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 4f 76 signed char *pOv
175fd 66 6c 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 flData;. if(
175fe 69 50 61 67 65 3c 31 20 29 7b 0a 20 20 20 20 20 iPage<1 ){.
175ff 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
17600 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 pCheck, zContext
17601 2c 0a 20 20 20 20 20 20 20 20 20 22 25 64 20 6f ,. "%d o
17602 66 20 25 64 20 70 61 67 65 73 20 6d 69 73 73 69 f %d pages missi
17603 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66 6c 6f 77 ng from overflow
17604 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 list starting a
17605 74 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 t %d",.
17606 20 4e 2b 31 2c 20 65 78 70 65 63 74 65 64 2c 20 N+1, expected,
17607 69 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20 62 iFirst);. b
17608 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
17609 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 43 68 if( checkRef(pCh
1760a 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e eck, iPage, zCon
1760b 74 65 78 74 29 20 29 20 62 72 65 61 6b 3b 0a 20 text) ) break;.
1760c 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 if( sqlite3Pa
1760d 67 65 72 47 65 74 28 70 43 68 65 63 6b 2d 3e 70 gerGet(pCheck->p
1760e 50 61 67 65 72 2c 20 28 50 67 6e 6f 29 69 50 61 Pager, (Pgno)iPa
1760f 67 65 2c 20 26 70 4f 76 66 6c 50 61 67 65 29 20 ge, &pOvflPage)
17610 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 ){. checkAp
17611 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
17612 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61 69 6c 65 zContext, "faile
17613 64 20 74 6f 20 67 65 74 20 70 61 67 65 20 25 64 d to get page %d
17614 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 ", iPage);.
17615 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
17616 20 20 70 4f 76 66 6c 44 61 74 61 20 3d 20 28 75 pOvflData = (u
17617 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 73 nsigned char *)s
17618 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
17619 74 61 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 ta(pOvflPage);.
1761a 20 20 20 69 66 28 20 69 73 46 72 65 65 4c 69 73 if( isFreeLis
1761b 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e t ){. int n
1761c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f 76 = get4byte(&pOv
1761d 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 69 66 6e flData[4]);.#ifn
1761e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1761f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
17620 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 if( pCheck->pBt
17621 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
17622 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 checkPtr
17623 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 50 61 67 map(pCheck, iPag
17624 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 e, PTRMAP_FREEPA
17625 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74 29 GE, 0, zContext)
17626 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 ;. }.#endif
17627 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 70 43 68 . if( n>pCh
17628 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 eck->pBt->usable
17629 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20 20 20 20 Size/4-2 ){.
1762a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
1762b 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
1762c 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ext,.
1762d 22 66 72 65 65 6c 69 73 74 20 6c 65 61 66 20 63 "freelist leaf c
1762e 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20 6f 6e 20 ount too big on
1762f 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 page %d", iPage)
17630 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d 3b 0a 20 ;. N--;.
17631 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
17632 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e for(i=0; i<n
17633 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
17634 20 20 50 67 6e 6f 20 69 46 72 65 65 50 61 67 65 Pgno iFreePage
17635 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f 76 = get4byte(&pOv
17636 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d 29 3b 0a flData[8+i*4]);.
17637 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
17638 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
17639 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 68 if( pCh
1763a 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 eck->pBt->autoVa
1763b 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 cuum ){.
1763c 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 checkPtrmap(
1763d 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 pCheck, iFreePag
1763e 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 e, PTRMAP_FREEPA
1763f 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74 29 GE, 0, zContext)
17640 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 ;. }.#e
17641 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 63 ndif. c
17642 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 heckRef(pCheck,
17643 69 46 72 65 65 50 61 67 65 2c 20 7a 43 6f 6e 74 iFreePage, zCont
17644 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a ext);. }.
17645 20 20 20 20 20 20 20 20 4e 20 2d 3d 20 6e 3b 0a N -= n;.
17646 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 }. }.#i
17647 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
17648 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
17649 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 else{. /*
1764a 49 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65 If this database
1764b 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 supports auto-v
1764c 61 63 75 75 6d 20 61 6e 64 20 69 50 61 67 65 20 acuum and iPage
1764d 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a is not the last.
1764e 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e ** page in
1764f 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 6c this overflow l
17650 69 73 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 ist, check that
17651 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 the pointer-map
17652 65 6e 74 72 79 20 66 6f 72 0a 20 20 20 20 20 20 entry for.
17653 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** the following
17654 20 70 61 67 65 20 6d 61 74 63 68 65 73 20 69 50 page matches iP
17655 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 age.. */.
17656 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e if( pCheck->
17657 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
17658 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20 20 20 20 && N>0 ){.
17659 20 20 69 20 3d 20 67 65 74 34 62 79 74 65 28 70 i = get4byte(p
1765a 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 20 OvflData);.
1765b 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 checkPtrmap(p
1765c 43 68 65 63 6b 2c 20 69 2c 20 50 54 52 4d 41 50 Check, i, PTRMAP
1765d 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 50 61 67 _OVERFLOW2, iPag
1765e 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 e, zContext);.
1765f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 }. }.#end
17660 69 66 0a 20 20 20 20 69 50 61 67 65 20 3d 20 67 if. iPage = g
17661 65 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 et4byte(pOvflDat
17662 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 a);. sqlite3P
17663 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66 6c 50 agerUnref(pOvflP
17664 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 age);. }.}.#end
17665 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
17666 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
17667 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 K */..#ifndef SQ
17668 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 LITE_OMIT_INTEGR
17669 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 ITY_CHECK./*.**
1766a 44 6f 20 76 61 72 69 6f 75 73 20 73 61 6e 69 74 Do various sanit
1766b 79 20 63 68 65 63 6b 73 20 6f 6e 20 61 20 73 69 y checks on a si
1766c 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 61 20 74 ngle page of a t
1766d 72 65 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 ree. Return.**
1766e 74 68 65 20 74 72 65 65 20 64 65 70 74 68 2e 20 the tree depth.
1766f 20 52 6f 6f 74 20 70 61 67 65 73 20 72 65 74 75 Root pages retu
17670 72 6e 20 30 2e 20 20 50 61 72 65 6e 74 73 20 6f rn 0. Parents o
17671 66 20 72 6f 6f 74 20 70 61 67 65 73 0a 2a 2a 20 f root pages.**
17672 72 65 74 75 72 6e 20 31 2c 20 61 6e 64 20 73 6f return 1, and so
17673 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 54 forth..** .** T
17674 68 65 73 65 20 63 68 65 63 6b 73 20 61 72 65 20 hese checks are
17675 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 done:.**.**
17676 20 31 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 1. Make sure t
17677 68 61 74 20 63 65 6c 6c 73 20 61 6e 64 20 66 72 hat cells and fr
17678 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e 6f 74 20 eeblocks do not
17679 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20 20 20 20 overlap.**
1767a 20 20 20 20 62 75 74 20 63 6f 6d 62 69 6e 65 20 but combine
1767b 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f to completely co
1767c 76 65 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a ver the page..**
1767d 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b 65 20 73 NO 2. Make s
1767e 75 72 65 20 63 65 6c 6c 20 6b 65 79 73 20 61 72 ure cell keys ar
1767f 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 e in order..**
17680 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 73 75 72 NO 3. Make sur
17681 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c 65 73 73 e no key is less
17682 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
17683 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 2e 0a 2a o zLowerBound..*
17684 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61 6b 65 20 * NO 4. Make
17685 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 67 sure no key is g
17686 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 reater than or e
17687 71 75 61 6c 20 74 6f 20 7a 55 70 70 65 72 42 6f qual to zUpperBo
17688 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 35 2e 20 und..** 5.
17689 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 Check the integ
1768a 72 69 74 79 20 6f 66 20 6f 76 65 72 66 6c 6f 77 rity of overflow
1768b 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20 20 20 20 pages..**
1768c 36 2e 20 20 52 65 63 75 72 73 69 76 65 6c 79 20 6. Recursively
1768d 63 61 6c 6c 20 63 68 65 63 6b 54 72 65 65 50 61 call checkTreePa
1768e 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69 6c 64 72 ge on all childr
1768f 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 2e 20 20 en..** 7.
17690 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 Verify that the
17691 64 65 70 74 68 20 6f 66 20 61 6c 6c 20 63 68 69 depth of all chi
17692 6c 64 72 65 6e 20 69 73 20 74 68 65 20 73 61 6d ldren is the sam
17693 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e 20 20 4d e..** 8. M
17694 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 70 61 ake sure this pa
17695 67 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 33 ge is at least 3
17696 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c 73 65 20 3% full or else
17697 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 it is.**
17698 20 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 the root of th
17699 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 e tree..*/.stati
1769a 63 20 69 6e 74 20 63 68 65 63 6b 54 72 65 65 50 c int checkTreeP
1769b 61 67 65 28 0a 20 20 49 6e 74 65 67 72 69 74 79 age(. Integrity
1769c 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 Ck *pCheck, /*
1769d 43 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65 20 Context for the
1769e 73 61 6e 69 74 79 20 63 68 65 63 6b 20 2a 2f 0a sanity check */.
1769f 20 20 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20 int iPage,
176a0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
176a1 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 number of the pa
176a2 67 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 ge to check */.
176a3 20 63 68 61 72 20 2a 7a 50 61 72 65 6e 74 43 6f char *zParentCo
176a4 6e 74 65 78 74 20 20 2f 2a 20 50 61 72 65 6e 74 ntext /* Parent
176a5 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 7b 0a 20 context */.){.
176a6 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
176a7 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c 20 64 65 . int i, rc, de
176a8 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f 2c 20 63 pth, d2, pgno, c
176a9 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72 2c 20 63 nt;. int hdr, c
176aa 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69 6e 74 20 ellStart;. int
176ab 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a 64 61 74 nCell;. u8 *dat
176ac 61 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 a;. BtShared *p
176ad 42 74 3b 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 Bt;. int usable
176ae 53 69 7a 65 3b 0a 20 20 63 68 61 72 20 7a 43 6f Size;. char zCo
176af 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20 20 63 68 ntext[100];. ch
176b0 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a 0a 20 20 ar *hit = 0;..
176b1 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
176b2 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 (sizeof(zContext
176b3 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 50 61 ), zContext, "Pa
176b4 67 65 20 25 64 3a 20 22 2c 20 69 50 61 67 65 29 ge %d: ", iPage)
176b5 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 ;.. /* Check th
176b6 61 74 20 74 68 65 20 70 61 67 65 20 65 78 69 73 at the page exis
176b7 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74 20 3d 20 ts. */. pBt =
176b8 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a 20 20 75 pCheck->pBt;. u
176b9 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d sableSize = pBt-
176ba 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 69 >usableSize;. i
176bb 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65 f( iPage==0 ) re
176bc 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 63 68 turn 0;. if( ch
176bd 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69 eckRef(pCheck, i
176be 50 61 67 65 2c 20 7a 50 61 72 65 6e 74 43 6f 6e Page, zParentCon
176bf 74 65 78 74 29 20 29 20 72 65 74 75 72 6e 20 30 text) ) return 0
176c0 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 73 71 ;. if( (rc = sq
176c1 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
176c2 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 50 61 e(pBt, (Pgno)iPa
176c3 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 21 ge, &pPage, 0))!
176c4 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63 =0 ){. if( rc
176c5 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
176c6 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 pCheck->mallocF
176c7 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 63 ailed = 1;. c
176c8 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
176c9 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a heck, zContext,.
176ca 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 "unable t
176cb 6f 20 67 65 74 20 74 68 65 20 70 61 67 65 2e 20 o get the page.
176cc 65 72 72 6f 72 20 63 6f 64 65 3d 25 64 22 2c 20 error code=%d",
176cd 72 63 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 rc);. return
176ce 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 72 63 0;. }. if( (rc
176cf 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 = sqlite3BtreeI
176d0 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 29 21 nitPage(pPage))!
176d1 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 =0 ){. assert
176d2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 ( rc==SQLITE_COR
176d3 52 55 50 54 20 29 3b 20 20 2f 2a 20 54 68 65 20 RUPT ); /* The
176d4 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 20 65 72 only possible er
176d5 72 6f 72 20 66 72 6f 6d 20 49 6e 69 74 50 61 67 ror from InitPag
176d6 65 20 2a 2f 0a 20 20 20 20 63 68 65 63 6b 41 70 e */. checkAp
176d7 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
176d8 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 zContext, .
176d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 73 "s
176da 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 qlite3BtreeInitP
176db 61 67 65 28 29 20 72 65 74 75 72 6e 73 20 65 72 age() returns er
176dc 72 6f 72 20 63 6f 64 65 20 25 64 22 2c 20 72 63 ror code %d", rc
176dd 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 );. releasePa
176de 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 ge(pPage);. r
176df 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 eturn 0;. }..
176e0 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c /* Check out all
176e1 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f the cells.. */
176e2 0a 20 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 . depth = 0;.
176e3 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 for(i=0; i<pPage
176e4 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 63 ->nCell && pChec
176e5 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a k->mxErr; i++){.
176e6 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 u8 *pCell;.
176e7 20 20 20 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 u32 sz;. C
176e8 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 ellInfo info;..
176e9 20 20 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c /* Check payl
176ea 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 oad overflow pag
176eb 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 es. */. sq
176ec 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
176ed 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c izeof(zContext),
176ee 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 zContext,.
176ef 20 20 20 20 20 20 20 20 22 4f 6e 20 74 72 65 65 "On tree
176f0 20 70 61 67 65 20 25 64 20 63 65 6c 6c 20 25 64 page %d cell %d
176f1 3a 20 22 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a : ", iPage, i);.
176f2 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 pCell = find
176f3 43 65 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 Cell(pPage,i);.
176f4 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 sqlite3BtreeP
176f5 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
176f6 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 e, pCell, &info)
176f7 3b 0a 20 20 20 20 73 7a 20 3d 20 69 6e 66 6f 2e ;. sz = info.
176f8 6e 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 21 nData;. if( !
176f9 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 20 pPage->intKey )
176fa 73 7a 20 2b 3d 20 28 69 6e 74 29 69 6e 66 6f 2e sz += (int)info.
176fb 6e 4b 65 79 3b 0a 20 20 20 20 61 73 73 65 72 74 nKey;. assert
176fc 28 20 73 7a 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c ( sz==info.nPayl
176fd 6f 61 64 20 29 3b 0a 20 20 20 20 69 66 28 20 73 oad );. if( s
176fe 7a 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b z>info.nLocal ){
176ff 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 . int nPage
17700 20 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c = (sz - info.nL
17701 6f 63 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a ocal + usableSiz
17702 65 20 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 e - 5)/(usableSi
17703 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 ze - 4);. P
17704 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 gno pgnoOvfl = g
17705 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 et4byte(&pCell[i
17706 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b nfo.iOverflow]);
17707 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
17708 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
17709 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 if( pBt->a
1770a 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 utoVacuum ){.
1770b 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 checkPtrmap
1770c 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 (pCheck, pgnoOvf
1770d 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c l, PTRMAP_OVERFL
1770e 4f 57 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e OW1, iPage, zCon
1770f 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 text);. }.#
17710 65 6e 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 endif. chec
17711 6b 4c 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c kList(pCheck, 0,
17712 20 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 pgnoOvfl, nPage
17713 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 , zContext);.
17714 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b }.. /* Check
17715 20 73 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 sanity of left
17716 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 child page..
17717 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 */. if( !pPag
17718 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 e->leaf ){.
17719 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 pgno = get4byte
1771a 28 70 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 (pCell);.#ifndef
1771b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1771c 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 OVACUUM. if
1771d 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
1771e 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 m ){. che
1771f 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c ckPtrmap(pCheck,
17720 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 pgno, PTRMAP_BT
17721 52 45 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e REE, iPage, zCon
17722 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 text);. }.#
17723 65 6e 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d endif. d2 =
17724 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 checkTreePage(p
17725 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f Check, pgno, zCo
17726 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 ntext);. if
17727 28 20 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 ( i>0 && d2!=dep
17728 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 th ){. ch
17729 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
1772a 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 eck, zContext, "
1772b 43 68 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 Child page depth
1772c 20 64 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 differs");.
1772d 20 20 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 }. depth
1772e 3d 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = d2;. }. }.
1772f 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
17730 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d af ){. pgno =
17731 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
17732 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
17733 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 drOffset+8]);.
17734 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
17735 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 tf(sizeof(zConte
17736 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a xt), zContext, .
17737 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17738 20 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 "On page %d
17739 20 61 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a at right child:
1773a 20 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e ", iPage);.#ifn
1773b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1773c 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 AUTOVACUUM. i
1773d 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 f( pBt->autoVacu
1773e 75 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 um ){. chec
1773f 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 kPtrmap(pCheck,
17740 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 pgno, PTRMAP_BTR
17741 45 45 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 EE, iPage, 0);.
17742 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
17743 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 checkTreePage(pC
17744 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e heck, pgno, zCon
17745 74 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f text);. }. . /
17746 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 * Check for comp
17747 6c 65 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 lete coverage of
17748 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 the page. */.
17749 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
1774a 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 Data;. hdr = pP
1774b 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a age->hdrOffset;.
1774c 20 20 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 hit = sqlite3P
1774d 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e ageMalloc( pBt->
1774e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 pageSize );. if
1774f 28 20 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ( hit==0 ){.
17750 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 pCheck->mallocFa
17751 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 iled = 1;. }els
17752 65 7b 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 e{. u16 conte
17753 6e 74 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 ntOffset = get2b
17754 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
17755 29 3b 0a 20 20 20 20 69 66 20 28 63 6f 6e 74 65 );. if (conte
17756 6e 74 4f 66 66 73 65 74 20 3e 20 75 73 61 62 6c ntOffset > usabl
17757 65 53 69 7a 65 29 20 7b 0a 20 20 20 20 20 20 63 eSize) {. c
17758 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
17759 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 heck, 0, .
1775a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
1775b 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 Corruption detec
1775c 74 65 64 20 69 6e 20 68 65 61 64 65 72 20 6f 6e ted in header on
1775d 20 70 61 67 65 20 25 64 22 2c 69 50 61 67 65 2c page %d",iPage,
1775e 30 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 0);. goto c
1775f 68 65 63 6b 5f 70 61 67 65 5f 61 62 6f 72 74 3b heck_page_abort;
17760 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 . }. memse
17761 74 28 68 69 74 2b 63 6f 6e 74 65 6e 74 4f 66 66 t(hit+contentOff
17762 73 65 74 2c 20 30 2c 20 75 73 61 62 6c 65 53 69 set, 0, usableSi
17763 7a 65 2d 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 ze-contentOffset
17764 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 );. memset(hi
17765 74 2c 20 31 2c 20 63 6f 6e 74 65 6e 74 4f 66 66 t, 1, contentOff
17766 73 65 74 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 set);. nCell
17767 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
17768 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20 63 65 [hdr+3]);. ce
17769 6c 6c 53 74 61 72 74 20 3d 20 68 64 72 20 2b 20 llStart = hdr +
1776a 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 12 - 4*pPage->le
1776b 61 66 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b af;. for(i=0;
1776c 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a i<nCell; i++){.
1776d 20 20 20 20 20 20 69 6e 74 20 70 63 20 3d 20 67 int pc = g
1776e 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 et2byte(&data[ce
1776f 6c 6c 53 74 61 72 74 2b 69 2a 32 5d 29 3b 0a 20 llStart+i*2]);.
17770 20 20 20 20 20 75 31 36 20 73 69 7a 65 20 3d 20 u16 size =
17771 31 30 32 34 3b 0a 20 20 20 20 20 20 69 6e 74 20 1024;. int
17772 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c j;. if( pc<
17773 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 =usableSize ){.
17774 20 20 20 20 20 20 20 73 69 7a 65 20 3d 20 63 65 size = ce
17775 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c llSizePtr(pPage,
17776 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 &data[pc]);.
17777 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 }. if( (
17778 70 63 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 pc+size-1)>=usab
17779 6c 65 53 69 7a 65 20 7c 7c 20 70 63 3c 30 20 29 leSize || pc<0 )
1777a 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 {. checkA
1777b 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
1777c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 0, .
1777d 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 "Corruption det
1777e 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 ected in cell %d
1777f 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 on page %d",i,i
17780 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d Page,0);. }
17781 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f else{. fo
17782 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a r(j=pc+size-1; j
17783 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a >=pc; j--) hit[j
17784 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ]++;. }.
17785 20 7d 0a 20 20 20 20 66 6f 72 28 63 6e 74 3d 30 }. for(cnt=0
17786 2c 20 69 3d 67 65 74 32 62 79 74 65 28 26 64 61 , i=get2byte(&da
17787 74 61 5b 68 64 72 2b 31 5d 29 3b 20 69 3e 30 20 ta[hdr+1]); i>0
17788 26 26 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 20 && i<usableSize
17789 26 26 20 63 6e 74 3c 31 30 30 30 30 3b 20 0a 20 && cnt<10000; .
1778a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 29 cnt++)
1778b 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 {. int size
1778c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
1778d 61 5b 69 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 a[i+2]);. i
1778e 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 nt j;. if(
1778f 28 69 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 (i+size-1)>=usab
17790 6c 65 53 69 7a 65 20 7c 7c 20 69 3c 30 20 29 7b leSize || i<0 ){
17791 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 . checkAp
17792 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
17793 30 2c 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 0, .
17794 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 "Corruption det
17795 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 ected in cell %d
17796 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 on page %d",i,i
17797 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d Page,0);. }
17798 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f else{. fo
17799 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a 3e r(j=i+size-1; j>
1779a 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b =i; j--) hit[j]+
1779b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 +;. }.
1779c 20 69 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 i = get2byte(&d
1779d 61 74 61 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 ata[i]);. }.
1779e 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20 for(i=cnt=0;
1779f 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b i<usableSize; i+
177a0 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69 +){. if( hi
177a1 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 t[i]==0 ){.
177a2 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 cnt++;.
177a3 7d 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d }else if( hit[i]
177a4 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 >1 ){. ch
177a5 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
177a6 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 eck, 0,.
177a7 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73 "Multiple uses
177a8 20 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20 for byte %d of
177a9 70 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61 page %d", i, iPa
177aa 67 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 ge);. bre
177ab 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
177ac 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64 }. if( cnt!=d
177ad 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20 ata[hdr+7] ){.
177ae 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
177af 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 sg(pCheck, 0, .
177b0 20 20 20 20 20 20 20 20 20 22 46 72 61 67 6d 65 "Fragme
177b1 6e 74 65 64 20 73 70 61 63 65 20 69 73 20 25 64 nted space is %d
177b2 20 62 79 74 65 20 72 65 70 6f 72 74 65 64 20 61 byte reported a
177b3 73 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 s %d on page %d"
177b4 2c 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c ,. cnt,
177b5 20 64 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 data[hdr+7], iP
177b6 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a age);. }. }.
177b7 63 68 65 63 6b 5f 70 61 67 65 5f 61 62 6f 72 74 check_page_abort
177b8 3a 0a 20 20 69 66 20 28 68 69 74 29 20 73 71 6c :. if (hit) sql
177b9 69 74 65 33 50 61 67 65 46 72 65 65 28 68 69 74 ite3PageFree(hit
177ba 29 3b 0a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 );.. releasePag
177bb 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 e(pPage);. retu
177bc 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a 23 65 rn depth+1;.}.#e
177bd 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
177be 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 MIT_INTEGRITY_CH
177bf 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 ECK */..#ifndef
177c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 SQLITE_OMIT_INTE
177c1 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a GRITY_CHECK./*.*
177c2 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
177c3 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74 65 20 63 oes a complete c
177c4 68 65 63 6b 20 6f 66 20 74 68 65 20 67 69 76 65 heck of the give
177c5 6e 20 42 54 72 65 65 20 66 69 6c 65 2e 20 20 61 n BTree file. a
177c6 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61 6e 20 Root[] is.** an
177c7 61 72 72 61 79 20 6f 66 20 70 61 67 65 73 20 6e array of pages n
177c8 75 6d 62 65 72 73 20 77 65 72 65 20 65 61 63 68 umbers were each
177c9 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 page number is
177ca 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 the root page of
177cb 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20 20 6e 52 .** a table. nR
177cc 6f 6f 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 oot is the numbe
177cd 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
177ce 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 aRoot..**.** Wri
177cf 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 te the number of
177d0 20 65 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a error seen in *
177d1 70 6e 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 pnErr. Except f
177d2 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a or some memory.*
177d3 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 * allocation err
177d4 6f 72 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d ors, an error m
177d5 65 73 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d essage held in m
177d6 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
177d7 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73 rom.** malloc is
177d8 20 72 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e returned if *pn
177d9 45 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e Err is non-zero.
177da 20 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74 If *pnErr==0 t
177db 68 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 72 hen NULL is.** r
177dc 65 74 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d eturned. If a m
177dd 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
177de 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e error occurs, N
177df 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e ULL is returned.
177e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
177e1 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
177e2 42 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 BtreeIntegrityCh
177e3 65 63 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c eck(. Btree *p,
177e4 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 /* The btre
177e5 65 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 e to be checked
177e6 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c */. int *aRoot,
177e7 20 20 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f /* An array o
177e8 66 20 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d f root pages num
177e9 62 65 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 bers for individ
177ea 75 61 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 ual trees */. i
177eb 6e 74 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 nt nRoot, /*
177ec 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
177ed 73 20 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a s in aRoot[] */.
177ee 20 20 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 int mxErr,
177ef 2f 2a 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e /* Stop reportin
177f0 67 20 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 g errors after t
177f1 68 69 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e his many */. in
177f2 74 20 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 t *pnErr /* W
177f3 72 69 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 rite number of e
177f4 72 72 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 rrors seen to th
177f5 69 73 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 is variable */.)
177f6 7b 0a 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 6e {. Pgno i;. in
177f7 74 20 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 t nRef;. Integr
177f8 69 74 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 ityCk sCheck;.
177f9 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
177fa 70 2d 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a p->pBt;. char z
177fb 45 72 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c Err[100];.. sql
177fc 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
177fd 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 );. pBt->db = p
177fe 2d 3e 64 62 3b 0a 20 20 6e 52 65 66 20 3d 20 73 ->db;. nRef = s
177ff 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f qlite3PagerRefco
17800 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 unt(pBt->pPager)
17801 3b 0a 20 20 69 66 28 20 6c 6f 63 6b 42 74 72 65 ;. if( lockBtre
17802 65 57 69 74 68 52 65 74 72 79 28 70 29 21 3d 53 eWithRetry(p)!=S
17803 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17804 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20 20 *pnErr = 1;.
17805 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
17806 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e e(p);. return
17807 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
17808 28 30 2c 20 22 63 61 6e 6e 6f 74 20 61 63 71 75 (0, "cannot acqu
17809 69 72 65 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 ire a read lock
1780a 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 22 on the database"
1780b 29 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e );. }. sCheck.
1780c 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 73 43 68 pBt = pBt;. sCh
1780d 65 63 6b 2e 70 50 61 67 65 72 20 3d 20 70 42 74 eck.pPager = pBt
1780e 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 43 68 65 ->pPager;. sChe
1780f 63 6b 2e 6e 50 61 67 65 20 3d 20 70 61 67 65 72 ck.nPage = pager
17810 50 61 67 65 63 6f 75 6e 74 28 73 43 68 65 63 6b Pagecount(sCheck
17811 2e 70 42 74 29 3b 0a 20 20 73 43 68 65 63 6b 2e .pBt);. sCheck.
17812 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20 mxErr = mxErr;.
17813 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30 sCheck.nErr = 0
17814 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f ;. sCheck.mallo
17815 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 2a cFailed = 0;. *
17816 70 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 pnErr = 0;. if(
17817 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d 3d 30 sCheck.nPage==0
17818 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 ){. unlockBt
17819 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
1781a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1781b 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 eeLeave(p);.
1781c 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
1781d 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d 20 73 sCheck.anRef = s
1781e 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 73 qlite3Malloc( (s
1781f 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 Check.nPage+1)*s
17820 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 6e 52 izeof(sCheck.anR
17821 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 ef[0]) );. if(
17822 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 29 7b !sCheck.anRef ){
17823 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 . unlockBtree
17824 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 IfUnused(pBt);.
17825 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 *pnErr = 1;.
17826 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
17827 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 eave(p);. ret
17828 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 urn 0;. }. for
17829 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e (i=0; i<=sCheck.
1782a 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 nPage; i++){ sCh
1782b 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 eck.anRef[i] = 0
1782c 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e ; }. i = PENDIN
1782d 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
1782e 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 ;. if( i<=sChec
1782f 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 k.nPage ){. s
17830 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d Check.anRef[i] =
17831 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 1;. }. sqlite
17832 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 3StrAccumInit(&s
17833 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 Check.errMsg, zE
17834 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 rr, sizeof(zErr)
17835 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 , 20000);.. /*
17836 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 Check the integr
17837 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c ity of the freel
17838 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b ist. */. check
17839 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c List(&sCheck, 1,
1783a 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e get4byte(&pBt->
1783b 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
1783c 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ]),.
1783d 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 get4byte(&pBt->p
1783e 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d Page1->aData[36]
1783f 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 ), "Main freelis
17840 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 t: ");.. /* Che
17841 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 ck all the table
17842 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d s.. */. for(i=
17843 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 0; (int)i<nRoot
17844 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b && sCheck.mxErr;
17845 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 i++){. if( a
17846 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e Root[i]==0 ) con
17847 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 tinue;.#ifndef S
17848 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
17849 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 ACUUM. if( pB
1784a 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 t->autoVacuum &&
1784b 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 aRoot[i]>1 ){.
1784c 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 checkPtrmap
1784d 28 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b (&sCheck, aRoot[
1784e 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 i], PTRMAP_ROOTP
1784f 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 AGE, 0, 0);.
17850 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 }.#endif. che
17851 63 6b 54 72 65 65 50 61 67 65 28 26 73 43 68 65 ckTreePage(&sChe
17852 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c ck, aRoot[i], "L
17853 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 ist of tree root
17854 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a s: ");. }.. /*
17855 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 Make sure every
17856 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c page in the fil
17857 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a e is referenced.
17858 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 */. for(i=1;
17859 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 i<=sCheck.nPage
1785a 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b && sCheck.mxErr;
1785b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 i++){.#ifdef SQ
1785c 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1785d 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 CUUM. if( sCh
1785e 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 eck.anRef[i]==0
1785f 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 ){. checkAp
17860 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c pendMsg(&sCheck,
17861 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 0, "Page %d is
17862 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b never used", i);
17863 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 . }.#else.
17864 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 /* If the datab
17865 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 ase supports aut
17866 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 o-vacuum, make s
17867 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f ure no tables co
17868 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 ntain. ** ref
17869 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 erences to point
1786a 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 er-map pages..
1786b 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 */. if( sCh
1786c 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 eck.anRef[i]==0
1786d 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d && . (PTRM
1786e 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 AP_PAGENO(pBt, i
1786f 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 )!=i || !pBt->au
17870 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 toVacuum) ){.
17871 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
17872 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 g(&sCheck, 0, "P
17873 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 age %d is never
17874 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d used", i);. }
17875 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e . if( sCheck.
17876 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a anRef[i]!=0 && .
17877 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 (PTRMAP_P
17878 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 AGENO(pBt, i)==i
17879 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 && pBt->autoVac
1787a 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 uum) ){. ch
1787b 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 eckAppendMsg(&sC
1787c 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 heck, 0, "Pointe
1787d 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 r map page %d is
1787e 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 referenced", i)
1787f 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
17880 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 }.. /* Make su
17881 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 re this analysis
17882 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 did not leave a
17883 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 ny unref() pages
17884 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 .. ** This is a
17885 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69 n internal consi
17886 73 74 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e stency check; an
17887 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b integrity check
17888 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74 . ** of the int
17889 65 67 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 20 egrity check..
1788a 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 */. unlockBtree
1788b 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 IfUnused(pBt);.
1788c 20 69 66 28 20 4e 45 56 45 52 28 6e 52 65 66 20 if( NEVER(nRef
1788d 21 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 != sqlite3PagerR
1788e 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 efcount(pBt->pPa
1788f 67 65 72 29 29 20 29 7b 0a 20 20 20 20 63 68 65 ger)) ){. che
17890 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 ckAppendMsg(&sCh
17891 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 22 eck, 0, . "
17892 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 Outstanding page
17893 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 72 6f 6d count goes from
17894 20 25 64 20 74 6f 20 25 64 20 64 75 72 69 6e 67 %d to %d during
17895 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 22 2c this analysis",
17896 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 73 71 6c . nRef, sql
17897 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e ite3PagerRefcoun
17898 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 0a 20 t(pBt->pPager).
17899 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 );. }.. /*
1789a 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 20 72 65 Clean up and re
1789b 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a 20 20 2a port errors.. *
1789c 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 /. sqlite3Btree
1789d 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 71 6c 69 Leave(p);. sqli
1789e 74 65 33 5f 66 72 65 65 28 73 43 68 65 63 6b 2e te3_free(sCheck.
1789f 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 20 73 43 anRef);. if( sC
178a0 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 heck.mallocFaile
178a1 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 d ){. sqlite3
178a2 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 26 73 StrAccumReset(&s
178a3 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 Check.errMsg);.
178a4 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 *pnErr = sChe
178a5 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 20 20 72 ck.nErr+1;. r
178a6 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a eturn 0;. }. *
178a7 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b 2e 6e pnErr = sCheck.n
178a8 45 72 72 3b 0a 20 20 69 66 28 20 73 43 68 65 63 Err;. if( sChec
178a9 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 71 6c 69 k.nErr==0 ) sqli
178aa 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 te3StrAccumReset
178ab 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 (&sCheck.errMsg)
178ac 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 ;. return sqlit
178ad 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 e3StrAccumFinish
178ae 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 (&sCheck.errMsg)
178af 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
178b0 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 LITE_OMIT_INTEGR
178b1 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a ITY_CHECK */../*
178b2 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 .** Return the f
178b3 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 ull pathname of
178b4 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 the underlying d
178b5 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
178b6 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 66 69 .** The pager fi
178b7 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 lename is invari
178b8 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 ant as long as t
178b9 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f he pager is.** o
178ba 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 pen so it is saf
178bb 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 e to access with
178bc 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 65 64 out the BtShared
178bd 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 mutex..*/.SQLIT
178be 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
178bf 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 char *sqlite3Btr
178c0 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 42 74 eeGetFilename(Bt
178c1 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
178c2 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 t( p->pBt->pPage
178c3 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e r!=0 );. return
178c4 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c sqlite3PagerFil
178c5 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 ename(p->pBt->pP
178c6 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ager);.}../*.**
178c7 52 65 74 75 72 6e 20 74 68 65 20 70 61 74 68 6e Return the pathn
178c8 61 6d 65 20 6f 66 20 74 68 65 20 64 69 72 65 63 ame of the direc
178c9 74 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 tory that contai
178ca 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ns the database
178cb 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 file..**.** The
178cc 70 61 67 65 72 20 64 69 72 65 63 74 6f 72 79 20 pager directory
178cd 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e name is invarian
178ce 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 t as long as the
178cf 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 pager is.** ope
178d0 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 65 20 n so it is safe
178d1 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f 75 to access withou
178d2 74 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d t the BtShared m
178d3 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f utex..*/.SQLITE_
178d4 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
178d5 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 ar *sqlite3Btree
178d6 47 65 74 44 69 72 6e 61 6d 65 28 42 74 72 65 65 GetDirname(Btree
178d7 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
178d8 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d p->pBt->pPager!=
178d9 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 0 );. return sq
178da 6c 69 74 65 33 50 61 67 65 72 44 69 72 6e 61 6d lite3PagerDirnam
178db 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 e(p->pBt->pPager
178dc 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
178dd 72 6e 20 74 68 65 20 70 61 74 68 6e 61 6d 65 20 rn the pathname
178de 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
178df 69 6c 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 ile for this dat
178e0 61 62 61 73 65 2e 20 54 68 65 20 72 65 74 75 72 abase. The retur
178e1 6e 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 n.** value of th
178e2 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
178e3 65 20 73 61 6d 65 20 72 65 67 61 72 64 6c 65 73 e same regardles
178e4 73 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 65 s of whether the
178e5 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a journal file.**
178e6 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 has been create
178e7 64 20 6f 72 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 d or not..**.**
178e8 54 68 65 20 70 61 67 65 72 20 6a 6f 75 72 6e 61 The pager journa
178e9 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e l filename is in
178ea 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 variant as long
178eb 61 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0a as the pager is.
178ec 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 ** open so it is
178ed 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 safe to access
178ee 77 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 without the BtSh
178ef 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 ared mutex..*/.S
178f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
178f1 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
178f2 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 3BtreeGetJournal
178f3 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a name(Btree *p){.
178f4 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 assert( p->pBt
178f5 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 ->pPager!=0 );.
178f6 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 return sqlite3P
178f7 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 agerJournalname(
178f8 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b p->pBt->pPager);
178f9 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
178fa 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f TE_OMIT_VACUUM./
178fb 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63 6f *.** Copy the co
178fc 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f mplete content o
178fd 66 20 70 42 74 46 72 6f 6d 20 69 6e 74 6f 20 70 f pBtFrom into p
178fe 42 74 54 6f 2e 20 20 41 20 74 72 61 6e 73 61 63 BtTo. A transac
178ff 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 tion.** must be
17900 61 63 74 69 76 65 20 66 6f 72 20 62 6f 74 68 20 active for both
17901 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 files..**.** The
17902 20 73 69 7a 65 20 6f 66 20 66 69 6c 65 20 70 54 size of file pT
17903 6f 20 6d 61 79 20 62 65 20 72 65 64 75 63 65 64 o may be reduced
17904 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 by this operati
17905 6f 6e 2e 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69 on..** If anythi
17906 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 74 ng goes wrong, t
17907 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f he transaction o
17908 6e 20 70 54 6f 20 69 73 20 72 6f 6c 6c 65 64 20 n pTo is rolled
17909 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 back. .**.** If
1790a 73 75 63 63 65 73 73 66 75 6c 2c 20 43 6f 6d 6d successful, Comm
1790b 69 74 50 68 61 73 65 4f 6e 65 28 29 20 6d 61 79 itPhaseOne() may
1790c 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 70 54 be called on pT
1790d 6f 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 o before returni
1790e 6e 67 2e 20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ng. .** The call
1790f 65 72 20 73 68 6f 75 6c 64 20 66 69 6e 69 73 68 er should finish
17910 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68 65 20 committing the
17911 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 70 transaction on p
17912 54 6f 20 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a To by calling.**
17913 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
17914 6d 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 mit()..*/.static
17915 20 69 6e 74 20 62 74 72 65 65 43 6f 70 79 46 69 int btreeCopyFi
17916 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 le(Btree *pTo, B
17917 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 tree *pFrom){.
17918 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
17919 4f 4b 3b 0a 20 20 50 67 6e 6f 20 69 3b 0a 0a 20 OK;. Pgno i;..
1791a 20 50 67 6e 6f 20 6e 46 72 6f 6d 50 61 67 65 3b Pgno nFromPage;
1791b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1791c 66 20 70 61 67 65 73 20 69 6e 20 70 46 72 6f 6d f pages in pFrom
1791d 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 54 6f 50 61 */. Pgno nToPa
1791e 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ge; /* Num
1791f 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
17920 70 54 6f 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4e pTo */. Pgno nN
17921 65 77 50 61 67 65 3b 20 20 20 20 20 20 2f 2a 20 ewPage; /*
17922 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
17923 69 6e 20 70 54 6f 20 61 66 74 65 72 20 74 68 65 in pTo after the
17924 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 50 67 6e 6f copy */.. Pgno
17925 20 69 53 6b 69 70 3b 20 20 20 20 20 20 20 20 20 iSkip;
17926 2f 2a 20 50 65 6e 64 69 6e 67 20 62 79 74 65 20 /* Pending byte
17927 70 61 67 65 20 69 6e 20 70 54 6f 20 2a 2f 0a 20 page in pTo */.
17928 20 69 6e 74 20 6e 54 6f 50 61 67 65 53 69 7a 65 int nToPageSize
17929 3b 20 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a ; /* Page siz
1792a 65 20 6f 66 20 70 54 6f 20 69 6e 20 62 79 74 65 e of pTo in byte
1792b 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 6f 6d s */. int nFrom
1792c 50 61 67 65 53 69 7a 65 3b 20 20 2f 2a 20 50 61 PageSize; /* Pa
1792d 67 65 20 73 69 7a 65 20 6f 66 20 70 46 72 6f 6d ge size of pFrom
1792e 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 0a 20 20 in bytes */..
1792f 42 74 53 68 61 72 65 64 20 2a 70 42 74 54 6f 20 BtShared *pBtTo
17930 3d 20 70 54 6f 2d 3e 70 42 74 3b 0a 20 20 42 74 = pTo->pBt;. Bt
17931 53 68 61 72 65 64 20 2a 70 42 74 46 72 6f 6d 20 Shared *pBtFrom
17932 3d 20 70 46 72 6f 6d 2d 3e 70 42 74 3b 0a 20 20 = pFrom->pBt;.
17933 70 42 74 54 6f 2d 3e 64 62 20 3d 20 70 54 6f 2d pBtTo->db = pTo-
17934 3e 64 62 3b 0a 20 20 70 42 74 46 72 6f 6d 2d 3e >db;. pBtFrom->
17935 64 62 20 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a db = pFrom->db;.
17936 0a 20 20 6e 54 6f 50 61 67 65 53 69 7a 65 20 3d . nToPageSize =
17937 20 70 42 74 54 6f 2d 3e 70 61 67 65 53 69 7a 65 pBtTo->pageSize
17938 3b 0a 20 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a ;. nFromPageSiz
17939 65 20 3d 20 70 42 74 46 72 6f 6d 2d 3e 70 61 67 e = pBtFrom->pag
1793a 65 53 69 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 eSize;.. assert
1793b 28 20 70 54 6f 2d 3e 69 6e 54 72 61 6e 73 3d 3d ( pTo->inTrans==
1793c 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
1793d 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e assert( pFrom->
1793e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
1793f 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20 4e 45 RITE );. if( NE
17940 56 45 52 28 70 42 74 54 6f 2d 3e 70 43 75 72 73 VER(pBtTo->pCurs
17941 6f 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 or) ){. retur
17942 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 n SQLITE_BUSY;.
17943 20 7d 0a 0a 20 20 6e 54 6f 50 61 67 65 20 3d 20 }.. nToPage =
17944 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
17945 42 74 54 6f 29 3b 0a 20 20 6e 46 72 6f 6d 50 61 BtTo);. nFromPa
17946 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f ge = pagerPageco
17947 75 6e 74 28 70 42 74 46 72 6f 6d 29 3b 0a 20 20 unt(pBtFrom);.
17948 69 53 6b 69 70 20 3d 20 50 45 4e 44 49 4e 47 5f iSkip = PENDING_
17949 42 59 54 45 5f 50 41 47 45 28 70 42 74 54 6f 29 BYTE_PAGE(pBtTo)
1794a 3b 0a 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 ;.. /* Variable
1794b 20 6e 4e 65 77 50 61 67 65 20 69 73 20 74 68 65 nNewPage is the
1794c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1794d 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
1794e 72 65 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6e 74 re the. ** cont
1794f 65 6e 74 73 20 6f 66 20 70 46 72 6f 6d 20 75 73 ents of pFrom us
17950 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 ing the current
17951 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 70 54 6f page-size of pTo
17952 2e 0a 20 20 2a 2f 0a 20 20 6e 4e 65 77 50 61 67 .. */. nNewPag
17953 65 20 3d 20 28 50 67 6e 6f 29 0a 20 20 20 20 20 e = (Pgno).
17954 28 28 28 69 36 34 29 6e 46 72 6f 6d 50 61 67 65 (((i64)nFromPage
17955 2a 28 69 36 34 29 6e 46 72 6f 6d 50 61 67 65 53 *(i64)nFromPageS
17956 69 7a 65 2b 28 69 36 34 29 6e 54 6f 50 61 67 65 ize+(i64)nToPage
17957 53 69 7a 65 2d 31 29 2f 28 69 36 34 29 6e 54 6f Size-1)/(i64)nTo
17958 50 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 66 6f PageSize);.. fo
17959 72 28 69 3d 31 3b 20 72 63 3d 3d 53 51 4c 49 54 r(i=1; rc==SQLIT
1795a 45 5f 4f 4b 20 26 26 20 28 69 3c 3d 6e 54 6f 50 E_OK && (i<=nToP
1795b 61 67 65 20 7c 7c 20 69 3c 3d 6e 4e 65 77 50 61 age || i<=nNewPa
1795c 67 65 29 3b 20 69 2b 2b 29 7b 0a 0a 20 20 20 20 ge); i++){..
1795d 2f 2a 20 4a 6f 75 72 6e 61 6c 20 74 68 65 20 6f /* Journal the o
1795e 72 69 67 69 6e 61 6c 20 70 61 67 65 2e 0a 20 20 riginal page..
1795f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 69 53 6b 69 **. ** iSki
17960 70 20 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 p is the page nu
17961 6d 62 65 72 20 6f 66 20 74 68 65 20 6c 6f 63 6b mber of the lock
17962 69 6e 67 20 70 61 67 65 20 28 50 45 4e 44 49 4e ing page (PENDIN
17963 47 5f 42 59 54 45 5f 50 41 47 45 29 0a 20 20 20 G_BYTE_PAGE).
17964 20 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 ** in database
17965 2a 70 54 6f 20 28 62 65 66 6f 72 65 20 74 68 65 *pTo (before the
17966 20 63 6f 70 79 29 2e 20 54 68 69 73 20 70 61 67 copy). This pag
17967 65 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 e is never writt
17968 65 6e 20 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 en . ** into
17969 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1796a 2e 20 55 6e 6c 65 73 73 20 69 3d 3d 69 53 6b 69 . Unless i==iSki
1796b 70 20 6f 72 20 74 68 65 20 70 61 67 65 20 77 61 p or the page wa
1796c 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 70 72 65 s not. ** pre
1796d 73 65 6e 74 20 69 6e 20 70 54 6f 20 62 65 66 6f sent in pTo befo
1796e 72 65 20 74 68 65 20 63 6f 70 79 20 6f 70 65 72 re the copy oper
1796f 61 74 69 6f 6e 2c 20 6a 6f 75 72 6e 61 6c 20 70 ation, journal p
17970 61 67 65 20 69 20 66 72 6f 6d 20 70 54 6f 2e 0a age i from pTo..
17971 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 */. if( i
17972 21 3d 69 53 6b 69 70 20 26 26 20 69 3c 3d 6e 54 !=iSkip && i<=nT
17973 6f 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 44 oPage ){. D
17974 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 20 3d bPage *pDbPage =
17975 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 0;. rc = s
17976 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
17977 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c 20 69 2c BtTo->pPager, i,
17978 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 &pDbPage);.
17979 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1797a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
1797b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1797c 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a Write(pDbPage);.
1797d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
1797e 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3e 6e SQLITE_OK && i>n
1797f 46 72 6f 6d 50 61 67 65 20 29 7b 0a 20 20 20 20 FromPage ){.
17980 20 20 20 20 20 20 2f 2a 20 59 65 61 68 2e 20 20 /* Yeah.
17981 49 74 20 73 65 65 6d 73 20 77 69 65 72 64 20 74 It seems wierd t
17982 6f 20 63 61 6c 6c 20 44 6f 6e 74 57 72 69 74 65 o call DontWrite
17983 28 29 20 72 69 67 68 74 20 61 66 74 65 72 20 57 () right after W
17984 72 69 74 65 28 29 2e 20 42 75 74 0a 20 20 20 20 rite(). But.
17985 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 69 73 ** that is
17986 20 62 65 63 61 75 73 65 20 74 68 65 20 6e 61 6d because the nam
17987 65 73 20 6f 66 20 74 68 6f 73 65 20 70 72 6f 63 es of those proc
17988 65 64 75 72 65 73 20 64 6f 20 6e 6f 74 20 65 78 edures do not ex
17989 61 63 74 6c 79 20 0a 20 20 20 20 20 20 20 20 20 actly .
1798a 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 20 77 68 ** represent wh
1798b 61 74 20 74 68 65 79 20 64 6f 2e 20 20 57 72 69 at they do. Wri
1798c 74 65 28 29 20 72 65 61 6c 6c 79 20 6d 65 61 6e te() really mean
1798d 73 20 22 70 75 74 20 74 68 69 73 20 70 61 67 65 s "put this page
1798e 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 in the.
1798f 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f ** rollback jo
17990 75 72 6e 61 6c 20 61 6e 64 20 6d 61 72 6b 20 69 urnal and mark i
17991 74 20 61 73 20 64 69 72 74 79 20 73 6f 20 74 68 t as dirty so th
17992 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 77 72 at it will be wr
17993 69 74 74 65 6e 0a 20 20 20 20 20 20 20 20 20 20 itten.
17994 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ** to the databa
17995 73 65 20 66 69 6c 65 20 6c 61 74 65 72 2e 22 20 se file later."
17996 20 44 6f 6e 74 57 72 69 74 65 28 29 20 75 6e 64 DontWrite() und
17997 6f 65 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 oes the second p
17998 61 72 74 20 6f 66 0a 20 20 20 20 20 20 20 20 20 art of.
17999 20 2a 2a 20 74 68 61 74 20 61 6e 64 20 70 72 65 ** that and pre
1799a 76 65 6e 74 73 20 74 68 65 20 70 61 67 65 20 66 vents the page f
1799b 72 6f 6d 20 62 65 69 6e 67 20 77 72 69 74 74 65 rom being writte
1799c 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 n to the databas
1799d 65 2e 20 54 68 65 0a 20 20 20 20 20 20 20 20 20 e. The.
1799e 20 2a 2a 20 70 61 67 65 20 69 73 20 73 74 69 6c ** page is stil
1799f 6c 20 6f 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 l on the rollbac
179a0 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 6f 75 67 k journal, thoug
179a1 68 2e 20 20 41 6e 64 20 74 68 61 74 20 69 73 20 h. And that is
179a2 74 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 2a the . *
179a3 2a 20 77 68 6f 6c 65 20 70 6f 69 6e 74 20 6f 66 * whole point of
179a4 20 74 68 69 73 20 62 6c 6f 63 6b 3a 20 74 6f 20 this block: to
179a5 70 75 74 20 70 61 67 65 73 20 6f 6e 20 74 68 65 put pages on the
179a6 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
179a7 6c 2e 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2f l. . */
179a8 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
179a9 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 sqlite3PagerDont
179aa 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a Write(pDbPage);.
179ab 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
179ac 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
179ad 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ref(pDbPage);.
179ae 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
179af 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74 68 /* Overwrite th
179b0 65 20 64 61 74 61 20 69 6e 20 70 61 67 65 20 69 e data in page i
179b1 20 6f 66 20 74 68 65 20 74 61 72 67 65 74 20 64 of the target d
179b2 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 69 atabase */. i
179b3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
179b4 20 26 26 20 69 21 3d 69 53 6b 69 70 20 26 26 20 && i!=iSkip &&
179b5 69 3c 3d 6e 4e 65 77 50 61 67 65 20 29 7b 0a 0a i<=nNewPage ){..
179b6 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 54 DbPage *pT
179b7 6f 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 oPage = 0;.
179b8 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 sqlite3_int64 i
179b9 4f 66 66 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d Off;.. rc =
179ba 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
179bb 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c 20 (pBtTo->pPager,
179bc 69 2c 20 26 70 54 6f 50 61 67 65 29 3b 0a 20 20 i, &pToPage);.
179bd 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
179be 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
179bf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
179c0 65 72 57 72 69 74 65 28 70 54 6f 50 61 67 65 29 erWrite(pToPage)
179c1 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
179c2 20 66 6f 72 28 0a 20 20 20 20 20 20 20 20 69 4f for(. iO
179c3 66 66 3d 28 69 2d 31 29 2a 6e 54 6f 50 61 67 65 ff=(i-1)*nToPage
179c4 53 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 72 Size; . r
179c5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
179c6 69 4f 66 66 3c 69 2a 6e 54 6f 50 61 67 65 53 69 iOff<i*nToPageSi
179c7 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 69 4f 66 ze; . iOf
179c8 66 20 2b 3d 20 6e 46 72 6f 6d 50 61 67 65 53 69 f += nFromPageSi
179c9 7a 65 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 ze. ){.
179ca 20 20 20 20 44 62 50 61 67 65 20 2a 70 46 72 6f DbPage *pFro
179cb 6d 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 mPage = 0;.
179cc 20 20 20 50 67 6e 6f 20 69 46 72 6f 6d 20 3d 20 Pgno iFrom =
179cd 28 50 67 6e 6f 29 28 69 4f 66 66 2f 6e 46 72 6f (Pgno)(iOff/nFro
179ce 6d 50 61 67 65 53 69 7a 65 29 2b 31 3b 0a 0a 20 mPageSize)+1;..
179cf 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d if( iFrom
179d0 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 ==PENDING_BYTE_P
179d1 41 47 45 28 70 42 74 46 72 6f 6d 29 20 29 7b 0a AGE(pBtFrom) ){.
179d2 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e contin
179d3 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 ue;. }..
179d4 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
179d5 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 46 te3PagerGet(pBtF
179d6 72 6f 6d 2d 3e 70 50 61 67 65 72 2c 20 69 46 72 rom->pPager, iFr
179d7 6f 6d 2c 20 26 70 46 72 6f 6d 50 61 67 65 29 3b om, &pFromPage);
179d8 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
179d9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
179da 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 char *zT
179db 6f 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 o = sqlite3Pager
179dc 47 65 74 44 61 74 61 28 70 54 6f 50 61 67 65 29 GetData(pToPage)
179dd 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 ;. char
179de 20 2a 7a 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 *zFrom = sqlite
179df 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 46 3PagerGetData(pF
179e0 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 romPage);.
179e1 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 3b 0a 0a int nCopy;..
179e2 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 46 if( nF
179e3 72 6f 6d 50 61 67 65 53 69 7a 65 3e 3d 6e 54 6f romPageSize>=nTo
179e4 50 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 PageSize ){.
179e5 20 20 20 20 20 20 20 20 7a 46 72 6f 6d 20 2b 3d zFrom +=
179e6 20 28 28 69 2d 31 29 2a 6e 54 6f 50 61 67 65 53 ((i-1)*nToPageS
179e7 69 7a 65 20 2d 20 28 28 69 46 72 6f 6d 2d 31 29 ize - ((iFrom-1)
179e8 2a 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 29 29 *nFromPageSize))
179e9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 ;. nC
179ea 6f 70 79 20 3d 20 6e 54 6f 50 61 67 65 53 69 7a opy = nToPageSiz
179eb 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c e;. }el
179ec 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
179ed 7a 54 6f 20 2b 3d 20 28 28 28 69 46 72 6f 6d 2d zTo += (((iFrom-
179ee 31 29 2a 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 1)*nFromPageSize
179ef 29 20 2d 20 28 69 2d 31 29 2a 6e 54 6f 50 61 67 ) - (i-1)*nToPag
179f0 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 eSize);.
179f1 20 20 20 20 6e 43 6f 70 79 20 3d 20 6e 46 72 6f nCopy = nFro
179f2 6d 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 mPageSize;.
179f3 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 }..
179f4 20 20 6d 65 6d 63 70 79 28 7a 54 6f 2c 20 7a 46 memcpy(zTo, zF
179f5 72 6f 6d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 rom, nCopy);.
179f6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
179f7 67 65 72 55 6e 72 65 66 28 70 46 72 6f 6d 50 61 gerUnref(pFromPa
179f8 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ge);. }.
179f9 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 }.. if
179fa 28 20 70 54 6f 50 61 67 65 20 29 7b 0a 20 20 20 ( pToPage ){.
179fb 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 20 MemPage *p
179fc 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71 6c = (MemPage *)sql
179fd 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 ite3PagerGetExtr
179fe 61 28 70 54 6f 50 61 67 65 29 3b 0a 20 20 20 20 a(pToPage);.
179ff 20 20 20 20 70 2d 3e 69 73 49 6e 69 74 20 3d 20 p->isInit =
17a00 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 0;. sqlit
17a01 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 54 6f e3PagerUnref(pTo
17a02 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 Page);. }.
17a03 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
17a04 66 20 74 68 69 6e 67 73 20 68 61 76 65 20 77 6f f things have wo
17a05 72 6b 65 64 20 73 6f 20 66 61 72 2c 20 74 68 65 rked so far, the
17a06 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d database file m
17a07 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 0a 20 ay need to be .
17a08 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e 20 54 ** truncated. T
17a09 68 65 20 63 6f 6d 70 6c 65 78 20 70 61 72 74 20 he complex part
17a0a 69 73 20 74 68 61 74 20 69 74 20 6d 61 79 20 6e is that it may n
17a0b 65 65 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61 eed to be trunca
17a0c 74 65 64 20 74 6f 0a 20 20 2a 2a 20 61 20 73 69 ted to. ** a si
17a0d 7a 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 ze that is not a
17a0e 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 n integer multip
17a0f 6c 65 20 6f 66 20 6e 54 6f 50 61 67 65 53 69 7a le of nToPageSiz
17a10 65 20 2d 20 74 68 65 20 63 75 72 72 65 6e 74 0a e - the current.
17a11 20 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 20 75 ** page size u
17a12 73 65 64 20 62 79 20 74 68 65 20 70 61 67 65 72 sed by the pager
17a13 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
17a14 20 42 2d 54 72 65 65 20 70 54 6f 2e 0a 20 20 2a B-Tree pTo.. *
17a15 2a 0a 20 20 2a 2a 20 46 6f 72 20 65 78 61 6d 70 *. ** For examp
17a16 6c 65 2c 20 73 61 79 20 74 68 65 20 70 61 67 65 le, say the page
17a17 2d 73 69 7a 65 20 6f 66 20 70 54 6f 20 69 73 20 -size of pTo is
17a18 32 30 34 38 20 62 79 74 65 73 20 61 6e 64 20 74 2048 bytes and t
17a19 68 65 20 6f 72 69 67 69 6e 61 6c 20 0a 20 20 2a he original . *
17a1a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * number of page
17a1b 73 20 69 73 20 35 20 28 31 30 20 4b 42 20 66 69 s is 5 (10 KB fi
17a1c 6c 65 29 2e 20 49 66 20 70 46 72 6f 6d 20 68 61 le). If pFrom ha
17a1d 73 20 61 20 70 61 67 65 20 73 69 7a 65 20 6f 66 s a page size of
17a1e 20 31 30 32 34 20 0a 20 20 2a 2a 20 62 79 74 65 1024 . ** byte
17a1f 73 20 61 6e 64 20 39 20 70 61 67 65 73 2c 20 74 s and 9 pages, t
17a20 68 65 6e 20 74 68 65 20 66 69 6c 65 20 6e 65 65 hen the file nee
17a21 64 73 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74 ds to be truncat
17a22 65 64 20 74 6f 20 39 4b 42 2e 0a 20 20 2a 2f 0a ed to 9KB.. */.
17a23 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
17a24 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
17a25 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 20 3d e3_file *pFile =
17a26 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c sqlite3PagerFil
17a27 65 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 29 e(pBtTo->pPager)
17a28 3b 0a 20 20 20 20 69 36 34 20 69 53 69 7a 65 20 ;. i64 iSize
17a29 3d 20 28 69 36 34 29 6e 46 72 6f 6d 50 61 67 65 = (i64)nFromPage
17a2a 53 69 7a 65 20 2a 20 28 69 36 34 29 6e 46 72 6f Size * (i64)nFro
17a2b 6d 50 61 67 65 3b 0a 20 20 20 20 69 36 34 20 69 mPage;. i64 i
17a2c 4e 6f 77 20 3d 20 28 69 36 34 29 28 28 6e 54 6f Now = (i64)((nTo
17a2d 50 61 67 65 3e 6e 4e 65 77 50 61 67 65 29 3f 6e Page>nNewPage)?n
17a2e 54 6f 50 61 67 65 3a 6e 4e 65 77 50 61 67 65 29 ToPage:nNewPage)
17a2f 20 2a 20 28 69 36 34 29 6e 54 6f 50 61 67 65 53 * (i64)nToPageS
17a30 69 7a 65 3b 20 0a 20 20 20 20 69 36 34 20 69 50 ize; . i64 iP
17a31 65 6e 64 69 6e 67 20 3d 20 28 28 69 36 34 29 50 ending = ((i64)P
17a32 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
17a33 28 70 42 74 54 6f 29 2d 31 29 20 2a 28 69 36 34 (pBtTo)-1) *(i64
17a34 29 6e 54 6f 50 61 67 65 53 69 7a 65 3b 0a 0a 20 )nToPageSize;..
17a35 20 20 20 61 73 73 65 72 74 28 20 69 53 69 7a 65 assert( iSize
17a36 3c 3d 69 4e 6f 77 20 29 3b 0a 0a 20 20 20 20 2f <=iNow );.. /
17a37 2a 20 43 6f 6d 6d 69 74 20 70 68 61 73 65 20 6f * Commit phase o
17a38 6e 65 20 73 79 6e 63 73 20 74 68 65 20 6a 6f 75 ne syncs the jou
17a39 72 6e 61 6c 20 66 69 6c 65 20 61 73 73 6f 63 69 rnal file associ
17a3a 61 74 65 64 20 77 69 74 68 20 70 54 6f 20 0a 20 ated with pTo .
17a3b 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 ** containing
17a3c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 the original da
17a3d 74 61 2e 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 ta. It does not
17a3e 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 sync the databas
17a3f 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 69 74 e file. ** it
17a40 73 65 6c 66 2e 20 41 66 74 65 72 20 64 6f 69 6e self. After doin
17a41 67 20 74 68 69 73 20 69 74 20 69 73 20 73 61 66 g this it is saf
17a42 65 20 74 6f 20 75 73 65 20 4f 73 54 72 75 6e 63 e to use OsTrunc
17a43 61 74 65 28 29 20 61 6e 64 20 6f 74 68 65 72 0a ate() and other.
17a44 20 20 20 20 2a 2a 20 66 69 6c 65 20 41 50 49 73 ** file APIs
17a45 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
17a46 20 66 69 6c 65 20 64 69 72 65 63 74 6c 79 2e 0a file directly..
17a47 20 20 20 20 2a 2f 0a 20 20 20 20 70 42 74 54 6f */. pBtTo
17a48 2d 3e 64 62 20 3d 20 70 54 6f 2d 3e 64 62 3b 0a ->db = pTo->db;.
17a49 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
17a4a 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
17a4b 4f 6e 65 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 One(pBtTo->pPage
17a4c 72 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20 69 66 r, 0, 1);. if
17a4d 28 20 69 53 69 7a 65 3c 69 4e 6f 77 20 26 26 20 ( iSize<iNow &&
17a4e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
17a4f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
17a50 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 46 te3OsTruncate(pF
17a51 69 6c 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 20 ile, iSize);.
17a52 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6c }.. /* The l
17a53 6f 6f 70 20 74 68 61 74 20 63 6f 70 69 65 64 20 oop that copied
17a54 64 61 74 61 20 66 72 6f 6d 20 64 61 74 61 62 61 data from databa
17a55 73 65 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 20 se pFrom to pTo
17a56 64 69 64 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 70 did not. ** p
17a57 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 63 6b opulate the lock
17a58 69 6e 67 20 70 61 67 65 20 6f 66 20 64 61 74 61 ing page of data
17a59 62 61 73 65 20 70 54 6f 2e 20 49 66 20 74 68 65 base pTo. If the
17a5a 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 0a 20 20 page-size of.
17a5b 20 20 2a 2a 20 70 46 72 6f 6d 20 69 73 20 73 6d ** pFrom is sm
17a5c 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 61 74 20 aller than that
17a5d 6f 66 20 70 54 6f 2c 20 74 68 69 73 20 6d 65 61 of pTo, this mea
17a5e 6e 73 20 73 6f 6d 65 20 64 61 74 61 20 77 69 6c ns some data wil
17a5f 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61 76 l. ** not hav
17a60 65 20 62 65 65 6e 20 63 6f 70 69 65 64 2e 20 0a e been copied. .
17a61 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
17a62 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20 is block copies
17a63 74 68 65 20 6d 69 73 73 69 6e 67 20 64 61 74 61 the missing data
17a64 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 70 from database p
17a65 46 72 6f 6d 20 74 6f 20 70 54 6f 20 0a 20 20 20 From to pTo .
17a66 20 2a 2a 20 75 73 69 6e 67 20 66 69 6c 65 20 41 ** using file A
17a67 50 49 73 2e 20 54 68 69 73 20 69 73 20 73 61 66 PIs. This is saf
17a68 65 20 62 65 63 61 75 73 65 20 61 74 20 74 68 69 e because at thi
17a69 73 20 70 6f 69 6e 74 20 77 65 20 6b 6e 6f 77 20 s point we know
17a6a 74 68 61 74 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 that. ** all
17a6b 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 of the original
17a6c 64 61 74 61 20 66 72 6f 6d 20 70 54 6f 20 68 61 data from pTo ha
17a6d 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 69 6e s been synced in
17a6e 74 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 6a to the . ** j
17a6f 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 74 20 ournal file. At
17a70 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20 77 6f this point it wo
17a71 75 6c 64 20 62 65 20 73 61 66 65 20 74 6f 20 64 uld be safe to d
17a72 6f 20 61 6e 79 74 68 69 6e 67 20 61 74 0a 20 20 o anything at.
17a73 20 20 2a 2a 20 61 6c 6c 20 74 6f 20 74 68 65 20 ** all to the
17a74 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 65 78 database file ex
17a75 63 65 70 74 20 74 72 75 6e 63 61 74 65 20 69 74 cept truncate it
17a76 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 2e 0a to zero bytes..
17a77 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 */. if( r
17a78 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
17a79 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 3c 6e 54 nFromPageSize<nT
17a7a 6f 50 61 67 65 53 69 7a 65 20 26 26 20 69 53 69 oPageSize && iSi
17a7b 7a 65 3e 69 50 65 6e 64 69 6e 67 29 7b 0a 20 20 ze>iPending){.
17a7c 20 20 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 20 i64 iOff;.
17a7d 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20 20 for(.
17a7e 20 69 4f 66 66 3d 69 50 65 6e 64 69 6e 67 3b 20 iOff=iPending;
17a7f 0a 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c . rc==SQL
17a80 49 54 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 28 ITE_OK && iOff<(
17a81 69 50 65 6e 64 69 6e 67 2b 6e 54 6f 50 61 67 65 iPending+nToPage
17a82 53 69 7a 65 29 3b 20 0a 20 20 20 20 20 20 20 20 Size); .
17a83 69 4f 66 66 20 2b 3d 20 6e 46 72 6f 6d 50 61 67 iOff += nFromPag
17a84 65 53 69 7a 65 0a 20 20 20 20 20 20 29 7b 0a 20 eSize. ){.
17a85 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 DbPage *p
17a86 46 72 6f 6d 50 61 67 65 20 3d 20 30 3b 0a 20 20 FromPage = 0;.
17a87 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 6f 6d Pgno iFrom
17a88 20 3d 20 28 50 67 6e 6f 29 28 69 4f 66 66 2f 6e = (Pgno)(iOff/n
17a89 46 72 6f 6d 50 61 67 65 53 69 7a 65 29 2b 31 3b FromPageSize)+1;
17a8a 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 46 .. if( iF
17a8b 72 6f 6d 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 rom==PENDING_BYT
17a8c 45 5f 50 41 47 45 28 70 42 74 46 72 6f 6d 29 20 E_PAGE(pBtFrom)
17a8d 7c 7c 20 69 46 72 6f 6d 3e 6e 46 72 6f 6d 50 61 || iFrom>nFromPa
17a8e 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ge ){.
17a8f 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
17a90 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20 }.. rc
17a91 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
17a92 74 28 70 42 74 46 72 6f 6d 2d 3e 70 50 61 67 65 t(pBtFrom->pPage
17a93 72 2c 20 69 46 72 6f 6d 2c 20 26 70 46 72 6f 6d r, iFrom, &pFrom
17a94 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 Page);. i
17a95 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
17a96 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68 ){. ch
17a97 61 72 20 2a 7a 46 72 6f 6d 20 3d 20 73 71 6c 69 ar *zFrom = sqli
17a98 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
17a99 70 46 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20 pFromPage);.
17a9a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
17a9b 65 33 4f 73 57 72 69 74 65 28 70 46 69 6c 65 2c e3OsWrite(pFile,
17a9c 20 7a 46 72 6f 6d 2c 20 6e 46 72 6f 6d 50 61 67 zFrom, nFromPag
17a9d 65 53 69 7a 65 2c 20 69 4f 66 66 29 3b 0a 20 20 eSize, iOff);.
17a9e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
17a9f 61 67 65 72 55 6e 72 65 66 28 70 46 72 6f 6d 50 agerUnref(pFromP
17aa0 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a age);. }.
17aa1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
17aa2 7d 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 }.. /* Sync the
17aa3 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
17aa4 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
17aa5 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
17aa6 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 = sqlite3PagerSy
17aa7 6e 63 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 nc(pBtTo->pPager
17aa8 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d );. }. if( rc=
17aa9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17aaa 20 20 70 42 74 54 6f 2d 3e 70 61 67 65 53 69 7a pBtTo->pageSiz
17aab 65 46 69 78 65 64 20 3d 20 30 3b 0a 20 20 7d 65 eFixed = 0;. }e
17aac 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
17aad 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 54 BtreeRollback(pT
17aae 6f 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 o);. }.. retur
17aaf 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 n rc;.}.SQLITE_P
17ab0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
17ab1 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 e3BtreeCopyFile(
17ab2 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 74 72 65 Btree *pTo, Btre
17ab3 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 e *pFrom){. int
17ab4 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
17ab5 72 65 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a 20 reeEnter(pTo);.
17ab6 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
17ab7 65 72 28 70 46 72 6f 6d 29 3b 0a 20 20 72 63 20 er(pFrom);. rc
17ab8 3d 20 62 74 72 65 65 43 6f 70 79 46 69 6c 65 28 = btreeCopyFile(
17ab9 70 54 6f 2c 20 70 46 72 6f 6d 29 3b 0a 20 20 73 pTo, pFrom);. s
17aba 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
17abb 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74 (pFrom);. sqlit
17abc 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f e3BtreeLeave(pTo
17abd 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
17abe 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
17abf 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 ITE_OMIT_VACUUM
17ac0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e */../*.** Return
17ac1 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 74 non-zero if a t
17ac2 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
17ac3 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tive..*/.SQLITE_
17ac4 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
17ac5 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e te3BtreeIsInTran
17ac6 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 s(Btree *p){. a
17ac7 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 73 ssert( p==0 || s
17ac8 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
17ac9 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
17aca 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 );. return (p &
17acb 26 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 & (p->inTrans==T
17acc 52 41 4e 53 5f 57 52 49 54 45 29 29 3b 0a 7d 0a RANS_WRITE));.}.
17acd 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f ./*.** Return no
17ace 6e 2d 7a 65 72 6f 20 69 66 20 61 20 73 74 61 74 n-zero if a stat
17acf 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
17ad0 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a n is active..*/.
17ad1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
17ad2 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
17ad3 73 49 6e 53 74 6d 74 28 42 74 72 65 65 20 2a 70 sInStmt(Btree *p
17ad4 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
17ad5 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
17ad6 74 65 78 28 70 29 20 29 3b 0a 20 20 72 65 74 75 tex(p) );. retu
17ad7 72 6e 20 41 4c 57 41 59 53 28 70 2d 3e 70 42 74 rn ALWAYS(p->pBt
17ad8 29 20 26 26 20 70 2d 3e 70 42 74 2d 3e 69 6e 53 ) && p->pBt->inS
17ad9 74 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 tmt;.}../*.** Re
17ada 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 turn non-zero if
17adb 20 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 a read (or writ
17adc 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 e) transaction i
17add 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c s active..*/.SQL
17ade 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
17adf 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e sqlite3BtreeIsIn
17ae0 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 ReadTrans(Btree
17ae1 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
17ae2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
17ae3 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
17ae4 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
17ae5 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e ;. return p->in
17ae6 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e Trans!=TRANS_NON
17ae7 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 E;.}../*.** This
17ae8 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
17ae9 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
17aea 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 blob of memory
17aeb 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a associated with.
17aec 2a 2a 20 61 20 73 69 6e 67 6c 65 20 73 68 61 72 ** a single shar
17aed 65 64 2d 62 74 72 65 65 2e 20 54 68 65 20 6d 65 ed-btree. The me
17aee 6d 6f 72 79 20 69 73 20 75 73 65 64 20 62 79 20 mory is used by
17aef 63 6c 69 65 6e 74 20 63 6f 64 65 20 66 6f 72 20 client code for
17af0 69 74 73 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f its own.** purpo
17af1 73 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 ses (for example
17af2 2c 20 74 6f 20 73 74 6f 72 65 20 61 20 68 69 67 , to store a hig
17af3 68 2d 6c 65 76 65 6c 20 73 63 68 65 6d 61 20 61 h-level schema a
17af4 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a ssociated with .
17af5 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 ** the shared-bt
17af6 72 65 65 29 2e 20 54 68 65 20 62 74 72 65 65 20 ree). The btree
17af7 6c 61 79 65 72 20 6d 61 6e 61 67 65 73 20 72 65 layer manages re
17af8 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 ference counting
17af9 20 69 73 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 issues..**.** T
17afa 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 he first time th
17afb 69 73 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 is is called on
17afc 61 20 73 68 61 72 65 64 2d 62 74 72 65 65 2c 20 a shared-btree,
17afd 6e 42 79 74 65 73 20 62 79 74 65 73 20 6f 66 20 nBytes bytes of
17afe 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 6c memory.** are al
17aff 6c 6f 63 61 74 65 64 2c 20 7a 65 72 6f 65 64 2c located, zeroed,
17b00 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 74 6f and returned to
17b01 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 46 6f 72 the caller. For
17b02 20 65 61 63 68 20 73 75 62 73 65 71 75 65 6e 74 each subsequent
17b03 20 0a 2a 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42 .** call the nB
17b04 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69 ytes parameter i
17b05 73 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 61 20 s ignored and a
17b06 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 pointer to the s
17b07 61 6d 65 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d ame blob.** of m
17b08 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 2e 20 emory returned.
17b09 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 .**.** If the nB
17b0a 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69 ytes parameter i
17b0b 73 20 30 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 s 0 and the blob
17b0c 20 6f 66 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e of memory has n
17b0d 6f 74 20 79 65 74 20 62 65 65 6e 0a 2a 2a 20 61 ot yet been.** a
17b0e 6c 6c 6f 63 61 74 65 64 2c 20 61 20 6e 75 6c 6c llocated, a null
17b0f 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 pointer is retu
17b10 72 6e 65 64 2e 20 49 66 20 74 68 65 20 62 6c 6f rned. If the blo
17b11 62 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 b has already be
17b12 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c en.** allocated,
17b13 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 it is returned
17b14 61 73 20 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a as normal..**.**
17b15 20 4a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 Just before the
17b16 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 69 73 shared-btree is
17b17 20 63 6c 6f 73 65 64 2c 20 74 68 65 20 66 75 6e closed, the fun
17b18 63 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20 ction passed as
17b19 74 68 65 20 0a 2a 2a 20 78 46 72 65 65 20 61 72 the .** xFree ar
17b1a 67 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 gument when the
17b1b 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
17b1c 6e 20 77 61 73 20 6d 61 64 65 20 69 73 20 69 6e n was made is in
17b1d 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a voked on the .**
17b1e 20 62 6c 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74 blob of allocat
17b1f 65 64 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20 ed memory. This
17b20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
17b21 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 not call sqlite3
17b22 5f 66 72 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 _free().** on th
17b23 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 62 74 e memory, the bt
17b24 72 65 65 20 6c 61 79 65 72 20 64 6f 65 73 20 74 ree layer does t
17b25 68 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 hat..*/.SQLITE_P
17b26 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
17b27 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 ite3BtreeSchema(
17b28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42 Btree *p, int nB
17b29 79 74 65 73 2c 20 76 6f 69 64 28 2a 78 46 72 65 ytes, void(*xFre
17b2a 65 29 28 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42 e)(void *)){. B
17b2b 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
17b2c 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 ->pBt;. sqlite3
17b2d 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
17b2e 20 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68 65 if( !pBt->pSche
17b2f 6d 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b 0a ma && nBytes ){.
17b30 20 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 pBt->pSchema
17b31 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
17b32 5a 65 72 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20 Zero(nBytes);.
17b33 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 pBt->xFreeSche
17b34 6d 61 20 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a ma = xFree;. }.
17b35 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
17b36 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
17b37 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d pBt->pSchema;.}
17b38 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
17b39 72 75 65 20 69 66 20 61 6e 6f 74 68 65 72 20 75 rue if another u
17b3a 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20 ser of the same
17b3b 73 68 61 72 65 64 20 62 74 72 65 65 20 61 73 20 shared btree as
17b3c 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 the argument.**
17b3d 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61 6e 20 handle holds an
17b3e 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f exclusive lock o
17b3f 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 n the sqlite_mas
17b40 74 65 72 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 ter table..*/.SQ
17b41 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
17b42 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 sqlite3BtreeSch
17b43 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 emaLocked(Btree
17b44 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 *p){. int rc;.
17b45 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17b46 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 _mutex_held(p->d
17b47 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 b->mutex) );. s
17b48 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
17b49 28 70 29 3b 0a 20 20 72 63 20 3d 20 28 71 75 65 (p);. rc = (que
17b4a 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d ryTableLock(p, M
17b4b 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44 ASTER_ROOT, READ
17b4c 5f 4c 4f 43 4b 29 21 3d 53 51 4c 49 54 45 5f 4f _LOCK)!=SQLITE_O
17b4d 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 K);. sqlite3Btr
17b4e 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
17b4f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69 66 turn rc;.}...#if
17b50 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
17b51 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a _SHARED_CACHE./*
17b52 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 .** Obtain a loc
17b53 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 k on the table w
17b54 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 hose root page i
17b55 73 20 69 54 61 62 2e 20 20 54 68 65 0a 2a 2a 20 s iTab. The.**
17b56 6c 6f 63 6b 20 69 73 20 61 20 77 72 69 74 65 20 lock is a write
17b57 6c 6f 63 6b 20 69 66 20 69 73 57 72 69 74 65 6c lock if isWritel
17b58 6f 63 6b 20 69 73 20 74 72 75 65 20 6f 72 20 61 ock is true or a
17b59 20 72 65 61 64 20 6c 6f 63 6b 0a 2a 2a 20 69 66 read lock.** if
17b5a 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2f it is false..*/
17b5b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
17b5c 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
17b5d 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20 LockTable(Btree
17b5e 2a 70 2c 20 69 6e 74 20 69 54 61 62 2c 20 75 38 *p, int iTab, u8
17b5f 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 7b 0a 20 isWriteLock){.
17b60 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
17b61 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 73 68 _OK;. if( p->sh
17b62 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 75 38 arable ){. u8
17b63 20 6c 6f 63 6b 54 79 70 65 20 3d 20 52 45 41 44 lockType = READ
17b64 5f 4c 4f 43 4b 20 2b 20 69 73 57 72 69 74 65 4c _LOCK + isWriteL
17b65 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ock;. assert(
17b66 20 52 45 41 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52 READ_LOCK+1==WR
17b67 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 ITE_LOCK );.
17b68 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65 4c assert( isWriteL
17b69 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 ock==0 || isWrit
17b6a 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 eLock==1 );.
17b6b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
17b6c 72 28 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 71 r(p);. rc = q
17b6d 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c ueryTableLock(p,
17b6e 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29 iTab, lockType)
17b6f 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
17b70 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17b71 20 72 63 20 3d 20 6c 6f 63 6b 54 61 62 6c 65 28 rc = lockTable(
17b72 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 p, iTab, lockTyp
17b73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 e);. }. sq
17b74 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
17b75 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e p);. }. return
17b76 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 rc;.}.#endif..#
17b77 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
17b78 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a IT_INCRBLOB./*.*
17b79 2a 20 41 72 67 75 6d 65 6e 74 20 70 43 73 72 20 * Argument pCsr
17b7a 6d 75 73 74 20 62 65 20 61 20 63 75 72 73 6f 72 must be a cursor
17b7b 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 opened for writ
17b7c 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e ing on an .** IN
17b7d 54 4b 45 59 20 74 61 62 6c 65 20 63 75 72 72 65 TKEY table curre
17b7e 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 ntly pointing at
17b7f 20 61 20 76 61 6c 69 64 20 74 61 62 6c 65 20 65 a valid table e
17b80 6e 74 72 79 2e 20 0a 2a 2a 20 54 68 69 73 20 66 ntry. .** This f
17b81 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73 unction modifies
17b82 20 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 the data stored
17b83 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 61 74 as part of that
17b84 20 65 6e 74 72 79 2e 0a 2a 2a 20 4f 6e 6c 79 20 entry..** Only
17b85 74 68 65 20 64 61 74 61 20 63 6f 6e 74 65 6e 74 the data content
17b86 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6d 6f 64 may only be mod
17b87 69 66 69 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 ified, it is not
17b88 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 possible.** to
17b89 63 68 61 6e 67 65 20 74 68 65 20 6c 65 6e 67 74 change the lengt
17b8a 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 73 74 h of the data st
17b8b 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ored..*/.SQLITE_
17b8c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
17b8d 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 te3BtreePutData(
17b8e 42 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 BtCursor *pCsr,
17b8f 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
17b90 61 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 amt, void *z){.
17b91 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
17b92 6f 6c 64 73 4d 75 74 65 78 28 70 43 73 72 29 20 oldsMutex(pCsr)
17b93 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
17b94 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
17b95 70 43 73 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCsr->pBtree->db
17b96 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
17b97 73 65 72 74 28 70 43 73 72 2d 3e 69 73 49 6e 63 sert(pCsr->isInc
17b98 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 0a 20 rblobHandle);..
17b99 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
17b9a 73 69 74 69 6f 6e 28 70 43 73 72 29 3b 0a 20 20 sition(pCsr);.
17b9b 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e 65 53 assert( pCsr->eS
17b9c 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 52 45 51 tate!=CURSOR_REQ
17b9d 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 UIRESEEK );. if
17b9e 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d ( pCsr->eState!=
17b9f 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a CURSOR_VALID ){.
17ba0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
17ba1 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 0a 20 20 E_ABORT;. }..
17ba2 2f 2a 20 43 68 65 63 6b 20 73 6f 6d 65 20 70 72 /* Check some pr
17ba3 65 63 6f 6e 64 69 74 69 6f 6e 73 3a 20 0a 20 20 econditions: .
17ba4 2a 2a 20 20 20 28 61 29 20 74 68 65 20 63 75 72 ** (a) the cur
17ba5 73 6f 72 20 69 73 20 6f 70 65 6e 20 66 6f 72 20 sor is open for
17ba6 77 72 69 74 69 6e 67 2c 0a 20 20 2a 2a 20 20 20 writing,. **
17ba7 28 62 29 20 74 68 65 72 65 20 69 73 20 6e 6f 20 (b) there is no
17ba8 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 read-lock on the
17ba9 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6d 6f 64 table being mod
17baa 69 66 69 65 64 20 61 6e 64 0a 20 20 2a 2a 20 20 ified and. **
17bab 20 28 63 29 20 74 68 65 20 63 75 72 73 6f 72 20 (c) the cursor
17bac 70 6f 69 6e 74 73 20 61 74 20 61 20 76 61 6c 69 points at a vali
17bad 64 20 72 6f 77 20 6f 66 20 61 6e 20 69 6e 74 4b d row of an intK
17bae 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 ey table.. */.
17baf 20 69 66 28 20 21 70 43 73 72 2d 3e 77 72 46 6c if( !pCsr->wrFl
17bb0 61 67 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ag ){. return
17bb1 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 SQLITE_READONLY
17bb2 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
17bb3 21 70 43 73 72 2d 3e 70 42 74 2d 3e 72 65 61 64 !pCsr->pBt->read
17bb4 4f 6e 6c 79 20 0a 20 20 20 20 20 20 20 20 20 20 Only .
17bb5 26 26 20 70 43 73 72 2d 3e 70 42 74 2d 3e 69 6e && pCsr->pBt->in
17bb6 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
17bb7 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 NS_WRITE );. if
17bb8 28 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 ( checkReadLocks
17bb9 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2c 20 70 (pCsr->pBtree, p
17bba 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 Csr->pgnoRoot, p
17bbb 43 73 72 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 Csr, 0) ){. r
17bbc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 eturn SQLITE_LOC
17bbd 4b 45 44 3b 20 2f 2a 20 54 68 65 20 74 61 62 6c KED; /* The tabl
17bbe 65 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f e pCur points to
17bbf 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b has a read lock
17bc0 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 */. }. if( pC
17bc1 73 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 sr->eState==CURS
17bc2 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c 20 21 70 OR_INVALID || !p
17bc3 43 73 72 2d 3e 61 70 50 61 67 65 5b 70 43 73 72 Csr->apPage[pCsr
17bc4 2d 3e 69 50 61 67 65 5d 2d 3e 69 6e 74 4b 65 79 ->iPage]->intKey
17bc5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
17bc6 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
17bc7 0a 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 .. return acces
17bc8 73 50 61 79 6c 6f 61 64 28 70 43 73 72 2c 20 6f sPayload(pCsr, o
17bc9 66 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 ffset, amt, (uns
17bca 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 2c 20 igned char *)z,
17bcb 30 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 0, 1);.}../* .**
17bcc 20 53 65 74 20 61 20 66 6c 61 67 20 6f 6e 20 74 Set a flag on t
17bcd 68 69 73 20 63 75 72 73 6f 72 20 74 6f 20 63 61 his cursor to ca
17bce 63 68 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e che the location
17bcf 73 20 6f 66 20 70 61 67 65 73 20 66 72 6f 6d 20 s of pages from
17bd0 74 68 65 20 0a 2a 2a 20 6f 76 65 72 66 6c 6f 77 the .** overflow
17bd1 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 63 75 list for the cu
17bd2 72 72 65 6e 74 20 72 6f 77 2e 20 54 68 69 73 20 rrent row. This
17bd3 69 73 20 75 73 65 64 20 62 79 20 63 75 72 73 6f is used by curso
17bd4 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 rs opened.** for
17bd5 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f incremental blo
17bd6 62 20 49 4f 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a b IO only..**.**
17bd7 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 This function s
17bd8 65 74 73 20 61 20 66 6c 61 67 20 6f 6e 6c 79 2e ets a flag only.
17bd9 20 54 68 65 20 61 63 74 75 61 6c 20 70 61 67 65 The actual page
17bda 20 6c 6f 63 61 74 69 6f 6e 20 63 61 63 68 65 0a location cache.
17bdb 2a 2a 20 28 73 74 6f 72 65 64 20 69 6e 20 42 74 ** (stored in Bt
17bdc 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 Cursor.aOverflow
17bdd 5b 5d 29 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 []) is allocated
17bde 20 61 6e 64 20 75 73 65 64 20 62 79 20 66 75 6e and used by fun
17bdf 63 74 69 6f 6e 0a 2a 2a 20 61 63 63 65 73 73 50 ction.** accessP
17be0 61 79 6c 6f 61 64 28 29 20 28 74 68 65 20 77 6f ayload() (the wo
17be1 72 6b 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f rker function fo
17be2 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 r sqlite3BtreeDa
17be3 74 61 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 ta() and.** sqli
17be4 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 te3BtreePutData(
17be5 29 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ))..*/.SQLITE_PR
17be6 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
17be7 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 e3BtreeCacheOver
17be8 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 70 flow(BtCursor *p
17be9 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Cur){. assert(
17bea 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
17beb 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
17bec 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
17bed 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 x_held(pCur->pBt
17bee 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 ree->db->mutex)
17bef 29 3b 0a 20 20 61 73 73 65 72 74 28 21 70 43 75 );. assert(!pCu
17bf0 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e r->isIncrblobHan
17bf1 64 6c 65 29 3b 0a 20 20 61 73 73 65 72 74 28 21 dle);. assert(!
17bf2 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 pCur->aOverflow)
17bf3 3b 0a 20 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 ;. pCur->isIncr
17bf4 62 6c 6f 62 48 61 6e 64 6c 65 20 3d 20 31 3b 0a blobHandle = 1;.
17bf5 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a }.#endif../*****
17bf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
17bf7 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a btree.c *******
17bf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17bf9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17bfa 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
17bfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
17bfc 66 69 6c 65 20 76 64 62 65 6d 65 6d 2e 63 20 2a file vdbemem.c *
17bfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17bfe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17bff 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
17c00 32 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2004 May 26.**.*
17c01 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
17c02 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
17c03 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
17c04 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
17c05 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
17c06 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
17c07 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
17c08 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
17c09 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
17c0a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
17c0b 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
17c0c 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
17c0d 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
17c0e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
17c0f 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
17c10 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
17c11 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
17c12 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
17c13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17c14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17c15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17c16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
17c17 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
17c18 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 ins code use to
17c19 6d 61 6e 69 70 75 6c 61 74 65 20 22 4d 65 6d 22 manipulate "Mem"
17c1a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 20 22 structure. A "
17c1b 4d 65 6d 22 0a 2a 2a 20 73 74 6f 72 65 73 20 61 Mem".** stores a
17c1c 20 73 69 6e 67 6c 65 20 76 61 6c 75 65 20 69 6e single value in
17c1d 20 74 68 65 20 56 44 42 45 2e 20 20 4d 65 6d 20 the VDBE. Mem
17c1e 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 is an opaque str
17c1f 75 63 74 75 72 65 20 76 69 73 69 62 6c 65 0a 2a ucture visible.*
17c20 2a 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 74 68 * only within th
17c21 65 20 56 44 42 45 2e 20 20 49 6e 74 65 72 66 61 e VDBE. Interfa
17c22 63 65 20 72 6f 75 74 69 6e 65 73 20 72 65 66 65 ce routines refe
17c23 72 20 74 6f 20 61 20 4d 65 6d 20 75 73 69 6e 67 r to a Mem using
17c24 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 73 71 6c the.** name sql
17c25 69 74 65 5f 76 61 6c 75 65 0a 2a 2a 0a 2a 2a 20 ite_value.**.**
17c26 24 49 64 3a 20 76 64 62 65 6d 65 6d 2e 63 2c 76 $Id: vdbemem.c,v
17c27 20 31 2e 31 33 34 20 32 30 30 39 2f 30 31 2f 30 1.134 2009/01/0
17c28 35 20 32 32 3a 33 30 3a 33 39 20 64 72 68 20 45 5 22:30:39 drh E
17c29 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 xp $.*/../*.** C
17c2a 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65 4d all sqlite3VdbeM
17c2b 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 20 6f emExpandBlob() o
17c2c 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 76 n the supplied v
17c2d 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d 2a 29 alue (type Mem*)
17c2e 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69 72 65 .** P if require
17c2f 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 65 78 d..*/.#define ex
17c30 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 28 50 pandBlob(P) (((P
17c31 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 )->flags&MEM_Zer
17c32 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 o)?sqlite3VdbeMe
17c33 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 3a 30 mExpandBlob(P):0
17c34 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d )../*.** If pMem
17c35 20 69 73 20 61 6e 20 6f 62 6a 65 63 74 20 77 69 is an object wi
17c36 74 68 20 61 20 76 61 6c 69 64 20 73 74 72 69 6e th a valid strin
17c37 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e g representation
17c38 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a , this routine.*
17c39 2a 20 65 6e 73 75 72 65 73 20 74 68 65 20 69 6e * ensures the in
17c3a 74 65 72 6e 61 6c 20 65 6e 63 6f 64 69 6e 67 20 ternal encoding
17c3b 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 72 for the string r
17c3c 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 epresentation is
17c3d 0a 2a 2a 20 27 64 65 73 69 72 65 64 45 6e 63 27 .** 'desiredEnc'
17c3e 2c 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f , one of SQLITE_
17c3f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 UTF8, SQLITE_UTF
17c40 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f 55 16LE or SQLITE_U
17c41 54 46 31 36 42 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 TF16BE..**.** If
17c42 20 70 4d 65 6d 20 69 73 20 6e 6f 74 20 61 20 73 pMem is not a s
17c43 74 72 69 6e 67 20 6f 62 6a 65 63 74 2c 20 6f 72 tring object, or
17c44 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 the encoding of
17c45 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 72 the string.** r
17c46 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 epresentation is
17c47 20 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20 already stored
17c48 75 73 69 6e 67 20 74 68 65 20 72 65 71 75 65 73 using the reques
17c49 74 65 64 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 ted encoding, th
17c4a 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 en this.** routi
17c4b 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ne is a no-op..*
17c4c 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 *.** SQLITE_OK i
17c4d 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 s returned if th
17c4e 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 e conversion is
17c4f 73 75 63 63 65 73 73 66 75 6c 20 28 6f 72 20 6e successful (or n
17c50 6f 74 20 72 65 71 75 69 72 65 64 29 2e 0a 2a 2a ot required)..**
17c51 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61 SQLITE_NOMEM ma
17c52 79 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 y be returned if
17c53 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c a malloc() fail
17c54 73 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 s during convers
17c55 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 66 ion.** between f
17c56 6f 72 6d 61 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ormats..*/.SQLIT
17c57 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
17c58 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
17c59 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a 70 4d 65 ncoding(Mem *pMe
17c5a 6d 2c 20 69 6e 74 20 64 65 73 69 72 65 64 45 6e m, int desiredEn
17c5b 63 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 c){. int rc;.
17c5c 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 assert( (pMem->f
17c5d 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 lags&MEM_RowSet)
17c5e 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
17c5f 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c desiredEnc==SQL
17c60 49 54 45 5f 55 54 46 38 20 7c 7c 20 64 65 73 69 ITE_UTF8 || desi
17c61 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 redEnc==SQLITE_U
17c62 54 46 31 36 4c 45 0a 20 20 20 20 20 20 20 20 20 TF16LE.
17c63 20 20 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d || desiredEnc=
17c64 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 =SQLITE_UTF16BE
17c65 29 3b 0a 20 20 69 66 28 20 21 28 70 4d 65 6d 2d );. if( !(pMem-
17c66 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 >flags&MEM_Str)
17c67 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 || pMem->enc==de
17c68 73 69 72 65 64 45 6e 63 20 29 7b 0a 20 20 20 20 siredEnc ){.
17c69 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
17c6a 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
17c6b 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
17c6c 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
17c6d 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
17c6e 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c x) );.#ifdef SQL
17c6f 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 ITE_OMIT_UTF16.
17c70 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
17c71 52 52 4f 52 3b 0a 23 65 6c 73 65 0a 0a 20 20 2f RROR;.#else.. /
17c72 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 * MemTranslate()
17c73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 may return SQLI
17c74 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 45 5f TE_OK or SQLITE_
17c75 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f 4d 45 4d 20 NOMEM. If NOMEM
17c76 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 20 20 2a is returned,. *
17c77 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 63 6f 64 * then the encod
17c78 69 6e 67 20 6f 66 20 74 68 65 20 76 61 6c 75 65 ing of the value
17c79 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 63 68 may not have ch
17c7a 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 anged.. */. rc
17c7b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
17c7c 6d 54 72 61 6e 73 6c 61 74 65 28 70 4d 65 6d 2c mTranslate(pMem,
17c7d 20 28 75 38 29 64 65 73 69 72 65 64 45 6e 63 29 (u8)desiredEnc)
17c7e 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 ;. assert(rc==S
17c7f 51 4c 49 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 QLITE_OK || r
17c80 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 c==SQLITE_NOMEM)
17c81 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 ;. assert(rc==S
17c82 51 4c 49 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 QLITE_OK || p
17c83 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 Mem->enc!=desire
17c84 64 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28 dEnc);. assert(
17c85 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
17c86 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 || pMem->enc==d
17c87 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 72 65 esiredEnc);. re
17c88 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a turn rc;.#endif.
17c89 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 }../*.** Make su
17c8a 72 65 20 70 4d 65 6d 2d 3e 7a 20 70 6f 69 6e 74 re pMem->z point
17c8b 73 20 74 6f 20 61 20 77 72 69 74 61 62 6c 65 20 s to a writable
17c8c 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 74 allocation of at
17c8d 20 6c 65 61 73 74 20 0a 2a 2a 20 6e 20 62 79 74 least .** n byt
17c8e 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 es..**.** If the
17c8f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 75 72 memory cell cur
17c90 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20 rently contains
17c91 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 64 string or blob d
17c92 61 74 61 0a 2a 2a 20 61 6e 64 20 74 68 65 20 74 ata.** and the t
17c93 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 70 61 hird argument pa
17c94 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e ssed to this fun
17c95 63 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 74 ction is true, t
17c96 68 65 20 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63 he .** current c
17c97 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 ontent of the ce
17c98 6c 6c 20 69 73 20 70 72 65 73 65 72 76 65 64 2e ll is preserved.
17c99 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d Otherwise, it m
17c9a 61 79 0a 2a 2a 20 62 65 20 64 69 73 63 61 72 64 ay.** be discard
17c9b 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ed. .**.** This
17c9c 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 function sets t
17c9d 68 65 20 4d 45 4d 5f 44 79 6e 20 66 6c 61 67 20 he MEM_Dyn flag
17c9e 61 6e 64 20 63 6c 65 61 72 73 20 61 6e 79 20 78 and clears any x
17c9f 44 65 6c 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a Del callback..**
17ca0 20 49 74 20 61 6c 73 6f 20 63 6c 65 61 72 73 20 It also clears
17ca1 4d 45 4d 5f 45 70 68 65 6d 20 61 6e 64 20 4d 45 MEM_Ephem and ME
17ca2 4d 5f 53 74 61 74 69 63 2e 20 49 66 20 74 68 65 M_Static. If the
17ca3 20 70 72 65 73 65 72 76 65 20 66 6c 61 67 20 69 preserve flag i
17ca4 73 20 0a 2a 2a 20 6e 6f 74 20 73 65 74 2c 20 4d s .** not set, M
17ca5 65 6d 2e 6e 20 69 73 20 7a 65 72 6f 65 64 2e 0a em.n is zeroed..
17ca6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
17ca7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
17ca8 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d eMemGrow(Mem *pM
17ca9 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 em, int n, int p
17caa 72 65 73 65 72 76 65 29 7b 0a 20 20 61 73 73 65 reserve){. asse
17cab 72 74 28 20 31 20 3e 3d 0a 20 20 20 20 28 28 70 rt( 1 >=. ((p
17cac 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 Mem->zMalloc &&
17cad 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 pMem->zMalloc==p
17cae 4d 65 6d 2d 3e 7a 29 20 3f 20 31 20 3a 20 30 29 Mem->z) ? 1 : 0)
17caf 20 2b 0a 20 20 20 20 28 28 28 70 4d 65 6d 2d 3e +. (((pMem->
17cb0 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 26 26 flags&MEM_Dyn)&&
17cb1 70 4d 65 6d 2d 3e 78 44 65 6c 29 20 3f 20 31 20 pMem->xDel) ? 1
17cb2 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 70 4d : 0) + . ((pM
17cb3 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 45 70 em->flags&MEM_Ep
17cb4 68 65 6d 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 hem) ? 1 : 0) +
17cb5 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 . ((pMem->fla
17cb6 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 29 20 3f gs&MEM_Static) ?
17cb7 20 31 20 3a 20 30 29 0a 20 20 29 3b 0a 20 20 61 1 : 0). );. a
17cb8 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
17cb9 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d ags&MEM_RowSet)=
17cba 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 6e 3c 33 =0 );.. if( n<3
17cbb 32 20 29 20 6e 20 3d 20 33 32 3b 0a 20 20 69 66 2 ) n = 32;. if
17cbc 28 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f ( sqlite3DbMallo
17cbd 63 53 69 7a 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 cSize(pMem->db,
17cbe 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3c 6e pMem->zMalloc)<n
17cbf 20 29 7b 0a 20 20 20 20 69 66 28 20 70 72 65 73 ){. if( pres
17cc0 65 72 76 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 3d erve && pMem->z=
17cc1 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 =pMem->zMalloc )
17cc2 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 {. pMem->z
17cc3 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 = pMem->zMalloc
17cc4 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c = sqlite3DbReall
17cc5 6f 63 4f 72 46 72 65 65 28 70 4d 65 6d 2d 3e 64 ocOrFree(pMem->d
17cc6 62 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6e 29 3b 0a b, pMem->z, n);.
17cc7 20 20 20 20 20 20 70 72 65 73 65 72 76 65 20 3d preserve =
17cc8 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
17cc9 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
17cca 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 ee(pMem->db, pMe
17ccb 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 m->zMalloc);.
17ccc 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 pMem->zMalloc
17ccd 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
17cce 6f 63 52 61 77 28 70 4d 65 6d 2d 3e 64 62 2c 20 ocRaw(pMem->db,
17ccf 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 n);. }. }..
17cd0 20 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26 if( preserve &&
17cd1 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70 4d 65 6d pMem->z && pMem
17cd2 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d 65 ->zMalloc && pMe
17cd3 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c m->z!=pMem->zMal
17cd4 6c 6f 63 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 loc ){. memcp
17cd5 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c y(pMem->zMalloc,
17cd6 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d 65 6d 2d 3e pMem->z, pMem->
17cd7 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d n);. }. if( pM
17cd8 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 em->flags&MEM_Dy
17cd9 6e 20 26 26 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 n && pMem->xDel
17cda 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 ){. pMem->xDe
17cdb 6c 28 28 76 6f 69 64 20 2a 29 28 70 4d 65 6d 2d l((void *)(pMem-
17cdc 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65 >z));. }.. pMe
17cdd 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 m->z = pMem->zMa
17cde 6c 6c 6f 63 3b 0a 20 20 69 66 28 20 70 4d 65 6d lloc;. if( pMem
17cdf 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4d ->z==0 ){. pM
17ce0 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
17ce1 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Null;. }else{.
17ce2 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 pMem->flags &
17ce3 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 = ~(MEM_Ephem|ME
17ce4 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 7d 0a 20 M_Static);. }.
17ce5 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b pMem->xDel = 0;
17ce6 0a 20 20 72 65 74 75 72 6e 20 28 70 4d 65 6d 2d . return (pMem-
17ce7 3e 7a 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a >z ? SQLITE_OK :
17ce8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a SQLITE_NOMEM);.
17ce9 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 74 68 }../*.** Make th
17cea 65 20 67 69 76 65 6e 20 4d 65 6d 20 6f 62 6a 65 e given Mem obje
17ceb 63 74 20 4d 45 4d 5f 44 79 6e 2e 20 20 49 6e 20 ct MEM_Dyn. In
17cec 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6d 61 6b other words, mak
17ced 65 20 69 74 20 73 6f 0a 2a 2a 20 74 68 61 74 20 e it so.** that
17cee 61 6e 79 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 any TEXT or BLOB
17cef 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 content is stor
17cf0 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 ed in memory obt
17cf1 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 ained from.** ma
17cf2 6c 6c 6f 63 28 29 2e 20 20 49 6e 20 74 68 69 73 lloc(). In this
17cf3 20 77 61 79 2c 20 77 65 20 6b 6e 6f 77 20 74 68 way, we know th
17cf4 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 73 at the memory is
17cf5 20 73 61 66 65 20 74 6f 20 62 65 0a 2a 2a 20 6f safe to be.** o
17cf6 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 61 6c verwritten or al
17cf7 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 tered..**.** Ret
17cf8 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e urn SQLITE_OK on
17cf9 20 73 75 63 63 65 73 73 20 6f 72 20 53 51 4c 49 success or SQLI
17cfa 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 6d 61 6c 6c TE_NOMEM if mall
17cfb 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c oc fails..*/.SQL
17cfc 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
17cfd 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 sqlite3VdbeMemMa
17cfe 6b 65 57 72 69 74 65 61 62 6c 65 28 4d 65 6d 20 keWriteable(Mem
17cff 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 3b *pMem){. int f;
17d00 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d . assert( pMem-
17d01 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 >db==0 || sqlite
17d02 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 3_mutex_held(pMe
17d03 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b m->db->mutex) );
17d04 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
17d05 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 ->flags&MEM_RowS
17d06 65 74 29 3d 3d 30 20 29 3b 0a 20 20 65 78 70 61 et)==0 );. expa
17d07 6e 64 42 6c 6f 62 28 70 4d 65 6d 29 3b 0a 20 20 ndBlob(pMem);.
17d08 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b f = pMem->flags;
17d09 0a 20 20 69 66 28 20 28 66 26 28 4d 45 4d 5f 53 . if( (f&(MEM_S
17d0a 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 26 26 tr|MEM_Blob)) &&
17d0b 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e pMem->z!=pMem->
17d0c 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69 zMalloc ){. i
17d0d 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
17d0e 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 70 4d 65 6d mGrow(pMem, pMem
17d0f 2d 3e 6e 20 2b 20 32 2c 20 31 29 20 29 7b 0a 20 ->n + 2, 1) ){.
17d10 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
17d11 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
17d12 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d pMem->z[pMem
17d13 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d ->n] = 0;. pM
17d14 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d em->z[pMem->n+1]
17d15 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e = 0;. pMem->
17d16 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 flags |= MEM_Ter
17d17 6d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e m;. }.. return
17d18 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
17d19 2a 0a 2a 2a 20 49 66 20 74 68 65 20 67 69 76 65 *.** If the give
17d1a 6e 20 4d 65 6d 2a 20 68 61 73 20 61 20 7a 65 72 n Mem* has a zer
17d1b 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2c 20 74 o-filled tail, t
17d1c 75 72 6e 20 69 74 20 69 6e 74 6f 20 61 6e 20 6f urn it into an o
17d1d 72 64 69 6e 61 72 79 0a 2a 2a 20 62 6c 6f 62 20 rdinary.** blob
17d1e 73 74 6f 72 65 64 20 69 6e 20 64 79 6e 61 6d 69 stored in dynami
17d1f 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 cally allocated
17d20 73 70 61 63 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 space..*/.#ifnde
17d21 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
17d22 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 CRBLOB.SQLITE_PR
17d23 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
17d24 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 3VdbeMemExpandBl
17d25 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 ob(Mem *pMem){.
17d26 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
17d27 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 & MEM_Zero ){.
17d28 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 int nByte;.
17d29 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
17d2a 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 flags&MEM_Blob )
17d2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
17d2c 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 Mem->flags&MEM_R
17d2d 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 owSet)==0 );.
17d2e 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
17d2f 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
17d30 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
17d31 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a >db->mutex) );..
17d32 20 20 20 20 2f 2a 20 53 65 74 20 6e 42 79 74 65 /* Set nByte
17d33 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
17d34 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 f bytes required
17d35 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 65 78 to store the ex
17d36 70 61 6e 64 65 64 20 62 6c 6f 62 2e 20 2a 2f 0a panded blob. */.
17d37 20 20 20 20 6e 42 79 74 65 20 3d 20 70 4d 65 6d nByte = pMem
17d38 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e 75 2e 6e 5a ->n + pMem->u.nZ
17d39 65 72 6f 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 ero;. if( nBy
17d3a 74 65 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e te<=0 ){. n
17d3b 42 79 74 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a Byte = 1;. }.
17d3c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 if( sqlite3V
17d3d 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c dbeMemGrow(pMem,
17d3e 20 6e 42 79 74 65 2c 20 31 29 20 29 7b 0a 20 20 nByte, 1) ){.
17d3f 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
17d40 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 0a E_NOMEM;. }..
17d41 20 20 20 20 6d 65 6d 73 65 74 28 26 70 4d 65 6d memset(&pMem
17d42 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c ->z[pMem->n], 0,
17d43 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 3b pMem->u.nZero);
17d44 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2b 3d 20 . pMem->n +=
17d45 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 pMem->u.nZero;.
17d46 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 pMem->flags &
17d47 3d 20 7e 28 4d 45 4d 5f 5a 65 72 6f 7c 4d 45 4d = ~(MEM_Zero|MEM
17d48 5f 54 65 72 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 _Term);. }. re
17d49 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
17d4a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a }.#endif.../*.**
17d4b 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 67 Make sure the g
17d4c 69 76 65 6e 20 4d 65 6d 20 69 73 20 5c 75 30 30 iven Mem is \u00
17d4d 30 30 20 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 00 terminated..*
17d4e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
17d4f 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
17d50 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 MemNulTerminate(
17d51 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 Mem *pMem){. as
17d52 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
17d53 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
17d54 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
17d55 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
17d56 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
17d57 20 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 7c 7c MEM_Term)!=0 ||
17d58 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 (pMem->flags &
17d59 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20 MEM_Str)==0 ){.
17d5a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
17d5b 5f 4f 4b 3b 20 20 20 2f 2a 20 4e 6f 74 68 69 6e _OK; /* Nothin
17d5c 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20 7d 0a 20 g to do */. }.
17d5d 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
17d5e 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 70 4d MemGrow(pMem, pM
17d5f 65 6d 2d 3e 6e 2b 32 2c 20 31 29 20 29 7b 0a 20 em->n+2, 1) ){.
17d60 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
17d61 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 4d _NOMEM;. }. pM
17d62 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d em->z[pMem->n] =
17d63 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 0;. pMem->z[pM
17d64 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 em->n+1] = 0;.
17d65 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d pMem->flags |= M
17d66 45 4d 5f 54 65 72 6d 3b 0a 20 20 72 65 74 75 72 EM_Term;. retur
17d67 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
17d68 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45 4d 5f 53 74 /*.** Add MEM_St
17d69 72 20 74 6f 20 74 68 65 20 73 65 74 20 6f 66 20 r to the set of
17d6a 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 representations
17d6b 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 4d 65 for the given Me
17d6c 6d 2e 20 20 4e 75 6d 62 65 72 73 0a 2a 2a 20 61 m. Numbers.** a
17d6d 72 65 20 63 6f 6e 76 65 72 74 65 64 20 75 73 69 re converted usi
17d6e 6e 67 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 ng sqlite3_snpri
17d6f 6e 74 66 28 29 2e 20 20 43 6f 6e 76 65 72 74 69 ntf(). Converti
17d70 6e 67 20 61 20 42 4c 4f 42 20 74 6f 20 61 20 73 ng a BLOB to a s
17d71 74 72 69 6e 67 0a 2a 2a 20 69 73 20 61 20 6e 6f tring.** is a no
17d72 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 69 73 74 -op..**.** Exist
17d73 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 ing representati
17d74 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20 61 6e 64 20 ons MEM_Int and
17d75 4d 45 4d 5f 52 65 61 6c 20 61 72 65 20 2a 6e 6f MEM_Real are *no
17d76 74 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a t* invalidated..
17d77 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f 4e 75 6c 6c **.** A MEM_Null
17d78 20 76 61 6c 75 65 20 77 69 6c 6c 20 6e 65 76 65 value will neve
17d79 72 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 74 r be passed to t
17d7a 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 his function. Th
17d7b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a is function is.*
17d7c 2a 20 75 73 65 64 20 66 6f 72 20 63 6f 6e 76 65 * used for conve
17d7d 72 74 69 6e 67 20 76 61 6c 75 65 73 20 74 6f 20 rting values to
17d7e 74 65 78 74 20 66 6f 72 20 72 65 74 75 72 6e 69 text for returni
17d7f 6e 67 20 74 6f 20 74 68 65 20 75 73 65 72 20 28 ng to the user (
17d80 69 2e 65 2e 20 76 69 61 0a 2a 2a 20 73 71 6c 69 i.e. via.** sqli
17d81 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 te3_value_text()
17d82 29 2c 20 6f 72 20 66 6f 72 20 65 6e 73 75 72 69 ), or for ensuri
17d83 6e 67 20 74 68 61 74 20 76 61 6c 75 65 73 20 74 ng that values t
17d84 6f 20 62 65 20 75 73 65 64 20 61 73 20 62 74 72 o be used as btr
17d85 65 65 0a 2a 2a 20 6b 65 79 73 20 61 72 65 20 73 ee.** keys are s
17d86 74 72 69 6e 67 73 2e 20 49 6e 20 74 68 65 20 66 trings. In the f
17d87 6f 72 6d 65 72 20 63 61 73 65 20 61 20 4e 55 4c ormer case a NUL
17d88 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 L pointer is ret
17d89 75 72 6e 65 64 20 74 68 65 0a 2a 2a 20 75 73 65 urned the.** use
17d8a 72 20 61 6e 64 20 74 68 65 20 6c 61 74 65 72 20 r and the later
17d8b 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 70 is an internal p
17d8c 72 6f 67 72 61 6d 6d 69 6e 67 20 65 72 72 6f 72 rogramming error
17d8d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
17d8e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
17d8f 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 dbeMemStringify(
17d90 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 65 Mem *pMem, int e
17d91 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 nc){. int rc =
17d92 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
17d93 20 66 67 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 fg = pMem->flag
17d94 73 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e s;. const int n
17d95 42 79 74 65 20 3d 20 33 32 3b 0a 0a 20 20 61 73 Byte = 32;.. as
17d96 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
17d97 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
17d98 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
17d99 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
17d9a 73 65 72 74 28 20 21 28 66 67 26 4d 45 4d 5f 5a sert( !(fg&MEM_Z
17d9b 65 72 6f 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ero) );. assert
17d9c 28 20 21 28 66 67 26 28 4d 45 4d 5f 53 74 72 7c ( !(fg&(MEM_Str|
17d9d 4d 45 4d 5f 42 6c 6f 62 29 29 20 29 3b 0a 20 20 MEM_Blob)) );.
17d9e 61 73 73 65 72 74 28 20 66 67 26 28 4d 45 4d 5f assert( fg&(MEM_
17d9f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 3b Int|MEM_Real) );
17da0 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
17da1 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 ->flags&MEM_RowS
17da2 65 74 29 3d 3d 30 20 29 3b 0a 0a 0a 20 20 69 66 et)==0 );... if
17da3 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
17da4 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 Grow(pMem, nByte
17da5 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 , 0) ){. retu
17da6 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
17da7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 . }.. /* For a
17da8 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 72 Real or Integer
17da9 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 70 , use sqlite3_mp
17daa 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 75 rintf() to produ
17dab 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 2a ce the UTF-8. *
17dac 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 * string represe
17dad 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 ntation of the v
17dae 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 74 alue. Then, if t
17daf 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 6f he required enco
17db0 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 46 ding. ** is UTF
17db1 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 62 -16le or UTF-16b
17db2 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 69 e do a translati
17db3 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 on.. ** . ** F
17db4 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20 IX ME: It would
17db5 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71 6c be better if sql
17db6 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 ite3_snprintf()
17db7 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e could do UTF-16.
17db8 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 26 . */. if( fg &
17db9 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
17dba 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
17dbb 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c (nByte, pMem->z,
17dbc 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 "%lld", pMem->u
17dbd 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 .i);. }else{.
17dbe 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20 4d assert( fg & M
17dbf 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 73 EM_Real );. s
17dc0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
17dc1 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 nByte, pMem->z,
17dc2 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e "%!.15g", pMem->
17dc3 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e r);. }. pMem->
17dc4 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
17dc5 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 n30(pMem->z);.
17dc6 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
17dc7 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d TE_UTF8;. pMem-
17dc8 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 >flags |= MEM_St
17dc9 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 r|MEM_Term;. sq
17dca 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
17dcb 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e ncoding(pMem, en
17dcc 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b c);. return rc;
17dcd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 .}../*.** Memory
17dce 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 cell pMem conta
17dcf 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20 ins the context
17dd0 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 of an aggregate
17dd1 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 function..** Thi
17dd2 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 s routine calls
17dd3 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74 the finalize met
17dd4 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e hod for that fun
17dd5 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 ction. The.** r
17dd6 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67 esult of the agg
17dd7 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64 regate is stored
17dd8 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e back into pMem.
17dd9 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
17dda 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68 LITE_ERROR if th
17ddb 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f e finalizer repo
17ddc 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 rts an error. S
17ddd 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 QLITE_OK.** othe
17dde 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rwise..*/.SQLITE
17ddf 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
17de0 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c ite3VdbeMemFinal
17de1 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 ize(Mem *pMem, F
17de2 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a uncDef *pFunc){.
17de3 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
17de4 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 46 75 6e E_OK;. if( pFun
17de5 63 20 26 26 20 70 46 75 6e 63 2d 3e 78 46 69 6e c && pFunc->xFin
17de6 61 6c 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c alize ){. sql
17de7 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 ite3_context ctx
17de8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
17de9 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
17dea 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c 20 70 46 75 _Null)!=0 || pFu
17deb 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 nc==pMem->u.pDef
17dec 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
17ded 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
17dee 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
17def 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
17df0 78 29 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 x) );. memset
17df1 28 26 63 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 (&ctx, 0, sizeof
17df2 28 63 74 78 29 29 3b 0a 20 20 20 20 63 74 78 2e (ctx));. ctx.
17df3 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 s.flags = MEM_Nu
17df4 6c 6c 3b 0a 20 20 20 20 63 74 78 2e 73 2e 64 62 ll;. ctx.s.db
17df5 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 20 = pMem->db;.
17df6 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d 65 6d ctx.pMem = pMem
17df7 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 20 ;. ctx.pFunc
17df8 3d 20 70 46 75 6e 63 3b 0a 20 20 20 20 70 46 75 = pFunc;. pFu
17df9 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 28 26 63 nc->xFinalize(&c
17dfa 74 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 tx);. assert(
17dfb 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 0==(pMem->flags
17dfc 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20 21 70 4d &MEM_Dyn) && !pM
17dfd 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 20 em->xDel );.
17dfe 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d sqlite3DbFree(pM
17dff 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d em->db, pMem->zM
17e00 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 6d 65 6d 63 alloc);. memc
17e01 70 79 28 70 4d 65 6d 2c 20 26 63 74 78 2e 73 2c py(pMem, &ctx.s,
17e02 20 73 69 7a 65 6f 66 28 63 74 78 2e 73 29 29 3b sizeof(ctx.s));
17e03 0a 20 20 20 20 72 63 20 3d 20 28 63 74 78 2e 69 . rc = (ctx.i
17e04 73 45 72 72 6f 72 3f 53 51 4c 49 54 45 5f 45 52 sError?SQLITE_ER
17e05 52 4f 52 3a 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a ROR:SQLITE_OK);.
17e06 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
17e07 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
17e08 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e memory cell con
17e09 74 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76 tains a string v
17e0a 61 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20 62 alue that must b
17e0b 65 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e e freed by.** in
17e0c 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e voking an extern
17e0d 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65 al callback, fre
17e0e 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e e it now. Callin
17e0f 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a g this function.
17e10 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65 ** does not free
17e11 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 any Mem.zMalloc
17e12 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 buffer..*/.SQLI
17e13 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
17e14 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
17e15 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 leaseExternal(Me
17e16 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 m *p){. assert(
17e17 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c p->db==0 || sql
17e18 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
17e19 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
17e1a 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 . if( p->flags&
17e1b 4d 45 4d 5f 41 67 67 20 29 7b 0a 20 20 20 20 73 MEM_Agg ){. s
17e1c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e qlite3VdbeMemFin
17e1d 61 6c 69 7a 65 28 70 2c 20 70 2d 3e 75 2e 70 44 alize(p, p->u.pD
17e1e 65 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ef);. assert(
17e1f 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d (p->flags & MEM
17e20 5f 41 67 67 29 3d 3d 30 20 29 3b 0a 20 20 20 20 _Agg)==0 );.
17e21 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
17e22 6c 65 61 73 65 28 70 29 3b 0a 20 20 7d 65 6c 73 lease(p);. }els
17e23 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d e if( p->flags&M
17e24 45 4d 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 EM_Dyn && p->xDe
17e25 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 l ){. assert(
17e26 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 (p->flags&MEM_R
17e27 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 owSet)==0 );.
17e28 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a p->xDel((void *
17e29 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 2d 3e 78 )p->z);. p->x
17e2a 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 Del = 0;. }else
17e2b 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 if( p->flags&ME
17e2c 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 M_RowSet ){.
17e2d 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 sqlite3RowSetCle
17e2e 61 72 28 70 2d 3e 75 2e 70 52 6f 77 53 65 74 29 ar(p->u.pRowSet)
17e2f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ;. }.}../*.** R
17e30 65 6c 65 61 73 65 20 61 6e 79 20 6d 65 6d 6f 72 elease any memor
17e31 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 4d 65 y held by the Me
17e32 6d 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 76 m. This may leav
17e33 65 20 74 68 65 20 4d 65 6d 20 69 6e 20 61 6e 0a e the Mem in an.
17e34 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 ** inconsistent
17e35 73 74 61 74 65 2c 20 66 6f 72 20 65 78 61 6d 70 state, for examp
17e36 6c 65 20 77 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d le with (Mem.z==
17e37 30 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 0) and.** (Mem.t
17e38 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 ype==SQLITE_TEXT
17e39 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
17e3a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
17e3b 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
17e3c 4d 65 6d 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 Mem *p){. sqlit
17e3d 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
17e3e 45 78 74 65 72 6e 61 6c 28 70 29 3b 0a 20 20 73 External(p);. s
17e3f 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e qlite3DbFree(p->
17e40 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b db, p->zMalloc);
17e41 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70 . p->z = 0;. p
17e42 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 ->zMalloc = 0;.
17e43 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a p->xDel = 0;.}.
17e44 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 ./*.** Convert a
17e45 20 36 34 2d 62 69 74 20 49 45 45 45 20 64 6f 75 64-bit IEEE dou
17e46 62 6c 65 20 69 6e 74 6f 20 61 20 36 34 2d 62 69 ble into a 64-bi
17e47 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
17e48 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 75 62 ..** If the doub
17e49 6c 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c le is too large,
17e4a 20 72 65 74 75 72 6e 20 30 78 38 30 30 30 30 30 return 0x800000
17e4b 30 30 30 30 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a 0000000000..**.*
17e4c 2a 20 4d 6f 73 74 20 73 79 73 74 65 6d 73 20 61 * Most systems a
17e4d 70 70 65 61 72 20 74 6f 20 64 6f 20 74 68 69 73 ppear to do this
17e4e 20 73 69 6d 70 6c 79 20 62 79 20 61 73 73 69 67 simply by assig
17e4f 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65 ning.** variable
17e50 73 20 61 6e 64 20 77 69 74 68 6f 75 74 20 74 68 s and without th
17e51 65 20 65 78 74 72 61 20 72 61 6e 67 65 20 74 65 e extra range te
17e52 73 74 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 sts. But.** the
17e53 72 65 20 61 72 65 20 72 65 70 6f 72 74 73 20 74 re are reports t
17e54 68 61 74 20 77 69 6e 64 6f 77 73 20 74 68 72 6f hat windows thro
17e55 77 73 20 61 6e 20 65 78 70 65 63 74 69 6f 6e 0a ws an expection.
17e56 2a 2a 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 ** if the floati
17e57 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 ng point value i
17e58 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 s out of range.
17e59 28 53 65 65 20 74 69 63 6b 65 74 20 23 32 38 38 (See ticket #288
17e5a 30 2e 29 0a 2a 2a 20 42 65 63 61 75 73 65 20 77 0.).** Because w
17e5b 65 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 e do not complet
17e5c 65 6c 79 20 75 6e 64 65 72 73 74 61 6e 64 20 74 ely understand t
17e5d 68 65 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 77 he problem, we w
17e5e 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 ill.** take the
17e5f 63 6f 6e 73 65 72 76 61 74 69 76 65 20 61 70 70 conservative app
17e60 72 6f 61 63 68 20 61 6e 64 20 61 6c 77 61 79 73 roach and always
17e61 20 64 6f 20 72 61 6e 67 65 20 74 65 73 74 73 0a do range tests.
17e62 2a 2a 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 ** before attemp
17e63 74 69 6e 67 20 74 68 65 20 63 6f 6e 76 65 72 73 ting the convers
17e64 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ion..*/.static i
17e65 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 64 doubleToInt64
17e66 28 64 6f 75 62 6c 65 20 72 29 7b 0a 20 20 2f 2a (double r){. /*
17e67 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 6f 6d 70 69 . ** Many compi
17e68 6c 65 72 73 20 77 65 20 65 6e 63 6f 75 6e 74 65 lers we encounte
17e69 72 20 64 6f 20 6e 6f 74 20 64 65 66 69 6e 65 20 r do not define
17e6a 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 constants for th
17e6b 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61 e. ** minimum a
17e6c 6e 64 20 6d 61 78 69 6d 75 6d 20 36 34 2d 62 69 nd maximum 64-bi
17e6d 74 20 69 6e 74 65 67 65 72 73 2c 20 6f 72 20 74 t integers, or t
17e6e 68 65 79 20 64 65 66 69 6e 65 20 74 68 65 6d 0a hey define them.
17e6f 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e ** inconsisten
17e70 74 6c 79 2e 20 20 41 6e 64 20 6d 61 6e 79 20 64 tly. And many d
17e71 6f 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 o not understand
17e72 20 74 68 65 20 22 4c 4c 22 20 6e 6f 74 61 74 69 the "LL" notati
17e73 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20 64 on.. ** So we d
17e74 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 20 73 74 efine our own st
17e75 61 74 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 68 atic constants h
17e76 65 72 65 20 75 73 69 6e 67 20 6e 6f 74 68 69 6e ere using nothin
17e77 67 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 g. ** larger th
17e78 61 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 an a 32-bit inte
17e79 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 0a 20 20 ger constant..
17e7a 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 */. static cons
17e7b 74 20 69 36 34 20 6d 61 78 49 6e 74 20 3d 20 4c t i64 maxInt = L
17e7c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a 20 20 ARGEST_INT64;.
17e7d 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34 static const i64
17e7e 20 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 minInt = SMALLE
17e7f 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 69 66 28 ST_INT64;.. if(
17e80 20 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e r<(double)minIn
17e81 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
17e82 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 minInt;. }else
17e83 69 66 28 20 72 3e 28 64 6f 75 62 6c 65 29 6d 61 if( r>(double)ma
17e84 78 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 xInt ){. retu
17e85 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c rn minInt;. }el
17e86 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 se{. return (
17e87 69 36 34 29 72 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a i64)r;. }.}../*
17e88 0a 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d 65 20 .** Return some
17e89 6b 69 6e 64 20 6f 66 20 69 6e 74 65 67 65 72 20 kind of integer
17e8a 76 61 6c 75 65 20 77 68 69 63 68 20 69 73 20 74 value which is t
17e8b 68 65 20 62 65 73 74 20 77 65 20 63 61 6e 20 64 he best we can d
17e8c 6f 0a 2a 2a 20 61 74 20 72 65 70 72 65 73 65 6e o.** at represen
17e8d 74 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 74 ting the value t
17e8e 68 61 74 20 2a 70 4d 65 6d 20 64 65 73 63 72 69 hat *pMem descri
17e8f 62 65 73 20 61 73 20 61 6e 20 69 6e 74 65 67 65 bes as an intege
17e90 72 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 r..** If pMem is
17e91 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 68 65 an integer, the
17e92 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 65 n the value is e
17e93 78 61 63 74 2e 20 20 49 66 20 70 4d 65 6d 20 69 xact. If pMem i
17e94 73 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e 67 2d s.** a floating-
17e95 70 6f 69 6e 74 20 74 68 65 6e 20 74 68 65 20 76 point then the v
17e96 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 alue returned is
17e97 20 74 68 65 20 69 6e 74 65 67 65 72 20 70 61 72 the integer par
17e98 74 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 t..** If pMem is
17e99 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f a string or blo
17e9a 62 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b 65 20 b, then we make
17e9b 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f an attempt to co
17e9c 6e 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e 74 6f nvert.** it into
17e9d 20 61 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72 a integer and r
17e9e 65 74 75 72 6e 20 74 68 61 74 2e 20 20 49 66 20 eturn that. If
17e9f 70 4d 65 6d 20 69 73 20 4e 55 4c 4c 2c 20 72 65 pMem is NULL, re
17ea0 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 turn 0..**.** If
17ea1 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e pMem is a strin
17ea2 67 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20 g, its encoding
17ea3 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 64 might be changed
17ea4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
17ea5 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 ATE i64 sqlite3V
17ea6 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20 dbeIntValue(Mem
17ea7 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c *pMem){. int fl
17ea8 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ags;. assert( p
17ea9 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
17eaa 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
17eab 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
17eac 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20 70 ) );. flags = p
17ead 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 Mem->flags;. if
17eae 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ( flags & MEM_In
17eaf 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
17eb0 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c pMem->u.i;. }el
17eb1 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d se if( flags & M
17eb2 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 EM_Real ){. r
17eb3 65 74 75 72 6e 20 64 6f 75 62 6c 65 54 6f 49 6e eturn doubleToIn
17eb4 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 t64(pMem->r);.
17eb5 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 }else if( flags
17eb6 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 & (MEM_Str|MEM_B
17eb7 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 36 34 20 lob) ){. i64
17eb8 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d 65 6d 2d value;. pMem-
17eb9 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 >flags |= MEM_St
17eba 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 r;. if( sqlit
17ebb 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
17ebc 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54 ding(pMem, SQLIT
17ebd 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20 7c E_UTF8). |
17ebe 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d | sqlite3VdbeMem
17ebf 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d 65 NulTerminate(pMe
17ec0 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 m) ){. retu
17ec1 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 0;. }.
17ec2 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 assert( pMem->z
17ec3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 );. sqlite3At
17ec4 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c 20 26 76 oi64(pMem->z, &v
17ec5 61 6c 75 65 29 3b 0a 20 20 20 20 72 65 74 75 72 alue);. retur
17ec6 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65 6c 73 65 n value;. }else
17ec7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
17ec8 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
17ec9 75 72 6e 20 74 68 65 20 62 65 73 74 20 72 65 70 urn the best rep
17eca 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 resentation of p
17ecb 4d 65 6d 20 74 68 61 74 20 77 65 20 63 61 6e 20 Mem that we can
17ecc 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a 20 64 6f get into a.** do
17ecd 75 62 6c 65 2e 20 20 49 66 20 70 4d 65 6d 20 69 uble. If pMem i
17ece 73 20 61 6c 72 65 61 64 79 20 61 20 64 6f 75 62 s already a doub
17ecf 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65 67 65 72 le or an integer
17ed0 2c 20 72 65 74 75 72 6e 20 69 74 73 0a 2a 2a 20 , return its.**
17ed1 76 61 6c 75 65 2e 20 20 49 66 20 69 74 20 69 73 value. If it is
17ed2 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f a string or blo
17ed3 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 b, try to conver
17ed4 74 20 69 74 20 74 6f 20 61 20 64 6f 75 62 6c 65 t it to a double
17ed5 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 61 20 ..** If it is a
17ed6 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 30 NULL, return 0.0
17ed7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
17ed8 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 ATE double sqlit
17ed9 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 e3VdbeRealValue(
17eda 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 Mem *pMem){. as
17edb 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
17edc 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
17edd 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
17ede 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
17edf 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
17ee0 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
17ee1 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a return pMem->r;.
17ee2 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d }else if( pMem
17ee3 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
17ee4 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
17ee5 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e (double)pMem->u.
17ee6 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 i;. }else if( p
17ee7 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Mem->flags & (ME
17ee8 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 M_Str|MEM_Blob)
17ee9 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 ){. double va
17eea 6c 20 3d 20 30 2e 30 3b 0a 20 20 20 20 70 4d 65 l = 0.0;. pMe
17eeb 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f m->flags |= MEM_
17eec 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c Str;. if( sql
17eed 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e ite3VdbeChangeEn
17eee 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c coding(pMem, SQL
17eef 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 ITE_UTF8).
17ef0 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d || sqlite3VdbeM
17ef1 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 emNulTerminate(p
17ef2 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 Mem) ){. re
17ef3 74 75 72 6e 20 30 2e 30 3b 0a 20 20 20 20 7d 0a turn 0.0;. }.
17ef4 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
17ef5 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ->z );. sqlit
17ef6 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e 7a 2c 20 e3AtoF(pMem->z,
17ef7 26 76 61 6c 29 3b 0a 20 20 20 20 72 65 74 75 72 &val);. retur
17ef8 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a n val;. }else{.
17ef9 20 20 20 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a return 0.0;.
17efa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 }.}../*.** The
17efb 20 4d 45 4d 20 73 74 72 75 63 74 75 72 65 20 69 MEM structure i
17efc 73 20 61 6c 72 65 61 64 79 20 61 20 4d 45 4d 5f s already a MEM_
17efd 52 65 61 6c 2e 20 20 54 72 79 20 74 6f 20 61 6c Real. Try to al
17efe 73 6f 20 6d 61 6b 65 20 69 74 20 61 0a 2a 2a 20 so make it a.**
17eff 4d 45 4d 5f 49 6e 74 20 69 66 20 77 65 20 63 61 MEM_Int if we ca
17f00 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
17f01 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
17f02 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 3VdbeIntegerAffi
17f03 6e 69 74 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b nity(Mem *pMem){
17f04 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d . assert( pMem-
17f05 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 >flags & MEM_Rea
17f06 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 l );. assert( (
17f07 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
17f08 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a M_RowSet)==0 );.
17f09 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
17f0a 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
17f0b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
17f0c 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
17f0d 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 . pMem->u.i = d
17f0e 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 oubleToInt64(pMe
17f0f 6d 2d 3e 72 29 3b 0a 20 20 69 66 28 20 70 4d 65 m->r);. if( pMe
17f10 6d 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65 29 70 4d m->r==(double)pM
17f11 65 6d 2d 3e 75 2e 69 20 29 7b 0a 20 20 20 20 70 em->u.i ){. p
17f12 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Mem->flags |= ME
17f13 4d 5f 49 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a M_Int;. }.}../*
17f14 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d .** Convert pMem
17f15 20 74 6f 20 74 79 70 65 20 69 6e 74 65 67 65 72 to type integer
17f16 2e 20 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6e . Invalidate an
17f17 79 20 70 72 69 6f 72 20 72 65 70 72 65 73 65 6e y prior represen
17f18 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 tations..*/.SQLI
17f19 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
17f1a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
17f1b 65 67 65 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 egerify(Mem *pMe
17f1c 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d m){. assert( pM
17f1d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
17f1e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
17f1f 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
17f20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
17f21 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
17f22 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
17f23 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 71 6c pMem->u.i = sql
17f24 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 ite3VdbeIntValue
17f25 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 74 (pMem);. MemSet
17f26 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d TypeFlag(pMem, M
17f27 45 4d 5f 49 6e 74 29 3b 0a 20 20 72 65 74 75 72 EM_Int);. retur
17f28 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
17f29 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d /*.** Convert pM
17f2a 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 em so that it is
17f2b 20 6f 66 20 74 79 70 65 20 4d 45 4d 5f 52 65 61 of type MEM_Rea
17f2c 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 l..** Invalidate
17f2d 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72 65 any prior repre
17f2e 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 sentations..*/.S
17f2f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
17f30 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
17f31 52 65 61 6c 69 66 79 28 4d 65 6d 20 2a 70 4d 65 Realify(Mem *pMe
17f32 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d m){. assert( pM
17f33 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
17f34 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
17f35 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
17f36 20 29 3b 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 );. pMem->r =
17f37 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 sqlite3VdbeRealV
17f38 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 alue(pMem);. Me
17f39 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 mSetTypeFlag(pMe
17f3a 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 m, MEM_Real);.
17f3b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
17f3c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
17f3d 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 rt pMem so that
17f3e 69 74 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d it has types MEM
17f3f 5f 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 _Real or MEM_Int
17f40 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 or both..** Inv
17f41 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f alidate any prio
17f42 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e r representation
17f43 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
17f44 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
17f45 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 VdbeMemNumerify(
17f46 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f Mem *pMem){. do
17f47 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 uble r1, r2;. i
17f48 36 34 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 64 i;. assert(
17f49 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 (pMem->flags & (
17f4a 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c MEM_Int|MEM_Real
17f4b 7c 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 |MEM_Null))==0 )
17f4c 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 ;. assert( (pMe
17f4d 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f m->flags & (MEM_
17f4e 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d Blob|MEM_Str))!=
17f4f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
17f50 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
17f51 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
17f52 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
17f53 29 20 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 ) );. r1 = sqli
17f54 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 te3VdbeRealValue
17f55 28 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f (pMem);. i = do
17f56 75 62 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b ubleToInt64(r1);
17f57 0a 20 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 . r2 = (double)
17f58 69 3b 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 i;. if( r1==r2
17f59 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
17f5a 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
17f5b 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a pMem);. }else{.
17f5c 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 pMem->r = r1
17f5d 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 ;. MemSetType
17f5e 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 Flag(pMem, MEM_R
17f5f 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 eal);. }. retu
17f60 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
17f61 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e ./*.** Delete an
17f62 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 y previous value
17f63 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c and set the val
17f64 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d ue stored in *pM
17f65 65 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 em to NULL..*/.S
17f66 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
17f67 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
17f68 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d mSetNull(Mem *pM
17f69 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d em){. if( pMem-
17f6a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
17f6b 53 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Set ){. sqlit
17f6c 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d e3RowSetClear(pM
17f6d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a em->u.pRowSet);.
17f6e 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 }. MemSetType
17f6f 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e Flag(pMem, MEM_N
17f70 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 ull);. pMem->ty
17f71 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c pe = SQLITE_NULL
17f72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 ;.}../*.** Delet
17f73 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 e any previous v
17f74 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 alue and set the
17f75 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 value to be a B
17f76 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a LOB of length.**
17f77 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c n containing al
17f78 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 l zeros..*/.SQLI
17f79 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
17f7a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
17f7b 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 tZeroBlob(Mem *p
17f7c 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 Mem, int n){. s
17f7d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
17f7e 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d ease(pMem);. pM
17f7f 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
17f80 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 Blob|MEM_Zero;.
17f81 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
17f82 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 LITE_BLOB;. pMe
17f83 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 m->n = 0;. if(
17f84 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 n<0 ) n = 0;. p
17f85 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e Mem->u.nZero = n
17f86 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 ;. pMem->enc =
17f87 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a SQLITE_UTF8;.}..
17f88 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 /*.** Delete any
17f89 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 previous value
17f8a 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 and set the valu
17f8b 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 e stored in *pMe
17f8c 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e m to val,.** man
17f8d 69 66 65 73 74 20 74 79 70 65 20 49 4e 54 45 47 ifest type INTEG
17f8e 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ER..*/.SQLITE_PR
17f8f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
17f90 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
17f91 34 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 4(Mem *pMem, i64
17f92 20 76 61 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 val){. sqlite3
17f93 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
17f94 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e Mem);. pMem->u.
17f95 69 20 3d 20 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d i = val;. pMem-
17f96 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
17f97 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d ;. pMem->type =
17f98 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b SQLITE_INTEGER;
17f99 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
17f9a 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 any previous va
17f9b 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 lue and set the
17f9c 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 value stored in
17f9d 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a *pMem to val,.**
17f9e 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 52 manifest type R
17f9f 45 41 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 EAL..*/.SQLITE_P
17fa0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
17fa1 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 te3VdbeMemSetDou
17fa2 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 ble(Mem *pMem, d
17fa3 6f 75 62 6c 65 20 76 61 6c 29 7b 0a 20 20 69 66 ouble val){. if
17fa4 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 ( sqlite3IsNaN(v
17fa5 61 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 al) ){. sqlit
17fa6 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c e3VdbeMemSetNull
17fa7 28 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b (pMem);. }else{
17fa8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
17fa9 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 MemRelease(pMem)
17faa 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 ;. pMem->r =
17fab 76 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 val;. pMem->f
17fac 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b lags = MEM_Real;
17fad 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 . pMem->type
17fae 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a = SQLITE_FLOAT;.
17faf 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c }.}../*.** Del
17fb0 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 ete any previous
17fb1 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 value and set t
17fb2 68 65 20 76 61 6c 75 65 20 6f 66 20 70 4d 65 6d he value of pMem
17fb3 20 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 65 6d 70 to be an.** emp
17fb4 74 79 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 ty boolean index
17fb5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
17fb6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
17fb7 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 VdbeMemSetRowSet
17fb8 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 73 (Mem *pMem){. s
17fb9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 4d 65 qlite3 *db = pMe
17fba 6d 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 m->db;. assert(
17fbb 20 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 db!=0 );. if(
17fbc 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
17fbd 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 M_RowSet ){.
17fbe 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 sqlite3RowSetCle
17fbf 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 ar(pMem->u.pRowS
17fc0 65 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 et);. }else{.
17fc1 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
17fc2 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 Release(pMem);.
17fc3 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 pMem->zMalloc
17fc4 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
17fc5 6f 63 52 61 77 28 64 62 2c 20 36 34 29 3b 0a 20 ocRaw(db, 64);.
17fc6 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c }. if( db->mal
17fc7 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
17fc8 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
17fc9 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 EM_Null;. }else
17fca 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d {. assert( pM
17fcb 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 em->zMalloc );.
17fcc 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 pMem->u.pRowS
17fcd 65 74 20 3d 20 73 71 6c 69 74 65 33 52 6f 77 53 et = sqlite3RowS
17fce 65 74 49 6e 69 74 28 64 62 2c 20 70 4d 65 6d 2d etInit(db, pMem-
17fcf 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a 20 20 20 20 20 >zMalloc, .
17fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17fd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17fd2 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f sqlite3DbMallo
17fd3 63 53 69 7a 65 28 64 62 2c 20 70 4d 65 6d 2d 3e cSize(db, pMem->
17fd4 7a 4d 61 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 61 zMalloc));. a
17fd5 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 75 2e 70 ssert( pMem->u.p
17fd6 52 6f 77 53 65 74 21 3d 30 20 29 3b 0a 20 20 20 RowSet!=0 );.
17fd7 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
17fd8 45 4d 5f 52 6f 77 53 65 74 3b 0a 20 20 7d 0a 7d EM_RowSet;. }.}
17fd9 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
17fda 72 75 65 20 69 66 20 74 68 65 20 4d 65 6d 20 6f rue if the Mem o
17fdb 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 20 61 bject contains a
17fdc 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 74 68 TEXT or BLOB th
17fdd 61 74 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61 72 at is.** too lar
17fde 67 65 20 2d 20 77 68 6f 73 65 20 73 69 7a 65 20 ge - whose size
17fdf 65 78 63 65 65 64 73 20 53 51 4c 49 54 45 5f 4d exceeds SQLITE_M
17fe0 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53 51 AX_LENGTH..*/.SQ
17fe1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
17fe2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 sqlite3VdbeMemT
17fe3 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 29 7b 0a 20 ooBig(Mem *p){.
17fe4 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d assert( p->db!=
17fe5 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 0 );. if( p->fl
17fe6 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d ags & (MEM_Str|M
17fe7 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 EM_Blob) ){.
17fe8 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 int n = p->n;.
17fe9 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 if( p->flags &
17fea 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 MEM_Zero ){.
17feb 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a 65 n += p->u.nZe
17fec 72 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 ro;. }. re
17fed 74 75 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c turn n>p->db->aL
17fee 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
17fef 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 T_LENGTH];. }.
17ff0 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f return 0; .}../
17ff1 2a 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 73 74 72 *.** Size of str
17ff2 75 63 74 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c uct Mem not incl
17ff3 75 64 69 6e 67 20 74 68 65 20 4d 65 6d 2e 7a 4d uding the Mem.zM
17ff4 61 6c 6c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f alloc member..*/
17ff5 0a 23 64 65 66 69 6e 65 20 4d 45 4d 43 45 4c 4c .#define MEMCELL
17ff6 53 49 5a 45 20 28 73 69 7a 65 5f 74 29 28 26 28 SIZE (size_t)(&(
17ff7 28 28 4d 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c ((Mem *)0)->zMal
17ff8 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b loc))../*.** Mak
17ff9 65 20 61 6e 20 73 68 61 6c 6c 6f 77 20 63 6f 70 e an shallow cop
17ffa 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 y of pFrom into
17ffb 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 pTo. Prior cont
17ffc 65 6e 74 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 ents of.** pTo a
17ffd 72 65 20 66 72 65 65 64 2e 20 20 54 68 65 20 70 re freed. The p
17ffe 46 72 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73 From->z field is
17fff 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e not duplicated.
18000 20 20 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a If.** pFrom->z
18001 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 70 is used, then p
18002 54 6f 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 To->z points to
18003 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 the same thing a
18004 73 20 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e s pFrom->z.** an
18005 64 20 66 6c 61 67 73 20 67 65 74 73 20 73 72 63 d flags gets src
18006 54 79 70 65 20 28 65 69 74 68 65 72 20 4d 45 4d Type (either MEM
18007 5f 45 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 _Ephem or MEM_St
18008 61 74 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 atic)..*/.SQLITE
18009 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1800a 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c lite3VdbeMemShal
1800b 6c 6f 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f lowCopy(Mem *pTo
1800c 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 , const Mem *pFr
1800d 6f 6d 2c 20 69 6e 74 20 73 72 63 54 79 70 65 29 om, int srcType)
1800e 7b 0a 20 20 61 73 73 65 72 74 28 20 28 70 46 72 {. assert( (pFr
1800f 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f om->flags & MEM_
18010 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 RowSet)==0 );.
18011 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
18012 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 54 leaseExternal(pT
18013 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f o);. memcpy(pTo
18014 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c , pFrom, MEMCELL
18015 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 78 44 SIZE);. pTo->xD
18016 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 70 el = 0;. if( (p
18017 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f From->flags&MEM_
18018 44 79 6e 29 21 3d 30 20 7c 7c 20 70 46 72 6f 6d Dyn)!=0 || pFrom
18019 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 6c ->z==pFrom->zMal
1801a 6c 6f 63 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e loc ){. pTo->
1801b 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 44 flags &= ~(MEM_D
1801c 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 yn|MEM_Static|ME
1801d 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 61 73 M_Ephem);. as
1801e 73 65 72 74 28 20 73 72 63 54 79 70 65 3d 3d 4d sert( srcType==M
1801f 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72 63 54 EM_Ephem || srcT
18020 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 63 20 ype==MEM_Static
18021 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 );. pTo->flag
18022 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a 20 20 s |= srcType;.
18023 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 }.}../*.** Make
18024 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66 20 70 a full copy of p
18025 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 From into pTo.
18026 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f Prior contents o
18027 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66 72 65 f pTo are.** fre
18028 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 63 6f ed before the co
18029 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a 53 py is made..*/.S
1802a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1802b 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
1802c 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 Copy(Mem *pTo, c
1802d 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 onst Mem *pFrom)
1802e 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
1802f 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 ITE_OK;.. asser
18030 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 t( (pFrom->flags
18031 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
18032 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 0 );. sqlite3Vd
18033 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 beMemReleaseExte
18034 72 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d rnal(pTo);. mem
18035 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 cpy(pTo, pFrom,
18036 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 MEMCELLSIZE);.
18037 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d pTo->flags &= ~M
18038 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69 66 28 20 70 EM_Dyn;.. if( p
18039 54 6f 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 To->flags&(MEM_S
1803a 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a tr|MEM_Blob) ){.
1803b 20 20 20 20 69 66 28 20 30 3d 3d 28 70 46 72 6f if( 0==(pFro
1803c 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 m->flags&MEM_Sta
1803d 74 69 63 29 20 29 7b 0a 20 20 20 20 20 20 70 54 tic) ){. pT
1803e 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f o->flags |= MEM_
1803f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 72 63 20 Ephem;. rc
18040 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
18041 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 54 MakeWriteable(pT
18042 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 o);. }. }..
18043 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
18044 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68 *.** Transfer th
18045 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 46 e contents of pF
18046 72 6f 6d 20 74 6f 20 70 54 6f 2e 20 41 6e 79 20 rom to pTo. Any
18047 65 78 69 73 74 69 6e 67 20 76 61 6c 75 65 20 69 existing value i
18048 6e 20 70 54 6f 20 69 73 0a 2a 2a 20 66 72 65 65 n pTo is.** free
18049 64 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f 6e 74 d. If pFrom cont
1804a 61 69 6e 73 20 65 70 68 65 6d 65 72 61 6c 20 64 ains ephemeral d
1804b 61 74 61 2c 20 61 20 63 6f 70 79 20 69 73 20 6d ata, a copy is m
1804c 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f 6d ade..**.** pFrom
1804d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c contains an SQL
1804e 20 4e 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 20 NULL when this
1804f 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e routine returns.
18050 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18051 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
18052 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20 2a dbeMemMove(Mem *
18053 70 54 6f 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 pTo, Mem *pFrom)
18054 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 6f {. assert( pFro
18055 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
18056 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
18057 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 From->db->mutex)
18058 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 );. assert( pT
18059 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 o->db==0 || sqli
1805a 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1805b 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 To->db->mutex) )
1805c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 6f ;. assert( pFro
1805d 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 54 6f 2d m->db==0 || pTo-
1805e 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d >db==0 || pFrom-
1805f 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b 0a >db==pTo->db );.
18060 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
18061 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20 mRelease(pTo);.
18062 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 memcpy(pTo, pFr
18063 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 29 om, sizeof(Mem))
18064 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 ;. pFrom->flags
18065 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 70 = MEM_Null;. p
18066 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a From->xDel = 0;.
18067 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 pFrom->zMalloc
18068 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 = 0;.}../*.** C
18069 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 hange the value
1806a 6f 66 20 61 20 4d 65 6d 20 74 6f 20 62 65 20 61 of a Mem to be a
1806b 20 73 74 72 69 6e 67 20 6f 72 20 61 20 42 4c 4f string or a BLO
1806c 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d B..**.** The mem
1806d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 ory management s
1806e 74 72 61 74 65 67 79 20 64 65 70 65 6e 64 73 20 trategy depends
1806f 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 on the value of
18070 74 68 65 20 78 44 65 6c 0a 2a 2a 20 70 61 72 61 the xDel.** para
18071 6d 65 74 65 72 2e 20 49 66 20 74 68 65 20 76 61 meter. If the va
18072 6c 75 65 20 70 61 73 73 65 64 20 69 73 20 53 51 lue passed is SQ
18073 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 20 LITE_TRANSIENT,
18074 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74 72 then the .** str
18075 69 6e 67 20 69 73 20 63 6f 70 69 65 64 20 69 6e ing is copied in
18076 74 6f 20 61 20 28 70 6f 73 73 69 62 6c 79 20 65 to a (possibly e
18077 78 69 73 74 69 6e 67 29 20 62 75 66 66 65 72 20 xisting) buffer
18078 6d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 0a managed by the .
18079 2a 2a 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 ** Mem structure
1807a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 . Otherwise, any
1807b 20 65 78 69 73 74 69 6e 67 20 62 75 66 66 65 72 existing buffer
1807c 20 69 73 20 66 72 65 65 64 20 61 6e 64 20 74 68 is freed and th
1807d 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 63 6f 70 e.** pointer cop
1807e 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ied..*/.SQLITE_P
1807f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18080 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
18081 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 . Mem *pMem,
18082 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 /* Memory
18083 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 cell to set to
18084 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a string value */.
18085 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c const char *z,
18086 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 /* String
18087 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 pointer */. int
18088 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
18089 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72 /* Bytes in str
1808a 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 ing, or negative
1808b 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 */. u8 enc,
1808c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 /* Enc
1808d 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 oding of z. 0 f
1808e 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f or BLOBs */. vo
1808f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a id (*xDel)(void*
18090 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 ) /* Destructor
18091 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 function */.){.
18092 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 int nByte = n;
18093 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 /* New valu
18094 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f e for pMem->n */
18095 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 . int iLimit;
18096 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
18097 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 m allowed string
18098 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f or blob size */
18099 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d 20 30 . u16 flags = 0
1809a 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 ; /* New va
1809b 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c lue for pMem->fl
1809c 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ags */.. assert
1809d 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
1809e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1809f 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
180a0 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
180a1 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
180a2 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 MEM_RowSet)==0
180a3 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 );.. /* If z is
180a4 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
180a5 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e set pMem to con
180a6 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c tain an SQL NULL
180a7 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b . */. if( !z ){
180a8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
180a9 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 MemSetNull(pMem)
180aa 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
180ab 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 ITE_OK;. }.. i
180ac 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 f( pMem->db ){.
180ad 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d iLimit = pMem
180ae 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ->db->aLimit[SQL
180af 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
180b0 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ];. }else{.
180b1 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f iLimit = SQLITE_
180b2 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a MAX_LENGTH;. }.
180b3 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d flags = (enc==
180b4 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 0?MEM_Blob:MEM_S
180b5 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 tr);. if( nByte
180b6 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 <0 ){. assert
180b7 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 ( enc!=0 );.
180b8 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f if( enc==SQLITE_
180b9 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 66 6f UTF8 ){. fo
180ba 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 r(nByte=0; nByte
180bb 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 <=iLimit && z[nB
180bc 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d yte]; nByte++){}
180bd 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
180be 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e for(nByte=0; n
180bf 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 Byte<=iLimit &&
180c0 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 (z[nByte] | z[nB
180c1 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d yte+1]); nByte+=
180c2 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 2){}. }. f
180c3 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d lags |= MEM_Term
180c4 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
180c5 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 following block
180c6 73 65 74 73 20 74 68 65 20 6e 65 77 20 76 61 6c sets the new val
180c7 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 ues of Mem.z and
180c8 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 Mem.xDel. It.
180c9 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 20 66 ** also sets a f
180ca 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 lag in local var
180cb 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20 74 6f iable "flags" to
180cc 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 6d 65 indicate the me
180cd 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 mory. ** manage
180ce 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d ment (one of MEM
180cf 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 _Dyn or MEM_Stat
180d0 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ic).. */. if(
180d1 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 xDel==SQLITE_TRA
180d2 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e NSIENT ){. in
180d3 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 t nAlloc = nByte
180d4 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 26 ;. if( flags&
180d5 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 MEM_Term ){.
180d6 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 nAlloc += (enc
180d7 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a ==SQLITE_UTF8?1:
180d8 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 2);. }. if
180d9 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 ( nByte>iLimit )
180da 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
180db 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 QLITE_TOOBIG;.
180dc 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }. if( sqli
180dd 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
180de 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 Mem, nAlloc, 0)
180df 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
180e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
180e1 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 }. memcpy(p
180e2 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f Mem->z, z, nAllo
180e3 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 c);. }else if(
180e4 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e xDel==SQLITE_DYN
180e5 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 AMIC ){. sqli
180e6 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
180e7 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 e(pMem);. pMe
180e8 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 m->zMalloc = pMe
180e9 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a m->z = (char *)z
180ea 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c ;. pMem->xDel
180eb 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
180ec 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
180ed 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
180ee 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 pMem->z = (c
180ef 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 har *)z;. pMe
180f0 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a m->xDel = xDel;.
180f1 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 flags |= ((x
180f2 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 Del==SQLITE_STAT
180f3 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d IC)?MEM_Static:M
180f4 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69 EM_Dyn);. }. i
180f5 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 f( nByte>iLimit
180f6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
180f7 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d LITE_TOOBIG;. }
180f8 0a 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 .. pMem->n = nB
180f9 79 74 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 yte;. pMem->fla
180fa 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d gs = flags;. pM
180fb 65 6d 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d em->enc = (enc==
180fc 30 20 3f 20 53 51 4c 49 54 45 5f 55 54 46 38 20 0 ? SQLITE_UTF8
180fd 3a 20 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e : enc);. pMem->
180fe 74 79 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f type = (enc==0 ?
180ff 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 SQLITE_BLOB : S
18100 51 4c 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 QLITE_TEXT);..#i
18101 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18102 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 70 4d T_UTF16. if( pM
18103 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f em->enc!=SQLITE_
18104 55 54 46 38 20 26 26 20 73 71 6c 69 74 65 33 56 UTF8 && sqlite3V
18105 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 dbeMemHandleBom(
18106 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 pMem) ){. ret
18107 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
18108 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
18109 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1810a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 ;.}../*.** Compa
1810b 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f re the values co
1810c 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 74 ntained by the t
1810d 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c wo memory cells,
1810e 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 returning.** ne
1810f 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 gative, zero or
18110 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d positive if pMem
18111 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 1 is less than,
18112 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 equal to, or gre
18113 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 ater.** than pMe
18114 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 m2. Sorting orde
18115 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 r is NULL's firs
18116 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e t, followed by n
18117 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65 72 73 umbers (integers
18118 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73 .** and reals) s
18119 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c orted numericall
1811a 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 y, followed by t
1811b 65 78 74 20 6f 72 64 65 72 65 64 20 62 79 20 74 ext ordered by t
1811c 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 he collating.**
1811d 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 sequence pColl a
1811e 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 nd finally blob'
1811f 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d s ordered by mem
18120 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f cmp()..**.** Two
18121 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 NULL values are
18122 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61 considered equa
18123 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 l by this functi
18124 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
18125 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
18126 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 3MemCompare(cons
18127 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f t Mem *pMem1, co
18128 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 nst Mem *pMem2,
18129 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 const CollSeq *p
1812a 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b Coll){. int rc;
1812b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 . int f1, f2;.
1812c 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c int combined_fl
1812d 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 ags;.. /* Inter
1812e 63 68 61 6e 67 65 20 70 4d 65 6d 31 20 61 6e 64 change pMem1 and
1812f 20 70 4d 65 6d 32 20 69 66 20 74 68 65 20 63 6f pMem2 if the co
18130 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
18131 20 73 70 65 63 69 66 69 65 73 0a 20 20 2a 2a 20 specifies. **
18132 44 45 53 43 20 6f 72 64 65 72 2e 0a 20 20 2a 2f DESC order.. */
18133 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31 2d 3e 66 . f1 = pMem1->f
18134 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20 70 4d 65 lags;. f2 = pMe
18135 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6d m2->flags;. com
18136 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d 20 66 31 bined_flags = f1
18137 7c 66 32 3b 0a 20 20 61 73 73 65 72 74 28 20 28 |f2;. assert( (
18138 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 26 combined_flags &
18139 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 MEM_RowSet)==0
1813a 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 );. . /* If one
1813b 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 value is NULL,
1813c 69 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 it is less than
1813d 74 68 65 20 6f 74 68 65 72 2e 20 49 66 20 62 6f the other. If bo
1813e 74 68 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61 th values. ** a
1813f 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 re NULL, return
18140 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 0.. */. if( co
18141 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d mbined_flags&MEM
18142 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 _Null ){. ret
18143 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c urn (f2&MEM_Null
18144 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c ) - (f1&MEM_Null
18145 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 );. }.. /* If
18146 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20 6e one value is a n
18147 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 6f 74 umber and the ot
18148 68 65 72 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 her is not, the
18149 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 2e 0a number is less..
1814a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 65 ** If both are
1814b 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72 numbers, compar
1814c 65 20 61 73 20 72 65 61 6c 73 20 69 66 20 6f 6e e as reals if on
1814d 65 20 69 73 20 61 20 72 65 61 6c 2c 20 6f 72 20 e is a real, or
1814e 61 73 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a as integers. **
1814f 20 69 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 if both values
18150 61 72 65 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 are integers..
18151 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 */. if( combine
18152 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74 d_flags&(MEM_Int
18153 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20 |MEM_Real) ){.
18154 20 20 69 66 28 20 21 28 66 31 26 28 4d 45 4d 5f if( !(f1&(MEM_
18155 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 Int|MEM_Real)) )
18156 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
18157 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
18158 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 !(f2&(MEM_Int|ME
18159 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 M_Real)) ){.
1815a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 return -1;.
1815b 20 7d 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 }. if( (f1 &
1815c 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d f2 & MEM_Int)==
1815d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 0 ){. doubl
1815e 65 20 72 31 2c 20 72 32 3b 0a 20 20 20 20 20 20 e r1, r2;.
1815f 69 66 28 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c if( (f1&MEM_Real
18160 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
18161 72 31 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 r1 = (double)pMe
18162 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d m1->u.i;. }
18163 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 31 else{. r1
18164 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20 = pMem1->r;.
18165 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 }. if( (
18166 66 32 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 f2&MEM_Real)==0
18167 29 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 ){. r2 =
18168 28 64 6f 75 62 6c 65 29 70 4d 65 6d 32 2d 3e 75 (double)pMem2->u
18169 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b .i;. }else{
1816a 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 70 4d . r2 = pM
1816b 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a em2->r;. }.
1816c 20 20 20 20 20 20 69 66 28 20 72 31 3c 72 32 20 if( r1<r2
1816d 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 ) return -1;.
1816e 20 20 20 69 66 28 20 72 31 3e 72 32 20 29 20 72 if( r1>r2 ) r
1816f 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 eturn 1;. r
18170 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c eturn 0;. }el
18171 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
18172 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a ( f1&MEM_Int );.
18173 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 32 assert( f2
18174 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 &MEM_Int );.
18175 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 if( pMem1->u.i
18176 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 < pMem2->u.i )
18177 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 return -1;.
18178 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 if( pMem1->u.i
18179 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 > pMem2->u.i ) r
1817a 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 eturn 1;. r
1817b 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 eturn 0;. }.
1817c 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 }.. /* If one
1817d 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e value is a strin
1817e 67 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20 g and the other
1817f 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65 20 73 is a blob, the s
18180 74 72 69 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20 tring is less..
18181 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 ** If both are
18182 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65 strings, compare
18183 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 using the colla
18184 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a ting functions..
18185 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 */. if( combi
18186 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74 ned_flags&MEM_St
18187 72 20 29 7b 0a 20 20 20 20 69 66 28 20 28 66 31 r ){. if( (f1
18188 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 & MEM_Str)==0 )
18189 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
1818a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1818b 28 66 32 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d (f2 & MEM_Str)==
1818c 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
1818d 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 n -1;. }..
1818e 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e assert( pMem1->
1818f 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 enc==pMem2->enc
18190 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
18191 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 Mem1->enc==SQLIT
18192 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 E_UTF8 || .
18193 20 20 20 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e pMem1->en
18194 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c c==SQLITE_UTF16L
18195 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d E || pMem1->enc=
18196 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 =SQLITE_UTF16BE
18197 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 );.. /* The c
18198 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
18199 65 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 e must be define
1819a 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c d at this point,
1819b 20 65 76 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20 even if. **
1819c 74 68 65 20 75 73 65 72 20 64 65 6c 65 74 65 73 the user deletes
1819d 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 the collation s
1819e 65 71 75 65 6e 63 65 20 61 66 74 65 72 20 74 68 equence after th
1819f 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 e vdbe program i
181a0 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 s. ** compile
181a1 64 20 28 74 68 69 73 20 77 61 73 20 6e 6f 74 20 d (this was not
181a2 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 29 always the case)
181a3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
181a4 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 ert( !pColl || p
181a5 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 Coll->xCmp );..
181a6 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a if( pColl ){.
181a7 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d if( pMem1-
181a8 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 >enc==pColl->enc
181a9 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 ){. /* T
181aa 68 65 20 73 74 72 69 6e 67 73 20 61 72 65 20 61 he strings are a
181ab 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 6f lready in the co
181ac 72 72 65 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20 rrect encoding.
181ad 20 43 61 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 Call the.
181ae 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 ** comparison
181af 66 75 6e 63 74 69 6f 6e 20 64 69 72 65 63 74 6c function directl
181b0 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 y */. ret
181b1 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 urn pColl->xCmp(
181b2 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 pColl->pUser,pMe
181b3 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 m1->n,pMem1->z,p
181b4 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a Mem2->n,pMem2->z
181b5 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
181b6 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f const vo
181b7 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a 20 20 20 id *v1, *v2;.
181b8 20 20 20 20 20 69 6e 74 20 6e 31 2c 20 6e 32 3b int n1, n2;
181b9 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63 31 3b . Mem c1;
181ba 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63 32 3b . Mem c2;
181bb 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 . memset(
181bc 26 63 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 &c1, 0, sizeof(c
181bd 31 29 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 1));. mem
181be 73 65 74 28 26 63 32 2c 20 30 2c 20 73 69 7a 65 set(&c2, 0, size
181bf 6f 66 28 63 32 29 29 3b 0a 20 20 20 20 20 20 20 of(c2));.
181c0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
181c1 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 31 2c 20 hallowCopy(&c1,
181c2 70 4d 65 6d 31 2c 20 4d 45 4d 5f 45 70 68 65 6d pMem1, MEM_Ephem
181c3 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
181c4 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 e3VdbeMemShallow
181c5 43 6f 70 79 28 26 63 32 2c 20 70 4d 65 6d 32 2c Copy(&c2, pMem2,
181c6 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 MEM_Ephem);.
181c7 20 20 20 20 20 76 31 20 3d 20 73 71 6c 69 74 65 v1 = sqlite
181c8 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69 3ValueText((sqli
181c9 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 31 2c 20 te3_value*)&c1,
181ca 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 pColl->enc);.
181cb 20 20 20 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 n1 = v1==0
181cc 3f 20 30 20 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 ? 0 : c1.n;.
181cd 20 20 20 20 76 32 20 3d 20 73 71 6c 69 74 65 33 v2 = sqlite3
181ce 56 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 ValueText((sqlit
181cf 65 33 5f 76 61 6c 75 65 2a 29 26 63 32 2c 20 70 e3_value*)&c2, p
181d0 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 Coll->enc);.
181d1 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f n2 = v2==0 ?
181d2 20 30 20 3a 20 63 32 2e 6e 3b 0a 20 20 20 20 20 0 : c2.n;.
181d3 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 rc = pColl->x
181d4 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 Cmp(pColl->pUser
181d5 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32 , n1, v1, n2, v2
181d6 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
181d7 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
181d8 28 26 63 31 29 3b 0a 20 20 20 20 20 20 20 20 73 (&c1);. s
181d9 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
181da 65 61 73 65 28 26 63 32 29 3b 0a 20 20 20 20 20 ease(&c2);.
181db 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
181dc 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
181dd 2f 2a 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 /* If a NULL poi
181de 6e 74 65 72 20 77 61 73 20 70 61 73 73 65 64 20 nter was passed
181df 61 73 20 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 as the collate f
181e0 75 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 unction, fall th
181e1 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 rough. ** to
181e2 74 68 65 20 62 6c 6f 62 20 63 61 73 65 20 61 6e the blob case an
181e3 64 20 75 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 d use memcmp().
181e4 20 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 */. }. . /* B
181e5 6f 74 68 20 76 61 6c 75 65 73 20 6d 75 73 74 20 oth values must
181e6 62 65 20 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 be blobs. Compa
181e7 72 65 20 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 re using memcmp(
181e8 29 2e 20 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 ). */. rc = me
181e9 6d 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 mcmp(pMem1->z, p
181ea 4d 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d Mem2->z, (pMem1-
181eb 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 >n>pMem2->n)?pMe
181ec 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b m2->n:pMem1->n);
181ed 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a . if( rc==0 ){.
181ee 20 20 20 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e rc = pMem1->
181ef 6e 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 n - pMem2->n;.
181f0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
181f1 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 ../*.** Move dat
181f2 61 20 6f 75 74 20 6f 66 20 61 20 62 74 72 65 65 a out of a btree
181f3 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 69 65 key or data fie
181f4 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 ld and into a Me
181f5 6d 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 m structure..**
181f6 54 68 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 The data or key
181f7 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 is taken from th
181f8 65 20 65 6e 74 72 79 20 74 68 61 74 20 70 43 75 e entry that pCu
181f9 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 r is currently p
181fa 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 ointing.** to.
181fb 6f 66 66 73 65 74 20 61 6e 64 20 61 6d 74 20 64 offset and amt d
181fc 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 70 6f etermine what po
181fd 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 rtion of the dat
181fe 61 20 6f 72 20 6b 65 79 20 74 6f 20 72 65 74 72 a or key to retr
181ff 69 65 76 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 ieve..** key is
18200 74 72 75 65 20 74 6f 20 67 65 74 20 74 68 65 20 true to get the
18201 6b 65 79 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 key or false to
18202 67 65 74 20 64 61 74 61 2e 20 20 54 68 65 20 72 get data. The r
18203 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e esult is written
18204 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 .** into the pMe
18205 6d 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a m element..**.**
18206 20 54 68 65 20 70 4d 65 6d 20 73 74 72 75 63 74 The pMem struct
18207 75 72 65 20 69 73 20 61 73 73 75 6d 65 64 20 74 ure is assumed t
18208 6f 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a o be uninitializ
18209 65 64 2e 20 20 41 6e 79 20 70 72 69 6f 72 20 63 ed. Any prior c
1820a 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 ontent.** is ove
1820b 72 77 72 69 74 74 65 6e 20 77 69 74 68 6f 75 74 rwritten without
1820c 20 62 65 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a being freed..**
1820d 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 .** If this rout
1820e 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e ine fails for an
1820f 79 20 72 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 y reason (malloc
18210 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 returns NULL or
18211 20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 unable.** to re
18212 61 64 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b ad from the disk
18213 29 20 74 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 ) then the pMem
18214 69 73 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e is left in an in
18215 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 consistent state
18216 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18217 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
18218 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
18219 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
1821a 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 r, /* Cursor p
1821b 6f 69 6e 74 69 6e 67 20 61 74 20 72 65 63 6f 72 ointing at recor
1821c 64 20 74 6f 20 72 65 74 72 69 65 76 65 2e 20 2a d to retrieve. *
1821d 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 /. int offset,
1821e 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
1821f 66 72 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f from the start o
18220 66 20 64 61 74 61 20 74 6f 20 72 65 74 75 72 6e f data to return
18221 20 62 79 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a bytes from. */.
18222 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 int amt,
18223 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
18224 20 62 79 74 65 73 20 74 6f 20 72 65 74 75 72 6e bytes to return
18225 2e 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 . */. int key,
18226 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 /* If t
18227 72 75 65 2c 20 72 65 74 72 69 65 76 65 20 66 72 rue, retrieve fr
18228 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6b 65 79 om the btree key
18229 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 , not data. */.
1822a 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 Mem *pMem
1822b 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 /* OUT: Retur
1822c 6e 20 64 61 74 61 20 69 6e 20 74 68 69 73 20 4d n data in this M
1822d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f em structure. */
1822e 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 .){. char *zDat
1822f 61 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 a; /* Data
18230 20 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 from the btree
18231 6c 61 79 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 layer */. int a
18232 76 61 69 6c 61 62 6c 65 20 3d 20 30 3b 20 2f 2a vailable = 0; /*
18233 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
18234 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 available on th
18235 65 20 6c 6f 63 61 6c 20 62 74 72 65 65 20 70 61 e local btree pa
18236 67 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 ge */. sqlite3
18237 2a 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 *db; /* Da
18238 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
18239 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 n */. int rc =
1823a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 SQLITE_OK;.. db
1823b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 = sqlite3BtreeC
1823c 75 72 73 6f 72 44 62 28 70 43 75 72 29 3b 0a 20 ursorDb(pCur);.
1823d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1823e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
1823f 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
18240 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
18241 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
18242 30 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 20 29 0 );. if( key )
18243 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 {. zData = (c
18244 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74 72 har *)sqlite3Btr
18245 65 65 4b 65 79 46 65 74 63 68 28 70 43 75 72 2c eeKeyFetch(pCur,
18246 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 &available);.
18247 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 }else{. zData
18248 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 = (char *)sqlit
18249 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 e3BtreeDataFetch
1824a 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c (pCur, &availabl
1824b 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 e);. }. assert
1824c 28 20 7a 44 61 74 61 21 3d 30 20 29 3b 0a 0a 20 ( zData!=0 );..
1824d 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 3c if( offset+amt<
1824e 3d 61 76 61 69 6c 61 62 6c 65 20 26 26 20 28 28 =available && ((
1824f 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
18250 44 79 6e 29 3d 3d 30 20 7c 7c 20 70 4d 65 6d 2d Dyn)==0 || pMem-
18251 3e 78 44 65 6c 29 20 29 7b 0a 20 20 20 20 73 71 >xDel) ){. sq
18252 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
18253 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 ase(pMem);. p
18254 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74 61 5b Mem->z = &zData[
18255 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 70 4d 65 offset];. pMe
18256 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 m->flags = MEM_B
18257 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 lob|MEM_Ephem;.
18258 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54 }else if( SQLIT
18259 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK==(rc = sqli
1825a 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
1825b 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 30 29 29 20 Mem, amt+2, 0))
1825c 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ){. pMem->fla
1825d 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 gs = MEM_Blob|ME
1825e 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Dyn|MEM_Term;.
1825f 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 pMem->enc =
18260 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 0;. pMem->typ
18261 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b e = SQLITE_BLOB;
18262 0a 20 20 20 20 69 66 28 20 6b 65 79 20 29 7b 0a . if( key ){.
18263 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
18264 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c e3BtreeKey(pCur,
18265 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d offset, amt, pM
18266 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 em->z);. }els
18267 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 e{. rc = sq
18268 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 70 lite3BtreeData(p
18269 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 Cur, offset, amt
1826a 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 , pMem->z);.
1826b 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d }. pMem->z[am
1826c 74 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d t] = 0;. pMem
1826d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a ->z[amt+1] = 0;.
1826e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1826f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 TE_OK ){. s
18270 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
18271 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 ease(pMem);.
18272 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 }. }. pMem->n
18273 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 74 75 72 6e = amt;.. return
18274 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 30 0a 2f 2a rc;.}..#if 0./*
18275 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 76 61 72 69 .** Perform vari
18276 6f 75 73 20 63 68 65 63 6b 73 20 6f 6e 20 74 68 ous checks on th
18277 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70 4d e memory cell pM
18278 65 6d 2e 20 41 6e 20 61 73 73 65 72 74 28 29 20 em. An assert()
18279 77 69 6c 6c 0a 2a 2a 20 66 61 69 6c 20 69 66 20 will.** fail if
1827a 70 4d 65 6d 20 69 73 20 69 6e 74 65 72 6e 61 6c pMem is internal
1827b 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2e ly inconsistent.
1827c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1827d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1827e 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d 65 6d dbeMemSanity(Mem
1827f 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 *pMem){. int f
18280 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 lags = pMem->fla
18281 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 66 6c gs;. assert( fl
18282 61 67 73 21 3d 30 20 29 3b 20 20 2f 2a 20 4d 75 ags!=0 ); /* Mu
18283 73 74 20 64 65 66 69 6e 65 20 73 6f 6d 65 20 74 st define some t
18284 79 70 65 20 2a 2f 0a 20 20 69 66 28 20 66 6c 61 ype */. if( fla
18285 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 gs & (MEM_Str|ME
18286 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 M_Blob) ){. i
18287 6e 74 20 78 20 3d 20 66 6c 61 67 73 20 26 20 28 nt x = flags & (
18288 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 MEM_Static|MEM_D
18289 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d yn|MEM_Ephem|MEM
1828a 5f 53 68 6f 72 74 29 3b 0a 20 20 20 20 61 73 73 _Short);. ass
1828b 65 72 74 28 20 78 21 3d 30 20 29 3b 20 20 20 20 ert( x!=0 );
1828c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e /* Strin
1828d 67 73 20 6d 75 73 74 20 64 65 66 69 6e 65 20 61 gs must define a
1828e 20 73 74 72 69 6e 67 20 73 75 62 74 79 70 65 20 string subtype
1828f 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 */. assert( (
18290 78 20 26 20 28 78 2d 31 29 29 3d 3d 30 20 29 3b x & (x-1))==0 );
18291 20 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73 74 /* Only one st
18292 72 69 6e 67 20 73 75 62 74 79 70 65 20 63 61 6e ring subtype can
18293 20 62 65 20 64 65 66 69 6e 65 64 20 2a 2f 0a 20 be defined */.
18294 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d assert( pMem-
18295 3e 7a 21 3d 30 20 29 3b 20 20 20 20 20 20 2f 2a >z!=0 ); /*
18296 20 53 74 72 69 6e 67 73 20 6d 75 73 74 20 68 61 Strings must ha
18297 76 65 20 61 20 76 61 6c 75 65 20 2a 2f 0a 20 20 ve a value */.
18298 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 /* Mem.z point
18299 73 20 74 6f 20 4d 65 6d 2e 7a 53 68 6f 72 74 20 s to Mem.zShort
1829a 69 66 66 20 74 68 65 20 73 75 62 74 79 70 65 20 iff the subtype
1829b 69 73 20 4d 45 4d 5f 53 68 6f 72 74 20 2a 2f 0a is MEM_Short */.
1829c 20 20 20 20 61 73 73 65 72 74 28 20 28 78 20 26 assert( (x &
1829d 20 4d 45 4d 5f 53 68 6f 72 74 29 3d 3d 30 20 7c MEM_Short)==0 |
1829e 7c 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d | pMem->z==pMem-
1829f 3e 7a 53 68 6f 72 74 20 29 3b 0a 20 20 20 20 61 >zShort );. a
182a0 73 73 65 72 74 28 20 28 78 20 26 20 4d 45 4d 5f ssert( (x & MEM_
182a1 53 68 6f 72 74 29 21 3d 30 20 7c 7c 20 70 4d 65 Short)!=0 || pMe
182a2 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 53 68 6f m->z!=pMem->zSho
182a3 72 74 20 29 3b 0a 20 20 20 20 2f 2a 20 4e 6f 20 rt );. /* No
182a4 64 65 73 74 72 75 63 74 6f 72 20 75 6e 6c 65 73 destructor unles
182a5 73 20 74 68 65 72 65 20 69 73 20 4d 45 4d 5f 44 s there is MEM_D
182a6 79 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 yn */. assert
182a7 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d 30 20 ( pMem->xDel==0
182a8 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 || (pMem->flags
182a9 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 29 3b & MEM_Dyn)!=0 );
182aa 0a 0a 20 20 20 20 69 66 28 20 28 66 6c 61 67 73 .. if( (flags
182ab 20 26 20 4d 45 4d 5f 53 74 72 29 20 29 7b 0a 20 & MEM_Str) ){.
182ac 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 assert( pMe
182ad 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 m->enc==SQLITE_U
182ae 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 TF8 || .
182af 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 3d pMem->enc=
182b0 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 =SQLITE_UTF16BE
182b1 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ||.
182b2 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 pMem->enc==SQLI
182b3 54 45 5f 55 54 46 31 36 4c 45 20 0a 20 20 20 20 TE_UTF16LE .
182b4 20 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 49 66 );. /* If
182b5 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 55 the string is U
182b6 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 61 6e 64 TF-8 encoded and
182b7 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 2c nul terminated,
182b8 20 74 68 65 6e 20 70 4d 65 6d 2d 3e 6e 0a 20 20 then pMem->n.
182b9 20 20 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 74 ** must be t
182ba 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 he length of the
182bb 20 73 74 72 69 6e 67 2e 20 20 28 4c 61 74 65 72 string. (Later
182bc 3a 29 20 20 49 66 20 74 68 65 20 64 61 74 61 62 :) If the datab
182bd 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a ase file. *
182be 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72 75 * has been corru
182bf 70 74 65 64 2c 20 27 5c 30 30 30 27 20 63 68 61 pted, '\000' cha
182c0 72 61 63 74 65 72 73 20 6d 69 67 68 74 20 68 61 racters might ha
182c1 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64 ve been inserted
182c2 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 . ** into t
182c3 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 65 he middle of the
182c4 20 73 74 72 69 6e 67 2e 20 20 49 6e 20 74 68 61 string. In tha
182c5 74 20 63 61 73 65 2c 20 74 68 65 20 73 71 6c 69 t case, the sqli
182c6 74 65 33 53 74 72 6c 65 6e 33 30 28 29 0a 20 20 te3Strlen30().
182c7 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 ** might be
182c8 6c 65 73 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 less.. */.
182c9 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 if( pMem->e
182ca 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc==SQLITE_UTF8
182cb 26 26 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f && (flags & MEM_
182cc 54 65 72 6d 29 20 29 7b 20 0a 20 20 20 20 20 20 Term) ){ .
182cd 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
182ce 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 3Strlen30(pMem->
182cf 7a 29 3c 3d 70 4d 65 6d 2d 3e 6e 20 29 3b 0a 20 z)<=pMem->n );.
182d0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
182d1 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 3d Mem->z[pMem->n]=
182d2 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 =0 );. }.
182d3 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
182d4 20 2f 2a 20 43 61 6e 6e 6f 74 20 64 65 66 69 6e /* Cannot defin
182d5 65 20 61 20 73 74 72 69 6e 67 20 73 75 62 74 79 e a string subty
182d6 70 65 20 66 6f 72 20 6e 6f 6e 2d 73 74 72 69 6e pe for non-strin
182d7 67 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 20 g objects */.
182d8 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
182d9 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 61 flags & (MEM_Sta
182da 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f tic|MEM_Dyn|MEM_
182db 45 70 68 65 6d 7c 4d 45 4d 5f 53 68 6f 72 74 29 Ephem|MEM_Short)
182dc 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 )==0 );. asse
182dd 72 74 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d rt( pMem->xDel==
182de 30 20 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 4d 45 0 );. }. /* ME
182df 4d 5f 4e 75 6c 6c 20 65 78 63 6c 75 64 65 73 20 M_Null excludes
182e0 61 6c 6c 20 6f 74 68 65 72 20 74 79 70 65 73 20 all other types
182e1 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d */. assert( (pM
182e2 65 6d 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 em->flags&(MEM_S
182e3 74 72 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 tr|MEM_Int|MEM_R
182e4 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d eal|MEM_Blob))==
182e5 30 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 0. || (
182e6 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
182e7 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 2f 2a Null)==0 );. /*
182e8 20 49 66 20 74 68 65 20 4d 45 4d 20 69 73 20 62 If the MEM is b
182e9 6f 74 68 20 72 65 61 6c 20 61 6e 64 20 69 6e 74 oth real and int
182ea 65 67 65 72 2c 20 74 68 65 20 76 61 6c 75 65 73 eger, the values
182eb 20 61 72 65 20 65 71 75 61 6c 20 2a 2f 0a 20 20 are equal */.
182ec 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 assert( (pMem->f
182ed 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c lags & (MEM_Int|
182ee 4d 45 4d 5f 52 65 61 6c 29 29 21 3d 28 4d 45 4d MEM_Real))!=(MEM
182ef 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 0a _Int|MEM_Real) .
182f0 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 4d 65 || pMe
182f1 6d 2d 3e 72 3d 3d 70 4d 65 6d 2d 3e 75 2e 69 20 m->r==pMem->u.i
182f2 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 );.}.#endif../*
182f3 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
182f4 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
182f5 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 internally, it i
182f6 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 s not part of th
182f7 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 e.** external AP
182f8 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 I. It works in a
182f9 20 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 similar way to
182fa 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
182fb 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 xt(),.** except
182fc 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 the data returne
182fd 64 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f d is in the enco
182fe 64 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 ding specified b
182ff 79 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 y the second.**
18300 70 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 parameter, which
18301 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 must be one of
18302 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 SQLITE_UTF16BE,
18303 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f SQLITE_UTF16LE o
18304 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 r.** SQLITE_UTF8
18305 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 ..**.** (2006-02
18306 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 -16:) The enc v
18307 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 alue can be or-e
18308 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 d with SQLITE_UT
18309 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 F16_ALIGNED..**
1830a 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 If that is the c
1830b 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 ase, then the re
1830c 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 sult must be ali
1830d 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 gned on an even
1830e 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 byte.** boundary
1830f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18310 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a ATE const void *
18311 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
18312 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 (sqlite3_value*
18313 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 pVal, u8 enc){.
18314 20 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 if( !pVal ) ret
18315 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 urn 0;.. assert
18316 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c ( pVal->db==0 ||
18317 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
18318 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 eld(pVal->db->mu
18319 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
1831a 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 ( (enc&3)==(enc&
1831b 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c ~SQLITE_UTF16_AL
1831c 49 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 73 65 IGNED) );. asse
1831d 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 rt( (pVal->flags
1831e 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
1831f 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c 0 );.. if( pVal
18320 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c ->flags&MEM_Null
18321 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
18322 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
18323 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d (MEM_Blob>>3) ==
18324 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56 MEM_Str );. pV
18325 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56 al->flags |= (pV
18326 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f al->flags & MEM_
18327 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61 Blob)>>3;. expa
18328 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20 ndBlob(pVal);.
18329 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 if( pVal->flags&
1832a 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73 MEM_Str ){. s
1832b 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1832c 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65 Encoding(pVal, e
1832d 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 nc & ~SQLITE_UTF
1832e 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20 16_ALIGNED);.
1832f 20 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49 if( (enc & SQLI
18330 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 TE_UTF16_ALIGNED
18331 29 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 53 51 )!=0 && 1==(1&SQ
18332 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 LITE_PTR_TO_INT(
18333 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 20 20 pVal->z)) ){.
18334 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c assert( (pVal
18335 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 45 ->flags & (MEM_E
18336 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 phem|MEM_Static)
18337 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 )!=0 );. if
18338 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
18339 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 56 MakeWriteable(pV
1833a 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 al)!=SQLITE_OK )
1833b 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
1833c 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
1833d 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
1833e 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 eMemNulTerminate
1833f 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b (pVal);. }else{
18340 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56 . assert( (pV
18341 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c al->flags&MEM_Bl
18342 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 ob)==0 );. sq
18343 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 lite3VdbeMemStri
18344 6e 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63 29 ngify(pVal, enc)
18345 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d ;. assert( 0=
18346 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 =(1&SQLITE_PTR_T
18347 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 O_INT(pVal->z))
18348 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 );. }. assert(
18349 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 pVal->enc==(enc
1834a 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f & ~SQLITE_UTF16_
1834b 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70 56 61 6c ALIGNED) || pVal
1834c 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20 20 20 20 ->db==0.
1834d 20 20 20 20 20 20 7c 7c 20 70 56 61 6c 2d 3e 64 || pVal->d
1834e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1834f 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65 );. if( pVal->e
18350 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 nc==(enc & ~SQLI
18351 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 TE_UTF16_ALIGNED
18352 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
18353 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 pVal->z;. }else
18354 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
18355 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 }.}../*.** Cre
18356 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 ate a new sqlite
18357 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a 3_value object..
18358 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18359 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 E sqlite3_value
1835a 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 *sqlite3ValueNew
1835b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
1835c 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c 69 74 65 Mem *p = sqlite
1835d 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
1835e 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 , sizeof(*p));.
1835f 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d if( p ){. p-
18360 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c >flags = MEM_Nul
18361 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70 65 20 3d l;. p->type =
18362 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 SQLITE_NULL;.
18363 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 p->db = db;.
18364 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
18365 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
18366 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 new sqlite3_valu
18367 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69 e object, contai
18368 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f ning the value o
18369 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 f pExpr..**.** T
1836a 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 his only works f
1836b 6f 72 20 76 65 72 79 20 73 69 6d 70 6c 65 20 65 or very simple e
1836c 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 xpressions that
1836d 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 63 consist of one c
1836e 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e onstant.** token
1836f 20 28 69 2e 65 2e 20 22 35 22 2c 20 22 35 2e 31 (i.e. "5", "5.1
18370 22 2c 20 22 27 61 20 73 74 72 69 6e 67 27 22 29 ", "'a string'")
18371 2e 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 . If the express
18372 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f ion can.** be co
18373 6e 76 65 72 74 65 64 20 64 69 72 65 63 74 6c 79 nverted directly
18374 20 69 6e 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 into a value, t
18375 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 hen the value is
18376 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a allocated and.*
18377 2a 20 61 20 70 6f 69 6e 74 65 72 20 77 72 69 74 * a pointer writ
18378 74 65 6e 20 74 6f 20 2a 70 70 56 61 6c 2e 20 54 ten to *ppVal. T
18379 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 he caller is res
1837a 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61 ponsible for dea
1837b 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 74 68 65 llocating.** the
1837c 20 76 61 6c 75 65 20 62 79 20 70 61 73 73 69 6e value by passin
1837d 67 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 56 g it to sqlite3V
1837e 61 6c 75 65 46 72 65 65 28 29 20 6c 61 74 65 72 alueFree() later
1837f 20 6f 6e 2e 20 49 66 20 74 68 65 20 65 78 70 72 on. If the expr
18380 65 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 ession.** cannot
18381 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f be converted to
18382 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a a value, then *
18383 70 70 56 61 6c 20 69 73 20 73 65 74 20 74 6f 20 ppVal is set to
18384 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f NULL..*/.SQLITE_
18385 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18386 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 te3ValueFromExpr
18387 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
18388 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18389 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
1838a 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 nnection */. Ex
1838b 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 pr *pExpr,
1838c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 /* The e
1838d 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 65 76 61 xpression to eva
1838e 6c 75 61 74 65 20 2a 2f 0a 20 20 75 38 20 65 6e luate */. u8 en
1838f 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
18390 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 /* Encoding
18391 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 75 38 20 to use */. u8
18392 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 20 affinity,
18393 20 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 /* Affini
18394 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 73 ty to use */. s
18395 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 qlite3_value **p
18396 70 56 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 pVal /* Writ
18397 65 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 e the new value
18398 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
18399 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61 op;. char *zVa
1839a 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 l = 0;. sqlite3
1839b 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30 _value *pVal = 0
1839c 3b 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20 ;.. if( !pExpr
1839d 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20 ){. *ppVal =
1839e 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 0;. return SQ
1839f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f LITE_OK;. }. o
183a0 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 0a p = pExpr->op;..
183a1 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 52 if( op==TK_STR
183a2 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c ING || op==TK_FL
183a3 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e OAT || op==TK_IN
183a4 54 45 47 45 52 20 29 7b 0a 20 20 20 20 7a 56 61 TEGER ){. zVa
183a5 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 l = sqlite3DbStr
183a6 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 NDup(db, (char*)
183a7 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 pExpr->token.z,
183a8 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b pExpr->token.n);
183a9 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 . pVal = sqli
183aa 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 3b te3ValueNew(db);
183ab 0a 20 20 20 20 69 66 28 20 21 7a 56 61 6c 20 7c . if( !zVal |
183ac 7c 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e | !pVal ) goto n
183ad 6f 5f 6d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 o_mem;. sqlit
183ae 65 33 44 65 71 75 6f 74 65 28 7a 56 61 6c 29 3b e3Dequote(zVal);
183af 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 . sqlite3Valu
183b0 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 eSetStr(pVal, -1
183b1 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 , zVal, SQLITE_U
183b2 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 TF8, SQLITE_DYNA
183b3 4d 49 43 29 3b 0a 20 20 20 20 69 66 28 20 28 6f MIC);. if( (o
183b4 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c p==TK_INTEGER ||
183b5 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 op==TK_FLOAT )
183b6 26 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c && affinity==SQL
183b7 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a ITE_AFF_NONE ){.
183b8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c sqlite3Val
183b9 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 ueApplyAffinity(
183ba 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 pVal, SQLITE_AFF
183bb 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63 29 3b 0a _NUMERIC, enc);.
183bc 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
183bd 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 sqlite3ValueApp
183be 6c 79 41 66 66 69 6e 69 74 79 28 70 56 61 6c 2c lyAffinity(pVal,
183bf 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 29 3b affinity, enc);
183c0 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
183c1 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 f( op==TK_UMINUS
183c2 20 29 20 7b 0a 20 20 20 20 69 66 28 20 53 51 4c ) {. if( SQL
183c3 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 ITE_OK==sqlite3V
183c4 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64 62 2c alueFromExpr(db,
183c5 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 pExpr->pLeft,enc
183c6 2c 61 66 66 69 6e 69 74 79 2c 26 70 56 61 6c 29 ,affinity,&pVal)
183c7 20 29 7b 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e ){. pVal->
183c8 75 2e 69 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d u.i = -1 * pVal-
183c9 3e 75 2e 69 3b 0a 20 20 20 20 20 20 70 56 61 6c >u.i;. pVal
183ca 2d 3e 72 20 3d 20 2d 31 2e 30 20 2a 20 70 56 61 ->r = -1.0 * pVa
183cb 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a l->r;. }. }.
183cc 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
183cd 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c MIT_BLOB_LITERAL
183ce 0a 20 20 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d . else if( op==
183cf 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 69 TK_BLOB ){. i
183d0 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61 73 73 nt nVal;. ass
183d1 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 ert( pExpr->toke
183d2 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20 20 61 73 n.n>=3 );. as
183d3 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b sert( pExpr->tok
183d4 65 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 en.z[0]=='x' ||
183d5 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 pExpr->token.z[0
183d6 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 61 73 ]=='X' );. as
183d7 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b sert( pExpr->tok
183d8 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b en.z[1]=='\'' );
183d9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 . assert( pEx
183da 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 45 78 70 pr->token.z[pExp
183db 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d 27 r->token.n-1]=='
183dc 5c 27 27 20 29 3b 0a 20 20 20 20 70 56 61 6c 20 \'' );. pVal
183dd 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 = sqlite3ValueNe
183de 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 21 w(db);. if( !
183df 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d pVal ) goto no_m
183e0 65 6d 3b 0a 20 20 20 20 6e 56 61 6c 20 3d 20 70 em;. nVal = p
183e1 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 2d 20 Expr->token.n -
183e2 33 3b 0a 20 20 20 20 7a 56 61 6c 20 3d 20 28 63 3;. zVal = (c
183e3 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 har*)pExpr->toke
183e4 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 73 71 6c n.z + 2;. sql
183e5 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
183e6 72 28 70 56 61 6c 2c 20 73 71 6c 69 74 65 33 48 r(pVal, sqlite3H
183e7 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20 7a 56 61 exToBlob(db, zVa
183e8 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 6c 2f 32 l, nVal), nVal/2
183e9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
183ea 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 53 51 0, SQ
183eb 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 LITE_DYNAMIC);.
183ec 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2a 70 70 }.#endif.. *pp
183ed 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 20 72 65 Val = pVal;. re
183ee 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
183ef 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d .no_mem:. db->m
183f0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
183f1 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
183f2 28 64 62 2c 20 7a 56 61 6c 29 3b 0a 20 20 73 71 (db, zVal);. sq
183f3 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 lite3ValueFree(p
183f4 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61 6c 20 3d Val);. *ppVal =
183f5 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 0;. return SQL
183f6 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a ITE_NOMEM;.}../*
183f7 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 .** Change the s
183f8 74 72 69 6e 67 20 76 61 6c 75 65 20 6f 66 20 61 tring value of a
183f9 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 n sqlite3_value
183fa 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 object.*/.SQLITE
183fb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
183fc 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 lite3ValueSetStr
183fd 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 (. sqlite3_valu
183fe 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20 56 61 6c e *v, /* Val
183ff 75 65 20 74 6f 20 62 65 20 73 65 74 20 2a 2f 0a ue to be set */.
18400 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 int n,
18401 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 /* Lengt
18402 68 20 6f 66 20 73 74 72 69 6e 67 20 7a 20 2a 2f h of string z */
18403 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
18404 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 , /* Text
18405 20 6f 66 20 74 68 65 20 6e 65 77 20 73 74 72 69 of the new stri
18406 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 ng */. u8 enc,
18407 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18408 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 Encoding to use
18409 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 */. void (*xDe
1840a 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f 2a 20 44 l)(void*) /* D
1840b 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 estructor for th
1840c 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 e string */.){.
1840d 20 69 66 28 20 76 20 29 20 73 71 6c 69 74 65 33 if( v ) sqlite3
1840e 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 28 4d VdbeMemSetStr((M
1840f 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c 20 65 6e em *)v, z, n, en
18410 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a c, xDel);.}../*.
18411 2a 2a 20 46 72 65 65 20 61 6e 20 73 71 6c 69 74 ** Free an sqlit
18412 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a e3_value object.
18413 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18414 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 E void sqlite3Va
18415 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f lueFree(sqlite3_
18416 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20 69 66 28 value *v){. if(
18417 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 !v ) return;.
18418 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
18419 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29 76 29 3b lease((Mem *)v);
1841a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1841b 28 28 28 4d 65 6d 2a 29 76 29 2d 3e 64 62 2c 20 (((Mem*)v)->db,
1841c 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 v);.}../*.** Ret
1841d 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1841e 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 73 f bytes in the s
1841f 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
18420 65 63 74 20 61 73 73 75 6d 69 6e 67 0a 2a 2a 20 ect assuming.**
18421 74 68 61 74 20 69 74 20 75 73 65 73 20 74 68 65 that it uses the
18422 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e 63 22 0a encoding "enc".
18423 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18424 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c E int sqlite3Val
18425 75 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f ueBytes(sqlite3_
18426 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 75 38 20 value *pVal, u8
18427 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d enc){. Mem *p =
18428 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 (Mem*)pVal;. i
18429 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d f( (p->flags & M
1842a 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c 7c 20 73 EM_Blob)!=0 || s
1842b 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
1842c 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b 0a 20 20 pVal, enc) ){.
1842d 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 if( p->flags &
1842e 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 MEM_Zero ){.
1842f 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 20 2b return p->n +
18430 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 p->u.nZero;.
18431 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 }else{. re
18432 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20 7d turn p->n;. }
18433 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
18434 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
18435 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 6d *** End of vdbem
18436 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a em.c ***********
18437 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18438 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18439 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1843a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 *** Begin file v
1843b 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a dbeaux.c *******
1843c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1843d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1843e 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 **/./*.** 2003 S
1843f 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a eptember 6.**.**
18440 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
18441 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
18442 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
18443 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
18444 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
18445 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
18446 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
18447 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
18448 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
18449 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1844a 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1844b 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1844c 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1844d 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1844e 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1844f 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
18450 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
18451 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18452 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18453 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18454 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18455 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
18456 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
18457 63 6f 64 65 20 75 73 65 64 20 66 6f 72 20 63 72 code used for cr
18458 65 61 74 69 6e 67 2c 20 64 65 73 74 72 6f 79 69 eating, destroyi
18459 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 74 69 ng, and populati
1845a 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 6f 72 ng.** a VDBE (or
1845b 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d an "sqlite3_stm
1845c 74 22 20 61 73 20 69 74 20 69 73 20 6b 6e 6f 77 t" as it is know
1845d 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 69 64 65 n to the outside
1845e 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f 72 0a world.) Prior.
1845f 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 32 2e ** to version 2.
18460 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 63 6f 8.7, all this co
18461 64 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 64 20 de was combined
18462 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 63 20 into the vdbe.c
18463 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 20 source file..**
18464 42 75 74 20 74 68 61 74 20 66 69 6c 65 20 77 61 But that file wa
18465 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 62 69 s getting too bi
18466 67 20 73 6f 20 74 68 69 73 20 73 75 62 72 6f 75 g so this subrou
18467 74 69 6e 65 73 20 77 65 72 65 20 73 70 6c 69 74 tines were split
18468 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a out..**.** $Id:
18469 20 76 64 62 65 61 75 78 2e 63 2c 76 20 31 2e 34 vdbeaux.c,v 1.4
1846a 33 30 20 32 30 30 39 2f 30 31 2f 30 37 20 30 38 30 2009/01/07 08
1846b 3a 31 32 3a 31 36 20 64 61 6e 69 65 6c 6b 31 39 :12:16 danielk19
1846c 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f 77 Exp $.*/..../
1846d 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62 75 67 67 *.** When debugg
1846e 69 6e 67 20 74 68 65 20 63 6f 64 65 20 67 65 6e ing the code gen
1846f 65 72 61 74 6f 72 20 69 6e 20 61 20 73 79 6d 62 erator in a symb
18470 6f 6c 69 63 20 64 65 62 75 67 67 65 72 2c 20 6f olic debugger, o
18471 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68 ne can.** set th
18472 65 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 e sqlite3VdbeAdd
18473 6f 70 54 72 61 63 65 20 74 6f 20 31 20 61 6e 64 opTrace to 1 and
18474 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c all opcodes wil
18475 6c 20 62 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20 l be printed.**
18476 61 73 20 74 68 65 79 20 61 72 65 20 61 64 64 65 as they are adde
18477 64 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 d to the instruc
18478 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a tion stream..*/.
18479 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
1847a 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
1847b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
1847c 62 65 41 64 64 6f 70 54 72 61 63 65 20 3d 20 30 beAddopTrace = 0
1847d 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a ;.#endif.../*.**
1847e 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 Create a new vi
1847f 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 rtual database e
18480 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ngine..*/.SQLITE
18481 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 _PRIVATE Vdbe *s
18482 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 qlite3VdbeCreate
18483 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
18484 20 56 64 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20 Vdbe *p;. p =
18485 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
18486 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 56 ero(db, sizeof(V
18487 64 62 65 29 20 29 3b 0a 20 20 69 66 28 20 70 3d dbe) );. if( p=
18488 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
18489 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69 p->db = db;. i
1848a 66 28 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a f( db->pVdbe ){.
1848b 20 20 20 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70 db->pVdbe->p
1848c 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 Prev = p;. }.
1848d 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70 p->pNext = db->p
1848e 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76 Vdbe;. p->pPrev
1848f 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62 = 0;. db->pVdb
18490 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69 e = p;. p->magi
18491 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 c = VDBE_MAGIC_I
18492 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b NIT;. return p;
18493 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 .}../*.** Rememb
18494 65 72 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e er the SQL strin
18495 67 20 66 6f 72 20 61 20 70 72 65 70 61 72 65 64 g for a prepared
18496 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 statement..*/.S
18497 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
18498 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 id sqlite3VdbeSe
18499 74 53 71 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f tSql(Vdbe *p, co
1849a 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
1849b 20 6e 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 n){. if( p==0
1849c 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
1849d 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29 rt( p->zSql==0 )
1849e 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 71 ;. p->zSql = sq
1849f 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 lite3DbStrNDup(p
184a0 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 7d 0a 0a ->db, z, n);.}..
184a1 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
184a2 20 53 51 4c 20 61 73 73 6f 63 69 61 74 65 64 20 SQL associated
184a3 77 69 74 68 20 61 20 70 72 65 70 61 72 65 64 20 with a prepared
184a4 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c statement.*/.SQL
184a5 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
184a6 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 ar *sqlite3_sql(
184a7 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
184a8 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 tmt){. return (
184a9 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 2d 3e (Vdbe *)pStmt)->
184aa 7a 53 71 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 zSql;.}../*.** S
184ab 77 61 70 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 wap all content
184ac 62 65 74 77 65 65 6e 20 74 77 6f 20 56 44 42 45 between two VDBE
184ad 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a structures..*/.
184ae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
184af 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 oid sqlite3VdbeS
184b0 77 61 70 28 56 64 62 65 20 2a 70 41 2c 20 56 64 wap(Vdbe *pA, Vd
184b1 62 65 20 2a 70 42 29 7b 0a 20 20 56 64 62 65 20 be *pB){. Vdbe
184b2 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68 tmp, *pTmp;. ch
184b3 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 69 6e 74 20 ar *zTmp;. int
184b4 6e 54 6d 70 3b 0a 20 20 74 6d 70 20 3d 20 2a 70 nTmp;. tmp = *p
184b5 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b 0a A;. *pA = *pB;.
184b6 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a 20 20 70 *pB = tmp;. p
184b7 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74 3b Tmp = pA->pNext;
184b8 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d 20 70 . pA->pNext = p
184b9 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 42 2d 3e B->pNext;. pB->
184ba 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b 0a 20 20 pNext = pTmp;.
184bb 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50 72 65 76 pTmp = pA->pPrev
184bc 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76 20 3d 20 ;. pA->pPrev =
184bd 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20 70 42 2d pB->pPrev;. pB-
184be 3e 70 50 72 65 76 20 3d 20 70 54 6d 70 3b 0a 20 >pPrev = pTmp;.
184bf 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a 53 71 6c zTmp = pA->zSql
184c0 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20 3d 20 70 ;. pA->zSql = p
184c1 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e 7a B->zSql;. pB->z
184c2 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20 20 6e 54 Sql = zTmp;. nT
184c3 6d 70 20 3d 20 70 41 2d 3e 6e 53 71 6c 3b 0a 20 mp = pA->nSql;.
184c4 20 70 41 2d 3e 6e 53 71 6c 20 3d 20 70 42 2d 3e pA->nSql = pB->
184c5 6e 53 71 6c 3b 0a 20 20 70 42 2d 3e 6e 53 71 6c nSql;. pB->nSql
184c6 20 3d 20 6e 54 6d 70 3b 0a 7d 0a 0a 23 69 66 64 = nTmp;.}..#ifd
184c7 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
184c8 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61 63 69 /*.** Turn traci
184c9 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a ng on or off.*/.
184ca 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
184cb 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54 oid sqlite3VdbeT
184cc 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20 46 49 race(Vdbe *p, FI
184cd 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d LE *trace){. p-
184ce 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65 3b 0a >trace = trace;.
184cf 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
184d0 52 65 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e Resize the Vdbe.
184d1 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74 68 61 aOp array so tha
184d2 74 20 69 74 20 69 73 20 61 74 20 6c 65 61 73 74 t it is at least
184d3 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 20 74 one op larger t
184d4 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73 2e 0a han .** it was..
184d5 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d **.** If an out-
184d6 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20 of-memory error
184d7 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 73 occurs while res
184d8 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79 2c izing the array,
184d9 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 return.** SQLIT
184da 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 69 73 E_NOMEM. In this
184db 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70 20 61 case Vdbe.aOp a
184dc 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63 nd Vdbe.nOpAlloc
184dd 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e 63 68 remain .** unch
184de 61 6e 67 65 64 20 28 74 68 69 73 20 69 73 20 73 anged (this is s
184df 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63 6f 64 o that any opcod
184e0 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 es already alloc
184e1 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a 2a 20 ated can be .**
184e2 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c 6c 6f correctly deallo
184e3 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 cated along with
184e4 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 the rest of the
184e5 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 Vdbe)..*/.stati
184e6 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72 72 61 c int growOpArra
184e7 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 56 64 y(Vdbe *p){. Vd
184e8 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e beOp *pNew;. in
184e9 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e 4f 70 t nNew = (p->nOp
184ea 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c Alloc ? p->nOpAl
184eb 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28 31 30 loc*2 : (int)(10
184ec 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 29 3b 24/sizeof(Op)));
184ed 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
184ee 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62 3DbRealloc(p->db
184ef 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73 , p->aOp, nNew*s
184f0 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66 izeof(Op));. if
184f1 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d ( pNew ){. p-
184f2 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 6e 4e 65 77 >nOpAlloc = nNew
184f3 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70 ;. p->aOp = p
184f4 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 New;. }. retur
184f5 6e 20 28 70 4e 65 77 20 3f 20 53 51 4c 49 54 45 n (pNew ? SQLITE
184f6 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d _OK : SQLITE_NOM
184f7 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 EM);.}../*.** Ad
184f8 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74 d a new instruct
184f9 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 ion to the list
184fa 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 of instructions
184fb 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a current in the.*
184fc 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20 * VDBE. Return
184fd 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 the address of t
184fe 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 he new instructi
184ff 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 on..**.** Parame
18500 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 ters:.**.** p
18501 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
18502 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44 ointer to the VD
18503 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 BE.**.** op
18504 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
18505 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20 opcode for this
18506 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a instruction.**.*
18507 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20 * p1, p2, p3
18508 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a Operands.**
18509 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69 .** Use the sqli
1850a 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
1850b 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 bel() function t
1850c 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73 o fix an address
1850d 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69 and.** the sqli
1850e 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
1850f 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68 ) function to ch
18510 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f ange the value o
18511 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72 f the P4.** oper
18512 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 and..*/.SQLITE_P
18513 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18514 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 e3VdbeAddOp3(Vdb
18515 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e e *p, int op, in
18516 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e t p1, int p2, in
18517 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a t p3){. int i;.
18518 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a VdbeOp *pOp;..
18519 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 i = p->nOp;.
1851a 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
1851b 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
1851c 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f T );. assert( o
1851d 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29 p>0 && op<0xff )
1851e 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41 6c ;. if( p->nOpAl
1851f 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 20 69 66 loc<=i ){. if
18520 28 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29 ( growOpArray(p)
18521 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
18522 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0;. }. }.
18523 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20 p->nOp++;. pOp
18524 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 = &p->aOp[i];.
18525 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 28 75 pOp->opcode = (u
18526 38 29 6f 70 3b 0a 20 20 70 4f 70 2d 3e 70 35 20 8)op;. pOp->p5
18527 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d = 0;. pOp->p1 =
18528 20 70 31 3b 0a 20 20 70 4f 70 2d 3e 70 32 20 3d p1;. pOp->p2 =
18529 20 70 32 3b 0a 20 20 70 4f 70 2d 3e 70 33 20 3d p2;. pOp->p3 =
1852a 20 70 33 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 p3;. pOp->p4.p
1852b 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 34 74 = 0;. pOp->p4t
1852c 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 ype = P4_NOTUSED
1852d 3b 0a 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d ;. p->expired =
1852e 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 0;.#ifdef SQLIT
1852f 45 5f 44 45 42 55 47 0a 20 20 70 4f 70 2d 3e 7a E_DEBUG. pOp->z
18530 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 69 Comment = 0;. i
18531 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 f( sqlite3VdbeAd
18532 64 6f 70 54 72 61 63 65 20 29 20 73 71 6c 69 74 dopTrace ) sqlit
18533 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c e3VdbePrintOp(0,
18534 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b i, &p->aOp[i]);
18535 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56 .#endif.#ifdef V
18536 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 70 4f DBE_PROFILE. pO
18537 70 2d 3e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 p->cycles = 0;.
18538 20 70 4f 70 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23 pOp->cnt = 0;.#
18539 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 69 endif. return i
1853a 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
1853b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
1853c 62 65 41 64 64 4f 70 30 28 56 64 62 65 20 2a 70 beAddOp0(Vdbe *p
1853d 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 72 65 74 , int op){. ret
1853e 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41 urn sqlite3VdbeA
1853f 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20 ddOp3(p, op, 0,
18540 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 0, 0);.}.SQLITE_
18541 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18542 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64 te3VdbeAddOp1(Vd
18543 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 be *p, int op, i
18544 6e 74 20 70 31 29 7b 0a 20 20 72 65 74 75 72 6e nt p1){. return
18545 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
18546 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c p3(p, op, p1, 0,
18547 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 0);.}.SQLITE_PR
18548 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
18549 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 3VdbeAddOp2(Vdbe
1854a 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 *p, int op, int
1854b 20 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20 p1, int p2){.
1854c 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 return sqlite3Vd
1854d 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 beAddOp3(p, op,
1854e 70 31 2c 20 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a p1, p2, 0);.}...
1854f 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 /*.** Add an opc
18550 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 ode that include
18551 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 s the p4 value a
18552 73 20 61 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a s a pointer..*/.
18553 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
18554 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
18555 64 4f 70 34 28 0a 20 20 56 64 62 65 20 2a 70 2c dOp4(. Vdbe *p,
18556 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
18557 64 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f dd the opcode to
18558 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e this VM */. in
18559 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 t op,
1855a 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63 /* The new opc
1855b 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c ode */. int p1,
1855c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1855d 54 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a The P1 operand *
1855e 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20 /. int p2,
1855f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 /* The P
18560 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 2 operand */. i
18561 6e 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20 nt p3,
18562 20 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65 /* The P3 ope
18563 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 rand */. const
18564 63 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a char *zP4, /*
18565 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 The P4 operand
18566 2a 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 20 */. int p4type
18567 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 6f /* P4 o
18568 70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0a 29 perand type */.)
18569 7b 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 {. int addr = s
1856a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1856b 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 (p, op, p1, p2,
1856c 70 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 p3);. sqlite3Vd
1856d 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 64 beChangeP4(p, ad
1856e 64 72 2c 20 7a 50 34 2c 20 70 34 74 79 70 65 29 dr, zP4, p4type)
1856f 3b 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b ;. return addr;
18570 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 .}../*.** Create
18571 20 61 20 6e 65 77 20 73 79 6d 62 6f 6c 69 63 20 a new symbolic
18572 6c 61 62 65 6c 20 66 6f 72 20 61 6e 20 69 6e 73 label for an ins
18573 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 68 61 truction that ha
18574 73 20 79 65 74 20 74 6f 20 62 65 0a 2a 2a 20 63 s yet to be.** c
18575 6f 64 65 64 2e 20 20 54 68 65 20 73 79 6d 62 6f oded. The symbo
18576 6c 69 63 20 6c 61 62 65 6c 20 69 73 20 72 65 61 lic label is rea
18577 6c 6c 79 20 6a 75 73 74 20 61 20 6e 65 67 61 74 lly just a negat
18578 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 ive number. The
18579 0a 2a 2a 20 6c 61 62 65 6c 20 63 61 6e 20 62 65 .** label can be
1857a 20 75 73 65 64 20 61 73 20 74 68 65 20 50 32 20 used as the P2
1857b 76 61 6c 75 65 20 6f 66 20 61 6e 20 6f 70 65 72 value of an oper
1857c 61 74 69 6f 6e 2e 20 20 4c 61 74 65 72 2c 20 77 ation. Later, w
1857d 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c hen.** the label
1857e 20 69 73 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 is resolved to
1857f 61 20 73 70 65 63 69 66 69 63 20 61 64 64 72 65 a specific addre
18580 73 73 2c 20 74 68 65 20 56 44 42 45 20 77 69 6c ss, the VDBE wil
18581 6c 20 73 63 61 6e 0a 2a 2a 20 74 68 72 6f 75 67 l scan.** throug
18582 68 20 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20 h its operation
18583 6c 69 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20 list and change
18584 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32 all values of P2
18585 20 77 68 69 63 68 20 6d 61 74 63 68 0a 2a 2a 20 which match.**
18586 74 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74 the label into t
18587 68 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72 he resolved addr
18588 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 ess..**.** The V
18589 44 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20 61 DBE knows that a
1858a 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c P2 value is a l
1858b 61 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61 62 abel because lab
1858c 65 6c 73 20 61 72 65 0a 2a 2a 20 61 6c 77 61 79 els are.** alway
1858d 73 20 6e 65 67 61 74 69 76 65 20 61 6e 64 20 50 s negative and P
1858e 32 20 76 61 6c 75 65 73 20 61 72 65 20 73 75 70 2 values are sup
1858f 70 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e 2d 6e pose to be non-n
18590 65 67 61 74 69 76 65 2e 0a 2a 2a 20 48 65 6e 63 egative..** Henc
18591 65 2c 20 61 20 6e 65 67 61 74 69 76 65 20 50 32 e, a negative P2
18592 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 value is a labe
18593 6c 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74 l that has yet t
18594 6f 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a o be resolved..*
18595 2a 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 72 65 74 *.** Zero is ret
18596 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f urned if a mallo
18597 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 c() fails..*/.SQ
18598 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
18599 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
1859a 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 29 7b 0a Label(Vdbe *p){.
1859b 20 20 69 6e 74 20 69 3b 0a 20 20 69 20 3d 20 70 int i;. i = p
1859c 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a 20 20 61 73 ->nLabel++;. as
1859d 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d sert( p->magic==
1859e 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 VDBE_MAGIC_INIT
1859f 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e );. if( i>=p->n
185a0 4c 61 62 65 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20 LabelAlloc ){.
185a1 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 4c 61 int n = p->nLa
185a2 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 35 3b 0a belAlloc*2 + 5;.
185a3 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 p->aLabel =
185a4 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
185a5 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d OrFree(p->db, p-
185a6 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20 20 >aLabel,.
185a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185a9 6e 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 n*sizeof(p->aLab
185aa 65 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 70 2d 3e el[0]));. p->
185ab 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 3d 20 73 71 nLabelAlloc = sq
185ac 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a lite3DbMallocSiz
185ad 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 e(p->db, p->aLab
185ae 65 6c 29 2f 73 69 7a 65 6f 66 28 70 2d 3e 61 4c el)/sizeof(p->aL
185af 61 62 65 6c 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20 abel[0]);. }.
185b0 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b if( p->aLabel ){
185b1 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 69 . p->aLabel[i
185b2 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65 ] = -1;. }. re
185b3 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a 2f 2a turn -1-i;.}../*
185b4 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6c 61 62 65 .** Resolve labe
185b5 6c 20 22 78 22 20 74 6f 20 62 65 20 74 68 65 20 l "x" to be the
185b6 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e address of the n
185b7 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
185b8 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 65 72 74 65 to.** be inserte
185b9 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 d. The paramete
185ba 72 20 22 78 22 20 6d 75 73 74 20 68 61 76 65 20 r "x" must have
185bb 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 been obtained fr
185bc 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f 72 20 63 61 om.** a prior ca
185bd 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 ll to sqlite3Vdb
185be 65 4d 61 6b 65 4c 61 62 65 6c 28 29 2e 0a 2a 2f eMakeLabel()..*/
185bf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
185c0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
185c1 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62 ResolveLabel(Vdb
185c2 65 20 2a 70 2c 20 69 6e 74 20 78 29 7b 0a 20 20 e *p, int x){.
185c3 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a 20 20 int j = -1-x;.
185c4 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
185c5 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
185c6 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a T );. assert( j
185c7 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62 >=0 && j<p->nLab
185c8 65 6c 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 el );. if( p->a
185c9 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e Label ){. p->
185ca 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e 6e aLabel[j] = p->n
185cb 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a Op;. }.}../*.**
185cc 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 Loop through th
185cd 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b 69 6e e program lookin
185ce 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65 73 20 g for P2 values
185cf 74 68 61 74 20 61 72 65 20 6e 65 67 61 74 69 76 that are negativ
185d0 65 0a 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 e.** on jump ins
185d1 74 72 75 63 74 69 6f 6e 73 2e 20 20 45 61 63 68 tructions. Each
185d2 20 73 75 63 68 20 76 61 6c 75 65 20 69 73 20 61 such value is a
185d3 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c 76 65 label. Resolve
185d4 20 74 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 62 79 the.** label by
185d5 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 32 20 setting the P2
185d6 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63 6f 72 value to its cor
185d7 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 rect non-zero va
185d8 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 lue..**.** This
185d9 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
185da 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61 6c 6c d once after all
185db 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 62 65 opcodes have be
185dc 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a en inserted..**.
185dd 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70 4d 61 ** Variable *pMa
185de 78 46 75 6e 63 41 72 67 73 20 69 73 20 73 65 74 xFuncArgs is set
185df 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 to the maximum
185e0 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50 32 20 value of any P2
185e1 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 argument .** to
185e2 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 an OP_Function,
185e3 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20 4f 50 OP_AggStep or OP
185e4 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64 65 2e _VFilter opcode.
185e5 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 This is used by
185e6 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 .** sqlite3Vdbe
185e7 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f 20 73 MakeReady() to s
185e8 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61 70 41 ize the Vdbe.apA
185e9 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a rg[] array..**.*
185ea 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 * This routine a
185eb 6c 73 6f 20 64 6f 65 73 20 74 68 65 20 66 6f 6c lso does the fol
185ec 6c 6f 77 69 6e 67 20 6f 70 74 69 6d 69 7a 61 74 lowing optimizat
185ed 69 6f 6e 3a 20 20 49 74 20 73 63 61 6e 73 20 66 ion: It scans f
185ee 6f 72 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f or.** instructio
185ef 6e 73 20 74 68 61 74 20 6d 69 67 68 74 20 63 61 ns that might ca
185f0 75 73 65 20 61 20 73 74 61 74 65 6d 65 6e 74 20 use a statement
185f1 72 6f 6c 6c 62 61 63 6b 2e 20 20 53 75 63 68 20 rollback. Such
185f2 69 6e 73 74 72 75 63 74 69 6f 6e 73 0a 2a 2a 20 instructions.**
185f3 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 are:.**.** *
185f4 4f 50 5f 48 61 6c 74 20 77 69 74 68 20 50 31 3d OP_Halt with P1=
185f5 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
185f6 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62 6f 72 T and P2=OE_Abor
185f7 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 44 65 t..** * OP_De
185f8 73 74 72 6f 79 0a 2a 2a 20 20 20 2a 20 20 4f 50 stroy.** * OP
185f9 5f 56 55 70 64 61 74 65 0a 2a 2a 20 20 20 2a 20 _VUpdate.** *
185fa 20 4f 50 5f 56 52 65 6e 61 6d 65 0a 2a 2a 0a 2a OP_VRename.**.*
185fb 2a 20 49 66 20 6e 6f 20 73 75 63 68 20 69 6e 73 * If no such ins
185fc 74 72 75 63 74 69 6f 6e 20 69 73 20 66 6f 75 6e truction is foun
185fd 64 2c 20 74 68 65 6e 20 65 76 65 72 79 20 53 74 d, then every St
185fe 61 74 65 6d 65 6e 74 20 69 6e 73 74 72 75 63 74 atement instruct
185ff 69 6f 6e 20 0a 2a 2a 20 69 73 20 63 68 61 6e 67 ion .** is chang
18600 65 64 20 74 6f 20 61 20 4e 6f 6f 70 2e 20 20 49 ed to a Noop. I
18601 6e 20 74 68 69 73 20 77 61 79 2c 20 77 65 20 61 n this way, we a
18602 76 6f 69 64 20 63 72 65 61 74 69 6e 67 20 74 68 void creating th
18603 65 20 73 74 61 74 65 6d 65 6e 74 20 0a 2a 2a 20 e statement .**
18604 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75 6e 6e journal file unn
18605 65 63 65 73 73 61 72 69 6c 79 2e 0a 2a 2f 0a 73 ecessarily..*/.s
18606 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c tatic void resol
18607 76 65 50 32 56 61 6c 75 65 73 28 56 64 62 65 20 veP2Values(Vdbe
18608 2a 70 2c 20 69 6e 74 20 2a 70 4d 61 78 46 75 6e *p, int *pMaxFun
18609 63 41 72 67 73 29 7b 0a 20 20 69 6e 74 20 69 3b cArgs){. int i;
1860a 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 73 20 . int nMaxArgs
1860b 3d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a = 0;. Op *pOp;.
1860c 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d 20 int *aLabel =
1860d 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20 69 6e 74 p->aLabel;. int
1860e 20 64 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f doesStatementRo
1860f 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 69 6e llback = 0;. in
18610 74 20 68 61 73 53 74 61 74 65 6d 65 6e 74 42 65 t hasStatementBe
18611 67 69 6e 20 3d 20 30 3b 0a 20 20 70 2d 3e 72 65 gin = 0;. p->re
18612 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 70 2d adOnly = 1;. p-
18613 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c >usesStmtJournal
18614 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 4f 70 3d = 0;. for(pOp=
18615 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 p->aOp, i=p->nOp
18616 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 -1; i>=0; i--, p
18617 4f 70 2b 2b 29 7b 0a 20 20 20 20 75 38 20 6f 70 Op++){. u8 op
18618 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f code = pOp->opco
18619 64 65 3b 0a 0a 20 20 20 20 69 66 28 20 6f 70 63 de;.. if( opc
1861a 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69 6f 6e ode==OP_Function
1861b 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 || opcode==OP_A
1861c 67 67 53 74 65 70 20 29 7b 0a 20 20 20 20 20 20 ggStep ){.
1861d 69 66 28 20 70 4f 70 2d 3e 70 35 3e 6e 4d 61 78 if( pOp->p5>nMax
1861e 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 Args ) nMaxArgs
1861f 3d 20 70 4f 70 2d 3e 70 35 3b 0a 23 69 66 6e 64 = pOp->p5;.#ifnd
18620 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
18621 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 IRTUALTABLE.
18622 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 }else if( opcode
18623 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 29 7b 0a ==OP_VUpdate ){.
18624 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 if( pOp->p
18625 32 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 2>nMaxArgs ) nMa
18626 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 32 3b xArgs = pOp->p2;
18627 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 .#endif. }.
18628 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 if( opcode==OP
18629 5f 48 61 6c 74 20 29 7b 0a 20 20 20 20 20 20 69 _Halt ){. i
1862a 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 53 51 4c 49 f( pOp->p1==SQLI
1862b 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 26 26 TE_CONSTRAINT &&
1862c 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41 62 6f pOp->p2==OE_Abo
1862d 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f rt ){. do
1862e 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 esStatementRollb
1862f 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d ack = 1;. }
18630 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f . }else if( o
18631 70 63 6f 64 65 3d 3d 4f 50 5f 53 74 61 74 65 6d pcode==OP_Statem
18632 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 68 61 73 ent ){. has
18633 53 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d StatementBegin =
18634 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 75 73 65 1;. p->use
18635 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 31 sStmtJournal = 1
18636 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
18637 6f 70 63 6f 64 65 3d 3d 4f 50 5f 44 65 73 74 72 opcode==OP_Destr
18638 6f 79 20 29 7b 0a 20 20 20 20 20 20 64 6f 65 73 oy ){. does
18639 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 StatementRollbac
1863a 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 k = 1;. }else
1863b 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f if( opcode==OP_
1863c 54 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 70 Transaction && p
1863d 4f 70 2d 3e 70 32 21 3d 30 20 29 7b 0a 20 20 20 Op->p2!=0 ){.
1863e 20 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d p->readOnly =
1863f 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 0;.#ifndef SQLI
18640 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
18641 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 ABLE. }else i
18642 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 f( opcode==OP_VU
18643 70 64 61 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d pdate || opcode=
18644 3d 4f 50 5f 56 52 65 6e 61 6d 65 20 29 7b 0a 20 =OP_VRename ){.
18645 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d 65 doesStateme
18646 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b 0a ntRollback = 1;.
18647 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 }else if( op
18648 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 code==OP_VFilter
18649 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b ){. int n;
1864a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1864b 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29 ->nOp - i >= 3 )
1864c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1864d 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d pOp[-1].opcode==
1864e 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20 20 OP_Integer );.
1864f 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e n = pOp[-1].
18650 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e p1;. if( n>
18651 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 nMaxArgs ) nMaxA
18652 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a rgs = n;.#endif.
18653 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 }.. if( s
18654 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 qlite3VdbeOpcode
18655 48 61 73 50 72 6f 70 65 72 74 79 28 6f 70 63 6f HasProperty(opco
18656 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 de, OPFLG_JUMP)
18657 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a && pOp->p2<0 ){.
18658 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2d 31 assert( -1
18659 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62 -pOp->p2<p->nLab
1865a 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d el );. pOp-
1865b 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d >p2 = aLabel[-1-
1865c 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a pOp->p2];. }.
1865d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 }. sqlite3DbF
1865e 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c ree(p->db, p->aL
1865f 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 abel);. p->aLab
18660 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 el = 0;.. *pMax
18661 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78 41 FuncArgs = nMaxA
18662 72 67 73 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 rgs;.. /* If we
18663 20 6e 65 76 65 72 20 72 6f 6c 6c 62 61 63 6b 20 never rollback
18664 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e a statement tran
18665 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 73 74 saction, then st
18666 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 72 61 atement. ** tra
18667 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f nsactions are no
18668 74 20 6e 65 65 64 65 64 2e 20 20 53 6f 20 63 68 t needed. So ch
18669 61 6e 67 65 20 65 76 65 72 79 20 4f 50 5f 53 74 ange every OP_St
1866a 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 6f 70 63 atement. ** opc
1866b 6f 64 65 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 4e ode into an OP_N
1866c 6f 6f 70 2e 20 20 54 68 69 73 20 61 76 6f 69 64 oop. This avoid
1866d 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
1866e 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69 76 e3OsOpenExclusiv
1866f 65 28 29 0a 20 20 2a 2a 20 77 68 69 63 68 20 63 e(). ** which c
18670 61 6e 20 62 65 20 65 78 70 65 6e 73 69 76 65 20 an be expensive
18671 6f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d on some platform
18672 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 68 61 s.. */. if( ha
18673 73 53 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 sStatementBegin
18674 26 26 20 21 64 6f 65 73 53 74 61 74 65 6d 65 6e && !doesStatemen
18675 74 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 tRollback ){.
18676 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 p->usesStmtJour
18677 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 nal = 0;. for
18678 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d 70 (pOp=p->aOp, i=p
18679 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20 69 ->nOp-1; i>=0; i
1867a 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 --, pOp++){.
1867b 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 if( pOp->opcod
1867c 65 3d 3d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 e==OP_Statement
1867d 29 7b 0a 20 20 20 20 20 20 20 20 70 4f 70 2d 3e ){. pOp->
1867e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 opcode = OP_Noop
1867f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
18680 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
18681 75 72 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 urn the address
18682 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 of the next inst
18683 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e ruction to be in
18684 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 serted..*/.SQLIT
18685 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
18686 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 lite3VdbeCurrent
18687 41 64 64 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 Addr(Vdbe *p){.
18688 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
18689 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e c==VDBE_MAGIC_IN
1868a 49 54 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 IT );. return p
1868b 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ->nOp;.}../*.**
1868c 41 64 64 20 61 20 77 68 6f 6c 65 20 6c 69 73 74 Add a whole list
1868d 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 of operations t
1868e 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 o the operation
1868f 73 74 61 63 6b 2e 20 20 52 65 74 75 72 6e 20 74 stack. Return t
18690 68 65 0a 2a 2a 20 61 64 64 72 65 73 73 20 6f 66 he.** address of
18691 20 74 68 65 20 66 69 72 73 74 20 6f 70 65 72 61 the first opera
18692 74 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f 0a 53 tion added..*/.S
18693 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
18694 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
18695 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70 2c 20 OpList(Vdbe *p,
18696 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c int nOp, VdbeOpL
18697 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 29 7b ist const *aOp){
18698 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 61 . int addr;. a
18699 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d ssert( p->magic=
1869a 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 =VDBE_MAGIC_INIT
1869b 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 );. if( p->nOp
1869c 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f 70 41 + nOp > p->nOpA
1869d 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f 70 41 72 lloc && growOpAr
1869e 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 72 65 ray(p) ){. re
1869f 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 64 turn 0;. }. ad
186a0 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 69 dr = p->nOp;. i
186a1 66 28 20 6e 4f 70 3e 30 20 29 7b 0a 20 20 20 20 f( nOp>0 ){.
186a2 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65 4f int i;. VdbeO
186a3 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 70 49 6e pList const *pIn
186a4 20 3d 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28 = aOp;. for(
186a5 69 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c i=0; i<nOp; i++,
186a6 20 70 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 69 pIn++){. i
186a7 6e 74 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32 3b nt p2 = pIn->p2;
186a8 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 . VdbeOp *p
186a9 4f 75 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b Out = &p->aOp[i+
186aa 61 64 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f 75 addr];. pOu
186ab 74 2d 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e 2d t->opcode = pIn-
186ac 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 20 20 70 >opcode;. p
186ad 4f 75 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 Out->p1 = pIn->p
186ae 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c 1;. if( p2<
186af 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 0 && sqlite3Vdbe
186b0 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 OpcodeHasPropert
186b1 79 28 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 2c 20 y(pOut->opcode,
186b2 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 29 7b 0a 20 OPFLG_JUMP) ){.
186b3 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 pOut->p2
186b4 3d 20 61 64 64 72 20 2b 20 41 44 44 52 28 70 32 = addr + ADDR(p2
186b5 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
186b6 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 pOut->p2
186b7 20 3d 20 70 32 3b 0a 20 20 20 20 20 20 7d 0a 20 = p2;. }.
186b8 20 20 20 20 20 70 4f 75 74 2d 3e 70 33 20 3d 20 pOut->p3 =
186b9 70 49 6e 2d 3e 70 33 3b 0a 20 20 20 20 20 20 70 pIn->p3;. p
186ba 4f 75 74 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 Out->p4type = P4
186bb 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 20 20 _NOTUSED;.
186bc 70 4f 75 74 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a pOut->p4.p = 0;.
186bd 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 35 20 3d pOut->p5 =
186be 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 0;.#ifdef SQLIT
186bf 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70 4f E_DEBUG. pO
186c0 75 74 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 ut->zComment = 0
186c1 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ;. if( sqli
186c2 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 te3VdbeAddopTrac
186c3 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c e ){. sql
186c4 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 ite3VdbePrintOp(
186c5 30 2c 20 69 2b 61 64 64 72 2c 20 26 70 2d 3e 61 0, i+addr, &p->a
186c6 4f 70 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 20 20 Op[i+addr]);.
186c7 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
186c8 7d 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20 }. p->nOp +=
186c9 6e 4f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 nOp;. }. retur
186ca 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n addr;.}../*.**
186cb 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 Change the valu
186cc 65 20 6f 66 20 74 68 65 20 50 31 20 6f 70 65 72 e of the P1 oper
186cd 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 and for a specif
186ce 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ic instruction..
186cf 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
186d0 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 is useful when a
186d1 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 large program i
186d2 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a s loaded from a.
186d3 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20 ** static array
186d4 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 using sqlite3Vdb
186d5 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 eAddOpList but w
186d6 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 e want to make a
186d7 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 .** few minor ch
186d8 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f anges to the pro
186d9 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f gram..*/.SQLITE_
186da 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
186db 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 ite3VdbeChangeP1
186dc 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 (Vdbe *p, int ad
186dd 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 dr, int val){.
186de 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 assert( p==0 ||
186df 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
186e0 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 AGIC_INIT );. i
186e1 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20 f( p && addr>=0
186e2 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 && p->nOp>addr &
186e3 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 & p->aOp ){.
186e4 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 31 20 p->aOp[addr].p1
186e5 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a = val;. }.}../*
186e6 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 .** Change the v
186e7 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 20 6f alue of the P2 o
186e8 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 perand for a spe
186e9 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f cific instructio
186ea 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 n..** This routi
186eb 6e 65 20 69 73 20 75 73 65 66 75 6c 20 66 6f 72 ne is useful for
186ec 20 73 65 74 74 69 6e 67 20 61 20 6a 75 6d 70 20 setting a jump
186ed 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a destination..*/.
186ee 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
186ef 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
186f0 68 61 6e 67 65 50 32 28 56 64 62 65 20 2a 70 2c hangeP2(Vdbe *p,
186f1 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 76 int addr, int v
186f2 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 al){. assert( p
186f3 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d ==0 || p->magic=
186f4 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 =VDBE_MAGIC_INIT
186f5 20 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 61 );. if( p && a
186f6 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e 6e 4f 70 ddr>=0 && p->nOp
186f7 3e 61 64 64 72 20 26 26 20 70 2d 3e 61 4f 70 20 >addr && p->aOp
186f8 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 ){. p->aOp[ad
186f9 64 72 5d 2e 70 32 20 3d 20 76 61 6c 3b 0a 20 20 dr].p2 = val;.
186fa 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 }.}../*.** Chang
186fb 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 e the value of t
186fc 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 66 6f he P3 operand fo
186fd 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 r a specific ins
186fe 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c truction..*/.SQL
186ff 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
18700 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
18701 67 65 50 33 28 56 64 62 65 20 2a 70 2c 20 69 6e geP3(Vdbe *p, in
18702 74 20 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 t addr, int val)
18703 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 {. assert( p==0
18704 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 || p->magic==VD
18705 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b BE_MAGIC_INIT );
18706 0a 20 20 69 66 28 20 70 20 26 26 20 61 64 64 72 . if( p && addr
18707 3e 3d 30 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 >=0 && p->nOp>ad
18708 64 72 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a dr && p->aOp ){.
18709 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d p->aOp[addr]
1870a 2e 70 33 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d .p3 = val;. }.}
1870b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
1870c 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
1870d 50 35 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 74 P5 operand for t
1870e 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
1870f 0a 2a 2a 20 61 64 64 65 64 20 6f 70 65 72 61 74 .** added operat
18710 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
18711 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
18712 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 te3VdbeChangeP5(
18713 56 64 62 65 20 2a 70 2c 20 75 38 20 76 61 6c 29 Vdbe *p, u8 val)
18714 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 {. assert( p==0
18715 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 || p->magic==VD
18716 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b BE_MAGIC_INIT );
18717 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 61 . if( p && p->a
18718 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 Op ){. assert
18719 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 ( p->nOp>0 );.
1871a 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d p->aOp[p->nOp-
1871b 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20 20 7d 1].p5 = val;. }
1871c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
1871d 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 the P2 operand
1871e 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 of instruction a
1871f 64 64 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 ddr so that it p
18720 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20 oints to.** the
18721 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e address of the n
18722 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
18723 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a 2f 0a to be coded..*/.
18724 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
18725 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4a oid sqlite3VdbeJ
18726 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a 70 2c umpHere(Vdbe *p,
18727 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73 71 int addr){. sq
18728 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
18729 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e 6e 4f 2(p, addr, p->nO
1872a 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 p);.}.../*.** If
1872b 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e 63 44 the input FuncD
1872c 65 66 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ef structure is
1872d 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65 6e 20 ephemeral, then
1872e 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a 2a 20 free it. If.**
1872f 74 68 65 20 46 75 6e 63 44 65 66 20 69 73 20 6e the FuncDef is n
18730 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74 68 65 ot ephermal, the
18731 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2f n do nothing..*/
18732 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 .static void fre
18733 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 eEphemeralFuncti
18734 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 on(sqlite3 *db,
18735 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29 7b 0a FuncDef *pDef){.
18736 20 20 69 66 28 20 70 44 65 66 20 26 26 20 28 70 if( pDef && (p
18737 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c Def->flags & SQL
18738 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 29 21 ITE_FUNC_EPHEM)!
18739 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1873a 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 66 3DbFree(db, pDef
1873b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
1873c 44 65 6c 65 74 65 20 61 20 50 34 20 76 61 6c 75 Delete a P4 valu
1873d 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a e if necessary..
1873e 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 */.static void f
1873f 72 65 65 50 34 28 73 71 6c 69 74 65 33 20 2a 64 reeP4(sqlite3 *d
18740 62 2c 20 69 6e 74 20 70 34 74 79 70 65 2c 20 76 b, int p4type, v
18741 6f 69 64 20 2a 70 34 29 7b 0a 20 20 69 66 28 20 oid *p4){. if(
18742 70 34 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 p4 ){. switch
18743 28 20 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 ( p4type ){.
18744 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 0a case P4_REAL:.
18745 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e case P4_IN
18746 54 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20 T64:. case
18747 50 34 5f 4d 50 52 49 4e 54 46 3a 0a 20 20 20 20 P4_MPRINTF:.
18748 20 20 63 61 73 65 20 50 34 5f 44 59 4e 41 4d 49 case P4_DYNAMI
18749 43 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 C:. case P4
1874a 5f 4b 45 59 49 4e 46 4f 3a 0a 20 20 20 20 20 20 _KEYINFO:.
1874b 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 41 59 case P4_INTARRAY
1874c 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f :. case P4_
1874d 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 3a KEYINFO_HANDOFF:
1874e 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 {. sqlit
1874f 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 34 29 e3DbFree(db, p4)
18750 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
18751 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 . }. c
18752 61 73 65 20 50 34 5f 56 44 42 45 46 55 4e 43 3a ase P4_VDBEFUNC:
18753 20 7b 0a 20 20 20 20 20 20 20 20 56 64 62 65 46 {. VdbeF
18754 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 20 3d unc *pVdbeFunc =
18755 20 28 56 64 62 65 46 75 6e 63 20 2a 29 70 34 3b (VdbeFunc *)p4;
18756 0a 20 20 20 20 20 20 20 20 66 72 65 65 45 70 68 . freeEph
18757 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 emeralFunction(d
18758 62 2c 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 b, pVdbeFunc->pF
18759 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 73 71 unc);. sq
1875a 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 lite3VdbeDeleteA
1875b 75 78 44 61 74 61 28 70 56 64 62 65 46 75 6e 63 uxData(pVdbeFunc
1875c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 , 0);. sq
1875d 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1875e 70 56 64 62 65 46 75 6e 63 29 3b 0a 20 20 20 20 pVdbeFunc);.
1875f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
18760 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 }. case P4
18761 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 _FUNCDEF: {.
18762 20 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 freeEphemera
18763 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 46 lFunction(db, (F
18764 75 6e 63 44 65 66 2a 29 70 34 29 3b 0a 20 20 20 uncDef*)p4);.
18765 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
18766 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 }. case P
18767 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 20 4_MEM: {.
18768 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 sqlite3ValueFre
18769 65 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 e((sqlite3_value
1876a 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 *)p4);. b
1876b 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1876c 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a }. }.}.../*.*
1876d 2a 20 43 68 61 6e 67 65 20 4e 20 6f 70 63 6f 64 * Change N opcod
1876e 65 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 61 es starting at a
1876f 64 64 72 20 74 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a ddr to No-ops..*
18770 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18771 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
18772 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 eChangeToNoop(Vd
18773 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c be *p, int addr,
18774 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20 70 int N){. if( p
18775 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 && p->aOp ){.
18776 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 VdbeOp *pOp =
18777 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 &p->aOp[addr];.
18778 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d sqlite3 *db =
18779 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 68 69 6c p->db;. whil
1877a 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 20 e( N-- ){.
1877b 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e freeP4(db, pOp->
1877c 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e p4type, pOp->p4.
1877d 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 p);. memset
1877e 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 (pOp, 0, sizeof(
1877f 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 pOp[0]));.
18780 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 pOp->opcode = OP
18781 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70 4f 70 _Noop;. pOp
18782 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a ++;. }. }.}.
18783 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
18784 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 e value of the P
18785 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 4 operand for a
18786 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 specific instruc
18787 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f tion..** This ro
18788 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 6c 20 utine is useful
18789 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 72 6f when a large pro
1878a 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 20 66 gram is loaded f
1878b 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69 63 20 rom a.** static
1878c 61 72 72 61 79 20 75 73 69 6e 67 20 73 71 6c 69 array using sqli
1878d 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 te3VdbeAddOpList
1878e 20 62 75 74 20 77 65 20 77 61 6e 74 20 74 6f 20 but we want to
1878f 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20 6d 69 make a.** few mi
18790 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74 nor changes to t
18791 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a he program..**.*
18792 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e 20 74 * If n>=0 then t
18793 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 69 73 he P4 operand is
18794 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e 69 6e dynamic, meanin
18795 67 20 74 68 61 74 20 61 20 63 6f 70 79 20 6f 66 g that a copy of
18796 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 .** the string i
18797 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65 6d 6f s made into memo
18798 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
18799 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
1879a 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 )..** A value of
1879b 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f 70 79 n==0 means copy
1879c 20 62 79 74 65 73 20 6f 66 20 7a 50 34 20 75 70 bytes of zP4 up
1879d 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 69 6e to and includin
1879e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6e g the.** first n
1879f 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20 6e 3e ull byte. If n>
187a0 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b 31 20 0 then copy n+1
187a1 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0a 2a 2a bytes of zP4..**
187a2 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b 45 59 .** If n==P4_KEY
187a3 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20 74 68 INFO it means th
187a4 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f 69 6e at zP4 is a poin
187a5 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f ter to a KeyInfo
187a6 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 41 structure..** A
187a7 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 6f 66 copy is made of
187a8 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 the KeyInfo str
187a9 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65 6d 6f ucture into memo
187aa 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
187ab 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c .** sqlite3_mall
187ac 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65 64 20 oc, to be freed
187ad 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 when the Vdbe is
187ae 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6e finalized..** n
187af 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e ==P4_KEYINFO_HAN
187b0 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73 20 74 DOFF indicates t
187b1 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 20 74 hat zP4 points t
187b2 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 o a KeyInfo stru
187b3 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65 64 20 cture.** stored
187b4 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 74 in memory that t
187b5 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62 he caller has ob
187b6 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
187b7 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68 65 20 te3_malloc. The
187b8 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c .** caller shoul
187b9 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65 20 61 d not free the a
187ba 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 77 69 llocation, it wi
187bb 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68 65 6e ll be freed when
187bc 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a 2a 20 the Vdbe is.**
187bd 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 0a 2a finalized..** .*
187be 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20 6f * Other values o
187bf 66 20 6e 20 28 50 34 5f 53 54 41 54 49 43 2c 20 f n (P4_STATIC,
187c0 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 63 2e 29 P4_COLLSEQ etc.)
187c1 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 7a indicate that z
187c2 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 P4 points.** to
187c3 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74 72 75 a string or stru
187c4 63 74 75 72 65 20 74 68 61 74 20 69 73 20 67 75 cture that is gu
187c5 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 73 aranteed to exis
187c6 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74 69 t for the lifeti
187c7 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56 64 62 me of.** the Vdb
187c8 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61 73 65 e. In these case
187c9 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20 63 6f s we can just co
187ca 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72 2e 0a py the pointer..
187cb 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c 30 20 **.** If addr<0
187cc 74 68 65 6e 20 63 68 61 6e 67 65 20 50 34 20 6f then change P4 o
187cd 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e n the most recen
187ce 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69 6e 73 tly inserted ins
187cf 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c truction..*/.SQL
187d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
187d1 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
187d2 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20 69 6e geP4(Vdbe *p, in
187d3 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63 68 t addr, const ch
187d4 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e 29 7b ar *zP4, int n){
187d5 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 73 71 . Op *pOp;. sq
187d6 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 61 73 73 lite3 *db;. ass
187d7 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 64 ert( p!=0 );. d
187d8 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73 b = p->db;. ass
187d9 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 ert( p->magic==V
187da 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 DBE_MAGIC_INIT )
187db 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 3d 3d ;. if( p->aOp==
187dc 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 0 || db->mallocF
187dd 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 66 20 ailed ){. if
187de 28 6e 20 21 3d 20 50 34 5f 4b 45 59 49 4e 46 4f (n != P4_KEYINFO
187df 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 ) {. freeP4
187e0 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 2a (db, n, (void*)*
187e1 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 3b 0a 20 (char**)&zP4);.
187e2 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 3b }. return;
187e3 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 . }. assert( a
187e4 64 64 72 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 ddr<p->nOp );.
187e5 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 20 20 if( addr<0 ){.
187e6 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 addr = p->nOp
187e7 2d 20 31 3b 0a 20 20 20 20 69 66 28 20 61 64 64 - 1;. if( add
187e8 72 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 r<0 ) return;.
187e9 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f }. pOp = &p->aO
187ea 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72 65 65 50 p[addr];. freeP
187eb 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 4(db, pOp->p4typ
187ec 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 e, pOp->p4.p);.
187ed 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a pOp->p4.p = 0;.
187ee 20 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 if( n==P4_INT3
187ef 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 2 ){. /* Note
187f0 3a 20 74 68 69 73 20 63 61 73 74 20 69 73 20 73 : this cast is s
187f1 61 66 65 2c 20 62 65 63 61 75 73 65 20 74 68 65 afe, because the
187f2 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70 6f 69 origin data poi
187f3 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0a 20 20 nt was an int.
187f4 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20 63 61 ** that was ca
187f5 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74 20 63 st to a (const c
187f6 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 70 har *). */. p
187f7 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c 49 54 Op->p4.i = SQLIT
187f8 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 34 E_PTR_TO_INT(zP4
187f9 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 );. pOp->p4ty
187fa 70 65 20 3d 20 50 34 5f 49 4e 54 33 32 3b 0a 20 pe = P4_INT32;.
187fb 20 7d 65 6c 73 65 20 69 66 28 20 7a 50 34 3d 3d }else if( zP4==
187fc 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 0 ){. pOp->p4
187fd 2e 70 20 3d 20 30 3b 0a 20 20 20 20 70 4f 70 2d .p = 0;. pOp-
187fe 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 >p4type = P4_NOT
187ff 55 53 45 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 USED;. }else if
18800 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 ( n==P4_KEYINFO
18801 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a ){. KeyInfo *
18802 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 6e pKeyInfo;. in
18803 74 20 6e 46 69 65 6c 64 2c 20 6e 42 79 74 65 3b t nField, nByte;
18804 0a 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 28 .. nField = (
18805 28 4b 65 79 49 6e 66 6f 2a 29 7a 50 34 29 2d 3e (KeyInfo*)zP4)->
18806 6e 46 69 65 6c 64 3b 0a 20 20 20 20 6e 42 79 74 nField;. nByt
18807 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 e = sizeof(*pKey
18808 49 6e 66 6f 29 20 2b 20 28 6e 46 69 65 6c 64 2d Info) + (nField-
18809 31 29 2a 73 69 7a 65 6f 66 28 70 4b 65 79 49 6e 1)*sizeof(pKeyIn
1880a 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20 fo->aColl[0]) +
1880b 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 4b 65 79 nField;. pKey
1880c 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 4d 61 Info = sqlite3Ma
1880d 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 lloc( nByte );.
1880e 20 20 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 pOp->p4.pKeyI
1880f 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a nfo = pKeyInfo;.
18810 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e 66 6f if( pKeyInfo
18811 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 61 53 ){. u8 *aS
18812 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 ortOrder;.
18813 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c memcpy(pKeyInfo,
18814 20 7a 50 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20 zP4, nByte);.
18815 20 20 20 20 61 53 6f 72 74 4f 72 64 65 72 20 3d aSortOrder =
18816 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 pKeyInfo->aSort
18817 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 69 66 28 Order;. if(
18818 20 61 53 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 aSortOrder ){.
18819 20 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d pKeyInfo-
1881a 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 >aSortOrder = (u
1881b 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 70 nsigned char*)&p
1881c 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e KeyInfo->aColl[n
1881d 46 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20 Field];.
1881e 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d memcpy(pKeyInfo-
1881f 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f >aSortOrder, aSo
18820 72 74 4f 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 rtOrder, nField)
18821 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
18822 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 pOp->p4type = P4
18823 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65 _KEYINFO;. }e
18824 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 64 62 lse{. p->db
18825 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
18826 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 1;. pOp->p
18827 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 4type = P4_NOTUS
18828 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 ED;. }. }els
18829 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 e if( n==P4_KEYI
1882a 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20 NFO_HANDOFF ){.
1882b 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 pOp->p4.p = (
1882c 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 void*)zP4;. p
1882d 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f Op->p4type = P4_
1882e 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65 KEYINFO;. }else
1882f 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 if( n<0 ){.
18830 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 pOp->p4.p = (voi
18831 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d d*)zP4;. pOp-
18832 3e 70 34 74 79 70 65 20 3d 20 28 73 69 67 6e 65 >p4type = (signe
18833 64 20 63 68 61 72 29 6e 3b 0a 20 20 7d 65 6c 73 d char)n;. }els
18834 65 7b 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 e{. if( n==0
18835 29 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 ) n = sqlite3Str
18836 6c 65 6e 33 30 28 7a 50 34 29 3b 0a 20 20 20 20 len30(zP4);.
18837 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 pOp->p4.z = sqli
18838 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e te3DbStrNDup(p->
18839 64 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 db, zP4, n);.
1883a 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
1883b 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 4_DYNAMIC;. }.}
1883c 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
1883d 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
1883e 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 e comment on the
1883f 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
18840 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63 ly coded instruc
18841 74 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 tion. Or.** ins
18842 65 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 ert a No-op and
18843 61 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 add the comment
18844 74 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 74 to that new inst
18845 72 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a ruction. This.*
18846 2a 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 * makes the code
18847 20 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 20 easier to read
18848 64 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e 67 during debugging
18849 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20 . None of this
1884a 68 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 happens.** in a
1884b 70 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c 64 production build
1884c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1884d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1884e 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 VdbeComment(Vdbe
1884f 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *p, const char
18850 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
18851 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 va_list ap;.
18852 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 assert( p->nOp>0
18853 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b || p->aOp==0 );
18854 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f . assert( p->aO
18855 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 p==0 || p->aOp[p
18856 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e ->nOp-1].zCommen
18857 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d t==0 || p->db->m
18858 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
18859 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 if( p->nOp ){.
1885a 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 char **pz = &
1885b 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d p->aOp[p->nOp-1]
1885c 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 .zComment;. v
1885d 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 a_start(ap, zFor
1885e 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 mat);. sqlite
1885f 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 3DbFree(p->db, *
18860 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 pz);. *pz = s
18861 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 qlite3VMPrintf(p
18862 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 ->db, zFormat, a
18863 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 p);. va_end(a
18864 70 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 p);. }.}.SQLITE
18865 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
18866 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d lite3VdbeNoopCom
18867 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f ment(Vdbe *p, co
18868 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 nst char *zForma
18869 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 t, ...){. va_li
1886a 73 74 20 61 70 3b 0a 20 20 73 71 6c 69 74 65 33 st ap;. sqlite3
1886b 56 64 62 65 41 64 64 4f 70 30 28 70 2c 20 4f 50 VdbeAddOp0(p, OP
1886c 5f 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 65 72 74 _Noop);. assert
1886d 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d ( p->nOp>0 || p-
1886e 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73 >aOp==0 );. ass
1886f 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c ert( p->aOp==0 |
18870 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d | p->aOp[p->nOp-
18871 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 1].zComment==0 |
18872 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 | p->db->mallocF
18873 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 ailed );. if( p
18874 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61 ->nOp ){. cha
18875 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 r **pz = &p->aOp
18876 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d [p->nOp-1].zComm
18877 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 ent;. va_star
18878 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a t(ap, zFormat);.
18879 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1887a 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20 e(p->db, *pz);.
1887b 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33 *pz = sqlite3
1887c 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20 VMPrintf(p->db,
1887d 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
1887e 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
1887f 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e }.}.#endif /* N
18880 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 DEBUG */../*.**
18881 52 65 74 75 72 6e 20 74 68 65 20 6f 70 63 6f 64 Return the opcod
18882 65 20 66 6f 72 20 61 20 67 69 76 65 6e 20 61 64 e for a given ad
18883 64 72 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 dress..*/.SQLITE
18884 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 _PRIVATE VdbeOp
18885 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f *sqlite3VdbeGetO
18886 70 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 p(Vdbe *p, int a
18887 64 64 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ddr){. assert(
18888 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
18889 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 AGIC_INIT );. a
1888a 73 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 20 ssert( (addr>=0
1888b 26 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 && addr<p->nOp)
1888c 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 || p->db->malloc
1888d 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 Failed );. retu
1888e 72 6e 20 28 28 61 64 64 72 3e 3d 30 20 26 26 20 rn ((addr>=0 &&
1888f 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 3f 28 26 70 addr<p->nOp)?(&p
18890 2d 3e 61 4f 70 5b 61 64 64 72 5d 29 3a 30 29 3b ->aOp[addr]):0);
18891 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .}..#if !defined
18892 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 (SQLITE_OMIT_EXP
18893 4c 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 LAIN) || !define
18894 64 28 4e 44 45 42 55 47 29 20 5c 0a 20 20 20 20 d(NDEBUG) \.
18895 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 || defined(VDBE
18896 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 _PROFILE) || def
18897 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
18898 47 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 G)./*.** Compute
18899 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 a string that d
1889a 65 73 63 72 69 62 65 73 20 74 68 65 20 50 34 20 escribes the P4
1889b 70 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6e parameter for an
1889c 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 opcode..** Use
1889d 7a 54 65 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 zTemp for any re
1889e 71 75 69 72 65 64 20 74 65 6d 70 6f 72 61 72 79 quired temporary
1889f 20 62 75 66 66 65 72 20 73 70 61 63 65 2e 0a 2a buffer space..*
188a0 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 /.static char *d
188a1 69 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70 isplayP4(Op *pOp
188a2 2c 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69 , char *zTemp, i
188a3 6e 74 20 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61 nt nTemp){. cha
188a4 72 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a r *zP4 = zTemp;.
188a5 20 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70 3e assert( nTemp>
188a6 3d 32 30 20 29 3b 0a 20 20 73 77 69 74 63 68 28 =20 );. switch(
188a7 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 29 7b 0a pOp->p4type ){.
188a8 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 case P4_KEYI
188a9 4e 46 4f 5f 53 54 41 54 49 43 3a 0a 20 20 20 20 NFO_STATIC:.
188aa 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a case P4_KEYINFO:
188ab 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20 {. int i,
188ac 6a 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f j;. KeyInfo
188ad 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 *pKeyInfo = pOp
188ae 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 ->p4.pKeyInfo;.
188af 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
188b0 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 rintf(nTemp, zTe
188b1 6d 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64 22 mp, "keyinfo(%d"
188b2 2c 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 , pKeyInfo->nFie
188b3 6c 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 73 ld);. i = s
188b4 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
188b5 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f 72 Temp);. for
188b6 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f (j=0; j<pKeyInfo
188b7 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a ->nField; j++){.
188b8 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 CollSeq
188b9 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 *pColl = pKeyInf
188ba 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 o->aColl[j];.
188bb 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 if( pColl )
188bc 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 {. int
188bd 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
188be 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 n30(pColl->zName
188bf 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
188c0 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a i+n>nTemp-6 ){.
188c1 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 memc
188c2 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e py(&zTemp[i],",.
188c3 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 ..",4);.
188c4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
188c5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
188c6 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c zTemp[i++] = ',
188c7 27 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ';. if(
188c8 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 pKeyInfo->aSort
188c9 4f 72 64 65 72 20 26 26 20 70 4b 65 79 49 6e 66 Order && pKeyInf
188ca 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d o->aSortOrder[j]
188cb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
188cc 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 zTemp[i++] = '-'
188cd 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
188ce 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
188cf 7a 54 65 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d zTemp[i], pColl-
188d0 3e 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 >zName,n+1);.
188d1 20 20 20 20 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 i += n;.
188d2 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
188d3 20 69 2b 34 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a i+4<nTemp-6 ){.
188d4 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
188d5 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c (&zTemp[i],",nil
188d6 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ",4);.
188d7 69 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 i += 4;.
188d8 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
188d9 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 zTemp[i++] = ')'
188da 3b 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d ;. zTemp[i]
188db 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 = 0;. asse
188dc 72 74 28 20 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 rt( i<nTemp );.
188dd 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
188de 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 43 4f }. case P4_CO
188df 4c 4c 53 45 51 3a 20 7b 0a 20 20 20 20 20 20 43 LLSEQ: {. C
188e0 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 ollSeq *pColl =
188e1 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 pOp->p4.pColl;.
188e2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
188e3 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 rintf(nTemp, zTe
188e4 6d 70 2c 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32 mp, "collseq(%.2
188e5 30 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 0s)", pColl->zNa
188e6 6d 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b me);. break
188e7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
188e8 20 50 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 P4_FUNCDEF: {.
188e9 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 FuncDef *pD
188ea 65 66 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 ef = pOp->p4.pFu
188eb 6e 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 nc;. sqlite
188ec 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
188ed 2c 20 7a 54 65 6d 70 2c 20 22 25 73 28 25 64 29 , zTemp, "%s(%d)
188ee 22 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 ", pDef->zName,
188ef 70 44 65 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 pDef->nArg);.
188f0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
188f1 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36 case P4_INT6
188f2 34 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 4: {. sqlit
188f3 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d e3_snprintf(nTem
188f4 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 p, zTemp, "%lld"
188f5 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29 , *pOp->p4.pI64)
188f6 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
188f7 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 }. case P4
188f8 5f 49 4e 54 33 32 3a 20 7b 0a 20 20 20 20 20 20 _INT32: {.
188f9 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
188fa 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 (nTemp, zTemp, "
188fb 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 3b %d", pOp->p4.i);
188fc 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
188fd 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
188fe 52 45 41 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 REAL: {. sq
188ff 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
18900 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e Temp, zTemp, "%.
18901 31 36 67 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 16g", *pOp->p4.p
18902 52 65 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 Real);. bre
18903 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
18904 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 se P4_MEM: {.
18905 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 Mem *pMem = p
18906 4f 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 Op->p4.pMem;.
18907 20 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d assert( (pMem
18908 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
18909 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ll)==0 );.
1890a 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
1890b 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 & MEM_Str ){.
1890c 20 20 20 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d zP4 = pMem-
1890d 3e 7a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 >z;. }else
1890e 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
1890f 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
18910 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
18911 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 rintf(nTemp, zTe
18912 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d mp, "%lld", pMem
18913 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65 ->u.i);. }e
18914 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c lse if( pMem->fl
18915 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 ags & MEM_Real )
18916 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
18917 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
18918 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 , zTemp, "%.16g"
18919 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 , pMem->r);.
1891a 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
1891b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 . }.#ifndef S
1891c 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1891d 41 4c 54 41 42 4c 45 0a 20 20 20 20 63 61 73 65 ALTABLE. case
1891e 20 50 34 5f 56 54 41 42 3a 20 7b 0a 20 20 20 20 P4_VTAB: {.
1891f 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
18920 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e pVtab = pOp->p4.
18921 70 56 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c pVtab;. sql
18922 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
18923 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 76 74 61 emp, zTemp, "vta
18924 62 3a 25 70 3a 25 70 22 2c 20 70 56 74 61 62 2c b:%p:%p", pVtab,
18925 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 29 pVtab->pModule)
18926 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
18927 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
18928 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 41 59 case P4_INTARRAY
18929 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
1892a 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
1892b 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74 61 72 72 , zTemp, "intarr
1892c 61 79 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 ay");. brea
1892d 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
1892e 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50 ault: {. zP
1892f 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 4 = pOp->p4.z;.
18930 20 20 20 20 20 69 66 28 20 7a 50 34 3d 3d 30 20 if( zP4==0
18931 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d ){. zP4 =
18932 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 zTemp;.
18933 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20 zTemp[0] = 0;.
18934 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
18935 20 20 61 73 73 65 72 74 28 20 7a 50 34 21 3d 30 assert( zP4!=0
18936 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 34 );. return zP4
18937 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
18938 2a 20 44 65 63 6c 61 72 65 20 74 6f 20 74 68 65 * Declare to the
18939 20 56 64 62 65 20 74 68 61 74 20 74 68 65 20 42 Vdbe that the B
1893a 54 72 65 65 20 6f 62 6a 65 63 74 20 61 74 20 64 Tree object at d
1893b 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 65 b->aDb[i] is use
1893c 64 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f d..**.*/.SQLITE_
1893d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1893e 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 ite3VdbeUsesBtre
1893f 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 e(Vdbe *p, int i
18940 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 ){. int mask;.
18941 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 assert( i>=0 &&
18942 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b i<p->db->nDb );
18943 0a 20 20 61 73 73 65 72 74 28 20 69 3c 28 69 6e . assert( i<(in
18944 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 74 72 65 t)sizeof(p->btre
18945 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 20 6d 61 eMask)*8 );. ma
18946 73 6b 20 3d 20 31 3c 3c 69 3b 0a 20 20 69 66 28 sk = 1<<i;. if(
18947 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 (p->btreeMask &
18948 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 mask)==0 ){.
18949 20 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 7c 3d p->btreeMask |=
1894a 20 6d 61 73 6b 3b 0a 20 20 20 20 73 71 6c 69 74 mask;. sqlit
1894b 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
1894c 79 49 6e 73 65 72 74 28 26 70 2d 3e 61 4d 75 74 yInsert(&p->aMut
1894d 65 78 2c 20 70 2d 3e 64 62 2d 3e 61 44 62 5b 69 ex, p->db->aDb[i
1894e 5d 2e 70 42 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a ].pBt);. }.}...
1894f 23 69 66 20 64 65 66 69 6e 65 64 28 56 44 42 45 #if defined(VDBE
18950 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 _PROFILE) || def
18951 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
18952 47 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 G)./*.** Print a
18953 20 73 69 6e 67 6c 65 20 6f 70 63 6f 64 65 2e 20 single opcode.
18954 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
18955 20 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 used for debugg
18956 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c ing only..*/.SQL
18957 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
18958 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
18959 74 4f 70 28 46 49 4c 45 20 2a 70 4f 75 74 2c 20 tOp(FILE *pOut,
1895a 69 6e 74 20 70 63 2c 20 4f 70 20 2a 70 4f 70 29 int pc, Op *pOp)
1895b 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 3b 0a 20 {. char *zP4;.
1895c 20 63 68 61 72 20 7a 50 74 72 5b 35 30 5d 3b 0a char zPtr[50];.
1895d 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 static const c
1895e 68 61 72 20 2a 7a 46 6f 72 6d 61 74 31 20 3d 20 har *zFormat1 =
1895f 22 25 34 64 20 25 2d 31 33 73 20 25 34 64 20 25 "%4d %-13s %4d %
18960 34 64 20 25 34 64 20 25 2d 34 73 20 25 2e 32 58 4d %4d %-4s %.2X
18961 20 25 73 5c 6e 22 3b 0a 20 20 69 66 28 20 70 4f %s\n";. if( pO
18962 75 74 3d 3d 30 20 29 20 70 4f 75 74 20 3d 20 73 ut==0 ) pOut = s
18963 74 64 6f 75 74 3b 0a 20 20 7a 50 34 20 3d 20 64 tdout;. zP4 = d
18964 69 73 70 6c 61 79 50 34 28 70 4f 70 2c 20 7a 50 isplayP4(pOp, zP
18965 74 72 2c 20 73 69 7a 65 6f 66 28 7a 50 74 72 29 tr, sizeof(zPtr)
18966 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 70 4f 75 );. fprintf(pOu
18967 74 2c 20 7a 46 6f 72 6d 61 74 31 2c 20 70 63 2c t, zFormat1, pc,
18968 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f . sqlite3O
18969 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f pcodeName(pOp->o
1896a 70 63 6f 64 65 29 2c 20 70 4f 70 2d 3e 70 31 2c pcode), pOp->p1,
1896b 20 70 4f 70 2d 3e 70 32 2c 20 70 4f 70 2d 3e 70 pOp->p2, pOp->p
1896c 33 2c 20 7a 50 34 2c 20 70 4f 70 2d 3e 70 35 2c 3, zP4, pOp->p5,
1896d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1896e 45 42 55 47 0a 20 20 20 20 20 20 70 4f 70 2d 3e EBUG. pOp->
1896f 7a 43 6f 6d 6d 65 6e 74 20 3f 20 70 4f 70 2d 3e zComment ? pOp->
18970 7a 43 6f 6d 6d 65 6e 74 20 3a 20 22 22 0a 23 65 zComment : "".#e
18971 6c 73 65 0a 20 20 20 20 20 20 22 22 0a 23 65 6e lse. "".#en
18972 64 69 66 0a 20 20 29 3b 0a 20 20 66 66 6c 75 73 dif. );. fflus
18973 68 28 70 4f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 h(pOut);.}.#endi
18974 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 f../*.** Release
18975 20 61 6e 20 61 72 72 61 79 20 6f 66 20 4e 20 4d an array of N M
18976 65 6d 20 65 6c 65 6d 65 6e 74 73 0a 2a 2f 0a 73 em elements.*/.s
18977 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 tatic void relea
18978 73 65 4d 65 6d 41 72 72 61 79 28 4d 65 6d 20 2a seMemArray(Mem *
18979 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28 p, int N){. if(
1897a 20 70 20 26 26 20 4e 20 29 7b 0a 20 20 20 20 4d p && N ){. M
1897b 65 6d 20 2a 70 45 6e 64 3b 0a 20 20 20 20 73 71 em *pEnd;. sq
1897c 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 lite3 *db = p->d
1897d 62 3b 0a 20 20 20 20 75 38 20 6d 61 6c 6c 6f 63 b;. u8 malloc
1897e 5f 66 61 69 6c 65 64 20 3d 20 64 62 2d 3e 6d 61 _failed = db->ma
1897f 6c 6c 6f 63 46 61 69 6c 65 64 3b 0a 20 20 20 20 llocFailed;.
18980 66 6f 72 28 70 45 6e 64 3d 26 70 5b 4e 5d 3b 20 for(pEnd=&p[N];
18981 70 3c 70 45 6e 64 3b 20 70 2b 2b 29 7b 0a 20 20 p<pEnd; p++){.
18982 20 20 20 20 61 73 73 65 72 74 28 20 28 26 70 5b assert( (&p[
18983 31 5d 29 3d 3d 70 45 6e 64 20 7c 7c 20 70 5b 30 1])==pEnd || p[0
18984 5d 2e 64 62 3d 3d 70 5b 31 5d 2e 64 62 20 29 3b ].db==p[1].db );
18985 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 .. /* This
18986 62 6c 6f 63 6b 20 69 73 20 72 65 61 6c 6c 79 20 block is really
18987 61 6e 20 69 6e 6c 69 6e 65 64 20 76 65 72 73 69 an inlined versi
18988 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 on of sqlite3Vdb
18989 65 4d 65 6d 52 65 6c 65 61 73 65 28 29 0a 20 20 eMemRelease().
1898a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 61 6b 65 ** that take
1898b 73 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74 s advantage of t
1898c 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 he fact that the
1898d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 76 61 6c memory cell val
1898e 75 65 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 ue is . **
1898f 62 65 69 6e 67 20 73 65 74 20 74 6f 20 4e 55 4c being set to NUL
18990 4c 20 61 66 74 65 72 20 72 65 6c 65 61 73 69 6e L after releasin
18991 67 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 72 65 g any dynamic re
18992 73 6f 75 72 63 65 73 2e 0a 20 20 20 20 20 20 2a sources.. *
18993 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 6a *. ** The j
18994 75 73 74 69 66 69 63 61 74 69 6f 6e 20 66 6f 72 ustification for
18995 20 64 75 70 6c 69 63 61 74 69 6e 67 20 63 6f 64 duplicating cod
18996 65 20 69 73 20 74 68 61 74 20 61 63 63 6f 72 64 e is that accord
18997 69 6e 67 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a ing to . **
18998 20 63 61 6c 6c 67 72 69 6e 64 2c 20 74 68 69 73 callgrind, this
18999 20 63 61 75 73 65 73 20 61 20 63 65 72 74 61 69 causes a certai
1899a 6e 20 74 65 73 74 20 63 61 73 65 20 74 6f 20 68 n test case to h
1899b 69 74 20 74 68 65 20 43 50 55 20 34 2e 37 20 0a it the CPU 4.7 .
1899c 20 20 20 20 20 20 2a 2a 20 70 65 72 63 65 6e 74 ** percent
1899d 20 6c 65 73 73 20 28 78 38 36 20 6c 69 6e 75 78 less (x86 linux
1899e 2c 20 67 63 63 20 76 65 72 73 69 6f 6e 20 34 2e , gcc version 4.
1899f 31 2e 32 2c 20 2d 4f 36 29 20 74 68 61 6e 20 69 1.2, -O6) than i
189a0 66 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 f . ** sqli
189a1 74 65 33 4d 65 6d 52 65 6c 65 61 73 65 28 29 20 te3MemRelease()
189a2 77 65 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d were called from
189a3 20 68 65 72 65 2e 20 57 69 74 68 20 2d 4f 32 2c here. With -O2,
189a4 20 74 68 69 73 20 6a 75 6d 70 73 0a 20 20 20 20 this jumps.
189a5 20 20 2a 2a 20 74 6f 20 36 2e 36 20 70 65 72 63 ** to 6.6 perc
189a6 65 6e 74 2e 20 54 68 65 20 74 65 73 74 20 63 61 ent. The test ca
189a7 73 65 20 69 73 20 69 6e 73 65 72 74 69 6e 67 20 se is inserting
189a8 31 30 30 30 20 72 6f 77 73 20 69 6e 74 6f 20 61 1000 rows into a
189a9 20 74 61 62 6c 65 20 0a 20 20 20 20 20 20 2a 2a table . **
189aa 20 77 69 74 68 20 6e 6f 20 69 6e 64 65 78 65 73 with no indexes
189ab 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 using a single
189ac 70 72 65 70 61 72 65 64 20 49 4e 53 45 52 54 20 prepared INSERT
189ad 73 74 61 74 65 6d 65 6e 74 2c 20 62 69 6e 64 28 statement, bind(
189ae 29 20 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 ) . ** and
189af 72 65 73 65 74 28 29 2e 20 49 6e 73 65 72 74 73 reset(). Inserts
189b0 20 61 72 65 20 67 72 6f 75 70 65 64 20 69 6e 74 are grouped int
189b1 6f 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e o a transaction.
189b2 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
189b3 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 if( p->flags&(ME
189b4 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e 29 20 29 M_Agg|MEM_Dyn) )
189b5 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
189b6 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
189b7 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 p);. }else
189b8 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 if( p->zMalloc )
189b9 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
189ba 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
189bb 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 Malloc);.
189bc 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b p->zMalloc = 0;
189bd 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
189be 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e p->flags = MEM_N
189bf 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 ull;. }. d
189c0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
189c1 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b = malloc_failed;
189c2 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66 20 53 . }.}..#ifdef S
189c3 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
189c4 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 ORY_MANAGEMENT.S
189c5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
189c6 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c t sqlite3VdbeRel
189c7 65 61 73 65 42 75 66 66 65 72 73 28 56 64 62 65 easeBuffers(Vdbe
189c8 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a *p){. int ii;.
189c9 20 20 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b int nFree = 0;
189ca 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
189cb 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
189cc 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
189cd 20 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 3d 70 for(ii=1; ii<=p
189ce 2d 3e 6e 4d 65 6d 3b 20 69 69 2b 2b 29 7b 0a 20 ->nMem; ii++){.
189cf 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 Mem *pMem = &
189d0 70 2d 3e 61 4d 65 6d 5b 69 69 5d 3b 0a 20 20 20 p->aMem[ii];.
189d1 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
189d2 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b & MEM_RowSet ){
189d3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f . sqlite3Ro
189d4 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e wSetClear(pMem->
189d5 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 u.pRowSet);.
189d6 7d 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e }. if( pMem->
189d7 7a 20 26 26 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 z && pMem->flags
189d8 26 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 &MEM_Dyn ){.
189d9 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 2d assert( !pMem-
189da 3e 78 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 6e >xDel );. n
189db 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 44 Free += sqlite3D
189dc 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d bMallocSize(pMem
189dd 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a ->db, pMem->z);.
189de 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
189df 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d eMemRelease(pMem
189e0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 );. }. }. r
189e1 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 eturn nFree;.}.#
189e2 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
189e3 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 QLITE_OMIT_EXPLA
189e4 49 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 IN./*.** Give a
189e5 6c 69 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70 listing of the p
189e6 72 6f 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69 rogram in the vi
189e7 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a rtual machine..*
189e8 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 *.** The interfa
189e9 63 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 ce is the same a
189ea 73 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 s sqlite3VdbeExe
189eb 63 28 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61 c(). But instea
189ec 64 20 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 d of.** running
189ed 74 68 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 the code, it inv
189ee 6f 6b 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 okes the callbac
189ef 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 k once for each
189f0 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 instruction..**
189f1 54 68 69 73 20 66 65 61 74 75 72 65 20 69 73 20 This feature is
189f2 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
189f3 74 20 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a t "EXPLAIN"..**.
189f4 2a 2a 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 ** When p->expla
189f5 69 6e 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 in==1, each inst
189f6 72 75 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 ruction is liste
189f7 64 2e 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 d. When.** p->e
189f8 78 70 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 xplain==2, only
189f9 4f 50 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 OP_Explain instr
189fa 75 63 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 uctions are list
189fb 65 64 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 ed and these.**
189fc 61 72 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 are shown in a d
189fd 69 66 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e ifferent format.
189fe 20 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 p->explain==2
189ff 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 is used to imple
18a00 6d 65 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 ment.** EXPLAIN
18a01 51 55 45 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 QUERY PLAN..*/.S
18a02 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
18a03 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 t sqlite3VdbeLis
18a04 74 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20 t(. Vdbe *p
18a05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18a06 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b * The VDBE */.){
18a07 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
18a08 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 3b p->db;. int i;
18a09 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
18a0a 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20 2a 70 4d TE_OK;. Mem *pM
18a0b 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 em = p->pResultS
18a0c 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 31 5d et = &p->aMem[1]
18a0d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e ;.. assert( p->
18a0e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20 69 66 28 explain );. if(
18a0f 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f p->magic!=VDBE_
18a10 4d 41 47 49 43 5f 52 55 4e 20 29 20 72 65 74 75 MAGIC_RUN ) retu
18a11 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 rn SQLITE_MISUSE
18a12 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e ;. assert( db->
18a13 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 magic==SQLITE_MA
18a14 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 61 73 GIC_BUSY );. as
18a15 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c sert( p->rc==SQL
18a16 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d ITE_OK || p->rc=
18a17 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 =SQLITE_BUSY ||
18a18 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f p->rc==SQLITE_NO
18a19 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20 45 76 65 MEM );.. /* Eve
18a1a 6e 20 74 68 6f 75 67 68 20 74 68 69 73 20 6f 70 n though this op
18a1b 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20 75 73 code does not us
18a1c 65 20 64 79 6e 61 6d 69 63 20 73 74 72 69 6e 67 e dynamic string
18a1d 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72 s for. ** the r
18a1e 65 73 75 6c 74 2c 20 72 65 73 75 6c 74 20 63 6f esult, result co
18a1f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63 6f 6d 65 lumns may become
18a20 20 64 79 6e 61 6d 69 63 20 69 66 20 74 68 65 20 dynamic if the
18a21 75 73 65 72 20 63 61 6c 6c 73 0a 20 20 2a 2a 20 user calls. **
18a22 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
18a23 65 78 74 31 36 28 29 2c 20 63 61 75 73 69 6e 67 ext16(), causing
18a24 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 a translation t
18a25 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e o UTF-16 encodin
18a26 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c 65 61 73 g.. */. releas
18a27 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 6d 2c 20 eMemArray(pMem,
18a28 70 2d 3e 6e 4d 65 6d 29 3b 0a 0a 20 20 69 66 28 p->nMem);.. if(
18a29 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e p->rc==SQLITE_N
18a2a 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 OMEM ){. /* T
18a2b 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61 his happens if a
18a2c 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 malloc() inside
18a2d 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
18a2e 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 e3_column_text()
18a2f 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 or. ** sqlit
18a30 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
18a31 28 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 () failed. */.
18a32 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
18a33 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 led = 1;. ret
18a34 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
18a35 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 ;. }.. do{.
18a36 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 i = p->pc++;.
18a37 7d 77 68 69 6c 65 28 20 69 3c 70 2d 3e 6e 4f 70 }while( i<p->nOp
18a38 20 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d && p->explain==
18a39 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 2 && p->aOp[i].o
18a3a 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69 pcode!=OP_Explai
18a3b 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d n );. if( i>=p-
18a3c 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 72 >nOp ){. p->r
18a3d 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
18a3e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 rc = SQLITE_D
18a3f 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ONE;. }else if(
18a40 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 db->u1.isInterr
18a41 75 70 74 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e upted ){. p->
18a42 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 rc = SQLITE_INTE
18a43 52 52 55 50 54 3b 0a 20 20 20 20 72 63 20 3d 20 RRUPT;. rc =
18a44 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
18a45 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
18a46 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
18a47 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 db, "%s", sqlite
18a48 33 45 72 72 53 74 72 28 70 2d 3e 72 63 29 29 3b 3ErrStr(p->rc));
18a49 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 . }else{. ch
18a4a 61 72 20 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 ar *z;. Op *p
18a4b 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b Op = &p->aOp[i];
18a4c 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c . if( p->expl
18a4d 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 ain==1 ){.
18a4e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
18a4f 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d 65 M_Int;. pMe
18a50 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
18a51 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 _INTEGER;.
18a52 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 3b 20 20 pMem->u.i = i;
18a53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18a54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18a55 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 Program counter
18a56 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b */. pMem++
18a57 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d 65 6d 2d ;. . pMem-
18a58 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 61 >flags = MEM_Sta
18a59 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f tic|MEM_Str|MEM_
18a5a 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d Term;. pMem
18a5b 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 6c ->z = (char*)sql
18a5c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70 ite3OpcodeName(p
18a5d 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20 20 2f 2a Op->opcode); /*
18a5e 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 Opcode */.
18a5f 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a assert( pMem->z
18a60 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d 65 !=0 );. pMe
18a61 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 m->n = sqlite3St
18a62 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b rlen30(pMem->z);
18a63 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 . pMem->typ
18a64 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b e = SQLITE_TEXT;
18a65 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 . pMem->enc
18a66 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a = SQLITE_UTF8;.
18a67 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 pMem++;.
18a68 20 20 7d 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 }.. pMem->f
18a69 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
18a6a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 pMem->u.i =
18a6b 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20 20 20 20 pOp->p1;
18a6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18a6d 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20 20 20 70 /* P1 */. p
18a6e 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
18a6f 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 TE_INTEGER;.
18a70 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 pMem++;.. pMe
18a71 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 m->flags = MEM_I
18a72 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e nt;. pMem->u.
18a73 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20 i = pOp->p2;
18a74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18a75 20 20 20 20 20 20 2f 2a 20 50 32 20 2a 2f 0a 20 /* P2 */.
18a76 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
18a77 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a SQLITE_INTEGER;.
18a78 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 pMem++;..
18a79 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d if( p->explain=
18a7a 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d =1 ){. pMem
18a7b 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
18a7c 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 t;. pMem->u
18a7d 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 3b 20 20 20 .i = pOp->p3;
18a7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18a7f 20 20 20 20 20 20 20 2f 2a 20 50 33 20 2a 2f 0a /* P3 */.
18a80 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 pMem->type
18a81 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 = SQLITE_INTEGE
18a82 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b R;. pMem++;
18a83 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
18a84 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
18a85 6f 77 28 70 4d 65 6d 2c 20 33 32 2c 20 30 29 20 ow(pMem, 32, 0)
18a86 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ){ /*
18a87 20 50 34 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e P4 */. p->
18a88 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
18a89 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 = 1;. retu
18a8a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
18a8b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
18a8c 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e >flags = MEM_Dyn
18a8d 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 |MEM_Str|MEM_Ter
18a8e 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c m;. z = displ
18a8f 61 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e ayP4(pOp, pMem->
18a90 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20 z, 32);. if(
18a91 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 z!=pMem->z ){.
18a92 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
18a93 65 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a emSetStr(pMem, z
18a94 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 , -1, SQLITE_UTF
18a95 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 8, 0);. }else
18a96 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
18a97 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 pMem->z!=0 );.
18a98 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 pMem->n = sq
18a99 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d lite3Strlen30(pM
18a9a 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d em->z);. pM
18a9b 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 em->enc = SQLITE
18a9c 5f 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 _UTF8;. }.
18a9d 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
18a9e 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 LITE_TEXT;. p
18a9f 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 Mem++;.. if(
18aa0 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b p->explain==1 ){
18aa1 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
18aa2 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
18aa3 65 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 em, 4, 0) ){.
18aa4 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c p->db->mall
18aa5 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
18aa6 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
18aa7 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
18aa8 20 7d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 }. pMem->f
18aa9 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d lags = MEM_Dyn|M
18aaa 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b EM_Str|MEM_Term;
18aab 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d . pMem->n =
18aac 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 2;. sqlite
18aad 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 70 4d 3_snprintf(3, pM
18aae 65 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22 2c 20 70 em->z, "%.2x", p
18aaf 4f 70 2d 3e 70 35 29 3b 20 20 20 2f 2a 20 50 35 Op->p5); /* P5
18ab0 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e */. pMem->
18ab1 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 type = SQLITE_TE
18ab2 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e XT;. pMem->
18ab3 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 enc = SQLITE_UTF
18ab4 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 8;. pMem++;
18ab5 0a 20 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 . .#ifdef SQLIT
18ab6 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66 E_DEBUG. if
18ab7 28 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 ( pOp->zComment
18ab8 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d ){. pMem-
18ab9 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 >flags = MEM_Str
18aba 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 |MEM_Term;.
18abb 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70 pMem->z = pOp
18abc 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 ->zComment;.
18abd 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 pMem->n = sq
18abe 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d lite3Strlen30(pM
18abf 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 20 20 em->z);.
18ac0 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
18ac1 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 20 TE_UTF8;.
18ac2 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
18ac3 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 LITE_TEXT;.
18ac4 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 }else.#endif.
18ac5 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 4d {. pM
18ac6 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
18ac7 4e 75 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 Null;
18ac8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
18ac9 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20 omment */.
18aca 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
18acb 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 QLITE_NULL;.
18acc 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 }. }.. p
18acd 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 38 ->nResColumn = 8
18ace 20 2d 20 35 2a 28 70 2d 3e 65 78 70 6c 61 69 6e - 5*(p->explain
18acf 2d 31 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d -1);. p->rc =
18ad0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
18ad1 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b rc = SQLITE_ROW;
18ad2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
18ad3 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
18ad4 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 LITE_OMIT_EXPLAI
18ad5 4e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c N */..#ifdef SQL
18ad6 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ITE_DEBUG./*.**
18ad7 50 72 69 6e 74 20 74 68 65 20 53 51 4c 20 74 68 Print the SQL th
18ad8 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 20 67 at was used to g
18ad9 65 6e 65 72 61 74 65 20 61 20 56 44 42 45 20 70 enerate a VDBE p
18ada 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 rogram..*/.SQLIT
18adb 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
18adc 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 qlite3VdbePrintS
18add 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 ql(Vdbe *p){. i
18ade 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b nt nOp = p->nOp;
18adf 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a . VdbeOp *pOp;.
18ae0 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 if( nOp<1 ) re
18ae1 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 turn;. pOp = &p
18ae2 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20 ->aOp[0];. if(
18ae3 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
18ae4 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 Trace && pOp->p4
18ae5 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e .z!=0 ){. con
18ae6 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 st char *z = pOp
18ae7 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 77 68 69 6c ->p4.z;. whil
18ae8 65 28 20 69 73 73 70 61 63 65 28 2a 28 75 38 2a e( isspace(*(u8*
18ae9 29 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 70 )z) ) z++;. p
18aea 72 69 6e 74 66 28 22 53 51 4c 3a 20 5b 25 73 5d rintf("SQL: [%s]
18aeb 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 \n", z);. }.}.#
18aec 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 endif..#if !defi
18aed 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
18aee 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 TRACE) && define
18aef 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
18af0 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 50 IOTRACE)./*.** P
18af1 72 69 6e 74 20 61 6e 20 49 4f 54 52 41 43 45 20 rint an IOTRACE
18af2 6d 65 73 73 61 67 65 20 73 68 6f 77 69 6e 67 20 message showing
18af3 53 51 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a SQL content..*/.
18af4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
18af5 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 oid sqlite3VdbeI
18af6 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65 20 2a OTraceSql(Vdbe *
18af7 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 p){. int nOp =
18af8 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 p->nOp;. VdbeOp
18af9 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 73 71 6c *pOp;. if( sql
18afa 69 74 65 33 49 6f 54 72 61 63 65 3d 3d 30 20 29 ite3IoTrace==0 )
18afb 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6e return;. if( n
18afc 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 Op<1 ) return;.
18afd 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 pOp = &p->aOp[0
18afe 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 ];. if( pOp->op
18aff 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 code==OP_Trace &
18b00 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 & pOp->p4.z!=0 )
18b01 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a {. int i, j;.
18b02 20 20 20 20 63 68 61 72 20 7a 5b 31 30 30 30 5d char z[1000]
18b03 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e ;. sqlite3_sn
18b04 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 29 printf(sizeof(z)
18b05 2c 20 7a 2c 20 22 25 73 22 2c 20 70 4f 70 2d 3e , z, "%s", pOp->
18b06 70 34 2e 7a 29 3b 0a 20 20 20 20 66 6f 72 28 69 p4.z);. for(i
18b07 3d 30 3b 20 69 73 73 70 61 63 65 28 28 75 6e 73 =0; isspace((uns
18b08 69 67 6e 65 64 20 63 68 61 72 29 7a 5b 69 5d 29 igned char)z[i])
18b09 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 ; i++){}. for
18b0a 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 (j=0; z[i]; i++)
18b0b 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 73 70 {. if( issp
18b0c 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 ace((unsigned ch
18b0d 61 72 29 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20 ar)z[i]) ){.
18b0e 20 20 20 20 69 66 28 20 7a 5b 69 2d 31 5d 21 3d if( z[i-1]!=
18b0f 27 20 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ' ' ){.
18b10 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 z[j++] = ' ';.
18b11 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
18b12 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b else{. z[
18b13 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 j++] = z[i];.
18b14 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a }. }. z
18b15 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c [j] = 0;. sql
18b16 69 74 65 33 49 6f 54 72 61 63 65 28 22 53 51 4c ite3IoTrace("SQL
18b17 20 25 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a %s\n", z);. }.
18b18 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
18b19 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 26 ITE_OMIT_TRACE &
18b1a 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
18b1b 49 4f 54 52 41 43 45 20 2a 2f 0a 0a 0a 2f 2a 0a IOTRACE */.../*.
18b1c 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 72 ** Prepare a vir
18b1d 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72 tual machine for
18b1e 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 69 execution. Thi
18b1f 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67 s involves thing
18b20 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c s such.** as all
18b21 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 70 ocating stack sp
18b22 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 ace and initiali
18b23 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 6d zing the program
18b24 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 counter..** Aft
18b25 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73 20 er the VDBE has
18b26 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 63 be prepped, it c
18b27 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 62 an be executed b
18b28 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a y one or more.**
18b29 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
18b2a 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a 2a 3VdbeExec(). .*
18b2b 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 *.** This is the
18b2c 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 only way to mov
18b2d 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 44 e a VDBE from VD
18b2e 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f BE_MAGIC_INIT to
18b2f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 .** VDBE_MAGIC_R
18b30 55 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 UN..*/.SQLITE_PR
18b31 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
18b32 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 e3VdbeMakeReady(
18b33 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 . Vdbe *p,
18b34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b35 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f /* The VDBE */
18b36 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 20 . int nVar,
18b37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b38 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 /* Number of '
18b39 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20 53 51 ?' see in the SQ
18b3a 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 L statement */.
18b3b 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 20 int nMem,
18b3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b3d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d /* Number of mem
18b3e 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c 6c ory cells to all
18b3f 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e ocate */. int n
18b40 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20 20 20 Cursor,
18b41 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
18b42 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 74 ber of cursors t
18b43 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 o allocate */.
18b44 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 20 20 20 int isExplain
18b45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18b46 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 45 58 * True if the EX
18b47 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 69 PLAIN keywords i
18b48 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 29 7b 0a s present */.){.
18b49 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 int n;. sqlit
18b4a 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a e3 *db = p->db;.
18b4b 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 . assert( p!=0
18b4c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
18b4d 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
18b4e 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 2f 2a 20 C_INIT );.. /*
18b4f 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 There should be
18b50 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 63 at least one opc
18b51 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ode.. */. asse
18b52 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a rt( p->nOp>0 );.
18b53 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d 61 . /* Set the ma
18b54 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41 47 49 gic to VDBE_MAGI
18b55 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72 61 74 C_RUN sooner rat
18b56 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 2e 20 her than later.
18b57 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 */. p->magic =
18b58 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a VDBE_MAGIC_RUN;.
18b59 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 63 . /* For each c
18b5a 75 72 73 6f 72 20 72 65 71 75 69 72 65 64 2c 20 ursor required,
18b5b 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 also allocate a
18b5c 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d 65 6d memory cell. Mem
18b5d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 28 ory. ** cells (
18b5e 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 29 2e nMem+1-nCursor).
18b5f 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 76 65 .nMem, inclusive
18b60 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 , will never be
18b61 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68 65 used by. ** the
18b62 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 49 vdbe program. I
18b63 6e 73 74 65 61 64 20 74 68 65 79 20 61 72 65 20 nstead they are
18b64 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 used to allocate
18b65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 space for. **
18b66 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 75 72 VdbeCursor/BtCur
18b67 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 2e 20 sor structures.
18b68 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f The blob of memo
18b69 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ry associated wi
18b6a 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 th . ** cursor
18b6b 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 0 is stored in m
18b6c 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 6d 2e emory cell nMem.
18b6d 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 6e 4d Memory cell (nM
18b6e 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f 72 65 em-1). ** store
18b6f 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 s the blob of me
18b70 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 mory associated
18b71 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c 20 65 with cursor 1, e
18b72 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 65 tc.. **. ** Se
18b73 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61 74 65 e also: allocate
18b74 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20 Cursor().. */.
18b75 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73 6f 72 nMem += nCursor
18b76 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c ;.. /*. ** All
18b77 6f 63 61 74 69 6f 6e 20 73 70 61 63 65 20 66 6f ocation space fo
18b78 72 20 72 65 67 69 73 74 65 72 73 2e 0a 20 20 2a r registers.. *
18b79 2f 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 3d /. if( p->aMem=
18b7a 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 =0 ){. int nA
18b7b 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 rg; /* Max
18b7c 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 imum number of a
18b7d 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 61 20 rgs passed to a
18b7e 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 2a user function. *
18b7f 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 56 /. resolveP2V
18b80 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 3b alues(p, &nArg);
18b81 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 56 61 . assert( nVa
18b82 72 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 r>=0 );. if(
18b83 69 73 45 78 70 6c 61 69 6e 20 26 26 20 6e 4d 65 isExplain && nMe
18b84 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 20 6e 4d m<10 ){. nM
18b85 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 7d 0a 20 em = 10;. }.
18b86 20 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 73 71 6c p->aMem = sql
18b87 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
18b88 28 64 62 2c 0a 20 20 20 20 20 20 20 20 6e 4d 65 (db,. nMe
18b89 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 20 20 20 m*sizeof(Mem)
18b8a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 /* a
18b8b 4d 65 6d 20 2a 2f 0a 20 20 20 20 20 20 2b 20 6e Mem */. + n
18b8c 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 20 Var*sizeof(Mem)
18b8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18b8e 20 61 56 61 72 20 2a 2f 0a 20 20 20 20 20 20 2b aVar */. +
18b8f 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d nArg*sizeof(Mem
18b90 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *)
18b91 2f 2a 20 61 70 41 72 67 20 2a 2f 0a 20 20 20 20 /* apArg */.
18b92 20 20 2b 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 + nVar*sizeof(
18b93 63 68 61 72 2a 29 20 20 20 20 20 20 20 20 20 20 char*)
18b94 20 20 20 2f 2a 20 61 7a 56 61 72 20 2a 2f 0a 20 /* azVar */.
18b95 20 20 20 20 20 2b 20 6e 43 75 72 73 6f 72 2a 73 + nCursor*s
18b96 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 izeof(VdbeCursor
18b97 2a 29 2b 31 20 20 2f 2a 20 61 70 43 73 72 20 2a *)+1 /* apCsr *
18b98 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 /. );. if(
18b99 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c !db->mallocFail
18b9a 65 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 ed ){. p->a
18b9b 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 20 20 20 Mem--;
18b9c 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 /* aMem[] goe
18b9d 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a s from 1..nMem *
18b9e 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 /. p->nMem
18b9f 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 2f = nMem; /
18ba0 2a 20 20 20 20 20 20 20 6e 6f 74 20 66 72 6f 6d * not from
18ba1 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a 2f 0a 20 20 0..nMem-1 */.
18ba2 20 20 20 20 70 2d 3e 61 56 61 72 20 3d 20 26 70 p->aVar = &p
18ba3 2d 3e 61 4d 65 6d 5b 6e 4d 65 6d 2b 31 5d 3b 0a ->aMem[nMem+1];.
18ba4 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 20 3d 20 p->nVar =
18ba5 6e 56 61 72 3b 0a 20 20 20 20 20 20 70 2d 3e 6f nVar;. p->o
18ba6 6b 56 61 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 kVar = 0;.
18ba7 70 2d 3e 61 70 41 72 67 20 3d 20 28 4d 65 6d 2a p->apArg = (Mem*
18ba8 2a 29 26 70 2d 3e 61 56 61 72 5b 6e 56 61 72 5d *)&p->aVar[nVar]
18ba9 3b 0a 20 20 20 20 20 20 70 2d 3e 61 7a 56 61 72 ;. p->azVar
18baa 20 3d 20 28 63 68 61 72 2a 2a 29 26 70 2d 3e 61 = (char**)&p->a
18bab 70 41 72 67 5b 6e 41 72 67 5d 3b 0a 20 20 20 20 pArg[nArg];.
18bac 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28 56 64 p->apCsr = (Vd
18bad 62 65 43 75 72 73 6f 72 2a 2a 29 26 70 2d 3e 61 beCursor**)&p->a
18bae 7a 56 61 72 5b 6e 56 61 72 5d 3b 0a 20 20 20 20 zVar[nVar];.
18baf 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 6e p->nCursor = n
18bb0 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 66 6f Cursor;. fo
18bb1 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b 20 6e r(n=0; n<nVar; n
18bb2 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e ++){. p->
18bb3 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 aVar[n].flags =
18bb4 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 MEM_Null;.
18bb5 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62 20 p->aVar[n].db
18bb6 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = db;. }.
18bb7 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e 3c 3d for(n=1; n<=
18bb8 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 nMem; n++){.
18bb9 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 66 p->aMem[n].f
18bba 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
18bbb 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d . p->aMem
18bbc 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 [n].db = db;.
18bbd 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 }. }. }.#
18bbe 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
18bbf 55 47 0a 20 20 66 6f 72 28 6e 3d 31 3b 20 6e 3c UG. for(n=1; n<
18bc0 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 p->nMem; n++){.
18bc1 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d assert( p->aM
18bc2 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62 20 29 3b 0a em[n].db==db );.
18bc3 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 70 2d }.#endif.. p-
18bc4 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d 3e 72 >pc = -1;. p->r
18bc5 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
18bc6 20 70 2d 3e 75 6e 69 71 75 65 43 6e 74 20 3d 20 p->uniqueCnt =
18bc7 30 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 0;. p->errorAct
18bc8 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a ion = OE_Abort;.
18bc9 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d 20 p->explain |=
18bca 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d 3e isExplain;. p->
18bcb 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 magic = VDBE_MAG
18bcc 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43 68 IC_RUN;. p->nCh
18bcd 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 ange = 0;. p->c
18bce 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20 70 acheCtr = 1;. p
18bcf 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f ->minWriteFileFo
18bd0 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70 2d rmat = 255;. p-
18bd1 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e 74 >openedStatement
18bd2 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44 42 = 0;.#ifdef VDB
18bd3 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 E_PROFILE. {.
18bd4 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 int i;. for
18bd5 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 (i=0; i<p->nOp;
18bd6 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 i++){. p->a
18bd7 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 Op[i].cnt = 0;.
18bd8 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 p->aOp[i].c
18bd9 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d ycles = 0;. }
18bda 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f . }.#endif.}../
18bdb 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 42 *.** Close a VDB
18bdc 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65 6c E cursor and rel
18bdd 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65 73 ease all the res
18bde 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72 73 ources that curs
18bdf 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74 or .** happens t
18be0 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 o hold..*/.SQLIT
18be1 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
18be2 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 qlite3VdbeFreeCu
18be3 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 64 rsor(Vdbe *p, Vd
18be4 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b 0a beCursor *pCx){.
18be5 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b 0a if( pCx==0 ){.
18be6 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
18be7 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20 29 if( pCx->pBt )
18be8 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 {. sqlite3Btr
18be9 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74 eeClose(pCx->pBt
18bea 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 43 );. /* The pC
18beb 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c 20 x->pCursor will
18bec 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74 be close automat
18bed 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 78 ically, if it ex
18bee 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a 20 ists, by. **
18bef 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 the call above.
18bf0 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 */. }else if( p
18bf1 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 Cx->pCursor ){.
18bf2 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
18bf3 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d 3e loseCursor(pCx->
18bf4 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 pCursor);. }.#i
18bf5 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18bf6 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
18bf7 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 43 if( pCx->pVtabC
18bf8 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c ursor ){. sql
18bf9 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
18bfa 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 *pVtabCursor =
18bfb 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 pCx->pVtabCursor
18bfc 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 ;. const sqli
18bfd 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
18bfe 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 ule = pCx->pModu
18bff 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 le;. p->inVta
18c00 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 bMethod = 1;.
18c01 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
18c02 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a fetyOff(p->db);.
18c03 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c pModule->xCl
18c04 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 29 ose(pVtabCursor)
18c05 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 ;. (void)sqli
18c06 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64 te3SafetyOn(p->d
18c07 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 b);. p->inVta
18c08 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d bMethod = 0;. }
18c09 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 70 .#endif. if( !p
18c0a 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 Cx->ephemPseudoT
18c0b 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 able ){. sqli
18c0c 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c te3DbFree(p->db,
18c0d 20 70 43 78 2d 3e 70 44 61 74 61 29 3b 0a 20 20 pCx->pData);.
18c0e 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 }.}../*.** Close
18c0f 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78 63 all cursors exc
18c10 65 70 74 20 66 6f 72 20 56 54 61 62 20 63 75 72 ept for VTab cur
18c11 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 63 75 sors that are cu
18c12 72 72 65 6e 74 6c 79 0a 2a 2a 20 69 6e 20 75 73 rrently.** in us
18c13 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
18c14 64 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 d closeAllCursor
18c15 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74 61 sExceptActiveVta
18c16 62 73 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 bs(Vdbe *p){. i
18c17 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 2d 3e 61 nt i;. if( p->a
18c18 70 43 73 72 3d 3d 30 20 29 20 72 65 74 75 72 6e pCsr==0 ) return
18c19 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
18c1a 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b 29 7b ->nCursor; i++){
18c1b 0a 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 . VdbeCursor
18c1c 2a 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 *pC = p->apCsr[i
18c1d 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 20 26 26 ];. if( pC &&
18c1e 20 28 21 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 (!p->inVtabMeth
18c1f 6f 64 20 7c 7c 20 21 70 43 2d 3e 70 56 74 61 62 od || !pC->pVtab
18c20 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 20 Cursor) ){.
18c21 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 sqlite3VdbeFree
18c22 43 75 72 73 6f 72 28 70 2c 20 70 43 29 3b 0a 20 Cursor(p, pC);.
18c23 20 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d p->apCsr[i]
18c24 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
18c25 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 }../*.** Clean u
18c26 70 20 74 68 65 20 56 4d 20 61 66 74 65 72 20 65 p the VM after e
18c27 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 xecution..**.**
18c28 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
18c29 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 l automatically
18c2a 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 6f 72 close any cursor
18c2b 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 s, lists, and/or
18c2c 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 61 74 .** sorters that
18c2d 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e were left open.
18c2e 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 It also delete
18c2f 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 0a s the values of.
18c30 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 ** variables in
18c31 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72 61 79 the aVar[] array
18c32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
18c33 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20 2a 70 Cleanup(Vdbe *p
18c34 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 ){. int i;. sq
18c35 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 lite3 *db = p->d
18c36 62 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a b;. Mem *pMem;.
18c37 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 closeAllCursor
18c38 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74 61 sExceptActiveVta
18c39 62 73 28 70 29 3b 0a 20 20 66 6f 72 28 70 4d 65 bs(p);. for(pMe
18c3a 6d 3d 26 70 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 69 m=&p->aMem[1], i
18c3b 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 =1; i<=p->nMem;
18c3c 69 2b 2b 2c 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 i++, pMem++){.
18c3d 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 if( pMem->flag
18c3e 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 s & MEM_RowSet )
18c3f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 {. sqlite3R
18c40 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d owSetClear(pMem-
18c41 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 >u.pRowSet);.
18c42 20 7d 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 }. MemSetTyp
18c43 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f eFlag(pMem, MEM_
18c44 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 6c Null);. }. rel
18c45 65 61 73 65 4d 65 6d 41 72 72 61 79 28 26 70 2d easeMemArray(&p-
18c46 3e 61 4d 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 >aMem[1], p->nMe
18c47 6d 29 3b 0a 20 20 69 66 28 20 70 2d 3e 63 6f 6e m);. if( p->con
18c48 74 65 78 74 53 74 61 63 6b 20 29 7b 0a 20 20 20 textStack ){.
18c49 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
18c4a 62 2c 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 b, p->contextSta
18c4b 63 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 63 6f ck);. }. p->co
18c4c 6e 74 65 78 74 53 74 61 63 6b 20 3d 20 30 3b 0a ntextStack = 0;.
18c4d 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 p->contextStac
18c4e 6b 44 65 70 74 68 20 3d 20 30 3b 0a 20 20 70 2d kDepth = 0;. p-
18c4f 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 >contextStackTop
18c50 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 = 0;. sqlite3D
18c51 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
18c52 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 rMsg);. p->zErr
18c53 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 Msg = 0;. p->pR
18c54 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d 0a esultSet = 0;.}.
18c55 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e ./*.** Set the n
18c56 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 umber of result
18c57 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 6c columns that wil
18c58 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 l be returned by
18c59 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61 this SQL.** sta
18c5a 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 tement. This is
18c5b 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70 69 now set at compi
18c5c 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72 20 le time, rather
18c5d 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65 than during.** e
18c5e 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 xecution of the
18c5f 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f 20 vdbe program so
18c60 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c that sqlite3_col
18c61 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a umn_count() can.
18c62 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 ** be called on
18c63 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 an SQL statement
18c64 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f before sqlite3_
18c65 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 step()..*/.SQLIT
18c66 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
18c67 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d qlite3VdbeSetNum
18c68 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e Cols(Vdbe *p, in
18c69 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 t nResColumn){.
18c6a 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a Mem *pColName;.
18c6b 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 int n;. sqlit
18c6c 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a e3 *db = p->db;.
18c6d 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 . releaseMemArr
18c6e 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 ay(p->aColName,
18c6f 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f p->nResColumn*CO
18c70 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 LNAME_N);. sqli
18c71 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
18c72 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 >aColName);. n
18c73 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c = nResColumn*COL
18c74 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 NAME_N;. p->nRe
18c75 73 43 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73 43 6f sColumn = nResCo
18c76 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c 4e lumn;. p->aColN
18c77 61 6d 65 20 3d 20 70 43 6f 6c 4e 61 6d 65 20 3d ame = pColName =
18c78 20 28 4d 65 6d 2a 29 73 71 6c 69 74 65 33 44 62 (Mem*)sqlite3Db
18c79 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 MallocZero(db, s
18c7a 69 7a 65 6f 66 28 4d 65 6d 29 2a 6e 20 29 3b 0a izeof(Mem)*n );.
18c7b 20 20 69 66 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d if( p->aColNam
18c7c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 e==0 ) return;.
18c7d 20 77 68 69 6c 65 28 20 6e 2d 2d 20 3e 20 30 20 while( n-- > 0
18c7e 29 7b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d ){. pColName-
18c7f 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c >flags = MEM_Nul
18c80 6c 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d l;. pColName-
18c81 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 >db = p->db;.
18c82 20 70 43 6f 6c 4e 61 6d 65 2b 2b 3b 0a 20 20 7d pColName++;. }
18c83 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
18c84 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 e name of the id
18c85 78 27 74 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 x'th column to b
18c86 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 e returned by th
18c87 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e e SQL statement.
18c88 0a 2a 2a 20 7a 4e 61 6d 65 20 6d 75 73 74 20 62 .** zName must b
18c89 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 e a pointer to a
18c8a 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 nul terminated
18c8b 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 string..**.** Th
18c8c 69 73 20 63 61 6c 6c 20 6d 75 73 74 20 62 65 20 is call must be
18c8d 6d 61 64 65 20 61 66 74 65 72 20 61 20 63 61 6c made after a cal
18c8e 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 l to sqlite3Vdbe
18c8f 53 65 74 4e 75 6d 43 6f 6c 73 28 29 2e 0a 2a 2a SetNumCols()..**
18c90 0a 2a 2a 20 54 68 65 20 66 69 6e 61 6c 20 70 61 .** The final pa
18c91 72 61 6d 65 74 65 72 2c 20 78 44 65 6c 2c 20 6d rameter, xDel, m
18c92 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53 51 ust be one of SQ
18c93 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c 20 53 51 LITE_DYNAMIC, SQ
18c94 4c 49 54 45 5f 53 54 41 54 49 43 0a 2a 2a 20 6f LITE_STATIC.** o
18c95 72 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 r SQLITE_TRANSIE
18c96 4e 54 2e 20 49 66 20 69 74 20 69 73 20 53 51 4c NT. If it is SQL
18c97 49 54 45 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 ITE_DYNAMIC, the
18c98 6e 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 n the buffer poi
18c99 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 7a 4e nted.** to by zN
18c9a 61 6d 65 20 77 69 6c 6c 20 62 65 20 66 72 65 65 ame will be free
18c9b 64 20 62 79 20 73 71 6c 69 74 65 33 44 62 46 72 d by sqlite3DbFr
18c9c 65 65 28 29 20 77 68 65 6e 20 74 68 65 20 76 64 ee() when the vd
18c9d 62 65 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e be is destroyed.
18c9e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18c9f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
18ca0 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 0a 20 20 beSetColName(.
18ca1 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 Vdbe *p,
18ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18ca3 20 2f 2a 20 56 64 62 65 20 62 65 69 6e 67 20 63 /* Vdbe being c
18ca4 6f 6e 66 69 67 75 72 65 64 20 2a 2f 0a 20 20 69 onfigured */. i
18ca5 6e 74 20 69 64 78 2c 20 20 20 20 20 20 20 20 20 nt idx,
18ca6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18ca7 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 /* Index of colu
18ca8 6d 6e 20 7a 4e 61 6d 65 20 61 70 70 6c 69 65 73 mn zName applies
18ca9 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 76 61 72 to */. int var
18caa 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
18cab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
18cac 20 6f 66 20 74 68 65 20 43 4f 4c 4e 41 4d 45 5f of the COLNAME_
18cad 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 * constants */.
18cae 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
18caf 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 me,
18cb0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
18cb1 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e buffer containin
18cb2 67 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 g name */. void
18cb3 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 (*xDel)(void*)
18cb4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18cb5 4d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e Memory managemen
18cb6 74 20 73 74 72 61 74 65 67 79 20 66 6f 72 20 7a t strategy for z
18cb7 4e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 Name */.){. int
18cb8 20 72 63 3b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c rc;. Mem *pCol
18cb9 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 28 20 Name;. assert(
18cba 69 64 78 3c 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d idx<p->nResColum
18cbb 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 n );. assert( v
18cbc 61 72 3c 43 4f 4c 4e 41 4d 45 5f 4e 20 29 3b 0a ar<COLNAME_N );.
18cbd 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c if( p->db->mal
18cbe 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
18cbf 20 61 73 73 65 72 74 28 20 21 7a 4e 61 6d 65 20 assert( !zName
18cc0 7c 7c 20 78 44 65 6c 21 3d 53 51 4c 49 54 45 5f || xDel!=SQLITE_
18cc1 44 59 4e 41 4d 49 43 20 29 3b 0a 20 20 20 20 72 DYNAMIC );. r
18cc2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
18cc3 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 EM;. }. assert
18cc4 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30 ( p->aColName!=0
18cc5 20 29 3b 0a 20 20 70 43 6f 6c 4e 61 6d 65 20 3d );. pColName =
18cc6 20 26 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69 &(p->aColName[i
18cc7 64 78 2b 76 61 72 2a 70 2d 3e 6e 52 65 73 43 6f dx+var*p->nResCo
18cc8 6c 75 6d 6e 5d 29 3b 0a 20 20 72 63 20 3d 20 73 lumn]);. rc = s
18cc9 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
18cca 53 74 72 28 70 43 6f 6c 4e 61 6d 65 2c 20 7a 4e Str(pColName, zN
18ccb 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f ame, -1, SQLITE_
18ccc 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 20 20 61 UTF8, xDel);. a
18ccd 73 73 65 72 74 28 20 72 63 21 3d 30 20 7c 7c 20 ssert( rc!=0 ||
18cce 21 7a 4e 61 6d 65 20 7c 7c 20 28 70 43 6f 6c 4e !zName || (pColN
18ccf 61 6d 65 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 54 ame->flags&MEM_T
18cd0 65 72 6d 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 erm)!=0 );. ret
18cd1 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
18cd2 20 41 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 A read or write
18cd3 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 transaction may
18cd4 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 or may not be a
18cd5 63 74 69 76 65 20 6f 6e 20 64 61 74 61 62 61 73 ctive on databas
18cd6 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 e handle.** db.
18cd7 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e If a transaction
18cd8 20 69 73 20 61 63 74 69 76 65 2c 20 63 6f 6d 6d is active, comm
18cd9 69 74 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 it it. If there
18cda 69 73 20 61 0a 2a 2a 20 77 72 69 74 65 2d 74 72 is a.** write-tr
18cdb 61 6e 73 61 63 74 69 6f 6e 20 73 70 61 6e 6e 69 ansaction spanni
18cdc 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 ng more than one
18cdd 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
18cde 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
18cdf 74 61 6b 65 73 20 63 61 72 65 20 6f 66 20 74 68 takes care of th
18ce0 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
18ce1 20 74 72 69 63 6b 65 72 79 2e 0a 2a 2f 0a 73 74 trickery..*/.st
18ce2 61 74 69 63 20 69 6e 74 20 76 64 62 65 43 6f 6d atic int vdbeCom
18ce3 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c mit(sqlite3 *db,
18ce4 20 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 Vdbe *p){. int
18ce5 20 69 3b 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 i;. int nTrans
18ce6 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 = 0; /* Number
18ce7 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 77 69 of databases wi
18ce8 74 68 20 61 6e 20 61 63 74 69 76 65 20 77 72 69 th an active wri
18ce9 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a te-transaction *
18cea 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
18ceb 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 65 ITE_OK;. int ne
18cec 65 64 58 63 6f 6d 6d 69 74 20 3d 20 30 3b 0a 0a edXcommit = 0;..
18ced 20 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e /* Before doin
18cee 67 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c g anything else,
18cef 20 63 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 call the xSync(
18cf0 29 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 ) callback for a
18cf1 6e 79 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 ny. ** virtual
18cf2 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72 module tables wr
18cf3 69 74 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72 itten in this tr
18cf4 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 ansaction. This
18cf5 68 61 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 has to. ** be d
18cf6 6f 6e 65 20 62 65 66 6f 72 65 20 64 65 74 65 72 one before deter
18cf7 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61 mining whether a
18cf8 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
18cf9 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 file is . ** re
18cfa 71 75 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53 quired, as an xS
18cfb 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d ync() callback m
18cfc 61 79 20 61 64 64 20 61 6e 20 61 74 74 61 63 68 ay add an attach
18cfd 65 64 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a ed database. **
18cfe 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 to the transact
18cff 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d ion.. */. rc =
18d00 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 sqlite3VtabSync
18d01 28 64 62 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 (db, &p->zErrMsg
18d02 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
18d03 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 ITE_OK ){. re
18d04 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
18d05 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 /* This loop det
18d06 65 72 6d 69 6e 65 73 20 28 61 29 20 69 66 20 74 ermines (a) if t
18d07 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 he commit hook s
18d08 68 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 hould be invoked
18d09 20 61 6e 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f and. ** (b) ho
18d0a 77 20 6d 61 6e 79 20 64 61 74 61 62 61 73 65 20 w many database
18d0b 66 69 6c 65 73 20 68 61 76 65 20 6f 70 65 6e 20 files have open
18d0c 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
18d0d 6e 73 2c 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e ns, not . ** in
18d0e 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 6d 70 cluding the temp
18d0f 20 64 61 74 61 62 61 73 65 2e 20 28 62 29 20 69 database. (b) i
18d10 73 20 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 61 s important beca
18d11 75 73 65 20 69 66 20 6d 6f 72 65 20 74 68 61 6e use if more than
18d12 20 0a 20 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62 . ** one datab
18d13 61 73 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 ase file has an
18d14 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 open write trans
18d15 61 63 74 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72 action, a master
18d16 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 journal. ** fi
18d17 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 le is required f
18d18 6f 72 20 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d or an atomic com
18d19 6d 69 74 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 mit.. */ . for
18d1a 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b (i=0; i<db->nDb;
18d1b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 42 74 72 65 i++){ . Btre
18d1c 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 e *pBt = db->aDb
18d1d 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 [i].pBt;. if(
18d1e 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
18d1f 6e 54 72 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 nTrans(pBt) ){.
18d20 20 20 20 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74 needXcommit
18d21 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 = 1;. if(
18d22 69 21 3d 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b i!=1 ) nTrans++;
18d23 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
18d24 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e If there are an
18d25 79 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 y write-transact
18d26 69 6f 6e 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76 ions at all, inv
18d27 6f 6b 65 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 oke the commit h
18d28 6f 6f 6b 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65 ook */. if( nee
18d29 64 58 63 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e dXcommit && db->
18d2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 xCommitCallback
18d2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 ){. assert( (
18d2c 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 db->flags & SQLI
18d2d 54 45 5f 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d TE_CommitBusy)==
18d2e 30 20 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 0 );. db->fla
18d2f 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 43 6f 6d gs |= SQLITE_Com
18d30 6d 69 74 42 75 73 79 3b 0a 20 20 20 20 28 76 6f mitBusy;. (vo
18d31 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
18d32 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63 20 Off(db);. rc
18d33 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c = db->xCommitCal
18d34 6c 62 61 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69 lback(db->pCommi
18d35 74 41 72 67 29 3b 0a 20 20 20 20 28 76 6f 69 64 tArg);. (void
18d36 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e )sqlite3SafetyOn
18d37 28 64 62 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c (db);. db->fl
18d38 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 43 ags &= ~SQLITE_C
18d39 6f 6d 6d 69 74 42 75 73 79 3b 0a 20 20 20 20 69 ommitBusy;. i
18d3a 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 f( rc ){. r
18d3b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e eturn SQLITE_CON
18d3c 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 STRAINT;. }.
18d3d 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d }.. /* The sim
18d3e 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f ple case - no mo
18d3f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 re than one data
18d40 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 63 base file (not c
18d41 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a ounting the. **
18d42 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 29 20 TEMP database)
18d43 68 61 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f has a transactio
18d44 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68 65 72 n active. Ther
18d45 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 e is no need for
18d46 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72 the. ** master
18d47 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 -journal.. **.
18d48 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74 75 72 ** If the retur
18d49 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 n value of sqlit
18d4a 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 e3BtreeGetFilena
18d4b 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f 20 6c me() is a zero l
18d4c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e ength. ** strin
18d4d 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 g, it means the
18d4e 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 main database is
18d4f 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74 :memory: or a t
18d50 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e 20 0a 20 emp file. In .
18d51 20 2a 2a 20 74 68 61 74 20 63 61 73 65 20 77 65 ** that case we
18d52 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 do not support
18d53 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c atomic multi-fil
18d54 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73 e commits, so us
18d55 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 e the . ** simp
18d56 6c 65 20 63 61 73 65 20 74 68 65 6e 20 74 6f 6f le case then too
18d57 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d .. */. if( 0==
18d58 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
18d59 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 sqlite3BtreeGetF
18d5a 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b ilename(db->aDb[
18d5b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c 7c 20 6e 0].pBt)). || n
18d5c 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 20 Trans<=1. ){.
18d5d 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 for(i=0; rc==S
18d5e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 QLITE_OK && i<db
18d5f 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 ->nDb; i++){ .
18d60 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d Btree *pBt =
18d61 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
18d62 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 . if( pBt )
18d63 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
18d64 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
18d65 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 tPhaseOne(pBt, 0
18d66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
18d67 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 .. /* Do the
18d68 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 commit only if a
18d69 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73 75 63 ll databases suc
18d6a 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 cessfully comple
18d6b 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20 20 te phase 1. .
18d6c 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 ** If one of th
18d6d 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e BtreeCommitPha
18d6e 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 seOne() calls fa
18d6f 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63 61 ils, this indica
18d70 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f tes an. ** IO
18d71 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65 6c error while del
18d72 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 eting or truncat
18d73 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 ing a journal fi
18d74 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 le. It is unlike
18d75 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 ly,. ** but c
18d76 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 ould happen. In
18d77 74 68 69 73 20 63 61 73 65 20 61 62 61 6e 64 6f this case abando
18d78 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 n processing and
18d79 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f return the erro
18d7a 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f r.. */. fo
18d7b 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 r(i=0; rc==SQLIT
18d7c 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 E_OK && i<db->nD
18d7d 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 b; i++){. B
18d7e 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e tree *pBt = db->
18d7f 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
18d80 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 if( pBt ){.
18d81 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
18d82 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
18d83 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 eTwo(pBt);.
18d84 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
18d85 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
18d86 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
18d87 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 tabCommit(db);.
18d88 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 }. }.. /* T
18d89 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 65 20 he complex case
18d8a 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d 75 6c - There is a mul
18d8b 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d 74 72 ti-file write-tr
18d8c 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76 65 ansaction active
18d8d 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 71 75 .. ** This requ
18d8e 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 6a 6f ires a master jo
18d8f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e urnal file to en
18d90 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 sure the transac
18d91 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d tion is. ** com
18d92 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e mitted atomicly.
18d93 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 . */.#ifndef SQ
18d94 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f LITE_OMIT_DISKIO
18d95 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c . else{. sql
18d96 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d ite3_vfs *pVfs =
18d97 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 db->pVfs;. i
18d98 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b nt needSync = 0;
18d99 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 . char *zMast
18d9a 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c er = 0; /* Fil
18d9b 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 6d e-name for the m
18d9c 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f aster journal */
18d9d 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 . char const
18d9e 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c *zMainFile = sql
18d9f 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 ite3BtreeGetFile
18da0 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e name(db->aDb[0].
18da1 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 pBt);. sqlite
18da2 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 20 3_file *pMaster
18da3 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 = 0;. i64 off
18da4 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 set = 0;. int
18da5 20 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 res;.. /* Se
18da6 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20 6a 6f lect a master jo
18da7 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
18da8 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 */. do {.
18da9 20 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b 0a 20 u32 iRandom;.
18daa 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
18dab 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b ee(db, zMaster);
18dac 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
18dad 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 andomness(sizeof
18dae 28 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52 61 6e (iRandom), &iRan
18daf 64 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d 61 73 dom);. zMas
18db0 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 ter = sqlite3MPr
18db1 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d 6a 25 intf(db, "%s-mj%
18db2 30 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c 65 2c 08X", zMainFile,
18db3 20 69 52 61 6e 64 6f 6d 26 30 78 37 66 66 66 66 iRandom&0x7ffff
18db4 66 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 fff);. if(
18db5 21 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 !zMaster ){.
18db6 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
18db7 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d E_NOMEM;. }
18db8 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
18db9 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 te3OsAccess(pVfs
18dba 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 , zMaster, SQLIT
18dbb 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c E_ACCESS_EXISTS,
18dbc 20 26 72 65 73 29 3b 0a 20 20 20 20 7d 77 68 69 &res);. }whi
18dbd 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
18dbe 4b 20 26 26 20 72 65 73 20 29 3b 0a 20 20 20 20 K && res );.
18dbf 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
18dc0 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70 K ){. /* Op
18dc1 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f en the master jo
18dc2 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 urnal. */.
18dc3 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 rc = sqlite3OsOp
18dc4 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c 20 7a enMalloc(pVfs, z
18dc5 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74 65 72 Master, &pMaster
18dc6 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c , . SQL
18dc7 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
18dc8 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 TE|SQLITE_OPEN_C
18dc9 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20 20 REATE|.
18dca 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 SQLITE_OPEN_EXC
18dcb 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f 4f 50 LUSIVE|SQLITE_OP
18dcc 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 EN_MASTER_JOURNA
18dcd 4c 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 L, 0. );.
18dce 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d }. if( rc!=
18dcf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
18dd0 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
18dd1 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 (db, zMaster);.
18dd2 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
18dd3 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a 20 57 }. . /* W
18dd4 72 69 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 rite the name of
18dd5 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 each database f
18dd6 69 6c 65 20 69 6e 20 74 68 65 20 74 72 61 6e 73 ile in the trans
18dd7 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 action into the
18dd8 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 new. ** maste
18dd9 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 r journal file.
18dda 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
18ddb 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 rs at this point
18ddc 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 61 6e close. ** an
18ddd 64 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 d delete the mas
18dde 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
18ddf 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 . All the indivi
18de0 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c dual journal fil
18de1 65 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 es. ** still
18de2 68 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 20 74 have 'null' as t
18de3 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
18de4 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 74 68 l pointer, so th
18de5 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20 ey will roll.
18de6 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e ** back indepen
18de7 64 65 6e 74 6c 79 20 69 66 20 61 20 66 61 69 6c dently if a fail
18de8 75 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 20 20 ure occurs..
18de9 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 */. for(i=0;
18dea 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
18deb 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 . Btree *pB
18dec 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 t = db->aDb[i].p
18ded 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d Bt;. if( i=
18dee 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 =1 ) continue;
18def 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 54 /* Ignore the T
18df0 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a EMP database */.
18df1 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
18df2 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 3BtreeIsInTrans(
18df3 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 pBt) ){.
18df4 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c char const *zFil
18df5 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 e = sqlite3Btree
18df6 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 GetJournalname(p
18df7 42 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 Bt);. if(
18df8 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 20 63 zFile[0]==0 ) c
18df9 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 67 6e ontinue; /* Ign
18dfa 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 ore :memory: dat
18dfb 61 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20 20 abases */.
18dfc 20 20 69 66 28 20 21 6e 65 65 64 53 79 6e 63 20 if( !needSync
18dfd 26 26 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 && !sqlite3Btree
18dfe 53 79 6e 63 44 69 73 61 62 6c 65 64 28 70 42 74 SyncDisabled(pBt
18dff 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e ) ){. n
18e00 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 eedSync = 1;.
18e01 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 }. r
18e02 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
18e03 74 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 69 6c te(pMaster, zFil
18e04 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e e, sqlite3Strlen
18e05 33 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66 30(zFile)+1, off
18e06 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 set);. of
18e07 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 53 fset += sqlite3S
18e08 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 trlen30(zFile)+1
18e09 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
18e0a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
18e0b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
18e0c 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 OsCloseFree(pMas
18e0d 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ter);.
18e0e 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
18e0f 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 pVfs, zMaster, 0
18e10 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
18e11 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
18e12 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 Master);.
18e13 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
18e14 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
18e15 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 }.. /* Sy
18e16 6e 63 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f nc the master jo
18e17 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 urnal file. If t
18e18 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 he IOCAP_SEQUENT
18e19 49 41 4c 20 64 65 76 69 63 65 0a 20 20 20 20 2a IAL device. *
18e1a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68 * flag is set th
18e1b 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 is is not requir
18e1c 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a ed.. */. z
18e1d 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 MainFile = sqlit
18e1e 65 33 42 74 72 65 65 47 65 74 44 69 72 6e 61 6d e3BtreeGetDirnam
18e1f 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 e(db->aDb[0].pBt
18e20 29 3b 0a 20 20 20 20 69 66 28 20 28 6e 65 65 64 );. if( (need
18e21 53 79 6e 63 20 0a 20 20 20 20 20 26 26 20 28 30 Sync . && (0
18e22 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 ==(sqlite3OsDevi
18e23 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
18e24 73 28 70 4d 61 73 74 65 72 29 26 53 51 4c 49 54 s(pMaster)&SQLIT
18e25 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 E_IOCAP_SEQUENTI
18e26 41 4c 29 29 0a 20 20 20 20 20 26 26 20 28 72 63 AL)). && (rc
18e27 3d 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 =sqlite3OsSync(p
18e28 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 53 Master, SQLITE_S
18e29 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29 21 3d 53 51 YNC_NORMAL))!=SQ
18e2a 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 LITE_OK) ){.
18e2b 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
18e2c 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 Free(pMaster);.
18e2d 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 sqlite3OsDe
18e2e 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 lete(pVfs, zMast
18e2f 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 er, 0);. sq
18e30 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
18e31 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 zMaster);.
18e32 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
18e33 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 61 6c .. /* Sync al
18e34 6c 20 74 68 65 20 64 62 20 66 69 6c 65 73 20 69 l the db files i
18e35 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65 20 74 nvolved in the t
18e36 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 ransaction. The
18e37 73 61 6d 65 20 63 61 6c 6c 0a 20 20 20 20 2a 2a same call. **
18e38 20 73 65 74 73 20 74 68 65 20 6d 61 73 74 65 72 sets the master
18e39 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 journal pointer
18e3a 20 69 6e 20 65 61 63 68 20 69 6e 64 69 76 69 64 in each individ
18e3b 75 61 6c 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a ual journal. If.
18e3c 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 ** an error
18e3d 6f 63 63 75 72 73 20 68 65 72 65 2c 20 64 6f 20 occurs here, do
18e3e 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 6d not delete the m
18e3f 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
18e40 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a le.. **. *
18e41 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72 20 6f * If the error o
18e42 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 65 ccurs during the
18e43 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 0a 20 first call to.
18e44 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 ** sqlite3Btr
18e45 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
18e46 28 29 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 (), then there i
18e47 73 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20 s a chance that
18e48 74 68 65 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 the. ** maste
18e49 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 r journal file w
18e4a 69 6c 6c 20 62 65 20 6f 72 70 68 61 6e 65 64 2e ill be orphaned.
18e4b 20 42 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 64 But we cannot d
18e4c 65 6c 65 74 65 20 69 74 2c 0a 20 20 20 20 2a 2a elete it,. **
18e4d 20 69 6e 20 63 61 73 65 20 74 68 65 20 6d 61 73 in case the mas
18e4e 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
18e4f 20 6e 61 6d 65 20 77 61 73 20 77 72 69 74 74 65 name was writte
18e50 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e n into the journ
18e51 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 62 al. ** file b
18e52 65 66 6f 72 65 20 74 68 65 20 66 61 69 6c 75 72 efore the failur
18e53 65 20 6f 63 63 75 72 65 64 2e 0a 20 20 20 20 2a e occured.. *
18e54 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 /. for(i=0; r
18e55 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
18e56 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
18e57 20 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 . Btree *p
18e58 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e Bt = db->aDb[i].
18e59 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 pBt;. if( p
18e5a 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 Bt ){. rc
18e5b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 = sqlite3BtreeC
18e5c 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 ommitPhaseOne(pB
18e5d 74 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 t, zMaster);.
18e5e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 }. }. s
18e5f 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 qlite3OsCloseFre
18e60 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 e(pMaster);.
18e61 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
18e62 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
18e63 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 e3DbFree(db, zMa
18e64 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 ster);. ret
18e65 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 urn rc;. }..
18e66 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 /* Delete the
18e67 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
18e68 66 69 6c 65 2e 20 54 68 69 73 20 63 6f 6d 6d 69 file. This commi
18e69 74 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ts the transacti
18e6a 6f 6e 2e 20 41 66 74 65 72 0a 20 20 20 20 2a 2a on. After. **
18e6b 20 64 6f 69 6e 67 20 74 68 69 73 20 74 68 65 20 doing this the
18e6c 64 69 72 65 63 74 6f 72 79 20 69 73 20 73 79 6e directory is syn
18e6d 63 65 64 20 61 67 61 69 6e 20 62 65 66 6f 72 65 ced again before
18e6e 20 61 6e 79 20 69 6e 64 69 76 69 64 75 61 6c 0a any individual.
18e6f 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 ** transacti
18e70 6f 6e 20 66 69 6c 65 73 20 61 72 65 20 64 65 6c on files are del
18e71 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 eted.. */.
18e72 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 rc = sqlite3OsD
18e73 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 elete(pVfs, zMas
18e74 74 65 72 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c ter, 1);. sql
18e75 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
18e76 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 7a 4d 61 Master);. zMa
18e77 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 ster = 0;. if
18e78 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 ( rc ){. re
18e79 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a turn rc;. }..
18e7a 20 20 20 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 /* All files
18e7b 20 61 6e 64 20 64 69 72 65 63 74 6f 72 69 65 73 and directories
18e7c 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 have already be
18e7d 65 6e 20 73 79 6e 63 65 64 2c 20 73 6f 20 74 68 en synced, so th
18e7e 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 e following.
18e7f 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 ** calls to sqli
18e80 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
18e81 61 73 65 54 77 6f 28 29 20 61 72 65 20 6f 6e 6c aseTwo() are onl
18e82 79 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 y closing files
18e83 61 6e 64 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 and. ** delet
18e84 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e ing or truncatin
18e85 67 20 6a 6f 75 72 6e 61 6c 73 2e 20 49 66 20 73 g journals. If s
18e86 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 omething goes wr
18e87 6f 6e 67 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a ong while. **
18e88 20 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 this is happeni
18e89 6e 67 20 77 65 20 64 6f 6e 27 74 20 72 65 61 6c ng we don't real
18e8a 6c 79 20 63 61 72 65 2e 20 54 68 65 20 69 6e 74 ly care. The int
18e8b 65 67 72 69 74 79 20 6f 66 20 74 68 65 0a 20 20 egrity of the.
18e8c 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
18e8d 20 69 73 20 61 6c 72 65 61 64 79 20 67 75 61 72 is already guar
18e8e 61 6e 74 65 65 64 2c 20 62 75 74 20 73 6f 6d 65 anteed, but some
18e8f 20 73 74 72 61 79 20 27 63 6f 6c 64 27 20 6a 6f stray 'cold' jo
18e90 75 72 6e 61 6c 73 0a 20 20 20 20 2a 2a 20 6d 61 urnals. ** ma
18e91 79 20 62 65 20 6c 79 69 6e 67 20 61 72 6f 75 6e y be lying aroun
18e92 64 2e 20 52 65 74 75 72 6e 69 6e 67 20 61 6e 20 d. Returning an
18e93 65 72 72 6f 72 20 63 6f 64 65 20 77 6f 6e 27 74 error code won't
18e94 20 68 65 6c 70 20 6d 61 74 74 65 72 73 2e 0a 20 help matters..
18e95 20 20 20 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c */. disabl
18e96 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
18e97 72 72 6f 72 73 28 29 3b 0a 20 20 20 20 73 71 6c rrors();. sql
18e98 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
18e99 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 66 6f 72 alloc();. for
18e9a 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b (i=0; i<db->nDb;
18e9b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 i++){ . Bt
18e9c 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 ree *pBt = db->a
18e9d 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 Db[i].pBt;.
18e9e 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 if( pBt ){.
18e9f 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
18ea0 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
18ea1 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 Bt);. }.
18ea2 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e }. sqlite3En
18ea3 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
18ea4 0a 20 20 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 . enable_simu
18ea5 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
18ea6 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 );.. sqlite3V
18ea7 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 tabCommit(db);.
18ea8 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 }.#endif.. ret
18ea9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a urn rc;.}../* .*
18eaa 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
18eab 68 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 73 hecks that the s
18eac 71 6c 69 74 65 33 2e 61 63 74 69 76 65 56 64 62 qlite3.activeVdb
18ead 65 43 6e 74 20 63 6f 75 6e 74 20 76 61 72 69 61 eCnt count varia
18eae 62 6c 65 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 ble.** matches t
18eaf 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 64 62 he number of vdb
18eb0 65 27 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 e's in the list
18eb1 73 71 6c 69 74 65 33 2e 70 56 64 62 65 20 74 68 sqlite3.pVdbe th
18eb2 61 74 20 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e at are.** curren
18eb3 74 6c 79 20 61 63 74 69 76 65 2e 20 41 6e 20 61 tly active. An a
18eb4 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 ssertion fails i
18eb5 66 20 74 68 65 20 74 77 6f 20 63 6f 75 6e 74 73 f the two counts
18eb6 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a do not match..*
18eb7 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 * This is an int
18eb8 65 72 6e 61 6c 20 73 65 6c 66 2d 63 68 65 63 6b ernal self-check
18eb9 20 6f 6e 6c 79 20 2d 20 69 74 20 69 73 20 6e 6f only - it is no
18eba 74 20 61 6e 20 65 73 73 65 6e 74 69 61 6c 20 70 t an essential p
18ebb 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 73 74 65 rocessing.** ste
18ebc 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 p..**.** This is
18ebd 20 61 20 6e 6f 2d 6f 70 20 69 66 20 4e 44 45 42 a no-op if NDEB
18ebe 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a UG is defined..*
18ebf 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 /.#ifndef NDEBUG
18ec0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 .static void che
18ec1 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 28 ckActiveVdbeCnt(
18ec2 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
18ec3 56 64 62 65 20 2a 70 3b 0a 20 20 69 6e 74 20 63 Vdbe *p;. int c
18ec4 6e 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 57 nt = 0;. int nW
18ec5 72 69 74 65 20 3d 20 30 3b 0a 20 20 70 20 3d 20 rite = 0;. p =
18ec6 64 62 2d 3e 70 56 64 62 65 3b 0a 20 20 77 68 69 db->pVdbe;. whi
18ec7 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 le( p ){. if(
18ec8 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
18ec9 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 70 2d 3e MAGIC_RUN && p->
18eca 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 pc>=0 ){. c
18ecb 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 nt++;. if(
18ecc 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 p->readOnly==0 )
18ecd 20 6e 57 72 69 74 65 2b 2b 3b 0a 20 20 20 20 7d nWrite++;. }
18ece 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 . p = p->pNex
18ecf 74 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 t;. }. assert(
18ed0 20 63 6e 74 3d 3d 64 62 2d 3e 61 63 74 69 76 65 cnt==db->active
18ed1 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 61 73 73 VdbeCnt );. ass
18ed2 65 72 74 28 20 6e 57 72 69 74 65 3d 3d 64 62 2d ert( nWrite==db-
18ed3 3e 77 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b >writeVdbeCnt );
18ed4 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 .}.#else.#define
18ed5 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 checkActiveVdbe
18ed6 43 6e 74 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f Cnt(x).#endif../
18ed7 2a 0a 2a 2a 20 46 6f 72 20 65 76 65 72 79 20 42 *.** For every B
18ed8 74 72 65 65 20 74 68 61 74 20 69 6e 20 64 61 74 tree that in dat
18ed9 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
18eda 20 64 62 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 db which .** ha
18edb 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2c s been modified,
18edc 20 22 74 72 69 70 22 20 6f 72 20 69 6e 76 61 6c "trip" or inval
18edd 69 64 61 74 65 20 65 61 63 68 20 63 75 72 73 6f idate each curso
18ede 72 20 69 6e 0a 2a 2a 20 74 68 61 74 20 42 74 72 r in.** that Btr
18edf 65 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 ee might have be
18ee0 65 6e 20 6d 6f 64 69 66 69 65 64 20 73 6f 20 74 en modified so t
18ee1 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a hat the cursor.*
18ee2 2a 20 63 61 6e 20 6e 65 76 65 72 20 62 65 20 75 * can never be u
18ee3 73 65 64 20 61 67 61 69 6e 2e 20 20 54 68 69 73 sed again. This
18ee4 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 20 happens when a
18ee5 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63 rollback.*** occ
18ee6 75 72 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f urs. We have to
18ee7 20 74 72 69 70 20 61 6c 6c 20 74 68 65 20 6f 74 trip all the ot
18ee8 68 65 72 20 63 75 72 73 6f 72 73 2c 20 65 76 65 her cursors, eve
18ee9 6e 0a 2a 2a 20 63 75 72 73 6f 72 20 66 72 6f 6d n.** cursor from
18eea 20 6f 74 68 65 72 20 56 4d 73 20 69 6e 20 64 69 other VMs in di
18eeb 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 fferent database
18eec 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a connections,.**
18eed 20 73 6f 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 so that none of
18eee 20 74 68 65 6d 20 74 72 79 20 74 6f 20 75 73 65 them try to use
18eef 20 74 68 65 20 64 61 74 61 20 61 74 20 77 68 69 the data at whi
18ef0 63 68 20 74 68 65 79 0a 2a 2a 20 77 65 72 65 20 ch they.** were
18ef1 70 6f 69 6e 74 69 6e 67 20 61 6e 64 20 77 68 69 pointing and whi
18ef2 63 68 20 6e 6f 77 20 6d 61 79 20 68 61 76 65 20 ch now may have
18ef3 62 65 65 6e 20 63 68 61 6e 67 65 64 20 64 75 65 been changed due
18ef4 0a 2a 2a 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 .** to the rollb
18ef5 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d ack..**.** Remem
18ef6 62 65 72 20 74 68 61 74 20 61 20 72 6f 6c 6c 62 ber that a rollb
18ef7 61 63 6b 20 63 61 6e 20 64 65 6c 65 74 65 20 74 ack can delete t
18ef8 61 62 6c 65 73 20 63 6f 6d 70 6c 65 74 65 20 61 ables complete a
18ef9 6e 64 0a 2a 2a 20 72 65 6f 72 64 65 72 20 72 6f nd.** reorder ro
18efa 6f 74 70 61 67 65 73 2e 20 20 53 6f 20 69 74 20 otpages. So it
18efb 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e is not sufficien
18efc 74 20 6a 75 73 74 20 74 6f 20 73 61 76 65 0a 2a t just to save.*
18efd 2a 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 * the state of t
18efe 68 65 20 63 75 72 73 6f 72 2e 20 20 57 65 20 68 he cursor. We h
18eff 61 76 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 ave to invalidat
18f00 65 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 e the cursor.**
18f01 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 65 so that it is ne
18f02 76 65 72 20 75 73 65 64 20 61 67 61 69 6e 2e 0a ver used again..
18f03 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 */.static void i
18f04 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 nvalidateCursors
18f05 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 OnModifiedBtrees
18f06 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
18f07 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
18f08 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
18f09 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 +){. Btree *p
18f0a 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
18f0b 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 t;. if( p &&
18f0c 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e sqlite3BtreeIsIn
18f0d 54 72 61 6e 73 28 70 29 20 29 7b 0a 20 20 20 20 Trans(p) ){.
18f0e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 sqlite3BtreeTr
18f0f 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 ipAllCursors(p,
18f10 53 51 4c 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20 SQLITE_ABORT);.
18f11 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
18f12 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
18f13 73 20 63 61 6c 6c 65 64 20 74 68 65 20 77 68 65 s called the whe
18f14 6e 20 61 20 56 44 42 45 20 74 72 69 65 73 20 74 n a VDBE tries t
18f15 6f 20 68 61 6c 74 2e 20 20 49 66 20 74 68 65 20 o halt. If the
18f16 56 44 42 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65 VDBE.** has made
18f17 20 63 68 61 6e 67 65 73 20 61 6e 64 20 69 73 20 changes and is
18f18 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f in autocommit mo
18f19 64 65 2c 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20 de, then commit
18f1a 74 68 6f 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73 those.** changes
18f1b 2e 20 20 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b . If a rollback
18f1c 20 69 73 20 6e 65 65 64 65 64 2c 20 74 68 65 6e is needed, then
18f1d 20 64 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b do the rollback
18f1e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
18f1f 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 tine is the only
18f20 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 way to move the
18f21 20 73 74 61 74 65 20 6f 66 20 61 20 56 4d 20 66 state of a VM f
18f22 72 6f 6d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 rom.** SQLITE_MA
18f23 47 49 43 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54 GIC_RUN to SQLIT
18f24 45 5f 4d 41 47 49 43 5f 48 41 4c 54 2e 20 20 49 E_MAGIC_HALT. I
18f25 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f t is harmless to
18f26 0a 2a 2a 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e .** call this on
18f27 20 61 20 56 4d 20 74 68 61 74 20 69 73 20 69 6e a VM that is in
18f28 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 the SQLITE_MAGI
18f29 43 5f 48 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a C_HALT state..**
18f2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 .** Return an er
18f2b 72 6f 72 20 63 6f 64 65 2e 20 20 49 66 20 74 68 ror code. If th
18f2c 65 20 63 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e e commit could n
18f2d 6f 74 20 63 6f 6d 70 6c 65 74 65 20 62 65 63 61 ot complete beca
18f2e 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 use of.** lock c
18f2f 6f 6e 74 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72 ontention, retur
18f30 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 n SQLITE_BUSY.
18f31 49 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 If SQLITE_BUSY i
18f32 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a s returned, it.*
18f33 2a 20 6d 65 61 6e 73 20 74 68 65 20 63 6c 6f 73 * means the clos
18f34 65 20 64 69 64 20 6e 6f 74 20 68 61 70 70 65 6e e did not happen
18f35 20 61 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65 and needs to be
18f36 20 72 65 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 repeated..*/.SQ
18f37 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
18f38 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 sqlite3VdbeHalt
18f39 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c (Vdbe *p){. sql
18f3a 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 ite3 *db = p->db
18f3b 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 ;. int i;. int
18f3c 20 28 2a 78 46 75 6e 63 29 28 42 74 72 65 65 20 (*xFunc)(Btree
18f3d 2a 70 42 74 29 20 3d 20 30 3b 20 20 2f 2a 20 46 *pBt) = 0; /* F
18f3e 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 unction to call
18f3f 6f 6e 20 65 61 63 68 20 62 74 72 65 65 20 62 61 on each btree ba
18f40 63 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 ckend */. int i
18f41 73 53 70 65 63 69 61 6c 45 72 72 6f 72 3b 20 20 sSpecialError;
18f42 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 /* Set
18f43 20 74 6f 20 74 72 75 65 20 69 66 20 53 51 4c 49 to true if SQLI
18f44 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 49 4f 45 52 TE_NOMEM or IOER
18f45 52 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 R */.. /* This
18f46 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e function contain
18f47 73 20 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74 s the logic that
18f48 20 64 65 74 65 72 6d 69 6e 65 73 20 69 66 20 61 determines if a
18f49 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20 statement or.
18f4a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 ** transaction w
18f4b 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 ill be committed
18f4c 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 or rolled back
18f4d 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 as a result of t
18f4e 68 65 0a 20 20 2a 2a 20 65 78 65 63 75 74 69 6f he. ** executio
18f4f 6e 20 6f 66 20 74 68 69 73 20 76 69 72 74 75 61 n of this virtua
18f50 6c 20 6d 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a l machine. . **
18f51 0a 20 20 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 . ** If any of
18f52 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 the following er
18f53 72 6f 72 73 20 6f 63 63 75 72 3a 0a 20 20 2a 2a rors occur:. **
18f54 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 . ** SQLITE
18f55 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a 20 20 20 20 20 _NOMEM. **
18f56 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 2a SQLITE_IOERR. *
18f57 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 46 55 4c * SQLITE_FUL
18f58 4c 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 L. ** SQLIT
18f59 45 5f 49 4e 54 45 52 52 55 50 54 0a 20 20 2a 2a E_INTERRUPT. **
18f5a 0a 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 69 . ** Then the i
18f5b 6e 74 65 72 6e 61 6c 20 63 61 63 68 65 20 6d 69 nternal cache mi
18f5c 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 6c 65 ght have been le
18f5d 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 ft in an inconsi
18f5e 73 74 65 6e 74 0a 20 20 2a 2a 20 73 74 61 74 65 stent. ** state
18f5f 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 20 72 6f . We need to ro
18f60 6c 6c 62 61 63 6b 20 74 68 65 20 73 74 61 74 65 llback the state
18f61 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
18f62 2c 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 , if there is.
18f63 2a 2a 20 6f 6e 65 2c 20 6f 72 20 74 68 65 20 63 ** one, or the c
18f64 6f 6d 70 6c 65 74 65 20 74 72 61 6e 73 61 63 74 omplete transact
18f65 69 6f 6e 20 69 66 20 74 68 65 72 65 20 69 73 20 ion if there is
18f66 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 no statement tra
18f67 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a nsaction.. */..
18f68 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c if( p->db->mal
18f69 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
18f6a 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f p->rc = SQLITE_
18f6b 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 63 6c 6f NOMEM;. }. clo
18f6c 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65 seAllCursorsExce
18f6d 70 74 41 63 74 69 76 65 56 74 61 62 73 28 70 29 ptActiveVtabs(p)
18f6e 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 ;. if( p->magic
18f6f 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e !=VDBE_MAGIC_RUN
18f70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
18f71 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
18f72 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 checkActiveVdbeC
18f73 6e 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f nt(db);.. /* No
18f74 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 commit or rollb
18f75 61 63 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68 ack needed if th
18f76 65 20 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20 e program never
18f77 73 74 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28 started */. if(
18f78 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 p->pc>=0 ){.
18f79 20 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50 int mrc; /* P
18f7a 72 69 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64 rimary error cod
18f7b 65 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a e from p->rc */.
18f7c 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 61 6c 6c . /* Lock all
18f7d 20 62 74 72 65 65 73 20 75 73 65 64 20 62 79 20 btrees used by
18f7e 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f the statement */
18f7f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
18f80 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 eMutexArrayEnter
18f81 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 0a 20 (&p->aMutex);..
18f82 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 /* Check for
18f83 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 one of the speci
18f84 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 al errors */.
18f85 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30 mrc = p->rc & 0
18f86 78 66 66 3b 0a 20 20 20 20 69 73 53 70 65 63 69 xff;. isSpeci
18f87 61 6c 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 alError = mrc==S
18f88 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d QLITE_NOMEM || m
18f89 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc==SQLITE_IOERR
18f8a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
18f8b 20 20 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 || mrc==SQ
18f8c 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c LITE_INTERRUPT |
18f8d 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 | mrc==SQLITE_FU
18f8e 4c 4c 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70 LL;. if( isSp
18f8f 65 63 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 ecialError ){.
18f90 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75 /* If the qu
18f91 65 72 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c ery was read-onl
18f92 79 2c 20 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f y, we need do no
18f93 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c rollback at all
18f94 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 20 . Otherwise,.
18f95 20 20 20 2a 2a 20 70 72 6f 63 65 65 64 20 77 69 ** proceed wi
18f96 74 68 20 74 68 65 20 73 70 65 63 69 61 6c 20 68 th the special h
18f97 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a andling.. *
18f98 2f 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e /. if( !p->
18f99 72 65 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 readOnly || mrc!
18f9a 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 =SQLITE_INTERRUP
18f9b 54 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 T ){. if(
18f9c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 49 p->rc==SQLITE_I
18f9d 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 26 26 20 OERR_BLOCKED &&
18f9e 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e p->usesStmtJourn
18f9f 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 al ){.
18fa0 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 42 xFunc = sqlite3B
18fa1 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 treeRollbackStmt
18fa2 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 ;. p->r
18fa3 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
18fa4 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 . }else i
18fa5 66 28 20 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f f( (mrc==SQLITE_
18fa6 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 NOMEM || mrc==SQ
18fa7 4c 49 54 45 5f 46 55 4c 4c 29 0a 20 20 20 20 20 LITE_FULL).
18fa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 &&
18fa9 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 p->usesStmtJour
18faa 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 nal ){.
18fab 20 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 xFunc = sqlite3
18fac 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d BtreeRollbackStm
18fad 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 t;. }else
18fae 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 {. /* W
18faf 65 20 61 72 65 20 66 6f 72 63 65 64 20 74 6f 20 e are forced to
18fb0 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 20 61 63 roll back the ac
18fb1 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e tive transaction
18fb2 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 0a 20 . Before doing.
18fb3 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 ** so,
18fb4 61 62 6f 72 74 20 61 6e 79 20 6f 74 68 65 72 20 abort any other
18fb5 73 74 61 74 65 6d 65 6e 74 73 20 74 68 69 73 20 statements this
18fb6 68 61 6e 64 6c 65 20 63 75 72 72 65 6e 74 6c 79 handle currently
18fb7 20 68 61 73 20 61 63 74 69 76 65 2e 0a 20 20 20 has active..
18fb8 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
18fb9 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43 75 invalidateCu
18fba 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 rsorsOnModifiedB
18fbb 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20 20 trees(db);.
18fbc 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c sqlite3Roll
18fbd 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 backAll(db);.
18fbe 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c sqlite3Cl
18fbf 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 oseSavepoints(db
18fc0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d );. db-
18fc1 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b >autoCommit = 1;
18fc2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
18fc3 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 }. }. .
18fc4 2f 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d 63 /* If the auto-c
18fc5 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65 ommit flag is se
18fc6 74 20 61 6e 64 20 74 68 69 73 20 69 73 20 74 68 t and this is th
18fc7 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 76 64 e only active vd
18fc8 62 65 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 be, then. **
18fc9 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 63 we do either a c
18fca 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63 ommit or rollbac
18fcb 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 k of the current
18fcc 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 20 transaction. .
18fcd 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 **. ** Not
18fce 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 6c e: This block al
18fcf 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 6f so runs if one o
18fd0 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 f the special er
18fd1 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 20 rors handled .
18fd2 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 6f ** above has o
18fd3 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a 2f ccurred. . */
18fd4 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 . if( !sqlite
18fd5 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 3VtabInSync(db)
18fd6 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 74 . && db->aut
18fd7 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 26 oCommit . &&
18fd8 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e db->writeVdbeCn
18fd9 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d t==(p->readOnly=
18fda 3d 30 29 20 0a 20 20 20 20 20 26 26 20 28 64 62 =0) . && (db
18fdb 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
18fdc 5f 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30 0a _CommitBusy)==0.
18fdd 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ){. if(
18fde 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f p->rc==SQLITE_O
18fdf 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63 K || (p->errorAc
18fe0 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 tion==OE_Fail &&
18fe1 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 !isSpecialError
18fe2 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ) ){. /*
18fe3 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 The auto-commit
18fe4 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 61 6e flag is true, an
18fe5 64 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 d the vdbe progr
18fe6 61 6d 20 77 61 73 20 0a 20 20 20 20 20 20 20 20 am was .
18fe7 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6f 72 ** successful or
18fe8 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49 4c hit an 'OR FAIL
18fe9 27 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54 68 ' constraint. Th
18fea 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d 69 is means a commi
18feb 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 t . ** is
18fec 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 20 required..
18fed 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e */. in
18fee 74 20 72 63 20 3d 20 76 64 62 65 43 6f 6d 6d 69 t rc = vdbeCommi
18fef 74 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 t(db, p);.
18ff0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
18ff1 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 _BUSY ){.
18ff2 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d sqlite3BtreeM
18ff3 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 utexArrayLeave(&
18ff4 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 p->aMutex);.
18ff5 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
18ff6 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 ITE_BUSY;.
18ff7 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d }else if( rc!=
18ff8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
18ff9 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 p->rc = r
18ffa 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c c;. sql
18ffb 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 ite3RollbackAll(
18ffc 64 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c db);. }el
18ffd 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 se{. sq
18ffe 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 lite3CommitInter
18fff 6e 61 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a nalChanges(db);.
19000 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
19001 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 }else{. s
19002 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c qlite3RollbackAl
19003 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 l(db);. }.
19004 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 78 46 }else if( !xF
19005 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 69 66 28 unc ){. if(
19006 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f p->rc==SQLITE_O
19007 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 K || p->errorAct
19008 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a ion==OE_Fail ){.
19009 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6f if( p->o
1900a 70 65 6e 65 64 53 74 61 74 65 6d 65 6e 74 20 29 penedStatement )
1900b 7b 0a 20 20 20 20 20 20 20 20 20 20 78 46 75 6e {. xFun
1900c 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1900d 43 6f 6d 6d 69 74 53 74 6d 74 3b 0a 20 20 20 20 CommitStmt;.
1900e 20 20 20 20 7d 20 0a 20 20 20 20 20 20 7d 65 6c } . }el
1900f 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 41 se if( p->errorA
19010 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 20 ction==OE_Abort
19011 29 7b 0a 20 20 20 20 20 20 20 20 78 46 75 6e 63 ){. xFunc
19012 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 = sqlite3BtreeR
19013 6f 6c 6c 62 61 63 6b 53 74 6d 74 3b 0a 20 20 20 ollbackStmt;.
19014 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
19015 20 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72 73 invalidateCurs
19016 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 orsOnModifiedBtr
19017 65 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 ees(db);.
19018 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b sqlite3Rollback
19019 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 All(db);.
1901a 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 sqlite3CloseSav
1901b 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 epoints(db);.
1901c 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d db->autoCom
1901d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d mit = 1;. }
1901e 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
1901f 20 49 66 20 78 46 75 6e 63 20 69 73 20 6e 6f 74 If xFunc is not
19020 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 NULL, then it i
19021 73 20 6f 6e 65 20 6f 66 20 73 71 6c 69 74 65 33 s one of sqlite3
19022 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d BtreeRollbackStm
19023 74 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 t or. ** sqli
19024 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53 74 te3BtreeCommitSt
19025 6d 74 2e 20 43 61 6c 6c 20 69 74 20 6f 6e 63 65 mt. Call it once
19026 20 6f 6e 20 65 61 63 68 20 62 61 63 6b 65 6e 64 on each backend
19027 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
19028 63 75 72 73 0a 20 20 20 20 2a 2a 20 61 6e 64 20 curs. ** and
19029 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 the return code
1902a 69 73 20 73 74 69 6c 6c 20 53 51 4c 49 54 45 5f is still SQLITE_
1902b 4f 4b 2c 20 73 65 74 20 74 68 65 20 72 65 74 75 OK, set the retu
1902c 72 6e 20 63 6f 64 65 20 74 6f 20 74 68 65 20 6e rn code to the n
1902d 65 77 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 ew. ** error
1902e 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 value.. */.
1902f 20 20 61 73 73 65 72 74 28 21 78 46 75 6e 63 20 assert(!xFunc
19030 7c 7c 0a 20 20 20 20 20 20 78 46 75 6e 63 3d 3d ||. xFunc==
19031 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
19032 69 74 53 74 6d 74 20 7c 7c 0a 20 20 20 20 20 20 itStmt ||.
19033 78 46 75 6e 63 3d 3d 73 71 6c 69 74 65 33 42 74 xFunc==sqlite3Bt
19034 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 0a reeRollbackStmt.
19035 20 20 20 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 );. for(i
19036 3d 30 3b 20 78 46 75 6e 63 20 26 26 20 69 3c 64 =0; xFunc && i<d
19037 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 b->nDb; i++){ .
19038 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 int rc;.
19039 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 Btree *pBt =
1903a 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
1903b 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b if( pBt ){
1903c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 78 46 . rc = xF
1903d 75 6e 63 28 70 42 74 29 3b 0a 20 20 20 20 20 20 unc(pBt);.
1903e 20 20 69 66 28 20 72 63 20 26 26 20 28 70 2d 3e if( rc && (p->
1903f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
19040 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 p->rc==SQLITE_C
19041 4f 4e 53 54 52 41 49 4e 54 29 20 29 7b 0a 20 20 ONSTRAINT) ){.
19042 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 p->rc =
19043 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 rc;. sq
19044 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
19045 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 p->zErrMsg);.
19046 20 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 p->zErrMs
19047 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d g = 0;. }
19048 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
19049 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 . /* If this
1904a 20 77 61 73 20 61 6e 20 49 4e 53 45 52 54 2c 20 was an INSERT,
1904b 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 UPDATE or DELETE
1904c 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65 and the stateme
1904d 6e 74 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 nt was committed
1904e 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68 , . ** set th
1904f 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 e change counter
19050 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 . . */. if
19051 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e ( p->changeCntOn
19052 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a && p->pc>=0 ){.
19053 20 20 20 20 20 20 69 66 28 20 21 78 46 75 6e 63 if( !xFunc
19054 20 7c 7c 20 78 46 75 6e 63 3d 3d 73 71 6c 69 74 || xFunc==sqlit
19055 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53 74 6d e3BtreeCommitStm
19056 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c t ){. sql
19057 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 ite3VdbeSetChang
19058 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 es(db, p->nChang
19059 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b e);. }else{
1905a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1905b 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 VdbeSetChanges(d
1905c 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 b, 0);. }.
1905d 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 p->nChange
1905e 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 = 0;. }. .
1905f 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 /* Rollback or
19060 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 commit any sche
19061 6d 61 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 ma changes that
19062 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 occurred. */.
19063 20 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 if( p->rc!=SQLI
19064 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 TE_OK && db->fla
19065 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e gs&SQLITE_Intern
19066 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 Changes ){.
19067 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 sqlite3ResetInt
19068 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 ernalSchema(db,
19069 30 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 0);. db->fl
1906a 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 ags = (db->flags
1906b 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e | SQLITE_Intern
1906c 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a Changes);. }.
1906d 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 . /* Release
1906e 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 the locks */.
1906f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
19070 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d exArrayLeave(&p-
19071 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 >aMutex);. }..
19072 20 2f 2a 20 57 65 20 68 61 76 65 20 73 75 63 63 /* We have succ
19073 65 73 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 essfully halted
19074 61 6e 64 20 63 6c 6f 73 65 64 20 74 68 65 20 56 and closed the V
19075 4d 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73 20 M. Record this
19076 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 fact. */. if( p
19077 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 ->pc>=0 ){. d
19078 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 b->activeVdbeCnt
19079 2d 2d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e --;. if( !p->
1907a 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 readOnly ){.
1907b 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 db->writeVdbeC
1907c 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 nt--;. }.
1907d 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 assert( db->acti
1907e 76 65 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 veVdbeCnt>=db->w
1907f 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 riteVdbeCnt );.
19080 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 }. p->magic =
19081 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b VDBE_MAGIC_HALT;
19082 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 . checkActiveVd
19083 62 65 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 beCnt(db);. if(
19084 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 p->db->mallocFa
19085 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 iled ){. p->r
19086 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
19087 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
19088 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
19089 2a 0a 2a 2a 20 45 61 63 68 20 56 44 42 45 20 68 *.** Each VDBE h
1908a 6f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 20 olds the result
1908b 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 of the most rece
1908c 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 nt sqlite3_step(
1908d 29 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e ) call.** in p->
1908e 72 63 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e rc. This routin
1908f 65 20 73 65 74 73 20 74 68 61 74 20 72 65 73 75 e sets that resu
19090 6c 74 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 lt back to SQLIT
19091 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f E_OK..*/.SQLITE_
19092 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19093 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65 ite3VdbeResetSte
19094 70 52 65 73 75 6c 74 28 56 64 62 65 20 2a 70 29 pResult(Vdbe *p)
19095 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 {. p->rc = SQLI
19096 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
19097 43 6c 65 61 6e 20 75 70 20 61 20 56 44 42 45 20 Clean up a VDBE
19098 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e 20 after execution
19099 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 but do not delet
1909a 65 20 74 68 65 20 56 44 42 45 20 6a 75 73 74 20 e the VDBE just
1909b 79 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e yet..** Write an
1909c 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 y error messages
1909d 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e into *pzErrMsg.
1909e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 Return the res
1909f 75 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ult code..**.**
190a0 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 After this routi
190a1 6e 65 20 69 73 20 72 75 6e 2c 20 74 68 65 20 56 ne is run, the V
190a2 44 42 45 20 73 68 6f 75 6c 64 20 62 65 20 72 65 DBE should be re
190a3 61 64 79 20 74 6f 20 62 65 20 65 78 65 63 75 74 ady to be execut
190a4 65 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a ed.** again..**.
190a5 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 ** To look at it
190a6 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68 another way, th
190a7 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74 is routine reset
190a8 73 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 s the state of t
190a9 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 he.** virtual ma
190aa 63 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f chine from VDBE_
190ab 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 MAGIC_RUN or VDB
190ac 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63 E_MAGIC_HALT bac
190ad 6b 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 k to.** VDBE_MAG
190ae 49 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 IC_INIT..*/.SQLI
190af 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
190b0 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 qlite3VdbeReset(
190b1 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 Vdbe *p){. sqli
190b2 74 65 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 te3 *db;. db =
190b3 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 p->db;.. /* If
190b4 74 68 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 the VM did not r
190b5 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e un to completion
190b6 20 6f 72 20 69 66 20 69 74 20 65 6e 63 6f 75 6e or if it encoun
190b7 74 65 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 tered an. ** er
190b8 72 6f 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 ror, then it mig
190b9 68 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e ht not have been
190ba 20 68 61 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 halted properly
190bb 2e 20 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 . So halt. **
190bc 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 it now.. */. (
190bd 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
190be 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 tyOn(db);. sqli
190bf 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a te3VdbeHalt(p);.
190c0 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
190c1 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 afetyOff(db);..
190c2 20 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45 20 /* If the VDBE
190c3 68 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e 20 has be run even
190c4 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 partially, then
190c5 74 72 61 6e 73 66 65 72 20 74 68 65 20 65 72 72 transfer the err
190c6 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 or code. ** and
190c7 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 error message f
190c8 72 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e 74 rom the VDBE int
190c9 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 o the main datab
190ca 61 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 ase structure.
190cb 42 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 But. ** if the
190cc 56 44 42 45 20 68 61 73 20 6a 75 73 74 20 62 65 VDBE has just be
190cd 65 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62 75 en set to run bu
190ce 74 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61 6c t has not actual
190cf 6c 79 20 65 78 65 63 75 74 65 64 20 61 6e 79 0a ly executed any.
190d0 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e ** instruction
190d1 73 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68 65 s yet, leave the
190d2 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 main database e
190d3 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e rror information
190d4 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f unchanged.. */
190d5 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 . if( p->pc>=0
190d6 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 ){. if( p->zE
190d7 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 rrMsg ){. s
190d8 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
190d9 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 nMalloc();.
190da 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 sqlite3ValueSet
190db 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c Str(db->pErr,-1,
190dc 70 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 p->zErrMsg,SQLIT
190dd 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 E_UTF8,SQLITE_TR
190de 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 ANSIENT);.
190df 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
190e0 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 Malloc();.
190e1 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d db->errCode = p-
190e2 3e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 >rc;. sqlit
190e3 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
190e4 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 zErrMsg);.
190e5 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a p->zErrMsg = 0;.
190e6 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d }else if( p-
190e7 3e 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c >rc ){. sql
190e8 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d ite3Error(db, p-
190e9 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c >rc, 0);. }el
190ea 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se{. sqlite
190eb 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 3Error(db, SQLIT
190ec 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a E_OK, 0);. }.
190ed 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 }else if( p->r
190ee 63 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 c && p->expired
190ef 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 ){. /* The ex
190f0 70 69 72 65 64 20 66 6c 61 67 20 77 61 73 20 73 pired flag was s
190f1 65 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62 et on the VDBE b
190f2 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 efore the first
190f3 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 call. ** to s
190f4 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46 qlite3_step(). F
190f5 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 or consistency (
190f6 73 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74 since sqlite3_st
190f7 65 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 ep() was. **
190f8 63 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65 called), set the
190f9 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 database error
190fa 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 73 20 in this case as
190fb 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 well.. */.
190fc 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 sqlite3Error(db
190fd 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 , p->rc, 0);.
190fe 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 sqlite3ValueSet
190ff 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 Str(db->pErr, -1
19100 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 , p->zErrMsg, SQ
19101 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
19102 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 E_TRANSIENT);.
19103 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
19104 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b db, p->zErrMsg);
19105 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 . p->zErrMsg
19106 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 = 0;. }.. /* R
19107 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 eclaim all memor
19108 79 20 75 73 65 64 20 62 79 20 74 68 65 20 56 44 y used by the VD
19109 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 BE. */. Cleanu
1910a 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 p(p);.. /* Save
1910b 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 profiling infor
1910c 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 mation from this
1910d 20 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a VDBE run.. */.
1910e 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
1910f 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 ILE. {. FILE
19110 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 *out = fopen("v
19111 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 dbe_profile.out"
19112 2c 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28 20 , "a");. if(
19113 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 out ){. int
19114 20 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 i;. fprint
19115 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b f(out, "---- ");
19116 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 . for(i=0;
19117 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a i<p->nOp; i++){.
19118 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 fprintf(
19119 6f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e out, "%02x", p->
1911a 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a aOp[i].opcode);.
1911b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 70 }. fp
1911c 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 rintf(out, "\n")
1911d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b ;. for(i=0;
1911e 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b i<p->nOp; i++){
1911f 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 . fprintf
19120 28 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c 6c (out, "%6d %10ll
19121 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 d %8lld ",.
19122 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e p->aOp[i].
19123 63 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 cnt,.
19124 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 p->aOp[i].cycles
19125 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e ,. p->
19126 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 aOp[i].cnt>0 ? p
19127 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f ->aOp[i].cycles/
19128 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 p->aOp[i].cnt :
19129 30 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 0. );.
1912a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1912b 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 PrintOp(out, i,
1912c 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 &p->aOp[i]);.
1912d 20 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 }. fclos
1912e 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 e(out);. }.
1912f 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 }.#endif. p->ma
19130 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 gic = VDBE_MAGIC
19131 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 _INIT;. return
19132 70 2d 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 4d p->rc & db->errM
19133 61 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 ask;.}. ./*.** C
19134 6c 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 lean up and dele
19135 74 65 20 61 20 56 44 42 45 20 61 66 74 65 72 20 te a VDBE after
19136 65 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 execution. Retu
19137 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 rn an integer wh
19138 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 ich is.** the re
19139 73 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 74 sult code. Writ
1913a 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 e any error mess
1913b 61 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 age text into *p
1913c 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 zErrMsg..*/.SQLI
1913d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1913e 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 qlite3VdbeFinali
1913f 7a 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 ze(Vdbe *p){. i
19140 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
19141 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 K;. if( p->magi
19142 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c==VDBE_MAGIC_RU
19143 4e 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 N || p->magic==V
19144 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 DBE_MAGIC_HALT )
19145 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
19146 65 33 56 64 62 65 52 65 73 65 74 28 70 29 3b 0a e3VdbeReset(p);.
19147 20 20 20 20 61 73 73 65 72 74 28 20 28 72 63 20 assert( (rc
19148 26 20 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b & p->db->errMask
19149 29 3d 3d 72 63 20 29 3b 0a 20 20 7d 65 6c 73 65 )==rc );. }else
1914a 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 if( p->magic!=V
1914b 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 DBE_MAGIC_INIT )
1914c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1914d 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a ITE_MISUSE;. }.
1914e 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c sqlite3VdbeDel
1914f 65 74 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ete(p);. return
19150 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 rc;.}../*.** Ca
19151 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f ll the destructo
19152 72 20 66 6f 72 20 65 61 63 68 20 61 75 78 64 61 r for each auxda
19153 74 61 20 65 6e 74 72 79 20 69 6e 20 70 56 64 62 ta entry in pVdb
19154 65 46 75 6e 63 20 66 6f 72 20 77 68 69 63 68 0a eFunc for which.
19155 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e ** the correspon
19156 64 69 6e 67 20 62 69 74 20 69 6e 20 6d 61 73 6b ding bit in mask
19157 20 69 73 20 63 6c 65 61 72 2e 20 20 41 75 78 64 is clear. Auxd
19158 61 74 61 20 65 6e 74 72 69 65 73 20 62 65 79 6f ata entries beyo
19159 6e 64 20 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77 nd 31.** are alw
1915a 61 79 73 20 64 65 73 74 72 6f 79 65 64 2e 20 20 ays destroyed.
1915b 54 6f 20 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 To destroy all a
1915c 75 78 64 61 74 61 20 65 6e 74 72 69 65 73 2c 20 uxdata entries,
1915d 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 call this.** rou
1915e 74 69 6e 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d tine with mask==
1915f 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 0..*/.SQLITE_PRI
19160 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
19161 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 3VdbeDeleteAuxDa
19162 74 61 28 56 64 62 65 46 75 6e 63 20 2a 70 56 64 ta(VdbeFunc *pVd
19163 62 65 46 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b beFunc, int mask
19164 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f ){. int i;. fo
19165 72 28 69 3d 30 3b 20 69 3c 70 56 64 62 65 46 75 r(i=0; i<pVdbeFu
19166 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a nc->nAux; i++){.
19167 20 20 20 20 73 74 72 75 63 74 20 41 75 78 44 61 struct AuxDa
19168 74 61 20 2a 70 41 75 78 20 3d 20 26 70 56 64 62 ta *pAux = &pVdb
19169 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b eFunc->apAux[i];
1916a 0a 20 20 20 20 69 66 28 20 28 69 3e 33 31 20 7c . if( (i>31 |
1916b 7c 20 21 28 6d 61 73 6b 26 28 31 3c 3c 69 29 29 | !(mask&(1<<i))
1916c 29 20 26 26 20 70 41 75 78 2d 3e 70 41 75 78 20 ) && pAux->pAux
1916d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 41 75 ){. if( pAu
1916e 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 x->xDelete ){.
1916f 20 20 20 20 20 20 70 41 75 78 2d 3e 78 44 65 6c pAux->xDel
19170 65 74 65 28 70 41 75 78 2d 3e 70 41 75 78 29 3b ete(pAux->pAux);
19171 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
19172 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b 0a 20 Aux->pAux = 0;.
19173 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
19174 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 * Delete an enti
19175 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 re VDBE..*/.SQLI
19176 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19177 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
19178 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e e(Vdbe *p){. in
19179 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a t i;. sqlite3 *
1917a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 db;.. if( p==0
1917b 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d ) return;. db =
1917c 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d p->db;. if( p-
1917d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70 2d >pPrev ){. p-
1917e 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
1917f 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 p->pNext;. }els
19180 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 e{. assert( d
19181 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20 b->pVdbe==p );.
19182 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 db->pVdbe = p
19183 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 ->pNext;. }. i
19184 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 f( p->pNext ){.
19185 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 p->pNext->pPr
19186 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 ev = p->pPrev;.
19187 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 }. if( p->aOp
19188 29 7b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 20 3d ){. Op *pOp =
19189 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 66 6f 72 p->aOp;. for
1918a 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 (i=0; i<p->nOp;
1918b 69 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 i++, pOp++){.
1918c 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f freeP4(db, pO
1918d 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e p->p4type, pOp->
1918e 70 34 2e 70 29 3b 0a 23 69 66 64 65 66 20 53 51 p4.p);.#ifdef SQ
1918f 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 LITE_DEBUG.
19190 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
19191 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 b, pOp->zComment
19192 29 3b 0a 23 65 6e 64 69 66 20 20 20 20 20 0a 20 );.#endif .
19193 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
19194 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 4f DbFree(db, p->aO
19195 70 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 p);. }. releas
19196 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 56 61 eMemArray(p->aVa
19197 72 2c 20 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 73 r, p->nVar);. s
19198 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
19199 20 70 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 69 p->aLabel);. i
1919a 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 f( p->aMem ){.
1919b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1919c 64 62 2c 20 26 70 2d 3e 61 4d 65 6d 5b 31 5d 29 db, &p->aMem[1])
1919d 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d ;. }. releaseM
1919e 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e emArray(p->aColN
1919f 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 ame, p->nResColu
191a0 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 mn*COLNAME_N);.
191a1 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
191a2 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b b, p->aColName);
191a3 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
191a4 28 64 62 2c 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 (db, p->zSql);.
191a5 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 p->magic = VDBE
191a6 5f 4d 41 47 49 43 5f 44 45 41 44 3b 0a 20 20 73 _MAGIC_DEAD;. s
191a7 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
191a8 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 p);.}../*.** If
191a9 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 61 74 a MoveTo operat
191aa 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 20 6f ion is pending o
191ab 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 73 n the given curs
191ac 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 61 74 or, then do that
191ad 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e 20 .** MoveTo now.
191ae 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
191af 20 63 6f 64 65 2e 20 20 49 66 20 6e 6f 20 4d 6f code. If no Mo
191b0 76 65 54 6f 20 69 73 20 70 65 6e 64 69 6e 67 2c veTo is pending,
191b1 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
191b2 20 64 6f 65 73 20 6e 6f 74 68 69 6e 67 20 61 6e does nothing an
191b3 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 d returns SQLITE
191b4 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 _OK..*/.SQLITE_P
191b5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
191b6 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 e3VdbeCursorMove
191b7 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20 2a 70 to(VdbeCursor *p
191b8 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65 66 65 ){. if( p->defe
191b9 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 20 rredMoveto ){.
191ba 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b 0a 23 int res, rc;.#
191bb 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
191bc 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 T. extern int
191bd 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f sqlite3_search_
191be 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 20 count;.#endif.
191bf 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73 54 assert( p->isT
191c0 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63 20 3d able );. rc =
191c1 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
191c2 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d 3e 70 etoUnpacked(p->p
191c3 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e 6d 6f Cursor, 0, p->mo
191c4 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c 20 26 vetoTarget, 0, &
191c5 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 res);. if( rc
191c6 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
191c7 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d p->lastRowid =
191c8 20 6b 65 79 54 6f 49 6e 74 28 70 2d 3e 6d 6f 76 keyToInt(p->mov
191c9 65 74 6f 54 61 72 67 65 74 29 3b 0a 20 20 20 20 etoTarget);.
191ca 70 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 p->rowidIsValid
191cb 3d 20 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 = res==0 ?1:0;.
191cc 20 20 20 69 66 28 20 72 65 73 3c 30 20 29 7b 0a if( res<0 ){.
191cd 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
191ce 65 33 42 74 72 65 65 4e 65 78 74 28 70 2d 3e 70 e3BtreeNext(p->p
191cf 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 Cursor, &res);.
191d0 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 if( rc ) re
191d1 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 23 turn rc;. }.#
191d2 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
191d3 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 T. sqlite3_se
191d4 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 arch_count++;.#e
191d5 6e 64 69 66 0a 20 20 20 20 70 2d 3e 64 65 66 65 ndif. p->defe
191d6 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a rredMoveto = 0;.
191d7 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74 p->cacheStat
191d8 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
191d9 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d ;. }else if( p-
191da 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 >pCursor ){.
191db 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20 20 int hasMoved;.
191dc 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 int rc = sqlit
191dd 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 e3BtreeCursorHas
191de 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 Moved(p->pCursor
191df 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20 20 , &hasMoved);.
191e0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
191e1 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68 61 n rc;. if( ha
191e2 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20 20 sMoved ){.
191e3 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d p->cacheStatus =
191e4 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
191e5 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d p->nullRow =
191e6 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
191e7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
191e8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 ;.}../*.** The f
191e9 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f ollowing functio
191ea 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 ns:.**.** sqlite
191eb 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 3VdbeSerialType(
191ec 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 ).** sqlite3Vdbe
191ed 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a SerialTypeLen().
191ee 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 ** sqlite3VdbeSe
191ef 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c rialLen().** sql
191f0 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 ite3VdbeSerialPu
191f1 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 t().** sqlite3Vd
191f2 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a 2a beSerialGet().**
191f3 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20 .** encapsulate
191f4 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65 the code that se
191f5 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20 rializes values
191f6 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53 for storage in S
191f7 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e QLite.** data an
191f8 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e d index records.
191f9 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64 Each serialized
191fa 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20 value consists
191fb 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d of a.** 'serial-
191fc 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f 62 type' and a blob
191fd 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 65 of data. The se
191fe 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e 20 rial type is an
191ff 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 0a 8-byte unsigned.
19200 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f 72 ** integer, stor
19201 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e 0a ed as a varint..
19202 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69 **.** In an SQLi
19203 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2c te index record,
19204 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 the serial type
19205 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 63 is stored direc
19206 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 tly before.** th
19207 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 74 e blob of data t
19208 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f 6e hat it correspon
19209 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c ds to. In a tabl
1920a 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65 e record, all se
1920b 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72 rial.** types ar
1920c 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 e stored at the
1920d 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 start of the rec
1920e 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f ord, and the blo
1920f 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a bs of data at.**
19210 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 20 the end. Hence
19211 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 these functions
19212 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72 allow the caller
19213 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a to handle the.*
19214 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 6e * serial-type an
19215 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 65 d data blob sepe
19216 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 rately..**.** Th
19217 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c e following tabl
19218 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 e describes the
19219 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 20 various storage
1921a 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 61 classes for data
1921b 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c :.**.** serial
1921c 20 74 79 70 65 20 20 20 20 20 20 20 20 62 79 74 type byt
1921d 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 20 es of data
1921e 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d type.** ------
1921f 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d -------- ---
19220 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 ------------
19221 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a ---------------.
19222 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 ** 0
19223 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 0
19224 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a NULL.
19225 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 ** 1
19226 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20 1
19227 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 signe
19228 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 d integer.**
19229 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 2
1922a 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20 2
1922b 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 signed inte
1922c 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 ger.** 3
1922d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1922e 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 73 3 s
1922f 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
19230 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20 4
19231 20 20 20 20 20 20 20 20 20 20 20 20 34 20 20 20 4
19232 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 signed
19233 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 integer.**
19234 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5
19235 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20 6
19236 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 signed intege
19237 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 20 r.** 6
19238 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19239 38 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 8 sig
1923a 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 ned integer.**
1923b 20 20 20 20 37 20 20 20 20 20 20 20 20 20 20 20 7
1923c 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20 8
1923d 20 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f 61 IEEE floa
1923e 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 t.** 8
1923f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19240 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 0 Int
19241 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a eger constant 0.
19242 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 20 ** 9
19243 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 0
19244 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67 Integ
19245 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a er constant 1.**
19246 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 20 10,11
19247 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19248 20 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65 reserve
19249 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a d for expansion.
1924a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20 ** N>=12 and
1924b 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 32 even (N-12
1924c 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 0a )/2 BLOB.
1924d 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20 ** N>=13 and
1924e 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 33 odd (N-13
1924f 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 0a )/2 text.
19250 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 20 **.** The 8 and
19251 39 20 74 79 70 65 73 20 77 65 72 65 20 61 64 64 9 types were add
19252 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c ed in 3.3.0, fil
19253 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 69 e format 4. Pri
19254 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f or versions.** o
19255 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f f SQLite will no
19256 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 6f t understand tho
19257 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e se serial types.
19258 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 .*/../*.** Retur
19259 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 70 n the serial-typ
1925a 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 e for the value
1925b 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a stored in pMem..
1925c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1925d 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 E u32 sqlite3Vdb
1925e 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 20 eSerialType(Mem
1925f 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f *pMem, int file_
19260 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66 format){. int f
19261 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 lags = pMem->fla
19262 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 gs;. int n;..
19263 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 if( flags&MEM_Nu
19264 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ll ){. return
19265 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 0;. }. if( fl
19266 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 ags&MEM_Int ){.
19267 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 /* Figure out
19268 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 20 whether to use
19269 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 20 1, 2, 4, 6 or 8
1926a 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65 bytes. */.# de
1926b 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 28 fine MAX_6BYTE (
1926c 28 28 28 69 36 34 29 30 78 30 30 30 30 38 30 30 (((i64)0x0000800
1926d 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 69 0)<<32)-1). i
1926e 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 64 i = pMem->u.i
1926f 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 20 ;. u64 u;.
19270 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74 if( file_format
19271 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20 >=4 && (i&1)==i
19272 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
19273 38 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d 0a 8+(u32)i;. }.
19274 20 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69 u = i<0 ? -i
19275 20 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75 3c : i;. if( u<
19276 3d 31 32 37 20 29 20 72 65 74 75 72 6e 20 31 3b =127 ) return 1;
19277 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37 36 . if( u<=3276
19278 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7 ) return 2;.
19279 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30 37 if( u<=8388607
1927a 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 ) return 3;.
1927b 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33 36 if( u<=21474836
1927c 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a 20 47 ) return 4;.
1927d 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42 if( u<=MAX_6B
1927e 59 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b 0a YTE ) return 5;.
1927f 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 return 6;.
19280 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 }. if( flags&ME
19281 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 M_Real ){. re
19282 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73 turn 7;. }. as
19283 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e sert( pMem->db->
19284 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 mallocFailed ||
19285 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d flags&(MEM_Str|M
19286 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20 EM_Blob) );. n
19287 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28 = pMem->n;. if(
19288 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 flags & MEM_Zer
19289 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d o ){. n += pM
1928a 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d em->u.nZero;. }
1928b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 . assert( n>=0
1928c 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e 2a );. return ((n*
1928d 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61 67 2) + 12 + ((flag
1928e 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b s&MEM_Str)!=0));
1928f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
19290 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 the length of t
19291 68 65 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f he data correspo
19292 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 nding to the sup
19293 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79 70 plied serial-typ
19294 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
19295 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
19296 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
19297 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 n(u32 serial_typ
19298 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61 6c e){. if( serial
19299 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 _type>=12 ){.
1929a 20 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c 5f return (serial_
1929b 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65 type-12)/2;. }e
1929c 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 lse{. static
1929d 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b 5d const u8 aSize[]
1929e 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c = { 0, 1, 2, 3,
1929f 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 4, 6, 8, 8, 0,
192a0 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72 0, 0, 0 };. r
192a1 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 eturn aSize[seri
192a2 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a al_type];. }.}.
192a3 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 ./*.** If we are
192a4 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63 74 on an architect
192a5 75 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d 65 ure with mixed-e
192a6 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a ndian floating .
192a7 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20 41 ** points (ex: A
192a8 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20 74 RM7) then swap t
192a9 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65 73 he lower 4 bytes
192aa 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75 70 with the .** up
192ab 70 65 72 20 34 20 62 79 74 65 73 2e 20 20 52 65 per 4 bytes. Re
192ac 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 2e turn the result.
192ad 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20 .**.** For most
192ae 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 architectures, t
192af 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a his is a no-op..
192b0 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20 **.** (later):
192b1 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74 It is reported t
192b2 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d 69 o me that the mi
192b3 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c xed-endian probl
192b4 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73 em.** on ARM7 is
192b5 20 61 6e 20 69 73 73 75 65 20 77 69 74 68 20 47 an issue with G
192b6 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 CC, not with the
192b7 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74 20 ARM7 chip. It
192b8 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61 seems.** that ea
192b9 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 rly versions of
192ba 47 43 43 20 73 74 6f 72 65 64 20 74 68 65 20 74 GCC stored the t
192bb 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36 34 wo words of a 64
192bc 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e -bit.** float in
192bd 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 the wrong order
192be 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72 72 6f . And that erro
192bf 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70 61 r has been propa
192c0 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73 69 gated.** ever si
192c1 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 nce. The blame
192c2 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 is not necessari
192c3 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74 68 6f ly with GCC, tho
192c4 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68 ugh..** GCC migh
192c5 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 79 t have just copy
192c6 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 ing the problem
192c7 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d from a prior com
192c8 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61 piler..** I am a
192c9 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e 65 lso told that ne
192ca 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 wer versions of
192cb 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 GCC that follow
192cc 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41 a different.** A
192cd 42 49 20 67 65 74 20 74 68 65 20 62 79 74 65 20 BI get the byte
192ce 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a order right..**.
192cf 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75 73 ** Developers us
192d0 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e ing SQLite on an
192d1 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d ARM7 should com
192d2 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68 65 pile and run the
192d3 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f ir.** applicatio
192d4 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 n using -DSQLITE
192d5 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61 73 _DEBUG=1 at leas
192d6 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44 45 t once. With DE
192d7 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 BUG.** enabled,
192d8 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65 6c some asserts bel
192d9 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20 74 ow will ensure t
192da 68 61 74 20 74 68 65 20 62 79 74 65 20 6f 72 64 hat the byte ord
192db 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e er of.** floatin
192dc 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 69 g point values i
192dd 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a s correct..**.**
192de 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20 46 (2007-08-30) F
192df 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68 61 rank van Vugt ha
192e0 73 20 73 74 75 64 69 65 64 20 74 68 69 73 20 70 s studied this p
192e1 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a roblem closely.*
192e2 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20 68 * and has send h
192e3 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20 74 is findings to t
192e4 68 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c 6f he SQLite develo
192e5 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20 pers. Frank.**
192e6 77 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d 65 writes that some
192e7 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f Linux kernels o
192e8 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f ffer floating po
192e9 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a 20 int hardware.**
192ea 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20 75 emulation that u
192eb 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20 ses only 32-bit
192ec 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65 61 mantissas instea
192ed 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20 d of a full .**
192ee 34 38 2d 62 69 74 73 20 61 73 20 72 65 71 75 69 48-bits as requi
192ef 72 65 64 20 62 79 20 74 68 65 20 49 45 45 45 20 red by the IEEE
192f0 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69 73 standard. (This
192f1 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49 is the.** CONFI
192f2 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f 70 G_FPE_FASTFPE op
192f3 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20 tion.) On such
192f4 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e systems, floatin
192f5 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20 g point.** byte
192f6 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 73 swapping becomes
192f7 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 65 very complicate
192f8 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72 6f d. To avoid pro
192f9 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65 blems,.** the ne
192fa 63 65 73 73 61 72 79 20 62 79 74 65 20 73 77 61 cessary byte swa
192fb 70 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 64 pping is carried
192fc 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 2d out using a 64-
192fd 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 bit integer.** r
192fe 61 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 2d ather than a 64-
192ff 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e bit float. Fran
19300 6b 20 61 73 73 75 72 65 73 20 75 73 20 74 68 61 k assures us tha
19301 74 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 0a t the code here.
19302 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d ** works for him
19303 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76 65 6c . We, the devel
19304 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20 77 opers, have no w
19305 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e ay to independen
19306 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 tly.** verify th
19307 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73 65 is, but Frank se
19308 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74 ems to know what
19309 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20 61 he is talking a
1930a 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72 bout.** so we tr
1930b 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64 ust him..*/.#ifd
1930c 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f ef SQLITE_MIXED_
1930d 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f ENDIAN_64BIT_FLO
1930e 41 54 0a 73 74 61 74 69 63 20 75 36 34 20 66 6c AT.static u64 fl
1930f 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e 29 7b oatSwap(u64 in){
19310 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75 . union {. u
19311 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69 5b 64 r;. u32 i[
19312 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32 2];. } u;. u32
19313 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b t;.. u.r = in;
19314 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20 . t = u.i[0];.
19315 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d u.i[0] = u.i[1]
19316 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a ;. u.i[1] = t;.
19317 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a return u.r;.}.
19318 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 # define swapMix
19319 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 edEndianFloat(X)
1931a 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70 28 X = floatSwap(
1931b 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e X).#else.# defin
1931c 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 e swapMixedEndia
1931d 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69 66 nFloat(X).#endif
1931e 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 ../*.** Write th
1931f 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 e serialized dat
19320 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20 76 a blob for the v
19321 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70 alue stored in p
19322 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66 Mem into .** buf
19323 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 . It is assumed
19324 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 that the caller
19325 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 75 has allocated su
19326 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e 0a fficient space..
19327 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
19328 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 mber of bytes wr
19329 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75 itten..**.** nBu
1932a 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 f is the amount
1932b 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e of space left in
1932c 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75 buf[]. nBuf mu
1932d 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20 st always be.**
1932e 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 large enough to
1932f 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65 20 hold the entire
19330 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c 20 field. Except,
19331 69 66 20 74 68 65 20 66 69 65 6c 64 20 69 73 0a if the field is.
19332 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 61 ** a blob with a
19333 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 zero-filled tai
19334 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69 l, then buf[] mi
19335 67 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 20 ght be just the
19336 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f right.** size to
19337 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67 hold everything
19338 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 20 except for the
19339 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c zero-filled tail
1933a 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69 . If buf[].** i
1933b 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67 s only big enoug
1933c 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f h to hold the no
1933d 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20 74 n-zero prefix, t
1933e 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 hen only write t
1933f 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e hat.** prefix in
19340 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20 69 to buf[]. But i
19341 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67 65 f buf[] is large
19342 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 enough to hold
19343 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65 66 both the.** pref
19344 69 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c 20 ix and the tail
19345 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 70 then write the p
19346 72 65 66 69 78 20 61 6e 64 20 73 65 74 20 74 68 refix and set th
19347 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a e tail to all.**
19348 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 zeros..**.** Re
19349 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
1934a 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c of bytes actuall
1934b 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 62 y written into b
1934c 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65 uf[]. The numbe
1934d 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69 6e r.** of bytes in
1934e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 the zero-filled
1934f 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64 65 tail is include
19350 64 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 20 d in the return
19351 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 value only.** if
19352 20 74 68 6f 73 65 20 62 79 74 65 73 20 77 65 72 those bytes wer
19353 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66 5b e zeroed in buf[
19354 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 ]..*/ .SQLITE_PR
19355 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19356 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 3VdbeSerialPut(u
19357 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75 66 8 *buf, int nBuf
19358 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 , Mem *pMem, int
19359 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 file_format){.
1935a 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 u32 serial_type
1935b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1935c 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66 rialType(pMem, f
1935d 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 69 ile_format);. i
1935e 6e 74 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e nt len;.. /* In
1935f 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20 2a teger and Real *
19360 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 /. if( serial_t
19361 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 6c ype<=7 && serial
19362 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20 75 _type>0 ){. u
19363 36 34 20 76 3b 0a 20 20 20 20 69 6e 74 20 69 3b 64 v;. int i;
19364 0a 20 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f . if( serial_
19365 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20 type==7 ){.
19366 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 assert( sizeof(
19367 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d v)==sizeof(pMem-
19368 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d >r) );. mem
19369 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72 cpy(&v, &pMem->r
1936a 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20 , sizeof(v));.
1936b 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 swapMixedEnd
1936c 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20 ianFloat(v);.
1936d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 20 }else{. v
1936e 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 = pMem->u.i;.
1936f 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d }. len = i =
19370 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
19371 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c alTypeLen(serial
19372 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65 _type);. asse
19373 72 74 28 20 6c 65 6e 3c 3d 6e 42 75 66 20 29 3b rt( len<=nBuf );
19374 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 . while( i--
19375 29 7b 0a 20 20 20 20 20 20 62 75 66 5b 69 5d 20 ){. buf[i]
19376 3d 20 28 75 38 29 28 76 26 30 78 46 46 29 3b 0a = (u8)(v&0xFF);.
19377 20 20 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 v >>= 8;.
19378 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
19379 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 len;. }.. /* S
1937a 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 2a 2f tring or blob */
1937b 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 . if( serial_ty
1937c 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 61 73 pe>=12 ){. as
1937d 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 20 2b 20 sert( pMem->n +
1937e 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ((pMem->flags &
1937f 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d 65 6d 2d 3e MEM_Zero)?pMem->
19380 75 2e 6e 5a 65 72 6f 3a 30 29 0a 20 20 20 20 20 u.nZero:0).
19381 20 20 20 20 20 20 20 20 3d 3d 20 73 71 6c 69 74 == sqlit
19382 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
19383 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 Len(serial_type)
19384 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
19385 70 4d 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29 3b pMem->n<=nBuf );
19386 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d . len = pMem-
19387 3e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 62 >n;. memcpy(b
19388 75 66 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e uf, pMem->z, len
19389 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d );. if( pMem-
1938a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 >flags & MEM_Zer
1938b 6f 20 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20 2b o ){. len +
1938c 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b = pMem->u.nZero;
1938d 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3e 6e . if( len>n
1938e 42 75 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6c Buf ){. l
1938f 65 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 20 20 20 en = nBuf;.
19390 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 }. memset(
19391 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 &buf[pMem->n], 0
19392 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a , len-pMem->n);.
19393 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
19394 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 len;. }.. /*
19395 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74 NULL or constant
19396 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65 s 0 or 1 */. re
19397 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
19398 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 Deserialize the
19399 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74 data blob point
1939a 65 64 20 74 6f 20 62 79 20 62 75 66 20 61 73 20 ed to by buf as
1939b 73 65 72 69 61 6c 20 74 79 70 65 20 73 65 72 69 serial type seri
1939c 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73 al_type.** and s
1939d 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
1939e 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e in pMem. Return
1939f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
193a0 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 ytes read..*/ .S
193a1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
193a2 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 t sqlite3VdbeSer
193a3 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 20 ialGet(. const
193a4 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 62 unsigned char *b
193a5 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 65 uf, /* Buffe
193a6 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 r to deserialize
193a7 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73 from */. u32 s
193a8 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 erial_type,
193a9 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72 69 /* Seri
193aa 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65 72 al type to deser
193ab 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 ialize */. Mem
193ac 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 *pMem
193ad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
193ae 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 ory cell to writ
193af 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a e value into */.
193b0 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65 72 ){. switch( ser
193b1 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 20 ial_type ){.
193b2 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 65 case 10: /* Re
193b3 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 served for futur
193b4 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 e use */. cas
193b5 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 72 e 11: /* Reser
193b6 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 ved for future u
193b7 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 30 se */. case 0
193b8 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a : { /* NULL */.
193b9 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
193ba 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
193bb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
193bc 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20 2f . case 1: { /
193bd 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 * 1-byte signed
193be 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 integer */.
193bf 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 pMem->u.i = (si
193c0 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d gned char)buf[0]
193c1 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ;. pMem->fl
193c2 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
193c3 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
193c4 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32 3a }. case 2:
193c5 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 { /* 2-byte sig
193c6 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 ned integer */.
193c7 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d pMem->u.i =
193c8 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 29 (((signed char)
193c9 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 buf[0])<<8) | bu
193ca 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d f[1];. pMem
193cb 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
193cc 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 t;. return
193cd 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 2;. }. cas
193ce 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 e 3: { /* 3-byte
193cf 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
193d0 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 */. pMem->u
193d1 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 .i = (((signed c
193d2 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29 har)buf[0])<<16)
193d3 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c | (buf[1]<<8) |
193d4 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70 buf[2];. p
193d5 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
193d6 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 _Int;. retu
193d7 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 3;. }.
193d8 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 case 4: { /* 4-b
193d9 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
193da 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d er */. pMem
193db 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c ->u.i = (buf[0]<
193dc 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c <24) | (buf[1]<<
193dd 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 16) | (buf[2]<<8
193de 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20 ) | buf[3];.
193df 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
193e0 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 MEM_Int;. r
193e1 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 eturn 4;. }.
193e2 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20 case 5: { /*
193e3 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 6-byte signed in
193e4 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 teger */. u
193e5 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65 64 64 x = (((signed
193e6 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 char)buf[0])<<8
193e7 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 ) | buf[1];.
193e8 20 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b 32 u32 y = (buf[2
193e9 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 5d ]<<24) | (buf[3]
193ea 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c <<16) | (buf[4]<
193eb 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 <8) | buf[5];.
193ec 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 x = (x<<32)
193ed 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d | y;. pMem-
193ee 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 >u.i = *(i64*)&x
193ef 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ;. pMem->fl
193f0 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
193f1 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 return 6;.
193f2 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36 3a }. case 6:
193f3 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 /* 8-byte sig
193f4 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 ned integer */.
193f5 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20 case 7: { /*
193f6 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f IEEE floating po
193f7 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 int */. u64
193f8 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79 3b x;. u32 y;
193f9 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
193fa 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 EBUG) && !define
193fb 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c d(SQLITE_OMIT_FL
193fc 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 OATING_POINT).
193fd 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 /* Verify th
193fe 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 at integers and
193ff 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 floating point v
19400 61 6c 75 65 73 20 75 73 65 20 74 68 65 20 73 61 alues use the sa
19401 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 me. ** byte
19402 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 61 order. Or, tha
19403 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 t if SQLITE_MIXE
19404 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 D_ENDIAN_64BIT_F
19405 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a 2a LOAT is. **
19406 20 64 65 66 69 6e 65 64 20 74 68 61 74 20 36 34 defined that 64
19407 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f -bit floating po
19408 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c 6c int values reall
19409 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20 20 y are mixed.
1940a 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20 ** endian..
1940b 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 */. stat
1940c 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31 20 ic const u64 t1
1940d 3d 20 28 28 75 36 34 29 30 78 33 66 66 30 30 30 = ((u64)0x3ff000
1940e 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73 00)<<32;. s
1940f 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 tatic const doub
19410 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20 le r1 = 1.0;.
19411 20 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0a u64 t2 = t1;.
19412 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 swapMixedE
19413 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a ndianFloat(t2);.
19414 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 assert( si
19415 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66 zeof(r1)==sizeof
19416 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 26 (t2) && memcmp(&
19417 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 28 r1, &t2, sizeof(
19418 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 r1))==0 );.#endi
19419 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62 75 f.. x = (bu
1941a 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 f[0]<<24) | (buf
1941b 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b [1]<<16) | (buf[
1941c 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 2]<<8) | buf[3];
1941d 0a 20 20 20 20 20 20 79 20 3d 20 28 62 75 66 5b . y = (buf[
1941e 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 35 4]<<24) | (buf[5
1941f 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5d ]<<16) | (buf[6]
19420 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 <<8) | buf[7];.
19421 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 x = (x<<32)
19422 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28 20 | y;. if(
19423 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29 serial_type==6 )
19424 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e {. pMem->
19425 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b u.i = *(i64*)&x;
19426 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 . pMem->f
19427 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
19428 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
19429 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a assert( siz
1942a 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69 7a eof(x)==8 && siz
1942b 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 eof(pMem->r)==8
1942c 29 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d );. swapM
1942d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 ixedEndianFloat(
1942e 78 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 x);. memc
1942f 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c py(&pMem->r, &x,
19430 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 sizeof(x));.
19431 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
19432 20 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 = sqlite3IsNaN(
19433 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e pMem->r) ? MEM_N
19434 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a ull : MEM_Real;.
19435 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
19436 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20 turn 8;. }.
19437 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a 20 case 8: /*
19438 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 20 Integer 0 */.
19439 20 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49 case 9: { /* I
1943a 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 20 nteger 1 */.
1943b 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 pMem->u.i = se
1943c 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20 rial_type-8;.
1943d 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1943e 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 MEM_Int;.
1943f 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a return 0;. }.
19440 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
19441 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 int len = (
19442 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f serial_type-12)/
19443 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 2;. pMem->z
19444 20 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b 0a = (char *)buf;.
19445 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 pMem->n =
19446 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d len;. pMem-
19447 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 >xDel = 0;.
19448 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 if( serial_type
19449 26 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20 20 &0x01 ){.
1944a 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1944b 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 EM_Str | MEM_Eph
1944c 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b em;. }else{
1944d 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 . pMem->f
1944e 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 lags = MEM_Blob
1944f 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 | MEM_Ephem;.
19450 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
19451 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d n len;. }. }
19452 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
19453 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 ./*.** Given the
19454 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f 64 nKey-byte encod
19455 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 ing of a record
19456 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65 in pKey[], parse
19457 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 the.** record i
19458 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 65 nto a UnpackedRe
19459 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e 20 cord structure.
1945a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
1945b 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72 r to.** that str
1945c 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 ucture..**.** Th
1945d 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
1945e 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65 on might provide
1945f 20 73 7a 53 70 61 63 65 20 62 79 74 65 73 20 6f szSpace bytes o
19460 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 f memory.** spac
19461 65 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 e at pSpace. Th
19462 69 73 20 73 70 61 63 65 20 63 61 6e 20 62 65 20 is space can be
19463 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 used to hold the
19464 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62 returned.** VDb
19465 65 50 61 72 73 65 64 52 65 63 6f 72 64 20 73 74 eParsedRecord st
19466 72 75 63 74 75 72 65 20 69 66 20 69 74 20 69 73 ructure if it is
19467 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 20 large enough.
19468 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 If it is.** not
19469 62 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 63 big enough, spac
1946a 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 e is obtained fr
1946b 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
1946c 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 c()..**.** The r
1946d 65 74 75 72 6e 65 64 20 73 74 72 75 63 74 75 72 eturned structur
1946e 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 e should be clos
1946f 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a ed by a call to.
19470 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 ** sqlite3VdbeDe
19471 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f leteUnpackedReco
19472 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 rd()..*/ .SQLITE
19473 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 _PRIVATE Unpacke
19474 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 dRecord *sqlite3
19475 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b VdbeRecordUnpack
19476 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 (. KeyInfo *pKe
19477 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e yInfo, /* In
19478 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
19479 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 the record forma
1947a 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c t */. int nKey,
1947b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1947c 20 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69 6e Size of the bin
1947d 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 ary record */.
1947e 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 const void *pKey
1947f 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 , /* The bi
19480 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 nary record */.
19481 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
19482 2a 70 53 70 61 63 65 2c 2f 2a 20 53 70 61 63 65 *pSpace,/* Space
19483 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68 6f available to ho
19484 6c 64 20 72 65 73 75 6c 74 69 6e 67 20 6f 62 6a ld resulting obj
19485 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 ect */. int szS
19486 70 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20 pace
19487 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70 61 63 /* Size of pSpac
19488 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a e[] in bytes */.
19489 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 ){. const unsig
1948a 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 20 3d ned char *aKey =
1948b 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 (const unsigned
1948c 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20 char *)pKey;.
1948d 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
1948e 70 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 p;. int nByte,
1948f 64 3b 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 20 d;. u32 idx;.
19490 75 31 36 20 75 3b 20 20 20 20 20 20 20 20 20 20 u16 u;
19491 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e /* Unsign
19492 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 ed loop counter
19493 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 0a */. u32 szHdr;.
19494 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 0a Mem *pMem;. .
19495 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
19496 28 4d 65 6d 29 3e 73 69 7a 65 6f 66 28 2a 70 29 (Mem)>sizeof(*p)
19497 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 );. nByte = si
19498 7a 65 6f 66 28 4d 65 6d 29 2a 28 70 4b 65 79 49 zeof(Mem)*(pKeyI
19499 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 32 29 3b 0a nfo->nField+2);.
1949a 20 20 69 66 28 20 6e 42 79 74 65 3e 73 7a 53 70 if( nByte>szSp
1949b 61 63 65 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 ace ){. p = s
1949c 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
1949d 77 28 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 w(pKeyInfo->db,
1949e 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 nByte);. if(
1949f 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b p==0 ) return 0;
194a0 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 . p->flags =
194a1 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 46 52 UNPACKED_NEED_FR
194a2 45 45 20 7c 20 55 4e 50 41 43 4b 45 44 5f 4e 45 EE | UNPACKED_NE
194a3 45 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20 7d 65 ED_DESTROY;. }e
194a4 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 70 53 70 lse{. p = pSp
194a5 61 63 65 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 ace;. p->flag
194a6 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 s = UNPACKED_NEE
194a7 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20 7d 0a 20 D_DESTROY;. }.
194a8 20 70 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 p->pKeyInfo = p
194a9 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e 46 KeyInfo;. p->nF
194aa 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d ield = pKeyInfo-
194ab 3e 6e 46 69 65 6c 64 20 2b 20 31 3b 0a 20 20 70 >nField + 1;. p
194ac 2d 3e 61 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 ->aMem = pMem =
194ad 26 28 28 4d 65 6d 2a 29 70 29 5b 31 5d 3b 0a 20 &((Mem*)p)[1];.
194ae 20 69 64 78 20 3d 20 67 65 74 56 61 72 69 6e 74 idx = getVarint
194af 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29 3b 32(aKey, szHdr);
194b0 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20 20 . d = szHdr;.
194b1 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 u = 0;. while(
194b2 69 64 78 3c 73 7a 48 64 72 20 26 26 20 75 3c 70 idx<szHdr && u<p
194b3 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 ->nField ){.
194b4 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b u32 serial_type;
194b5 0a 0a 20 20 20 20 69 64 78 20 2b 3d 20 67 65 74 .. idx += get
194b6 56 61 72 69 6e 74 33 32 28 26 61 4b 65 79 5b 69 Varint32(&aKey[i
194b7 64 78 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 dx], serial_type
194b8 29 3b 0a 20 20 20 20 69 66 28 20 64 3e 3d 6e 4b );. if( d>=nK
194b9 65 79 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 ey && sqlite3Vdb
194ba 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 eSerialTypeLen(s
194bb 65 72 69 61 6c 5f 74 79 70 65 29 3e 30 20 29 20 erial_type)>0 )
194bc 62 72 65 61 6b 3b 0a 20 20 20 20 70 4d 65 6d 2d break;. pMem-
194bd 3e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f 2d >enc = pKeyInfo-
194be 3e 65 6e 63 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e >enc;. pMem->
194bf 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 db = pKeyInfo->d
194c0 62 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 b;. pMem->fla
194c1 67 73 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d gs = 0;. pMem
194c2 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 ->zMalloc = 0;.
194c3 20 20 20 64 20 2b 3d 20 73 71 6c 69 74 65 33 56 d += sqlite3V
194c4 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 4b dbeSerialGet(&aK
194c5 65 79 5b 64 5d 2c 20 73 65 72 69 61 6c 5f 74 79 ey[d], serial_ty
194c6 70 65 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 70 pe, pMem);. p
194c7 4d 65 6d 2b 2b 3b 0a 20 20 20 20 75 2b 2b 3b 0a Mem++;. u++;.
194c8 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 75 3c }. assert( u<
194c9 3d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c =pKeyInfo->nFiel
194ca 64 20 2b 20 31 20 29 3b 0a 20 20 70 2d 3e 6e 46 d + 1 );. p->nF
194cb 69 65 6c 64 20 3d 20 75 3b 0a 20 20 72 65 74 75 ield = u;. retu
194cc 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a rn (void*)p;.}..
194cd 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
194ce 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20 55 6e ne destroys a Un
194cf 70 61 63 6b 65 64 52 65 63 6f 72 64 20 6f 62 6a packedRecord obj
194d0 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ect.*/.SQLITE_PR
194d1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
194d2 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 e3VdbeDeleteUnpa
194d3 63 6b 65 64 52 65 63 6f 72 64 28 55 6e 70 61 63 ckedRecord(Unpac
194d4 6b 65 64 52 65 63 6f 72 64 20 2a 70 29 7b 0a 20 kedRecord *p){.
194d5 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 if( p ){. if
194d6 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 ( p->flags & UNP
194d7 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 ACKED_NEED_DESTR
194d8 4f 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 OY ){. int
194d9 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d i;. Mem *pM
194da 65 6d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d em;. for(i=
194db 30 2c 20 70 4d 65 6d 3d 70 2d 3e 61 4d 65 6d 3b 0, pMem=p->aMem;
194dc 20 69 3c 70 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b i<p->nField; i+
194dd 2b 2c 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 +, pMem++){.
194de 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 4d if( pMem->zM
194df 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 alloc ){.
194e0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
194e1 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
194e2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
194e3 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
194e4 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 p->flags & UNPAC
194e5 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 29 7b KED_NEED_FREE ){
194e6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
194e7 46 72 65 65 28 70 2d 3e 70 4b 65 79 49 6e 66 6f Free(p->pKeyInfo
194e8 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 0a ->db, p);. }.
194e9 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
194ea 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 s function compa
194eb 72 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c res the two tabl
194ec 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 e rows or index
194ed 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63 69 records.** speci
194ee 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20 fied by {nKey1,
194ef 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 79 pKey1} and pPKey
194f0 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 2. It returns a
194f1 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 0a negative, zero.
194f2 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 ** or positive i
194f3 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 69 nteger if key1 i
194f4 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 s less than, equ
194f5 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 al to or .** gre
194f6 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e 20 ater than key2.
194f7 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 The {nKey1, pKe
194f8 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 20 y1} key must be
194f9 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65 a blob.** create
194fa 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52 d by th OP_MakeR
194fb 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 ecord opcode of
194fc 74 68 65 20 56 44 42 45 2e 20 20 54 68 65 20 70 the VDBE. The p
194fd 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 PKey2.** key mus
194fe 74 20 62 65 20 61 20 70 61 72 73 65 64 20 6b 65 t be a parsed ke
194ff 79 20 73 75 63 68 20 61 73 20 6f 62 74 61 69 6e y such as obtain
19500 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 ed from.** sqlit
19501 65 33 56 64 62 65 50 61 72 73 65 52 65 63 6f 72 e3VdbeParseRecor
19502 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e d..**.** Key1 an
19503 64 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 61 d Key2 do not ha
19504 76 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 ve to contain th
19505 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 e same number of
19506 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 20 fields..** The
19507 6b 65 79 20 77 69 74 68 20 66 65 77 65 72 20 66 key with fewer f
19508 69 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c 79 ields is usually
19509 20 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20 74 compares less t
1950a 68 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67 han the .** long
1950b 65 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65 72 er key. However
1950c 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 if the UNPACKED
1950d 5f 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20 69 _INCRKEY flags i
1950e 6e 20 70 50 4b 65 79 32 20 69 73 20 73 65 74 0a n pPKey2 is set.
1950f 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d 6f ** and the commo
19510 6e 20 70 72 65 66 69 78 65 73 20 61 72 65 20 65 n prefixes are e
19511 71 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31 20 qual, then key1
19512 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65 79 is less than key
19513 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65 20 2..** Or if the
19514 55 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f 50 UNPACKED_MATCH_P
19515 52 45 46 49 58 20 66 6c 61 67 20 69 73 20 73 65 REFIX flag is se
19516 74 20 61 6e 64 20 74 68 65 20 70 72 65 66 69 78 t and the prefix
19517 65 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c 2c es are.** equal,
19518 20 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20 61 then the keys a
19519 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f re considered to
1951a 20 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a 2a be equal and.**
1951b 20 74 68 65 20 70 61 72 74 73 20 62 65 79 6f 6e the parts beyon
1951c 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 d the common pre
1951d 66 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64 2e fix are ignored.
1951e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55 4e .**.** If the UN
1951f 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f PACKED_IGNORE_RO
19520 57 49 44 20 66 6c 61 67 20 69 73 20 73 65 74 2c WID flag is set,
19521 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 62 then the last b
19522 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65 yte of.** the he
19523 61 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69 73 ader of pKey1 is
19524 20 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69 73 ignored. It is
19525 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 70 4b assumed that pK
19526 65 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 ey1 is.** an ind
19527 65 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75 73 ex key, and thus
19528 20 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f 77 ends with a row
19529 69 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20 6c id value. The l
1952a 61 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 ast byte.** of t
1952b 68 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20 74 he header will t
1952c 68 65 72 65 66 6f 72 65 20 62 65 20 74 68 65 20 herefore be the
1952d 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 serial type of t
1952e 68 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65 he rowid:.** one
1952f 20 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 of 1, 2, 3, 4,
19530 35 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d 20 5, 6, 8, or 9 -
19531 74 68 65 20 69 6e 74 65 67 65 72 20 73 65 72 69 the integer seri
19532 61 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68 65 al types..** The
19533 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 serial type of
19534 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 the final rowid
19535 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 61 will always be a
19536 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a single byte..**
19537 20 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68 69 By ignoring thi
19538 73 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20 74 s last byte of t
19539 68 65 20 68 65 61 64 65 72 2c 20 77 65 20 66 6f he header, we fo
1953a 72 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 rce the comparis
1953b 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 on.** to ignore
1953c 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 the rowid at the
1953d 20 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f end of key1..*/
1953e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1953f 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 int sqlite3VdbeR
19540 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20 20 ecordCompare(.
19541 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 int nKey1, const
19542 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f 2a void *pKey1, /*
19543 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20 55 Left key */. U
19544 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 npackedRecord *p
19545 50 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a 20 PKey2 /*
19546 52 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a Right key */.){.
19547 20 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20 20 int d1;
19548 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
19549 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 nto aKey[] of ne
1954a 78 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 xt data element
1954b 2a 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20 20 */. u32 idx1;
1954c 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
1954d 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 t into aKey[] of
1954e 20 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c 65 next header ele
1954f 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 7a ment */. u32 sz
19550 48 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 Hdr1; /*
19551 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
19552 69 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 in header */. i
19553 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20 nt i = 0;. int
19554 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63 nField;. int rc
19555 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e = 0;. const un
19556 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 signed char *aKe
19557 79 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 y1 = (const unsi
19558 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 gned char *)pKey
19559 31 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 1;. KeyInfo *pK
1955a 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 eyInfo;. Mem me
1955b 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f 20 m1;.. pKeyInfo
1955c 3d 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e = pPKey2->pKeyIn
1955d 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d fo;. mem1.enc =
1955e 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a pKeyInfo->enc;.
1955f 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65 79 mem1.db = pKey
19560 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65 6d 31 Info->db;. mem1
19561 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d 65 .flags = 0;. me
19562 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a m1.zMalloc = 0;.
19563 20 20 0a 20 20 69 64 78 31 20 3d 20 67 65 74 56 . idx1 = getV
19564 61 72 69 6e 74 33 32 28 61 4b 65 79 31 2c 20 73 arint32(aKey1, s
19565 7a 48 64 72 31 29 3b 0a 20 20 64 31 20 3d 20 73 zHdr1);. d1 = s
19566 7a 48 64 72 31 3b 0a 20 20 69 66 28 20 70 50 4b zHdr1;. if( pPK
19567 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 ey2->flags & UNP
19568 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 ACKED_IGNORE_ROW
19569 49 44 20 29 7b 0a 20 20 20 20 73 7a 48 64 72 31 ID ){. szHdr1
1956a 2d 2d 3b 0a 20 20 7d 0a 20 20 6e 46 69 65 6c 64 --;. }. nField
1956b 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 = pKeyInfo->nFi
1956c 65 6c 64 3b 0a 20 20 77 68 69 6c 65 28 20 69 64 eld;. while( id
1956d 78 31 3c 73 7a 48 64 72 31 20 26 26 20 69 3c 70 x1<szHdr1 && i<p
1956e 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 20 29 7b PKey2->nField ){
1956f 0a 20 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f . u32 serial_
19570 74 79 70 65 31 3b 0a 0a 20 20 20 20 2f 2a 20 52 type1;.. /* R
19571 65 61 64 20 74 68 65 20 73 65 72 69 61 6c 20 74 ead the serial t
19572 79 70 65 73 20 66 6f 72 20 74 68 65 20 6e 65 78 ypes for the nex
19573 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 65 61 63 t element in eac
19574 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 69 64 h key. */. id
19575 78 31 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 x1 += getVarint3
19576 32 28 20 61 4b 65 79 31 2b 69 64 78 31 2c 20 73 2( aKey1+idx1, s
19577 65 72 69 61 6c 5f 74 79 70 65 31 20 29 3b 0a 20 erial_type1 );.
19578 20 20 20 69 66 28 20 64 31 3e 3d 6e 4b 65 79 31 if( d1>=nKey1
19579 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 53 && sqlite3VdbeS
1957a 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 erialTypeLen(ser
1957b 69 61 6c 5f 74 79 70 65 31 29 3e 30 20 29 20 62 ial_type1)>0 ) b
1957c 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 reak;.. /* Ex
1957d 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 73 tract the values
1957e 20 74 6f 20 62 65 20 63 6f 6d 70 61 72 65 64 2e to be compared.
1957f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 31 20 2b . */. d1 +
19580 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 = sqlite3VdbeSer
19581 69 61 6c 47 65 74 28 26 61 4b 65 79 31 5b 64 31 ialGet(&aKey1[d1
19582 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 31 2c ], serial_type1,
19583 20 26 6d 65 6d 31 29 3b 0a 0a 20 20 20 20 2f 2a &mem1);.. /*
19584 20 44 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 Do the comparis
19585 6f 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 on. */. rc
19586 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d = sqlite3MemCom
19587 70 61 72 65 28 26 6d 65 6d 31 2c 20 26 70 50 4b pare(&mem1, &pPK
19588 65 79 32 2d 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 20 ey2->aMem[i],.
19589 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1958a 20 20 20 20 20 20 20 20 20 69 3c 6e 46 69 65 6c i<nFiel
1958b 64 20 3f 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 d ? pKeyInfo->aC
1958c 6f 6c 6c 5b 69 5d 20 3a 20 30 29 3b 0a 20 20 20 oll[i] : 0);.
1958d 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 if( rc!=0 ){.
1958e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1958f 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 . i++;. }.
19590 69 66 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 if( mem1.zMalloc
19591 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 ) sqlite3VdbeMe
19592 6d 52 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b mRelease(&mem1);
19593 0a 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b .. if( rc==0 ){
19594 0a 20 20 20 20 2f 2a 20 72 63 3d 3d 30 20 68 65 . /* rc==0 he
19595 72 65 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e re means that on
19596 65 20 6f 66 20 74 68 65 20 6b 65 79 73 20 72 61 e of the keys ra
19597 6e 20 6f 75 74 20 6f 66 20 66 69 65 6c 64 73 20 n out of fields
19598 61 6e 64 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 74 and. ** all t
19599 68 65 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 he fields up to
1959a 74 68 61 74 20 70 6f 69 6e 74 20 77 65 72 65 20 that point were
1959b 65 71 75 61 6c 2e 20 49 66 20 74 68 65 20 55 4e equal. If the UN
1959c 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0a 20 PACKED_INCRKEY.
1959d 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 ** flag is se
1959e 74 2c 20 74 68 65 6e 20 62 72 65 61 6b 20 74 68 t, then break th
1959f 65 20 74 69 65 20 62 79 20 74 72 65 61 74 69 6e e tie by treatin
195a0 67 20 6b 65 79 32 20 61 73 20 6c 61 72 67 65 72 g key2 as larger
195a1 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 .. ** If the
195a2 55 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d UPACKED_PREFIX_M
195a3 41 54 43 48 20 66 6c 61 67 20 69 73 20 73 65 74 ATCH flag is set
195a4 2c 20 74 68 65 6e 20 6b 65 79 73 20 77 69 74 68 , then keys with
195a5 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65 73 common prefixes
195a6 0a 20 20 20 20 2a 2a 20 61 72 65 20 63 6f 6e 73 . ** are cons
195a7 69 64 65 72 65 64 20 74 6f 20 62 65 20 65 71 75 idered to be equ
195a8 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 al. Otherwise,
195a9 74 68 65 20 6c 6f 6e 67 65 72 20 6b 65 79 20 69 the longer key i
195aa 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 6c 61 s the . ** la
195ab 72 67 65 72 2e 20 20 41 73 20 69 74 20 68 61 70 rger. As it hap
195ac 70 65 6e 73 2c 20 74 68 65 20 70 50 4b 65 79 32 pens, the pPKey2
195ad 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 will always be
195ae 74 68 65 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a the longer. *
195af 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 * if there is a
195b0 64 69 66 66 65 72 65 6e 63 65 2e 0a 20 20 20 20 difference..
195b1 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 4b 65 79 */. if( pPKey
195b2 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 2->flags & UNPAC
195b3 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29 7b 0a 20 KED_INCRKEY ){.
195b4 20 20 20 20 20 72 63 20 3d 20 2d 31 3b 0a 20 20 rc = -1;.
195b5 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 4b 65 }else if( pPKe
195b6 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 y2->flags & UNPA
195b7 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 CKED_PREFIX_MATC
195b8 48 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 H ){. /* Le
195b9 61 76 65 20 72 63 3d 3d 30 20 2a 2f 0a 20 20 20 ave rc==0 */.
195ba 20 7d 65 6c 73 65 20 69 66 28 20 69 64 78 31 3c }else if( idx1<
195bb 73 7a 48 64 72 31 20 29 7b 0a 20 20 20 20 20 20 szHdr1 ){.
195bc 72 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 rc = 1;. }.
195bd 7d 65 6c 73 65 20 69 66 28 20 70 4b 65 79 49 6e }else if( pKeyIn
195be 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26 fo->aSortOrder &
195bf 26 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 & i<pKeyInfo->nF
195c0 69 65 6c 64 0a 20 20 20 20 20 20 20 20 20 20 20 ield.
195c1 20 20 20 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d && pKeyInfo-
195c2 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29 >aSortOrder[i] )
195c3 7b 0a 20 20 20 20 72 63 20 3d 20 2d 72 63 3b 0a {. rc = -rc;.
195c4 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
195c5 3b 0a 7d 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 ;.}. ../*.** pCu
195c6 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 69 r points at an i
195c7 6e 64 65 78 20 65 6e 74 72 79 20 63 72 65 61 74 ndex entry creat
195c8 65 64 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f ed using the OP_
195c9 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 MakeRecord opcod
195ca 65 2e 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 72 e..** Read the r
195cb 6f 77 69 64 20 28 74 68 65 20 6c 61 73 74 20 66 owid (the last f
195cc 69 65 6c 64 20 69 6e 20 74 68 65 20 72 65 63 6f ield in the reco
195cd 72 64 29 20 61 6e 64 20 73 74 6f 72 65 20 69 74 rd) and store it
195ce 20 69 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52 in *rowid..** R
195cf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
195d0 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f if everything wo
195d1 72 6b 73 2c 20 6f 72 20 61 6e 20 65 72 72 6f 72 rks, or an error
195d2 20 63 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e code otherwise.
195d3 0a 2a 2a 0a 2a 2a 20 70 43 75 72 20 6d 69 67 68 .**.** pCur migh
195d4 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f t be pointing to
195d5 20 74 65 78 74 20 6f 62 74 61 69 6e 65 64 20 66 text obtained f
195d6 72 6f 6d 20 61 20 63 6f 72 72 75 70 74 20 64 61 rom a corrupt da
195d7 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 tabase file..**
195d8 53 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 So the content c
195d9 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 annot be trusted
195da 2e 20 20 44 6f 20 61 70 70 72 6f 70 72 69 61 74 . Do appropriat
195db 65 20 63 68 65 63 6b 73 20 6f 6e 20 74 68 65 20 e checks on the
195dc 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 content..*/.SQLI
195dd 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
195de 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 qlite3VdbeIdxRow
195df 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 id(BtCursor *pCu
195e0 72 2c 20 69 36 34 20 2a 72 6f 77 69 64 29 7b 0a r, i64 *rowid){.
195e1 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d i64 nCellKey =
195e2 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 0;. int rc;.
195e3 75 33 32 20 73 7a 48 64 72 3b 20 20 20 20 20 20 u32 szHdr;
195e4 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
195e5 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 header */. u32
195e6 20 74 79 70 65 52 6f 77 69 64 3b 20 20 20 20 2f typeRowid; /
195e7 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 6f 66 * Serial type of
195e8 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 the rowid */.
195e9 75 33 32 20 6c 65 6e 52 6f 77 69 64 3b 20 20 20 u32 lenRowid;
195ea 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
195eb 20 72 6f 77 69 64 20 2a 2f 0a 20 20 4d 65 6d 20 rowid */. Mem
195ec 6d 2c 20 76 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 m, v;.. /* Get
195ed 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
195ee 69 6e 64 65 78 20 65 6e 74 72 79 2e 20 20 4f 6e index entry. On
195ef 6c 79 20 69 6e 64 69 63 65 73 20 65 6e 74 72 69 ly indices entri
195f0 65 73 20 6f 66 20 6c 65 73 73 0a 20 20 2a 2a 20 es of less. **
195f1 74 68 61 6e 20 32 47 69 42 20 61 72 65 20 73 75 than 2GiB are su
195f2 70 70 6f 72 74 20 2d 20 61 6e 79 74 68 69 6e 67 pport - anything
195f3 20 6c 61 72 67 65 20 6d 75 73 74 20 62 65 20 64 large must be d
195f4 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 atabase corrupti
195f5 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 on */. sqlite3B
195f6 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 treeKeySize(pCur
195f7 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 , &nCellKey);.
195f8 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 6e 43 65 if( unlikely(nCe
195f9 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65 6c llKey<=0 || nCel
195fa 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66 29 lKey>0x7fffffff)
195fb 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
195fc 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
195fd 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 PT;. }.. /* Re
195fe 61 64 20 69 6e 20 74 68 65 20 63 6f 6d 70 6c 65 ad in the comple
195ff 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 te content of th
19600 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f e index entry */
19601 0a 20 20 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a . m.flags = 0;.
19602 20 20 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 6d 2e m.db = 0;. m.
19603 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 72 zMalloc = 0;. r
19604 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
19605 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 75 72 emFromBtree(pCur
19606 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b , 0, (int)nCellK
19607 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 ey, 1, &m);. if
19608 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 ( rc ){. retu
19609 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rn rc;. }.. /*
1960a 20 54 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 The index entry
1960b 20 6d 75 73 74 20 62 65 67 69 6e 20 77 69 74 68 must begin with
1960c 20 61 20 68 65 61 64 65 72 20 73 69 7a 65 20 2a a header size *
1960d 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72 /. (void)getVar
1960e 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c 20 int32((u8*)m.z,
1960f 73 7a 48 64 72 29 3b 0a 20 20 74 65 73 74 63 61 szHdr);. testca
19610 73 65 28 20 73 7a 48 64 72 3d 3d 32 20 29 3b 0a se( szHdr==2 );.
19611 20 20 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 testcase( szHd
19612 72 3d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66 28 20 r==m.n );. if(
19613 75 6e 6c 69 6b 65 6c 79 28 73 7a 48 64 72 3c 32 unlikely(szHdr<2
19614 20 7c 7c 20 28 69 6e 74 29 73 7a 48 64 72 3e 6d || (int)szHdr>m
19615 2e 6e 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 .n) ){. goto
19616 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 idx_rowid_corrup
19617 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 tion;. }.. /*
19618 54 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 6f The last field o
19619 66 20 74 68 65 20 69 6e 64 65 78 20 73 68 6f 75 f the index shou
1961a 6c 64 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 ld be an integer
1961b 20 2d 20 74 68 65 20 52 4f 57 49 44 2e 0a 20 20 - the ROWID..
1961c 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 ** Verify that t
1961d 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 72 65 he last entry re
1961e 61 6c 6c 79 20 69 73 20 61 6e 20 69 6e 74 65 67 ally is an integ
1961f 65 72 2e 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67 er. */. (void)g
19620 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29 etVarint32((u8*)
19621 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 74 &m.z[szHdr-1], t
19622 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 73 ypeRowid);. tes
19623 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 tcase( typeRowid
19624 3d 3d 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 ==1 );. testcas
19625 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 32 20 e( typeRowid==2
19626 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 );. testcase( t
19627 79 70 65 52 6f 77 69 64 3d 3d 33 20 29 3b 0a 20 ypeRowid==3 );.
19628 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 testcase( typeR
19629 6f 77 69 64 3d 3d 34 20 29 3b 0a 20 20 74 65 73 owid==4 );. tes
1962a 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 tcase( typeRowid
1962b 3d 3d 35 20 29 3b 0a 20 20 74 65 73 74 63 61 73 ==5 );. testcas
1962c 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 36 20 e( typeRowid==6
1962d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 );. testcase( t
1962e 79 70 65 52 6f 77 69 64 3d 3d 38 20 29 3b 0a 20 ypeRowid==8 );.
1962f 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 testcase( typeR
19630 6f 77 69 64 3d 3d 39 20 29 3b 0a 20 20 69 66 28 owid==9 );. if(
19631 20 75 6e 6c 69 6b 65 6c 79 28 74 79 70 65 52 6f unlikely(typeRo
19632 77 69 64 3c 31 20 7c 7c 20 74 79 70 65 52 6f 77 wid<1 || typeRow
19633 69 64 3e 39 20 7c 7c 20 74 79 70 65 52 6f 77 69 id>9 || typeRowi
19634 64 3d 3d 37 29 20 29 7b 0a 20 20 20 20 67 6f 74 d==7) ){. got
19635 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 o idx_rowid_corr
19636 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 6c 65 uption;. }. le
19637 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 nRowid = sqlite3
19638 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
19639 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 n(typeRowid);.
1963a 74 65 73 74 63 61 73 65 28 20 6d 2e 6e 2d 6c 65 testcase( m.n-le
1963b 6e 52 6f 77 69 64 3d 3d 73 7a 48 64 72 20 29 3b nRowid==szHdr );
1963c 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 . if( unlikely(
1963d 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 3c 73 7a 48 m.n-lenRowid<szH
1963e 64 72 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 dr) ){. goto
1963f 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 idx_rowid_corrup
19640 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 tion;. }.. /*
19641 46 65 74 63 68 20 74 68 65 20 69 6e 74 65 67 65 Fetch the intege
19642 72 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66 r off the end of
19643 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f 72 the index recor
19644 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 d */. sqlite3Vd
19645 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a beSerialGet((u8*
19646 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77 )&m.z[m.n-lenRow
19647 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64 2c 20 id], typeRowid,
19648 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20 3d 20 &v);. *rowid =
19649 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74 65 33 v.u.i;. sqlite3
1964a 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
1964b 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c m);. return SQL
1964c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75 ITE_OK;.. /* Ju
1964d 6d 70 20 68 65 72 65 20 69 66 20 64 61 74 61 62 mp here if datab
1964e 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 ase corruption i
1964f 73 20 64 65 74 65 63 74 65 64 20 61 66 74 65 72 s detected after
19650 20 6d 20 68 61 73 20 62 65 65 6e 0a 20 20 2a 2a m has been. **
19651 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 72 65 allocated. Fre
19652 65 20 74 68 65 20 6d 20 6f 62 6a 65 63 74 20 61 e the m object a
19653 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 nd return SQLITE
19654 5f 43 4f 52 52 55 50 54 2e 20 2a 2f 0a 69 64 78 _CORRUPT. */.idx
19655 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f _rowid_corruptio
19656 6e 3a 0a 20 20 74 65 73 74 63 61 73 65 28 20 6d n:. testcase( m
19657 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b 0a 20 .zMalloc!=0 );.
19658 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19659 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 elease(&m);. re
1965a 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1965b 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a 0a UPT_BKPT;.}../*.
1965c 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 6b ** Compare the k
1965d 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 ey of the index
1965e 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 6f entry that curso
1965f 72 20 70 43 20 69 73 20 70 6f 69 6e 74 20 74 6f r pC is point to
19660 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20 against.** the
19661 6b 65 79 20 73 74 72 69 6e 67 20 69 6e 20 70 4b key string in pK
19662 65 79 20 28 6f 66 20 6c 65 6e 67 74 68 20 6e 4b ey (of length nK
19663 65 79 29 2e 20 20 57 72 69 74 65 20 69 6e 74 6f ey). Write into
19664 20 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72 0a *pRes a number.
19665 2a 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61 74 ** that is negat
19666 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f ive, zero, or po
19667 73 69 74 69 76 65 20 69 66 20 70 43 20 69 73 20 sitive if pC is
19668 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c less than, equal
19669 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61 74 to,.** or great
1966a 65 72 20 74 68 61 6e 20 70 4b 65 79 2e 20 20 52 er than pKey. R
1966b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
1966c 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a on success..**.*
1966d 2a 20 70 4b 65 79 20 69 73 20 65 69 74 68 65 72 * pKey is either
1966e 20 63 72 65 61 74 65 64 20 77 69 74 68 6f 75 74 created without
1966f 20 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20 74 a rowid or is t
19670 72 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61 74 runcated so that
19671 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 65 it.** omits the
19672 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e rowid at the en
19673 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61 74 d. The rowid at
19674 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
19675 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20 69 index entry.** i
19676 73 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65 6c s ignored as wel
19677 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73 20 l. Hence, this
19678 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6d routine only com
19679 70 61 72 65 73 20 74 68 65 20 70 72 65 66 69 78 pares the prefix
1967a 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b 65 es .** of the ke
1967b 79 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 ys prior to the
1967c 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f 74 final rowid, not
1967d 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 2e the entire key.
1967e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65 64 .**.** pUnpacked
1967f 20 6d 61 79 20 62 65 20 61 6e 20 75 6e 70 61 63 may be an unpac
19680 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 ked version of p
19681 4b 65 79 2c 6e 4b 65 79 2e 20 20 49 66 20 70 55 Key,nKey. If pU
19682 6e 70 61 63 6b 65 64 20 69 73 0a 2a 2a 20 73 75 npacked is.** su
19683 70 70 6c 69 65 64 20 69 74 20 69 73 20 75 73 65 pplied it is use
19684 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 70 4b d in place of pK
19685 65 79 2c 6e 4b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 ey,nKey..*/.SQLI
19686 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
19687 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 qlite3VdbeIdxKey
19688 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 62 65 43 Compare(. VdbeC
19689 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20 20 ursor *pC,
1968a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 /* The cu
1968b 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65 20 rsor to compare
1968c 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e 70 against */. Unp
1968d 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e ackedRecord *pUn
1968e 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70 61 packed, /* Unpa
1968f 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 cked version of
19690 70 4b 65 79 20 61 6e 64 20 6e 4b 65 79 20 2a 2f pKey and nKey */
19691 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20 20 20 . int *res
19692 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19693 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d 70 * Write the comp
19694 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20 68 65 arison result he
19695 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 20 6e re */.){. i64 n
19696 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 69 CellKey = 0;. i
19697 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72 73 6f nt rc;. BtCurso
19698 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e 70 43 r *pCur = pC->pC
19699 75 72 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d 3b 0a ursor;. Mem m;.
1969a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b . sqlite3BtreeK
1969b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 6e 43 eySize(pCur, &nC
1969c 65 6c 6c 4b 65 79 29 3b 0a 20 20 69 66 28 20 6e ellKey);. if( n
1969d 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 CellKey<=0 || nC
1969e 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66 ellKey>0x7ffffff
1969f 66 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d 20 f ){. *res =
196a0 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 0;. return SQ
196a1 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6d LITE_OK;. }. m
196a2 2e 64 62 20 3d 20 30 3b 0a 20 20 6d 2e 66 6c 61 .db = 0;. m.fla
196a3 67 73 20 3d 20 30 3b 0a 20 20 6d 2e 7a 4d 61 6c gs = 0;. m.zMal
196a4 6c 6f 63 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 loc = 0;. rc =
196a5 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 sqlite3VdbeMemFr
196a6 6f 6d 42 74 72 65 65 28 70 43 2d 3e 70 43 75 72 omBtree(pC->pCur
196a7 73 6f 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 sor, 0, (int)nCe
196a8 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 llKey, 1, &m);.
196a9 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 if( rc ){. r
196aa 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
196ab 61 73 73 65 72 74 28 20 70 55 6e 70 61 63 6b 65 assert( pUnpacke
196ac 64 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 d->flags & UNPAC
196ad 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 KED_IGNORE_ROWID
196ae 20 29 3b 0a 20 20 2a 72 65 73 20 3d 20 73 71 6c );. *res = sql
196af 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f ite3VdbeRecordCo
196b0 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c 20 mpare(m.n, m.z,
196b1 70 55 6e 70 61 63 6b 65 64 29 3b 0a 20 20 73 71 pUnpacked);. sq
196b2 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
196b3 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 ase(&m);. retur
196b4 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
196b5 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
196b6 6e 65 20 73 65 74 73 20 74 68 65 20 76 61 6c 75 ne sets the valu
196b7 65 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 e to be returned
196b8 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 by subsequent c
196b9 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 alls to.** sqlit
196ba 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 6e 20 e3_changes() on
196bb 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e the database han
196bc 64 6c 65 20 27 64 62 27 2e 20 0a 2a 2f 0a 53 51 dle 'db'. .*/.SQ
196bd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
196be 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 d sqlite3VdbeSet
196bf 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 Changes(sqlite3
196c0 2a 64 62 2c 20 69 6e 74 20 6e 43 68 61 6e 67 65 *db, int nChange
196c1 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
196c2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
196c3 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
196c4 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 6e 43 db->nChange = nC
196c5 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f hange;. db->nTo
196c6 74 61 6c 43 68 61 6e 67 65 20 2b 3d 20 6e 43 68 talChange += nCh
196c7 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 ange;.}../*.** S
196c8 65 74 20 61 20 66 6c 61 67 20 69 6e 20 74 68 65 et a flag in the
196c9 20 76 64 62 65 20 74 6f 20 75 70 64 61 74 65 20 vdbe to update
196ca 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 the change count
196cb 65 72 20 77 68 65 6e 20 69 74 20 69 73 20 66 69 er when it is fi
196cc 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f 72 20 72 65 nalised.** or re
196cd 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 set..*/.SQLITE_P
196ce 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
196cf 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e te3VdbeCountChan
196d0 67 65 73 28 56 64 62 65 20 2a 76 29 7b 0a 20 20 ges(Vdbe *v){.
196d1 76 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20 3d v->changeCntOn =
196d2 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 1;.}../*.** Mar
196d3 6b 20 65 76 65 72 79 20 70 72 65 70 61 72 65 64 k every prepared
196d4 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 6f 63 statement assoc
196d5 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74 iated with a dat
196d6 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
196d7 0a 2a 2a 20 61 73 20 65 78 70 69 72 65 64 2e 0a .** as expired..
196d8 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 69 72 65 64 **.** An expired
196d9 20 73 74 61 74 65 6d 65 6e 74 20 6d 65 61 6e 73 statement means
196da 20 74 68 61 74 20 72 65 63 6f 6d 70 69 6c 61 74 that recompilat
196db 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65 ion of the state
196dc 6d 65 6e 74 20 69 73 0a 2a 2a 20 72 65 63 6f 6d ment is.** recom
196dd 6d 65 6e 64 2e 20 20 53 74 61 74 65 6d 65 6e 74 mend. Statement
196de 73 20 65 78 70 69 72 65 20 77 68 65 6e 20 74 68 s expire when th
196df 69 6e 67 73 20 68 61 70 70 65 6e 20 74 68 61 74 ings happen that
196e0 20 6d 61 6b 65 20 74 68 65 69 72 0a 2a 2a 20 70 make their.** p
196e1 72 6f 67 72 61 6d 73 20 6f 62 73 6f 6c 65 74 65 rograms obsolete
196e2 2e 20 20 52 65 6d 6f 76 69 6e 67 20 75 73 65 72 . Removing user
196e3 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
196e4 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6e 67 0a ns or collating.
196e5 2a 2a 20 73 65 71 75 65 6e 63 65 73 2c 20 6f 72 ** sequences, or
196e6 20 63 68 61 6e 67 69 6e 67 20 61 6e 20 61 75 74 changing an aut
196e7 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 horization funct
196e8 69 6f 6e 20 61 72 65 20 74 68 65 20 74 79 70 65 ion are the type
196e9 73 20 6f 66 0a 2a 2a 20 74 68 69 6e 67 73 20 74 s of.** things t
196ea 68 61 74 20 6d 61 6b 65 20 70 72 65 70 61 72 65 hat make prepare
196eb 64 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 62 73 d statements obs
196ec 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 olete..*/.SQLITE
196ed 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
196ee 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 lite3ExpirePrepa
196ef 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71 redStatements(sq
196f0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 lite3 *db){. Vd
196f1 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28 70 20 3d be *p;. for(p =
196f2 20 64 62 2d 3e 70 56 64 62 65 3b 20 70 3b 20 70 db->pVdbe; p; p
196f3 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 =p->pNext){.
196f4 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a p->expired = 1;.
196f5 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
196f6 75 72 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 urn the database
196f7 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
196f8 20 74 68 65 20 56 64 62 65 2e 0a 2a 2f 0a 53 51 the Vdbe..*/.SQ
196f9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
196fa 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56 64 62 ite3 *sqlite3Vdb
196fb 65 44 62 28 56 64 62 65 20 2a 76 29 7b 0a 20 20 eDb(Vdbe *v){.
196fc 72 65 74 75 72 6e 20 76 2d 3e 64 62 3b 0a 7d 0a return v->db;.}.
196fd 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
196fe 20 45 6e 64 20 6f 66 20 76 64 62 65 61 75 78 2e End of vdbeaux.
196ff 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
19700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19701 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
19702 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
19703 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 Begin file vdbe
19704 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a api.c **********
19705 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19706 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
19707 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 ./*.** 2004 May
19708 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 26.**.** The aut
19709 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
1970a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
1970b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
1970c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
1970d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
1970e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
1970f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
19710 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
19711 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
19712 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
19713 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
19714 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
19715 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
19716 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
19717 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
19718 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
19719 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
1971a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1971b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1971c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1971d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1971e 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
1971f 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
19720 75 73 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 use to implement
19721 20 41 50 49 73 20 74 68 61 74 20 61 72 65 20 70 APIs that are p
19722 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 art of the.** VD
19723 42 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 BE..**.** $Id: v
19724 64 62 65 61 70 69 2e 63 2c 76 20 31 2e 31 35 30 dbeapi.c,v 1.150
19725 20 32 30 30 38 2f 31 32 2f 31 30 20 31 38 3a 30 2008/12/10 18:0
19726 33 3a 34 37 20 64 72 68 20 45 78 70 20 24 0a 2a 3:47 drh Exp $.*
19727 2f 0a 0a 23 69 66 20 30 20 26 26 20 64 65 66 69 /..#if 0 && defi
19728 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c ned(SQLITE_ENABL
19729 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d E_MEMORY_MANAGEM
1972a 45 4e 54 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 ENT)./*.** The f
1972b 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
1972c 72 65 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e re contains poin
1972d 74 65 72 73 20 74 6f 20 74 68 65 20 65 6e 64 20 ters to the end
1972e 70 6f 69 6e 74 73 20 6f 66 20 61 0a 2a 2a 20 64 points of a.** d
1972f 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 oubly-linked lis
19730 74 20 6f 66 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 t of all compile
19731 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 d SQL statements
19732 20 74 68 61 74 20 6d 61 79 20 62 65 20 68 6f 6c that may be hol
19733 64 69 6e 67 0a 2a 2a 20 62 75 66 66 65 72 73 20 ding.** buffers
19734 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 72 65 6c eligible for rel
19735 65 61 73 65 20 77 68 65 6e 20 74 68 65 20 73 71 ease when the sq
19736 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 lite3_release_me
19737 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 mory() interface
19738 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e 20 is.** invoked.
19739 41 63 63 65 73 73 20 74 6f 20 74 68 69 73 20 6c Access to this l
1973a 69 73 74 20 69 73 20 70 72 6f 74 65 63 74 65 64 ist is protected
1973b 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 4d by the SQLITE_M
1973c 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 UTEX_STATIC_LRU2
1973d 0a 2a 2a 20 6d 75 74 65 78 2e 0a 2a 2a 0a 2a 2a .** mutex..**.**
1973e 20 53 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 Statements are
1973f 61 64 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64 added to the end
19740 20 6f 66 20 74 68 69 73 20 6c 69 73 74 20 77 68 of this list wh
19741 65 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 en sqlite3_reset
19742 28 29 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e () is.** called.
19743 20 54 68 65 79 20 61 72 65 20 72 65 6d 6f 76 65 They are remove
19744 64 20 65 69 74 68 65 72 20 77 68 65 6e 20 73 71 d either when sq
19745 6c 69 74 65 33 5f 73 74 65 70 28 29 20 6f 72 20 lite3_step() or
19746 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
19747 28 29 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e ().** is called.
19748 20 57 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 73 When statements
19749 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 are added to th
1974a 69 73 20 6c 69 73 74 2c 20 74 68 65 20 61 73 73 is list, the ass
1974b 6f 63 69 61 74 65 64 20 0a 2a 2a 20 72 65 67 69 ociated .** regi
1974c 73 74 65 72 20 61 72 72 61 79 20 28 70 2d 3e 61 ster array (p->a
1974d 4d 65 6d 5b 31 2e 2e 70 2d 3e 6e 4d 65 6d 5d 29 Mem[1..p->nMem])
1974e 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 64 79 6e may contain dyn
1974f 61 6d 69 63 20 62 75 66 66 65 72 73 20 74 68 61 amic buffers tha
19750 74 0a 2a 2a 20 63 61 6e 20 62 65 20 66 72 65 65 t.** can be free
19751 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 d using sqlite3V
19752 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 dbeReleaseMemory
19753 28 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 73 ()..**.** When s
19754 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 61 64 tatements are ad
19755 64 65 64 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 ded or removed f
19756 72 6f 6d 20 74 68 69 73 20 6c 69 73 74 2c 20 74 rom this list, t
19757 68 65 20 6d 75 74 65 78 0a 2a 2a 20 61 73 73 6f he mutex.** asso
19758 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
19759 56 64 62 65 20 62 65 69 6e 67 20 61 64 64 65 64 Vdbe being added
1975a 20 6f 72 20 72 65 6d 6f 76 65 64 20 28 56 64 62 or removed (Vdb
1975b 65 2e 64 62 2d 3e 6d 75 74 65 78 29 20 69 73 0a e.db->mutex) is.
1975c 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e ** already held.
1975d 20 54 68 65 20 4c 52 55 32 20 6d 75 74 65 78 20 The LRU2 mutex
1975e 69 73 20 74 68 65 6e 20 6f 62 74 61 69 6e 65 64 is then obtained
1975f 2c 20 62 6c 6f 63 6b 69 6e 67 20 69 66 20 6e 65 , blocking if ne
19760 63 65 73 73 61 72 79 2c 0a 2a 2a 20 74 68 65 20 cessary,.** the
19761 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 70 6f 69 6e linked-list poin
19762 74 65 72 73 20 6d 61 6e 69 70 75 6c 61 74 65 64 ters manipulated
19763 20 61 6e 64 20 74 68 65 20 4c 52 55 32 20 6d 75 and the LRU2 mu
19764 74 65 78 20 72 65 6c 69 6e 71 75 69 73 68 65 64 tex relinquished
19765 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 74 61 74 ..*/.struct Stat
19766 65 6d 65 6e 74 4c 72 75 4c 69 73 74 20 7b 0a 20 ementLruList {.
19767 20 56 64 62 65 20 2a 70 46 69 72 73 74 3b 0a 20 Vdbe *pFirst;.
19768 20 56 64 62 65 20 2a 70 4c 61 73 74 3b 0a 7d 3b Vdbe *pLast;.};
19769 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 53 .static struct S
1976a 74 61 74 65 6d 65 6e 74 4c 72 75 4c 69 73 74 20 tatementLruList
1976b 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d sqlite3LruStatem
1976c 65 6e 74 73 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 ents;../*.** Che
1976d 63 6b 20 74 68 61 74 20 74 68 65 20 6c 69 73 74 ck that the list
1976e 20 6c 6f 6f 6b 73 20 74 6f 20 62 65 20 69 6e 74 looks to be int
1976f 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65 ernally consiste
19770 6e 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 nt. This is used
19771 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 61 .** as part of a
19772 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 n assert() state
19773 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a ment as follows:
19774 0a 2a 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 .**.** assert(
19775 20 73 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 stmtLruCheck()
19776 29 3b 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 );.*/.#ifndef ND
19777 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 20 EBUG.static int
19778 73 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 7b 0a stmtLruCheck(){.
19779 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 Vdbe *p;. for
1977a 28 70 3d 73 71 6c 69 74 65 33 4c 72 75 53 74 61 (p=sqlite3LruSta
1977b 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 3b 20 tements.pFirst;
1977c 70 3b 20 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 p; p=p->pLruNext
1977d 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70 2d ){. assert(p-
1977e 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 3d 3d >pLruNext || p==
1977f 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d sqlite3LruStatem
19780 65 6e 74 73 2e 70 4c 61 73 74 29 3b 0a 20 20 20 ents.pLast);.
19781 20 61 73 73 65 72 74 28 21 70 2d 3e 70 4c 72 75 assert(!p->pLru
19782 4e 65 78 74 20 7c 7c 20 70 2d 3e 70 4c 72 75 4e Next || p->pLruN
19783 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 70 ext->pLruPrev==p
19784 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70 2d );. assert(p-
19785 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 3d 3d >pLruPrev || p==
19786 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d sqlite3LruStatem
19787 65 6e 74 73 2e 70 46 69 72 73 74 29 3b 0a 20 20 ents.pFirst);.
19788 20 20 61 73 73 65 72 74 28 21 70 2d 3e 70 4c 72 assert(!p->pLr
19789 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75 uPrev || p->pLru
1978a 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d Prev->pLruNext==
1978b 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e p);. }. return
1978c 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 1;.}.#endif../*
1978d 0a 2a 2a 20 41 64 64 20 76 64 62 65 20 70 20 74 .** Add vdbe p t
1978e 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 o the end of the
1978f 20 73 74 61 74 65 6d 65 6e 74 20 6c 72 75 20 6c statement lru l
19790 69 73 74 2e 20 49 74 20 69 73 20 61 73 73 75 6d ist. It is assum
19791 65 64 20 74 68 61 74 0a 2a 2a 20 70 20 69 73 20 ed that.** p is
19792 6e 6f 74 20 61 6c 72 65 61 64 79 20 70 61 72 74 not already part
19793 20 6f 66 20 74 68 65 20 6c 69 73 74 20 77 68 65 of the list whe
19794 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 n this is called
19795 2e 20 54 68 65 20 6c 72 75 20 6c 69 73 74 0a 2a . The lru list.*
19796 2a 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 * is protected b
19797 79 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 y the SQLITE_MUT
19798 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20 6d 75 EX_STATIC_LRU mu
19799 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tex..*/.static v
1979a 6f 69 64 20 73 74 6d 74 4c 72 75 41 64 64 28 56 oid stmtLruAdd(V
1979b 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 dbe *p){. sqlit
1979c 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 e3_mutex_enter(s
1979d 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
1979e 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
1979f 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 0a 20 20 ATIC_LRU2));..
197a0 69 66 28 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 if( p->pLruPrev
197a1 7c 7c 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c || p->pLruNext |
197a2 7c 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 | sqlite3LruStat
197a3 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 3d 3d 70 ements.pFirst==p
197a4 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
197a5 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 mutex_leave(sqli
197a6 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
197a7 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
197a8 43 5f 4c 52 55 32 29 29 3b 0a 20 20 20 20 72 65 C_LRU2));. re
197a9 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 turn;. }.. ass
197aa 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63 ert( stmtLruChec
197ab 6b 28 29 20 29 3b 0a 0a 20 20 69 66 28 20 21 73 k() );.. if( !s
197ac 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 qlite3LruStateme
197ad 6e 74 73 2e 70 46 69 72 73 74 20 29 7b 0a 20 20 nts.pFirst ){.
197ae 20 20 61 73 73 65 72 74 28 20 21 73 71 6c 69 74 assert( !sqlit
197af 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e e3LruStatements.
197b0 70 4c 61 73 74 20 29 3b 0a 20 20 20 20 73 71 6c pLast );. sql
197b1 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 ite3LruStatement
197b2 73 2e 70 46 69 72 73 74 20 3d 20 70 3b 0a 20 20 s.pFirst = p;.
197b3 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 sqlite3LruStat
197b4 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20 3d 20 70 ements.pLast = p
197b5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
197b6 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 4c ssert( !sqlite3L
197b7 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 ruStatements.pLa
197b8 73 74 2d 3e 70 4c 72 75 4e 65 78 74 20 29 3b 0a st->pLruNext );.
197b9 20 20 20 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 p->pLruPrev
197ba 3d 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 = sqlite3LruStat
197bb 65 6d 65 6e 74 73 2e 70 4c 61 73 74 3b 0a 20 20 ements.pLast;.
197bc 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 sqlite3LruStat
197bd 65 6d 65 6e 74 73 2e 70 4c 61 73 74 2d 3e 70 4c ements.pLast->pL
197be 72 75 4e 65 78 74 20 3d 20 70 3b 0a 20 20 20 20 ruNext = p;.
197bf 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d sqlite3LruStatem
197c0 65 6e 74 73 2e 70 4c 61 73 74 20 3d 20 70 3b 0a ents.pLast = p;.
197c1 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 73 }.. assert( s
197c2 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b tmtLruCheck() );
197c3 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .. sqlite3_mute
197c4 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d x_leave(sqlite3M
197c5 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
197c6 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
197c7 55 32 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 U2));.}../*.** A
197c8 73 73 75 6d 69 6e 67 20 74 68 65 20 53 51 4c 49 ssuming the SQLI
197c9 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
197ca 4c 52 55 32 20 6d 75 74 65 78 74 20 69 73 20 61 LRU2 mutext is a
197cb 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 72 65 6d lready held, rem
197cc 6f 76 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ove.** statement
197cd 20 70 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 73 p from the leas
197ce 74 2d 72 65 63 65 6e 74 6c 79 2d 75 73 65 64 20 t-recently-used
197cf 73 74 61 74 65 6d 65 6e 74 20 6c 69 73 74 2e 20 statement list.
197d0 49 66 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 If the .** state
197d1 6d 65 6e 74 20 69 73 20 6e 6f 74 20 63 75 72 72 ment is not curr
197d2 65 6e 74 6c 79 20 70 61 72 74 20 6f 66 20 74 68 ently part of th
197d3 65 20 6c 69 73 74 2c 20 74 68 69 73 20 63 61 6c e list, this cal
197d4 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f l is a no-op..*/
197d5 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 6d .static void stm
197d6 74 4c 72 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 tLruRemoveNomute
197d7 78 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 x(Vdbe *p){. if
197d8 28 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c ( p->pLruPrev ||
197d9 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 p->pLruNext ||
197da 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75 53 74 61 p==sqlite3LruSta
197db 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20 29 tements.pFirst )
197dc 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74 {. assert( st
197dd 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a mtLruCheck() );.
197de 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 72 75 4e if( p->pLruN
197df 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e ext ){. p->
197e0 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50 72 pLruNext->pLruPr
197e1 65 76 20 3d 20 70 2d 3e 70 4c 72 75 50 72 65 76 ev = p->pLruPrev
197e2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
197e3 20 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 sqlite3LruSta
197e4 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20 3d 20 tements.pLast =
197e5 70 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 p->pLruPrev;.
197e6 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c }. if( p->pL
197e7 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 ruPrev ){.
197e8 70 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 p->pLruPrev->pLr
197e9 75 4e 65 78 74 20 3d 20 70 2d 3e 70 4c 72 75 4e uNext = p->pLruN
197ea 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ext;. }else{.
197eb 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c 72 75 sqlite3Lru
197ec 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 Statements.pFirs
197ed 74 20 3d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b t = p->pLruNext;
197ee 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 4c . }. p->pL
197ef 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 ruNext = 0;.
197f0 70 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b p->pLruPrev = 0;
197f1 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74 6d . assert( stm
197f2 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a 20 tLruCheck() );.
197f3 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 }.}../*.** Assu
197f4 6d 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f ming the SQLITE_
197f5 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 MUTEX_STATIC_LRU
197f6 32 20 6d 75 74 65 78 74 20 69 73 20 6e 6f 74 20 2 mutext is not
197f7 68 65 6c 64 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20 held, remove.**
197f8 73 74 61 74 65 6d 65 6e 74 20 70 20 66 72 6f 6d statement p from
197f9 20 74 68 65 20 6c 65 61 73 74 2d 72 65 63 65 6e the least-recen
197fa 74 6c 79 2d 75 73 65 64 20 73 74 61 74 65 6d 65 tly-used stateme
197fb 6e 74 20 6c 69 73 74 2e 20 49 66 20 74 68 65 20 nt list. If the
197fc 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 .** statement is
197fd 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70 not currently p
197fe 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73 74 2c art of the list,
197ff 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 this call is a
19800 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
19801 20 76 6f 69 64 20 73 74 6d 74 4c 72 75 52 65 6d void stmtLruRem
19802 6f 76 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 ove(Vdbe *p){.
19803 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
19804 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ter(sqlite3Mutex
19805 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
19806 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 EX_STATIC_LRU2))
19807 3b 0a 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 ;. stmtLruRemov
19808 65 4e 6f 6d 75 74 65 78 28 70 29 3b 0a 20 20 73 eNomutex(p);. s
19809 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1980a 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ve(sqlite3MutexA
1980b 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
1980c 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b X_STATIC_LRU2));
1980d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f .}../*.** Try to
1980e 20 72 65 6c 65 61 73 65 20 6e 20 62 79 74 65 73 release n bytes
1980f 20 6f 66 20 6d 65 6d 6f 72 79 20 62 79 20 66 72 of memory by fr
19810 65 65 69 6e 67 20 62 75 66 66 65 72 73 20 61 73 eeing buffers as
19811 73 6f 63 69 61 74 65 64 20 0a 2a 2a 20 77 69 74 sociated .** wit
19812 68 20 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 67 h the memory reg
19813 69 73 74 65 72 73 20 6f 66 20 63 75 72 72 65 6e isters of curren
19814 74 6c 79 20 75 6e 75 73 65 64 20 76 64 62 65 73 tly unused vdbes
19815 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19816 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
19817 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 dbeReleaseMemory
19818 28 69 6e 74 20 6e 29 7b 0a 20 20 56 64 62 65 20 (int n){. Vdbe
19819 2a 70 3b 0a 20 20 56 64 62 65 20 2a 70 4e 65 78 *p;. Vdbe *pNex
1981a 74 3b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d t;. int nFree =
1981b 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 0;.. sqlite3_m
1981c 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 utex_enter(sqlit
1981d 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
1981e 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
1981f 5f 4c 52 55 32 29 29 3b 0a 20 20 66 6f 72 28 70 _LRU2));. for(p
19820 3d 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 =sqlite3LruState
19821 6d 65 6e 74 73 2e 70 46 69 72 73 74 3b 20 70 20 ments.pFirst; p
19822 26 26 20 6e 46 72 65 65 3c 6e 3b 20 70 3d 70 4e && nFree<n; p=pN
19823 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 ext){. pNext
19824 3d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 0a = p->pLruNext;..
19825 20 20 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 /* For each
19826 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 statement handle
19827 20 69 6e 20 74 68 65 20 6c 72 75 20 6c 69 73 74 in the lru list
19828 2c 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 , attempt to obt
19829 61 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 ain the. ** a
1982a 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 ssociated databa
1982b 73 65 20 6d 75 74 65 78 2e 20 49 66 20 69 74 20 se mutex. If it
1982c 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e cannot be obtain
1982d 65 64 2c 20 63 6f 6e 74 69 6e 75 65 0a 20 20 20 ed, continue.
1982e 20 2a 2a 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ** to the next
1982f 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 statement handle
19830 2e 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 . It is not poss
19831 69 62 6c 65 20 74 6f 20 62 6c 6f 63 6b 20 6f 6e ible to block on
19832 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 . ** the data
19833 62 61 73 65 20 6d 75 74 65 78 20 2d 20 74 68 61 base mutex - tha
19834 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 64 65 t could cause de
19835 61 64 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 adlock.. */.
19836 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
19837 3d 3d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ==sqlite3_mutex_
19838 74 72 79 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 try(p->db->mutex
19839 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 72 65 65 ) ){. nFree
1983a 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 += sqlite3VdbeR
1983b 65 6c 65 61 73 65 42 75 66 66 65 72 73 28 70 29 eleaseBuffers(p)
1983c 3b 0a 20 20 20 20 20 20 73 74 6d 74 4c 72 75 52 ;. stmtLruR
1983d 65 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 70 29 3b emoveNomutex(p);
1983e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
1983f 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 utex_leave(p->db
19840 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a ->mutex);. }.
19841 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 }. sqlite3_mu
19842 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 tex_leave(sqlite
19843 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
19844 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
19845 4c 52 55 32 29 29 3b 0a 0a 20 20 72 65 74 75 72 LRU2));.. retur
19846 6e 20 6e 46 72 65 65 3b 0a 7d 0a 0a 2f 2a 0a 2a n nFree;.}../*.*
19847 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 * Call sqlite3Re
19848 70 72 65 70 61 72 65 28 29 20 6f 6e 20 74 68 65 prepare() on the
19849 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 6d 6f statement. Remo
1984a 76 65 20 69 74 20 66 72 6f 6d 20 74 68 65 0a 2a ve it from the.*
1984b 2a 20 6c 72 75 20 6c 69 73 74 20 62 65 66 6f 72 * lru list befor
1984c 65 20 64 6f 69 6e 67 20 73 6f 2c 20 61 73 20 52 e doing so, as R
1984d 65 70 72 65 70 61 72 65 28 29 20 77 69 6c 6c 20 eprepare() will
1984e 66 72 65 65 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 free all the.**
1984f 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20 memory register
19850 62 75 66 66 65 72 73 20 61 6e 79 77 61 79 2e 0a buffers anyway..
19851 2a 2f 0a 69 6e 74 20 76 64 62 65 52 65 70 72 65 */.int vdbeRepre
19852 70 61 72 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 pare(Vdbe *p){.
19853 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 70 stmtLruRemove(p
19854 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 );. return sqli
19855 74 65 33 52 65 70 72 65 70 61 72 65 28 70 29 3b te3Reprepare(p);
19856 0a 7d 0a 0a 23 65 6c 73 65 20 20 20 20 20 20 20 .}..#else
19857 2f 2a 20 21 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* !SQLITE_ENABL
19858 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d E_MEMORY_MANAGEM
19859 45 4e 54 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 ENT */. #define
1985a 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 78 stmtLruRemove(x
1985b 29 0a 20 20 23 64 65 66 69 6e 65 20 73 74 6d 74 ). #define stmt
1985c 4c 72 75 41 64 64 28 78 29 0a 20 20 23 64 65 66 LruAdd(x). #def
1985d 69 6e 65 20 76 64 62 65 52 65 70 72 65 70 61 72 ine vdbeReprepar
1985e 65 28 78 29 20 73 71 6c 69 74 65 33 52 65 70 72 e(x) sqlite3Repr
1985f 65 70 61 72 65 28 78 29 0a 23 65 6e 64 69 66 0a epare(x).#endif.
19860 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
19861 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 _OMIT_DEPRECATED
19862 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
19863 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 6f 66 UE (non-zero) of
19864 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73 the statement s
19865 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 upplied as an ar
19866 67 75 6d 65 6e 74 20 6e 65 65 64 73 0a 2a 2a 20 gument needs.**
19867 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 to be recompiled
19868 2e 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 6e . A statement n
19869 65 65 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6d eeds to be recom
1986a 70 69 6c 65 64 20 77 68 65 6e 65 76 65 72 20 74 piled whenever t
1986b 68 65 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 he.** execution
1986c 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e environment chan
1986d 67 65 73 20 69 6e 20 61 20 77 61 79 20 74 68 61 ges in a way tha
1986e 74 20 77 6f 75 6c 64 20 61 6c 74 65 72 20 74 68 t would alter th
1986f 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 74 68 61 e program.** tha
19870 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 t sqlite3_prepar
19871 65 28 29 20 67 65 6e 65 72 61 74 65 73 2e 20 20 e() generates.
19872 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 For example, if
19873 6e 65 77 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 new functions or
19874 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 .** collating se
19875 71 75 65 6e 63 65 73 20 61 72 65 20 72 65 67 69 quences are regi
19876 73 74 65 72 65 64 20 6f 72 20 69 66 20 61 6e 20 stered or if an
19877 61 75 74 68 6f 72 69 7a 65 72 20 66 75 6e 63 74 authorizer funct
19878 69 6f 6e 20 69 73 0a 2a 2a 20 61 64 64 65 64 20 ion is.** added
19879 6f 72 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 or changed..*/.S
1987a 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1987b 6c 69 74 65 33 5f 65 78 70 69 72 65 64 28 73 71 lite3_expired(sq
1987c 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1987d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 t){. Vdbe *p =
1987e 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 (Vdbe*)pStmt;.
1987f 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 return p==0 || p
19880 2d 3e 65 78 70 69 72 65 64 3b 0a 7d 0a 23 65 6e ->expired;.}.#en
19881 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
19882 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
19883 20 64 65 73 74 72 6f 79 73 20 61 20 76 69 72 74 destroys a virt
19884 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 61 74 ual machine that
19885 20 69 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a is created by.*
19886 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f * the sqlite3_co
19887 6d 70 69 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e mpile() routine.
19888 20 54 68 65 20 69 6e 74 65 67 65 72 20 72 65 74 The integer ret
19889 75 72 6e 65 64 20 69 73 20 61 6e 20 53 51 4c 49 urned is an SQLI
1988a 54 45 5f 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 TE_.** success/f
1988b 61 69 6c 75 72 65 20 63 6f 64 65 20 74 68 61 74 ailure code that
1988c 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 72 describes the r
1988d 65 73 75 6c 74 20 6f 66 20 65 78 65 63 75 74 69 esult of executi
1988e 6e 67 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a ng the virtual.*
1988f 2a 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a * machine..**.**
19890 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 This routine se
19891 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 ts the error cod
19892 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74 e and string ret
19893 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 urned by.** sqli
19894 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 te3_errcode(), s
19895 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 qlite3_errmsg()
19896 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d and sqlite3_errm
19897 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 sg16()..*/.SQLIT
19898 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
19899 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 3_finalize(sqlit
1989a 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
1989b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 . int rc;. if(
1989c 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 pStmt==0 ){.
1989d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1989e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 . }else{. Vd
1989f 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 be *v = (Vdbe*)p
198a0 53 74 6d 74 3b 0a 23 69 66 20 53 51 4c 49 54 45 Stmt;.#if SQLITE
198a1 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 _THREADSAFE.
198a2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
198a3 75 74 65 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 utex = v->db->mu
198a4 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 tex;.#endif.
198a5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
198a6 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 ter(mutex);.
198a7 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 76 29 stmtLruRemove(v)
198a8 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
198a9 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 e3VdbeFinalize(v
198aa 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
198ab 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 utex_leave(mutex
198ac 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
198ad 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72 rc;.}../*.** Ter
198ae 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72 72 65 minate the curre
198af 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 nt execution of
198b0 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 an SQL statement
198b1 20 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a and reset it.**
198b2 20 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74 61 back to its sta
198b3 72 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20 74 rting state so t
198b4 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 hat it can be re
198b5 75 73 65 64 2e 20 41 20 73 75 63 63 65 73 73 20 used. A success
198b6 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 code from.** the
198b7 20 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f 6e prior execution
198b8 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
198b9 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
198ba 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 sets the error
198bb 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20 code and string
198bc 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 returned by.** s
198bd 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 qlite3_errcode()
198be 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 , sqlite3_errmsg
198bf 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 () and sqlite3_e
198c0 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 rrmsg16()..*/.SQ
198c1 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
198c2 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74 ite3_reset(sqlit
198c3 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
198c4 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 . int rc;. if(
198c5 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 pStmt==0 ){.
198c6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
198c7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 . }else{. Vd
198c8 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 be *v = (Vdbe*)p
198c9 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 Stmt;. sqlite
198ca 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76 2d 3_mutex_enter(v-
198cb 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 >db->mutex);.
198cc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
198cd 65 52 65 73 65 74 28 76 29 3b 0a 20 20 20 20 73 eReset(v);. s
198ce 74 6d 74 4c 72 75 41 64 64 28 76 29 3b 0a 20 20 tmtLruAdd(v);.
198cf 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b sqlite3VdbeMak
198d0 65 52 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c eReady(v, -1, 0,
198d1 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 0, 0);. asse
198d2 72 74 28 20 28 72 63 20 26 20 28 76 2d 3e 64 62 rt( (rc & (v->db
198d3 2d 3e 65 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20 ->errMask))==rc
198d4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
198d5 75 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 utex_leave(v->db
198d6 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 ->mutex);. }.
198d7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
198d8 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 74 68 65 20 .** Set all the
198d9 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 74 68 parameters in th
198da 65 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 e compiled SQL s
198db 74 61 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c tatement to NULL
198dc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
198dd 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 int sqlite3_clea
198de 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 r_bindings(sqlit
198df 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
198e0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
198e1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
198e2 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 Vdbe *p = (Vdb
198e3 65 2a 29 70 53 74 6d 74 3b 0a 23 69 66 20 53 51 e*)pStmt;.#if SQ
198e4 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a LITE_THREADSAFE.
198e5 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
198e6 2a 6d 75 74 65 78 20 3d 20 28 28 56 64 62 65 2a *mutex = ((Vdbe*
198e7 29 70 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74 )pStmt)->db->mut
198e8 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c ex;.#endif. sql
198e9 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
198ea 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 (mutex);. for(i
198eb 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 =0; i<p->nVar; i
198ec 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
198ed 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
198ee 70 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20 p->aVar[i]);.
198ef 20 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67 p->aVar[i].flag
198f0 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
198f1 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
198f2 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a x_leave(mutex);.
198f3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
198f4 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
198f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 ************** s
198f6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a qlite3_value_ *
198f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
198f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
198f9 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
198fa 72 6f 75 74 69 6e 65 73 20 65 78 74 72 61 63 74 routines extract
198fb 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f information fro
198fc 6d 20 61 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74 m a Mem or sqlit
198fd 65 33 5f 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75 e3_value.** stru
198fe 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cture..*/.SQLITE
198ff 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
19900 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 *sqlite3_value_b
19901 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 lob(sqlite3_valu
19902 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 e *pVal){. Mem
19903 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b *p = (Mem*)pVal;
19904 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 . if( p->flags
19905 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f & (MEM_Blob|MEM_
19906 53 74 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 Str) ){. sqli
19907 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
19908 42 6c 6f 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e Blob(p);. p->
19909 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 flags &= ~MEM_St
1990a 72 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 r;. p->flags
1990b 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 |= MEM_Blob;.
1990c 20 72 65 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 return p->z;.
1990d 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
1990e 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f n sqlite3_value_
1990f 74 65 78 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a text(pVal);. }.
19910 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
19911 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
19912 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c ytes(sqlite3_val
19913 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue *pVal){. ret
19914 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 urn sqlite3Value
19915 42 79 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 Bytes(pVal, SQLI
19916 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 53 51 4c 49 TE_UTF8);.}.SQLI
19917 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
19918 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 e3_value_bytes16
19919 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
1991a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 pVal){. return
1991b 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 sqlite3ValueByte
1991c 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 s(pVal, SQLITE_U
1991d 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 TF16NATIVE);.}.S
1991e 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 QLITE_API double
1991f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 sqlite3_value_d
19920 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 ouble(sqlite3_va
19921 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 lue *pVal){. re
19922 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 turn sqlite3Vdbe
19923 52 65 61 6c 56 61 6c 75 65 28 28 4d 65 6d 2a 29 RealValue((Mem*)
19924 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f pVal);.}.SQLITE_
19925 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
19926 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 value_int(sqlite
19927 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 3_value *pVal){.
19928 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 73 71 return (int)sq
19929 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
1992a 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d e((Mem*)pVal);.}
1992b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
1992c 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 te_int64 sqlite3
1992d 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c _value_int64(sql
1992e 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c ite3_value *pVal
1992f 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
19930 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 te3VdbeIntValue(
19931 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 (Mem*)pVal);.}.S
19932 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
19933 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 unsigned char *s
19934 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
19935 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 t(sqlite3_value
19936 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e *pVal){. return
19937 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 (const unsigned
19938 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 56 char *)sqlite3V
19939 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 alueText(pVal, S
1993a 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 QLITE_UTF8);.}.#
1993b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1993c 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1993d 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1993e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1993f 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c xt16(sqlite3_val
19940 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue* pVal){. ret
19941 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 urn sqlite3Value
19942 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 Text(pVal, SQLIT
19943 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a E_UTF16NATIVE);.
19944 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e }.SQLITE_API con
19945 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
19946 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 _value_text16be(
19947 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
19948 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 Val){. return s
19949 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
1994a 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 pVal, SQLITE_UTF
1994b 31 36 42 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 16BE);.}.SQLITE_
1994c 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1994d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1994e 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 xt16le(sqlite3_v
1994f 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 alue *pVal){. r
19950 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c eturn sqlite3Val
19951 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c ueText(pVal, SQL
19952 49 54 45 5f 55 54 46 31 36 4c 45 29 3b 0a 7d 0a ITE_UTF16LE);.}.
19953 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
19954 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 _OMIT_UTF16 */.S
19955 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
19956 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
19957 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 (sqlite3_value*
19958 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 pVal){. return
19959 70 56 61 6c 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f pVal->type;.}../
1995a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1995b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c ************ sql
1995c 69 74 65 33 5f 72 65 73 75 6c 74 5f 20 20 2a 2a ite3_result_ **
1995d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1995e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
1995f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
19960 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
19961 20 62 79 20 75 73 65 72 2d 64 65 66 69 6e 65 64 by user-defined
19962 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 70 functions to sp
19963 65 63 69 66 79 0a 2a 2a 20 74 68 65 20 66 75 6e ecify.** the fun
19964 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2f ction result..*/
19965 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
19966 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
19967 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f blob(. sqlite3_
19968 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a context *pCtx, .
19969 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c const void *z,
1996a 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f . int n, . vo
1996b 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 id (*xDel)(void
1996c 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *).){. assert(
1996d 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 n>=0 );. assert
1996e 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1996f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
19970 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
19971 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
19972 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e r(&pCtx->s, z, n
19973 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 , 0, xDel);.}.SQ
19974 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
19975 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 lite3_result_dou
19976 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ble(sqlite3_cont
19977 65 78 74 20 2a 70 43 74 78 2c 20 64 6f 75 62 6c ext *pCtx, doubl
19978 65 20 72 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 e rVal){. asser
19979 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1997a 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1997b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
1997c 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 lite3VdbeMemSetD
1997d 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e 73 2c 20 ouble(&pCtx->s,
1997e 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f rVal);.}.SQLITE_
1997f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
19980 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 _result_error(sq
19981 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
19982 43 74 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Ctx, const char
19983 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 *z, int n){. as
19984 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
19985 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
19986 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
19987 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d pCtx->isError =
19988 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
19989 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1998a 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
1998b 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
1998c 38 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 8, SQLITE_TRANSI
1998d 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 ENT);.}.#ifndef
1998e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1998f 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 6.SQLITE_API voi
19990 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
19991 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 _error16(sqlite3
19992 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
19993 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 const void *z, i
19994 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 nt n){. assert(
19995 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
19996 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
19997 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78 mutex) );. pCtx
19998 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 ->isError = SQLI
19999 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69 TE_ERROR;. sqli
1999a 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
1999b 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c (&pCtx->s, z, n,
1999c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
1999d 49 56 45 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e IVE, SQLITE_TRAN
1999e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 SIENT);.}.#endif
1999f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
199a0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
199a1 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 int(sqlite3_cont
199a2 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 ext *pCtx, int i
199a3 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Val){. assert(
199a4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
199a5 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
199a6 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
199a7 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
199a8 34 28 26 70 43 74 78 2d 3e 73 2c 20 28 69 36 34 4(&pCtx->s, (i64
199a9 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 )iVal);.}.SQLITE
199aa 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
199ab 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 3_result_int64(s
199ac 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
199ad 70 43 74 78 2c 20 69 36 34 20 69 56 61 6c 29 7b pCtx, i64 iVal){
199ae 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
199af 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
199b0 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
199b1 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
199b2 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 43 eMemSetInt64(&pC
199b3 74 78 2d 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a tx->s, iVal);.}.
199b4 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
199b5 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e sqlite3_result_n
199b6 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ull(sqlite3_cont
199b7 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 ext *pCtx){. as
199b8 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
199b9 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
199ba 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
199bb 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
199bc 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 etNull(&pCtx->s)
199bd 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 ;.}.SQLITE_API v
199be 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
199bf 6c 74 5f 74 65 78 74 28 0a 20 20 73 71 6c 69 74 lt_text(. sqlit
199c0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
199c1 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
199c2 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 *z, . int n,.
199c3 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
199c4 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 d *).){. assert
199c5 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
199c6 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
199c7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
199c8 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
199c9 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e r(&pCtx->s, z, n
199ca 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 , SQLITE_UTF8, x
199cb 44 65 6c 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 Del);.}.#ifndef
199cc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
199cd 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 6.SQLITE_API voi
199ce 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
199cf 5f 74 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74 _text16(. sqlit
199d0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
199d1 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 , . const void
199d2 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 *z, . int n, .
199d3 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
199d4 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 id *).){. asser
199d5 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
199d6 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
199d7 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
199d8 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
199d9 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 tr(&pCtx->s, z,
199da 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e n, SQLITE_UTF16N
199db 41 54 49 56 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a ATIVE, xDel);.}.
199dc 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
199dd 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
199de 65 78 74 31 36 62 65 28 0a 20 20 73 71 6c 69 74 ext16be(. sqlit
199df 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
199e0 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 , . const void
199e1 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 *z, . int n, .
199e2 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
199e3 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 id *).){. asser
199e4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
199e5 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
199e6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
199e7 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
199e8 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 tr(&pCtx->s, z,
199e9 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 n, SQLITE_UTF16B
199ea 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 E, xDel);.}.SQLI
199eb 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
199ec 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 te3_result_text1
199ed 36 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6le(. sqlite3_c
199ee 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 ontext *pCtx, .
199ef 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 const void *z,
199f0 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 . int n, . voi
199f1 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a d (*xDel)(void *
199f2 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 ).){. assert( s
199f3 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
199f4 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 d(pCtx->s.db->mu
199f5 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
199f6 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 3VdbeMemSetStr(&
199f7 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 pCtx->s, z, n, S
199f8 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 78 QLITE_UTF16LE, x
199f9 44 65 6c 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f Del);.}.#endif /
199fa 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 * SQLITE_OMIT_UT
199fb 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 F16 */.SQLITE_AP
199fc 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
199fd 65 73 75 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69 esult_value(sqli
199fe 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
199ff 78 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 x, sqlite3_value
19a00 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 61 73 73 *pValue){. ass
19a01 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
19a02 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
19a03 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
19a04 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f sqlite3VdbeMemCo
19a05 70 79 28 26 70 43 74 78 2d 3e 73 2c 20 70 56 61 py(&pCtx->s, pVa
19a06 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 lue);.}.SQLITE_A
19a07 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
19a08 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 result_zeroblob(
19a09 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
19a0a 2a 70 43 74 78 2c 20 69 6e 74 20 6e 29 7b 0a 20 *pCtx, int n){.
19a0b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
19a0c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
19a0d 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
19a0e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
19a0f 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 emSetZeroBlob(&p
19a10 43 74 78 2d 3e 73 2c 20 6e 29 3b 0a 7d 0a 53 51 Ctx->s, n);.}.SQ
19a11 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
19a12 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
19a13 6f 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f or_code(sqlite3_
19a14 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 context *pCtx, i
19a15 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 70 nt errCode){. p
19a16 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 65 Ctx->isError = e
19a17 72 72 43 6f 64 65 3b 0a 7d 0a 0a 2f 2a 20 46 6f rrCode;.}../* Fo
19a18 72 63 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f rce an SQLITE_TO
19a19 4f 42 49 47 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 OBIG error. */.S
19a1a 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
19a1b 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
19a1c 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 ror_toobig(sqlit
19a1d 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
19a1e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
19a1f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19a20 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
19a21 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 x) );. pCtx->is
19a22 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 54 Error = SQLITE_T
19a23 4f 4f 42 49 47 3b 0a 20 20 73 71 6c 69 74 65 33 OOBIG;. sqlite3
19a24 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 VdbeMemSetStr(&p
19a25 43 74 78 2d 3e 73 2c 20 22 73 74 72 69 6e 67 20 Ctx->s, "string
19a26 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 or blob too big"
19a27 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 , -1, .
19a28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 SQ
19a29 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
19a2a 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a E_STATIC);.}../*
19a2b 20 41 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d An SQLITE_NOMEM
19a2c 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 error. */.SQLIT
19a2d 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
19a2e 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f e3_result_error_
19a2f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 6f nomem(sqlite3_co
19a30 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 ntext *pCtx){.
19a31 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
19a32 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
19a33 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
19a34 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
19a35 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e mSetNull(&pCtx->
19a36 73 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 s);. pCtx->isEr
19a37 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d ror = SQLITE_NOM
19a38 45 4d 3b 0a 20 20 70 43 74 78 2d 3e 73 2e 64 62 EM;. pCtx->s.db
19a39 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
19a3a 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 1;.}../*.** Exe
19a3b 63 75 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 cute the stateme
19a3c 6e 74 20 70 53 74 6d 74 2c 20 65 69 74 68 65 72 nt pStmt, either
19a3d 20 75 6e 74 69 6c 20 61 20 72 6f 77 20 6f 66 20 until a row of
19a3e 64 61 74 61 20 69 73 20 72 65 61 64 79 2c 20 74 data is ready, t
19a3f 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 he.** statement
19a40 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78 is completely ex
19a41 65 63 75 74 65 64 20 6f 72 20 61 6e 20 65 72 72 ecuted or an err
19a42 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a or occurs..**.**
19a43 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d This routine im
19a44 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 62 75 6c plements the bul
19a45 6b 20 6f 66 20 74 68 65 20 6c 6f 67 69 63 20 62 k of the logic b
19a46 65 68 69 6e 64 20 74 68 65 20 73 71 6c 69 74 65 ehind the sqlite
19a47 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 2e 20 _step().** API.
19a48 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 The only thing
19a49 6f 6d 69 74 74 65 64 20 69 73 20 74 68 65 20 61 omitted is the a
19a4a 75 74 6f 6d 61 74 69 63 20 72 65 63 6f 6d 70 69 utomatic recompi
19a4b 6c 65 20 69 66 20 61 20 0a 2a 2a 20 73 63 68 65 le if a .** sche
19a4c 6d 61 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 ma change has oc
19a4d 63 75 72 72 65 64 2e 20 20 54 68 61 74 20 64 65 curred. That de
19a4e 74 61 69 6c 20 69 73 20 68 61 6e 64 6c 65 64 20 tail is handled
19a4f 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 20 by the.** outer
19a50 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 77 sqlite3_step() w
19a51 72 61 70 70 65 72 20 70 72 6f 63 65 64 75 72 65 rapper procedure
19a52 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
19a53 73 71 6c 69 74 65 33 53 74 65 70 28 56 64 62 65 sqlite3Step(Vdbe
19a54 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 *p){. sqlite3
19a55 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a *db;. int rc;..
19a56 20 20 61 73 73 65 72 74 28 70 29 3b 0a 20 20 69 assert(p);. i
19a57 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 f( p->magic!=VDB
19a58 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20 E_MAGIC_RUN ){.
19a59 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
19a5a 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 _MISUSE;. }..
19a5b 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 6d /* Assert that m
19a5c 61 6c 6c 6f 63 28 29 20 68 61 73 20 6e 6f 74 20 alloc() has not
19a5d 66 61 69 6c 65 64 20 2a 2f 0a 20 20 64 62 20 3d failed */. db =
19a5e 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 p->db;. if( db
19a5f 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
19a60 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
19a61 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a ITE_NOMEM;. }..
19a62 20 20 69 66 28 20 70 2d 3e 70 63 3c 3d 30 20 26 if( p->pc<=0 &
19a63 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a & p->expired ){.
19a64 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 if( p->rc==S
19a65 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
19a66 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 p->rc = SQLITE
19a67 5f 53 43 48 45 4d 41 3b 0a 20 20 20 20 7d 0a 20 _SCHEMA;. }.
19a68 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
19a69 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65 RROR;. goto e
19a6a 6e 64 5f 6f 66 5f 73 74 65 70 3b 0a 20 20 7d 0a nd_of_step;. }.
19a6b 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
19a6c 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 etyOn(db) ){.
19a6d 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f p->rc = SQLITE_
19a6e 4d 49 53 55 53 45 3b 0a 20 20 20 20 72 65 74 75 MISUSE;. retu
19a6f 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 rn SQLITE_MISUSE
19a70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 ;. }. if( p->p
19a71 63 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 c<0 ){. /* If
19a72 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 74 there are no ot
19a73 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 20 63 her statements c
19a74 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 urrently running
19a75 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 72 65 , then. ** re
19a76 73 65 74 20 74 68 65 20 69 6e 74 65 72 72 75 70 set the interrup
19a77 74 20 66 6c 61 67 2e 20 20 54 68 69 73 20 70 72 t flag. This pr
19a78 65 76 65 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f events a call to
19a79 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
19a7a 70 74 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 69 pt. ** from i
19a7b 6e 74 65 72 72 75 70 74 69 6e 67 20 61 20 73 74 nterrupting a st
19a7c 61 74 65 6d 65 6e 74 20 74 68 61 74 20 68 61 73 atement that has
19a7d 20 6e 6f 74 20 79 65 74 20 73 74 61 72 74 65 64 not yet started
19a7e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
19a7f 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 db->activeVdbeC
19a80 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 nt==0 ){. d
19a81 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 b->u1.isInterrup
19a82 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a ted = 0;. }..
19a83 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
19a84 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 MIT_TRACE. if
19a85 28 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 ( db->xProfile &
19a86 26 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 & !db->init.busy
19a87 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 ){. double
19a88 20 72 4e 6f 77 3b 0a 20 20 20 20 20 20 73 71 6c rNow;. sql
19a89 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d ite3OsCurrentTim
19a8a 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f e(db->pVfs, &rNo
19a8b 77 29 3b 0a 20 20 20 20 20 20 70 2d 3e 73 74 61 w);. p->sta
19a8c 72 74 54 69 6d 65 20 3d 20 28 75 36 34 29 28 28 rtTime = (u64)((
19a8d 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 rNow - (int)rNow
19a8e 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 )*3600.0*24.0*10
19a8f 30 30 30 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 00000000.0);.
19a90 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 64 }.#endif.. d
19a91 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 b->activeVdbeCnt
19a92 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 ++;. if( p->r
19a93 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 64 62 2d eadOnly==0 ) db-
19a94 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2b 2b 3b >writeVdbeCnt++;
19a95 0a 20 20 20 20 70 2d 3e 70 63 20 3d 20 30 3b 0a . p->pc = 0;.
19a96 20 20 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 stmtLruRemov
19a97 65 28 70 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 e(p);. }.#ifnde
19a98 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 f SQLITE_OMIT_EX
19a99 50 4c 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e 65 PLAIN. if( p->e
19a9a 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 63 xplain ){. rc
19a9b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 = sqlite3VdbeLi
19a9c 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 st(p);. }else.#
19a9d 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
19a9e 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a OMIT_EXPLAIN */.
19a9f 20 20 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
19aa0 69 74 65 33 56 64 62 65 45 78 65 63 28 70 29 3b ite3VdbeExec(p);
19aa1 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 . }.. if( sqli
19aa2 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 te3SafetyOff(db)
19aa3 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
19aa4 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a ITE_MISUSE;. }.
19aa5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
19aa6 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a 20 OMIT_TRACE. /*
19aa7 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 6f 66 69 Invoke the profi
19aa8 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 le callback if t
19aa9 68 65 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a 2f here is one. */
19aaa 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
19aab 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e 78 50 72 E_ROW && db->xPr
19aac 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e ofile && !db->in
19aad 69 74 2e 62 75 73 79 20 26 26 20 70 2d 3e 6e 4f it.busy && p->nO
19aae 70 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 26 p>0. &
19aaf 26 20 70 2d 3e 61 4f 70 5b 30 5d 2e 6f 70 63 6f & p->aOp[0].opco
19ab0 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 de==OP_Trace &&
19ab1 70 2d 3e 61 4f 70 5b 30 5d 2e 70 34 2e 7a 21 3d p->aOp[0].p4.z!=
19ab2 30 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 0 ){. double
19ab3 72 4e 6f 77 3b 0a 20 20 20 20 75 36 34 20 65 6c rNow;. u64 el
19ab4 61 70 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20 73 apseTime;.. s
19ab5 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 qlite3OsCurrentT
19ab6 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 ime(db->pVfs, &r
19ab7 4e 6f 77 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 Now);. elapse
19ab8 54 69 6d 65 20 3d 20 28 75 36 34 29 28 28 72 4e Time = (u64)((rN
19ab9 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a ow - (int)rNow)*
19aba 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 3600.0*24.0*1000
19abb 30 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 65 000000.0);. e
19abc 6c 61 70 73 65 54 69 6d 65 20 2d 3d 20 70 2d 3e lapseTime -= p->
19abd 73 74 61 72 74 54 69 6d 65 3b 0a 20 20 20 20 64 startTime;. d
19abe 62 2d 3e 78 50 72 6f 66 69 6c 65 28 64 62 2d 3e b->xProfile(db->
19abf 70 50 72 6f 66 69 6c 65 41 72 67 2c 20 70 2d 3e pProfileArg, p->
19ac0 61 4f 70 5b 30 5d 2e 70 34 2e 7a 2c 20 65 6c 61 aOp[0].p4.z, ela
19ac1 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23 65 pseTime);. }.#e
19ac2 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72 43 ndif.. db->errC
19ac3 6f 64 65 20 3d 20 72 63 3b 0a 20 20 2f 2a 73 71 ode = rc;. /*sq
19ac4 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 lite3Error(p->db
19ac5 2c 20 72 63 2c 20 30 29 3b 2a 2f 0a 20 20 70 2d , rc, 0);*/. p-
19ac6 3e 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 >rc = sqlite3Api
19ac7 45 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 Exit(p->db, p->r
19ac8 63 29 3b 0a 65 6e 64 5f 6f 66 5f 73 74 65 70 3a c);.end_of_step:
19ac9 0a 20 20 61 73 73 65 72 74 28 20 28 72 63 26 30 . assert( (rc&0
19aca 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20 69 66 xff)==rc );. if
19acb 28 20 70 2d 3e 7a 53 71 6c 20 26 26 20 28 72 63 ( p->zSql && (rc
19acc 26 30 78 66 66 29 3c 53 51 4c 49 54 45 5f 52 4f &0xff)<SQLITE_RO
19acd 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 W ){. /* This
19ace 20 62 65 68 61 76 69 6f 72 20 6f 63 63 75 72 73 behavior occurs
19acf 20 69 66 20 73 71 6c 69 74 65 33 5f 70 72 65 70 if sqlite3_prep
19ad0 61 72 65 5f 76 32 28 29 20 77 61 73 20 75 73 65 are_v2() was use
19ad1 64 20 74 6f 20 62 75 69 6c 64 0a 20 20 20 20 2a d to build. *
19ad2 2a 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 * the prepared s
19ad3 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 tatement. Retur
19ad4 6e 20 65 72 72 6f 72 20 63 6f 64 65 73 20 64 69 n error codes di
19ad5 72 65 63 74 6c 79 20 2a 2f 0a 20 20 20 20 70 2d rectly */. p-
19ad6 3e 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 >db->errCode = p
19ad7 2d 3e 72 63 3b 0a 20 20 20 20 2f 2a 20 73 71 6c ->rc;. /* sql
19ad8 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c ite3Error(p->db,
19ad9 20 70 2d 3e 72 63 2c 20 30 29 3b 20 2a 2f 0a 20 p->rc, 0); */.
19ada 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 72 63 3b return p->rc;
19adb 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
19adc 20 54 68 69 73 20 69 73 20 66 6f 72 20 6c 65 67 This is for leg
19add 61 63 79 20 73 71 6c 69 74 65 33 5f 70 72 65 70 acy sqlite3_prep
19ade 61 72 65 28 29 20 62 75 69 6c 64 73 20 61 6e 64 are() builds and
19adf 20 77 68 65 6e 20 74 68 65 20 63 6f 64 65 0a 20 when the code.
19ae0 20 20 20 2a 2a 20 69 73 20 53 51 4c 49 54 45 5f ** is SQLITE_
19ae1 52 4f 57 20 6f 72 20 53 51 4c 49 54 45 5f 44 4f ROW or SQLITE_DO
19ae2 4e 45 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e NE */. return
19ae3 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a rc;. }.}../*.*
19ae4 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 74 6f * This is the to
19ae5 70 2d 6c 65 76 65 6c 20 69 6d 70 6c 65 6d 65 6e p-level implemen
19ae6 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 tation of sqlite
19ae7 33 5f 73 74 65 70 28 29 2e 20 20 43 61 6c 6c 0a 3_step(). Call.
19ae8 2a 2a 20 73 71 6c 69 74 65 33 53 74 65 70 28 29 ** sqlite3Step()
19ae9 20 74 6f 20 64 6f 20 6d 6f 73 74 20 6f 66 20 74 to do most of t
19aea 68 65 20 77 6f 72 6b 2e 20 20 49 66 20 61 20 73 he work. If a s
19aeb 63 68 65 6d 61 20 65 72 72 6f 72 20 6f 63 63 75 chema error occu
19aec 72 73 2c 0a 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 rs,.** call sqli
19aed 74 65 33 52 65 70 72 65 70 61 72 65 28 29 20 61 te3Reprepare() a
19aee 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 0a 2a 2f nd try again..*/
19aef 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
19af0 4d 49 54 5f 50 41 52 53 45 52 0a 53 51 4c 49 54 MIT_PARSER.SQLIT
19af1 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
19af2 33 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 3_step(sqlite3_s
19af3 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 tmt *pStmt){. i
19af4 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d nt rc = SQLITE_M
19af5 49 53 55 53 45 3b 0a 20 20 69 66 28 20 70 53 74 ISUSE;. if( pSt
19af6 6d 74 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a mt ){. Vdbe *
19af7 76 3b 0a 20 20 20 20 76 20 3d 20 28 56 64 62 65 v;. v = (Vdbe
19af8 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c *)pStmt;. sql
19af9 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
19afa 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (v->db->mutex);.
19afb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
19afc 53 74 65 70 28 76 29 3b 0a 20 20 20 20 73 71 6c Step(v);. sql
19afd 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
19afe 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (v->db->mutex);.
19aff 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
19b00 0a 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f .}.#else.SQLITE_
19b01 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
19b02 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d step(sqlite3_stm
19b03 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 t *pStmt){. int
19b04 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 rc = SQLITE_MIS
19b05 55 53 45 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 USE;. if( pStmt
19b06 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 ){. int cnt
19b07 3d 20 30 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 = 0;. Vdbe *v
19b08 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b = (Vdbe*)pStmt;
19b09 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 . sqlite3 *db
19b0a 20 3d 20 76 2d 3e 64 62 3b 0a 20 20 20 20 73 71 = v->db;. sq
19b0b 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
19b0c 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 r(db->mutex);.
19b0d 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73 while( (rc = s
19b0e 71 6c 69 74 65 33 53 74 65 70 28 76 29 29 3d 3d qlite3Step(v))==
19b0f 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 0a 20 20 SQLITE_SCHEMA.
19b10 20 20 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b && cnt+
19b11 2b 20 3c 20 35 0a 20 20 20 20 20 20 20 20 20 20 + < 5.
19b12 20 26 26 20 76 64 62 65 52 65 70 72 65 70 61 72 && vdbeReprepar
19b13 65 28 76 29 20 29 7b 0a 20 20 20 20 20 20 73 71 e(v) ){. sq
19b14 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d lite3_reset(pStm
19b15 74 29 3b 0a 20 20 20 20 20 20 76 2d 3e 65 78 70 t);. v->exp
19b16 69 72 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a ired = 0;. }.
19b17 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
19b18 54 45 5f 53 43 48 45 4d 41 20 26 26 20 76 2d 3e TE_SCHEMA && v->
19b19 7a 53 71 6c 20 26 26 20 64 62 2d 3e 70 45 72 72 zSql && db->pErr
19b1a 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 ){. /* Thi
19b1b 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 61 66 s case occurs af
19b1c 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f 20 72 ter failing to r
19b1d 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71 6c 20 ecompile an sql
19b1e 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 20 20 20 statement. .
19b1f 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 6d ** The error m
19b20 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65 20 essage from the
19b21 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68 61 73 SQL compiler has
19b22 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c 6f already been lo
19b23 61 64 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 69 aded . ** i
19b24 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
19b25 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 62 6c handle. This bl
19b26 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65 20 65 ock copies the e
19b27 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0a 20 20 rror message .
19b28 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ** from the
19b29 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
19b2a 69 6e 74 6f 20 74 68 65 20 73 74 61 74 65 6d 65 into the stateme
19b2b 6e 74 20 61 6e 64 20 73 65 74 73 20 74 68 65 20 nt and sets the
19b2c 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 20 20 statement.
19b2d 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 ** program count
19b2e 65 72 20 74 6f 20 30 20 74 6f 20 65 6e 73 75 72 er to 0 to ensur
19b2f 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 e that when the
19b30 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a 20 20 statement is .
19b31 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 ** finalized
19b32 20 6f 72 20 72 65 73 65 74 20 74 68 65 20 70 61 or reset the pa
19b33 72 73 65 72 20 65 72 72 6f 72 20 6d 65 73 73 61 rser error messa
19b34 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 ge is available
19b35 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c via. ** sql
19b36 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e ite3_errmsg() an
19b37 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 d sqlite3_errcod
19b38 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 e().. */.
19b39 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
19b3a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 20 63 68 zErr = (const ch
19b3b 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c ar *)sqlite3_val
19b3c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 72 72 ue_text(db->pErr
19b3d 29 3b 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ); . sqlite
19b3e 33 44 62 46 72 65 65 28 64 62 2c 20 76 2d 3e 7a 3DbFree(db, v->z
19b3f 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 69 ErrMsg);. i
19b40 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 f( !db->mallocFa
19b41 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 iled ){.
19b42 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c v->zErrMsg = sql
19b43 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c ite3DbStrDup(db,
19b44 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7d 20 zErr);. }
19b45 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 76 else {. v
19b46 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
19b47 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 53 v->rc = S
19b48 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
19b49 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 }. }. r
19b4a 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 c = sqlite3ApiEx
19b4b 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 it(db, rc);.
19b4c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
19b4d 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ave(db->mutex);.
19b4e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
19b4f 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
19b50 20 45 78 74 72 61 63 74 20 74 68 65 20 75 73 65 Extract the use
19b51 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71 r data from a sq
19b52 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74 lite3_context st
19b53 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 ructure and retu
19b54 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 rn a.** pointer
19b55 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 to it..*/.SQLITE
19b56 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
19b57 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c e3_user_data(sql
19b58 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 ite3_context *p)
19b59 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 {. assert( p &&
19b5a 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 p->pFunc );. r
19b5b 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d 3e eturn p->pFunc->
19b5c 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f 2a pUserData;.}../*
19b5d 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 .** Extract the
19b5e 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 user data from a
19b5f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
19b60 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 structure and r
19b61 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 eturn a.** point
19b62 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c er to it..*/.SQL
19b63 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 ITE_API sqlite3
19b64 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 *sqlite3_context
19b65 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 _db_handle(sqlit
19b66 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a e3_context *p){.
19b67 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 assert( p && p
19b68 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 74 ->pFunc );. ret
19b69 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a 0a urn p->s.db;.}..
19b6a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
19b6b 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c 65 ing is the imple
19b6c 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 mentation of an
19b6d 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 SQL function tha
19b6e 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69 6c t always.** fail
19b6f 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 s with an error
19b70 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e 67 20 message stating
19b71 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f that the functio
19b72 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 65 n is used in the
19b73 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 78 .** wrong contex
19b74 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f t. The sqlite3_
19b75 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f overload_functio
19b76 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63 6f n() API might co
19b77 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20 66 nstruct.** SQL f
19b78 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65 unction that use
19b79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 6f this routine so
19b7a 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 that the functi
19b7b 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a 2a ons will exist.*
19b7c 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c * for name resol
19b7d 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 63 ution but are ac
19b7e 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 65 tually overloade
19b7f 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 75 d by the xFindFu
19b80 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f 64 nction.** method
19b81 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c of virtual tabl
19b82 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
19b83 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
19b84 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f e3InvalidFunctio
19b85 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e n(. sqlite3_con
19b86 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 20 text *context,
19b87 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 /* The function
19b88 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74 20 calling context
19b89 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 */. int NotUsed
19b8a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
19b8b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 /* Number of arg
19b8c 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 uments to the fu
19b8d 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 nction */. sqli
19b8e 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 te3_value **NotU
19b8f 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65 20 sed2 /* Value
19b90 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74 of each argument
19b91 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 */.){. const c
19b92 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f 6e har *zName = con
19b93 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e 61 text->pFunc->zNa
19b94 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 me;. char *zErr
19b95 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
19b96 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e ETER2(NotUsed, N
19b97 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72 72 otUsed2);. zErr
19b98 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
19b99 66 28 30 2c 0a 20 20 20 20 20 20 22 75 6e 61 62 f(0,. "unab
19b9a 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 le to use functi
19b9b 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 71 on %s in the req
19b9c 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c uested context",
19b9d 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 zName);. sqlit
19b9e 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
19b9f 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d context, zErr, -
19ba0 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 1);. sqlite3_fr
19ba1 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a ee(zErr);.}../*.
19ba2 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72 ** Allocate or r
19ba3 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 67 eturn the aggreg
19ba4 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 ate context for
19ba5 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e a user function.
19ba6 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65 A new.** conte
19ba7 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 xt is allocated
19ba8 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c on the first cal
19ba9 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 l. Subsequent c
19baa 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a alls return the.
19bab 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20 ** same context
19bac 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 that was returne
19bad 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73 d on prior calls
19bae 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
19baf 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 void *sqlite3_ag
19bb0 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
19bb1 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
19bb2 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a *p, int nByte){.
19bb3 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 Mem *pMem;. a
19bb4 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 ssert( p && p->p
19bb5 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 Func && p->pFunc
19bb6 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73 ->xStep );. ass
19bb7 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
19bb8 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d ex_held(p->s.db-
19bb9 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 >mutex) );. pMe
19bba 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69 m = p->pMem;. i
19bbb 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 f( (pMem->flags
19bbc 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b & MEM_Agg)==0 ){
19bbd 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d . if( nByte==
19bbe 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
19bbf 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
19bc0 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a External(pMem);.
19bc1 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
19bc2 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
19bc3 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b pMem->z = 0;
19bc4 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
19bc5 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19bc6 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 Grow(pMem, nByte
19bc7 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d , 0);. pMem
19bc8 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67 ->flags = MEM_Ag
19bc9 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 g;. pMem->u
19bca 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63 .pDef = p->pFunc
19bcb 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d ;. if( pMem
19bcc 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d ->z ){. m
19bcd 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 emset(pMem->z, 0
19bce 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 , nByte);.
19bcf 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
19bd0 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d turn (void*)pMem
19bd1 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ->z;.}../*.** Re
19bd2 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72 turn the auxilar
19bd3 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 y data pointer,
19bd4 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 if any, for the
19bd5 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74 iArg'th argument
19bd6 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d to.** the user-
19bd7 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 function defined
19bd8 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c by pCtx..*/.SQL
19bd9 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
19bda 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 lite3_get_auxdat
19bdb 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 a(sqlite3_contex
19bdc 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72 t *pCtx, int iAr
19bdd 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a g){. VdbeFunc *
19bde 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 pVdbeFunc;.. as
19bdf 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
19be0 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
19be1 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
19be2 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 pVdbeFunc = pCt
19be3 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 x->pVdbeFunc;.
19be4 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c if( !pVdbeFunc |
19be5 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e | iArg>=pVdbeFun
19be6 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c c->nAux || iArg<
19be7 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
19be8 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
19be9 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 pVdbeFunc->apAux
19bea 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a [iArg].pAux;.}..
19beb 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75 /*.** Set the au
19bec 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e xilary data poin
19bed 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66 ter and delete f
19bee 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 unction, for the
19bef 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 iArg'th.** argu
19bf0 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72 ment to the user
19bf1 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 -function define
19bf2 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70 d by pCtx. Any p
19bf3 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73 revious value is
19bf4 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63 .** deleted by c
19bf5 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74 alling the delet
19bf6 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 e function speci
19bf7 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 61 73 fied when it was
19bf8 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f set..*/.SQLITE_
19bf9 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
19bfa 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20 _set_auxdata(.
19bfb 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
19bfc 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 *pCtx, . int iA
19bfd 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 rg, . void *pAu
19bfe 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 x, . void (*xDe
19bff 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a lete)(void*).){.
19c00 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 struct AuxData
19c01 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64 *pAuxData;. Vd
19c02 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e beFunc *pVdbeFun
19c03 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20 c;. if( iArg<0
19c04 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a ) goto failed;..
19c05 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
19c06 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
19c07 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
19c08 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d );. pVdbeFunc =
19c09 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 pCtx->pVdbeFunc
19c0a 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75 ;. if( !pVdbeFu
19c0b 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d nc || pVdbeFunc-
19c0c 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 >nAux<=iArg ){.
19c0d 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 int nAux = (p
19c0e 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65 VdbeFunc ? pVdbe
19c0f 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b Func->nAux : 0);
19c10 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 . int nMalloc
19c11 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75 = sizeof(VdbeFu
19c12 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 nc) + sizeof(str
19c13 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 41 72 uct AuxData)*iAr
19c14 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 g;. pVdbeFunc
19c15 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c = sqlite3DbReal
19c16 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 loc(pCtx->s.db,
19c17 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c pVdbeFunc, nMall
19c18 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 oc);. if( !pV
19c19 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 dbeFunc ){.
19c1a 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 goto failed;.
19c1b 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 }. pCtx->pV
19c1c 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46 dbeFunc = pVdbeF
19c1d 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 unc;. memset(
19c1e 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 &pVdbeFunc->apAu
19c1f 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 x[nAux], 0, size
19c20 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 61 74 of(struct AuxDat
19c21 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 a)*(iArg+1-nAux)
19c22 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 );. pVdbeFunc
19c23 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b ->nAux = iArg+1;
19c24 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e . pVdbeFunc->
19c25 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 pFunc = pCtx->pF
19c26 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 unc;. }.. pAux
19c27 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e Data = &pVdbeFun
19c28 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a c->apAux[iArg];.
19c29 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e if( pAuxData->
19c2a 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74 61 pAux && pAuxData
19c2b 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 ->xDelete ){.
19c2c 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 pAuxData->xDele
19c2d 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75 te(pAuxData->pAu
19c2e 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 x);. }. pAuxDa
19c2f 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b ta->pAux = pAux;
19c30 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 . pAuxData->xDe
19c31 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a lete = xDelete;.
19c32 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 return;..faile
19c33 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65 d:. if( xDelete
19c34 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28 ){. xDelete(
19c35 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 pAux);. }.}..#i
19c36 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
19c37 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a T_DEPRECATED./*.
19c38 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
19c39 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 mber of times th
19c3a 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 e Step function
19c3b 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20 68 of a aggregate h
19c3c 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c as been .** call
19c3d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ed..**.** This f
19c3e 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 unction is depre
19c3f 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 cated. Do not u
19c40 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f se it for new co
19c41 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 de. It is.** pr
19c42 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 ovide only to av
19c43 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 oid breaking leg
19c44 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 acy code. New a
19c45 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
19c46 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 n.** implementat
19c47 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 ions should keep
19c48 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 their own count
19c49 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 20 61 s within their a
19c4a 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 ggregate.** cont
19c4b 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ext..*/.SQLITE_A
19c4c 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 PI int sqlite3_a
19c4d 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 ggregate_count(s
19c4e 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
19c4f 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 p){. assert( p
19c50 26 26 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 && p->pFunc && p
19c51 2d 3e 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 ->pFunc->xStep )
19c52 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d ;. return p->pM
19c53 65 6d 2d 3e 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a em->n;.}.#endif.
19c54 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
19c55 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 e number of colu
19c56 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c mns in the resul
19c57 74 20 73 65 74 20 66 6f 72 20 74 68 65 20 73 74 t set for the st
19c58 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a atement pStmt..*
19c59 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
19c5a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
19c5b 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 count(sqlite3_st
19c5c 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 mt *pStmt){. Vd
19c5d 62 65 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 be *pVm = (Vdbe
19c5e 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 *)pStmt;. retur
19c5f 6e 20 70 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 n pVm ? pVm->nRe
19c60 73 43 6f 6c 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a sColumn : 0;.}..
19c61 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
19c62 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 number of value
19c63 73 20 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d s available from
19c64 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 the current row
19c65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 of the.** curre
19c66 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 ntly executing s
19c67 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a tatement pStmt..
19c68 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
19c69 74 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 t sqlite3_data_c
19c6a 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d ount(sqlite3_stm
19c6b 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 t *pStmt){. Vdb
19c6c 65 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a e *pVm = (Vdbe *
19c6d 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 )pStmt;. if( pV
19c6e 6d 3d 3d 30 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 m==0 || pVm->pRe
19c6f 73 75 6c 74 53 65 74 3d 3d 30 20 29 20 72 65 74 sultSet==0 ) ret
19c70 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 urn 0;. return
19c71 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b pVm->nResColumn;
19c72 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b .}.../*.** Check
19c73 20 74 6f 20 73 65 65 20 69 66 20 63 6f 6c 75 6d to see if colum
19c74 6e 20 69 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 n iCol of the gi
19c75 76 65 6e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 ven statement is
19c76 20 76 61 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69 valid. If.** i
19c77 74 20 69 73 2c 20 72 65 74 75 72 6e 20 61 20 70 t is, return a p
19c78 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 ointer to the Me
19c79 6d 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 m for the value
19c7a 6f 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a of that column..
19c7b 2a 2a 20 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f ** If iCol is no
19c7c 74 20 76 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 t valid, return
19c7d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d a pointer to a M
19c7e 65 6d 20 77 68 69 63 68 20 68 61 73 20 61 20 76 em which has a v
19c7f 61 6c 75 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e alue.** of NULL.
19c80 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a .*/.static Mem *
19c81 63 6f 6c 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 columnMem(sqlite
19c82 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
19c83 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 nt i){. Vdbe *p
19c84 56 6d 3b 0a 20 20 69 6e 74 20 76 61 6c 73 3b 0a Vm;. int vals;.
19c85 20 20 4d 65 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 Mem *pOut;..
19c86 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 pVm = (Vdbe *)pS
19c87 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 20 26 tmt;. if( pVm &
19c88 26 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 & pVm->pResultSe
19c89 74 21 3d 30 20 26 26 20 69 3c 70 56 6d 2d 3e 6e t!=0 && i<pVm->n
19c8a 52 65 73 43 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d ResColumn && i>=
19c8b 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
19c8c 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d _mutex_enter(pVm
19c8d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
19c8e 20 20 76 61 6c 73 20 3d 20 73 71 6c 69 74 65 33 vals = sqlite3
19c8f 5f 64 61 74 61 5f 63 6f 75 6e 74 28 70 53 74 6d _data_count(pStm
19c90 74 29 3b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 t);. pOut = &
19c91 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 5b pVm->pResultSet[
19c92 69 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 i];. }else{.
19c93 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 4d 65 static const Me
19c94 6d 20 6e 75 6c 6c 4d 65 6d 20 3d 20 7b 7b 30 7d m nullMem = {{0}
19c95 2c 20 30 2e 30 2c 20 30 2c 20 22 22 2c 20 30 2c , 0.0, 0, "", 0,
19c96 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 MEM_Null, SQLIT
19c97 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 20 E_NULL, 0, 0, 0
19c98 7d 3b 0a 20 20 20 20 69 66 28 20 70 56 6d 2d 3e };. if( pVm->
19c99 64 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 db ){. sqli
19c9a 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
19c9b 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b pVm->db->mutex);
19c9c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
19c9d 72 6f 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c ror(pVm->db, SQL
19c9e 49 54 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 ITE_RANGE, 0);.
19c9f 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 }. pOut =
19ca0 28 4d 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a (Mem*)&nullMem;.
19ca1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 }. return pOu
19ca2 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 t;.}../*.** This
19ca3 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
19ca4 6c 65 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 led after invoki
19ca5 6e 67 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 ng an sqlite3_va
19ca6 6c 75 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e lue_XXX function
19ca7 20 6f 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e on a .** column
19ca8 20 76 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 value (i.e. a v
19ca9 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
19caa 20 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 evaluating an S
19cab 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e QL expression in
19cac 20 74 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c the.** select l
19cad 69 73 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 ist of a SELECT
19cae 73 74 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 statement) that
19caf 6d 61 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c may cause a mall
19cb0 6f 63 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 oc() failure. If
19cb1 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 .** malloc() ha
19cb2 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 s failed, the th
19cb3 72 65 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c reads mallocFail
19cb4 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 ed flag is clear
19cb5 65 64 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c ed and the resul
19cb6 74 0a 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 t.** code of sta
19cb7 74 65 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 tement pStmt set
19cb8 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d to SQLITE_NOMEM
19cb9 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 ..**.** Specific
19cba 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 ally, this is ca
19cbb 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e lled from within
19cbc 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 :.**.** sqli
19cbd 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 te3_column_int()
19cbe 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f .** sqlite3_
19cbf 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a column_int64().*
19cc0 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f * sqlite3_co
19cc1 6c 75 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 lumn_text().**
19cc2 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d sqlite3_colum
19cc3 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 n_text16().**
19cc4 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e sqlite3_column
19cc5 5f 72 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 _real().** s
19cc6 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
19cc7 74 65 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c tes().** sql
19cc8 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
19cc9 73 31 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 s16().**.** But
19cca 6e 6f 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f not for sqlite3_
19ccb 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 column_blob(), w
19ccc 68 69 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 hich never calls
19ccd 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 malloc()..*/.st
19cce 61 74 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e atic void column
19ccf 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 MallocFailure(sq
19cd0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
19cd1 74 29 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c t).{. /* If mal
19cd2 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 loc() failed dur
19cd3 69 6e 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 ing an encoding
19cd4 63 6f 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69 conversion withi
19cd5 6e 20 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 n an. ** sqlite
19cd6 33 5f 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 3_column_XXX API
19cd7 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 72 , then set the r
19cd8 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 eturn code of th
19cd9 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 e statement to.
19cda 20 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d ** SQLITE_NOMEM
19cdb 2e 20 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 . The next call
19cdc 74 6f 20 5f 73 74 65 70 28 29 20 28 69 66 20 61 to _step() (if a
19cdd 6e 79 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ny) will return
19cde 53 51 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a SQLITE_ERROR. *
19cdf 2a 20 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 * and _finalize(
19ce0 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f ) will return NO
19ce1 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 MEM.. */. Vdbe
19ce2 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 *p = (Vdbe *)pS
19ce3 74 6d 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a tmt;. if( p ){.
19ce4 20 20 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 p->rc = sqli
19ce5 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 te3ApiExit(p->db
19ce6 2c 20 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71 , p->rc);. sq
19ce7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
19ce8 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
19ce9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a . }.}../*******
19cea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ceb 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f ***** sqlite3_co
19cec 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a lumn_ *********
19ced 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19cee 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ******.** The fo
19cef 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 llowing routines
19cf0 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 63 63 are used to acc
19cf1 65 73 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 ess elements of
19cf2 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a the current row.
19cf3 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 ** in the result
19cf4 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f set..*/.SQLITE_
19cf5 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
19cf6 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
19cf7 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 lob(sqlite3_stmt
19cf8 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b *pStmt, int i){
19cf9 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 . const void *v
19cfa 61 6c 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69 al;. val = sqli
19cfb 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20 te3_value_blob(
19cfc 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c columnMem(pStmt,
19cfd 69 29 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 i) );. /* Even
19cfe 74 68 6f 75 67 68 20 74 68 65 72 65 20 69 73 20 though there is
19cff 6e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 no encoding conv
19d00 65 72 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c ersion, value_bl
19d01 6f 62 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 ob() might. **
19d02 6e 65 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c need to call mal
19d03 6c 6f 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20 loc() to expand
19d04 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 the result of a
19d05 7a 65 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a zeroblob() . **
19d06 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20 expression. .
19d07 2a 2f 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f */. columnMallo
19d08 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
19d09 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d . return val;.}
19d0a 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
19d0b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
19d0c 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d ytes(sqlite3_stm
19d0d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
19d0e 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 {. int val = sq
19d0f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
19d10 73 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 s( columnMem(pSt
19d11 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
19d12 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
19d13 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
19d14 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 val;.}.SQLITE_AP
19d15 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f I int sqlite3_co
19d16 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c lumn_bytes16(sql
19d17 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
19d18 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 , int i){. int
19d19 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 val = sqlite3_va
19d1a 6c 75 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c lue_bytes16( col
19d1b 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 umnMem(pStmt,i)
19d1c 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f );. columnMallo
19d1d 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
19d1e 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d . return val;.}
19d1f 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 .SQLITE_API doub
19d20 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d le sqlite3_colum
19d21 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 n_double(sqlite3
19d22 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
19d23 74 20 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76 t i){. double v
19d24 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
19d25 75 65 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d ue_double( colum
19d26 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
19d27 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
19d28 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
19d29 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
19d2a 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
19d2b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 lite3_column_int
19d2c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
19d2d 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 Stmt, int i){.
19d2e 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 int val = sqlite
19d2f 33 5f 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 3_value_int( col
19d30 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 umnMem(pStmt,i)
19d31 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f );. columnMallo
19d32 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
19d33 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d . return val;.}
19d34 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
19d35 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 te_int64 sqlite3
19d36 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 _column_int64(sq
19d37 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
19d38 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c t, int i){. sql
19d39 69 74 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 ite_int64 val =
19d3a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e sqlite3_value_in
19d3b 74 36 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 t64( columnMem(p
19d3c 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c Stmt,i) );. col
19d3d 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 umnMallocFailure
19d3e 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 (pStmt);. retur
19d3f 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f n val;.}.SQLITE_
19d40 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e API const unsign
19d41 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 ed char *sqlite3
19d42 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c _column_text(sql
19d43 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
19d44 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 , int i){. cons
19d45 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
19d46 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 *val = sqlite3_v
19d47 61 6c 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d alue_text( colum
19d48 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
19d49 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
19d4a 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
19d4b 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
19d4c 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
19d4d 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 3_value *sqlite3
19d4e 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 _column_value(sq
19d4f 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
19d50 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d t, int i){. Mem
19d51 20 2a 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d *pOut = columnM
19d52 65 6d 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 em(pStmt, i);.
19d53 69 66 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26 if( pOut->flags&
19d54 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 MEM_Static ){.
19d55 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d pOut->flags &=
19d56 20 7e 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 ~MEM_Static;.
19d57 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d pOut->flags |=
19d58 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a MEM_Ephem;. }.
19d59 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 columnMallocFa
19d5a 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 ilure(pStmt);.
19d5b 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f return (sqlite3_
19d5c 76 61 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a value *)pOut;.}.
19d5d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
19d5e 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 MIT_UTF16.SQLITE
19d5f 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
19d60 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
19d61 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 text16(sqlite3_s
19d62 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
19d63 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 i){. const void
19d64 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f *val = sqlite3_
19d65 76 61 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f value_text16( co
19d66 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 lumnMem(pStmt,i)
19d67 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c );. columnMall
19d68 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 ocFailure(pStmt)
19d69 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a ;. return val;.
19d6a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
19d6b 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
19d6c 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
19d6d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
19d6e 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ype(sqlite3_stmt
19d6f 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b *pStmt, int i){
19d70 0a 20 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 . int iType = s
19d71 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
19d72 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 e( columnMem(pSt
19d73 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
19d74 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
19d75 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
19d76 69 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 iType;.}../* The
19d77 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 following funct
19d78 69 6f 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e ion is experimen
19d79 74 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 tal and subject
19d7a 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 to change or.**
19d7b 72 65 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 removal */./*int
19d7c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
19d7d 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c numeric_type(sql
19d7e 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
19d7f 2c 20 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65 , int i){.** re
19d80 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c turn sqlite3_val
19d81 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 ue_numeric_type(
19d82 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
19d83 2c 69 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f ,i) );.**}.*/../
19d84 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 *.** Convert the
19d85 20 4e 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 N-th element of
19d86 20 70 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 pStmt->pColName
19d87 5b 5d 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 [] into a string
19d88 20 75 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 using.** xFunc(
19d89 29 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 ) then return th
19d8a 61 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e at string. If N
19d8b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 is out of range
19d8c 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a , return 0..**.*
19d8d 2a 20 54 68 65 72 65 20 61 72 65 20 75 70 20 74 * There are up t
19d8e 6f 20 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 o 5 names for ea
19d8f 63 68 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 ch column. useT
19d90 79 70 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 ype determines w
19d91 68 69 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 hich.** name is
19d92 72 65 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20 returned. Here
19d93 61 72 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a are the names:.*
19d94 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54 *.** 0 T
19d95 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 he column name a
19d96 73 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64 s it should be d
19d97 69 73 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74 isplayed for out
19d98 70 75 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20 put.** 1
19d99 20 54 68 65 20 64 61 74 61 74 79 70 65 20 6e 61 The datatype na
19d9a 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d me for the colum
19d9b 6e 0a 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54 n.** 2 T
19d9c 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 he name of the d
19d9d 61 74 61 62 61 73 65 20 74 68 61 74 20 74 68 65 atabase that the
19d9e 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 column derives
19d9f 66 72 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20 from.** 3
19da0 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 The name of th
19da1 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 e table that the
19da2 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 column derives
19da3 66 72 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20 from.** 4
19da4 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 The name of th
19da5 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 e table column t
19da6 68 61 74 20 74 68 65 20 72 65 73 75 6c 74 20 63 hat the result c
19da7 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 olumn derives fr
19da8 6f 6d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 om.**.** If the
19da9 72 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20 result is not a
19daa 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 simple column re
19dab 66 65 72 65 6e 63 65 20 28 69 66 20 69 74 20 69 ference (if it i
19dac 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a s an expression.
19dad 2a 2a 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74 ** or a constant
19dae 29 20 74 68 65 6e 20 75 73 65 54 79 70 65 73 20 ) then useTypes
19daf 32 2c 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75 2, 3, and 4 retu
19db0 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 rn NULL..*/.stat
19db1 69 63 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63 ic const void *c
19db2 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c olumnName(. sql
19db3 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
19db4 2c 0a 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e ,. int N,. con
19db5 73 74 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 st void *(*xFunc
19db6 29 28 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 )(Mem*),. int u
19db7 73 65 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 seType.){. cons
19db8 74 20 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b t void *ret = 0;
19db9 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 . Vdbe *p = (Vd
19dba 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e be *)pStmt;. in
19dbb 74 20 6e 3b 0a 20 20 0a 0a 20 20 69 66 28 20 70 t n;. .. if( p
19dbc 21 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 !=0 ){. n = s
19dbd 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f qlite3_column_co
19dbe 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 unt(pStmt);.
19dbf 69 66 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20 if( N<n && N>=0
19dc0 29 7b 0a 20 20 20 20 20 20 4e 20 2b 3d 20 75 73 ){. N += us
19dc1 65 54 79 70 65 2a 6e 3b 0a 20 20 20 20 20 20 73 eType*n;. s
19dc2 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
19dc3 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 er(p->db->mutex)
19dc4 3b 0a 20 20 20 20 20 20 72 65 74 20 3d 20 78 46 ;. ret = xF
19dc5 75 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 unc(&p->aColName
19dc6 5b 4e 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 [N]);.. /*
19dc7 41 20 6d 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 A malloc may hav
19dc8 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 e failed inside
19dc9 6f 66 20 74 68 65 20 78 46 75 6e 63 28 29 20 63 of the xFunc() c
19dca 61 6c 6c 2e 20 49 66 20 74 68 69 73 0a 20 20 20 all. If this.
19dcb 20 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61 73 ** is the cas
19dcc 65 2c 20 63 6c 65 61 72 20 74 68 65 20 6d 61 6c e, clear the mal
19dcd 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 61 locFailed flag a
19dce 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a nd return NULL..
19dcf 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
19dd0 66 28 20 70 2d 3e 64 62 20 26 26 20 70 2d 3e 64 f( p->db && p->d
19dd1 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
19dd2 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 64 62 ){. p->db
19dd3 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
19dd4 20 30 3b 0a 20 20 20 20 20 20 20 20 72 65 74 20 0;. ret
19dd5 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
19dd6 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
19dd7 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 _leave(p->db->mu
19dd8 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a tex);. }. }.
19dd9 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a return ret;.}.
19dda 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
19ddb 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 4e 74 e name of the Nt
19ddc 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 h column of the
19ddd 72 65 73 75 6c 74 20 73 65 74 20 72 65 74 75 72 result set retur
19dde 6e 65 64 20 62 79 20 53 51 4c 0a 2a 2a 20 73 74 ned by SQL.** st
19ddf 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a atement pStmt..*
19de0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
19de1 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
19de2 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c _column_name(sql
19de3 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
19de4 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
19de5 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
19de6 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
19de7 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
19de8 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
19de9 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 ue_text, COLNAME
19dea 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 _NAME);.}.#ifnde
19deb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
19dec 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 F16.SQLITE_API c
19ded 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
19dee 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 e3_column_name16
19def 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
19df0 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 Stmt, int N){.
19df1 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d return columnNam
19df2 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 e(. pStmt,
19df3 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 N, (const void*(
19df4 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 *)(Mem*))sqlite3
19df5 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 _value_text16, C
19df6 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a OLNAME_NAME);.}.
19df7 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f #endif../*.** Co
19df8 6e 73 74 72 61 69 6e 74 3a 20 20 49 66 20 79 6f nstraint: If yo
19df9 75 20 68 61 76 65 20 45 4e 41 42 4c 45 5f 43 4f u have ENABLE_CO
19dfa 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 74 68 LUMN_METADATA th
19dfb 65 6e 20 79 6f 75 20 6d 75 73 74 0a 2a 2a 20 6e en you must.** n
19dfc 6f 74 20 64 65 66 69 6e 65 20 4f 4d 49 54 5f 44 ot define OMIT_D
19dfd 45 43 4c 54 59 50 45 2e 0a 2a 2f 0a 23 69 66 20 ECLTYPE..*/.#if
19dfe 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
19dff 4d 49 54 5f 44 45 43 4c 54 59 50 45 29 20 26 26 MIT_DECLTYPE) &&
19e00 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
19e01 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 ENABLE_COLUMN_ME
19e02 54 41 44 41 54 41 29 0a 23 20 65 72 72 6f 72 20 TADATA).# error
19e03 22 4d 75 73 74 20 6e 6f 74 20 64 65 66 69 6e 65 "Must not define
19e04 20 62 6f 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 both SQLITE_OMI
19e05 54 5f 44 45 43 4c 54 59 50 45 20 5c 0a 20 20 20 T_DECLTYPE \.
19e06 20 20 20 20 20 20 61 6e 64 20 53 51 4c 49 54 45 and SQLITE
19e07 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d _ENABLE_COLUMN_M
19e08 45 54 41 44 41 54 41 22 0a 23 65 6e 64 69 66 0a ETADATA".#endif.
19e09 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
19e0a 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a 2f 2a OMIT_DECLTYPE./*
19e0b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 .** Return the c
19e0c 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f olumn declaratio
19e0d 6e 20 74 79 70 65 20 28 69 66 20 61 70 70 6c 69 n type (if appli
19e0e 63 61 62 6c 65 29 20 6f 66 20 74 68 65 20 27 69 cable) of the 'i
19e0f 27 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 'th column.** of
19e10 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
19e11 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 of SQL statement
19e12 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 pStmt..*/.SQLIT
19e13 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
19e14 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
19e15 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65 _decltype(sqlite
19e16 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
19e17 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt N){. return
19e18 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 columnName(.
19e19 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e pStmt, N, (con
19e1a 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a st void*(*)(Mem*
19e1b 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ))sqlite3_value_
19e1c 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 text, COLNAME_DE
19e1d 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 69 66 6e 64 CLTYPE);.}.#ifnd
19e1e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
19e1f 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 TF16.SQLITE_API
19e20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
19e21 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 te3_column_declt
19e22 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ype16(sqlite3_st
19e23 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e mt *pStmt, int N
19e24 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 ){. return colu
19e25 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 mnName(. pS
19e26 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 tmt, N, (const v
19e27 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 oid*(*)(Mem*))sq
19e28 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
19e29 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 16, COLNAME_DECL
19e2a 54 59 50 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 TYPE);.}.#endif
19e2b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 /* SQLITE_OMIT_U
19e2c 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f TF16 */.#endif /
19e2d 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 * SQLITE_OMIT_DE
19e2e 43 4c 54 59 50 45 20 2a 2f 0a 0a 23 69 66 64 65 CLTYPE */..#ifde
19e2f 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
19e30 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a COLUMN_METADATA.
19e31 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
19e32 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
19e33 61 62 61 73 65 20 66 72 6f 6d 20 77 68 69 63 68 abase from which
19e34 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e a result column
19e35 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c derives..** NUL
19e36 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 L is returned if
19e37 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 the result colu
19e38 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 mn is an express
19e39 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 ion or constant
19e3a 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 or.** anything e
19e3b 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 lse which is not
19e3c 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 an unabiguous r
19e3d 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 eference to a da
19e3e 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a tabase column..*
19e3f 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
19e40 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
19e41 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 _column_database
19e42 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
19e43 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e mt *pStmt, int N
19e44 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 ){. return colu
19e45 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 mnName(. pS
19e46 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 tmt, N, (const v
19e47 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 oid*(*)(Mem*))sq
19e48 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
19e49 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 , COLNAME_DATABA
19e4a 53 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 SE);.}.#ifndef S
19e4b 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
19e4c 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
19e4d 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
19e4e 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f column_database_
19e4f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 name16(sqlite3_s
19e50 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
19e51 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c N){. return col
19e52 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 umnName(. p
19e53 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 Stmt, N, (const
19e54 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 void*(*)(Mem*))s
19e55 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
19e56 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 t16, COLNAME_DAT
19e57 41 42 41 53 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 ABASE);.}.#endif
19e58 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
19e59 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 UTF16 */../*.**
19e5a 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 Return the name
19e5b 6f 66 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f of the table fro
19e5c 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 m which a result
19e5d 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e column derives.
19e5e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 .** NULL is retu
19e5f 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75 rned if the resu
19e60 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 lt column is an
19e61 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f expression or co
19e62 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 nstant or.** any
19e63 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 thing else which
19e64 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 is not an unabi
19e65 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 guous reference
19e66 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f to a database co
19e67 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lumn..*/.SQLITE_
19e68 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
19e69 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
19e6a 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 able_name(sqlite
19e6b 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
19e6c 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt N){. return
19e6d 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 columnName(.
19e6e 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e pStmt, N, (con
19e6f 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a st void*(*)(Mem*
19e70 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ))sqlite3_value_
19e71 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 text, COLNAME_TA
19e72 42 4c 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 BLE);.}.#ifndef
19e73 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
19e74 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 6.SQLITE_API con
19e75 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
19e76 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 _column_table_na
19e77 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d me16(sqlite3_stm
19e78 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 t *pStmt, int N)
19e79 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d {. return colum
19e7a 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 nName(. pSt
19e7b 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f mt, N, (const vo
19e7c 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c id*(*)(Mem*))sql
19e7d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
19e7e 36 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 6, COLNAME_TABLE
19e7f 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 );.}.#endif /* S
19e80 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
19e81 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 */../*.** Retur
19e82 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 n the name of th
19e83 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66 e table column f
19e84 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 rom which a resu
19e85 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 lt column derive
19e86 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 s..** NULL is re
19e87 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 turned if the re
19e88 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 sult column is a
19e89 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 n expression or
19e8a 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 constant or.** a
19e8b 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 nything else whi
19e8c 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 ch is not an una
19e8d 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 biguous referenc
19e8e 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 e to a database
19e8f 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 column..*/.SQLIT
19e90 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
19e91 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
19e92 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c _origin_name(sql
19e93 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
19e94 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
19e95 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
19e96 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
19e97 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
19e98 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
19e99 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 ue_text, COLNAME
19e9a 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66 6e _COLUMN);.}.#ifn
19e9b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
19e9c 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 UTF16.SQLITE_API
19e9d 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
19e9e 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 ite3_column_orig
19e9f 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 in_name16(sqlite
19ea0 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
19ea1 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt N){. return
19ea2 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 columnName(.
19ea3 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e pStmt, N, (con
19ea4 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a st void*(*)(Mem*
19ea5 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ))sqlite3_value_
19ea6 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f text16, COLNAME_
19ea7 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 6e 64 69 COLUMN);.}.#endi
19ea8 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
19ea9 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 _UTF16 */.#endif
19eaa 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
19eab 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 E_COLUMN_METADAT
19eac 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a A */.../********
19ead 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19eae 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f ******* sqlite3_
19eaf 62 69 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a bind_ *********
19eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19eb1 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 74 69 6e **.** .** Routin
19eb2 65 73 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 es used to attac
19eb3 68 20 76 61 6c 75 65 73 20 74 6f 20 77 69 6c 64 h values to wild
19eb4 63 61 72 64 73 20 69 6e 20 61 20 63 6f 6d 70 69 cards in a compi
19eb5 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e led SQL statemen
19eb6 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e 62 69 t..*/./*.** Unbi
19eb7 6e 64 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75 nd the value bou
19eb8 6e 64 20 74 6f 20 76 61 72 69 61 62 6c 65 20 69 nd to variable i
19eb9 20 69 6e 20 76 69 72 74 75 61 6c 20 6d 61 63 68 in virtual mach
19eba 69 6e 65 20 70 2e 20 54 68 69 73 20 69 73 20 74 ine p. This is t
19ebb 68 65 20 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 he .** the same
19ebc 61 73 20 62 69 6e 64 69 6e 67 20 61 20 4e 55 4c as binding a NUL
19ebd 4c 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 63 L value to the c
19ebe 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 20 22 69 olumn. If the "i
19ebf 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a " parameter is.*
19ec0 2a 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 * out of range,
19ec1 74 68 65 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 then SQLITE_RANG
19ec2 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f E is returned. O
19ec3 74 68 65 77 69 73 65 20 53 51 4c 49 54 45 5f 4f thewise SQLITE_O
19ec4 4b 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 75 63 63 65 K..**.** A succe
19ec5 73 73 66 75 6c 20 65 76 61 6c 75 61 74 69 6f 6e ssful evaluation
19ec6 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
19ec7 20 61 63 71 75 69 72 65 73 20 74 68 65 20 6d 75 acquires the mu
19ec8 74 65 78 20 6f 6e 20 70 2e 0a 2a 2a 20 74 68 65 tex on p..** the
19ec9 20 6d 75 74 65 78 20 69 73 20 72 65 6c 65 61 73 mutex is releas
19eca 65 64 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f ed if any kind o
19ecb 66 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a f error occurs..
19ecc 2a 2a 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 **.** The error
19ecd 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e 20 64 code stored in d
19ece 61 74 61 62 61 73 65 20 70 2d 3e 64 62 20 69 73 atabase p->db is
19ecf 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 overwritten wit
19ed0 68 20 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 h the return.**
19ed1 76 61 6c 75 65 20 69 6e 20 61 6e 79 20 63 61 73 value in any cas
19ed2 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
19ed3 20 76 64 62 65 55 6e 62 69 6e 64 28 56 64 62 65 vdbeUnbind(Vdbe
19ed4 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d *p, int i){. M
19ed5 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 66 28 20 em *pVar;. if(
19ed6 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 p==0 ) return SQ
19ed7 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 73 LITE_MISUSE;. s
19ed8 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
19ed9 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 er(p->db->mutex)
19eda 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 ;. if( p->magic
19edb 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e !=VDBE_MAGIC_RUN
19edc 20 7c 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a || p->pc>=0 ){.
19edd 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
19ede 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4d (p->db, SQLITE_M
19edf 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 73 ISUSE, 0);. s
19ee0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
19ee1 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
19ee2 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
19ee3 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a ITE_MISUSE;. }.
19ee4 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 70 if( i<1 || i>p
19ee5 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 73 71 ->nVar ){. sq
19ee6 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 lite3Error(p->db
19ee7 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 , SQLITE_RANGE,
19ee8 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 0);. sqlite3_
19ee9 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 mutex_leave(p->d
19eea 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 b->mutex);. r
19eeb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 41 4e eturn SQLITE_RAN
19eec 47 45 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b 0a 20 GE;. }. i--;.
19eed 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 pVar = &p->aVar
19eee 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 [i];. sqlite3Vd
19eef 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 56 61 beMemRelease(pVa
19ef0 72 29 3b 0a 20 20 70 56 61 72 2d 3e 66 6c 61 67 r);. pVar->flag
19ef1 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
19ef2 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e sqlite3Error(p->
19ef3 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 db, SQLITE_OK, 0
19ef4 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
19ef5 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
19ef6 42 69 6e 64 20 61 20 74 65 78 74 20 6f 72 20 42 Bind a text or B
19ef7 4c 4f 42 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 LOB value..*/.st
19ef8 61 74 69 63 20 69 6e 74 20 62 69 6e 64 54 65 78 atic int bindTex
19ef9 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d t(. sqlite3_stm
19efa 74 20 2a 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 t *pStmt, /* T
19efb 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 he statement to
19efc 62 69 6e 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a bind against */.
19efd 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 int i,
19efe 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
19eff 78 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 x of the paramet
19f00 65 72 20 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 er to bind */.
19f01 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 const void *zDat
19f02 61 2c 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 a, /* Pointe
19f03 72 20 74 6f 20 74 68 65 20 64 61 74 61 20 74 6f r to the data to
19f04 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 be bound */. i
19f05 6e 74 20 6e 44 61 74 61 2c 20 20 20 20 20 20 20 nt nData,
19f06 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
19f07 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 of bytes of data
19f08 20 74 6f 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a to be bound */.
19f09 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
19f0a 6f 69 64 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 oid*), /* Dest
19f0b 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 64 ructor for the d
19f0c 61 74 61 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f ata */. u8 enco
19f0d 64 69 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 ding
19f0e 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 /* Encoding for
19f0f 74 68 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 the data */.){.
19f10 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
19f11 20 2a 29 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 *)pStmt;. Mem
19f12 2a 70 56 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b *pVar;. int rc;
19f13 0a 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 .. rc = vdbeUnb
19f14 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 ind(p, i);. if(
19f15 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
19f16 7b 0a 20 20 20 20 69 66 28 20 7a 44 61 74 61 21 {. if( zData!
19f17 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 56 61 72 =0 ){. pVar
19f18 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d = &p->aVar[i-1]
19f19 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
19f1a 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
19f1b 72 28 70 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e r(pVar, zData, n
19f1c 44 61 74 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 Data, encoding,
19f1d 78 44 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 xDel);. if(
19f1e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
19f1f 26 20 65 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b & encoding!=0 ){
19f20 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
19f21 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
19f22 6e 63 6f 64 69 6e 67 28 70 56 61 72 2c 20 45 4e ncoding(pVar, EN
19f23 43 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 C(p->db));.
19f24 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
19f25 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c Error(p->db, rc,
19f26 20 30 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 0);. rc =
19f27 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 sqlite3ApiExit(p
19f28 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d ->db, rc);. }
19f29 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
19f2a 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e ex_leave(p->db->
19f2b 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 mutex);. }. re
19f2c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
19f2d 2a 2a 20 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 ** Bind a blob v
19f2e 61 6c 75 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 alue to an SQL s
19f2f 74 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c tatement variabl
19f30 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
19f31 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
19f32 64 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 d_blob(. sqlite
19f33 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 3_stmt *pStmt, .
19f34 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 int i, . cons
19f35 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a t void *zData, .
19f36 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 int nData, .
19f37 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
19f38 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 d*).){. return
19f39 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 bindText(pStmt,
19f3a 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c i, zData, nData,
19f3b 20 78 44 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c xDel, 0);.}.SQL
19f3c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
19f3d 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 te3_bind_double(
19f3e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
19f3f 74 6d 74 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 tmt, int i, doub
19f40 6c 65 20 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e le rValue){. in
19f41 74 20 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 t rc;. Vdbe *p
19f42 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b = (Vdbe *)pStmt;
19f43 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 . rc = vdbeUnbi
19f44 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 nd(p, i);. if(
19f45 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
19f46 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
19f47 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 2d MemSetDouble(&p-
19f48 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c >aVar[i-1], rVal
19f49 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ue);. sqlite3
19f4a 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
19f4b 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a db->mutex);. }.
19f4c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 return rc;.}.S
19f4d 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
19f4e 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 lite3_bind_int(s
19f4f 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 qlite3_stmt *p,
19f50 69 6e 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 int i, int iValu
19f51 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c e){. return sql
19f52 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 ite3_bind_int64(
19f53 70 2c 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75 p, i, (i64)iValu
19f54 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 e);.}.SQLITE_API
19f55 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
19f56 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f d_int64(sqlite3_
19f57 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
19f58 20 69 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 i, sqlite_int64
19f59 20 69 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 iValue){. int
19f5a 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 rc;. Vdbe *p =
19f5b 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe *)pStmt;.
19f5c 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 rc = vdbeUnbind
19f5d 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 (p, i);. if( rc
19f5e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
19f5f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19f60 6d 53 65 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 mSetInt64(&p->aV
19f61 61 72 5b 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 ar[i-1], iValue)
19f62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
19f63 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
19f64 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 >mutex);. }. r
19f65 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 eturn rc;.}.SQLI
19f66 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
19f67 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c e3_bind_null(sql
19f68 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
19f69 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 , int i){. int
19f6a 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 rc;. Vdbe *p =
19f6b 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 (Vdbe*)pStmt;.
19f6c 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 rc = vdbeUnbind(
19f6d 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d p, i);. if( rc=
19f6e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
19f6f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
19f70 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 leave(p->db->mut
19f71 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ex);. }. retur
19f72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 n rc;.}.SQLITE_A
19f73 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
19f74 69 6e 64 5f 74 65 78 74 28 20 0a 20 20 73 71 6c ind_text( . sql
19f75 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
19f76 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 , . int i, . c
19f77 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 onst char *zData
19f78 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 , . int nData,
19f79 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
19f7a 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 void*).){. retu
19f7b 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d rn bindText(pStm
19f7c 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 t, i, zData, nDa
19f7d 74 61 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 ta, xDel, SQLITE
19f7e 5f 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 _UTF8);.}.#ifnde
19f7f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
19f80 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 F16.SQLITE_API i
19f81 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
19f82 74 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65 text16(. sqlite
19f83 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 3_stmt *pStmt, .
19f84 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 int i, . cons
19f85 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a t void *zData, .
19f86 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 int nData, .
19f87 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
19f88 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 d*).){. return
19f89 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 bindText(pStmt,
19f8a 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c i, zData, nData,
19f8b 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 xDel, SQLITE_UT
19f8c 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 F16NATIVE);.}.#e
19f8d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
19f8e 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c MIT_UTF16 */.SQL
19f8f 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
19f90 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 te3_bind_value(s
19f91 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
19f92 6d 74 2c 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 mt, int i, const
19f93 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
19f94 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 pValue){. int r
19f95 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 c;. Vdbe *p = (
19f96 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
19f97 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 rc = vdbeUnbind(
19f98 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d p, i);. if( rc=
19f99 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
19f9a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
19f9b 62 65 4d 65 6d 43 6f 70 79 28 26 70 2d 3e 61 56 beMemCopy(&p->aV
19f9c 61 72 5b 69 2d 31 5d 2c 20 70 56 61 6c 75 65 29 ar[i-1], pValue)
19f9d 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
19f9e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
19f9f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
19fa0 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
19fa1 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 45 &p->aVar[i-1], E
19fa2 4e 43 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 NC(p->db));.
19fa3 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 }. sqlite3_mu
19fa4 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
19fa5 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 >mutex);. }. r
19fa6 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 c = sqlite3ApiEx
19fa7 69 74 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 it(p->db, rc);.
19fa8 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 return rc;.}.SQ
19fa9 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
19faa 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c ite3_bind_zerobl
19fab 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ob(sqlite3_stmt
19fac 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 69 *pStmt, int i, i
19fad 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b nt n){. int rc;
19fae 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 . Vdbe *p = (Vd
19faf 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 be *)pStmt;. rc
19fb0 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c = vdbeUnbind(p,
19fb1 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 i);. if( rc==S
19fb2 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
19fb3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
19fb4 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 tZeroBlob(&p->aV
19fb5 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20 ar[i-1], n);.
19fb6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
19fb7 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 eave(p->db->mute
19fb8 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e x);. }. return
19fb9 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
19fba 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
19fbb 6f 66 20 77 69 6c 64 63 61 72 64 73 20 74 68 61 of wildcards tha
19fbc 74 20 63 61 6e 20 62 65 20 70 6f 74 65 6e 74 69 t can be potenti
19fbd 61 6c 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a ally bound to..*
19fbe 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
19fbf 73 20 61 64 64 65 64 20 74 6f 20 73 75 70 70 6f s added to suppo
19fc0 72 74 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20 rt DBD::SQLite.
19fc1 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 .*/.SQLITE_API
19fc2 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
19fc3 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 _parameter_count
19fc4 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
19fc5 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 Stmt){. Vdbe *p
19fc6 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b = (Vdbe*)pStmt;
19fc7 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70 2d . return p ? p-
19fc8 3e 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a >nVar : 0;.}../*
19fc9 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6d 61 70 .** Create a map
19fca 70 69 6e 67 20 66 72 6f 6d 20 76 61 72 69 61 62 ping from variab
19fcb 6c 65 20 6e 75 6d 62 65 72 73 20 74 6f 20 76 61 le numbers to va
19fcc 72 69 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 riable names.**
19fcd 69 6e 20 74 68 65 20 56 64 62 65 2e 61 7a 56 61 in the Vdbe.azVa
19fce 72 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 73 75 r[] array, if su
19fcf 63 68 20 61 20 6d 61 70 70 69 6e 67 20 64 6f 65 ch a mapping doe
19fd0 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a s not already.**
19fd1 20 65 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 exist..*/.stati
19fd2 63 20 76 6f 69 64 20 63 72 65 61 74 65 56 61 72 c void createVar
19fd3 4d 61 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 Map(Vdbe *p){.
19fd4 69 66 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29 7b if( !p->okVar ){
19fd5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
19fd6 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e ex_enter(p->db->
19fd7 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 mutex);. if(
19fd8 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 !p->okVar ){.
19fd9 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 int j;.
19fda 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 66 Op *pOp;. f
19fdb 6f 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 or(j=0, pOp=p->a
19fdc 4f 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b Op; j<p->nOp; j+
19fdd 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 +, pOp++){.
19fde 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f if( pOp->opco
19fdf 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 de==OP_Variable
19fe0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 ){. ass
19fe1 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 ert( pOp->p1>0 &
19fe2 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 & pOp->p1<=p->nV
19fe3 61 72 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ar );.
19fe4 70 2d 3e 61 7a 56 61 72 5b 70 4f 70 2d 3e 70 31 p->azVar[pOp->p1
19fe5 2d 31 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b -1] = pOp->p4.z;
19fe6 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
19fe7 20 7d 0a 20 20 20 20 20 20 70 2d 3e 6f 6b 56 61 }. p->okVa
19fe8 72 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 r = 1;. }.
19fe9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
19fea 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 eave(p->db->mute
19feb 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a x);. }.}../*.**
19fec 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 Return the name
19fed 20 6f 66 20 61 20 77 69 6c 64 63 61 72 64 20 70 of a wildcard p
19fee 61 72 61 6d 65 74 65 72 2e 20 20 52 65 74 75 72 arameter. Retur
19fef 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 69 6e n NULL if the in
19ff0 64 65 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 dex.** is out of
19ff1 20 72 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 range or if the
19ff2 20 77 69 6c 64 63 61 72 64 20 69 73 20 75 6e 6e wildcard is unn
19ff3 61 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 amed..**.** The
19ff4 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 result is always
19ff5 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 UTF-8..*/.SQLIT
19ff6 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
19ff7 20 2a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 *sqlite3_bind_p
19ff8 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 arameter_name(sq
19ff9 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
19ffa 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 t, int i){. Vdb
19ffb 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 e *p = (Vdbe*)pS
19ffc 74 6d 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 tmt;. if( p==0
19ffd 7c 7c 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e || i<1 || i>p->n
19ffe 56 61 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 Var ){. retur
19fff 6e 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 n 0;. }. creat
1a000 65 56 61 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 eVarMap(p);. re
1a001 74 75 72 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d turn p->azVar[i-
1a002 31 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 1];.}../*.** Giv
1a003 65 6e 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 en a wildcard pa
1a004 72 61 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 rameter name, re
1a005 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f turn the index o
1a006 66 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a f the variable.*
1a007 2a 20 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65 * with that name
1a008 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e . If there is n
1a009 6f 20 76 61 72 69 61 62 6c 65 20 77 69 74 68 20 o variable with
1a00a 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a the given name,.
1a00b 2a 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a ** return 0..*/.
1a00c 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1a00d 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
1a00e 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 meter_index(sqli
1a00f 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1a010 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
1a011 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d me){. Vdbe *p =
1a012 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe*)pStmt;.
1a013 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d int i;. if( p=
1a014 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
1a015 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 0;. }. create
1a016 56 61 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 VarMap(p); . if
1a017 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 ( zName ){. f
1a018 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 or(i=0; i<p->nVa
1a019 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 r; i++){. c
1a01a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 onst char *z = p
1a01b 2d 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 ->azVar[i];.
1a01c 20 20 69 66 28 20 7a 20 26 26 20 73 74 72 63 6d if( z && strcm
1a01d 70 28 7a 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b p(z,zName)==0 ){
1a01e 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1a01f 69 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 i+1;. }.
1a020 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1a021 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 0;.}../*.** Tran
1a022 73 66 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 sfer all binding
1a023 73 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 s from the first
1a024 20 73 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 statement over
1a025 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a to the second..*
1a026 2a 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 61 * If the two sta
1a027 74 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20 tements contain
1a028 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 a different numb
1a029 65 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20 er of bindings,
1a02a 74 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54 then.** an SQLIT
1a02b 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 E_ERROR is retur
1a02c 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
1a02d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1a02e 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e e3TransferBindin
1a02f 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 gs(sqlite3_stmt
1a030 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 *pFromStmt, sqli
1a031 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d te3_stmt *pToStm
1a032 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 6f t){. Vdbe *pFro
1a033 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f 6d m = (Vdbe*)pFrom
1a034 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 54 Stmt;. Vdbe *pT
1a035 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 74 o = (Vdbe*)pToSt
1a036 6d 74 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 mt;. int i, rc
1a037 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
1a038 66 28 20 28 70 46 72 6f 6d 2d 3e 6d 61 67 69 63 f( (pFrom->magic
1a039 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e !=VDBE_MAGIC_RUN
1a03a 20 26 26 20 70 46 72 6f 6d 2d 3e 6d 61 67 69 63 && pFrom->magic
1a03b 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c !=VDBE_MAGIC_HAL
1a03c 54 29 0a 20 20 20 20 7c 7c 20 28 70 54 6f 2d 3e T). || (pTo->
1a03d 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 magic!=VDBE_MAGI
1a03e 43 5f 52 55 4e 20 26 26 20 70 54 6f 2d 3e 6d 61 C_RUN && pTo->ma
1a03f 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic!=VDBE_MAGIC_
1a040 48 41 4c 54 29 0a 20 20 20 20 7c 7c 20 70 54 6f HALT). || pTo
1a041 2d 3e 64 62 21 3d 70 46 72 6f 6d 2d 3e 64 62 20 ->db!=pFrom->db
1a042 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
1a043 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d LITE_MISUSE;. }
1a044 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 . if( pFrom->nV
1a045 61 72 21 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b ar!=pTo->nVar ){
1a046 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1a047 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 TE_ERROR;. }.
1a048 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1a049 74 65 72 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 ter(pTo->db->mut
1a04a 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 ex);. for(i=0;
1a04b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1a04c 20 69 3c 70 46 72 6f 6d 2d 3e 6e 56 61 72 3b 20 i<pFrom->nVar;
1a04d 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 i++){. sqlite
1a04e 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 70 54 3VdbeMemMove(&pT
1a04f 6f 2d 3e 61 56 61 72 5b 69 5d 2c 20 26 70 46 72 o->aVar[i], &pFr
1a050 6f 6d 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 om->aVar[i]);.
1a051 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
1a052 78 5f 6c 65 61 76 65 28 70 54 6f 2d 3e 64 62 2d x_leave(pTo->db-
1a053 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 >mutex);. asser
1a054 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
1a055 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e || rc==SQLITE_N
1a056 4f 4d 45 4d 20 29 3b 0a 20 20 72 65 74 75 72 6e OMEM );. return
1a057 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 rc;.}..#ifndef
1a058 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 SQLITE_OMIT_DEPR
1a059 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 ECATED./*.** Dep
1a05a 72 65 63 61 74 65 64 20 65 78 74 65 72 6e 61 6c recated external
1a05b 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 6e 74 interface. Int
1a05c 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 ernal/core SQLit
1a05d 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 e code.** should
1a05e 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 54 72 61 call sqlite3Tra
1a05f 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 2e 0a 2a nsferBindings..*
1a060 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1a061 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 sqlite3_transfe
1a062 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 r_bindings(sqlit
1a063 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 e3_stmt *pFromSt
1a064 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 mt, sqlite3_stmt
1a065 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 72 65 *pToStmt){. re
1a066 74 75 72 6e 20 73 71 6c 69 74 65 33 54 72 61 6e turn sqlite3Tran
1a067 73 66 65 72 42 69 6e 64 69 6e 67 73 28 70 46 72 sferBindings(pFr
1a068 6f 6d 53 74 6d 74 2c 20 70 54 6f 53 74 6d 74 29 omStmt, pToStmt)
1a069 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1a06a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 71 6c * Return the sql
1a06b 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 ite3* database h
1a06c 61 6e 64 6c 65 20 74 6f 20 77 68 69 63 68 20 74 andle to which t
1a06d 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 he prepared stat
1a06e 65 6d 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 69 ement given.** i
1a06f 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 62 n the argument b
1a070 65 6c 6f 6e 67 73 2e 20 20 54 68 69 73 20 69 73 elongs. This is
1a071 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
1a072 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 se handle that w
1a073 61 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 as.** the first
1a074 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
1a075 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
1a076 29 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 ) that was used
1a077 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68 65 to create.** the
1a078 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 statement in th
1a079 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a e first place..*
1a07a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
1a07b 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 ite3 *sqlite3_db
1a07c 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f _handle(sqlite3_
1a07d 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 stmt *pStmt){.
1a07e 72 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20 28 return pStmt ? (
1a07f 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 (Vdbe*)pStmt)->d
1a080 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 b : 0;.}../*.**
1a081 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
1a082 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70 72 65 to the next pre
1a083 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 pared statement
1a084 61 66 74 65 72 20 70 53 74 6d 74 20 61 73 73 6f after pStmt asso
1a085 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 64 ciated.** with d
1a086 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1a087 6f 6e 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d on pDb. If pStm
1a088 74 20 69 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72 t is NULL, retur
1a089 6e 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 n the first.** p
1a08a 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
1a08b 74 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 t for the databa
1a08c 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 se connection.
1a08d 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 Return NULL if t
1a08e 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d here.** are no m
1a08f 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ore..*/.SQLITE_A
1a090 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 PI sqlite3_stmt
1a091 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 *sqlite3_next_st
1a092 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c mt(sqlite3 *pDb,
1a093 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 sqlite3_stmt *p
1a094 53 74 6d 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 Stmt){. sqlite3
1a095 5f 73 74 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 20 _stmt *pNext;.
1a096 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1a097 74 65 72 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b ter(pDb->mutex);
1a098 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 . if( pStmt==0
1a099 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 ){. pNext = (
1a09a 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 44 sqlite3_stmt*)pD
1a09b 62 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 65 6c 73 b->pVdbe;. }els
1a09c 65 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 e{. pNext = (
1a09d 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 28 28 sqlite3_stmt*)((
1a09e 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e Vdbe*)pStmt)->pN
1a09f 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 ext;. }. sqlit
1a0a0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
1a0a1 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 Db->mutex);. re
1a0a2 74 75 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a 2f turn pNext;.}../
1a0a3 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1a0a4 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 value of a statu
1a0a5 73 20 63 6f 75 6e 74 65 72 20 66 6f 72 20 61 20 s counter for a
1a0a6 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1a0a7 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 nt.*/.SQLITE_API
1a0a8 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d int sqlite3_stm
1a0a9 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 t_status(sqlite3
1a0aa 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1a0ab 74 20 6f 70 2c 20 69 6e 74 20 72 65 73 65 74 46 t op, int resetF
1a0ac 6c 61 67 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 lag){. Vdbe *pV
1a0ad 64 62 65 20 3d 20 28 56 64 62 65 2a 29 70 53 74 dbe = (Vdbe*)pSt
1a0ae 6d 74 3b 0a 20 20 69 6e 74 20 76 20 3d 20 70 56 mt;. int v = pV
1a0af 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 dbe->aCounter[op
1a0b0 2d 31 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74 -1];. if( reset
1a0b1 46 6c 61 67 20 29 20 70 56 64 62 65 2d 3e 61 43 Flag ) pVdbe->aC
1a0b2 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 30 ounter[op-1] = 0
1a0b3 3b 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a ;. return v;.}.
1a0b4 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1a0b5 20 45 6e 64 20 6f 66 20 76 64 62 65 61 70 69 2e End of vdbeapi.
1a0b6 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1a0b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a0b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1a0b9 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1a0ba 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 Begin file vdbe
1a0bb 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1a0bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a0bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1a0be 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
1a0bf 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
1a0c0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
1a0c1 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
1a0c2 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
1a0c3 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
1a0c4 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
1a0c5 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
1a0c6 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
1a0c7 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
1a0c8 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
1a0c9 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
1a0ca 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
1a0cb 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1a0cc 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
1a0cd 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
1a0ce 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
1a0cf 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
1a0d0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
1a0d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a0d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a0d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a0d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a0d5 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 *******.** The c
1a0d6 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ode in this file
1a0d7 20 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65 63 implements exec
1a0d8 75 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20 ution method of
1a0d9 74 68 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20 the .** Virtual
1a0da 44 61 74 61 62 61 73 65 20 45 6e 67 69 6e 65 20 Database Engine
1a0db 28 56 44 42 45 29 2e 20 20 41 20 73 65 70 61 72 (VDBE). A separ
1a0dc 61 74 65 20 66 69 6c 65 20 28 22 76 64 62 65 61 ate file ("vdbea
1a0dd 75 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65 ux.c").** handle
1a0de 73 20 68 6f 75 73 65 6b 65 65 70 69 6e 67 20 64 s housekeeping d
1a0df 65 74 61 69 6c 73 20 73 75 63 68 20 61 73 20 63 etails such as c
1a0e0 72 65 61 74 69 6e 67 20 61 6e 64 20 64 65 6c 65 reating and dele
1a0e1 74 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73 ting.** VDBE ins
1a0e2 74 61 6e 63 65 73 2e 20 20 54 68 69 73 20 66 69 tances. This fi
1a0e3 6c 65 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74 le is solely int
1a0e4 65 72 65 73 74 65 64 20 69 6e 20 65 78 65 63 75 erested in execu
1a0e5 74 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42 45 ting.** the VDBE
1a0e6 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 program..**.**
1a0e7 49 6e 20 74 68 65 20 65 78 74 65 72 6e 61 6c 20 In the external
1a0e8 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 20 22 73 interface, an "s
1a0e9 71 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20 69 73 qlite3_stmt*" is
1a0ea 20 61 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e 74 an opaque point
1a0eb 65 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e er.** to a VDBE.
1a0ec 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70 .**.** The SQL p
1a0ed 61 72 73 65 72 20 67 65 6e 65 72 61 74 65 73 20 arser generates
1a0ee 61 20 70 72 6f 67 72 61 6d 20 77 68 69 63 68 20 a program which
1a0ef 69 73 20 74 68 65 6e 20 65 78 65 63 75 74 65 64 is then executed
1a0f0 20 62 79 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 by.** the VDBE
1a0f1 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f to do the work o
1a0f2 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d f the SQL statem
1a0f3 65 6e 74 2e 20 20 56 44 42 45 20 70 72 6f 67 72 ent. VDBE progr
1a0f4 61 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69 ams are .** simi
1a0f5 6c 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61 lar in form to a
1a0f6 73 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65 ssembly language
1a0f7 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 . The program c
1a0f8 6f 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20 onsists of.** a
1a0f9 6c 69 6e 65 61 72 20 73 65 71 75 65 6e 63 65 20 linear sequence
1a0fa 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 of operations.
1a0fb 45 61 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 68 Each operation h
1a0fc 61 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a as an opcode .**
1a0fd 20 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 2e and 5 operands.
1a0fe 20 20 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 50 Operands P1, P
1a0ff 32 2c 20 61 6e 64 20 50 33 20 61 72 65 20 69 6e 2, and P3 are in
1a100 74 65 67 65 72 73 2e 20 20 4f 70 65 72 61 6e 64 tegers. Operand
1a101 20 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c P4 .** is a nul
1a102 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 l-terminated str
1a103 69 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 35 ing. Operand P5
1a104 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 is an unsigned
1a105 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65 character..** Fe
1a106 77 20 6f 70 63 6f 64 65 73 20 75 73 65 20 61 6c w opcodes use al
1a107 6c 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a l 5 operands..**
1a108 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20 .** Computation
1a109 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 results are stor
1a10a 65 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 20 72 ed on a set of r
1a10b 65 67 69 73 74 65 72 73 20 6e 75 6d 62 65 72 65 egisters numbere
1a10c 64 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 d beginning.** w
1a10d 69 74 68 20 31 20 61 6e 64 20 67 6f 69 6e 67 20 ith 1 and going
1a10e 75 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e up to Vdbe.nMem.
1a10f 20 20 45 61 63 68 20 72 65 67 69 73 74 65 72 20 Each register
1a110 63 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74 can store.** eit
1a111 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 her an integer,
1a112 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 a null-terminate
1a113 64 20 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61 d string, a floa
1a114 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75 ting point.** nu
1a115 6d 62 65 72 2c 20 6f 72 20 74 68 65 20 53 51 4c mber, or the SQL
1a116 20 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20 "NULL" value.
1a117 41 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76 An implicit conv
1a118 65 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a ersion from one.
1a119 2a 2a 20 74 79 70 65 20 74 6f 20 74 68 65 20 6f ** type to the o
1a11a 74 68 65 72 20 6f 63 63 75 72 73 20 61 73 20 6e ther occurs as n
1a11b 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a ecessary..** .**
1a11c 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64 Most of the cod
1a11d 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 e in this file i
1a11e 73 20 74 61 6b 65 6e 20 75 70 20 62 79 20 74 68 s taken up by th
1a11f 65 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 e sqlite3VdbeExe
1a120 63 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 c().** function
1a121 77 68 69 63 68 20 64 6f 65 73 20 74 68 65 20 77 which does the w
1a122 6f 72 6b 20 6f 66 20 69 6e 74 65 72 70 72 65 74 ork of interpret
1a123 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 ing a VDBE progr
1a124 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72 am..** But other
1a125 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c routines are al
1a126 73 6f 20 70 72 6f 76 69 64 65 64 20 74 6f 20 68 so provided to h
1a127 65 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e 67 20 elp in building
1a128 75 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20 up.** a program
1a129 69 6e 73 74 72 75 63 74 69 6f 6e 20 62 79 20 69 instruction by i
1a12a 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a nstruction..**.*
1a12b 2a 20 56 61 72 69 6f 75 73 20 73 63 72 69 70 74 * Various script
1a12c 73 20 73 63 61 6e 20 74 68 69 73 20 73 6f 75 72 s scan this sour
1a12d 63 65 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 ce file in order
1a12e 20 74 6f 20 67 65 6e 65 72 61 74 65 20 48 54 4d to generate HTM
1a12f 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 L.** documentati
1a130 6f 6e 2c 20 68 65 61 64 65 72 73 20 66 69 6c 65 on, headers file
1a131 73 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 72 69 s, or other deri
1a132 76 65 64 20 66 69 6c 65 73 2e 20 20 54 68 65 20 ved files. The
1a133 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66 formatting.** of
1a134 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 the code in thi
1a135 73 20 66 69 6c 65 20 69 73 2c 20 74 68 65 72 65 s file is, there
1a136 66 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e fore, important.
1a137 20 20 53 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d See other comm
1a138 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20 ents.** in this
1a139 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 file for details
1a13a 2e 20 20 49 66 20 69 6e 20 64 6f 75 62 74 2c 20 . If in doubt,
1a13b 64 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 do not deviate f
1a13c 72 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 rom existing.**
1a13d 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69 commenting and i
1a13e 6e 64 65 6e 74 61 74 69 6f 6e 20 70 72 61 63 74 ndentation pract
1a13f 69 63 65 73 20 77 68 65 6e 20 63 68 61 6e 67 69 ices when changi
1a140 6e 67 20 6f 72 20 61 64 64 69 6e 67 20 63 6f 64 ng or adding cod
1a141 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 e..**.** $Id: vd
1a142 62 65 2e 63 2c 76 20 31 2e 38 31 31 20 32 30 30 be.c,v 1.811 200
1a143 39 2f 30 31 2f 31 34 20 30 30 3a 35 35 3a 31 30 9/01/14 00:55:10
1a144 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f drh Exp $.*/../
1a145 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
1a146 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 ng global variab
1a147 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 le is incremente
1a148 64 20 65 76 65 72 79 20 74 69 6d 65 20 61 20 63 d every time a c
1a149 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 ursor.** moves,
1a14a 65 69 74 68 65 72 20 62 79 20 74 68 65 20 4f 50 either by the OP
1a14b 5f 53 65 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 74 _SeekXX, OP_Next
1a14c 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70 63 , or OP_Prev opc
1a14d 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73 74 0a odes. The test.
1a14e 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20 75 73 ** procedures us
1a14f 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 e this informati
1a150 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 on to make sure
1a151 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72 65 that indices are
1a152 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 .** working corr
1a153 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76 61 72 ectly. This var
1a154 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e iable has no fun
1a155 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e ction other than
1a156 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 to.** help veri
1a157 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f fy the correct o
1a158 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 peration of the
1a159 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 library..*/.#ifd
1a15a 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
1a15b 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1a15c 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 lite3_search_cou
1a15d 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
1a15e 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 /*.** When this
1a15f 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1a160 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69 74 20 is positive, it
1a161 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74 65 64 gets decremented
1a162 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 once before.**
1a163 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e each instruction
1a164 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 57 in the VDBE. W
1a165 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65 72 6f hen reaches zero
1a166 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74 65 72 , the u1.isInter
1a167 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20 rupted.** field
1a168 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 20 73 of the sqlite3 s
1a169 74 72 75 63 74 75 72 65 20 69 73 20 73 65 74 20 tructure is set
1a16a 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 in order to simu
1a16b 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72 72 75 late and interru
1a16c 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 pt..**.** This f
1a16d 61 63 69 6c 69 74 79 20 69 73 20 75 73 65 64 20 acility is used
1a16e 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 for testing purp
1a16f 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64 oses only. It d
1a170 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e oes not function
1a171 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61 .** in an ordina
1a172 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 ry build..*/.#if
1a173 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
1a174 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1a175 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
1a176 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 _count = 0;.#end
1a177 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 if../*.** The ne
1a178 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 xt global variab
1a179 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 le is incremente
1a17a 64 20 65 61 63 68 20 74 79 70 65 20 74 68 65 20 d each type the
1a17b 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a OP_Sort opcode.*
1a17c 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 20 * is executed.
1a17d 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 The test procedu
1a17e 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e 66 res use this inf
1a17f 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 ormation to make
1a180 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f sure that.** so
1a181 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72 72 69 rting is occurri
1a182 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 ng or not occurr
1a183 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 69 61 ing at appropria
1a184 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 69 73 te times. This
1a185 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 variable.** has
1a186 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 no function oth
1a187 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 er than to help
1a188 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 verify the corre
1a189 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 ct operation of
1a18a 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a the.** library..
1a18b 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
1a18c 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
1a18d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f 72 int sqlite3_sor
1a18e 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e t_count = 0;.#en
1a18f 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e dif../*.** The n
1a190 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 ext global varia
1a191 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65 20 ble records the
1a192 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 size of the larg
1a193 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 est MEM_Blob.**
1a194 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61 74 20 or MEM_Str that
1a195 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62 79 has been used by
1a196 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65 2e 20 a VDBE opcode.
1a197 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64 The test proced
1a198 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68 69 73 ures.** use this
1a199 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 information to
1a19a 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
1a19b 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e he zero-blob fun
1a19c 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 ctionality.** is
1a19d 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 working correct
1a19e 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72 69 61 ly. This varia
1a19f 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 ble has no funct
1a1a0 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 ion other than t
1a1a1 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 o.** help verify
1a1a2 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 the correct ope
1a1a3 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 ration of the li
1a1a4 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 brary..*/.#ifdef
1a1a5 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
1a1a6 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1a1a7 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 te3_max_blobsize
1a1a8 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 = 0;.static voi
1a1a9 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 d updateMaxBlobs
1a1aa 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 ize(Mem *p){. i
1a1ab 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 f( (p->flags & (
1a1ac 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
1a1ad 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 ))!=0 && p->n>sq
1a1ae 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 lite3_max_blobsi
1a1af 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ze ){. sqlite
1a1b0 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 3_max_blobsize =
1a1b1 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e p->n;. }.}.#en
1a1b2 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 dif../*.** Test
1a1b3 61 20 72 65 67 69 73 74 65 72 20 74 6f 20 73 65 a register to se
1a1b4 65 20 69 66 20 69 74 20 65 78 63 65 65 64 73 20 e if it exceeds
1a1b5 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 the current maxi
1a1b6 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a mum blob size..*
1a1b7 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20 72 65 * If it does, re
1a1b8 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d 61 78 cord the new max
1a1b9 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a imum blob size..
1a1ba 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
1a1bb 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 21 QLITE_TEST) && !
1a1bc 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1a1bd 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 MIT_BUILTIN_TEST
1a1be 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 ).# define UPDAT
1a1bf 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 E_MAX_BLOBSIZE(P
1a1c0 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 ) updateMaxBlob
1a1c1 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 size(P).#else.#
1a1c2 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 define UPDATE_MA
1a1c3 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 X_BLOBSIZE(P).#e
1a1c4 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 ndif../*.** Conv
1a1c5 65 72 74 20 74 68 65 20 67 69 76 65 6e 20 72 65 ert the given re
1a1c6 67 69 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74 gister into a st
1a1c7 72 69 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74 ring if it isn't
1a1c8 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e one.** already.
1a1c9 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f Return non-zero
1a1ca 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 if a malloc() f
1a1cb 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ails..*/.#define
1a1cc 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e Stringify(P, en
1a1cd 63 29 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d c) \. if(((P)-
1a1ce 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c >flags&(MEM_Str|
1a1cf 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 MEM_Blob))==0 &&
1a1d0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1a1d1 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 tringify(P,enc))
1a1d2 20 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e \. { goto n
1a1d3 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 o_mem; }../*.**
1a1d4 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 An ephemeral str
1a1d5 69 6e 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69 ing value (signi
1a1d6 66 69 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f fied by the MEM_
1a1d7 45 70 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 Ephem flag) cont
1a1d8 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 ains.** a pointe
1a1d9 72 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c r to a dynamical
1a1da 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 ly allocated str
1a1db 69 6e 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f ing where some o
1a1dc 74 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 ther entity.** i
1a1dd 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
1a1de 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 r deallocating t
1a1df 68 61 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63 hat string. Bec
1a1e0 61 75 73 65 20 74 68 65 20 72 65 67 69 73 74 65 ause the registe
1a1e1 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f r.** does not co
1a1e2 6e 74 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 ntrol the string
1a1e3 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 65 , it might be de
1a1e4 6c 65 74 65 64 20 77 69 74 68 6f 75 74 20 74 68 leted without th
1a1e5 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e e register.** kn
1a1e6 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 owing it..**.**
1a1e7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e This routine con
1a1e8 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72 verts an ephemer
1a1e9 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 al string into a
1a1ea 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c dynamically all
1a1eb 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 ocated.** string
1a1ec 20 74 68 61 74 20 74 68 65 20 72 65 67 69 73 74 that the regist
1a1ed 65 72 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f er itself contro
1a1ee 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f ls. In other wo
1a1ef 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 rds, it.** conve
1a1f0 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d rts an MEM_Ephem
1a1f1 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 string into an
1a1f2 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a MEM_Dyn string..
1a1f3 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68 */.#define Deeph
1a1f4 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 emeralize(P) \.
1a1f5 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 if( ((P)->flag
1a1f6 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 s&MEM_Ephem)!=0
1a1f7 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 \. && sqli
1a1f8 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 te3VdbeMemMakeWr
1a1f9 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f iteable(P) ){ go
1a1fa 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a to no_mem;}../*.
1a1fb 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 ** Call sqlite3V
1a1fc 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 dbeMemExpandBlob
1a1fd 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 () on the suppli
1a1fe 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d ed value (type M
1a1ff 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 em*).** P if req
1a200 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e uired..*/.#defin
1a201 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 e ExpandBlob(P)
1a202 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d (((P)->flags&MEM
1a203 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 _Zero)?sqlite3Vd
1a204 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 beMemExpandBlob(
1a205 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 P):0)../*.** Arg
1a206 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 ument pMem point
1a207 73 20 61 74 20 61 20 72 65 67 69 73 74 65 72 20 s at a register
1a208 74 68 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73 that will be pas
1a209 73 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 sed to a.** user
1a20a 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
1a20b 6e 20 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f n or returned to
1a20c 20 74 68 65 20 75 73 65 72 20 61 73 20 74 68 65 the user as the
1a20d 20 72 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65 result of a que
1a20e 72 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e ry..** The secon
1a20f 64 20 61 72 67 75 6d 65 6e 74 2c 20 27 64 62 5f d argument, 'db_
1a210 65 6e 63 27 20 69 73 20 74 68 65 20 74 65 78 74 enc' is the text
1a211 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 encoding used b
1a212 79 20 74 68 65 20 76 64 62 65 20 66 6f 72 0a 2a y the vdbe for.*
1a213 2a 20 72 65 67 69 73 74 65 72 20 76 61 72 69 61 * register varia
1a214 62 6c 65 73 2e 20 20 54 68 69 73 20 72 6f 75 74 bles. This rout
1a215 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 4d 65 ine sets the pMe
1a216 6d 2d 3e 65 6e 63 20 61 6e 64 20 70 4d 65 6d 2d m->enc and pMem-
1a217 3e 74 79 70 65 0a 2a 2a 20 76 61 72 69 61 62 6c >type.** variabl
1a218 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73 es used by the s
1a219 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29 qlite3_value_*()
1a21a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23 64 routines..*/.#d
1a21b 65 66 69 6e 65 20 73 74 6f 72 65 54 79 70 65 49 efine storeTypeI
1a21c 6e 66 6f 28 41 2c 42 29 20 5f 73 74 6f 72 65 54 nfo(A,B) _storeT
1a21d 79 70 65 49 6e 66 6f 28 41 29 0a 73 74 61 74 69 ypeInfo(A).stati
1a21e 63 20 76 6f 69 64 20 5f 73 74 6f 72 65 54 79 70 c void _storeTyp
1a21f 65 49 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d 29 eInfo(Mem *pMem)
1a220 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 {. int flags =
1a221 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 pMem->flags;. i
1a222 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e f( flags & MEM_N
1a223 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d ull ){. pMem-
1a224 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e >type = SQLITE_N
1a225 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 ULL;. }. else
1a226 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f if( flags & MEM_
1a227 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d Int ){. pMem-
1a228 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 >type = SQLITE_I
1a229 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65 6c NTEGER;. }. el
1a22a 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d se if( flags & M
1a22b 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 70 EM_Real ){. p
1a22c 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1a22d 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 20 TE_FLOAT;. }.
1a22e 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 else if( flags &
1a22f 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 MEM_Str ){.
1a230 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1a231 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c 73 ITE_TEXT;. }els
1a232 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 e{. pMem->typ
1a233 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b e = SQLITE_BLOB;
1a234 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 . }.}../*.** Pr
1a235 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70 63 6f operties of opco
1a236 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f des. The OPFLG_
1a237 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61 63 72 INITIALIZER macr
1a238 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 o is.** created
1a239 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b by mkopcodeh.awk
1a23a 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74 during compilat
1a23b 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20 6f 62 ion. Data is ob
1a23c 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 tained.** from t
1a23d 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c he comments foll
1a23e 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 65 20 owing the "case
1a23f 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74 65 6d OP_xxxx:" statem
1a240 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 69 73 20 ents in.** this
1a241 66 69 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 file. .*/.stati
1a242 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
1a243 20 63 68 61 72 20 6f 70 63 6f 64 65 50 72 6f 70 char opcodeProp
1a244 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f 49 erty[] = OPFLG_I
1a245 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a 0a NITIALIZER;../*.
1a246 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 ** Return true i
1a247 66 20 61 6e 20 6f 70 63 6f 64 65 20 68 61 73 20 f an opcode has
1a248 61 6e 79 20 6f 66 20 74 68 65 20 4f 50 46 4c 47 any of the OPFLG
1a249 5f 78 78 78 20 70 72 6f 70 65 72 74 69 65 73 0a _xxx properties.
1a24a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ** specified by
1a24b 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f mask..*/.SQLITE_
1a24c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1a24d 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73 te3VdbeOpcodeHas
1a24e 50 72 6f 70 65 72 74 79 28 69 6e 74 20 6f 70 63 Property(int opc
1a24f 6f 64 65 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a ode, int mask){.
1a250 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64 65 assert( opcode
1a251 3e 30 20 26 26 20 6f 70 63 6f 64 65 3c 28 69 6e >0 && opcode<(in
1a252 74 29 73 69 7a 65 6f 66 28 6f 70 63 6f 64 65 50 t)sizeof(opcodeP
1a253 72 6f 70 65 72 74 79 29 20 29 3b 0a 20 20 72 65 roperty) );. re
1a254 74 75 72 6e 20 28 6f 70 63 6f 64 65 50 72 6f 70 turn (opcodeProp
1a255 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26 6d 61 73 erty[opcode]&mas
1a256 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 k)!=0;.}../*.**
1a257 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 75 72 Allocate VdbeCur
1a258 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2e sor number iCur.
1a259 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 Return a point
1a25a 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74 75 72 er to it. Retur
1a25b 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 20 n NULL.** if we
1a25c 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 run out of memor
1a25d 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 64 62 y..*/.static Vdb
1a25e 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 61 74 eCursor *allocat
1a25f 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62 65 20 eCursor(. Vdbe
1a260 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
1a261 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 /* The virtual
1a262 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 machine */. int
1a263 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 iCur,
1a264 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 /* Index of t
1a265 68 65 20 6e 65 77 20 56 64 62 65 43 75 72 73 6f he new VdbeCurso
1a266 72 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 2c 20 r */. Op *pOp,
1a267 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a268 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 */. int iDb,
1a269 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 /* Wh
1a26a 65 6e 20 64 61 74 61 62 61 73 65 20 74 68 65 20 en database the
1a26b 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 cursor belongs t
1a26c 6f 2c 20 6f 72 20 2d 31 20 2a 2f 0a 20 20 69 6e o, or -1 */. in
1a26d 74 20 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 t isBtreeCursor
1a26e 20 20 20 20 2f 2a 20 2a 2f 0a 29 7b 0a 20 20 2f /* */.){. /
1a26f 2a 20 46 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72 * Find the memor
1a270 79 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c y cell that will
1a271 20 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 be used to stor
1a272 65 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 e the blob of me
1a273 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72 mory. ** requir
1a274 65 64 20 66 6f 72 20 74 68 69 73 20 56 64 62 65 ed for this Vdbe
1a275 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 Cursor structure
1a276 2e 20 49 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 . It is convenie
1a277 6e 74 20 74 6f 20 75 73 65 20 61 20 0a 20 20 2a nt to use a . *
1a278 2a 20 76 64 62 65 20 6d 65 6d 6f 72 79 20 63 65 * vdbe memory ce
1a279 6c 6c 20 74 6f 20 6d 61 6e 61 67 65 20 74 68 65 ll to manage the
1a27a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
1a27b 6f 6e 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 on required for
1a27c 61 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f a. ** VdbeCurso
1a27d 72 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 r structure for
1a27e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 the following re
1a27f 61 73 6f 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a asons:. **. **
1a280 20 20 20 2a 20 53 6f 6d 65 74 69 6d 65 73 20 63 * Sometimes c
1a281 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 ursor numbers ar
1a282 65 20 75 73 65 64 20 66 6f 72 20 61 20 63 6f 75 e used for a cou
1a283 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 ple of different
1a284 0a 20 20 2a 2a 20 20 20 20 20 70 75 72 70 6f 73 . ** purpos
1a285 65 73 20 69 6e 20 61 20 76 64 62 65 20 70 72 6f es in a vdbe pro
1a286 67 72 61 6d 2e 20 54 68 65 20 64 69 66 66 65 72 gram. The differ
1a287 65 6e 74 20 75 73 65 73 20 6d 69 67 68 74 20 72 ent uses might r
1a288 65 71 75 69 72 65 0a 20 20 2a 2a 20 20 20 20 20 equire. **
1a289 64 69 66 66 65 72 65 6e 74 20 73 69 7a 65 64 20 different sized
1a28a 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 4d 65 6d allocations. Mem
1a28b 6f 72 79 20 63 65 6c 6c 73 20 70 72 6f 76 69 64 ory cells provid
1a28c 65 20 67 72 6f 77 61 62 6c 65 0a 20 20 2a 2a 20 e growable. **
1a28d 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e allocations.
1a28e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 . **. ** * W
1a28f 68 65 6e 20 75 73 69 6e 67 20 45 4e 41 42 4c 45 hen using ENABLE
1a290 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
1a291 4e 54 2c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 NT, memory cell
1a292 62 75 66 66 65 72 73 20 63 61 6e 0a 20 20 2a 2a buffers can. **
1a293 20 20 20 20 20 62 65 20 66 72 65 65 64 20 6c 61 be freed la
1a294 7a 69 6c 79 20 76 69 61 20 74 68 65 20 73 71 6c zily via the sql
1a295 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d ite3_release_mem
1a296 6f 72 79 28 29 20 41 50 49 2e 20 54 68 69 73 0a ory() API. This.
1a297 20 20 2a 2a 20 20 20 20 20 6d 69 6e 69 6d 69 7a ** minimiz
1a298 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 es the number of
1a299 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 6d 61 malloc calls ma
1a29a 64 65 20 62 79 20 74 68 65 20 73 79 73 74 65 6d de by the system
1a29b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f .. **. ** Memo
1a29c 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 75 72 ry cells for cur
1a29d 73 6f 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 sors are allocat
1a29e 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 ed at the top of
1a29f 20 74 68 65 20 61 64 64 72 65 73 73 0a 20 20 2a the address. *
1a2a0 2a 20 73 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20 * space. Memory
1a2a1 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63 cell (p->nMem) c
1a2a2 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 63 75 orresponds to cu
1a2a3 72 73 6f 72 20 30 2e 20 53 70 61 63 65 20 66 6f rsor 0. Space fo
1a2a4 72 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 31 20 r. ** cursor 1
1a2a5 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 6d 65 is managed by me
1a2a6 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d mory cell (p->nM
1a2a7 65 6d 2d 31 29 2c 20 65 74 63 2e 0a 20 20 2a 2f em-1), etc.. */
1a2a8 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 . Mem *pMem = &
1a2a9 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d p->aMem[p->nMem-
1a2aa 69 43 75 72 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42 iCur];.. int nB
1a2ab 79 74 65 3b 0a 20 20 56 64 62 65 43 75 72 73 6f yte;. VdbeCurso
1a2ac 72 20 2a 70 43 78 20 3d 20 30 3b 0a 20 20 2f 2a r *pCx = 0;. /*
1a2ad 20 49 66 20 74 68 65 20 6f 70 63 6f 64 65 20 6f If the opcode o
1a2ae 66 20 70 4f 70 20 69 73 20 4f 50 5f 53 65 74 4e f pOp is OP_SetN
1a2af 75 6d 43 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 umColumns, then
1a2b0 70 4f 70 2d 3e 70 32 20 63 6f 6e 74 61 69 6e 73 pOp->p2 contains
1a2b1 0a 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 . ** the number
1a2b2 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 of fields in th
1a2b3 65 20 72 65 63 6f 72 64 73 20 63 6f 6e 74 61 69 e records contai
1a2b4 6e 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ned in the table
1a2b5 20 6f 72 0a 20 20 2a 2a 20 69 6e 64 65 78 20 62 or. ** index b
1a2b6 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 55 73 65 eing opened. Use
1a2b7 20 74 68 69 73 20 74 6f 20 72 65 73 65 72 76 65 this to reserve
1a2b8 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 0a space for the .
1a2b9 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 2e ** VdbeCursor.
1a2ba 61 54 79 70 65 5b 5d 20 61 72 72 61 79 2e 0a 20 aType[] array..
1a2bb 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 */. int nField
1a2bc 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4f 70 2d = 0;. if( pOp-
1a2bd 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 65 74 4e >opcode==OP_SetN
1a2be 75 6d 43 6f 6c 75 6d 6e 73 20 7c 7c 20 70 4f 70 umColumns || pOp
1a2bf 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 ->opcode==OP_Ope
1a2c0 6e 45 70 68 65 6d 65 72 61 6c 20 29 7b 0a 20 20 nEphemeral ){.
1a2c1 20 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e nField = pOp->
1a2c2 70 32 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 p2;. }. nByte
1a2c3 3d 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 = . sizeof(
1a2c4 56 64 62 65 43 75 72 73 6f 72 29 20 2b 20 0a 20 VdbeCursor) + .
1a2c5 20 20 20 20 20 28 69 73 42 74 72 65 65 43 75 72 (isBtreeCur
1a2c6 73 6f 72 3f 73 71 6c 69 74 65 33 42 74 72 65 65 sor?sqlite3Btree
1a2c7 43 75 72 73 6f 72 53 69 7a 65 28 29 3a 30 29 20 CursorSize():0)
1a2c8 2b 20 0a 20 20 20 20 20 20 32 2a 6e 46 69 65 6c + . 2*nFiel
1a2c9 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 3b 0a 0a d*sizeof(u32);..
1a2ca 20 20 61 73 73 65 72 74 28 20 69 43 75 72 3c 70 assert( iCur<p
1a2cb 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 ->nCursor );. i
1a2cc 66 28 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 f( p->apCsr[iCur
1a2cd 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ] ){. sqlite3
1a2ce 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 VdbeFreeCursor(p
1a2cf 2c 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d , p->apCsr[iCur]
1a2d0 29 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b );. p->apCsr[
1a2d1 69 43 75 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 iCur] = 0;. }.
1a2d2 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
1a2d3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1a2d4 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 ow(pMem, nByte,
1a2d5 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 61 70 43 0) ){. p->apC
1a2d6 73 72 5b 69 43 75 72 5d 20 3d 20 70 43 78 20 3d sr[iCur] = pCx =
1a2d7 20 28 56 64 62 65 43 75 72 73 6f 72 2a 29 70 4d (VdbeCursor*)pM
1a2d8 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 65 em->z;. memse
1a2d9 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 t(pMem->z, 0, nB
1a2da 79 74 65 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 yte);. pCx->i
1a2db 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 Db = iDb;. pC
1a2dc 78 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 x->nField = nFie
1a2dd 6c 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65 ld;. if( nFie
1a2de 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d ld ){. pCx-
1a2df 3e 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29 >aType = (u32 *)
1a2e0 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 &pMem->z[sizeof(
1a2e1 56 64 62 65 43 75 72 73 6f 72 29 5d 3b 0a 20 20 VdbeCursor)];.
1a2e2 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 42 74 }. if( isBt
1a2e3 72 65 65 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 reeCursor ){.
1a2e4 20 20 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 pCx->pCursor
1a2e5 3d 20 28 42 74 43 75 72 73 6f 72 2a 29 0a 20 20 = (BtCursor*).
1a2e6 20 20 20 20 20 20 20 20 26 70 4d 65 6d 2d 3e 7a &pMem->z
1a2e7 5b 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 [sizeof(VdbeCurs
1a2e8 6f 72 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a or)+2*nField*siz
1a2e9 65 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 7d eof(u32)];. }
1a2ea 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 . }. return pC
1a2eb 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 x;.}../*.** Try
1a2ec 74 6f 20 63 6f 6e 76 65 72 74 20 61 20 76 61 6c to convert a val
1a2ed 75 65 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 ue into a numeri
1a2ee 63 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e c representation
1a2ef 20 69 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f if we can.** do
1a2f0 20 73 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 so without loss
1a2f1 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e of information.
1a2f2 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
1a2f3 2c 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 0a , if the string.
1a2f4 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 ** looks like a
1a2f5 6e 75 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74 20 number, convert
1a2f6 69 74 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 it into a number
1a2f7 2e 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f . If it does no
1a2f8 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 t.** look like a
1a2f9 20 6e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20 69 number, leave i
1a2fa 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 t alone..*/.stat
1a2fb 69 63 20 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d ic void applyNum
1a2fc 65 72 69 63 41 66 66 69 6e 69 74 79 28 4d 65 6d ericAffinity(Mem
1a2fd 20 2a 70 52 65 63 29 7b 0a 20 20 69 66 28 20 28 *pRec){. if( (
1a2fe 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d pRec->flags & (M
1a2ff 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 EM_Real|MEM_Int)
1a300 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 )==0 ){. int
1a301 72 65 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c realnum;. sql
1a302 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
1a303 72 6d 69 6e 61 74 65 28 70 52 65 63 29 3b 0a 20 rminate(pRec);.
1a304 20 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c if( (pRec->fl
1a305 61 67 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20 ags&MEM_Str).
1a306 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 && sqlite3
1a307 49 73 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a IsNumber(pRec->z
1a308 2c 20 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63 , &realnum, pRec
1a309 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 ->enc) ){.
1a30a 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 i64 value;.
1a30b 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1a30c 67 65 45 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c geEncoding(pRec,
1a30d 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 SQLITE_UTF8);.
1a30e 20 20 20 20 20 69 66 28 20 21 72 65 61 6c 6e 75 if( !realnu
1a30f 6d 20 26 26 20 73 71 6c 69 74 65 33 41 74 6f 69 m && sqlite3Atoi
1a310 36 34 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c 64(pRec->z, &val
1a311 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ue) ){. p
1a312 52 65 63 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 65 Rec->u.i = value
1a313 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 53 65 74 ;. MemSet
1a314 54 79 70 65 46 6c 61 67 28 70 52 65 63 2c 20 4d TypeFlag(pRec, M
1a315 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 7d EM_Int);. }
1a316 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 else{. sq
1a317 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c lite3VdbeMemReal
1a318 69 66 79 28 70 52 65 63 29 3b 0a 20 20 20 20 20 ify(pRec);.
1a319 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a }. }. }.}..
1a31a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67 /*.** Processing
1a31b 20 69 73 20 64 65 74 65 72 6d 69 6e 65 20 62 79 is determine by
1a31c 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 70 61 the affinity pa
1a31d 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 rameter:.**.** S
1a31e 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
1a31f 52 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 R:.** SQLITE_AFF
1a320 5f 52 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45 _REAL:.** SQLITE
1a321 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a _AFF_NUMERIC:.**
1a322 20 20 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 Try to conve
1a323 72 74 20 70 52 65 63 20 74 6f 20 61 6e 20 69 6e rt pRec to an in
1a324 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 teger representa
1a325 74 69 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20 tion or a .**
1a326 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 floating-point
1a327 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 representation i
1a328 66 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 f an integer rep
1a329 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 resentation.**
1a32a 20 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c is not possibl
1a32b 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 e. Note that th
1a32c 65 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 e integer repres
1a32d 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 entation is.**
1a32e 20 20 61 6c 77 61 79 73 20 70 72 65 66 65 72 72 always preferr
1a32f 65 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20 ed, even if the
1a330 61 66 66 69 6e 69 74 79 20 69 73 20 52 45 41 4c affinity is REAL
1a331 2c 20 62 65 63 61 75 73 65 0a 2a 2a 20 20 20 20 , because.**
1a332 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 an integer repre
1a333 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72 sentation is mor
1a334 65 20 73 70 61 63 65 20 65 66 66 69 63 69 65 6e e space efficien
1a335 74 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a t on disk..**.**
1a336 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
1a337 3a 0a 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74 20 :.** Convert
1a338 70 52 65 63 20 74 6f 20 61 20 74 65 78 74 20 72 pRec to a text r
1a339 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a epresentation..*
1a33a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f *.** SQLITE_AFF_
1a33b 4e 4f 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f NONE:.** No-o
1a33c 70 2e 20 20 70 52 65 63 20 69 73 20 75 6e 63 68 p. pRec is unch
1a33d 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 anged..*/.static
1a33e 20 76 6f 69 64 20 61 70 70 6c 79 41 66 66 69 6e void applyAffin
1a33f 69 74 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63 ity(. Mem *pRec
1a340 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 , /* Th
1a341 65 20 76 61 6c 75 65 20 74 6f 20 61 70 70 6c 79 e value to apply
1a342 20 61 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a affinity to */.
1a343 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 2c char affinity,
1a344 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 /* The aff
1a345 69 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c inity to be appl
1a346 69 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20 ied */. u8 enc
1a347 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a348 55 73 65 20 74 68 69 73 20 74 65 78 74 20 65 6e Use this text en
1a349 63 6f 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 coding */.){. i
1a34a 66 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c f( affinity==SQL
1a34b 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a ITE_AFF_TEXT ){.
1a34c 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65 /* Only atte
1a34d 6d 70 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 mpt the conversi
1a34e 6f 6e 20 74 6f 20 54 45 58 54 20 69 66 20 74 68 on to TEXT if th
1a34f 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 ere is an intege
1a350 72 20 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a r or real. **
1a351 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1a352 28 62 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64 (blob and NULL d
1a353 6f 20 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65 72 o not get conver
1a354 74 65 64 29 20 62 75 74 20 6e 6f 20 73 74 72 69 ted) but no stri
1a355 6e 67 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73 ng. ** repres
1a356 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f entation.. */
1a357 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 52 65 . if( 0==(pRe
1a358 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 c->flags&MEM_Str
1a359 29 20 26 26 20 28 70 52 65 63 2d 3e 66 6c 61 67 ) && (pRec->flag
1a35a 73 26 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f s&(MEM_Real|MEM_
1a35b 49 6e 74 29 29 20 29 7b 0a 20 20 20 20 20 20 73 Int)) ){. s
1a35c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 qlite3VdbeMemStr
1a35d 69 6e 67 69 66 79 28 70 52 65 63 2c 20 65 6e 63 ingify(pRec, enc
1a35e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 );. }. pRe
1a35f 63 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 c->flags &= ~(ME
1a360 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b M_Real|MEM_Int);
1a361 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 66 66 . }else if( aff
1a362 69 6e 69 74 79 21 3d 53 51 4c 49 54 45 5f 41 46 inity!=SQLITE_AF
1a363 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 F_NONE ){. as
1a364 73 65 72 74 28 20 61 66 66 69 6e 69 74 79 3d 3d sert( affinity==
1a365 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 SQLITE_AFF_INTEG
1a366 45 52 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d ER || affinity==
1a367 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a SQLITE_AFF_REAL.
1a368 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
1a369 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 affinity==SQLITE
1a36a 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a _AFF_NUMERIC );.
1a36b 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 applyNumeric
1a36c 41 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a Affinity(pRec);.
1a36d 20 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c if( pRec->fl
1a36e 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 ags & MEM_Real )
1a36f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1a370 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 dbeIntegerAffini
1a371 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a ty(pRec);. }.
1a372 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 }.}../*.** Try
1a373 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 to convert the
1a374 74 79 70 65 20 6f 66 20 61 20 66 75 6e 63 74 69 type of a functi
1a375 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 72 20 61 on argument or a
1a376 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a result column.*
1a377 2a 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 * into a numeric
1a378 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e representation.
1a379 20 20 55 73 65 20 65 69 74 68 65 72 20 49 4e 54 Use either INT
1a37a 45 47 45 52 20 6f 72 20 52 45 41 4c 20 77 68 69 EGER or REAL whi
1a37b 63 68 65 76 65 72 0a 2a 2a 20 69 73 20 61 70 70 chever.** is app
1a37c 72 6f 70 72 69 61 74 65 2e 20 20 42 75 74 20 6f ropriate. But o
1a37d 6e 6c 79 20 64 6f 20 74 68 65 20 63 6f 6e 76 65 nly do the conve
1a37e 72 73 69 6f 6e 20 69 66 20 69 74 20 69 73 20 70 rsion if it is p
1a37f 6f 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74 0a ossible without.
1a380 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 ** loss of infor
1a381 6d 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 mation and retur
1a382 6e 20 74 68 65 20 72 65 76 69 73 65 64 20 74 79 n the revised ty
1a383 70 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65 pe of the argume
1a384 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 nt..**.** This i
1a385 73 20 61 6e 20 45 58 50 45 52 49 4d 45 4e 54 41 s an EXPERIMENTA
1a386 4c 20 61 70 69 20 61 6e 64 20 69 73 20 73 75 62 L api and is sub
1a387 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f ject to change o
1a388 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 r removal..*/.SQ
1a389 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1a38a 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 ite3_value_numer
1a38b 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f ic_type(sqlite3_
1a38c 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 value *pVal){.
1a38d 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d Mem *pMem = (Mem
1a38e 2a 29 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e *)pVal;. applyN
1a38f 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 umericAffinity(p
1a390 4d 65 6d 29 3b 0a 20 20 73 74 6f 72 65 54 79 70 Mem);. storeTyp
1a391 65 49 6e 66 6f 28 70 4d 65 6d 2c 20 30 29 3b 0a eInfo(pMem, 0);.
1a392 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74 return pMem->t
1a393 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 ype;.}../*.** Ex
1a394 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e 20 6f ported version o
1a395 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 f applyAffinity(
1a396 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b ). This one work
1a397 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c s on sqlite3_val
1a398 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65 ue*, .** not the
1a399 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74 internal Mem* t
1a39a 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ype..*/.SQLITE_P
1a39b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a39c 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 te3ValueApplyAff
1a39d 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74 65 33 inity(. sqlite3
1a39e 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20 _value *pVal, .
1a39f 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 0a 20 u8 affinity, .
1a3a0 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70 u8 enc.){. app
1a3a1 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65 6d 20 lyAffinity((Mem
1a3a2 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 *)pVal, affinity
1a3a3 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65 , enc);.}..#ifde
1a3a4 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f f SQLITE_DEBUG./
1a3a5 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 63 *.** Write a nic
1a3a6 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 e string represe
1a3a7 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 ntation of the c
1a3a8 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 ontents of cell
1a3a9 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66 pMem.** into buf
1a3aa 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68 fer zBuf, length
1a3ab 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nBuf..*/.SQLITE
1a3ac 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a3ad 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 lite3VdbeMemPret
1a3ae 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 tyPrint(Mem *pMe
1a3af 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a m, char *zBuf){.
1a3b0 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a char *zCsr = z
1a3b1 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70 Buf;. int f = p
1a3b2 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73 Mem->flags;.. s
1a3b3 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
1a3b4 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73 *const encnames
1a3b5 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 38 [] = {"(X)", "(8
1a3b6 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 28 )", "(16LE)", "(
1a3b7 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20 16BE)"};.. if(
1a3b8 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20 f&MEM_Blob ){.
1a3b9 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 int i;. cha
1a3ba 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66 20 26 r c;. if( f &
1a3bb 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 MEM_Dyn ){.
1a3bc 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 c = 'z';.
1a3bd 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d assert( (f & (M
1a3be 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 EM_Static|MEM_Ep
1a3bf 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 hem))==0 );.
1a3c0 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 }else if( f & ME
1a3c1 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 M_Static ){.
1a3c2 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 c = 't';.
1a3c3 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d assert( (f & (M
1a3c4 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d EM_Dyn|MEM_Ephem
1a3c5 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c ))==0 );. }el
1a3c6 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 se if( f & MEM_E
1a3c7 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 20 phem ){. c
1a3c8 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 = 'e';. ass
1a3c9 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 ert( (f & (MEM_S
1a3ca 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d tatic|MEM_Dyn))=
1a3cb 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b =0 );. }else{
1a3cc 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0a . c = 's';.
1a3cd 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 }.. sqlit
1a3ce 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c e3_snprintf(100,
1a3cf 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 29 3b zCsr, "%c", c);
1a3d0 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c . zCsr += sql
1a3d1 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 ite3Strlen30(zCs
1a3d2 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f r);. sqlite3_
1a3d3 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 snprintf(100, zC
1a3d4 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65 6d 2d sr, "%d[", pMem-
1a3d5 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d >n);. zCsr +=
1a3d6 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1a3d7 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f 72 28 (zCsr);. for(
1a3d8 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c 70 i=0; i<16 && i<p
1a3d9 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 Mem->n; i++){.
1a3da 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1a3db 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 intf(100, zCsr,
1a3dc 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29 70 4d "%02X", ((int)pM
1a3dd 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46 46 29 em->z[i] & 0xFF)
1a3de 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d );. zCsr +=
1a3df 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1a3e0 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 (zCsr);. }.
1a3e1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 for(i=0; i<16
1a3e2 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b && i<pMem->n; i+
1a3e3 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a +){. char z
1a3e4 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 = pMem->z[i];.
1a3e5 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20 7c 7c if( z<32 ||
1a3e6 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72 2b 2b z>126 ) *zCsr++
1a3e7 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 65 6c = '.';. el
1a3e8 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b 0a se *zCsr++ = z;.
1a3e9 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 }.. sqlit
1a3ea 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c e3_snprintf(100,
1a3eb 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 65 6e zCsr, "]%s", en
1a3ec 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 cnames[pMem->enc
1a3ed 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 ]);. zCsr +=
1a3ee 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1a3ef 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 66 zCsr);. if( f
1a3f0 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 & MEM_Zero ){.
1a3f1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
1a3f2 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c rintf(100, zCsr,
1a3f3 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 6e "+%dz",pMem->u.n
1a3f4 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a 43 73 Zero);. zCs
1a3f5 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c r += sqlite3Strl
1a3f6 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 en30(zCsr);.
1a3f7 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c }. *zCsr = '\
1a3f8 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0';. }else if(
1a3f9 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 f & MEM_Str ){.
1a3fa 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20 int j, k;.
1a3fb 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a zBuf[0] = ' ';.
1a3fc 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f if( f & MEM_
1a3fd 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 Dyn ){. zBu
1a3fe 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20 f[1] = 'z';.
1a3ff 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 assert( (f & (
1a400 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 MEM_Static|MEM_E
1a401 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 phem))==0 );.
1a402 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d }else if( f & M
1a403 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 EM_Static ){.
1a404 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27 zBuf[1] = 't'
1a405 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1a406 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 (f & (MEM_Dyn|ME
1a407 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a M_Ephem))==0 );.
1a408 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 }else if( f
1a409 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 & MEM_Ephem ){.
1a40a 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 zBuf[1] = '
1a40b 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 e';. assert
1a40c 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 ( (f & (MEM_Stat
1a40d 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 ic|MEM_Dyn))==0
1a40e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
1a40f 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73 zBuf[1] = 's
1a410 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d ';. }. k =
1a411 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 2;. sqlite3_
1a412 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 7a snprintf(100, &z
1a413 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d Buf[k], "%d", pM
1a414 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d em->n);. k +=
1a415 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1a416 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 (&zBuf[k]);.
1a417 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b zBuf[k++] = '[';
1a418 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c . for(j=0; j<
1a419 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 15 && j<pMem->n;
1a41a 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 j++){. u8
1a41b 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a c = pMem->z[j];.
1a41c 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30 78 32 if( c>=0x2
1a41d 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b 0a 20 0 && c<0x7f ){.
1a41e 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d zBuf[k++]
1a41f 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 = c;. }els
1a420 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b e{. zBuf[
1a421 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20 k++] = '.';.
1a422 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 }. }. zB
1a423 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20 uf[k++] = ']';.
1a424 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1a425 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d ntf(100,&zBuf[k]
1a426 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d , encnames[pMem-
1a427 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d >enc]);. k +=
1a428 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1a429 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 (&zBuf[k]);.
1a42a 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 zBuf[k++] = 0;.
1a42b 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 }.}.#endif..#if
1a42c 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1a42d 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 ./*.** Print the
1a42e 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65 67 69 value of a regi
1a42f 73 74 65 72 20 66 6f 72 20 74 72 61 63 69 6e 67 ster for tracing
1a430 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 74 purposes:.*/.st
1a431 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 72 61 atic void memTra
1a432 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75 cePrint(FILE *ou
1a433 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 t, Mem *p){. if
1a434 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ( p->flags & MEM
1a435 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66 70 72 _Null ){. fpr
1a436 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 4c 4c intf(out, " NULL
1a437 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ");. }else if(
1a438 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d (p->flags & (MEM
1a439 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29 3d 3d _Int|MEM_Str))==
1a43a 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 (MEM_Int|MEM_Str
1a43b 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 ) ){. fprintf
1a43c 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c 64 22 (out, " si:%lld"
1a43d 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c , p->u.i);. }el
1a43e 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 se if( p->flags
1a43f 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
1a440 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 fprintf(out, "
1a441 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 i:%lld", p->u.i)
1a442 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d ;. }else if( p-
1a443 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 >flags & MEM_Rea
1a444 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 l ){. fprintf
1a445 28 6f 75 74 2c 20 22 20 72 3a 25 67 22 2c 20 70 (out, " r:%g", p
1a446 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ->r);. }else{.
1a447 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 30 char zBuf[200
1a448 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ];. sqlite3Vd
1a449 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 beMemPrettyPrint
1a44a 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 (p, zBuf);. f
1a44b 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22 29 printf(out, " ")
1a44c 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 ;. fprintf(ou
1a44d 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a t, "%s", zBuf);.
1a44e 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 }.}.static voi
1a44f 64 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28 d registerTrace(
1a450 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69 FILE *out, int i
1a451 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 Reg, Mem *p){.
1a452 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52 45 fprintf(out, "RE
1a453 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67 29 G[%d] = ", iReg)
1a454 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69 6e ;. memTracePrin
1a455 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70 72 t(out, p);. fpr
1a456 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b intf(out, "\n");
1a457 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 .}.#endif..#ifde
1a458 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 f SQLITE_DEBUG.#
1a459 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45 define REGISTE
1a45a 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66 28 R_TRACE(R,M) if(
1a45b 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74 65 p->trace)registe
1a45c 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c rTrace(p->trace,
1a45d 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 R,M).#else.# de
1a45e 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52 fine REGISTER_TR
1a45f 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a ACE(R,M).#endif.
1a460 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 ..#ifdef VDBE_PR
1a461 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 OFILE../* .** hw
1a462 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 time.h contains
1a463 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 inline assembler
1a464 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d code for implem
1a465 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d enting .** high-
1a466 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 performance timi
1a467 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a ng routines..*/.
1a468 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1a469 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 Include hwtime.h
1a46a 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
1a46b 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a f vdbe.c *******
1a46c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1a46d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1a46e 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d Begin file hwtim
1a46f 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
1a470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a471 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1a472 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 /*.** 2008 May 2
1a473 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 7.**.** The auth
1a474 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
1a475 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
1a476 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
1a477 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
1a478 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
1a479 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
1a47a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1a47b 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
1a47c 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1a47d 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1a47e 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1a47f 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1a480 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1a481 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1a482 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
1a483 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
1a484 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
1a485 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a486 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a487 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a488 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a489 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
1a48a 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 file contains i
1a48b 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 nline asm code f
1a48c 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 or retrieving "h
1a48d 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 igh-performance"
1a48e 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 .** counters for
1a48f 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e x86 class CPUs.
1a490 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 .**.** $Id: hwti
1a491 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f me.h,v 1.3 2008/
1a492 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 08/01 14:33:15 s
1a493 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 hane Exp $.*/.#i
1a494 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f fndef _HWTIME_H_
1a495 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 .#define _HWTIME
1a496 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 _H_../*.** The f
1a497 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
1a498 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 only works on p
1a499 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 entium-class (or
1a49a 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f newer) processo
1a49b 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 rs..** It uses t
1a49c 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 he RDTSC opcode
1a49d 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c to read the cycl
1a49e 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 e count value ou
1a49f 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 t of the.** proc
1a4a0 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e essor and return
1a4a1 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 s that value. T
1a4a2 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 his can be used
1a4a3 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 for high-res.**
1a4a4 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 profiling..*/.#i
1a4a5 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 f (defined(__GNU
1a4a6 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 C__) || defined(
1a4a7 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a _MSC_VER)) && \.
1a4a8 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 (defined(i
1a4a9 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 386) || defined(
1a4aa 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 __i386__) || def
1a4ab 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a ined(_M_IX86))..
1a4ac 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f #if defined(__
1a4ad 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c GNUC__).. __inl
1a4ae 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
1a4af 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
1a4b0 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e e(void){. un
1a4b1 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 signed int lo, h
1a4b2 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 i;. __asm__
1a4b3 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 __volatile__ ("r
1a4b4 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f dtsc" : "=a" (lo
1a4b5 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 ), "=d" (hi));.
1a4b6 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 return (sqli
1a4b7 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 te_uint64)hi <<
1a4b8 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 32 | lo;. }..
1a4b9 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d #elif defined(_M
1a4ba 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 SC_VER).. __dec
1a4bb 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 lspec(naked) __i
1a4bc 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e nline sqlite_uin
1a4bd 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 t64 __cdecl sqli
1a4be 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
1a4bf 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 . __asm {.
1a4c0 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 rdtsc.
1a4c1 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 ret ;
1a4c2 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 return value at
1a4c3 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 EDX:EAX. }.
1a4c4 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 }.. #endif..#e
1a4c5 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 lif (defined(__G
1a4c6 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 NUC__) && define
1a4c7 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a d(__x86_64__))..
1a4c8 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
1a4c9 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
1a4ca 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
1a4cb 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c unsigned l
1a4cc 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f ong val;. _
1a4cd 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
1a4ce 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 e__ ("rdtsc" : "
1a4cf 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 =A" (val));.
1a4d0 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 return val;.
1a4d1 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e }. .#elif (defin
1a4d2 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
1a4d3 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 defined(__ppc__)
1a4d4 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 ).. __inline__
1a4d5 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
1a4d6 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
1a4d7 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 ){. unsigne
1a4d8 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 d long long retv
1a4d9 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e al;. unsign
1a4da 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 ed long junk;.
1a4db 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f __asm__ __vo
1a4dc 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 latile__ ("\n\.
1a4dd 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 1:
1a4de 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 mftbu %1\n\.
1a4df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a4e0 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a mftb %L0\n\.
1a4e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a4e2 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a mftbu %0\n\.
1a4e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a4e4 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c cmpw %0,%1\
1a4e5 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
1a4e6 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 bne 1b"
1a4e7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1a4e8 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 : "=r" (retva
1a4e9 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 l), "=r" (junk))
1a4ea 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
1a4eb 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 etval;. }..#els
1a4ec 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 e.. #error Need
1a4ed 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
1a4ee 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 of sqlite3Hwtime
1a4ef 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 () for your plat
1a4f0 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a form... /*. **
1a4f1 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 To compile with
1a4f2 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 out implementing
1a4f3 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
1a4f4 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f for your platfo
1a4f5 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e rm,. ** you can
1a4f6 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 remove the abov
1a4f7 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 e #error and use
1a4f8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 the following.
1a4f9 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f ** stub functio
1a4fa 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 n. You will los
1a4fb 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 e timing support
1a4fc 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f for many. ** o
1a4fd 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 f the debugging
1a4fe 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c and testing util
1a4ff 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 ities, but it sh
1a500 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 ould at. ** lea
1a501 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 st compile and r
1a502 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f un.. */.SQLITE_
1a503 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 PRIVATE sqlite
1a504 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
1a505 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 wtime(void){ ret
1a506 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e urn ((sqlite_uin
1a507 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 t64)0); }..#endi
1a508 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 f..#endif /* !de
1a509 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f fined(_HWTIME_H_
1a50a 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
1a50b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 ***** End of hwt
1a50c 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ime.h **********
1a50d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a50e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a50f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1a510 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
1a511 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
1a512 66 66 20 69 6e 20 76 64 62 65 2e 63 20 2a 2a 2a ff in vdbe.c ***
1a513 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a514 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 66 0a 0a 2f ****/..#endif../
1a515 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 46 *.** The CHECK_F
1a516 4f 52 5f 49 4e 54 45 52 52 55 50 54 20 6d 61 63 OR_INTERRUPT mac
1a517 72 6f 20 64 65 66 69 6e 65 64 20 68 65 72 65 20 ro defined here
1a518 6c 6f 6f 6b 73 20 74 6f 20 73 65 65 20 69 66 20 looks to see if
1a519 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 the.** sqlite3_i
1a51a 6e 74 65 72 72 75 70 74 28 29 20 72 6f 75 74 69 nterrupt() routi
1a51b 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c ne has been call
1a51c 65 64 2e 20 20 49 66 20 69 74 20 68 61 73 20 62 ed. If it has b
1a51d 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a 20 70 72 6f een, then.** pro
1a51e 63 65 73 73 69 6e 67 20 6f 66 20 74 68 65 20 56 cessing of the V
1a51f 44 42 45 20 70 72 6f 67 72 61 6d 20 69 73 20 69 DBE program is i
1a520 6e 74 65 72 72 75 70 74 65 64 2e 0a 2a 2a 0a 2a nterrupted..**.*
1a521 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 61 64 64 * This macro add
1a522 65 64 20 74 6f 20 65 76 65 72 79 20 69 6e 73 74 ed to every inst
1a523 72 75 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 ruction that doe
1a524 73 20 61 20 6a 75 6d 70 20 69 6e 20 6f 72 64 65 s a jump in orde
1a525 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e r to.** implemen
1a526 74 20 61 20 6c 6f 6f 70 2e 20 20 54 68 69 73 20 t a loop. This
1a527 74 65 73 74 20 75 73 65 64 20 74 6f 20 62 65 20 test used to be
1a528 6f 6e 20 65 76 65 72 79 20 73 69 6e 67 6c 65 20 on every single
1a529 69 6e 73 74 72 75 63 74 69 6f 6e 2c 0a 2a 2a 20 instruction,.**
1a52a 62 75 74 20 74 68 61 74 20 6d 65 61 6e 74 20 77 but that meant w
1a52b 65 20 6d 6f 72 65 20 74 65 73 74 69 6e 67 20 74 e more testing t
1a52c 68 61 74 20 77 65 20 6e 65 65 64 65 64 2e 20 20 hat we needed.
1a52d 42 79 20 6f 6e 6c 79 20 74 65 73 74 69 6e 67 20 By only testing
1a52e 74 68 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 6a the.** flag on j
1a52f 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 ump instructions
1a530 2c 20 77 65 20 67 65 74 20 61 20 28 73 6d 61 6c , we get a (smal
1a531 6c 29 20 73 70 65 65 64 20 69 6d 70 72 6f 76 65 l) speed improve
1a532 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ment..*/.#define
1a533 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 CHECK_FOR_INTER
1a534 52 55 50 54 20 5c 0a 20 20 20 69 66 28 20 64 62 RUPT \. if( db
1a535 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 ->u1.isInterrupt
1a536 65 64 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f ed ) goto abort_
1a537 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 due_to_interrupt
1a538 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
1a539 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e _DEBUG.static in
1a53a 74 20 66 69 6c 65 45 78 69 73 74 73 28 73 71 6c t fileExists(sql
1a53b 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
1a53c 63 68 61 72 20 2a 7a 46 69 6c 65 29 7b 0a 20 20 char *zFile){.
1a53d 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 20 69 int res = 0;. i
1a53e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1a53f 4b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 K;.#ifdef SQLITE
1a540 5f 54 45 53 54 0a 20 20 2f 2a 20 49 66 20 77 65 _TEST. /* If we
1a541 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 74 are currently t
1a542 65 73 74 69 6e 67 20 49 4f 20 65 72 72 6f 72 73 esting IO errors
1a543 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 61 , then do not ca
1a544 6c 6c 20 4f 73 41 63 63 65 73 73 28 29 20 74 6f ll OsAccess() to
1a545 0a 20 20 2a 2a 20 74 65 73 74 20 66 6f 72 20 74 . ** test for t
1a546 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 7a he presence of z
1a547 46 69 6c 65 2e 20 54 68 69 73 20 69 73 20 62 65 File. This is be
1a548 63 61 75 73 65 20 61 6e 79 20 49 4f 20 65 72 72 cause any IO err
1a549 6f 72 20 74 68 61 74 0a 20 20 2a 2a 20 6f 63 63 or that. ** occ
1a54a 75 72 73 20 68 65 72 65 20 77 69 6c 6c 20 6e 6f urs here will no
1a54b 74 20 62 65 20 72 65 70 6f 72 74 65 64 2c 20 63 t be reported, c
1a54c 61 75 73 69 6e 67 20 74 68 65 20 74 65 73 74 20 ausing the test
1a54d 74 6f 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 to fail.. */.
1a54e 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 extern int sqlit
1a54f 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
1a550 69 6e 67 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 ing;. if( sqlit
1a551 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
1a552 69 6e 67 3c 3d 30 20 29 0a 23 65 6e 64 69 66 0a ing<=0 ).#endif.
1a553 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1a554 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70 56 66 OsAccess(db->pVf
1a555 73 2c 20 7a 46 69 6c 65 2c 20 53 51 4c 49 54 45 s, zFile, SQLITE
1a556 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 _ACCESS_EXISTS,
1a557 26 72 65 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 &res);. return
1a558 28 72 65 73 20 26 26 20 72 63 3d 3d 53 51 4c 49 (res && rc==SQLI
1a559 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 TE_OK);.}.#endif
1a55a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
1a55b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
1a55c 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 61 6c tion is only cal
1a55d 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 led from within
1a55e 61 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 72 an assert() expr
1a55f 65 73 73 69 6f 6e 2e 20 49 74 0a 2a 2a 20 63 68 ession. It.** ch
1a560 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 73 71 ecks that the sq
1a561 6c 69 74 65 33 2e 6e 54 72 61 6e 73 61 63 74 69 lite3.nTransacti
1a562 6f 6e 20 76 61 72 69 61 62 6c 65 20 69 73 20 63 on variable is c
1a563 6f 72 72 65 63 74 6c 79 20 73 65 74 20 74 6f 0a orrectly set to.
1a564 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
1a565 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e non-transaction
1a566 20 73 61 76 65 70 6f 69 6e 74 73 20 63 75 72 72 savepoints curr
1a567 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 0a 2a 2a ently in the .**
1a568 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74 61 linked list sta
1a569 72 74 69 6e 67 20 61 74 20 73 71 6c 69 74 65 33 rting at sqlite3
1a56a 2e 70 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20 .pSavepoint..**
1a56b 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a 2a .** Usage:.**.**
1a56c 20 20 20 20 20 61 73 73 65 72 74 28 20 63 68 65 assert( che
1a56d 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 ckSavepointCount
1a56e 28 64 62 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 (db) );.*/.stati
1a56f 63 20 69 6e 74 20 63 68 65 63 6b 53 61 76 65 70 c int checkSavep
1a570 6f 69 6e 74 43 6f 75 6e 74 28 73 71 6c 69 74 65 ointCount(sqlite
1a571 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 6e 20 3 *db){. int n
1a572 3d 20 30 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 = 0;. Savepoint
1a573 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 64 62 2d *p;. for(p=db-
1a574 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 70 3b 20 >pSavepoint; p;
1a575 70 3d 70 2d 3e 70 4e 65 78 74 29 20 6e 2b 2b 3b p=p->pNext) n++;
1a576 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 28 64 . assert( n==(d
1a577 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b 20 b->nSavepoint +
1a578 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f db->isTransactio
1a579 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a 20 nSavepoint) );.
1a57a 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e return 1;.}.#en
1a57b 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 dif../*.** Execu
1a57c 74 65 20 61 73 20 6d 75 63 68 20 6f 66 20 61 20 te as much of a
1a57d 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61 73 20 VDBE program as
1a57e 77 65 20 63 61 6e 20 74 68 65 6e 20 72 65 74 75 we can then retu
1a57f 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 rn..**.** sqlite
1a580 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 29 3VdbeMakeReady()
1a581 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 must be called
1a582 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 before this rout
1a583 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a ine in order to.
1a584 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 70 72 6f ** close the pro
1a585 67 72 61 6d 20 77 69 74 68 20 61 20 66 69 6e 61 gram with a fina
1a586 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20 74 6f l OP_Halt and to
1a587 20 73 65 74 20 75 70 20 74 68 65 20 63 61 6c 6c set up the call
1a588 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74 68 65 backs.** and the
1a589 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70 error message p
1a58a 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 ointer..**.** Wh
1a58b 65 6e 65 76 65 72 20 61 20 72 6f 77 20 6f 72 20 enever a row or
1a58c 72 65 73 75 6c 74 20 64 61 74 61 20 69 73 20 61 result data is a
1a58d 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 72 vailable, this r
1a58e 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 69 74 68 outine will eith
1a58f 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 er.** invoke the
1a590 20 72 65 73 75 6c 74 20 63 61 6c 6c 62 61 63 6b result callback
1a591 20 28 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e (if there is on
1a592 65 29 20 6f 72 20 72 65 74 75 72 6e 20 77 69 74 e) or return wit
1a593 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57 2e h.** SQLITE_ROW.
1a594 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61 74 74 .**.** If an att
1a595 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 empt is made to
1a596 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20 64 61 open a locked da
1a597 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74 68 69 tabase, then thi
1a598 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 69 6c s routine.** wil
1a599 6c 20 65 69 74 68 65 72 20 69 6e 76 6f 6b 65 20 l either invoke
1a59a 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 the busy callbac
1a59b 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20 6f k (if there is o
1a59c 6e 65 29 20 6f 72 20 69 74 20 77 69 6c 6c 0a 2a ne) or it will.*
1a59d 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * return SQLITE_
1a59e 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 BUSY..**.** If a
1a59f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
1a5a0 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1a5a1 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6d is written to m
1a5a2 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a emory obtained.*
1a5a3 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d * from sqlite3_m
1a5a4 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d 3e 7a alloc() and p->z
1a5a5 45 72 72 4d 73 67 20 69 73 20 6d 61 64 65 20 74 ErrMsg is made t
1a5a6 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 o point to that
1a5a7 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 20 65 memory..** The e
1a5a8 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 74 6f rror code is sto
1a5a9 72 65 64 20 69 6e 20 70 2d 3e 72 63 20 61 6e 64 red in p->rc and
1a5aa 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
1a5ab 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 52 52 turns SQLITE_ERR
1a5ac 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 OR..**.** If the
1a5ad 20 63 61 6c 6c 62 61 63 6b 20 65 76 65 72 20 72 callback ever r
1a5ae 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c eturns non-zero,
1a5af 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 61 then the progra
1a5b0 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d 65 64 m exits.** immed
1a5b1 69 61 74 65 6c 79 2e 20 20 54 68 65 72 65 20 77 iately. There w
1a5b2 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f 72 20 ill be no error
1a5b3 6d 65 73 73 61 67 65 20 62 75 74 20 74 68 65 20 message but the
1a5b4 70 2d 3e 72 63 20 66 69 65 6c 64 20 69 73 0a 2a p->rc field is.*
1a5b5 2a 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f * set to SQLITE_
1a5b6 41 42 4f 52 54 20 61 6e 64 20 74 68 69 73 20 72 ABORT and this r
1a5b7 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 outine will retu
1a5b8 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e rn SQLITE_ERROR.
1a5b9 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 .**.** A memory
1a5ba 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
1a5bb 20 63 61 75 73 65 73 20 70 2d 3e 72 63 20 74 6f causes p->rc to
1a5bc 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 be set to SQLIT
1a5bd 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68 69 73 E_NOMEM and this
1a5be 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 72 .** routine to r
1a5bf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
1a5c0 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 OR..**.** Other
1a5c1 66 61 74 61 6c 20 65 72 72 6f 72 73 20 72 65 74 fatal errors ret
1a5c2 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
1a5c3 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 ..**.** After th
1a5c4 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66 is routine has f
1a5c5 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 74 65 33 inished, sqlite3
1a5c6 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 29 20 73 VdbeFinalize() s
1a5c7 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73 65 64 hould be.** used
1a5c8 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 68 65 to clean up the
1a5c9 20 6d 65 73 73 20 74 68 61 74 20 77 61 73 20 6c mess that was l
1a5ca 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f 0a 53 eft behind..*/.S
1a5cb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1a5cc 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 t sqlite3VdbeExe
1a5cd 63 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20 c(. Vdbe *p
1a5ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a5cf 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 /* The VDBE */.)
1a5d0 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 {. int pc;
1a5d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a5d2 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f * The program co
1a5d3 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 70 unter */. Op *p
1a5d4 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Op;
1a5d5 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 /* Current
1a5d6 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 operation */.
1a5d7 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1a5d8 4f 4b 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 OK; /* Va
1a5d9 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f lue to return */
1a5da 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1a5db 20 70 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a p->db; /*
1a5dc 20 54 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f The database */
1a5dd 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 3d . u8 encoding =
1a5de 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f 2a ENC(db); /*
1a5df 20 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e The database en
1a5e0 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 4d 65 6d 20 coding */. Mem
1a5e1 2a 70 49 6e 31 20 3d 20 30 3b 20 20 20 20 20 20 *pIn1 = 0;
1a5e2 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 69 6e /* 1st in
1a5e3 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 put operand */.
1a5e4 20 4d 65 6d 20 2a 70 49 6e 32 20 3d 20 30 3b 20 Mem *pIn2 = 0;
1a5e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 /* 2
1a5e6 6e 64 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 nd input operand
1a5e7 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 33 20 */. Mem *pIn3
1a5e8 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
1a5e9 20 2f 2a 20 33 72 64 20 69 6e 70 75 74 20 6f 70 /* 3rd input op
1a5ea 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a erand */. Mem *
1a5eb 70 4f 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20 pOut = 0;
1a5ec 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 /* Output
1a5ed 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 75 38 20 operand */. u8
1a5ee 6f 70 50 72 6f 70 65 72 74 79 3b 0a 20 20 69 6e opProperty;. in
1a5ef 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 30 3b 20 t iCompare = 0;
1a5f0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 /* Resu
1a5f1 6c 74 20 6f 66 20 6c 61 73 74 20 4f 50 5f 43 6f lt of last OP_Co
1a5f2 6d 70 61 72 65 20 6f 70 65 72 61 74 69 6f 6e 20 mpare operation
1a5f3 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d 75 */. int *aPermu
1a5f4 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 te = 0;
1a5f5 2f 2a 20 50 65 72 6d 75 61 74 69 6f 6e 20 6f 66 /* Permuation of
1a5f6 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 4f 50 5f columns for OP_
1a5f7 43 6f 6d 70 61 72 65 20 2a 2f 0a 23 69 66 64 65 Compare */.#ifde
1a5f8 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 f VDBE_PROFILE.
1a5f9 20 75 36 34 20 73 74 61 72 74 3b 20 20 20 20 20 u64 start;
1a5fa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1a5fb 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e 74 20 61 PU clock count a
1a5fc 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63 6f 64 t start of opcod
1a5fd 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 69 67 50 e */. int origP
1a5fe 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
1a5ff 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 /* Program cou
1a600 6e 74 65 72 20 61 74 20 73 74 61 72 74 20 6f 66 nter at start of
1a601 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 opcode */.#endi
1a602 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
1a603 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 _OMIT_PROGRESS_C
1a604 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 6e 50 ALLBACK. int nP
1a605 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 20 rogressOps = 0;
1a606 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20 /* Opcodes
1a607 65 78 65 63 75 74 65 64 20 73 69 6e 63 65 20 70 executed since p
1a608 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b rogress callback
1a609 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 55 6e . */.#endif. Un
1a60a 70 61 63 6b 65 64 52 65 63 6f 72 64 20 61 54 65 packedRecord aTe
1a60b 6d 70 52 65 63 5b 31 36 5d 3b 20 2f 2a 20 53 70 mpRec[16]; /* Sp
1a60c 61 63 65 20 74 6f 20 68 6f 6c 64 20 61 20 74 72 ace to hold a tr
1a60d 61 6e 73 69 65 6e 74 20 55 6e 70 61 63 6b 65 64 ansient Unpacked
1a60e 52 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 61 73 73 Record */.. ass
1a60f 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 ert( p->magic==V
1a610 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b DBE_MAGIC_RUN );
1a611 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 /* sqlite3_ste
1a612 70 28 29 20 76 65 72 69 66 69 65 73 20 74 68 69 p() verifies thi
1a613 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 s */. assert( d
1a614 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 b->magic==SQLITE
1a615 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 _MAGIC_BUSY );.
1a616 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
1a617 65 78 41 72 72 61 79 45 6e 74 65 72 28 26 70 2d exArrayEnter(&p-
1a618 3e 61 4d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 >aMutex);. if(
1a619 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f p->rc==SQLITE_NO
1a61a 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 MEM ){. /* Th
1a61b 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61 20 is happens if a
1a61c 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 malloc() inside
1a61d 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
1a61e 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 3_column_text()
1a61f 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 or. ** sqlite
1a620 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
1a621 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 ) failed. */.
1a622 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1a623 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e }. assert( p->
1a624 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
1a625 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 p->rc==SQLITE_B
1a626 55 53 59 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d USY );. p->rc =
1a627 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 SQLITE_OK;. as
1a628 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e sert( p->explain
1a629 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 70 52 65 73 ==0 );. p->pRes
1a62a 75 6c 74 53 65 74 20 3d 20 30 3b 0a 20 20 64 62 ultSet = 0;. db
1a62b 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42 ->busyHandler.nB
1a62c 75 73 79 20 3d 20 30 3b 0a 20 20 43 48 45 43 4b usy = 0;. CHECK
1a62d 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a _FOR_INTERRUPT;.
1a62e 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 sqlite3VdbeIOT
1a62f 72 61 63 65 53 71 6c 28 70 29 3b 0a 23 69 66 64 raceSql(p);.#ifd
1a630 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1a631 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
1a632 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
1a633 69 66 28 20 70 2d 3e 70 63 3d 3d 30 20 0a 20 20 if( p->pc==0 .
1a634 20 26 26 20 28 28 70 2d 3e 64 62 2d 3e 66 6c 61 && ((p->db->fla
1a635 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65 gs & SQLITE_Vdbe
1a636 4c 69 73 74 69 6e 67 29 20 7c 7c 20 66 69 6c 65 Listing) || file
1a637 45 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 Exists(db, "vdbe
1a638 5f 65 78 70 6c 61 69 6e 22 29 29 0a 20 20 29 7b _explain")). ){
1a639 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1a63a 70 72 69 6e 74 66 28 22 56 44 42 45 20 50 72 6f printf("VDBE Pro
1a63b 67 72 61 6d 20 4c 69 73 74 69 6e 67 3a 5c 6e 22 gram Listing:\n"
1a63c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1a63d 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 bePrintSql(p);.
1a63e 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d for(i=0; i<p-
1a63f 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nOp; i++){.
1a640 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1a641 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 69 2c 20 ntOp(stdout, i,
1a642 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 &p->aOp[i]);.
1a643 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 66 69 6c }. }. if( fil
1a644 65 45 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 eExists(db, "vdb
1a645 65 5f 74 72 61 63 65 22 29 20 29 7b 0a 20 20 20 e_trace") ){.
1a646 20 70 2d 3e 74 72 61 63 65 20 3d 20 73 74 64 6f p->trace = stdo
1a647 75 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ut;. }. sqlite
1a648 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
1a649 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 ();.#endif. for
1a64a 28 70 63 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d 53 (pc=p->pc; rc==S
1a64b 51 4c 49 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b QLITE_OK; pc++){
1a64c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e . assert( pc>
1a64d 3d 30 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 =0 && pc<p->nOp
1a64e 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d );. if( db->m
1a64f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f allocFailed ) go
1a650 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65 to no_mem;.#ifde
1a651 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 f VDBE_PROFILE.
1a652 20 20 20 6f 72 69 67 50 63 20 3d 20 70 63 3b 0a origPc = pc;.
1a653 20 20 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69 start = sqli
1a654 74 65 33 48 77 74 69 6d 65 28 29 3b 0a 23 65 6e te3Hwtime();.#en
1a655 64 69 66 0a 20 20 20 20 70 4f 70 20 3d 20 26 70 dif. pOp = &p
1a656 2d 3e 61 4f 70 5b 70 63 5d 3b 0a 0a 20 20 20 20 ->aOp[pc];..
1a657 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f 77 20 74 72 /* Only allow tr
1a658 61 63 69 6e 67 20 69 66 20 53 51 4c 49 54 45 5f acing if SQLITE_
1a659 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 DEBUG is defined
1a65a 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 .. */.#ifdef
1a65b 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
1a65c 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b if( p->trace ){
1a65d 0a 20 20 20 20 20 20 69 66 28 20 70 63 3d 3d 30 . if( pc==0
1a65e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 69 6e ){. prin
1a65f 74 66 28 22 56 44 42 45 20 45 78 65 63 75 74 69 tf("VDBE Executi
1a660 6f 6e 20 54 72 61 63 65 3a 5c 6e 22 29 3b 0a 20 on Trace:\n");.
1a661 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1a662 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 bePrintSql(p);.
1a663 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
1a664 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 ite3VdbePrintOp(
1a665 70 2d 3e 74 72 61 63 65 2c 20 70 63 2c 20 70 4f p->trace, pc, pO
1a666 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 p);. }. if
1a667 28 20 70 2d 3e 74 72 61 63 65 3d 3d 30 20 26 26 ( p->trace==0 &&
1a668 20 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pc==0 ){.
1a669 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
1a66a 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
1a66b 20 20 69 66 28 20 66 69 6c 65 45 78 69 73 74 73 if( fileExists
1a66c 28 64 62 2c 20 22 76 64 62 65 5f 73 71 6c 74 72 (db, "vdbe_sqltr
1a66d 61 63 65 22 29 20 29 7b 0a 20 20 20 20 20 20 20 ace") ){.
1a66e 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
1a66f 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d tSql(p);. }
1a670 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e . sqlite3En
1a671 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
1a672 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1a673 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68 65 .. /* Che
1a674 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 ck to see if we
1a675 6e 65 65 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 need to simulate
1a676 20 61 6e 20 69 6e 74 65 72 72 75 70 74 2e 20 20 an interrupt.
1a677 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e This only happen
1a678 73 0a 20 20 20 20 2a 2a 20 69 66 20 77 65 20 68 s. ** if we h
1a679 61 76 65 20 61 20 73 70 65 63 69 61 6c 20 74 65 ave a special te
1a67a 73 74 20 62 75 69 6c 64 2e 0a 20 20 20 20 2a 2f st build.. */
1a67b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1a67c 45 53 54 0a 20 20 20 20 69 66 28 20 73 71 6c 69 EST. if( sqli
1a67d 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f te3_interrupt_co
1a67e 75 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 unt>0 ){. s
1a67f 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
1a680 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 _count--;.
1a681 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 if( sqlite3_inte
1a682 72 72 75 70 74 5f 63 6f 75 6e 74 3d 3d 30 20 29 rrupt_count==0 )
1a683 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1a684 33 5f 69 6e 74 65 72 72 75 70 74 28 64 62 29 3b 3_interrupt(db);
1a685 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
1a686 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
1a687 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 QLITE_OMIT_PROGR
1a688 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 20 ESS_CALLBACK.
1a689 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70 72 6f /* Call the pro
1a68a 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 gress callback i
1a68b 66 20 69 74 20 69 73 20 63 6f 6e 66 69 67 75 72 f it is configur
1a68c 65 64 20 61 6e 64 20 74 68 65 20 72 65 71 75 69 ed and the requi
1a68d 72 65 64 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a red number. *
1a68e 2a 20 6f 66 20 56 44 42 45 20 6f 70 73 20 68 61 * of VDBE ops ha
1a68f 76 65 20 62 65 65 6e 20 65 78 65 63 75 74 65 64 ve been executed
1a690 20 28 65 69 74 68 65 72 20 73 69 6e 63 65 20 74 (either since t
1a691 68 69 73 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f his invocation o
1a692 66 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 f. ** sqlite3
1a693 56 64 62 65 45 78 65 63 28 29 20 6f 72 20 73 69 VdbeExec() or si
1a694 6e 63 65 20 6c 61 73 74 20 74 69 6d 65 20 74 68 nce last time th
1a695 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
1a696 61 63 6b 20 77 61 73 20 63 61 6c 6c 65 64 29 2e ack was called).
1a697 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70 . ** If the p
1a698 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b rogress callback
1a699 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
1a69a 6f 2c 20 65 78 69 74 20 74 68 65 20 76 69 72 74 o, exit the virt
1a69b 75 61 6c 20 6d 61 63 68 69 6e 65 20 77 69 74 68 ual machine with
1a69c 0a 20 20 20 20 2a 2a 20 61 20 72 65 74 75 72 6e . ** a return
1a69d 20 63 6f 64 65 20 53 51 4c 49 54 45 5f 41 42 4f code SQLITE_ABO
1a69e 52 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 RT.. */. i
1a69f 66 28 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 f( db->xProgress
1a6a0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62 ){. if( db
1a6a1 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73 3d 3d ->nProgressOps==
1a6a2 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 29 7b 0a nProgressOps ){.
1a6a3 20 20 20 20 20 20 20 20 69 6e 74 20 70 72 63 3b int prc;
1a6a4 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
1a6a5 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1a6a6 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1a6a7 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 ue_to_misuse;.
1a6a8 20 20 20 20 20 20 70 72 63 20 3d 64 62 2d 3e 78 prc =db->x
1a6a9 50 72 6f 67 72 65 73 73 28 64 62 2d 3e 70 50 72 Progress(db->pPr
1a6aa 6f 67 72 65 73 73 41 72 67 29 3b 0a 20 20 20 20 ogressArg);.
1a6ab 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1a6ac 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f afetyOn(db) ) go
1a6ad 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1a6ae 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20 misuse;.
1a6af 69 66 28 20 70 72 63 21 3d 30 20 29 7b 0a 20 20 if( prc!=0 ){.
1a6b0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1a6b1 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 ITE_INTERRUPT;.
1a6b2 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 goto vd
1a6b3 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 20 be_error_halt;.
1a6b4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1a6b5 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 nProgressOps =
1a6b6 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
1a6b7 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b nProgressOps++;
1a6b8 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
1a6b9 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 /* Do common
1a6ba 73 65 74 75 70 20 70 72 6f 63 65 73 73 69 6e 67 setup processing
1a6bb 20 66 6f 72 20 61 6e 79 20 6f 70 63 6f 64 65 20 for any opcode
1a6bc 74 68 61 74 20 69 73 20 6d 61 72 6b 65 64 0a 20 that is marked.
1a6bd 20 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 22 ** with the "
1a6be 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 22 out2-prerelease"
1a6bf 20 74 61 67 2e 20 20 53 75 63 68 20 6f 70 63 6f tag. Such opco
1a6c0 64 65 73 20 68 61 76 65 20 61 20 73 69 6e 67 6c des have a singl
1a6c1 65 0a 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 e. ** output
1a6c2 77 68 69 63 68 20 69 73 20 73 70 65 63 69 66 69 which is specifi
1a6c3 65 64 20 62 79 20 74 68 65 20 50 32 20 70 61 72 ed by the P2 par
1a6c4 61 6d 65 74 65 72 2e 20 20 54 68 65 20 50 32 20 ameter. The P2
1a6c5 72 65 67 69 73 74 65 72 0a 20 20 20 20 2a 2a 20 register. **
1a6c6 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 is initialized t
1a6c7 6f 20 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f o a NULL.. */
1a6c8 0a 20 20 20 20 6f 70 50 72 6f 70 65 72 74 79 20 . opProperty
1a6c9 3d 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 = opcodeProperty
1a6ca 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20 [pOp->opcode];.
1a6cb 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 if( (opProper
1a6cc 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f ty & OPFLG_OUT2_
1a6cd 50 52 45 52 45 4c 45 41 53 45 29 21 3d 30 20 29 PRERELEASE)!=0 )
1a6ce 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1a6cf 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 pOp->p2>0 );.
1a6d0 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1a6d1 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p2<=p->nMem );.
1a6d2 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e pOut = &p->
1a6d3 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1a6d4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1a6d5 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e MemReleaseExtern
1a6d6 61 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 al(pOut);.
1a6d7 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pOut->flags = ME
1a6d8 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 M_Null;. }els
1a6d9 65 0a 20 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f e. . /* Do co
1a6da 6d 6d 6f 6e 20 73 65 74 75 70 20 66 6f 72 20 6f mmon setup for o
1a6db 70 63 6f 64 65 73 20 6d 61 72 6b 65 64 20 77 69 pcodes marked wi
1a6dc 74 68 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f th one of the fo
1a6dd 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 llowing. ** c
1a6de 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70 ombinations of p
1a6df 72 6f 70 65 72 74 69 65 73 2e 0a 20 20 20 20 2a roperties.. *
1a6e0 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 *. **
1a6e1 20 20 20 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20 in1. **
1a6e2 20 20 20 20 20 20 20 20 69 6e 31 20 69 6e 32 0a in1 in2.
1a6e3 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
1a6e4 20 69 6e 31 20 69 6e 32 20 6f 75 74 33 0a 20 20 in1 in2 out3.
1a6e5 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 ** i
1a6e6 6e 31 20 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 n1 in3. **.
1a6e7 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 73 20 70 ** Variables p
1a6e8 49 6e 31 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70 In1, pIn2, and p
1a6e9 49 6e 33 20 61 72 65 20 6d 61 64 65 20 74 6f 20 In3 are made to
1a6ea 70 6f 69 6e 74 20 74 6f 20 61 70 70 72 6f 70 72 point to appropr
1a6eb 69 61 74 65 0a 20 20 20 20 2a 2a 20 72 65 67 69 iate. ** regi
1a6ec 73 74 65 72 73 20 66 6f 72 20 69 6e 70 75 74 73 sters for inputs
1a6ed 2e 20 20 56 61 72 69 61 62 6c 65 20 70 4f 75 74 . Variable pOut
1a6ee 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6f points to the o
1a6ef 75 74 70 75 74 20 72 65 67 69 73 74 65 72 2e 0a utput register..
1a6f0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 */. if( (
1a6f1 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 opProperty & OPF
1a6f2 4c 47 5f 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20 LG_IN1)!=0 ){.
1a6f3 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1a6f4 3e 70 31 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 >p1>0 );. a
1a6f5 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d ssert( pOp->p1<=
1a6f6 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 p->nMem );.
1a6f7 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d pIn1 = &p->aMem
1a6f8 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 [pOp->p1];.
1a6f9 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1a6fa 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a pOp->p1, pIn1);.
1a6fb 20 20 20 20 20 20 69 66 28 20 28 6f 70 50 72 6f if( (opPro
1a6fc 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e perty & OPFLG_IN
1a6fd 32 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 2)!=0 ){.
1a6fe 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1a6ff 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 >0 );. as
1a700 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 sert( pOp->p2<=p
1a701 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 ->nMem );.
1a702 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 pIn2 = &p->aMe
1a703 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 m[pOp->p2];.
1a704 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1a705 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 CE(pOp->p2, pIn2
1a706 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 );. if( (
1a707 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 opProperty & OPF
1a708 4c 47 5f 4f 55 54 33 29 21 3d 30 20 29 7b 0a 20 LG_OUT3)!=0 ){.
1a709 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
1a70a 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 pOp->p3>0 );.
1a70b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1a70c 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p3<=p->nMem
1a70d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f );. pO
1a70e 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1a70f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20 20 p->p3];.
1a710 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }. }else if
1a711 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 ( (opProperty &
1a712 4f 50 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b OPFLG_IN3)!=0 ){
1a713 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1a714 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 pOp->p3>0 );.
1a715 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1a716 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
1a717 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 33 20 3d ;. pIn3 =
1a718 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1a719 33 5d 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 3];. REGI
1a71a 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1a71b 70 33 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20 20 p3, pIn3);.
1a71c 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
1a71d 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f (opProperty & O
1a71e 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a PFLG_IN2)!=0 ){.
1a71f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1a720 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 p->p2>0 );.
1a721 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1a722 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 <=p->nMem );.
1a723 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d pIn2 = &p->aM
1a724 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 em[pOp->p2];.
1a725 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 REGISTER_TRAC
1a726 45 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 E(pOp->p2, pIn2)
1a727 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1a728 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 (opProperty & OP
1a729 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 FLG_IN3)!=0 ){.
1a72a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1a72b 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 ->p3>0 );.
1a72c 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c assert( pOp->p3<
1a72d 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 =p->nMem );.
1a72e 20 20 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 pIn3 = &p->aMe
1a72f 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 m[pOp->p3];.
1a730 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1a731 28 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b (pOp->p3, pIn3);
1a732 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 . }.. swit
1a733 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 ch( pOp->opcode
1a734 29 7b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ){../***********
1a735 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a736 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a737 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a738 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a739 2a 2a 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c 6f **.** What follo
1a73a 77 73 20 69 73 20 61 20 6d 61 73 73 69 76 65 20 ws is a massive
1a73b 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 switch statement
1a73c 20 77 68 65 72 65 20 65 61 63 68 20 63 61 73 65 where each case
1a73d 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a implements a.**
1a73e 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 72 75 separate instru
1a73f 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 69 72 ction in the vir
1a740 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 tual machine. I
1a741 66 20 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65 20 f we follow the
1a742 75 73 75 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61 usual.** indenta
1a743 74 69 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 tion conventions
1a744 2c 20 65 61 63 68 20 63 61 73 65 20 73 68 6f 75 , each case shou
1a745 6c 64 20 62 65 20 69 6e 64 65 6e 74 65 64 20 62 ld be indented b
1a746 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75 74 y 6 spaces. But
1a747 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 20 6c 6f .** that is a lo
1a748 74 20 6f 66 20 77 61 73 74 65 64 20 73 70 61 63 t of wasted spac
1a749 65 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 6d 61 e on the left ma
1a74a 72 67 69 6e 2e 20 20 53 6f 20 74 68 65 20 63 6f rgin. So the co
1a74b 64 65 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 de within.** the
1a74c 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e switch statemen
1a74d 74 20 77 69 6c 6c 20 62 72 65 61 6b 20 77 69 74 t will break wit
1a74e 68 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e 64 h convention and
1a74f 20 62 65 20 66 6c 75 73 68 2d 6c 65 66 74 2e 20 be flush-left.
1a750 41 6e 6f 74 68 65 72 0a 2a 2a 20 62 69 67 20 63 Another.** big c
1a751 6f 6d 6d 65 6e 74 20 28 73 69 6d 69 6c 61 72 20 omment (similar
1a752 74 6f 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c to this one) wil
1a753 6c 20 6d 61 72 6b 20 74 68 65 20 70 6f 69 6e 74 l mark the point
1a754 20 69 6e 20 74 68 65 20 63 6f 64 65 20 77 68 65 in the code whe
1a755 72 65 0a 2a 2a 20 77 65 20 74 72 61 6e 73 69 74 re.** we transit
1a756 69 6f 6e 20 62 61 63 6b 20 74 6f 20 6e 6f 72 6d ion back to norm
1a757 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a al indentation..
1a758 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 **.** The format
1a759 74 69 6e 67 20 6f 66 20 65 61 63 68 20 63 61 73 ting of each cas
1a75a 65 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 e is important.
1a75b 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 66 6f The makefile fo
1a75c 72 20 53 51 4c 69 74 65 0a 2a 2a 20 67 65 6e 65 r SQLite.** gene
1a75d 72 61 74 65 73 20 74 77 6f 20 43 20 66 69 6c 65 rates two C file
1a75e 73 20 22 6f 70 63 6f 64 65 73 2e 68 22 20 61 6e s "opcodes.h" an
1a75f 64 20 22 6f 70 63 6f 64 65 73 2e 63 22 20 62 79 d "opcodes.c" by
1a760 20 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 0a 2a scanning this.*
1a761 2a 20 66 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 * file looking f
1a762 6f 72 20 6c 69 6e 65 73 20 74 68 61 74 20 62 65 or lines that be
1a763 67 69 6e 20 77 69 74 68 20 22 63 61 73 65 20 4f gin with "case O
1a764 50 5f 22 2e 20 20 54 68 65 20 6f 70 63 6f 64 65 P_". The opcode
1a765 73 2e 68 20 66 69 6c 65 73 0a 2a 2a 20 77 69 6c s.h files.** wil
1a766 6c 20 62 65 20 66 69 6c 6c 65 64 20 77 69 74 68 l be filled with
1a767 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20 67 #defines that g
1a768 69 76 65 20 75 6e 69 71 75 65 20 69 6e 74 65 67 ive unique integ
1a769 65 72 20 76 61 6c 75 65 73 20 74 6f 20 65 61 63 er values to eac
1a76a 68 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 6e 64 20 h.** opcode and
1a76b 74 68 65 20 6f 70 63 6f 64 65 73 2e 63 20 66 69 the opcodes.c fi
1a76c 6c 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 le is filled wit
1a76d 68 20 61 6e 20 61 72 72 61 79 20 6f 66 20 73 74 h an array of st
1a76e 72 69 6e 67 73 20 77 68 65 72 65 0a 2a 2a 20 65 rings where.** e
1a76f 61 63 68 20 73 74 72 69 6e 67 20 69 73 20 74 68 ach string is th
1a770 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 e symbolic name
1a771 66 6f 72 20 74 68 65 20 63 6f 72 72 65 73 70 6f for the correspo
1a772 6e 64 69 6e 67 20 6f 70 63 6f 64 65 2e 20 20 49 nding opcode. I
1a773 66 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 73 74 f the.** case st
1a774 61 74 65 6d 65 6e 74 20 69 73 20 66 6f 6c 6c 6f atement is follo
1a775 77 65 64 20 62 79 20 61 20 63 6f 6d 6d 65 6e 74 wed by a comment
1a776 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2f 23 of the form "/#
1a777 20 73 61 6d 65 20 61 73 20 2e 2e 2e 20 23 2f 22 same as ... #/"
1a778 0a 2a 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e 74 .** that comment
1a779 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 is used to dete
1a77a 72 6d 69 6e 65 20 74 68 65 20 70 61 72 74 69 63 rmine the partic
1a77b 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68 ular value of th
1a77c 65 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 e opcode..**.**
1a77d 4f 74 68 65 72 20 6b 65 79 77 6f 72 64 73 20 69 Other keywords i
1a77e 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 68 n the comment th
1a77f 61 74 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68 20 at follows each
1a780 63 61 73 65 20 61 72 65 20 75 73 65 64 20 74 6f case are used to
1a781 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20 74 68 .** construct th
1a782 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 e OPFLG_INITIALI
1a783 5a 45 52 20 76 61 6c 75 65 20 74 68 61 74 20 69 ZER value that i
1a784 6e 69 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f 64 nitializes opcod
1a785 65 50 72 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20 eProperty[]..**
1a786 4b 65 79 77 6f 72 64 73 20 69 6e 63 6c 75 64 65 Keywords include
1a787 3a 20 69 6e 31 2c 20 69 6e 32 2c 20 69 6e 33 2c : in1, in2, in3,
1a788 20 6f 75 74 32 5f 70 72 65 72 65 6c 65 61 73 65 out2_prerelease
1a789 2c 20 6f 75 74 32 2c 20 6f 75 74 33 2e 20 20 53 , out2, out3. S
1a78a 65 65 0a 2a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f ee.** the mkopco
1a78b 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 deh.awk script f
1a78c 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
1a78d 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a formation..**.**
1a78e 20 44 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 61 Documentation a
1a78f 62 6f 75 74 20 56 44 42 45 20 6f 70 63 6f 64 65 bout VDBE opcode
1a790 73 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 s is generated b
1a791 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 20 y scanning this
1a792 66 69 6c 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65 file.** for line
1a793 73 20 6f 66 20 74 68 61 74 20 63 6f 6e 74 61 69 s of that contai
1a794 6e 20 22 4f 70 63 6f 64 65 3a 22 2e 20 20 54 68 n "Opcode:". Th
1a795 61 74 20 6c 69 6e 65 20 61 6e 64 20 61 6c 6c 20 at line and all
1a796 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 6f subsequent.** co
1a797 6d 6d 65 6e 74 20 6c 69 6e 65 73 20 61 72 65 20 mment lines are
1a798 75 73 65 64 20 69 6e 20 74 68 65 20 67 65 6e 65 used in the gene
1a799 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 ration of the op
1a79a 63 6f 64 65 2e 68 74 6d 6c 20 64 6f 63 75 6d 65 code.html docume
1a79b 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e ntation.** file.
1a79c 0a 2a 2a 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a .**.** SUMMARY:.
1a79d 2a 2a 0a 2a 2a 20 20 20 20 20 46 6f 72 6d 61 74 **.** Format
1a79e 74 69 6e 67 20 69 73 20 69 6d 70 6f 72 74 61 6e ting is importan
1a79f 74 20 74 6f 20 73 63 72 69 70 74 73 20 74 68 61 t to scripts tha
1a7a0 74 20 73 63 61 6e 20 74 68 69 73 20 66 69 6c 65 t scan this file
1a7a1 2e 0a 2a 2a 20 20 20 20 20 44 6f 20 6e 6f 74 20 ..** Do not
1a7a2 64 65 76 69 61 74 65 20 66 72 6f 6d 20 74 68 65 deviate from the
1a7a3 20 66 6f 72 6d 61 74 74 69 6e 67 20 73 74 79 6c formatting styl
1a7a4 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 e currently in u
1a7a5 73 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a se..**.*********
1a7a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a7a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a7a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a7a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a7aa 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ****/../* Opcode
1a7ab 3a 20 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20 2a : Goto * P2 * *
1a7ac 20 2a 0a 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f *.**.** An unco
1a7ad 6e 64 69 74 69 6f 6e 61 6c 20 6a 75 6d 70 20 74 nditional jump t
1a7ae 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a o address P2..**
1a7af 20 54 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 The next instru
1a7b0 63 74 69 6f 6e 20 65 78 65 63 75 74 65 64 20 77 ction executed w
1a7b1 69 6c 6c 20 62 65 20 0a 2a 2a 20 74 68 65 20 6f ill be .** the o
1a7b2 6e 65 20 61 74 20 69 6e 64 65 78 20 50 32 20 66 ne at index P2 f
1a7b3 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rom the beginnin
1a7b4 67 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 67 g of.** the prog
1a7b5 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ram..*/.case OP_
1a7b6 47 6f 74 6f 3a 20 7b 20 20 20 20 20 20 20 20 20 Goto: {
1a7b7 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 /* jump */.
1a7b8 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 CHECK_FOR_INTER
1a7b9 52 55 50 54 3b 0a 20 20 70 63 20 3d 20 70 4f 70 RUPT;. pc = pOp
1a7ba 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 ->p2 - 1;. brea
1a7bb 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1a7bc 20 20 47 6f 73 75 62 20 50 31 20 50 32 20 2a 20 Gosub P1 P2 *
1a7bd 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 * *.**.** Write
1a7be 74 68 65 20 63 75 72 72 65 6e 74 20 61 64 64 72 the current addr
1a7bf 65 73 73 20 6f 6e 74 6f 20 72 65 67 69 73 74 65 ess onto registe
1a7c0 72 20 50 31 0a 2a 2a 20 61 6e 64 20 74 68 65 6e r P1.** and then
1a7c1 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 jump to address
1a7c2 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P2..*/.case OP_
1a7c3 47 6f 73 75 62 3a 20 7b 20 20 20 20 20 20 20 20 Gosub: {
1a7c4 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 /* jump */.
1a7c5 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1a7c6 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
1a7c7 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p1<=p->nMem
1a7c8 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d );. pIn1 = &p-
1a7c9 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
1a7ca 20 20 61 73 73 65 72 74 28 20 28 70 49 6e 31 2d assert( (pIn1-
1a7cb 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e >flags & MEM_Dyn
1a7cc 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e )==0 );. pIn1->
1a7cd 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1a7ce 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 . pIn1->u.i = p
1a7cf 63 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 c;. REGISTER_TR
1a7d0 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e ACE(pOp->p1, pIn
1a7d1 31 29 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 1);. pc = pOp->
1a7d2 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b p2 - 1;. break;
1a7d3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 .}../* Opcode:
1a7d4 52 65 74 75 72 6e 20 50 31 20 2a 20 2a 20 2a 20 Return P1 * * *
1a7d5 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 *.**.** Jump to
1a7d6 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 the next instruc
1a7d7 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20 61 tion after the a
1a7d8 64 64 72 65 73 73 20 69 6e 20 72 65 67 69 73 74 ddress in regist
1a7d9 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f er P1..*/.case O
1a7da 50 5f 52 65 74 75 72 6e 3a 20 7b 20 20 20 20 20 P_Return: {
1a7db 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a /* in1 */.
1a7dc 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e assert( pIn1->
1a7dd 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 flags & MEM_Int
1a7de 29 3b 0a 20 20 70 63 20 3d 20 28 69 6e 74 29 70 );. pc = (int)p
1a7df 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61 In1->u.i;. brea
1a7e0 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1a7e1 20 20 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a Yield P1 * * *
1a7e2 20 2a 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68 *.**.** Swap th
1a7e3 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 e program counte
1a7e4 72 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 r with the value
1a7e5 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e in register P1.
1a7e6 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c .*/.case OP_Yiel
1a7e7 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 d: {
1a7e8 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 /* in1 */. int
1a7e9 70 63 44 65 73 74 3b 0a 20 20 61 73 73 65 72 74 pcDest;. assert
1a7ea 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1a7eb 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a MEM_Dyn)==0 );.
1a7ec 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 pIn1->flags =
1a7ed 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 63 44 65 73 MEM_Int;. pcDes
1a7ee 74 20 3d 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75 t = (int)pIn1->u
1a7ef 2e 69 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 .i;. pIn1->u.i
1a7f0 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54 45 52 = pc;. REGISTER
1a7f1 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 _TRACE(pOp->p1,
1a7f2 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20 70 63 pIn1);. pc = pc
1a7f3 44 65 73 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Dest;. break;.}
1a7f4 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 .../* Opcode: H
1a7f5 61 6c 74 20 50 31 20 50 32 20 2a 20 50 34 20 2a alt P1 P2 * P4 *
1a7f6 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d 6d 65 .**.** Exit imme
1a7f7 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20 6f 70 diately. All op
1a7f8 65 6e 20 63 75 72 73 6f 72 73 2c 20 65 74 63 20 en cursors, etc
1a7f9 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a 20 61 75 are closed.** au
1a7fa 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a tomatically..**.
1a7fb 2a 2a 20 50 31 20 69 73 20 74 68 65 20 72 65 73 ** P1 is the res
1a7fc 75 6c 74 20 63 6f 64 65 20 72 65 74 75 72 6e 65 ult code returne
1a7fd 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65 78 65 d by sqlite3_exe
1a7fe 63 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 c(), sqlite3_res
1a7ff 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69 et(),.** or sqli
1a800 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 2e 20 te3_finalize().
1a801 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c 20 68 61 For a normal ha
1a802 6c 74 2c 20 74 68 69 73 20 73 68 6f 75 6c 64 20 lt, this should
1a803 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 be SQLITE_OK (0)
1a804 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f 72 73 2c ..** For errors,
1a805 20 69 74 20 63 61 6e 20 62 65 20 73 6f 6d 65 20 it can be some
1a806 6f 74 68 65 72 20 76 61 6c 75 65 2e 20 20 49 66 other value. If
1a807 20 50 31 21 3d 30 20 74 68 65 6e 20 50 32 20 77 P1!=0 then P2 w
1a808 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a ill determine.**
1a809 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1a80a 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 to rollback the
1a80b 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
1a80c 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72 6f 6c ion. Do not rol
1a80d 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d lback.** if P2==
1a80e 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74 68 65 20 OE_Fail. Do the
1a80f 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50 32 3d 3d rollback if P2==
1a810 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20 49 66 OE_Rollback. If
1a811 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a P2==OE_Abort,.*
1a812 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f 75 74 20 * then back out
1a813 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 68 61 74 all changes that
1a814 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 64 have occurred d
1a815 75 72 69 6e 67 20 74 68 69 73 20 65 78 65 63 75 uring this execu
1a816 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 56 tion of the.** V
1a817 44 42 45 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 DBE, but do not
1a818 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 rollback the tra
1a819 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a nsaction. .**.**
1a81a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 6e 75 If P4 is not nu
1a81b 6c 6c 20 74 68 65 6e 20 69 74 20 69 73 20 61 6e ll then it is an
1a81c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 error message s
1a81d 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tring..**.** The
1a81e 72 65 20 69 73 20 61 6e 20 69 6d 70 6c 69 65 64 re is an implied
1a81f 20 22 48 61 6c 74 20 30 20 30 20 30 22 20 69 6e "Halt 0 0 0" in
1a820 73 74 72 75 63 74 69 6f 6e 20 69 6e 73 65 72 74 struction insert
1a821 65 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65 ed at the very e
1a822 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72 79 20 70 nd of.** every p
1a823 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61 20 6a 75 rogram. So a ju
1a824 6d 70 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 mp past the last
1a825 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 instruction of
1a826 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 the program.** i
1a827 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 65 78 s the same as ex
1a828 65 63 75 74 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f ecuting Halt..*/
1a829 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 3a 20 7b .case OP_Halt: {
1a82a 0a 20 20 70 2d 3e 72 63 20 3d 20 70 4f 70 2d 3e . p->rc = pOp->
1a82b 70 31 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 p1;. p->pc = pc
1a82c 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 ;. p->errorActi
1a82d 6f 6e 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 on = pOp->p2;.
1a82e 69 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b if( pOp->p4.z ){
1a82f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1a830 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1a831 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 70 4f 70 g, db, "%s", pOp
1a832 2d 3e 70 34 2e 7a 29 3b 0a 20 20 7d 0a 20 20 72 ->p4.z);. }. r
1a833 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 48 c = sqlite3VdbeH
1a834 61 6c 74 28 70 29 3b 0a 20 20 61 73 73 65 72 74 alt(p);. assert
1a835 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 ( rc==SQLITE_BUS
1a836 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f Y || rc==SQLITE_
1a837 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d OK );. if( rc==
1a838 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 SQLITE_BUSY ){.
1a839 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 p->rc = rc =
1a83a 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
1a83b 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 else{. rc = p
1a83c 2d 3e 72 63 20 3f 20 53 51 4c 49 54 45 5f 45 52 ->rc ? SQLITE_ER
1a83d 52 4f 52 20 3a 20 53 51 4c 49 54 45 5f 44 4f 4e ROR : SQLITE_DON
1a83e 45 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 E;. }. goto vd
1a83f 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a be_return;.}../*
1a840 20 4f 70 63 6f 64 65 3a 20 49 6e 74 65 67 65 72 Opcode: Integer
1a841 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1a842 2a 2a 20 54 68 65 20 33 32 2d 62 69 74 20 69 6e ** The 32-bit in
1a843 74 65 67 65 72 20 76 61 6c 75 65 20 50 31 20 69 teger value P1 i
1a844 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 72 s written into r
1a845 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 egister P2..*/.c
1a846 61 73 65 20 4f 50 5f 49 6e 74 65 67 65 72 3a 20 ase OP_Integer:
1a847 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 { /* out
1a848 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 2-prerelease */.
1a849 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 pOut->flags =
1a84a 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d MEM_Int;. pOut-
1a84b 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a >u.i = pOp->p1;.
1a84c 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1a84d 70 63 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 50 pcode: Int64 * P
1a84e 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 2 * P4 *.**.** P
1a84f 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
1a850 6f 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 o a 64-bit integ
1a851 65 72 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 er value..** Wri
1a852 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e te that value in
1a853 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
1a854 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 34 */.case OP_Int64
1a855 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a : { /*
1a856 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
1a857 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f */. assert( pO
1a858 70 2d 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 3b p->p4.pI64!=0 );
1a859 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d . pOut->flags =
1a85a 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 MEM_Int;. pOut
1a85b 2d 3e 75 2e 69 20 3d 20 2a 70 4f 70 2d 3e 70 34 ->u.i = *pOp->p4
1a85c 2e 70 49 36 34 3b 0a 20 20 62 72 65 61 6b 3b 0a .pI64;. break;.
1a85d 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 }../* Opcode: Re
1a85e 61 6c 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a al * P2 * P4 *.*
1a85f 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 *.** P4 is a poi
1a860 6e 74 65 72 20 74 6f 20 61 20 36 34 2d 62 69 74 nter to a 64-bit
1a861 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
1a862 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20 value..** Write
1a863 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 that value into
1a864 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a register P2..*/.
1a865 63 61 73 65 20 4f 50 5f 52 65 61 6c 3a 20 7b 20 case OP_Real: {
1a866 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1a867 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 2c 20 me as TK_FLOAT,
1a868 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1a869 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 */. pOut->flags
1a86a 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 61 = MEM_Real;. a
1a86b 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 49 ssert( !sqlite3I
1a86c 73 4e 61 4e 28 2a 70 4f 70 2d 3e 70 34 2e 70 52 sNaN(*pOp->p4.pR
1a86d 65 61 6c 29 20 29 3b 0a 20 20 70 4f 75 74 2d 3e eal) );. pOut->
1a86e 72 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 r = *pOp->p4.pRe
1a86f 61 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a al;. break;.}..
1a870 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e /* Opcode: Strin
1a871 67 38 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a g8 * P2 * P4 *.*
1a872 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 *.** P4 points t
1a873 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 o a nul terminat
1a874 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e ed UTF-8 string.
1a875 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 This opcode is
1a876 74 72 61 6e 73 66 6f 72 6d 65 64 20 0a 2a 2a 20 transformed .**
1a877 69 6e 74 6f 20 61 6e 20 4f 50 5f 53 74 72 69 6e into an OP_Strin
1a878 67 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 65 g before it is e
1a879 78 65 63 75 74 65 64 20 66 6f 72 20 74 68 65 20 xecuted for the
1a87a 66 69 72 73 74 20 74 69 6d 65 2e 0a 2a 2f 0a 63 first time..*/.c
1a87b 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 38 3a 20 ase OP_String8:
1a87c 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d { /* sam
1a87d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 2c 20 e as TK_STRING,
1a87e 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1a87f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1a880 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 ->p4.z!=0 );. p
1a881 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f Op->opcode = OP_
1a882 53 74 72 69 6e 67 3b 0a 20 20 70 4f 70 2d 3e 70 String;. pOp->p
1a883 31 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 1 = sqlite3Strle
1a884 6e 33 30 28 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a n30(pOp->p4.z);.
1a885 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a886 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28 OMIT_UTF16. if(
1a887 20 65 6e 63 6f 64 69 6e 67 21 3d 53 51 4c 49 54 encoding!=SQLIT
1a888 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 73 71 E_UTF8 ){. sq
1a889 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
1a88a 74 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 tr(pOut, pOp->p4
1a88b 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 .z, -1, SQLITE_U
1a88c 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 TF8, SQLITE_STAT
1a88d 49 43 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c IC);. if( SQL
1a88e 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56 ITE_OK!=sqlite3V
1a88f 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
1a890 67 28 70 4f 75 74 2c 20 65 6e 63 6f 64 69 6e 67 g(pOut, encoding
1a891 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b ) ) goto no_mem;
1a892 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f . if( SQLITE_
1a893 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64 62 65 4d OK!=sqlite3VdbeM
1a894 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
1a895 70 4f 75 74 29 20 29 20 67 6f 74 6f 20 6e 6f 5f pOut) ) goto no_
1a896 6d 65 6d 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a mem;. pOut->z
1a897 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 Malloc = 0;.
1a898 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d pOut->flags |= M
1a899 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 EM_Static;. p
1a89a 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d Out->flags &= ~M
1a89b 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 EM_Dyn;. if(
1a89c 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1a89d 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 DYNAMIC ){.
1a89e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1a89f 62 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 b, pOp->p4.z);.
1a8a0 20 20 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 }. pOp->p4
1a8a1 74 79 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 type = P4_DYNAMI
1a8a2 43 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a C;. pOp->p4.z
1a8a3 20 3d 20 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 = pOut->z;.
1a8a4 70 4f 70 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e pOp->p1 = pOut->
1a8a5 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e n;. if( pOp->
1a8a6 70 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 p1>db->aLimit[SQ
1a8a7 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1a8a8 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f H] ){. goto
1a8a9 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a too_big;. }.
1a8aa 20 20 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 UPDATE_MAX_B
1a8ab 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 LOBSIZE(pOut);.
1a8ac 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 23 65 break;. }.#e
1a8ad 6e 64 69 66 0a 20 20 69 66 28 20 70 4f 70 2d 3e ndif. if( pOp->
1a8ae 70 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 p1>db->aLimit[SQ
1a8af 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1a8b0 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 H] ){. goto t
1a8b1 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 2f 2a oo_big;. }. /*
1a8b2 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f Fall through to
1a8b3 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 2c 20 the next case,
1a8b4 4f 50 5f 53 74 72 69 6e 67 20 2a 2f 0a 7d 0a 20 OP_String */.}.
1a8b5 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72 ./* Opcode: Str
1a8b6 69 6e 67 20 50 31 20 50 32 20 2a 20 50 34 20 2a ing P1 P2 * P4 *
1a8b7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e .**.** The strin
1a8b8 67 20 76 61 6c 75 65 20 50 34 20 6f 66 20 6c 65 g value P4 of le
1a8b9 6e 67 74 68 20 50 31 20 28 62 79 74 65 73 29 20 ngth P1 (bytes)
1a8ba 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 is stored in reg
1a8bb 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 ister P2..*/.cas
1a8bc 65 20 4f 50 5f 53 74 72 69 6e 67 3a 20 7b 20 20 e OP_String: {
1a8bd 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1a8be 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 prerelease */.
1a8bf 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e assert( pOp->p4.
1a8c0 7a 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e z!=0 );. pOut->
1a8c1 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c flags = MEM_Str|
1a8c2 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 54 MEM_Static|MEM_T
1a8c3 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e 7a 20 3d erm;. pOut->z =
1a8c4 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 70 4f pOp->p4.z;. pO
1a8c5 75 74 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70 31 3b ut->n = pOp->p1;
1a8c6 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 . pOut->enc = e
1a8c7 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 ncoding;. UPDAT
1a8c8 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
1a8c9 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
1a8ca 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c ../* Opcode: Nul
1a8cb 6c 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a l * P2 * * *.**.
1a8cc 2a 2a 20 57 72 69 74 65 20 61 20 4e 55 4c 4c 20 ** Write a NULL
1a8cd 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 into register P2
1a8ce 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c ..*/.case OP_Nul
1a8cf 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f l: { /
1a8d0 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1a8d1 65 20 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a e */. break;.}.
1a8d2 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c 6f ../* Opcode: Blo
1a8d3 62 20 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a 0a b P1 P2 * P4.**.
1a8d4 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 ** P4 points to
1a8d5 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 50 a blob of data P
1a8d6 31 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 53 1 bytes long. S
1a8d7 74 6f 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c 6f tore this.** blo
1a8d8 62 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 b in register P2
1a8d9 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 . This instructi
1a8da 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 20 on is not coded
1a8db 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 74 directly.** by t
1a8dc 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e 73 he compiler. Ins
1a8dd 74 65 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 6c tead, the compil
1a8de 65 72 20 6c 61 79 65 72 20 73 70 65 63 69 66 69 er layer specifi
1a8df 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 42 es.** an OP_HexB
1a8e0 6c 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 68 lob opcode, with
1a8e1 20 74 68 65 20 68 65 78 20 73 74 72 69 6e 67 20 the hex string
1a8e2 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
1a8e3 66 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 73 f.** the blob as
1a8e4 20 50 34 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 P4. This opcode
1a8e5 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 is transformed
1a8e6 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a to an OP_Blob.**
1a8e7 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 the first time
1a8e8 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a it is executed..
1a8e9 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 3a */.case OP_Blob:
1a8ea 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1a8eb 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1a8ec 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 ease */. assert
1a8ed 28 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 4c ( pOp->p1 <= SQL
1a8ee 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 ITE_MAX_LENGTH )
1a8ef 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1a8f0 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 emSetStr(pOut, p
1a8f1 4f 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70 Op->p4.z, pOp->p
1a8f2 31 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 74 1, 0, 0);. pOut
1a8f3 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 ->enc = encoding
1a8f4 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 ;. UPDATE_MAX_B
1a8f5 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 LOBSIZE(pOut);.
1a8f6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1a8f7 63 6f 64 65 3a 20 56 61 72 69 61 62 6c 65 20 50 code: Variable P
1a8f8 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1a8f9 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 76 61 The value of va
1a8fa 72 69 61 62 6c 65 20 50 31 20 69 73 20 77 72 69 riable P1 is wri
1a8fb 74 74 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 tten into regist
1a8fc 65 72 20 50 32 2e 20 41 20 76 61 72 69 61 62 6c er P2. A variabl
1a8fd 65 20 69 73 0a 2a 2a 20 61 6e 20 75 6e 6b 6e 6f e is.** an unkno
1a8fe 77 6e 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e wn in the origin
1a8ff 61 6c 20 53 51 4c 20 73 74 72 69 6e 67 20 61 73 al SQL string as
1a900 20 68 61 6e 64 65 64 20 74 6f 20 73 71 6c 69 74 handed to sqlit
1a901 65 33 5f 63 6f 6d 70 69 6c 65 28 29 2e 0a 2a 2a e3_compile()..**
1a902 20 41 6e 79 20 6f 63 63 75 72 72 65 6e 63 65 20 Any occurrence
1a903 6f 66 20 74 68 65 20 27 3f 27 20 63 68 61 72 61 of the '?' chara
1a904 63 74 65 72 20 69 6e 20 74 68 65 20 6f 72 69 67 cter in the orig
1a905 69 6e 61 6c 20 53 51 4c 20 69 73 20 63 6f 6e 73 inal SQL is cons
1a906 69 64 65 72 65 64 0a 2a 2a 20 61 20 76 61 72 69 idered.** a vari
1a907 61 62 6c 65 2e 20 20 56 61 72 69 61 62 6c 65 73 able. Variables
1a908 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74 72 69 in the SQL stri
1a909 6e 67 20 61 72 65 20 6e 75 6d 62 65 72 20 66 72 ng are number fr
1a90a 6f 6d 20 6c 65 66 74 20 74 6f 0a 2a 2a 20 72 69 om left to.** ri
1a90b 67 68 74 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 ght beginning wi
1a90c 74 68 20 31 2e 20 20 54 68 65 20 76 61 6c 75 65 th 1. The value
1a90d 73 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 61 s of variables a
1a90e 72 65 20 73 65 74 20 75 73 69 6e 67 20 74 68 65 re set using the
1a90f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 .** sqlite3_bind
1a910 28 29 20 41 50 49 2e 0a 2a 2f 0a 63 61 73 65 20 () API..*/.case
1a911 4f 50 5f 56 61 72 69 61 62 6c 65 3a 20 7b 20 20 OP_Variable: {
1a912 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 /* out2
1a913 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 -prerelease */.
1a914 20 69 6e 74 20 6a 20 3d 20 70 4f 70 2d 3e 70 31 int j = pOp->p1
1a915 20 2d 20 31 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 - 1;. Mem *pVa
1a916 72 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3e 3d r;. assert( j>=
1a917 30 20 26 26 20 6a 3c 70 2d 3e 6e 56 61 72 20 29 0 && j<p->nVar )
1a918 3b 0a 0a 20 20 70 56 61 72 20 3d 20 26 70 2d 3e ;.. pVar = &p->
1a919 61 56 61 72 5b 6a 5d 3b 0a 20 20 69 66 28 20 73 aVar[j];. if( s
1a91a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f qlite3VdbeMemToo
1a91b 42 69 67 28 70 56 61 72 29 20 29 7b 0a 20 20 20 Big(pVar) ){.
1a91c 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 goto too_big;.
1a91d 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 }. sqlite3Vdbe
1a91e 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 MemShallowCopy(p
1a91f 4f 75 74 2c 20 26 70 2d 3e 61 56 61 72 5b 6a 5d Out, &p->aVar[j]
1a920 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 , MEM_Static);.
1a921 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1a922 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 SIZE(pOut);. br
1a923 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1a924 65 3a 20 4d 6f 76 65 20 50 31 20 50 32 20 50 33 e: Move P1 P2 P3
1a925 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 * *.**.** Move
1a926 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 the values in re
1a927 67 69 73 74 65 72 20 50 31 2e 2e 50 31 2b 50 33 gister P1..P1+P3
1a928 2d 31 20 6f 76 65 72 20 69 6e 74 6f 0a 2a 2a 20 -1 over into.**
1a929 72 65 67 69 73 74 65 72 73 20 50 32 2e 2e 50 32 registers P2..P2
1a92a 2b 50 33 2d 31 2e 20 20 52 65 67 69 73 74 65 72 +P3-1. Register
1a92b 73 20 50 31 2e 2e 50 31 2b 50 31 2d 31 20 61 72 s P1..P1+P1-1 ar
1a92c 65 0a 2a 2a 20 6c 65 66 74 20 68 6f 6c 64 69 6e e.** left holdin
1a92d 67 20 61 20 4e 55 4c 4c 2e 20 20 49 74 20 69 73 g a NULL. It is
1a92e 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20 72 65 an error for re
1a92f 67 69 73 74 65 72 20 72 61 6e 67 65 73 0a 2a 2a gister ranges.**
1a930 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 61 6e 64 P1..P1+P3-1 and
1a931 20 50 32 2e 2e 50 32 2b 50 33 2d 31 20 74 6f 20 P2..P2+P3-1 to
1a932 6f 76 65 72 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65 overlap..*/.case
1a933 20 4f 50 5f 4d 6f 76 65 3a 20 7b 0a 20 20 63 68 OP_Move: {. ch
1a934 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 ar *zMalloc;. i
1a935 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a nt n = pOp->p3;.
1a936 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e int p1 = pOp->
1a937 70 31 3b 0a 20 20 69 6e 74 20 70 32 20 3d 20 70 p1;. int p2 = p
1a938 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 Op->p2;. assert
1a939 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 ( n>0 );. asser
1a93a 74 28 20 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 t( p1>0 );. ass
1a93b 65 72 74 28 20 70 31 2b 6e 3c 70 2d 3e 6e 4d 65 ert( p1+n<p->nMe
1a93c 6d 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 m );. pIn1 = &p
1a93d 2d 3e 61 4d 65 6d 5b 70 31 5d 3b 0a 20 20 61 73 ->aMem[p1];. as
1a93e 73 65 72 74 28 20 70 32 3e 30 20 29 3b 0a 20 20 sert( p2>0 );.
1a93f 61 73 73 65 72 74 28 20 70 32 2b 6e 3c 70 2d 3e assert( p2+n<p->
1a940 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d nMem );. pOut =
1a941 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 5d 3b 0a 20 &p->aMem[p2];.
1a942 20 61 73 73 65 72 74 28 20 70 31 2b 6e 3c 3d 70 assert( p1+n<=p
1a943 32 20 7c 7c 20 70 32 2b 6e 3c 3d 70 31 20 29 3b 2 || p2+n<=p1 );
1a944 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29 7b . while( n-- ){
1a945 0a 20 20 20 20 7a 4d 61 6c 6c 6f 63 20 3d 20 70 . zMalloc = p
1a946 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 Out->zMalloc;.
1a947 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 pOut->zMalloc
1a948 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = 0;. sqlite3
1a949 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 VdbeMemMove(pOut
1a94a 2c 20 70 49 6e 31 29 3b 0a 20 20 20 20 70 49 6e , pIn1);. pIn
1a94b 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a 4d 61 1->zMalloc = zMa
1a94c 6c 6c 6f 63 3b 0a 20 20 20 20 52 45 47 49 53 54 lloc;. REGIST
1a94d 45 52 5f 54 52 41 43 45 28 70 32 2b 2b 2c 20 70 ER_TRACE(p2++, p
1a94e 4f 75 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b Out);. pIn1++
1a94f 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 ;. pOut++;.
1a950 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1a951 20 4f 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31 Opcode: Copy P1
1a952 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1a953 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72 Make a copy of r
1a954 65 67 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20 egister P1 into
1a955 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a register P2..**.
1a956 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 ** This instruct
1a957 69 6f 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70 ion makes a deep
1a958 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c copy of the val
1a959 75 65 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65 ue. A duplicate
1a95a 0a 2a 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61 .** is made of a
1a95b 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f ny string or blo
1a95c 62 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65 b constant. See
1a95d 20 61 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a also OP_SCopy..
1a95e 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a */.case OP_Copy:
1a95f 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
1a960 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 * in1 */. asser
1a961 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a t( pOp->p2>0 );.
1a962 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1a963 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 2<=p->nMem );.
1a964 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pOut = &p->aMem[
1a965 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 pOp->p2];. asse
1a966 72 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 rt( pOut!=pIn1 )
1a967 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1a968 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f emShallowCopy(pO
1a969 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 ut, pIn1, MEM_Ep
1a96a 68 65 6d 29 3b 0a 20 20 44 65 65 70 68 65 6d 65 hem);. Deepheme
1a96b 72 61 6c 69 7a 65 28 70 4f 75 74 29 3b 0a 20 20 ralize(pOut);.
1a96c 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1a96d 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 Op->p2, pOut);.
1a96e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1a96f 63 6f 64 65 3a 20 53 43 6f 70 79 20 50 31 20 50 code: SCopy P1 P
1a970 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 2 * * *.**.** Ma
1a971 6b 65 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 ke a shallow cop
1a972 79 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 y of register P1
1a973 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1a974 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 2..**.** This in
1a975 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 struction makes
1a976 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f a shallow copy o
1a977 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 49 66 f the value. If
1a978 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73 the value.** is
1a979 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f a string or blo
1a97a 62 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 70 79 b, then the copy
1a97b 20 69 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 is only a point
1a97c 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 6f 72 69 er to the.** ori
1a97d 67 69 6e 61 6c 20 61 6e 64 20 68 65 6e 63 65 20 ginal and hence
1a97e 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 if the original
1a97f 63 68 61 6e 67 65 73 20 73 6f 20 77 69 6c 6c 20 changes so will
1a980 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 20 57 6f 72 the copy..** Wor
1a981 73 65 2c 20 69 66 20 74 68 65 20 6f 72 69 67 69 se, if the origi
1a982 6e 61 6c 20 69 73 20 64 65 61 6c 6c 6f 63 61 74 nal is deallocat
1a983 65 64 2c 20 74 68 65 20 63 6f 70 79 20 62 65 63 ed, the copy bec
1a984 6f 6d 65 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a omes invalid..**
1a985 20 54 68 75 73 20 74 68 65 20 70 72 6f 67 72 61 Thus the progra
1a986 6d 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 m must guarantee
1a987 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e that the origin
1a988 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e al will not chan
1a989 67 65 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 ge.** during the
1a98a 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 lifetime of the
1a98b 20 63 6f 70 79 2e 20 20 55 73 65 20 4f 50 5f 43 copy. Use OP_C
1a98c 6f 70 79 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f opy to make a co
1a98d 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 70 79 2e 0a mplete.** copy..
1a98e 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 43 6f 70 79 */.case OP_SCopy
1a98f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f : { /
1a990 2a 20 69 6e 31 20 2a 2f 0a 20 20 52 45 47 49 53 * in1 */. REGIS
1a991 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1a992 31 2c 20 70 49 6e 31 29 3b 0a 20 20 61 73 73 65 1, pIn1);. asse
1a993 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b rt( pOp->p2>0 );
1a994 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1a995 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p2<=p->nMem );.
1a996 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d pOut = &p->aMem
1a997 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 [pOp->p2];. ass
1a998 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 ert( pOut!=pIn1
1a999 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1a99a 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 MemShallowCopy(p
1a99b 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 Out, pIn1, MEM_E
1a99c 70 68 65 6d 29 3b 0a 20 20 52 45 47 49 53 54 45 phem);. REGISTE
1a99d 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c R_TRACE(pOp->p2,
1a99e 20 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b pOut);. break;
1a99f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .}../* Opcode: R
1a9a0 65 73 75 6c 74 52 6f 77 20 50 31 20 50 32 20 2a esultRow P1 P2 *
1a9a1 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 * *.**.** The r
1a9a2 65 67 69 73 74 65 72 73 20 50 31 20 74 68 72 6f egisters P1 thro
1a9a3 75 67 68 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74 ugh P1+P2-1 cont
1a9a4 61 69 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 ain a single row
1a9a5 20 6f 66 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 of.** results.
1a9a6 54 68 69 73 20 6f 70 63 6f 64 65 20 63 61 75 73 This opcode caus
1a9a7 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 es the sqlite3_s
1a9a8 74 65 70 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 tep() call to te
1a9a9 72 6d 69 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20 rminate.** with
1a9aa 61 6e 20 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 an SQLITE_ROW re
1a9ab 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 turn code and it
1a9ac 20 73 65 74 73 20 75 70 20 74 68 65 20 73 71 6c sets up the sql
1a9ad 69 74 65 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 ite3_stmt.** str
1a9ae 75 63 74 75 72 65 20 74 6f 20 70 72 6f 76 69 64 ucture to provid
1a9af 65 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 e access to the
1a9b0 74 6f 70 20 50 31 20 76 61 6c 75 65 73 20 61 73 top P1 values as
1a9b1 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 the result.** r
1a9b2 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 ow..*/.case OP_R
1a9b3 65 73 75 6c 74 52 6f 77 3a 20 7b 0a 20 20 4d 65 esultRow: {. Me
1a9b4 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 69 m *pMem;. int i
1a9b5 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e ;. assert( p->n
1a9b6 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e ResColumn==pOp->
1a9b7 70 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p2 );. assert(
1a9b8 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 pOp->p1>0 );. a
1a9b9 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 2b 70 ssert( pOp->p1+p
1a9ba 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p2<=p->nMem
1a9bb 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 64 );.. /* Invalid
1a9bc 61 74 65 20 61 6c 6c 20 65 70 68 65 6d 65 72 61 ate all ephemera
1a9bd 6c 20 63 75 72 73 6f 72 20 72 6f 77 20 63 61 63 l cursor row cac
1a9be 68 65 73 20 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 hes */. p->cach
1a9bf 65 43 74 72 20 3d 20 28 70 2d 3e 63 61 63 68 65 eCtr = (p->cache
1a9c0 43 74 72 20 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f Ctr + 2)|1;.. /
1a9c1 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
1a9c2 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 63 results of the c
1a9c3 75 72 72 65 6e 74 20 72 6f 77 20 61 72 65 20 5c urrent row are \
1a9c4 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 20 000 terminated.
1a9c5 20 2a 2a 20 61 6e 64 20 68 61 76 65 20 61 6e 20 ** and have an
1a9c6 61 73 73 69 67 6e 65 64 20 74 79 70 65 2e 20 20 assigned type.
1a9c7 54 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 The results are
1a9c8 64 65 2d 65 70 68 65 6d 65 72 61 6c 69 7a 65 64 de-ephemeralized
1a9c9 20 61 73 0a 20 20 2a 2a 20 61 73 20 73 69 64 65 as. ** as side
1a9ca 20 65 66 66 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 effect.. */.
1a9cb 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c pMem = p->pResul
1a9cc 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b tSet = &p->aMem[
1a9cd 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 pOp->p1];. for(
1a9ce 69 3d 30 3b 20 69 3c 70 4f 70 2d 3e 70 32 3b 20 i=0; i<pOp->p2;
1a9cf 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 i++){. sqlite
1a9d0 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 3VdbeMemNulTermi
1a9d1 6e 61 74 65 28 26 70 4d 65 6d 5b 69 5d 29 3b 0a nate(&pMem[i]);.
1a9d2 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 storeTypeInf
1a9d3 6f 28 26 70 4d 65 6d 5b 69 5d 2c 20 65 6e 63 6f o(&pMem[i], enco
1a9d4 64 69 6e 67 29 3b 0a 20 20 20 20 52 45 47 49 53 ding);. REGIS
1a9d5 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1a9d6 31 2b 69 2c 20 26 70 4d 65 6d 5b 69 5d 29 3b 0a 1+i, &pMem[i]);.
1a9d7 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 }. if( db->ma
1a9d8 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 llocFailed ) got
1a9d9 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 o no_mem;.. /*
1a9da 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f Return SQLITE_RO
1a9db 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 6e 43 61 6c W. */. p->nCal
1a9dc 6c 62 61 63 6b 2b 2b 3b 0a 20 20 70 2d 3e 70 63 lback++;. p->pc
1a9dd 20 3d 20 70 63 20 2b 20 31 3b 0a 20 20 72 63 20 = pc + 1;. rc
1a9de 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 = SQLITE_ROW;.
1a9df 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1a9e0 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1a9e1 43 6f 6e 63 61 74 20 50 31 20 50 32 20 50 33 20 Concat P1 P2 P3
1a9e2 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 * *.**.** Add th
1a9e3 65 20 74 65 78 74 20 69 6e 20 72 65 67 69 73 74 e text in regist
1a9e4 65 72 20 50 31 20 6f 6e 74 6f 20 74 68 65 20 65 er P1 onto the e
1a9e5 6e 64 20 6f 66 20 74 68 65 20 74 65 78 74 20 69 nd of the text i
1a9e6 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 n.** register P2
1a9e7 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 and store the r
1a9e8 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 esult in registe
1a9e9 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 r P3..** If eith
1a9ea 65 72 20 74 68 65 20 50 31 20 6f 72 20 50 32 20 er the P1 or P2
1a9eb 74 65 78 74 20 61 72 65 20 4e 55 4c 4c 20 74 68 text are NULL th
1a9ec 65 6e 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e en store NULL in
1a9ed 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20 P3..**.** P3
1a9ee 3d 20 50 32 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a = P2 || P1.**.**
1a9ef 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66 It is illegal f
1a9f0 6f 72 20 50 31 20 61 6e 64 20 50 33 20 74 6f 20 or P1 and P3 to
1a9f1 62 65 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 be the same regi
1a9f2 73 74 65 72 2e 20 53 6f 6d 65 74 69 6d 65 73 2c ster. Sometimes,
1a9f3 0a 2a 2a 20 69 66 20 50 33 20 69 73 20 74 68 65 .** if P3 is the
1a9f4 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 20 61 same register a
1a9f5 73 20 50 32 2c 20 74 68 65 20 69 6d 70 6c 65 6d s P2, the implem
1a9f6 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 62 6c 65 entation is able
1a9f7 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 6d .** to avoid a m
1a9f8 65 6d 63 70 79 28 29 2e 0a 2a 2f 0a 63 61 73 65 emcpy()..*/.case
1a9f9 20 4f 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20 OP_Concat: {
1a9fa 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1a9fb 61 73 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e as TK_CONCAT, in
1a9fc 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 1, in2, out3 */.
1a9fd 20 20 69 36 34 20 6e 42 79 74 65 3b 0a 0a 20 20 i64 nByte;..
1a9fe 61 73 73 65 72 74 28 20 70 49 6e 31 21 3d 70 4f assert( pIn1!=pO
1a9ff 75 74 20 29 3b 0a 20 20 69 66 28 20 28 70 49 6e ut );. if( (pIn
1aa00 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 1->flags | pIn2-
1aa01 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75 >flags) & MEM_Nu
1aa02 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ll ){. sqlite
1aa03 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
1aa04 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b pOut);. break
1aa05 3b 0a 20 20 7d 0a 20 20 45 78 70 61 6e 64 42 6c ;. }. ExpandBl
1aa06 6f 62 28 70 49 6e 31 29 3b 0a 20 20 53 74 72 69 ob(pIn1);. Stri
1aa07 6e 67 69 66 79 28 70 49 6e 31 2c 20 65 6e 63 6f ngify(pIn1, enco
1aa08 64 69 6e 67 29 3b 0a 20 20 45 78 70 61 6e 64 42 ding);. ExpandB
1aa09 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 53 74 72 lob(pIn2);. Str
1aa0a 69 6e 67 69 66 79 28 70 49 6e 32 2c 20 65 6e 63 ingify(pIn2, enc
1aa0b 6f 64 69 6e 67 29 3b 0a 20 20 6e 42 79 74 65 20 oding);. nByte
1aa0c 3d 20 70 49 6e 31 2d 3e 6e 20 2b 20 70 49 6e 32 = pIn1->n + pIn2
1aa0d 2d 3e 6e 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 ->n;. if( nByte
1aa0e 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
1aa0f 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
1aa10 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f ){. goto too
1aa11 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 _big;. }. MemS
1aa12 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c etTypeFlag(pOut,
1aa13 20 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 MEM_Str);. if(
1aa14 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
1aa15 72 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 6e row(pOut, (int)n
1aa16 42 79 74 65 2b 32 2c 20 70 4f 75 74 3d 3d 70 49 Byte+2, pOut==pI
1aa17 6e 32 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 n2) ){. goto
1aa18 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 69 66 no_mem;. }. if
1aa19 28 20 70 4f 75 74 21 3d 70 49 6e 32 20 29 7b 0a ( pOut!=pIn2 ){.
1aa1a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 75 74 2d memcpy(pOut-
1aa1b 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c 20 70 49 6e >z, pIn2->z, pIn
1aa1c 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 2->n);. }. mem
1aa1d 63 70 79 28 26 70 4f 75 74 2d 3e 7a 5b 70 49 6e cpy(&pOut->z[pIn
1aa1e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d 3e 7a 2c 20 2->n], pIn1->z,
1aa1f 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 70 4f 75 74 pIn1->n);. pOut
1aa20 2d 3e 7a 5b 6e 42 79 74 65 5d 20 3d 20 30 3b 0a ->z[nByte] = 0;.
1aa21 20 20 70 4f 75 74 2d 3e 7a 5b 6e 42 79 74 65 2b pOut->z[nByte+
1aa22 31 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 1] = 0;. pOut->
1aa23 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 flags |= MEM_Ter
1aa24 6d 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 m;. pOut->n = (
1aa25 69 6e 74 29 6e 42 79 74 65 3b 0a 20 20 70 4f 75 int)nByte;. pOu
1aa26 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e t->enc = encodin
1aa27 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f g;. UPDATE_MAX_
1aa28 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a BLOBSIZE(pOut);.
1aa29 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1aa2a 70 63 6f 64 65 3a 20 41 64 64 20 50 31 20 50 32 pcode: Add P1 P2
1aa2b 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 P3 * *.**.** Ad
1aa2c 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 d the value in r
1aa2d 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74 68 egister P1 to th
1aa2e 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1aa2f 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 ter P2.** and st
1aa30 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1aa31 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1aa32 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 * If either inpu
1aa33 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 t is NULL, the r
1aa34 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
1aa35 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 6c /./* Opcode: Mul
1aa36 74 69 70 6c 79 20 50 31 20 50 32 20 50 33 20 2a tiply P1 P2 P3 *
1aa37 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 *.**.**.** Mult
1aa38 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65 20 69 iply the value i
1aa39 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79 n register P1 by
1aa3a 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1aa3b 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 gister P2.** and
1aa3c 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
1aa3d 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
1aa3e 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 ..** If either i
1aa3f 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 nput is NULL, th
1aa40 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1aa41 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1aa42 53 75 62 74 72 61 63 74 20 50 31 20 50 32 20 50 Subtract P1 P2 P
1aa43 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 75 62 74 3 * *.**.** Subt
1aa44 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 69 ract the value i
1aa45 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 66 72 n register P1 fr
1aa46 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 om the value in
1aa47 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 register P2.** a
1aa48 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 nd store the res
1aa49 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1aa4a 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1aa4b 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
1aa4c 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
1aa4d 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
1aa4e 3a 20 44 69 76 69 64 65 20 50 31 20 50 32 20 50 : Divide P1 P2 P
1aa4f 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 69 76 69 3 * *.**.** Divi
1aa50 64 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 de the value in
1aa51 72 65 67 69 73 74 65 72 20 50 31 20 62 79 20 74 register P1 by t
1aa52 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1aa53 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 ster P2.** and s
1aa54 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
1aa55 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 in register P3.
1aa56 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e If the value in
1aa57 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 register P2.**
1aa58 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 is zero, then th
1aa59 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1aa5a 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 ..** If either i
1aa5b 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 nput is NULL, th
1aa5c 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1aa5d 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1aa5e 52 65 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20 Remainder P1 P2
1aa5f 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d P3 * *.**.** Com
1aa60 70 75 74 65 20 74 68 65 20 72 65 6d 61 69 6e 64 pute the remaind
1aa61 65 72 20 61 66 74 65 72 20 69 6e 74 65 67 65 72 er after integer
1aa62 20 64 69 76 69 73 69 6f 6e 20 6f 66 20 74 68 65 division of the
1aa63 20 76 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 65 67 value in.** reg
1aa64 69 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 ister P1 by the
1aa65 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1aa66 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 r P2 and store t
1aa67 68 65 20 72 65 73 75 6c 74 20 69 6e 20 50 33 2e he result in P3.
1aa68 20 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 .** If the valu
1aa69 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 e in register P2
1aa6a 20 69 73 20 7a 65 72 6f 20 74 68 65 20 72 65 73 is zero the res
1aa6b 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 ult is NULL..**
1aa6c 49 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e If either operan
1aa6d 64 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 d is NULL, the r
1aa6e 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
1aa6f 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 /.case OP_Add:
1aa70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aa71 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 /* same as TK_P
1aa72 4c 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f LUS, in1, in2, o
1aa73 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 ut3 */.case OP_S
1aa74 75 62 74 72 61 63 74 3a 20 20 20 20 20 20 20 20 ubtract:
1aa75 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1aa76 20 54 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 TK_MINUS, in1,
1aa77 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 in2, out3 */.cas
1aa78 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 e OP_Multiply:
1aa79 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1aa7a 61 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 2c 20 ame as TK_STAR,
1aa7b 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1aa7c 2f 0a 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 /.case OP_Divide
1aa7d 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 :
1aa7e 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 /* same as TK_S
1aa7f 4c 41 53 48 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 LASH, in1, in2,
1aa80 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f out3 */.case OP_
1aa81 52 65 6d 61 69 6e 64 65 72 3a 20 7b 20 20 20 20 Remainder: {
1aa82 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1aa83 73 20 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 s TK_REM, in1, i
1aa84 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 69 6e n2, out3 */. in
1aa85 74 20 66 6c 61 67 73 3b 0a 20 20 61 70 70 6c 79 t flags;. apply
1aa86 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 NumericAffinity(
1aa87 70 49 6e 31 29 3b 0a 20 20 61 70 70 6c 79 4e 75 pIn1);. applyNu
1aa88 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 mericAffinity(pI
1aa89 6e 32 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20 70 n2);. flags = p
1aa8a 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e In1->flags | pIn
1aa8b 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 2->flags;. if(
1aa8c 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c (flags & MEM_Nul
1aa8d 6c 29 21 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 l)!=0 ) goto ari
1aa8e 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 thmetic_result_i
1aa8f 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 28 70 s_null;. if( (p
1aa90 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e In1->flags & pIn
1aa91 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 2->flags & MEM_I
1aa92 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b 0a nt)==MEM_Int ){.
1aa93 20 20 20 20 69 36 34 20 61 2c 20 62 3b 0a 20 20 i64 a, b;.
1aa94 20 20 61 20 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b a = pIn1->u.i;
1aa95 0a 20 20 20 20 62 20 3d 20 70 49 6e 32 2d 3e 75 . b = pIn2->u
1aa96 2e 69 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 .i;. switch(
1aa97 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 pOp->opcode ){.
1aa98 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 case OP_Add
1aa99 3a 20 20 20 20 20 20 20 20 20 62 20 2b 3d 20 61 : b += a
1aa9a 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ; break;.
1aa9b 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 case OP_Sub
1aa9c 74 72 61 63 74 3a 20 20 20 20 62 20 2d 3d 20 61 tract: b -= a
1aa9d 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ; break;.
1aa9e 20 20 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c case OP_Mul
1aa9f 74 69 70 6c 79 3a 20 20 20 20 62 20 2a 3d 20 61 tiply: b *= a
1aaa0 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ; break;.
1aaa1 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76 case OP_Div
1aaa2 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 ide: {. i
1aaa3 66 28 20 61 3d 3d 30 20 29 20 67 6f 74 6f 20 61 f( a==0 ) goto a
1aaa4 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 rithmetic_result
1aaa5 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 _is_null;.
1aaa6 20 20 2f 2a 20 44 69 76 69 64 69 6e 67 20 74 68 /* Dividing th
1aaa7 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 e largest possib
1aaa8 6c 65 20 6e 65 67 61 74 69 76 65 20 36 34 2d 62 le negative 64-b
1aaa9 69 74 20 69 6e 74 65 67 65 72 20 28 31 3c 3c 36 it integer (1<<6
1aaaa 33 29 20 62 79 20 0a 20 20 20 20 20 20 20 20 2a 3) by . *
1aaab 2a 20 2d 31 20 72 65 74 75 72 6e 73 20 61 6e 20 * -1 returns an
1aaac 69 6e 74 65 67 65 72 20 74 6f 6f 20 6c 61 72 67 integer too larg
1aaad 65 20 74 6f 20 73 74 6f 72 65 20 69 6e 20 61 20 e to store in a
1aaae 36 34 2d 62 69 74 20 64 61 74 61 2d 74 79 70 65 64-bit data-type
1aaaf 2e 20 4f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 . On. **
1aab0 73 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75 72 some architectur
1aab1 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 76 es, the value ov
1aab2 65 72 66 6c 6f 77 73 20 74 6f 20 28 31 3c 3c 36 erflows to (1<<6
1aab3 33 29 2e 20 4f 6e 20 6f 74 68 65 72 73 2c 0a 20 3). On others,.
1aab4 20 20 20 20 20 20 20 2a 2a 20 61 20 53 49 47 46 ** a SIGF
1aab5 50 45 20 69 73 20 69 73 73 75 65 64 2e 20 54 68 PE is issued. Th
1aab6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 e following stat
1aab7 65 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69 7a 65 73 ement normalizes
1aab8 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a this. **
1aab9 20 62 65 68 61 76 69 6f 72 20 73 6f 20 74 68 61 behavior so tha
1aaba 74 20 61 6c 6c 20 61 72 63 68 69 74 65 63 74 75 t all architectu
1aabb 72 65 73 20 62 65 68 61 76 65 20 61 73 20 69 66 res behave as if
1aabc 20 69 6e 74 65 67 65 72 20 0a 20 20 20 20 20 20 integer .
1aabd 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6f 63 ** overflow oc
1aabe 63 75 72 72 65 64 2e 0a 20 20 20 20 20 20 20 20 curred..
1aabf 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 */. if( a
1aac0 3d 3d 2d 31 20 26 26 20 62 3d 3d 53 4d 41 4c 4c ==-1 && b==SMALL
1aac1 45 53 54 5f 49 4e 54 36 34 20 29 20 61 20 3d 20 EST_INT64 ) a =
1aac2 31 3b 0a 20 20 20 20 20 20 20 20 62 20 2f 3d 20 1;. b /=
1aac3 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b a;. break
1aac4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1aac5 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
1aac6 20 20 20 69 66 28 20 61 3d 3d 30 20 29 20 67 6f if( a==0 ) go
1aac7 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 to arithmetic_re
1aac8 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 sult_is_null;.
1aac9 20 20 20 20 20 20 69 66 28 20 61 3d 3d 2d 31 20 if( a==-1
1aaca 29 20 61 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ) a = 1;.
1aacb 20 62 20 25 3d 20 61 3b 0a 20 20 20 20 20 20 20 b %= a;.
1aacc 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
1aacd 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e }. pOut->
1aace 75 2e 69 20 3d 20 62 3b 0a 20 20 20 20 4d 65 6d u.i = b;. Mem
1aacf 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
1aad0 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 , MEM_Int);. }e
1aad1 6c 73 65 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 lse{. double
1aad2 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20 73 71 a, b;. a = sq
1aad3 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
1aad4 75 65 28 70 49 6e 31 29 3b 0a 20 20 20 20 62 20 ue(pIn1);. b
1aad5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 = sqlite3VdbeRea
1aad6 6c 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20 lValue(pIn2);.
1aad7 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f switch( pOp->o
1aad8 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 63 pcode ){. c
1aad9 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 ase OP_Add:
1aada 20 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 20 20 b += a;
1aadb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1aadc 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a ase OP_Subtract:
1aadd 20 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 20 20 b -= a;
1aade 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1aadf 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a ase OP_Multiply:
1aae0 20 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 20 20 b *= a;
1aae1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1aae2 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b ase OP_Divide: {
1aae3 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d . if( a==
1aae4 30 2e 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 0.0 ) goto arith
1aae5 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f metic_result_is_
1aae6 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 62 20 null;. b
1aae7 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 62 72 /= a;. br
1aae8 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1aae9 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
1aaea 20 20 20 20 20 20 69 36 34 20 69 61 20 3d 20 28 i64 ia = (
1aaeb 69 36 34 29 61 3b 0a 20 20 20 20 20 20 20 20 69 i64)a;. i
1aaec 36 34 20 69 62 20 3d 20 28 69 36 34 29 62 3b 0a 64 ib = (i64)b;.
1aaed 20 20 20 20 20 20 20 20 69 66 28 20 69 61 3d 3d if( ia==
1aaee 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 0 ) goto arithme
1aaef 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 tic_result_is_nu
1aaf0 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ll;. if(
1aaf1 69 61 3d 3d 2d 31 20 29 20 69 61 20 3d 20 31 3b ia==-1 ) ia = 1;
1aaf2 0a 20 20 20 20 20 20 20 20 62 20 3d 20 28 64 6f . b = (do
1aaf3 75 62 6c 65 29 28 69 62 20 25 20 69 61 29 3b 0a uble)(ib % ia);.
1aaf4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1aaf5 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1aaf6 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 if( sqlite3IsNa
1aaf7 4e 28 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f N(b) ){. go
1aaf8 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 to arithmetic_re
1aaf9 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 sult_is_null;.
1aafa 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e 72 20 }. pOut->r
1aafb 3d 20 62 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 = b;. MemSetT
1aafc 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1aafd 4d 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 66 28 M_Real);. if(
1aafe 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 (flags & MEM_Re
1aaff 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 al)==0 ){.
1ab00 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 sqlite3VdbeInteg
1ab01 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75 74 29 erAffinity(pOut)
1ab02 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 ;. }. }. br
1ab03 65 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 69 63 eak;..arithmetic
1ab04 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a _result_is_null:
1ab05 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1ab06 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a mSetNull(pOut);.
1ab07 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1ab08 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a pcode: CollSeq *
1ab09 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 * P4.**.** P4 i
1ab0a 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1ab0b 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 2e CollSeq struct.
1ab0c 20 49 66 20 74 68 65 20 6e 65 78 74 20 63 61 6c If the next cal
1ab0d 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e 63 l to a user func
1ab0e 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 65 tion.** or aggre
1ab0f 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c 69 74 gate calls sqlit
1ab10 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 e3GetFuncCollSeq
1ab11 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 (), this collati
1ab12 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c on sequence will
1ab13 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 64 2e .** be returned.
1ab14 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 This is used by
1ab15 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 69 the built-in mi
1ab16 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 20 6e n(), max() and n
1ab17 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63 74 ullif().** funct
1ab18 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ions..**.** The
1ab19 69 6e 74 65 72 66 61 63 65 20 75 73 65 64 20 62 interface used b
1ab1a 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 y the implementa
1ab1b 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f 72 tion of the afor
1ab1c 65 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63 74 ementioned funct
1ab1d 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72 69 ions.** to retri
1ab1e 65 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f eve the collatio
1ab1f 6e 20 73 65 71 75 65 6e 63 65 20 73 65 74 20 62 n sequence set b
1ab20 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 y this opcode is
1ab21 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a not available.*
1ab22 2a 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 * publicly, only
1ab23 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f to user functio
1ab24 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20 66 75 ns defined in fu
1ab25 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 nc.c..*/.case OP
1ab26 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 61 73 _CollSeq: {. as
1ab27 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 sert( pOp->p4typ
1ab28 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b e==P4_COLLSEQ );
1ab29 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1ab2a 4f 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f 6e Opcode: Function
1ab2b 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
1ab2c 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 75 **.** Invoke a u
1ab2d 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 50 34 ser function (P4
1ab2e 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1ab2f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 72 75 a Function stru
1ab30 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 64 65 cture that.** de
1ab31 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63 74 69 fines the functi
1ab32 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72 67 75 on) with P5 argu
1ab33 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f 6d ments taken from
1ab34 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 register P2 and
1ab35 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e 20 .** successors.
1ab36 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 The result of t
1ab37 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 he function is s
1ab38 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1ab39 72 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 74 65 r P3..** Registe
1ab3a 72 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 62 65 r P3 must not be
1ab3b 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 6e 63 one of the func
1ab3c 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a tion inputs..**.
1ab3d 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d 62 69 ** P1 is a 32-bi
1ab3e 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 t bitmask indica
1ab3f 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 ting whether or
1ab40 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d 65 6e not each argumen
1ab41 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 6e t to the .** fun
1ab42 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65 72 6d ction was determ
1ab43 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 74 ined to be const
1ab44 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 ant at compile t
1ab45 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69 72 73 ime. If the firs
1ab46 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 61 t.** argument wa
1ab47 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 s constant then
1ab48 62 69 74 20 30 20 6f 66 20 50 31 20 69 73 20 73 bit 0 of P1 is s
1ab49 65 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 et. This is used
1ab4a 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a to determine.**
1ab4b 20 77 68 65 74 68 65 72 20 6d 65 74 61 20 64 61 whether meta da
1ab4c 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ta associated wi
1ab4d 74 68 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 th a user functi
1ab4e 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 69 6e on argument usin
1ab4f 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 g the.** sqlite3
1ab50 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 _set_auxdata() A
1ab51 50 49 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 PI may be safely
1ab52 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 retained until
1ab53 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f the next.** invo
1ab54 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6f cation of this o
1ab55 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 pcode..**.** See
1ab56 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 61 also: AggStep a
1ab57 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 nd AggFinal.*/.c
1ab58 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a ase OP_Function:
1ab59 20 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 {. int i;. Me
1ab5a 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 m *pArg;. sqlit
1ab5b 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a e3_context ctx;.
1ab5c 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
1ab5d 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e **apVal;. int n
1ab5e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 0a 20 20 61 = pOp->p5;.. a
1ab5f 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b pVal = p->apArg;
1ab60 0a 20 20 61 73 73 65 72 74 28 20 61 70 56 61 6c . assert( apVal
1ab61 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 0a 20 20 61 || n==0 );.. a
1ab62 73 73 65 72 74 28 20 6e 3d 3d 30 20 7c 7c 20 28 ssert( n==0 || (
1ab63 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 pOp->p2>0 && pOp
1ab64 2d 3e 70 32 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d 29 ->p2+n<=p->nMem)
1ab65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1ab66 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c p->p3<pOp->p2 ||
1ab67 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 pOp->p3>=pOp->p
1ab68 32 2b 6e 20 29 3b 0a 20 20 70 41 72 67 20 3d 20 2+n );. pArg =
1ab69 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1ab6a 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c ];. for(i=0; i<
1ab6b 6e 3b 20 69 2b 2b 2c 20 70 41 72 67 2b 2b 29 7b n; i++, pArg++){
1ab6c 0a 20 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20 . apVal[i] =
1ab6d 70 41 72 67 3b 0a 20 20 20 20 73 74 6f 72 65 54 pArg;. storeT
1ab6e 79 70 65 49 6e 66 6f 28 70 41 72 67 2c 20 65 6e ypeInfo(pArg, en
1ab6f 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 52 45 47 coding);. REG
1ab70 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1ab71 3e 70 32 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a >p2, pArg);. }.
1ab72 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1ab73 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 p4type==P4_FUNCD
1ab74 45 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 EF || pOp->p4typ
1ab75 65 3d 3d 50 34 5f 56 44 42 45 46 55 4e 43 20 29 e==P4_VDBEFUNC )
1ab76 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 ;. if( pOp->p4t
1ab77 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 ype==P4_FUNCDEF
1ab78 29 7b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 ){. ctx.pFunc
1ab79 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 = pOp->p4.pFunc
1ab7a 3b 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 ;. ctx.pVdbeF
1ab7b 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 unc = 0;. }else
1ab7c 7b 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 {. ctx.pVdbeF
1ab7d 75 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a unc = (VdbeFunc*
1ab7e 29 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 )pOp->p4.pVdbeFu
1ab7f 6e 63 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e nc;. ctx.pFun
1ab80 63 20 3d 20 63 74 78 2e 70 56 64 62 65 46 75 6e c = ctx.pVdbeFun
1ab81 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 c->pFunc;. }..
1ab82 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1ab83 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 >0 && pOp->p3<=p
1ab84 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 ->nMem );. pOut
1ab85 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1ab86 3e 70 33 5d 3b 0a 20 20 63 74 78 2e 73 2e 66 6c >p3];. ctx.s.fl
1ab87 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a ags = MEM_Null;.
1ab88 20 20 63 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b ctx.s.db = db;
1ab89 0a 20 20 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 . ctx.s.xDel =
1ab8a 30 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d 61 6c 6c 0;. ctx.s.zMall
1ab8b 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 oc = 0;.. /* Th
1ab8c 65 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 e output cell ma
1ab8d 79 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 y already have a
1ab8e 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 buffer allocate
1ab8f 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 d. Move. ** the
1ab90 20 70 6f 69 6e 74 65 72 20 74 6f 20 63 74 78 2e pointer to ctx.
1ab91 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65 s so in case the
1ab92 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 63 user-function c
1ab93 61 6e 20 75 73 65 0a 20 20 2a 2a 20 74 68 65 20 an use. ** the
1ab94 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 already allocate
1ab95 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 d buffer instead
1ab96 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 of allocating a
1ab97 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 new one.. */.
1ab98 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
1ab99 6f 76 65 28 26 63 74 78 2e 73 2c 20 70 4f 75 74 ove(&ctx.s, pOut
1ab9a 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 );. MemSetTypeF
1ab9b 6c 61 67 28 26 63 74 78 2e 73 2c 20 4d 45 4d 5f lag(&ctx.s, MEM_
1ab9c 4e 75 6c 6c 29 3b 0a 0a 20 20 63 74 78 2e 69 73 Null);.. ctx.is
1ab9d 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 Error = 0;. if(
1ab9e 20 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 ctx.pFunc->flag
1ab9f 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f s & SQLITE_FUNC_
1aba0 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 NEEDCOLL ){.
1aba1 61 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 assert( pOp>p->a
1aba2 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 Op );. assert
1aba3 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 ( pOp[-1].p4type
1aba4 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a ==P4_COLLSEQ );.
1aba5 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b assert( pOp[
1aba6 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 -1].opcode==OP_C
1aba7 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74 ollSeq );. ct
1aba8 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 x.pColl = pOp[-1
1aba9 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a ].p4.pColl;. }.
1abaa 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1abab 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 etyOff(db) ) got
1abac 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1abad 69 73 75 73 65 3b 0a 20 20 28 2a 63 74 78 2e 70 isuse;. (*ctx.p
1abae 46 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 63 74 Func->xFunc)(&ct
1abaf 78 2c 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 x, n, apVal);.
1abb0 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1abb1 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73 yOn(db) ){. s
1abb2 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1abb3 65 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 ease(&ctx.s);.
1abb4 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 goto abort_due
1abb5 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a _to_misuse;. }.
1abb6 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
1abb7 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a Failed ){. /*
1abb8 20 45 76 65 6e 20 74 68 6f 75 67 68 20 61 20 6d Even though a m
1abb9 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c alloc() has fail
1abba 65 64 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e ed, the implemen
1abbb 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 tation of the.
1abbc 20 20 2a 2a 20 75 73 65 72 20 66 75 6e 63 74 69 ** user functi
1abbd 6f 6e 20 6d 61 79 20 68 61 76 65 20 63 61 6c 6c on may have call
1abbe 65 64 20 61 6e 20 73 71 6c 69 74 65 33 5f 72 65 ed an sqlite3_re
1abbf 73 75 6c 74 5f 58 58 58 28 29 20 66 75 6e 63 74 sult_XXX() funct
1abc0 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 ion. ** to re
1abc1 74 75 72 6e 20 61 20 76 61 6c 75 65 2e 20 54 68 turn a value. Th
1abc2 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c e following call
1abc3 20 72 65 6c 65 61 73 65 73 20 61 6e 79 20 72 65 releases any re
1abc4 73 6f 75 72 63 65 73 0a 20 20 20 20 2a 2a 20 61 sources. ** a
1abc5 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 ssociated with s
1abc6 75 63 68 20 61 20 76 61 6c 75 65 2e 0a 20 20 20 uch a value..
1abc7 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a **. ** Note:
1abc8 20 4d 61 79 62 65 20 4d 65 6d 52 65 6c 65 61 73 Maybe MemReleas
1abc9 65 28 29 20 73 68 6f 75 6c 64 20 62 65 20 63 61 e() should be ca
1abca 6c 6c 65 64 20 69 66 20 73 71 6c 69 74 65 33 53 lled if sqlite3S
1abcb 61 66 65 74 79 4f 6e 28 29 0a 20 20 20 20 2a 2a afetyOn(). **
1abcc 20 66 61 69 6c 73 20 61 6c 73 6f 20 28 74 68 65 fails also (the
1abcd 20 69 66 28 2e 2e 2e 29 20 73 74 61 74 65 6d 65 if(...) stateme
1abce 6e 74 20 61 62 6f 76 65 29 2e 20 42 75 74 20 69 nt above). But i
1abcf 66 20 70 65 6f 70 6c 65 20 61 72 65 0a 20 20 20 f people are.
1abd0 20 2a 2a 20 6d 69 73 75 73 69 6e 67 20 73 71 6c ** misusing sql
1abd1 69 74 65 2c 20 74 68 65 79 20 68 61 76 65 20 62 ite, they have b
1abd2 69 67 67 65 72 20 70 72 6f 62 6c 65 6d 73 20 74 igger problems t
1abd3 68 61 6e 20 61 20 6c 65 61 6b 65 64 20 76 61 6c han a leaked val
1abd4 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 ue.. */. s
1abd5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1abd6 65 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 ease(&ctx.s);.
1abd7 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1abd8 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 }.. /* If any
1abd9 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 20 66 auxiliary data f
1abda 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 unctions have be
1abdb 65 6e 20 63 61 6c 6c 65 64 20 62 79 20 74 68 69 en called by thi
1abdc 73 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c s user function,
1abdd 0a 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c . ** immediatel
1abde 79 20 63 61 6c 6c 20 74 68 65 20 64 65 73 74 72 y call the destr
1abdf 75 63 74 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f uctor for any no
1abe0 6e 2d 73 74 61 74 69 63 20 76 61 6c 75 65 73 2e n-static values.
1abe1 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 74 78 2e . */. if( ctx.
1abe2 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 pVdbeFunc ){.
1abe3 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
1abe4 74 65 41 75 78 44 61 74 61 28 63 74 78 2e 70 56 teAuxData(ctx.pV
1abe5 64 62 65 46 75 6e 63 2c 20 70 4f 70 2d 3e 70 31 dbeFunc, pOp->p1
1abe6 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 );. pOp->p4.p
1abe7 56 64 62 65 46 75 6e 63 20 3d 20 63 74 78 2e 70 VdbeFunc = ctx.p
1abe8 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 70 4f VdbeFunc;. pO
1abe9 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56 p->p4type = P4_V
1abea 44 42 45 46 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 DBEFUNC;. }..
1abeb 2f 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 /* If the functi
1abec 6f 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 on returned an e
1abed 72 72 6f 72 2c 20 74 68 72 6f 77 20 61 6e 20 65 rror, throw an e
1abee 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 xception */. if
1abef 28 20 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b ( ctx.isError ){
1abf0 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1abf1 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1abf2 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c g, db, "%s", sql
1abf3 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
1abf4 26 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 &ctx.s));. rc
1abf5 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a = ctx.isError;.
1abf6 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 }.. /* Copy t
1abf7 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
1abf8 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 72 function into r
1abf9 65 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 egister P3 */.
1abfa 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1abfb 65 45 6e 63 6f 64 69 6e 67 28 26 63 74 78 2e 73 eEncoding(&ctx.s
1abfc 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 , encoding);. s
1abfd 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1abfe 65 28 70 4f 75 74 2c 20 26 63 74 78 2e 73 29 3b e(pOut, &ctx.s);
1abff 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
1ac00 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f 75 74 beMemTooBig(pOut
1ac01 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f ) ){. goto to
1ac02 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 52 45 47 o_big;. }. REG
1ac03 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1ac04 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 >p3, pOut);. UP
1ac05 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1ac06 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b E(pOut);. break
1ac07 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1ac08 42 69 74 41 6e 64 20 50 31 20 50 32 20 50 33 20 BitAnd P1 P2 P3
1ac09 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 * *.**.** Take t
1ac0a 68 65 20 62 69 74 2d 77 69 73 65 20 41 4e 44 20 he bit-wise AND
1ac0b 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e of the values in
1ac0c 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 register P1 and
1ac0d 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 P2 and.** store
1ac0e 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
1ac0f 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
1ac10 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
1ac11 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
1ac12 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f lt is NULL..*/./
1ac13 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72 20 * Opcode: BitOr
1ac14 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1ac15 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d ** Take the bit-
1ac16 77 69 73 65 20 4f 52 20 6f 66 20 74 68 65 20 76 wise OR of the v
1ac17 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 alues in registe
1ac18 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a r P1 and P2 and.
1ac19 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 ** store the res
1ac1a 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1ac1b 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1ac1c 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
1ac1d 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
1ac1e 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
1ac1f 3a 20 53 68 69 66 74 4c 65 66 74 20 50 31 20 50 : ShiftLeft P1 P
1ac20 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 2 P3 * *.**.** S
1ac21 68 69 66 74 20 74 68 65 20 69 6e 74 65 67 65 72 hift the integer
1ac22 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1ac23 65 72 20 50 32 20 74 6f 20 74 68 65 20 6c 65 66 er P2 to the lef
1ac24 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 t by the.** numb
1ac25 65 72 20 6f 66 20 62 69 74 73 20 73 70 65 63 69 er of bits speci
1ac26 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 fied by the inte
1ac27 67 65 72 20 69 6e 20 72 65 67 69 73 65 72 20 50 ger in regiser P
1ac28 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 1..** Store the
1ac29 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 result in regist
1ac2a 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 er P3..** If eit
1ac2b 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1ac2c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1ac2d 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 NULL..*/./* Opc
1ac2e 6f 64 65 3a 20 53 68 69 66 74 52 69 67 68 74 20 ode: ShiftRight
1ac2f 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1ac30 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 ** Shift the int
1ac31 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 eger value in re
1ac32 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 gister P2 to the
1ac33 20 72 69 67 68 74 20 62 79 20 74 68 65 0a 2a 2a right by the.**
1ac34 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 number of bits
1ac35 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 specified by the
1ac36 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 integer in regi
1ac37 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 ster P1..** Stor
1ac38 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 e the result in
1ac39 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 register P3..**
1ac3a 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 If either input
1ac3b 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 is NULL, the res
1ac3c 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a ult is NULL..*/.
1ac3d 63 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 case OP_BitAnd:
1ac3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ac3f 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 /* same as TK_BI
1ac40 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 TAND, in1, in2,
1ac41 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f out3 */.case OP_
1ac42 42 69 74 4f 72 3a 20 20 20 20 20 20 20 20 20 20 BitOr:
1ac43 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1ac44 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69 6e 31 as TK_BITOR, in1
1ac45 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 , in2, out3 */.c
1ac46 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 ase OP_ShiftLeft
1ac47 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f : /
1ac48 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 * same as TK_LSH
1ac49 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f IFT, in1, in2, o
1ac4a 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 ut3 */.case OP_S
1ac4b 68 69 66 74 52 69 67 68 74 3a 20 7b 20 20 20 20 hiftRight: {
1ac4c 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1ac4d 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 69 6e 31 s TK_RSHIFT, in1
1ac4e 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 , in2, out3 */.
1ac4f 20 69 36 34 20 61 2c 20 62 3b 0a 0a 20 20 69 66 i64 a, b;.. if
1ac50 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c ( (pIn1->flags |
1ac51 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 pIn2->flags) &
1ac52 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
1ac53 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1ac54 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 tNull(pOut);.
1ac55 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 61 20 break;. }. a
1ac56 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 = sqlite3VdbeInt
1ac57 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20 62 Value(pIn2);. b
1ac58 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e = sqlite3VdbeIn
1ac59 74 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 tValue(pIn1);.
1ac5a 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 switch( pOp->opc
1ac5b 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 ode ){. case
1ac5c 4f 50 5f 42 69 74 41 6e 64 3a 20 20 20 20 20 20 OP_BitAnd:
1ac5d 61 20 26 3d 20 62 3b 20 20 20 20 20 62 72 65 61 a &= b; brea
1ac5e 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 k;. case OP_B
1ac5f 69 74 4f 72 3a 20 20 20 20 20 20 20 61 20 7c 3d itOr: a |=
1ac60 20 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 b; break;.
1ac61 20 20 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 case OP_Shift
1ac62 4c 65 66 74 3a 20 20 20 61 20 3c 3c 3d 20 62 3b Left: a <<= b;
1ac63 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 break;. d
1ac64 65 66 61 75 6c 74 3a 20 20 61 73 73 65 72 74 28 efault: assert(
1ac65 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1ac66 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b 0a 20 _ShiftRight );.
1ac67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ac68 20 20 20 20 20 20 20 20 61 20 3e 3e 3d 20 62 3b a >>= b;
1ac69 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 break;. }.
1ac6a 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 61 3b 0a pOut->u.i = a;.
1ac6b 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1ac6c 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1ac6d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1ac6e 4f 70 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20 20 Opcode: AddImm
1ac6f 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a P1 P2 * * *.** .
1ac70 2a 2a 20 41 64 64 20 74 68 65 20 63 6f 6e 73 74 ** Add the const
1ac71 61 6e 74 20 50 32 20 74 6f 20 74 68 65 20 76 61 ant P2 to the va
1ac72 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1ac73 50 31 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c P1..** The resul
1ac74 74 20 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69 t is always an i
1ac75 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f nteger..**.** To
1ac76 20 66 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73 force any regis
1ac77 74 65 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 ter to be an int
1ac78 65 67 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30 eger, just add 0
1ac79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 ..*/.case OP_Add
1ac7a 49 6d 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 Imm: {
1ac7b 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73 71 /* in1 */. sq
1ac7c 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 lite3VdbeMemInte
1ac7d 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 gerify(pIn1);.
1ac7e 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 pIn1->u.i += pOp
1ac7f 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d ->p2;. break;.}
1ac80 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73 ../* Opcode: Mus
1ac81 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a tBeInt P1 P2 * *
1ac82 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20 *.** .** Force
1ac83 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1ac84 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 ister P1 to be a
1ac85 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 74 n integer. If t
1ac86 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 he value.** in P
1ac87 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 1 is not an inte
1ac88 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62 ger and cannot b
1ac89 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f e converted into
1ac8a 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 an integer.** w
1ac8b 69 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 73 ithout data loss
1ac8c 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 , then jump imme
1ac8d 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f diately to P2, o
1ac8e 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61 r if P2==0.** ra
1ac8f 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 ise an SQLITE_MI
1ac90 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f 6e SMATCH exception
1ac91 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73 ..*/.case OP_Mus
1ac92 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 tBeInt: {
1ac93 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1ac94 31 20 2a 2f 0a 20 20 61 70 70 6c 79 41 66 66 69 1 */. applyAffi
1ac95 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 nity(pIn1, SQLIT
1ac96 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65 E_AFF_NUMERIC, e
1ac97 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28 20 ncoding);. if(
1ac98 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn1->flags & M
1ac99 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 EM_Int)==0 ){.
1ac9a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 if( pOp->p2==0
1ac9b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
1ac9c 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b 0a QLITE_MISMATCH;.
1ac9d 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 goto abort
1ac9e 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1ac9f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1aca0 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1aca1 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
1aca2 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 . MemSetTypeF
1aca3 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 49 6e lag(pIn1, MEM_In
1aca4 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b t);. }. break;
1aca5 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .}../* Opcode: R
1aca6 65 61 6c 41 66 66 69 6e 69 74 79 20 50 31 20 2a ealAffinity P1 *
1aca7 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 * * *.**.** If
1aca8 72 65 67 69 73 74 65 72 20 50 31 20 68 6f 6c 64 register P1 hold
1aca9 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e s an integer con
1acaa 76 65 72 74 20 69 74 20 74 6f 20 61 20 72 65 61 vert it to a rea
1acab 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 l value..**.** T
1acac 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 his opcode is us
1acad 65 64 20 77 68 65 6e 20 65 78 74 72 61 63 74 69 ed when extracti
1acae 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 ng information f
1acaf 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 rom a column tha
1acb0 74 0a 2a 2a 20 68 61 73 20 52 45 41 4c 20 61 66 t.** has REAL af
1acb1 66 69 6e 69 74 79 2e 20 20 53 75 63 68 20 63 6f finity. Such co
1acb2 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79 20 lumn values may
1acb3 73 74 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 still be stored
1acb4 61 73 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c 20 as.** integers,
1acb5 66 6f 72 20 73 70 61 63 65 20 65 66 66 69 63 69 for space effici
1acb6 65 6e 63 79 2c 20 62 75 74 20 61 66 74 65 72 20 ency, but after
1acb7 65 78 74 72 61 63 74 69 6f 6e 20 77 65 20 77 61 extraction we wa
1acb8 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68 61 nt them.** to ha
1acb9 76 65 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20 76 ve only a real v
1acba 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 alue..*/.case OP
1acbb 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 3a 20 7b _RealAffinity: {
1acbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1acbd 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 66 /* in1 */. if
1acbe 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 ( pIn1->flags &
1acbf 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 MEM_Int ){. s
1acc0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 qlite3VdbeMemRea
1acc1 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a lify(pIn1);. }.
1acc2 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e break;.}..#ifn
1acc3 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1acc4 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 CAST./* Opcode:
1acc5 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a 20 ToText P1 * * *
1acc6 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 *.**.** Force th
1acc7 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1acc8 74 65 72 20 50 31 20 74 6f 20 62 65 20 74 65 78 ter P1 to be tex
1acc9 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c t..** If the val
1acca 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 ue is numeric, c
1accb 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 onvert it to a s
1accc 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65 0a tring using the.
1accd 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 ** equivalent of
1acce 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f 62 printf(). Blob
1accf 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63 68 values are unch
1acd0 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 65 anged and.** are
1acd1 20 61 66 74 65 72 77 61 72 64 73 20 73 69 6d 70 afterwards simp
1acd2 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 ly interpreted a
1acd3 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 s text..**.** A
1acd4 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f NULL value is no
1acd5 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 t changed by thi
1acd6 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 s routine. It r
1acd7 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a emains NULL..*/.
1acd8 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20 case OP_ToText:
1acd9 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1acda 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1acdb 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a 2f _TO_TEXT, in1 */
1acdc 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 . if( pIn1->fla
1acdd 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 gs & MEM_Null )
1acde 62 72 65 61 6b 3b 0a 20 20 61 73 73 65 72 74 28 break;. assert(
1acdf 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f 42 MEM_Str==(MEM_B
1ace0 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20 20 70 49 6e lob>>3) );. pIn
1ace1 31 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 49 6e 1->flags |= (pIn
1ace2 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 1->flags&MEM_Blo
1ace3 62 29 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41 66 b)>>3;. applyAf
1ace4 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c finity(pIn1, SQL
1ace5 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e ITE_AFF_TEXT, en
1ace6 63 6f 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d 20 coding);. rc =
1ace7 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 ExpandBlob(pIn1)
1ace8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 ;. assert( pIn1
1ace9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 ->flags & MEM_St
1acea 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 r || db->mallocF
1aceb 61 69 6c 65 64 20 29 3b 0a 20 20 70 49 6e 31 2d ailed );. pIn1-
1acec 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
1aced 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d Int|MEM_Real|MEM
1acee 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 29 3b _Blob|MEM_Zero);
1acef 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c . UPDATE_MAX_BL
1acf0 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 OBSIZE(pIn1);.
1acf1 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1acf2 6f 64 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20 2a ode: ToBlob P1 *
1acf3 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 * * *.**.** For
1acf4 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 ce the value in
1acf5 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 register P1 to b
1acf6 65 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66 20 e a BLOB..** If
1acf7 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d the value is num
1acf8 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74 eric, convert it
1acf9 20 74 6f 20 61 20 73 74 72 69 6e 67 20 66 69 72 to a string fir
1acfa 73 74 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20 61 st..** Strings a
1acfb 72 65 20 73 69 6d 70 6c 79 20 72 65 69 6e 74 65 re simply reinte
1acfc 72 70 72 65 74 65 64 20 61 73 20 62 6c 6f 62 73 rpreted as blobs
1acfd 20 77 69 74 68 20 6e 6f 20 63 68 61 6e 67 65 0a with no change.
1acfe 2a 2a 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c ** to the underl
1acff 79 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a ying data..**.**
1ad00 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 A NULL value is
1ad01 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 not changed by
1ad02 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 this routine. I
1ad03 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a t remains NULL..
1ad04 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c 6f */.case OP_ToBlo
1ad05 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 b: {
1ad06 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1ad07 20 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e 31 TK_TO_BLOB, in1
1ad08 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e */. if( pIn1->
1ad09 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1ad0a 20 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 20 ) break;. if(
1ad0b 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn1->flags & M
1ad0c 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20 EM_Blob)==0 ){.
1ad0d 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 applyAffinity
1ad0e 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 (pIn1, SQLITE_AF
1ad0f 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 F_TEXT, encoding
1ad10 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1ad11 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1ad12 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c _Str || db->mall
1ad13 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 ocFailed );.
1ad14 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
1ad15 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a In1, MEM_Blob);.
1ad16 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e }else{. pIn
1ad17 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 1->flags &= ~(ME
1ad18 4d 5f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d 5f M_TypeMask&~MEM_
1ad19 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50 44 Blob);. }. UPD
1ad1a 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
1ad1b 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b (pIn1);. break;
1ad1c 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 .}../* Opcode: T
1ad1d 6f 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a 20 oNumeric P1 * *
1ad1e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 * *.**.** Force
1ad1f 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1ad20 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 6e ister P1 to be n
1ad21 75 6d 65 72 69 63 20 28 65 69 74 68 65 72 20 61 umeric (either a
1ad22 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 20 n.** integer or
1ad23 61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 a floating-point
1ad24 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 20 number.).** If
1ad25 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 the value is tex
1ad26 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 t or blob, try t
1ad27 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 o convert it to
1ad28 61 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 an using the.**
1ad29 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 equivalent of at
1ad2a 6f 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20 61 oi() or atof() a
1ad2b 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f nd store 0 if no
1ad2c 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e such conversion
1ad2d 20 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 .** is possible
1ad2e 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 ..**.** A NULL v
1ad2f 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e alue is not chan
1ad30 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 ged by this rout
1ad31 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 ine. It remains
1ad32 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f NULL..*/.case O
1ad33 50 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20 P_ToNumeric: {
1ad34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ad35 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f /* same as TK_TO
1ad36 5f 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a 2f _NUMERIC, in1 */
1ad37 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c . if( (pIn1->fl
1ad38 61 67 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c ags & (MEM_Null|
1ad39 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c MEM_Int|MEM_Real
1ad3a 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c ))==0 ){. sql
1ad3b 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 ite3VdbeMemNumer
1ad3c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 ify(pIn1);. }.
1ad3d 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1ad3e 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1ad3f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f CAST */../* Opco
1ad40 64 65 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a de: ToInt P1 * *
1ad41 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 * *.**.** Force
1ad42 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1ad43 67 69 73 74 65 72 20 50 31 20 62 65 20 61 6e 20 gister P1 be an
1ad44 69 6e 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 integer. If.**
1ad45 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 72 The value is cur
1ad46 72 65 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 rently a real nu
1ad47 6d 62 65 72 2c 20 64 72 6f 70 20 69 74 73 20 66 mber, drop its f
1ad48 72 61 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a ractional part..
1ad49 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 ** If the value
1ad4a 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c is text or blob,
1ad4b 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 try to convert
1ad4c 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 it to an integer
1ad4d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 using the.** eq
1ad4e 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 uivalent of atoi
1ad4f 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 () and store 0 i
1ad50 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 f no such conver
1ad51 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 sion is possible
1ad52 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 ..**.** A NULL v
1ad53 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e alue is not chan
1ad54 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 ged by this rout
1ad55 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 ine. It remains
1ad56 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f NULL..*/.case O
1ad57 50 5f 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 P_ToInt: {
1ad58 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1ad59 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 ame as TK_TO_INT
1ad5a 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 , in1 */. if( (
1ad5b 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1ad5c 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 M_Null)==0 ){.
1ad5d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1ad5e 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 Integerify(pIn1)
1ad5f 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1ad60 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1ad61 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 _OMIT_CAST./* Op
1ad62 63 6f 64 65 3a 20 54 6f 52 65 61 6c 20 50 31 20 code: ToReal P1
1ad63 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f * * * *.**.** Fo
1ad64 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e rce the value in
1ad65 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
1ad66 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f be a floating po
1ad67 69 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 49 int number..** I
1ad68 66 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 f The value is c
1ad69 75 72 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74 65 urrently an inte
1ad6a 67 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 2e ger, convert it.
1ad6b 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1ad6c 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 is text or blob
1ad6d 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 , try to convert
1ad6e 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 it to an intege
1ad6f 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 r using the.** e
1ad70 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f quivalent of ato
1ad71 69 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 2e i() and store 0.
1ad72 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 0 if no such con
1ad73 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 version is possi
1ad74 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c ble..**.** A NUL
1ad75 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 L value is not c
1ad76 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 hanged by this r
1ad77 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 outine. It rema
1ad78 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 ins NULL..*/.cas
1ad79 65 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b 20 20 e OP_ToReal: {
1ad7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ad7b 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f /* same as TK_TO
1ad7c 5f 52 45 41 4c 2c 20 69 6e 31 20 2a 2f 0a 20 20 _REAL, in1 */.
1ad7d 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1ad7e 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 & MEM_Null)==0
1ad7f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1ad80 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e beMemRealify(pIn
1ad81 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1);. }. break;
1ad82 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1ad83 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f ITE_OMIT_CAST */
1ad84 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 ../* Opcode: Lt
1ad85 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1ad86 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 *.** Compare the
1ad87 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 values in regis
1ad88 74 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 ter P1 and P3.
1ad89 49 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 If reg(P3)<reg(P
1ad8a 31 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 1) then.** jump
1ad8b 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 to address P2.
1ad8c 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 .**.** If the SQ
1ad8d 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 LITE_JUMPIFNULL
1ad8e 62 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 bit of P5 is set
1ad8f 20 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 and either reg(
1ad90 50 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 P1) or.** reg(P3
1ad91 29 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 ) is NULL then t
1ad92 61 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 ake the jump. I
1ad93 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d f the SQLITE_JUM
1ad94 50 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 PIFNULL .** bit
1ad95 69 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 is clear then fa
1ad96 6c 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 65 ll thru if eithe
1ad97 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c r operand is NUL
1ad98 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c L..**.** The SQL
1ad99 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 ITE_AFF_MASK por
1ad9a 74 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 tion of P5 must
1ad9b 62 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 be an affinity c
1ad9c 68 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 haracter -.** SQ
1ad9d 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 LITE_AFF_TEXT, S
1ad9e 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
1ad9f 52 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e R, and so forth.
1ada0 20 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d An attempt is m
1ada1 61 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 ade .** to coerc
1ada2 65 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 e both inputs ac
1ada3 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 cording to this
1ada4 61 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 affinity before
1ada5 74 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f the.** compariso
1ada6 6e 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 n is made. If th
1ada7 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 e SQLITE_AFF_MAS
1ada8 4b 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 K is 0x00, then
1ada9 6e 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e numeric.** affin
1adaa 69 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 ity is used. Not
1adab 65 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e e that the affin
1adac 69 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 ity conversions
1adad 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 are stored.** ba
1adae 63 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 ck into the inpu
1adaf 74 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 t registers P1 a
1adb0 6e 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 nd P3. So this
1adb1 6f 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 opcode can cause
1adb2 0a 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 .** persistent c
1adb3 68 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 hanges to regist
1adb4 65 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a ers P1 and P3..*
1adb5 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f *.** Once any co
1adb6 6e 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 nversions have t
1adb7 61 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 aken place, and
1adb8 6e 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 neither value is
1adb9 20 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 NULL, .** the v
1adba 61 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 alues are compar
1adbb 65 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 ed. If both valu
1adbc 65 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 es are blobs the
1adbd 6e 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a n memcmp() is.**
1adbe 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 used to determi
1adbf 6e 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f ne the results o
1adc0 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e f the comparison
1adc1 2e 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 . If both value
1adc2 73 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 s.** are text, t
1adc3 68 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 hen the appropri
1adc4 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 ate collating fu
1adc5 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 nction specified
1adc6 20 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 in.** P4 is us
1adc7 65 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d ed to do the com
1adc8 70 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 parison. If P4
1adc9 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 is not specified
1adca 20 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 then.** memcmp(
1adcb 29 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d ) is used to com
1adcc 70 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 pare text string
1adcd 2e 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 . If both value
1adce 73 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 s are.** numeric
1adcf 2c 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 , then a numeric
1add0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 comparison is u
1add1 73 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 sed. If the two
1add2 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 values.** are of
1add3 20 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 different types
1add4 2c 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 , then numbers a
1add5 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 re considered le
1add6 73 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e ss than.** strin
1add7 67 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 gs and strings a
1add8 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 re considered le
1add9 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a ss than blobs..*
1adda 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 *.** If the SQLI
1addb 54 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f TE_STOREP2 bit o
1addc 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 f P5 is set, the
1addd 6e 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 n do not jump.
1adde 49 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 Instead,.** stor
1addf 65 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 e a boolean resu
1ade0 6c 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 lt (either 0, or
1ade1 20 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 1, or NULL) in
1ade2 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a register P2..*/.
1ade3 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 /* Opcode: Ne P1
1ade4 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a P2 P3 P4 P5.**.
1ade5 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 ** This works ju
1ade6 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f st like the Lt o
1ade7 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 pcode except tha
1ade8 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 t the jump is ta
1ade9 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 ken if.** the op
1adea 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 erands in regist
1adeb 65 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 ers P1 and P3 ar
1adec 65 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 e not equal. Se
1aded 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 e the Lt opcode
1adee 66 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 for.** additiona
1adef 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
1adf0 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 71 20 /./* Opcode: Eq
1adf1 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1adf2 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 *.** This works
1adf3 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 just like the Lt
1adf4 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 opcode except t
1adf5 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 hat the jump is
1adf6 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 taken if.** the
1adf7 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69 operands in regi
1adf8 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 20 sters P1 and P3
1adf9 61 72 65 20 65 71 75 61 6c 2e 0a 2a 2a 20 53 65 are equal..** Se
1adfa 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 e the Lt opcode
1adfb 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
1adfc 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f nformation..*/./
1adfd 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 20 * Opcode: Le P1
1adfe 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
1adff 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 * This works jus
1ae00 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 t like the Lt op
1ae01 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 code except that
1ae02 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b the jump is tak
1ae03 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e en if.** the con
1ae04 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 tent of register
1ae05 20 50 33 20 69 73 20 6c 65 73 73 20 74 68 61 6e P3 is less than
1ae06 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
1ae07 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 content of.** r
1ae08 65 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 egister P1. See
1ae09 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 the Lt opcode f
1ae0a 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
1ae0b 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a formation..*/./*
1ae0c 20 4f 70 63 6f 64 65 3a 20 47 74 20 50 31 20 50 Opcode: Gt P1 P
1ae0d 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
1ae0e 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 This works just
1ae0f 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 like the Lt opc
1ae10 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 ode except that
1ae11 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 the jump is take
1ae12 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 n if.** the cont
1ae13 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 ent of register
1ae14 50 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 P3 is greater th
1ae15 61 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f an the content o
1ae16 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 f.** register P1
1ae17 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 . See the Lt op
1ae18 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f code for additio
1ae19 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
1ae1a 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 .*/./* Opcode: G
1ae1b 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 e P1 P2 P3 P4 P5
1ae1c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b .**.** This work
1ae1d 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 s just like the
1ae1e 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 Lt opcode except
1ae1f 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 that the jump i
1ae20 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 s taken if.** th
1ae21 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 e content of reg
1ae22 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65 61 ister P3 is grea
1ae23 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ter than or equa
1ae24 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 l to the content
1ae25 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 of.** register
1ae26 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 P1. See the Lt
1ae27 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 opcode for addit
1ae28 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
1ae29 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 71 n..*/.case OP_Eq
1ae2a 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 :
1ae2b 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 /* same as TK_EQ
1ae2c 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 , jump, in1, in3
1ae2d 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a 20 */.case OP_Ne:
1ae2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ae2f 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c 20 same as TK_NE,
1ae30 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a jump, in1, in3 *
1ae31 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20 /.case OP_Lt:
1ae32 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1ae33 61 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6a 75 ame as TK_LT, ju
1ae34 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a mp, in1, in3 */.
1ae35 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 20 case OP_Le:
1ae36 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1ae37 65 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 6d 70 e as TK_LE, jump
1ae38 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 , in1, in3 */.ca
1ae39 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 20 20 20 se OP_Gt:
1ae3a 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1ae3b 61 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 2c 20 as TK_GT, jump,
1ae3c 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 in1, in3 */.case
1ae3d 20 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20 20 20 OP_Ge: {
1ae3e 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1ae3f 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 69 6e TK_GE, jump, in
1ae40 31 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 1, in3 */. int
1ae41 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72 65 73 flags;. int res
1ae42 3b 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 ;. char affinit
1ae43 79 3b 0a 0a 20 20 66 6c 61 67 73 20 3d 20 70 49 y;.. flags = pI
1ae44 6e 31 2d 3e 66 6c 61 67 73 7c 70 49 6e 33 2d 3e n1->flags|pIn3->
1ae45 66 6c 61 67 73 3b 0a 0a 20 20 69 66 28 20 66 6c flags;.. if( fl
1ae46 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a ags&MEM_Null ){.
1ae47 20 20 20 20 2f 2a 20 49 66 20 65 69 74 68 65 72 /* If either
1ae48 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
1ae49 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 then the result
1ae4a 20 69 73 20 61 6c 77 61 79 73 20 4e 55 4c 4c 2e is always NULL.
1ae4b 0a 20 20 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 . ** The jump
1ae4c 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 is taken if the
1ae4d 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 SQLITE_JUMPIFNU
1ae4e 4c 4c 20 62 69 74 20 69 73 20 73 65 74 2e 0a 20 LL bit is set..
1ae4f 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f */. if( pO
1ae50 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 p->p5 & SQLITE_S
1ae51 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 20 20 TOREP2 ){.
1ae52 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pOut = &p->aMem[
1ae53 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 pOp->p2];.
1ae54 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
1ae55 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a Out, MEM_Null);.
1ae56 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 REGISTER_T
1ae57 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f RACE(pOp->p2, pO
1ae58 75 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 ut);. }else i
1ae59 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c f( pOp->p5 & SQL
1ae5a 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 29 ITE_JUMPIFNULL )
1ae5b 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 {. pc = pOp
1ae5c 2d 3e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 ->p2-1;. }.
1ae5d 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 break;. }..
1ae5e 61 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e affinity = pOp->
1ae5f 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f p5 & SQLITE_AFF_
1ae60 4d 41 53 4b 3b 0a 20 20 69 66 28 20 61 66 66 69 MASK;. if( affi
1ae61 6e 69 74 79 20 29 7b 0a 20 20 20 20 61 70 70 6c nity ){. appl
1ae62 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 yAffinity(pIn1,
1ae63 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 affinity, encodi
1ae64 6e 67 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66 ng);. applyAf
1ae65 66 69 6e 69 74 79 28 70 49 6e 33 2c 20 61 66 66 finity(pIn3, aff
1ae66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 inity, encoding)
1ae67 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 ;. if( db->ma
1ae68 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 llocFailed ) got
1ae69 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 o no_mem;. }..
1ae6a 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1ae6b 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 type==P4_COLLSEQ
1ae6c 20 7c 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c || pOp->p4.pCol
1ae6d 6c 3d 3d 30 20 29 3b 0a 20 20 45 78 70 61 6e 64 l==0 );. Expand
1ae6e 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 45 78 Blob(pIn1);. Ex
1ae6f 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a pandBlob(pIn3);.
1ae70 20 20 72 65 73 20 3d 20 73 71 6c 69 74 65 33 4d res = sqlite3M
1ae71 65 6d 43 6f 6d 70 61 72 65 28 70 49 6e 33 2c 20 emCompare(pIn3,
1ae72 70 49 6e 31 2c 20 70 4f 70 2d 3e 70 34 2e 70 43 pIn1, pOp->p4.pC
1ae73 6f 6c 6c 29 3b 0a 20 20 73 77 69 74 63 68 28 20 oll);. switch(
1ae74 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 pOp->opcode ){.
1ae75 20 20 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 case OP_Eq:
1ae76 20 20 72 65 73 20 3d 20 72 65 73 3d 3d 30 3b 20 res = res==0;
1ae77 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 break;. c
1ae78 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20 20 72 65 ase OP_Ne: re
1ae79 73 20 3d 20 72 65 73 21 3d 30 3b 20 20 20 20 20 s = res!=0;
1ae7a 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
1ae7b 4f 50 5f 4c 74 3a 20 20 20 20 72 65 73 20 3d 20 OP_Lt: res =
1ae7c 72 65 73 3c 30 3b 20 20 20 20 20 20 62 72 65 61 res<0; brea
1ae7d 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c k;. case OP_L
1ae7e 65 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3c e: res = res<
1ae7f 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 =0; break;.
1ae80 20 20 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 case OP_Gt:
1ae81 20 20 72 65 73 20 3d 20 72 65 73 3e 30 3b 20 20 res = res>0;
1ae82 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 break;. d
1ae83 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 72 65 efault: re
1ae84 73 20 3d 20 72 65 73 3e 3d 30 3b 20 20 20 20 20 s = res>=0;
1ae85 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 break;. }.. if
1ae86 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 ( pOp->p5 & SQLI
1ae87 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 TE_STOREP2 ){.
1ae88 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 pOut = &p->aMe
1ae89 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 m[pOp->p2];.
1ae8a 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
1ae8b 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 Out, MEM_Int);.
1ae8c 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 72 pOut->u.i = r
1ae8d 65 73 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 es;. REGISTER
1ae8e 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 _TRACE(pOp->p2,
1ae8f 70 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 pOut);. }else i
1ae90 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 70 63 f( res ){. pc
1ae91 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 = pOp->p2-1;.
1ae92 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1ae93 20 4f 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 Opcode: Permuta
1ae94 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a tion * * * P4 *.
1ae95 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65 **.** Set the pe
1ae96 72 6d 75 61 74 69 6f 6e 20 75 73 65 64 20 62 79 rmuation used by
1ae97 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 the OP_Compare
1ae98 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 74 operator to be t
1ae99 68 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 he array.** of i
1ae9a 6e 74 65 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a ntegers in P4..*
1ae9b 2a 0a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 61 *.** The permuta
1ae9c 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c tion is only val
1ae9d 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 id until the nex
1ae9e 74 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e t OP_Permutation
1ae9f 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a , OP_Compare,.**
1aea0 20 4f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f OP_Halt, or OP_
1aea1 52 65 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 ResultRow. Typi
1aea2 63 61 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 cally the OP_Per
1aea3 6d 75 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 mutation should
1aea4 6f 63 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 occur.** immedia
1aea5 74 65 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 68 tely prior to th
1aea6 65 20 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f e OP_Compare..*/
1aea7 0a 63 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 .case OP_Permuta
1aea8 74 69 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 tion: {. assert
1aea9 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1aeaa 34 5f 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 4_INTARRAY );.
1aeab 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e assert( pOp->p4.
1aeac 61 69 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 ai );. aPermute
1aead 20 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 = pOp->p4.ai;.
1aeae 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1aeaf 63 6f 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 code: Compare P1
1aeb0 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1aeb1 2a 20 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 * Compare to vec
1aeb2 74 6f 72 73 20 6f 66 20 72 65 67 69 73 74 65 72 tors of register
1aeb3 73 20 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 s in reg(P1)..re
1aeb4 67 28 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 g(P1+P3-1) (all
1aeb5 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 this.** one "A")
1aeb6 20 61 6e 64 20 69 6e 20 72 65 67 28 50 32 29 2e and in reg(P2).
1aeb7 2e 72 65 67 28 50 32 2b 50 33 2d 31 29 20 28 22 .reg(P2+P3-1) ("
1aeb8 42 22 29 2e 20 20 53 61 76 65 20 74 68 65 20 72 B"). Save the r
1aeb9 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 esult of.** the
1aeba 63 6f 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 comparison for u
1aebb 73 65 20 62 79 20 74 68 65 20 6e 65 78 74 20 4f se by the next O
1aebc 50 5f 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 2e P_Jump instruct.
1aebd 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b .**.** P4 is a K
1aebe 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 eyInfo structure
1aebf 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f that defines co
1aec0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
1aec1 73 20 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 s and sort.** or
1aec2 64 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d ders for the com
1aec3 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 parison. The pe
1aec4 72 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 rmutation applie
1aec5 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a s to registers.*
1aec6 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 * only. The Key
1aec7 49 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 Info elements ar
1aec8 65 20 75 73 65 64 20 73 65 71 75 65 6e 74 69 61 e used sequentia
1aec9 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 lly..**.** The c
1aeca 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 73 omparison is a s
1aecb 6f 72 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 ort comparison,
1aecc 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 so NULLs compare
1aecd 20 65 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 equal,.** NULLs
1aece 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6e are less than n
1aecf 75 6d 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 umbers, numbers
1aed0 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 73 74 are less than st
1aed1 72 69 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 rings,.** and st
1aed2 72 69 6e 67 73 20 61 72 65 20 6c 65 73 73 20 74 rings are less t
1aed3 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 han blobs..*/.ca
1aed4 73 65 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b se OP_Compare: {
1aed5 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e . int n = pOp->
1aed6 70 33 3b 0a 20 20 69 6e 74 20 69 2c 20 70 31 2c p3;. int i, p1,
1aed7 20 70 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 p2;. const Key
1aed8 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d Info *pKeyInfo =
1aed9 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 pOp->p4.pKeyInf
1aeda 6f 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 o;. assert( n>0
1aedb 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b );. assert( pK
1aedc 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 70 eyInfo!=0 );. p
1aedd 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 1 = pOp->p1;. a
1aede 73 73 65 72 74 28 20 70 31 3e 30 20 26 26 20 70 ssert( p1>0 && p
1aedf 31 2b 6e 2d 31 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 1+n-1<p->nMem );
1aee0 0a 20 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b . p2 = pOp->p2;
1aee1 0a 20 20 61 73 73 65 72 74 28 20 70 32 3e 30 20 . assert( p2>0
1aee2 26 26 20 70 32 2b 6e 2d 31 3c 70 2d 3e 6e 4d 65 && p2+n-1<p->nMe
1aee3 6d 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 m );. for(i=0;
1aee4 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 i<n; i++){. i
1aee5 6e 74 20 69 64 78 20 3d 20 61 50 65 72 6d 75 74 nt idx = aPermut
1aee6 65 20 3f 20 61 50 65 72 6d 75 74 65 5b 69 5d 20 e ? aPermute[i]
1aee7 3a 20 69 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 : i;. CollSeq
1aee8 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 *pColl; /* C
1aee9 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1aeea 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 e to use on this
1aeeb 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 term */. int
1aeec 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 bRev;
1aeed 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 /* True for DESC
1aeee 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 ENDING sort orde
1aeef 72 20 2a 2f 0a 20 20 20 20 52 45 47 49 53 54 45 r */. REGISTE
1aef0 52 5f 54 52 41 43 45 28 70 31 2b 69 64 78 2c 20 R_TRACE(p1+idx,
1aef1 26 70 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 5d &p->aMem[p1+idx]
1aef2 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f );. REGISTER_
1aef3 54 52 41 43 45 28 70 32 2b 69 64 78 2c 20 26 70 TRACE(p2+idx, &p
1aef4 2d 3e 61 4d 65 6d 5b 70 32 2b 69 64 78 5d 29 3b ->aMem[p2+idx]);
1aef5 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 70 . assert( i<p
1aef6 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 KeyInfo->nField
1aef7 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 );. pColl = p
1aef8 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 KeyInfo->aColl[i
1aef9 5d 3b 0a 20 20 20 20 62 52 65 76 20 3d 20 70 4b ];. bRev = pK
1aefa 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 eyInfo->aSortOrd
1aefb 65 72 5b 69 5d 3b 0a 20 20 20 20 69 43 6f 6d 70 er[i];. iComp
1aefc 61 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d are = sqlite3Mem
1aefd 43 6f 6d 70 61 72 65 28 26 70 2d 3e 61 4d 65 6d Compare(&p->aMem
1aefe 5b 70 31 2b 69 64 78 5d 2c 20 26 70 2d 3e 61 4d [p1+idx], &p->aM
1aeff 65 6d 5b 70 32 2b 69 64 78 5d 2c 20 70 43 6f 6c em[p2+idx], pCol
1af00 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6d l);. if( iCom
1af01 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20 69 66 pare ){. if
1af02 28 20 62 52 65 76 20 29 20 69 43 6f 6d 70 61 72 ( bRev ) iCompar
1af03 65 20 3d 20 2d 69 43 6f 6d 70 61 72 65 3b 0a 20 e = -iCompare;.
1af04 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1af05 7d 0a 20 20 7d 0a 20 20 61 50 65 72 6d 75 74 65 }. }. aPermute
1af06 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d = 0;. break;.}
1af07 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a 75 6d ../* Opcode: Jum
1af08 70 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a p P1 P2 P3 * *.*
1af09 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 *.** Jump to the
1af0a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 20 instruction at
1af0b 61 64 64 72 65 73 73 20 50 31 2c 20 50 32 2c 20 address P1, P2,
1af0c 6f 72 20 50 33 20 64 65 70 65 6e 64 69 6e 67 20 or P3 depending
1af0d 6f 6e 20 77 68 65 74 68 65 72 0a 2a 2a 20 69 6e on whether.** in
1af0e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
1af0f 20 4f 50 5f 43 6f 6d 70 61 72 65 20 69 6e 73 74 OP_Compare inst
1af10 72 75 63 74 69 6f 6e 20 74 68 65 20 50 31 20 76 ruction the P1 v
1af11 65 63 74 6f 72 20 77 61 73 20 6c 65 73 73 20 74 ector was less t
1af12 68 61 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c han.** equal to,
1af13 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e or greater than
1af14 20 74 68 65 20 50 32 20 76 65 63 74 6f 72 2c 20 the P2 vector,
1af15 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f respectively..*/
1af16 0a 63 61 73 65 20 4f 50 5f 4a 75 6d 70 3a 20 7b .case OP_Jump: {
1af17 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1af18 6a 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 69 43 jump */. if( iC
1af19 6f 6d 70 61 72 65 3c 30 20 29 7b 0a 20 20 20 20 ompare<0 ){.
1af1a 70 63 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 pc = pOp->p1 - 1
1af1b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 43 ;. }else if( iC
1af1c 6f 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20 ompare==0 ){.
1af1d 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1af1e 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
1af1f 70 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20 31 pc = pOp->p3 - 1
1af20 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1af21 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e 64 ../* Opcode: And
1af22 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1af23 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 .** Take the log
1af24 69 63 61 6c 20 41 4e 44 20 6f 66 20 74 68 65 20 ical AND of the
1af25 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 values in regist
1af26 65 72 73 20 50 31 20 61 6e 64 20 50 32 20 61 6e ers P1 and P2 an
1af27 64 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 20 72 d.** write the r
1af28 65 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 esult into regis
1af29 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 ter P3..**.** If
1af2a 20 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 either P1 or P2
1af2b 20 69 73 20 30 20 28 66 61 6c 73 65 29 20 74 68 is 0 (false) th
1af2c 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 en the result is
1af2d 20 30 20 65 76 65 6e 20 69 66 0a 2a 2a 20 74 68 0 even if.** th
1af2e 65 20 6f 74 68 65 72 20 69 6e 70 75 74 20 69 73 e other input is
1af2f 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20 61 NULL. A NULL a
1af30 6e 64 20 74 72 75 65 20 6f 72 20 74 77 6f 20 4e nd true or two N
1af31 55 4c 4c 73 20 67 69 76 65 0a 2a 2a 20 61 20 4e ULLs give.** a N
1af32 55 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a 2f ULL output..*/./
1af33 2a 20 4f 70 63 6f 64 65 3a 20 4f 72 20 50 31 20 * Opcode: Or P1
1af34 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
1af35 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 6c Take the logical
1af36 20 4f 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65 OR of the value
1af37 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 s in register P1
1af38 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 and P2 and.** s
1af39 74 6f 72 65 20 74 68 65 20 61 6e 73 77 65 72 20 tore the answer
1af3a 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a in register P3..
1af3b 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 **.** If either
1af3c 50 31 20 6f 72 20 50 32 20 69 73 20 6e 6f 6e 7a P1 or P2 is nonz
1af3d 65 72 6f 20 28 74 72 75 65 29 20 74 68 65 6e 20 ero (true) then
1af3e 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 31 20 the result is 1
1af3f 28 74 72 75 65 29 0a 2a 2a 20 65 76 65 6e 20 69 (true).** even i
1af40 66 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75 f the other inpu
1af41 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 t is NULL. A NU
1af42 4c 4c 20 61 6e 64 20 66 61 6c 73 65 20 6f 72 20 LL and false or
1af43 74 77 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 69 76 two NULLs.** giv
1af44 65 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e e a NULL output.
1af45 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64 3a .*/.case OP_And:
1af46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1af47 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 2c same as TK_AND,
1af48 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 in1, in2, out3
1af49 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20 7b */.case OP_Or: {
1af4a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1af4b 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20 69 same as TK_OR, i
1af4c 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1af4d 0a 20 20 69 6e 74 20 76 31 2c 20 76 32 3b 20 20 . int v1, v2;
1af4e 20 20 2f 2a 20 30 3d 3d 46 41 4c 53 45 2c 20 31 /* 0==FALSE, 1
1af4f 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f ==TRUE, 2==UNKNO
1af50 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 WN or NULL */..
1af51 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 if( pIn1->flags
1af52 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 & MEM_Null ){.
1af53 20 20 20 76 31 20 3d 20 32 3b 0a 20 20 7d 65 6c v1 = 2;. }el
1af54 73 65 7b 0a 20 20 20 20 76 31 20 3d 20 73 71 6c se{. v1 = sql
1af55 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 ite3VdbeIntValue
1af56 28 70 49 6e 31 29 21 3d 30 3b 0a 20 20 7d 0a 20 (pIn1)!=0;. }.
1af57 20 69 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 if( pIn2->flags
1af58 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 & MEM_Null ){.
1af59 20 20 20 76 32 20 3d 20 32 3b 0a 20 20 7d 65 6c v2 = 2;. }el
1af5a 73 65 7b 0a 20 20 20 20 76 32 20 3d 20 73 71 6c se{. v2 = sql
1af5b 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 ite3VdbeIntValue
1af5c 28 70 49 6e 32 29 21 3d 30 3b 0a 20 20 7d 0a 20 (pIn2)!=0;. }.
1af5d 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1af5e 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20 20 ==OP_And ){.
1af5f 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
1af60 69 67 6e 65 64 20 63 68 61 72 20 61 6e 64 5f 6c igned char and_l
1af61 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c ogic[] = { 0, 0,
1af62 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 0, 0, 1, 2, 0,
1af63 32 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31 20 3d 2, 2 };. v1 =
1af64 20 61 6e 64 5f 6c 6f 67 69 63 5b 76 31 2a 33 2b and_logic[v1*3+
1af65 76 32 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 v2];. }else{.
1af66 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 static const u
1af67 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 72 5f nsigned char or_
1af68 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 31 logic[] = { 0, 1
1af69 2c 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32 2c , 2, 1, 1, 1, 2,
1af6a 20 31 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31 20 1, 2 };. v1
1af6b 3d 20 6f 72 5f 6c 6f 67 69 63 5b 76 31 2a 33 2b = or_logic[v1*3+
1af6c 76 32 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76 v2];. }. if( v
1af6d 31 3d 3d 32 20 29 7b 0a 20 20 20 20 4d 65 6d 53 1==2 ){. MemS
1af6e 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c etTypeFlag(pOut,
1af6f 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 65 MEM_Null);. }e
1af70 6c 73 65 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 lse{. pOut->u
1af71 2e 69 20 3d 20 76 31 3b 0a 20 20 20 20 4d 65 6d .i = v1;. Mem
1af72 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
1af73 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a , MEM_Int);. }.
1af74 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1af75 70 63 6f 64 65 3a 20 4e 6f 74 20 50 31 20 50 32 pcode: Not P1 P2
1af76 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 * * *.**.** Int
1af77 65 72 70 72 65 74 20 74 68 65 20 76 61 6c 75 65 erpret the value
1af78 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1af79 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c as a boolean val
1af7a 75 65 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a ue. Store the.*
1af7b 2a 20 62 6f 6f 6c 65 61 6e 20 63 6f 6d 70 6c 65 * boolean comple
1af7c 6d 65 6e 74 20 69 6e 20 72 65 67 69 73 74 65 72 ment in register
1af7d 20 50 32 2e 20 20 49 66 20 74 68 65 20 76 61 6c P2. If the val
1af7e 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1af7f 31 20 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 1 is .** NULL, t
1af80 68 65 6e 20 61 20 4e 55 4c 4c 20 69 73 20 73 74 hen a NULL is st
1af81 6f 72 65 64 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 ored in P2..*/.c
1af82 61 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20 20 ase OP_Not: {
1af83 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1af84 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 2c 20 same as TK_NOT,
1af85 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d 20 in1 */. pOut =
1af86 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1af87 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 ];. if( pIn1->f
1af88 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
1af89 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1af8a 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 beMemSetNull(pOu
1af8b 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
1af8c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1af8d 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 21 73 etInt64(pOut, !s
1af8e 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1af8f 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20 ue(pIn1));. }.
1af90 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1af91 63 6f 64 65 3a 20 42 69 74 4e 6f 74 20 50 31 20 code: BitNot P1
1af92 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 P2 * * *.**.** I
1af93 6e 74 65 72 70 72 65 74 20 74 68 65 20 63 6f 6e nterpret the con
1af94 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 tent of register
1af95 20 50 31 20 61 73 20 61 6e 20 69 6e 74 65 67 65 P1 as an intege
1af96 72 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a 2a r. Store the.**
1af97 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74 ones-complement
1af98 20 6f 66 20 74 68 65 20 50 31 20 76 61 6c 75 65 of the P1 value
1af99 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1af9a 32 2e 20 20 49 66 20 50 31 20 68 6f 6c 64 73 0a 2. If P1 holds.
1af9b 2a 2a 20 61 20 4e 55 4c 4c 20 74 68 65 6e 20 73 ** a NULL then s
1af9c 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 50 tore a NULL in P
1af9d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 2..*/.case OP_Bi
1af9e 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 tNot: {
1af9f 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1afa0 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 20 2a 2f K_BITNOT, in1 */
1afa1 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d . pOut = &p->aM
1afa2 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 em[pOp->p2];. i
1afa3 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 f( pIn1->flags &
1afa4 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
1afa5 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1afa6 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 etNull(pOut);.
1afa7 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
1afa8 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
1afa9 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 74 65 33 4(pOut, ~sqlite3
1afaa 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e VdbeIntValue(pIn
1afab 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 1));. }. break
1afac 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1afad 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a If P1 P2 P3 * *.
1afae 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 **.** Jump to P2
1afaf 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e if the value in
1afb0 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1afb1 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65 true. The value
1afb2 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 is.** is consid
1afb3 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20 ered true if it
1afb4 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e is numeric and n
1afb5 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 on-zero. If the
1afb6 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 value.** in P1
1afb7 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b is NULL then tak
1afb8 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 e the jump if P3
1afb9 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20 is true..*/./*
1afba 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31 Opcode: IfNot P1
1afbb 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1afbc 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 Jump to P2 if t
1afbd 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1afbe 73 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 65 ster P1 is False
1afbf 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a . The value is.
1afc0 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ** is considered
1afc1 20 74 72 75 65 20 69 66 20 69 74 20 68 61 73 20 true if it has
1afc2 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 a numeric value
1afc3 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 of zero. If the
1afc4 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 value.** in P1
1afc5 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b is NULL then tak
1afc6 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 e the jump if P3
1afc7 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73 is true..*/.cas
1afc8 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 20 e OP_If:
1afc9 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 /* jump
1afca 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f 50 , in1 */.case OP
1afcb 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 _IfNot: {
1afcc 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1afcd 31 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 20 20 1 */. int c;.
1afce 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 if( pIn1->flags
1afcf 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 & MEM_Null ){.
1afd0 20 20 63 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 c = pOp->p3;.
1afd1 20 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 }else{.#ifdef S
1afd2 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
1afd3 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 63 20 ING_POINT. c
1afd4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 = sqlite3VdbeInt
1afd5 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 23 65 6c Value(pIn1);.#el
1afd6 73 65 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 se. c = sqlit
1afd7 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 e3VdbeRealValue(
1afd8 70 49 6e 31 29 21 3d 30 2e 30 3b 0a 23 65 6e 64 pIn1)!=0.0;.#end
1afd9 69 66 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e if. if( pOp->
1afda 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74 opcode==OP_IfNot
1afdb 20 29 20 63 20 3d 20 21 63 3b 0a 20 20 7d 0a 20 ) c = !c;. }.
1afdc 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 70 63 if( c ){. pc
1afdd 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 = pOp->p2-1;.
1afde 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1afdf 20 4f 70 63 6f 64 65 3a 20 49 73 4e 75 6c 6c 20 Opcode: IsNull
1afe0 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1afe1 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 ** Jump to P2 if
1afe2 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1afe3 67 69 73 74 65 72 20 50 31 20 69 73 20 4e 55 4c gister P1 is NUL
1afe4 4c 2e 20 20 49 66 20 50 33 20 69 73 20 67 72 65 L. If P3 is gre
1afe5 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 7a 65 72 ater.** than zer
1afe6 6f 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 61 6c o, then check al
1afe7 6c 20 76 61 6c 75 65 73 20 72 65 67 28 50 31 29 l values reg(P1)
1afe8 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 0a 2a 2a , reg(P1+1), .**
1afe9 20 72 65 67 28 50 31 2b 32 29 2c 20 2e 2e 2e 2c reg(P1+2), ...,
1afea 20 72 65 67 28 50 31 2b 50 33 2d 31 29 2e 0a 2a reg(P1+P3-1)..*
1afeb 2f 0a 63 61 73 65 20 4f 50 5f 49 73 4e 75 6c 6c /.case OP_IsNull
1afec 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f : { /
1afed 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e * same as TK_ISN
1afee 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a ULL, jump, in1 *
1afef 2f 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d /. int n = pOp-
1aff0 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28 20 70 >p3;. assert( p
1aff1 4f 70 2d 3e 70 33 3d 3d 30 20 7c 7c 20 70 4f 70 Op->p3==0 || pOp
1aff2 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 64 6f 7b 0a ->p1>0 );. do{.
1aff3 20 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 if( (pIn1->f
1aff4 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
1aff5 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 !=0 ){. pc
1aff6 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1aff7 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1aff8 7d 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20 }. pIn1++;.
1aff9 7d 77 68 69 6c 65 28 20 2d 2d 6e 20 3e 20 30 20 }while( --n > 0
1affa 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1affb 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c * Opcode: NotNul
1affc 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a l P1 P2 * * *.**
1affd 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 .** Jump to P2 i
1affe 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 f the value in r
1afff 65 67 69 73 74 65 72 20 50 31 20 69 73 20 6e 6f egister P1 is no
1b000 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73 t NULL. .*/.cas
1b001 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 e OP_NotNull: {
1b002 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1b003 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c me as TK_NOTNULL
1b004 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 , jump, in1 */.
1b005 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 if( (pIn1->flag
1b006 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 s & MEM_Null)==0
1b007 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
1b008 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 ->p2 - 1;. }.
1b009 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1b00a 6f 64 65 3a 20 53 65 74 4e 75 6d 43 6f 6c 75 6d ode: SetNumColum
1b00b 6e 73 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a ns * P2 * * *.**
1b00c 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 .** This opcode
1b00d 73 65 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 sets the number
1b00e 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 of columns for t
1b00f 68 65 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 he cursor opened
1b010 20 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f by the.** follo
1b011 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e wing instruction
1b012 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 6e to P2..**.** An
1b013 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e OP_SetNumColumn
1b014 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 66 75 6c s is only useful
1b015 20 69 66 20 69 74 20 6f 63 63 75 72 73 20 69 6d if it occurs im
1b016 6d 65 64 69 61 74 65 6c 79 20 62 65 66 6f 72 65 mediately before
1b017 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 .** one of the
1b018 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64 65 following opcode
1b019 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4f 70 65 s:.**.** Ope
1b01a 6e 52 65 61 64 0a 2a 2a 20 20 20 20 20 4f 70 65 nRead.** Ope
1b01b 6e 57 72 69 74 65 0a 2a 2a 20 20 20 20 20 4f 70 nWrite.** Op
1b01c 65 6e 50 73 65 75 64 6f 0a 2a 2a 0a 2a 2a 20 49 enPseudo.**.** I
1b01d 66 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 f the OP_Column
1b01e 6f 70 63 6f 64 65 20 69 73 20 74 6f 20 62 65 20 opcode is to be
1b01f 65 78 65 63 75 74 65 64 20 6f 6e 20 61 20 63 75 executed on a cu
1b020 72 73 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 rsor, then.** th
1b021 69 73 20 6f 70 63 6f 64 65 20 6d 75 73 74 20 62 is opcode must b
1b022 65 20 70 72 65 73 65 6e 74 20 69 6d 6d 65 64 69 e present immedi
1b023 61 74 65 6c 79 20 62 65 66 6f 72 65 20 74 68 65 ately before the
1b024 20 6f 70 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 opcode that.**
1b025 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73 6f 72 opens the cursor
1b026 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 ..*/.case OP_Set
1b027 4e 75 6d 43 6f 6c 75 6d 6e 73 3a 20 7b 0a 20 20 NumColumns: {.
1b028 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1b029 6f 64 65 3a 20 43 6f 6c 75 6d 6e 20 50 31 20 50 ode: Column P1 P
1b02a 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
1b02b 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 64 61 Interpret the da
1b02c 74 61 20 74 68 61 74 20 63 75 72 73 6f 72 20 50 ta that cursor P
1b02d 31 20 70 6f 69 6e 74 73 20 74 6f 20 61 73 20 61 1 points to as a
1b02e 20 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 structure built
1b02f 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 4d 61 using.** the Ma
1b030 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 63 keRecord instruc
1b031 74 69 6f 6e 2e 20 20 28 53 65 65 20 74 68 65 20 tion. (See the
1b032 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 MakeRecord opcod
1b033 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c e for additional
1b034 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
1b035 61 62 6f 75 74 20 74 68 65 20 66 6f 72 6d 61 74 about the format
1b036 20 6f 66 20 74 68 65 20 64 61 74 61 2e 29 20 20 of the data.)
1b037 45 78 74 72 61 63 74 20 74 68 65 20 50 32 2d 74 Extract the P2-t
1b038 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 66 72 6f 6d h column.** from
1b039 20 74 68 69 73 20 72 65 63 6f 72 64 2e 20 20 49 this record. I
1b03a 66 20 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 f there are less
1b03b 20 74 68 61 74 20 28 50 32 2b 31 29 20 0a 2a 2a that (P2+1) .**
1b03c 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 72 values in the r
1b03d 65 63 6f 72 64 2c 20 65 78 74 72 61 63 74 20 61 ecord, extract a
1b03e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 NULL..**.** The
1b03f 20 76 61 6c 75 65 20 65 78 74 72 61 63 74 65 64 value extracted
1b040 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 is stored in re
1b041 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a gister P3..**.**
1b042 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 If the column c
1b043 6f 6e 74 61 69 6e 73 20 66 65 77 65 72 20 74 68 ontains fewer th
1b044 61 6e 20 50 32 20 66 69 65 6c 64 73 2c 20 74 68 an P2 fields, th
1b045 65 6e 20 65 78 74 72 61 63 74 20 61 20 4e 55 4c en extract a NUL
1b046 4c 2e 20 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 L. Or,.** if th
1b047 65 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 e P4 argument is
1b048 20 61 20 50 34 5f 4d 45 4d 20 75 73 65 20 74 68 a P4_MEM use th
1b049 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 e value of the P
1b04a 34 20 61 72 67 75 6d 65 6e 74 20 61 73 0a 2a 2a 4 argument as.**
1b04b 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a the result..*/.
1b04c 63 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a 20 case OP_Column:
1b04d 7b 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 53 {. int payloadS
1b04e 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ize; /* Number
1b04f 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
1b050 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 record */. int
1b051 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20 p1 = pOp->p1;
1b052 2f 2a 20 50 31 20 76 61 6c 75 65 20 6f 66 20 74 /* P1 value of t
1b053 68 65 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 he opcode */. i
1b054 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b nt p2 = pOp->p2;
1b055 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 /* column numb
1b056 65 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a er to retrieve *
1b057 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1b058 70 43 20 3d 20 30 3b 2f 2a 20 54 68 65 20 56 44 pC = 0;/* The VD
1b059 42 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 BE cursor */. c
1b05a 68 61 72 20 2a 7a 52 65 63 3b 20 20 20 20 20 20 har *zRec;
1b05b 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
1b05c 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 2d complete record-
1b05d 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 data */. BtCurs
1b05e 6f 72 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a 20 or *pCrsr; /*
1b05f 54 68 65 20 42 54 72 65 65 20 63 75 72 73 6f 72 The BTree cursor
1b060 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65 */. u32 *aType
1b061 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70 ; /* aTyp
1b062 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e e[i] holds the n
1b063 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20 74 umeric type of t
1b064 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a he i-th column *
1b065 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 /. u32 *aOffset
1b066 3b 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 73 65 ; /* aOffse
1b067 74 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 20 74 t[i] is offset t
1b068 6f 20 73 74 61 72 74 20 6f 66 20 64 61 74 61 20 o start of data
1b069 66 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 for i-th column
1b06a 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b */. int nField;
1b06b 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 /* numbe
1b06c 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 r of fields in t
1b06d 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 he record */. i
1b06e 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 nt len;
1b06f 20 20 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 /* The length
1b070 6f 66 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 of the serialize
1b071 64 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 d data for the c
1b072 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 olumn */. int i
1b073 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1b074 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
1b075 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 . char *zData;
1b076 20 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f 66 /* Part of
1b077 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e the record bein
1b078 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 4d g decoded */. M
1b079 65 6d 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 em *pDest;
1b07a 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 /* Where to wr
1b07b 69 74 65 20 74 68 65 20 65 78 74 72 61 63 74 65 ite the extracte
1b07c 64 20 76 61 6c 75 65 20 2a 2f 0a 20 20 4d 65 6d d value */. Mem
1b07d 20 73 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 sMem;
1b07e 2f 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 74 /* For storing t
1b07f 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 he record being
1b080 64 65 63 6f 64 65 64 20 2a 2f 0a 0a 20 20 6d 65 decoded */.. me
1b081 6d 73 65 74 28 26 73 4d 65 6d 2c 20 30 2c 20 73 mset(&sMem, 0, s
1b082 69 7a 65 6f 66 28 73 4d 65 6d 29 29 3b 0a 20 20 izeof(sMem));.
1b083 61 73 73 65 72 74 28 20 70 31 3c 70 2d 3e 6e 43 assert( p1<p->nC
1b084 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 ursor );. asser
1b085 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 t( pOp->p3>0 &&
1b086 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p3<=p->nMem
1b087 20 29 3b 0a 20 20 70 44 65 73 74 20 3d 20 26 70 );. pDest = &p
1b088 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
1b089 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1b08a 67 28 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 6c g(pDest, MEM_Nul
1b08b 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 62 l);.. /* This b
1b08c 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 76 61 lock sets the va
1b08d 72 69 61 62 6c 65 20 70 61 79 6c 6f 61 64 53 69 riable payloadSi
1b08e 7a 65 20 74 6f 20 62 65 20 74 68 65 20 74 6f 74 ze to be the tot
1b08f 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a al number of. *
1b090 2a 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 * bytes in the r
1b091 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a ecord.. **. **
1b092 20 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 zRec is set to
1b093 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 be the complete
1b094 74 65 78 74 20 6f 66 20 74 68 65 20 72 65 63 6f text of the reco
1b095 72 64 20 69 66 20 69 74 20 69 73 20 61 76 61 69 rd if it is avai
1b096 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68 65 20 lable.. ** The
1b097 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 20 complete record
1b098 74 65 78 74 20 69 73 20 61 6c 77 61 79 73 20 61 text is always a
1b099 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70 73 65 vailable for pse
1b09a 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a 2a 20 udo-tables. **
1b09b 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 If the record is
1b09c 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63 75 72 stored in a cur
1b09d 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c 65 74 sor, the complet
1b09e 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a 20 20 e record text.
1b09f 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76 61 69 ** might be avai
1b0a0 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20 70 43 lable in the pC
1b0a1 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 4f ->aRow cache. O
1b0a2 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 r it might not b
1b0a3 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 e.. ** If the d
1b0a4 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c 61 62 ata is unavailab
1b0a5 6c 65 2c 20 20 7a 52 65 63 20 69 73 20 73 65 74 le, zRec is set
1b0a6 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2a 0a 20 to NULL.. **.
1b0a7 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63 6f 6d 70 ** We also comp
1b0a8 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ute the number o
1b0a9 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
1b0aa 20 72 65 63 6f 72 64 2e 20 20 46 6f 72 20 63 75 record. For cu
1b0ab 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74 68 65 20 rsors,. ** the
1b0ac 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
1b0ad 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 s is stored in t
1b0ae 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 6e 46 he VdbeCursor.nF
1b0af 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e 0a 20 20 ield element..
1b0b0 2a 2f 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 */. pC = p->apC
1b0b1 73 72 5b 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 sr[p1];. assert
1b0b2 28 20 70 43 21 3d 30 20 29 3b 0a 23 69 66 6e 64 ( pC!=0 );.#ifnd
1b0b3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1b0b4 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 61 73 IRTUALTABLE. as
1b0b5 73 65 72 74 28 20 70 43 2d 3e 70 56 74 61 62 43 sert( pC->pVtabC
1b0b6 75 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 6e 64 ursor==0 );.#end
1b0b7 69 66 0a 20 20 69 66 28 20 70 43 2d 3e 70 43 75 if. if( pC->pCu
1b0b8 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f rsor!=0 ){. /
1b0b9 2a 20 54 68 65 20 72 65 63 6f 72 64 20 69 73 20 * The record is
1b0ba 73 74 6f 72 65 64 20 69 6e 20 61 20 42 2d 54 72 stored in a B-Tr
1b0bb 65 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 ee */. rc = s
1b0bc 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 qlite3VdbeCursor
1b0bd 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 20 20 Moveto(pC);.
1b0be 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 if( rc ) goto ab
1b0bf 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1b0c0 3b 0a 20 20 20 20 7a 52 65 63 20 3d 20 30 3b 0a ;. zRec = 0;.
1b0c1 20 20 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e pCrsr = pC->
1b0c2 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 28 pCursor;. if(
1b0c3 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a pC->nullRow ){.
1b0c4 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a payloadSiz
1b0c5 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 e = 0;. }else
1b0c6 20 69 66 28 20 70 43 2d 3e 63 61 63 68 65 53 74 if( pC->cacheSt
1b0c7 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74 atus==p->cacheCt
1b0c8 72 20 29 7b 0a 20 20 20 20 20 20 70 61 79 6c 6f r ){. paylo
1b0c9 61 64 53 69 7a 65 20 3d 20 70 43 2d 3e 70 61 79 adSize = pC->pay
1b0ca 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 20 loadSize;.
1b0cb 7a 52 65 63 20 3d 20 28 63 68 61 72 2a 29 70 43 zRec = (char*)pC
1b0cc 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 ->aRow;. }els
1b0cd 65 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 e if( pC->isInde
1b0ce 78 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 70 x ){. i64 p
1b0cf 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 ayloadSize64;.
1b0d0 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1b0d1 4b 65 79 53 69 7a 65 28 70 43 72 73 72 2c 20 26 KeySize(pCrsr, &
1b0d2 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 29 3b 0a payloadSize64);.
1b0d3 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a payloadSiz
1b0d4 65 20 3d 20 28 69 6e 74 29 70 61 79 6c 6f 61 64 e = (int)payload
1b0d5 53 69 7a 65 36 34 3b 0a 20 20 20 20 7d 65 6c 73 Size64;. }els
1b0d6 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
1b0d7 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 70 43 BtreeDataSize(pC
1b0d8 72 73 72 2c 20 28 75 33 32 20 2a 29 26 70 61 79 rsr, (u32 *)&pay
1b0d9 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20 20 20 7d loadSize);. }
1b0da 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 43 . nField = pC
1b0db 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 7d 65 6c 73 ->nField;. }els
1b0dc 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
1b0dd 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 C->pseudoTable )
1b0de 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 ;. /* The rec
1b0df 6f 72 64 20 69 73 20 74 68 65 20 73 6f 6c 65 20 ord is the sole
1b0e0 65 6e 74 72 79 20 6f 66 20 61 20 70 73 65 75 64 entry of a pseud
1b0e1 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 70 o-table */. p
1b0e2 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 43 2d ayloadSize = pC-
1b0e3 3e 6e 44 61 74 61 3b 0a 20 20 20 20 7a 52 65 63 >nData;. zRec
1b0e4 20 3d 20 70 43 2d 3e 70 44 61 74 61 3b 0a 20 20 = pC->pData;.
1b0e5 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 pC->cacheStatu
1b0e6 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b s = CACHE_STALE;
1b0e7 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 79 . assert( pay
1b0e8 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 7c 7c 20 7a loadSize==0 || z
1b0e9 52 65 63 21 3d 30 20 29 3b 0a 20 20 20 20 6e 46 Rec!=0 );. nF
1b0ea 69 65 6c 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c ield = pC->nFiel
1b0eb 64 3b 0a 20 20 20 20 70 43 72 73 72 20 3d 20 30 d;. pCrsr = 0
1b0ec 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 70 ;. }.. /* If p
1b0ed 61 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 2c ayloadSize is 0,
1b0ee 20 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 65 then just store
1b0ef 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 a NULL */. if(
1b0f0 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 payloadSize==0
1b0f1 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
1b0f2 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f Dest->flags&MEM_
1b0f3 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 67 6f 74 6f Null );. goto
1b0f4 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a op_column_out;.
1b0f5 20 20 7d 0a 20 20 69 66 28 20 70 61 79 6c 6f 61 }. if( payloa
1b0f6 64 53 69 7a 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 dSize>db->aLimit
1b0f7 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 [SQLITE_LIMIT_LE
1b0f8 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 NGTH] ){. got
1b0f9 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a o too_big;. }..
1b0fa 20 20 61 73 73 65 72 74 28 20 70 32 3c 6e 46 69 assert( p2<nFi
1b0fb 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 eld );.. /* Rea
1b0fc 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20 d and parse the
1b0fd 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53 table header. S
1b0fe 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 tore the results
1b0ff 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20 of the parse.
1b100 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f ** into the reco
1b101 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20 rd header cache
1b102 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 fields of the cu
1b103 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 61 54 79 rsor.. */. aTy
1b104 70 65 20 3d 20 70 43 2d 3e 61 54 79 70 65 3b 0a pe = pC->aType;.
1b105 20 20 69 66 28 20 70 43 2d 3e 63 61 63 68 65 53 if( pC->cacheS
1b106 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 tatus==p->cacheC
1b107 74 72 20 29 7b 0a 20 20 20 20 61 4f 66 66 73 65 tr ){. aOffse
1b108 74 20 3d 20 70 43 2d 3e 61 4f 66 66 73 65 74 3b t = pC->aOffset;
1b109 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 38 . }else{. u8
1b10a 20 2a 7a 49 64 78 3b 20 20 20 20 20 20 20 20 2f *zIdx; /
1b10b 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 * Index into hea
1b10c 64 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 7a der */. u8 *z
1b10d 45 6e 64 48 64 72 3b 20 20 20 20 20 2f 2a 20 50 EndHdr; /* P
1b10e 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 ointer to first
1b10f 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20 68 byte after the h
1b110 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 eader */. int
1b111 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a offset; /*
1b112 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 Offset into the
1b113 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 69 6e 74 data */. int
1b114 20 73 7a 48 64 72 53 7a 3b 20 20 20 20 20 2f 2a szHdrSz; /*
1b115 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 Size of the hea
1b116 64 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20 61 der size field a
1b117 74 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f 72 t start of recor
1b118 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 76 61 d */. int ava
1b119 69 6c 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 6d il = 0; /* Num
1b11a 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
1b11b 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 2a available data *
1b11c 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 61 54 /.. assert(aT
1b11d 79 70 65 29 3b 0a 20 20 20 20 70 43 2d 3e 61 4f ype);. pC->aO
1b11e 66 66 73 65 74 20 3d 20 61 4f 66 66 73 65 74 20 ffset = aOffset
1b11f 3d 20 26 61 54 79 70 65 5b 6e 46 69 65 6c 64 5d = &aType[nField]
1b120 3b 0a 20 20 20 20 70 43 2d 3e 70 61 79 6c 6f 61 ;. pC->payloa
1b121 64 53 69 7a 65 20 3d 20 70 61 79 6c 6f 61 64 53 dSize = payloadS
1b122 69 7a 65 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 ize;. pC->cac
1b123 68 65 53 74 61 74 75 73 20 3d 20 70 2d 3e 63 61 heStatus = p->ca
1b124 63 68 65 43 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 cheCtr;.. /*
1b125 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d Figure out how m
1b126 61 6e 79 20 62 79 74 65 73 20 61 72 65 20 69 6e any bytes are in
1b127 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 the header */.
1b128 20 20 20 69 66 28 20 7a 52 65 63 20 29 7b 0a 20 if( zRec ){.
1b129 20 20 20 20 20 7a 44 61 74 61 20 3d 20 7a 52 65 zData = zRe
1b12a 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 c;. }else{.
1b12b 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73 49 6e if( pC->isIn
1b12c 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 7a dex ){. z
1b12d 44 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71 Data = (char*)sq
1b12e 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 lite3BtreeKeyFet
1b12f 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69 6c ch(pCrsr, &avail
1b130 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1b131 20 20 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 zData =
1b132 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 42 74 (char*)sqlite3Bt
1b133 72 65 65 44 61 74 61 46 65 74 63 68 28 70 43 72 reeDataFetch(pCr
1b134 73 72 2c 20 26 61 76 61 69 6c 29 3b 0a 20 20 20 sr, &avail);.
1b135 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 }. /* If
1b136 20 4b 65 79 46 65 74 63 68 28 29 2f 44 61 74 61 KeyFetch()/Data
1b137 46 65 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20 Fetch() managed
1b138 74 6f 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 to get the entir
1b139 65 20 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 e payload,.
1b13a 20 2a 2a 20 73 61 76 65 20 74 68 65 20 70 61 79 ** save the pay
1b13b 6c 6f 61 64 20 69 6e 20 74 68 65 20 70 43 2d 3e load in the pC->
1b13c 61 52 6f 77 20 63 61 63 68 65 2e 20 20 54 68 61 aRow cache. Tha
1b13d 74 20 77 69 6c 6c 20 73 61 76 65 20 75 73 20 66 t will save us f
1b13e 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 rom. ** hav
1b13f 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61 64 64 69 ing to make addi
1b140 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20 74 6f 20 tional calls to
1b141 66 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e fetch the conten
1b142 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a 20 20 20 t portion of.
1b143 20 20 20 2a 2a 20 74 68 65 20 72 65 63 6f 72 64 ** the record
1b144 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1b145 20 69 66 28 20 61 76 61 69 6c 3e 3d 70 61 79 6c if( avail>=payl
1b146 6f 61 64 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 oadSize ){.
1b147 20 20 20 7a 52 65 63 20 3d 20 7a 44 61 74 61 3b zRec = zData;
1b148 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61 52 6f . pC->aRo
1b149 77 20 3d 20 28 75 38 2a 29 7a 44 61 74 61 3b 0a w = (u8*)zData;.
1b14a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1b14b 20 20 20 20 20 70 43 2d 3e 61 52 6f 77 20 3d 20 pC->aRow =
1b14c 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
1b14d 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c . /* The foll
1b14e 6f 77 69 6e 67 20 61 73 73 65 72 74 20 69 73 20 owing assert is
1b14f 74 72 75 65 20 69 6e 20 61 6c 6c 20 63 61 73 65 true in all case
1b150 73 20 61 63 63 65 70 74 20 77 68 65 6e 0a 20 20 s accept when.
1b151 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 ** the databas
1b152 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 e file has been
1b153 63 6f 72 72 75 70 74 65 64 20 65 78 74 65 72 6e corrupted extern
1b154 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a 20 20 20 20 ally.. **
1b155 61 73 73 65 72 74 28 20 7a 52 65 63 21 3d 30 20 assert( zRec!=0
1b156 7c 7c 20 61 76 61 69 6c 3e 3d 70 61 79 6c 6f 61 || avail>=payloa
1b157 64 53 69 7a 65 20 7c 7c 20 61 76 61 69 6c 3e 3d dSize || avail>=
1b158 39 20 29 3b 20 2a 2f 0a 20 20 20 20 73 7a 48 64 9 ); */. szHd
1b159 72 53 7a 20 3d 20 67 65 74 56 61 72 69 6e 74 33 rSz = getVarint3
1b15a 32 28 28 75 38 2a 29 7a 44 61 74 61 2c 20 6f 66 2((u8*)zData, of
1b15b 66 73 65 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 fset);.. /* T
1b15c 68 65 20 4b 65 79 46 65 74 63 68 28 29 20 6f 72 he KeyFetch() or
1b15d 20 44 61 74 61 46 65 74 63 68 28 29 20 61 62 6f DataFetch() abo
1b15e 76 65 20 61 72 65 20 66 61 73 74 20 61 6e 64 20 ve are fast and
1b15f 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65 6e 74 will get the ent
1b160 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 ire. ** recor
1b161 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f 73 74 d header in most
1b162 20 63 61 73 65 73 2e 20 20 42 75 74 20 74 68 65 cases. But the
1b163 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f 20 67 y will fail to g
1b164 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a et the complete.
1b165 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 68 65 ** record he
1b166 61 64 65 72 20 69 66 20 74 68 65 20 72 65 63 6f ader if the reco
1b167 72 64 20 68 65 61 64 65 72 20 64 6f 65 73 20 6e rd header does n
1b168 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 ot fit on a sing
1b169 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 69 le page. ** i
1b16a 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 20 57 n the B-Tree. W
1b16b 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 hen that happens
1b16c 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56 64 62 , use sqlite3Vdb
1b16d 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 20 eMemFromBtree()
1b16e 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75 69 72 to. ** acquir
1b16f 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 e the complete h
1b170 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20 20 20 eader text..
1b171 2a 2f 0a 20 20 20 20 69 66 28 20 21 7a 52 65 63 */. if( !zRec
1b172 20 26 26 20 61 76 61 69 6c 3c 6f 66 66 73 65 74 && avail<offset
1b173 20 29 7b 0a 20 20 20 20 20 20 73 4d 65 6d 2e 66 ){. sMem.f
1b174 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 lags = 0;.
1b175 73 4d 65 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 20 sMem.db = 0;.
1b176 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1b177 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
1b178 70 43 72 73 72 2c 20 30 2c 20 6f 66 66 73 65 74 pCrsr, 0, offset
1b179 2c 20 70 43 2d 3e 69 73 49 6e 64 65 78 2c 20 26 , pC->isIndex, &
1b17a 73 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 sMem);. if(
1b17b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1b17c 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6f {. goto o
1b17d 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 p_column_out;.
1b17e 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 44 61 74 }. zDat
1b17f 61 20 3d 20 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 a = sMem.z;.
1b180 7d 0a 20 20 20 20 7a 45 6e 64 48 64 72 20 3d 20 }. zEndHdr =
1b181 28 75 38 20 2a 29 26 7a 44 61 74 61 5b 6f 66 66 (u8 *)&zData[off
1b182 73 65 74 5d 3b 0a 20 20 20 20 7a 49 64 78 20 3d set];. zIdx =
1b183 20 28 75 38 20 2a 29 26 7a 44 61 74 61 5b 73 7a (u8 *)&zData[sz
1b184 48 64 72 53 7a 5d 3b 0a 0a 20 20 20 20 2f 2a 20 HdrSz];.. /*
1b185 53 63 61 6e 20 74 68 65 20 68 65 61 64 65 72 20 Scan the header
1b186 61 6e 64 20 75 73 65 20 69 74 20 74 6f 20 66 69 and use it to fi
1b187 6c 6c 20 69 6e 20 74 68 65 20 61 54 79 70 65 5b ll in the aType[
1b188 5d 20 61 6e 64 20 61 4f 66 66 73 65 74 5b 5d 0a ] and aOffset[].
1b189 20 20 20 20 2a 2a 20 61 72 72 61 79 73 2e 20 20 ** arrays.
1b18a 61 54 79 70 65 5b 69 5d 20 77 69 6c 6c 20 63 6f aType[i] will co
1b18b 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69 ntain the type i
1b18c 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 69 nteger for the i
1b18d 2d 74 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d -th. ** colum
1b18e 6e 20 61 6e 64 20 61 4f 66 66 73 65 74 5b 69 5d n and aOffset[i]
1b18f 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 will contain th
1b190 65 20 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 e offset from th
1b191 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20 e beginning.
1b192 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 ** of the record
1b193 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
1b194 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 the data for th
1b195 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 0a 20 20 e i-th column.
1b196 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 */. for(i=0
1b197 3b 20 69 3c 6e 46 69 65 6c 64 3b 20 69 2b 2b 29 ; i<nField; i++)
1b198 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 49 64 78 {. if( zIdx
1b199 3c 7a 45 6e 64 48 64 72 20 29 7b 0a 20 20 20 20 <zEndHdr ){.
1b19a 20 20 20 20 61 4f 66 66 73 65 74 5b 69 5d 20 3d aOffset[i] =
1b19b 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 offset;.
1b19c 20 7a 49 64 78 20 2b 3d 20 67 65 74 56 61 72 69 zIdx += getVari
1b19d 6e 74 33 32 28 7a 49 64 78 2c 20 61 54 79 70 65 nt32(zIdx, aType
1b19e 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 [i]);. of
1b19f 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 56 fset += sqlite3V
1b1a0 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e dbeSerialTypeLen
1b1a1 28 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20 20 (aType[i]);.
1b1a2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1b1a3 20 2f 2a 20 49 66 20 69 20 69 73 20 6c 65 73 73 /* If i is less
1b1a4 20 74 68 61 74 20 6e 46 69 65 6c 64 2c 20 74 68 that nField, th
1b1a5 65 6e 20 74 68 65 72 65 20 61 72 65 20 6c 65 73 en there are les
1b1a6 73 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69 73 s fields in this
1b1a7 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 63 6f . ** reco
1b1a8 72 64 20 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f rd than SetNumCo
1b1a9 6c 75 6d 6e 73 20 69 6e 64 69 63 61 74 65 64 20 lumns indicated
1b1aa 74 68 65 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e there are column
1b1ab 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 s in the.
1b1ac 20 2a 2a 20 74 61 62 6c 65 2e 20 53 65 74 20 74 ** table. Set t
1b1ad 68 65 20 6f 66 66 73 65 74 20 66 6f 72 20 61 6e he offset for an
1b1ae 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 73 20 y extra columns
1b1af 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 0a 20 not present in.
1b1b0 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 ** the re
1b1b1 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 73 20 cord to 0. This
1b1b2 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c 6f 77 tells code below
1b1b3 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c to store a NULL
1b1b4 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 . ** inst
1b1b5 65 61 64 20 6f 66 20 64 65 73 65 72 69 61 6c 69 ead of deseriali
1b1b6 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66 72 6f zing a value fro
1b1b7 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 m the record..
1b1b8 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
1b1b9 20 61 4f 66 66 73 65 74 5b 69 5d 20 3d 20 30 3b aOffset[i] = 0;
1b1ba 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1b1bb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1b1bc 6d 52 65 6c 65 61 73 65 28 26 73 4d 65 6d 29 3b mRelease(&sMem);
1b1bd 0a 20 20 20 20 73 4d 65 6d 2e 66 6c 61 67 73 20 . sMem.flags
1b1be 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20 20 = MEM_Null;..
1b1bf 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 72 /* If we have r
1b1c0 65 61 64 20 6d 6f 72 65 20 68 65 61 64 65 72 20 ead more header
1b1c1 64 61 74 61 20 74 68 61 6e 20 77 61 73 20 63 6f data than was co
1b1c2 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 68 ntained in the h
1b1c3 65 61 64 65 72 2c 0a 20 20 20 20 2a 2a 20 6f 72 eader,. ** or
1b1c4 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 if the end of t
1b1c5 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 he last field ap
1b1c6 70 65 61 72 73 20 74 6f 20 62 65 20 70 61 73 74 pears to be past
1b1c7 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a the end of the.
1b1c8 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2c 20 6f ** record, o
1b1c9 72 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 r if the end of
1b1ca 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61 the last field a
1b1cb 70 70 65 61 72 73 20 74 6f 20 62 65 20 62 65 66 ppears to be bef
1b1cc 6f 72 65 20 74 68 65 20 65 6e 64 0a 20 20 20 20 ore the end.
1b1cd 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 ** of the record
1b1ce 20 28 77 68 65 6e 20 61 6c 6c 20 66 69 65 6c 64 (when all field
1b1cf 73 20 70 72 65 73 65 6e 74 29 2c 20 74 68 65 6e s present), then
1b1d0 20 77 65 20 6d 75 73 74 20 62 65 20 64 65 61 6c we must be deal
1b1d1 69 6e 67 20 0a 20 20 20 20 2a 2a 20 77 69 74 68 ing . ** with
1b1d2 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 a corrupt datab
1b1d3 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ase.. */.
1b1d4 69 66 28 20 7a 49 64 78 3e 7a 45 6e 64 48 64 72 if( zIdx>zEndHdr
1b1d5 20 7c 7c 20 6f 66 66 73 65 74 3e 70 61 79 6c 6f || offset>paylo
1b1d6 61 64 53 69 7a 65 20 0a 20 20 20 20 20 7c 7c 20 adSize . ||
1b1d7 28 7a 49 64 78 3d 3d 7a 45 6e 64 48 64 72 20 26 (zIdx==zEndHdr &
1b1d8 26 20 6f 66 66 73 65 74 21 3d 70 61 79 6c 6f 61 & offset!=payloa
1b1d9 64 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 20 20 dSize) ){.
1b1da 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
1b1db 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
1b1dc 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f goto op_column_o
1b1dd 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 ut;. }. }..
1b1de 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6c 75 /* Get the colu
1b1df 6d 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 mn information.
1b1e0 49 66 20 61 4f 66 66 73 65 74 5b 70 32 5d 20 69 If aOffset[p2] i
1b1e1 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
1b1e2 20 0a 20 20 2a 2a 20 64 65 73 65 72 69 61 6c 69 . ** deseriali
1b1e3 7a 65 20 74 68 65 20 76 61 6c 75 65 20 66 72 6f ze the value fro
1b1e4 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 20 49 66 m the record. If
1b1e5 20 61 4f 66 66 73 65 74 5b 70 32 5d 20 69 73 20 aOffset[p2] is
1b1e6 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 zero,. ** then
1b1e7 74 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 6e there are not en
1b1e8 6f 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 74 ough fields in t
1b1e9 68 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 74 he record to sat
1b1ea 69 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 65 isfy the. ** re
1b1eb 71 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 20 quest. In this
1b1ec 63 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 61 case, set the va
1b1ed 6c 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 lue NULL or to P
1b1ee 34 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a 20 4 if P4 is. **
1b1ef 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d a pointer to a M
1b1f0 65 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a em object.. */.
1b1f1 20 20 69 66 28 20 61 4f 66 66 73 65 74 5b 70 32 if( aOffset[p2
1b1f2 5d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ] ){. assert(
1b1f3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1b1f4 3b 0a 20 20 20 20 69 66 28 20 7a 52 65 63 20 29 ;. if( zRec )
1b1f5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1b1f6 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 dbeMemReleaseExt
1b1f7 65 72 6e 61 6c 28 70 44 65 73 74 29 3b 0a 20 20 ernal(pDest);.
1b1f8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 sqlite3VdbeS
1b1f9 65 72 69 61 6c 47 65 74 28 28 75 38 20 2a 29 26 erialGet((u8 *)&
1b1fa 7a 52 65 63 5b 61 4f 66 66 73 65 74 5b 70 32 5d zRec[aOffset[p2]
1b1fb 5d 2c 20 61 54 79 70 65 5b 70 32 5d 2c 20 70 44 ], aType[p2], pD
1b1fc 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b est);. }else{
1b1fd 0a 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c . len = sql
1b1fe 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 ite3VdbeSerialTy
1b1ff 70 65 4c 65 6e 28 61 54 79 70 65 5b 70 32 5d 29 peLen(aType[p2])
1b200 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1b201 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 4d 65 6d dbeMemMove(&sMem
1b202 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 20 20 , pDest);.
1b203 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1b204 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 72 MemFromBtree(pCr
1b205 73 72 2c 20 61 4f 66 66 73 65 74 5b 70 32 5d 2c sr, aOffset[p2],
1b206 20 6c 65 6e 2c 20 70 43 2d 3e 69 73 49 6e 64 65 len, pC->isInde
1b207 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20 20 20 x, &sMem);.
1b208 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1b209 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
1b20a 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 to op_column_out
1b20b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1b20c 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a 3b 0a zData = sMem.z;.
1b20d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1b20e 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a 29 eSerialGet((u8*)
1b20f 7a 44 61 74 61 2c 20 61 54 79 70 65 5b 70 32 5d zData, aType[p2]
1b210 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 7d 0a , pDest);. }.
1b211 20 20 20 20 70 44 65 73 74 2d 3e 65 6e 63 20 3d pDest->enc =
1b212 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 7d 65 6c encoding;. }el
1b213 73 65 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d se{. if( pOp-
1b214 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4d 45 4d 20 >p4type==P4_MEM
1b215 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1b216 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f VdbeMemShallowCo
1b217 70 79 28 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70 py(pDest, pOp->p
1b218 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74 4.pMem, MEM_Stat
1b219 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ic);. }else{.
1b21a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 44 assert( pD
1b21b 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e est->flags&MEM_N
1b21c 75 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ull );. }. }
1b21d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 64 79 6e .. /* If we dyn
1b21e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 amically allocat
1b21f 65 64 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 ed space to hold
1b220 20 74 68 65 20 64 61 74 61 20 28 69 6e 20 74 68 the data (in th
1b221 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 e. ** sqlite3Vd
1b222 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 beMemFromBtree()
1b223 20 63 61 6c 6c 20 61 62 6f 76 65 29 20 74 68 65 call above) the
1b224 6e 20 74 72 61 6e 73 66 65 72 20 63 6f 6e 74 72 n transfer contr
1b225 6f 6c 20 6f 66 20 74 68 61 74 0a 20 20 2a 2a 20 ol of that. **
1b226 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f dynamically allo
1b227 63 61 74 65 64 20 73 70 61 63 65 20 6f 76 65 72 cated space over
1b228 20 74 6f 20 74 68 65 20 70 44 65 73 74 20 73 74 to the pDest st
1b229 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 ructure.. ** Th
1b22a 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 6d 65 is prevents a me
1b22b 6d 6f 72 79 20 63 6f 70 79 2e 0a 20 20 2a 2f 0a mory copy.. */.
1b22c 20 20 69 66 28 20 73 4d 65 6d 2e 7a 4d 61 6c 6c if( sMem.zMall
1b22d 6f 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 oc ){. assert
1b22e 28 20 73 4d 65 6d 2e 7a 3d 3d 73 4d 65 6d 2e 7a ( sMem.z==sMem.z
1b22f 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 61 73 Malloc );. as
1b230 73 65 72 74 28 20 21 28 70 44 65 73 74 2d 3e 66 sert( !(pDest->f
1b231 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20 lags & MEM_Dyn)
1b232 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
1b233 28 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 (pDest->flags &
1b234 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 (MEM_Blob|MEM_St
1b235 72 29 29 20 7c 7c 20 70 44 65 73 74 2d 3e 7a 3d r)) || pDest->z=
1b236 3d 73 4d 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 70 =sMem.z );. p
1b237 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e Dest->flags &= ~
1b238 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 (MEM_Ephem|MEM_S
1b239 74 61 74 69 63 29 3b 0a 20 20 20 20 70 44 65 73 tatic);. pDes
1b23a 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f t->flags |= MEM_
1b23b 54 65 72 6d 3b 0a 20 20 20 20 70 44 65 73 74 2d Term;. pDest-
1b23c 3e 7a 20 3d 20 73 4d 65 6d 2e 7a 3b 0a 20 20 20 >z = sMem.z;.
1b23d 20 70 44 65 73 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 pDest->zMalloc
1b23e 3d 20 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a = sMem.zMalloc;.
1b23f 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 }.. rc = sqli
1b240 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 te3VdbeMemMakeWr
1b241 69 74 65 61 62 6c 65 28 70 44 65 73 74 29 3b 0a iteable(pDest);.
1b242 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a .op_column_out:.
1b243 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1b244 42 53 49 5a 45 28 70 44 65 73 74 29 3b 0a 20 20 BSIZE(pDest);.
1b245 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1b246 4f 70 2d 3e 70 33 2c 20 70 44 65 73 74 29 3b 0a Op->p3, pDest);.
1b247 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1b248 70 63 6f 64 65 3a 20 41 66 66 69 6e 69 74 79 20 pcode: Affinity
1b249 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 P2 * P4 *.**.
1b24a 2a 2a 20 41 70 70 6c 79 20 61 66 66 69 6e 69 74 ** Apply affinit
1b24b 69 65 73 20 74 6f 20 61 20 72 61 6e 67 65 20 6f ies to a range o
1b24c 66 20 50 32 20 72 65 67 69 73 74 65 72 73 20 73 f P2 registers s
1b24d 74 61 72 74 69 6e 67 20 77 69 74 68 20 50 31 2e tarting with P1.
1b24e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 73 .**.** P4 is a s
1b24f 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 50 32 tring that is P2
1b250 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 characters long
1b251 2e 20 54 68 65 20 6e 74 68 20 63 68 61 72 61 63 . The nth charac
1b252 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 ter of the.** st
1b253 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73 20 74 ring indicates t
1b254 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 he column affini
1b255 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 ty that should b
1b256 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6e e used for the n
1b257 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 6c th.** memory cel
1b258 6c 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a l in the range..
1b259 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 66 66 69 6e */.case OP_Affin
1b25a 69 74 79 3a 20 7b 0a 20 20 63 68 61 72 20 2a 7a ity: {. char *z
1b25b 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e Affinity = pOp->
1b25c 70 34 2e 7a 3b 0a 20 20 4d 65 6d 20 2a 70 44 61 p4.z;. Mem *pDa
1b25d 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 ta0 = &p->aMem[p
1b25e 4f 70 2d 3e 70 31 5d 3b 0a 20 20 4d 65 6d 20 2a Op->p1];. Mem *
1b25f 70 4c 61 73 74 20 3d 20 26 70 44 61 74 61 30 5b pLast = &pData0[
1b260 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a 20 20 4d 65 pOp->p2-1];. Me
1b261 6d 20 2a 70 52 65 63 3b 0a 0a 20 20 66 6f 72 28 m *pRec;.. for(
1b262 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52 65 pRec=pData0; pRe
1b263 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b 2b c<=pLast; pRec++
1b264 29 7b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f ){. ExpandBlo
1b265 62 28 70 52 65 63 29 3b 0a 20 20 20 20 61 70 70 b(pRec);. app
1b266 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65 63 2c lyAffinity(pRec,
1b267 20 7a 41 66 66 69 6e 69 74 79 5b 70 52 65 63 2d zAffinity[pRec-
1b268 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e pData0], encodin
1b269 67 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b g);. }. break;
1b26a 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d .}../* Opcode: M
1b26b 61 6b 65 52 65 63 6f 72 64 20 50 31 20 50 32 20 akeRecord P1 P2
1b26c 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f P3 P4 *.**.** Co
1b26d 6e 76 65 72 74 20 50 32 20 72 65 67 69 73 74 65 nvert P2 registe
1b26e 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 rs beginning wit
1b26f 68 20 50 31 20 69 6e 74 6f 20 61 20 73 69 6e 67 h P1 into a sing
1b270 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 73 75 69 74 le entry.** suit
1b271 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20 able for use as
1b272 61 20 64 61 74 61 20 72 65 63 6f 72 64 20 69 6e a data record in
1b273 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c a database tabl
1b274 65 20 6f 72 20 61 73 20 61 20 6b 65 79 0a 2a 2a e or as a key.**
1b275 20 69 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20 54 in an index. T
1b276 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 he details of th
1b277 65 20 66 6f 72 6d 61 74 20 61 72 65 20 69 72 72 e format are irr
1b278 65 6c 65 76 61 6e 74 20 61 73 20 6c 6f 6e 67 20 elevant as long
1b279 61 73 0a 2a 2a 20 74 68 65 20 4f 50 5f 43 6f 6c as.** the OP_Col
1b27a 75 6d 6e 20 6f 70 63 6f 64 65 20 63 61 6e 20 64 umn opcode can d
1b27b 65 63 6f 64 65 20 74 68 65 20 72 65 63 6f 72 64 ecode the record
1b27c 20 6c 61 74 65 72 2e 0a 2a 2a 20 52 65 66 65 72 later..** Refer
1b27d 20 74 6f 20 73 6f 75 72 63 65 20 63 6f 64 65 20 to source code
1b27e 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 74 68 65 comments for the
1b27f 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 details of the
1b280 72 65 63 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 record.** format
1b281 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 ..**.** P4 may b
1b282 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 e a string that
1b283 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72 73 is P2 characters
1b284 20 6c 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68 20 long. The nth
1b285 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 character of the
1b286 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63 .** string indic
1b287 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 ates the column
1b288 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 affinity that sh
1b289 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 ould be used for
1b28a 20 74 68 65 20 6e 74 68 0a 2a 2a 20 66 69 65 6c the nth.** fiel
1b28b 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b d of the index k
1b28c 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 ey..**.** The ma
1b28d 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61 72 61 pping from chara
1b28e 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69 74 79 cter to affinity
1b28f 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 is given by the
1b290 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a 2a 20 SQLITE_AFF_.**
1b291 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20 69 macros defined i
1b292 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a n sqliteInt.h..*
1b293 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 4e 55 *.** If P4 is NU
1b294 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 64 65 LL then all inde
1b295 78 20 66 69 65 6c 64 73 20 68 61 76 65 20 74 68 x fields have th
1b296 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e 45 2e e affinity NONE.
1b297 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 61 6b 65 .*/.case OP_Make
1b298 52 65 63 6f 72 64 3a 20 7b 0a 20 20 2f 2a 20 41 Record: {. /* A
1b299 73 73 75 6d 69 6e 67 20 74 68 65 20 72 65 63 6f ssuming the reco
1b29a 72 64 20 63 6f 6e 74 61 69 6e 73 20 4e 20 66 69 rd contains N fi
1b29b 65 6c 64 73 2c 20 74 68 65 20 72 65 63 6f 72 64 elds, the record
1b29c 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20 20 format looks.
1b29d 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 ** like this:.
1b29e 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d **. ** --------
1b29f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b2a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b2a1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b2a2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b2a3 0a 20 20 2a 2a 20 7c 20 68 64 72 2d 73 69 7a 65 . ** | hdr-size
1b2a4 20 7c 20 74 79 70 65 20 30 20 7c 20 74 79 70 65 | type 0 | type
1b2a5 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 20 1 | ... | type
1b2a6 4e 2d 31 20 7c 20 64 61 74 61 30 20 7c 20 2e 2e N-1 | data0 | ..
1b2a7 2e 20 7c 20 64 61 74 61 20 4e 2d 31 20 7c 20 0a . | data N-1 | .
1b2a8 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ** -----------
1b2a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b2aa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b2ab 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1b2ac 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 -------------.
1b2ad 2a 2a 0a 20 20 2a 2a 20 44 61 74 61 28 30 29 20 **. ** Data(0)
1b2ae 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 is taken from re
1b2af 67 69 73 74 65 72 20 50 31 2e 20 20 44 61 74 61 gister P1. Data
1b2b0 28 31 29 20 63 6f 6d 65 73 20 66 72 6f 6d 20 72 (1) comes from r
1b2b1 65 67 69 73 74 65 72 20 50 31 2b 31 0a 20 20 2a egister P1+1. *
1b2b2 2a 20 61 6e 64 20 73 6f 20 66 72 6f 74 68 2e 0a * and so froth..
1b2b3 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20 74 **. ** Each t
1b2b4 79 70 65 20 66 69 65 6c 64 20 69 73 20 61 20 76 ype field is a v
1b2b5 61 72 69 6e 74 20 72 65 70 72 65 73 65 6e 74 69 arint representi
1b2b6 6e 67 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 ng the serial ty
1b2b7 70 65 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a 20 pe of the . **
1b2b8 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64 61 corresponding da
1b2b9 74 61 20 65 6c 65 6d 65 6e 74 20 28 73 65 65 20 ta element (see
1b2ba 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1b2bb 6c 54 79 70 65 28 29 29 2e 20 54 68 65 0a 20 20 lType()). The.
1b2bc 2a 2a 20 68 64 72 2d 73 69 7a 65 20 66 69 65 6c ** hdr-size fiel
1b2bd 64 20 69 73 20 61 6c 73 6f 20 61 20 76 61 72 69 d is also a vari
1b2be 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 65 20 nt which is the
1b2bf 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 offset from the
1b2c0 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f beginning. ** o
1b2c1 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 f the record to
1b2c2 64 61 74 61 30 2e 0a 20 20 2a 2f 0a 20 20 75 38 data0.. */. u8
1b2c3 20 2a 7a 4e 65 77 52 65 63 6f 72 64 3b 20 20 20 *zNewRecord;
1b2c4 20 20 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72 /* A buffer
1b2c5 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 to hold the dat
1b2c6 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 a for the new re
1b2c7 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 cord */. Mem *p
1b2c8 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Rec;
1b2c9 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65 63 6f /* The new reco
1b2ca 72 64 20 2a 2f 0a 20 20 75 36 34 20 6e 44 61 74 rd */. u64 nDat
1b2cb 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f a = 0; /
1b2cc 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1b2cd 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 s of data space
1b2ce 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 72 20 3d 20 */. int nHdr =
1b2cf 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 0; /* N
1b2d0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1b2d1 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 2a f header space *
1b2d2 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 20 3d 20 /. i64 nByte =
1b2d3 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 0; /* Da
1b2d4 74 61 20 73 70 61 63 65 20 72 65 71 75 69 72 65 ta space require
1b2d5 64 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f 72 d for this recor
1b2d6 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f d */. int nZero
1b2d7 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
1b2d8 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 Number of zero
1b2d9 62 79 74 65 73 20 61 74 20 74 68 65 20 65 6e 64 bytes at the end
1b2da 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a of the record *
1b2db 2f 0a 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 3b /. int nVarint;
1b2dc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1b2dd 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
1b2de 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 75 a varint */. u
1b2df 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 20 32 serial_type;
1b2e0 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 66 69 /* Type fi
1b2e1 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 eld */. Mem *pD
1b2e2 61 74 61 30 3b 20 20 20 20 20 20 20 20 20 20 20 ata0;
1b2e3 2f 2a 20 46 69 72 73 74 20 66 69 65 6c 64 20 74 /* First field t
1b2e4 6f 20 62 65 20 63 6f 6d 62 69 6e 65 64 20 69 6e o be combined in
1b2e5 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f to the record */
1b2e6 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 . Mem *pLast;
1b2e7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 /* Las
1b2e8 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 72 t field of the r
1b2e9 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e ecord */. int n
1b2ea 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 Field;
1b2eb 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 /* Number of f
1b2ec 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 ields in the rec
1b2ed 6f 72 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ord */. char *z
1b2ee 41 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 Affinity;
1b2ef 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 /* The affinity
1b2f0 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 72 string for the r
1b2f1 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 66 ecord */. int f
1b2f2 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 ile_format;
1b2f3 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72 6d 61 74 /* File format
1b2f4 20 74 6f 20 75 73 65 20 66 6f 72 20 65 6e 63 6f to use for enco
1b2f5 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 3b ding */. int i;
1b2f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b2f7 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64 20 69 /* Space used i
1b2f8 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a n zNewRecord[] *
1b2f9 2f 0a 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4f /.. nField = pO
1b2fa 70 2d 3e 70 31 3b 0a 20 20 7a 41 66 66 69 6e 69 p->p1;. zAffini
1b2fb 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a ty = pOp->p4.z;.
1b2fc 20 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c 64 assert( nField
1b2fd 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 20 >0 && pOp->p2>0
1b2fe 26 26 20 70 4f 70 2d 3e 70 32 2b 6e 46 69 65 6c && pOp->p2+nFiel
1b2ff 64 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 d<=p->nMem );.
1b300 70 44 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65 pData0 = &p->aMe
1b301 6d 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 6e 46 69 m[nField];. nFi
1b302 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 eld = pOp->p2;.
1b303 20 70 4c 61 73 74 20 3d 20 26 70 44 61 74 61 30 pLast = &pData0
1b304 5b 6e 46 69 65 6c 64 2d 31 5d 3b 0a 20 20 66 69 [nField-1];. fi
1b305 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70 2d 3e 6d le_format = p->m
1b306 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 inWriteFileForma
1b307 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 t;.. /* Loop th
1b308 72 6f 75 67 68 20 74 68 65 20 65 6c 65 6d 65 6e rough the elemen
1b309 74 73 20 74 68 61 74 20 77 69 6c 6c 20 6d 61 6b ts that will mak
1b30a 65 20 75 70 20 74 68 65 20 72 65 63 6f 72 64 20 e up the record
1b30b 74 6f 20 66 69 67 75 72 65 0a 20 20 2a 2a 20 6f to figure. ** o
1b30c 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 ut how much spac
1b30d 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f e is required fo
1b30e 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 r the new record
1b30f 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 52 65 .. */. for(pRe
1b310 63 3d 70 44 61 74 61 30 3b 20 70 52 65 63 3c 3d c=pData0; pRec<=
1b311 70 4c 61 73 74 3b 20 70 52 65 63 2b 2b 29 7b 0a pLast; pRec++){.
1b312 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 int len;.
1b313 20 69 66 28 20 7a 41 66 66 69 6e 69 74 79 20 29 if( zAffinity )
1b314 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66 66 {. applyAff
1b315 69 6e 69 74 79 28 70 52 65 63 2c 20 7a 41 66 66 inity(pRec, zAff
1b316 69 6e 69 74 79 5b 70 52 65 63 2d 70 44 61 74 61 inity[pRec-pData
1b317 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 0], encoding);.
1b318 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 52 65 }. if( pRe
1b319 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 c->flags&MEM_Zer
1b31a 6f 20 26 26 20 70 52 65 63 2d 3e 6e 3e 30 20 29 o && pRec->n>0 )
1b31b 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1b31c 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 dbeMemExpandBlob
1b31d 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 (pRec);. }.
1b31e 20 20 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 serial_type =
1b31f 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1b320 6c 54 79 70 65 28 70 52 65 63 2c 20 66 69 6c 65 lType(pRec, file
1b321 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20 6c 65 _format);. le
1b322 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 n = sqlite3VdbeS
1b323 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 erialTypeLen(ser
1b324 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 6e ial_type);. n
1b325 44 61 74 61 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 Data += len;.
1b326 20 6e 48 64 72 20 2b 3d 20 73 71 6c 69 74 65 33 nHdr += sqlite3
1b327 56 61 72 69 6e 74 4c 65 6e 28 73 65 72 69 61 6c VarintLen(serial
1b328 5f 74 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 _type);. if(
1b329 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pRec->flags & ME
1b32a 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 M_Zero ){.
1b32b 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 72 /* Only pure zer
1b32c 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 63 o-filled BLOBs c
1b32d 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20 74 an be input to t
1b32e 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 20 his Opcode..
1b32f 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 61 ** We do not a
1b330 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 20 llow blobs with
1b331 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20 7a a prefix and a z
1b332 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e ero-filled tail.
1b333 20 2a 2f 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20 */. nZero
1b334 2b 3d 20 70 52 65 63 2d 3e 75 2e 6e 5a 65 72 6f += pRec->u.nZero
1b335 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1b336 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65 len ){. nZe
1b337 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 ro = 0;. }.
1b338 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 }.. /* Add the
1b339 69 6e 69 74 69 61 6c 20 68 65 61 64 65 72 20 76 initial header v
1b33a 61 72 69 6e 74 20 61 6e 64 20 74 6f 74 61 6c 20 arint and total
1b33b 74 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 6e 48 the size */. nH
1b33c 64 72 20 2b 3d 20 6e 56 61 72 69 6e 74 20 3d 20 dr += nVarint =
1b33d 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e sqlite3VarintLen
1b33e 28 6e 48 64 72 29 3b 0a 20 20 69 66 28 20 6e 56 (nHdr);. if( nV
1b33f 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 56 61 72 arint<sqlite3Var
1b340 69 6e 74 4c 65 6e 28 6e 48 64 72 29 20 29 7b 0a intLen(nHdr) ){.
1b341 20 20 20 20 6e 48 64 72 2b 2b 3b 0a 20 20 7d 0a nHdr++;. }.
1b342 20 20 6e 42 79 74 65 20 3d 20 6e 48 64 72 2b 6e nByte = nHdr+n
1b343 44 61 74 61 2d 6e 5a 65 72 6f 3b 0a 20 20 69 66 Data-nZero;. if
1b344 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d ( nByte>db->aLim
1b345 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
1b346 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 LENGTH] ){. g
1b347 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d oto too_big;. }
1b348 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
1b349 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 the output regi
1b34a 73 74 65 72 20 68 61 73 20 61 20 62 75 66 66 65 ster has a buffe
1b34b 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 r large enough t
1b34c 6f 20 73 74 6f 72 65 20 0a 20 20 2a 2a 20 74 68 o store . ** th
1b34d 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20 54 68 e new record. Th
1b34e 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 e output registe
1b34f 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73 20 6e r (pOp->p3) is n
1b350 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 20 20 ot allowed to.
1b351 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 ** be one of the
1b352 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 73 input registers
1b353 20 28 62 65 63 61 75 73 65 20 74 68 65 20 66 6f (because the fo
1b354 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 6f 0a llowing call to.
1b355 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 ** sqlite3Vdbe
1b356 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 6c 64 20 MemGrow() could
1b357 63 6c 6f 62 62 65 72 20 74 68 65 20 76 61 6c 75 clobber the valu
1b358 65 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 75 e before it is u
1b359 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 sed).. */. ass
1b35a 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 ert( pOp->p3<pOp
1b35b 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e ->p1 || pOp->p3>
1b35c 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 =pOp->p1+pOp->p2
1b35d 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d );. pOut = &p-
1b35e 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1b35f 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1b360 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 28 eMemGrow(pOut, (
1b361 69 6e 74 29 6e 42 79 74 65 2c 20 30 29 20 29 7b int)nByte, 0) ){
1b362 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d . goto no_mem
1b363 3b 0a 20 20 7d 0a 20 20 7a 4e 65 77 52 65 63 6f ;. }. zNewReco
1b364 72 64 20 3d 20 28 75 38 20 2a 29 70 4f 75 74 2d rd = (u8 *)pOut-
1b365 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 >z;.. /* Write
1b366 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1b367 69 20 3d 20 70 75 74 56 61 72 69 6e 74 33 32 28 i = putVarint32(
1b368 7a 4e 65 77 52 65 63 6f 72 64 2c 20 6e 48 64 72 zNewRecord, nHdr
1b369 29 3b 0a 20 20 66 6f 72 28 70 52 65 63 3d 70 44 );. for(pRec=pD
1b36a 61 74 61 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 ata0; pRec<=pLas
1b36b 74 3b 20 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 t; pRec++){.
1b36c 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 serial_type = sq
1b36d 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
1b36e 79 70 65 28 70 52 65 63 2c 20 66 69 6c 65 5f 66 ype(pRec, file_f
1b36f 6f 72 6d 61 74 29 3b 0a 20 20 20 20 69 20 2b 3d ormat);. i +=
1b370 20 70 75 74 56 61 72 69 6e 74 33 32 28 26 7a 4e putVarint32(&zN
1b371 65 77 52 65 63 6f 72 64 5b 69 5d 2c 20 73 65 72 ewRecord[i], ser
1b372 69 61 6c 5f 74 79 70 65 29 3b 20 20 20 20 20 20 ial_type);
1b373 2f 2a 20 73 65 72 69 61 6c 20 74 79 70 65 20 2a /* serial type *
1b374 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 70 52 65 63 /. }. for(pRec
1b375 3d 70 44 61 74 61 30 3b 20 70 52 65 63 3c 3d 70 =pData0; pRec<=p
1b376 4c 61 73 74 3b 20 70 52 65 63 2b 2b 29 7b 20 20 Last; pRec++){
1b377 2f 2a 20 73 65 72 69 61 6c 20 64 61 74 61 20 2a /* serial data *
1b378 2f 0a 20 20 20 20 69 20 2b 3d 20 73 71 6c 69 74 /. i += sqlit
1b379 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 e3VdbeSerialPut(
1b37a 26 7a 4e 65 77 52 65 63 6f 72 64 5b 69 5d 2c 20 &zNewRecord[i],
1b37b 28 69 6e 74 29 28 6e 42 79 74 65 2d 69 29 2c 20 (int)(nByte-i),
1b37c 70 52 65 63 2c 66 69 6c 65 5f 66 6f 72 6d 61 74 pRec,file_format
1b37d 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 );. }. assert(
1b37e 20 69 3d 3d 6e 42 79 74 65 20 29 3b 0a 0a 20 20 i==nByte );..
1b37f 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e assert( pOp->p3>
1b380 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 0 && pOp->p3<=p-
1b381 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 2d >nMem );. pOut-
1b382 3e 6e 20 3d 20 28 69 6e 74 29 6e 42 79 74 65 3b >n = (int)nByte;
1b383 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d . pOut->flags =
1b384 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f MEM_Blob | MEM_
1b385 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78 44 65 Dyn;. pOut->xDe
1b386 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 5a 65 l = 0;. if( nZe
1b387 72 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e ro ){. pOut->
1b388 75 2e 6e 5a 65 72 6f 20 3d 20 6e 5a 65 72 6f 3b u.nZero = nZero;
1b389 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 . pOut->flags
1b38a 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 |= MEM_Zero;.
1b38b 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 }. pOut->enc =
1b38c 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a SQLITE_UTF8; /*
1b38d 20 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f In case the blo
1b38e 62 20 69 73 20 65 76 65 72 20 63 6f 6e 76 65 72 b is ever conver
1b38f 74 65 64 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 ted to text */.
1b390 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1b391 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a pOp->p3, pOut);.
1b392 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1b393 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 BSIZE(pOut);. b
1b394 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1b395 64 65 3a 20 53 74 61 74 65 6d 65 6e 74 20 50 31 de: Statement P1
1b396 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 * * * *.**.** B
1b397 65 67 69 6e 20 61 6e 20 69 6e 64 69 76 69 64 75 egin an individu
1b398 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 al statement tra
1b399 6e 73 61 63 74 69 6f 6e 20 77 68 69 63 68 20 69 nsaction which i
1b39a 73 20 70 61 72 74 20 6f 66 20 61 20 6c 61 72 67 s part of a larg
1b39b 65 72 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f er.** transactio
1b39c 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 64 n. This is need
1b39d 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 73 ed so that the s
1b39e 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 tatement.** can
1b39f 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 be rolled back a
1b3a0 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 77 69 fter an error wi
1b3a1 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 thout having to
1b3a2 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a 2a roll back the.**
1b3a3 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 entire transact
1b3a4 69 6f 6e 2e 20 20 54 68 65 20 73 74 61 74 65 6d ion. The statem
1b3a5 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
1b3a6 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c will automatical
1b3a7 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 77 68 65 ly.** commit whe
1b3a8 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c 74 73 n the VDBE halts
1b3a9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 ..**.** If the d
1b3aa 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1b3ab 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 on is currently
1b3ac 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f in autocommit mo
1b3ad 64 65 20 28 74 68 61 74 20 0a 2a 2a 20 69 73 20 de (that .** is
1b3ae 74 6f 20 73 61 79 2c 20 69 66 20 69 74 20 69 73 to say, if it is
1b3af 20 69 6e 20 62 65 74 77 65 65 6e 20 42 45 47 49 in between BEGI
1b3b0 4e 20 61 6e 64 20 43 4f 4d 4d 49 54 29 0a 2a 2a N and COMMIT).**
1b3b1 20 61 6e 64 20 69 66 20 74 68 65 72 65 20 61 72 and if there ar
1b3b2 65 20 6e 6f 20 6f 74 68 65 72 20 61 63 74 69 76 e no other activ
1b3b3 65 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 20 e statements on
1b3b4 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
1b3b5 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c e.** connection,
1b3b6 20 74 68 65 6e 20 74 68 69 73 20 6f 70 65 72 61 then this opera
1b3b7 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
1b3b8 20 20 4e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 No statement t
1b3b9 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 ransaction.** is
1b3ba 20 6e 65 65 64 65 64 20 73 69 6e 63 65 20 61 6e needed since an
1b3bb 79 20 65 72 72 6f 72 20 63 61 6e 20 75 73 65 20 y error can use
1b3bc 74 68 65 20 6e 6f 72 6d 61 6c 20 52 4f 4c 4c 42 the normal ROLLB
1b3bd 41 43 4b 20 70 72 6f 63 65 73 73 20 74 6f 0a 2a ACK process to.*
1b3be 2a 20 75 6e 64 6f 20 63 68 61 6e 67 65 73 2e 0a * undo changes..
1b3bf 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 74 61 74 65 **.** If a state
1b3c0 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1b3c1 20 69 73 20 73 74 61 72 74 65 64 2c 20 74 68 65 is started, the
1b3c2 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f n a statement jo
1b3c3 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 77 69 urnal file.** wi
1b3c4 6c 6c 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 ll be allocated
1b3c5 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e and initialized.
1b3c6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 .**.** The state
1b3c7 6d 65 6e 74 20 69 73 20 62 65 67 75 6e 20 6f 6e ment is begun on
1b3c8 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1b3c9 6c 65 20 77 69 74 68 20 69 6e 64 65 78 20 50 31 le with index P1
1b3ca 2e 20 20 54 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 . The main.** d
1b3cb 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 atabase file has
1b3cc 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 61 an index of 0 a
1b3cd 6e 64 20 74 68 65 20 66 69 6c 65 20 75 73 65 64 nd the file used
1b3ce 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74 for temporary t
1b3cf 61 62 6c 65 73 0a 2a 2a 20 68 61 73 20 61 6e 20 ables.** has an
1b3d0 69 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2f 0a 63 index of 1..*/.c
1b3d1 61 73 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74 ase OP_Statement
1b3d2 3a 20 7b 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 : {. if( db->au
1b3d3 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 toCommit==0 || d
1b3d4 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 b->activeVdbeCnt
1b3d5 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 >1 ){. int i
1b3d6 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 42 = pOp->p1;. B
1b3d7 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 61 tree *pBt;. a
1b3d8 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
1b3d9 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 <db->nDb );.
1b3da 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b assert( db->aDb[
1b3db 69 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 i].pBt!=0 );.
1b3dc 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 pBt = db->aDb[i
1b3dd 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 ].pBt;. asser
1b3de 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 t( sqlite3BtreeI
1b3df 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29 3b sInTrans(pBt) );
1b3e0 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d . assert( (p-
1b3e1 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1b3e2 3c 69 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 69 <i))!=0 );. i
1b3e3 66 28 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 f( !sqlite3Btree
1b3e4 49 73 49 6e 53 74 6d 74 28 70 42 74 29 20 29 7b IsInStmt(pBt) ){
1b3e5 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1b3e6 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d te3BtreeBeginStm
1b3e7 74 28 70 42 74 29 3b 0a 20 20 20 20 20 20 70 2d t(pBt);. p-
1b3e8 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e 74 >openedStatement
1b3e9 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 1;. }. }.
1b3ea 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1b3eb 70 63 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 pcode: Savepoint
1b3ec 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 * * P4 *.**.
1b3ed 2a 2a 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 ** Open, release
1b3ee 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 or rollback the
1b3ef 20 73 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 savepoint named
1b3f0 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 50 34 by parameter P4
1b3f1 2c 20 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f , depending.** o
1b3f2 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 n the value of P
1b3f3 31 2e 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 1. To open a new
1b3f4 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d savepoint, P1==
1b3f5 30 2e 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63 0. To release (c
1b3f6 6f 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 ommit) an.** exi
1b3f7 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c sting savepoint,
1b3f8 20 50 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f P1==1, or to ro
1b3f9 6c 6c 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69 llback an existi
1b3fa 6e 67 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d ng savepoint P1=
1b3fb 3d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 =2..*/.case OP_S
1b3fc 61 76 65 70 6f 69 6e 74 3a 20 7b 0a 20 20 69 6e avepoint: {. in
1b3fd 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a t p1 = pOp->p1;.
1b3fe 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 char *zName =
1b3ff 70 4f 70 2d 3e 70 34 2e 7a 3b 20 20 20 20 20 20 pOp->p4.z;
1b400 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 /* Name of sa
1b401 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 2f 2a vepoint */.. /*
1b402 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 Assert that the
1b403 20 70 31 20 70 61 72 61 6d 65 74 65 72 20 69 73 p1 parameter is
1b404 20 76 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 61 valid. Also tha
1b405 74 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f t if there is no
1b406 20 6f 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 open. ** trans
1b407 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 action, then the
1b408 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 79 re cannot be any
1b409 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 0a 20 20 savepoints. .
1b40a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d */. assert( db-
1b40b 3e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c >pSavepoint==0 |
1b40c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 | db->autoCommit
1b40d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
1b40e 20 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 p1==SAVEPOINT_B
1b40f 45 47 49 4e 7c 7c 70 31 3d 3d 53 41 56 45 50 4f EGIN||p1==SAVEPO
1b410 49 4e 54 5f 52 45 4c 45 41 53 45 7c 7c 70 31 3d INT_RELEASE||p1=
1b411 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
1b412 41 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ACK );. assert(
1b413 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 db->pSavepoint
1b414 7c 7c 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 || db->isTransac
1b415 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 tionSavepoint==0
1b416 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 68 );. assert( ch
1b417 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e eckSavepointCoun
1b418 74 28 64 62 29 20 29 3b 0a 0a 20 20 69 66 28 20 t(db) );.. if(
1b419 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 p1==SAVEPOINT_BE
1b41a 47 49 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 64 GIN ){. if( d
1b41b 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e b->writeVdbeCnt>
1b41c 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 0 ){. /* A
1b41d 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20 63 61 new savepoint ca
1b41e 6e 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 20 nnot be created
1b41f 69 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 if there are act
1b420 69 76 65 20 77 72 69 74 65 20 0a 20 20 20 20 20 ive write .
1b421 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28 ** statements (
1b422 69 2e 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 i.e. open read/w
1b423 72 69 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c rite incremental
1b424 20 62 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a blob handles)..
1b425 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
1b426 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1b427 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1b428 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 "cannot open sa
1b429 76 65 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 vepoint - ".
1b42a 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 "SQL stateme
1b42b 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 nts in progress"
1b42c 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 );. rc = SQ
1b42d 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d LITE_BUSY;. }
1b42e 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 else{. int
1b42f 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 nName = sqlite3S
1b430 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a trlen30(zName);.
1b431 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 Savepoint
1b432 2a 70 4e 65 77 3b 0a 0a 20 20 20 20 20 20 2f 2a *pNew;.. /*
1b433 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 61 Create a new sa
1b434 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 vepoint structur
1b435 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 e. */. pNew
1b436 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1b437 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 ocRaw(db, sizeof
1b438 28 53 61 76 65 70 6f 69 6e 74 29 2b 6e 4e 61 6d (Savepoint)+nNam
1b439 65 2b 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 e+1);. if(
1b43a 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 pNew ){.
1b43b 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 pNew->zName = (c
1b43c 68 61 72 20 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a har *)&pNew[1];.
1b43d 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 memcpy(p
1b43e 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d New->zName, zNam
1b43f 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 e, nName+1);.
1b440 20 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 . /* If
1b441 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e there is no open
1b442 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 transaction, th
1b443 65 6e 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20 en mark this as
1b444 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 a special.
1b445 20 20 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f ** "transactio
1b446 6e 20 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f n savepoint". */
1b447 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d . if( db-
1b448 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 >autoCommit ){.
1b449 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 db->aut
1b44a 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 oCommit = 0;.
1b44b 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61 db->isTra
1b44c 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e nsactionSavepoin
1b44d 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d t = 1;. }
1b44e 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
1b44f 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b db->nSavepoint++
1b450 3b 0a 09 7d 0a 20 20 20 20 0a 20 20 20 20 20 20 ;..}. .
1b451 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65 /* Link the ne
1b452 77 20 73 61 76 65 70 6f 69 6e 74 20 69 6e 74 6f w savepoint into
1b453 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
1b454 6e 64 6c 65 27 73 20 6c 69 73 74 2e 20 2a 2f 0a ndle's list. */.
1b455 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e pNew->pN
1b456 65 78 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 ext = db->pSavep
1b457 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 oint;. db
1b458 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 ->pSavepoint = p
1b459 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 New;. }.
1b45a 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
1b45b 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 Savepoint *pSave
1b45c 70 6f 69 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 point;. int i
1b45d 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a Savepoint = 0;..
1b45e 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 /* Find the
1b45f 6e 61 6d 65 64 20 73 61 76 65 70 6f 69 6e 74 2e named savepoint.
1b460 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
1b461 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 2c 20 such savepoint,
1b462 74 68 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 61 then an. ** a
1b463 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
1b464 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e ned to the user.
1b465 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 0a 20 20 */. for(.
1b466 20 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 64 pSavepoint=d
1b467 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 0a b->pSavepoint; .
1b468 20 20 20 20 20 20 70 53 61 76 65 70 6f 69 6e 74 pSavepoint
1b469 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1b46a 6d 70 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 7a mp(pSavepoint->z
1b46b 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 Name, zName);.
1b46c 20 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 70 pSavepoint=p
1b46d 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 Savepoint->pNext
1b46e 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 53 . ){. iS
1b46f 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 avepoint++;.
1b470 7d 0a 20 20 20 20 69 66 28 20 21 70 53 61 76 65 }. if( !pSave
1b471 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 point ){. s
1b472 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1b473 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1b474 20 22 6e 6f 20 73 75 63 68 20 73 61 76 65 70 6f "no such savepo
1b475 69 6e 74 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 int: %s", zName)
1b476 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
1b477 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d ITE_ERROR;. }
1b478 65 6c 73 65 20 69 66 28 20 0a 20 20 20 20 20 20 else if( .
1b479 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 db->writeVdbeC
1b47a 6e 74 3e 30 20 7c 7c 20 28 70 31 3d 3d 53 41 56 nt>0 || (p1==SAV
1b47b 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1b47c 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 && db->activeVdb
1b47d 65 43 6e 74 3e 31 29 20 0a 20 20 20 20 29 7b 0a eCnt>1) . ){.
1b47e 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e /* It is n
1b47f 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 ot possible to r
1b480 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 elease (commit)
1b481 61 20 73 61 76 65 70 6f 69 6e 74 20 69 66 20 74 a savepoint if t
1b482 68 65 72 65 20 61 72 65 20 0a 20 20 20 20 20 20 here are .
1b483 2a 2a 20 61 63 74 69 76 65 20 77 72 69 74 65 20 ** active write
1b484 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 statements. It i
1b485 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 s not possible t
1b486 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 76 o rollback a sav
1b487 65 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 epoint. **
1b488 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 if there are any
1b489 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e active statemen
1b48a 74 73 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 20 ts at all..
1b48b 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 */. sqlite
1b48c 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1b48d 45 72 72 4d 73 67 2c 20 64 62 2c 20 0a 20 20 20 ErrMsg, db, .
1b48e 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 "cannot %s
1b48f 73 61 76 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 savepoint - SQL
1b490 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 statements in pr
1b491 6f 67 72 65 73 73 22 2c 0a 20 20 20 20 20 20 20 ogress",.
1b492 20 28 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f (p1==SAVEPOINT_
1b493 52 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c ROLLBACK ? "roll
1b494 62 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 back": "release"
1b495 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 ). );.
1b496 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
1b497 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 Y;. }else{..
1b498 20 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e /* Determin
1b499 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 e whether or not
1b49a 20 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 this is a trans
1b49b 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 action savepoint
1b49c 2e 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a . If so,. *
1b49d 2a 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 * and this is a
1b49e 52 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c RELEASE command,
1b49f 20 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e then the curren
1b4a0 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 t transaction .
1b4a1 20 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 ** is commi
1b4a2 74 74 65 64 2e 20 0a 20 20 20 20 20 20 2a 2f 0a tted. . */.
1b4a3 20 20 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e int isTran
1b4a4 73 61 63 74 69 6f 6e 20 3d 20 70 53 61 76 65 70 saction = pSavep
1b4a5 6f 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 oint->pNext==0 &
1b4a6 26 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 & db->isTransact
1b4a7 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 ionSavepoint;.
1b4a8 20 20 20 20 69 66 28 20 69 73 54 72 61 6e 73 61 if( isTransa
1b4a9 63 74 69 6f 6e 20 26 26 20 70 31 3d 3d 53 41 56 ction && p1==SAV
1b4aa 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 EPOINT_RELEASE )
1b4ab 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 {. db->au
1b4ac 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 toCommit = 1;.
1b4ad 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1b4ae 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 3VdbeHalt(p)==SQ
1b4af 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 LITE_BUSY ){.
1b4b0 20 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 p->pc = p
1b4b1 63 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d c;. db-
1b4b2 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b >autoCommit = 0;
1b4b3 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 . p->rc
1b4b4 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 = rc = SQLITE_B
1b4b5 55 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20 67 USY;. g
1b4b6 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b oto vdbe_return;
1b4b7 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1b4b8 20 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 db->isTransac
1b4b9 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 tionSavepoint =
1b4ba 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 0;. rc =
1b4bb 70 2d 3e 72 63 3b 0a 20 20 20 20 20 20 7d 65 6c p->rc;. }el
1b4bc 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 se{. int
1b4bd 69 69 3b 0a 20 20 20 20 20 20 20 20 69 53 61 76 ii;. iSav
1b4be 65 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 epoint = db->nSa
1b4bf 76 65 70 6f 69 6e 74 20 2d 20 69 53 61 76 65 70 vepoint - iSavep
1b4c0 6f 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20 20 oint - 1;.
1b4c1 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 64 for(ii=0; ii<d
1b4c2 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 b->nDb; ii++){.
1b4c3 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
1b4c4 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f lite3BtreeSavepo
1b4c5 69 6e 74 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e int(db->aDb[ii].
1b4c6 70 42 74 2c 20 70 31 2c 20 69 53 61 76 65 70 6f pBt, p1, iSavepo
1b4c7 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 int);.
1b4c8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1b4c9 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
1b4ca 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1b4cb 74 6f 5f 65 72 72 6f 72 3b 0a 09 20 20 7d 0a 20 to_error;.. }.
1b4cc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1b4cd 20 69 66 28 20 70 31 3d 3d 53 41 56 45 50 4f 49 if( p1==SAVEPOI
1b4ce 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 28 NT_ROLLBACK && (
1b4cf 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 db->flags&SQLITE
1b4d0 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 21 _InternChanges)!
1b4d1 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1b4d2 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 sqlite3ExpirePre
1b4d3 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 paredStatements(
1b4d4 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 db);. s
1b4d5 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 qlite3ResetInter
1b4d6 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 nalSchema(db, 0)
1b4d7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1b4d8 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 }. . /*
1b4d9 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 Regardless of wh
1b4da 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 20 ether this is a
1b4db 52 45 4c 45 41 53 45 20 6f 72 20 52 4f 4c 4c 42 RELEASE or ROLLB
1b4dc 41 43 4b 2c 20 64 65 73 74 72 6f 79 20 61 6c 6c ACK, destroy all
1b4dd 20 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 70 . ** savep
1b4de 6f 69 6e 74 73 20 6e 65 73 74 65 64 20 69 6e 73 oints nested ins
1b4df 69 64 65 20 6f 66 20 74 68 65 20 73 61 76 65 70 ide of the savep
1b4e0 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 oint being opera
1b4e1 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 ted on. */.
1b4e2 20 77 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76 while( db->pSav
1b4e3 65 70 6f 69 6e 74 21 3d 70 53 61 76 65 70 6f 69 epoint!=pSavepoi
1b4e4 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 53 61 nt ){. Sa
1b4e5 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 20 3d 20 vepoint *pTmp =
1b4e6 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a db->pSavepoint;.
1b4e7 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 db->pSav
1b4e8 65 70 6f 69 6e 74 20 3d 20 70 54 6d 70 2d 3e 70 epoint = pTmp->p
1b4e9 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71 Next;. sq
1b4ea 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1b4eb 70 54 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 64 pTmp);. d
1b4ec 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d 3b b->nSavepoint--;
1b4ed 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
1b4ee 2f 2a 20 49 66 20 69 74 20 69 73 20 61 20 52 45 /* If it is a RE
1b4ef 4c 45 41 53 45 2c 20 74 68 65 6e 20 64 65 73 74 LEASE, then dest
1b4f0 72 6f 79 20 74 68 65 20 73 61 76 65 70 6f 69 6e roy the savepoin
1b4f1 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 t being operated
1b4f2 20 6f 6e 20 74 6f 6f 20 2a 2f 0a 20 20 20 20 20 on too */.
1b4f3 20 69 66 28 20 70 31 3d 3d 53 41 56 45 50 4f 49 if( p1==SAVEPOI
1b4f4 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 NT_RELEASE ){.
1b4f5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 assert( pS
1b4f6 61 76 65 70 6f 69 6e 74 3d 3d 64 62 2d 3e 70 53 avepoint==db->pS
1b4f7 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 20 20 avepoint );.
1b4f8 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 db->pSavepoi
1b4f9 6e 74 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d nt = pSavepoint-
1b4fa 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 >pNext;.
1b4fb 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1b4fc 2c 20 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 , pSavepoint);.
1b4fd 20 20 20 20 20 20 20 69 66 28 20 21 69 73 54 72 if( !isTr
1b4fe 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 ansaction ){.
1b4ff 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 db->nSave
1b500 70 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 20 point--;.
1b501 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1b502 0a 20 20 7d 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d . }.. break;.}
1b503 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 75 74 ../* Opcode: Aut
1b504 6f 43 6f 6d 6d 69 74 20 50 31 20 50 32 20 2a 20 oCommit P1 P2 *
1b505 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 * *.**.** Set th
1b506 65 20 64 61 74 61 62 61 73 65 20 61 75 74 6f 2d e database auto-
1b507 63 6f 6d 6d 69 74 20 66 6c 61 67 20 74 6f 20 50 commit flag to P
1b508 31 20 28 31 20 6f 72 20 30 29 2e 20 49 66 20 50 1 (1 or 0). If P
1b509 32 20 69 73 20 74 72 75 65 2c 20 72 6f 6c 6c 0a 2 is true, roll.
1b50a 2a 2a 20 62 61 63 6b 20 61 6e 79 20 63 75 72 72 ** back any curr
1b50b 65 6e 74 6c 79 20 61 63 74 69 76 65 20 62 74 72 ently active btr
1b50c 65 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e ee transactions.
1b50d 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e If there are an
1b50e 79 20 61 63 74 69 76 65 0a 2a 2a 20 56 4d 73 20 y active.** VMs
1b50f 28 61 70 61 72 74 20 66 72 6f 6d 20 74 68 69 73 (apart from this
1b510 20 6f 6e 65 29 2c 20 74 68 65 6e 20 74 68 65 20 one), then the
1b511 43 4f 4d 4d 49 54 20 6f 72 20 52 4f 4c 4c 42 41 COMMIT or ROLLBA
1b512 43 4b 20 73 74 61 74 65 6d 65 6e 74 20 66 61 69 CK statement fai
1b513 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ls..**.** This i
1b514 6e 73 74 72 75 63 74 69 6f 6e 20 63 61 75 73 65 nstruction cause
1b515 73 20 74 68 65 20 56 4d 20 74 6f 20 68 61 6c 74 s the VM to halt
1b516 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 75 74 ..*/.case OP_Aut
1b517 6f 43 6f 6d 6d 69 74 3a 20 7b 0a 20 20 69 6e 74 oCommit: {. int
1b518 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d desiredAutoComm
1b519 69 74 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 it = pOp->p1;.
1b51a 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 3d 20 70 int rollback = p
1b51b 4f 70 2d 3e 70 32 3b 0a 20 20 69 6e 74 20 74 75 Op->p2;. int tu
1b51c 72 6e 4f 6e 41 43 20 3d 20 64 65 73 69 72 65 64 rnOnAC = desired
1b51d 41 75 74 6f 43 6f 6d 6d 69 74 20 26 26 20 21 64 AutoCommit && !d
1b51e 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a 0a b->autoCommit;..
1b51f 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 assert( desire
1b520 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c dAutoCommit==1 |
1b521 7c 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d | desiredAutoCom
1b522 6d 69 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 mit==0 );. asse
1b523 72 74 28 20 64 65 73 69 72 65 64 41 75 74 6f 43 rt( desiredAutoC
1b524 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 72 6f 6c 6c ommit==1 || roll
1b525 62 61 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 61 73 back==0 );.. as
1b526 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 sert( db->active
1b527 56 64 62 65 43 6e 74 3e 30 20 29 3b 20 20 2f 2a VdbeCnt>0 ); /*
1b528 20 41 74 20 6c 65 61 73 74 20 74 68 69 73 20 6f At least this o
1b529 6e 65 20 56 4d 20 69 73 20 61 63 74 69 76 65 20 ne VM is active
1b52a 2a 2f 0a 0a 20 20 69 66 28 20 74 75 72 6e 4f 6e */.. if( turnOn
1b52b 41 43 20 26 26 20 72 6f 6c 6c 62 61 63 6b 20 26 AC && rollback &
1b52c 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 & db->activeVdbe
1b52d 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 Cnt>1 ){. /*
1b52e 49 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 If this instruct
1b52f 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 ion implements a
1b530 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 6f 74 ROLLBACK and ot
1b531 68 65 72 20 56 4d 73 20 61 72 65 0a 20 20 20 20 her VMs are.
1b532 2a 2a 20 73 74 69 6c 6c 20 72 75 6e 6e 69 6e 67 ** still running
1b533 2c 20 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 , and a transact
1b534 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c 20 72 ion is active, r
1b535 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 eturn an error i
1b536 6e 64 69 63 61 74 69 6e 67 0a 20 20 20 20 2a 2a ndicating. **
1b537 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20 that the other
1b538 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74 VMs must complet
1b539 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20 2a 2f e first. . */
1b53a 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1b53b 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1b53c 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 72 g, db, "cannot r
1b53d 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74 ollback transact
1b53e 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20 ion - ".
1b53f 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 "SQL statements
1b540 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 in progress");.
1b541 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
1b542 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 USY;. }else if(
1b543 20 74 75 72 6e 4f 6e 41 43 20 26 26 20 21 72 6f turnOnAC && !ro
1b544 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 llback && db->wr
1b545 69 74 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a iteVdbeCnt>1 ){.
1b546 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
1b547 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 nstruction imple
1b548 6d 65 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 ments a COMMIT a
1b549 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 nd other VMs are
1b54a 20 77 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 writing. **
1b54b 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
1b54c 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 indicating that
1b54d 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 the other VMs mu
1b54e 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 st complete firs
1b54f 74 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 t. . */. s
1b550 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1b551 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1b552 20 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 "cannot commit
1b553 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a transaction - ".
1b554 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 "SQL sta
1b555 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 tements in progr
1b556 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 ess");. rc =
1b557 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
1b558 65 6c 73 65 20 69 66 28 20 64 65 73 69 72 65 64 else if( desired
1b559 41 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 2d 3e AutoCommit!=db->
1b55a 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 autoCommit ){.
1b55b 20 20 69 66 28 20 72 6f 6c 6c 62 61 63 6b 20 29 if( rollback )
1b55c 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1b55d 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 desiredAutoCommi
1b55e 74 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 t==1 );. sq
1b55f 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c lite3RollbackAll
1b560 28 64 62 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e (db);. db->
1b561 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a autoCommit = 1;.
1b562 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1b563 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 db->autoCommit
1b564 3d 20 28 75 38 29 64 65 73 69 72 65 64 41 75 74 = (u8)desiredAut
1b565 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20 69 oCommit;. i
1b566 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 f( sqlite3VdbeHa
1b567 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 lt(p)==SQLITE_BU
1b568 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d SY ){. p-
1b569 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 >pc = pc;.
1b56a 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 db->autoCommit
1b56b 20 3d 20 28 75 38 29 28 31 2d 64 65 73 69 72 65 = (u8)(1-desire
1b56c 64 41 75 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20 dAutoCommit);.
1b56d 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 p->rc = rc
1b56e 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a = SQLITE_BUSY;.
1b56f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 goto vdb
1b570 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 e_return;.
1b571 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 }. }. sqli
1b572 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e te3CloseSavepoin
1b573 74 73 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 ts(db);. if(
1b574 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b p->rc==SQLITE_OK
1b575 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
1b576 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 QLITE_DONE;.
1b577 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
1b578 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1b579 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 }. goto v
1b57a 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 dbe_return;. }e
1b57b 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
1b57c 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1b57d 72 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 rrMsg, db,.
1b57e 20 20 20 28 21 64 65 73 69 72 65 64 41 75 74 6f (!desiredAuto
1b57f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 20 Commit)?"cannot
1b580 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 start a transact
1b581 69 6f 6e 20 77 69 74 68 69 6e 20 61 20 74 72 61 ion within a tra
1b582 6e 73 61 63 74 69 6f 6e 22 3a 28 0a 20 20 20 20 nsaction":(.
1b583 20 20 20 20 28 72 6f 6c 6c 62 61 63 6b 29 3f 22 (rollback)?"
1b584 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 cannot rollback
1b585 2d 20 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e - no transaction
1b586 20 69 73 20 61 63 74 69 76 65 22 3a 0a 20 20 20 is active":.
1b587 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b588 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 2d "cannot commit -
1b589 20 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 no transaction
1b58a 69 73 20 61 63 74 69 76 65 22 29 29 3b 0a 20 20 is active"));.
1b58b 20 20 20 20 20 20 20 0a 20 20 20 20 72 63 20 3d . rc =
1b58c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1b58d 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1b58e 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 * Opcode: Transa
1b58f 63 74 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 ction P1 P2 * *
1b590 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 *.**.** Begin a
1b591 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 transaction. Th
1b592 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e e transaction en
1b593 64 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 ds when a Commit
1b594 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 or Rollback.**
1b595 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e opcode is encoun
1b596 74 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69 6e tered. Dependin
1b597 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 g on the ON CONF
1b598 4c 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74 68 LICT setting, th
1b599 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
1b59a 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 72 might also be r
1b59b 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e olled back if an
1b59c 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e error is encoun
1b59d 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 tered..**.** P1
1b59e 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 is the index of
1b59f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1b5a0 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74 e on which the t
1b5a1 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a ransaction is.**
1b5a2 20 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78 started. Index
1b5a3 20 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 0 is the main d
1b5a4 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1b5a5 20 69 6e 64 65 78 20 31 20 69 73 20 74 68 65 0a index 1 is the.
1b5a6 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 ** file used for
1b5a7 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
1b5a8 73 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32 s. Indices of 2
1b5a9 20 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65 or more are use
1b5aa 64 20 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 d for.** attache
1b5ab 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a d databases..**.
1b5ac 2a 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d ** If P2 is non-
1b5ad 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 zero, then a wri
1b5ae 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 te-transaction i
1b5af 73 20 73 74 61 72 74 65 64 2e 20 20 41 20 52 45 s started. A RE
1b5b0 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a SERVED lock is.*
1b5b1 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 * obtained on th
1b5b2 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1b5b3 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 when a write-tra
1b5b4 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 nsaction is star
1b5b5 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 ted. No.** othe
1b5b6 72 20 70 72 6f 63 65 73 73 20 63 61 6e 20 73 74 r process can st
1b5b7 61 72 74 20 61 6e 6f 74 68 65 72 20 77 72 69 74 art another writ
1b5b8 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 e transaction wh
1b5b9 69 6c 65 20 74 68 69 73 20 74 72 61 6e 73 61 63 ile this transac
1b5ba 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 tion is.** under
1b5bb 77 61 79 2e 20 20 53 74 61 72 74 69 6e 67 20 61 way. Starting a
1b5bc 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
1b5bd 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74 65 73 20 on also creates
1b5be 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e a rollback journ
1b5bf 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 al. A.** write t
1b5c0 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 ransaction must
1b5c1 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 be started befor
1b5c2 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 63 61 e any changes ca
1b5c3 6e 20 62 65 20 6d 61 64 65 20 74 6f 20 74 68 65 n be made to the
1b5c4 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 49 .** database. I
1b5c5 66 20 50 32 20 69 73 20 32 20 6f 72 20 67 72 65 f P2 is 2 or gre
1b5c6 61 74 65 72 20 74 68 65 6e 20 61 6e 20 45 58 43 ater then an EXC
1b5c7 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 LUSIVE lock is a
1b5c8 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 lso obtained.**
1b5c9 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a on the file..**.
1b5ca 2a 2a 20 49 66 20 50 32 20 69 73 20 7a 65 72 6f ** If P2 is zero
1b5cb 2c 20 74 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f , then a read-lo
1b5cc 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f ck is obtained o
1b5cd 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
1b5ce 69 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ile..*/.case OP_
1b5cf 54 72 61 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 20 Transaction: {.
1b5d0 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 int i = pOp->p1
1b5d1 3b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a ;. Btree *pBt;.
1b5d2 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
1b5d3 26 26 20 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a && i<db->nDb );.
1b5d4 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1b5d5 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29 reeMask & (1<<i)
1b5d6 29 21 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 )!=0 );. pBt =
1b5d7 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
1b5d8 0a 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 . if( pBt ){.
1b5d9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1b5da 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 42 reeBeginTrans(pB
1b5db 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 t, pOp->p2);.
1b5dc 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1b5dd 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 70 2d BUSY ){. p-
1b5de 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 >pc = pc;.
1b5df 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c p->rc = rc = SQL
1b5e0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 ITE_BUSY;.
1b5e1 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1b5e2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1b5e3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc!=SQLITE_OK &&
1b5e4 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 45 41 44 rc!=SQLITE_READ
1b5e5 4f 4e 4c 59 20 2f 2a 20 26 26 20 72 63 21 3d 53 ONLY /* && rc!=S
1b5e6 51 4c 49 54 45 5f 42 55 53 59 20 2a 2f 20 29 7b QLITE_BUSY */ ){
1b5e7 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 . goto abor
1b5e8 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1b5e9 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 }. }. brea
1b5ea 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1b5eb 20 52 65 61 64 43 6f 6f 6b 69 65 20 50 31 20 50 ReadCookie P1 P
1b5ec 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 2 P3 * *.**.** R
1b5ed 65 61 64 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 ead cookie numbe
1b5ee 72 20 50 33 20 66 72 6f 6d 20 64 61 74 61 62 61 r P3 from databa
1b5ef 73 65 20 50 31 20 61 6e 64 20 77 72 69 74 65 20 se P1 and write
1b5f0 69 74 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 it into register
1b5f1 20 50 32 2e 0a 2a 2a 20 50 33 3d 3d 30 20 69 73 P2..** P3==0 is
1b5f2 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 the schema vers
1b5f3 69 6f 6e 2e 20 20 50 33 3d 3d 31 20 69 73 20 74 ion. P3==1 is t
1b5f4 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d he database form
1b5f5 61 74 2e 0a 2a 2a 20 50 33 3d 3d 32 20 69 73 20 at..** P3==2 is
1b5f6 74 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 the recommended
1b5f7 70 61 67 65 72 20 63 61 63 68 65 20 73 69 7a 65 pager cache size
1b5f8 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 , and so forth.
1b5f9 20 50 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 P1==0 is.** the
1b5fa 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
1b5fb 69 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 ile and P1==1 is
1b5fc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1b5fd 6c 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 le used to store
1b5fe 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 .** temporary ta
1b5ff 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 bles..**.** If P
1b600 31 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 1 is negative, t
1b601 68 65 6e 20 74 68 69 73 20 69 73 20 61 20 72 65 hen this is a re
1b602 71 75 65 73 74 20 74 6f 20 72 65 61 64 20 74 68 quest to read th
1b603 65 20 73 69 7a 65 20 6f 66 20 61 0a 2a 2a 20 64 e size of a.** d
1b604 61 74 61 62 61 73 65 73 20 66 72 65 65 2d 6c 69 atabases free-li
1b605 73 74 2e 20 50 33 20 6d 75 73 74 20 62 65 20 73 st. P3 must be s
1b606 65 74 20 74 6f 20 31 20 69 6e 20 74 68 69 73 20 et to 1 in this
1b607 63 61 73 65 2e 20 54 68 65 20 61 63 74 75 61 6c case. The actual
1b608 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 63 .** database acc
1b609 65 73 73 65 64 20 69 73 20 28 28 50 31 2b 31 29 essed is ((P1+1)
1b60a 2a 2d 31 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c *-1). For exampl
1b60b 65 2c 20 61 20 50 31 20 70 61 72 61 6d 65 74 65 e, a P1 paramete
1b60c 72 20 6f 66 20 2d 31 0a 2a 2a 20 63 6f 72 72 65 r of -1.** corre
1b60d 73 70 6f 6e 64 73 20 74 6f 20 64 61 74 61 62 61 sponds to databa
1b60e 73 65 20 30 20 28 22 6d 61 69 6e 22 29 2c 20 61 se 0 ("main"), a
1b60f 20 50 31 20 6f 66 20 2d 32 20 69 73 20 64 61 74 P1 of -2 is dat
1b610 61 62 61 73 65 20 31 20 28 22 74 65 6d 70 22 29 abase 1 ("temp")
1b611 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 ..**.** There mu
1b612 73 74 20 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 st be a read-loc
1b613 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
1b614 65 20 28 65 69 74 68 65 72 20 61 20 74 72 61 6e e (either a tran
1b615 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 saction.** must
1b616 62 65 20 73 74 61 72 74 65 64 20 6f 72 20 74 68 be started or th
1b617 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f ere must be an o
1b618 70 65 6e 20 63 75 72 73 6f 72 29 20 62 65 66 6f pen cursor) befo
1b619 72 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 re.** executing
1b61a 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
1b61b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 ..*/.case OP_Rea
1b61c 64 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 dCookie: {
1b61d 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 /* out2
1b61e 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 -prerelease */.
1b61f 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 69 6e int iMeta;. in
1b620 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b t iDb = pOp->p1;
1b621 0a 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 20 3d . int iCookie =
1b622 20 70 4f 70 2d 3e 70 33 3b 0a 0a 20 20 61 73 73 pOp->p3;.. ass
1b623 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 53 51 4c ert( pOp->p3<SQL
1b624 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 ITE_N_BTREE_META
1b625 20 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 );. if( iDb<0
1b626 29 7b 0a 20 20 20 20 69 44 62 20 3d 20 28 2d 31 ){. iDb = (-1
1b627 2a 28 69 44 62 2b 31 29 29 3b 0a 20 20 20 20 69 *(iDb+1));. i
1b628 43 6f 6f 6b 69 65 20 2a 3d 20 2d 31 3b 0a 20 20 Cookie *= -1;.
1b629 7d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e }. assert( iDb>
1b62a 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 =0 && iDb<db->nD
1b62b 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 b );. assert( d
1b62c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 b->aDb[iDb].pBt!
1b62d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1b62e 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 (p->btreeMask &
1b62f 28 31 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a (1<<iDb))!=0 );.
1b630 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 69 6e /* The indexin
1b631 67 20 6f 66 20 6d 65 74 61 20 76 61 6c 75 65 73 g of meta values
1b632 20 61 74 20 74 68 65 20 73 63 68 65 6d 61 20 6c at the schema l
1b633 61 79 65 72 20 69 73 20 6f 66 66 20 62 79 20 6f ayer is off by o
1b634 6e 65 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 ne from. ** the
1b635 20 69 6e 64 65 78 69 6e 67 20 69 6e 20 74 68 65 indexing in the
1b636 20 62 74 72 65 65 20 6c 61 79 65 72 2e 20 20 54 btree layer. T
1b637 68 65 20 62 74 72 65 65 20 63 6f 6e 73 69 64 65 he btree conside
1b638 72 73 20 6d 65 74 61 5b 30 5d 20 74 6f 0a 20 20 rs meta[0] to.
1b639 2a 2a 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 ** be the number
1b63a 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 69 of free pages i
1b63b 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 n the database (
1b63c 61 20 72 65 61 64 2d 6f 6e 6c 79 20 76 61 6c 75 a read-only valu
1b63d 65 29 0a 20 20 2a 2a 20 61 6e 64 20 6d 65 74 61 e). ** and meta
1b63e 5b 31 5d 20 74 6f 20 62 65 20 74 68 65 20 73 63 [1] to be the sc
1b63f 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 54 68 hema cookie. Th
1b640 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 63 e schema layer c
1b641 6f 6e 73 69 64 65 72 73 0a 20 20 2a 2a 20 6d 65 onsiders. ** me
1b642 74 61 5b 31 5d 20 74 6f 20 62 65 20 74 68 65 20 ta[1] to be the
1b643 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 schema cookie.
1b644 53 6f 20 77 65 20 68 61 76 65 20 74 6f 20 73 68 So we have to sh
1b645 69 66 74 20 74 68 65 20 69 6e 64 65 78 0a 20 20 ift the index.
1b646 2a 2a 20 62 79 20 6f 6e 65 20 69 6e 20 74 68 65 ** by one in the
1b647 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 following state
1b648 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 ment.. */. rc
1b649 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
1b64a 74 4d 65 74 61 28 64 62 2d 3e 61 44 62 5b 69 44 tMeta(db->aDb[iD
1b64b 62 5d 2e 70 42 74 2c 20 31 20 2b 20 69 43 6f 6f b].pBt, 1 + iCoo
1b64c 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 69 4d 65 kie, (u32 *)&iMe
1b64d 74 61 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 ta);. pOut->u.i
1b64e 20 3d 20 69 4d 65 74 61 3b 0a 20 20 4d 65 6d 53 = iMeta;. MemS
1b64f 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c etTypeFlag(pOut,
1b650 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 MEM_Int);. bre
1b651 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1b652 3a 20 53 65 74 43 6f 6f 6b 69 65 20 50 31 20 50 : SetCookie P1 P
1b653 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 2 P3 * *.**.** W
1b654 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 rite the content
1b655 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 of register P3
1b656 28 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 (interpreted as
1b657 61 6e 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20 69 an integer).** i
1b658 6e 74 6f 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 nto cookie numbe
1b659 72 20 50 32 20 6f 66 20 64 61 74 61 62 61 73 65 r P2 of database
1b65a 20 50 31 2e 0a 2a 2a 20 50 32 3d 3d 30 20 69 73 P1..** P2==0 is
1b65b 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 the schema vers
1b65c 69 6f 6e 2e 20 20 50 32 3d 3d 31 20 69 73 20 74 ion. P2==1 is t
1b65d 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d he database form
1b65e 61 74 2e 0a 2a 2a 20 50 32 3d 3d 32 20 69 73 20 at..** P2==2 is
1b65f 74 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 the recommended
1b660 70 61 67 65 72 20 63 61 63 68 65 20 73 69 7a 65 pager cache size
1b661 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 , and so forth.
1b662 20 50 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 P1==0 is.** the
1b663 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
1b664 69 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 ile and P1==1 is
1b665 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1b666 6c 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 le used to store
1b667 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 .** temporary ta
1b668 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 bles..**.** A tr
1b669 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 ansaction must b
1b66a 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 e started before
1b66b 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 executing this
1b66c 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 opcode..*/.case
1b66d 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 OP_SetCookie: {
1b66e 20 20 20 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a /* in3 */.
1b66f 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 61 73 73 Db *pDb;. ass
1b670 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 53 51 4c ert( pOp->p2<SQL
1b671 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 ITE_N_BTREE_META
1b672 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1b673 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1b674 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 >p1<db->nDb );.
1b675 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 assert( (p->btr
1b676 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 eeMask & (1<<pOp
1b677 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 ->p1))!=0 );. p
1b678 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f Db = &db->aDb[pO
1b679 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1b67a 28 20 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b ( pDb->pBt!=0 );
1b67b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1b67c 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33 mIntegerify(pIn3
1b67d 29 3b 0a 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65 );. /* See note
1b67e 20 61 62 6f 75 74 20 69 6e 64 65 78 20 73 68 69 about index shi
1b67f 66 74 69 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64 fting on OP_Read
1b680 43 6f 6f 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d Cookie */. rc =
1b681 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
1b682 61 74 65 4d 65 74 61 28 70 44 62 2d 3e 70 42 74 ateMeta(pDb->pBt
1b683 2c 20 31 2b 70 4f 70 2d 3e 70 32 2c 20 28 69 6e , 1+pOp->p2, (in
1b684 74 29 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 t)pIn3->u.i);.
1b685 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 if( pOp->p2==0 )
1b686 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 {. /* When th
1b687 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 e schema cookie
1b688 63 68 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 changes, record
1b689 74 68 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 the new cookie i
1b68a 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 nternally */.
1b68b 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 pDb->pSchema->s
1b68c 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 28 chema_cookie = (
1b68d 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 int)pIn3->u.i;.
1b68e 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 db->flags |=
1b68f 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
1b690 6e 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 nges;. }else if
1b691 28 20 70 4f 70 2d 3e 70 32 3d 3d 31 20 29 7b 0a ( pOp->p2==1 ){.
1b692 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63 68 /* Record ch
1b693 61 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c anges in the fil
1b694 65 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 20 e format */.
1b695 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 pDb->pSchema->fi
1b696 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38 29 le_format = (u8)
1b697 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 pIn3->u.i;. }.
1b698 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 31 20 if( pOp->p1==1
1b699 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61 6c 69 ){. /* Invali
1b69a 64 61 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65 date all prepare
1b69b 64 20 73 74 61 74 65 6d 65 6e 74 73 20 77 68 65 d statements whe
1b69c 6e 65 76 65 72 20 74 68 65 20 54 45 4d 50 20 64 never the TEMP d
1b69d 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 73 atabase. ** s
1b69e 63 68 65 6d 61 20 69 73 20 63 68 61 6e 67 65 64 chema is changed
1b69f 2e 20 20 54 69 63 6b 65 74 20 23 31 36 34 34 20 . Ticket #1644
1b6a0 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 */. sqlite3Ex
1b6a1 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 pirePreparedStat
1b6a2 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 0a ements(db);. }.
1b6a3 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1b6a4 70 63 6f 64 65 3a 20 56 65 72 69 66 79 43 6f 6f pcode: VerifyCoo
1b6a5 6b 69 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a kie P1 P2 *.**.*
1b6a6 2a 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75 * Check the valu
1b6a7 65 20 6f 66 20 67 6c 6f 62 61 6c 20 64 61 74 61 e of global data
1b6a8 62 61 73 65 20 70 61 72 61 6d 65 74 65 72 20 6e base parameter n
1b6a9 75 6d 62 65 72 20 30 20 28 74 68 65 0a 2a 2a 20 umber 0 (the.**
1b6aa 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 29 20 schema version)
1b6ab 61 6e 64 20 6d 61 6b 65 20 73 75 72 65 20 69 74 and make sure it
1b6ac 20 69 73 20 65 71 75 61 6c 20 74 6f 20 50 32 2e is equal to P2.
1b6ad 20 20 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 .** P1 is the
1b6ae 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 database number
1b6af 77 68 69 63 68 20 69 73 20 30 20 66 6f 72 20 74 which is 0 for t
1b6b0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1b6b1 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66 file.** and 1 f
1b6b2 6f 72 20 74 68 65 20 66 69 6c 65 20 68 6f 6c 64 or the file hold
1b6b3 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 ing temporary ta
1b6b4 62 6c 65 73 20 61 6e 64 20 73 6f 6d 65 20 68 69 bles and some hi
1b6b5 67 68 65 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 gher number.** f
1b6b6 6f 72 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 or auxiliary dat
1b6b7 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 abases..**.** Th
1b6b8 65 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 e cookie changes
1b6b9 20 69 74 73 20 76 61 6c 75 65 20 77 68 65 6e 65 its value whene
1b6ba 76 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 ver the database
1b6bb 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2e schema changes.
1b6bc 0a 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69 .** This operati
1b6bd 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 on is used to de
1b6be 74 65 63 74 20 77 68 65 6e 20 74 68 61 74 20 74 tect when that t
1b6bf 68 65 20 63 6f 6f 6b 69 65 20 68 61 73 20 63 68 he cookie has ch
1b6c0 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 anged.** and tha
1b6c1 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 t the current pr
1b6c2 6f 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 72 ocess needs to r
1b6c3 65 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 eread the schema
1b6c4 2e 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61 ..**.** Either a
1b6c5 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 transaction nee
1b6c6 64 73 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20 ds to have been
1b6c7 73 74 61 72 74 65 64 20 6f 72 20 61 6e 20 4f 50 started or an OP
1b6c8 5f 4f 70 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 _Open needs.** t
1b6c9 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 28 74 o be executed (t
1b6ca 6f 20 65 73 74 61 62 6c 69 73 68 20 61 20 72 65 o establish a re
1b6cb 61 64 20 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20 ad lock) before
1b6cc 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a this opcode is.*
1b6cd 2a 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 * invoked..*/.ca
1b6ce 73 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b se OP_VerifyCook
1b6cf 69 65 3a 20 7b 0a 20 20 69 6e 74 20 69 4d 65 74 ie: {. int iMet
1b6d0 61 3b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b a;. Btree *pBt;
1b6d1 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1b6d2 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1b6d3 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 <db->nDb );. as
1b6d4 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d sert( (p->btreeM
1b6d5 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 ask & (1<<pOp->p
1b6d6 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 42 74 20 1))!=0 );. pBt
1b6d7 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 = db->aDb[pOp->p
1b6d8 31 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20 70 42 1].pBt;. if( pB
1b6d9 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 t ){. rc = sq
1b6da 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 lite3BtreeGetMet
1b6db 61 28 70 42 74 2c 20 31 2c 20 28 75 33 32 20 2a a(pBt, 1, (u32 *
1b6dc 29 26 69 4d 65 74 61 29 3b 0a 20 20 7d 65 6c 73 )&iMeta);. }els
1b6dd 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 e{. rc = SQLI
1b6de 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 4d 65 74 61 TE_OK;. iMeta
1b6df 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 0;. }. if(
1b6e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1b6e1 20 69 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 32 20 iMeta!=pOp->p2
1b6e2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 ){. sqlite3Db
1b6e3 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 Free(db, p->zErr
1b6e4 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 Msg);. p->zEr
1b6e5 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 rMsg = sqlite3Db
1b6e6 53 74 72 44 75 70 28 64 62 2c 20 22 64 61 74 61 StrDup(db, "data
1b6e7 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 base schema has
1b6e8 63 68 61 6e 67 65 64 22 29 3b 0a 20 20 20 20 2f changed");. /
1b6e9 2a 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 2d * If the schema-
1b6ea 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 cookie from the
1b6eb 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 database file ma
1b6ec 74 63 68 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 tches the cookie
1b6ed 20 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 . ** stored
1b6ee 77 69 74 68 20 74 68 65 20 69 6e 2d 6d 65 6d 6f with the in-memo
1b6ef 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ry representatio
1b6f0 6e 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 2c n of the schema,
1b6f1 20 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 do. ** not r
1b6f2 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61 eload the schema
1b6f3 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
1b6f4 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a se file.. **.
1b6f5 20 20 20 20 2a 2a 20 49 66 20 76 69 72 74 75 61 ** If virtua
1b6f6 6c 2d 74 61 62 6c 65 73 20 61 72 65 20 69 6e 20 l-tables are in
1b6f7 75 73 65 2c 20 74 68 69 73 20 69 73 20 6e 6f 74 use, this is not
1b6f8 20 6a 75 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a just an optimiz
1b6f9 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66 ation.. ** Of
1b6fa 74 65 6e 2c 20 76 2d 74 61 62 6c 65 73 20 73 74 ten, v-tables st
1b6fb 6f 72 65 20 74 68 65 69 72 20 64 61 74 61 20 69 ore their data i
1b6fc 6e 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 74 n other SQLite t
1b6fd 61 62 6c 65 73 2c 20 77 68 69 63 68 0a 20 20 20 ables, which.
1b6fe 20 2a 2a 20 61 72 65 20 71 75 65 72 69 65 64 20 ** are queried
1b6ff 66 72 6f 6d 20 77 69 74 68 69 6e 20 78 4e 65 78 from within xNex
1b700 74 28 29 20 61 6e 64 20 6f 74 68 65 72 20 76 2d t() and other v-
1b701 74 61 62 6c 65 20 6d 65 74 68 6f 64 73 20 75 73 table methods us
1b702 69 6e 67 0a 20 20 20 20 2a 2a 20 70 72 65 70 61 ing. ** prepa
1b703 72 65 64 20 71 75 65 72 69 65 73 2e 20 49 66 20 red queries. If
1b704 73 75 63 68 20 61 20 71 75 65 72 79 20 69 73 20 such a query is
1b705 6f 75 74 2d 6f 66 2d 64 61 74 65 2c 20 77 65 20 out-of-date, we
1b706 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 0a 20 do not want to.
1b707 20 20 20 2a 2a 20 64 69 73 63 61 72 64 20 74 68 ** discard th
1b708 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
1b709 61 2c 20 61 73 20 74 68 65 20 75 73 65 72 20 63 a, as the user c
1b70a 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 ode implementing
1b70b 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 2d 74 61 the. ** v-ta
1b70c 62 6c 65 20 77 6f 75 6c 64 20 68 61 76 65 20 74 ble would have t
1b70d 6f 20 62 65 20 72 65 61 64 79 20 66 6f 72 20 74 o be ready for t
1b70e 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 he sqlite3_vtab
1b70f 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 structure itself
1b710 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e . ** to be in
1b711 76 61 6c 69 64 61 74 65 64 20 77 68 65 6e 65 76 validated whenev
1b712 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 er sqlite3_step(
1b713 29 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d ) is called from
1b714 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 within . **
1b715 61 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64 a v-table method
1b716 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1b717 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 db->aDb[pOp->p1
1b718 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d ].pSchema->schem
1b719 61 5f 63 6f 6f 6b 69 65 21 3d 69 4d 65 74 61 20 a_cookie!=iMeta
1b71a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1b71b 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 ResetInternalSch
1b71c 65 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 ema(db, pOp->p1)
1b71d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c ;. }.. sql
1b71e 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 ite3ExpirePrepar
1b71f 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 edStatements(db)
1b720 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
1b721 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 E_SCHEMA;. }.
1b722 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1b723 6f 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 50 31 ode: OpenRead P1
1b724 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a P2 P3 P4 P5.**.
1b725 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f ** Open a read-o
1b726 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20 74 nly cursor for t
1b727 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c he database tabl
1b728 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 e whose root pag
1b729 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 20 e is.** P2 in a
1b72a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
1b72b 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
1b72c 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 e is determined
1b72d 62 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30 by P3. .** P3==0
1b72e 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 means the main
1b72f 64 61 74 61 62 61 73 65 2c 20 50 33 3d 3d 31 20 database, P3==1
1b730 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 means the databa
1b731 73 65 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a 20 se used for .**
1b732 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
1b733 2c 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e 73 , and P3>1 means
1b734 20 75 73 65 64 20 74 68 65 20 63 6f 72 72 65 73 used the corres
1b735 70 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 65 64 ponding attached
1b736 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 47 .** database. G
1b737 69 76 65 20 74 68 65 20 6e 65 77 20 63 75 72 73 ive the new curs
1b738 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 or an identifier
1b739 20 6f 66 20 50 31 2e 20 20 54 68 65 20 50 31 0a of P1. The P1.
1b73a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20 6e ** values need n
1b73b 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 73 ot be contiguous
1b73c 20 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c 75 but all P1 valu
1b73d 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d 61 es should be sma
1b73e 6c 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 ll integers..**
1b73f 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66 It is an error f
1b740 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67 61 or P1 to be nega
1b741 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 tive..**.** If P
1b742 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20 74 68 5!=0 then use th
1b743 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 e content of reg
1b744 69 73 74 65 72 20 50 32 20 61 73 20 74 68 65 20 ister P2 as the
1b745 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a 2a root page, not.*
1b746 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 * the value of P
1b747 32 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 2 itself..**.**
1b748 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61 20 There will be a
1b749 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 read lock on the
1b74a 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 65 76 database whenev
1b74b 65 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a 2a er there is an.*
1b74c 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 20 * open cursor.
1b74d 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
1b74e 77 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 69 was unlocked pri
1b74f 6f 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74 72 or to this instr
1b750 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 uction.** then a
1b751 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 63 read lock is ac
1b752 71 75 69 72 65 64 20 61 73 20 70 61 72 74 20 6f quired as part o
1b753 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 f this instructi
1b754 6f 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 6c on. A read.** l
1b755 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 ock allows other
1b756 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72 65 processes to re
1b757 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ad the database
1b758 62 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a 2a but prohibits.**
1b759 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 any other proce
1b75a 73 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 6e ss from modifyin
1b75b 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 g the database.
1b75c 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 69 The read lock i
1b75d 73 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68 s.** released wh
1b75e 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 en all cursors a
1b75f 72 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 re closed. If t
1b760 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1b761 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 attempts.** to g
1b762 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 62 et a read lock b
1b763 75 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73 63 ut fails, the sc
1b764 72 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73 20 ript terminates
1b765 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 with an.** SQLIT
1b766 45 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f 64 E_BUSY error cod
1b767 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 e..**.** The P4
1b768 76 61 6c 75 65 20 69 73 20 61 20 70 6f 69 6e 74 value is a point
1b769 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 er to a KeyInfo
1b76a 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 structure that d
1b76b 65 66 69 6e 65 73 20 74 68 65 0a 2a 2a 20 63 6f efines the.** co
1b76c 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 ntent and collat
1b76d 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20 ing sequence of
1b76e 69 6e 64 69 63 65 73 2e 20 20 50 34 20 69 73 20 indices. P4 is
1b76f 4e 55 4c 4c 20 66 6f 72 20 63 75 72 73 6f 72 73 NULL for cursors
1b770 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 .** that are not
1b771 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 64 pointing to ind
1b772 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 ices..**.** See
1b773 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a also OpenWrite..
1b774 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 */./* Opcode: Op
1b775 65 6e 57 72 69 74 65 20 50 31 20 50 32 20 50 33 enWrite P1 P2 P3
1b776 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 P4 P5.**.** Ope
1b777 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 n a read/write c
1b778 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 20 6f ursor named P1 o
1b779 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 n the table or i
1b77a 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 0a ndex whose root.
1b77b 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e 20 20 ** page is P2.
1b77c 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73 65 20 Or if P5!=0 use
1b77d 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1b77e 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 66 69 egister P2 to fi
1b77f 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 nd the.** root p
1b780 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 age..**.** The P
1b781 34 20 76 61 6c 75 65 20 69 73 20 61 20 70 6f 69 4 value is a poi
1b782 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 nter to a KeyInf
1b783 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 o structure that
1b784 20 64 65 66 69 6e 65 73 20 74 68 65 0a 2a 2a 20 defines the.**
1b785 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c content and coll
1b786 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f ating sequence o
1b787 66 20 69 6e 64 69 63 65 73 2e 20 20 50 34 20 69 f indices. P4 i
1b788 73 20 4e 55 4c 4c 20 66 6f 72 20 63 75 72 73 6f s NULL for curso
1b789 72 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 6e rs.** that are n
1b78a 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 ot pointing to i
1b78b 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ndices..**.** Th
1b78c 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 is instruction w
1b78d 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 4f orks just like O
1b78e 70 65 6e 52 65 61 64 20 65 78 63 65 70 74 20 74 penRead except t
1b78f 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74 68 65 hat it opens the
1b790 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 65 cursor.** in re
1b791 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e 20 20 ad/write mode.
1b792 46 6f 72 20 61 20 67 69 76 65 6e 20 74 61 62 6c For a given tabl
1b793 65 2c 20 74 68 65 72 65 20 63 61 6e 20 62 65 20 e, there can be
1b794 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 one or more read
1b795 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 73 -only.** cursors
1b796 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65 61 or a single rea
1b797 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 62 d/write cursor b
1b798 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a ut not both..**.
1b799 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e ** See also Open
1b79a 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 Read..*/.case OP
1b79b 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 65 20 _OpenRead:.case
1b79c 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b 0a OP_OpenWrite: {.
1b79d 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 int i = pOp->p
1b79e 31 3b 0a 20 20 69 6e 74 20 70 32 20 3d 20 70 4f 1;. int p2 = pO
1b79f 70 2d 3e 70 32 3b 0a 20 20 69 6e 74 20 69 44 62 p->p2;. int iDb
1b7a0 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e = pOp->p3;. in
1b7a1 74 20 77 72 46 6c 61 67 3b 0a 20 20 42 74 72 65 t wrFlag;. Btre
1b7a2 65 20 2a 70 58 3b 0a 20 20 56 64 62 65 43 75 72 e *pX;. VdbeCur
1b7a3 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 44 62 20 sor *pCur;. Db
1b7a4 2a 70 44 62 3b 0a 20 20 0a 20 20 61 73 73 65 72 *pDb;. . asser
1b7a5 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 t( iDb>=0 && iDb
1b7a6 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 <db->nDb );. as
1b7a7 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d sert( (p->btreeM
1b7a8 61 73 6b 20 26 20 28 31 3c 3c 69 44 62 29 29 21 ask & (1<<iDb))!
1b7a9 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 =0 );. pDb = &d
1b7aa 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 70 b->aDb[iDb];. p
1b7ab 58 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20 X = pDb->pBt;.
1b7ac 61 73 73 65 72 74 28 20 70 58 21 3d 30 20 29 3b assert( pX!=0 );
1b7ad 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f . if( pOp->opco
1b7ae 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 de==OP_OpenWrite
1b7af 20 29 7b 0a 20 20 20 20 77 72 46 6c 61 67 20 3d ){. wrFlag =
1b7b0 20 31 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 1;. if( pDb-
1b7b1 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 >pSchema->file_f
1b7b2 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d 69 6e 57 72 ormat < p->minWr
1b7b3 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 29 7b iteFileFormat ){
1b7b4 0a 20 20 20 20 20 20 70 2d 3e 6d 69 6e 57 72 69 . p->minWri
1b7b5 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 70 teFileFormat = p
1b7b6 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c Db->pSchema->fil
1b7b7 65 5f 66 6f 72 6d 61 74 3b 0a 20 20 20 20 7d 0a e_format;. }.
1b7b8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 72 46 }else{. wrF
1b7b9 6c 61 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 lag = 0;. }. i
1b7ba 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 f( pOp->p5 ){.
1b7bb 20 20 61 73 73 65 72 74 28 20 70 32 3e 30 20 29 assert( p2>0 )
1b7bc 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 32 ;. assert( p2
1b7bd 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 <=p->nMem );.
1b7be 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d pIn2 = &p->aMem
1b7bf 5b 70 32 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 [p2];. sqlite
1b7c0 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 3VdbeMemIntegeri
1b7c1 66 79 28 70 49 6e 32 29 3b 0a 20 20 20 20 70 32 fy(pIn2);. p2
1b7c2 20 3d 20 28 69 6e 74 29 70 49 6e 32 2d 3e 75 2e = (int)pIn2->u.
1b7c3 69 3b 0a 20 20 20 20 69 66 28 20 70 32 3c 32 20 i;. if( p2<2
1b7c4 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ) {. rc = S
1b7c5 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
1b7c6 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 PT;. goto a
1b7c7 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1b7c8 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 r;. }. }. a
1b7c9 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 ssert( i>=0 );.
1b7ca 20 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 pCur = allocate
1b7cb 43 75 72 73 6f 72 28 70 2c 20 69 2c 20 26 70 4f Cursor(p, i, &pO
1b7cc 70 5b 2d 31 5d 2c 20 69 44 62 2c 20 31 29 3b 0a p[-1], iDb, 1);.
1b7cd 20 20 69 66 28 20 70 43 75 72 3d 3d 30 20 29 20 if( pCur==0 )
1b7ce 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 goto no_mem;. p
1b7cf 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 Cur->nullRow = 1
1b7d0 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1b7d1 42 74 72 65 65 43 75 72 73 6f 72 28 70 58 2c 20 BtreeCursor(pX,
1b7d2 70 32 2c 20 77 72 46 6c 61 67 2c 20 70 4f 70 2d p2, wrFlag, pOp-
1b7d3 3e 70 34 2e 70 2c 20 70 43 75 72 2d 3e 70 43 75 >p4.p, pCur->pCu
1b7d4 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 70 4f 70 rsor);. if( pOp
1b7d5 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 ->p4type==P4_KEY
1b7d6 49 4e 46 4f 20 29 7b 0a 20 20 20 20 70 43 75 72 INFO ){. pCur
1b7d7 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 ->pKeyInfo = pOp
1b7d8 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 ->p4.pKeyInfo;.
1b7d9 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 pCur->pKeyInf
1b7da 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e o->enc = ENC(p->
1b7db 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 db);. }else{.
1b7dc 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f pCur->pKeyInfo
1b7dd 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 77 69 74 = 0;. }. swit
1b7de 63 68 28 20 72 63 20 29 7b 0a 20 20 20 20 63 61 ch( rc ){. ca
1b7df 73 65 20 53 51 4c 49 54 45 5f 42 55 53 59 3a 20 se SQLITE_BUSY:
1b7e0 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 {. p->pc =
1b7e1 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 pc;. p->rc
1b7e2 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 = rc = SQLITE_BU
1b7e3 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 SY;. goto v
1b7e4 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 dbe_return;.
1b7e5 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
1b7e6 45 5f 4f 4b 3a 20 7b 0a 20 20 20 20 20 20 69 6e E_OK: {. in
1b7e7 74 20 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 t flags = sqlite
1b7e8 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 75 72 3BtreeFlags(pCur
1b7e9 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 ->pCursor);.
1b7ea 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 /* Sanity chec
1b7eb 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 74 68 65 20 king. Only the
1b7ec 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73 20 lower four bits
1b7ed 6f 66 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 of the flags byt
1b7ee 65 20 73 68 6f 75 6c 64 0a 20 20 20 20 20 20 2a e should. *
1b7ef 2a 20 62 65 20 75 73 65 64 2e 20 20 42 69 74 20 * be used. Bit
1b7f0 33 20 28 6d 61 73 6b 20 30 78 30 38 29 20 69 73 3 (mask 0x08) is
1b7f1 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 2e 20 unpredictable.
1b7f2 20 54 68 65 20 6c 6f 77 65 72 20 33 20 62 69 74 The lower 3 bit
1b7f3 73 0a 20 20 20 20 20 20 2a 2a 20 28 6d 61 73 6b s. ** (mask
1b7f4 20 30 78 30 37 29 20 73 68 6f 75 6c 64 20 62 65 0x07) should be
1b7f5 20 65 69 74 68 65 72 20 35 20 28 69 6e 74 6b 65 either 5 (intke
1b7f6 79 2b 6c 65 61 66 64 61 74 61 20 66 6f 72 20 74 y+leafdata for t
1b7f7 61 62 6c 65 73 29 20 6f 72 0a 20 20 20 20 20 20 ables) or.
1b7f8 2a 2a 20 32 20 28 7a 65 72 6f 64 61 74 61 20 66 ** 2 (zerodata f
1b7f9 6f 72 20 69 6e 64 69 63 65 73 29 2e 20 20 49 66 or indices). If
1b7fa 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e these condition
1b7fb 73 20 61 72 65 20 6e 6f 74 20 6d 65 74 20 69 74 s are not met it
1b7fc 20 63 61 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 6e can. ** on
1b7fd 6c 79 20 6d 65 61 6e 20 74 68 61 74 20 77 65 20 ly mean that we
1b7fe 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
1b7ff 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 a corrupt datab
1b800 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a ase file. *
1b801 2f 0a 20 20 20 20 20 20 69 66 28 20 28 66 6c 61 /. if( (fla
1b802 67 73 20 26 20 30 78 66 30 29 21 3d 30 20 7c 7c gs & 0xf0)!=0 ||
1b803 20 28 28 66 6c 61 67 73 20 26 20 30 78 30 37 29 ((flags & 0x07)
1b804 21 3d 35 20 26 26 20 28 66 6c 61 67 73 20 26 20 !=5 && (flags &
1b805 30 78 30 37 29 21 3d 32 29 20 29 7b 0a 20 20 20 0x07)!=2) ){.
1b806 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1b807 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1b808 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 goto abor
1b809 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1b80a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 }. pC
1b80b 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20 28 66 ur->isTable = (f
1b80c 6c 61 67 73 20 26 20 42 54 52 45 45 5f 49 4e 54 lags & BTREE_INT
1b80d 4b 45 59 29 21 3d 30 20 3f 31 3a 30 3b 0a 20 20 KEY)!=0 ?1:0;.
1b80e 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e 64 65 pCur->isInde
1b80f 78 20 3d 20 28 66 6c 61 67 73 20 26 20 42 54 52 x = (flags & BTR
1b810 45 45 5f 5a 45 52 4f 44 41 54 41 29 21 3d 30 20 EE_ZERODATA)!=0
1b811 3f 31 3a 30 3b 0a 20 20 20 20 20 20 2f 2a 20 49 ?1:0;. /* I
1b812 66 20 50 34 3d 3d 30 20 69 74 20 6d 65 61 6e 73 f P4==0 it means
1b813 20 77 65 20 61 72 65 20 65 78 70 65 63 74 65 64 we are expected
1b814 20 74 6f 20 6f 70 65 6e 20 61 20 74 61 62 6c 65 to open a table
1b815 2e 20 20 49 66 20 50 34 21 3d 30 20 74 68 65 6e . If P4!=0 then
1b816 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 65 78 70 . ** we exp
1b817 65 63 74 20 74 6f 20 62 65 20 6f 70 65 6e 69 6e ect to be openin
1b818 67 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20 g an index. If
1b819 74 68 69 73 20 69 73 20 6e 6f 74 20 77 68 61 74 this is not what
1b81a 20 68 61 70 70 65 6e 65 64 2c 0a 20 20 20 20 20 happened,.
1b81b 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 64 61 74 ** then the dat
1b81c 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 abase is corrupt
1b81d 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1b81e 69 66 28 20 28 70 43 75 72 2d 3e 69 73 54 61 62 if( (pCur->isTab
1b81f 6c 65 20 26 26 20 70 4f 70 2d 3e 70 34 74 79 70 le && pOp->p4typ
1b820 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 29 0a 20 e==P4_KEYINFO).
1b821 20 20 20 20 20 20 7c 7c 20 28 70 43 75 72 2d 3e || (pCur->
1b822 69 73 49 6e 64 65 78 20 26 26 20 70 4f 70 2d 3e isIndex && pOp->
1b823 70 34 74 79 70 65 21 3d 50 34 5f 4b 45 59 49 4e p4type!=P4_KEYIN
1b824 46 4f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 FO) ){. r
1b825 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
1b826 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 PT_BKPT;.
1b827 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1b828 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 to_error;.
1b829 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1b82a 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 }. case SQ
1b82b 4c 49 54 45 5f 45 4d 50 54 59 3a 20 7b 0a 20 20 LITE_EMPTY: {.
1b82c 20 20 20 20 70 43 75 72 2d 3e 69 73 54 61 62 6c pCur->isTabl
1b82d 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 70 65 21 e = pOp->p4type!
1b82e 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20 =P4_KEYINFO;.
1b82f 20 20 20 70 43 75 72 2d 3e 69 73 49 6e 64 65 78 pCur->isIndex
1b830 20 3d 20 21 70 43 75 72 2d 3e 69 73 54 61 62 6c = !pCur->isTabl
1b831 65 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 e;. pCur->p
1b832 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 Cursor = 0;.
1b833 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
1b834 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1b835 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 }. default
1b836 3a 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 : {. goto a
1b837 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1b838 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 r;. }. }. b
1b839 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1b83a 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 de: OpenEphemera
1b83b 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a l P1 P2 * P4 *.*
1b83c 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 *.** Open a new
1b83d 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20 74 cursor P1 to a t
1b83e 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 2e 0a ransient table..
1b83f 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 ** The cursor is
1b840 20 61 6c 77 61 79 73 20 6f 70 65 6e 65 64 20 72 always opened r
1b841 65 61 64 2f 77 72 69 74 65 20 65 76 65 6e 20 69 ead/write even i
1b842 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 f .** the main d
1b843 61 74 61 62 61 73 65 20 69 73 20 72 65 61 64 2d atabase is read-
1b844 6f 6e 6c 79 2e 20 20 54 68 65 20 74 72 61 6e 73 only. The trans
1b845 69 65 6e 74 20 6f 72 20 76 69 72 74 75 61 6c 0a ient or virtual.
1b846 2a 2a 20 74 61 62 6c 65 20 69 73 20 64 65 6c 65 ** table is dele
1b847 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ted automaticall
1b848 79 20 77 68 65 6e 20 74 68 65 20 63 75 72 73 6f y when the curso
1b849 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a r is closed..**.
1b84a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d ** P2 is the num
1b84b 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
1b84c 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 n the virtual ta
1b84d 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 ble..** The curs
1b84e 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 42 or points to a B
1b84f 54 72 65 65 20 74 61 62 6c 65 20 69 66 20 50 34 Tree table if P4
1b850 3d 3d 30 20 61 6e 64 20 74 6f 20 61 20 42 54 72 ==0 and to a BTr
1b851 65 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66 20 50 ee index.** if P
1b852 34 20 69 73 20 6e 6f 74 20 30 2e 20 20 49 66 20 4 is not 0. If
1b853 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 P4 is not NULL,
1b854 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b it points to a K
1b855 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 eyInfo structure
1b856 0a 2a 2a 20 74 68 61 74 20 64 65 66 69 6e 65 73 .** that defines
1b857 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 6b the format of k
1b858 65 79 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 eys in the index
1b859 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 ..**.** This opc
1b85a 6f 64 65 20 77 61 73 20 6f 6e 63 65 20 63 61 6c ode was once cal
1b85b 6c 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20 20 42 led OpenTemp. B
1b85c 75 74 20 74 68 61 74 20 63 72 65 61 74 65 64 0a ut that created.
1b85d 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62 65 63 ** confusion bec
1b85e 61 75 73 65 20 74 68 65 20 74 65 72 6d 20 22 74 ause the term "t
1b85f 65 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69 67 68 emp table", migh
1b860 74 20 72 65 66 65 72 20 65 69 74 68 65 72 0a 2a t refer either.*
1b861 2a 20 74 6f 20 61 20 54 45 4d 50 20 74 61 62 6c * to a TEMP tabl
1b862 65 20 61 74 20 74 68 65 20 53 51 4c 20 6c 65 76 e at the SQL lev
1b863 65 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61 62 6c el, or to a tabl
1b864 65 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a 20 74 e opened by.** t
1b865 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 his opcode. The
1b866 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 77 61 n this opcode wa
1b867 73 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72 74 75 s call OpenVirtu
1b868 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 al. But.** that
1b869 20 63 72 65 61 74 65 64 20 63 6f 6e 66 75 73 69 created confusi
1b86a 6f 6e 20 77 69 74 68 20 74 68 65 20 77 68 6f 6c on with the whol
1b86b 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 e virtual-table
1b86c 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 idea..*/.case OP
1b86d 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 3a 20 _OpenEphemeral:
1b86e 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d {. int i = pOp-
1b86f 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f >p1;. VdbeCurso
1b870 72 20 2a 70 43 78 3b 0a 20 20 73 74 61 74 69 63 r *pCx;. static
1b871 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 65 6e 46 const int openF
1b872 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 20 53 51 lags = . SQ
1b873 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
1b874 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 ITE |. SQLI
1b875 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c TE_OPEN_CREATE |
1b876 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 . SQLITE_OP
1b877 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20 EN_EXCLUSIVE |.
1b878 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
1b879 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c _DELETEONCLOSE |
1b87a 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 . SQLITE_OP
1b87b 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 3b EN_TRANSIENT_DB;
1b87c 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 .. assert( i>=0
1b87d 20 29 3b 0a 20 20 70 43 78 20 3d 20 61 6c 6c 6f );. pCx = allo
1b87e 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 69 2c cateCursor(p, i,
1b87f 20 70 4f 70 2c 20 2d 31 2c 20 31 29 3b 0a 20 20 pOp, -1, 1);.
1b880 69 66 28 20 70 43 78 3d 3d 30 20 29 20 67 6f 74 if( pCx==0 ) got
1b881 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 78 2d o no_mem;. pCx-
1b882 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 >nullRow = 1;.
1b883 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1b884 65 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c 20 eFactory(db, 0,
1b885 31 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 1, SQLITE_DEFAUL
1b886 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a T_TEMP_CACHE_SIZ
1b887 45 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 E, openFlags,.
1b888 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b889 20 20 20 20 20 20 20 20 20 26 70 43 78 2d 3e 70 &pCx->p
1b88a 42 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 Bt);. if( rc==S
1b88b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1b88c 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1b88d 65 42 65 67 69 6e 54 72 61 6e 73 28 70 43 78 2d eBeginTrans(pCx-
1b88e 3e 70 42 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 >pBt, 1);. }.
1b88f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1b890 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 K ){. /* If a
1b891 20 74 72 61 6e 73 69 65 6e 74 20 69 6e 64 65 78 transient index
1b892 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 63 72 is required, cr
1b893 65 61 74 65 20 69 74 20 62 79 20 63 61 6c 6c 69 eate it by calli
1b894 6e 67 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 ng. ** sqlite
1b895 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 3BtreeCreateTabl
1b896 65 28 29 20 77 69 74 68 20 74 68 65 20 42 54 52 e() with the BTR
1b897 45 45 5f 5a 45 52 4f 44 41 54 41 20 66 6c 61 67 EE_ZERODATA flag
1b898 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 6f before. ** o
1b899 70 65 6e 69 6e 67 20 69 74 2e 20 49 66 20 61 20 pening it. If a
1b89a 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 transient table
1b89b 69 73 20 72 65 71 75 69 72 65 64 2c 20 6a 75 73 is required, jus
1b89c 74 20 75 73 65 20 74 68 65 0a 20 20 20 20 2a 2a t use the. **
1b89d 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 automatically c
1b89e 72 65 61 74 65 64 20 74 61 62 6c 65 20 77 69 74 reated table wit
1b89f 68 20 72 6f 6f 74 2d 70 61 67 65 20 31 20 28 61 h root-page 1 (a
1b8a0 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 29 2e n INTKEY table).
1b8a1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1b8a2 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f pOp->p4.pKeyInfo
1b8a3 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 67 ){. int pg
1b8a4 6e 6f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 no;. assert
1b8a5 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1b8a6 34 5f 4b 45 59 49 4e 46 4f 20 29 3b 0a 20 20 20 4_KEYINFO );.
1b8a7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1b8a8 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 treeCreateTable(
1b8a9 70 43 78 2d 3e 70 42 74 2c 20 26 70 67 6e 6f 2c pCx->pBt, &pgno,
1b8aa 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 BTREE_ZERODATA)
1b8ab 3b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d ; . if( rc=
1b8ac 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1b8ad 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 67 assert( pg
1b8ae 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 2b no==MASTER_ROOT+
1b8af 31 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 1 );. rc
1b8b0 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 = sqlite3BtreeCu
1b8b1 72 73 6f 72 28 70 43 78 2d 3e 70 42 74 2c 20 70 rsor(pCx->pBt, p
1b8b2 67 6e 6f 2c 20 31 2c 20 0a 20 20 20 20 20 20 20 gno, 1, .
1b8b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b8b4 20 20 20 20 20 20 20 20 20 28 4b 65 79 49 6e 66 (KeyInf
1b8b5 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c 20 70 43 o*)pOp->p4.z, pC
1b8b6 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 x->pCursor);.
1b8b7 20 20 20 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e pCx->pKeyIn
1b8b8 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 fo = pOp->p4.pKe
1b8b9 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 70 yInfo;. p
1b8ba 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e Cx->pKeyInfo->en
1b8bb 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a c = ENC(p->db);.
1b8bc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 }. pC
1b8bd 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 30 3b 0a x->isTable = 0;.
1b8be 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1b8bf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1b8c0 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 42 eeCursor(pCx->pB
1b8c1 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 t, MASTER_ROOT,
1b8c2 31 2c 20 30 2c 20 70 43 78 2d 3e 70 43 75 72 73 1, 0, pCx->pCurs
1b8c3 6f 72 29 3b 0a 20 20 20 20 20 20 70 43 78 2d 3e or);. pCx->
1b8c4 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 isTable = 1;.
1b8c5 20 7d 0a 20 20 7d 0a 20 20 70 43 78 2d 3e 69 73 }. }. pCx->is
1b8c6 49 6e 64 65 78 20 3d 20 21 70 43 78 2d 3e 69 73 Index = !pCx->is
1b8c7 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a Table;. break;.
1b8c8 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 }../* Opcode: Op
1b8c9 65 6e 50 73 65 75 64 6f 20 50 31 20 50 32 20 2a enPseudo P1 P2 *
1b8ca 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 * *.**.** Open
1b8cb 61 20 6e 65 77 20 63 75 72 73 6f 72 20 74 68 61 a new cursor tha
1b8cc 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 61 t points to a fa
1b8cd 6b 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f ke table that co
1b8ce 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a ntains a single.
1b8cf 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 20 ** row of data.
1b8d0 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 Any attempt to
1b8d1 77 72 69 74 65 20 61 20 73 65 63 6f 6e 64 20 72 write a second r
1b8d2 6f 77 20 6f 66 20 64 61 74 61 20 63 61 75 73 65 ow of data cause
1b8d3 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 72 s the.** first r
1b8d4 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 ow to be deleted
1b8d5 2e 20 20 41 6c 6c 20 64 61 74 61 20 69 73 20 64 . All data is d
1b8d6 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 eleted when the
1b8d7 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 63 6c 6f cursor is.** clo
1b8d8 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 73 65 sed..**.** A pse
1b8d9 75 64 6f 2d 74 61 62 6c 65 20 63 72 65 61 74 65 udo-table create
1b8da 64 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 d by this opcode
1b8db 20 69 73 20 75 73 65 66 75 6c 20 66 6f 72 20 68 is useful for h
1b8dc 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 45 olding the.** NE
1b8dd 57 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65 73 20 W or OLD tables
1b8de 69 6e 20 61 20 74 72 69 67 67 65 72 2e 20 20 41 in a trigger. A
1b8df 6c 73 6f 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 lso used to hold
1b8e0 20 74 68 65 20 61 20 73 69 6e 67 6c 65 0a 2a 2a the a single.**
1b8e1 20 72 6f 77 20 6f 75 74 70 75 74 20 66 72 6f 6d row output from
1b8e2 20 74 68 65 20 73 6f 72 74 65 72 20 73 6f 20 74 the sorter so t
1b8e3 68 61 74 20 74 68 65 20 72 6f 77 20 63 61 6e 20 hat the row can
1b8e4 62 65 20 64 65 63 6f 6d 70 6f 73 65 64 20 69 6e be decomposed in
1b8e5 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c to.** individual
1b8e6 20 63 6f 6c 75 6d 6e 73 20 75 73 69 6e 67 20 74 columns using t
1b8e7 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 he OP_Column opc
1b8e8 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ode..**.** When
1b8e9 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 65 78 65 OP_Insert is exe
1b8ea 63 75 74 65 64 20 74 6f 20 69 6e 73 65 72 74 20 cuted to insert
1b8eb 61 20 72 6f 77 20 69 6e 20 74 6f 20 74 68 65 20 a row in to the
1b8ec 70 73 65 75 64 6f 20 74 61 62 6c 65 2c 0a 2a 2a pseudo table,.**
1b8ed 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 6c the pseudo-tabl
1b8ee 65 20 63 75 72 73 6f 72 20 6d 61 79 20 6f 72 20 e cursor may or
1b8ef 6d 61 79 20 6e 6f 74 20 6d 61 6b 65 20 69 74 27 may not make it'
1b8f0 73 20 6f 77 6e 20 63 6f 70 79 20 6f 66 20 74 68 s own copy of th
1b8f1 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 72 6f e.** original ro
1b8f2 77 20 64 61 74 61 2e 20 49 66 20 50 32 20 69 73 w data. If P2 is
1b8f3 20 30 2c 20 74 68 65 6e 20 74 68 65 20 70 73 65 0, then the pse
1b8f4 75 64 6f 2d 74 61 62 6c 65 20 77 69 6c 6c 20 63 udo-table will c
1b8f5 6f 70 79 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 opy the.** origi
1b8f6 6e 61 6c 20 72 6f 77 20 64 61 74 61 2e 20 4f 74 nal row data. Ot
1b8f7 68 65 72 77 69 73 65 2c 20 61 20 70 6f 69 6e 74 herwise, a point
1b8f8 65 72 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e er to the origin
1b8f9 61 6c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 0a 2a al memory cell.*
1b8fa 2a 20 69 73 20 73 74 6f 72 65 64 2e 20 49 6e 20 * is stored. In
1b8fb 74 68 69 73 20 63 61 73 65 2c 20 74 68 65 20 76 this case, the v
1b8fc 64 62 65 20 70 72 6f 67 72 61 6d 20 6d 75 73 74 dbe program must
1b8fd 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 ensure that the
1b8fe 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 6c 6c .** memory cell
1b8ff 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
1b900 72 6f 77 20 64 61 74 61 20 69 73 20 6e 6f 74 20 row data is not
1b901 6f 76 65 72 77 72 69 74 74 65 6e 20 75 6e 74 69 overwritten unti
1b902 6c 20 74 68 65 0a 2a 2a 20 70 73 65 75 64 6f 20 l the.** pseudo
1b903 74 61 62 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 table is closed
1b904 28 6f 72 20 61 20 6e 65 77 20 72 6f 77 20 69 73 (or a new row is
1b905 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69 inserted into i
1b906 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f t)..*/.case OP_O
1b907 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a 20 20 69 penPseudo: {. i
1b908 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a nt i = pOp->p1;.
1b909 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1b90a 78 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d x;. assert( i>=
1b90b 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 61 6c 6c 0 );. pCx = all
1b90c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 69 ocateCursor(p, i
1b90d 2c 20 26 70 4f 70 5b 2d 31 5d 2c 20 2d 31 2c 20 , &pOp[-1], -1,
1b90e 30 29 3b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 0);. if( pCx==0
1b90f 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1b910 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d pCx->nullRow =
1b911 20 31 3b 0a 20 20 70 43 78 2d 3e 70 73 65 75 64 1;. pCx->pseud
1b912 6f 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 70 43 oTable = 1;. pC
1b913 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 x->ephemPseudoTa
1b914 62 6c 65 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70 ble = (u8)pOp->p
1b915 32 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 2;. pCx->isTabl
1b916 65 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 69 73 e = 1;. pCx->is
1b917 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62 72 65 Index = 0;. bre
1b918 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1b919 3a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a : Close P1 * * *
1b91a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 *.**.** Close a
1b91b 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 cursor previous
1b91c 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50 31 2e ly opened as P1.
1b91d 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74 0a 2a If P1 is not.*
1b91e 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e * currently open
1b91f 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 , this instructi
1b920 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a on is a no-op..*
1b921 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73 65 3a /.case OP_Close:
1b922 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 {. int i = pOp
1b923 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 ->p1;. assert(
1b924 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 i>=0 && i<p->nCu
1b925 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 rsor );. sqlite
1b926 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 3VdbeFreeCursor(
1b927 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 3b p, p->apCsr[i]);
1b928 0a 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d . p->apCsr[i] =
1b929 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0;. break;.}..
1b92a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 /* Opcode: SeekG
1b92b 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a e P1 P2 P3 P4 *.
1b92c 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 **.** If cursor
1b92d 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 P1 refers to an
1b92e 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 SQL table (B-Tre
1b92f 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 e that uses inte
1b930 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 ger keys), .** u
1b931 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 se the value in
1b932 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 74 register P3 as t
1b933 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75 72 73 he key. If curs
1b934 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a or P1 refers .**
1b935 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 to an SQL index
1b936 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 , then P3 is the
1b937 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 first in an arr
1b938 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 ay of P4 registe
1b939 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 rs .** that are
1b93a 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 used as an unpac
1b93b 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a ked index key. .
1b93c 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e **.** Reposition
1b93d 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 cursor P1 so th
1b93e 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f at it points to
1b93f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e the smallest en
1b940 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 try that .** is
1b941 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
1b942 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 equal to the key
1b943 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 value. If there
1b944 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 are no records
1b945 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e .** greater than
1b946 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
1b947 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e key and P2 is n
1b948 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 ot zero, then ju
1b949 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 mp to P2..**.**
1b94a 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c See also: Found,
1b94b 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 NotFound, Disti
1b94c 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 nct, SeekLt, See
1b94d 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f kGt, SeekLe.*/./
1b94e 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 74 * Opcode: SeekGt
1b94f 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1b950 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 *.** If cursor P
1b951 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 1 refers to an S
1b952 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 QL table (B-Tree
1b953 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 that uses integ
1b954 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 er keys), .** us
1b955 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 e the value in r
1b956 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20 egister P3 as a
1b957 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 key. If cursor P
1b958 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 1 refers .** to
1b959 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 an SQL index, th
1b95a 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72 en P3 is the fir
1b95b 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f st in an array o
1b95c 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a f P4 registers .
1b95d 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 ** that are used
1b95e 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 as an unpacked
1b95f 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a index key. .**.*
1b960 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 * Reposition cur
1b961 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 20 sor P1 so that
1b962 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 it points to the
1b963 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 smallest entry
1b964 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 that .** is grea
1b965 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b 65 79 ter than the key
1b966 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 value. If there
1b967 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 are no records
1b968 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a 2a 2a greater than .**
1b969 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 the key and P2
1b96a 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 is not zero, the
1b96b 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a n jump to P2..**
1b96c 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f .** See also: Fo
1b96d 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 und, NotFound, D
1b96e 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c istinct, SeekLt,
1b96f 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a SeekGe, SeekLe.
1b970 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 */./* Opcode: Se
1b971 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20 50 34 ekLt P1 P2 P3 P4
1b972 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 * .**.** If cur
1b973 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f sor P1 refers to
1b974 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 an SQL table (B
1b975 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 -Tree that uses
1b976 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a integer keys), .
1b977 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 ** use the value
1b978 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 in register P3
1b979 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 as a key. If cur
1b97a 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a sor P1 refers .*
1b97b 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 * to an SQL inde
1b97c 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 x, then P3 is th
1b97d 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 e first in an ar
1b97e 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 ray of P4 regist
1b97f 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 ers .** that are
1b980 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 used as an unpa
1b981 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 cked index key.
1b982 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f .**.** Repositio
1b983 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 n cursor P1 so t
1b984 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 hat it points t
1b985 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e o the largest en
1b986 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 try that .** is
1b987 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 less than the ke
1b988 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 y value. If ther
1b989 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 e are no records
1b98a 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 less than .** t
1b98b 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 he key and P2 is
1b98c 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 not zero, then
1b98d 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a jump to P2..**.*
1b98e 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e * See also: Foun
1b98f 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 d, NotFound, Dis
1b990 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 tinct, SeekGt, S
1b991 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f eekGe, SeekLe.*/
1b992 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b ./* Opcode: Seek
1b993 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a Le P1 P2 P3 P4 *
1b994 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 .**.** If cursor
1b995 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e P1 refers to an
1b996 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 SQL table (B-Tr
1b997 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 ee that uses int
1b998 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 eger keys), .**
1b999 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e use the value in
1b99a 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 register P3 as
1b99b 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 a key. If cursor
1b99c 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 P1 refers .** t
1b99d 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 o an SQL index,
1b99e 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 then P3 is the f
1b99f 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 irst in an array
1b9a0 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 of P4 registers
1b9a1 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 .** that are us
1b9a2 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 ed as an unpacke
1b9a3 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a d index key. .**
1b9a4 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 .** Reposition c
1b9a5 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 ursor P1 so that
1b9a6 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 it points to th
1b9a7 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 e largest entry
1b9a8 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 that .** is less
1b9a9 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
1b9aa 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e o the key value.
1b9ab 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
1b9ac 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c 65 73 records .** les
1b9ad 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 s than or equal
1b9ae 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 to the key and P
1b9af 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 2 is not zero, t
1b9b0 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a hen jump to P2..
1b9b1 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1b9b2 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c Found, NotFound,
1b9b3 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 Distinct, SeekG
1b9b4 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c t, SeekGe, SeekL
1b9b5 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 t.*/.case OP_See
1b9b6 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 kLt: /*
1b9b7 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 jump, in3 */.cas
1b9b8 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20 20 20 e OP_SeekLe:
1b9b9 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1b9ba 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 3 */.case OP_See
1b9bb 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 kGe: /*
1b9bc 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 jump, in3 */.cas
1b9bd 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b 20 20 e OP_SeekGt: {
1b9be 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1b9bf 33 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 3 */. int i = p
1b9c0 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 Op->p1;. VdbeCu
1b9c1 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73 rsor *pC;.. ass
1b9c2 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 ert( i>=0 && i<p
1b9c3 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 ->nCursor );. a
1b9c4 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 21 3d ssert( pOp->p2!=
1b9c5 30 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 0 );. pC = p->a
1b9c6 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 pCsr[i];. asser
1b9c7 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 t( pC!=0 );. if
1b9c8 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 ( pC->pCursor!=0
1b9c9 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c ){. int res,
1b9ca 20 6f 63 3b 0a 20 20 20 20 6f 63 20 3d 20 70 4f oc;. oc = pO
1b9cb 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 70 p->opcode;. p
1b9cc 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a C->nullRow = 0;.
1b9cd 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73 54 61 if( pC->isTa
1b9ce 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 36 34 ble ){. i64
1b9cf 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 iKey; /* T
1b9d0 68 65 20 72 6f 77 69 64 20 77 65 20 61 72 65 20 he rowid we are
1b9d1 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a 0a 20 to seek to */..
1b9d2 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 /* The inpu
1b9d3 74 20 76 61 6c 75 65 20 69 6e 20 50 33 20 6d 69 t value in P3 mi
1b9d4 67 68 74 20 62 65 20 6f 66 20 61 6e 79 20 74 79 ght be of any ty
1b9d5 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 72 65 61 pe: integer, rea
1b9d6 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20 20 20 l, string,.
1b9d7 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e 55 4c ** blob, or NUL
1b9d8 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 65 64 73 L. But it needs
1b9d9 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 to be an intege
1b9da 72 20 62 65 66 6f 72 65 20 77 65 20 63 61 6e 20 r before we can
1b9db 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 do. ** the
1b9dc 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72 74 20 seek, so covert
1b9dd 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 61 70 70 it. */. app
1b9de 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 lyNumericAffinit
1b9df 79 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 69 y(pIn3);. i
1b9e0 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 Key = sqlite3Vdb
1b9e1 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b eIntValue(pIn3);
1b9e2 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 . pC->rowid
1b9e3 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20 IsValid = 0;..
1b9e4 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 33 /* If the P3
1b9e5 20 76 61 6c 75 65 20 63 6f 75 6c 64 20 6e 6f 74 value could not
1b9e6 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e be converted in
1b9e7 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 77 69 to an integer wi
1b9e8 74 68 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20 6c thout. ** l
1b9e9 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 oss of informati
1b9ea 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63 69 61 6c on, then special
1b9eb 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 72 processing is r
1b9ec 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f 0a 20 20 equired... */.
1b9ed 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 if( (pIn3->f
1b9ee 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d lags & MEM_Int)=
1b9ef 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 =0 ){. if
1b9f0 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 ( (pIn3->flags &
1b9f1 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b MEM_Real)==0 ){
1b9f2 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 . /* If
1b9f3 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 63 61 the P3 value ca
1b9f4 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 nnot be converte
1b9f5 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69 6e 64 20 d into any kind
1b9f6 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a 20 20 20 of a number,.
1b9f7 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 ** then t
1b9f8 68 65 20 73 65 65 6b 20 69 73 20 6e 6f 74 20 70 he seek is not p
1b9f9 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a 75 6d 70 ossible, so jump
1b9fa 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 20 20 to P2 */.
1b9fb 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1b9fc 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 - 1;.
1b9fd 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
1b9fe 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 . /* If w
1b9ff 65 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 e reach this poi
1ba00 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 50 33 20 nt, then the P3
1ba01 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 61 20 value must be a
1ba02 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20 20 20 20 floating.
1ba03 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 ** point number
1ba04 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 . */. ass
1ba05 65 72 74 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 ert( (pIn3->flag
1ba06 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 21 3d 30 s & MEM_Real)!=0
1ba07 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 );.. if(
1ba08 20 69 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53 54 5f iKey==SMALLEST_
1ba09 49 4e 54 36 34 20 26 26 20 28 70 49 6e 33 2d 3e INT64 && (pIn3->
1ba0a 72 3c 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 7c r<(double)iKey |
1ba0b 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29 7b 0a | pIn3->r>0) ){.
1ba0c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1ba0d 20 50 33 20 76 61 6c 75 65 20 69 73 20 74 6f 20 P3 value is to
1ba0e 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e 69 74 75 large in magnitu
1ba0f 64 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 de to be express
1ba10 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20 20 ed as an.
1ba11 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 2a ** integer. *
1ba12 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 20 /. res
1ba13 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 69 = 1;. i
1ba14 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20 29 7b 0a f( pIn3->r<0 ){.
1ba15 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
1ba16 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c oc==OP_SeekGt ||
1ba17 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 oc==OP_SeekGe )
1ba18 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1ba19 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1ba1a 65 46 69 72 73 74 28 70 43 2d 3e 70 43 75 72 73 eFirst(pC->pCurs
1ba1b 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 or, &res);.
1ba1c 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 if( rc!
1ba1d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
1ba1e 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1ba1f 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 rror;.
1ba20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 }. }e
1ba21 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
1ba22 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b if( oc==OP_Seek
1ba23 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65 Lt || oc==OP_See
1ba24 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 kLe ){.
1ba25 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1ba26 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d 3e 70 3BtreeLast(pC->p
1ba27 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 Cursor, &res);.
1ba28 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 if(
1ba29 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1ba2a 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1ba2b 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 to_error;.
1ba2c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1ba2d 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 }. if
1ba2e 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 ( res ){.
1ba2f 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1ba30 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 2 - 1;.
1ba31 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 }. bre
1ba32 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 ak;. }els
1ba33 65 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 e if( oc==OP_See
1ba34 6b 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 kLt || oc==OP_Se
1ba35 65 6b 47 65 20 29 7b 0a 20 20 20 20 20 20 20 20 ekGe ){.
1ba36 20 20 2f 2a 20 55 73 65 20 74 68 65 20 63 65 69 /* Use the cei
1ba37 6c 69 6e 67 28 29 20 66 75 6e 63 74 69 6f 6e 20 ling() function
1ba38 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d to convert real-
1ba39 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 >int */.
1ba3a 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 3e 20 if( pIn3->r >
1ba3b 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29 20 69 (double)iKey ) i
1ba3c 4b 65 79 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d Key++;. }
1ba3d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
1ba3e 2f 2a 20 55 73 65 20 74 68 65 20 66 6c 6f 6f 72 /* Use the floor
1ba3f 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 () function to c
1ba40 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74 onvert real->int
1ba41 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 */. as
1ba42 73 65 72 74 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 sert( oc==OP_See
1ba43 6b 4c 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 kLe || oc==OP_Se
1ba44 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 20 20 ekGt );.
1ba45 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 3c 20 if( pIn3->r <
1ba46 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29 20 69 (double)iKey ) i
1ba47 4b 65 79 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d Key--;. }
1ba48 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 . } .
1ba49 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1ba4a 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
1ba4b 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 pC->pCursor, 0,
1ba4c 28 75 36 34 29 69 4b 65 79 2c 20 30 2c 20 26 72 (u64)iKey, 0, &r
1ba4d 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 es);. if( r
1ba4e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1ba4f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f goto abo
1ba50 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1ba51 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
1ba52 66 28 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 f( res==0 ){.
1ba53 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 pC->rowidIs
1ba54 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 Valid = 1;.
1ba55 20 20 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 pC->lastRowid
1ba56 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 20 20 7d = iKey;. }
1ba57 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1ba58 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1ba59 20 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 r;. int nF
1ba5a 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 ield = pOp->p4.i
1ba5b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ba5c 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1ba5d 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20 20 61 INT32 );. a
1ba5e 73 73 65 72 74 28 20 6e 46 69 65 6c 64 3e 30 20 ssert( nField>0
1ba5f 29 3b 0a 20 20 20 20 20 20 72 2e 70 4b 65 79 49 );. r.pKeyI
1ba60 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e nfo = pC->pKeyIn
1ba61 66 6f 3b 0a 20 20 20 20 20 20 72 2e 6e 46 69 65 fo;. r.nFie
1ba62 6c 64 20 3d 20 28 75 31 36 29 6e 46 69 65 6c 64 ld = (u16)nField
1ba63 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 63 3d 3d ;. if( oc==
1ba64 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 6f 63 3d OP_SeekGt || oc=
1ba65 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 20 =OP_SeekLe ){.
1ba66 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 r.flags =
1ba67 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 UNPACKED_INCRKEY
1ba68 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1ba69 20 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d r.flags =
1ba6a 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
1ba6b 20 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 r.aMem = &p->a
1ba6c 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
1ba6d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ba6e 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
1ba6f 6b 65 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c ked(pC->pCursor,
1ba70 20 26 72 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 &r, 0, 0, &res)
1ba71 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
1ba72 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1ba73 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f goto abort_
1ba74 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1ba75 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 2d 3e }. pC->
1ba76 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
1ba77 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e ;. }. pC->
1ba78 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d deferredMoveto =
1ba79 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 0;. pC->cach
1ba7a 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1ba7b 53 54 41 4c 45 3b 0a 23 69 66 64 65 66 20 53 51 STALE;.#ifdef SQ
1ba7c 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 LITE_TEST. sq
1ba7d 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 lite3_search_cou
1ba7e 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 nt++;.#endif.
1ba7f 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b if( oc==OP_Seek
1ba80 47 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65 Ge || oc==OP_See
1ba81 6b 47 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 kGt ){. if(
1ba82 20 72 65 73 3c 30 20 7c 7c 20 28 72 65 73 3d 3d res<0 || (res==
1ba83 30 20 26 26 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 0 && oc==OP_Seek
1ba84 47 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 Gt) ){. r
1ba85 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1ba86 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73 6f 72 Next(pC->pCursor
1ba87 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 , &res);.
1ba88 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1ba89 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f OK ) goto abort_
1ba8a 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1ba8b 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 pC->rowidI
1ba8c 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 sValid = 0;.
1ba8d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1ba8e 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 res = 0;.
1ba8f 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1ba90 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d 3d 4f assert( oc==O
1ba91 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 6f 63 3d 3d P_SeekLt || oc==
1ba92 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20 OP_SeekLe );.
1ba93 20 20 20 69 66 28 20 72 65 73 3e 30 20 7c 7c 20 if( res>0 ||
1ba94 28 72 65 73 3d 3d 30 20 26 26 20 6f 63 3d 3d 4f (res==0 && oc==O
1ba95 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a 20 20 20 P_SeekLt) ){.
1ba96 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1ba97 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 3BtreePrevious(p
1ba98 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 C->pCursor, &res
1ba99 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
1ba9a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 c!=SQLITE_OK ) g
1ba9b 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1ba9c 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 _error;.
1ba9d 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1ba9e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 = 0;. }els
1ba9f 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 72 65 e{. /* re
1baa0 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67 61 74 s might be negat
1baa1 69 76 65 20 62 65 63 61 75 73 65 20 74 68 65 20 ive because the
1baa2 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 20 table is empty.
1baa3 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20 20 20 Check to.
1baa4 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68 69 73 ** see if this
1baa5 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 is the case..
1baa6 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
1baa7 20 72 65 73 20 3d 20 73 71 6c 69 74 65 33 42 74 res = sqlite3Bt
1baa8 72 65 65 45 6f 66 28 70 43 2d 3e 70 43 75 72 73 reeEof(pC->pCurs
1baa9 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 or);. }.
1baaa 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
1baab 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 Op->p2>0 );.
1baac 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 if( res ){.
1baad 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1baae 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 1;. }. }else
1baaf 20 69 66 28 20 21 70 43 2d 3e 70 73 65 75 64 6f if( !pC->pseudo
1bab0 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 Table ){. /*
1bab1 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 This happens whe
1bab2 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 n attempting to
1bab3 6f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 open the sqlite3
1bab4 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 _master table.
1bab5 20 20 2a 2a 20 66 6f 72 20 72 65 61 64 20 61 63 ** for read ac
1bab6 63 65 73 73 20 72 65 74 75 72 6e 73 20 53 51 4c cess returns SQL
1bab7 49 54 45 5f 45 4d 50 54 59 2e 20 49 6e 20 74 68 ITE_EMPTY. In th
1bab8 69 73 20 63 61 73 65 20 61 6c 77 61 79 73 0a 20 is case always.
1bab9 20 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a ** take the j
1baba 75 6d 70 20 28 73 69 6e 63 65 20 74 68 65 72 65 ump (since there
1babb 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 are no records
1babc 69 6e 20 74 68 65 20 74 61 62 6c 65 29 2e 0a 20 in the table)..
1babd 20 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 */. pc = p
1babe 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a Op->p2 - 1;. }.
1babf 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1bac0 70 63 6f 64 65 3a 20 53 65 65 6b 20 50 31 20 50 pcode: Seek P1 P
1bac1 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 2 * * *.**.** P1
1bac2 20 69 73 20 61 6e 20 6f 70 65 6e 20 74 61 62 6c is an open tabl
1bac3 65 20 63 75 72 73 6f 72 20 61 6e 64 20 50 32 20 e cursor and P2
1bac4 69 73 20 61 20 72 6f 77 69 64 20 69 6e 74 65 67 is a rowid integ
1bac5 65 72 2e 20 20 41 72 72 61 6e 67 65 0a 2a 2a 20 er. Arrange.**
1bac6 66 6f 72 20 50 31 20 74 6f 20 6d 6f 76 65 20 73 for P1 to move s
1bac7 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 o that it points
1bac8 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 67 69 to the rowid gi
1bac9 76 65 6e 20 62 79 20 50 32 2e 0a 2a 2a 0a 2a 2a ven by P2..**.**
1baca 20 54 68 69 73 20 69 73 20 61 63 74 75 61 6c 6c This is actuall
1bacb 79 20 61 20 64 65 66 65 72 72 65 64 20 73 65 65 y a deferred see
1bacc 6b 2e 20 20 4e 6f 74 68 69 6e 67 20 61 63 74 75 k. Nothing actu
1bacd 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 75 6e 74 ally happens unt
1bace 69 6c 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 il.** the cursor
1bacf 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 is used to read
1bad0 20 61 20 72 65 63 6f 72 64 2e 20 20 54 68 61 74 a record. That
1bad1 20 77 61 79 2c 20 69 66 20 6e 6f 20 72 65 61 64 way, if no read
1bad2 73 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e 6f 20 75 s.** occur, no u
1bad3 6e 6e 65 63 65 73 73 61 72 79 20 49 2f 4f 20 68 nnecessary I/O h
1bad4 61 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 73 65 20 appens..*/.case
1bad5 4f 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20 2f 2a OP_Seek: { /*
1bad6 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20 69 20 in2 */. int i
1bad7 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 = pOp->p1;. Vdb
1bad8 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 eCursor *pC;..
1bad9 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
1bada 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a i<p->nCursor );.
1badb 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b pC = p->apCsr[
1badc 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 i];. assert( pC
1badd 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43 2d !=0 );. if( pC-
1bade 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 >pCursor!=0 ){.
1badf 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 assert( pC->i
1bae0 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 43 sTable );. pC
1bae1 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 ->nullRow = 0;.
1bae2 20 20 20 70 43 2d 3e 6d 6f 76 65 74 6f 54 61 72 pC->movetoTar
1bae3 67 65 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 get = sqlite3Vdb
1bae4 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b eIntValue(pIn2);
1bae5 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 . pC->rowidIs
1bae6 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 70 Valid = 0;. p
1bae7 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1bae8 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 o = 1;. }. bre
1bae9 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63 ak;.}. ../* Opc
1baea 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 20 50 32 ode: Found P1 P2
1baeb 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 P3 * *.**.** Re
1baec 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 gister P3 holds
1baed 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 a blob construct
1baee 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 ed by MakeRecord
1baef 2e 20 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 . P1 is an inde
1baf0 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 6e 74 72 x..** If an entr
1baf1 79 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 y that matches t
1baf2 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1baf3 73 74 65 72 20 70 33 20 65 78 69 73 74 73 20 69 ster p3 exists i
1baf4 6e 20 50 31 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d n P1 then.** jum
1baf5 70 20 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65 p to P2. If the
1baf6 20 50 33 20 76 61 6c 75 65 20 64 6f 65 73 20 6e P3 value does n
1baf7 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 65 6e 74 ot match any ent
1baf8 72 79 20 69 6e 20 50 31 0a 2a 2a 20 74 68 65 6e ry in P1.** then
1baf9 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 68 65 fall thru. The
1bafa 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 6c 65 P1 cursor is le
1bafb 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 ft pointing at t
1bafc 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 he matching entr
1bafd 79 0a 2a 2a 20 69 66 20 69 74 20 65 78 69 73 74 y.** if it exist
1bafe 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e s..**.** This in
1baff 73 74 72 75 63 74 69 6f 6e 20 69 73 20 75 73 65 struction is use
1bb00 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 d to implement t
1bb01 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 he IN operator w
1bb02 68 65 72 65 20 74 68 65 0a 2a 2a 20 6c 65 66 74 here the.** left
1bb03 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 -hand side is a
1bb04 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1bb05 2e 20 20 50 31 20 6d 61 79 20 62 65 20 61 20 74 . P1 may be a t
1bb06 72 75 65 20 69 6e 64 65 78 2c 20 6f 72 20 69 74 rue index, or it
1bb07 0a 2a 2a 20 6d 61 79 20 62 65 20 61 20 74 65 6d .** may be a tem
1bb08 70 6f 72 61 72 79 20 69 6e 64 65 78 20 74 68 61 porary index tha
1bb09 74 20 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75 t holds the resu
1bb0a 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 lts of the SELEC
1bb0b 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 T.** statement.
1bb0c 20 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 This instructi
1bb0d 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 on is also used
1bb0e 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1bb0f 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20 6b 65 79 .** DISTINCT key
1bb10 77 6f 72 64 20 69 6e 20 53 45 4c 45 43 54 20 73 word in SELECT s
1bb11 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a tatements..**.**
1bb12 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1bb13 6e 20 63 68 65 63 6b 73 20 69 66 20 69 6e 64 65 n checks if inde
1bb14 78 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 20 x P1 contains a
1bb15 72 65 63 6f 72 64 20 66 6f 72 20 77 68 69 63 68 record for which
1bb16 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 4e .** the first N
1bb17 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 serialized valu
1bb18 65 73 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 es exactly match
1bb19 20 74 68 65 20 4e 20 73 65 72 69 61 6c 69 7a 65 the N serialize
1bb1a 64 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 74 d values.** in t
1bb1b 68 65 20 72 65 63 6f 72 64 20 69 6e 20 72 65 67 he record in reg
1bb1c 69 73 74 65 72 20 50 33 2c 20 77 68 65 72 65 20 ister P3, where
1bb1d 4e 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e N is the total n
1bb1e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 umber of values
1bb1f 69 6e 0a 2a 2a 20 74 68 65 20 50 33 20 72 65 63 in.** the P3 rec
1bb20 6f 72 64 20 28 74 68 65 20 50 33 20 72 65 63 6f ord (the P3 reco
1bb21 72 64 20 69 73 20 61 20 70 72 65 66 69 78 20 6f rd is a prefix o
1bb22 66 20 74 68 65 20 50 31 20 72 65 63 6f 72 64 29 f the P1 record)
1bb23 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 . .**.** See als
1bb24 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 o: NotFound, IsU
1bb25 6e 69 71 75 65 2c 20 4e 6f 74 45 78 69 73 74 73 nique, NotExists
1bb26 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e .*/./* Opcode: N
1bb27 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33 otFound P1 P2 P3
1bb28 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 * *.**.** Regis
1bb29 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62 ter P3 holds a b
1bb2a 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 lob constructed
1bb2b 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20 by MakeRecord.
1bb2c 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 P1 is.** an inde
1bb2d 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74 72 79 20 x. If no entry
1bb2e 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 61 exists in P1 tha
1bb2f 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 62 6c t matches the bl
1bb30 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a 2a 2a 20 ob then jump.**
1bb31 74 6f 20 50 32 2e 20 20 49 66 20 61 6e 20 65 6e to P2. If an en
1bb32 74 72 79 20 64 6f 65 73 20 65 78 69 73 74 69 6e try does existin
1bb33 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e g, fall through.
1bb34 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 The cursor is
1bb35 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 left.** pointing
1bb36 20 74 6f 20 74 68 65 20 65 6e 74 72 79 20 74 68 to the entry th
1bb37 61 74 20 6d 61 74 63 68 65 73 2e 0a 2a 2a 0a 2a at matches..**.*
1bb38 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e * See also: Foun
1bb39 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 49 73 d, NotExists, Is
1bb3a 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f Unique.*/.case O
1bb3b 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 20 20 P_NotFound:
1bb3c 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1bb3d 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e 64 3a /.case OP_Found:
1bb3e 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1bb3f 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 p, in3 */. int
1bb40 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 i = pOp->p1;. i
1bb41 6e 74 20 61 6c 72 65 61 64 79 45 78 69 73 74 73 nt alreadyExists
1bb42 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75 72 73 = 0;. VdbeCurs
1bb43 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 72 74 or *pC;. assert
1bb44 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e ( i>=0 && i<p->n
1bb45 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 Cursor );. asse
1bb46 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 rt( p->apCsr[i]!
1bb47 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 20 =0 );. if( (pC
1bb48 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e = p->apCsr[i])->
1bb49 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 pCursor!=0 ){.
1bb4a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 55 int res;. U
1bb4b 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 npackedRecord *p
1bb4c 49 64 78 4b 65 79 3b 0a 0a 20 20 20 20 61 73 73 IdxKey;.. ass
1bb4d 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 ert( pC->isTable
1bb4e 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
1bb4f 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 t( pIn3->flags &
1bb50 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 MEM_Blob );.
1bb51 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 pIdxKey = sqlit
1bb52 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 e3VdbeRecordUnpa
1bb53 63 6b 28 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c ck(pC->pKeyInfo,
1bb54 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33 2d 3e pIn3->n, pIn3->
1bb55 7a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 z,.
1bb56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bb57 20 20 20 20 20 20 20 20 20 61 54 65 6d 70 52 65 aTempRe
1bb58 63 2c 20 73 69 7a 65 6f 66 28 61 54 65 6d 70 52 c, sizeof(aTempR
1bb59 65 63 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 ec));. if( pI
1bb5a 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 dxKey==0 ){.
1bb5b 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1bb5c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70 }. if( pOp
1bb5d 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 ->opcode==OP_Fou
1bb5e 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 49 64 78 nd ){. pIdx
1bb5f 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 55 4e Key->flags |= UN
1bb60 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 PACKED_PREFIX_MA
1bb61 54 43 48 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 TCH;. }. r
1bb62 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1bb63 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 MovetoUnpacked(p
1bb64 43 2d 3e 70 43 75 72 73 6f 72 2c 20 70 49 64 78 C->pCursor, pIdx
1bb65 4b 65 79 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 Key, 0, 0, &res)
1bb66 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1bb67 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 eDeleteUnpackedR
1bb68 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a ecord(pIdxKey);.
1bb69 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1bb6a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 62 TE_OK ){. b
1bb6b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1bb6c 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 3d 20 alreadyExists =
1bb6d 28 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20 70 43 (res==0);. pC
1bb6e 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
1bb6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 = 0;. pC->ca
1bb70 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1bb71 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 69 E_STALE;. }. i
1bb72 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
1bb73 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20 OP_Found ){.
1bb74 69 66 28 20 61 6c 72 65 61 64 79 45 78 69 73 74 if( alreadyExist
1bb75 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 s ) pc = pOp->p2
1bb76 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 - 1;. }else{.
1bb77 20 20 20 69 66 28 20 21 61 6c 72 65 61 64 79 45 if( !alreadyE
1bb78 78 69 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 xists ) pc = pOp
1bb79 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 ->p2 - 1;. }.
1bb7a 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1bb7b 6f 64 65 3a 20 49 73 55 6e 69 71 75 65 20 50 31 ode: IsUnique P1
1bb7c 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1bb7d 2a 20 54 68 65 20 50 33 20 72 65 67 69 73 74 65 * The P3 registe
1bb7e 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6e r contains an in
1bb7f 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d teger record num
1bb80 62 65 72 2e 20 20 43 61 6c 6c 20 74 68 69 73 0a ber. Call this.
1bb81 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 ** record number
1bb82 20 52 2e 20 20 54 68 65 20 50 34 20 72 65 67 69 R. The P4 regi
1bb83 73 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e ster contains an
1bb84 20 69 6e 64 65 78 20 6b 65 79 20 63 72 65 61 74 index key creat
1bb85 65 64 0a 2a 2a 20 75 73 69 6e 67 20 4d 61 6b 65 ed.** using Make
1bb86 52 65 63 6f 72 64 2e 20 20 43 61 6c 6c 20 69 74 Record. Call it
1bb87 20 4b 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 K..**.** P1 is
1bb88 61 6e 20 69 6e 64 65 78 2e 20 20 53 6f 20 69 74 an index. So it
1bb89 20 68 61 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 has no data and
1bb8a 20 69 74 73 20 6b 65 79 20 63 6f 6e 73 69 73 74 its key consist
1bb8b 73 20 6f 66 20 61 0a 2a 2a 20 72 65 63 6f 72 64 s of a.** record
1bb8c 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 4f 50 generated by OP
1bb8d 5f 4d 61 6b 65 52 65 63 6f 72 64 20 77 68 65 72 _MakeRecord wher
1bb8e 65 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 e the last field
1bb8f 20 69 73 20 74 68 65 20 0a 2a 2a 20 72 6f 77 69 is the .** rowi
1bb90 64 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 74 d of the entry t
1bb91 68 61 74 20 74 68 65 20 69 6e 64 65 78 20 72 65 hat the index re
1bb92 66 65 72 73 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 fers to..** .**
1bb93 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1bb94 20 61 73 6b 73 20 69 66 20 74 68 65 72 65 20 69 asks if there i
1bb95 73 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 50 31 s an entry in P1
1bb96 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 66 69 where the.** fi
1bb97 65 6c 64 73 20 6d 61 74 63 68 65 73 20 4b 20 62 elds matches K b
1bb98 75 74 20 74 68 65 20 72 6f 77 69 64 20 69 73 20 ut the rowid is
1bb99 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 52 different from R
1bb9a 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 ..** If there is
1bb9b 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 no such entry,
1bb9c 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6e then there is an
1bb9d 20 69 6d 6d 65 64 69 61 74 65 0a 2a 2a 20 6a 75 immediate.** ju
1bb9e 6d 70 20 74 6f 20 50 32 2e 20 20 49 66 20 61 6e mp to P2. If an
1bb9f 79 20 65 6e 74 72 79 20 64 6f 65 73 20 65 78 69 y entry does exi
1bba0 73 74 20 77 68 65 72 65 20 74 68 65 20 69 6e 64 st where the ind
1bba1 65 78 20 73 74 72 69 6e 67 0a 2a 2a 20 6d 61 74 ex string.** mat
1bba2 63 68 65 73 20 4b 20 62 75 74 20 74 68 65 20 72 ches K but the r
1bba3 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 ecord number is
1bba4 6e 6f 74 20 52 2c 20 74 68 65 6e 20 74 68 65 20 not R, then the
1bba5 72 65 63 6f 72 64 0a 2a 2a 20 6e 75 6d 62 65 72 record.** number
1bba6 20 66 6f 72 20 74 68 61 74 20 65 6e 74 72 79 20 for that entry
1bba7 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
1bba8 50 33 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 0a 2a P3 and control.*
1bba9 2a 20 66 61 6c 6c 73 20 74 68 72 6f 75 67 68 20 * falls through
1bbaa 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 to the next inst
1bbab 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 ruction..**.** S
1bbac 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e ee also: NotFoun
1bbad 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 46 6f d, NotExists, Fo
1bbae 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 und.*/.case OP_I
1bbaf 73 55 6e 69 71 75 65 3a 20 7b 20 20 20 20 20 20 sUnique: {
1bbb0 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1bbb1 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d /. int i = pOp-
1bbb2 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f >p1;. VdbeCurso
1bbb3 72 20 2a 70 43 78 3b 0a 20 20 42 74 43 75 72 73 r *pCx;. BtCurs
1bbb4 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 4d 65 6d or *pCrsr;. Mem
1bbb5 20 2a 70 4b 3b 0a 20 20 69 36 34 20 52 3b 0a 0a *pK;. i64 R;..
1bbb6 20 20 2f 2a 20 50 6f 70 20 74 68 65 20 76 61 6c /* Pop the val
1bbb7 75 65 20 52 20 6f 66 66 20 74 68 65 20 74 6f 70 ue R off the top
1bbb8 20 6f 66 20 74 68 65 20 73 74 61 63 6b 0a 20 20 of the stack.
1bbb9 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1bbba 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 ->p4type==P4_INT
1bbbb 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 32 );. assert(
1bbbc 70 4f 70 2d 3e 70 34 2e 69 3e 30 20 26 26 20 70 pOp->p4.i>0 && p
1bbbd 4f 70 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d 65 Op->p4.i<=p->nMe
1bbbe 6d 20 29 3b 0a 20 20 70 4b 20 3d 20 26 70 2d 3e m );. pK = &p->
1bbbf 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e 69 5d 3b aMem[pOp->p4.i];
1bbc0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1bbc1 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33 mIntegerify(pIn3
1bbc2 29 3b 0a 20 20 52 20 3d 20 70 49 6e 33 2d 3e 75 );. R = pIn3->u
1bbc3 2e 69 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e .i;. assert( i>
1bbc4 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 =0 && i<p->nCurs
1bbc5 6f 72 20 29 3b 0a 20 20 70 43 78 20 3d 20 70 2d or );. pCx = p-
1bbc6 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 >apCsr[i];. ass
1bbc7 65 72 74 28 20 70 43 78 21 3d 30 20 29 3b 0a 20 ert( pCx!=0 );.
1bbc8 20 70 43 72 73 72 20 3d 20 70 43 78 2d 3e 70 43 pCrsr = pCx->pC
1bbc9 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 72 ursor;. if( pCr
1bbca 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 sr!=0 ){. int
1bbcb 20 72 65 73 3b 0a 20 20 20 20 69 36 34 20 76 3b res;. i64 v;
1bbcc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bbcd 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f /* The reco
1bbce 72 64 20 6e 75 6d 62 65 72 20 74 68 61 74 20 6d rd number that m
1bbcf 61 74 63 68 65 73 20 4b 20 2a 2f 0a 20 20 20 20 atches K */.
1bbd0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
1bbd1 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20 55 6e pIdxKey; /* Un
1bbd2 70 61 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f packed version o
1bbd3 66 20 50 34 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 f P4 */.. /*
1bbd4 4d 61 6b 65 20 73 75 72 65 20 4b 20 69 73 20 61 Make sure K is a
1bbd5 20 73 74 72 69 6e 67 20 61 6e 64 20 6d 61 6b 65 string and make
1bbd6 20 7a 4b 65 79 20 70 6f 69 6e 74 20 74 6f 20 4b zKey point to K
1bbd7 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
1bbd8 72 74 28 20 70 4b 2d 3e 66 6c 61 67 73 20 26 20 rt( pK->flags &
1bbd9 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 MEM_Blob );.
1bbda 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 pIdxKey = sqlite
1bbdb 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 3VdbeRecordUnpac
1bbdc 6b 28 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2c k(pCx->pKeyInfo,
1bbdd 20 70 4b 2d 3e 6e 2c 20 70 4b 2d 3e 7a 2c 0a 20 pK->n, pK->z,.
1bbde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bbdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bbe0 20 20 20 20 20 61 54 65 6d 70 52 65 63 2c 20 73 aTempRec, s
1bbe1 69 7a 65 6f 66 28 61 54 65 6d 70 52 65 63 29 29 izeof(aTempRec))
1bbe2 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 4b 65 ;. if( pIdxKe
1bbe3 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f y==0 ){. go
1bbe4 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d to no_mem;. }
1bbe5 0a 20 20 20 20 70 49 64 78 4b 65 79 2d 3e 66 6c . pIdxKey->fl
1bbe6 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f ags |= UNPACKED_
1bbe7 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a 0a 20 IGNORE_ROWID;..
1bbe8 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 /* Search for
1bbe9 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 50 31 20 an entry in P1
1bbea 77 68 65 72 65 20 61 6c 6c 20 62 75 74 20 74 68 where all but th
1bbeb 65 20 6c 61 73 74 20 72 6f 77 69 64 20 6d 61 74 e last rowid mat
1bbec 63 68 20 4b 0a 20 20 20 20 2a 2a 20 49 66 20 74 ch K. ** If t
1bbed 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 here is no such
1bbee 65 6e 74 72 79 2c 20 6a 75 6d 70 20 69 6d 6d 65 entry, jump imme
1bbef 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 20 diately to P2..
1bbf0 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
1bbf1 28 20 70 43 78 2d 3e 64 65 66 65 72 72 65 64 4d ( pCx->deferredM
1bbf2 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 oveto==0 );.
1bbf3 70 43 78 2d 3e 63 61 63 68 65 53 74 61 74 75 73 pCx->cacheStatus
1bbf4 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a = CACHE_STALE;.
1bbf5 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1bbf6 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
1bbf7 6b 65 64 28 70 43 72 73 72 2c 20 70 49 64 78 4b ked(pCrsr, pIdxK
1bbf8 65 79 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b ey, 0, 0, &res);
1bbf9 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1bbfa 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1bbfb 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
1bbfc 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 eUnpackedRecord(
1bbfd 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 pIdxKey);.
1bbfe 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1bbff 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 o_error;. }.
1bc00 20 20 20 69 66 28 20 72 65 73 3c 30 20 29 7b 0a if( res<0 ){.
1bc01 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1bc02 65 33 42 74 72 65 65 4e 65 78 74 28 70 43 72 73 e3BtreeNext(pCrs
1bc03 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 r, &res);.
1bc04 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 if( res ){.
1bc05 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1bc06 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c - 1;. sql
1bc07 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e ite3VdbeDeleteUn
1bc08 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49 64 packedRecord(pId
1bc09 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62 xKey);. b
1bc0a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1bc0b 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c }. rc = sql
1bc0c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f ite3VdbeIdxKeyCo
1bc0d 6d 70 61 72 65 28 70 43 78 2c 20 70 49 64 78 4b mpare(pCx, pIdxK
1bc0e 65 79 2c 20 26 72 65 73 29 3b 20 0a 20 20 20 20 ey, &res); .
1bc0f 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
1bc10 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 eUnpackedRecord(
1bc11 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 pIdxKey);. if
1bc12 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1bc13 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1bc14 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 69 _to_error;. i
1bc15 66 28 20 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 f( res>0 ){.
1bc16 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1bc17 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 1;. break;
1bc18 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 . }.. /* A
1bc19 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 70 43 t this point, pC
1bc1a 72 73 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 rsr is pointing
1bc1b 74 6f 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 50 to an entry in P
1bc1c 31 20 77 68 65 72 65 20 61 6c 6c 20 62 75 74 0a 1 where all but.
1bc1d 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6e 61 6c ** the final
1bc1e 20 65 6e 74 72 79 20 28 74 68 65 20 72 6f 77 69 entry (the rowi
1bc1f 64 29 20 6d 61 74 63 68 65 73 20 4b 2e 20 20 43 d) matches K. C
1bc20 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
1bc21 68 65 0a 20 20 20 20 2a 2a 20 66 69 6e 61 6c 20 he. ** final
1bc22 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20 rowid column is
1bc23 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 52 different from R
1bc24 2e 20 20 49 66 20 69 74 20 65 71 75 61 6c 73 20 . If it equals
1bc25 52 20 74 68 65 6e 20 6a 75 6d 70 0a 20 20 20 20 R then jump.
1bc26 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 ** immediately t
1bc27 6f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 o P2.. */.
1bc28 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1bc29 65 49 64 78 52 6f 77 69 64 28 70 43 72 73 72 2c eIdxRowid(pCrsr,
1bc2a 20 26 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63 &v);. if( rc
1bc2b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1bc2c 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f goto abort_
1bc2d 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1bc2e 20 20 7d 0a 20 20 20 20 69 66 28 20 76 3d 3d 52 }. if( v==R
1bc2f 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 ){. pc = p
1bc30 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1bc31 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a break;. }..
1bc32 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6e 61 6c /* The final
1bc33 20 76 61 72 69 6e 74 20 6f 66 20 74 68 65 20 6b varint of the k
1bc34 65 79 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 ey is different
1bc35 66 72 6f 6d 20 52 2e 20 20 53 74 6f 72 65 20 69 from R. Store i
1bc36 74 20 62 61 63 6b 0a 20 20 20 20 2a 2a 20 69 6e t back. ** in
1bc37 74 6f 20 72 65 67 69 73 74 65 72 20 52 33 2e 20 to register R3.
1bc38 20 28 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d (The record num
1bc39 62 65 72 20 6f 66 20 61 6e 20 65 6e 74 72 79 20 ber of an entry
1bc3a 74 68 61 74 20 76 69 6f 6c 61 74 65 73 0a 20 20 that violates.
1bc3b 20 20 2a 2a 20 61 20 55 4e 49 51 55 45 20 63 6f ** a UNIQUE co
1bc3c 6e 73 74 72 61 69 6e 74 2e 29 0a 20 20 20 20 2a nstraint.). *
1bc3d 2f 0a 20 20 20 20 70 49 6e 33 2d 3e 75 2e 69 20 /. pIn3->u.i
1bc3e 3d 20 76 3b 0a 20 20 20 20 61 73 73 65 72 74 28 = v;. assert(
1bc3f 20 70 49 6e 33 2d 3e 66 6c 61 67 73 26 4d 45 4d pIn3->flags&MEM
1bc40 5f 49 6e 74 20 29 3b 0a 20 20 7d 0a 20 20 62 72 _Int );. }. br
1bc41 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1bc42 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 31 20 e: NotExists P1
1bc43 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
1bc44 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 Use the content
1bc45 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 61 of register P3 a
1bc46 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79 2e s a integer key.
1bc47 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 0a 2a If a record .*
1bc48 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 79 20 * with that key
1bc49 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 does not exist i
1bc4a 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 74 n table of P1, t
1bc4b 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 hen jump to P2.
1bc4c 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 .** If the recor
1bc4d 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74 68 d does exist, th
1bc4e 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 en fall thru. T
1bc4f 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 he cursor is lef
1bc50 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 t .** pointing t
1bc51 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 o the record if
1bc52 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a it exists..**.**
1bc53 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 The difference
1bc54 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70 65 between this ope
1bc55 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46 6f ration and NotFo
1bc56 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 69 73 und is that this
1bc57 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61 73 .** operation as
1bc58 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 69 73 sumes the key is
1bc59 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20 an integer and
1bc5a 74 68 61 74 20 50 31 20 69 73 20 61 20 74 61 62 that P1 is a tab
1bc5b 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e 6f le whereas.** No
1bc5c 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 20 6b tFound assumes k
1bc5d 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e ey is a blob con
1bc5e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 4d 61 structed from Ma
1bc5f 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a 20 keRecord and.**
1bc60 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e 0a P1 is an index..
1bc61 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1bc62 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c Found, NotFound,
1bc63 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 IsUnique.*/.cas
1bc64 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a 20 e OP_NotExists:
1bc65 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1bc66 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 69 , in3 */. int i
1bc67 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 = pOp->p1;. Vd
1bc68 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1bc69 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1bc6a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
1bc6b 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 && i<p->nCursor
1bc6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
1bc6d 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 apCsr[i]!=0 );.
1bc6e 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 70 if( (pCrsr = (p
1bc6f 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 C = p->apCsr[i])
1bc70 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b ->pCursor)!=0 ){
1bc71 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d 20 30 . int res = 0
1bc72 3b 0a 20 20 20 20 75 36 34 20 69 4b 65 79 3b 0a ;. u64 iKey;.
1bc73 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 assert( pIn3
1bc74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
1bc75 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
1bc76 20 70 2d 3e 61 70 43 73 72 5b 69 5d 2d 3e 69 73 p->apCsr[i]->is
1bc77 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 69 4b 65 Table );. iKe
1bc78 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 70 49 6e y = intToKey(pIn
1bc79 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 72 63 20 3->u.i);. rc
1bc7a 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
1bc7b 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 72 vetoUnpacked(pCr
1bc7c 73 72 2c 20 30 2c 20 69 4b 65 79 2c 20 30 2c 26 sr, 0, iKey, 0,&
1bc7d 72 65 73 29 3b 0a 20 20 20 20 70 43 2d 3e 6c 61 res);. pC->la
1bc7e 73 74 52 6f 77 69 64 20 3d 20 70 49 6e 33 2d 3e stRowid = pIn3->
1bc7f 75 2e 69 3b 0a 20 20 20 20 70 43 2d 3e 72 6f 77 u.i;. pC->row
1bc80 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65 73 3d idIsValid = res=
1bc81 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 70 43 2d =0 ?1:0;. pC-
1bc82 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 >nullRow = 0;.
1bc83 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 pC->cacheStatu
1bc84 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b s = CACHE_STALE;
1bc85 0a 20 20 20 20 69 66 28 20 72 65 73 21 3d 30 20 . if( res!=0
1bc86 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f ){. pc = pO
1bc87 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 p->p2 - 1;.
1bc88 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72 6f 77 assert( pC->row
1bc89 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a idIsValid==0 );.
1bc8a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
1bc8b 28 20 21 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 ( !pC->pseudoTab
1bc8c 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 le ){. /* Thi
1bc8d 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 s happens when a
1bc8e 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 70 65 n attempt to ope
1bc8f 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 20 n a read cursor
1bc90 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73 on the . ** s
1bc91 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
1bc92 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 le returns SQLIT
1bc93 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f 0a E_EMPTY.. */.
1bc94 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e assert( pC->
1bc95 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 isTable );. p
1bc96 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1bc97 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1bc98 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 >rowidIsValid==0
1bc99 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b );. }. break;
1bc9a 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .}../* Opcode: S
1bc9b 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a 20 equence P1 P2 *
1bc9c 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20 74 * *.**.** Find t
1bc9d 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c he next availabl
1bc9e 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 e sequence numbe
1bc9f 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e r for cursor P1.
1bca0 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 65 .** Write the se
1bca1 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e quence number in
1bca2 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
1bca3 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65 20 ** The sequence
1bca4 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63 75 number on the cu
1bca5 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65 6e rsor is incremen
1bca6 74 65 64 20 61 66 74 65 72 20 74 68 69 73 0a 2a ted after this.*
1bca7 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 * instruction.
1bca8 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 75 .*/.case OP_Sequ
1bca9 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 20 ence: {
1bcaa 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1bcab 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 ease */. int i
1bcac 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 = pOp->p1;. ass
1bcad 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 ert( i>=0 && i<p
1bcae 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 ->nCursor );. a
1bcaf 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b ssert( p->apCsr[
1bcb0 69 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d i]!=0 );. pOut-
1bcb1 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b >u.i = p->apCsr[
1bcb2 69 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a i]->seqCount++;.
1bcb3 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1bcb4 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1bcb5 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a . break;.}.../*
1bcb6 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69 Opcode: NewRowi
1bcb7 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a d P1 P2 P3 * *.*
1bcb8 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69 *.** Get a new i
1bcb9 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 nteger record nu
1bcba 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77 mber (a.k.a "row
1bcbb 69 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65 id") used as the
1bcbc 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e key to a table.
1bcbd 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e .** The record n
1bcbe 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65 umber is not pre
1bcbf 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20 viously used as
1bcc0 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74 a key in the dat
1bcc1 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74 abase.** table t
1bcc2 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f hat cursor P1 po
1bcc3 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65 ints to. The ne
1bcc4 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 w record number
1bcc5 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72 is written.** wr
1bcc6 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65 itten to registe
1bcc7 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 r P2..**.** If P
1bcc8 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61 3>0 then P3 is a
1bcc9 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 68 register that h
1bcca 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 olds the largest
1bccb 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 67 previously.** g
1bccc 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 enerated record
1bccd 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65 77 20 number. No new
1bcce 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61 record numbers a
1bccf 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 re allowed to be
1bcd0 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 less.** than th
1bcd1 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65 6e 20 is value. When
1bcd2 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63 68 this value reach
1bcd3 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 es its maximum,
1bcd4 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a a SQLITE_FULL.**
1bcd5 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61 error is genera
1bcd6 74 65 64 2e 20 20 54 68 65 20 50 33 20 72 65 67 ted. The P3 reg
1bcd7 69 73 74 65 72 20 69 73 20 75 70 64 61 74 65 64 ister is updated
1bcd8 20 77 69 74 68 20 74 68 65 20 67 65 6e 65 72 61 with the genera
1bcd9 74 65 64 0a 2a 2a 20 72 65 63 6f 72 64 20 6e 75 ted.** record nu
1bcda 6d 62 65 72 2e 20 20 54 68 69 73 20 50 33 20 6d mber. This P3 m
1bcdb 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64 echanism is used
1bcdc 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 to help impleme
1bcdd 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e nt the.** AUTOIN
1bcde 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e CREMENT feature.
1bcdf 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52 .*/.case OP_NewR
1bce0 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 owid: {
1bce1 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1bce2 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 ease */. int i
1bce3 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 36 34 = pOp->p1;. i64
1bce4 20 76 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75 v = 0;. VdbeCu
1bce5 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 rsor *pC;. asse
1bce6 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1bce7 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 >nCursor );. as
1bce8 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 sert( p->apCsr[i
1bce9 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 ]!=0 );. if( (p
1bcea 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 C = p->apCsr[i])
1bceb 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29 7b 0a ->pCursor==0 ){.
1bcec 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f 20 /* The zero
1bced 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 initialization a
1bcee 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 74 bove is all that
1bcef 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 is needed */.
1bcf0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 }else{. /* Th
1bcf1 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 20 e next rowid or
1bcf2 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 64 record number (d
1bcf3 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 66 ifferent terms f
1bcf4 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 or the same.
1bcf5 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 74 ** thing) is obt
1bcf6 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d 73 ained in a two-s
1bcf7 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 tep algorithm..
1bcf8 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 72 **. ** Fir
1bcf9 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 6f st we attempt to
1bcfa 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 find the larges
1bcfb 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 t existing rowid
1bcfc 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 20 and add one.
1bcfd 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 75 ** to that. Bu
1bcfe 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 74 t if the largest
1bcff 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20 existing rowid
1bd00 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 6d is already the m
1bd01 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 6f aximum. ** po
1bd02 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c 20 sitive integer,
1bd03 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c 20 we have to fall
1bd04 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 through to the s
1bd05 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 6f econd. ** pro
1bd06 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f 72 babilistic algor
1bd07 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ithm. **.
1bd08 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6c ** The second al
1bd09 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 65 gorithm is to se
1bd0a 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 20 lect a rowid at
1bd0b 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 69 random and see i
1bd0c 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 65 f. ** it alre
1bd0d 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 68 ady exists in th
1bd0e 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20 e table. If it
1bd0f 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 does not exist,
1bd10 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 73 we have. ** s
1bd11 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 68 ucceeded. If th
1bd12 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 64 e random rowid d
1bd13 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 65 oes exist, we se
1bd14 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a 20 lect a new one.
1bd15 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 67 ** and try ag
1bd16 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 30 20 ain, up to 1000
1bd17 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 times.. **.
1bd18 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 ** For a table
1bd19 20 77 69 74 68 20 6c 65 73 73 20 74 68 61 6e 20 with less than
1bd1a 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72 69 65 2 billion entrie
1bd1b 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69 s, the probabili
1bd1c 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e 6f 74 ty. ** of not
1bd1d 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75 73 65 finding a unuse
1bd1e 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f 75 74 d rowid is about
1bd1f 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68 69 73 1.0e-300. This
1bd20 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20 6e 6f is a . ** no
1bd21 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69 n-zero probabili
1bd22 74 79 2c 20 62 75 74 20 69 74 20 69 73 20 73 74 ty, but it is st
1bd23 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c 79 20 ill vanishingly
1bd24 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75 6c 64 small and should
1bd25 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 63 61 . ** never ca
1bd26 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e 20 20 use a problem.
1bd27 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20 6d 75 You are much, mu
1bd28 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74 ch more likely t
1bd29 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a 2a 20 o have a. **
1bd2a 68 61 72 64 77 61 72 65 20 66 61 69 6c 75 72 65 hardware failure
1bd2b 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73 20 61 than for this a
1bd2c 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61 69 6c lgorithm to fail
1bd2d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1bd2e 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69 6e 20 The analysis in
1bd2f 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72 the previous par
1bd30 61 67 72 61 70 68 20 61 73 73 75 6d 65 73 20 74 agraph assumes t
1bd31 68 61 74 20 79 6f 75 20 68 61 76 65 20 61 20 67 hat you have a g
1bd32 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 ood. ** sourc
1bd33 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 e of random numb
1bd34 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62 72 61 ers. Is a libra
1bd35 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 ry function like
1bd36 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20 20 2a lrand48(). *
1bd37 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f 20 20 * good enough?
1bd38 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e 6f 74 Maybe. Maybe not
1bd39 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f 20 6b . It's hard to k
1bd3a 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68 65 72 now whether ther
1bd3b 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 62 e. ** might b
1bd3c 65 20 73 75 62 74 6c 65 20 62 75 67 73 20 69 73 e subtle bugs is
1bd3d 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61 some implementa
1bd3e 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64 34 38 tions of lrand48
1bd3f 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 63 () that. ** c
1bd40 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c ould cause probl
1bd41 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20 75 6e ems. To avoid un
1bd42 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c 69 74 certainty, SQLit
1bd43 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e 20 0a e uses its own .
1bd44 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20 6e 75 ** random nu
1bd45 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62 mber generator b
1bd46 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43 34 20 ased on the RC4
1bd47 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a algorithm.. *
1bd48 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72 6f 6d *. ** To prom
1bd49 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f 66 20 ote locality of
1bd4a 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20 72 65 reference for re
1bd4b 70 65 74 69 74 69 76 65 20 69 6e 73 65 72 74 73 petitive inserts
1bd4c 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72 , the. ** fir
1bd4d 73 74 20 66 65 77 20 61 74 74 65 6d 70 74 73 20 st few attempts
1bd4e 61 74 20 63 68 6f 6f 73 69 6e 67 20 61 20 72 61 at choosing a ra
1bd4f 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 6b 20 ndom rowid pick
1bd50 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 6c 69 values just a li
1bd51 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67 ttle. ** larg
1bd52 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 76 er than the prev
1bd53 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 68 69 ious rowid. Thi
1bd54 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f 77 6e s has been shown
1bd55 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c 79 0a experimentally.
1bd56 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 6c 65 ** to double
1bd57 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 74 68 the speed of th
1bd58 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 6f 6e e COPY operation
1bd59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 .. */. int
1bd5a 20 72 65 73 2c 20 72 78 3d 53 51 4c 49 54 45 5f res, rx=SQLITE_
1bd5b 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20 69 36 34 OK, cnt;. i64
1bd5c 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d 20 30 3b x;. cnt = 0;
1bd5d 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74 65 . if( (sqlite
1bd5e 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 3BtreeFlags(pC->
1bd5f 70 43 75 72 73 6f 72 29 26 28 42 54 52 45 45 5f pCursor)&(BTREE_
1bd60 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 5a 45 52 INTKEY|BTREE_ZER
1bd61 4f 44 41 54 41 29 29 20 21 3d 0a 20 20 20 20 20 ODATA)) !=.
1bd62 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 4b 45 BTREE_INTKE
1bd63 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 Y ){. rc =
1bd64 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1bd65 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 KPT;. goto
1bd66 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
1bd67 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 or;. }. as
1bd68 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 42 74 sert( (sqlite3Bt
1bd69 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 43 75 reeFlags(pC->pCu
1bd6a 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f 49 4e rsor) & BTREE_IN
1bd6b 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20 20 20 20 TKEY)!=0 );.
1bd6c 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 assert( (sqlite3
1bd6d 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 BtreeFlags(pC->p
1bd6e 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f Cursor) & BTREE_
1bd6f 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20 29 3b 0a ZERODATA)==0 );.
1bd70 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33 .#ifdef SQLITE_3
1bd71 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64 2BIT_ROWID.# d
1bd72 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 efine MAX_ROWID
1bd73 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65 0x7fffffff.#else
1bd74 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d . /* Some com
1bd75 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 pilers complain
1bd76 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20 about constants
1bd77 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66 of the form 0x7f
1bd78 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a ffffffffffffff..
1bd79 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f ** Others co
1bd7a 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37 mplain about 0x7
1bd7b 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 ffffffffffffffff
1bd7c 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 LL. The followi
1bd7d 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20 ng macro seems.
1bd7e 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65 ** to provide
1bd7f 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68 the constant wh
1bd80 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63 ile making all c
1bd81 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a ompilers happy..
1bd82 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e */.# defin
1bd83 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36 e MAX_ROWID (i6
1bd84 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66 4)( (((u64)0x7ff
1bd85 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75 fffff)<<32) | (u
1bd86 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0a 64)0xffffffff ).
1bd87 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 #endif.. if(
1bd88 21 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f !pC->useRandomRo
1bd89 77 69 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 wid ){. if(
1bd8a 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61 pC->nextRowidVa
1bd8b 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 76 lid ){. v
1bd8c 20 3d 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 = pC->nextRowid
1bd8d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1bd8e 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1bd8f 74 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d te3BtreeLast(pC-
1bd90 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b >pCursor, &res);
1bd91 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
1bd92 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1bd93 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f goto abo
1bd94 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1bd95 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1bd96 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 if( res ){.
1bd97 20 20 20 20 20 20 20 20 76 20 3d 20 31 3b 0a 20 v = 1;.
1bd98 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1bd99 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
1bd9a 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d 3e treeKeySize(pC->
1bd9b 70 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20 pCursor, &v);.
1bd9c 20 20 20 20 20 20 20 20 76 20 3d 20 6b 65 79 54 v = keyT
1bd9d 6f 49 6e 74 28 76 29 3b 0a 20 20 20 20 20 20 20 oInt(v);.
1bd9e 20 20 20 69 66 28 20 76 3d 3d 4d 41 58 5f 52 4f if( v==MAX_RO
1bd9f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 WID ){.
1bda0 20 20 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d pC->useRandom
1bda1 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 Rowid = 1;.
1bda2 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1bda3 20 20 20 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 v++;.
1bda4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1bda5 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e }. }..#ifn
1bda6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1bda7 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 AUTOINCREMENT.
1bda8 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 if( pOp->p3
1bda9 29 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 2a ){. Mem *
1bdaa 70 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 61 73 pMem;. as
1bdab 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 sert( pOp->p3>0
1bdac 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e && pOp->p3<=p->n
1bdad 4d 65 6d 20 29 3b 20 2f 2a 20 50 33 20 69 73 20 Mem ); /* P3 is
1bdae 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 a valid memory c
1bdaf 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 ell */. p
1bdb0 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Mem = &p->aMem[p
1bdb1 4f 70 2d 3e 70 33 5d 3b 0a 09 52 45 47 49 53 54 Op->p3];..REGIST
1bdb2 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 ER_TRACE(pOp->p3
1bdb3 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 , pMem);.
1bdb4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
1bdb5 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b ntegerify(pMem);
1bdb6 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1bdb7 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 (pMem->flags &
1bdb8 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 20 MEM_Int)!=0 );
1bdb9 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64 73 /* mem(P3) holds
1bdba 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 an integer */.
1bdbb 20 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d if( pMem-
1bdbc 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 >u.i==MAX_ROWID
1bdbd 7c 7c 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d || pC->useRandom
1bdbe 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 Rowid ){.
1bdbf 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 rc = SQLITE_F
1bdc0 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 67 ULL;. g
1bdc1 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1bdc2 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 _error;.
1bdc3 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3c }. if( v<
1bdc4 70 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 pMem->u.i+1 ){.
1bdc5 20 20 20 20 20 20 20 20 20 76 20 3d 20 70 4d 65 v = pMe
1bdc6 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20 m->u.i + 1;.
1bdc7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d }. pM
1bdc8 65 6d 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20 em->u.i = v;.
1bdc9 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
1bdca 20 20 20 69 66 28 20 76 3c 4d 41 58 5f 52 4f 57 if( v<MAX_ROW
1bdcb 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 ID ){. pC
1bdcc 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64 ->nextRowidValid
1bdcd 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43 = 1;. pC
1bdce 2d 3e 6e 65 78 74 52 6f 77 69 64 20 3d 20 76 2b ->nextRowid = v+
1bdcf 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 1;. }else{.
1bdd0 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65 78 74 pC->next
1bdd1 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b 0a RowidValid = 0;.
1bdd2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1bdd3 20 20 69 66 28 20 70 43 2d 3e 75 73 65 52 61 6e if( pC->useRan
1bdd4 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 domRowid ){.
1bdd5 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1bdd6 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 51 4c 49 3==0 ); /* SQLI
1bdd7 54 45 5f 46 55 4c 4c 20 6d 75 73 74 20 68 61 76 TE_FULL must hav
1bdd8 65 20 6f 63 63 75 72 72 65 64 20 70 72 69 6f 72 e occurred prior
1bdd9 20 74 6f 20 74 68 69 73 20 2a 2f 0a 20 20 20 20 to this */.
1bdda 20 20 76 20 3d 20 64 62 2d 3e 70 72 69 6f 72 4e v = db->priorN
1bddb 65 77 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 63 ewRowid;. c
1bddc 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f nt = 0;. do
1bddd 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6e {. if( cn
1bdde 74 3d 3d 30 20 26 26 20 28 76 26 30 78 66 66 66 t==0 && (v&0xfff
1bddf 66 66 66 29 3d 3d 76 20 29 7b 0a 20 20 20 20 20 fff)==v ){.
1bde0 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20 20 v++;.
1bde1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1bde2 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f sqlite3_rando
1bde3 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 76 29 2c mness(sizeof(v),
1bde4 20 26 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 &v);.
1bde5 69 66 28 20 63 6e 74 3c 35 20 29 20 76 20 26 3d if( cnt<5 ) v &=
1bde6 20 30 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20 0xffffff;.
1bde7 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
1bde8 20 76 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 v==0 ) continue
1bde9 3b 0a 20 20 20 20 20 20 20 20 78 20 3d 20 69 6e ;. x = in
1bdea 74 54 6f 4b 65 79 28 76 29 3b 0a 20 20 20 20 20 tToKey(v);.
1bdeb 20 20 20 72 78 20 3d 20 73 71 6c 69 74 65 33 42 rx = sqlite3B
1bdec 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
1bded 65 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 ed(pC->pCursor,
1bdee 30 2c 20 28 75 36 34 29 78 2c 20 30 2c 20 26 72 0, (u64)x, 0, &r
1bdef 65 73 29 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 es);. cnt
1bdf0 2b 2b 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 ++;. }while
1bdf1 28 20 63 6e 74 3c 31 30 30 20 26 26 20 72 78 3d ( cnt<100 && rx=
1bdf2 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 =SQLITE_OK && re
1bdf3 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62 s==0 );. db
1bdf4 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 20 ->priorNewRowid
1bdf5 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 72 = v;. if( r
1bdf6 78 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 x==SQLITE_OK &&
1bdf7 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 res==0 ){.
1bdf8 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 rc = SQLITE_FU
1bdf9 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f LL;. goto
1bdfa 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1bdfb 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ror;. }.
1bdfc 20 7d 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 }. pC->rowid
1bdfd 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 IsValid = 0;.
1bdfe 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 pC->deferredMov
1bdff 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d eto = 0;. pC-
1be00 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 >cacheStatus = C
1be01 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a ACHE_STALE;. }.
1be02 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1be03 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1be04 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 . pOut->u.i = v
1be05 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1be06 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 20 Opcode: Insert
1be07 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1be08 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e *.** Write an en
1be09 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74 61 62 try into the tab
1be0a 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50 31 2e le of cursor P1.
1be0b 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73 A new entry is
1be0c 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66 20 69 .** created if i
1be0d 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 61 64 t doesn't alread
1be0e 79 20 65 78 69 73 74 20 6f 72 20 74 68 65 20 64 y exist or the d
1be0f 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69 73 74 ata for an exist
1be10 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20 ing.** entry is
1be11 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 54 68 overwritten. Th
1be12 65 20 64 61 74 61 20 69 73 20 74 68 65 20 76 61 e data is the va
1be13 6c 75 65 20 73 74 6f 72 65 64 20 72 65 67 69 73 lue stored regis
1be14 74 65 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32 ter.** number P2
1be15 2e 20 54 68 65 20 6b 65 79 20 69 73 20 73 74 6f . The key is sto
1be16 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 red in register
1be17 50 33 2e 20 54 68 65 20 6b 65 79 20 6d 75 73 74 P3. The key must
1be18 0a 2a 2a 20 62 65 20 61 6e 20 69 6e 74 65 67 65 .** be an intege
1be19 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 r..**.** If the
1be1a 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 OPFLAG_NCHANGE f
1be1b 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 lag of P5 is set
1be1c 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 , then the row c
1be1d 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a hange count is.*
1be1e 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f * incremented (o
1be1f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 20 20 therwise not).
1be20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c 41 If the OPFLAG_LA
1be21 53 54 52 4f 57 49 44 20 66 6c 61 67 20 6f 66 20 STROWID flag of
1be22 50 35 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 P5 is set,.** th
1be23 65 6e 20 72 6f 77 69 64 20 69 73 20 73 74 6f 72 en rowid is stor
1be24 65 64 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e ed for subsequen
1be25 74 20 72 65 74 75 72 6e 20 62 79 20 74 68 65 0a t return by the.
1be26 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f ** sqlite3_last_
1be27 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 66 insert_rowid() f
1be28 75 6e 63 74 69 6f 6e 20 28 6f 74 68 65 72 77 69 unction (otherwi
1be29 73 65 20 69 74 20 69 73 20 75 6e 6d 6f 64 69 66 se it is unmodif
1be2a 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 ied)..**.** Para
1be2b 6d 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f 69 meter P4 may poi
1be2c 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63 nt to a string c
1be2d 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 ontaining the ta
1be2e 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 ble-name, or.**
1be2f 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 20 may be NULL. If
1be30 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 it is not NULL,
1be31 74 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 2d then the update-
1be32 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 65 hook .** (sqlite
1be33 33 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 3.xUpdateCallbac
1be34 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f k) is invoked fo
1be35 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 73 llowing a succes
1be36 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a sful insert..**.
1be37 2a 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 4f ** (WARNING/TODO
1be38 3a 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65 : If P1 is a pse
1be39 75 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 50 udo-cursor and P
1be3a 32 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 2 is dynamically
1be3b 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 .** allocated, t
1be3c 68 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 hen ownership of
1be3d 20 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 72 P2 is transferr
1be3e 65 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f ed to the pseudo
1be3f 2d 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 -cursor.** and r
1be40 65 67 69 73 74 65 72 20 50 32 20 62 65 63 6f 6d egister P2 becom
1be41 65 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 49 es ephemeral. I
1be42 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
1be43 63 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a 20 changed, the.**
1be44 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 value of registe
1be45 72 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 63 r P2 will then c
1be46 68 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 72 hange. Make sur
1be47 65 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a e this does not.
1be48 2a 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f ** cause any pro
1be49 62 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 blems.).**.** Th
1be4a 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f is instruction o
1be4b 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62 nly works on tab
1be4c 6c 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 les. The equiva
1be4d 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e lent instruction
1be4e 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20 .** for indices
1be4f 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e is OP_IdxInsert.
1be50 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65 .*/.case OP_Inse
1be51 72 74 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70 44 61 rt: {. Mem *pDa
1be52 74 61 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ta = &p->aMem[pO
1be53 70 2d 3e 70 32 5d 3b 0a 20 20 4d 65 6d 20 2a 70 p->p2];. Mem *p
1be54 4b 65 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Key = &p->aMem[p
1be55 4f 70 2d 3e 70 33 5d 3b 0a 0a 20 20 69 36 34 20 Op->p3];.. i64
1be56 69 4b 65 79 3b 20 20 20 2f 2a 20 54 68 65 20 69 iKey; /* The i
1be57 6e 74 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20 nteger ROWID or
1be58 6b 65 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f key for the reco
1be59 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 rd to be inserte
1be5a 64 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 d */. int i = p
1be5b 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 Op->p1;. VdbeCu
1be5c 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 rsor *pC;. asse
1be5d 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1be5e 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 >nCursor );. pC
1be5f 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a = p->apCsr[i];.
1be60 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 assert( pC!=0
1be61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d );. assert( pC-
1be62 3e 70 43 75 72 73 6f 72 21 3d 30 20 7c 7c 20 70 >pCursor!=0 || p
1be63 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 C->pseudoTable )
1be64 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 ;. assert( pKey
1be65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
1be66 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t );. assert( p
1be67 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1be68 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1be69 4f 70 2d 3e 70 32 2c 20 70 44 61 74 61 29 3b 0a Op->p2, pData);.
1be6a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1be6b 28 70 4f 70 2d 3e 70 33 2c 20 70 4b 65 79 29 3b (pOp->p3, pKey);
1be6c 0a 0a 20 20 69 4b 65 79 20 3d 20 69 6e 74 54 6f .. iKey = intTo
1be6d 4b 65 79 28 70 4b 65 79 2d 3e 75 2e 69 29 3b 0a Key(pKey->u.i);.
1be6e 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 if( pOp->p5 &
1be6f 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 OPFLAG_NCHANGE )
1be70 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 p->nChange++;.
1be71 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f if( pOp->p5 & O
1be72 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 PFLAG_LASTROWID
1be73 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 ) db->lastRowid
1be74 3d 20 70 4b 65 79 2d 3e 75 2e 69 3b 0a 20 20 69 = pKey->u.i;. i
1be75 66 28 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 f( pC->nextRowid
1be76 56 61 6c 69 64 20 26 26 20 70 4b 65 79 2d 3e 75 Valid && pKey->u
1be77 2e 69 3e 3d 70 43 2d 3e 6e 65 78 74 52 6f 77 69 .i>=pC->nextRowi
1be78 64 20 29 7b 0a 20 20 20 20 70 43 2d 3e 6e 65 78 d ){. pC->nex
1be79 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b tRowidValid = 0;
1be7a 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 61 74 61 . }. if( pData
1be7b 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1be7c 6c 6c 20 29 7b 0a 20 20 20 20 70 44 61 74 61 2d ll ){. pData-
1be7d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 70 44 61 74 >z = 0;. pDat
1be7e 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 a->n = 0;. }els
1be7f 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
1be80 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 28 4d Data->flags & (M
1be81 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 EM_Blob|MEM_Str)
1be82 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 );. }. if( pC
1be83 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 7b ->pseudoTable ){
1be84 0a 20 20 20 20 69 66 28 20 21 70 43 2d 3e 65 70 . if( !pC->ep
1be85 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 20 29 hemPseudoTable )
1be86 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
1be87 62 46 72 65 65 28 64 62 2c 20 70 43 2d 3e 70 44 bFree(db, pC->pD
1be88 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ata);. }.
1be89 70 43 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b pC->iKey = iKey;
1be8a 0a 20 20 20 20 70 43 2d 3e 6e 44 61 74 61 20 3d . pC->nData =
1be8b 20 70 44 61 74 61 2d 3e 6e 3b 0a 20 20 20 20 69 pData->n;. i
1be8c 66 28 20 70 44 61 74 61 2d 3e 7a 3d 3d 70 44 61 f( pData->z==pDa
1be8d 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20 7c 7c 20 70 ta->zMalloc || p
1be8e 43 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 C->ephemPseudoTa
1be8f 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 2d ble ){. pC-
1be90 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 2d 3e >pData = pData->
1be91 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 z;. if( !pC
1be92 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 ->ephemPseudoTab
1be93 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 le ){. pD
1be94 61 74 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d ata->flags &= ~M
1be95 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 20 20 20 20 EM_Dyn;.
1be96 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 pData->flags |=
1be97 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 MEM_Ephem;.
1be98 20 20 20 70 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f pData->zMallo
1be99 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 c = 0;. }.
1be9a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1be9b 70 43 2d 3e 70 44 61 74 61 20 3d 20 73 71 6c 69 pC->pData = sqli
1be9c 74 65 33 4d 61 6c 6c 6f 63 28 20 70 43 2d 3e 6e te3Malloc( pC->n
1be9d 44 61 74 61 2b 32 20 29 3b 0a 20 20 20 20 20 20 Data+2 );.
1be9e 69 66 28 20 21 70 43 2d 3e 70 44 61 74 61 20 29 if( !pC->pData )
1be9f 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1bea0 20 20 20 20 6d 65 6d 63 70 79 28 70 43 2d 3e 70 memcpy(pC->p
1bea1 44 61 74 61 2c 20 70 44 61 74 61 2d 3e 7a 2c 20 Data, pData->z,
1bea2 70 43 2d 3e 6e 44 61 74 61 29 3b 0a 20 20 20 20 pC->nData);.
1bea3 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 2d 3e pC->pData[pC->
1bea4 6e 44 61 74 61 5d 20 3d 20 30 3b 0a 20 20 20 20 nData] = 0;.
1bea5 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 2d 3e pC->pData[pC->
1bea6 6e 44 61 74 61 2b 31 5d 20 3d 20 30 3b 0a 20 20 nData+1] = 0;.
1bea7 20 20 7d 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c }. pC->null
1bea8 52 6f 77 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 Row = 0;. }else
1bea9 7b 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b {. int nZero;
1beaa 0a 20 20 20 20 69 66 28 20 70 44 61 74 61 2d 3e . if( pData->
1beab 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f flags & MEM_Zero
1beac 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20 ){. nZero
1bead 3d 20 70 44 61 74 61 2d 3e 75 2e 6e 5a 65 72 6f = pData->u.nZero
1beae 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1beaf 20 20 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 nZero = 0;.
1beb0 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c }. rc = sql
1beb1 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 ite3BtreeInsert(
1beb2 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 pC->pCursor, 0,
1beb3 69 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 iKey,.
1beb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1beb5 20 20 70 44 61 74 61 2d 3e 7a 2c 20 70 44 61 74 pData->z, pDat
1beb6 61 2d 3e 6e 2c 20 6e 5a 65 72 6f 2c 0a 20 20 20 a->n, nZero,.
1beb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1beb8 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 35 pOp->p5
1beb9 20 26 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 & OPFLAG_APPEND
1beba 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 70 43 2d 3e );. }. . pC->
1bebb 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
1bebc 3b 0a 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 ;. pC->deferred
1bebd 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 70 43 Moveto = 0;. pC
1bebe 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1bebf 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 CACHE_STALE;..
1bec0 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 70 /* Invoke the up
1bec1 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71 date-hook if req
1bec2 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 uired. */. if(
1bec3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1bec4 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c db->xUpdateCall
1bec5 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e back && pOp->p4.
1bec6 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 z ){. const c
1bec7 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 har *zDb = db->a
1bec8 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d Db[pC->iDb].zNam
1bec9 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 e;. const cha
1beca 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 r *zTbl = pOp->p
1becb 34 2e 7a 3b 0a 20 20 20 20 69 6e 74 20 6f 70 20 4.z;. int op
1becc 3d 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 = ((pOp->p5 & OP
1becd 46 4c 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f FLAG_ISUPDATE) ?
1bece 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a SQLITE_UPDATE :
1becf 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b SQLITE_INSERT);
1bed0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1bed1 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 >isTable );.
1bed2 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 db->xUpdateCallb
1bed3 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 ack(db->pUpdateA
1bed4 72 67 2c 20 6f 70 2c 20 7a 44 62 2c 20 7a 54 62 rg, op, zDb, zTb
1bed5 6c 2c 20 69 4b 65 79 29 3b 0a 20 20 20 20 61 73 l, iKey);. as
1bed6 73 65 72 74 28 20 70 43 2d 3e 69 44 62 3e 3d 30 sert( pC->iDb>=0
1bed7 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b );. }. break;
1bed8 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 .}../* Opcode: D
1bed9 65 6c 65 74 65 20 50 31 20 50 32 20 2a 20 50 34 elete P1 P2 * P4
1beda 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 *.**.** Delete
1bedb 74 68 65 20 72 65 63 6f 72 64 20 61 74 20 77 68 the record at wh
1bedc 69 63 68 20 74 68 65 20 50 31 20 63 75 72 73 6f ich the P1 curso
1bedd 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 r is currently p
1bede 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 ointing..**.** T
1bedf 68 65 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 62 he cursor will b
1bee0 65 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 e left pointing
1bee1 61 74 20 65 69 74 68 65 72 20 74 68 65 20 6e 65 at either the ne
1bee2 78 74 20 6f 72 20 74 68 65 20 70 72 65 76 69 6f xt or the previo
1bee3 75 73 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 20 us.** record in
1bee4 74 68 65 20 74 61 62 6c 65 2e 20 49 66 20 69 74 the table. If it
1bee5 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
1bee6 67 20 61 74 20 74 68 65 20 6e 65 78 74 20 72 65 g at the next re
1bee7 63 6f 72 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 cord, then.** th
1bee8 65 20 6e 65 78 74 20 4e 65 78 74 20 69 6e 73 74 e next Next inst
1bee9 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 ruction will be
1beea 61 20 6e 6f 2d 6f 70 2e 20 20 48 65 6e 63 65 20 a no-op. Hence
1beeb 69 74 20 69 73 20 4f 4b 20 74 6f 20 64 65 6c 65 it is OK to dele
1beec 74 65 0a 2a 2a 20 61 20 72 65 63 6f 72 64 20 66 te.** a record f
1beed 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 4e 65 rom within an Ne
1beee 78 74 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 xt loop..**.** I
1beef 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 f the OPFLAG_NCH
1bef0 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 32 20 ANGE flag of P2
1bef1 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 is set, then the
1bef2 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e row change coun
1bef3 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e t is.** incremen
1bef4 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e ted (otherwise n
1bef5 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 ot)..**.** P1 mu
1bef6 73 74 20 6e 6f 74 20 62 65 20 70 73 65 75 64 6f st not be pseudo
1bef7 2d 74 61 62 6c 65 2e 20 20 49 74 20 68 61 73 20 -table. It has
1bef8 74 6f 20 62 65 20 61 20 72 65 61 6c 20 74 61 62 to be a real tab
1bef9 6c 65 20 77 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 le with.** multi
1befa 70 6c 65 20 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 ple rows..**.**
1befb 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c If P4 is not NUL
1befc 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 L, then it is th
1befd 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 e name of the ta
1befe 62 6c 65 20 74 68 61 74 20 50 31 20 69 73 0a 2a ble that P1 is.*
1beff 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 * pointing to.
1bf00 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 The update hook
1bf01 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c will be invoked,
1bf02 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a if it exists..*
1bf03 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e * If P4 is not N
1bf04 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 50 31 20 ULL then the P1
1bf05 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 cursor must have
1bf06 20 62 65 65 6e 20 70 6f 73 69 74 69 6f 6e 65 64 been positioned
1bf07 0a 2a 2a 20 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 .** using OP_Not
1bf08 46 6f 75 6e 64 20 70 72 69 6f 72 20 74 6f 20 69 Found prior to i
1bf09 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 6f 70 63 nvoking this opc
1bf0a 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ode..*/.case OP_
1bf0b 44 65 6c 65 74 65 3a 20 7b 0a 20 20 69 6e 74 20 Delete: {. int
1bf0c 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 i = pOp->p1;. i
1bf0d 36 34 20 69 4b 65 79 3b 0a 20 20 56 64 62 65 43 64 iKey;. VdbeC
1bf0e 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 ursor *pC;.. as
1bf0f 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1bf10 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1bf11 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d pC = p->apCsr[i]
1bf12 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d ;. assert( pC!=
1bf13 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1bf14 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b C->pCursor!=0 );
1bf15 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c 69 64 20 /* Only valid
1bf16 66 6f 72 20 72 65 61 6c 20 74 61 62 6c 65 73 2c for real tables,
1bf17 20 6e 6f 20 70 73 65 75 64 6f 74 61 62 6c 65 73 no pseudotables
1bf18 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 */.. /* If the
1bf19 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 77 69 6c update-hook wil
1bf1a 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 73 65 l be invoked, se
1bf1b 74 20 69 4b 65 79 20 74 6f 20 74 68 65 20 72 6f t iKey to the ro
1bf1c 77 69 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 wid of the. **
1bf1d 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65 row being delete
1bf1e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 d.. */. if( db
1bf1f 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 ->xUpdateCallbac
1bf20 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 k && pOp->p4.z )
1bf21 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 {. assert( pC
1bf22 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 ->isTable );.
1bf23 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72 6f 77 assert( pC->row
1bf24 69 64 49 73 56 61 6c 69 64 20 29 3b 20 20 2f 2a idIsValid ); /*
1bf25 20 6c 61 73 74 52 6f 77 69 64 20 73 65 74 20 62 lastRowid set b
1bf26 79 20 70 72 65 76 69 6f 75 73 20 4f 50 5f 4e 6f y previous OP_No
1bf27 74 46 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 69 4b tFound */. iK
1bf28 65 79 20 3d 20 70 43 2d 3e 6c 61 73 74 52 6f 77 ey = pC->lastRow
1bf29 69 64 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 id;. }.. rc =
1bf2a 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f sqlite3VdbeCurso
1bf2b 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 69 rMoveto(pC);. i
1bf2c 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f f( rc ) goto abo
1bf2d 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1bf2e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
1bf2f 74 72 65 65 44 65 6c 65 74 65 28 70 43 2d 3e 70 treeDelete(pC->p
1bf30 43 75 72 73 6f 72 29 3b 0a 20 20 70 43 2d 3e 6e Cursor);. pC->n
1bf31 65 78 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 extRowidValid =
1bf32 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74 0;. pC->cacheSt
1bf33 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1bf34 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 LE;.. /* Invoke
1bf35 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b the update-hook
1bf36 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f if required. */
1bf37 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1bf38 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 E_OK && db->xUpd
1bf39 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 ateCallback && p
1bf3a 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 Op->p4.z ){.
1bf3b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 const char *zDb
1bf3c 3d 20 64 62 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 = db->aDb[pC->iD
1bf3d 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f b].zName;. co
1bf3e 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d nst char *zTbl =
1bf3f 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 pOp->p4.z;.
1bf40 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 db->xUpdateCallb
1bf41 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 ack(db->pUpdateA
1bf42 72 67 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 rg, SQLITE_DELET
1bf43 45 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 4b E, zDb, zTbl, iK
1bf44 65 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ey);. assert(
1bf45 20 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 pC->iDb>=0 );.
1bf46 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 }. if( pOp->p2
1bf47 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 & OPFLAG_NCHANG
1bf48 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b E ) p->nChange++
1bf49 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1bf4a 20 4f 70 63 6f 64 65 3a 20 52 65 73 65 74 43 6f Opcode: ResetCo
1bf4b 75 6e 74 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a unt P1 * *.**.**
1bf4c 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 73 This opcode res
1bf4d 65 74 73 20 74 68 65 20 56 4d 73 20 69 6e 74 65 ets the VMs inte
1bf4e 72 6e 61 6c 20 63 68 61 6e 67 65 20 63 6f 75 6e rnal change coun
1bf4f 74 65 72 20 74 6f 20 30 2e 20 49 66 20 50 31 20 ter to 0. If P1
1bf50 69 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e is true,.** then
1bf51 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1bf52 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 e change counter
1bf53 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 is copied to th
1bf54 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
1bf55 65 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e e.** change coun
1bf56 74 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79 ter (returned by
1bf57 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
1bf58 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61 s to sqlite3_cha
1bf59 6e 67 65 73 28 29 29 0a 2a 2a 20 62 65 66 6f 72 nges()).** befor
1bf5a 65 20 69 74 20 69 73 20 72 65 73 65 74 2e 20 54 e it is reset. T
1bf5b 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 his is used by t
1bf5c 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e rigger programs.
1bf5d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 65 .*/.case OP_Rese
1bf5e 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 69 66 28 20 tCount: {. if(
1bf5f 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73 pOp->p1 ){. s
1bf60 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 qlite3VdbeSetCha
1bf61 6e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 nges(db, p->nCha
1bf62 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e nge);. }. p->n
1bf63 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 62 72 Change = 0;. br
1bf64 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1bf65 65 3a 20 52 6f 77 44 61 74 61 20 50 31 20 50 32 e: RowData P1 P2
1bf66 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 * * *.**.** Wri
1bf67 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 te into register
1bf68 20 50 32 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 P2 the complete
1bf69 20 72 6f 77 20 64 61 74 61 20 66 6f 72 20 63 75 row data for cu
1bf6a 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 72 rsor P1..** Ther
1bf6b 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65 e is no interpre
1bf6c 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 tation of the da
1bf6d 74 61 2e 20 20 0a 2a 2a 20 49 74 20 69 73 20 6a ta. .** It is j
1bf6e 75 73 74 20 63 6f 70 69 65 64 20 6f 6e 74 6f 20 ust copied onto
1bf6f 74 68 65 20 50 32 20 72 65 67 69 73 74 65 72 20 the P2 register
1bf70 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20 69 exactly as .** i
1bf71 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 t is found in th
1bf72 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1bf73 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 .**.** If the P1
1bf74 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 cursor must be
1bf75 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 pointing to a va
1bf76 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 20 4e lid row (not a N
1bf77 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 ULL row).** of a
1bf78 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 real table, not
1bf79 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e a pseudo-table.
1bf7a 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .*/./* Opcode: R
1bf7b 6f 77 4b 65 79 20 50 31 20 50 32 20 2a 20 2a 20 owKey P1 P2 * *
1bf7c 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e *.**.** Write in
1bf7d 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74 to register P2 t
1bf7e 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 he complete row
1bf7f 6b 65 79 20 66 6f 72 20 63 75 72 73 6f 72 20 50 key for cursor P
1bf80 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 1..** There is n
1bf81 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e o interpretation
1bf82 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a of the data. .
1bf83 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 63 6f ** The key is co
1bf84 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 33 pied onto the P3
1bf85 20 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c register exactl
1bf86 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 y as .** it is f
1bf87 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 ound in the data
1bf88 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a base file..**.**
1bf89 20 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f If the P1 curso
1bf8a 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 r must be pointi
1bf8b 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f ng to a valid ro
1bf8c 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f w (not a NULL ro
1bf8d 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 w).** of a real
1bf8e 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 table, not a pse
1bf8f 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 udo-table..*/.ca
1bf90 73 65 20 4f 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 se OP_RowKey:.ca
1bf91 73 65 20 4f 50 5f 52 6f 77 44 61 74 61 3a 20 7b se OP_RowData: {
1bf92 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e . int i = pOp->
1bf93 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 p1;. VdbeCursor
1bf94 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 *pC;. BtCursor
1bf95 20 2a 70 43 72 73 72 3b 0a 20 20 75 33 32 20 6e *pCrsr;. u32 n
1bf96 3b 0a 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e ;.. pOut = &p->
1bf97 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a aMem[pOp->p2];..
1bf98 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74 20 52 /* Note that R
1bf99 6f 77 4b 65 79 20 61 6e 64 20 52 6f 77 44 61 74 owKey and RowDat
1bf9a 61 20 61 72 65 20 72 65 61 6c 6c 79 20 65 78 61 a are really exa
1bf9b 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 69 6e ctly the same in
1bf9c 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 struction */. a
1bf9d 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
1bf9e 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1bf9f 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 pC = p->apCsr[i
1bfa0 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d ];. assert( pC-
1bfa1 3e 69 73 54 61 62 6c 65 20 7c 7c 20 70 4f 70 2d >isTable || pOp-
1bfa2 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 4b >opcode==OP_RowK
1bfa3 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ey );. assert(
1bfa4 70 43 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 pC->isIndex || p
1bfa5 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 Op->opcode==OP_R
1bfa6 6f 77 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 owData );. asse
1bfa7 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61 rt( pC!=0 );. a
1bfa8 73 73 65 72 74 28 20 70 43 2d 3e 6e 75 6c 6c 52 ssert( pC->nullR
1bfa9 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ow==0 );. asser
1bfaa 74 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 t( pC->pseudoTab
1bfab 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 le==0 );. asser
1bfac 74 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d t( pC->pCursor!=
1bfad 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20 70 0 );. pCrsr = p
1bfae 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 72 63 C->pCursor;. rc
1bfaf 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 = sqlite3VdbeCu
1bfb0 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a rsorMoveto(pC);.
1bfb1 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
1bfb2 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
1bfb3 6f 72 3b 0a 20 20 69 66 28 20 70 43 2d 3e 69 73 or;. if( pC->is
1bfb4 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 69 36 34 Index ){. i64
1bfb5 20 6e 36 34 3b 0a 20 20 20 20 61 73 73 65 72 74 n64;. assert
1bfb6 28 20 21 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 ( !pC->isTable )
1bfb7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1bfb8 65 65 4b 65 79 53 69 7a 65 28 70 43 72 73 72 2c eeKeySize(pCrsr,
1bfb9 20 26 6e 36 34 29 3b 0a 20 20 20 20 69 66 28 20 &n64);. if(
1bfba 6e 36 34 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 n64>db->aLimit[S
1bfbb 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
1bfbc 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 TH] ){. got
1bfbd 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d o too_big;. }
1bfbe 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 6e 36 . n = (int)n6
1bfbf 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4;. }else{.
1bfc0 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
1bfc1 53 69 7a 65 28 70 43 72 73 72 2c 20 26 6e 29 3b Size(pCrsr, &n);
1bfc2 0a 20 20 20 20 69 66 28 20 28 69 6e 74 29 6e 3e . if( (int)n>
1bfc3 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1bfc4 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
1bfc5 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f ){. goto to
1bfc6 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d o_big;. }. }
1bfc7 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
1bfc8 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 beMemGrow(pOut,
1bfc9 6e 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 n, 0) ){. got
1bfca 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 o no_mem;. }.
1bfcb 70 4f 75 74 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 4d pOut->n = n;. M
1bfcc 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
1bfcd 75 74 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 ut, MEM_Blob);.
1bfce 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 if( pC->isIndex
1bfcf 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1bfd0 69 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 72 ite3BtreeKey(pCr
1bfd1 73 72 2c 20 30 2c 20 6e 2c 20 70 4f 75 74 2d 3e sr, 0, n, pOut->
1bfd2 7a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 z);. }else{.
1bfd3 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1bfd4 65 65 44 61 74 61 28 70 43 72 73 72 2c 20 30 2c eeData(pCrsr, 0,
1bfd5 20 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 n, pOut->z);.
1bfd6 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 }. pOut->enc =
1bfd7 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a SQLITE_UTF8; /*
1bfd8 20 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f In case the blo
1bfd9 62 20 69 73 20 65 76 65 72 20 63 61 73 74 20 74 b is ever cast t
1bfda 6f 20 74 65 78 74 20 2a 2f 0a 20 20 55 50 44 41 o text */. UPDA
1bfdb 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
1bfdc 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a pOut);. break;.
1bfdd 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f }../* Opcode: Ro
1bfde 77 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a wid P1 P2 * * *.
1bfdf 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72 **.** Store in r
1bfe0 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e egister P2 an in
1bfe1 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 teger which is t
1bfe2 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61 he key of the ta
1bfe3 62 6c 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a ble entry that.*
1bfe4 2a 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c * P1 is currentl
1bfe5 79 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2f 0a 63 y point to..*/.c
1bfe6 61 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b 20 ase OP_Rowid: {
1bfe7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bfe8 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1bfe9 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 se */. int i =
1bfea 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 pOp->p1;. VdbeC
1bfeb 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34 ursor *pC;. i64
1bfec 20 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 v;.. assert( i
1bfed 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 >=0 && i<p->nCur
1bfee 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d sor );. pC = p-
1bfef 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 >apCsr[i];. ass
1bff0 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 ert( pC!=0 );.
1bff1 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1bff2 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 CursorMoveto(pC)
1bff3 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 ;. if( rc ) got
1bff4 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1bff5 72 72 6f 72 3b 0a 20 20 69 66 28 20 70 43 2d 3e rror;. if( pC->
1bff6 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 7b 0a rowidIsValid ){.
1bff7 20 20 20 20 76 20 3d 20 70 43 2d 3e 6c 61 73 74 v = pC->last
1bff8 52 6f 77 69 64 3b 0a 20 20 7d 65 6c 73 65 20 69 Rowid;. }else i
1bff9 66 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 f( pC->pseudoTab
1bffa 6c 65 20 29 7b 0a 20 20 20 20 76 20 3d 20 6b 65 le ){. v = ke
1bffb 79 54 6f 49 6e 74 28 70 43 2d 3e 69 4b 65 79 29 yToInt(pC->iKey)
1bffc 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 ;. }else if( pC
1bffd 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 ->nullRow ){.
1bffe 20 2f 2a 20 4c 65 61 76 65 20 74 68 65 20 72 6f /* Leave the ro
1bfff 77 69 64 20 73 65 74 20 74 6f 20 61 20 4e 55 4c wid set to a NUL
1c000 4c 20 2a 2f 0a 20 20 20 20 62 72 65 61 6b 3b 0a L */. break;.
1c001 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
1c002 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 ert( pC->pCursor
1c003 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 !=0 );. sqlit
1c004 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 e3BtreeKeySize(p
1c005 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 76 29 3b C->pCursor, &v);
1c006 0a 20 20 20 20 76 20 3d 20 6b 65 79 54 6f 49 6e . v = keyToIn
1c007 74 28 76 29 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 t(v);. }. pOut
1c008 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 4d 65 6d ->u.i = v;. Mem
1c009 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
1c00a 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 , MEM_Int);. br
1c00b 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1c00c 65 3a 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 e: NullRow P1 *
1c00d 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 * * *.**.** Move
1c00e 20 74 68 65 20 63 75 72 73 6f 72 20 50 31 20 74 the cursor P1 t
1c00f 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 o a null row. A
1c010 6e 79 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 ny OP_Column ope
1c011 72 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 rations.** that
1c012 6f 63 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 occur while the
1c013 63 75 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 cursor is on the
1c014 20 6e 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 null row will a
1c015 6c 77 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61 lways.** write a
1c016 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f NULL..*/.case O
1c017 50 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 20 20 69 P_NullRow: {. i
1c018 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a nt i = pOp->p1;.
1c019 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c01a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d ;.. assert( i>=
1c01b 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 0 && i<p->nCurso
1c01c 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 r );. pC = p->a
1c01d 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 pCsr[i];. asser
1c01e 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 70 43 t( pC!=0 );. pC
1c01f 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 ->nullRow = 1;.
1c020 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 pC->rowidIsVali
1c021 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 2d d = 0;. if( pC-
1c022 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 >pCursor ){.
1c023 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 sqlite3BtreeClea
1c024 72 43 75 72 73 6f 72 28 70 43 2d 3e 70 43 75 72 rCursor(pC->pCur
1c025 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 sor);. }. brea
1c026 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1c027 20 4c 61 73 74 20 50 31 20 50 32 20 2a 20 2a 20 Last P1 P2 * *
1c028 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 *.**.** The next
1c029 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 use of the Rowi
1c02a 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e d or Column or N
1c02b 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
1c02c 66 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 for P1 .** will
1c02d 72 65 66 65 72 20 74 6f 20 74 68 65 20 6c 61 73 refer to the las
1c02e 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 t entry in the d
1c02f 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 atabase table or
1c030 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 index..** If th
1c031 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 e table or index
1c032 20 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 is empty and P2
1c033 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d >0, then jump im
1c034 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e mediately to P2.
1c035 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f .** If P2 is 0 o
1c036 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f r if the table o
1c037 72 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 r index is not e
1c038 6d 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 mpty, fall throu
1c039 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c gh.** to the fol
1c03a 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 lowing instructi
1c03b 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c on..*/.case OP_L
1c03c 61 73 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a ast: { /*
1c03d 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69 jump */. int i
1c03e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 = pOp->p1;. Vd
1c03f 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1c040 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1c041 0a 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61 . int res;.. a
1c042 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
1c043 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1c044 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 pC = p->apCsr[i
1c045 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 ];. assert( pC!
1c046 3d 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20 =0 );. pCrsr =
1c047 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 pC->pCursor;. a
1c048 73 73 65 72 74 28 20 70 43 72 73 72 21 3d 30 20 ssert( pCrsr!=0
1c049 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1c04a 33 42 74 72 65 65 4c 61 73 74 28 70 43 72 73 72 3BtreeLast(pCrsr
1c04b 2c 20 26 72 65 73 29 3b 0a 20 20 70 43 2d 3e 6e , &res);. pC->n
1c04c 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 72 65 73 ullRow = (u8)res
1c04d 3b 0a 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 ;. pC->deferred
1c04e 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 70 43 Moveto = 0;. pC
1c04f 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d ->rowidIsValid =
1c050 20 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 0;. pC->cacheS
1c051 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1c052 41 4c 45 3b 0a 20 20 69 66 28 20 72 65 73 20 26 ALE;. if( res &
1c053 26 20 70 4f 70 2d 3e 70 32 3e 30 20 29 7b 0a 20 & pOp->p2>0 ){.
1c054 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1c055 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b - 1;. }. break
1c056 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a ;.}.../* Opcode:
1c057 20 53 6f 72 74 20 50 31 20 50 32 20 2a 20 2a 20 Sort P1 P2 * *
1c058 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 *.**.** This opc
1c059 6f 64 65 20 64 6f 65 73 20 65 78 61 63 74 6c 79 ode does exactly
1c05a 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 the same thing
1c05b 61 73 20 4f 50 5f 52 65 77 69 6e 64 20 65 78 63 as OP_Rewind exc
1c05c 65 70 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 ept that.** it i
1c05d 6e 63 72 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64 ncrements an und
1c05e 6f 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c ocumented global
1c05f 20 76 61 72 69 61 62 6c 65 20 75 73 65 64 20 66 variable used f
1c060 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a or testing..**.*
1c061 2a 20 53 6f 72 74 69 6e 67 20 69 73 20 61 63 63 * Sorting is acc
1c062 6f 6d 70 6c 69 73 68 65 64 20 62 79 20 77 72 69 omplished by wri
1c063 74 69 6e 67 20 72 65 63 6f 72 64 73 20 69 6e 74 ting records int
1c064 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 o a sorting inde
1c065 78 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e x,.** then rewin
1c066 64 69 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20 ding that index
1c067 61 6e 64 20 70 6c 61 79 69 6e 67 20 69 74 20 62 and playing it b
1c068 61 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69 ack from beginni
1c069 6e 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 ng to.** end. W
1c06a 65 20 75 73 65 20 74 68 65 20 4f 50 5f 53 6f 72 e use the OP_Sor
1c06b 74 20 6f 70 63 6f 64 65 20 69 6e 73 74 65 61 64 t opcode instead
1c06c 20 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f of OP_Rewind to
1c06d 20 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e do the.** rewin
1c06e 64 69 6e 67 20 73 6f 20 74 68 61 74 20 74 68 65 ding so that the
1c06f 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
1c070 20 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 will be increme
1c071 6e 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72 nted and.** regr
1c072 65 73 73 69 6f 6e 20 74 65 73 74 73 20 63 61 6e ession tests can
1c073 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 determine wheth
1c074 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 er or not the op
1c075 74 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f timizer is.** co
1c076 72 72 65 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 rrectly optimizi
1c077 6e 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f ng out sorts..*/
1c078 0a 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b .case OP_Sort: {
1c079 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 /* jump
1c07a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
1c07b 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f _TEST. sqlite3_
1c07c 73 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 sort_count++;.
1c07d 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 sqlite3_search_c
1c07e 6f 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 ount--;.#endif.
1c07f 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 53 51 4c p->aCounter[SQL
1c080 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 ITE_STMTSTATUS_S
1c081 4f 52 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 ORT-1]++;. /* F
1c082 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f all through into
1c083 20 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a OP_Rewind */.}.
1c084 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e /* Opcode: Rewin
1c085 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a d P1 P2 * * *.**
1c086 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 .** The next use
1c087 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 of the Rowid or
1c088 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 Column or Next
1c089 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 instruction for
1c08a 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 P1 .** will refe
1c08b 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 r to the first e
1c08c 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 ntry in the data
1c08d 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e base table or in
1c08e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 dex..** If the t
1c08f 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 able or index is
1c090 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c empty and P2>0,
1c091 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 then jump immed
1c092 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a iately to P2..**
1c093 20 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 If P2 is 0 or i
1c094 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 f the table or i
1c095 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 ndex is not empt
1c096 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a y, fall through.
1c097 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 ** to the follow
1c098 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ing instruction.
1c099 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 77 69 .*/.case OP_Rewi
1c09a 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 nd: { /*
1c09b 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20 jump */. int i
1c09c 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 = pOp->p1;. Vdb
1c09d 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1c09e 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c09f 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73 int res;.. as
1c0a0 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1c0a1 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1c0a2 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d pC = p->apCsr[i]
1c0a3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d ;. assert( pC!=
1c0a4 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73 0 );. if( (pCrs
1c0a5 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 29 r = pC->pCursor)
1c0a6 21 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 !=0 ){. rc =
1c0a7 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 sqlite3BtreeFirs
1c0a8 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a t(pCrsr, &res);.
1c0a9 20 20 20 20 70 43 2d 3e 61 74 46 69 72 73 74 20 pC->atFirst
1c0aa 3d 20 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 = res==0 ?1:0;.
1c0ab 20 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d pC->deferredM
1c0ac 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 oveto = 0;. p
1c0ad 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1c0ae 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1c0af 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c pC->rowidIsVal
1c0b0 69 64 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b id = 0;. }else{
1c0b1 0a 20 20 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 . res = 1;.
1c0b2 7d 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 }. pC->nullRow
1c0b3 3d 20 28 75 38 29 72 65 73 3b 0a 20 20 61 73 73 = (u8)res;. ass
1c0b4 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 ert( pOp->p2>0 &
1c0b5 26 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 & pOp->p2<p->nOp
1c0b6 20 29 3b 0a 20 20 69 66 28 20 72 65 73 20 29 7b );. if( res ){
1c0b7 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1c0b8 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 2 - 1;. }. bre
1c0b9 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1c0ba 3a 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a : Next P1 P2 * *
1c0bb 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 *.**.** Advance
1c0bc 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 cursor P1 so th
1c0bd 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 at it points to
1c0be 74 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61 74 the next key/dat
1c0bf 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a a pair in its.**
1c0c0 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e table or index.
1c0c1 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e If there are n
1c0c2 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75 65 o more key/value
1c0c3 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c pairs then fall
1c0c4 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 through.** to t
1c0c5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 he following ins
1c0c6 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 truction. But i
1c0c7 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 64 76 f the cursor adv
1c0c8 61 6e 63 65 20 77 61 73 20 73 75 63 63 65 73 73 ance was success
1c0c9 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d ful,.** jump imm
1c0ca 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a ediately to P2..
1c0cb 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 72 **.** The P1 cur
1c0cc 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 20 sor must be for
1c0cd 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f a real table, no
1c0ce 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 t a pseudo-table
1c0cf 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1c0d0 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 : Prev.*/./* Opc
1c0d1 6f 64 65 3a 20 50 72 65 76 20 50 31 20 50 32 20 ode: Prev P1 P2
1c0d2 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b * * *.**.** Back
1c0d3 20 75 70 20 63 75 72 73 6f 72 20 50 31 20 73 6f up cursor P1 so
1c0d4 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 that it points
1c0d5 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 to the previous
1c0d6 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e key/data pair in
1c0d7 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 its.** table or
1c0d8 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72 index. If ther
1c0d9 65 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75 73 e is no previous
1c0da 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 key/value pairs
1c0db 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 then fall throu
1c0dc 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c gh.** to the fol
1c0dd 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 lowing instructi
1c0de 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 on. But if the
1c0df 63 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77 61 cursor backup wa
1c0e0 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a s successful,.**
1c0e1 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c jump immediatel
1c0e2 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 y to P2..**.** T
1c0e3 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 he P1 cursor mus
1c0e4 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 t be for a real
1c0e5 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 table, not a pse
1c0e6 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 udo-table..*/.ca
1c0e7 73 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20 20 se OP_Prev:
1c0e8 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1c0e9 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 case OP_Next: {
1c0ea 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a /* jump *
1c0eb 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1c0ec 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a pC;. BtCursor *
1c0ed 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 pCrsr;. int res
1c0ee 3b 0a 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 ;.. CHECK_FOR_I
1c0ef 4e 54 45 52 52 55 50 54 3b 0a 20 20 61 73 73 65 NTERRUPT;. asse
1c0f0 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1c0f1 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1c0f2 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 rsor );. pC = p
1c0f3 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1c0f4 3b 0a 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b ;. if( pC==0 ){
1c0f5 0a 20 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 . break; /*
1c0f6 53 65 65 20 74 69 63 6b 65 74 20 23 32 32 37 33 See ticket #2273
1c0f7 20 2a 2f 0a 20 20 7d 0a 20 20 70 43 72 73 72 20 */. }. pCrsr
1c0f8 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 = pC->pCursor;.
1c0f9 20 61 73 73 65 72 74 28 20 70 43 72 73 72 20 29 assert( pCrsr )
1c0fa 3b 0a 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 61 ;. res = 1;. a
1c0fb 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 ssert( pC->defer
1c0fc 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a redMoveto==0 );.
1c0fd 20 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f rc = pOp->opco
1c0fe 64 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71 de==OP_Next ? sq
1c0ff 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 lite3BtreeNext(p
1c100 43 72 73 72 2c 20 26 72 65 73 29 20 3a 0a 20 20 Crsr, &res) :.
1c101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c102 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
1c103 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
1c104 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 (pCrsr, &res);.
1c105 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 pC->nullRow = (
1c106 75 38 29 72 65 73 3b 0a 20 20 70 43 2d 3e 63 61 u8)res;. pC->ca
1c107 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1c108 45 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 72 E_STALE;. if( r
1c109 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 es==0 ){. pc
1c10a 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1c10b 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 if( pOp->p5 )
1c10c 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70 p->aCounter[pOp
1c10d 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 ->p5-1]++;.#ifde
1c10e 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
1c10f 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 sqlite3_search
1c110 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 _count++;.#endif
1c111 0a 20 20 7d 0a 20 20 70 43 2d 3e 72 6f 77 69 64 . }. pC->rowid
1c112 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 62 IsValid = 0;. b
1c113 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1c114 64 65 3a 20 49 64 78 49 6e 73 65 72 74 20 50 31 de: IdxInsert P1
1c115 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1c116 20 52 65 67 69 73 74 65 72 20 50 32 20 68 6f 6c Register P2 hol
1c117 64 73 20 61 20 53 51 4c 20 69 6e 64 65 78 20 6b ds a SQL index k
1c118 65 79 20 6d 61 64 65 20 75 73 69 6e 67 20 74 68 ey made using th
1c119 65 0a 2a 2a 20 4d 61 6b 65 49 64 78 52 65 63 20 e.** MakeIdxRec
1c11a 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 54 instructions. T
1c11b 68 69 73 20 6f 70 63 6f 64 65 20 77 72 69 74 65 his opcode write
1c11c 73 20 74 68 61 74 20 6b 65 79 0a 2a 2a 20 69 6e s that key.** in
1c11d 74 6f 20 74 68 65 20 69 6e 64 65 78 20 50 31 2e to the index P1.
1c11e 20 20 44 61 74 61 20 66 6f 72 20 74 68 65 20 65 Data for the e
1c11f 6e 74 72 79 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a ntry is nil..**.
1c120 2a 2a 20 50 33 20 69 73 20 61 20 66 6c 61 67 20 ** P3 is a flag
1c121 74 68 61 74 20 70 72 6f 76 69 64 65 73 20 61 20 that provides a
1c122 68 69 6e 74 20 74 6f 20 74 68 65 20 62 2d 74 72 hint to the b-tr
1c123 65 65 20 6c 61 79 65 72 20 74 68 61 74 20 74 68 ee layer that th
1c124 69 73 0a 2a 2a 20 69 6e 73 65 72 74 20 69 73 20 is.** insert is
1c125 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 likely to be an
1c126 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 append..**.** Th
1c127 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f is instruction o
1c128 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 69 6e nly works for in
1c129 64 69 63 65 73 2e 20 20 54 68 65 20 65 71 75 69 dices. The equi
1c12a 76 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 valent instructi
1c12b 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61 62 6c 65 73 on.** for tables
1c12c 20 69 73 20 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a is OP_Insert..*
1c12d 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 49 6e 73 /.case OP_IdxIns
1c12e 65 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a ert: { /*
1c12f 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20 69 20 in2 */. int i
1c130 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 = pOp->p1;. Vdb
1c131 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1c132 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c133 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1c134 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
1c135 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 ;. assert( p->a
1c136 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 pCsr[i]!=0 );.
1c137 61 73 73 65 72 74 28 20 70 49 6e 32 2d 3e 66 6c assert( pIn2->fl
1c138 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 ags & MEM_Blob )
1c139 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d ;. if( (pCrsr =
1c13a 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b (pC = p->apCsr[
1c13b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 i])->pCursor)!=0
1c13c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1c13d 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 pC->isTable==0 )
1c13e 3b 0a 20 20 20 20 72 63 20 3d 20 45 78 70 61 6e ;. rc = Expan
1c13f 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 20 dBlob(pIn2);.
1c140 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1c141 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 OK ){. int
1c142 6e 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 6e 3b 0a nKey = pIn2->n;.
1c143 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1c144 20 2a 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a *zKey = pIn2->z
1c145 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
1c146 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 ite3BtreeInsert(
1c147 70 43 72 73 72 2c 20 7a 4b 65 79 2c 20 6e 4b 65 pCrsr, zKey, nKe
1c148 79 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 y, "", 0, 0, pOp
1c149 2d 3e 70 33 29 3b 0a 20 20 20 20 20 20 61 73 73 ->p3);. ass
1c14a 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 ert( pC->deferre
1c14b 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 dMoveto==0 );.
1c14c 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 pC->cacheSta
1c14d 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
1c14e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 E;. }. }. b
1c14f 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1c150 64 65 3a 20 49 64 78 44 65 6c 65 74 65 20 50 31 de: IdxDelete P1
1c151 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1c152 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 The content of
1c153 50 33 20 72 65 67 69 73 74 65 72 73 20 73 74 61 P3 registers sta
1c154 72 74 69 6e 67 20 61 74 20 72 65 67 69 73 74 65 rting at registe
1c155 72 20 50 32 20 66 6f 72 6d 0a 2a 2a 20 61 6e 20 r P2 form.** an
1c156 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b unpacked index k
1c157 65 79 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 ey. This opcode
1c158 72 65 6d 6f 76 65 73 20 74 68 61 74 20 65 6e 74 removes that ent
1c159 72 79 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 ry from the .**
1c15a 69 6e 64 65 78 20 6f 70 65 6e 65 64 20 62 79 20 index opened by
1c15b 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2f 0a 63 61 cursor P1..*/.ca
1c15c 73 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 3a se OP_IdxDelete:
1c15d 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 {. int i = pOp
1c15e 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 ->p1;. VdbeCurs
1c15f 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 or *pC;. BtCurs
1c160 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 or *pCrsr;. ass
1c161 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 ert( pOp->p3>0 )
1c162 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1c163 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 >p2>0 && pOp->p2
1c164 2b 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 +pOp->p3<=p->nMe
1c165 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 m );. assert( i
1c166 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 >=0 && i<p->nCur
1c167 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 sor );. assert(
1c168 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 p->apCsr[i]!=0
1c169 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 );. if( (pCrsr
1c16a 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 = (pC = p->apCsr
1c16b 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d [i])->pCursor)!=
1c16c 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 0 ){. int res
1c16d 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 ;. UnpackedRe
1c16e 63 6f 72 64 20 72 3b 0a 20 20 20 20 72 2e 70 4b cord r;. r.pK
1c16f 65 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 eyInfo = pC->pKe
1c170 79 49 6e 66 6f 3b 0a 20 20 20 20 72 2e 6e 46 69 yInfo;. r.nFi
1c171 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e eld = (u16)pOp->
1c172 70 33 3b 0a 20 20 20 20 72 2e 66 6c 61 67 73 20 p3;. r.flags
1c173 3d 20 30 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20 = 0;. r.aMem
1c174 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1c175 70 32 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 p2];. rc = sq
1c176 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f lite3BtreeMoveto
1c177 55 6e 70 61 63 6b 65 64 28 70 43 72 73 72 2c 20 Unpacked(pCrsr,
1c178 26 72 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b &r, 0, 0, &res);
1c179 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1c17a 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 ITE_OK && res==0
1c17b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
1c17c 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 qlite3BtreeDelet
1c17d 65 28 70 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a e(pCrsr);. }.
1c17e 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e assert( pC->
1c17f 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d deferredMoveto==
1c180 30 20 29 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 0 );. pC->cac
1c181 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1c182 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 _STALE;. }. br
1c183 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1c184 65 3a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 e: IdxRowid P1 P
1c185 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 2 * * *.**.** Wr
1c186 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 ite into registe
1c187 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 r P2 an integer
1c188 77 68 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 which is the las
1c189 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 t entry in the r
1c18a 65 63 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 ecord at.** the
1c18b 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 end of the index
1c18c 20 6b 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 key pointed to
1c18d 62 79 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 by cursor P1. T
1c18e 68 69 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 his integer shou
1c18f 6c 64 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 ld be.** the row
1c190 69 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 id of the table
1c191 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 entry to which t
1c192 68 69 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 his index entry
1c193 70 6f 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 points..**.** Se
1c194 65 20 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d e also: Rowid, M
1c195 61 6b 65 49 64 78 52 65 63 2e 0a 2a 2f 0a 63 61 akeIdxRec..*/.ca
1c196 73 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 se OP_IdxRowid:
1c197 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
1c198 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1c199 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 e */. int i = p
1c19a 4f 70 2d 3e 70 31 3b 0a 20 20 42 74 43 75 72 73 Op->p1;. BtCurs
1c19b 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 56 64 62 or *pCrsr;. Vdb
1c19c 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 eCursor *pC;..
1c19d 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
1c19e 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a i<p->nCursor );.
1c19f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 assert( p->apC
1c1a0 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 sr[i]!=0 );. if
1c1a1 28 20 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d ( (pCrsr = (pC =
1c1a2 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 p->apCsr[i])->p
1c1a3 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 Cursor)!=0 ){.
1c1a4 20 20 69 36 34 20 72 6f 77 69 64 3b 0a 0a 20 20 i64 rowid;..
1c1a5 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 assert( pC->de
1c1a6 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 ferredMoveto==0
1c1a7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1c1a8 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b C->isTable==0 );
1c1a9 0a 20 20 20 20 69 66 28 20 21 70 43 2d 3e 6e 75 . if( !pC->nu
1c1aa 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72 llRow ){. r
1c1ab 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 c = sqlite3VdbeI
1c1ac 64 78 52 6f 77 69 64 28 70 43 72 73 72 2c 20 26 dxRowid(pCrsr, &
1c1ad 72 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 rowid);. if
1c1ae 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1c1af 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 ){. goto
1c1b0 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
1c1b1 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 or;. }.
1c1b2 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1c1b3 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1c1b4 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 . pOut->u.i
1c1b5 20 3d 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a = rowid;. }.
1c1b6 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1c1b7 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 47 45 /* Opcode: IdxGE
1c1b8 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
1c1b9 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 **.** The P4 reg
1c1ba 69 73 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 ister values beg
1c1bb 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 inning with P3 f
1c1bc 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 orm an unpacked
1c1bd 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 index .** key th
1c1be 61 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 at omits the ROW
1c1bf 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 ID. Compare thi
1c1c0 73 20 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 s key value agai
1c1c1 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a nst the index .*
1c1c2 2a 20 74 68 61 74 20 50 31 20 69 73 20 63 75 72 * that P1 is cur
1c1c3 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 rently pointing
1c1c4 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 to, ignoring the
1c1c5 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 ROWID on the P1
1c1c6 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 index..**.** If
1c1c7 20 74 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e the P1 index en
1c1c8 74 72 79 20 69 73 20 67 72 65 61 74 65 72 20 74 try is greater t
1c1c9 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
1c1ca 74 68 65 20 6b 65 79 20 76 61 6c 75 65 0a 2a 2a the key value.**
1c1cb 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 then jump to P2
1c1cc 2e 20 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c . Otherwise fal
1c1cd 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 l through to the
1c1ce 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
1c1cf 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 n..**.** If P5 i
1c1d0 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 s non-zero then
1c1d1 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 the key value is
1c1d2 20 69 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e increased by an
1c1d3 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 epsilon .** pri
1c1d4 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 or to the compar
1c1d5 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 ison. This make
1c1d6 20 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b the opcode work
1c1d7 20 6c 69 6b 65 20 49 64 78 47 54 20 65 78 63 65 like IdxGT exce
1c1d8 70 74 0a 2a 2a 20 74 68 61 74 20 69 66 20 74 68 pt.** that if th
1c1d9 65 20 6b 65 79 20 66 72 6f 6d 20 72 65 67 69 73 e key from regis
1c1da 74 65 72 20 50 33 20 69 73 20 61 20 70 72 65 66 ter P3 is a pref
1c1db 69 78 20 6f 66 20 74 68 65 20 6b 65 79 20 69 6e ix of the key in
1c1dc 20 74 68 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 the cursor,.**
1c1dd 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 66 61 the result is fa
1c1de 6c 73 65 20 77 68 65 72 65 61 73 20 69 74 20 77 lse whereas it w
1c1df 6f 75 6c 64 20 62 65 20 74 72 75 65 20 77 69 74 ould be true wit
1c1e0 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f h IdxGT..*/./* O
1c1e1 70 63 6f 64 65 3a 20 49 64 78 4c 54 20 50 31 20 pcode: IdxLT P1
1c1e2 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a P2 P3 * P5.**.**
1c1e3 20 54 68 65 20 50 34 20 72 65 67 69 73 74 65 72 The P4 register
1c1e4 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e values beginnin
1c1e5 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 g with P3 form a
1c1e6 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 n unpacked index
1c1e7 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d .** key that om
1c1e8 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 its the ROWID.
1c1e9 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 Compare this key
1c1ea 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 value against t
1c1eb 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 he index .** tha
1c1ec 74 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c t P1 is currentl
1c1ed 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 y pointing to, i
1c1ee 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 gnoring the ROWI
1c1ef 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 D on the P1 inde
1c1f0 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 x..**.** If the
1c1f1 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 P1 index entry i
1c1f2 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 s less than the
1c1f3 6b 65 79 20 76 61 6c 75 65 20 74 68 65 6e 20 6a key value then j
1c1f4 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 ump to P2..** Ot
1c1f5 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 herwise fall thr
1c1f6 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 ough to the next
1c1f7 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a instruction..**
1c1f8 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e .** If P5 is non
1c1f9 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b -zero then the k
1c1fa 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 ey value is incr
1c1fb 65 61 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 eased by an epsi
1c1fc 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 74 6f lon prior .** to
1c1fd 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e the comparison.
1c1fe 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 This makes the
1c1ff 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b opcode work lik
1c200 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 e IdxLE..*/.case
1c201 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20 20 20 20 OP_IdxLT:
1c202 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 /* jump, in3
1c203 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 47 */.case OP_IdxG
1c204 45 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a E: { /* j
1c205 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e ump, in3 */. in
1c206 74 20 69 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 t i= pOp->p1;.
1c207 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c208 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
1c209 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 && i<p->nCursor
1c20a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
1c20b 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 apCsr[i]!=0 );.
1c20c 20 69 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 if( (pC = p->ap
1c20d 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 Csr[i])->pCursor
1c20e 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 !=0 ){. int r
1c20f 65 73 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 es;. Unpacked
1c210 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 61 73 Record r;. as
1c211 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 sert( pC->deferr
1c212 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 edMoveto==0 );.
1c213 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1c214 70 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 p5==0 || pOp->p5
1c215 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==1 );. asser
1c216 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1c217 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 P4_INT32 );.
1c218 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43 2d r.pKeyInfo = pC-
1c219 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 72 >pKeyInfo;. r
1c21a 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 .nField = (u16)p
1c21b 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 Op->p4.i;. if
1c21c 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 ( pOp->p5 ){.
1c21d 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 r.flags = UNP
1c21e 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 7c 20 ACKED_INCRKEY |
1c21f 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f UNPACKED_IGNORE_
1c220 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65 6c 73 65 ROWID;. }else
1c221 7b 0a 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 {. r.flags
1c222 3d 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 = UNPACKED_IGNOR
1c223 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 E_ROWID;. }.
1c224 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e r.aMem = &p->
1c225 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1c226 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1c227 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 dbeIdxKeyCompare
1c228 28 70 43 2c 20 26 72 2c 20 26 72 65 73 29 3b 0a (pC, &r, &res);.
1c229 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 if( pOp->opc
1c22a 6f 64 65 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b ode==OP_IdxLT ){
1c22b 0a 20 20 20 20 20 20 72 65 73 20 3d 20 2d 72 65 . res = -re
1c22c 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 s;. }else{.
1c22d 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1c22e 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 47 >opcode==OP_IdxG
1c22f 45 20 29 3b 0a 20 20 20 20 20 20 72 65 73 2b 2b E );. res++
1c230 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1c231 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 res>0 ){. p
1c232 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 c = pOp->p2 - 1
1c233 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 ;. }. }. br
1c234 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1c235 65 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 32 e: Destroy P1 P2
1c236 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 P3 * *.**.** De
1c237 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64 lete an entire d
1c238 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 atabase table or
1c239 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f index whose roo
1c23a 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 t page in the da
1c23b 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 tabase.** file i
1c23c 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a s given by P1..*
1c23d 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 *.** The table b
1c23e 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69 eing destroyed i
1c23f 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 s in the main da
1c240 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 tabase file if P
1c241 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d 3==0. If.** P3=
1c242 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c =1 then the tabl
1c243 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 e to be clear is
1c244 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 in the auxiliar
1c245 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a y database file.
1c246 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20 ** that is used
1c247 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 to store tables
1c248 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 create using CRE
1c249 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 ATE TEMPORARY TA
1c24a 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 BLE..**.** If AU
1c24b 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 TOVACUUM is enab
1c24c 6c 65 64 20 74 68 65 6e 20 69 74 20 69 73 20 70 led then it is p
1c24d 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f ossible that ano
1c24e 74 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a ther root page.*
1c24f 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64 * might be moved
1c250 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 into the newly
1c251 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67 deleted root pag
1c252 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 e in order to ke
1c253 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 ep all.** root p
1c254 61 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20 ages contiguous
1c255 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
1c256 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1c257 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a . The former.**
1c258 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f value of the ro
1c259 6f 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76 ot page that mov
1c25a 65 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62 ed - its value b
1c25b 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f efore the move o
1c25c 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20 ccurred -.** is
1c25d 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1c25e 65 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61 er P2. If no pa
1c25f 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 ge .** movement
1c260 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62 65 was required (be
1c261 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 cause the table
1c262 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61 being dropped wa
1c263 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68 s already .** th
1c264 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 e last one in th
1c265 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65 6e e database) then
1c266 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 a zero is store
1c267 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 d in register P2
1c268 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 ..** If AUTOVACU
1c269 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 74 UM is disabled t
1c26a 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 hen a zero is st
1c26b 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
1c26c 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 P2..**.** See a
1c26d 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61 lso: Clear.*/.ca
1c26e 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b se OP_Destroy: {
1c26f 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1c270 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 release */. int
1c271 20 69 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 iMoved;. int i
1c272 43 6e 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c Cnt;.#ifndef SQL
1c273 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1c274 54 41 42 4c 45 0a 20 20 56 64 62 65 20 2a 70 56 TABLE. Vdbe *pV
1c275 64 62 65 3b 0a 20 20 69 43 6e 74 20 3d 20 30 3b dbe;. iCnt = 0;
1c276 0a 20 20 66 6f 72 28 70 56 64 62 65 3d 64 62 2d . for(pVdbe=db-
1c277 3e 70 56 64 62 65 3b 20 70 56 64 62 65 3b 20 70 >pVdbe; pVdbe; p
1c278 56 64 62 65 3d 70 56 64 62 65 2d 3e 70 4e 65 78 Vdbe=pVdbe->pNex
1c279 74 29 7b 0a 20 20 20 20 69 66 28 20 70 56 64 62 t){. if( pVdb
1c27a 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d e->magic==VDBE_M
1c27b 41 47 49 43 5f 52 55 4e 20 26 26 20 70 56 64 62 AGIC_RUN && pVdb
1c27c 65 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 3c e->inVtabMethod<
1c27d 32 20 26 26 20 70 56 64 62 65 2d 3e 70 63 3e 3d 2 && pVdbe->pc>=
1c27e 30 20 29 7b 0a 20 20 20 20 20 20 69 43 6e 74 2b 0 ){. iCnt+
1c27f 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c +;. }. }.#el
1c280 73 65 0a 20 20 69 43 6e 74 20 3d 20 64 62 2d 3e se. iCnt = db->
1c281 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 0a 23 activeVdbeCnt;.#
1c282 65 6e 64 69 66 0a 20 20 69 66 28 20 69 43 6e 74 endif. if( iCnt
1c283 3e 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 >1 ){. rc = S
1c284 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 QLITE_LOCKED;.
1c285 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e p->errorAction
1c286 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d = OE_Abort;. }
1c287 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 44 else{. int iD
1c288 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 b = pOp->p3;.
1c289 20 61 73 73 65 72 74 28 20 69 43 6e 74 3d 3d 31 assert( iCnt==1
1c28a 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1c28b 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 (p->btreeMask &
1c28c 28 31 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a (1<<iDb))!=0 );.
1c28d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1c28e 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 64 BtreeDropTable(d
1c28f 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 2c b->aDb[iDb].pBt,
1c290 20 70 4f 70 2d 3e 70 31 2c 20 26 69 4d 6f 76 65 pOp->p1, &iMove
1c291 64 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 d);. MemSetTy
1c292 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1c293 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d _Int);. pOut-
1c294 3e 75 2e 69 20 3d 20 69 4d 6f 76 65 64 3b 0a 23 >u.i = iMoved;.#
1c295 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1c296 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
1c297 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1c298 5f 4f 4b 20 26 26 20 69 4d 6f 76 65 64 21 3d 30 _OK && iMoved!=0
1c299 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1c29a 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 26 3RootPageMoved(&
1c29b 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2c 20 69 4d db->aDb[iDb], iM
1c29c 6f 76 65 64 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a oved, pOp->p1);.
1c29d 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
1c29e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1c29f 4f 70 63 6f 64 65 3a 20 43 6c 65 61 72 20 50 31 Opcode: Clear P1
1c2a0 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44 65 6c P2 P3.**.** Del
1c2a1 65 74 65 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 73 ete all contents
1c2a2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1c2a3 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1c2a4 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 0a whose root page.
1c2a5 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ** in the databa
1c2a6 73 65 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e se file is given
1c2a7 20 62 79 20 50 31 2e 20 20 42 75 74 2c 20 75 6e by P1. But, un
1c2a8 6c 69 6b 65 20 44 65 73 74 72 6f 79 2c 20 64 6f like Destroy, do
1c2a9 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 not.** remove t
1c2aa 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 he table or inde
1c2ab 78 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 x from the datab
1c2ac 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
1c2ad 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 The table being
1c2ae 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 20 clear is in the
1c2af 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1c2b0 6c 65 20 69 66 20 50 32 3d 3d 30 2e 20 20 49 66 le if P2==0. If
1c2b1 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20 74 .** P2==1 then t
1c2b2 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63 he table to be c
1c2b3 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 20 61 lear is in the a
1c2b4 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 uxiliary databas
1c2b5 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69 e file.** that i
1c2b6 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 s used to store
1c2b7 74 61 62 6c 65 73 20 63 72 65 61 74 65 20 75 73 tables create us
1c2b8 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f ing CREATE TEMPO
1c2b9 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a RARY TABLE..**.*
1c2ba 2a 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 * If the P3 valu
1c2bb 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 e is non-zero, t
1c2bc 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 72 65 hen the table re
1c2bd 66 65 72 65 64 20 74 6f 20 6d 75 73 74 20 62 65 fered to must be
1c2be 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 74 61 an.** intkey ta
1c2bf 62 6c 65 20 28 61 6e 20 53 51 4c 20 74 61 62 6c ble (an SQL tabl
1c2c0 65 2c 20 6e 6f 74 20 61 6e 20 69 6e 64 65 78 29 e, not an index)
1c2c1 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
1c2c2 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 0a 2a he row change .*
1c2c3 2a 20 63 6f 75 6e 74 20 69 73 20 69 6e 63 72 65 * count is incre
1c2c4 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 mented by the nu
1c2c5 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 mber of rows in
1c2c6 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
1c2c7 63 6c 65 61 72 65 64 2e 20 0a 2a 2a 20 49 66 20 cleared. .** If
1c2c8 50 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 P3 is greater th
1c2c9 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 an zero, then th
1c2ca 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 e value stored i
1c2cb 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 n register P3 is
1c2cc 0a 2a 2a 20 61 6c 73 6f 20 69 6e 63 72 65 6d 65 .** also increme
1c2cd 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 nted by the numb
1c2ce 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 er of rows in th
1c2cf 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c e table being cl
1c2d0 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 eared..**.** See
1c2d1 20 61 6c 73 6f 3a 20 44 65 73 74 72 6f 79 0a 2a also: Destroy.*
1c2d2 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 65 61 72 3a /.case OP_Clear:
1c2d3 20 7b 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 {. int nChange
1c2d4 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
1c2d5 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 (p->btreeMask &
1c2d6 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d 30 (1<<pOp->p2))!=0
1c2d7 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
1c2d8 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c e3BtreeClearTabl
1c2d9 65 28 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 e(. db->aDb
1c2da 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 70 [pOp->p2].pBt, p
1c2db 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e 70 33 Op->p1, (pOp->p3
1c2dc 20 3f 20 26 6e 43 68 61 6e 67 65 20 3a 20 30 29 ? &nChange : 0)
1c2dd 0a 20 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d . );. if( pOp-
1c2de 3e 70 33 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 43 >p3 ){. p->nC
1c2df 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65 hange += nChange
1c2e0 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 ;. if( pOp->p
1c2e1 33 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 3>0 ){. p->
1c2e2 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e aMem[pOp->p3].u.
1c2e3 69 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 i += nChange;.
1c2e4 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b }. }. break;
1c2e5 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 .}../* Opcode: C
1c2e6 72 65 61 74 65 54 61 62 6c 65 20 50 31 20 50 32 reateTable P1 P2
1c2e7 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c * * *.**.** All
1c2e8 6f 63 61 74 65 20 61 20 6e 65 77 20 74 61 62 6c ocate a new tabl
1c2e9 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 e in the main da
1c2ea 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 tabase file if P
1c2eb 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a 1==0 or in the.*
1c2ec 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 * auxiliary data
1c2ed 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d base file if P1=
1c2ee 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 61 =1 or in an atta
1c2ef 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69 66 ched database if
1c2f0 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74 65 .** P1>1. Write
1c2f1 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e the root page n
1c2f2 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 umber of the new
1c2f3 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 table into.** r
1c2f4 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a egister P2.**.**
1c2f5 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 The difference
1c2f6 62 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65 20 between a table
1c2f7 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73 20 and an index is
1c2f8 74 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20 6d this: A table m
1c2f9 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34 2d ust.** have a 4-
1c2fa 62 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65 79 byte integer key
1c2fb 20 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61 72 and can have ar
1c2fc 62 69 74 72 61 72 79 20 64 61 74 61 2e 20 20 41 bitrary data. A
1c2fd 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20 61 n index.** has a
1c2fe 6e 20 61 72 62 69 74 72 61 72 79 20 6b 65 79 20 n arbitrary key
1c2ff 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a but no data..**.
1c300 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72 65 ** See also: Cre
1c301 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f ateIndex.*/./* O
1c302 70 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e 64 pcode: CreateInd
1c303 65 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ex P1 P2 * * *.*
1c304 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 *.** Allocate a
1c305 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68 65 new index in the
1c306 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
1c307 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72 20 ile if P1==0 or
1c308 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69 in the.** auxili
1c309 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c ary database fil
1c30a 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69 6e e if P1==1 or in
1c30b 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 an attached dat
1c30c 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e 31 abase if.** P1>1
1c30d 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 6f 6f . Write the roo
1c30e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 t page number of
1c30f 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 the new table i
1c310 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 nto.** register
1c311 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f P2..**.** See do
1c312 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f cumentation on O
1c313 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66 6f P_CreateTable fo
1c314 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
1c315 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 ormation..*/.cas
1c316 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 e OP_CreateIndex
1c317 3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 : /*
1c318 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1c319 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 74 */.case OP_Creat
1c31a 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 20 eTable: {
1c31b 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1c31c 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 70 lease */. int p
1c31d 67 6e 6f 20 3d 20 30 3b 0a 20 20 69 6e 74 20 66 gno = 0;. int f
1c31e 6c 61 67 73 3b 0a 20 20 44 62 20 2a 70 44 62 3b lags;. Db *pDb;
1c31f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1c320 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1c321 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 <db->nDb );. as
1c322 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d sert( (p->btreeM
1c323 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 ask & (1<<pOp->p
1c324 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 1))!=0 );. pDb
1c325 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e = &db->aDb[pOp->
1c326 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 p1];. assert( p
1c327 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 Db->pBt!=0 );.
1c328 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d if( pOp->opcode=
1c329 3d 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 =OP_CreateTable
1c32a 29 7b 0a 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 ){. /* flags
1c32b 3d 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b 20 = BTREE_INTKEY;
1c32c 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 42 */. flags = B
1c32d 54 52 45 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 TREE_LEAFDATA|BT
1c32e 52 45 45 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 REE_INTKEY;. }e
1c32f 6c 73 65 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d lse{. flags =
1c330 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b BTREE_ZERODATA;
1c331 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 . }. rc = sqli
1c332 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 te3BtreeCreateTa
1c333 62 6c 65 28 70 44 62 2d 3e 70 42 74 2c 20 26 70 ble(pDb->pBt, &p
1c334 67 6e 6f 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 gno, flags);. p
1c335 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 67 6e 6f 3b Out->u.i = pgno;
1c336 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1c337 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 g(pOut, MEM_Int)
1c338 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1c339 20 4f 70 63 6f 64 65 3a 20 50 61 72 73 65 53 63 Opcode: ParseSc
1c33a 68 65 6d 61 20 50 31 20 50 32 20 2a 20 50 34 20 hema P1 P2 * P4
1c33b 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 64 *.**.** Read and
1c33c 20 70 61 72 73 65 20 61 6c 6c 20 65 6e 74 72 69 parse all entri
1c33d 65 73 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 es from the SQLI
1c33e 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 TE_MASTER table
1c33f 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 0a 2a of database P1.*
1c340 2a 20 74 68 61 74 20 6d 61 74 63 68 20 74 68 65 * that match the
1c341 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 50 34 WHERE clause P4
1c342 2e 20 20 50 32 20 69 73 20 74 68 65 20 22 66 6f . P2 is the "fo
1c343 72 63 65 22 20 66 6c 61 67 2e 20 20 20 41 6c 77 rce" flag. Alw
1c344 61 79 73 20 64 6f 0a 2a 2a 20 74 68 65 20 70 61 ays do.** the pa
1c345 72 73 69 6e 67 20 69 66 20 50 32 20 69 73 20 74 rsing if P2 is t
1c346 72 75 65 2e 20 20 49 66 20 50 32 20 69 73 20 66 rue. If P2 is f
1c347 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 alse, then this
1c348 72 6f 75 74 69 6e 65 20 69 73 20 61 0a 2a 2a 20 routine is a.**
1c349 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 63 68 no-op if the sch
1c34a 65 6d 61 20 69 73 20 6e 6f 74 20 63 75 72 72 65 ema is not curre
1c34b 6e 74 6c 79 20 6c 6f 61 64 65 64 2e 20 20 49 6e ntly loaded. In
1c34c 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 other words, if
1c34d 20 50 32 0a 2a 2a 20 69 73 20 66 61 6c 73 65 2c P2.** is false,
1c34e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 the SQLITE_MAST
1c34f 45 52 20 74 61 62 6c 65 20 69 73 20 6f 6e 6c 79 ER table is only
1c350 20 70 61 72 73 65 64 20 69 66 20 74 68 65 20 72 parsed if the r
1c351 65 73 74 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 est of the.** sc
1c352 68 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 hema is already
1c353 6c 6f 61 64 65 64 20 69 6e 74 6f 20 74 68 65 20 loaded into the
1c354 73 79 6d 62 6f 6c 20 74 61 62 6c 65 2e 0a 2a 2a symbol table..**
1c355 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 .** This opcode
1c356 69 6e 76 6f 6b 65 73 20 74 68 65 20 70 61 72 73 invokes the pars
1c357 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e er to create a n
1c358 65 77 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 ew virtual machi
1c359 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20 72 75 6e 73 ne,.** then runs
1c35a 20 74 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c the new virtual
1c35b 20 6d 61 63 68 69 6e 65 2e 20 20 49 74 20 69 73 machine. It is
1c35c 20 74 68 75 73 20 61 20 72 65 2d 65 6e 74 72 61 thus a re-entra
1c35d 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 nt opcode..*/.ca
1c35e 73 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d se OP_ParseSchem
1c35f 61 3a 20 7b 0a 20 20 63 68 61 72 20 2a 7a 53 71 a: {. char *zSq
1c360 6c 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 l;. int iDb = p
1c361 4f 70 2d 3e 70 31 3b 0a 20 20 63 6f 6e 73 74 20 Op->p1;. const
1c362 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 char *zMaster;.
1c363 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44 61 InitData initDa
1c364 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 ta;.. assert( i
1c365 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d Db>=0 && iDb<db-
1c366 3e 6e 44 62 20 29 3b 0a 20 20 69 66 28 20 21 70 >nDb );. if( !p
1c367 4f 70 2d 3e 70 32 20 26 26 20 21 44 62 48 61 73 Op->p2 && !DbHas
1c368 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 Property(db, iDb
1c369 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 , DB_SchemaLoade
1c36a 64 29 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b d) ){. break;
1c36b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d . }. zMaster =
1c36c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 SCHEMA_TABLE(iD
1c36d 62 29 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 64 b);. initData.d
1c36e 62 20 3d 20 64 62 3b 0a 20 20 69 6e 69 74 44 61 b = db;. initDa
1c36f 74 61 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 ta.iDb = pOp->p1
1c370 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 70 7a 45 ;. initData.pzE
1c371 72 72 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72 rrMsg = &p->zErr
1c372 4d 73 67 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 Msg;. zSql = sq
1c373 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c lite3MPrintf(db,
1c374 0a 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61 . "SELECT na
1c375 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 me, rootpage, sq
1c376 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 20 57 l FROM '%q'.%s W
1c377 48 45 52 45 20 25 73 22 2c 0a 20 20 20 20 20 64 HERE %s",. d
1c378 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
1c379 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 4f 70 2d e, zMaster, pOp-
1c37a 3e 70 34 2e 7a 29 3b 0a 20 20 69 66 28 20 7a 53 >p4.z);. if( zS
1c37b 71 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f ql==0 ) goto no_
1c37c 6d 65 6d 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c mem;. (void)sql
1c37d 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1c37e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d );. assert( db-
1c37f 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 3b >init.busy==0 );
1c380 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 . db->init.busy
1c381 20 3d 20 31 3b 0a 20 20 69 6e 69 74 44 61 74 61 = 1;. initData
1c382 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b .rc = SQLITE_OK;
1c383 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e . assert( !db->
1c384 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
1c385 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 rc = sqlite3_e
1c386 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71 xec(db, zSql, sq
1c387 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 lite3InitCallbac
1c388 6b 2c 20 26 69 6e 69 74 44 61 74 61 2c 20 30 29 k, &initData, 0)
1c389 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1c38a 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69 6e 69 TE_OK ) rc = ini
1c38b 74 44 61 74 61 2e 72 63 3b 0a 20 20 73 71 6c 69 tData.rc;. sqli
1c38c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 te3DbFree(db, zS
1c38d 71 6c 29 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e ql);. db->init.
1c38e 62 75 73 79 20 3d 20 30 3b 0a 20 20 28 76 6f 69 busy = 0;. (voi
1c38f 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f d)sqlite3SafetyO
1c390 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d n(db);. if( rc=
1c391 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b =SQLITE_NOMEM ){
1c392 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d . goto no_mem
1c393 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 20 20 ;. }. break;
1c394 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .}..#if !defined
1c395 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 (SQLITE_OMIT_ANA
1c396 4c 59 5a 45 29 20 26 26 20 21 64 65 66 69 6e 65 LYZE) && !define
1c397 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 d(SQLITE_OMIT_PA
1c398 52 53 45 52 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a RSER)./* Opcode:
1c399 20 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 50 31 LoadAnalysis P1
1c39a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 * * * *.**.** R
1c39b 65 61 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 ead the sqlite_s
1c39c 74 61 74 31 20 74 61 62 6c 65 20 66 6f 72 20 64 tat1 table for d
1c39d 61 74 61 62 61 73 65 20 50 31 20 61 6e 64 20 6c atabase P1 and l
1c39e 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a oad the content.
1c39f 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 ** of that table
1c3a0 20 69 6e 74 6f 20 74 68 65 20 69 6e 74 65 72 6e into the intern
1c3a1 61 6c 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 al index hash ta
1c3a2 62 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 ble. This will
1c3a3 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 61 6e 61 cause.** the ana
1c3a4 6c 79 73 69 73 20 74 6f 20 62 65 20 75 73 65 64 lysis to be used
1c3a5 20 77 68 65 6e 20 70 72 65 70 61 72 69 6e 67 20 when preparing
1c3a6 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 71 all subsequent q
1c3a7 75 65 72 69 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 ueries..*/.case
1c3a8 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 3a OP_LoadAnalysis:
1c3a9 20 7b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 {. int iDb = p
1c3aa 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 Op->p1;. assert
1c3ab 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c ( iDb>=0 && iDb<
1c3ac 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 72 63 20 db->nDb );. rc
1c3ad 3d 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 = sqlite3Analysi
1c3ae 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29 3b 0a sLoad(db, iDb);.
1c3af 20 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e break; .}.#en
1c3b0 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
1c3b1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c SQLITE_OMIT_ANAL
1c3b2 59 5a 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 YZE) && !defined
1c3b3 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 (SQLITE_OMIT_PAR
1c3b4 53 45 52 29 20 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 SER) */../* Opc
1c3b5 6f 64 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 ode: DropTable P
1c3b6 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 1 * * P4 *.**.**
1c3b7 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 Remove the inte
1c3b8 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 rnal (in-memory)
1c3b9 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 data structures
1c3ba 20 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a that describe.*
1c3bb 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 * the table name
1c3bc 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 d P4 in database
1c3bd 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 P1. This is ca
1c3be 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 61 62 lled after a tab
1c3bf 6c 65 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 le.** is dropped
1c3c0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 in order to kee
1c3c1 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 p the internal r
1c3c2 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
1c3c3 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 the.** schema c
1c3c4 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 onsistent with w
1c3c5 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a hat is on disk..
1c3c6 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 */.case OP_DropT
1c3c7 61 62 6c 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 able: {. sqlite
1c3c8 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
1c3c9 54 61 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 Table(db, pOp->p
1c3ca 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 1, pOp->p4.z);.
1c3cb 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1c3cc 63 6f 64 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 code: DropIndex
1c3cd 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a P1 * * P4 *.**.*
1c3ce 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 * Remove the int
1c3cf 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 ernal (in-memory
1c3d0 29 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 ) data structure
1c3d1 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 0a s that describe.
1c3d2 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d ** the index nam
1c3d3 65 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 ed P4 in databas
1c3d4 65 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 e P1. This is c
1c3d5 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69 alled after an i
1c3d6 6e 64 65 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 ndex.** is dropp
1c3d7 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b ed in order to k
1c3d8 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c eep the internal
1c3d9 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1c3da 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 of the.** schema
1c3db 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 consistent with
1c3dc 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b what is on disk
1c3dd 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f ..*/.case OP_Dro
1c3de 70 49 6e 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 pIndex: {. sqli
1c3df 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 te3UnlinkAndDele
1c3e0 74 65 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d teIndex(db, pOp-
1c3e1 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b >p1, pOp->p4.z);
1c3e2 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1c3e3 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 Opcode: DropTrig
1c3e4 67 65 72 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a ger P1 * * P4 *.
1c3e5 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 **.** Remove the
1c3e6 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 internal (in-me
1c3e7 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63 mory) data struc
1c3e8 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72 tures that descr
1c3e9 69 62 65 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 ibe.** the trigg
1c3ea 65 72 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 er named P4 in d
1c3eb 61 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 atabase P1. Thi
1c3ec 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 s is called afte
1c3ed 72 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 r a trigger.** i
1c3ee 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 s dropped in ord
1c3ef 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 er to keep the i
1c3f0 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e nternal represen
1c3f1 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a tation of the.**
1c3f2 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 schema consiste
1c3f3 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20 nt with what is
1c3f4 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 on disk..*/.case
1c3f5 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 3a OP_DropTrigger:
1c3f6 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 {. sqlite3Unli
1c3f7 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 nkAndDeleteTrigg
1c3f8 65 72 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 er(db, pOp->p1,
1c3f9 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 pOp->p4.z);. br
1c3fa 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 eak;.}...#ifndef
1c3fb 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
1c3fc 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 EGRITY_CHECK./*
1c3fd 4f 70 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 Opcode: Integrit
1c3fe 79 43 6b 20 50 31 20 50 32 20 50 33 20 2a 20 50 yCk P1 P2 P3 * P
1c3ff 35 0a 2a 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 5.**.** Do an an
1c400 61 6c 79 73 69 73 20 6f 66 20 74 68 65 20 63 75 alysis of the cu
1c401 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 rrently open dat
1c402 61 62 61 73 65 2e 20 20 53 74 6f 72 65 20 69 6e abase. Store in
1c403 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 .** register P1
1c404 74 68 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 the text of an e
1c405 72 72 6f 72 20 6d 65 73 73 61 67 65 20 64 65 73 rror message des
1c406 63 72 69 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 cribing any prob
1c407 6c 65 6d 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 lems..** If no p
1c408 72 6f 62 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e roblems are foun
1c409 64 2c 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 d, store a NULL
1c40a 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a in register P1..
1c40b 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 **.** The regist
1c40c 65 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 er P3 contains t
1c40d 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
1c40e 72 20 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 r of allowed err
1c40f 6f 72 73 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 ors..** At most
1c410 72 65 67 28 50 33 29 20 65 72 72 6f 72 73 20 77 reg(P3) errors w
1c411 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e ill be reported.
1c412 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 .** In other wor
1c413 64 73 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 ds, the analysis
1c414 20 73 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 stops as soon a
1c415 73 20 72 65 67 28 50 31 29 20 65 72 72 6f 72 73 s reg(P1) errors
1c416 20 61 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 are .** seen.
1c417 52 65 67 28 50 31 29 20 69 73 20 75 70 64 61 74 Reg(P1) is updat
1c418 65 64 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62 ed with the numb
1c419 65 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d er of errors rem
1c41a 61 69 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 aining..**.** Th
1c41b 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 e root page numb
1c41c 65 72 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 ers of all table
1c41d 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 s in the databas
1c41e 65 20 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a e are integer.**
1c41f 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50 stored in reg(P
1c420 31 29 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72 1), reg(P1+1), r
1c421 65 67 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 eg(P1+2), ....
1c422 54 68 65 72 65 20 61 72 65 20 50 32 20 74 61 62 There are P2 tab
1c423 6c 65 73 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a les.** total..**
1c424 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 74 .** If P5 is not
1c425 20 7a 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b zero, the check
1c426 20 69 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 is done on the
1c427 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 auxiliary databa
1c428 73 65 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 se.** file, not
1c429 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1c42a 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e file..**.** Th
1c42b 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 is opcode is use
1c42c 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 d to implement t
1c42d 68 65 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 he integrity_che
1c42e 63 6b 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 ck pragma..*/.ca
1c42f 73 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 se OP_IntegrityC
1c430 6b 3a 20 7b 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 k: {. int nRoot
1c431 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ; /* Number
1c432 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 63 68 of tables to ch
1c433 65 63 6b 2e 20 20 28 4e 75 6d 62 65 72 20 6f 66 eck. (Number of
1c434 20 72 6f 6f 74 20 70 61 67 65 73 2e 29 20 2a 2f root pages.) */
1c435 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 3b 20 20 . int *aRoot;
1c436 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 72 /* Array of r
1c437 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72 73 20 ootpage numbers
1c438 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20 62 65 for tables to be
1c439 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20 69 6e checked */. in
1c43a 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 2f 2a t j; /*
1c43b 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
1c43c 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 . int nErr;
1c43d 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1c43e 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65 64 20 errors reported
1c43f 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 */. char *z;
1c440 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 /* Text of
1c441 74 68 65 20 65 72 72 6f 72 20 72 65 70 6f 72 74 the error report
1c442 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 6e 45 72 72 */. Mem *pnErr
1c443 3b 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 ; /* Registe
1c444 72 20 6b 65 65 70 69 6e 67 20 74 72 61 63 6b 20 r keeping track
1c445 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e of errors remain
1c446 69 6e 67 20 2a 2f 0a 20 20 0a 20 20 6e 52 6f 6f ing */. . nRoo
1c447 74 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 t = pOp->p2;. a
1c448 73 73 65 72 74 28 20 6e 52 6f 6f 74 3e 30 20 29 ssert( nRoot>0 )
1c449 3b 0a 20 20 61 52 6f 6f 74 20 3d 20 73 71 6c 69 ;. aRoot = sqli
1c44a 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
1c44b 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 b, sizeof(int)*(
1c44c 6e 52 6f 6f 74 2b 31 29 20 29 3b 0a 20 20 69 66 nRoot+1) );. if
1c44d 28 20 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 ( aRoot==0 ) got
1c44e 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 o no_mem;. asse
1c44f 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 rt( pOp->p3>0 &&
1c450 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 pOp->p3<=p->nMe
1c451 6d 20 29 3b 0a 20 20 70 6e 45 72 72 20 3d 20 26 m );. pnErr = &
1c452 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1c453 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 6e 45 ;. assert( (pnE
1c454 72 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f rr->flags & MEM_
1c455 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 Int)!=0 );. ass
1c456 65 72 74 28 20 28 70 6e 45 72 72 2d 3e 66 6c 61 ert( (pnErr->fla
1c457 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 gs & (MEM_Str|ME
1c458 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 M_Blob))==0 );.
1c459 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d pIn1 = &p->aMem
1c45a 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 [pOp->p1];. for
1c45b 28 6a 3d 30 3b 20 6a 3c 6e 52 6f 6f 74 3b 20 6a (j=0; j<nRoot; j
1c45c 2b 2b 29 7b 0a 20 20 20 20 61 52 6f 6f 74 5b 6a ++){. aRoot[j
1c45d 5d 20 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 ] = (int)sqlite3
1c45e 56 64 62 65 49 6e 74 56 61 6c 75 65 28 26 70 49 VdbeIntValue(&pI
1c45f 6e 31 5b 6a 5d 29 3b 0a 20 20 7d 0a 20 20 61 52 n1[j]);. }. aR
1c460 6f 6f 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 61 73 oot[j] = 0;. as
1c461 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 3c 64 62 sert( pOp->p5<db
1c462 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
1c463 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1c464 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 29 29 & (1<<pOp->p5))
1c465 21 3d 30 20 29 3b 0a 20 20 7a 20 3d 20 73 71 6c !=0 );. z = sql
1c466 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 ite3BtreeIntegri
1c467 74 79 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 5b tyCheck(db->aDb[
1c468 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 61 52 pOp->p5].pBt, aR
1c469 6f 6f 74 2c 20 6e 52 6f 6f 74 2c 0a 20 20 20 20 oot, nRoot,.
1c46a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c46b 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e (in
1c46c 74 29 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 6e t)pnErr->u.i, &n
1c46d 45 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 Err);. sqlite3D
1c46e 62 46 72 65 65 28 64 62 2c 20 61 52 6f 6f 74 29 bFree(db, aRoot)
1c46f 3b 0a 20 20 70 6e 45 72 72 2d 3e 75 2e 69 20 2d ;. pnErr->u.i -
1c470 3d 20 6e 45 72 72 3b 0a 20 20 73 71 6c 69 74 65 = nErr;. sqlite
1c471 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
1c472 70 49 6e 31 29 3b 0a 20 20 69 66 28 20 6e 45 72 pIn1);. if( nEr
1c473 72 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 r==0 ){. asse
1c474 72 74 28 20 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65 rt( z==0 );. }e
1c475 6c 73 65 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a lse if( z==0 ){.
1c476 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1c477 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
1c478 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
1c479 74 72 28 70 49 6e 31 2c 20 7a 2c 20 2d 31 2c 20 tr(pIn1, z, -1,
1c47a 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 73 71 6c SQLITE_UTF8, sql
1c47b 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a ite3_free);. }.
1c47c 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1c47d 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 73 BSIZE(pIn1);. s
1c47e 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1c47f 45 6e 63 6f 64 69 6e 67 28 70 49 6e 31 2c 20 65 Encoding(pIn1, e
1c480 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 62 72 65 61 ncoding);. brea
1c481 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 k;.}.#endif /* S
1c482 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
1c483 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f RITY_CHECK */../
1c484 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 * Opcode: RowSet
1c485 41 64 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a Add P1 P2 * * *.
1c486 2a 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 **.** Insert the
1c487 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 68 integer value h
1c488 65 6c 64 20 62 79 20 72 65 67 69 73 74 65 72 20 eld by register
1c489 50 32 20 69 6e 74 6f 20 61 20 62 6f 6f 6c 65 61 P2 into a boolea
1c48a 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 65 6c 64 20 n index.** held
1c48b 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a in register P1..
1c48c 2a 2a 0a 2a 2a 20 41 6e 20 61 73 73 65 72 74 69 **.** An asserti
1c48d 6f 6e 20 66 61 69 6c 73 20 69 66 20 50 32 20 69 on fails if P2 i
1c48e 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 s not an integer
1c48f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 ..*/.case OP_Row
1c490 53 65 74 41 64 64 3a 20 7b 20 20 20 20 20 20 20 SetAdd: {
1c491 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20 4d 65 6d 20 /* in2 */. Mem
1c492 2a 70 49 64 78 3b 0a 20 20 4d 65 6d 20 2a 70 56 *pIdx;. Mem *pV
1c493 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f al;. assert( pO
1c494 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e p->p1>0 && pOp->
1c495 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p1<=p->nMem );.
1c496 20 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 6d pIdx = &p->aMem
1c497 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1c498 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 ert( pOp->p2>0 &
1c499 26 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d & pOp->p2<=p->nM
1c49a 65 6d 20 29 3b 0a 20 20 70 56 61 6c 20 3d 20 26 em );. pVal = &
1c49b 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d p->aMem[pOp->p2]
1c49c 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 56 61 ;. assert( (pVa
1c49d 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 l->flags & MEM_I
1c49e 6e 74 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 nt)!=0 );. if(
1c49f 28 70 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d (pIdx->flags & M
1c4a0 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b EM_RowSet)==0 ){
1c4a1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1c4a2 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49 64 MemSetRowSet(pId
1c4a3 78 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49 64 x);. if( (pId
1c4a4 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 x->flags & MEM_R
1c4a5 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f owSet)==0 ) goto
1c4a6 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 no_mem;. }. s
1c4a7 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 qlite3RowSetInse
1c4a8 72 74 28 70 49 64 78 2d 3e 75 2e 70 52 6f 77 53 rt(pIdx->u.pRowS
1c4a9 65 74 2c 20 70 56 61 6c 2d 3e 75 2e 69 29 3b 0a et, pVal->u.i);.
1c4aa 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c4ab 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 52 65 61 pcode: RowSetRea
1c4ac 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a d P1 P2 P3 * *.*
1c4ad 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 *.** Extract the
1c4ae 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 20 smallest value
1c4af 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 from boolean ind
1c4b0 65 78 20 50 31 20 61 6e 64 20 70 75 74 20 74 68 ex P1 and put th
1c4b1 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 0a 2a 2a at value into.**
1c4b2 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 20 4f register P3. O
1c4b3 72 2c 20 69 66 20 62 6f 6f 6c 65 61 6e 20 69 6e r, if boolean in
1c4b4 64 65 78 20 50 31 20 69 73 20 69 6e 69 74 69 61 dex P1 is initia
1c4b5 6c 6c 79 20 65 6d 70 74 79 2c 20 6c 65 61 76 65 lly empty, leave
1c4b6 20 50 33 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 P3.** unchanged
1c4b7 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 69 6e 73 and jump to ins
1c4b8 74 72 75 63 74 69 6f 6e 20 50 32 2e 0a 2a 2f 0a truction P2..*/.
1c4b9 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 52 65 case OP_RowSetRe
1c4ba 61 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a ad: { /* j
1c4bb 75 6d 70 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 4d ump, out3 */. M
1c4bc 65 6d 20 2a 70 49 64 78 3b 0a 20 20 69 36 34 20 em *pIdx;. i64
1c4bd 76 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 val;. assert( p
1c4be 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d Op->p1>0 && pOp-
1c4bf 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p1<=p->nMem );.
1c4c0 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 CHECK_FOR_INTE
1c4c1 52 52 55 50 54 3b 0a 20 20 70 49 64 78 20 3d 20 RRUPT;. pIdx =
1c4c2 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 &p->aMem[pOp->p1
1c4c3 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e ];. pOut = &p->
1c4c4 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1c4c5 20 69 66 28 20 28 70 49 64 78 2d 3e 66 6c 61 67 if( (pIdx->flag
1c4c6 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d s & MEM_RowSet)=
1c4c7 3d 30 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 =0 . || sqlite
1c4c8 33 52 6f 77 53 65 74 4e 65 78 74 28 70 49 64 78 3RowSetNext(pIdx
1c4c9 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 26 76 61 ->u.pRowSet, &va
1c4ca 6c 29 3d 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f l)==0. ){. /
1c4cb 2a 20 54 68 65 20 62 6f 6f 6c 65 61 6e 20 69 6e * The boolean in
1c4cc 64 65 78 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a dex is empty */.
1c4cd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1c4ce 65 6d 53 65 74 4e 75 6c 6c 28 70 49 64 78 29 3b emSetNull(pIdx);
1c4cf 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1c4d0 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 2 - 1;. }else{.
1c4d1 20 20 20 20 2f 2a 20 41 20 76 61 6c 75 65 20 77 /* A value w
1c4d2 61 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 74 as pulled from t
1c4d3 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 he index */.
1c4d4 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e assert( pOp->p3>
1c4d5 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 0 && pOp->p3<=p-
1c4d6 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 73 71 6c >nMem );. sql
1c4d7 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e ite3VdbeMemSetIn
1c4d8 74 36 34 28 70 4f 75 74 2c 20 76 61 6c 29 3b 0a t64(pOut, val);.
1c4d9 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1c4da 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1c4db 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a 20 OMIT_TRIGGER./*
1c4dc 4f 70 63 6f 64 65 3a 20 43 6f 6e 74 65 78 74 50 Opcode: ContextP
1c4dd 75 73 68 20 2a 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a ush * * * .**.**
1c4de 20 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e Save the curren
1c4df 74 20 56 64 62 65 20 63 6f 6e 74 65 78 74 20 73 t Vdbe context s
1c4e0 75 63 68 20 74 68 61 74 20 69 74 20 63 61 6e 20 uch that it can
1c4e1 62 65 20 72 65 73 74 6f 72 65 64 20 62 79 20 61 be restored by a
1c4e2 20 43 6f 6e 74 65 78 74 50 6f 70 0a 2a 2a 20 6f ContextPop.** o
1c4e3 70 63 6f 64 65 2e 20 54 68 65 20 63 6f 6e 74 65 pcode. The conte
1c4e4 78 74 20 73 74 6f 72 65 73 20 74 68 65 20 6c 61 xt stores the la
1c4e5 73 74 20 69 6e 73 65 72 74 20 72 6f 77 20 69 64 st insert row id
1c4e6 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 , the last state
1c4e7 6d 65 6e 74 20 63 68 61 6e 67 65 0a 2a 2a 20 63 ment change.** c
1c4e8 6f 75 6e 74 2c 20 61 6e 64 20 74 68 65 20 63 75 ount, and the cu
1c4e9 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 20 rrent statement
1c4ea 63 68 61 6e 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f change count..*/
1c4eb 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 74 65 78 74 .case OP_Context
1c4ec 50 75 73 68 3a 20 7b 0a 20 20 69 6e 74 20 69 20 Push: {. int i
1c4ed 3d 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 = p->contextStac
1c4ee 6b 54 6f 70 2b 2b 3b 0a 20 20 43 6f 6e 74 65 78 kTop++;. Contex
1c4ef 74 20 2a 70 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 t *pContext;..
1c4f0 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a assert( i>=0 );.
1c4f1 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 54 68 69 /* FIX ME: Thi
1c4f2 73 20 73 68 6f 75 6c 64 20 62 65 20 61 6c 6c 6f s should be allo
1c4f3 63 61 74 65 64 20 61 73 20 70 61 72 74 20 6f 66 cated as part of
1c4f4 20 74 68 65 20 76 64 62 65 20 61 74 20 63 6f 6d the vdbe at com
1c4f5 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 69 pile-time */. i
1c4f6 66 28 20 69 3e 3d 70 2d 3e 63 6f 6e 74 65 78 74 f( i>=p->context
1c4f7 53 74 61 63 6b 44 65 70 74 68 20 29 7b 0a 20 20 StackDepth ){.
1c4f8 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 p->contextStac
1c4f9 6b 44 65 70 74 68 20 3d 20 69 2b 31 3b 0a 20 20 kDepth = i+1;.
1c4fa 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 p->contextStac
1c4fb 6b 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 k = sqlite3DbRea
1c4fc 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70 llocOrFree(db, p
1c4fd 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 2c 0a ->contextStack,.
1c4fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c4ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c500 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 sizeof
1c501 28 43 6f 6e 74 65 78 74 29 2a 28 69 2b 31 29 29 (Context)*(i+1))
1c502 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 6f 6e ;. if( p->con
1c503 74 65 78 74 53 74 61 63 6b 3d 3d 30 20 29 20 67 textStack==0 ) g
1c504 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1c505 20 20 70 43 6f 6e 74 65 78 74 20 3d 20 26 70 2d pContext = &p-
1c506 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b 69 5d >contextStack[i]
1c507 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 6c 61 ;. pContext->la
1c508 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e 6c 61 stRowid = db->la
1c509 73 74 52 6f 77 69 64 3b 0a 20 20 70 43 6f 6e 74 stRowid;. pCont
1c50a 65 78 74 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 ext->nChange = p
1c50b 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 62 72 65 ->nChange;. bre
1c50c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1c50d 3a 20 43 6f 6e 74 65 78 74 50 6f 70 20 2a 20 2a : ContextPop * *
1c50e 20 2a 20 0a 2a 2a 0a 2a 2a 20 52 65 73 74 6f 72 * .**.** Restor
1c50f 65 20 74 68 65 20 56 64 62 65 20 63 6f 6e 74 65 e the Vdbe conte
1c510 78 74 20 74 6f 20 74 68 65 20 73 74 61 74 65 20 xt to the state
1c511 69 74 20 77 61 73 20 69 6e 20 77 68 65 6e 20 63 it was in when c
1c512 6f 6e 74 65 78 74 50 75 73 68 20 77 61 73 20 6c ontextPush was l
1c513 61 73 74 0a 2a 2a 20 65 78 65 63 75 74 65 64 2e ast.** executed.
1c514 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74 6f The context sto
1c515 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 res the last ins
1c516 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65 20 ert row id, the
1c517 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a last statement.*
1c518 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c 20 * change count,
1c519 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 and the current
1c51a 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 statement change
1c51b 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65 20 count..*/.case
1c51c 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 3a 20 7b OP_ContextPop: {
1c51d 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e . Context *pCon
1c51e 74 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e 74 65 text = &p->conte
1c51f 78 74 53 74 61 63 6b 5b 2d 2d 70 2d 3e 63 6f 6e xtStack[--p->con
1c520 74 65 78 74 53 74 61 63 6b 54 6f 70 5d 3b 0a 20 textStackTop];.
1c521 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6f 6e 74 assert( p->cont
1c522 65 78 74 53 74 61 63 6b 54 6f 70 3e 3d 30 20 29 extStackTop>=0 )
1c523 3b 0a 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 ;. db->lastRowi
1c524 64 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 6c 61 d = pContext->la
1c525 73 74 52 6f 77 69 64 3b 0a 20 20 70 2d 3e 6e 43 stRowid;. p->nC
1c526 68 61 6e 67 65 20 3d 20 70 43 6f 6e 74 65 78 74 hange = pContext
1c527 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 62 72 65 ->nChange;. bre
1c528 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1c529 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1c52a 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a MIT_TRIGGER */..
1c52b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1c52c 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e MIT_AUTOINCREMEN
1c52d 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 65 6d T./* Opcode: Mem
1c52e 4d 61 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a Max P1 P2 * * *.
1c52f 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 **.** Set the va
1c530 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 lue of register
1c531 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 P1 to the maximu
1c532 6d 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e 74 m of its current
1c533 20 76 61 6c 75 65 0a 2a 2a 20 61 6e 64 20 74 68 value.** and th
1c534 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1c535 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 ter P2..**.** Th
1c536 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 is instruction t
1c537 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72 20 69 hrows an error i
1c538 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c f the memory cel
1c539 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c l is not initial
1c53a 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 ly.** an integer
1c53b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 65 6d ..*/.case OP_Mem
1c53c 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a Max: { /*
1c53d 20 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a 20 20 73 in1, in2 */. s
1c53e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
1c53f 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 egerify(pIn1);.
1c540 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
1c541 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b ntegerify(pIn2);
1c542 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 . if( pIn1->u.i
1c543 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20 20 <pIn2->u.i){.
1c544 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 49 6e pIn1->u.i = pIn
1c545 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62 72 2->u.i;. }. br
1c546 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1c547 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1c548 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f OINCREMENT */../
1c549 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73 20 * Opcode: IfPos
1c54a 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1c54b 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f * If the value o
1c54c 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 f register P1 is
1c54d 20 31 20 6f 72 20 67 72 65 61 74 65 72 2c 20 6a 1 or greater, j
1c54e 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a ump to P2..**.**
1c54f 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 It is illegal t
1c550 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 o use this instr
1c551 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 uction on a regi
1c552 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a ster that does.*
1c553 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e * not contain an
1c554 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 integer. An as
1c555 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 sertion fault wi
1c556 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 ll result if you
1c557 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 try..*/.case OP
1c558 5f 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 20 20 _IfPos: {
1c559 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f /* jump, in1 */
1c55a 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d . assert( pIn1-
1c55b 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 >flags&MEM_Int )
1c55c 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e ;. if( pIn1->u.
1c55d 69 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d i>0 ){. pc =
1c55e 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1c55f 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1c560 20 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67 20 50 Opcode: IfNeg P
1c561 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1c562 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 If the value of
1c563 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1c564 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 less than zero,
1c565 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a jump to P2. .**.
1c566 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c ** It is illegal
1c567 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 to use this ins
1c568 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 truction on a re
1c569 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 gister that does
1c56a 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 .** not contain
1c56b 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 an integer. An
1c56c 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 assertion fault
1c56d 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 will result if y
1c56e 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 ou try..*/.case
1c56f 4f 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 20 20 OP_IfNeg: {
1c570 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 /* jump, in1
1c571 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e */. assert( pIn
1c572 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 1->flags&MEM_Int
1c573 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e );. if( pIn1->
1c574 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20 70 63 u.i<0 ){. pc
1c575 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1c576 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1c577 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a 65 72 /* Opcode: IfZer
1c578 6f 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a o P1 P2 * * *.**
1c579 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1c57a 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 of register P1
1c57b 69 73 20 65 78 61 63 74 6c 79 20 30 2c 20 6a 75 is exactly 0, ju
1c57c 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a mp to P2. .**.**
1c57d 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 It is illegal t
1c57e 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 o use this instr
1c57f 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 uction on a regi
1c580 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a ster that does.*
1c581 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e * not contain an
1c582 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 integer. An as
1c583 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 sertion fault wi
1c584 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 ll result if you
1c585 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 try..*/.case OP
1c586 5f 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20 _IfZero: {
1c587 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a /* jump, in1 *
1c588 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 /. assert( pIn1
1c589 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 ->flags&MEM_Int
1c58a 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 );. if( pIn1->u
1c58b 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 70 63 .i==0 ){. pc
1c58c 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1c58d 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1c58e 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 53 74 /* Opcode: AggSt
1c58f 65 70 20 2a 20 50 32 20 50 33 20 50 34 20 50 35 ep * P2 P3 P4 P5
1c590 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 .**.** Execute t
1c591 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e he step function
1c592 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 for an aggregat
1c593 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63 74 e. The.** funct
1c594 69 6f 6e 20 68 61 73 20 50 35 20 61 72 67 75 6d ion has P5 argum
1c595 65 6e 74 73 2e 20 20 20 50 34 20 69 73 20 61 20 ents. P4 is a
1c596 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 pointer to the F
1c597 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 uncDef.** struct
1c598 75 72 65 20 74 68 61 74 20 73 70 65 63 69 66 69 ure that specifi
1c599 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e es the function.
1c59a 20 20 55 73 65 20 72 65 67 69 73 74 65 72 0a 2a Use register.*
1c59b 2a 20 50 33 20 61 73 20 74 68 65 20 61 63 63 75 * P3 as the accu
1c59c 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 mulator..**.** T
1c59d 68 65 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20 he P5 arguments
1c59e 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 are taken from r
1c59f 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 69 egister P2 and i
1c5a0 74 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 ts.** successors
1c5a1 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 ..*/.case OP_Agg
1c5a2 53 74 65 70 3a 20 7b 0a 20 20 69 6e 74 20 6e 20 Step: {. int n
1c5a3 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 69 6e 74 = pOp->p5;. int
1c5a4 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c i;. Mem *pMem,
1c5a5 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 74 65 *pRec;. sqlite
1c5a6 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 3_context ctx;.
1c5a7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1c5a8 2a 61 70 56 61 6c 3b 0a 0a 20 20 61 73 73 65 72 *apVal;.. asser
1c5a9 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 70 52 65 t( n>=0 );. pRe
1c5aa 63 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 c = &p->aMem[pOp
1c5ab 2d 3e 70 32 5d 3b 0a 20 20 61 70 56 61 6c 20 3d ->p2];. apVal =
1c5ac 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 p->apArg;. ass
1c5ad 65 72 74 28 20 61 70 56 61 6c 20 7c 7c 20 6e 3d ert( apVal || n=
1c5ae 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b =0 );. for(i=0;
1c5af 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 52 65 63 2b i<n; i++, pRec+
1c5b0 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b 69 5d +){. apVal[i]
1c5b1 20 3d 20 70 52 65 63 3b 0a 20 20 20 20 73 74 6f = pRec;. sto
1c5b2 72 65 54 79 70 65 49 6e 66 6f 28 70 52 65 63 2c reTypeInfo(pRec,
1c5b3 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a encoding);. }.
1c5b4 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f ctx.pFunc = pO
1c5b5 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 61 p->p4.pFunc;. a
1c5b6 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 ssert( pOp->p3>0
1c5b7 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e && pOp->p3<=p->
1c5b8 6e 4d 65 6d 20 29 3b 0a 20 20 63 74 78 2e 70 4d nMem );. ctx.pM
1c5b9 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 70 2d 3e em = pMem = &p->
1c5ba 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1c5bb 20 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 63 74 pMem->n++;. ct
1c5bc 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f x.s.flags = MEM_
1c5bd 4e 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e 7a 20 Null;. ctx.s.z
1c5be 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d 61 = 0;. ctx.s.zMa
1c5bf 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 63 74 78 2e lloc = 0;. ctx.
1c5c0 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 63 74 s.xDel = 0;. ct
1c5c1 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 63 x.s.db = db;. c
1c5c2 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a tx.isError = 0;.
1c5c3 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b ctx.pColl = 0;
1c5c4 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75 6e 63 . if( ctx.pFunc
1c5c5 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
1c5c6 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 _FUNC_NEEDCOLL )
1c5c7 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f {. assert( pO
1c5c8 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20 p>p->aOp );.
1c5c9 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e assert( pOp[-1].
1c5ca 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 p4type==P4_COLLS
1c5cb 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 EQ );. assert
1c5cc 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 ( pOp[-1].opcode
1c5cd 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a ==OP_CollSeq );.
1c5ce 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 ctx.pColl =
1c5cf 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c pOp[-1].p4.pColl
1c5d0 3b 0a 20 20 7d 0a 20 20 28 63 74 78 2e 70 46 75 ;. }. (ctx.pFu
1c5d1 6e 63 2d 3e 78 53 74 65 70 29 28 26 63 74 78 2c nc->xStep)(&ctx,
1c5d2 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66 n, apVal);. if
1c5d3 28 20 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b ( ctx.isError ){
1c5d4 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1c5d5 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1c5d6 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c g, db, "%s", sql
1c5d7 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
1c5d8 26 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 &ctx.s));. rc
1c5d9 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a = ctx.isError;.
1c5da 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
1c5db 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 74 78 eMemRelease(&ctx
1c5dc 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a .s);. break;.}.
1c5dd 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 46 ./* Opcode: AggF
1c5de 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20 inal P1 P2 * P4
1c5df 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 *.**.** Execute
1c5e0 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 66 75 the finalizer fu
1c5e1 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67 nction for an ag
1c5e2 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 73 0a gregate. P1 is.
1c5e3 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f ** the memory lo
1c5e4 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 74 cation that is t
1c5e5 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 66 he accumulator f
1c5e6 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 74 65 or the aggregate
1c5e7 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 ..**.** P2 is th
1c5e8 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 e number of argu
1c5e9 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 73 ments that the s
1c5ea 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b tep function tak
1c5eb 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 73 20 es and.** P4 is
1c5ec 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1c5ed 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 68 69 FuncDef for thi
1c5ee 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 s function. The
1c5ef 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 P2.** argument
1c5f0 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 20 74 is not used by t
1c5f1 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 74 20 his opcode. It
1c5f2 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 74 6f is only there to
1c5f3 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a 2a 2a disambiguate.**
1c5f4 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
1c5f5 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 6e 67 can take varying
1c5f6 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 67 75 numbers of argu
1c5f7 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a 20 50 ments. The.** P
1c5f8 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 4 argument is on
1c5f9 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 74 68 ly needed for th
1c5fa 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 61 73 e degenerate cas
1c5fb 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 20 73 e where.** the s
1c5fc 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 tep function was
1c5fd 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 not previously
1c5fe 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 called..*/.case
1c5ff 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b 0a 20 OP_AggFinal: {.
1c600 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 Mem *pMem;. as
1c601 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 sert( pOp->p1>0
1c602 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e && pOp->p1<=p->n
1c603 4d 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 Mem );. pMem =
1c604 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 &p->aMem[pOp->p1
1c605 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d ];. assert( (pM
1c606 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e 28 4d 45 em->flags & ~(ME
1c607 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 67 29 29 M_Null|MEM_Agg))
1c608 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 ==0 );. rc = sq
1c609 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 lite3VdbeMemFina
1c60a 6c 69 7a 65 28 70 4d 65 6d 2c 20 70 4f 70 2d 3e lize(pMem, pOp->
1c60b 70 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69 66 28 p4.pFunc);. if(
1c60c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f rc==SQLITE_ERRO
1c60d 52 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 R ){. sqlite3
1c60e 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1c60f 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c rrMsg, db, "%s",
1c610 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1c611 65 78 74 28 70 4d 65 6d 29 29 3b 0a 20 20 7d 0a ext(pMem));. }.
1c612 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1c613 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d ngeEncoding(pMem
1c614 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 55 , encoding);. U
1c615 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1c616 5a 45 28 70 4d 65 6d 29 3b 0a 20 20 69 66 28 20 ZE(pMem);. if(
1c617 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f sqlite3VdbeMemTo
1c618 6f 42 69 67 28 70 4d 65 6d 29 20 29 7b 0a 20 20 oBig(pMem) ){.
1c619 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1c61a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1c61b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
1c61c 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d LITE_OMIT_VACUUM
1c61d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
1c61e 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 LITE_OMIT_ATTACH
1c61f 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 63 )./* Opcode: Vac
1c620 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a uum * * * * *.**
1c621 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20 65 .** Vacuum the e
1c622 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e 20 ntire database.
1c623 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69 6c This opcode wil
1c624 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76 69 l cause other vi
1c625 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 rtual.** machine
1c626 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 s to be created
1c627 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 79 and run. It may
1c628 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 66 not be called f
1c629 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20 rom within.** a
1c62a 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a transaction..*/.
1c62b 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a 20 case OP_Vacuum:
1c62c 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 {. if( sqlite3S
1c62d 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 afetyOff(db) ) g
1c62e 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1c62f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20 3d _misuse; . rc =
1c630 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75 sqlite3RunVacuu
1c631 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 m(&p->zErrMsg, d
1c632 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 b);. if( sqlite
1c633 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 3SafetyOn(db) )
1c634 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1c635 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 61 o_misuse;. brea
1c636 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 k;.}.#endif..#if
1c637 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1c638 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
1c639 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 63 )./* Opcode: Inc
1c63a 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a 20 rVacuum P1 P2 *
1c63b 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 * *.**.** Perfor
1c63c 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 m a single step
1c63d 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 of the increment
1c63e 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65 64 al vacuum proced
1c63f 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 ure on.** the P1
1c640 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 database. If th
1c641 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69 6e e vacuum has fin
1c642 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69 ished, jump to i
1c643 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 nstruction.** P2
1c644 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c . Otherwise, fal
1c645 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 l through to the
1c646 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
1c647 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e n..*/.case OP_In
1c648 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 20 crVacuum: {
1c649 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 /* jump */.
1c64a 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61 Btree *pBt;.. a
1c64b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1c64c 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 0 && pOp->p1<db-
1c64d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1c64e 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1c64f 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 & (1<<pOp->p1))!
1c650 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 =0 );. pBt = db
1c651 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 ->aDb[pOp->p1].p
1c652 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 Bt;. rc = sqlit
1c653 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 e3BtreeIncrVacuu
1c654 6d 28 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63 m(pBt);. if( rc
1c655 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b ==SQLITE_DONE ){
1c656 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1c657 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 2 - 1;. rc =
1c658 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1c659 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1c65a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 78 70 ../* Opcode: Exp
1c65b 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a ire P1 * * * *.*
1c65c 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65 63 6f *.** Cause preco
1c65d 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 mpiled statement
1c65e 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78 70 69 s to become expi
1c65f 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65 64 20 red. An expired
1c660 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66 61 69 statement.** fai
1c661 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 ls with an error
1c662 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f code of SQLITE_
1c663 53 43 48 45 4d 41 20 69 66 20 69 74 20 69 73 20 SCHEMA if it is
1c664 65 76 65 72 20 65 78 65 63 75 74 65 64 20 0a 2a ever executed .*
1c665 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33 5f 73 * (via sqlite3_s
1c666 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49 tep())..** .** I
1c667 66 20 50 31 20 69 73 20 30 2c 20 74 68 65 6e 20 f P1 is 0, then
1c668 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e all SQL statemen
1c669 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65 ts become expire
1c66a 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d d. If P1 is non-
1c66b 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e zero,.** then on
1c66c 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 ly the currently
1c66d 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 executing state
1c66e 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74 65 64 ment is affected
1c66f 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 78 . .*/.case OP_Ex
1c670 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20 21 70 pire: {. if( !p
1c671 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73 71 Op->p1 ){. sq
1c672 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 lite3ExpirePrepa
1c673 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 redStatements(db
1c674 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1c675 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a p->expired = 1;.
1c676 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1c677 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1c678 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
1c679 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61 62 6c ./* Opcode: Tabl
1c67a 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33 20 50 eLock P1 P2 P3 P
1c67b 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e 4 *.**.** Obtain
1c67c 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 61 72 a lock on a par
1c67d 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 54 ticular table. T
1c67e 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1c67f 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 is only used whe
1c680 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d n.** the shared-
1c681 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73 cache feature is
1c682 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a enabled. .**.**
1c683 20 49 66 20 50 31 20 69 73 20 20 74 68 65 20 69 If P1 is the i
1c684 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 ndex of the data
1c685 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e base in sqlite3.
1c686 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 aDb[] of the dat
1c687 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 63 abase.** on whic
1c688 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63 h the lock is ac
1c689 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c quired. A readl
1c68a 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ock is obtained
1c68b 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61 if P3==0 or.** a
1c68c 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50 write lock if P
1c68d 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63 3==1..**.** P2 c
1c68e 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74 ontains the root
1c68f 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 -page of the tab
1c690 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a le to lock..**.*
1c691 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20 * P4 contains a
1c692 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
1c693 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1c694 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 being locked. T
1c695 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 his is only.** u
1c696 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 sed to generate
1c697 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1c698 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e if the lock can
1c699 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e not be obtained.
1c69a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c .*/.case OP_Tabl
1c69b 65 4c 6f 63 6b 3a 20 7b 0a 20 20 69 6e 74 20 70 eLock: {. int p
1c69c 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 1 = pOp->p1; .
1c69d 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d u8 isWriteLock =
1c69e 20 28 75 38 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 (u8)pOp->p3;.
1c69f 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26 assert( p1>=0 &&
1c6a0 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 p1<db->nDb );.
1c6a1 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 assert( (p->btr
1c6a2 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 31 29 eeMask & (1<<p1)
1c6a3 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 )!=0 );. assert
1c6a4 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 ( isWriteLock==0
1c6a5 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d || isWriteLock=
1c6a6 3d 31 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c =1 );. rc = sql
1c6a7 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 ite3BtreeLockTab
1c6a8 6c 65 28 64 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 le(db->aDb[p1].p
1c6a9 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 69 73 57 Bt, pOp->p2, isW
1c6aa 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 69 66 28 riteLock);. if(
1c6ab 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b rc==SQLITE_LOCK
1c6ac 45 44 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 ED ){. const
1c6ad 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 char *z = pOp->p
1c6ae 34 2e 7a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4.z;. sqlite3
1c6af 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1c6b0 72 72 4d 73 67 2c 20 64 62 2c 20 22 64 61 74 61 rrMsg, db, "data
1c6b1 62 61 73 65 20 74 61 62 6c 65 20 69 73 20 6c 6f base table is lo
1c6b2 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 cked: %s", z);.
1c6b3 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1c6b4 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1c6b5 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
1c6b6 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1c6b7 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1c6b8 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a TABLE./* Opcode:
1c6b9 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a 20 50 34 VBegin * * * P4
1c6ba 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 *.**.** P4 may
1c6bb 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 be a pointer to
1c6bc 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 an sqlite3_vtab
1c6bd 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 73 6f structure. If so
1c6be 2c 20 63 61 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 , call the .** x
1c6bf 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 66 6f 72 Begin method for
1c6c0 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a that table..**.
1c6c1 2a 2a 20 41 6c 73 6f 2c 20 77 68 65 74 68 65 72 ** Also, whether
1c6c2 20 6f 72 20 6e 6f 74 20 50 34 20 69 73 20 73 65 or not P4 is se
1c6c3 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 t, check that th
1c6c4 69 73 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 is is not being
1c6c5 63 61 6c 6c 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 called from.** w
1c6c6 69 74 68 69 6e 20 61 20 63 61 6c 6c 62 61 63 6b ithin a callback
1c6c7 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 to a virtual ta
1c6c8 62 6c 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 ble xSync() meth
1c6c9 6f 64 2e 20 49 66 20 69 74 20 69 73 2c 20 73 65 od. If it is, se
1c6ca 74 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 63 t the.** error c
1c6cb 6f 64 65 20 74 6f 20 53 51 4c 49 54 45 5f 4c 4f ode to SQLITE_LO
1c6cc 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 CKED..*/.case OP
1c6cd 5f 56 42 65 67 69 6e 3a 20 7b 0a 20 20 73 71 6c _VBegin: {. sql
1c6ce 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1c6cf 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 = pOp->p4.pVtab
1c6d0 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1c6d1 56 74 61 62 42 65 67 69 6e 28 64 62 2c 20 70 56 VtabBegin(db, pV
1c6d2 74 61 62 29 3b 0a 20 20 69 66 28 20 70 56 74 61 tab);. if( pVta
1c6d3 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 b ){. sqlite3
1c6d4 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1c6d5 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a rrMsg);. p->z
1c6d6 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e ErrMsg = pVtab->
1c6d7 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 74 zErrMsg;. pVt
1c6d8 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b ab->zErrMsg = 0;
1c6d9 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1c6da 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1c6db 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1c6dc 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 LE */..#ifndef S
1c6dd 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1c6de 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 ALTABLE./* Opcod
1c6df 65 3a 20 56 43 72 65 61 74 65 20 50 31 20 2a 20 e: VCreate P1 *
1c6e0 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1c6e1 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 is the name of a
1c6e2 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 virtual table i
1c6e3 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 43 n database P1. C
1c6e4 61 6c 6c 20 74 68 65 20 78 43 72 65 61 74 65 20 all the xCreate
1c6e5 6d 65 74 68 6f 64 0a 2a 2a 20 66 6f 72 20 74 68 method.** for th
1c6e6 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 at table..*/.cas
1c6e7 65 20 4f 50 5f 56 43 72 65 61 74 65 3a 20 7b 0a e OP_VCreate: {.
1c6e8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 rc = sqlite3Vt
1c6e9 61 62 43 61 6c 6c 43 72 65 61 74 65 28 64 62 2c abCallCreate(db,
1c6ea 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 pOp->p1, pOp->p
1c6eb 34 2e 7a 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 4.z, &p->zErrMsg
1c6ec 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 );. break;.}.#e
1c6ed 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1c6ee 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1c6ef 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1c6f0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1c6f1 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a TABLE./* Opcode:
1c6f2 20 56 44 65 73 74 72 6f 79 20 50 31 20 2a 20 2a VDestroy P1 * *
1c6f3 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 P4 *.**.** P4 i
1c6f4 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 s the name of a
1c6f5 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e virtual table in
1c6f6 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 43 database P1. C
1c6f7 61 6c 6c 20 74 68 65 20 78 44 65 73 74 72 6f 79 all the xDestroy
1c6f8 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 method.** of th
1c6f9 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 at table..*/.cas
1c6fa 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 3a 20 7b e OP_VDestroy: {
1c6fb 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 . p->inVtabMeth
1c6fc 6f 64 20 3d 20 32 3b 0a 20 20 72 63 20 3d 20 73 od = 2;. rc = s
1c6fd 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 qlite3VtabCallDe
1c6fe 73 74 72 6f 79 28 64 62 2c 20 70 4f 70 2d 3e 70 stroy(db, pOp->p
1c6ff 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 1, pOp->p4.z);.
1c700 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 p->inVtabMethod
1c701 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d = 0;. break;.}
1c702 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1c703 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1c704 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 BLE */..#ifndef
1c705 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1c706 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f UALTABLE./* Opco
1c707 64 65 3a 20 56 4f 70 65 6e 20 50 31 20 2a 20 2a de: VOpen P1 * *
1c708 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 P4 *.**.** P4 i
1c709 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1c70a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f virtual table o
1c70b 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 bject, an sqlite
1c70c 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 3_vtab structure
1c70d 2e 0a 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 ..** P1 is a cur
1c70e 73 6f 72 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 sor number. Thi
1c70f 73 20 6f 70 63 6f 64 65 20 6f 70 65 6e 73 20 61 s opcode opens a
1c710 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 76 cursor to the v
1c711 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 irtual.** table
1c712 61 6e 64 20 73 74 6f 72 65 73 20 74 68 61 74 20 and stores that
1c713 63 75 72 73 6f 72 20 69 6e 20 50 31 2e 0a 2a 2f cursor in P1..*/
1c714 0a 63 61 73 65 20 4f 50 5f 56 4f 70 65 6e 3a 20 .case OP_VOpen:
1c715 7b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a {. VdbeCursor *
1c716 70 43 75 72 20 3d 20 30 3b 0a 20 20 73 71 6c 69 pCur = 0;. sqli
1c717 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
1c718 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 30 *pVtabCursor = 0
1c719 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 ;.. sqlite3_vta
1c71a 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e b *pVtab = pOp->
1c71b 70 34 2e 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 p4.pVtab;. sqli
1c71c 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
1c71d 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d ule = (sqlite3_m
1c71e 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70 odule *)pVtab->p
1c71f 4d 6f 64 75 6c 65 3b 0a 0a 20 20 61 73 73 65 72 Module;.. asser
1c720 74 28 70 56 74 61 62 20 26 26 20 70 4d 6f 64 75 t(pVtab && pModu
1c721 6c 65 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 le);. if( sqlit
1c722 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 e3SafetyOff(db)
1c723 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1c724 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 _to_misuse;. rc
1c725 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 = pModule->xOpe
1c726 6e 28 70 56 74 61 62 2c 20 26 70 56 74 61 62 43 n(pVtab, &pVtabC
1c727 75 72 73 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 ursor);. sqlite
1c728 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1c729 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 ErrMsg);. p->zE
1c72a 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a rrMsg = pVtab->z
1c72b 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d ErrMsg;. pVtab-
1c72c 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
1c72d 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1c72e 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 yOn(db) ) goto a
1c72f 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1c730 73 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 se;. if( SQLITE
1c731 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f _OK==rc ){. /
1c732 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 6c * Initialize sql
1c733 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
1c734 20 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20 base class */.
1c735 20 20 20 70 56 74 61 62 43 75 72 73 6f 72 2d 3e pVtabCursor->
1c736 70 56 74 61 62 20 3d 20 70 56 74 61 62 3b 0a 0a pVtab = pVtab;..
1c737 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 73 /* Initialis
1c738 65 20 76 64 62 65 20 63 75 72 73 6f 72 20 6f 62 e vdbe cursor ob
1c739 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70 43 75 72 ject */. pCur
1c73a 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f = allocateCurso
1c73b 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 26 70 r(p, pOp->p1, &p
1c73c 4f 70 5b 2d 31 5d 2c 20 2d 31 2c 20 30 29 3b 0a Op[-1], -1, 0);.
1c73d 20 20 20 20 69 66 28 20 70 43 75 72 20 29 7b 0a if( pCur ){.
1c73e 20 20 20 20 20 20 70 43 75 72 2d 3e 70 56 74 61 pCur->pVta
1c73f 62 43 75 72 73 6f 72 20 3d 20 70 56 74 61 62 43 bCursor = pVtabC
1c740 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 43 75 ursor;. pCu
1c741 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74 r->pModule = pVt
1c742 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d abCursor->pVtab-
1c743 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65 >pModule;. }e
1c744 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d lse{. db->m
1c745 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
1c746 0a 20 20 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e . pModule->
1c747 78 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 xClose(pVtabCurs
1c748 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 or);. }. }.
1c749 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1c74a 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1c74b 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a VIRTUALTABLE */.
1c74c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1c74d 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1c74e 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 46 69 E./* Opcode: VFi
1c74f 6c 74 65 72 20 50 31 20 50 32 20 50 33 20 50 34 lter P1 P2 P3 P4
1c750 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 *.**.** P1 is a
1c751 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 75 cursor opened u
1c752 73 69 6e 67 20 56 4f 70 65 6e 2e 20 20 50 32 20 sing VOpen. P2
1c753 69 73 20 61 6e 20 61 64 64 72 65 73 73 20 74 6f is an address to
1c754 20 6a 75 6d 70 20 74 6f 20 69 66 0a 2a 2a 20 74 jump to if.** t
1c755 68 65 20 66 69 6c 74 65 72 65 64 20 72 65 73 75 he filtered resu
1c756 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 2e lt set is empty.
1c757 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 65 69 74 .**.** P4 is eit
1c758 68 65 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74 her NULL or a st
1c759 72 69 6e 67 20 74 68 61 74 20 77 61 73 20 67 65 ring that was ge
1c75a 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 78 nerated by the x
1c75b 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 BestIndex.** met
1c75c 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c hod of the modul
1c75d 65 2e 20 20 54 68 65 20 69 6e 74 65 72 70 72 65 e. The interpre
1c75e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 50 34 tation of the P4
1c75f 20 73 74 72 69 6e 67 20 69 73 20 6c 65 66 74 0a string is left.
1c760 2a 2a 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 ** to the module
1c761 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e implementation.
1c762 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
1c763 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 de invokes the x
1c764 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 6f 6e Filter method on
1c765 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
1c766 6c 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 le specified.**
1c767 62 79 20 50 31 2e 20 20 54 68 65 20 69 6e 74 65 by P1. The inte
1c768 67 65 72 20 71 75 65 72 79 20 70 6c 61 6e 20 70 ger query plan p
1c769 61 72 61 6d 65 74 65 72 20 74 6f 20 78 46 69 6c arameter to xFil
1c76a 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 69 6e ter is stored in
1c76b 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 2e register.** P3.
1c76c 20 52 65 67 69 73 74 65 72 20 50 33 2b 31 20 73 Register P3+1 s
1c76d 74 6f 72 65 73 20 74 68 65 20 61 72 67 63 20 70 tores the argc p
1c76e 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 70 arameter to be p
1c76f 61 73 73 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 assed to the.**
1c770 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e 20 xFilter method.
1c771 52 65 67 69 73 74 65 72 73 20 50 33 2b 32 2e 2e Registers P3+2..
1c772 50 33 2b 31 2b 61 72 67 63 20 61 72 65 20 74 68 P3+1+argc are th
1c773 65 20 61 72 67 63 0a 2a 2a 20 61 64 64 69 74 69 e argc.** additi
1c774 6f 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20 onal parameters
1c775 77 68 69 63 68 20 61 72 65 20 70 61 73 73 65 64 which are passed
1c776 20 74 6f 0a 2a 2a 20 78 46 69 6c 74 65 72 20 61 to.** xFilter a
1c777 73 20 61 72 67 76 2e 20 52 65 67 69 73 74 65 72 s argv. Register
1c778 20 50 33 2b 32 20 62 65 63 6f 6d 65 73 20 61 72 P3+2 becomes ar
1c779 67 76 5b 30 5d 20 77 68 65 6e 20 70 61 73 73 65 gv[0] when passe
1c77a 64 20 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a d to xFilter..**
1c77b 0a 2a 2a 20 41 20 6a 75 6d 70 20 69 73 20 6d 61 .** A jump is ma
1c77c 64 65 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 de to P2 if the
1c77d 72 65 73 75 6c 74 20 73 65 74 20 61 66 74 65 72 result set after
1c77e 20 66 69 6c 74 65 72 69 6e 67 20 77 6f 75 6c 64 filtering would
1c77f 20 62 65 20 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61 be empty..*/.ca
1c780 73 65 20 4f 50 5f 56 46 69 6c 74 65 72 3a 20 7b se OP_VFilter: {
1c781 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 /* jump */.
1c782 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e 74 20 int nArg;. int
1c783 69 51 75 65 72 79 3b 0a 20 20 63 6f 6e 73 74 20 iQuery;. const
1c784 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a sqlite3_module *
1c785 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a pModule;. Mem *
1c786 70 51 75 65 72 79 20 3d 20 26 70 2d 3e 61 4d 65 pQuery = &p->aMe
1c787 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 m[pOp->p3];. Me
1c788 6d 20 2a 70 41 72 67 63 20 3d 20 26 70 51 75 65 m *pArgc = &pQue
1c789 72 79 5b 31 5d 3b 0a 20 20 73 71 6c 69 74 65 33 ry[1];. sqlite3
1c78a 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 _vtab_cursor *pV
1c78b 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c tabCursor;. sql
1c78c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1c78d 3b 0a 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 ;.. VdbeCursor
1c78e 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 *pCur = p->apCsr
1c78f 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a 20 20 52 45 [pOp->p1];.. RE
1c790 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1c791 2d 3e 70 33 2c 20 70 51 75 65 72 79 29 3b 0a 20 ->p3, pQuery);.
1c792 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 assert( pCur->p
1c793 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 VtabCursor );.
1c794 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 43 pVtabCursor = pC
1c795 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b ur->pVtabCursor;
1c796 0a 20 20 70 56 74 61 62 20 3d 20 70 56 74 61 62 . pVtab = pVtab
1c797 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 Cursor->pVtab;.
1c798 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61 62 pModule = pVtab
1c799 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a ->pModule;.. /*
1c79a 20 47 72 61 62 20 74 68 65 20 69 6e 64 65 78 20 Grab the index
1c79b 6e 75 6d 62 65 72 20 61 6e 64 20 61 72 67 63 20 number and argc
1c79c 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20 parameters */.
1c79d 61 73 73 65 72 74 28 20 28 70 51 75 65 72 79 2d assert( (pQuery-
1c79e 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29 21 >flags&MEM_Int)!
1c79f 3d 30 20 26 26 20 70 41 72 67 63 2d 3e 66 6c 61 =0 && pArgc->fla
1c7a0 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 gs==MEM_Int );.
1c7a1 20 6e 41 72 67 20 3d 20 28 69 6e 74 29 70 41 72 nArg = (int)pAr
1c7a2 67 63 2d 3e 75 2e 69 3b 0a 20 20 69 51 75 65 72 gc->u.i;. iQuer
1c7a3 79 20 3d 20 28 69 6e 74 29 70 51 75 65 72 79 2d y = (int)pQuery-
1c7a4 3e 75 2e 69 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f >u.i;.. /* Invo
1c7a5 6b 65 20 74 68 65 20 78 46 69 6c 74 65 72 20 6d ke the xFilter m
1c7a6 65 74 68 6f 64 20 2a 2f 0a 20 20 7b 0a 20 20 20 ethod */. {.
1c7a7 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 20 int res = 0;.
1c7a8 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4d 65 6d int i;. Mem
1c7a9 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 **apArg = p->ap
1c7aa 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 69 20 3d Arg;. for(i =
1c7ab 20 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b 2b 29 0; i<nArg; i++)
1c7ac 7b 0a 20 20 20 20 20 20 61 70 41 72 67 5b 69 5d {. apArg[i]
1c7ad 20 3d 20 26 70 41 72 67 63 5b 69 2b 31 5d 3b 0a = &pArgc[i+1];.
1c7ae 20 20 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 storeTypeI
1c7af 6e 66 6f 28 61 70 41 72 67 5b 69 5d 2c 20 30 29 nfo(apArg[i], 0)
1c7b0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 ;. }.. if(
1c7b1 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1c7b2 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1c7b3 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1c7b4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 ;. sqlite3Vta
1c7b5 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 bLock(pVtab);.
1c7b6 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f p->inVtabMetho
1c7b7 64 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 d = 1;. rc =
1c7b8 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 65 72 pModule->xFilter
1c7b9 28 70 56 74 61 62 43 75 72 73 6f 72 2c 20 69 51 (pVtabCursor, iQ
1c7ba 75 65 72 79 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c uery, pOp->p4.z,
1c7bb 20 6e 41 72 67 2c 20 61 70 41 72 67 29 3b 0a 20 nArg, apArg);.
1c7bc 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 p->inVtabMeth
1c7bd 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 od = 0;. sqli
1c7be 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1c7bf 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 >zErrMsg);. p
1c7c0 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 ->zErrMsg = pVta
1c7c1 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 b->zErrMsg;.
1c7c2 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d pVtab->zErrMsg =
1c7c3 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 0;. sqlite3V
1c7c4 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 tabUnlock(db, pV
1c7c5 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 tab);. if( rc
1c7c6 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1c7c7 20 20 20 20 20 72 65 73 20 3d 20 70 4d 6f 64 75 res = pModu
1c7c8 6c 65 2d 3e 78 45 6f 66 28 70 56 74 61 62 43 75 le->xEof(pVtabCu
1c7c9 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 rsor);. }.
1c7ca 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1c7cb 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 tyOn(db) ) goto
1c7cc 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1c7cd 75 73 65 3b 0a 0a 20 20 20 20 69 66 28 20 72 65 use;.. if( re
1c7ce 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 s ){. pc =
1c7cf 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 pOp->p2 - 1;.
1c7d0 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 6e }. }. pCur->n
1c7d1 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 20 62 ullRow = 0;.. b
1c7d2 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1c7d3 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
1c7d4 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 RTUALTABLE */..#
1c7d5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1c7d6 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1c7d7 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 6f 77 69 /* Opcode: VRowi
1c7d8 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a d P1 P2 * * *.**
1c7d9 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 74 6f 20 72 .** Store into r
1c7da 65 67 69 73 74 65 72 20 50 32 20 20 74 68 65 20 egister P2 the
1c7db 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 74 68 65 20 rowid of.** the
1c7dc 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 68 virtual-table th
1c7dd 61 74 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 at the P1 cursor
1c7de 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e is pointing to.
1c7df 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52 6f 77 .*/.case OP_VRow
1c7e0 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 id: {
1c7e1 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1c7e2 65 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ease */. sqlite
1c7e3 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 3_vtab *pVtab;.
1c7e4 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
1c7e5 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a odule *pModule;.
1c7e6 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 sqlite_int64 i
1c7e7 52 6f 77 3b 0a 20 20 56 64 62 65 43 75 72 73 6f Row;. VdbeCurso
1c7e8 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 r *pCur = p->apC
1c7e9 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a 20 20 sr[pOp->p1];..
1c7ea 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56 assert( pCur->pV
1c7eb 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 tabCursor );. i
1c7ec 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 f( pCur->nullRow
1c7ed 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 ){. break;.
1c7ee 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43 75 }. pVtab = pCu
1c7ef 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e r->pVtabCursor->
1c7f0 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 pVtab;. pModule
1c7f1 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c = pVtab->pModul
1c7f2 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 6f e;. assert( pMo
1c7f3 64 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29 3b 0a dule->xRowid );.
1c7f4 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1c7f5 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 etyOff(db) ) got
1c7f6 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1c7f7 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 70 4d isuse;. rc = pM
1c7f8 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28 70 43 odule->xRowid(pC
1c7f9 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2c ur->pVtabCursor,
1c7fa 20 26 69 52 6f 77 29 3b 0a 20 20 73 71 6c 69 74 &iRow);. sqlit
1c7fb 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1c7fc 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a zErrMsg);. p->z
1c7fd 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e ErrMsg = pVtab->
1c7fe 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 zErrMsg;. pVtab
1c7ff 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1c800 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1c801 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 tyOn(db) ) goto
1c802 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1c803 75 73 65 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 use;. MemSetTyp
1c804 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
1c805 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e Int);. pOut->u.
1c806 69 20 3d 20 69 52 6f 77 3b 0a 20 20 62 72 65 61 i = iRow;. brea
1c807 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 k;.}.#endif /* S
1c808 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1c809 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e ALTABLE */..#ifn
1c80a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1c80b 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 VIRTUALTABLE./*
1c80c 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75 6d 6e 20 Opcode: VColumn
1c80d 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1c80e 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 76 61 6c ** Store the val
1c80f 75 65 20 6f 66 20 74 68 65 20 50 32 2d 74 68 20 ue of the P2-th
1c810 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 68 65 column of.** the
1c811 20 72 6f 77 20 6f 66 20 74 68 65 20 76 69 72 74 row of the virt
1c812 75 61 6c 2d 74 61 62 6c 65 20 74 68 61 74 20 74 ual-table that t
1c813 68 65 20 0a 2a 2a 20 50 31 20 63 75 72 73 6f 72 he .** P1 cursor
1c814 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 is pointing to
1c815 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 33 into register P3
1c816 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 6f ..*/.case OP_VCo
1c817 6c 75 6d 6e 3a 20 7b 0a 20 20 73 71 6c 69 74 65 lumn: {. sqlite
1c818 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 3_vtab *pVtab;.
1c819 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
1c81a 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a odule *pModule;.
1c81b 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 Mem *pDest;.
1c81c 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1c81d 73 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 56 64 62 sContext;.. Vdb
1c81e 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 eCursor *pCur =
1c81f 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1c820 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 ];. assert( pCu
1c821 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 r->pVtabCursor )
1c822 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1c823 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 >p3>0 && pOp->p3
1c824 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 <=p->nMem );. p
1c825 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b Dest = &p->aMem[
1c826 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 pOp->p3];. if(
1c827 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b pCur->nullRow ){
1c828 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1c829 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 44 65 73 74 MemSetNull(pDest
1c82a 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 );. break;.
1c82b 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43 75 72 }. pVtab = pCur
1c82c 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 ->pVtabCursor->p
1c82d 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 Vtab;. pModule
1c82e 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 = pVtab->pModule
1c82f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 6f 64 ;. assert( pMod
1c830 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a ule->xColumn );.
1c831 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74 65 memset(&sConte
1c832 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 43 xt, 0, sizeof(sC
1c833 6f 6e 74 65 78 74 29 29 3b 0a 0a 20 20 2f 2a 20 ontext));.. /*
1c834 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 The output cell
1c835 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 76 65 may already have
1c836 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 a buffer alloca
1c837 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 ted. Move. ** t
1c838 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 he current conte
1c839 6e 74 73 20 74 6f 20 73 43 6f 6e 74 65 78 74 2e nts to sContext.
1c83a 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65 s so in case the
1c83b 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 0a user-function .
1c83c 20 20 2a 2a 20 63 61 6e 20 75 73 65 20 74 68 65 ** can use the
1c83d 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 already allocat
1c83e 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 ed buffer instea
1c83f 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 d of allocating
1c840 61 20 0a 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e a . ** new one.
1c841 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 . */. sqlite3V
1c842 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 43 6f 6e dbeMemMove(&sCon
1c843 74 65 78 74 2e 73 2c 20 70 44 65 73 74 29 3b 0a text.s, pDest);.
1c844 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1c845 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 4d 45 (&sContext.s, ME
1c846 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 M_Null);.. if(
1c847 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1c848 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1c849 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1c84a 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d . rc = pModule-
1c84b 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e 70 >xColumn(pCur->p
1c84c 56 74 61 62 43 75 72 73 6f 72 2c 20 26 73 43 6f VtabCursor, &sCo
1c84d 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b ntext, pOp->p2);
1c84e 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1c84f 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1c850 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d ;. p->zErrMsg =
1c851 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b pVtab->zErrMsg;
1c852 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 . pVtab->zErrMs
1c853 67 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 70 g = 0;.. /* Cop
1c854 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 y the result of
1c855 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 the function to
1c856 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72 2e the P3 register.
1c857 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 We. ** do this
1c858 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 regardless of w
1c859 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e hether or not an
1c85a 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 74 error occured t
1c85b 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a o ensure any. *
1c85c 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 * dynamic alloca
1c85d 74 69 6f 6e 20 69 6e 20 73 43 6f 6e 74 65 78 74 tion in sContext
1c85e 2e 73 20 28 61 20 4d 65 6d 20 73 74 72 75 63 74 .s (a Mem struct
1c85f 29 20 69 73 20 20 72 65 6c 65 61 73 65 64 2e 0a ) is released..
1c860 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 */. sqlite3Vd
1c861 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
1c862 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 65 6e (&sContext.s, en
1c863 63 6f 64 69 6e 67 29 3b 0a 20 20 52 45 47 49 53 coding);. REGIS
1c864 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1c865 33 2c 20 70 44 65 73 74 29 3b 0a 20 20 73 71 6c 3, pDest);. sql
1c866 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
1c867 70 44 65 73 74 2c 20 26 73 43 6f 6e 74 65 78 74 pDest, &sContext
1c868 2e 73 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 .s);. UPDATE_MA
1c869 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 73 74 X_BLOBSIZE(pDest
1c86a 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 );.. if( sqlite
1c86b 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 3SafetyOn(db) ){
1c86c 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f . goto abort_
1c86d 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1c86e 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1c86f 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 44 VdbeMemTooBig(pD
1c870 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f est) ){. goto
1c871 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 too_big;. }.
1c872 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1c873 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
1c874 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a IRTUALTABLE */..
1c875 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1c876 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1c877 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e 65 78 ./* Opcode: VNex
1c878 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a t P1 P2 * * *.**
1c879 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69 72 74 .** Advance virt
1c87a 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74 6f 20 ual table P1 to
1c87b 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 20 the next row in
1c87c 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 20 61 its result set a
1c87d 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 69 6e nd.** jump to in
1c87e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 20 4f struction P2. O
1c87f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74 75 61 r, if the virtua
1c880 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65 61 63 l table has reac
1c881 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f hed.** the end o
1c882 66 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 f its result set
1c883 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f , then fall thro
1c884 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ugh to the next
1c885 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a instruction..*/.
1c886 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a 20 7b case OP_VNext: {
1c887 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 /* jump */.
1c888 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1c889 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c tab;. const sql
1c88a 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
1c88b 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 20 dule;. int res
1c88c 3d 20 30 3b 0a 0a 20 20 56 64 62 65 43 75 72 73 = 0;.. VdbeCurs
1c88d 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 or *pCur = p->ap
1c88e 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1c88f 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56 assert( pCur->pV
1c890 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 tabCursor );. i
1c891 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 f( pCur->nullRow
1c892 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 ){. break;.
1c893 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43 75 }. pVtab = pCu
1c894 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e r->pVtabCursor->
1c895 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 pVtab;. pModule
1c896 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c = pVtab->pModul
1c897 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 6f e;. assert( pMo
1c898 64 75 6c 65 2d 3e 78 4e 65 78 74 20 29 3b 0a 0a dule->xNext );..
1c899 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 /* Invoke the
1c89a 78 4e 65 78 74 28 29 20 6d 65 74 68 6f 64 20 6f xNext() method o
1c89b 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 54 68 f the module. Th
1c89c 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f ere is no way fo
1c89d 72 20 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72 r the. ** under
1c89e 6c 79 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 lying implementa
1c89f 74 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 61 tion to return a
1c8a0 6e 20 65 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f n error if one o
1c8a1 63 63 75 72 73 20 64 75 72 69 6e 67 0a 20 20 2a ccurs during. *
1c8a2 2a 20 78 4e 65 78 74 28 29 2e 20 49 6e 73 74 65 * xNext(). Inste
1c8a3 61 64 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 ad, if an error
1c8a4 6f 63 63 75 72 73 2c 20 74 72 75 65 20 69 73 20 occurs, true is
1c8a5 72 65 74 75 72 6e 65 64 20 28 69 6e 64 69 63 61 returned (indica
1c8a6 74 69 6e 67 20 74 68 61 74 20 0a 20 20 2a 2a 20 ting that . **
1c8a7 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c data is availabl
1c8a8 65 29 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 e) and the error
1c8a9 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 77 code returned w
1c8aa 68 65 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20 hen xColumn or.
1c8ab 20 2a 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20 6d ** some other m
1c8ac 65 74 68 6f 64 20 69 73 20 6e 65 78 74 20 69 6e ethod is next in
1c8ad 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 73 61 76 voked on the sav
1c8ae 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
1c8af 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 cursor.. */. i
1c8b0 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1c8b1 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 Off(db) ) goto a
1c8b2 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1c8b3 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 se;. sqlite3Vta
1c8b4 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 bLock(pVtab);.
1c8b5 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 p->inVtabMethod
1c8b6 3d 20 31 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 = 1;. rc = pMod
1c8b7 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72 2d ule->xNext(pCur-
1c8b8 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 >pVtabCursor);.
1c8b9 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 p->inVtabMethod
1c8ba 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 = 0;. sqlite3D
1c8bb 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
1c8bc 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 rMsg);. p->zErr
1c8bd 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 Msg = pVtab->zEr
1c8be 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a rMsg;. pVtab->z
1c8bf 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 73 71 ErrMsg = 0;. sq
1c8c0 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 lite3VtabUnlock(
1c8c1 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 69 66 db, pVtab);. if
1c8c2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1c8c3 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 70 4d 6f ){. res = pMo
1c8c4 64 75 6c 65 2d 3e 78 45 6f 66 28 70 43 75 72 2d dule->xEof(pCur-
1c8c5 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 >pVtabCursor);.
1c8c6 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1c8c7 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 SafetyOn(db) ) g
1c8c8 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1c8c9 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 20 _misuse;.. if(
1c8ca 21 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 !res ){. /* I
1c8cb 66 20 74 68 65 72 65 20 69 73 20 64 61 74 61 2c f there is data,
1c8cc 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 jump to P2 */.
1c8cd 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1c8ce 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b - 1;. }. break
1c8cf 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1c8d0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1c8d1 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 LTABLE */..#ifnd
1c8d2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1c8d3 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f IRTUALTABLE./* O
1c8d4 70 63 6f 64 65 3a 20 56 52 65 6e 61 6d 65 20 50 pcode: VRename P
1c8d5 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 1 * * P4 *.**.**
1c8d6 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
1c8d7 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 to a virtual ta
1c8d8 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 ble object, an s
1c8d9 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 qlite3_vtab stru
1c8da 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f cture..** This o
1c8db 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 pcode invokes th
1c8dc 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
1c8dd 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 20 xRename method.
1c8de 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 The value.** in
1c8df 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 70 register P1 is p
1c8e0 61 73 73 65 64 20 61 73 20 74 68 65 20 7a 4e 61 assed as the zNa
1c8e1 6d 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 me argument to t
1c8e2 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f he xRename metho
1c8e3 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52 d..*/.case OP_VR
1c8e4 65 6e 61 6d 65 3a 20 7b 0a 20 20 73 71 6c 69 74 ename: {. sqlit
1c8e5 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d e3_vtab *pVtab =
1c8e6 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a pOp->p4.pVtab;.
1c8e7 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 20 3d 20 26 Mem *pName = &
1c8e8 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d p->aMem[pOp->p1]
1c8e9 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 74 61 ;. assert( pVta
1c8ea 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e b->pModule->xRen
1c8eb 61 6d 65 20 29 3b 0a 20 20 52 45 47 49 53 54 45 ame );. REGISTE
1c8ec 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c R_TRACE(pOp->p1,
1c8ed 20 70 4e 61 6d 65 29 3b 0a 0a 20 20 53 74 72 69 pName);.. Stri
1c8ee 6e 67 69 66 79 28 70 4e 61 6d 65 2c 20 65 6e 63 ngify(pName, enc
1c8ef 6f 64 69 6e 67 29 3b 0a 0a 20 20 69 66 28 20 73 oding);.. if( s
1c8f0 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1c8f1 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1c8f2 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1c8f3 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 sqlite3VtabLoc
1c8f4 6b 28 70 56 74 61 62 29 3b 0a 20 20 72 63 20 3d k(pVtab);. rc =
1c8f5 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d pVtab->pModule-
1c8f6 3e 78 52 65 6e 61 6d 65 28 70 56 74 61 62 2c 20 >xRename(pVtab,
1c8f7 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73 71 6c pName->z);. sql
1c8f8 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1c8f9 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d ->zErrMsg);. p-
1c8fa 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 >zErrMsg = pVtab
1c8fb 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 ->zErrMsg;. pVt
1c8fc 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b ab->zErrMsg = 0;
1c8fd 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e . sqlite3VtabUn
1c8fe 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b lock(db, pVtab);
1c8ff 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1c900 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 fetyOn(db) ) got
1c901 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1c902 69 73 75 73 65 3b 0a 0a 20 20 62 72 65 61 6b 3b isuse;.. break;
1c903 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
1c904 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1c905 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f IRTUALTABLE./* O
1c906 70 63 6f 64 65 3a 20 56 55 70 64 61 74 65 20 50 pcode: VUpdate P
1c907 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1c908 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
1c909 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 er to a virtual
1c90a 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e table object, an
1c90b 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 sqlite3_vtab st
1c90c 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73 ructure..** This
1c90d 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 opcode invokes
1c90e 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
1c90f 67 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 g xUpdate method
1c910 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a 2a 20 61 . P2 values.** a
1c911 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 6d 65 re contiguous me
1c912 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74 61 72 74 mory cells start
1c913 69 6e 67 20 61 74 20 50 33 20 74 6f 20 70 61 73 ing at P3 to pas
1c914 73 20 74 6f 20 74 68 65 20 78 55 70 64 61 74 65 s to the xUpdate
1c915 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 2e .** invocation.
1c916 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 The value in re
1c917 67 69 73 74 65 72 20 28 50 33 2b 50 32 2d 31 29 gister (P3+P2-1)
1c918 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 corresponds to
1c919 74 68 65 20 0a 2a 2a 20 70 32 74 68 20 65 6c 65 the .** p2th ele
1c91a 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 67 76 ment of the argv
1c91b 20 61 72 72 61 79 20 70 61 73 73 65 64 20 74 6f array passed to
1c91c 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 xUpdate..**.**
1c91d 54 68 65 20 78 55 70 64 61 74 65 20 6d 65 74 68 The xUpdate meth
1c91e 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20 44 45 4c od will do a DEL
1c91f 45 54 45 20 6f 72 20 61 6e 20 49 4e 53 45 52 54 ETE or an INSERT
1c920 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 54 68 65 or both..** The
1c921 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d 65 6e 74 argv[0] element
1c922 20 28 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f (which correspo
1c923 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79 20 63 65 nds to memory ce
1c924 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20 74 68 65 ll P3).** is the
1c925 20 72 6f 77 69 64 20 6f 66 20 61 20 72 6f 77 20 rowid of a row
1c926 74 6f 20 64 65 6c 65 74 65 2e 20 20 49 66 20 61 to delete. If a
1c927 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c 4c 20 74 rgv[0] is NULL t
1c928 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c 65 74 hen no .** delet
1c929 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54 68 65 ion occurs. The
1c92a 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d 65 6e 74 argv[1] element
1c92b 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 is the rowid of
1c92c 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 the new .** row
1c92d 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 4e . This can be N
1c92e 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 68 65 20 ULL to have the
1c92f 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 65 virtual table se
1c930 6c 65 63 74 20 74 68 65 20 6e 65 77 20 0a 2a 2a lect the new .**
1c931 20 72 6f 77 69 64 20 66 6f 72 20 69 74 73 65 6c rowid for itsel
1c932 66 2e 20 20 54 68 65 20 73 75 62 73 65 71 75 65 f. The subseque
1c933 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 nt elements in t
1c934 68 65 20 61 72 72 61 79 20 61 72 65 20 0a 2a 2a he array are .**
1c935 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 63 the values of c
1c936 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6e 65 olumns in the ne
1c937 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 w row..**.** If
1c938 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20 69 6e P2==1 then no in
1c939 73 65 72 74 20 69 73 20 70 65 72 66 6f 72 6d 65 sert is performe
1c93a 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74 d. argv[0] is t
1c93b 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 61 he rowid of.** a
1c93c 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 0a row to delete..
1c93d 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 62 6f **.** P1 is a bo
1c93e 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 66 20 69 olean flag. If i
1c93f 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 t is set to true
1c940 20 61 6e 64 20 74 68 65 20 78 55 70 64 61 74 65 and the xUpdate
1c941 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 75 63 63 call.** is succ
1c942 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 74 68 65 essful, then the
1c943 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
1c944 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f by sqlite3_last_
1c945 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 0a insert_rowid() .
1c946 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ** is set to the
1c947 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f value of the ro
1c948 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77 20 wid for the row
1c949 6a 75 73 74 20 69 6e 73 65 72 74 65 64 2e 0a 2a just inserted..*
1c94a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 64 61 74 /.case OP_VUpdat
1c94b 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 e: {. sqlite3_v
1c94c 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 tab *pVtab = pOp
1c94d 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 73 71 ->p4.pVtab;. sq
1c94e 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
1c94f 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 odule = (sqlite3
1c950 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d _module *)pVtab-
1c951 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 >pModule;. int
1c952 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a nArg = pOp->p2;.
1c953 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c954 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 4type==P4_VTAB )
1c955 3b 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d ;. if( pModule-
1c956 3e 78 55 70 64 61 74 65 3d 3d 30 20 29 7b 0a 20 >xUpdate==0 ){.
1c957 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1c958 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1c959 20 64 62 2c 20 22 72 65 61 64 2d 6f 6e 6c 79 20 db, "read-only
1c95a 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 72 63 20 table");. rc
1c95b 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1c95c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 }else{. int
1c95d 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 i;. sqlite_i
1c95e 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 nt64 rowid;.
1c95f 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d Mem **apArg = p-
1c960 3e 61 70 41 72 67 3b 0a 20 20 20 20 4d 65 6d 20 >apArg;. Mem
1c961 2a 70 58 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 *pX = &p->aMem[p
1c962 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 66 6f 72 Op->p3];. for
1c963 28 69 3d 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b (i=0; i<nArg; i+
1c964 2b 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 54 +){. storeT
1c965 79 70 65 49 6e 66 6f 28 70 58 2c 20 30 29 3b 0a ypeInfo(pX, 0);.
1c966 20 20 20 20 20 20 61 70 41 72 67 5b 69 5d 20 3d apArg[i] =
1c967 20 70 58 3b 0a 20 20 20 20 20 20 70 58 2b 2b 3b pX;. pX++;
1c968 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
1c969 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1c96a 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1c96b 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1c96c 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c sqlite3VtabL
1c96d 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 20 20 ock(pVtab);.
1c96e 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 55 rc = pModule->xU
1c96f 70 64 61 74 65 28 70 56 74 61 62 2c 20 6e 41 72 pdate(pVtab, nAr
1c970 67 2c 20 61 70 41 72 67 2c 20 26 72 6f 77 69 64 g, apArg, &rowid
1c971 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
1c972 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 Free(db, p->zErr
1c973 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 Msg);. p->zEr
1c974 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 rMsg = pVtab->zE
1c975 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 74 61 62 rrMsg;. pVtab
1c976 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1c977 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e sqlite3VtabUn
1c978 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b lock(db, pVtab);
1c979 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1c97a 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 SafetyOn(db) ) g
1c97b 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1c97c 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 69 66 28 _misuse;. if(
1c97d 20 70 4f 70 2d 3e 70 31 20 26 26 20 72 63 3d 3d pOp->p1 && rc==
1c97e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1c97f 20 20 20 61 73 73 65 72 74 28 20 6e 41 72 67 3e assert( nArg>
1c980 31 20 26 26 20 61 70 41 72 67 5b 30 5d 20 26 26 1 && apArg[0] &&
1c981 20 28 61 70 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 (apArg[0]->flag
1c982 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 s&MEM_Null) );.
1c983 20 20 20 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 db->lastRow
1c984 69 64 20 3d 20 72 6f 77 69 64 3b 0a 20 20 20 20 id = rowid;.
1c985 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 }. p->nChange
1c986 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b ++;. }. break;
1c987 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1c988 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1c989 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 TABLE */..#ifnde
1c98a 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 f SQLITE_OMIT_P
1c98b 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 20 AGER_PRAGMAS./*
1c98c 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f 75 6e Opcode: Pagecoun
1c98d 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a t P1 P2 * * *.**
1c98e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 75 .** Write the cu
1c98f 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 rrent number of
1c990 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 pages in databas
1c991 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 20 63 e P1 to memory c
1c992 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 ell P2..*/.case
1c993 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 7b 20 OP_Pagecount: {
1c994 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 /* ou
1c995 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f t2-prerelease */
1c996 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d . int p1 = pOp-
1c997 3e 70 31 3b 20 0a 20 20 69 6e 74 20 6e 50 61 67 >p1; . int nPag
1c998 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 e;. Pager *pPag
1c999 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 er = sqlite3Btre
1c99a 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 70 ePager(db->aDb[p
1c99b 31 5d 2e 70 42 74 29 3b 0a 0a 20 20 72 63 20 3d 1].pBt);.. rc =
1c99c 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
1c99d 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 ecount(pPager, &
1c99e 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 nPage);. if( rc
1c99f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1c9a0 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d pOut->flags =
1c9a1 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4f MEM_Int;. pO
1c9a2 75 74 2d 3e 75 2e 69 20 3d 20 6e 50 61 67 65 3b ut->u.i = nPage;
1c9a3 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1c9a4 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
1c9a5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
1c9a6 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 E./* Opcode: Tra
1c9a7 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a ce * * * P4 *.**
1c9a8 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e 67 20 69 .** If tracing i
1c9a9 73 20 65 6e 61 62 6c 65 64 20 28 62 79 20 74 68 s enabled (by th
1c9aa 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 e sqlite3_trace(
1c9ab 29 29 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 )) interface, th
1c9ac 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d 38 20 en.** the UTF-8
1c9ad 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 65 64 string contained
1c9ae 20 69 6e 20 50 34 20 69 73 20 65 6d 69 74 74 65 in P4 is emitte
1c9af 64 20 6f 6e 20 74 68 65 20 74 72 61 63 65 20 63 d on the trace c
1c9b0 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 allback..*/.case
1c9b1 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 20 20 69 OP_Trace: {. i
1c9b2 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a f( pOp->p4.z ){.
1c9b3 20 20 20 20 69 66 28 20 64 62 2d 3e 78 54 72 61 if( db->xTra
1c9b4 63 65 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e ce ){. db->
1c9b5 78 54 72 61 63 65 28 64 62 2d 3e 70 54 72 61 63 xTrace(db->pTrac
1c9b6 65 41 72 67 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 eArg, pOp->p4.z)
1c9b7 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 ;. }.#ifdef S
1c9b8 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
1c9b9 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 if( (db->flags &
1c9ba 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 SQLITE_SqlTrace
1c9bb 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 )!=0 ){. sq
1c9bc 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
1c9bd 28 22 53 51 4c 2d 74 72 61 63 65 3a 20 25 73 5c ("SQL-trace: %s\
1c9be 6e 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a n", pOp->p4.z);.
1c9bf 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.#endif /*
1c9c0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a SQLITE_DEBUG */.
1c9c1 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1c9c2 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 endif.../* Opcod
1c9c3 65 3a 20 4e 6f 6f 70 20 2a 20 2a 20 2a 20 2a 20 e: Noop * * * *
1c9c4 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f 74 68 69 *.**.** Do nothi
1c9c5 6e 67 2e 20 20 54 68 69 73 20 69 6e 73 74 72 75 ng. This instru
1c9c6 63 74 69 6f 6e 20 69 73 20 6f 66 74 65 6e 20 75 ction is often u
1c9c7 73 65 66 75 6c 20 61 73 20 61 20 6a 75 6d 70 0a seful as a jump.
1c9c8 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a ** destination..
1c9c9 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 67 */./*.** The mag
1c9ca 69 63 20 45 78 70 6c 61 69 6e 20 6f 70 63 6f 64 ic Explain opcod
1c9cb 65 20 61 72 65 20 6f 6e 6c 79 20 69 6e 73 65 72 e are only inser
1c9cc 74 65 64 20 77 68 65 6e 20 65 78 70 6c 61 69 6e ted when explain
1c9cd 3d 3d 32 20 28 77 68 69 63 68 0a 2a 2a 20 69 73 ==2 (which.** is
1c9ce 20 74 6f 20 73 61 79 20 77 68 65 6e 20 74 68 65 to say when the
1c9cf 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50 EXPLAIN QUERY P
1c9d0 4c 41 4e 20 73 79 6e 74 61 78 20 69 73 20 75 73 LAN syntax is us
1c9d1 65 64 2e 29 0a 2a 2a 20 54 68 69 73 20 6f 70 63 ed.).** This opc
1c9d2 6f 64 65 20 72 65 63 6f 72 64 73 20 69 6e 66 6f ode records info
1c9d3 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 rmation from the
1c9d4 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 49 74 20 optimizer. It
1c9d5 69 73 20 74 68 65 0a 2a 2a 20 74 68 65 20 73 61 is the.** the sa
1c9d6 6d 65 20 61 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 me as a no-op.
1c9d7 54 68 69 73 20 6f 70 63 6f 64 65 73 6e 65 76 65 This opcodesneve
1c9d8 72 20 61 70 70 65 61 72 73 20 69 6e 20 61 20 72 r appears in a r
1c9d9 65 61 6c 20 56 4d 20 70 72 6f 67 72 61 6d 2e 0a eal VM program..
1c9da 2a 2f 0a 64 65 66 61 75 6c 74 3a 20 7b 20 20 20 */.default: {
1c9db 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 /* This i
1c9dc 73 20 72 65 61 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 s really OP_Noop
1c9dd 20 61 6e 64 20 4f 50 5f 45 78 70 6c 61 69 6e 20 and OP_Explain
1c9de 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f */. break;.}../
1c9df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c9e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c9e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c9e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
1c9e4 20 54 68 65 20 63 61 73 65 73 20 6f 66 20 74 68 The cases of th
1c9e5 65 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 e switch stateme
1c9e6 6e 74 20 61 62 6f 76 65 20 74 68 69 73 20 6c 69 nt above this li
1c9e7 6e 65 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 ne should all be
1c9e8 20 69 6e 64 65 6e 74 65 64 0a 2a 2a 20 62 79 20 indented.** by
1c9e9 36 20 73 70 61 63 65 73 2e 20 20 42 75 74 20 74 6 spaces. But t
1c9ea 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 36 20 73 he left-most 6 s
1c9eb 70 61 63 65 73 20 68 61 76 65 20 62 65 65 6e 20 paces have been
1c9ec 72 65 6d 6f 76 65 64 20 74 6f 20 69 6d 70 72 6f removed to impro
1c9ed 76 65 20 74 68 65 0a 2a 2a 20 72 65 61 64 61 62 ve the.** readab
1c9ee 69 6c 69 74 79 2e 20 20 46 72 6f 6d 20 74 68 69 ility. From thi
1c9ef 73 20 70 6f 69 6e 74 20 6f 6e 20 64 6f 77 6e 2c s point on down,
1c9f0 20 74 68 65 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 the normal inde
1c9f1 6e 74 61 74 69 6f 6e 20 72 75 6c 65 73 20 61 72 ntation rules ar
1c9f2 65 0a 2a 2a 20 72 65 73 74 6f 72 65 64 2e 0a 2a e.** restored..*
1c9f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c9f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c9f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c9f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c9f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 ************/.
1c9f8 20 20 7d 0a 0a 23 69 66 64 65 66 20 56 44 42 45 }..#ifdef VDBE
1c9f9 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 7b 0a 20 _PROFILE. {.
1c9fa 20 20 20 20 20 75 36 34 20 65 6c 61 70 73 65 64 u64 elapsed
1c9fb 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 = sqlite3Hwtime
1c9fc 28 29 20 2d 20 73 74 61 72 74 3b 0a 20 20 20 20 () - start;.
1c9fd 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 20 2b 3d pOp->cycles +=
1c9fe 20 65 6c 61 70 73 65 64 3b 0a 20 20 20 20 20 20 elapsed;.
1c9ff 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23 69 66 20 pOp->cnt++;.#if
1ca00 30 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 0. fprint
1ca01 66 28 73 74 64 6f 75 74 2c 20 22 25 31 30 6c 6c f(stdout, "%10ll
1ca02 75 20 22 2c 20 65 6c 61 70 73 65 64 29 3b 0a 20 u ", elapsed);.
1ca03 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1ca04 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f 75 74 bePrintOp(stdout
1ca05 2c 20 6f 72 69 67 50 63 2c 20 26 70 2d 3e 61 4f , origPc, &p->aO
1ca06 70 5b 6f 72 69 67 50 63 5d 29 3b 0a 23 65 6e 64 p[origPc]);.#end
1ca07 69 66 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a if. }.#endif.
1ca08 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c . /* The foll
1ca09 6f 77 69 6e 67 20 63 6f 64 65 20 61 64 64 73 20 owing code adds
1ca0a 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20 61 nothing to the a
1ca0b 63 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e 61 6c ctual functional
1ca0c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 ity. ** of th
1ca0d 65 20 70 72 6f 67 72 61 6d 2e 20 20 49 74 20 69 e program. It i
1ca0e 73 20 6f 6e 6c 79 20 68 65 72 65 20 66 6f 72 20 s only here for
1ca0f 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 testing and debu
1ca10 67 67 69 6e 67 2e 0a 20 20 20 20 2a 2a 20 4f 6e gging.. ** On
1ca11 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
1ca12 20 69 74 20 64 6f 65 73 20 62 75 72 6e 20 43 50 it does burn CP
1ca13 55 20 63 79 63 6c 65 73 20 65 76 65 72 79 20 74 U cycles every t
1ca14 69 6d 65 20 74 68 72 6f 75 67 68 0a 20 20 20 20 ime through.
1ca15 2a 2a 20 74 68 65 20 65 76 61 6c 75 61 74 6f 72 ** the evaluator
1ca16 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 20 63 61 loop. So we ca
1ca17 6e 20 6c 65 61 76 65 20 69 74 20 6f 75 74 20 77 n leave it out w
1ca18 68 65 6e 20 4e 44 45 42 55 47 20 69 73 20 64 65 hen NDEBUG is de
1ca19 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 fined.. */.#i
1ca1a 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 fndef NDEBUG.
1ca1b 20 61 73 73 65 72 74 28 20 70 63 3e 3d 2d 31 20 assert( pc>=-1
1ca1c 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a && pc<p->nOp );.
1ca1d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1ca1e 45 42 55 47 0a 20 20 20 20 69 66 28 20 70 2d 3e EBUG. if( p->
1ca1f 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 69 trace ){. i
1ca20 66 28 20 72 63 21 3d 30 20 29 20 66 70 72 69 6e f( rc!=0 ) fprin
1ca21 74 66 28 70 2d 3e 74 72 61 63 65 2c 22 72 63 3d tf(p->trace,"rc=
1ca22 25 64 5c 6e 22 2c 72 63 29 3b 0a 20 20 20 20 20 %d\n",rc);.
1ca23 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74 79 20 if( opProperty
1ca24 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 & OPFLG_OUT2_PRE
1ca25 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 RELEASE ){.
1ca26 20 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65 registerTrace
1ca27 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e (p->trace, pOp->
1ca28 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 p2, pOut);.
1ca29 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f 70 50 }. if( opP
1ca2a 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f roperty & OPFLG_
1ca2b 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 20 20 20 OUT3 ){.
1ca2c 72 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d registerTrace(p-
1ca2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 33 2c >trace, pOp->p3,
1ca2e 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a pOut);. }.
1ca2f 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 20 2f 2a }.#endif /*
1ca30 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f SQLITE_DEBUG */
1ca31 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e 44 45 42 .#endif /* NDEB
1ca32 55 47 20 2a 2f 0a 20 20 7d 20 20 2f 2a 20 54 68 UG */. } /* Th
1ca33 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 6f 72 e end of the for
1ca34 28 3b 3b 29 20 6c 6f 6f 70 20 74 68 65 20 6c 6f (;;) loop the lo
1ca35 6f 70 73 20 74 68 72 6f 75 67 68 20 6f 70 63 6f ops through opco
1ca36 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 des */.. /* If
1ca37 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70 6f we reach this po
1ca38 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 int, it means th
1ca39 61 74 20 65 78 65 63 75 74 69 6f 6e 20 69 73 20 at execution is
1ca3a 66 69 6e 69 73 68 65 64 20 77 69 74 68 0a 20 20 finished with.
1ca3b 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 66 20 73 ** an error of s
1ca3c 6f 6d 65 20 6b 69 6e 64 2e 0a 20 20 2a 2f 0a 76 ome kind.. */.v
1ca3d 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3a 0a dbe_error_halt:.
1ca3e 20 20 61 73 73 65 72 74 28 20 72 63 20 29 3b 0a assert( rc );.
1ca3f 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
1ca40 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 sqlite3VdbeHalt(
1ca41 70 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 p);. if( rc==SQ
1ca42 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d LITE_IOERR_NOMEM
1ca43 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 ) db->mallocFai
1ca44 6c 65 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 led = 1;. rc =
1ca45 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 SQLITE_ERROR;..
1ca46 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 /* This is the
1ca47 6f 6e 6c 79 20 77 61 79 20 6f 75 74 20 6f 66 20 only way out of
1ca48 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e 20 this procedure.
1ca49 20 57 65 20 68 61 76 65 20 74 6f 0a 20 20 2a 2a We have to. **
1ca4a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 75 74 release the mut
1ca4b 65 78 65 73 20 6f 6e 20 62 74 72 65 65 73 20 74 exes on btrees t
1ca4c 68 61 74 20 77 65 72 65 20 61 63 71 75 69 72 65 hat were acquire
1ca4d 64 20 61 74 20 74 68 65 0a 20 20 2a 2a 20 74 6f d at the. ** to
1ca4e 70 2e 20 2a 2f 0a 76 64 62 65 5f 72 65 74 75 72 p. */.vdbe_retur
1ca4f 6e 3a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 n:. sqlite3Btre
1ca50 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 eMutexArrayLeave
1ca51 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 (&p->aMutex);.
1ca52 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a return rc;.. /*
1ca53 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 Jump to here if
1ca54 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f a string or blo
1ca55 62 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53 51 b larger than SQ
1ca56 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a LITE_MAX_LENGTH.
1ca57 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 6e 74 65 ** is encounte
1ca58 72 65 64 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f 62 69 red.. */.too_bi
1ca59 67 3a 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 g:. sqlite3SetS
1ca5a 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1ca5b 67 2c 20 64 62 2c 20 22 73 74 72 69 6e 67 20 6f g, db, "string o
1ca5c 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 29 r blob too big")
1ca5d 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f ;. rc = SQLITE_
1ca5e 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74 6f 20 76 TOOBIG;. goto v
1ca5f 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a dbe_error_halt;.
1ca60 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 . /* Jump to he
1ca61 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 re if a malloc()
1ca62 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f fails.. */.no_
1ca63 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f mem:. db->mallo
1ca64 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 73 cFailed = 1;. s
1ca65 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1ca66 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1ca67 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 "out of memory"
1ca68 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 );. rc = SQLITE
1ca69 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74 6f 20 76 _NOMEM;. goto v
1ca6a 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a dbe_error_halt;.
1ca6b 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 . /* Jump to he
1ca6c 72 65 20 66 6f 72 20 61 6e 20 53 51 4c 49 54 45 re for an SQLITE
1ca6d 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 2e 0a 20 _MISUSE error..
1ca6e 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f */.abort_due_to
1ca6f 5f 6d 69 73 75 73 65 3a 0a 20 20 72 63 20 3d 20 _misuse:. rc =
1ca70 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1ca71 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e /* Fall thru in
1ca72 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1ca73 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 4a error */.. /* J
1ca74 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f 72 20 ump to here for
1ca75 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f any other kind o
1ca76 66 20 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 f fatal error.
1ca77 54 68 65 20 22 72 63 22 20 76 61 72 69 61 62 6c The "rc" variabl
1ca78 65 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 6f e. ** should ho
1ca79 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6e 75 6d ld the error num
1ca7a 62 65 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f ber.. */.abort_
1ca7b 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3a 0a 20 20 due_to_error:.
1ca7c 61 73 73 65 72 74 28 20 70 2d 3e 7a 45 72 72 4d assert( p->zErrM
1ca7d 73 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 64 sg==0 );. if( d
1ca7e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1ca7f 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f ) rc = SQLITE_NO
1ca80 4d 45 4d 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 MEM;. if( rc!=S
1ca81 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 QLITE_IOERR_NOME
1ca82 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 M ){. sqlite3
1ca83 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1ca84 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c rrMsg, db, "%s",
1ca85 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 sqlite3ErrStr(r
1ca86 63 29 29 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 c));. }. goto
1ca87 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b vdbe_error_halt;
1ca88 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 .. /* Jump to h
1ca89 65 72 65 20 69 66 20 74 68 65 20 73 71 6c 69 74 ere if the sqlit
1ca8a 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 41 e3_interrupt() A
1ca8b 50 49 20 73 65 74 73 20 74 68 65 20 69 6e 74 65 PI sets the inte
1ca8c 72 72 75 70 74 0a 20 20 2a 2a 20 66 6c 61 67 2e rrupt. ** flag.
1ca8d 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f . */.abort_due_
1ca8e 74 6f 5f 69 6e 74 65 72 72 75 70 74 3a 0a 20 20 to_interrupt:.
1ca8f 61 73 73 65 72 74 28 20 64 62 2d 3e 75 31 2e 69 assert( db->u1.i
1ca90 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 3b 0a sInterrupted );.
1ca91 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e rc = SQLITE_IN
1ca92 54 45 52 52 55 50 54 3b 0a 20 20 70 2d 3e 72 63 TERRUPT;. p->rc
1ca93 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 = rc;. sqlite3
1ca94 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1ca95 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c rrMsg, db, "%s",
1ca96 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 sqlite3ErrStr(r
1ca97 63 29 29 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 c));. goto vdbe
1ca98 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 7d 0a 0a _error_halt;.}..
1ca99 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1ca9a 45 6e 64 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a End of vdbe.c **
1ca9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ca9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ca9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1ca9e 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1ca9f 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 62 Begin file vdbeb
1caa0 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a lob.c **********
1caa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1caa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1caa3 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 31 /*.** 2007 May 1
1caa4 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1caa5 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1caa6 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1caa7 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1caa8 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1caa9 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1caaa 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1caab 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1caac 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1caad 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1caae 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1caaf 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1cab0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1cab1 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1cab2 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1cab3 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1cab4 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1cab5 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1cab6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cab7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cab8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cab9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1caba 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
1cabb 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 contains code us
1cabc 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
1cabd 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 incremental BLOB
1cabe 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a I/O..**.** $Id:
1cabf 20 76 64 62 65 62 6c 6f 62 2e 63 2c 76 20 31 2e vdbeblob.c,v 1.
1cac0 32 36 20 32 30 30 38 2f 31 30 2f 30 32 20 31 34 26 2008/10/02 14
1cac1 3a 34 39 3a 30 32 20 64 61 6e 69 65 6c 6b 31 39 :49:02 danielk19
1cac2 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 77 Exp $.*/...#i
1cac3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1cac4 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a 2f 2a 0a 2a T_INCRBLOB../*.*
1cac5 2a 20 56 61 6c 69 64 20 73 71 6c 69 74 65 33 5f * Valid sqlite3_
1cac6 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73 20 70 6f blob* handles po
1cac7 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c 6f 62 20 int to Incrblob
1cac8 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 74 structures..*/.t
1cac9 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e ypedef struct In
1caca 63 72 62 6c 6f 62 20 49 6e 63 72 62 6c 6f 62 3b crblob Incrblob;
1cacb 0a 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f 62 .struct Incrblob
1cacc 20 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 {. int flags;
1cacd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cace 43 6f 70 79 20 6f 66 20 22 66 6c 61 67 73 22 20 Copy of "flags"
1cacf 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 passed to sqlite
1cad0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 20 2a 2f 3_blob_open() */
1cad1 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 . int nByte;
1cad2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
1cad3 7a 65 20 6f 66 20 6f 70 65 6e 20 62 6c 6f 62 2c ze of open blob,
1cad4 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 in bytes */. i
1cad5 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 nt iOffset;
1cad6 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f /* Byte o
1cad7 66 66 73 65 74 20 6f 66 20 62 6c 6f 62 20 69 6e ffset of blob in
1cad8 20 63 75 72 73 6f 72 20 64 61 74 61 20 2a 2f 0a cursor data */.
1cad9 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 73 72 BtCursor *pCsr
1cada 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 ; /* Cur
1cadb 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 sor pointing at
1cadc 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a 20 20 73 71 blob row */. sq
1cadd 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1cade 74 3b 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 t; /* Stateme
1cadf 6e 74 20 68 6f 6c 64 69 6e 67 20 63 75 72 73 6f nt holding curso
1cae0 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 r open */. sqli
1cae1 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
1cae2 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 /* The assoc
1cae3 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 2a iated database *
1cae4 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e /.};../*.** Open
1cae5 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a a blob handle..
1cae6 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1cae7 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f t sqlite3_blob_o
1cae8 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 pen(. sqlite3*
1cae9 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f db, /
1caea 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
1caeb 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 onnection */. c
1caec 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 onst char *zDb,
1caed 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 74 /* The at
1caee 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 tached database
1caef 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 containing the b
1caf0 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 lob */. const c
1caf1 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20 20 20 20 har *zTable,
1caf2 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 63 6f /* The table co
1caf3 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f ntaining the blo
1caf4 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 b */. const cha
1caf5 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f r *zColumn, /
1caf6 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e * The column con
1caf7 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 taining the blob
1caf8 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 */. sqlite_int
1caf9 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20 2f 2a 64 iRow, /*
1cafa 20 54 68 65 20 72 6f 77 20 63 6f 6e 74 61 69 6e The row contain
1cafb 69 6e 67 20 74 68 65 20 67 6c 6f 62 20 2a 2f 0a ing the glob */.
1cafc 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 int flags,
1cafd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1cafe 65 20 2d 3e 20 72 65 61 64 2f 77 72 69 74 65 20 e -> read/write
1caff 61 63 63 65 73 73 2c 20 66 61 6c 73 65 20 2d 3e access, false ->
1cb00 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 read-only */.
1cb01 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 sqlite3_blob **p
1cb02 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61 6e 64 6c pBlob /* Handl
1cb03 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 e for accessing
1cb04 74 68 65 20 62 6c 6f 62 20 72 65 74 75 72 6e 65 the blob returne
1cb05 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 d here */.){. i
1cb06 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d 20 30 3b nt nAttempt = 0;
1cb07 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 . int iCol;
1cb08 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1cb09 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d 6e 20 69 dex of zColumn i
1cb0a 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20 2a 2f 0a n row-record */.
1cb0b 0a 20 20 2f 2a 20 54 68 69 73 20 56 44 42 45 20 . /* This VDBE
1cb0c 70 72 6f 67 72 61 6d 20 73 65 65 6b 73 20 61 20 program seeks a
1cb0d 62 74 72 65 65 20 63 75 72 73 6f 72 20 74 6f 20 btree cursor to
1cb0e 74 68 65 20 69 64 65 6e 74 69 66 69 65 64 20 0a the identified .
1cb0f 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65 2f 72 6f ** db/table/ro
1cb10 77 20 65 6e 74 72 79 2e 20 54 68 65 20 72 65 61 w entry. The rea
1cb11 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20 61 20 son for using a
1cb12 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 6e 73 vdbe program ins
1cb13 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 77 72 69 tead. ** of wri
1cb14 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75 73 65 ting code to use
1cb15 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65 the b-tree laye
1cb16 72 20 64 69 72 65 63 74 6c 79 20 69 73 20 74 68 r directly is th
1cb17 61 74 20 74 68 65 0a 20 20 2a 2a 20 76 64 62 65 at the. ** vdbe
1cb18 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20 74 61 program will ta
1cb19 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 ke advantage of
1cb1a 74 68 65 20 76 61 72 69 6f 75 73 20 74 72 61 6e the various tran
1cb1b 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 6c 6f saction,. ** lo
1cb1c 63 6b 69 6e 67 20 61 6e 64 20 65 72 72 6f 72 20 cking and error
1cb1d 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72 61 73 74 handling infrast
1cb1e 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 69 6e ructure built in
1cb1f 74 6f 20 74 68 65 20 76 64 62 65 2e 0a 20 20 2a to the vdbe.. *
1cb20 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20 73 65 65 *. ** After see
1cb21 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 2c king the cursor,
1cb22 20 74 68 65 20 76 64 62 65 20 65 78 65 63 75 74 the vdbe execut
1cb23 65 73 20 61 6e 20 4f 50 5f 52 65 73 75 6c 74 52 es an OP_ResultR
1cb24 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64 65 20 65 78 ow.. ** Code ex
1cb25 74 65 72 6e 61 6c 20 74 6f 20 74 68 65 20 56 64 ternal to the Vd
1cb26 62 65 20 74 68 65 6e 20 22 62 6f 72 72 6f 77 73 be then "borrows
1cb27 22 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 " the b-tree cur
1cb28 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20 75 73 65 sor and. ** use
1cb29 73 20 69 74 20 74 6f 20 69 6d 70 6c 65 6d 65 6e s it to implemen
1cb2a 74 20 74 68 65 20 62 6c 6f 62 5f 72 65 61 64 28 t the blob_read(
1cb2b 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65 28 29 20 ), blob_write()
1cb2c 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f 62 5f 62 and . ** blob_b
1cb2d 79 74 65 73 28 29 20 66 75 6e 63 74 69 6f 6e 73 ytes() functions
1cb2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 .. **. ** The
1cb2f 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f sqlite3_blob_clo
1cb30 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 69 se() function fi
1cb31 6e 61 6c 69 7a 65 73 20 74 68 65 20 76 64 62 65 nalizes the vdbe
1cb32 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a 2a 20 77 program,. ** w
1cb33 68 69 63 68 20 63 6c 6f 73 65 73 20 74 68 65 20 hich closes the
1cb34 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 6e b-tree cursor an
1cb35 64 20 28 70 6f 73 73 69 62 6c 79 29 20 63 6f 6d d (possibly) com
1cb36 6d 69 74 73 20 74 68 65 20 0a 20 20 2a 2a 20 74 mits the . ** t
1cb37 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f ransaction.. */
1cb38 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
1cb39 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65 6e 42 VdbeOpList openB
1cb3a 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 4f lob[] = {. {O
1cb3b 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 30 P_Transaction, 0
1cb3c 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 2f 2a 20 , 0, 0}, /*
1cb3d 30 3a 20 53 74 61 72 74 20 61 20 74 72 61 6e 73 0: Start a trans
1cb3e 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 7b 4f action */. {O
1cb3f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c 20 P_VerifyCookie,
1cb40 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 2f 2a 20 0, 0, 0}, /*
1cb41 31 3a 20 43 68 65 63 6b 20 74 68 65 20 73 63 68 1: Check the sch
1cb42 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 0a 20 ema cookie */..
1cb43 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 /* One of the
1cb44 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69 following two i
1cb45 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 73 20 72 nstructions is r
1cb46 65 70 6c 61 63 65 64 20 62 79 20 61 6e 0a 20 20 eplaced by an.
1cb47 20 20 2a 2a 20 4f 50 5f 4e 6f 6f 70 20 62 65 66 ** OP_Noop bef
1cb48 6f 72 65 20 65 78 65 63 74 69 6f 6e 2e 0a 20 20 ore exection..
1cb49 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 53 65 74 */. {OP_Set
1cb4a 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 30 NumColumns, 0, 0
1cb4b 2c 20 30 7d 2c 20 20 20 2f 2a 20 32 3a 20 4e 75 , 0}, /* 2: Nu
1cb4c 6d 20 63 6f 6c 73 20 66 6f 72 20 63 75 72 73 6f m cols for curso
1cb4d 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 r */. {OP_Ope
1cb4e 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 30 7d 2c nRead, 0, 0, 0},
1cb4f 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 4f 70 /* 3: Op
1cb50 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20 en cursor 0 for
1cb51 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 20 20 7b reading */. {
1cb52 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 OP_SetNumColumns
1cb53 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 2f 2a , 0, 0, 0}, /*
1cb54 20 34 3a 20 4e 75 6d 20 63 6f 6c 73 20 66 6f 72 4: Num cols for
1cb55 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b cursor */. {
1cb56 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c OP_OpenWrite, 0,
1cb57 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 0, 0}, /*
1cb58 20 35 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 5: Open cursor
1cb59 30 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 0 for read/write
1cb5a 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56 61 72 */.. {OP_Var
1cb5b 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 30 7d 2c iable, 1, 1, 0},
1cb5c 20 20 20 20 20 20 20 20 2f 2a 20 36 3a 20 50 75 /* 6: Pu
1cb5d 73 68 20 74 68 65 20 72 6f 77 69 64 20 74 6f 20 sh the rowid to
1cb5e 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 20 the stack */.
1cb5f 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 {OP_NotExists,
1cb60 30 2c 20 31 30 2c 20 31 7d 2c 20 20 20 20 20 20 0, 10, 1},
1cb61 2f 2a 20 37 3a 20 53 65 65 6b 20 74 68 65 20 63 /* 7: Seek the c
1cb62 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 ursor */. {OP
1cb63 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 31 _Column, 0, 0, 1
1cb64 7d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 38 }, /* 8
1cb65 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 52 65 73 */. {OP_Res
1cb66 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c 20 30 7d ultRow, 1, 0, 0}
1cb67 2c 20 20 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f , /* 9 */
1cb68 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65 2c 20 . {OP_Close,
1cb69 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 0, 0, 0},
1cb6a 20 20 20 20 2f 2a 20 31 30 20 20 2a 2f 0a 20 20 /* 10 */.
1cb6b 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 {OP_Halt, 0, 0
1cb6c 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 , 0},
1cb6d 20 2f 2a 20 31 31 20 2a 2f 0a 20 20 7d 3b 0a 0a /* 11 */. };..
1cb6e 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20 Vdbe *v = 0;.
1cb6f 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1cb70 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 _OK;. char zErr
1cb71 5b 31 32 38 5d 3b 0a 0a 20 20 7a 45 72 72 5b 30 [128];.. zErr[0
1cb72 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 ] = 0;. sqlite3
1cb73 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d _mutex_enter(db-
1cb74 3e 6d 75 74 65 78 29 3b 0a 20 20 64 6f 20 7b 0a >mutex);. do {.
1cb75 20 20 20 20 50 61 72 73 65 20 73 50 61 72 73 65 Parse sParse
1cb76 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 ;. Table *pTa
1cb77 62 3b 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 b;.. memset(&
1cb78 73 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f sParse, 0, sizeo
1cb79 66 28 50 61 72 73 65 29 29 3b 0a 20 20 20 20 73 f(Parse));. s
1cb7a 50 61 72 73 65 2e 64 62 20 3d 20 64 62 3b 0a 0a Parse.db = db;..
1cb7b 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1cb7c 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 afetyOn(db) ){.
1cb7d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
1cb7e 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 ex_leave(db->mut
1cb7f 65 78 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ex);. retur
1cb80 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
1cb81 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 . }.. sqli
1cb82 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
1cb83 28 64 62 29 3b 0a 20 20 20 20 70 54 61 62 20 3d (db);. pTab =
1cb84 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 sqlite3LocateTa
1cb85 62 6c 65 28 26 73 50 61 72 73 65 2c 20 30 2c 20 ble(&sParse, 0,
1cb86 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20 20 zTable, zDb);.
1cb87 20 20 69 66 28 20 70 54 61 62 20 26 26 20 49 73 if( pTab && Is
1cb88 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b Virtual(pTab) ){
1cb89 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b . pTab = 0;
1cb8a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
1cb8b 72 6f 72 4d 73 67 28 26 73 50 61 72 73 65 2c 20 rorMsg(&sParse,
1cb8c 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 72 "cannot open vir
1cb8d 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 22 2c tual table: %s",
1cb8e 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a zTable);. }.
1cb8f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1cb90 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69 66 28 MIT_VIEW. if(
1cb91 20 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 pTab && pTab->p
1cb92 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 Select ){.
1cb93 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 pTab = 0;.
1cb94 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1cb95 26 73 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 &sParse, "cannot
1cb96 20 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22 2c open view: %s",
1cb97 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a zTable);. }.
1cb98 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21 #endif. if( !
1cb99 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 66 pTab ){. if
1cb9a 28 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 ( sParse.zErrMsg
1cb9b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1cb9c 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
1cb9d 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c eof(zErr), zErr,
1cb9e 20 22 25 73 22 2c 20 73 50 61 72 73 65 2e 7a 45 "%s", sParse.zE
1cb9f 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a rrMsg);. }.
1cba0 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1cba1 72 65 65 28 64 62 2c 20 73 50 61 72 73 65 2e 7a ree(db, sParse.z
1cba2 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 72 ErrMsg);. r
1cba3 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1cba4 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 ;. (void)sq
1cba5 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1cba6 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 b);. sqlite
1cba7 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 3BtreeLeaveAll(d
1cba8 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 b);. goto b
1cba9 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 lob_open_out;.
1cbaa 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 }.. /* Now
1cbab 73 65 61 72 63 68 20 70 54 61 62 20 66 6f 72 20 search pTab for
1cbac 74 68 65 20 65 78 61 63 74 20 63 6f 6c 75 6d 6e the exact column
1cbad 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 6f . */. for(iCo
1cbae 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 61 62 l=0; iCol < pTab
1cbaf 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 20 ->nCol; iCol++)
1cbb0 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 {. if( sqli
1cbb1 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d te3StrICmp(pTab-
1cbb2 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d >aCol[iCol].zNam
1cbb3 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 e, zColumn)==0 )
1cbb4 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b {. break;
1cbb5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1cbb6 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61 if( iCol==pTa
1cbb7 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 b->nCol ){.
1cbb8 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1cbb9 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 f(sizeof(zErr),
1cbba 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 20 63 zErr, "no such c
1cbbb 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20 olumn: \"%s\"",
1cbbc 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 zColumn);.
1cbbd 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1cbbe 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 R;. (void)s
1cbbf 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1cbc0 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 db);. sqlit
1cbc1 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 e3BtreeLeaveAll(
1cbc2 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 db);. goto
1cbc3 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 blob_open_out;.
1cbc4 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
1cbc5 74 68 65 20 76 61 6c 75 65 20 69 73 20 62 65 69 the value is bei
1cbc6 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 ng opened for wr
1cbc7 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 68 61 iting, check tha
1cbc8 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c t the. ** col
1cbc9 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 65 78 umn is not index
1cbca 65 64 2e 20 49 74 20 69 73 20 61 67 61 69 6e 73 ed. It is agains
1cbcb 74 20 74 68 65 20 72 75 6c 65 73 20 74 6f 20 6f t the rules to o
1cbcc 70 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 69 6e pen an. ** in
1cbcd 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 66 6f 72 dexed column for
1cbce 20 77 72 69 74 69 6e 67 2e 0a 20 20 20 20 2a 2f writing.. */
1cbcf 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 29 . if( flags )
1cbd0 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 {. Index *p
1cbd1 49 64 78 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 Idx;. for(p
1cbd2 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 Idx=pTab->pIndex
1cbd3 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 ; pIdx; pIdx=pId
1cbd4 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 x->pNext){.
1cbd5 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 int j;.
1cbd6 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 for(j=0; j<pId
1cbd7 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 x->nColumn; j++)
1cbd8 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
1cbd9 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a pIdx->aiColumn[j
1cbda 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 ]==iCol ){.
1cbdb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
1cbdc 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
1cbdd 45 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 Err), zErr,.
1cbde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cbdf 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 "cannot
1cbe0 20 6f 70 65 6e 20 69 6e 64 65 78 65 64 20 63 6f open indexed co
1cbe1 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 lumn for writing
1cbe2 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ");.
1cbe3 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1cbe4 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 28 R;. (
1cbe5 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
1cbe6 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 tyOff(db);.
1cbe7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 sqlite3Bt
1cbe8 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b reeLeaveAll(db);
1cbe9 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 . got
1cbea 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b o blob_open_out;
1cbeb 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1cbec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
1cbed 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73 71 }.. v = sq
1cbee 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28 lite3VdbeCreate(
1cbef 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20 29 db);. if( v )
1cbf0 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 {. int iDb
1cbf1 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
1cbf2 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d oIndex(db, pTab-
1cbf3 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 >pSchema);.
1cbf4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1cbf5 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66 28 pList(v, sizeof(
1cbf6 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f 66 openBlob)/sizeof
1cbf7 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f 70 (VdbeOpList), op
1cbf8 65 6e 42 6c 6f 62 29 3b 0a 0a 20 20 20 20 20 20 enBlob);..
1cbf9 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 /* Configure the
1cbfa 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 OP_Transaction
1cbfb 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1cbfc 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 VdbeChangeP1(v,
1cbfd 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 0, iDb);. s
1cbfe 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1cbff 50 32 28 76 2c 20 30 2c 20 28 66 6c 61 67 73 20 P2(v, 0, (flags
1cc00 3f 20 31 20 3a 20 30 29 29 3b 0a 0a 20 20 20 20 ? 1 : 0));..
1cc01 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 /* Configure t
1cc02 68 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b he OP_VerifyCook
1cc03 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 ie */. sqli
1cc04 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 te3VdbeChangeP1(
1cc05 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 v, 1, iDb);.
1cc06 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1cc07 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 54 61 62 ngeP2(v, 1, pTab
1cc08 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d ->pSchema->schem
1cc09 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 a_cookie);..
1cc0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 /* Make sure a
1cc0b 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 6f mutex is held o
1cc0c 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 n the table to b
1cc0d 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 e accessed */.
1cc0e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 sqlite3VdbeU
1cc0f 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 sesBtree(v, iDb)
1cc10 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d ; .. /* Rem
1cc11 6f 76 65 20 65 69 74 68 65 72 20 74 68 65 20 4f ove either the O
1cc12 50 5f 4f 70 65 6e 57 72 69 74 65 20 6f 72 20 4f P_OpenWrite or O
1cc13 70 65 6e 52 65 61 64 2e 20 53 65 74 20 74 68 65 penRead. Set the
1cc14 20 50 32 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 P2 . ** pa
1cc15 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 6f rameter of the o
1cc16 74 68 65 72 20 74 6f 20 70 54 61 62 2d 3e 74 6e ther to pTab->tn
1cc17 75 6d 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 um. . */.
1cc18 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1cc19 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 28 hangeToNoop(v, (
1cc1a 66 6c 61 67 73 20 3f 20 33 20 3a 20 35 29 2c 20 flags ? 3 : 5),
1cc1b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 1);. sqlite
1cc1c 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 3VdbeChangeP2(v,
1cc1d 20 28 66 6c 61 67 73 20 3f 20 35 20 3a 20 33 29 (flags ? 5 : 3)
1cc1e 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 , pTab->tnum);.
1cc1f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1cc20 43 68 61 6e 67 65 50 33 28 76 2c 20 28 66 6c 61 ChangeP3(v, (fla
1cc21 67 73 20 3f 20 35 20 3a 20 33 29 2c 20 69 44 62 gs ? 5 : 3), iDb
1cc22 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e );.. /* Con
1cc23 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 53 65 figure the OP_Se
1cc24 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2e 20 43 6f 6e tNumColumns. Con
1cc25 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73 6f figure the curso
1cc26 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 r to. ** th
1cc27 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61 62 ink that the tab
1cc28 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65 20 le has one more
1cc29 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20 72 column than it r
1cc2a 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 64 eally. ** d
1cc2b 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75 6d oes. An OP_Colum
1cc2c 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 n to retrieve th
1cc2d 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f 6c is imaginary col
1cc2e 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a umn will. *
1cc2f 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 * always return
1cc30 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68 69 an SQL NULL. Thi
1cc31 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63 61 s is useful beca
1cc32 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 use it means.
1cc33 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e 76 ** we can inv
1cc34 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f oke OP_Column to
1cc35 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64 62 fill in the vdb
1cc36 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20 0a e cursors type .
1cc37 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66 66 ** and off
1cc38 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f 75 set cache withou
1cc39 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49 4f t causing any IO
1cc3a 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1cc3b 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1cc3c 67 65 50 32 28 76 2c 20 66 6c 61 67 73 20 3f 20 geP2(v, flags ?
1cc3d 34 20 3a 20 32 2c 20 70 54 61 62 2d 3e 6e 43 6f 4 : 2, pTab->nCo
1cc3e 6c 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 l+1);. sqli
1cc3f 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 te3VdbeChangeP2(
1cc40 76 2c 20 38 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c v, 8, pTab->nCol
1cc41 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 );. if( !db
1cc42 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1cc43 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1cc44 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 76 3VdbeMakeReady(v
1cc45 2c 20 31 2c 20 31 2c 20 31 2c 20 30 29 3b 0a 20 , 1, 1, 1, 0);.
1cc46 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1cc47 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1cc48 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 eLeaveAll(db);.
1cc49 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 rc = sqlite3S
1cc4a 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 afetyOff(db);.
1cc4b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1cc4c 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f _OK || db->mallo
1cc4d 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 cFailed ){.
1cc4e 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f goto blob_open_
1cc4f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 out;. }..
1cc50 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 sqlite3_bind_int
1cc51 36 34 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 64((sqlite3_stmt
1cc52 20 2a 29 76 2c 20 31 2c 20 69 52 6f 77 29 3b 0a *)v, 1, iRow);.
1cc53 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1cc54 5f 73 74 65 70 28 28 73 71 6c 69 74 65 33 5f 73 _step((sqlite3_s
1cc55 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 69 66 tmt *)v);. if
1cc56 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 ( rc!=SQLITE_ROW
1cc57 20 29 7b 0a 20 20 20 20 20 20 6e 41 74 74 65 6d ){. nAttem
1cc58 70 74 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d pt++;. rc =
1cc59 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a sqlite3_finaliz
1cc5a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 e((sqlite3_stmt
1cc5b 2a 29 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 *)v);. sqli
1cc5c 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
1cc5d 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c eof(zErr), zErr,
1cc5e 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 sqlite3_errmsg(
1cc5f 64 62 29 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 db));. v =
1cc60 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 0;. }. } whi
1cc61 6c 65 28 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 le( nAttempt<5 &
1cc62 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 & rc==SQLITE_SCH
1cc63 45 4d 41 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 EMA );.. if( rc
1cc64 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a ==SQLITE_ROW ){.
1cc65 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 /* The row-r
1cc66 65 63 6f 72 64 20 68 61 73 20 62 65 65 6e 20 6f ecord has been o
1cc67 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c pened successful
1cc68 6c 79 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 ly. Check that t
1cc69 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e he. ** column
1cc6a 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e in question con
1cc6b 74 61 69 6e 73 20 74 65 78 74 20 6f 72 20 61 20 tains text or a
1cc6c 62 6c 6f 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 blob. If it cont
1cc6d 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 ains. ** text
1cc6e 2c 20 69 74 20 69 73 20 75 70 20 74 6f 20 74 68 , it is up to th
1cc6f 65 20 63 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 e caller to get
1cc70 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 the encoding rig
1cc71 68 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 ht.. */. I
1cc72 6e 63 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a ncrblob *pBlob;.
1cc73 20 20 20 20 75 33 32 20 74 79 70 65 20 3d 20 76 u32 type = v
1cc74 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 ->apCsr[0]->aTyp
1cc75 65 5b 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 e[iCol];.. if
1cc76 28 20 74 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 ( type<12 ){.
1cc77 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1cc78 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 ntf(sizeof(zErr)
1cc79 2c 20 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20 , zErr, "cannot
1cc7a 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 74 79 open value of ty
1cc7b 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 pe %s",.
1cc7c 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 type==0?"null"
1cc7d 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c 22 : type==7?"real"
1cc7e 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20 20 20 : "integer".
1cc7f 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
1cc80 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1cc81 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 goto blob_op
1cc82 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 en_out;. }.
1cc83 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72 62 pBlob = (Incrb
1cc84 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 62 4d lob *)sqlite3DbM
1cc85 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 allocZero(db, si
1cc86 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 29 3b zeof(Incrblob));
1cc87 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c . if( db->mal
1cc88 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
1cc89 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1cc8a 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20 (db, pBlob);.
1cc8b 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 goto blob_ope
1cc8c 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 n_out;. }.
1cc8d 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 pBlob->flags =
1cc8e 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c 6f 62 flags;. pBlob
1cc8f 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 70 43 ->pCsr = v->apC
1cc90 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a sr[0]->pCursor;.
1cc91 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1cc92 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42 6c 6f EnterCursor(pBlo
1cc93 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 71 b->pCsr);. sq
1cc94 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65 4f lite3BtreeCacheO
1cc95 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 verflow(pBlob->p
1cc96 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Csr);. sqlite
1cc97 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 3BtreeLeaveCurso
1cc98 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a r(pBlob->pCsr);.
1cc99 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 pBlob->pStmt
1cc9a 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 = (sqlite3_stmt
1cc9b 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f 62 2d *)v;. pBlob-
1cc9c 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e 61 70 >iOffset = v->ap
1cc9d 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 74 5b Csr[0]->aOffset[
1cc9e 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 iCol];. pBlob
1cc9f 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 ->nByte = sqlite
1cca0 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 3VdbeSerialTypeL
1cca1 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 70 42 en(type);. pB
1cca2 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 lob->db = db;.
1cca3 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 71 6c *ppBlob = (sql
1cca4 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f ite3_blob *)pBlo
1cca5 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 b;. rc = SQLI
1cca6 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 TE_OK;. }else i
1cca7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1cca8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
1cca9 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
1ccaa 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 6e 6f zErr), zErr, "no
1ccab 20 73 75 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c such rowid: %ll
1ccac 64 22 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 d", iRow);. r
1ccad 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1ccae 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e ;. }..blob_open
1ccaf 5f 6f 75 74 3a 0a 20 20 7a 45 72 72 5b 73 69 7a _out:. zErr[siz
1ccb0 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20 3d 20 27 eof(zErr)-1] = '
1ccb1 5c 30 27 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 \0';. if( rc!=S
1ccb2 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e QLITE_OK || db->
1ccb3 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
1ccb4 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 sqlite3_fina
1ccb5 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 lize((sqlite3_st
1ccb6 6d 74 20 2a 29 76 29 3b 0a 20 20 7d 0a 20 20 73 mt *)v);. }. s
1ccb7 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 qlite3Error(db,
1ccb8 72 63 2c 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f rc, (rc==SQLITE_
1ccb9 4f 4b 3f 30 3a 7a 45 72 72 29 29 3b 0a 20 20 72 OK?0:zErr));. r
1ccba 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 c = sqlite3ApiEx
1ccbb 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 it(db, rc);. sq
1ccbc 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1ccbd 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 e(db->mutex);.
1ccbe 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1ccbf 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c 6f 62 .** Close a blob
1ccc0 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 73 handle that was
1ccc1 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 65 61 previously crea
1ccc2 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c ted using.** sql
1ccc3 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 ite3_blob_open()
1ccc4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1ccc5 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
1ccc6 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 _close(sqlite3_b
1ccc7 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 lob *pBlob){. I
1ccc8 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e ncrblob *p = (In
1ccc9 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a crblob *)pBlob;.
1ccca 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 int rc;.. rc
1cccb 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 = sqlite3_finali
1cccc 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 ze(p->pStmt);.
1cccd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d sqlite3DbFree(p-
1ccce 3e 64 62 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 >db, p);. retur
1cccf 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 n rc;.}../*.** P
1ccd0 65 72 66 6f 72 6d 20 61 20 72 65 61 64 20 6f 72 erform a read or
1ccd1 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e write operation
1ccd2 20 6f 6e 20 61 20 62 6c 6f 62 0a 2a 2f 0a 73 74 on a blob.*/.st
1ccd3 61 74 69 63 20 69 6e 74 20 62 6c 6f 62 52 65 61 atic int blobRea
1ccd4 64 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 dWrite(. sqlite
1ccd5 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 0a 3_blob *pBlob, .
1ccd6 20 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e void *z, . in
1ccd7 74 20 6e 2c 20 0a 20 20 69 6e 74 20 69 4f 66 66 t n, . int iOff
1ccd8 73 65 74 2c 20 0a 20 20 69 6e 74 20 28 2a 78 43 set, . int (*xC
1ccd9 61 6c 6c 29 28 42 74 43 75 72 73 6f 72 2a 2c 20 all)(BtCursor*,
1ccda 75 33 32 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29 u32, u32, void*)
1ccdb 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
1ccdc 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 Incrblob *p = (I
1ccdd 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b ncrblob *)pBlob;
1ccde 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 . Vdbe *v;. sq
1ccdf 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 lite3 *db = p->d
1cce0 62 3b 20 20 0a 0a 20 20 73 71 6c 69 74 65 33 5f b; .. sqlite3_
1cce1 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e mutex_enter(db->
1cce2 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 mutex);. v = (V
1cce3 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a dbe*)p->pStmt;..
1cce4 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 if( n<0 || iOf
1cce5 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 fset<0 || (iOffs
1cce6 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 et+n)>p->nByte )
1cce7 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75 65 73 74 {. /* Request
1cce8 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 is out of range
1cce9 2e 20 52 65 74 75 72 6e 20 61 20 74 72 61 6e 73 . Return a trans
1ccea 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 ient error. */.
1cceb 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
1ccec 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 RROR;. sqlite
1cced 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 3Error(db, SQLIT
1ccee 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d E_ERROR, 0);. }
1ccef 20 65 6c 73 65 20 69 66 28 20 76 3d 3d 30 20 29 else if( v==0 )
1ccf0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 {. /* If ther
1ccf1 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e e is no statemen
1ccf2 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 t handle, then t
1ccf3 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 he blob-handle h
1ccf4 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 as. ** alread
1ccf5 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 y been invalidat
1ccf6 65 64 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 ed. Return SQLIT
1ccf7 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 69 73 20 E_ABORT in this
1ccf8 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 case.. */.
1ccf9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f rc = SQLITE_ABO
1ccfa 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 RT;. }else{.
1ccfb 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65 72 20 /* Call either
1ccfc 42 74 72 65 65 44 61 74 61 28 29 20 6f 72 20 42 BtreeData() or B
1ccfd 74 72 65 65 50 75 74 44 61 74 61 28 29 2e 20 49 treePutData(). I
1ccfe 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 f SQLITE_ABORT i
1ccff 73 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 s. ** returne
1cd00 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 d, clean-up the
1cd01 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 statement handle
1cd02 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
1cd03 65 72 74 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 ert( db == v->db
1cd04 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 );. sqlite3B
1cd05 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 treeEnterCursor(
1cd06 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 72 63 p->pCsr);. rc
1cd07 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 = xCall(p->pCsr
1cd08 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 , iOffset+p->iOf
1cd09 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 fset, n, z);.
1cd0a 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1cd0b 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 veCursor(p->pCsr
1cd0c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1cd0d 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 QLITE_ABORT ){.
1cd0e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1cd0f 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 Finalize(v);.
1cd10 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b p->pStmt = 0;
1cd11 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1cd12 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 db->errCode =
1cd13 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 rc;. v->rc
1cd14 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = rc;. }. }.
1cd15 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 rc = sqlite3Ap
1cd16 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 iExit(db, rc);.
1cd17 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1cd18 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b eave(db->mutex);
1cd19 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1cd1a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 ./*.** Read data
1cd1b 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e from a blob han
1cd1c 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 dle..*/.SQLITE_A
1cd1d 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1cd1e 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33 lob_read(sqlite3
1cd1f 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f _blob *pBlob, vo
1cd20 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e id *z, int n, in
1cd21 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 t iOffset){. re
1cd22 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 turn blobReadWri
1cd23 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 te(pBlob, z, n,
1cd24 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 iOffset, sqlite3
1cd25 42 74 72 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f BtreeData);.}../
1cd26 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 *.** Write data
1cd27 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 to a blob handle
1cd28 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1cd29 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
1cd2a 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 _write(sqlite3_b
1cd2b 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 lob *pBlob, cons
1cd2c 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e t void *z, int n
1cd2d 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a , int iOffset){.
1cd2e 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 return blobRea
1cd2f 64 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 dWrite(pBlob, (v
1cd30 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 oid *)z, n, iOff
1cd31 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65 set, sqlite3Btre
1cd32 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a ePutData);.}../*
1cd33 0a 2a 2a 20 51 75 65 72 79 20 61 20 62 6c 6f 62 .** Query a blob
1cd34 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 handle for the
1cd35 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
1cd36 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 ..**.** The Incr
1cd37 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 blob.nByte field
1cd38 20 69 73 20 66 69 78 65 64 20 66 6f 72 20 74 68 is fixed for th
1cd39 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 e lifetime of th
1cd3a 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f e Incrblob.** so
1cd3b 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 72 65 71 no mutex is req
1cd3c 75 69 72 65 64 20 66 6f 72 20 61 63 63 65 73 73 uired for access
1cd3d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1cd3e 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
1cd3f 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 62 _bytes(sqlite3_b
1cd40 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 lob *pBlob){. I
1cd41 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e ncrblob *p = (In
1cd42 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a crblob *)pBlob;.
1cd43 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 42 79 74 return p->nByt
1cd44 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 e;.}..#endif /*
1cd45 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1cd46 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a MIT_INCRBLOB */.
1cd47 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1cd48 20 45 6e 64 20 6f 66 20 76 64 62 65 62 6c 6f 62 End of vdbeblob
1cd49 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1cd4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cd4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1cd4c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1cd4d 20 42 65 67 69 6e 20 66 69 6c 65 20 6a 6f 75 72 Begin file jour
1cd4e 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nal.c **********
1cd4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1cd51 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
1cd52 73 74 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 22.**.** The
1cd53 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1cd54 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1cd55 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1cd56 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1cd57 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1cd58 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1cd59 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1cd5a 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1cd5b 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1cd5c 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1cd5d 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1cd5e 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1cd5f 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1cd60 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1cd61 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1cd62 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1cd63 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1cd64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cd65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cd66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cd67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cd68 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 ****.**.** @(#)
1cd69 24 49 64 3a 20 6a 6f 75 72 6e 61 6c 2e 63 2c 76 $Id: journal.c,v
1cd6a 20 31 2e 38 20 32 30 30 38 2f 30 35 2f 30 31 20 1.8 2008/05/01
1cd6b 31 38 3a 30 31 3a 34 37 20 64 72 68 20 45 78 70 18:01:47 drh Exp
1cd6c 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 $.*/..#ifdef SQ
1cd6d 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d LITE_ENABLE_ATOM
1cd6e 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a 20 IC_WRITE../*.**
1cd6f 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d This file implem
1cd70 65 6e 74 73 20 61 20 73 70 65 63 69 61 6c 20 6b ents a special k
1cd71 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 ind of sqlite3_f
1cd72 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 64 0a ile object used.
1cd73 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 ** by SQLite to
1cd74 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 create journal f
1cd75 69 6c 65 73 20 69 66 20 74 68 65 20 61 74 6f 6d iles if the atom
1cd76 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a ic-write optimiz
1cd77 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 ation.** is enab
1cd78 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 led..**.** The d
1cd79 69 73 74 69 6e 63 74 69 76 65 20 63 68 61 72 61 istinctive chara
1cd7a 63 74 65 72 69 73 74 69 63 20 6f 66 20 74 68 69 cteristic of thi
1cd7b 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 s sqlite3_file i
1cd7c 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 s that the.** ac
1cd7d 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c tual on disk fil
1cd7e 65 20 69 73 20 63 72 65 61 74 65 64 20 6c 61 7a e is created laz
1cd7f 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 20 66 69 ily. When the fi
1cd80 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c 0a 2a le is created,.*
1cd81 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 73 70 65 * the caller spe
1cd82 63 69 66 69 65 73 20 61 20 62 75 66 66 65 72 20 cifies a buffer
1cd83 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d size for an in-m
1cd84 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 6f 0a emory buffer to.
1cd85 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 ** be used to se
1cd86 72 76 69 63 65 20 72 65 61 64 28 29 20 61 6e 64 rvice read() and
1cd87 20 77 72 69 74 65 28 29 20 72 65 71 75 65 73 74 write() request
1cd88 73 2e 20 54 68 65 20 61 63 74 75 61 6c 20 66 69 s. The actual fi
1cd89 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 le.** on disk is
1cd8a 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f 72 20 not created or
1cd8b 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 populated until
1cd8c 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 either:.**.**
1cd8d 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 1) The in-memory
1cd8e 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1cd8f 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 grows too large
1cd90 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 for the allocate
1cd91 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 66 66 65 d .** buffe
1cd92 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 r, or.** 2) Th
1cd93 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 e xSync() method
1cd94 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a is called..*/..
1cd95 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e 61 ../*.** A Journa
1cd96 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 lFile object is
1cd97 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 a subclass of sq
1cd98 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65 64 20 lite3_file used
1cd99 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65 6e by.** as an open
1cd9a 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 file handle for
1cd9b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a journal files..
1cd9c 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 */.struct Journa
1cd9d 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 lFile {. sqlite
1cd9e 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 3_io_methods *pM
1cd9f 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f 4f ethod; /* I/O
1cda0 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 72 methods on jour
1cda1 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 69 nal files */. i
1cda2 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 20 20 20 nt nBuf;
1cda3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1cda4 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b 5d * Size of zBuf[]
1cda5 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 in bytes */. c
1cda6 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 20 20 20 har *zBuf;
1cda7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1cda8 2a 20 53 70 61 63 65 20 74 6f 20 62 75 66 66 65 * Space to buffe
1cda9 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65 73 r journal writes
1cdaa 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65 3b */. int iSize;
1cdab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cdac 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 /* Amount
1cdad 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 72 65 6e of zBuf[] curren
1cdae 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e tly used */. in
1cdaf 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 t flags;
1cdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1cdb1 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f 0a xOpen flags */.
1cdb2 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
1cdb3 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 Vfs;
1cdb4 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 /* The "real"
1cdb5 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 53 20 2a underlying VFS *
1cdb6 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
1cdb7 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20 *pReal;
1cdb8 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c /* The "real
1cdb9 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c " underlying fil
1cdba 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a e descriptor */.
1cdbb 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a const char *zJ
1cdbc 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 ournal;
1cdbd 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
1cdbe 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f journal file */
1cdbf 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 .};.typedef stru
1cdc0 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a ct JournalFile J
1cdc1 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a ournalFile;../*.
1cdc2 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f ** If it does no
1cdc3 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 t already exists
1cdc4 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70 6f 70 , create and pop
1cdc5 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64 69 73 ulate the on-dis
1cdc6 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 4a k file .** for J
1cdc7 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a 2f ournalFile p..*/
1cdc8 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 .static int crea
1cdc9 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 69 teFile(JournalFi
1cdca 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 le *p){. int rc
1cdcb 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1cdcc 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20 29 7b if( !p->pReal ){
1cdcd 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c . sqlite3_fil
1cdce 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c 69 e *pReal = (sqli
1cdcf 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31 5d te3_file *)&p[1]
1cdd0 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1cdd1 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66 73 e3OsOpen(p->pVfs
1cdd2 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 , p->zJournal, p
1cdd3 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c 20 Real, p->flags,
1cdd4 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 0);. if( rc==
1cdd5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1cdd6 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70 52 p->pReal = pR
1cdd7 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 eal;. if( p
1cdd8 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 ->iSize>0 ){.
1cdd9 20 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 69 assert(p->i
1cdda 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0a Size<=p->nBuf);.
1cddb 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1cddc 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 ite3OsWrite(p->p
1cddd 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70 Real, p->zBuf, p
1cdde 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 ->iSize, 0);.
1cddf 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1cde0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1cde1 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 *.** Close the f
1cde2 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
1cde3 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c nt jrnlClose(sql
1cde4 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 ite3_file *pJfd)
1cde5 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 {. JournalFile
1cde6 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c *p = (JournalFil
1cde7 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 e *)pJfd;. if(
1cde8 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 p->pReal ){.
1cde9 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
1cdea 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 ->pReal);. }.
1cdeb 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e sqlite3_free(p->
1cdec 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 zBuf);. return
1cded 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
1cdee 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 .** Read data fr
1cdef 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a om the file..*/.
1cdf0 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 52 static int jrnlR
1cdf1 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 ead(. sqlite3_f
1cdf2 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a ile *pJfd, /*
1cdf3 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c The journal fil
1cdf4 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 e from which to
1cdf5 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a read */. void *
1cdf6 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 zBuf,
1cdf7 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 /* Put the resu
1cdf8 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e lts here */. in
1cdf9 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 t iAmt,
1cdfa 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1cdfb 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 f bytes to read
1cdfc 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 */. sqlite_int6
1cdfd 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 4 iOfst /* B
1cdfe 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 egin reading at
1cdff 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 this offset */.)
1ce00 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
1ce01 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 ITE_OK;. Journa
1ce02 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 lFile *p = (Jour
1ce03 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a nalFile *)pJfd;.
1ce04 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 if( p->pReal )
1ce05 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1ce06 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 52 65 61 e3OsRead(p->pRea
1ce07 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 l, zBuf, iAmt, i
1ce08 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Ofst);. }else{.
1ce09 20 20 20 20 61 73 73 65 72 74 28 20 69 41 6d 74 assert( iAmt
1ce0a 2b 69 4f 66 73 74 3c 3d 70 2d 3e 69 53 69 7a 65 +iOfst<=p->iSize
1ce0b 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a );. memcpy(z
1ce0c 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b 69 4f Buf, &p->zBuf[iO
1ce0d 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20 20 7d fst], iAmt);. }
1ce0e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1ce0f 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 ./*.** Write dat
1ce10 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a a to the file..*
1ce11 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e /.static int jrn
1ce12 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 lWrite(. sqlite
1ce13 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 3_file *pJfd,
1ce14 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 /* The journal
1ce15 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 file into which
1ce16 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 6f to write */. co
1ce17 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 nst void *zBuf,
1ce18 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 74 /* Take dat
1ce19 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 a to be written
1ce1a 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 69 from here */. i
1ce1b 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 nt iAmt,
1ce1c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1ce1d 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 of bytes to writ
1ce1e 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e e */. sqlite_in
1ce1f 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a t64 iOfst /*
1ce20 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 Begin writing a
1ce21 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 6e t this offset in
1ce22 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29 to the file */.)
1ce23 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
1ce24 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 ITE_OK;. Journa
1ce25 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 lFile *p = (Jour
1ce26 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a nalFile *)pJfd;.
1ce27 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20 if( !p->pReal
1ce28 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 3e && (iOfst+iAmt)>
1ce29 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20 20 72 p->nBuf ){. r
1ce2a 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65 28 70 c = createFile(p
1ce2b 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d );. }. if( rc=
1ce2c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1ce2d 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 if( p->pReal )
1ce2e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
1ce2f 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 ite3OsWrite(p->p
1ce30 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 Real, zBuf, iAmt
1ce31 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20 7d 65 , iOfst);. }e
1ce32 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 lse{. memcp
1ce33 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 74 y(&p->zBuf[iOfst
1ce34 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29 3b 0a ], zBuf, iAmt);.
1ce35 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53 69 if( p->iSi
1ce36 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74 29 20 ze<(iOfst+iAmt)
1ce37 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 53 ){. p->iS
1ce38 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69 41 6d ize = (iOfst+iAm
1ce39 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 t);. }.
1ce3a 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
1ce3b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e c;.}../*.** Trun
1ce3c 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a cate the file..*
1ce3d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e /.static int jrn
1ce3e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 lTruncate(sqlite
1ce3f 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 3_file *pJfd, sq
1ce40 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 29 lite_int64 size)
1ce41 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
1ce42 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 ITE_OK;. Journa
1ce43 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 lFile *p = (Jour
1ce44 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a nalFile *)pJfd;.
1ce45 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 if( p->pReal )
1ce46 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1ce47 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 2d 3e e3OsTruncate(p->
1ce48 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a 20 20 pReal, size);.
1ce49 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 3c 70 }else if( size<p
1ce4a 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 70 ->iSize ){. p
1ce4b 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a ->iSize = size;.
1ce4c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1ce4d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 .}../*.** Sync t
1ce4e 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 he file..*/.stat
1ce4f 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e 63 28 ic int jrnlSync(
1ce50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1ce51 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a fd, int flags){.
1ce52 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f 75 72 int rc;. Jour
1ce53 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f nalFile *p = (Jo
1ce54 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 urnalFile *)pJfd
1ce55 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c ;. if( p->pReal
1ce56 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1ce57 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e 70 52 ite3OsSync(p->pR
1ce58 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20 20 7d eal, flags);. }
1ce59 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 else{. rc = S
1ce5a 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
1ce5b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1ce5c 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69 .** Query the si
1ce5d 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ze of the file i
1ce5e 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 n bytes..*/.stat
1ce5f 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c 65 53 ic int jrnlFileS
1ce60 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ize(sqlite3_file
1ce61 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 *pJfd, sqlite_i
1ce62 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 nt64 *pSize){.
1ce63 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1ce64 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c OK;. JournalFil
1ce65 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 e *p = (JournalF
1ce66 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 ile *)pJfd;. if
1ce67 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 ( p->pReal ){.
1ce68 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1ce69 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52 65 61 FileSize(p->pRea
1ce6a 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d 65 6c l, pSize);. }el
1ce6b 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d se{. *pSize =
1ce6c 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 20 (sqlite_int64)
1ce6d 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 p->iSize;. }.
1ce6e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1ce6f 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 .** Table of met
1ce70 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e 61 6c hods for Journal
1ce71 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 6c File sqlite3_fil
1ce72 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 e object..*/.sta
1ce73 74 69 63 20 73 74 72 75 63 74 20 73 71 6c 69 74 tic struct sqlit
1ce74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4a 6f e3_io_methods Jo
1ce75 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 urnalFileMethods
1ce76 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 = {. 1,
1ce77 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
1ce78 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f 73 65 n */. jrnlClose
1ce79 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 , /* xClose
1ce7a 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c 20 20 */. jrnlRead,
1ce7b 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a /* xRead */.
1ce7c 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 20 jrnlWrite,
1ce7d 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 /* xWrite */.
1ce7e 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20 2f jrnlTruncate, /
1ce7f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a 20 * xTruncate */.
1ce80 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 jrnlSync,
1ce81 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6a 72 /* xSync */. jr
1ce82 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a 20 nlFileSize, /*
1ce83 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 30 xFileSize */. 0
1ce84 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
1ce85 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 xLock */. 0,
1ce86 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 /* xU
1ce87 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 nlock */. 0,
1ce88 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 68 /* xCh
1ce89 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
1ce8a 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 */. 0,
1ce8b 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 /* xFileCont
1ce8c 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 rol */. 0,
1ce8d 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74 /* xSect
1ce8e 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 20 orSize */. 0
1ce8f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 /* xD
1ce90 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
1ce91 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a tics */.};../* .
1ce92 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 ** Open a journa
1ce93 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 l file..*/.SQLIT
1ce94 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1ce95 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e lite3JournalOpen
1ce96 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
1ce97 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 2f *pVfs, /
1ce98 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 73 65 * The VFS to use
1ce99 20 66 6f 72 20 61 63 74 75 61 6c 20 66 69 6c 65 for actual file
1ce9a 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 I/O */. const
1ce9b 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 char *zName,
1ce9c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
1ce9d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1ce9e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
1ce9f 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 20 20 20 le *pJfd,
1cea0 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 /* Preallocated
1cea1 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 20 68 61 6e , blank file han
1cea2 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 dle */. int fla
1cea3 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
1cea4 20 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 20 66 /* Opening f
1cea5 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 lags */. int nB
1cea6 75 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 uf
1cea7 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 62 75 /* Bytes bu
1cea8 66 66 65 72 65 64 20 62 65 66 6f 72 65 20 6f 70 ffered before op
1cea9 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a ening the file *
1ceaa 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 /.){. JournalFi
1ceab 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c le *p = (Journal
1ceac 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 6d File *)pJfd;. m
1cead 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 6c 69 emset(p, 0, sqli
1ceae 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 te3JournalSize(p
1ceaf 56 66 73 29 29 3b 0a 20 20 69 66 28 20 6e 42 75 Vfs));. if( nBu
1ceb0 66 3e 30 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 42 f>0 ){. p->zB
1ceb1 75 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c uf = sqlite3Mall
1ceb2 6f 63 5a 65 72 6f 28 6e 42 75 66 29 3b 0a 20 20 ocZero(nBuf);.
1ceb3 20 20 69 66 28 20 21 70 2d 3e 7a 42 75 66 20 29 if( !p->zBuf )
1ceb4 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
1ceb5 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1ceb6 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
1ceb7 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 return sqlite3Os
1ceb8 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d 65 Open(pVfs, zName
1ceb9 2c 20 70 4a 66 64 2c 20 66 6c 61 67 73 2c 20 30 , pJfd, flags, 0
1ceba 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4d 65 74 );. }. p->pMet
1cebb 68 6f 64 20 3d 20 26 4a 6f 75 72 6e 61 6c 46 69 hod = &JournalFi
1cebc 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 20 70 2d 3e leMethods;. p->
1cebd 6e 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20 20 70 nBuf = nBuf;. p
1cebe 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b ->flags = flags;
1cebf 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d . p->zJournal =
1cec0 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d 3e 70 56 66 zName;. p->pVf
1cec1 73 20 3d 20 70 56 66 73 3b 0a 20 20 72 65 74 75 s = pVfs;. retu
1cec2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1cec3 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 72 ./*.** If the ar
1cec4 67 75 6d 65 6e 74 20 70 20 70 6f 69 6e 74 73 20 gument p points
1cec5 74 6f 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 to a JournalFile
1cec6 20 73 74 72 75 63 74 75 72 65 2c 20 61 6e 64 20 structure, and
1cec7 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a the underlying.*
1cec8 2a 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 79 * file has not y
1cec9 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c et been created,
1ceca 20 63 72 65 61 74 65 20 69 74 20 6e 6f 77 2e 0a create it now..
1cecb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1cecc 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 E int sqlite3Jou
1cecd 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69 74 rnalCreate(sqlit
1cece 65 33 5f 66 69 6c 65 20 2a 70 29 7b 0a 20 20 69 e3_file *p){. i
1cecf 66 28 20 70 2d 3e 70 4d 65 74 68 6f 64 73 21 3d f( p->pMethods!=
1ced0 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 &JournalFileMeth
1ced1 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ods ){. retur
1ced2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
1ced3 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74 65 . return create
1ced4 46 69 6c 65 28 28 4a 6f 75 72 6e 61 6c 46 69 6c File((JournalFil
1ced5 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a e *)p);.}../* .*
1ced6 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
1ced7 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 ber of bytes req
1ced8 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61 uired to store a
1ced9 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 68 61 JournalFile tha
1ceda 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 t uses vfs.** pV
1cedb 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 fs to create the
1cedc 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 underlying on-d
1cedd 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 isk files..*/.SQ
1cede 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1cedf 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 sqlite3JournalS
1cee0 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ize(sqlite3_vfs
1cee1 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75 72 6e *pVfs){. return
1cee2 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 (pVfs->szOsFile
1cee3 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61 6c 46 +sizeof(JournalF
1cee4 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a ile));.}.#endif.
1cee5 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1cee6 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 2e End of journal.
1cee7 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1cee8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cee9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1ceea 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1ceeb 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 6a Begin file memj
1ceec 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a ournal.c *******
1ceed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ceee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1ceef 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f ./*.** 2008 Octo
1cef0 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ber 7.**.** The
1cef1 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1cef2 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1cef3 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1cef4 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1cef5 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1cef6 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1cef7 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1cef8 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1cef9 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1cefa 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1cefb 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1cefc 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1cefd 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1cefe 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1ceff 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1cf00 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1cf01 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1cf02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cf03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cf04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cf05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cf06 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
1cf07 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
1cf08 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 6d de use to implem
1cf09 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 ent an in-memory
1cf0a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
1cf0b 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d l..** The in-mem
1cf0c 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 ory rollback jou
1cf0d 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74 6f 20 rnal is used to
1cf0e 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61 63 74 journal transact
1cf0f 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a 6d 65 ions for.** ":me
1cf10 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73 65 73 mory:" databases
1cf11 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 6a 6f and when the jo
1cf12 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f 52 urnal_mode=MEMOR
1cf13 59 20 70 72 61 67 6d 61 20 69 73 20 75 73 65 64 Y pragma is used
1cf14 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 ..**.** @(#) $Id
1cf15 3a 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 2c 76 : memjournal.c,v
1cf16 20 31 2e 38 20 32 30 30 38 2f 31 32 2f 32 30 20 1.8 2008/12/20
1cf17 30 32 3a 31 34 3a 34 30 20 64 72 68 20 45 78 70 02:14:40 drh Exp
1cf18 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 $.*/../* Forwar
1cf19 64 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 d references to
1cf1a 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75 internal structu
1cf1b 72 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 res */.typedef s
1cf1c 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c truct MemJournal
1cf1d 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74 79 70 MemJournal;.typ
1cf1e 65 64 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 edef struct File
1cf1f 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e 74 3b Point FilePoint;
1cf20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1cf21 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65 43 68 FileChunk FileCh
1cf22 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65 20 74 unk;../* Space t
1cf23 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c 6c 62 o hold the rollb
1cf24 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 ack journal is a
1cf25 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e 63 72 llocated in incr
1cf26 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 69 ements of.** thi
1cf27 73 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a 2a 2f s many bytes..*/
1cf28 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c .#define JOURNAL
1cf29 5f 43 48 55 4e 4b 53 49 5a 45 20 31 30 32 34 0a _CHUNKSIZE 1024.
1cf2a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e ./* Macro to fin
1cf2b 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 d the minimum of
1cf2c 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c two numeric val
1cf2d 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ues..*/.#ifndef
1cf2e 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e MIN.# define MIN
1cf2f 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f 28 (x,y) ((x)<(y)?(
1cf30 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a x):(y)).#endif..
1cf31 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 /*.** The rollba
1cf32 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63 6f ck journal is co
1cf33 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e 6b mposed of a link
1cf34 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 ed list of these
1cf35 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a structures..*/.
1cf36 73 74 72 75 63 74 20 46 69 6c 65 43 68 75 6e 6b struct FileChunk
1cf37 20 7b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a {. FileChunk *
1cf38 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 pNext;
1cf39 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 75 /* Next chu
1cf3a 6e 6b 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 nk in the journa
1cf3b 6c 20 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e 6b l */. u8 zChunk
1cf3c 5b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 [JOURNAL_CHUNKSI
1cf3d 5a 45 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65 6e ZE]; /* Conten
1cf3e 74 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 20 t of this chunk
1cf3f 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
1cf40 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
1cf41 20 6f 62 6a 65 63 74 20 73 65 72 76 65 73 20 61 object serves a
1cf42 73 20 61 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 s a cursor into
1cf43 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
1cf44 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75 72 rnal..** The cur
1cf45 73 6f 72 20 63 61 6e 20 62 65 20 65 69 74 68 65 sor can be eithe
1cf46 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 r for reading or
1cf47 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74 72 writing..*/.str
1cf48 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b 0a uct FilePoint {.
1cf49 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
1cf4a 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 iOffset;
1cf4b 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d /* Offset from
1cf4c 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
1cf4d 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 f the file */.
1cf4e 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e FileChunk *pChun
1cf4f 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
1cf50 2f 2a 20 53 70 65 63 69 66 69 63 20 63 68 75 6e /* Specific chun
1cf51 6b 20 69 6e 74 6f 20 77 68 69 63 68 20 63 75 72 k into which cur
1cf52 73 6f 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d 3b sor points */.};
1cf53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75 62 ../*.** This sub
1cf54 63 6c 61 73 73 20 69 73 20 61 20 73 75 62 63 6c class is a subcl
1cf55 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 ass of sqlite3_f
1cf56 69 6c 65 2e 20 20 45 61 63 68 20 6f 70 65 6e 20 ile. Each open
1cf57 6d 65 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a 2a memory-journal.*
1cf58 2a 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 * is an instance
1cf59 20 6f 66 20 74 68 69 73 20 63 6c 61 73 73 2e 0a of this class..
1cf5a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 */.struct MemJou
1cf5b 72 6e 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 rnal {. sqlite3
1cf5c 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 _io_methods *pMe
1cf5d 74 68 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72 65 thod; /* Pare
1cf5e 6e 74 20 63 6c 61 73 73 2e 20 4d 55 53 54 20 42 nt class. MUST B
1cf5f 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 46 69 6c E FIRST */. Fil
1cf60 65 43 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b 20 eChunk *pFirst;
1cf61 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cf62 48 65 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 Head of in-memor
1cf63 79 20 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f 0a y chunk-list */.
1cf64 20 20 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64 70 FilePoint endp
1cf65 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 oint;
1cf66 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
1cf67 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 the end of the f
1cf68 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 ile */. FilePoi
1cf69 6e 74 20 72 65 61 64 70 6f 69 6e 74 3b 20 20 20 nt readpoint;
1cf6a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
1cf6b 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f ter to the end o
1cf6c 66 20 74 68 65 20 6c 61 73 74 20 78 52 65 61 64 f the last xRead
1cf6d 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 () */.};../*.**
1cf6e 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 74 Read data from t
1cf6f 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 he file..*/.stat
1cf70 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65 ic int memjrnlRe
1cf71 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 ad(. sqlite3_fi
1cf72 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 le *pJfd, /*
1cf73 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 The journal file
1cf74 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 from which to r
1cf75 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a ead */. void *z
1cf76 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 Buf,
1cf77 2f 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c /* Put the resul
1cf78 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 ts here */. int
1cf79 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 iAmt,
1cf7a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1cf7b 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a bytes to read *
1cf7c 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 /. sqlite_int64
1cf7d 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 iOfst /* Be
1cf7e 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 gin reading at t
1cf7f 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b his offset */.){
1cf80 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 . MemJournal *p
1cf81 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a = (MemJournal *
1cf82 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a 7a 4f 75 )pJfd;. u8 *zOu
1cf83 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 t = zBuf;. int
1cf84 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b 0a 20 20 nRead = iAmt;.
1cf85 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 int iChunkOffset
1cf86 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 ;. FileChunk *p
1cf87 43 68 75 6e 6b 3b 0a 0a 20 20 61 73 73 65 72 74 Chunk;.. assert
1cf88 28 20 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d ( iOfst+iAmt<=p-
1cf89 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 >endpoint.iOffse
1cf8a 74 20 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 t );.. if( p->r
1cf8b 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 eadpoint.iOffset
1cf8c 21 3d 69 4f 66 73 74 20 7c 7c 20 69 4f 66 73 74 !=iOfst || iOfst
1cf8d 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
1cf8e 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 20 3d 20 e3_int64 iOff =
1cf8f 30 3b 0a 20 20 20 20 66 6f 72 28 70 43 68 75 6e 0;. for(pChun
1cf90 6b 3d 70 2d 3e 70 46 69 72 73 74 3b 20 0a 20 20 k=p->pFirst; .
1cf91 20 20 20 20 20 20 70 43 68 75 6e 6b 20 26 26 20 pChunk &&
1cf92 28 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 (iOff+JOURNAL_CH
1cf93 55 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b UNKSIZE)<=iOfst;
1cf94 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d . pChunk=
1cf95 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 pChunk->pNext.
1cf96 20 20 29 7b 0a 20 20 20 20 20 20 69 4f 66 66 20 ){. iOff
1cf97 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b += JOURNAL_CHUNK
1cf98 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 SIZE;. }. }e
1cf99 6c 73 65 7b 0a 20 20 20 20 70 43 68 75 6e 6b 20 lse{. pChunk
1cf9a 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 = p->readpoint.p
1cf9b 43 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 Chunk;. }.. iC
1cf9c 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e hunkOffset = (in
1cf9d 74 29 28 69 4f 66 73 74 25 4a 4f 55 52 4e 41 4c t)(iOfst%JOURNAL
1cf9e 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 _CHUNKSIZE);. d
1cf9f 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69 53 70 61 o {. int iSpa
1cfa0 63 65 20 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 ce = JOURNAL_CHU
1cfa1 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f NKSIZE - iChunkO
1cfa2 66 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 6e ffset;. int n
1cfa3 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 52 65 61 64 Copy = MIN(nRead
1cfa4 2c 20 28 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b , (JOURNAL_CHUNK
1cfa5 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 SIZE - iChunkOff
1cfa6 73 65 74 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70 set));. memcp
1cfa7 79 28 7a 4f 75 74 2c 20 26 70 43 68 75 6e 6b 2d y(zOut, &pChunk-
1cfa8 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 >zChunk[iChunkOf
1cfa9 66 73 65 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 fset], nCopy);.
1cfaa 20 20 20 7a 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 zOut += nCopy
1cfab 3b 0a 20 20 20 20 6e 52 65 61 64 20 2d 3d 20 69 ;. nRead -= i
1cfac 53 70 61 63 65 3b 0a 20 20 20 20 69 43 68 75 6e Space;. iChun
1cfad 6b 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 7d kOffset = 0;. }
1cfae 20 77 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30 while( nRead>=0
1cfaf 20 26 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75 && (pChunk=pChu
1cfb0 6e 6b 2d 3e 70 4e 65 78 74 29 21 3d 30 20 26 26 nk->pNext)!=0 &&
1cfb1 20 6e 52 65 61 64 3e 30 20 29 3b 0a 20 20 70 2d nRead>0 );. p-
1cfb2 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73 >readpoint.iOffs
1cfb3 65 74 20 3d 20 69 4f 66 73 74 2b 69 41 6d 74 3b et = iOfst+iAmt;
1cfb4 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e . p->readpoint.
1cfb5 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b pChunk = pChunk;
1cfb6 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
1cfb7 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 E_OK;.}../*.** W
1cfb8 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 rite data to the
1cfb9 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1cfba 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 int memjrnlWrit
1cfbb 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c e(. sqlite3_fil
1cfbc 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 e *pJfd, /* T
1cfbd 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1cfbe 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 into which to wr
1cfbf 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 ite */. const v
1cfc0 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 oid *zBuf,
1cfc1 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20 /* Take data to
1cfc2 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20 be written from
1cfc3 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 here */. int iA
1cfc4 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
1cfc5 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1cfc6 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a tes to write */.
1cfc7 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 sqlite_int64 i
1cfc8 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 Ofst /* Begi
1cfc9 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69 n writing at thi
1cfca 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 s offset into th
1cfcb 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d e file */.){. M
1cfcc 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 emJournal *p = (
1cfcd 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 MemJournal *)pJf
1cfce 64 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20 d;. int nWrite
1cfcf 3d 20 69 41 6d 74 3b 0a 20 20 75 38 20 2a 7a 57 = iAmt;. u8 *zW
1cfd0 72 69 74 65 20 3d 20 28 75 38 20 2a 29 7a 42 75 rite = (u8 *)zBu
1cfd1 66 3b 0a 0a 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d f;.. /* An in-m
1cfd2 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 emory journal fi
1cfd3 6c 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 65 le should only e
1cfd4 76 65 72 20 62 65 20 61 70 70 65 6e 64 65 64 20 ver be appended
1cfd5 74 6f 2e 20 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20 to. Random. **
1cfd6 61 63 63 65 73 73 20 77 72 69 74 65 73 20 61 72 access writes ar
1cfd7 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 62 e not required b
1cfd8 79 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 y sqlite.. */.
1cfd9 20 61 73 73 65 72 74 28 69 4f 66 73 74 3d 3d 70 assert(iOfst==p
1cfda 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 ->endpoint.iOffs
1cfdb 65 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 et);. UNUSED_PA
1cfdc 52 41 4d 45 54 45 52 28 69 4f 66 73 74 29 3b 0a RAMETER(iOfst);.
1cfdd 0a 20 20 77 68 69 6c 65 28 20 6e 57 72 69 74 65 . while( nWrite
1cfde 3e 30 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 >0 ){. FileCh
1cfdf 75 6e 6b 20 2a 70 43 68 75 6e 6b 20 3d 20 70 2d unk *pChunk = p-
1cfe0 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b >endpoint.pChunk
1cfe1 3b 0a 20 20 20 20 69 6e 74 20 69 43 68 75 6e 6b ;. int iChunk
1cfe2 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28 70 Offset = (int)(p
1cfe3 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 ->endpoint.iOffs
1cfe4 65 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b et%JOURNAL_CHUNK
1cfe5 53 49 5a 45 29 3b 0a 20 20 20 20 69 6e 74 20 69 SIZE);. int i
1cfe6 53 70 61 63 65 20 3d 20 4d 49 4e 28 6e 57 72 69 Space = MIN(nWri
1cfe7 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e te, JOURNAL_CHUN
1cfe8 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 KSIZE - iChunkOf
1cfe9 66 73 65 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 fset);.. if(
1cfea 69 43 68 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 20 iChunkOffset==0
1cfeb 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 ){. /* New
1cfec 63 68 75 6e 6b 20 69 73 20 72 65 71 75 69 72 65 chunk is require
1cfed 64 20 74 6f 20 65 78 74 65 6e 64 20 74 68 65 20 d to extend the
1cfee 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 file. */. F
1cfef 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 77 20 3d ileChunk *pNew =
1cff0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
1cff1 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e 6b sizeof(FileChunk
1cff2 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 ));. if( !p
1cff3 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 New ){. r
1cff4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
1cff5 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 RR_NOMEM;.
1cff6 7d 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e }. pNew->pN
1cff7 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 ext = 0;. i
1cff8 66 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 f( pChunk ){.
1cff9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
1cffa 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 pFirst );.
1cffb 20 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 20 pChunk->pNext
1cffc 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 = pNew;. }e
1cffd 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 lse{. ass
1cffe 65 72 74 28 20 21 70 2d 3e 70 46 69 72 73 74 20 ert( !p->pFirst
1cfff 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46 );. p->pF
1d000 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 irst = pNew;.
1d001 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 65 6e }. p->en
1d002 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 dpoint.pChunk =
1d003 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 pNew;. }..
1d004 20 6d 65 6d 63 70 79 28 26 70 2d 3e 65 6e 64 70 memcpy(&p->endp
1d005 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 68 oint.pChunk->zCh
1d006 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 unk[iChunkOffset
1d007 5d 2c 20 7a 57 72 69 74 65 2c 20 69 53 70 61 63 ], zWrite, iSpac
1d008 65 29 3b 0a 20 20 20 20 7a 57 72 69 74 65 20 2b e);. zWrite +
1d009 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20 6e 57 = iSpace;. nW
1d00a 72 69 74 65 20 2d 3d 20 69 53 70 61 63 65 3b 0a rite -= iSpace;.
1d00b 20 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e p->endpoint.
1d00c 69 4f 66 66 73 65 74 20 2b 3d 20 69 53 70 61 63 iOffset += iSpac
1d00d 65 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e e;. }.. return
1d00e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1d00f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 *.** Truncate th
1d010 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 e file..*/.stati
1d011 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72 75 c int memjrnlTru
1d012 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 ncate(sqlite3_fi
1d013 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 le *pJfd, sqlite
1d014 5f 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 _int64 size){.
1d015 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 MemJournal *p =
1d016 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a (MemJournal *)pJ
1d017 66 64 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 fd;. FileChunk
1d018 2a 70 43 68 75 6e 6b 3b 0a 20 20 61 73 73 65 72 *pChunk;. asser
1d019 74 28 73 69 7a 65 3d 3d 30 29 3b 0a 20 20 55 4e t(size==0);. UN
1d01a 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73 USED_PARAMETER(s
1d01b 69 7a 65 29 3b 0a 20 20 70 43 68 75 6e 6b 20 3d ize);. pChunk =
1d01c 20 70 2d 3e 70 46 69 72 73 74 3b 0a 20 20 77 68 p->pFirst;. wh
1d01d 69 6c 65 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 ile( pChunk ){.
1d01e 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 54 FileChunk *pT
1d01f 6d 70 20 3d 20 70 43 68 75 6e 6b 3b 0a 20 20 20 mp = pChunk;.
1d020 20 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b pChunk = pChunk
1d021 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c ->pNext;. sql
1d022 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70 29 3b ite3_free(pTmp);
1d023 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d 65 . }. sqlite3Me
1d024 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 4a 66 mJournalOpen(pJf
1d025 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c d);. return SQL
1d026 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
1d027 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e Close the file.
1d028 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
1d029 65 6d 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 emjrnlClose(sqli
1d02a 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b te3_file *pJfd){
1d02b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 . memjrnlTrunca
1d02c 74 65 28 70 4a 66 64 2c 20 30 29 3b 0a 20 20 72 te(pJfd, 0);. r
1d02d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1d02e 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 .}.../*.** Sync
1d02f 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1d030 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 53 tic int memjrnlS
1d031 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ync(sqlite3_file
1d032 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e *NotUsed, int N
1d033 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 otUsed2){. UNUS
1d034 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f ED_PARAMETER2(No
1d035 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 tUsed, NotUsed2)
1d036 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1d037 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 E_OK;.}../*.** Q
1d038 75 65 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66 uery the size of
1d039 20 74 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74 the file in byt
1d03a 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
1d03b 74 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a t memjrnlFileSiz
1d03c 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
1d03d 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 pJfd, sqlite_int
1d03e 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 4d 65 64 *pSize){. Me
1d03f 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d mJournal *p = (M
1d040 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 emJournal *)pJfd
1d041 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 ;. *pSize = (sq
1d042 6c 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 65 lite_int64) p->e
1d043 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 3b ndpoint.iOffset;
1d044 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1d045 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 _OK;.}../*.** Ta
1d046 62 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 ble of methods f
1d047 6f 72 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 or MemJournal sq
1d048 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 lite3_file objec
1d049 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 t..*/.static str
1d04a 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d uct sqlite3_io_m
1d04b 65 74 68 6f 64 73 20 4d 65 6d 4a 6f 75 72 6e 61 ethods MemJourna
1d04c 6c 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 lMethods = {. 1
1d04d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1d04e 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
1d04f 20 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 2c 20 memjrnlClose,
1d050 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f /* xClose */
1d051 0a 20 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 2c 20 . memjrnlRead,
1d052 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f /* xRead */
1d053 0a 20 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 2c . memjrnlWrite,
1d054 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a /* xWrite *
1d055 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 /. memjrnlTrunc
1d056 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 ate, /* xTrunca
1d057 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 53 te */. memjrnlS
1d058 79 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 ync, /* xSy
1d059 6e 63 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 46 nc */. memjrnlF
1d05a 69 6c 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 ileSize, /* xFi
1d05b 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 leSize */. 0,
1d05c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d05d 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 xLock */. 0,
1d05e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d05f 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c xUnlock */. 0,
1d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d061 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
1d062 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 dLock */. 0,
1d063 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d064 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a xFileControl */.
1d065 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
1d066 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 /* xSectorSi
1d067 7a 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 ze */. 0
1d068 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 /* xDe
1d069 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
1d06a 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a ics */.};../* .*
1d06b 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c * Open a journal
1d06c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 file..*/.SQLITE
1d06d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1d06e 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f lite3MemJournalO
1d06f 70 65 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 pen(sqlite3_file
1d070 20 2a 70 4a 66 64 29 7b 0a 20 20 4d 65 6d 4a 6f *pJfd){. MemJo
1d071 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a urnal *p = (MemJ
1d072 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 ournal *)pJfd;.
1d073 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 memset(p, 0, sq
1d074 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 lite3MemJournalS
1d075 69 7a 65 28 29 29 3b 0a 20 20 70 2d 3e 70 4d 65 ize());. p->pMe
1d076 74 68 6f 64 20 3d 20 26 4d 65 6d 4a 6f 75 72 6e thod = &MemJourn
1d077 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a alMethods;.}../*
1d078 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
1d079 69 66 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 if the file-hand
1d07a 6c 65 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 le passed as an
1d07b 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 argument is .**
1d07c 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 an in-memory jou
1d07d 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f rnal .*/.SQLITE_
1d07e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1d07f 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 te3IsMemJournal(
1d080 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1d081 66 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 4a fd){. return pJ
1d082 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 26 4d fd->pMethods==&M
1d083 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 emJournalMethods
1d084 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 ;.}../* .** Retu
1d085 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
1d086 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 bytes required
1d087 74 6f 20 73 74 6f 72 65 20 61 20 4d 65 6d 4a 6f to store a MemJo
1d088 75 72 6e 61 6c 20 74 68 61 74 20 75 73 65 73 20 urnal that uses
1d089 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 vfs.** pVfs to c
1d08a 72 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c reate the underl
1d08b 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c ying on-disk fil
1d08c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
1d08d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1d08e 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 3MemJournalSize(
1d08f 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 void){. return
1d090 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e 61 sizeof(MemJourna
1d091 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a l);.}../********
1d092 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 ****** End of me
1d093 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a mjournal.c *****
1d094 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d095 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d096 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1d097 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1d098 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a e walker.c *****
1d099 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d09a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d09b 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1d09c 38 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a 2a 8 August 16.**.*
1d09d 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
1d09e 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
1d09f 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
1d0a0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1d0a1 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1d0a2 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1d0a3 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1d0a4 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1d0a5 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1d0a6 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
1d0a7 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
1d0a8 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
1d0a9 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
1d0aa 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
1d0ab 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
1d0ac 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
1d0ad 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
1d0ae 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1d0af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d0b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1d0b3 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
1d0b4 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 routines used f
1d0b5 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 or walking the p
1d0b6 61 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a 2a arser tree for.*
1d0b7 2a 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 * an SQL stateme
1d0b8 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77 nt..**.** $Id: w
1d0b9 61 6c 6b 65 72 2e 63 2c 76 20 31 2e 31 20 32 30 alker.c,v 1.1 20
1d0ba 30 38 2f 30 38 2f 32 30 20 31 36 3a 33 35 3a 31 08/08/20 16:35:1
1d0bb 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0 drh Exp $.*/..
1d0bc 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 ./*.** Walk an e
1d0bd 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 xpression tree.
1d0be 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c Invoke the call
1d0bf 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 back once for ea
1d0c0 63 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 ch node.** of th
1d0c1 65 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 e expression, wh
1d0c2 69 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 ile decending.
1d0c3 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c (In other words,
1d0c4 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a the callback.**
1d0c5 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f is invoked befo
1d0c6 72 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c re visiting chil
1d0c7 64 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 dren.).**.** The
1d0c8 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 return value fr
1d0c9 6f 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 om the callback
1d0ca 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 should be one of
1d0cb 20 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f the WRC_*.** co
1d0cc 6e 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69 nstants to speci
1d0cd 66 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 fy how to procee
1d0ce 64 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e d with the walk.
1d0cf 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f .**.** WRC_Co
1d0d0 6e 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 ntinue Cont
1d0d1 69 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20 inue descending
1d0d2 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a down the tree..*
1d0d3 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e *.** WRC_Prun
1d0d4 65 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 e Do not
1d0d5 20 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 descend into ch
1d0d6 69 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 ild nodes. But
1d0d7 61 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 allow.**
1d0d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
1d0d9 65 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e e walk to contin
1d0da 75 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20 ue with sibling
1d0db 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 nodes..**.**
1d0dc 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 WRC_Abort
1d0dd 20 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c Do no more cal
1d0de 6c 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 lbacks. Unwind
1d0df 74 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a the stack and.**
1d0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d0e1 20 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65 return the
1d0e2 20 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 top-level walk
1d0e3 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 call..**.** The
1d0e4 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f return value fro
1d0e5 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 m this routine i
1d0e6 73 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 s WRC_Abort to a
1d0e7 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 bandon the tree
1d0e8 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f walk.** and WRC_
1d0e9 43 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 Continue to cont
1d0ea 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f inue..*/.SQLITE_
1d0eb 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1d0ec 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b te3WalkExpr(Walk
1d0ed 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 er *pWalker, Exp
1d0ee 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 r *pExpr){. int
1d0ef 20 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72 rc;. if( pExpr
1d0f0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 ==0 ) return WRC
1d0f1 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 _Continue;. rc
1d0f2 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72 = pWalker->xExpr
1d0f3 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 Callback(pWalker
1d0f4 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 , pExpr);. if(
1d0f5 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65 rc==WRC_Continue
1d0f6 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ){. if( sqli
1d0f7 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1d0f8 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 ker, pExpr->pLef
1d0f9 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f t) ) return WRC_
1d0fa 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73 Abort;. if( s
1d0fb 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 qlite3WalkExpr(p
1d0fc 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 Walker, pExpr->p
1d0fd 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 Right) ) return
1d0fe 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 WRC_Abort;. i
1d0ff 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 f( sqlite3WalkEx
1d100 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 prList(pWalker,
1d101 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 20 29 20 pExpr->pList) )
1d102 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1d103 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1d104 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 3WalkSelect(pWal
1d105 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c ker, pExpr->pSel
1d106 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 ect) ){. re
1d107 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1d108 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1d109 72 6e 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72 rn rc & WRC_Abor
1d10a 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c t;.}../*.** Call
1d10b 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
1d10c 28 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 () for every exp
1d10d 72 65 73 73 69 6f 6e 20 69 6e 20 6c 69 73 74 20 ression in list
1d10e 70 20 6f 72 20 75 6e 74 69 6c 0a 2a 2a 20 61 6e p or until.** an
1d10f 20 61 62 6f 72 74 20 72 65 71 75 65 73 74 20 69 abort request i
1d110 73 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 s seen..*/.SQLIT
1d111 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1d112 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
1d113 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 t(Walker *pWalke
1d114 72 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 29 7b r, ExprList *p){
1d115 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 57 . int i, rc = W
1d116 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 73 RC_Continue;. s
1d117 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
1d118 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 tem *pItem;. if
1d119 28 20 70 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 ( p ){. for(i
1d11a 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d =p->nExpr, pItem
1d11b 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c =p->a; i>0; i--,
1d11c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1d11d 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1d11e 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 49 Expr(pWalker, pI
1d11f 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20 72 65 tem->pExpr) ) re
1d120 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1d121 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1d122 72 6e 20 72 63 20 26 20 57 52 43 5f 43 6f 6e 74 rn rc & WRC_Cont
1d123 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 inue;.}../*.** W
1d124 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69 alk all expressi
1d125 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ons associated w
1d126 69 74 68 20 53 45 4c 45 43 54 20 73 74 61 74 65 ith SELECT state
1d127 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e ment p. Do.** n
1d128 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 ot invoke the SE
1d129 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e LECT callback on
1d12a 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63 p, but do (of c
1d12b 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a ourse) invoke.**
1d12c 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61 any expr callba
1d12d 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63 cks and SELECT c
1d12e 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f allbacks that co
1d12f 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69 me from subqueri
1d130 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 es..** Return WR
1d131 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 C_Abort or WRC_C
1d132 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 ontinue..*/.SQLI
1d133 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1d134 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 qlite3WalkSelect
1d135 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 Expr(Walker *pWa
1d136 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 lker, Select *p)
1d137 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 {. if( sqlite3W
1d138 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c alkExprList(pWal
1d139 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 ker, p->pEList)
1d13a 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1d13b 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 rt;. if( sqlite
1d13c 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 3WalkExpr(pWalke
1d13d 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20 r, p->pWhere) )
1d13e 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1d13f 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 ;. if( sqlite3W
1d140 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c alkExprList(pWal
1d141 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 ker, p->pGroupBy
1d142 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1d143 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 bort;. if( sqli
1d144 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1d145 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 ker, p->pHaving)
1d146 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1d147 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 ort;. if( sqlit
1d148 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 e3WalkExprList(p
1d149 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 Walker, p->pOrde
1d14a 72 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 rBy) ) return WR
1d14b 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 C_Abort;. if( s
1d14c 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 qlite3WalkExpr(p
1d14d 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 Walker, p->pLimi
1d14e 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f t) ) return WRC_
1d14f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c Abort;. if( sql
1d150 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1d151 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 lker, p->pOffset
1d152 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1d153 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 bort;. return W
1d154 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a RC_Continue;.}..
1d155 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 /*.** Walk the p
1d156 61 72 73 65 20 74 72 65 65 73 20 61 73 73 6f 63 arse trees assoc
1d157 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73 iated with all s
1d158 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65 ubqueries in the
1d159 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 .** FROM clause
1d15a 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d of SELECT statem
1d15b 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 ent p. Do not i
1d15c 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74 nvoke the select
1d15d 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 .** callback on
1d15e 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 p, but do invoke
1d15f 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d it on each FROM
1d160 20 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79 clause subquery
1d161 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 .** and on any s
1d162 75 62 71 75 65 72 69 65 73 20 66 75 72 74 68 65 ubqueries furthe
1d163 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 r down in the tr
1d164 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 ee. Return .**
1d165 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 WRC_Abort or WRC
1d166 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 _Continue;.*/.SQ
1d167 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1d168 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
1d169 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 ctFrom(Walker *p
1d16a 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a Walker, Select *
1d16b 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 p){. SrcList *p
1d16c 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 Src;. int i;.
1d16d 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
1d16e 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 tem *pItem;.. p
1d16f 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 Src = p->pSrc;.
1d170 20 69 66 28 20 70 53 72 63 20 29 7b 0a 20 20 20 if( pSrc ){.
1d171 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 for(i=pSrc->nSr
1d172 63 2c 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61 c, pItem=pSrc->a
1d173 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 ; i>0; i--, pIte
1d174 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 m++){. if(
1d175 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
1d176 74 28 70 57 61 6c 6b 65 72 2c 20 70 49 74 65 6d t(pWalker, pItem
1d177 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 ->pSelect) ){.
1d178 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1d179 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a _Abort;. }.
1d17a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1d17b 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b rn WRC_Continue;
1d17c 0a 7d 20 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 .} ../*.** Call
1d17d 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1d17e 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 72 ) for every expr
1d17f 65 73 73 69 6f 6e 20 69 6e 20 53 65 6c 65 63 74 ession in Select
1d180 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 2a 2a statement p..**
1d181 20 49 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 57 Invoke sqlite3W
1d182 61 6c 6b 53 65 6c 65 63 74 28 29 20 66 6f 72 20 alkSelect() for
1d183 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 subqueries in th
1d184 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e e FROM clause an
1d185 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 d.** on the comp
1d186 6f 75 6e 64 20 73 65 6c 65 63 74 20 63 68 61 69 ound select chai
1d187 6e 2c 20 70 2d 3e 70 50 72 69 6f 72 2e 0a 2a 2a n, p->pPrior..**
1d188 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f 43 .** Return WRC_C
1d189 6f 6e 74 69 6e 75 65 20 75 6e 64 65 72 20 6e 6f ontinue under no
1d18a 72 6d 61 6c 20 63 6f 6e 64 69 74 69 6f 6e 73 2e rmal conditions.
1d18b 20 20 52 65 74 75 72 6e 20 57 52 43 5f 41 62 6f Return WRC_Abo
1d18c 72 74 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 69 rt if.** there i
1d18d 73 20 61 6e 20 61 62 6f 72 74 20 72 65 71 75 65 s an abort reque
1d18e 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 st..**.** If the
1d18f 20 57 61 6c 6b 65 72 20 64 6f 65 73 20 6e 6f 74 Walker does not
1d190 20 68 61 76 65 20 61 6e 20 78 53 65 6c 65 63 74 have an xSelect
1d191 43 61 6c 6c 62 61 63 6b 28 29 20 74 68 65 6e 20 Callback() then
1d192 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
1d193 69 73 20 61 20 6e 6f 2d 6f 70 20 72 65 74 75 72 is a no-op retur
1d194 6e 69 6e 67 20 57 52 43 5f 43 6f 6e 74 69 6e 75 ning WRC_Continu
1d195 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1d196 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1d197 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 WalkSelect(Walke
1d198 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 r *pWalker, Sele
1d199 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 ct *p){. int rc
1d19a 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 ;. if( p==0 ||
1d19b 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74 pWalker->xSelect
1d19c 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 65 Callback==0 ) re
1d19d 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
1d19e 65 3b 0a 20 20 72 63 20 3d 20 57 52 43 5f 43 6f e;. rc = WRC_Co
1d19f 6e 74 69 6e 75 65 3b 0a 20 20 77 68 69 6c 65 28 ntinue;. while(
1d1a0 20 70 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 p ){. rc =
1d1a1 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74 pWalker->xSelect
1d1a2 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 Callback(pWalker
1d1a3 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , p);. if( rc
1d1a4 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 ) break;. if
1d1a5 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c ( sqlite3WalkSel
1d1a6 65 63 74 45 78 70 72 28 70 57 61 6c 6b 65 72 2c ectExpr(pWalker,
1d1a7 20 70 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 p) ) return WRC
1d1a8 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 _Abort;. if(
1d1a9 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
1d1aa 74 46 72 6f 6d 28 70 57 61 6c 6b 65 72 2c 20 70 tFrom(pWalker, p
1d1ab 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1d1ac 62 6f 72 74 3b 0a 20 20 20 20 70 20 3d 20 70 2d bort;. p = p-
1d1ad 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 >pPrior;. }. r
1d1ae 65 74 75 72 6e 20 72 63 20 26 20 57 52 43 5f 41 eturn rc & WRC_A
1d1af 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a bort;.}../******
1d1b0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
1d1b1 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a walker.c *******
1d1b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1b4 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1d1b5 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
1d1b6 69 6c 65 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a ile resolve.c **
1d1b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1b9 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
1d1ba 30 30 38 20 41 75 67 75 73 74 20 31 38 0a 2a 2a 008 August 18.**
1d1bb 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1d1bc 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1d1bd 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1d1be 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1d1bf 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1d1c0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1d1c1 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1d1c2 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1d1c3 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1d1c4 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1d1c5 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1d1c6 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1d1c7 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1d1c8 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1d1c9 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1d1ca 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1d1cb 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1d1cc 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1d1cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
1d1d1 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
1d1d2 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 tains routines u
1d1d3 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 sed for walking
1d1d4 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20 the parser tree
1d1d5 61 6e 64 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 61 and.** resolve a
1d1d6 6c 6c 20 69 64 65 6e 74 69 66 69 65 72 73 20 62 ll identifiers b
1d1d7 79 20 61 73 73 6f 63 69 61 74 69 6e 67 20 74 68 y associating th
1d1d8 65 6d 20 77 69 74 68 20 61 20 70 61 72 74 69 63 em with a partic
1d1d9 75 6c 61 72 0a 2a 2a 20 74 61 62 6c 65 20 61 6e ular.** table an
1d1da 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 d column..**.**
1d1db 24 49 64 3a 20 72 65 73 6f 6c 76 65 2e 63 2c 76 $Id: resolve.c,v
1d1dc 20 31 2e 31 35 20 32 30 30 38 2f 31 32 2f 31 30 1.15 2008/12/10
1d1dd 20 31 39 3a 32 36 3a 32 34 20 64 72 68 20 45 78 19:26:24 drh Ex
1d1de 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 p $.*/../*.** Tu
1d1df 72 6e 20 74 68 65 20 70 45 78 70 72 20 65 78 70 rn the pExpr exp
1d1e0 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 6e 20 ression into an
1d1e1 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 69 43 alias for the iC
1d1e2 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 ol-th column of
1d1e3 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 the.** result se
1d1e4 74 20 69 6e 20 70 45 4c 69 73 74 2e 0a 2a 2a 0a t in pEList..**.
1d1e5 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 ** If the result
1d1e6 20 73 65 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 set column is a
1d1e7 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 simple column r
1d1e8 65 66 65 72 65 6e 63 65 2c 20 74 68 65 6e 20 74 eference, then t
1d1e9 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d his routine.** m
1d1ea 61 6b 65 73 20 61 6e 20 65 78 61 63 74 20 63 6f akes an exact co
1d1eb 70 79 2e 20 20 42 75 74 20 66 6f 72 20 61 6e 79 py. But for any
1d1ec 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 65 other kind of e
1d1ed 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 69 73 0a xpression, this.
1d1ee 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20 ** routine make
1d1ef 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 a copy of the re
1d1f0 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 20 sult set column
1d1f1 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 as the argument
1d1f2 74 6f 20 74 68 65 0a 2a 2a 20 54 4b 5f 41 53 20 to the.** TK_AS
1d1f3 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 54 operator. The T
1d1f4 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20 63 61 K_AS operator ca
1d1f5 75 73 65 73 20 74 68 65 20 65 78 70 72 65 73 73 uses the express
1d1f6 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 76 61 ion to be.** eva
1d1f7 6c 75 61 74 65 64 20 6a 75 73 74 20 6f 6e 63 65 luated just once
1d1f8 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 64 and then reused
1d1f9 20 66 6f 72 20 65 61 63 68 20 61 6c 69 61 73 2e for each alias.
1d1fa 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 61 73 6f .**.** The reaso
1d1fb 6e 20 66 6f 72 20 73 75 70 70 72 65 73 73 69 6e n for suppressin
1d1fc 67 20 74 68 65 20 54 4b 5f 41 53 20 74 65 72 6d g the TK_AS term
1d1fd 20 77 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 when the expres
1d1fe 73 69 6f 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 sion is a simple
1d1ff 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 .** column refer
1d200 65 6e 63 65 20 69 73 20 73 6f 20 74 68 61 74 20 ence is so that
1d201 74 68 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 the column refer
1d202 65 6e 63 65 20 77 69 6c 6c 20 62 65 20 72 65 63 ence will be rec
1d203 6f 67 6e 69 7a 65 64 20 61 73 0a 2a 2a 20 75 73 ognized as.** us
1d204 61 62 6c 65 20 62 79 20 69 6e 64 69 63 65 73 20 able by indices
1d205 77 69 74 68 69 6e 20 74 68 65 20 57 48 45 52 45 within the WHERE
1d206 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 clause processi
1d207 6e 67 20 6c 6f 67 69 63 2e 20 0a 2a 2a 0a 2a 2a ng logic. .**.**
1d208 20 48 61 63 6b 3a 20 20 54 68 65 20 54 4b 5f 41 Hack: The TK_A
1d209 53 20 6f 70 65 72 61 74 6f 72 20 69 73 20 69 6e S operator is in
1d20a 68 69 62 69 74 65 64 20 69 66 20 7a 54 79 70 65 hibited if zType
1d20b 5b 30 5d 3d 3d 27 47 27 2e 20 20 54 68 69 73 20 [0]=='G'. This
1d20c 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 6e means.** that in
1d20d 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 a GROUP BY clau
1d20e 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 69 se, the expressi
1d20f 6f 6e 20 69 73 20 65 76 61 6c 75 61 74 65 64 20 on is evaluated
1d210 74 77 69 63 65 2e 20 20 48 65 6e 63 65 3a 0a 2a twice. Hence:.*
1d211 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 *.** SELECT
1d212 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c random()%5 AS x,
1d213 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 count(*) FROM t
1d214 61 62 20 47 52 4f 55 50 20 42 59 20 78 0a 2a 2a ab GROUP BY x.**
1d215 0a 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e .** Is equivalen
1d216 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 t to:.**.**
1d217 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 SELECT random()%
1d218 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 5 AS x, count(*)
1d219 20 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 FROM tab GROUP
1d21a 42 59 20 72 61 6e 64 6f 6d 28 29 25 35 0a 2a 2a BY random()%5.**
1d21b 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f .** The result o
1d21c 66 20 72 61 6e 64 6f 6d 28 29 25 35 20 69 6e 20 f random()%5 in
1d21d 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 the GROUP BY cla
1d21e 75 73 65 20 69 73 20 70 72 6f 62 61 62 6c 79 20 use is probably
1d21f 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f different.** fro
1d220 6d 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 m the result in
1d221 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 2e 20 the result-set.
1d222 20 57 65 20 6d 69 67 68 74 20 66 69 78 20 74 68 We might fix th
1d223 69 73 20 73 6f 6d 65 64 61 79 2e 20 20 4f 72 0a is someday. Or.
1d224 2a 2a 20 74 68 65 6e 20 61 67 61 69 6e 2c 20 77 ** then again, w
1d225 65 20 6d 69 67 68 74 20 6e 6f 74 2e 2e 2e 0a 2a e might not....*
1d226 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 /.static void re
1d227 73 6f 6c 76 65 41 6c 69 61 73 28 0a 20 20 50 61 solveAlias(. Pa
1d228 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1d229 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
1d22a 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 context */. Exp
1d22b 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 rList *pEList,
1d22c 20 20 20 20 2f 2a 20 41 20 72 65 73 75 6c 74 20 /* A result
1d22d 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f set */. int iCo
1d22e 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l,
1d22f 2f 2a 20 41 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 /* A column in t
1d230 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 he result set.
1d231 30 2e 2e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 0..pEList->nExpr
1d232 2d 31 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 -1 */. Expr *pE
1d233 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f xpr, /
1d234 2a 20 54 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 * Transform this
1d235 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 74 into an alias t
1d236 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 o the result set
1d237 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1d238 20 2a 7a 54 79 70 65 20 20 20 20 20 20 2f 2a 20 *zType /*
1d239 22 47 52 4f 55 50 22 20 6f 72 20 22 4f 52 44 45 "GROUP" or "ORDE
1d23a 52 22 20 6f 72 20 22 22 20 2a 2f 0a 29 7b 0a 20 R" or "" */.){.
1d23b 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 20 20 20 Expr *pOrig;
1d23c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 /* The i
1d23d 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 Col-th column of
1d23e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1d23f 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 75 70 3b */. Expr *pDup;
1d240 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1d241 6f 70 79 20 6f 66 20 70 4f 72 69 67 20 2a 2f 0a opy of pOrig */.
1d242 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
1d243 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1d244 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1d245 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ion */.. assert
1d246 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f ( iCol>=0 && iCo
1d247 6c 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 l<pEList->nExpr
1d248 29 3b 0a 20 20 70 4f 72 69 67 20 3d 20 70 45 4c );. pOrig = pEL
1d249 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 ist->a[iCol].pEx
1d24a 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f pr;. assert( pO
1d24b 72 69 67 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 rig!=0 );. asse
1d24c 72 74 28 20 70 4f 72 69 67 2d 3e 66 6c 61 67 73 rt( pOrig->flags
1d24d 20 26 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 29 & EP_Resolved )
1d24e 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d ;. db = pParse-
1d24f 3e 64 62 3b 0a 20 20 70 44 75 70 20 3d 20 73 71 >db;. pDup = sq
1d250 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
1d251 20 70 4f 72 69 67 29 3b 0a 20 20 69 66 28 20 70 pOrig);. if( p
1d252 44 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b Dup==0 ) return;
1d253 0a 20 20 69 66 28 20 70 44 75 70 2d 3e 6f 70 21 . if( pDup->op!
1d254 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54 =TK_COLUMN && zT
1d255 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20 ype[0]!='G' ){.
1d256 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 pDup = sqlite
1d257 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 3PExpr(pParse, T
1d258 4b 5f 41 53 2c 20 70 44 75 70 2c 20 30 2c 20 30 K_AS, pDup, 0, 0
1d259 29 3b 0a 20 20 20 20 69 66 28 20 70 44 75 70 3d );. if( pDup=
1d25a 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 =0 ) return;.
1d25b 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 69 if( pEList->a[i
1d25c 43 6f 6c 5d 2e 69 41 6c 69 61 73 3d 3d 30 20 29 Col].iAlias==0 )
1d25d 7b 0a 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e {. pEList->
1d25e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 20 3d a[iCol].iAlias =
1d25f 20 28 75 31 36 29 28 2b 2b 70 50 61 72 73 65 2d (u16)(++pParse-
1d260 3e 6e 41 6c 69 61 73 29 3b 0a 20 20 20 20 7d 0a >nAlias);. }.
1d261 20 20 20 20 70 44 75 70 2d 3e 69 54 61 62 6c 65 pDup->iTable
1d262 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f = pEList->a[iCo
1d263 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 l].iAlias;. }.
1d264 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 if( pExpr->flag
1d265 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 s & EP_ExpCollat
1d266 65 20 29 7b 0a 20 20 20 20 70 44 75 70 2d 3e 70 e ){. pDup->p
1d267 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 Coll = pExpr->pC
1d268 6f 6c 6c 3b 0a 20 20 20 20 70 44 75 70 2d 3e 66 oll;. pDup->f
1d269 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f lags |= EP_ExpCo
1d26a 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20 20 73 71 6c llate;. }. sql
1d26b 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 ite3ExprClear(db
1d26c 2c 20 70 45 78 70 72 29 3b 0a 20 20 6d 65 6d 63 , pExpr);. memc
1d26d 70 79 28 70 45 78 70 72 2c 20 70 44 75 70 2c 20 py(pExpr, pDup,
1d26e 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 29 29 3b sizeof(*pExpr));
1d26f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1d270 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a 2f (db, pDup);.}../
1d271 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e *.** Given the n
1d272 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 ame of a column
1d273 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 2e 59 2e of the form X.Y.
1d274 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 Z or Y.Z or just
1d275 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 74 Z, look up.** t
1d276 68 61 74 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 hat name in the
1d277 73 65 74 20 6f 66 20 73 6f 75 72 63 65 20 74 61 set of source ta
1d278 62 6c 65 73 20 69 6e 20 70 53 72 63 4c 69 73 74 bles in pSrcList
1d279 20 61 6e 64 20 6d 61 6b 65 20 74 68 65 20 70 45 and make the pE
1d27a 78 70 72 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 xpr .** expressi
1d27b 6f 6e 20 6e 6f 64 65 20 72 65 66 65 72 20 62 61 on node refer ba
1d27c 63 6b 20 74 6f 20 74 68 61 74 20 73 6f 75 72 63 ck to that sourc
1d27d 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 66 e column. The f
1d27e 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 ollowing changes
1d27f 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 .** are made to
1d280 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 pExpr:.**.**
1d281 70 45 78 70 72 2d 3e 69 44 62 20 20 20 20 20 20 pExpr->iDb
1d282 20 20 20 20 20 53 65 74 20 74 68 65 20 69 6e 64 Set the ind
1d283 65 78 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 ex in db->aDb[]
1d284 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1d285 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 X.**
1d286 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 76 (ev
1d287 65 6e 20 69 66 20 58 20 69 73 20 69 6d 70 6c 69 en if X is impli
1d288 65 64 29 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 ed)..** pExpr
1d289 2d 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20 20 ->iTable
1d28a 53 65 74 20 74 6f 20 74 68 65 20 63 75 72 73 6f Set to the curso
1d28b 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 r number for the
1d28c 20 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64 0a table obtained.
1d28d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1d28e 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20 from
1d28f 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 20 pSrcList..**
1d290 70 45 78 70 72 2d 3e 70 54 61 62 20 20 20 20 20 pExpr->pTab
1d291 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 Points to t
1d292 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 he Table structu
1d293 72 65 20 6f 66 20 58 2e 59 20 28 65 76 65 6e 20 re of X.Y (even
1d294 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 if.**
1d295 20 20 20 20 20 20 20 20 20 20 20 20 20 20 58 20 X
1d296 61 6e 64 2f 6f 72 20 59 20 61 72 65 20 69 6d 70 and/or Y are imp
1d297 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 20 70 45 78 lied.).** pEx
1d298 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20 20 20 pr->iColumn
1d299 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 6f 6c Set to the col
1d29a 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 umn number withi
1d29b 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 n the table..**
1d29c 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 pExpr->op
1d29d 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20 54 Set to T
1d29e 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 20 K_COLUMN..**
1d29f 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 20 20 20 pExpr->pLeft
1d2a0 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 Any express
1d2a1 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 ion this points
1d2a2 74 6f 20 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a to is deleted.**
1d2a3 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 pExpr->pRigh
1d2a4 74 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70 t Any exp
1d2a5 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 ression this poi
1d2a6 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65 nts to is delete
1d2a7 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 44 62 d..**.** The pDb
1d2a8 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 6e 61 6d Token is the nam
1d2a9 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1d2aa 65 20 28 74 68 65 20 22 58 22 29 2e 20 20 54 68 e (the "X"). Th
1d2ab 69 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a is value may be.
1d2ac 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 ** NULL meaning
1d2ad 74 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 that name is of
1d2ae 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 the form Y.Z or
1d2af 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c Z. Any availabl
1d2b0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 e database.** ca
1d2b1 6e 20 62 65 20 75 73 65 64 2e 20 20 54 68 65 20 n be used. The
1d2b2 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69 73 20 74 pTableToken is t
1d2b3 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 he name of the t
1d2b4 61 62 6c 65 20 28 74 68 65 20 22 59 22 29 2e 20 able (the "Y").
1d2b5 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65 20 63 This.** value c
1d2b6 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20 70 44 an be NULL if pD
1d2b7 62 54 6f 6b 65 6e 20 69 73 20 61 6c 73 6f 20 4e bToken is also N
1d2b8 55 4c 4c 2e 20 20 49 66 20 70 54 61 62 6c 65 54 ULL. If pTableT
1d2b9 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 69 74 0a oken is NULL it.
1d2ba 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 ** means that th
1d2bb 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 6e 61 e form of the na
1d2bc 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68 61 74 me is Z and that
1d2bd 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61 6e columns from an
1d2be 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e 20 62 y table.** can b
1d2bf 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 e used..**.** If
1d2c0 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e 6f 74 the name cannot
1d2c1 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75 6e 61 be resolved una
1d2c2 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65 61 76 mbiguously, leav
1d2c3 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 e an error messa
1d2c4 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 20 ge.** in pParse
1d2c5 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a and return non-z
1d2c6 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65 72 ero. Return zer
1d2c7 6f 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f o on success..*/
1d2c8 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b .static int look
1d2c9 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 upName(. Parse
1d2ca 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f *pParse, /
1d2cb 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f * The parsing co
1d2cc 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e ntext */. Token
1d2cd 20 2a 70 44 62 54 6f 6b 65 6e 2c 20 20 20 20 20 *pDbToken,
1d2ce 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 /* Name of the d
1d2cf 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 atabase containi
1d2d0 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c ng table, or NUL
1d2d1 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 L */. Token *pT
1d2d2 61 62 6c 65 54 6f 6b 65 6e 2c 20 20 2f 2a 20 4e ableToken, /* N
1d2d3 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e ame of table con
1d2d4 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 taining column,
1d2d5 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b or NULL */. Tok
1d2d6 65 6e 20 2a 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e en *pColumnToken
1d2d7 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 , /* Name of the
1d2d8 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 column. */. Na
1d2d9 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 meContext *pNC,
1d2da 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 /* The name c
1d2db 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20 72 ontext used to r
1d2dc 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 esolve the name
1d2dd 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 */. Expr *pExpr
1d2de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b /* Mak
1d2df 65 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65 e this EXPR node
1d2e0 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65 point to the se
1d2e1 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f lected column */
1d2e2 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 62 20 .){. char *zDb
1d2e3 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 = 0; /* Na
1d2e4 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
1d2e5 73 65 2e 20 20 54 68 65 20 22 58 22 20 69 6e 20 se. The "X" in
1d2e6 58 2e 59 2e 5a 20 2a 2f 0a 20 20 63 68 61 72 20 X.Y.Z */. char
1d2e7 2a 7a 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 *zTab = 0;
1d2e8 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 /* Name of the t
1d2e9 61 62 6c 65 2e 20 20 54 68 65 20 22 59 22 20 69 able. The "Y" i
1d2ea 6e 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 2a n X.Y.Z or Y.Z *
1d2eb 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d /. char *zCol =
1d2ec 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 0; /* Name
1d2ed 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 of the column.
1d2ee 20 54 68 65 20 22 5a 22 20 2a 2f 0a 20 20 69 6e The "Z" */. in
1d2ef 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 t i, j;
1d2f0 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
1d2f1 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 ers */. int cnt
1d2f2 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1d2f3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1d2f4 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 mber of matching
1d2f5 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f column names */
1d2f6 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 20 3d 20 . int cntTab =
1d2f7 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1d2f8 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1d2f9 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 f matching table
1d2fa 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c 69 names */. sqli
1d2fb 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1d2fc 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a ->db; /*
1d2fd 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
1d2fe 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 nnection */. st
1d2ff 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
1d300 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 20 m *pItem;
1d301 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 69 /* Use for loopi
1d302 6e 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74 ng over pSrcList
1d303 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 75 items */. stru
1d304 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
1d305 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f 2a *pMatch = 0; /*
1d306 20 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70 53 The matching pS
1d307 72 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a 20 rcList item */.
1d308 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 54 NameContext *pT
1d309 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 20 opNC = pNC;
1d30a 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d 65 /* First name
1d30b 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 6c context in the l
1d30c 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 ist */. Schema
1d30d 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b 20 20 20 *pSchema = 0;
1d30e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 /* Sc
1d30f 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72 hema of the expr
1d310 65 73 73 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 ession */.. ass
1d311 65 72 74 28 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 ert( pColumnToke
1d312 6e 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 n && pColumnToke
1d313 6e 2d 3e 7a 20 29 3b 20 2f 2a 20 54 68 65 20 5a n->z ); /* The Z
1d314 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74 in X.Y.Z cannot
1d315 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 20 2f be NULL */.. /
1d316 2a 20 44 65 71 75 6f 74 65 20 61 6e 64 20 7a 65 * Dequote and ze
1d317 72 6f 2d 74 65 72 6d 69 6e 61 74 65 20 74 68 65 ro-terminate the
1d318 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 7a 44 62 20 names */. zDb
1d319 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
1d31a 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 62 54 6f mToken(db, pDbTo
1d31b 6b 65 6e 29 3b 0a 20 20 7a 54 61 62 20 3d 20 73 ken);. zTab = s
1d31c 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
1d31d 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 54 6f ken(db, pTableTo
1d31e 6b 65 6e 29 3b 0a 20 20 7a 43 6f 6c 20 3d 20 73 ken);. zCol = s
1d31f 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
1d320 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 54 ken(db, pColumnT
1d321 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 64 62 2d oken);. if( db-
1d322 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
1d323 0a 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70 . goto lookup
1d324 6e 61 6d 65 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 name_end;. }..
1d325 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
1d326 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 he node to no-ma
1d327 74 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e tch */. pExpr->
1d328 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70 iTable = -1;. p
1d329 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a Expr->pTab = 0;.
1d32a 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20 74 . /* Start at t
1d32b 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 6f he inner-most co
1d32c 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 6f ntext and move o
1d32d 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20 6d utward until a m
1d32e 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a 2f atch is found */
1d32f 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26 . while( pNC &&
1d330 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 45 cnt==0 ){. E
1d331 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b xprList *pEList;
1d332 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 . SrcList *pS
1d333 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 rcList = pNC->pS
1d334 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 28 rcList;.. if(
1d335 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 20 pSrcList ){.
1d336 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 for(i=0, pIte
1d337 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 69 m=pSrcList->a; i
1d338 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b <pSrcList->nSrc;
1d339 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i++, pItem++){.
1d33a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 Table *p
1d33b 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 Tab;. int
1d33c 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43 6f iDb;. Co
1d33d 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a 20 lumn *pCol;. .
1d33e 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 49 pTab = pI
1d33f 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 tem->pTab;.
1d340 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 assert( pTab!
1d341 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61 6d =0 && pTab->zNam
1d342 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 e!=0 );.
1d343 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 iDb = sqlite3Sch
1d344 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 emaToIndex(db, p
1d345 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 Tab->pSchema);.
1d346 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1d347 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 Tab->nCol>0 );.
1d348 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 20 if( zTab
1d349 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
1d34a 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 pItem->zAlias )
1d34b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 {. ch
1d34c 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 ar *zTabName = p
1d34d 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 Item->zAlias;.
1d34e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 if( sq
1d34f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61 lite3StrICmp(zTa
1d350 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 bName, zTab)!=0
1d351 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
1d352 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1d353 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a char *z
1d354 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e TabName = pTab->
1d355 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 zName;.
1d356 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65 3d if( zTabName=
1d357 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 =0 || sqlite3Str
1d358 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a ICmp(zTabName, z
1d359 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e Tab)!=0 ) contin
1d35a 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ue;.
1d35b 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71 if( zDb!=0 && sq
1d35c 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d lite3StrICmp(db-
1d35d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c >aDb[iDb].zName,
1d35e 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 zDb)!=0 ){.
1d35f 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e contin
1d360 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ue;.
1d361 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1d362 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1d363 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b if( 0==(cntTab++
1d364 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
1d365 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 Expr->iTable = p
1d366 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 Item->iCursor;.
1d367 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
1d368 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 pTab = pTab;.
1d369 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d pSchema =
1d36a 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a pTab->pSchema;.
1d36b 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 pMatch
1d36c 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 = pItem;.
1d36d 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 }. for(
1d36e 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e j=0, pCol=pTab->
1d36f 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 aCol; j<pTab->nC
1d370 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 ol; j++, pCol++)
1d371 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
1d372 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 sqlite3StrICmp(p
1d373 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c Col->zName, zCol
1d374 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
1d375 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 IdList *pUsi
1d376 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ng;.
1d377 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 cnt++;.
1d378 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1d379 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f = pItem->iCurso
1d37a 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 r;. p
1d37b 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 Expr->pTab = pTa
1d37c 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 b;. p
1d37d 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 Match = pItem;.
1d37e 20 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 pSche
1d37f 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 ma = pTab->pSche
1d380 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ma;.
1d381 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 68 /* Substitute th
1d382 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20 e rowid (column
1d383 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45 -1) for the INTE
1d384 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 GER PRIMARY KEY
1d385 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 */. p
1d386 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 Expr->iColumn =
1d387 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f j==pTab->iPKey ?
1d388 20 2d 31 20 3a 20 6a 3b 0a 20 20 20 20 20 20 20 -1 : j;.
1d389 20 20 20 20 20 69 66 28 20 69 3c 70 53 72 63 4c if( i<pSrcL
1d38a 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 ist->nSrc-1 ){.
1d38b 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 if(
1d38c 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 79 pItem[1].jointy
1d38d 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 pe & JT_NATURAL
1d38e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1d38f 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61 /* If this ma
1d390 74 63 68 20 6f 63 63 75 72 72 65 64 20 69 6e 20 tch occurred in
1d391 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f the left table o
1d392 66 20 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 6e f a natural join
1d393 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1d394 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 74 ** then skip t
1d395 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 74 he right table t
1d396 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63 o avoid a duplic
1d397 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 ate match */.
1d398 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 pIt
1d399 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 em++;.
1d39a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 i++;.
1d39b 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 }else i
1d39c 66 28 20 28 70 55 73 69 6e 67 20 3d 20 70 49 74 f( (pUsing = pIt
1d39d 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d 30 em[1].pUsing)!=0
1d39e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1d39f 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d /* If this m
1d3a0 61 74 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 61 atch occurs on a
1d3a1 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 column that is
1d3a2 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 in the USING cla
1d3a3 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 use.
1d3a4 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 6e ** of a join
1d3a5 2c 20 73 6b 69 70 20 74 68 65 20 73 65 61 72 63 , skip the searc
1d3a6 68 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 h of the right t
1d3a7 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e able of the join
1d3a8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1d3a9 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 64 ** to avoid a d
1d3aa 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 74 uplicate match t
1d3ab 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 here. */.
1d3ac 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a int k;.
1d3ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d3ae 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 6e for(k=0; k<pUsin
1d3af 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 20 g->nId; k++){.
1d3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d3b1 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
1d3b2 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d 2e mp(pUsing->a[k].
1d3b3 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 zName, zCol)==0
1d3b4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1d3b5 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a pItem++;.
1d3b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d3b7 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 i++;.
1d3b8 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 bre
1d3b9 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ak;.
1d3ba 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1d3bb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1d3bc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1d3bd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1d3be 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1d3bf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1d3c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
1d3c1 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1d3c2 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 MIT_TRIGGER.
1d3c3 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6e 6f /* If we have no
1d3c4 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f 6c 76 t already resolv
1d3c5 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 65 ed the name, the
1d3c6 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a 20 n maybe . **
1d3c7 69 74 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f 72 it is a new.* or
1d3c8 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 61 old.* trigger a
1d3c9 72 67 75 6d 65 6e 74 20 72 65 66 65 72 65 6e 63 rgument referenc
1d3ca 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 e. */. if(
1d3cb 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 21 zDb==0 && zTab!
1d3cc 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 20 =0 && cnt==0 &&
1d3cd 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 pParse->trigStac
1d3ce 6b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 54 72 k!=0 ){. Tr
1d3cf 69 67 67 65 72 53 74 61 63 6b 20 2a 70 54 72 69 iggerStack *pTri
1d3d0 67 67 65 72 53 74 61 63 6b 20 3d 20 70 50 61 72 ggerStack = pPar
1d3d1 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a 20 se->trigStack;.
1d3d2 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 Table *pTab
1d3d3 20 3d 20 30 3b 0a 20 20 20 20 20 20 75 33 32 20 = 0;. u32
1d3d4 2a 70 69 43 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a *piColMask = 0;.
1d3d5 20 20 20 20 20 20 69 66 28 20 70 54 72 69 67 67 if( pTrigg
1d3d6 65 72 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20 erStack->newIdx
1d3d7 21 3d 20 2d 31 20 26 26 20 73 71 6c 69 74 65 33 != -1 && sqlite3
1d3d8 53 74 72 49 43 6d 70 28 22 6e 65 77 22 2c 20 7a StrICmp("new", z
1d3d9 54 61 62 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20 Tab) == 0 ){.
1d3da 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 pExpr->iTab
1d3db 6c 65 20 3d 20 70 54 72 69 67 67 65 72 53 74 61 le = pTriggerSta
1d3dc 63 6b 2d 3e 6e 65 77 49 64 78 3b 0a 20 20 20 20 ck->newIdx;.
1d3dd 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72 69 assert( pTri
1d3de 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 20 ggerStack->pTab
1d3df 29 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 );. pTab
1d3e0 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d = pTriggerStack-
1d3e1 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 70 >pTab;. p
1d3e2 69 43 6f 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72 iColMask = &(pTr
1d3e3 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e 65 77 43 iggerStack->newC
1d3e4 6f 6c 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d olMask);. }
1d3e5 65 6c 73 65 20 69 66 28 20 70 54 72 69 67 67 65 else if( pTrigge
1d3e6 72 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78 20 21 rStack->oldIdx !
1d3e7 3d 20 2d 31 20 26 26 20 73 71 6c 69 74 65 33 53 = -1 && sqlite3S
1d3e8 74 72 49 43 6d 70 28 22 6f 6c 64 22 2c 20 7a 54 trICmp("old", zT
1d3e9 61 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ab)==0 ){.
1d3ea 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
1d3eb 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d = pTriggerStack-
1d3ec 3e 6f 6c 64 49 64 78 3b 0a 20 20 20 20 20 20 20 >oldIdx;.
1d3ed 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65 assert( pTrigge
1d3ee 72 53 74 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a rStack->pTab );.
1d3ef 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 pTab = p
1d3f0 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 TriggerStack->pT
1d3f1 61 62 3b 0a 20 20 20 20 20 20 20 20 70 69 43 6f ab;. piCo
1d3f2 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72 69 67 67 lMask = &(pTrigg
1d3f3 65 72 53 74 61 63 6b 2d 3e 6f 6c 64 43 6f 6c 4d erStack->oldColM
1d3f4 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 ask);. }..
1d3f5 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b if( pTab ){
1d3f6 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 . int iC
1d3f7 6f 6c 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 ol;. Colu
1d3f8 6d 6e 20 2a 70 43 6f 6c 20 3d 20 70 54 61 62 2d mn *pCol = pTab-
1d3f9 3e 61 43 6f 6c 3b 0a 0a 20 20 20 20 20 20 20 20 >aCol;..
1d3fa 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e pSchema = pTab->
1d3fb 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 pSchema;.
1d3fc 20 63 6e 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 cntTab++;.
1d3fd 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 for(iCol=0; i
1d3fe 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c Col < pTab->nCol
1d3ff 3b 20 69 43 6f 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b ; iCol++, pCol++
1d400 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ) {. if
1d401 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
1d402 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 (pCol->zName, zC
1d403 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ol)==0 ){.
1d404 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 cnt++;.
1d405 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
1d406 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3d 3d iColumn = iCol==
1d407 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 pTab->iPKey ? -1
1d408 20 3a 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 : iCol;.
1d409 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 pExpr->pTab
1d40a 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20 = pTab;.
1d40b 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 if( iCol>=0
1d40c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1d40d 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c testcase( iCol
1d40e 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20 ==31 );.
1d40f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1d410 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20 iCol==32 );.
1d411 20 20 20 20 20 20 20 20 20 20 2a 70 69 43 6f 6c *piCol
1d412 4d 61 73 6b 20 7c 3d 20 28 28 75 33 32 29 31 3c Mask |= ((u32)1<
1d413 3c 69 43 6f 6c 29 20 7c 20 28 69 43 6f 6c 3e 3d <iCol) | (iCol>=
1d414 33 32 3f 30 78 66 66 66 66 66 66 66 66 3a 30 29 32?0xffffffff:0)
1d415 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
1d416 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
1d417 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 k;. }.
1d418 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1d419 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.#endif /*
1d41a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1d41b 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a _OMIT_TRIGGER) *
1d41c 2f 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a /.. /*. **
1d41d 20 50 65 72 68 61 70 73 20 74 68 65 20 6e 61 6d Perhaps the nam
1d41e 65 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 e is a reference
1d41f 20 74 6f 20 74 68 65 20 52 4f 57 49 44 0a 20 20 to the ROWID.
1d420 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 */. if( cnt
1d421 3d 3d 30 20 26 26 20 63 6e 74 54 61 62 3d 3d 31 ==0 && cntTab==1
1d422 20 26 26 20 73 71 6c 69 74 65 33 49 73 52 6f 77 && sqlite3IsRow
1d423 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 id(zCol) ){.
1d424 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 cnt = 1;.
1d425 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 pExpr->iColumn
1d426 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 78 70 = -1;. pExp
1d427 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 r->affinity = SQ
1d428 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
1d429 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a ;. }.. /*.
1d42a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 69 6e ** If the in
1d42b 70 75 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f put is of the fo
1d42c 72 6d 20 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 rm Z (not Y.Z or
1d42d 20 58 2e 59 2e 5a 29 20 74 68 65 6e 20 74 68 65 X.Y.Z) then the
1d42e 20 6e 61 6d 65 20 5a 0a 20 20 20 20 2a 2a 20 6d name Z. ** m
1d42f 69 67 68 74 20 72 65 66 65 72 20 74 6f 20 61 6e ight refer to an
1d430 20 72 65 73 75 6c 74 2d 73 65 74 20 61 6c 69 61 result-set alia
1d431 73 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 s. This happens
1d432 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 , for example, w
1d433 68 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20 61 72 hen. ** we ar
1d434 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 e resolving name
1d435 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 s in the WHERE c
1d436 6c 61 75 73 65 20 6f 66 20 74 68 65 20 66 6f 6c lause of the fol
1d437 6c 6f 77 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a lowing command:.
1d438 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
1d439 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 SELECT a+b AS
1d43a 78 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 x FROM table WHE
1d43b 52 45 20 78 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a RE x<10;. **.
1d43c 20 20 20 20 2a 2a 20 49 6e 20 63 61 73 65 73 20 ** In cases
1d43d 6c 69 6b 65 20 74 68 69 73 2c 20 72 65 70 6c 61 like this, repla
1d43e 63 65 20 70 45 78 70 72 20 77 69 74 68 20 61 20 ce pExpr with a
1d43f 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70 72 copy of the expr
1d440 65 73 73 69 6f 6e 20 74 68 61 74 0a 20 20 20 20 ession that.
1d441 2a 2a 20 66 6f 72 6d 73 20 74 68 65 20 72 65 73 ** forms the res
1d442 75 6c 74 20 73 65 74 20 65 6e 74 72 79 20 28 22 ult set entry ("
1d443 61 2b 62 22 20 69 6e 20 74 68 65 20 65 78 61 6d a+b" in the exam
1d444 70 6c 65 29 20 61 6e 64 20 72 65 74 75 72 6e 20 ple) and return
1d445 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 immediately..
1d446 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 ** Note that th
1d447 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 e expression in
1d448 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 73 the result set s
1d449 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 hould have alrea
1d44a 64 79 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 72 dy been. ** r
1d44b 65 73 6f 6c 76 65 64 20 62 79 20 74 68 65 20 74 esolved by the t
1d44c 69 6d 65 20 74 68 65 20 57 48 45 52 45 20 63 6c ime the WHERE cl
1d44d 61 75 73 65 20 69 73 20 72 65 73 6f 6c 76 65 64 ause is resolved
1d44e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1d44f 20 63 6e 74 3d 3d 30 20 26 26 20 28 70 45 4c 69 cnt==0 && (pELi
1d450 73 74 20 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74 st = pNC->pEList
1d451 29 21 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 )!=0 && zTab==0
1d452 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 ){. for(j=0
1d453 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 ; j<pEList->nExp
1d454 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 r; j++){.
1d455 20 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c char *zAs = pEL
1d456 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b ist->a[j].zName;
1d457 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 41 73 . if( zAs
1d458 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 !=0 && sqlite3St
1d459 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 rICmp(zAs, zCol)
1d45a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1d45b 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 0a 20 20 Expr *pOrig;.
1d45c 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1d45d 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 pExpr->pLeft==0
1d45e 26 26 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 && pExpr->pRight
1d45f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ==0 );.
1d460 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
1d461 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 pList==0 );.
1d462 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 assert( pE
1d463 78 70 72 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 xpr->pSelect==0
1d464 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 );. pOr
1d465 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a ig = pEList->a[j
1d466 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 ].pExpr;.
1d467 20 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c if( !pNC->all
1d468 6f 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73 owAgg && ExprHas
1d469 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20 Property(pOrig,
1d46a 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 EP_Agg) ){.
1d46b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 sqlite3Er
1d46c 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1d46d 6d 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65 misuse of aliase
1d46e 64 20 61 67 67 72 65 67 61 74 65 20 25 73 22 2c d aggregate %s",
1d46f 20 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20 zAs);.
1d470 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1d471 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 (db, zCol);.
1d472 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 return 2
1d473 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1d474 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 resolveA
1d475 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c lias(pParse, pEL
1d476 69 73 74 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 ist, j, pExpr, "
1d477 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e ");. cn
1d478 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 t = 1;.
1d479 20 70 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 pMatch = 0;.
1d47a 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a assert( z
1d47b 54 61 62 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 Tab==0 && zDb==0
1d47c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f );. go
1d47d 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e to lookupname_en
1d47e 64 5f 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 d_2;. }.
1d47f 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 } . }..
1d480 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f /* Advance to
1d481 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 the next name c
1d482 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f ontext. The loo
1d483 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65 6e p will exit when
1d484 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 either. ** w
1d485 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 20 28 e have a match (
1d486 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 cnt>0) or when w
1d487 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d e run out of nam
1d488 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 e contexts..
1d489 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d */. if( cnt==
1d48a 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 0 ){. pNC =
1d48b 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pNC->pNext;.
1d48c 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a }. }.. /*. *
1d48d 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61 72 65 * If X and Y are
1d48e 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 NULL (in other
1d48f 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 words if only th
1d490 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 e column name Z
1d491 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 is. ** supplied
1d492 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 ) and the value
1d493 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 of Z is enclosed
1d494 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 in double-quote
1d495 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 s, then. ** Z i
1d496 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 s a string liter
1d497 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 al if it doesn't
1d498 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d match any colum
1d499 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 n names. In tha
1d49a 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 t. ** case, we
1d49b 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 72 need to return r
1d49c 69 67 68 74 20 61 77 61 79 20 61 6e 64 20 6e 6f ight away and no
1d49d 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 t make any chang
1d49e 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 es to. ** pExpr
1d49f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 .. **. ** Beca
1d4a0 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 use no reference
1d4a1 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 was made to out
1d4a2 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 er contexts, the
1d4a3 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 pNC->nRef. **
1d4a4 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 fields are not c
1d4a5 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 6f hanged in any co
1d4a6 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 ntext.. */. if
1d4a7 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 ( cnt==0 && zTab
1d4a8 3d 3d 30 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f ==0 && pColumnTo
1d4a9 6b 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 22 27 20 29 ken->z[0]=='"' )
1d4aa 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 {. sqlite3DbF
1d4ab 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 ree(db, zCol);.
1d4ac 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
1d4ad 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 K_STRING;. pE
1d4ae 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 xpr->pTab = 0;.
1d4af 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1d4b0 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 6e 74 3d .. /*. ** cnt=
1d4b1 3d 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 =0 means there w
1d4b2 61 73 20 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63 as not match. c
1d4b3 6e 74 3e 31 20 6d 65 61 6e 73 20 74 68 65 72 65 nt>1 means there
1d4b4 20 77 65 72 65 20 74 77 6f 20 6f 72 0a 20 20 2a were two or. *
1d4b5 2a 20 6d 6f 72 65 20 6d 61 74 63 68 65 73 2e 20 * more matches.
1d4b6 20 45 69 74 68 65 72 20 77 61 79 2c 20 77 65 20 Either way, we
1d4b7 68 61 76 65 20 61 6e 20 65 72 72 6f 72 2e 0a 20 have an error..
1d4b8 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74 21 3d 31 */. if( cnt!=1
1d4b9 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 ){. const ch
1d4ba 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20 7a 45 ar *zErr;. zE
1d4bb 72 72 20 3d 20 63 6e 74 3d 3d 30 20 3f 20 22 6e rr = cnt==0 ? "n
1d4bc 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 22 20 3a o such column" :
1d4bd 20 22 61 6d 62 69 67 75 6f 75 73 20 63 6f 6c 75 "ambiguous colu
1d4be 6d 6e 20 6e 61 6d 65 22 3b 0a 20 20 20 20 69 66 mn name";. if
1d4bf 28 20 7a 44 62 20 29 7b 0a 20 20 20 20 20 20 73 ( zDb ){. s
1d4c0 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1d4c1 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 Parse, "%s: %s.%
1d4c2 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 44 62 s.%s", zErr, zDb
1d4c3 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 , zTab, zCol);.
1d4c4 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 54 61 }else if( zTa
1d4c5 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 b ){. sqlit
1d4c6 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1d4c7 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 e, "%s: %s.%s",
1d4c8 7a 45 72 72 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c zErr, zTab, zCol
1d4c9 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
1d4ca 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1d4cb 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a Msg(pParse, "%s:
1d4cc 20 25 73 22 2c 20 7a 45 72 72 2c 20 7a 43 6f 6c %s", zErr, zCol
1d4cd 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 6f );. }. pTo
1d4ce 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d pNC->nErr++;. }
1d4cf 0a 0a 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 .. /* If a colu
1d4d0 6d 6e 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 mn from a table
1d4d1 69 6e 20 70 53 72 63 4c 69 73 74 20 69 73 20 72 in pSrcList is r
1d4d2 65 66 65 72 65 6e 63 65 64 2c 20 74 68 65 6e 20 eferenced, then
1d4d3 72 65 63 6f 72 64 0a 20 20 2a 2a 20 74 68 69 73 record. ** this
1d4d4 20 66 61 63 74 20 69 6e 20 74 68 65 20 70 53 72 fact in the pSr
1d4d5 63 4c 69 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65 cList.a[].colUse
1d4d6 64 20 62 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75 d bitmask. Colu
1d4d7 6d 6e 20 30 20 63 61 75 73 65 73 0a 20 20 2a 2a mn 0 causes. **
1d4d8 20 62 69 74 20 30 20 74 6f 20 62 65 20 73 65 74 bit 0 to be set
1d4d9 2e 20 20 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73 . Column 1 sets
1d4da 20 62 69 74 20 31 2e 20 20 41 6e 64 20 73 6f 20 bit 1. And so
1d4db 66 6f 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 forth. If the.
1d4dc 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 ** column numbe
1d4dd 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 r is greater tha
1d4de 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1d4df 62 69 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d bits in the bitm
1d4e0 61 73 6b 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 ask. ** then se
1d4e1 74 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65 72 t the high-order
1d4e2 20 62 69 74 20 6f 66 20 74 68 65 20 62 69 74 6d bit of the bitm
1d4e3 61 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ask.. */. if(
1d4e4 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d pExpr->iColumn>=
1d4e5 30 20 26 26 20 70 4d 61 74 63 68 21 3d 30 20 29 0 && pMatch!=0 )
1d4e6 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 {. int n = pE
1d4e7 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 xpr->iColumn;.
1d4e8 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 42 testcase( n==B
1d4e9 4d 53 2d 31 20 29 3b 0a 20 20 20 20 69 66 28 20 MS-1 );. if(
1d4ea 6e 3e 3d 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 n>=BMS ){.
1d4eb 6e 20 3d 20 42 4d 53 2d 31 3b 0a 20 20 20 20 7d n = BMS-1;. }
1d4ec 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 61 . assert( pMa
1d4ed 74 63 68 2d 3e 69 43 75 72 73 6f 72 3d 3d 70 45 tch->iCursor==pE
1d4ee 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 xpr->iTable );.
1d4ef 20 20 20 70 4d 61 74 63 68 2d 3e 63 6f 6c 55 73 pMatch->colUs
1d4f0 65 64 20 7c 3d 20 28 28 42 69 74 6d 61 73 6b 29 ed |= ((Bitmask)
1d4f1 31 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a 6c 6f 6f 6b 1)<<n;. }..look
1d4f2 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20 2f 2a upname_end:. /*
1d4f3 20 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 72 65 Clean up and re
1d4f4 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 turn. */. sqli
1d4f5 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 44 te3DbFree(db, zD
1d4f6 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 b);. sqlite3DbF
1d4f7 72 65 65 28 64 62 2c 20 7a 54 61 62 29 3b 0a 20 ree(db, zTab);.
1d4f8 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
1d4f9 74 65 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c te(db, pExpr->pL
1d4fa 65 66 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 eft);. pExpr->p
1d4fb 4c 65 66 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 Left = 0;. sqli
1d4fc 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1d4fd 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 , pExpr->pRight)
1d4fe 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 ;. pExpr->pRigh
1d4ff 74 20 3d 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e t = 0;. pExpr->
1d500 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a op = TK_COLUMN;.
1d501 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32 lookupname_end_2
1d502 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 :. sqlite3DbFre
1d503 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 69 e(db, zCol);. i
1d504 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 20 f( cnt==1 ){.
1d505 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20 assert( pNC!=0
1d506 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 );. sqlite3Au
1d507 74 68 52 65 61 64 28 70 50 61 72 73 65 2c 20 70 thRead(pParse, p
1d508 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20 70 Expr, pSchema, p
1d509 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a 20 NC->pSrcList);.
1d50a 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 /* Increment
1d50b 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20 6f the nRef value o
1d50c 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65 n all name conte
1d50d 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75 xts from TopNC u
1d50e 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 p to. ** the
1d50f 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65 20 point where the
1d510 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a 2f name matched. */
1d511 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 . for(;;){.
1d512 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 70 assert( pTop
1d513 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 NC!=0 );. p
1d514 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 TopNC->nRef++;.
1d515 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43 3d if( pTopNC=
1d516 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20 20 =pNC ) break;.
1d517 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54 6f pTopNC = pTo
1d518 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 pNC->pNext;.
1d519 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
1d51a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 } else {. r
1d51b 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a eturn 1;. }.}..
1d51c 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
1d51d 6e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20 66 ne is callback f
1d51e 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 or sqlite3WalkEx
1d51f 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f pr()..**.** Reso
1d520 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d lve symbolic nam
1d521 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 4d es into TK_COLUM
1d522 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72 20 N operators for
1d523 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 6e the current.** n
1d524 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70 72 65 ode in the expre
1d525 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 ssion tree. Ret
1d526 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e 75 urn 0 to continu
1d527 65 20 74 68 65 20 73 65 61 72 63 68 20 64 6f 77 e the search dow
1d528 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f 72 n.** the tree or
1d529 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20 2 to abort the
1d52a 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a tree walk..**.**
1d52b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c This routine al
1d52c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63 68 so does error ch
1d52d 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65 20 ecking and name
1d52e 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a 2a resolution for.*
1d52f 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 * function names
1d530 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 . The operator
1d531 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75 for aggregate fu
1d532 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e 67 nctions is chang
1d533 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 5f ed.** to TK_AGG_
1d534 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 FUNCTION..*/.sta
1d535 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 45 tic int resolveE
1d536 78 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20 2a xprStep(Walker *
1d537 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 pWalker, Expr *p
1d538 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e Expr){. NameCon
1d539 74 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61 72 text *pNC;. Par
1d53a 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 70 se *pParse;.. p
1d53b 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e NC = pWalker->u.
1d53c 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 70 pNC;. assert( p
1d53d 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 NC!=0 );. pPars
1d53e 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b e = pNC->pParse;
1d53f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 . assert( pPars
1d540 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 e==pWalker->pPar
1d541 73 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78 70 se );.. if( Exp
1d542 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
1d543 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 pExpr, EP_Resolv
1d544 65 64 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 ed) ) return WRC
1d545 5f 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53 65 _Prune;. ExprSe
1d546 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c tProperty(pExpr,
1d547 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 23 EP_Resolved);.#
1d548 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
1d549 69 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 if( pNC->pSrcLis
1d54a 74 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c 69 t && pNC->pSrcLi
1d54b 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b 0a st->nAlloc>0 ){.
1d54c 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 SrcList *pSr
1d54d 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 cList = pNC->pSr
1d54e 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 cList;. int i
1d54f 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
1d550 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e <pNC->pSrcList->
1d551 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nSrc; i++){.
1d552 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 assert( pSrcLi
1d553 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 st->a[i].iCursor
1d554 3e 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74 2d >=0 && pSrcList-
1d555 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50 >a[i].iCursor<pP
1d556 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 20 arse->nTab);.
1d557 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }. }.#endif.
1d558 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f switch( pExpr->o
1d559 70 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 p ){..#if define
1d55a 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
1d55b 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 UPDATE_DELETE_LI
1d55c 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 MIT) && !defined
1d55d 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 (SQLITE_OMIT_SUB
1d55e 51 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54 68 QUERY). /* Th
1d55f 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74 e special operat
1d560 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 20 or TK_ROW means
1d561 75 73 65 20 74 68 65 20 72 6f 77 69 64 20 66 6f use the rowid fo
1d562 72 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 r the first.
1d563 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 ** column in the
1d564 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 FROM clause. T
1d565 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 his is used by t
1d566 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52 44 he LIMIT and ORD
1d567 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 ER BY. ** cla
1d568 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f use processing o
1d569 6e 20 55 50 44 41 54 45 20 61 6e 64 20 44 45 4c n UPDATE and DEL
1d56a 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a ETE statements..
1d56b 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 */. case
1d56c 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20 20 TK_ROW: {.
1d56d 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 SrcList *pSrcLis
1d56e 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 t = pNC->pSrcLis
1d56f 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 t;. struct
1d570 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 SrcList_item *pI
1d571 74 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 tem;. asser
1d572 74 28 20 70 53 72 63 4c 69 73 74 20 26 26 20 70 t( pSrcList && p
1d573 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 SrcList->nSrc==1
1d574 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 20 );. pItem
1d575 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 0a = pSrcList->a; .
1d576 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 pExpr->op
1d577 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 = TK_COLUMN;.
1d578 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d pExpr->pTab =
1d579 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 pItem->pTab;.
1d57a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
1d57b 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 e = pItem->iCurs
1d57c 6f 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d or;. pExpr-
1d57d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 >iColumn = -1;.
1d57e 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 pExpr->affi
1d57f 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 nity = SQLITE_AF
1d580 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 F_INTEGER;.
1d581 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 break;. }.#e
1d582 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 ndif /* defined(
1d583 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 SQLITE_ENABLE_UP
1d584 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 DATE_DELETE_LIMI
1d585 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 T) && !defined(S
1d586 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
1d587 45 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 ERY) */.. /*
1d588 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69 65 A lone identifie
1d589 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 r is the name of
1d58a 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a a column.. *
1d58b 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44 /. case TK_ID
1d58c 3a 20 7b 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70 : {. lookup
1d58d 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 30 2c 20 Name(pParse, 0,
1d58e 30 2c 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 0, &pExpr->token
1d58f 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 , pNC, pExpr);.
1d590 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
1d591 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 0a Prune;. }. .
1d592 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 6e /* A table n
1d593 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e ame and column n
1d594 61 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 0a 20 ame: ID.ID.
1d595 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 61 62 ** Or a datab
1d596 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 20 63 ase, table and c
1d597 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e 49 44 olumn: ID.ID.ID
1d598 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 . */. case
1d599 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20 20 20 TK_DOT: {.
1d59a 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 3b Token *pColumn;
1d59b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 . Token *pT
1d59c 61 62 6c 65 3b 0a 20 20 20 20 20 20 54 6f 6b 65 able;. Toke
1d59d 6e 20 2a 70 44 62 3b 0a 20 20 20 20 20 20 45 78 n *pDb;. Ex
1d59e 70 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20 20 pr *pRight;..
1d59f 20 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c 69 /* if( pSrcLi
1d5a0 73 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20 2a st==0 ) break; *
1d5a1 2f 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 3d /. pRight =
1d5a2 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a pExpr->pRight;.
1d5a3 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 if( pRight
1d5a4 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 ->op==TK_ID ){.
1d5a5 20 20 20 20 20 20 20 70 44 62 20 3d 20 30 3b 0a pDb = 0;.
1d5a6 20 20 20 20 20 20 20 20 70 54 61 62 6c 65 20 3d pTable =
1d5a7 20 26 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e &pExpr->pLeft->
1d5a8 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 token;. p
1d5a9 43 6f 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 74 Column = &pRight
1d5aa 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d ->token;. }
1d5ab 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 else{. as
1d5ac 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 sert( pRight->op
1d5ad 3d 3d 54 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20 ==TK_DOT );.
1d5ae 20 20 20 20 70 44 62 20 3d 20 26 70 45 78 70 72 pDb = &pExpr
1d5af 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a ->pLeft->token;.
1d5b0 20 20 20 20 20 20 20 20 70 54 61 62 6c 65 20 3d pTable =
1d5b1 20 26 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d &pRight->pLeft-
1d5b2 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 >token;.
1d5b3 70 43 6f 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 pColumn = &pRigh
1d5b4 74 2d 3e 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e t->pRight->token
1d5b5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1d5b6 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 73 lookupName(pPars
1d5b7 65 2c 20 70 44 62 2c 20 70 54 61 62 6c 65 2c 20 e, pDb, pTable,
1d5b8 70 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20 70 45 pColumn, pNC, pE
1d5b9 78 70 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 xpr);. retu
1d5ba 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 rn WRC_Prune;.
1d5bb 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f }.. /* Reso
1d5bc 6c 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d lve function nam
1d5bd 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 es. */. ca
1d5be 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 se TK_CONST_FUNC
1d5bf 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 :. case TK_FU
1d5c0 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 NCTION: {.
1d5c1 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 ExprList *pList
1d5c2 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 20 = pExpr->pList;
1d5c3 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 /* The argume
1d5c4 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 nt list */.
1d5c5 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 20 3f int n = pList ?
1d5c6 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 pList->nExpr :
1d5c7 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 0; /* Number of
1d5c8 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 arguments */.
1d5c9 20 20 20 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f int no_such_
1d5ca 66 75 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 func = 0;
1d5cb 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 20 73 75 /* True if no su
1d5cc 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 ch function exis
1d5cd 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 ts */. int
1d5ce 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d wrong_num_args =
1d5cf 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 0; /* True
1d5d0 69 66 20 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 if wrong number
1d5d1 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a of arguments */.
1d5d2 20 20 20 20 20 20 69 6e 74 20 69 73 5f 61 67 67 int is_agg
1d5d3 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1d5d4 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 /* True if is
1d5d5 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e an aggregate fun
1d5d6 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 ction */. i
1d5d7 6e 74 20 61 75 74 68 3b 20 20 20 20 20 20 20 20 nt auth;
1d5d8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 /* Au
1d5d9 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 thorization to u
1d5da 73 65 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 se the function
1d5db 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 */. int nId
1d5dc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1d5dd 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1d5de 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 f characters in
1d5df 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
1d5e0 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
1d5e1 72 20 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 r *zId;
1d5e2 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 /* The functi
1d5e3 6f 6e 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 on name. */.
1d5e4 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b FuncDef *pDef;
1d5e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d5e6 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f Information abo
1d5e7 75 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 ut the function
1d5e8 2a 2f 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 */. u8 enc
1d5e9 3d 20 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 = ENC(pParse->db
1d5ea 29 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 ); /* The data
1d5eb 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f base encoding */
1d5ec 0a 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28 63 .. zId = (c
1d5ed 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 har*)pExpr->toke
1d5ee 6e 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d n.z;. nId =
1d5ef 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b pExpr->token.n;
1d5f0 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 . pDef = sq
1d5f1 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f lite3FindFunctio
1d5f2 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 n(pParse->db, zI
1d5f3 64 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c 20 d, nId, n, enc,
1d5f4 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 0);. if( pD
1d5f5 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ef==0 ){.
1d5f6 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 pDef = sqlite3F
1d5f7 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 indFunction(pPar
1d5f8 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 se->db, zId, nId
1d5f9 2c 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 , -1, enc, 0);.
1d5fa 20 20 20 20 20 20 20 69 66 28 20 70 44 65 66 3d if( pDef=
1d5fb 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1d5fc 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 31 no_such_func = 1
1d5fd 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
1d5fe 0a 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e 67 . wrong
1d5ff 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a 20 _num_args = 1;.
1d600 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1d601 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 73 else{. is
1d602 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46 75 _agg = pDef->xFu
1d603 6e 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 23 nc==0;. }.#
1d604 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1d605 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e IT_AUTHORIZATION
1d606 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66 20 . if( pDef
1d607 29 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68 20 ){. auth
1d608 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 = sqlite3AuthChe
1d609 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 ck(pParse, SQLIT
1d60a 45 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 70 E_FUNCTION, 0, p
1d60b 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a Def->zName, 0);.
1d60c 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68 if( auth
1d60d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1d60e 20 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 if( aut
1d60f 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 h==SQLITE_DENY )
1d610 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 {. sq
1d611 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1d612 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f arse, "not autho
1d613 72 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75 6e rized to use fun
1d614 63 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 20 ction: %s",.
1d615 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d616 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d617 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pDef->zName);.
1d618 20 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e pNC->n
1d619 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 Err++;.
1d61a 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 }. pEx
1d61b 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c pr->op = TK_NULL
1d61c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
1d61d 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 rn WRC_Prune;.
1d61e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1d61f 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 #endif. if(
1d620 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e 43 2d is_agg && !pNC-
1d621 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 20 20 >allowAgg ){.
1d622 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1d623 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 rMsg(pParse, "mi
1d624 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 suse of aggregat
1d625 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 e function %.*s(
1d626 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 20 )", nId,zId);.
1d627 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b pNC->nErr+
1d628 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67 +;. is_ag
1d629 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c g = 0;. }el
1d62a 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f 66 se if( no_such_f
1d62b 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 unc ){. s
1d62c 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1d62d 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 Parse, "no such
1d62e 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 2c function: %.*s",
1d62f 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 nId, zId);.
1d630 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b pNC->nErr++;
1d631 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
1d632 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 wrong_num_args
1d633 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1d634 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1d635 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 e,"wrong number
1d636 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 of arguments to
1d637 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 function %.*s()"
1d638 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 6e ,. n
1d639 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 Id, zId);.
1d63a 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 pNC->nErr++;.
1d63b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1d63c 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 20 20 is_agg ){.
1d63d 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
1d63e 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b 0a K_AGG_FUNCTION;.
1d63f 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61 73 pNC->has
1d640 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d Agg = 1;. }
1d641 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 . if( is_ag
1d642 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 g ) pNC->allowAg
1d643 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c g = 0;. sql
1d644 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 ite3WalkExprList
1d645 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74 29 (pWalker, pList)
1d646 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 ;. if( is_a
1d647 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 gg ) pNC->allowA
1d648 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a gg = 1;. /*
1d649 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75 74 FIX ME: Comput
1d64a 65 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 e pExpr->affinit
1d64b 79 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 65 y based on the e
1d64c 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 0a 20 xpected return.
1d64d 20 20 20 20 20 2a 2a 20 74 79 70 65 20 6f 66 20 ** type of
1d64e 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20 the function .
1d64f 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 */. ret
1d650 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 urn WRC_Prune;.
1d651 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
1d652 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 ITE_OMIT_SUBQUER
1d653 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 Y. case TK_SE
1d654 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54 LECT:. case T
1d655 4b 5f 45 58 49 53 54 53 3a 0a 23 65 6e 64 69 66 K_EXISTS:.#endif
1d656 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a . case TK_IN:
1d657 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 {. if( pEx
1d658 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 pr->pSelect ){.
1d659 20 20 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20 int nRef
1d65a 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 = pNC->nRef;.#if
1d65b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d65c 5f 43 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69 _CHECK. i
1d65d 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 f( pNC->isCheck
1d65e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
1d65f 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1d660 72 73 65 2c 22 73 75 62 71 75 65 72 69 65 73 20 rse,"subqueries
1d661 70 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 prohibited in CH
1d662 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 ECK constraints"
1d663 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e );. }.#en
1d664 64 69 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69 dif. sqli
1d665 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 te3WalkSelect(pW
1d666 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 53 alker, pExpr->pS
1d667 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 elect);.
1d668 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65 assert( pNC->nRe
1d669 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20 f>=nRef );.
1d66a 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70 4e 43 if( nRef!=pNC
1d66b 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 ->nRef ){.
1d66c 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 ExprSetPrope
1d66d 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61 rty(pExpr, EP_Va
1d66e 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 rSelect);.
1d66f 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1d670 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1d671 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1d672 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 61 73 IT_CHECK. cas
1d673 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b e TK_VARIABLE: {
1d674 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e . if( pNC->
1d675 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 isCheck ){.
1d676 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1d677 73 67 28 70 50 61 72 73 65 2c 22 70 61 72 61 6d sg(pParse,"param
1d678 65 74 65 72 73 20 70 72 6f 68 69 62 69 74 65 64 eters prohibited
1d679 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 in CHECK constr
1d67a 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d aints");. }
1d67b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1d67c 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 }.#endif. }.
1d67d 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d return (pParse-
1d67e 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65 2d >nErr || pParse-
1d67f 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1d680 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a d) ? WRC_Abort :
1d681 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d WRC_Continue;.}
1d682 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 ../*.** pEList i
1d683 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70 72 s a list of expr
1d684 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61 72 essions which ar
1d685 65 20 72 65 61 6c 6c 79 20 74 68 65 20 72 65 73 e really the res
1d686 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a 2a ult set of the.*
1d687 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 * a SELECT state
1d688 6d 65 6e 74 2e 20 20 70 45 20 69 73 20 61 20 74 ment. pE is a t
1d689 65 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 erm in an ORDER
1d68a 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 BY or GROUP BY c
1d68b 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72 lause..** This r
1d68c 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 6f outine checks to
1d68d 20 73 65 65 20 69 66 20 70 45 20 69 73 20 61 20 see if pE is a
1d68e 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65 simple identifie
1d68f 72 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f r which correspo
1d690 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53 nds.** to the AS
1d691 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20 -name of one of
1d692 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
1d693 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
1d694 2e 20 20 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20 . If it is,.**
1d695 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
1d696 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 62 urn an integer b
1d697 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 77 etween 1 and N w
1d698 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 here N is the nu
1d699 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65 mber of.** eleme
1d69a 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20 63 nts in pEList, c
1d69b 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
1d69c 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 the matching ent
1d69d 72 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 ry. If there is
1d69e 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72 .** no match, or
1d69f 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61 20 if pE is not a
1d6a0 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65 simple identifie
1d6a1 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 r, then this rou
1d6a2 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30 tine.** return 0
1d6a3 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68 ..**.** pEList h
1d6a4 61 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 as been resolved
1d6a5 2e 20 20 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a . pE has not..*
1d6a6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 /.static int res
1d6a7 6f 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 50 61 olveAsName(. Pa
1d6a8 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1d6a9 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
1d6aa 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 ext for error me
1d6ab 73 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 ssages */. Expr
1d6ac 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f List *pEList, /
1d6ad 2a 20 4c 69 73 74 20 6f 66 20 65 78 70 72 65 73 * List of expres
1d6ae 73 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f sions to scan */
1d6af 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20 . Expr *pE
1d6b0 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 /* Express
1d6b1 69 6f 6e 20 77 65 20 61 72 65 20 74 72 79 69 6e ion we are tryin
1d6b2 67 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b g to match */.){
1d6b3 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
1d6b4 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
1d6b5 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 unter */.. if(
1d6b6 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 7c 7c pE->op==TK_ID ||
1d6b7 20 28 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 (pE->op==TK_STR
1d6b8 49 4e 47 20 26 26 20 70 45 2d 3e 74 6f 6b 65 6e ING && pE->token
1d6b9 2e 7a 5b 30 5d 21 3d 27 5c 27 27 29 20 29 7b 0a .z[0]!='\'') ){.
1d6ba 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 sqlite3 *db
1d6bb 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
1d6bc 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 73 char *zCol = s
1d6bd 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
1d6be 6b 65 6e 28 64 62 2c 20 26 70 45 2d 3e 74 6f 6b ken(db, &pE->tok
1d6bf 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 43 6f en);. if( zCo
1d6c0 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 l==0 ){. re
1d6c1 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 turn -1;. }.
1d6c2 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 for(i=0; i<pE
1d6c3 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b List->nExpr; i++
1d6c4 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a ){. char *z
1d6c5 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 As = pEList->a[i
1d6c6 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 ].zName;. i
1d6c7 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c f( zAs!=0 && sql
1d6c8 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c ite3StrICmp(zAs,
1d6c9 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 zCol)==0 ){.
1d6ca 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1d6cb 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 ee(db, zCol);.
1d6cc 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 return i+1
1d6cd 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1d6ce 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1d6cf 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d e(db, zCol);. }
1d6d0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
1d6d1 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 70 6f /*.** pE is a po
1d6d2 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72 inter to an expr
1d6d3 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 73 20 ession which is
1d6d4 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e a single term in
1d6d5 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 the.** ORDER BY
1d6d6 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 of a compound S
1d6d7 45 4c 45 43 54 2e 20 20 54 68 65 20 65 78 70 72 ELECT. The expr
1d6d8 65 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62 ession has not b
1d6d9 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f een.** name reso
1d6da 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 lved..**.** At t
1d6db 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f he point this ro
1d6dc 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
1d6dd 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 we already know
1d6de 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44 that the.** ORD
1d6df 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e 6f ER BY term is no
1d6e0 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 64 t an integer ind
1d6e1 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73 75 ex into the resu
1d6e2 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a lt set. That.**
1d6e3 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 case is handled
1d6e4 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 by the calling
1d6e5 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 routine..**.** A
1d6e6 74 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 68 20 ttempt to match
1d6e7 70 45 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c pE against resul
1d6e8 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e t set columns in
1d6e9 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a the left-most.*
1d6ea 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 * SELECT stateme
1d6eb 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 nt. Return the
1d6ec 69 6e 64 65 78 20 69 20 6f 66 20 74 68 65 20 6d index i of the m
1d6ed 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a atching column,.
1d6ee 2a 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 61 74 ** as an indicat
1d6ef 69 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ion to the calle
1d6f0 72 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 r that it should
1d6f1 20 73 6f 72 74 20 62 79 20 74 68 65 20 69 2d 74 sort by the i-t
1d6f2 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65 h column..** The
1d6f3 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d left-most colum
1d6f4 6e 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 68 65 n is 1. In othe
1d6f5 72 20 77 6f 72 64 73 2c 20 74 68 65 20 76 61 6c r words, the val
1d6f6 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 ue returned is t
1d6f7 68 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67 he.** same integ
1d6f8 65 72 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f er value that wo
1d6f9 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 74 uld be used in t
1d6fa 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1d6fb 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 to indicate.**
1d6fc 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a the column..**.*
1d6fd 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f * If there is no
1d6fe 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30 match, return 0
1d6ff 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20 . Return -1 if
1d700 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e an error occurs.
1d701 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
1d702 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72 esolveOrderByTer
1d703 6d 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20 50 mToExprList(. P
1d704 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
1d705 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
1d706 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d text for error m
1d707 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c essages */. Sel
1d708 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 ect *pSelect,
1d709 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 /* The SELECT st
1d70a 61 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 65 atement with the
1d70b 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
1d70c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20 */. Expr *pE
1d70d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1d70e 73 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 42 specific ORDER B
1d70f 59 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69 Y term */.){. i
1d710 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
1d711 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
1d712 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 r */. ExprList
1d713 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65 *pEList; /* The
1d714 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 columns of the
1d715 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 result set */.
1d716 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20 NameContext nc;
1d717 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 /* Name conte
1d718 78 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67 xt for resolving
1d719 20 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 pE */.. assert
1d71a 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 ( sqlite3ExprIsI
1d71b 6e 74 65 67 65 72 28 70 45 2c 20 26 69 29 3d 3d nteger(pE, &i)==
1d71c 30 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 0 );. pEList =
1d71d 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b pSelect->pEList;
1d71e 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61 .. /* Resolve a
1d71f 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 ll names in the
1d720 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 65 78 ORDER BY term ex
1d721 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20 pression. */.
1d722 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20 73 memset(&nc, 0, s
1d723 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63 izeof(nc));. nc
1d724 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 .pParse = pParse
1d725 3b 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 74 20 ;. nc.pSrcList
1d726 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b = pSelect->pSrc;
1d727 0a 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70 . nc.pEList = p
1d728 45 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f EList;. nc.allo
1d729 77 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e wAgg = 1;. nc.n
1d72a 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 Err = 0;. if( s
1d72b 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
1d72c 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 29 20 rNames(&nc, pE)
1d72d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1d72e 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 rorClear(pParse)
1d72f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
1d730 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f }.. /* Try to
1d731 20 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45 52 match the ORDER
1d732 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 61 BY expression a
1d733 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 73 gainst an expres
1d734 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65 sion. ** in the
1d735 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52 65 result set. Re
1d736 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64 20 turn an 1-based
1d737 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74 index of the mat
1d738 63 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c ching. ** resul
1d739 74 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a t-set entry.. *
1d73a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 /. for(i=0; i<p
1d73b 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b EList->nExpr; i+
1d73c 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 +){. if( sqli
1d73d 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 te3ExprCompare(p
1d73e 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 EList->a[i].pExp
1d73f 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20 r, pE) ){.
1d740 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 return i+1;.
1d741 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e }. }.. /* If n
1d742 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 o match, return
1d743 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 0. */. return 0
1d744 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 ;.}../*.** Gener
1d745 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 ate an ORDER BY
1d746 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d or GROUP BY term
1d747 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72 out-of-range er
1d748 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ror..*/.static v
1d749 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 oid resolveOutOf
1d74a 52 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 50 61 RangeError(. Pa
1d74b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1d74c 20 20 20 20 20 2f 2a 20 54 68 65 20 65 72 72 6f /* The erro
1d74d 72 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77 r context into w
1d74e 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 68 hich to write th
1d74f 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e e error */. con
1d750 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 st char *zType,
1d751 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f /* "ORDER" o
1d752 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69 r "GROUP" */. i
1d753 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 nt i,
1d754 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 /* The ind
1d755 65 78 20 28 31 2d 62 61 73 65 64 29 20 6f 66 20 ex (1-based) of
1d756 74 68 65 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 the term out of
1d757 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d range */. int m
1d758 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x
1d759 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 65 72 /* Largest per
1d75a 6d 69 73 73 69 62 6c 65 20 76 61 6c 75 65 20 6f missible value o
1d75b 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 f i */.){. sqli
1d75c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1d75d 73 65 2c 20 0a 20 20 20 20 22 25 72 20 25 73 20 se, . "%r %s
1d75e 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 BY term out of r
1d75f 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 65 ange - should be
1d760 20 22 0a 20 20 20 20 22 62 65 74 77 65 65 6e 20 ". "between
1d761 31 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54 1 and %d", i, zT
1d762 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a ype, mx);.}../*.
1d763 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f ** Analyze the O
1d764 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 RDER BY clause i
1d765 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c n a compound SEL
1d766 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ECT statement.
1d767 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 68 20 Modify.** each
1d768 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 term of the ORDE
1d769 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 R BY clause is a
1d76a 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 constant intege
1d76b 72 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61 r between 1.** a
1d76c 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20 nd N where N is
1d76d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f the number of co
1d76e 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d lumns in the com
1d76f 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a pound SELECT..**
1d770 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 .** ORDER BY ter
1d771 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c 72 65 ms that are alre
1d772 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72 20 62 ady an integer b
1d773 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 61 etween 1 and N a
1d774 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64 re.** unmodified
1d775 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d . ORDER BY term
1d776 73 20 74 68 61 74 20 61 72 65 20 69 6e 74 65 67 s that are integ
1d777 65 72 73 20 6f 75 74 73 69 64 65 20 74 68 65 20 ers outside the
1d778 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68 range of.** 1 th
1d779 72 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 74 65 rough N generate
1d77a 20 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 44 45 an error. ORDE
1d77b 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 R BY terms that
1d77c 61 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a are expressions.
1d77d 2a 2a 20 61 72 65 20 6d 61 74 63 68 65 64 20 61 ** are matched a
1d77e 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 65 gainst result se
1d77f 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 t expressions of
1d780 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
1d781 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 .** beginning wi
1d782 74 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 th the left-most
1d783 20 53 45 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b SELECT and work
1d784 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 72 ing toward the r
1d785 69 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20 ight..** At the
1d786 66 69 72 73 74 20 6d 61 74 63 68 2c 20 74 68 65 first match, the
1d787 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73 ORDER BY expres
1d788 73 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 6f 72 sion is transfor
1d789 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 med into.** the
1d78a 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e integer column n
1d78b 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 umber..**.** Ret
1d78c 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1d78d 66 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a f errors seen..*
1d78e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 /.static int res
1d78f 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 olveCompoundOrde
1d790 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 rBy(. Parse *pP
1d791 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 arse, /*
1d792 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e Parsing context.
1d793 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65 Leave error me
1d794 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 ssages here */.
1d795 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
1d796 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 /* The SE
1d797 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 LECT statement c
1d798 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 ontaining the OR
1d799 44 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69 DER BY */.){. i
1d79a 6e 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 73 74 nt i;. ExprList
1d79b 20 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 45 78 *pOrderBy;. Ex
1d79c 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a prList *pEList;.
1d79d 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 sqlite3 *db;.
1d79e 20 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 int moreToDo =
1d79f 31 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 1;.. pOrderBy =
1d7a0 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 pSelect->pOrder
1d7a1 42 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 72 By;. if( pOrder
1d7a2 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 By==0 ) return 0
1d7a3 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d ;. db = pParse-
1d7a4 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f >db;.#if SQLITE_
1d7a5 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 MAX_COLUMN. if(
1d7a6 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 pOrderBy->nExpr
1d7a7 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
1d7a8 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d TE_LIMIT_COLUMN]
1d7a9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1d7aa 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1d7ab 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 "too many terms
1d7ac 69 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 in ORDER BY clau
1d7ad 73 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e se");. return
1d7ae 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 1;. }.#endif.
1d7af 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 for(i=0; i<pOrd
1d7b0 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b erBy->nExpr; i++
1d7b1 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d ){. pOrderBy-
1d7b2 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a >a[i].done = 0;.
1d7b3 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70 }. pSelect->p
1d7b4 4e 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c Next = 0;. whil
1d7b5 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 e( pSelect->pPri
1d7b6 6f 72 20 29 7b 0a 20 20 20 20 70 53 65 6c 65 63 or ){. pSelec
1d7b7 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74 t->pPrior->pNext
1d7b8 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 = pSelect;.
1d7b9 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 pSelect = pSelec
1d7ba 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 t->pPrior;. }.
1d7bb 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20 while( pSelect
1d7bc 26 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20 && moreToDo ){.
1d7bd 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
1d7be 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
1d7bf 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30 moreToDo = 0
1d7c0 3b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 ;. pEList = p
1d7c1 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a Select->pEList;.
1d7c2 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 assert( pELi
1d7c3 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 st!=0 );. for
1d7c4 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 (i=0, pItem=pOrd
1d7c5 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 erBy->a; i<pOrde
1d7c6 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c rBy->nExpr; i++,
1d7c7 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1d7c8 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a int iCol = -1;.
1d7c9 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 2c 20 Expr *pE,
1d7ca 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28 *pDup;. if(
1d7cb 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63 pItem->done ) c
1d7cc 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 ontinue;. p
1d7cd 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 E = pItem->pExpr
1d7ce 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ;. if( sqli
1d7cf 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 te3ExprIsInteger
1d7d0 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 (pE, &iCol) ){.
1d7d1 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c if( iCol<
1d7d2 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69 73 74 0 || iCol>pEList
1d7d3 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 ->nExpr ){.
1d7d4 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f resolveOutO
1d7d5 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 fRangeError(pPar
1d7d6 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69 2b 31 se, "ORDER", i+1
1d7d7 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 , pEList->nExpr)
1d7d8 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
1d7d9 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a rn 1;. }.
1d7da 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1d7db 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f iCol = reso
1d7dc 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 lveAsName(pParse
1d7dd 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20 , pEList, pE);.
1d7de 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3d if( iCol=
1d7df 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1d7e0 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 pDup = sqlite3Ex
1d7e1 70 72 44 75 70 28 64 62 2c 20 70 45 29 3b 0a 20 prDup(db, pE);.
1d7e2 20 20 20 20 20 20 20 20 20 69 66 28 20 21 64 62 if( !db
1d7e3 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1d7e4 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 {. as
1d7e5 73 65 72 74 28 70 44 75 70 29 3b 0a 20 20 20 20 sert(pDup);.
1d7e6 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 iCol = r
1d7e7 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72 esolveOrderByTer
1d7e8 6d 54 6f 45 78 70 72 4c 69 73 74 28 70 50 61 72 mToExprList(pPar
1d7e9 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 44 75 se, pSelect, pDu
1d7ea 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a p);. }.
1d7eb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1d7ec 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
1d7ed 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 7d pDup);. }
1d7ee 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f . if( iCo
1d7ef 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 l<0 ){.
1d7f0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
1d7f1 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1d7f2 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b if( iCol>0 ){
1d7f3 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 . CollSeq
1d7f4 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43 *pColl = pE->pC
1d7f5 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 oll;. int
1d7f6 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61 flags = pE->fla
1d7f7 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 gs & EP_ExpColla
1d7f8 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 te;. sqli
1d7f9 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1d7fa 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70 , pE);. p
1d7fb 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 Item->pExpr = pE
1d7fc 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 = sqlite3Expr(d
1d7fd 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 b, TK_INTEGER, 0
1d7fe 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 , 0, 0);.
1d7ff 20 69 66 28 20 70 45 3d 3d 30 20 29 20 72 65 74 if( pE==0 ) ret
1d800 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 70 urn 1;. p
1d801 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c E->pColl = pColl
1d802 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 66 6c ;. pE->fl
1d803 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c ags |= EP_IntVal
1d804 75 65 20 7c 20 66 6c 61 67 73 3b 0a 20 20 20 20 ue | flags;.
1d805 20 20 20 20 70 45 2d 3e 69 54 61 62 6c 65 20 3d pE->iTable =
1d806 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 iCol;. p
1d807 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 Item->iCol = (u1
1d808 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 6)iCol;.
1d809 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b pItem->done = 1;
1d80a 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1d80b 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d moreToDo =
1d80c 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
1d80d 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 }. pSelect =
1d80e 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74 3b 0a pSelect->pNext;.
1d80f 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
1d810 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 <pOrderBy->nExpr
1d811 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
1d812 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 pOrderBy->a[i].d
1d813 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 one==0 ){.
1d814 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1d815 70 50 61 72 73 65 2c 20 22 25 72 20 4f 52 44 45 pParse, "%r ORDE
1d816 52 20 42 59 20 74 65 72 6d 20 64 6f 65 73 20 6e R BY term does n
1d817 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 22 0a 20 ot match any ".
1d818 20 20 20 20 20 20 20 20 20 20 20 22 63 6f 6c 75 "colu
1d819 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 mn in the result
1d81a 20 73 65 74 22 2c 20 69 2b 31 29 3b 0a 20 20 20 set", i+1);.
1d81b 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1d81c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1d81d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 0;.}../*.** Chec
1d81e 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20 k every term in
1d81f 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20 the ORDER BY or
1d820 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
1d821 70 4f 72 64 65 72 42 79 20 6f 66 0a 2a 2a 20 74 pOrderBy of.** t
1d822 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
1d823 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20 49 66 ent pSelect. If
1d824 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72 65 66 any term is ref
1d825 65 72 65 6e 63 65 20 74 6f 20 61 0a 2a 2a 20 72 erence to a.** r
1d826 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 esult set expres
1d827 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72 6d 69 sion (as determi
1d828 6e 65 64 20 62 79 20 74 68 65 20 45 78 70 72 4c ned by the ExprL
1d829 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65 6c 64 ist.a.iCol field
1d82a 29 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e 76 65 72 ).** then conver
1d82b 74 20 74 68 61 74 20 74 65 72 6d 20 69 6e 74 6f t that term into
1d82c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 a copy of the c
1d82d 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 65 73 orresponding res
1d82e 75 6c 74 20 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d ult set.** colum
1d82f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 n..**.** If any
1d830 65 72 72 6f 72 73 20 61 72 65 20 64 65 74 65 63 errors are detec
1d831 74 65 64 2c 20 61 64 64 20 61 6e 20 65 72 72 6f ted, add an erro
1d832 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61 r message to pPa
1d833 72 73 65 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 rse and.** retur
1d834 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 n non-zero. Ret
1d835 75 72 6e 20 7a 65 72 6f 20 69 66 20 6e 6f 20 65 urn zero if no e
1d836 72 72 6f 72 73 20 61 72 65 20 73 65 65 6e 2e 0a rrors are seen..
1d837 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1d838 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 E int sqlite3Res
1d839 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 olveOrderGroupBy
1d83a 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1d83b 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 e, /* Par
1d83c 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c sing context. L
1d83d 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61 eave error messa
1d83e 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 ges here */. Se
1d83f 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 lect *pSelect,
1d840 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 /* The SELEC
1d841 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 T statement cont
1d842 61 69 6e 69 6e 67 20 74 68 65 20 63 6c 61 75 73 aining the claus
1d843 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 e */. ExprList
1d844 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 *pOrderBy, /*
1d845 54 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20 The ORDER BY or
1d846 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
1d847 74 6f 20 62 65 20 70 72 6f 63 65 73 73 65 64 20 to be processed
1d848 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1d849 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 22 4f *zType /* "O
1d84a 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 RDER" or "GROUP"
1d84b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a */.){. int i;.
1d84c 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1d84d 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 45 78 pParse->db;. Ex
1d84e 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a prList *pEList;.
1d84f 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
1d850 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a t_item *pItem;..
1d851 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d if( pOrderBy==
1d852 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 0 || pParse->db-
1d853 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 >mallocFailed )
1d854 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51 return 0;.#if SQ
1d855 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a LITE_MAX_COLUMN.
1d856 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e if( pOrderBy->
1d857 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 nExpr>db->aLimit
1d858 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f [SQLITE_LIMIT_CO
1d859 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c LUMN] ){. sql
1d85a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1d85b 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 rse, "too many t
1d85c 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20 63 6c erms in %s BY cl
1d85d 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b 0a 20 ause", zType);.
1d85e 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
1d85f 0a 23 65 6e 64 69 66 0a 20 20 70 45 4c 69 73 74 .#endif. pEList
1d860 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 = pSelect->pELi
1d861 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 st;. assert( pE
1d862 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20 73 List!=0 ); /* s
1d863 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 qlite3SelectNew(
1d864 29 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 69 ) guarantees thi
1d865 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 s */. for(i=0,
1d866 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e pItem=pOrderBy->
1d867 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e a; i<pOrderBy->n
1d868 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
1d869 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 ++){. if( pIt
1d86a 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20 20 20 em->iCol ){.
1d86b 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f if( pItem->iCo
1d86c 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 l>pEList->nExpr
1d86d 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c ){. resol
1d86e 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f veOutOfRangeErro
1d86f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c r(pParse, zType,
1d870 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 i+1, pEList->nE
1d871 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 xpr);. re
1d872 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a turn 1;. }.
1d873 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 resolveAli
1d874 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 as(pParse, pELis
1d875 74 2c 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31 t, pItem->iCol-1
1d876 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 , pItem->pExpr,
1d877 7a 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 zType);. }.
1d878 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
1d879 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20 ./*.** pOrderBy
1d87a 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f is an ORDER BY o
1d87b 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 r GROUP BY claus
1d87c 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61 74 e in SELECT stat
1d87d 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 0a 2a ement pSelect..*
1d87e 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e 74 65 * The Name conte
1d87f 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 xt of the SELECT
1d880 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 4e statement is pN
1d881 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65 69 74 C. zType is eit
1d882 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22 20 6f her.** "ORDER" o
1d883 72 20 22 47 52 4f 55 50 22 20 64 65 70 65 6e 64 r "GROUP" depend
1d884 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74 79 70 ing on which typ
1d885 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f 72 64 e of clause pOrd
1d886 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 erBy is..**.** T
1d887 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 6f his routine reso
1d888 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d 20 6f lves each term o
1d889 66 20 74 68 65 20 63 6c 61 75 73 65 20 69 6e 74 f the clause int
1d88a 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e o an expression.
1d88b 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64 65 72 .** If the order
1d88c 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 69 -by term is an i
1d88d 6e 74 65 67 65 72 20 49 20 62 65 74 77 65 65 6e nteger I between
1d88e 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72 65 20 1 and N (where
1d88f 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 N is the.** numb
1d890 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
1d891 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1d892 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29 20 74 of the SELECT) t
1d893 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 hen the expressi
1d894 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 on.** in the res
1d895 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63 6f 70 olution is a cop
1d896 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20 72 65 y of the I-th re
1d897 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 sult-set express
1d898 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 ion. If.** the
1d899 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73 order-by term is
1d89a 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74 68 61 an identify tha
1d89b 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f t corresponds to
1d89c 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 0a the AS-name of.
1d89d 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65 74 20 ** a result-set
1d89e 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e expression, then
1d89f 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f 6c 76 the term resolv
1d8a0 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 es to a copy of
1d8a1 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65 the.** result-se
1d8a2 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4f t expression. O
1d8a3 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 65 78 therwise, the ex
1d8a4 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 73 6f pression is reso
1d8a5 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 75 lved in.** the u
1d8a6 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69 6e 67 sual way - using
1d8a7 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
1d8a8 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a xprNames()..**.*
1d8a9 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
1d8aa 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
1d8ab 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 r of errors. If
1d8ac 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 errors occur, t
1d8ad 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 hen.** an approp
1d8ae 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 riate error mess
1d8af 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c 65 66 age might be lef
1d8b0 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20 28 4f t in pParse. (O
1d8b1 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65 78 63 OM errors.** exc
1d8b2 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 epted.).*/.stati
1d8b3 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 c int resolveOrd
1d8b4 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e 61 6d erGroupBy(. Nam
1d8b5 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 eContext *pNC,
1d8b6 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 /* The name c
1d8b7 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53 45 ontext of the SE
1d8b8 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a LECT statement *
1d8b9 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c /. Select *pSel
1d8ba 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 ect, /* The
1d8bb 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1d8bc 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64 65 72 t holding pOrder
1d8bd 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 By */. ExprList
1d8be 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a *pOrderBy, /*
1d8bf 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 An ORDER BY or
1d8c0 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
1d8c1 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a 20 20 to resolve */.
1d8c2 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 const char *zTyp
1d8c3 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 e /* Either
1d8c4 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 "ORDER" or "GROU
1d8c5 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 P", as appropria
1d8c6 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 te */.){. int i
1d8c7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1d8c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
1d8c9 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 p counter */. i
1d8ca 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 nt iCol;
1d8cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d8cc 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a Column number *
1d8cd 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c /. struct ExprL
1d8ce 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
1d8cf 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20 /* A term of
1d8d0 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
1d8d1 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a use */. Parse *
1d8d2 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 pParse;
1d8d3 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 /* Parsi
1d8d4 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
1d8d5 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20 20 20 20 int nResult;
1d8d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d8d7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d * Number of term
1d8d8 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 s in the result
1d8d9 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4f set */.. if( pO
1d8da 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75 rderBy==0 ) retu
1d8db 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75 6c 74 20 rn 0;. nResult
1d8dc 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 = pSelect->pELis
1d8dd 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 50 61 72 t->nExpr;. pPar
1d8de 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 se = pNC->pParse
1d8df 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 ;. for(i=0, pIt
1d8e0 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 em=pOrderBy->a;
1d8e1 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 i<pOrderBy->nExp
1d8e2 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 r; i++, pItem++)
1d8e3 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d {. Expr *pE =
1d8e4 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 pItem->pExpr;.
1d8e5 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 iCol = resolv
1d8e6 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 eAsName(pParse,
1d8e7 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c pSelect->pEList,
1d8e8 20 70 45 29 3b 0a 20 20 20 20 69 66 28 20 69 43 pE);. if( iC
1d8e9 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 65 ol<0 ){. re
1d8ea 74 75 72 6e 20 31 3b 20 20 2f 2a 20 4f 4f 4d 20 turn 1; /* OOM
1d8eb 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 7d 0a 20 error */. }.
1d8ec 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b if( iCol>0 ){
1d8ed 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 . /* If an
1d8ee 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68 20 69 73 AS-name match is
1d8ef 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20 74 68 69 found, mark thi
1d8f0 73 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 75 6d s ORDER BY colum
1d8f1 6e 20 61 73 20 62 65 69 6e 67 0a 20 20 20 20 20 n as being.
1d8f2 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ** a copy of th
1d8f3 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c 74 e iCol-th result
1d8f4 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 -set column. Th
1d8f5 65 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c e subsequent cal
1d8f6 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 71 l to. ** sq
1d8f7 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 lite3ResolveOrde
1d8f8 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c 6c 20 rGroupBy() will
1d8f9 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72 convert the expr
1d8fa 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20 20 20 20 ession to a.
1d8fb 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 ** copy of the
1d8fc 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c 74 2d iCol-th result-
1d8fd 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 set expression.
1d8fe 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e */. pItem->
1d8ff 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c iCol = (u16)iCol
1d900 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 ;. continue
1d901 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1d902 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 sqlite3ExprIsInt
1d903 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 eger(pE, &iCol)
1d904 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ){. /* The
1d905 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 69 73 ORDER BY term is
1d906 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 an integer cons
1d907 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c 20 73 65 tant. Again, se
1d908 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 t the column.
1d909 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 73 6f 20 ** number so
1d90a 74 68 61 74 20 73 71 6c 69 74 65 33 52 65 73 6f that sqlite3Reso
1d90b 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 lveOrderGroupBy(
1d90c 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 ) will convert t
1d90d 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64 65 he. ** orde
1d90e 72 2d 62 79 20 74 65 72 6d 20 74 6f 20 61 20 63 r-by term to a c
1d90f 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c opy of the resul
1d910 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t-set expression
1d911 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 43 */. if( iC
1d912 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20 20 20 20 ol<1 ){.
1d913 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 resolveOutOfRang
1d914 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 7a eError(pParse, z
1d915 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52 65 73 75 Type, i+1, nResu
1d916 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 lt);. ret
1d917 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 urn 1;. }.
1d918 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c pItem->iCol
1d919 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 = (u16)iCol;.
1d91a 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 continue;.
1d91b 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 74 68 65 }.. /* Othe
1d91c 72 77 69 73 65 2c 20 74 72 65 61 74 20 74 68 65 rwise, treat the
1d91d 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 61 ORDER BY term a
1d91e 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 65 78 s an ordinary ex
1d91f 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 pression */.
1d920 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 30 3b pItem->iCol = 0;
1d921 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1d922 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
1d923 28 70 4e 43 2c 20 70 45 29 20 29 7b 0a 20 20 20 (pNC, pE) ){.
1d924 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1d925 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1d926 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 sqlite3ResolveOr
1d927 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 derGroupBy(pPars
1d928 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 4f 72 64 e, pSelect, pOrd
1d929 65 72 42 79 2c 20 7a 54 79 70 65 29 3b 0a 7d 0a erBy, zType);.}.
1d92a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6e ./*.** Resolve n
1d92b 61 6d 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45 ames in the SELE
1d92c 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 20 61 CT statement p a
1d92d 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 65 nd all of its de
1d92e 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f 0a 73 74 scendents..*/.st
1d92f 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 atic int resolve
1d930 53 65 6c 65 63 74 53 74 65 70 28 57 61 6c 6b 65 SelectStep(Walke
1d931 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 r *pWalker, Sele
1d932 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d 65 43 6f ct *p){. NameCo
1d933 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 3b ntext *pOuterNC;
1d934 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 74 68 61 /* Context tha
1d935 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 t contains this
1d936 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e 61 6d 65 SELECT */. Name
1d937 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 Context sNC;
1d938 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 /* Name cont
1d939 65 78 74 20 6f 66 20 74 68 69 73 20 53 45 4c 45 ext of this SELE
1d93a 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f CT */. int isCo
1d93b 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20 mpound;
1d93c 2f 2a 20 54 72 75 65 20 69 66 20 70 20 69 73 20 /* True if p is
1d93d 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 a compound selec
1d93e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6d 70 t */. int nComp
1d93f 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f ound; /
1d940 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6d 70 * Number of comp
1d941 6f 75 6e 64 20 74 65 72 6d 73 20 70 72 6f 63 65 ound terms proce
1d942 73 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 ssed so far */.
1d943 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 Parse *pParse;
1d944 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
1d945 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
1d946 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
1d947 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 t; /* Resu
1d948 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f lt set expressio
1d949 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 n list */. int
1d94a 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1d94b 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1d94c 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ter */. ExprLis
1d94d 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 t *pGroupBy;
1d94e 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59 /* The GROUP BY
1d94f 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c clause */. Sel
1d950 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73 74 3b 20 ect *pLeftmost;
1d951 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d 6d 6f 73 /* Left-mos
1d952 74 20 6f 66 20 53 45 4c 45 43 54 20 6f 66 20 61 t of SELECT of a
1d953 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 73 compound */. s
1d954 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
1d955 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
1d956 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
1d957 0a 20 20 0a 0a 20 20 61 73 73 65 72 74 28 20 70 . .. assert( p
1d958 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e !=0 );. if( p->
1d959 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 selFlags & SF_Re
1d95a 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20 20 72 65 solved ){. re
1d95b 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a turn WRC_Prune;.
1d95c 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e 43 20 3d }. pOuterNC =
1d95d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b pWalker->u.pNC;
1d95e 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57 61 6c . pParse = pWal
1d95f 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 64 ker->pParse;. d
1d960 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1d961 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 20 73 . /* Normally s
1d962 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 qlite3SelectExpa
1d963 6e 64 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c nd() will be cal
1d964 6c 65 64 20 66 69 72 73 74 20 61 6e 64 20 77 69 led first and wi
1d965 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 ll have. ** alr
1d966 65 61 64 79 20 65 78 70 61 6e 64 65 64 20 74 68 eady expanded th
1d967 69 73 20 53 45 4c 45 43 54 2e 20 20 48 6f 77 65 is SELECT. Howe
1d968 76 65 72 2c 20 69 66 20 74 68 69 73 20 69 73 20 ver, if this is
1d969 61 20 73 75 62 71 75 65 72 79 20 77 69 74 68 69 a subquery withi
1d96a 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 n. ** an expres
1d96b 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33 52 65 73 sion, sqlite3Res
1d96c 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 olveExprNames()
1d96d 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 77 will be called w
1d96e 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a 20 70 72 ithout a. ** pr
1d96f 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ior call to sqli
1d970 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 te3SelectExpand(
1d971 29 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 61 ). When that ha
1d972 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20 2a 2a 20 ppens, let. **
1d973 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 sqlite3SelectPre
1d974 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66 20 74 68 p() do all of th
1d975 65 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 e processing for
1d976 20 74 68 69 73 20 53 45 4c 45 43 54 2e 0a 20 20 this SELECT..
1d977 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 ** sqlite3Select
1d978 50 72 65 70 28 29 20 77 69 6c 6c 20 69 6e 76 6f Prep() will invo
1d979 6b 65 20 62 6f 74 68 20 73 71 6c 69 74 65 33 53 ke both sqlite3S
1d97a 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 61 6e electExpand() an
1d97b 64 0a 20 20 2a 2a 20 74 68 69 73 20 72 6f 75 74 d. ** this rout
1d97c 69 6e 65 20 69 6e 20 74 68 65 20 63 6f 72 72 65 ine in the corre
1d97d 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 ct order.. */.
1d97e 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 if( (p->selFlag
1d97f 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29 s & SF_Expanded)
1d980 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
1d981 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61 e3SelectPrep(pPa
1d982 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e 43 rse, p, pOuterNC
1d983 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 );. return (p
1d984 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 Parse->nErr || d
1d985 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 b->mallocFailed)
1d986 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 ? WRC_Abort : W
1d987 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 RC_Prune;. }..
1d988 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d 20 70 2d isCompound = p-
1d989 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20 20 6e 43 >pPrior!=0;. nC
1d98a 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 70 ompound = 0;. p
1d98b 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20 Leftmost = p;.
1d98c 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 while( p ){.
1d98d 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 assert( (p->selF
1d98e 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64 lags & SF_Expand
1d98f 65 64 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 ed)!=0 );. as
1d990 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 sert( (p->selFla
1d991 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 gs & SF_Resolved
1d992 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 73 )==0 );. p->s
1d993 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 52 65 elFlags |= SF_Re
1d994 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20 2f 2a 20 solved;.. /*
1d995 52 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72 Resolve the expr
1d996 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 4c essions in the L
1d997 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20 IMIT and OFFSET
1d998 63 6c 61 75 73 65 73 2e 20 54 68 65 73 65 0a 20 clauses. These.
1d999 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 61 6c ** are not al
1d99a 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 lowed to refer t
1d99b 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20 73 6f 20 o any names, so
1d99c 70 61 73 73 20 61 6e 20 65 6d 70 74 79 20 4e 61 pass an empty Na
1d99d 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a meContext.. *
1d99e 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e /. memset(&sN
1d99f 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 C, 0, sizeof(sNC
1d9a0 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 ));. sNC.pPar
1d9a1 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 se = pParse;.
1d9a2 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f if( sqlite3Reso
1d9a3 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e lveExprNames(&sN
1d9a4 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20 7c 7c C, p->pLimit) ||
1d9a5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1d9a6 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
1d9a7 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65 (&sNC, p->pOffse
1d9a8 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 t) ){. retu
1d9a9 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1d9aa 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 53 65 }. . /* Se
1d9ab 74 20 75 70 20 74 68 65 20 6c 6f 63 61 6c 20 6e t up the local n
1d9ac 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20 70 ame-context to p
1d9ad 61 73 73 20 74 6f 20 73 71 6c 69 74 65 33 52 65 ass to sqlite3Re
1d9ae 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 solveExprNames()
1d9af 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c to. ** resol
1d9b0 76 65 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 ve the result-se
1d9b1 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 t expression lis
1d9b2 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e t.. */. sN
1d9b3 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a C.allowAgg = 1;.
1d9b4 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 sNC.pSrcList
1d9b5 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 = p->pSrc;.
1d9b6 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4f 75 74 sNC.pNext = pOut
1d9b7 65 72 4e 43 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 erNC;. . /*
1d9b8 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e Resolve names in
1d9b9 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e the result set.
1d9ba 20 2a 2f 0a 20 20 20 20 70 45 4c 69 73 74 20 3d */. pEList =
1d9bb 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 p->pEList;.
1d9bc 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d assert( pEList!=
1d9bd 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 0 );. for(i=0
1d9be 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 ; i<pEList->nExp
1d9bf 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 r; i++){. E
1d9c0 78 70 72 20 2a 70 58 20 3d 20 70 45 4c 69 73 74 xpr *pX = pEList
1d9c1 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 ->a[i].pExpr;.
1d9c2 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 if( sqlite3R
1d9c3 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 esolveExprNames(
1d9c4 26 73 4e 43 2c 20 70 58 29 20 29 7b 0a 20 20 20 &sNC, pX) ){.
1d9c5 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
1d9c6 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 Abort;. }.
1d9c7 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 }. . /* R
1d9c8 65 63 75 72 73 69 76 65 6c 79 20 72 65 73 6f 6c ecursively resol
1d9c9 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 ve names in all
1d9ca 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 20 2a subqueries. *
1d9cb 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 /. for(i=0; i
1d9cc 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20 <p->pSrc->nSrc;
1d9cd 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75 i++){. stru
1d9ce 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
1d9cf 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72 *pItem = &p->pSr
1d9d0 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 69 c->a[i];. i
1d9d1 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 f( pItem->pSelec
1d9d2 74 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e t ){. con
1d9d3 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65 64 43 st char *zSavedC
1d9d4 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d ontext = pParse-
1d9d5 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 >zAuthContext;.
1d9d6 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d if( pItem
1d9d7 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61 72 73 65 ->zName ) pParse
1d9d8 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d ->zAuthContext =
1d9d9 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 pItem->zName;.
1d9da 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 sqlite3Re
1d9db 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 solveSelectNames
1d9dc 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e (pParse, pItem->
1d9dd 70 53 65 6c 65 63 74 2c 20 70 4f 75 74 65 72 4e pSelect, pOuterN
1d9de 43 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 C);. pPar
1d9df 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 se->zAuthContext
1d9e0 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 = zSavedContext
1d9e1 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 ;. if( pP
1d9e2 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 arse->nErr || db
1d9e3 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1d9e4 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1d9e5 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d t;. }. }
1d9e6 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 . . /* If th
1d9e7 65 72 65 20 61 72 65 20 6e 6f 20 61 67 67 72 65 ere are no aggre
1d9e8 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 gate functions i
1d9e9 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 n the result-set
1d9ea 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55 50 20 42 , and no GROUP B
1d9eb 59 20 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73 Y . ** expres
1d9ec 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20 61 6c 6c sion, do not all
1d9ed 6f 77 20 61 67 67 72 65 67 61 74 65 73 20 69 6e ow aggregates in
1d9ee 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65 any of the othe
1d9ef 72 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20 r expressions..
1d9f0 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
1d9f1 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 ( (p->selFlags &
1d9f2 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 3d 3d SF_Aggregate)==
1d9f3 30 20 29 3b 0a 20 20 20 20 70 47 72 6f 75 70 42 0 );. pGroupB
1d9f4 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b y = p->pGroupBy;
1d9f5 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 . if( pGroupB
1d9f6 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41 67 67 20 y || sNC.hasAgg
1d9f7 29 7b 0a 20 20 20 20 20 20 70 2d 3e 73 65 6c 46 ){. p->selF
1d9f8 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67 67 72 65 lags |= SF_Aggre
1d9f9 67 61 74 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b gate;. }else{
1d9fa 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 . sNC.allow
1d9fb 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 Agg = 0;. }.
1d9fc 20 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 48 41 . /* If a HA
1d9fd 56 49 4e 47 20 63 6c 61 75 73 65 20 69 73 20 70 VING clause is p
1d9fe 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74 68 65 resent, then the
1d9ff 72 65 20 6d 75 73 74 20 62 65 20 61 20 47 52 4f re must be a GRO
1da00 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 20 20 UP BY clause..
1da01 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e */. if( p->
1da02 70 48 61 76 69 6e 67 20 26 26 20 21 70 47 72 6f pHaving && !pGro
1da03 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 73 71 upBy ){. sq
1da04 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1da05 61 72 73 65 2c 20 22 61 20 47 52 4f 55 50 20 42 arse, "a GROUP B
1da06 59 20 63 6c 61 75 73 65 20 69 73 20 72 65 71 75 Y clause is requ
1da07 69 72 65 64 20 62 65 66 6f 72 65 20 48 41 56 49 ired before HAVI
1da08 4e 47 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 NG");. retu
1da09 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1da0a 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 64 }. . /* Ad
1da0b 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e d the expression
1da0c 20 6c 69 73 74 20 74 6f 20 74 68 65 20 6e 61 6d list to the nam
1da0d 65 2d 63 6f 6e 74 65 78 74 20 62 65 66 6f 72 65 e-context before
1da0e 20 70 61 72 73 69 6e 67 20 74 68 65 0a 20 20 20 parsing the.
1da0f 20 2a 2a 20 6f 74 68 65 72 20 65 78 70 72 65 73 ** other expres
1da10 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45 4c sions in the SEL
1da11 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 ECT statement. T
1da12 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 0a 20 his is so that.
1da13 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e ** expression
1da14 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 s in the WHERE c
1da15 6c 61 75 73 65 20 28 65 74 63 2e 29 20 63 61 6e lause (etc.) can
1da16 20 72 65 66 65 72 20 74 6f 20 65 78 70 72 65 73 refer to expres
1da17 73 69 6f 6e 73 20 62 79 0a 20 20 20 20 2a 2a 20 sions by. **
1da18 61 6c 69 61 73 65 73 20 69 6e 20 74 68 65 20 72 aliases in the r
1da19 65 73 75 6c 74 20 73 65 74 2e 0a 20 20 20 20 2a esult set.. *
1da1a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f 72 20 70 *. ** Minor p
1da1b 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 20 69 73 oint: If this is
1da1c 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 the case, then
1da1d 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 77 the expression w
1da1e 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a 20 72 65 ill be. ** re
1da1f 2d 65 76 61 6c 75 61 74 65 64 20 66 6f 72 20 65 -evaluated for e
1da20 61 63 68 20 72 65 66 65 72 65 6e 63 65 20 74 6f ach reference to
1da21 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 it.. */.
1da22 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20 70 2d 3e sNC.pEList = p->
1da23 70 45 4c 69 73 74 3b 0a 20 20 20 20 69 66 28 20 pEList;. if(
1da24 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
1da25 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d prNames(&sNC, p-
1da26 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20 20 20 20 >pWhere) ||.
1da27 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 sqlite3Resolv
1da28 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c eExprNames(&sNC,
1da29 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a 20 20 20 p->pHaving).
1da2a 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1da2b 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
1da2c 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 }.. /* The OR
1da2d 44 45 52 20 42 59 20 61 6e 64 20 47 52 4f 55 50 DER BY and GROUP
1da2e 20 42 59 20 63 6c 61 75 73 65 73 20 6d 61 79 20 BY clauses may
1da2f 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 74 65 72 not refer to ter
1da30 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20 6f 75 74 ms in. ** out
1da31 65 72 20 71 75 65 72 69 65 73 20 0a 20 20 20 20 er queries .
1da32 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 */. sNC.pNext
1da33 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43 2e 61 6c = 0;. sNC.al
1da34 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 0a 20 20 20 lowAgg = 1;..
1da35 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 65 20 /* Process the
1da36 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
1da37 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e 20 53 45 for singleton SE
1da38 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e LECT statements.
1da39 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f 52 44 45 . ** The ORDE
1da3a 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 R BY clause for
1da3b 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c 45 43 54 compounds SELECT
1da3c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 20 68 statements is h
1da3d 61 6e 64 6c 65 64 0a 20 20 20 20 2a 2a 20 62 65 andled. ** be
1da3e 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c 6c 20 6f low, after all o
1da3f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 f the result-set
1da40 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 s for all of the
1da41 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0a 20 20 20 elements of.
1da42 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 ** the compound
1da43 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c have been resol
1da44 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ved.. */.
1da45 69 66 28 20 21 69 73 43 6f 6d 70 6f 75 6e 64 20 if( !isCompound
1da46 26 26 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 && resolveOrderG
1da47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70 2c 20 roupBy(&sNC, p,
1da48 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 p->pOrderBy, "OR
1da49 44 45 52 22 29 20 29 7b 0a 20 20 20 20 20 20 72 DER") ){. r
1da4a 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1da4b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 . }. if( d
1da4c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1da4d 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1da4e 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d WRC_Abort;. }
1da4f 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c . . /* Resol
1da50 76 65 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 ve the GROUP BY
1da51 63 6c 61 75 73 65 2e 20 20 41 74 20 74 68 65 20 clause. At the
1da52 73 61 6d 65 20 74 69 6d 65 2c 20 6d 61 6b 65 20 same time, make
1da53 73 75 72 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 sure . ** the
1da54 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 GROUP BY clause
1da55 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 does not contai
1da56 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 n aggregate func
1da57 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 tions.. */.
1da58 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 if( pGroupBy )
1da59 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 {. struct E
1da5a 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
1da5b 74 65 6d 3b 0a 20 20 20 20 0a 20 20 20 20 20 20 tem;. .
1da5c 69 66 28 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 if( resolveOrder
1da5d 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70 2c GroupBy(&sNC, p,
1da5e 20 70 47 72 6f 75 70 42 79 2c 20 22 47 52 4f 55 pGroupBy, "GROU
1da5f 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f P") || db->mallo
1da60 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 cFailed ){.
1da61 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
1da62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ort;. }.
1da63 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 for(i=0, pIte
1da64 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 3b 20 69 m=pGroupBy->a; i
1da65 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 <pGroupBy->nExpr
1da66 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i++, pItem++){
1da67 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70 . if( Exp
1da68 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 49 74 rHasProperty(pIt
1da69 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50 5f 41 67 em->pExpr, EP_Ag
1da6a 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 g) ){.
1da6b 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1da6c 70 50 61 72 73 65 2c 20 22 61 67 67 72 65 67 61 pParse, "aggrega
1da6d 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 te functions are
1da6e 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 not allowed in
1da6f 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ".
1da70 22 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c "the GROUP BY cl
1da71 61 75 73 65 22 29 3b 0a 20 20 20 20 20 20 20 20 ause");.
1da72 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
1da73 72 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rt;. }.
1da74 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1da75 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 /* Advance to t
1da76 68 65 20 6e 65 78 74 20 74 65 72 6d 20 6f 66 20 he next term of
1da77 74 68 65 20 63 6f 6d 70 6f 75 6e 64 0a 20 20 20 the compound.
1da78 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 */. p = p->p
1da79 50 72 69 6f 72 3b 0a 20 20 20 20 6e 43 6f 6d 70 Prior;. nComp
1da7a 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f ound++;. }.. /
1da7b 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 4f 52 * Resolve the OR
1da7c 44 45 52 20 42 59 20 6f 6e 20 61 20 63 6f 6d 70 DER BY on a comp
1da7d 6f 75 6e 64 20 53 45 4c 45 43 54 20 61 66 74 65 ound SELECT afte
1da7e 72 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 0a 20 r all terms of.
1da7f 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 ** the compound
1da80 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c have been resol
1da81 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ved.. */. if(
1da82 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 isCompound && re
1da83 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 solveCompoundOrd
1da84 65 72 42 79 28 70 50 61 72 73 65 2c 20 70 4c 65 erBy(pParse, pLe
1da85 66 74 6d 6f 73 74 29 20 29 7b 0a 20 20 20 20 72 ftmost) ){. r
1da86 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1da87 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 57 . }.. return W
1da88 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a 0a 2f 2a 0a RC_Prune;.}../*.
1da89 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1da8a 77 61 6c 6b 73 20 61 6e 20 65 78 70 72 65 73 73 walks an express
1da8b 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 72 65 73 ion tree and res
1da8c 6f 6c 76 65 73 20 72 65 66 65 72 65 6e 63 65 73 olves references
1da8d 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 63 6f 6c to.** table col
1da8e 75 6d 6e 73 20 61 6e 64 20 72 65 73 75 6c 74 2d umns and result-
1da8f 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20 20 41 74 set columns. At
1da90 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 the same time,
1da91 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 63 68 65 63 do error.** chec
1da92 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74 69 6f 6e king on function
1da93 20 75 73 61 67 65 20 61 6e 64 20 73 65 74 20 61 usage and set a
1da94 20 66 6c 61 67 20 69 66 20 61 6e 79 20 61 67 67 flag if any agg
1da95 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
1da96 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2a .** are seen..**
1da97 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 20 74 .** To resolve t
1da98 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 72 65 66 able columns ref
1da99 65 72 65 6e 63 65 73 20 77 65 20 6c 6f 6f 6b 20 erences we look
1da9a 66 6f 72 20 6e 6f 64 65 73 20 28 6f 72 20 73 75 for nodes (or su
1da9b 62 74 72 65 65 73 29 20 6f 66 20 74 68 65 20 0a btrees) of the .
1da9c 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 ** form X.Y.Z or
1da9d 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 20 77 Y.Z or just Z w
1da9e 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 here.**.**
1da9f 58 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 X: The name of
1daa0 20 61 20 64 61 74 61 62 61 73 65 2e 20 20 45 78 a database. Ex
1daa1 3a 20 20 22 6d 61 69 6e 22 20 6f 72 20 22 74 65 : "main" or "te
1daa2 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 mp" or.**
1daa3 20 20 20 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 the symbolic
1daa4 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 name assigned t
1daa5 6f 20 61 6e 20 41 54 54 41 43 48 2d 65 64 20 64 o an ATTACH-ed d
1daa6 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 atabase..**.**
1daa7 20 20 20 20 59 3a 20 20 20 54 68 65 20 6e 61 6d Y: The nam
1daa8 65 20 6f 66 20 61 20 74 61 62 6c 65 20 69 6e 20 e of a table in
1daa9 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 a FROM clause.
1daaa 4f 72 20 69 6e 20 61 20 74 72 69 67 67 65 72 0a Or in a trigger.
1daab 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 6e 65 ** one
1daac 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 of the special
1daad 6e 61 6d 65 73 20 22 6f 6c 64 22 20 6f 72 20 22 names "old" or "
1daae 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 new"..**.**
1daaf 20 5a 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f Z: The name o
1dab0 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 f a column in ta
1dab1 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ble Y..**.** The
1dab2 20 6e 6f 64 65 20 61 74 20 74 68 65 20 72 6f 6f node at the roo
1dab3 74 20 6f 66 20 74 68 65 20 73 75 62 74 72 65 65 t of the subtree
1dab4 20 69 73 20 6d 6f 64 69 66 69 65 64 20 61 73 20 is modified as
1dab5 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
1dab6 20 20 45 78 70 72 2e 6f 70 20 20 20 20 20 20 20 Expr.op
1dab7 20 43 68 61 6e 67 65 64 20 74 6f 20 54 4b 5f 43 Changed to TK_C
1dab8 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 45 78 70 72 OLUMN.** Expr
1dab9 2e 70 54 61 62 20 20 20 20 20 20 50 6f 69 6e 74 .pTab Point
1daba 73 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 6f s to the Table o
1dabb 62 6a 65 63 74 20 66 6f 72 20 58 2e 59 0a 2a 2a bject for X.Y.**
1dabc 20 20 20 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e Expr.iColumn
1dabd 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 69 6e The column in
1dabe 64 65 78 20 69 6e 20 58 2e 59 2e 20 20 2d 31 20 dex in X.Y. -1
1dabf 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a for the rowid..*
1dac0 2a 20 20 20 20 45 78 70 72 2e 69 54 61 62 6c 65 * Expr.iTable
1dac1 20 20 20 20 54 68 65 20 56 44 42 45 20 63 75 72 The VDBE cur
1dac2 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 58 sor number for X
1dac3 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 .Y.**.**.** To r
1dac4 65 73 6f 6c 76 65 20 72 65 73 75 6c 74 2d 73 65 esolve result-se
1dac5 74 20 72 65 66 65 72 65 6e 63 65 73 2c 20 6c 6f t references, lo
1dac6 6f 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f ok for expressio
1dac7 6e 20 6e 6f 64 65 73 20 6f 66 20 74 68 65 0a 2a n nodes of the.*
1dac8 2a 20 66 6f 72 6d 20 5a 20 28 77 69 74 68 20 6e * form Z (with n
1dac9 6f 20 58 20 61 6e 64 20 59 20 70 72 65 66 69 78 o X and Y prefix
1daca 29 20 77 68 65 72 65 20 74 68 65 20 5a 20 6d 61 ) where the Z ma
1dacb 74 63 68 65 73 20 74 68 65 20 72 69 67 68 74 2d tches the right-
1dacc 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 hand.** size of
1dacd 61 6e 20 41 53 20 63 6c 61 75 73 65 20 69 6e 20 an AS clause in
1dace 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 6f the result-set o
1dacf 66 20 61 20 53 45 4c 45 43 54 2e 20 20 54 68 65 f a SELECT. The
1dad0 20 5a 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a Z expression.**
1dad1 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 is replaced by
1dad2 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6c 65 a copy of the le
1dad3 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 ft-hand side of
1dad4 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65 the result-set e
1dad5 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 61 xpression..** Ta
1dad6 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 20 66 75 6e ble-name and fun
1dad7 63 74 69 6f 6e 20 72 65 73 6f 6c 75 74 69 6f 6e ction resolution
1dad8 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65 20 73 occurs on the s
1dad9 75 62 73 74 69 74 75 74 65 64 20 65 78 70 72 65 ubstituted expre
1dada 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 20 20 ssion.** tree.
1dadb 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 6e 3a For example, in:
1dadc 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 .**.** SELE
1dadd 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 CT a+b AS x, c+d
1dade 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 AS y FROM t1 OR
1dadf 44 45 52 20 42 59 20 78 3b 0a 2a 2a 0a 2a 2a 20 DER BY x;.**.**
1dae0 54 68 65 20 22 78 22 20 74 65 72 6d 20 6f 66 20 The "x" term of
1dae1 74 68 65 20 6f 72 64 65 72 20 62 79 20 69 73 20 the order by is
1dae2 72 65 70 6c 61 63 65 64 20 62 79 20 22 61 2b 62 replaced by "a+b
1dae3 22 20 74 6f 20 72 65 6e 64 65 72 3a 0a 2a 2a 0a " to render:.**.
1dae4 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 ** SELECT a
1dae5 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20 +b AS x, c+d AS
1dae6 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 y FROM t1 ORDER
1dae7 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a 2a 20 46 75 BY a+b;.**.** Fu
1dae8 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 61 72 65 nction calls are
1dae9 20 63 68 65 63 6b 65 64 20 74 6f 20 6d 61 6b 65 checked to make
1daea 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 66 sure that the f
1daeb 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a 2a 20 64 unction is .** d
1daec 65 66 69 6e 65 64 20 61 6e 64 20 74 68 61 74 20 efined and that
1daed 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 the correct numb
1daee 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
1daef 61 72 65 20 73 70 65 63 69 66 69 65 64 2e 0a 2a are specified..*
1daf0 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f * If the functio
1daf1 6e 20 69 73 20 61 6e 20 61 67 67 72 65 67 61 74 n is an aggregat
1daf2 65 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e e function, then
1daf3 20 74 68 65 20 70 4e 43 2d 3e 68 61 73 41 67 67 the pNC->hasAgg
1daf4 20 69 73 0a 2a 2a 20 73 65 74 20 61 6e 64 20 74 is.** set and t
1daf5 68 65 20 6f 70 63 6f 64 65 20 69 73 20 63 68 61 he opcode is cha
1daf6 6e 67 65 64 20 66 72 6f 6d 20 54 4b 5f 46 55 4e nged from TK_FUN
1daf7 43 54 49 4f 4e 20 74 6f 20 54 4b 5f 41 47 47 5f CTION to TK_AGG_
1daf8 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a 20 49 66 20 FUNCTION..** If
1daf9 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f an expression co
1dafa 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 ntains aggregate
1dafb 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 65 6e 20 functions then
1dafc 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a 20 70 72 the EP_Agg.** pr
1dafd 6f 70 65 72 74 79 20 6f 6e 20 74 68 65 20 65 78 operty on the ex
1dafe 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 74 2e pression is set.
1daff 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 .**.** An error
1db00 6d 65 73 73 61 67 65 20 69 73 20 6c 65 66 74 20 message is left
1db01 69 6e 20 70 50 61 72 73 65 20 69 66 20 61 6e 79 in pParse if any
1db02 74 68 69 6e 67 20 69 73 20 61 6d 69 73 73 2e 20 thing is amiss.
1db03 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 69 The number.** i
1db04 66 20 65 72 72 6f 72 73 20 69 73 20 72 65 74 75 f errors is retu
1db05 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
1db06 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1db07 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
1db08 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43 6f 6e 74 mes( . NameCont
1db09 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 20 20 ext *pNC,
1db0a 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20 74 6f 20 /* Namespace to
1db0b 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73 69 resolve expressi
1db0c 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20 45 78 70 ons in. */. Exp
1db0d 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20 r *pExpr
1db0e 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 /* The expr
1db0f 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61 ession to be ana
1db10 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a 20 20 69 lyzed. */.){. i
1db11 6e 74 20 73 61 76 65 64 48 61 73 41 67 67 3b 0a nt savedHasAgg;.
1db12 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 69 Walker w;.. i
1db13 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 f( pExpr==0 ) re
1db14 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 turn 0;.#if SQLI
1db15 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 TE_MAX_EXPR_DEPT
1db16 48 3e 30 0a 20 20 7b 0a 20 20 20 20 50 61 72 73 H>0. {. Pars
1db17 65 20 2a 70 50 61 72 73 65 20 3d 20 70 4e 43 2d e *pParse = pNC-
1db18 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 69 66 28 >pParse;. if(
1db19 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 sqlite3ExprChec
1db1a 6b 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 kHeight(pParse,
1db1b 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 2b 70 pExpr->nHeight+p
1db1c 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 NC->pParse->nHei
1db1d 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 ght) ){. re
1db1e 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
1db1f 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 pParse->nHeigh
1db20 74 20 2b 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 t += pExpr->nHei
1db21 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ght;. }.#endif.
1db22 20 20 73 61 76 65 64 48 61 73 41 67 67 20 3d 20 savedHasAgg =
1db23 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a 20 20 70 pNC->hasAgg;. p
1db24 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 30 3b 0a NC->hasAgg = 0;.
1db25 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 w.xExprCallbac
1db26 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53 k = resolveExprS
1db27 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 tep;. w.xSelect
1db28 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c Callback = resol
1db29 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 veSelectStep;.
1db2a 77 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e w.pParse = pNC->
1db2b 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e pParse;. w.u.pN
1db2c 43 20 3d 20 70 4e 43 3b 0a 20 20 73 71 6c 69 74 C = pNC;. sqlit
1db2d 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 e3WalkExpr(&w, p
1db2e 45 78 70 72 29 3b 0a 23 69 66 20 53 51 4c 49 54 Expr);.#if SQLIT
1db2f 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 E_MAX_EXPR_DEPTH
1db30 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61 72 73 65 >0. pNC->pParse
1db31 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 70 45 78 ->nHeight -= pEx
1db32 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 23 65 6e pr->nHeight;.#en
1db33 64 69 66 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e dif. if( pNC->n
1db34 45 72 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 70 Err>0 ){. Exp
1db35 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 rSetProperty(pEx
1db36 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a 20 pr, EP_Error);.
1db37 20 7d 0a 20 20 69 66 28 20 70 4e 43 2d 3e 68 61 }. if( pNC->ha
1db38 73 41 67 67 20 29 7b 0a 20 20 20 20 45 78 70 72 sAgg ){. Expr
1db39 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 SetProperty(pExp
1db3a 72 2c 20 45 50 5f 41 67 67 29 3b 0a 20 20 7d 65 r, EP_Agg);. }e
1db3b 6c 73 65 20 69 66 28 20 73 61 76 65 64 48 61 73 lse if( savedHas
1db3c 41 67 67 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e Agg ){. pNC->
1db3d 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a hasAgg = 1;. }.
1db3e 20 20 72 65 74 75 72 6e 20 45 78 70 72 48 61 73 return ExprHas
1db3f 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
1db40 45 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f EP_Error);.}.../
1db41 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c *.** Resolve all
1db42 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 65 78 names in all ex
1db43 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 20 53 pressions of a S
1db44 45 4c 45 43 54 20 61 6e 64 20 69 6e 20 61 6c 6c ELECT and in all
1db45 0a 2a 2a 20 64 65 63 65 6e 64 65 6e 74 73 20 6f .** decendents o
1db46 66 20 74 68 65 20 53 45 4c 45 43 54 2c 20 69 6e f the SELECT, in
1db47 63 6c 75 64 69 6e 67 20 63 6f 6d 70 6f 75 6e 64 cluding compound
1db48 73 20 6f 66 66 20 6f 66 20 70 2d 3e 70 50 72 69 s off of p->pPri
1db49 6f 72 2c 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 or,.** subquerie
1db4a 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 s in expressions
1db4b 2c 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73 , and subqueries
1db4c 20 75 73 65 64 20 61 73 20 46 52 4f 4d 20 63 6c used as FROM cl
1db4d 61 75 73 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a 2a ause.** terms..*
1db4e 2a 0a 2a 2a 20 53 65 65 20 73 71 6c 69 74 65 33 *.** See sqlite3
1db4f 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
1db50 28 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 () for a descrip
1db51 74 69 6f 6e 20 6f 66 20 74 68 65 20 6b 69 6e 64 tion of the kind
1db52 73 20 6f 66 0a 2a 2a 20 74 72 61 6e 73 66 6f 72 s of.** transfor
1db53 6d 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 63 63 mations that occ
1db54 75 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 45 ur..**.** All SE
1db55 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 LECT statements
1db56 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e should have been
1db57 20 65 78 70 61 6e 64 65 64 20 75 73 69 6e 67 0a expanded using.
1db58 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 ** sqlite3Select
1db59 45 78 70 61 6e 64 28 29 20 70 72 69 6f 72 20 74 Expand() prior t
1db5a 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 o invoking this
1db5b 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 routine..*/.SQLI
1db5c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1db5d 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 sqlite3ResolveSe
1db5e 6c 65 63 74 4e 61 6d 65 73 28 0a 20 20 50 61 72 lectNames(. Par
1db5f 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
1db60 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 /* The parse
1db61 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 r context */. S
1db62 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 elect *p,
1db63 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c /* The SEL
1db64 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 ECT statement be
1db65 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 ing coded. */.
1db66 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 NameContext *pOu
1db67 74 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63 terNC /* Name c
1db68 6f 6e 74 65 78 74 20 66 6f 72 20 70 61 72 65 6e ontext for paren
1db69 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 t SELECT stateme
1db6a 6e 74 20 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b 65 nt */.){. Walke
1db6b 72 20 77 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 r w;.. assert(
1db6c 70 21 3d 30 20 29 3b 0a 20 20 77 2e 78 45 78 70 p!=0 );. w.xExp
1db6d 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f rCallback = reso
1db6e 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 77 lveExprStep;. w
1db6f 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b .xSelectCallback
1db70 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 = resolveSelect
1db71 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 Step;. w.pParse
1db72 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 = pParse;. w.u
1db73 2e 70 4e 43 20 3d 20 70 4f 75 74 65 72 4e 43 3b .pNC = pOuterNC;
1db74 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 . sqlite3WalkSe
1db75 6c 65 63 74 28 26 77 2c 20 70 29 3b 0a 7d 0a 0a lect(&w, p);.}..
1db76 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1db77 45 6e 64 20 6f 66 20 72 65 73 6f 6c 76 65 2e 63 End of resolve.c
1db78 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1db79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1db7b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1db7c 42 65 67 69 6e 20 66 69 6c 65 20 65 78 70 72 2e Begin file expr.
1db7d 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1db7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1db80 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
1db81 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
1db82 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1db83 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1db84 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1db85 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1db86 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1db87 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1db88 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1db89 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1db8a 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1db8b 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1db8c 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1db8d 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1db8e 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1db8f 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1db90 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1db91 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1db92 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1db93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db97 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
1db98 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 ile contains rou
1db99 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 61 tines used for a
1db9a 6e 61 6c 79 7a 69 6e 67 20 65 78 70 72 65 73 73 nalyzing express
1db9b 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 ions and.** for
1db9c 67 65 6e 65 72 61 74 69 6e 67 20 56 44 42 45 20 generating VDBE
1db9d 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 61 code that evalua
1db9e 74 65 73 20 65 78 70 72 65 73 73 69 6f 6e 73 20 tes expressions
1db9f 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a in SQLite..**.**
1dba0 20 24 49 64 3a 20 65 78 70 72 2e 63 2c 76 20 31 $Id: expr.c,v 1
1dba1 2e 34 30 39 20 32 30 30 39 2f 30 31 2f 31 30 20 .409 2009/01/10
1dba2 31 33 3a 32 34 3a 35 31 20 64 72 68 20 45 78 70 13:24:51 drh Exp
1dba3 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 $.*/../*.** Ret
1dba4 75 72 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74 urn the 'affinit
1dba5 79 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 y' of the expres
1dba6 73 69 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e sion pExpr if an
1dba7 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70 y..**.** If pExp
1dba8 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 r is a column, a
1dba9 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 reference to a
1dbaa 63 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41 column via an 'A
1dbab 53 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 S' alias,.** or
1dbac 61 20 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74 a sub-select wit
1dbad 68 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 h a column as th
1dbae 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20 e return value,
1dbaf 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66 then the .** aff
1dbb0 69 6e 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f inity of that co
1dbb1 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64 lumn is returned
1dbb2 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30 . Otherwise, 0x0
1dbb3 30 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 0 is returned,.*
1dbb4 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 * indicating no
1dbb5 61 66 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65 affinity for the
1dbb6 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a expression..**.
1dbb7 2a 2a 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52 ** i.e. the WHER
1dbb8 45 20 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 E clause express
1dbb9 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c sions in the fol
1dbba 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 lowing statement
1dbbb 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e s all.** have an
1dbbc 20 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a affinity:.**.**
1dbbd 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 CREATE TABLE t1
1dbbe 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a (a);.** SELECT *
1dbbf 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 FROM t1 WHERE a
1dbc0 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53 ;.** SELECT a AS
1dbc1 20 62 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 b FROM t1 WHERE
1dbc2 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 b;.** SELECT *
1dbc3 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73 FROM t1 WHERE (s
1dbc4 65 6c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29 elect a from t1)
1dbc5 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.*/.SQLITE_PRIV
1dbc6 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 ATE char sqlite3
1dbc7 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 ExprAffinity(Exp
1dbc8 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 r *pExpr){. int
1dbc9 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b op = pExpr->op;
1dbca 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 . if( op==TK_SE
1dbcb 4c 45 43 54 20 29 7b 0a 20 20 20 20 72 65 74 75 LECT ){. retu
1dbcc 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 rn sqlite3ExprAf
1dbcd 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 53 finity(pExpr->pS
1dbce 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 elect->pEList->a
1dbcf 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a [0].pExpr);. }.
1dbd0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1dbd1 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f MIT_CAST. if( o
1dbd2 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20 p==TK_CAST ){.
1dbd3 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1dbd4 41 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45 AffinityType(&pE
1dbd5 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 7d xpr->token);. }
1dbd6 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f .#endif. if( (o
1dbd7 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e p==TK_AGG_COLUMN
1dbd8 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d || op==TK_COLUM
1dbd9 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 N || op==TK_REGI
1dbda 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78 STER) . && pEx
1dbdb 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b pr->pTab!=0. ){
1dbdc 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 . /* op==TK_R
1dbdd 45 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72 EGISTER && pExpr
1dbde 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e ->pTab!=0 happen
1dbdf 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 s when pExpr was
1dbe0 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 originally.
1dbe1 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 ** a TK_COLUMN b
1dbe2 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c ut was previousl
1dbe3 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 y evaluated and
1dbe4 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 cached in a regi
1dbe5 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ster */. int
1dbe6 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 j = pExpr->iColu
1dbe7 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20 mn;. if( j<0
1dbe8 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
1dbe9 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 AFF_INTEGER;.
1dbea 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
1dbeb 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d pTab && j<pExpr-
1dbec 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 >pTab->nCol );.
1dbed 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d return pExpr-
1dbee 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61 >pTab->aCol[j].a
1dbef 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72 ffinity;. }. r
1dbf0 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66 eturn pExpr->aff
1dbf1 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 inity;.}../*.**
1dbf2 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e Set the collatin
1dbf3 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 g sequence for e
1dbf4 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 xpression pExpr
1dbf5 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74 to be the collat
1dbf6 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ing.** sequence
1dbf7 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e named by pToken.
1dbf8 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e Return a poin
1dbf9 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73 ter to the revis
1dbfa 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a ed expression..*
1dbfb 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 * The collating
1dbfc 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b sequence is mark
1dbfd 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22 ed as "explicit"
1dbfe 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78 using the EP_Ex
1dbff 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67 pCollate.** flag
1dc00 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63 . An explicit c
1dc01 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1dc02 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20 e will override
1dc03 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c implicit.** coll
1dc04 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e ating sequences.
1dc05 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1dc06 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
1dc07 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 ExprSetColl(Pars
1dc08 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
1dc09 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 *pExpr, Token *p
1dc0a 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61 CollName){. cha
1dc0b 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 r *zColl = 0;
1dc0c 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 /* Dequ
1dc0d 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c oted name of col
1dc0e 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
1dc0f 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 */. CollSeq *pC
1dc10 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a oll;. sqlite3 *
1dc11 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
1dc12 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 . zColl = sqlit
1dc13 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
1dc14 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a db, pCollName);.
1dc15 20 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a if( pExpr && z
1dc16 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c Coll ){. pCol
1dc17 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 l = sqlite3Locat
1dc18 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c eCollSeq(pParse,
1dc19 20 7a 43 6f 6c 6c 2c 20 2d 31 29 3b 0a 20 20 20 zColl, -1);.
1dc1a 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 if( pColl ){.
1dc1b 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c pExpr->pColl
1dc1c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 = pColl;.
1dc1d 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 pExpr->flags |=
1dc1e 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 EP_ExpCollate;.
1dc1f 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
1dc20 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f e3DbFree(db, zCo
1dc21 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 45 ll);. return pE
1dc22 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 xpr;.}../*.** Re
1dc23 74 75 72 6e 20 74 68 65 20 64 65 66 61 75 6c 74 turn the default
1dc24 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
1dc25 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78 70 72 nce for the expr
1dc26 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 49 66 ession pExpr. If
1dc27 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 .** there is no
1dc28 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f default collatio
1dc29 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e 20 30 n type, return 0
1dc2a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1dc2b 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c ATE CollSeq *sql
1dc2c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 ite3ExprCollSeq(
1dc2d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
1dc2e 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 43 xpr *pExpr){. C
1dc2f 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 ollSeq *pColl =
1dc30 30 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 70 0;. Expr *p = p
1dc31 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 Expr;. while( p
1dc32 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 3b 0a ){. int op;.
1dc33 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70 pColl = p->p
1dc34 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 28 20 70 43 Coll;. if( pC
1dc35 6f 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 oll ) break;.
1dc36 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 20 op = p->op;.
1dc37 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47 if( (op==TK_AGG
1dc38 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 _COLUMN || op==T
1dc39 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d K_COLUMN || op==
1dc3a 54 4b 5f 52 45 47 49 53 54 45 52 29 20 26 26 20 TK_REGISTER) &&
1dc3b 70 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20 p->pTab!=0 ){.
1dc3c 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45 /* op==TK_RE
1dc3d 47 49 53 54 45 52 20 26 26 20 70 2d 3e 70 54 61 GISTER && p->pTa
1dc3e 62 21 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65 b!=0 happens whe
1dc3f 6e 20 70 45 78 70 72 20 77 61 73 20 6f 72 69 67 n pExpr was orig
1dc40 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 inally. **
1dc41 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 a TK_COLUMN but
1dc42 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 65 was previously e
1dc43 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63 61 63 valuated and cac
1dc44 68 65 64 20 69 6e 20 61 20 72 65 67 69 73 74 65 hed in a registe
1dc45 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 r */. const
1dc46 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 char *zColl;.
1dc47 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 2d 3e 69 int j = p->i
1dc48 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69 66 Column;. if
1dc49 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 ( j>=0 ){.
1dc4a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1dc4b 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 pParse->db;.
1dc4c 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 2d 3e 70 zColl = p->p
1dc4d 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f Tab->aCol[j].zCo
1dc4e 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c ll;. pCol
1dc4f 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 l = sqlite3FindC
1dc50 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 ollSeq(db, ENC(d
1dc51 62 29 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30 b), zColl, -1, 0
1dc52 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 );. pExpr
1dc53 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b ->pColl = pColl;
1dc54 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
1dc55 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1dc56 69 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 if( op!=TK_CAST
1dc57 26 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 && op!=TK_UPLUS
1dc58 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a ){. break;.
1dc59 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d }. p = p-
1dc5a 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 >pLeft;. }. if
1dc5b 28 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f ( sqlite3CheckCo
1dc5c 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 llSeq(pParse, pC
1dc5d 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f oll) ){ . pCo
1dc5e 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 ll = 0;. }. re
1dc5f 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f turn pColl;.}../
1dc60 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e *.** pExpr is an
1dc61 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f operand of a co
1dc62 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f mparison operato
1dc63 72 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a r. aff2 is the.
1dc64 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 ** type affinity
1dc65 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 of the other op
1dc66 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 erand. This rou
1dc67 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
1dc68 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 .** type affinit
1dc69 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 y that should be
1dc6a 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f used for the co
1dc6b 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f mparison operato
1dc6c 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
1dc6d 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 VATE char sqlite
1dc6e 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 3CompareAffinity
1dc6f 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 (Expr *pExpr, ch
1dc70 61 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 ar aff2){. char
1dc71 20 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 aff1 = sqlite3E
1dc72 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 xprAffinity(pExp
1dc73 72 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 r);. if( aff1 &
1dc74 26 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a & aff2 ){. /*
1dc75 20 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 Both sides of t
1dc76 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 he comparison ar
1dc77 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e e columns. If on
1dc78 65 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 e has numeric.
1dc79 20 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 ** affinity, u
1dc7a 73 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 se that. Otherwi
1dc7b 73 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 se use no affini
1dc7c 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ty.. */. i
1dc7d 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 f( sqlite3IsNume
1dc7e 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 ricAffinity(aff1
1dc7f 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 ) || sqlite3IsNu
1dc80 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 mericAffinity(af
1dc81 66 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 f2) ){. ret
1dc82 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e urn SQLITE_AFF_N
1dc83 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 UMERIC;. }els
1dc84 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e{. return
1dc85 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b SQLITE_AFF_NONE;
1dc86 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
1dc87 66 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66 f( !aff1 && !aff
1dc88 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 2 ){. /* Neit
1dc89 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 her side of the
1dc8a 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 comparison is a
1dc8b 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 column. Compare
1dc8c 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 the. ** resu
1dc8d 6c 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 lts directly..
1dc8e 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 */. return
1dc8f 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b SQLITE_AFF_NONE;
1dc90 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1dc91 20 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 One side is a c
1dc92 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 olumn, the other
1dc93 20 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 is not. Use the
1dc94 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 columns affinit
1dc95 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 y. */. assert
1dc96 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 ( aff1==0 || aff
1dc97 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 2==0 );. retu
1dc98 72 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 rn (aff1 + aff2)
1dc99 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 ;. }.}../*.** p
1dc9a 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 Expr is a compar
1dc9b 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 ison operator.
1dc9c 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 Return the type
1dc9d 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 affinity that sh
1dc9e 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 ould.** be appli
1dc9f 65 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 ed to both opera
1dca0 6e 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 nds prior to doi
1dca1 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f ng the compariso
1dca2 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 n..*/.static cha
1dca3 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 r comparisonAffi
1dca4 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 nity(Expr *pExpr
1dca5 29 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 ){. char aff;.
1dca6 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
1dca7 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 op==TK_EQ || pEx
1dca8 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c pr->op==TK_IN ||
1dca9 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c pExpr->op==TK_L
1dcaa 54 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 T ||. p
1dcab 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 Expr->op==TK_GT
1dcac 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b || pExpr->op==TK
1dcad 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 _GE || pExpr->op
1dcae 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 ==TK_LE ||.
1dcaf 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d pExpr->op==
1dcb0 54 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 TK_NE );. asser
1dcb1 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 t( pExpr->pLeft
1dcb2 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 );. aff = sqlit
1dcb3 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 e3ExprAffinity(p
1dcb4 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 Expr->pLeft);.
1dcb5 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 if( pExpr->pRigh
1dcb6 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 t ){. aff = s
1dcb7 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 qlite3CompareAff
1dcb8 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69 inity(pExpr->pRi
1dcb9 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 0a 20 ght, aff);. }.
1dcba 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d else if( pExpr-
1dcbb 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 >pSelect ){.
1dcbc 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d aff = sqlite3Com
1dcbd 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45 78 pareAffinity(pEx
1dcbe 70 72 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 45 4c pr->pSelect->pEL
1dcbf 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c ist->a[0].pExpr,
1dcc0 20 61 66 66 29 3b 0a 20 20 7d 0a 20 20 65 6c 73 aff);. }. els
1dcc1 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a 20 20 e if( !aff ){.
1dcc2 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 aff = SQLITE_A
1dcc3 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20 72 FF_NONE;. }. r
1dcc4 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a eturn aff;.}../*
1dcc5 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 63 .** pExpr is a c
1dcc6 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 65 73 omparison expres
1dcc7 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c 20 27 sion, eg. '=', '
1dcc8 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 63 2e <', IN(...) etc.
1dcc9 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 74 79 .** idx_affinity
1dcca 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 74 79 is the affinity
1dccb 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20 63 of an indexed c
1dccc 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 74 72 olumn. Return tr
1dccd 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 6e 64 ue.** if the ind
1dcce 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 74 79 ex with affinity
1dccf 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 6d 61 idx_affinity ma
1dcd0 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 y be used to imp
1dcd1 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 63 6f lement.** the co
1dcd2 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 78 70 mparison in pExp
1dcd3 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
1dcd4 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1dcd5 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 IndexAffinityOk(
1dcd6 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 Expr *pExpr, cha
1dcd7 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29 7b r idx_affinity){
1dcd8 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 6f . char aff = co
1dcd9 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 mparisonAffinity
1dcda 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 74 63 (pExpr);. switc
1dcdb 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 63 61 h( aff ){. ca
1dcdc 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f se SQLITE_AFF_NO
1dcdd 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e NE:. return
1dcde 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 1;. case SQL
1dcdf 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 20 20 ITE_AFF_TEXT:.
1dce0 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f 61 return idx_a
1dce1 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f ffinity==SQLITE_
1dce2 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 64 65 AFF_TEXT;. de
1dce3 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 65 74 fault:. ret
1dce4 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e 75 6d urn sqlite3IsNum
1dce5 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 64 78 ericAffinity(idx
1dce6 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 7d 0a _affinity);. }.
1dce7 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1dce8 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 68 61 the P5 value tha
1dce9 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 t should be used
1dcea 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 63 6f for a binary co
1dceb 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63 6f mparison.** opco
1dcec 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f 47 65 de (OP_Eq, OP_Ge
1dced 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f 20 63 etc.) used to c
1dcee 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 61 6e ompare pExpr1 an
1dcef 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 74 61 d pExpr2..*/.sta
1dcf0 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 6f 6d tic u8 binaryCom
1dcf1 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 45 78 pareP5(Expr *pEx
1dcf2 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 70 72 pr1, Expr *pExpr
1dcf3 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 2, int jumpIfNul
1dcf4 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d 20 28 l){. u8 aff = (
1dcf5 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 70 72 char)sqlite3Expr
1dcf6 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 32 29 Affinity(pExpr2)
1dcf7 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 73 71 ;. aff = (u8)sq
1dcf8 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 lite3CompareAffi
1dcf9 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66 66 nity(pExpr1, aff
1dcfa 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 4e 75 ) | (u8)jumpIfNu
1dcfb 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 66 66 ll;. return aff
1dcfc 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1dcfd 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
1dcfe 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 he collation seq
1dcff 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75 6c uence that shoul
1dd00 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 d be used by.**
1dd01 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69 a binary compari
1dd02 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 6f 6d son operator com
1dd03 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 6e 64 paring pLeft and
1dd04 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 pRight..**.** I
1dd05 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20 f the left hand
1dd06 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61 expression has a
1dd07 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
1dd08 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e 20 69 nce type, then i
1dd09 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 4f 74 t is.** used. Ot
1dd0a 68 65 72 77 69 73 65 20 74 68 65 20 63 6f 6c 6c herwise the coll
1dd0b 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 ation sequence f
1dd0c 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 61 6e or the right han
1dd0d 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 d expression.**
1dd0e 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 65 20 is used, or the
1dd0f 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 59 29 default (BINARY)
1dd10 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 70 72 if neither expr
1dd11 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c ession has a col
1dd12 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a lating.** type..
1dd13 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 **.** Argument p
1dd14 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 20 70 Right (but not p
1dd15 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 20 6e Left) may be a n
1dd16 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 6e 20 ull pointer. In
1dd17 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 74 this case,.** it
1dd18 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 is not consider
1dd19 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1dd1a 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
1dd1b 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 qlite3BinaryComp
1dd1c 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 50 61 areCollSeq(. Pa
1dd1d 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 rse *pParse, .
1dd1e 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a 20 20 Expr *pLeft, .
1dd1f 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a Expr *pRight.){.
1dd20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
1dd21 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 ;. assert( pLef
1dd22 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 t );. if( pLeft
1dd23 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 ->flags & EP_Exp
1dd24 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 Collate ){. a
1dd25 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 70 43 ssert( pLeft->pC
1dd26 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c oll );. pColl
1dd27 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b = pLeft->pColl;
1dd28 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 69 . }else if( pRi
1dd29 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e 66 ght && pRight->f
1dd2a 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c lags & EP_ExpCol
1dd2b 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 late ){. asse
1dd2c 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c rt( pRight->pCol
1dd2d 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d l );. pColl =
1dd2e 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a pRight->pColl;.
1dd2f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f }else{. pCo
1dd30 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ll = sqlite3Expr
1dd31 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 CollSeq(pParse,
1dd32 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 20 pLeft);. if(
1dd33 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 !pColl ){.
1dd34 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 pColl = sqlite3E
1dd35 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 xprCollSeq(pPars
1dd36 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20 e, pRight);.
1dd37 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 }. }. return p
1dd38 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 Coll;.}../*.** G
1dd39 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70 65 72 enerate the oper
1dd3a 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d 70 61 ands for a compa
1dd3b 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 2e rison operation.
1dd3c 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65 6e 65 Before.** gene
1dd3d 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20 rating the code
1dd3e 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61 6e 64 for each operand
1dd3f 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41 6e 79 , set the EP_Any
1dd40 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 74 Aff.** flag on t
1dd41 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 6f he expression so
1dd42 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 that it will be
1dd43 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20 61 0a able to used a.
1dd44 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e ** cached column
1dd45 20 76 61 6c 75 65 20 74 68 61 74 20 68 61 73 20 value that has
1dd46 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64 65 72 previously under
1dd47 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66 69 6e gone an.** affin
1dd48 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 73 ity change..*/.s
1dd49 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 43 tatic void codeC
1dd4a 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 0a ompareOperands(.
1dd4b 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
1dd4c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 /* Parsing a
1dd4d 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 nd code generati
1dd4e 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
1dd4f 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 Expr *pLeft,
1dd50 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 /* The left op
1dd51 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a erand */. int *
1dd52 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f 2a 20 pRegLeft, /*
1dd53 52 65 67 69 73 74 65 72 20 77 68 65 72 65 20 6c Register where l
1dd54 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73 eft operand is s
1dd55 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a tored */. int *
1dd56 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f 2a 20 pFreeLeft, /*
1dd57 46 72 65 65 20 74 68 69 73 20 72 65 67 69 73 74 Free this regist
1dd58 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a 2f 0a er when done */.
1dd59 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 Expr *pRight,
1dd5a 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 /* The right
1dd5b 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e operand */. in
1dd5c 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20 20 20 t *pRegRight,
1dd5d 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 72 /* Register wher
1dd5e 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 e right operand
1dd5f 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 is stored */. i
1dd60 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74 20 20 nt *pFreeRight
1dd61 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70 20 72 /* Write temp r
1dd62 65 67 69 73 74 65 72 20 66 6f 72 20 72 69 67 68 egister for righ
1dd63 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72 65 20 t operand there
1dd64 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 */.){. while( p
1dd65 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c Left->op==TK_UPL
1dd66 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70 4c 65 US ) pLeft = pLe
1dd67 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 4c 65 ft->pLeft;. pLe
1dd68 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f ft->flags |= EP_
1dd69 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 4c AnyAff;. *pRegL
1dd6a 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 eft = sqlite3Exp
1dd6b 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
1dd6c 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65 4c 65 , pLeft, pFreeLe
1dd6d 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 52 ft);. while( pR
1dd6e 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c ight->op==TK_UPL
1dd6f 55 53 20 29 20 70 52 69 67 68 74 20 3d 20 70 52 US ) pRight = pR
1dd70 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 ight->pLeft;. p
1dd71 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 Right->flags |=
1dd72 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 EP_AnyAff;. *pR
1dd73 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 egRight = sqlite
1dd74 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
1dd75 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 70 46 arse, pRight, pF
1dd76 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a reeRight);.}../*
1dd77 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
1dd78 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 e for a comparis
1dd79 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a on operator..*/.
1dd7a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 43 static int codeC
1dd7b 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73 65 20 ompare(. Parse
1dd7c 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 54 *pParse, /* T
1dd7d 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e 64 20 he parsing (and
1dd7e 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 29 code generating)
1dd7f 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 context */. Ex
1dd80 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 pr *pLeft,
1dd81 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 65 72 /* The left oper
1dd82 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 and */. Expr *p
1dd83 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 54 68 Right, /* Th
1dd84 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 e right operand
1dd85 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65 2c */. int opcode,
1dd86 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f /* The co
1dd87 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64 65 20 mparison opcode
1dd88 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20 69 6e */. int in1, in
1dd89 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69 73 74 t in2, /* Regist
1dd8a 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65 72 61 er holding opera
1dd8b 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 nds */. int des
1dd8c 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 t, /* Ju
1dd8d 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 65 2e mp here if true.
1dd8e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d 70 49 */. int jumpI
1dd8f 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 20 74 fNull /* If t
1dd90 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 69 74 rue, jump if eit
1dd91 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e her operand is N
1dd92 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ULL */.){. int
1dd93 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a p5;. int addr;.
1dd94 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a CollSeq *p4;..
1dd95 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33 42 69 p4 = sqlite3Bi
1dd96 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 naryCompareCollS
1dd97 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 eq(pParse, pLeft
1dd98 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 35 20 , pRight);. p5
1dd99 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 = binaryCompareP
1dd9a 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 5(pLeft, pRight,
1dd9b 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 jumpIfNull);.
1dd9c 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 addr = sqlite3Vd
1dd9d 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 65 2d beAddOp4(pParse-
1dd9e 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 2c 20 >pVdbe, opcode,
1dd9f 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 2c 0a in2, dest, in1,.
1dda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dda1 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 (void
1dda2 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 *)p4, P4_COLLSEQ
1dda3 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1dda4 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 65 2d ChangeP5(pParse-
1dda5 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 29 3b >pVdbe, (u8)p5);
1dda6 0a 20 20 69 66 28 20 28 70 35 20 26 20 53 51 4c . if( (p5 & SQL
1dda7 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21 3d 53 ITE_AFF_MASK)!=S
1dda8 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 QLITE_AFF_NONE )
1dda9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 {. sqlite3Exp
1ddaa 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 rCacheAffinityCh
1ddab 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 31 ange(pParse, in1
1ddac 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 1);. sqlite
1ddad 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 3ExprCacheAffini
1ddae 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c tyChange(pParse,
1ddaf 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 in2, 1);. }.
1ddb0 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a return addr;.}..
1ddb1 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 #if SQLITE_MAX_E
1ddb2 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a 2a XPR_DEPTH>0./*.*
1ddb3 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 72 67 * Check that arg
1ddb4 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 69 73 ument nHeight is
1ddb5 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
1ddb6 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 6d ual to the maxim
1ddb7 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e um.** expression
1ddb8 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 2e 20 depth allowed.
1ddb9 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 6c 65 If it is not, le
1ddba 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ave an error mes
1ddbb 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 72 73 sage in.** pPars
1ddbc 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1ddbd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1ddbe 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 ExprCheckHeight(
1ddbf 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
1ddc0 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 nt nHeight){. i
1ddc1 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1ddc2 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 67 68 K;. int mxHeigh
1ddc3 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e t = pParse->db->
1ddc4 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1ddc5 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d 3b MIT_EXPR_DEPTH];
1ddc6 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 3e 6d . if( nHeight>m
1ddc7 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 73 xHeight ){. s
1ddc8 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1ddc9 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 22 Parse, . "
1ddca 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 Expression tree
1ddcb 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 6d 61 is too large (ma
1ddcc 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64 29 22 ximum depth %d)"
1ddcd 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20 20 29 , mxHeight. )
1ddce 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
1ddcf 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 E_ERROR;. }. r
1ddd0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 eturn rc;.}../*
1ddd1 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 The following th
1ddd2 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 68 ree functions, h
1ddd3 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20 68 eightOfExpr(), h
1ddd4 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 eightOfExprList(
1ddd5 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74 4f ).** and heightO
1ddd6 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20 75 fSelect(), are u
1ddd7 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 sed to determine
1ddd8 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 the maximum hei
1ddd9 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 78 ght.** of any ex
1ddda 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 65 pression tree re
1dddb 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 20 ferenced by the
1dddc 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 structure passed
1dddd 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 as the.** first
1ddde 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
1dddf 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75 6d If this maximum
1dde0 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74 height is great
1dde1 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 er than the curr
1dde2 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 ent value pointe
1dde3 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 69 d.** to by pnHei
1dde4 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 ght, the second
1dde5 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 parameter, then
1dde6 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74 6f set *pnHeight to
1dde7 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a that.** value..
1dde8 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 */.static void h
1dde9 65 69 67 68 74 4f 66 45 78 70 72 28 45 78 70 72 eightOfExpr(Expr
1ddea 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 *p, int *pnHeig
1ddeb 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a ht){. if( p ){.
1ddec 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69 67 if( p->nHeig
1dded 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b 0a ht>*pnHeight ){.
1ddee 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74 20 *pnHeight
1ddef 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 = p->nHeight;.
1ddf0 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 }. }.}.static
1ddf1 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 78 void heightOfEx
1ddf2 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20 prList(ExprList
1ddf3 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 *p, int *pnHeigh
1ddf4 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 t){. if( p ){.
1ddf5 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
1ddf6 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 r(i=0; i<p->nExp
1ddf7 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 68 r; i++){. h
1ddf8 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 61 eightOfExpr(p->a
1ddf9 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 69 [i].pExpr, pnHei
1ddfa 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ght);. }. }.
1ddfb 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 }.static void he
1ddfc 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65 6c ightOfSelect(Sel
1ddfd 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 ect *p, int *pnH
1ddfe 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 eight){. if( p
1ddff 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 ){. heightOfE
1de00 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20 70 xpr(p->pWhere, p
1de01 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 nHeight);. he
1de02 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 48 ightOfExpr(p->pH
1de03 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74 29 aving, pnHeight)
1de04 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 ;. heightOfEx
1de05 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e pr(p->pLimit, pn
1de06 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 Height);. hei
1de07 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f 66 ghtOfExpr(p->pOf
1de08 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29 3b fset, pnHeight);
1de09 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 . heightOfExp
1de0a 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 2c rList(p->pEList,
1de0b 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 pnHeight);.
1de0c 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 heightOfExprList
1de0d 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70 6e (p->pGroupBy, pn
1de0e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 Height);. hei
1de0f 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d ghtOfExprList(p-
1de10 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65 69 >pOrderBy, pnHei
1de11 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 ght);. height
1de12 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72 69 OfSelect(p->pPri
1de13 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 or, pnHeight);.
1de14 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 }.}../*.** Set
1de15 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74 the Expr.nHeight
1de16 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68 65 variable in the
1de17 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 structure passe
1de18 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67 75 d as an .** argu
1de19 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73 73 ment. An express
1de1a 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69 6c ion with no chil
1de1b 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73 74 dren, Expr.pList
1de1c 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53 65 or .** Expr.pSe
1de1d 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73 20 lect member has
1de1e 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20 41 a height of 1. A
1de1f 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 ny other express
1de20 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65 69 ion.** has a hei
1de21 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68 65 ght equal to the
1de22 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 maximum height
1de23 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a 2a of any other .**
1de24 20 72 65 66 65 72 65 6e 63 65 64 20 45 78 70 72 referenced Expr
1de25 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 plus one..*/.st
1de26 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 53 65 atic void exprSe
1de27 74 48 65 69 67 68 74 28 45 78 70 72 20 2a 70 29 tHeight(Expr *p)
1de28 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 {. int nHeight
1de29 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 = 0;. heightOfE
1de2a 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 6e xpr(p->pLeft, &n
1de2b 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69 67 68 Height);. heigh
1de2c 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 67 68 tOfExpr(p->pRigh
1de2d 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 t, &nHeight);.
1de2e 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 heightOfExprList
1de2f 28 70 2d 3e 70 4c 69 73 74 2c 20 26 6e 48 65 69 (p->pList, &nHei
1de30 67 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 ght);. heightOf
1de31 53 65 6c 65 63 74 28 70 2d 3e 70 53 65 6c 65 63 Select(p->pSelec
1de32 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 t, &nHeight);.
1de33 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65 p->nHeight = nHe
1de34 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a ight + 1;.}../*.
1de35 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e ** Set the Expr.
1de36 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c 65 nHeight variable
1de37 20 75 73 69 6e 67 20 74 68 65 20 65 78 70 72 53 using the exprS
1de38 65 74 48 65 69 67 68 74 28 29 20 66 75 6e 63 74 etHeight() funct
1de39 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 68 ion. If.** the h
1de3a 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72 eight is greater
1de3b 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 than the maximu
1de3c 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 73 m allowed expres
1de3d 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 6c sion depth,.** l
1de3e 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e eave an error in
1de3f 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 pParse..*/.SQLI
1de40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1de41 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 sqlite3ExprSetHe
1de42 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 ight(Parse *pPar
1de43 73 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 se, Expr *p){.
1de44 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70 29 exprSetHeight(p)
1de45 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 ;. sqlite3ExprC
1de46 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73 heckHeight(pPars
1de47 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b 0a e, p->nHeight);.
1de48 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1de49 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 the maximum heig
1de4a 68 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 ht of any expres
1de4b 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 sion tree refere
1de4c 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 nced.** by the s
1de4d 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 elect statement
1de4e 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 passed as an arg
1de4f 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ument..*/.SQLITE
1de50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1de51 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 ite3SelectExprHe
1de52 69 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 7b ight(Select *p){
1de53 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d . int nHeight =
1de54 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65 0;. heightOfSe
1de55 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68 74 lect(p, &nHeight
1de56 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 69 );. return nHei
1de57 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 ght;.}.#else. #
1de58 64 65 66 69 6e 65 20 65 78 70 72 53 65 74 48 65 define exprSetHe
1de59 69 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20 2f ight(y).#endif /
1de5a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 * SQLITE_MAX_EXP
1de5b 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a R_DEPTH>0 */../*
1de5c 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 .** Construct a
1de5d 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e new expression n
1de5e 6f 64 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ode and return a
1de5f 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 pointer to it.
1de60 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f 72 20 74 Memory.** for t
1de61 68 69 73 20 6e 6f 64 65 20 69 73 20 6f 62 74 61 his node is obta
1de62 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
1de63 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65 3_malloc(). The
1de64 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f calling functio
1de65 6e 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 n.** is responsi
1de66 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 ble for making s
1de67 75 72 65 20 74 68 65 20 6e 6f 64 65 20 65 76 65 ure the node eve
1de68 6e 74 75 61 6c 6c 79 20 67 65 74 73 20 66 72 65 ntually gets fre
1de69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1de6a 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
1de6b 74 65 33 45 78 70 72 28 0a 20 20 73 71 6c 69 74 te3Expr(. sqlit
1de6c 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
1de6d 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 /* Handle for
1de6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1de6f 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e Zero() (may be n
1de70 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 ull) */. int op
1de71 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1de72 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 /* Expression
1de73 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 opcode */. Expr
1de74 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20 *pLeft,
1de75 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 /* Left oper
1de76 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 and */. Expr *p
1de77 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 Right,
1de78 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e /* Right operan
1de79 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b d */. const Tok
1de7a 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f en *pToken /
1de7b 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e * Argument token
1de7c 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 */.){. Expr *p
1de7d 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 New;. pNew = sq
1de7e 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
1de7f 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 o(db, sizeof(Exp
1de80 72 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d r));. if( pNew=
1de81 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 =0 ){. /* Whe
1de82 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 n malloc fails,
1de83 64 65 6c 65 74 65 20 70 4c 65 66 74 20 61 6e 64 delete pLeft and
1de84 20 70 52 69 67 68 74 2e 20 45 78 70 72 65 73 73 pRight. Express
1de85 69 6f 6e 73 20 70 61 73 73 65 64 20 74 6f 20 0a ions passed to .
1de86 20 20 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63 ** this func
1de87 74 69 6f 6e 20 6d 75 73 74 20 61 6c 77 61 79 73 tion must always
1de88 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 77 69 be allocated wi
1de89 74 68 20 73 71 6c 69 74 65 33 45 78 70 72 28 29 th sqlite3Expr()
1de8a 20 66 6f 72 20 74 68 69 73 20 0a 20 20 20 20 2a for this . *
1de8b 2a 20 72 65 61 73 6f 6e 2e 20 0a 20 20 20 20 2a * reason. . *
1de8c 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 /. sqlite3Exp
1de8d 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 65 66 rDelete(db, pLef
1de8e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 t);. sqlite3E
1de8f 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 52 xprDelete(db, pR
1de90 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75 72 ight);. retur
1de91 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d n 0;. }. pNew-
1de92 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 >op = (u8)op;.
1de93 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 70 4c pNew->pLeft = pL
1de94 65 66 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69 eft;. pNew->pRi
1de95 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 ght = pRight;.
1de96 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b pNew->iAgg = -1;
1de97 0a 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20 . pNew->span.z
1de98 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 69 66 28 = (u8*)"";. if(
1de99 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 61 pToken ){. a
1de9a 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 64 ssert( pToken->d
1de9b 79 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 70 4e 65 yn==0 );. pNe
1de9c 77 2d 3e 73 70 61 6e 20 3d 20 70 4e 65 77 2d 3e w->span = pNew->
1de9d 74 6f 6b 65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b token = *pToken;
1de9e 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 . }else if( pLe
1de9f 66 74 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52 ft ){. if( pR
1dea0 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66 ight ){. if
1dea1 28 20 70 52 69 67 68 74 2d 3e 73 70 61 6e 2e 64 ( pRight->span.d
1dea2 79 6e 3d 3d 30 20 26 26 20 70 4c 65 66 74 2d 3e yn==0 && pLeft->
1dea3 73 70 61 6e 2e 64 79 6e 3d 3d 30 20 29 7b 0a 20 span.dyn==0 ){.
1dea4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
1dea5 70 72 53 70 61 6e 28 70 4e 65 77 2c 20 26 70 4c prSpan(pNew, &pL
1dea6 65 66 74 2d 3e 73 70 61 6e 2c 20 26 70 52 69 67 eft->span, &pRig
1dea7 68 74 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20 ht->span);.
1dea8 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 }. if( pRi
1dea9 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f ght->flags & EP_
1deaa 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 ExpCollate ){.
1deab 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 pNew->flag
1deac 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 s |= EP_ExpColla
1dead 74 65 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 te;. pNew
1deae 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68 74 ->pColl = pRight
1deaf 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d ->pColl;. }
1deb0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1deb1 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 Left->flags & EP
1deb2 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 _ExpCollate ){.
1deb3 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 pNew->flags
1deb4 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 |= EP_ExpCollat
1deb5 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 e;. pNew->p
1deb6 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 Coll = pLeft->pC
1deb7 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a oll;. }. }..
1deb8 20 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 exprSetHeight(
1deb9 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 pNew);. return
1deba 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 pNew;.}../*.** W
1debb 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 orks like sqlite
1debc 33 45 78 70 72 28 29 20 65 78 63 65 70 74 20 74 3Expr() except t
1debd 68 61 74 20 69 74 20 74 61 6b 65 73 20 61 6e 20 hat it takes an
1debe 65 78 74 72 61 20 50 61 72 73 65 2a 0a 2a 2a 20 extra Parse*.**
1debf 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 6e 6f 74 argument and not
1dec0 69 66 69 65 73 20 74 68 65 20 61 73 73 6f 63 69 ifies the associ
1dec1 61 74 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 ated connection
1dec2 6f 62 6a 65 63 74 20 69 66 20 6d 61 6c 6c 6f 63 object if malloc
1dec3 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 fails..*/.SQLIT
1dec4 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
1dec5 73 71 6c 69 74 65 33 50 45 78 70 72 28 0a 20 20 sqlite3PExpr(.
1dec6 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
1dec7 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 /* Parsi
1dec8 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
1dec9 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 int op,
1deca 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 /* Expre
1decb 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a ssion opcode */.
1decc 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 Expr *pLeft,
1decd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 /* Lef
1dece 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 t operand */. E
1decf 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 xpr *pRight,
1ded0 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 /* Right
1ded1 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e operand */. con
1ded2 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e st Token *pToken
1ded3 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
1ded4 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 token */.){. E
1ded5 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 xpr *p = sqlite3
1ded6 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64 62 2c Expr(pParse->db,
1ded7 20 6f 70 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 op, pLeft, pRig
1ded8 68 74 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 ht, pToken);. i
1ded9 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 f( p ){. sqli
1deda 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 te3ExprCheckHeig
1dedb 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 6e 48 ht(pParse, p->nH
1dedc 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 72 65 eight);. }. re
1dedd 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
1dede 20 57 68 65 6e 20 64 6f 69 6e 67 20 61 20 6e 65 When doing a ne
1dedf 73 74 65 64 20 70 61 72 73 65 2c 20 79 6f 75 20 sted parse, you
1dee0 63 61 6e 20 69 6e 63 6c 75 64 65 20 74 65 72 6d can include term
1dee1 73 20 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 s in an expressi
1dee2 6f 6e 0a 2a 2a 20 74 68 61 74 20 6c 6f 6f 6b 20 on.** that look
1dee3 6c 69 6b 65 20 74 68 69 73 3a 20 20 20 23 31 20 like this: #1
1dee4 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20 74 65 #2 ... These te
1dee5 72 6d 73 20 72 65 66 65 72 20 74 6f 20 72 65 67 rms refer to reg
1dee6 69 73 74 65 72 73 0a 2a 2a 20 69 6e 20 74 68 65 isters.** in the
1dee7 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1dee8 2e 20 20 23 4e 20 69 73 20 74 68 65 20 4e 2d 74 . #N is the N-t
1dee9 68 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a h register..**.*
1deea 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1deeb 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 s called by the
1deec 70 61 72 73 65 72 20 74 6f 20 64 65 61 6c 20 77 parser to deal w
1deed 69 74 68 20 6f 6e 20 6f 66 20 74 68 6f 73 65 20 ith on of those
1deee 74 65 72 6d 73 2e 0a 2a 2a 20 49 74 20 69 6d 6d terms..** It imm
1deef 65 64 69 61 74 65 6c 79 20 67 65 6e 65 72 61 74 ediately generat
1def0 65 73 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 es code to store
1def1 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 61 20 the value in a
1def2 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 2e memory location.
1def3 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 73 20 .** The returns
1def4 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 an expression th
1def5 61 74 20 77 69 6c 6c 20 63 6f 64 65 20 74 6f 20 at will code to
1def6 65 78 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 extract the valu
1def7 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 61 74 20 6d e from.** that m
1def8 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 61 emory location a
1def9 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c s needed..*/.SQL
1defa 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
1defb 20 2a 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 *sqlite3Registe
1defc 72 45 78 70 72 28 50 61 72 73 65 20 2a 70 50 61 rExpr(Parse *pPa
1defd 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b rse, Token *pTok
1defe 65 6e 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d en){. Vdbe *v =
1deff 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a pParse->pVdbe;.
1df00 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 69 66 28 Expr *p;. if(
1df01 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d pParse->nested=
1df02 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1df03 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1df04 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20 , "near \"%T\":
1df05 73 79 6e 74 61 78 20 65 72 72 6f 72 22 2c 20 70 syntax error", p
1df06 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 72 65 74 75 Token);. retu
1df07 72 6e 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 rn sqlite3PExpr(
1df08 70 50 61 72 73 65 2c 20 54 4b 5f 4e 55 4c 4c 2c pParse, TK_NULL,
1df09 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 0, 0, 0);. }.
1df0a 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 if( v==0 ) retu
1df0b 72 6e 20 30 3b 0a 20 20 70 20 3d 20 73 71 6c 69 rn 0;. p = sqli
1df0c 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c te3PExpr(pParse,
1df0d 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 30 2c TK_REGISTER, 0,
1df0e 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 0, pToken);. i
1df0f 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 f( p==0 ){. r
1df10 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4d 61 6c eturn 0; /* Mal
1df11 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 loc failed */.
1df12 7d 0a 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 }. p->iTable =
1df13 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70 54 6f atoi((char*)&pTo
1df14 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20 72 65 ken->z[1]);. re
1df15 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
1df16 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72 65 73 Join two expres
1df17 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e 20 41 sions using an A
1df18 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20 49 66 ND operator. If
1df19 20 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69 either expressi
1df1a 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 on is.** NULL, t
1df1b 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e 20 hen just return
1df1c 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 65 73 the other expres
1df1d 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f sion..*/.SQLITE_
1df1e 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
1df1f 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c lite3ExprAnd(sql
1df20 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a ite3 *db, Expr *
1df21 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70 52 69 pLeft, Expr *pRi
1df22 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c 65 66 ght){. if( pLef
1df23 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 t==0 ){. retu
1df24 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d 65 6c rn pRight;. }el
1df25 73 65 20 69 66 28 20 70 52 69 67 68 74 3d 3d 30 se if( pRight==0
1df26 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ){. return p
1df27 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Left;. }else{.
1df28 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 return sqlite
1df29 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 41 4e 44 3Expr(db, TK_AND
1df2a 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c , pLeft, pRight,
1df2b 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 0);. }.}../*.*
1df2c 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e 73 * Set the Expr.s
1df2d 70 61 6e 20 66 69 65 6c 64 20 6f 66 20 74 68 65 pan field of the
1df2e 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f given expressio
1df2f 6e 20 74 6f 20 73 70 61 6e 20 61 6c 6c 0a 2a 2a n to span all.**
1df30 20 74 65 78 74 20 62 65 74 77 65 65 6e 20 74 68 text between th
1df31 65 20 74 77 6f 20 67 69 76 65 6e 20 74 6f 6b 65 e two given toke
1df32 6e 73 2e 20 20 42 6f 74 68 20 74 6f 6b 65 6e 73 ns. Both tokens
1df33 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e must be pointin
1df34 67 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 6d 65 g.** at the same
1df35 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 string..*/.SQLI
1df36 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1df37 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 sqlite3ExprSpan(
1df38 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54 6f 6b Expr *pExpr, Tok
1df39 65 6e 20 2a 70 4c 65 66 74 2c 20 54 6f 6b 65 6e en *pLeft, Token
1df3a 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61 73 73 *pRight){. ass
1df3b 65 72 74 28 20 70 52 69 67 68 74 21 3d 30 20 29 ert( pRight!=0 )
1df3c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 ;. assert( pLef
1df3d 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 45 t!=0 );. if( pE
1df3e 78 70 72 20 29 7b 0a 20 20 20 20 70 45 78 70 72 xpr ){. pExpr
1df3f 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70 4c 65 66 74 ->span.z = pLeft
1df40 2d 3e 7a 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e ->z;. pExpr->
1df41 73 70 61 6e 2e 6e 20 3d 20 70 52 69 67 68 74 2d span.n = pRight-
1df42 3e 6e 20 2b 20 28 70 52 69 67 68 74 2d 3e 7a 20 >n + (pRight->z
1df43 2d 20 70 4c 65 66 74 2d 3e 7a 29 3b 0a 20 20 7d - pLeft->z);. }
1df44 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 .}../*.** Constr
1df45 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 uct a new expres
1df46 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 sion node for a
1df47 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6d 75 function with mu
1df48 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 ltiple.** argume
1df49 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nts..*/.SQLITE_P
1df4a 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
1df4b 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e ite3ExprFunction
1df4c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1df4d 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c ExprList *pList,
1df4e 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b Token *pToken){
1df4f 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 . Expr *pNew;.
1df50 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1df51 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 Parse->db;. ass
1df52 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b 0a 20 ert( pToken );.
1df53 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 pNew = sqlite3D
1df54 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
1df55 73 69 7a 65 6f 66 28 45 78 70 72 29 20 29 3b 0a sizeof(Expr) );.
1df56 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b if( pNew==0 ){
1df57 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 . sqlite3Expr
1df58 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
1df59 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69 64 20 List); /* Avoid
1df5a 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 77 leaking memory w
1df5b 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 hen malloc fails
1df5c 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 */. return 0
1df5d 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 6f 70 ;. }. pNew->op
1df5e 20 3d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3b 0a = TK_FUNCTION;.
1df5f 20 20 70 4e 65 77 2d 3e 70 4c 69 73 74 20 3d 20 pNew->pList =
1df60 70 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28 pList;. assert(
1df61 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d 30 20 pToken->dyn==0
1df62 29 3b 0a 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e );. pNew->token
1df63 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 70 4e = *pToken;. pN
1df64 65 77 2d 3e 73 70 61 6e 20 3d 20 70 4e 65 77 2d ew->span = pNew-
1df65 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 73 71 6c 69 74 >token;.. sqlit
1df66 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 e3ExprSetHeight(
1df67 70 50 61 72 73 65 2c 20 70 4e 65 77 29 3b 0a 20 pParse, pNew);.
1df68 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
1df69 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 61 20 ./*.** Assign a
1df6a 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 variable number
1df6b 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e to an expression
1df6c 20 74 68 61 74 20 65 6e 63 6f 64 65 73 20 61 20 that encodes a
1df6d 77 69 6c 64 63 61 72 64 0a 2a 2a 20 69 6e 20 74 wildcard.** in t
1df6e 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 he original SQL
1df6f 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a statement. .**.
1df70 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 63 6f 6e ** Wildcards con
1df71 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 69 6e sisting of a sin
1df72 67 6c 65 20 22 3f 22 20 61 72 65 20 61 73 73 69 gle "?" are assi
1df73 67 6e 65 64 20 74 68 65 20 6e 65 78 74 20 73 65 gned the next se
1df74 71 75 65 6e 74 69 61 6c 0a 2a 2a 20 76 61 72 69 quential.** vari
1df75 61 62 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a able number..**.
1df76 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 ** Wildcards of
1df77 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 the form "?nnn"
1df78 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 are assigned the
1df79 20 6e 75 6d 62 65 72 20 22 6e 6e 6e 22 2e 20 20 number "nnn".
1df7a 57 65 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20 We make.** sure
1df7b 22 6e 6e 6e 22 20 69 73 20 6e 6f 74 20 74 6f 6f "nnn" is not too
1df7c 20 62 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 be to avoid a d
1df7d 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 enial of service
1df7e 20 61 74 74 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 attack when.**
1df7f 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
1df80 74 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 6e 20 t comes from an
1df81 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 2e external source.
1df82 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 .**.** Wildcards
1df83 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3a 61 of the form ":a
1df84 61 61 22 20 6f 72 20 22 24 61 61 61 22 20 61 72 aa" or "$aaa" ar
1df85 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 73 e assigned the s
1df86 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 61 73 ame number.** as
1df87 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e the previous in
1df88 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61 stance of the sa
1df89 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 4f 72 me wildcard. Or
1df8a 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 if this is the
1df8b 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 first.** instanc
1df8c 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 61 72 e of the wildcar
1df8d 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 d, the next sequ
1df8e 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 20 6e enial variable n
1df8f 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 73 69 umber is.** assi
1df90 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f gned..*/.SQLITE_
1df91 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1df92 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 ite3ExprAssignVa
1df93 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 2a 70 rNumber(Parse *p
1df94 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 Parse, Expr *pEx
1df95 70 72 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 pr){. Token *pT
1df96 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 oken;. sqlite3
1df97 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1df98 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d ;.. if( pExpr==
1df99 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 54 0 ) return;. pT
1df9a 6f 6b 65 6e 20 3d 20 26 70 45 78 70 72 2d 3e 74 oken = &pExpr->t
1df9b 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 oken;. assert(
1df9c 70 54 6f 6b 65 6e 2d 3e 6e 3e 3d 31 20 29 3b 0a pToken->n>=1 );.
1df9d 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e assert( pToken
1df9e 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ->z!=0 );. asse
1df9f 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d rt( pToken->z[0]
1dfa0 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 54 6f !=0 );. if( pTo
1dfa1 6b 65 6e 2d 3e 6e 3d 3d 31 20 29 7b 0a 20 20 20 ken->n==1 ){.
1dfa2 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20 /* Wildcard of
1dfa3 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20 20 41 the form "?". A
1dfa4 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74 20 76 ssign the next v
1dfa5 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a ariable number *
1dfa6 2f 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 /. pExpr->iTa
1dfa7 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e ble = ++pParse->
1dfa8 6e 56 61 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66 nVar;. }else if
1dfa9 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d 3d 3d ( pToken->z[0]==
1dfaa 27 3f 27 20 29 7b 0a 20 20 20 20 2f 2a 20 57 69 '?' ){. /* Wi
1dfab 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 66 6f ldcard of the fo
1dfac 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76 rm "?nnn". Conv
1dfad 65 72 74 20 22 6e 6e 6e 22 20 74 6f 20 61 6e 20 ert "nnn" to an
1dfae 69 6e 74 65 67 65 72 20 61 6e 64 0a 20 20 20 20 integer and.
1dfaf 2a 2a 20 75 73 65 20 69 74 20 61 73 20 74 68 65 ** use it as the
1dfb0 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 variable number
1dfb1 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 */. int i;.
1dfb2 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1dfb3 20 3d 20 69 20 3d 20 61 74 6f 69 28 28 63 68 61 = i = atoi((cha
1dfb4 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d r*)&pToken->z[1]
1dfb5 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
1dfb6 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 i==0 );. tes
1dfb7 74 63 61 73 65 28 20 69 3d 3d 31 20 29 3b 0a 20 tcase( i==1 );.
1dfb8 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d testcase( i==
1dfb9 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1dfba 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 E_LIMIT_VARIABLE
1dfbb 5f 4e 55 4d 42 45 52 5d 2d 31 20 29 3b 0a 20 20 _NUMBER]-1 );.
1dfbc 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 64 testcase( i==d
1dfbd 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
1dfbe 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f _LIMIT_VARIABLE_
1dfbf 4e 55 4d 42 45 52 5d 20 29 3b 0a 20 20 20 20 69 NUMBER] );. i
1dfc0 66 28 20 69 3c 31 20 7c 7c 20 69 3e 64 62 2d 3e f( i<1 || i>db->
1dfc1 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1dfc2 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
1dfc3 42 45 52 5d 20 29 7b 0a 20 20 20 20 20 20 73 71 BER] ){. sq
1dfc4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1dfc5 61 72 73 65 2c 20 22 76 61 72 69 61 62 6c 65 20 arse, "variable
1dfc6 6e 75 6d 62 65 72 20 6d 75 73 74 20 62 65 20 62 number must be b
1dfc7 65 74 77 65 65 6e 20 3f 31 20 61 6e 64 20 3f 25 etween ?1 and ?%
1dfc8 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 64 62 d",. db
1dfc9 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
1dfca 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e LIMIT_VARIABLE_N
1dfcb 55 4d 42 45 52 5d 29 3b 0a 20 20 20 20 7d 0a 20 UMBER]);. }.
1dfcc 20 20 20 69 66 28 20 69 3e 70 50 61 72 73 65 2d if( i>pParse-
1dfcd 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 20 20 70 >nVar ){. p
1dfce 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 69 3b Parse->nVar = i;
1dfcf 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
1dfd0 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 73 /* Wildcards
1dfd1 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3a 61 of the form ":a
1dfd2 61 61 22 20 6f 72 20 22 24 61 61 61 22 2e 20 20 aa" or "$aaa".
1dfd3 52 65 75 73 65 20 74 68 65 20 73 61 6d 65 20 76 Reuse the same v
1dfd4 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 6e ariable. ** n
1dfd5 75 6d 62 65 72 20 61 73 20 74 68 65 20 70 72 69 umber as the pri
1dfd6 6f 72 20 61 70 70 65 61 72 61 6e 63 65 20 6f 66 or appearance of
1dfd7 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 the same name,
1dfd8 6f 72 20 69 66 20 74 68 65 20 6e 61 6d 65 0a 20 or if the name.
1dfd9 20 20 20 2a 2a 20 68 61 73 20 6e 65 76 65 72 20 ** has never
1dfda 61 70 70 65 61 72 65 64 20 62 65 66 6f 72 65 2c appeared before,
1dfdb 20 72 65 75 73 65 20 74 68 65 20 73 61 6d 65 20 reuse the same
1dfdc 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 0a variable number.
1dfdd 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 */. int i
1dfde 3b 0a 20 20 20 20 75 33 32 20 6e 3b 0a 20 20 20 ;. u32 n;.
1dfdf 20 6e 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e 3b 0a n = pToken->n;.
1dfe0 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
1dfe1 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3b Parse->nVarExpr;
1dfe2 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 i++){. Exp
1dfe3 72 20 2a 70 45 3b 0a 20 20 20 20 20 20 69 66 28 r *pE;. if(
1dfe4 20 28 70 45 20 3d 20 70 50 61 72 73 65 2d 3e 61 (pE = pParse->a
1dfe5 70 56 61 72 45 78 70 72 5b 69 5d 29 21 3d 30 0a pVarExpr[i])!=0.
1dfe6 20 20 20 20 20 20 20 20 20 20 26 26 20 70 45 2d && pE-
1dfe7 3e 74 6f 6b 65 6e 2e 6e 3d 3d 6e 0a 20 20 20 20 >token.n==n.
1dfe8 20 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 && memcmp(
1dfe9 70 45 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 54 6f pE->token.z, pTo
1dfea 6b 65 6e 2d 3e 7a 2c 20 6e 29 3d 3d 30 20 29 7b ken->z, n)==0 ){
1dfeb 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e . pExpr->
1dfec 69 54 61 62 6c 65 20 3d 20 70 45 2d 3e 69 54 61 iTable = pE->iTa
1dfed 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 62 72 65 ble;. bre
1dfee 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1dfef 7d 0a 20 20 20 20 69 66 28 20 69 3e 3d 70 50 61 }. if( i>=pPa
1dff0 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 20 29 7b rse->nVarExpr ){
1dff1 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 . pExpr->iT
1dff2 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d able = ++pParse-
1dff3 3e 6e 56 61 72 3b 0a 20 20 20 20 20 20 69 66 28 >nVar;. if(
1dff4 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 pParse->nVarExp
1dff5 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 r>=pParse->nVarE
1dff6 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20 xprAlloc-1 ){.
1dff7 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 pParse->nV
1dff8 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b 3d 20 70 arExprAlloc += p
1dff9 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 Parse->nVarExprA
1dffa 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 20 20 20 lloc + 10;.
1dffb 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 pParse->apVar
1dffc 45 78 70 72 20 3d 0a 20 20 20 20 20 20 20 20 20 Expr =.
1dffd 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c sqlite3DbReal
1dffe 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20 20 20 20 locOrFree(.
1dfff 20 20 20 20 20 20 20 20 20 64 62 2c 0a 20 20 20 db,.
1e000 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 pPars
1e001 65 2d 3e 61 70 56 61 72 45 78 70 72 2c 0a 20 20 e->apVarExpr,.
1e002 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 pPar
1e003 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f se->nVarExprAllo
1e004 63 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d c*sizeof(pParse-
1e005 3e 61 70 56 61 72 45 78 70 72 5b 30 5d 29 0a 20 >apVarExpr[0]).
1e006 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 );.
1e007 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1e008 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 !db->mallocFaile
1e009 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 d ){. ass
1e00a 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70 56 ert( pParse->apV
1e00b 61 72 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 arExpr!=0 );.
1e00c 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 pParse->apV
1e00d 61 72 45 78 70 72 5b 70 50 61 72 73 65 2d 3e 6e arExpr[pParse->n
1e00e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20 70 45 78 VarExpr++] = pEx
1e00f 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 pr;. }.
1e010 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20 21 70 50 }. } . if( !pP
1e011 61 72 73 65 2d 3e 6e 45 72 72 20 26 26 20 70 50 arse->nErr && pP
1e012 61 72 73 65 2d 3e 6e 56 61 72 3e 64 62 2d 3e 61 arse->nVar>db->a
1e013 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1e014 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 IT_VARIABLE_NUMB
1e015 45 52 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ER] ){. sqlit
1e016 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1e017 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 53 51 4c e, "too many SQL
1e018 20 76 61 72 69 61 62 6c 65 73 22 29 3b 0a 20 20 variables");.
1e019 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 }.}../*.** Clear
1e01a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 an expression s
1e01b 74 72 75 63 74 75 72 65 20 77 69 74 68 6f 75 74 tructure without
1e01c 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 73 74 deleting the st
1e01d 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a ructure itself..
1e01e 2a 2a 20 53 75 62 73 74 72 75 63 74 75 72 65 20 ** Substructure
1e01f 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 is deleted..*/.S
1e020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1e021 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c id sqlite3ExprCl
1e022 65 61 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ear(sqlite3 *db,
1e023 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 Expr *p){. if(
1e024 20 70 2d 3e 73 70 61 6e 2e 64 79 6e 20 29 20 73 p->span.dyn ) s
1e025 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1e026 20 28 63 68 61 72 2a 29 70 2d 3e 73 70 61 6e 2e (char*)p->span.
1e027 7a 29 3b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6b z);. if( p->tok
1e028 65 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33 en.dyn ) sqlite3
1e029 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 61 72 DbFree(db, (char
1e02a 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 20 *)p->token.z);.
1e02b 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
1e02c 74 65 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29 te(db, p->pLeft)
1e02d 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 ;. sqlite3ExprD
1e02e 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 69 elete(db, p->pRi
1e02f 67 68 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 ght);. sqlite3E
1e030 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 xprListDelete(db
1e031 2c 20 70 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 73 , p->pList);. s
1e032 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
1e033 74 65 28 64 62 2c 20 70 2d 3e 70 53 65 6c 65 63 te(db, p->pSelec
1e034 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 t);.}../*.** Rec
1e035 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65 20 ursively delete
1e036 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 an expression tr
1e037 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ee..*/.SQLITE_PR
1e038 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1e039 65 33 45 78 70 72 44 65 6c 65 74 65 28 73 71 6c e3ExprDelete(sql
1e03a 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a ite3 *db, Expr *
1e03b 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 p){. if( p==0 )
1e03c 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 return;. sqlit
1e03d 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 2c 20 e3ExprClear(db,
1e03e 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 p);. sqlite3DbF
1e03f 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f ree(db, p);.}../
1e040 2a 0a 2a 2a 20 54 68 65 20 45 78 70 72 2e 74 6f *.** The Expr.to
1e041 6b 65 6e 20 66 69 65 6c 64 20 6d 69 67 68 74 20 ken field might
1e042 62 65 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 be a string lite
1e043 72 61 6c 20 74 68 61 74 20 69 73 20 71 75 6f 74 ral that is quot
1e044 65 64 2e 0a 2a 2a 20 49 66 20 73 6f 2c 20 72 65 ed..** If so, re
1e045 6d 6f 76 65 20 74 68 65 20 71 75 6f 74 61 74 69 move the quotati
1e046 6f 6e 20 6d 61 72 6b 73 2e 0a 2a 2f 0a 53 51 4c on marks..*/.SQL
1e047 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1e048 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 45 sqlite3DequoteE
1e049 78 70 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c xpr(sqlite3 *db,
1e04a 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 Expr *p){. if(
1e04b 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 ExprHasAnyPrope
1e04c 72 74 79 28 70 2c 20 45 50 5f 44 65 71 75 6f 74 rty(p, EP_Dequot
1e04d 65 64 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ed) ){. retur
1e04e 6e 3b 0a 20 20 7d 0a 20 20 45 78 70 72 53 65 74 n;. }. ExprSet
1e04f 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 44 Property(p, EP_D
1e050 65 71 75 6f 74 65 64 29 3b 0a 20 20 69 66 28 20 equoted);. if(
1e051 70 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20 p->token.dyn==0
1e052 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 6f ){. sqlite3To
1e053 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 2d 3e kenCopy(db, &p->
1e054 74 6f 6b 65 6e 2c 20 26 70 2d 3e 74 6f 6b 65 6e token, &p->token
1e055 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
1e056 44 65 71 75 6f 74 65 28 28 63 68 61 72 2a 29 70 Dequote((char*)p
1e057 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 7d 0a 0a 2f ->token.z);.}../
1e058 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
1e059 6e 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 ng group of rout
1e05a 69 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20 63 ines make deep c
1e05b 6f 70 69 65 73 20 6f 66 20 65 78 70 72 65 73 73 opies of express
1e05c 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73 ions,.** express
1e05d 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c 69 ion lists, ID li
1e05e 73 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74 20 sts, and select
1e05f 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 statements. The
1e060 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62 copies can.** b
1e061 65 20 64 65 6c 65 74 65 64 20 28 62 79 20 62 65 e deleted (by be
1e062 69 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74 68 ing passed to th
1e063 65 69 72 20 72 65 73 70 65 63 74 69 76 65 20 2e eir respective .
1e064 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74 69 ..Delete() routi
1e065 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20 nes).** without
1e066 65 66 66 65 63 74 69 6e 67 20 74 68 65 20 6f 72 effecting the or
1e067 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 iginals..**.** T
1e068 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 he expression li
1e069 73 74 2c 20 49 44 2c 20 61 6e 64 20 73 6f 75 72 st, ID, and sour
1e06a 63 65 20 6c 69 73 74 73 20 72 65 74 75 72 6e 20 ce lists return
1e06b 62 79 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 by sqlite3ExprLi
1e06c 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 stDup(),.** sqli
1e06d 74 65 33 49 64 4c 69 73 74 44 75 70 28 29 2c 20 te3IdListDup(),
1e06e 61 6e 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 and sqlite3SrcLi
1e06f 73 74 44 75 70 28 29 20 63 61 6e 20 6e 6f 74 20 stDup() can not
1e070 62 65 20 66 75 72 74 68 65 72 20 65 78 70 61 6e be further expan
1e071 64 65 64 20 0a 2a 2a 20 62 79 20 73 75 62 73 65 ded .** by subse
1e072 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 quent calls to s
1e073 71 6c 69 74 65 2a 4c 69 73 74 41 70 70 65 6e 64 qlite*ListAppend
1e074 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a () routines..**.
1e075 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73 20 74 68 ** Any tables th
1e076 61 74 20 74 68 65 20 53 72 63 4c 69 73 74 20 6d at the SrcList m
1e077 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 72 ight point to ar
1e078 65 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 e not duplicated
1e079 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1e07a 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
1e07b 33 45 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 3ExprDup(sqlite3
1e07c 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a *db, Expr *p){.
1e07d 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 Expr *pNew;.
1e07e 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
1e07f 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 n 0;. pNew = sq
1e080 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
1e081 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 (db, sizeof(*p)
1e082 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 );. if( pNew==0
1e083 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d ) return 0;. m
1e084 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20 73 emcpy(pNew, p, s
1e085 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 izeof(*pNew));.
1e086 20 69 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 21 if( p->token.z!
1e087 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e =0 ){. pNew->
1e088 74 6f 6b 65 6e 2e 7a 20 3d 20 28 75 38 2a 29 73 token.z = (u8*)s
1e089 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
1e08a 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 74 6f db, (char*)p->to
1e08b 6b 65 6e 2e 7a 2c 20 70 2d 3e 74 6f 6b 65 6e 2e ken.z, p->token.
1e08c 6e 29 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 74 6f n);. pNew->to
1e08d 6b 65 6e 2e 64 79 6e 20 3d 20 31 3b 0a 20 20 7d ken.dyn = 1;. }
1e08e 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
1e08f 28 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 3d ( pNew->token.z=
1e090 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 =0 );. }. pNew
1e091 2d 3e 73 70 61 6e 2e 7a 20 3d 20 30 3b 0a 20 20 ->span.z = 0;.
1e092 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 73 71 pNew->pLeft = sq
1e093 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
1e094 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 4e p->pLeft);. pN
1e095 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c ew->pRight = sql
1e096 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 ite3ExprDup(db,
1e097 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 70 4e p->pRight);. pN
1e098 65 77 2d 3e 70 4c 69 73 74 20 3d 20 73 71 6c 69 ew->pList = sqli
1e099 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 te3ExprListDup(d
1e09a 62 2c 20 70 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 b, p->pList);.
1e09b 70 4e 65 77 2d 3e 70 53 65 6c 65 63 74 20 3d 20 pNew->pSelect =
1e09c 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 sqlite3SelectDup
1e09d 28 64 62 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29 (db, p->pSelect)
1e09e 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b ;. return pNew;
1e09f 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
1e0a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 6f E void sqlite3To
1e0a1 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74 65 33 20 kenCopy(sqlite3
1e0a2 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c *db, Token *pTo,
1e0a3 20 54 6f 6b 65 6e 20 2a 70 46 72 6f 6d 29 7b 0a Token *pFrom){.
1e0a4 20 20 69 66 28 20 70 54 6f 2d 3e 64 79 6e 20 29 if( pTo->dyn )
1e0a5 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1e0a6 62 2c 20 28 63 68 61 72 2a 29 70 54 6f 2d 3e 7a b, (char*)pTo->z
1e0a7 29 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e );. if( pFrom->
1e0a8 7a 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 6e 20 z ){. pTo->n
1e0a9 3d 20 70 46 72 6f 6d 2d 3e 6e 3b 0a 20 20 20 20 = pFrom->n;.
1e0aa 70 54 6f 2d 3e 7a 20 3d 20 28 75 38 2a 29 73 71 pTo->z = (u8*)sq
1e0ab 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 lite3DbStrNDup(d
1e0ac 62 2c 20 28 63 68 61 72 2a 29 70 46 72 6f 6d 2d b, (char*)pFrom-
1e0ad 3e 7a 2c 20 70 46 72 6f 6d 2d 3e 6e 29 3b 0a 20 >z, pFrom->n);.
1e0ae 20 20 20 70 54 6f 2d 3e 64 79 6e 20 3d 20 31 3b pTo->dyn = 1;
1e0af 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 . }else{. pT
1e0b0 6f 2d 3e 7a 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a o->z = 0;. }.}.
1e0b1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 SQLITE_PRIVATE E
1e0b2 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 xprList *sqlite3
1e0b3 45 78 70 72 4c 69 73 74 44 75 70 28 73 71 6c 69 ExprListDup(sqli
1e0b4 74 65 33 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 te3 *db, ExprLis
1e0b5 74 20 2a 70 29 7b 0a 20 20 45 78 70 72 4c 69 73 t *p){. ExprLis
1e0b6 74 20 2a 70 4e 65 77 3b 0a 20 20 73 74 72 75 63 t *pNew;. struc
1e0b7 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
1e0b8 2a 70 49 74 65 6d 2c 20 2a 70 4f 6c 64 49 74 65 *pItem, *pOldIte
1e0b9 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 m;. int i;. if
1e0ba 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
1e0bb 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 0;. pNew = sqli
1e0bc 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
1e0bd 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 b, sizeof(*pNew)
1e0be 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d );. if( pNew==
1e0bf 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
1e0c0 70 4e 65 77 2d 3e 69 45 43 75 72 73 6f 72 20 3d pNew->iECursor =
1e0c1 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45 78 70 0;. pNew->nExp
1e0c2 72 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 r = pNew->nAlloc
1e0c3 20 3d 20 70 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 = p->nExpr;. p
1e0c4 4e 65 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20 3d New->a = pItem =
1e0c5 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1e0c6 52 61 77 28 64 62 2c 20 20 70 2d 3e 6e 45 78 70 Raw(db, p->nExp
1e0c7 72 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d r*sizeof(p->a[0]
1e0c8 29 20 29 3b 0a 20 20 69 66 28 20 70 49 74 65 6d ) );. if( pItem
1e0c9 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
1e0ca 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 e3DbFree(db, pNe
1e0cb 77 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 w);. return 0
1e0cc 3b 0a 20 20 7d 20 0a 20 20 70 4f 6c 64 49 74 65 ;. } . pOldIte
1e0cd 6d 20 3d 20 70 2d 3e 61 3b 0a 20 20 66 6f 72 28 m = p->a;. for(
1e0ce 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b i=0; i<p->nExpr;
1e0cf 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 2c 20 70 i++, pItem++, p
1e0d0 4f 6c 64 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 OldItem++){.
1e0d1 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 2c 20 Expr *pNewExpr,
1e0d2 2a 70 4f 6c 64 45 78 70 72 3b 0a 20 20 20 20 70 *pOldExpr;. p
1e0d3 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 4e Item->pExpr = pN
1e0d4 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 ewExpr = sqlite3
1e0d5 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 ExprDup(db, pOld
1e0d6 45 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d Expr = pOldItem-
1e0d7 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28 >pExpr);. if(
1e0d8 20 70 4f 6c 64 45 78 70 72 2d 3e 73 70 61 6e 2e pOldExpr->span.
1e0d9 7a 21 3d 30 20 26 26 20 70 4e 65 77 45 78 70 72 z!=0 && pNewExpr
1e0da 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6c 77 ){. /* Alw
1e0db 61 79 73 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 ays make a copy
1e0dc 6f 66 20 74 68 65 20 73 70 61 6e 20 66 6f 72 20 of the span for
1e0dd 74 6f 70 2d 6c 65 76 65 6c 20 65 78 70 72 65 73 top-level expres
1e0de 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 20 20 20 sions in the.
1e0df 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e ** expression
1e0e0 20 6c 69 73 74 2e 20 20 54 68 65 20 6c 6f 67 69 list. The logi
1e0e1 63 20 69 6e 20 53 45 4c 45 43 54 20 70 72 6f 63 c in SELECT proc
1e0e2 65 73 73 69 6e 67 20 74 68 61 74 20 64 65 74 65 essing that dete
1e0e3 72 6d 69 6e 65 73 0a 20 20 20 20 20 20 2a 2a 20 rmines. **
1e0e4 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 63 6f 6c the names of col
1e0e5 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 umns in the resu
1e0e6 6c 74 20 73 65 74 20 6e 65 65 64 73 20 74 68 69 lt set needs thi
1e0e7 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f s information */
1e0e8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 6f . sqlite3To
1e0e9 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 4e 65 kenCopy(db, &pNe
1e0ea 77 45 78 70 72 2d 3e 73 70 61 6e 2c 20 26 70 4f wExpr->span, &pO
1e0eb 6c 64 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 ldExpr->span);.
1e0ec 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1e0ed 20 70 4e 65 77 45 78 70 72 3d 3d 30 20 7c 7c 20 pNewExpr==0 ||
1e0ee 70 4e 65 77 45 78 70 72 2d 3e 73 70 61 6e 2e 7a pNewExpr->span.z
1e0ef 21 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 20 !=0 .
1e0f0 20 7c 7c 20 70 4f 6c 64 45 78 70 72 2d 3e 73 70 || pOldExpr->sp
1e0f1 61 6e 2e 7a 3d 3d 30 0a 20 20 20 20 20 20 20 20 an.z==0.
1e0f2 20 20 20 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f || db->mallo
1e0f3 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 70 cFailed );. p
1e0f4 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 Item->zName = sq
1e0f5 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
1e0f6 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d , pOldItem->zNam
1e0f7 65 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73 e);. pItem->s
1e0f8 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49 ortOrder = pOldI
1e0f9 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a tem->sortOrder;.
1e0fa 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 pItem->done
1e0fb 3d 20 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e = 0;. pItem->
1e0fc 69 43 6f 6c 20 3d 20 70 4f 6c 64 49 74 65 6d 2d iCol = pOldItem-
1e0fd 3e 69 43 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d >iCol;. pItem
1e0fe 2d 3e 69 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49 ->iAlias = pOldI
1e0ff 74 65 6d 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d tem->iAlias;. }
1e100 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
1e101 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 }../*.** If curs
1e102 6f 72 73 2c 20 74 72 69 67 67 65 72 73 2c 20 76 ors, triggers, v
1e103 69 65 77 73 20 61 6e 64 20 73 75 62 71 75 65 72 iews and subquer
1e104 69 65 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 ies are all omit
1e105 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 ted from.** the
1e106 62 75 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 build, then none
1e107 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
1e108 67 20 72 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 g routines, exce
1e109 70 74 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 pt for .** sqlit
1e10a 65 33 53 65 6c 65 63 74 44 75 70 28 29 2c 20 63 e3SelectDup(), c
1e10b 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 an be called. sq
1e10c 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 29 lite3SelectDup()
1e10d 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a is sometimes.**
1e10e 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e called with a N
1e10f 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f ULL argument..*/
1e110 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
1e111 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
1e112 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
1e113 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 TE_OMIT_TRIGGER)
1e114 20 5c 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 \. || !defined(
1e115 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
1e116 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 UERY).SQLITE_PRI
1e117 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 VATE SrcList *sq
1e118 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 lite3SrcListDup(
1e119 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63 sqlite3 *db, Src
1e11a 4c 69 73 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c List *p){. SrcL
1e11b 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 ist *pNew;. int
1e11c 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b i;. int nByte;
1e11d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
1e11e 74 75 72 6e 20 30 3b 0a 20 20 6e 42 79 74 65 20 turn 0;. nByte
1e11f 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20 28 = sizeof(*p) + (
1e120 70 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69 7a 65 p->nSrc>0 ? size
1e121 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 28 70 of(p->a[0]) * (p
1e122 2d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29 3b 0a ->nSrc-1) : 0);.
1e123 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
1e124 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
1e125 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 70 nByte );. if( p
1e126 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 New==0 ) return
1e127 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 53 72 63 20 0;. pNew->nSrc
1e128 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d = pNew->nAlloc =
1e129 20 70 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f 72 28 p->nSrc;. for(
1e12a 69 3d 30 3b 20 69 3c 70 2d 3e 6e 53 72 63 3b 20 i=0; i<p->nSrc;
1e12b 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 i++){. struct
1e12c 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 SrcList_item *p
1e12d 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d NewItem = &pNew-
1e12e 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63 >a[i];. struc
1e12f 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a t SrcList_item *
1e130 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 pOldItem = &p->a
1e131 5b 69 5d 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a [i];. Table *
1e132 70 54 61 62 3b 0a 20 20 20 20 70 4e 65 77 49 74 pTab;. pNewIt
1e133 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 em->zDatabase =
1e134 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
1e135 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 44 db, pOldItem->zD
1e136 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 70 4e atabase);. pN
1e137 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 ewItem->zName =
1e138 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
1e139 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e db, pOldItem->zN
1e13a 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 ame);. pNewIt
1e13b 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c em->zAlias = sql
1e13c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c ite3DbStrDup(db,
1e13d 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c 69 61 pOldItem->zAlia
1e13e 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d s);. pNewItem
1e13f 2d 3e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 4f 6c ->jointype = pOl
1e140 64 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b dItem->jointype;
1e141 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 . pNewItem->i
1e142 43 75 72 73 6f 72 20 3d 20 70 4f 6c 64 49 74 65 Cursor = pOldIte
1e143 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 m->iCursor;.
1e144 70 4e 65 77 49 74 65 6d 2d 3e 69 73 50 6f 70 75 pNewItem->isPopu
1e145 6c 61 74 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d lated = pOldItem
1e146 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3b 0a 20 ->isPopulated;.
1e147 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 49 6e pNewItem->zIn
1e148 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 62 53 dex = sqlite3DbS
1e149 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 trDup(db, pOldIt
1e14a 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 20 20 em->zIndex);.
1e14b 20 70 4e 65 77 49 74 65 6d 2d 3e 6e 6f 74 49 6e pNewItem->notIn
1e14c 64 65 78 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d dexed = pOldItem
1e14d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3b 0a 20 20 ->notIndexed;.
1e14e 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 49 6e 64 pNewItem->pInd
1e14f 65 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 ex = pOldItem->p
1e150 49 6e 64 65 78 3b 0a 20 20 20 20 70 54 61 62 20 Index;. pTab
1e151 3d 20 70 4e 65 77 49 74 65 6d 2d 3e 70 54 61 62 = pNewItem->pTab
1e152 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 54 61 = pOldItem->pTa
1e153 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 b;. if( pTab
1e154 29 7b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 6e ){. pTab->n
1e155 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 Ref++;. }.
1e156 20 70 4e 65 77 49 74 65 6d 2d 3e 70 53 65 6c 65 pNewItem->pSele
1e157 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 ct = sqlite3Sele
1e158 63 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 ctDup(db, pOldIt
1e159 65 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 em->pSelect);.
1e15a 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 pNewItem->pOn
1e15b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 = sqlite3ExprDup
1e15c 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 (db, pOldItem->p
1e15d 4f 6e 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 On);. pNewIte
1e15e 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 73 71 6c 69 m->pUsing = sqli
1e15f 74 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c te3IdListDup(db,
1e160 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 55 73 69 6e pOldItem->pUsin
1e161 67 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d g);. pNewItem
1e162 2d 3e 63 6f 6c 55 73 65 64 20 3d 20 70 4f 6c 64 ->colUsed = pOld
1e163 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 Item->colUsed;.
1e164 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }. return pNew
1e165 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
1e166 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 TE IdList *sqlit
1e167 65 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69 e3IdListDup(sqli
1e168 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 te3 *db, IdList
1e169 2a 70 29 7b 0a 20 20 49 64 4c 69 73 74 20 2a 70 *p){. IdList *p
1e16a 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 New;. int i;.
1e16b 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
1e16c 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 n 0;. pNew = sq
1e16d 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
1e16e 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 (db, sizeof(*pNe
1e16f 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 w) );. if( pNew
1e170 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
1e171 20 20 70 4e 65 77 2d 3e 6e 49 64 20 3d 20 70 4e pNew->nId = pN
1e172 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e ew->nAlloc = p->
1e173 6e 49 64 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d nId;. pNew->a =
1e174 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1e175 52 61 77 28 64 62 2c 20 70 2d 3e 6e 49 64 2a 73 Raw(db, p->nId*s
1e176 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29 izeof(p->a[0]) )
1e177 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 61 3d ;. if( pNew->a=
1e178 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1e179 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77 3DbFree(db, pNew
1e17a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
1e17b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
1e17c 69 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a i<p->nId; i++){.
1e17d 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 struct IdLis
1e17e 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d t_item *pNewItem
1e17f 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a = &pNew->a[i];.
1e180 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 struct IdLis
1e181 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d t_item *pOldItem
1e182 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 = &p->a[i];.
1e183 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 pNewItem->zName
1e184 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
1e185 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d up(db, pOldItem-
1e186 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 >zName);. pNe
1e187 77 49 74 65 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c wItem->idx = pOl
1e188 64 49 74 65 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a dItem->idx;. }.
1e189 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
1e18a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1e18b 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 Select *sqlite3S
1e18c 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 electDup(sqlite3
1e18d 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 *db, Select *p)
1e18e 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 {. Select *pNew
1e18f 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
1e190 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 eturn 0;. pNew
1e191 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
1e192 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 cRaw(db, sizeof(
1e193 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 *p) );. if( pNe
1e194 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b w==0 ) return 0;
1e195 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20 . pNew->pEList
1e196 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 = sqlite3ExprLis
1e197 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69 tDup(db, p->pELi
1e198 73 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 53 72 st);. pNew->pSr
1e199 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 c = sqlite3SrcLi
1e19a 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 53 72 stDup(db, p->pSr
1e19b 63 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 c);. pNew->pWhe
1e19c 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 re = sqlite3Expr
1e19d 44 75 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 Dup(db, p->pWher
1e19e 65 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f e);. pNew->pGro
1e19f 75 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 upBy = sqlite3Ex
1e1a0 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d prListDup(db, p-
1e1a1 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 70 4e >pGroupBy);. pN
1e1a2 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 ew->pHaving = sq
1e1a3 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
1e1a4 20 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20 p->pHaving);.
1e1a5 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d pNew->pOrderBy =
1e1a6 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
1e1a7 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 Dup(db, p->pOrde
1e1a8 72 42 79 29 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 rBy);. pNew->op
1e1a9 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 = p->op;. pNew
1e1aa 2d 3e 70 50 72 69 6f 72 20 3d 20 73 71 6c 69 74 ->pPrior = sqlit
1e1ab 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 e3SelectDup(db,
1e1ac 70 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 70 4e p->pPrior);. pN
1e1ad 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c ew->pLimit = sql
1e1ae 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 ite3ExprDup(db,
1e1af 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 70 4e p->pLimit);. pN
1e1b0 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20 73 71 ew->pOffset = sq
1e1b1 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
1e1b2 20 70 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 20 20 p->pOffset);.
1e1b3 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 pNew->iLimit = 0
1e1b4 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65 ;. pNew->iOffse
1e1b5 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73 t = 0;. pNew->s
1e1b6 65 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c elFlags = p->sel
1e1b7 46 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65 73 Flags & ~SF_Uses
1e1b8 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65 Ephemeral;. pNe
1e1b9 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 w->pRightmost =
1e1ba 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 0;. pNew->addrO
1e1bb 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b penEphm[0] = -1;
1e1bc 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 . pNew->addrOpe
1e1bd 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 nEphm[1] = -1;.
1e1be 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 pNew->addrOpenE
1e1bf 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72 phm[2] = -1;. r
1e1c0 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65 eturn pNew;.}.#e
1e1c1 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 lse.SQLITE_PRIVA
1e1c2 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 TE Select *sqlit
1e1c3 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 e3SelectDup(sqli
1e1c4 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 te3 *db, Select
1e1c5 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
1e1c6 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ==0 );. return
1e1c7 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0;.}.#endif.../*
1e1c8 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c .** Add a new el
1e1c9 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 65 6e 64 ement to the end
1e1ca 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f of an expressio
1e1cb 6e 20 6c 69 73 74 2e 20 20 49 66 20 70 4c 69 73 n list. If pLis
1e1cc 74 20 69 73 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c t is.** initiall
1e1cd 79 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 63 72 65 y NULL, then cre
1e1ce 61 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 ate a new expres
1e1cf 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 sion list..*/.SQ
1e1d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
1e1d1 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 rList *sqlite3Ex
1e1d2 70 72 4c 69 73 74 41 70 70 65 6e 64 28 0a 20 20 prListAppend(.
1e1d3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
1e1d4 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 /* Parsi
1e1d5 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
1e1d6 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c ExprList *pList,
1e1d7 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
1e1d8 74 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 65 to which to appe
1e1d9 6e 64 2e 20 4d 69 67 68 74 20 62 65 20 4e 55 4c nd. Might be NUL
1e1da 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 L */. Expr *pEx
1e1db 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f pr, /
1e1dc 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 * Expression to
1e1dd 62 65 20 61 70 70 65 6e 64 65 64 20 2a 2f 0a 20 be appended */.
1e1de 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 Token *pName
1e1df 20 20 20 20 20 20 20 20 20 2f 2a 20 41 53 20 6b /* AS k
1e1e0 65 79 77 6f 72 64 20 66 6f 72 20 74 68 65 20 65 eyword for the e
1e1e1 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a xpression */.){.
1e1e2 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1e1e3 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 pParse->db;. if
1e1e4 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 ( pList==0 ){.
1e1e5 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 pList = sqlite
1e1e6 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
1e1e7 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 4c 69 73 , sizeof(ExprLis
1e1e8 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c t) );. if( pL
1e1e9 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ist==0 ){.
1e1ea 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 goto no_mem;.
1e1eb 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
1e1ec 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 List->nAlloc==0
1e1ed 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 );. }. if( pLi
1e1ee 73 74 2d 3e 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73 st->nAlloc<=pLis
1e1ef 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 t->nExpr ){.
1e1f0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
1e1f1 69 74 65 6d 20 2a 61 3b 0a 20 20 20 20 69 6e 74 item *a;. int
1e1f2 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c n = pList->nAll
1e1f3 6f 63 2a 32 20 2b 20 34 3b 0a 20 20 20 20 61 20 oc*2 + 4;. a
1e1f4 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c = sqlite3DbReall
1e1f5 6f 63 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 2c oc(db, pList->a,
1e1f6 20 6e 2a 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d n*sizeof(pList-
1e1f7 3e 61 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 >a[0]));. if(
1e1f8 20 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 a==0 ){. g
1e1f9 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 oto no_mem;.
1e1fa 7d 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 20 3d }. pList->a =
1e1fb 20 61 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e a;. pList->n
1e1fc 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 Alloc = sqlite3D
1e1fd 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 bMallocSize(db,
1e1fe 61 29 2f 73 69 7a 65 6f 66 28 61 5b 30 5d 29 3b a)/sizeof(a[0]);
1e1ff 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
1e200 4c 69 73 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 List->a!=0 );.
1e201 69 66 28 20 70 45 78 70 72 20 7c 7c 20 70 4e 61 if( pExpr || pNa
1e202 6d 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 me ){. struct
1e203 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1e204 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e pItem = &pList->
1e205 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b a[pList->nExpr++
1e206 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49 ];. memset(pI
1e207 74 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a tem, 0, sizeof(*
1e208 70 49 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 74 pItem));. pIt
1e209 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 em->zName = sqli
1e20a 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
1e20b 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 (db, pName);.
1e20c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 pItem->pExpr =
1e20d 70 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d pExpr;. pItem
1e20e 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 ->iAlias = 0;.
1e20f 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 }. return pList
1e210 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20 20 0a ;..no_mem: .
1e211 20 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61 6b 69 /* Avoid leaki
1e212 6e 67 20 6d 65 6d 6f 72 79 20 69 66 20 6d 61 6c ng memory if mal
1e213 6c 6f 63 20 68 61 73 20 66 61 69 6c 65 64 2e 20 loc has failed.
1e214 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 */. sqlite3Expr
1e215 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 Delete(db, pExpr
1e216 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 );. sqlite3Expr
1e217 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
1e218 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 List);. return
1e219 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 0;.}../*.** If t
1e21a 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 he expression li
1e21b 73 74 20 70 45 4c 69 73 74 20 63 6f 6e 74 61 69 st pEList contai
1e21c 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20 69 4c 69 ns more than iLi
1e21d 6d 69 74 20 65 6c 65 6d 65 6e 74 73 2c 0a 2a 2a mit elements,.**
1e21e 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 leave an error
1e21f 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 message in pPars
1e220 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1e221 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1e222 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 3ExprListCheckLe
1e223 6e 67 74 68 28 0a 20 20 50 61 72 73 65 20 2a 70 ngth(. Parse *p
1e224 50 61 72 73 65 2c 0a 20 20 45 78 70 72 4c 69 73 Parse,. ExprLis
1e225 74 20 2a 70 45 4c 69 73 74 2c 0a 20 20 63 6f 6e t *pEList,. con
1e226 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a 65 63 74 st char *zObject
1e227 0a 29 7b 0a 20 20 69 6e 74 20 6d 78 20 3d 20 70 .){. int mx = p
1e228 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 Parse->db->aLimi
1e229 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 t[SQLITE_LIMIT_C
1e22a 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73 74 63 61 OLUMN];. testca
1e22b 73 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 se( pEList && pE
1e22c 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 20 List->nExpr==mx
1e22d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 );. testcase( p
1e22e 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d EList && pEList-
1e22f 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31 20 29 3b 0a >nExpr==mx+1 );.
1e230 20 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 if( pEList &&
1e231 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 6d 78 pEList->nExpr>mx
1e232 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1e233 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1e234 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e "too many column
1e235 73 20 69 6e 20 25 73 22 2c 20 7a 4f 62 6a 65 63 s in %s", zObjec
1e236 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a t);. }.}../*.**
1e237 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 Delete an entir
1e238 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 e expression lis
1e239 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
1e23a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1e23b 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 3ExprListDelete(
1e23c 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 sqlite3 *db, Exp
1e23d 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 rList *pList){.
1e23e 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
1e23f 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1e240 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 pItem;. if( pLi
1e241 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a st==0 ) return;.
1e242 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d assert( pList-
1e243 3e 61 21 3d 30 20 7c 7c 20 28 70 4c 69 73 74 2d >a!=0 || (pList-
1e244 3e 6e 45 78 70 72 3d 3d 30 20 26 26 20 70 4c 69 >nExpr==0 && pLi
1e245 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 20 29 st->nAlloc==0) )
1e246 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 ;. assert( pLis
1e247 74 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 74 2d t->nExpr<=pList-
1e248 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 6f 72 >nAlloc );. for
1e249 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c (pItem=pList->a,
1e24a 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e i=0; i<pList->n
1e24b 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
1e24c 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
1e24d 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1e24e 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 Item->pExpr);.
1e24f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1e250 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 db, pItem->zName
1e251 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
1e252 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 DbFree(db, pList
1e253 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 ->a);. sqlite3D
1e254 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 bFree(db, pList)
1e255 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 ;.}../*.** These
1e256 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 57 61 routines are Wa
1e257 6c 6b 65 72 20 63 61 6c 6c 62 61 63 6b 73 2e 20 lker callbacks.
1e258 20 57 61 6c 6b 65 72 2e 75 2e 70 69 20 69 73 20 Walker.u.pi is
1e259 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 a pointer.** to
1e25a 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 54 68 65 an integer. The
1e25b 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 se routines are
1e25c 63 68 65 63 6b 69 6e 67 20 61 6e 20 65 78 70 72 checking an expr
1e25d 65 73 73 69 6f 6e 20 74 6f 20 73 65 65 0a 2a 2a ession to see.**
1e25e 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6e 73 if it is a cons
1e25f 74 61 6e 74 2e 20 20 53 65 74 20 2a 57 61 6c 6b tant. Set *Walk
1e260 65 72 2e 75 2e 70 69 20 74 6f 20 30 20 69 66 20 er.u.pi to 0 if
1e261 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
1e262 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 73 74 61 6e s.** not constan
1e263 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 t..**.** These c
1e264 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 73 allback routines
1e265 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 are used to imp
1e266 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f lement the follo
1e267 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 wing:.**.**
1e268 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e sqlite3ExprIsCon
1e269 73 74 61 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 stant().** s
1e26a 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
1e26b 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 29 0a 2a 2a tantNotJoin().**
1e26c 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1e26d 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 IsConstantOrFunc
1e26e 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 tion().**.*/.sta
1e26f 74 69 63 20 69 6e 74 20 65 78 70 72 4e 6f 64 65 tic int exprNode
1e270 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 IsConstant(Walke
1e271 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 r *pWalker, Expr
1e272 20 2a 70 45 78 70 72 29 7b 0a 0a 20 20 2f 2a 20 *pExpr){.. /*
1e273 49 66 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 If pWalker->u.i
1e274 69 73 20 33 20 74 68 65 6e 20 61 6e 79 20 74 65 is 3 then any te
1e275 72 6d 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 rm of the expres
1e276 73 69 6f 6e 20 74 68 61 74 20 63 6f 6d 65 73 20 sion that comes
1e277 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 4f 4e from. ** the ON
1e278 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 or USING clause
1e279 73 20 6f 66 20 61 20 6a 6f 69 6e 20 64 69 73 71 s of a join disq
1e27a 75 61 6c 69 66 69 65 73 20 74 68 65 20 65 78 70 ualifies the exp
1e27b 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 66 72 6f ression. ** fro
1e27c 6d 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 m being consider
1e27d 65 64 20 63 6f 6e 73 74 61 6e 74 2e 20 2a 2f 0a ed constant. */.
1e27e 20 20 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 if( pWalker->u
1e27f 2e 69 3d 3d 33 20 26 26 20 45 78 70 72 48 61 73 .i==3 && ExprHas
1e280 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 AnyProperty(pExp
1e281 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 r, EP_FromJoin)
1e282 29 7b 0a 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e ){. pWalker->
1e283 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 u.i = 0;. ret
1e284 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1e285 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 }.. switch( pE
1e286 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 2f xpr->op ){. /
1e287 2a 20 43 6f 6e 73 69 64 65 72 20 66 75 6e 63 74 * Consider funct
1e288 69 6f 6e 73 20 74 6f 20 62 65 20 63 6f 6e 73 74 ions to be const
1e289 61 6e 74 20 69 66 20 61 6c 6c 20 74 68 65 69 72 ant if all their
1e28a 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 63 arguments are c
1e28b 6f 6e 73 74 61 6e 74 0a 20 20 20 20 2a 2a 20 61 onstant. ** a
1e28c 6e 64 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d nd pWalker->u.i=
1e28d 3d 32 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 =2 */. case T
1e28e 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 K_FUNCTION:.
1e28f 20 20 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 if( pWalker->u
1e290 2e 69 3d 3d 32 20 29 20 72 65 74 75 72 6e 20 30 .i==2 ) return 0
1e291 3b 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 ;. /* Fall
1e292 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 20 20 63 through */. c
1e293 61 73 65 20 54 4b 5f 49 44 3a 0a 20 20 20 20 63 ase TK_ID:. c
1e294 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 ase TK_COLUMN:.
1e295 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 case TK_AGG_F
1e296 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 UNCTION:. cas
1e297 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a e TK_AGG_COLUMN:
1e298 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1e299 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 OMIT_SUBQUERY.
1e29a 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 case TK_SELECT
1e29b 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 :. case TK_EX
1e29c 49 53 54 53 3a 0a 20 20 20 20 20 20 74 65 73 74 ISTS:. test
1e29d 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d case( pExpr->op=
1e29e 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 =TK_SELECT );.
1e29f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 testcase( pE
1e2a0 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 xpr->op==TK_EXIS
1e2a1 54 53 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 TS );.#endif.
1e2a2 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 testcase( pEx
1e2a3 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b pr->op==TK_ID );
1e2a4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1e2a5 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 pExpr->op==TK_C
1e2a6 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 OLUMN );. t
1e2a7 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e estcase( pExpr->
1e2a8 6f 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 op==TK_AGG_FUNCT
1e2a9 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ION );. tes
1e2aa 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 tcase( pExpr->op
1e2ab 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 ==TK_AGG_COLUMN
1e2ac 29 3b 0a 20 20 20 20 20 20 70 57 61 6c 6b 65 72 );. pWalker
1e2ad 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 20 ->u.i = 0;.
1e2ae 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1e2af 74 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a t;. default:.
1e2b0 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1e2b1 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d _Continue;. }.}
1e2b2 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 .static int sele
1e2b3 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 ctNodeIsConstant
1e2b4 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 (Walker *pWalker
1e2b5 2c 20 53 65 6c 65 63 74 20 2a 4e 6f 74 55 73 65 , Select *NotUse
1e2b6 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
1e2b7 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
1e2b8 0a 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 . pWalker->u.i
1e2b9 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 = 0;. return WR
1e2ba 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69 C_Abort;.}.stati
1e2bb 63 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73 c int exprIsCons
1e2bc 74 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 t(Expr *p, int i
1e2bd 6e 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b nitFlag){. Walk
1e2be 65 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20 er w;. w.u.i =
1e2bf 69 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45 initFlag;. w.xE
1e2c0 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 xprCallback = ex
1e2c1 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 prNodeIsConstant
1e2c2 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c ;. w.xSelectCal
1e2c3 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f lback = selectNo
1e2c4 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 deIsConstant;.
1e2c5 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1e2c6 26 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 6e &w, p);. return
1e2c7 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a w.u.i;.}../*.**
1e2c8 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 Walk an express
1e2c9 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 ion tree. Retur
1e2ca 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 n 1 if the expre
1e2cb 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e ssion is constan
1e2cc 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74 t.** and 0 if it
1e2cd 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 involves variab
1e2ce 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 les or function
1e2cf 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 calls..**.** For
1e2d0 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 the purposes of
1e2d1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 this function,
1e2d2 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 a double-quoted
1e2d3 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 string (ex: "abc
1e2d4 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 ").** is conside
1e2d5 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 red a variable b
1e2d6 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 ut a single-quot
1e2d7 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 ed string (ex: '
1e2d8 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f abc') is.** a co
1e2d9 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 nstant..*/.SQLIT
1e2da 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1e2db 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
1e2dc 61 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 ant(Expr *p){.
1e2dd 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e return exprIsCon
1e2de 73 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a st(p, 1);.}../*.
1e2df 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 ** Walk an expre
1e2e0 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 ssion tree. Ret
1e2e1 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 urn 1 if the exp
1e2e2 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 ression is const
1e2e3 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 ant.** that does
1e2e4 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 72 no originate fr
1e2e5 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 om the ON or USI
1e2e6 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 NG clauses of a
1e2e7 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 join..** Return
1e2e8 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 0 if it involves
1e2e9 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75 variables or fu
1e2ea 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20 nction calls or
1e2eb 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e terms from.** an
1e2ec 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 ON or USING cla
1e2ed 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 use..*/.SQLITE_P
1e2ee 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1e2ef 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
1e2f0 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29 NotJoin(Expr *p)
1e2f1 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 {. return exprI
1e2f2 73 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a sConst(p, 3);.}.
1e2f3 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 ./*.** Walk an e
1e2f4 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 xpression tree.
1e2f5 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
1e2f6 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 expression is c
1e2f7 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 onstant.** or a
1e2f8 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 function call wi
1e2f9 74 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 th constant argu
1e2fa 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61 ments. Return a
1e2fb 6e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a nd 0 if there.**
1e2fc 20 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c are any variabl
1e2fd 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 es..**.** For th
1e2fe 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 e purposes of th
1e2ff 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 is function, a d
1e300 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 ouble-quoted str
1e301 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a ing (ex: "abc").
1e302 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ** is considered
1e303 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 a variable but
1e304 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 a single-quoted
1e305 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 string (ex: 'abc
1e306 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 ') is.** a const
1e307 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ant..*/.SQLITE_P
1e308 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1e309 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
1e30a 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 OrFunction(Expr
1e30b 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 *p){. return ex
1e30c 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b prIsConst(p, 2);
1e30d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
1e30e 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 6f expression p co
1e30f 64 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 des a constant i
1e310 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 73 nteger that is s
1e311 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 mall enough.** t
1e312 6f 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69 o fit in a 32-bi
1e313 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 t integer, retur
1e314 6e 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 20 n 1 and put the
1e315 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 value of the int
1e316 65 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c eger.** in *pVal
1e317 75 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72 ue. If the expr
1e318 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e ession is not an
1e319 20 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69 integer or if i
1e31a 74 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 t is too big.**
1e31b 74 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e to fit in a sign
1e31c 65 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 ed 32-bit intege
1e31d 72 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 r, return 0 and
1e31e 6c 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e leave *pValue un
1e31f 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 changed..*/.SQLI
1e320 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1e321 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 qlite3ExprIsInte
1e322 67 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 ger(Expr *p, int
1e323 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 *pValue){. int
1e324 20 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 rc = 0;. if( p
1e325 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 ->flags & EP_Int
1e326 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 56 Value ){. *pV
1e327 61 6c 75 65 20 3d 20 70 2d 3e 69 54 61 62 6c 65 alue = p->iTable
1e328 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a ;. return 1;.
1e329 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 2d }. switch( p-
1e32a 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 >op ){. case
1e32b 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 TK_INTEGER: {.
1e32c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e32d 47 65 74 49 6e 74 33 32 28 28 63 68 61 72 2a 29 GetInt32((char*)
1e32e 70 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 56 61 6c p->token.z, pVal
1e32f 75 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ue);. break
1e330 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1e331 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 TK_UPLUS: {.
1e332 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 45 rc = sqlite3E
1e333 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e xprIsInteger(p->
1e334 70 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a pLeft, pValue);.
1e335 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1e336 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 }. case TK_U
1e337 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 MINUS: {. i
1e338 6e 74 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 nt v;. if(
1e339 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 sqlite3ExprIsInt
1e33a 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 eger(p->pLeft, &
1e33b 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 v) ){. *p
1e33c 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 Value = -v;.
1e33d 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 rc = 1;.
1e33e 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
1e33f 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
1e340 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 lt: break;. }.
1e341 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 if( rc ){. p
1e342 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 45 47 45 ->op = TK_INTEGE
1e343 52 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 R;. p->flags
1e344 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a |= EP_IntValue;.
1e345 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 p->iTable =
1e346 2a 70 56 61 6c 75 65 3b 0a 20 20 7d 0a 20 20 72 *pValue;. }. r
1e347 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1e348 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
1e349 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 f the given stri
1e34a 6e 67 20 69 73 20 61 20 72 6f 77 2d 69 64 20 63 ng is a row-id c
1e34b 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 olumn name..*/.S
1e34c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1e34d 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 t sqlite3IsRowid
1e34e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b (const char *z){
1e34f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 . if( sqlite3St
1e350 72 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 49 44 rICmp(z, "_ROWID
1e351 5f 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 _")==0 ) return
1e352 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 1;. if( sqlite3
1e353 53 74 72 49 43 6d 70 28 7a 2c 20 22 52 4f 57 49 StrICmp(z, "ROWI
1e354 44 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 D")==0 ) return
1e355 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 1;. if( sqlite3
1e356 53 74 72 49 43 6d 70 28 7a 2c 20 22 4f 49 44 22 StrICmp(z, "OID"
1e357 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b )==0 ) return 1;
1e358 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
1e359 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
1e35a 65 20 69 66 20 74 68 65 20 49 4e 20 6f 70 65 72 e if the IN oper
1e35b 61 74 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f ator optimizatio
1e35c 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 n is enabled and
1e35d 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 73 .** the SELECT s
1e35e 74 61 74 65 6d 65 6e 74 20 70 20 65 78 69 73 74 tatement p exist
1e35f 73 20 61 6e 64 20 69 73 20 6f 66 20 74 68 65 0a s and is of the.
1e360 2a 2a 20 73 69 6d 70 6c 65 20 66 6f 72 6d 3a 0a ** simple form:.
1e361 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 **.** SELECT
1e362 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c <column> FROM <
1e363 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 table>.**.** If
1e364 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 this is the case
1e365 2c 20 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 , it may be poss
1e366 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 ible to use an e
1e367 78 69 73 74 69 6e 67 20 74 61 62 6c 65 0a 2a 2a xisting table.**
1e368 20 6f 72 20 69 6e 64 65 78 20 69 6e 73 74 65 61 or index instea
1e369 64 20 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 20 d of generating
1e36a 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 an epheremal tab
1e36b 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 le..*/.#ifndef S
1e36c 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
1e36d 45 52 59 0a 73 74 61 74 69 63 20 69 6e 74 20 69 ERY.static int i
1e36e 73 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f sCandidateForInO
1e36f 70 74 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 pt(Select *p){.
1e370 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a SrcList *pSrc;.
1e371 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 ExprList *pELi
1e372 73 74 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 st;. Table *pTa
1e373 62 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 b;. if( p==0 )
1e374 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 return 0;
1e375 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 /* r
1e376 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f ight-hand side o
1e377 66 20 49 4e 20 69 73 20 53 45 4c 45 43 54 20 2a f IN is SELECT *
1e378 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f /. if( p->pPrio
1e379 72 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 r ) return 0;
1e37a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f /* No
1e37b 74 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c t a compound SEL
1e37c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e ECT */. if( p->
1e37d 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 selFlags & (SF_D
1e37e 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 istinct|SF_Aggre
1e37f 67 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 72 gate) ){. r
1e380 65 74 75 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44 eturn 0; /* No D
1e381 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 ISTINCT keyword
1e382 61 6e 64 20 6e 6f 20 61 67 67 72 65 67 61 74 65 and no aggregate
1e383 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 functions */.
1e384 7d 0a 20 20 69 66 28 20 70 2d 3e 70 47 72 6f 75 }. if( p->pGrou
1e385 70 42 79 20 29 20 72 65 74 75 72 6e 20 30 3b 20 pBy ) return 0;
1e386 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 /* Ha
1e387 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c s no GROUP BY cl
1e388 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d ause */. if( p-
1e389 3e 70 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e >pLimit ) return
1e38a 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1e38b 20 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54 /* Has no LIMIT
1e38c 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 clause */. if(
1e38d 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 20 72 65 p->pOffset ) re
1e38e 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 2d turn 0;. if( p-
1e38f 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 72 6e >pWhere ) return
1e390 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1e391 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45 52 45 /* Has no WHERE
1e392 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70 53 72 clause */. pSr
1e393 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 c = p->pSrc;. a
1e394 73 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29 ssert( pSrc!=0 )
1e395 3b 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 ;. if( pSrc->nS
1e396 72 63 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 rc!=1 ) return 0
1e397 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 ; /* Si
1e398 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 46 52 4f ngle term in FRO
1e399 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 M clause */. if
1e39a 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 ( pSrc->a[0].pSe
1e39b 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b lect ) return 0;
1e39c 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 /* FROM cla
1e39d 75 73 65 20 69 73 20 6e 6f 74 20 61 20 73 75 62 use is not a sub
1e39e 71 75 65 72 79 20 2a 2f 0a 20 20 70 54 61 62 20 query */. pTab
1e39f 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 = pSrc->a[0].pTa
1e3a0 62 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 b;. if( pTab==0
1e3a1 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 ) return 0;. i
1e3a2 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 f( pTab->pSelect
1e3a3 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
1e3a4 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c /* FROM cl
1e3a5 61 75 73 65 20 69 73 20 6e 6f 74 20 61 20 76 69 ause is not a vi
1e3a6 65 77 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 ew */. if( IsVi
1e3a7 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 rtual(pTab) ) re
1e3a8 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 2f turn 0; /
1e3a9 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6e 6f * FROM clause no
1e3aa 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c t a virtual tabl
1e3ab 65 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 e */. pEList =
1e3ac 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 p->pEList;. if(
1e3ad 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d pEList->nExpr!=
1e3ae 31 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 1 ) return 0;
1e3af 20 20 20 20 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d /* One colum
1e3b0 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 n in the result
1e3b1 73 65 74 20 2a 2f 0a 20 20 69 66 28 20 70 45 4c set */. if( pEL
1e3b2 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d ist->a[0].pExpr-
1e3b3 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 >op!=TK_COLUMN )
1e3b4 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 52 65 return 0; /* Re
1e3b5 73 75 6c 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e sult is a column
1e3b6 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a */. return 1;.
1e3b7 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1e3b8 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 TE_OMIT_SUBQUERY
1e3b9 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 */../*.** This
1e3ba 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 function is used
1e3bb 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e by the implemen
1e3bc 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 4e tation of the IN
1e3bd 20 28 2e 2e 2e 29 20 6f 70 65 72 61 74 6f 72 2e (...) operator.
1e3be 0a 2a 2a 20 49 74 27 73 20 6a 6f 62 20 69 73 20 .** It's job is
1e3bf 74 6f 20 66 69 6e 64 20 6f 72 20 63 72 65 61 74 to find or creat
1e3c0 65 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63 e a b-tree struc
1e3c1 74 75 72 65 20 74 68 61 74 20 6d 61 79 20 62 65 ture that may be
1e3c2 20 75 73 65 64 0a 2a 2a 20 65 69 74 68 65 72 20 used.** either
1e3c3 74 6f 20 74 65 73 74 20 66 6f 72 20 6d 65 6d 62 to test for memb
1e3c4 65 72 73 68 69 70 20 6f 66 20 74 68 65 20 28 2e ership of the (.
1e3c5 2e 2e 29 20 73 65 74 20 6f 72 20 74 6f 20 69 74 ..) set or to it
1e3c6 65 72 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a erate through.**
1e3c7 20 69 74 73 20 6d 65 6d 62 65 72 73 2c 20 73 6b its members, sk
1e3c8 69 70 70 69 6e 67 20 64 75 70 6c 69 63 61 74 65 ipping duplicate
1e3c9 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 s..**.** The cur
1e3ca 73 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 sor opened on th
1e3cb 65 20 73 74 72 75 63 74 75 72 65 20 28 64 61 74 e structure (dat
1e3cc 61 62 61 73 65 20 74 61 62 6c 65 2c 20 64 61 74 abase table, dat
1e3cd 61 62 61 73 65 20 69 6e 64 65 78 20 0a 2a 2a 20 abase index .**
1e3ce 6f 72 20 65 70 68 65 72 6d 61 6c 20 74 61 62 6c or ephermal tabl
1e3cf 65 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 e) is stored in
1e3d0 70 58 2d 3e 69 54 61 62 6c 65 20 62 65 66 6f 72 pX->iTable befor
1e3d1 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 e this function
1e3d2 72 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 68 65 20 returns..** The
1e3d3 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 returned value i
1e3d4 6e 64 69 63 61 74 65 73 20 74 68 65 20 73 74 72 ndicates the str
1e3d5 75 63 74 75 72 65 20 74 79 70 65 2c 20 61 73 20 ucture type, as
1e3d6 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
1e3d7 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 IN_INDEX_ROWID
1e3d8 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 - The cursor was
1e3d9 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 opened on a dat
1e3da 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 abase table..**
1e3db 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 IN_INDEX_INDEX
1e3dc 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 - The cursor wa
1e3dd 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 s opened on a da
1e3de 74 61 62 61 73 65 20 69 6e 64 65 78 2e 0a 2a 2a tabase index..**
1e3df 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 IN_INDEX_EPH
1e3e0 2d 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 77 - The cursor w
1e3e1 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 73 as opened on a s
1e3e2 70 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65 64 pecially created
1e3e3 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 and.**
1e3e4 20 20 20 20 20 20 20 20 20 20 20 70 6f 70 75 6c popul
1e3e5 61 74 65 64 20 65 70 68 65 72 65 6d 61 6c 20 74 ated epheremal t
1e3e6 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 able..**.** An e
1e3e7 78 69 73 74 69 6e 67 20 73 74 72 75 63 74 75 72 xisting structur
1e3e8 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 e may only be us
1e3e9 65 64 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 ed if the SELECT
1e3ea 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70 6c is of the simpl
1e3eb 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a e.** form:.**.**
1e3ec 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c SELECT <col
1e3ed 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 umn> FROM <table
1e3ee 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 72 4e 6f 74 >.**.** If prNot
1e3ef 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20 Found parameter
1e3f0 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20 73 is 0, then the s
1e3f1 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 62 65 tructure will be
1e3f2 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 used to iterate
1e3f3 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 .** through the
1e3f4 73 65 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 set members, ski
1e3f5 70 70 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 63 pping any duplic
1e3f6 61 74 65 73 2e 20 49 6e 20 74 68 69 73 20 63 61 ates. In this ca
1e3f7 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 6d se an.** epherem
1e3f8 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 al table must be
1e3f9 20 75 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65 used unless the
1e3fa 20 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 6d selected <colum
1e3fb 6e 3e 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 n> is guaranteed
1e3fc 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 65 .** to be unique
1e3fd 20 2d 20 65 69 74 68 65 72 20 62 65 63 61 75 73 - either becaus
1e3fe 65 20 69 74 20 69 73 20 61 6e 20 49 4e 54 45 47 e it is an INTEG
1e3ff 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f ER PRIMARY KEY o
1e400 72 20 69 74 0a 2a 2a 20 69 73 20 75 6e 69 71 75 r it.** is uniqu
1e401 65 20 62 79 20 76 69 72 74 75 65 20 6f 66 20 61 e by virtue of a
1e402 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72 20 69 constraint or i
1e403 6d 70 6c 69 63 69 74 20 69 6e 64 65 78 2e 0a 2a mplicit index..*
1e404 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f *.** If the prNo
1e405 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 tFound parameter
1e406 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 is not 0, then
1e407 74 68 65 20 73 74 72 75 63 74 75 72 65 20 77 69 the structure wi
1e408 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 ll be used .** f
1e409 6f 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d 62 or fast set memb
1e40a 65 72 73 68 69 70 20 74 65 73 74 73 2e 20 49 6e ership tests. In
1e40b 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 70 this case an ep
1e40c 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 heremal table mu
1e40d 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 st .** be used u
1e40e 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 nless <column> i
1e40f 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 s an INTEGER PRI
1e410 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 MARY KEY or an i
1e411 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20 ndex can .** be
1e412 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 found with <colu
1e413 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 2d mn> as its left-
1e414 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a most column..**.
1e415 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 74 72 75 ** When the stru
1e416 63 74 75 72 65 20 69 73 20 62 65 69 6e 67 20 75 cture is being u
1e417 73 65 64 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 sed for set memb
1e418 65 72 73 68 69 70 20 74 65 73 74 73 2c 20 74 68 ership tests, th
1e419 65 20 75 73 65 72 0a 2a 2a 20 6e 65 65 64 73 20 e user.** needs
1e41a 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 to know whether
1e41b 6f 72 20 6e 6f 74 20 74 68 65 20 73 74 72 75 63 or not the struc
1e41c 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e ture contains an
1e41d 20 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a 20 76 61 SQL NULL .** va
1e41e 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 lue in order to
1e41f 63 6f 72 72 65 63 74 6c 79 20 65 76 61 6c 75 61 correctly evalua
1e420 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c te expressions l
1e421 69 6b 65 20 22 58 20 49 4e 20 28 59 2c 20 5a 29 ike "X IN (Y, Z)
1e422 22 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 "..** If there i
1e423 73 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20 s a chance that
1e424 74 68 65 20 73 74 72 75 63 74 75 72 65 20 6d 61 the structure ma
1e425 79 20 63 6f 6e 74 61 69 6e 20 61 20 4e 55 4c 4c y contain a NULL
1e426 20 76 61 6c 75 65 20 61 74 0a 2a 2a 20 72 75 6e value at.** run
1e427 74 69 6d 65 2c 20 74 68 65 6e 20 61 20 72 65 67 time, then a reg
1e428 69 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 ister is allocat
1e429 65 64 20 61 6e 64 20 74 68 65 20 72 65 67 69 73 ed and the regis
1e42a 74 65 72 20 6e 75 6d 62 65 72 20 77 72 69 74 74 ter number writt
1e42b 65 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e 6f 74 46 en.** to *prNotF
1e42c 6f 75 6e 64 2e 20 49 66 20 74 68 65 72 65 20 69 ound. If there i
1e42d 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74 s no chance that
1e42e 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 63 the structure c
1e42f 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 4c ontains a.** NUL
1e430 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 L value, then *p
1e431 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 66 rNotFound is lef
1e432 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a t unchanged..**.
1e433 2a 2a 20 49 66 20 61 20 72 65 67 69 73 74 65 72 ** If a register
1e434 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
1e435 64 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 73 d its location s
1e436 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 46 tored in *prNotF
1e437 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 74 ound, then.** it
1e438 73 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 s initial value
1e439 69 73 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 is NULL. If the
1e43a 73 74 72 75 63 74 75 72 65 20 64 6f 65 73 20 6e structure does n
1e43b 6f 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 ot remain consta
1e43c 6e 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 nt.** for the du
1e43d 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 ration of the qu
1e43e 65 72 79 20 28 69 2e 65 2e 20 74 68 65 20 73 65 ery (i.e. the se
1e43f 74 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 t is a correlate
1e440 64 20 73 75 62 2d 73 65 6c 65 63 74 29 2c 20 0a d sub-select), .
1e441 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
1e442 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 the allocated re
1e443 67 69 73 74 65 72 20 69 73 20 72 65 73 65 74 20 gister is reset
1e444 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20 74 69 6d to NULL each tim
1e445 65 20 74 68 65 20 0a 2a 2a 20 73 74 72 75 63 74 e the .** struct
1e446 75 72 65 20 69 73 20 72 65 70 6f 70 75 6c 61 74 ure is repopulat
1e447 65 64 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 ed. This allows
1e448 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 the caller to us
1e449 65 20 76 64 62 65 20 63 6f 64 65 20 0a 2a 2a 20 e vdbe code .**
1e44a 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 equivalent to th
1e44b 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
1e44c 2a 2a 20 20 20 69 66 28 20 72 65 67 69 73 74 65 ** if( registe
1e44d 72 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20 r==NULL ){.**
1e44e 20 20 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65 has_null = <te
1e44f 73 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 st if data struc
1e450 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 ture contains nu
1e451 6c 6c 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73 ll>.** regis
1e452 74 65 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a ter = 1.** }.*
1e453 2a 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f *.** in order to
1e454 20 61 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 avoid running t
1e455 68 65 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 he <test if data
1e456 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
1e457 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73 ins null>.** tes
1e458 74 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 t more often tha
1e459 6e 20 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a n is necessary..
1e45a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1e45b 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a E_OMIT_SUBQUERY.
1e45c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1e45d 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e nt sqlite3FindIn
1e45e 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 Index(Parse *pPa
1e45f 72 73 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69 rse, Expr *pX, i
1e460 6e 74 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b nt *prNotFound){
1e461 0a 20 20 53 65 6c 65 63 74 20 2a 70 3b 0a 20 20 . Select *p;.
1e462 69 6e 74 20 65 54 79 70 65 20 3d 20 30 3b 0a 20 int eType = 0;.
1e463 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 int iTab = pPar
1e464 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 6e se->nTab++;. in
1e465 74 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d t mustBeUnique =
1e466 20 21 70 72 4e 6f 74 46 6f 75 6e 64 3b 0a 0a 20 !prNotFound;..
1e467 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 77 69 6e 67 /* The follwing
1e468 20 69 66 28 2e 2e 2e 29 20 65 78 70 72 65 73 73 if(...) express
1e469 69 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74 ion is true if t
1e46a 68 65 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 he SELECT is of
1e46b 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 the . ** simple
1e46c 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a form:. **. **
1e46d 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c SELECT <col
1e46e 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 umn> FROM <table
1e46f 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 >. **. ** If t
1e470 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c his is the case,
1e471 20 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69 it may be possi
1e472 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 78 ble to use an ex
1e473 69 73 74 69 6e 67 20 74 61 62 6c 65 0a 20 20 2a isting table. *
1e474 2a 20 6f 72 20 69 6e 64 65 78 20 69 6e 73 74 65 * or index inste
1e475 61 64 20 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 ad of generating
1e476 20 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 an epheremal ta
1e477 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 ble.. */. p =
1e478 70 58 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 69 pX->pSelect;. i
1e479 66 28 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f f( isCandidateFo
1e47a 72 49 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 rInOpt(p) ){.
1e47b 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1e47c 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 49 Parse->db;. I
1e47d 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 ndex *pIdx;.
1e47e 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 2d Expr *pExpr = p-
1e47f 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 >pEList->a[0].pE
1e480 78 70 72 3b 0a 20 20 20 20 69 6e 74 20 69 43 6f xpr;. int iCo
1e481 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 l = pExpr->iColu
1e482 6d 6e 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 mn;. Vdbe *v
1e483 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
1e484 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 2f (pParse);.. /
1e485 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1e486 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 is only called f
1e487 72 6f 6d 20 74 77 6f 20 70 6c 61 63 65 73 2e 20 rom two places.
1e488 49 6e 20 62 6f 74 68 20 63 61 73 65 73 20 74 68 In both cases th
1e489 65 20 76 64 62 65 0a 20 20 20 20 2a 2a 20 68 61 e vdbe. ** ha
1e48a 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 s already been a
1e48b 6c 6c 6f 63 61 74 65 64 2e 20 53 6f 20 61 73 73 llocated. So ass
1e48c 75 6d 65 20 73 71 6c 69 74 65 33 47 65 74 56 64 ume sqlite3GetVd
1e48d 62 65 28 29 20 69 73 20 61 6c 77 61 79 73 0a 20 be() is always.
1e48e 20 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c ** successful
1e48f 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 here.. */.
1e490 20 20 61 73 73 65 72 74 28 76 29 3b 0a 20 20 20 assert(v);.
1e491 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 if( iCol<0 ){.
1e492 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 int iMem =
1e493 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
1e494 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b int iAddr;
1e495 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 . Table *pT
1e496 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b ab = p->pSrc->a[
1e497 30 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 69 0].pTab;. i
1e498 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 nt iDb = sqlite3
1e499 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 SchemaToIndex(db
1e49a 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 , pTab->pSchema)
1e49b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1e49c 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 dbeUsesBtree(v,
1e49d 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 69 41 64 iDb);.. iAd
1e49e 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dr = sqlite3Vdbe
1e49f 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c AddOp1(v, OP_If,
1e4a0 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 iMem);. sq
1e4a1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1e4a2 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 v, OP_Integer, 1
1e4a3 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 20 20 , iMem);..
1e4a4 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 sqlite3OpenTable
1e4a5 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c 20 69 (pParse, iTab, i
1e4a6 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 Db, pTab, OP_Ope
1e4a7 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20 65 54 nRead);. eT
1e4a8 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 ype = IN_INDEX_R
1e4a9 4f 57 49 44 3b 0a 0a 20 20 20 20 20 20 73 71 6c OWID;.. sql
1e4aa 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
1e4ab 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 20 (v, iAddr);.
1e4ac 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
1e4ad 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 The collation se
1e4ae 71 75 65 6e 63 65 20 75 73 65 64 20 62 79 20 74 quence used by t
1e4af 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 he comparison. I
1e4b0 66 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74 6f f an index is to
1e4b1 20 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 75 73 . ** be us
1e4b2 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 ed in place of a
1e4b3 20 74 65 6d 70 2d 74 61 62 6c 65 2c 20 69 74 20 temp-table, it
1e4b4 6d 75 73 74 20 62 65 20 6f 72 64 65 72 65 64 20 must be ordered
1e4b5 61 63 63 6f 72 64 69 6e 67 0a 20 20 20 20 20 20 according.
1e4b6 2a 2a 20 74 6f 20 74 68 69 73 20 63 6f 6c 6c 61 ** to this colla
1e4b7 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 20 tion sequence..
1e4b8 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 43 6f */. Co
1e4b9 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 73 71 llSeq *pReq = sq
1e4ba 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 lite3BinaryCompa
1e4bb 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 reCollSeq(pParse
1e4bc 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78 , pX->pLeft, pEx
1e4bd 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 pr);.. /* C
1e4be 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 66 heck that the af
1e4bf 66 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c 6c finity that will
1e4c0 20 62 65 20 75 73 65 64 20 74 6f 20 70 65 72 66 be used to perf
1e4c1 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 20 20 2a orm the . *
1e4c2 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 * comparison is
1e4c3 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 the same as the
1e4c4 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 affinity of the
1e4c5 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 20 20 column. If.
1e4c6 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c 20 69 ** it is not, i
1e4c7 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
1e4c8 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69 6e 64 e to use any ind
1e4c9 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ex.. */.
1e4ca 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d Table *pTab =
1e4cb 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 p->pSrc->a[0].p
1e4cc 54 61 62 3b 0a 20 20 20 20 20 20 63 68 61 72 20 Tab;. char
1e4cd 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e aff = comparison
1e4ce 41 66 66 69 6e 69 74 79 28 70 58 29 3b 0a 20 20 Affinity(pX);.
1e4cf 20 20 20 20 69 6e 74 20 61 66 66 69 6e 69 74 79 int affinity
1e4d0 5f 6f 6b 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f _ok = (pTab->aCo
1e4d1 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79 l[iCol].affinity
1e4d2 3d 3d 61 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49 ==aff||aff==SQLI
1e4d3 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 TE_AFF_NONE);..
1e4d4 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 for(pIdx=pT
1e4d5 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 ab->pIndex; pIdx
1e4d6 20 26 26 20 65 54 79 70 65 3d 3d 30 20 26 26 20 && eType==0 &&
1e4d7 61 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64 affinity_ok; pId
1e4d8 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a x=pIdx->pNext){.
1e4d9 20 20 20 20 20 20 20 20 69 66 28 20 28 70 49 64 if( (pId
1e4da 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d x->aiColumn[0]==
1e4db 69 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 26 iCol). &
1e4dc 26 20 28 70 52 65 71 3d 3d 73 71 6c 69 74 65 33 & (pReq==sqlite3
1e4dd 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 FindCollSeq(db,
1e4de 45 4e 43 28 64 62 29 2c 20 70 49 64 78 2d 3e 61 ENC(db), pIdx->a
1e4df 7a 43 6f 6c 6c 5b 30 5d 2c 20 2d 31 2c 20 30 29 zColl[0], -1, 0)
1e4e0 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 21 ). && (!
1e4e1 6d 75 73 74 42 65 55 6e 69 71 75 65 20 7c 7c 20 mustBeUnique ||
1e4e2 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d (pIdx->nColumn==
1e4e3 31 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72 1 && pIdx->onErr
1e4e4 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20 or!=OE_None)).
1e4e5 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
1e4e6 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 int iDb;.
1e4e7 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d int iMem =
1e4e8 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
1e4e9 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 . int i
1e4ea 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20 Addr;.
1e4eb 63 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 0a 20 char *pKey;. .
1e4ec 20 20 20 20 20 20 20 20 20 70 4b 65 79 20 3d 20 pKey =
1e4ed 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 49 (char *)sqlite3I
1e4ee 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 ndexKeyinfo(pPar
1e4ef 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 se, pIdx);.
1e4f0 20 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 iDb = sqlit
1e4f1 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
1e4f2 64 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 65 6d db, pIdx->pSchem
1e4f3 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 a);. sq
1e4f4 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 lite3VdbeUsesBtr
1e4f5 65 65 28 76 2c 20 69 44 62 29 3b 0a 0a 20 20 20 ee(v, iDb);..
1e4f6 20 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 iAddr = s
1e4f7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
1e4f8 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 (v, OP_If, iMem)
1e4f9 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
1e4fa 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1e4fb 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 OP_Integer, 1,
1e4fc 69 4d 65 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 iMem);. .
1e4fd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1e4fe 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e ddOp2(v, OP_SetN
1e4ff 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 70 49 umColumns, 0, pI
1e500 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 dx->nColumn);.
1e501 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1e502 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
1e503 4f 70 65 6e 52 65 61 64 2c 20 69 54 61 62 2c 20 OpenRead, iTab,
1e504 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c pIdx->tnum, iDb,
1e505 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1e506 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e507 70 4b 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f 5f pKey,P4_KEYINFO_
1e508 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 HANDOFF);.
1e509 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 VdbeComment(
1e50a 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d 3e (v, "%s", pIdx->
1e50b 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 20 zName));.
1e50c 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e eType = IN_IN
1e50d 44 45 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20 20 DEX_INDEX;..
1e50e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1e50f 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64 eJumpHere(v, iAd
1e510 64 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 dr);. i
1e511 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26 26 f( prNotFound &&
1e512 20 21 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f !pTab->aCol[iCo
1e513 6c 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 l].notNull ){.
1e514 20 20 20 20 20 20 20 20 20 20 2a 70 72 4e 6f 74 *prNot
1e515 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 Found = ++pParse
1e516 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 ->nMem;.
1e517 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1e518 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1e519 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 30 20 . if( eType==0
1e51a 29 7b 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48 ){. int rMayH
1e51b 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 aveNull = 0;.
1e51c 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 eType = IN_INDE
1e51d 58 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70 X_EPH;. if( p
1e51e 72 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 rNotFound ){.
1e51f 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d *prNotFound =
1e520 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 rMayHaveNull =
1e521 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
1e522 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 }else if( pX
1e523 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e ->pLeft->iColumn
1e524 3c 30 20 26 26 20 70 58 2d 3e 70 53 65 6c 65 63 <0 && pX->pSelec
1e525 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 54 t==0 ){. eT
1e526 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 ype = IN_INDEX_R
1e527 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OWID;. }.
1e528 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 sqlite3CodeSubse
1e529 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 58 2c lect(pParse, pX,
1e52a 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 65 rMayHaveNull, e
1e52b 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 Type==IN_INDEX_R
1e52c 4f 57 49 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a OWID);. }else{.
1e52d 20 20 20 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d pX->iTable =
1e52e 20 69 54 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74 iTab;. }. ret
1e52f 75 72 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e urn eType;.}.#en
1e530 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 dif../*.** Gener
1e531 61 74 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61 ate code for sca
1e532 6c 61 72 20 73 75 62 71 75 65 72 69 65 73 20 75 lar subqueries u
1e533 73 65 64 20 61 73 20 61 6e 20 65 78 70 72 65 73 sed as an expres
1e534 73 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f sion.** and IN o
1e535 70 65 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70 perators. Examp
1e536 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 les:.**.** (
1e537 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 SELECT a FROM b)
1e538 20 20 20 20 20 20 20 20 20 20 2d 2d 20 73 75 62 -- sub
1e539 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 45 58 49 query.** EXI
1e53a 53 54 53 20 28 53 45 4c 45 43 54 20 61 20 46 52 STS (SELECT a FR
1e53b 4f 4d 20 62 29 20 20 20 2d 2d 20 45 58 49 53 54 OM b) -- EXIST
1e53c 53 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 S subquery.**
1e53d 20 20 78 20 49 4e 20 28 34 2c 35 2c 31 31 29 20 x IN (4,5,11)
1e53e 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 --
1e53f 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 IN operator with
1e540 20 6c 69 73 74 20 6f 6e 20 72 69 67 68 74 2d 68 list on right-h
1e541 61 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20 20 20 and side.**
1e542 78 20 49 4e 20 28 53 45 4c 45 43 54 20 61 20 46 x IN (SELECT a F
1e543 52 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e ROM b) -- IN
1e544 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 73 operator with s
1e545 75 62 71 75 65 72 79 20 6f 6e 20 74 68 65 20 72 ubquery on the r
1e546 69 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ight.**.** The p
1e547 45 78 70 72 20 70 61 72 61 6d 65 74 65 72 20 64 Expr parameter d
1e548 65 73 63 72 69 62 65 73 20 74 68 65 20 65 78 70 escribes the exp
1e549 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e ression that con
1e54a 74 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20 tains the IN.**
1e54b 6f 70 65 72 61 74 6f 72 20 6f 72 20 73 75 62 71 operator or subq
1e54c 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 uery..**.** If p
1e54d 61 72 61 6d 65 74 65 72 20 69 73 52 6f 77 69 64 arameter isRowid
1e54e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 is non-zero, th
1e54f 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 en expression pE
1e550 78 70 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 xpr is guarantee
1e551 64 0a 2a 2a 20 74 6f 20 62 65 20 6f 66 20 74 68 d.** to be of th
1e552 65 20 66 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20 e form "<rowid>
1e553 49 4e 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 IN (?, ?, ?)", w
1e554 68 65 72 65 20 3c 72 6f 77 69 64 3e 20 69 73 20 here <rowid> is
1e555 61 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 a reference.** t
1e556 6f 20 73 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b o some integer k
1e557 65 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 ey column of a t
1e558 61 62 6c 65 20 42 2d 54 72 65 65 2e 20 49 6e 20 able B-Tree. In
1e559 74 68 69 73 20 63 61 73 65 2c 20 75 73 65 20 61 this case, use a
1e55a 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72 n.** intkey B-Tr
1e55b 65 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 ee to store the
1e55c 73 65 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 set of IN(...) v
1e55d 61 6c 75 65 73 20 69 6e 73 74 65 61 64 20 6f 66 alues instead of
1e55e 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73 the usual.** (s
1e55f 6c 6f 77 65 72 29 20 76 61 72 69 61 62 6c 65 20 lower) variable
1e560 6c 65 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72 length keys B-Tr
1e561 65 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ee..*/.#ifndef S
1e562 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
1e563 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ERY.SQLITE_PRIVA
1e564 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
1e565 6f 64 65 53 75 62 73 65 6c 65 63 74 28 0a 20 20 odeSubselect(.
1e566 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a Parse *pParse, .
1e567 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 0a Expr *pExpr, .
1e568 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 int rMayHaveNu
1e569 6c 6c 2c 0a 20 20 69 6e 74 20 69 73 52 6f 77 69 ll,. int isRowi
1e56a 64 0a 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 d.){. int testA
1e56b 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ddr = 0;
1e56c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e56d 2a 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 * One-time test
1e56e 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 56 64 62 address */. Vdb
1e56f 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 e *v = sqlite3Ge
1e570 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
1e571 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 if( v==0 ) retu
1e572 72 6e 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 rn;... /* This
1e573 63 6f 64 65 20 6d 75 73 74 20 62 65 20 72 75 6e code must be run
1e574 20 69 6e 20 69 74 73 20 65 6e 74 69 72 65 74 79 in its entirety
1e575 20 65 76 65 72 79 20 74 69 6d 65 20 69 74 20 69 every time it i
1e576 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20 s encountered.
1e577 2a 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 ** if any of the
1e578 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 following is tr
1e579 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 ue:. **. **
1e57a 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 * The right-ha
1e57b 6e 64 20 73 69 64 65 20 69 73 20 61 20 63 6f 72 nd side is a cor
1e57c 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 related subquery
1e57d 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 . ** * The
1e57e 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 right-hand side
1e57f 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e is an expression
1e580 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 list containing
1e581 20 76 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20 variables. **
1e582 20 20 20 2a 20 20 57 65 20 61 72 65 20 69 6e 73 * We are ins
1e583 69 64 65 20 61 20 74 72 69 67 67 65 72 0a 20 20 ide a trigger.
1e584 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f **. ** If all o
1e585 66 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 f the above are
1e586 66 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63 false, then we c
1e587 61 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 an run this code
1e588 20 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 just once. **
1e589 73 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 save the results
1e58a 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68 65 20 , and reuse the
1e58b 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73 same result on s
1e58c 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 ubsequent invoca
1e58d 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 tions.. */. if
1e58e 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f ( !ExprHasAnyPro
1e58f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
1e590 56 61 72 53 65 6c 65 63 74 29 20 26 26 20 21 70 VarSelect) && !p
1e591 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b Parse->trigStack
1e592 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 ){. int mem
1e593 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
1e594 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1e595 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 eAddOp1(v, OP_If
1e596 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74 , mem);. test
1e597 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 Addr = sqlite3Vd
1e598 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
1e599 6e 74 65 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b nteger, 1, mem);
1e59a 0a 20 20 20 20 61 73 73 65 72 74 28 20 74 65 73 . assert( tes
1e59b 74 41 64 64 72 3e 30 20 7c 7c 20 70 50 61 72 73 tAddr>0 || pPars
1e59c 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e->db->mallocFai
1e59d 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 led );. }.. sw
1e59e 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 itch( pExpr->op
1e59f 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 ){. case TK_I
1e5a0 4e 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 N: {. char
1e5a1 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 affinity;.
1e5a2 4b 65 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b KeyInfo keyInfo;
1e5a3 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b . int addr;
1e5a4 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 /* Addre
1e5a5 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 ss of OP_OpenEph
1e5a6 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 emeral instructi
1e5a7 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 on */. Expr
1e5a8 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d *pLeft = pExpr-
1e5a9 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 20 20 20 69 >pLeft;.. i
1e5aa 66 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 f( rMayHaveNull
1e5ab 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1e5ac 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1e5ad 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79 OP_Null, 0, rMay
1e5ae 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 HaveNull);.
1e5af 20 7d 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69 }.. affini
1e5b0 74 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ty = sqlite3Expr
1e5b1 41 66 66 69 6e 69 74 79 28 70 4c 65 66 74 29 3b Affinity(pLeft);
1e5b2 0a 0a 20 20 20 20 20 20 2f 2a 20 57 68 65 74 68 .. /* Wheth
1e5b3 65 72 20 74 68 69 73 20 69 73 20 61 6e 20 27 78 er this is an 'x
1e5b4 20 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20 IN(SELECT...)'
1e5b5 6f 72 20 61 6e 20 27 78 20 49 4e 28 3c 65 78 70 or an 'x IN(<exp
1e5b6 72 6c 69 73 74 3e 29 27 0a 20 20 20 20 20 20 2a rlist>)'. *
1e5b7 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 74 20 * expression it
1e5b8 69 73 20 68 61 6e 64 6c 65 64 20 74 68 65 20 73 is handled the s
1e5b9 61 6d 65 20 77 61 79 2e 20 41 20 76 69 72 74 75 ame way. A virtu
1e5ba 61 6c 20 74 61 62 6c 65 20 69 73 20 0a 20 20 20 al table is .
1e5bb 20 20 20 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 ** filled wit
1e5bc 68 20 73 69 6e 67 6c 65 2d 66 69 65 6c 64 20 69 h single-field i
1e5bd 6e 64 65 78 20 6b 65 79 73 20 72 65 70 72 65 73 ndex keys repres
1e5be 65 6e 74 69 6e 67 20 74 68 65 20 72 65 73 75 6c enting the resul
1e5bf 74 73 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d ts. ** from
1e5c0 20 74 68 65 20 53 45 4c 45 43 54 20 6f 72 20 74 the SELECT or t
1e5c1 68 65 20 3c 65 78 70 72 6c 69 73 74 3e 2e 0a 20 he <exprlist>..
1e5c2 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
1e5c3 20 49 66 20 74 68 65 20 27 78 27 20 65 78 70 72 If the 'x' expr
1e5c4 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 ession is a colu
1e5c5 6d 6e 20 76 61 6c 75 65 2c 20 6f 72 20 74 68 65 mn value, or the
1e5c6 20 53 45 4c 45 43 54 2e 2e 2e 0a 20 20 20 20 20 SELECT....
1e5c7 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 65 ** statement re
1e5c8 74 75 72 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76 turns a column v
1e5c9 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 61 alue, then the a
1e5ca 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 0a ffinity of that.
1e5cb 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 ** column
1e5cc 69 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 is used to build
1e5cd 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 73 2e the index keys.
1e5ce 20 49 66 20 62 6f 74 68 20 27 78 27 20 61 6e 64 If both 'x' and
1e5cf 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 53 45 the. ** SE
1e5d0 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e LECT... statemen
1e5d1 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 t are columns, t
1e5d2 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 hen numeric affi
1e5d3 6e 69 74 79 20 69 73 20 75 73 65 64 0a 20 20 20 nity is used.
1e5d4 20 20 20 2a 2a 20 69 66 20 65 69 74 68 65 72 20 ** if either
1e5d5 63 6f 6c 75 6d 6e 20 68 61 73 20 4e 55 4d 45 52 column has NUMER
1e5d6 49 43 20 6f 72 20 49 4e 54 45 47 45 52 20 61 66 IC or INTEGER af
1e5d7 66 69 6e 69 74 79 2e 20 49 66 20 6e 65 69 74 68 finity. If neith
1e5d8 65 72 0a 20 20 20 20 20 20 2a 2a 20 27 78 27 20 er. ** 'x'
1e5d9 6e 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e nor the SELECT..
1e5da 2e 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 . statement are
1e5db 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 columns, then nu
1e5dc 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79 0a 20 meric affinity.
1e5dd 20 20 20 20 20 2a 2a 20 69 73 20 75 73 65 64 2e ** is used.
1e5de 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1e5df 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
1e5e0 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a pParse->nTab++;.
1e5e1 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c addr = sql
1e5e2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1e5e3 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 , OP_OpenEphemer
1e5e4 61 6c 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c al, pExpr->iTabl
1e5e5 65 2c 20 21 69 73 52 6f 77 69 64 29 3b 0a 20 20 e, !isRowid);.
1e5e6 20 20 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 memset(&keyI
1e5e7 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b nfo, 0, sizeof(k
1e5e8 65 79 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 eyInfo));.
1e5e9 6b 65 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d keyInfo.nField =
1e5ea 20 31 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 1;.. if( p
1e5eb 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b Expr->pSelect ){
1e5ec 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 . /* Case
1e5ed 20 31 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 1: expr IN
1e5ee 28 53 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 (SELECT ...).
1e5ef 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 **.
1e5f0 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
1e5f1 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 72 65 to write the re
1e5f2 73 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c sults of the sel
1e5f3 65 63 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d ect into the tem
1e5f4 70 6f 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a porary. *
1e5f5 2a 20 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 * table allocate
1e5f6 64 20 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f d and opened abo
1e5f7 76 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ve.. */.
1e5f8 20 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 SelectDes
1e5f9 74 20 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20 t dest;.
1e5fa 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
1e5fb 3b 0a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ;.. asser
1e5fc 74 28 20 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 t( !isRowid );.
1e5fd 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 sqlite3Se
1e5fe 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 lectDestInit(&de
1e5ff 73 74 2c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 st, SRT_Set, pEx
1e600 70 72 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 pr->iTable);.
1e601 20 20 20 20 20 64 65 73 74 2e 61 66 66 69 6e 69 dest.affini
1e602 74 79 20 3d 20 28 75 38 29 61 66 66 69 6e 69 74 ty = (u8)affinit
1e603 79 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 y;. asser
1e604 74 28 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c t( (pExpr->iTabl
1e605 65 26 30 78 30 30 30 30 46 46 46 46 29 3d 3d 70 e&0x0000FFFF)==p
1e606 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a Expr->iTable );.
1e607 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 if( sqli
1e608 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 te3Select(pParse
1e609 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 , pExpr->pSelect
1e60a 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20 , &dest) ){.
1e60b 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
1e60c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1e60d 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e pEList = pExpr->
1e60e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b pSelect->pEList;
1e60f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 4c . if( pEL
1e610 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e ist && pEList->n
1e611 45 78 70 72 3e 30 20 29 7b 20 0a 20 20 20 20 20 Expr>0 ){ .
1e612 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f keyInfo.aCo
1e613 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42 ll[0] = sqlite3B
1e614 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c inaryCompareColl
1e615 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 Seq(pParse, pExp
1e616 72 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20 r->pLeft,.
1e617 20 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e pEList->
1e618 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 a[0].pExpr);.
1e619 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1e61a 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 70 4c se if( pExpr->pL
1e61b 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ist ){. /
1e61c 2a 20 43 61 73 65 20 32 3a 20 20 20 20 20 65 78 * Case 2: ex
1e61d 70 72 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29 pr IN (exprlist)
1e61e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
1e61f 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 ** For each
1e620 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c expression, buil
1e621 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66 d an index key f
1e622 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69 rom the evaluati
1e623 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a on and. *
1e624 2a 20 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68 * store it in th
1e625 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c e temporary tabl
1e626 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 e. If <expr> is
1e627 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75 a column, then u
1e628 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 se. ** th
1e629 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e at columns affin
1e62a 69 74 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e ity when buildin
1e62b 67 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66 g index keys. If
1e62c 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20 <expr> is not.
1e62d 20 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75 ** a colu
1e62e 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20 mn, use numeric
1e62f 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20 affinity..
1e630 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 */. int
1e631 20 69 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 i;. Expr
1e632 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 List *pList = pE
1e633 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 xpr->pList;.
1e634 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c struct ExprL
1e635 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
1e636 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c . int r1,
1e637 20 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20 r2, r3;..
1e638 20 20 69 66 28 20 21 61 66 66 69 6e 69 74 79 20 if( !affinity
1e639 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66 ){. aff
1e63a 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 inity = SQLITE_A
1e63b 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 FF_NONE;.
1e63c 20 7d 0a 20 20 20 20 20 20 20 20 6b 65 79 49 6e }. keyIn
1e63d 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 fo.aColl[0] = sq
1e63e 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 lite3ExprCollSeq
1e63f 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
1e640 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 pLeft);..
1e641 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 /* Loop through
1e642 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e each expression
1e643 20 69 6e 20 3c 65 78 70 72 6c 69 73 74 3e 2e 20 in <exprlist>.
1e644 2a 2f 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 */. r1 =
1e645 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 sqlite3GetTempRe
1e646 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 g(pParse);.
1e647 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 r2 = sqlite3G
1e648 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 etTempReg(pParse
1e649 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1e64a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1e64b 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 32 29 3b OP_Null, 0, r2);
1e64c 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 70 . for(i=p
1e64d 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 List->nExpr, pIt
1e64e 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3e 30 em=pList->a; i>0
1e64f 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i--, pItem++){
1e650 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 . Expr
1e651 2a 70 45 32 20 3d 20 70 49 74 65 6d 2d 3e 70 45 *pE2 = pItem->pE
1e652 78 70 72 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 xpr;..
1e653 2f 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 /* If the expres
1e654 73 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 sion is not cons
1e655 74 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c tant then we wil
1e656 6c 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 l need to.
1e657 20 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 ** disable t
1e658 68 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73 he test that was
1e659 20 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 generated above
1e65a 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 that makes sure
1e65b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 . ** th
1e65c 69 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 is code only exe
1e65d 63 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 cutes once. Bec
1e65e 61 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 ause for a non-c
1e65f 6f 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 onstant.
1e660 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 ** expression
1e661 77 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e we need to rerun
1e662 20 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 this code each
1e663 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 time..
1e664 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 */. if(
1e665 20 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71 testAddr && !sq
1e666 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
1e667 61 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 ant(pE2) ){.
1e668 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1e669 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 dbeChangeToNoop(
1e66a 76 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 v, testAddr-1, 2
1e66b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 );. t
1e66c 65 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 estAddr = 0;.
1e66d 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
1e66e 20 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 /* Evaluate
1e66f 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 the expression a
1e670 6e 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 nd insert it int
1e671 6f 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 o the temp table
1e672 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 50 */. pP
1e673 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1e674 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 20 Cache++;.
1e675 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 45 r3 = sqlite3E
1e676 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 xprCodeTarget(pP
1e677 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a arse, pE2, r1);.
1e678 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
1e679 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c ( pParse->disabl
1e67a 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 eColCache>0 );.
1e67b 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d pParse-
1e67c 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 >disableColCache
1e67d 2d 2d 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 69 --;.. i
1e67e 66 28 20 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 f( isRowid ){.
1e67f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1e680 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1e681 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 33 2c P_MustBeInt, r3,
1e682 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 sqlite3VdbeCurr
1e683 65 6e 74 41 64 64 72 28 76 29 2b 32 29 3b 0a 20 entAddr(v)+2);.
1e684 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1e685 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
1e686 4f 50 5f 49 6e 73 65 72 74 2c 20 70 45 78 70 72 OP_Insert, pExpr
1e687 2d 3e 69 54 61 62 6c 65 2c 20 72 32 2c 20 72 33 ->iTable, r2, r3
1e688 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
1e689 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
1e68a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1e68b 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 4(v, OP_MakeReco
1e68c 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 2c 20 26 rd, r3, 1, r2, &
1e68d 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 affinity, 1);.
1e68e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1e68f 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 3ExprCacheAffini
1e690 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c tyChange(pParse,
1e691 20 72 33 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 r3, 1);.
1e692 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1e693 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 AddOp2(v, OP_Idx
1e694 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 Insert, pExpr->i
1e695 54 61 62 6c 65 2c 20 72 32 29 3b 0a 20 20 20 20 Table, r2);.
1e696 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1e697 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1e698 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
1e699 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 pParse, r1);.
1e69a 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 sqlite3Rele
1e69b 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
1e69c 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a e, r2);. }.
1e69d 20 20 20 20 20 20 69 66 28 20 21 69 73 52 6f 77 if( !isRow
1e69e 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 id ){. sq
1e69f 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1e6a0 34 28 76 2c 20 61 64 64 72 2c 20 28 76 6f 69 64 4(v, addr, (void
1e6a1 20 2a 29 26 6b 65 79 49 6e 66 6f 2c 20 50 34 5f *)&keyInfo, P4_
1e6a2 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20 KEYINFO);.
1e6a3 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1e6a4 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 54 }.. case T
1e6a5 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61 K_EXISTS:. ca
1e6a6 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a se TK_SELECT: {.
1e6a7 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 /* This ha
1e6a8 73 20 74 6f 20 62 65 20 61 20 73 63 61 6c 61 72 s to be a scalar
1e6a9 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65 72 61 SELECT. Genera
1e6aa 74 65 20 63 6f 64 65 20 74 6f 20 70 75 74 20 74 te code to put t
1e6ab 68 65 0a 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 he. ** valu
1e6ac 65 20 6f 66 20 74 68 69 73 20 73 65 6c 65 63 74 e of this select
1e6ad 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c in a memory cel
1e6ae 6c 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68 65 l and record the
1e6af 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 2a 2a number. **
1e6b0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 of the memory c
1e6b1 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d 6e 2e 0a ell in iColumn..
1e6b2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
1e6b3 74 61 74 69 63 20 63 6f 6e 73 74 20 54 6f 6b 65 tatic const Toke
1e6b4 6e 20 6f 6e 65 20 3d 20 7b 20 28 75 38 2a 29 22 n one = { (u8*)"
1e6b5 31 22 2c 20 30 2c 20 31 20 7d 3b 0a 20 20 20 20 1", 0, 1 };.
1e6b6 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 0a Select *pSel;.
1e6b7 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 SelectDest
1e6b8 20 64 65 73 74 3b 0a 0a 20 20 20 20 20 20 70 53 dest;.. pS
1e6b9 65 6c 20 3d 20 70 45 78 70 72 2d 3e 70 53 65 6c el = pExpr->pSel
1e6ba 65 63 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ect;. sqlit
1e6bb 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 e3SelectDestInit
1e6bc 28 26 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61 (&dest, 0, ++pPa
1e6bd 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 rse->nMem);.
1e6be 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d if( pExpr->op=
1e6bf 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 =TK_SELECT ){.
1e6c0 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 dest.eDest
1e6c1 20 3d 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 = SRT_Mem;.
1e6c2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1e6c3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c ddOp2(v, OP_Null
1e6c4 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 , 0, dest.iParm)
1e6c5 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f ;. VdbeCo
1e6c6 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 mment((v, "Init
1e6c7 73 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 22 subquery result"
1e6c8 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ));. }else{
1e6c9 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 65 44 . dest.eD
1e6ca 65 73 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73 est = SRT_Exists
1e6cb 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1e6cc 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1e6cd 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65 P_Integer, 0, de
1e6ce 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 st.iParm);.
1e6cf 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 VdbeComment((
1e6d0 76 2c 20 22 49 6e 69 74 20 45 58 49 53 54 53 20 v, "Init EXISTS
1e6d1 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 result"));.
1e6d2 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1e6d3 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 ExprDelete(pPars
1e6d4 65 2d 3e 64 62 2c 20 70 53 65 6c 2d 3e 70 4c 69 e->db, pSel->pLi
1e6d5 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c mit);. pSel
1e6d6 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 ->pLimit = sqlit
1e6d7 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 e3PExpr(pParse,
1e6d8 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 TK_INTEGER, 0, 0
1e6d9 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69 , &one);. i
1e6da 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 f( sqlite3Select
1e6db 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26 (pParse, pSel, &
1e6dc 64 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 dest) ){.
1e6dd 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
1e6de 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 . pExpr->iC
1e6df 6f 6c 75 6d 6e 20 3d 20 64 65 73 74 2e 69 50 61 olumn = dest.iPa
1e6e0 72 6d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b rm;. break;
1e6e1 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
1e6e2 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20 ( testAddr ){.
1e6e3 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
1e6e4 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 64 64 pHere(v, testAdd
1e6e5 72 2d 31 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 r-1);. }.. ret
1e6e6 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a urn;.}.#endif /*
1e6e7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 SQLITE_OMIT_SUB
1e6e8 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 QUERY */../*.**
1e6e9 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 2d 62 Duplicate an 8-b
1e6ea 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61 yte value.*/.sta
1e6eb 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 62 79 tic char *dup8by
1e6ec 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e tes(Vdbe *v, con
1e6ed 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20 st char *in){.
1e6ee 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69 char *out = sqli
1e6ef 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 te3DbMallocRaw(s
1e6f0 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c qlite3VdbeDb(v),
1e6f1 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 29 8);. if( out )
1e6f2 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 74 {. memcpy(out
1e6f3 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20 , in, 8);. }.
1e6f4 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f return out;.}../
1e6f5 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e *.** Generate an
1e6f6 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 instruction tha
1e6f7 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 66 t will put the f
1e6f8 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a loating point.**
1e6f9 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 65 64 value described
1e6fa 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e by z[0..n-1] in
1e6fb 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d to register iMem
1e6fc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 ..**.** The z[]
1e6fd 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 string will prob
1e6fe 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f ably not be zero
1e6ff 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 -terminated. Bu
1e700 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 t the .** z[n] c
1e701 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72 haracter is guar
1e702 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d anteed to be som
1e703 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 ething that does
1e704 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b not look.** lik
1e705 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 e the continuati
1e706 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 on of the number
1e707 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1e708 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 20 2a codeReal(Vdbe *
1e709 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a v, const char *z
1e70a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6e 65 67 , int n, int neg
1e70b 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 ateFlag, int iMe
1e70c 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 7a 20 m){. assert( z
1e70d 7c 7c 20 76 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 || v==0 || sqlit
1e70e 65 33 56 64 62 65 44 62 28 76 29 2d 3e 6d 61 6c e3VdbeDb(v)->mal
1e70f 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 locFailed );. a
1e710 73 73 65 72 74 28 20 21 7a 20 7c 7c 20 21 69 73 ssert( !z || !is
1e711 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20 digit(z[n]) );.
1e712 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1e713 52 28 6e 29 3b 0a 20 20 69 66 28 20 7a 20 29 7b R(n);. if( z ){
1e714 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75 . double valu
1e715 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b e;. char *zV;
1e716 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 . sqlite3AtoF
1e717 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 (z, &value);.
1e718 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 if( sqlite3IsNa
1e719 4e 28 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 N(value) ){.
1e71a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1e71b 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 Op2(v, OP_Null,
1e71c 30 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 65 0, iMem);. }e
1e71d 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 6e lse{. if( n
1e71e 65 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75 egateFlag ) valu
1e71f 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 e = -value;.
1e720 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73 zV = dup8bytes
1e721 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75 (v, (char*)&valu
1e722 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 e);. sqlite
1e723 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
1e724 50 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c P_Real, 0, iMem,
1e725 20 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 0, zV, P4_REAL)
1e726 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a ;. }. }.}...
1e727 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 /*.** Generate a
1e728 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 n instruction th
1e729 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 at will put the
1e72a 69 6e 74 65 67 65 72 20 64 65 73 63 72 69 62 65 integer describe
1e72b 20 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e by.** text z[0.
1e72c 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 .n-1] into regis
1e72d 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 ter iMem..**.**
1e72e 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 The z[] string w
1e72f 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 ill probably not
1e730 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 be zero-termina
1e731 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a ted. But the .*
1e732 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 * z[n] character
1e733 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
1e734 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 o be something t
1e735 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f hat does not loo
1e736 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f k.** like the co
1e737 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 ntinuation of th
1e738 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 e number..*/.sta
1e739 74 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74 tic void codeInt
1e73a 65 67 65 72 28 56 64 62 65 20 2a 76 2c 20 45 78 eger(Vdbe *v, Ex
1e73b 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6e pr *pExpr, int n
1e73c 65 67 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d egFlag, int iMem
1e73d 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 ){. const char
1e73e 2a 7a 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d *z;. if( pExpr-
1e73f 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 >flags & EP_IntV
1e740 61 6c 75 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 alue ){. int
1e741 69 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c i = pExpr->iTabl
1e742 65 3b 0a 20 20 20 20 69 66 28 20 6e 65 67 46 6c e;. if( negFl
1e743 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20 ag ) i = -i;.
1e744 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1e745 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
1e746 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 65 , i, iMem);. }e
1e747 6c 73 65 20 69 66 28 20 28 7a 20 3d 20 28 63 68 lse if( (z = (ch
1e748 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e ar*)pExpr->token
1e749 2e 7a 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e .z)!=0 ){. in
1e74a 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d t i;. int n =
1e74b 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b pExpr->token.n;
1e74c 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73 . assert( !is
1e74d 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20 digit(z[n]) );.
1e74e 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 65 if( sqlite3Ge
1e74f 74 49 6e 74 33 32 28 7a 2c 20 26 69 29 20 29 7b tInt32(z, &i) ){
1e750 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c . if( negFl
1e751 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20 ag ) i = -i;.
1e752 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1e753 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
1e754 65 72 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 er, i, iMem);.
1e755 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 }else if( sqli
1e756 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 te3FitsIn64Bits(
1e757 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b 0a 20 z, negFlag) ){.
1e758 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a i64 value;.
1e759 20 20 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a char *zV;.
1e75a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f sqlite3Ato
1e75b 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a i64(z, &value);.
1e75c 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61 if( negFla
1e75d 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c g ) value = -val
1e75e 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64 ue;. zV = d
1e75f 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 up8bytes(v, (cha
1e760 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 r*)&value);.
1e761 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1e762 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c Op4(v, OP_Int64,
1e763 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 0, iMem, 0, zV,
1e764 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 20 20 P4_INT64);.
1e765 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 64 }else{. cod
1e766 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 2c 20 6e eReal(v, z, n, n
1e767 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 egFlag, iMem);.
1e768 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a }. }.}.../*.
1e769 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
1e76a 20 74 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61 that will extra
1e76b 63 74 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 ct the iColumn-t
1e76c 68 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 0a 2a 2a h column from.**
1e76d 20 74 61 62 6c 65 20 70 54 61 62 20 61 6e 64 20 table pTab and
1e76e 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e store the column
1e76f 20 76 61 6c 75 65 20 69 6e 20 61 20 72 65 67 69 value in a regi
1e770 73 74 65 72 2e 20 20 41 6e 20 65 66 66 6f 72 74 ster. An effort
1e771 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 73 .** is made to s
1e772 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 tore the column
1e773 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1e774 72 20 69 52 65 67 2c 20 62 75 74 20 74 68 69 73 r iReg, but this
1e775 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 75 61 72 61 is.** not guara
1e776 6e 74 65 65 64 2e 20 20 54 68 65 20 6c 6f 63 61 nteed. The loca
1e777 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c 75 tion of the colu
1e778 6d 6e 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 mn value is retu
1e779 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 rned..**.** Ther
1e77a 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 e must be an ope
1e77b 6e 20 63 75 72 73 6f 72 20 74 6f 20 70 54 61 62 n cursor to pTab
1e77c 20 69 6e 20 69 54 61 62 6c 65 20 77 68 65 6e 20 in iTable when
1e77d 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
1e77e 69 73 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 is called. If i
1e77f 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e 20 63 6f Column<0 then co
1e780 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 de is generated
1e781 74 68 61 74 20 65 78 74 72 61 63 74 73 20 74 68 that extracts th
1e782 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 e rowid..**.** T
1e783 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 his routine migh
1e784 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 75 t attempt to reu
1e785 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 se the value of
1e786 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a the column that.
1e787 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ** has already b
1e788 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 een loaded into
1e789 61 20 72 65 67 69 73 74 65 72 2e 20 20 54 68 65 a register. The
1e78a 20 76 61 6c 75 65 20 77 69 6c 6c 20 61 6c 77 61 value will alwa
1e78b 79 73 0a 2a 2a 20 62 65 20 75 73 65 64 20 69 66 ys.** be used if
1e78c 20 69 74 20 68 61 73 20 6e 6f 74 20 75 6e 64 65 it has not unde
1e78d 72 67 6f 6e 65 20 61 6e 79 20 61 66 66 69 6e 69 rgone any affini
1e78e 74 79 20 63 68 61 6e 67 65 73 2e 20 20 42 75 74 ty changes. But
1e78f 20 69 66 0a 2a 2a 20 61 6e 20 61 66 66 69 6e 69 if.** an affini
1e790 74 79 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 ty change has oc
1e791 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 curred, then the
1e792 20 63 61 63 68 65 64 20 76 61 6c 75 65 20 77 69 cached value wi
1e793 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 75 73 ll only be.** us
1e794 65 64 20 69 66 20 61 6c 6c 6f 77 41 66 66 43 68 ed if allowAffCh
1e795 6e 67 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 53 ng is true..*/.S
1e796 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1e797 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
1e798 65 47 65 74 43 6f 6c 75 6d 6e 28 0a 20 20 50 61 eGetColumn(. Pa
1e799 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f rse *pParse, /
1e79a 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f * Parsing and co
1e79b 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f de generating co
1e79c 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 ntext */. Table
1e79d 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 44 *pTab, /* D
1e79e 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 escription of th
1e79f 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 72 e table we are r
1e7a0 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a 2f 0a 20 eading from */.
1e7a1 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 int iColumn,
1e7a2 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 /* Index of th
1e7a3 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a e table column *
1e7a4 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 /. int iTable,
1e7a5 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 /* The curs
1e7a6 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 or pointing to t
1e7a7 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e he table */. in
1e7a8 74 20 69 52 65 67 2c 20 20 20 20 20 20 20 20 2f t iReg, /
1e7a9 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 * Store results
1e7aa 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 61 6c here */. int al
1e7ab 6c 6f 77 41 66 66 43 68 6e 67 20 2f 2a 20 54 72 lowAffChng /* Tr
1e7ac 75 65 20 69 66 20 70 72 69 6f 72 20 61 66 66 69 ue if prior affi
1e7ad 6e 69 74 79 20 63 68 61 6e 67 65 73 20 61 72 65 nity changes are
1e7ae 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 OK */.){. Vdbe
1e7af 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 *v = pParse->pV
1e7b0 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 dbe;. int i;.
1e7b1 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 struct yColCache
1e7b2 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c *p;.. for(i=0,
1e7b3 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 p=pParse->aColC
1e7b4 61 63 68 65 3b 20 69 3c 70 50 61 72 73 65 2d 3e ache; i<pParse->
1e7b5 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 2c 20 nColCache; i++,
1e7b6 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p++){. if( p-
1e7b7 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 >iTable==iTable
1e7b8 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 && p->iColumn==i
1e7b9 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 20 Column.
1e7ba 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 68 61 && (!p->affCha
1e7bb 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 66 43 nge || allowAffC
1e7bc 68 6e 67 29 20 29 7b 0a 23 69 66 20 30 0a 20 20 hng) ){.#if 0.
1e7bd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1e7be 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4e 6f 6f 70 ddOp0(v, OP_Noop
1e7bf 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d );. VdbeCom
1e7c0 6d 65 6e 74 28 28 76 2c 20 22 4f 50 54 3a 20 74 ment((v, "OPT: t
1e7c1 61 62 25 64 2e 63 6f 6c 25 64 20 2d 3e 20 72 25 ab%d.col%d -> r%
1e7c2 64 22 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c d", iTable, iCol
1e7c3 75 6d 6e 2c 20 70 2d 3e 69 52 65 67 29 29 3b 0a umn, p->iReg));.
1e7c4 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 65 74 #endif. ret
1e7c5 75 72 6e 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 urn p->iReg;.
1e7c6 20 7d 0a 20 20 7d 20 20 0a 20 20 61 73 73 65 72 }. } . asser
1e7c7 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28 t( v!=0 );. if(
1e7c8 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 iColumn<0 ){.
1e7c9 20 20 69 6e 74 20 6f 70 20 3d 20 28 70 54 61 62 int op = (pTab
1e7ca 20 26 26 20 49 73 56 69 72 74 75 61 6c 28 70 54 && IsVirtual(pT
1e7cb 61 62 29 29 20 3f 20 4f 50 5f 56 52 6f 77 69 64 ab)) ? OP_VRowid
1e7cc 20 3a 20 4f 50 5f 52 6f 77 69 64 3b 0a 20 20 20 : OP_Rowid;.
1e7cd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1e7ce 70 32 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 p2(v, op, iTable
1e7cf 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65 , iReg);. }else
1e7d0 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a if( pTab==0 ){.
1e7d1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1e7d2 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 ddOp3(v, OP_Colu
1e7d3 6d 6e 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c mn, iTable, iCol
1e7d4 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 umn, iReg);. }e
1e7d5 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20 lse{. int op
1e7d6 3d 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 = IsVirtual(pTab
1e7d7 29 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 3a ) ? OP_VColumn :
1e7d8 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 OP_Column;.
1e7d9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1e7da 33 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c 3(v, op, iTable,
1e7db 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b iColumn, iReg);
1e7dc 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 . sqlite3Colu
1e7dd 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 mnDefault(v, pTa
1e7de 62 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 23 69 66 b, iColumn);.#if
1e7df 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1e7e0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
1e7e1 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 61 43 if( pTab->aC
1e7e2 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 ol[iColumn].affi
1e7e3 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 nity==SQLITE_AFF
1e7e4 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20 20 20 73 _REAL ){. s
1e7e5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
1e7e6 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e (v, OP_RealAffin
1e7e7 69 74 79 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 ity, iReg);.
1e7e8 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 }.#endif. }. i
1e7e9 66 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 f( pParse->disab
1e7ea 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 7b leColCache==0 ){
1e7eb 0a 20 20 20 20 69 20 3d 20 70 50 61 72 73 65 2d . i = pParse-
1e7ec 3e 69 43 6f 6c 43 61 63 68 65 3b 0a 20 20 20 20 >iColCache;.
1e7ed 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 43 6f p = &pParse->aCo
1e7ee 6c 43 61 63 68 65 5b 69 5d 3b 0a 20 20 20 20 70 lCache[i];. p
1e7ef 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c ->iTable = iTabl
1e7f0 65 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d e;. p->iColum
1e7f1 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 n = iColumn;.
1e7f2 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b p->iReg = iReg;
1e7f3 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 . p->affChang
1e7f4 65 20 3d 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a e = 0;. i++;.
1e7f5 20 20 20 20 69 66 28 20 69 3e 3d 41 72 72 61 79 if( i>=Array
1e7f6 53 69 7a 65 28 70 50 61 72 73 65 2d 3e 61 43 6f Size(pParse->aCo
1e7f7 6c 43 61 63 68 65 29 20 29 20 69 20 3d 20 30 3b lCache) ) i = 0;
1e7f8 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 72 73 . if( i>pPars
1e7f9 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 20 29 20 70 e->nColCache ) p
1e7fa 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 Parse->nColCache
1e7fb 20 3d 20 69 3b 0a 20 20 20 20 70 50 61 72 73 65 = i;. pParse
1e7fc 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 69 3b ->iColCache = i;
1e7fd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52 . }. return iR
1e7fe 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 eg;.}../*.** Cle
1e7ff 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61 ar all column ca
1e800 63 68 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f che entries asso
1e801 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
1e802 76 64 62 65 0a 2a 2a 20 63 75 72 73 6f 72 20 77 vdbe.** cursor w
1e803 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 ith cursor numbe
1e804 72 20 69 54 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c r iTable..*/.SQL
1e805 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1e806 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 sqlite3ExprClea
1e807 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 rColumnCache(Par
1e808 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
1e809 69 54 61 62 6c 65 29 7b 0a 20 20 69 66 28 20 69 iTable){. if( i
1e80a 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 70 Table<0 ){. p
1e80b 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 Parse->nColCache
1e80c 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 65 = 0;. pParse
1e80d 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 30 3b ->iColCache = 0;
1e80e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
1e80f 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 t i;. for(i=0
1e810 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c ; i<pParse->nCol
1e811 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 Cache; i++){.
1e812 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61 if( pParse->a
1e813 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 54 61 62 ColCache[i].iTab
1e814 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20 le==iTable ){.
1e815 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1e816 69 3d 3d 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 i==pParse->nColC
1e817 61 63 68 65 2d 31 20 29 3b 0a 20 20 20 20 20 20 ache-1 );.
1e818 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 pParse->aColCa
1e819 63 68 65 5b 69 5d 20 3d 20 70 50 61 72 73 65 2d che[i] = pParse-
1e81a 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d 70 50 61 >aColCache[--pPa
1e81b 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 5d 3b rse->nColCache];
1e81c 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d . pParse-
1e81d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 70 50 61 >iColCache = pPa
1e81e 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 0a rse->nColCache;.
1e81f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1e820 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 }.}../*.** Recor
1e821 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 d the fact that
1e822 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e an affinity chan
1e823 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 ge has occurred
1e824 6f 6e 20 69 43 6f 75 6e 74 0a 2a 2a 20 72 65 67 on iCount.** reg
1e825 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 isters starting
1e826 77 69 74 68 20 69 53 74 61 72 74 2e 0a 2a 2f 0a with iStart..*/.
1e827 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1e828 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
1e829 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e acheAffinityChan
1e82a 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ge(Parse *pParse
1e82b 2c 20 69 6e 74 20 69 53 74 61 72 74 2c 20 69 6e , int iStart, in
1e82c 74 20 69 43 6f 75 6e 74 29 7b 0a 20 20 69 6e 74 t iCount){. int
1e82d 20 69 45 6e 64 20 3d 20 69 53 74 61 72 74 20 2b iEnd = iStart +
1e82e 20 69 43 6f 75 6e 74 20 2d 20 31 3b 0a 20 20 69 iCount - 1;. i
1e82f 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
1e830 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 i<pParse->nColC
1e831 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 ache; i++){.
1e832 69 6e 74 20 72 20 3d 20 70 50 61 72 73 65 2d 3e int r = pParse->
1e833 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 aColCache[i].iRe
1e834 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 53 g;. if( r>=iS
1e835 74 61 72 74 20 26 26 20 72 3c 3d 69 45 6e 64 20 tart && r<=iEnd
1e836 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d ){. pParse-
1e837 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 61 66 >aColCache[i].af
1e838 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20 20 20 fChange = 1;.
1e839 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
1e83a 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1e83b 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 66 72 move content fr
1e83c 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46 72 om registers iFr
1e83d 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d om...iFrom+nReg-
1e83e 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 1.** over to iTo
1e83f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 4b 65 ..iTo+nReg-1. Ke
1e840 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 ep the column ca
1e841 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 2e 0a che up-to-date..
1e842 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1e843 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
1e844 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 65 prCodeMove(Parse
1e845 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 *pParse, int iF
1e846 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e rom, int iTo, in
1e847 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 t nReg){. int i
1e848 3b 0a 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 69 ;. if( iFrom==i
1e849 54 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 To ) return;. s
1e84a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1e84b 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 (pParse->pVdbe,
1e84c 4f 50 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20 OP_Move, iFrom,
1e84d 69 54 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 66 6f iTo, nReg);. fo
1e84e 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d r(i=0; i<pParse-
1e84f 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 29 >nColCache; i++)
1e850 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 70 50 {. int x = pP
1e851 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b arse->aColCache[
1e852 69 5d 2e 69 52 65 67 3b 0a 20 20 20 20 69 66 28 i].iReg;. if(
1e853 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c 69 x>=iFrom && x<i
1e854 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 20 From+nReg ){.
1e855 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 pParse->aColC
1e856 61 63 68 65 5b 69 5d 2e 69 52 65 67 20 2b 3d 20 ache[i].iReg +=
1e857 69 54 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d iTo-iFrom;. }
1e858 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 . }.}../*.** Ge
1e859 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63 nerate code to c
1e85a 6f 70 79 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d opy content from
1e85b 20 72 65 67 69 73 74 65 72 73 20 69 46 72 6f 6d registers iFrom
1e85c 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a ...iFrom+nReg-1.
1e85d 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e ** over to iTo..
1e85e 69 54 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 iTo+nReg-1..*/.S
1e85f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1e860 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f id sqlite3ExprCo
1e861 64 65 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 deCopy(Parse *pP
1e862 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c arse, int iFrom,
1e863 20 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 int iTo, int nR
1e864 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 eg){. int i;.
1e865 69 66 28 20 69 46 72 6f 6d 3d 3d 69 54 6f 20 29 if( iFrom==iTo )
1e866 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 return;. for(i
1e867 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b 2b 29 =0; i<nReg; i++)
1e868 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1e869 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e eAddOp2(pParse->
1e86a 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c 20 pVdbe, OP_Copy,
1e86b 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 29 3b iFrom+i, iTo+i);
1e86c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
1e86d 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 79 turn true if any
1e86e 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 register in the
1e86f 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69 54 range iFrom..iT
1e870 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a 2a o (inclusive).**
1e871 20 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74 is used as part
1e872 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 of the column c
1e873 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ache..*/.static
1e874 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e int usedAsColumn
1e875 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 Cache(Parse *pPa
1e876 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 rse, int iFrom,
1e877 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74 20 int iTo){. int
1e878 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
1e879 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 pParse->nColCach
1e87a 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 e; i++){. int
1e87b 20 72 20 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f r = pParse->aCo
1e87c 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 3b 0a lCache[i].iReg;.
1e87d 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d if( r>=iFrom
1e87e 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74 && r<=iTo ) ret
1e87f 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 urn 1;. }. ret
1e880 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
1e881 54 68 65 72 65 20 69 73 20 61 20 76 61 6c 75 65 There is a value
1e882 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65 in register iRe
1e883 67 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 72 65 20 g..**.** We are
1e884 67 6f 69 6e 67 20 74 6f 20 6d 6f 64 69 66 79 20 going to modify
1e885 74 68 65 20 76 61 6c 75 65 2c 20 73 6f 20 77 65 the value, so we
1e886 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 need to make su
1e887 72 65 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 re it.** is not
1e888 61 20 63 61 63 68 65 64 20 72 65 67 69 73 74 65 a cached registe
1e889 72 2e 20 20 49 66 20 69 52 65 67 20 69 73 20 61 r. If iReg is a
1e88a 20 63 61 63 68 65 64 20 72 65 67 69 73 74 65 72 cached register
1e88b 2c 0a 2a 2a 20 74 68 65 6e 20 63 6c 65 61 72 20 ,.** then clear
1e88c 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
1e88d 67 20 63 61 63 68 65 20 6c 69 6e 65 2e 0a 2a 2f g cache line..*/
1e88e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1e88f 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
1e890 57 72 69 74 61 62 6c 65 52 65 67 69 73 74 65 72 WritableRegister
1e891 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1e892 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 int iReg){. int
1e893 20 69 3b 0a 20 20 69 66 28 20 75 73 65 64 41 73 i;. if( usedAs
1e894 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72 ColumnCache(pPar
1e895 73 65 2c 20 69 52 65 67 2c 20 69 52 65 67 29 20 se, iReg, iReg)
1e896 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 ){. for(i=0;
1e897 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 i<pParse->nColCa
1e898 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 che; i++){.
1e899 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61 43 6f if( pParse->aCo
1e89a 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 3d 3d lCache[i].iReg==
1e89b 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 20 20 iReg ){.
1e89c 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 pParse->aColCach
1e89d 65 5b 69 5d 20 3d 20 70 50 61 72 73 65 2d 3e 61 e[i] = pParse->a
1e89e 43 6f 6c 43 61 63 68 65 5b 2d 2d 70 50 61 72 73 ColCache[--pPars
1e89f 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 5d 3b 0a 20 e->nColCache];.
1e8a0 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 69 pParse->i
1e8a1 43 6f 6c 43 61 63 68 65 20 3d 20 70 50 61 72 73 ColCache = pPars
1e8a2 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 0a 20 20 e->nColCache;.
1e8a3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1e8a4 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
1e8a5 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e last instruction
1e8a6 20 63 6f 64 65 64 20 69 73 20 61 6e 20 65 70 68 coded is an eph
1e8a7 65 6d 65 72 61 6c 20 63 6f 70 79 20 6f 66 20 61 emeral copy of a
1e8a8 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65 67 ny of.** the reg
1e8a9 69 73 74 65 72 73 20 69 6e 20 74 68 65 20 6e 52 isters in the nR
1e8aa 65 67 20 72 65 67 69 73 74 65 72 73 20 62 65 67 eg registers beg
1e8ab 69 6e 6e 69 6e 67 20 77 69 74 68 20 69 52 65 67 inning with iReg
1e8ac 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 , then.** conver
1e8ad 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72 t the last instr
1e8ae 75 63 74 69 6f 6e 20 66 72 6f 6d 20 4f 50 5f 53 uction from OP_S
1e8af 43 6f 70 79 20 74 6f 20 4f 50 5f 43 6f 70 79 2e Copy to OP_Copy.
1e8b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1e8b1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
1e8b2 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72 73 xprHardCopy(Pars
1e8b3 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
1e8b4 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a Reg, int nReg){.
1e8b5 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 56 64 int addr;. Vd
1e8b6 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 beOp *pOp;. Vdb
1e8b7 65 20 2a 76 3b 0a 0a 20 20 76 20 3d 20 70 50 61 e *v;.. v = pPa
1e8b8 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 64 rse->pVdbe;. ad
1e8b9 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dr = sqlite3Vdbe
1e8ba 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a CurrentAddr(v);.
1e8bb 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 pOp = sqlite3V
1e8bc 64 62 65 47 65 74 4f 70 28 76 2c 20 61 64 64 72 dbeGetOp(v, addr
1e8bd 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 -1);. assert( p
1e8be 4f 70 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 Op || pParse->db
1e8bf 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1e8c0 3b 0a 20 20 69 66 28 20 70 4f 70 20 26 26 20 70 ;. if( pOp && p
1e8c1 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 Op->opcode==OP_S
1e8c2 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e 70 31 3e Copy && pOp->p1>
1e8c3 3d 69 52 65 67 20 26 26 20 70 4f 70 2d 3e 70 31 =iReg && pOp->p1
1e8c4 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b 0a 20 20 <iReg+nReg ){.
1e8c5 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 pOp->opcode =
1e8c6 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a OP_Copy;. }.}..
1e8c7 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
1e8c8 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 ode to store the
1e8c9 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 41 value of the iA
1e8ca 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69 6e lias-th alias in
1e8cb 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 61 72 register.** tar
1e8cc 67 65 74 2e 20 20 54 68 65 20 66 69 72 73 74 20 get. The first
1e8cd 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 61 6c time this is cal
1e8ce 6c 65 64 2c 20 70 45 78 70 72 20 69 73 20 65 76 led, pExpr is ev
1e8cf 61 6c 75 61 74 65 64 20 74 6f 20 63 6f 6d 70 75 aluated to compu
1e8d0 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 te.** the value
1e8d1 6f 66 20 74 68 65 20 61 6c 69 61 73 2e 20 20 54 of the alias. T
1e8d2 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 he value is stor
1e8d3 65 64 20 69 6e 20 61 6e 20 61 75 78 69 6c 69 61 ed in an auxilia
1e8d4 72 79 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 ry register.** a
1e8d5 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 nd the number of
1e8d6 20 74 68 61 74 20 72 65 67 69 73 74 65 72 20 69 that register i
1e8d7 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 6e 20 s returned. On
1e8d8 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
1e8d9 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 ,.** the registe
1e8da 72 20 6e 75 6d 62 65 72 20 69 73 20 72 65 74 75 r number is retu
1e8db 72 6e 65 64 20 77 69 74 68 6f 75 74 20 67 65 6e rned without gen
1e8dc 65 72 61 74 69 6e 67 20 61 6e 79 20 63 6f 64 65 erating any code
1e8dd 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
1e8de 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 t in order for t
1e8df 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 63 6f 64 his to work, cod
1e8e0 65 20 6d 75 73 74 20 62 65 20 67 65 6e 65 72 61 e must be genera
1e8e1 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61 ted in the.** sa
1e8e2 6d 65 20 6f 72 64 65 72 20 74 68 61 74 20 69 74 me order that it
1e8e3 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a is executed..**
1e8e4 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 72 65 20 .** Aliases are
1e8e5 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e numbered startin
1e8e6 67 20 77 69 74 68 20 31 2e 20 20 53 6f 20 69 41 g with 1. So iA
1e8e7 6c 69 61 73 20 69 73 20 69 6e 20 74 68 65 20 72 lias is in the r
1e8e8 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 74 6f 20 ange.** of 1 to
1e8e9 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 69 pParse->nAlias i
1e8ea 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a 2a 0a 2a nclusive. .**.*
1e8eb 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 * pParse->aAlias
1e8ec 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 63 6f 72 [iAlias-1] recor
1e8ed 64 73 20 74 68 65 20 72 65 67 69 73 74 65 72 20 ds the register
1e8ee 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74 68 65 number where the
1e8ef 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 value.** of the
1e8f0 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 iAlias-th alias
1e8f1 20 69 73 20 73 74 6f 72 65 64 2e 20 20 49 66 20 is stored. If
1e8f2 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 zero, that means
1e8f3 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 6c 69 that the.** ali
1e8f4 61 73 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 as has not yet b
1e8f5 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2f een computed..*/
1e8f6 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 .static int code
1e8f7 41 6c 69 61 73 28 50 61 72 73 65 20 2a 70 50 61 Alias(Parse *pPa
1e8f8 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 61 73 2c rse, int iAlias,
1e8f9 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e Expr *pExpr, in
1e8fa 74 20 74 61 72 67 65 74 29 7b 0a 20 20 73 71 6c t target){. sql
1e8fb 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
1e8fc 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 52 65 e->db;. int iRe
1e8fd 67 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d g;. if( pParse-
1e8fe 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3c 70 50 61 >nAliasAlloc<pPa
1e8ff 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 7b 0a 20 rse->nAlias ){.
1e900 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 pParse->aAlia
1e901 73 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 s = sqlite3DbRea
1e902 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70 llocOrFree(db, p
1e903 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 2c 0a 20 Parse->aAlias,.
1e904 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e905 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e906 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 sizeof(pParse->a
1e907 41 6c 69 61 73 5b 30 5d 29 2a 70 50 61 72 73 65 Alias[0])*pParse
1e908 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 20 20 ->nAlias );.
1e909 74 65 73 74 63 61 73 65 28 20 64 62 2d 3e 6d 61 testcase( db->ma
1e90a 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 70 50 llocFailed && pP
1e90b 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f arse->nAliasAllo
1e90c 63 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 64 c>0 );. if( d
1e90d 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1e90e 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 ) return 0;.
1e90f 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65 2d 3e memset(&pParse->
1e910 61 41 6c 69 61 73 5b 70 50 61 72 73 65 2d 3e 6e aAlias[pParse->n
1e911 41 6c 69 61 73 41 6c 6c 6f 63 5d 2c 20 30 2c 0a AliasAlloc], 0,.
1e912 20 20 20 20 20 20 20 20 20 20 20 28 70 50 61 72 (pPar
1e913 73 65 2d 3e 6e 41 6c 69 61 73 2d 70 50 61 72 73 se->nAlias-pPars
1e914 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 29 2a e->nAliasAlloc)*
1e915 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 sizeof(pParse->a
1e916 41 6c 69 61 73 5b 30 5d 29 29 3b 0a 20 20 20 20 Alias[0]));.
1e917 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c pParse->nAliasAl
1e918 6c 6f 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 41 loc = pParse->nA
1e919 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 lias;. }. asse
1e91a 72 74 28 20 69 41 6c 69 61 73 3e 30 20 26 26 20 rt( iAlias>0 &&
1e91b 69 41 6c 69 61 73 3c 3d 70 50 61 72 73 65 2d 3e iAlias<=pParse->
1e91c 6e 41 6c 69 61 73 20 29 3b 0a 20 20 69 52 65 67 nAlias );. iReg
1e91d 20 3d 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 = pParse->aAlia
1e91e 73 5b 69 41 6c 69 61 73 2d 31 5d 3b 0a 20 20 69 s[iAlias-1];. i
1e91f 66 28 20 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20 f( iReg==0 ){.
1e920 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69 if( pParse->di
1e921 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 20 29 7b sableColCache ){
1e922 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20 73 71 . iReg = sq
1e923 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 lite3ExprCodeTar
1e924 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 get(pParse, pExp
1e925 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 r, target);.
1e926 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 52 65 }else{. iRe
1e927 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d g = ++pParse->nM
1e928 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 em;. sqlite
1e929 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 3ExprCode(pParse
1e92a 2c 20 70 45 78 70 72 2c 20 69 52 65 67 29 3b 0a , pExpr, iReg);.
1e92b 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 41 pParse->aA
1e92c 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 3d lias[iAlias-1] =
1e92d 20 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d iReg;. }. }
1e92e 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a . return iReg;.
1e92f 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
1e930 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 65 20 e code into the
1e931 63 75 72 72 65 6e 74 20 56 64 62 65 20 74 6f 20 current Vdbe to
1e932 65 76 61 6c 75 61 74 65 20 74 68 65 20 67 69 76 evaluate the giv
1e933 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e en.** expression
1e934 2e 20 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74 . Attempt to st
1e935 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 ore the results
1e936 69 6e 20 72 65 67 69 73 74 65 72 20 22 74 61 72 in register "tar
1e937 67 65 74 22 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 get"..** Return
1e938 74 68 65 20 72 65 67 69 73 74 65 72 20 77 68 65 the register whe
1e939 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 re results are s
1e93a 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 tored..**.** Wit
1e93b 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 h this routine,
1e93c 74 68 65 72 65 20 69 73 20 6e 6f 20 67 75 61 72 there is no guar
1e93d 61 6e 74 65 65 20 74 68 61 74 20 72 65 73 75 6c antee that resul
1e93e 74 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 73 74 ts will.** be st
1e93f 6f 72 65 64 20 69 6e 20 74 61 72 67 65 74 2e 20 ored in target.
1e940 20 54 68 65 20 72 65 73 75 6c 74 20 6d 69 67 68 The result migh
1e941 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 73 t be stored in s
1e942 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 72 65 67 ome other.** reg
1e943 69 73 74 65 72 20 69 66 20 69 74 20 69 73 20 63 ister if it is c
1e944 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64 6f 20 onvenient to do
1e945 73 6f 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 so. The calling
1e946 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 function.** mus
1e947 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 t check the retu
1e948 72 6e 20 63 6f 64 65 20 61 6e 64 20 6d 6f 76 65 rn code and move
1e949 20 74 68 65 20 72 65 73 75 6c 74 73 20 74 6f 20 the results to
1e94a 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a 20 72 the desired.** r
1e94b 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 egister..*/.SQLI
1e94c 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1e94d 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
1e94e 72 67 65 74 28 50 61 72 73 65 20 2a 70 50 61 72 rget(Parse *pPar
1e94f 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c se, Expr *pExpr,
1e950 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 int target){.
1e951 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 Vdbe *v = pParse
1e952 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 54 68 65 ->pVdbe; /* The
1e953 20 56 4d 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 VM under constr
1e954 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 uction */. int
1e955 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 op;
1e956 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 63 /* The opc
1e957 6f 64 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 ode being coded
1e958 2a 2f 0a 20 20 69 6e 74 20 69 6e 52 65 67 20 3d */. int inReg =
1e959 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 20 2f target; /
1e95a 2a 20 52 65 73 75 6c 74 73 20 73 74 6f 72 65 64 * Results stored
1e95b 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 6e 52 in register inR
1e95c 65 67 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 eg */. int regF
1e95d 72 65 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 ree1 = 0;
1e95e 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f /* If non-zero
1e95f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f free this tempo
1e960 72 61 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f rary register */
1e961 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 . int regFree2
1e962 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
1e963 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 If non-zero free
1e964 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 this temporary
1e965 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e register */. in
1e966 74 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 72 34 t r1, r2, r3, r4
1e967 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f ; /* Vario
1e968 75 73 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 us register numb
1e969 65 72 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ers */. sqlite3
1e96a 20 2a 64 62 3b 0a 0a 20 20 64 62 20 3d 20 70 50 *db;.. db = pP
1e96b 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 arse->db;. asse
1e96c 72 74 28 20 76 21 3d 30 20 7c 7c 20 64 62 2d 3e rt( v!=0 || db->
1e96d 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
1e96e 20 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74 assert( target
1e96f 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 >0 && target<=pP
1e970 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 arse->nMem );.
1e971 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 if( v==0 ) retur
1e972 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 n 0;.. if( pExp
1e973 72 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 70 20 3d r==0 ){. op =
1e974 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 TK_NULL;. }els
1e975 65 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70 e{. op = pExp
1e976 72 2d 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 77 69 r->op;. }. swi
1e977 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 tch( op ){. c
1e978 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d ase TK_AGG_COLUM
1e979 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e N: {. AggIn
1e97a 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 fo *pAggInfo = p
1e97b 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a Expr->pAggInfo;.
1e97c 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67 struct Agg
1e97d 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d Info_col *pCol =
1e97e 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c &pAggInfo->aCol
1e97f 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 3b 0a 20 [pExpr->iAgg];.
1e980 20 20 20 20 20 69 66 28 20 21 70 41 67 67 49 6e if( !pAggIn
1e981 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 29 fo->directMode )
1e982 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1e983 28 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30 20 29 ( pCol->iMem>0 )
1e984 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 ;. inReg
1e985 3d 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a 20 20 = pCol->iMem;.
1e986 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1e987 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 67 }else if( pAg
1e988 67 49 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 69 6e gInfo->useSortin
1e989 67 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 gIdx ){.
1e98a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1e98b 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 3(v, OP_Column,
1e98c 70 41 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 69 6e pAggInfo->sortin
1e98d 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 gIdx,.
1e98e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e98f 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 pCol->iSorte
1e990 72 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29 rColumn, target)
1e991 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
1e992 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f . }. /
1e993 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c * Otherwise, fal
1e994 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 l thru into the
1e995 54 4b 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a TK_COLUMN case *
1e996 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 /. }. case
1e997 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 TK_COLUMN: {.
1e998 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 if( pExpr->i
1e999 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20 Table<0 ){.
1e99a 20 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 /* This only
1e99b 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 happens when cod
1e99c 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 ing check constr
1e99d 61 69 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 aints */.
1e99e 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d assert( pParse-
1e99f 3e 63 6b 42 61 73 65 3e 30 20 29 3b 0a 20 20 20 >ckBase>0 );.
1e9a0 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 inReg = pEx
1e9a1 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 70 50 pr->iColumn + pP
1e9a2 61 72 73 65 2d 3e 63 6b 42 61 73 65 3b 0a 20 20 arse->ckBase;.
1e9a3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e9a4 20 20 20 74 65 73 74 63 61 73 65 28 20 28 70 45 testcase( (pE
1e9a5 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f xpr->flags & EP_
1e9a6 41 6e 79 41 66 66 29 21 3d 30 20 29 3b 0a 20 20 AnyAff)!=0 );.
1e9a7 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 inReg = sq
1e9a8 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 lite3ExprCodeGet
1e9a9 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 Column(pParse, p
1e9aa 45 78 70 72 2d 3e 70 54 61 62 2c 0a 20 20 20 20 Expr->pTab,.
1e9ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e9ac 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 pEx
1e9ad 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 45 78 pr->iColumn, pEx
1e9ae 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 pr->iTable, targ
1e9af 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 et,.
1e9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e9b1 20 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 pExpr->flag
1e9b2 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 3b 0a s & EP_AnyAff);.
1e9b3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
1e9b4 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1e9b5 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 ase TK_INTEGER:
1e9b6 7b 0a 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 {. codeInte
1e9b7 67 65 72 28 76 2c 20 70 45 78 70 72 2c 20 30 2c ger(v, pExpr, 0,
1e9b8 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
1e9b9 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1e9ba 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 20 case TK_FLOAT:
1e9bb 7b 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c {. codeReal
1e9bc 28 76 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72 (v, (char*)pExpr
1e9bd 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 ->token.z, pExpr
1e9be 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 30 2c 20 74 61 ->token.n, 0, ta
1e9bf 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 rget);. bre
1e9c0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
1e9c1 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a se TK_STRING: {.
1e9c2 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 sqlite3Deq
1e9c3 75 6f 74 65 45 78 70 72 28 64 62 2c 20 70 45 78 uoteExpr(db, pEx
1e9c4 70 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 pr);. sqlit
1e9c5 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 4f e3VdbeAddOp4(v,O
1e9c6 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 74 61 P_String8, 0, ta
1e9c7 72 67 65 74 2c 20 30 2c 0a 20 20 20 20 20 20 20 rget, 0,.
1e9c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e9c9 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 (char*)pExpr->t
1e9ca 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 oken.z, pExpr->t
1e9cb 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 62 oken.n);. b
1e9cc 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1e9cd 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a case TK_NULL: {.
1e9ce 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1e9cf 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 eAddOp2(v, OP_Nu
1e9d0 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a ll, 0, target);.
1e9d1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1e9d2 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
1e9d3 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 E_OMIT_BLOB_LITE
1e9d4 52 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f RAL. case TK_
1e9d5 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 6e BLOB: {. in
1e9d6 74 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 t n;. const
1e9d7 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 char *z;.
1e9d8 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20 char *zBlob;.
1e9d9 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
1e9da 2d 3e 74 6f 6b 65 6e 2e 6e 3e 3d 33 20 29 3b 0a ->token.n>=3 );.
1e9db 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 assert( pE
1e9dc 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d xpr->token.z[0]=
1e9dd 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 74 ='x' || pExpr->t
1e9de 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 58 27 20 29 oken.z[0]=='X' )
1e9df 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1e9e0 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 31 pExpr->token.z[1
1e9e1 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 ]=='\'' );.
1e9e2 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
1e9e3 74 6f 6b 65 6e 2e 7a 5b 70 45 78 70 72 2d 3e 74 token.z[pExpr->t
1e9e4 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d 27 5c 27 27 20 oken.n-1]=='\''
1e9e5 29 3b 0a 20 20 20 20 20 20 6e 20 3d 20 70 45 78 );. n = pEx
1e9e6 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 2d 20 33 3b pr->token.n - 3;
1e9e7 0a 20 20 20 20 20 20 7a 20 3d 20 28 63 68 61 72 . z = (char
1e9e8 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a *)pExpr->token.z
1e9e9 20 2b 20 32 3b 0a 20 20 20 20 20 20 7a 42 6c 6f + 2;. zBlo
1e9ea 62 20 3d 20 73 71 6c 69 74 65 33 48 65 78 54 6f b = sqlite3HexTo
1e9eb 42 6c 6f 62 28 73 71 6c 69 74 65 33 56 64 62 65 Blob(sqlite3Vdbe
1e9ec 44 62 28 76 29 2c 20 7a 2c 20 6e 29 3b 0a 20 20 Db(v), z, n);.
1e9ed 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1e9ee 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 ddOp4(v, OP_Blob
1e9ef 2c 20 6e 2f 32 2c 20 74 61 72 67 65 74 2c 20 30 , n/2, target, 0
1e9f0 2c 20 7a 42 6c 6f 62 2c 20 50 34 5f 44 59 4e 41 , zBlob, P4_DYNA
1e9f1 4d 49 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 MIC);. brea
1e9f2 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a k;. }.#endif.
1e9f3 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 case TK_VARI
1e9f4 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 ABLE: {. sq
1e9f5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1e9f6 76 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 v, OP_Variable,
1e9f7 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74 pExpr->iTable, t
1e9f8 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 69 66 arget);. if
1e9f9 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e ( pExpr->token.n
1e9fa 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 >1 ){. sq
1e9fb 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1e9fc 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 2a 29 4(v, -1, (char*)
1e9fd 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 pExpr->token.z,
1e9fe 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b pExpr->token.n);
1e9ff 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
1ea00 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1ea01 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52 case TK_REGISTER
1ea02 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 : {. inReg
1ea03 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b = pExpr->iTable;
1ea04 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1ea05 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1ea06 41 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 AS: {. inRe
1ea07 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 70 50 g = codeAlias(pP
1ea08 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 69 54 61 arse, pExpr->iTa
1ea09 62 6c 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 ble, pExpr->pLef
1ea0a 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 t, target);.
1ea0b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1ea0c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1ea0d 49 54 5f 43 41 53 54 0a 20 20 20 20 63 61 73 65 IT_CAST. case
1ea0e 20 54 4b 5f 43 41 53 54 3a 20 7b 0a 20 20 20 20 TK_CAST: {.
1ea0f 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 /* Expressions
1ea10 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 20 of the form:
1ea11 43 41 53 54 28 70 4c 65 66 74 20 41 53 20 74 6f CAST(pLeft AS to
1ea12 6b 65 6e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e ken) */. in
1ea13 74 20 61 66 66 2c 20 74 6f 5f 6f 70 3b 0a 20 20 t aff, to_op;.
1ea14 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 inReg = sqli
1ea15 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 te3ExprCodeTarge
1ea16 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d t(pParse, pExpr-
1ea17 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b >pLeft, target);
1ea18 0a 20 20 20 20 20 20 61 66 66 20 3d 20 73 71 6c . aff = sql
1ea19 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 ite3AffinityType
1ea1a 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b (&pExpr->token);
1ea1b 0a 20 20 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61 . to_op = a
1ea1c 66 66 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f ff - SQLITE_AFF_
1ea1d 54 45 58 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74 TEXT + OP_ToText
1ea1e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ea1f 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 to_op==OP_ToText
1ea20 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 || aff!=SQLI
1ea21 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20 29 TE_AFF_TEXT )
1ea22 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ea23 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 to_op==OP_ToBlob
1ea24 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 || aff!=SQLI
1ea25 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29 TE_AFF_NONE )
1ea26 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ea27 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 to_op==OP_ToNume
1ea28 72 69 63 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 ric || aff!=SQLI
1ea29 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 TE_AFF_NUMERIC )
1ea2a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ea2b 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 to_op==OP_ToInt
1ea2c 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 || aff!=SQLI
1ea2d 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 29 TE_AFF_INTEGER )
1ea2e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ea2f 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c to_op==OP_ToReal
1ea30 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 || aff!=SQLI
1ea31 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 29 TE_AFF_REAL )
1ea32 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1ea33 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 ( to_op==OP_ToTe
1ea34 78 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 xt );. test
1ea35 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f case( to_op==OP_
1ea36 54 6f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 ToBlob );.
1ea37 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d testcase( to_op=
1ea38 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b =OP_ToNumeric );
1ea39 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1ea3a 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 to_op==OP_ToInt
1ea3b 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1ea3c 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f se( to_op==OP_To
1ea3d 52 65 61 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 Real );. if
1ea3e 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20 ( inReg!=target
1ea3f 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1ea40 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1ea41 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c OP_SCopy, inReg,
1ea42 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
1ea43 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 inReg = target
1ea44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1ea45 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1ea46 31 28 76 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 1(v, to_op, inRe
1ea47 67 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 g);. testca
1ea48 73 65 28 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e se( usedAsColumn
1ea49 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 69 6e Cache(pParse, in
1ea4a 52 65 67 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 Reg, inReg) );.
1ea4b 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1ea4c 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 CacheAffinityCha
1ea4d 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 nge(pParse, inRe
1ea4e 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 g, 1);. bre
1ea4f 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ak;. }.#endif
1ea50 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1ea51 43 41 53 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 CAST */. case
1ea52 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 TK_LT:. case
1ea53 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 TK_LE:. case
1ea54 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 TK_GT:. case
1ea55 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 TK_GE:. case
1ea56 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 TK_NE:. case
1ea57 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 TK_EQ: {.
1ea58 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f assert( TK_LT==O
1ea59 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 P_Lt );. as
1ea5a 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f sert( TK_LE==OP_
1ea5b 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Le );. asse
1ea5c 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 rt( TK_GT==OP_Gt
1ea5d 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1ea5e 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 ( TK_GE==OP_Ge )
1ea5f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ea60 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a TK_EQ==OP_Eq );.
1ea61 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1ea62 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 _NE==OP_Ne );.
1ea63 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
1ea64 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 ==TK_LT );.
1ea65 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1ea66 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 K_LE );. te
1ea67 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 stcase( op==TK_G
1ea68 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
1ea69 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 ase( op==TK_GE )
1ea6a 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1ea6b 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 ( op==TK_EQ );.
1ea6c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1ea6d 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 p==TK_NE );.
1ea6e 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 codeCompareOpe
1ea6f 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 rands(pParse, pE
1ea70 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c xpr->pLeft, &r1,
1ea71 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 ®Free1,.
1ea72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ea73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 pE
1ea74 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 xpr->pRight, &r2
1ea75 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
1ea76 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 codeCompare(
1ea77 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1ea78 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 Left, pExpr->pRi
1ea79 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 ght, op,.
1ea7a 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 r1, r
1ea7b 32 2c 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 2, inReg, SQLITE
1ea7c 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 _STOREP2);.
1ea7d 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
1ea7e 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee1==0 );.
1ea7f 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
1ea80 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 e2==0 );. b
1ea81 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1ea82 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0a 20 20 20 case TK_AND:.
1ea83 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0a 20 20 20 case TK_OR:.
1ea84 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 3a 0a 20 case TK_PLUS:.
1ea85 20 20 20 63 61 73 65 20 54 4b 5f 53 54 41 52 3a case TK_STAR:
1ea86 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4d 49 4e . case TK_MIN
1ea87 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f US:. case TK_
1ea88 52 45 4d 3a 0a 20 20 20 20 63 61 73 65 20 54 4b REM:. case TK
1ea89 5f 42 49 54 41 4e 44 3a 0a 20 20 20 20 63 61 73 _BITAND:. cas
1ea8a 65 20 54 4b 5f 42 49 54 4f 52 3a 0a 20 20 20 20 e TK_BITOR:.
1ea8b 63 61 73 65 20 54 4b 5f 53 4c 41 53 48 3a 0a 20 case TK_SLASH:.
1ea8c 20 20 20 63 61 73 65 20 54 4b 5f 4c 53 48 49 46 case TK_LSHIF
1ea8d 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 T:. case TK_R
1ea8e 53 48 49 46 54 3a 20 0a 20 20 20 20 63 61 73 65 SHIFT: . case
1ea8f 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 7b 0a 20 20 TK_CONCAT: {.
1ea90 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 41 assert( TK_A
1ea91 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20 ND==OP_And );.
1ea92 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4f assert( TK_O
1ea93 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a 20 20 20 20 R==OP_Or );.
1ea94 20 20 61 73 73 65 72 74 28 20 54 4b 5f 50 4c 55 assert( TK_PLU
1ea95 53 3d 3d 4f 50 5f 41 64 64 20 29 3b 0a 20 20 20 S==OP_Add );.
1ea96 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4d 49 assert( TK_MI
1ea97 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74 NUS==OP_Subtract
1ea98 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1ea99 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d ( TK_REM==OP_Rem
1ea9a 61 69 6e 64 65 72 20 29 3b 0a 20 20 20 20 20 20 ainder );.
1ea9b 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 41 4e assert( TK_BITAN
1ea9c 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20 29 3b 0a D==OP_BitAnd );.
1ea9d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1ea9e 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72 _BITOR==OP_BitOr
1ea9f 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1eaa0 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44 ( TK_SLASH==OP_D
1eaa1 69 76 69 64 65 20 29 3b 0a 20 20 20 20 20 20 61 ivide );. a
1eaa2 73 73 65 72 74 28 20 54 4b 5f 4c 53 48 49 46 54 ssert( TK_LSHIFT
1eaa3 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 29 ==OP_ShiftLeft )
1eaa4 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1eaa5 54 4b 5f 52 53 48 49 46 54 3d 3d 4f 50 5f 53 68 TK_RSHIFT==OP_Sh
1eaa6 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 20 20 iftRight );.
1eaa7 20 20 61 73 73 65 72 74 28 20 54 4b 5f 43 4f 4e assert( TK_CON
1eaa8 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 61 74 20 29 CAT==OP_Concat )
1eaa9 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1eaaa 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a ( op==TK_AND );.
1eaab 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1eaac 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 20 op==TK_OR );.
1eaad 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1eaae 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a 20 20 20 20 =TK_PLUS );.
1eaaf 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1eab0 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a 20 20 20 20 TK_MINUS );.
1eab1 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1eab2 54 4b 5f 52 45 4d 20 29 3b 0a 20 20 20 20 20 20 TK_REM );.
1eab3 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1eab4 5f 42 49 54 41 4e 44 20 29 3b 0a 20 20 20 20 20 _BITAND );.
1eab5 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1eab6 4b 5f 42 49 54 4f 52 20 29 3b 0a 20 20 20 20 20 K_BITOR );.
1eab7 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1eab8 4b 5f 53 4c 41 53 48 20 29 3b 0a 20 20 20 20 20 K_SLASH );.
1eab9 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1eaba 4b 5f 4c 53 48 49 46 54 20 29 3b 0a 20 20 20 20 K_LSHIFT );.
1eabb 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1eabc 54 4b 5f 52 53 48 49 46 54 20 29 3b 0a 20 20 20 TK_RSHIFT );.
1eabd 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1eabe 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 3b 0a 20 20 =TK_CONCAT );.
1eabf 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 r1 = sqlite3
1eac0 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 ExprCodeTemp(pPa
1eac1 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1eac2 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 t, ®Free1);.
1eac3 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 r2 = sqlite
1eac4 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
1eac5 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 arse, pExpr->pRi
1eac6 67 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b ght, ®Free2);
1eac7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1eac8 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20 beAddOp3(v, op,
1eac9 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b r2, r1, target);
1eaca 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1eacb 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
1eacc 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1eacd 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 regFree2==0 );.
1eace 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1eacf 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d }. case TK_UM
1ead0 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 45 78 INUS: {. Ex
1ead1 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 pr *pLeft = pExp
1ead2 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 r->pLeft;.
1ead3 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b assert( pLeft );
1ead4 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 . if( pLeft
1ead5 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c ->op==TK_FLOAT |
1ead6 7c 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f | pLeft->op==TK_
1ead7 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 INTEGER ){.
1ead8 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 if( pLeft->op
1ead9 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 ==TK_FLOAT ){.
1eada 20 20 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c codeReal
1eadb 28 76 2c 20 28 63 68 61 72 2a 29 70 4c 65 66 74 (v, (char*)pLeft
1eadc 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 4c 65 66 74 ->token.z, pLeft
1eadd 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 31 2c 20 74 61 ->token.n, 1, ta
1eade 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d rget);. }
1eadf 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
1eae0 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 70 codeInteger(v, p
1eae1 4c 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74 29 Left, 1, target)
1eae2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1eae3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1eae4 20 72 65 67 46 72 65 65 31 20 3d 20 72 31 20 3d regFree1 = r1 =
1eae5 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
1eae6 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 eg(pParse);.
1eae7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1eae8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
1eae9 67 65 72 2c 20 30 2c 20 72 31 29 3b 0a 20 20 20 ger, 0, r1);.
1eaea 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 r2 = sqlite
1eaeb 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
1eaec 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
1eaed 66 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a ft, ®Free2);.
1eaee 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1eaef 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
1eaf0 53 75 62 74 72 61 63 74 2c 20 72 32 2c 20 72 31 Subtract, r2, r1
1eaf1 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
1eaf2 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
1eaf3 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 Free2==0 );.
1eaf4 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 67 20 }. inReg
1eaf5 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 = target;.
1eaf6 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1eaf7 20 63 61 73 65 20 54 4b 5f 42 49 54 4e 4f 54 3a case TK_BITNOT:
1eaf8 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 . case TK_NOT
1eaf9 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 : {. assert
1eafa 28 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f 50 5f ( TK_BITNOT==OP_
1eafb 42 69 74 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 BitNot );.
1eafc 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 3d 3d assert( TK_NOT==
1eafd 4f 50 5f 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 OP_Not );.
1eafe 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1eaff 5f 42 49 54 4e 4f 54 20 29 3b 0a 20 20 20 20 20 _BITNOT );.
1eb00 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1eb01 4b 5f 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 72 K_NOT );. r
1eb02 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 1 = sqlite3ExprC
1eb03 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
1eb04 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 pExpr->pLeft, &r
1eb05 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 egFree1);.
1eb06 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
1eb07 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 e1==0 );. i
1eb08 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 nReg = target;.
1eb09 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1eb0a 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 AddOp2(v, op, r1
1eb0b 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20 , inReg);.
1eb0c 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1eb0d 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a case TK_ISNULL:
1eb0e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 . case TK_NOT
1eb0f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 6e NULL: {. in
1eb10 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 61 73 t addr;. as
1eb11 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d sert( TK_ISNULL=
1eb12 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 =OP_IsNull );.
1eb13 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e assert( TK_N
1eb14 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 OTNULL==OP_NotNu
1eb15 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ll );. test
1eb16 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e case( op==TK_ISN
1eb17 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ULL );. tes
1eb18 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f tcase( op==TK_NO
1eb19 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73 TNULL );. s
1eb1a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1eb1b 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
1eb1c 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 1, target);.
1eb1d 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 r1 = sqlite3Ex
1eb1e 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
1eb1f 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
1eb20 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 ®Free1);.
1eb21 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
1eb22 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 Free1==0 );.
1eb23 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 addr = sqlite3
1eb24 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 6f 70 VdbeAddOp1(v, op
1eb25 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c , r1);. sql
1eb26 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1eb27 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74 61 72 , OP_AddImm, tar
1eb28 67 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 get, -1);.
1eb29 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
1eb2a 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 ere(v, addr);.
1eb2b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1eb2c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 . case TK_AGG
1eb2d 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 _FUNCTION: {.
1eb2e 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 AggInfo *pInf
1eb2f 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49 o = pExpr->pAggI
1eb30 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 nfo;. if( p
1eb31 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Info==0 ){.
1eb32 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1eb33 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 sg(pParse, "misu
1eb34 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 3a se of aggregate:
1eb35 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20 20 20 %T",.
1eb36 20 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b &pExpr->span);
1eb37 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1eb38 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 49 inReg = pI
1eb39 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 78 70 72 nfo->aFunc[pExpr
1eb3a 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b 0a 20 20 ->iAgg].iMem;.
1eb3b 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
1eb3c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1eb3d 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a e TK_CONST_FUNC:
1eb3e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e . case TK_FUN
1eb3f 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45 CTION: {. E
1eb40 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d xprList *pList =
1eb41 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 pExpr->pList;.
1eb42 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72 20 3d int nExpr =
1eb43 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e pList ? pList->
1eb44 6e 45 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 20 nExpr : 0;.
1eb45 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a FuncDef *pDef;.
1eb46 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 0a 20 int nId;.
1eb47 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1eb48 2a 7a 49 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 *zId;. int
1eb49 63 6f 6e 73 74 4d 61 73 6b 20 3d 20 30 3b 0a 20 constMask = 0;.
1eb4a 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
1eb4b 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 u8 enc = ENC(d
1eb4c 62 29 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 b);. CollSe
1eb4d 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 0a 20 q *pColl = 0;..
1eb4e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1eb4f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 p==TK_CONST_FUNC
1eb50 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1eb51 73 65 28 20 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 se( op==TK_FUNCT
1eb52 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 7a 49 64 ION );. zId
1eb53 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d = (char*)pExpr-
1eb54 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 >token.z;.
1eb55 6e 49 64 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b nId = pExpr->tok
1eb56 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44 65 66 en.n;. pDef
1eb57 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 = sqlite3FindFu
1eb58 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 49 64 2c 20 nction(db, zId,
1eb59 6e 49 64 2c 20 6e 45 78 70 72 2c 20 65 6e 63 2c nId, nExpr, enc,
1eb5a 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0);. asser
1eb5b 74 28 20 70 44 65 66 21 3d 30 20 29 3b 0a 20 20 t( pDef!=0 );.
1eb5c 20 20 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b if( pList ){
1eb5d 0a 20 20 20 20 20 20 20 20 6e 45 78 70 72 20 3d . nExpr =
1eb5e 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 pList->nExpr;.
1eb5f 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 r1 = sqli
1eb60 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 te3GetTempRange(
1eb61 70 50 61 72 73 65 2c 20 6e 45 78 70 72 29 3b 0a pParse, nExpr);.
1eb62 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
1eb63 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 xprCodeExprList(
1eb64 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 72 pParse, pList, r
1eb65 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 1, 1);. }el
1eb66 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 45 78 70 se{. nExp
1eb67 72 20 3d 20 72 31 20 3d 20 30 3b 0a 20 20 20 20 r = r1 = 0;.
1eb68 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
1eb69 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1eb6a 41 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50 6f ABLE. /* Po
1eb6b 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20 ssibly overload
1eb6c 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 the function if
1eb6d 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
1eb6e 6e 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 nt is. ** a
1eb6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 virtual table c
1eb70 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a olumn.. **.
1eb71 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e 66 ** For inf
1eb72 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c 49 ix functions (LI
1eb73 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58 50 KE, GLOB, REGEXP
1eb74 2c 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73 65 , and MATCH) use
1eb75 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 the. ** se
1eb76 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 6e cond argument, n
1eb77 6f 74 20 74 68 65 20 66 69 72 73 74 2c 20 61 73 ot the first, as
1eb78 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f the argument to
1eb79 20 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20 2a test to. *
1eb7a 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 20 61 * see if it is a
1eb7b 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 column in a vir
1eb7c 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 tual table. Thi
1eb7d 73 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75 73 s is done becaus
1eb7e 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c e. ** the l
1eb7f 65 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 eft operand of i
1eb80 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 nfix functions (
1eb81 74 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20 77 the operand we w
1eb82 61 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 ant to. **
1eb83 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 control overload
1eb84 69 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73 20 ing) ends up as
1eb85 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
1eb86 65 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20 20 ent to the.
1eb87 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 ** function. T
1eb88 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 41 he expression "A
1eb89 20 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 69 glob B" is equi
1eb8a 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 valent to .
1eb8b 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e 20 ** "glob(B,A).
1eb8c 20 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 20 We want to use
1eb8d 74 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f 62 the A in "A glob
1eb8e 20 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20 20 B" to test.
1eb8f 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69 6f ** for functio
1eb90 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20 20 n overloading.
1eb91 42 75 74 20 77 65 20 75 73 65 20 74 68 65 20 42 But we use the B
1eb92 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28 42 term in "glob(B
1eb93 2c 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ,A)".. */.
1eb94 20 20 20 20 20 69 66 28 20 6e 45 78 70 72 3e 3d if( nExpr>=
1eb95 32 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c 61 2 && (pExpr->fla
1eb96 67 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75 6e gs & EP_InfixFun
1eb97 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 c) ){. pD
1eb98 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 ef = sqlite3Vtab
1eb99 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e OverloadFunction
1eb9a 28 64 62 2c 20 70 44 65 66 2c 20 6e 45 78 70 72 (db, pDef, nExpr
1eb9b 2c 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 , pList->a[1].pE
1eb9c 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 xpr);. }els
1eb9d 65 20 69 66 28 20 6e 45 78 70 72 3e 30 20 29 7b e if( nExpr>0 ){
1eb9e 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 . pDef =
1eb9f 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c sqlite3VtabOverl
1eba0 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 oadFunction(db,
1eba1 70 44 65 66 2c 20 6e 45 78 70 72 2c 20 70 4c 69 pDef, nExpr, pLi
1eba2 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b st->a[0].pExpr);
1eba3 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
1eba4 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
1eba5 3c 6e 45 78 70 72 20 26 26 20 69 3c 33 32 3b 20 <nExpr && i<32;
1eba6 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 i++){. if
1eba7 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 ( sqlite3ExprIsC
1eba8 6f 6e 73 74 61 6e 74 28 70 4c 69 73 74 2d 3e 61 onstant(pList->a
1eba9 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 [i].pExpr) ){.
1ebaa 20 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61 73 constMas
1ebab 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20 20 k |= (1<<i);.
1ebac 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1ebad 66 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 f( (pDef->flags
1ebae 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 & SQLITE_FUNC_NE
1ebaf 45 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 21 70 EDCOLL)!=0 && !p
1ebb0 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 Coll ){.
1ebb1 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 pColl = sqlite
1ebb2 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 3ExprCollSeq(pPa
1ebb3 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d rse, pList->a[i]
1ebb4 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 .pExpr);.
1ebb5 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
1ebb6 20 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73 if( pDef->flags
1ebb7 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e & SQLITE_FUNC_N
1ebb8 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 EEDCOLL ){.
1ebb9 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 20 if( !pColl )
1ebba 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c pColl = db->pDfl
1ebbb 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20 20 tColl; .
1ebbc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1ebbd 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 4(v, OP_CollSeq,
1ebbe 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20 0, 0, 0, (char
1ebbf 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c *)pColl, P4_COLL
1ebc0 53 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 SEQ);. }.
1ebc1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1ebc2 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e 63 ddOp4(v, OP_Func
1ebc3 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c tion, constMask,
1ebc4 20 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 r1, target,.
1ebc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ebc6 20 20 20 20 20 28 63 68 61 72 2a 29 70 44 65 66 (char*)pDef
1ebc7 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 , P4_FUNCDEF);.
1ebc8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1ebc9 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 ChangeP5(v, (u8)
1ebca 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66 nExpr);. if
1ebcb 28 20 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 ( nExpr ){.
1ebcc 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 sqlite3Releas
1ebcd 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 eTempRange(pPars
1ebce 65 2c 20 72 31 2c 20 6e 45 78 70 72 29 3b 0a 20 e, r1, nExpr);.
1ebcf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
1ebd0 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 ite3ExprCacheAff
1ebd1 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 inityChange(pPar
1ebd2 73 65 2c 20 72 31 2c 20 6e 45 78 70 72 29 3b 0a se, r1, nExpr);.
1ebd3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1ebd4 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
1ebd5 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a E_OMIT_SUBQUERY.
1ebd6 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 case TK_EXIS
1ebd7 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f TS:. case TK_
1ebd8 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 SELECT: {.
1ebd9 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1ebda 5f 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 _EXISTS );.
1ebdb 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1ebdc 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 K_SELECT );.
1ebdd 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f if( pExpr->iCo
1ebde 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 lumn==0 ){.
1ebdf 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 sqlite3CodeSu
1ebe0 62 73 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 bselect(pParse,
1ebe1 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 pExpr, 0, 0);.
1ebe2 20 20 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 }. inRe
1ebe3 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 g = pExpr->iColu
1ebe4 6d 6e 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b mn;. break;
1ebe5 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1ebe6 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 TK_IN: {. i
1ebe7 6e 74 20 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 30 nt rNotFound = 0
1ebe8 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 4d 61 79 ;. int rMay
1ebe9 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 HaveNull = 0;.
1ebea 20 20 20 20 69 6e 74 20 6a 32 2c 20 6a 33 2c 20 int j2, j3,
1ebeb 6a 34 2c 20 6a 35 3b 0a 20 20 20 20 20 20 63 68 j4, j5;. ch
1ebec 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 ar affinity;.
1ebed 20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 0a 20 int eType;..
1ebee 20 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d VdbeNoopCom
1ebef 6d 65 6e 74 28 28 76 2c 20 22 62 65 67 69 6e 20 ment((v, "begin
1ebf0 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20 74 61 IN expr r%d", ta
1ebf1 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20 65 54 rget));. eT
1ebf2 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e ype = sqlite3Fin
1ebf3 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c dInIndex(pParse,
1ebf4 20 70 45 78 70 72 2c 20 26 72 4d 61 79 48 61 76 pExpr, &rMayHav
1ebf5 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66 eNull);. if
1ebf6 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 ( rMayHaveNull )
1ebf7 7b 0a 20 20 20 20 20 20 20 20 72 4e 6f 74 46 6f {. rNotFo
1ebf8 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e und = ++pParse->
1ebf9 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 nMem;. }..
1ebfa 20 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f /* Figure o
1ebfb 75 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 ut the affinity
1ebfc 74 6f 20 75 73 65 20 74 6f 20 63 72 65 61 74 65 to use to create
1ebfd 20 61 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 a key from the
1ebfe 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a results. **
1ebff 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
1ec00 6f 6e 2e 20 61 66 66 69 6e 69 74 79 53 74 72 20 on. affinityStr
1ec01 73 74 6f 72 65 73 20 61 20 73 74 61 74 69 63 20 stores a static
1ec02 73 74 72 69 6e 67 20 73 75 69 74 61 62 6c 65 20 string suitable
1ec03 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 50 34 20 for. ** P4
1ec04 6f 66 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 of OP_MakeRecord
1ec05 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1ec06 20 61 66 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70 affinity = comp
1ec07 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 arisonAffinity(p
1ec08 45 78 70 72 29 3b 0a 0a 0a 20 20 20 20 20 20 2f Expr);... /
1ec09 2a 20 43 6f 64 65 20 74 68 65 20 3c 65 78 70 72 * Code the <expr
1ec0a 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49 > from "<expr> I
1ec0b 4e 20 28 2e 2e 2e 29 22 2e 20 54 68 65 20 74 65 N (...)". The te
1ec0c 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 20 20 mporary table.
1ec0d 20 20 20 20 2a 2a 20 70 45 78 70 72 2d 3e 69 54 ** pExpr->iT
1ec0e 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 able contains th
1ec0f 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 6d 61 e values that ma
1ec10 6b 65 20 75 70 20 74 68 65 20 28 2e 2e 2e 29 20 ke up the (...)
1ec11 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 set.. */.
1ec12 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 pParse->disa
1ec13 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 bleColCache++;.
1ec14 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1ec15 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 Code(pParse, pEx
1ec16 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 pr->pLeft, targe
1ec17 74 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 t);. pParse
1ec18 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 ->disableColCach
1ec19 65 2d 2d 3b 0a 20 20 20 20 20 20 6a 32 20 3d 20 e--;. j2 =
1ec1a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1ec1b 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 1(v, OP_IsNull,
1ec1c 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 69 target);. i
1ec1d 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 f( eType==IN_IND
1ec1e 45 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 EX_ROWID ){.
1ec1f 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 j3 = sqlite3
1ec20 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
1ec21 5f 4d 75 73 74 42 65 49 6e 74 2c 20 74 61 72 67 _MustBeInt, targ
1ec22 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 34 20 et);. j4
1ec23 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 = sqlite3VdbeAdd
1ec24 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 Op3(v, OP_NotExi
1ec25 73 74 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 sts, pExpr->iTab
1ec26 6c 65 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a le, 0, target);.
1ec27 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1ec28 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1ec29 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67 Integer, 1, targ
1ec2a 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 et);. j5
1ec2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 = sqlite3VdbeAdd
1ec2c 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b Op0(v, OP_Goto);
1ec2d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1ec2e 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
1ec2f 6a 33 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c j3);. sql
1ec30 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
1ec31 28 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 (v, j4);.
1ec32 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1ec33 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
1ec34 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 0, target);.
1ec35 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ec36 20 20 20 72 32 20 3d 20 72 65 67 46 72 65 65 32 r2 = regFree2
1ec37 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
1ec38 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 0a 20 pReg(pParse);..
1ec39 20 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 /* Create
1ec3a 20 61 20 72 65 63 6f 72 64 20 61 6e 64 20 74 65 a record and te
1ec3b 73 74 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 65 st for set membe
1ec3c 72 73 68 69 70 2e 20 49 66 20 74 68 65 20 73 65 rship. If the se
1ec3d 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 t contains.
1ec3e 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 2c ** the value,
1ec3f 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68 then jump to th
1ec40 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74 65 73 e end of the tes
1ec41 74 20 63 6f 64 65 2e 20 54 68 65 20 74 61 72 67 t code. The targ
1ec42 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 et. ** re
1ec43 67 69 73 74 65 72 20 73 74 69 6c 6c 20 63 6f 6e gister still con
1ec44 74 61 69 6e 73 20 74 68 65 20 74 72 75 65 20 28 tains the true (
1ec45 31 29 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 1) value written
1ec46 20 74 6f 20 69 74 20 65 61 72 6c 69 65 72 2e 0a to it earlier..
1ec47 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1ec48 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1ec49 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 dOp4(v, OP_MakeR
1ec4a 65 63 6f 72 64 2c 20 74 61 72 67 65 74 2c 20 31 ecord, target, 1
1ec4b 2c 20 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c , r2, &affinity,
1ec4c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 1);. sql
1ec4d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1ec4e 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c , OP_Integer, 1,
1ec4f 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
1ec50 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 j5 = sqlite3Vd
1ec51 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 beAddOp3(v, OP_F
1ec52 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 ound, pExpr->iTa
1ec53 62 6c 65 2c 20 30 2c 20 72 32 29 3b 0a 0a 20 20 ble, 0, r2);..
1ec54 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
1ec55 73 65 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74 set membership t
1ec56 65 73 74 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 est fails, then
1ec57 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 the result of th
1ec58 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 22 78 e . ** "x
1ec59 20 49 4e 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 IN (...)" expre
1ec5a 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 69 ssion must be ei
1ec5b 74 68 65 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 ther 0 or NULL.
1ec5c 49 66 20 74 68 65 20 73 65 74 0a 20 20 20 20 20 If the set.
1ec5d 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e ** contains n
1ec5e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 o NULL values, t
1ec5f 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 hen the result i
1ec60 73 20 30 2e 20 49 66 20 74 68 65 20 73 65 74 20 s 0. If the set
1ec61 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 . ** cont
1ec62 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 ains one or more
1ec63 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 NULL values, th
1ec64 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 en the result of
1ec65 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 the. **
1ec66 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6c expression is al
1ec67 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 so NULL..
1ec68 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
1ec69 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 20 29 7b 0a rNotFound==0 ){.
1ec6a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 /* Thi
1ec6b 73 20 62 72 61 6e 63 68 20 72 75 6e 73 20 69 66 s branch runs if
1ec6c 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 it is known at
1ec6d 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 28 6e 6f compile time (no
1ec6e 77 29 20 74 68 61 74 20 0a 20 20 20 20 20 20 20 w) that .
1ec6f 20 20 20 2a 2a 20 74 68 65 20 73 65 74 20 63 6f ** the set co
1ec70 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 ntains no NULL v
1ec71 61 6c 75 65 73 2e 20 54 68 69 73 20 68 61 70 70 alues. This happ
1ec72 65 6e 73 20 61 73 20 74 68 65 20 72 65 73 75 6c ens as the resul
1ec73 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f t. ** o
1ec74 66 20 61 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 f a "NOT NULL" c
1ec75 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 68 65 onstraint in the
1ec76 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
1ec77 2e 20 4e 6f 20 6e 65 65 64 0a 20 20 20 20 20 20 . No need.
1ec78 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 ** to test t
1ec79 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 he data structur
1ec7a 65 20 61 74 20 72 75 6e 74 69 6d 65 20 69 6e 20 e at runtime in
1ec7b 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 this case..
1ec7c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1ec7d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1ec7e 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 Op2(v, OP_Intege
1ec7f 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 r, 0, target);.
1ec80 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1ec81 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
1ec82 62 6c 6f 63 6b 20 70 6f 70 75 6c 61 74 65 73 20 block populates
1ec83 74 68 65 20 72 4e 6f 74 46 6f 75 6e 64 20 72 65 the rNotFound re
1ec84 67 69 73 74 65 72 20 77 69 74 68 20 65 69 74 68 gister with eith
1ec85 65 72 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 er NULL.
1ec86 20 20 2a 2a 20 6f 72 20 30 20 28 61 6e 20 69 6e ** or 0 (an in
1ec87 74 65 67 65 72 20 76 61 6c 75 65 29 2e 20 49 66 teger value). If
1ec88 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 the data struct
1ec89 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 ure contains one
1ec8a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 . ** or
1ec8b 20 6d 6f 72 65 20 4e 55 4c 4c 73 2c 20 74 68 65 more NULLs, the
1ec8c 6e 20 73 65 74 20 72 4e 6f 74 46 6f 75 6e 64 20 n set rNotFound
1ec8d 74 6f 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 to NULL. Otherwi
1ec8e 73 65 2c 20 73 65 74 20 69 74 0a 20 20 20 20 20 se, set it.
1ec8f 20 20 20 20 20 2a 2a 20 74 6f 20 30 2e 20 49 66 ** to 0. If
1ec90 20 72 65 67 69 73 74 65 72 20 72 4d 61 79 48 61 register rMayHa
1ec91 76 65 4e 75 6c 6c 20 69 73 20 61 6c 72 65 61 64 veNull is alread
1ec92 79 20 73 65 74 20 74 6f 20 73 6f 6d 65 20 76 61 y set to some va
1ec93 6c 75 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a lue. **
1ec94 20 6f 74 68 65 72 20 74 68 61 6e 20 4e 55 4c 4c other than NULL
1ec95 2c 20 74 68 65 6e 20 74 68 65 20 74 65 73 74 20 , then the test
1ec96 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
1ec97 20 72 75 6e 20 61 6e 64 20 0a 20 20 20 20 20 20 run and .
1ec98 20 20 20 20 2a 2a 20 72 4e 6f 74 46 6f 75 6e 64 ** rNotFound
1ec99 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 70 75 is already popu
1ec9a 6c 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 20 lated..
1ec9b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74 */. st
1ec9c 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
1ec9d 6e 75 6c 6c 52 65 63 6f 72 64 5b 5d 20 3d 20 7b nullRecord[] = {
1ec9e 20 30 78 30 32 2c 20 30 78 30 30 20 7d 3b 0a 20 0x02, 0x00 };.
1ec9f 20 20 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71 j3 = sq
1eca0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
1eca1 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 v, OP_NotNull, r
1eca2 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 MayHaveNull);.
1eca3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1eca4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1eca5 4e 75 6c 6c 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 Null, 0, rNotFou
1eca6 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 nd);. s
1eca7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
1eca8 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 32 2c 20 (v, OP_Blob, 2,
1eca9 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 30 2c rMayHaveNull, 0,
1ecaa 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1ecab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e n
1ecac 75 6c 6c 52 65 63 6f 72 64 2c 20 50 34 5f 53 54 ullRecord, P4_ST
1ecad 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 ATIC);.
1ecae 20 6a 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j4 = sqlite3Vdb
1ecaf 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f eAddOp3(v, OP_Fo
1ecb0 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 und, pExpr->iTab
1ecb1 6c 65 2c 20 30 2c 20 72 4d 61 79 48 61 76 65 4e le, 0, rMayHaveN
1ecb2 75 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ull);.
1ecb3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1ecb4 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
1ecb5 20 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 0, rNotFound);.
1ecb6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1ecb7 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
1ecb8 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 j4);.
1ecb9 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
1ecba 65 72 65 28 76 2c 20 6a 33 29 3b 0a 0a 20 20 20 ere(v, j3);..
1ecbb 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 /* Copy t
1ecbc 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 he value of regi
1ecbd 73 74 65 72 20 72 4e 6f 74 46 6f 75 6e 64 20 28 ster rNotFound (
1ecbe 77 68 69 63 68 20 69 73 20 65 69 74 68 65 72 20 which is either
1ecbf 4e 55 4c 4c 20 6f 72 20 30 29 0a 20 20 20 20 20 NULL or 0).
1ecc0 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 ** into the
1ecc1 20 74 61 72 67 65 74 20 72 65 67 69 73 74 65 72 target register
1ecc2 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20 74 . This will be t
1ecc3 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
1ecc4 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78 . ** ex
1ecc5 70 72 65 73 73 69 6f 6e 2e 0a 20 20 20 20 20 20 pression..
1ecc6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
1ecc7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1ecc8 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 p2(v, OP_Copy, r
1ecc9 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 72 67 65 74 NotFound, target
1ecca 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1eccb 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
1eccc 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
1eccd 2c 20 6a 32 29 3b 0a 20 20 20 20 20 20 73 71 6c , j2);. sql
1ecce 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
1eccf 28 76 2c 20 6a 35 29 3b 0a 20 20 20 20 20 20 56 (v, j5);. V
1ecd0 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
1ecd1 65 6e 64 20 49 4e 20 65 78 70 72 20 72 25 64 22 end IN expr r%d"
1ecd2 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 20 20 20 , target));.
1ecd3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1ecd4 65 6e 64 69 66 0a 20 20 20 20 2f 2a 0a 20 20 20 endif. /*.
1ecd5 20 2a 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e ** x BETWEEN
1ecd6 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a y AND z. **.
1ecd7 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65 ** This is e
1ecd8 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 quivalent to.
1ecd9 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e **. ** x>
1ecda 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 =y AND x<=z.
1ecdb 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73 **. ** X is s
1ecdc 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e tored in pExpr->
1ecdd 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 pLeft.. ** Y
1ecde 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 is stored in pEx
1ecdf 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e pr->pList->a[0].
1ece0 70 45 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 pExpr.. ** Z
1ece1 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 is stored in pEx
1ece2 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e pr->pList->a[1].
1ece3 70 45 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 pExpr.. */.
1ece4 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 case TK_BETWEE
1ece5 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 N: {. Expr
1ece6 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e *pLeft = pExpr->
1ece7 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 73 74 72 pLeft;. str
1ece8 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
1ece9 6d 20 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 m *pLItem = pExp
1ecea 72 2d 3e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 r->pList->a;.
1eceb 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 Expr *pRight
1ecec 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b = pLItem->pExpr;
1eced 0a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 .. codeComp
1ecee 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 areOperands(pPar
1ecef 73 65 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c 20 se, pLeft, &r1,
1ecf0 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 ®Free1,.
1ecf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ecf2 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69 pRi
1ecf3 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 ght, &r2, ®Fr
1ecf4 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ee2);. test
1ecf5 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
1ecf6 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
1ecf7 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
1ecf8 20 29 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 73 );. r3 = s
1ecf9 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
1ecfa 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 (pParse);.
1ecfb 72 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r4 = sqlite3GetT
1ecfc 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
1ecfd 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 codeCompar
1ecfe 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c e(pParse, pLeft,
1ecff 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a pRight, OP_Ge,.
1ed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ed01 20 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 51 r1, r2, r3, SQ
1ed02 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 LITE_STOREP2);.
1ed03 20 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 pLItem++;.
1ed04 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c pRight = pL
1ed05 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 Item->pExpr;.
1ed06 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 sqlite3Releas
1ed07 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
1ed08 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 regFree2);.
1ed09 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 r2 = sqlite3Ex
1ed0a 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
1ed0b 65 2c 20 70 52 69 67 68 74 2c 20 26 72 65 67 46 e, pRight, ®F
1ed0c 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 ree2);. tes
1ed0d 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d tcase( regFree2=
1ed0e 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 =0 );. code
1ed0f 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 Compare(pParse,
1ed10 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f pLeft, pRight, O
1ed11 50 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 34 P_Le, r1, r2, r4
1ed12 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 , SQLITE_STOREP2
1ed13 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1ed14 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
1ed15 5f 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 61 _And, r3, r4, ta
1ed16 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c rget);. sql
1ed17 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
1ed18 65 67 28 70 50 61 72 73 65 2c 20 72 33 29 3b 0a eg(pParse, r3);.
1ed19 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c sqlite3Rel
1ed1a 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
1ed1b 73 65 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 62 se, r4);. b
1ed1c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1ed1d 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b case TK_UPLUS: {
1ed1e 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 . inReg = s
1ed1f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
1ed20 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 rget(pParse, pEx
1ed21 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 pr->pLeft, targe
1ed22 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b t);. break;
1ed23 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 . }.. /*.
1ed24 20 20 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20 ** Form A:.
1ed25 20 20 2a 2a 20 20 20 43 41 53 45 20 78 20 57 48 ** CASE x WH
1ed26 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48 EN e1 THEN r1 WH
1ed27 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e EN e2 THEN r2 ..
1ed28 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 . WHEN eN THEN r
1ed29 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 N ELSE y END.
1ed2a 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 **. ** Form
1ed2b 42 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 B:. ** CASE
1ed2c 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31 WHEN e1 THEN r1
1ed2d 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32 WHEN e2 THEN r2
1ed2e 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 ... WHEN eN THE
1ed2f 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a N rN ELSE y END.
1ed30 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f **. ** Fo
1ed31 72 6d 20 41 20 69 73 20 63 61 6e 20 62 65 20 74 rm A is can be t
1ed32 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20 ransformed into
1ed33 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 the equivalent f
1ed34 6f 72 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73 orm B as follows
1ed35 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 :. ** CASE
1ed36 57 48 45 4e 20 78 3d 65 31 20 54 48 45 4e 20 72 WHEN x=e1 THEN r
1ed37 31 20 57 48 45 4e 20 78 3d 65 32 20 54 48 45 4e 1 WHEN x=e2 THEN
1ed38 20 72 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 r2 .... **
1ed39 20 20 20 20 20 20 57 48 45 4e 20 78 3d 65 4e 20 WHEN x=eN
1ed3a 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 THEN rN ELSE y E
1ed3b 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a ND. **. **
1ed3c 20 58 20 28 69 66 20 69 74 20 65 78 69 73 74 73 X (if it exists
1ed3d 29 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 ) is in pExpr->p
1ed3e 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 Left.. ** Y i
1ed3f 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 52 69 67 s in pExpr->pRig
1ed40 68 74 2e 20 20 54 68 65 20 59 20 69 73 20 61 6c ht. The Y is al
1ed41 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 so optional. If
1ed42 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 there is no.
1ed43 20 2a 2a 20 45 4c 53 45 20 63 6c 61 75 73 65 20 ** ELSE clause
1ed44 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 74 65 72 and no other ter
1ed45 6d 20 6d 61 74 63 68 65 73 2c 20 74 68 65 6e 20 m matches, then
1ed46 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 the result of th
1ed47 65 0a 20 20 20 20 2a 2a 20 65 78 70 72 73 73 69 e. ** exprssi
1ed48 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 on is NULL..
1ed49 2a 2a 20 45 69 20 69 73 20 69 6e 20 70 45 78 70 ** Ei is in pExp
1ed4a 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d r->pList->a[i*2]
1ed4b 20 61 6e 64 20 52 69 20 69 73 20 70 45 78 70 72 and Ri is pExpr
1ed4c 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31 ->pList->a[i*2+1
1ed4d 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a ].. **. **
1ed4e 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 The result of t
1ed4f 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
1ed50 20 74 68 65 20 52 69 20 66 6f 72 20 74 68 65 20 the Ri for the
1ed51 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 45 first matching E
1ed52 69 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 i,. ** or if
1ed53 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 there is no matc
1ed54 68 69 6e 67 20 45 69 2c 20 74 68 65 20 45 4c 53 hing Ei, the ELS
1ed55 45 20 74 65 72 6d 20 59 2c 20 6f 72 20 69 66 20 E term Y, or if
1ed56 74 68 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 there is. **
1ed57 6e 6f 20 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55 no ELSE term, NU
1ed58 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 LL.. */. c
1ed59 61 73 65 20 54 4b 5f 43 41 53 45 3a 20 7b 0a 20 ase TK_CASE: {.
1ed5a 20 20 20 20 20 69 6e 74 20 65 6e 64 4c 61 62 65 int endLabe
1ed5b 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l;
1ed5c 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c /* GOTO l
1ed5d 61 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66 20 abel for end of
1ed5e 43 41 53 45 20 73 74 6d 74 20 2a 2f 0a 20 20 20 CASE stmt */.
1ed5f 20 20 20 69 6e 74 20 6e 65 78 74 43 61 73 65 3b int nextCase;
1ed60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ed61 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 /* GOTO lab
1ed62 65 6c 20 66 6f 72 20 6e 65 78 74 20 57 48 45 4e el for next WHEN
1ed63 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 clause */.
1ed64 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 int nExpr;
1ed65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ed66 20 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72 20 /* 2x number
1ed67 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f of WHEN terms */
1ed68 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 . int i;
1ed69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ed6a 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
1ed6b 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 counter */.
1ed6c 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 ExprList *pELi
1ed6d 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 st;
1ed6e 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 57 /* List of W
1ed6f 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 HEN terms */.
1ed70 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
1ed71 73 74 5f 69 74 65 6d 20 2a 61 4c 69 73 74 65 6c st_item *aListel
1ed72 65 6d 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 em; /* Array of
1ed73 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 WHEN terms */.
1ed74 20 20 20 20 20 45 78 70 72 20 6f 70 43 6f 6d 70 Expr opComp
1ed75 61 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 are;
1ed76 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58 3d /* The X=
1ed77 3d 45 69 20 65 78 70 72 65 73 73 69 6f 6e 20 2a =Ei expression *
1ed78 2f 0a 20 20 20 20 20 20 45 78 70 72 20 63 61 63 /. Expr cac
1ed79 68 65 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 heX;
1ed7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 /* Cac
1ed7b 68 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 58 hed expression X
1ed7c 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a */. Expr *
1ed7d 70 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pX;
1ed7e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1ed7f 68 65 20 58 20 65 78 70 72 65 73 73 69 6f 6e 20 he X expression
1ed80 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 */. Expr *p
1ed81 54 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 Test = 0;
1ed82 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58 3d /* X=
1ed83 3d 45 69 20 28 66 6f 72 6d 20 41 29 20 6f 72 20 =Ei (form A) or
1ed84 6a 75 73 74 20 45 69 20 28 66 6f 72 6d 20 42 29 just Ei (form B)
1ed85 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72 */.. asser
1ed86 74 28 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 3b t(pExpr->pList);
1ed87 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 28 70 . assert((p
1ed88 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 Expr->pList->nEx
1ed89 70 72 20 25 20 32 29 20 3d 3d 20 30 29 3b 0a 20 pr % 2) == 0);.
1ed8a 20 20 20 20 20 61 73 73 65 72 74 28 70 45 78 70 assert(pExp
1ed8b 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 r->pList->nExpr
1ed8c 3e 20 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69 > 0);. pELi
1ed8d 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 st = pExpr->pLis
1ed8e 74 3b 0a 20 20 20 20 20 20 61 4c 69 73 74 65 6c t;. aListel
1ed8f 65 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a em = pEList->a;.
1ed90 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20 70 45 nExpr = pE
1ed91 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 List->nExpr;.
1ed92 20 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71 endLabel = sq
1ed93 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
1ed94 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28 el(v);. if(
1ed95 20 28 70 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c (pX = pExpr->pL
1ed96 65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 eft)!=0 ){.
1ed97 20 20 20 63 61 63 68 65 58 20 3d 20 2a 70 58 3b cacheX = *pX;
1ed98 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
1ed99 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f e( pX->op==TK_CO
1ed9a 4c 55 4d 4e 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d LUMN || pX->op==
1ed9b 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20 TK_REGISTER );.
1ed9c 20 20 20 20 20 20 20 63 61 63 68 65 58 2e 69 54 cacheX.iT
1ed9d 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78 able = sqlite3Ex
1ed9e 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
1ed9f 65 2c 20 70 58 2c 20 26 72 65 67 46 72 65 65 31 e, pX, ®Free1
1eda0 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 );. testc
1eda1 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 ase( regFree1==0
1eda2 20 29 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 );. cach
1eda3 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 eX.op = TK_REGIS
1eda4 54 45 52 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 TER;. opC
1eda5 6f 6d 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45 ompare.op = TK_E
1eda6 51 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d Q;. opCom
1eda7 70 61 72 65 2e 70 4c 65 66 74 20 3d 20 26 63 61 pare.pLeft = &ca
1eda8 63 68 65 58 3b 0a 20 20 20 20 20 20 20 20 70 54 cheX;. pT
1eda9 65 73 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65 est = &opCompare
1edaa 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1edab 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 pParse->disableC
1edac 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 olCache++;.
1edad 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 for(i=0; i<nExp
1edae 72 3b 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20 r; i=i+2){.
1edaf 20 20 20 69 66 28 20 70 58 20 29 7b 0a 20 20 20 if( pX ){.
1edb0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1edb1 54 65 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 Test!=0 );.
1edb2 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 opCompare.p
1edb3 52 69 67 68 74 20 3d 20 61 4c 69 73 74 65 6c 65 Right = aListele
1edb4 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 m[i].pExpr;.
1edb5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1edb6 20 20 20 20 20 70 54 65 73 74 20 3d 20 61 4c 69 pTest = aLi
1edb7 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b stelem[i].pExpr;
1edb8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1edb9 20 20 20 6e 65 78 74 43 61 73 65 20 3d 20 73 71 nextCase = sq
1edba 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
1edbb 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 74 el(v);. t
1edbc 65 73 74 63 61 73 65 28 20 70 54 65 73 74 2d 3e estcase( pTest->
1edbd 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c op==TK_COLUMN ||
1edbe 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 52 pTest->op==TK_R
1edbf 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 EGISTER );.
1edc0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 sqlite3ExprIf
1edc1 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 False(pParse, pT
1edc2 65 73 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53 est, nextCase, S
1edc3 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c QLITE_JUMPIFNULL
1edc4 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 );. testc
1edc5 61 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 ase( aListelem[i
1edc6 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 +1].pExpr->op==T
1edc7 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 K_COLUMN );.
1edc8 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c testcase( aL
1edc9 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 istelem[i+1].pEx
1edca 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 pr->op==TK_REGIS
1edcb 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 TER );. s
1edcc 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
1edcd 50 61 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d Parse, aListelem
1edce 5b 69 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72 [i+1].pExpr, tar
1edcf 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 get);. sq
1edd0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1edd1 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 v, OP_Goto, 0, e
1edd2 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 ndLabel);.
1edd3 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 sqlite3VdbeRes
1edd4 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78 olveLabel(v, nex
1edd5 74 43 61 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a tCase);. }.
1edd6 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
1edd7 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 >pRight ){.
1edd8 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f sqlite3ExprCo
1edd9 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 de(pParse, pExpr
1edda 2d 3e 70 52 69 67 68 74 2c 20 74 61 72 67 65 74 ->pRight, target
1eddb 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1eddc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1eddd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1edde 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 Null, 0, target)
1eddf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1ede0 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c sqlite3VdbeResol
1ede1 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61 veLabel(v, endLa
1ede2 62 65 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 bel);. asse
1ede3 72 74 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 rt( pParse->disa
1ede4 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b bleColCache>0 );
1ede5 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 . pParse->d
1ede6 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d isableColCache--
1ede7 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1ede8 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
1ede9 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1edea 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 41 49 . case TK_RAI
1edeb 53 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 SE: {. if(
1edec 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 !pParse->trigSta
1eded 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ck ){. sq
1edee 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1edef 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 arse,.
1edf0 20 20 20 20 20 20 20 20 20 20 20 20 20 22 52 41 "RA
1edf1 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c 79 20 62 ISE() may only b
1edf2 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 61 20 e used within a
1edf3 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 22 trigger-program"
1edf4 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
1edf5 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 n 0;. }.
1edf6 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 if( pExpr->iC
1edf7 6f 6c 75 6d 6e 21 3d 4f 45 5f 49 67 6e 6f 72 65 olumn!=OE_Ignore
1edf8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 61 73 73 ){. ass
1edf9 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c ert( pExpr->iCol
1edfa 75 6d 6e 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b umn==OE_Rollback
1edfb 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ||.
1edfc 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c pExpr->iCol
1edfd 75 6d 6e 20 3d 3d 20 4f 45 5f 41 62 6f 72 74 20 umn == OE_Abort
1edfe 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ||.
1edff 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 pExpr->iColu
1ee00 6d 6e 20 3d 3d 20 4f 45 5f 46 61 69 6c 20 29 3b mn == OE_Fail );
1ee01 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 . sqlite
1ee02 33 44 65 71 75 6f 74 65 45 78 70 72 28 64 62 2c 3DequoteExpr(db,
1ee03 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 pExpr);.
1ee04 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1ee05 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 Op4(v, OP_Halt,
1ee06 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
1ee07 54 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d T, pExpr->iColum
1ee08 6e 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 n, 0,.
1ee09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 (c
1ee0a 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 har*)pExpr->toke
1ee0b 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 n.z, pExpr->toke
1ee0c 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c n.n);. } el
1ee0d 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 61 73 se {. as
1ee0e 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f sert( pExpr->iCo
1ee0f 6c 75 6d 6e 20 3d 3d 20 4f 45 5f 49 67 6e 6f 72 lumn == OE_Ignor
1ee10 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 e );. sq
1ee11 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1ee12 76 2c 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 v, OP_ContextPop
1ee13 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 , 0, 0);.
1ee14 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1ee15 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 Op2(v, OP_Goto,
1ee16 30 2c 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 0, pParse->trigS
1ee17 74 61 63 6b 2d 3e 69 67 6e 6f 72 65 4a 75 6d 70 tack->ignoreJump
1ee18 29 3b 0a 20 20 20 20 20 20 20 20 20 56 64 62 65 );. Vdbe
1ee19 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 61 69 Comment((v, "rai
1ee1a 73 65 28 49 47 4e 4f 52 45 29 22 29 29 3b 0a 20 se(IGNORE)"));.
1ee1b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
1ee1c 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ak;. }.#endif
1ee1d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 . }. sqlite3Re
1ee1e 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
1ee1f 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a rse, regFree1);.
1ee20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
1ee21 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
1ee22 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72 65 74 regFree2);. ret
1ee23 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a urn inReg;.}../*
1ee24 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
1ee25 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6e e to evaluate an
1ee26 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 expression and
1ee27 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
1ee28 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67 69 s.** into a regi
1ee29 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68 ster. Return th
1ee2a 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 e register numbe
1ee2b 72 20 77 68 65 72 65 20 74 68 65 20 72 65 73 75 r where the resu
1ee2c 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 lts.** are store
1ee2d 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
1ee2e 72 65 67 69 73 74 65 72 20 69 73 20 61 20 74 65 register is a te
1ee2f 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 mporary register
1ee30 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 65 61 that can be dea
1ee31 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 68 65 llocated,.** the
1ee32 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75 6d 62 n write its numb
1ee33 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e 20 20 er into *pReg.
1ee34 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 72 65 If the result re
1ee35 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a 2a 2a gister is not.**
1ee36 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20 74 68 a temporary, th
1ee37 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74 6f 20 en set *pReg to
1ee38 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f zero..*/.SQLITE_
1ee39 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1ee3a 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
1ee3b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
1ee3c 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
1ee3d 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20 72 31 *pReg){. int r1
1ee3e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
1ee3f 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
1ee40 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 int r2 = sqlite3
1ee41 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 ExprCodeTarget(p
1ee42 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72 31 Parse, pExpr, r1
1ee43 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72 31 20 );. if( r2==r1
1ee44 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 72 ){. *pReg = r
1ee45 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
1ee46 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
1ee47 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 mpReg(pParse, r1
1ee48 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 30 );. *pReg = 0
1ee49 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1ee4a 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 2;.}../*.** Gene
1ee4b 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 rate code that w
1ee4c 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65 78 70 ill evaluate exp
1ee4d 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61 6e ression pExpr an
1ee4e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 d store the.** r
1ee4f 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 esults in regist
1ee50 65 72 20 74 61 72 67 65 74 2e 20 20 54 68 65 20 er target. The
1ee51 72 65 73 75 6c 74 73 20 61 72 65 20 67 75 61 72 results are guar
1ee52 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65 61 72 anteed to appear
1ee53 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20 .** in register
1ee54 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 target..*/.SQLIT
1ee55 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1ee56 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 50 61 lite3ExprCode(Pa
1ee57 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
1ee58 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 r *pExpr, int ta
1ee59 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 6e 52 rget){. int inR
1ee5a 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 74 eg;.. assert( t
1ee5b 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 65 arget>0 && targe
1ee5c 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 t<=pParse->nMem
1ee5d 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 6c );. inReg = sql
1ee5e 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 ite3ExprCodeTarg
1ee5f 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 et(pParse, pExpr
1ee60 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73 , target);. ass
1ee61 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56 64 ert( pParse->pVd
1ee62 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 be || pParse->db
1ee63 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1ee64 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21 3d 74 ;. if( inReg!=t
1ee65 61 72 67 65 74 20 26 26 20 70 50 61 72 73 65 2d arget && pParse-
1ee66 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73 71 >pVdbe ){. sq
1ee67 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1ee68 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f pParse->pVdbe, O
1ee69 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 P_SCopy, inReg,
1ee6a 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20 72 target);. }. r
1ee6b 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d 0a eturn target;.}.
1ee6c 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
1ee6d 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 74 code that evalut
1ee6e 65 73 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 es the given exp
1ee6f 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75 74 73 ression and puts
1ee70 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 the result.** i
1ee71 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65 n register targe
1ee72 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61 t..**.** Also ma
1ee73 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ke a copy of the
1ee74 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 73 75 expression resu
1ee75 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65 72 lts into another
1ee76 20 22 63 61 63 68 65 22 20 72 65 67 69 73 74 65 "cache" registe
1ee77 72 0a 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79 20 r.** and modify
1ee78 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 the expression s
1ee79 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 o that the next
1ee7a 74 69 6d 65 20 69 74 20 69 73 20 65 76 61 6c 75 time it is evalu
1ee7b 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65 73 ated,.** the res
1ee7c 75 6c 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66 ult is a copy of
1ee7d 20 74 68 65 20 63 61 63 68 65 20 72 65 67 69 73 the cache regis
1ee7e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ter..**.** This
1ee7f 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
1ee80 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 for expressions
1ee81 74 68 61 74 20 61 72 65 20 75 73 65 64 20 6d 75 that are used mu
1ee82 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65 73 ltiple .** times
1ee83 2e 20 20 54 68 65 79 20 61 72 65 20 65 76 61 6c . They are eval
1ee84 75 61 74 65 64 20 6f 6e 63 65 20 61 6e 64 20 74 uated once and t
1ee85 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 he results of th
1ee86 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 e expression.**
1ee87 61 72 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a 53 are reused..*/.S
1ee88 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1ee89 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
1ee8a 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65 20 eAndCache(Parse
1ee8b 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
1ee8c 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 Expr, int target
1ee8d 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 ){. Vdbe *v = p
1ee8e 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 Parse->pVdbe;.
1ee8f 69 6e 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e 52 int inReg;. inR
1ee90 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 eg = sqlite3Expr
1ee91 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 Code(pParse, pEx
1ee92 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 pr, target);. a
1ee93 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 ssert( target>0
1ee94 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e );. if( pExpr->
1ee95 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 op!=TK_REGISTER
1ee96 29 7b 20 20 0a 20 20 20 20 69 6e 74 20 69 4d 65 ){ . int iMe
1ee97 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d 20 2b 2b m;. iMem = ++
1ee98 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
1ee99 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1ee9a 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 Op2(v, OP_Copy,
1ee9b 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 inReg, iMem);.
1ee9c 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
1ee9d 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70 45 78 70 = iMem;. pExp
1ee9e 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 r->op = TK_REGIS
1ee9f 54 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 TER;. }. retur
1eea0 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a n inReg;.}../*.*
1eea1 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 * Return TRUE if
1eea2 20 70 45 78 70 72 20 69 73 20 61 6e 20 63 6f 6e pExpr is an con
1eea3 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e stant expression
1eea4 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 that is appropr
1eea5 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63 74 iate.** for fact
1eea6 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c oring out of a l
1eea7 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72 69 61 74 oop. Appropriat
1eea8 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 e expressions ar
1eea9 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 e:.**.** * A
1eeaa 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 ny expression th
1eeab 61 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 at evaluates to
1eeac 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63 6f two or more opco
1eead 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 des..**.** *
1eeae 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72 2c Any OP_Integer,
1eeaf 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74 72 OP_Real, OP_Str
1eeb0 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50 ing, OP_Blob, OP
1eeb1 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20 _Null, .**
1eeb2 20 6f 72 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 or OP_Variable
1eeb3 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 that does not ne
1eeb4 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20 ed to be placed
1eeb5 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20 20 20 73 in a .** s
1eeb6 70 65 63 69 66 69 63 20 72 65 67 69 73 74 65 72 pecific register
1eeb7 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 ..**.** There is
1eeb8 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61 63 no point in fac
1eeb9 74 6f 72 69 6e 67 20 6f 75 74 20 73 69 6e 67 6c toring out singl
1eeba 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f e-instruction co
1eebb 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 73 nstant.** expres
1eebc 73 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 sions that need
1eebd 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 to be placed in
1eebe 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 a particular reg
1eebf 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20 63 ister. .** We c
1eec0 6f 75 6c 64 20 66 61 63 74 6f 72 20 74 68 65 6d ould factor them
1eec1 20 6f 75 74 2c 20 62 75 74 20 74 68 65 6e 20 77 out, but then w
1eec2 65 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 61 e would end up a
1eec3 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53 dding an.** OP_S
1eec4 43 6f 70 79 20 69 6e 73 74 72 75 63 74 69 6f 6e Copy instruction
1eec5 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c to move the val
1eec6 75 65 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 72 ue into the corr
1eec7 65 63 74 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 ect register.**
1eec8 6c 61 74 65 72 2e 20 20 57 65 20 6d 69 67 68 74 later. We might
1eec9 20 61 73 20 77 65 6c 6c 20 6a 75 73 74 20 75 73 as well just us
1eeca 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69 e the original i
1eecb 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a 2a nstruction and.*
1eecc 2a 20 61 76 6f 69 64 20 74 68 65 20 4f 50 5f 53 * avoid the OP_S
1eecd 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Copy..*/.static
1eece 69 6e 74 20 69 73 41 70 70 72 6f 70 72 69 61 74 int isAppropriat
1eecf 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 78 eForFactoring(Ex
1eed0 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 73 pr *p){. if( !s
1eed1 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
1eed2 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 tantNotJoin(p) )
1eed3 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 {. return 0;
1eed4 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e /* Only constan
1eed5 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 t expressions ar
1eed6 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f e appropriate fo
1eed7 72 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a 20 r factoring */.
1eed8 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 }. if( (p->fla
1eed9 67 73 20 26 20 45 50 5f 46 69 78 65 64 44 65 73 gs & EP_FixedDes
1eeda 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 t)==0 ){. ret
1eedb 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20 63 urn 1; /* Any c
1eedc 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f 75 74 20 onstant without
1eedd 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 a fixed destinat
1eede 69 6f 6e 20 69 73 20 61 70 70 72 6f 70 72 69 61 ion is appropria
1eedf 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c te */. }. whil
1eee0 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c e( p->op==TK_UPL
1eee1 55 53 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65 66 US ) p = p->pLef
1eee2 74 3b 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e t;. switch( p->
1eee3 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 op ){.#ifndef SQ
1eee4 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c LITE_OMIT_BLOB_L
1eee5 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 ITERAL. case
1eee6 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a TK_BLOB:.#endif.
1eee7 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 case TK_VARI
1eee8 41 42 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 ABLE:. case T
1eee9 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 K_INTEGER:. c
1eeea 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 ase TK_FLOAT:.
1eeeb 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a case TK_NULL:.
1eeec 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 case TK_STRI
1eeed 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 NG: {. test
1eeee 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f case( p->op==TK_
1eeef 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20 20 74 65 BLOB );. te
1eef0 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 stcase( p->op==T
1eef1 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a 20 20 K_VARIABLE );.
1eef2 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d testcase( p-
1eef3 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 >op==TK_INTEGER
1eef4 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1eef5 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f e( p->op==TK_FLO
1eef6 41 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 AT );. test
1eef7 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f case( p->op==TK_
1eef8 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 NULL );. te
1eef9 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 stcase( p->op==T
1eefa 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20 20 20 20 K_STRING );.
1eefb 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73 74 /* Single-inst
1eefc 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 ruction constant
1eefd 73 20 77 69 74 68 20 61 20 66 69 78 65 64 20 64 s with a fixed d
1eefe 65 73 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a 20 estination are.
1eeff 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 64 ** better d
1ef00 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49 66 one in-line. If
1ef01 20 77 65 20 66 61 63 74 6f 72 20 74 68 65 6d 2c we factor them,
1ef02 20 74 68 65 79 20 77 69 6c 6c 20 6a 75 73 74 20 they will just
1ef03 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20 75 70 20 end. ** up
1ef04 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f 50 generating an OP
1ef05 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20 74 _SCopy to move t
1ef06 68 65 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 he value to the
1ef07 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 destination.
1ef08 20 20 2a 2a 20 72 65 67 69 73 74 65 72 2e 20 2a ** register. *
1ef09 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 /. return 0
1ef0a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1ef0b 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 TK_UMINUS: {.
1ef0c 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 if( p->pLef
1ef0d 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 t->op==TK_FLOAT
1ef0e 7c 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d || p->pLeft->op=
1ef0f 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 =TK_INTEGER ){.
1ef10 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 return 0
1ef11 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 ;. }.
1ef12 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1ef13 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
1ef14 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1ef15 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b . }. return 1;
1ef16 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 .}../*.** If pEx
1ef17 70 72 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 pr is a constant
1ef18 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 expression that
1ef19 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 is appropriate
1ef1a 66 6f 72 0a 2a 2a 20 66 61 63 74 6f 72 69 6e 67 for.** factoring
1ef1b 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 out of a loop,
1ef1c 74 68 65 6e 20 65 76 61 6c 75 61 74 65 20 74 68 then evaluate th
1ef1d 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 e expression.**
1ef1e 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 20 into a register
1ef1f 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 68 65 20 and convert the
1ef20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 expression into
1ef21 61 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 2a 2a a TK_REGISTER.**
1ef22 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a expression..*/.
1ef23 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c 43 static int evalC
1ef24 6f 6e 73 74 45 78 70 72 28 57 61 6c 6b 65 72 20 onstExpr(Walker
1ef25 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a *pWalker, Expr *
1ef26 70 45 78 70 72 29 7b 0a 20 20 50 61 72 73 65 20 pExpr){. Parse
1ef27 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 *pParse = pWalke
1ef28 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73 77 69 r->pParse;. swi
1ef29 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 tch( pExpr->op )
1ef2a 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 {. case TK_RE
1ef2b 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 GISTER: {.
1ef2c 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
1ef2d 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 case TK_FUNC
1ef2e 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 TION:. case T
1ef2f 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a K_AGG_FUNCTION:.
1ef30 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 case TK_CONS
1ef31 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20 20 T_FUNC: {.
1ef32 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73 /* The arguments
1ef33 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 68 to a function h
1ef34 61 76 65 20 61 20 66 69 78 65 64 20 64 65 73 74 ave a fixed dest
1ef35 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a ination.. *
1ef36 2a 20 4d 61 72 6b 20 74 68 65 6d 20 74 68 69 73 * Mark them this
1ef37 20 77 61 79 20 74 6f 20 61 76 6f 69 64 20 67 65 way to avoid ge
1ef38 6e 65 72 61 74 65 64 20 75 6e 6e 65 65 64 65 64 nerated unneeded
1ef39 20 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20 20 20 OP_SCopy.
1ef3a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e ** instructions.
1ef3b 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 . */.
1ef3c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
1ef3d 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b = pExpr->pList;
1ef3e 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 . if( pList
1ef3f 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
1ef40 69 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 i = pList->nExpr
1ef41 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 ;. struct
1ef42 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1ef43 70 49 74 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61 pItem = pList->a
1ef44 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 ;. for(;
1ef45 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b i>0; i--, pItem+
1ef46 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
1ef47 28 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 29 ( pItem->pExpr )
1ef48 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 66 pItem->pExpr->f
1ef49 6c 61 67 73 20 7c 3d 20 45 50 5f 46 69 78 65 64 lags |= EP_Fixed
1ef4a 44 65 73 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a Dest;. }.
1ef4b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
1ef4c 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
1ef4d 20 69 66 28 20 69 73 41 70 70 72 6f 70 72 69 61 if( isAppropria
1ef4e 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 70 teForFactoring(p
1ef4f 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 Expr) ){. int
1ef50 20 72 31 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e r1 = ++pParse->
1ef51 6e 4d 65 6d 3b 0a 20 20 20 20 69 6e 74 20 72 32 nMem;. int r2
1ef52 3b 0a 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 ;. r2 = sqlit
1ef53 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 e3ExprCodeTarget
1ef54 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
1ef55 72 31 29 3b 0a 20 20 20 20 69 66 28 20 72 31 21 r1);. if( r1!
1ef56 3d 72 32 20 29 20 73 71 6c 69 74 65 33 52 65 6c =r2 ) sqlite3Rel
1ef57 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
1ef58 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 70 45 78 se, r1);. pEx
1ef59 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 pr->op = TK_REGI
1ef5a 53 54 45 52 3b 0a 20 20 20 20 70 45 78 70 72 2d STER;. pExpr-
1ef5b 3e 69 54 61 62 6c 65 20 3d 20 72 32 3b 0a 20 20 >iTable = r2;.
1ef5c 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 return WRC_Pru
1ef5d 6e 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ne;. }. return
1ef5e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d WRC_Continue;.}
1ef5f 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 65 76 61 6c 75 ../*.** Preevalu
1ef60 61 74 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 ate constant sub
1ef61 65 78 70 72 65 73 73 69 6f 6e 73 20 77 69 74 68 expressions with
1ef62 69 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f in pExpr and sto
1ef63 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 re the.** result
1ef64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 2e 20 s in registers.
1ef65 20 4d 6f 64 69 66 79 20 70 45 78 70 72 20 73 6f Modify pExpr so
1ef66 20 74 68 61 74 20 74 68 65 20 63 6f 6e 73 74 61 that the consta
1ef67 6e 74 20 73 75 62 65 78 70 72 65 73 69 6f 6e 73 nt subexpresions
1ef68 0a 2a 2a 20 61 72 65 20 54 4b 5f 52 45 47 49 53 .** are TK_REGIS
1ef69 54 45 52 20 6f 70 63 6f 64 65 73 20 74 68 61 74 TER opcodes that
1ef6a 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70 72 refer to the pr
1ef6b 65 63 6f 6d 70 75 74 65 64 20 76 61 6c 75 65 73 ecomputed values
1ef6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1ef6d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1ef6e 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 ExprCodeConstant
1ef6f 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c s(Parse *pParse,
1ef70 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 Expr *pExpr){.
1ef71 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 Walker w;. w.x
1ef72 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 ExprCallback = e
1ef73 76 61 6c 43 6f 6e 73 74 45 78 70 72 3b 0a 20 20 valConstExpr;.
1ef74 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 w.xSelectCallbac
1ef75 6b 20 3d 20 30 3b 0a 20 20 77 2e 70 50 61 72 73 k = 0;. w.pPars
1ef76 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71 e = pParse;. sq
1ef77 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 lite3WalkExpr(&w
1ef78 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 0a 2f 2a , pExpr);.}.../*
1ef79 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
1ef7a 65 20 74 68 61 74 20 70 75 73 68 65 73 20 74 68 e that pushes th
1ef7b 65 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72 79 e value of every
1ef7c 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 element of the
1ef7d 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 given.** express
1ef7e 69 6f 6e 20 6c 69 73 74 20 69 6e 74 6f 20 61 20 ion list into a
1ef7f 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69 sequence of regi
1ef80 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 sters beginning
1ef81 61 74 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a at target..**.**
1ef82 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1ef83 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 65 er of elements e
1ef84 76 61 6c 75 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c valuated..*/.SQL
1ef85 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1ef86 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 sqlite3ExprCodeE
1ef87 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65 xprList(. Parse
1ef88 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a *pParse, /*
1ef89 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
1ef8a 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
1ef8b 70 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 pList, /* The
1ef8c 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 expression list
1ef8d 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 to be coded */.
1ef8e 20 69 6e 74 20 74 61 72 67 65 74 2c 20 20 20 20 int target,
1ef8f 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 /* Where to
1ef90 77 72 69 74 65 20 72 65 73 75 6c 74 73 20 2a 2f write results */
1ef91 0a 20 20 69 6e 74 20 64 6f 48 61 72 64 43 6f 70 . int doHardCop
1ef92 79 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 61 20 y /* Make a
1ef93 68 61 72 64 20 63 6f 70 79 20 6f 66 20 65 76 65 hard copy of eve
1ef94 72 79 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b ry element */.){
1ef95 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 . struct ExprLi
1ef96 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
1ef97 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 61 73 int i, n;. as
1ef98 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 sert( pList!=0 )
1ef99 3b 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67 ;. assert( targ
1ef9a 65 74 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 70 4c et>0 );. n = pL
1ef9b 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 66 6f ist->nExpr;. fo
1ef9c 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 r(pItem=pList->a
1ef9d 2c 20 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c , i=0; i<n; i++,
1ef9e 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 pItem++){. i
1ef9f 66 28 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 f( pItem->iAlias
1efa0 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 ){. int iR
1efa1 65 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 70 eg = codeAlias(p
1efa2 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 69 41 Parse, pItem->iA
1efa3 6c 69 61 73 2c 20 70 49 74 65 6d 2d 3e 70 45 78 lias, pItem->pEx
1efa4 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 pr, target+i);.
1efa5 20 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 Vdbe *v = s
1efa6 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
1efa7 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 66 28 arse);. if(
1efa8 20 69 52 65 67 21 3d 74 61 72 67 65 74 2b 69 20 iReg!=target+i
1efa9 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1efaa 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1efab 4f 50 5f 53 43 6f 70 79 2c 20 69 52 65 67 2c 20 OP_SCopy, iReg,
1efac 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20 target+i);.
1efad 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1efae 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
1efaf 6f 64 65 28 70 50 61 72 73 65 2c 20 70 49 74 65 ode(pParse, pIte
1efb0 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74 m->pExpr, target
1efb1 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 +i);. }. i
1efb2 66 28 20 64 6f 48 61 72 64 43 6f 70 79 20 29 7b f( doHardCopy ){
1efb3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
1efb4 70 72 48 61 72 64 43 6f 70 79 28 70 50 61 72 73 prHardCopy(pPars
1efb5 65 2c 20 74 61 72 67 65 74 2c 20 6e 29 3b 0a 20 e, target, n);.
1efb6 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1efb7 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 n n;.}../*.** Ge
1efb8 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 nerate code for
1efb9 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 a boolean expres
1efba 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 sion such that a
1efbb 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a jump is made.**
1efbc 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 to the label "d
1efbd 65 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72 est" if the expr
1efbe 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20 62 ession is true b
1efbf 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 ut execution.**
1efc0 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 continues straig
1efc1 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65 ht thru if the e
1efc2 78 70 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c xpression is fal
1efc3 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 se..**.** If the
1efc4 20 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c expression eval
1efc5 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e uates to NULL (n
1efc6 65 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 either true nor
1efc7 66 61 6c 73 65 29 2c 20 74 68 65 6e 0a 2a 2a 20 false), then.**
1efc8 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 take the jump if
1efc9 20 74 68 65 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 the jumpIfNull
1efca 66 6c 61 67 20 69 73 20 53 51 4c 49 54 45 5f 4a flag is SQLITE_J
1efcb 55 4d 50 49 46 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a UMPIFNULL..**.**
1efcc 20 54 68 69 73 20 63 6f 64 65 20 64 65 70 65 6e This code depen
1efcd 64 73 20 6f 6e 20 74 68 65 20 66 61 63 74 20 74 ds on the fact t
1efce 68 61 74 20 63 65 72 74 61 69 6e 20 74 6f 6b 65 hat certain toke
1efcf 6e 20 76 61 6c 75 65 73 20 28 65 78 3a 20 54 4b n values (ex: TK
1efd0 5f 45 51 29 0a 2a 2a 20 61 72 65 20 74 68 65 20 _EQ).** are the
1efd1 73 61 6d 65 20 61 73 20 6f 70 63 6f 64 65 20 76 same as opcode v
1efd2 61 6c 75 65 73 20 28 65 78 3a 20 4f 50 5f 45 71 alues (ex: OP_Eq
1efd3 29 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 ) that implement
1efd4 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
1efd5 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e ng.** operation.
1efd6 20 20 53 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e Special commen
1efd7 74 73 20 69 6e 20 76 64 62 65 2e 63 20 61 6e 64 ts in vdbe.c and
1efd8 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 the mkopcodeh.a
1efd9 77 6b 20 73 63 72 69 70 74 20 69 6e 0a 2a 2a 20 wk script in.**
1efda 74 68 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73 the make process
1efdb 20 63 61 75 73 65 20 74 68 65 73 65 20 76 61 6c cause these val
1efdc 75 65 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41 ues to align. A
1efdd 73 73 65 72 74 28 29 73 20 69 6e 20 74 68 65 20 ssert()s in the
1efde 63 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77 20 76 65 code.** below ve
1efdf 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75 rify that the nu
1efe0 6d 62 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65 mbers are aligne
1efe1 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a d correctly..*/.
1efe2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1efe3 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 oid sqlite3ExprI
1efe4 66 54 72 75 65 28 50 61 72 73 65 20 2a 70 50 61 fTrue(Parse *pPa
1efe5 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
1efe6 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 , int dest, int
1efe7 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 jumpIfNull){. V
1efe8 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d dbe *v = pParse-
1efe9 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 >pVdbe;. int op
1efea 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 = 0;. int regF
1efeb 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 ree1 = 0;. int
1efec 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20 regFree2 = 0;.
1efed 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61 int r1, r2;.. a
1efee 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c ssert( jumpIfNul
1efef 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 l==SQLITE_JUMPIF
1eff0 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 NULL || jumpIfNu
1eff1 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 76 ll==0 );. if( v
1eff2 3d 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30 20 ==0 || pExpr==0
1eff3 29 20 72 65 74 75 72 6e 3b 0a 20 20 6f 70 20 3d ) return;. op =
1eff4 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 73 77 pExpr->op;. sw
1eff5 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 itch( op ){.
1eff6 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 case TK_AND: {.
1eff7 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 73 71 int d2 = sq
1eff8 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
1eff9 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 74 65 73 el(v);. tes
1effa 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c tcase( jumpIfNul
1effb 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 l==0 );. te
1effc 73 74 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e stcase( pParse->
1effd 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d disableColCache=
1effe 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 =0 );. sqli
1efff 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 te3ExprIfFalse(p
1f000 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
1f001 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75 eft, d2,jumpIfNu
1f002 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 ll^SQLITE_JUMPIF
1f003 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 70 50 61 NULL);. pPa
1f004 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 rse->disableColC
1f005 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71 ache++;. sq
1f006 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 lite3ExprIfTrue(
1f007 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1f008 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d Right, dest, jum
1f009 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 pIfNull);.
1f00a 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
1f00b 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e disableColCache>
1f00c 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 0 );. pPars
1f00d 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 e->disableColCac
1f00e 68 65 2d 2d 3b 0a 20 20 20 20 20 20 73 71 6c 69 he--;. sqli
1f00f 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
1f010 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 bel(v, d2);.
1f011 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1f012 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 20 7b case TK_OR: {
1f013 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1f014 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 jumpIfNull==0 )
1f015 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1f016 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c ( pParse->disabl
1f017 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 3b 0a eColCache==0 );.
1f018 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1f019 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 rIfTrue(pParse,
1f01a 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 pExpr->pLeft, de
1f01b 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b st, jumpIfNull);
1f01c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 . pParse->d
1f01d 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b isableColCache++
1f01e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
1f01f 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65 xprIfTrue(pParse
1f020 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c , pExpr->pRight,
1f021 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c dest, jumpIfNul
1f022 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 l);. assert
1f023 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c ( pParse->disabl
1f024 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 eColCache>0 );.
1f025 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 pParse->dis
1f026 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a ableColCache--;.
1f027 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1f028 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e }. case TK_N
1f029 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 OT: {. test
1f02a 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c case( jumpIfNull
1f02b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ==0 );. sql
1f02c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 ite3ExprIfFalse(
1f02d 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1f02e 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 Left, dest, jump
1f02f 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 IfNull);. b
1f030 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1f031 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 case TK_LT:.
1f032 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 case TK_LE:.
1f033 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 case TK_GT:.
1f034 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 case TK_GE:.
1f035 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 case TK_NE:.
1f036 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 case TK_EQ: {.
1f037 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c assert( TK_L
1f038 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20 T==OP_Lt );.
1f039 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d assert( TK_LE=
1f03a 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20 =OP_Le );.
1f03b 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f assert( TK_GT==O
1f03c 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73 P_Gt );. as
1f03d 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f sert( TK_GE==OP_
1f03e 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Ge );. asse
1f03f 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 rt( TK_EQ==OP_Eq
1f040 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1f041 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 ( TK_NE==OP_Ne )
1f042 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1f043 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 ( op==TK_LT );.
1f044 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1f045 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 p==TK_LE );.
1f046 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1f047 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 TK_GT );. t
1f048 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1f049 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 GE );. test
1f04a 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 case( op==TK_EQ
1f04b 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1f04c 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a e( op==TK_NE );.
1f04d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1f04e 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b jumpIfNull==0 );
1f04f 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 . codeCompa
1f050 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 reOperands(pPars
1f051 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
1f052 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c &r1, ®Free1,
1f053 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1f054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f055 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 pExpr->pRight
1f056 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 , &r2, ®Free2
1f057 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d );. codeCom
1f058 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 pare(pParse, pEx
1f059 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 pr->pLeft, pExpr
1f05a 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 ->pRight, op,.
1f05b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f05c 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 r1, r2, dest, ju
1f05d 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 mpIfNull);.
1f05e 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
1f05f 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee1==0 );.
1f060 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
1f061 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 e2==0 );. b
1f062 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1f063 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a case TK_ISNULL:.
1f064 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e case TK_NOTN
1f065 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 ULL: {. ass
1f066 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d ert( TK_ISNULL==
1f067 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 OP_IsNull );.
1f068 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f assert( TK_NO
1f069 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c TNULL==OP_NotNul
1f06a 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 l );. testc
1f06b 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 ase( op==TK_ISNU
1f06c 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 LL );. test
1f06d 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 case( op==TK_NOT
1f06e 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 NULL );. r1
1f06f 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1f070 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
1f071 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 Expr->pLeft, &re
1f072 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 gFree1);. s
1f073 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1f074 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 (v, op, r1, dest
1f075 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1f076 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 e( regFree1==0 )
1f077 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1f078 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
1f079 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 _BETWEEN: {.
1f07a 20 20 2f 2a 20 20 20 20 78 20 42 45 54 57 45 45 /* x BETWEE
1f07b 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 N y AND z.
1f07c 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 73 20 65 **. ** Is e
1f07d 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 quivalent to .
1f07e 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
1f07f 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a x>=y AND x<=z
1f080 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
1f081 2a 2a 20 43 6f 64 65 20 69 74 20 61 73 20 73 75 ** Code it as su
1f082 63 68 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 20 ch, taking care
1f083 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e to do the common
1f084 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 subexpression.
1f085 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 ** elementa
1f086 74 69 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 tion of x..
1f087 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 65 */. Expr e
1f088 78 70 72 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 xprAnd;. Ex
1f089 70 72 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 pr compLeft;.
1f08a 20 20 20 45 78 70 72 20 63 6f 6d 70 52 69 67 68 Expr compRigh
1f08b 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 t;. Expr ex
1f08c 70 72 58 3b 0a 0a 20 20 20 20 20 20 65 78 70 72 prX;.. expr
1f08d 58 20 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 X = *pExpr->pLef
1f08e 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 t;. exprAnd
1f08f 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 .op = TK_AND;.
1f090 20 20 20 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 exprAnd.pLef
1f091 74 20 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 t = &compLeft;.
1f092 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70 52 69 exprAnd.pRi
1f093 67 68 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 ght = &compRight
1f094 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 ;. compLeft
1f095 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 .op = TK_GE;.
1f096 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 compLeft.pLef
1f097 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 t = &exprX;.
1f098 20 20 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 compLeft.pRigh
1f099 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 t = pExpr->pList
1f09a 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 ->a[0].pExpr;.
1f09b 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 compRight.op
1f09c 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 = TK_LE;.
1f09d 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20 compRight.pLeft
1f09e 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 = &exprX;.
1f09f 63 6f 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74 compRight.pRight
1f0a0 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d = pExpr->pList-
1f0a1 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 >a[1].pExpr;.
1f0a2 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20 exprX.iTable
1f0a3 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
1f0a4 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65 eTemp(pParse, &e
1f0a5 78 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29 xprX, ®Free1)
1f0a6 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1f0a7 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
1f0a8 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20 . exprX.op
1f0a9 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 = TK_REGISTER;.
1f0aa 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a testcase( j
1f0ab 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a umpIfNull==0 );.
1f0ac 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1f0ad 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 rIfTrue(pParse,
1f0ae 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 &exprAnd, dest,
1f0af 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 jumpIfNull);.
1f0b0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1f0b1 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
1f0b2 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 r1 = sqlite
1f0b3 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
1f0b4 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 arse, pExpr, &re
1f0b5 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 gFree1);. s
1f0b6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1f0b7 28 76 2c 20 4f 50 5f 49 66 2c 20 72 31 2c 20 64 (v, OP_If, r1, d
1f0b8 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 est, jumpIfNull!
1f0b9 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 =0);. testc
1f0ba 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 ase( regFree1==0
1f0bb 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1f0bc 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d se( jumpIfNull==
1f0bd 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
1f0be 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 ;. }. }. sq
1f0bf 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
1f0c0 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 Reg(pParse, regF
1f0c1 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 ree1);. sqlite3
1f0c2 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
1f0c3 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 Parse, regFree2)
1f0c4 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e ; .}../*.** Gen
1f0c5 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 erate code for a
1f0c6 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 boolean express
1f0c7 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 20 ion such that a
1f0c8 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20 jump is made.**
1f0c9 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65 to the label "de
1f0ca 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72 65 st" if the expre
1f0cb 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 20 62 ssion is false b
1f0cc 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 ut execution.**
1f0cd 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 continues straig
1f0ce 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65 ht thru if the e
1f0cf 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75 xpression is tru
1f0d0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
1f0d1 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 expression evalu
1f0d2 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 ates to NULL (ne
1f0d3 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66 ither true nor f
1f0d4 61 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 alse) then.** ju
1f0d5 6d 70 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c mp if jumpIfNull
1f0d6 20 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 is SQLITE_JUMPI
1f0d7 46 4e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20 74 68 FNULL or fall th
1f0d8 72 6f 75 67 68 20 69 66 20 6a 75 6d 70 49 66 4e rough if jumpIfN
1f0d9 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a ull.** is 0..*/.
1f0da 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1f0db 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 oid sqlite3ExprI
1f0dc 66 46 61 6c 73 65 28 50 61 72 73 65 20 2a 70 50 fFalse(Parse *pP
1f0dd 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 arse, Expr *pExp
1f0de 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 r, int dest, int
1f0df 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 jumpIfNull){.
1f0e0 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 Vdbe *v = pParse
1f0e1 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f ->pVdbe;. int o
1f0e2 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 p = 0;. int reg
1f0e3 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 Free1 = 0;. int
1f0e4 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 regFree2 = 0;.
1f0e5 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 int r1, r2;..
1f0e6 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 assert( jumpIfNu
1f0e7 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 ll==SQLITE_JUMPI
1f0e8 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e FNULL || jumpIfN
1f0e9 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 ull==0 );. if(
1f0ea 76 3d 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30 v==0 || pExpr==0
1f0eb 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a ) return;.. /*
1f0ec 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45 The value of pE
1f0ed 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61 xpr->op and op a
1f0ee 72 65 20 72 65 6c 61 74 65 64 20 61 73 20 66 6f re related as fo
1f0ef 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a llows:. **. **
1f0f0 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 pExpr->op
1f0f1 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 0a 20 op.
1f0f2 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d ** ------
1f0f3 2d 2d 2d 20 20 20 20 20 20 20 20 20 20 2d 2d 2d --- ---
1f0f4 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20 -------. **
1f0f5 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 TK_ISNULL
1f0f6 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c OP_NotNull
1f0f7 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e . ** TK_N
1f0f8 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 4f OTNULL O
1f0f9 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 P_IsNull. **
1f0fa 20 20 20 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 TK_NE
1f0fb 20 20 20 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a OP_Eq. *
1f0fc 2a 20 20 20 20 20 20 20 54 4b 5f 45 51 20 20 20 * TK_EQ
1f0fd 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 65 OP_Ne
1f0fe 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47 . ** TK_G
1f0ff 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f T O
1f100 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 P_Le. **
1f101 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 TK_LE
1f102 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20 OP_Gt. **
1f103 20 20 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20 TK_GE
1f104 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a OP_Lt. *
1f105 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54 20 20 20 * TK_LT
1f106 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47 65 OP_Ge
1f107 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f . **. ** For o
1f108 74 68 65 72 20 76 61 6c 75 65 73 20 6f 66 20 70 ther values of p
1f109 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20 Expr->op, op is
1f10a 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 75 6e undefined and un
1f10b 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76 used.. ** The v
1f10c 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20 alue of TK_ and
1f10d 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 OP_ constants ar
1f10e 65 20 61 72 72 61 6e 67 65 64 20 73 75 63 68 20 e arranged such
1f10f 74 68 61 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e that we. ** can
1f110 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 70 compute the map
1f111 70 69 6e 67 20 61 62 6f 76 65 20 75 73 69 6e 67 ping above using
1f112 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 the following e
1f113 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20 xpression.. **
1f114 41 73 73 65 72 74 28 29 73 20 76 65 72 69 66 79 Assert()s verify
1f115 20 74 68 61 74 20 74 68 65 20 63 6f 6d 70 75 74 that the comput
1f116 61 74 69 6f 6e 20 69 73 20 63 6f 72 72 65 63 74 ation is correct
1f117 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28 .. */. op = ((
1f118 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53 pExpr->op+(TK_IS
1f119 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f NULL&1))^1)-(TK_
1f11a 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a ISNULL&1);.. /*
1f11b 20 56 65 72 69 66 79 20 63 6f 72 72 65 63 74 20 Verify correct
1f11c 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f alignment of TK_
1f11d 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e and OP_ constan
1f11e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ts. */. assert
1f11f 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f ( pExpr->op!=TK_
1f120 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 ISNULL || op==OP
1f121 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 _NotNull );. as
1f122 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 sert( pExpr->op!
1f123 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f =TK_NOTNULL || o
1f124 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a p==OP_IsNull );.
1f125 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
1f126 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 >op!=TK_NE || op
1f127 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 61 73 73 ==OP_Eq );. ass
1f128 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d ert( pExpr->op!=
1f129 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f TK_EQ || op==OP_
1f12a 4e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Ne );. assert(
1f12b 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 pExpr->op!=TK_LT
1f12c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b || op==OP_Ge );
1f12d 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 . assert( pExpr
1f12e 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f ->op!=TK_LE || o
1f12f 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73 p==OP_Gt );. as
1f130 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 sert( pExpr->op!
1f131 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50 =TK_GT || op==OP
1f132 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 _Le );. assert(
1f133 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 pExpr->op!=TK_G
1f134 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29 E || op==OP_Lt )
1f135 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 ;.. switch( pEx
1f136 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 pr->op ){. ca
1f137 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 se TK_AND: {.
1f138 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d testcase( jum
1f139 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
1f13a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 50 testcase( pP
1f13b 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1f13c 43 61 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 Cache==0 );.
1f13d 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 sqlite3ExprIfF
1f13e 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 alse(pParse, pEx
1f13f 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c pr->pLeft, dest,
1f140 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 jumpIfNull);.
1f141 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 pParse->disa
1f142 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 bleColCache++;.
1f143 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1f144 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 IfFalse(pParse,
1f145 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 pExpr->pRight, d
1f146 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 est, jumpIfNull)
1f147 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1f148 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 pParse->disableC
1f149 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 olCache>0 );.
1f14a 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 pParse->disab
1f14b 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 leColCache--;.
1f14c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1f14d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a . case TK_OR:
1f14e 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20 {. int d2
1f14f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b = sqlite3VdbeMak
1f150 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 eLabel(v);.
1f151 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 testcase( jumpI
1f152 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 fNull==0 );.
1f153 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72 testcase( pPar
1f154 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 se->disableColCa
1f155 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 che==0 );.
1f156 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 sqlite3ExprIfTru
1f157 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
1f158 3e 70 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70 >pLeft, d2, jump
1f159 49 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 IfNull^SQLITE_JU
1f15a 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 MPIFNULL);.
1f15b 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 pParse->disable
1f15c 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 ColCache++;.
1f15d 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 sqlite3ExprIfF
1f15e 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 alse(pParse, pEx
1f15f 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 pr->pRight, dest
1f160 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 , jumpIfNull);.
1f161 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
1f162 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 rse->disableColC
1f163 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 ache>0 );.
1f164 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 pParse->disableC
1f165 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 olCache--;.
1f166 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f sqlite3VdbeReso
1f167 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b lveLabel(v, d2);
1f168 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1f169 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1f16a 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c NOT: {. sql
1f16b 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70 ite3ExprIfTrue(p
1f16c 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
1f16d 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 eft, dest, jumpI
1f16e 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 fNull);. br
1f16f 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1f170 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 ase TK_LT:. c
1f171 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 ase TK_LE:. c
1f172 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 ase TK_GT:. c
1f173 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 ase TK_GE:. c
1f174 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 ase TK_NE:. c
1f175 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 ase TK_EQ: {.
1f176 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1f177 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 =TK_LT );.
1f178 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1f179 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 _LE );. tes
1f17a 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 tcase( op==TK_GT
1f17b 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1f17c 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b se( op==TK_GE );
1f17d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1f17e 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 op==TK_EQ );.
1f17f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
1f180 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 ==TK_NE );.
1f181 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 testcase( jumpI
1f182 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 fNull==0 );.
1f183 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 codeCompareOpe
1f184 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 rands(pParse, pE
1f185 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c xpr->pLeft, &r1,
1f186 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 ®Free1,.
1f187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f188 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 pE
1f189 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 xpr->pRight, &r2
1f18a 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
1f18b 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 codeCompare(
1f18c 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1f18d 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 Left, pExpr->pRi
1f18e 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 ght, op,.
1f18f 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 r1, r
1f190 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 2, dest, jumpIfN
1f191 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ull);. test
1f192 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
1f193 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
1f194 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
1f195 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
1f196 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1f197 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 TK_ISNULL:. c
1f198 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 ase TK_NOTNULL:
1f199 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 {. testcase
1f19a 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 ( op==TK_ISNULL
1f19b 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1f19c 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c e( op==TK_NOTNUL
1f19d 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 L );. r1 =
1f19e 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
1f19f 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 emp(pParse, pExp
1f1a0 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 r->pLeft, ®Fr
1f1a1 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ee1);. sqli
1f1a2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1f1a3 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a op, r1, dest);.
1f1a4 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1f1a5 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
1f1a6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1f1a7 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 }. case TK_BE
1f1a8 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f TWEEN: {. /
1f1a9 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79 * x BETWEEN y
1f1aa 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a AND z. **.
1f1ab 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75 69 ** Is equi
1f1ac 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 valent to .
1f1ad 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 **. **
1f1ae 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 x>=y AND x<=z.
1f1af 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
1f1b0 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68 2c Code it as such,
1f1b1 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f 20 taking care to
1f1b2 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 do the common su
1f1b3 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 bexpression.
1f1b4 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f ** elementatio
1f1b5 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f n of x.. */
1f1b6 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 . Expr expr
1f1b7 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72 20 And;. Expr
1f1b8 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 compLeft;.
1f1b9 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a Expr compRight;.
1f1ba 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 58 Expr exprX
1f1bb 3b 0a 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d ;.. exprX =
1f1bc 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a *pExpr->pLeft;.
1f1bd 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 exprAnd.op
1f1be 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 = TK_AND;.
1f1bf 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d exprAnd.pLeft =
1f1c0 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 &compLeft;.
1f1c1 20 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74 exprAnd.pRight
1f1c2 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 = &compRight;.
1f1c3 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 compLeft.op
1f1c4 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 = TK_GE;.
1f1c5 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d compLeft.pLeft =
1f1c6 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 &exprX;. c
1f1c7 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d ompLeft.pRight =
1f1c8 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 pExpr->pList->a
1f1c9 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 [0].pExpr;.
1f1ca 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 compRight.op =
1f1cb 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d TK_LE;. com
1f1cc 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 pRight.pLeft = &
1f1cd 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d exprX;. com
1f1ce 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20 pRight.pRight =
1f1cf 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b pExpr->pList->a[
1f1d0 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 1].pExpr;.
1f1d1 65 78 70 72 58 2e 69 54 61 62 6c 65 20 3d 20 73 exprX.iTable = s
1f1d2 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 qlite3ExprCodeTe
1f1d3 6d 70 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 mp(pParse, &expr
1f1d4 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 X, ®Free1);.
1f1d5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
1f1d6 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 egFree1==0 );.
1f1d7 20 20 20 20 65 78 70 72 58 2e 6f 70 20 3d 20 54 exprX.op = T
1f1d8 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 K_REGISTER;.
1f1d9 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 testcase( jump
1f1da 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 IfNull==0 );.
1f1db 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 sqlite3ExprIf
1f1dc 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 26 65 False(pParse, &e
1f1dd 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75 xprAnd, dest, ju
1f1de 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 mpIfNull);.
1f1df 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1f1e0 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
1f1e1 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 r1 = sqlite3E
1f1e2 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 xprCodeTemp(pPar
1f1e3 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46 se, pExpr, ®F
1f1e4 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c ree1);. sql
1f1e5 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1f1e6 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 31 2c 20 , OP_IfNot, r1,
1f1e7 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c dest, jumpIfNull
1f1e8 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 !=0);. test
1f1e9 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
1f1ea 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
1f1eb 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d ase( jumpIfNull=
1f1ec 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 =0 );. brea
1f1ed 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 k;. }. }. s
1f1ee 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
1f1ef 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 pReg(pParse, reg
1f1f0 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 Free1);. sqlite
1f1f1 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
1f1f2 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 pParse, regFree2
1f1f3 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 );.}../*.** Do a
1f1f4 20 64 65 65 70 20 63 6f 6d 70 61 72 69 73 6f 6e deep comparison
1f1f5 20 6f 66 20 74 77 6f 20 65 78 70 72 65 73 73 69 of two expressi
1f1f6 6f 6e 20 74 72 65 65 73 2e 20 20 52 65 74 75 72 on trees. Retur
1f1f7 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f n TRUE (non-zero
1f1f8 29 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65 ).** if they are
1f1f9 20 69 64 65 6e 74 69 63 61 6c 20 61 6e 64 20 72 identical and r
1f1fa 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74 eturn FALSE if t
1f1fb 68 65 79 20 64 69 66 66 65 72 20 69 6e 20 61 6e hey differ in an
1f1fc 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d y way..**.** Som
1f1fd 65 74 69 6d 65 73 20 74 68 69 73 20 72 6f 75 74 etimes this rout
1f1fe 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ine will return
1f1ff 46 41 4c 53 45 20 65 76 65 6e 20 69 66 20 74 68 FALSE even if th
1f200 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e e two expression
1f201 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 72 65 20 s.** really are
1f202 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 49 66 20 equivalent. If
1f203 77 65 20 63 61 6e 6e 6f 74 20 70 72 6f 76 65 20 we cannot prove
1f204 74 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73 that the express
1f205 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 69 64 65 6e ions are.** iden
1f206 74 69 63 61 6c 2c 20 77 65 20 72 65 74 75 72 6e tical, we return
1f207 20 46 41 4c 53 45 20 6a 75 73 74 20 74 6f 20 62 FALSE just to b
1f208 65 20 73 61 66 65 2e 20 20 53 6f 20 69 66 20 74 e safe. So if t
1f209 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 his routine.** r
1f20a 65 74 75 72 6e 73 20 66 61 6c 73 65 2c 20 74 68 eturns false, th
1f20b 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74 20 72 65 en you do not re
1f20c 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72 20 63 65 ally know for ce
1f20d 72 74 61 69 6e 20 69 66 20 74 68 65 20 74 77 6f rtain if the two
1f20e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 .** expressions
1f20f 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 42 are the same. B
1f210 75 74 20 69 66 20 79 6f 75 20 67 65 74 20 61 20 ut if you get a
1f211 54 52 55 45 20 72 65 74 75 72 6e 2c 20 74 68 65 TRUE return, the
1f212 6e 20 79 6f 75 0a 2a 2a 20 63 61 6e 20 62 65 20 n you.** can be
1f213 73 75 72 65 20 74 68 65 20 65 78 70 72 65 73 73 sure the express
1f214 69 6f 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d ions are the sam
1f215 65 2e 20 20 49 6e 20 74 68 65 20 70 6c 61 63 65 e. In the place
1f216 73 20 77 68 65 72 65 0a 2a 2a 20 74 68 69 73 20 s where.** this
1f217 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 2c routine is used,
1f218 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 68 75 72 it does not hur
1f219 74 20 74 6f 20 67 65 74 20 61 6e 20 65 78 74 72 t to get an extr
1f21a 61 20 46 41 4c 53 45 20 2d 20 74 68 61 74 0a 2a a FALSE - that.*
1f21b 2a 20 6a 75 73 74 20 6d 69 67 68 74 20 72 65 73 * just might res
1f21c 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73 6c 69 67 ult in some slig
1f21d 68 74 6c 79 20 73 6c 6f 77 65 72 20 63 6f 64 65 htly slower code
1f21e 2e 20 20 42 75 74 20 72 65 74 75 72 6e 69 6e 67 . But returning
1f21f 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 .** an incorrect
1f220 20 54 52 55 45 20 63 6f 75 6c 64 20 6c 65 61 64 TRUE could lead
1f221 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f to a malfunctio
1f222 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
1f223 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1f224 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78 70 72 ExprCompare(Expr
1f225 20 2a 70 41 2c 20 45 78 70 72 20 2a 70 42 29 7b *pA, Expr *pB){
1f226 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
1f227 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a pA==0||pB==0 ){.
1f228 20 20 20 20 72 65 74 75 72 6e 20 70 42 3d 3d 70 return pB==p
1f229 41 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 2d A;. }. if( pA-
1f22a 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20 72 65 >op!=pB->op ) re
1f22b 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 70 turn 0;. if( (p
1f22c 41 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 A->flags & EP_Di
1f22d 73 74 69 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c stinct)!=(pB->fl
1f22e 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 ags & EP_Distinc
1f22f 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 t) ) return 0;.
1f230 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 if( !sqlite3Exp
1f231 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65 rCompare(pA->pLe
1f232 66 74 2c 20 70 42 2d 3e 70 4c 65 66 74 29 20 29 ft, pB->pLeft) )
1f233 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
1f234 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d !sqlite3ExprCom
1f235 70 61 72 65 28 70 41 2d 3e 70 52 69 67 68 74 2c pare(pA->pRight,
1f236 20 70 42 2d 3e 70 52 69 67 68 74 29 20 29 20 72 pB->pRight) ) r
1f237 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 eturn 0;. if( p
1f238 41 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 A->pList ){.
1f239 69 66 28 20 70 42 2d 3e 70 4c 69 73 74 3d 3d 30 if( pB->pList==0
1f23a 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
1f23b 20 69 66 28 20 70 41 2d 3e 70 4c 69 73 74 2d 3e if( pA->pList->
1f23c 6e 45 78 70 72 21 3d 70 42 2d 3e 70 4c 69 73 74 nExpr!=pB->pList
1f23d 2d 3e 6e 45 78 70 72 20 29 20 72 65 74 75 72 6e ->nExpr ) return
1f23e 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 0;. for(i=0;
1f23f 20 69 3c 70 41 2d 3e 70 4c 69 73 74 2d 3e 6e 45 i<pA->pList->nE
1f240 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 xpr; i++){.
1f241 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 if( !sqlite3Exp
1f242 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 69 rCompare(pA->pLi
1f243 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 st->a[i].pExpr,
1f244 70 42 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e pB->pList->a[i].
1f245 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 pExpr) ){.
1f246 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
1f247 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 }. }. }els
1f248 65 20 69 66 28 20 70 42 2d 3e 70 4c 69 73 74 20 e if( pB->pList
1f249 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
1f24a 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 2d 3e 70 . }. if( pA->p
1f24b 53 65 6c 65 63 74 20 7c 7c 20 70 42 2d 3e 70 53 Select || pB->pS
1f24c 65 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 elect ) return 0
1f24d 3b 0a 20 20 69 66 28 20 70 41 2d 3e 69 54 61 62 ;. if( pA->iTab
1f24e 6c 65 21 3d 70 42 2d 3e 69 54 61 62 6c 65 20 7c le!=pB->iTable |
1f24f 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70 | pA->iColumn!=p
1f250 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29 20 72 65 74 B->iColumn ) ret
1f251 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41 2d urn 0;. if( pA-
1f252 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 >op!=TK_COLUMN &
1f253 26 20 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 20 29 7b & pA->token.z ){
1f254 0a 20 20 20 20 69 66 28 20 70 42 2d 3e 74 6f 6b . if( pB->tok
1f255 65 6e 2e 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e en.z==0 ) return
1f256 20 30 3b 0a 20 20 20 20 69 66 28 20 70 42 2d 3e 0;. if( pB->
1f257 74 6f 6b 65 6e 2e 6e 21 3d 70 41 2d 3e 74 6f 6b token.n!=pA->tok
1f258 65 6e 2e 6e 20 29 20 72 65 74 75 72 6e 20 30 3b en.n ) return 0;
1f259 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1f25a 53 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a 29 StrNICmp((char*)
1f25b 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 2c 28 63 68 61 pA->token.z,(cha
1f25c 72 2a 29 70 42 2d 3e 74 6f 6b 65 6e 2e 7a 2c 70 r*)pB->token.z,p
1f25d 42 2d 3e 74 6f 6b 65 6e 2e 6e 29 21 3d 30 20 29 B->token.n)!=0 )
1f25e 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 {. return 0
1f25f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1f260 74 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn 1;.}.../*.*
1f261 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d * Add a new elem
1f262 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 49 ent to the pAggI
1f263 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 61 nfo->aCol[] arra
1f264 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 y. Return the i
1f265 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e ndex of.** the n
1f266 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74 ew element. Ret
1f267 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e urn a negative n
1f268 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20 umber if malloc
1f269 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 fails..*/.static
1f26a 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 43 int addAggInfoC
1f26b 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a 64 olumn(sqlite3 *d
1f26c 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 b, AggInfo *pInf
1f26d 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 o){. int i;. p
1f26e 49 6e 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c Info->aCol = sql
1f26f 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 ite3ArrayAllocat
1f270 65 28 0a 20 20 20 20 20 20 20 64 62 2c 0a 20 20 e(. db,.
1f271 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c pInfo->aCol
1f272 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 ,. sizeof(
1f273 70 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c pInfo->aCol[0]),
1f274 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20 . 3,.
1f275 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d &pInfo->nColum
1f276 6e 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f n,. &pInfo
1f277 2d 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a ->nColumnAlloc,.
1f278 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 &i. );.
1f279 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 return i;.}
1f27a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 ../*.** Add a ne
1f27b 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 w element to the
1f27c 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 pAggInfo->aFunc
1f27d 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 72 [] array. Retur
1f27e 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a n the index of.*
1f27f 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e * the new elemen
1f280 74 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 t. Return a neg
1f281 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 ative number if
1f282 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f malloc fails..*/
1f283 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 41 .static int addA
1f284 67 67 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69 74 ggInfoFunc(sqlit
1f285 65 33 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20 e3 *db, AggInfo
1f286 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69 *pInfo){. int i
1f287 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 ;. pInfo->aFunc
1f288 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 = sqlite3ArrayA
1f289 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20 llocate(.
1f28a 64 62 2c 20 0a 20 20 20 20 20 20 20 70 49 6e 66 db, . pInf
1f28b 6f 2d 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20 20 o->aFunc,.
1f28c 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61 sizeof(pInfo->a
1f28d 46 75 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20 20 Func[0]),.
1f28e 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 3,. &pInf
1f28f 6f 2d 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20 20 o->nFunc,.
1f290 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c &pInfo->nFuncAl
1f291 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20 loc,. &i.
1f292 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a );. return i;.
1f293 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 } ../*.** Thi
1f294 73 20 69 73 20 74 68 65 20 78 45 78 70 72 43 61 s is the xExprCa
1f295 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 74 72 65 llback for a tre
1f296 65 20 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 73 e walker. It is
1f297 20 75 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c used to.** impl
1f298 65 6d 65 6e 74 20 73 71 6c 69 74 65 33 45 78 70 ement sqlite3Exp
1f299 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 rAnalyzeAggregat
1f29a 65 73 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74 es(). See sqlit
1f29b 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 e3ExprAnalyzeAgg
1f29c 72 65 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61 regates.** for a
1f29d 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
1f29e 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ation..*/.static
1f29f 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 int analyzeAggr
1f2a0 65 67 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57 egate(Walker *pW
1f2a1 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 alker, Expr *pEx
1f2a2 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 pr){. int i;.
1f2a3 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 NameContext *pNC
1f2a4 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e = pWalker->u.pN
1f2a5 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 C;. Parse *pPar
1f2a6 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 se = pNC->pParse
1f2a7 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 ;. SrcList *pSr
1f2a8 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 cList = pNC->pSr
1f2a9 63 4c 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 6f cList;. AggInfo
1f2aa 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e 43 *pAggInfo = pNC
1f2ab 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 73 ->pAggInfo;.. s
1f2ac 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 witch( pExpr->op
1f2ad 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ){. case TK_
1f2ae 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 AGG_COLUMN:.
1f2af 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 case TK_COLUMN:
1f2b0 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 {. testcase
1f2b1 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
1f2b2 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 AGG_COLUMN );.
1f2b3 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 testcase( pE
1f2b4 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 xpr->op==TK_COLU
1f2b5 4d 4e 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 43 MN );. /* C
1f2b6 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
1f2b7 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e 20 he column is in
1f2b8 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 one of the table
1f2b9 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20 s in the FROM.
1f2ba 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f 66 ** clause of
1f2bb 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 71 the aggregate q
1f2bc 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 66 uery */. if
1f2bd 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 ( pSrcList ){.
1f2be 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 struct Src
1f2bf 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1f2c0 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 0a = pSrcList->a;.
1f2c1 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b for(i=0;
1f2c2 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 i<pSrcList->nSr
1f2c3 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 c; i++, pItem++)
1f2c4 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 75 {. stru
1f2c5 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a ct AggInfo_col *
1f2c6 70 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 pCol;.
1f2c7 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c if( pExpr->iTabl
1f2c8 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f e==pItem->iCurso
1f2c9 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 r ){.
1f2ca 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 /* If we reach
1f2cb 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d this point, it m
1f2cc 65 61 6e 73 20 74 68 61 74 20 70 45 78 70 72 20 eans that pExpr
1f2cd 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c refers to a tabl
1f2ce 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a e. **
1f2cf 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 that is in the
1f2d0 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 FROM clause of t
1f2d1 68 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65 he aggregate que
1f2d2 72 79 2e 20 20 0a 20 20 20 20 20 20 20 20 20 20 ry. .
1f2d3 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20 **.
1f2d4 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 ** Make an entr
1f2d5 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e y for the column
1f2d6 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 in pAggInfo->aC
1f2d7 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a 20 20 ol[] if there.
1f2d8 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 ** is
1f2d9 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20 74 68 65 not an entry the
1f2da 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 re already..
1f2db 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1f2dc 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 int k;.
1f2dd 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d pCol =
1f2de 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b pAggInfo->aCol;
1f2df 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 . for
1f2e0 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f (k=0; k<pAggInfo
1f2e1 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20 ->nColumn; k++,
1f2e2 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 pCol++){.
1f2e3 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d if( pCol-
1f2e4 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e >iTable==pExpr->
1f2e5 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 iTable &&.
1f2e6 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c pCol
1f2e7 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 ->iColumn==pExpr
1f2e8 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 ->iColumn ){.
1f2e9 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 bre
1f2ea 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ak;.
1f2eb 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
1f2ec 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 }. if
1f2ed 28 20 28 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e ( (k>=pAggInfo->
1f2ee 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20 nColumn).
1f2ef 20 20 20 20 20 20 26 26 20 28 6b 20 3d 20 61 64 && (k = ad
1f2f0 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70 dAggInfoColumn(p
1f2f1 50 61 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49 Parse->db, pAggI
1f2f2 6e 66 6f 29 29 3e 3d 30 20 0a 20 20 20 20 20 20 nfo))>=0 .
1f2f3 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
1f2f4 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 pCol = &p
1f2f5 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d AggInfo->aCol[k]
1f2f6 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1f2f7 70 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78 pCol->pTab = pEx
1f2f8 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 pr->pTab;.
1f2f9 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54 pCol->iT
1f2fa 61 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 69 54 able = pExpr->iT
1f2fb 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 able;.
1f2fc 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d pCol->iColum
1f2fd 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 n = pExpr->iColu
1f2fe 6d 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 mn;.
1f2ff 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b pCol->iMem = +
1f300 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
1f301 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f pCo
1f302 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e l->iSorterColumn
1f303 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 = -1;.
1f304 20 20 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72 pCol->pExpr
1f305 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 = pExpr;.
1f306 20 20 20 20 20 20 20 20 69 66 28 20 70 41 67 67 if( pAgg
1f307 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29 Info->pGroupBy )
1f308 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1f309 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20 int j, n;.
1f30a 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 Expr
1f30b 4c 69 73 74 20 2a 70 47 42 20 3d 20 70 41 67 67 List *pGB = pAgg
1f30c 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a Info->pGroupBy;.
1f30d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f30e 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
1f30f 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47 item *pTerm = pG
1f310 42 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 20 20 B->a;.
1f311 20 20 20 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e n = pGB->n
1f312 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 Expr;.
1f313 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a for(j=0; j
1f314 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b <n; j++, pTerm++
1f315 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1f316 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 Expr *pE =
1f317 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 pTerm->pExpr;.
1f318 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f319 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43 if( pE->op==TK_C
1f31a 4f 4c 55 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61 OLUMN && pE->iTa
1f31b 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 ble==pExpr->iTab
1f31c 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 le &&.
1f31d 20 20 20 20 20 20 20 20 20 20 20 20 70 45 2d 3e pE->
1f31e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e iColumn==pExpr->
1f31f 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 iColumn ){.
1f320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1f321 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 Col->iSorterColu
1f322 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 mn = j;.
1f323 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
1f324 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 k;.
1f325 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1f326 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1f327 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1f328 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d if( pCol-
1f329 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30 >iSorterColumn<0
1f32a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1f32b 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 pCol->iSorte
1f32c 72 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e rColumn = pAggIn
1f32d 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 fo->nSortingColu
1f32e 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 mn++;.
1f32f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1f330 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
1f331 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 77 20 /* There is now
1f332 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 70 45 78 an entry for pEx
1f333 70 72 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e pr in pAggInfo->
1f334 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20 aCol[] (either.
1f335 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 ** be
1f336 63 61 75 73 65 20 69 74 20 77 61 73 20 74 68 65 cause it was the
1f337 72 65 20 62 65 66 6f 72 65 20 6f 72 20 62 65 63 re before or bec
1f338 61 75 73 65 20 77 65 20 6a 75 73 74 20 63 72 65 ause we just cre
1f339 61 74 65 64 20 69 74 29 2e 0a 20 20 20 20 20 20 ated it)..
1f33a 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74 ** Convert
1f33b 20 74 68 65 20 70 45 78 70 72 20 74 6f 20 62 65 the pExpr to be
1f33c 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e a TK_AGG_COLUMN
1f33d 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20 74 68 referring to th
1f33e 61 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a at. *
1f33f 2a 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c * pAggInfo->aCol
1f340 5b 5d 20 65 6e 74 72 79 2e 0a 20 20 20 20 20 20 [] entry..
1f341 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
1f342 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67 pExpr->pAgg
1f343 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b Info = pAggInfo;
1f344 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 . pEx
1f345 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f pr->op = TK_AGG_
1f346 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20 COLUMN;.
1f347 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20 pExpr->iAgg
1f348 3d 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = k;.
1f349 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1f34a 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45 78 } /* endif pEx
1f34b 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65 pr->iTable==pIte
1f34c 6d 2d 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20 20 m->iCursor */.
1f34d 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20 6c } /* end l
1f34e 6f 6f 70 20 6f 76 65 72 20 70 53 72 63 4c 69 73 oop over pSrcLis
1f34f 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 t */. }.
1f350 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 return WRC_Pr
1f351 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 une;. }. c
1f352 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 ase TK_AGG_FUNCT
1f353 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 ION: {. /*
1f354 54 68 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d The pNC->nDepth=
1f355 3d 30 20 74 65 73 74 20 63 61 75 73 65 73 20 61 =0 test causes a
1f356 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
1f357 6e 73 20 69 6e 20 73 75 62 71 75 65 72 69 65 73 ns in subqueries
1f358 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 . ** to be
1f359 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20 ignored */.
1f35a 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 if( pNC->nDepth
1f35b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ==0 ){. /
1f35c 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
1f35d 66 20 70 45 78 70 72 20 69 73 20 61 20 64 75 70 f pExpr is a dup
1f35e 6c 69 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65 licate of anothe
1f35f 72 20 61 67 67 72 65 67 61 74 65 20 0a 20 20 20 r aggregate .
1f360 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e ** function
1f361 20 74 68 61 74 20 69 73 20 61 6c 72 65 61 64 79 that is already
1f362 20 69 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f in the pAggInfo
1f363 20 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 20 structure.
1f364 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74 */. st
1f365 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e ruct AggInfo_fun
1f366 63 20 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49 c *pItem = pAggI
1f367 6e 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20 nfo->aFunc;.
1f368 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
1f369 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 AggInfo->nFunc;
1f36a 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i++, pItem++){.
1f36b 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
1f36c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 ite3ExprCompare(
1f36d 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45 pItem->pExpr, pE
1f36e 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 xpr) ){.
1f36f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1f370 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1f371 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d . if( i>=
1f372 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20 pAggInfo->nFunc
1f373 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
1f374 70 45 78 70 72 20 69 73 20 6f 72 69 67 69 6e 61 pExpr is origina
1f375 6c 2e 20 20 4d 61 6b 65 20 61 20 6e 65 77 20 65 l. Make a new e
1f376 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f ntry in pAggInfo
1f377 2d 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20 20 ->aFunc[].
1f378 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
1f379 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 u8 enc = ENC(pP
1f37a 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 arse->db);.
1f37b 20 20 20 20 20 69 20 3d 20 61 64 64 41 67 67 49 i = addAggI
1f37c 6e 66 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e nfoFunc(pParse->
1f37d 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a 20 db, pAggInfo);.
1f37e 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d if( i>=
1f37f 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
1f380 20 70 49 74 65 6d 20 3d 20 26 70 41 67 67 49 6e pItem = &pAggIn
1f381 66 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b 0a 20 20 fo->aFunc[i];.
1f382 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d pItem-
1f383 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a >pExpr = pExpr;.
1f384 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 pIte
1f385 6d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 m->iMem = ++pPar
1f386 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 se->nMem;.
1f387 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 46 75 pItem->pFu
1f388 6e 63 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 nc = sqlite3Find
1f389 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d Function(pParse-
1f38a 3e 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 >db,.
1f38b 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 (char*)p
1f38c 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 Expr->token.z, p
1f38d 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 0a 20 Expr->token.n,.
1f38e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f38f 20 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 20 3f pExpr->pList ?
1f390 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e pExpr->pList->n
1f391 45 78 70 72 20 3a 20 30 2c 20 65 6e 63 2c 20 30 Expr : 0, enc, 0
1f392 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
1f393 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 f( pExpr->flags
1f394 26 20 45 50 5f 44 69 73 74 69 6e 63 74 20 29 7b & EP_Distinct ){
1f395 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 . p
1f396 49 74 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 Item->iDistinct
1f397 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b = pParse->nTab++
1f398 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 ;. }e
1f399 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
1f39a 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69 pItem->iDisti
1f39b 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 nct = -1;.
1f39c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1f39d 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1f39e 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 70 45 /* Make pE
1f39f 78 70 72 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 xpr point to the
1f3a0 20 61 70 70 72 6f 70 72 69 61 74 65 20 70 41 67 appropriate pAg
1f3a1 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 65 gInfo->aFunc[] e
1f3a2 6e 74 72 79 0a 20 20 20 20 20 20 20 20 2a 2f 0a ntry. */.
1f3a3 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1f3a4 41 67 67 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 Agg = i;.
1f3a5 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f pExpr->pAggInfo
1f3a6 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 = pAggInfo;.
1f3a7 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
1f3a8 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 7d 0a 20 Prune;. }.
1f3a9 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1f3aa 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a n WRC_Continue;.
1f3ab 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61 }.static int ana
1f3ac 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 49 6e lyzeAggregatesIn
1f3ad 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 Select(Walker *p
1f3ae 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a Walker, Select *
1f3af 70 53 65 6c 65 63 74 29 7b 0a 20 20 4e 61 6d 65 pSelect){. Name
1f3b0 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70 Context *pNC = p
1f3b1 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 Walker->u.pNC;.
1f3b2 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 if( pNC->nDepth
1f3b3 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e ==0 ){. pNC->
1f3b4 6e 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20 73 71 nDepth++;. sq
1f3b5 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 lite3WalkSelect(
1f3b6 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c 65 63 74 pWalker, pSelect
1f3b7 29 3b 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65 70 );. pNC->nDep
1f3b8 74 68 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e th--;. return
1f3b9 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 65 WRC_Prune;. }e
1f3ba 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
1f3bb 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 WRC_Continue;.
1f3bc 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 }.}../*.** Analy
1f3bd 7a 65 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 ze the given exp
1f3be 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69 6e 67 20 ression looking
1f3bf 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75 for aggregate fu
1f3c0 6e 63 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 nctions and.** f
1f3c1 6f 72 20 76 61 72 69 61 62 6c 65 73 20 74 68 61 or variables tha
1f3c2 74 20 6e 65 65 64 20 74 6f 20 62 65 20 61 64 64 t need to be add
1f3c3 65 64 20 74 6f 20 74 68 65 20 70 50 61 72 73 65 ed to the pParse
1f3c4 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 2e 0a ->aAgg[] array..
1f3c5 2a 2a 20 4d 61 6b 65 20 61 64 64 69 74 69 6f 6e ** Make addition
1f3c6 61 6c 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 al entries to th
1f3c7 65 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d e pParse->aAgg[]
1f3c8 20 61 72 72 61 79 20 61 73 20 6e 65 63 65 73 73 array as necess
1f3c9 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ary..**.** This
1f3ca 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f routine should o
1f3cb 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 61 66 nly be called af
1f3cc 74 65 72 20 74 68 65 20 65 78 70 72 65 73 73 69 ter the expressi
1f3cd 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 on has been.** a
1f3ce 6e 61 6c 79 7a 65 64 20 62 79 20 73 71 6c 69 74 nalyzed by sqlit
1f3cf 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
1f3d0 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f es()..*/.SQLITE_
1f3d1 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1f3d2 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 ite3ExprAnalyzeA
1f3d3 67 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f ggregates(NameCo
1f3d4 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72 ntext *pNC, Expr
1f3d5 20 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b *pExpr){. Walk
1f3d6 65 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 er w;. w.xExprC
1f3d7 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a allback = analyz
1f3d8 65 41 67 67 72 65 67 61 74 65 3b 0a 20 20 77 2e eAggregate;. w.
1f3d9 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 xSelectCallback
1f3da 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 = analyzeAggrega
1f3db 74 65 73 49 6e 53 65 6c 65 63 74 3b 0a 20 20 77 tesInSelect;. w
1f3dc 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 .u.pNC = pNC;.
1f3dd 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1f3de 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f &w, pExpr);.}../
1f3df 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 *.** Call sqlite
1f3e0 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 3ExprAnalyzeAggr
1f3e1 65 67 61 74 65 73 28 29 20 66 6f 72 20 65 76 65 egates() for eve
1f3e2 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e ry expression in
1f3e3 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f an.** expressio
1f3e4 6e 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 20 n list. Return
1f3e5 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 the number of er
1f3e6 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 rors..**.** If a
1f3e7 6e 20 65 72 72 6f 72 20 69 73 20 66 6f 75 6e 64 n error is found
1f3e8 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 69 , the analysis i
1f3e9 73 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a 2f 0a s cut short..*/.
1f3ea 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1f3eb 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 oid sqlite3ExprA
1f3ec 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61 nalyzeAggList(Na
1f3ed 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 meContext *pNC,
1f3ee 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 ExprList *pList)
1f3ef 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c {. struct ExprL
1f3f0 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
1f3f1 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
1f3f2 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 pList ){. for
1f3f3 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c (pItem=pList->a,
1f3f4 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e i=0; i<pList->n
1f3f5 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
1f3f6 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
1f3f7 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 e3ExprAnalyzeAgg
1f3f8 72 65 67 61 74 65 73 28 70 4e 43 2c 20 70 49 74 regates(pNC, pIt
1f3f9 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 em->pExpr);.
1f3fa 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 }. }.}../*.** A
1f3fb 6c 6c 6f 63 61 74 65 20 6f 72 20 64 65 61 6c 6c llocate or deall
1f3fc 6f 63 61 74 65 20 74 65 6d 70 6f 72 61 72 79 20 ocate temporary
1f3fd 75 73 65 20 72 65 67 69 73 74 65 72 73 20 64 75 use registers du
1f3fe 72 69 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61 ring code genera
1f3ff 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
1f400 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1f401 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50 61 te3GetTempReg(Pa
1f402 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 rse *pParse){.
1f403 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d if( pParse->nTem
1f404 70 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 72 pReg==0 ){. r
1f405 65 74 75 72 6e 20 2b 2b 70 50 61 72 73 65 2d 3e eturn ++pParse->
1f406 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 nMem;. }. retu
1f407 72 6e 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 rn pParse->aTemp
1f408 52 65 67 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e 54 Reg[--pParse->nT
1f409 65 6d 70 52 65 67 5d 3b 0a 7d 0a 53 51 4c 49 54 empReg];.}.SQLIT
1f40a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1f40b 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
1f40c 70 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72 pReg(Parse *pPar
1f40d 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 se, int iReg){.
1f40e 20 69 66 28 20 69 52 65 67 20 26 26 20 70 50 61 if( iReg && pPa
1f40f 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c 41 72 rse->nTempReg<Ar
1f410 72 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d 3e raySize(pParse->
1f411 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 20 20 aTempReg) ){.
1f412 20 73 71 6c 69 74 65 33 45 78 70 72 57 72 69 74 sqlite3ExprWrit
1f413 61 62 6c 65 52 65 67 69 73 74 65 72 28 70 50 61 ableRegister(pPa
1f414 72 73 65 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 rse, iReg);.
1f415 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 pParse->aTempReg
1f416 5b 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 [pParse->nTempRe
1f417 67 2b 2b 5d 20 3d 20 69 52 65 67 3b 0a 20 20 7d g++] = iReg;. }
1f418 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
1f419 74 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 te or deallocate
1f41a 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6e 52 65 67 a block of nReg
1f41b 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 consecutive reg
1f41c 69 73 74 65 72 73 0a 2a 2f 0a 53 51 4c 49 54 45 isters.*/.SQLITE
1f41d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1f41e 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 ite3GetTempRange
1f41f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1f420 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 int nReg){. int
1f421 20 69 2c 20 6e 3b 0a 20 20 69 20 3d 20 70 50 61 i, n;. i = pPa
1f422 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 3b 0a rse->iRangeReg;.
1f423 20 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 52 n = pParse->nR
1f424 61 6e 67 65 52 65 67 3b 0a 20 20 69 66 28 20 6e angeReg;. if( n
1f425 52 65 67 3c 3d 6e 20 26 26 20 21 75 73 65 64 41 Reg<=n && !usedA
1f426 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 sColumnCache(pPa
1f427 72 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29 20 29 rse, i, i+n-1) )
1f428 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52 {. pParse->iR
1f429 61 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65 67 3b angeReg += nReg;
1f42a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 52 61 . pParse->nRa
1f42b 6e 67 65 52 65 67 20 2d 3d 20 6e 52 65 67 3b 0a ngeReg -= nReg;.
1f42c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 20 3d }else{. i =
1f42d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b pParse->nMem+1;
1f42e 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 . pParse->nMe
1f42f 6d 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 7d 0a 20 m += nReg;. }.
1f430 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c return i;.}.SQL
1f431 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1f432 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
1f433 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a empRange(Parse *
1f434 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 pParse, int iReg
1f435 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 , int nReg){. i
1f436 66 28 20 6e 52 65 67 3e 70 50 61 72 73 65 2d 3e f( nReg>pParse->
1f437 6e 52 61 6e 67 65 52 65 67 20 29 7b 0a 20 20 20 nRangeReg ){.
1f438 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 pParse->nRangeR
1f439 65 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70 eg = nReg;. p
1f43a 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 Parse->iRangeReg
1f43b 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a = iReg;. }.}..
1f43c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1f43d 45 6e 64 20 6f 66 20 65 78 70 72 2e 63 20 2a 2a End of expr.c **
1f43e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f43f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1f441 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1f442 42 65 67 69 6e 20 66 69 6c 65 20 61 6c 74 65 72 Begin file alter
1f443 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1f444 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f445 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1f446 2f 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62 72 75 /*.** 2005 Febru
1f447 61 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 ary 15.**.** The
1f448 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
1f449 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
1f44a 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
1f44b 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
1f44c 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
1f44d 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
1f44e 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1f44f 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1f450 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1f451 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1f452 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1f453 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1f454 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
1f455 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
1f456 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
1f457 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
1f458 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
1f459 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f45a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f45b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f45c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f45d 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
1f45e 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f le contains C co
1f45f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 de routines that
1f460 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 used to generat
1f461 65 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a 20 74 e VDBE code.** t
1f462 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 hat implements t
1f463 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 he ALTER TABLE c
1f464 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 ommand..**.** $I
1f465 64 3a 20 61 6c 74 65 72 2e 63 2c 76 20 31 2e 35 d: alter.c,v 1.5
1f466 31 20 32 30 30 38 2f 31 32 2f 31 30 20 31 39 3a 1 2008/12/10 19:
1f467 32 36 3a 32 32 20 64 72 68 20 45 78 70 20 24 0a 26:22 drh Exp $.
1f468 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f */../*.** The co
1f469 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
1f46a 6f 6e 6c 79 20 65 78 69 73 74 73 20 69 66 20 77 only exists if w
1f46b 65 20 61 72 65 20 6e 6f 74 20 6f 6d 69 74 74 69 e are not omitti
1f46c 6e 67 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 ng the.** ALTER
1f46d 54 41 42 4c 45 20 6c 6f 67 69 63 20 66 72 6f 6d TABLE logic from
1f46e 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 the build..*/.#
1f46f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f470 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 0a 0a IT_ALTERTABLE...
1f471 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
1f472 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 53 ion is used by S
1f473 51 4c 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 QL generated to
1f474 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 0a 2a implement the .*
1f475 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f * ALTER TABLE co
1f476 6d 6d 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74 mmand. The first
1f477 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
1f478 20 74 65 78 74 20 6f 66 20 61 20 43 52 45 41 54 text of a CREAT
1f479 45 20 54 41 42 4c 45 20 6f 72 0a 2a 2a 20 43 52 E TABLE or.** CR
1f47a 45 41 54 45 20 49 4e 44 45 58 20 63 6f 6d 6d 61 EATE INDEX comma
1f47b 6e 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 nd. The second i
1f47c 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 s a table name.
1f47d 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 The table name i
1f47e 6e 20 0a 2a 2a 20 74 68 65 20 43 52 45 41 54 45 n .** the CREATE
1f47f 20 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 TABLE or CREATE
1f480 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 INDEX statement
1f481 20 69 73 20 72 65 70 6c 61 63 65 64 20 77 69 74 is replaced wit
1f482 68 20 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 61 h the third.** a
1f483 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 rgument and the
1f484 72 65 73 75 6c 74 20 72 65 74 75 72 6e 65 64 2e result returned.
1f485 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a Examples:.**.**
1f486 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 sqlite_rename_t
1f487 61 62 6c 65 28 27 43 52 45 41 54 45 20 54 41 42 able('CREATE TAB
1f488 4c 45 20 61 62 63 28 61 2c 20 62 2c 20 63 29 27 LE abc(a, b, c)'
1f489 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20 20 20 20 , 'def').**
1f48a 2d 3e 20 27 43 52 45 41 54 45 20 54 41 42 4c 45 -> 'CREATE TABLE
1f48b 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a def(a, b, c)'.*
1f48c 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61 *.** sqlite_rena
1f48d 6d 65 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45 me_table('CREATE
1f48e 20 49 4e 44 45 58 20 69 20 4f 4e 20 61 62 63 28 INDEX i ON abc(
1f48f 61 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20 a)', 'def').**
1f490 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20 49 4e -> 'CREATE IN
1f491 44 45 58 20 69 20 4f 4e 20 64 65 66 28 61 2c 20 DEX i ON def(a,
1f492 62 2c 20 63 29 27 0a 2a 2f 0a 73 74 61 74 69 63 b, c)'.*/.static
1f493 20 76 6f 69 64 20 72 65 6e 61 6d 65 54 61 62 6c void renameTabl
1f494 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 eFunc(. sqlite3
1f495 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
1f496 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 t,. int NotUsed
1f497 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
1f498 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e e **argv.){. un
1f499 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 signed char cons
1f49a 74 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 t *zSql = sqlite
1f49b 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
1f49c 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 v[0]);. unsigne
1f49d 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 d char const *zT
1f49e 61 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 ableName = sqlit
1f49f 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
1f4a0 67 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 gv[1]);.. int t
1f4a1 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e oken;. Token tn
1f4a2 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 ame;. unsigned
1f4a3 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 char const *zCsr
1f4a4 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c = zSql;. int l
1f4a5 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a en = 0;. char *
1f4a6 7a 52 65 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 zRet;.. sqlite3
1f4a7 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 *db = sqlite3_c
1f4a8 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
1f4a9 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e (context);.. UN
1f4aa 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
1f4ab 6f 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 otUsed);.. /* T
1f4ac 68 65 20 70 72 69 6e 63 69 70 6c 65 20 75 73 65 he principle use
1f4ad 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 d to locate the
1f4ae 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 table name in th
1f4af 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 0a e CREATE TABLE .
1f4b0 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 ** statement i
1f4b1 73 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 s that the table
1f4b2 20 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 name is the fir
1f4b3 73 74 20 6e 6f 6e 2d 73 70 61 63 65 20 74 6f 6b st non-space tok
1f4b4 65 6e 20 74 68 61 74 0a 20 20 2a 2a 20 69 73 20 en that. ** is
1f4b5 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c immediately foll
1f4b6 6f 77 65 64 20 62 79 20 61 20 54 4b 5f 4c 50 20 owed by a TK_LP
1f4b7 6f 72 20 54 4b 5f 55 53 49 4e 47 20 74 6f 6b 65 or TK_USING toke
1f4b8 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53 n.. */. if( zS
1f4b9 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 ql ){. do {.
1f4ba 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73 72 20 if( !*zCsr
1f4bb 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 61 ){. /* Ra
1f4bc 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 62 n out of input b
1f4bd 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20 61 6e efore finding an
1f4be 20 6f 70 65 6e 69 6e 67 20 62 72 61 63 6b 65 74 opening bracket
1f4bf 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a . Return NULL. *
1f4c0 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e /. return
1f4c1 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
1f4c2 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f /* Store the to
1f4c3 6b 65 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f ken that zCsr po
1f4c4 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 ints to in tname
1f4c5 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 . */. tname
1f4c6 2e 7a 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 .z = zCsr;.
1f4c7 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a tname.n = len;.
1f4c8 0a 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 . /* Advanc
1f4c9 65 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65 e zCsr to the ne
1f4ca 78 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 xt token. Store
1f4cb 74 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 that token type
1f4cc 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 in 'token',.
1f4cd 20 20 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e ** and its len
1f4ce 67 74 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f gth in 'len' (to
1f4cf 20 62 65 20 75 73 65 64 20 6e 65 78 74 20 69 74 be used next it
1f4d0 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 eration of this
1f4d1 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a loop).. */.
1f4d2 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 do {.
1f4d3 20 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a zCsr += len;.
1f4d4 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 len = sq
1f4d5 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 lite3GetToken(zC
1f4d6 73 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 sr, &token);.
1f4d7 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 } while( toke
1f4d8 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20 n==TK_SPACE );.
1f4d9 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e assert( len
1f4da 3e 30 20 29 3b 0a 20 20 20 20 7d 20 77 68 69 6c >0 );. } whil
1f4db 65 28 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50 20 e( token!=TK_LP
1f4dc 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53 49 && token!=TK_USI
1f4dd 4e 47 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74 20 NG );.. zRet
1f4de 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 = sqlite3MPrintf
1f4df 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22 (db, "%.*s\"%w\"
1f4e0 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20 7a %s", tname.z - z
1f4e1 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20 Sql, zSql, .
1f4e2 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74 zTableName, t
1f4e3 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b name.z+tname.n);
1f4e4 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 . sqlite3_res
1f4e5 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
1f4e6 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49 , zRet, -1, SQLI
1f4e7 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d TE_DYNAMIC);. }
1f4e8 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
1f4e9 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a TE_OMIT_TRIGGER.
1f4ea 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
1f4eb 20 69 73 20 75 73 65 64 20 62 79 20 53 51 4c 20 is used by SQL
1f4ec 67 65 6e 65 72 61 74 65 64 20 74 6f 20 69 6d 70 generated to imp
1f4ed 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 4c lement the.** AL
1f4ee 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e TER TABLE comman
1f4ef 64 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 d. The first arg
1f4f0 75 6d 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 ument is the tex
1f4f1 74 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 t of a CREATE TR
1f4f2 49 47 47 45 52 20 0a 2a 2a 20 73 74 61 74 65 6d IGGER .** statem
1f4f3 65 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 ent. The second
1f4f4 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e is a table name.
1f4f5 20 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 The table name
1f4f6 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 0a 2a in the CREATE .*
1f4f7 2a 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d * TRIGGER statem
1f4f8 65 6e 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 ent is replaced
1f4f9 77 69 74 68 20 74 68 65 20 74 68 69 72 64 20 61 with the third a
1f4fa 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 rgument and the
1f4fb 72 65 73 75 6c 74 20 0a 2a 2a 20 72 65 74 75 72 result .** retur
1f4fc 6e 65 64 2e 20 54 68 69 73 20 69 73 20 61 6e 61 ned. This is ana
1f4fd 6c 61 67 6f 75 73 20 74 6f 20 72 65 6e 61 6d 65 lagous to rename
1f4fe 54 61 62 6c 65 46 75 6e 63 28 29 20 61 62 6f 76 TableFunc() abov
1f4ff 65 2c 20 65 78 63 65 70 74 20 66 6f 72 20 43 52 e, except for CR
1f500 45 41 54 45 0a 2a 2a 20 54 52 49 47 47 45 52 2c EATE.** TRIGGER,
1f501 20 6e 6f 74 20 43 52 45 41 54 45 20 49 4e 44 45 not CREATE INDE
1f502 58 20 61 6e 64 20 43 52 45 41 54 45 20 54 41 42 X and CREATE TAB
1f503 4c 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f LE..*/.static vo
1f504 69 64 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 id renameTrigger
1f505 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
1f506 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
1f507 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c ,. int NotUsed,
1f508 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
1f509 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 **argv.){. uns
1f50a 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 igned char const
1f50b 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 *zSql = sqlite3
1f50c 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
1f50d 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 [0]);. unsigned
1f50e 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 char const *zTa
1f50f 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 bleName = sqlite
1f510 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
1f511 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f v[1]);.. int to
1f512 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 ken;. Token tna
1f513 6d 65 3b 0a 20 20 69 6e 74 20 64 69 73 74 20 3d me;. int dist =
1f514 20 33 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 3;. unsigned c
1f515 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 har const *zCsr
1f516 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 = zSql;. int le
1f517 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a n = 0;. char *z
1f518 52 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a Ret;. sqlite3 *
1f519 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
1f51a 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
1f51b 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53 ontext);.. UNUS
1f51c 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
1f51d 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 Used);.. /* The
1f51e 20 70 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20 principle used
1f51f 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61 to locate the ta
1f520 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 ble name in the
1f521 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 0a CREATE TRIGGER .
1f522 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 ** statement i
1f523 73 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 s that the table
1f524 20 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 name is the fir
1f525 73 74 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 st token that is
1f526 20 69 6d 6d 65 64 69 61 74 65 64 6c 79 0a 20 20 immediatedly.
1f527 2a 2a 20 70 72 65 63 65 64 65 64 20 62 79 20 65 ** preceded by e
1f528 69 74 68 65 72 20 54 4b 5f 4f 4e 20 6f 72 20 54 ither TK_ON or T
1f529 4b 5f 44 4f 54 20 61 6e 64 20 69 6d 6d 65 64 69 K_DOT and immedi
1f52a 61 74 65 64 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 atedly followed
1f52b 62 79 20 6f 6e 65 0a 20 20 2a 2a 20 6f 66 20 54 by one. ** of T
1f52c 4b 5f 57 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e K_WHEN, TK_BEGIN
1f52d 20 6f 72 20 54 4b 5f 46 4f 52 2e 0a 20 20 2a 2f or TK_FOR.. */
1f52e 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 . if( zSql ){.
1f52f 20 20 20 64 6f 20 7b 0a 0a 20 20 20 20 20 20 69 do {.. i
1f530 66 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 f( !*zCsr ){.
1f531 20 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 /* Ran out
1f532 6f 66 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 of input before
1f533 66 69 6e 64 69 6e 67 20 74 68 65 20 74 61 62 6c finding the tabl
1f534 65 20 6e 61 6d 65 2e 20 52 65 74 75 72 6e 20 4e e name. Return N
1f535 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ULL. */.
1f536 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a return;. }.
1f537 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 . /* Store
1f538 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a the token that z
1f539 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e Csr points to in
1f53a 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 tname. */.
1f53b 20 74 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72 3b tname.z = zCsr;
1f53c 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d . tname.n =
1f53d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 len;.. /*
1f53e 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f 20 Advance zCsr to
1f53f 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 the next token.
1f540 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 6e Store that token
1f541 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e 27 type in 'token'
1f542 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 69 ,. ** and i
1f543 74 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 ts length in 'le
1f544 6e 27 20 28 74 6f 20 62 65 20 75 73 65 64 20 6e n' (to be used n
1f545 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 ext iteration of
1f546 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 this loop)..
1f547 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b */. do {
1f548 0a 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b 3d . zCsr +=
1f549 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65 len;. le
1f54a 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f n = sqlite3GetTo
1f54b 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e ken(zCsr, &token
1f54c 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 );. }while(
1f54d 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 token==TK_SPACE
1f54e 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1f54f 28 20 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20 20 20 ( len>0 );..
1f550 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 27 64 /* Variable 'd
1f551 69 73 74 27 20 73 74 6f 72 65 73 20 74 68 65 20 ist' stores the
1f552 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 number of tokens
1f553 20 72 65 61 64 20 73 69 6e 63 65 20 74 68 65 20 read since the
1f554 6d 6f 73 74 0a 20 20 20 20 20 20 2a 2a 20 72 65 most. ** re
1f555 63 65 6e 74 20 54 4b 5f 44 4f 54 20 6f 72 20 54 cent TK_DOT or T
1f556 4b 5f 4f 4e 2e 20 54 68 69 73 20 6d 65 61 6e 73 K_ON. This means
1f557 20 74 68 61 74 20 77 68 65 6e 20 61 20 57 48 45 that when a WHE
1f558 4e 2c 20 46 4f 52 20 6f 72 20 42 45 47 49 4e 20 N, FOR or BEGIN
1f559 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 . ** token
1f55a 69 73 20 72 65 61 64 20 61 6e 64 20 27 64 69 73 is read and 'dis
1f55b 74 27 20 65 71 75 61 6c 73 20 32 2c 20 74 68 65 t' equals 2, the
1f55c 20 63 6f 6e 64 69 74 69 6f 6e 20 73 74 61 74 65 condition state
1f55d 64 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a d above. **
1f55e 20 74 6f 20 62 65 20 6d 65 74 2e 0a 20 20 20 20 to be met..
1f55f 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f **. ** No
1f560 74 65 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f te that ON canno
1f561 74 20 62 65 20 61 20 64 61 74 61 62 61 73 65 2c t be a database,
1f562 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e table or column
1f563 20 6e 61 6d 65 2c 20 73 6f 0a 20 20 20 20 20 20 name, so.
1f564 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e ** there is no n
1f565 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f eed to worry abo
1f566 75 74 20 73 79 6e 74 61 78 20 6c 69 6b 65 20 0a ut syntax like .
1f567 20 20 20 20 20 20 2a 2a 20 22 43 52 45 41 54 45 ** "CREATE
1f568 20 54 52 49 47 47 45 52 20 2e 2e 2e 20 4f 4e 20 TRIGGER ... ON
1f569 4f 4e 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e 22 ON.ON BEGIN ..."
1f56a 20 65 74 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 etc.. */.
1f56b 20 20 20 20 20 64 69 73 74 2b 2b 3b 0a 20 20 20 dist++;.
1f56c 20 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b if( token==TK
1f56d 5f 44 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54 _DOT || token==T
1f56e 4b 5f 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 K_ON ){.
1f56f 64 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 dist = 0;.
1f570 7d 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20 64 }. } while( d
1f571 69 73 74 21 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e ist!=2 || (token
1f572 21 3d 54 4b 5f 57 48 45 4e 20 26 26 20 74 6f 6b !=TK_WHEN && tok
1f573 65 6e 21 3d 54 4b 5f 46 4f 52 20 26 26 20 74 6f en!=TK_FOR && to
1f574 6b 65 6e 21 3d 54 4b 5f 42 45 47 49 4e 29 20 29 ken!=TK_BEGIN) )
1f575 3b 0a 0a 20 20 20 20 2f 2a 20 56 61 72 69 61 62 ;.. /* Variab
1f576 6c 65 20 74 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e le tname now con
1f577 74 61 69 6e 73 20 74 68 65 20 74 6f 6b 65 6e 20 tains the token
1f578 74 68 61 74 20 69 73 20 74 68 65 20 6f 6c 64 20 that is the old
1f579 74 61 62 6c 65 2d 6e 61 6d 65 0a 20 20 20 20 2a table-name. *
1f57a 2a 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 * in the CREATE
1f57b 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e TRIGGER statemen
1f57c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 52 t.. */. zR
1f57d 65 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 et = sqlite3MPri
1f57e 6e 74 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 ntf(db, "%.*s\"%
1f57f 77 5c 22 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 w\"%s", tname.z
1f580 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 - zSql, zSql, .
1f581 20 20 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 zTableName
1f582 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e , tname.z+tname.
1f583 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f n);. sqlite3_
1f584 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 result_text(cont
1f585 65 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 ext, zRet, -1, S
1f586 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a QLITE_DYNAMIC);.
1f587 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f }.}.#endif /
1f588 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 * !SQLITE_OMIT_T
1f589 52 49 47 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a RIGGER */../*.**
1f58a 20 52 65 67 69 73 74 65 72 20 62 75 69 6c 74 2d Register built-
1f58b 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 in functions use
1f58c 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d d to help implem
1f58d 65 6e 74 20 41 4c 54 45 52 20 54 41 42 4c 45 0a ent ALTER TABLE.
1f58e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1f58f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c E void sqlite3Al
1f590 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c terFunctions(sql
1f591 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c ite3 *db){. sql
1f592 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 ite3CreateFunc(d
1f593 62 2c 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d b, "sqlite_renam
1f594 65 5f 74 61 62 6c 65 22 2c 20 32 2c 20 53 51 4c e_table", 2, SQL
1f595 49 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 ITE_UTF8, 0,.
1f596 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f597 20 20 20 20 20 20 72 65 6e 61 6d 65 54 61 62 6c renameTabl
1f598 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 69 eFunc, 0, 0);.#i
1f599 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1f59a 54 5f 54 52 49 47 47 45 52 0a 20 20 73 71 6c 69 T_TRIGGER. sqli
1f59b 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 te3CreateFunc(db
1f59c 2c 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 , "sqlite_rename
1f59d 5f 74 72 69 67 67 65 72 22 2c 20 32 2c 20 53 51 _trigger", 2, SQ
1f59e 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 LITE_UTF8, 0,.
1f59f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f5a0 20 20 20 20 20 20 20 72 65 6e 61 6d 65 54 72 69 renameTri
1f5a1 67 67 65 72 46 75 6e 63 2c 20 30 2c 20 30 29 3b ggerFunc, 0, 0);
1f5a2 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
1f5a3 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 Generate the te
1f5a4 78 74 20 6f 66 20 61 20 57 48 45 52 45 20 65 78 xt of a WHERE ex
1f5a5 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63 pression which c
1f5a6 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 an be used to se
1f5a7 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70 lect all.** temp
1f5a8 6f 72 61 72 79 20 74 72 69 67 67 65 72 73 20 6f orary triggers o
1f5a9 6e 20 74 61 62 6c 65 20 70 54 61 62 20 66 72 6f n table pTab fro
1f5aa 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d m the sqlite_tem
1f5ab 70 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 p_master table.
1f5ac 49 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 If.** table pTab
1f5ad 20 68 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72 has no temporar
1f5ae 79 20 74 72 69 67 67 65 72 73 2c 20 6f 72 20 69 y triggers, or i
1f5af 73 20 69 74 73 65 6c 66 20 73 74 6f 72 65 64 20 s itself stored
1f5b0 69 6e 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f in the .** tempo
1f5b1 72 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 4e rary database, N
1f5b2 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e ULL is returned.
1f5b3 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 .*/.static char
1f5b4 2a 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 *whereTempTrigge
1f5b5 72 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 rs(Parse *pParse
1f5b6 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a , Table *pTab){.
1f5b7 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 Trigger *pTrig
1f5b8 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 ;. char *zWhere
1f5b9 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 74 6d = 0;. char *tm
1f5ba 70 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53 p = 0;. const S
1f5bb 63 68 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65 chema *pTempSche
1f5bc 6d 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d ma = pParse->db-
1f5bd 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b >aDb[1].pSchema;
1f5be 20 2f 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65 /* Temp db sche
1f5bf 6d 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 ma */.. /* If t
1f5c0 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 he table is not
1f5c1 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 74 located in the t
1f5c2 65 6d 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68 emp-db (in which
1f5c3 20 63 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20 case NULL is .
1f5c4 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f ** returned, lo
1f5c5 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 op through the t
1f5c6 61 62 6c 65 73 20 6c 69 73 74 20 6f 66 20 74 72 ables list of tr
1f5c7 69 67 67 65 72 73 2e 20 46 6f 72 20 65 61 63 68 iggers. For each
1f5c8 20 74 72 69 67 67 65 72 0a 20 20 2a 2a 20 74 68 trigger. ** th
1f5c9 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f at is not part o
1f5ca 66 20 74 68 65 20 74 65 6d 70 2d 64 62 20 73 63 f the temp-db sc
1f5cb 68 65 6d 61 2c 20 61 64 64 20 61 20 63 6c 61 75 hema, add a clau
1f5cc 73 65 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 se to the WHERE
1f5cd 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e . ** expression
1f5ce 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 being built up
1f5cf 69 6e 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a in zWhere.. */.
1f5d0 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 63 68 if( pTab->pSch
1f5d1 65 6d 61 21 3d 70 54 65 6d 70 53 63 68 65 6d 61 ema!=pTempSchema
1f5d2 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 ){. sqlite3
1f5d3 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1f5d4 3b 0a 20 20 20 20 66 6f 72 28 20 70 54 72 69 67 ;. for( pTrig
1f5d5 3d 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b =pTab->pTrigger;
1f5d6 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 pTrig; pTrig=pT
1f5d7 72 69 67 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 rig->pNext ){.
1f5d8 20 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70 if( pTrig->p
1f5d9 53 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68 Schema==pTempSch
1f5da 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ema ){. i
1f5db 66 28 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20 f( !zWhere ){.
1f5dc 20 20 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d zWhere =
1f5dd 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1f5de 64 62 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20 70 db, "name=%Q", p
1f5df 54 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 Trig->name);.
1f5e0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1f5e1 20 20 20 20 20 20 74 6d 70 20 3d 20 7a 57 68 65 tmp = zWhe
1f5e2 72 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 57 re;. zW
1f5e3 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 here = sqlite3MP
1f5e4 72 69 6e 74 66 28 64 62 2c 20 22 25 73 20 4f 52 rintf(db, "%s OR
1f5e5 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65 72 name=%Q", zWher
1f5e6 65 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 29 3b e, pTrig->name);
1f5e7 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1f5e8 65 33 44 62 46 72 65 65 28 64 62 2c 20 74 6d 70 e3DbFree(db, tmp
1f5e9 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1f5ea 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1f5eb 20 72 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a return zWhere;.
1f5ec 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
1f5ed 65 20 63 6f 64 65 20 74 6f 20 64 72 6f 70 20 61 e code to drop a
1f5ee 6e 64 20 72 65 6c 6f 61 64 20 74 68 65 20 69 6e nd reload the in
1f5ef 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 ternal represent
1f5f0 61 74 69 6f 6e 20 6f 66 20 74 61 62 6c 65 0a 2a ation of table.*
1f5f1 2a 20 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20 * pTab from the
1f5f2 64 61 74 61 62 61 73 65 2c 20 69 6e 63 6c 75 64 database, includ
1f5f3 69 6e 67 20 74 72 69 67 67 65 72 73 20 61 6e 64 ing triggers and
1f5f4 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 temporary trigg
1f5f5 65 72 73 2e 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 ers..** Argument
1f5f6 20 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 zName is the na
1f5f7 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1f5f8 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1f5f9 73 63 68 65 6d 61 20 61 74 0a 2a 2a 20 74 68 65 schema at.** the
1f5fa 20 74 69 6d 65 20 74 68 65 20 67 65 6e 65 72 61 time the genera
1f5fb 74 65 64 20 63 6f 64 65 20 69 73 20 65 78 65 63 ted code is exec
1f5fc 75 74 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62 uted. This can b
1f5fd 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d e different from
1f5fe 0a 2a 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 .** pTab->zName
1f5ff 69 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e if this function
1f600 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 is being called
1f601 20 74 6f 20 63 6f 64 65 20 70 61 72 74 20 6f 66 to code part of
1f602 20 61 6e 20 0a 2a 2a 20 22 41 4c 54 45 52 20 54 an .** "ALTER T
1f603 41 42 4c 45 20 52 45 4e 41 4d 45 20 54 4f 22 20 ABLE RENAME TO"
1f604 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 statement..*/.st
1f605 61 74 69 63 20 76 6f 69 64 20 72 65 6c 6f 61 64 atic void reload
1f606 54 61 62 6c 65 53 63 68 65 6d 61 28 50 61 72 73 TableSchema(Pars
1f607 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 e *pParse, Table
1f608 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 *pTab, const ch
1f609 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 ar *zName){. Vd
1f60a 62 65 20 2a 76 3b 0a 20 20 63 68 61 72 20 2a 7a be *v;. char *z
1f60b 57 68 65 72 65 3b 0a 20 20 69 6e 74 20 69 44 62 Where;. int iDb
1f60c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1f60d 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
1f60e 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e database contain
1f60f 69 6e 67 20 70 54 61 62 20 2a 2f 0a 23 69 66 6e ing pTab */.#ifn
1f610 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1f611 54 52 49 47 47 45 52 0a 20 20 54 72 69 67 67 65 TRIGGER. Trigge
1f612 72 20 2a 70 54 72 69 67 3b 0a 23 65 6e 64 69 66 r *pTrig;.#endif
1f613 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 .. v = sqlite3G
1f614 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
1f615 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75 72 if( !v ) retur
1f616 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c n;. assert( sql
1f617 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c ite3BtreeHoldsAl
1f618 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73 65 2d lMutexes(pParse-
1f619 3e 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 >db) );. iDb =
1f61a 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
1f61b 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c ndex(pParse->db,
1f61c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b pTab->pSchema);
1f61d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d . assert( iDb>=
1f61e 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 0 );..#ifndef SQ
1f61f 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
1f620 52 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6e 79 20 R. /* Drop any
1f621 74 61 62 6c 65 20 74 72 69 67 67 65 72 73 20 66 table triggers f
1f622 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c rom the internal
1f623 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 66 6f schema. */. fo
1f624 72 28 70 54 72 69 67 3d 70 54 61 62 2d 3e 70 54 r(pTrig=pTab->pT
1f625 72 69 67 67 65 72 3b 20 70 54 72 69 67 3b 20 70 rigger; pTrig; p
1f626 54 72 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78 Trig=pTrig->pNex
1f627 74 29 7b 0a 20 20 20 20 69 6e 74 20 69 54 72 69 t){. int iTri
1f628 67 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 gDb = sqlite3Sch
1f629 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 emaToIndex(pPars
1f62a 65 2d 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53 e->db, pTrig->pS
1f62b 63 68 65 6d 61 29 3b 0a 20 20 20 20 61 73 73 65 chema);. asse
1f62c 72 74 28 20 69 54 72 69 67 44 62 3d 3d 69 44 62 rt( iTrigDb==iDb
1f62d 20 7c 7c 20 69 54 72 69 67 44 62 3d 3d 31 20 29 || iTrigDb==1 )
1f62e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1f62f 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 eAddOp4(v, OP_Dr
1f630 6f 70 54 72 69 67 67 65 72 2c 20 69 54 72 69 67 opTrigger, iTrig
1f631 44 62 2c 20 30 2c 20 30 2c 20 70 54 72 69 67 2d Db, 0, 0, pTrig-
1f632 3e 6e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 23 >name, 0);. }.#
1f633 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 endif.. /* Drop
1f634 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 the table and i
1f635 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e ndex from the in
1f636 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 2a 2f ternal schema */
1f637 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
1f638 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 dOp4(v, OP_DropT
1f639 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c able, iDb, 0, 0,
1f63a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 pTab->zName, 0)
1f63b 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74 ;.. /* Reload t
1f63c 68 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 20 he table, index
1f63d 61 6e 64 20 70 65 72 6d 61 6e 65 6e 74 20 74 72 and permanent tr
1f63e 69 67 67 65 72 20 73 63 68 65 6d 61 73 2e 20 2a igger schemas. *
1f63f 2f 0a 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c /. zWhere = sql
1f640 69 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 ite3MPrintf(pPar
1f641 73 65 2d 3e 64 62 2c 20 22 74 62 6c 5f 6e 61 6d se->db, "tbl_nam
1f642 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 e=%Q", zName);.
1f643 20 69 66 28 20 21 7a 57 68 65 72 65 20 29 20 72 if( !zWhere ) r
1f644 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 eturn;. sqlite3
1f645 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1f646 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 _ParseSchema, iD
1f647 62 2c 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c b, 0, 0, zWhere,
1f648 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 0a 23 P4_DYNAMIC);..#
1f649 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f64a 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 IT_TRIGGER. /*
1f64b 4e 6f 77 2c 20 69 66 20 74 68 65 20 74 61 62 6c Now, if the tabl
1f64c 65 20 69 73 20 6e 6f 74 20 73 74 6f 72 65 64 20 e is not stored
1f64d 69 6e 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 in the temp data
1f64e 62 61 73 65 2c 20 72 65 6c 6f 61 64 20 61 6e 79 base, reload any
1f64f 20 74 65 6d 70 20 0a 20 20 2a 2a 20 74 72 69 67 temp . ** trig
1f650 67 65 72 73 2e 20 44 6f 6e 27 74 20 75 73 65 20 gers. Don't use
1f651 49 4e 28 2e 2e 2e 29 20 69 6e 20 63 61 73 65 20 IN(...) in case
1f652 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
1f653 55 45 52 59 20 69 73 20 64 65 66 69 6e 65 64 2e UERY is defined.
1f654 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 . */. if( (zW
1f655 68 65 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 here=whereTempTr
1f656 69 67 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 iggers(pParse, p
1f657 54 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 Tab))!=0 ){.
1f658 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f659 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 4(v, OP_ParseSch
1f65a 65 6d 61 2c 20 31 2c 20 30 2c 20 30 2c 20 7a 57 ema, 1, 0, 0, zW
1f65b 68 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 here, P4_DYNAMIC
1f65c 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a );. }.#endif.}.
1f65d 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
1f65e 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e code to implemen
1f65f 74 20 74 68 65 20 22 41 4c 54 45 52 20 54 41 42 t the "ALTER TAB
1f660 4c 45 20 78 78 78 20 52 45 4e 41 4d 45 20 54 4f LE xxx RENAME TO
1f661 20 79 79 79 22 20 0a 2a 2a 20 63 6f 6d 6d 61 6e yyy" .** comman
1f662 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 d. .*/.SQLITE_PR
1f663 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1f664 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 e3AlterRenameTab
1f665 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 le(. Parse *pPa
1f666 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 rse,
1f667 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 /* Parser contex
1f668 74 2e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 t. */. SrcList
1f669 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 *pSrc,
1f66a 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 /* The table t
1f66b 6f 20 72 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 54 o rename. */. T
1f66c 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 oken *pName
1f66d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1f66e 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 new table name.
1f66f 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b */.){. int iDb;
1f670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f671 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 68 /* Database th
1f672 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
1f673 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 table */. char
1f674 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 *zDb;
1f675 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
1f676 64 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a database iDb */.
1f677 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 Table *pTab;
1f678 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1f679 61 62 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 6d able being renam
1f67a 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e ed */. char *zN
1f67b 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ame = 0;
1f67c 20 20 2f 2a 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e /* NULL-termin
1f67d 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 ated version of
1f67e 70 4e 61 6d 65 20 2a 2f 20 0a 20 20 73 71 6c 69 pName */ . sqli
1f67f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1f680 2d 3e 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73 ->db; /* Databas
1f681 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
1f682 20 20 69 6e 74 20 6e 54 61 62 4e 61 6d 65 3b 20 int nTabName;
1f683 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1f684 75 6d 62 65 72 20 6f 66 20 55 54 46 2d 38 20 63 umber of UTF-8 c
1f685 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 54 61 haracters in zTa
1f686 62 4e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 bName */. const
1f687 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 3b char *zTabName;
1f688 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c /* Original
1f689 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 name of the tab
1f68a 6c 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b le */. Vdbe *v;
1f68b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f68c 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 63 OMIT_TRIGGER. c
1f68d 68 61 72 20 2a 7a 57 68 65 72 65 20 3d 20 30 3b har *zWhere = 0;
1f68e 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 /* Wher
1f68f 65 20 63 6c 61 75 73 65 20 74 6f 20 6c 6f 63 61 e clause to loca
1f690 74 65 20 74 65 6d 70 20 74 72 69 67 67 65 72 73 te temp triggers
1f691 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 */.#endif. int
1f692 20 69 73 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 isVirtualRename
1f693 20 3d 20 30 3b 20 20 2f 2a 20 54 72 75 65 20 69 = 0; /* True i
1f694 66 20 74 68 69 73 20 69 73 20 61 20 76 2d 74 61 f this is a v-ta
1f695 62 6c 65 20 77 69 74 68 20 61 6e 20 78 52 65 6e ble with an xRen
1f696 61 6d 65 28 29 20 2a 2f 0a 20 20 0a 20 20 69 66 ame() */. . if
1f697 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
1f698 65 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 ed ) goto exit_r
1f699 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 61 ename_table;. a
1f69a 73 73 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 72 ssert( pSrc->nSr
1f69b 63 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 c==1 );. assert
1f69c 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
1f69d 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 ldsAllMutexes(pP
1f69e 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 0a 20 20 arse->db) );..
1f69f 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f pTab = sqlite3Lo
1f6a0 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 cateTable(pParse
1f6a1 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e , 0, pSrc->a[0].
1f6a2 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b 30 zName, pSrc->a[0
1f6a3 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 ].zDatabase);.
1f6a4 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f if( !pTab ) goto
1f6a5 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 exit_rename_tab
1f6a6 6c 65 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 le;. iDb = sqli
1f6a7 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
1f6a8 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 (pParse->db, pTa
1f6a9 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a b->pSchema);. z
1f6aa 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 Db = db->aDb[iDb
1f6ab 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 47 ].zName;.. /* G
1f6ac 65 74 20 61 20 4e 55 4c 4c 20 74 65 72 6d 69 6e et a NULL termin
1f6ad 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 ated version of
1f6ae 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 the new table na
1f6af 6d 65 2e 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d me. */. zName =
1f6b0 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d sqlite3NameFrom
1f6b1 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 Token(db, pName)
1f6b2 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 ;. if( !zName )
1f6b3 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d goto exit_renam
1f6b4 65 5f 74 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 43 e_table;.. /* C
1f6b5 68 65 63 6b 20 74 68 61 74 20 61 20 74 61 62 6c heck that a tabl
1f6b6 65 20 6f 72 20 69 6e 64 65 78 20 6e 61 6d 65 64 e or index named
1f6b7 20 27 7a 4e 61 6d 65 27 20 64 6f 65 73 20 6e 6f 'zName' does no
1f6b8 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 0a t already exist.
1f6b9 20 20 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 ** in database
1f6ba 20 69 44 62 2e 20 49 66 20 73 6f 2c 20 74 68 69 iDb. If so, thi
1f6bb 73 20 69 73 20 61 6e 20 65 72 72 6f 72 2e 0a 20 s is an error..
1f6bc 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
1f6bd 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 3FindTable(db, z
1f6be 4e 61 6d 65 2c 20 7a 44 62 29 20 7c 7c 20 73 71 Name, zDb) || sq
1f6bf 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 lite3FindIndex(d
1f6c0 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 b, zName, zDb) )
1f6c1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
1f6c2 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 orMsg(pParse, .
1f6c3 20 20 20 20 20 20 20 22 74 68 65 72 65 20 69 73 "there is
1f6c4 20 61 6c 72 65 61 64 79 20 61 6e 6f 74 68 65 72 already another
1f6c5 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1f6c6 77 69 74 68 20 74 68 69 73 20 6e 61 6d 65 3a 20 with this name:
1f6c7 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 %s", zName);.
1f6c8 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d goto exit_renam
1f6c9 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 e_table;. }..
1f6ca 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 /* Make sure it
1f6cb 69 73 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 is not a system
1f6cc 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 table being alte
1f6cd 72 65 64 2c 20 6f 72 20 61 20 72 65 73 65 72 76 red, or a reserv
1f6ce 65 64 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 68 61 ed name. ** tha
1f6cf 74 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 62 t the table is b
1f6d0 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 74 6f 2e eing renamed to.
1f6d1 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 . */. if( sqli
1f6d2 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61 62 te3Strlen30(pTab
1f6d3 2d 3e 7a 4e 61 6d 65 29 3e 36 20 0a 20 20 20 26 ->zName)>6 . &
1f6d4 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e & 0==sqlite3StrN
1f6d5 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 ICmp(pTab->zName
1f6d6 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 0a , "sqlite_", 7).
1f6d7 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1f6d8 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1f6d9 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e "table %s may n
1f6da 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20 ot be altered",
1f6db 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pTab->zName);.
1f6dc 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 goto exit_rena
1f6dd 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 me_table;. }.
1f6de 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 if( SQLITE_OK!=s
1f6df 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 qlite3CheckObjec
1f6e0 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e tName(pParse, zN
1f6e1 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f ame) ){. goto
1f6e2 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 exit_rename_tab
1f6e3 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 le;. }..#ifndef
1f6e4 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 SQLITE_OMIT_VIE
1f6e5 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 W. if( pTab->pS
1f6e6 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c elect ){. sql
1f6e7 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1f6e8 72 73 65 2c 20 22 76 69 65 77 20 25 73 20 6d 61 rse, "view %s ma
1f6e9 79 20 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 64 y not be altered
1f6ea 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b ", pTab->zName);
1f6eb 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 . goto exit_r
1f6ec 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d ename_table;. }
1f6ed 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
1f6ee 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1f6ef 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 HORIZATION. /*
1f6f0 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 6f Invoke the autho
1f6f1 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 rization callbac
1f6f2 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 k. */. if( sqli
1f6f3 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 te3AuthCheck(pPa
1f6f4 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45 rse, SQLITE_ALTE
1f6f5 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54 R_TABLE, zDb, pT
1f6f6 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b ab->zName, 0) ){
1f6f7 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 . goto exit_r
1f6f8 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d ename_table;. }
1f6f9 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
1f6fa 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1f6fb 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 TUALTABLE. if(
1f6fc 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f sqlite3ViewGetCo
1f6fd 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 lumnNames(pParse
1f6fe 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 , pTab) ){. g
1f6ff 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f oto exit_rename_
1f700 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 table;. }. if(
1f701 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
1f702 20 26 26 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e && pTab->pMod->
1f703 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 pModule->xRename
1f704 20 29 7b 0a 20 20 20 20 69 73 56 69 72 74 75 61 ){. isVirtua
1f705 6c 52 65 6e 61 6d 65 20 3d 20 31 3b 0a 20 20 7d lRename = 1;. }
1f706 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 .#endif.. /* Be
1f707 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f gin a transactio
1f708 6e 20 61 6e 64 20 63 6f 64 65 20 74 68 65 20 56 n and code the V
1f709 65 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72 20 erifyCookie for
1f70a 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a 20 database iDb. .
1f70b 20 2a 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79 20 ** Then modify
1f70c 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 the schema cooki
1f70d 65 20 28 73 69 6e 63 65 20 74 68 65 20 41 4c 54 e (since the ALT
1f70e 45 52 20 54 41 42 4c 45 20 6d 6f 64 69 66 69 65 ER TABLE modifie
1f70f 73 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d s the. ** schem
1f710 61 29 2e 20 4f 70 65 6e 20 61 20 73 74 61 74 65 a). Open a state
1f711 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1f712 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 if the table is
1f713 20 61 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 a virtual. **
1f714 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 table.. */. v
1f715 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
1f716 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 (pParse);. if(
1f717 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f v==0 ){. goto
1f718 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 exit_rename_tab
1f719 6c 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 le;. }. sqlite
1f71a 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 3BeginWriteOpera
1f71b 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 69 73 56 tion(pParse, isV
1f71c 69 72 74 75 61 6c 52 65 6e 61 6d 65 2c 20 69 44 irtualRename, iD
1f71d 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 61 b);. sqlite3Cha
1f71e 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 ngeCookie(pParse
1f71f 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 , iDb);.. /* If
1f720 20 74 68 69 73 20 69 73 20 61 20 76 69 72 74 75 this is a virtu
1f721 61 6c 20 74 61 62 6c 65 2c 20 69 6e 76 6f 6b 65 al table, invoke
1f722 20 74 68 65 20 78 52 65 6e 61 6d 65 28 29 20 66 the xRename() f
1f723 75 6e 63 74 69 6f 6e 20 69 66 0a 20 20 2a 2a 20 unction if. **
1f724 6f 6e 65 20 69 73 20 64 65 66 69 6e 65 64 2e 20 one is defined.
1f725 54 68 65 20 78 52 65 6e 61 6d 65 28 29 20 63 61 The xRename() ca
1f726 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d 6f 64 69 llback will modi
1f727 66 79 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a fy the names. *
1f728 2a 20 6f 66 20 61 6e 79 20 72 65 73 6f 75 72 63 * of any resourc
1f729 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76 es used by the v
1f72a 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 -table implement
1f72b 61 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67 ation (including
1f72c 20 6f 74 68 65 72 0a 20 20 2a 2a 20 53 51 4c 69 other. ** SQLi
1f72d 74 65 20 74 61 62 6c 65 73 29 20 74 68 61 74 20 te tables) that
1f72e 61 72 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 are identified b
1f72f 79 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 y the name of th
1f730 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e e virtual table.
1f731 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 . */.#ifndef SQ
1f732 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1f733 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 69 73 56 LTABLE. if( isV
1f734 69 72 74 75 61 6c 52 65 6e 61 6d 65 20 29 7b 0a irtualRename ){.
1f735 20 20 20 20 69 6e 74 20 69 20 3d 20 2b 2b 70 50 int i = ++pP
1f736 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 arse->nMem;.
1f737 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f738 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 4(v, OP_String8,
1f739 20 30 2c 20 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c 0, i, 0, zName,
1f73a 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0);. sqlite3
1f73b 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1f73c 5f 56 52 65 6e 61 6d 65 2c 20 69 2c 20 30 2c 20 _VRename, i, 0,
1f73d 30 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 0,(const char*)p
1f73e 54 61 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f 56 Tab->pVtab, P4_V
1f73f 54 41 42 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 TAB);. }.#endif
1f740 0a 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f 75 .. /* figure ou
1f741 74 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d 38 t how many UTF-8
1f742 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 characters are
1f743 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a 54 in zName */. zT
1f744 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a abName = pTab->z
1f745 4e 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d 65 Name;. nTabName
1f746 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 = sqlite3Utf8Ch
1f747 61 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c 20 arLen(zTabName,
1f748 2d 31 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 -1);.. /* Modif
1f749 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 y the sqlite_mas
1f74a 74 65 72 20 74 61 62 6c 65 20 74 6f 20 75 73 65 ter table to use
1f74b 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e the new table n
1f74c 61 6d 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ame. */. sqlite
1f74d 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 3NestedParse(pPa
1f74e 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41 rse,. "UPDA
1f74f 54 45 20 25 51 2e 25 73 20 53 45 54 20 22 0a 23 TE %Q.%s SET ".#
1f750 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
1f751 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20 T_TRIGGER.
1f752 20 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 "sql = sqlit
1f753 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73 e_rename_table(s
1f754 71 6c 2c 20 25 51 29 2c 20 22 0a 23 65 6c 73 65 ql, %Q), ".#else
1f755 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 . "sql
1f756 3d 20 43 41 53 45 20 22 0a 20 20 20 20 20 20 20 = CASE ".
1f757 20 20 20 20 20 22 57 48 45 4e 20 74 79 70 65 20 "WHEN type
1f758 3d 20 27 74 72 69 67 67 65 72 27 20 54 48 45 4e = 'trigger' THEN
1f759 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 sqlite_rename_t
1f75a 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 22 rigger(sql, %Q)"
1f75b 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c . "EL
1f75c 53 45 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 SE sqlite_rename
1f75d 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 20 _table(sql, %Q)
1f75e 45 4e 44 2c 20 22 0a 23 65 6e 64 69 66 0a 20 20 END, ".#endif.
1f75f 20 20 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d "tbl_nam
1f760 65 20 3d 20 25 51 2c 20 22 0a 20 20 20 20 20 20 e = %Q, ".
1f761 20 20 20 20 22 6e 61 6d 65 20 3d 20 43 41 53 45 "name = CASE
1f762 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 ". "
1f763 57 48 45 4e 20 74 79 70 65 3d 27 74 61 62 6c 65 WHEN type='table
1f764 27 20 54 48 45 4e 20 25 51 20 22 0a 20 20 20 20 ' THEN %Q ".
1f765 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 6e 61 "WHEN na
1f766 6d 65 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 5f me LIKE 'sqlite_
1f767 61 75 74 6f 69 6e 64 65 78 25 25 27 20 41 4e 44 autoindex%%' AND
1f768 20 74 79 70 65 3d 27 69 6e 64 65 78 27 20 54 48 type='index' TH
1f769 45 4e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 EN ".
1f76a 20 20 22 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 "'sqlite_autoi
1f76b 6e 64 65 78 5f 27 20 7c 7c 20 25 51 20 7c 7c 20 ndex_' || %Q ||
1f76c 73 75 62 73 74 72 28 6e 61 6d 65 2c 25 64 2b 31 substr(name,%d+1
1f76d 38 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 8) ".
1f76e 20 22 45 4c 53 45 20 6e 61 6d 65 20 45 4e 44 20 "ELSE name END
1f76f 22 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 74 ". "WHERE t
1f770 62 6c 5f 6e 61 6d 65 3d 25 51 20 41 4e 44 20 22 bl_name=%Q AND "
1f771 0a 20 20 20 20 20 20 20 20 20 20 22 28 74 79 70 . "(typ
1f772 65 3d 27 74 61 62 6c 65 27 20 4f 52 20 74 79 70 e='table' OR typ
1f773 65 3d 27 69 6e 64 65 78 27 20 4f 52 20 74 79 70 e='index' OR typ
1f774 65 3d 27 74 72 69 67 67 65 72 27 29 3b 22 2c 20 e='trigger');",
1f775 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 . zDb, SCHE
1f776 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 7a MA_TABLE(iDb), z
1f777 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 Name, zName, zNa
1f778 6d 65 2c 20 0a 23 69 66 6e 64 65 66 20 53 51 4c me, .#ifndef SQL
1f779 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1f77a 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 0a 23 65 . zName,.#e
1f77b 6e 64 69 66 0a 20 20 20 20 20 20 7a 4e 61 6d 65 ndif. zName
1f77c 2c 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 , nTabName, zTab
1f77d 4e 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 Name. );..#ifnd
1f77e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1f77f 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f UTOINCREMENT. /
1f780 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f * If the sqlite_
1f781 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 65 sequence table e
1f782 78 69 73 74 73 20 69 6e 20 74 68 69 73 20 64 61 xists in this da
1f783 74 61 62 61 73 65 2c 20 74 68 65 6e 20 75 70 64 tabase, then upd
1f784 61 74 65 20 0a 20 20 2a 2a 20 69 74 20 77 69 74 ate . ** it wit
1f785 68 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 h the new table
1f786 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 name.. */. if(
1f787 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c sqlite3FindTabl
1f788 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 65 e(db, "sqlite_se
1f789 71 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29 7b quence", zDb) ){
1f78a 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 . sqlite3Nest
1f78b 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a edParse(pParse,.
1f78c 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20 "UPDATE
1f78d 5c 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73 65 \"%w\".sqlite_se
1f78e 71 75 65 6e 63 65 20 73 65 74 20 6e 61 6d 65 20 quence set name
1f78f 3d 20 25 51 20 57 48 45 52 45 20 6e 61 6d 65 20 = %Q WHERE name
1f790 3d 20 25 51 22 2c 0a 20 20 20 20 20 20 20 20 7a = %Q",. z
1f791 44 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d Db, zName, pTab-
1f792 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e >zName);. }.#en
1f793 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
1f794 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1f795 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 . /* If there a
1f796 72 65 20 54 45 4d 50 20 74 72 69 67 67 65 72 73 re TEMP triggers
1f797 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c 20 on this table,
1f798 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 74 modify the sqlit
1f799 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 0a 20 20 e_temp_master.
1f79a 2a 2a 20 74 61 62 6c 65 2e 20 44 6f 6e 27 74 20 ** table. Don't
1f79b 64 6f 20 74 68 69 73 20 69 66 20 74 68 65 20 74 do this if the t
1f79c 61 62 6c 65 20 62 65 69 6e 67 20 41 4c 54 45 52 able being ALTER
1f79d 65 64 20 69 73 20 69 74 73 65 6c 66 20 6c 6f 63 ed is itself loc
1f79e 61 74 65 64 20 69 6e 0a 20 20 2a 2a 20 74 68 65 ated in. ** the
1f79f 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 0a temp database..
1f7a0 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65 */. if( (zWhe
1f7a1 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67 re=whereTempTrig
1f7a2 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 gers(pParse, pTa
1f7a3 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 b))!=0 ){. sq
1f7a4 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 lite3NestedParse
1f7a5 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 (pParse, .
1f7a6 20 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 65 "UPDATE sqlite
1f7a7 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 54 _temp_master SET
1f7a8 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 ". "
1f7a9 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e sql = sqlite_ren
1f7aa 61 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c 2c ame_trigger(sql,
1f7ab 20 25 51 29 2c 20 22 0a 20 20 20 20 20 20 20 20 %Q), ".
1f7ac 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 "tbl_name =
1f7ad 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 %Q ".
1f7ae 20 22 57 48 45 52 45 20 25 73 3b 22 2c 20 7a 4e "WHERE %s;", zN
1f7af 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 ame, zName, zWhe
1f7b0 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 re);. sqlite3
1f7b1 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72 DbFree(db, zWher
1f7b2 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a e);. }.#endif..
1f7b3 20 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72 65 /* Drop and re
1f7b4 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e 61 load the interna
1f7b5 6c 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e 20 l table schema.
1f7b6 2a 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65 */. reloadTable
1f7b7 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70 Schema(pParse, p
1f7b8 54 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78 Tab, zName);..ex
1f7b9 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3a it_rename_table:
1f7ba 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 . sqlite3SrcLis
1f7bb 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 tDelete(db, pSrc
1f7bc 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1f7bd 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d ee(db, zName);.}
1f7be 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 .../*.** This fu
1f7bf 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
1f7c0 20 61 66 74 65 72 20 61 6e 20 22 41 4c 54 45 52 after an "ALTER
1f7c1 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 TABLE ... ADD"
1f7c2 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 statement.** has
1f7c3 20 62 65 65 6e 20 70 61 72 73 65 64 2e 20 41 72 been parsed. Ar
1f7c4 67 75 6d 65 6e 74 20 70 43 6f 6c 44 65 66 20 63 gument pColDef c
1f7c5 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 ontains the text
1f7c6 20 6f 66 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63 of the new.** c
1f7c7 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e olumn definition
1f7c8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c ..**.** The Tabl
1f7c9 65 20 73 74 72 75 63 74 75 72 65 20 70 50 61 72 e structure pPar
1f7ca 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 77 61 se->pNewTable wa
1f7cb 73 20 65 78 74 65 6e 64 65 64 20 74 6f 20 69 6e s extended to in
1f7cc 63 6c 75 64 65 0a 2a 2a 20 74 68 65 20 6e 65 77 clude.** the new
1f7cd 20 63 6f 6c 75 6d 6e 20 64 75 72 69 6e 67 20 70 column during p
1f7ce 61 72 73 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 arsing..*/.SQLIT
1f7cf 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1f7d0 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 qlite3AlterFinis
1f7d1 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 hAddColumn(Parse
1f7d2 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 *pParse, Token
1f7d3 2a 70 43 6f 6c 44 65 66 29 7b 0a 20 20 54 61 62 *pColDef){. Tab
1f7d4 6c 65 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 le *pNew;
1f7d5 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
1f7d6 66 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 f pParse->pNewTa
1f7d7 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ble */. Table *
1f7d8 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 pTab;
1f7d9 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e /* Table bein
1f7da 67 20 61 6c 74 65 72 65 64 20 2a 2f 0a 20 20 69 g altered */. i
1f7db 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 nt iDb;
1f7dc 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
1f7dd 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 base number */.
1f7de 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
1f7df 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 ; /* Da
1f7e0 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 tabase name */.
1f7e1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 const char *zTa
1f7e2 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 b; /* Ta
1f7e3 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 ble name */. ch
1f7e4 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 ar *zCol;
1f7e5 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c 6c 2d /* Null-
1f7e6 74 65 72 6d 69 6e 61 74 65 64 20 63 6f 6c 75 6d terminated colum
1f7e7 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a n definition */.
1f7e8 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 20 Column *pCol;
1f7e9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1f7ea 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f he new column */
1f7eb 0a 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 . Expr *pDflt;
1f7ec 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f7ed 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f Default value fo
1f7ee 72 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e r the new column
1f7ef 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
1f7f0 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
1f7f1 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
1f7f2 63 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2a 2f 0a 0a connection; */..
1f7f3 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 db = pParse->d
1f7f4 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d b;. if( pParse-
1f7f5 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c >nErr || db->mal
1f7f6 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 locFailed ) retu
1f7f7 72 6e 3b 0a 20 20 70 4e 65 77 20 3d 20 70 50 61 rn;. pNew = pPa
1f7f8 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a rse->pNewTable;.
1f7f9 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 20 29 assert( pNew )
1f7fa 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
1f7fb 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c ite3BtreeHoldsAl
1f7fc 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a lMutexes(db) );.
1f7fd 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
1f7fe 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
1f7ff 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 29 3b pNew->pSchema);
1f800 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 . zDb = db->aDb
1f801 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7a [iDb].zName;. z
1f802 54 61 62 20 3d 20 70 4e 65 77 2d 3e 7a 4e 61 6d Tab = pNew->zNam
1f803 65 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 4e 65 e;. pCol = &pNe
1f804 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77 2d 3e 6e 43 w->aCol[pNew->nC
1f805 6f 6c 2d 31 5d 3b 0a 20 20 70 44 66 6c 74 20 3d ol-1];. pDflt =
1f806 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 3b 0a 20 20 pCol->pDflt;.
1f807 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 pTab = sqlite3Fi
1f808 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 ndTable(db, zTab
1f809 2c 20 7a 44 62 29 3b 0a 20 20 61 73 73 65 72 74 , zDb);. assert
1f80a 28 20 70 54 61 62 20 29 3b 0a 0a 23 69 66 6e 64 ( pTab );..#ifnd
1f80b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1f80c 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f UTHORIZATION. /
1f80d 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 * Invoke the aut
1f80e 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 horization callb
1f80f 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 ack. */. if( sq
1f810 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 lite3AuthCheck(p
1f811 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c Parse, SQLITE_AL
1f812 54 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 TER_TABLE, zDb,
1f813 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 pTab->zName, 0)
1f814 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 ){. return;.
1f815 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
1f816 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 If the default v
1f817 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77 alue for the new
1f818 20 63 6f 6c 75 6d 6e 20 77 61 73 20 73 70 65 63 column was spec
1f819 69 66 69 65 64 20 77 69 74 68 20 61 20 0a 20 20 ified with a .
1f81a 2a 2a 20 6c 69 74 65 72 61 6c 20 4e 55 4c 4c 2c ** literal NULL,
1f81b 20 74 68 65 6e 20 73 65 74 20 70 44 66 6c 74 20 then set pDflt
1f81c 74 6f 20 30 2e 20 54 68 69 73 20 73 69 6d 70 6c to 0. This simpl
1f81d 69 66 69 65 73 20 63 68 65 63 6b 69 6e 67 0a 20 ifies checking.
1f81e 20 2a 2a 20 66 6f 72 20 61 6e 20 53 51 4c 20 4e ** for an SQL N
1f81f 55 4c 4c 20 64 65 66 61 75 6c 74 20 62 65 6c 6f ULL default belo
1f820 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 w.. */. if( pD
1f821 66 6c 74 20 26 26 20 70 44 66 6c 74 2d 3e 6f 70 flt && pDflt->op
1f822 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 ==TK_NULL ){.
1f823 20 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a pDflt = 0;. }.
1f824 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 . /* Check that
1f825 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 the new column
1f826 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 is not specified
1f827 20 61 73 20 50 52 49 4d 41 52 59 20 4b 45 59 20 as PRIMARY KEY
1f828 6f 72 20 55 4e 49 51 55 45 2e 0a 20 20 2a 2a 20 or UNIQUE.. **
1f829 49 66 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f If there is a NO
1f82a 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e T NULL constrain
1f82b 74 2c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 t, then the defa
1f82c 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 ult value for th
1f82d 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6d 75 e. ** column mu
1f82e 73 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a st not be NULL..
1f82f 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 2d */. if( pCol-
1f830 3e 69 73 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20 >isPrimKey ){.
1f831 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1f832 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f g(pParse, "Canno
1f833 74 20 61 64 64 20 61 20 50 52 49 4d 41 52 59 20 t add a PRIMARY
1f834 4b 45 59 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 KEY column");.
1f835 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
1f836 69 66 28 20 70 4e 65 77 2d 3e 70 49 6e 64 65 78 if( pNew->pIndex
1f837 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1f838 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1f839 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 55 4e "Cannot add a UN
1f83a 49 51 55 45 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 IQUE column");.
1f83b 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
1f83c 20 69 66 28 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 if( pCol->notNu
1f83d 6c 6c 20 26 26 20 21 70 44 66 6c 74 20 29 7b 0a ll && !pDflt ){.
1f83e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1f83f 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 Msg(pParse, .
1f840 20 20 20 20 20 22 43 61 6e 6e 6f 74 20 61 64 64 "Cannot add
1f841 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75 a NOT NULL colu
1f842 6d 6e 20 77 69 74 68 20 64 65 66 61 75 6c 74 20 mn with default
1f843 76 61 6c 75 65 20 4e 55 4c 4c 22 29 3b 0a 20 20 value NULL");.
1f844 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 return;. }..
1f845 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 64 /* Ensure the d
1f846 65 66 61 75 6c 74 20 65 78 70 72 65 73 73 69 6f efault expressio
1f847 6e 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 74 n is something t
1f848 68 61 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 hat sqlite3Value
1f849 46 72 6f 6d 45 78 70 72 28 29 0a 20 20 2a 2a 20 FromExpr(). **
1f84a 63 61 6e 20 68 61 6e 64 6c 65 20 28 69 2e 65 2e can handle (i.e.
1f84b 20 6e 6f 74 20 43 55 52 52 45 4e 54 5f 54 49 4d not CURRENT_TIM
1f84c 45 20 65 74 63 2e 29 0a 20 20 2a 2f 0a 20 20 69 E etc.). */. i
1f84d 66 28 20 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 f( pDflt ){.
1f84e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1f84f 56 61 6c 3b 0a 20 20 20 20 69 66 28 20 73 71 6c Val;. if( sql
1f850 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 ite3ValueFromExp
1f851 72 28 64 62 2c 20 70 44 66 6c 74 2c 20 53 51 4c r(db, pDflt, SQL
1f852 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 ITE_UTF8, SQLITE
1f853 5f 41 46 46 5f 4e 4f 4e 45 2c 20 26 70 56 61 6c _AFF_NONE, &pVal
1f854 29 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d ) ){. db->m
1f855 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
1f856 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
1f857 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 56 }. if( !pV
1f858 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 al ){. sqli
1f859 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1f85a 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 se, "Cannot add
1f85b 61 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f a column with no
1f85c 6e 2d 63 6f 6e 73 74 61 6e 74 20 64 65 66 61 75 n-constant defau
1f85d 6c 74 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 lt");. retu
1f85e 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 rn;. }. sq
1f85f 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 lite3ValueFree(p
1f860 56 61 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Val);. }.. /*
1f861 4d 6f 64 69 66 79 20 74 68 65 20 43 52 45 41 54 Modify the CREAT
1f862 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e E TABLE statemen
1f863 74 2e 20 2a 2f 0a 20 20 7a 43 6f 6c 20 3d 20 73 t. */. zCol = s
1f864 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
1f865 64 62 2c 20 28 63 68 61 72 2a 29 70 43 6f 6c 44 db, (char*)pColD
1f866 65 66 2d 3e 7a 2c 20 70 43 6f 6c 44 65 66 2d 3e ef->z, pColDef->
1f867 6e 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6c 20 29 n);. if( zCol )
1f868 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 6e 64 {. char *zEnd
1f869 20 3d 20 26 7a 43 6f 6c 5b 70 43 6f 6c 44 65 66 = &zCol[pColDef
1f86a 2d 3e 6e 2d 31 5d 3b 0a 20 20 20 20 77 68 69 6c ->n-1];. whil
1f86b 65 28 20 28 7a 45 6e 64 3e 7a 43 6f 6c 20 26 26 e( (zEnd>zCol &&
1f86c 20 2a 7a 45 6e 64 3d 3d 27 3b 27 29 20 7c 7c 20 *zEnd==';') ||
1f86d 69 73 73 70 61 63 65 28 2a 28 75 6e 73 69 67 6e isspace(*(unsign
1f86e 65 64 20 63 68 61 72 20 2a 29 7a 45 6e 64 29 20 ed char *)zEnd)
1f86f 29 7b 0a 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d ){. *zEnd--
1f870 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20 = '\0';. }.
1f871 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 sqlite3Nested
1f872 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 Parse(pParse, .
1f873 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c "UPDATE \
1f874 22 25 77 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 "%w\".%s SET ".
1f875 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 "sql =
1f876 73 75 62 73 74 72 28 73 71 6c 2c 31 2c 25 64 29 substr(sql,1,%d)
1f877 20 7c 7c 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c || ', ' || %Q |
1f878 7c 20 73 75 62 73 74 72 28 73 71 6c 2c 25 64 29 | substr(sql,%d)
1f879 20 22 0a 20 20 20 20 20 20 20 20 22 57 48 45 52 ". "WHER
1f87a 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27 E type = 'table'
1f87b 20 41 4e 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c AND name = %Q",
1f87c 20 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 . zDb, SCH
1f87d 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 EMA_TABLE(iDb),
1f87e 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 pNew->addColOffs
1f87f 65 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e et, zCol, pNew->
1f880 61 64 64 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a addColOffset+1,.
1f881 20 20 20 20 20 20 7a 54 61 62 0a 20 20 20 20 29 zTab. )
1f882 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
1f883 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 ree(db, zCol);.
1f884 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
1f885 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 default value of
1f886 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 the new column
1f887 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 is NULL, then se
1f888 74 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 t the file. **
1f889 66 6f 72 6d 61 74 20 74 6f 20 32 2e 20 49 66 20 format to 2. If
1f88a 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 the default valu
1f88b 65 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c e of the new col
1f88c 75 6d 6e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c umn is not NULL,
1f88d 0a 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 66 . ** the file f
1f88e 6f 72 6d 61 74 20 62 65 63 6f 6d 65 73 20 33 2e ormat becomes 3.
1f88f 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4d . */. sqlite3M
1f890 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 inimumFileFormat
1f891 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 44 (pParse, iDb, pD
1f892 66 6c 74 20 3f 20 33 20 3a 20 32 29 3b 0a 0a 20 flt ? 3 : 2);..
1f893 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 73 /* Reload the s
1f894 63 68 65 6d 61 20 6f 66 20 74 68 65 20 6d 6f 64 chema of the mod
1f895 69 66 69 65 64 20 74 61 62 6c 65 2e 20 2a 2f 0a ified table. */.
1f896 20 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 reloadTableSch
1f897 65 6d 61 28 70 50 61 72 73 65 2c 20 70 54 61 62 ema(pParse, pTab
1f898 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a , pTab->zName);.
1f899 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
1f89a 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
1f89b 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 61 by the parser a
1f89c 66 74 65 72 20 74 68 65 20 74 61 62 6c 65 2d 6e fter the table-n
1f89d 61 6d 65 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 4c ame in.** an "AL
1f89e 54 45 52 20 54 41 42 4c 45 20 3c 74 61 62 6c 65 TER TABLE <table
1f89f 2d 6e 61 6d 65 3e 20 41 44 44 22 20 73 74 61 74 -name> ADD" stat
1f8a0 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2e ement is parsed.
1f8a1 20 41 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 53 Argument .** pS
1f8a2 72 63 20 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e rc is the full-n
1f8a3 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1f8a4 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 0a being altered..
1f8a5 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1f8a6 6e 65 20 6d 61 6b 65 73 20 61 20 28 70 61 72 74 ne makes a (part
1f8a7 69 61 6c 29 20 63 6f 70 79 20 6f 66 20 74 68 65 ial) copy of the
1f8a8 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 Table structure
1f8a9 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c .** for the tabl
1f8aa 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 20 e being altered
1f8ab 61 6e 64 20 73 65 74 73 20 50 61 72 73 65 2e 70 and sets Parse.p
1f8ac 4e 65 77 54 61 62 6c 65 20 74 6f 20 70 6f 69 6e NewTable to poin
1f8ad 74 0a 2a 2a 20 74 6f 20 69 74 2e 20 52 6f 75 74 t.** to it. Rout
1f8ae 69 6e 65 73 20 63 61 6c 6c 65 64 20 62 79 20 74 ines called by t
1f8af 68 65 20 70 61 72 73 65 72 20 61 73 20 74 68 65 he parser as the
1f8b0 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 column definiti
1f8b1 6f 6e 0a 2a 2a 20 69 73 20 70 61 72 73 65 64 20 on.** is parsed
1f8b2 28 69 2e 65 2e 20 73 71 6c 69 74 65 33 41 64 64 (i.e. sqlite3Add
1f8b3 43 6f 6c 75 6d 6e 28 29 29 20 61 64 64 20 74 68 Column()) add th
1f8b4 65 20 6e 65 77 20 43 6f 6c 75 6d 6e 20 64 61 74 e new Column dat
1f8b5 61 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 70 a to .** the cop
1f8b6 79 2e 20 54 68 65 20 63 6f 70 79 20 6f 66 20 74 y. The copy of t
1f8b7 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 he Table structu
1f8b8 72 65 20 69 73 20 64 65 6c 65 74 65 64 20 62 79 re is deleted by
1f8b9 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a 20 tokenize.c .**
1f8ba 61 66 74 65 72 20 70 61 72 73 69 6e 67 20 69 73 after parsing is
1f8bb 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a 2a finished..**.**
1f8bc 20 52 6f 75 74 69 6e 65 20 73 71 6c 69 74 65 33 Routine sqlite3
1f8bd 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f AlterFinishAddCo
1f8be 6c 75 6d 6e 28 29 20 77 69 6c 6c 20 62 65 20 63 lumn() will be c
1f8bf 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 alled to complet
1f8c0 65 0a 2a 2a 20 63 6f 64 69 6e 67 20 74 68 65 20 e.** coding the
1f8c1 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e "ALTER TABLE ...
1f8c2 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 2e ADD" statement.
1f8c3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1f8c4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
1f8c5 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 lterBeginAddColu
1f8c6 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 mn(Parse *pParse
1f8c7 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 29 , SrcList *pSrc)
1f8c8 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b {. Table *pNew;
1f8c9 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a . Table *pTab;.
1f8ca 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 Vdbe *v;. int
1f8cb 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 iDb;. int i;.
1f8cc 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 73 int nAlloc;. s
1f8cd 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1f8ce 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4c rse->db;.. /* L
1f8cf 6f 6f 6b 20 75 70 20 74 68 65 20 74 61 62 6c 65 ook up the table
1f8d0 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 20 being altered.
1f8d1 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
1f8d2 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d rse->pNewTable==
1f8d3 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
1f8d4 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
1f8d5 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 AllMutexes(db) )
1f8d6 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c ;. if( db->mall
1f8d7 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 ocFailed ) goto
1f8d8 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 exit_begin_add_c
1f8d9 6f 6c 75 6d 6e 3b 0a 20 20 70 54 61 62 20 3d 20 olumn;. pTab =
1f8da 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 sqlite3LocateTab
1f8db 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 le(pParse, 0, pS
1f8dc 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 rc->a[0].zName,
1f8dd 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 pSrc->a[0].zData
1f8de 62 61 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 base);. if( !pT
1f8df 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 ab ) goto exit_b
1f8e0 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b egin_add_column;
1f8e1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1f8e2 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1f8e3 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 LE. if( IsVirtu
1f8e4 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 al(pTab) ){.
1f8e5 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1f8e6 70 50 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c pParse, "virtual
1f8e7 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 tables may not
1f8e8 62 65 20 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 be altered");.
1f8e9 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 goto exit_begi
1f8ea 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 n_add_column;.
1f8eb 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d }.#endif.. /* M
1f8ec 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 69 73 ake sure this is
1f8ed 20 6e 6f 74 20 61 6e 20 61 74 74 65 6d 70 74 20 not an attempt
1f8ee 74 6f 20 41 4c 54 45 52 20 61 20 76 69 65 77 2e to ALTER a view.
1f8ef 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e */. if( pTab->
1f8f0 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 pSelect ){. s
1f8f1 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1f8f2 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 Parse, "Cannot a
1f8f3 64 64 20 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 dd a column to a
1f8f4 20 76 69 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 view");. got
1f8f5 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 o exit_begin_add
1f8f6 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 _column;. }..
1f8f7 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 61 64 assert( pTab->ad
1f8f8 64 43 6f 6c 4f 66 66 73 65 74 3e 30 20 29 3b 0a dColOffset>0 );.
1f8f9 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
1f8fa 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
1f8fb 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b pTab->pSchema);
1f8fc 0a 0a 20 20 2f 2a 20 50 75 74 20 61 20 63 6f 70 .. /* Put a cop
1f8fd 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 y of the Table s
1f8fe 74 72 75 63 74 20 69 6e 20 50 61 72 73 65 2e 70 truct in Parse.p
1f8ff 4e 65 77 54 61 62 6c 65 20 66 6f 72 20 74 68 65 NewTable for the
1f900 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 41 64 64 . ** sqlite3Add
1f901 43 6f 6c 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f Column() functio
1f902 6e 20 61 6e 64 20 66 72 69 65 6e 64 73 20 74 6f n and friends to
1f903 20 6d 6f 64 69 66 79 2e 0a 20 20 2a 2f 0a 20 20 modify.. */.
1f904 70 4e 65 77 20 3d 20 28 54 61 62 6c 65 2a 29 73 pNew = (Table*)s
1f905 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
1f906 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 ro(db, sizeof(Ta
1f907 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 70 4e ble));. if( !pN
1f908 65 77 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 ew ) goto exit_b
1f909 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b egin_add_column;
1f90a 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 . pParse->pNewT
1f90b 61 62 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 70 able = pNew;. p
1f90c 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 New->nRef = 1;.
1f90d 20 70 4e 65 77 2d 3e 64 62 20 3d 20 64 62 3b 0a pNew->db = db;.
1f90e 20 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70 pNew->nCol = p
1f90f 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73 Tab->nCol;. ass
1f910 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e ert( pNew->nCol>
1f911 30 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20 0 );. nAlloc =
1f912 28 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29 (((pNew->nCol-1)
1f913 2f 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 65 /8)*8)+8;. asse
1f914 72 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77 rt( nAlloc>=pNew
1f915 2d 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63 ->nCol && nAlloc
1f916 25 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d %8==0 && nAlloc-
1f917 70 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a pNew->nCol<8 );.
1f918 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28 pNew->aCol = (
1f919 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 44 Column*)sqlite3D
1f91a 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
1f91b 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e sizeof(Column)*n
1f91c 41 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d 3e Alloc);. pNew->
1f91d 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 zName = sqlite3D
1f91e 62 53 74 72 44 75 70 28 64 62 2c 20 70 54 61 62 bStrDup(db, pTab
1f91f 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 ->zName);. if(
1f920 21 70 4e 65 77 2d 3e 61 43 6f 6c 20 7c 7c 20 21 !pNew->aCol || !
1f921 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 pNew->zName ){.
1f922 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
1f923 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 led = 1;. got
1f924 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 o exit_begin_add
1f925 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20 6d _column;. }. m
1f926 65 6d 63 70 79 28 70 4e 65 77 2d 3e 61 43 6f 6c emcpy(pNew->aCol
1f927 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 73 69 , pTab->aCol, si
1f928 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 70 4e 65 zeof(Column)*pNe
1f929 77 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28 w->nCol);. for(
1f92a 69 3d 30 3b 20 69 3c 70 4e 65 77 2d 3e 6e 43 6f i=0; i<pNew->nCo
1f92b 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 43 6f 6c l; i++){. Col
1f92c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 4e 65 umn *pCol = &pNe
1f92d 77 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20 w->aCol[i];.
1f92e 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 pCol->zName = sq
1f92f 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
1f930 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a , pCol->zName);.
1f931 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 pCol->zColl
1f932 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a = 0;. pCol->z
1f933 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 Type = 0;. pC
1f934 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 30 3b 0a 20 ol->pDflt = 0;.
1f935 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 53 63 68 65 }. pNew->pSche
1f936 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 ma = db->aDb[iDb
1f937 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 4e 65 ].pSchema;. pNe
1f938 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 w->addColOffset
1f939 3d 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 = pTab->addColOf
1f93a 66 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 fset;. pNew->nR
1f93b 65 66 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 42 65 ef = 1;.. /* Be
1f93c 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f gin a transactio
1f93d 6e 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 n and increment
1f93e 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 the schema cooki
1f93f 65 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 e. */. sqlite3
1f940 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 BeginWriteOperat
1f941 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 ion(pParse, 0, i
1f942 44 62 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 Db);. v = sqlit
1f943 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
1f944 29 3b 0a 20 20 69 66 28 20 21 76 20 29 20 67 6f );. if( !v ) go
1f945 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 to exit_begin_ad
1f946 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 73 71 6c 69 d_column;. sqli
1f947 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 te3ChangeCookie(
1f948 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a 65 pParse, iDb);..e
1f949 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f xit_begin_add_co
1f94a 6c 75 6d 6e 3a 0a 20 20 73 71 6c 69 74 65 33 53 lumn:. sqlite3S
1f94b 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c rcListDelete(db,
1f94c 20 70 53 72 63 29 3b 0a 20 20 72 65 74 75 72 6e pSrc);. return
1f94d 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 ;.}.#endif /* S
1f94e 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c QLITE_ALTER_TABL
1f94f 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a E */../*********
1f950 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6c 74 ***** End of alt
1f951 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.c ***********
1f952 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f953 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f954 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1f955 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1f956 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a analyze.c *****
1f957 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f958 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f959 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 ****/./*.** 2005
1f95a 20 4a 75 6c 79 20 38 0a 2a 2a 0a 2a 2a 20 54 68 July 8.**.** Th
1f95b 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1f95c 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1f95d 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1f95e 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1f95f 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1f960 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1f961 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1f962 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1f963 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1f964 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1f965 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1f966 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1f967 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1f968 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1f969 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1f96a 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1f96b 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1f96c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f96d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f96e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f96f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f970 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
1f971 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
1f972 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
1f973 68 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f h the ANALYZE co
1f974 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 mmand..**.** @(#
1f975 29 20 24 49 64 3a 20 61 6e 61 6c 79 7a 65 2e 63 ) $Id: analyze.c
1f976 2c 76 20 31 2e 34 37 20 32 30 30 38 2f 31 32 2f ,v 1.47 2008/12/
1f977 31 30 20 31 36 3a 34 35 3a 35 31 20 64 72 68 20 10 16:45:51 drh
1f978 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
1f979 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 SQLITE_OMIT_ANA
1f97a 4c 59 5a 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 LYZE../*.** This
1f97b 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 routine generat
1f97c 65 73 20 63 6f 64 65 20 74 68 61 74 20 6f 70 65 es code that ope
1f97d 6e 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 ns the sqlite_st
1f97e 61 74 31 20 74 61 62 6c 65 20 6f 6e 20 63 75 72 at1 table on cur
1f97f 73 6f 72 0a 2a 2a 20 69 53 74 61 74 43 75 72 2e sor.** iStatCur.
1f980 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 71 .**.** If the sq
1f981 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 lite_stat1 table
1f982 73 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69 s does not previ
1f983 6f 75 73 6c 79 20 65 78 69 73 74 2c 20 69 74 20 ously exist, it
1f984 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 20 49 is created..** I
1f985 66 20 69 74 20 64 6f 65 73 20 70 72 65 76 69 6f f it does previo
1f986 75 73 6c 79 20 65 78 69 73 74 2c 20 61 6c 6c 20 usly exist, all
1f987 65 6e 74 69 72 65 73 20 61 73 73 6f 63 69 61 74 entires associat
1f988 65 64 20 77 69 74 68 20 74 61 62 6c 65 20 7a 57 ed with table zW
1f989 68 65 72 65 0a 2a 2a 20 61 72 65 20 72 65 6d 6f here.** are remo
1f98a 76 65 64 2e 20 20 49 66 20 7a 57 68 65 72 65 3d ved. If zWhere=
1f98b 3d 30 20 74 68 65 6e 20 61 6c 6c 20 65 6e 74 72 =0 then all entr
1f98c 69 65 73 20 61 72 65 20 72 65 6d 6f 76 65 64 2e ies are removed.
1f98d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1f98e 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 0a 20 openStatTable(.
1f98f 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
1f990 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
1f991 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
1f992 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 int iDb,
1f993 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1f994 64 61 74 61 62 61 73 65 20 77 65 20 61 72 65 20 database we are
1f995 6c 6f 6f 6b 69 6e 67 20 69 6e 20 2a 2f 0a 20 20 looking in */.
1f996 69 6e 74 20 69 53 74 61 74 43 75 72 2c 20 20 20 int iStatCur,
1f997 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 /* Open
1f998 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 the sqlite_stat1
1f999 20 74 61 62 6c 65 20 6f 6e 20 74 68 69 73 20 63 table on this c
1f99a 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 ursor */. const
1f99b 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 20 20 char *zWhere
1f99c 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 6e 74 /* Delete ent
1f99d 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 ries associated
1f99e 77 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 20 with this table
1f99f 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 */.){. sqlite3
1f9a0 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1f9a1 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 69 ;. Db *pDb;. i
1f9a2 6e 74 20 69 52 6f 6f 74 50 61 67 65 3b 0a 20 20 nt iRootPage;.
1f9a3 75 38 20 63 72 65 61 74 65 53 74 61 74 31 20 3d u8 createStat1 =
1f9a4 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 53 74 0;. Table *pSt
1f9a5 61 74 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 at;. Vdbe *v =
1f9a6 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 sqlite3GetVdbe(p
1f9a7 50 61 72 73 65 29 3b 0a 0a 20 20 69 66 28 20 76 Parse);.. if( v
1f9a8 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
1f9a9 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1f9aa 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 treeHoldsAllMute
1f9ab 78 65 73 28 64 62 29 20 29 3b 0a 20 20 61 73 73 xes(db) );. ass
1f9ac 65 72 74 28 20 73 71 6c 69 74 65 33 56 64 62 65 ert( sqlite3Vdbe
1f9ad 44 62 28 76 29 3d 3d 64 62 20 29 3b 0a 20 20 70 Db(v)==db );. p
1f9ae 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 Db = &db->aDb[iD
1f9af 62 5d 3b 0a 20 20 69 66 28 20 28 70 53 74 61 74 b];. if( (pStat
1f9b0 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 = sqlite3FindTa
1f9b1 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f ble(db, "sqlite_
1f9b2 73 74 61 74 31 22 2c 20 70 44 62 2d 3e 7a 4e 61 stat1", pDb->zNa
1f9b3 6d 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f me))==0 ){. /
1f9b4 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 * The sqlite_sta
1f9b5 74 31 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e t1 tables does n
1f9b6 6f 74 20 65 78 69 73 74 2e 20 20 43 72 65 61 74 ot exist. Creat
1f9b7 65 20 69 74 2e 20 20 0a 20 20 20 20 2a 2a 20 4e e it. . ** N
1f9b8 6f 74 65 20 74 68 61 74 20 61 20 73 69 64 65 2d ote that a side-
1f9b9 65 66 66 65 63 74 20 6f 66 20 74 68 65 20 43 52 effect of the CR
1f9ba 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 EATE TABLE state
1f9bb 6d 65 6e 74 20 69 73 20 74 6f 20 6c 65 61 76 65 ment is to leave
1f9bc 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 6f 6f 74 . ** the root
1f9bd 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65 77 20 page of the new
1f9be 74 61 62 6c 65 20 69 6e 20 72 65 67 69 73 74 65 table in registe
1f9bf 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f r pParse->regRoo
1f9c0 74 2e 20 20 54 68 69 73 20 69 73 0a 20 20 20 20 t. This is.
1f9c1 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 ** important bec
1f9c2 61 75 73 65 20 74 68 65 20 4f 70 65 6e 57 72 69 ause the OpenWri
1f9c3 74 65 20 6f 70 63 6f 64 65 20 62 65 6c 6f 77 20 te opcode below
1f9c4 77 69 6c 6c 20 62 65 20 6e 65 65 64 69 6e 67 20 will be needing
1f9c5 69 74 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 it. */. sqlit
1f9c6 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 e3NestedParse(pP
1f9c7 61 72 73 65 2c 0a 20 20 20 20 20 20 22 43 52 45 arse,. "CRE
1f9c8 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73 71 6c ATE TABLE %Q.sql
1f9c9 69 74 65 5f 73 74 61 74 31 28 74 62 6c 2c 69 64 ite_stat1(tbl,id
1f9ca 78 2c 73 74 61 74 29 22 2c 0a 20 20 20 20 20 20 x,stat)",.
1f9cb 70 44 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 pDb->zName. )
1f9cc 3b 0a 20 20 20 20 69 52 6f 6f 74 50 61 67 65 20 ;. iRootPage
1f9cd 3d 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f = pParse->regRoo
1f9ce 74 3b 0a 20 20 20 20 63 72 65 61 74 65 53 74 61 t;. createSta
1f9cf 74 31 20 3d 20 31 3b 20 20 2f 2a 20 43 61 75 73 t1 = 1; /* Caus
1f9d0 65 20 72 6f 6f 74 70 61 67 65 20 74 6f 20 62 65 e rootpage to be
1f9d1 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 6f 70 20 taken from top
1f9d2 6f 66 20 73 74 61 63 6b 20 2a 2f 0a 20 20 7d 65 of stack */. }e
1f9d3 6c 73 65 20 69 66 28 20 7a 57 68 65 72 65 20 29 lse if( zWhere )
1f9d4 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c {. /* The sql
1f9d5 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 ite_stat1 table
1f9d6 65 78 69 73 74 73 2e 20 20 44 65 6c 65 74 65 20 exists. Delete
1f9d7 61 6c 6c 20 65 6e 74 72 69 65 73 20 61 73 73 6f all entries asso
1f9d8 63 69 61 74 65 64 20 77 69 74 68 0a 20 20 20 20 ciated with.
1f9d9 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 7a 57 68 ** the table zWh
1f9da 65 72 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 ere. */. sqli
1f9db 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 te3NestedParse(p
1f9dc 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 22 44 Parse,. "D
1f9dd 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 ELETE FROM %Q.sq
1f9de 6c 69 74 65 5f 73 74 61 74 31 20 57 48 45 52 45 lite_stat1 WHERE
1f9df 20 74 62 6c 3d 25 51 22 2c 0a 20 20 20 20 20 20 tbl=%Q",.
1f9e0 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 57 68 pDb->zName, zWh
1f9e1 65 72 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 ere. );. i
1f9e2 52 6f 6f 74 50 61 67 65 20 3d 20 70 53 74 61 74 RootPage = pStat
1f9e3 2d 3e 74 6e 75 6d 3b 0a 20 20 7d 65 6c 73 65 7b ->tnum;. }else{
1f9e4 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 . /* The sqli
1f9e5 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 61 te_stat1 table a
1f9e6 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 20 20 lready exists.
1f9e7 44 65 6c 65 74 65 20 61 6c 6c 20 72 6f 77 73 2e Delete all rows.
1f9e8 20 2a 2f 0a 20 20 20 20 69 52 6f 6f 74 50 61 67 */. iRootPag
1f9e9 65 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b e = pStat->tnum;
1f9ea 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1f9eb 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 AddOp2(v, OP_Cle
1f9ec 61 72 2c 20 70 53 74 61 74 2d 3e 74 6e 75 6d 2c ar, pStat->tnum,
1f9ed 20 69 44 62 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a iDb);. }.. /*
1f9ee 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 Open the sqlite
1f9ef 5f 73 74 61 74 31 20 74 61 62 6c 65 20 66 6f 72 _stat1 table for
1f9f0 20 77 72 69 74 69 6e 67 2e 20 55 6e 6c 65 73 73 writing. Unless
1f9f1 20 69 74 20 77 61 73 20 63 72 65 61 74 65 64 0a it was created.
1f9f2 20 20 2a 2a 20 62 79 20 74 68 69 73 20 76 64 62 ** by this vdb
1f9f3 65 20 70 72 6f 67 72 61 6d 2c 20 6c 6f 63 6b 20 e program, lock
1f9f4 69 74 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 it for writing a
1f9f5 74 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 t the shared-cac
1f9f6 68 65 20 6c 65 76 65 6c 2e 20 0a 20 20 2a 2a 20 he level. . **
1f9f7 49 66 20 74 68 69 73 20 76 64 62 65 20 64 69 64 If this vdbe did
1f9f8 20 63 72 65 61 74 65 20 74 68 65 20 73 71 6c 69 create the sqli
1f9f9 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2c 20 te_stat1 table,
1f9fa 74 68 65 6e 20 69 74 20 6d 75 73 74 20 68 61 76 then it must hav
1f9fb 65 20 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 e . ** already
1f9fc 6f 62 74 61 69 6e 65 64 20 61 20 73 63 68 65 6d obtained a schem
1f9fd 61 2d 6c 6f 63 6b 2c 20 6d 61 6b 69 6e 67 20 74 a-lock, making t
1f9fe 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 72 65 he write-lock re
1f9ff 64 75 6e 64 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 dundant.. */.
1fa00 69 66 28 20 21 63 72 65 61 74 65 53 74 61 74 31 if( !createStat1
1fa01 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 ){. sqlite3T
1fa02 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c ableLock(pParse,
1fa03 20 69 44 62 2c 20 69 52 6f 6f 74 50 61 67 65 2c iDb, iRootPage,
1fa04 20 31 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 1, "sqlite_stat
1fa05 31 22 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 1");. }. sqlit
1fa06 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1fa07 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 OP_SetNumColumns
1fa08 2c 20 30 2c 20 33 29 3b 0a 20 20 73 71 6c 69 74 , 0, 3);. sqlit
1fa09 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
1fa0a 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 53 OP_OpenWrite, iS
1fa0b 74 61 74 43 75 72 2c 20 69 52 6f 6f 74 50 61 67 tatCur, iRootPag
1fa0c 65 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 e, iDb);. sqlit
1fa0d 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 e3VdbeChangeP5(v
1fa0e 2c 20 63 72 65 61 74 65 53 74 61 74 31 29 3b 0a , createStat1);.
1fa0f 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
1fa10 65 20 63 6f 64 65 20 74 6f 20 64 6f 20 61 6e 20 e code to do an
1fa11 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6c 6c 20 analysis of all
1fa12 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 indices associat
1fa13 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e ed with.** a sin
1fa14 67 6c 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 gle table..*/.st
1fa15 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a atic void analyz
1fa16 65 4f 6e 65 54 61 62 6c 65 28 0a 20 20 50 61 72 eOneTable(. Par
1fa17 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a se *pParse, /*
1fa18 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 Parser context
1fa19 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 */. Table *pTab
1fa1a 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 77 , /* Table w
1fa1b 68 6f 73 65 20 69 6e 64 69 63 65 73 20 61 72 65 hose indices are
1fa1c 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 to be analyzed
1fa1d 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75 */. int iStatCu
1fa1e 72 2c 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f r, /* Index o
1fa1f 66 20 56 64 62 65 43 75 72 73 6f 72 20 74 68 61 f VdbeCursor tha
1fa20 74 20 77 72 69 74 65 73 20 74 68 65 20 73 71 6c t writes the sql
1fa21 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 ite_stat1 table
1fa22 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 */. int iMem
1fa23 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 /* Availab
1fa24 6c 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 le memory locati
1fa25 6f 6e 73 20 62 65 67 69 6e 20 68 65 72 65 20 2a ons begin here *
1fa26 2f 0a 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 /.){. Index *pI
1fa27 64 78 3b 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e dx; /* An in
1fa28 64 65 78 20 74 6f 20 62 65 69 6e 67 20 61 6e 61 dex to being ana
1fa29 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 lyzed */. int i
1fa2a 49 64 78 43 75 72 3b 20 20 20 20 20 2f 2a 20 49 IdxCur; /* I
1fa2b 6e 64 65 78 20 6f 66 20 56 64 62 65 43 75 72 73 ndex of VdbeCurs
1fa2c 6f 72 20 66 6f 72 20 69 6e 64 65 78 20 62 65 69 or for index bei
1fa2d 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 ng analyzed */.
1fa2e 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 int nCol;
1fa2f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
1fa30 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e olumns in the in
1fa31 64 65 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 dex */. Vdbe *v
1fa32 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
1fa33 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1fa34 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 being built up
1fa35 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 */. int i;
1fa36 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
1fa37 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 unter */. int t
1fa38 6f 70 4f 66 4c 6f 6f 70 3b 20 20 20 2f 2a 20 54 opOfLoop; /* T
1fa39 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f he top of the lo
1fa3a 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f op */. int endO
1fa3b 66 4c 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65 20 fLoop; /* The
1fa3c 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 end of the loop
1fa3d 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 */. int addr;
1fa3e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 64 64 /* The add
1fa3f 72 65 73 73 20 6f 66 20 61 6e 20 69 6e 73 74 72 ress of an instr
1fa40 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 uction */. int
1fa41 69 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 iDb; /*
1fa42 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 Index of databas
1fa43 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 e containing pTa
1fa44 62 20 2a 2f 0a 0a 20 20 76 20 3d 20 73 71 6c 69 b */.. v = sqli
1fa45 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 te3GetVdbe(pPars
1fa46 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c e);. if( v==0 |
1fa47 7c 20 70 54 61 62 3d 3d 30 20 7c 7c 20 70 54 61 | pTab==0 || pTa
1fa48 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a b->pIndex==0 ){.
1fa49 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 20 61 6e 61 /* Do no ana
1fa4a 6c 79 73 69 73 20 66 6f 72 20 74 61 62 6c 65 73 lysis for tables
1fa4b 20 74 68 61 74 20 68 61 76 65 20 6e 6f 20 69 6e that have no in
1fa4c 64 69 63 65 73 20 2a 2f 0a 20 20 20 20 72 65 74 dices */. ret
1fa4d 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 urn;. }. asser
1fa4e 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
1fa4f 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 oldsAllMutexes(p
1fa50 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 Parse->db) );.
1fa51 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 iDb = sqlite3Sch
1fa52 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 emaToIndex(pPars
1fa53 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 e->db, pTab->pSc
1fa54 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 hema);. assert(
1fa55 20 69 44 62 3e 3d 30 20 29 3b 0a 23 69 66 6e 64 iDb>=0 );.#ifnd
1fa56 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1fa57 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 UTHORIZATION. i
1fa58 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 f( sqlite3AuthCh
1fa59 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 eck(pParse, SQLI
1fa5a 54 45 5f 41 4e 41 4c 59 5a 45 2c 20 70 54 61 62 TE_ANALYZE, pTab
1fa5b 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 ->zName, 0,.
1fa5c 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 pParse->db->aD
1fa5d 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 b[iDb].zName ) )
1fa5e 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
1fa5f 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 }.#endif.. /* E
1fa60 73 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 2d stablish a read-
1fa61 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c lock on the tabl
1fa62 65 20 61 74 20 74 68 65 20 73 68 61 72 65 64 2d e at the shared-
1fa63 63 61 63 68 65 20 6c 65 76 65 6c 2e 20 2a 2f 0a cache level. */.
1fa64 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f sqlite3TableLo
1fa65 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 ck(pParse, iDb,
1fa66 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 pTab->tnum, 0, p
1fa67 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 Tab->zName);..
1fa68 69 49 64 78 43 75 72 20 3d 20 70 50 61 72 73 65 iIdxCur = pParse
1fa69 2d 3e 6e 54 61 62 3b 0a 20 20 66 6f 72 28 70 49 ->nTab;. for(pI
1fa6a 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b dx=pTab->pIndex;
1fa6b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 pIdx; pIdx=pIdx
1fa6c 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 4b 65 ->pNext){. Ke
1fa6d 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 yInfo *pKey = sq
1fa6e 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 lite3IndexKeyinf
1fa6f 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b o(pParse, pIdx);
1fa70 0a 20 20 20 20 69 6e 74 20 72 65 67 46 69 65 6c . int regFiel
1fa71 64 73 3b 20 20 20 20 2f 2a 20 52 65 67 69 73 74 ds; /* Regist
1fa72 65 72 20 62 6c 6f 63 6b 20 66 6f 72 20 62 75 69 er block for bui
1fa73 6c 64 69 6e 67 20 72 65 63 6f 72 64 73 20 2a 2f lding records */
1fa74 0a 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b . int regRec;
1fa75 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
1fa76 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6d 70 6c er holding compl
1fa77 65 74 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20 eted record */.
1fa78 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 3b 20 int regTemp;
1fa79 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 /* Temporar
1fa7a 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20 2a y use register *
1fa7b 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 43 6f 6c /. int regCol
1fa7c 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 ; /* Conte
1fa7d 6e 74 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 66 nt of a column f
1fa7e 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 62 65 rom the table be
1fa7f 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a ing analyzed */.
1fa80 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 int regRowid
1fa81 3b 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 66 ; /* Rowid f
1fa82 6f 72 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 or the inserted
1fa83 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e record */. in
1fa84 74 20 72 65 67 46 32 3b 0a 0a 20 20 20 20 2f 2a t regF2;.. /*
1fa85 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72 20 74 Open a cursor t
1fa86 6f 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 62 o the index to b
1fa87 65 20 61 6e 61 6c 79 7a 65 64 0a 20 20 20 20 2a e analyzed. *
1fa88 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 /. assert( iD
1fa89 62 3d 3d 73 71 6c 69 74 65 33 53 63 68 65 6d 61 b==sqlite3Schema
1fa8a 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e ToIndex(pParse->
1fa8b 64 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 65 6d db, pIdx->pSchem
1fa8c 61 29 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d a) );. nCol =
1fa8d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a pIdx->nColumn;.
1fa8e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1fa8f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e ddOp2(v, OP_SetN
1fa90 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 6e 43 umColumns, 0, nC
1fa91 6f 6c 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 ol+1);. sqlit
1fa92 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
1fa93 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64 OP_OpenRead, iId
1fa94 78 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d xCur, pIdx->tnum
1fa95 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 28 , iDb,. (
1fa96 63 68 61 72 20 2a 29 70 4b 65 79 2c 20 50 34 5f char *)pKey, P4_
1fa97 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 KEYINFO_HANDOFF)
1fa98 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e ;. VdbeCommen
1fa99 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 t((v, "%s", pIdx
1fa9a 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 72 ->zName));. r
1fa9b 65 67 46 69 65 6c 64 73 20 3d 20 69 4d 65 6d 2b egFields = iMem+
1fa9c 6e 43 6f 6c 2a 32 3b 0a 20 20 20 20 72 65 67 54 nCol*2;. regT
1fa9d 65 6d 70 20 3d 20 72 65 67 52 6f 77 69 64 20 3d emp = regRowid =
1fa9e 20 72 65 67 43 6f 6c 20 3d 20 72 65 67 46 69 65 regCol = regFie
1fa9f 6c 64 73 2b 33 3b 0a 20 20 20 20 72 65 67 52 65 lds+3;. regRe
1faa0 63 20 3d 20 72 65 67 43 6f 6c 2b 31 3b 0a 20 20 c = regCol+1;.
1faa1 20 20 69 66 28 20 72 65 67 52 65 63 3e 70 50 61 if( regRec>pPa
1faa2 72 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20 20 20 rse->nMem ){.
1faa3 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 pParse->nMem
1faa4 3d 20 72 65 67 52 65 63 3b 0a 20 20 20 20 7d 0a = regRec;. }.
1faa5 0a 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 . /* Memory c
1faa6 65 6c 6c 73 20 61 72 65 20 75 73 65 64 20 61 73 ells are used as
1faa7 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a follows:. **
1faa8 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 . ** mem[i
1faa9 4d 65 6d 5d 3a 20 20 20 20 20 20 20 20 20 20 20 Mem]:
1faaa 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 The total numb
1faab 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 er of rows in th
1faac 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 e table.. **
1faad 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 31 5d 3a 20 mem[iMem+1]:
1faae 20 20 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72 Number
1faaf 20 6f 66 20 64 69 73 74 69 6e 63 74 20 76 61 6c of distinct val
1fab0 75 65 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 31 0a ues in column 1.
1fab1 20 20 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20 ** ....
1fab2 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d ** mem[iMem
1fab3 2b 6e 43 6f 6c 5d 3a 20 20 20 20 20 20 20 20 4e +nCol]: N
1fab4 75 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 umber of distinc
1fab5 74 20 76 61 6c 75 65 73 20 69 6e 20 63 6f 6c 75 t values in colu
1fab6 6d 6e 20 4e 0a 20 20 20 20 2a 2a 20 20 20 20 6d mn N. ** m
1fab7 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 2b 31 5d 20 em[iMem+nCol+1]
1fab8 20 20 20 20 20 20 4c 61 73 74 20 6f 62 73 65 72 Last obser
1fab9 76 65 64 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c ved value of col
1faba 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 umn 1. **
1fabb 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 .... ** me
1fabc 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 2b 6e 43 6f 6c m[iMem+nCol+nCol
1fabd 5d 3a 20 20 20 4c 61 73 74 20 6f 62 73 65 72 76 ]: Last observ
1fabe 65 64 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75 ed value of colu
1fabf 6d 6e 20 4e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 mn N. **.
1fac0 2a 2a 20 43 65 6c 6c 73 20 69 4d 65 6d 20 74 68 ** Cells iMem th
1fac1 72 6f 75 67 68 20 69 4d 65 6d 2b 6e 43 6f 6c 20 rough iMem+nCol
1fac2 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 are initialized
1fac3 74 6f 20 30 2e 20 20 54 68 65 20 6f 74 68 65 72 to 0. The other
1fac4 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 69 6e 69 s. ** are ini
1fac5 74 69 61 6c 69 7a 65 64 20 74 6f 20 4e 55 4c 4c tialized to NULL
1fac6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
1fac7 28 69 3d 30 3b 20 69 3c 3d 6e 43 6f 6c 3b 20 69 (i=0; i<=nCol; i
1fac8 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
1fac9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1faca 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 OP_Integer, 0, i
1facb 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20 Mem+i);. }.
1facc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f for(i=0; i<nCo
1facd 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 l; i++){. s
1face 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1facf 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 (v, OP_Null, 0,
1fad0 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a iMem+nCol+i+1);.
1fad1 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f }.. /* Do
1fad2 20 74 68 65 20 61 6e 61 6c 79 73 69 73 2e 0a 20 the analysis..
1fad3 20 20 20 2a 2f 0a 20 20 20 20 65 6e 64 4f 66 4c */. endOfL
1fad4 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 oop = sqlite3Vdb
1fad5 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 eMakeLabel(v);.
1fad6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1fad7 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e dOp2(v, OP_Rewin
1fad8 64 2c 20 69 49 64 78 43 75 72 2c 20 65 6e 64 4f d, iIdxCur, endO
1fad9 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 74 6f 70 4f fLoop);. topO
1fada 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 fLoop = sqlite3V
1fadb 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 dbeCurrentAddr(v
1fadc 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1fadd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 beAddOp2(v, OP_A
1fade 64 64 49 6d 6d 2c 20 69 4d 65 6d 2c 20 31 29 3b ddImm, iMem, 1);
1fadf 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1fae0 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nCol; i++){.
1fae1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1fae2 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e Op3(v, OP_Column
1fae3 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20 72 65 , iIdxCur, i, re
1fae4 67 43 6f 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c gCol);. sql
1fae5 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1fae6 2c 20 4f 50 5f 4e 65 2c 20 72 65 67 43 6f 6c 2c , OP_Ne, regCol,
1fae7 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 0, iMem+nCol+i+
1fae8 31 29 3b 0a 20 20 20 20 20 20 2f 2a 2a 2a 2a 20 1);. /****
1fae9 54 4f 44 4f 3a 20 20 61 64 64 20 63 6f 6c 6c 61 TODO: add colla
1faea 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2a ting sequence **
1faeb 2a 2a 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 ***/. sqlit
1faec 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 e3VdbeChangeP5(v
1faed 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e , SQLITE_JUMPIFN
1faee 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ULL);. }.
1faef 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1faf0 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 2(v, OP_Goto, 0,
1faf1 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 endOfLoop);.
1faf2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c for(i=0; i<nCol
1faf3 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 ; i++){. sq
1faf4 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
1faf5 65 28 76 2c 20 74 6f 70 4f 66 4c 6f 6f 70 20 2b e(v, topOfLoop +
1faf6 20 32 2a 28 69 20 2b 20 31 29 29 3b 0a 20 20 20 2*(i + 1));.
1faf7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1faf8 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d dOp2(v, OP_AddIm
1faf9 6d 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 31 29 3b m, iMem+i+1, 1);
1fafa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1fafb 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 beAddOp3(v, OP_C
1fafc 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20 olumn, iIdxCur,
1fafd 69 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 i, iMem+nCol+i+1
1fafe 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
1faff 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c ite3VdbeResolveL
1fb00 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f abel(v, endOfLoo
1fb01 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 p);. sqlite3V
1fb02 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1fb03 4e 65 78 74 2c 20 69 49 64 78 43 75 72 2c 20 74 Next, iIdxCur, t
1fb04 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73 opOfLoop);. s
1fb05 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
1fb06 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49 (v, OP_Close, iI
1fb07 64 78 43 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 dxCur);.. /*
1fb08 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 Store the result
1fb09 73 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 s. . **.
1fb0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 ** The result is
1fb0b 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 a single row of
1fb0c 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 the sqlite_stat
1fb0d 31 20 74 61 62 6c 65 2e 20 20 54 68 65 20 66 69 1 table. The fi
1fb0e 72 73 74 0a 20 20 20 20 2a 2a 20 74 77 6f 20 63 rst. ** two c
1fb0f 6f 6c 75 6d 6e 73 20 61 72 65 20 74 68 65 20 6e olumns are the n
1fb10 61 6d 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c ames of the tabl
1fb11 65 20 61 6e 64 20 69 6e 64 65 78 2e 20 20 54 68 e and index. Th
1fb12 65 20 74 68 69 72 64 20 63 6f 6c 75 6d 6e 0a 20 e third column.
1fb13 20 20 20 2a 2a 20 69 73 20 61 20 73 74 72 69 6e ** is a strin
1fb14 67 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 g composed of a
1fb15 6c 69 73 74 20 6f 66 20 69 6e 74 65 67 65 72 20 list of integer
1fb16 73 74 61 74 69 73 74 69 63 73 20 61 62 6f 75 74 statistics about
1fb17 20 74 68 65 0a 20 20 20 20 2a 2a 20 69 6e 64 65 the. ** inde
1fb18 78 2e 20 20 54 68 65 20 66 69 72 73 74 20 69 6e x. The first in
1fb19 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69 73 teger in the lis
1fb1a 74 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e t is the total n
1fb1b 75 6d 62 65 72 20 6f 66 20 65 6e 74 69 72 65 73 umber of entires
1fb1c 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 69 . ** in the i
1fb1d 6e 64 65 78 2e 20 20 54 68 65 72 65 20 69 73 20 ndex. There is
1fb1e 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 one additional i
1fb1f 6e 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69 nteger in the li
1fb20 73 74 20 66 6f 72 20 65 61 63 68 0a 20 20 20 20 st for each.
1fb21 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 ** column of the
1fb22 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 61 64 table. This ad
1fb23 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 ditional integer
1fb24 20 69 73 20 61 20 67 75 65 73 73 20 6f 66 20 68 is a guess of h
1fb25 6f 77 20 6d 61 6e 79 0a 20 20 20 20 2a 2a 20 72 ow many. ** r
1fb26 6f 77 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ows of the table
1fb27 20 74 68 65 20 69 6e 64 65 78 20 77 69 6c 6c 20 the index will
1fb28 73 65 6c 65 63 74 2e 20 20 49 66 20 44 20 69 73 select. If D is
1fb29 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 64 69 the count of di
1fb2a 73 74 69 6e 63 74 0a 20 20 20 20 2a 2a 20 76 61 stinct. ** va
1fb2b 6c 75 65 73 20 61 6e 64 20 4b 20 69 73 20 74 68 lues and K is th
1fb2c 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
1fb2d 66 20 72 6f 77 73 2c 20 74 68 65 6e 20 74 68 65 f rows, then the
1fb2e 20 69 6e 74 65 67 65 72 20 69 73 20 63 6f 6d 70 integer is comp
1fb2f 75 74 65 64 0a 20 20 20 20 2a 2a 20 61 73 3a 0a uted. ** as:.
1fb30 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
1fb31 20 20 20 20 20 49 20 3d 20 28 4b 2b 44 2d 31 29 I = (K+D-1)
1fb32 2f 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a /D. **. **
1fb33 20 49 66 20 4b 3d 3d 30 20 74 68 65 6e 20 6e 6f If K==0 then no
1fb34 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69 entry is made i
1fb35 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 nto the sqlite_s
1fb36 74 61 74 31 20 74 61 62 6c 65 2e 20 20 0a 20 20 tat1 table. .
1fb37 20 20 2a 2a 20 49 66 20 4b 3e 30 20 74 68 65 6e ** If K>0 then
1fb38 20 69 74 20 69 73 20 61 6c 77 61 79 73 20 74 68 it is always th
1fb39 65 20 63 61 73 65 20 74 68 65 20 44 3e 30 20 73 e case the D>0 s
1fb3a 6f 20 64 69 76 69 73 69 6f 6e 20 62 79 20 7a 65 o division by ze
1fb3b 72 6f 0a 20 20 20 20 2a 2a 20 69 73 20 6e 65 76 ro. ** is nev
1fb3c 65 72 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20 er possible..
1fb3d 20 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 73 */. addr = s
1fb3e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
1fb3f 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 69 4d (v, OP_IfNot, iM
1fb40 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 em);. sqlite3
1fb41 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1fb42 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 _String8, 0, reg
1fb43 46 69 65 6c 64 73 2c 20 30 2c 20 70 54 61 62 2d Fields, 0, pTab-
1fb44 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 >zName, 0);.
1fb45 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1fb46 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 4(v, OP_String8,
1fb47 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2b 31 2c 0, regFields+1,
1fb48 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 0, pIdx->zName,
1fb49 20 30 29 3b 0a 20 20 20 20 72 65 67 46 32 20 3d 0);. regF2 =
1fb4a 20 72 65 67 46 69 65 6c 64 73 2b 32 3b 0a 20 20 regFields+2;.
1fb4b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1fb4c 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c Op2(v, OP_SCopy,
1fb4d 20 69 4d 65 6d 2c 20 72 65 67 46 32 29 3b 0a 20 iMem, regF2);.
1fb4e 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 for(i=0; i<nC
1fb4f 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ol; i++){.
1fb50 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1fb51 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 4(v, OP_String8,
1fb52 20 30 2c 20 72 65 67 54 65 6d 70 2c 20 30 2c 20 0, regTemp, 0,
1fb53 22 20 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 " ", 0);. s
1fb54 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1fb55 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 72 (v, OP_Concat, r
1fb56 65 67 54 65 6d 70 2c 20 72 65 67 46 32 2c 20 72 egTemp, regF2, r
1fb57 65 67 46 32 29 3b 0a 20 20 20 20 20 20 73 71 6c egF2);. sql
1fb58 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1fb59 2c 20 4f 50 5f 41 64 64 2c 20 69 4d 65 6d 2c 20 , OP_Add, iMem,
1fb5a 69 4d 65 6d 2b 69 2b 31 2c 20 72 65 67 54 65 6d iMem+i+1, regTem
1fb5b 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 p);. sqlite
1fb5c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1fb5d 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 54 65 6d P_AddImm, regTem
1fb5e 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 p, -1);. sq
1fb5f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1fb60 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 69 4d v, OP_Divide, iM
1fb61 65 6d 2b 69 2b 31 2c 20 72 65 67 54 65 6d 70 2c em+i+1, regTemp,
1fb62 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 regTemp);.
1fb63 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1fb64 70 31 28 76 2c 20 4f 50 5f 54 6f 49 6e 74 2c 20 p1(v, OP_ToInt,
1fb65 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 regTemp);.
1fb66 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1fb67 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 3(v, OP_Concat,
1fb68 72 65 67 54 65 6d 70 2c 20 72 65 67 46 32 2c 20 regTemp, regF2,
1fb69 72 65 67 46 32 29 3b 0a 20 20 20 20 7d 0a 20 20 regF2);. }.
1fb6a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1fb6b 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 Op4(v, OP_MakeRe
1fb6c 63 6f 72 64 2c 20 72 65 67 46 69 65 6c 64 73 2c cord, regFields,
1fb6d 20 33 2c 20 72 65 67 52 65 63 2c 20 22 61 61 61 3, regRec, "aaa
1fb6e 22 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 ", 0);. sqlit
1fb6f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1fb70 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53 74 OP_NewRowid, iSt
1fb71 61 74 43 75 72 2c 20 72 65 67 52 6f 77 69 64 29 atCur, regRowid)
1fb72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1fb73 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e eAddOp3(v, OP_In
1fb74 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2c 20 sert, iStatCur,
1fb75 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64 regRec, regRowid
1fb76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1fb77 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 beChangeP5(v, OP
1fb78 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 FLAG_APPEND);.
1fb79 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
1fb7a 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a pHere(v, addr);.
1fb7b 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e }.}../*.** Gen
1fb7c 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 erate code that
1fb7d 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20 6d will cause the m
1fb7e 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 64 65 78 ost recent index
1fb7f 20 61 6e 61 6c 79 73 69 73 20 74 6f 0a 2a 2a 20 analysis to.**
1fb80 62 65 20 6c 61 6f 64 65 64 20 69 6e 74 6f 20 69 be laoded into i
1fb81 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 nternal hash tab
1fb82 6c 65 73 20 77 68 65 72 65 20 69 73 20 63 61 6e les where is can
1fb83 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 be used..*/.sta
1fb84 74 69 63 20 76 6f 69 64 20 6c 6f 61 64 41 6e 61 tic void loadAna
1fb85 6c 79 73 69 73 28 50 61 72 73 65 20 2a 70 50 61 lysis(Parse *pPa
1fb86 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 rse, int iDb){.
1fb87 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 Vdbe *v = sqlit
1fb88 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
1fb89 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 );. if( v ){.
1fb8a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1fb8b 4f 70 31 28 76 2c 20 4f 50 5f 4c 6f 61 64 41 6e Op1(v, OP_LoadAn
1fb8c 61 6c 79 73 69 73 2c 20 69 44 62 29 3b 0a 20 20 alysis, iDb);.
1fb8d 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 }.}../*.** Gener
1fb8e 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 ate code that wi
1fb8f 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 ll do an analysi
1fb90 73 20 6f 66 20 61 6e 20 65 6e 74 69 72 65 20 64 s of an entire d
1fb91 61 74 61 62 61 73 65 0a 2a 2f 0a 73 74 61 74 69 atabase.*/.stati
1fb92 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 44 61 c void analyzeDa
1fb93 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 70 50 tabase(Parse *pP
1fb94 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a arse, int iDb){.
1fb95 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1fb96 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 53 63 pParse->db;. Sc
1fb97 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 hema *pSchema =
1fb98 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 db->aDb[iDb].pSc
1fb99 68 65 6d 61 3b 20 20 20 20 2f 2a 20 53 63 68 65 hema; /* Sche
1fb9a 6d 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 69 ma of database i
1fb9b 44 62 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d Db */. HashElem
1fb9c 20 2a 6b 3b 0a 20 20 69 6e 74 20 69 53 74 61 74 *k;. int iStat
1fb9d 43 75 72 3b 0a 20 20 69 6e 74 20 69 4d 65 6d 3b Cur;. int iMem;
1fb9e 0a 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e .. sqlite3Begin
1fb9f 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 WriteOperation(p
1fba0 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a Parse, 0, iDb);.
1fba1 20 20 69 53 74 61 74 43 75 72 20 3d 20 70 50 61 iStatCur = pPa
1fba2 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 6f rse->nTab++;. o
1fba3 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70 50 61 penStatTable(pPa
1fba4 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61 74 43 rse, iDb, iStatC
1fba5 75 72 2c 20 30 29 3b 0a 20 20 69 4d 65 6d 20 3d ur, 0);. iMem =
1fba6 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b pParse->nMem+1;
1fba7 0a 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74 65 48 . for(k=sqliteH
1fba8 61 73 68 46 69 72 73 74 28 26 70 53 63 68 65 6d ashFirst(&pSchem
1fba9 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 6b 3b 20 a->tblHash); k;
1fbaa 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 k=sqliteHashNext
1fbab 28 6b 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 (k)){. Table
1fbac 2a 70 54 61 62 20 3d 20 28 54 61 62 6c 65 2a 29 *pTab = (Table*)
1fbad 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 6b sqliteHashData(k
1fbae 29 3b 0a 20 20 20 20 61 6e 61 6c 79 7a 65 4f 6e );. analyzeOn
1fbaf 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 eTable(pParse, p
1fbb0 54 61 62 2c 20 69 53 74 61 74 43 75 72 2c 20 69 Tab, iStatCur, i
1fbb1 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 6c 6f 61 64 Mem);. }. load
1fbb2 41 6e 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c Analysis(pParse,
1fbb3 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 iDb);.}../*.**
1fbb4 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 Generate code th
1fbb5 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e at will do an an
1fbb6 61 6c 79 73 69 73 20 6f 66 20 61 20 73 69 6e 67 alysis of a sing
1fbb7 6c 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 61 le table in.** a
1fbb8 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 database..*/.st
1fbb9 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a atic void analyz
1fbba 65 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 eTable(Parse *pP
1fbbb 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 arse, Table *pTa
1fbbc 62 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 b){. int iDb;.
1fbbd 20 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 0a int iStatCur;..
1fbbe 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d assert( pTab!=
1fbbf 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
1fbc0 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
1fbc1 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73 AllMutexes(pPars
1fbc2 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44 62 20 e->db) );. iDb
1fbc3 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
1fbc4 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 oIndex(pParse->d
1fbc5 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 b, pTab->pSchema
1fbc6 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 );. sqlite3Begi
1fbc7 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 nWriteOperation(
1fbc8 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b pParse, 0, iDb);
1fbc9 0a 20 20 69 53 74 61 74 43 75 72 20 3d 20 70 50 . iStatCur = pP
1fbca 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 arse->nTab++;.
1fbcb 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70 50 openStatTable(pP
1fbcc 61 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61 74 arse, iDb, iStat
1fbcd 43 75 72 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 Cur, pTab->zName
1fbce 29 3b 0a 20 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 );. analyzeOneT
1fbcf 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 able(pParse, pTa
1fbd0 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 50 61 b, iStatCur, pPa
1fbd1 72 73 65 2d 3e 6e 4d 65 6d 2b 31 29 3b 0a 20 20 rse->nMem+1);.
1fbd2 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 70 50 61 loadAnalysis(pPa
1fbd3 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a rse, iDb);.}../*
1fbd4 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
1fbd5 65 20 66 6f 72 20 74 68 65 20 41 4e 41 4c 59 5a e for the ANALYZ
1fbd6 45 20 63 6f 6d 6d 61 6e 64 2e 20 20 54 68 65 20 E command. The
1fbd7 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 parser calls thi
1fbd8 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 68 65 s routine.** whe
1fbd9 6e 20 69 74 20 72 65 63 6f 67 6e 69 7a 65 73 20 n it recognizes
1fbda 61 6e 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 an ANALYZE comma
1fbdb 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 nd..**.**
1fbdc 20 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 ANALYZE
1fbdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbde 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 -- 1.**
1fbdf 20 20 20 41 4e 41 4c 59 5a 45 20 20 3c 64 61 74 ANALYZE <dat
1fbe0 61 62 61 73 65 3e 20 20 20 20 20 20 20 20 20 20 abase>
1fbe1 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 -- 2.**
1fbe2 20 20 20 20 20 41 4e 41 4c 59 5a 45 20 20 3f 3c ANALYZE ?<
1fbe3 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c database>.?<tabl
1fbe4 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 0a ename> -- 3.**.
1fbe5 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 73 ** Form 1 causes
1fbe6 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 all indices in
1fbe7 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 all attached dat
1fbe8 61 62 61 73 65 73 20 74 6f 20 62 65 20 61 6e 61 abases to be ana
1fbe9 6c 79 7a 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20 32 lyzed..** Form 2
1fbea 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e analyzes all in
1fbeb 64 69 63 65 73 20 74 68 65 20 73 69 6e 67 6c 65 dices the single
1fbec 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 2e database named.
1fbed 0a 2a 2a 20 46 6f 72 6d 20 33 20 61 6e 61 6c 79 .** Form 3 analy
1fbee 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 zes all indices
1fbef 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1fbf0 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e the named table.
1fbf1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1fbf2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
1fbf3 6e 61 6c 79 7a 65 28 50 61 72 73 65 20 2a 70 50 nalyze(Parse *pP
1fbf4 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 arse, Token *pNa
1fbf5 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d me1, Token *pNam
1fbf6 65 32 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a e2){. sqlite3 *
1fbf7 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
1fbf8 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e . int iDb;. in
1fbf9 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 t i;. char *z,
1fbfa 2a 7a 44 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70 *zDb;. Table *p
1fbfb 54 61 62 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 Tab;. Token *pT
1fbfc 61 62 6c 65 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 ableName;.. /*
1fbfd 52 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 Read the databas
1fbfe 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 e schema. If an
1fbff 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 error occurs, le
1fc00 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ave an error mes
1fc01 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f sage. ** and co
1fc02 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 de in pParse and
1fc03 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f return NULL. */
1fc04 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1fc05 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d e3BtreeHoldsAllM
1fc06 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 utexes(pParse->d
1fc07 62 29 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 b) );. if( SQLI
1fc08 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 TE_OK!=sqlite3Re
1fc09 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 adSchema(pParse)
1fc0a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ){. return;.
1fc0b 20 20 7d 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 }.. if( pName
1fc0c 31 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 1==0 ){. /* F
1fc0d 6f 72 6d 20 31 3a 20 20 41 6e 61 6c 79 7a 65 20 orm 1: Analyze
1fc0e 65 76 65 72 79 74 68 69 6e 67 20 2a 2f 0a 20 20 everything */.
1fc0f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
1fc10 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nDb; i++){.
1fc11 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e if( i==1 ) con
1fc12 74 69 6e 75 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f tinue; /* Do no
1fc13 74 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 54 45 t analyze the TE
1fc14 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 MP database */.
1fc15 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 analyzeData
1fc16 62 61 73 65 28 70 50 61 72 73 65 2c 20 69 29 3b base(pParse, i);
1fc17 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
1fc18 66 28 20 70 4e 61 6d 65 32 3d 3d 30 20 7c 7c 20 f( pName2==0 ||
1fc19 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 29 7b 0a pName2->n==0 ){.
1fc1a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32 3a 20 20 /* Form 2:
1fc1b 41 6e 61 6c 79 7a 65 20 74 68 65 20 64 61 74 61 Analyze the data
1fc1c 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6e 61 base or table na
1fc1d 6d 65 64 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d med */. iDb =
1fc1e 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 64 sqlite3FindDb(d
1fc1f 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 b, pName1);.
1fc20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 if( iDb>=0 ){.
1fc21 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62 analyzeDatab
1fc22 61 73 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 ase(pParse, iDb)
1fc23 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1fc24 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 z = sqlite3Na
1fc25 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 meFromToken(db,
1fc26 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 20 69 pName1);. i
1fc27 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 f( z ){.
1fc28 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f pTab = sqlite3Lo
1fc29 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 cateTable(pParse
1fc2a 2c 20 30 2c 20 7a 2c 20 30 29 3b 0a 20 20 20 20 , 0, z, 0);.
1fc2b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1fc2c 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 e(db, z);.
1fc2d 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 if( pTab ){.
1fc2e 20 20 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 54 analyzeT
1fc2f 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 able(pParse, pTa
1fc30 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 b);. }.
1fc31 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 }. }. }e
1fc32 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d lse{. /* Form
1fc33 20 33 3a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 3: Analyze the
1fc34 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 20 fully qualified
1fc35 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 table name */.
1fc36 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 iDb = sqlite3T
1fc37 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 woPartName(pPars
1fc38 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 e, pName1, pName
1fc39 32 2c 20 26 70 54 61 62 6c 65 4e 61 6d 65 29 3b 2, &pTableName);
1fc3a 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d 30 20 . if( iDb>=0
1fc3b 29 7b 0a 20 20 20 20 20 20 7a 44 62 20 3d 20 64 ){. zDb = d
1fc3c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
1fc3d 65 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c e;. z = sql
1fc3e 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
1fc3f 6e 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65 n(db, pTableName
1fc40 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 );. if( z )
1fc41 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d {. pTab =
1fc42 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 sqlite3LocateTa
1fc43 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a ble(pParse, 0, z
1fc44 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 20 , zDb);.
1fc45 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1fc46 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , z);. if
1fc47 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 ( pTab ){.
1fc48 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 analyzeTable
1fc49 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a (pParse, pTab);.
1fc4a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1fc4b 7d 0a 20 20 20 20 7d 20 20 20 0a 20 20 7d 0a 7d }. } . }.}
1fc4c 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 64 20 74 6f 20 ../*.** Used to
1fc4d 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e pass information
1fc4e 20 66 72 6f 6d 20 74 68 65 20 61 6e 61 6c 79 7a from the analyz
1fc4f 65 72 20 72 65 61 64 65 72 20 74 68 72 6f 75 67 er reader throug
1fc50 68 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c h to the.** call
1fc51 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f back routine..*/
1fc52 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1fc53 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 61 6e 61 analysisInfo ana
1fc54 6c 79 73 69 73 49 6e 66 6f 3b 0a 73 74 72 75 63 lysisInfo;.struc
1fc55 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 7b t analysisInfo {
1fc56 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
1fc57 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
1fc58 61 74 61 62 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a atabase;.};../*.
1fc59 2a 2a 20 54 68 69 73 20 63 61 6c 6c 62 61 63 6b ** This callback
1fc5a 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 is invoked once
1fc5b 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 for each index
1fc5c 77 68 65 6e 20 72 65 61 64 69 6e 67 20 74 68 65 when reading the
1fc5d 0a 2a 2a 20 73 71 6c 69 74 65 5f 73 74 61 74 31 .** sqlite_stat1
1fc5e 20 74 61 62 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 table. .**.**
1fc5f 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61 argv[0] = na
1fc60 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 0a me of the index.
1fc61 2a 2a 20 20 20 20 20 61 72 67 76 5b 31 5d 20 3d ** argv[1] =
1fc62 20 72 65 73 75 6c 74 73 20 6f 66 20 61 6e 61 6c results of anal
1fc63 79 73 69 73 20 2d 20 6f 6e 20 69 6e 74 65 67 65 ysis - on intege
1fc64 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d r for each colum
1fc65 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 n.*/.static int
1fc66 61 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 28 76 analysisLoader(v
1fc67 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 oid *pData, int
1fc68 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 argc, char **arg
1fc69 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74 55 73 65 v, char **NotUse
1fc6a 64 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49 6e d){. analysisIn
1fc6b 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 28 61 6e 61 fo *pInfo = (ana
1fc6c 6c 79 73 69 73 49 6e 66 6f 2a 29 70 44 61 74 61 lysisInfo*)pData
1fc6d 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 ;. Index *pInde
1fc6e 78 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 x;. int i, c;.
1fc6f 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 76 3b unsigned int v;
1fc70 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1fc71 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 ;.. assert( arg
1fc72 63 3d 3d 32 20 29 3b 0a 20 20 55 4e 55 53 45 44 c==2 );. UNUSED
1fc73 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
1fc74 73 65 64 2c 20 61 72 67 63 29 3b 0a 0a 20 20 69 sed, argc);.. i
1fc75 66 28 20 61 72 67 76 3d 3d 30 20 7c 7c 20 61 72 f( argv==0 || ar
1fc76 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20 61 72 67 76 gv[0]==0 || argv
1fc77 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 [1]==0 ){. re
1fc78 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49 turn 0;. }. pI
1fc79 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 ndex = sqlite3Fi
1fc7a 6e 64 49 6e 64 65 78 28 70 49 6e 66 6f 2d 3e 64 ndIndex(pInfo->d
1fc7b 62 2c 20 61 72 67 76 5b 30 5d 2c 20 70 49 6e 66 b, argv[0], pInf
1fc7c 6f 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 o->zDatabase);.
1fc7d 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29 if( pIndex==0 )
1fc7e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
1fc7f 20 20 7d 0a 20 20 7a 20 3d 20 61 72 67 76 5b 31 }. z = argv[1
1fc80 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 2a 7a ];. for(i=0; *z
1fc81 20 26 26 20 69 3c 3d 70 49 6e 64 65 78 2d 3e 6e && i<=pIndex->n
1fc82 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 Column; i++){.
1fc83 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 v = 0;. whi
1fc84 6c 65 28 20 28 63 3d 7a 5b 30 5d 29 3e 3d 27 30 le( (c=z[0])>='0
1fc85 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 ' && c<='9' ){.
1fc86 20 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 v = v*10 +
1fc87 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a c - '0';. z
1fc88 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 ++;. }. pI
1fc89 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 5b 69 ndex->aiRowEst[i
1fc8a 5d 20 3d 20 76 3b 0a 20 20 20 20 69 66 28 20 2a ] = v;. if( *
1fc8b 7a 3d 3d 27 20 27 20 29 20 7a 2b 2b 3b 0a 20 20 z==' ' ) z++;.
1fc8c 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
1fc8d 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 74 68 65 20 ./*.** Load the
1fc8e 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 73 content of the s
1fc8f 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c qlite_stat1 tabl
1fc90 65 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 78 e into the index
1fc91 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a 2f hash tables..*/
1fc92 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1fc93 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 int sqlite3Analy
1fc94 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 20 sisLoad(sqlite3
1fc95 2a 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 *db, int iDb){.
1fc96 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 73 49 analysisInfo sI
1fc97 6e 66 6f 3b 0a 20 20 48 61 73 68 45 6c 65 6d 20 nfo;. HashElem
1fc98 2a 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c *i;. char *zSql
1fc99 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 ;. int rc;.. a
1fc9a 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 ssert( iDb>=0 &&
1fc9b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a iDb<db->nDb );.
1fc9c 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 assert( db->aD
1fc9d 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b b[iDb].pBt!=0 );
1fc9e 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1fc9f 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
1fca0 78 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 x(db->aDb[iDb].p
1fca1 42 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 Bt) );.. /* Cle
1fca2 61 72 20 61 6e 79 20 70 72 69 6f 72 20 73 74 61 ar any prior sta
1fca3 74 69 73 74 69 63 73 20 2a 2f 0a 20 20 66 6f 72 tistics */. for
1fca4 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 (i=sqliteHashFir
1fca5 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d st(&db->aDb[iDb]
1fca6 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 .pSchema->idxHas
1fca7 68 29 3b 69 3b 69 3d 73 71 6c 69 74 65 48 61 73 h);i;i=sqliteHas
1fca8 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 49 hNext(i)){. I
1fca9 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73 71 6c ndex *pIdx = sql
1fcaa 69 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a iteHashData(i);.
1fcab 20 20 20 20 73 71 6c 69 74 65 33 44 65 66 61 75 sqlite3Defau
1fcac 6c 74 52 6f 77 45 73 74 28 70 49 64 78 29 3b 0a ltRowEst(pIdx);.
1fcad 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 }.. /* Check
1fcae 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 to make sure the
1fcaf 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 sqlite_stat1 ta
1fcb0 62 6c 65 20 65 78 69 73 74 73 73 20 2a 2f 0a 20 ble existss */.
1fcb1 20 73 49 6e 66 6f 2e 64 62 20 3d 20 64 62 3b 0a sInfo.db = db;.
1fcb2 20 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 sInfo.zDatabas
1fcb3 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d e = db->aDb[iDb]
1fcb4 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73 71 .zName;. if( sq
1fcb5 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 lite3FindTable(d
1fcb6 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31 b, "sqlite_stat1
1fcb7 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 ", sInfo.zDataba
1fcb8 73 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 72 se)==0 ){. r
1fcb9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
1fcba 4f 52 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 4c OR;. }... /* L
1fcbb 6f 61 64 20 6e 65 77 20 73 74 61 74 69 73 74 69 oad new statisti
1fcbc 63 73 20 6f 75 74 20 6f 66 20 74 68 65 20 73 71 cs out of the sq
1fcbd 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 lite_stat1 table
1fcbe 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c */. zSql = sql
1fcbf 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 ite3MPrintf(db,
1fcc0 22 53 45 4c 45 43 54 20 69 64 78 2c 20 73 74 61 "SELECT idx, sta
1fcc1 74 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 t FROM %Q.sqlite
1fcc2 5f 73 74 61 74 31 22 2c 0a 20 20 20 20 20 20 20 _stat1",.
1fcc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcc4 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 sInfo.zDatabase
1fcc5 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 );. (void)sqlit
1fcc6 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b e3SafetyOff(db);
1fcc7 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f . rc = sqlite3_
1fcc8 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 61 exec(db, zSql, a
1fcc9 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 2c 20 26 nalysisLoader, &
1fcca 73 49 6e 66 6f 2c 20 30 29 3b 0a 20 20 28 76 6f sInfo, 0);. (vo
1fccb 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
1fccc 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 On(db);. sqlite
1fccd 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 3DbFree(db, zSql
1fcce 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1fccf 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }...#endif /* SQ
1fcd0 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a LITE_OMIT_ANALYZ
1fcd1 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a E */../*********
1fcd2 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6e 61 ***** End of ana
1fcd3 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a lyze.c *********
1fcd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fcd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fcd6 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1fcd7 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1fcd8 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a 2a attach.c ******
1fcd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fcda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fcdb 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 ****/./*.** 2003
1fcdc 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 April 6.**.** T
1fcdd 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
1fcde 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
1fcdf 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
1fce0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
1fce1 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
1fce2 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
1fce3 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
1fce4 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
1fce5 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
1fce6 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
1fce7 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
1fce8 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1fce9 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
1fcea 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
1fceb 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
1fcec 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
1fced 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
1fcee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fcef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fcf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fcf2 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
1fcf3 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
1fcf4 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 de used to imple
1fcf5 6d 65 6e 74 20 74 68 65 20 41 54 54 41 43 48 20 ment the ATTACH
1fcf6 61 6e 64 20 44 45 54 41 43 48 20 63 6f 6d 6d 61 and DETACH comma
1fcf7 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 nds..**.** $Id:
1fcf8 61 74 74 61 63 68 2e 63 2c 76 20 31 2e 38 31 20 attach.c,v 1.81
1fcf9 32 30 30 38 2f 31 32 2f 31 30 20 31 36 3a 34 35 2008/12/10 16:45
1fcfa 3a 35 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :51 drh Exp $.*/
1fcfb 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1fcfc 5f 4f 4d 49 54 5f 41 54 54 41 43 48 0a 2f 2a 0a _OMIT_ATTACH./*.
1fcfd 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6e 20 65 78 ** Resolve an ex
1fcfe 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77 61 pression that wa
1fcff 73 20 70 61 72 74 20 6f 66 20 61 6e 20 41 54 54 s part of an ATT
1fd00 41 43 48 20 6f 72 20 44 45 54 41 43 48 20 73 74 ACH or DETACH st
1fd01 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 0a 2a 2a atement. This.**
1fd02 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66 is slightly dif
1fd03 66 65 72 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f ferent from reso
1fd04 6c 76 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53 lving a normal S
1fd05 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 QL expression, b
1fd06 65 63 61 75 73 65 20 73 69 6d 70 6c 65 0a 2a 2a ecause simple.**
1fd07 20 69 64 65 6e 74 69 66 69 65 72 73 20 61 72 65 identifiers are
1fd08 20 74 72 65 61 74 65 64 20 61 73 20 73 74 72 69 treated as stri
1fd09 6e 67 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c ngs, not possibl
1fd0a 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f e column names o
1fd0b 72 20 61 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a 2a r aliases..**.**
1fd0c 20 69 2e 65 2e 20 69 66 20 74 68 65 20 70 61 72 i.e. if the par
1fd0d 73 65 72 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 ser sees:.**.**
1fd0e 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42 ATTACH DATAB
1fd0f 41 53 45 20 61 62 63 20 41 53 20 64 65 66 0a 2a ASE abc AS def.*
1fd10 2a 0a 2a 2a 20 69 74 20 74 72 65 61 74 73 20 74 *.** it treats t
1fd11 68 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f he two expressio
1fd12 6e 73 20 61 73 20 6c 69 74 65 72 61 6c 20 73 74 ns as literal st
1fd13 72 69 6e 67 73 20 27 61 62 63 27 20 61 6e 64 20 rings 'abc' and
1fd14 27 64 65 66 27 20 69 6e 73 74 65 61 64 20 6f 66 'def' instead of
1fd15 0a 2a 2a 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 .** looking for
1fd16 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 columns of the s
1fd17 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 ame name..**.**
1fd18 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 This only applie
1fd19 73 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f s to the root no
1fd1a 64 65 20 6f 66 20 70 45 78 70 72 2c 20 73 6f 20 de of pExpr, so
1fd1b 74 68 65 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a the statement:.*
1fd1c 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20 *.** ATTACH
1fd1d 44 41 54 41 42 41 53 45 20 61 62 63 7c 7c 64 65 DATABASE abc||de
1fd1e 66 20 41 53 20 27 64 62 32 27 0a 2a 2a 0a 2a 2a f AS 'db2'.**.**
1fd1f 20 77 69 6c 6c 20 66 61 69 6c 20 62 65 63 61 75 will fail becau
1fd20 73 65 20 6e 65 69 74 68 65 72 20 61 62 63 20 6f se neither abc o
1fd21 72 20 64 65 66 20 63 61 6e 20 62 65 20 72 65 73 r def can be res
1fd22 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 olved..*/.static
1fd23 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 74 74 61 int resolveAtta
1fd24 63 68 45 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65 chExpr(NameConte
1fd25 78 74 20 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20 xt *pName, Expr
1fd26 2a 70 45 78 70 72 29 0a 7b 0a 20 20 69 6e 74 20 *pExpr).{. int
1fd27 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1fd28 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20 if( pExpr ){.
1fd29 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 if( pExpr->op
1fd2a 21 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 !=TK_ID ){.
1fd2b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73 rc = sqlite3Res
1fd2c 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e olveExprNames(pN
1fd2d 61 6d 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 ame, pExpr);.
1fd2e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1fd2f 45 5f 4f 4b 20 26 26 20 21 73 71 6c 69 74 65 33 E_OK && !sqlite3
1fd30 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 ExprIsConstant(p
1fd31 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 Expr) ){.
1fd32 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1fd33 28 70 4e 61 6d 65 2d 3e 70 50 61 72 73 65 2c 20 (pName->pParse,
1fd34 22 69 6e 76 61 6c 69 64 20 6e 61 6d 65 3a 20 5c "invalid name: \
1fd35 22 25 54 5c 22 22 2c 20 26 70 45 78 70 72 2d 3e "%T\"", &pExpr->
1fd36 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 span);. r
1fd37 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
1fd38 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 OR;. }.
1fd39 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 }else{. pEx
1fd3a 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 pr->op = TK_STRI
1fd3b 4e 47 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 NG;. }. }.
1fd3c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1fd3d 0a 2a 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d .** An SQL user-
1fd3e 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 function registe
1fd3f 72 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f red to do the wo
1fd40 72 6b 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 rk of an ATTACH
1fd41 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a statement. The.*
1fd42 2a 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 * three argument
1fd43 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f s to the functio
1fd44 6e 20 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 n come directly
1fd45 66 72 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73 from an attach s
1fd46 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 tatement:.**.**
1fd47 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42 ATTACH DATAB
1fd48 41 53 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a ASE x AS y KEY z
1fd49 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 .**.** SELEC
1fd4a 54 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 T sqlite_attach(
1fd4b 78 2c 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 x, y, z).**.** I
1fd4c 66 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 f the optional "
1fd4d 4b 45 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73 KEY z" syntax is
1fd4e 20 6f 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c omitted, an SQL
1fd4f 20 4e 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20 NULL is passed
1fd50 61 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 as the.** third
1fd51 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 argument..*/.sta
1fd52 74 69 63 20 76 6f 69 64 20 61 74 74 61 63 68 46 tic void attachF
1fd53 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
1fd54 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
1fd55 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a . int NotUsed,.
1fd56 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
1fd57 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 **argv.){. int
1fd58 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b i;. int rc = 0;
1fd59 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1fd5a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1fd5b 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 _db_handle(conte
1fd5c 78 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 xt);. const cha
1fd5d 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 r *zName;. cons
1fd5e 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 t char *zFile;.
1fd5f 20 44 62 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61 Db *aNew;. cha
1fd60 72 20 2a 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a r *zErrDyn = 0;.
1fd61 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d char zErr[128]
1fd62 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ;.. UNUSED_PARA
1fd63 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
1fd64 0a 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 . zFile = (cons
1fd65 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 t char *)sqlite3
1fd66 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
1fd67 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 [0]);. zName =
1fd68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 (const char *)sq
1fd69 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1fd6a 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 (argv[1]);. if(
1fd6b 20 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46 69 6c zFile==0 ) zFil
1fd6c 65 20 3d 20 22 22 3b 0a 20 20 69 66 28 20 7a 4e e = "";. if( zN
1fd6d 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d ame==0 ) zName =
1fd6e 20 22 22 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b "";.. /* Check
1fd6f 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 for the followi
1fd70 6e 67 20 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a ng errors:. **.
1fd71 20 20 2a 2a 20 20 20 20 20 2a 20 54 6f 6f 20 6d ** * Too m
1fd72 61 6e 79 20 61 74 74 61 63 68 65 64 20 64 61 74 any attached dat
1fd73 61 62 61 73 65 73 2c 0a 20 20 2a 2a 20 20 20 20 abases,. **
1fd74 20 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 63 * Transaction c
1fd75 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 0a 20 20 urrently open.
1fd76 2a 2a 20 20 20 20 20 2a 20 53 70 65 63 69 66 69 ** * Specifi
1fd77 65 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 ed database name
1fd78 20 61 6c 72 65 61 64 79 20 62 65 69 6e 67 20 75 already being u
1fd79 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 sed.. */. if(
1fd7a 64 62 2d 3e 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69 db->nDb>=db->aLi
1fd7b 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
1fd7c 5f 41 54 54 41 43 48 45 44 5d 2b 32 20 29 7b 0a _ATTACHED]+2 ){.
1fd7d 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1fd7e 69 6e 74 66 28 0a 20 20 20 20 20 20 73 69 7a 65 intf(. size
1fd7f 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 of(zErr), zErr,
1fd80 22 74 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 "too many attach
1fd81 65 64 20 64 61 74 61 62 61 73 65 73 20 2d 20 6d ed databases - m
1fd82 61 78 20 25 64 22 2c 20 0a 20 20 20 20 20 20 64 ax %d", . d
1fd83 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
1fd84 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d _LIMIT_ATTACHED]
1fd85 0a 20 20 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f . );. goto
1fd86 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 attach_error;.
1fd87 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 }. if( !db->au
1fd88 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 toCommit ){.
1fd89 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1fd8a 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a (sizeof(zErr), z
1fd8b 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Err,.
1fd8c 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f "canno
1fd8d 74 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73 t ATTACH databas
1fd8e 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 e within transac
1fd8f 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f tion");. goto
1fd90 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 attach_error;.
1fd91 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
1fd92 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 db->nDb; i++){.
1fd93 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d char *z = db-
1fd94 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 >aDb[i].zName;.
1fd95 20 20 20 69 66 28 20 7a 20 26 26 20 7a 4e 61 6d if( z && zNam
1fd96 65 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 e && sqlite3StrI
1fd97 43 6d 70 28 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30 Cmp(z, zName)==0
1fd98 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1fd99 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
1fd9a 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 0a f(zErr), zErr, .
1fd9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fd9c 20 20 20 20 20 20 20 22 64 61 74 61 62 61 73 65 "database
1fd9d 20 25 73 20 69 73 20 61 6c 72 65 61 64 79 20 69 %s is already i
1fd9e 6e 20 75 73 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a n use", zName);.
1fd9f 20 20 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 goto attac
1fda0 68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 h_error;. }.
1fda1 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 }.. /* Allocat
1fda2 65 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 e the new entry
1fda3 69 6e 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d in the db->aDb[]
1fda4 20 61 72 72 61 79 20 61 6e 64 20 69 6e 69 74 69 array and initi
1fda5 61 6c 69 73 65 20 74 68 65 20 73 63 68 65 6d 61 alise the schema
1fda6 0a 20 20 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 . ** hash table
1fda7 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 s.. */. if( db
1fda8 2d 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 ->aDb==db->aDbSt
1fda9 61 74 69 63 20 29 7b 0a 20 20 20 20 61 4e 65 77 atic ){. aNew
1fdaa 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1fdab 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 ocRaw(db, sizeof
1fdac 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29 (db->aDb[0])*3 )
1fdad 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d ;. if( aNew==
1fdae 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 0 ) return;.
1fdaf 6d 65 6d 63 70 79 28 61 4e 65 77 2c 20 64 62 2d memcpy(aNew, db-
1fdb0 3e 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d >aDb, sizeof(db-
1fdb1 3e 61 44 62 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d >aDb[0])*2);. }
1fdb2 65 6c 73 65 7b 0a 20 20 20 20 61 4e 65 77 20 3d else{. aNew =
1fdb3 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
1fdb4 63 28 64 62 2c 20 64 62 2d 3e 61 44 62 2c 20 73 c(db, db->aDb, s
1fdb5 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d izeof(db->aDb[0]
1fdb6 29 2a 28 64 62 2d 3e 6e 44 62 2b 31 29 20 29 3b )*(db->nDb+1) );
1fdb7 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 . if( aNew==0
1fdb8 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 ) return;. }.
1fdb9 20 64 62 2d 3e 61 44 62 20 3d 20 61 4e 65 77 3b db->aDb = aNew;
1fdba 0a 20 20 61 4e 65 77 20 3d 20 26 64 62 2d 3e 61 . aNew = &db->a
1fdbb 44 62 5b 64 62 2d 3e 6e 44 62 2b 2b 5d 3b 0a 20 Db[db->nDb++];.
1fdbc 20 6d 65 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c memset(aNew, 0,
1fdbd 20 73 69 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b sizeof(*aNew));
1fdbe 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 .. /* Open the
1fdbf 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
1fdc0 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 73 f the btree is s
1fdc1 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e uccessfully open
1fdc2 65 64 2c 20 75 73 65 0a 20 20 2a 2a 20 69 74 20 ed, use. ** it
1fdc3 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 64 61 to obtain the da
1fdc4 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 41 tabase schema. A
1fdc5 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 t this point the
1fdc6 20 73 63 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a schema may. **
1fdc7 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 or may not be i
1fdc8 6e 69 74 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f nitialised.. */
1fdc9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
1fdca 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 treeFactory(db,
1fdcb 7a 46 69 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45 zFile, 0, SQLITE
1fdcc 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 _DEFAULT_CACHE_S
1fdcd 49 5a 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20 IZE,.
1fdce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdcf 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20 db->openFlags |
1fdd0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
1fdd1 5f 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20 _DB,.
1fdd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdd3 26 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 69 &aNew->pBt);. i
1fdd4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1fdd5 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 ){. Pager *p
1fdd6 50 61 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d Pager;. aNew-
1fdd7 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 >pSchema = sqlit
1fdd8 65 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 e3SchemaGet(db,
1fdd9 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 aNew->pBt);.
1fdda 69 66 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 if( !aNew->pSche
1fddb 6d 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ma ){. rc =
1fddc 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1fddd 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 }else if( aNe
1fdde 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 w->pSchema->file
1fddf 5f 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d _format && aNew-
1fde0 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 >pSchema->enc!=E
1fde1 4e 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 NC(db) ){.
1fde2 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1fde3 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a (sizeof(zErr), z
1fde4 45 72 72 2c 20 0a 20 20 20 20 20 20 20 20 22 61 Err, . "a
1fde5 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
1fde6 73 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 73 s must use the s
1fde7 61 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e ame text encodin
1fde8 67 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61 g as main databa
1fde9 73 65 22 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f se");. goto
1fdea 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 attach_error;.
1fdeb 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 20 }. pPager
1fdec 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 = sqlite3BtreePa
1fded 67 65 72 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a ger(aNew->pBt);.
1fdee 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
1fdef 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 LockingMode(pPag
1fdf0 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b er, db->dfltLock
1fdf1 4d 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 Mode);. sqlit
1fdf2 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f e3PagerJournalMo
1fdf3 64 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e 64 de(pPager, db->d
1fdf4 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b fltJournalMode);
1fdf5 0a 20 20 7d 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 . }. aNew->zNa
1fdf6 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 me = sqlite3DbSt
1fdf7 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b rDup(db, zName);
1fdf8 0a 20 20 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f . aNew->safety_
1fdf9 6c 65 76 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 level = 3;..#if
1fdfa 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
1fdfb 0a 20 20 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 . {. extern
1fdfc 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 63 int sqlite3Codec
1fdfd 41 74 74 61 63 68 28 73 71 6c 69 74 65 33 2a 2c Attach(sqlite3*,
1fdfe 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 int, const void
1fdff 2a 2c 20 69 6e 74 29 3b 0a 20 20 20 20 65 78 74 *, int);. ext
1fe00 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ern void sqlite3
1fe01 43 6f 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69 CodecGetKey(sqli
1fe02 74 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a te3*, int, void*
1fe03 2a 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e *, int*);. in
1fe04 74 20 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 t nKey;. char
1fe05 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20 *zKey;. int
1fe06 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 t = sqlite3_valu
1fe07 65 5f 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3b e_type(argv[2]);
1fe08 0a 20 20 20 20 73 77 69 74 63 68 28 20 74 20 29 . switch( t )
1fe09 7b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c {. case SQL
1fe0a 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 ITE_INTEGER:.
1fe0b 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 case SQLITE_F
1fe0c 4c 4f 41 54 3a 0a 20 20 20 20 20 20 20 20 7a 45 LOAT:. zE
1fe0d 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 44 rrDyn = sqlite3D
1fe0e 62 53 74 72 44 75 70 28 64 62 2c 20 22 49 6e 76 bStrDup(db, "Inv
1fe0f 61 6c 69 64 20 6b 65 79 20 76 61 6c 75 65 22 29 alid key value")
1fe10 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 ;. rc = S
1fe11 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
1fe12 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1fe13 20 20 20 20 0a 20 20 20 20 20 20 63 61 73 65 20 . case
1fe14 53 51 4c 49 54 45 5f 54 45 58 54 3a 0a 20 20 20 SQLITE_TEXT:.
1fe15 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 case SQLITE_B
1fe16 4c 4f 42 3a 0a 20 20 20 20 20 20 20 20 6e 4b 65 LOB:. nKe
1fe17 79 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 y = sqlite3_valu
1fe18 65 5f 62 79 74 65 73 28 61 72 67 76 5b 32 5d 29 e_bytes(argv[2])
1fe19 3b 0a 20 20 20 20 20 20 20 20 7a 4b 65 79 20 3d ;. zKey =
1fe1a 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 (char *)sqlite3
1fe1b 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 _value_blob(argv
1fe1c 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 [2]);. sq
1fe1d 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68 lite3CodecAttach
1fe1e 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 (db, db->nDb-1,
1fe1f 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 zKey, nKey);.
1fe20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 break;..
1fe21 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e case SQLITE_N
1fe22 55 4c 4c 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 ULL:. /*
1fe23 4e 6f 20 6b 65 79 20 73 70 65 63 69 66 69 65 64 No key specified
1fe24 2e 20 20 55 73 65 20 74 68 65 20 6b 65 79 20 66 . Use the key f
1fe25 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 rom the main dat
1fe26 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 abase */.
1fe27 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 sqlite3CodecGet
1fe28 4b 65 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 Key(db, 0, (void
1fe29 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 **)&zKey, &nKey)
1fe2a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1fe2b 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c 3CodecAttach(db,
1fe2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 db->nDb-1, zKey
1fe2d 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 , nKey);.
1fe2e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1fe2f 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 }.#endif.. /* I
1fe30 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 6f f the file was o
1fe31 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c pened successful
1fe32 6c 79 2c 20 72 65 61 64 20 74 68 65 20 73 63 68 ly, read the sch
1fe33 65 6d 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20 ema for the new
1fe34 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 database.. ** I
1fe35 66 20 74 68 69 73 20 66 61 69 6c 73 2c 20 6f 72 f this fails, or
1fe36 20 69 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 if opening the
1fe37 66 69 6c 65 20 66 61 69 6c 65 64 2c 20 74 68 65 file failed, the
1fe38 6e 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 n close the file
1fe39 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 and . ** remov
1fe3a 65 20 74 68 65 20 65 6e 74 72 79 20 66 72 6f 6d e the entry from
1fe3b 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 the db->aDb[] a
1fe3c 72 72 61 79 2e 20 69 2e 65 2e 20 70 75 74 20 65 rray. i.e. put e
1fe3d 76 65 72 79 74 68 69 6e 67 20 62 61 63 6b 20 74 verything back t
1fe3e 68 65 20 77 61 79 0a 20 20 2a 2a 20 77 65 20 66 he way. ** we f
1fe3f 6f 75 6e 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 ound it.. */.
1fe40 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1fe41 4b 20 29 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 K ){. (void)s
1fe42 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1fe43 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 b);. sqlite3B
1fe44 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 treeEnterAll(db)
1fe45 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1fe46 65 33 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 e3Init(db, &zErr
1fe47 44 79 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Dyn);. sqlite
1fe48 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 3BtreeLeaveAll(d
1fe49 62 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 b);. (void)sq
1fe4a 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1fe4b 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 b);. }. if( rc
1fe4c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20 ){. int iDb
1fe4d 3d 20 64 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 = db->nDb - 1;.
1fe4e 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d assert( iDb>=
1fe4f 32 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 2 );. if( db-
1fe50 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b >aDb[iDb].pBt ){
1fe51 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 . sqlite3Bt
1fe52 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 reeClose(db->aDb
1fe53 5b 69 44 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20 [iDb].pBt);.
1fe54 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 db->aDb[iDb].p
1fe55 42 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 62 Bt = 0;. db
1fe56 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 ->aDb[iDb].pSche
1fe57 6d 61 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 ma = 0;. }.
1fe58 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e sqlite3ResetIn
1fe59 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c ternalSchema(db,
1fe5a 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62 0);. db->nDb
1fe5b 20 3d 20 69 44 62 3b 0a 20 20 20 20 69 66 28 20 = iDb;. if(
1fe5c 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
1fe5d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 || rc==SQLITE_I
1fe5e 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 OERR_NOMEM ){.
1fe5f 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
1fe60 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 iled = 1;.
1fe61 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1fe62 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 (sizeof(zErr),zE
1fe63 72 72 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f rr, "out of memo
1fe64 72 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ry");. }else{
1fe65 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1fe66 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
1fe67 45 72 72 29 2c 7a 45 72 72 2c 20 22 75 6e 61 62 Err),zErr, "unab
1fe68 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74 61 62 le to open datab
1fe69 61 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c 65 29 ase: %s", zFile)
1fe6a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f ;. }. goto
1fe6b 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 attach_error;.
1fe6c 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 3b 0a }. . return;.
1fe6d 0a 61 74 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 .attach_error:.
1fe6e 20 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 /* Return an er
1fe6f 72 6f 72 20 69 66 20 77 65 20 67 65 74 20 68 65 ror if we get he
1fe70 72 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45 72 72 re */. if( zErr
1fe71 44 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Dyn ){. sqlit
1fe72 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
1fe73 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44 79 6e context, zErrDyn
1fe74 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 , -1);. sqlit
1fe75 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 e3DbFree(db, zEr
1fe76 72 44 79 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a rDyn);. }else{.
1fe77 20 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 zErr[sizeof(
1fe78 7a 45 72 72 29 2d 31 5d 20 3d 20 30 3b 0a 20 20 zErr)-1] = 0;.
1fe79 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
1fe7a 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 _error(context,
1fe7b 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 zErr, -1);. }.
1fe7c 20 69 66 28 20 72 63 20 29 20 73 71 6c 69 74 65 if( rc ) sqlite
1fe7d 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 3_result_error_c
1fe7e 6f 64 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 ode(context, rc)
1fe7f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 ;.}../*.** An SQ
1fe80 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 L user-function
1fe81 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64 6f registered to do
1fe82 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 the work of an
1fe83 44 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 DETACH statement
1fe84 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 . The.** three a
1fe85 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
1fe86 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 function come di
1fe87 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 20 64 65 rectly from a de
1fe88 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a tach statement:.
1fe89 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 **.** DETACH
1fe8a 20 44 41 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a DATABASE x.**.*
1fe8b 2a 20 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c * SELECT sql
1fe8c 69 74 65 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f ite_detach(x).*/
1fe8d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 74 .static void det
1fe8e 61 63 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 achFunc(. sqlit
1fe8f 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
1fe90 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 ext,. int NotUs
1fe91 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 ed,. sqlite3_va
1fe92 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
1fe93 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
1fe94 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 e = (const char
1fe95 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *)sqlite3_value_
1fe96 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 text(argv[0]);.
1fe97 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 sqlite3 *db = s
1fe98 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
1fe99 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 b_handle(context
1fe9a 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 44 62 );. int i;. Db
1fe9b 20 2a 70 44 62 20 3d 20 30 3b 0a 20 20 63 68 61 *pDb = 0;. cha
1fe9c 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 r zErr[128];..
1fe9d 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1fe9e 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 (NotUsed);.. if
1fe9f 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 ( zName==0 ) zNa
1fea0 6d 65 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69 me = "";. for(i
1fea1 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
1fea2 2b 2b 29 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 ++){. pDb = &
1fea3 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 db->aDb[i];.
1fea4 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 if( pDb->pBt==0
1fea5 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
1fea6 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
1fea7 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a mp(pDb->zName, z
1fea8 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b Name)==0 ) break
1fea9 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3e 3d ;. }.. if( i>=
1feaa 64 62 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 73 db->nDb ){. s
1feab 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1feac 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 sizeof(zErr),zEr
1fead 72 2c 20 22 6e 6f 20 73 75 63 68 20 64 61 74 61 r, "no such data
1feae 62 61 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 base: %s", zName
1feaf 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 );. goto deta
1feb0 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 ch_error;. }.
1feb1 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 73 if( i<2 ){. s
1feb2 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1feb3 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 sizeof(zErr),zEr
1feb4 72 2c 20 22 63 61 6e 6e 6f 74 20 64 65 74 61 63 r, "cannot detac
1feb5 68 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20 h database %s",
1feb6 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f zName);. goto
1feb7 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 detach_error;.
1feb8 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 }. if( !db->au
1feb9 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 toCommit ){.
1feba 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1febb 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a (sizeof(zErr), z
1febc 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Err,.
1febd 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f "canno
1febe 74 20 44 45 54 41 43 48 20 64 61 74 61 62 61 73 t DETACH databas
1febf 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 e within transac
1fec0 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f tion");. goto
1fec1 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 detach_error;.
1fec2 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1fec3 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 BtreeIsInReadTra
1fec4 6e 73 28 70 44 62 2d 3e 70 42 74 29 20 29 7b 0a ns(pDb->pBt) ){.
1fec5 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1fec6 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 intf(sizeof(zErr
1fec7 29 2c 7a 45 72 72 2c 20 22 64 61 74 61 62 61 73 ),zErr, "databas
1fec8 65 20 25 73 20 69 73 20 6c 6f 63 6b 65 64 22 2c e %s is locked",
1fec9 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 zName);. got
1feca 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a o detach_error;.
1fecb 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 }.. sqlite3Bt
1fecc 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 reeClose(pDb->pB
1fecd 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42 74 20 3d t);. pDb->pBt =
1fece 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 0;. pDb->pSche
1fecf 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 ma = 0;. sqlite
1fed0 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 3ResetInternalSc
1fed1 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 72 hema(db, 0);. r
1fed2 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63 68 5f 65 eturn;..detach_e
1fed3 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33 5f rror:. sqlite3_
1fed4 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e result_error(con
1fed5 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b text, zErr, -1);
1fed6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 .}../*.** This p
1fed7 72 6f 63 65 64 75 72 65 20 67 65 6e 65 72 61 74 rocedure generat
1fed8 65 73 20 56 44 42 45 20 63 6f 64 65 20 66 6f 72 es VDBE code for
1fed9 20 61 20 73 69 6e 67 6c 65 20 69 6e 76 6f 63 61 a single invoca
1feda 74 69 6f 6e 20 6f 66 20 65 69 74 68 65 72 20 74 tion of either t
1fedb 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 64 65 74 he.** sqlite_det
1fedc 61 63 68 28 29 20 6f 72 20 73 71 6c 69 74 65 5f ach() or sqlite_
1fedd 61 74 74 61 63 68 28 29 20 53 51 4c 20 75 73 65 attach() SQL use
1fede 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a r functions..*/.
1fedf 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 static void code
1fee0 41 74 74 61 63 68 28 0a 20 20 50 61 72 73 65 20 Attach(. Parse
1fee1 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f *pParse, /
1fee2 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e * The parser con
1fee3 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 74 79 text */. int ty
1fee4 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f pe, /
1fee5 2a 20 45 69 74 68 65 72 20 53 51 4c 49 54 45 5f * Either SQLITE_
1fee6 41 54 54 41 43 48 20 6f 72 20 53 51 4c 49 54 45 ATTACH or SQLITE
1fee7 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20 46 75 6e _DETACH */. Fun
1fee8 63 44 65 66 20 2a 70 46 75 6e 63 2c 20 20 20 20 cDef *pFunc,
1fee9 20 20 2f 2a 20 46 75 6e 63 44 65 66 20 77 72 61 /* FuncDef wra
1feea 70 70 65 72 20 66 6f 72 20 64 65 74 61 63 68 46 pper for detachF
1feeb 75 6e 63 28 29 20 6f 72 20 61 74 74 61 63 68 46 unc() or attachF
1feec 75 6e 63 28 29 20 2a 2f 0a 20 20 45 78 70 72 20 unc() */. Expr
1feed 2a 70 41 75 74 68 41 72 67 2c 20 20 20 20 20 20 *pAuthArg,
1feee 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f /* Expression to
1feef 20 70 61 73 73 20 74 6f 20 61 75 74 68 6f 72 69 pass to authori
1fef0 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 zation callback
1fef1 2a 2f 0a 20 20 45 78 70 72 20 2a 70 46 69 6c 65 */. Expr *pFile
1fef2 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d name, /* Nam
1fef3 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 e of database fi
1fef4 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 le */. Expr *pD
1fef5 62 6e 61 6d 65 2c 20 20 20 20 20 20 20 2f 2a 20 bname, /*
1fef6 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 Name of the data
1fef7 62 61 73 65 20 74 6f 20 75 73 65 20 69 6e 74 65 base to use inte
1fef8 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45 78 70 72 rnally */. Expr
1fef9 20 2a 70 4b 65 79 20 20 20 20 20 20 20 20 20 20 *pKey
1fefa 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6b 65 79 /* Database key
1fefb 20 66 6f 72 20 65 6e 63 72 79 70 74 69 6f 6e 20 for encryption
1fefc 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a extension */.){.
1fefd 20 20 69 6e 74 20 72 63 3b 0a 20 20 4e 61 6d 65 int rc;. Name
1fefe 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65 3b 0a 20 Context sName;.
1feff 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 Vdbe *v;. sqli
1ff00 74 65 33 2a 20 64 62 20 3d 20 70 50 61 72 73 65 te3* db = pParse
1ff01 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 65 67 41 ->db;. int regA
1ff02 72 67 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 rgs;..#ifndef SQ
1ff03 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
1ff04 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 72 74 IZATION. assert
1ff05 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
1ff06 65 64 20 7c 7c 20 70 41 75 74 68 41 72 67 20 29 ed || pAuthArg )
1ff07 3b 0a 20 20 69 66 28 20 70 41 75 74 68 41 72 67 ;. if( pAuthArg
1ff08 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 41 ){. char *zA
1ff09 75 74 68 41 72 67 20 3d 20 73 71 6c 69 74 65 33 uthArg = sqlite3
1ff0a 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 NameFromToken(db
1ff0b 2c 20 26 70 41 75 74 68 41 72 67 2d 3e 73 70 61 , &pAuthArg->spa
1ff0c 6e 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 41 75 n);. if( !zAu
1ff0d 74 68 41 72 67 20 29 7b 0a 20 20 20 20 20 20 67 thArg ){. g
1ff0e 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a oto attach_end;.
1ff0f 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 }. rc = s
1ff10 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
1ff11 70 50 61 72 73 65 2c 20 74 79 70 65 2c 20 7a 41 pParse, type, zA
1ff12 75 74 68 41 72 67 2c 20 30 2c 20 30 29 3b 0a 20 uthArg, 0, 0);.
1ff13 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1ff14 28 64 62 2c 20 7a 41 75 74 68 41 72 67 29 3b 0a (db, zAuthArg);.
1ff15 20 20 20 20 69 66 28 72 63 21 3d 53 51 4c 49 54 if(rc!=SQLIT
1ff16 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f E_OK ){. go
1ff17 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 to attach_end;.
1ff18 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 }. }.#endif
1ff19 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 /* SQLITE_OMIT_A
1ff1a 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a UTHORIZATION */.
1ff1b 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 61 6d 65 . memset(&sName
1ff1c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65 , 0, sizeof(Name
1ff1d 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73 4e 61 Context));. sNa
1ff1e 6d 65 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 me.pParse = pPar
1ff1f 73 65 3b 0a 0a 20 20 69 66 28 20 0a 20 20 20 20 se;.. if( .
1ff20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 SQLITE_OK!=(rc
1ff21 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 = resolveAttach
1ff22 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 46 69 Expr(&sName, pFi
1ff23 6c 65 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 lename)) ||.
1ff24 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 SQLITE_OK!=(rc
1ff25 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 = resolveAttach
1ff26 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 44 62 Expr(&sName, pDb
1ff27 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 name)) ||.
1ff28 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
1ff29 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 resolveAttachEx
1ff2a 70 72 28 26 73 4e 61 6d 65 2c 20 70 4b 65 79 29 pr(&sName, pKey)
1ff2b 29 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 72 73 ). ){. pPars
1ff2c 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 e->nErr++;. g
1ff2d 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a oto attach_end;.
1ff2e 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 }.. v = sqlit
1ff2f 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
1ff30 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d 20 73 );. regArgs = s
1ff31 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e qlite3GetTempRan
1ff32 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b 0a 20 ge(pParse, 4);.
1ff33 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
1ff34 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65 6e 61 (pParse, pFilena
1ff35 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a 20 20 me, regArgs);.
1ff36 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 sqlite3ExprCode(
1ff37 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d 65 2c pParse, pDbname,
1ff38 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20 20 73 regArgs+1);. s
1ff39 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
1ff3a 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72 65 67 Parse, pKey, reg
1ff3b 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73 73 65 Args+2);.. asse
1ff3c 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c rt( v || db->mal
1ff3d 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 locFailed );. i
1ff3e 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 f( v ){. sqli
1ff3f 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
1ff40 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30 2c OP_Function, 0,
1ff41 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75 6e 63 regArgs+3-pFunc
1ff42 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67 73 2b ->nArg, regArgs+
1ff43 33 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 3);. assert(
1ff44 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d 2d 31 20 pFunc->nArg==-1
1ff45 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41 72 67 26 || (pFunc->nArg&
1ff46 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d 3e 6e 41 0xff)==pFunc->nA
1ff47 72 67 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 rg );. sqlite
1ff48 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 3VdbeChangeP5(v,
1ff49 20 28 75 38 29 28 70 46 75 6e 63 2d 3e 6e 41 72 (u8)(pFunc->nAr
1ff4a 67 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 g));. sqlite3
1ff4b 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 VdbeChangeP4(v,
1ff4c 2d 31 2c 20 28 63 68 61 72 20 2a 29 70 46 75 6e -1, (char *)pFun
1ff4d 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a c, P4_FUNCDEF);.
1ff4e 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 . /* Code an
1ff4f 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f 72 20 61 OP_Expire. For a
1ff50 6e 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 n ATTACH stateme
1ff51 6e 74 2c 20 73 65 74 20 50 31 20 74 6f 20 74 72 nt, set P1 to tr
1ff52 75 65 20 28 65 78 70 69 72 65 20 74 68 69 73 0a ue (expire this.
1ff53 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ** statement
1ff54 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 45 54 41 only). For DETA
1ff55 43 48 2c 20 73 65 74 20 69 74 20 74 6f 20 66 61 CH, set it to fa
1ff56 6c 73 65 20 28 65 78 70 69 72 65 20 61 6c 6c 20 lse (expire all
1ff57 65 78 69 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20 existing. **
1ff58 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 20 20 20 statements)..
1ff59 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 */. sqlite3V
1ff5a 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
1ff5b 45 78 70 69 72 65 2c 20 28 74 79 70 65 3d 3d 53 Expire, (type==S
1ff5c 51 4c 49 54 45 5f 41 54 54 41 43 48 29 29 3b 0a QLITE_ATTACH));.
1ff5d 20 20 7d 0a 20 20 0a 61 74 74 61 63 68 5f 65 6e }. .attach_en
1ff5e 64 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 d:. sqlite3Expr
1ff5f 44 65 6c 65 74 65 28 64 62 2c 20 70 46 69 6c 65 Delete(db, pFile
1ff60 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 name);. sqlite3
1ff61 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1ff62 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 Dbname);. sqlit
1ff63 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
1ff64 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pKey);.}../*.**
1ff65 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 Called by the p
1ff66 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69 6c 65 arser to compile
1ff67 20 61 20 44 45 54 41 43 48 20 73 74 61 74 65 6d a DETACH statem
1ff68 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 ent..**.** D
1ff69 45 54 41 43 48 20 70 44 62 6e 61 6d 65 0a 2a 2f ETACH pDbname.*/
1ff6a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1ff6b 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 61 void sqlite3Deta
1ff6c 63 68 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ch(Parse *pParse
1ff6d 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 29 , Expr *pDbname)
1ff6e 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63 44 {. static FuncD
1ff6f 65 66 20 64 65 74 61 63 68 5f 66 75 6e 63 20 3d ef detach_func =
1ff70 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 {. 1,
1ff71 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 /* nArg
1ff72 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 */. SQLITE_U
1ff73 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 TF8, /* iPr
1ff74 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 efEnc */. 0,
1ff75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ff76 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 * flags */. 0
1ff77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1ff78 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a 2f /* pUserData */
1ff79 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 . 0,
1ff7a 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 /* pNext
1ff7b 2a 2f 0a 20 20 20 20 64 65 74 61 63 68 46 75 6e */. detachFun
1ff7c 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e c, /* xFun
1ff7d 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 c */. 0,
1ff7e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 /* xS
1ff7f 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 tep */. 0,
1ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ff81 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 xFinalize */.
1ff82 20 22 73 71 6c 69 74 65 5f 64 65 74 61 63 68 22 "sqlite_detach"
1ff83 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 , /* zName */.
1ff84 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 0
1ff85 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f /* pHash */
1ff86 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 . };. codeAtta
1ff87 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 ch(pParse, SQLIT
1ff88 45 5f 44 45 54 41 43 48 2c 20 26 64 65 74 61 63 E_DETACH, &detac
1ff89 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61 6d 65 2c h_func, pDbname,
1ff8a 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d 65 29 3b 0, 0, pDbname);
1ff8b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 .}../*.** Called
1ff8c 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74 by the parser t
1ff8d 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 41 54 54 o compile an ATT
1ff8e 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a ACH statement..*
1ff8f 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20 *.** ATTACH
1ff90 70 20 41 53 20 70 44 62 6e 61 6d 65 20 4b 45 59 p AS pDbname KEY
1ff91 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f pKey.*/.SQLITE_
1ff92 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1ff93 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73 65 ite3Attach(Parse
1ff94 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
1ff95 70 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 p, Expr *pDbname
1ff96 2c 20 45 78 70 72 20 2a 70 4b 65 79 29 7b 0a 20 , Expr *pKey){.
1ff97 20 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 20 static FuncDef
1ff98 61 74 74 61 63 68 5f 66 75 6e 63 20 3d 20 7b 0a attach_func = {.
1ff99 20 20 20 20 33 2c 20 20 20 20 20 20 20 20 20 20 3,
1ff9a 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f /* nArg */
1ff9b 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 . SQLITE_UTF8
1ff9c 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66 45 , /* iPrefE
1ff9d 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 nc */. 0,
1ff9e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 /* f
1ff9f 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c 20 20 lags */. 0,
1ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ffa1 20 70 55 73 65 72 44 61 74 61 20 2a 2f 0a 20 20 pUserData */.
1ffa2 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
1ffa3 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a /* pNext */.
1ffa4 20 20 20 20 61 74 74 61 63 68 46 75 6e 63 2c 20 attachFunc,
1ffa5 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20 2a /* xFunc *
1ffa6 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 /. 0,
1ffa7 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74 65 70 /* xStep
1ffa8 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 */. 0,
1ffa9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 /* xFi
1ffaa 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20 22 73 nalize */. "s
1ffab 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20 20 qlite_attach",
1ffac 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 /* zName */.
1ffad 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
1ffae 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20 /* pHash */.
1ffaf 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 63 68 28 };. codeAttach(
1ffb0 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 pParse, SQLITE_A
1ffb1 54 54 41 43 48 2c 20 26 61 74 74 61 63 68 5f 66 TTACH, &attach_f
1ffb2 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44 62 6e 61 unc, p, p, pDbna
1ffb3 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e me, pKey);.}.#en
1ffb4 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1ffb5 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a IT_ATTACH */../*
1ffb6 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 .** Initialize a
1ffb7 20 44 62 46 69 78 65 72 20 73 74 72 75 63 74 75 DbFixer structu
1ffb8 72 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e re. This routin
1ffb9 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 e must be called
1ffba 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 70 61 73 prior.** to pas
1ffbb 73 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75 sing the structu
1ffbc 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 re to one of the
1ffbd 20 73 71 6c 69 74 65 46 69 78 41 41 41 41 28 29 sqliteFixAAAA()
1ffbe 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e routines below.
1ffbf 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 .**.** The retur
1ffc0 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 n value indicate
1ffc1 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 s whether or not
1ffc2 20 66 69 78 61 74 69 6f 6e 20 69 73 20 72 65 71 fixation is req
1ffc3 75 69 72 65 64 2e 20 20 54 52 55 45 0a 2a 2a 20 uired. TRUE.**
1ffc4 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 65 65 64 means we do need
1ffc5 20 74 6f 20 66 69 78 20 74 68 65 20 64 61 74 61 to fix the data
1ffc6 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73 2c base references,
1ffc7 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 77 65 20 FALSE means we
1ffc8 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 do not..*/.SQLIT
1ffc9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1ffca 6c 69 74 65 33 46 69 78 49 6e 69 74 28 0a 20 20 lite3FixInit(.
1ffcb 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 DbFixer *pFix,
1ffcc 20 20 20 20 2f 2a 20 54 68 65 20 66 69 78 65 72 /* The fixer
1ffcd 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a to be initializ
1ffce 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 ed */. Parse *p
1ffcf 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 45 Parse, /* E
1ffd0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 69 rror messages wi
1ffd1 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 68 65 ll be written he
1ffd2 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c re */. int iDb,
1ffd3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1ffd4 68 69 73 20 69 73 20 74 68 65 20 64 61 74 61 62 his is the datab
1ffd5 61 73 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 ase that must be
1ffd6 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 used */. const
1ffd7 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 2f char *zType, /
1ffd8 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69 67 67 * "view", "trigg
1ffd9 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78 22 20 er", or "index"
1ffda 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e */. const Token
1ffdb 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 *pName /* Name
1ffdc 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20 74 72 of the view, tr
1ffdd 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65 78 20 igger, or index
1ffde 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 */.){. sqlite3
1ffdf 2a 64 62 3b 0a 0a 20 20 69 66 28 20 69 44 62 3c *db;.. if( iDb<
1ffe0 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 20 72 65 0 || iDb==1 ) re
1ffe1 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20 70 turn 0;. db = p
1ffe2 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 Parse->db;. ass
1ffe3 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e 69 44 62 ert( db->nDb>iDb
1ffe4 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70 50 61 72 );. pFix->pPar
1ffe5 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70 se = pParse;. p
1ffe6 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62 2d 3e 61 Fix->zDb = db->a
1ffe7 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 Db[iDb].zName;.
1ffe8 20 70 46 69 78 2d 3e 7a 54 79 70 65 20 3d 20 7a pFix->zType = z
1ffe9 54 79 70 65 3b 0a 20 20 70 46 69 78 2d 3e 70 4e Type;. pFix->pN
1ffea 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a 20 20 72 ame = pName;. r
1ffeb 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 1;.}../*.*
1ffec 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1ffed 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 set of routines
1ffee 77 61 6c 6b 20 74 68 72 6f 75 67 68 20 74 68 65 walk through the
1ffef 20 70 61 72 73 65 20 74 72 65 65 20 61 6e 64 20 parse tree and
1fff0 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73 70 65 63 assign.** a spec
1fff1 69 66 69 63 20 64 61 74 61 62 61 73 65 20 74 6f ific database to
1fff2 20 61 6c 6c 20 74 61 62 6c 65 20 72 65 66 65 72 all table refer
1fff3 65 6e 63 65 73 20 77 68 65 72 65 20 74 68 65 20 ences where the
1fff4 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a database name.**
1fff5 20 77 61 73 20 6c 65 66 74 20 75 6e 73 70 65 63 was left unspec
1fff6 69 66 69 65 64 20 69 6e 20 74 68 65 20 6f 72 69 ified in the ori
1fff7 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d ginal SQL statem
1fff8 65 6e 74 2e 20 20 54 68 65 20 70 46 69 78 20 73 ent. The pFix s
1fff9 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d 75 73 74 tructure.** must
1fffa 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69 have been initi
1fffb 61 6c 69 7a 65 64 20 62 79 20 61 20 70 72 69 6f alized by a prio
1fffc 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 r call to sqlite
1fffd 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 3FixInit()..**.*
1fffe 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
1ffff 20 61 72 65 20 75 73 65 64 20 74 6f 20 6d 61 6b are used to mak
20000 65 20 73 75 72 65 20 74 68 61 74 20 61 6e 20 69 e sure that an i
20001 6e 64 65 78 2c 20 74 72 69 67 67 65 72 2c 20 6f ndex, trigger, o
20002 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20 6f 6e 65 r.** view in one
20003 20 64 61 74 61 62 61 73 65 20 64 6f 65 73 20 6e database does n
20004 6f 74 20 72 65 66 65 72 20 74 6f 20 6f 62 6a 65 ot refer to obje
20005 63 74 73 20 69 6e 20 61 20 64 69 66 66 65 72 65 cts in a differe
20006 6e 74 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 nt database..**
20007 28 45 78 63 65 70 74 69 6f 6e 3a 20 69 6e 64 69 (Exception: indi
20008 63 65 73 2c 20 74 72 69 67 67 65 72 73 2c 20 61 ces, triggers, a
20009 6e 64 20 76 69 65 77 73 20 69 6e 20 74 68 65 20 nd views in the
2000a 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 61 72 TEMP database ar
2000b 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20 e.** allowed to
2000c 72 65 66 65 72 20 74 6f 20 61 6e 79 74 68 69 6e refer to anythin
2000d 67 2e 29 20 20 49 66 20 61 20 72 65 66 65 72 65 g.) If a refere
2000e 6e 63 65 20 69 73 20 65 78 70 6c 69 63 69 74 6c nce is explicitl
2000f 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 61 6e 20 y made.** to an
20010 6f 62 6a 65 63 74 20 69 6e 20 61 20 64 69 66 66 object in a diff
20011 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2c 20 erent database,
20012 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
20013 20 69 73 20 61 64 64 65 64 20 74 6f 0a 2a 2a 20 is added to.**
20014 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 pParse->zErrMsg
20015 61 6e 64 20 74 68 65 73 65 20 72 6f 75 74 69 6e and these routin
20016 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 es return non-ze
20017 72 6f 2e 20 20 49 66 20 65 76 65 72 79 74 68 69 ro. If everythi
20018 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20 6f 75 74 ng.** checks out
20019 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 , these routines
2001a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 return 0..*/.SQ
2001b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
2001c 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 sqlite3FixSrcLi
2001d 73 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 st(. DbFixer *p
2001e 46 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f Fix, /* Co
2001f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 ntext of the fix
20020 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 ation */. SrcLi
20021 73 74 20 2a 70 4c 69 73 74 20 20 20 20 20 20 20 st *pList
20022 2f 2a 20 54 68 65 20 53 6f 75 72 63 65 20 6c 69 /* The Source li
20023 73 74 20 74 6f 20 63 68 65 63 6b 20 61 6e 64 20 st to check and
20024 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a 20 20 69 modify */.){. i
20025 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 nt i;. const ch
20026 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74 72 75 63 ar *zDb;. struc
20027 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a t SrcList_item *
20028 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 20 70 4c pItem;.. if( pL
20029 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ist==0 ) return
2002a 30 3b 0a 20 20 7a 44 62 20 3d 20 70 46 69 78 2d 0;. zDb = pFix-
2002b 3e 7a 44 62 3b 0a 20 20 66 6f 72 28 69 3d 30 2c >zDb;. for(i=0,
2002c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b pItem=pList->a;
2002d 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 i<pList->nSrc;
2002e 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i++, pItem++){.
2002f 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 if( pItem->zD
20030 61 74 61 62 61 73 65 3d 3d 30 20 29 7b 0a 20 20 atabase==0 ){.
20031 20 20 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 pItem->zData
20032 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 base = sqlite3Db
20033 53 74 72 44 75 70 28 70 46 69 78 2d 3e 70 50 61 StrDup(pFix->pPa
20034 72 73 65 2d 3e 64 62 2c 20 7a 44 62 29 3b 0a 20 rse->db, zDb);.
20035 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c }else if( sql
20036 69 74 65 33 53 74 72 49 43 6d 70 28 70 49 74 65 ite3StrICmp(pIte
20037 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 7a 44 62 m->zDatabase,zDb
20038 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 )!=0 ){. sq
20039 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 46 lite3ErrorMsg(pF
2003a 69 78 2d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20 ix->pParse,.
2003b 20 20 20 20 20 22 25 73 20 25 54 20 63 61 6e 6e "%s %T cann
2003c 6f 74 20 72 65 66 65 72 65 6e 63 65 20 6f 62 6a ot reference obj
2003d 65 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65 ects in database
2003e 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 70 %s",. p
2003f 46 69 78 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78 Fix->zType, pFix
20040 2d 3e 70 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e ->pName, pItem->
20041 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 zDatabase);.
20042 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
20043 7d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 }.#if !defined(S
20044 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 QLITE_OMIT_VIEW)
20045 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
20046 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
20047 29 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ). if( sqlite
20048 33 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 3FixSelect(pFix,
20049 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 pItem->pSelect)
2004a 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 ) return 1;.
2004b 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 if( sqlite3FixE
2004c 78 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d xpr(pFix, pItem-
2004d 3e 70 4f 6e 29 20 29 20 72 65 74 75 72 6e 20 31 >pOn) ) return 1
2004e 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 ;.#endif. }. r
2004f 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 69 66 20 21 eturn 0;.}.#if !
20050 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
20051 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 MIT_VIEW) || !de
20052 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
20053 54 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49 54 T_TRIGGER).SQLIT
20054 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
20055 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 0a lite3FixSelect(.
20056 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c DbFixer *pFix,
20057 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 /* Contex
20058 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f t of the fixatio
20059 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 n */. Select *p
2005a 53 65 6c 65 63 74 20 20 20 20 20 20 2f 2a 20 54 Select /* T
2005b 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
2005c 65 6e 74 20 74 6f 20 62 65 20 66 69 78 65 64 20 ent to be fixed
2005d 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 to one database
2005e 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 */.){. while( p
2005f 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 69 66 Select ){. if
20060 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 ( sqlite3FixExpr
20061 4c 69 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65 List(pFix, pSele
20062 63 74 2d 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20 ct->pEList) ){.
20063 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
20064 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
20065 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 70 ite3FixSrcList(p
20066 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53 Fix, pSelect->pS
20067 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 rc) ){. ret
20068 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 1;. }.
20069 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 if( sqlite3FixE
2006a 78 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63 xpr(pFix, pSelec
2006b 74 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20 t->pWhere) ){.
2006c 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
2006d 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }. if( sqli
2006e 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c te3FixExpr(pFix,
2006f 20 70 53 65 6c 65 63 74 2d 3e 70 48 61 76 69 6e pSelect->pHavin
20070 67 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 g) ){. retu
20071 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
20072 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 pSelect = pSelec
20073 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 t->pPrior;. }.
20074 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c return 0;.}.SQL
20075 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
20076 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 0a sqlite3FixExpr(.
20077 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c DbFixer *pFix,
20078 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 /* Context
20079 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 of the fixation
2007a 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 */. Expr *pExpr
2007b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 /* The e
2007c 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 xpression to be
2007d 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 fixed to one dat
2007e 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 abase */.){. wh
2007f 69 6c 65 28 20 70 45 78 70 72 20 29 7b 0a 20 20 ile( pExpr ){.
20080 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 if( sqlite3Fix
20081 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70 45 78 Select(pFix, pEx
20082 70 72 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a pr->pSelect) ){.
20083 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
20084 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 }. if( sq
20085 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 lite3FixExprList
20086 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 70 4c (pFix, pExpr->pL
20087 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 ist) ){. re
20088 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
20089 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 if( sqlite3Fix
2008a 45 78 70 72 28 70 46 69 78 2c 20 70 45 78 70 72 Expr(pFix, pExpr
2008b 2d 3e 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20 ->pRight) ){.
2008c 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
2008d 20 7d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70 }. pExpr = p
2008e 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d Expr->pLeft;. }
2008f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 . return 0;.}.S
20090 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
20091 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 t sqlite3FixExpr
20092 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 20 List(. DbFixer
20093 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f *pFix, /* Co
20094 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 ntext of the fix
20095 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c ation */. ExprL
20096 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a ist *pList /*
20097 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 The expression
20098 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f to be fixed to o
20099 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 ne database */.)
2009a 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 {. int i;. str
2009b 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
2009c 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 m *pItem;. if(
2009d 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 pList==0 ) retur
2009e 6e 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 n 0;. for(i=0,
2009f 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 pItem=pList->a;
200a0 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 i<pList->nExpr;
200a1 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i++, pItem++){.
200a2 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 if( sqlite3Fi
200a3 78 45 78 70 72 28 70 46 69 78 2c 20 70 49 74 65 xExpr(pFix, pIte
200a4 6d 2d 3e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 m->pExpr) ){.
200a5 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
200a6 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
200a7 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 0;.}.#endif..#if
200a8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
200a9 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45 5f _TRIGGER.SQLITE_
200aa 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
200ab 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65 te3FixTriggerSte
200ac 70 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 p(. DbFixer *pF
200ad 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 ix, /* Conte
200ae 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 xt of the fixati
200af 6f 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 on */. TriggerS
200b0 74 65 70 20 2a 70 53 74 65 70 20 2f 2a 20 54 68 tep *pStep /* Th
200b1 65 20 74 72 69 67 67 65 72 20 73 74 65 70 20 62 e trigger step b
200b2 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 e fixed to one d
200b3 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 atabase */.){.
200b4 77 68 69 6c 65 28 20 70 53 74 65 70 20 29 7b 0a while( pStep ){.
200b5 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 if( sqlite3F
200b6 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70 ixSelect(pFix, p
200b7 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 20 29 Step->pSelect) )
200b8 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
200b9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
200ba 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 sqlite3FixExpr(p
200bb 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 Fix, pStep->pWhe
200bc 72 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 re) ){. ret
200bd 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 1;. }.
200be 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 if( sqlite3FixE
200bf 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 53 xprList(pFix, pS
200c0 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 20 tep->pExprList)
200c1 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
200c2 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 74 1;. }. pSt
200c3 65 70 20 3d 20 70 53 74 65 70 2d 3e 70 4e 65 78 ep = pStep->pNex
200c4 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 t;. }. return
200c5 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 0;.}.#endif../**
200c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
200c7 20 6f 66 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a of attach.c ***
200c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
200c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
200ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
200cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
200cc 69 6e 20 66 69 6c 65 20 61 75 74 68 2e 63 20 2a in file auth.c *
200cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
200ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
200cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
200d0 2a 2a 20 32 30 30 33 20 4a 61 6e 75 61 72 79 20 ** 2003 January
200d1 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 11.**.** The aut
200d2 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
200d3 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
200d4 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
200d5 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
200d6 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
200d7 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
200d8 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
200d9 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
200da 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
200db 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
200dc 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
200dd 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
200de 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
200df 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
200e0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
200e1 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
200e2 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
200e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
200e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
200e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
200e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
200e7 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
200e8 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 ontains code use
200e9 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 d to implement t
200ea 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 he sqlite3_set_a
200eb 75 74 68 6f 72 69 7a 65 72 28 29 0a 2a 2a 20 41 uthorizer().** A
200ec 50 49 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69 PI. This facili
200ed 74 79 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 ty is an optiona
200ee 6c 20 66 65 61 74 75 72 65 20 6f 66 20 74 68 65 l feature of the
200ef 20 6c 69 62 72 61 72 79 2e 20 20 45 6d 62 65 64 library. Embed
200f0 64 65 64 0a 2a 2a 20 73 79 73 74 65 6d 73 20 74 ded.** systems t
200f1 68 61 74 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 hat do not need
200f2 74 68 69 73 20 66 61 63 69 6c 69 74 79 20 6d 61 this facility ma
200f3 79 20 6f 6d 69 74 20 69 74 20 62 79 20 72 65 63 y omit it by rec
200f4 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 ompiling.** the
200f5 6c 69 62 72 61 72 79 20 77 69 74 68 20 2d 44 53 library with -DS
200f6 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
200f7 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a 2a 0a 2a 2a RIZATION=1.**.**
200f8 20 24 49 64 3a 20 61 75 74 68 2e 63 2c 76 20 31 $Id: auth.c,v 1
200f9 2e 32 39 20 32 30 30 37 2f 30 39 2f 31 38 20 31 .29 2007/09/18 1
200fa 35 3a 35 35 3a 30 37 20 64 72 68 20 45 78 70 20 5:55:07 drh Exp
200fb 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 $.*/../*.** All
200fc 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 of the code in t
200fd 68 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65 20 his file may be
200fe 6f 6d 69 74 74 65 64 20 62 79 20 64 65 66 69 6e omitted by defin
200ff 69 6e 67 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 ing a single.**
20100 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 macro..*/.#ifnde
20101 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
20102 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a THORIZATION../*.
20103 2a 2a 20 53 65 74 20 6f 72 20 63 6c 65 61 72 20 ** Set or clear
20104 74 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f the access autho
20105 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f rization functio
20106 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 63 n..**.** The acc
20107 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f ess authorizatio
20108 6e 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 n function is be
20109 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74 called during t
2010a 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a he compilation.*
2010b 2a 20 70 68 61 73 65 20 74 6f 20 76 65 72 69 66 * phase to verif
2010c 79 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20 y that the user
2010d 68 61 73 20 72 65 61 64 20 61 6e 64 2f 6f 72 20 has read and/or
2010e 77 72 69 74 65 20 61 63 63 65 73 73 20 70 65 72 write access per
2010f 6d 69 73 73 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61 mission on.** va
20110 72 69 6f 75 73 20 66 69 65 6c 64 73 20 6f 66 20 rious fields of
20111 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 the database. T
20112 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
20113 74 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 t to the auth fu
20114 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 20 63 nction.** is a c
20115 6f 70 79 20 6f 66 20 74 68 65 20 33 72 64 20 61 opy of the 3rd a
20116 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 rgument to this
20117 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 65 routine. The se
20118 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a cond argument.**
20119 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e to the auth fun
2011a 63 74 69 6f 6e 20 69 73 20 6f 6e 65 20 6f 66 20 ction is one of
2011b 74 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 3a these constants:
2011c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c .**.** SQL
2011d 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 ITE_CREATE_INDEX
2011e 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
2011f 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 0a 2a 2a _CREATE_TABLE.**
20120 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 SQLITE_CR
20121 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 0a EATE_TEMP_INDEX.
20122 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f ** SQLITE_
20123 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c CREATE_TEMP_TABL
20124 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 E.** SQLIT
20125 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 E_CREATE_TEMP_TR
20126 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 IGGER.** S
20127 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d QLITE_CREATE_TEM
20128 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 P_VIEW.**
20129 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 SQLITE_CREATE_TR
2012a 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 IGGER.** S
2012b 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 QLITE_CREATE_VIE
2012c 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 W.** SQLIT
2012d 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 E_DELETE.**
2012e 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e SQLITE_DROP_IN
2012f 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c DEX.** SQL
20130 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 0a 2a ITE_DROP_TABLE.*
20131 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 * SQLITE_D
20132 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a ROP_TEMP_INDEX.*
20133 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 * SQLITE_D
20134 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a ROP_TEMP_TABLE.*
20135 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 * SQLITE_D
20136 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 ROP_TEMP_TRIGGER
20137 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
20138 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 0a _DROP_TEMP_VIEW.
20139 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f ** SQLITE_
2013a 44 52 4f 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 DROP_TRIGGER.**
2013b 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f SQLITE_DRO
2013c 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 P_VIEW.**
2013d 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 0a 2a 2a SQLITE_INSERT.**
2013e 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 50 52 SQLITE_PR
2013f 41 47 4d 41 0a 2a 2a 20 20 20 20 20 20 20 53 51 AGMA.** SQ
20140 4c 49 54 45 5f 52 45 41 44 0a 2a 2a 20 20 20 20 LITE_READ.**
20141 20 20 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 SQLITE_SELECT
20142 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
20143 5f 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 _TRANSACTION.**
20144 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 50 44 SQLITE_UPD
20145 41 54 45 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 ATE.**.** The th
20146 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 ird and fourth a
20147 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
20148 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 61 72 auth function ar
20149 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a e the name of.**
2014a 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74 the table and t
2014b 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 he column that a
2014c 72 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65 re being accesse
2014d 64 2e 20 20 54 68 65 20 61 75 74 68 20 66 75 6e d. The auth fun
2014e 63 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 ction.** should
2014f 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51 return either SQ
20150 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f LITE_OK, SQLITE_
20151 44 45 4e 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f DENY, or SQLITE_
20152 49 47 4e 4f 52 45 2e 20 20 49 66 0a 2a 2a 20 53 IGNORE. If.** S
20153 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
20154 72 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 rned, it means t
20155 68 61 74 20 61 63 63 65 73 73 20 69 73 20 61 6c hat access is al
20156 6c 6f 77 65 64 2e 20 20 53 51 4c 49 54 45 5f 44 lowed. SQLITE_D
20157 45 4e 59 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 ENY.** means tha
20158 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d t the SQL statem
20159 65 6e 74 20 77 69 6c 6c 20 6e 65 76 65 72 2d 72 ent will never-r
2015a 75 6e 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33 un - the sqlite3
2015b 5f 65 78 65 63 28 29 20 63 61 6c 6c 0a 2a 2a 20 _exec() call.**
2015c 77 69 6c 6c 20 72 65 74 75 72 6e 20 77 69 74 68 will return with
2015d 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 an error. SQLI
2015e 54 45 5f 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 TE_IGNORE means
2015f 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 61 that the SQL sta
20160 74 65 6d 65 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64 tement.** should
20161 20 72 75 6e 20 62 75 74 20 61 74 74 65 6d 70 74 run but attempt
20162 73 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 70 s to read the sp
20163 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 77 ecified column w
20164 69 6c 6c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a ill return NULL.
20165 2a 2a 20 61 6e 64 20 61 74 74 65 6d 70 74 73 20 ** and attempts
20166 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 6f 6c to write the col
20167 75 6d 6e 20 77 69 6c 6c 20 62 65 20 69 67 6e 6f umn will be igno
20168 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 red..**.** Setti
20169 6e 67 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 ng the auth func
2016a 74 69 6f 6e 20 74 6f 20 4e 55 4c 4c 20 64 69 73 tion to NULL dis
2016b 61 62 6c 65 73 20 74 68 69 73 20 68 6f 6f 6b 2e ables this hook.
2016c 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a The default.**
2016d 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 setting of the
2016e 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 auth function is
2016f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 NULL..*/.SQLITE
20170 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
20171 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 _set_authorizer(
20172 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a . sqlite3 *db,.
20173 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 int (*xAuth)(v
20174 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 oid*,int,const c
20175 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
20176 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
20177 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 st char*),. voi
20178 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c d *pArg.){. sql
20179 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
2017a 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 (db->mutex);. d
2017b 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68 b->xAuth = xAuth
2017c 3b 0a 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67 ;. db->pAuthArg
2017d 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 = pArg;. sqlit
2017e 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 e3ExpirePrepared
2017f 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a Statements(db);.
20180 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
20181 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 leave(db->mutex)
20182 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
20183 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 E_OK;.}../*.** W
20184 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 rite an error me
20185 73 73 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73 ssage into pPars
20186 65 2d 3e 7a 45 72 72 4d 73 67 20 74 68 61 74 20 e->zErrMsg that
20187 65 78 70 6c 61 69 6e 73 20 74 68 61 74 20 74 68 explains that th
20188 65 0a 2a 2a 20 75 73 65 72 2d 73 75 70 70 6c 69 e.** user-suppli
20189 65 64 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e ed authorization
2018a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
2018b 65 64 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61 ed an illegal va
2018c 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 lue..*/.static v
2018d 6f 69 64 20 73 71 6c 69 74 65 41 75 74 68 42 61 oid sqliteAuthBa
2018e 64 52 65 74 75 72 6e 43 6f 64 65 28 50 61 72 73 dReturnCode(Pars
2018f 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 72 e *pParse, int r
20190 63 29 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 c){. sqlite3Err
20191 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 orMsg(pParse, "i
20192 6c 6c 65 67 61 6c 20 72 65 74 75 72 6e 20 76 61 llegal return va
20193 6c 75 65 20 28 25 64 29 20 66 72 6f 6d 20 74 68 lue (%d) from th
20194 65 20 22 0a 20 20 20 20 22 61 75 74 68 6f 72 69 e ". "authori
20195 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 zation function
20196 2d 20 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49 - should be SQLI
20197 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 49 47 TE_OK, SQLITE_IG
20198 4e 4f 52 45 2c 20 22 0a 20 20 20 20 22 6f 72 20 NORE, ". "or
20199 53 51 4c 49 54 45 5f 44 45 4e 59 22 2c 20 72 63 SQLITE_DENY", rc
2019a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20 );. pParse->rc
2019b 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
2019c 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 45 78 }../*.** The pEx
2019d 70 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 54 pr should be a T
2019e 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 K_COLUMN express
2019f 69 6f 6e 2e 20 20 54 68 65 20 74 61 62 6c 65 20 ion. The table
201a0 72 65 66 65 72 72 65 64 20 74 6f 0a 2a 2a 20 69 referred to.** i
201a1 73 20 69 6e 20 70 54 61 62 4c 69 73 74 20 6f 72 s in pTabList or
201a2 20 65 6c 73 65 20 69 74 20 69 73 20 74 68 65 20 else it is the
201a3 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65 NEW or OLD table
201a4 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 20 20 of a trigger.
201a5 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 .** Check to see
201a6 20 69 66 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 if it is OK to
201a7 72 65 61 64 20 74 68 69 73 20 70 61 72 74 69 63 read this partic
201a8 75 6c 61 72 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a ular column..**.
201a9 2a 2a 20 49 66 20 74 68 65 20 61 75 74 68 20 66 ** If the auth f
201aa 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
201ab 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c 20 63 SQLITE_IGNORE, c
201ac 68 61 6e 67 65 20 74 68 65 20 54 4b 5f 43 4f 4c hange the TK_COL
201ad 55 4d 4e 20 0a 2a 2a 20 69 6e 73 74 72 75 63 74 UMN .** instruct
201ae 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 4e 55 ion into a TK_NU
201af 4c 4c 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 LL. If the auth
201b0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
201b1 73 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c 0a 2a s SQLITE_DENY,.*
201b2 2a 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 * then generate
201b3 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c an error..*/.SQL
201b4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
201b5 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 sqlite3AuthRead
201b6 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
201b7 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 e, /* The
201b8 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 parser context
201b9 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 */. Expr *pExpr
201ba 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 , /* Th
201bb 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 e expression to
201bc 63 68 65 63 6b 20 61 75 74 68 6f 72 69 7a 61 74 check authorizat
201bd 69 6f 6e 20 6f 6e 20 2a 2f 0a 20 20 53 63 68 65 ion on */. Sche
201be 6d 61 20 2a 70 53 63 68 65 6d 61 2c 20 20 20 20 ma *pSchema,
201bf 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d 61 20 /* The schema
201c0 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
201c1 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a n */. SrcList *
201c2 70 54 61 62 4c 69 73 74 20 20 20 20 20 2f 2a 20 pTabList /*
201c3 41 6c 6c 20 74 61 62 6c 65 20 74 68 61 74 20 70 All table that p
201c4 45 78 70 72 20 6d 69 67 68 74 20 72 65 66 65 72 Expr might refer
201c5 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 to */.){. sqli
201c6 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
201c7 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a ->db;. int rc;.
201c8 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 Table *pTab =
201c9 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 0; /* The t
201ca 61 62 6c 65 20 62 65 69 6e 67 20 72 65 61 64 20 able being read
201cb 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
201cc 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f 2a 20 4e 61 *zCol; /* Na
201cd 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e me of the column
201ce 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f of the table */
201cf 0a 20 20 69 6e 74 20 69 53 72 63 3b 20 20 20 20 . int iSrc;
201d0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
201d1 78 20 69 6e 20 70 54 61 62 4c 69 73 74 2d 3e 61 x in pTabList->a
201d2 5b 5d 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e [] of table bein
201d3 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 g read */. cons
201d4 74 20 63 68 61 72 20 2a 7a 44 42 61 73 65 3b 20 t char *zDBase;
201d5 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 /* Name of dat
201d6 61 62 61 73 65 20 62 65 69 6e 67 20 61 63 63 65 abase being acce
201d7 73 73 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65 ssed */. Trigge
201d8 72 53 74 61 63 6b 20 2a 70 53 74 61 63 6b 3b 20 rStack *pStack;
201d9 2f 2a 20 54 68 65 20 73 74 61 63 6b 20 6f 66 20 /* The stack of
201da 63 75 72 72 65 6e 74 20 74 72 69 67 67 65 72 73 current triggers
201db 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 */. int iDb;
201dc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
201dd 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
201de 64 61 74 61 62 61 73 65 20 74 68 65 20 65 78 70 database the exp
201df 72 65 73 73 69 6f 6e 20 72 65 66 65 72 73 20 74 ression refers t
201e0 6f 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e o */.. if( db->
201e1 78 41 75 74 68 3d 3d 30 20 29 20 72 65 74 75 72 xAuth==0 ) retur
201e2 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e n;. if( pExpr->
201e3 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 op!=TK_COLUMN )
201e4 72 65 74 75 72 6e 3b 0a 20 20 69 44 62 20 3d 20 return;. iDb =
201e5 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
201e6 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c ndex(pParse->db,
201e7 20 70 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 pSchema);. if(
201e8 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 2f 2a iDb<0 ){. /*
201e9 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 An attempt to r
201ea 65 61 64 20 61 20 63 6f 6c 75 6d 6e 20 6f 75 74 ead a column out
201eb 20 6f 66 20 61 20 73 75 62 71 75 65 72 79 20 6f of a subquery o
201ec 72 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 r other. ** t
201ed 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 emporary table.
201ee 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 */. return;.
201ef 20 7d 0a 20 20 66 6f 72 28 69 53 72 63 3d 30 3b }. for(iSrc=0;
201f0 20 70 54 61 62 4c 69 73 74 20 26 26 20 69 53 72 pTabList && iSr
201f1 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 c<pTabList->nSrc
201f2 3b 20 69 53 72 63 2b 2b 29 7b 0a 20 20 20 20 69 ; iSrc++){. i
201f3 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 f( pExpr->iTable
201f4 3d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53 ==pTabList->a[iS
201f5 72 63 5d 2e 69 43 75 72 73 6f 72 20 29 20 62 72 rc].iCursor ) br
201f6 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 eak;. }. if( i
201f7 53 72 63 3e 3d 30 20 26 26 20 70 54 61 62 4c 69 Src>=0 && pTabLi
201f8 73 74 20 26 26 20 69 53 72 63 3c 70 54 61 62 4c st && iSrc<pTabL
201f9 69 73 74 2d 3e 6e 53 72 63 20 29 7b 0a 20 20 20 ist->nSrc ){.
201fa 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 pTab = pTabList
201fb 2d 3e 61 5b 69 53 72 63 5d 2e 70 54 61 62 3b 0a ->a[iSrc].pTab;.
201fc 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 53 74 }else if( (pSt
201fd 61 63 6b 20 3d 20 70 50 61 72 73 65 2d 3e 74 72 ack = pParse->tr
201fe 69 67 53 74 61 63 6b 29 21 3d 30 20 29 7b 0a 20 igStack)!=0 ){.
201ff 20 20 20 2f 2a 20 54 68 69 73 20 6d 75 73 74 20 /* This must
20200 62 65 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f be an attempt to
20201 20 72 65 61 64 20 74 68 65 20 4e 45 57 20 6f 72 read the NEW or
20202 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c OLD pseudo-tabl
20203 65 73 0a 20 20 20 20 2a 2a 20 6f 66 20 61 20 74 es. ** of a t
20204 72 69 67 67 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 rigger.. */.
20205 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
20206 2d 3e 69 54 61 62 6c 65 3d 3d 70 53 74 61 63 6b ->iTable==pStack
20207 2d 3e 6e 65 77 49 64 78 20 7c 7c 20 70 45 78 70 ->newIdx || pExp
20208 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 53 74 61 63 r->iTable==pStac
20209 6b 2d 3e 6f 6c 64 49 64 78 20 29 3b 0a 20 20 20 k->oldIdx );.
2020a 20 70 54 61 62 20 3d 20 70 53 74 61 63 6b 2d 3e pTab = pStack->
2020b 70 54 61 62 3b 0a 20 20 7d 0a 20 20 69 66 28 20 pTab;. }. if(
2020c 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e pTab==0 ) return
2020d 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 ;. if( pExpr->i
2020e 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20 Column>=0 ){.
2020f 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
20210 69 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 iColumn<pTab->nC
20211 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d ol );. zCol =
20212 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 pTab->aCol[pExp
20213 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d r->iColumn].zNam
20214 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 e;. }else if( p
20215 54 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b Tab->iPKey>=0 ){
20216 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 . assert( pTa
20217 62 2d 3e 69 50 4b 65 79 3c 70 54 61 62 2d 3e 6e b->iPKey<pTab->n
20218 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 Col );. zCol
20219 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 = pTab->aCol[pTa
2021a 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b b->iPKey].zName;
2021b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43 . }else{. zC
2021c 6f 6c 20 3d 20 22 52 4f 57 49 44 22 3b 0a 20 20 ol = "ROWID";.
2021d 7d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e }. assert( iDb>
2021e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 =0 && iDb<db->nD
2021f 62 20 29 3b 0a 20 20 7a 44 42 61 73 65 20 3d 20 b );. zDBase =
20220 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 db->aDb[iDb].zNa
20221 6d 65 3b 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 me;. rc = db->x
20222 41 75 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72 Auth(db->pAuthAr
20223 67 2c 20 53 51 4c 49 54 45 5f 52 45 41 44 2c 20 g, SQLITE_READ,
20224 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f pTab->zName, zCo
20225 6c 2c 20 7a 44 42 61 73 65 2c 20 0a 20 20 20 20 l, zDBase, .
20226 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 pPa
20227 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 rse->zAuthContex
20228 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 t);. if( rc==SQ
20229 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 LITE_IGNORE ){.
2022a 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
2022b 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 20 K_NULL;. }else
2022c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 if( rc==SQLITE_D
2022d 45 4e 59 20 29 7b 0a 20 20 20 20 69 66 28 20 64 ENY ){. if( d
2022e 62 2d 3e 6e 44 62 3e 32 20 7c 7c 20 69 44 62 21 b->nDb>2 || iDb!
2022f 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
20230 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
20231 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f 20 25 se, "access to %
20232 73 2e 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69 s.%s.%s is prohi
20233 62 69 74 65 64 22 2c 20 0a 20 20 20 20 20 20 20 bited", .
20234 20 20 7a 44 42 61 73 65 2c 20 70 54 61 62 2d 3e zDBase, pTab->
20235 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 zName, zCol);.
20236 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 }else{. s
20237 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
20238 50 61 72 73 65 2c 20 22 61 63 63 65 73 73 20 74 Parse, "access t
20239 6f 20 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69 o %s.%s is prohi
2023a 62 69 74 65 64 22 2c 70 54 61 62 2d 3e 7a 4e 61 bited",pTab->zNa
2023b 6d 65 2c 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a me,zCol);. }.
2023c 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d pParse->rc =
2023d 20 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20 SQLITE_AUTH;.
2023e 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 }else if( rc!=SQ
2023f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 LITE_OK ){. s
20240 71 6c 69 74 65 41 75 74 68 42 61 64 52 65 74 75 qliteAuthBadRetu
20241 72 6e 43 6f 64 65 28 70 50 61 72 73 65 2c 20 72 rnCode(pParse, r
20242 63 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a c);. }.}../*.**
20243 20 44 6f 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 Do an authoriza
20244 74 69 6f 6e 20 63 68 65 63 6b 20 75 73 69 6e 67 tion check using
20245 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20 61 72 the code and ar
20246 67 75 6d 65 6e 74 73 20 67 69 76 65 6e 2e 20 20 guments given.
20247 52 65 74 75 72 6e 0a 2a 2a 20 65 69 74 68 65 72 Return.** either
20248 20 53 51 4c 49 54 45 5f 4f 4b 20 28 7a 65 72 6f SQLITE_OK (zero
20249 29 20 6f 72 20 53 51 4c 49 54 45 5f 49 47 4e 4f ) or SQLITE_IGNO
2024a 52 45 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 4e RE or SQLITE_DEN
2024b 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 44 45 Y. If SQLITE_DE
2024c 4e 59 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 NY.** is returne
2024d 64 2c 20 74 68 65 6e 20 74 68 65 20 65 72 72 6f d, then the erro
2024e 72 20 63 6f 75 6e 74 20 61 6e 64 20 65 72 72 6f r count and erro
2024f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 r message in pPa
20250 72 73 65 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 rse are.** modif
20251 69 65 64 20 61 70 70 72 6f 70 72 69 61 74 65 6c ied appropriatel
20252 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
20253 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
20254 41 75 74 68 43 68 65 63 6b 28 0a 20 20 50 61 72 AuthCheck(. Par
20255 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 69 6e se *pParse,. in
20256 74 20 63 6f 64 65 2c 0a 20 20 63 6f 6e 73 74 20 t code,. const
20257 63 68 61 72 20 2a 7a 41 72 67 31 2c 0a 20 20 63 char *zArg1,. c
20258 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 32 onst char *zArg2
20259 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ,. const char *
2025a 7a 41 72 67 33 0a 29 7b 0a 20 20 73 71 6c 69 74 zArg3.){. sqlit
2025b 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
2025c 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a >db;. int rc;..
2025d 20 20 2f 2a 20 44 6f 6e 27 74 20 64 6f 20 61 6e /* Don't do an
2025e 79 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 y authorization
2025f 63 68 65 63 6b 73 20 69 66 20 74 68 65 20 64 61 checks if the da
20260 74 61 62 61 73 65 20 69 73 20 69 6e 69 74 69 61 tabase is initia
20261 6c 69 73 69 6e 67 0a 20 20 2a 2a 20 6f 72 20 69 lising. ** or i
20262 66 20 74 68 65 20 70 61 72 73 65 72 20 69 73 20 f the parser is
20263 62 65 69 6e 67 20 69 6e 76 6f 6b 65 64 20 66 72 being invoked fr
20264 6f 6d 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 om within sqlite
20265 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 2e 0a 3_declare_vtab..
20266 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 */. if( db->i
20267 6e 69 74 2e 62 75 73 79 20 7c 7c 20 49 4e 5f 44 nit.busy || IN_D
20268 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 ECLARE_VTAB ){.
20269 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
2026a 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 _OK;. }.. if(
2026b 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20 29 7b 0a db->xAuth==0 ){.
2026c 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
2026d 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d E_OK;. }. rc =
2026e 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70 db->xAuth(db->p
2026f 41 75 74 68 41 72 67 2c 20 63 6f 64 65 2c 20 7a AuthArg, code, z
20270 41 72 67 31 2c 20 7a 41 72 67 32 2c 20 7a 41 72 Arg1, zArg2, zAr
20271 67 33 2c 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 g3, pParse->zAut
20272 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28 hContext);. if(
20273 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 rc==SQLITE_DENY
20274 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
20275 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
20276 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22 "not authorized"
20277 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 );. pParse->r
20278 63 20 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 3b c = SQLITE_AUTH;
20279 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21 . }else if( rc!
2027a 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 =SQLITE_OK && rc
2027b 21 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 !=SQLITE_IGNORE
2027c 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
2027d 54 45 5f 44 45 4e 59 3b 0a 20 20 20 20 73 71 6c TE_DENY;. sql
2027e 69 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e iteAuthBadReturn
2027f 43 6f 64 65 28 70 50 61 72 73 65 2c 20 72 63 29 Code(pParse, rc)
20280 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
20281 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73 68 c;.}../*.** Push
20282 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f an authorizatio
20283 6e 20 63 6f 6e 74 65 78 74 2e 20 20 41 66 74 65 n context. Afte
20284 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 r this routine i
20285 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 0a 2a 2a s called, the.**
20286 20 7a 41 72 67 33 20 61 72 67 75 6d 65 6e 74 20 zArg3 argument
20287 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e to authorization
20288 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 callbacks will
20289 62 65 20 7a 43 6f 6e 74 65 78 74 20 75 6e 74 69 be zContext unti
2028a 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e 20 20 4f 72 l.** popped. Or
2028b 20 69 66 20 70 50 61 72 73 65 3d 3d 30 2c 20 74 if pParse==0, t
2028c 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
2028d 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 no-op..*/.SQLIT
2028e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
2028f 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 qlite3AuthContex
20290 74 50 75 73 68 28 0a 20 20 50 61 72 73 65 20 2a tPush(. Parse *
20291 70 50 61 72 73 65 2c 0a 20 20 41 75 74 68 43 6f pParse,. AuthCo
20292 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c ntext *pContext,
20293 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a . const char *
20294 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a 20 20 70 43 zContext.){. pC
20295 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 3d ontext->pParse =
20296 20 70 50 61 72 73 65 3b 0a 20 20 69 66 28 20 70 pParse;. if( p
20297 50 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43 6f Parse ){. pCo
20298 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74 ntext->zAuthCont
20299 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41 ext = pParse->zA
2029a 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 uthContext;.
2029b 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e pParse->zAuthCon
2029c 74 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78 74 3b text = zContext;
2029d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f . }.}../*.** Po
2029e 70 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 p an authorizati
2029f 6f 6e 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20 on context that
202a0 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70 was previously p
202a1 75 73 68 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 ushed.** by sqli
202a2 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 te3AuthContextPu
202a3 73 68 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 sh.*/.SQLITE_PRI
202a4 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
202a5 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 3AuthContextPop(
202a6 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f AuthContext *pCo
202a7 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 70 43 ntext){. if( pC
202a8 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 29 ontext->pParse )
202a9 7b 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e {. pContext->
202aa 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e pParse->zAuthCon
202ab 74 65 78 74 20 3d 20 70 43 6f 6e 74 65 78 74 2d text = pContext-
202ac 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 >zAuthContext;.
202ad 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 pContext->pPa
202ae 72 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a rse = 0;. }.}..
202af 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
202b0 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 _OMIT_AUTHORIZAT
202b1 49 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a ION */../*******
202b2 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 ******* End of a
202b3 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a uth.c **********
202b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202b6 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
202b7 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
202b8 6c 65 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a le build.c *****
202b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202bb 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
202bc 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 01 September 15.
202bd 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
202be 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
202bf 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
202c0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
202c1 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
202c2 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
202c3 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
202c4 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
202c5 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
202c6 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
202c7 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
202c8 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
202c9 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
202ca 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
202cb 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
202cc 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
202cd 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
202ce 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
202cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
202d3 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
202d4 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 ains C code rout
202d5 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61 ines that are ca
202d6 6c 6c 65 64 20 62 79 20 74 68 65 20 53 51 4c 69 lled by the SQLi
202d7 74 65 20 70 61 72 73 65 72 0a 2a 2a 20 77 68 65 te parser.** whe
202d8 6e 20 73 79 6e 74 61 78 20 72 75 6c 65 73 20 61 n syntax rules a
202d9 72 65 20 72 65 64 75 63 65 64 2e 20 20 54 68 65 re reduced. The
202da 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 routines in thi
202db 73 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 74 68 s file handle th
202dc 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6b e.** following k
202dd 69 6e 64 73 20 6f 66 20 53 51 4c 20 73 79 6e 74 inds of SQL synt
202de 61 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 ax:.**.** CR
202df 45 41 54 45 20 54 41 42 4c 45 0a 2a 2a 20 20 20 EATE TABLE.**
202e0 20 20 44 52 4f 50 20 54 41 42 4c 45 0a 2a 2a 20 DROP TABLE.**
202e1 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 CREATE INDEX
202e2 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 49 4e 44 .** DROP IND
202e3 45 58 0a 2a 2a 20 20 20 20 20 63 72 65 61 74 69 EX.** creati
202e4 6e 67 20 49 44 20 6c 69 73 74 73 0a 2a 2a 20 20 ng ID lists.**
202e5 20 20 20 42 45 47 49 4e 20 54 52 41 4e 53 41 43 BEGIN TRANSAC
202e6 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 43 4f 4d 4d TION.** COMM
202e7 49 54 0a 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 IT.** ROLLBA
202e8 43 4b 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 75 CK.**.** $Id: bu
202e9 69 6c 64 2e 63 2c 76 20 31 2e 35 31 31 20 32 30 ild.c,v 1.511 20
202ea 30 38 2f 31 32 2f 33 30 20 30 36 3a 32 34 3a 35 08/12/30 06:24:5
202eb 38 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 8 danielk1977 Ex
202ec 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 p $.*/../*.** Th
202ed 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
202ee 6c 6c 65 64 20 77 68 65 6e 20 61 20 6e 65 77 20 lled when a new
202ef 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 SQL statement is
202f0 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a beginning to.**
202f1 20 62 65 20 70 61 72 73 65 64 2e 20 20 49 6e 69 be parsed. Ini
202f2 74 69 61 6c 69 7a 65 20 74 68 65 20 70 50 61 72 tialize the pPar
202f3 73 65 20 73 74 72 75 63 74 75 72 65 20 61 73 20 se structure as
202f4 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 needed..*/.SQLIT
202f5 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
202f6 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 qlite3BeginParse
202f7 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
202f8 69 6e 74 20 65 78 70 6c 61 69 6e 46 6c 61 67 29 int explainFlag)
202f9 7b 0a 20 20 70 50 61 72 73 65 2d 3e 65 78 70 6c {. pParse->expl
202fa 61 69 6e 20 3d 20 28 75 38 29 65 78 70 6c 61 69 ain = (u8)explai
202fb 6e 46 6c 61 67 3b 0a 20 20 70 50 61 72 73 65 2d nFlag;. pParse-
202fc 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d 0a 0a 23 69 >nVar = 0;.}..#i
202fd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
202fe 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f T_SHARED_CACHE./
202ff 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 4c 6f *.** The TableLo
20300 63 6b 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ck structure is
20301 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 74 68 65 only used by the
20302 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 sqlite3TableLoc
20303 6b 28 29 20 61 6e 64 0a 2a 2a 20 63 6f 64 65 54 k() and.** codeT
20304 61 62 6c 65 4c 6f 63 6b 73 28 29 20 66 75 6e 63 ableLocks() func
20305 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 tions..*/.struct
20306 20 54 61 62 6c 65 4c 6f 63 6b 20 7b 0a 20 20 69 TableLock {. i
20307 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 nt iDb;
20308 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
20309 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ase containing t
2030a 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c he table to be l
2030b 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 ocked */. int i
2030c 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 Tab;
2030d 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 /* The root page
2030e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
2030f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 be locked */.
20310 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 20 u8 isWriteLock;
20311 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 /* True for
20312 20 77 72 69 74 65 20 6c 6f 63 6b 2e 20 20 46 61 write lock. Fa
20313 6c 73 65 20 66 6f 72 20 61 20 72 65 61 64 20 6c lse for a read l
20314 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ock */. const c
20315 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a har *zName; /*
20316 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 Name of the tab
20317 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 le */.};../*.**
20318 52 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 Record the fact
20319 74 68 61 74 20 77 65 20 77 61 6e 74 20 74 6f 20 that we want to
2031a 6c 6f 63 6b 20 61 20 74 61 62 6c 65 20 61 74 20 lock a table at
2031b 72 75 6e 2d 74 69 6d 65 2e 20 20 0a 2a 2a 0a 2a run-time. .**.*
2031c 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 * The table to b
2031d 65 20 6c 6f 63 6b 65 64 20 68 61 73 20 72 6f 6f e locked has roo
2031e 74 20 70 61 67 65 20 69 54 61 62 20 61 6e 64 20 t page iTab and
2031f 69 73 20 66 6f 75 6e 64 20 69 6e 20 64 61 74 61 is found in data
20320 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41 20 72 base iDb..** A r
20321 65 61 64 20 6f 72 20 61 20 77 72 69 74 65 20 6c ead or a write l
20322 6f 63 6b 20 63 61 6e 20 62 65 20 74 61 6b 65 6e ock can be taken
20323 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 69 73 depending on is
20324 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a Writelock..**.**
20325 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 This routine ju
20326 73 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 66 st records the f
20327 61 63 74 20 74 68 61 74 20 74 68 65 20 6c 6f 63 act that the loc
20328 6b 20 69 73 20 64 65 73 69 72 65 64 2e 20 20 54 k is desired. T
20329 68 65 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 6d 61 he.** code to ma
2032a 6b 65 20 74 68 65 20 6c 6f 63 6b 20 6f 63 63 75 ke the lock occu
2032b 72 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 r is generated b
2032c 79 20 61 20 6c 61 74 65 72 20 63 61 6c 6c 20 74 y a later call t
2032d 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f o.** codeTableLo
2032e 63 6b 73 28 29 20 77 68 69 63 68 20 6f 63 63 75 cks() which occu
2032f 72 73 20 64 75 72 69 6e 67 20 73 71 6c 69 74 65 rs during sqlite
20330 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 29 2e 3FinishCoding().
20331 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20332 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 TE void sqlite3T
20333 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 50 61 72 73 ableLock(. Pars
20334 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f e *pParse, /
20335 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
20336 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 t */. int iDb,
20337 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
20338 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ex of the databa
20339 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 se containing th
2033a 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 20 e table to lock
2033b 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20 */. int iTab,
2033c 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 /* Root
2033d 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 page number of t
2033e 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c he table to be l
2033f 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 73 ocked */. u8 is
20340 57 72 69 74 65 4c 6f 63 6b 2c 20 20 20 20 2f 2a WriteLock, /*
20341 20 54 72 75 65 20 66 6f 72 20 61 20 77 72 69 74 True for a writ
20342 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 e lock */. cons
20343 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 20 2f t char *zName /
20344 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 * Name of the ta
20345 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 ble to be locked
20346 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a */.){. int i;.
20347 20 20 69 6e 74 20 6e 42 79 74 65 73 3b 0a 20 20 int nBytes;.
20348 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b 0a 0a 20 TableLock *p;..
20349 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 if( iDb<0 ){.
2034a 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 return;. }..
2034b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 for(i=0; i<pPar
2034c 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 se->nTableLock;
2034d 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 i++){. p = &p
2034e 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 Parse->aTableLoc
2034f 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d k[i];. if( p-
20350 3e 69 44 62 3d 3d 69 44 62 20 26 26 20 70 2d 3e >iDb==iDb && p->
20351 69 54 61 62 3d 3d 69 54 61 62 20 29 7b 0a 20 20 iTab==iTab ){.
20352 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f p->isWriteLo
20353 63 6b 20 3d 20 28 70 2d 3e 69 73 57 72 69 74 65 ck = (p->isWrite
20354 4c 6f 63 6b 20 7c 7c 20 69 73 57 72 69 74 65 4c Lock || isWriteL
20355 6f 63 6b 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ock);. retu
20356 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 rn;. }. }..
20357 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 nBytes = sizeof
20358 28 54 61 62 6c 65 4c 6f 63 6b 29 20 2a 20 28 70 (TableLock) * (p
20359 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 Parse->nTableLoc
2035a 6b 2b 31 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e k+1);. pParse->
2035b 61 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 0a 20 20 aTableLock = .
2035c 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 sqlite3DbRea
2035d 6c 6c 6f 63 4f 72 46 72 65 65 28 70 50 61 72 73 llocOrFree(pPars
2035e 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 e->db, pParse->a
2035f 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e 42 79 74 65 TableLock, nByte
20360 73 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 s);. if( pParse
20361 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 29 7b 0a ->aTableLock ){.
20362 20 20 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d p = &pParse-
20363 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 70 50 61 72 >aTableLock[pPar
20364 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 2b se->nTableLock++
20365 5d 3b 0a 20 20 20 20 70 2d 3e 69 44 62 20 3d 20 ];. p->iDb =
20366 69 44 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 iDb;. p->iTab
20367 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e = iTab;. p->
20368 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 69 73 isWriteLock = is
20369 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 70 WriteLock;. p
2036a 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b ->zName = zName;
2036b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 . }else{. pP
2036c 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b arse->nTableLock
2036d 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 65 = 0;. pParse
2036e 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
2036f 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f ed = 1;. }.}../
20370 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f *.** Code an OP_
20371 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 75 TableLock instru
20372 63 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 ction for each t
20373 61 62 6c 65 20 6c 6f 63 6b 65 64 20 62 79 20 74 able locked by t
20374 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 he.** statement
20375 28 63 6f 6e 66 69 67 75 72 65 64 20 62 79 20 63 (configured by c
20376 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 54 alls to sqlite3T
20377 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a 2a 2f 0a ableLock())..*/.
20378 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 static void code
20379 54 61 62 6c 65 4c 6f 63 6b 73 28 50 61 72 73 65 TableLocks(Parse
2037a 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 *pParse){. int
2037b 20 69 3b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 i;. Vdbe *pVdb
2037c 65 3b 20 0a 0a 20 20 69 66 28 20 30 3d 3d 28 70 e; .. if( 0==(p
2037d 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33 47 65 Vdbe = sqlite3Ge
2037e 74 56 64 62 65 28 70 50 61 72 73 65 29 29 20 29 tVdbe(pParse)) )
2037f 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
20380 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }.. for(i=0; i<
20381 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f pParse->nTableLo
20382 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 54 61 ck; i++){. Ta
20383 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d 20 26 70 50 bleLock *p = &pP
20384 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b arse->aTableLock
20385 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 70 31 20 [i];. int p1
20386 3d 20 70 2d 3e 69 44 62 3b 0a 20 20 20 20 73 71 = p->iDb;. sq
20387 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
20388 70 56 64 62 65 2c 20 4f 50 5f 54 61 62 6c 65 4c pVdbe, OP_TableL
20389 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e 69 54 61 62 ock, p1, p->iTab
2038a 2c 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b , p->isWriteLock
2038b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
2038c 20 20 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 p->zName
2038d 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 , P4_STATIC);.
2038e 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 }.}.#else. #def
2038f 69 6e 65 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 ine codeTableLoc
20390 6b 73 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a ks(x).#endif../*
20391 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
20392 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 is called after
20393 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 a single SQL st
20394 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 65 6e atement has been
20395 0a 2a 2a 20 70 61 72 73 65 64 20 61 6e 64 20 61 .** parsed and a
20396 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 74 6f VDBE program to
20397 20 65 78 65 63 75 74 65 20 74 68 61 74 20 73 74 execute that st
20398 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 65 6e atement has been
20399 0a 2a 2a 20 70 72 65 70 61 72 65 64 2e 20 20 54 .** prepared. T
2039a 68 69 73 20 72 6f 75 74 69 6e 65 20 70 75 74 73 his routine puts
2039b 20 74 68 65 20 66 69 6e 69 73 68 69 6e 67 20 74 the finishing t
2039c 6f 75 63 68 65 73 20 6f 6e 20 74 68 65 0a 2a 2a ouches on the.**
2039d 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61 6e VDBE program an
2039e 64 20 72 65 73 65 74 73 20 74 68 65 20 70 50 61 d resets the pPa
2039f 72 73 65 20 73 74 72 75 63 74 75 72 65 20 66 6f rse structure fo
203a0 72 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 70 61 r the next.** pa
203a1 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 rse..**.** Note
203a2 74 68 61 74 20 69 66 20 61 6e 20 65 72 72 6f 72 that if an error
203a3 20 6f 63 63 75 72 72 65 64 2c 20 69 74 20 6d 69 occurred, it mi
203a4 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65 20 ght be the case
203a5 74 68 61 74 0a 2a 2a 20 6e 6f 20 56 44 42 45 20 that.** no VDBE
203a6 63 6f 64 65 20 77 61 73 20 67 65 6e 65 72 61 74 code was generat
203a7 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
203a8 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
203a9 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 50 e3FinishCoding(P
203aa 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 arse *pParse){.
203ab 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 sqlite3 *db;.
203ac 56 64 62 65 20 2a 76 3b 0a 0a 20 20 64 62 20 3d Vdbe *v;.. db =
203ad 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 pParse->db;. i
203ae 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
203af 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 led ) return;.
203b0 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 if( pParse->nest
203b1 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 ed ) return;. i
203b2 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 f( pParse->nErr
203b3 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 ) return;.. /*
203b4 42 65 67 69 6e 20 62 79 20 67 65 6e 65 72 61 74 Begin by generat
203b5 69 6e 67 20 73 6f 6d 65 20 74 65 72 6d 69 6e 61 ing some termina
203b6 74 69 6f 6e 20 63 6f 64 65 20 61 74 20 74 68 65 tion code at the
203b7 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a end of the. **
203b8 20 76 64 62 65 20 70 72 6f 67 72 61 6d 0a 20 20 vdbe program.
203b9 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 */. v = sqlite3
203ba 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
203bb 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 . if( v ){.
203bc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
203bd 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a 0a 0(v, OP_Halt);..
203be 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6f 6b 69 /* The cooki
203bf 65 20 6d 61 73 6b 20 63 6f 6e 74 61 69 6e 73 20 e mask contains
203c0 6f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68 one bit for each
203c1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f database file o
203c2 70 65 6e 2e 0a 20 20 20 20 2a 2a 20 28 42 69 74 pen.. ** (Bit
203c3 20 30 20 69 73 20 66 6f 72 20 6d 61 69 6e 2c 20 0 is for main,
203c4 62 69 74 20 31 20 69 73 20 66 6f 72 20 74 65 6d bit 1 is for tem
203c5 70 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e p, and so forth.
203c6 29 20 20 42 69 74 73 20 61 72 65 0a 20 20 20 20 ) Bits are.
203c7 2a 2a 20 73 65 74 20 66 6f 72 20 65 61 63 68 20 ** set for each
203c8 64 61 74 61 62 61 73 65 20 74 68 61 74 20 69 73 database that is
203c9 20 75 73 65 64 2e 20 20 47 65 6e 65 72 61 74 65 used. Generate
203ca 20 63 6f 64 65 20 74 6f 20 73 74 61 72 74 20 61 code to start a
203cb 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 . ** transact
203cc 69 6f 6e 20 6f 6e 20 65 61 63 68 20 75 73 65 64 ion on each used
203cd 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 6f database and to
203ce 20 76 65 72 69 66 79 20 74 68 65 20 73 63 68 65 verify the sche
203cf 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 20 20 2a 2a ma cookie. **
203d0 20 6f 6e 20 65 61 63 68 20 75 73 65 64 20 64 61 on each used da
203d1 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 tabase.. */.
203d2 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63 if( pParse->c
203d3 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20 29 7b 0a 20 ookieGoto>0 ){.
203d4 20 20 20 20 20 75 33 32 20 6d 61 73 6b 3b 0a 20 u32 mask;.
203d5 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 int iDb;.
203d6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
203d7 75 6d 70 48 65 72 65 28 76 2c 20 70 50 61 72 73 umpHere(v, pPars
203d8 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 2d 31 29 e->cookieGoto-1)
203d9 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d ;. for(iDb=
203da 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 44 62 3c 64 0, mask=1; iDb<d
203db 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b 3c 3c 3d 31 b->nDb; mask<<=1
203dc 2c 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20 20 20 , iDb++){.
203dd 20 20 69 66 28 20 28 6d 61 73 6b 20 26 20 70 50 if( (mask & pP
203de 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b arse->cookieMask
203df 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b )==0 ) continue;
203e0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
203e1 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c VdbeUsesBtree(v,
203e2 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 73 iDb);. s
203e3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
203e4 28 76 2c 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f (v,OP_Transactio
203e5 6e 2c 20 69 44 62 2c 20 28 6d 61 73 6b 20 26 20 n, iDb, (mask &
203e6 70 50 61 72 73 65 2d 3e 77 72 69 74 65 4d 61 73 pParse->writeMas
203e7 6b 29 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 k)!=0);.
203e8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
203e9 32 28 76 2c 4f 50 5f 56 65 72 69 66 79 43 6f 6f 2(v,OP_VerifyCoo
203ea 6b 69 65 2c 20 69 44 62 2c 20 70 50 61 72 73 65 kie, iDb, pParse
203eb 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44 ->cookieValue[iD
203ec 62 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 b]);. }.#if
203ed 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
203ee 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
203ef 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e {. in
203f0 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 t i;. for
203f1 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e (i=0; i<pParse->
203f2 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b nVtabLock; i++){
203f3 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 . char
203f4 2a 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a 29 *vtab = (char *)
203f5 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f pParse->apVtabLo
203f6 63 6b 5b 69 5d 2d 3e 70 56 74 61 62 3b 0a 20 20 ck[i]->pVtab;.
203f7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
203f8 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
203f9 56 42 65 67 69 6e 2c 20 30 2c 20 30 2c 20 30 2c VBegin, 0, 0, 0,
203fa 20 76 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b vtab, P4_VTAB);
203fb 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
203fc 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 pParse->nVtab
203fd 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 Lock = 0;.
203fe 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 }.#endif..
203ff 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 /* Once all the
20400 63 6f 6f 6b 69 65 73 20 68 61 76 65 20 62 65 65 cookies have bee
20401 6e 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 74 n verified and t
20402 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e ransactions open
20403 65 64 2c 20 0a 20 20 20 20 20 20 2a 2a 20 6f 62 ed, . ** ob
20404 74 61 69 6e 20 74 68 65 20 72 65 71 75 69 72 65 tain the require
20405 64 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 54 d table-locks. T
20406 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75 his is a no-op u
20407 6e 6c 65 73 73 20 74 68 65 20 0a 20 20 20 20 20 nless the .
20408 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 ** shared-cache
20409 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 feature is enab
2040a 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 led.. */.
2040b 20 20 20 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 codeTableLoc
2040c 6b 73 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 ks(pParse);.
2040d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2040e 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 Op2(v, OP_Goto,
2040f 30 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 0, pParse->cooki
20410 65 47 6f 74 6f 29 3b 0a 20 20 20 20 7d 0a 0a 23 eGoto);. }..#
20411 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20412 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 IT_TRACE. if(
20413 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 !db->init.busy
20414 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 68 61 6e ){. /* Chan
20415 67 65 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 ge the P4 argume
20416 6e 74 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 nt of the first
20417 6f 70 63 6f 64 65 20 28 77 68 69 63 68 20 77 69 opcode (which wi
20418 6c 6c 20 61 6c 77 61 79 73 20 62 65 0a 20 20 20 ll always be.
20419 20 20 20 2a 2a 20 61 6e 20 4f 50 5f 54 72 61 63 ** an OP_Trac
2041a 65 29 20 74 6f 20 62 65 20 74 68 65 20 63 6f 6d e) to be the com
2041b 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 plete text of th
2041c 65 20 63 75 72 72 65 6e 74 20 53 51 4c 20 73 74 e current SQL st
2041d 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 2a atement.. *
2041e 2f 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a /. VdbeOp *
2041f 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 pOp = sqlite3Vdb
20420 65 47 65 74 4f 70 28 76 2c 20 30 29 3b 0a 20 20 eGetOp(v, 0);.
20421 20 20 20 20 69 66 28 20 70 4f 70 20 26 26 20 70 if( pOp && p
20422 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 Op->opcode==OP_T
20423 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 race ){.
20424 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
20425 65 50 34 28 76 2c 20 30 2c 20 70 50 61 72 73 65 eP4(v, 0, pParse
20426 2d 3e 7a 53 71 6c 2c 0a 20 20 20 20 20 20 20 20 ->zSql,.
20427 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20428 20 20 20 20 28 69 6e 74 29 28 70 50 61 72 73 65 (int)(pParse
20429 2d 3e 7a 54 61 69 6c 20 2d 20 70 50 61 72 73 65 ->zTail - pParse
2042a 2d 3e 7a 53 71 6c 29 29 3b 0a 20 20 20 20 20 20 ->zSql));.
2042b 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f }. }.#endif /
2042c 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 * SQLITE_OMIT_TR
2042d 41 43 45 20 2a 2f 0a 20 20 7d 0a 0a 0a 20 20 2f ACE */. }... /
2042e 2a 20 47 65 74 20 74 68 65 20 56 44 42 45 20 70 * Get the VDBE p
2042f 72 6f 67 72 61 6d 20 72 65 61 64 79 20 66 6f 72 rogram ready for
20430 20 65 78 65 63 75 74 69 6f 6e 0a 20 20 2a 2f 0a execution. */.
20431 20 20 69 66 28 20 76 20 26 26 20 70 50 61 72 73 if( v && pPars
20432 65 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 20 21 64 e->nErr==0 && !d
20433 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
20434 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ){.#ifdef SQLITE
20435 5f 44 45 42 55 47 0a 20 20 20 20 46 49 4c 45 20 _DEBUG. FILE
20436 2a 74 72 61 63 65 20 3d 20 28 64 62 2d 3e 66 6c *trace = (db->fl
20437 61 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62 ags & SQLITE_Vdb
20438 65 54 72 61 63 65 29 21 3d 30 20 3f 20 73 74 64 eTrace)!=0 ? std
20439 6f 75 74 20 3a 20 30 3b 0a 20 20 20 20 73 71 6c out : 0;. sql
2043a 69 74 65 33 56 64 62 65 54 72 61 63 65 28 76 2c ite3VdbeTrace(v,
2043b 20 74 72 61 63 65 29 3b 0a 23 65 6e 64 69 66 0a trace);.#endif.
2043c 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 assert( pPar
2043d 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 se->disableColCa
2043e 63 68 65 3d 3d 30 20 29 3b 20 20 2f 2a 20 44 69 che==0 ); /* Di
2043f 73 61 62 6c 65 73 20 61 6e 64 20 72 65 2d 65 6e sables and re-en
20440 61 62 6c 65 73 20 6d 61 74 63 68 20 2a 2f 0a 20 ables match */.
20441 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 sqlite3VdbeMa
20442 6b 65 52 65 61 64 79 28 76 2c 20 70 50 61 72 73 keReady(v, pPars
20443 65 2d 3e 6e 56 61 72 2c 20 70 50 61 72 73 65 2d e->nVar, pParse-
20444 3e 6e 4d 65 6d 2b 33 2c 0a 20 20 20 20 20 20 20 >nMem+3,.
20445 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20446 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 33 pParse->nTab+3
20447 2c 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 , pParse->explai
20448 6e 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e n);. pParse->
20449 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 rc = SQLITE_DONE
2044a 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f ;. pParse->co
2044b 6c 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b 0a 20 lNamesSet = 0;.
2044c 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 73 }else if( pPars
2044d 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b e->rc==SQLITE_OK
2044e 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e ){. pParse->
2044f 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
20450 52 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d R;. }. pParse-
20451 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70 50 61 >nTab = 0;. pPa
20452 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 20 rse->nMem = 0;.
20453 20 70 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d 20 pParse->nSet =
20454 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 0;. pParse->nVa
20455 72 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d r = 0;. pParse-
20456 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20 30 3b >cookieMask = 0;
20457 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 . pParse->cooki
20458 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a eGoto = 0;.}../*
20459 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 .** Run the pars
2045a 65 72 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 er and code gene
2045b 72 61 74 6f 72 20 72 65 63 75 72 73 69 76 65 6c rator recursivel
2045c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 y in order to ge
2045d 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 nerate.** code f
2045e 6f 72 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 or the SQL state
2045f 6d 65 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f 20 ment given onto
20460 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 the end of the p
20461 50 61 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a 2a Parse context.**
20462 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 currently under
20463 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 construction.
20464 57 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 When the parser
20465 69 73 20 72 75 6e 20 72 65 63 75 72 73 69 76 65 is run recursive
20466 6c 79 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c 20 ly.** this way,
20467 74 68 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c the final OP_Hal
20468 74 20 69 73 20 6e 6f 74 20 61 70 70 65 6e 64 65 t is not appende
20469 64 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 69 74 d and other init
2046a 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61 6e ialization.** an
2046b 64 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 73 d finalization s
2046c 74 65 70 73 20 61 72 65 20 6f 6d 69 74 74 65 64 teps are omitted
2046d 20 62 65 63 61 75 73 65 20 74 68 6f 73 65 20 61 because those a
2046e 72 65 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74 re handling by t
2046f 68 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74 20 he.** outermost
20470 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f parser..**.** No
20471 74 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 t everything is
20472 6e 65 73 74 61 62 6c 65 2e 20 20 54 68 69 73 20 nestable. This
20473 66 61 63 69 6c 69 74 79 20 69 73 20 64 65 73 69 facility is desi
20474 67 6e 65 64 20 74 6f 20 70 65 72 6d 69 74 0a 2a gned to permit.*
20475 2a 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 * INSERT, UPDATE
20476 2c 20 61 6e 64 20 44 45 4c 45 54 45 20 6f 70 65 , and DELETE ope
20477 72 61 74 69 6f 6e 73 20 61 67 61 69 6e 73 74 20 rations against
20478 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20 20 SQLITE_MASTER.
20479 55 73 65 0a 2a 2a 20 63 61 72 65 20 69 66 20 79 Use.** care if y
2047a 6f 75 20 64 65 63 69 64 65 20 74 6f 20 74 72 79 ou decide to try
2047b 20 74 6f 20 75 73 65 20 74 68 69 73 20 72 6f 75 to use this rou
2047c 74 69 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 tine for some ot
2047d 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2f her purposes..*/
2047e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
2047f 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 void sqlite3Nest
20480 65 64 50 61 72 73 65 28 50 61 72 73 65 20 2a 70 edParse(Parse *p
20481 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 Parse, const cha
20482 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 r *zFormat, ...)
20483 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a {. va_list ap;.
20484 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 char *zSql;.
20485 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 char *zErrMsg =
20486 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 0;. sqlite3 *db
20487 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 = pParse->db;.#
20488 20 64 65 66 69 6e 65 20 53 41 56 45 5f 53 5a 20 define SAVE_SZ
20489 20 28 73 69 7a 65 6f 66 28 50 61 72 73 65 29 20 (sizeof(Parse)
2048a 2d 20 6f 66 66 73 65 74 6f 66 28 50 61 72 73 65 - offsetof(Parse
2048b 2c 6e 56 61 72 29 29 0a 20 20 63 68 61 72 20 73 ,nVar)). char s
2048c 61 76 65 42 75 66 5b 53 41 56 45 5f 53 5a 5d 3b aveBuf[SAVE_SZ];
2048d 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e .. if( pParse->
2048e 6e 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 nErr ) return;.
2048f 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d assert( pParse-
20490 3e 6e 65 73 74 65 64 3c 31 30 20 29 3b 20 20 2f >nested<10 ); /
20491 2a 20 4e 65 73 74 69 6e 67 20 73 68 6f 75 6c 64 * Nesting should
20492 20 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69 6d 69 only be of limi
20493 74 65 64 20 64 65 70 74 68 20 2a 2f 0a 20 20 76 ted depth */. v
20494 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 a_start(ap, zFor
20495 6d 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 mat);. zSql = s
20496 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 qlite3VMPrintf(d
20497 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b b, zFormat, ap);
20498 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 . va_end(ap);.
20499 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a if( zSql==0 ){.
2049a 20 20 20 20 72 65 74 75 72 6e 3b 20 20 20 2f 2a return; /*
2049b 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 A malloc must h
2049c 61 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 ave failed */.
2049d 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 }. pParse->nest
2049e 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28 73 ed++;. memcpy(s
2049f 61 76 65 42 75 66 2c 20 26 70 50 61 72 73 65 2d aveBuf, &pParse-
204a0 3e 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a 29 3b >nVar, SAVE_SZ);
204a1 0a 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73 . memset(&pPars
204a2 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41 56 45 e->nVar, 0, SAVE
204a3 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 _SZ);. sqlite3R
204a4 75 6e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c unParser(pParse,
204a5 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 zSql, &zErrMsg)
204a6 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
204a7 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a e(db, zErrMsg);.
204a8 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
204a9 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d db, zSql);. mem
204aa 63 70 79 28 26 70 50 61 72 73 65 2d 3e 6e 56 61 cpy(&pParse->nVa
204ab 72 2c 20 73 61 76 65 42 75 66 2c 20 53 41 56 45 r, saveBuf, SAVE
204ac 5f 53 5a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e _SZ);. pParse->
204ad 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a nested--;.}../*.
204ae 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e ** Locate the in
204af 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 -memory structur
204b0 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 e that describes
204b1 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 a particular da
204b2 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 tabase.** table
204b3 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f given the name o
204b4 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 f that table and
204b5 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68 (optionally) th
204b6 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a e name of the.**
204b7 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 database contai
204b8 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 ning the table.
204b9 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 Return NULL if
204ba 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a not found..**.**
204bb 20 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 If zDatabase is
204bc 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0, all database
204bd 73 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 s are searched f
204be 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 or the table and
204bf 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6d 61 the.** first ma
204c0 74 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73 20 tching table is
204c1 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 returned. (No c
204c2 68 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c hecking for dupl
204c3 69 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 6e icate table.** n
204c4 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 ames is done.)
204c5 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 The search order
204c6 20 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c 20 is TEMP first,
204c7 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 then MAIN, then
204c8 61 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 any.** auxiliary
204c9 20 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 databases added
204ca 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 using the ATTAC
204cb 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a H command..**.**
204cc 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 See also sqlite
204cd 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 0a 3LocateTable()..
204ce 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
204cf 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 E Table *sqlite3
204d0 46 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 FindTable(sqlite
204d1 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 3 *db, const cha
204d2 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 r *zName, const
204d3 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 29 char *zDatabase)
204d4 7b 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 30 {. Table *p = 0
204d5 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 ;. int i;. int
204d6 20 6e 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 nName;. assert
204d7 28 20 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 ( zName!=0 );.
204d8 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 nName = sqlite3S
204d9 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 6d 65 29 trlen(db, zName)
204da 20 2b 20 31 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d + 1;. for(i=OM
204db 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d IT_TEMPDB; i<db-
204dc 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nDb; i++){.
204dd 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 int j = (i<2) ?
204de 69 5e 31 20 3a 20 69 3b 20 20 20 2f 2a 20 53 65 i^1 : i; /* Se
204df 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 arch TEMP before
204e0 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 MAIN */. if(
204e1 20 7a 44 61 74 61 62 61 73 65 21 3d 30 20 26 26 zDatabase!=0 &&
204e2 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
204e3 7a 44 61 74 61 62 61 73 65 2c 20 64 62 2d 3e 61 zDatabase, db->a
204e4 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 Db[j].zName) ) c
204e5 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 20 3d ontinue;. p =
204e6 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 sqlite3HashFind
204e7 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 (&db->aDb[j].pSc
204e8 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a hema->tblHash, z
204e9 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 Name, nName);.
204ea 20 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b if( p ) break;
204eb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
204ec 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 .}../*.** Locate
204ed 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 the in-memory s
204ee 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 tructure that de
204ef 73 63 72 69 62 65 73 20 61 20 70 61 72 74 69 63 scribes a partic
204f0 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a ular database.**
204f1 20 74 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65 table given the
204f2 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61 name of that ta
204f3 62 6c 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 ble and (optiona
204f4 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 lly) the name of
204f5 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
204f6 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
204f7 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e table. Return N
204f8 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 ULL if not found
204f9 2e 20 20 41 6c 73 6f 20 6c 65 61 76 65 20 61 6e . Also leave an
204fa 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 .** error messag
204fb 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 e in pParse->zEr
204fc 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rMsg..**.** The
204fd 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 difference betwe
204fe 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
204ff 61 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64 54 and sqlite3FindT
20500 61 62 6c 65 28 29 20 69 73 20 74 68 61 74 20 74 able() is that t
20501 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6c his.** routine l
20502 65 61 76 65 73 20 61 6e 20 65 72 72 6f 72 20 6d eaves an error m
20503 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 essage in pParse
20504 2d 3e 7a 45 72 72 4d 73 67 20 77 68 65 72 65 0a ->zErrMsg where.
20505 2a 2a 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 ** sqlite3FindTa
20506 62 6c 65 28 29 20 64 6f 65 73 20 6e 6f 74 2e 0a ble() does not..
20507 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20508 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 E Table *sqlite3
20509 4c 6f 63 61 74 65 54 61 62 6c 65 28 0a 20 20 50 LocateTable(. P
2050a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
2050b 20 20 20 20 20 20 2f 2a 20 63 6f 6e 74 65 78 74 /* context
2050c 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 70 in which to rep
2050d 6f 72 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 ort errors */.
2050e 69 6e 74 20 69 73 56 69 65 77 2c 20 20 20 20 20 int isView,
2050f 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
20510 66 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 f looking for a
20511 56 49 45 57 20 72 61 74 68 65 72 20 74 68 61 6e VIEW rather than
20512 20 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 6f a TABLE */. co
20513 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
20514 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
20515 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 the table we are
20516 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 2a 2f 0a looking for */.
20517 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
20518 62 61 73 65 20 20 20 20 20 2f 2a 20 4e 61 6d 65 base /* Name
20519 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
2051a 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c . Might be NULL
2051b 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a */.){. Table *
2051c 70 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 p;.. /* Read th
2051d 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
2051e 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f a. If an error o
2051f 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 ccurs, leave an
20520 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 error message.
20521 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 ** and code in p
20522 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e Parse and return
20523 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 NULL. */. if(
20524 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 SQLITE_OK!=sqlit
20525 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 e3ReadSchema(pPa
20526 72 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 rse) ){. retu
20527 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 20 3d rn 0;. }.. p =
20528 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c sqlite3FindTabl
20529 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e e(pParse->db, zN
2052a 61 6d 65 2c 20 7a 44 62 61 73 65 29 3b 0a 20 20 ame, zDbase);.
2052b 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 if( p==0 ){.
2052c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 73 67 const char *zMsg
2052d 20 3d 20 69 73 56 69 65 77 20 3f 20 22 6e 6f 20 = isView ? "no
2052e 73 75 63 68 20 76 69 65 77 22 20 3a 20 22 6e 6f such view" : "no
2052f 20 73 75 63 68 20 74 61 62 6c 65 22 3b 0a 20 20 such table";.
20530 20 20 69 66 28 20 7a 44 62 61 73 65 20 29 7b 0a if( zDbase ){.
20531 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
20532 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 orMsg(pParse, "%
20533 73 3a 20 25 73 2e 25 73 22 2c 20 7a 4d 73 67 2c s: %s.%s", zMsg,
20534 20 7a 44 62 61 73 65 2c 20 7a 4e 61 6d 65 29 3b zDbase, zName);
20535 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
20536 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
20537 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 g(pParse, "%s: %
20538 73 22 2c 20 7a 4d 73 67 2c 20 7a 4e 61 6d 65 29 s", zMsg, zName)
20539 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 ;. }. pPar
2053a 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 se->checkSchema
2053b 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 1;. }. retur
2053c 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f n p;.}../*.** Lo
2053d 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f cate the in-memo
2053e 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 ry structure tha
2053f 74 20 64 65 73 63 72 69 62 65 73 20 0a 2a 2a 20 t describes .**
20540 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 a particular ind
20541 65 78 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d ex given the nam
20542 65 20 6f 66 20 74 68 61 74 20 69 6e 64 65 78 0a e of that index.
20543 2a 2a 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 20 ** and the name
20544 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
20545 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
20546 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 52 65 74 75 e index..** Retu
20547 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 rn NULL if not f
20548 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a ound..**.** If z
20549 44 61 74 61 62 61 73 65 20 69 73 20 30 2c 20 61 Database is 0, a
2054a 6c 6c 20 64 61 74 61 62 61 73 65 73 20 61 72 65 ll databases are
2054b 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 searched for th
2054c 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 74 e.** table and t
2054d 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e he first matchin
2054e 67 20 69 6e 64 65 78 20 69 73 20 72 65 74 75 72 g index is retur
2054f 6e 65 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 ned. (No checki
20550 6e 67 0a 2a 2a 20 66 6f 72 20 64 75 70 6c 69 63 ng.** for duplic
20551 61 74 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 ate index names
20552 69 73 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 is done.) The s
20553 65 61 72 63 68 20 6f 72 64 65 72 20 69 73 0a 2a earch order is.*
20554 2a 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74 68 * TEMP first, th
20555 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61 6e en MAIN, then an
20556 79 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 y auxiliary data
20557 62 61 73 65 73 20 61 64 64 65 64 0a 2a 2a 20 75 bases added.** u
20558 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 48 20 sing the ATTACH
20559 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 command..*/.SQLI
2055a 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 TE_PRIVATE Index
2055b 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 *sqlite3FindInd
2055c 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ex(sqlite3 *db,
2055d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
2055e 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
2055f 44 62 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 20 Db){. Index *p
20560 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 = 0;. int i;.
20561 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 int nName = sqli
20562 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e te3Strlen(db, zN
20563 61 6d 65 29 2b 31 3b 0a 20 20 66 6f 72 28 69 3d ame)+1;. for(i=
20564 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 OMIT_TEMPDB; i<d
20565 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
20566 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 int j = (i<2)
20567 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53 ? i^1 : i; /* S
20568 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 earch TEMP befor
20569 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 53 63 e MAIN */. Sc
2056a 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 hema *pSchema =
2056b 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 db->aDb[j].pSche
2056c 6d 61 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 ma;. if( zDb
2056d 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d && sqlite3StrICm
2056e 70 28 7a 44 62 2c 20 64 62 2d 3e 61 44 62 5b 6a p(zDb, db->aDb[j
2056f 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 ].zName) ) conti
20570 6e 75 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 nue;. assert(
20571 20 70 53 63 68 65 6d 61 20 7c 7c 20 28 6a 3d 3d pSchema || (j==
20572 31 20 26 26 20 21 64 62 2d 3e 61 44 62 5b 31 5d 1 && !db->aDb[1]
20573 2e 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28 .pBt) );. if(
20574 20 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 pSchema ){.
20575 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 p = sqlite3Has
20576 68 46 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e hFind(&pSchema->
20577 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 idxHash, zName,
20578 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 nName);. }.
20579 20 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b if( p ) break;
2057a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
2057b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69 .}../*.** Reclai
2057c 6d 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65 m the memory use
2057d 64 20 62 79 20 61 6e 20 69 6e 64 65 78 0a 2a 2f d by an index.*/
2057e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 .static void fre
2057f 65 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29 eIndex(Index *p)
20580 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
20581 3d 20 70 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 3b = p->pTable->db;
20582 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
20583 28 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 29 (db, p->zColAff)
20584 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
20585 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a e(db, p);.}../*.
20586 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 67 69 ** Remove the gi
20587 76 65 6e 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 ven index from t
20588 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 he index hash ta
20589 62 6c 65 2c 20 61 6e 64 20 66 72 65 65 0a 2a 2a ble, and free.**
2058a 20 69 74 73 20 6d 65 6d 6f 72 79 20 73 74 72 75 its memory stru
2058b 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ctures..**.** Th
2058c 65 20 69 6e 64 65 78 20 69 73 20 72 65 6d 6f 76 e index is remov
2058d 65 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ed from the data
2058e 62 61 73 65 20 68 61 73 68 20 74 61 62 6c 65 73 base hash tables
2058f 20 62 75 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f but.** it is no
20590 74 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 t unlinked from
20591 74 68 65 20 54 61 62 6c 65 20 74 68 61 74 20 69 the Table that i
20592 74 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 55 6e t indexes..** Un
20593 6c 69 6e 6b 69 6e 67 20 66 72 6f 6d 20 74 68 65 linking from the
20594 20 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 64 Table must be d
20595 6f 6e 65 20 62 79 20 74 68 65 20 63 61 6c 6c 69 one by the calli
20596 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a ng function..*/.
20597 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
20598 74 65 44 65 6c 65 74 65 49 6e 64 65 78 28 49 6e teDeleteIndex(In
20599 64 65 78 20 2a 70 29 7b 0a 20 20 49 6e 64 65 78 dex *p){. Index
2059a 20 2a 70 4f 6c 64 3b 0a 20 20 63 6f 6e 73 74 20 *pOld;. const
2059b 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 2d char *zName = p-
2059c 3e 7a 4e 61 6d 65 3b 0a 0a 20 20 70 4f 6c 64 20 >zName;.. pOld
2059d 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 = sqlite3HashIns
2059e 65 72 74 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d ert(&p->pSchema-
2059f 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c >idxHash, zName,
205a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
205a1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
205a2 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d te3Strlen30(zNam
205a3 65 29 2b 31 2c 20 30 29 3b 0a 20 20 61 73 73 65 e)+1, 0);. asse
205a4 72 74 28 20 70 4f 6c 64 3d 3d 30 20 7c 7c 20 70 rt( pOld==0 || p
205a5 4f 6c 64 3d 3d 70 20 29 3b 0a 20 20 66 72 65 65 Old==p );. free
205a6 49 6e 64 65 78 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a Index(p);.}../*.
205a7 2a 2a 20 46 6f 72 20 74 68 65 20 69 6e 64 65 78 ** For the index
205a8 20 63 61 6c 6c 65 64 20 7a 49 64 78 4e 61 6d 65 called zIdxName
205a9 20 77 68 69 63 68 20 69 73 20 66 6f 75 6e 64 20 which is found
205aa 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
205ab 69 44 62 2c 0a 2a 2a 20 75 6e 6c 69 6b 65 20 74 iDb,.** unlike t
205ac 68 61 74 20 69 6e 64 65 78 20 66 72 6f 6d 20 69 hat index from i
205ad 74 73 20 54 61 62 6c 65 20 74 68 65 6e 20 72 65 ts Table then re
205ae 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 66 move the index f
205af 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 rom.** the index
205b0 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20 hash table and
205b1 66 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 free all memory
205b2 73 74 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 structures assoc
205b3 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 iated.** with th
205b4 65 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 e index..*/.SQLI
205b5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
205b6 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 sqlite3UnlinkAnd
205b7 44 65 6c 65 74 65 49 6e 64 65 78 28 73 71 6c 69 DeleteIndex(sqli
205b8 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 te3 *db, int iDb
205b9 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 , const char *zI
205ba 64 78 4e 61 6d 65 29 7b 0a 20 20 49 6e 64 65 78 dxName){. Index
205bb 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20 *pIndex;. int
205bc 6c 65 6e 3b 0a 20 20 48 61 73 68 20 2a 70 48 61 len;. Hash *pHa
205bd 73 68 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 sh = &db->aDb[iD
205be 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 b].pSchema->idxH
205bf 61 73 68 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73 71 ash;.. len = sq
205c0 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 lite3Strlen(db,
205c1 7a 49 64 78 4e 61 6d 65 29 3b 0a 20 20 70 49 6e zIdxName);. pIn
205c2 64 65 78 20 3d 20 73 71 6c 69 74 65 33 48 61 73 dex = sqlite3Has
205c3 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a hInsert(pHash, z
205c4 49 64 78 4e 61 6d 65 2c 20 6c 65 6e 2b 31 2c 20 IdxName, len+1,
205c5 30 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 0);. if( pIndex
205c6 20 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e 64 ){. if( pInd
205c7 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 ex->pTable->pInd
205c8 65 78 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 ex==pIndex ){.
205c9 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 pIndex->pTab
205ca 6c 65 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e le->pIndex = pIn
205cb 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 dex->pNext;.
205cc 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 }else{. Ind
205cd 65 78 20 2a 70 3b 0a 20 20 20 20 20 20 66 6f 72 ex *p;. for
205ce 28 70 3d 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c (p=pIndex->pTabl
205cf 65 2d 3e 70 49 6e 64 65 78 3b 20 70 20 26 26 20 e->pIndex; p &&
205d0 70 2d 3e 70 4e 65 78 74 21 3d 70 49 6e 64 65 78 p->pNext!=pIndex
205d1 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 7d 0a ; p=p->pNext){}.
205d2 20 20 20 20 20 20 69 66 28 20 70 20 26 26 20 70 if( p && p
205d3 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 64 65 78 20 ->pNext==pIndex
205d4 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 4e ){. p->pN
205d5 65 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e ext = pIndex->pN
205d6 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ext;. }.
205d7 20 7d 0a 20 20 20 20 66 72 65 65 49 6e 64 65 78 }. freeIndex
205d8 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20 (pIndex);. }.
205d9 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c db->flags |= SQL
205da 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 ITE_InternChange
205db 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 s;.}../*.** Eras
205dc 65 20 61 6c 6c 20 73 63 68 65 6d 61 20 69 6e 66 e all schema inf
205dd 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 ormation from th
205de 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68 e in-memory hash
205df 20 74 61 62 6c 65 73 20 6f 66 0a 2a 2a 20 61 20 tables of.** a
205e0 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 2e single database.
205e1 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 This routine i
205e2 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 63 6c s called to recl
205e3 61 69 6d 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 aim memory.** be
205e4 66 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 fore the databas
205e5 65 20 63 6c 6f 73 65 73 2e 20 20 49 74 20 69 73 e closes. It is
205e6 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20 64 75 72 also called dur
205e7 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a ing a rollback.*
205e8 2a 20 69 66 20 74 68 65 72 65 20 77 65 72 65 20 * if there were
205e9 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 64 schema changes d
205ea 75 72 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 uring the transa
205eb 63 74 69 6f 6e 20 6f 72 20 69 66 20 61 0a 2a 2a ction or if a.**
205ec 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 6d schema-cookie m
205ed 69 73 6d 61 74 63 68 20 6f 63 63 75 72 73 2e 0a ismatch occurs..
205ee 2a 2a 0a 2a 2a 20 49 66 20 69 44 62 3c 3d 30 20 **.** If iDb<=0
205ef 74 68 65 6e 20 72 65 73 65 74 20 74 68 65 20 69 then reset the i
205f0 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 74 nternal schema t
205f1 61 62 6c 65 73 20 66 6f 72 20 61 6c 6c 20 64 61 ables for all da
205f2 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 73 2e tabase.** files.
205f3 20 20 49 66 20 69 44 62 3e 3d 32 20 74 68 65 6e If iDb>=2 then
205f4 20 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 reset the inter
205f5 6e 61 6c 20 73 63 68 65 6d 61 20 66 6f 72 20 6f nal schema for o
205f6 6e 6c 79 20 74 68 65 0a 2a 2a 20 73 69 6e 67 6c nly the.** singl
205f7 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 e file indicated
205f8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
205f9 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
205fa 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 ResetInternalSch
205fb 65 6d 61 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ema(sqlite3 *db,
205fc 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 69 6e 74 int iDb){. int
205fd 20 69 2c 20 6a 3b 0a 20 20 61 73 73 65 72 74 28 i, j;. assert(
205fe 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 iDb>=0 && iDb<d
205ff 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 69 66 28 b->nDb );.. if(
20600 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 iDb==0 ){. s
20601 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
20602 41 6c 6c 28 64 62 29 3b 0a 20 20 7d 0a 20 20 66 All(db);. }. f
20603 6f 72 28 69 3d 69 44 62 3b 20 69 3c 64 62 2d 3e or(i=iDb; i<db->
20604 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 44 nDb; i++){. D
20605 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 b *pDb = &db->aD
20606 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 b[i];. if( pD
20607 62 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 b->pSchema ){.
20608 20 20 20 20 61 73 73 65 72 74 28 69 3d 3d 31 20 assert(i==1
20609 7c 7c 20 28 70 44 62 2d 3e 70 42 74 20 26 26 20 || (pDb->pBt &&
2060a 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
2060b 73 4d 75 74 65 78 28 70 44 62 2d 3e 70 42 74 29 sMutex(pDb->pBt)
2060c 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ));. sqlite
2060d 33 53 63 68 65 6d 61 46 72 65 65 28 70 44 62 2d 3SchemaFree(pDb-
2060e 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d >pSchema);. }
2060f 0a 20 20 20 20 69 66 28 20 69 44 62 3e 30 20 29 . if( iDb>0 )
20610 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 return;. }. a
20611 73 73 65 72 74 28 20 69 44 62 3d 3d 30 20 29 3b ssert( iDb==0 );
20612 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 . db->flags &=
20613 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 ~SQLITE_InternCh
20614 61 6e 67 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 anges;. sqlite3
20615 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 BtreeLeaveAll(db
20616 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 );.. /* If one
20617 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 61 or more of the a
20618 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 uxiliary databas
20619 65 20 66 69 6c 65 73 20 68 61 73 20 62 65 65 6e e files has been
2061a 20 63 6c 6f 73 65 64 2c 0a 20 20 2a 2a 20 74 68 closed,. ** th
2061b 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66 en remove them f
2061c 72 6f 6d 20 74 68 65 20 61 75 78 69 6c 69 61 72 rom the auxiliar
2061d 79 20 64 61 74 61 62 61 73 65 20 6c 69 73 74 2e y database list.
2061e 20 20 57 65 20 74 61 6b 65 20 74 68 65 0a 20 20 We take the.
2061f 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 ** opportunity t
20620 6f 20 64 6f 20 74 68 69 73 20 68 65 72 65 20 73 o do this here s
20621 69 6e 63 65 20 77 65 20 68 61 76 65 20 6a 75 73 ince we have jus
20622 74 20 64 65 6c 65 74 65 64 20 61 6c 6c 20 6f 66 t deleted all of
20623 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 the. ** schema
20624 20 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e 64 hash tables and
20625 20 74 68 65 72 65 66 6f 72 65 20 64 6f 20 6e 6f therefore do no
20626 74 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 t have to make a
20627 6e 79 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 ny changes. **
20628 74 6f 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 20 to any of those
20629 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 tables.. */. f
2062a 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
2062b 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 b; i++){. str
2062c 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 uct Db *pDb = &d
2062d 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 b->aDb[i];. i
2062e 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 f( pDb->pBt==0 )
2062f 7b 0a 20 20 20 20 20 20 69 66 28 20 70 44 62 2d {. if( pDb-
20630 3e 70 41 75 78 20 26 26 20 70 44 62 2d 3e 78 46 >pAux && pDb->xF
20631 72 65 65 41 75 78 20 29 20 70 44 62 2d 3e 78 46 reeAux ) pDb->xF
20632 72 65 65 41 75 78 28 70 44 62 2d 3e 70 41 75 78 reeAux(pDb->pAux
20633 29 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 41 );. pDb->pA
20634 75 78 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 ux = 0;. }.
20635 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 32 3b 20 69 }. for(i=j=2; i
20636 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
20637 20 20 20 20 73 74 72 75 63 74 20 44 62 20 2a 70 struct Db *p
20638 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d Db = &db->aDb[i]
20639 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 ;. if( pDb->p
2063a 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 Bt==0 ){. s
2063b 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
2063c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pDb->zName);.
2063d 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3d pDb->zName =
2063e 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 0;. contin
2063f 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ue;. }. if
20640 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20 20 20 64 ( j<i ){. d
20641 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64 62 2d 3e b->aDb[j] = db->
20642 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 aDb[i];. }.
20643 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 6d 65 6d j++;. }. mem
20644 73 65 74 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2c set(&db->aDb[j],
20645 20 30 2c 20 28 64 62 2d 3e 6e 44 62 2d 6a 29 2a 0, (db->nDb-j)*
20646 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 6a sizeof(db->aDb[j
20647 5d 29 29 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d ]));. db->nDb =
20648 20 6a 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6e 44 j;. if( db->nD
20649 62 3c 3d 32 20 26 26 20 64 62 2d 3e 61 44 62 21 b<=2 && db->aDb!
2064a 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29 =db->aDbStatic )
2064b 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 64 62 2d {. memcpy(db-
2064c 3e 61 44 62 53 74 61 74 69 63 2c 20 64 62 2d 3e >aDbStatic, db->
2064d 61 44 62 2c 20 32 2a 73 69 7a 65 6f 66 28 64 62 aDb, 2*sizeof(db
2064e 2d 3e 61 44 62 5b 30 5d 29 29 3b 0a 20 20 20 20 ->aDb[0]));.
2064f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
20650 2c 20 64 62 2d 3e 61 44 62 29 3b 0a 20 20 20 20 , db->aDb);.
20651 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e 61 44 db->aDb = db->aD
20652 62 53 74 61 74 69 63 3b 0a 20 20 7d 0a 7d 0a 0a bStatic;. }.}..
20653 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
20654 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 ne is called whe
20655 6e 20 61 20 63 6f 6d 6d 69 74 20 6f 63 63 75 72 n a commit occur
20656 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
20657 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
20658 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 3CommitInternalC
20659 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a hanges(sqlite3 *
2065a 64 62 29 7b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 db){. db->flags
2065b 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65 &= ~SQLITE_Inte
2065c 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a rnChanges;.}../*
2065d 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f .** Clear the co
2065e 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 lumn names from
2065f 61 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2e a table or view.
20660 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
20661 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d sqliteResetColum
20662 6e 4e 61 6d 65 73 28 54 61 62 6c 65 20 2a 70 54 nNames(Table *pT
20663 61 62 6c 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a able){. int i;.
20664 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a Column *pCol;.
20665 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
20666 70 54 61 62 6c 65 2d 3e 64 62 3b 0a 20 20 61 73 pTable->db;. as
20667 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20 sert( pTable!=0
20668 29 3b 0a 20 20 69 66 28 20 28 70 43 6f 6c 20 3d );. if( (pCol =
20669 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 21 3d pTable->aCol)!=
2066a 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 0 ){. for(i=0
2066b 3b 20 69 3c 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c ; i<pTable->nCol
2066c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a ; i++, pCol++){.
2066d 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
2066e 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e ree(db, pCol->zN
2066f 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ame);. sqli
20670 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
20671 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a , pCol->pDflt);.
20672 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
20673 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 54 ree(db, pCol->zT
20674 79 70 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ype);. sqli
20675 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 te3DbFree(db, pC
20676 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 ol->zColl);.
20677 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 }. sqlite3DbF
20678 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e ree(db, pTable->
20679 61 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 70 54 61 aCol);. }. pTa
2067a 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 ble->aCol = 0;.
2067b 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 pTable->nCol =
2067c 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 0;.}../*.** Remo
2067d 76 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 61 ve the memory da
2067e 74 61 20 73 74 72 75 63 74 75 72 65 73 20 61 73 ta structures as
2067f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
20680 65 20 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c 65 e given.** Table
20681 2e 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 . No changes ar
20682 65 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20 62 e made to disk b
20683 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a y this routine..
20684 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
20685 6e 65 20 6a 75 73 74 20 64 65 6c 65 74 65 73 20 ne just deletes
20686 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75 the data structu
20687 72 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 re. It does not
20688 20 75 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20 74 unlink.** the t
20689 61 62 6c 65 20 64 61 74 61 20 73 74 72 75 63 74 able data struct
2068a 75 72 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73 ure from the has
2068b 68 20 74 61 62 6c 65 2e 20 20 4e 6f 72 20 64 6f h table. Nor do
2068c 65 73 20 69 74 20 72 65 6d 6f 76 65 0a 2a 2a 20 es it remove.**
2068d 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 66 72 6f foreign keys fro
2068e 6d 20 74 68 65 20 73 71 6c 69 74 65 2e 61 46 4b m the sqlite.aFK
2068f 65 79 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 ey hash table.
20690 42 75 74 20 69 74 20 64 6f 65 73 20 64 65 73 74 But it does dest
20691 72 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74 roy.** memory st
20692 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20 ructures of the
20693 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65 indices and fore
20694 69 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61 ign keys associa
20695 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65 ted with .** the
20696 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 table..*/.SQLIT
20697 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
20698 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c qlite3DeleteTabl
20699 65 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 e(Table *pTable)
2069a 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 {. Index *pInde
2069b 78 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 46 4b 65 x, *pNext;. FKe
2069c 79 20 2a 70 46 4b 65 79 2c 20 2a 70 4e 65 78 74 y *pFKey, *pNext
2069d 46 4b 65 79 3b 0a 20 20 73 71 6c 69 74 65 33 20 FKey;. sqlite3
2069e 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 *db;.. if( pTab
2069f 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a le==0 ) return;.
206a0 20 20 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 64 db = pTable->d
206a1 62 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 b;.. /* Do not
206a2 64 65 6c 65 74 65 20 74 68 65 20 74 61 62 6c 65 delete the table
206a3 20 75 6e 74 69 6c 20 74 68 65 20 72 65 66 65 72 until the refer
206a4 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 ence count reach
206a5 65 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 70 54 es zero. */. pT
206a6 61 62 6c 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 able->nRef--;.
206a7 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 if( pTable->nRef
206a8 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e >0 ){. return
206a9 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
206aa 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20 pTable->nRef==0
206ab 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 );.. /* Delete
206ac 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f all indices asso
206ad 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 ciated with this
206ae 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 66 6f table. */. fo
206af 72 28 70 49 6e 64 65 78 20 3d 20 70 54 61 62 6c r(pIndex = pTabl
206b0 65 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 e->pIndex; pInde
206b1 78 3b 20 70 49 6e 64 65 78 3d 70 4e 65 78 74 29 x; pIndex=pNext)
206b2 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 49 {. pNext = pI
206b3 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 ndex->pNext;.
206b4 20 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d assert( pIndex-
206b5 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 6c 65 >pSchema==pTable
206b6 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 ->pSchema );.
206b7 20 73 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64 sqliteDeleteInd
206b8 65 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a ex(pIndex);. }.
206b9 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
206ba 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
206bb 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c . /* Delete all
206bc 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73 foreign keys as
206bd 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
206be 69 73 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6b is table. The k
206bf 65 79 73 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 eys. ** should
206c0 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 have already bee
206c1 6e 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 n unlinked from
206c2 74 68 65 20 70 53 63 68 65 6d 61 2d 3e 61 46 4b the pSchema->aFK
206c3 65 79 20 68 61 73 68 20 74 61 62 6c 65 20 0a 20 ey hash table .
206c4 20 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d */. for(pFKey=
206c5 70 54 61 62 6c 65 2d 3e 70 46 4b 65 79 3b 20 70 pTable->pFKey; p
206c6 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 FKey; pFKey=pNex
206c7 74 46 4b 65 79 29 7b 0a 20 20 20 20 70 4e 65 78 tFKey){. pNex
206c8 74 46 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e 70 tFKey = pFKey->p
206c9 4e 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 61 73 NextFrom;. as
206ca 73 65 72 74 28 20 73 71 6c 69 74 65 33 48 61 73 sert( sqlite3Has
206cb 68 46 69 6e 64 28 26 70 54 61 62 6c 65 2d 3e 70 hFind(&pTable->p
206cc 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 0a 20 Schema->aFKey,.
206cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
206ce 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d pFKey-
206cf 3e 7a 54 6f 2c 20 73 71 6c 69 74 65 33 53 74 72 >zTo, sqlite3Str
206d0 6c 65 6e 33 30 28 70 46 4b 65 79 2d 3e 7a 54 6f len30(pFKey->zTo
206d1 29 2b 31 29 21 3d 70 46 4b 65 79 20 29 3b 0a 20 )+1)!=pFKey );.
206d2 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
206d3 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 20 20 7d (db, pFKey);. }
206d4 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 65 .#endif.. /* De
206d5 6c 65 74 65 20 74 68 65 20 54 61 62 6c 65 20 73 lete the Table s
206d6 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e tructure itself.
206d7 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 52 65 . */. sqliteRe
206d8 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 setColumnNames(p
206d9 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 Table);. sqlite
206da 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 3DbFree(db, pTab
206db 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 le->zName);. sq
206dc 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
206dd 70 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29 pTable->zColAff)
206de 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 ;. sqlite3Selec
206df 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 tDelete(db, pTab
206e0 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 23 69 le->pSelect);.#i
206e1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
206e2 54 5f 43 48 45 43 4b 0a 20 20 73 71 6c 69 74 65 T_CHECK. sqlite
206e3 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
206e4 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b pTable->pCheck);
206e5 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 .#endif. sqlite
206e6 33 56 74 61 62 43 6c 65 61 72 28 70 54 61 62 6c 3VtabClear(pTabl
206e7 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 e);. sqlite3DbF
206e8 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 29 3b ree(db, pTable);
206e9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b .}../*.** Unlink
206ea 20 74 68 65 20 67 69 76 65 6e 20 74 61 62 6c 65 the given table
206eb 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 from the hash t
206ec 61 62 6c 65 73 20 61 6e 64 20 74 68 65 20 64 65 ables and the de
206ed 6c 65 74 65 20 74 68 65 0a 2a 2a 20 74 61 62 6c lete the.** tabl
206ee 65 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 e structure with
206ef 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 all its indices
206f0 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 and foreign key
206f1 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
206f2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
206f3 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
206f4 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 Table(sqlite3 *d
206f5 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 b, int iDb, cons
206f6 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 t char *zTabName
206f7 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 ){. Table *p;.
206f8 20 46 4b 65 79 20 2a 70 46 31 2c 20 2a 70 46 32 FKey *pF1, *pF2
206f9 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 0a 20 20 ;. Db *pDb;..
206fa 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b assert( db!=0 );
206fb 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d . assert( iDb>=
206fc 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 0 && iDb<db->nDb
206fd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 54 );. assert( zT
206fe 61 62 4e 61 6d 65 20 26 26 20 7a 54 61 62 4e 61 abName && zTabNa
206ff 6d 65 5b 30 5d 20 29 3b 0a 20 20 70 44 62 20 3d me[0] );. pDb =
20700 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a &db->aDb[iDb];.
20701 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 p = sqlite3Has
20702 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53 hInsert(&pDb->pS
20703 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 chema->tblHash,
20704 7a 54 61 62 4e 61 6d 65 2c 0a 20 20 20 20 20 20 zTabName,.
20705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20706 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 sqlite3Strlen3
20707 30 28 7a 54 61 62 4e 61 6d 65 29 2b 31 2c 30 29 0(zTabName)+1,0)
20708 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 ;. if( p ){.#if
20709 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
2070a 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 _FOREIGN_KEY.
2070b 20 66 6f 72 28 70 46 31 3d 70 2d 3e 70 46 4b 65 for(pF1=p->pFKe
2070c 79 3b 20 70 46 31 3b 20 70 46 31 3d 70 46 31 2d y; pF1; pF1=pF1-
2070d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20 >pNextFrom){.
2070e 20 20 20 69 6e 74 20 6e 54 6f 20 3d 20 73 71 6c int nTo = sql
2070f 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 46 31 ite3Strlen30(pF1
20710 2d 3e 7a 54 6f 29 20 2b 20 31 3b 0a 20 20 20 20 ->zTo) + 1;.
20711 20 20 70 46 32 20 3d 20 73 71 6c 69 74 65 33 48 pF2 = sqlite3H
20712 61 73 68 46 69 6e 64 28 26 70 44 62 2d 3e 70 53 ashFind(&pDb->pS
20713 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 70 46 chema->aFKey, pF
20714 31 2d 3e 7a 54 6f 2c 20 6e 54 6f 29 3b 0a 20 20 1->zTo, nTo);.
20715 20 20 20 20 69 66 28 20 70 46 32 3d 3d 70 46 31 if( pF2==pF1
20716 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
20717 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 te3HashInsert(&p
20718 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 61 46 4b Db->pSchema->aFK
20719 65 79 2c 20 70 46 31 2d 3e 7a 54 6f 2c 20 6e 54 ey, pF1->zTo, nT
2071a 6f 2c 20 70 46 31 2d 3e 70 4e 65 78 74 54 6f 29 o, pF1->pNextTo)
2071b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
2071c 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 46 while( pF
2071d 32 20 26 26 20 70 46 32 2d 3e 70 4e 65 78 74 54 2 && pF2->pNextT
2071e 6f 21 3d 70 46 31 20 29 7b 20 70 46 32 3d 70 46 o!=pF1 ){ pF2=pF
2071f 32 2d 3e 70 4e 65 78 74 54 6f 3b 20 7d 0a 20 20 2->pNextTo; }.
20720 20 20 20 20 20 20 69 66 28 20 70 46 32 20 29 7b if( pF2 ){
20721 0a 20 20 20 20 20 20 20 20 20 20 70 46 32 2d 3e . pF2->
20722 70 4e 65 78 74 54 6f 20 3d 20 70 46 31 2d 3e 70 pNextTo = pF1->p
20723 4e 65 78 74 54 6f 3b 0a 20 20 20 20 20 20 20 20 NextTo;.
20724 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
20725 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 #endif. sqlit
20726 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 29 e3DeleteTable(p)
20727 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 ;. }. db->flag
20728 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 s |= SQLITE_Inte
20729 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a rnChanges;.}../*
2072a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 74 6f 6b 65 .** Given a toke
2072b 6e 2c 20 72 65 74 75 72 6e 20 61 20 73 74 72 69 n, return a stri
2072c 6e 67 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73 ng that consists
2072d 20 6f 66 20 74 68 65 20 74 65 78 74 20 6f 66 20 of the text of
2072e 74 68 61 74 0a 2a 2a 20 74 6f 6b 65 6e 20 77 69 that.** token wi
2072f 74 68 20 61 6e 79 20 71 75 6f 74 61 74 69 6f 6e th any quotation
20730 73 20 72 65 6d 6f 76 65 64 2e 20 20 53 70 61 63 s removed. Spac
20731 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 e to hold the re
20732 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a turned string.**
20733 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f is obtained fro
20734 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 m sqliteMalloc()
20735 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 and must be fre
20736 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e ed by the callin
20737 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a g.** function..*
20738 2a 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20 *.** Tokens are
20739 6f 66 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e 74 often just point
2073a 65 72 73 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 ers into the ori
2073b 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 61 ginal SQL text a
2073c 6e 64 20 73 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74 nd so.** are not
2073d 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 \000 terminated
2073e 20 61 6e 64 20 61 72 65 20 6e 6f 74 20 70 65 72 and are not per
2073f 73 69 73 74 65 6e 74 2e 20 20 54 68 65 20 72 65 sistent. The re
20740 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a turned string.**
20741 20 69 73 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 is \000 termina
20742 74 65 64 20 61 6e 64 20 69 73 20 70 65 72 73 69 ted and is persi
20743 73 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 stent..*/.SQLITE
20744 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
20745 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
20746 6b 65 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ken(sqlite3 *db,
20747 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a Token *pName){.
20748 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 char *zName;.
20749 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20 if( pName ){.
2074a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 zName = sqlite
2074b 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 3DbStrNDup(db, (
2074c 63 68 61 72 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 char*)pName->z,
2074d 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 73 pName->n);. s
2074e 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e qlite3Dequote(zN
2074f 61 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ame);. }else{.
20750 20 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 zName = 0;.
20751 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 }. return zName
20752 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
20753 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 the sqlite_maste
20754 72 20 74 61 62 6c 65 20 73 74 6f 72 65 64 20 69 r table stored i
20755 6e 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 n database numbe
20756 72 20 69 44 62 20 66 6f 72 0a 2a 2a 20 77 72 69 r iDb for.** wri
20757 74 69 6e 67 2e 20 54 68 65 20 74 61 62 6c 65 20 ting. The table
20758 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 is opened using
20759 63 75 72 73 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c cursor 0..*/.SQL
2075a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
2075b 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 sqlite3OpenMast
2075c 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 erTable(Parse *p
2075d 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 , int iDb){. Vd
2075e 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 be *v = sqlite3G
2075f 65 74 56 64 62 65 28 70 29 3b 0a 20 20 73 71 6c etVdbe(p);. sql
20760 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c ite3TableLock(p,
20761 20 69 44 62 2c 20 4d 41 53 54 45 52 5f 52 4f 4f iDb, MASTER_ROO
20762 54 2c 20 31 2c 20 53 43 48 45 4d 41 5f 54 41 42 T, 1, SCHEMA_TAB
20763 4c 45 28 69 44 62 29 29 3b 0a 20 20 73 71 6c 69 LE(iDb));. sqli
20764 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
20765 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e OP_SetNumColumn
20766 73 2c 20 30 2c 20 35 29 3b 2f 2a 20 73 71 6c 69 s, 0, 5);/* sqli
20767 74 65 5f 6d 61 73 74 65 72 20 68 61 73 20 35 20 te_master has 5
20768 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 73 71 6c columns */. sql
20769 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
2076a 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 , OP_OpenWrite,
2076b 30 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 0, MASTER_ROOT,
2076c 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 iDb);.}../*.** T
2076d 68 65 20 74 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 he token *pName
2076e 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d contains the nam
2076f 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 e of a database
20770 28 65 69 74 68 65 72 20 22 6d 61 69 6e 22 20 6f (either "main" o
20771 72 0a 2a 2a 20 22 74 65 6d 70 22 20 6f 72 20 74 r.** "temp" or t
20772 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74 he name of an at
20773 74 61 63 68 65 64 20 64 62 29 2e 20 54 68 69 73 tached db). This
20774 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
20775 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 the.** index of
20776 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 the named datab
20777 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d ase in db->aDb[]
20778 2c 20 6f 72 20 2d 31 20 69 66 20 74 68 65 20 6e , or -1 if the n
20779 61 6d 65 64 20 64 62 20 0a 2a 2a 20 64 6f 65 73 amed db .** does
2077a 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2f 0a 53 not exist..*/.S
2077b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
2077c 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 t sqlite3FindDb(
2077d 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b sqlite3 *db, Tok
2077e 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 69 6e en *pName){. in
2077f 74 20 69 20 3d 20 2d 31 3b 20 20 20 20 2f 2a 20 t i = -1; /*
20780 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 Database number
20781 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 */. int n;
20782 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
20783 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 characters in t
20784 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 44 62 20 he name */. Db
20785 2a 70 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 41 *pDb; /* A
20786 20 64 61 74 61 62 61 73 65 20 77 68 6f 73 65 20 database whose
20787 6e 61 6d 65 20 73 70 61 63 65 20 69 73 20 62 65 name space is be
20788 69 6e 67 20 73 65 61 72 63 68 65 64 20 2a 2f 0a ing searched */.
20789 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
2078a 20 2f 2a 20 4e 61 6d 65 20 77 65 20 61 72 65 20 /* Name we are
2078b 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f searching for */
2078c 0a 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 .. zName = sqli
2078d 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
2078e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 (db, pName);. i
2078f 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 f( zName ){.
20790 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
20791 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 n30(zName);.
20792 66 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31 for(i=(db->nDb-1
20793 29 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b ), pDb=&db->aDb[
20794 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 i]; i>=0; i--, p
20795 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66 28 Db--){. if(
20796 20 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c (!OMIT_TEMPDB |
20797 7c 20 69 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73 | i!=1 ) && n==s
20798 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 qlite3Strlen30(p
20799 44 62 2d 3e 7a 4e 61 6d 65 29 20 26 26 20 0a 20 Db->zName) && .
2079a 20 20 20 20 20 20 20 20 20 30 3d 3d 73 71 6c 69 0==sqli
2079b 74 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e te3StrICmp(pDb->
2079c 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 7b zName, zName) ){
2079d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
2079e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
2079f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
207a0 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a db, zName);. }.
207a1 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f return i;.}../
207a2 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 * The table or v
207a3 69 65 77 20 6f 72 20 74 72 69 67 67 65 72 20 6e iew or trigger n
207a4 61 6d 65 20 69 73 20 70 61 73 73 65 64 20 74 6f ame is passed to
207a5 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69 this routine vi
207a6 61 20 74 6f 6b 65 6e 73 0a 2a 2a 20 70 4e 61 6d a tokens.** pNam
207a7 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49 e1 and pName2. I
207a8 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 f the table name
207a9 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69 was fully quali
207aa 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c fied, for exampl
207ab 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 e:.**.** CREATE
207ac 54 41 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e TABLE xxx.yyy (.
207ad 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65 6e ..);.** .** Then
207ae 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 pName1 is set t
207af 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d o "xxx" and pNam
207b0 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65 e2 "yyy". On the
207b1 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a 2a other hand if.*
207b2 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 * the table name
207b3 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 is not fully qu
207b4 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 2a alified, i.e.:.*
207b5 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c *.** CREATE TABL
207b6 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 2a 2a 0a 2a E yyy(...);.**.*
207b7 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 * Then pName1 is
207b8 20 73 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e set to "yyy" an
207b9 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a d pName2 is ""..
207ba 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
207bb 6e 65 20 73 65 74 73 20 74 68 65 20 2a 70 70 55 ne sets the *ppU
207bc 6e 71 75 61 6c 20 70 6f 69 6e 74 65 72 20 74 6f nqual pointer to
207bd 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f point at the to
207be 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 2a ken (pName1 or.*
207bf 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73 * pName2) that s
207c0 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c tores the unqual
207c1 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 ified table name
207c2 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 . The index of
207c3 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
207c4 22 78 78 78 22 20 69 73 20 72 65 74 75 72 6e 65 "xxx" is returne
207c5 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
207c6 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
207c7 54 77 6f 50 61 72 74 4e 61 6d 65 28 0a 20 20 50 TwoPartName(. P
207c8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
207c9 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e /* Parsing an
207ca 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e d code generatin
207cb 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 g context */. T
207cc 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 oken *pName1,
207cd 20 20 20 2f 2a 20 54 68 65 20 22 78 78 78 22 20 /* The "xxx"
207ce 69 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78 in the name "xxx
207cf 2e 79 79 79 22 20 6f 72 20 22 78 78 78 22 20 2a .yyy" or "xxx" *
207d0 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
207d1 32 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 2, /* The "
207d2 79 79 79 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 yyy" in the name
207d3 20 22 78 78 78 2e 79 79 79 22 20 2a 2f 0a 20 20 "xxx.yyy" */.
207d4 54 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75 61 6c 20 Token **pUnqual
207d5 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
207d6 20 75 6e 71 75 61 6c 69 66 69 65 64 20 6f 62 6a unqualified obj
207d7 65 63 74 20 6e 61 6d 65 20 68 65 72 65 20 2a 2f ect name here */
207d8 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 .){. int iDb;
207d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
207da 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 6f /* Database ho
207db 6c 64 69 6e 67 20 74 68 65 20 6f 62 6a 65 63 74 lding the object
207dc 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
207dd 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
207de 0a 20 20 69 66 28 20 70 4e 61 6d 65 32 20 26 26 . if( pName2 &&
207df 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a pName2->n>0 ){.
207e0 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 if( db->init
207e1 2e 62 75 73 79 20 29 20 7b 0a 20 20 20 20 20 20 .busy ) {.
207e2 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
207e3 70 50 61 72 73 65 2c 20 22 63 6f 72 72 75 70 74 pParse, "corrupt
207e4 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20 database");.
207e5 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b pParse->nErr+
207e6 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 +;. return
207e7 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 -1;. }. *p
207e8 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65 32 3b Unqual = pName2;
207e9 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 . iDb = sqlit
207ea 65 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 e3FindDb(db, pNa
207eb 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 me1);. if( iD
207ec 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c b<0 ){. sql
207ed 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
207ee 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 rse, "unknown da
207ef 74 61 62 61 73 65 20 25 54 22 2c 20 70 4e 61 6d tabase %T", pNam
207f0 65 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 e1);. pPars
207f1 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 e->nErr++;.
207f2 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 return -1;.
207f3 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 }. }else{. a
207f4 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e ssert( db->init.
207f5 69 44 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e iDb==0 || db->in
207f6 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 69 it.busy );. i
207f7 44 62 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 69 44 Db = db->init.iD
207f8 62 3b 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 b;. *pUnqual
207f9 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 0a 20 20 = pName1;. }.
207fa 72 65 74 75 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f return iDb;.}../
207fb 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
207fc 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 e is used to che
207fd 63 6b 20 69 66 20 74 68 65 20 55 54 46 2d 38 20 ck if the UTF-8
207fe 73 74 72 69 6e 67 20 7a 4e 61 6d 65 20 69 73 20 string zName is
207ff 61 20 6c 65 67 61 6c 0a 2a 2a 20 75 6e 71 75 61 a legal.** unqua
20800 6c 69 66 69 65 64 20 6e 61 6d 65 20 66 6f 72 20 lified name for
20801 61 20 6e 65 77 20 73 63 68 65 6d 61 20 6f 62 6a a new schema obj
20802 65 63 74 20 28 74 61 62 6c 65 2c 20 69 6e 64 65 ect (table, inde
20803 78 2c 20 76 69 65 77 20 6f 72 0a 2a 2a 20 74 72 x, view or.** tr
20804 69 67 67 65 72 29 2e 20 41 6c 6c 20 6e 61 6d 65 igger). All name
20805 73 20 61 72 65 20 6c 65 67 61 6c 20 65 78 63 65 s are legal exce
20806 70 74 20 74 68 6f 73 65 20 74 68 61 74 20 62 65 pt those that be
20807 67 69 6e 20 77 69 74 68 20 74 68 65 20 73 74 72 gin with the str
20808 69 6e 67 0a 2a 2a 20 22 73 71 6c 69 74 65 5f 22 ing.** "sqlite_"
20809 20 28 69 6e 20 75 70 70 65 72 2c 20 6c 6f 77 65 (in upper, lowe
2080a 72 20 6f 72 20 6d 69 78 65 64 20 63 61 73 65 29 r or mixed case)
2080b 2e 20 54 68 69 73 20 70 6f 72 74 69 6f 6e 20 6f . This portion o
2080c 66 20 74 68 65 20 6e 61 6d 65 73 70 61 63 65 0a f the namespace.
2080d 2a 2a 20 69 73 20 72 65 73 65 72 76 65 64 20 66 ** is reserved f
2080e 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 2e or internal use.
2080f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20810 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 TE int sqlite3Ch
20811 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 eckObjectName(Pa
20812 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e rse *pParse, con
20813 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b st char *zName){
20814 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e . if( !pParse->
20815 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 db->init.busy &&
20816 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d pParse->nested=
20817 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 =0 . &&
20818 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c (pParse->db->fl
20819 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57 72 69 ags & SQLITE_Wri
2081a 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 teSchema)==0.
2081b 20 20 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c && 0==sql
2081c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61 ite3StrNICmp(zNa
2081d 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 me, "sqlite_", 7
2081e 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
2081f 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
20820 20 22 6f 62 6a 65 63 74 20 6e 61 6d 65 20 72 65 "object name re
20821 73 65 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72 served for inter
20822 6e 61 6c 20 75 73 65 3a 20 25 73 22 2c 20 7a 4e nal use: %s", zN
20823 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ame);. return
20824 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
20825 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
20826 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
20827 42 65 67 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 Begin constructi
20828 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65 20 72 ng a new table r
20829 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e epresentation in
2082a 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 memory. This i
2082b 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f s.** the first o
2082c 66 20 73 65 76 65 72 61 6c 20 61 63 74 69 6f 6e f several action
2082d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 67 routines that g
2082e 65 74 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 et called in res
2082f 70 6f 6e 73 65 0a 2a 2a 20 74 6f 20 61 20 43 52 ponse.** to a CR
20830 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 EATE TABLE state
20831 6d 65 6e 74 2e 20 20 49 6e 20 70 61 72 74 69 63 ment. In partic
20832 75 6c 61 72 2c 20 74 68 69 73 20 72 6f 75 74 69 ular, this routi
20833 6e 65 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 ne is called.**
20834 61 66 74 65 72 20 73 65 65 69 6e 67 20 74 6f 6b after seeing tok
20835 65 6e 73 20 22 43 52 45 41 54 45 22 20 61 6e 64 ens "CREATE" and
20836 20 22 54 41 42 4c 45 22 20 61 6e 64 20 74 68 65 "TABLE" and the
20837 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 table name. The
20838 20 69 73 54 65 6d 70 0a 2a 2a 20 66 6c 61 67 20 isTemp.** flag
20839 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 74 is true if the t
2083a 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 73 able should be s
2083b 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61 75 78 tored in the aux
2083c 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 0a iliary database.
2083d 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 ** file instead
2083e 6f 66 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 of in the main d
2083f 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
20840 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 his is normally
20841 74 68 65 20 63 61 73 65 0a 2a 2a 20 77 68 65 6e the case.** when
20842 20 74 68 65 20 22 54 45 4d 50 22 20 6f 72 20 22 the "TEMP" or "
20843 54 45 4d 50 4f 52 41 52 59 22 20 6b 65 79 77 6f TEMPORARY" keywo
20844 72 64 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 rd occurs in bet
20845 77 65 65 6e 0a 2a 2a 20 43 52 45 41 54 45 20 61 ween.** CREATE a
20846 6e 64 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 nd TABLE..**.**
20847 54 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 The new table re
20848 63 6f 72 64 20 69 73 20 69 6e 69 74 69 61 6c 69 cord is initiali
20849 7a 65 64 20 61 6e 64 20 70 75 74 20 69 6e 20 70 zed and put in p
2084a 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
2084b 2e 0a 2a 2a 20 41 73 20 6d 6f 72 65 20 6f 66 20 ..** As more of
2084c 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 the CREATE TABLE
2084d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 statement is pa
2084e 72 73 65 64 2c 20 61 64 64 69 74 69 6f 6e 61 6c rsed, additional
2084f 20 61 63 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 action.** routi
20850 6e 65 73 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c nes will be call
20851 65 64 20 74 6f 20 61 64 64 20 6d 6f 72 65 20 69 ed to add more i
20852 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 nformation to th
20853 69 73 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 41 74 is record..** At
20854 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
20855 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 CREATE TABLE sta
20856 74 65 6d 65 6e 74 2c 20 74 68 65 20 73 71 6c 69 tement, the sqli
20857 74 65 33 45 6e 64 54 61 62 6c 65 28 29 20 72 6f te3EndTable() ro
20858 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c utine.** is call
20859 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 ed to complete t
2085a 68 65 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 he construction
2085b 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 of the new table
2085c 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 record..*/.SQLI
2085d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
2085e 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c sqlite3StartTabl
2085f 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
20860 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 se, /* Parser
20861 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b context */. Tok
20862 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a en *pName1, /*
20863 20 46 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 First part of t
20864 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 he name of the t
20865 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a able or view */.
20866 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c Token *pName2,
20867 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 /* Second par
20868 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 t of the name of
20869 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 the table or vi
2086a 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 ew */. int isTe
2086b 6d 70 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 mp, /* True
2086c 20 69 66 20 74 68 69 73 20 69 73 20 61 20 54 45 if this is a TE
2086d 4d 50 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e MP table */. in
2086e 74 20 69 73 56 69 65 77 2c 20 20 20 20 20 20 2f t isView, /
2086f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 * True if this i
20870 73 20 61 20 56 49 45 57 20 2a 2f 0a 20 20 69 6e s a VIEW */. in
20871 74 20 69 73 56 69 72 74 75 61 6c 2c 20 20 20 2f t isVirtual, /
20872 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 * True if this i
20873 73 20 61 20 56 49 52 54 55 41 4c 20 74 61 62 6c s a VIRTUAL tabl
20874 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 e */. int noErr
20875 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f /* Do no
20876 74 68 69 6e 67 20 69 66 20 74 61 62 6c 65 20 61 thing if table a
20877 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f lready exists */
20878 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 .){. Table *pTa
20879 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 ble;. char *zNa
2087a 6d 65 20 3d 20 30 3b 20 2f 2a 20 54 68 65 20 6e me = 0; /* The n
2087b 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 ame of the new t
2087c 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 able */. sqlite
2087d 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
2087e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 db;. Vdbe *v;.
2087f 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
20880 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 /* Database nu
20881 6d 62 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 mber to create t
20882 68 65 20 74 61 62 6c 65 20 69 6e 20 2a 2f 0a 20 he table in */.
20883 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 Token *pName;
20884 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 /* Unqualified
20885 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 name of the tab
20886 6c 65 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a le to create */.
20887 0a 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 . /* The table
20888 6f 72 20 76 69 65 77 20 6e 61 6d 65 20 74 6f 20 or view name to
20889 63 72 65 61 74 65 20 69 73 20 70 61 73 73 65 64 create is passed
2088a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 to this routine
2088b 20 76 69 61 20 74 6f 6b 65 6e 73 0a 20 20 2a 2a via tokens. **
2088c 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d pName1 and pNam
2088d 65 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 e2. If the table
2088e 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 name was fully
2088f 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 qualified, for e
20890 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a xample:. **. *
20891 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 * CREATE TABLE x
20892 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 20 20 xx.yyy (...);.
20893 2a 2a 20 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e ** . ** Then pN
20894 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 ame1 is set to "
20895 78 78 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 xxx" and pName2
20896 22 79 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 "yyy". On the ot
20897 68 65 72 20 68 61 6e 64 20 69 66 0a 20 20 2a 2a her hand if. **
20898 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 the table name
20899 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61 is not fully qua
2089a 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 20 20 lified, i.e.:.
2089b 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54 **. ** CREATE T
2089c 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 20 ABLE yyy(...);.
2089d 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e **. ** Then pN
2089e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 ame1 is set to "
2089f 79 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 yyy" and pName2
208a0 69 73 20 22 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a is "".. **. **
208a1 20 54 68 65 20 63 61 6c 6c 20 62 65 6c 6f 77 20 The call below
208a2 73 65 74 73 20 74 68 65 20 70 4e 61 6d 65 20 70 sets the pName p
208a3 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 ointer to point
208a4 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e at the token (pN
208a5 61 6d 65 31 20 6f 72 0a 20 20 2a 2a 20 70 4e 61 ame1 or. ** pNa
208a6 6d 65 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 me2) that stores
208a7 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 the unqualified
208a8 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 table name. The
208a9 20 76 61 72 69 61 62 6c 65 20 69 44 62 20 69 73 variable iDb is
208aa 0a 20 20 2a 2a 20 73 65 74 20 74 6f 20 74 68 65 . ** set to the
208ab 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 index of the da
208ac 74 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20 tabase that the
208ad 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 69 73 table or view is
208ae 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63 72 65 61 to be. ** crea
208af 74 65 64 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 ted in.. */. i
208b0 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 Db = sqlite3TwoP
208b1 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 artName(pParse,
208b2 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 pName1, pName2,
208b3 26 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 &pName);. if( i
208b4 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 Db<0 ) return;.
208b5 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 if( !OMIT_TEMPD
208b6 42 20 26 26 20 69 73 54 65 6d 70 20 26 26 20 69 B && isTemp && i
208b7 44 62 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 Db>1 ){. /* I
208b8 66 20 63 72 65 61 74 69 6e 67 20 61 20 74 65 6d f creating a tem
208b9 70 20 74 61 62 6c 65 2c 20 74 68 65 20 6e 61 6d p table, the nam
208ba 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 71 75 61 e may not be qua
208bb 6c 69 66 69 65 64 20 2a 2f 0a 20 20 20 20 73 71 lified */. sq
208bc 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
208bd 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 arse, "temporary
208be 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d 75 73 74 table name must
208bf 20 62 65 20 75 6e 71 75 61 6c 69 66 69 65 64 22 be unqualified"
208c0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
208c1 20 7d 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 }. if( !OMIT_T
208c2 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 EMPDB && isTemp
208c3 29 20 69 44 62 20 3d 20 31 3b 0a 0a 20 20 70 50 ) iDb = 1;.. pP
208c4 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e arse->sNameToken
208c5 20 3d 20 2a 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61 = *pName;. zNa
208c6 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 me = sqlite3Name
208c7 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e FromToken(db, pN
208c8 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d ame);. if( zNam
208c9 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 e==0 ) return;.
208ca 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
208cb 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 sqlite3CheckObje
208cc 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a ctName(pParse, z
208cd 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 Name) ){. got
208ce 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 o begin_table_er
208cf 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 ror;. }. if( d
208d0 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29 b->init.iDb==1 )
208d1 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 69 66 isTemp = 1;.#if
208d2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
208d3 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 _AUTHORIZATION.
208d4 20 61 73 73 65 72 74 28 20 28 69 73 54 65 6d 70 assert( (isTemp
208d5 20 26 20 31 29 3d 3d 69 73 54 65 6d 70 20 29 3b & 1)==isTemp );
208d6 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 . {. int cod
208d7 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 62 e;. char *zDb
208d8 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
208d9 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 zName;. if( s
208da 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
208db 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 pParse, SQLITE_I
208dc 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 NSERT, SCHEMA_TA
208dd 42 4c 45 28 69 73 54 65 6d 70 29 2c 20 30 2c 20 BLE(isTemp), 0,
208de 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f zDb) ){. go
208df 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 to begin_table_e
208e0 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rror;. }.
208e1 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 if( isView ){.
208e2 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 if( !OMIT_TE
208e3 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 MPDB && isTemp )
208e4 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d {. code =
208e5 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
208e6 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 EMP_VIEW;.
208e7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 }else{. c
208e8 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 ode = SQLITE_CRE
208e9 41 54 45 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 ATE_VIEW;.
208ea 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
208eb 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d if( !OMIT_TEM
208ec 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b PDB && isTemp ){
208ed 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 . code =
208ee 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 SQLITE_CREATE_TE
208ef 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 MP_TABLE;.
208f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 }else{. c
208f1 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 ode = SQLITE_CRE
208f2 41 54 45 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 ATE_TABLE;.
208f3 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
208f4 20 21 69 73 56 69 72 74 75 61 6c 20 26 26 20 73 !isVirtual && s
208f5 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
208f6 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e pParse, code, zN
208f7 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a ame, 0, zDb) ){.
208f8 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e goto begin
208f9 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 _table_error;.
208fa 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a }. }.#endif..
208fb 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 /* Make sure t
208fc 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d he new table nam
208fd 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69 e does not colli
208fe 64 65 20 77 69 74 68 20 61 6e 20 65 78 69 73 74 de with an exist
208ff 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f ing. ** index o
20900 72 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 r table name in
20901 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
20902 65 2e 20 20 49 73 73 75 65 20 61 6e 20 65 72 72 e. Issue an err
20903 6f 72 20 6d 65 73 73 61 67 65 20 69 66 0a 20 20 or message if.
20904 2a 2a 20 69 74 20 64 6f 65 73 2e 20 54 68 65 20 ** it does. The
20905 65 78 63 65 70 74 69 6f 6e 20 69 73 20 69 66 20 exception is if
20906 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65 the statement be
20907 69 6e 67 20 70 61 72 73 65 64 20 77 61 73 20 70 ing parsed was p
20908 61 73 73 65 64 0a 20 20 2a 2a 20 74 6f 20 61 6e assed. ** to an
20909 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 sqlite3_declare
2090a 5f 76 74 61 62 28 29 20 63 61 6c 6c 2e 20 49 6e _vtab() call. In
2090b 20 74 68 61 74 20 63 61 73 65 20 6f 6e 6c 79 20 that case only
2090c 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 the column names
2090d 0a 20 20 2a 2a 20 61 6e 64 20 74 79 70 65 73 20 . ** and types
2090e 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 73 6f will be used, so
2090f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
20910 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6e 61 d to test for na
20911 6d 65 73 70 61 63 65 0a 20 20 2a 2a 20 63 6f 6c mespace. ** col
20912 6c 69 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 lisions.. */.
20913 69 66 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f if( !IN_DECLARE_
20914 56 54 41 42 20 29 7b 0a 20 20 20 20 69 66 28 20 VTAB ){. if(
20915 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 SQLITE_OK!=sqlit
20916 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 e3ReadSchema(pPa
20917 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f rse) ){. go
20918 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 to begin_table_e
20919 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rror;. }.
2091a 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 pTable = sqlite3
2091b 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e FindTable(db, zN
2091c 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 ame, db->aDb[iDb
2091d 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 ].zName);. if
2091e 28 20 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 ( pTable ){.
2091f 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a if( !noErr ){.
20920 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
20921 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
20922 22 74 61 62 6c 65 20 25 54 20 61 6c 72 65 61 64 "table %T alread
20923 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65 y exists", pName
20924 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
20925 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c goto begin_tabl
20926 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 e_error;. }.
20927 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 if( sqlite3Fi
20928 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d ndIndex(db, zNam
20929 65 2c 20 30 29 21 3d 30 20 26 26 20 28 69 44 62 e, 0)!=0 && (iDb
2092a 3d 3d 30 20 7c 7c 20 21 64 62 2d 3e 69 6e 69 74 ==0 || !db->init
2092b 2e 62 75 73 79 29 20 29 7b 0a 20 20 20 20 20 20 .busy) ){.
2092c 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
2092d 70 50 61 72 73 65 2c 20 22 74 68 65 72 65 20 69 pParse, "there i
2092e 73 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 s already an ind
2092f 65 78 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e ex named %s", zN
20930 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f ame);. goto
20931 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 begin_table_err
20932 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 or;. }. }..
20933 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 pTable = sqlite
20934 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
20935 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 , sizeof(Table))
20936 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d ;. if( pTable==
20937 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 0 ){. db->mal
20938 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
20939 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 pParse->rc =
2093a 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
2093b 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b pParse->nErr++
2093c 3b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e ;. goto begin
2093d 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 _table_error;.
2093e 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d }. pTable->zNam
2093f 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 e = zName;. pTa
20940 62 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b ble->iPKey = -1;
20941 0a 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 . pTable->pSche
20942 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 ma = db->aDb[iDb
20943 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 61 ].pSchema;. pTa
20944 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 ble->nRef = 1;.
20945 20 70 54 61 62 6c 65 2d 3e 64 62 20 3d 20 64 62 pTable->db = db
20946 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
20947 70 4e 65 77 54 61 62 6c 65 20 29 20 73 71 6c 69 pNewTable ) sqli
20948 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 te3DeleteTable(p
20949 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
2094a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 );. pParse->pNe
2094b 77 54 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b wTable = pTable;
2094c 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 .. /* If this i
2094d 73 20 74 68 65 20 6d 61 67 69 63 20 73 71 6c 69 s the magic sqli
2094e 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c te_sequence tabl
2094f 65 20 75 73 65 64 20 62 79 20 61 75 74 6f 69 6e e used by autoin
20950 63 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68 crement,. ** th
20951 65 6e 20 72 65 63 6f 72 64 20 61 20 70 6f 69 6e en record a poin
20952 74 65 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c ter to this tabl
20953 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 e in the main da
20954 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 tabase structure
20955 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e . ** so that IN
20956 53 45 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68 SERT can find th
20957 65 20 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a e table easily..
20958 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
20959 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
2095a 52 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50 REMENT. if( !pP
2095b 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 arse->nested &&
2095c 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 strcmp(zName, "s
2095d 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 29 qlite_sequence")
2095e 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 61 62 6c ==0 ){. pTabl
2095f 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 e->pSchema->pSeq
20960 54 61 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 Tab = pTable;.
20961 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 }.#endif.. /* B
20962 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 egin generating
20963 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 the code that wi
20964 6c 6c 20 69 6e 73 65 72 74 20 74 68 65 20 74 61 ll insert the ta
20965 62 6c 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a ble record into.
20966 20 20 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f ** the SQLITE_
20967 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 4e MASTER table. N
20968 6f 74 65 20 69 6e 20 70 61 72 74 69 63 75 6c 61 ote in particula
20969 72 20 74 68 61 74 20 77 65 20 6d 75 73 74 20 67 r that we must g
2096a 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64 o ahead. ** and
2096b 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 65 allocate the re
2096c 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 cord number for
2096d 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 the table entry
2096e 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20 61 6e 79 now. Before any
2096f 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 . ** PRIMARY KE
20970 59 20 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 77 Y or UNIQUE keyw
20971 6f 72 64 73 20 61 72 65 20 70 61 72 73 65 64 2e ords are parsed.
20972 20 20 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 Those keywords
20973 20 77 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a will cause. **
20974 20 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 indices to be c
20975 72 65 61 74 65 64 20 61 6e 64 20 74 68 65 20 74 reated and the t
20976 61 62 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 74 able record must
20977 20 63 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 65 come before the
20978 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 . ** indices.
20979 20 48 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f Hence, the reco
2097a 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 rd number for th
2097b 65 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 e table must be
2097c 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e allocated. ** n
2097d 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 ow.. */. if( !
2097e 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 db->init.busy &&
2097f 20 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 (v = sqlite3Get
20980 56 64 62 65 28 70 50 61 72 73 65 29 29 21 3d 30 Vdbe(pParse))!=0
20981 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a ){. int j1;.
20982 20 20 20 20 69 6e 74 20 66 69 6c 65 46 6f 72 6d int fileForm
20983 61 74 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 31 at;. int reg1
20984 2c 20 72 65 67 32 2c 20 72 65 67 33 3b 0a 20 20 , reg2, reg3;.
20985 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 sqlite3BeginWr
20986 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 iteOperation(pPa
20987 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 0a 23 rse, 0, iDb);..#
20988 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20989 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
2098a 20 20 20 20 69 66 28 20 69 73 56 69 72 74 75 61 if( isVirtua
2098b 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 l ){. sqlit
2098c 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 e3VdbeAddOp0(v,
2098d 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20 OP_VBegin);.
2098e 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a }.#endif.. /*
2098f 20 49 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 If the file for
20990 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67 mat and encoding
20991 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
20992 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 73 have not been s
20993 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 et, . ** set
20994 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f them now.. */
20995 0a 20 20 20 20 72 65 67 31 20 3d 20 70 50 61 72 . reg1 = pPar
20996 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 3d 20 2b se->regRowid = +
20997 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
20998 20 20 20 72 65 67 32 20 3d 20 70 50 61 72 73 65 reg2 = pParse
20999 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b 2b 70 50 ->regRoot = ++pP
2099a 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 arse->nMem;.
2099b 72 65 67 33 20 3d 20 2b 2b 70 50 61 72 73 65 2d reg3 = ++pParse-
2099c 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 >nMem;. sqlit
2099d 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
2099e 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 OP_ReadCookie, i
2099f 44 62 2c 20 72 65 67 33 2c 20 31 29 3b 20 20 20 Db, reg3, 1);
209a0 2f 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 2a /* file_format *
209a1 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 /. sqlite3Vdb
209a2 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 eUsesBtree(v, iD
209a3 62 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c b);. j1 = sql
209a4 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
209a5 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b 0a , OP_If, reg3);.
209a6 20 20 20 20 66 69 6c 65 46 6f 72 6d 61 74 20 3d fileFormat =
209a7 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 (db->flags & SQ
209a8 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 LITE_LegacyFileF
209a9 6d 74 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 20 mt)!=0 ?.
209aa 20 20 20 20 20 20 20 20 20 20 20 31 20 3a 20 53 1 : S
209ab 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 QLITE_MAX_FILE_F
209ac 4f 52 4d 41 54 3b 0a 20 20 20 20 73 71 6c 69 74 ORMAT;. sqlit
209ad 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
209ae 4f 50 5f 49 6e 74 65 67 65 72 2c 20 66 69 6c 65 OP_Integer, file
209af 46 6f 72 6d 61 74 2c 20 72 65 67 33 29 3b 0a 20 Format, reg3);.
209b0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
209b1 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f dOp3(v, OP_SetCo
209b2 6f 6b 69 65 2c 20 69 44 62 2c 20 31 2c 20 72 65 okie, iDb, 1, re
209b3 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 g3);. sqlite3
209b4 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
209b5 5f 49 6e 74 65 67 65 72 2c 20 45 4e 43 28 64 62 _Integer, ENC(db
209b6 29 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 ), reg3);. sq
209b7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
209b8 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c v, OP_SetCookie,
209b9 20 69 44 62 2c 20 34 2c 20 72 65 67 33 29 3b 0a iDb, 4, reg3);.
209ba 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
209bb 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a umpHere(v, j1);.
209bc 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 6a 75 73 . /* This jus
209bd 74 20 63 72 65 61 74 65 73 20 61 20 70 6c 61 63 t creates a plac
209be 65 2d 68 6f 6c 64 65 72 20 72 65 63 6f 72 64 20 e-holder record
209bf 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 in the sqlite_ma
209c0 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 20 20 ster table..
209c1 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 63 72 ** The record cr
209c2 65 61 74 65 64 20 64 6f 65 73 20 6e 6f 74 20 63 eated does not c
209c3 6f 6e 74 61 69 6e 20 61 6e 79 74 68 69 6e 67 20 ontain anything
209c4 79 65 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 yet. It will be
209c5 20 72 65 70 6c 61 63 65 64 0a 20 20 20 20 2a 2a replaced. **
209c6 20 62 79 20 74 68 65 20 72 65 61 6c 20 65 6e 74 by the real ent
209c7 72 79 20 69 6e 20 63 6f 64 65 20 67 65 6e 65 72 ry in code gener
209c8 61 74 65 64 20 61 74 20 73 71 6c 69 74 65 33 45 ated at sqlite3E
209c9 6e 64 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a ndTable().. *
209ca 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77 *. ** The row
209cb 69 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65 id for the new e
209cc 6e 74 72 79 20 69 73 20 6c 65 66 74 20 6f 6e 20 ntry is left on
209cd 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 the top of the s
209ce 74 61 63 6b 2e 0a 20 20 20 20 2a 2a 20 54 68 65 tack.. ** The
209cf 20 72 6f 77 69 64 20 76 61 6c 75 65 20 69 73 20 rowid value is
209d0 6e 65 65 64 65 64 20 62 79 20 74 68 65 20 63 6f needed by the co
209d1 64 65 20 74 68 61 74 20 73 71 6c 69 74 65 33 45 de that sqlite3E
209d2 6e 64 54 61 62 6c 65 20 77 69 6c 6c 0a 20 20 20 ndTable will.
209d3 20 2a 2a 20 67 65 6e 65 72 61 74 65 2e 0a 20 20 ** generate..
209d4 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 */.#if !define
209d5 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 d(SQLITE_OMIT_VI
209d6 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 EW) || !defined(
209d7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
209d8 55 41 4c 54 41 42 4c 45 29 0a 20 20 20 20 69 66 UALTABLE). if
209d9 28 20 69 73 56 69 65 77 20 7c 7c 20 69 73 56 69 ( isView || isVi
209da 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 rtual ){. s
209db 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
209dc 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
209dd 30 2c 20 72 65 67 32 29 3b 0a 20 20 20 20 7d 65 0, reg2);. }e
209de 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b lse.#endif. {
209df 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
209e0 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 beAddOp2(v, OP_C
209e1 72 65 61 74 65 54 61 62 6c 65 2c 20 69 44 62 2c reateTable, iDb,
209e2 20 72 65 67 32 29 3b 0a 20 20 20 20 7d 0a 20 20 reg2);. }.
209e3 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 sqlite3OpenMas
209e4 74 65 72 54 61 62 6c 65 28 70 50 61 72 73 65 2c terTable(pParse,
209e5 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 iDb);. sqlit
209e6 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
209e7 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c 20 OP_NewRowid, 0,
209e8 72 65 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 reg1);. sqlit
209e9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
209ea 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 33 OP_Null, 0, reg3
209eb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
209ec 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 beAddOp3(v, OP_I
209ed 6e 73 65 72 74 2c 20 30 2c 20 72 65 67 33 2c 20 nsert, 0, reg3,
209ee 72 65 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 reg1);. sqlit
209ef 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 e3VdbeChangeP5(v
209f0 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 , OPFLAG_APPEND)
209f1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
209f2 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c eAddOp0(v, OP_Cl
209f3 6f 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ose);. }.. /*
209f4 4e 6f 72 6d 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f Normal (non-erro
209f5 72 29 20 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 r) return. */.
209f6 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 66 return;.. /* If
209f7 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
209f8 2c 20 77 65 20 6a 75 6d 70 20 68 65 72 65 20 2a , we jump here *
209f9 2f 0a 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 /.begin_table_er
209fa 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 ror:. sqlite3Db
209fb 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b Free(db, zName);
209fc 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a . return;.}../*
209fd 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 .** This macro i
209fe 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 s used to compar
209ff 65 20 74 77 6f 20 73 74 72 69 6e 67 73 20 69 6e e two strings in
20a00 20 61 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 a case-insensit
20a01 69 76 65 20 6d 61 6e 6e 65 72 2e 0a 2a 2a 20 49 ive manner..** I
20a02 74 20 69 73 20 73 6c 69 67 68 74 6c 79 20 66 61 t is slightly fa
20a03 73 74 65 72 20 74 68 61 6e 20 63 61 6c 6c 69 6e ster than callin
20a04 67 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 g sqlite3StrICmp
20a05 28 29 20 64 69 72 65 63 74 6c 79 2c 20 62 75 74 () directly, but
20a06 0a 2a 2a 20 70 72 6f 64 75 63 65 73 20 6c 61 72 .** produces lar
20a07 67 65 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ger code..**.**
20a08 57 41 52 4e 49 4e 47 3a 20 54 68 69 73 20 6d 61 WARNING: This ma
20a09 63 72 6f 20 69 73 20 6e 6f 74 20 63 6f 6d 70 61 cro is not compa
20a0a 74 69 62 6c 65 20 77 69 74 68 20 74 68 65 20 73 tible with the s
20a0b 74 72 63 6d 70 28 29 20 66 61 6d 69 6c 79 2e 20 trcmp() family.
20a0c 49 74 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 72 It.** returns tr
20a0d 75 65 20 69 66 20 74 68 65 20 74 77 6f 20 73 74 ue if the two st
20a0e 72 69 6e 67 73 20 61 72 65 20 65 71 75 61 6c 2c rings are equal,
20a0f 20 6f 74 68 65 72 77 69 73 65 20 66 61 6c 73 65 otherwise false
20a10 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 54 52 ..*/.#define STR
20a11 49 43 4d 50 28 78 2c 20 79 29 20 28 5c 0a 73 71 ICMP(x, y) (\.sq
20a12 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
20a13 72 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 r[*(unsigned cha
20a14 72 20 2a 29 28 78 29 5d 3d 3d 20 20 20 5c 0a 73 r *)(x)]== \.s
20a15 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 qlite3UpperToLow
20a16 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 er[*(unsigned ch
20a17 61 72 20 2a 29 28 79 29 5d 20 20 20 20 20 5c 0a ar *)(y)] \.
20a18 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d && sqlite3StrICm
20a19 70 28 28 78 29 2b 31 2c 28 79 29 2b 31 29 3d 3d p((x)+1,(y)+1)==
20a1a 30 20 29 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 0 )../*.** Add a
20a1b 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 74 6f 20 74 new column to t
20a1c 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 he table current
20a1d 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 ly being constru
20a1e 63 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 cted..**.** The
20a1f 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 parser calls thi
20a20 73 20 72 6f 75 74 69 6e 65 20 6f 6e 63 65 20 66 s routine once f
20a21 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 64 or each column d
20a22 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e eclaration.** in
20a23 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 a CREATE TABLE
20a24 73 74 61 74 65 6d 65 6e 74 2e 20 20 73 71 6c 69 statement. sqli
20a25 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 29 20 te3StartTable()
20a26 67 65 74 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 66 gets called.** f
20a27 69 72 73 74 20 74 6f 20 67 65 74 20 74 68 69 6e irst to get thin
20a28 67 73 20 67 6f 69 6e 67 2e 20 20 54 68 65 6e 20 gs going. Then
20a29 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
20a2a 63 61 6c 6c 65 64 20 66 6f 72 20 65 61 63 68 0a called for each.
20a2b 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 ** column..*/.SQ
20a2c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20a2d 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 d sqlite3AddColu
20a2e 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 mn(Parse *pParse
20a2f 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b , Token *pName){
20a30 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 . Table *p;. i
20a31 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 3b nt i;. char *z;
20a32 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b . Column *pCol;
20a33 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
20a34 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 pParse->db;. i
20a35 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e f( (p = pParse->
20a36 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 pNewTable)==0 )
20a37 72 65 74 75 72 6e 3b 0a 23 69 66 20 53 51 4c 49 return;.#if SQLI
20a38 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 TE_MAX_COLUMN.
20a39 69 66 28 20 70 2d 3e 6e 43 6f 6c 2b 31 3e 64 62 if( p->nCol+1>db
20a3a 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
20a3b 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b LIMIT_COLUMN] ){
20a3c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
20a3d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f rMsg(pParse, "to
20a3e 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f o many columns o
20a3f 6e 20 25 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 n %s", p->zName)
20a40 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
20a41 7d 0a 23 65 6e 64 69 66 0a 20 20 7a 20 3d 20 73 }.#endif. z = s
20a42 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
20a43 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a ken(db, pName);.
20a44 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 if( z==0 ) ret
20a45 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 urn;. for(i=0;
20a46 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b i<p->nCol; i++){
20a47 0a 20 20 20 20 69 66 28 20 53 54 52 49 43 4d 50 . if( STRICMP
20a48 28 7a 2c 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a (z, p->aCol[i].z
20a49 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73 Name) ){. s
20a4a 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
20a4b 50 61 72 73 65 2c 20 22 64 75 70 6c 69 63 61 74 Parse, "duplicat
20a4c 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 25 e column name: %
20a4d 73 22 2c 20 7a 29 3b 0a 20 20 20 20 20 20 73 71 s", z);. sq
20a4e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
20a4f 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e z);. return
20a50 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
20a51 28 20 28 70 2d 3e 6e 43 6f 6c 20 26 20 30 78 37 ( (p->nCol & 0x7
20a52 29 3d 3d 30 20 29 7b 0a 20 20 20 20 43 6f 6c 75 )==0 ){. Colu
20a53 6d 6e 20 2a 61 4e 65 77 3b 0a 20 20 20 20 61 4e mn *aNew;. aN
20a54 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 ew = sqlite3DbRe
20a55 61 6c 6c 6f 63 28 64 62 2c 70 2d 3e 61 43 6f 6c alloc(db,p->aCol
20a56 2c 28 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a ,(p->nCol+8)*siz
20a57 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 eof(p->aCol[0]))
20a58 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d ;. if( aNew==
20a59 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
20a5a 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b e3DbFree(db, z);
20a5b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
20a5c 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 43 6f 6c }. p->aCol
20a5d 20 3d 20 61 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 = aNew;. }. p
20a5e 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70 Col = &p->aCol[p
20a5f 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20 6d 65 6d 73 65 ->nCol];. memse
20a60 74 28 70 43 6f 6c 2c 20 30 2c 20 73 69 7a 65 6f t(pCol, 0, sizeo
20a61 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a f(p->aCol[0]));.
20a62 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 pCol->zName =
20a63 7a 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 74 68 65 z;. . /* If the
20a64 72 65 20 69 73 20 6e 6f 20 74 79 70 65 20 73 70 re is no type sp
20a65 65 63 69 66 69 65 64 2c 20 63 6f 6c 75 6d 6e 73 ecified, columns
20a66 20 68 61 76 65 20 74 68 65 20 64 65 66 61 75 6c have the defaul
20a67 74 20 61 66 66 69 6e 69 74 79 0a 20 20 2a 2a 20 t affinity. **
20a68 27 4e 4f 4e 45 27 2e 20 49 66 20 74 68 65 72 65 'NONE'. If there
20a69 20 69 73 20 61 20 74 79 70 65 20 73 70 65 63 69 is a type speci
20a6a 66 69 65 64 2c 20 74 68 65 6e 20 73 71 6c 69 74 fied, then sqlit
20a6b 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 e3AddColumnType(
20a6c 29 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 63 ) will. ** be c
20a6d 61 6c 6c 65 64 20 6e 65 78 74 20 74 6f 20 73 65 alled next to se
20a6e 74 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 t pCol->affinity
20a6f 20 63 6f 72 72 65 63 74 6c 79 2e 0a 20 20 2a 2f correctly.. */
20a70 0a 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 . pCol->affinit
20a71 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e y = SQLITE_AFF_N
20a72 4f 4e 45 3b 0a 20 20 70 2d 3e 6e 43 6f 6c 2b 2b ONE;. p->nCol++
20a73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
20a74 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
20a75 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 d by the parser
20a76 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 while in the mid
20a77 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e dle of.** parsin
20a78 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 g a CREATE TABLE
20a79 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 22 statement. A "
20a7a 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 NOT NULL" constr
20a7b 61 69 6e 74 20 68 61 73 0a 2a 2a 20 62 65 65 6e aint has.** been
20a7c 20 73 65 65 6e 20 6f 6e 20 61 20 63 6f 6c 75 6d seen on a colum
20a7d 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 n. This routine
20a7e 20 73 65 74 73 20 74 68 65 20 6e 6f 74 4e 75 6c sets the notNul
20a7f 6c 20 66 6c 61 67 20 6f 6e 0a 2a 2a 20 74 68 65 l flag on.** the
20a80 20 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c column currentl
20a81 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 y under construc
20a82 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
20a83 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
20a84 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 ite3AddNotNull(P
20a85 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
20a86 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 54 61 t onError){. Ta
20a87 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b ble *p;. int i;
20a88 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 . if( (p = pPar
20a89 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d se->pNewTable)==
20a8a 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 0 ) return;. i
20a8b 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 69 = p->nCol-1;. i
20a8c 66 28 20 69 3e 3d 30 20 29 20 70 2d 3e 61 43 6f f( i>=0 ) p->aCo
20a8d 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 3d 20 28 l[i].notNull = (
20a8e 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 7d 0a 0a 2f u8)onError;.}../
20a8f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 65 20 63 6f *.** Scan the co
20a90 6c 75 6d 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a lumn type name z
20a91 54 79 70 65 20 28 6c 65 6e 67 74 68 20 6e 54 79 Type (length nTy
20a92 70 65 29 20 61 6e 64 20 72 65 74 75 72 6e 20 74 pe) and return t
20a93 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 he.** associated
20a94 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 2e 0a affinity type..
20a95 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
20a96 6e 65 20 64 6f 65 73 20 61 20 63 61 73 65 2d 69 ne does a case-i
20a97 6e 64 65 70 65 6e 64 65 6e 74 20 73 65 61 72 63 ndependent searc
20a98 68 20 6f 66 20 7a 54 79 70 65 20 66 6f 72 20 74 h of zType for t
20a99 68 65 20 0a 2a 2a 20 73 75 62 73 74 72 69 6e 67 he .** substring
20a9a 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 s in the followi
20a9b 6e 67 20 74 61 62 6c 65 2e 20 49 66 20 6f 6e 65 ng table. If one
20a9c 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 69 6e of the substrin
20a9d 67 73 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 gs is.** found,
20a9e 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
20a9f 67 20 61 66 66 69 6e 69 74 79 20 69 73 20 72 65 g affinity is re
20aa0 74 75 72 6e 65 64 2e 20 49 66 20 7a 54 79 70 65 turned. If zType
20aa1 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 6d 6f 72 contains.** mor
20aa2 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 e than one of th
20aa3 65 20 73 75 62 73 74 72 69 6e 67 73 2c 20 65 6e e substrings, en
20aa4 74 72 69 65 73 20 74 6f 77 61 72 64 20 74 68 65 tries toward the
20aa5 20 74 6f 70 20 6f 66 20 0a 2a 2a 20 74 68 65 20 top of .** the
20aa6 74 61 62 6c 65 20 74 61 6b 65 20 70 72 69 6f 72 table take prior
20aa7 69 74 79 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 ity. For example
20aa8 2c 20 69 66 20 7a 54 79 70 65 20 69 73 20 27 42 , if zType is 'B
20aa9 4c 4f 42 49 4e 54 27 2c 20 0a 2a 2a 20 53 51 4c LOBINT', .** SQL
20aaa 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 ITE_AFF_INTEGER
20aab 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
20aac 2a 2a 20 53 75 62 73 74 72 69 6e 67 20 20 20 20 ** Substring
20aad 20 7c 20 41 66 66 69 6e 69 74 79 0a 2a 2a 20 2d | Affinity.** -
20aae 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
20aaf 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a ---------------.
20ab0 2a 2a 20 27 49 4e 54 27 20 20 20 20 20 20 20 20 ** 'INT'
20ab1 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e | SQLITE_AFF_IN
20ab2 54 45 47 45 52 0a 2a 2a 20 27 43 48 41 52 27 20 TEGER.** 'CHAR'
20ab3 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f | SQLITE_
20ab4 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 43 4c 4f AFF_TEXT.** 'CLO
20ab5 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 B' | SQLI
20ab6 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 TE_AFF_TEXT.** '
20ab7 54 45 58 54 27 20 20 20 20 20 20 20 20 7c 20 53 TEXT' | S
20ab8 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a QLITE_AFF_TEXT.*
20ab9 2a 20 27 42 4c 4f 42 27 20 20 20 20 20 20 20 20 * 'BLOB'
20aba 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e | SQLITE_AFF_NON
20abb 45 0a 2a 2a 20 27 52 45 41 4c 27 20 20 20 20 20 E.** 'REAL'
20abc 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f | SQLITE_AFF_
20abd 52 45 41 4c 0a 2a 2a 20 27 46 4c 4f 41 27 20 20 REAL.** 'FLOA'
20abe 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 | SQLITE_A
20abf 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 44 4f 55 42 FF_REAL.** 'DOUB
20ac0 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 ' | SQLIT
20ac1 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 0a 2a 2a E_AFF_REAL.**.**
20ac2 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 If none of the
20ac3 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74 68 substrings in th
20ac4 65 20 61 62 6f 76 65 20 74 61 62 6c 65 20 61 72 e above table ar
20ac5 65 20 66 6f 75 6e 64 2c 0a 2a 2a 20 53 51 4c 49 e found,.** SQLI
20ac6 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 69 TE_AFF_NUMERIC i
20ac7 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 s returned..*/.S
20ac8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
20ac9 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 ar sqlite3Affini
20aca 74 79 54 79 70 65 28 63 6f 6e 73 74 20 54 6f 6b tyType(const Tok
20acb 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20 20 75 33 en *pType){. u3
20acc 32 20 68 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2 h = 0;. char
20acd 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 aff = SQLITE_AFF
20ace 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 63 6f 6e 73 _NUMERIC;. cons
20acf 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
20ad0 2a 7a 49 6e 20 3d 20 70 54 79 70 65 2d 3e 7a 3b *zIn = pType->z;
20ad1 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 . const unsigne
20ad2 64 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 26 d char *zEnd = &
20ad3 70 54 79 70 65 2d 3e 7a 5b 70 54 79 70 65 2d 3e pType->z[pType->
20ad4 6e 5d 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 n];.. while( zI
20ad5 6e 21 3d 7a 45 6e 64 20 29 7b 0a 20 20 20 20 68 n!=zEnd ){. h
20ad6 20 3d 20 28 68 3c 3c 38 29 20 2b 20 73 71 6c 69 = (h<<8) + sqli
20ad7 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b te3UpperToLower[
20ad8 2a 7a 49 6e 5d 3b 0a 20 20 20 20 7a 49 6e 2b 2b *zIn];. zIn++
20ad9 3b 0a 20 20 20 20 69 66 28 20 68 3d 3d 28 28 27 ;. if( h==(('
20ada 63 27 3c 3c 32 34 29 2b 28 27 68 27 3c 3c 31 36 c'<<24)+('h'<<16
20adb 29 2b 28 27 61 27 3c 3c 38 29 2b 27 72 27 29 20 )+('a'<<8)+'r')
20adc 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ){ /
20add 2a 20 43 48 41 52 20 2a 2f 0a 20 20 20 20 20 20 * CHAR */.
20ade 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 aff = SQLITE_AFF
20adf 5f 54 45 58 54 3b 20 0a 20 20 20 20 7d 65 6c 73 _TEXT; . }els
20ae0 65 20 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c e if( h==(('c'<<
20ae1 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 24)+('l'<<16)+('
20ae2 6f 27 3c 3c 38 29 2b 27 62 27 29 20 29 7b 20 20 o'<<8)+'b') ){
20ae3 20 20 20 20 20 2f 2a 20 43 4c 4f 42 20 2a 2f 0a /* CLOB */.
20ae4 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 aff = SQLI
20ae5 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 TE_AFF_TEXT;.
20ae6 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 }else if( h==((
20ae7 27 74 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 't'<<24)+('e'<<1
20ae8 36 29 2b 28 27 78 27 3c 3c 38 29 2b 27 74 27 29 6)+('x'<<8)+'t')
20ae9 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 54 45 58 ){ /* TEX
20aea 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d T */. aff =
20aeb 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
20aec 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
20aed 68 3d 3d 28 28 27 62 27 3c 3c 32 34 29 2b 28 27 h==(('b'<<24)+('
20aee 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 l'<<16)+('o'<<8)
20aef 2b 27 62 27 29 20 20 20 20 20 20 20 20 20 20 2f +'b') /
20af0 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 * BLOB */.
20af1 20 20 26 26 20 28 61 66 66 3d 3d 53 51 4c 49 54 && (aff==SQLIT
20af2 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 7c E_AFF_NUMERIC ||
20af3 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 aff==SQLITE_AFF
20af4 5f 52 45 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 _REAL) ){.
20af5 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 aff = SQLITE_AFF
20af6 5f 4e 4f 4e 45 3b 0a 23 69 66 6e 64 65 66 20 53 _NONE;.#ifndef S
20af7 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
20af8 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 7d 65 ING_POINT. }e
20af9 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 72 27 lse if( h==(('r'
20afa 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b <<24)+('e'<<16)+
20afb 28 27 61 27 3c 3c 38 29 2b 27 6c 27 29 20 20 20 ('a'<<8)+'l')
20afc 20 20 20 20 20 20 20 2f 2a 20 52 45 41 4c 20 2a /* REAL *
20afd 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 /. && aff
20afe 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d ==SQLITE_AFF_NUM
20aff 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 ERIC ){. af
20b00 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 f = SQLITE_AFF_R
20b01 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 EAL;. }else i
20b02 66 28 20 68 3d 3d 28 28 27 66 27 3c 3c 32 34 29 f( h==(('f'<<24)
20b03 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c +('l'<<16)+('o'<
20b04 3c 38 29 2b 27 61 27 29 20 20 20 20 20 20 20 20 <8)+'a')
20b05 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f 0a 20 20 20 /* FLOA */.
20b06 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c && aff==SQL
20b07 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 ITE_AFF_NUMERIC
20b08 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 ){. aff = S
20b09 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a QLITE_AFF_REAL;.
20b0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d }else if( h=
20b0b 3d 28 28 27 64 27 3c 3c 32 34 29 2b 28 27 6f 27 =(('d'<<24)+('o'
20b0c 3c 3c 31 36 29 2b 28 27 75 27 3c 3c 38 29 2b 27 <<16)+('u'<<8)+'
20b0d 62 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 b') /*
20b0e 44 4f 55 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 DOUB */.
20b0f 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 && aff==SQLITE_A
20b10 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 FF_NUMERIC ){.
20b11 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 aff = SQLITE
20b12 5f 41 46 46 5f 52 45 41 4c 3b 0a 23 65 6e 64 69 _AFF_REAL;.#endi
20b13 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 f. }else if(
20b14 28 68 26 30 78 30 30 46 46 46 46 46 46 29 3d 3d (h&0x00FFFFFF)==
20b15 28 28 27 69 27 3c 3c 31 36 29 2b 28 27 6e 27 3c (('i'<<16)+('n'<
20b16 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20 20 2f <8)+'t') ){ /
20b17 2a 20 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 61 * INT */. a
20b18 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f ff = SQLITE_AFF_
20b19 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 62 INTEGER;. b
20b1a 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
20b1b 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d . return aff;.}
20b1c 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
20b1d 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 tine is called b
20b1e 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69 y the parser whi
20b1f 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 le in the middle
20b20 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 of.** parsing a
20b21 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
20b22 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 atement. The pF
20b23 69 72 73 74 20 74 6f 6b 65 6e 20 69 73 20 74 68 irst token is th
20b24 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e e first.** token
20b25 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63 65 in the sequence
20b26 20 6f 66 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 of tokens that
20b27 64 65 73 63 72 69 62 65 20 74 68 65 20 74 79 70 describe the typ
20b28 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 e of the.** colu
20b29 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 mn currently und
20b2a 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e er construction.
20b2b 20 20 20 70 4c 61 73 74 20 69 73 20 74 68 65 20 pLast is the
20b2c 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e last token.** in
20b2d 20 74 68 65 20 73 65 71 75 65 6e 63 65 2e 20 20 the sequence.
20b2e 55 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 Use this informa
20b2f 74 69 6f 6e 20 74 6f 20 63 6f 6e 73 74 72 75 63 tion to construc
20b30 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 t a string.** th
20b31 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
20b32 74 79 70 65 6e 61 6d 65 20 6f 66 20 74 68 65 20 typename of the
20b33 63 6f 6c 75 6d 6e 20 61 6e 64 20 73 74 6f 72 65 column and store
20b34 20 74 68 61 74 20 73 74 72 69 6e 67 0a 2a 2a 20 that string.**
20b35 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f 20 0a 53 51 in zType..*/ .SQ
20b36 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20b37 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 d sqlite3AddColu
20b38 6d 6e 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 mnType(Parse *pP
20b39 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 79 arse, Token *pTy
20b3a 70 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b pe){. Table *p;
20b3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 . int i;. Colu
20b3c 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 mn *pCol;. sqli
20b3d 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 te3 *db;.. if(
20b3e 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 (p = pParse->pNe
20b3f 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 wTable)==0 ) ret
20b40 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 urn;. i = p->nC
20b41 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 69 3c 30 20 ol-1;. if( i<0
20b42 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43 6f 6c ) return;. pCol
20b43 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a = &p->aCol[i];.
20b44 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 db = pParse->d
20b45 62 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 b;. sqlite3DbFr
20b46 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 ee(db, pCol->zTy
20b47 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 54 79 pe);. pCol->zTy
20b48 70 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 pe = sqlite3Name
20b49 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 FromToken(db, pT
20b4a 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 61 66 ype);. pCol->af
20b4b 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 finity = sqlite3
20b4c 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 54 79 AffinityType(pTy
20b4d 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 pe);.}../*.** Th
20b4e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
20b4f 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 the default valu
20b50 65 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 e for the most r
20b51 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 63 6f ecently added co
20b52 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 74 lumn.** of the t
20b53 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75 able currently u
20b54 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
20b55 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 n..**.** Default
20b56 20 76 61 6c 75 65 20 65 78 70 72 65 73 73 69 6f value expressio
20b57 6e 73 20 6d 75 73 74 20 62 65 20 63 6f 6e 73 74 ns must be const
20b58 61 6e 74 2e 20 20 52 61 69 73 65 20 61 6e 20 65 ant. Raise an e
20b59 78 63 65 70 74 69 6f 6e 20 69 66 20 74 68 69 73 xception if this
20b5a 0a 2a 2a 20 69 73 20 6e 6f 74 20 74 68 65 20 63 .** is not the c
20b5b 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ase..**.** This
20b5c 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
20b5d 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 d by the parser
20b5e 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 while in the mid
20b5f 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e dle of.** parsin
20b60 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 g a CREATE TABLE
20b61 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 statement..*/.S
20b62 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
20b63 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 id sqlite3AddDef
20b64 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 20 aultValue(Parse
20b65 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
20b66 45 78 70 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a Expr){. Table *
20b67 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f p;. Column *pCo
20b68 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 l;. sqlite3 *db
20b69 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
20b6a 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 if( (p = pParse
20b6b 2d 3e 70 4e 65 77 54 61 62 6c 65 29 21 3d 30 20 ->pNewTable)!=0
20b6c 29 7b 0a 20 20 20 20 70 43 6f 6c 20 3d 20 26 28 ){. pCol = &(
20b6d 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d p->aCol[p->nCol-
20b6e 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 73 71 1]);. if( !sq
20b6f 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
20b70 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 70 45 antOrFunction(pE
20b71 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 xpr) ){. sq
20b72 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
20b73 61 72 73 65 2c 20 22 64 65 66 61 75 6c 74 20 76 arse, "default v
20b74 61 6c 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 5b alue of column [
20b75 25 73 5d 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 %s] is not const
20b76 61 6e 74 22 2c 0a 20 20 20 20 20 20 20 20 20 20 ant",.
20b77 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pCol->zName);.
20b78 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 45 }else{. E
20b79 78 70 72 20 2a 70 43 6f 70 79 3b 0a 20 20 20 20 xpr *pCopy;.
20b7a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
20b7b 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 ete(db, pCol->pD
20b7c 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c flt);. pCol
20b7d 2d 3e 70 44 66 6c 74 20 3d 20 70 43 6f 70 79 20 ->pDflt = pCopy
20b7e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 = sqlite3ExprDup
20b7f 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 (db, pExpr);.
20b80 20 20 20 69 66 28 20 70 43 6f 70 79 20 29 7b 0a if( pCopy ){.
20b81 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 sqlite3T
20b82 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 43 okenCopy(db, &pC
20b83 6f 70 79 2d 3e 73 70 61 6e 2c 20 26 70 45 78 70 opy->span, &pExp
20b84 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 r->span);.
20b85 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 }. }. }. sq
20b86 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
20b87 64 62 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f db, pExpr);.}../
20b88 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65 20 74 *.** Designate t
20b89 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 66 he PRIMARY KEY f
20b8a 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 20 70 or the table. p
20b8b 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f List is a list o
20b8c 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 63 f names .** of c
20b8d 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d olumns that form
20b8e 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 the primary key
20b8f 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 20 4e . If pList is N
20b90 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a ULL, then the.**
20b91 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 most recently a
20b92 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 dded column of t
20b93 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 he table is the
20b94 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a primary key..**.
20b95 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e 20 68 ** A table can h
20b96 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 ave at most one
20b97 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 primary key. If
20b98 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61 the table alrea
20b99 64 79 20 68 61 73 0a 2a 2a 20 61 20 70 72 69 6d dy has.** a prim
20b9a 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74 68 69 ary key (and thi
20b9b 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 s is the second
20b9c 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74 68 65 primary key) the
20b9d 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a 20 65 n create an.** e
20b9e 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 rror..**.** If t
20b9f 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 he PRIMARY KEY i
20ba0 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 63 6f s on a single co
20ba1 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61 74 61 74 lumn whose datat
20ba2 79 70 65 20 69 73 20 49 4e 54 45 47 45 52 2c 0a ype is INTEGER,.
20ba3 2a 2a 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 ** then we will
20ba4 74 72 79 20 74 6f 20 75 73 65 20 74 68 61 74 20 try to use that
20ba5 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 72 6f column as the ro
20ba6 77 69 64 2e 20 20 53 65 74 20 74 68 65 20 54 61 wid. Set the Ta
20ba7 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66 69 65 ble.iPKey.** fie
20ba8 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 ld of the table
20ba9 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 under constructi
20baa 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 69 6e 64 on to be the ind
20bab 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 54 ex of the.** INT
20bac 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
20bad 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e column. Table.
20bae 69 50 4b 65 79 20 69 73 20 73 65 74 20 74 6f 20 iPKey is set to
20baf 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a -1 if there is.*
20bb0 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 * no INTEGER PRI
20bb1 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20 MARY KEY..**.**
20bb2 49 66 20 74 68 65 20 6b 65 79 20 69 73 20 6e 6f If the key is no
20bb3 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 t an INTEGER PRI
20bb4 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 63 MARY KEY, then c
20bb5 72 65 61 74 65 20 61 20 75 6e 69 71 75 65 0a 2a reate a unique.*
20bb6 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68 65 20 * index for the
20bb7 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78 20 69 key. No index i
20bb8 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 49 4e s created for IN
20bb9 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
20bba 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 Ys..*/.SQLITE_PR
20bbb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
20bbc 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 e3AddPrimaryKey(
20bbd 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
20bbe 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 , /* Parsing
20bbf 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 context */. Exp
20bc0 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f rList *pList, /
20bc1 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c 64 20 * List of field
20bc2 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e 64 65 names to be inde
20bc3 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 xed */. int onE
20bc4 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 57 68 rror, /* Wh
20bc5 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 61 20 at to do with a
20bc6 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c uniqueness confl
20bc7 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61 75 74 ict */. int aut
20bc8 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20 54 72 oInc, /* Tr
20bc9 75 65 20 69 66 20 74 68 65 20 41 55 54 4f 49 4e ue if the AUTOIN
20bca 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72 64 20 CREMENT keyword
20bcb 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 is present */.
20bcc 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 20 20 int sortOrder
20bcd 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f 5f 41 /* SQLITE_SO_A
20bce 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53 4f 5f SC or SQLITE_SO_
20bcf 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 DESC */.){. Tab
20bd0 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73 le *pTab = pPars
20bd1 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 e->pNewTable;.
20bd2 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20 30 3b char *zType = 0;
20bd3 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 . int iCol = -1
20bd4 2c 20 69 3b 0a 20 20 69 66 28 20 70 54 61 62 3d , i;. if( pTab=
20bd5 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 =0 || IN_DECLARE
20bd6 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 70 72 69 _VTAB ) goto pri
20bd7 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 mary_key_exit;.
20bd8 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c if( pTab->tabFl
20bd9 61 67 73 20 26 20 54 46 5f 48 61 73 50 72 69 6d ags & TF_HasPrim
20bda 61 72 79 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 aryKey ){. sq
20bdb 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
20bdc 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 74 61 arse, . "ta
20bdd 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73 20 6d ble \"%s\" has m
20bde 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70 72 69 ore than one pri
20bdf 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61 62 2d mary key", pTab-
20be0 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 >zName);. got
20be1 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 o primary_key_ex
20be2 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e it;. }. pTab->
20be3 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48 tabFlags |= TF_H
20be4 61 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a 20 20 asPrimaryKey;.
20be5 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a if( pList==0 ){.
20be6 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d iCol = pTab-
20be7 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 70 >nCol - 1;. p
20be8 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e Tab->aCol[iCol].
20be9 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 isPrimKey = 1;.
20bea 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 }else{. for(
20beb 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 i=0; i<pList->nE
20bec 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 xpr; i++){.
20bed 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f for(iCol=0; iCo
20bee 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 l<pTab->nCol; iC
20bef 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 ol++){. i
20bf0 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
20bf1 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e p(pList->a[i].zN
20bf2 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b ame, pTab->aCol[
20bf3 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 iCol].zName)==0
20bf4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 ){. bre
20bf5 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
20bf6 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
20bf7 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 iCol<pTab->nCol
20bf8 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d ){. pTab-
20bf9 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72 >aCol[iCol].isPr
20bfa 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 20 imKey = 1;.
20bfb 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
20bfc 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 31 20 pList->nExpr>1
20bfd 29 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 7d ) iCol = -1;. }
20bfe 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 . if( iCol>=0 &
20bff 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f & iCol<pTab->nCo
20c00 6c 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d l ){. zType =
20c01 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c pTab->aCol[iCol
20c02 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20 20 69 ].zType;. }. i
20c03 66 28 20 7a 54 79 70 65 20 26 26 20 73 71 6c 69 f( zType && sqli
20c04 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79 70 65 te3StrICmp(zType
20c05 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d 30 0a , "INTEGER")==0.
20c06 20 20 20 20 20 20 20 20 26 26 20 73 6f 72 74 4f && sortO
20c07 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f rder==SQLITE_SO_
20c08 41 53 43 20 29 7b 0a 20 20 20 20 70 54 61 62 2d ASC ){. pTab-
20c09 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a 20 >iPKey = iCol;.
20c0a 20 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 pTab->keyConf
20c0b 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a = (u8)onError;.
20c0c 20 20 20 20 61 73 73 65 72 74 28 20 61 75 74 6f assert( auto
20c0d 49 6e 63 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e Inc==0 || autoIn
20c0e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 54 61 62 c==1 );. pTab
20c0f 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 61 75 ->tabFlags |= au
20c10 74 6f 49 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63 toInc*TF_Autoinc
20c11 72 65 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 rement;. }else
20c12 69 66 28 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23 if( autoInc ){.#
20c13 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20c14 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 IT_AUTOINCREMENT
20c15 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
20c16 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 41 55 rMsg(pParse, "AU
20c17 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f TOINCREMENT is o
20c18 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61 nly allowed on a
20c19 6e 20 22 0a 20 20 20 20 20 20 20 22 49 4e 54 45 n ". "INTE
20c1a 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 22 GER PRIMARY KEY"
20c1b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 );.#endif. }els
20c1c 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 e{. sqlite3Cr
20c1d 65 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 eateIndex(pParse
20c1e 2c 20 30 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74 , 0, 0, 0, pList
20c1f 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c , onError, 0, 0,
20c20 20 73 6f 72 74 4f 72 64 65 72 2c 20 30 29 3b 0a sortOrder, 0);.
20c21 20 20 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 pList = 0;.
20c22 20 7d 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f }..primary_key_
20c23 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 exit:. sqlite3E
20c24 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 xprListDelete(pP
20c25 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 arse->db, pList)
20c26 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f ;. return;.}../
20c27 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43 *.** Add a new C
20c28 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 HECK constraint
20c29 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 to the table cur
20c2a 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e rently under con
20c2b 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 struction..*/.SQ
20c2c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20c2d 64 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 d sqlite3AddChec
20c2e 6b 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 kConstraint(. P
20c2f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
20c30 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
20c31 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ext */. Expr *p
20c32 43 68 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 CheckExpr /* Th
20c33 65 20 63 68 65 63 6b 20 65 78 70 72 65 73 73 69 e check expressi
20c34 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 on */.){. sqlit
20c35 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
20c36 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c >db;.#ifndef SQL
20c37 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 ITE_OMIT_CHECK.
20c38 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 Table *pTab = p
20c39 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
20c3a 3b 0a 20 20 69 66 28 20 70 54 61 62 20 26 26 20 ;. if( pTab &&
20c3b 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 !IN_DECLARE_VTAB
20c3c 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 43 ){. /* The C
20c3d 48 45 43 4b 20 65 78 70 72 65 73 73 69 6f 6e 20 HECK expression
20c3e 6d 75 73 74 20 62 65 20 64 75 70 6c 69 63 61 74 must be duplicat
20c3f 65 64 20 73 6f 20 74 68 61 74 20 74 6f 6b 65 6e ed so that token
20c40 73 20 72 65 66 65 72 0a 20 20 20 20 2a 2a 20 74 s refer. ** t
20c41 6f 20 6d 61 6c 6c 6f 63 65 64 20 73 70 61 63 65 o malloced space
20c42 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 28 65 70 and not the (ep
20c43 68 65 6d 65 72 61 6c 29 20 74 65 78 74 20 6f 66 hemeral) text of
20c44 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c the CREATE TABL
20c45 45 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 E. ** stateme
20c46 6e 74 20 2a 2f 0a 20 20 20 20 70 54 61 62 2d 3e nt */. pTab->
20c47 70 43 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 pCheck = sqlite3
20c48 45 78 70 72 41 6e 64 28 64 62 2c 20 70 54 61 62 ExprAnd(db, pTab
20c49 2d 3e 70 43 68 65 63 6b 2c 20 0a 20 20 20 20 20 ->pCheck, .
20c4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20c4b 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
20c4c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 ite3ExprDup(db,
20c4d 70 43 68 65 63 6b 45 78 70 72 29 29 3b 0a 20 20 pCheckExpr));.
20c4e 7d 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 }.#endif. sqlit
20c4f 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
20c50 20 70 43 68 65 63 6b 45 78 70 72 29 3b 0a 7d 0a pCheckExpr);.}.
20c51 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 ./*.** Set the c
20c52 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f ollation functio
20c53 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 n of the most re
20c54 63 65 6e 74 6c 79 20 70 61 72 73 65 64 20 74 61 cently parsed ta
20c55 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f ble column.** to
20c56 20 74 68 65 20 43 6f 6c 6c 53 65 71 20 67 69 76 the CollSeq giv
20c57 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 en..*/.SQLITE_PR
20c58 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
20c59 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 e3AddCollateType
20c5a 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
20c5b 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a Token *pToken){.
20c5c 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e Table *p;. in
20c5d 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 43 6f t i;. char *zCo
20c5e 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
20c5f 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d /* Dequoted nam
20c60 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 e of collation s
20c61 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 73 71 6c equence */. sql
20c62 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 ite3 *db;.. if(
20c63 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e (p = pParse->pN
20c64 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 ewTable)==0 ) re
20c65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e turn;. i = p->n
20c66 43 6f 6c 2d 31 3b 0a 20 20 64 62 20 3d 20 70 50 Col-1;. db = pP
20c67 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c arse->db;. zCol
20c68 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 l = sqlite3NameF
20c69 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f romToken(db, pTo
20c6a 6b 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a 43 6f ken);. if( !zCo
20c6b 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 ll ) return;..
20c6c 69 66 28 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 if( sqlite3Locat
20c6d 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c eCollSeq(pParse,
20c6e 20 7a 43 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 zColl, -1) ){.
20c6f 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a Index *pIdx;.
20c70 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a p->aCol[i].z
20c71 43 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 Coll = zColl;.
20c72 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 . /* If the c
20c73 6f 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65 olumn is declare
20c74 64 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49 d as "<name> PRI
20c75 4d 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45 MARY KEY COLLATE
20c76 20 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a <type>",. **
20c77 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d then an index m
20c78 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 72 65 ay have been cre
20c79 61 74 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c ated on this col
20c7a 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20 umn before the.
20c7b 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 ** collation
20c7c 74 79 70 65 20 77 61 73 20 61 64 64 65 64 2e 20 type was added.
20c7d 43 6f 72 72 65 63 74 20 74 68 69 73 20 69 66 20 Correct this if
20c7e 69 74 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a it is the case..
20c7f 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 */. for(p
20c80 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 Idx=p->pIndex; p
20c81 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
20c82 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 pNext){. as
20c83 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c sert( pIdx->nCol
20c84 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 umn==1 );.
20c85 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 if( pIdx->aiColu
20c86 6d 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20 mn[0]==i ){.
20c87 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c pIdx->azColl
20c88 5b 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d [0] = p->aCol[i]
20c89 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a .zColl;. }.
20c8a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
20c8b 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
20c8c 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d (db, zColl);. }
20c8d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
20c8e 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
20c8f 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 the collation se
20c90 71 75 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62 quence for datab
20c91 61 73 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a ase native text.
20c92 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e ** encoding iden
20c93 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73 74 tified by the st
20c94 72 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 ring zName, leng
20c95 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 th nName..**.**
20c96 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 If the requested
20c97 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
20c98 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c nce is not avail
20c99 61 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61 able, or not ava
20c9a 69 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 ilable.** in the
20c9b 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 database native
20c9c 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63 encoding, the c
20c9d 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 ollation factory
20c9e 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a is invoked to.*
20c9f 2a 20 72 65 71 75 65 73 74 20 69 74 2e 20 49 66 * request it. If
20ca0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 the collation f
20ca1 61 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20 actory does not
20ca2 73 75 70 70 6c 79 20 73 75 63 68 20 61 20 73 65 supply such a se
20ca3 71 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74 quence,.** and t
20ca4 68 65 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 he sequence is a
20ca5 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74 vailable in anot
20ca6 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e her text encodin
20ca7 67 2c 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a g, then that is.
20ca8 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 ** returned inst
20ca9 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f ead..**.** If no
20caa 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 versions of the
20cab 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 requested colla
20cac 74 69 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61 tions sequence a
20cad 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 re available, or
20cae 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f .** another erro
20caf 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 r occurs, NULL i
20cb0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 s returned and a
20cb1 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
20cb2 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 written into.**
20cb3 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 pParse..**.** Th
20cb4 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
20cb5 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 wrapper around s
20cb6 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 qlite3FindCollSe
20cb7 71 28 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69 q(). This routi
20cb8 6e 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68 ne.** invokes th
20cb9 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 e collation fact
20cba 6f 72 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64 ory if the named
20cbb 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f collation canno
20cbc 74 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e t be found.** an
20cbd 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 d generates an e
20cbe 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f rror message..*/
20cbf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20cc0 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 CollSeq *sqlite3
20cc1 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 LocateCollSeq(Pa
20cc2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e rse *pParse, con
20cc3 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
20cc4 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a 20 20 73 71 int nName){. sq
20cc5 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
20cc6 73 65 2d 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63 se->db;. u8 enc
20cc7 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 75 38 = ENC(db);. u8
20cc8 20 69 6e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e initbusy = db->
20cc9 69 6e 69 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c init.busy;. Col
20cca 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 lSeq *pColl;..
20ccb 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 pColl = sqlite3F
20ccc 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 indCollSeq(db, e
20ccd 6e 63 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 nc, zName, nName
20cce 2c 20 69 6e 69 74 62 75 73 79 29 3b 0a 20 20 69 , initbusy);. i
20ccf 66 28 20 21 69 6e 69 74 62 75 73 79 20 26 26 20 f( !initbusy &&
20cd0 28 21 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c (!pColl || !pCol
20cd1 6c 2d 3e 78 43 6d 70 29 20 29 7b 0a 20 20 20 20 l->xCmp) ){.
20cd2 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 pColl = sqlite3G
20cd3 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 70 43 etCollSeq(db, pC
20cd4 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d oll, zName, nNam
20cd5 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f e);. if( !pCo
20cd6 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ll ){. if(
20cd7 6e 4e 61 6d 65 3c 30 20 29 7b 0a 20 20 20 20 20 nName<0 ){.
20cd8 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 nName = sqlit
20cd9 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 e3Strlen(db, zNa
20cda 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 me);. }.
20cdb 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
20cdc 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 sg(pParse, "no s
20cdd 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 uch collation se
20cde 71 75 65 6e 63 65 3a 20 25 2e 2a 73 22 2c 20 6e quence: %.*s", n
20cdf 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 Name, zName);.
20ce0 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 pColl = 0;.
20ce1 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 }. }.. retu
20ce2 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a rn pColl;.}.../*
20ce3 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
20ce4 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 63 72 e that will incr
20ce5 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61 ement the schema
20ce6 20 63 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54 cookie..**.** T
20ce7 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 he schema cookie
20ce8 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 is used to dete
20ce9 72 6d 69 6e 65 20 77 68 65 6e 20 74 68 65 20 73 rmine when the s
20cea 63 68 65 6d 61 20 66 6f 72 20 74 68 65 0a 2a 2a chema for the.**
20ceb 20 64 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 database change
20cec 73 2e 20 20 41 66 74 65 72 20 65 61 63 68 20 73 s. After each s
20ced 63 68 65 6d 61 20 63 68 61 6e 67 65 2c 20 74 68 chema change, th
20cee 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 0a 2a e cookie value.*
20cef 2a 20 63 68 61 6e 67 65 73 2e 20 20 57 68 65 6e * changes. When
20cf0 20 61 20 70 72 6f 63 65 73 73 20 66 69 72 73 74 a process first
20cf1 20 72 65 61 64 73 20 74 68 65 20 73 63 68 65 6d reads the schem
20cf2 61 20 69 74 20 72 65 63 6f 72 64 73 20 74 68 65 a it records the
20cf3 0a 2a 2a 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 .** cookie. The
20cf4 72 65 61 66 74 65 72 2c 20 77 68 65 6e 65 76 65 reafter, wheneve
20cf5 72 20 69 74 20 67 6f 65 73 20 74 6f 20 61 63 63 r it goes to acc
20cf6 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 ess the database
20cf7 2c 0a 2a 2a 20 69 74 20 63 68 65 63 6b 73 20 74 ,.** it checks t
20cf8 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b he cookie to mak
20cf9 65 20 73 75 72 65 20 74 68 65 20 73 63 68 65 6d e sure the schem
20cfa 61 20 68 61 73 20 6e 6f 74 20 63 68 61 6e 67 65 a has not change
20cfb 64 0a 2a 2a 20 73 69 6e 63 65 20 69 74 20 77 61 d.** since it wa
20cfc 73 20 6c 61 73 74 20 72 65 61 64 2e 0a 2a 2a 0a s last read..**.
20cfd 2a 2a 20 54 68 69 73 20 70 6c 61 6e 20 69 73 20 ** This plan is
20cfe 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62 not completely b
20cff 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e 20 20 49 74 ullet-proof. It
20d00 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 is possible for
20d01 0a 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 74 .** the schema t
20d02 6f 20 63 68 61 6e 67 65 20 6d 75 6c 74 69 70 6c o change multipl
20d03 65 20 74 69 6d 65 73 20 61 6e 64 20 66 6f 72 20 e times and for
20d04 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 62 65 the cookie to be
20d05 0a 2a 2a 20 73 65 74 20 62 61 63 6b 20 74 6f 20 .** set back to
20d06 70 72 69 6f 72 20 76 61 6c 75 65 2e 20 20 42 75 prior value. Bu
20d07 74 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 t schema changes
20d08 20 61 72 65 20 69 6e 66 72 65 71 75 65 6e 74 0a are infrequent.
20d09 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 6f 62 61 ** and the proba
20d0a 62 69 6c 69 74 79 20 6f 66 20 68 69 74 74 69 6e bility of hittin
20d0b 67 20 74 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69 g the same cooki
20d0c 65 20 76 61 6c 75 65 20 69 73 20 6f 6e 6c 79 0a e value is only.
20d0d 2a 2a 20 31 20 63 68 61 6e 63 65 20 69 6e 20 32 ** 1 chance in 2
20d0e 5e 33 32 2e 20 20 53 6f 20 77 65 27 72 65 20 73 ^32. So we're s
20d0f 61 66 65 20 65 6e 6f 75 67 68 2e 0a 2a 2f 0a 53 afe enough..*/.S
20d10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
20d11 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 id sqlite3Change
20d12 43 6f 6f 6b 69 65 28 50 61 72 73 65 20 2a 70 50 Cookie(Parse *pP
20d13 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a arse, int iDb){.
20d14 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 int r1 = sqlit
20d15 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
20d16 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 rse);. sqlite3
20d17 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
20d18 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 ;. Vdbe *v = pP
20d19 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73 arse->pVdbe;. s
20d1a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
20d1b 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
20d1c 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 db->aDb[iDb].pSc
20d1d 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f hema->schema_coo
20d1e 6b 69 65 2b 31 2c 20 72 31 29 3b 0a 20 20 73 71 kie+1, r1);. sq
20d1f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
20d20 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c v, OP_SetCookie,
20d21 20 69 44 62 2c 20 30 2c 20 72 31 29 3b 0a 20 20 iDb, 0, r1);.
20d22 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
20d23 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 mpReg(pParse, r1
20d24 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73 );.}../*.** Meas
20d25 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ure the number o
20d26 66 20 63 68 61 72 61 63 74 65 72 73 20 6e 65 65 f characters nee
20d27 64 65 64 20 74 6f 20 6f 75 74 70 75 74 20 74 68 ded to output th
20d28 65 20 67 69 76 65 6e 0a 2a 2a 20 69 64 65 6e 74 e given.** ident
20d29 69 66 69 65 72 2e 20 20 54 68 65 20 6e 75 6d 62 ifier. The numb
20d2a 65 72 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c er returned incl
20d2b 75 64 65 73 20 61 6e 79 20 71 75 6f 74 65 73 20 udes any quotes
20d2c 75 73 65 64 0a 2a 2a 20 62 75 74 20 64 6f 65 73 used.** but does
20d2d 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65 not include the
20d2e 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 null terminator
20d2f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 ..**.** The esti
20d30 6d 61 74 65 20 69 73 20 63 6f 6e 73 65 72 76 61 mate is conserva
20d31 74 69 76 65 2e 20 20 49 74 20 6d 69 67 68 74 20 tive. It might
20d32 62 65 20 6c 61 72 67 65 72 20 74 68 61 74 20 77 be larger that w
20d33 68 61 74 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 hat is.** really
20d34 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 needed..*/.stat
20d35 69 63 20 69 6e 74 20 69 64 65 6e 74 4c 65 6e 67 ic int identLeng
20d36 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a th(const char *z
20d37 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66 6f ){. int n;. fo
20d38 72 28 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20 r(n=0; *z; n++,
20d39 7a 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a z++){. if( *z
20d3a 3d 3d 27 22 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a =='"' ){ n++; }.
20d3b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 20 2b }. return n +
20d3c 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 2;.}../*.** Wri
20d3d 74 65 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 te an identifier
20d3e 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 onto the end of
20d3f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e the given strin
20d40 67 2e 20 20 41 64 64 0a 2a 2a 20 71 75 6f 74 65 g. Add.** quote
20d41 20 63 68 61 72 61 63 74 65 72 73 20 61 73 20 6e characters as n
20d42 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 eeded..*/.static
20d43 20 76 6f 69 64 20 69 64 65 6e 74 50 75 74 28 63 void identPut(c
20d44 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 har *z, int *pId
20d45 78 2c 20 63 68 61 72 20 2a 7a 53 69 67 6e 65 64 x, char *zSigned
20d46 49 64 65 6e 74 29 7b 0a 20 20 75 6e 73 69 67 6e Ident){. unsign
20d47 65 64 20 63 68 61 72 20 2a 7a 49 64 65 6e 74 20 ed char *zIdent
20d48 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
20d49 2a 29 7a 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a *)zSignedIdent;.
20d4a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64 int i, j, need
20d4b 51 75 6f 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49 Quote;. i = *pI
20d4c 64 78 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a dx;. for(j=0; z
20d4d 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a Ident[j]; j++){.
20d4e 20 20 20 20 69 66 28 20 21 69 73 61 6c 6e 75 6d if( !isalnum
20d4f 28 7a 49 64 65 6e 74 5b 6a 5d 29 20 26 26 20 7a (zIdent[j]) && z
20d50 49 64 65 6e 74 5b 6a 5d 21 3d 27 5f 27 20 29 20 Ident[j]!='_' )
20d51 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 6e 65 65 break;. }. nee
20d52 64 51 75 6f 74 65 20 3d 20 20 7a 49 64 65 6e 74 dQuote = zIdent
20d53 5b 6a 5d 21 3d 30 20 7c 7c 20 69 73 64 69 67 69 [j]!=0 || isdigi
20d54 74 28 7a 49 64 65 6e 74 5b 30 5d 29 0a 20 20 20 t(zIdent[0]).
20d55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
20d56 7c 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 | sqlite3Keyword
20d57 43 6f 64 65 28 7a 49 64 65 6e 74 2c 20 6a 29 21 Code(zIdent, j)!
20d58 3d 54 4b 5f 49 44 3b 0a 20 20 69 66 28 20 6e 65 =TK_ID;. if( ne
20d59 65 64 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d edQuote ) z[i++]
20d5a 20 3d 20 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d = '"';. for(j=
20d5b 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 0; zIdent[j]; j+
20d5c 2b 29 7b 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d +){. z[i++] =
20d5d 20 7a 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 zIdent[j];.
20d5e 69 66 28 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 if( zIdent[j]=='
20d5f 22 27 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 "' ) z[i++] = '"
20d60 27 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 ';. }. if( nee
20d61 64 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 dQuote ) z[i++]
20d62 3d 20 27 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 = '"';. z[i] =
20d63 30 3b 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 0;. *pIdx = i;.
20d64 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
20d65 65 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 e a CREATE TABLE
20d66 20 73 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f statement appro
20d67 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 67 priate for the g
20d68 69 76 65 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 iven.** table.
20d69 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 Memory to hold t
20d6a 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 he text of the s
20d6b 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 tatement is obta
20d6c 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c ined.** from sql
20d6d 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 iteMalloc() and
20d6e 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 must be freed by
20d6f 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e the calling fun
20d70 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
20d71 20 63 68 61 72 20 2a 63 72 65 61 74 65 54 61 62 char *createTab
20d72 6c 65 53 74 6d 74 28 73 71 6c 69 74 65 33 20 2a leStmt(sqlite3 *
20d73 64 62 2c 20 54 61 62 6c 65 20 2a 70 2c 20 69 6e db, Table *p, in
20d74 74 20 69 73 54 65 6d 70 29 7b 0a 20 20 69 6e 74 t isTemp){. int
20d75 20 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 61 72 i, k, n;. char
20d76 20 2a 7a 53 74 6d 74 3b 0a 20 20 63 68 61 72 20 *zStmt;. char
20d77 2a 7a 53 65 70 2c 20 2a 7a 53 65 70 32 2c 20 2a *zSep, *zSep2, *
20d78 7a 45 6e 64 2c 20 2a 7a 3b 0a 20 20 43 6f 6c 75 zEnd, *z;. Colu
20d79 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 mn *pCol;. n =
20d7a 30 3b 0a 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20 0;. for(pCol =
20d7b 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c p->aCol, i=0; i<
20d7c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 p->nCol; i++, pC
20d7d 6f 6c 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 ol++){. n +=
20d7e 69 64 65 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c identLength(pCol
20d7f 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7a 20 ->zName);. z
20d80 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b 0a 20 = pCol->zType;.
20d81 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 if( z ){.
20d82 20 20 6e 20 2b 3d 20 28 73 71 6c 69 74 65 33 53 n += (sqlite3S
20d83 74 72 6c 65 6e 33 30 28 7a 29 20 2b 20 31 29 3b trlen30(z) + 1);
20d84 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6e 20 2b . }. }. n +
20d85 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 70 2d = identLength(p-
20d86 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 6e >zName);. if( n
20d87 3c 35 30 20 29 7b 0a 20 20 20 20 7a 53 65 70 20 <50 ){. zSep
20d88 3d 20 22 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 = "";. zSep2
20d89 3d 20 22 2c 22 3b 0a 20 20 20 20 7a 45 6e 64 20 = ",";. zEnd
20d8a 3d 20 22 29 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a = ")";. }else{.
20d8b 20 20 20 20 7a 53 65 70 20 3d 20 22 5c 6e 20 20 zSep = "\n
20d8c 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d 20 22 ";. zSep2 = "
20d8d 2c 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 45 6e 64 ,\n ";. zEnd
20d8e 20 3d 20 22 5c 6e 29 22 3b 0a 20 20 7d 0a 20 20 = "\n)";. }.
20d8f 6e 20 2b 3d 20 33 35 20 2b 20 36 2a 70 2d 3e 6e n += 35 + 6*p->n
20d90 43 6f 6c 3b 0a 20 20 7a 53 74 6d 74 20 3d 20 73 Col;. zStmt = s
20d91 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 qlite3Malloc( n
20d92 29 3b 0a 20 20 69 66 28 20 7a 53 74 6d 74 3d 3d );. if( zStmt==
20d93 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 0 ){. db->mal
20d94 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
20d95 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
20d96 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
20d97 6e 74 66 28 6e 2c 20 7a 53 74 6d 74 2c 0a 20 20 ntf(n, zStmt,.
20d98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d99 21 4f 4d 49 54 5f 54 45 4d 50 44 42 26 26 69 73 !OMIT_TEMPDB&&is
20d9a 54 65 6d 70 20 3f 20 22 43 52 45 41 54 45 20 54 Temp ? "CREATE T
20d9b 45 4d 50 20 54 41 42 4c 45 20 22 3a 22 43 52 45 EMP TABLE ":"CRE
20d9c 41 54 45 20 54 41 42 4c 45 20 22 29 3b 0a 20 20 ATE TABLE ");.
20d9d 6b 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 k = sqlite3Strle
20d9e 6e 33 30 28 7a 53 74 6d 74 29 3b 0a 20 20 69 64 n30(zStmt);. id
20d9f 65 6e 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b entPut(zStmt, &k
20da0 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7a , p->zName);. z
20da1 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 28 27 3b Stmt[k++] = '(';
20da2 0a 20 20 66 6f 72 28 70 43 6f 6c 3d 70 2d 3e 61 . for(pCol=p->a
20da3 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e Col, i=0; i<p->n
20da4 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b Col; i++, pCol++
20da5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ){. sqlite3_s
20da6 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 nprintf(n-k, &zS
20da7 74 6d 74 5b 6b 5d 2c 20 7a 53 65 70 29 3b 0a 20 tmt[k], zSep);.
20da8 20 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 k += sqlite3S
20da9 74 72 6c 65 6e 33 30 28 26 7a 53 74 6d 74 5b 6b trlen30(&zStmt[k
20daa 5d 29 3b 0a 20 20 20 20 7a 53 65 70 20 3d 20 7a ]);. zSep = z
20dab 53 65 70 32 3b 0a 20 20 20 20 69 64 65 6e 74 50 Sep2;. identP
20dac 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 43 ut(zStmt, &k, pC
20dad 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ol->zName);.
20dae 69 66 28 20 28 7a 20 3d 20 70 43 6f 6c 2d 3e 7a if( (z = pCol->z
20daf 54 79 70 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Type)!=0 ){.
20db0 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 zStmt[k++] = '
20db1 20 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ';. assert
20db2 28 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33 53 ( (int)(sqlite3S
20db3 74 72 6c 65 6e 33 30 28 7a 29 2b 6b 2b 31 29 3c trlen30(z)+k+1)<
20db4 3d 6e 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 =n );. sqli
20db5 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b te3_snprintf(n-k
20db6 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 22 25 73 , &zStmt[k], "%s
20db7 22 2c 20 7a 29 3b 0a 20 20 20 20 20 20 6b 20 2b ", z);. k +
20db8 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
20db9 30 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0(z);. }. }.
20dba 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
20dbb 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b tf(n-k, &zStmt[k
20dbc 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29 3b 0a ], "%s", zEnd);.
20dbd 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74 3b 0a return zStmt;.
20dbe 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
20dbf 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
20dc0 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20 66 69 to report the fi
20dc1 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74 65 72 nal ")" that ter
20dc2 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43 52 45 minates.** a CRE
20dc3 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d ATE TABLE statem
20dc4 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 ent..**.** The t
20dc5 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 able structure t
20dc6 68 61 74 20 6f 74 68 65 72 20 61 63 74 69 6f 6e hat other action
20dc7 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 62 routines have b
20dc8 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a 2a 2a 20 een building.**
20dc9 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 is added to the
20dca 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 internal hash ta
20dcb 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e 67 20 6e bles, assuming n
20dcc 6f 20 65 72 72 6f 72 73 20 68 61 76 65 0a 2a 2a o errors have.**
20dcd 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a 2a occurred..**.**
20dce 20 41 6e 20 65 6e 74 72 79 20 66 6f 72 20 74 68 An entry for th
20dcf 65 20 74 61 62 6c 65 20 69 73 20 6d 61 64 65 20 e table is made
20dd0 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 in the master ta
20dd1 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 75 6e 6c ble on disk, unl
20dd2 65 73 73 0a 2a 2a 20 74 68 69 73 20 69 73 20 61 ess.** this is a
20dd3 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
20dd4 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 or db->init.bus
20dd5 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64 62 2d 3e y==1. When db->
20dd6 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a 2a 2a 20 init.busy==1.**
20dd7 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 it means we are
20dd8 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c 69 reading the sqli
20dd9 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 te_master table
20dda 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 0a because we just.
20ddb 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20 ** connected to
20ddc 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 the database or
20ddd 62 65 63 61 75 73 65 20 74 68 65 20 73 71 6c 69 because the sqli
20dde 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 te_master table
20ddf 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74 6c 79 20 has.** recently
20de0 63 68 61 6e 67 65 64 2c 20 73 6f 20 74 68 65 20 changed, so the
20de1 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73 20 74 entry for this t
20de2 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 able already exi
20de3 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71 sts in.** the sq
20de4 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c lite_master tabl
20de5 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 e. We do not wa
20de6 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69 74 20 nt to create it
20de7 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 again..**.** If
20de8 74 68 65 20 70 53 65 6c 65 63 74 20 61 72 67 75 the pSelect argu
20de9 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c ment is not NULL
20dea 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
20deb 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
20dec 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 was called to cr
20ded 65 61 74 65 20 61 20 74 61 62 6c 65 20 67 65 6e eate a table gen
20dee 65 72 61 74 65 64 20 66 72 6f 6d 20 61 20 0a 2a erated from a .*
20def 2a 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 * "CREATE TABLE
20df0 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 20 2e 2e ... AS SELECT ..
20df1 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 ." statement. T
20df2 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 he column names
20df3 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 74 61 of.** the new ta
20df4 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63 68 20 74 ble will match t
20df5 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 he result set of
20df6 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a the SELECT..*/.
20df7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
20df8 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 54 61 oid sqlite3EndTa
20df9 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 ble(. Parse *pP
20dfa 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f arse, /
20dfb 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 * Parse context
20dfc 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43 6f 6e */. Token *pCon
20dfd 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
20dfe 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e 20 61 66 The ',' token af
20dff 74 65 72 20 74 68 65 20 6c 61 73 74 20 63 6f 6c ter the last col
20e00 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a 20 20 54 umn defn. */. T
20e01 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20 20 oken *pEnd,
20e02 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 /* The fi
20e03 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e 20 69 6e nal ')' token in
20e04 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c the CREATE TABL
20e05 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 E */. Select *p
20e06 53 65 6c 65 63 74 20 20 20 20 20 20 20 20 20 2f Select /
20e07 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d 20 61 20 * Select from a
20e08 22 43 52 45 41 54 45 20 2e 2e 2e 20 41 53 20 53 "CREATE ... AS S
20e09 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20 20 54 ELECT" */.){. T
20e0a 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71 6c 69 74 able *p;. sqlit
20e0b 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
20e0c 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a >db;. int iDb;.
20e0d 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d 30 20 . if( (pEnd==0
20e0e 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29 20 7c && pSelect==0) |
20e0f 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c | pParse->nErr |
20e10 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
20e11 65 64 20 29 20 7b 0a 20 20 20 20 72 65 74 75 72 ed ) {. retur
20e12 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 50 61 n;. }. p = pPa
20e13 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a rse->pNewTable;.
20e14 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
20e15 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 urn;.. assert(
20e16 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c !db->init.busy |
20e17 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a 0a 20 | !pSelect );..
20e18 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
20e19 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 hemaToIndex(db,
20e1a 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69 p->pSchema);..#i
20e1b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
20e1c 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52 65 73 T_CHECK. /* Res
20e1d 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 6c olve names in al
20e1e 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 l CHECK constrai
20e1f 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 0a nt expressions..
20e20 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 43 */. if( p->pC
20e21 68 65 63 6b 20 29 7b 0a 20 20 20 20 53 72 63 4c heck ){. SrcL
20e22 69 73 74 20 73 53 72 63 3b 20 20 20 20 20 20 20 ist sSrc;
20e23 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
20e24 61 6b 65 20 53 72 63 4c 69 73 74 20 66 6f 72 20 ake SrcList for
20e25 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
20e26 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e e */. NameCon
20e27 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 text sNC;
20e28 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
20e29 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 50 61 context for pPa
20e2a 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a rse->pNewTable *
20e2b 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 /.. memset(&s
20e2c 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e NC, 0, sizeof(sN
20e2d 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 C));. memset(
20e2e 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65 6f 66 &sSrc, 0, sizeof
20e2f 28 73 53 72 63 29 29 3b 0a 20 20 20 20 73 53 72 (sSrc));. sSr
20e30 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 20 c.nSrc = 1;.
20e31 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d 65 20 sSrc.a[0].zName
20e32 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 = p->zName;.
20e33 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62 20 3d sSrc.a[0].pTab =
20e34 20 70 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b 30 p;. sSrc.a[0
20e35 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a ].iCursor = -1;.
20e36 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d sNC.pParse =
20e37 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73 4e 43 pParse;. sNC
20e38 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73 53 72 .pSrcList = &sSr
20e39 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43 68 65 c;. sNC.isChe
20e3a 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 ck = 1;. if(
20e3b 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
20e3c 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d prNames(&sNC, p-
20e3d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20 20 20 >pCheck) ){.
20e3e 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
20e3f 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 }.#endif /* !d
20e40 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
20e41 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20 IT_CHECK) */..
20e42 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e 69 6e /* If the db->in
20e43 69 74 2e 62 75 73 79 20 69 73 20 31 20 69 74 20 it.busy is 1 it
20e44 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65 61 means we are rea
20e45 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f 66 66 ding the SQL off
20e46 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c 69 74 the. ** "sqlit
20e47 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 73 71 e_master" or "sq
20e48 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 lite_temp_master
20e49 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 64 " table on the d
20e4a 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64 6f 20 isk.. ** So do
20e4b 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 65 not write to the
20e4c 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20 45 78 disk again. Ex
20e4d 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74 20 70 tract the root p
20e4e 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 age number. **
20e4f 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 66 72 for the table fr
20e50 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e om the db->init.
20e51 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e 20 20 newTnum field.
20e52 28 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 (The page number
20e53 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61 76 . ** should hav
20e54 65 20 62 65 65 6e 20 70 75 74 20 74 68 65 72 65 e been put there
20e55 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 4f 70 by the sqliteOp
20e56 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29 0a 20 enCb routine.).
20e57 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e */. if( db->in
20e58 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 70 it.busy ){. p
20e59 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 ->tnum = db->ini
20e5a 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d 0a 0a t.newTnum;. }..
20e5b 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e 69 74 /* If not init
20e5c 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e 20 63 ializing, then c
20e5d 72 65 61 74 65 20 61 20 72 65 63 6f 72 64 20 66 reate a record f
20e5e 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 or the new table
20e5f 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 53 51 4c . ** in the SQL
20e60 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 ITE_MASTER table
20e61 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
20e62 2e 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 . The record nu
20e63 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 74 68 mber. ** for th
20e64 65 20 6e 65 77 20 74 61 62 6c 65 20 65 6e 74 72 e new table entr
20e65 79 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 y should already
20e66 20 62 65 20 6f 6e 20 74 68 65 20 73 74 61 63 6b be on the stack
20e67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 .. **. ** If t
20e68 68 69 73 20 69 73 20 61 20 54 45 4d 50 4f 52 41 his is a TEMPORA
20e69 52 59 20 74 61 62 6c 65 2c 20 77 72 69 74 65 20 RY table, write
20e6a 74 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 the entry into t
20e6b 68 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20 2a he auxiliary. *
20e6c 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f * file instead o
20e6d 66 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 f into the main
20e6e 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 database file..
20e6f 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 */. if( !db->i
20e70 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 nit.busy ){.
20e71 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62 65 20 int n;. Vdbe
20e72 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 *v;. char *zT
20e73 79 70 65 3b 20 20 20 20 2f 2a 20 22 76 69 65 77 ype; /* "view
20e74 22 20 6f 72 20 22 74 61 62 6c 65 22 20 2a 2f 0a " or "table" */.
20e75 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65 32 char *zType2
20e76 3b 20 20 20 2f 2a 20 22 56 49 45 57 22 20 6f 72 ; /* "VIEW" or
20e77 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20 20 20 "TABLE" */.
20e78 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20 20 20 char *zStmt;
20e79 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 43 /* Text of the C
20e7a 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20 43 REATE TABLE or C
20e7b 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74 65 REATE VIEW state
20e7c 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76 20 3d ment */.. v =
20e7d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
20e7e 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 pParse);. if(
20e7f 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a v==0 ) return;.
20e80 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
20e81 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f AddOp1(v, OP_Clo
20e82 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 se, 0);.. /*
20e83 43 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74 70 Create the rootp
20e84 61 67 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 age for the new
20e85 74 61 62 6c 65 20 61 6e 64 20 70 75 73 68 20 69 table and push i
20e86 74 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b t onto the stack
20e87 2e 0a 20 20 20 20 2a 2a 20 41 20 76 69 65 77 20 .. ** A view
20e88 68 61 73 20 6e 6f 20 72 6f 6f 74 70 61 67 65 2c has no rootpage,
20e89 20 73 6f 20 6a 75 73 74 20 70 75 73 68 20 61 20 so just push a
20e8a 7a 65 72 6f 20 6f 6e 74 6f 20 74 68 65 20 73 74 zero onto the st
20e8b 61 63 6b 20 66 6f 72 0a 20 20 20 20 2a 2a 20 76 ack for. ** v
20e8c 69 65 77 73 2e 20 20 49 6e 69 74 69 61 6c 69 7a iews. Initializ
20e8d 65 20 7a 54 79 70 65 20 61 74 20 74 68 65 20 73 e zType at the s
20e8e 61 6d 65 20 74 69 6d 65 2e 0a 20 20 20 20 2a 2f ame time.. */
20e8f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 53 65 6c . if( p->pSel
20e90 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ect==0 ){.
20e91 2f 2a 20 41 20 72 65 67 75 6c 61 72 20 74 61 62 /* A regular tab
20e92 6c 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70 le */. zTyp
20e93 65 20 3d 20 22 74 61 62 6c 65 22 3b 0a 20 20 20 e = "table";.
20e94 20 20 20 7a 54 79 70 65 32 20 3d 20 22 54 41 42 zType2 = "TAB
20e95 4c 45 22 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c LE";.#ifndef SQL
20e96 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 ITE_OMIT_VIEW.
20e97 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f }else{. /
20e98 2a 20 41 20 76 69 65 77 20 2a 2f 0a 20 20 20 20 * A view */.
20e99 20 20 7a 54 79 70 65 20 3d 20 22 76 69 65 77 22 zType = "view"
20e9a 3b 0a 20 20 20 20 20 20 7a 54 79 70 65 32 20 3d ;. zType2 =
20e9b 20 22 56 49 45 57 22 3b 0a 23 65 6e 64 69 66 0a "VIEW";.#endif.
20e9c 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
20e9d 20 74 68 69 73 20 69 73 20 61 20 43 52 45 41 54 this is a CREAT
20e9e 45 20 54 41 42 4c 45 20 78 78 20 41 53 20 53 45 E TABLE xx AS SE
20e9f 4c 45 43 54 20 2e 2e 2e 2c 20 65 78 65 63 75 74 LECT ..., execut
20ea0 65 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 e the SELECT.
20ea1 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 6f ** statement to
20ea2 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65 populate the ne
20ea3 77 20 74 61 62 6c 65 2e 20 54 68 65 20 72 6f 6f w table. The roo
20ea4 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f t-page number fo
20ea5 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 65 77 r the. ** new
20ea6 20 74 61 62 6c 65 20 69 73 20 6f 6e 20 74 68 65 table is on the
20ea7 20 74 6f 70 20 6f 66 20 74 68 65 20 76 64 62 65 top of the vdbe
20ea8 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2a 0a 20 stack.. **.
20ea9 20 20 20 2a 2a 20 4f 6e 63 65 20 74 68 65 20 53 ** Once the S
20eaa 45 4c 45 43 54 20 68 61 73 20 62 65 65 6e 20 63 ELECT has been c
20eab 6f 64 65 64 20 62 79 20 73 71 6c 69 74 65 33 53 oded by sqlite3S
20eac 65 6c 65 63 74 28 29 2c 20 69 74 20 69 73 20 69 elect(), it is i
20ead 6e 20 61 0a 20 20 20 20 2a 2a 20 73 75 69 74 61 n a. ** suita
20eae 62 6c 65 20 73 74 61 74 65 20 74 6f 20 71 75 65 ble state to que
20eaf 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d ry for the colum
20eb0 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74 79 70 65 n names and type
20eb1 73 20 74 6f 20 62 65 20 75 73 65 64 0a 20 20 20 s to be used.
20eb2 20 2a 2a 20 62 79 20 74 68 65 20 6e 65 77 20 74 ** by the new t
20eb3 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 able.. **.
20eb4 20 2a 2a 20 41 20 73 68 61 72 65 64 2d 63 61 63 ** A shared-cac
20eb5 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73 he write-lock is
20eb6 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f not required to
20eb7 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 6e 65 write to the ne
20eb8 77 20 74 61 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 w table,. **
20eb9 61 73 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b as a schema-lock
20eba 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72 65 61 must have alrea
20ebb 64 79 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 dy been obtained
20ebc 20 74 6f 20 63 72 65 61 74 65 20 69 74 2e 20 53 to create it. S
20ebd 69 6e 63 65 0a 20 20 20 20 2a 2a 20 61 20 73 63 ince. ** a sc
20ebe 68 65 6d 61 2d 6c 6f 63 6b 20 65 78 63 6c 75 64 hema-lock exclud
20ebf 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 64 61 74 es all other dat
20ec0 61 62 61 73 65 20 75 73 65 72 73 2c 20 74 68 65 abase users, the
20ec1 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 6f 75 6c write-lock woul
20ec2 64 0a 20 20 20 20 2a 2a 20 62 65 20 72 65 64 75 d. ** be redu
20ec3 6e 64 61 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 ndant.. */.
20ec4 20 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b if( pSelect ){
20ec5 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 . SelectDes
20ec6 74 20 64 65 73 74 3b 0a 20 20 20 20 20 20 54 61 t dest;. Ta
20ec7 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 0a 0a 20 ble *pSelTab;..
20ec8 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61 72 assert(pPar
20ec9 73 65 2d 3e 6e 54 61 62 3d 3d 30 29 3b 0a 20 20 se->nTab==0);.
20eca 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
20ecb 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e ddOp3(v, OP_Open
20ecc 57 72 69 74 65 2c 20 31 2c 20 70 50 61 72 73 65 Write, 1, pParse
20ecd 2d 3e 72 65 67 52 6f 6f 74 2c 20 69 44 62 29 3b ->regRoot, iDb);
20ece 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
20ecf 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 31 29 beChangeP5(v, 1)
20ed0 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e ;. pParse->
20ed1 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20 20 20 20 nTab = 2;.
20ed2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 sqlite3SelectDes
20ed3 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 tInit(&dest, SRT
20ed4 5f 54 61 62 6c 65 2c 20 31 29 3b 0a 20 20 20 20 _Table, 1);.
20ed5 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 sqlite3Select(
20ed6 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c pParse, pSelect,
20ed7 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 73 &dest);. s
20ed8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
20ed9 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 31 29 (v, OP_Close, 1)
20eda 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 ;. if( pPar
20edb 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 se->nErr==0 ){.
20edc 20 20 20 20 20 20 20 70 53 65 6c 54 61 62 20 3d pSelTab =
20edd 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 sqlite3ResultSe
20ede 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 65 tOfSelect(pParse
20edf 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 , pSelect);.
20ee0 20 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 3d if( pSelTab=
20ee1 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 =0 ) return;.
20ee2 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
20ee3 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 aCol==0 );.
20ee4 20 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 p->nCol = pSe
20ee5 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 lTab->nCol;.
20ee6 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 70 53 p->aCol = pS
20ee7 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 elTab->aCol;.
20ee8 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43 pSelTab->nC
20ee9 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ol = 0;.
20eea 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 pSelTab->aCol =
20eeb 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 0;. sqlit
20eec 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 53 e3DeleteTable(pS
20eed 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20 7d 0a elTab);. }.
20eee 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f }.. /* Co
20eef 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65 mpute the comple
20ef0 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 te text of the C
20ef1 52 45 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 REATE statement
20ef2 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65 */. if( pSele
20ef3 63 74 20 29 7b 0a 20 20 20 20 20 20 7a 53 74 6d ct ){. zStm
20ef4 74 20 3d 20 63 72 65 61 74 65 54 61 62 6c 65 53 t = createTableS
20ef5 74 6d 74 28 64 62 2c 20 70 2c 20 70 2d 3e 70 53 tmt(db, p, p->pS
20ef6 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 chema==db->aDb[1
20ef7 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 ].pSchema);.
20ef8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 20 3d }else{. n =
20ef9 20 28 69 6e 74 29 28 70 45 6e 64 2d 3e 7a 20 2d (int)(pEnd->z -
20efa 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f pParse->sNameTo
20efb 6b 65 6e 2e 7a 29 20 2b 20 31 3b 0a 20 20 20 20 ken.z) + 1;.
20efc 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 zStmt = sqlite
20efd 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 3MPrintf(db, .
20efe 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20 "CREATE
20eff 25 73 20 25 2e 2a 73 22 2c 20 7a 54 79 70 65 32 %s %.*s", zType2
20f00 2c 20 6e 2c 20 70 50 61 72 73 65 2d 3e 73 4e 61 , n, pParse->sNa
20f01 6d 65 54 6f 6b 65 6e 2e 7a 0a 20 20 20 20 20 20 meToken.z.
20f02 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a );. }.. /*
20f03 20 41 20 73 6c 6f 74 20 66 6f 72 20 74 68 65 20 A slot for the
20f04 72 65 63 6f 72 64 20 68 61 73 20 61 6c 72 65 61 record has alrea
20f05 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 dy been allocate
20f06 64 20 69 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a d in the . **
20f07 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 SQLITE_MASTER t
20f08 61 62 6c 65 2e 20 20 57 65 20 6a 75 73 74 20 6e able. We just n
20f09 65 65 64 20 74 6f 20 75 70 64 61 74 65 20 74 68 eed to update th
20f0a 61 74 20 73 6c 6f 74 20 77 69 74 68 20 61 6c 6c at slot with all
20f0b 0a 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 66 6f . ** the info
20f0c 72 6d 61 74 69 6f 6e 20 77 65 27 76 65 20 63 6f rmation we've co
20f0d 6c 6c 65 63 74 65 64 2e 20 20 54 68 65 20 72 6f llected. The ro
20f0e 77 69 64 20 66 6f 72 20 74 68 65 20 70 72 65 61 wid for the prea
20f0f 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 2a 2a 20 llocated. **
20f10 73 6c 6f 74 20 69 73 20 74 68 65 20 32 6e 64 20 slot is the 2nd
20f11 69 74 65 6d 20 6f 6e 20 74 68 65 20 73 74 61 63 item on the stac
20f12 6b 2e 20 20 54 68 65 20 74 6f 70 20 6f 66 20 74 k. The top of t
20f13 68 65 20 73 74 61 63 6b 20 69 73 20 74 68 65 0a he stack is the.
20f14 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 ** root page
20f15 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 for the new tab
20f16 6c 65 20 28 6f 72 20 61 20 30 20 69 66 20 74 68 le (or a 0 if th
20f17 69 73 20 69 73 20 61 20 76 69 65 77 29 2e 0a 20 is is a view)..
20f18 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 */. sqlite
20f19 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 3NestedParse(pPa
20f1a 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41 rse,. "UPDA
20f1b 54 45 20 25 51 2e 25 73 20 22 0a 20 20 20 20 20 TE %Q.%s ".
20f1c 20 20 20 20 22 53 45 54 20 74 79 70 65 3d 27 25 "SET type='%
20f1d 73 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c s', name=%Q, tbl
20f1e 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 _name=%Q, rootpa
20f1f 67 65 3d 23 25 64 2c 20 73 71 6c 3d 25 51 20 22 ge=#%d, sql=%Q "
20f20 0a 20 20 20 20 20 20 20 22 57 48 45 52 45 20 72 . "WHERE r
20f21 6f 77 69 64 3d 23 25 64 22 2c 0a 20 20 20 20 20 owid=#%d",.
20f22 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e db->aDb[iDb].zN
20f23 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c ame, SCHEMA_TABL
20f24 45 28 69 44 62 29 2c 0a 20 20 20 20 20 20 7a 54 E(iDb),. zT
20f25 79 70 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e ype,. p->zN
20f26 61 6d 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e ame,. p->zN
20f27 61 6d 65 2c 0a 20 20 20 20 20 20 70 50 61 72 73 ame,. pPars
20f28 65 2d 3e 72 65 67 52 6f 6f 74 2c 0a 20 20 20 20 e->regRoot,.
20f29 20 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 zStmt,. p
20f2a 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a Parse->regRowid.
20f2b 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 );. sqlit
20f2c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 e3DbFree(db, zSt
20f2d 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 mt);. sqlite3
20f2e 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 ChangeCookie(pPa
20f2f 72 73 65 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e rse, iDb);..#ifn
20f30 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
20f31 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 AUTOINCREMENT.
20f32 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 /* Check to se
20f33 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 e if we need to
20f34 63 72 65 61 74 65 20 61 6e 20 73 71 6c 69 74 65 create an sqlite
20f35 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 _sequence table
20f36 66 6f 72 0a 20 20 20 20 2a 2a 20 6b 65 65 70 69 for. ** keepi
20f37 6e 67 20 74 72 61 63 6b 20 6f 66 20 61 75 74 6f ng track of auto
20f38 69 6e 63 72 65 6d 65 6e 74 20 6b 65 79 73 2e 0a increment keys..
20f39 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
20f3a 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f ->tabFlags & TF_
20f3b 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b Autoincrement ){
20f3c 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62 20 3d . Db *pDb =
20f3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a &db->aDb[iDb];.
20f3e 20 20 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 if( pDb->p
20f3f 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 3d Schema->pSeqTab=
20f40 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 =0 ){. sq
20f41 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 lite3NestedParse
20f42 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 (pParse,.
20f43 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 "CREATE TABLE
20f44 20 25 51 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 %Q.sqlite_seque
20f45 6e 63 65 28 6e 61 6d 65 2c 73 65 71 29 22 2c 0a nce(name,seq)",.
20f46 20 20 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a pDb->z
20f47 4e 61 6d 65 0a 20 20 20 20 20 20 20 20 29 3b 0a Name. );.
20f48 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 }. }.#e
20f49 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 52 65 70 ndif.. /* Rep
20f4a 61 72 73 65 20 65 76 65 72 79 74 68 69 6e 67 20 arse everything
20f4b 74 6f 20 75 70 64 61 74 65 20 6f 75 72 20 69 6e to update our in
20f4c 74 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 ternal data stru
20f4d 63 74 75 72 65 73 20 2a 2f 0a 20 20 20 20 73 71 ctures */. sq
20f4e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
20f4f 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d v, OP_ParseSchem
20f50 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 0a 20 20 a, iDb, 0, 0,.
20f51 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72 sqlite3MPr
20f52 69 6e 74 66 28 64 62 2c 20 22 74 62 6c 5f 6e 61 intf(db, "tbl_na
20f53 6d 65 3d 27 25 71 27 22 2c 70 2d 3e 7a 4e 61 6d me='%q'",p->zNam
20f54 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b e), P4_DYNAMIC);
20f55 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 64 64 20 . }... /* Add
20f56 74 68 65 20 74 61 62 6c 65 20 74 6f 20 74 68 65 the table to the
20f57 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 in-memory repre
20f58 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 sentation of the
20f59 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a database.. */.
20f5a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 if( db->init.b
20f5b 75 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e usy && pParse->n
20f5c 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 54 61 Err==0 ){. Ta
20f5d 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 46 ble *pOld;. F
20f5e 4b 65 79 20 2a 70 46 4b 65 79 3b 20 0a 20 20 20 Key *pFKey; .
20f5f 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
20f60 20 3d 20 70 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 = p->pSchema;.
20f61 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 pOld = sqlite
20f62 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 3HashInsert(&pSc
20f63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 hema->tblHash, p
20f64 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 ->zName,.
20f65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20f66 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
20f67 6c 65 6e 33 30 28 70 2d 3e 7a 4e 61 6d 65 29 2b len30(p->zName)+
20f68 31 2c 70 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 1,p);. if( pO
20f69 6c 64 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 ld ){. asse
20f6a 72 74 28 20 70 3d 3d 70 4f 6c 64 20 29 3b 20 20 rt( p==pOld );
20f6b 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 /* Malloc must h
20f6c 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64 ave failed insid
20f6d 65 20 48 61 73 68 49 6e 73 65 72 74 28 29 20 2a e HashInsert() *
20f6e 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c /. db->mall
20f6f 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
20f70 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
20f71 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
20f72 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 _OMIT_FOREIGN_KE
20f73 59 0a 20 20 20 20 66 6f 72 28 70 46 4b 65 79 3d Y. for(pFKey=
20f74 70 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b p->pFKey; pFKey;
20f75 20 70 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e pFKey=pFKey->pN
20f76 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 extFrom){.
20f77 76 6f 69 64 20 2a 64 61 74 61 3b 0a 20 20 20 20 void *data;.
20f78 20 20 69 6e 74 20 6e 54 6f 20 3d 20 73 71 6c 69 int nTo = sqli
20f79 74 65 33 53 74 72 6c 65 6e 33 30 28 70 46 4b 65 te3Strlen30(pFKe
20f7a 79 2d 3e 7a 54 6f 29 20 2b 20 31 3b 0a 20 20 20 y->zTo) + 1;.
20f7b 20 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 pFKey->pNextT
20f7c 6f 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 o = sqlite3HashF
20f7d 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e 61 46 ind(&pSchema->aF
20f7e 4b 65 79 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c Key, pFKey->zTo,
20f7f 20 6e 54 6f 29 3b 0a 20 20 20 20 20 20 64 61 74 nTo);. dat
20f80 61 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 a = sqlite3HashI
20f81 6e 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e nsert(&pSchema->
20f82 61 46 4b 65 79 2c 20 70 46 4b 65 79 2d 3e 7a 54 aFKey, pFKey->zT
20f83 6f 2c 20 6e 54 6f 2c 20 70 46 4b 65 79 29 3b 0a o, nTo, pFKey);.
20f84 20 20 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d if( data==
20f85 28 76 6f 69 64 20 2a 29 70 46 4b 65 79 20 29 7b (void *)pFKey ){
20f86 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c . db->mal
20f87 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
20f88 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e }. }.#en
20f89 64 69 66 0a 20 20 20 20 70 50 61 72 73 65 2d 3e dif. pParse->
20f8a 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 pNewTable = 0;.
20f8b 20 20 20 64 62 2d 3e 6e 54 61 62 6c 65 2b 2b 3b db->nTable++;
20f8c 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c . db->flags |
20f8d 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 = SQLITE_InternC
20f8e 68 61 6e 67 65 73 3b 0a 0a 23 69 66 6e 64 65 66 hanges;..#ifndef
20f8f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 SQLITE_OMIT_ALT
20f90 45 52 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 ERTABLE. if(
20f91 21 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 !p->pSelect ){.
20f92 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
20f93 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 *zName = (const
20f94 63 68 61 72 20 2a 29 70 50 61 72 73 65 2d 3e 73 char *)pParse->s
20f95 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 NameToken.z;.
20f96 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 int nName;.
20f97 20 20 20 20 61 73 73 65 72 74 28 20 21 70 53 65 assert( !pSe
20f98 6c 65 63 74 20 26 26 20 70 43 6f 6e 73 20 26 26 lect && pCons &&
20f99 20 70 45 6e 64 20 29 3b 0a 20 20 20 20 20 20 69 pEnd );. i
20f9a 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d 3d 30 20 29 f( pCons->z==0 )
20f9b 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6e 73 20 {. pCons
20f9c 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a = pEnd;. }.
20f9d 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20 28 69 nName = (i
20f9e 6e 74 29 28 28 63 6f 6e 73 74 20 63 68 61 72 20 nt)((const char
20f9f 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d 20 7a 4e 61 *)pCons->z - zNa
20fa0 6d 65 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 me);. p->ad
20fa1 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 31 33 20 dColOffset = 13
20fa2 2b 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61 + sqlite3Utf8Cha
20fa3 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20 6e 4e 61 6d rLen(zName, nNam
20fa4 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 e);. }.#endif
20fa5 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 . }.}..#ifndef
20fa6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
20fa7 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 ./*.** The parse
20fa8 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 r calls this rou
20fa9 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f tine in order to
20faa 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 56 49 create a new VI
20fab 45 57 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 EW.*/.SQLITE_PRI
20fac 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
20fad 33 43 72 65 61 74 65 56 69 65 77 28 0a 20 20 50 3CreateView(. P
20fae 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
20faf 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 /* The parsing
20fb0 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f context */. To
20fb1 6b 65 6e 20 2a 70 42 65 67 69 6e 2c 20 20 20 20 ken *pBegin,
20fb2 20 2f 2a 20 54 68 65 20 43 52 45 41 54 45 20 74 /* The CREATE t
20fb3 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73 oken that begins
20fb4 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 2a the statement *
20fb5 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
20fb6 31 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 1, /* The to
20fb7 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 20 74 ken that holds t
20fb8 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76 he name of the v
20fb9 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a iew */. Token *
20fba 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f 2a 20 54 pName2, /* T
20fbb 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f he token that ho
20fbc 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 lds the name of
20fbd 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 53 65 the view */. Se
20fbe 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 lect *pSelect,
20fbf 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 /* A SELECT sta
20fc0 74 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c tement that will
20fc1 20 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 become the new
20fc2 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 view */. int is
20fc3 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 Temp, /*
20fc4 54 52 55 45 20 66 6f 72 20 61 20 54 45 4d 50 4f TRUE for a TEMPO
20fc5 52 41 52 59 20 76 69 65 77 20 2a 2f 0a 20 20 69 RARY view */. i
20fc6 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20 nt noErr
20fc7 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20 65 72 /* Suppress er
20fc8 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 66 20 ror messages if
20fc9 56 49 45 57 20 61 6c 72 65 61 64 79 20 65 78 69 VIEW already exi
20fca 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c sts */.){. Tabl
20fcb 65 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 e *p;. int n;.
20fcc 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
20fcd 63 68 61 72 20 2a 7a 3b 0a 20 20 54 6f 6b 65 6e char *z;. Token
20fce 20 73 45 6e 64 3b 0a 20 20 44 62 46 69 78 65 72 sEnd;. DbFixer
20fcf 20 73 46 69 78 3b 0a 20 20 54 6f 6b 65 6e 20 2a sFix;. Token *
20fd0 70 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 69 44 62 pName;. int iDb
20fd1 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
20fd2 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 = pParse->db;..
20fd3 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 if( pParse->nVa
20fd4 72 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 r>0 ){. sqlit
20fd5 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
20fd6 65 2c 20 22 70 61 72 61 6d 65 74 65 72 73 20 61 e, "parameters a
20fd7 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69 re not allowed i
20fd8 6e 20 76 69 65 77 73 22 29 3b 0a 20 20 20 20 73 n views");. s
20fd9 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
20fda 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b te(db, pSelect);
20fdb 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
20fdc 0a 20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 . sqlite3StartT
20fdd 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 4e 61 able(pParse, pNa
20fde 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 69 73 54 me1, pName2, isT
20fdf 65 6d 70 2c 20 31 2c 20 30 2c 20 6e 6f 45 72 72 emp, 1, 0, noErr
20fe0 29 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d );. p = pParse-
20fe1 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 >pNewTable;. if
20fe2 28 20 70 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 ( p==0 || pParse
20fe3 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 73 71 ->nErr ){. sq
20fe4 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 lite3SelectDelet
20fe5 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a e(db, pSelect);.
20fe6 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
20fe7 20 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 sqlite3TwoPart
20fe8 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 Name(pParse, pNa
20fe9 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e me1, pName2, &pN
20fea 61 6d 65 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 ame);. iDb = sq
20feb 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 lite3SchemaToInd
20fec 65 78 28 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d ex(db, p->pSchem
20fed 61 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 a);. if( sqlite
20fee 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 3FixInit(&sFix,
20fef 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 76 69 pParse, iDb, "vi
20ff0 65 77 22 2c 20 70 4e 61 6d 65 29 0a 20 20 20 20 ew", pName).
20ff1 26 26 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c && sqlite3FixSel
20ff2 65 63 74 28 26 73 46 69 78 2c 20 70 53 65 6c 65 ect(&sFix, pSele
20ff3 63 74 29 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c ct). ){. sql
20ff4 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 ite3SelectDelete
20ff5 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 (db, pSelect);.
20ff6 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a return;. }..
20ff7 20 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 /* Make a copy
20ff8 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 53 of the entire S
20ff9 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
20ffa 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 that defines the
20ffb 20 76 69 65 77 2e 0a 20 20 2a 2a 20 54 68 69 73 view.. ** This
20ffc 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 6c 6c 20 will force all
20ffd 74 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a the Expr.token.z
20ffe 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 64 79 values to be dy
20fff 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 61 namically. ** a
21000 6c 6c 6f 63 61 74 65 64 20 72 61 74 68 65 72 20 llocated rather
21001 74 68 61 6e 20 70 6f 69 6e 74 20 74 6f 20 74 68 than point to th
21002 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 2d e input string -
21003 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 which means tha
21004 74 0a 20 20 2a 2a 20 74 68 65 79 20 77 69 6c 6c t. ** they will
21005 20 70 65 72 73 69 73 74 20 61 66 74 65 72 20 74 persist after t
21006 68 65 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 he current sqlit
21007 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 72 e3_exec() call r
21008 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 eturns.. */. p
21009 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 ->pSelect = sqli
2100a 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c te3SelectDup(db,
2100b 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 73 71 6c pSelect);. sql
2100c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 ite3SelectDelete
2100d 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 (db, pSelect);.
2100e 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
2100f 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 ailed ){. ret
21010 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 urn;. }. if( !
21011 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b db->init.busy ){
21012 0a 20 20 20 20 73 71 6c 69 74 65 33 56 69 65 77 . sqlite3View
21013 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 GetColumnNames(p
21014 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 7d 0a 0a Parse, p);. }..
21015 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 /* Locate the
21016 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41 54 end of the CREAT
21017 45 20 56 49 45 57 20 73 74 61 74 65 6d 65 6e 74 E VIEW statement
21018 2e 20 20 4d 61 6b 65 20 73 45 6e 64 20 70 6f 69 . Make sEnd poi
21019 6e 74 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 65 nt to. ** the e
2101a 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 45 6e 64 20 nd.. */. sEnd
2101b 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 = pParse->sLastT
2101c 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 73 45 6e 64 oken;. if( sEnd
2101d 2e 7a 5b 30 5d 21 3d 30 20 26 26 20 73 45 6e 64 .z[0]!=0 && sEnd
2101e 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b 0a 20 20 .z[0]!=';' ){.
2101f 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 73 45 6e 64 sEnd.z += sEnd
21020 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e 64 2e 6e .n;. }. sEnd.n
21021 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 = 0;. n = (int
21022 29 28 73 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69 )(sEnd.z - pBegi
21023 6e 2d 3e 7a 29 3b 0a 20 20 7a 20 3d 20 28 63 6f n->z);. z = (co
21024 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
21025 72 2a 29 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 r*)pBegin->z;.
21026 77 68 69 6c 65 28 20 6e 3e 30 20 26 26 20 28 7a while( n>0 && (z
21027 5b 6e 2d 31 5d 3d 3d 27 3b 27 20 7c 7c 20 69 73 [n-1]==';' || is
21028 73 70 61 63 65 28 7a 5b 6e 2d 31 5d 29 29 20 29 space(z[n-1])) )
21029 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e { n--; }. sEnd.
2102a 7a 20 3d 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 z = &z[n-1];. s
2102b 45 6e 64 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a End.n = 1;.. /*
2102c 20 55 73 65 20 73 71 6c 69 74 65 33 45 6e 64 54 Use sqlite3EndT
2102d 61 62 6c 65 28 29 20 74 6f 20 61 64 64 20 74 68 able() to add th
2102e 65 20 76 69 65 77 20 74 6f 20 74 68 65 20 53 51 e view to the SQ
2102f 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c LITE_MASTER tabl
21030 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 6e e */. sqlite3En
21031 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 dTable(pParse, 0
21032 2c 20 26 73 45 6e 64 2c 20 30 29 3b 0a 20 20 72 , &sEnd, 0);. r
21033 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 eturn;.}.#endif
21034 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
21035 49 45 57 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 IEW */..#if !def
21036 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
21037 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e _VIEW) || !defin
21038 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
21039 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a IRTUALTABLE)./*.
2103a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74 72 ** The Table str
2103b 75 63 74 75 72 65 20 70 54 61 62 6c 65 20 69 73 ucture pTable is
2103c 20 72 65 61 6c 6c 79 20 61 20 56 49 45 57 2e 20 really a VIEW.
2103d 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 6e 61 6d Fill in the nam
2103e 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c es of.** the col
2103f 75 6d 6e 73 20 6f 66 20 74 68 65 20 76 69 65 77 umns of the view
21040 20 69 6e 20 74 68 65 20 70 54 61 62 6c 65 20 73 in the pTable s
21041 74 72 75 63 74 75 72 65 2e 20 20 52 65 74 75 72 tructure. Retur
21042 6e 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 n the number.**
21043 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20 61 of errors. If a
21044 6e 20 65 72 72 6f 72 20 69 73 20 73 65 65 6e 20 n error is seen
21045 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d leave an error m
21046 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 essage in pParse
21047 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 ->zErrMsg..*/.SQ
21048 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
21049 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 sqlite3ViewGetC
2104a 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 olumnNames(Parse
2104b 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 *pParse, Table
2104c 2a 70 54 61 62 6c 65 29 7b 0a 20 20 54 61 62 6c *pTable){. Tabl
2104d 65 20 2a 70 53 65 6c 54 61 62 3b 20 20 20 2f 2a e *pSelTab; /*
2104e 20 41 20 66 61 6b 65 20 74 61 62 6c 65 20 66 72 A fake table fr
2104f 6f 6d 20 77 68 69 63 68 20 77 65 20 67 65 74 20 om which we get
21050 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a the result set *
21051 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c /. Select *pSel
21052 3b 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 ; /* Copy of
21053 20 74 68 65 20 53 45 4c 45 43 54 20 74 68 61 74 the SELECT that
21054 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
21055 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 view */. int nE
21056 72 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e rr = 0; /* N
21057 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 umber of errors
21058 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20 encountered */.
21059 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 int n;
2105a 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 69 6c /* Temporaril
2105b 79 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 62 y holds the numb
2105c 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 61 73 er of cursors as
2105d 73 69 67 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 signed */. sqli
2105e 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
2105f 2d 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 ->db; /* Databa
21060 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f se connection fo
21061 72 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 r malloc errors
21062 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 68 */. int (*xAuth
21063 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 )(void*,int,cons
21064 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 t char*,const ch
21065 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c ar*,const char*,
21066 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 0a 20 const char*);..
21067 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 20 assert( pTable
21068 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
21069 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
2106a 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74 ABLE. if( sqlit
2106b 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 e3VtabCallConnec
2106c 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65 t(pParse, pTable
2106d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
2106e 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
2106f 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 }. if( IsVirtua
21070 6c 28 70 54 61 62 6c 65 29 20 29 20 72 65 74 75 l(pTable) ) retu
21071 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 rn 0;.#endif..#i
21072 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
21073 54 5f 56 49 45 57 0a 20 20 2f 2a 20 41 20 70 6f T_VIEW. /* A po
21074 73 69 74 69 76 65 20 6e 43 6f 6c 20 6d 65 61 6e sitive nCol mean
21075 73 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 s the columns na
21076 6d 65 73 20 66 6f 72 20 74 68 69 73 20 76 69 65 mes for this vie
21077 77 20 61 72 65 0a 20 20 2a 2a 20 61 6c 72 65 61 w are. ** alrea
21078 64 79 20 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 dy known.. */.
21079 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f if( pTable->nCo
2107a 6c 3e 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a l>0 ) return 0;.
2107b 0a 20 20 2f 2a 20 41 20 6e 65 67 61 74 69 76 65 . /* A negative
2107c 20 6e 43 6f 6c 20 69 73 20 61 20 73 70 65 63 69 nCol is a speci
2107d 61 6c 20 6d 61 72 6b 65 72 20 6d 65 61 6e 69 6e al marker meanin
2107e 67 20 74 68 61 74 20 77 65 20 61 72 65 20 63 75 g that we are cu
2107f 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 74 72 79 rrently. ** try
21080 69 6e 67 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 ing to compute t
21081 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e he column names.
21082 20 20 49 66 20 77 65 20 65 6e 74 65 72 20 74 68 If we enter th
21083 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 0a is routine with.
21084 20 20 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20 ** a negative
21085 6e 43 6f 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 nCol, it means t
21086 77 6f 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 wo or more views
21087 20 66 6f 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 form a loop, li
21088 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 ke this:. **.
21089 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 56 49 ** CREATE VI
2108a 45 57 20 6f 6e 65 20 41 53 20 53 45 4c 45 43 54 EW one AS SELECT
2108b 20 2a 20 46 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a * FROM two;. *
2108c 2a 20 20 20 20 20 43 52 45 41 54 45 20 56 49 45 * CREATE VIE
2108d 57 20 74 77 6f 20 41 53 20 53 45 4c 45 43 54 20 W two AS SELECT
2108e 2a 20 46 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a * FROM one;. **
2108f 0a 20 20 2a 2a 20 41 63 74 75 61 6c 6c 79 2c 20 . ** Actually,
21090 74 68 69 73 20 65 72 72 6f 72 20 69 73 20 63 61 this error is ca
21091 75 67 68 74 20 70 72 65 76 69 6f 75 73 6c 79 20 ught previously
21092 61 6e 64 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f and so the follo
21093 77 69 6e 67 20 74 65 73 74 0a 20 20 2a 2a 20 73 wing test. ** s
21094 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 66 61 69 hould always fai
21095 6c 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 l. But we will
21096 6c 65 61 76 65 20 69 74 20 69 6e 20 70 6c 61 63 leave it in plac
21097 65 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61 66 e just to be saf
21098 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 e.. */. if( pT
21099 61 62 6c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a able->nCol<0 ){.
2109a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
2109b 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 65 Msg(pParse, "vie
2109c 77 20 25 73 20 69 73 20 63 69 72 63 75 6c 61 72 w %s is circular
2109d 6c 79 20 64 65 66 69 6e 65 64 22 2c 20 70 54 61 ly defined", pTa
2109e 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 ble->zName);.
2109f 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
210a0 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d assert( pTable-
210a1 3e 6e 43 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f >nCol>=0 );.. /
210a2 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69 73 * If we get this
210a3 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 77 far, it means w
210a4 65 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 75 74 e need to comput
210a5 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 e the table name
210a6 73 2e 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 s.. ** Note tha
210a7 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 t the call to sq
210a8 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 lite3ResultSetOf
210a9 53 65 6c 65 63 74 28 29 20 77 69 6c 6c 20 65 78 Select() will ex
210aa 70 61 6e 64 20 61 6e 79 0a 20 20 2a 2a 20 22 2a pand any. ** "*
210ab 22 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 " elements in th
210ac 65 20 72 65 73 75 6c 74 73 20 73 65 74 20 6f 66 e results set of
210ad 20 74 68 65 20 76 69 65 77 20 61 6e 64 20 77 69 the view and wi
210ae 6c 6c 20 61 73 73 69 67 6e 20 63 75 72 73 6f 72 ll assign cursor
210af 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 65 6c s. ** to the el
210b0 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 46 52 ements of the FR
210b1 4f 4d 20 63 6c 61 75 73 65 2e 20 20 42 75 74 20 OM clause. But
210b2 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 we do not want t
210b3 68 65 73 65 20 63 68 61 6e 67 65 73 0a 20 20 2a hese changes. *
210b4 2a 20 74 6f 20 62 65 20 70 65 72 6d 61 6e 65 6e * to be permanen
210b5 74 2e 20 20 53 6f 20 74 68 65 20 63 6f 6d 70 75 t. So the compu
210b6 74 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 6f tation is done o
210b7 6e 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 n a copy of the
210b8 53 45 4c 45 43 54 0a 20 20 2a 2a 20 73 74 61 74 SELECT. ** stat
210b9 65 6d 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e ement that defin
210ba 65 73 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a es the view.. *
210bb 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 /. assert( pTab
210bc 6c 65 2d 3e 70 53 65 6c 65 63 74 20 29 3b 0a 20 le->pSelect );.
210bd 20 70 53 65 6c 20 3d 20 73 71 6c 69 74 65 33 53 pSel = sqlite3S
210be 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54 61 electDup(db, pTa
210bf 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 ble->pSelect);.
210c0 20 69 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 if( pSel ){.
210c1 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 n = pParse->nTa
210c2 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 b;. sqlite3Sr
210c3 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f cListAssignCurso
210c4 72 73 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d rs(pParse, pSel-
210c5 3e 70 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62 >pSrc);. pTab
210c6 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 23 le->nCol = -1;.#
210c7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
210c8 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e IT_AUTHORIZATION
210c9 0a 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d . xAuth = db-
210ca 3e 78 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e >xAuth;. db->
210cb 78 41 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 xAuth = 0;. p
210cc 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 SelTab = sqlite3
210cd 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 ResultSetOfSelec
210ce 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b t(pParse, pSel);
210cf 0a 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d . db->xAuth =
210d0 20 78 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 xAuth;.#else.
210d1 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 pSelTab = sqli
210d2 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 te3ResultSetOfSe
210d3 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 lect(pParse, pSe
210d4 6c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 l);.#endif. p
210d5 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b Parse->nTab = n;
210d6 0a 20 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 . if( pSelTab
210d7 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
210d8 28 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d ( pTable->aCol==
210d9 30 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 0 );. pTabl
210da 65 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 e->nCol = pSelTa
210db 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 b->nCol;. p
210dc 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 Table->aCol = pS
210dd 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 elTab->aCol;.
210de 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c pSelTab->nCol
210df 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 53 65 6c = 0;. pSel
210e0 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 Tab->aCol = 0;.
210e1 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 sqlite3Dele
210e2 74 65 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29 teTable(pSelTab)
210e3 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e ;. pTable->
210e4 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c pSchema->flags |
210e5 3d 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 = DB_UnresetView
210e6 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 s;. }else{.
210e7 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c pTable->nCol
210e8 20 3d 20 30 3b 0a 20 20 20 20 20 20 6e 45 72 72 = 0;. nErr
210e9 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 ++;. }. sq
210ea 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 lite3SelectDelet
210eb 65 28 64 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d e(db, pSel);. }
210ec 20 65 6c 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 else {. nErr
210ed 2b 2b 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f ++;. }.#endif /
210ee 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
210ef 45 57 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e EW */. return n
210f0 45 72 72 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 Err; .}.#endif
210f1 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 /* !defined(SQLI
210f2 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c TE_OMIT_VIEW) ||
210f3 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
210f4 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
210f5 4c 45 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 LE) */..#ifndef
210f6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
210f7 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 ./*.** Clear the
210f8 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 column names fr
210f9 6f 6d 20 65 76 65 72 79 20 56 49 45 57 20 69 6e om every VIEW in
210fa 20 64 61 74 61 62 61 73 65 20 69 64 78 2e 0a 2a database idx..*
210fb 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 /.static void sq
210fc 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c liteViewResetAll
210fd 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e (sqlite3 *db, in
210fe 74 20 69 64 78 29 7b 0a 20 20 48 61 73 68 45 6c t idx){. HashEl
210ff 65 6d 20 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 em *i;. if( !Db
21100 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 HasProperty(db,
21101 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 idx, DB_UnresetV
21102 69 65 77 73 29 20 29 20 72 65 74 75 72 6e 3b 0a iews) ) return;.
21103 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 for(i=sqliteHa
21104 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 44 62 shFirst(&db->aDb
21105 5b 69 64 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 [idx].pSchema->t
21106 62 6c 48 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c blHash); i;i=sql
21107 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b iteHashNext(i)){
21108 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 . Table *pTab
21109 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 = sqliteHashDat
2110a 61 28 69 29 3b 0a 20 20 20 20 69 66 28 20 70 54 a(i);. if( pT
2110b 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 ab->pSelect ){.
2110c 20 20 20 20 20 73 71 6c 69 74 65 52 65 73 65 74 sqliteReset
2110d 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 ColumnNames(pTab
2110e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 );. }. }. D
2110f 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64 bClearProperty(d
21110 62 2c 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 b, idx, DB_Unres
21111 65 74 56 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 etViews);.}.#els
21112 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 e.# define sqlit
21113 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 41 2c eViewResetAll(A,
21114 42 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c B).#endif /* SQL
21115 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f ITE_OMIT_VIEW */
21116 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
21117 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
21118 62 79 20 74 68 65 20 56 44 42 45 20 74 6f 20 61 by the VDBE to a
21119 64 6a 75 73 74 20 74 68 65 20 69 6e 74 65 72 6e djust the intern
2111a 61 6c 20 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 al schema.** use
2111b 64 20 62 79 20 53 51 4c 69 74 65 20 77 68 65 6e d by SQLite when
2111c 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 the btree layer
2111d 20 6d 6f 76 65 73 20 61 20 74 61 62 6c 65 20 72 moves a table r
2111e 6f 6f 74 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a oot page. The.**
2111f 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 root-page of a
21120 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 table or index i
21121 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 20 68 n database iDb h
21122 61 73 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 as changed from
21123 69 46 72 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e iFrom.** to iTo.
21124 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 .**.** Ticket #1
21125 37 32 38 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 728: The symbol
21126 20 74 61 62 6c 65 20 6d 69 67 68 74 20 73 74 69 table might sti
21127 6c 6c 20 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 ll contain infor
21128 6d 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 mation.** on tab
21129 6c 65 73 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 les and/or indic
2112a 65 73 20 74 68 61 74 20 61 72 65 20 74 68 65 20 es that are the
2112b 70 72 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 process of being
2112c 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 deleted..** If
2112d 79 6f 75 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c you are unlucky,
2112e 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 64 65 one of those de
2112f 6c 65 74 65 64 20 69 6e 64 69 63 65 73 20 6f 72 leted indices or
21130 20 74 61 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a tables might.**
21131 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 72 have the same r
21132 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72 20 61 ootpage number a
21133 73 20 74 68 65 20 72 65 61 6c 20 74 61 62 6c 65 s the real table
21134 20 6f 72 20 69 6e 64 65 78 20 74 68 61 74 20 69 or index that i
21135 73 0a 2a 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 s.** being moved
21136 2e 20 20 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 . So we cannot
21137 73 74 6f 70 20 73 65 61 72 63 68 69 6e 67 20 61 stop searching a
21138 66 74 65 72 20 74 68 65 20 66 69 72 73 74 20 6d fter the first m
21139 61 74 63 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 atch .** because
2113a 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 the first match
2113b 20 6d 69 67 68 74 20 62 65 20 66 6f 72 20 6f 6e might be for on
2113c 65 20 6f 66 20 74 68 65 20 64 65 6c 65 74 65 64 e of the deleted
2113d 20 69 6e 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 indices.** or t
2113e 61 62 6c 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 ables and not th
2113f 65 20 74 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 e table/index th
21140 61 74 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 at is actually b
21141 65 69 6e 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 eing moved..** W
21142 65 20 6d 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 e must continue
21143 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c looping until al
21144 6c 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 l tables and ind
21145 69 63 65 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f ices with.** roo
21146 74 70 61 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 tpage==iFrom hav
21147 65 20 62 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 e been converted
21148 20 74 6f 20 68 61 76 65 20 61 20 72 6f 6f 74 70 to have a rootp
21149 61 67 65 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e age of iTo.** in
2114a 20 6f 72 64 65 72 20 74 6f 20 62 65 20 63 65 72 order to be cer
2114b 74 61 69 6e 20 74 68 61 74 20 77 65 20 67 6f 74 tain that we got
2114c 20 74 68 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a the right one..
2114d 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
2114e 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
2114f 4d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 M.SQLITE_PRIVATE
21150 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f void sqlite3Roo
21151 74 50 61 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 tPageMoved(Db *p
21152 44 62 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 Db, int iFrom, i
21153 6e 74 20 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 nt iTo){. HashE
21154 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 lem *pElem;. Ha
21155 73 68 20 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 sh *pHash;.. pH
21156 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 ash = &pDb->pSch
21157 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 ema->tblHash;.
21158 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 for(pElem=sqlite
21159 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29 HashFirst(pHash)
2115a 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 ; pElem; pElem=s
2115b 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 qliteHashNext(pE
2115c 6c 65 6d 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 lem)){. Table
2115d 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 *pTab = sqliteH
2115e 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a ashData(pElem);.
2115f 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 74 6e if( pTab->tn
21160 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 um==iFrom ){.
21161 20 20 20 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 pTab->tnum =
21162 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 iTo;. }. }.
21163 20 70 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 pHash = &pDb->p
21164 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b Schema->idxHash;
21165 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c . for(pElem=sql
21166 69 74 65 48 61 73 68 46 69 72 73 74 28 70 48 61 iteHashFirst(pHa
21167 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 sh); pElem; pEle
21168 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 m=sqliteHashNext
21169 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e (pElem)){. In
2116a 64 65 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 dex *pIdx = sqli
2116b 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d teHashData(pElem
2116c 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 2d );. if( pIdx-
2116d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a >tnum==iFrom ){.
2116e 20 20 20 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d pIdx->tnum
2116f 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 = iTo;. }.
21170 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a }.}.#endif../*.*
21171 2a 20 57 72 69 74 65 20 63 6f 64 65 20 74 6f 20 * Write code to
21172 65 72 61 73 65 20 74 68 65 20 74 61 62 6c 65 20 erase the table
21173 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 with root-page i
21174 54 61 62 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 Table from datab
21175 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f ase iDb..** Also
21176 20 77 72 69 74 65 20 63 6f 64 65 20 74 6f 20 6d write code to m
21177 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 74 65 odify the sqlite
21178 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e _master table an
21179 64 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d d internal schem
2117a 61 0a 2a 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 a.** if a root-p
2117b 61 67 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 age of another t
2117c 61 62 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79 able is moved by
2117d 20 74 68 65 20 62 74 72 65 65 2d 6c 61 79 65 72 the btree-layer
2117e 20 77 68 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 whilst.** erasi
2117f 6e 67 20 69 54 61 62 6c 65 20 28 74 68 69 73 20 ng iTable (this
21180 63 61 6e 20 68 61 70 70 65 6e 20 77 69 74 68 20 can happen with
21181 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 an auto-vacuum d
21182 61 74 61 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 atabase)..*/ .st
21183 61 74 69 63 20 76 6f 69 64 20 64 65 73 74 72 6f atic void destro
21184 79 52 6f 6f 74 50 61 67 65 28 50 61 72 73 65 20 yRootPage(Parse
21185 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 54 61 *pParse, int iTa
21186 62 6c 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 ble, int iDb){.
21187 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 Vdbe *v = sqlit
21188 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
21189 29 3b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 );. int r1 = sq
2118a 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 lite3GetTempReg(
2118b 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 pParse);. sqlit
2118c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
2118d 4f 50 5f 44 65 73 74 72 6f 79 2c 20 69 54 61 62 OP_Destroy, iTab
2118e 6c 65 2c 20 72 31 2c 20 69 44 62 29 3b 0a 23 69 le, r1, iDb);.#i
2118f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
21190 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f T_AUTOVACUUM. /
21191 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 73 74 6f * OP_Destroy sto
21192 72 65 73 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 res an in intege
21193 72 20 72 31 2e 20 49 66 20 74 68 69 73 20 69 6e r r1. If this in
21194 74 65 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f teger. ** is no
21195 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 n-zero, then it
21196 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 is the root page
21197 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 74 61 62 number of a tab
21198 6c 65 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a le moved to. **
21199 20 6c 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 location iTable
2119a 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 . The following
2119b 63 6f 64 65 20 6d 6f 64 69 66 69 65 73 20 74 68 code modifies th
2119c 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 e sqlite_master
2119d 74 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 table to. ** re
2119e 66 6c 65 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a flect this.. **
2119f 0a 20 20 2a 2a 20 54 68 65 20 22 23 25 64 22 20 . ** The "#%d"
211a0 69 6e 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 in the SQL is a
211a1 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 special constant
211a2 20 74 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74 that means what
211a3 65 76 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 ever value. **
211a4 69 73 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 is on the top of
211a5 20 74 68 65 20 73 74 61 63 6b 2e 20 20 53 65 65 the stack. See
211a6 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 sqlite3Register
211a7 45 78 70 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 73 Expr().. */. s
211a8 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 qlite3NestedPars
211a9 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 e(pParse, .
211aa 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 "UPDATE %Q.%s SE
211ab 54 20 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 T rootpage=%d WH
211ac 45 52 45 20 23 25 64 20 41 4e 44 20 72 6f 6f 74 ERE #%d AND root
211ad 70 61 67 65 3d 23 25 64 22 2c 0a 20 20 20 20 20 page=#%d",.
211ae 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b pParse->db->aDb[
211af 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 iDb].zName, SCHE
211b0 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 69 MA_TABLE(iDb), i
211b1 54 61 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a Table, r1, r1);.
211b2 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 #endif. sqlite3
211b3 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
211b4 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f Parse, r1);.}../
211b5 2a 0a 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20 *.** Write VDBE
211b6 63 6f 64 65 20 74 6f 20 65 72 61 73 65 20 74 61 code to erase ta
211b7 62 6c 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c ble pTab and all
211b8 20 61 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69 associated indi
211b9 63 65 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 ces on disk..**
211ba 43 6f 64 65 20 74 6f 20 75 70 64 61 74 65 20 74 Code to update t
211bb 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 he sqlite_master
211bc 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65 tables and inte
211bd 72 6e 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69 rnal schema defi
211be 6e 69 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 nitions.** in ca
211bf 73 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62 se a root-page b
211c0 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74 elonging to anot
211c1 68 65 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 her table is mov
211c2 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20 ed by the btree
211c3 6c 61 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f layer.** is also
211c4 20 61 64 64 65 64 20 28 74 68 69 73 20 63 61 6e added (this can
211c5 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 happen with an
211c6 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 auto-vacuum data
211c7 62 61 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 base)..*/.static
211c8 20 76 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62 void destroyTab
211c9 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 le(Parse *pParse
211ca 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a , Table *pTab){.
211cb 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
211cc 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
211cd 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 Index *pIdx;. i
211ce 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 nt iDb = sqlite3
211cf 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 SchemaToIndex(pP
211d0 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e arse->db, pTab->
211d1 70 53 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74 pSchema);. dest
211d2 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 royRootPage(pPar
211d3 73 65 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 se, pTab->tnum,
211d4 69 44 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 iDb);. for(pIdx
211d5 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 =pTab->pIndex; p
211d6 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
211d7 70 4e 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74 pNext){. dest
211d8 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 royRootPage(pPar
211d9 73 65 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 se, pIdx->tnum,
211da 69 44 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a iDb);. }.#else.
211db 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 /* If the data
211dc 62 61 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f base may be auto
211dd 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 -vacuum capable
211de 28 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f (if SQLITE_OMIT_
211df 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20 AUTOVACUUM. **
211e0 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c is not defined),
211e1 20 74 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f then it is impo
211e2 72 74 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50 rtant to call OP
211e3 5f 44 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a _Destroy on the.
211e4 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 ** table and i
211e5 6e 64 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20 ndex root-pages
211e6 69 6e 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69 in order, starti
211e7 6e 67 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65 ng with the nume
211e8 72 69 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 rically . ** la
211e9 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 rgest root-page
211ea 6e 75 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61 number. This gua
211eb 72 61 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e rantees that non
211ec 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 e of the root-pa
211ed 67 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64 ges. ** to be d
211ee 65 73 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f estroyed is relo
211ef 63 61 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c cated by an earl
211f0 69 65 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20 ier OP_Destroy.
211f1 69 2e 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a i.e. if the. **
211f2 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 following were
211f3 63 6f 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a coded:. **. **
211f4 20 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a OP_Destroy 4 0.
211f5 20 20 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 ** .... ** OP
211f6 5f 44 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a _Destroy 5 0. *
211f7 2a 0a 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 *. ** and root
211f8 70 61 67 65 20 35 20 68 61 70 70 65 6e 65 64 20 page 5 happened
211f9 74 6f 20 62 65 20 74 68 65 20 6c 61 72 67 65 73 to be the larges
211fa 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 t root-page numb
211fb 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 er in the. ** d
211fc 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f atabase, then ro
211fd 6f 74 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20 ot page 5 would
211fe 62 65 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65 be moved to page
211ff 20 34 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 4 by the . **
21200 22 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22 "OP_Destroy 4 0"
21201 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62 opcode. The sub
21202 73 65 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74 sequent "OP_Dest
21203 72 6f 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68 roy 5 0" would h
21204 69 74 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c it. ** a free-l
21205 69 73 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 ist page.. */.
21206 20 69 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62 int iTab = pTab
21207 2d 3e 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44 ->tnum;. int iD
21208 65 73 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 estroyed = 0;..
21209 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 while( 1 ){.
2120a 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 Index *pIdx;.
2120b 20 20 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d int iLargest =
2120c 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65 0;.. if( iDe
2120d 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 stroyed==0 || iT
2120e 61 62 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b ab<iDestroyed ){
2120f 0a 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 . iLargest
21210 3d 20 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 = iTab;. }.
21211 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d for(pIdx=pTab-
21212 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 >pIndex; pIdx; p
21213 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 Idx=pIdx->pNext)
21214 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 {. int iIdx
21215 20 3d 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 = pIdx->tnum;.
21216 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 assert( pId
21217 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 x->pSchema==pTab
21218 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 ->pSchema );.
21219 20 20 20 69 66 28 20 28 69 44 65 73 74 72 6f 79 if( (iDestroy
2121a 65 64 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69 ed==0 || (iIdx<i
2121b 44 65 73 74 72 6f 79 65 64 29 29 20 26 26 20 69 Destroyed)) && i
2121c 49 64 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a Idx>iLargest ){.
2121d 20 20 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 iLargest
2121e 20 3d 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d = iIdx;. }
2121f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 . }. if( i
21220 4c 61 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20 Largest==0 ){.
21221 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
21222 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 }else{. int
21223 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
21224 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 hemaToIndex(pPar
21225 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 se->db, pTab->pS
21226 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 64 65 chema);. de
21227 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 stroyRootPage(pP
21228 61 72 73 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 arse, iLargest,
21229 69 44 62 29 3b 0a 20 20 20 20 20 20 69 44 65 73 iDb);. iDes
2122a 74 72 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73 troyed = iLarges
2122b 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e t;. }. }.#en
2122c 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 dif.}../*.** Thi
2122d 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
2122e 6c 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f led to do the wo
2122f 72 6b 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42 rk of a DROP TAB
21230 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a LE statement..**
21231 20 70 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 pName is the na
21232 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
21233 74 6f 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a to be dropped..*
21234 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21235 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f void sqlite3Dro
21236 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 pTable(Parse *pP
21237 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 arse, SrcList *p
21238 4e 61 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77 Name, int isView
21239 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 , int noErr){.
2123a 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 Table *pTab;. V
2123b 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 dbe *v;. sqlite
2123c 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
2123d 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a db;. int iDb;..
2123e 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 if( pParse->nE
2123f 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 rr || db->malloc
21240 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f Failed ){. go
21241 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 to exit_drop_tab
21242 6c 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 le;. }. assert
21243 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 ( pName->nSrc==1
21244 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c );. pTab = sql
21245 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 ite3LocateTable(
21246 70 50 61 72 73 65 2c 20 69 73 56 69 65 77 2c 20 pParse, isView,
21247 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
21248 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 61 pNa
21249 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 me->a[0].zName,
2124a 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 pName->a[0].zDat
2124b 61 62 61 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 abase);.. if( p
2124c 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 Tab==0 ){. if
2124d 28 20 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 ( noErr ){.
2124e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 sqlite3ErrorCle
2124f 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 ar(pParse);.
21250 7d 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f }. goto exit_
21251 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a drop_table;. }.
21252 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
21253 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
21254 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b pTab->pSchema);
21255 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d . assert( iDb>=
21256 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 0 && iDb<db->nDb
21257 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 );.. /* If pTa
21258 62 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 b is a virtual t
21259 61 62 6c 65 2c 20 63 61 6c 6c 20 56 69 65 77 47 able, call ViewG
2125a 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 etColumnNames()
2125b 74 6f 20 65 6e 73 75 72 65 0a 20 20 2a 2a 20 69 to ensure. ** i
2125c 74 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 t is initialized
2125d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 .. */. if( IsV
2125e 69 72 74 75 61 6c 28 70 54 61 62 29 20 26 26 20 irtual(pTab) &&
2125f 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f sqlite3ViewGetCo
21260 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 lumnNames(pParse
21261 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 , pTab) ){. g
21262 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 oto exit_drop_ta
21263 62 6c 65 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 ble;. }.#ifndef
21264 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
21265 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 HORIZATION. {.
21266 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20 int code;.
21267 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 const char *zTa
21268 62 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 b = SCHEMA_TABLE
21269 28 69 44 62 29 3b 0a 20 20 20 20 63 6f 6e 73 74 (iDb);. const
2126a 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d char *zDb = db-
2126b 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b >aDb[iDb].zName;
2126c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
2126d 2a 7a 41 72 67 32 20 3d 20 30 3b 0a 20 20 20 20 *zArg2 = 0;.
2126e 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 if( sqlite3AuthC
2126f 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c heck(pParse, SQL
21270 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 ITE_DELETE, zTab
21271 2c 20 30 2c 20 7a 44 62 29 29 7b 0a 20 20 20 20 , 0, zDb)){.
21272 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 goto exit_drop
21273 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 _table;. }.
21274 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a if( isView ){.
21275 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f if( !OMIT_
21276 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 TEMPDB && iDb==1
21277 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 ){. code
21278 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 = SQLITE_DROP_T
21279 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 EMP_VIEW;.
2127a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 }else{. c
2127b 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f ode = SQLITE_DRO
2127c 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a P_VIEW;. }.
2127d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
2127e 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
2127f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 49 . }else if( I
21280 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 sVirtual(pTab) )
21281 7b 0a 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 {. code = S
21282 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c QLITE_DROP_VTABL
21283 45 3b 0a 20 20 20 20 20 20 7a 41 72 67 32 20 3d E;. zArg2 =
21284 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 pTab->pMod->zNa
21285 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d me;.#endif. }
21286 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 else{. if(
21287 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 !OMIT_TEMPDB &&
21288 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 iDb==1 ){.
21289 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f code = SQLITE_
2128a 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b DROP_TEMP_TABLE;
2128b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
2128c 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c code = SQL
2128d 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a ITE_DROP_TABLE;.
2128e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
2128f 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 if( sqlite3Aut
21290 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 hCheck(pParse, c
21291 6f 64 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 ode, pTab->zName
21292 2c 20 7a 41 72 67 32 2c 20 7a 44 62 29 20 29 7b , zArg2, zDb) ){
21293 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 . goto exit
21294 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 _drop_table;.
21295 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }. if( sqlit
21296 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
21297 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 se, SQLITE_DELET
21298 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 E, pTab->zName,
21299 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 0, zDb) ){.
2129a 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f goto exit_drop_
2129b 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d table;. }. }
2129c 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 73 71 .#endif. if( sq
2129d 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 lite3StrNICmp(pT
2129e 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 ab->zName, "sqli
2129f 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 te_", 7)==0 ){.
212a0 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
212a1 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c sg(pParse, "tabl
212a2 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 e %s may not be
212a3 64 72 6f 70 70 65 64 22 2c 20 70 54 61 62 2d 3e dropped", pTab->
212a4 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f zName);. goto
212a5 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 exit_drop_table
212a6 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 ;. }..#ifndef S
212a7 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a QLITE_OMIT_VIEW.
212a8 20 20 2f 2a 20 45 6e 73 75 72 65 20 44 52 4f 50 /* Ensure DROP
212a9 20 54 41 42 4c 45 20 69 73 20 6e 6f 74 20 75 73 TABLE is not us
212aa 65 64 20 6f 6e 20 61 20 76 69 65 77 2c 20 61 6e ed on a view, an
212ab 64 20 44 52 4f 50 20 56 49 45 57 20 69 73 20 6e d DROP VIEW is n
212ac 6f 74 20 75 73 65 64 0a 20 20 2a 2a 20 6f 6e 20 ot used. ** on
212ad 61 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 a table.. */.
212ae 69 66 28 20 69 73 56 69 65 77 20 26 26 20 70 54 if( isView && pT
212af 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 ab->pSelect==0 )
212b0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
212b1 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 orMsg(pParse, "u
212b2 73 65 20 44 52 4f 50 20 54 41 42 4c 45 20 74 6f se DROP TABLE to
212b3 20 64 65 6c 65 74 65 20 74 61 62 6c 65 20 25 73 delete table %s
212b4 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b ", pTab->zName);
212b5 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 . goto exit_d
212b6 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 rop_table;. }.
212b7 20 69 66 28 20 21 69 73 56 69 65 77 20 26 26 20 if( !isView &&
212b8 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b pTab->pSelect ){
212b9 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
212ba 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 73 rMsg(pParse, "us
212bb 65 20 44 52 4f 50 20 56 49 45 57 20 74 6f 20 64 e DROP VIEW to d
212bc 65 6c 65 74 65 20 76 69 65 77 20 25 73 22 2c 20 elete view %s",
212bd 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pTab->zName);.
212be 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 goto exit_drop
212bf 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 _table;. }.#end
212c0 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 if.. /* Generat
212c1 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 e code to remove
212c2 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 the table from
212c3 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 the master table
212c4 0a 20 20 2a 2a 20 6f 6e 20 64 69 73 6b 2e 0a 20 . ** on disk..
212c5 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 */. v = sqlite
212c6 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
212c7 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 ;. if( v ){.
212c8 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 Trigger *pTrigg
212c9 65 72 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 er;. Db *pDb
212ca 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b = &db->aDb[iDb];
212cb 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 . sqlite3Begi
212cc 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 nWriteOperation(
212cd 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b pParse, 1, iDb);
212ce 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
212cf 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
212d0 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 LE. if( IsVir
212d1 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 tual(pTab) ){.
212d2 20 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 if( v ){.
212d3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
212d4 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42 65 AddOp0(v, OP_VBe
212d5 67 69 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 gin);. }.
212d6 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
212d7 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 74 72 69 67 /* Drop all trig
212d8 67 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 gers associated
212d9 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 62 with the table b
212da 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 43 6f eing dropped. Co
212db 64 65 0a 20 20 20 20 2a 2a 20 69 73 20 67 65 6e de. ** is gen
212dc 65 72 61 74 65 64 20 74 6f 20 72 65 6d 6f 76 65 erated to remove
212dd 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 73 71 entries from sq
212de 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 2f lite_master and/
212df 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 or. ** sqlite
212e0 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 69 66 20 _temp_master if
212e1 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f required.. */
212e2 0a 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 . pTrigger =
212e3 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 0a pTab->pTrigger;.
212e4 20 20 20 20 77 68 69 6c 65 28 20 70 54 72 69 67 while( pTrig
212e5 67 65 72 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ger ){. ass
212e6 65 72 74 28 20 70 54 72 69 67 67 65 72 2d 3e 70 ert( pTrigger->p
212e7 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 Schema==pTab->pS
212e8 63 68 65 6d 61 20 7c 7c 20 0a 20 20 20 20 20 20 chema || .
212e9 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e 70 53 pTrigger->pS
212ea 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 chema==db->aDb[1
212eb 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 ].pSchema );.
212ec 20 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 sqlite3DropTr
212ed 69 67 67 65 72 50 74 72 28 70 50 61 72 73 65 2c iggerPtr(pParse,
212ee 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 pTrigger);.
212ef 20 20 70 54 72 69 67 67 65 72 20 3d 20 70 54 72 pTrigger = pTr
212f0 69 67 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 igger->pNext;.
212f1 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c }..#ifndef SQL
212f2 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
212f3 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 52 65 REMENT. /* Re
212f4 6d 6f 76 65 20 61 6e 79 20 65 6e 74 72 69 65 73 move any entries
212f5 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 of the sqlite_s
212f6 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 61 73 equence table as
212f7 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 20 20 sociated with.
212f8 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 62 ** the table b
212f9 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 54 68 eing dropped. Th
212fa 69 73 20 69 73 20 64 6f 6e 65 20 62 65 66 6f 72 is is done befor
212fb 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 64 e the table is d
212fc 72 6f 70 70 65 64 0a 20 20 20 20 2a 2a 20 61 74 ropped. ** at
212fd 20 74 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c the btree level
212fe 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 71 , in case the sq
212ff 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 lite_sequence ta
21300 62 6c 65 20 6e 65 65 64 73 20 74 6f 0a 20 20 20 ble needs to.
21301 20 2a 2a 20 6d 6f 76 65 20 61 73 20 61 20 72 65 ** move as a re
21302 73 75 6c 74 20 6f 66 20 74 68 65 20 64 72 6f 70 sult of the drop
21303 20 28 63 61 6e 20 68 61 70 70 65 6e 20 69 6e 20 (can happen in
21304 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 auto-vacuum mode
21305 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 ).. */. if
21306 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 ( pTab->tabFlags
21307 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d & TF_Autoincrem
21308 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ent ){. sql
21309 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
2130a 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 pParse,.
2130b 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 73 2e "DELETE FROM %s.
2130c 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 sqlite_sequence
2130d 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a WHERE name=%Q",.
2130e 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 pDb->zNa
2130f 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a me, pTab->zName.
21310 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 23 );. }.#
21311 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 72 endif.. /* Dr
21312 6f 70 20 61 6c 6c 20 53 51 4c 49 54 45 5f 4d 41 op all SQLITE_MA
21313 53 54 45 52 20 74 61 62 6c 65 20 61 6e 64 20 69 STER table and i
21314 6e 64 65 78 20 65 6e 74 72 69 65 73 20 74 68 61 ndex entries tha
21315 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 0a 20 t refer to the.
21316 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65 ** table. The
21317 20 70 72 6f 67 72 61 6d 20 6e 61 6d 65 20 6c 6f program name lo
21318 6f 70 73 20 74 68 72 6f 75 67 68 20 74 68 65 20 ops through the
21319 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 master table and
2131a 20 64 65 6c 65 74 65 73 0a 20 20 20 20 2a 2a 20 deletes. **
2131b 65 76 65 72 79 20 72 6f 77 20 74 68 61 74 20 72 every row that r
2131c 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 efers to a table
2131d 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d of the same nam
2131e 65 20 61 73 20 74 68 65 20 6f 6e 65 20 62 65 69 e as the one bei
2131f 6e 67 0a 20 20 20 20 2a 2a 20 64 72 6f 70 70 65 ng. ** droppe
21320 64 2e 20 54 72 69 67 67 65 72 73 20 61 72 65 20 d. Triggers are
21321 68 61 6e 64 6c 65 64 20 73 65 70 65 72 61 74 65 handled seperate
21322 6c 79 20 62 65 63 61 75 73 65 20 61 20 74 72 69 ly because a tri
21323 67 67 65 72 20 63 61 6e 20 62 65 0a 20 20 20 20 gger can be.
21324 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 ** created in th
21325 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 20 e temp database
21326 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 61 that refers to a
21327 20 74 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68 65 table in anothe
21328 72 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 r. ** databas
21329 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 e.. */. sq
2132a 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 lite3NestedParse
2132b 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 (pParse, .
2132c 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 "DELETE FROM %
2132d 51 2e 25 73 20 57 48 45 52 45 20 74 62 6c 5f 6e Q.%s WHERE tbl_n
2132e 61 6d 65 3d 25 51 20 61 6e 64 20 74 79 70 65 21 ame=%Q and type!
2132f 3d 27 74 72 69 67 67 65 72 27 22 2c 0a 20 20 20 ='trigger'",.
21330 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c pDb->zName,
21331 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 SCHEMA_TABLE(iD
21332 62 29 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 b), pTab->zName)
21333 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 ;.. /* Drop a
21334 6e 79 20 73 74 61 74 69 73 74 69 63 73 20 66 72 ny statistics fr
21335 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 om the sqlite_st
21336 61 74 31 20 74 61 62 6c 65 2c 20 69 66 20 69 74 at1 table, if it
21337 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 69 exists */. i
21338 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 f( sqlite3FindTa
21339 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f ble(db, "sqlite_
2133a 73 74 61 74 31 22 2c 20 64 62 2d 3e 61 44 62 5b stat1", db->aDb[
2133b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 iDb].zName) ){.
2133c 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 sqlite3Nest
2133d 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a edParse(pParse,.
2133e 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 "DELETE
2133f 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 FROM %Q.sqlite_s
21340 74 61 74 31 20 57 48 45 52 45 20 74 62 6c 3d 25 tat1 WHERE tbl=%
21341 51 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 Q", pDb->zName,
21342 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 pTab->zName.
21343 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 );. }..
21344 69 66 28 20 21 69 73 56 69 65 77 20 26 26 20 21 if( !isView && !
21345 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 IsVirtual(pTab)
21346 29 7b 0a 20 20 20 20 20 20 64 65 73 74 72 6f 79 ){. destroy
21347 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 Table(pParse, pT
21348 61 62 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ab);. }..
21349 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 74 61 /* Remove the ta
2134a 62 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 53 ble entry from S
2134b 51 4c 69 74 65 27 73 20 69 6e 74 65 72 6e 61 6c QLite's internal
2134c 20 73 63 68 65 6d 61 20 61 6e 64 20 6d 6f 64 69 schema and modi
2134d 66 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63 fy. ** the sc
2134e 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 20 20 20 hema cookie..
2134f 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 73 56 69 */. if( IsVi
21350 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 rtual(pTab) ){.
21351 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21352 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 44 65 AddOp4(v, OP_VDe
21353 73 74 72 6f 79 2c 20 69 44 62 2c 20 30 2c 20 30 stroy, iDb, 0, 0
21354 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 , pTab->zName, 0
21355 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
21356 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
21357 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20 , OP_DropTable,
21358 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d iDb, 0, 0, pTab-
21359 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 >zName, 0);.
2135a 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f sqlite3ChangeCoo
2135b 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 kie(pParse, iDb)
2135c 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 56 69 ;. }. sqliteVi
2135d 65 77 52 65 73 65 74 41 6c 6c 28 64 62 2c 20 69 ewResetAll(db, i
2135e 44 62 29 3b 0a 0a 65 78 69 74 5f 64 72 6f 70 5f Db);..exit_drop_
2135f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 74 65 33 table:. sqlite3
21360 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 SrcListDelete(db
21361 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a , pName);.}../*.
21362 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
21363 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 is called to cre
21364 61 74 65 20 61 20 6e 65 77 20 66 6f 72 65 69 67 ate a new foreig
21365 6e 20 6b 65 79 20 6f 6e 20 74 68 65 20 74 61 62 n key on the tab
21366 6c 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 le.** currently
21367 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 under constructi
21368 6f 6e 2e 20 20 70 46 72 6f 6d 43 6f 6c 20 64 65 on. pFromCol de
21369 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20 63 termines which c
2136a 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 olumns.** in the
2136b 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 20 70 current table p
2136c 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 6f 72 65 oint to the fore
2136d 69 67 6e 20 6b 65 79 2e 20 20 49 66 20 70 46 72 ign key. If pFr
2136e 6f 6d 43 6f 6c 3d 3d 30 20 74 68 65 6e 0a 2a 2a omCol==0 then.**
2136f 20 63 6f 6e 6e 65 63 74 20 74 68 65 20 6b 65 79 connect the key
21370 20 74 6f 20 74 68 65 20 6c 61 73 74 20 63 6f 6c to the last col
21371 75 6d 6e 20 69 6e 73 65 72 74 65 64 2e 20 20 70 umn inserted. p
21372 54 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f To is the name o
21373 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 72 f.** the table r
21374 65 66 65 72 72 65 64 20 74 6f 2e 20 20 70 54 6f eferred to. pTo
21375 43 6f 6c 20 69 73 20 61 20 6c 69 73 74 20 6f 66 Col is a list of
21376 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6f tables in the o
21377 74 68 65 72 0a 2a 2a 20 70 54 6f 20 74 61 62 6c ther.** pTo tabl
21378 65 20 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 e that the forei
21379 67 6e 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f gn key points to
2137a 2e 20 20 66 6c 61 67 73 20 63 6f 6e 74 61 69 6e . flags contain
2137b 73 20 61 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 s all.** informa
2137c 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 tion about the c
2137d 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 onflict resoluti
2137e 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 73 70 on algorithms sp
2137f 65 63 69 66 69 65 64 0a 2a 2a 20 69 6e 20 74 68 ecified.** in th
21380 65 20 4f 4e 20 44 45 4c 45 54 45 2c 20 4f 4e 20 e ON DELETE, ON
21381 55 50 44 41 54 45 20 61 6e 64 20 4f 4e 20 49 4e UPDATE and ON IN
21382 53 45 52 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a SERT clauses..**
21383 0a 2a 2a 20 41 6e 20 46 4b 65 79 20 73 74 72 75 .** An FKey stru
21384 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64 cture is created
21385 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68 and added to th
21386 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c e table currentl
21387 79 0a 2a 2a 20 75 6e 64 65 72 20 63 6f 6e 73 74 y.** under const
21388 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 70 ruction in the p
21389 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
2138a 20 66 69 65 6c 64 2e 20 20 54 68 65 20 6e 65 77 field. The new
2138b 20 46 4b 65 79 0a 2a 2a 20 69 73 20 6e 6f 74 20 FKey.** is not
2138c 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 64 62 2d 3e linked into db->
2138d 61 46 4b 65 79 20 61 74 20 74 68 69 73 20 70 6f aFKey at this po
2138e 69 6e 74 20 2d 20 74 68 61 74 20 64 6f 65 73 20 int - that does
2138f 6e 6f 74 20 68 61 70 70 65 6e 0a 2a 2a 20 75 6e not happen.** un
21390 74 69 6c 20 73 71 6c 69 74 65 33 45 6e 64 54 61 til sqlite3EndTa
21391 62 6c 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ble()..**.** The
21392 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 foreign key is
21393 73 65 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 set for IMMEDIAT
21394 45 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 41 E processing. A
21395 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
21396 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 44 65 .** to sqlite3De
21397 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 29 20 ferForeignKey()
21398 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 69 might change thi
21399 73 20 74 6f 20 44 45 46 45 52 52 45 44 2e 0a 2a s to DEFERRED..*
2139a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
2139b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 void sqlite3Cre
2139c 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 0a 20 ateForeignKey(.
2139d 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
2139e 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 /* Parsing
2139f 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 context */. Ex
213a0 70 72 4c 69 73 74 20 2a 70 46 72 6f 6d 43 6f 6c prList *pFromCol
213a1 2c 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e , /* Columns in
213a2 20 74 68 69 73 20 74 61 62 6c 65 20 74 68 61 74 this table that
213a3 20 70 6f 69 6e 74 20 74 6f 20 6f 74 68 65 72 20 point to other
213a4 74 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e table */. Token
213a5 20 2a 70 54 6f 2c 20 20 20 20 20 20 20 20 20 20 *pTo,
213a6 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f /* Name of the o
213a7 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 ther table */.
213a8 45 78 70 72 4c 69 73 74 20 2a 70 54 6f 43 6f 6c ExprList *pToCol
213a9 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 , /* Columns
213aa 69 6e 20 74 68 65 20 6f 74 68 65 72 20 74 61 62 in the other tab
213ab 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 le */. int flag
213ac 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s /*
213ad 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 Conflict resolut
213ae 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 2e 20 ion algorithms.
213af 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 */.){. sqlite3
213b0 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
213b1 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
213b2 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 _OMIT_FOREIGN_KE
213b3 59 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 20 Y. FKey *pFKey
213b4 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 20 = 0;. Table *p
213b5 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 = pParse->pNewTa
213b6 62 6c 65 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 ble;. int nByte
213b7 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 ;. int i;. int
213b8 20 6e 43 6f 6c 3b 0a 20 20 63 68 61 72 20 2a 7a nCol;. char *z
213b9 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f ;.. assert( pTo
213ba 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d !=0 );. if( p==
213bb 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 0 || pParse->nEr
213bc 72 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f r || IN_DECLARE_
213bd 56 54 41 42 20 29 20 67 6f 74 6f 20 66 6b 5f 65 VTAB ) goto fk_e
213be 6e 64 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 43 nd;. if( pFromC
213bf 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 ol==0 ){. int
213c0 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e 43 6f 6c 2d iCol = p->nCol-
213c1 31 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 1;. if( iCol<
213c2 30 20 29 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0 ) goto fk_end;
213c3 0a 20 20 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 . if( pToCol
213c4 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 && pToCol->nExpr
213c5 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c !=1 ){. sql
213c6 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
213c7 72 73 65 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65 rse, "foreign ke
213c8 79 20 6f 6e 20 25 73 22 0a 20 20 20 20 20 20 20 y on %s".
213c9 20 20 22 20 73 68 6f 75 6c 64 20 72 65 66 65 72 " should refer
213ca 65 6e 63 65 20 6f 6e 6c 79 20 6f 6e 65 20 63 6f ence only one co
213cb 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 25 54 lumn of table %T
213cc 22 2c 0a 20 20 20 20 20 20 20 20 20 70 2d 3e 61 ",. p->a
213cd 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c Col[iCol].zName,
213ce 20 70 54 6f 29 3b 0a 20 20 20 20 20 20 67 6f 74 pTo);. got
213cf 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a o fk_end;. }.
213d0 20 20 20 20 6e 43 6f 6c 20 3d 20 31 3b 0a 20 20 nCol = 1;.
213d1 7d 65 6c 73 65 20 69 66 28 20 70 54 6f 43 6f 6c }else if( pToCol
213d2 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 && pToCol->nExp
213d3 72 21 3d 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 r!=pFromCol->nEx
213d4 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 pr ){. sqlite
213d5 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
213d6 2c 0a 20 20 20 20 20 20 20 20 22 6e 75 6d 62 65 ,. "numbe
213d7 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
213d8 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 6f 65 73 foreign key does
213d9 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e not match the n
213da 75 6d 62 65 72 20 6f 66 20 22 0a 20 20 20 20 20 umber of ".
213db 20 20 20 22 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 "columns in t
213dc 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 he referenced ta
213dd 62 6c 65 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 ble");. goto
213de 66 6b 5f 65 6e 64 3b 0a 20 20 7d 65 6c 73 65 7b fk_end;. }else{
213df 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 46 72 6f . nCol = pFro
213e0 6d 43 6f 6c 2d 3e 6e 45 78 70 72 3b 0a 20 20 7d mCol->nExpr;. }
213e1 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f . nByte = sizeo
213e2 66 28 2a 70 46 4b 65 79 29 20 2b 20 6e 43 6f 6c f(*pFKey) + nCol
213e3 2a 73 69 7a 65 6f 66 28 70 46 4b 65 79 2d 3e 61 *sizeof(pFKey->a
213e4 43 6f 6c 5b 30 5d 29 20 2b 20 70 54 6f 2d 3e 6e Col[0]) + pTo->n
213e5 20 2b 20 31 3b 0a 20 20 69 66 28 20 70 54 6f 43 + 1;. if( pToC
213e6 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d ol ){. for(i=
213e7 30 3b 20 69 3c 70 54 6f 43 6f 6c 2d 3e 6e 45 78 0; i<pToCol->nEx
213e8 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 pr; i++){.
213e9 6e 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 nByte += sqlite3
213ea 53 74 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d Strlen30(pToCol-
213eb 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 >a[i].zName) + 1
213ec 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 ;. }. }. pF
213ed 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 44 62 4d Key = sqlite3DbM
213ee 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 allocZero(db, nB
213ef 79 74 65 20 29 3b 0a 20 20 69 66 28 20 70 46 4b yte );. if( pFK
213f0 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 ey==0 ){. got
213f1 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 o fk_end;. }.
213f2 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 pFKey->pFrom = p
213f3 3b 0a 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 ;. pFKey->pNext
213f4 46 72 6f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b From = p->pFKey;
213f5 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 70 . z = (char*)&p
213f6 46 4b 65 79 5b 31 5d 3b 0a 20 20 70 46 4b 65 79 FKey[1];. pFKey
213f7 2d 3e 61 43 6f 6c 20 3d 20 28 73 74 72 75 63 74 ->aCol = (struct
213f8 20 73 43 6f 6c 4d 61 70 2a 29 7a 3b 0a 20 20 7a sColMap*)z;. z
213f9 20 2b 3d 20 73 69 7a 65 6f 66 28 73 74 72 75 63 += sizeof(struc
213fa 74 20 73 43 6f 6c 4d 61 70 29 2a 6e 43 6f 6c 3b t sColMap)*nCol;
213fb 0a 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 . pFKey->zTo =
213fc 7a 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 z;. memcpy(z, p
213fd 54 6f 2d 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a To->z, pTo->n);.
213fe 20 20 7a 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b z[pTo->n] = 0;
213ff 0a 20 20 7a 20 2b 3d 20 70 54 6f 2d 3e 6e 2b 31 . z += pTo->n+1
21400 3b 0a 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 ;. pFKey->pNext
21401 54 6f 20 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d To = 0;. pFKey-
21402 3e 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 >nCol = nCol;.
21403 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 if( pFromCol==0
21404 29 7b 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 ){. pFKey->aC
21405 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d ol[0].iFrom = p-
21406 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 >nCol-1;. }else
21407 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 {. for(i=0; i
21408 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nCol; i++){.
21409 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 int j;.
2140a 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 for(j=0; j<p->nC
2140b 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ol; j++){.
2140c 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 if( sqlite3Str
2140d 49 43 6d 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e ICmp(p->aCol[j].
2140e 7a 4e 61 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d zName, pFromCol-
2140f 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 >a[i].zName)==0
21410 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b ){. pFK
21411 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f ey->aCol[i].iFro
21412 6d 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 m = j;.
21413 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
21414 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
21415 69 66 28 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 if( j>=p->nCol )
21416 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
21417 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
21418 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e , . "un
21419 6b 6e 6f 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 known column \"%
2141a 73 5c 22 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b s\" in foreign k
2141b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 ey definition",
2141c 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d . pFrom
2141d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 Col->a[i].zName)
2141e 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 ;. goto f
2141f 6b 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 k_end;. }.
21420 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 }. }. if( p
21421 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 ToCol ){. for
21422 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b (i=0; i<nCol; i+
21423 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 +){. int n
21424 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
21425 30 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 0(pToCol->a[i].z
21426 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 46 4b Name);. pFK
21427 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c ey->aCol[i].zCol
21428 20 3d 20 7a 3b 0a 20 20 20 20 20 20 6d 65 6d 63 = z;. memc
21429 70 79 28 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61 5b py(z, pToCol->a[
2142a 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 i].zName, n);.
2142b 20 20 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 z[n] = 0;.
2142c 20 20 20 20 7a 20 2b 3d 20 6e 2b 31 3b 0a 20 20 z += n+1;.
2142d 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d }. }. pFKey-
2142e 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20 30 3b >isDeferred = 0;
2142f 0a 20 20 70 46 4b 65 79 2d 3e 64 65 6c 65 74 65 . pFKey->delete
21430 43 6f 6e 66 20 3d 20 28 75 38 29 28 66 6c 61 67 Conf = (u8)(flag
21431 73 20 26 20 30 78 66 66 29 3b 0a 20 20 70 46 4b s & 0xff);. pFK
21432 65 79 2d 3e 75 70 64 61 74 65 43 6f 6e 66 20 3d ey->updateConf =
21433 20 28 75 38 29 28 28 66 6c 61 67 73 20 3e 3e 20 (u8)((flags >>
21434 38 20 29 20 26 20 30 78 66 66 29 3b 0a 20 20 70 8 ) & 0xff);. p
21435 46 4b 65 79 2d 3e 69 6e 73 65 72 74 43 6f 6e 66 FKey->insertConf
21436 20 3d 20 28 75 38 29 28 28 66 6c 61 67 73 20 3e = (u8)((flags >
21437 3e 20 31 36 20 29 20 26 20 30 78 66 66 29 3b 0a > 16 ) & 0xff);.
21438 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 66 . /* Link the f
21439 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f 20 74 68 oreign key to th
2143a 65 20 74 61 62 6c 65 20 61 73 20 74 68 65 20 6c e table as the l
2143b 61 73 74 20 73 74 65 70 2e 0a 20 20 2a 2f 0a 20 ast step.. */.
2143c 20 70 2d 3e 70 46 4b 65 79 20 3d 20 70 46 4b 65 p->pFKey = pFKe
2143d 79 3b 0a 20 20 70 46 4b 65 79 20 3d 20 30 3b 0a y;. pFKey = 0;.
2143e 0a 66 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 .fk_end:. sqlit
2143f 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b e3DbFree(db, pFK
21440 65 79 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 21 ey);.#endif /* !
21441 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
21442 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 MIT_FOREIGN_KEY)
21443 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 */. sqlite3Exp
21444 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 rListDelete(db,
21445 70 46 72 6f 6d 43 6f 6c 29 3b 0a 20 20 73 71 6c pFromCol);. sql
21446 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 ite3ExprListDele
21447 74 65 28 64 62 2c 20 70 54 6f 43 6f 6c 29 3b 0a te(db, pToCol);.
21448 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
21449 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
2144a 77 68 65 6e 20 61 6e 20 49 4e 49 54 49 41 4c 4c when an INITIALL
2144b 59 20 49 4d 4d 45 44 49 41 54 45 20 6f 72 20 49 Y IMMEDIATE or I
2144c 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 NITIALLY DEFERRE
2144d 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 73 D.** clause is s
2144e 65 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20 61 een as part of a
2144f 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 foreign key def
21450 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20 69 73 inition. The is
21451 44 65 66 65 72 72 65 64 0a 2a 2a 20 70 61 72 61 Deferred.** para
21452 6d 65 74 65 72 20 69 73 20 31 20 66 6f 72 20 49 meter is 1 for I
21453 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 NITIALLY DEFERRE
21454 44 20 61 6e 64 20 30 20 66 6f 72 20 49 4e 49 54 D and 0 for INIT
21455 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 2e IALLY IMMEDIATE.
21456 0a 2a 2a 20 54 68 65 20 62 65 68 61 76 69 6f 72 .** The behavior
21457 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 of the most rec
21458 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20 66 6f ently created fo
21459 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 64 6a reign key is adj
2145a 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f 72 64 69 usted.** accordi
2145b 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ngly..*/.SQLITE_
2145c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
2145d 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e ite3DeferForeign
2145e 4b 65 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 Key(Parse *pPars
2145f 65 2c 20 69 6e 74 20 69 73 44 65 66 65 72 72 65 e, int isDeferre
21460 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 d){.#ifndef SQLI
21461 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f TE_OMIT_FOREIGN_
21462 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 KEY. Table *pTa
21463 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 b;. FKey *pFKey
21464 3b 0a 20 20 69 66 28 20 28 70 54 61 62 20 3d 20 ;. if( (pTab =
21465 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
21466 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65 79 20 e)==0 || (pFKey
21467 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 29 3d 3d = pTab->pFKey)==
21468 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 0 ) return;. as
21469 73 65 72 74 28 20 69 73 44 65 66 65 72 72 65 64 sert( isDeferred
2146a 3d 3d 30 20 7c 7c 20 69 73 44 65 66 65 72 72 65 ==0 || isDeferre
2146b 64 3d 3d 31 20 29 3b 0a 20 20 70 46 4b 65 79 2d d==1 );. pFKey-
2146c 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20 28 75 >isDeferred = (u
2146d 38 29 69 73 44 65 66 65 72 72 65 64 3b 0a 23 65 8)isDeferred;.#e
2146e 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 ndif.}../*.** Ge
2146f 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 nerate code that
21470 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e 64 20 will erase and
21471 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a 70 49 refill index *pI
21472 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 dx. This is.**
21473 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 used to initiali
21474 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65 61 74 ze a newly creat
21475 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f 20 72 ed index or to r
21476 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 ecompute the.**
21477 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 69 6e content of an in
21478 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 dex in response
21479 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63 6f 6d to a REINDEX com
2147a 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66 20 6d mand..**.** if m
2147b 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20 6e 6f emRootPage is no
2147c 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74 20 6d t negative, it m
2147d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 69 6e eans that the in
2147e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a 2a 20 dex is newly.**
2147f 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 72 65 created. The re
21480 67 69 73 74 65 72 20 73 70 65 63 69 66 69 65 64 gister specified
21481 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 by memRootPage
21482 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 contains the.**
21483 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 root page number
21484 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 of the index.
21485 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 If memRootPage i
21486 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e s negative, then
21487 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 61 6c .** the index al
21488 72 65 61 64 79 20 65 78 69 73 74 73 20 61 6e 64 ready exists and
21489 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72 65 64 must be cleared
2148a 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 72 65 before being re
2148b 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 74 68 filled and.** th
2148c 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 e root page numb
2148d 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 er of the index
2148e 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 70 49 is taken from pI
2148f 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f 0a 73 ndex->tnum..*/.s
21490 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
21491 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 50 61 e3RefillIndex(Pa
21492 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e 64 rse *pParse, Ind
21493 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e 74 20 ex *pIndex, int
21494 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a 20 20 memRootPage){.
21495 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 Table *pTab = pI
21496 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20 20 2f ndex->pTable; /
21497 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 * The table that
21498 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 is indexed */.
21499 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 int iTab = pPar
2149a 73 65 2d 3e 6e 54 61 62 3b 20 20 20 20 20 20 20 se->nTab;
2149b 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f 72 20 /* Btree cursor
2149c 75 73 65 64 20 66 6f 72 20 70 54 61 62 20 2a 2f used for pTab */
2149d 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 70 50 . int iIdx = pP
2149e 61 72 73 65 2d 3e 6e 54 61 62 2b 31 3b 20 20 20 arse->nTab+1;
2149f 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f /* Btree curso
214a0 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e 64 65 r used for pInde
214a1 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 31 x */. int addr1
214a2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
214a3 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 /* Address
214a4 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 of top of loop
214a5 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 */. int tnum;
214a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214a7 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 /* Root page
214a8 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a 20 20 56 of index */. V
214a9 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 dbe *v;
214aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
214ab 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 Generate code i
214ac 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c nto this virtual
214ad 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 4b 65 machine */. Ke
214ae 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20 yInfo *pKey;
214af 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
214b0 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69 6e 64 65 KeyInfo for inde
214b1 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 64 x */. int regId
214b2 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 xKey;
214b3 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
214b4 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 rs containing th
214b5 65 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 e index key */.
214b6 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 20 int regRecord;
214b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214b8 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 /* Register hold
214b9 69 6e 67 20 61 73 73 65 6d 62 6c 69 65 64 20 69 ing assemblied i
214ba 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20 ndex record */.
214bb 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
214bc 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 Parse->db;
214bd 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
214be 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
214bf 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 int iDb = sqlite
214c0 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 3SchemaToIndex(d
214c1 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 b, pIndex->pSche
214c2 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 ma);..#ifndef SQ
214c3 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
214c4 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 IZATION. if( sq
214c5 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 lite3AuthCheck(p
214c6 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 52 45 Parse, SQLITE_RE
214c7 49 4e 44 45 58 2c 20 70 49 6e 64 65 78 2d 3e 7a INDEX, pIndex->z
214c8 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20 64 Name, 0,. d
214c9 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
214ca 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 e ) ){. retur
214cb 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 n;. }.#endif..
214cc 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20 77 72 /* Require a wr
214cd 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ite-lock on the
214ce 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f 72 6d table to perform
214cf 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 this operation
214d0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c */. sqlite3Tabl
214d1 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 eLock(pParse, iD
214d2 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 31 b, pTab->tnum, 1
214d3 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a , pTab->zName);.
214d4 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 . v = sqlite3Ge
214d5 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
214d6 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 if( v==0 ) retu
214d7 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f 6f rn;. if( memRoo
214d8 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 tPage>=0 ){.
214d9 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50 61 tnum = memRootPa
214da 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ge;. }else{.
214db 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78 2d 3e tnum = pIndex->
214dc 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 tnum;. sqlite
214dd 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
214de 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d 2c 20 69 P_Clear, tnum, i
214df 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b 65 79 20 Db);. }. pKey
214e0 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 = sqlite3IndexKe
214e1 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 yinfo(pParse, pI
214e2 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 ndex);. sqlite3
214e3 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
214e4 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49 64 78 _OpenWrite, iIdx
214e5 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0a 20 20 , tnum, iDb, .
214e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214e7 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c 20 (char *)pKey,
214e8 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f P4_KEYINFO_HANDO
214e9 46 46 29 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f FF);. if( memRo
214ea 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 otPage>=0 ){.
214eb 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
214ec 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 7d 0a geP5(v, 1);. }.
214ed 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 sqlite3OpenTab
214ee 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c le(pParse, iTab,
214ef 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f iDb, pTab, OP_O
214f0 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64 64 72 penRead);. addr
214f1 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 1 = sqlite3VdbeA
214f2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 ddOp2(v, OP_Rewi
214f3 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20 nd, iTab, 0);.
214f4 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 regRecord = sqli
214f5 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
214f6 61 72 73 65 29 3b 0a 20 20 72 65 67 49 64 78 4b arse);. regIdxK
214f7 65 79 20 3d 20 73 71 6c 69 74 65 33 47 65 6e 65 ey = sqlite3Gene
214f8 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61 rateIndexKey(pPa
214f9 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 54 61 rse, pIndex, iTa
214fa 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 31 29 b, regRecord, 1)
214fb 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e ;. if( pIndex->
214fc 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 onError!=OE_None
214fd 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 2c 20 ){. int j1,
214fe 6a 32 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 52 j2;. int regR
214ff 6f 77 69 64 3b 0a 0a 20 20 20 20 72 65 67 52 6f owid;.. regRo
21500 77 69 64 20 3d 20 72 65 67 49 64 78 4b 65 79 20 wid = regIdxKey
21501 2b 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d + pIndex->nColum
21502 6e 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 n;. j1 = sqli
21503 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
21504 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 49 OP_IsNull, regI
21505 64 78 4b 65 79 2c 20 30 2c 20 70 49 6e 64 65 78 dxKey, 0, pIndex
21506 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 ->nColumn);.
21507 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 j2 = sqlite3Vdbe
21508 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 73 55 AddOp4(v, OP_IsU
21509 6e 69 71 75 65 2c 20 69 49 64 78 2c 0a 20 20 20 nique, iIdx,.
2150a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2150b 20 20 20 20 20 20 20 20 30 2c 20 72 65 67 52 6f 0, regRo
2150c 77 69 64 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f wid, SQLITE_INT_
2150d 54 4f 5f 50 54 52 28 72 65 67 52 65 63 6f 72 64 TO_PTR(regRecord
2150e 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 ), P4_INT32);.
2150f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21510 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 Op4(v, OP_Halt,
21511 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
21512 54 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 30 2c 0a T, OE_Abort, 0,.
21513 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21514 20 20 20 20 22 69 6e 64 65 78 65 64 20 63 6f 6c "indexed col
21515 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75 6e 69 umns are not uni
21516 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43 29 que", P4_STATIC)
21517 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
21518 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 eJumpHere(v, j1)
21519 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
2151a 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 eJumpHere(v, j2)
2151b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 ;. }. sqlite3V
2151c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
2151d 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64 78 2c IdxInsert, iIdx,
2151e 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 regRecord);. s
2151f 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
21520 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 pReg(pParse, reg
21521 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74 Record);. sqlit
21522 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21523 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c 20 61 OP_Next, iTab, a
21524 64 64 72 31 2b 31 29 3b 0a 20 20 73 71 6c 69 74 ddr1+1);. sqlit
21525 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
21526 2c 20 61 64 64 72 31 29 3b 0a 20 20 73 71 6c 69 , addr1);. sqli
21527 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
21528 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 54 61 62 29 OP_Close, iTab)
21529 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 ;. sqlite3VdbeA
2152a 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 ddOp1(v, OP_Clos
2152b 65 2c 20 69 49 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a e, iIdx);.}../*.
2152c 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
2152d 69 6e 64 65 78 20 66 6f 72 20 61 6e 20 53 51 4c index for an SQL
2152e 20 74 61 62 6c 65 2e 20 20 70 4e 61 6d 65 31 2e table. pName1.
2152f 70 4e 61 6d 65 32 20 69 73 20 74 68 65 20 6e 61 pName2 is the na
21530 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 me of the index
21531 0a 2a 2a 20 61 6e 64 20 70 54 62 6c 4c 69 73 74 .** and pTblList
21532 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
21533 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 the table that i
21534 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e s to be indexed.
21535 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a 2a 20 Both will .**
21536 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20 70 72 be NULL for a pr
21537 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 61 6e 20 imary key or an
21538 69 6e 64 65 78 20 74 68 61 74 20 69 73 20 63 72 index that is cr
21539 65 61 74 65 64 20 74 6f 20 73 61 74 69 73 66 79 eated to satisfy
2153a 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 63 6f 6e a.** UNIQUE con
2153b 73 74 72 61 69 6e 74 2e 20 20 49 66 20 70 54 61 straint. If pTa
2153c 62 6c 65 20 61 6e 64 20 70 49 6e 64 65 78 20 61 ble and pIndex a
2153d 72 65 20 4e 55 4c 4c 2c 20 75 73 65 20 70 50 61 re NULL, use pPa
2153e 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 0a 2a rse->pNewTable.*
2153f 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 74 * as the table t
21540 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 70 o be indexed. p
21541 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
21542 20 69 73 20 61 20 74 61 62 6c 65 20 74 68 61 74 is a table that
21543 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 is.** currently
21544 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 being construct
21545 65 64 20 62 79 20 61 20 43 52 45 41 54 45 20 54 ed by a CREATE T
21546 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a ABLE statement..
21547 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73 20 61 **.** pList is a
21548 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 list of columns
21549 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 to be indexed.
2154a 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 65 20 4e pList will be N
2154b 55 4c 4c 20 69 66 20 74 68 69 73 0a 2a 2a 20 69 ULL if this.** i
2154c 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 s a primary key
2154d 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e 73 74 72 or unique-constr
2154e 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d 6f 73 74 aint on the most
2154f 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 61 recent column a
21550 64 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 74 dded.** to the t
21551 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75 able currently u
21552 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
21553 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 n. .*/.SQLITE_P
21554 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21555 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 0a te3CreateIndex(.
21556 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
21557 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e 66 6f /* All info
21558 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 rmation about th
21559 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20 54 6f is parse */. To
2155a 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20 ken *pName1,
2155b 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20 6f /* First part o
2155c 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d 61 f index name. Ma
2155d 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 y be NULL */. T
2155e 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 oken *pName2,
2155f 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 /* Second part
21560 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 of index name.
21561 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 May be NULL */.
21562 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c 4e 61 SrcList *pTblNa
21563 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 me, /* Table to
21564 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61 72 73 index. Use pPars
21565 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 66 20 e->pNewTable if
21566 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 0 */. ExprList
21567 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41 20 6c *pList, /* A l
21568 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 ist of columns t
21569 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a o be indexed */.
2156a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 int onError,
2156b 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 /* OE_Abort
2156c 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f , OE_Ignore, OE_
2156d 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e Replace, or OE_N
2156e 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a one */. Token *
2156f 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a 20 54 pStart, /* T
21570 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e 20 he CREATE token
21571 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 69 73 that begins this
21572 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
21573 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20 Token *pEnd,
21574 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20 74 68 /* The ")" th
21575 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20 43 52 at closes the CR
21576 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 EATE INDEX state
21577 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f ment */. int so
21578 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f 2a 20 rtOrder, /*
21579 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20 70 72 Sort order of pr
2157a 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e 20 70 imary key when p
2157b 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a 20 20 List==NULL */.
2157c 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74 20 20 int ifNotExist
2157d 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72 6f 72 /* Omit error
2157e 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65 61 64 if index alread
2157f 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 y exists */.){.
21580 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 Table *pTab = 0
21581 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 ; /* Table t
21582 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a o be indexed */.
21583 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 20 Index *pIndex
21584 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e = 0; /* The in
21585 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 74 65 dex to be create
21586 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 d */. char *zNa
21587 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e me = 0; /* N
21588 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 ame of the index
21589 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b */. int nName;
2158a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
2158b 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
2158c 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 rs in zName */.
2158d 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54 6f 6b int i, j;. Tok
2158e 65 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20 20 20 en nullId;
2158f 20 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65 6e 20 /* Fake token
21590 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 49 44 20 for an empty ID
21591 6c 69 73 74 20 2a 2f 0a 20 20 44 62 46 69 78 65 list */. DbFixe
21592 72 20 73 46 69 78 3b 20 20 20 20 20 20 20 20 2f r sFix; /
21593 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 6e 67 20 * For assigning
21594 64 61 74 61 62 61 73 65 20 6e 61 6d 65 73 20 74 database names t
21595 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 69 6e o pTable */. in
21596 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b t sortOrderMask;
21597 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e 6f 72 /* 1 to honor
21598 20 44 45 53 43 20 69 6e 20 69 6e 64 65 78 2e 20 DESC in index.
21599 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20 2a 2f 0 to ignore. */
2159a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
2159b 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 pParse->db;. D
2159c 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20 b *pDb;
2159d 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 /* The speci
2159e 66 69 63 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 fic table contai
2159f 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 65 64 ning the indexed
215a0 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 database */. i
215a1 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 nt iDb;
215a2 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
215a3 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 the database tha
215a4 74 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74 t is being writt
215a5 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 en */. Token *p
215a6 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f 2a 20 Name = 0; /*
215a7 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 Unqualified name
215a8 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 74 6f of the index to
215a9 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 73 74 72 create */. str
215aa 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
215ab 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20 2f 2a m *pListItem; /*
215ac 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 For looping ove
215ad 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 r pList */. int
215ae 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 45 78 nCol;. int nEx
215af 74 72 61 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 tra = 0;. char
215b0 2a 7a 45 78 74 72 61 3b 0a 0a 20 20 69 66 28 20 *zExtra;.. if(
215b1 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 pParse->nErr ||
215b2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
215b3 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 || IN_DECLARE_V
215b4 54 41 42 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 TAB ){. goto
215b5 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
215b6 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a x;. }.. /*. *
215b7 2a 20 46 69 6e 64 20 74 68 65 20 74 61 62 6c 65 * Find the table
215b8 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 that is to be i
215b9 6e 64 65 78 65 64 2e 20 20 52 65 74 75 72 6e 20 ndexed. Return
215ba 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 66 6f 75 early if not fou
215bb 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 nd.. */. if( p
215bc 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a 20 TblName!=0 ){..
215bd 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 74 77 /* Use the tw
215be 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 6e 61 6d o-part index nam
215bf 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 e to determine t
215c0 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 20 he database .
215c1 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20 66 6f ** to search fo
215c2 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27 46 69 r the table. 'Fi
215c3 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d x' the table nam
215c4 65 20 74 6f 20 74 68 69 73 20 64 62 0a 20 20 20 e to this db.
215c5 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f 6b 69 ** before looki
215c6 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c 65 2e ng up the table.
215c7 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
215c8 72 74 28 20 70 4e 61 6d 65 31 20 26 26 20 70 4e rt( pName1 && pN
215c9 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 44 62 20 ame2 );. iDb
215ca 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 = sqlite3TwoPart
215cb 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 Name(pParse, pNa
215cc 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e me1, pName2, &pN
215cd 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44 ame);. if( iD
215ce 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f b<0 ) goto exit_
215cf 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 23 create_index;..#
215d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
215d1 49 54 5f 54 45 4d 50 44 42 0a 20 20 20 20 2f 2a IT_TEMPDB. /*
215d2 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 6e 61 If the index na
215d3 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69 66 69 me was unqualifi
215d4 65 64 2c 20 63 68 65 63 6b 20 69 66 20 74 68 65 ed, check if the
215d5 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20 2a the table. *
215d6 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c * is a temp tabl
215d7 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68 e. If so, set th
215d8 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 31 2e e database to 1.
215d9 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 0a Do not do this.
215da 20 20 20 20 2a 2a 20 69 66 20 69 6e 69 74 69 61 ** if initia
215db 6c 69 73 69 6e 67 20 61 20 64 61 74 61 62 61 73 lising a databas
215dc 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f e schema.. */
215dd 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e . if( !db->in
215de 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 it.busy ){.
215df 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 pTab = sqlite3S
215e0 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 rcListLookup(pPa
215e1 72 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a rse, pTblName);.
215e2 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32 if( pName2
215e3 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 && pName2->n==0
215e4 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61 62 && pTab && pTab
215e5 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 ->pSchema==db->a
215e6 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 7b Db[1].pSchema ){
215e7 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20 31 . iDb = 1
215e8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
215e9 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 #endif.. if(
215ea 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 sqlite3FixInit(&
215eb 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 sFix, pParse, iD
215ec 62 2c 20 22 69 6e 64 65 78 22 2c 20 70 4e 61 6d b, "index", pNam
215ed 65 29 20 26 26 0a 20 20 20 20 20 20 20 20 73 71 e) &&. sq
215ee 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 lite3FixSrcList(
215ef 26 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d 65 29 &sFix, pTblName)
215f0 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a . ){. /*
215f1 20 42 65 63 61 75 73 65 20 74 68 65 20 70 61 72 Because the par
215f2 73 65 72 20 63 6f 6e 73 74 72 75 63 74 73 20 70 ser constructs p
215f3 54 62 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 20 73 TblName from a s
215f4 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72 ingle identifier
215f5 2c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 ,. ** sqlit
215f6 65 33 46 69 78 53 72 63 4c 69 73 74 20 63 61 6e e3FixSrcList can
215f7 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 2a 2f 0a never fail. */.
215f8 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29 3b assert(0);
215f9 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62 20 . }. pTab
215fa 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 = sqlite3LocateT
215fb 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 able(pParse, 0,
215fc 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a pTblName->a[0].z
215fd 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 20 70 Name, . p
215fe 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 TblName->a[0].zD
215ff 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 69 66 atabase);. if
21600 28 20 21 70 54 61 62 20 7c 7c 20 64 62 2d 3e 6d ( !pTab || db->m
21601 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f allocFailed ) go
21602 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
21603 6e 64 65 78 3b 0a 20 20 20 20 61 73 73 65 72 74 ndex;. assert
21604 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 ( db->aDb[iDb].p
21605 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 Schema==pTab->pS
21606 63 68 65 6d 61 20 29 3b 0a 20 20 7d 65 6c 73 65 chema );. }else
21607 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e {. assert( pN
21608 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 54 ame==0 );. pT
21609 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 ab = pParse->pNe
2160a 77 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 28 20 wTable;. if(
2160b 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 !pTab ) goto exi
2160c 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a t_create_index;.
2160d 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 iDb = sqlite
2160e 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 3SchemaToIndex(d
2160f 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 b, pTab->pSchema
21610 29 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d 20 26 );. }. pDb = &
21611 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 db->aDb[iDb];..
21612 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c 7c 20 if( pTab==0 ||
21613 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 67 pParse->nErr ) g
21614 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f oto exit_create_
21615 69 6e 64 65 78 3b 0a 20 20 69 66 28 20 73 71 6c index;. if( sql
21616 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 ite3StrNICmp(pTa
21617 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 b->zName, "sqlit
21618 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 e_", 7)==0 ){.
21619 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
2161a 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 g(pParse, "table
2161b 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 %s may not be i
2161c 6e 64 65 78 65 64 22 2c 20 70 54 61 62 2d 3e 7a ndexed", pTab->z
2161d 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 Name);. goto
2161e 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
2161f 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 x;. }.#ifndef S
21620 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a QLITE_OMIT_VIEW.
21621 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c if( pTab->pSel
21622 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ect ){. sqlit
21623 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
21624 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20 6e 6f e, "views may no
21625 74 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b 0a t be indexed");.
21626 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 goto exit_cr
21627 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a eate_index;. }.
21628 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
21629 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
2162a 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49 73 ALTABLE. if( Is
2162b 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b Virtual(pTab) ){
2162c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
2162d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 rMsg(pParse, "vi
2162e 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 61 79 rtual tables may
2162f 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 not be indexed"
21630 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 );. goto exit
21631 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
21632 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a }.#endif.. /*.
21633 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 61 ** Find the na
21634 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e me of the index.
21635 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72 Make sure ther
21636 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 e is not already
21637 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e another. ** in
21638 64 65 78 20 6f 72 20 74 61 62 6c 65 20 77 69 74 dex or table wit
21639 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e h the same name.
2163a 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 . **. ** Exc
2163b 65 70 74 69 6f 6e 3a 20 20 49 66 20 77 65 20 61 eption: If we a
2163c 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6e re reading the n
2163d 61 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e 65 6e ames of permanen
2163e 74 20 69 6e 64 69 63 65 73 20 66 72 6f 6d 20 74 t indices from t
2163f 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d he. ** sqlite_m
21640 61 73 74 65 72 20 74 61 62 6c 65 20 28 62 65 63 aster table (bec
21641 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 ause some other
21642 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65 64 20 process changed
21643 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e 64 0a the schema) and.
21644 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 ** one of the
21645 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f 6c 6c index names coll
21646 69 64 65 73 20 77 69 74 68 20 74 68 65 20 6e 61 ides with the na
21647 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61 72 me of a temporar
21648 79 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a 2a 20 y table or. **
21649 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65 20 77 index, then we w
2164a 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 ill continue to
2164b 70 72 6f 63 65 73 73 20 74 68 69 73 20 69 6e 64 process this ind
2164c 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 ex.. **. ** If
2164d 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 61 pName==0 it mea
2164e 6e 73 20 74 68 61 74 20 77 65 20 61 72 65 0a 20 ns that we are.
2164f 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69 74 68 ** dealing with
21650 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f a primary key o
21651 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 r UNIQUE constra
21652 69 6e 74 2e 20 20 57 65 20 68 61 76 65 20 74 6f int. We have to
21653 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20 2a 2a invent our. **
21654 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a own name.. */.
21655 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 if( pName ){.
21656 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 zName = sqlit
21657 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
21658 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 db, pName);.
21659 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 if( SQLITE_OK!=s
2165a 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 qlite3ReadSchema
2165b 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 (pParse) ) goto
2165c 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
2165d 78 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 x;. if( zName
2165e 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f ==0 ) goto exit_
2165f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 create_index;.
21660 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
21661 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a =sqlite3CheckObj
21662 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 ectName(pParse,
21663 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 zName) ){.
21664 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
21665 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 _index;. }.
21666 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e if( !db->init.
21667 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 69 66 busy ){. if
21668 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c ( SQLITE_OK!=sql
21669 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 ite3ReadSchema(p
2166a 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 65 78 Parse) ) goto ex
2166b 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b it_create_index;
2166c 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
2166d 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 e3FindTable(db,
2166e 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29 7b 0a zName, 0)!=0 ){.
2166f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
21670 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
21671 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 "there is alread
21672 79 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 y a table named
21673 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 %s", zName);.
21674 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 goto exit_c
21675 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 reate_index;.
21676 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
21677 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e f( sqlite3FindIn
21678 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 dex(db, zName, p
21679 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 29 7b Db->zName)!=0 ){
2167a 0a 20 20 20 20 20 20 69 66 28 20 21 69 66 4e 6f . if( !ifNo
2167b 74 45 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20 tExist ){.
2167c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
2167d 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64 65 78 g(pParse, "index
2167e 20 25 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 %s already exis
2167f 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 ts", zName);.
21680 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 }. goto
21681 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
21682 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 x;. }. }else
21683 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 {. int n;.
21684 20 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 3b 0a 20 Index *pLoop;.
21685 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 54 61 for(pLoop=pTa
21686 62 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d 31 3b 20 b->pIndex, n=1;
21687 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f pLoop; pLoop=pLo
21688 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b 2b 29 7b op->pNext, n++){
21689 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 }. zName = sq
2168a 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c lite3MPrintf(db,
2168b 20 22 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 "sqlite_autoind
2168c 65 78 5f 25 73 5f 25 64 22 2c 20 70 54 61 62 2d ex_%s_%d", pTab-
2168d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 >zName, n);.
2168e 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a if( zName==0 ){.
2168f 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f goto exit_
21690 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 create_index;.
21691 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 }. }.. /* Ch
21692 65 63 6b 20 66 6f 72 20 61 75 74 68 6f 72 69 7a eck for authoriz
21693 61 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 ation to create
21694 61 6e 20 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a 23 an index.. */.#
21695 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
21696 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e IT_AUTHORIZATION
21697 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 . {. const c
21698 68 61 72 20 2a 7a 44 62 20 3d 20 70 44 62 2d 3e har *zDb = pDb->
21699 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 zName;. if( s
2169a 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
2169b 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 pParse, SQLITE_I
2169c 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 NSERT, SCHEMA_TA
2169d 42 4c 45 28 69 44 62 29 2c 20 30 2c 20 7a 44 62 BLE(iDb), 0, zDb
2169e 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ) ){. goto
2169f 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
216a0 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d x;. }. i =
216a1 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 SQLITE_CREATE_I
216a2 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 21 4f NDEX;. if( !O
216a3 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 MIT_TEMPDB && iD
216a4 62 3d 3d 31 20 29 20 69 20 3d 20 53 51 4c 49 54 b==1 ) i = SQLIT
216a5 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e E_CREATE_TEMP_IN
216a6 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c DEX;. if( sql
216a7 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
216a8 61 72 73 65 2c 20 69 2c 20 7a 4e 61 6d 65 2c 20 arse, i, zName,
216a9 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 pTab->zName, zDb
216aa 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ) ){. goto
216ab 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
216ac 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e x;. }. }.#en
216ad 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 4c 69 dif.. /* If pLi
216ae 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e 73 20 st==0, it means
216af 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 this routine was
216b0 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b 65 20 called to make
216b1 61 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b a primary. ** k
216b2 65 79 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 61 ey out of the la
216b3 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64 20 st column added
216b4 74 6f 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64 to the table und
216b5 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e er construction.
216b6 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61 74 65 20 . ** So create
216b7 61 20 66 61 6b 65 20 6c 69 73 74 20 74 6f 20 73 a fake list to s
216b8 69 6d 75 6c 61 74 65 20 74 68 69 73 2e 0a 20 20 imulate this..
216b9 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d */. if( pList==
216ba 30 20 29 7b 0a 20 20 20 20 6e 75 6c 6c 49 64 2e 0 ){. nullId.
216bb 7a 20 3d 20 28 75 38 2a 29 70 54 61 62 2d 3e 61 z = (u8*)pTab->a
216bc 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 Col[pTab->nCol-1
216bd 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 75 6c ].zName;. nul
216be 6c 49 64 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53 lId.n = sqlite3S
216bf 74 72 6c 65 6e 33 30 28 28 63 68 61 72 2a 29 6e trlen30((char*)n
216c0 75 6c 6c 49 64 2e 7a 29 3b 0a 20 20 20 20 70 4c ullId.z);. pL
216c1 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ist = sqlite3Exp
216c2 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 rListAppend(pPar
216c3 73 65 2c 20 30 2c 20 30 2c 20 26 6e 75 6c 6c 49 se, 0, 0, &nullI
216c4 64 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 d);. if( pLis
216c5 74 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 t==0 ) goto exit
216c6 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
216c7 20 20 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73 pList->a[0].s
216c8 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29 73 ortOrder = (u8)s
216c9 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 0a 20 ortOrder;. }..
216ca 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 /* Figure out h
216cb 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 6f 66 ow many bytes of
216cc 20 73 70 61 63 65 20 61 72 65 20 72 65 71 75 69 space are requi
216cd 72 65 64 20 74 6f 20 73 74 6f 72 65 20 65 78 70 red to store exp
216ce 6c 69 63 69 74 6c 79 0a 20 20 2a 2a 20 73 70 65 licitly. ** spe
216cf 63 69 66 69 65 64 20 63 6f 6c 6c 61 74 69 6f 6e cified collation
216d0 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 2e sequence names.
216d1 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b . */. for(i=0;
216d2 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b i<pList->nExpr;
216d3 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 i++){. Expr
216d4 2a 70 45 78 70 72 3b 0a 20 20 20 20 43 6f 6c 6c *pExpr;. Coll
216d5 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 Seq *pColl;.
216d6 69 66 28 20 28 70 45 78 70 72 20 3d 20 70 4c 69 if( (pExpr = pLi
216d7 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 21 st->a[i].pExpr)!
216d8 3d 30 20 26 26 20 28 70 43 6f 6c 6c 20 3d 20 70 =0 && (pColl = p
216d9 45 78 70 72 2d 3e 70 43 6f 6c 6c 29 21 3d 30 20 Expr->pColl)!=0
216da 29 7b 0a 20 20 20 20 20 20 6e 45 78 74 72 61 20 ){. nExtra
216db 2b 3d 20 28 31 20 2b 20 73 71 6c 69 74 65 33 53 += (1 + sqlite3S
216dc 74 72 6c 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a trlen30(pColl->z
216dd 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a 20 20 Name));. }.
216de 7d 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 41 6c }.. /* . ** Al
216df 6c 6f 63 61 74 65 20 74 68 65 20 69 6e 64 65 78 locate the index
216e0 20 73 74 72 75 63 74 75 72 65 2e 20 0a 20 20 2a structure. . *
216e1 2f 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 /. nName = sqli
216e2 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d te3Strlen30(zNam
216e3 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 70 4c 69 e);. nCol = pLi
216e4 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e st->nExpr;. pIn
216e5 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d dex = sqlite3DbM
216e6 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20 allocZero(db, .
216e7 20 20 20 20 20 73 69 7a 65 6f 66 28 49 6e 64 65 sizeof(Inde
216e8 78 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 x) +
216e9 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 72 75 63 /* Index struc
216ea 74 75 72 65 20 20 2a 2f 0a 20 20 20 20 20 20 73 ture */. s
216eb 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 6f 6c 20 izeof(int)*nCol
216ec 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 + /* I
216ed 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e 20 20 20 ndex.aiColumn
216ee 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 */. sizeof(
216ef 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29 20 2b 20 int)*(nCol+1) +
216f0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 /* Index.a
216f1 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a 20 20 20 iRowEst */.
216f2 20 20 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a sizeof(char *
216f3 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 )*nCol +
216f4 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f 6c 6c 20 /* Index.azColl
216f5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a */. siz
216f6 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20 2b 20 20 eof(u8)*nCol +
216f7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
216f8 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 20 2a 2f ex.aSortOrder */
216f9 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 2b 20 31 . nName + 1
216fa 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 +
216fb 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 7a 4e 61 /* Index.zNa
216fc 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 me */.
216fd 20 6e 45 78 74 72 61 20 20 20 20 20 20 20 20 20 nExtra
216fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
216ff 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 Collation seque
21700 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 29 nce names */. )
21701 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c ;. if( db->mall
21702 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
21703 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
21704 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70 49 _index;. }. pI
21705 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20 3d 20 28 ndex->azColl = (
21706 63 68 61 72 2a 2a 29 28 26 70 49 6e 64 65 78 5b char**)(&pIndex[
21707 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 1]);. pIndex->a
21708 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74 20 2a iColumn = (int *
21709 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c )(&pIndex->azCol
2170a 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 l[nCol]);. pInd
2170b 65 78 2d 3e 61 69 52 6f 77 45 73 74 20 3d 20 28 ex->aiRowEst = (
2170c 75 6e 73 69 67 6e 65 64 20 2a 29 28 26 70 49 6e unsigned *)(&pIn
2170d 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 43 dex->aiColumn[nC
2170e 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e ol]);. pIndex->
2170f 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 aSortOrder = (u8
21710 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 69 52 *)(&pIndex->aiR
21711 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d 29 3b 0a owEst[nCol+1]);.
21712 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 20 pIndex->zName
21713 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 6e 64 = (char *)(&pInd
21714 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e ex->aSortOrder[n
21715 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74 72 61 20 Col]);. zExtra
21716 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 6e 64 = (char *)(&pInd
21717 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 2b ex->zName[nName+
21718 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 49 1]);. memcpy(pI
21719 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 ndex->zName, zNa
2171a 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 me, nName+1);.
2171b 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 20 3d pIndex->pTable =
2171c 20 70 54 61 62 3b 0a 20 20 70 49 6e 64 65 78 2d pTab;. pIndex-
2171d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73 74 >nColumn = pList
2171e 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 64 65 ->nExpr;. pInde
2171f 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 28 75 38 x->onError = (u8
21720 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 70 49 6e 64 )onError;. pInd
21721 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d 20 ex->autoIndex =
21722 28 75 38 29 28 70 4e 61 6d 65 3d 3d 30 29 3b 0a (u8)(pName==0);.
21723 20 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d pIndex->pSchem
21724 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d a = db->aDb[iDb]
21725 2e 70 53 63 68 65 6d 61 3b 0a 0a 20 20 2f 2a 20 .pSchema;.. /*
21726 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 Check to see if
21727 77 65 20 73 68 6f 75 6c 64 20 68 6f 6e 6f 72 20 we should honor
21728 44 45 53 43 20 72 65 71 75 65 73 74 73 20 6f 6e DESC requests on
21729 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 0a 20 index columns.
2172a 20 2a 2f 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 */. if( pDb->p
2172b 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 Schema->file_for
2172c 6d 61 74 3e 3d 34 20 29 7b 0a 20 20 20 20 73 6f mat>=4 ){. so
2172d 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20 2d 31 rtOrderMask = -1
2172e 3b 20 20 20 2f 2a 20 48 6f 6e 6f 72 20 44 45 53 ; /* Honor DES
2172f 43 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 C */. }else{.
21730 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20 sortOrderMask
21731 3d 20 30 3b 20 20 20 20 2f 2a 20 49 67 6e 6f 72 = 0; /* Ignor
21732 65 20 44 45 53 43 20 2a 2f 0a 20 20 7d 0a 0a 20 e DESC */. }..
21733 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 6e 61 6d /* Scan the nam
21734 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e es of the column
21735 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 s of the table t
21736 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61 6e 64 o be indexed and
21737 0a 20 20 2a 2a 20 6c 6f 61 64 20 74 68 65 20 63 . ** load the c
21738 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73 20 69 6e olumn indices in
21739 74 6f 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 to the Index str
2173a 75 63 74 75 72 65 2e 20 20 52 65 70 6f 72 74 20 ucture. Report
2173b 61 6e 20 65 72 72 6f 72 0a 20 20 2a 2a 20 69 66 an error. ** if
2173c 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 69 73 20 6e any column is n
2173d 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 ot found.. */.
2173e 20 66 6f 72 28 69 3d 30 2c 20 70 4c 69 73 74 49 for(i=0, pListI
2173f 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c tem=pList->a; i<
21740 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b pList->nExpr; i+
21741 2b 2c 20 70 4c 69 73 74 49 74 65 6d 2b 2b 29 7b +, pListItem++){
21742 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
21743 2a 7a 43 6f 6c 4e 61 6d 65 20 3d 20 70 4c 69 73 *zColName = pLis
21744 74 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 tItem->zName;.
21745 20 20 43 6f 6c 75 6d 6e 20 2a 70 54 61 62 43 6f Column *pTabCo
21746 6c 3b 0a 20 20 20 20 69 6e 74 20 72 65 71 75 65 l;. int reque
21747 73 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 stedSortOrder;.
21748 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 char *zColl;
21749 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2174a 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 /* Collation s
2174b 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a equence name */.
2174c 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 54 . for(j=0, pT
2174d 61 62 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c abCol=pTab->aCol
2174e 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 ; j<pTab->nCol;
2174f 6a 2b 2b 2c 20 70 54 61 62 43 6f 6c 2b 2b 29 7b j++, pTabCol++){
21750 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
21751 65 33 53 74 72 49 43 6d 70 28 7a 43 6f 6c 4e 61 e3StrICmp(zColNa
21752 6d 65 2c 20 70 54 61 62 43 6f 6c 2d 3e 7a 4e 61 me, pTabCol->zNa
21753 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a me)==0 ) break;.
21754 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3e }. if( j>
21755 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 =pTab->nCol ){.
21756 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
21757 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 rMsg(pParse, "ta
21758 62 6c 65 20 25 73 20 68 61 73 20 6e 6f 20 63 6f ble %s has no co
21759 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73 22 2c 0a lumn named %s",.
2175a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e pTab->zN
2175b 61 6d 65 2c 20 7a 43 6f 6c 4e 61 6d 65 29 3b 0a ame, zColName);.
2175c 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f goto exit_
2175d 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 create_index;.
2175e 20 20 7d 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a }. /* TODO:
2175f 20 20 41 64 64 20 61 20 74 65 73 74 20 74 6f 20 Add a test to
21760 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
21761 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 69 he same column i
21762 73 20 6e 6f 74 20 6e 61 6d 65 64 0a 20 20 20 20 s not named.
21763 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ** more than onc
21764 65 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d e within the sam
21765 65 20 69 6e 64 65 78 2e 20 20 4f 6e 6c 79 20 74 e index. Only t
21766 68 65 20 66 69 72 73 74 20 69 6e 73 74 61 6e 63 he first instanc
21767 65 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 e of. ** the
21768 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 65 76 65 72 column will ever
21769 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65 20 be used by the
2176a 6f 70 74 69 6d 69 7a 65 72 2e 20 20 4e 6f 74 65 optimizer. Note
2176b 20 74 68 61 74 20 75 73 69 6e 67 20 74 68 65 0a that using the.
2176c 20 20 20 20 2a 2a 20 73 61 6d 65 20 63 6f 6c 75 ** same colu
2176d 6d 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 mn more than onc
2176e 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 65 e cannot be an e
2176f 72 72 6f 72 20 62 65 63 61 75 73 65 20 74 68 61 rror because tha
21770 74 20 77 6f 75 6c 64 20 0a 20 20 20 20 2a 2a 20 t would . **
21771 62 72 65 61 6b 20 62 61 63 6b 77 61 72 64 73 20 break backwards
21772 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2d 20 compatibility -
21773 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 it needs to be a
21774 20 77 61 72 6e 69 6e 67 2e 0a 20 20 20 20 2a 2f warning.. */
21775 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 43 . pIndex->aiC
21776 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 6a 3b 0a 20 20 olumn[i] = j;.
21777 20 20 69 66 28 20 70 4c 69 73 74 49 74 65 6d 2d if( pListItem-
21778 3e 70 45 78 70 72 20 26 26 20 70 4c 69 73 74 49 >pExpr && pListI
21779 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c tem->pExpr->pCol
2177a 6c 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 l ){. asser
2177b 74 28 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 t( pListItem->pE
2177c 78 70 72 2d 3e 70 43 6f 6c 6c 20 29 3b 0a 20 20 xpr->pColl );.
2177d 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 7a 45 78 74 zColl = zExt
2177e 72 61 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ra;. sqlite
2177f 33 5f 73 6e 70 72 69 6e 74 66 28 6e 45 78 74 72 3_snprintf(nExtr
21780 61 2c 20 7a 45 78 74 72 61 2c 20 22 25 73 22 2c a, zExtra, "%s",
21781 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 pListItem->pExp
21782 72 2d 3e 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 r->pColl->zName)
21783 3b 0a 20 20 20 20 20 20 7a 45 78 74 72 61 20 2b ;. zExtra +
21784 3d 20 28 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = (sqlite3Strlen
21785 33 30 28 7a 43 6f 6c 6c 29 20 2b 20 31 29 3b 0a 30(zColl) + 1);.
21786 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
21787 20 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 zColl = pTab->a
21788 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 Col[j].zColl;.
21789 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 if( !zColl )
2178a 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 {. zColl
2178b 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d = db->pDfltColl-
2178c 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a >zName;. }.
2178d 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64 }. if( !d
2178e 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 b->init.busy &&
2178f 21 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f !sqlite3LocateCo
21790 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 llSeq(pParse, zC
21791 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 oll, -1) ){.
21792 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 goto exit_crea
21793 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a te_index;. }.
21794 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f pIndex->azCo
21795 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 ll[i] = zColl;.
21796 20 20 20 72 65 71 75 65 73 74 65 64 53 6f 72 74 requestedSort
21797 4f 72 64 65 72 20 3d 20 70 4c 69 73 74 49 74 65 Order = pListIte
21798 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26 20 73 m->sortOrder & s
21799 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a 20 20 ortOrderMask;.
2179a 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f pIndex->aSortO
2179b 72 64 65 72 5b 69 5d 20 3d 20 28 75 38 29 72 65 rder[i] = (u8)re
2179c 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72 questedSortOrder
2179d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 ;. }. sqlite3D
2179e 65 66 61 75 6c 74 52 6f 77 45 73 74 28 70 49 6e efaultRowEst(pIn
2179f 64 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 dex);.. if( pTa
217a0 62 3d 3d 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 b==pParse->pNewT
217a1 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 able ){. /* T
217a2 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 his routine has
217a3 62 65 65 6e 20 63 61 6c 6c 65 64 20 74 6f 20 63 been called to c
217a4 72 65 61 74 65 20 61 6e 20 61 75 74 6f 6d 61 74 reate an automat
217a5 69 63 20 69 6e 64 65 78 20 61 73 20 61 0a 20 20 ic index as a.
217a6 20 20 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 61 ** result of a
217a7 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 PRIMARY KEY or
217a8 55 4e 49 51 55 45 20 63 6c 61 75 73 65 20 6f 6e UNIQUE clause on
217a9 20 61 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 a column defini
217aa 74 69 6f 6e 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 tion, or. **
217ab 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 a PRIMARY KEY or
217ac 20 55 4e 49 51 55 45 20 63 6c 61 75 73 65 20 66 UNIQUE clause f
217ad 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 63 6f 6c ollowing the col
217ae 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e umn definitions.
217af 0a 20 20 20 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65 . ** i.e. one
217b0 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 of:. **.
217b1 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 ** CREATE TABLE
217b2 74 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 2c t(x PRIMARY KEY,
217b3 20 79 29 3b 0a 20 20 20 20 2a 2a 20 43 52 45 41 y);. ** CREA
217b4 54 45 20 54 41 42 4c 45 20 74 28 78 2c 20 79 2c TE TABLE t(x, y,
217b5 20 55 4e 49 51 55 45 28 78 2c 20 79 29 29 3b 0a UNIQUE(x, y));.
217b6 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 69 **. ** Ei
217b7 74 68 65 72 20 77 61 79 2c 20 63 68 65 63 6b 20 ther way, check
217b8 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 74 61 to see if the ta
217b9 62 6c 65 20 61 6c 72 65 61 64 79 20 68 61 73 20 ble already has
217ba 73 75 63 68 20 61 6e 20 69 6e 64 65 78 2e 20 49 such an index. I
217bb 66 0a 20 20 20 20 2a 2a 20 73 6f 2c 20 64 6f 6e f. ** so, don
217bc 27 74 20 62 6f 74 68 65 72 20 63 72 65 61 74 69 't bother creati
217bd 6e 67 20 74 68 69 73 20 6f 6e 65 2e 20 54 68 69 ng this one. Thi
217be 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74 s only applies t
217bf 6f 0a 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 o. ** automat
217c0 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 69 ically created i
217c1 6e 64 69 63 65 73 2e 20 55 73 65 72 73 20 63 61 ndices. Users ca
217c2 6e 20 64 6f 20 61 73 20 74 68 65 79 20 77 69 73 n do as they wis
217c3 68 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 65 78 h with. ** ex
217c4 70 6c 69 63 69 74 20 69 6e 64 69 63 65 73 2e 0a plicit indices..
217c5 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 */. Index
217c6 20 2a 70 49 64 78 3b 0a 20 20 20 20 66 6f 72 28 *pIdx;. for(
217c7 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 pIdx=pTab->pInde
217c8 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 x; pIdx; pIdx=pI
217c9 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 dx->pNext){.
217ca 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 61 int k;. a
217cb 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6f 6e 45 ssert( pIdx->onE
217cc 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b rror!=OE_None );
217cd 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
217ce 49 64 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 Idx->autoIndex )
217cf 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
217d0 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 pIndex->onError!
217d1 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 0a 20 20 20 =OE_None );..
217d2 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6e 43 6f if( pIdx->nCo
217d3 6c 75 6d 6e 21 3d 70 49 6e 64 65 78 2d 3e 6e 43 lumn!=pIndex->nC
217d4 6f 6c 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 olumn ) continue
217d5 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b ;. for(k=0;
217d6 20 6b 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e k<pIdx->nColumn
217d7 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; k++){.
217d8 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31 20 3d const char *z1 =
217d9 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d pIdx->azColl[k]
217da 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 ;. const
217db 63 68 61 72 20 2a 7a 32 20 3d 20 70 49 6e 64 65 char *z2 = pInde
217dc 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 x->azColl[k];.
217dd 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e if( pIdx->
217de 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 6e aiColumn[k]!=pIn
217df 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d dex->aiColumn[k]
217e0 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 ) break;.
217e1 20 20 69 66 28 20 70 49 64 78 2d 3e 61 53 6f 72 if( pIdx->aSor
217e2 74 4f 72 64 65 72 5b 6b 5d 21 3d 70 49 6e 64 65 tOrder[k]!=pInde
217e3 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6b 5d x->aSortOrder[k]
217e4 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 ) break;.
217e5 20 20 69 66 28 20 7a 31 21 3d 7a 32 20 26 26 20 if( z1!=z2 &&
217e6 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a sqlite3StrICmp(z
217e7 31 2c 20 7a 32 29 20 29 20 62 72 65 61 6b 3b 0a 1, z2) ) break;.
217e8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
217e9 28 20 6b 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 ( k==pIdx->nColu
217ea 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 mn ){. if
217eb 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 ( pIdx->onError!
217ec 3d 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 =pIndex->onError
217ed 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
217ee 20 54 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 This constraint
217ef 20 63 72 65 61 74 65 73 20 74 68 65 20 73 61 6d creates the sam
217f0 65 20 69 6e 64 65 78 20 61 73 20 61 20 70 72 65 e index as a pre
217f1 76 69 6f 75 73 0a 20 20 20 20 20 20 20 20 20 20 vious.
217f2 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 73 70 ** constraint sp
217f3 65 63 69 66 69 65 64 20 73 6f 6d 65 77 68 65 72 ecified somewher
217f4 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 e in the CREATE
217f5 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e TABLE statement.
217f6 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 48 6f . ** Ho
217f7 77 65 76 65 72 20 74 68 65 20 4f 4e 20 43 4f 4e wever the ON CON
217f8 46 4c 49 43 54 20 63 6c 61 75 73 65 73 20 61 72 FLICT clauses ar
217f9 65 20 64 69 66 66 65 72 65 6e 74 2e 20 49 66 20 e different. If
217fa 62 6f 74 68 20 74 68 69 73 20 0a 20 20 20 20 20 both this .
217fb 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 ** constrai
217fc 6e 74 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 nt and the previ
217fd 6f 75 73 20 65 71 75 69 76 61 6c 65 6e 74 20 63 ous equivalent c
217fe 6f 6e 73 74 72 61 69 6e 74 20 68 61 76 65 20 65 onstraint have e
217ff 78 70 6c 69 63 69 74 0a 20 20 20 20 20 20 20 20 xplicit.
21800 20 20 2a 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 ** ON CONFLICT
21801 20 63 6c 61 75 73 65 73 20 74 68 69 73 20 69 73 clauses this is
21802 20 61 6e 20 65 72 72 6f 72 2e 20 4f 74 68 65 72 an error. Other
21803 77 69 73 65 2c 20 75 73 65 20 74 68 65 0a 20 20 wise, use the.
21804 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 6c 69 ** expli
21805 63 69 74 6c 79 20 73 70 65 63 69 66 69 65 64 20 citly specified
21806 62 65 68 61 76 69 6f 75 72 20 66 6f 72 20 74 68 behaviour for th
21807 65 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 e index..
21808 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
21809 69 66 28 20 21 28 70 49 64 78 2d 3e 6f 6e 45 72 if( !(pIdx->onEr
2180a 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 ror==OE_Default
2180b 7c 7c 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 || pIndex->onErr
2180c 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 29 20 or==OE_Default)
2180d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 ){. s
2180e 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
2180f 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 Parse, .
21810 20 20 20 20 20 20 20 20 22 63 6f 6e 66 6c 69 63 "conflic
21811 74 69 6e 67 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 ting ON CONFLICT
21812 20 63 6c 61 75 73 65 73 20 73 70 65 63 69 66 69 clauses specifi
21813 65 64 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 ed", 0);.
21814 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 }. i
21815 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 f( pIdx->onError
21816 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a ==OE_Default ){.
21817 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64 78 pIdx
21818 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 70 49 6e 64 ->onError = pInd
21819 65 78 2d 3e 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 ex->onError;.
2181a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2181b 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 }. goto
2181c 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
2181d 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d x;. }. }
2181e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 . }.. /* Link
2181f 74 68 65 20 6e 65 77 20 49 6e 64 65 78 20 73 74 the new Index st
21820 72 75 63 74 75 72 65 20 74 6f 20 69 74 73 20 74 ructure to its t
21821 61 62 6c 65 20 61 6e 64 20 74 6f 20 74 68 65 20 able and to the
21822 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e 2d 6d 65 other. ** in-me
21823 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 73 74 mory database st
21824 72 75 63 74 75 72 65 73 2e 20 0a 20 20 2a 2f 0a ructures. . */.
21825 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 if( db->init.b
21826 75 73 79 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 usy ){. Index
21827 20 2a 70 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c *p;. p = sql
21828 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 ite3HashInsert(&
21829 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d pIndex->pSchema-
2182a 3e 69 64 78 48 61 73 68 2c 20 0a 20 20 20 20 20 >idxHash, .
2182b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2182c 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 pIndex->zNa
2182d 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 me, sqlite3Strle
2182e 6e 33 30 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d n30(pIndex->zNam
2182f 65 29 2b 31 2c 0a 20 20 20 20 20 20 20 20 20 20 e)+1,.
21830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21831 70 49 6e 64 65 78 29 3b 0a 20 20 20 20 69 66 28 pIndex);. if(
21832 20 70 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 p ){. asse
21833 72 74 28 20 70 3d 3d 70 49 6e 64 65 78 20 29 3b rt( p==pIndex );
21834 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 /* Malloc must
21835 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a have failed */.
21836 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 db->malloc
21837 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 Failed = 1;.
21838 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 goto exit_crea
21839 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a te_index;. }.
2183a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d db->flags |=
2183b 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 SQLITE_InternCh
2183c 61 6e 67 65 73 3b 0a 20 20 20 20 69 66 28 20 70 anges;. if( p
2183d 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 20 20 TblName!=0 ){.
2183e 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d pIndex->tnum
2183f 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 = db->init.newT
21840 6e 75 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a num;. }. }..
21841 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e /* If the db->
21842 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30 20 74 init.busy is 0 t
21843 68 65 6e 20 63 72 65 61 74 65 20 74 68 65 20 69 hen create the i
21844 6e 64 65 78 20 6f 6e 20 64 69 73 6b 2e 20 20 54 ndex on disk. T
21845 68 69 73 0a 20 20 2a 2a 20 69 6e 76 6f 6c 76 65 his. ** involve
21846 73 20 77 72 69 74 69 6e 67 20 74 68 65 20 69 6e s writing the in
21847 64 65 78 20 69 6e 74 6f 20 74 68 65 20 6d 61 73 dex into the mas
21848 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 66 69 ter table and fi
21849 6c 6c 69 6e 67 20 69 6e 20 74 68 65 0a 20 20 2a lling in the. *
2184a 2a 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 65 * index with the
2184b 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 20 63 current table c
2184c 6f 6e 74 65 6e 74 73 2e 0a 20 20 2a 2a 0a 20 20 ontents.. **.
2184d 2a 2a 20 54 68 65 20 64 62 2d 3e 69 6e 69 74 2e ** The db->init.
2184e 62 75 73 79 20 69 73 20 30 20 77 68 65 6e 20 74 busy is 0 when t
2184f 68 65 20 75 73 65 72 20 66 69 72 73 74 20 65 6e he user first en
21850 74 65 72 73 20 61 20 43 52 45 41 54 45 20 49 4e ters a CREATE IN
21851 44 45 58 20 0a 20 20 2a 2a 20 63 6f 6d 6d 61 6e DEX . ** comman
21852 64 2e 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 d. db->init.bus
21853 79 20 69 73 20 31 20 77 68 65 6e 20 61 20 64 61 y is 1 when a da
21854 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 tabase is opened
21855 20 61 6e 64 20 0a 20 20 2a 2a 20 43 52 45 41 54 and . ** CREAT
21856 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e E INDEX statemen
21857 74 73 20 61 72 65 20 72 65 61 64 20 6f 75 74 20 ts are read out
21858 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 of the master ta
21859 62 6c 65 2e 20 20 49 6e 0a 20 20 2a 2a 20 74 68 ble. In. ** th
2185a 65 20 6c 61 74 74 65 72 20 63 61 73 65 20 74 68 e latter case th
2185b 65 20 69 6e 64 65 78 20 61 6c 72 65 61 64 79 20 e index already
2185c 65 78 69 73 74 73 20 6f 6e 20 64 69 73 6b 2c 20 exists on disk,
2185d 77 68 69 63 68 20 69 73 20 77 68 79 0a 20 20 2a which is why. *
2185e 2a 20 77 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 * we don't want
2185f 74 6f 20 72 65 63 72 65 61 74 65 20 69 74 2e 0a to recreate it..
21860 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 54 62 **. ** If pTb
21861 6c 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 61 6e lName==0 it mean
21862 73 20 74 68 69 73 20 69 6e 64 65 78 20 69 73 20 s this index is
21863 67 65 6e 65 72 61 74 65 64 20 61 73 20 61 20 70 generated as a p
21864 72 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20 rimary key. **
21865 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 or UNIQUE constr
21866 61 69 6e 74 20 6f 66 20 61 20 43 52 45 41 54 45 aint of a CREATE
21867 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
21868 2e 20 20 53 69 6e 63 65 20 74 68 65 20 74 61 62 . Since the tab
21869 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6a 75 73 74 le. ** has just
2186a 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 69 been created, i
2186b 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 61 t contains no da
2186c 74 61 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 ta and the index
2186d 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a initialization.
2186e 20 20 2a 2a 20 73 74 65 70 20 63 61 6e 20 62 65 ** step can be
2186f 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 skipped.. */.
21870 20 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 69 6e else if( db->in
21871 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a 20 20 it.busy==0 ){.
21872 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 63 Vdbe *v;. c
21873 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 har *zStmt;.
21874 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 int iMem = ++pPa
21875 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 20 20 20 rse->nMem;..
21876 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
21877 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 be(pParse);.
21878 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 if( v==0 ) goto
21879 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 exit_create_inde
2187a 78 3b 0a 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 x;... /* Crea
2187b 74 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 te the rootpage
2187c 66 6f 72 20 74 68 65 20 69 6e 64 65 78 0a 20 20 for the index.
2187d 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
2187e 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 BeginWriteOperat
2187f 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 ion(pParse, 1, i
21880 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Db);. sqlite3
21881 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21882 5f 43 72 65 61 74 65 49 6e 64 65 78 2c 20 69 44 _CreateIndex, iD
21883 62 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 2f b, iMem);.. /
21884 2a 20 47 61 74 68 65 72 20 74 68 65 20 63 6f 6d * Gather the com
21885 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 plete text of th
21886 65 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 e CREATE INDEX s
21887 74 61 74 65 6d 65 6e 74 20 69 6e 74 6f 0a 20 20 tatement into.
21888 20 20 2a 2a 20 74 68 65 20 7a 53 74 6d 74 20 76 ** the zStmt v
21889 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2f 0a 20 ariable. */.
2188a 20 20 20 69 66 28 20 70 53 74 61 72 74 20 26 26 if( pStart &&
2188b 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 2f pEnd ){. /
2188c 2a 20 41 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 * A named index
2188d 77 69 74 68 20 61 6e 20 65 78 70 6c 69 63 69 74 with an explicit
2188e 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 CREATE INDEX st
2188f 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 atement */.
21890 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 zStmt = sqlite3
21891 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 43 52 45 MPrintf(db, "CRE
21892 41 54 45 25 73 20 49 4e 44 45 58 20 25 2e 2a 73 ATE%s INDEX %.*s
21893 22 2c 0a 20 20 20 20 20 20 20 20 6f 6e 45 72 72 ",. onErr
21894 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 3f 20 22 22 or==OE_None ? ""
21895 20 3a 20 22 20 55 4e 49 51 55 45 22 2c 0a 20 20 : " UNIQUE",.
21896 20 20 20 20 20 20 70 45 6e 64 2d 3e 7a 20 2d 20 pEnd->z -
21897 70 4e 61 6d 65 2d 3e 7a 20 2b 20 31 2c 0a 20 20 pName->z + 1,.
21898 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e 7a 29 3b pName->z);
21899 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
2189a 20 20 2f 2a 20 41 6e 20 61 75 74 6f 6d 61 74 69 /* An automati
2189b 63 20 69 6e 64 65 78 20 63 72 65 61 74 65 64 20 c index created
2189c 62 79 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 by a PRIMARY KEY
2189d 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 or UNIQUE const
2189e 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 2f raint */. /
2189f 2a 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 * zStmt = sqlite
218a0 33 4d 50 72 69 6e 74 66 28 22 22 29 3b 20 2a 2f 3MPrintf(""); */
218a1 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 30 . zStmt = 0
218a2 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
218a3 41 64 64 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 Add an entry in
218a4 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 66 6f sqlite_master fo
218a5 72 20 74 68 69 73 20 69 6e 64 65 78 0a 20 20 20 r this index.
218a6 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e */. sqlite3N
218a7 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 estedParse(pPars
218a8 65 2c 20 0a 20 20 20 20 20 20 20 20 22 49 4e 53 e, . "INS
218a9 45 52 54 20 49 4e 54 4f 20 25 51 2e 25 73 20 56 ERT INTO %Q.%s V
218aa 41 4c 55 45 53 28 27 69 6e 64 65 78 27 2c 25 51 ALUES('index',%Q
218ab 2c 25 51 2c 23 25 64 2c 25 51 29 3b 22 2c 0a 20 ,%Q,#%d,%Q);",.
218ac 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 db->aDb[i
218ad 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d Db].zName, SCHEM
218ae 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 A_TABLE(iDb),.
218af 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e pIndex->zN
218b0 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 70 54 61 ame,. pTa
218b1 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 b->zName,.
218b2 20 20 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 iMem,.
218b3 7a 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 20 zStmt. );.
218b4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
218b5 62 2c 20 7a 53 74 6d 74 29 3b 0a 0a 20 20 20 20 b, zStmt);..
218b6 2f 2a 20 46 69 6c 6c 20 74 68 65 20 69 6e 64 65 /* Fill the inde
218b7 78 20 77 69 74 68 20 64 61 74 61 20 61 6e 64 20 x with data and
218b8 72 65 70 61 72 73 65 20 74 68 65 20 73 63 68 65 reparse the sche
218b9 6d 61 2e 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 ma. Code an OP_E
218ba 78 70 69 72 65 0a 20 20 20 20 2a 2a 20 74 6f 20 xpire. ** to
218bb 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 invalidate all p
218bc 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 re-compiled stat
218bd 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 ements.. */.
218be 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 20 if( pTblName
218bf 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
218c0 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 RefillIndex(pPar
218c1 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 4d 65 6d se, pIndex, iMem
218c2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
218c3 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 ChangeCookie(pPa
218c4 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 rse, iDb);.
218c5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
218c6 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 p4(v, OP_ParseSc
218c7 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c hema, iDb, 0, 0,
218c8 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 . sqlite
218c9 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 3MPrintf(db, "na
218ca 6d 65 3d 27 25 71 27 22 2c 20 70 49 6e 64 65 78 me='%q'", pIndex
218cb 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e ->zName), P4_DYN
218cc 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c AMIC);. sql
218cd 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
218ce 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 29 3b , OP_Expire, 0);
218cf 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
218d0 20 57 68 65 6e 20 61 64 64 69 6e 67 20 61 6e 20 When adding an
218d1 69 6e 64 65 78 20 74 6f 20 74 68 65 20 6c 69 73 index to the lis
218d2 74 20 6f 66 20 69 6e 64 69 63 65 73 20 66 6f 72 t of indices for
218d3 20 61 20 74 61 62 6c 65 2c 20 6d 61 6b 65 0a 20 a table, make.
218d4 20 2a 2a 20 73 75 72 65 20 61 6c 6c 20 69 6e 64 ** sure all ind
218d5 69 63 65 73 20 6c 61 62 65 6c 65 64 20 4f 45 5f ices labeled OE_
218d6 52 65 70 6c 61 63 65 20 63 6f 6d 65 20 61 66 74 Replace come aft
218d7 65 72 20 61 6c 6c 20 74 68 6f 73 65 20 6c 61 62 er all those lab
218d8 65 6c 65 64 0a 20 20 2a 2a 20 4f 45 5f 49 67 6e eled. ** OE_Ign
218d9 6f 72 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 ore. This is ne
218da 63 65 73 73 61 72 79 20 66 6f 72 20 74 68 65 20 cessary for the
218db 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f correct operatio
218dc 6e 20 6f 66 20 55 50 44 41 54 45 0a 20 20 2a 2a n of UPDATE. **
218dd 20 61 6e 64 20 49 4e 53 45 52 54 2e 0a 20 20 2a and INSERT.. *
218de 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 /. if( db->init
218df 2e 62 75 73 79 20 7c 7c 20 70 54 62 6c 4e 61 6d .busy || pTblNam
218e0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 e==0 ){. if(
218e1 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c onError!=OE_Repl
218e2 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e ace || pTab->pIn
218e3 64 65 78 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 dex==0.
218e4 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 2d || pTab->pIndex-
218e5 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 >onError==OE_Rep
218e6 6c 61 63 65 29 7b 0a 20 20 20 20 20 20 70 49 6e lace){. pIn
218e7 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61 dex->pNext = pTa
218e8 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 b->pIndex;.
218e9 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 20 3d 20 pTab->pIndex =
218ea 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d 65 6c 73 pIndex;. }els
218eb 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a e{. Index *
218ec 70 4f 74 68 65 72 20 3d 20 70 54 61 62 2d 3e 70 pOther = pTab->p
218ed 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 77 68 69 Index;. whi
218ee 6c 65 28 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 le( pOther->pNex
218ef 74 20 26 26 20 70 4f 74 68 65 72 2d 3e 70 4e 65 t && pOther->pNe
218f0 78 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f xt->onError!=OE_
218f1 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 Replace ){.
218f2 20 20 20 70 4f 74 68 65 72 20 3d 20 70 4f 74 68 pOther = pOth
218f3 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 er->pNext;.
218f4 20 7d 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d }. pIndex-
218f5 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68 65 72 2d >pNext = pOther-
218f6 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 4f >pNext;. pO
218f7 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20 70 49 ther->pNext = pI
218f8 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ndex;. }.
218f9 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 7d 0a pIndex = 0;. }.
218fa 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20 62 . /* Clean up b
218fb 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f efore exiting */
218fc 0a 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 .exit_create_ind
218fd 65 78 3a 0a 20 20 69 66 28 20 70 49 6e 64 65 78 ex:. if( pIndex
218fe 20 29 7b 0a 20 20 20 20 66 72 65 65 49 6e 64 65 ){. freeInde
218ff 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 x(pIndex);. }.
21900 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
21901 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 Delete(db, pList
21902 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c );. sqlite3SrcL
21903 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 istDelete(db, pT
21904 62 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 blName);. sqlit
21905 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 e3DbFree(db, zNa
21906 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d me);. return;.}
21907 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
21908 20 63 6f 64 65 20 74 6f 20 6d 61 6b 65 20 73 75 code to make su
21909 72 65 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d re the file form
2190a 61 74 20 6e 75 6d 62 65 72 20 69 73 20 61 74 20 at number is at
2190b 6c 65 61 73 74 20 6d 69 6e 46 6f 72 6d 61 74 2e least minFormat.
2190c 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74 65 .** The generate
2190d 64 20 63 6f 64 65 20 77 69 6c 6c 20 69 6e 63 72 d code will incr
2190e 65 61 73 65 20 74 68 65 20 66 69 6c 65 20 66 6f ease the file fo
2190f 72 6d 61 74 20 6e 75 6d 62 65 72 20 69 66 20 6e rmat number if n
21910 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c ecessary..*/.SQL
21911 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
21912 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 sqlite3MinimumF
21913 69 6c 65 46 6f 72 6d 61 74 28 50 61 72 73 65 20 ileFormat(Parse
21914 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62 *pParse, int iDb
21915 2c 20 69 6e 74 20 6d 69 6e 46 6f 72 6d 61 74 29 , int minFormat)
21916 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 76 {. Vdbe *v;. v
21917 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 = sqlite3GetVdb
21918 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 e(pParse);. if(
21919 20 76 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 v ){. int r1
2191a 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
2191b 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
2191c 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 int r2 = sqlit
2191d 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
2191e 72 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 6a 31 rse);. int j1
2191f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
21920 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65 eAddOp3(v, OP_Re
21921 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 72 adCookie, iDb, r
21922 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 1, 1);. sqlit
21923 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 e3VdbeUsesBtree(
21924 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c v, iDb);. sql
21925 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21926 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6d 69 , OP_Integer, mi
21927 6e 46 6f 72 6d 61 74 2c 20 72 32 29 3b 0a 20 20 nFormat, r2);.
21928 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 j1 = sqlite3Vd
21929 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 47 beAddOp3(v, OP_G
2192a 65 2c 20 72 32 2c 20 30 2c 20 72 31 29 3b 0a 20 e, r2, 0, r1);.
2192b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2192c 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f dOp3(v, OP_SetCo
2192d 6f 6b 69 65 2c 20 69 44 62 2c 20 31 2c 20 72 32 okie, iDb, 1, r2
2192e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
2192f 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 beJumpHere(v, j1
21930 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 );. sqlite3Re
21931 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
21932 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 73 71 rse, r1);. sq
21933 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
21934 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b Reg(pParse, r2);
21935 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 . }.}../*.** Fi
21936 6c 6c 20 74 68 65 20 49 6e 64 65 78 2e 61 69 52 ll the Index.aiR
21937 6f 77 45 73 74 5b 5d 20 61 72 72 61 79 20 77 69 owEst[] array wi
21938 74 68 20 64 65 66 61 75 6c 74 20 69 6e 66 6f 72 th default infor
21939 6d 61 74 69 6f 6e 20 2d 20 69 6e 66 6f 72 6d 61 mation - informa
2193a 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 75 73 tion.** to be us
2193b 65 64 20 77 68 65 6e 20 77 65 20 68 61 76 65 20 ed when we have
2193c 6e 6f 74 20 72 75 6e 20 74 68 65 20 41 4e 41 4c not run the ANAL
2193d 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a YZE command..**.
2193e 2a 2a 20 61 69 52 6f 77 45 73 74 5b 30 5d 20 69 ** aiRowEst[0] i
2193f 73 20 73 75 70 70 6f 73 65 20 74 6f 20 63 6f 6e s suppose to con
21940 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 tain the number
21941 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 of elements in t
21942 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 53 69 6e he index..** Sin
21943 63 65 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f ce we do not kno
21944 77 2c 20 67 75 65 73 73 20 31 20 6d 69 6c 6c 69 w, guess 1 milli
21945 6f 6e 2e 20 20 61 69 52 6f 77 45 73 74 5b 31 5d on. aiRowEst[1]
21946 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 is an estimate
21947 6f 66 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 of the.** number
21948 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 of rows in the
21949 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 table that match
2194a 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61 72 20 any particular
2194b 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 value of the.**
2194c 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 first column of
2194d 74 68 65 20 69 6e 64 65 78 2e 20 20 61 69 52 6f the index. aiRo
2194e 77 45 73 74 5b 32 5d 20 69 73 20 61 6e 20 65 73 wEst[2] is an es
2194f 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 6e 75 timate of the nu
21950 6d 62 65 72 0a 2a 2a 20 6f 66 20 72 6f 77 73 20 mber.** of rows
21951 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20 70 that match any p
21952 61 72 74 69 63 75 6c 61 72 20 63 6f 6d 62 69 6e articular combin
21953 69 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 69 iation of the fi
21954 72 73 74 20 32 20 63 6f 6c 75 6d 6e 73 0a 2a 2a rst 2 columns.**
21955 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 of the index.
21956 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 And so forth. I
21957 74 20 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 t must always be
21958 20 74 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a the case that.*
21959 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69 .** ai
2195a 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69 52 6f 77 RowEst[N]<=aiRow
2195b 45 73 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20 20 20 Est[N-1].**
2195c 20 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e aiRowEst[N
2195d 5d 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61 72 74 ]>=1.**.** Apart
2195e 20 66 72 6f 6d 20 74 68 61 74 2c 20 77 65 20 68 from that, we h
2195f 61 76 65 20 6c 69 74 74 6c 65 20 74 6f 20 67 6f ave little to go
21960 20 6f 6e 20 62 65 73 69 64 65 73 20 69 6e 74 75 on besides intu
21961 69 74 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a 20 68 ition as to.** h
21962 6f 77 20 61 69 52 6f 77 45 73 74 5b 5d 20 73 68 ow aiRowEst[] sh
21963 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 69 ould be initiali
21964 7a 65 64 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 zed. The number
21965 73 20 67 65 6e 65 72 61 74 65 64 20 68 65 72 65 s generated here
21966 0a 2a 2a 20 61 72 65 20 62 61 73 65 64 20 6f 6e .** are based on
21967 20 74 79 70 69 63 61 6c 20 76 61 6c 75 65 73 20 typical values
21968 66 6f 75 6e 64 20 69 6e 20 61 63 74 75 61 6c 20 found in actual
21969 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 indices..*/.SQLI
2196a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
2196b 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f sqlite3DefaultRo
2196c 77 45 73 74 28 49 6e 64 65 78 20 2a 70 49 64 78 wEst(Index *pIdx
2196d 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a 61 ){. unsigned *a
2196e 20 3d 20 70 49 64 78 2d 3e 61 69 52 6f 77 45 73 = pIdx->aiRowEs
2196f 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 t;. int i;. as
21970 73 65 72 74 28 20 61 21 3d 30 20 29 3b 0a 20 20 sert( a!=0 );.
21971 61 5b 30 5d 20 3d 20 31 30 30 30 30 30 30 3b 0a a[0] = 1000000;.
21972 20 20 66 6f 72 28 69 3d 70 49 64 78 2d 3e 6e 43 for(i=pIdx->nC
21973 6f 6c 75 6d 6e 3b 20 69 3e 3d 35 3b 20 69 2d 2d olumn; i>=5; i--
21974 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20 35 3b ){. a[i] = 5;
21975 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 69 3e . }. while( i>
21976 3d 31 20 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d =1 ){. a[i] =
21977 20 31 31 20 2d 20 69 3b 0a 20 20 20 20 69 2d 2d 11 - i;. i--
21978 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78 ;. }. if( pIdx
21979 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f ->onError!=OE_No
2197a 6e 65 20 29 7b 0a 20 20 20 20 61 5b 70 49 64 78 ne ){. a[pIdx
2197b 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 31 3b 0a ->nColumn] = 1;.
2197c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
2197d 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 s routine will d
2197e 72 6f 70 20 61 6e 20 65 78 69 73 74 69 6e 67 20 rop an existing
2197f 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 20 54 68 named index. Th
21980 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6d is routine.** im
21981 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 44 52 4f plements the DRO
21982 50 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e P INDEX statemen
21983 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
21984 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
21985 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 3DropIndex(Parse
21986 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 *pParse, SrcLis
21987 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 69 66 t *pName, int if
21988 45 78 69 73 74 73 29 7b 0a 20 20 49 6e 64 65 78 Exists){. Index
21989 20 2a 70 49 6e 64 65 78 3b 0a 20 20 56 64 62 65 *pIndex;. Vdbe
2198a 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a *v;. sqlite3 *
2198b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
2198c 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69 . int iDb;.. i
2198d 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 f( pParse->nErr
2198e 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
2198f 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 led ){. goto
21990 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b exit_drop_index;
21991 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
21992 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b Name->nSrc==1 );
21993 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
21994 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 !=sqlite3ReadSch
21995 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 ema(pParse) ){.
21996 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f goto exit_dro
21997 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70 p_index;. }. p
21998 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46 Index = sqlite3F
21999 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 70 4e 61 indIndex(db, pNa
2199a 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 me->a[0].zName,
2199b 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 pName->a[0].zDat
2199c 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49 abase);. if( pI
2199d 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 69 ndex==0 ){. i
2199e 66 28 20 21 69 66 45 78 69 73 74 73 20 29 7b 0a f( !ifExists ){.
2199f 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
219a0 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e orMsg(pParse, "n
219a1 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20 25 53 o such index: %S
219a2 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 ", pName, 0);.
219a3 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e }. pParse->
219a4 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b checkSchema = 1;
219a5 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 . goto exit_d
219a6 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 rop_index;. }.
219a7 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 61 75 74 if( pIndex->aut
219a8 6f 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71 oIndex ){. sq
219a9 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
219aa 61 72 73 65 2c 20 22 69 6e 64 65 78 20 61 73 73 arse, "index ass
219ab 6f 63 69 61 74 65 64 20 77 69 74 68 20 55 4e 49 ociated with UNI
219ac 51 55 45 20 22 0a 20 20 20 20 20 20 22 6f 72 20 QUE ". "or
219ad 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73 PRIMARY KEY cons
219ae 74 72 61 69 6e 74 20 63 61 6e 6e 6f 74 20 62 65 traint cannot be
219af 20 64 72 6f 70 70 65 64 22 2c 20 30 29 3b 0a 20 dropped", 0);.
219b0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f goto exit_dro
219b1 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 p_index;. }. i
219b2 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 Db = sqlite3Sche
219b3 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 49 maToIndex(db, pI
219b4 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a ndex->pSchema);.
219b5 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
219b6 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f MIT_AUTHORIZATIO
219b7 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f N. {. int co
219b8 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 de = SQLITE_DROP
219b9 5f 49 4e 44 45 58 3b 0a 20 20 20 20 54 61 62 6c _INDEX;. Tabl
219ba 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64 65 78 e *pTab = pIndex
219bb 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 63 6f ->pTable;. co
219bc 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 nst char *zDb =
219bd 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 db->aDb[iDb].zNa
219be 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 me;. const ch
219bf 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d ar *zTab = SCHEM
219c0 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 A_TABLE(iDb);.
219c1 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 if( sqlite3Aut
219c2 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 hCheck(pParse, S
219c3 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54 QLITE_DELETE, zT
219c4 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 ab, 0, zDb) ){.
219c5 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 goto exit_d
219c6 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d rop_index;. }
219c7 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 . if( !OMIT_T
219c8 45 4d 50 44 42 20 26 26 20 69 44 62 20 29 20 63 EMPDB && iDb ) c
219c9 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f ode = SQLITE_DRO
219ca 50 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a 20 20 P_TEMP_INDEX;.
219cb 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 if( sqlite3Aut
219cc 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 hCheck(pParse, c
219cd 6f 64 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 ode, pIndex->zNa
219ce 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c me, pTab->zName,
219cf 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 zDb) ){. g
219d0 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e oto exit_drop_in
219d1 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 dex;. }. }.#
219d2 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 endif.. /* Gene
219d3 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6d rate code to rem
219d4 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 61 6e ove the index an
219d5 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74 65 d from the maste
219d6 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 20 3d r table */. v =
219d7 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
219d8 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 pParse);. if( v
219d9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
219da 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 eginWriteOperati
219db 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 on(pParse, 1, iD
219dc 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e b);. sqlite3N
219dd 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 estedParse(pPars
219de 65 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45 54 e,. "DELET
219df 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 E FROM %Q.%s WHE
219e0 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 20 RE name=%Q",.
219e1 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d db->aDb[iDb]
219e2 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 .zName, SCHEMA_T
219e3 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20 20 ABLE(iDb),.
219e4 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0a pIndex->zName.
219e5 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 73 );. if( s
219e6 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 qlite3FindTable(
219e7 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 db, "sqlite_stat
219e8 31 22 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 1", db->aDb[iDb]
219e9 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 .zName) ){.
219ea 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 sqlite3NestedPa
219eb 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 rse(pParse,.
219ec 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d "DELETE FROM
219ed 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 %Q.sqlite_stat1
219ee 20 57 48 45 52 45 20 69 64 78 3d 25 51 22 2c 0a WHERE idx=%Q",.
219ef 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b db->aDb[
219f0 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 70 49 6e 64 iDb].zName, pInd
219f1 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 ex->zName.
219f2 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
219f3 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 ite3ChangeCookie
219f4 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 (pParse, iDb);.
219f5 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 destroyRootPa
219f6 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 ge(pParse, pInde
219f7 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 x->tnum, iDb);.
219f8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
219f9 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 49 dOp4(v, OP_DropI
219fa 6e 64 65 78 2c 20 69 44 62 2c 20 30 2c 20 30 2c ndex, iDb, 0, 0,
219fb 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 pIndex->zName,
219fc 30 29 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 64 72 0);. }..exit_dr
219fd 6f 70 5f 69 6e 64 65 78 3a 0a 20 20 73 71 6c 69 op_index:. sqli
219fe 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 te3SrcListDelete
219ff 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a (db, pName);.}..
21a00 2f 2a 0a 2a 2a 20 70 41 72 72 61 79 20 69 73 20 /*.** pArray is
21a01 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
21a02 61 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 array of objects
21a03 2e 20 20 45 61 63 68 20 6f 62 6a 65 63 74 20 69 . Each object i
21a04 6e 20 74 68 65 0a 2a 2a 20 61 72 72 61 79 20 69 n the.** array i
21a05 73 20 73 7a 45 6e 74 72 79 20 62 79 74 65 73 20 s szEntry bytes
21a06 69 6e 20 73 69 7a 65 2e 20 20 54 68 69 73 20 72 in size. This r
21a07 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
21a08 20 61 20 6e 65 77 0a 2a 2a 20 6f 62 6a 65 63 74 a new.** object
21a09 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 74 on the end of t
21a0a 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 he array..**.**
21a0b 2a 70 6e 45 6e 74 72 79 20 69 73 20 74 68 65 20 *pnEntry is the
21a0c 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 number of entrie
21a0d 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 73 65 s already in use
21a0e 2e 20 20 2a 70 6e 41 6c 6c 6f 63 20 69 73 0a 2a . *pnAlloc is.*
21a0f 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 * the previously
21a10 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 allocated size
21a11 6f 66 20 74 68 65 20 61 72 72 61 79 2e 20 20 69 of the array. i
21a12 6e 69 74 53 69 7a 65 20 69 73 20 74 68 65 0a 2a nitSize is the.*
21a13 2a 20 73 75 67 67 65 73 74 65 64 20 69 6e 69 74 * suggested init
21a14 69 61 6c 20 61 72 72 61 79 20 73 69 7a 65 20 61 ial array size a
21a15 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a llocation..**.**
21a16 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 The index of th
21a17 65 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 72 e new entry is r
21a18 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 49 64 78 eturned in *pIdx
21a19 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
21a1a 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 tine returns a p
21a1b 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 72 ointer to the ar
21a1c 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 ray of objects.
21a1d 20 54 68 69 73 0a 2a 2a 20 6d 69 67 68 74 20 62 This.** might b
21a1e 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 e the same as th
21a1f 65 20 70 41 72 72 61 79 20 70 61 72 61 6d 65 74 e pArray paramet
21a20 65 72 20 6f 72 20 69 74 20 6d 69 67 68 74 20 62 er or it might b
21a21 65 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a e a different.**
21a22 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68 65 20 pointer if the
21a23 61 72 72 61 79 20 77 61 73 20 72 65 73 69 7a 65 array was resize
21a24 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
21a25 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
21a26 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 e3ArrayAllocate(
21a27 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
21a28 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 /* Connecti
21a29 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 on to notify of
21a2a 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 malloc failures
21a2b 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 72 61 */. void *pArra
21a2c 79 2c 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 y, /* Array
21a2d 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 4d 69 67 of objects. Mig
21a2e 68 74 20 62 65 20 72 65 61 6c 6c 6f 63 61 74 65 ht be reallocate
21a2f 64 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 6e 74 d */. int szEnt
21a30 72 79 2c 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 ry, /* Size
21a31 20 6f 66 20 65 61 63 68 20 6f 62 6a 65 63 74 20 of each object
21a32 69 6e 20 74 68 65 20 61 72 72 61 79 20 2a 2f 0a in the array */.
21a33 20 20 69 6e 74 20 69 6e 69 74 53 69 7a 65 2c 20 int initSize,
21a34 20 20 20 20 2f 2a 20 53 75 67 67 65 73 74 65 64 /* Suggested
21a35 20 69 6e 69 74 69 61 6c 20 61 6c 6c 6f 63 61 74 initial allocat
21a36 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65 6e 74 73 ion, in elements
21a37 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 6e 74 */. int *pnEnt
21a38 72 79 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ry, /* Numbe
21a39 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 63 75 72 r of objects cur
21a3a 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f rently in use */
21a3b 0a 20 20 69 6e 74 20 2a 70 6e 41 6c 6c 6f 63 2c . int *pnAlloc,
21a3c 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
21a3d 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f size of the allo
21a3e 63 61 74 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65 cation, in eleme
21a3f 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 49 nts */. int *pI
21a40 64 78 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 dx /* Wr
21a41 69 74 65 20 74 68 65 20 69 6e 64 65 78 20 6f 66 ite the index of
21a42 20 61 20 6e 65 77 20 73 6c 6f 74 20 68 65 72 65 a new slot here
21a43 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a */.){. char *z
21a44 3b 0a 20 20 69 66 28 20 2a 70 6e 45 6e 74 72 79 ;. if( *pnEntry
21a45 20 3e 3d 20 2a 70 6e 41 6c 6c 6f 63 20 29 7b 0a >= *pnAlloc ){.
21a46 20 20 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a void *pNew;.
21a47 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b int newSize;
21a48 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 28 . newSize = (
21a49 2a 70 6e 41 6c 6c 6f 63 29 2a 32 20 2b 20 69 6e *pnAlloc)*2 + in
21a4a 69 74 53 69 7a 65 3b 0a 20 20 20 20 70 4e 65 77 itSize;. pNew
21a4b 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c = sqlite3DbReal
21a4c 6c 6f 63 28 64 62 2c 20 70 41 72 72 61 79 2c 20 loc(db, pArray,
21a4d 6e 65 77 53 69 7a 65 2a 73 7a 45 6e 74 72 79 29 newSize*szEntry)
21a4e 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d ;. if( pNew==
21a4f 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 49 64 78 0 ){. *pIdx
21a50 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 = -1;. ret
21a51 75 72 6e 20 70 41 72 72 61 79 3b 0a 20 20 20 20 urn pArray;.
21a52 7d 0a 20 20 20 20 2a 70 6e 41 6c 6c 6f 63 20 3d }. *pnAlloc =
21a53 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
21a54 53 69 7a 65 28 64 62 2c 20 70 4e 65 77 29 2f 73 Size(db, pNew)/s
21a55 7a 45 6e 74 72 79 3b 0a 20 20 20 20 70 41 72 72 zEntry;. pArr
21a56 61 79 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 ay = pNew;. }.
21a57 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 41 72 72 z = (char*)pArr
21a58 61 79 3b 0a 20 20 6d 65 6d 73 65 74 28 26 7a 5b ay;. memset(&z[
21a59 2a 70 6e 45 6e 74 72 79 20 2a 20 73 7a 45 6e 74 *pnEntry * szEnt
21a5a 72 79 5d 2c 20 30 2c 20 73 7a 45 6e 74 72 79 29 ry], 0, szEntry)
21a5b 3b 0a 20 20 2a 70 49 64 78 20 3d 20 2a 70 6e 45 ;. *pIdx = *pnE
21a5c 6e 74 72 79 3b 0a 20 20 2b 2b 2a 70 6e 45 6e 74 ntry;. ++*pnEnt
21a5d 72 79 3b 0a 20 20 72 65 74 75 72 6e 20 70 41 72 ry;. return pAr
21a5e 72 61 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 ray;.}../*.** Ap
21a5f 70 65 6e 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 pend a new eleme
21a60 6e 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 nt to the given
21a61 49 64 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20 IdList. Create
21a62 61 20 6e 65 77 20 49 64 4c 69 73 74 20 69 66 0a a new IdList if.
21a63 2a 2a 20 6e 65 65 64 20 62 65 2e 0a 2a 2a 0a 2a ** need be..**.*
21a64 2a 20 41 20 6e 65 77 20 49 64 4c 69 73 74 20 69 * A new IdList i
21a65 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e s returned, or N
21a66 55 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 ULL if malloc()
21a67 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fails..*/.SQLITE
21a68 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 _PRIVATE IdList
21a69 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70 *sqlite3IdListAp
21a6a 70 65 6e 64 28 73 71 6c 69 74 65 33 20 2a 64 62 pend(sqlite3 *db
21a6b 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c , IdList *pList,
21a6c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b Token *pToken){
21a6d 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
21a6e 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 pList==0 ){.
21a6f 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 pList = sqlite3D
21a70 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
21a71 73 69 7a 65 6f 66 28 49 64 4c 69 73 74 29 20 29 sizeof(IdList) )
21a72 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d ;. if( pList=
21a73 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
21a74 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 pList->nAlloc
21a75 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 = 0;. }. pLis
21a76 74 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 41 72 t->a = sqlite3Ar
21a77 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 rayAllocate(.
21a78 20 20 20 64 62 2c 0a 20 20 20 20 20 20 70 4c 69 db,. pLi
21a79 73 74 2d 3e 61 2c 0a 20 20 20 20 20 20 73 69 7a st->a,. siz
21a7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 eof(pList->a[0])
21a7b 2c 0a 20 20 20 20 20 20 35 2c 0a 20 20 20 20 20 ,. 5,.
21a7c 20 26 70 4c 69 73 74 2d 3e 6e 49 64 2c 0a 20 20 &pList->nId,.
21a7d 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e 41 6c 6c &pList->nAll
21a7e 6f 63 2c 0a 20 20 20 20 20 20 26 69 0a 20 20 29 oc,. &i. )
21a7f 3b 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a 20 ;. if( i<0 ){.
21a80 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 sqlite3IdList
21a81 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 Delete(db, pList
21a82 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
21a83 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 5b . }. pList->a[
21a84 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 i].zName = sqlit
21a85 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
21a86 64 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 72 db, pToken);. r
21a87 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a eturn pList;.}..
21a88 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 /*.** Delete an
21a89 49 64 4c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 IdList..*/.SQLIT
21a8a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21a8b 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 qlite3IdListDele
21a8c 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 te(sqlite3 *db,
21a8d 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a IdList *pList){.
21a8e 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 int i;. if( p
21a8f 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e List==0 ) return
21a90 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
21a91 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b List->nId; i++){
21a92 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
21a93 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b ee(db, pList->a[
21a94 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 i].zName);. }.
21a95 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
21a96 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 b, pList->a);.
21a97 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
21a98 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a , pList);.}../*.
21a99 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e ** Return the in
21a9a 64 65 78 20 69 6e 20 70 4c 69 73 74 20 6f 66 20 dex in pList of
21a9b 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 20 6e the identifier n
21a9c 61 6d 65 64 20 7a 49 64 2e 20 20 52 65 74 75 72 amed zId. Retur
21a9d 6e 20 2d 31 0a 2a 2a 20 69 66 20 6e 6f 74 20 66 n -1.** if not f
21a9e 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ound..*/.SQLITE_
21a9f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
21aa0 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49 te3IdListIndex(I
21aa1 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 63 6f dList *pList, co
21aa2 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 nst char *zName)
21aa3 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 {. int i;. if(
21aa4 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 pList==0 ) retu
21aa5 72 6e 20 2d 31 3b 0a 20 20 66 6f 72 28 69 3d 30 rn -1;. for(i=0
21aa6 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 ; i<pList->nId;
21aa7 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 i++){. if( sq
21aa8 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 4c 69 lite3StrICmp(pLi
21aa9 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 st->a[i].zName,
21aaa 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 72 65 74 75 zName)==0 ) retu
21aab 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rn i;. }. retu
21aac 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn -1;.}../*.**
21aad 45 78 70 61 6e 64 20 74 68 65 20 73 70 61 63 65 Expand the space
21aae 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 allocated for t
21aaf 68 65 20 67 69 76 65 6e 20 53 72 63 4c 69 73 74 he given SrcList
21ab0 20 6f 62 6a 65 63 74 20 62 79 0a 2a 2a 20 63 72 object by.** cr
21ab1 65 61 74 69 6e 67 20 6e 45 78 74 72 61 20 6e 65 eating nExtra ne
21ab2 77 20 73 6c 6f 74 73 20 62 65 67 69 6e 6e 69 6e w slots beginnin
21ab3 67 20 61 74 20 69 53 74 61 72 74 2e 20 20 69 53 g at iStart. iS
21ab4 74 61 72 74 20 69 73 20 7a 65 72 6f 20 62 61 73 tart is zero bas
21ab5 65 64 2e 0a 2a 2a 20 4e 65 77 20 73 6c 6f 74 73 ed..** New slots
21ab6 20 61 72 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a are zeroed..**.
21ab7 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 ** For example,
21ab8 73 75 70 70 6f 73 65 20 61 20 53 72 63 4c 69 73 suppose a SrcLis
21ab9 74 20 69 6e 69 74 69 61 6c 6c 79 20 63 6f 6e 74 t initially cont
21aba 61 69 6e 73 20 74 77 6f 20 65 6e 74 72 69 65 73 ains two entries
21abb 3a 20 41 2c 42 2e 0a 2a 2a 20 54 6f 20 61 70 70 : A,B..** To app
21abc 65 6e 64 20 33 20 6e 65 77 20 65 6e 74 72 69 65 end 3 new entrie
21abd 73 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 2c 20 s onto the end,
21abe 64 6f 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 do this:.**.**
21abf 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 sqlite3SrcList
21ac0 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 53 72 63 Enlarge(db, pSrc
21ac1 6c 69 73 74 2c 20 33 2c 20 32 29 3b 0a 2a 2a 0a list, 3, 2);.**.
21ac2 2a 2a 20 41 66 74 65 72 20 74 68 65 20 63 61 6c ** After the cal
21ac3 6c 20 61 62 6f 76 65 20 69 74 20 77 6f 75 6c 64 l above it would
21ac4 20 63 6f 6e 74 61 69 6e 3a 20 20 41 2c 20 42 2c contain: A, B,
21ac5 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2e 0a nil, nil, nil..
21ac6 2a 2a 20 49 66 20 74 68 65 20 69 53 74 61 72 74 ** If the iStart
21ac7 20 61 72 67 75 6d 65 6e 74 20 68 61 64 20 62 65 argument had be
21ac8 65 6e 20 31 20 69 6e 73 74 65 61 64 20 6f 66 20 en 1 instead of
21ac9 32 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 2, then the resu
21aca 6c 74 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 lt.** would have
21acb 20 62 65 65 6e 3a 20 20 41 2c 20 6e 69 6c 2c 20 been: A, nil,
21acc 6e 69 6c 2c 20 6e 69 6c 2c 20 42 2e 20 20 54 6f nil, nil, B. To
21acd 20 70 72 65 70 65 6e 64 20 74 68 65 20 6e 65 77 prepend the new
21ace 20 73 6c 6f 74 73 2c 0a 2a 2a 20 74 68 65 20 69 slots,.** the i
21acf 53 74 61 72 74 20 76 61 6c 75 65 20 77 6f 75 6c Start value woul
21ad0 64 20 62 65 20 30 2e 20 20 54 68 65 20 72 65 73 d be 0. The res
21ad1 75 6c 74 20 74 68 65 6e 20 77 6f 75 6c 64 0a 2a ult then would.*
21ad2 2a 20 62 65 3a 20 6e 69 6c 2c 20 6e 69 6c 2c 20 * be: nil, nil,
21ad3 6e 69 6c 2c 20 41 2c 20 42 2e 0a 2a 2a 0a 2a 2a nil, A, B..**.**
21ad4 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c If a memory all
21ad5 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 74 68 ocation fails th
21ad6 65 20 53 72 63 4c 69 73 74 20 69 73 20 75 6e 63 e SrcList is unc
21ad7 68 61 6e 67 65 64 2e 20 20 54 68 65 0a 2a 2a 20 hanged. The.**
21ad8 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
21ad9 20 66 6c 61 67 20 77 69 6c 6c 20 62 65 20 73 65 flag will be se
21ada 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 t to true..*/.SQ
21adb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 LITE_PRIVATE Src
21adc 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 List *sqlite3Src
21add 4c 69 73 74 45 6e 6c 61 72 67 65 28 0a 20 20 73 ListEnlarge(. s
21ade 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
21adf 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f /* Database co
21ae0 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 nnection to noti
21ae1 66 79 20 6f 66 20 4f 4f 4d 20 65 72 72 6f 72 73 fy of OOM errors
21ae2 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 */. SrcList *p
21ae3 53 72 63 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 Src, /* The
21ae4 53 72 63 4c 69 73 74 20 74 6f 20 62 65 20 65 6e SrcList to be en
21ae5 6c 61 72 67 65 64 20 2a 2f 0a 20 20 69 6e 74 20 larged */. int
21ae6 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 2f nExtra, /
21ae7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 77 20 * Number of new
21ae8 73 6c 6f 74 73 20 74 6f 20 61 64 64 20 74 6f 20 slots to add to
21ae9 70 53 72 63 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 69 pSrc->a[] */. i
21aea 6e 74 20 69 53 74 61 72 74 20 20 20 20 20 20 20 nt iStart
21aeb 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 53 /* Index in pS
21aec 72 63 2d 3e 61 5b 5d 20 6f 66 20 66 69 72 73 74 rc->a[] of first
21aed 20 6e 65 77 20 73 6c 6f 74 20 2a 2f 0a 29 7b 0a new slot */.){.
21aee 20 20 69 6e 74 20 69 3b 0a 0a 20 20 2f 2a 20 53 int i;.. /* S
21aef 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f anity checking o
21af0 6e 20 63 61 6c 6c 69 6e 67 20 70 61 72 61 6d 65 n calling parame
21af1 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 ters */. assert
21af2 28 20 69 53 74 61 72 74 3e 3d 30 20 29 3b 0a 20 ( iStart>=0 );.
21af3 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61 3e assert( nExtra>
21af4 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 53 72 63 =1 );. if( pSrc
21af5 3d 3d 30 20 7c 7c 20 69 53 74 61 72 74 3e 70 53 ==0 || iStart>pS
21af6 72 63 2d 3e 6e 53 72 63 20 29 7b 0a 20 20 20 20 rc->nSrc ){.
21af7 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c assert( db->mall
21af8 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 ocFailed );.
21af9 72 65 74 75 72 6e 20 70 53 72 63 3b 0a 20 20 7d return pSrc;. }
21afa 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 .. /* Allocate
21afb 61 64 64 69 74 69 6f 6e 61 6c 20 73 70 61 63 65 additional space
21afc 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 if needed */.
21afd 69 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e if( pSrc->nSrc+n
21afe 45 78 74 72 61 3e 70 53 72 63 2d 3e 6e 41 6c 6c Extra>pSrc->nAll
21aff 6f 63 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 oc ){. SrcLis
21b00 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 t *pNew;. int
21b01 20 6e 41 6c 6c 6f 63 20 3d 20 70 53 72 63 2d 3e nAlloc = pSrc->
21b02 6e 53 72 63 2b 6e 45 78 74 72 61 3b 0a 20 20 20 nSrc+nExtra;.
21b03 20 69 6e 74 20 6e 47 6f 74 3b 0a 20 20 20 20 70 int nGot;. p
21b04 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 New = sqlite3DbR
21b05 65 61 6c 6c 6f 63 28 64 62 2c 20 70 53 72 63 2c ealloc(db, pSrc,
21b06 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
21b07 73 69 7a 65 6f 66 28 2a 70 53 72 63 29 20 2b 20 sizeof(*pSrc) +
21b08 28 6e 41 6c 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f (nAlloc-1)*sizeo
21b09 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 20 29 3b f(pSrc->a[0]) );
21b0a 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 . if( pNew==0
21b0b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
21b0c 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
21b0d 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ed );. retu
21b0e 72 6e 20 70 53 72 63 3b 0a 20 20 20 20 7d 0a 20 rn pSrc;. }.
21b0f 20 20 20 70 53 72 63 20 3d 20 70 4e 65 77 3b 0a pSrc = pNew;.
21b10 20 20 20 20 6e 47 6f 74 20 3d 20 28 73 71 6c 69 nGot = (sqli
21b11 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
21b12 64 62 2c 20 70 4e 65 77 29 20 2d 20 73 69 7a 65 db, pNew) - size
21b13 6f 66 28 2a 70 53 72 63 29 29 2f 73 69 7a 65 6f of(*pSrc))/sizeo
21b14 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2b 31 3b f(pSrc->a[0])+1;
21b15 0a 20 20 20 20 70 53 72 63 2d 3e 6e 41 6c 6c 6f . pSrc->nAllo
21b16 63 20 3d 20 28 75 31 36 29 6e 47 6f 74 3b 0a 20 c = (u16)nGot;.
21b17 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 65 78 }.. /* Move ex
21b18 69 73 74 69 6e 67 20 73 6c 6f 74 73 20 74 68 61 isting slots tha
21b19 74 20 63 6f 6d 65 20 61 66 74 65 72 20 74 68 65 t come after the
21b1a 20 6e 65 77 6c 79 20 69 6e 73 65 72 74 65 64 20 newly inserted
21b1b 73 6c 6f 74 73 0a 20 20 2a 2a 20 6f 75 74 20 6f slots. ** out o
21b1c 66 20 74 68 65 20 77 61 79 20 2a 2f 0a 20 20 66 f the way */. f
21b1d 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2d or(i=pSrc->nSrc-
21b1e 31 3b 20 69 3e 3d 69 53 74 61 72 74 3b 20 69 2d 1; i>=iStart; i-
21b1f 2d 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b -){. pSrc->a[
21b20 69 2b 6e 45 78 74 72 61 5d 20 3d 20 70 53 72 63 i+nExtra] = pSrc
21b21 2d 3e 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 70 53 ->a[i];. }. pS
21b22 72 63 2d 3e 6e 53 72 63 20 2b 3d 20 28 69 31 36 rc->nSrc += (i16
21b23 29 6e 45 78 74 72 61 3b 0a 0a 20 20 2f 2a 20 5a )nExtra;.. /* Z
21b24 65 72 6f 20 74 68 65 20 6e 65 77 6c 79 20 61 6c ero the newly al
21b25 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 2a 2f located slots */
21b26 0a 20 20 6d 65 6d 73 65 74 28 26 70 53 72 63 2d . memset(&pSrc-
21b27 3e 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c 20 73 >a[iStart], 0, s
21b28 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d izeof(pSrc->a[0]
21b29 29 2a 6e 45 78 74 72 61 29 3b 0a 20 20 66 6f 72 )*nExtra);. for
21b2a 28 69 3d 69 53 74 61 72 74 3b 20 69 3c 69 53 74 (i=iStart; i<iSt
21b2b 61 72 74 2b 6e 45 78 74 72 61 3b 20 69 2b 2b 29 art+nExtra; i++)
21b2c 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 5d {. pSrc->a[i]
21b2d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 .iCursor = -1;.
21b2e 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 }.. /* Return
21b2f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
21b30 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c 69 73 enlarged SrcLis
21b31 74 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 53 t */. return pS
21b32 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 70 rc;.}.../*.** Ap
21b33 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62 6c 65 pend a new table
21b34 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67 69 76 name to the giv
21b35 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43 72 65 en SrcList. Cre
21b36 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c 69 73 ate a new SrcLis
21b37 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65 2e t if.** need be.
21b38 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73 A new entry is
21b39 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 created in the
21b3a 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69 66 20 SrcList even if
21b3b 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 2e 0a pToken is NULL..
21b3c 2a 2a 0a 2a 2a 20 41 20 53 72 63 4c 69 73 74 20 **.** A SrcList
21b3d 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 is returned, or
21b3e 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 NULL if there is
21b3f 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 an OOM error.
21b40 54 68 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 The returned.**
21b41 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 62 65 SrcList might be
21b42 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 the same as the
21b43 20 53 72 63 4c 69 73 74 20 74 68 61 74 20 77 61 SrcList that wa
21b44 73 20 69 6e 70 75 74 20 6f 72 20 69 74 20 6d 69 s input or it mi
21b45 67 68 74 20 62 65 0a 2a 2a 20 61 20 6e 65 77 20 ght be.** a new
21b46 6f 6e 65 2e 20 20 49 66 20 61 6e 20 4f 4f 4d 20 one. If an OOM
21b47 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63 75 72 error does occur
21b48 73 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f s, then the prio
21b49 72 20 76 61 6c 75 65 20 6f 66 20 70 4c 69 73 74 r value of pList
21b4a 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 70 75 .** that is inpu
21b4b 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e t to this routin
21b4c 65 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c e is automatical
21b4d 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 ly freed..**.**
21b4e 49 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20 If pDatabase is
21b4f 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d 65 61 not null, it mea
21b50 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 62 6c ns that the tabl
21b51 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f 6e 61 e has an optiona
21b52 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6e 61 l.** database na
21b53 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69 6b 65 me prefix. Like
21b54 20 74 68 69 73 3a 20 20 22 64 61 74 61 62 61 73 this: "databas
21b55 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65 20 70 e.table". The p
21b56 44 61 74 61 62 61 73 65 0a 2a 2a 20 70 6f 69 6e Database.** poin
21b57 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 ts to the table
21b58 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70 54 61 name and the pTa
21b59 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 ble points to th
21b5a 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2e e database name.
21b5b 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69 73 74 2e .** The SrcList.
21b5c 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c 64 20 a[].zName field
21b5d 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 is filled with t
21b5e 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 68 he table name wh
21b5f 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d ich might.** com
21b60 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 20 28 69 e from pTable (i
21b61 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20 4e f pDatabase is N
21b62 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20 70 44 61 ULL) or from pDa
21b63 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20 53 72 63 tabase. .** Src
21b64 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 62 61 List.a[].zDataba
21b65 73 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 se is filled wit
21b66 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e h the database n
21b67 61 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 2c ame from pTable,
21b68 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e 55 4c 4c .** or with NULL
21b69 20 69 66 20 6e 6f 20 64 61 74 61 62 61 73 65 20 if no database
21b6a 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a is specified..**
21b6b 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 .** In other wor
21b6c 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c 69 6b 65 ds, if call like
21b6d 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 this:.**.**
21b6e 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c sqlite3SrcL
21b6f 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c istAppend(D,A,B,
21b70 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 42 0);.**.** Then B
21b71 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 is a table name
21b72 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 and the databas
21b73 65 20 6e 61 6d 65 20 69 73 20 75 6e 73 70 65 63 e name is unspec
21b74 69 66 69 65 64 2e 20 20 49 66 20 63 61 6c 6c 65 ified. If calle
21b75 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a d.** like this:.
21b76 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71 **.** sq
21b77 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 lite3SrcListAppe
21b78 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a 2a 0a nd(D,A,B,C);.**.
21b79 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74 68 65 ** Then C is the
21b7a 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 table name and
21b7b 42 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 B is the databas
21b7c 65 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 e name..*/.SQLIT
21b7d 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 E_PRIVATE SrcLis
21b7e 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 t *sqlite3SrcLis
21b7f 74 41 70 70 65 6e 64 28 0a 20 20 73 71 6c 69 74 tAppend(. sqlit
21b80 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f e3 *db, /
21b81 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 * Connection to
21b82 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 notify of malloc
21b83 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 53 failures */. S
21b84 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 rcList *pList,
21b85 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 6f 20 /* Append to
21b86 74 68 69 73 20 53 72 63 4c 69 73 74 2e 20 4e 55 this SrcList. NU
21b87 4c 4c 20 63 72 65 61 74 65 73 20 61 20 6e 65 77 LL creates a new
21b88 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 54 6f SrcList */. To
21b89 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 ken *pTable,
21b8a 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 61 70 /* Table to ap
21b8b 70 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 pend */. Token
21b8c 2a 70 44 61 74 61 62 61 73 65 20 20 20 20 2f 2a *pDatabase /*
21b8d 20 44 61 74 61 62 61 73 65 20 6f 66 20 74 68 65 Database of the
21b8e 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 table */.){. s
21b8f 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
21b90 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 em *pItem;. if(
21b91 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 pList==0 ){.
21b92 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 pList = sqlite3
21b93 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
21b94 20 73 69 7a 65 6f 66 28 53 72 63 4c 69 73 74 29 sizeof(SrcList)
21b95 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 );. if( pLis
21b96 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b t==0 ) return 0;
21b97 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c . pList->nAll
21b98 6f 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 4c oc = 1;. }. pL
21b99 69 73 74 20 3d 20 73 71 6c 69 74 65 33 53 72 63 ist = sqlite3Src
21b9a 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62 2c 20 ListEnlarge(db,
21b9b 70 4c 69 73 74 2c 20 31 2c 20 70 4c 69 73 74 2d pList, 1, pList-
21b9c 3e 6e 53 72 63 29 3b 0a 20 20 69 66 28 20 64 62 >nSrc);. if( db
21b9d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
21b9e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 {. sqlite3Src
21b9f 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
21ba0 4c 69 73 74 29 3b 0a 20 20 20 20 72 65 74 75 72 List);. retur
21ba1 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d n 0;. }. pItem
21ba2 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 = &pList->a[pLi
21ba3 73 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 69 st->nSrc-1];. i
21ba4 66 28 20 70 44 61 74 61 62 61 73 65 20 26 26 20 f( pDatabase &&
21ba5 70 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d 30 20 pDatabase->z==0
21ba6 29 7b 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 ){. pDatabase
21ba7 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 0;. }. if(
21ba8 70 44 61 74 61 62 61 73 65 20 26 26 20 70 54 61 pDatabase && pTa
21ba9 62 6c 65 20 29 7b 0a 20 20 20 20 54 6f 6b 65 6e ble ){. Token
21baa 20 2a 70 54 65 6d 70 20 3d 20 70 44 61 74 61 62 *pTemp = pDatab
21bab 61 73 65 3b 0a 20 20 20 20 70 44 61 74 61 62 61 ase;. pDataba
21bac 73 65 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 20 se = pTable;.
21bad 20 70 54 61 62 6c 65 20 3d 20 70 54 65 6d 70 3b pTable = pTemp;
21bae 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e . }. pItem->zN
21baf 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d ame = sqlite3Nam
21bb0 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 eFromToken(db, p
21bb1 54 61 62 6c 65 29 3b 0a 20 20 70 49 74 65 6d 2d Table);. pItem-
21bb2 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c >zDatabase = sql
21bb3 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
21bb4 6e 28 64 62 2c 20 70 44 61 74 61 62 61 73 65 29 n(db, pDatabase)
21bb5 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 ;. return pList
21bb6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 ;.}../*.** Assig
21bb7 6e 20 56 64 62 65 43 75 72 73 6f 72 20 69 6e 64 n VdbeCursor ind
21bb8 65 78 20 6e 75 6d 62 65 72 73 20 74 6f 20 61 6c ex numbers to al
21bb9 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 20 53 72 l tables in a Sr
21bba 63 4c 69 73 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f cList.*/.SQLITE_
21bbb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
21bbc 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 ite3SrcListAssig
21bbd 6e 43 75 72 73 6f 72 73 28 50 61 72 73 65 20 2a nCursors(Parse *
21bbe 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 pParse, SrcList
21bbf 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 *pList){. int i
21bc0 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 ;. struct SrcLi
21bc1 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
21bc2 20 20 61 73 73 65 72 74 28 70 4c 69 73 74 20 7c assert(pList |
21bc3 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 | pParse->db->ma
21bc4 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
21bc5 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 if( pList ){.
21bc6 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d for(i=0, pItem=
21bc7 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 pList->a; i<pLis
21bc8 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 t->nSrc; i++, pI
21bc9 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 tem++){. if
21bca 28 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 ( pItem->iCursor
21bcb 3e 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 >=0 ) break;.
21bcc 20 20 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f pItem->iCurso
21bcd 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 r = pParse->nTab
21bce 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 ++;. if( pI
21bcf 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a tem->pSelect ){.
21bd0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 sqlite3S
21bd1 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 rcListAssignCurs
21bd2 6f 72 73 28 70 50 61 72 73 65 2c 20 70 49 74 65 ors(pParse, pIte
21bd3 6d 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 m->pSelect->pSrc
21bd4 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
21bd5 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 . }.}../*.** De
21bd6 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 53 lete an entire S
21bd7 72 63 4c 69 73 74 20 69 6e 63 6c 75 64 69 6e 67 rcList including
21bd8 20 61 6c 6c 20 69 74 73 20 73 75 62 73 74 72 75 all its substru
21bd9 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cture..*/.SQLITE
21bda 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
21bdb 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 lite3SrcListDele
21bdc 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 te(sqlite3 *db,
21bdd 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b SrcList *pList){
21bde 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 . int i;. stru
21bdf 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
21be0 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c *pItem;. if( pL
21be1 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ist==0 ) return;
21be2 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 . for(pItem=pLi
21be3 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c st->a, i=0; i<pL
21be4 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 ist->nSrc; i++,
21be5 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 pItem++){. sq
21be6 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
21be7 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 pItem->zDatabase
21be8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
21be9 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e Free(db, pItem->
21bea 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 zName);. sqli
21beb 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 te3DbFree(db, pI
21bec 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 tem->zAlias);.
21bed 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
21bee 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 db, pItem->zInde
21bef 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 x);. sqlite3D
21bf0 65 6c 65 74 65 54 61 62 6c 65 28 70 49 74 65 6d eleteTable(pItem
21bf1 2d 3e 70 54 61 62 29 3b 0a 20 20 20 20 73 71 6c ->pTab);. sql
21bf2 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 ite3SelectDelete
21bf3 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c (db, pItem->pSel
21bf4 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ect);. sqlite
21bf5 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
21bf6 70 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 20 20 pItem->pOn);.
21bf7 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 sqlite3IdListDe
21bf8 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e lete(db, pItem->
21bf9 70 55 73 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 73 pUsing);. }. s
21bfa 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
21bfb 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pList);.}../*.*
21bfc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
21bfd 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 s called by the
21bfe 70 61 72 73 65 72 20 74 6f 20 61 64 64 20 61 20 parser to add a
21bff 6e 65 77 20 74 65 72 6d 20 74 6f 20 74 68 65 0a new term to the.
21c00 2a 2a 20 65 6e 64 20 6f 66 20 61 20 67 72 6f 77 ** end of a grow
21c01 69 6e 67 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e ing FROM clause.
21c02 20 20 54 68 65 20 22 70 22 20 70 61 72 61 6d 65 The "p" parame
21c03 74 65 72 20 69 73 20 74 68 65 20 70 61 72 74 20 ter is the part
21c04 6f 66 0a 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 of.** the FROM c
21c05 6c 61 75 73 65 20 74 68 61 74 20 68 61 73 20 61 lause that has a
21c06 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6e 73 lready been cons
21c07 74 72 75 63 74 65 64 2e 20 20 22 70 22 20 69 73 tructed. "p" is
21c08 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 74 68 69 73 NULL.** if this
21c09 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74 65 is the first te
21c0a 72 6d 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 rm of the FROM c
21c0b 6c 61 75 73 65 2e 20 20 70 54 61 62 6c 65 20 61 lause. pTable a
21c0c 6e 64 20 70 44 61 74 61 62 61 73 65 0a 2a 2a 20 nd pDatabase.**
21c0d 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 are the name of
21c0e 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 64 61 the table and da
21c0f 74 61 62 61 73 65 20 6e 61 6d 65 64 20 69 6e 20 tabase named in
21c10 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 the FROM clause
21c11 74 65 72 6d 2e 0a 2a 2a 20 70 44 61 74 61 62 61 term..** pDataba
21c12 73 65 20 69 73 20 4e 55 4c 4c 20 69 66 20 74 68 se is NULL if th
21c13 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 e database name
21c14 71 75 61 6c 69 66 69 65 72 20 69 73 20 6d 69 73 qualifier is mis
21c15 73 69 6e 67 20 2d 20 74 68 65 0a 2a 2a 20 75 73 sing - the.** us
21c16 75 61 6c 20 63 61 73 65 2e 20 20 49 66 20 74 68 ual case. If th
21c17 65 20 74 65 72 6d 20 68 61 73 20 61 20 61 6c 69 e term has a ali
21c18 61 73 2c 20 74 68 65 6e 20 70 41 6c 69 61 73 20 as, then pAlias
21c19 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a points to the.**
21c1a 20 61 6c 69 61 73 20 74 6f 6b 65 6e 2e 20 20 49 alias token. I
21c1b 66 20 74 68 65 20 74 65 72 6d 20 69 73 20 61 20 f the term is a
21c1c 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e 20 70 subquery, then p
21c1d 53 75 62 71 75 65 72 79 20 69 73 20 74 68 65 0a Subquery is the.
21c1e 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d ** SELECT statem
21c1f 65 6e 74 20 74 68 61 74 20 74 68 65 20 73 75 62 ent that the sub
21c20 71 75 65 72 79 20 65 6e 63 6f 64 65 73 2e 20 20 query encodes.
21c21 54 68 65 20 70 54 61 62 6c 65 20 61 6e 64 0a 2a The pTable and.*
21c22 2a 20 70 44 61 74 61 62 61 73 65 20 70 61 72 61 * pDatabase para
21c23 6d 65 74 65 72 73 20 61 72 65 20 4e 55 4c 4c 20 meters are NULL
21c24 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 2e 20 for subqueries.
21c25 20 54 68 65 20 70 4f 6e 20 61 6e 64 20 70 55 73 The pOn and pUs
21c26 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 ing.** parameter
21c27 73 20 61 72 65 20 74 68 65 20 63 6f 6e 74 65 6e s are the conten
21c28 74 20 6f 66 20 74 68 65 20 4f 4e 20 61 6e 64 20 t of the ON and
21c29 55 53 49 4e 47 20 63 6c 61 75 73 65 73 2e 0a 2a USING clauses..*
21c2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6e 65 *.** Return a ne
21c2b 77 20 53 72 63 4c 69 73 74 20 77 68 69 63 68 20 w SrcList which
21c2c 65 6e 63 6f 64 65 73 20 69 73 20 74 68 65 20 46 encodes is the F
21c2d 52 4f 4d 20 77 69 74 68 20 74 68 65 20 6e 65 77 ROM with the new
21c2e 0a 2a 2a 20 74 65 72 6d 20 61 64 64 65 64 2e 0a .** term added..
21c2f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21c30 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 E SrcList *sqlit
21c31 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 e3SrcListAppendF
21c32 72 6f 6d 54 65 72 6d 28 0a 20 20 50 61 72 73 65 romTerm(. Parse
21c33 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
21c34 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
21c35 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 ntext */. SrcLi
21c36 73 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 st *p,
21c37 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 70 /* The left p
21c38 61 72 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 art of the FROM
21c39 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79 20 73 clause already s
21c3a 65 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a een */. Token *
21c3b 70 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 pTable,
21c3c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
21c3d 74 61 62 6c 65 20 74 6f 20 61 64 64 20 74 6f 20 table to add to
21c3e 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 the FROM clause
21c3f 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61 74 */. Token *pDat
21c40 61 62 61 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 abase, /*
21c41 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 Name of the data
21c42 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 base containing
21c43 70 54 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 pTable */. Toke
21c44 6e 20 2a 70 41 6c 69 61 73 2c 20 20 20 20 20 20 n *pAlias,
21c45 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 /* The right
21c46 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 -hand side of th
21c47 65 20 41 53 20 73 75 62 65 78 70 72 65 73 73 69 e AS subexpressi
21c48 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a on */. Select *
21c49 70 53 75 62 71 75 65 72 79 2c 20 20 20 20 20 20 pSubquery,
21c4a 2f 2a 20 41 20 73 75 62 71 75 65 72 79 20 75 73 /* A subquery us
21c4b 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 ed in place of a
21c4c 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 table name */.
21c4d 20 45 78 70 72 20 2a 70 4f 6e 2c 20 20 20 20 20 Expr *pOn,
21c4e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
21c4f 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a ON clause of a j
21c50 6f 69 6e 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 oin */. IdList
21c51 2a 70 55 73 69 6e 67 20 20 20 20 20 20 20 20 20 *pUsing
21c52 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c /* The USING cl
21c53 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a ause of a join *
21c54 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 53 72 /.){. struct Sr
21c55 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 cList_item *pIte
21c56 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 m;. sqlite3 *db
21c57 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
21c58 20 70 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c p = sqlite3SrcL
21c59 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20 70 2c istAppend(db, p,
21c5a 20 70 54 61 62 6c 65 2c 20 70 44 61 74 61 62 61 pTable, pDataba
21c5b 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 se);. if( p==0
21c5c 7c 7c 20 70 2d 3e 6e 53 72 63 3d 3d 30 20 29 7b || p->nSrc==0 ){
21c5d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 . sqlite3Expr
21c5e 44 65 6c 65 74 65 28 64 62 2c 20 70 4f 6e 29 3b Delete(db, pOn);
21c5f 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 . sqlite3IdLi
21c60 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 55 73 stDelete(db, pUs
21c61 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ing);. sqlite
21c62 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 3SelectDelete(db
21c63 2c 20 70 53 75 62 71 75 65 72 79 29 3b 0a 20 20 , pSubquery);.
21c64 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a return p;. }.
21c65 20 20 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b pItem = &p->a[
21c66 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 69 66 p->nSrc-1];. if
21c67 28 20 70 41 6c 69 61 73 20 26 26 20 70 41 6c 69 ( pAlias && pAli
21c68 61 73 2d 3e 6e 20 29 7b 0a 20 20 20 20 70 49 74 as->n ){. pIt
21c69 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c em->zAlias = sql
21c6a 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
21c6b 6e 28 64 62 2c 20 70 41 6c 69 61 73 29 3b 0a 20 n(db, pAlias);.
21c6c 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 70 53 65 6c }. pItem->pSel
21c6d 65 63 74 20 3d 20 70 53 75 62 71 75 65 72 79 3b ect = pSubquery;
21c6e 0a 20 20 70 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 . pItem->pOn =
21c6f 70 4f 6e 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 55 pOn;. pItem->pU
21c70 73 69 6e 67 20 3d 20 70 55 73 69 6e 67 3b 0a 20 sing = pUsing;.
21c71 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
21c72 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e 44 45 58 .** Add an INDEX
21c73 45 44 20 42 59 20 6f 72 20 4e 4f 54 20 49 4e 44 ED BY or NOT IND
21c74 45 58 45 44 20 63 6c 61 75 73 65 20 74 6f 20 74 EXED clause to t
21c75 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
21c76 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c 65 6d 65 added .** eleme
21c77 6e 74 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 nt of the source
21c78 2d 6c 69 73 74 20 70 61 73 73 65 64 20 61 73 20 -list passed as
21c79 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
21c7a 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ent..*/.SQLITE_P
21c7b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21c7c 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 te3SrcListIndexe
21c7d 64 42 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 dBy(Parse *pPars
21c7e 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 54 e, SrcList *p, T
21c7f 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65 64 42 79 oken *pIndexedBy
21c80 29 7b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 65 ){. if( pIndexe
21c81 64 42 79 20 26 26 20 70 20 26 26 20 70 2d 3e 6e dBy && p && p->n
21c82 53 72 63 3e 30 20 29 7b 0a 20 20 20 20 73 74 72 Src>0 ){. str
21c83 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
21c84 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b *pItem = &p->a[
21c85 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 20 20 p->nSrc-1];.
21c86 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 6e assert( pItem->n
21c87 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 26 26 20 otIndexed==0 &&
21c88 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 3d 3d 30 pItem->zIndex==0
21c89 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 64 );. if( pInd
21c8a 65 78 65 64 42 79 2d 3e 6e 3d 3d 31 20 26 26 20 exedBy->n==1 &&
21c8b 21 70 49 6e 64 65 78 65 64 42 79 2d 3e 7a 20 29 !pIndexedBy->z )
21c8c 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 22 4e 4f {. /* A "NO
21c8d 54 20 49 4e 44 45 58 45 44 22 20 63 6c 61 75 73 T INDEXED" claus
21c8e 65 20 77 61 73 20 73 75 70 70 6c 69 65 64 2e 20 e was supplied.
21c8f 53 65 65 20 70 61 72 73 65 2e 79 20 0a 20 20 20 See parse.y .
21c90 20 20 20 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20 ** construct
21c91 22 69 6e 64 65 78 65 64 5f 6f 70 74 22 20 66 6f "indexed_opt" fo
21c92 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 20 20 r details. */.
21c93 20 20 20 20 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e pItem->notIn
21c94 64 65 78 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d dexed = 1;. }
21c95 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 74 65 else{. pIte
21c96 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 6c 69 m->zIndex = sqli
21c97 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
21c98 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 6e (pParse->db, pIn
21c99 64 65 78 65 64 42 79 29 3b 0a 20 20 20 20 7d 0a dexedBy);. }.
21c9a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 }.}../*.** Whe
21c9b 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 20 61 20 n building up a
21c9c 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e 20 74 FROM clause in t
21c9d 68 65 20 70 61 72 73 65 72 2c 20 74 68 65 20 6a he parser, the j
21c9e 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 oin operator.**
21c9f 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 61 74 74 is initially att
21ca0 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65 66 ached to the lef
21ca1 74 20 6f 70 65 72 61 6e 64 2e 20 20 42 75 74 20 t operand. But
21ca2 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 the code generat
21ca3 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73 20 74 68 or.** expects th
21ca4 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 e join operator
21ca5 74 6f 20 62 65 20 6f 6e 20 74 68 65 20 72 69 67 to be on the rig
21ca6 68 74 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 ht operand. Thi
21ca7 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53 68 69 s routine.** Shi
21ca8 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f 70 65 fts all join ope
21ca9 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65 66 74 rators from left
21caa 20 74 6f 20 72 69 67 68 74 20 66 6f 72 20 61 6e to right for an
21cab 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a 2a 20 entire FROM.**
21cac 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 clause..**.** Ex
21cad 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65 20 74 ample: Suppose t
21cae 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b 65 20 he join is like
21caf 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 this:.**.**
21cb0 20 20 20 20 20 20 41 20 6e 61 74 75 72 61 6c 20 A natural
21cb1 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a 2a 0a cross join B.**.
21cb2 2a 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 ** The operator
21cb3 69 73 20 22 6e 61 74 75 72 61 6c 20 63 72 6f 73 is "natural cros
21cb4 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20 41 20 s join". The A
21cb5 61 6e 64 20 42 20 6f 70 65 72 61 6e 64 73 20 61 and B operands a
21cb6 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 re stored.** in
21cb7 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d 3e 61 p->a[0] and p->a
21cb8 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76 65 6c [1], respectivel
21cb9 79 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 69 y. The parser i
21cba 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 73 20 nitially stores
21cbb 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 the.** operator
21cbc 77 69 74 68 20 41 2e 20 20 54 68 69 73 20 72 6f with A. This ro
21cbd 75 74 69 6e 65 20 73 68 69 66 74 73 20 74 68 61 utine shifts tha
21cbe 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65 72 20 t operator over
21cbf 74 6f 20 42 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f to B..*/.SQLITE_
21cc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
21cc1 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 ite3SrcListShift
21cc2 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 JoinType(SrcList
21cc3 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 26 26 *p){. if( p &&
21cc4 20 70 2d 3e 61 20 29 7b 0a 20 20 20 20 69 6e 74 p->a ){. int
21cc5 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 70 2d i;. for(i=p-
21cc6 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20 69 2d >nSrc-1; i>0; i-
21cc7 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 5b 69 -){. p->a[i
21cc8 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 2d 3e ].jointype = p->
21cc9 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65 3b a[i-1].jointype;
21cca 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 5b . }. p->a[
21ccb 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0].jointype = 0;
21ccc 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 . }.}../*.** Be
21ccd 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f gin a transactio
21cce 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 n.*/.SQLITE_PRIV
21ccf 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
21cd0 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e BeginTransaction
21cd1 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
21cd2 69 6e 74 20 74 79 70 65 29 7b 0a 20 20 73 71 6c int type){. sql
21cd3 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 ite3 *db;. Vdbe
21cd4 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 *v;. int i;..
21cd5 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 7c if( pParse==0 |
21cd6 7c 20 28 64 62 3d 70 50 61 72 73 65 2d 3e 64 62 | (db=pParse->db
21cd7 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 44 62 5b )==0 || db->aDb[
21cd8 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 0].pBt==0 ) retu
21cd9 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 rn;. if( pParse
21cda 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 ->nErr || db->ma
21cdb 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 llocFailed ) ret
21cdc 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 urn;. if( sqlit
21cdd 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
21cde 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 se, SQLITE_TRANS
21cdf 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e 22 2c ACTION, "BEGIN",
21ce0 20 30 2c 20 30 29 20 29 20 72 65 74 75 72 6e 3b 0, 0) ) return;
21ce1 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 .. v = sqlite3G
21ce2 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
21ce3 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75 72 if( !v ) retur
21ce4 6e 3b 0a 20 20 69 66 28 20 74 79 70 65 21 3d 54 n;. if( type!=T
21ce5 4b 5f 44 45 46 45 52 52 45 44 20 29 7b 0a 20 20 K_DEFERRED ){.
21ce6 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
21ce7 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nDb; i++){.
21ce8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21ce9 4f 70 32 28 76 2c 20 4f 50 5f 54 72 61 6e 73 61 Op2(v, OP_Transa
21cea 63 74 69 6f 6e 2c 20 69 2c 20 28 74 79 70 65 3d ction, i, (type=
21ceb 3d 54 4b 5f 45 58 43 4c 55 53 49 56 45 29 2b 31 =TK_EXCLUSIVE)+1
21cec 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21ced 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c VdbeUsesBtree(v,
21cee 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 i);. }. }.
21cef 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21cf0 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d p2(v, OP_AutoCom
21cf1 6d 69 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f mit, 0, 0);.}../
21cf2 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 20 74 72 *.** Commit a tr
21cf3 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c ansaction.*/.SQL
21cf4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
21cf5 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 sqlite3CommitTr
21cf6 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 ansaction(Parse
21cf7 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 *pParse){. sqli
21cf8 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 te3 *db;. Vdbe
21cf9 2a 76 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 *v;.. if( pPars
21cfa 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 61 72 e==0 || (db=pPar
21cfb 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64 62 se->db)==0 || db
21cfc 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 ->aDb[0].pBt==0
21cfd 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
21cfe 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 pParse->nErr ||
21cff 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
21d00 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 ) return;. if(
21d01 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
21d02 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 k(pParse, SQLITE
21d03 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 43 _TRANSACTION, "C
21d04 4f 4d 4d 49 54 22 2c 20 30 2c 20 30 29 20 29 20 OMMIT", 0, 0) )
21d05 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73 return;.. v = s
21d06 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
21d07 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 arse);. if( v )
21d08 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
21d09 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 eAddOp2(v, OP_Au
21d0a 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 30 29 3b toCommit, 1, 0);
21d0b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f . }.}../*.** Ro
21d0c 6c 6c 62 61 63 6b 20 61 20 74 72 61 6e 73 61 63 llback a transac
21d0d 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 tion.*/.SQLITE_P
21d0e 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21d0f 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 te3RollbackTrans
21d10 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 action(Parse *pP
21d11 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 arse){. sqlite3
21d12 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b *db;. Vdbe *v;
21d13 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d .. if( pParse==
21d14 30 20 7c 7c 20 28 64 62 3d 70 50 61 72 73 65 2d 0 || (db=pParse-
21d15 3e 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 >db)==0 || db->a
21d16 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 Db[0].pBt==0 ) r
21d17 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 eturn;. if( pPa
21d18 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d rse->nErr || db-
21d19 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 >mallocFailed )
21d1a 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 return;. if( sq
21d1b 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 lite3AuthCheck(p
21d1c 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 Parse, SQLITE_TR
21d1d 41 4e 53 41 43 54 49 4f 4e 2c 20 22 52 4f 4c 4c ANSACTION, "ROLL
21d1e 42 41 43 4b 22 2c 20 30 2c 20 30 29 20 29 20 72 BACK", 0, 0) ) r
21d1f 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73 71 eturn;.. v = sq
21d20 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 lite3GetVdbe(pPa
21d21 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b rse);. if( v ){
21d22 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
21d23 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 AddOp2(v, OP_Aut
21d24 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 31 29 3b 0a oCommit, 1, 1);.
21d25 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
21d26 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
21d27 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 lled by the pars
21d28 65 72 20 77 68 65 6e 20 69 74 20 70 61 72 73 65 er when it parse
21d29 73 20 61 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 s a command to c
21d2a 72 65 61 74 65 2c 0a 2a 2a 20 72 65 6c 65 61 73 reate,.** releas
21d2b 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 61 6e e or rollback an
21d2c 20 53 51 4c 20 73 61 76 65 70 6f 69 6e 74 2e 20 SQL savepoint.
21d2d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
21d2e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
21d2f 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65 20 2a avepoint(Parse *
21d30 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 70 2c 20 pParse, int op,
21d31 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 Token *pName){.
21d32 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 73 char *zName = s
21d33 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
21d34 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ken(pParse->db,
21d35 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e pName);. if( zN
21d36 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 65 20 ame ){. Vdbe
21d37 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 *v = sqlite3GetV
21d38 64 62 65 28 70 50 61 72 73 65 29 3b 0a 23 69 66 dbe(pParse);.#if
21d39 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
21d3a 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 _AUTHORIZATION.
21d3b 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
21d3c 63 68 61 72 20 2a 61 7a 5b 5d 20 3d 20 7b 20 22 char *az[] = { "
21d3d 42 45 47 49 4e 22 2c 20 22 52 45 4c 45 41 53 45 BEGIN", "RELEASE
21d3e 22 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 20 7d 3b ", "ROLLBACK" };
21d3f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 53 41 . assert( !SA
21d40 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20 26 26 VEPOINT_BEGIN &&
21d41 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 SAVEPOINT_RELEA
21d42 53 45 3d 3d 31 20 26 26 20 53 41 56 45 50 4f 49 SE==1 && SAVEPOI
21d43 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3d 3d 32 20 29 NT_ROLLBACK==2 )
21d44 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 ;.#endif. if(
21d45 20 21 76 20 7c 7c 20 73 71 6c 69 74 65 33 41 75 !v || sqlite3Au
21d46 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 thCheck(pParse,
21d47 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 SQLITE_SAVEPOINT
21d48 2c 20 61 7a 5b 6f 70 5d 2c 20 7a 4e 61 6d 65 2c , az[op], zName,
21d49 20 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 0) ){. sql
21d4a 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73 ite3DbFree(pPars
21d4b 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 e->db, zName);.
21d4c 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
21d4d 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 }. sqlite3Vd
21d4e 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 beAddOp4(v, OP_S
21d4f 61 76 65 70 6f 69 6e 74 2c 20 6f 70 2c 20 30 2c avepoint, op, 0,
21d50 20 30 2c 20 7a 4e 61 6d 65 2c 20 50 34 5f 44 59 0, zName, P4_DY
21d51 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f NAMIC);. }.}../
21d52 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 *.** Make sure t
21d53 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 he TEMP database
21d54 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 61 76 61 is open and ava
21d55 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 2e 20 ilable for use.
21d56 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e Return.** the n
21d57 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e umber of errors.
21d58 20 20 4c 65 61 76 65 20 61 6e 79 20 65 72 72 6f Leave any erro
21d59 72 20 6d 65 73 73 61 67 65 73 20 69 6e 20 74 68 r messages in th
21d5a 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 e pParse structu
21d5b 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 re..*/.SQLITE_PR
21d5c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
21d5d 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 3OpenTempDatabas
21d5e 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 e(Parse *pParse)
21d5f 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
21d60 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
21d61 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 if( db->aDb[1].p
21d62 42 74 3d 3d 30 20 26 26 20 21 70 50 61 72 73 65 Bt==0 && !pParse
21d63 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 ->explain ){.
21d64 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73 74 61 int rc;. sta
21d65 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c tic const int fl
21d66 61 67 73 20 3d 20 0a 20 20 20 20 20 20 20 20 20 ags = .
21d67 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
21d68 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 20 DWRITE |.
21d69 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 SQLITE_OPEN_C
21d6a 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 REATE |.
21d6b 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 SQLITE_OPEN_EX
21d6c 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20 20 CLUSIVE |.
21d6d 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
21d6e 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a DELETEONCLOSE |.
21d6f 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
21d70 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 0a _OPEN_TEMP_DB;..
21d71 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
21d72 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c BtreeFactory(db,
21d73 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 0, 0, SQLITE_DE
21d74 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 FAULT_CACHE_SIZE
21d75 2c 20 66 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 , flags,.
21d76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21d77 20 20 20 20 20 20 20 20 20 20 26 64 62 2d 3e 61 &db->a
21d78 44 62 5b 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 Db[1].pBt);.
21d79 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
21d7a 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
21d7b 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
21d7c 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 e, "unable to op
21d7d 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 64 en a temporary d
21d7e 61 74 61 62 61 73 65 20 22 0a 20 20 20 20 20 20 atabase ".
21d7f 20 20 22 66 69 6c 65 20 66 6f 72 20 73 74 6f 72 "file for stor
21d80 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 ing temporary ta
21d81 62 6c 65 73 22 29 3b 0a 20 20 20 20 20 20 70 50 bles");. pP
21d82 61 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 arse->rc = rc;.
21d83 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
21d84 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
21d85 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 (db->flags & SQ
21d86 4c 49 54 45 5f 49 6e 54 72 61 6e 73 29 3d 3d 30 LITE_InTrans)==0
21d87 20 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d || db->autoComm
21d88 69 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 it );. assert
21d89 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 ( db->aDb[1].pSc
21d8a 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 hema );. sqli
21d8b 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d te3PagerJournalM
21d8c 6f 64 65 28 73 71 6c 69 74 65 33 42 74 72 65 65 ode(sqlite3Btree
21d8d 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 31 5d Pager(db->aDb[1]
21d8e 2e 70 42 74 29 2c 0a 20 20 20 20 20 20 20 20 20 .pBt),.
21d8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21d90 20 20 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e db->dfltJourn
21d91 61 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 72 alMode);. }. r
21d92 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
21d93 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 * Generate VDBE
21d94 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 76 code that will v
21d95 65 72 69 66 79 20 74 68 65 20 73 63 68 65 6d 61 erify the schema
21d96 20 63 6f 6f 6b 69 65 20 61 6e 64 20 73 74 61 72 cookie and star
21d97 74 0a 2a 2a 20 61 20 72 65 61 64 2d 74 72 61 6e t.** a read-tran
21d98 73 61 63 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 saction for all
21d99 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 66 named database f
21d9a 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 iles..**.** It i
21d9b 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 s important that
21d9c 20 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b all schema cook
21d9d 69 65 73 20 62 65 20 76 65 72 69 66 69 65 64 20 ies be verified
21d9e 61 6e 64 20 61 6c 6c 0a 2a 2a 20 72 65 61 64 20 and all.** read
21d9f 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 62 65 20 transactions be
21da0 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 started before a
21da1 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 68 61 70 nything else hap
21da2 70 65 6e 73 20 69 6e 0a 2a 2a 20 74 68 65 20 56 pens in.** the V
21da3 44 42 45 20 70 72 6f 67 72 61 6d 2e 20 20 42 75 DBE program. Bu
21da4 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 t this routine c
21da5 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 an be called aft
21da6 65 72 20 6d 75 63 68 20 6f 74 68 65 72 0a 2a 2a er much other.**
21da7 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e 20 67 code has been g
21da8 65 6e 65 72 61 74 65 64 2e 20 20 53 6f 20 68 65 enerated. So he
21da9 72 65 20 69 73 20 77 68 61 74 20 77 65 20 64 6f re is what we do
21daa 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 :.**.** The firs
21dab 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 t time this rout
21dac 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 ine is called, w
21dad 65 20 63 6f 64 65 20 61 6e 20 4f 50 5f 47 6f 74 e code an OP_Got
21dae 6f 20 74 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 6a o that.** will j
21daf 75 6d 70 20 74 6f 20 61 20 73 75 62 72 6f 75 74 ump to a subrout
21db0 69 6e 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f ine at the end o
21db1 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 f the program.
21db2 54 68 65 6e 20 77 65 0a 2a 2a 20 72 65 63 6f 72 Then we.** recor
21db3 64 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 d every database
21db4 20 74 68 61 74 20 6e 65 65 64 73 20 69 74 73 20 that needs its
21db5 73 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 schema verified
21db6 69 6e 20 74 68 65 0a 2a 2a 20 70 50 61 72 73 65 in the.** pParse
21db7 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 66 69 65 ->cookieMask fie
21db8 6c 64 2e 20 20 4c 61 74 65 72 2c 20 61 66 74 65 ld. Later, afte
21db9 72 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 r all other code
21dba 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 67 65 6e has been.** gen
21dbb 65 72 61 74 65 64 2c 20 74 68 65 20 73 75 62 72 erated, the subr
21dbc 6f 75 74 69 6e 65 20 74 68 61 74 20 64 6f 65 73 outine that does
21dbd 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 the cookie veri
21dbe 66 69 63 61 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a fications and.**
21dbf 20 73 74 61 72 74 73 20 74 68 65 20 74 72 61 6e starts the tran
21dc0 73 61 63 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 sactions will be
21dc1 20 63 6f 64 65 64 20 61 6e 64 20 74 68 65 20 4f coded and the O
21dc2 50 5f 47 6f 74 6f 20 50 32 20 76 61 6c 75 65 0a P_Goto P2 value.
21dc3 2a 2a 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 20 ** will be made
21dc4 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 to point to that
21dc5 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 54 68 subroutine. Th
21dc6 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 e generation of
21dc7 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 20 76 65 the.** cookie ve
21dc8 72 69 66 69 63 61 74 69 6f 6e 20 73 75 62 72 6f rification subro
21dc9 75 74 69 6e 65 20 63 6f 64 65 20 68 61 70 70 65 utine code happe
21dca 6e 73 20 69 6e 20 73 71 6c 69 74 65 33 46 69 6e ns in sqlite3Fin
21dcb 69 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a 2a 0a ishCoding()..**.
21dcc 2a 2a 20 49 66 20 69 44 62 3c 30 20 74 68 65 6e ** If iDb<0 then
21dcd 20 63 6f 64 65 20 74 68 65 20 4f 50 5f 47 6f 74 code the OP_Got
21dce 6f 20 6f 6e 6c 79 20 2d 20 64 6f 6e 27 74 20 73 o only - don't s
21dcf 65 74 20 66 6c 61 67 20 74 6f 20 76 65 72 69 66 et flag to verif
21dd0 79 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 y the.** schema
21dd1 6f 6e 20 61 6e 79 20 64 61 74 61 62 61 73 65 73 on any databases
21dd2 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
21dd3 73 65 64 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20 sed to position
21dd4 74 68 65 20 4f 50 5f 47 6f 74 6f 0a 2a 2a 20 65 the OP_Goto.** e
21dd5 61 72 6c 79 20 69 6e 20 74 68 65 20 63 6f 64 65 arly in the code
21dd6 2c 20 62 65 66 6f 72 65 20 77 65 20 6b 6e 6f 77 , before we know
21dd7 20 69 66 20 61 6e 79 20 64 61 74 61 62 61 73 65 if any database
21dd8 20 74 61 62 6c 65 73 20 77 69 6c 6c 20 62 65 20 tables will be
21dd9 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f used..*/.SQLITE_
21dda 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
21ddb 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 ite3CodeVerifySc
21ddc 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 hema(Parse *pPar
21ddd 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 se, int iDb){.
21dde 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 sqlite3 *db;. V
21ddf 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 6d 61 dbe *v;. int ma
21de0 73 6b 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 sk;.. v = sqlit
21de1 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
21de2 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 );. if( v==0 )
21de3 72 65 74 75 72 6e 3b 20 20 2f 2a 20 54 68 69 73 return; /* This
21de4 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 66 only happens if
21de5 20 74 68 65 72 65 20 77 61 73 20 61 20 70 72 69 there was a pri
21de6 6f 72 20 65 72 72 6f 72 20 2a 2f 0a 20 20 64 62 or error */. db
21de7 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
21de8 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63 6f 6f if( pParse->coo
21de9 6b 69 65 47 6f 74 6f 3d 3d 30 20 29 7b 0a 20 20 kieGoto==0 ){.
21dea 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 pParse->cookie
21deb 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 Goto = sqlite3Vd
21dec 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 beAddOp2(v, OP_G
21ded 6f 74 6f 2c 20 30 2c 20 30 29 2b 31 3b 0a 20 20 oto, 0, 0)+1;.
21dee 7d 0a 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29 }. if( iDb>=0 )
21def 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 {. assert( iD
21df0 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 b<db->nDb );.
21df1 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 assert( db->aDb
21df2 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 7c 7c 20 [iDb].pBt!=0 ||
21df3 69 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 iDb==1 );. as
21df4 73 65 72 74 28 20 69 44 62 3c 53 51 4c 49 54 45 sert( iDb<SQLITE
21df5 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32 20 _MAX_ATTACHED+2
21df6 29 3b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 31 3c );. mask = 1<
21df7 3c 69 44 62 3b 0a 20 20 20 20 69 66 28 20 28 70 <iDb;. if( (p
21df8 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 Parse->cookieMas
21df9 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a k & mask)==0 ){.
21dfa 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f pParse->co
21dfb 6f 6b 69 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b okieMask |= mask
21dfc 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e ;. pParse->
21dfd 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d cookieValue[iDb]
21dfe 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
21dff 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f pSchema->schema_
21e00 63 6f 6f 6b 69 65 3b 0a 20 20 20 20 20 20 69 66 cookie;. if
21e01 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 ( !OMIT_TEMPDB &
21e02 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 & iDb==1 ){.
21e03 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 sqlite3OpenT
21e04 65 6d 70 44 61 74 61 62 61 73 65 28 70 50 61 72 empDatabase(pPar
21e05 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 se);. }.
21e06 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
21e07 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f Generate VDBE co
21e08 64 65 20 74 68 61 74 20 70 72 65 70 61 72 65 73 de that prepares
21e09 20 66 6f 72 20 64 6f 69 6e 67 20 61 6e 20 6f 70 for doing an op
21e0a 65 72 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 eration that.**
21e0b 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 65 might change the
21e0c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a database..**.**
21e0d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 This routine st
21e0e 61 72 74 73 20 61 20 6e 65 77 20 74 72 61 6e 73 arts a new trans
21e0f 61 63 74 69 6f 6e 20 69 66 20 77 65 20 61 72 65 action if we are
21e10 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 77 69 74 not already wit
21e11 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 hin.** a transac
21e12 74 69 6f 6e 2e 20 20 49 66 20 77 65 20 61 72 65 tion. If we are
21e13 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 already within
21e14 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 a transaction, t
21e15 68 65 6e 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 hen a checkpoint
21e16 0a 2a 2a 20 69 73 20 73 65 74 20 69 66 20 74 68 .** is set if th
21e17 65 20 73 65 74 53 74 61 74 65 6d 65 6e 74 20 70 e setStatement p
21e18 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 arameter is true
21e19 2e 20 20 41 20 63 68 65 63 6b 70 6f 69 6e 74 20 . A checkpoint
21e1a 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 65 74 should.** be set
21e1b 20 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 for operations
21e1c 74 68 61 74 20 6d 69 67 68 74 20 66 61 69 6c 20 that might fail
21e1d 28 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74 72 (due to a constr
21e1e 61 69 6e 74 29 20 70 61 72 74 20 6f 66 0a 2a 2a aint) part of.**
21e1f 20 74 68 65 20 77 61 79 20 74 68 72 6f 75 67 68 the way through
21e20 20 61 6e 64 20 77 68 69 63 68 20 77 69 6c 6c 20 and which will
21e21 6e 65 65 64 20 74 6f 20 75 6e 64 6f 20 73 6f 6d need to undo som
21e22 65 20 77 72 69 74 65 73 20 77 69 74 68 6f 75 74 e writes without
21e23 20 68 61 76 69 6e 67 20 74 6f 0a 2a 2a 20 72 6f having to.** ro
21e24 6c 6c 62 61 63 6b 20 74 68 65 20 77 68 6f 6c 65 llback the whole
21e25 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 46 transaction. F
21e26 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 77 68 or operations wh
21e27 65 72 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 ere all constrai
21e28 6e 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 63 68 nts.** can be ch
21e29 65 63 6b 65 64 20 62 65 66 6f 72 65 20 61 6e 79 ecked before any
21e2a 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 changes are mad
21e2b 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 e to the databas
21e2c 65 2c 20 69 74 20 69 73 20 6e 65 76 65 72 0a 2a e, it is never.*
21e2d 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 75 * necessary to u
21e2e 6e 64 6f 20 61 20 77 72 69 74 65 20 61 6e 64 20 ndo a write and
21e2f 74 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 the checkpoint s
21e30 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 65 74 hould not be set
21e31 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 64 61 74 ..**.** Only dat
21e32 61 62 61 73 65 20 69 44 62 20 61 6e 64 20 74 68 abase iDb and th
21e33 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 20 e temp database
21e34 61 72 65 20 6d 61 64 65 20 77 72 69 74 61 62 6c are made writabl
21e35 65 20 62 79 20 74 68 69 73 20 63 61 6c 6c 2e 0a e by this call..
21e36 2a 2a 20 49 66 20 69 44 62 3d 3d 30 2c 20 74 68 ** If iDb==0, th
21e37 65 6e 20 74 68 65 20 6d 61 69 6e 20 61 6e 64 20 en the main and
21e38 74 65 6d 70 20 64 61 74 61 62 61 73 65 73 20 61 temp databases a
21e39 72 65 20 6d 61 64 65 20 77 72 69 74 61 62 6c 65 re made writable
21e3a 2e 20 20 20 49 66 0a 2a 2a 20 69 44 62 3d 3d 31 . If.** iDb==1
21e3b 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 20 74 then only the t
21e3c 65 6d 70 20 64 61 74 61 62 61 73 65 20 69 73 20 emp database is
21e3d 6d 61 64 65 20 77 72 69 74 61 62 6c 65 2e 20 20 made writable.
21e3e 49 66 20 69 44 62 3e 31 20 74 68 65 6e 20 74 68 If iDb>1 then th
21e3f 65 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 61 e.** specified a
21e40 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 uxiliary databas
21e41 65 20 61 6e 64 20 74 68 65 20 74 65 6d 70 20 64 e and the temp d
21e42 61 74 61 62 61 73 65 20 61 72 65 20 6d 61 64 65 atabase are made
21e43 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 writable..*/.SQ
21e44 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
21e45 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 d sqlite3BeginWr
21e46 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 iteOperation(Par
21e47 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
21e48 73 65 74 53 74 61 74 65 6d 65 6e 74 2c 20 69 6e setStatement, in
21e49 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a t iDb){. Vdbe *
21e4a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
21e4b 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 be(pParse);. if
21e4c 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( v==0 ) return;
21e4d 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 . sqlite3CodeVe
21e4e 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 rifySchema(pPars
21e4f 65 2c 20 69 44 62 29 3b 0a 20 20 70 50 61 72 73 e, iDb);. pPars
21e50 65 2d 3e 77 72 69 74 65 4d 61 73 6b 20 7c 3d 20 e->writeMask |=
21e51 31 3c 3c 69 44 62 3b 0a 20 20 69 66 28 20 73 65 1<<iDb;. if( se
21e52 74 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70 50 tStatement && pP
21e53 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 arse->nested==0
21e54 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
21e55 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 53 beAddOp1(v, OP_S
21e56 74 61 74 65 6d 65 6e 74 2c 20 69 44 62 29 3b 0a tatement, iDb);.
21e57 20 20 7d 0a 20 20 69 66 28 20 28 4f 4d 49 54 5f }. if( (OMIT_
21e58 54 45 4d 50 44 42 20 7c 7c 20 69 44 62 21 3d 31 TEMPDB || iDb!=1
21e59 29 20 26 26 20 70 50 61 72 73 65 2d 3e 64 62 2d ) && pParse->db-
21e5a 3e 61 44 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29 >aDb[1].pBt!=0 )
21e5b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 {. sqlite3Beg
21e5c 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e inWriteOperation
21e5d 28 70 50 61 72 73 65 2c 20 73 65 74 53 74 61 74 (pParse, setStat
21e5e 65 6d 65 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 7d ement, 1);. }.}
21e5f 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f ../*.** Check to
21e60 20 73 65 65 20 69 66 20 70 49 6e 64 65 78 20 75 see if pIndex u
21e61 73 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e ses the collatin
21e62 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c g sequence pColl
21e63 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 . Return.** tru
21e64 65 20 69 66 20 69 74 20 64 6f 65 73 20 61 6e 64 e if it does and
21e65 20 66 61 6c 73 65 20 69 66 20 69 74 20 64 6f 65 false if it doe
21e66 73 20 6e 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 s not..*/.#ifnde
21e67 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 f SQLITE_OMIT_RE
21e68 49 4e 44 45 58 0a 73 74 61 74 69 63 20 69 6e 74 INDEX.static int
21e69 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 collationMatch(
21e6a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c const char *zCol
21e6b 6c 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 l, Index *pIndex
21e6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f ){. int i;. fo
21e6d 72 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65 78 2d r(i=0; i<pIndex-
21e6e 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a >nColumn; i++){.
21e6f 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
21e70 7a 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f z = pIndex->azCo
21e71 6c 6c 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 7a ll[i];. if( z
21e72 3d 3d 7a 43 6f 6c 6c 20 7c 7c 20 28 7a 20 26 26 ==zColl || (z &&
21e73 20 7a 43 6f 6c 6c 20 26 26 20 30 3d 3d 73 71 6c zColl && 0==sql
21e74 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a ite3StrICmp(z, z
21e75 43 6f 6c 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 Coll)) ){.
21e76 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
21e77 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
21e78 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
21e79 52 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e Recompute all in
21e7a 64 69 63 65 73 20 6f 66 20 70 54 61 62 20 74 68 dices of pTab th
21e7b 61 74 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 at use the colla
21e7c 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 ting sequence pC
21e7d 6f 6c 6c 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c oll..** If pColl
21e7e 3d 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 ==0 then recompu
21e7f 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f te all indices o
21e80 66 20 70 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 f pTab..*/.#ifnd
21e81 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 e